linux/drivers/scsi/qla2xxx/qla_os.c
<<
>>
Prefs
   1/*
   2 * QLogic Fibre Channel HBA Driver
   3 * Copyright (c)  2003-2014 QLogic Corporation
   4 *
   5 * See LICENSE.qla2xxx for copyright and licensing details.
   6 */
   7#include "qla_def.h"
   8
   9#include <linux/moduleparam.h>
  10#include <linux/vmalloc.h>
  11#include <linux/delay.h>
  12#include <linux/kthread.h>
  13#include <linux/mutex.h>
  14#include <linux/kobject.h>
  15#include <linux/slab.h>
  16#include <linux/blk-mq-pci.h>
  17#include <linux/refcount.h>
  18#include <linux/crash_dump.h>
  19
  20#include <scsi/scsi_tcq.h>
  21#include <scsi/scsicam.h>
  22#include <scsi/scsi_transport.h>
  23#include <scsi/scsi_transport_fc.h>
  24
  25#include "qla_target.h"
  26
  27/*
  28 * Driver version
  29 */
  30char qla2x00_version_str[40];
  31
  32static int apidev_major;
  33
  34/*
  35 * SRB allocation cache
  36 */
  37struct kmem_cache *srb_cachep;
  38
  39int ql2xfulldump_on_mpifail;
  40module_param(ql2xfulldump_on_mpifail, int, S_IRUGO | S_IWUSR);
  41MODULE_PARM_DESC(ql2xfulldump_on_mpifail,
  42                 "Set this to take full dump on MPI hang.");
  43
  44int ql2xenforce_iocb_limit = 1;
  45module_param(ql2xenforce_iocb_limit, int, S_IRUGO | S_IWUSR);
  46MODULE_PARM_DESC(ql2xenforce_iocb_limit,
  47                 "Enforce IOCB throttling, to avoid FW congestion. (default: 1)");
  48
  49/*
  50 * CT6 CTX allocation cache
  51 */
  52static struct kmem_cache *ctx_cachep;
  53/*
  54 * error level for logging
  55 */
  56uint ql_errlev = 0x8001;
  57
  58int ql2xsecenable;
  59module_param(ql2xsecenable, int, S_IRUGO);
  60MODULE_PARM_DESC(ql2xsecenable,
  61        "Enable/disable security. 0(Default) - Security disabled. 1 - Security enabled.");
  62
  63static int ql2xenableclass2;
  64module_param(ql2xenableclass2, int, S_IRUGO|S_IRUSR);
  65MODULE_PARM_DESC(ql2xenableclass2,
  66                "Specify if Class 2 operations are supported from the very "
  67                "beginning. Default is 0 - class 2 not supported.");
  68
  69
  70int ql2xlogintimeout = 20;
  71module_param(ql2xlogintimeout, int, S_IRUGO);
  72MODULE_PARM_DESC(ql2xlogintimeout,
  73                "Login timeout value in seconds.");
  74
  75int qlport_down_retry;
  76module_param(qlport_down_retry, int, S_IRUGO);
  77MODULE_PARM_DESC(qlport_down_retry,
  78                "Maximum number of command retries to a port that returns "
  79                "a PORT-DOWN status.");
  80
  81int ql2xplogiabsentdevice;
  82module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
  83MODULE_PARM_DESC(ql2xplogiabsentdevice,
  84                "Option to enable PLOGI to devices that are not present after "
  85                "a Fabric scan.  This is needed for several broken switches. "
  86                "Default is 0 - no PLOGI. 1 - perform PLOGI.");
  87
  88int ql2xloginretrycount;
  89module_param(ql2xloginretrycount, int, S_IRUGO);
  90MODULE_PARM_DESC(ql2xloginretrycount,
  91                "Specify an alternate value for the NVRAM login retry count.");
  92
  93int ql2xallocfwdump = 1;
  94module_param(ql2xallocfwdump, int, S_IRUGO);
  95MODULE_PARM_DESC(ql2xallocfwdump,
  96                "Option to enable allocation of memory for a firmware dump "
  97                "during HBA initialization.  Memory allocation requirements "
  98                "vary by ISP type.  Default is 1 - allocate memory.");
  99
 100int ql2xextended_error_logging;
 101module_param(ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
 102module_param_named(logging, ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
 103MODULE_PARM_DESC(ql2xextended_error_logging,
 104                "Option to enable extended error logging,\n"
 105                "\t\tDefault is 0 - no logging.  0x40000000 - Module Init & Probe.\n"
 106                "\t\t0x20000000 - Mailbox Cmnds. 0x10000000 - Device Discovery.\n"
 107                "\t\t0x08000000 - IO tracing.    0x04000000 - DPC Thread.\n"
 108                "\t\t0x02000000 - Async events.  0x01000000 - Timer routines.\n"
 109                "\t\t0x00800000 - User space.    0x00400000 - Task Management.\n"
 110                "\t\t0x00200000 - AER/EEH.       0x00100000 - Multi Q.\n"
 111                "\t\t0x00080000 - P3P Specific.  0x00040000 - Virtual Port.\n"
 112                "\t\t0x00020000 - Buffer Dump.   0x00010000 - Misc.\n"
 113                "\t\t0x00008000 - Verbose.       0x00004000 - Target.\n"
 114                "\t\t0x00002000 - Target Mgmt.   0x00001000 - Target TMF.\n"
 115                "\t\t0x7fffffff - For enabling all logs, can be too many logs.\n"
 116                "\t\t0x1e400000 - Preferred value for capturing essential "
 117                "debug information (equivalent to old "
 118                "ql2xextended_error_logging=1).\n"
 119                "\t\tDo LOGICAL OR of the value to enable more than one level");
 120
 121int ql2xshiftctondsd = 6;
 122module_param(ql2xshiftctondsd, int, S_IRUGO);
 123MODULE_PARM_DESC(ql2xshiftctondsd,
 124                "Set to control shifting of command type processing "
 125                "based on total number of SG elements.");
 126
 127int ql2xfdmienable = 1;
 128module_param(ql2xfdmienable, int, S_IRUGO|S_IWUSR);
 129module_param_named(fdmi, ql2xfdmienable, int, S_IRUGO|S_IWUSR);
 130MODULE_PARM_DESC(ql2xfdmienable,
 131                "Enables FDMI registrations. "
 132                "0 - no FDMI registrations. "
 133                "1 - provide FDMI registrations (default).");
 134
 135#define MAX_Q_DEPTH     64
 136static int ql2xmaxqdepth = MAX_Q_DEPTH;
 137module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
 138MODULE_PARM_DESC(ql2xmaxqdepth,
 139                "Maximum queue depth to set for each LUN. "
 140                "Default is 64.");
 141
 142int ql2xenabledif = 2;
 143module_param(ql2xenabledif, int, S_IRUGO);
 144MODULE_PARM_DESC(ql2xenabledif,
 145                " Enable T10-CRC-DIF:\n"
 146                " Default is 2.\n"
 147                "  0 -- No DIF Support\n"
 148                "  1 -- Enable DIF for all types\n"
 149                "  2 -- Enable DIF for all types, except Type 0.\n");
 150
 151#if (IS_ENABLED(CONFIG_NVME_FC))
 152int ql2xnvmeenable = 1;
 153#else
 154int ql2xnvmeenable;
 155#endif
 156module_param(ql2xnvmeenable, int, 0644);
 157MODULE_PARM_DESC(ql2xnvmeenable,
 158    "Enables NVME support. "
 159    "0 - no NVMe.  Default is Y");
 160
 161int ql2xenablehba_err_chk = 2;
 162module_param(ql2xenablehba_err_chk, int, S_IRUGO|S_IWUSR);
 163MODULE_PARM_DESC(ql2xenablehba_err_chk,
 164                " Enable T10-CRC-DIF Error isolation by HBA:\n"
 165                " Default is 2.\n"
 166                "  0 -- Error isolation disabled\n"
 167                "  1 -- Error isolation enabled only for DIX Type 0\n"
 168                "  2 -- Error isolation enabled for all Types\n");
 169
 170int ql2xiidmaenable = 1;
 171module_param(ql2xiidmaenable, int, S_IRUGO);
 172MODULE_PARM_DESC(ql2xiidmaenable,
 173                "Enables iIDMA settings "
 174                "Default is 1 - perform iIDMA. 0 - no iIDMA.");
 175
 176int ql2xmqsupport = 1;
 177module_param(ql2xmqsupport, int, S_IRUGO);
 178MODULE_PARM_DESC(ql2xmqsupport,
 179                "Enable on demand multiple queue pairs support "
 180                "Default is 1 for supported. "
 181                "Set it to 0 to turn off mq qpair support.");
 182
 183int ql2xfwloadbin;
 184module_param(ql2xfwloadbin, int, S_IRUGO|S_IWUSR);
 185module_param_named(fwload, ql2xfwloadbin, int, S_IRUGO|S_IWUSR);
 186MODULE_PARM_DESC(ql2xfwloadbin,
 187                "Option to specify location from which to load ISP firmware:.\n"
 188                " 2 -- load firmware via the request_firmware() (hotplug).\n"
 189                "      interface.\n"
 190                " 1 -- load firmware from flash.\n"
 191                " 0 -- use default semantics.\n");
 192
 193int ql2xetsenable;
 194module_param(ql2xetsenable, int, S_IRUGO);
 195MODULE_PARM_DESC(ql2xetsenable,
 196                "Enables firmware ETS burst."
 197                "Default is 0 - skip ETS enablement.");
 198
 199int ql2xdbwr = 1;
 200module_param(ql2xdbwr, int, S_IRUGO|S_IWUSR);
 201MODULE_PARM_DESC(ql2xdbwr,
 202                "Option to specify scheme for request queue posting.\n"
 203                " 0 -- Regular doorbell.\n"
 204                " 1 -- CAMRAM doorbell (faster).\n");
 205
 206int ql2xtargetreset = 1;
 207module_param(ql2xtargetreset, int, S_IRUGO);
 208MODULE_PARM_DESC(ql2xtargetreset,
 209                 "Enable target reset."
 210                 "Default is 1 - use hw defaults.");
 211
 212int ql2xgffidenable;
 213module_param(ql2xgffidenable, int, S_IRUGO);
 214MODULE_PARM_DESC(ql2xgffidenable,
 215                "Enables GFF_ID checks of port type. "
 216                "Default is 0 - Do not use GFF_ID information.");
 217
 218int ql2xasynctmfenable = 1;
 219module_param(ql2xasynctmfenable, int, S_IRUGO);
 220MODULE_PARM_DESC(ql2xasynctmfenable,
 221                "Enables issue of TM IOCBs asynchronously via IOCB mechanism"
 222                "Default is 1 - Issue TM IOCBs via mailbox mechanism.");
 223
 224int ql2xdontresethba;
 225module_param(ql2xdontresethba, int, S_IRUGO|S_IWUSR);
 226MODULE_PARM_DESC(ql2xdontresethba,
 227                "Option to specify reset behaviour.\n"
 228                " 0 (Default) -- Reset on failure.\n"
 229                " 1 -- Do not reset on failure.\n");
 230
 231uint64_t ql2xmaxlun = MAX_LUNS;
 232module_param(ql2xmaxlun, ullong, S_IRUGO);
 233MODULE_PARM_DESC(ql2xmaxlun,
 234                "Defines the maximum LU number to register with the SCSI "
 235                "midlayer. Default is 65535.");
 236
 237int ql2xmdcapmask = 0x1F;
 238module_param(ql2xmdcapmask, int, S_IRUGO);
 239MODULE_PARM_DESC(ql2xmdcapmask,
 240                "Set the Minidump driver capture mask level. "
 241                "Default is 0x1F - Can be set to 0x3, 0x7, 0xF, 0x1F, 0x7F.");
 242
 243int ql2xmdenable = 1;
 244module_param(ql2xmdenable, int, S_IRUGO);
 245MODULE_PARM_DESC(ql2xmdenable,
 246                "Enable/disable MiniDump. "
 247                "0 - MiniDump disabled. "
 248                "1 (Default) - MiniDump enabled.");
 249
 250int ql2xexlogins;
 251module_param(ql2xexlogins, uint, S_IRUGO|S_IWUSR);
 252MODULE_PARM_DESC(ql2xexlogins,
 253                 "Number of extended Logins. "
 254                 "0 (Default)- Disabled.");
 255
 256int ql2xexchoffld = 1024;
 257module_param(ql2xexchoffld, uint, 0644);
 258MODULE_PARM_DESC(ql2xexchoffld,
 259        "Number of target exchanges.");
 260
 261int ql2xiniexchg = 1024;
 262module_param(ql2xiniexchg, uint, 0644);
 263MODULE_PARM_DESC(ql2xiniexchg,
 264        "Number of initiator exchanges.");
 265
 266int ql2xfwholdabts;
 267module_param(ql2xfwholdabts, int, S_IRUGO);
 268MODULE_PARM_DESC(ql2xfwholdabts,
 269                "Allow FW to hold status IOCB until ABTS rsp received. "
 270                "0 (Default) Do not set fw option. "
 271                "1 - Set fw option to hold ABTS.");
 272
 273int ql2xmvasynctoatio = 1;
 274module_param(ql2xmvasynctoatio, int, S_IRUGO|S_IWUSR);
 275MODULE_PARM_DESC(ql2xmvasynctoatio,
 276                "Move PUREX, ABTS RX and RIDA IOCBs to ATIOQ"
 277                "0 (Default). Do not move IOCBs"
 278                "1 - Move IOCBs.");
 279
 280int ql2xautodetectsfp = 1;
 281module_param(ql2xautodetectsfp, int, 0444);
 282MODULE_PARM_DESC(ql2xautodetectsfp,
 283                 "Detect SFP range and set appropriate distance.\n"
 284                 "1 (Default): Enable\n");
 285
 286int ql2xenablemsix = 1;
 287module_param(ql2xenablemsix, int, 0444);
 288MODULE_PARM_DESC(ql2xenablemsix,
 289                 "Set to enable MSI or MSI-X interrupt mechanism.\n"
 290                 " Default is 1, enable MSI-X interrupt mechanism.\n"
 291                 " 0 -- enable traditional pin-based mechanism.\n"
 292                 " 1 -- enable MSI-X interrupt mechanism.\n"
 293                 " 2 -- enable MSI interrupt mechanism.\n");
 294
 295int qla2xuseresexchforels;
 296module_param(qla2xuseresexchforels, int, 0444);
 297MODULE_PARM_DESC(qla2xuseresexchforels,
 298                 "Reserve 1/2 of emergency exchanges for ELS.\n"
 299                 " 0 (default): disabled");
 300
 301static int ql2xprotmask;
 302module_param(ql2xprotmask, int, 0644);
 303MODULE_PARM_DESC(ql2xprotmask,
 304                 "Override DIF/DIX protection capabilities mask\n"
 305                 "Default is 0 which sets protection mask based on "
 306                 "capabilities reported by HBA firmware.\n");
 307
 308static int ql2xprotguard;
 309module_param(ql2xprotguard, int, 0644);
 310MODULE_PARM_DESC(ql2xprotguard, "Override choice of DIX checksum\n"
 311                 "  0 -- Let HBA firmware decide\n"
 312                 "  1 -- Force T10 CRC\n"
 313                 "  2 -- Force IP checksum\n");
 314
 315int ql2xdifbundlinginternalbuffers;
 316module_param(ql2xdifbundlinginternalbuffers, int, 0644);
 317MODULE_PARM_DESC(ql2xdifbundlinginternalbuffers,
 318    "Force using internal buffers for DIF information\n"
 319    "0 (Default). Based on check.\n"
 320    "1 Force using internal buffers\n");
 321
 322int ql2xsmartsan;
 323module_param(ql2xsmartsan, int, 0444);
 324module_param_named(smartsan, ql2xsmartsan, int, 0444);
 325MODULE_PARM_DESC(ql2xsmartsan,
 326                "Send SmartSAN Management Attributes for FDMI Registration."
 327                " Default is 0 - No SmartSAN registration,"
 328                " 1 - Register SmartSAN Management Attributes.");
 329
 330int ql2xrdpenable;
 331module_param(ql2xrdpenable, int, 0444);
 332module_param_named(rdpenable, ql2xrdpenable, int, 0444);
 333MODULE_PARM_DESC(ql2xrdpenable,
 334                "Enables RDP responses. "
 335                "0 - no RDP responses (default). "
 336                "1 - provide RDP responses.");
 337int ql2xabts_wait_nvme = 1;
 338module_param(ql2xabts_wait_nvme, int, 0444);
 339MODULE_PARM_DESC(ql2xabts_wait_nvme,
 340                 "To wait for ABTS response on I/O timeouts for NVMe. (default: 1)");
 341
 342
 343static void qla2x00_clear_drv_active(struct qla_hw_data *);
 344static void qla2x00_free_device(scsi_qla_host_t *);
 345static int qla2xxx_map_queues(struct Scsi_Host *shost);
 346static void qla2x00_destroy_deferred_work(struct qla_hw_data *);
 347
 348static struct scsi_transport_template *qla2xxx_transport_template = NULL;
 349struct scsi_transport_template *qla2xxx_transport_vport_template = NULL;
 350
 351/* TODO Convert to inlines
 352 *
 353 * Timer routines
 354 */
 355
 356__inline__ void
 357qla2x00_start_timer(scsi_qla_host_t *vha, unsigned long interval)
 358{
 359        timer_setup(&vha->timer, qla2x00_timer, 0);
 360        vha->timer.expires = jiffies + interval * HZ;
 361        add_timer(&vha->timer);
 362        vha->timer_active = 1;
 363}
 364
 365static inline void
 366qla2x00_restart_timer(scsi_qla_host_t *vha, unsigned long interval)
 367{
 368        /* Currently used for 82XX only. */
 369        if (vha->device_flags & DFLG_DEV_FAILED) {
 370                ql_dbg(ql_dbg_timer, vha, 0x600d,
 371                    "Device in a failed state, returning.\n");
 372                return;
 373        }
 374
 375        mod_timer(&vha->timer, jiffies + interval * HZ);
 376}
 377
 378static __inline__ void
 379qla2x00_stop_timer(scsi_qla_host_t *vha)
 380{
 381        del_timer_sync(&vha->timer);
 382        vha->timer_active = 0;
 383}
 384
 385static int qla2x00_do_dpc(void *data);
 386
 387static void qla2x00_rst_aen(scsi_qla_host_t *);
 388
 389static int qla2x00_mem_alloc(struct qla_hw_data *, uint16_t, uint16_t,
 390        struct req_que **, struct rsp_que **);
 391static void qla2x00_free_fw_dump(struct qla_hw_data *);
 392static void qla2x00_mem_free(struct qla_hw_data *);
 393int qla2xxx_mqueuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd,
 394        struct qla_qpair *qpair);
 395
 396/* -------------------------------------------------------------------------- */
 397static void qla_init_base_qpair(struct scsi_qla_host *vha, struct req_que *req,
 398    struct rsp_que *rsp)
 399{
 400        struct qla_hw_data *ha = vha->hw;
 401
 402        rsp->qpair = ha->base_qpair;
 403        rsp->req = req;
 404        ha->base_qpair->hw = ha;
 405        ha->base_qpair->req = req;
 406        ha->base_qpair->rsp = rsp;
 407        ha->base_qpair->vha = vha;
 408        ha->base_qpair->qp_lock_ptr = &ha->hardware_lock;
 409        ha->base_qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
 410        ha->base_qpair->msix = &ha->msix_entries[QLA_MSIX_RSP_Q];
 411        ha->base_qpair->srb_mempool = ha->srb_mempool;
 412        INIT_LIST_HEAD(&ha->base_qpair->hints_list);
 413        ha->base_qpair->enable_class_2 = ql2xenableclass2;
 414        /* init qpair to this cpu. Will adjust at run time. */
 415        qla_cpu_update(rsp->qpair, raw_smp_processor_id());
 416        ha->base_qpair->pdev = ha->pdev;
 417
 418        if (IS_QLA27XX(ha) || IS_QLA83XX(ha) || IS_QLA28XX(ha))
 419                ha->base_qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
 420}
 421
 422static int qla2x00_alloc_queues(struct qla_hw_data *ha, struct req_que *req,
 423                                struct rsp_que *rsp)
 424{
 425        scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
 426
 427        ha->req_q_map = kcalloc(ha->max_req_queues, sizeof(struct req_que *),
 428                                GFP_KERNEL);
 429        if (!ha->req_q_map) {
 430                ql_log(ql_log_fatal, vha, 0x003b,
 431                    "Unable to allocate memory for request queue ptrs.\n");
 432                goto fail_req_map;
 433        }
 434
 435        ha->rsp_q_map = kcalloc(ha->max_rsp_queues, sizeof(struct rsp_que *),
 436                                GFP_KERNEL);
 437        if (!ha->rsp_q_map) {
 438                ql_log(ql_log_fatal, vha, 0x003c,
 439                    "Unable to allocate memory for response queue ptrs.\n");
 440                goto fail_rsp_map;
 441        }
 442
 443        ha->base_qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
 444        if (ha->base_qpair == NULL) {
 445                ql_log(ql_log_warn, vha, 0x00e0,
 446                    "Failed to allocate base queue pair memory.\n");
 447                goto fail_base_qpair;
 448        }
 449
 450        qla_init_base_qpair(vha, req, rsp);
 451
 452        if ((ql2xmqsupport || ql2xnvmeenable) && ha->max_qpairs) {
 453                ha->queue_pair_map = kcalloc(ha->max_qpairs, sizeof(struct qla_qpair *),
 454                        GFP_KERNEL);
 455                if (!ha->queue_pair_map) {
 456                        ql_log(ql_log_fatal, vha, 0x0180,
 457                            "Unable to allocate memory for queue pair ptrs.\n");
 458                        goto fail_qpair_map;
 459                }
 460        }
 461
 462        /*
 463         * Make sure we record at least the request and response queue zero in
 464         * case we need to free them if part of the probe fails.
 465         */
 466        ha->rsp_q_map[0] = rsp;
 467        ha->req_q_map[0] = req;
 468        set_bit(0, ha->rsp_qid_map);
 469        set_bit(0, ha->req_qid_map);
 470        return 0;
 471
 472fail_qpair_map:
 473        kfree(ha->base_qpair);
 474        ha->base_qpair = NULL;
 475fail_base_qpair:
 476        kfree(ha->rsp_q_map);
 477        ha->rsp_q_map = NULL;
 478fail_rsp_map:
 479        kfree(ha->req_q_map);
 480        ha->req_q_map = NULL;
 481fail_req_map:
 482        return -ENOMEM;
 483}
 484
 485static void qla2x00_free_req_que(struct qla_hw_data *ha, struct req_que *req)
 486{
 487        if (IS_QLAFX00(ha)) {
 488                if (req && req->ring_fx00)
 489                        dma_free_coherent(&ha->pdev->dev,
 490                            (req->length_fx00 + 1) * sizeof(request_t),
 491                            req->ring_fx00, req->dma_fx00);
 492        } else if (req && req->ring)
 493                dma_free_coherent(&ha->pdev->dev,
 494                (req->length + 1) * sizeof(request_t),
 495                req->ring, req->dma);
 496
 497        if (req)
 498                kfree(req->outstanding_cmds);
 499
 500        kfree(req);
 501}
 502
 503static void qla2x00_free_rsp_que(struct qla_hw_data *ha, struct rsp_que *rsp)
 504{
 505        if (IS_QLAFX00(ha)) {
 506                if (rsp && rsp->ring_fx00)
 507                        dma_free_coherent(&ha->pdev->dev,
 508                            (rsp->length_fx00 + 1) * sizeof(request_t),
 509                            rsp->ring_fx00, rsp->dma_fx00);
 510        } else if (rsp && rsp->ring) {
 511                dma_free_coherent(&ha->pdev->dev,
 512                (rsp->length + 1) * sizeof(response_t),
 513                rsp->ring, rsp->dma);
 514        }
 515        kfree(rsp);
 516}
 517
 518static void qla2x00_free_queues(struct qla_hw_data *ha)
 519{
 520        struct req_que *req;
 521        struct rsp_que *rsp;
 522        int cnt;
 523        unsigned long flags;
 524
 525        if (ha->queue_pair_map) {
 526                kfree(ha->queue_pair_map);
 527                ha->queue_pair_map = NULL;
 528        }
 529        if (ha->base_qpair) {
 530                kfree(ha->base_qpair);
 531                ha->base_qpair = NULL;
 532        }
 533
 534        spin_lock_irqsave(&ha->hardware_lock, flags);
 535        for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
 536                if (!test_bit(cnt, ha->req_qid_map))
 537                        continue;
 538
 539                req = ha->req_q_map[cnt];
 540                clear_bit(cnt, ha->req_qid_map);
 541                ha->req_q_map[cnt] = NULL;
 542
 543                spin_unlock_irqrestore(&ha->hardware_lock, flags);
 544                qla2x00_free_req_que(ha, req);
 545                spin_lock_irqsave(&ha->hardware_lock, flags);
 546        }
 547        spin_unlock_irqrestore(&ha->hardware_lock, flags);
 548
 549        kfree(ha->req_q_map);
 550        ha->req_q_map = NULL;
 551
 552
 553        spin_lock_irqsave(&ha->hardware_lock, flags);
 554        for (cnt = 0; cnt < ha->max_rsp_queues; cnt++) {
 555                if (!test_bit(cnt, ha->rsp_qid_map))
 556                        continue;
 557
 558                rsp = ha->rsp_q_map[cnt];
 559                clear_bit(cnt, ha->rsp_qid_map);
 560                ha->rsp_q_map[cnt] =  NULL;
 561                spin_unlock_irqrestore(&ha->hardware_lock, flags);
 562                qla2x00_free_rsp_que(ha, rsp);
 563                spin_lock_irqsave(&ha->hardware_lock, flags);
 564        }
 565        spin_unlock_irqrestore(&ha->hardware_lock, flags);
 566
 567        kfree(ha->rsp_q_map);
 568        ha->rsp_q_map = NULL;
 569}
 570
 571static char *
 572qla2x00_pci_info_str(struct scsi_qla_host *vha, char *str, size_t str_len)
 573{
 574        struct qla_hw_data *ha = vha->hw;
 575        static const char *const pci_bus_modes[] = {
 576                "33", "66", "100", "133",
 577        };
 578        uint16_t pci_bus;
 579
 580        pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
 581        if (pci_bus) {
 582                snprintf(str, str_len, "PCI-X (%s MHz)",
 583                         pci_bus_modes[pci_bus]);
 584        } else {
 585                pci_bus = (ha->pci_attr & BIT_8) >> 8;
 586                snprintf(str, str_len, "PCI (%s MHz)", pci_bus_modes[pci_bus]);
 587        }
 588
 589        return str;
 590}
 591
 592static char *
 593qla24xx_pci_info_str(struct scsi_qla_host *vha, char *str, size_t str_len)
 594{
 595        static const char *const pci_bus_modes[] = {
 596                "33", "66", "100", "133",
 597        };
 598        struct qla_hw_data *ha = vha->hw;
 599        uint32_t pci_bus;
 600
 601        if (pci_is_pcie(ha->pdev)) {
 602                uint32_t lstat, lspeed, lwidth;
 603                const char *speed_str;
 604
 605                pcie_capability_read_dword(ha->pdev, PCI_EXP_LNKCAP, &lstat);
 606                lspeed = lstat & PCI_EXP_LNKCAP_SLS;
 607                lwidth = (lstat & PCI_EXP_LNKCAP_MLW) >> 4;
 608
 609                switch (lspeed) {
 610                case 1:
 611                        speed_str = "2.5GT/s";
 612                        break;
 613                case 2:
 614                        speed_str = "5.0GT/s";
 615                        break;
 616                case 3:
 617                        speed_str = "8.0GT/s";
 618                        break;
 619                case 4:
 620                        speed_str = "16.0GT/s";
 621                        break;
 622                default:
 623                        speed_str = "<unknown>";
 624                        break;
 625                }
 626                snprintf(str, str_len, "PCIe (%s x%d)", speed_str, lwidth);
 627
 628                return str;
 629        }
 630
 631        pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
 632        if (pci_bus == 0 || pci_bus == 8)
 633                snprintf(str, str_len, "PCI (%s MHz)",
 634                         pci_bus_modes[pci_bus >> 3]);
 635        else
 636                snprintf(str, str_len, "PCI-X Mode %d (%s MHz)",
 637                         pci_bus & 4 ? 2 : 1,
 638                         pci_bus_modes[pci_bus & 3]);
 639
 640        return str;
 641}
 642
 643static char *
 644qla2x00_fw_version_str(struct scsi_qla_host *vha, char *str, size_t size)
 645{
 646        char un_str[10];
 647        struct qla_hw_data *ha = vha->hw;
 648
 649        snprintf(str, size, "%d.%02d.%02d ", ha->fw_major_version,
 650            ha->fw_minor_version, ha->fw_subminor_version);
 651
 652        if (ha->fw_attributes & BIT_9) {
 653                strcat(str, "FLX");
 654                return (str);
 655        }
 656
 657        switch (ha->fw_attributes & 0xFF) {
 658        case 0x7:
 659                strcat(str, "EF");
 660                break;
 661        case 0x17:
 662                strcat(str, "TP");
 663                break;
 664        case 0x37:
 665                strcat(str, "IP");
 666                break;
 667        case 0x77:
 668                strcat(str, "VI");
 669                break;
 670        default:
 671                sprintf(un_str, "(%x)", ha->fw_attributes);
 672                strcat(str, un_str);
 673                break;
 674        }
 675        if (ha->fw_attributes & 0x100)
 676                strcat(str, "X");
 677
 678        return (str);
 679}
 680
 681static char *
 682qla24xx_fw_version_str(struct scsi_qla_host *vha, char *str, size_t size)
 683{
 684        struct qla_hw_data *ha = vha->hw;
 685
 686        snprintf(str, size, "%d.%02d.%02d (%x)", ha->fw_major_version,
 687            ha->fw_minor_version, ha->fw_subminor_version, ha->fw_attributes);
 688        return str;
 689}
 690
 691void qla2x00_sp_free_dma(srb_t *sp)
 692{
 693        struct qla_hw_data *ha = sp->vha->hw;
 694        struct scsi_cmnd *cmd = GET_CMD_SP(sp);
 695
 696        if (sp->flags & SRB_DMA_VALID) {
 697                scsi_dma_unmap(cmd);
 698                sp->flags &= ~SRB_DMA_VALID;
 699        }
 700
 701        if (sp->flags & SRB_CRC_PROT_DMA_VALID) {
 702                dma_unmap_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
 703                    scsi_prot_sg_count(cmd), cmd->sc_data_direction);
 704                sp->flags &= ~SRB_CRC_PROT_DMA_VALID;
 705        }
 706
 707        if (sp->flags & SRB_CRC_CTX_DSD_VALID) {
 708                /* List assured to be having elements */
 709                qla2x00_clean_dsd_pool(ha, sp->u.scmd.crc_ctx);
 710                sp->flags &= ~SRB_CRC_CTX_DSD_VALID;
 711        }
 712
 713        if (sp->flags & SRB_CRC_CTX_DMA_VALID) {
 714                struct crc_context *ctx0 = sp->u.scmd.crc_ctx;
 715
 716                dma_pool_free(ha->dl_dma_pool, ctx0, ctx0->crc_ctx_dma);
 717                sp->flags &= ~SRB_CRC_CTX_DMA_VALID;
 718        }
 719
 720        if (sp->flags & SRB_FCP_CMND_DMA_VALID) {
 721                struct ct6_dsd *ctx1 = sp->u.scmd.ct6_ctx;
 722
 723                dma_pool_free(ha->fcp_cmnd_dma_pool, ctx1->fcp_cmnd,
 724                    ctx1->fcp_cmnd_dma);
 725                list_splice(&ctx1->dsd_list, &ha->gbl_dsd_list);
 726                ha->gbl_dsd_inuse -= ctx1->dsd_use_cnt;
 727                ha->gbl_dsd_avail += ctx1->dsd_use_cnt;
 728                mempool_free(ctx1, ha->ctx_mempool);
 729        }
 730}
 731
 732void qla2x00_sp_compl(srb_t *sp, int res)
 733{
 734        struct scsi_cmnd *cmd = GET_CMD_SP(sp);
 735        struct completion *comp = sp->comp;
 736
 737        sp->free(sp);
 738        cmd->result = res;
 739        CMD_SP(cmd) = NULL;
 740        cmd->scsi_done(cmd);
 741        if (comp)
 742                complete(comp);
 743}
 744
 745void qla2xxx_qpair_sp_free_dma(srb_t *sp)
 746{
 747        struct scsi_cmnd *cmd = GET_CMD_SP(sp);
 748        struct qla_hw_data *ha = sp->fcport->vha->hw;
 749
 750        if (sp->flags & SRB_DMA_VALID) {
 751                scsi_dma_unmap(cmd);
 752                sp->flags &= ~SRB_DMA_VALID;
 753        }
 754
 755        if (sp->flags & SRB_CRC_PROT_DMA_VALID) {
 756                dma_unmap_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
 757                    scsi_prot_sg_count(cmd), cmd->sc_data_direction);
 758                sp->flags &= ~SRB_CRC_PROT_DMA_VALID;
 759        }
 760
 761        if (sp->flags & SRB_CRC_CTX_DSD_VALID) {
 762                /* List assured to be having elements */
 763                qla2x00_clean_dsd_pool(ha, sp->u.scmd.crc_ctx);
 764                sp->flags &= ~SRB_CRC_CTX_DSD_VALID;
 765        }
 766
 767        if (sp->flags & SRB_DIF_BUNDL_DMA_VALID) {
 768                struct crc_context *difctx = sp->u.scmd.crc_ctx;
 769                struct dsd_dma *dif_dsd, *nxt_dsd;
 770
 771                list_for_each_entry_safe(dif_dsd, nxt_dsd,
 772                    &difctx->ldif_dma_hndl_list, list) {
 773                        list_del(&dif_dsd->list);
 774                        dma_pool_free(ha->dif_bundl_pool, dif_dsd->dsd_addr,
 775                            dif_dsd->dsd_list_dma);
 776                        kfree(dif_dsd);
 777                        difctx->no_dif_bundl--;
 778                }
 779
 780                list_for_each_entry_safe(dif_dsd, nxt_dsd,
 781                    &difctx->ldif_dsd_list, list) {
 782                        list_del(&dif_dsd->list);
 783                        dma_pool_free(ha->dl_dma_pool, dif_dsd->dsd_addr,
 784                            dif_dsd->dsd_list_dma);
 785                        kfree(dif_dsd);
 786                        difctx->no_ldif_dsd--;
 787                }
 788
 789                if (difctx->no_ldif_dsd) {
 790                        ql_dbg(ql_dbg_tgt+ql_dbg_verbose, sp->vha, 0xe022,
 791                            "%s: difctx->no_ldif_dsd=%x\n",
 792                            __func__, difctx->no_ldif_dsd);
 793                }
 794
 795                if (difctx->no_dif_bundl) {
 796                        ql_dbg(ql_dbg_tgt+ql_dbg_verbose, sp->vha, 0xe022,
 797                            "%s: difctx->no_dif_bundl=%x\n",
 798                            __func__, difctx->no_dif_bundl);
 799                }
 800                sp->flags &= ~SRB_DIF_BUNDL_DMA_VALID;
 801        }
 802
 803        if (sp->flags & SRB_FCP_CMND_DMA_VALID) {
 804                struct ct6_dsd *ctx1 = sp->u.scmd.ct6_ctx;
 805
 806                dma_pool_free(ha->fcp_cmnd_dma_pool, ctx1->fcp_cmnd,
 807                    ctx1->fcp_cmnd_dma);
 808                list_splice(&ctx1->dsd_list, &ha->gbl_dsd_list);
 809                ha->gbl_dsd_inuse -= ctx1->dsd_use_cnt;
 810                ha->gbl_dsd_avail += ctx1->dsd_use_cnt;
 811                mempool_free(ctx1, ha->ctx_mempool);
 812                sp->flags &= ~SRB_FCP_CMND_DMA_VALID;
 813        }
 814
 815        if (sp->flags & SRB_CRC_CTX_DMA_VALID) {
 816                struct crc_context *ctx0 = sp->u.scmd.crc_ctx;
 817
 818                dma_pool_free(ha->dl_dma_pool, ctx0, ctx0->crc_ctx_dma);
 819                sp->flags &= ~SRB_CRC_CTX_DMA_VALID;
 820        }
 821}
 822
 823void qla2xxx_qpair_sp_compl(srb_t *sp, int res)
 824{
 825        struct scsi_cmnd *cmd = GET_CMD_SP(sp);
 826        struct completion *comp = sp->comp;
 827
 828        sp->free(sp);
 829        cmd->result = res;
 830        CMD_SP(cmd) = NULL;
 831        cmd->scsi_done(cmd);
 832        if (comp)
 833                complete(comp);
 834}
 835
 836static int
 837qla2xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
 838{
 839        scsi_qla_host_t *vha = shost_priv(host);
 840        fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
 841        struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
 842        struct qla_hw_data *ha = vha->hw;
 843        struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
 844        srb_t *sp;
 845        int rval;
 846
 847        if (unlikely(test_bit(UNLOADING, &base_vha->dpc_flags)) ||
 848            WARN_ON_ONCE(!rport)) {
 849                cmd->result = DID_NO_CONNECT << 16;
 850                goto qc24_fail_command;
 851        }
 852
 853        if (ha->mqenable) {
 854                uint32_t tag;
 855                uint16_t hwq;
 856                struct qla_qpair *qpair = NULL;
 857
 858                tag = blk_mq_unique_tag(cmd->request);
 859                hwq = blk_mq_unique_tag_to_hwq(tag);
 860                qpair = ha->queue_pair_map[hwq];
 861
 862                if (qpair)
 863                        return qla2xxx_mqueuecommand(host, cmd, qpair);
 864        }
 865
 866        if (ha->flags.eeh_busy) {
 867                if (ha->flags.pci_channel_io_perm_failure) {
 868                        ql_dbg(ql_dbg_aer, vha, 0x9010,
 869                            "PCI Channel IO permanent failure, exiting "
 870                            "cmd=%p.\n", cmd);
 871                        cmd->result = DID_NO_CONNECT << 16;
 872                } else {
 873                        ql_dbg(ql_dbg_aer, vha, 0x9011,
 874                            "EEH_Busy, Requeuing the cmd=%p.\n", cmd);
 875                        cmd->result = DID_REQUEUE << 16;
 876                }
 877                goto qc24_fail_command;
 878        }
 879
 880        rval = fc_remote_port_chkready(rport);
 881        if (rval) {
 882                cmd->result = rval;
 883                ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3003,
 884                    "fc_remote_port_chkready failed for cmd=%p, rval=0x%x.\n",
 885                    cmd, rval);
 886                goto qc24_fail_command;
 887        }
 888
 889        if (!vha->flags.difdix_supported &&
 890                scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
 891                        ql_dbg(ql_dbg_io, vha, 0x3004,
 892                            "DIF Cap not reg, fail DIF capable cmd's:%p.\n",
 893                            cmd);
 894                        cmd->result = DID_NO_CONNECT << 16;
 895                        goto qc24_fail_command;
 896        }
 897
 898        if (!fcport || fcport->deleted) {
 899                cmd->result = DID_IMM_RETRY << 16;
 900                goto qc24_fail_command;
 901        }
 902
 903        if (atomic_read(&fcport->state) != FCS_ONLINE || fcport->deleted) {
 904                if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
 905                        atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
 906                        ql_dbg(ql_dbg_io, vha, 0x3005,
 907                            "Returning DNC, fcport_state=%d loop_state=%d.\n",
 908                            atomic_read(&fcport->state),
 909                            atomic_read(&base_vha->loop_state));
 910                        cmd->result = DID_NO_CONNECT << 16;
 911                        goto qc24_fail_command;
 912                }
 913                goto qc24_target_busy;
 914        }
 915
 916        /*
 917         * Return target busy if we've received a non-zero retry_delay_timer
 918         * in a FCP_RSP.
 919         */
 920        if (fcport->retry_delay_timestamp == 0) {
 921                /* retry delay not set */
 922        } else if (time_after(jiffies, fcport->retry_delay_timestamp))
 923                fcport->retry_delay_timestamp = 0;
 924        else
 925                goto qc24_target_busy;
 926
 927        sp = scsi_cmd_priv(cmd);
 928        qla2xxx_init_sp(sp, vha, vha->hw->base_qpair, fcport);
 929
 930        sp->u.scmd.cmd = cmd;
 931        sp->type = SRB_SCSI_CMD;
 932
 933        CMD_SP(cmd) = (void *)sp;
 934        sp->free = qla2x00_sp_free_dma;
 935        sp->done = qla2x00_sp_compl;
 936
 937        rval = ha->isp_ops->start_scsi(sp);
 938        if (rval != QLA_SUCCESS) {
 939                ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3013,
 940                    "Start scsi failed rval=%d for cmd=%p.\n", rval, cmd);
 941                goto qc24_host_busy_free_sp;
 942        }
 943
 944        return 0;
 945
 946qc24_host_busy_free_sp:
 947        sp->free(sp);
 948
 949qc24_target_busy:
 950        return SCSI_MLQUEUE_TARGET_BUSY;
 951
 952qc24_fail_command:
 953        cmd->scsi_done(cmd);
 954
 955        return 0;
 956}
 957
 958/* For MQ supported I/O */
 959int
 960qla2xxx_mqueuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd,
 961    struct qla_qpair *qpair)
 962{
 963        scsi_qla_host_t *vha = shost_priv(host);
 964        fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
 965        struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
 966        struct qla_hw_data *ha = vha->hw;
 967        struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
 968        srb_t *sp;
 969        int rval;
 970
 971        rval = rport ? fc_remote_port_chkready(rport) : (DID_NO_CONNECT << 16);
 972        if (rval) {
 973                cmd->result = rval;
 974                ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3076,
 975                    "fc_remote_port_chkready failed for cmd=%p, rval=0x%x.\n",
 976                    cmd, rval);
 977                goto qc24_fail_command;
 978        }
 979
 980        if (!qpair->online) {
 981                ql_dbg(ql_dbg_io, vha, 0x3077,
 982                       "qpair not online. eeh_busy=%d.\n", ha->flags.eeh_busy);
 983                cmd->result = DID_NO_CONNECT << 16;
 984                goto qc24_fail_command;
 985        }
 986
 987        if (!fcport || fcport->deleted) {
 988                cmd->result = DID_IMM_RETRY << 16;
 989                goto qc24_fail_command;
 990        }
 991
 992        if (atomic_read(&fcport->state) != FCS_ONLINE || fcport->deleted) {
 993                if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
 994                        atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
 995                        ql_dbg(ql_dbg_io, vha, 0x3077,
 996                            "Returning DNC, fcport_state=%d loop_state=%d.\n",
 997                            atomic_read(&fcport->state),
 998                            atomic_read(&base_vha->loop_state));
 999                        cmd->result = DID_NO_CONNECT << 16;
1000                        goto qc24_fail_command;
1001                }
1002                goto qc24_target_busy;
1003        }
1004
1005        /*
1006         * Return target busy if we've received a non-zero retry_delay_timer
1007         * in a FCP_RSP.
1008         */
1009        if (fcport->retry_delay_timestamp == 0) {
1010                /* retry delay not set */
1011        } else if (time_after(jiffies, fcport->retry_delay_timestamp))
1012                fcport->retry_delay_timestamp = 0;
1013        else
1014                goto qc24_target_busy;
1015
1016        sp = scsi_cmd_priv(cmd);
1017        qla2xxx_init_sp(sp, vha, qpair, fcport);
1018
1019        sp->u.scmd.cmd = cmd;
1020        sp->type = SRB_SCSI_CMD;
1021        CMD_SP(cmd) = (void *)sp;
1022        sp->free = qla2xxx_qpair_sp_free_dma;
1023        sp->done = qla2xxx_qpair_sp_compl;
1024
1025        rval = ha->isp_ops->start_scsi_mq(sp);
1026        if (rval != QLA_SUCCESS) {
1027                ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3078,
1028                    "Start scsi failed rval=%d for cmd=%p.\n", rval, cmd);
1029                goto qc24_host_busy_free_sp;
1030        }
1031
1032        return 0;
1033
1034qc24_host_busy_free_sp:
1035        sp->free(sp);
1036
1037qc24_target_busy:
1038        return SCSI_MLQUEUE_TARGET_BUSY;
1039
1040qc24_fail_command:
1041        cmd->scsi_done(cmd);
1042
1043        return 0;
1044}
1045
1046/*
1047 * qla2x00_eh_wait_on_command
1048 *    Waits for the command to be returned by the Firmware for some
1049 *    max time.
1050 *
1051 * Input:
1052 *    cmd = Scsi Command to wait on.
1053 *
1054 * Return:
1055 *    Completed in time : QLA_SUCCESS
1056 *    Did not complete in time : QLA_FUNCTION_FAILED
1057 */
1058static int
1059qla2x00_eh_wait_on_command(struct scsi_cmnd *cmd)
1060{
1061#define ABORT_POLLING_PERIOD    1000
1062#define ABORT_WAIT_ITER         ((2 * 1000) / (ABORT_POLLING_PERIOD))
1063        unsigned long wait_iter = ABORT_WAIT_ITER;
1064        scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1065        struct qla_hw_data *ha = vha->hw;
1066        int ret = QLA_SUCCESS;
1067
1068        if (unlikely(pci_channel_offline(ha->pdev)) || ha->flags.eeh_busy) {
1069                ql_dbg(ql_dbg_taskm, vha, 0x8005,
1070                    "Return:eh_wait.\n");
1071                return ret;
1072        }
1073
1074        while (CMD_SP(cmd) && wait_iter--) {
1075                msleep(ABORT_POLLING_PERIOD);
1076        }
1077        if (CMD_SP(cmd))
1078                ret = QLA_FUNCTION_FAILED;
1079
1080        return ret;
1081}
1082
1083/*
1084 * qla2x00_wait_for_hba_online
1085 *    Wait till the HBA is online after going through
1086 *    <= MAX_RETRIES_OF_ISP_ABORT  or
1087 *    finally HBA is disabled ie marked offline
1088 *
1089 * Input:
1090 *     ha - pointer to host adapter structure
1091 *
1092 * Note:
1093 *    Does context switching-Release SPIN_LOCK
1094 *    (if any) before calling this routine.
1095 *
1096 * Return:
1097 *    Success (Adapter is online) : 0
1098 *    Failed  (Adapter is offline/disabled) : 1
1099 */
1100int
1101qla2x00_wait_for_hba_online(scsi_qla_host_t *vha)
1102{
1103        int             return_status;
1104        unsigned long   wait_online;
1105        struct qla_hw_data *ha = vha->hw;
1106        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1107
1108        wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
1109        while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
1110            test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
1111            test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
1112            ha->dpc_active) && time_before(jiffies, wait_online)) {
1113
1114                msleep(1000);
1115        }
1116        if (base_vha->flags.online)
1117                return_status = QLA_SUCCESS;
1118        else
1119                return_status = QLA_FUNCTION_FAILED;
1120
1121        return (return_status);
1122}
1123
1124static inline int test_fcport_count(scsi_qla_host_t *vha)
1125{
1126        struct qla_hw_data *ha = vha->hw;
1127        unsigned long flags;
1128        int res;
1129        /* Return 0 = sleep, x=wake */
1130
1131        spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1132        ql_dbg(ql_dbg_init, vha, 0x00ec,
1133            "tgt %p, fcport_count=%d\n",
1134            vha, vha->fcport_count);
1135        res = (vha->fcport_count == 0);
1136        if  (res) {
1137                struct fc_port *fcport;
1138
1139                list_for_each_entry(fcport, &vha->vp_fcports, list) {
1140                        if (fcport->deleted != QLA_SESS_DELETED) {
1141                                /* session(s) may not be fully logged in
1142                                 * (ie fcport_count=0), but session
1143                                 * deletion thread(s) may be inflight.
1144                                 */
1145
1146                                res = 0;
1147                                break;
1148                        }
1149                }
1150        }
1151        spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1152
1153        return res;
1154}
1155
1156/*
1157 * qla2x00_wait_for_sess_deletion can only be called from remove_one.
1158 * it has dependency on UNLOADING flag to stop device discovery
1159 */
1160void
1161qla2x00_wait_for_sess_deletion(scsi_qla_host_t *vha)
1162{
1163        u8 i;
1164
1165        qla2x00_mark_all_devices_lost(vha);
1166
1167        for (i = 0; i < 10; i++) {
1168                if (wait_event_timeout(vha->fcport_waitQ,
1169                    test_fcport_count(vha), HZ) > 0)
1170                        break;
1171        }
1172
1173        flush_workqueue(vha->hw->wq);
1174}
1175
1176/*
1177 * qla2x00_wait_for_hba_ready
1178 * Wait till the HBA is ready before doing driver unload
1179 *
1180 * Input:
1181 *     ha - pointer to host adapter structure
1182 *
1183 * Note:
1184 *    Does context switching-Release SPIN_LOCK
1185 *    (if any) before calling this routine.
1186 *
1187 */
1188static void
1189qla2x00_wait_for_hba_ready(scsi_qla_host_t *vha)
1190{
1191        struct qla_hw_data *ha = vha->hw;
1192        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1193
1194        while ((qla2x00_reset_active(vha) || ha->dpc_active ||
1195                ha->flags.mbox_busy) ||
1196               test_bit(FX00_RESET_RECOVERY, &vha->dpc_flags) ||
1197               test_bit(FX00_TARGET_SCAN, &vha->dpc_flags)) {
1198                if (test_bit(UNLOADING, &base_vha->dpc_flags))
1199                        break;
1200                msleep(1000);
1201        }
1202}
1203
1204int
1205qla2x00_wait_for_chip_reset(scsi_qla_host_t *vha)
1206{
1207        int             return_status;
1208        unsigned long   wait_reset;
1209        struct qla_hw_data *ha = vha->hw;
1210        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1211
1212        wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
1213        while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
1214            test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
1215            test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
1216            ha->dpc_active) && time_before(jiffies, wait_reset)) {
1217
1218                msleep(1000);
1219
1220                if (!test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
1221                    ha->flags.chip_reset_done)
1222                        break;
1223        }
1224        if (ha->flags.chip_reset_done)
1225                return_status = QLA_SUCCESS;
1226        else
1227                return_status = QLA_FUNCTION_FAILED;
1228
1229        return return_status;
1230}
1231
1232/**************************************************************************
1233* qla2xxx_eh_abort
1234*
1235* Description:
1236*    The abort function will abort the specified command.
1237*
1238* Input:
1239*    cmd = Linux SCSI command packet to be aborted.
1240*
1241* Returns:
1242*    Either SUCCESS or FAILED.
1243*
1244* Note:
1245*    Only return FAILED if command not returned by firmware.
1246**************************************************************************/
1247static int
1248qla2xxx_eh_abort(struct scsi_cmnd *cmd)
1249{
1250        scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1251        DECLARE_COMPLETION_ONSTACK(comp);
1252        srb_t *sp;
1253        int ret;
1254        unsigned int id;
1255        uint64_t lun;
1256        int rval;
1257        struct qla_hw_data *ha = vha->hw;
1258        uint32_t ratov_j;
1259        struct qla_qpair *qpair;
1260        unsigned long flags;
1261
1262        if (qla2x00_isp_reg_stat(ha)) {
1263                ql_log(ql_log_info, vha, 0x8042,
1264                    "PCI/Register disconnect, exiting.\n");
1265                qla_pci_set_eeh_busy(vha);
1266                return FAILED;
1267        }
1268
1269        ret = fc_block_scsi_eh(cmd);
1270        if (ret != 0)
1271                return ret;
1272
1273        sp = scsi_cmd_priv(cmd);
1274        qpair = sp->qpair;
1275
1276        vha->cmd_timeout_cnt++;
1277
1278        if ((sp->fcport && sp->fcport->deleted) || !qpair)
1279                return SUCCESS;
1280
1281        spin_lock_irqsave(qpair->qp_lock_ptr, flags);
1282        sp->comp = &comp;
1283        spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1284
1285
1286        id = cmd->device->id;
1287        lun = cmd->device->lun;
1288
1289        ql_dbg(ql_dbg_taskm, vha, 0x8002,
1290            "Aborting from RISC nexus=%ld:%d:%llu sp=%p cmd=%p handle=%x\n",
1291            vha->host_no, id, lun, sp, cmd, sp->handle);
1292
1293        /*
1294         * Abort will release the original Command/sp from FW. Let the
1295         * original command call scsi_done. In return, he will wakeup
1296         * this sleeping thread.
1297         */
1298        rval = ha->isp_ops->abort_command(sp);
1299
1300        ql_dbg(ql_dbg_taskm, vha, 0x8003,
1301               "Abort command mbx cmd=%p, rval=%x.\n", cmd, rval);
1302
1303        /* Wait for the command completion. */
1304        ratov_j = ha->r_a_tov/10 * 4 * 1000;
1305        ratov_j = msecs_to_jiffies(ratov_j);
1306        switch (rval) {
1307        case QLA_SUCCESS:
1308                if (!wait_for_completion_timeout(&comp, ratov_j)) {
1309                        ql_dbg(ql_dbg_taskm, vha, 0xffff,
1310                            "%s: Abort wait timer (4 * R_A_TOV[%d]) expired\n",
1311                            __func__, ha->r_a_tov/10);
1312                        ret = FAILED;
1313                } else {
1314                        ret = SUCCESS;
1315                }
1316                break;
1317        default:
1318                ret = FAILED;
1319                break;
1320        }
1321
1322        sp->comp = NULL;
1323
1324        ql_log(ql_log_info, vha, 0x801c,
1325            "Abort command issued nexus=%ld:%d:%llu -- %x.\n",
1326            vha->host_no, id, lun, ret);
1327
1328        return ret;
1329}
1330
1331/*
1332 * Returns: QLA_SUCCESS or QLA_FUNCTION_FAILED.
1333 */
1334int
1335qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *vha, unsigned int t,
1336        uint64_t l, enum nexus_wait_type type)
1337{
1338        int cnt, match, status;
1339        unsigned long flags;
1340        struct qla_hw_data *ha = vha->hw;
1341        struct req_que *req;
1342        srb_t *sp;
1343        struct scsi_cmnd *cmd;
1344
1345        status = QLA_SUCCESS;
1346
1347        spin_lock_irqsave(&ha->hardware_lock, flags);
1348        req = vha->req;
1349        for (cnt = 1; status == QLA_SUCCESS &&
1350                cnt < req->num_outstanding_cmds; cnt++) {
1351                sp = req->outstanding_cmds[cnt];
1352                if (!sp)
1353                        continue;
1354                if (sp->type != SRB_SCSI_CMD)
1355                        continue;
1356                if (vha->vp_idx != sp->vha->vp_idx)
1357                        continue;
1358                match = 0;
1359                cmd = GET_CMD_SP(sp);
1360                switch (type) {
1361                case WAIT_HOST:
1362                        match = 1;
1363                        break;
1364                case WAIT_TARGET:
1365                        match = cmd->device->id == t;
1366                        break;
1367                case WAIT_LUN:
1368                        match = (cmd->device->id == t &&
1369                                cmd->device->lun == l);
1370                        break;
1371                }
1372                if (!match)
1373                        continue;
1374
1375                spin_unlock_irqrestore(&ha->hardware_lock, flags);
1376                status = qla2x00_eh_wait_on_command(cmd);
1377                spin_lock_irqsave(&ha->hardware_lock, flags);
1378        }
1379        spin_unlock_irqrestore(&ha->hardware_lock, flags);
1380
1381        return status;
1382}
1383
1384static char *reset_errors[] = {
1385        "HBA not online",
1386        "HBA not ready",
1387        "Task management failed",
1388        "Waiting for command completions",
1389};
1390
1391static int
1392__qla2xxx_eh_generic_reset(char *name, enum nexus_wait_type type,
1393    struct scsi_cmnd *cmd, int (*do_reset)(struct fc_port *, uint64_t, int))
1394{
1395        scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1396        fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
1397        int err;
1398
1399        if (!fcport) {
1400                return FAILED;
1401        }
1402
1403        err = fc_block_scsi_eh(cmd);
1404        if (err != 0)
1405                return err;
1406
1407        if (fcport->deleted)
1408                return SUCCESS;
1409
1410        ql_log(ql_log_info, vha, 0x8009,
1411            "%s RESET ISSUED nexus=%ld:%d:%llu cmd=%p.\n", name, vha->host_no,
1412            cmd->device->id, cmd->device->lun, cmd);
1413
1414        err = 0;
1415        if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1416                ql_log(ql_log_warn, vha, 0x800a,
1417                    "Wait for hba online failed for cmd=%p.\n", cmd);
1418                goto eh_reset_failed;
1419        }
1420        err = 2;
1421        if (do_reset(fcport, cmd->device->lun, 1)
1422                != QLA_SUCCESS) {
1423                ql_log(ql_log_warn, vha, 0x800c,
1424                    "do_reset failed for cmd=%p.\n", cmd);
1425                goto eh_reset_failed;
1426        }
1427        err = 3;
1428        if (qla2x00_eh_wait_for_pending_commands(vha, cmd->device->id,
1429            cmd->device->lun, type) != QLA_SUCCESS) {
1430                ql_log(ql_log_warn, vha, 0x800d,
1431                    "wait for pending cmds failed for cmd=%p.\n", cmd);
1432                goto eh_reset_failed;
1433        }
1434
1435        ql_log(ql_log_info, vha, 0x800e,
1436            "%s RESET SUCCEEDED nexus:%ld:%d:%llu cmd=%p.\n", name,
1437            vha->host_no, cmd->device->id, cmd->device->lun, cmd);
1438
1439        return SUCCESS;
1440
1441eh_reset_failed:
1442        ql_log(ql_log_info, vha, 0x800f,
1443            "%s RESET FAILED: %s nexus=%ld:%d:%llu cmd=%p.\n", name,
1444            reset_errors[err], vha->host_no, cmd->device->id, cmd->device->lun,
1445            cmd);
1446        vha->reset_cmd_err_cnt++;
1447        return FAILED;
1448}
1449
1450static int
1451qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
1452{
1453        scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1454        struct qla_hw_data *ha = vha->hw;
1455
1456        if (qla2x00_isp_reg_stat(ha)) {
1457                ql_log(ql_log_info, vha, 0x803e,
1458                    "PCI/Register disconnect, exiting.\n");
1459                qla_pci_set_eeh_busy(vha);
1460                return FAILED;
1461        }
1462
1463        return __qla2xxx_eh_generic_reset("DEVICE", WAIT_LUN, cmd,
1464            ha->isp_ops->lun_reset);
1465}
1466
1467static int
1468qla2xxx_eh_target_reset(struct scsi_cmnd *cmd)
1469{
1470        scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1471        struct qla_hw_data *ha = vha->hw;
1472
1473        if (qla2x00_isp_reg_stat(ha)) {
1474                ql_log(ql_log_info, vha, 0x803f,
1475                    "PCI/Register disconnect, exiting.\n");
1476                qla_pci_set_eeh_busy(vha);
1477                return FAILED;
1478        }
1479
1480        return __qla2xxx_eh_generic_reset("TARGET", WAIT_TARGET, cmd,
1481            ha->isp_ops->target_reset);
1482}
1483
1484/**************************************************************************
1485* qla2xxx_eh_bus_reset
1486*
1487* Description:
1488*    The bus reset function will reset the bus and abort any executing
1489*    commands.
1490*
1491* Input:
1492*    cmd = Linux SCSI command packet of the command that cause the
1493*          bus reset.
1494*
1495* Returns:
1496*    SUCCESS/FAILURE (defined as macro in scsi.h).
1497*
1498**************************************************************************/
1499static int
1500qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
1501{
1502        scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1503        fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
1504        int ret = FAILED;
1505        unsigned int id;
1506        uint64_t lun;
1507        struct qla_hw_data *ha = vha->hw;
1508
1509        if (qla2x00_isp_reg_stat(ha)) {
1510                ql_log(ql_log_info, vha, 0x8040,
1511                    "PCI/Register disconnect, exiting.\n");
1512                qla_pci_set_eeh_busy(vha);
1513                return FAILED;
1514        }
1515
1516        id = cmd->device->id;
1517        lun = cmd->device->lun;
1518
1519        if (!fcport) {
1520                return ret;
1521        }
1522
1523        ret = fc_block_scsi_eh(cmd);
1524        if (ret != 0)
1525                return ret;
1526        ret = FAILED;
1527
1528        if (qla2x00_chip_is_down(vha))
1529                return ret;
1530
1531        ql_log(ql_log_info, vha, 0x8012,
1532            "BUS RESET ISSUED nexus=%ld:%d:%llu.\n", vha->host_no, id, lun);
1533
1534        if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1535                ql_log(ql_log_fatal, vha, 0x8013,
1536                    "Wait for hba online failed board disabled.\n");
1537                goto eh_bus_reset_done;
1538        }
1539
1540        if (qla2x00_loop_reset(vha) == QLA_SUCCESS)
1541                ret = SUCCESS;
1542
1543        if (ret == FAILED)
1544                goto eh_bus_reset_done;
1545
1546        /* Flush outstanding commands. */
1547        if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST) !=
1548            QLA_SUCCESS) {
1549                ql_log(ql_log_warn, vha, 0x8014,
1550                    "Wait for pending commands failed.\n");
1551                ret = FAILED;
1552        }
1553
1554eh_bus_reset_done:
1555        ql_log(ql_log_warn, vha, 0x802b,
1556            "BUS RESET %s nexus=%ld:%d:%llu.\n",
1557            (ret == FAILED) ? "FAILED" : "SUCCEEDED", vha->host_no, id, lun);
1558
1559        return ret;
1560}
1561
1562/**************************************************************************
1563* qla2xxx_eh_host_reset
1564*
1565* Description:
1566*    The reset function will reset the Adapter.
1567*
1568* Input:
1569*      cmd = Linux SCSI command packet of the command that cause the
1570*            adapter reset.
1571*
1572* Returns:
1573*      Either SUCCESS or FAILED.
1574*
1575* Note:
1576**************************************************************************/
1577static int
1578qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
1579{
1580        scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1581        struct qla_hw_data *ha = vha->hw;
1582        int ret = FAILED;
1583        unsigned int id;
1584        uint64_t lun;
1585        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1586
1587        if (qla2x00_isp_reg_stat(ha)) {
1588                ql_log(ql_log_info, vha, 0x8041,
1589                    "PCI/Register disconnect, exiting.\n");
1590                qla_pci_set_eeh_busy(vha);
1591                return SUCCESS;
1592        }
1593
1594        id = cmd->device->id;
1595        lun = cmd->device->lun;
1596
1597        ql_log(ql_log_info, vha, 0x8018,
1598            "ADAPTER RESET ISSUED nexus=%ld:%d:%llu.\n", vha->host_no, id, lun);
1599
1600        /*
1601         * No point in issuing another reset if one is active.  Also do not
1602         * attempt a reset if we are updating flash.
1603         */
1604        if (qla2x00_reset_active(vha) || ha->optrom_state != QLA_SWAITING)
1605                goto eh_host_reset_lock;
1606
1607        if (vha != base_vha) {
1608                if (qla2x00_vp_abort_isp(vha))
1609                        goto eh_host_reset_lock;
1610        } else {
1611                if (IS_P3P_TYPE(vha->hw)) {
1612                        if (!qla82xx_fcoe_ctx_reset(vha)) {
1613                                /* Ctx reset success */
1614                                ret = SUCCESS;
1615                                goto eh_host_reset_lock;
1616                        }
1617                        /* fall thru if ctx reset failed */
1618                }
1619                if (ha->wq)
1620                        flush_workqueue(ha->wq);
1621
1622                set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1623                if (ha->isp_ops->abort_isp(base_vha)) {
1624                        clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1625                        /* failed. schedule dpc to try */
1626                        set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
1627
1628                        if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1629                                ql_log(ql_log_warn, vha, 0x802a,
1630                                    "wait for hba online failed.\n");
1631                                goto eh_host_reset_lock;
1632                        }
1633                }
1634                clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1635        }
1636
1637        /* Waiting for command to be returned to OS.*/
1638        if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST) ==
1639                QLA_SUCCESS)
1640                ret = SUCCESS;
1641
1642eh_host_reset_lock:
1643        ql_log(ql_log_info, vha, 0x8017,
1644            "ADAPTER RESET %s nexus=%ld:%d:%llu.\n",
1645            (ret == FAILED) ? "FAILED" : "SUCCEEDED", vha->host_no, id, lun);
1646
1647        return ret;
1648}
1649
1650/*
1651* qla2x00_loop_reset
1652*      Issue loop reset.
1653*
1654* Input:
1655*      ha = adapter block pointer.
1656*
1657* Returns:
1658*      0 = success
1659*/
1660int
1661qla2x00_loop_reset(scsi_qla_host_t *vha)
1662{
1663        int ret;
1664        struct fc_port *fcport;
1665        struct qla_hw_data *ha = vha->hw;
1666
1667        if (IS_QLAFX00(ha)) {
1668                return qlafx00_loop_reset(vha);
1669        }
1670
1671        if (ql2xtargetreset == 1 && ha->flags.enable_target_reset) {
1672                list_for_each_entry(fcport, &vha->vp_fcports, list) {
1673                        if (fcport->port_type != FCT_TARGET)
1674                                continue;
1675
1676                        ret = ha->isp_ops->target_reset(fcport, 0, 0);
1677                        if (ret != QLA_SUCCESS) {
1678                                ql_dbg(ql_dbg_taskm, vha, 0x802c,
1679                                    "Bus Reset failed: Reset=%d "
1680                                    "d_id=%x.\n", ret, fcport->d_id.b24);
1681                        }
1682                }
1683        }
1684
1685
1686        if (ha->flags.enable_lip_full_login && !IS_CNA_CAPABLE(ha)) {
1687                atomic_set(&vha->loop_state, LOOP_DOWN);
1688                atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1689                qla2x00_mark_all_devices_lost(vha);
1690                ret = qla2x00_full_login_lip(vha);
1691                if (ret != QLA_SUCCESS) {
1692                        ql_dbg(ql_dbg_taskm, vha, 0x802d,
1693                            "full_login_lip=%d.\n", ret);
1694                }
1695        }
1696
1697        if (ha->flags.enable_lip_reset) {
1698                ret = qla2x00_lip_reset(vha);
1699                if (ret != QLA_SUCCESS)
1700                        ql_dbg(ql_dbg_taskm, vha, 0x802e,
1701                            "lip_reset failed (%d).\n", ret);
1702        }
1703
1704        /* Issue marker command only when we are going to start the I/O */
1705        vha->marker_needed = 1;
1706
1707        return QLA_SUCCESS;
1708}
1709
1710/*
1711 * The caller must ensure that no completion interrupts will happen
1712 * while this function is in progress.
1713 */
1714static void qla2x00_abort_srb(struct qla_qpair *qp, srb_t *sp, const int res,
1715                              unsigned long *flags)
1716        __releases(qp->qp_lock_ptr)
1717        __acquires(qp->qp_lock_ptr)
1718{
1719        DECLARE_COMPLETION_ONSTACK(comp);
1720        scsi_qla_host_t *vha = qp->vha;
1721        struct qla_hw_data *ha = vha->hw;
1722        struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1723        int rval;
1724        bool ret_cmd;
1725        uint32_t ratov_j;
1726
1727        if (qla2x00_chip_is_down(vha)) {
1728                sp->done(sp, res);
1729                return;
1730        }
1731
1732        if (sp->type == SRB_NVME_CMD || sp->type == SRB_NVME_LS ||
1733            (sp->type == SRB_SCSI_CMD && !ha->flags.eeh_busy &&
1734             !test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) &&
1735             !qla2x00_isp_reg_stat(ha))) {
1736                if (sp->comp) {
1737                        sp->done(sp, res);
1738                        return;
1739                }
1740
1741                sp->comp = &comp;
1742                spin_unlock_irqrestore(qp->qp_lock_ptr, *flags);
1743
1744                rval = ha->isp_ops->abort_command(sp);
1745                /* Wait for command completion. */
1746                ret_cmd = false;
1747                ratov_j = ha->r_a_tov/10 * 4 * 1000;
1748                ratov_j = msecs_to_jiffies(ratov_j);
1749                switch (rval) {
1750                case QLA_SUCCESS:
1751                        if (wait_for_completion_timeout(&comp, ratov_j)) {
1752                                ql_dbg(ql_dbg_taskm, vha, 0xffff,
1753                                    "%s: Abort wait timer (4 * R_A_TOV[%d]) expired\n",
1754                                    __func__, ha->r_a_tov/10);
1755                                ret_cmd = true;
1756                        }
1757                        /* else FW return SP to driver */
1758                        break;
1759                default:
1760                        ret_cmd = true;
1761                        break;
1762                }
1763
1764                spin_lock_irqsave(qp->qp_lock_ptr, *flags);
1765                if (ret_cmd && blk_mq_request_started(cmd->request))
1766                        sp->done(sp, res);
1767        } else {
1768                sp->done(sp, res);
1769        }
1770}
1771
1772/*
1773 * The caller must ensure that no completion interrupts will happen
1774 * while this function is in progress.
1775 */
1776static void
1777__qla2x00_abort_all_cmds(struct qla_qpair *qp, int res)
1778{
1779        int cnt;
1780        unsigned long flags;
1781        srb_t *sp;
1782        scsi_qla_host_t *vha = qp->vha;
1783        struct qla_hw_data *ha = vha->hw;
1784        struct req_que *req;
1785        struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1786        struct qla_tgt_cmd *cmd;
1787
1788        if (!ha->req_q_map)
1789                return;
1790        spin_lock_irqsave(qp->qp_lock_ptr, flags);
1791        req = qp->req;
1792        for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
1793                sp = req->outstanding_cmds[cnt];
1794                if (sp) {
1795                        switch (sp->cmd_type) {
1796                        case TYPE_SRB:
1797                                qla2x00_abort_srb(qp, sp, res, &flags);
1798                                break;
1799                        case TYPE_TGT_CMD:
1800                                if (!vha->hw->tgt.tgt_ops || !tgt ||
1801                                    qla_ini_mode_enabled(vha)) {
1802                                        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
1803                                            "HOST-ABORT-HNDLR: dpc_flags=%lx. Target mode disabled\n",
1804                                            vha->dpc_flags);
1805                                        continue;
1806                                }
1807                                cmd = (struct qla_tgt_cmd *)sp;
1808                                cmd->aborted = 1;
1809                                break;
1810                        case TYPE_TGT_TMCMD:
1811                                /* Skip task management functions. */
1812                                break;
1813                        default:
1814                                break;
1815                        }
1816                        req->outstanding_cmds[cnt] = NULL;
1817                }
1818        }
1819        spin_unlock_irqrestore(qp->qp_lock_ptr, flags);
1820}
1821
1822/*
1823 * The caller must ensure that no completion interrupts will happen
1824 * while this function is in progress.
1825 */
1826void
1827qla2x00_abort_all_cmds(scsi_qla_host_t *vha, int res)
1828{
1829        int que;
1830        struct qla_hw_data *ha = vha->hw;
1831
1832        /* Continue only if initialization complete. */
1833        if (!ha->base_qpair)
1834                return;
1835        __qla2x00_abort_all_cmds(ha->base_qpair, res);
1836
1837        if (!ha->queue_pair_map)
1838                return;
1839        for (que = 0; que < ha->max_qpairs; que++) {
1840                if (!ha->queue_pair_map[que])
1841                        continue;
1842
1843                __qla2x00_abort_all_cmds(ha->queue_pair_map[que], res);
1844        }
1845}
1846
1847static int
1848qla2xxx_slave_alloc(struct scsi_device *sdev)
1849{
1850        struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1851
1852        if (!rport || fc_remote_port_chkready(rport))
1853                return -ENXIO;
1854
1855        sdev->hostdata = *(fc_port_t **)rport->dd_data;
1856
1857        return 0;
1858}
1859
1860static int
1861qla2xxx_slave_configure(struct scsi_device *sdev)
1862{
1863        scsi_qla_host_t *vha = shost_priv(sdev->host);
1864        struct req_que *req = vha->req;
1865
1866        if (IS_T10_PI_CAPABLE(vha->hw))
1867                blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1868
1869        scsi_change_queue_depth(sdev, req->max_q_depth);
1870        return 0;
1871}
1872
1873static void
1874qla2xxx_slave_destroy(struct scsi_device *sdev)
1875{
1876        sdev->hostdata = NULL;
1877}
1878
1879/**
1880 * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1881 * @ha: HA context
1882 *
1883 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1884 * supported addressing method.
1885 */
1886static void
1887qla2x00_config_dma_addressing(struct qla_hw_data *ha)
1888{
1889        /* Assume a 32bit DMA mask. */
1890        ha->flags.enable_64bit_addressing = 0;
1891
1892        if (!dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(64))) {
1893                /* Any upper-dword bits set? */
1894                if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
1895                    !dma_set_coherent_mask(&ha->pdev->dev, DMA_BIT_MASK(64))) {
1896                        /* Ok, a 64bit DMA mask is applicable. */
1897                        ha->flags.enable_64bit_addressing = 1;
1898                        ha->isp_ops->calc_req_entries = qla2x00_calc_iocbs_64;
1899                        ha->isp_ops->build_iocbs = qla2x00_build_scsi_iocbs_64;
1900                        return;
1901                }
1902        }
1903
1904        dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(32));
1905        dma_set_coherent_mask(&ha->pdev->dev, DMA_BIT_MASK(32));
1906}
1907
1908static void
1909qla2x00_enable_intrs(struct qla_hw_data *ha)
1910{
1911        unsigned long flags = 0;
1912        struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1913
1914        spin_lock_irqsave(&ha->hardware_lock, flags);
1915        ha->interrupts_on = 1;
1916        /* enable risc and host interrupts */
1917        wrt_reg_word(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1918        rd_reg_word(&reg->ictrl);
1919        spin_unlock_irqrestore(&ha->hardware_lock, flags);
1920
1921}
1922
1923static void
1924qla2x00_disable_intrs(struct qla_hw_data *ha)
1925{
1926        unsigned long flags = 0;
1927        struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1928
1929        spin_lock_irqsave(&ha->hardware_lock, flags);
1930        ha->interrupts_on = 0;
1931        /* disable risc and host interrupts */
1932        wrt_reg_word(&reg->ictrl, 0);
1933        rd_reg_word(&reg->ictrl);
1934        spin_unlock_irqrestore(&ha->hardware_lock, flags);
1935}
1936
1937static void
1938qla24xx_enable_intrs(struct qla_hw_data *ha)
1939{
1940        unsigned long flags = 0;
1941        struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1942
1943        spin_lock_irqsave(&ha->hardware_lock, flags);
1944        ha->interrupts_on = 1;
1945        wrt_reg_dword(&reg->ictrl, ICRX_EN_RISC_INT);
1946        rd_reg_dword(&reg->ictrl);
1947        spin_unlock_irqrestore(&ha->hardware_lock, flags);
1948}
1949
1950static void
1951qla24xx_disable_intrs(struct qla_hw_data *ha)
1952{
1953        unsigned long flags = 0;
1954        struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1955
1956        if (IS_NOPOLLING_TYPE(ha))
1957                return;
1958        spin_lock_irqsave(&ha->hardware_lock, flags);
1959        ha->interrupts_on = 0;
1960        wrt_reg_dword(&reg->ictrl, 0);
1961        rd_reg_dword(&reg->ictrl);
1962        spin_unlock_irqrestore(&ha->hardware_lock, flags);
1963}
1964
1965static int
1966qla2x00_iospace_config(struct qla_hw_data *ha)
1967{
1968        resource_size_t pio;
1969        uint16_t msix;
1970
1971        if (pci_request_selected_regions(ha->pdev, ha->bars,
1972            QLA2XXX_DRIVER_NAME)) {
1973                ql_log_pci(ql_log_fatal, ha->pdev, 0x0011,
1974                    "Failed to reserve PIO/MMIO regions (%s), aborting.\n",
1975                    pci_name(ha->pdev));
1976                goto iospace_error_exit;
1977        }
1978        if (!(ha->bars & 1))
1979                goto skip_pio;
1980
1981        /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1982        pio = pci_resource_start(ha->pdev, 0);
1983        if (pci_resource_flags(ha->pdev, 0) & IORESOURCE_IO) {
1984                if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
1985                        ql_log_pci(ql_log_warn, ha->pdev, 0x0012,
1986                            "Invalid pci I/O region size (%s).\n",
1987                            pci_name(ha->pdev));
1988                        pio = 0;
1989                }
1990        } else {
1991                ql_log_pci(ql_log_warn, ha->pdev, 0x0013,
1992                    "Region #0 no a PIO resource (%s).\n",
1993                    pci_name(ha->pdev));
1994                pio = 0;
1995        }
1996        ha->pio_address = pio;
1997        ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0014,
1998            "PIO address=%llu.\n",
1999            (unsigned long long)ha->pio_address);
2000
2001skip_pio:
2002        /* Use MMIO operations for all accesses. */
2003        if (!(pci_resource_flags(ha->pdev, 1) & IORESOURCE_MEM)) {
2004                ql_log_pci(ql_log_fatal, ha->pdev, 0x0015,
2005                    "Region #1 not an MMIO resource (%s), aborting.\n",
2006                    pci_name(ha->pdev));
2007                goto iospace_error_exit;
2008        }
2009        if (pci_resource_len(ha->pdev, 1) < MIN_IOBASE_LEN) {
2010                ql_log_pci(ql_log_fatal, ha->pdev, 0x0016,
2011                    "Invalid PCI mem region size (%s), aborting.\n",
2012                    pci_name(ha->pdev));
2013                goto iospace_error_exit;
2014        }
2015
2016        ha->iobase = ioremap(pci_resource_start(ha->pdev, 1), MIN_IOBASE_LEN);
2017        if (!ha->iobase) {
2018                ql_log_pci(ql_log_fatal, ha->pdev, 0x0017,
2019                    "Cannot remap MMIO (%s), aborting.\n",
2020                    pci_name(ha->pdev));
2021                goto iospace_error_exit;
2022        }
2023
2024        /* Determine queue resources */
2025        ha->max_req_queues = ha->max_rsp_queues = 1;
2026        ha->msix_count = QLA_BASE_VECTORS;
2027
2028        /* Check if FW supports MQ or not */
2029        if (!(ha->fw_attributes & BIT_6))
2030                goto mqiobase_exit;
2031
2032        if (!ql2xmqsupport || !ql2xnvmeenable ||
2033            (!IS_QLA25XX(ha) && !IS_QLA81XX(ha)))
2034                goto mqiobase_exit;
2035
2036        ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 3),
2037                        pci_resource_len(ha->pdev, 3));
2038        if (ha->mqiobase) {
2039                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0018,
2040                    "MQIO Base=%p.\n", ha->mqiobase);
2041                /* Read MSIX vector size of the board */
2042                pci_read_config_word(ha->pdev, QLA_PCI_MSIX_CONTROL, &msix);
2043                ha->msix_count = msix + 1;
2044                /* Max queues are bounded by available msix vectors */
2045                /* MB interrupt uses 1 vector */
2046                ha->max_req_queues = ha->msix_count - 1;
2047                ha->max_rsp_queues = ha->max_req_queues;
2048                /* Queue pairs is the max value minus the base queue pair */
2049                ha->max_qpairs = ha->max_rsp_queues - 1;
2050                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0188,
2051                    "Max no of queues pairs: %d.\n", ha->max_qpairs);
2052
2053                ql_log_pci(ql_log_info, ha->pdev, 0x001a,
2054                    "MSI-X vector count: %d.\n", ha->msix_count);
2055        } else
2056                ql_log_pci(ql_log_info, ha->pdev, 0x001b,
2057                    "BAR 3 not enabled.\n");
2058
2059mqiobase_exit:
2060        ql_dbg_pci(ql_dbg_init, ha->pdev, 0x001c,
2061            "MSIX Count: %d.\n", ha->msix_count);
2062        return (0);
2063
2064iospace_error_exit:
2065        return (-ENOMEM);
2066}
2067
2068
2069static int
2070qla83xx_iospace_config(struct qla_hw_data *ha)
2071{
2072        uint16_t msix;
2073
2074        if (pci_request_selected_regions(ha->pdev, ha->bars,
2075            QLA2XXX_DRIVER_NAME)) {
2076                ql_log_pci(ql_log_fatal, ha->pdev, 0x0117,
2077                    "Failed to reserve PIO/MMIO regions (%s), aborting.\n",
2078                    pci_name(ha->pdev));
2079
2080                goto iospace_error_exit;
2081        }
2082
2083        /* Use MMIO operations for all accesses. */
2084        if (!(pci_resource_flags(ha->pdev, 0) & IORESOURCE_MEM)) {
2085                ql_log_pci(ql_log_warn, ha->pdev, 0x0118,
2086                    "Invalid pci I/O region size (%s).\n",
2087                    pci_name(ha->pdev));
2088                goto iospace_error_exit;
2089        }
2090        if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
2091                ql_log_pci(ql_log_warn, ha->pdev, 0x0119,
2092                    "Invalid PCI mem region size (%s), aborting\n",
2093                        pci_name(ha->pdev));
2094                goto iospace_error_exit;
2095        }
2096
2097        ha->iobase = ioremap(pci_resource_start(ha->pdev, 0), MIN_IOBASE_LEN);
2098        if (!ha->iobase) {
2099                ql_log_pci(ql_log_fatal, ha->pdev, 0x011a,
2100                    "Cannot remap MMIO (%s), aborting.\n",
2101                    pci_name(ha->pdev));
2102                goto iospace_error_exit;
2103        }
2104
2105        /* 64bit PCI BAR - BAR2 will correspoond to region 4 */
2106        /* 83XX 26XX always use MQ type access for queues
2107         * - mbar 2, a.k.a region 4 */
2108        ha->max_req_queues = ha->max_rsp_queues = 1;
2109        ha->msix_count = QLA_BASE_VECTORS;
2110        ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 4),
2111                        pci_resource_len(ha->pdev, 4));
2112
2113        if (!ha->mqiobase) {
2114                ql_log_pci(ql_log_fatal, ha->pdev, 0x011d,
2115                    "BAR2/region4 not enabled\n");
2116                goto mqiobase_exit;
2117        }
2118
2119        ha->msixbase = ioremap(pci_resource_start(ha->pdev, 2),
2120                        pci_resource_len(ha->pdev, 2));
2121        if (ha->msixbase) {
2122                /* Read MSIX vector size of the board */
2123                pci_read_config_word(ha->pdev,
2124                    QLA_83XX_PCI_MSIX_CONTROL, &msix);
2125                ha->msix_count = (msix & PCI_MSIX_FLAGS_QSIZE)  + 1;
2126                /*
2127                 * By default, driver uses at least two msix vectors
2128                 * (default & rspq)
2129                 */
2130                if (ql2xmqsupport || ql2xnvmeenable) {
2131                        /* MB interrupt uses 1 vector */
2132                        ha->max_req_queues = ha->msix_count - 1;
2133
2134                        /* ATIOQ needs 1 vector. That's 1 less QPair */
2135                        if (QLA_TGT_MODE_ENABLED())
2136                                ha->max_req_queues--;
2137
2138                        ha->max_rsp_queues = ha->max_req_queues;
2139
2140                        /* Queue pairs is the max value minus
2141                         * the base queue pair */
2142                        ha->max_qpairs = ha->max_req_queues - 1;
2143                        ql_dbg_pci(ql_dbg_init, ha->pdev, 0x00e3,
2144                            "Max no of queues pairs: %d.\n", ha->max_qpairs);
2145                }
2146                ql_log_pci(ql_log_info, ha->pdev, 0x011c,
2147                    "MSI-X vector count: %d.\n", ha->msix_count);
2148        } else
2149                ql_log_pci(ql_log_info, ha->pdev, 0x011e,
2150                    "BAR 1 not enabled.\n");
2151
2152mqiobase_exit:
2153        ql_dbg_pci(ql_dbg_init, ha->pdev, 0x011f,
2154            "MSIX Count: %d.\n", ha->msix_count);
2155        return 0;
2156
2157iospace_error_exit:
2158        return -ENOMEM;
2159}
2160
2161static struct isp_operations qla2100_isp_ops = {
2162        .pci_config             = qla2100_pci_config,
2163        .reset_chip             = qla2x00_reset_chip,
2164        .chip_diag              = qla2x00_chip_diag,
2165        .config_rings           = qla2x00_config_rings,
2166        .reset_adapter          = qla2x00_reset_adapter,
2167        .nvram_config           = qla2x00_nvram_config,
2168        .update_fw_options      = qla2x00_update_fw_options,
2169        .load_risc              = qla2x00_load_risc,
2170        .pci_info_str           = qla2x00_pci_info_str,
2171        .fw_version_str         = qla2x00_fw_version_str,
2172        .intr_handler           = qla2100_intr_handler,
2173        .enable_intrs           = qla2x00_enable_intrs,
2174        .disable_intrs          = qla2x00_disable_intrs,
2175        .abort_command          = qla2x00_abort_command,
2176        .target_reset           = qla2x00_abort_target,
2177        .lun_reset              = qla2x00_lun_reset,
2178        .fabric_login           = qla2x00_login_fabric,
2179        .fabric_logout          = qla2x00_fabric_logout,
2180        .calc_req_entries       = qla2x00_calc_iocbs_32,
2181        .build_iocbs            = qla2x00_build_scsi_iocbs_32,
2182        .prep_ms_iocb           = qla2x00_prep_ms_iocb,
2183        .prep_ms_fdmi_iocb      = qla2x00_prep_ms_fdmi_iocb,
2184        .read_nvram             = qla2x00_read_nvram_data,
2185        .write_nvram            = qla2x00_write_nvram_data,
2186        .fw_dump                = qla2100_fw_dump,
2187        .beacon_on              = NULL,
2188        .beacon_off             = NULL,
2189        .beacon_blink           = NULL,
2190        .read_optrom            = qla2x00_read_optrom_data,
2191        .write_optrom           = qla2x00_write_optrom_data,
2192        .get_flash_version      = qla2x00_get_flash_version,
2193        .start_scsi             = qla2x00_start_scsi,
2194        .start_scsi_mq          = NULL,
2195        .abort_isp              = qla2x00_abort_isp,
2196        .iospace_config         = qla2x00_iospace_config,
2197        .initialize_adapter     = qla2x00_initialize_adapter,
2198};
2199
2200static struct isp_operations qla2300_isp_ops = {
2201        .pci_config             = qla2300_pci_config,
2202        .reset_chip             = qla2x00_reset_chip,
2203        .chip_diag              = qla2x00_chip_diag,
2204        .config_rings           = qla2x00_config_rings,
2205        .reset_adapter          = qla2x00_reset_adapter,
2206        .nvram_config           = qla2x00_nvram_config,
2207        .update_fw_options      = qla2x00_update_fw_options,
2208        .load_risc              = qla2x00_load_risc,
2209        .pci_info_str           = qla2x00_pci_info_str,
2210        .fw_version_str         = qla2x00_fw_version_str,
2211        .intr_handler           = qla2300_intr_handler,
2212        .enable_intrs           = qla2x00_enable_intrs,
2213        .disable_intrs          = qla2x00_disable_intrs,
2214        .abort_command          = qla2x00_abort_command,
2215        .target_reset           = qla2x00_abort_target,
2216        .lun_reset              = qla2x00_lun_reset,
2217        .fabric_login           = qla2x00_login_fabric,
2218        .fabric_logout          = qla2x00_fabric_logout,
2219        .calc_req_entries       = qla2x00_calc_iocbs_32,
2220        .build_iocbs            = qla2x00_build_scsi_iocbs_32,
2221        .prep_ms_iocb           = qla2x00_prep_ms_iocb,
2222        .prep_ms_fdmi_iocb      = qla2x00_prep_ms_fdmi_iocb,
2223        .read_nvram             = qla2x00_read_nvram_data,
2224        .write_nvram            = qla2x00_write_nvram_data,
2225        .fw_dump                = qla2300_fw_dump,
2226        .beacon_on              = qla2x00_beacon_on,
2227        .beacon_off             = qla2x00_beacon_off,
2228        .beacon_blink           = qla2x00_beacon_blink,
2229        .read_optrom            = qla2x00_read_optrom_data,
2230        .write_optrom           = qla2x00_write_optrom_data,
2231        .get_flash_version      = qla2x00_get_flash_version,
2232        .start_scsi             = qla2x00_start_scsi,
2233        .start_scsi_mq          = NULL,
2234        .abort_isp              = qla2x00_abort_isp,
2235        .iospace_config         = qla2x00_iospace_config,
2236        .initialize_adapter     = qla2x00_initialize_adapter,
2237};
2238
2239static struct isp_operations qla24xx_isp_ops = {
2240        .pci_config             = qla24xx_pci_config,
2241        .reset_chip             = qla24xx_reset_chip,
2242        .chip_diag              = qla24xx_chip_diag,
2243        .config_rings           = qla24xx_config_rings,
2244        .reset_adapter          = qla24xx_reset_adapter,
2245        .nvram_config           = qla24xx_nvram_config,
2246        .update_fw_options      = qla24xx_update_fw_options,
2247        .load_risc              = qla24xx_load_risc,
2248        .pci_info_str           = qla24xx_pci_info_str,
2249        .fw_version_str         = qla24xx_fw_version_str,
2250        .intr_handler           = qla24xx_intr_handler,
2251        .enable_intrs           = qla24xx_enable_intrs,
2252        .disable_intrs          = qla24xx_disable_intrs,
2253        .abort_command          = qla24xx_abort_command,
2254        .target_reset           = qla24xx_abort_target,
2255        .lun_reset              = qla24xx_lun_reset,
2256        .fabric_login           = qla24xx_login_fabric,
2257        .fabric_logout          = qla24xx_fabric_logout,
2258        .calc_req_entries       = NULL,
2259        .build_iocbs            = NULL,
2260        .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2261        .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2262        .read_nvram             = qla24xx_read_nvram_data,
2263        .write_nvram            = qla24xx_write_nvram_data,
2264        .fw_dump                = qla24xx_fw_dump,
2265        .beacon_on              = qla24xx_beacon_on,
2266        .beacon_off             = qla24xx_beacon_off,
2267        .beacon_blink           = qla24xx_beacon_blink,
2268        .read_optrom            = qla24xx_read_optrom_data,
2269        .write_optrom           = qla24xx_write_optrom_data,
2270        .get_flash_version      = qla24xx_get_flash_version,
2271        .start_scsi             = qla24xx_start_scsi,
2272        .start_scsi_mq          = NULL,
2273        .abort_isp              = qla2x00_abort_isp,
2274        .iospace_config         = qla2x00_iospace_config,
2275        .initialize_adapter     = qla2x00_initialize_adapter,
2276};
2277
2278static struct isp_operations qla25xx_isp_ops = {
2279        .pci_config             = qla25xx_pci_config,
2280        .reset_chip             = qla24xx_reset_chip,
2281        .chip_diag              = qla24xx_chip_diag,
2282        .config_rings           = qla24xx_config_rings,
2283        .reset_adapter          = qla24xx_reset_adapter,
2284        .nvram_config           = qla24xx_nvram_config,
2285        .update_fw_options      = qla24xx_update_fw_options,
2286        .load_risc              = qla24xx_load_risc,
2287        .pci_info_str           = qla24xx_pci_info_str,
2288        .fw_version_str         = qla24xx_fw_version_str,
2289        .intr_handler           = qla24xx_intr_handler,
2290        .enable_intrs           = qla24xx_enable_intrs,
2291        .disable_intrs          = qla24xx_disable_intrs,
2292        .abort_command          = qla24xx_abort_command,
2293        .target_reset           = qla24xx_abort_target,
2294        .lun_reset              = qla24xx_lun_reset,
2295        .fabric_login           = qla24xx_login_fabric,
2296        .fabric_logout          = qla24xx_fabric_logout,
2297        .calc_req_entries       = NULL,
2298        .build_iocbs            = NULL,
2299        .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2300        .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2301        .read_nvram             = qla25xx_read_nvram_data,
2302        .write_nvram            = qla25xx_write_nvram_data,
2303        .fw_dump                = qla25xx_fw_dump,
2304        .beacon_on              = qla24xx_beacon_on,
2305        .beacon_off             = qla24xx_beacon_off,
2306        .beacon_blink           = qla24xx_beacon_blink,
2307        .read_optrom            = qla25xx_read_optrom_data,
2308        .write_optrom           = qla24xx_write_optrom_data,
2309        .get_flash_version      = qla24xx_get_flash_version,
2310        .start_scsi             = qla24xx_dif_start_scsi,
2311        .start_scsi_mq          = qla2xxx_dif_start_scsi_mq,
2312        .abort_isp              = qla2x00_abort_isp,
2313        .iospace_config         = qla2x00_iospace_config,
2314        .initialize_adapter     = qla2x00_initialize_adapter,
2315};
2316
2317static struct isp_operations qla81xx_isp_ops = {
2318        .pci_config             = qla25xx_pci_config,
2319        .reset_chip             = qla24xx_reset_chip,
2320        .chip_diag              = qla24xx_chip_diag,
2321        .config_rings           = qla24xx_config_rings,
2322        .reset_adapter          = qla24xx_reset_adapter,
2323        .nvram_config           = qla81xx_nvram_config,
2324        .update_fw_options      = qla24xx_update_fw_options,
2325        .load_risc              = qla81xx_load_risc,
2326        .pci_info_str           = qla24xx_pci_info_str,
2327        .fw_version_str         = qla24xx_fw_version_str,
2328        .intr_handler           = qla24xx_intr_handler,
2329        .enable_intrs           = qla24xx_enable_intrs,
2330        .disable_intrs          = qla24xx_disable_intrs,
2331        .abort_command          = qla24xx_abort_command,
2332        .target_reset           = qla24xx_abort_target,
2333        .lun_reset              = qla24xx_lun_reset,
2334        .fabric_login           = qla24xx_login_fabric,
2335        .fabric_logout          = qla24xx_fabric_logout,
2336        .calc_req_entries       = NULL,
2337        .build_iocbs            = NULL,
2338        .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2339        .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2340        .read_nvram             = NULL,
2341        .write_nvram            = NULL,
2342        .fw_dump                = qla81xx_fw_dump,
2343        .beacon_on              = qla24xx_beacon_on,
2344        .beacon_off             = qla24xx_beacon_off,
2345        .beacon_blink           = qla83xx_beacon_blink,
2346        .read_optrom            = qla25xx_read_optrom_data,
2347        .write_optrom           = qla24xx_write_optrom_data,
2348        .get_flash_version      = qla24xx_get_flash_version,
2349        .start_scsi             = qla24xx_dif_start_scsi,
2350        .start_scsi_mq          = qla2xxx_dif_start_scsi_mq,
2351        .abort_isp              = qla2x00_abort_isp,
2352        .iospace_config         = qla2x00_iospace_config,
2353        .initialize_adapter     = qla2x00_initialize_adapter,
2354};
2355
2356static struct isp_operations qla82xx_isp_ops = {
2357        .pci_config             = qla82xx_pci_config,
2358        .reset_chip             = qla82xx_reset_chip,
2359        .chip_diag              = qla24xx_chip_diag,
2360        .config_rings           = qla82xx_config_rings,
2361        .reset_adapter          = qla24xx_reset_adapter,
2362        .nvram_config           = qla81xx_nvram_config,
2363        .update_fw_options      = qla24xx_update_fw_options,
2364        .load_risc              = qla82xx_load_risc,
2365        .pci_info_str           = qla24xx_pci_info_str,
2366        .fw_version_str         = qla24xx_fw_version_str,
2367        .intr_handler           = qla82xx_intr_handler,
2368        .enable_intrs           = qla82xx_enable_intrs,
2369        .disable_intrs          = qla82xx_disable_intrs,
2370        .abort_command          = qla24xx_abort_command,
2371        .target_reset           = qla24xx_abort_target,
2372        .lun_reset              = qla24xx_lun_reset,
2373        .fabric_login           = qla24xx_login_fabric,
2374        .fabric_logout          = qla24xx_fabric_logout,
2375        .calc_req_entries       = NULL,
2376        .build_iocbs            = NULL,
2377        .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2378        .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2379        .read_nvram             = qla24xx_read_nvram_data,
2380        .write_nvram            = qla24xx_write_nvram_data,
2381        .fw_dump                = qla82xx_fw_dump,
2382        .beacon_on              = qla82xx_beacon_on,
2383        .beacon_off             = qla82xx_beacon_off,
2384        .beacon_blink           = NULL,
2385        .read_optrom            = qla82xx_read_optrom_data,
2386        .write_optrom           = qla82xx_write_optrom_data,
2387        .get_flash_version      = qla82xx_get_flash_version,
2388        .start_scsi             = qla82xx_start_scsi,
2389        .start_scsi_mq          = NULL,
2390        .abort_isp              = qla82xx_abort_isp,
2391        .iospace_config         = qla82xx_iospace_config,
2392        .initialize_adapter     = qla2x00_initialize_adapter,
2393};
2394
2395static struct isp_operations qla8044_isp_ops = {
2396        .pci_config             = qla82xx_pci_config,
2397        .reset_chip             = qla82xx_reset_chip,
2398        .chip_diag              = qla24xx_chip_diag,
2399        .config_rings           = qla82xx_config_rings,
2400        .reset_adapter          = qla24xx_reset_adapter,
2401        .nvram_config           = qla81xx_nvram_config,
2402        .update_fw_options      = qla24xx_update_fw_options,
2403        .load_risc              = qla82xx_load_risc,
2404        .pci_info_str           = qla24xx_pci_info_str,
2405        .fw_version_str         = qla24xx_fw_version_str,
2406        .intr_handler           = qla8044_intr_handler,
2407        .enable_intrs           = qla82xx_enable_intrs,
2408        .disable_intrs          = qla82xx_disable_intrs,
2409        .abort_command          = qla24xx_abort_command,
2410        .target_reset           = qla24xx_abort_target,
2411        .lun_reset              = qla24xx_lun_reset,
2412        .fabric_login           = qla24xx_login_fabric,
2413        .fabric_logout          = qla24xx_fabric_logout,
2414        .calc_req_entries       = NULL,
2415        .build_iocbs            = NULL,
2416        .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2417        .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2418        .read_nvram             = NULL,
2419        .write_nvram            = NULL,
2420        .fw_dump                = qla8044_fw_dump,
2421        .beacon_on              = qla82xx_beacon_on,
2422        .beacon_off             = qla82xx_beacon_off,
2423        .beacon_blink           = NULL,
2424        .read_optrom            = qla8044_read_optrom_data,
2425        .write_optrom           = qla8044_write_optrom_data,
2426        .get_flash_version      = qla82xx_get_flash_version,
2427        .start_scsi             = qla82xx_start_scsi,
2428        .start_scsi_mq          = NULL,
2429        .abort_isp              = qla8044_abort_isp,
2430        .iospace_config         = qla82xx_iospace_config,
2431        .initialize_adapter     = qla2x00_initialize_adapter,
2432};
2433
2434static struct isp_operations qla83xx_isp_ops = {
2435        .pci_config             = qla25xx_pci_config,
2436        .reset_chip             = qla24xx_reset_chip,
2437        .chip_diag              = qla24xx_chip_diag,
2438        .config_rings           = qla24xx_config_rings,
2439        .reset_adapter          = qla24xx_reset_adapter,
2440        .nvram_config           = qla81xx_nvram_config,
2441        .update_fw_options      = qla24xx_update_fw_options,
2442        .load_risc              = qla81xx_load_risc,
2443        .pci_info_str           = qla24xx_pci_info_str,
2444        .fw_version_str         = qla24xx_fw_version_str,
2445        .intr_handler           = qla24xx_intr_handler,
2446        .enable_intrs           = qla24xx_enable_intrs,
2447        .disable_intrs          = qla24xx_disable_intrs,
2448        .abort_command          = qla24xx_abort_command,
2449        .target_reset           = qla24xx_abort_target,
2450        .lun_reset              = qla24xx_lun_reset,
2451        .fabric_login           = qla24xx_login_fabric,
2452        .fabric_logout          = qla24xx_fabric_logout,
2453        .calc_req_entries       = NULL,
2454        .build_iocbs            = NULL,
2455        .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2456        .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2457        .read_nvram             = NULL,
2458        .write_nvram            = NULL,
2459        .fw_dump                = qla83xx_fw_dump,
2460        .beacon_on              = qla24xx_beacon_on,
2461        .beacon_off             = qla24xx_beacon_off,
2462        .beacon_blink           = qla83xx_beacon_blink,
2463        .read_optrom            = qla25xx_read_optrom_data,
2464        .write_optrom           = qla24xx_write_optrom_data,
2465        .get_flash_version      = qla24xx_get_flash_version,
2466        .start_scsi             = qla24xx_dif_start_scsi,
2467        .start_scsi_mq          = qla2xxx_dif_start_scsi_mq,
2468        .abort_isp              = qla2x00_abort_isp,
2469        .iospace_config         = qla83xx_iospace_config,
2470        .initialize_adapter     = qla2x00_initialize_adapter,
2471};
2472
2473static struct isp_operations qlafx00_isp_ops = {
2474        .pci_config             = qlafx00_pci_config,
2475        .reset_chip             = qlafx00_soft_reset,
2476        .chip_diag              = qlafx00_chip_diag,
2477        .config_rings           = qlafx00_config_rings,
2478        .reset_adapter          = qlafx00_soft_reset,
2479        .nvram_config           = NULL,
2480        .update_fw_options      = NULL,
2481        .load_risc              = NULL,
2482        .pci_info_str           = qlafx00_pci_info_str,
2483        .fw_version_str         = qlafx00_fw_version_str,
2484        .intr_handler           = qlafx00_intr_handler,
2485        .enable_intrs           = qlafx00_enable_intrs,
2486        .disable_intrs          = qlafx00_disable_intrs,
2487        .abort_command          = qla24xx_async_abort_command,
2488        .target_reset           = qlafx00_abort_target,
2489        .lun_reset              = qlafx00_lun_reset,
2490        .fabric_login           = NULL,
2491        .fabric_logout          = NULL,
2492        .calc_req_entries       = NULL,
2493        .build_iocbs            = NULL,
2494        .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2495        .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2496        .read_nvram             = qla24xx_read_nvram_data,
2497        .write_nvram            = qla24xx_write_nvram_data,
2498        .fw_dump                = NULL,
2499        .beacon_on              = qla24xx_beacon_on,
2500        .beacon_off             = qla24xx_beacon_off,
2501        .beacon_blink           = NULL,
2502        .read_optrom            = qla24xx_read_optrom_data,
2503        .write_optrom           = qla24xx_write_optrom_data,
2504        .get_flash_version      = qla24xx_get_flash_version,
2505        .start_scsi             = qlafx00_start_scsi,
2506        .start_scsi_mq          = NULL,
2507        .abort_isp              = qlafx00_abort_isp,
2508        .iospace_config         = qlafx00_iospace_config,
2509        .initialize_adapter     = qlafx00_initialize_adapter,
2510};
2511
2512static struct isp_operations qla27xx_isp_ops = {
2513        .pci_config             = qla25xx_pci_config,
2514        .reset_chip             = qla24xx_reset_chip,
2515        .chip_diag              = qla24xx_chip_diag,
2516        .config_rings           = qla24xx_config_rings,
2517        .reset_adapter          = qla24xx_reset_adapter,
2518        .nvram_config           = qla81xx_nvram_config,
2519        .update_fw_options      = qla24xx_update_fw_options,
2520        .load_risc              = qla81xx_load_risc,
2521        .pci_info_str           = qla24xx_pci_info_str,
2522        .fw_version_str         = qla24xx_fw_version_str,
2523        .intr_handler           = qla24xx_intr_handler,
2524        .enable_intrs           = qla24xx_enable_intrs,
2525        .disable_intrs          = qla24xx_disable_intrs,
2526        .abort_command          = qla24xx_abort_command,
2527        .target_reset           = qla24xx_abort_target,
2528        .lun_reset              = qla24xx_lun_reset,
2529        .fabric_login           = qla24xx_login_fabric,
2530        .fabric_logout          = qla24xx_fabric_logout,
2531        .calc_req_entries       = NULL,
2532        .build_iocbs            = NULL,
2533        .prep_ms_iocb           = qla24xx_prep_ms_iocb,
2534        .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
2535        .read_nvram             = NULL,
2536        .write_nvram            = NULL,
2537        .fw_dump                = qla27xx_fwdump,
2538        .mpi_fw_dump            = qla27xx_mpi_fwdump,
2539        .beacon_on              = qla24xx_beacon_on,
2540        .beacon_off             = qla24xx_beacon_off,
2541        .beacon_blink           = qla83xx_beacon_blink,
2542        .read_optrom            = qla25xx_read_optrom_data,
2543        .write_optrom           = qla24xx_write_optrom_data,
2544        .get_flash_version      = qla24xx_get_flash_version,
2545        .start_scsi             = qla24xx_dif_start_scsi,
2546        .start_scsi_mq          = qla2xxx_dif_start_scsi_mq,
2547        .abort_isp              = qla2x00_abort_isp,
2548        .iospace_config         = qla83xx_iospace_config,
2549        .initialize_adapter     = qla2x00_initialize_adapter,
2550};
2551
2552static inline void
2553qla2x00_set_isp_flags(struct qla_hw_data *ha)
2554{
2555        ha->device_type = DT_EXTENDED_IDS;
2556        switch (ha->pdev->device) {
2557        case PCI_DEVICE_ID_QLOGIC_ISP2532:
2558                ha->isp_type |= DT_ISP2532;
2559                ha->device_type |= DT_ZIO_SUPPORTED;
2560                ha->device_type |= DT_FWI2;
2561                ha->device_type |= DT_IIDMA;
2562                ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2563                break;
2564        case PCI_DEVICE_ID_QLOGIC_ISP2031:
2565                ha->isp_type |= DT_ISP2031;
2566                ha->device_type |= DT_ZIO_SUPPORTED;
2567                ha->device_type |= DT_FWI2;
2568                ha->device_type |= DT_IIDMA;
2569                ha->device_type |= DT_T10_PI;
2570                ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2571                break;
2572        case PCI_DEVICE_ID_QLOGIC_ISP8031:
2573                ha->isp_type |= DT_ISP8031;
2574                ha->device_type |= DT_ZIO_SUPPORTED;
2575                ha->device_type |= DT_FWI2;
2576                ha->device_type |= DT_IIDMA;
2577                ha->device_type |= DT_T10_PI;
2578                ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2579                break;
2580        case PCI_DEVICE_ID_QLOGIC_ISP2071:
2581                ha->isp_type |= DT_ISP2071;
2582                ha->device_type |= DT_ZIO_SUPPORTED;
2583                ha->device_type |= DT_FWI2;
2584                ha->device_type |= DT_IIDMA;
2585                ha->device_type |= DT_T10_PI;
2586                ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2587                break;
2588        case PCI_DEVICE_ID_QLOGIC_ISP2271:
2589                ha->isp_type |= DT_ISP2271;
2590                ha->device_type |= DT_ZIO_SUPPORTED;
2591                ha->device_type |= DT_FWI2;
2592                ha->device_type |= DT_IIDMA;
2593                ha->device_type |= DT_T10_PI;
2594                ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2595                break;
2596        case PCI_DEVICE_ID_QLOGIC_ISP2261:
2597                ha->isp_type |= DT_ISP2261;
2598                ha->device_type |= DT_ZIO_SUPPORTED;
2599                ha->device_type |= DT_FWI2;
2600                ha->device_type |= DT_IIDMA;
2601                ha->device_type |= DT_T10_PI;
2602                ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2603                break;
2604        case PCI_DEVICE_ID_QLOGIC_ISP2081:
2605        case PCI_DEVICE_ID_QLOGIC_ISP2089:
2606                ha->isp_type |= DT_ISP2081;
2607                ha->device_type |= DT_ZIO_SUPPORTED;
2608                ha->device_type |= DT_FWI2;
2609                ha->device_type |= DT_IIDMA;
2610                ha->device_type |= DT_T10_PI;
2611                ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2612                break;
2613        case PCI_DEVICE_ID_QLOGIC_ISP2281:
2614        case PCI_DEVICE_ID_QLOGIC_ISP2289:
2615                ha->isp_type |= DT_ISP2281;
2616                ha->device_type |= DT_ZIO_SUPPORTED;
2617                ha->device_type |= DT_FWI2;
2618                ha->device_type |= DT_IIDMA;
2619                ha->device_type |= DT_T10_PI;
2620                ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2621                break;
2622        }
2623
2624        if (IS_QLA82XX(ha))
2625                ha->port_no = ha->portnum & 1;
2626        else {
2627                /* Get adapter physical port no from interrupt pin register. */
2628                pci_read_config_byte(ha->pdev, PCI_INTERRUPT_PIN, &ha->port_no);
2629                if (IS_QLA25XX(ha) || IS_QLA2031(ha) ||
2630                    IS_QLA27XX(ha) || IS_QLA28XX(ha))
2631                        ha->port_no--;
2632                else
2633                        ha->port_no = !(ha->port_no & 1);
2634        }
2635
2636        ql_dbg_pci(ql_dbg_init, ha->pdev, 0x000b,
2637            "device_type=0x%x port=%d fw_srisc_address=0x%x.\n",
2638            ha->device_type, ha->port_no, ha->fw_srisc_address);
2639}
2640
2641static void
2642qla2xxx_scan_start(struct Scsi_Host *shost)
2643{
2644        scsi_qla_host_t *vha = shost_priv(shost);
2645
2646        if (vha->hw->flags.running_gold_fw)
2647                return;
2648
2649        set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
2650        set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
2651        set_bit(RSCN_UPDATE, &vha->dpc_flags);
2652        set_bit(NPIV_CONFIG_NEEDED, &vha->dpc_flags);
2653}
2654
2655static int
2656qla2xxx_scan_finished(struct Scsi_Host *shost, unsigned long time)
2657{
2658        scsi_qla_host_t *vha = shost_priv(shost);
2659
2660        if (test_bit(UNLOADING, &vha->dpc_flags))
2661                return 1;
2662        if (!vha->host)
2663                return 1;
2664        if (time > vha->hw->loop_reset_delay * HZ)
2665                return 1;
2666
2667        return atomic_read(&vha->loop_state) == LOOP_READY;
2668}
2669
2670static void qla2x00_iocb_work_fn(struct work_struct *work)
2671{
2672        struct scsi_qla_host *vha = container_of(work,
2673                struct scsi_qla_host, iocb_work);
2674        struct qla_hw_data *ha = vha->hw;
2675        struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2676        int i = 2;
2677        unsigned long flags;
2678
2679        if (test_bit(UNLOADING, &base_vha->dpc_flags))
2680                return;
2681
2682        while (!list_empty(&vha->work_list) && i > 0) {
2683                qla2x00_do_work(vha);
2684                i--;
2685        }
2686
2687        spin_lock_irqsave(&vha->work_lock, flags);
2688        clear_bit(IOCB_WORK_ACTIVE, &vha->dpc_flags);
2689        spin_unlock_irqrestore(&vha->work_lock, flags);
2690}
2691
2692#ifdef CONFIG_RHEL_DIFFERENCES
2693static const struct pci_device_id rh_deprecated_pci_table[] = {
2694        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2532) },
2695        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2031) },
2696        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8031) },
2697        { 0 }
2698};
2699
2700static const struct pci_device_id rh_unmaintained_pci_table[] = {
2701        { 0 }
2702};
2703
2704static const struct pci_device_id rh_disabled_pci_table[] = {
2705        { 0 }
2706};
2707#endif
2708
2709/*
2710 * PCI driver interface
2711 */
2712static int
2713qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2714{
2715        int     ret = -ENODEV;
2716        struct Scsi_Host *host;
2717        scsi_qla_host_t *base_vha = NULL;
2718        struct qla_hw_data *ha;
2719        char pci_info[30];
2720        char fw_str[30], wq_name[30];
2721        struct scsi_host_template *sht;
2722        int bars, mem_only = 0;
2723        uint16_t req_length = 0, rsp_length = 0;
2724        struct req_que *req = NULL;
2725        struct rsp_que *rsp = NULL;
2726        int i;
2727
2728#ifdef CONFIG_RHEL_DIFFERENCES
2729        if (pci_hw_disabled(rh_disabled_pci_table, pdev))
2730                return -ENODEV;
2731
2732        pci_hw_deprecated(rh_deprecated_pci_table, pdev);
2733        pci_hw_unmaintained(rh_unmaintained_pci_table, pdev);
2734#endif
2735
2736        bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
2737        sht = &qla2xxx_driver_template;
2738        if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2532 ||
2739            pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2031 ||
2740            pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8031 ||
2741            pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2071 ||
2742            pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2271 ||
2743            pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2261 ||
2744            pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2081 ||
2745            pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2281 ||
2746            pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2089 ||
2747            pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2289) {
2748                bars = pci_select_bars(pdev, IORESOURCE_MEM);
2749                mem_only = 1;
2750                ql_dbg_pci(ql_dbg_init, pdev, 0x0007,
2751                    "Mem only adapter.\n");
2752        }
2753        ql_dbg_pci(ql_dbg_init, pdev, 0x0008,
2754            "Bars=%d.\n", bars);
2755
2756        if (mem_only) {
2757                if (pci_enable_device_mem(pdev))
2758                        return ret;
2759        } else {
2760                if (pci_enable_device(pdev))
2761                        return ret;
2762        }
2763
2764        if (is_kdump_kernel()) {
2765                ql2xmqsupport = 0;
2766                ql2xallocfwdump = 0;
2767        }
2768
2769        /* This may fail but that's ok */
2770        pci_enable_pcie_error_reporting(pdev);
2771
2772        ha = kzalloc(sizeof(struct qla_hw_data), GFP_KERNEL);
2773        if (!ha) {
2774                ql_log_pci(ql_log_fatal, pdev, 0x0009,
2775                    "Unable to allocate memory for ha.\n");
2776                goto disable_device;
2777        }
2778        ql_dbg_pci(ql_dbg_init, pdev, 0x000a,
2779            "Memory allocated for ha=%p.\n", ha);
2780        ha->pdev = pdev;
2781        INIT_LIST_HEAD(&ha->tgt.q_full_list);
2782        spin_lock_init(&ha->tgt.q_full_lock);
2783        spin_lock_init(&ha->tgt.sess_lock);
2784        spin_lock_init(&ha->tgt.atio_lock);
2785
2786        spin_lock_init(&ha->sadb_lock);
2787        INIT_LIST_HEAD(&ha->sadb_tx_index_list);
2788        INIT_LIST_HEAD(&ha->sadb_rx_index_list);
2789
2790        spin_lock_init(&ha->sadb_fp_lock);
2791
2792        if (qla_edif_sadb_build_free_pool(ha)) {
2793                kfree(ha);
2794                goto  disable_device;
2795        }
2796
2797        atomic_set(&ha->nvme_active_aen_cnt, 0);
2798
2799        /* Clear our data area */
2800        ha->bars = bars;
2801        ha->mem_only = mem_only;
2802        spin_lock_init(&ha->hardware_lock);
2803        spin_lock_init(&ha->vport_slock);
2804        mutex_init(&ha->selflogin_lock);
2805        mutex_init(&ha->optrom_mutex);
2806
2807        /* Set ISP-type information. */
2808        qla2x00_set_isp_flags(ha);
2809
2810        /* Set EEH reset type to fundamental if required by hba */
2811        if (IS_QLA24XX(ha) || IS_QLA25XX(ha) || IS_QLA81XX(ha) ||
2812            IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))
2813                pdev->needs_freset = 1;
2814
2815        ha->prev_topology = 0;
2816        ha->init_cb_size = sizeof(init_cb_t);
2817        ha->link_data_rate = PORT_SPEED_UNKNOWN;
2818        ha->optrom_size = OPTROM_SIZE_2300;
2819        ha->max_exchg = FW_MAX_EXCHANGES_CNT;
2820        atomic_set(&ha->num_pend_mbx_stage1, 0);
2821        atomic_set(&ha->num_pend_mbx_stage2, 0);
2822        atomic_set(&ha->num_pend_mbx_stage3, 0);
2823        atomic_set(&ha->zio_threshold, DEFAULT_ZIO_THRESHOLD);
2824        ha->last_zio_threshold = DEFAULT_ZIO_THRESHOLD;
2825
2826        /* Assign ISP specific operations. */
2827        if (IS_QLA2100(ha)) {
2828                ha->max_fibre_devices = MAX_FIBRE_DEVICES_2100;
2829                ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
2830                req_length = REQUEST_ENTRY_CNT_2100;
2831                rsp_length = RESPONSE_ENTRY_CNT_2100;
2832                ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
2833                ha->gid_list_info_size = 4;
2834                ha->flash_conf_off = ~0;
2835                ha->flash_data_off = ~0;
2836                ha->nvram_conf_off = ~0;
2837                ha->nvram_data_off = ~0;
2838                ha->isp_ops = &qla2100_isp_ops;
2839        } else if (IS_QLA2200(ha)) {
2840                ha->max_fibre_devices = MAX_FIBRE_DEVICES_2100;
2841                ha->mbx_count = MAILBOX_REGISTER_COUNT_2200;
2842                req_length = REQUEST_ENTRY_CNT_2200;
2843                rsp_length = RESPONSE_ENTRY_CNT_2100;
2844                ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
2845                ha->gid_list_info_size = 4;
2846                ha->flash_conf_off = ~0;
2847                ha->flash_data_off = ~0;
2848                ha->nvram_conf_off = ~0;
2849                ha->nvram_data_off = ~0;
2850                ha->isp_ops = &qla2100_isp_ops;
2851        } else if (IS_QLA23XX(ha)) {
2852                ha->max_fibre_devices = MAX_FIBRE_DEVICES_2100;
2853                ha->mbx_count = MAILBOX_REGISTER_COUNT;
2854                req_length = REQUEST_ENTRY_CNT_2200;
2855                rsp_length = RESPONSE_ENTRY_CNT_2300;
2856                ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2857                ha->gid_list_info_size = 6;
2858                if (IS_QLA2322(ha) || IS_QLA6322(ha))
2859                        ha->optrom_size = OPTROM_SIZE_2322;
2860                ha->flash_conf_off = ~0;
2861                ha->flash_data_off = ~0;
2862                ha->nvram_conf_off = ~0;
2863                ha->nvram_data_off = ~0;
2864                ha->isp_ops = &qla2300_isp_ops;
2865        } else if (IS_QLA24XX_TYPE(ha)) {
2866                ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2867                ha->mbx_count = MAILBOX_REGISTER_COUNT;
2868                req_length = REQUEST_ENTRY_CNT_24XX;
2869                rsp_length = RESPONSE_ENTRY_CNT_2300;
2870                ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2871                ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2872                ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
2873                ha->gid_list_info_size = 8;
2874                ha->optrom_size = OPTROM_SIZE_24XX;
2875                ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA24XX;
2876                ha->isp_ops = &qla24xx_isp_ops;
2877                ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2878                ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2879                ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2880                ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2881        } else if (IS_QLA25XX(ha)) {
2882                ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2883                ha->mbx_count = MAILBOX_REGISTER_COUNT;
2884                req_length = REQUEST_ENTRY_CNT_24XX;
2885                rsp_length = RESPONSE_ENTRY_CNT_2300;
2886                ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2887                ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2888                ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
2889                ha->gid_list_info_size = 8;
2890                ha->optrom_size = OPTROM_SIZE_25XX;
2891                ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2892                ha->isp_ops = &qla25xx_isp_ops;
2893                ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2894                ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2895                ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2896                ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2897        } else if (IS_QLA81XX(ha)) {
2898                ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2899                ha->mbx_count = MAILBOX_REGISTER_COUNT;
2900                req_length = REQUEST_ENTRY_CNT_24XX;
2901                rsp_length = RESPONSE_ENTRY_CNT_2300;
2902                ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2903                ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2904                ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2905                ha->gid_list_info_size = 8;
2906                ha->optrom_size = OPTROM_SIZE_81XX;
2907                ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2908                ha->isp_ops = &qla81xx_isp_ops;
2909                ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
2910                ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
2911                ha->nvram_conf_off = ~0;
2912                ha->nvram_data_off = ~0;
2913        } else if (IS_QLA82XX(ha)) {
2914                ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2915                ha->mbx_count = MAILBOX_REGISTER_COUNT;
2916                req_length = REQUEST_ENTRY_CNT_82XX;
2917                rsp_length = RESPONSE_ENTRY_CNT_82XX;
2918                ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2919                ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2920                ha->gid_list_info_size = 8;
2921                ha->optrom_size = OPTROM_SIZE_82XX;
2922                ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2923                ha->isp_ops = &qla82xx_isp_ops;
2924                ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2925                ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2926                ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2927                ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2928        } else if (IS_QLA8044(ha)) {
2929                ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2930                ha->mbx_count = MAILBOX_REGISTER_COUNT;
2931                req_length = REQUEST_ENTRY_CNT_82XX;
2932                rsp_length = RESPONSE_ENTRY_CNT_82XX;
2933                ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2934                ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2935                ha->gid_list_info_size = 8;
2936                ha->optrom_size = OPTROM_SIZE_83XX;
2937                ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2938                ha->isp_ops = &qla8044_isp_ops;
2939                ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2940                ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2941                ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2942                ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2943        } else if (IS_QLA83XX(ha)) {
2944                ha->portnum = PCI_FUNC(ha->pdev->devfn);
2945                ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2946                ha->mbx_count = MAILBOX_REGISTER_COUNT;
2947                req_length = REQUEST_ENTRY_CNT_83XX;
2948                rsp_length = RESPONSE_ENTRY_CNT_83XX;
2949                ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2950                ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2951                ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2952                ha->gid_list_info_size = 8;
2953                ha->optrom_size = OPTROM_SIZE_83XX;
2954                ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2955                ha->isp_ops = &qla83xx_isp_ops;
2956                ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
2957                ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
2958                ha->nvram_conf_off = ~0;
2959                ha->nvram_data_off = ~0;
2960        }  else if (IS_QLAFX00(ha)) {
2961                ha->max_fibre_devices = MAX_FIBRE_DEVICES_FX00;
2962                ha->mbx_count = MAILBOX_REGISTER_COUNT_FX00;
2963                ha->aen_mbx_count = AEN_MAILBOX_REGISTER_COUNT_FX00;
2964                req_length = REQUEST_ENTRY_CNT_FX00;
2965                rsp_length = RESPONSE_ENTRY_CNT_FX00;
2966                ha->isp_ops = &qlafx00_isp_ops;
2967                ha->port_down_retry_count = 30; /* default value */
2968                ha->mr.fw_hbt_cnt = QLAFX00_HEARTBEAT_INTERVAL;
2969                ha->mr.fw_reset_timer_tick = QLAFX00_RESET_INTERVAL;
2970                ha->mr.fw_critemp_timer_tick = QLAFX00_CRITEMP_INTERVAL;
2971                ha->mr.fw_hbt_en = 1;
2972                ha->mr.host_info_resend = false;
2973                ha->mr.hinfo_resend_timer_tick = QLAFX00_HINFO_RESEND_INTERVAL;
2974        } else if (IS_QLA27XX(ha)) {
2975                ha->portnum = PCI_FUNC(ha->pdev->devfn);
2976                ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2977                ha->mbx_count = MAILBOX_REGISTER_COUNT;
2978                req_length = REQUEST_ENTRY_CNT_83XX;
2979                rsp_length = RESPONSE_ENTRY_CNT_83XX;
2980                ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2981                ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2982                ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2983                ha->gid_list_info_size = 8;
2984                ha->optrom_size = OPTROM_SIZE_83XX;
2985                ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2986                ha->isp_ops = &qla27xx_isp_ops;
2987                ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
2988                ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
2989                ha->nvram_conf_off = ~0;
2990                ha->nvram_data_off = ~0;
2991        } else if (IS_QLA28XX(ha)) {
2992                ha->portnum = PCI_FUNC(ha->pdev->devfn);
2993                ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2994                ha->mbx_count = MAILBOX_REGISTER_COUNT;
2995                req_length = REQUEST_ENTRY_CNT_83XX;
2996                rsp_length = RESPONSE_ENTRY_CNT_83XX;
2997                ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2998                ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2999                ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
3000                ha->gid_list_info_size = 8;
3001                ha->optrom_size = OPTROM_SIZE_28XX;
3002                ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
3003                ha->isp_ops = &qla27xx_isp_ops;
3004                ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_28XX;
3005                ha->flash_data_off = FARX_ACCESS_FLASH_DATA_28XX;
3006                ha->nvram_conf_off = ~0;
3007                ha->nvram_data_off = ~0;
3008        }
3009
3010        ql_dbg_pci(ql_dbg_init, pdev, 0x001e,
3011            "mbx_count=%d, req_length=%d, "
3012            "rsp_length=%d, max_loop_id=%d, init_cb_size=%d, "
3013            "gid_list_info_size=%d, optrom_size=%d, nvram_npiv_size=%d, "
3014            "max_fibre_devices=%d.\n",
3015            ha->mbx_count, req_length, rsp_length, ha->max_loop_id,
3016            ha->init_cb_size, ha->gid_list_info_size, ha->optrom_size,
3017            ha->nvram_npiv_size, ha->max_fibre_devices);
3018        ql_dbg_pci(ql_dbg_init, pdev, 0x001f,
3019            "isp_ops=%p, flash_conf_off=%d, "
3020            "flash_data_off=%d, nvram_conf_off=%d, nvram_data_off=%d.\n",
3021            ha->isp_ops, ha->flash_conf_off, ha->flash_data_off,
3022            ha->nvram_conf_off, ha->nvram_data_off);
3023
3024        /* Configure PCI I/O space */
3025        ret = ha->isp_ops->iospace_config(ha);
3026        if (ret)
3027                goto iospace_config_failed;
3028
3029        ql_log_pci(ql_log_info, pdev, 0x001d,
3030            "Found an ISP%04X irq %d iobase 0x%p.\n",
3031            pdev->device, pdev->irq, ha->iobase);
3032        mutex_init(&ha->vport_lock);
3033        mutex_init(&ha->mq_lock);
3034        init_completion(&ha->mbx_cmd_comp);
3035        complete(&ha->mbx_cmd_comp);
3036        init_completion(&ha->mbx_intr_comp);
3037        init_completion(&ha->dcbx_comp);
3038        init_completion(&ha->lb_portup_comp);
3039
3040        set_bit(0, (unsigned long *) ha->vp_idx_map);
3041
3042        qla2x00_config_dma_addressing(ha);
3043        ql_dbg_pci(ql_dbg_init, pdev, 0x0020,
3044            "64 Bit addressing is %s.\n",
3045            ha->flags.enable_64bit_addressing ? "enable" :
3046            "disable");
3047        ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
3048        if (ret) {
3049                ql_log_pci(ql_log_fatal, pdev, 0x0031,
3050                    "Failed to allocate memory for adapter, aborting.\n");
3051
3052                goto probe_hw_failed;
3053        }
3054
3055        req->max_q_depth = MAX_Q_DEPTH;
3056        if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
3057                req->max_q_depth = ql2xmaxqdepth;
3058
3059
3060        base_vha = qla2x00_create_host(sht, ha);
3061        if (!base_vha) {
3062                ret = -ENOMEM;
3063                goto probe_hw_failed;
3064        }
3065
3066        pci_set_drvdata(pdev, base_vha);
3067        set_bit(PFLG_DRIVER_PROBING, &base_vha->pci_flags);
3068
3069        host = base_vha->host;
3070        base_vha->req = req;
3071        if (IS_QLA2XXX_MIDTYPE(ha))
3072                base_vha->mgmt_svr_loop_id =
3073                        qla2x00_reserve_mgmt_server_loop_id(base_vha);
3074        else
3075                base_vha->mgmt_svr_loop_id = MANAGEMENT_SERVER +
3076                                                base_vha->vp_idx;
3077
3078        /* Setup fcport template structure. */
3079        ha->mr.fcport.vha = base_vha;
3080        ha->mr.fcport.port_type = FCT_UNKNOWN;
3081        ha->mr.fcport.loop_id = FC_NO_LOOP_ID;
3082        qla2x00_set_fcport_state(&ha->mr.fcport, FCS_UNCONFIGURED);
3083        ha->mr.fcport.supported_classes = FC_COS_UNSPECIFIED;
3084        ha->mr.fcport.scan_state = 1;
3085
3086        qla2xxx_reset_stats(host, QLA2XX_HW_ERROR | QLA2XX_SHT_LNK_DWN |
3087                            QLA2XX_INT_ERR | QLA2XX_CMD_TIMEOUT |
3088                            QLA2XX_RESET_CMD_ERR | QLA2XX_TGT_SHT_LNK_DOWN);
3089
3090        /* Set the SG table size based on ISP type */
3091        if (!IS_FWI2_CAPABLE(ha)) {
3092                if (IS_QLA2100(ha))
3093                        host->sg_tablesize = 32;
3094        } else {
3095                if (!IS_QLA82XX(ha))
3096                        host->sg_tablesize = QLA_SG_ALL;
3097        }
3098        host->max_id = ha->max_fibre_devices;
3099        host->cmd_per_lun = 3;
3100        host->unique_id = host->host_no;
3101        if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif)
3102                host->max_cmd_len = 32;
3103        else
3104                host->max_cmd_len = MAX_CMDSZ;
3105        host->max_channel = MAX_BUSES - 1;
3106        /* Older HBAs support only 16-bit LUNs */
3107        if (!IS_QLAFX00(ha) && !IS_FWI2_CAPABLE(ha) &&
3108            ql2xmaxlun > 0xffff)
3109                host->max_lun = 0xffff;
3110        else
3111                host->max_lun = ql2xmaxlun;
3112        host->transportt = qla2xxx_transport_template;
3113        sht->vendor_id = (SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC);
3114
3115        ql_dbg(ql_dbg_init, base_vha, 0x0033,
3116            "max_id=%d this_id=%d "
3117            "cmd_per_len=%d unique_id=%d max_cmd_len=%d max_channel=%d "
3118            "max_lun=%llu transportt=%p, vendor_id=%llu.\n", host->max_id,
3119            host->this_id, host->cmd_per_lun, host->unique_id,
3120            host->max_cmd_len, host->max_channel, host->max_lun,
3121            host->transportt, sht->vendor_id);
3122
3123        INIT_WORK(&base_vha->iocb_work, qla2x00_iocb_work_fn);
3124
3125        /* Set up the irqs */
3126        ret = qla2x00_request_irqs(ha, rsp);
3127        if (ret)
3128                goto probe_failed;
3129
3130        /* Alloc arrays of request and response ring ptrs */
3131        ret = qla2x00_alloc_queues(ha, req, rsp);
3132        if (ret) {
3133                ql_log(ql_log_fatal, base_vha, 0x003d,
3134                    "Failed to allocate memory for queue pointers..."
3135                    "aborting.\n");
3136                ret = -ENODEV;
3137                goto probe_failed;
3138        }
3139
3140        if (ha->mqenable) {
3141                /* number of hardware queues supported by blk/scsi-mq*/
3142                host->nr_hw_queues = ha->max_qpairs;
3143
3144                ql_dbg(ql_dbg_init, base_vha, 0x0192,
3145                        "blk/scsi-mq enabled, HW queues = %d.\n", host->nr_hw_queues);
3146        } else {
3147                if (ql2xnvmeenable) {
3148                        host->nr_hw_queues = ha->max_qpairs;
3149                        ql_dbg(ql_dbg_init, base_vha, 0x0194,
3150                            "FC-NVMe support is enabled, HW queues=%d\n",
3151                            host->nr_hw_queues);
3152                } else {
3153                        ql_dbg(ql_dbg_init, base_vha, 0x0193,
3154                            "blk/scsi-mq disabled.\n");
3155                }
3156        }
3157
3158        qlt_probe_one_stage1(base_vha, ha);
3159
3160        pci_save_state(pdev);
3161
3162        /* Assign back pointers */
3163        rsp->req = req;
3164        req->rsp = rsp;
3165
3166        if (IS_QLAFX00(ha)) {
3167                ha->rsp_q_map[0] = rsp;
3168                ha->req_q_map[0] = req;
3169                set_bit(0, ha->req_qid_map);
3170                set_bit(0, ha->rsp_qid_map);
3171        }
3172
3173        /* FWI2-capable only. */
3174        req->req_q_in = &ha->iobase->isp24.req_q_in;
3175        req->req_q_out = &ha->iobase->isp24.req_q_out;
3176        rsp->rsp_q_in = &ha->iobase->isp24.rsp_q_in;
3177        rsp->rsp_q_out = &ha->iobase->isp24.rsp_q_out;
3178        if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
3179            IS_QLA28XX(ha)) {
3180                req->req_q_in = &ha->mqiobase->isp25mq.req_q_in;
3181                req->req_q_out = &ha->mqiobase->isp25mq.req_q_out;
3182                rsp->rsp_q_in = &ha->mqiobase->isp25mq.rsp_q_in;
3183                rsp->rsp_q_out =  &ha->mqiobase->isp25mq.rsp_q_out;
3184        }
3185
3186        if (IS_QLAFX00(ha)) {
3187                req->req_q_in = &ha->iobase->ispfx00.req_q_in;
3188                req->req_q_out = &ha->iobase->ispfx00.req_q_out;
3189                rsp->rsp_q_in = &ha->iobase->ispfx00.rsp_q_in;
3190                rsp->rsp_q_out = &ha->iobase->ispfx00.rsp_q_out;
3191        }
3192
3193        if (IS_P3P_TYPE(ha)) {
3194                req->req_q_out = &ha->iobase->isp82.req_q_out[0];
3195                rsp->rsp_q_in = &ha->iobase->isp82.rsp_q_in[0];
3196                rsp->rsp_q_out = &ha->iobase->isp82.rsp_q_out[0];
3197        }
3198
3199        ql_dbg(ql_dbg_multiq, base_vha, 0xc009,
3200            "rsp_q_map=%p req_q_map=%p rsp->req=%p req->rsp=%p.\n",
3201            ha->rsp_q_map, ha->req_q_map, rsp->req, req->rsp);
3202        ql_dbg(ql_dbg_multiq, base_vha, 0xc00a,
3203            "req->req_q_in=%p req->req_q_out=%p "
3204            "rsp->rsp_q_in=%p rsp->rsp_q_out=%p.\n",
3205            req->req_q_in, req->req_q_out,
3206            rsp->rsp_q_in, rsp->rsp_q_out);
3207        ql_dbg(ql_dbg_init, base_vha, 0x003e,
3208            "rsp_q_map=%p req_q_map=%p rsp->req=%p req->rsp=%p.\n",
3209            ha->rsp_q_map, ha->req_q_map, rsp->req, req->rsp);
3210        ql_dbg(ql_dbg_init, base_vha, 0x003f,
3211            "req->req_q_in=%p req->req_q_out=%p rsp->rsp_q_in=%p rsp->rsp_q_out=%p.\n",
3212            req->req_q_in, req->req_q_out, rsp->rsp_q_in, rsp->rsp_q_out);
3213
3214        ha->wq = alloc_workqueue("qla2xxx_wq", WQ_MEM_RECLAIM, 0);
3215        if (unlikely(!ha->wq)) {
3216                ret = -ENOMEM;
3217                goto probe_failed;
3218        }
3219
3220        if (ha->isp_ops->initialize_adapter(base_vha)) {
3221                ql_log(ql_log_fatal, base_vha, 0x00d6,
3222                    "Failed to initialize adapter - Adapter flags %x.\n",
3223                    base_vha->device_flags);
3224
3225                if (IS_QLA82XX(ha)) {
3226                        qla82xx_idc_lock(ha);
3227                        qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3228                                QLA8XXX_DEV_FAILED);
3229                        qla82xx_idc_unlock(ha);
3230                        ql_log(ql_log_fatal, base_vha, 0x00d7,
3231                            "HW State: FAILED.\n");
3232                } else if (IS_QLA8044(ha)) {
3233                        qla8044_idc_lock(ha);
3234                        qla8044_wr_direct(base_vha,
3235                                QLA8044_CRB_DEV_STATE_INDEX,
3236                                QLA8XXX_DEV_FAILED);
3237                        qla8044_idc_unlock(ha);
3238                        ql_log(ql_log_fatal, base_vha, 0x0150,
3239                            "HW State: FAILED.\n");
3240                }
3241
3242                ret = -ENODEV;
3243                goto probe_failed;
3244        }
3245
3246        if (IS_QLAFX00(ha))
3247                host->can_queue = QLAFX00_MAX_CANQUEUE;
3248        else
3249                host->can_queue = req->num_outstanding_cmds - 10;
3250
3251        ql_dbg(ql_dbg_init, base_vha, 0x0032,
3252            "can_queue=%d, req=%p, mgmt_svr_loop_id=%d, sg_tablesize=%d.\n",
3253            host->can_queue, base_vha->req,
3254            base_vha->mgmt_svr_loop_id, host->sg_tablesize);
3255
3256        if (ha->mqenable) {
3257                bool startit = false;
3258
3259                if (QLA_TGT_MODE_ENABLED())
3260                        startit = false;
3261
3262                if (ql2x_ini_mode == QLA2XXX_INI_MODE_ENABLED)
3263                        startit = true;
3264
3265                /* Create start of day qpairs for Block MQ */
3266                for (i = 0; i < ha->max_qpairs; i++)
3267                        qla2xxx_create_qpair(base_vha, 5, 0, startit);
3268        }
3269        qla_init_iocb_limit(base_vha);
3270
3271        if (ha->flags.running_gold_fw)
3272                goto skip_dpc;
3273
3274        /*
3275         * Startup the kernel thread for this host adapter
3276         */
3277        ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
3278            "%s_dpc", base_vha->host_str);
3279        if (IS_ERR(ha->dpc_thread)) {
3280                ql_log(ql_log_fatal, base_vha, 0x00ed,
3281                    "Failed to start DPC thread.\n");
3282                ret = PTR_ERR(ha->dpc_thread);
3283                ha->dpc_thread = NULL;
3284                goto probe_failed;
3285        }
3286        ql_dbg(ql_dbg_init, base_vha, 0x00ee,
3287            "DPC thread started successfully.\n");
3288
3289        /*
3290         * If we're not coming up in initiator mode, we might sit for
3291         * a while without waking up the dpc thread, which leads to a
3292         * stuck process warning.  So just kick the dpc once here and
3293         * let the kthread start (and go back to sleep in qla2x00_do_dpc).
3294         */
3295        qla2xxx_wake_dpc(base_vha);
3296
3297        INIT_WORK(&ha->board_disable, qla2x00_disable_board_on_pci_error);
3298
3299        if (IS_QLA8031(ha) || IS_MCTP_CAPABLE(ha)) {
3300                sprintf(wq_name, "qla2xxx_%lu_dpc_lp_wq", base_vha->host_no);
3301                ha->dpc_lp_wq = create_singlethread_workqueue(wq_name);
3302                INIT_WORK(&ha->idc_aen, qla83xx_service_idc_aen);
3303
3304                sprintf(wq_name, "qla2xxx_%lu_dpc_hp_wq", base_vha->host_no);
3305                ha->dpc_hp_wq = create_singlethread_workqueue(wq_name);
3306                INIT_WORK(&ha->nic_core_reset, qla83xx_nic_core_reset_work);
3307                INIT_WORK(&ha->idc_state_handler,
3308                    qla83xx_idc_state_handler_work);
3309                INIT_WORK(&ha->nic_core_unrecoverable,
3310                    qla83xx_nic_core_unrecoverable_work);
3311        }
3312
3313skip_dpc:
3314        list_add_tail(&base_vha->list, &ha->vp_list);
3315        base_vha->host->irq = ha->pdev->irq;
3316
3317        /* Initialized the timer */
3318        qla2x00_start_timer(base_vha, WATCH_INTERVAL);
3319        ql_dbg(ql_dbg_init, base_vha, 0x00ef,
3320            "Started qla2x00_timer with "
3321            "interval=%d.\n", WATCH_INTERVAL);
3322        ql_dbg(ql_dbg_init, base_vha, 0x00f0,
3323            "Detected hba at address=%p.\n",
3324            ha);
3325
3326        if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
3327                if (ha->fw_attributes & BIT_4) {
3328                        int prot = 0, guard;
3329
3330                        base_vha->flags.difdix_supported = 1;
3331                        ql_dbg(ql_dbg_init, base_vha, 0x00f1,
3332                            "Registering for DIF/DIX type 1 and 3 protection.\n");
3333                        if (ql2xenabledif == 1)
3334                                prot = SHOST_DIX_TYPE0_PROTECTION;
3335                        if (ql2xprotmask)
3336                                scsi_host_set_prot(host, ql2xprotmask);
3337                        else
3338                                scsi_host_set_prot(host,
3339                                    prot | SHOST_DIF_TYPE1_PROTECTION
3340                                    | SHOST_DIF_TYPE2_PROTECTION
3341                                    | SHOST_DIF_TYPE3_PROTECTION
3342                                    | SHOST_DIX_TYPE1_PROTECTION
3343                                    | SHOST_DIX_TYPE2_PROTECTION
3344                                    | SHOST_DIX_TYPE3_PROTECTION);
3345
3346                        guard = SHOST_DIX_GUARD_CRC;
3347
3348                        if (IS_PI_IPGUARD_CAPABLE(ha) &&
3349                            (ql2xenabledif > 1 || IS_PI_DIFB_DIX0_CAPABLE(ha)))
3350                                guard |= SHOST_DIX_GUARD_IP;
3351
3352                        if (ql2xprotguard)
3353                                scsi_host_set_guard(host, ql2xprotguard);
3354                        else
3355                                scsi_host_set_guard(host, guard);
3356                } else
3357                        base_vha->flags.difdix_supported = 0;
3358        }
3359
3360        ha->isp_ops->enable_intrs(ha);
3361
3362        if (IS_QLAFX00(ha)) {
3363                ret = qlafx00_fx_disc(base_vha,
3364                        &base_vha->hw->mr.fcport, FXDISC_GET_CONFIG_INFO);
3365                host->sg_tablesize = (ha->mr.extended_io_enabled) ?
3366                    QLA_SG_ALL : 128;
3367        }
3368
3369        ret = scsi_add_host(host, &pdev->dev);
3370        if (ret)
3371                goto probe_failed;
3372
3373        base_vha->flags.init_done = 1;
3374        base_vha->flags.online = 1;
3375        ha->prev_minidump_failed = 0;
3376
3377        ql_dbg(ql_dbg_init, base_vha, 0x00f2,
3378            "Init done and hba is online.\n");
3379
3380        if (qla_ini_mode_enabled(base_vha) ||
3381                qla_dual_mode_enabled(base_vha))
3382                scsi_scan_host(host);
3383        else
3384                ql_dbg(ql_dbg_init, base_vha, 0x0122,
3385                        "skipping scsi_scan_host() for non-initiator port\n");
3386
3387        qla2x00_alloc_sysfs_attr(base_vha);
3388
3389        if (IS_QLAFX00(ha)) {
3390                ret = qlafx00_fx_disc(base_vha,
3391                        &base_vha->hw->mr.fcport, FXDISC_GET_PORT_INFO);
3392
3393                /* Register system information */
3394                ret =  qlafx00_fx_disc(base_vha,
3395                        &base_vha->hw->mr.fcport, FXDISC_REG_HOST_INFO);
3396        }
3397
3398        qla2x00_init_host_attr(base_vha);
3399
3400        qla2x00_dfs_setup(base_vha);
3401
3402        ql_log(ql_log_info, base_vha, 0x00fb,
3403            "QLogic %s - %s.\n", ha->model_number, ha->model_desc);
3404        ql_log(ql_log_info, base_vha, 0x00fc,
3405            "ISP%04X: %s @ %s hdma%c host#=%ld fw=%s.\n",
3406            pdev->device, ha->isp_ops->pci_info_str(base_vha, pci_info,
3407                                                       sizeof(pci_info)),
3408            pci_name(pdev), ha->flags.enable_64bit_addressing ? '+' : '-',
3409            base_vha->host_no,
3410            ha->isp_ops->fw_version_str(base_vha, fw_str, sizeof(fw_str)));
3411
3412        qlt_add_target(ha, base_vha);
3413
3414        clear_bit(PFLG_DRIVER_PROBING, &base_vha->pci_flags);
3415
3416        if (test_bit(UNLOADING, &base_vha->dpc_flags))
3417                return -ENODEV;
3418
3419        return 0;
3420
3421probe_failed:
3422        qla_enode_stop(base_vha);
3423        qla_edb_stop(base_vha);
3424        if (base_vha->gnl.l) {
3425                dma_free_coherent(&ha->pdev->dev, base_vha->gnl.size,
3426                                base_vha->gnl.l, base_vha->gnl.ldma);
3427                base_vha->gnl.l = NULL;
3428        }
3429
3430        if (base_vha->timer_active)
3431                qla2x00_stop_timer(base_vha);
3432        base_vha->flags.online = 0;
3433        if (ha->dpc_thread) {
3434                struct task_struct *t = ha->dpc_thread;
3435
3436                ha->dpc_thread = NULL;
3437                kthread_stop(t);
3438        }
3439
3440        qla2x00_free_device(base_vha);
3441        scsi_host_put(base_vha->host);
3442        /*
3443         * Need to NULL out local req/rsp after
3444         * qla2x00_free_device => qla2x00_free_queues frees
3445         * what these are pointing to. Or else we'll
3446         * fall over below in qla2x00_free_req/rsp_que.
3447         */
3448        req = NULL;
3449        rsp = NULL;
3450
3451probe_hw_failed:
3452        qla2x00_mem_free(ha);
3453        qla2x00_free_req_que(ha, req);
3454        qla2x00_free_rsp_que(ha, rsp);
3455        qla2x00_clear_drv_active(ha);
3456
3457iospace_config_failed:
3458        if (IS_P3P_TYPE(ha)) {
3459                if (!ha->nx_pcibase)
3460                        iounmap((device_reg_t *)ha->nx_pcibase);
3461                if (!ql2xdbwr)
3462                        iounmap((device_reg_t *)ha->nxdb_wr_ptr);
3463        } else {
3464                if (ha->iobase)
3465                        iounmap(ha->iobase);
3466                if (ha->cregbase)
3467                        iounmap(ha->cregbase);
3468        }
3469        pci_release_selected_regions(ha->pdev, ha->bars);
3470        kfree(ha);
3471
3472disable_device:
3473        pci_disable_device(pdev);
3474        return ret;
3475}
3476
3477static void __qla_set_remove_flag(scsi_qla_host_t *base_vha)
3478{
3479        scsi_qla_host_t *vp;
3480        unsigned long flags;
3481        struct qla_hw_data *ha;
3482
3483        if (!base_vha)
3484                return;
3485
3486        ha = base_vha->hw;
3487
3488        spin_lock_irqsave(&ha->vport_slock, flags);
3489        list_for_each_entry(vp, &ha->vp_list, list)
3490                set_bit(PFLG_DRIVER_REMOVING, &vp->pci_flags);
3491
3492        /*
3493         * Indicate device removal to prevent future board_disable
3494         * and wait until any pending board_disable has completed.
3495         */
3496        set_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags);
3497        spin_unlock_irqrestore(&ha->vport_slock, flags);
3498}
3499
3500static void
3501qla2x00_shutdown(struct pci_dev *pdev)
3502{
3503        scsi_qla_host_t *vha;
3504        struct qla_hw_data  *ha;
3505
3506        vha = pci_get_drvdata(pdev);
3507        ha = vha->hw;
3508
3509        ql_log(ql_log_info, vha, 0xfffa,
3510                "Adapter shutdown\n");
3511
3512        /*
3513         * Prevent future board_disable and wait
3514         * until any pending board_disable has completed.
3515         */
3516        __qla_set_remove_flag(vha);
3517        cancel_work_sync(&ha->board_disable);
3518
3519        if (!atomic_read(&pdev->enable_cnt))
3520                return;
3521
3522        /* Notify ISPFX00 firmware */
3523        if (IS_QLAFX00(ha))
3524                qlafx00_driver_shutdown(vha, 20);
3525
3526        /* Turn-off FCE trace */
3527        if (ha->flags.fce_enabled) {
3528                qla2x00_disable_fce_trace(vha, NULL, NULL);
3529                ha->flags.fce_enabled = 0;
3530        }
3531
3532        /* Turn-off EFT trace */
3533        if (ha->eft)
3534                qla2x00_disable_eft_trace(vha);
3535
3536        if (IS_QLA25XX(ha) ||  IS_QLA2031(ha) || IS_QLA27XX(ha) ||
3537            IS_QLA28XX(ha)) {
3538                if (ha->flags.fw_started)
3539                        qla2x00_abort_isp_cleanup(vha);
3540        } else {
3541                /* Stop currently executing firmware. */
3542                qla2x00_try_to_stop_firmware(vha);
3543        }
3544
3545        /* Disable timer */
3546        if (vha->timer_active)
3547                qla2x00_stop_timer(vha);
3548
3549        /* Turn adapter off line */
3550        vha->flags.online = 0;
3551
3552        /* turn-off interrupts on the card */
3553        if (ha->interrupts_on) {
3554                vha->flags.init_done = 0;
3555                ha->isp_ops->disable_intrs(ha);
3556        }
3557
3558        qla2x00_free_irqs(vha);
3559
3560        qla2x00_free_fw_dump(ha);
3561
3562        pci_disable_device(pdev);
3563        ql_log(ql_log_info, vha, 0xfffe,
3564                "Adapter shutdown successfully.\n");
3565}
3566
3567/* Deletes all the virtual ports for a given ha */
3568static void
3569qla2x00_delete_all_vps(struct qla_hw_data *ha, scsi_qla_host_t *base_vha)
3570{
3571        scsi_qla_host_t *vha;
3572        unsigned long flags;
3573
3574        mutex_lock(&ha->vport_lock);
3575        while (ha->cur_vport_count) {
3576                spin_lock_irqsave(&ha->vport_slock, flags);
3577
3578                BUG_ON(base_vha->list.next == &ha->vp_list);
3579                /* This assumes first entry in ha->vp_list is always base vha */
3580                vha = list_first_entry(&base_vha->list, scsi_qla_host_t, list);
3581                scsi_host_get(vha->host);
3582
3583                spin_unlock_irqrestore(&ha->vport_slock, flags);
3584                mutex_unlock(&ha->vport_lock);
3585
3586                qla_nvme_delete(vha);
3587
3588                fc_vport_terminate(vha->fc_vport);
3589                scsi_host_put(vha->host);
3590
3591                mutex_lock(&ha->vport_lock);
3592        }
3593        mutex_unlock(&ha->vport_lock);
3594}
3595
3596/* Stops all deferred work threads */
3597static void
3598qla2x00_destroy_deferred_work(struct qla_hw_data *ha)
3599{
3600        /* Cancel all work and destroy DPC workqueues */
3601        if (ha->dpc_lp_wq) {
3602                cancel_work_sync(&ha->idc_aen);
3603                destroy_workqueue(ha->dpc_lp_wq);
3604                ha->dpc_lp_wq = NULL;
3605        }
3606
3607        if (ha->dpc_hp_wq) {
3608                cancel_work_sync(&ha->nic_core_reset);
3609                cancel_work_sync(&ha->idc_state_handler);
3610                cancel_work_sync(&ha->nic_core_unrecoverable);
3611                destroy_workqueue(ha->dpc_hp_wq);
3612                ha->dpc_hp_wq = NULL;
3613        }
3614
3615        /* Kill the kernel thread for this host */
3616        if (ha->dpc_thread) {
3617                struct task_struct *t = ha->dpc_thread;
3618
3619                /*
3620                 * qla2xxx_wake_dpc checks for ->dpc_thread
3621                 * so we need to zero it out.
3622                 */
3623                ha->dpc_thread = NULL;
3624                kthread_stop(t);
3625        }
3626}
3627
3628static void
3629qla2x00_unmap_iobases(struct qla_hw_data *ha)
3630{
3631        if (IS_QLA82XX(ha)) {
3632
3633                iounmap((device_reg_t *)ha->nx_pcibase);
3634                if (!ql2xdbwr)
3635                        iounmap((device_reg_t *)ha->nxdb_wr_ptr);
3636        } else {
3637                if (ha->iobase)
3638                        iounmap(ha->iobase);
3639
3640                if (ha->cregbase)
3641                        iounmap(ha->cregbase);
3642
3643                if (ha->mqiobase)
3644                        iounmap(ha->mqiobase);
3645
3646                if ((IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) &&
3647                    ha->msixbase)
3648                        iounmap(ha->msixbase);
3649        }
3650}
3651
3652static void
3653qla2x00_clear_drv_active(struct qla_hw_data *ha)
3654{
3655        if (IS_QLA8044(ha)) {
3656                qla8044_idc_lock(ha);
3657                qla8044_clear_drv_active(ha);
3658                qla8044_idc_unlock(ha);
3659        } else if (IS_QLA82XX(ha)) {
3660                qla82xx_idc_lock(ha);
3661                qla82xx_clear_drv_active(ha);
3662                qla82xx_idc_unlock(ha);
3663        }
3664}
3665
3666static void
3667qla2x00_remove_one(struct pci_dev *pdev)
3668{
3669        scsi_qla_host_t *base_vha;
3670        struct qla_hw_data  *ha;
3671
3672        base_vha = pci_get_drvdata(pdev);
3673        ha = base_vha->hw;
3674        ql_log(ql_log_info, base_vha, 0xb079,
3675            "Removing driver\n");
3676        __qla_set_remove_flag(base_vha);
3677        cancel_work_sync(&ha->board_disable);
3678
3679        /*
3680         * If the PCI device is disabled then there was a PCI-disconnect and
3681         * qla2x00_disable_board_on_pci_error has taken care of most of the
3682         * resources.
3683         */
3684        if (!atomic_read(&pdev->enable_cnt)) {
3685                dma_free_coherent(&ha->pdev->dev, base_vha->gnl.size,
3686                    base_vha->gnl.l, base_vha->gnl.ldma);
3687                base_vha->gnl.l = NULL;
3688                scsi_host_put(base_vha->host);
3689                kfree(ha);
3690                pci_set_drvdata(pdev, NULL);
3691                return;
3692        }
3693        qla2x00_wait_for_hba_ready(base_vha);
3694
3695        /*
3696         * if UNLOADING flag is already set, then continue unload,
3697         * where it was set first.
3698         */
3699        if (test_and_set_bit(UNLOADING, &base_vha->dpc_flags))
3700                return;
3701
3702        if (IS_QLA25XX(ha) || IS_QLA2031(ha) || IS_QLA27XX(ha) ||
3703            IS_QLA28XX(ha)) {
3704                if (ha->flags.fw_started)
3705                        qla2x00_abort_isp_cleanup(base_vha);
3706        } else if (!IS_QLAFX00(ha)) {
3707                if (IS_QLA8031(ha)) {
3708                        ql_dbg(ql_dbg_p3p, base_vha, 0xb07e,
3709                            "Clearing fcoe driver presence.\n");
3710                        if (qla83xx_clear_drv_presence(base_vha) != QLA_SUCCESS)
3711                                ql_dbg(ql_dbg_p3p, base_vha, 0xb079,
3712                                    "Error while clearing DRV-Presence.\n");
3713                }
3714
3715                qla2x00_try_to_stop_firmware(base_vha);
3716        }
3717
3718        qla2x00_wait_for_sess_deletion(base_vha);
3719
3720        qla_nvme_delete(base_vha);
3721
3722        dma_free_coherent(&ha->pdev->dev,
3723                base_vha->gnl.size, base_vha->gnl.l, base_vha->gnl.ldma);
3724
3725        base_vha->gnl.l = NULL;
3726        qla_enode_stop(base_vha);
3727        qla_edb_stop(base_vha);
3728
3729        vfree(base_vha->scan.l);
3730
3731        if (IS_QLAFX00(ha))
3732                qlafx00_driver_shutdown(base_vha, 20);
3733
3734        qla2x00_delete_all_vps(ha, base_vha);
3735
3736        qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
3737
3738        qla2x00_dfs_remove(base_vha);
3739
3740        qla84xx_put_chip(base_vha);
3741
3742        /* Disable timer */
3743        if (base_vha->timer_active)
3744                qla2x00_stop_timer(base_vha);
3745
3746        base_vha->flags.online = 0;
3747
3748        /* free DMA memory */
3749        if (ha->exlogin_buf)
3750                qla2x00_free_exlogin_buffer(ha);
3751
3752        /* free DMA memory */
3753        if (ha->exchoffld_buf)
3754                qla2x00_free_exchoffld_buffer(ha);
3755
3756        qla2x00_destroy_deferred_work(ha);
3757
3758        qlt_remove_target(ha, base_vha);
3759
3760        qla2x00_free_sysfs_attr(base_vha, true);
3761
3762        fc_remove_host(base_vha->host);
3763
3764        scsi_remove_host(base_vha->host);
3765
3766        qla2x00_free_device(base_vha);
3767
3768        qla2x00_clear_drv_active(ha);
3769
3770        scsi_host_put(base_vha->host);
3771
3772        qla2x00_unmap_iobases(ha);
3773
3774        pci_release_selected_regions(ha->pdev, ha->bars);
3775        kfree(ha);
3776
3777        pci_disable_pcie_error_reporting(pdev);
3778
3779        pci_disable_device(pdev);
3780}
3781
3782static inline void
3783qla24xx_free_purex_list(struct purex_list *list)
3784{
3785        struct list_head *item, *next;
3786        ulong flags;
3787
3788        spin_lock_irqsave(&list->lock, flags);
3789        list_for_each_safe(item, next, &list->head) {
3790                list_del(item);
3791                kfree(list_entry(item, struct purex_item, list));
3792        }
3793        spin_unlock_irqrestore(&list->lock, flags);
3794}
3795
3796static void
3797qla2x00_free_device(scsi_qla_host_t *vha)
3798{
3799        struct qla_hw_data *ha = vha->hw;
3800
3801        qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
3802
3803        /* Disable timer */
3804        if (vha->timer_active)
3805                qla2x00_stop_timer(vha);
3806
3807        qla25xx_delete_queues(vha);
3808        vha->flags.online = 0;
3809
3810        /* turn-off interrupts on the card */
3811        if (ha->interrupts_on) {
3812                vha->flags.init_done = 0;
3813                ha->isp_ops->disable_intrs(ha);
3814        }
3815
3816        qla2x00_free_fcports(vha);
3817
3818        qla2x00_free_irqs(vha);
3819
3820        /* Flush the work queue and remove it */
3821        if (ha->wq) {
3822                flush_workqueue(ha->wq);
3823                destroy_workqueue(ha->wq);
3824                ha->wq = NULL;
3825        }
3826
3827
3828        qla24xx_free_purex_list(&vha->purex_list);
3829
3830        qla2x00_mem_free(ha);
3831
3832        qla82xx_md_free(vha);
3833
3834        qla_edif_sadb_release_free_pool(ha);
3835        qla_edif_sadb_release(ha);
3836
3837        qla2x00_free_queues(ha);
3838}
3839
3840void qla2x00_free_fcports(struct scsi_qla_host *vha)
3841{
3842        fc_port_t *fcport, *tfcport;
3843
3844        list_for_each_entry_safe(fcport, tfcport, &vha->vp_fcports, list)
3845                qla2x00_free_fcport(fcport);
3846}
3847
3848static inline void
3849qla2x00_schedule_rport_del(struct scsi_qla_host *vha, fc_port_t *fcport)
3850{
3851        int now;
3852
3853        if (!fcport->rport)
3854                return;
3855
3856        if (fcport->rport) {
3857                ql_dbg(ql_dbg_disc, fcport->vha, 0x2109,
3858                    "%s %8phN. rport %p roles %x\n",
3859                    __func__, fcport->port_name, fcport->rport,
3860                    fcport->rport->roles);
3861                fc_remote_port_delete(fcport->rport);
3862        }
3863        qlt_do_generation_tick(vha, &now);
3864}
3865
3866/*
3867 * qla2x00_mark_device_lost Updates fcport state when device goes offline.
3868 *
3869 * Input: ha = adapter block pointer.  fcport = port structure pointer.
3870 *
3871 * Return: None.
3872 *
3873 * Context:
3874 */
3875void qla2x00_mark_device_lost(scsi_qla_host_t *vha, fc_port_t *fcport,
3876    int do_login)
3877{
3878        if (IS_QLAFX00(vha->hw)) {
3879                qla2x00_set_fcport_state(fcport, FCS_DEVICE_LOST);
3880                qla2x00_schedule_rport_del(vha, fcport);
3881                return;
3882        }
3883
3884        if (atomic_read(&fcport->state) == FCS_ONLINE &&
3885            vha->vp_idx == fcport->vha->vp_idx) {
3886                qla2x00_set_fcport_state(fcport, FCS_DEVICE_LOST);
3887                qla2x00_schedule_rport_del(vha, fcport);
3888        }
3889
3890        /*
3891         * We may need to retry the login, so don't change the state of the
3892         * port but do the retries.
3893         */
3894        if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
3895                qla2x00_set_fcport_state(fcport, FCS_DEVICE_LOST);
3896
3897        if (!do_login)
3898                return;
3899
3900        set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
3901}
3902
3903void
3904qla2x00_mark_all_devices_lost(scsi_qla_host_t *vha)
3905{
3906        fc_port_t *fcport;
3907
3908        ql_dbg(ql_dbg_disc, vha, 0x20f1,
3909            "Mark all dev lost\n");
3910
3911        list_for_each_entry(fcport, &vha->vp_fcports, list) {
3912                if (fcport->loop_id != FC_NO_LOOP_ID &&
3913                    (fcport->flags & FCF_FCP2_DEVICE) &&
3914                    fcport->port_type == FCT_TARGET &&
3915                    !qla2x00_reset_active(vha)) {
3916                        ql_dbg(ql_dbg_disc, vha, 0x211a,
3917                               "Delaying session delete for FCP2 flags 0x%x port_type = 0x%x port_id=%06x %phC",
3918                               fcport->flags, fcport->port_type,
3919                               fcport->d_id.b24, fcport->port_name);
3920                        continue;
3921                }
3922                fcport->scan_state = 0;
3923                qlt_schedule_sess_for_deletion(fcport);
3924        }
3925}
3926
3927static void qla2x00_set_reserved_loop_ids(struct qla_hw_data *ha)
3928{
3929        int i;
3930
3931        if (IS_FWI2_CAPABLE(ha))
3932                return;
3933
3934        for (i = 0; i < SNS_FIRST_LOOP_ID; i++)
3935                set_bit(i, ha->loop_id_map);
3936        set_bit(MANAGEMENT_SERVER, ha->loop_id_map);
3937        set_bit(BROADCAST, ha->loop_id_map);
3938}
3939
3940/*
3941* qla2x00_mem_alloc
3942*      Allocates adapter memory.
3943*
3944* Returns:
3945*      0  = success.
3946*      !0  = failure.
3947*/
3948static int
3949qla2x00_mem_alloc(struct qla_hw_data *ha, uint16_t req_len, uint16_t rsp_len,
3950        struct req_que **req, struct rsp_que **rsp)
3951{
3952        char    name[16];
3953        int rc;
3954
3955        ha->init_cb = dma_alloc_coherent(&ha->pdev->dev, ha->init_cb_size,
3956                &ha->init_cb_dma, GFP_KERNEL);
3957        if (!ha->init_cb)
3958                goto fail;
3959
3960        rc = btree_init32(&ha->host_map);
3961        if (rc)
3962                goto fail_free_init_cb;
3963
3964        if (qlt_mem_alloc(ha) < 0)
3965                goto fail_free_btree;
3966
3967        ha->gid_list = dma_alloc_coherent(&ha->pdev->dev,
3968                qla2x00_gid_list_size(ha), &ha->gid_list_dma, GFP_KERNEL);
3969        if (!ha->gid_list)
3970                goto fail_free_tgt_mem;
3971
3972        ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
3973        if (!ha->srb_mempool)
3974                goto fail_free_gid_list;
3975
3976        if (IS_P3P_TYPE(ha) || IS_QLA27XX(ha) || (ql2xsecenable && IS_QLA28XX(ha))) {
3977                /* Allocate cache for CT6 Ctx. */
3978                if (!ctx_cachep) {
3979                        ctx_cachep = kmem_cache_create("qla2xxx_ctx",
3980                                sizeof(struct ct6_dsd), 0,
3981                                SLAB_HWCACHE_ALIGN, NULL);
3982                        if (!ctx_cachep)
3983                                goto fail_free_srb_mempool;
3984                }
3985                ha->ctx_mempool = mempool_create_slab_pool(SRB_MIN_REQ,
3986                        ctx_cachep);
3987                if (!ha->ctx_mempool)
3988                        goto fail_free_srb_mempool;
3989                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0021,
3990                    "ctx_cachep=%p ctx_mempool=%p.\n",
3991                    ctx_cachep, ha->ctx_mempool);
3992        }
3993
3994        /* Get memory for cached NVRAM */
3995        ha->nvram = kzalloc(MAX_NVRAM_SIZE, GFP_KERNEL);
3996        if (!ha->nvram)
3997                goto fail_free_ctx_mempool;
3998
3999        snprintf(name, sizeof(name), "%s_%d", QLA2XXX_DRIVER_NAME,
4000                ha->pdev->device);
4001        ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
4002                DMA_POOL_SIZE, 8, 0);
4003        if (!ha->s_dma_pool)
4004                goto fail_free_nvram;
4005
4006        ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0022,
4007            "init_cb=%p gid_list=%p, srb_mempool=%p s_dma_pool=%p.\n",
4008            ha->init_cb, ha->gid_list, ha->srb_mempool, ha->s_dma_pool);
4009
4010        if (IS_P3P_TYPE(ha) || ql2xenabledif || (IS_QLA28XX(ha) && ql2xsecenable)) {
4011                ha->dl_dma_pool = dma_pool_create(name, &ha->pdev->dev,
4012                        DSD_LIST_DMA_POOL_SIZE, 8, 0);
4013                if (!ha->dl_dma_pool) {
4014                        ql_log_pci(ql_log_fatal, ha->pdev, 0x0023,
4015                            "Failed to allocate memory for dl_dma_pool.\n");
4016                        goto fail_s_dma_pool;
4017                }
4018
4019                ha->fcp_cmnd_dma_pool = dma_pool_create(name, &ha->pdev->dev,
4020                        FCP_CMND_DMA_POOL_SIZE, 8, 0);
4021                if (!ha->fcp_cmnd_dma_pool) {
4022                        ql_log_pci(ql_log_fatal, ha->pdev, 0x0024,
4023                            "Failed to allocate memory for fcp_cmnd_dma_pool.\n");
4024                        goto fail_dl_dma_pool;
4025                }
4026
4027                if (ql2xenabledif) {
4028                        u64 bufsize = DIF_BUNDLING_DMA_POOL_SIZE;
4029                        struct dsd_dma *dsd, *nxt;
4030                        uint i;
4031                        /* Creata a DMA pool of buffers for DIF bundling */
4032                        ha->dif_bundl_pool = dma_pool_create(name,
4033                            &ha->pdev->dev, DIF_BUNDLING_DMA_POOL_SIZE, 8, 0);
4034                        if (!ha->dif_bundl_pool) {
4035                                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0024,
4036                                    "%s: failed create dif_bundl_pool\n",
4037                                    __func__);
4038                                goto fail_dif_bundl_dma_pool;
4039                        }
4040
4041                        INIT_LIST_HEAD(&ha->pool.good.head);
4042                        INIT_LIST_HEAD(&ha->pool.unusable.head);
4043                        ha->pool.good.count = 0;
4044                        ha->pool.unusable.count = 0;
4045                        for (i = 0; i < 128; i++) {
4046                                dsd = kzalloc(sizeof(*dsd), GFP_ATOMIC);
4047                                if (!dsd) {
4048                                        ql_dbg_pci(ql_dbg_init, ha->pdev,
4049                                            0xe0ee, "%s: failed alloc dsd\n",
4050                                            __func__);
4051                                        return 1;
4052                                }
4053                                ha->dif_bundle_kallocs++;
4054
4055                                dsd->dsd_addr = dma_pool_alloc(
4056                                    ha->dif_bundl_pool, GFP_ATOMIC,
4057                                    &dsd->dsd_list_dma);
4058                                if (!dsd->dsd_addr) {
4059                                        ql_dbg_pci(ql_dbg_init, ha->pdev,
4060                                            0xe0ee,
4061                                            "%s: failed alloc ->dsd_addr\n",
4062                                            __func__);
4063                                        kfree(dsd);
4064                                        ha->dif_bundle_kallocs--;
4065                                        continue;
4066                                }
4067                                ha->dif_bundle_dma_allocs++;
4068
4069                                /*
4070                                 * if DMA buffer crosses 4G boundary,
4071                                 * put it on bad list
4072                                 */
4073                                if (MSD(dsd->dsd_list_dma) ^
4074                                    MSD(dsd->dsd_list_dma + bufsize)) {
4075                                        list_add_tail(&dsd->list,
4076                                            &ha->pool.unusable.head);
4077                                        ha->pool.unusable.count++;
4078                                } else {
4079                                        list_add_tail(&dsd->list,
4080                                            &ha->pool.good.head);
4081                                        ha->pool.good.count++;
4082                                }
4083                        }
4084
4085                        /* return the good ones back to the pool */
4086                        list_for_each_entry_safe(dsd, nxt,
4087                            &ha->pool.good.head, list) {
4088                                list_del(&dsd->list);
4089                                dma_pool_free(ha->dif_bundl_pool,
4090                                    dsd->dsd_addr, dsd->dsd_list_dma);
4091                                ha->dif_bundle_dma_allocs--;
4092                                kfree(dsd);
4093                                ha->dif_bundle_kallocs--;
4094                        }
4095
4096                        ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0024,
4097                            "%s: dif dma pool (good=%u unusable=%u)\n",
4098                            __func__, ha->pool.good.count,
4099                            ha->pool.unusable.count);
4100                }
4101
4102                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0025,
4103                    "dl_dma_pool=%p fcp_cmnd_dma_pool=%p dif_bundl_pool=%p.\n",
4104                    ha->dl_dma_pool, ha->fcp_cmnd_dma_pool,
4105                    ha->dif_bundl_pool);
4106        }
4107
4108        /* Allocate memory for SNS commands */
4109        if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4110        /* Get consistent memory allocated for SNS commands */
4111                ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
4112                sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma, GFP_KERNEL);
4113                if (!ha->sns_cmd)
4114                        goto fail_dma_pool;
4115                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0026,
4116                    "sns_cmd: %p.\n", ha->sns_cmd);
4117        } else {
4118        /* Get consistent memory allocated for MS IOCB */
4119                ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
4120                        &ha->ms_iocb_dma);
4121                if (!ha->ms_iocb)
4122                        goto fail_dma_pool;
4123        /* Get consistent memory allocated for CT SNS commands */
4124                ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
4125                        sizeof(struct ct_sns_pkt), &ha->ct_sns_dma, GFP_KERNEL);
4126                if (!ha->ct_sns)
4127                        goto fail_free_ms_iocb;
4128                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0027,
4129                    "ms_iocb=%p ct_sns=%p.\n",
4130                    ha->ms_iocb, ha->ct_sns);
4131        }
4132
4133        /* Allocate memory for request ring */
4134        *req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
4135        if (!*req) {
4136                ql_log_pci(ql_log_fatal, ha->pdev, 0x0028,
4137                    "Failed to allocate memory for req.\n");
4138                goto fail_req;
4139        }
4140        (*req)->length = req_len;
4141        (*req)->ring = dma_alloc_coherent(&ha->pdev->dev,
4142                ((*req)->length + 1) * sizeof(request_t),
4143                &(*req)->dma, GFP_KERNEL);
4144        if (!(*req)->ring) {
4145                ql_log_pci(ql_log_fatal, ha->pdev, 0x0029,
4146                    "Failed to allocate memory for req_ring.\n");
4147                goto fail_req_ring;
4148        }
4149        /* Allocate memory for response ring */
4150        *rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
4151        if (!*rsp) {
4152                ql_log_pci(ql_log_fatal, ha->pdev, 0x002a,
4153                    "Failed to allocate memory for rsp.\n");
4154                goto fail_rsp;
4155        }
4156        (*rsp)->hw = ha;
4157        (*rsp)->length = rsp_len;
4158        (*rsp)->ring = dma_alloc_coherent(&ha->pdev->dev,
4159                ((*rsp)->length + 1) * sizeof(response_t),
4160                &(*rsp)->dma, GFP_KERNEL);
4161        if (!(*rsp)->ring) {
4162                ql_log_pci(ql_log_fatal, ha->pdev, 0x002b,
4163                    "Failed to allocate memory for rsp_ring.\n");
4164                goto fail_rsp_ring;
4165        }
4166        (*req)->rsp = *rsp;
4167        (*rsp)->req = *req;
4168        ql_dbg_pci(ql_dbg_init, ha->pdev, 0x002c,
4169            "req=%p req->length=%d req->ring=%p rsp=%p "
4170            "rsp->length=%d rsp->ring=%p.\n",
4171            *req, (*req)->length, (*req)->ring, *rsp, (*rsp)->length,
4172            (*rsp)->ring);
4173        /* Allocate memory for NVRAM data for vports */
4174        if (ha->nvram_npiv_size) {
4175                ha->npiv_info = kcalloc(ha->nvram_npiv_size,
4176                                        sizeof(struct qla_npiv_entry),
4177                                        GFP_KERNEL);
4178                if (!ha->npiv_info) {
4179                        ql_log_pci(ql_log_fatal, ha->pdev, 0x002d,
4180                            "Failed to allocate memory for npiv_info.\n");
4181                        goto fail_npiv_info;
4182                }
4183        } else
4184                ha->npiv_info = NULL;
4185
4186        /* Get consistent memory allocated for EX-INIT-CB. */
4187        if (IS_CNA_CAPABLE(ha) || IS_QLA2031(ha) || IS_QLA27XX(ha) ||
4188            IS_QLA28XX(ha)) {
4189                ha->ex_init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
4190                    &ha->ex_init_cb_dma);
4191                if (!ha->ex_init_cb)
4192                        goto fail_ex_init_cb;
4193                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x002e,
4194                    "ex_init_cb=%p.\n", ha->ex_init_cb);
4195        }
4196
4197        /* Get consistent memory allocated for Special Features-CB. */
4198        if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
4199                ha->sf_init_cb = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL,
4200                                                &ha->sf_init_cb_dma);
4201                if (!ha->sf_init_cb)
4202                        goto fail_sf_init_cb;
4203                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0199,
4204                           "sf_init_cb=%p.\n", ha->sf_init_cb);
4205        }
4206
4207        INIT_LIST_HEAD(&ha->gbl_dsd_list);
4208
4209        /* Get consistent memory allocated for Async Port-Database. */
4210        if (!IS_FWI2_CAPABLE(ha)) {
4211                ha->async_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
4212                        &ha->async_pd_dma);
4213                if (!ha->async_pd)
4214                        goto fail_async_pd;
4215                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x002f,
4216                    "async_pd=%p.\n", ha->async_pd);
4217        }
4218
4219        INIT_LIST_HEAD(&ha->vp_list);
4220
4221        /* Allocate memory for our loop_id bitmap */
4222        ha->loop_id_map = kcalloc(BITS_TO_LONGS(LOOPID_MAP_SIZE),
4223                                  sizeof(long),
4224                                  GFP_KERNEL);
4225        if (!ha->loop_id_map)
4226                goto fail_loop_id_map;
4227        else {
4228                qla2x00_set_reserved_loop_ids(ha);
4229                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0123,
4230                    "loop_id_map=%p.\n", ha->loop_id_map);
4231        }
4232
4233        ha->sfp_data = dma_alloc_coherent(&ha->pdev->dev,
4234            SFP_DEV_SIZE, &ha->sfp_data_dma, GFP_KERNEL);
4235        if (!ha->sfp_data) {
4236                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x011b,
4237                    "Unable to allocate memory for SFP read-data.\n");
4238                goto fail_sfp_data;
4239        }
4240
4241        ha->flt = dma_alloc_coherent(&ha->pdev->dev,
4242            sizeof(struct qla_flt_header) + FLT_REGIONS_SIZE, &ha->flt_dma,
4243            GFP_KERNEL);
4244        if (!ha->flt) {
4245                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x011b,
4246                    "Unable to allocate memory for FLT.\n");
4247                goto fail_flt_buffer;
4248        }
4249
4250        /* allocate the purex dma pool */
4251        ha->purex_dma_pool = dma_pool_create(name, &ha->pdev->dev,
4252            MAX_PAYLOAD, 8, 0);
4253
4254        if (!ha->purex_dma_pool) {
4255                ql_dbg_pci(ql_dbg_init, ha->pdev, 0x011b,
4256                    "Unable to allocate purex_dma_pool.\n");
4257                goto fail_flt;
4258        }
4259
4260        ha->elsrej.size = sizeof(struct fc_els_ls_rjt) + 16;
4261        ha->elsrej.c = dma_alloc_coherent(&ha->pdev->dev,
4262            ha->elsrej.size, &ha->elsrej.cdma, GFP_KERNEL);
4263
4264        if (!ha->elsrej.c) {
4265                ql_dbg_pci(ql_dbg_init, ha->pdev, 0xffff,
4266                    "Alloc failed for els reject cmd.\n");
4267                goto fail_elsrej;
4268        }
4269        ha->elsrej.c->er_cmd = ELS_LS_RJT;
4270        ha->elsrej.c->er_reason = ELS_RJT_LOGIC;
4271        ha->elsrej.c->er_explan = ELS_EXPL_UNAB_DATA;
4272        return 0;
4273
4274fail_elsrej:
4275        dma_pool_destroy(ha->purex_dma_pool);
4276fail_flt:
4277        dma_free_coherent(&ha->pdev->dev, SFP_DEV_SIZE,
4278            ha->flt, ha->flt_dma);
4279
4280fail_flt_buffer:
4281        dma_free_coherent(&ha->pdev->dev, SFP_DEV_SIZE,
4282            ha->sfp_data, ha->sfp_data_dma);
4283fail_sfp_data:
4284        kfree(ha->loop_id_map);
4285fail_loop_id_map:
4286        dma_pool_free(ha->s_dma_pool, ha->async_pd, ha->async_pd_dma);
4287fail_async_pd:
4288        dma_pool_free(ha->s_dma_pool, ha->sf_init_cb, ha->sf_init_cb_dma);
4289fail_sf_init_cb:
4290        dma_pool_free(ha->s_dma_pool, ha->ex_init_cb, ha->ex_init_cb_dma);
4291fail_ex_init_cb:
4292        kfree(ha->npiv_info);
4293fail_npiv_info:
4294        dma_free_coherent(&ha->pdev->dev, ((*rsp)->length + 1) *
4295                sizeof(response_t), (*rsp)->ring, (*rsp)->dma);
4296        (*rsp)->ring = NULL;
4297        (*rsp)->dma = 0;
4298fail_rsp_ring:
4299        kfree(*rsp);
4300        *rsp = NULL;
4301fail_rsp:
4302        dma_free_coherent(&ha->pdev->dev, ((*req)->length + 1) *
4303                sizeof(request_t), (*req)->ring, (*req)->dma);
4304        (*req)->ring = NULL;
4305        (*req)->dma = 0;
4306fail_req_ring:
4307        kfree(*req);
4308        *req = NULL;
4309fail_req:
4310        dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
4311                ha->ct_sns, ha->ct_sns_dma);
4312        ha->ct_sns = NULL;
4313        ha->ct_sns_dma = 0;
4314fail_free_ms_iocb:
4315        dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
4316        ha->ms_iocb = NULL;
4317        ha->ms_iocb_dma = 0;
4318
4319        if (ha->sns_cmd)
4320                dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
4321                    ha->sns_cmd, ha->sns_cmd_dma);
4322fail_dma_pool:
4323        if (ql2xenabledif) {
4324                struct dsd_dma *dsd, *nxt;
4325
4326                list_for_each_entry_safe(dsd, nxt, &ha->pool.unusable.head,
4327                    list) {
4328                        list_del(&dsd->list);
4329                        dma_pool_free(ha->dif_bundl_pool, dsd->dsd_addr,
4330                            dsd->dsd_list_dma);
4331                        ha->dif_bundle_dma_allocs--;
4332                        kfree(dsd);
4333                        ha->dif_bundle_kallocs--;
4334                        ha->pool.unusable.count--;
4335                }
4336                dma_pool_destroy(ha->dif_bundl_pool);
4337                ha->dif_bundl_pool = NULL;
4338        }
4339
4340fail_dif_bundl_dma_pool:
4341        if (IS_QLA82XX(ha) || ql2xenabledif) {
4342                dma_pool_destroy(ha->fcp_cmnd_dma_pool);
4343                ha->fcp_cmnd_dma_pool = NULL;
4344        }
4345fail_dl_dma_pool:
4346        if (IS_QLA82XX(ha) || ql2xenabledif) {
4347                dma_pool_destroy(ha->dl_dma_pool);
4348                ha->dl_dma_pool = NULL;
4349        }
4350fail_s_dma_pool:
4351        dma_pool_destroy(ha->s_dma_pool);
4352        ha->s_dma_pool = NULL;
4353fail_free_nvram:
4354        kfree(ha->nvram);
4355        ha->nvram = NULL;
4356fail_free_ctx_mempool:
4357        mempool_destroy(ha->ctx_mempool);
4358        ha->ctx_mempool = NULL;
4359fail_free_srb_mempool:
4360        mempool_destroy(ha->srb_mempool);
4361        ha->srb_mempool = NULL;
4362fail_free_gid_list:
4363        dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
4364        ha->gid_list,
4365        ha->gid_list_dma);
4366        ha->gid_list = NULL;
4367        ha->gid_list_dma = 0;
4368fail_free_tgt_mem:
4369        qlt_mem_free(ha);
4370fail_free_btree:
4371        btree_destroy32(&ha->host_map);
4372fail_free_init_cb:
4373        dma_free_coherent(&ha->pdev->dev, ha->init_cb_size, ha->init_cb,
4374        ha->init_cb_dma);
4375        ha->init_cb = NULL;
4376        ha->init_cb_dma = 0;
4377fail:
4378        ql_log(ql_log_fatal, NULL, 0x0030,
4379            "Memory allocation failure.\n");
4380        return -ENOMEM;
4381}
4382
4383int
4384qla2x00_set_exlogins_buffer(scsi_qla_host_t *vha)
4385{
4386        int rval;
4387        uint16_t        size, max_cnt;
4388        uint32_t temp;
4389        struct qla_hw_data *ha = vha->hw;
4390
4391        /* Return if we don't need to alloacate any extended logins */
4392        if (ql2xexlogins <= MAX_FIBRE_DEVICES_2400)
4393                return QLA_SUCCESS;
4394
4395        if (!IS_EXLOGIN_OFFLD_CAPABLE(ha))
4396                return QLA_SUCCESS;
4397
4398        ql_log(ql_log_info, vha, 0xd021, "EXLOGIN count: %d.\n", ql2xexlogins);
4399        max_cnt = 0;
4400        rval = qla_get_exlogin_status(vha, &size, &max_cnt);
4401        if (rval != QLA_SUCCESS) {
4402                ql_log_pci(ql_log_fatal, ha->pdev, 0xd029,
4403                    "Failed to get exlogin status.\n");
4404                return rval;
4405        }
4406
4407        temp = (ql2xexlogins > max_cnt) ? max_cnt : ql2xexlogins;
4408        temp *= size;
4409
4410        if (temp != ha->exlogin_size) {
4411                qla2x00_free_exlogin_buffer(ha);
4412                ha->exlogin_size = temp;
4413
4414                ql_log(ql_log_info, vha, 0xd024,
4415                    "EXLOGIN: max_logins=%d, portdb=0x%x, total=%d.\n",
4416                    max_cnt, size, temp);
4417
4418                ql_log(ql_log_info, vha, 0xd025,
4419                    "EXLOGIN: requested size=0x%x\n", ha->exlogin_size);
4420
4421                /* Get consistent memory for extended logins */
4422                ha->exlogin_buf = dma_alloc_coherent(&ha->pdev->dev,
4423                        ha->exlogin_size, &ha->exlogin_buf_dma, GFP_KERNEL);
4424                if (!ha->exlogin_buf) {
4425                        ql_log_pci(ql_log_fatal, ha->pdev, 0xd02a,
4426                    "Failed to allocate memory for exlogin_buf_dma.\n");
4427                        return -ENOMEM;
4428                }
4429        }
4430
4431        /* Now configure the dma buffer */
4432        rval = qla_set_exlogin_mem_cfg(vha, ha->exlogin_buf_dma);
4433        if (rval) {
4434                ql_log(ql_log_fatal, vha, 0xd033,
4435                    "Setup extended login buffer  ****FAILED****.\n");
4436                qla2x00_free_exlogin_buffer(ha);
4437        }
4438
4439        return rval;
4440}
4441
4442/*
4443* qla2x00_free_exlogin_buffer
4444*
4445* Input:
4446*       ha = adapter block pointer
4447*/
4448void
4449qla2x00_free_exlogin_buffer(struct qla_hw_data *ha)
4450{
4451        if (ha->exlogin_buf) {
4452                dma_free_coherent(&ha->pdev->dev, ha->exlogin_size,
4453                    ha->exlogin_buf, ha->exlogin_buf_dma);
4454                ha->exlogin_buf = NULL;
4455                ha->exlogin_size = 0;
4456        }
4457}
4458
4459static void
4460qla2x00_number_of_exch(scsi_qla_host_t *vha, u32 *ret_cnt, u16 max_cnt)
4461{
4462        u32 temp;
4463        struct init_cb_81xx *icb = (struct init_cb_81xx *)&vha->hw->init_cb;
4464        *ret_cnt = FW_DEF_EXCHANGES_CNT;
4465
4466        if (max_cnt > vha->hw->max_exchg)
4467                max_cnt = vha->hw->max_exchg;
4468
4469        if (qla_ini_mode_enabled(vha)) {
4470                if (vha->ql2xiniexchg > max_cnt)
4471                        vha->ql2xiniexchg = max_cnt;
4472
4473                if (vha->ql2xiniexchg > FW_DEF_EXCHANGES_CNT)
4474                        *ret_cnt = vha->ql2xiniexchg;
4475
4476        } else if (qla_tgt_mode_enabled(vha)) {
4477                if (vha->ql2xexchoffld > max_cnt) {
4478                        vha->ql2xexchoffld = max_cnt;
4479                        icb->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
4480                }
4481
4482                if (vha->ql2xexchoffld > FW_DEF_EXCHANGES_CNT)
4483                        *ret_cnt = vha->ql2xexchoffld;
4484        } else if (qla_dual_mode_enabled(vha)) {
4485                temp = vha->ql2xiniexchg + vha->ql2xexchoffld;
4486                if (temp > max_cnt) {
4487                        vha->ql2xiniexchg -= (temp - max_cnt)/2;
4488                        vha->ql2xexchoffld -= (((temp - max_cnt)/2) + 1);
4489                        temp = max_cnt;
4490                        icb->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
4491                }
4492
4493                if (temp > FW_DEF_EXCHANGES_CNT)
4494                        *ret_cnt = temp;
4495        }
4496}
4497
4498int
4499qla2x00_set_exchoffld_buffer(scsi_qla_host_t *vha)
4500{
4501        int rval;
4502        u16     size, max_cnt;
4503        u32 actual_cnt, totsz;
4504        struct qla_hw_data *ha = vha->hw;
4505
4506        if (!ha->flags.exchoffld_enabled)
4507                return QLA_SUCCESS;
4508
4509        if (!IS_EXCHG_OFFLD_CAPABLE(ha))
4510                return QLA_SUCCESS;
4511
4512        max_cnt = 0;
4513        rval = qla_get_exchoffld_status(vha, &size, &max_cnt);
4514        if (rval != QLA_SUCCESS) {
4515                ql_log_pci(ql_log_fatal, ha->pdev, 0xd012,
4516                    "Failed to get exlogin status.\n");
4517                return rval;
4518        }
4519
4520        qla2x00_number_of_exch(vha, &actual_cnt, max_cnt);
4521        ql_log(ql_log_info, vha, 0xd014,
4522            "Actual exchange offload count: %d.\n", actual_cnt);
4523
4524        totsz = actual_cnt * size;
4525
4526        if (totsz != ha->exchoffld_size) {
4527                qla2x00_free_exchoffld_buffer(ha);
4528                if (actual_cnt <= FW_DEF_EXCHANGES_CNT) {
4529                        ha->exchoffld_size = 0;
4530                        ha->flags.exchoffld_enabled = 0;
4531                        return QLA_SUCCESS;
4532                }
4533
4534                ha->exchoffld_size = totsz;
4535
4536                ql_log(ql_log_info, vha, 0xd016,
4537                    "Exchange offload: max_count=%d, actual count=%d entry sz=0x%x, total sz=0x%x\n",
4538                    max_cnt, actual_cnt, size, totsz);
4539
4540                ql_log(ql_log_info, vha, 0xd017,
4541                    "Exchange Buffers requested size = 0x%x\n",
4542                    ha->exchoffld_size);
4543
4544                /* Get consistent memory for extended logins */
4545                ha->exchoffld_buf = dma_alloc_coherent(&ha->pdev->dev,
4546                        ha->exchoffld_size, &ha->exchoffld_buf_dma, GFP_KERNEL);
4547                if (!ha->exchoffld_buf) {
4548                        ql_log_pci(ql_log_fatal, ha->pdev, 0xd013,
4549                        "Failed to allocate memory for Exchange Offload.\n");
4550
4551                        if (ha->max_exchg >
4552                            (FW_DEF_EXCHANGES_CNT + REDUCE_EXCHANGES_CNT)) {
4553                                ha->max_exchg -= REDUCE_EXCHANGES_CNT;
4554                        } else if (ha->max_exchg >
4555                            (FW_DEF_EXCHANGES_CNT + 512)) {
4556                                ha->max_exchg -= 512;
4557                        } else {
4558                                ha->flags.exchoffld_enabled = 0;
4559                                ql_log_pci(ql_log_fatal, ha->pdev, 0xd013,
4560                                    "Disabling Exchange offload due to lack of memory\n");
4561                        }
4562                        ha->exchoffld_size = 0;
4563
4564                        return -ENOMEM;
4565                }
4566        } else if (!ha->exchoffld_buf || (actual_cnt <= FW_DEF_EXCHANGES_CNT)) {
4567                /* pathological case */
4568                qla2x00_free_exchoffld_buffer(ha);
4569                ha->exchoffld_size = 0;
4570                ha->flags.exchoffld_enabled = 0;
4571                ql_log(ql_log_info, vha, 0xd016,
4572                    "Exchange offload not enable: offld size=%d, actual count=%d entry sz=0x%x, total sz=0x%x.\n",
4573                    ha->exchoffld_size, actual_cnt, size, totsz);
4574                return 0;
4575        }
4576
4577        /* Now configure the dma buffer */
4578        rval = qla_set_exchoffld_mem_cfg(vha);
4579        if (rval) {
4580                ql_log(ql_log_fatal, vha, 0xd02e,
4581                    "Setup exchange offload buffer ****FAILED****.\n");
4582                qla2x00_free_exchoffld_buffer(ha);
4583        } else {
4584                /* re-adjust number of target exchange */
4585                struct init_cb_81xx *icb = (struct init_cb_81xx *)ha->init_cb;
4586
4587                if (qla_ini_mode_enabled(vha))
4588                        icb->exchange_count = 0;
4589                else
4590                        icb->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
4591        }
4592
4593        return rval;
4594}
4595
4596/*
4597* qla2x00_free_exchoffld_buffer
4598*
4599* Input:
4600*       ha = adapter block pointer
4601*/
4602void
4603qla2x00_free_exchoffld_buffer(struct qla_hw_data *ha)
4604{
4605        if (ha->exchoffld_buf) {
4606                dma_free_coherent(&ha->pdev->dev, ha->exchoffld_size,
4607                    ha->exchoffld_buf, ha->exchoffld_buf_dma);
4608                ha->exchoffld_buf = NULL;
4609                ha->exchoffld_size = 0;
4610        }
4611}
4612
4613/*
4614* qla2x00_free_fw_dump
4615*       Frees fw dump stuff.
4616*
4617* Input:
4618*       ha = adapter block pointer
4619*/
4620static void
4621qla2x00_free_fw_dump(struct qla_hw_data *ha)
4622{
4623        struct fwdt *fwdt = ha->fwdt;
4624        uint j;
4625
4626        if (ha->fce)
4627                dma_free_coherent(&ha->pdev->dev,
4628                    FCE_SIZE, ha->fce, ha->fce_dma);
4629
4630        if (ha->eft)
4631                dma_free_coherent(&ha->pdev->dev,
4632                    EFT_SIZE, ha->eft, ha->eft_dma);
4633
4634        vfree(ha->fw_dump);
4635
4636        ha->fce = NULL;
4637        ha->fce_dma = 0;
4638        ha->flags.fce_enabled = 0;
4639        ha->eft = NULL;
4640        ha->eft_dma = 0;
4641        ha->fw_dumped = false;
4642        ha->fw_dump_cap_flags = 0;
4643        ha->fw_dump_reading = 0;
4644        ha->fw_dump = NULL;
4645        ha->fw_dump_len = 0;
4646
4647        for (j = 0; j < 2; j++, fwdt++) {
4648                vfree(fwdt->template);
4649                fwdt->template = NULL;
4650                fwdt->length = 0;
4651        }
4652}
4653
4654/*
4655* qla2x00_mem_free
4656*      Frees all adapter allocated memory.
4657*
4658* Input:
4659*      ha = adapter block pointer.
4660*/
4661static void
4662qla2x00_mem_free(struct qla_hw_data *ha)
4663{
4664        qla2x00_free_fw_dump(ha);
4665
4666        if (ha->mctp_dump)
4667                dma_free_coherent(&ha->pdev->dev, MCTP_DUMP_SIZE, ha->mctp_dump,
4668                    ha->mctp_dump_dma);
4669        ha->mctp_dump = NULL;
4670
4671        mempool_destroy(ha->srb_mempool);
4672        ha->srb_mempool = NULL;
4673
4674        if (ha->dcbx_tlv)
4675                dma_free_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
4676                    ha->dcbx_tlv, ha->dcbx_tlv_dma);
4677        ha->dcbx_tlv = NULL;
4678
4679        if (ha->xgmac_data)
4680                dma_free_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
4681                    ha->xgmac_data, ha->xgmac_data_dma);
4682        ha->xgmac_data = NULL;
4683
4684        if (ha->sns_cmd)
4685                dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
4686                ha->sns_cmd, ha->sns_cmd_dma);
4687        ha->sns_cmd = NULL;
4688        ha->sns_cmd_dma = 0;
4689
4690        if (ha->ct_sns)
4691                dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
4692                ha->ct_sns, ha->ct_sns_dma);
4693        ha->ct_sns = NULL;
4694        ha->ct_sns_dma = 0;
4695
4696        if (ha->sfp_data)
4697                dma_free_coherent(&ha->pdev->dev, SFP_DEV_SIZE, ha->sfp_data,
4698                    ha->sfp_data_dma);
4699        ha->sfp_data = NULL;
4700
4701        if (ha->flt)
4702                dma_free_coherent(&ha->pdev->dev,
4703                    sizeof(struct qla_flt_header) + FLT_REGIONS_SIZE,
4704                    ha->flt, ha->flt_dma);
4705        ha->flt = NULL;
4706        ha->flt_dma = 0;
4707
4708        if (ha->ms_iocb)
4709                dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
4710        ha->ms_iocb = NULL;
4711        ha->ms_iocb_dma = 0;
4712
4713        if (ha->sf_init_cb)
4714                dma_pool_free(ha->s_dma_pool,
4715                              ha->sf_init_cb, ha->sf_init_cb_dma);
4716
4717        if (ha->ex_init_cb)
4718                dma_pool_free(ha->s_dma_pool,
4719                        ha->ex_init_cb, ha->ex_init_cb_dma);
4720        ha->ex_init_cb = NULL;
4721        ha->ex_init_cb_dma = 0;
4722
4723        if (ha->async_pd)
4724                dma_pool_free(ha->s_dma_pool, ha->async_pd, ha->async_pd_dma);
4725        ha->async_pd = NULL;
4726        ha->async_pd_dma = 0;
4727
4728        dma_pool_destroy(ha->s_dma_pool);
4729        ha->s_dma_pool = NULL;
4730
4731        if (ha->gid_list)
4732                dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
4733                ha->gid_list, ha->gid_list_dma);
4734        ha->gid_list = NULL;
4735        ha->gid_list_dma = 0;
4736
4737        if (IS_QLA82XX(ha)) {
4738                if (!list_empty(&ha->gbl_dsd_list)) {
4739                        struct dsd_dma *dsd_ptr, *tdsd_ptr;
4740
4741                        /* clean up allocated prev pool */
4742                        list_for_each_entry_safe(dsd_ptr,
4743                                tdsd_ptr, &ha->gbl_dsd_list, list) {
4744                                dma_pool_free(ha->dl_dma_pool,
4745                                dsd_ptr->dsd_addr, dsd_ptr->dsd_list_dma);
4746                                list_del(&dsd_ptr->list);
4747                                kfree(dsd_ptr);
4748                        }
4749                }
4750        }
4751
4752        dma_pool_destroy(ha->dl_dma_pool);
4753        ha->dl_dma_pool = NULL;
4754
4755        dma_pool_destroy(ha->fcp_cmnd_dma_pool);
4756        ha->fcp_cmnd_dma_pool = NULL;
4757
4758        mempool_destroy(ha->ctx_mempool);
4759        ha->ctx_mempool = NULL;
4760
4761        if (ql2xenabledif && ha->dif_bundl_pool) {
4762                struct dsd_dma *dsd, *nxt;
4763
4764                list_for_each_entry_safe(dsd, nxt, &ha->pool.unusable.head,
4765                                         list) {
4766                        list_del(&dsd->list);
4767                        dma_pool_free(ha->dif_bundl_pool, dsd->dsd_addr,
4768                                      dsd->dsd_list_dma);
4769                        ha->dif_bundle_dma_allocs--;
4770                        kfree(dsd);
4771                        ha->dif_bundle_kallocs--;
4772                        ha->pool.unusable.count--;
4773                }
4774                list_for_each_entry_safe(dsd, nxt, &ha->pool.good.head, list) {
4775                        list_del(&dsd->list);
4776                        dma_pool_free(ha->dif_bundl_pool, dsd->dsd_addr,
4777                                      dsd->dsd_list_dma);
4778                        ha->dif_bundle_dma_allocs--;
4779                        kfree(dsd);
4780                        ha->dif_bundle_kallocs--;
4781                }
4782        }
4783
4784        dma_pool_destroy(ha->dif_bundl_pool);
4785        ha->dif_bundl_pool = NULL;
4786
4787        qlt_mem_free(ha);
4788        qla_remove_hostmap(ha);
4789
4790        if (ha->init_cb)
4791                dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
4792                        ha->init_cb, ha->init_cb_dma);
4793
4794        dma_pool_destroy(ha->purex_dma_pool);
4795        ha->purex_dma_pool = NULL;
4796
4797        if (ha->elsrej.c) {
4798                dma_free_coherent(&ha->pdev->dev, ha->elsrej.size,
4799                    ha->elsrej.c, ha->elsrej.cdma);
4800                ha->elsrej.c = NULL;
4801        }
4802
4803        ha->init_cb = NULL;
4804        ha->init_cb_dma = 0;
4805
4806        vfree(ha->optrom_buffer);
4807        ha->optrom_buffer = NULL;
4808        kfree(ha->nvram);
4809        ha->nvram = NULL;
4810        kfree(ha->npiv_info);
4811        ha->npiv_info = NULL;
4812        kfree(ha->swl);
4813        ha->swl = NULL;
4814        kfree(ha->loop_id_map);
4815        ha->sf_init_cb = NULL;
4816        ha->sf_init_cb_dma = 0;
4817        ha->loop_id_map = NULL;
4818}
4819
4820struct scsi_qla_host *qla2x00_create_host(struct scsi_host_template *sht,
4821                                                struct qla_hw_data *ha)
4822{
4823        struct Scsi_Host *host;
4824        struct scsi_qla_host *vha = NULL;
4825
4826        host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
4827        if (!host) {
4828                ql_log_pci(ql_log_fatal, ha->pdev, 0x0107,
4829                    "Failed to allocate host from the scsi layer, aborting.\n");
4830                return NULL;
4831        }
4832
4833        /* Clear our data area */
4834        vha = shost_priv(host);
4835        memset(vha, 0, sizeof(scsi_qla_host_t));
4836
4837        vha->host = host;
4838        vha->host_no = host->host_no;
4839        vha->hw = ha;
4840
4841        vha->qlini_mode = ql2x_ini_mode;
4842        vha->ql2xexchoffld = ql2xexchoffld;
4843        vha->ql2xiniexchg = ql2xiniexchg;
4844
4845        INIT_LIST_HEAD(&vha->vp_fcports);
4846        INIT_LIST_HEAD(&vha->work_list);
4847        INIT_LIST_HEAD(&vha->list);
4848        INIT_LIST_HEAD(&vha->qla_cmd_list);
4849        INIT_LIST_HEAD(&vha->qla_sess_op_cmd_list);
4850        INIT_LIST_HEAD(&vha->logo_list);
4851        INIT_LIST_HEAD(&vha->plogi_ack_list);
4852        INIT_LIST_HEAD(&vha->qp_list);
4853        INIT_LIST_HEAD(&vha->gnl.fcports);
4854        INIT_LIST_HEAD(&vha->gpnid_list);
4855        INIT_WORK(&vha->iocb_work, qla2x00_iocb_work_fn);
4856
4857        INIT_LIST_HEAD(&vha->purex_list.head);
4858        spin_lock_init(&vha->purex_list.lock);
4859
4860        spin_lock_init(&vha->work_lock);
4861        spin_lock_init(&vha->cmd_list_lock);
4862        init_waitqueue_head(&vha->fcport_waitQ);
4863        init_waitqueue_head(&vha->vref_waitq);
4864        qla_enode_init(vha);
4865        qla_edb_init(vha);
4866
4867
4868        vha->gnl.size = sizeof(struct get_name_list_extended) *
4869                        (ha->max_loop_id + 1);
4870        vha->gnl.l = dma_alloc_coherent(&ha->pdev->dev,
4871            vha->gnl.size, &vha->gnl.ldma, GFP_KERNEL);
4872        if (!vha->gnl.l) {
4873                ql_log(ql_log_fatal, vha, 0xd04a,
4874                    "Alloc failed for name list.\n");
4875                scsi_host_put(vha->host);
4876                return NULL;
4877        }
4878
4879        /* todo: what about ext login? */
4880        vha->scan.size = ha->max_fibre_devices * sizeof(struct fab_scan_rp);
4881        vha->scan.l = vmalloc(vha->scan.size);
4882        if (!vha->scan.l) {
4883                ql_log(ql_log_fatal, vha, 0xd04a,
4884                    "Alloc failed for scan database.\n");
4885                dma_free_coherent(&ha->pdev->dev, vha->gnl.size,
4886                    vha->gnl.l, vha->gnl.ldma);
4887                vha->gnl.l = NULL;
4888                scsi_host_put(vha->host);
4889                return NULL;
4890        }
4891        INIT_DELAYED_WORK(&vha->scan.scan_work, qla_scan_work_fn);
4892
4893        sprintf(vha->host_str, "%s_%lu", QLA2XXX_DRIVER_NAME, vha->host_no);
4894        ql_dbg(ql_dbg_init, vha, 0x0041,
4895            "Allocated the host=%p hw=%p vha=%p dev_name=%s",
4896            vha->host, vha->hw, vha,
4897            dev_name(&(ha->pdev->dev)));
4898
4899        return vha;
4900}
4901
4902struct qla_work_evt *
4903qla2x00_alloc_work(struct scsi_qla_host *vha, enum qla_work_type type)
4904{
4905        struct qla_work_evt *e;
4906        uint8_t bail;
4907
4908        if (test_bit(UNLOADING, &vha->dpc_flags))
4909                return NULL;
4910
4911        QLA_VHA_MARK_BUSY(vha, bail);
4912        if (bail)
4913                return NULL;
4914
4915        e = kzalloc(sizeof(struct qla_work_evt), GFP_ATOMIC);
4916        if (!e) {
4917                QLA_VHA_MARK_NOT_BUSY(vha);
4918                return NULL;
4919        }
4920
4921        INIT_LIST_HEAD(&e->list);
4922        e->type = type;
4923        e->flags = QLA_EVT_FLAG_FREE;
4924        return e;
4925}
4926
4927int
4928qla2x00_post_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
4929{
4930        unsigned long flags;
4931        bool q = false;
4932
4933        spin_lock_irqsave(&vha->work_lock, flags);
4934        list_add_tail(&e->list, &vha->work_list);
4935
4936        if (!test_and_set_bit(IOCB_WORK_ACTIVE, &vha->dpc_flags))
4937                q = true;
4938
4939        spin_unlock_irqrestore(&vha->work_lock, flags);
4940
4941        if (q)
4942                queue_work(vha->hw->wq, &vha->iocb_work);
4943
4944        return QLA_SUCCESS;
4945}
4946
4947int
4948qla2x00_post_aen_work(struct scsi_qla_host *vha, enum fc_host_event_code code,
4949    u32 data)
4950{
4951        struct qla_work_evt *e;
4952
4953        e = qla2x00_alloc_work(vha, QLA_EVT_AEN);
4954        if (!e)
4955                return QLA_FUNCTION_FAILED;
4956
4957        e->u.aen.code = code;
4958        e->u.aen.data = data;
4959        return qla2x00_post_work(vha, e);
4960}
4961
4962int
4963qla2x00_post_idc_ack_work(struct scsi_qla_host *vha, uint16_t *mb)
4964{
4965        struct qla_work_evt *e;
4966
4967        e = qla2x00_alloc_work(vha, QLA_EVT_IDC_ACK);
4968        if (!e)
4969                return QLA_FUNCTION_FAILED;
4970
4971        memcpy(e->u.idc_ack.mb, mb, QLA_IDC_ACK_REGS * sizeof(uint16_t));
4972        return qla2x00_post_work(vha, e);
4973}
4974
4975#define qla2x00_post_async_work(name, type)     \
4976int qla2x00_post_async_##name##_work(           \
4977    struct scsi_qla_host *vha,                  \
4978    fc_port_t *fcport, uint16_t *data)          \
4979{                                               \
4980        struct qla_work_evt *e;                 \
4981                                                \
4982        e = qla2x00_alloc_work(vha, type);      \
4983        if (!e)                                 \
4984                return QLA_FUNCTION_FAILED;     \
4985                                                \
4986        e->u.logio.fcport = fcport;             \
4987        if (data) {                             \
4988                e->u.logio.data[0] = data[0];   \
4989                e->u.logio.data[1] = data[1];   \
4990        }                                       \
4991        fcport->flags |= FCF_ASYNC_ACTIVE;      \
4992        return qla2x00_post_work(vha, e);       \
4993}
4994
4995qla2x00_post_async_work(login, QLA_EVT_ASYNC_LOGIN);
4996qla2x00_post_async_work(logout, QLA_EVT_ASYNC_LOGOUT);
4997qla2x00_post_async_work(adisc, QLA_EVT_ASYNC_ADISC);
4998qla2x00_post_async_work(prlo, QLA_EVT_ASYNC_PRLO);
4999qla2x00_post_async_work(prlo_done, QLA_EVT_ASYNC_PRLO_DONE);
5000
5001int
5002qla2x00_post_uevent_work(struct scsi_qla_host *vha, u32 code)
5003{
5004        struct qla_work_evt *e;
5005
5006        e = qla2x00_alloc_work(vha, QLA_EVT_UEVENT);
5007        if (!e)
5008                return QLA_FUNCTION_FAILED;
5009
5010        e->u.uevent.code = code;
5011        return qla2x00_post_work(vha, e);
5012}
5013
5014static void
5015qla2x00_uevent_emit(struct scsi_qla_host *vha, u32 code)
5016{
5017        char event_string[40];
5018        char *envp[] = { event_string, NULL };
5019
5020        switch (code) {
5021        case QLA_UEVENT_CODE_FW_DUMP:
5022                snprintf(event_string, sizeof(event_string), "FW_DUMP=%lu",
5023                    vha->host_no);
5024                break;
5025        default:
5026                /* do nothing */
5027                break;
5028        }
5029        kobject_uevent_env(&vha->hw->pdev->dev.kobj, KOBJ_CHANGE, envp);
5030}
5031
5032int
5033qlafx00_post_aenfx_work(struct scsi_qla_host *vha,  uint32_t evtcode,
5034                        uint32_t *data, int cnt)
5035{
5036        struct qla_work_evt *e;
5037
5038        e = qla2x00_alloc_work(vha, QLA_EVT_AENFX);
5039        if (!e)
5040                return QLA_FUNCTION_FAILED;
5041
5042        e->u.aenfx.evtcode = evtcode;
5043        e->u.aenfx.count = cnt;
5044        memcpy(e->u.aenfx.mbx, data, sizeof(*data) * cnt);
5045        return qla2x00_post_work(vha, e);
5046}
5047
5048void qla24xx_sched_upd_fcport(fc_port_t *fcport)
5049{
5050        unsigned long flags;
5051
5052        if (IS_SW_RESV_ADDR(fcport->d_id))
5053                return;
5054
5055        spin_lock_irqsave(&fcport->vha->work_lock, flags);
5056        if (fcport->disc_state == DSC_UPD_FCPORT) {
5057                spin_unlock_irqrestore(&fcport->vha->work_lock, flags);
5058                return;
5059        }
5060        fcport->jiffies_at_registration = jiffies;
5061        fcport->sec_since_registration = 0;
5062        fcport->next_disc_state = DSC_DELETED;
5063        qla2x00_set_fcport_disc_state(fcport, DSC_UPD_FCPORT);
5064        spin_unlock_irqrestore(&fcport->vha->work_lock, flags);
5065
5066        queue_work(system_unbound_wq, &fcport->reg_work);
5067}
5068
5069static
5070void qla24xx_create_new_sess(struct scsi_qla_host *vha, struct qla_work_evt *e)
5071{
5072        unsigned long flags;
5073        fc_port_t *fcport =  NULL, *tfcp;
5074        struct qlt_plogi_ack_t *pla =
5075            (struct qlt_plogi_ack_t *)e->u.new_sess.pla;
5076        uint8_t free_fcport = 0;
5077
5078        ql_dbg(ql_dbg_disc, vha, 0xffff,
5079            "%s %d %8phC enter\n",
5080            __func__, __LINE__, e->u.new_sess.port_name);
5081
5082        spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5083        fcport = qla2x00_find_fcport_by_wwpn(vha, e->u.new_sess.port_name, 1);
5084        if (fcport) {
5085                fcport->d_id = e->u.new_sess.id;
5086                if (pla) {
5087                        fcport->fw_login_state = DSC_LS_PLOGI_PEND;
5088                        memcpy(fcport->node_name,
5089                            pla->iocb.u.isp24.u.plogi.node_name,
5090                            WWN_SIZE);
5091                        qlt_plogi_ack_link(vha, pla, fcport, QLT_PLOGI_LINK_SAME_WWN);
5092                        /* we took an extra ref_count to prevent PLOGI ACK when
5093                         * fcport/sess has not been created.
5094                         */
5095                        pla->ref_count--;
5096                }
5097        } else {
5098                spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5099                fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5100                if (fcport) {
5101                        fcport->d_id = e->u.new_sess.id;
5102                        fcport->flags |= FCF_FABRIC_DEVICE;
5103                        fcport->fw_login_state = DSC_LS_PLOGI_PEND;
5104                        fcport->tgt_short_link_down_cnt = 0;
5105
5106                        memcpy(fcport->port_name, e->u.new_sess.port_name,
5107                            WWN_SIZE);
5108
5109                        fcport->fc4_type = e->u.new_sess.fc4_type;
5110                        if (NVME_PRIORITY(vha->hw, fcport))
5111                                fcport->do_prli_nvme = 1;
5112                        else
5113                                fcport->do_prli_nvme = 0;
5114
5115                        if (e->u.new_sess.fc4_type & FS_FCP_IS_N2N) {
5116                                fcport->dm_login_expire = jiffies +
5117                                        QLA_N2N_WAIT_TIME * HZ;
5118                                fcport->fc4_type = FS_FC4TYPE_FCP;
5119                                fcport->n2n_flag = 1;
5120                                if (vha->flags.nvme_enabled)
5121                                        fcport->fc4_type |= FS_FC4TYPE_NVME;
5122                        }
5123
5124                } else {
5125                        ql_dbg(ql_dbg_disc, vha, 0xffff,
5126                                   "%s %8phC mem alloc fail.\n",
5127                                   __func__, e->u.new_sess.port_name);
5128
5129                        if (pla) {
5130                                list_del(&pla->list);
5131                                kmem_cache_free(qla_tgt_plogi_cachep, pla);
5132                        }
5133                        return;
5134                }
5135
5136                spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5137                /* search again to make sure no one else got ahead */
5138                tfcp = qla2x00_find_fcport_by_wwpn(vha,
5139                    e->u.new_sess.port_name, 1);
5140                if (tfcp) {
5141                        /* should rarily happen */
5142                        ql_dbg(ql_dbg_disc, vha, 0xffff,
5143                            "%s %8phC found existing fcport b4 add. DS %d LS %d\n",
5144                            __func__, tfcp->port_name, tfcp->disc_state,
5145                            tfcp->fw_login_state);
5146
5147                        free_fcport = 1;
5148                } else {
5149                        list_add_tail(&fcport->list, &vha->vp_fcports);
5150
5151                }
5152                if (pla) {
5153                        qlt_plogi_ack_link(vha, pla, fcport,
5154                            QLT_PLOGI_LINK_SAME_WWN);
5155                        pla->ref_count--;
5156                }
5157        }
5158        spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5159
5160        if (fcport) {
5161                fcport->id_changed = 1;
5162                fcport->scan_state = QLA_FCPORT_FOUND;
5163                fcport->chip_reset = vha->hw->base_qpair->chip_reset;
5164                memcpy(fcport->node_name, e->u.new_sess.node_name, WWN_SIZE);
5165
5166                if (pla) {
5167                        if (pla->iocb.u.isp24.status_subcode == ELS_PRLI) {
5168                                u16 wd3_lo;
5169
5170                                fcport->fw_login_state = DSC_LS_PRLI_PEND;
5171                                fcport->local = 0;
5172                                fcport->loop_id =
5173                                        le16_to_cpu(
5174                                            pla->iocb.u.isp24.nport_handle);
5175                                fcport->fw_login_state = DSC_LS_PRLI_PEND;
5176                                wd3_lo =
5177                                    le16_to_cpu(
5178                                        pla->iocb.u.isp24.u.prli.wd3_lo);
5179
5180                                if (wd3_lo & BIT_7)
5181                                        fcport->conf_compl_supported = 1;
5182
5183                                if ((wd3_lo & BIT_4) == 0)
5184                                        fcport->port_type = FCT_INITIATOR;
5185                                else
5186                                        fcport->port_type = FCT_TARGET;
5187                        }
5188                        qlt_plogi_ack_unref(vha, pla);
5189                } else {
5190                        fc_port_t *dfcp = NULL;
5191
5192                        spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5193                        tfcp = qla2x00_find_fcport_by_nportid(vha,
5194                            &e->u.new_sess.id, 1);
5195                        if (tfcp && (tfcp != fcport)) {
5196                                /*
5197                                 * We have a conflict fcport with same NportID.
5198                                 */
5199                                ql_dbg(ql_dbg_disc, vha, 0xffff,
5200                                    "%s %8phC found conflict b4 add. DS %d LS %d\n",
5201                                    __func__, tfcp->port_name, tfcp->disc_state,
5202                                    tfcp->fw_login_state);
5203
5204                                switch (tfcp->disc_state) {
5205                                case DSC_DELETED:
5206                                        break;
5207                                case DSC_DELETE_PEND:
5208                                        fcport->login_pause = 1;
5209                                        tfcp->conflict = fcport;
5210                                        break;
5211                                default:
5212                                        fcport->login_pause = 1;
5213                                        tfcp->conflict = fcport;
5214                                        dfcp = tfcp;
5215                                        break;
5216                                }
5217                        }
5218                        spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5219                        if (dfcp)
5220                                qlt_schedule_sess_for_deletion(tfcp);
5221
5222                        if (N2N_TOPO(vha->hw)) {
5223                                fcport->flags &= ~FCF_FABRIC_DEVICE;
5224                                fcport->keep_nport_handle = 1;
5225                                if (vha->flags.nvme_enabled) {
5226                                        fcport->fc4_type =
5227                                            (FS_FC4TYPE_NVME | FS_FC4TYPE_FCP);
5228                                        fcport->n2n_flag = 1;
5229                                }
5230                                fcport->fw_login_state = 0;
5231
5232                                schedule_delayed_work(&vha->scan.scan_work, 5);
5233                        } else {
5234                                qla24xx_fcport_handle_login(vha, fcport);
5235                        }
5236                }
5237        }
5238
5239        if (free_fcport) {
5240                qla2x00_free_fcport(fcport);
5241                if (pla) {
5242                        list_del(&pla->list);
5243                        kmem_cache_free(qla_tgt_plogi_cachep, pla);
5244                }
5245        }
5246}
5247
5248static void qla_sp_retry(struct scsi_qla_host *vha, struct qla_work_evt *e)
5249{
5250        struct srb *sp = e->u.iosb.sp;
5251        int rval;
5252
5253        rval = qla2x00_start_sp(sp);
5254        if (rval != QLA_SUCCESS) {
5255                ql_dbg(ql_dbg_disc, vha, 0x2043,
5256                    "%s: %s: Re-issue IOCB failed (%d).\n",
5257                    __func__, sp->name, rval);
5258                qla24xx_sp_unmap(vha, sp);
5259        }
5260}
5261
5262void
5263qla2x00_do_work(struct scsi_qla_host *vha)
5264{
5265        struct qla_work_evt *e, *tmp;
5266        unsigned long flags;
5267        LIST_HEAD(work);
5268        int rc;
5269
5270        spin_lock_irqsave(&vha->work_lock, flags);
5271        list_splice_init(&vha->work_list, &work);
5272        spin_unlock_irqrestore(&vha->work_lock, flags);
5273
5274        list_for_each_entry_safe(e, tmp, &work, list) {
5275                rc = QLA_SUCCESS;
5276                switch (e->type) {
5277                case QLA_EVT_AEN:
5278                        fc_host_post_event(vha->host, fc_get_event_number(),
5279                            e->u.aen.code, e->u.aen.data);
5280                        break;
5281                case QLA_EVT_IDC_ACK:
5282                        qla81xx_idc_ack(vha, e->u.idc_ack.mb);
5283                        break;
5284                case QLA_EVT_ASYNC_LOGIN:
5285                        qla2x00_async_login(vha, e->u.logio.fcport,
5286                            e->u.logio.data);
5287                        break;
5288                case QLA_EVT_ASYNC_LOGOUT:
5289                        rc = qla2x00_async_logout(vha, e->u.logio.fcport);
5290                        break;
5291                case QLA_EVT_ASYNC_ADISC:
5292                        qla2x00_async_adisc(vha, e->u.logio.fcport,
5293                            e->u.logio.data);
5294                        break;
5295                case QLA_EVT_UEVENT:
5296                        qla2x00_uevent_emit(vha, e->u.uevent.code);
5297                        break;
5298                case QLA_EVT_AENFX:
5299                        qlafx00_process_aen(vha, e);
5300                        break;
5301                case QLA_EVT_GPNID:
5302                        qla24xx_async_gpnid(vha, &e->u.gpnid.id);
5303                        break;
5304                case QLA_EVT_UNMAP:
5305                        qla24xx_sp_unmap(vha, e->u.iosb.sp);
5306                        break;
5307                case QLA_EVT_RELOGIN:
5308                        qla2x00_relogin(vha);
5309                        break;
5310                case QLA_EVT_NEW_SESS:
5311                        qla24xx_create_new_sess(vha, e);
5312                        break;
5313                case QLA_EVT_GPDB:
5314                        qla24xx_async_gpdb(vha, e->u.fcport.fcport,
5315                            e->u.fcport.opt);
5316                        break;
5317                case QLA_EVT_PRLI:
5318                        qla24xx_async_prli(vha, e->u.fcport.fcport);
5319                        break;
5320                case QLA_EVT_GPSC:
5321                        qla24xx_async_gpsc(vha, e->u.fcport.fcport);
5322                        break;
5323                case QLA_EVT_GNL:
5324                        qla24xx_async_gnl(vha, e->u.fcport.fcport);
5325                        break;
5326                case QLA_EVT_NACK:
5327                        qla24xx_do_nack_work(vha, e);
5328                        break;
5329                case QLA_EVT_ASYNC_PRLO:
5330                        rc = qla2x00_async_prlo(vha, e->u.logio.fcport);
5331                        break;
5332                case QLA_EVT_ASYNC_PRLO_DONE:
5333                        qla2x00_async_prlo_done(vha, e->u.logio.fcport,
5334                            e->u.logio.data);
5335                        break;
5336                case QLA_EVT_GPNFT:
5337                        qla24xx_async_gpnft(vha, e->u.gpnft.fc4_type,
5338                            e->u.gpnft.sp);
5339                        break;
5340                case QLA_EVT_GPNFT_DONE:
5341                        qla24xx_async_gpnft_done(vha, e->u.iosb.sp);
5342                        break;
5343                case QLA_EVT_GNNFT_DONE:
5344                        qla24xx_async_gnnft_done(vha, e->u.iosb.sp);
5345                        break;
5346                case QLA_EVT_GNNID:
5347                        qla24xx_async_gnnid(vha, e->u.fcport.fcport);
5348                        break;
5349                case QLA_EVT_GFPNID:
5350                        qla24xx_async_gfpnid(vha, e->u.fcport.fcport);
5351                        break;
5352                case QLA_EVT_SP_RETRY:
5353                        qla_sp_retry(vha, e);
5354                        break;
5355                case QLA_EVT_IIDMA:
5356                        qla_do_iidma_work(vha, e->u.fcport.fcport);
5357                        break;
5358                case QLA_EVT_ELS_PLOGI:
5359                        qla24xx_els_dcmd2_iocb(vha, ELS_DCMD_PLOGI,
5360                            e->u.fcport.fcport, false);
5361                        break;
5362                case QLA_EVT_SA_REPLACE:
5363                        qla24xx_issue_sa_replace_iocb(vha, e);
5364                        break;
5365                }
5366
5367                if (rc == EAGAIN) {
5368                        /* put 'work' at head of 'vha->work_list' */
5369                        spin_lock_irqsave(&vha->work_lock, flags);
5370                        list_splice(&work, &vha->work_list);
5371                        spin_unlock_irqrestore(&vha->work_lock, flags);
5372                        break;
5373                }
5374                list_del_init(&e->list);
5375                if (e->flags & QLA_EVT_FLAG_FREE)
5376                        kfree(e);
5377
5378                /* For each work completed decrement vha ref count */
5379                QLA_VHA_MARK_NOT_BUSY(vha);
5380        }
5381}
5382
5383int qla24xx_post_relogin_work(struct scsi_qla_host *vha)
5384{
5385        struct qla_work_evt *e;
5386
5387        e = qla2x00_alloc_work(vha, QLA_EVT_RELOGIN);
5388
5389        if (!e) {
5390                set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
5391                return QLA_FUNCTION_FAILED;
5392        }
5393
5394        return qla2x00_post_work(vha, e);
5395}
5396
5397/* Relogins all the fcports of a vport
5398 * Context: dpc thread
5399 */
5400void qla2x00_relogin(struct scsi_qla_host *vha)
5401{
5402        fc_port_t       *fcport;
5403        int status, relogin_needed = 0;
5404        struct event_arg ea;
5405
5406        list_for_each_entry(fcport, &vha->vp_fcports, list) {
5407                /*
5408                 * If the port is not ONLINE then try to login
5409                 * to it if we haven't run out of retries.
5410                 */
5411                if (atomic_read(&fcport->state) != FCS_ONLINE &&
5412                    fcport->login_retry) {
5413                        if (fcport->scan_state != QLA_FCPORT_FOUND ||
5414                            fcport->disc_state == DSC_LOGIN_AUTH_PEND ||
5415                            fcport->disc_state == DSC_LOGIN_COMPLETE)
5416                                continue;
5417
5418                        if (fcport->flags & (FCF_ASYNC_SENT|FCF_ASYNC_ACTIVE) ||
5419                                fcport->disc_state == DSC_DELETE_PEND) {
5420                                relogin_needed = 1;
5421                        } else {
5422                                if (vha->hw->current_topology != ISP_CFG_NL) {
5423                                        memset(&ea, 0, sizeof(ea));
5424                                        ea.fcport = fcport;
5425                                        qla24xx_handle_relogin_event(vha, &ea);
5426                                } else if (vha->hw->current_topology ==
5427                                    ISP_CFG_NL) {
5428                                        fcport->login_retry--;
5429                                        status =
5430                                            qla2x00_local_device_login(vha,
5431                                                fcport);
5432                                        if (status == QLA_SUCCESS) {
5433                                                fcport->old_loop_id =
5434                                                    fcport->loop_id;
5435                                                ql_dbg(ql_dbg_disc, vha, 0x2003,
5436                                                    "Port login OK: logged in ID 0x%x.\n",
5437                                                    fcport->loop_id);
5438                                                qla2x00_update_fcport
5439                                                        (vha, fcport);
5440                                        } else if (status == 1) {
5441                                                set_bit(RELOGIN_NEEDED,
5442                                                    &vha->dpc_flags);
5443                                                /* retry the login again */
5444                                                ql_dbg(ql_dbg_disc, vha, 0x2007,
5445                                                    "Retrying %d login again loop_id 0x%x.\n",
5446                                                    fcport->login_retry,
5447                                                    fcport->loop_id);
5448                                        } else {
5449                                                fcport->login_retry = 0;
5450                                        }
5451
5452                                        if (fcport->login_retry == 0 &&
5453                                            status != QLA_SUCCESS)
5454                                                qla2x00_clear_loop_id(fcport);
5455                                }
5456                        }
5457                }
5458                if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5459                        break;
5460        }
5461
5462        if (relogin_needed)
5463                set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
5464
5465        ql_dbg(ql_dbg_disc, vha, 0x400e,
5466            "Relogin end.\n");
5467}
5468
5469/* Schedule work on any of the dpc-workqueues */
5470void
5471qla83xx_schedule_work(scsi_qla_host_t *base_vha, int work_code)
5472{
5473        struct qla_hw_data *ha = base_vha->hw;
5474
5475        switch (work_code) {
5476        case MBA_IDC_AEN: /* 0x8200 */
5477                if (ha->dpc_lp_wq)
5478                        queue_work(ha->dpc_lp_wq, &ha->idc_aen);
5479                break;
5480
5481        case QLA83XX_NIC_CORE_RESET: /* 0x1 */
5482                if (!ha->flags.nic_core_reset_hdlr_active) {
5483                        if (ha->dpc_hp_wq)
5484                                queue_work(ha->dpc_hp_wq, &ha->nic_core_reset);
5485                } else
5486                        ql_dbg(ql_dbg_p3p, base_vha, 0xb05e,
5487                            "NIC Core reset is already active. Skip "
5488                            "scheduling it again.\n");
5489                break;
5490        case QLA83XX_IDC_STATE_HANDLER: /* 0x2 */
5491                if (ha->dpc_hp_wq)
5492                        queue_work(ha->dpc_hp_wq, &ha->idc_state_handler);
5493                break;
5494        case QLA83XX_NIC_CORE_UNRECOVERABLE: /* 0x3 */
5495                if (ha->dpc_hp_wq)
5496                        queue_work(ha->dpc_hp_wq, &ha->nic_core_unrecoverable);
5497                break;
5498        default:
5499                ql_log(ql_log_warn, base_vha, 0xb05f,
5500                    "Unknown work-code=0x%x.\n", work_code);
5501        }
5502
5503        return;
5504}
5505
5506/* Work: Perform NIC Core Unrecoverable state handling */
5507void
5508qla83xx_nic_core_unrecoverable_work(struct work_struct *work)
5509{
5510        struct qla_hw_data *ha =
5511                container_of(work, struct qla_hw_data, nic_core_unrecoverable);
5512        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5513        uint32_t dev_state = 0;
5514
5515        qla83xx_idc_lock(base_vha, 0);
5516        qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5517        qla83xx_reset_ownership(base_vha);
5518        if (ha->flags.nic_core_reset_owner) {
5519                ha->flags.nic_core_reset_owner = 0;
5520                qla83xx_wr_reg(base_vha, QLA83XX_IDC_DEV_STATE,
5521                    QLA8XXX_DEV_FAILED);
5522                ql_log(ql_log_info, base_vha, 0xb060, "HW State: FAILED.\n");
5523                qla83xx_schedule_work(base_vha, QLA83XX_IDC_STATE_HANDLER);
5524        }
5525        qla83xx_idc_unlock(base_vha, 0);
5526}
5527
5528/* Work: Execute IDC state handler */
5529void
5530qla83xx_idc_state_handler_work(struct work_struct *work)
5531{
5532        struct qla_hw_data *ha =
5533                container_of(work, struct qla_hw_data, idc_state_handler);
5534        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5535        uint32_t dev_state = 0;
5536
5537        qla83xx_idc_lock(base_vha, 0);
5538        qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5539        if (dev_state == QLA8XXX_DEV_FAILED ||
5540                        dev_state == QLA8XXX_DEV_NEED_QUIESCENT)
5541                qla83xx_idc_state_handler(base_vha);
5542        qla83xx_idc_unlock(base_vha, 0);
5543}
5544
5545static int
5546qla83xx_check_nic_core_fw_alive(scsi_qla_host_t *base_vha)
5547{
5548        int rval = QLA_SUCCESS;
5549        unsigned long heart_beat_wait = jiffies + (1 * HZ);
5550        uint32_t heart_beat_counter1, heart_beat_counter2;
5551
5552        do {
5553                if (time_after(jiffies, heart_beat_wait)) {
5554                        ql_dbg(ql_dbg_p3p, base_vha, 0xb07c,
5555                            "Nic Core f/w is not alive.\n");
5556                        rval = QLA_FUNCTION_FAILED;
5557                        break;
5558                }
5559
5560                qla83xx_idc_lock(base_vha, 0);
5561                qla83xx_rd_reg(base_vha, QLA83XX_FW_HEARTBEAT,
5562                    &heart_beat_counter1);
5563                qla83xx_idc_unlock(base_vha, 0);
5564                msleep(100);
5565                qla83xx_idc_lock(base_vha, 0);
5566                qla83xx_rd_reg(base_vha, QLA83XX_FW_HEARTBEAT,
5567                    &heart_beat_counter2);
5568                qla83xx_idc_unlock(base_vha, 0);
5569        } while (heart_beat_counter1 == heart_beat_counter2);
5570
5571        return rval;
5572}
5573
5574/* Work: Perform NIC Core Reset handling */
5575void
5576qla83xx_nic_core_reset_work(struct work_struct *work)
5577{
5578        struct qla_hw_data *ha =
5579                container_of(work, struct qla_hw_data, nic_core_reset);
5580        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5581        uint32_t dev_state = 0;
5582
5583        if (IS_QLA2031(ha)) {
5584                if (qla2xxx_mctp_dump(base_vha) != QLA_SUCCESS)
5585                        ql_log(ql_log_warn, base_vha, 0xb081,
5586                            "Failed to dump mctp\n");
5587                return;
5588        }
5589
5590        if (!ha->flags.nic_core_reset_hdlr_active) {
5591                if (qla83xx_check_nic_core_fw_alive(base_vha) == QLA_SUCCESS) {
5592                        qla83xx_idc_lock(base_vha, 0);
5593                        qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE,
5594                            &dev_state);
5595                        qla83xx_idc_unlock(base_vha, 0);
5596                        if (dev_state != QLA8XXX_DEV_NEED_RESET) {
5597                                ql_dbg(ql_dbg_p3p, base_vha, 0xb07a,
5598                                    "Nic Core f/w is alive.\n");
5599                                return;
5600                        }
5601                }
5602
5603                ha->flags.nic_core_reset_hdlr_active = 1;
5604                if (qla83xx_nic_core_reset(base_vha)) {
5605                        /* NIC Core reset failed. */
5606                        ql_dbg(ql_dbg_p3p, base_vha, 0xb061,
5607                            "NIC Core reset failed.\n");
5608                }
5609                ha->flags.nic_core_reset_hdlr_active = 0;
5610        }
5611}
5612
5613/* Work: Handle 8200 IDC aens */
5614void
5615qla83xx_service_idc_aen(struct work_struct *work)
5616{
5617        struct qla_hw_data *ha =
5618                container_of(work, struct qla_hw_data, idc_aen);
5619        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5620        uint32_t dev_state, idc_control;
5621
5622        qla83xx_idc_lock(base_vha, 0);
5623        qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5624        qla83xx_rd_reg(base_vha, QLA83XX_IDC_CONTROL, &idc_control);
5625        qla83xx_idc_unlock(base_vha, 0);
5626        if (dev_state == QLA8XXX_DEV_NEED_RESET) {
5627                if (idc_control & QLA83XX_IDC_GRACEFUL_RESET) {
5628                        ql_dbg(ql_dbg_p3p, base_vha, 0xb062,
5629                            "Application requested NIC Core Reset.\n");
5630                        qla83xx_schedule_work(base_vha, QLA83XX_NIC_CORE_RESET);
5631                } else if (qla83xx_check_nic_core_fw_alive(base_vha) ==
5632                    QLA_SUCCESS) {
5633                        ql_dbg(ql_dbg_p3p, base_vha, 0xb07b,
5634                            "Other protocol driver requested NIC Core Reset.\n");
5635                        qla83xx_schedule_work(base_vha, QLA83XX_NIC_CORE_RESET);
5636                }
5637        } else if (dev_state == QLA8XXX_DEV_FAILED ||
5638                        dev_state == QLA8XXX_DEV_NEED_QUIESCENT) {
5639                qla83xx_schedule_work(base_vha, QLA83XX_IDC_STATE_HANDLER);
5640        }
5641}
5642
5643static void
5644qla83xx_wait_logic(void)
5645{
5646        int i;
5647
5648        /* Yield CPU */
5649        if (!in_interrupt()) {
5650                /*
5651                 * Wait about 200ms before retrying again.
5652                 * This controls the number of retries for single
5653                 * lock operation.
5654                 */
5655                msleep(100);
5656                schedule();
5657        } else {
5658                for (i = 0; i < 20; i++)
5659                        cpu_relax(); /* This a nop instr on i386 */
5660        }
5661}
5662
5663static int
5664qla83xx_force_lock_recovery(scsi_qla_host_t *base_vha)
5665{
5666        int rval;
5667        uint32_t data;
5668        uint32_t idc_lck_rcvry_stage_mask = 0x3;
5669        uint32_t idc_lck_rcvry_owner_mask = 0x3c;
5670        struct qla_hw_data *ha = base_vha->hw;
5671
5672        ql_dbg(ql_dbg_p3p, base_vha, 0xb086,
5673            "Trying force recovery of the IDC lock.\n");
5674
5675        rval = qla83xx_rd_reg(base_vha, QLA83XX_IDC_LOCK_RECOVERY, &data);
5676        if (rval)
5677                return rval;
5678
5679        if ((data & idc_lck_rcvry_stage_mask) > 0) {
5680                return QLA_SUCCESS;
5681        } else {
5682                data = (IDC_LOCK_RECOVERY_STAGE1) | (ha->portnum << 2);
5683                rval = qla83xx_wr_reg(base_vha, QLA83XX_IDC_LOCK_RECOVERY,
5684                    data);
5685                if (rval)
5686                        return rval;
5687
5688                msleep(200);
5689
5690                rval = qla83xx_rd_reg(base_vha, QLA83XX_IDC_LOCK_RECOVERY,
5691                    &data);
5692                if (rval)
5693                        return rval;
5694
5695                if (((data & idc_lck_rcvry_owner_mask) >> 2) == ha->portnum) {
5696                        data &= (IDC_LOCK_RECOVERY_STAGE2 |
5697                                        ~(idc_lck_rcvry_stage_mask));
5698                        rval = qla83xx_wr_reg(base_vha,
5699                            QLA83XX_IDC_LOCK_RECOVERY, data);
5700                        if (rval)
5701                                return rval;
5702
5703                        /* Forcefully perform IDC UnLock */
5704                        rval = qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_UNLOCK,
5705                            &data);
5706                        if (rval)
5707                                return rval;
5708                        /* Clear lock-id by setting 0xff */
5709                        rval = qla83xx_wr_reg(base_vha, QLA83XX_DRIVER_LOCKID,
5710                            0xff);
5711                        if (rval)
5712                                return rval;
5713                        /* Clear lock-recovery by setting 0x0 */
5714                        rval = qla83xx_wr_reg(base_vha,
5715                            QLA83XX_IDC_LOCK_RECOVERY, 0x0);
5716                        if (rval)
5717                                return rval;
5718                } else
5719                        return QLA_SUCCESS;
5720        }
5721
5722        return rval;
5723}
5724
5725static int
5726qla83xx_idc_lock_recovery(scsi_qla_host_t *base_vha)
5727{
5728        int rval = QLA_SUCCESS;
5729        uint32_t o_drv_lockid, n_drv_lockid;
5730        unsigned long lock_recovery_timeout;
5731
5732        lock_recovery_timeout = jiffies + QLA83XX_MAX_LOCK_RECOVERY_WAIT;
5733retry_lockid:
5734        rval = qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCKID, &o_drv_lockid);
5735        if (rval)
5736                goto exit;
5737
5738        /* MAX wait time before forcing IDC Lock recovery = 2 secs */
5739        if (time_after_eq(jiffies, lock_recovery_timeout)) {
5740                if (qla83xx_force_lock_recovery(base_vha) == QLA_SUCCESS)
5741                        return QLA_SUCCESS;
5742                else
5743                        return QLA_FUNCTION_FAILED;
5744        }
5745
5746        rval = qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCKID, &n_drv_lockid);
5747        if (rval)
5748                goto exit;
5749
5750        if (o_drv_lockid == n_drv_lockid) {
5751                qla83xx_wait_logic();
5752                goto retry_lockid;
5753        } else
5754                return QLA_SUCCESS;
5755
5756exit:
5757        return rval;
5758}
5759
5760void
5761qla83xx_idc_lock(scsi_qla_host_t *base_vha, uint16_t requester_id)
5762{
5763        uint32_t data;
5764        uint32_t lock_owner;
5765        struct qla_hw_data *ha = base_vha->hw;
5766
5767        /* IDC-lock implementation using driver-lock/lock-id remote registers */
5768retry_lock:
5769        if (qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCK, &data)
5770            == QLA_SUCCESS) {
5771                if (data) {
5772                        /* Setting lock-id to our function-number */
5773                        qla83xx_wr_reg(base_vha, QLA83XX_DRIVER_LOCKID,
5774                            ha->portnum);
5775                } else {
5776                        qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCKID,
5777                            &lock_owner);
5778                        ql_dbg(ql_dbg_p3p, base_vha, 0xb063,
5779                            "Failed to acquire IDC lock, acquired by %d, "
5780                            "retrying...\n", lock_owner);
5781
5782                        /* Retry/Perform IDC-Lock recovery */
5783                        if (qla83xx_idc_lock_recovery(base_vha)
5784                            == QLA_SUCCESS) {
5785                                qla83xx_wait_logic();
5786                                goto retry_lock;
5787                        } else
5788                                ql_log(ql_log_warn, base_vha, 0xb075,
5789                                    "IDC Lock recovery FAILED.\n");
5790                }
5791
5792        }
5793
5794        return;
5795}
5796
5797static bool
5798qla25xx_rdp_rsp_reduce_size(struct scsi_qla_host *vha,
5799        struct purex_entry_24xx *purex)
5800{
5801        char fwstr[16];
5802        u32 sid = purex->s_id[2] << 16 | purex->s_id[1] << 8 | purex->s_id[0];
5803        struct port_database_24xx *pdb;
5804
5805        /* Domain Controller is always logged-out. */
5806        /* if RDP request is not from Domain Controller: */
5807        if (sid != 0xfffc01)
5808                return false;
5809
5810        ql_dbg(ql_dbg_init, vha, 0x0181, "%s: s_id=%#x\n", __func__, sid);
5811
5812        pdb = kzalloc(sizeof(*pdb), GFP_KERNEL);
5813        if (!pdb) {
5814                ql_dbg(ql_dbg_init, vha, 0x0181,
5815                    "%s: Failed allocate pdb\n", __func__);
5816        } else if (qla24xx_get_port_database(vha,
5817                                le16_to_cpu(purex->nport_handle), pdb)) {
5818                ql_dbg(ql_dbg_init, vha, 0x0181,
5819                    "%s: Failed get pdb sid=%x\n", __func__, sid);
5820        } else if (pdb->current_login_state != PDS_PLOGI_COMPLETE &&
5821            pdb->current_login_state != PDS_PRLI_COMPLETE) {
5822                ql_dbg(ql_dbg_init, vha, 0x0181,
5823                    "%s: Port not logged in sid=%#x\n", __func__, sid);
5824        } else {
5825                /* RDP request is from logged in port */
5826                kfree(pdb);
5827                return false;
5828        }
5829        kfree(pdb);
5830
5831        vha->hw->isp_ops->fw_version_str(vha, fwstr, sizeof(fwstr));
5832        fwstr[strcspn(fwstr, " ")] = 0;
5833        /* if FW version allows RDP response length upto 2048 bytes: */
5834        if (strcmp(fwstr, "8.09.00") > 0 || strcmp(fwstr, "8.05.65") == 0)
5835                return false;
5836
5837        ql_dbg(ql_dbg_init, vha, 0x0181, "%s: fw=%s\n", __func__, fwstr);
5838
5839        /* RDP response length is to be reduced to maximum 256 bytes */
5840        return true;
5841}
5842
5843/*
5844 * Function Name: qla24xx_process_purex_iocb
5845 *
5846 * Description:
5847 * Prepare a RDP response and send to Fabric switch
5848 *
5849 * PARAMETERS:
5850 * vha: SCSI qla host
5851 * purex: RDP request received by HBA
5852 */
5853void qla24xx_process_purex_rdp(struct scsi_qla_host *vha,
5854                               struct purex_item *item)
5855{
5856        struct qla_hw_data *ha = vha->hw;
5857        struct purex_entry_24xx *purex =
5858            (struct purex_entry_24xx *)&item->iocb;
5859        dma_addr_t rsp_els_dma;
5860        dma_addr_t rsp_payload_dma;
5861        dma_addr_t stat_dma;
5862        dma_addr_t sfp_dma;
5863        struct els_entry_24xx *rsp_els = NULL;
5864        struct rdp_rsp_payload *rsp_payload = NULL;
5865        struct link_statistics *stat = NULL;
5866        uint8_t *sfp = NULL;
5867        uint16_t sfp_flags = 0;
5868        uint rsp_payload_length = sizeof(*rsp_payload);
5869        int rval;
5870
5871        ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x0180,
5872            "%s: Enter\n", __func__);
5873
5874        ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x0181,
5875            "-------- ELS REQ -------\n");
5876        ql_dump_buffer(ql_dbg_init + ql_dbg_verbose, vha, 0x0182,
5877            purex, sizeof(*purex));
5878
5879        if (qla25xx_rdp_rsp_reduce_size(vha, purex)) {
5880                rsp_payload_length =
5881                    offsetof(typeof(*rsp_payload), optical_elmt_desc);
5882                ql_dbg(ql_dbg_init, vha, 0x0181,
5883                    "Reducing RSP payload length to %u bytes...\n",
5884                    rsp_payload_length);
5885        }
5886
5887        rsp_els = dma_alloc_coherent(&ha->pdev->dev, sizeof(*rsp_els),
5888            &rsp_els_dma, GFP_KERNEL);
5889        if (!rsp_els) {
5890                ql_log(ql_log_warn, vha, 0x0183,
5891                    "Failed allocate dma buffer ELS RSP.\n");
5892                goto dealloc;
5893        }
5894
5895        rsp_payload = dma_alloc_coherent(&ha->pdev->dev, sizeof(*rsp_payload),
5896            &rsp_payload_dma, GFP_KERNEL);
5897        if (!rsp_payload) {
5898                ql_log(ql_log_warn, vha, 0x0184,
5899                    "Failed allocate dma buffer ELS RSP payload.\n");
5900                goto dealloc;
5901        }
5902
5903        sfp = dma_alloc_coherent(&ha->pdev->dev, SFP_RTDI_LEN,
5904            &sfp_dma, GFP_KERNEL);
5905
5906        stat = dma_alloc_coherent(&ha->pdev->dev, sizeof(*stat),
5907            &stat_dma, GFP_KERNEL);
5908
5909        /* Prepare Response IOCB */
5910        rsp_els->entry_type = ELS_IOCB_TYPE;
5911        rsp_els->entry_count = 1;
5912        rsp_els->sys_define = 0;
5913        rsp_els->entry_status = 0;
5914        rsp_els->handle = 0;
5915        rsp_els->nport_handle = purex->nport_handle;
5916        rsp_els->tx_dsd_count = cpu_to_le16(1);
5917        rsp_els->vp_index = purex->vp_idx;
5918        rsp_els->sof_type = EST_SOFI3;
5919        rsp_els->rx_xchg_address = purex->rx_xchg_addr;
5920        rsp_els->rx_dsd_count = 0;
5921        rsp_els->opcode = purex->els_frame_payload[0];
5922
5923        rsp_els->d_id[0] = purex->s_id[0];
5924        rsp_els->d_id[1] = purex->s_id[1];
5925        rsp_els->d_id[2] = purex->s_id[2];
5926
5927        rsp_els->control_flags = cpu_to_le16(EPD_ELS_ACC);
5928        rsp_els->rx_byte_count = 0;
5929        rsp_els->tx_byte_count = cpu_to_le32(rsp_payload_length);
5930
5931        put_unaligned_le64(rsp_payload_dma, &rsp_els->tx_address);
5932        rsp_els->tx_len = rsp_els->tx_byte_count;
5933
5934        rsp_els->rx_address = 0;
5935        rsp_els->rx_len = 0;
5936
5937        /* Prepare Response Payload */
5938        rsp_payload->hdr.cmd = cpu_to_be32(0x2 << 24); /* LS_ACC */
5939        rsp_payload->hdr.len = cpu_to_be32(le32_to_cpu(rsp_els->tx_byte_count) -
5940                                           sizeof(rsp_payload->hdr));
5941
5942        /* Link service Request Info Descriptor */
5943        rsp_payload->ls_req_info_desc.desc_tag = cpu_to_be32(0x1);
5944        rsp_payload->ls_req_info_desc.desc_len =
5945            cpu_to_be32(RDP_DESC_LEN(rsp_payload->ls_req_info_desc));
5946        rsp_payload->ls_req_info_desc.req_payload_word_0 =
5947            cpu_to_be32p((uint32_t *)purex->els_frame_payload);
5948
5949        /* Link service Request Info Descriptor 2 */
5950        rsp_payload->ls_req_info_desc2.desc_tag = cpu_to_be32(0x1);
5951        rsp_payload->ls_req_info_desc2.desc_len =
5952            cpu_to_be32(RDP_DESC_LEN(rsp_payload->ls_req_info_desc2));
5953        rsp_payload->ls_req_info_desc2.req_payload_word_0 =
5954            cpu_to_be32p((uint32_t *)purex->els_frame_payload);
5955
5956
5957        rsp_payload->sfp_diag_desc.desc_tag = cpu_to_be32(0x10000);
5958        rsp_payload->sfp_diag_desc.desc_len =
5959                cpu_to_be32(RDP_DESC_LEN(rsp_payload->sfp_diag_desc));
5960
5961        if (sfp) {
5962                /* SFP Flags */
5963                memset(sfp, 0, SFP_RTDI_LEN);
5964                rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa0, 0x7, 2, 0);
5965                if (!rval) {
5966                        /* SFP Flags bits 3-0: Port Tx Laser Type */
5967                        if (sfp[0] & BIT_2 || sfp[1] & (BIT_6|BIT_5))
5968                                sfp_flags |= BIT_0; /* short wave */
5969                        else if (sfp[0] & BIT_1)
5970                                sfp_flags |= BIT_1; /* long wave 1310nm */
5971                        else if (sfp[1] & BIT_4)
5972                                sfp_flags |= BIT_1|BIT_0; /* long wave 1550nm */
5973                }
5974
5975                /* SFP Type */
5976                memset(sfp, 0, SFP_RTDI_LEN);
5977                rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa0, 0x0, 1, 0);
5978                if (!rval) {
5979                        sfp_flags |= BIT_4; /* optical */
5980                        if (sfp[0] == 0x3)
5981                                sfp_flags |= BIT_6; /* sfp+ */
5982                }
5983
5984                rsp_payload->sfp_diag_desc.sfp_flags = cpu_to_be16(sfp_flags);
5985
5986                /* SFP Diagnostics */
5987                memset(sfp, 0, SFP_RTDI_LEN);
5988                rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa2, 0x60, 10, 0);
5989                if (!rval) {
5990                        __be16 *trx = (__force __be16 *)sfp; /* already be16 */
5991                        rsp_payload->sfp_diag_desc.temperature = trx[0];
5992                        rsp_payload->sfp_diag_desc.vcc = trx[1];
5993                        rsp_payload->sfp_diag_desc.tx_bias = trx[2];
5994                        rsp_payload->sfp_diag_desc.tx_power = trx[3];
5995                        rsp_payload->sfp_diag_desc.rx_power = trx[4];
5996                }
5997        }
5998
5999        /* Port Speed Descriptor */
6000        rsp_payload->port_speed_desc.desc_tag = cpu_to_be32(0x10001);
6001        rsp_payload->port_speed_desc.desc_len =
6002            cpu_to_be32(RDP_DESC_LEN(rsp_payload->port_speed_desc));
6003        rsp_payload->port_speed_desc.speed_capab = cpu_to_be16(
6004            qla25xx_fdmi_port_speed_capability(ha));
6005        rsp_payload->port_speed_desc.operating_speed = cpu_to_be16(
6006            qla25xx_fdmi_port_speed_currently(ha));
6007
6008        /* Link Error Status Descriptor */
6009        rsp_payload->ls_err_desc.desc_tag = cpu_to_be32(0x10002);
6010        rsp_payload->ls_err_desc.desc_len =
6011                cpu_to_be32(RDP_DESC_LEN(rsp_payload->ls_err_desc));
6012
6013        if (stat) {
6014                rval = qla24xx_get_isp_stats(vha, stat, stat_dma, 0);
6015                if (!rval) {
6016                        rsp_payload->ls_err_desc.link_fail_cnt =
6017                            cpu_to_be32(le32_to_cpu(stat->link_fail_cnt));
6018                        rsp_payload->ls_err_desc.loss_sync_cnt =
6019                            cpu_to_be32(le32_to_cpu(stat->loss_sync_cnt));
6020                        rsp_payload->ls_err_desc.loss_sig_cnt =
6021                            cpu_to_be32(le32_to_cpu(stat->loss_sig_cnt));
6022                        rsp_payload->ls_err_desc.prim_seq_err_cnt =
6023                            cpu_to_be32(le32_to_cpu(stat->prim_seq_err_cnt));
6024                        rsp_payload->ls_err_desc.inval_xmit_word_cnt =
6025                            cpu_to_be32(le32_to_cpu(stat->inval_xmit_word_cnt));
6026                        rsp_payload->ls_err_desc.inval_crc_cnt =
6027                            cpu_to_be32(le32_to_cpu(stat->inval_crc_cnt));
6028                        rsp_payload->ls_err_desc.pn_port_phy_type |= BIT_6;
6029                }
6030        }
6031
6032        /* Portname Descriptor */
6033        rsp_payload->port_name_diag_desc.desc_tag = cpu_to_be32(0x10003);
6034        rsp_payload->port_name_diag_desc.desc_len =
6035            cpu_to_be32(RDP_DESC_LEN(rsp_payload->port_name_diag_desc));
6036        memcpy(rsp_payload->port_name_diag_desc.WWNN,
6037            vha->node_name,
6038            sizeof(rsp_payload->port_name_diag_desc.WWNN));
6039        memcpy(rsp_payload->port_name_diag_desc.WWPN,
6040            vha->port_name,
6041            sizeof(rsp_payload->port_name_diag_desc.WWPN));
6042
6043        /* F-Port Portname Descriptor */
6044        rsp_payload->port_name_direct_desc.desc_tag = cpu_to_be32(0x10003);
6045        rsp_payload->port_name_direct_desc.desc_len =
6046            cpu_to_be32(RDP_DESC_LEN(rsp_payload->port_name_direct_desc));
6047        memcpy(rsp_payload->port_name_direct_desc.WWNN,
6048            vha->fabric_node_name,
6049            sizeof(rsp_payload->port_name_direct_desc.WWNN));
6050        memcpy(rsp_payload->port_name_direct_desc.WWPN,
6051            vha->fabric_port_name,
6052            sizeof(rsp_payload->port_name_direct_desc.WWPN));
6053
6054        /* Bufer Credit Descriptor */
6055        rsp_payload->buffer_credit_desc.desc_tag = cpu_to_be32(0x10006);
6056        rsp_payload->buffer_credit_desc.desc_len =
6057                cpu_to_be32(RDP_DESC_LEN(rsp_payload->buffer_credit_desc));
6058        rsp_payload->buffer_credit_desc.fcport_b2b = 0;
6059        rsp_payload->buffer_credit_desc.attached_fcport_b2b = cpu_to_be32(0);
6060        rsp_payload->buffer_credit_desc.fcport_rtt = cpu_to_be32(0);
6061
6062        if (ha->flags.plogi_template_valid) {
6063                uint32_t tmp =
6064                be16_to_cpu(ha->plogi_els_payld.fl_csp.sp_bb_cred);
6065                rsp_payload->buffer_credit_desc.fcport_b2b = cpu_to_be32(tmp);
6066        }
6067
6068        if (rsp_payload_length < sizeof(*rsp_payload))
6069                goto send;
6070
6071        /* Optical Element Descriptor, Temperature */
6072        rsp_payload->optical_elmt_desc[0].desc_tag = cpu_to_be32(0x10007);
6073        rsp_payload->optical_elmt_desc[0].desc_len =
6074                cpu_to_be32(RDP_DESC_LEN(*rsp_payload->optical_elmt_desc));
6075        /* Optical Element Descriptor, Voltage */
6076        rsp_payload->optical_elmt_desc[1].desc_tag = cpu_to_be32(0x10007);
6077        rsp_payload->optical_elmt_desc[1].desc_len =
6078                cpu_to_be32(RDP_DESC_LEN(*rsp_payload->optical_elmt_desc));
6079        /* Optical Element Descriptor, Tx Bias Current */
6080        rsp_payload->optical_elmt_desc[2].desc_tag = cpu_to_be32(0x10007);
6081        rsp_payload->optical_elmt_desc[2].desc_len =
6082                cpu_to_be32(RDP_DESC_LEN(*rsp_payload->optical_elmt_desc));
6083        /* Optical Element Descriptor, Tx Power */
6084        rsp_payload->optical_elmt_desc[3].desc_tag = cpu_to_be32(0x10007);
6085        rsp_payload->optical_elmt_desc[3].desc_len =
6086                cpu_to_be32(RDP_DESC_LEN(*rsp_payload->optical_elmt_desc));
6087        /* Optical Element Descriptor, Rx Power */
6088        rsp_payload->optical_elmt_desc[4].desc_tag = cpu_to_be32(0x10007);
6089        rsp_payload->optical_elmt_desc[4].desc_len =
6090                cpu_to_be32(RDP_DESC_LEN(*rsp_payload->optical_elmt_desc));
6091
6092        if (sfp) {
6093                memset(sfp, 0, SFP_RTDI_LEN);
6094                rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa2, 0, 64, 0);
6095                if (!rval) {
6096                        __be16 *trx = (__force __be16 *)sfp; /* already be16 */
6097
6098                        /* Optical Element Descriptor, Temperature */
6099                        rsp_payload->optical_elmt_desc[0].high_alarm = trx[0];
6100                        rsp_payload->optical_elmt_desc[0].low_alarm = trx[1];
6101                        rsp_payload->optical_elmt_desc[0].high_warn = trx[2];
6102                        rsp_payload->optical_elmt_desc[0].low_warn = trx[3];
6103                        rsp_payload->optical_elmt_desc[0].element_flags =
6104                            cpu_to_be32(1 << 28);
6105
6106                        /* Optical Element Descriptor, Voltage */
6107                        rsp_payload->optical_elmt_desc[1].high_alarm = trx[4];
6108                        rsp_payload->optical_elmt_desc[1].low_alarm = trx[5];
6109                        rsp_payload->optical_elmt_desc[1].high_warn = trx[6];
6110                        rsp_payload->optical_elmt_desc[1].low_warn = trx[7];
6111                        rsp_payload->optical_elmt_desc[1].element_flags =
6112                            cpu_to_be32(2 << 28);
6113
6114                        /* Optical Element Descriptor, Tx Bias Current */
6115                        rsp_payload->optical_elmt_desc[2].high_alarm = trx[8];
6116                        rsp_payload->optical_elmt_desc[2].low_alarm = trx[9];
6117                        rsp_payload->optical_elmt_desc[2].high_warn = trx[10];
6118                        rsp_payload->optical_elmt_desc[2].low_warn = trx[11];
6119                        rsp_payload->optical_elmt_desc[2].element_flags =
6120                            cpu_to_be32(3 << 28);
6121
6122                        /* Optical Element Descriptor, Tx Power */
6123                        rsp_payload->optical_elmt_desc[3].high_alarm = trx[12];
6124                        rsp_payload->optical_elmt_desc[3].low_alarm = trx[13];
6125                        rsp_payload->optical_elmt_desc[3].high_warn = trx[14];
6126                        rsp_payload->optical_elmt_desc[3].low_warn = trx[15];
6127                        rsp_payload->optical_elmt_desc[3].element_flags =
6128                            cpu_to_be32(4 << 28);
6129
6130                        /* Optical Element Descriptor, Rx Power */
6131                        rsp_payload->optical_elmt_desc[4].high_alarm = trx[16];
6132                        rsp_payload->optical_elmt_desc[4].low_alarm = trx[17];
6133                        rsp_payload->optical_elmt_desc[4].high_warn = trx[18];
6134                        rsp_payload->optical_elmt_desc[4].low_warn = trx[19];
6135                        rsp_payload->optical_elmt_desc[4].element_flags =
6136                            cpu_to_be32(5 << 28);
6137                }
6138
6139                memset(sfp, 0, SFP_RTDI_LEN);
6140                rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa2, 112, 64, 0);
6141                if (!rval) {
6142                        /* Temperature high/low alarm/warning */
6143                        rsp_payload->optical_elmt_desc[0].element_flags |=
6144                            cpu_to_be32(
6145                                (sfp[0] >> 7 & 1) << 3 |
6146                                (sfp[0] >> 6 & 1) << 2 |
6147                                (sfp[4] >> 7 & 1) << 1 |
6148                                (sfp[4] >> 6 & 1) << 0);
6149
6150                        /* Voltage high/low alarm/warning */
6151                        rsp_payload->optical_elmt_desc[1].element_flags |=
6152                            cpu_to_be32(
6153                                (sfp[0] >> 5 & 1) << 3 |
6154                                (sfp[0] >> 4 & 1) << 2 |
6155                                (sfp[4] >> 5 & 1) << 1 |
6156                                (sfp[4] >> 4 & 1) << 0);
6157
6158                        /* Tx Bias Current high/low alarm/warning */
6159                        rsp_payload->optical_elmt_desc[2].element_flags |=
6160                            cpu_to_be32(
6161                                (sfp[0] >> 3 & 1) << 3 |
6162                                (sfp[0] >> 2 & 1) << 2 |
6163                                (sfp[4] >> 3 & 1) << 1 |
6164                                (sfp[4] >> 2 & 1) << 0);
6165
6166                        /* Tx Power high/low alarm/warning */
6167                        rsp_payload->optical_elmt_desc[3].element_flags |=
6168                            cpu_to_be32(
6169                                (sfp[0] >> 1 & 1) << 3 |
6170                                (sfp[0] >> 0 & 1) << 2 |
6171                                (sfp[4] >> 1 & 1) << 1 |
6172                                (sfp[4] >> 0 & 1) << 0);
6173
6174                        /* Rx Power high/low alarm/warning */
6175                        rsp_payload->optical_elmt_desc[4].element_flags |=
6176                            cpu_to_be32(
6177                                (sfp[1] >> 7 & 1) << 3 |
6178                                (sfp[1] >> 6 & 1) << 2 |
6179                                (sfp[5] >> 7 & 1) << 1 |
6180                                (sfp[5] >> 6 & 1) << 0);
6181                }
6182        }
6183
6184        /* Optical Product Data Descriptor */
6185        rsp_payload->optical_prod_desc.desc_tag = cpu_to_be32(0x10008);
6186        rsp_payload->optical_prod_desc.desc_len =
6187                cpu_to_be32(RDP_DESC_LEN(rsp_payload->optical_prod_desc));
6188
6189        if (sfp) {
6190                memset(sfp, 0, SFP_RTDI_LEN);
6191                rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa0, 20, 64, 0);
6192                if (!rval) {
6193                        memcpy(rsp_payload->optical_prod_desc.vendor_name,
6194                            sfp + 0,
6195                            sizeof(rsp_payload->optical_prod_desc.vendor_name));
6196                        memcpy(rsp_payload->optical_prod_desc.part_number,
6197                            sfp + 20,
6198                            sizeof(rsp_payload->optical_prod_desc.part_number));
6199                        memcpy(rsp_payload->optical_prod_desc.revision,
6200                            sfp + 36,
6201                            sizeof(rsp_payload->optical_prod_desc.revision));
6202                        memcpy(rsp_payload->optical_prod_desc.serial_number,
6203                            sfp + 48,
6204                            sizeof(rsp_payload->optical_prod_desc.serial_number));
6205                }
6206
6207                memset(sfp, 0, SFP_RTDI_LEN);
6208                rval = qla2x00_read_sfp(vha, sfp_dma, sfp, 0xa0, 84, 8, 0);
6209                if (!rval) {
6210                        memcpy(rsp_payload->optical_prod_desc.date,
6211                            sfp + 0,
6212                            sizeof(rsp_payload->optical_prod_desc.date));
6213                }
6214        }
6215
6216send:
6217        ql_dbg(ql_dbg_init, vha, 0x0183,
6218            "Sending ELS Response to RDP Request...\n");
6219        ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x0184,
6220            "-------- ELS RSP -------\n");
6221        ql_dump_buffer(ql_dbg_init + ql_dbg_verbose, vha, 0x0185,
6222            rsp_els, sizeof(*rsp_els));
6223        ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x0186,
6224            "-------- ELS RSP PAYLOAD -------\n");
6225        ql_dump_buffer(ql_dbg_init + ql_dbg_verbose, vha, 0x0187,
6226            rsp_payload, rsp_payload_length);
6227
6228        rval = qla2x00_issue_iocb(vha, rsp_els, rsp_els_dma, 0);
6229
6230        if (rval) {
6231                ql_log(ql_log_warn, vha, 0x0188,
6232                    "%s: iocb failed to execute -> %x\n", __func__, rval);
6233        } else if (rsp_els->comp_status) {
6234                ql_log(ql_log_warn, vha, 0x0189,
6235                    "%s: iocb failed to complete -> completion=%#x subcode=(%#x,%#x)\n",
6236                    __func__, rsp_els->comp_status,
6237                    rsp_els->error_subcode_1, rsp_els->error_subcode_2);
6238        } else {
6239                ql_dbg(ql_dbg_init, vha, 0x018a, "%s: done.\n", __func__);
6240        }
6241
6242dealloc:
6243        if (stat)
6244                dma_free_coherent(&ha->pdev->dev, sizeof(*stat),
6245                    stat, stat_dma);
6246        if (sfp)
6247                dma_free_coherent(&ha->pdev->dev, SFP_RTDI_LEN,
6248                    sfp, sfp_dma);
6249        if (rsp_payload)
6250                dma_free_coherent(&ha->pdev->dev, sizeof(*rsp_payload),
6251                    rsp_payload, rsp_payload_dma);
6252        if (rsp_els)
6253                dma_free_coherent(&ha->pdev->dev, sizeof(*rsp_els),
6254                    rsp_els, rsp_els_dma);
6255}
6256
6257void
6258qla24xx_free_purex_item(struct purex_item *item)
6259{
6260        if (item == &item->vha->default_item)
6261                memset(&item->vha->default_item, 0, sizeof(struct purex_item));
6262        else
6263                kfree(item);
6264}
6265
6266void qla24xx_process_purex_list(struct purex_list *list)
6267{
6268        struct list_head head = LIST_HEAD_INIT(head);
6269        struct purex_item *item, *next;
6270        ulong flags;
6271
6272        spin_lock_irqsave(&list->lock, flags);
6273        list_splice_init(&list->head, &head);
6274        spin_unlock_irqrestore(&list->lock, flags);
6275
6276        list_for_each_entry_safe(item, next, &head, list) {
6277                list_del(&item->list);
6278                item->process_item(item->vha, item);
6279                qla24xx_free_purex_item(item);
6280        }
6281}
6282
6283void
6284qla83xx_idc_unlock(scsi_qla_host_t *base_vha, uint16_t requester_id)
6285{
6286#if 0
6287        uint16_t options = (requester_id << 15) | BIT_7;
6288#endif
6289        uint16_t retry;
6290        uint32_t data;
6291        struct qla_hw_data *ha = base_vha->hw;
6292
6293        /* IDC-unlock implementation using driver-unlock/lock-id
6294         * remote registers
6295         */
6296        retry = 0;
6297retry_unlock:
6298        if (qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCKID, &data)
6299            == QLA_SUCCESS) {
6300                if (data == ha->portnum) {
6301                        qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_UNLOCK, &data);
6302                        /* Clearing lock-id by setting 0xff */
6303                        qla83xx_wr_reg(base_vha, QLA83XX_DRIVER_LOCKID, 0xff);
6304                } else if (retry < 10) {
6305                        /* SV: XXX: IDC unlock retrying needed here? */
6306
6307                        /* Retry for IDC-unlock */
6308                        qla83xx_wait_logic();
6309                        retry++;
6310                        ql_dbg(ql_dbg_p3p, base_vha, 0xb064,
6311                            "Failed to release IDC lock, retrying=%d\n", retry);
6312                        goto retry_unlock;
6313                }
6314        } else if (retry < 10) {
6315                /* Retry for IDC-unlock */
6316                qla83xx_wait_logic();
6317                retry++;
6318                ql_dbg(ql_dbg_p3p, base_vha, 0xb065,
6319                    "Failed to read drv-lockid, retrying=%d\n", retry);
6320                goto retry_unlock;
6321        }
6322
6323        return;
6324
6325#if 0
6326        /* XXX: IDC-unlock implementation using access-control mbx */
6327        retry = 0;
6328retry_unlock2:
6329        if (qla83xx_access_control(base_vha, options, 0, 0, NULL)) {
6330                if (retry < 10) {
6331                        /* Retry for IDC-unlock */
6332                        qla83xx_wait_logic();
6333                        retry++;
6334                        ql_dbg(ql_dbg_p3p, base_vha, 0xb066,
6335                            "Failed to release IDC lock, retrying=%d\n", retry);
6336                        goto retry_unlock2;
6337                }
6338        }
6339
6340        return;
6341#endif
6342}
6343
6344int
6345__qla83xx_set_drv_presence(scsi_qla_host_t *vha)
6346{
6347        int rval = QLA_SUCCESS;
6348        struct qla_hw_data *ha = vha->hw;
6349        uint32_t drv_presence;
6350
6351        rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6352        if (rval == QLA_SUCCESS) {
6353                drv_presence |= (1 << ha->portnum);
6354                rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRV_PRESENCE,
6355                    drv_presence);
6356        }
6357
6358        return rval;
6359}
6360
6361int
6362qla83xx_set_drv_presence(scsi_qla_host_t *vha)
6363{
6364        int rval = QLA_SUCCESS;
6365
6366        qla83xx_idc_lock(vha, 0);
6367        rval = __qla83xx_set_drv_presence(vha);
6368        qla83xx_idc_unlock(vha, 0);
6369
6370        return rval;
6371}
6372
6373int
6374__qla83xx_clear_drv_presence(scsi_qla_host_t *vha)
6375{
6376        int rval = QLA_SUCCESS;
6377        struct qla_hw_data *ha = vha->hw;
6378        uint32_t drv_presence;
6379
6380        rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6381        if (rval == QLA_SUCCESS) {
6382                drv_presence &= ~(1 << ha->portnum);
6383                rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRV_PRESENCE,
6384                    drv_presence);
6385        }
6386
6387        return rval;
6388}
6389
6390int
6391qla83xx_clear_drv_presence(scsi_qla_host_t *vha)
6392{
6393        int rval = QLA_SUCCESS;
6394
6395        qla83xx_idc_lock(vha, 0);
6396        rval = __qla83xx_clear_drv_presence(vha);
6397        qla83xx_idc_unlock(vha, 0);
6398
6399        return rval;
6400}
6401
6402static void
6403qla83xx_need_reset_handler(scsi_qla_host_t *vha)
6404{
6405        struct qla_hw_data *ha = vha->hw;
6406        uint32_t drv_ack, drv_presence;
6407        unsigned long ack_timeout;
6408
6409        /* Wait for IDC ACK from all functions (DRV-ACK == DRV-PRESENCE) */
6410        ack_timeout = jiffies + (ha->fcoe_reset_timeout * HZ);
6411        while (1) {
6412                qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6413                qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6414                if ((drv_ack & drv_presence) == drv_presence)
6415                        break;
6416
6417                if (time_after_eq(jiffies, ack_timeout)) {
6418                        ql_log(ql_log_warn, vha, 0xb067,
6419                            "RESET ACK TIMEOUT! drv_presence=0x%x "
6420                            "drv_ack=0x%x\n", drv_presence, drv_ack);
6421                        /*
6422                         * The function(s) which did not ack in time are forced
6423                         * to withdraw any further participation in the IDC
6424                         * reset.
6425                         */
6426                        if (drv_ack != drv_presence)
6427                                qla83xx_wr_reg(vha, QLA83XX_IDC_DRV_PRESENCE,
6428                                    drv_ack);
6429                        break;
6430                }
6431
6432                qla83xx_idc_unlock(vha, 0);
6433                msleep(1000);
6434                qla83xx_idc_lock(vha, 0);
6435        }
6436
6437        qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE, QLA8XXX_DEV_COLD);
6438        ql_log(ql_log_info, vha, 0xb068, "HW State: COLD/RE-INIT.\n");
6439}
6440
6441static int
6442qla83xx_device_bootstrap(scsi_qla_host_t *vha)
6443{
6444        int rval = QLA_SUCCESS;
6445        uint32_t idc_control;
6446
6447        qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE, QLA8XXX_DEV_INITIALIZING);
6448        ql_log(ql_log_info, vha, 0xb069, "HW State: INITIALIZING.\n");
6449
6450        /* Clearing IDC-Control Graceful-Reset Bit before resetting f/w */
6451        __qla83xx_get_idc_control(vha, &idc_control);
6452        idc_control &= ~QLA83XX_IDC_GRACEFUL_RESET;
6453        __qla83xx_set_idc_control(vha, 0);
6454
6455        qla83xx_idc_unlock(vha, 0);
6456        rval = qla83xx_restart_nic_firmware(vha);
6457        qla83xx_idc_lock(vha, 0);
6458
6459        if (rval != QLA_SUCCESS) {
6460                ql_log(ql_log_fatal, vha, 0xb06a,
6461                    "Failed to restart NIC f/w.\n");
6462                qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE, QLA8XXX_DEV_FAILED);
6463                ql_log(ql_log_info, vha, 0xb06b, "HW State: FAILED.\n");
6464        } else {
6465                ql_dbg(ql_dbg_p3p, vha, 0xb06c,
6466                    "Success in restarting nic f/w.\n");
6467                qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE, QLA8XXX_DEV_READY);
6468                ql_log(ql_log_info, vha, 0xb06d, "HW State: READY.\n");
6469        }
6470
6471        return rval;
6472}
6473
6474/* Assumes idc_lock always held on entry */
6475int
6476qla83xx_idc_state_handler(scsi_qla_host_t *base_vha)
6477{
6478        struct qla_hw_data *ha = base_vha->hw;
6479        int rval = QLA_SUCCESS;
6480        unsigned long dev_init_timeout;
6481        uint32_t dev_state;
6482
6483        /* Wait for MAX-INIT-TIMEOUT for the device to go ready */
6484        dev_init_timeout = jiffies + (ha->fcoe_dev_init_timeout * HZ);
6485
6486        while (1) {
6487
6488                if (time_after_eq(jiffies, dev_init_timeout)) {
6489                        ql_log(ql_log_warn, base_vha, 0xb06e,
6490                            "Initialization TIMEOUT!\n");
6491                        /* Init timeout. Disable further NIC Core
6492                         * communication.
6493                         */
6494                        qla83xx_wr_reg(base_vha, QLA83XX_IDC_DEV_STATE,
6495                                QLA8XXX_DEV_FAILED);
6496                        ql_log(ql_log_info, base_vha, 0xb06f,
6497                            "HW State: FAILED.\n");
6498                }
6499
6500                qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6501                switch (dev_state) {
6502                case QLA8XXX_DEV_READY:
6503                        if (ha->flags.nic_core_reset_owner)
6504                                qla83xx_idc_audit(base_vha,
6505                                    IDC_AUDIT_COMPLETION);
6506                        ha->flags.nic_core_reset_owner = 0;
6507                        ql_dbg(ql_dbg_p3p, base_vha, 0xb070,
6508                            "Reset_owner reset by 0x%x.\n",
6509                            ha->portnum);
6510                        goto exit;
6511                case QLA8XXX_DEV_COLD:
6512                        if (ha->flags.nic_core_reset_owner)
6513                                rval = qla83xx_device_bootstrap(base_vha);
6514                        else {
6515                        /* Wait for AEN to change device-state */
6516                                qla83xx_idc_unlock(base_vha, 0);
6517                                msleep(1000);
6518                                qla83xx_idc_lock(base_vha, 0);
6519                        }
6520                        break;
6521                case QLA8XXX_DEV_INITIALIZING:
6522                        /* Wait for AEN to change device-state */
6523                        qla83xx_idc_unlock(base_vha, 0);
6524                        msleep(1000);
6525                        qla83xx_idc_lock(base_vha, 0);
6526                        break;
6527                case QLA8XXX_DEV_NEED_RESET:
6528                        if (!ql2xdontresethba && ha->flags.nic_core_reset_owner)
6529                                qla83xx_need_reset_handler(base_vha);
6530                        else {
6531                                /* Wait for AEN to change device-state */
6532                                qla83xx_idc_unlock(base_vha, 0);
6533                                msleep(1000);
6534                                qla83xx_idc_lock(base_vha, 0);
6535                        }
6536                        /* reset timeout value after need reset handler */
6537                        dev_init_timeout = jiffies +
6538                            (ha->fcoe_dev_init_timeout * HZ);
6539                        break;
6540                case QLA8XXX_DEV_NEED_QUIESCENT:
6541                        /* XXX: DEBUG for now */
6542                        qla83xx_idc_unlock(base_vha, 0);
6543                        msleep(1000);
6544                        qla83xx_idc_lock(base_vha, 0);
6545                        break;
6546                case QLA8XXX_DEV_QUIESCENT:
6547                        /* XXX: DEBUG for now */
6548                        if (ha->flags.quiesce_owner)
6549                                goto exit;
6550
6551                        qla83xx_idc_unlock(base_vha, 0);
6552                        msleep(1000);
6553                        qla83xx_idc_lock(base_vha, 0);
6554                        dev_init_timeout = jiffies +
6555                            (ha->fcoe_dev_init_timeout * HZ);
6556                        break;
6557                case QLA8XXX_DEV_FAILED:
6558                        if (ha->flags.nic_core_reset_owner)
6559                                qla83xx_idc_audit(base_vha,
6560                                    IDC_AUDIT_COMPLETION);
6561                        ha->flags.nic_core_reset_owner = 0;
6562                        __qla83xx_clear_drv_presence(base_vha);
6563                        qla83xx_idc_unlock(base_vha, 0);
6564                        qla8xxx_dev_failed_handler(base_vha);
6565                        rval = QLA_FUNCTION_FAILED;
6566                        qla83xx_idc_lock(base_vha, 0);
6567                        goto exit;
6568                case QLA8XXX_BAD_VALUE:
6569                        qla83xx_idc_unlock(base_vha, 0);
6570                        msleep(1000);
6571                        qla83xx_idc_lock(base_vha, 0);
6572                        break;
6573                default:
6574                        ql_log(ql_log_warn, base_vha, 0xb071,
6575                            "Unknown Device State: %x.\n", dev_state);
6576                        qla83xx_idc_unlock(base_vha, 0);
6577                        qla8xxx_dev_failed_handler(base_vha);
6578                        rval = QLA_FUNCTION_FAILED;
6579                        qla83xx_idc_lock(base_vha, 0);
6580                        goto exit;
6581                }
6582        }
6583
6584exit:
6585        return rval;
6586}
6587
6588void
6589qla2x00_disable_board_on_pci_error(struct work_struct *work)
6590{
6591        struct qla_hw_data *ha = container_of(work, struct qla_hw_data,
6592            board_disable);
6593        struct pci_dev *pdev = ha->pdev;
6594        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6595
6596        ql_log(ql_log_warn, base_vha, 0x015b,
6597            "Disabling adapter.\n");
6598
6599        if (!atomic_read(&pdev->enable_cnt)) {
6600                ql_log(ql_log_info, base_vha, 0xfffc,
6601                    "PCI device disabled, no action req for PCI error=%lx\n",
6602                    base_vha->pci_flags);
6603                return;
6604        }
6605
6606        /*
6607         * if UNLOADING flag is already set, then continue unload,
6608         * where it was set first.
6609         */
6610        if (test_and_set_bit(UNLOADING, &base_vha->dpc_flags))
6611                return;
6612
6613        qla2x00_wait_for_sess_deletion(base_vha);
6614
6615        qla2x00_delete_all_vps(ha, base_vha);
6616
6617        qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
6618
6619        qla2x00_dfs_remove(base_vha);
6620
6621        qla84xx_put_chip(base_vha);
6622
6623        if (base_vha->timer_active)
6624                qla2x00_stop_timer(base_vha);
6625
6626        base_vha->flags.online = 0;
6627
6628        qla2x00_destroy_deferred_work(ha);
6629
6630        /*
6631         * Do not try to stop beacon blink as it will issue a mailbox
6632         * command.
6633         */
6634        qla2x00_free_sysfs_attr(base_vha, false);
6635
6636        fc_remove_host(base_vha->host);
6637
6638        scsi_remove_host(base_vha->host);
6639
6640        base_vha->flags.init_done = 0;
6641        qla25xx_delete_queues(base_vha);
6642        qla2x00_free_fcports(base_vha);
6643        qla2x00_free_irqs(base_vha);
6644        qla2x00_mem_free(ha);
6645        qla82xx_md_free(base_vha);
6646        qla2x00_free_queues(ha);
6647
6648        qla2x00_unmap_iobases(ha);
6649
6650        pci_release_selected_regions(ha->pdev, ha->bars);
6651        pci_disable_pcie_error_reporting(pdev);
6652        pci_disable_device(pdev);
6653
6654        /*
6655         * Let qla2x00_remove_one cleanup qla_hw_data on device removal.
6656         */
6657}
6658
6659/**************************************************************************
6660* qla2x00_do_dpc
6661*   This kernel thread is a task that is schedule by the interrupt handler
6662*   to perform the background processing for interrupts.
6663*
6664* Notes:
6665* This task always run in the context of a kernel thread.  It
6666* is kick-off by the driver's detect code and starts up
6667* up one per adapter. It immediately goes to sleep and waits for
6668* some fibre event.  When either the interrupt handler or
6669* the timer routine detects a event it will one of the task
6670* bits then wake us up.
6671**************************************************************************/
6672static int
6673qla2x00_do_dpc(void *data)
6674{
6675        scsi_qla_host_t *base_vha;
6676        struct qla_hw_data *ha;
6677        uint32_t online;
6678        struct qla_qpair *qpair;
6679
6680        ha = (struct qla_hw_data *)data;
6681        base_vha = pci_get_drvdata(ha->pdev);
6682
6683        set_user_nice(current, MIN_NICE);
6684
6685        set_current_state(TASK_INTERRUPTIBLE);
6686        while (!kthread_should_stop()) {
6687                ql_dbg(ql_dbg_dpc, base_vha, 0x4000,
6688                    "DPC handler sleeping.\n");
6689
6690                schedule();
6691
6692                if (test_and_clear_bit(DO_EEH_RECOVERY, &base_vha->dpc_flags))
6693                        qla_pci_set_eeh_busy(base_vha);
6694
6695                if (!base_vha->flags.init_done || ha->flags.mbox_busy)
6696                        goto end_loop;
6697
6698                if (ha->flags.eeh_busy) {
6699                        ql_dbg(ql_dbg_dpc, base_vha, 0x4003,
6700                            "eeh_busy=%d.\n", ha->flags.eeh_busy);
6701                        goto end_loop;
6702                }
6703
6704                ha->dpc_active = 1;
6705
6706                ql_dbg(ql_dbg_dpc + ql_dbg_verbose, base_vha, 0x4001,
6707                    "DPC handler waking up, dpc_flags=0x%lx.\n",
6708                    base_vha->dpc_flags);
6709
6710                if (test_bit(UNLOADING, &base_vha->dpc_flags))
6711                        break;
6712
6713                if (IS_P3P_TYPE(ha)) {
6714                        if (IS_QLA8044(ha)) {
6715                                if (test_and_clear_bit(ISP_UNRECOVERABLE,
6716                                        &base_vha->dpc_flags)) {
6717                                        qla8044_idc_lock(ha);
6718                                        qla8044_wr_direct(base_vha,
6719                                                QLA8044_CRB_DEV_STATE_INDEX,
6720                                                QLA8XXX_DEV_FAILED);
6721                                        qla8044_idc_unlock(ha);
6722                                        ql_log(ql_log_info, base_vha, 0x4004,
6723                                                "HW State: FAILED.\n");
6724                                        qla8044_device_state_handler(base_vha);
6725                                        continue;
6726                                }
6727
6728                        } else {
6729                                if (test_and_clear_bit(ISP_UNRECOVERABLE,
6730                                        &base_vha->dpc_flags)) {
6731                                        qla82xx_idc_lock(ha);
6732                                        qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
6733                                                QLA8XXX_DEV_FAILED);
6734                                        qla82xx_idc_unlock(ha);
6735                                        ql_log(ql_log_info, base_vha, 0x0151,
6736                                                "HW State: FAILED.\n");
6737                                        qla82xx_device_state_handler(base_vha);
6738                                        continue;
6739                                }
6740                        }
6741
6742                        if (test_and_clear_bit(FCOE_CTX_RESET_NEEDED,
6743                                &base_vha->dpc_flags)) {
6744
6745                                ql_dbg(ql_dbg_dpc, base_vha, 0x4005,
6746                                    "FCoE context reset scheduled.\n");
6747                                if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
6748                                        &base_vha->dpc_flags))) {
6749                                        if (qla82xx_fcoe_ctx_reset(base_vha)) {
6750                                                /* FCoE-ctx reset failed.
6751                                                 * Escalate to chip-reset
6752                                                 */
6753                                                set_bit(ISP_ABORT_NEEDED,
6754                                                        &base_vha->dpc_flags);
6755                                        }
6756                                        clear_bit(ABORT_ISP_ACTIVE,
6757                                                &base_vha->dpc_flags);
6758                                }
6759
6760                                ql_dbg(ql_dbg_dpc, base_vha, 0x4006,
6761                                    "FCoE context reset end.\n");
6762                        }
6763                } else if (IS_QLAFX00(ha)) {
6764                        if (test_and_clear_bit(ISP_UNRECOVERABLE,
6765                                &base_vha->dpc_flags)) {
6766                                ql_dbg(ql_dbg_dpc, base_vha, 0x4020,
6767                                    "Firmware Reset Recovery\n");
6768                                if (qlafx00_reset_initialize(base_vha)) {
6769                                        /* Failed. Abort isp later. */
6770                                        if (!test_bit(UNLOADING,
6771                                            &base_vha->dpc_flags)) {
6772                                                set_bit(ISP_UNRECOVERABLE,
6773                                                    &base_vha->dpc_flags);
6774                                                ql_dbg(ql_dbg_dpc, base_vha,
6775                                                    0x4021,
6776                                                    "Reset Recovery Failed\n");
6777                                        }
6778                                }
6779                        }
6780
6781                        if (test_and_clear_bit(FX00_TARGET_SCAN,
6782                                &base_vha->dpc_flags)) {
6783                                ql_dbg(ql_dbg_dpc, base_vha, 0x4022,
6784                                    "ISPFx00 Target Scan scheduled\n");
6785                                if (qlafx00_rescan_isp(base_vha)) {
6786                                        if (!test_bit(UNLOADING,
6787                                            &base_vha->dpc_flags))
6788                                                set_bit(ISP_UNRECOVERABLE,
6789                                                    &base_vha->dpc_flags);
6790                                        ql_dbg(ql_dbg_dpc, base_vha, 0x401e,
6791                                            "ISPFx00 Target Scan Failed\n");
6792                                }
6793                                ql_dbg(ql_dbg_dpc, base_vha, 0x401f,
6794                                    "ISPFx00 Target Scan End\n");
6795                        }
6796                        if (test_and_clear_bit(FX00_HOST_INFO_RESEND,
6797                                &base_vha->dpc_flags)) {
6798                                ql_dbg(ql_dbg_dpc, base_vha, 0x4023,
6799                                    "ISPFx00 Host Info resend scheduled\n");
6800                                qlafx00_fx_disc(base_vha,
6801                                    &base_vha->hw->mr.fcport,
6802                                    FXDISC_REG_HOST_INFO);
6803                        }
6804                }
6805
6806                if (test_and_clear_bit(DETECT_SFP_CHANGE,
6807                    &base_vha->dpc_flags)) {
6808                        /* Semantic:
6809                         *  - NO-OP -- await next ISP-ABORT. Preferred method
6810                         *             to minimize disruptions that will occur
6811                         *             when a forced chip-reset occurs.
6812                         *  - Force -- ISP-ABORT scheduled.
6813                         */
6814                        /* set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags); */
6815                }
6816
6817                if (test_and_clear_bit
6818                    (ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
6819                    !test_bit(UNLOADING, &base_vha->dpc_flags)) {
6820                        bool do_reset = true;
6821
6822                        switch (base_vha->qlini_mode) {
6823                        case QLA2XXX_INI_MODE_ENABLED:
6824                                break;
6825                        case QLA2XXX_INI_MODE_DISABLED:
6826                                if (!qla_tgt_mode_enabled(base_vha) &&
6827                                    !ha->flags.fw_started)
6828                                        do_reset = false;
6829                                break;
6830                        case QLA2XXX_INI_MODE_DUAL:
6831                                if (!qla_dual_mode_enabled(base_vha) &&
6832                                    !ha->flags.fw_started)
6833                                        do_reset = false;
6834                                break;
6835                        default:
6836                                break;
6837                        }
6838
6839                        if (do_reset && !(test_and_set_bit(ABORT_ISP_ACTIVE,
6840                            &base_vha->dpc_flags))) {
6841                                base_vha->flags.online = 1;
6842                                ql_dbg(ql_dbg_dpc, base_vha, 0x4007,
6843                                    "ISP abort scheduled.\n");
6844                                if (ha->isp_ops->abort_isp(base_vha)) {
6845                                        /* failed. retry later */
6846                                        set_bit(ISP_ABORT_NEEDED,
6847                                            &base_vha->dpc_flags);
6848                                }
6849                                clear_bit(ABORT_ISP_ACTIVE,
6850                                                &base_vha->dpc_flags);
6851                                ql_dbg(ql_dbg_dpc, base_vha, 0x4008,
6852                                    "ISP abort end.\n");
6853                        }
6854                }
6855
6856                if (test_bit(PROCESS_PUREX_IOCB, &base_vha->dpc_flags)) {
6857                        if (atomic_read(&base_vha->loop_state) == LOOP_READY) {
6858                                qla24xx_process_purex_list
6859                                        (&base_vha->purex_list);
6860                                clear_bit(PROCESS_PUREX_IOCB,
6861                                    &base_vha->dpc_flags);
6862                        }
6863                }
6864
6865                if (test_and_clear_bit(FCPORT_UPDATE_NEEDED,
6866                    &base_vha->dpc_flags)) {
6867                        qla2x00_update_fcports(base_vha);
6868                }
6869
6870                if (IS_QLAFX00(ha))
6871                        goto loop_resync_check;
6872
6873                if (test_bit(ISP_QUIESCE_NEEDED, &base_vha->dpc_flags)) {
6874                        ql_dbg(ql_dbg_dpc, base_vha, 0x4009,
6875                            "Quiescence mode scheduled.\n");
6876                        if (IS_P3P_TYPE(ha)) {
6877                                if (IS_QLA82XX(ha))
6878                                        qla82xx_device_state_handler(base_vha);
6879                                if (IS_QLA8044(ha))
6880                                        qla8044_device_state_handler(base_vha);
6881                                clear_bit(ISP_QUIESCE_NEEDED,
6882                                    &base_vha->dpc_flags);
6883                                if (!ha->flags.quiesce_owner) {
6884                                        qla2x00_perform_loop_resync(base_vha);
6885                                        if (IS_QLA82XX(ha)) {
6886                                                qla82xx_idc_lock(ha);
6887                                                qla82xx_clear_qsnt_ready(
6888                                                    base_vha);
6889                                                qla82xx_idc_unlock(ha);
6890                                        } else if (IS_QLA8044(ha)) {
6891                                                qla8044_idc_lock(ha);
6892                                                qla8044_clear_qsnt_ready(
6893                                                    base_vha);
6894                                                qla8044_idc_unlock(ha);
6895                                        }
6896                                }
6897                        } else {
6898                                clear_bit(ISP_QUIESCE_NEEDED,
6899                                    &base_vha->dpc_flags);
6900                                qla2x00_quiesce_io(base_vha);
6901                        }
6902                        ql_dbg(ql_dbg_dpc, base_vha, 0x400a,
6903                            "Quiescence mode end.\n");
6904                }
6905
6906                if (test_and_clear_bit(RESET_MARKER_NEEDED,
6907                                &base_vha->dpc_flags) &&
6908                    (!(test_and_set_bit(RESET_ACTIVE, &base_vha->dpc_flags)))) {
6909
6910                        ql_dbg(ql_dbg_dpc, base_vha, 0x400b,
6911                            "Reset marker scheduled.\n");
6912                        qla2x00_rst_aen(base_vha);
6913                        clear_bit(RESET_ACTIVE, &base_vha->dpc_flags);
6914                        ql_dbg(ql_dbg_dpc, base_vha, 0x400c,
6915                            "Reset marker end.\n");
6916                }
6917
6918                /* Retry each device up to login retry count */
6919                if (test_bit(RELOGIN_NEEDED, &base_vha->dpc_flags) &&
6920                    !test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags) &&
6921                    atomic_read(&base_vha->loop_state) != LOOP_DOWN) {
6922
6923                        if (!base_vha->relogin_jif ||
6924                            time_after_eq(jiffies, base_vha->relogin_jif)) {
6925                                base_vha->relogin_jif = jiffies + HZ;
6926                                clear_bit(RELOGIN_NEEDED, &base_vha->dpc_flags);
6927
6928                                ql_dbg(ql_dbg_disc, base_vha, 0x400d,
6929                                    "Relogin scheduled.\n");
6930                                qla24xx_post_relogin_work(base_vha);
6931                        }
6932                }
6933loop_resync_check:
6934                if (test_and_clear_bit(LOOP_RESYNC_NEEDED,
6935                    &base_vha->dpc_flags)) {
6936
6937                        ql_dbg(ql_dbg_dpc, base_vha, 0x400f,
6938                            "Loop resync scheduled.\n");
6939
6940                        if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
6941                            &base_vha->dpc_flags))) {
6942
6943                                qla2x00_loop_resync(base_vha);
6944
6945                                clear_bit(LOOP_RESYNC_ACTIVE,
6946                                                &base_vha->dpc_flags);
6947                        }
6948
6949                        ql_dbg(ql_dbg_dpc, base_vha, 0x4010,
6950                            "Loop resync end.\n");
6951                }
6952
6953                if (IS_QLAFX00(ha))
6954                        goto intr_on_check;
6955
6956                if (test_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags) &&
6957                    atomic_read(&base_vha->loop_state) == LOOP_READY) {
6958                        clear_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags);
6959                        qla2xxx_flash_npiv_conf(base_vha);
6960                }
6961
6962intr_on_check:
6963                if (!ha->interrupts_on)
6964                        ha->isp_ops->enable_intrs(ha);
6965
6966                if (test_and_clear_bit(BEACON_BLINK_NEEDED,
6967                                        &base_vha->dpc_flags)) {
6968                        if (ha->beacon_blink_led == 1)
6969                                ha->isp_ops->beacon_blink(base_vha);
6970                }
6971
6972                /* qpair online check */
6973                if (test_and_clear_bit(QPAIR_ONLINE_CHECK_NEEDED,
6974                    &base_vha->dpc_flags)) {
6975                        if (ha->flags.eeh_busy ||
6976                            ha->flags.pci_channel_io_perm_failure)
6977                                online = 0;
6978                        else
6979                                online = 1;
6980
6981                        mutex_lock(&ha->mq_lock);
6982                        list_for_each_entry(qpair, &base_vha->qp_list,
6983                            qp_list_elem)
6984                        qpair->online = online;
6985                        mutex_unlock(&ha->mq_lock);
6986                }
6987
6988                if (test_and_clear_bit(SET_ZIO_THRESHOLD_NEEDED,
6989                                       &base_vha->dpc_flags)) {
6990                        u16 threshold = ha->nvme_last_rptd_aen + ha->last_zio_threshold;
6991
6992                        if (threshold > ha->orig_fw_xcb_count)
6993                                threshold = ha->orig_fw_xcb_count;
6994
6995                        ql_log(ql_log_info, base_vha, 0xffffff,
6996                               "SET ZIO Activity exchange threshold to %d.\n",
6997                               threshold);
6998                        if (qla27xx_set_zio_threshold(base_vha, threshold)) {
6999                                ql_log(ql_log_info, base_vha, 0xffffff,
7000                                       "Unable to SET ZIO Activity exchange threshold to %d.\n",
7001                                       threshold);
7002                        }
7003                }
7004
7005                if (!IS_QLAFX00(ha))
7006                        qla2x00_do_dpc_all_vps(base_vha);
7007
7008                if (test_and_clear_bit(N2N_LINK_RESET,
7009                        &base_vha->dpc_flags)) {
7010                        qla2x00_lip_reset(base_vha);
7011                }
7012
7013                if (test_bit(HEARTBEAT_CHK, &base_vha->dpc_flags)) {
7014                        /*
7015                         * if there is a mb in progress then that's
7016                         * enough of a check to see if fw is still ticking.
7017                         */
7018                        if (!ha->flags.mbox_busy && base_vha->flags.init_done)
7019                                qla_no_op_mb(base_vha);
7020
7021                        clear_bit(HEARTBEAT_CHK, &base_vha->dpc_flags);
7022                }
7023
7024                ha->dpc_active = 0;
7025end_loop:
7026                set_current_state(TASK_INTERRUPTIBLE);
7027        } /* End of while(1) */
7028        __set_current_state(TASK_RUNNING);
7029
7030        ql_dbg(ql_dbg_dpc, base_vha, 0x4011,
7031            "DPC handler exiting.\n");
7032
7033        /*
7034         * Make sure that nobody tries to wake us up again.
7035         */
7036        ha->dpc_active = 0;
7037
7038        /* Cleanup any residual CTX SRBs. */
7039        qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
7040
7041        return 0;
7042}
7043
7044void
7045qla2xxx_wake_dpc(struct scsi_qla_host *vha)
7046{
7047        struct qla_hw_data *ha = vha->hw;
7048        struct task_struct *t = ha->dpc_thread;
7049
7050        if (!test_bit(UNLOADING, &vha->dpc_flags) && t)
7051                wake_up_process(t);
7052}
7053
7054/*
7055*  qla2x00_rst_aen
7056*      Processes asynchronous reset.
7057*
7058* Input:
7059*      ha  = adapter block pointer.
7060*/
7061static void
7062qla2x00_rst_aen(scsi_qla_host_t *vha)
7063{
7064        if (vha->flags.online && !vha->flags.reset_active &&
7065            !atomic_read(&vha->loop_down_timer) &&
7066            !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))) {
7067                do {
7068                        clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
7069
7070                        /*
7071                         * Issue marker command only when we are going to start
7072                         * the I/O.
7073                         */
7074                        vha->marker_needed = 1;
7075                } while (!atomic_read(&vha->loop_down_timer) &&
7076                    (test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags)));
7077        }
7078}
7079
7080static bool qla_do_heartbeat(struct scsi_qla_host *vha)
7081{
7082        u64 cmd_cnt, prev_cmd_cnt;
7083        bool do_hb = false;
7084        struct qla_hw_data *ha = vha->hw;
7085        int i;
7086
7087        /* if cmds are still pending down in fw, then do hb */
7088        if (ha->base_qpair->cmd_cnt != ha->base_qpair->cmd_completion_cnt) {
7089                do_hb = true;
7090                goto skip;
7091        }
7092
7093        for (i = 0; i < ha->max_qpairs; i++) {
7094                if (ha->queue_pair_map[i] &&
7095                    ha->queue_pair_map[i]->cmd_cnt !=
7096                    ha->queue_pair_map[i]->cmd_completion_cnt) {
7097                        do_hb = true;
7098                        break;
7099                }
7100        }
7101
7102skip:
7103        prev_cmd_cnt = ha->prev_cmd_cnt;
7104        cmd_cnt = ha->base_qpair->cmd_cnt;
7105        for (i = 0; i < ha->max_qpairs; i++) {
7106                if (ha->queue_pair_map[i])
7107                        cmd_cnt += ha->queue_pair_map[i]->cmd_cnt;
7108        }
7109        ha->prev_cmd_cnt = cmd_cnt;
7110
7111        if (!do_hb && ((cmd_cnt - prev_cmd_cnt) > 50))
7112                /*
7113                 * IOs are completing before periodic hb check.
7114                 * IOs seems to be running, do hb for sanity check.
7115                 */
7116                do_hb = true;
7117
7118        return do_hb;
7119}
7120
7121static void qla_heart_beat(struct scsi_qla_host *vha)
7122{
7123        if (vha->vp_idx)
7124                return;
7125
7126        if (vha->hw->flags.eeh_busy || qla2x00_chip_is_down(vha))
7127                return;
7128
7129        if (qla_do_heartbeat(vha)) {
7130                set_bit(HEARTBEAT_CHK, &vha->dpc_flags);
7131                qla2xxx_wake_dpc(vha);
7132        }
7133}
7134
7135/**************************************************************************
7136*   qla2x00_timer
7137*
7138* Description:
7139*   One second timer
7140*
7141* Context: Interrupt
7142***************************************************************************/
7143void
7144qla2x00_timer(struct timer_list *t)
7145{
7146        scsi_qla_host_t *vha = from_timer(vha, t, timer);
7147        unsigned long   cpu_flags = 0;
7148        int             start_dpc = 0;
7149        int             index;
7150        srb_t           *sp;
7151        uint16_t        w;
7152        struct qla_hw_data *ha = vha->hw;
7153        struct req_que *req;
7154        unsigned long flags;
7155        fc_port_t *fcport = NULL;
7156
7157        if (ha->flags.eeh_busy) {
7158                ql_dbg(ql_dbg_timer, vha, 0x6000,
7159                    "EEH = %d, restarting timer.\n",
7160                    ha->flags.eeh_busy);
7161                qla2x00_restart_timer(vha, WATCH_INTERVAL);
7162                return;
7163        }
7164
7165        /*
7166         * Hardware read to raise pending EEH errors during mailbox waits. If
7167         * the read returns -1 then disable the board.
7168         */
7169        if (!pci_channel_offline(ha->pdev)) {
7170                pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
7171                qla2x00_check_reg16_for_disconnect(vha, w);
7172        }
7173
7174        /* Make sure qla82xx_watchdog is run only for physical port */
7175        if (!vha->vp_idx && IS_P3P_TYPE(ha)) {
7176                if (test_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags))
7177                        start_dpc++;
7178                if (IS_QLA82XX(ha))
7179                        qla82xx_watchdog(vha);
7180                else if (IS_QLA8044(ha))
7181                        qla8044_watchdog(vha);
7182        }
7183
7184        if (!vha->vp_idx && IS_QLAFX00(ha))
7185                qlafx00_timer_routine(vha);
7186
7187        if (vha->link_down_time < QLA2XX_MAX_LINK_DOWN_TIME)
7188                vha->link_down_time++;
7189
7190        spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
7191        list_for_each_entry(fcport, &vha->vp_fcports, list) {
7192                if (fcport->tgt_link_down_time < QLA2XX_MAX_LINK_DOWN_TIME)
7193                        fcport->tgt_link_down_time++;
7194        }
7195        spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
7196
7197        /* Loop down handler. */
7198        if (atomic_read(&vha->loop_down_timer) > 0 &&
7199            !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) &&
7200            !(test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags))
7201                && vha->flags.online) {
7202
7203                if (atomic_read(&vha->loop_down_timer) ==
7204                    vha->loop_down_abort_time) {
7205
7206                        ql_log(ql_log_info, vha, 0x6008,
7207                            "Loop down - aborting the queues before time expires.\n");
7208
7209                        if (!IS_QLA2100(ha) && vha->link_down_timeout)
7210                                atomic_set(&vha->loop_state, LOOP_DEAD);
7211
7212                        /*
7213                         * Schedule an ISP abort to return any FCP2-device
7214                         * commands.
7215                         */
7216                        /* NPIV - scan physical port only */
7217                        if (!vha->vp_idx) {
7218                                spin_lock_irqsave(&ha->hardware_lock,
7219                                    cpu_flags);
7220                                req = ha->req_q_map[0];
7221                                for (index = 1;
7222                                    index < req->num_outstanding_cmds;
7223                                    index++) {
7224                                        fc_port_t *sfcp;
7225
7226                                        sp = req->outstanding_cmds[index];
7227                                        if (!sp)
7228                                                continue;
7229                                        if (sp->cmd_type != TYPE_SRB)
7230                                                continue;
7231                                        if (sp->type != SRB_SCSI_CMD)
7232                                                continue;
7233                                        sfcp = sp->fcport;
7234                                        if (!(sfcp->flags & FCF_FCP2_DEVICE))
7235                                                continue;
7236
7237                                        if (IS_QLA82XX(ha))
7238                                                set_bit(FCOE_CTX_RESET_NEEDED,
7239                                                        &vha->dpc_flags);
7240                                        else
7241                                                set_bit(ISP_ABORT_NEEDED,
7242                                                        &vha->dpc_flags);
7243                                        break;
7244                                }
7245                                spin_unlock_irqrestore(&ha->hardware_lock,
7246                                                                cpu_flags);
7247                        }
7248                        start_dpc++;
7249                }
7250
7251                /* if the loop has been down for 4 minutes, reinit adapter */
7252                if (atomic_dec_and_test(&vha->loop_down_timer) != 0) {
7253                        if (!(vha->device_flags & DFLG_NO_CABLE)) {
7254                                ql_log(ql_log_warn, vha, 0x6009,
7255                                    "Loop down - aborting ISP.\n");
7256
7257                                if (IS_QLA82XX(ha))
7258                                        set_bit(FCOE_CTX_RESET_NEEDED,
7259                                                &vha->dpc_flags);
7260                                else
7261                                        set_bit(ISP_ABORT_NEEDED,
7262                                                &vha->dpc_flags);
7263                        }
7264                }
7265                ql_dbg(ql_dbg_timer, vha, 0x600a,
7266                    "Loop down - seconds remaining %d.\n",
7267                    atomic_read(&vha->loop_down_timer));
7268        }
7269        /* Check if beacon LED needs to be blinked for physical host only */
7270        if (!vha->vp_idx && (ha->beacon_blink_led == 1)) {
7271                /* There is no beacon_blink function for ISP82xx */
7272                if (!IS_P3P_TYPE(ha)) {
7273                        set_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags);
7274                        start_dpc++;
7275                }
7276        }
7277
7278        /* check if edif running */
7279        if (vha->hw->flags.edif_enabled)
7280                qla_edif_timer(vha);
7281
7282        /* Process any deferred work. */
7283        if (!list_empty(&vha->work_list)) {
7284                unsigned long flags;
7285                bool q = false;
7286
7287                spin_lock_irqsave(&vha->work_lock, flags);
7288                if (!test_and_set_bit(IOCB_WORK_ACTIVE, &vha->dpc_flags))
7289                        q = true;
7290                spin_unlock_irqrestore(&vha->work_lock, flags);
7291                if (q)
7292                        queue_work(vha->hw->wq, &vha->iocb_work);
7293        }
7294
7295        /*
7296         * FC-NVME
7297         * see if the active AEN count has changed from what was last reported.
7298         */
7299        index = atomic_read(&ha->nvme_active_aen_cnt);
7300        if (!vha->vp_idx &&
7301            (index != ha->nvme_last_rptd_aen) &&
7302            ha->zio_mode == QLA_ZIO_MODE_6 &&
7303            !ha->flags.host_shutting_down) {
7304                ha->nvme_last_rptd_aen = atomic_read(&ha->nvme_active_aen_cnt);
7305                ql_log(ql_log_info, vha, 0x3002,
7306                    "nvme: Sched: Set ZIO exchange threshold to %d.\n",
7307                    ha->nvme_last_rptd_aen);
7308                set_bit(SET_ZIO_THRESHOLD_NEEDED, &vha->dpc_flags);
7309                start_dpc++;
7310        }
7311
7312        if (!vha->vp_idx &&
7313            atomic_read(&ha->zio_threshold) != ha->last_zio_threshold &&
7314            IS_ZIO_THRESHOLD_CAPABLE(ha)) {
7315                ql_log(ql_log_info, vha, 0x3002,
7316                    "Sched: Set ZIO exchange threshold to %d.\n",
7317                    ha->last_zio_threshold);
7318                ha->last_zio_threshold = atomic_read(&ha->zio_threshold);
7319                set_bit(SET_ZIO_THRESHOLD_NEEDED, &vha->dpc_flags);
7320                start_dpc++;
7321        }
7322
7323        /* Schedule the DPC routine if needed */
7324        if ((test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
7325            test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
7326            test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags) ||
7327            start_dpc ||
7328            test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) ||
7329            test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags) ||
7330            test_bit(ISP_UNRECOVERABLE, &vha->dpc_flags) ||
7331            test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) ||
7332            test_bit(VP_DPC_NEEDED, &vha->dpc_flags) ||
7333            test_bit(RELOGIN_NEEDED, &vha->dpc_flags) ||
7334            test_bit(PROCESS_PUREX_IOCB, &vha->dpc_flags))) {
7335                ql_dbg(ql_dbg_timer, vha, 0x600b,
7336                    "isp_abort_needed=%d loop_resync_needed=%d "
7337                    "fcport_update_needed=%d start_dpc=%d "
7338                    "reset_marker_needed=%d",
7339                    test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags),
7340                    test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags),
7341                    test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags),
7342                    start_dpc,
7343                    test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags));
7344                ql_dbg(ql_dbg_timer, vha, 0x600c,
7345                    "beacon_blink_needed=%d isp_unrecoverable=%d "
7346                    "fcoe_ctx_reset_needed=%d vp_dpc_needed=%d "
7347                    "relogin_needed=%d, Process_purex_iocb=%d.\n",
7348                    test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags),
7349                    test_bit(ISP_UNRECOVERABLE, &vha->dpc_flags),
7350                    test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags),
7351                    test_bit(VP_DPC_NEEDED, &vha->dpc_flags),
7352                    test_bit(RELOGIN_NEEDED, &vha->dpc_flags),
7353                    test_bit(PROCESS_PUREX_IOCB, &vha->dpc_flags));
7354                qla2xxx_wake_dpc(vha);
7355        }
7356
7357        qla_heart_beat(vha);
7358
7359        qla2x00_restart_timer(vha, WATCH_INTERVAL);
7360}
7361
7362/* Firmware interface routines. */
7363
7364#define FW_ISP21XX      0
7365#define FW_ISP22XX      1
7366#define FW_ISP2300      2
7367#define FW_ISP2322      3
7368#define FW_ISP24XX      4
7369#define FW_ISP25XX      5
7370#define FW_ISP81XX      6
7371#define FW_ISP82XX      7
7372#define FW_ISP2031      8
7373#define FW_ISP8031      9
7374#define FW_ISP27XX      10
7375#define FW_ISP28XX      11
7376
7377#define FW_FILE_ISP21XX "ql2100_fw.bin"
7378#define FW_FILE_ISP22XX "ql2200_fw.bin"
7379#define FW_FILE_ISP2300 "ql2300_fw.bin"
7380#define FW_FILE_ISP2322 "ql2322_fw.bin"
7381#define FW_FILE_ISP24XX "ql2400_fw.bin"
7382#define FW_FILE_ISP25XX "ql2500_fw.bin"
7383#define FW_FILE_ISP81XX "ql8100_fw.bin"
7384#define FW_FILE_ISP82XX "ql8200_fw.bin"
7385#define FW_FILE_ISP2031 "ql2600_fw.bin"
7386#define FW_FILE_ISP8031 "ql8300_fw.bin"
7387#define FW_FILE_ISP27XX "ql2700_fw.bin"
7388#define FW_FILE_ISP28XX "ql2800_fw.bin"
7389
7390
7391static DEFINE_MUTEX(qla_fw_lock);
7392
7393static struct fw_blob qla_fw_blobs[] = {
7394        { .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
7395        { .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
7396        { .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
7397        { .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
7398        { .name = FW_FILE_ISP24XX, },
7399        { .name = FW_FILE_ISP25XX, },
7400        { .name = FW_FILE_ISP81XX, },
7401        { .name = FW_FILE_ISP82XX, },
7402        { .name = FW_FILE_ISP2031, },
7403        { .name = FW_FILE_ISP8031, },
7404        { .name = FW_FILE_ISP27XX, },
7405        { .name = FW_FILE_ISP28XX, },
7406        { .name = NULL, },
7407};
7408
7409struct fw_blob *
7410qla2x00_request_firmware(scsi_qla_host_t *vha)
7411{
7412        struct qla_hw_data *ha = vha->hw;
7413        struct fw_blob *blob;
7414
7415        if (IS_QLA2100(ha)) {
7416                blob = &qla_fw_blobs[FW_ISP21XX];
7417        } else if (IS_QLA2200(ha)) {
7418                blob = &qla_fw_blobs[FW_ISP22XX];
7419        } else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
7420                blob = &qla_fw_blobs[FW_ISP2300];
7421        } else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
7422                blob = &qla_fw_blobs[FW_ISP2322];
7423        } else if (IS_QLA24XX_TYPE(ha)) {
7424                blob = &qla_fw_blobs[FW_ISP24XX];
7425        } else if (IS_QLA25XX(ha)) {
7426                blob = &qla_fw_blobs[FW_ISP25XX];
7427        } else if (IS_QLA81XX(ha)) {
7428                blob = &qla_fw_blobs[FW_ISP81XX];
7429        } else if (IS_QLA82XX(ha)) {
7430                blob = &qla_fw_blobs[FW_ISP82XX];
7431        } else if (IS_QLA2031(ha)) {
7432                blob = &qla_fw_blobs[FW_ISP2031];
7433        } else if (IS_QLA8031(ha)) {
7434                blob = &qla_fw_blobs[FW_ISP8031];
7435        } else if (IS_QLA27XX(ha)) {
7436                blob = &qla_fw_blobs[FW_ISP27XX];
7437        } else if (IS_QLA28XX(ha)) {
7438                blob = &qla_fw_blobs[FW_ISP28XX];
7439        } else {
7440                return NULL;
7441        }
7442
7443        if (!blob->name)
7444                return NULL;
7445
7446        mutex_lock(&qla_fw_lock);
7447        if (blob->fw)
7448                goto out;
7449
7450        if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
7451                ql_log(ql_log_warn, vha, 0x0063,
7452                    "Failed to load firmware image (%s).\n", blob->name);
7453                blob->fw = NULL;
7454                blob = NULL;
7455        }
7456
7457out:
7458        mutex_unlock(&qla_fw_lock);
7459        return blob;
7460}
7461
7462static void
7463qla2x00_release_firmware(void)
7464{
7465        struct fw_blob *blob;
7466
7467        mutex_lock(&qla_fw_lock);
7468        for (blob = qla_fw_blobs; blob->name; blob++)
7469                release_firmware(blob->fw);
7470        mutex_unlock(&qla_fw_lock);
7471}
7472
7473static void qla_pci_error_cleanup(scsi_qla_host_t *vha)
7474{
7475        struct qla_hw_data *ha = vha->hw;
7476        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
7477        struct qla_qpair *qpair = NULL;
7478        struct scsi_qla_host *vp, *tvp;
7479        fc_port_t *fcport;
7480        int i;
7481        unsigned long flags;
7482
7483        ql_dbg(ql_dbg_aer, vha, 0x9000,
7484               "%s\n", __func__);
7485        ha->chip_reset++;
7486
7487        ha->base_qpair->chip_reset = ha->chip_reset;
7488        for (i = 0; i < ha->max_qpairs; i++) {
7489                if (ha->queue_pair_map[i])
7490                        ha->queue_pair_map[i]->chip_reset =
7491                            ha->base_qpair->chip_reset;
7492        }
7493
7494        /*
7495         * purge mailbox might take a while. Slot Reset/chip reset
7496         * will take care of the purge
7497         */
7498
7499        mutex_lock(&ha->mq_lock);
7500        ha->base_qpair->online = 0;
7501        list_for_each_entry(qpair, &base_vha->qp_list, qp_list_elem)
7502                qpair->online = 0;
7503        wmb();
7504        mutex_unlock(&ha->mq_lock);
7505
7506        qla2x00_mark_all_devices_lost(vha);
7507
7508        spin_lock_irqsave(&ha->vport_slock, flags);
7509        list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
7510                atomic_inc(&vp->vref_count);
7511                spin_unlock_irqrestore(&ha->vport_slock, flags);
7512                qla2x00_mark_all_devices_lost(vp);
7513                spin_lock_irqsave(&ha->vport_slock, flags);
7514                atomic_dec(&vp->vref_count);
7515        }
7516        spin_unlock_irqrestore(&ha->vport_slock, flags);
7517
7518        /* Clear all async request states across all VPs. */
7519        list_for_each_entry(fcport, &vha->vp_fcports, list)
7520                fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
7521
7522        spin_lock_irqsave(&ha->vport_slock, flags);
7523        list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
7524                atomic_inc(&vp->vref_count);
7525                spin_unlock_irqrestore(&ha->vport_slock, flags);
7526                list_for_each_entry(fcport, &vp->vp_fcports, list)
7527                        fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
7528                spin_lock_irqsave(&ha->vport_slock, flags);
7529                atomic_dec(&vp->vref_count);
7530        }
7531        spin_unlock_irqrestore(&ha->vport_slock, flags);
7532}
7533
7534
7535static pci_ers_result_t
7536qla2xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7537{
7538        scsi_qla_host_t *vha = pci_get_drvdata(pdev);
7539        struct qla_hw_data *ha = vha->hw;
7540        pci_ers_result_t ret = PCI_ERS_RESULT_NEED_RESET;
7541
7542        ql_log(ql_log_warn, vha, 0x9000,
7543               "PCI error detected, state %x.\n", state);
7544        ha->pci_error_state = QLA_PCI_ERR_DETECTED;
7545
7546        if (!atomic_read(&pdev->enable_cnt)) {
7547                ql_log(ql_log_info, vha, 0xffff,
7548                        "PCI device is disabled,state %x\n", state);
7549                ret = PCI_ERS_RESULT_NEED_RESET;
7550                goto out;
7551        }
7552
7553        switch (state) {
7554        case pci_channel_io_normal:
7555                ha->flags.eeh_busy = 0;
7556                if (ql2xmqsupport || ql2xnvmeenable) {
7557                        set_bit(QPAIR_ONLINE_CHECK_NEEDED, &vha->dpc_flags);
7558                        qla2xxx_wake_dpc(vha);
7559                }
7560                ret = PCI_ERS_RESULT_CAN_RECOVER;
7561                break;
7562        case pci_channel_io_frozen:
7563                qla_pci_set_eeh_busy(vha);
7564                ret = PCI_ERS_RESULT_NEED_RESET;
7565                break;
7566        case pci_channel_io_perm_failure:
7567                ha->flags.pci_channel_io_perm_failure = 1;
7568                qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
7569                if (ql2xmqsupport || ql2xnvmeenable) {
7570                        set_bit(QPAIR_ONLINE_CHECK_NEEDED, &vha->dpc_flags);
7571                        qla2xxx_wake_dpc(vha);
7572                }
7573                ret = PCI_ERS_RESULT_DISCONNECT;
7574        }
7575out:
7576        ql_dbg(ql_dbg_aer, vha, 0x600d,
7577               "PCI error detected returning [%x].\n", ret);
7578        return ret;
7579}
7580
7581static pci_ers_result_t
7582qla2xxx_pci_mmio_enabled(struct pci_dev *pdev)
7583{
7584        int risc_paused = 0;
7585        uint32_t stat;
7586        unsigned long flags;
7587        scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
7588        struct qla_hw_data *ha = base_vha->hw;
7589        struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
7590        struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
7591
7592        ql_log(ql_log_warn, base_vha, 0x9000,
7593               "mmio enabled\n");
7594
7595        ha->pci_error_state = QLA_PCI_MMIO_ENABLED;
7596        if (IS_QLA82XX(ha))
7597                return PCI_ERS_RESULT_RECOVERED;
7598
7599        spin_lock_irqsave(&ha->hardware_lock, flags);
7600        if (IS_QLA2100(ha) || IS_QLA2200(ha)){
7601                stat = rd_reg_word(&reg->hccr);
7602                if (stat & HCCR_RISC_PAUSE)
7603                        risc_paused = 1;
7604        } else if (IS_QLA23XX(ha)) {
7605                stat = rd_reg_dword(&reg->u.isp2300.host_status);
7606                if (stat & HSR_RISC_PAUSED)
7607                        risc_paused = 1;
7608        } else if (IS_FWI2_CAPABLE(ha)) {
7609                stat = rd_reg_dword(&reg24->host_status);
7610                if (stat & HSRX_RISC_PAUSED)
7611                        risc_paused = 1;
7612        }
7613        spin_unlock_irqrestore(&ha->hardware_lock, flags);
7614
7615        if (risc_paused) {
7616                ql_log(ql_log_info, base_vha, 0x9003,
7617                    "RISC paused -- mmio_enabled, Dumping firmware.\n");
7618                qla2xxx_dump_fw(base_vha);
7619        }
7620        /* set PCI_ERS_RESULT_NEED_RESET to trigger call to qla2xxx_pci_slot_reset */
7621        ql_dbg(ql_dbg_aer, base_vha, 0x600d,
7622               "mmio enabled returning.\n");
7623        return PCI_ERS_RESULT_NEED_RESET;
7624}
7625
7626static pci_ers_result_t
7627qla2xxx_pci_slot_reset(struct pci_dev *pdev)
7628{
7629        pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
7630        scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
7631        struct qla_hw_data *ha = base_vha->hw;
7632        int rc;
7633        struct qla_qpair *qpair = NULL;
7634
7635        ql_log(ql_log_warn, base_vha, 0x9004,
7636               "Slot Reset.\n");
7637
7638        ha->pci_error_state = QLA_PCI_SLOT_RESET;
7639        /* Workaround: qla2xxx driver which access hardware earlier
7640         * needs error state to be pci_channel_io_online.
7641         * Otherwise mailbox command timesout.
7642         */
7643        pdev->error_state = pci_channel_io_normal;
7644
7645        pci_restore_state(pdev);
7646
7647        /* pci_restore_state() clears the saved_state flag of the device
7648         * save restored state which resets saved_state flag
7649         */
7650        pci_save_state(pdev);
7651
7652        if (ha->mem_only)
7653                rc = pci_enable_device_mem(pdev);
7654        else
7655                rc = pci_enable_device(pdev);
7656
7657        if (rc) {
7658                ql_log(ql_log_warn, base_vha, 0x9005,
7659                    "Can't re-enable PCI device after reset.\n");
7660                goto exit_slot_reset;
7661        }
7662
7663
7664        if (ha->isp_ops->pci_config(base_vha))
7665                goto exit_slot_reset;
7666
7667        mutex_lock(&ha->mq_lock);
7668        list_for_each_entry(qpair, &base_vha->qp_list, qp_list_elem)
7669                qpair->online = 1;
7670        mutex_unlock(&ha->mq_lock);
7671
7672        ha->flags.eeh_busy = 0;
7673        base_vha->flags.online = 1;
7674        set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
7675        ha->isp_ops->abort_isp(base_vha);
7676        clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
7677
7678        if (qla2x00_isp_reg_stat(ha)) {
7679                ha->flags.eeh_busy = 1;
7680                qla_pci_error_cleanup(base_vha);
7681                ql_log(ql_log_warn, base_vha, 0x9005,
7682                       "Device unable to recover from PCI error.\n");
7683        } else {
7684                ret =  PCI_ERS_RESULT_RECOVERED;
7685        }
7686
7687exit_slot_reset:
7688        ql_dbg(ql_dbg_aer, base_vha, 0x900e,
7689            "Slot Reset returning %x.\n", ret);
7690
7691        return ret;
7692}
7693
7694static void
7695qla2xxx_pci_resume(struct pci_dev *pdev)
7696{
7697        scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
7698        struct qla_hw_data *ha = base_vha->hw;
7699        int ret;
7700
7701        ql_log(ql_log_warn, base_vha, 0x900f,
7702               "Pci Resume.\n");
7703
7704
7705        ret = qla2x00_wait_for_hba_online(base_vha);
7706        if (ret != QLA_SUCCESS) {
7707                ql_log(ql_log_fatal, base_vha, 0x9002,
7708                    "The device failed to resume I/O from slot/link_reset.\n");
7709        }
7710        ha->pci_error_state = QLA_PCI_RESUME;
7711        ql_dbg(ql_dbg_aer, base_vha, 0x600d,
7712               "Pci Resume returning.\n");
7713}
7714
7715void qla_pci_set_eeh_busy(struct scsi_qla_host *vha)
7716{
7717        struct qla_hw_data *ha = vha->hw;
7718        struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
7719        bool do_cleanup = false;
7720        unsigned long flags;
7721
7722        if (ha->flags.eeh_busy)
7723                return;
7724
7725        spin_lock_irqsave(&base_vha->work_lock, flags);
7726        if (!ha->flags.eeh_busy) {
7727                ha->flags.eeh_busy = 1;
7728                do_cleanup = true;
7729        }
7730        spin_unlock_irqrestore(&base_vha->work_lock, flags);
7731
7732        if (do_cleanup)
7733                qla_pci_error_cleanup(base_vha);
7734}
7735
7736/*
7737 * this routine will schedule a task to pause IO from interrupt context
7738 * if caller sees a PCIE error event (register read = 0xf's)
7739 */
7740void qla_schedule_eeh_work(struct scsi_qla_host *vha)
7741{
7742        struct qla_hw_data *ha = vha->hw;
7743        struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
7744
7745        if (ha->flags.eeh_busy)
7746                return;
7747
7748        set_bit(DO_EEH_RECOVERY, &base_vha->dpc_flags);
7749        qla2xxx_wake_dpc(base_vha);
7750}
7751
7752static void
7753qla_pci_reset_prepare(struct pci_dev *pdev)
7754{
7755        scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
7756        struct qla_hw_data *ha = base_vha->hw;
7757        struct qla_qpair *qpair;
7758
7759        ql_log(ql_log_warn, base_vha, 0xffff,
7760            "%s.\n", __func__);
7761
7762        /*
7763         * PCI FLR/function reset is about to reset the
7764         * slot. Stop the chip to stop all DMA access.
7765         * It is assumed that pci_reset_done will be called
7766         * after FLR to resume Chip operation.
7767         */
7768        ha->flags.eeh_busy = 1;
7769        mutex_lock(&ha->mq_lock);
7770        list_for_each_entry(qpair, &base_vha->qp_list, qp_list_elem)
7771                qpair->online = 0;
7772        mutex_unlock(&ha->mq_lock);
7773
7774        set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
7775        qla2x00_abort_isp_cleanup(base_vha);
7776        qla2x00_abort_all_cmds(base_vha, DID_RESET << 16);
7777}
7778
7779static void
7780qla_pci_reset_done(struct pci_dev *pdev)
7781{
7782        scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
7783        struct qla_hw_data *ha = base_vha->hw;
7784        struct qla_qpair *qpair;
7785
7786        ql_log(ql_log_warn, base_vha, 0xffff,
7787            "%s.\n", __func__);
7788
7789        /*
7790         * FLR just completed by PCI layer. Resume adapter
7791         */
7792        ha->flags.eeh_busy = 0;
7793        mutex_lock(&ha->mq_lock);
7794        list_for_each_entry(qpair, &base_vha->qp_list, qp_list_elem)
7795                qpair->online = 1;
7796        mutex_unlock(&ha->mq_lock);
7797
7798        base_vha->flags.online = 1;
7799        ha->isp_ops->abort_isp(base_vha);
7800        clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
7801}
7802
7803static int qla2xxx_map_queues(struct Scsi_Host *shost)
7804{
7805        int rc;
7806        scsi_qla_host_t *vha = (scsi_qla_host_t *)shost->hostdata;
7807        struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
7808
7809        if (USER_CTRL_IRQ(vha->hw) || !vha->hw->mqiobase)
7810                rc = blk_mq_map_queues(qmap);
7811        else
7812                rc = blk_mq_pci_map_queues(qmap, vha->hw->pdev, vha->irq_offset);
7813        return rc;
7814}
7815
7816struct scsi_host_template qla2xxx_driver_template = {
7817        .module                 = THIS_MODULE,
7818        .name                   = QLA2XXX_DRIVER_NAME,
7819        .queuecommand           = qla2xxx_queuecommand,
7820
7821        .eh_timed_out           = fc_eh_timed_out,
7822        .eh_abort_handler       = qla2xxx_eh_abort,
7823        .eh_should_retry_cmd    = fc_eh_should_retry_cmd,
7824        .eh_device_reset_handler = qla2xxx_eh_device_reset,
7825        .eh_target_reset_handler = qla2xxx_eh_target_reset,
7826        .eh_bus_reset_handler   = qla2xxx_eh_bus_reset,
7827        .eh_host_reset_handler  = qla2xxx_eh_host_reset,
7828
7829        .slave_configure        = qla2xxx_slave_configure,
7830
7831        .slave_alloc            = qla2xxx_slave_alloc,
7832        .slave_destroy          = qla2xxx_slave_destroy,
7833        .scan_finished          = qla2xxx_scan_finished,
7834        .scan_start             = qla2xxx_scan_start,
7835        .change_queue_depth     = scsi_change_queue_depth,
7836        .map_queues             = qla2xxx_map_queues,
7837        .this_id                = -1,
7838        .cmd_per_lun            = 3,
7839        .use_clustering         = ENABLE_CLUSTERING,
7840        .sg_tablesize           = SG_ALL,
7841
7842        .max_sectors            = 0xFFFF,
7843        .shost_attrs            = qla2x00_host_attrs,
7844
7845        .supported_mode         = MODE_INITIATOR,
7846        .track_queue_depth      = 1,
7847        .cmd_size               = sizeof(srb_t),
7848};
7849
7850static const struct pci_error_handlers qla2xxx_err_handler = {
7851        .error_detected = qla2xxx_pci_error_detected,
7852        .mmio_enabled = qla2xxx_pci_mmio_enabled,
7853        .slot_reset = qla2xxx_pci_slot_reset,
7854        .resume = qla2xxx_pci_resume,
7855        .reset_prepare = qla_pci_reset_prepare,
7856        .reset_done = qla_pci_reset_done,
7857};
7858
7859static struct pci_device_id qla2xxx_pci_tbl[] = {
7860        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2532) },
7861        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2031) },
7862        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8031) },
7863        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2071) },
7864        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2271) },
7865        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2261) },
7866        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2061) },
7867        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2081) },
7868        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2281) },
7869        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2089) },
7870        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2289) },
7871        { 0 },
7872};
7873MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
7874
7875static struct pci_driver qla2xxx_pci_driver = {
7876        .name           = QLA2XXX_DRIVER_NAME,
7877        .driver         = {
7878                .owner          = THIS_MODULE,
7879        },
7880        .id_table       = qla2xxx_pci_tbl,
7881        .probe          = qla2x00_probe_one,
7882        .remove         = qla2x00_remove_one,
7883        .shutdown       = qla2x00_shutdown,
7884        .err_handler    = &qla2xxx_err_handler,
7885};
7886
7887static const struct file_operations apidev_fops = {
7888        .owner = THIS_MODULE,
7889        .llseek = noop_llseek,
7890};
7891
7892/**
7893 * qla2x00_module_init - Module initialization.
7894 **/
7895static int __init
7896qla2x00_module_init(void)
7897{
7898        int ret = 0;
7899
7900        BUILD_BUG_ON(sizeof(cmd_a64_entry_t) != 64);
7901        BUILD_BUG_ON(sizeof(cmd_entry_t) != 64);
7902        BUILD_BUG_ON(sizeof(cont_a64_entry_t) != 64);
7903        BUILD_BUG_ON(sizeof(cont_entry_t) != 64);
7904        BUILD_BUG_ON(sizeof(init_cb_t) != 96);
7905        BUILD_BUG_ON(sizeof(mrk_entry_t) != 64);
7906        BUILD_BUG_ON(sizeof(ms_iocb_entry_t) != 64);
7907        BUILD_BUG_ON(sizeof(request_t) != 64);
7908        BUILD_BUG_ON(sizeof(struct abort_entry_24xx) != 64);
7909        BUILD_BUG_ON(sizeof(struct abort_iocb_entry_fx00) != 64);
7910        BUILD_BUG_ON(sizeof(struct abts_entry_24xx) != 64);
7911        BUILD_BUG_ON(sizeof(struct access_chip_84xx) != 64);
7912        BUILD_BUG_ON(sizeof(struct access_chip_rsp_84xx) != 64);
7913        BUILD_BUG_ON(sizeof(struct cmd_bidir) != 64);
7914        BUILD_BUG_ON(sizeof(struct cmd_nvme) != 64);
7915        BUILD_BUG_ON(sizeof(struct cmd_type_6) != 64);
7916        BUILD_BUG_ON(sizeof(struct cmd_type_7) != 64);
7917        BUILD_BUG_ON(sizeof(struct cmd_type_7_fx00) != 64);
7918        BUILD_BUG_ON(sizeof(struct cmd_type_crc_2) != 64);
7919        BUILD_BUG_ON(sizeof(struct ct_entry_24xx) != 64);
7920        BUILD_BUG_ON(sizeof(struct ct_fdmi1_hba_attributes) != 2604);
7921        BUILD_BUG_ON(sizeof(struct ct_fdmi2_hba_attributes) != 4424);
7922        BUILD_BUG_ON(sizeof(struct ct_fdmi2_port_attributes) != 4164);
7923        BUILD_BUG_ON(sizeof(struct ct_fdmi_hba_attr) != 260);
7924        BUILD_BUG_ON(sizeof(struct ct_fdmi_port_attr) != 260);
7925        BUILD_BUG_ON(sizeof(struct ct_rsp_hdr) != 16);
7926        BUILD_BUG_ON(sizeof(struct ctio_crc2_to_fw) != 64);
7927        BUILD_BUG_ON(sizeof(struct device_reg_24xx) != 256);
7928        BUILD_BUG_ON(sizeof(struct device_reg_25xxmq) != 24);
7929        BUILD_BUG_ON(sizeof(struct device_reg_2xxx) != 256);
7930        BUILD_BUG_ON(sizeof(struct device_reg_82xx) != 1288);
7931        BUILD_BUG_ON(sizeof(struct device_reg_fx00) != 216);
7932        BUILD_BUG_ON(sizeof(struct els_entry_24xx) != 64);
7933        BUILD_BUG_ON(sizeof(struct els_sts_entry_24xx) != 64);
7934        BUILD_BUG_ON(sizeof(struct fxdisc_entry_fx00) != 64);
7935        BUILD_BUG_ON(sizeof(struct imm_ntfy_from_isp) != 64);
7936        BUILD_BUG_ON(sizeof(struct init_cb_24xx) != 128);
7937        BUILD_BUG_ON(sizeof(struct init_cb_81xx) != 128);
7938        BUILD_BUG_ON(sizeof(struct logio_entry_24xx) != 64);
7939        BUILD_BUG_ON(sizeof(struct mbx_entry) != 64);
7940        BUILD_BUG_ON(sizeof(struct mid_init_cb_24xx) != 5252);
7941        BUILD_BUG_ON(sizeof(struct mrk_entry_24xx) != 64);
7942        BUILD_BUG_ON(sizeof(struct nvram_24xx) != 512);
7943        BUILD_BUG_ON(sizeof(struct nvram_81xx) != 512);
7944        BUILD_BUG_ON(sizeof(struct pt_ls4_request) != 64);
7945        BUILD_BUG_ON(sizeof(struct pt_ls4_rx_unsol) != 64);
7946        BUILD_BUG_ON(sizeof(struct purex_entry_24xx) != 64);
7947        BUILD_BUG_ON(sizeof(struct qla2100_fw_dump) != 123634);
7948        BUILD_BUG_ON(sizeof(struct qla2300_fw_dump) != 136100);
7949        BUILD_BUG_ON(sizeof(struct qla24xx_fw_dump) != 37976);
7950        BUILD_BUG_ON(sizeof(struct qla25xx_fw_dump) != 39228);
7951        BUILD_BUG_ON(sizeof(struct qla2xxx_fce_chain) != 52);
7952        BUILD_BUG_ON(sizeof(struct qla2xxx_fw_dump) != 136172);
7953        BUILD_BUG_ON(sizeof(struct qla2xxx_mq_chain) != 524);
7954        BUILD_BUG_ON(sizeof(struct qla2xxx_mqueue_chain) != 8);
7955        BUILD_BUG_ON(sizeof(struct qla2xxx_mqueue_header) != 12);
7956        BUILD_BUG_ON(sizeof(struct qla2xxx_offld_chain) != 24);
7957        BUILD_BUG_ON(sizeof(struct qla81xx_fw_dump) != 39420);
7958        BUILD_BUG_ON(sizeof(struct qla82xx_uri_data_desc) != 28);
7959        BUILD_BUG_ON(sizeof(struct qla82xx_uri_table_desc) != 32);
7960        BUILD_BUG_ON(sizeof(struct qla83xx_fw_dump) != 51196);
7961        BUILD_BUG_ON(sizeof(struct qla_fcp_prio_cfg) != FCP_PRIO_CFG_SIZE);
7962        BUILD_BUG_ON(sizeof(struct qla_fdt_layout) != 128);
7963        BUILD_BUG_ON(sizeof(struct qla_flt_header) != 8);
7964        BUILD_BUG_ON(sizeof(struct qla_flt_region) != 16);
7965        BUILD_BUG_ON(sizeof(struct qla_npiv_entry) != 24);
7966        BUILD_BUG_ON(sizeof(struct qla_npiv_header) != 16);
7967        BUILD_BUG_ON(sizeof(struct rdp_rsp_payload) != 336);
7968        BUILD_BUG_ON(sizeof(struct sns_cmd_pkt) != 2064);
7969        BUILD_BUG_ON(sizeof(struct sts_entry_24xx) != 64);
7970        BUILD_BUG_ON(sizeof(struct tsk_mgmt_entry) != 64);
7971        BUILD_BUG_ON(sizeof(struct tsk_mgmt_entry_fx00) != 64);
7972        BUILD_BUG_ON(sizeof(struct verify_chip_entry_84xx) != 64);
7973        BUILD_BUG_ON(sizeof(struct verify_chip_rsp_84xx) != 52);
7974        BUILD_BUG_ON(sizeof(struct vf_evfp_entry_24xx) != 56);
7975        BUILD_BUG_ON(sizeof(struct vp_config_entry_24xx) != 64);
7976        BUILD_BUG_ON(sizeof(struct vp_ctrl_entry_24xx) != 64);
7977        BUILD_BUG_ON(sizeof(struct vp_rpt_id_entry_24xx) != 64);
7978        BUILD_BUG_ON(sizeof(sts21_entry_t) != 64);
7979        BUILD_BUG_ON(sizeof(sts22_entry_t) != 64);
7980        BUILD_BUG_ON(sizeof(sts_cont_entry_t) != 64);
7981        BUILD_BUG_ON(sizeof(sts_entry_t) != 64);
7982        BUILD_BUG_ON(sizeof(sw_info_t) != 32);
7983        BUILD_BUG_ON(sizeof(target_id_t) != 2);
7984
7985        /* Allocate cache for SRBs. */
7986        srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
7987            SLAB_HWCACHE_ALIGN, NULL);
7988        if (srb_cachep == NULL) {
7989                ql_log(ql_log_fatal, NULL, 0x0001,
7990                    "Unable to allocate SRB cache...Failing load!.\n");
7991                return -ENOMEM;
7992        }
7993
7994        /* Initialize target kmem_cache and mem_pools */
7995        ret = qlt_init();
7996        if (ret < 0) {
7997                goto destroy_cache;
7998        } else if (ret > 0) {
7999                /*
8000                 * If initiator mode is explictly disabled by qlt_init(),
8001                 * prevent scsi_transport_fc.c:fc_scsi_scan_rport() from
8002                 * performing scsi_scan_target() during LOOP UP event.
8003                 */
8004                qla2xxx_transport_functions.disable_target_scan = 1;
8005                qla2xxx_transport_vport_functions.disable_target_scan = 1;
8006        }
8007
8008        /* Derive version string. */
8009        strcpy(qla2x00_version_str, QLA2XXX_VERSION);
8010        if (ql2xextended_error_logging)
8011                strcat(qla2x00_version_str, "-debug");
8012        if (ql2xextended_error_logging == 1)
8013                ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
8014
8015        if (ql2x_ini_mode == QLA2XXX_INI_MODE_DUAL)
8016                qla_insert_tgt_attrs();
8017
8018        qla2xxx_transport_template =
8019            fc_attach_transport(&qla2xxx_transport_functions);
8020        if (!qla2xxx_transport_template) {
8021                ql_log(ql_log_fatal, NULL, 0x0002,
8022                    "fc_attach_transport failed...Failing load!.\n");
8023                ret = -ENODEV;
8024                goto qlt_exit;
8025        }
8026
8027        apidev_major = register_chrdev(0, QLA2XXX_APIDEV, &apidev_fops);
8028        if (apidev_major < 0) {
8029                ql_log(ql_log_fatal, NULL, 0x0003,
8030                    "Unable to register char device %s.\n", QLA2XXX_APIDEV);
8031        }
8032
8033        qla2xxx_transport_vport_template =
8034            fc_attach_transport(&qla2xxx_transport_vport_functions);
8035        if (!qla2xxx_transport_vport_template) {
8036                ql_log(ql_log_fatal, NULL, 0x0004,
8037                    "fc_attach_transport vport failed...Failing load!.\n");
8038                ret = -ENODEV;
8039                goto unreg_chrdev;
8040        }
8041        ql_log(ql_log_info, NULL, 0x0005,
8042            "QLogic Fibre Channel HBA Driver: %s.\n",
8043            qla2x00_version_str);
8044        ret = pci_register_driver(&qla2xxx_pci_driver);
8045        if (ret) {
8046                ql_log(ql_log_fatal, NULL, 0x0006,
8047                    "pci_register_driver failed...ret=%d Failing load!.\n",
8048                    ret);
8049                goto release_vport_transport;
8050        }
8051        return ret;
8052
8053release_vport_transport:
8054        fc_release_transport(qla2xxx_transport_vport_template);
8055
8056unreg_chrdev:
8057        if (apidev_major >= 0)
8058                unregister_chrdev(apidev_major, QLA2XXX_APIDEV);
8059        fc_release_transport(qla2xxx_transport_template);
8060
8061qlt_exit:
8062        qlt_exit();
8063
8064destroy_cache:
8065        kmem_cache_destroy(srb_cachep);
8066        return ret;
8067}
8068
8069/**
8070 * qla2x00_module_exit - Module cleanup.
8071 **/
8072static void __exit
8073qla2x00_module_exit(void)
8074{
8075        pci_unregister_driver(&qla2xxx_pci_driver);
8076        qla2x00_release_firmware();
8077        kmem_cache_destroy(ctx_cachep);
8078        fc_release_transport(qla2xxx_transport_vport_template);
8079        if (apidev_major >= 0)
8080                unregister_chrdev(apidev_major, QLA2XXX_APIDEV);
8081        fc_release_transport(qla2xxx_transport_template);
8082        qlt_exit();
8083        kmem_cache_destroy(srb_cachep);
8084}
8085
8086module_init(qla2x00_module_init);
8087module_exit(qla2x00_module_exit);
8088
8089MODULE_AUTHOR("QLogic Corporation");
8090MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
8091MODULE_LICENSE("GPL");
8092MODULE_VERSION(QLA2XXX_VERSION);
8093MODULE_FIRMWARE(FW_FILE_ISP21XX);
8094MODULE_FIRMWARE(FW_FILE_ISP22XX);
8095MODULE_FIRMWARE(FW_FILE_ISP2300);
8096MODULE_FIRMWARE(FW_FILE_ISP2322);
8097MODULE_FIRMWARE(FW_FILE_ISP24XX);
8098MODULE_FIRMWARE(FW_FILE_ISP25XX);
8099