linux/drivers/scsi/bfa/bfad.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
   3 * All rights reserved
   4 * www.brocade.com
   5 *
   6 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
   7 *
   8 * This program is free software; you can redistribute it and/or modify it
   9 * under the terms of the GNU General Public License (GPL) Version 2 as
  10 * published by the Free Software Foundation
  11 *
  12 * This program is distributed in the hope that it will be useful, but
  13 * WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15 * General Public License for more details.
  16 */
  17
  18/*
  19 *  bfad.c Linux driver PCI interface module.
  20 */
  21#include <linux/module.h>
  22#include <linux/kthread.h>
  23#include <linux/errno.h>
  24#include <linux/sched.h>
  25#include <linux/init.h>
  26#include <linux/fs.h>
  27#include <linux/pci.h>
  28#include <linux/firmware.h>
  29#include <asm/uaccess.h>
  30#include <asm/fcntl.h>
  31
  32#include "bfad_drv.h"
  33#include "bfad_im.h"
  34#include "bfa_fcs.h"
  35#include "bfa_defs.h"
  36#include "bfa.h"
  37
  38BFA_TRC_FILE(LDRV, BFAD);
  39DEFINE_MUTEX(bfad_mutex);
  40LIST_HEAD(bfad_list);
  41
  42static int      bfad_inst;
  43static int      num_sgpgs_parm;
  44int             supported_fc4s;
  45char            *host_name, *os_name, *os_patch;
  46int             num_rports, num_ios, num_tms;
  47int             num_fcxps, num_ufbufs;
  48int             reqq_size, rspq_size, num_sgpgs;
  49int             rport_del_timeout = BFA_FCS_RPORT_DEF_DEL_TIMEOUT;
  50int             bfa_lun_queue_depth = BFAD_LUN_QUEUE_DEPTH;
  51int             bfa_io_max_sge = BFAD_IO_MAX_SGE;
  52int             bfa_log_level = 3; /* WARNING log level */
  53int             ioc_auto_recover = BFA_TRUE;
  54int             bfa_linkup_delay = -1;
  55int             fdmi_enable = BFA_TRUE;
  56int             pcie_max_read_reqsz;
  57int             bfa_debugfs_enable = 1;
  58int             msix_disable_cb = 0, msix_disable_ct = 0;
  59int             max_xfer_size = BFAD_MAX_SECTORS >> 1;
  60int             max_rport_logins = BFA_FCS_MAX_RPORT_LOGINS;
  61
  62/* Firmware releated */
  63u32     bfi_image_cb_size, bfi_image_ct_size, bfi_image_ct2_size;
  64u32     *bfi_image_cb, *bfi_image_ct, *bfi_image_ct2;
  65
  66#define BFAD_FW_FILE_CB         "cbfw-3.2.1.1.bin"
  67#define BFAD_FW_FILE_CT         "ctfw-3.2.1.1.bin"
  68#define BFAD_FW_FILE_CT2        "ct2fw-3.2.1.1.bin"
  69
  70static u32 *bfad_load_fwimg(struct pci_dev *pdev);
  71static void bfad_free_fwimg(void);
  72static void bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
  73                u32 *bfi_image_size, char *fw_name);
  74
  75static const char *msix_name_ct[] = {
  76        "ctrl",
  77        "cpe0", "cpe1", "cpe2", "cpe3",
  78        "rme0", "rme1", "rme2", "rme3" };
  79
  80static const char *msix_name_cb[] = {
  81        "cpe0", "cpe1", "cpe2", "cpe3",
  82        "rme0", "rme1", "rme2", "rme3",
  83        "eemc", "elpu0", "elpu1", "epss", "mlpu" };
  84
  85MODULE_FIRMWARE(BFAD_FW_FILE_CB);
  86MODULE_FIRMWARE(BFAD_FW_FILE_CT);
  87MODULE_FIRMWARE(BFAD_FW_FILE_CT2);
  88
  89module_param(os_name, charp, S_IRUGO | S_IWUSR);
  90MODULE_PARM_DESC(os_name, "OS name of the hba host machine");
  91module_param(os_patch, charp, S_IRUGO | S_IWUSR);
  92MODULE_PARM_DESC(os_patch, "OS patch level of the hba host machine");
  93module_param(host_name, charp, S_IRUGO | S_IWUSR);
  94MODULE_PARM_DESC(host_name, "Hostname of the hba host machine");
  95module_param(num_rports, int, S_IRUGO | S_IWUSR);
  96MODULE_PARM_DESC(num_rports, "Max number of rports supported per port "
  97                                "(physical/logical), default=1024");
  98module_param(num_ios, int, S_IRUGO | S_IWUSR);
  99MODULE_PARM_DESC(num_ios, "Max number of ioim requests, default=2000");
 100module_param(num_tms, int, S_IRUGO | S_IWUSR);
 101MODULE_PARM_DESC(num_tms, "Max number of task im requests, default=128");
 102module_param(num_fcxps, int, S_IRUGO | S_IWUSR);
 103MODULE_PARM_DESC(num_fcxps, "Max number of fcxp requests, default=64");
 104module_param(num_ufbufs, int, S_IRUGO | S_IWUSR);
 105MODULE_PARM_DESC(num_ufbufs, "Max number of unsolicited frame "
 106                                "buffers, default=64");
 107module_param(reqq_size, int, S_IRUGO | S_IWUSR);
 108MODULE_PARM_DESC(reqq_size, "Max number of request queue elements, "
 109                                "default=256");
 110module_param(rspq_size, int, S_IRUGO | S_IWUSR);
 111MODULE_PARM_DESC(rspq_size, "Max number of response queue elements, "
 112                                "default=64");
 113module_param(num_sgpgs, int, S_IRUGO | S_IWUSR);
 114MODULE_PARM_DESC(num_sgpgs, "Number of scatter/gather pages, default=2048");
 115module_param(rport_del_timeout, int, S_IRUGO | S_IWUSR);
 116MODULE_PARM_DESC(rport_del_timeout, "Rport delete timeout, default=90 secs, "
 117                                        "Range[>0]");
 118module_param(bfa_lun_queue_depth, int, S_IRUGO | S_IWUSR);
 119MODULE_PARM_DESC(bfa_lun_queue_depth, "Lun queue depth, default=32, Range[>0]");
 120module_param(bfa_io_max_sge, int, S_IRUGO | S_IWUSR);
 121MODULE_PARM_DESC(bfa_io_max_sge, "Max io scatter/gather elements, default=255");
 122module_param(bfa_log_level, int, S_IRUGO | S_IWUSR);
 123MODULE_PARM_DESC(bfa_log_level, "Driver log level, default=3, "
 124                                "Range[Critical:1|Error:2|Warning:3|Info:4]");
 125module_param(ioc_auto_recover, int, S_IRUGO | S_IWUSR);
 126MODULE_PARM_DESC(ioc_auto_recover, "IOC auto recovery, default=1, "
 127                                "Range[off:0|on:1]");
 128module_param(bfa_linkup_delay, int, S_IRUGO | S_IWUSR);
 129MODULE_PARM_DESC(bfa_linkup_delay, "Link up delay, default=30 secs for "
 130                        "boot port. Otherwise 10 secs in RHEL4 & 0 for "
 131                        "[RHEL5, SLES10, ESX40] Range[>0]");
 132module_param(msix_disable_cb, int, S_IRUGO | S_IWUSR);
 133MODULE_PARM_DESC(msix_disable_cb, "Disable Message Signaled Interrupts "
 134                        "for Brocade-415/425/815/825 cards, default=0, "
 135                        " Range[false:0|true:1]");
 136module_param(msix_disable_ct, int, S_IRUGO | S_IWUSR);
 137MODULE_PARM_DESC(msix_disable_ct, "Disable Message Signaled Interrupts "
 138                        "if possible for Brocade-1010/1020/804/1007/902/1741 "
 139                        "cards, default=0, Range[false:0|true:1]");
 140module_param(fdmi_enable, int, S_IRUGO | S_IWUSR);
 141MODULE_PARM_DESC(fdmi_enable, "Enables fdmi registration, default=1, "
 142                                "Range[false:0|true:1]");
 143module_param(pcie_max_read_reqsz, int, S_IRUGO | S_IWUSR);
 144MODULE_PARM_DESC(pcie_max_read_reqsz, "PCIe max read request size, default=0 "
 145                "(use system setting), Range[128|256|512|1024|2048|4096]");
 146module_param(bfa_debugfs_enable, int, S_IRUGO | S_IWUSR);
 147MODULE_PARM_DESC(bfa_debugfs_enable, "Enables debugfs feature, default=1,"
 148                " Range[false:0|true:1]");
 149module_param(max_xfer_size, int, S_IRUGO | S_IWUSR);
 150MODULE_PARM_DESC(max_xfer_size, "default=32MB,"
 151                " Range[64k|128k|256k|512k|1024k|2048k]");
 152module_param(max_rport_logins, int, S_IRUGO | S_IWUSR);
 153MODULE_PARM_DESC(max_rport_logins, "Max number of logins to initiator and target rports on a port (physical/logical), default=1024");
 154
 155static void
 156bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event);
 157static void
 158bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event);
 159static void
 160bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event);
 161static void
 162bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event);
 163static void
 164bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event);
 165static void
 166bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event);
 167static void
 168bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event);
 169
 170/*
 171 * Beginning state for the driver instance, awaiting the pci_probe event
 172 */
 173static void
 174bfad_sm_uninit(struct bfad_s *bfad, enum bfad_sm_event event)
 175{
 176        bfa_trc(bfad, event);
 177
 178        switch (event) {
 179        case BFAD_E_CREATE:
 180                bfa_sm_set_state(bfad, bfad_sm_created);
 181                bfad->bfad_tsk = kthread_create(bfad_worker, (void *) bfad,
 182                                                "%s", "bfad_worker");
 183                if (IS_ERR(bfad->bfad_tsk)) {
 184                        printk(KERN_INFO "bfad[%d]: Kernel thread "
 185                                "creation failed!\n", bfad->inst_no);
 186                        bfa_sm_send_event(bfad, BFAD_E_KTHREAD_CREATE_FAILED);
 187                }
 188                bfa_sm_send_event(bfad, BFAD_E_INIT);
 189                break;
 190
 191        case BFAD_E_STOP:
 192                /* Ignore stop; already in uninit */
 193                break;
 194
 195        default:
 196                bfa_sm_fault(bfad, event);
 197        }
 198}
 199
 200/*
 201 * Driver Instance is created, awaiting event INIT to initialize the bfad
 202 */
 203static void
 204bfad_sm_created(struct bfad_s *bfad, enum bfad_sm_event event)
 205{
 206        unsigned long flags;
 207
 208        bfa_trc(bfad, event);
 209
 210        switch (event) {
 211        case BFAD_E_INIT:
 212                bfa_sm_set_state(bfad, bfad_sm_initializing);
 213
 214                init_completion(&bfad->comp);
 215
 216                /* Enable Interrupt and wait bfa_init completion */
 217                if (bfad_setup_intr(bfad)) {
 218                        printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
 219                                        bfad->inst_no);
 220                        bfa_sm_send_event(bfad, BFAD_E_INTR_INIT_FAILED);
 221                        break;
 222                }
 223
 224                spin_lock_irqsave(&bfad->bfad_lock, flags);
 225                bfa_iocfc_init(&bfad->bfa);
 226                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 227
 228                /* Set up interrupt handler for each vectors */
 229                if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
 230                        bfad_install_msix_handler(bfad)) {
 231                        printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
 232                                __func__, bfad->inst_no);
 233                }
 234
 235                bfad_init_timer(bfad);
 236
 237                wait_for_completion(&bfad->comp);
 238
 239                if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
 240                        bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
 241                } else {
 242                        printk(KERN_WARNING
 243                                "bfa %s: bfa init failed\n",
 244                                bfad->pci_name);
 245                        bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
 246                        bfa_sm_send_event(bfad, BFAD_E_INIT_FAILED);
 247                }
 248
 249                break;
 250
 251        case BFAD_E_KTHREAD_CREATE_FAILED:
 252                bfa_sm_set_state(bfad, bfad_sm_uninit);
 253                break;
 254
 255        default:
 256                bfa_sm_fault(bfad, event);
 257        }
 258}
 259
 260static void
 261bfad_sm_initializing(struct bfad_s *bfad, enum bfad_sm_event event)
 262{
 263        int     retval;
 264        unsigned long   flags;
 265
 266        bfa_trc(bfad, event);
 267
 268        switch (event) {
 269        case BFAD_E_INIT_SUCCESS:
 270                kthread_stop(bfad->bfad_tsk);
 271                spin_lock_irqsave(&bfad->bfad_lock, flags);
 272                bfad->bfad_tsk = NULL;
 273                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 274
 275                retval = bfad_start_ops(bfad);
 276                if (retval != BFA_STATUS_OK)
 277                        break;
 278                bfa_sm_set_state(bfad, bfad_sm_operational);
 279                break;
 280
 281        case BFAD_E_INTR_INIT_FAILED:
 282                bfa_sm_set_state(bfad, bfad_sm_uninit);
 283                kthread_stop(bfad->bfad_tsk);
 284                spin_lock_irqsave(&bfad->bfad_lock, flags);
 285                bfad->bfad_tsk = NULL;
 286                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 287                break;
 288
 289        case BFAD_E_INIT_FAILED:
 290                bfa_sm_set_state(bfad, bfad_sm_failed);
 291                break;
 292        default:
 293                bfa_sm_fault(bfad, event);
 294        }
 295}
 296
 297static void
 298bfad_sm_failed(struct bfad_s *bfad, enum bfad_sm_event event)
 299{
 300        int     retval;
 301
 302        bfa_trc(bfad, event);
 303
 304        switch (event) {
 305        case BFAD_E_INIT_SUCCESS:
 306                retval = bfad_start_ops(bfad);
 307                if (retval != BFA_STATUS_OK)
 308                        break;
 309                bfa_sm_set_state(bfad, bfad_sm_operational);
 310                break;
 311
 312        case BFAD_E_STOP:
 313                if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
 314                        bfad_uncfg_pport(bfad);
 315                if (bfad->bfad_flags & BFAD_FC4_PROBE_DONE) {
 316                        bfad_im_probe_undo(bfad);
 317                        bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
 318                }
 319                bfad_stop(bfad);
 320                break;
 321
 322        case BFAD_E_EXIT_COMP:
 323                bfa_sm_set_state(bfad, bfad_sm_uninit);
 324                bfad_remove_intr(bfad);
 325                del_timer_sync(&bfad->hal_tmo);
 326                break;
 327
 328        default:
 329                bfa_sm_fault(bfad, event);
 330        }
 331}
 332
 333static void
 334bfad_sm_operational(struct bfad_s *bfad, enum bfad_sm_event event)
 335{
 336        bfa_trc(bfad, event);
 337
 338        switch (event) {
 339        case BFAD_E_STOP:
 340                bfa_sm_set_state(bfad, bfad_sm_fcs_exit);
 341                bfad_fcs_stop(bfad);
 342                break;
 343
 344        default:
 345                bfa_sm_fault(bfad, event);
 346        }
 347}
 348
 349static void
 350bfad_sm_fcs_exit(struct bfad_s *bfad, enum bfad_sm_event event)
 351{
 352        bfa_trc(bfad, event);
 353
 354        switch (event) {
 355        case BFAD_E_FCS_EXIT_COMP:
 356                bfa_sm_set_state(bfad, bfad_sm_stopping);
 357                bfad_stop(bfad);
 358                break;
 359
 360        default:
 361                bfa_sm_fault(bfad, event);
 362        }
 363}
 364
 365static void
 366bfad_sm_stopping(struct bfad_s *bfad, enum bfad_sm_event event)
 367{
 368        bfa_trc(bfad, event);
 369
 370        switch (event) {
 371        case BFAD_E_EXIT_COMP:
 372                bfa_sm_set_state(bfad, bfad_sm_uninit);
 373                bfad_remove_intr(bfad);
 374                del_timer_sync(&bfad->hal_tmo);
 375                bfad_im_probe_undo(bfad);
 376                bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
 377                bfad_uncfg_pport(bfad);
 378                break;
 379
 380        default:
 381                bfa_sm_fault(bfad, event);
 382                break;
 383        }
 384}
 385
 386/*
 387 *  BFA callbacks
 388 */
 389void
 390bfad_hcb_comp(void *arg, bfa_status_t status)
 391{
 392        struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
 393
 394        fcomp->status = status;
 395        complete(&fcomp->comp);
 396}
 397
 398/*
 399 * bfa_init callback
 400 */
 401void
 402bfa_cb_init(void *drv, bfa_status_t init_status)
 403{
 404        struct bfad_s         *bfad = drv;
 405
 406        if (init_status == BFA_STATUS_OK) {
 407                bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
 408
 409                /*
 410                 * If BFAD_HAL_INIT_FAIL flag is set:
 411                 * Wake up the kernel thread to start
 412                 * the bfad operations after HAL init done
 413                 */
 414                if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
 415                        bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
 416                        wake_up_process(bfad->bfad_tsk);
 417                }
 418        }
 419
 420        complete(&bfad->comp);
 421}
 422
 423/*
 424 *  BFA_FCS callbacks
 425 */
 426struct bfad_port_s *
 427bfa_fcb_lport_new(struct bfad_s *bfad, struct bfa_fcs_lport_s *port,
 428                 enum bfa_lport_role roles, struct bfad_vf_s *vf_drv,
 429                 struct bfad_vport_s *vp_drv)
 430{
 431        bfa_status_t    rc;
 432        struct bfad_port_s    *port_drv;
 433
 434        if (!vp_drv && !vf_drv) {
 435                port_drv = &bfad->pport;
 436                port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
 437        } else if (!vp_drv && vf_drv) {
 438                port_drv = &vf_drv->base_port;
 439                port_drv->pvb_type = BFAD_PORT_VF_BASE;
 440        } else if (vp_drv && !vf_drv) {
 441                port_drv = &vp_drv->drv_port;
 442                port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
 443        } else {
 444                port_drv = &vp_drv->drv_port;
 445                port_drv->pvb_type = BFAD_PORT_VF_VPORT;
 446        }
 447
 448        port_drv->fcs_port = port;
 449        port_drv->roles = roles;
 450
 451        if (roles & BFA_LPORT_ROLE_FCP_IM) {
 452                rc = bfad_im_port_new(bfad, port_drv);
 453                if (rc != BFA_STATUS_OK) {
 454                        bfad_im_port_delete(bfad, port_drv);
 455                        port_drv = NULL;
 456                }
 457        }
 458
 459        return port_drv;
 460}
 461
 462/*
 463 * FCS RPORT alloc callback, after successful PLOGI by FCS
 464 */
 465bfa_status_t
 466bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
 467                    struct bfad_rport_s **rport_drv)
 468{
 469        bfa_status_t    rc = BFA_STATUS_OK;
 470
 471        *rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
 472        if (*rport_drv == NULL) {
 473                rc = BFA_STATUS_ENOMEM;
 474                goto ext;
 475        }
 476
 477        *rport = &(*rport_drv)->fcs_rport;
 478
 479ext:
 480        return rc;
 481}
 482
 483/*
 484 * FCS PBC VPORT Create
 485 */
 486void
 487bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
 488{
 489
 490        struct bfa_lport_cfg_s port_cfg = {0};
 491        struct bfad_vport_s   *vport;
 492        int rc;
 493
 494        vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
 495        if (!vport) {
 496                bfa_trc(bfad, 0);
 497                return;
 498        }
 499
 500        vport->drv_port.bfad = bfad;
 501        port_cfg.roles = BFA_LPORT_ROLE_FCP_IM;
 502        port_cfg.pwwn = pbc_vport.vp_pwwn;
 503        port_cfg.nwwn = pbc_vport.vp_nwwn;
 504        port_cfg.preboot_vp  = BFA_TRUE;
 505
 506        rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, 0,
 507                                  &port_cfg, vport);
 508
 509        if (rc != BFA_STATUS_OK) {
 510                bfa_trc(bfad, 0);
 511                return;
 512        }
 513
 514        list_add_tail(&vport->list_entry, &bfad->pbc_vport_list);
 515}
 516
 517void
 518bfad_hal_mem_release(struct bfad_s *bfad)
 519{
 520        struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
 521        struct bfa_mem_dma_s *dma_info, *dma_elem;
 522        struct bfa_mem_kva_s *kva_info, *kva_elem;
 523        struct list_head *dm_qe, *km_qe;
 524
 525        dma_info = &hal_meminfo->dma_info;
 526        kva_info = &hal_meminfo->kva_info;
 527
 528        /* Iterate through the KVA meminfo queue */
 529        list_for_each(km_qe, &kva_info->qe) {
 530                kva_elem = (struct bfa_mem_kva_s *) km_qe;
 531                vfree(kva_elem->kva);
 532        }
 533
 534        /* Iterate through the DMA meminfo queue */
 535        list_for_each(dm_qe, &dma_info->qe) {
 536                dma_elem = (struct bfa_mem_dma_s *) dm_qe;
 537                dma_free_coherent(&bfad->pcidev->dev,
 538                                dma_elem->mem_len, dma_elem->kva,
 539                                (dma_addr_t) dma_elem->dma);
 540        }
 541
 542        memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
 543}
 544
 545void
 546bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
 547{
 548        if (num_rports > 0)
 549                bfa_cfg->fwcfg.num_rports = num_rports;
 550        if (num_ios > 0)
 551                bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
 552        if (num_tms > 0)
 553                bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
 554        if (num_fcxps > 0 && num_fcxps <= BFA_FCXP_MAX)
 555                bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
 556        if (num_ufbufs > 0 && num_ufbufs <= BFA_UF_MAX)
 557                bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
 558        if (reqq_size > 0)
 559                bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
 560        if (rspq_size > 0)
 561                bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
 562        if (num_sgpgs > 0 && num_sgpgs <= BFA_SGPG_MAX)
 563                bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;
 564
 565        /*
 566         * populate the hal values back to the driver for sysfs use.
 567         * otherwise, the default values will be shown as 0 in sysfs
 568         */
 569        num_rports = bfa_cfg->fwcfg.num_rports;
 570        num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
 571        num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
 572        num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
 573        num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
 574        reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
 575        rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
 576        num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
 577}
 578
 579bfa_status_t
 580bfad_hal_mem_alloc(struct bfad_s *bfad)
 581{
 582        struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
 583        struct bfa_mem_dma_s *dma_info, *dma_elem;
 584        struct bfa_mem_kva_s *kva_info, *kva_elem;
 585        struct list_head *dm_qe, *km_qe;
 586        bfa_status_t    rc = BFA_STATUS_OK;
 587        dma_addr_t      phys_addr;
 588
 589        bfa_cfg_get_default(&bfad->ioc_cfg);
 590        bfad_update_hal_cfg(&bfad->ioc_cfg);
 591        bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
 592        bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo, &bfad->bfa);
 593
 594        dma_info = &hal_meminfo->dma_info;
 595        kva_info = &hal_meminfo->kva_info;
 596
 597        /* Iterate through the KVA meminfo queue */
 598        list_for_each(km_qe, &kva_info->qe) {
 599                kva_elem = (struct bfa_mem_kva_s *) km_qe;
 600                kva_elem->kva = vmalloc(kva_elem->mem_len);
 601                if (kva_elem->kva == NULL) {
 602                        bfad_hal_mem_release(bfad);
 603                        rc = BFA_STATUS_ENOMEM;
 604                        goto ext;
 605                }
 606                memset(kva_elem->kva, 0, kva_elem->mem_len);
 607        }
 608
 609        /* Iterate through the DMA meminfo queue */
 610        list_for_each(dm_qe, &dma_info->qe) {
 611                dma_elem = (struct bfa_mem_dma_s *) dm_qe;
 612                dma_elem->kva = dma_alloc_coherent(&bfad->pcidev->dev,
 613                                                dma_elem->mem_len,
 614                                                &phys_addr, GFP_KERNEL);
 615                if (dma_elem->kva == NULL) {
 616                        bfad_hal_mem_release(bfad);
 617                        rc = BFA_STATUS_ENOMEM;
 618                        goto ext;
 619                }
 620                dma_elem->dma = phys_addr;
 621                memset(dma_elem->kva, 0, dma_elem->mem_len);
 622        }
 623ext:
 624        return rc;
 625}
 626
 627/*
 628 * Create a vport under a vf.
 629 */
 630bfa_status_t
 631bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
 632                  struct bfa_lport_cfg_s *port_cfg, struct device *dev)
 633{
 634        struct bfad_vport_s   *vport;
 635        int             rc = BFA_STATUS_OK;
 636        unsigned long   flags;
 637        struct completion fcomp;
 638
 639        vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
 640        if (!vport) {
 641                rc = BFA_STATUS_ENOMEM;
 642                goto ext;
 643        }
 644
 645        vport->drv_port.bfad = bfad;
 646        spin_lock_irqsave(&bfad->bfad_lock, flags);
 647        rc = bfa_fcs_vport_create(&vport->fcs_vport, &bfad->bfa_fcs, vf_id,
 648                                  port_cfg, vport);
 649        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 650
 651        if (rc != BFA_STATUS_OK)
 652                goto ext_free_vport;
 653
 654        if (port_cfg->roles & BFA_LPORT_ROLE_FCP_IM) {
 655                rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
 656                                                        dev);
 657                if (rc != BFA_STATUS_OK)
 658                        goto ext_free_fcs_vport;
 659        }
 660
 661        spin_lock_irqsave(&bfad->bfad_lock, flags);
 662        bfa_fcs_vport_start(&vport->fcs_vport);
 663        list_add_tail(&vport->list_entry, &bfad->vport_list);
 664        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 665
 666        return BFA_STATUS_OK;
 667
 668ext_free_fcs_vport:
 669        spin_lock_irqsave(&bfad->bfad_lock, flags);
 670        vport->comp_del = &fcomp;
 671        init_completion(vport->comp_del);
 672        bfa_fcs_vport_delete(&vport->fcs_vport);
 673        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 674        wait_for_completion(vport->comp_del);
 675ext_free_vport:
 676        kfree(vport);
 677ext:
 678        return rc;
 679}
 680
 681void
 682bfad_bfa_tmo(unsigned long data)
 683{
 684        struct bfad_s         *bfad = (struct bfad_s *) data;
 685        unsigned long   flags;
 686        struct list_head               doneq;
 687
 688        spin_lock_irqsave(&bfad->bfad_lock, flags);
 689
 690        bfa_timer_beat(&bfad->bfa.timer_mod);
 691
 692        bfa_comp_deq(&bfad->bfa, &doneq);
 693        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 694
 695        if (!list_empty(&doneq)) {
 696                bfa_comp_process(&bfad->bfa, &doneq);
 697                spin_lock_irqsave(&bfad->bfad_lock, flags);
 698                bfa_comp_free(&bfad->bfa, &doneq);
 699                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 700        }
 701
 702        mod_timer(&bfad->hal_tmo,
 703                  jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
 704}
 705
 706void
 707bfad_init_timer(struct bfad_s *bfad)
 708{
 709        init_timer(&bfad->hal_tmo);
 710        bfad->hal_tmo.function = bfad_bfa_tmo;
 711        bfad->hal_tmo.data = (unsigned long)bfad;
 712
 713        mod_timer(&bfad->hal_tmo,
 714                  jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
 715}
 716
 717int
 718bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
 719{
 720        int             rc = -ENODEV;
 721
 722        if (pci_enable_device(pdev)) {
 723                printk(KERN_ERR "pci_enable_device fail %p\n", pdev);
 724                goto out;
 725        }
 726
 727        if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
 728                goto out_disable_device;
 729
 730        pci_set_master(pdev);
 731
 732
 733        if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
 734            (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
 735                if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
 736                   (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)) {
 737                        printk(KERN_ERR "pci_set_dma_mask fail %p\n", pdev);
 738                        goto out_release_region;
 739                }
 740        }
 741
 742        /* Enable PCIE Advanced Error Recovery (AER) if kernel supports */
 743        pci_enable_pcie_error_reporting(pdev);
 744
 745        bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
 746        bfad->pci_bar2_kva = pci_iomap(pdev, 2, pci_resource_len(pdev, 2));
 747
 748        if (bfad->pci_bar0_kva == NULL) {
 749                printk(KERN_ERR "Fail to map bar0\n");
 750                goto out_release_region;
 751        }
 752
 753        bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
 754        bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
 755        bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
 756        bfad->hal_pcidev.device_id = pdev->device;
 757        bfad->hal_pcidev.ssid = pdev->subsystem_device;
 758        bfad->pci_name = pci_name(pdev);
 759
 760        bfad->pci_attr.vendor_id = pdev->vendor;
 761        bfad->pci_attr.device_id = pdev->device;
 762        bfad->pci_attr.ssid = pdev->subsystem_device;
 763        bfad->pci_attr.ssvid = pdev->subsystem_vendor;
 764        bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
 765
 766        bfad->pcidev = pdev;
 767
 768        /* Adjust PCIe Maximum Read Request Size */
 769        if (pci_is_pcie(pdev) && pcie_max_read_reqsz) {
 770                if (pcie_max_read_reqsz >= 128 &&
 771                    pcie_max_read_reqsz <= 4096 &&
 772                    is_power_of_2(pcie_max_read_reqsz)) {
 773                        int max_rq = pcie_get_readrq(pdev);
 774                        printk(KERN_WARNING "BFA[%s]: "
 775                                "pcie_max_read_request_size is %d, "
 776                                "reset to %d\n", bfad->pci_name, max_rq,
 777                                pcie_max_read_reqsz);
 778                        pcie_set_readrq(pdev, pcie_max_read_reqsz);
 779                } else {
 780                        printk(KERN_WARNING "BFA[%s]: invalid "
 781                               "pcie_max_read_request_size %d ignored\n",
 782                               bfad->pci_name, pcie_max_read_reqsz);
 783                }
 784        }
 785
 786        pci_save_state(pdev);
 787
 788        return 0;
 789
 790out_release_region:
 791        pci_release_regions(pdev);
 792out_disable_device:
 793        pci_disable_device(pdev);
 794out:
 795        return rc;
 796}
 797
 798void
 799bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
 800{
 801        pci_iounmap(pdev, bfad->pci_bar0_kva);
 802        pci_iounmap(pdev, bfad->pci_bar2_kva);
 803        pci_release_regions(pdev);
 804        /* Disable PCIE Advanced Error Recovery (AER) */
 805        pci_disable_pcie_error_reporting(pdev);
 806        pci_disable_device(pdev);
 807}
 808
 809bfa_status_t
 810bfad_drv_init(struct bfad_s *bfad)
 811{
 812        bfa_status_t    rc;
 813        unsigned long   flags;
 814
 815        bfad->cfg_data.rport_del_timeout = rport_del_timeout;
 816        bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
 817        bfad->cfg_data.io_max_sge = bfa_io_max_sge;
 818        bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
 819
 820        rc = bfad_hal_mem_alloc(bfad);
 821        if (rc != BFA_STATUS_OK) {
 822                printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
 823                       bfad->inst_no);
 824                printk(KERN_WARNING
 825                        "Not enough memory to attach all Brocade HBA ports, %s",
 826                        "System may need more memory.\n");
 827                goto out_hal_mem_alloc_failure;
 828        }
 829
 830        bfad->bfa.trcmod = bfad->trcmod;
 831        bfad->bfa.plog = &bfad->plog_buf;
 832        bfa_plog_init(&bfad->plog_buf);
 833        bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
 834                     0, "Driver Attach");
 835
 836        bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
 837                   &bfad->hal_pcidev);
 838
 839        /* FCS INIT */
 840        spin_lock_irqsave(&bfad->bfad_lock, flags);
 841        bfad->bfa_fcs.trcmod = bfad->trcmod;
 842        bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
 843        bfad->bfa_fcs.fdmi_enabled = fdmi_enable;
 844        bfa_fcs_init(&bfad->bfa_fcs);
 845        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 846
 847        bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
 848
 849        /* configure base port */
 850        rc = bfad_cfg_pport(bfad, BFA_LPORT_ROLE_FCP_IM);
 851        if (rc != BFA_STATUS_OK)
 852                goto out_cfg_pport_fail;
 853
 854        return BFA_STATUS_OK;
 855
 856out_cfg_pport_fail:
 857        /* fcs exit - on cfg pport failure */
 858        spin_lock_irqsave(&bfad->bfad_lock, flags);
 859        init_completion(&bfad->comp);
 860        bfad->pport.flags |= BFAD_PORT_DELETE;
 861        bfa_fcs_exit(&bfad->bfa_fcs);
 862        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 863        wait_for_completion(&bfad->comp);
 864        /* bfa detach - free hal memory */
 865        bfa_detach(&bfad->bfa);
 866        bfad_hal_mem_release(bfad);
 867out_hal_mem_alloc_failure:
 868        return BFA_STATUS_FAILED;
 869}
 870
 871void
 872bfad_drv_uninit(struct bfad_s *bfad)
 873{
 874        unsigned long   flags;
 875
 876        spin_lock_irqsave(&bfad->bfad_lock, flags);
 877        init_completion(&bfad->comp);
 878        bfa_iocfc_stop(&bfad->bfa);
 879        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 880        wait_for_completion(&bfad->comp);
 881
 882        del_timer_sync(&bfad->hal_tmo);
 883        bfa_isr_disable(&bfad->bfa);
 884        bfa_detach(&bfad->bfa);
 885        bfad_remove_intr(bfad);
 886        bfad_hal_mem_release(bfad);
 887
 888        bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
 889}
 890
 891void
 892bfad_drv_start(struct bfad_s *bfad)
 893{
 894        unsigned long   flags;
 895
 896        spin_lock_irqsave(&bfad->bfad_lock, flags);
 897        bfa_iocfc_start(&bfad->bfa);
 898        bfa_fcs_pbc_vport_init(&bfad->bfa_fcs);
 899        bfa_fcs_fabric_modstart(&bfad->bfa_fcs);
 900        bfad->bfad_flags |= BFAD_HAL_START_DONE;
 901        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 902
 903        if (bfad->im)
 904                flush_workqueue(bfad->im->drv_workq);
 905}
 906
 907void
 908bfad_fcs_stop(struct bfad_s *bfad)
 909{
 910        unsigned long   flags;
 911
 912        spin_lock_irqsave(&bfad->bfad_lock, flags);
 913        init_completion(&bfad->comp);
 914        bfad->pport.flags |= BFAD_PORT_DELETE;
 915        bfa_fcs_exit(&bfad->bfa_fcs);
 916        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 917        wait_for_completion(&bfad->comp);
 918
 919        bfa_sm_send_event(bfad, BFAD_E_FCS_EXIT_COMP);
 920}
 921
 922void
 923bfad_stop(struct bfad_s *bfad)
 924{
 925        unsigned long   flags;
 926
 927        spin_lock_irqsave(&bfad->bfad_lock, flags);
 928        init_completion(&bfad->comp);
 929        bfa_iocfc_stop(&bfad->bfa);
 930        bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
 931        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 932        wait_for_completion(&bfad->comp);
 933
 934        bfa_sm_send_event(bfad, BFAD_E_EXIT_COMP);
 935}
 936
 937bfa_status_t
 938bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role)
 939{
 940        int             rc = BFA_STATUS_OK;
 941
 942        /* Allocate scsi_host for the physical port */
 943        if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
 944            (role & BFA_LPORT_ROLE_FCP_IM)) {
 945                if (bfad->pport.im_port == NULL) {
 946                        rc = BFA_STATUS_FAILED;
 947                        goto out;
 948                }
 949
 950                rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
 951                                                &bfad->pcidev->dev);
 952                if (rc != BFA_STATUS_OK)
 953                        goto out;
 954
 955                bfad->pport.roles |= BFA_LPORT_ROLE_FCP_IM;
 956        }
 957
 958        bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
 959
 960out:
 961        return rc;
 962}
 963
 964void
 965bfad_uncfg_pport(struct bfad_s *bfad)
 966{
 967        if ((supported_fc4s & BFA_LPORT_ROLE_FCP_IM) &&
 968            (bfad->pport.roles & BFA_LPORT_ROLE_FCP_IM)) {
 969                bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
 970                bfad_im_port_clean(bfad->pport.im_port);
 971                kfree(bfad->pport.im_port);
 972                bfad->pport.roles &= ~BFA_LPORT_ROLE_FCP_IM;
 973        }
 974
 975        bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
 976}
 977
 978bfa_status_t
 979bfad_start_ops(struct bfad_s *bfad) {
 980
 981        int     retval;
 982        unsigned long   flags;
 983        struct bfad_vport_s *vport, *vport_new;
 984        struct bfa_fcs_driver_info_s driver_info;
 985
 986        /* Limit min/max. xfer size to [64k-32MB] */
 987        if (max_xfer_size < BFAD_MIN_SECTORS >> 1)
 988                max_xfer_size = BFAD_MIN_SECTORS >> 1;
 989        if (max_xfer_size > BFAD_MAX_SECTORS >> 1)
 990                max_xfer_size = BFAD_MAX_SECTORS >> 1;
 991
 992        /* Fill the driver_info info to fcs*/
 993        memset(&driver_info, 0, sizeof(driver_info));
 994        strncpy(driver_info.version, BFAD_DRIVER_VERSION,
 995                sizeof(driver_info.version) - 1);
 996        if (host_name)
 997                strncpy(driver_info.host_machine_name, host_name,
 998                        sizeof(driver_info.host_machine_name) - 1);
 999        if (os_name)
1000                strncpy(driver_info.host_os_name, os_name,
1001                        sizeof(driver_info.host_os_name) - 1);
1002        if (os_patch)
1003                strncpy(driver_info.host_os_patch, os_patch,
1004                        sizeof(driver_info.host_os_patch) - 1);
1005
1006        strncpy(driver_info.os_device_name, bfad->pci_name,
1007                sizeof(driver_info.os_device_name) - 1);
1008
1009        /* FCS driver info init */
1010        spin_lock_irqsave(&bfad->bfad_lock, flags);
1011        bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
1012        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1013
1014        /*
1015         * FCS update cfg - reset the pwwn/nwwn of fabric base logical port
1016         * with values learned during bfa_init firmware GETATTR REQ.
1017         */
1018        bfa_fcs_update_cfg(&bfad->bfa_fcs);
1019
1020        /* Setup fc host fixed attribute if the lk supports */
1021        bfad_fc_host_init(bfad->pport.im_port);
1022
1023        /* BFAD level FC4 IM specific resource allocation */
1024        retval = bfad_im_probe(bfad);
1025        if (retval != BFA_STATUS_OK) {
1026                printk(KERN_WARNING "bfad_im_probe failed\n");
1027                if (bfa_sm_cmp_state(bfad, bfad_sm_initializing))
1028                        bfa_sm_set_state(bfad, bfad_sm_failed);
1029                bfad_im_probe_undo(bfad);
1030                bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
1031                bfad_uncfg_pport(bfad);
1032                bfad_stop(bfad);
1033                return BFA_STATUS_FAILED;
1034        } else
1035                bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
1036
1037        bfad_drv_start(bfad);
1038
1039        /* Complete pbc vport create */
1040        list_for_each_entry_safe(vport, vport_new, &bfad->pbc_vport_list,
1041                                list_entry) {
1042                struct fc_vport_identifiers vid;
1043                struct fc_vport *fc_vport;
1044                char pwwn_buf[BFA_STRING_32];
1045
1046                memset(&vid, 0, sizeof(vid));
1047                vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1048                vid.vport_type = FC_PORTTYPE_NPIV;
1049                vid.disable = false;
1050                vid.node_name = wwn_to_u64((u8 *)
1051                                (&((vport->fcs_vport).lport.port_cfg.nwwn)));
1052                vid.port_name = wwn_to_u64((u8 *)
1053                                (&((vport->fcs_vport).lport.port_cfg.pwwn)));
1054                fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
1055                if (!fc_vport) {
1056                        wwn2str(pwwn_buf, vid.port_name);
1057                        printk(KERN_WARNING "bfad%d: failed to create pbc vport"
1058                                " %s\n", bfad->inst_no, pwwn_buf);
1059                }
1060                list_del(&vport->list_entry);
1061                kfree(vport);
1062        }
1063
1064        /*
1065         * If bfa_linkup_delay is set to -1 default; try to retrive the
1066         * value using the bfad_get_linkup_delay(); else use the
1067         * passed in module param value as the bfa_linkup_delay.
1068         */
1069        if (bfa_linkup_delay < 0) {
1070                bfa_linkup_delay = bfad_get_linkup_delay(bfad);
1071                bfad_rport_online_wait(bfad);
1072                bfa_linkup_delay = -1;
1073        } else
1074                bfad_rport_online_wait(bfad);
1075
1076        BFA_LOG(KERN_INFO, bfad, bfa_log_level, "bfa device claimed\n");
1077
1078        return BFA_STATUS_OK;
1079}
1080
1081int
1082bfad_worker(void *ptr)
1083{
1084        struct bfad_s *bfad;
1085        unsigned long   flags;
1086
1087        bfad = (struct bfad_s *)ptr;
1088
1089        while (!kthread_should_stop()) {
1090
1091                /* Send event BFAD_E_INIT_SUCCESS */
1092                bfa_sm_send_event(bfad, BFAD_E_INIT_SUCCESS);
1093
1094                spin_lock_irqsave(&bfad->bfad_lock, flags);
1095                bfad->bfad_tsk = NULL;
1096                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1097
1098                break;
1099        }
1100
1101        return 0;
1102}
1103
1104/*
1105 *  BFA driver interrupt functions
1106 */
1107irqreturn_t
1108bfad_intx(int irq, void *dev_id)
1109{
1110        struct bfad_s   *bfad = dev_id;
1111        struct list_head        doneq;
1112        unsigned long   flags;
1113        bfa_boolean_t rc;
1114
1115        spin_lock_irqsave(&bfad->bfad_lock, flags);
1116        rc = bfa_intx(&bfad->bfa);
1117        if (!rc) {
1118                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1119                return IRQ_NONE;
1120        }
1121
1122        bfa_comp_deq(&bfad->bfa, &doneq);
1123        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1124
1125        if (!list_empty(&doneq)) {
1126                bfa_comp_process(&bfad->bfa, &doneq);
1127
1128                spin_lock_irqsave(&bfad->bfad_lock, flags);
1129                bfa_comp_free(&bfad->bfa, &doneq);
1130                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1131        }
1132
1133        return IRQ_HANDLED;
1134
1135}
1136
1137static irqreturn_t
1138bfad_msix(int irq, void *dev_id)
1139{
1140        struct bfad_msix_s *vec = dev_id;
1141        struct bfad_s *bfad = vec->bfad;
1142        struct list_head doneq;
1143        unsigned long   flags;
1144
1145        spin_lock_irqsave(&bfad->bfad_lock, flags);
1146
1147        bfa_msix(&bfad->bfa, vec->msix.entry);
1148        bfa_comp_deq(&bfad->bfa, &doneq);
1149        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1150
1151        if (!list_empty(&doneq)) {
1152                bfa_comp_process(&bfad->bfa, &doneq);
1153
1154                spin_lock_irqsave(&bfad->bfad_lock, flags);
1155                bfa_comp_free(&bfad->bfa, &doneq);
1156                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1157        }
1158
1159        return IRQ_HANDLED;
1160}
1161
1162/*
1163 * Initialize the MSIX entry table.
1164 */
1165static void
1166bfad_init_msix_entry(struct bfad_s *bfad, struct msix_entry *msix_entries,
1167                         int mask, int max_bit)
1168{
1169        int     i;
1170        int     match = 0x00000001;
1171
1172        for (i = 0, bfad->nvec = 0; i < MAX_MSIX_ENTRY; i++) {
1173                if (mask & match) {
1174                        bfad->msix_tab[bfad->nvec].msix.entry = i;
1175                        bfad->msix_tab[bfad->nvec].bfad = bfad;
1176                        msix_entries[bfad->nvec].entry = i;
1177                        bfad->nvec++;
1178                }
1179
1180                match <<= 1;
1181        }
1182
1183}
1184
1185int
1186bfad_install_msix_handler(struct bfad_s *bfad)
1187{
1188        int i, error = 0;
1189
1190        for (i = 0; i < bfad->nvec; i++) {
1191                sprintf(bfad->msix_tab[i].name, "bfa-%s-%s",
1192                                bfad->pci_name,
1193                                ((bfa_asic_id_cb(bfad->hal_pcidev.device_id)) ?
1194                                msix_name_cb[i] : msix_name_ct[i]));
1195
1196                error = request_irq(bfad->msix_tab[i].msix.vector,
1197                                    (irq_handler_t) bfad_msix, 0,
1198                                    bfad->msix_tab[i].name, &bfad->msix_tab[i]);
1199                bfa_trc(bfad, i);
1200                bfa_trc(bfad, bfad->msix_tab[i].msix.vector);
1201                if (error) {
1202                        int     j;
1203
1204                        for (j = 0; j < i; j++)
1205                                free_irq(bfad->msix_tab[j].msix.vector,
1206                                                &bfad->msix_tab[j]);
1207
1208                        bfad->bfad_flags &= ~BFAD_MSIX_ON;
1209                        pci_disable_msix(bfad->pcidev);
1210
1211                        return 1;
1212                }
1213        }
1214
1215        return 0;
1216}
1217
1218/*
1219 * Setup MSIX based interrupt.
1220 */
1221int
1222bfad_setup_intr(struct bfad_s *bfad)
1223{
1224        int error = 0;
1225        u32 mask = 0, i, num_bit = 0, max_bit = 0;
1226        struct msix_entry msix_entries[MAX_MSIX_ENTRY];
1227        struct pci_dev *pdev = bfad->pcidev;
1228        u16     reg;
1229
1230        /* Call BFA to get the msix map for this PCI function.  */
1231        bfa_msix_getvecs(&bfad->bfa, &mask, &num_bit, &max_bit);
1232
1233        /* Set up the msix entry table */
1234        bfad_init_msix_entry(bfad, msix_entries, mask, max_bit);
1235
1236        if ((bfa_asic_id_ctc(pdev->device) && !msix_disable_ct) ||
1237           (bfa_asic_id_cb(pdev->device) && !msix_disable_cb)) {
1238
1239                error = pci_enable_msix(bfad->pcidev, msix_entries, bfad->nvec);
1240                if (error) {
1241                        /* In CT1 & CT2, try to allocate just one vector */
1242                        if (bfa_asic_id_ctc(pdev->device)) {
1243                                printk(KERN_WARNING "bfa %s: trying one msix "
1244                                       "vector failed to allocate %d[%d]\n",
1245                                       bfad->pci_name, bfad->nvec, error);
1246                                bfad->nvec = 1;
1247                                error = pci_enable_msix(bfad->pcidev,
1248                                                msix_entries, bfad->nvec);
1249                        }
1250
1251                        /*
1252                         * Only error number of vector is available.
1253                         * We don't have a mechanism to map multiple
1254                         * interrupts into one vector, so even if we
1255                         * can try to request less vectors, we don't
1256                         * know how to associate interrupt events to
1257                         *  vectors. Linux doesn't duplicate vectors
1258                         * in the MSIX table for this case.
1259                         */
1260                        if (error) {
1261                                printk(KERN_WARNING "bfad%d: "
1262                                       "pci_enable_msix failed (%d), "
1263                                       "use line based.\n",
1264                                        bfad->inst_no, error);
1265                                goto line_based;
1266                        }
1267                }
1268
1269                /* Disable INTX in MSI-X mode */
1270                pci_read_config_word(pdev, PCI_COMMAND, &reg);
1271
1272                if (!(reg & PCI_COMMAND_INTX_DISABLE))
1273                        pci_write_config_word(pdev, PCI_COMMAND,
1274                                reg | PCI_COMMAND_INTX_DISABLE);
1275
1276                /* Save the vectors */
1277                for (i = 0; i < bfad->nvec; i++) {
1278                        bfa_trc(bfad, msix_entries[i].vector);
1279                        bfad->msix_tab[i].msix.vector = msix_entries[i].vector;
1280                }
1281
1282                bfa_msix_init(&bfad->bfa, bfad->nvec);
1283
1284                bfad->bfad_flags |= BFAD_MSIX_ON;
1285
1286                return error;
1287        }
1288
1289line_based:
1290        error = 0;
1291        if (request_irq
1292            (bfad->pcidev->irq, (irq_handler_t) bfad_intx, BFAD_IRQ_FLAGS,
1293             BFAD_DRIVER_NAME, bfad) != 0) {
1294                /* Enable interrupt handler failed */
1295                return 1;
1296        }
1297        bfad->bfad_flags |= BFAD_INTX_ON;
1298
1299        return error;
1300}
1301
1302void
1303bfad_remove_intr(struct bfad_s *bfad)
1304{
1305        int     i;
1306
1307        if (bfad->bfad_flags & BFAD_MSIX_ON) {
1308                for (i = 0; i < bfad->nvec; i++)
1309                        free_irq(bfad->msix_tab[i].msix.vector,
1310                                        &bfad->msix_tab[i]);
1311
1312                pci_disable_msix(bfad->pcidev);
1313                bfad->bfad_flags &= ~BFAD_MSIX_ON;
1314        } else if (bfad->bfad_flags & BFAD_INTX_ON) {
1315                free_irq(bfad->pcidev->irq, bfad);
1316        }
1317}
1318
1319/*
1320 * PCI probe entry.
1321 */
1322int
1323bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1324{
1325        struct bfad_s   *bfad;
1326        int             error = -ENODEV, retval, i;
1327
1328        /* For single port cards - only claim function 0 */
1329        if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P) &&
1330                (PCI_FUNC(pdev->devfn) != 0))
1331                return -ENODEV;
1332
1333        bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1334        if (!bfad) {
1335                error = -ENOMEM;
1336                goto out;
1337        }
1338
1339        bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1340        if (!bfad->trcmod) {
1341                printk(KERN_WARNING "Error alloc trace buffer!\n");
1342                error = -ENOMEM;
1343                goto out_alloc_trace_failure;
1344        }
1345
1346        /* TRACE INIT */
1347        bfa_trc_init(bfad->trcmod);
1348        bfa_trc(bfad, bfad_inst);
1349
1350        /* AEN INIT */
1351        INIT_LIST_HEAD(&bfad->free_aen_q);
1352        INIT_LIST_HEAD(&bfad->active_aen_q);
1353        for (i = 0; i < BFA_AEN_MAX_ENTRY; i++)
1354                list_add_tail(&bfad->aen_list[i].qe, &bfad->free_aen_q);
1355
1356        if (!(bfad_load_fwimg(pdev))) {
1357                kfree(bfad->trcmod);
1358                goto out_alloc_trace_failure;
1359        }
1360
1361        retval = bfad_pci_init(pdev, bfad);
1362        if (retval) {
1363                printk(KERN_WARNING "bfad_pci_init failure!\n");
1364                error = retval;
1365                goto out_pci_init_failure;
1366        }
1367
1368        mutex_lock(&bfad_mutex);
1369        bfad->inst_no = bfad_inst++;
1370        list_add_tail(&bfad->list_entry, &bfad_list);
1371        mutex_unlock(&bfad_mutex);
1372
1373        /* Initializing the state machine: State set to uninit */
1374        bfa_sm_set_state(bfad, bfad_sm_uninit);
1375
1376        spin_lock_init(&bfad->bfad_lock);
1377        spin_lock_init(&bfad->bfad_aen_spinlock);
1378
1379        pci_set_drvdata(pdev, bfad);
1380
1381        bfad->ref_count = 0;
1382        bfad->pport.bfad = bfad;
1383        INIT_LIST_HEAD(&bfad->pbc_vport_list);
1384        INIT_LIST_HEAD(&bfad->vport_list);
1385
1386        /* Setup the debugfs node for this bfad */
1387        if (bfa_debugfs_enable)
1388                bfad_debugfs_init(&bfad->pport);
1389
1390        retval = bfad_drv_init(bfad);
1391        if (retval != BFA_STATUS_OK)
1392                goto out_drv_init_failure;
1393
1394        bfa_sm_send_event(bfad, BFAD_E_CREATE);
1395
1396        if (bfa_sm_cmp_state(bfad, bfad_sm_uninit))
1397                goto out_bfad_sm_failure;
1398
1399        return 0;
1400
1401out_bfad_sm_failure:
1402        bfa_detach(&bfad->bfa);
1403        bfad_hal_mem_release(bfad);
1404out_drv_init_failure:
1405        /* Remove the debugfs node for this bfad */
1406        kfree(bfad->regdata);
1407        bfad_debugfs_exit(&bfad->pport);
1408        mutex_lock(&bfad_mutex);
1409        bfad_inst--;
1410        list_del(&bfad->list_entry);
1411        mutex_unlock(&bfad_mutex);
1412        bfad_pci_uninit(pdev, bfad);
1413out_pci_init_failure:
1414        kfree(bfad->trcmod);
1415out_alloc_trace_failure:
1416        kfree(bfad);
1417out:
1418        return error;
1419}
1420
1421/*
1422 * PCI remove entry.
1423 */
1424void
1425bfad_pci_remove(struct pci_dev *pdev)
1426{
1427        struct bfad_s         *bfad = pci_get_drvdata(pdev);
1428        unsigned long   flags;
1429
1430        bfa_trc(bfad, bfad->inst_no);
1431
1432        spin_lock_irqsave(&bfad->bfad_lock, flags);
1433        if (bfad->bfad_tsk != NULL) {
1434                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1435                kthread_stop(bfad->bfad_tsk);
1436        } else {
1437                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1438        }
1439
1440        /* Send Event BFAD_E_STOP */
1441        bfa_sm_send_event(bfad, BFAD_E_STOP);
1442
1443        /* Driver detach and dealloc mem */
1444        spin_lock_irqsave(&bfad->bfad_lock, flags);
1445        bfa_detach(&bfad->bfa);
1446        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1447        bfad_hal_mem_release(bfad);
1448
1449        /* Remove the debugfs node for this bfad */
1450        kfree(bfad->regdata);
1451        bfad_debugfs_exit(&bfad->pport);
1452
1453        /* Cleaning the BFAD instance */
1454        mutex_lock(&bfad_mutex);
1455        bfad_inst--;
1456        list_del(&bfad->list_entry);
1457        mutex_unlock(&bfad_mutex);
1458        bfad_pci_uninit(pdev, bfad);
1459
1460        kfree(bfad->trcmod);
1461        kfree(bfad);
1462}
1463
1464/*
1465 * PCI Error Recovery entry, error detected.
1466 */
1467static pci_ers_result_t
1468bfad_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
1469{
1470        struct bfad_s *bfad = pci_get_drvdata(pdev);
1471        unsigned long   flags;
1472        pci_ers_result_t ret = PCI_ERS_RESULT_NONE;
1473
1474        dev_printk(KERN_ERR, &pdev->dev,
1475                   "error detected state: %d - flags: 0x%x\n",
1476                   state, bfad->bfad_flags);
1477
1478        switch (state) {
1479        case pci_channel_io_normal: /* non-fatal error */
1480                spin_lock_irqsave(&bfad->bfad_lock, flags);
1481                bfad->bfad_flags &= ~BFAD_EEH_BUSY;
1482                /* Suspend/fail all bfa operations */
1483                bfa_ioc_suspend(&bfad->bfa.ioc);
1484                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1485                del_timer_sync(&bfad->hal_tmo);
1486                ret = PCI_ERS_RESULT_CAN_RECOVER;
1487                break;
1488        case pci_channel_io_frozen: /* fatal error */
1489                init_completion(&bfad->comp);
1490                spin_lock_irqsave(&bfad->bfad_lock, flags);
1491                bfad->bfad_flags |= BFAD_EEH_BUSY;
1492                /* Suspend/fail all bfa operations */
1493                bfa_ioc_suspend(&bfad->bfa.ioc);
1494                bfa_fcs_stop(&bfad->bfa_fcs);
1495                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1496                wait_for_completion(&bfad->comp);
1497
1498                bfad_remove_intr(bfad);
1499                del_timer_sync(&bfad->hal_tmo);
1500                pci_disable_device(pdev);
1501                ret = PCI_ERS_RESULT_NEED_RESET;
1502                break;
1503        case pci_channel_io_perm_failure: /* PCI Card is DEAD */
1504                spin_lock_irqsave(&bfad->bfad_lock, flags);
1505                bfad->bfad_flags |= BFAD_EEH_BUSY |
1506                                    BFAD_EEH_PCI_CHANNEL_IO_PERM_FAILURE;
1507                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1508
1509                /* If the error_detected handler is called with the reason
1510                 * pci_channel_io_perm_failure - it will subsequently call
1511                 * pci_remove() entry point to remove the pci device from the
1512                 * system - So defer the cleanup to pci_remove(); cleaning up
1513                 * here causes inconsistent state during pci_remove().
1514                 */
1515                ret = PCI_ERS_RESULT_DISCONNECT;
1516                break;
1517        default:
1518                WARN_ON(1);
1519        }
1520
1521        return ret;
1522}
1523
1524int
1525restart_bfa(struct bfad_s *bfad)
1526{
1527        unsigned long flags;
1528        struct pci_dev *pdev = bfad->pcidev;
1529
1530        bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg,
1531                   &bfad->meminfo, &bfad->hal_pcidev);
1532
1533        /* Enable Interrupt and wait bfa_init completion */
1534        if (bfad_setup_intr(bfad)) {
1535                dev_printk(KERN_WARNING, &pdev->dev,
1536                           "%s: bfad_setup_intr failed\n", bfad->pci_name);
1537                bfa_sm_send_event(bfad, BFAD_E_INTR_INIT_FAILED);
1538                return -1;
1539        }
1540
1541        init_completion(&bfad->comp);
1542        spin_lock_irqsave(&bfad->bfad_lock, flags);
1543        bfa_iocfc_init(&bfad->bfa);
1544        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1545
1546        /* Set up interrupt handler for each vectors */
1547        if ((bfad->bfad_flags & BFAD_MSIX_ON) &&
1548            bfad_install_msix_handler(bfad))
1549                dev_printk(KERN_WARNING, &pdev->dev,
1550                           "%s: install_msix failed.\n", bfad->pci_name);
1551
1552        bfad_init_timer(bfad);
1553        wait_for_completion(&bfad->comp);
1554        bfad_drv_start(bfad);
1555
1556        return 0;
1557}
1558
1559/*
1560 * PCI Error Recovery entry, re-initialize the chip.
1561 */
1562static pci_ers_result_t
1563bfad_pci_slot_reset(struct pci_dev *pdev)
1564{
1565        struct bfad_s *bfad = pci_get_drvdata(pdev);
1566        u8 byte;
1567
1568        dev_printk(KERN_ERR, &pdev->dev,
1569                   "bfad_pci_slot_reset flags: 0x%x\n", bfad->bfad_flags);
1570
1571        if (pci_enable_device(pdev)) {
1572                dev_printk(KERN_ERR, &pdev->dev, "Cannot re-enable "
1573                           "PCI device after reset.\n");
1574                return PCI_ERS_RESULT_DISCONNECT;
1575        }
1576
1577        pci_restore_state(pdev);
1578
1579        /*
1580         * Read some byte (e.g. DMA max. payload size which can't
1581         * be 0xff any time) to make sure - we did not hit another PCI error
1582         * in the middle of recovery. If we did, then declare permanent failure.
1583         */
1584        pci_read_config_byte(pdev, 0x68, &byte);
1585        if (byte == 0xff) {
1586                dev_printk(KERN_ERR, &pdev->dev,
1587                           "slot_reset failed ... got another PCI error !\n");
1588                goto out_disable_device;
1589        }
1590
1591        pci_save_state(pdev);
1592        pci_set_master(pdev);
1593
1594        if (pci_set_dma_mask(bfad->pcidev, DMA_BIT_MASK(64)) != 0)
1595                if (pci_set_dma_mask(bfad->pcidev, DMA_BIT_MASK(32)) != 0)
1596                        goto out_disable_device;
1597
1598        pci_cleanup_aer_uncorrect_error_status(pdev);
1599
1600        if (restart_bfa(bfad) == -1)
1601                goto out_disable_device;
1602
1603        pci_enable_pcie_error_reporting(pdev);
1604        dev_printk(KERN_WARNING, &pdev->dev,
1605                   "slot_reset completed  flags: 0x%x!\n", bfad->bfad_flags);
1606
1607        return PCI_ERS_RESULT_RECOVERED;
1608
1609out_disable_device:
1610        pci_disable_device(pdev);
1611        return PCI_ERS_RESULT_DISCONNECT;
1612}
1613
1614static pci_ers_result_t
1615bfad_pci_mmio_enabled(struct pci_dev *pdev)
1616{
1617        unsigned long   flags;
1618        struct bfad_s *bfad = pci_get_drvdata(pdev);
1619
1620        dev_printk(KERN_INFO, &pdev->dev, "mmio_enabled\n");
1621
1622        /* Fetch FW diagnostic information */
1623        bfa_ioc_debug_save_ftrc(&bfad->bfa.ioc);
1624
1625        /* Cancel all pending IOs */
1626        spin_lock_irqsave(&bfad->bfad_lock, flags);
1627        init_completion(&bfad->comp);
1628        bfa_fcs_stop(&bfad->bfa_fcs);
1629        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1630        wait_for_completion(&bfad->comp);
1631
1632        bfad_remove_intr(bfad);
1633        del_timer_sync(&bfad->hal_tmo);
1634        pci_disable_device(pdev);
1635
1636        return PCI_ERS_RESULT_NEED_RESET;
1637}
1638
1639static void
1640bfad_pci_resume(struct pci_dev *pdev)
1641{
1642        unsigned long   flags;
1643        struct bfad_s *bfad = pci_get_drvdata(pdev);
1644
1645        dev_printk(KERN_WARNING, &pdev->dev, "resume\n");
1646
1647        /* wait until the link is online */
1648        bfad_rport_online_wait(bfad);
1649
1650        spin_lock_irqsave(&bfad->bfad_lock, flags);
1651        bfad->bfad_flags &= ~BFAD_EEH_BUSY;
1652        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1653}
1654
1655struct pci_device_id bfad_id_table[] = {
1656        {
1657                .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1658                .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1659                .subvendor = PCI_ANY_ID,
1660                .subdevice = PCI_ANY_ID,
1661        },
1662        {
1663                .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1664                .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1665                .subvendor = PCI_ANY_ID,
1666                .subdevice = PCI_ANY_ID,
1667        },
1668        {
1669                .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1670                .device = BFA_PCI_DEVICE_ID_CT,
1671                .subvendor = PCI_ANY_ID,
1672                .subdevice = PCI_ANY_ID,
1673                .class = (PCI_CLASS_SERIAL_FIBER << 8),
1674                .class_mask = ~0,
1675        },
1676        {
1677                .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1678                .device = BFA_PCI_DEVICE_ID_CT_FC,
1679                .subvendor = PCI_ANY_ID,
1680                .subdevice = PCI_ANY_ID,
1681                .class = (PCI_CLASS_SERIAL_FIBER << 8),
1682                .class_mask = ~0,
1683        },
1684        {
1685                .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1686                .device = BFA_PCI_DEVICE_ID_CT2,
1687                .subvendor = PCI_ANY_ID,
1688                .subdevice = PCI_ANY_ID,
1689                .class = (PCI_CLASS_SERIAL_FIBER << 8),
1690                .class_mask = ~0,
1691        },
1692
1693        {
1694                .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1695                .device = BFA_PCI_DEVICE_ID_CT2_QUAD,
1696                .subvendor = PCI_ANY_ID,
1697                .subdevice = PCI_ANY_ID,
1698                .class = (PCI_CLASS_SERIAL_FIBER << 8),
1699                .class_mask = ~0,
1700        },
1701        {0, 0},
1702};
1703
1704MODULE_DEVICE_TABLE(pci, bfad_id_table);
1705
1706/*
1707 * PCI error recovery handlers.
1708 */
1709static struct pci_error_handlers bfad_err_handler = {
1710        .error_detected = bfad_pci_error_detected,
1711        .slot_reset = bfad_pci_slot_reset,
1712        .mmio_enabled = bfad_pci_mmio_enabled,
1713        .resume = bfad_pci_resume,
1714};
1715
1716static struct pci_driver bfad_pci_driver = {
1717        .name = BFAD_DRIVER_NAME,
1718        .id_table = bfad_id_table,
1719        .probe = bfad_pci_probe,
1720        .remove = bfad_pci_remove,
1721        .err_handler = &bfad_err_handler,
1722};
1723
1724/*
1725 * Driver module init.
1726 */
1727static int __init
1728bfad_init(void)
1729{
1730        int             error = 0;
1731
1732        printk(KERN_INFO "Brocade BFA FC/FCOE SCSI driver - version: %s\n",
1733                        BFAD_DRIVER_VERSION);
1734
1735        if (num_sgpgs > 0)
1736                num_sgpgs_parm = num_sgpgs;
1737
1738        error = bfad_im_module_init();
1739        if (error) {
1740                error = -ENOMEM;
1741                printk(KERN_WARNING "bfad_im_module_init failure\n");
1742                goto ext;
1743        }
1744
1745        if (strcmp(FCPI_NAME, " fcpim") == 0)
1746                supported_fc4s |= BFA_LPORT_ROLE_FCP_IM;
1747
1748        bfa_auto_recover = ioc_auto_recover;
1749        bfa_fcs_rport_set_del_timeout(rport_del_timeout);
1750        bfa_fcs_rport_set_max_logins(max_rport_logins);
1751
1752        error = pci_register_driver(&bfad_pci_driver);
1753        if (error) {
1754                printk(KERN_WARNING "pci_register_driver failure\n");
1755                goto ext;
1756        }
1757
1758        return 0;
1759
1760ext:
1761        bfad_im_module_exit();
1762        return error;
1763}
1764
1765/*
1766 * Driver module exit.
1767 */
1768static void __exit
1769bfad_exit(void)
1770{
1771        pci_unregister_driver(&bfad_pci_driver);
1772        bfad_im_module_exit();
1773        bfad_free_fwimg();
1774}
1775
1776/* Firmware handling */
1777static void
1778bfad_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
1779                u32 *bfi_image_size, char *fw_name)
1780{
1781        const struct firmware *fw;
1782
1783        if (request_firmware(&fw, fw_name, &pdev->dev)) {
1784                printk(KERN_ALERT "Can't locate firmware %s\n", fw_name);
1785                *bfi_image = NULL;
1786                goto out;
1787        }
1788
1789        *bfi_image = vmalloc(fw->size);
1790        if (NULL == *bfi_image) {
1791                printk(KERN_ALERT "Fail to allocate buffer for fw image "
1792                        "size=%x!\n", (u32) fw->size);
1793                goto out;
1794        }
1795
1796        memcpy(*bfi_image, fw->data, fw->size);
1797        *bfi_image_size = fw->size/sizeof(u32);
1798out:
1799        release_firmware(fw);
1800}
1801
1802static u32 *
1803bfad_load_fwimg(struct pci_dev *pdev)
1804{
1805        if (pdev->device == BFA_PCI_DEVICE_ID_CT2) {
1806                if (bfi_image_ct2_size == 0)
1807                        bfad_read_firmware(pdev, &bfi_image_ct2,
1808                                &bfi_image_ct2_size, BFAD_FW_FILE_CT2);
1809                return bfi_image_ct2;
1810        } else if (bfa_asic_id_ct(pdev->device)) {
1811                if (bfi_image_ct_size == 0)
1812                        bfad_read_firmware(pdev, &bfi_image_ct,
1813                                &bfi_image_ct_size, BFAD_FW_FILE_CT);
1814                return bfi_image_ct;
1815        } else {
1816                if (bfi_image_cb_size == 0)
1817                        bfad_read_firmware(pdev, &bfi_image_cb,
1818                                &bfi_image_cb_size, BFAD_FW_FILE_CB);
1819                return bfi_image_cb;
1820        }
1821}
1822
1823static void
1824bfad_free_fwimg(void)
1825{
1826        if (bfi_image_ct2_size && bfi_image_ct2)
1827                vfree(bfi_image_ct2);
1828        if (bfi_image_ct_size && bfi_image_ct)
1829                vfree(bfi_image_ct);
1830        if (bfi_image_cb_size && bfi_image_cb)
1831                vfree(bfi_image_cb);
1832}
1833
1834module_init(bfad_init);
1835module_exit(bfad_exit);
1836MODULE_LICENSE("GPL");
1837MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1838MODULE_AUTHOR("Brocade Communications Systems, Inc.");
1839MODULE_VERSION(BFAD_DRIVER_VERSION);
1840