linux/drivers/scsi/qla2xxx/qla_mid.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#include "qla_gbl.h"
   9#include "qla_target.h"
  10
  11#include <linux/moduleparam.h>
  12#include <linux/vmalloc.h>
  13#include <linux/slab.h>
  14#include <linux/list.h>
  15
  16#include <scsi/scsi_tcq.h>
  17#include <scsi/scsicam.h>
  18#include <linux/delay.h>
  19
  20void
  21qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
  22{
  23        if (vha->vp_idx && vha->timer_active) {
  24                del_timer_sync(&vha->timer);
  25                vha->timer_active = 0;
  26        }
  27}
  28
  29static uint32_t
  30qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
  31{
  32        uint32_t vp_id;
  33        struct qla_hw_data *ha = vha->hw;
  34        unsigned long flags;
  35
  36        /* Find an empty slot and assign an vp_id */
  37        mutex_lock(&ha->vport_lock);
  38        vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
  39        if (vp_id > ha->max_npiv_vports) {
  40                ql_dbg(ql_dbg_vport, vha, 0xa000,
  41                    "vp_id %d is bigger than max-supported %d.\n",
  42                    vp_id, ha->max_npiv_vports);
  43                mutex_unlock(&ha->vport_lock);
  44                return vp_id;
  45        }
  46
  47        set_bit(vp_id, ha->vp_idx_map);
  48        ha->num_vhosts++;
  49        vha->vp_idx = vp_id;
  50
  51        spin_lock_irqsave(&ha->vport_slock, flags);
  52        list_add_tail(&vha->list, &ha->vp_list);
  53        spin_unlock_irqrestore(&ha->vport_slock, flags);
  54
  55        spin_lock_irqsave(&ha->hardware_lock, flags);
  56        qlt_update_vp_map(vha, SET_VP_IDX);
  57        spin_unlock_irqrestore(&ha->hardware_lock, flags);
  58
  59        mutex_unlock(&ha->vport_lock);
  60        return vp_id;
  61}
  62
  63void
  64qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
  65{
  66        uint16_t vp_id;
  67        struct qla_hw_data *ha = vha->hw;
  68        unsigned long flags = 0;
  69
  70        mutex_lock(&ha->vport_lock);
  71        /*
  72         * Wait for all pending activities to finish before removing vport from
  73         * the list.
  74         * Lock needs to be held for safe removal from the list (it
  75         * ensures no active vp_list traversal while the vport is removed
  76         * from the queue)
  77         */
  78        wait_event_timeout(vha->vref_waitq, !atomic_read(&vha->vref_count),
  79            10*HZ);
  80
  81        spin_lock_irqsave(&ha->vport_slock, flags);
  82        if (atomic_read(&vha->vref_count)) {
  83                ql_dbg(ql_dbg_vport, vha, 0xfffa,
  84                    "vha->vref_count=%u timeout\n", vha->vref_count.counter);
  85                vha->vref_count = (atomic_t)ATOMIC_INIT(0);
  86        }
  87        list_del(&vha->list);
  88        qlt_update_vp_map(vha, RESET_VP_IDX);
  89        spin_unlock_irqrestore(&ha->vport_slock, flags);
  90
  91        vp_id = vha->vp_idx;
  92        ha->num_vhosts--;
  93        clear_bit(vp_id, ha->vp_idx_map);
  94
  95        mutex_unlock(&ha->vport_lock);
  96}
  97
  98static scsi_qla_host_t *
  99qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
 100{
 101        scsi_qla_host_t *vha;
 102        struct scsi_qla_host *tvha;
 103        unsigned long flags;
 104
 105        spin_lock_irqsave(&ha->vport_slock, flags);
 106        /* Locate matching device in database. */
 107        list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
 108                if (!memcmp(port_name, vha->port_name, WWN_SIZE)) {
 109                        spin_unlock_irqrestore(&ha->vport_slock, flags);
 110                        return vha;
 111                }
 112        }
 113        spin_unlock_irqrestore(&ha->vport_slock, flags);
 114        return NULL;
 115}
 116
 117/*
 118 * qla2x00_mark_vp_devices_dead
 119 *      Updates fcport state when device goes offline.
 120 *
 121 * Input:
 122 *      ha = adapter block pointer.
 123 *      fcport = port structure pointer.
 124 *
 125 * Return:
 126 *      None.
 127 *
 128 * Context:
 129 */
 130static void
 131qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
 132{
 133        /*
 134         * !!! NOTE !!!
 135         * This function, if called in contexts other than vp create, disable
 136         * or delete, please make sure this is synchronized with the
 137         * delete thread.
 138         */
 139        fc_port_t *fcport;
 140
 141        list_for_each_entry(fcport, &vha->vp_fcports, list) {
 142                ql_dbg(ql_dbg_vport, vha, 0xa001,
 143                    "Marking port dead, loop_id=0x%04x : %x.\n",
 144                    fcport->loop_id, fcport->vha->vp_idx);
 145
 146                qla2x00_mark_device_lost(vha, fcport, 0, 0);
 147                qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
 148        }
 149}
 150
 151int
 152qla24xx_disable_vp(scsi_qla_host_t *vha)
 153{
 154        unsigned long flags;
 155        int ret;
 156
 157        ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
 158        atomic_set(&vha->loop_state, LOOP_DOWN);
 159        atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
 160
 161        /* Remove port id from vp target map */
 162        spin_lock_irqsave(&vha->hw->hardware_lock, flags);
 163        qlt_update_vp_map(vha, RESET_AL_PA);
 164        spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
 165
 166        qla2x00_mark_vp_devices_dead(vha);
 167        atomic_set(&vha->vp_state, VP_FAILED);
 168        vha->flags.management_server_logged_in = 0;
 169        if (ret == QLA_SUCCESS) {
 170                fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
 171        } else {
 172                fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
 173                return -1;
 174        }
 175        return 0;
 176}
 177
 178int
 179qla24xx_enable_vp(scsi_qla_host_t *vha)
 180{
 181        int ret;
 182        struct qla_hw_data *ha = vha->hw;
 183        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
 184
 185        /* Check if physical ha port is Up */
 186        if (atomic_read(&base_vha->loop_state) == LOOP_DOWN  ||
 187                atomic_read(&base_vha->loop_state) == LOOP_DEAD ||
 188                !(ha->current_topology & ISP_CFG_F)) {
 189                vha->vp_err_state =  VP_ERR_PORTDWN;
 190                fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
 191                ql_dbg(ql_dbg_taskm, vha, 0x800b,
 192                    "%s skip enable. loop_state %x topo %x\n",
 193                    __func__, base_vha->loop_state.counter,
 194                    ha->current_topology);
 195
 196                goto enable_failed;
 197        }
 198
 199        /* Initialize the new vport unless it is a persistent port */
 200        mutex_lock(&ha->vport_lock);
 201        ret = qla24xx_modify_vp_config(vha);
 202        mutex_unlock(&ha->vport_lock);
 203
 204        if (ret != QLA_SUCCESS) {
 205                fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
 206                goto enable_failed;
 207        }
 208
 209        ql_dbg(ql_dbg_taskm, vha, 0x801a,
 210            "Virtual port with id: %d - Enabled.\n", vha->vp_idx);
 211        return 0;
 212
 213enable_failed:
 214        ql_dbg(ql_dbg_taskm, vha, 0x801b,
 215            "Virtual port with id: %d - Disabled.\n", vha->vp_idx);
 216        return 1;
 217}
 218
 219static void
 220qla24xx_configure_vp(scsi_qla_host_t *vha)
 221{
 222        struct fc_vport *fc_vport;
 223        int ret;
 224
 225        fc_vport = vha->fc_vport;
 226
 227        ql_dbg(ql_dbg_vport, vha, 0xa002,
 228            "%s: change request #3.\n", __func__);
 229        ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
 230        if (ret != QLA_SUCCESS) {
 231                ql_dbg(ql_dbg_vport, vha, 0xa003, "Failed to enable "
 232                    "receiving of RSCN requests: 0x%x.\n", ret);
 233                return;
 234        } else {
 235                /* Corresponds to SCR enabled */
 236                clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
 237        }
 238
 239        vha->flags.online = 1;
 240        if (qla24xx_configure_vhba(vha))
 241                return;
 242
 243        atomic_set(&vha->vp_state, VP_ACTIVE);
 244        fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
 245}
 246
 247void
 248qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
 249{
 250        scsi_qla_host_t *vha;
 251        struct qla_hw_data *ha = rsp->hw;
 252        int i = 0;
 253        unsigned long flags;
 254
 255        spin_lock_irqsave(&ha->vport_slock, flags);
 256        list_for_each_entry(vha, &ha->vp_list, list) {
 257                if (vha->vp_idx) {
 258                        atomic_inc(&vha->vref_count);
 259                        spin_unlock_irqrestore(&ha->vport_slock, flags);
 260
 261                        switch (mb[0]) {
 262                        case MBA_LIP_OCCURRED:
 263                        case MBA_LOOP_UP:
 264                        case MBA_LOOP_DOWN:
 265                        case MBA_LIP_RESET:
 266                        case MBA_POINT_TO_POINT:
 267                        case MBA_CHG_IN_CONNECTION:
 268                                ql_dbg(ql_dbg_async, vha, 0x5024,
 269                                    "Async_event for VP[%d], mb=0x%x vha=%p.\n",
 270                                    i, *mb, vha);
 271                                qla2x00_async_event(vha, rsp, mb);
 272                                break;
 273                        case MBA_PORT_UPDATE:
 274                        case MBA_RSCN_UPDATE:
 275                                if ((mb[3] & 0xff) == vha->vp_idx) {
 276                                        ql_dbg(ql_dbg_async, vha, 0x5024,
 277                                            "Async_event for VP[%d], mb=0x%x vha=%p\n",
 278                                            i, *mb, vha);
 279                                        qla2x00_async_event(vha, rsp, mb);
 280                                }
 281                                break;
 282                        }
 283
 284                        spin_lock_irqsave(&ha->vport_slock, flags);
 285                        atomic_dec(&vha->vref_count);
 286                        wake_up(&vha->vref_waitq);
 287                }
 288                i++;
 289        }
 290        spin_unlock_irqrestore(&ha->vport_slock, flags);
 291}
 292
 293int
 294qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
 295{
 296        /*
 297         * Physical port will do most of the abort and recovery work. We can
 298         * just treat it as a loop down
 299         */
 300        if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
 301                atomic_set(&vha->loop_state, LOOP_DOWN);
 302                qla2x00_mark_all_devices_lost(vha, 0);
 303        } else {
 304                if (!atomic_read(&vha->loop_down_timer))
 305                        atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
 306        }
 307
 308        /*
 309         * To exclusively reset vport, we need to log it out first.  Note: this
 310         * control_vp can fail if ISP reset is already issued, this is
 311         * expected, as the vp would be already logged out due to ISP reset.
 312         */
 313        if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
 314                qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
 315
 316        ql_dbg(ql_dbg_taskm, vha, 0x801d,
 317            "Scheduling enable of Vport %d.\n", vha->vp_idx);
 318        return qla24xx_enable_vp(vha);
 319}
 320
 321static int
 322qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
 323{
 324        struct qla_hw_data *ha = vha->hw;
 325        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
 326
 327        ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x4012,
 328            "Entering %s vp_flags: 0x%lx.\n", __func__, vha->vp_flags);
 329
 330        /* Check if Fw is ready to configure VP first */
 331        if (test_bit(VP_CONFIG_OK, &base_vha->vp_flags)) {
 332                if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
 333                        /* VP acquired. complete port configuration */
 334                        ql_dbg(ql_dbg_dpc, vha, 0x4014,
 335                            "Configure VP scheduled.\n");
 336                        qla24xx_configure_vp(vha);
 337                        ql_dbg(ql_dbg_dpc, vha, 0x4015,
 338                            "Configure VP end.\n");
 339                        return 0;
 340                }
 341        }
 342
 343        if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
 344                ql_dbg(ql_dbg_dpc, vha, 0x4016,
 345                    "FCPort update scheduled.\n");
 346                qla2x00_update_fcports(vha);
 347                clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
 348                ql_dbg(ql_dbg_dpc, vha, 0x4017,
 349                    "FCPort update end.\n");
 350        }
 351
 352        if (test_bit(RELOGIN_NEEDED, &vha->dpc_flags) &&
 353            !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
 354            atomic_read(&vha->loop_state) != LOOP_DOWN) {
 355
 356                if (!vha->relogin_jif ||
 357                    time_after_eq(jiffies, vha->relogin_jif)) {
 358                        vha->relogin_jif = jiffies + HZ;
 359                        clear_bit(RELOGIN_NEEDED, &vha->dpc_flags);
 360
 361                        ql_dbg(ql_dbg_dpc, vha, 0x4018,
 362                            "Relogin needed scheduled.\n");
 363                        qla24xx_post_relogin_work(vha);
 364                }
 365        }
 366
 367        if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
 368            (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
 369                clear_bit(RESET_ACTIVE, &vha->dpc_flags);
 370        }
 371
 372        if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
 373                if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
 374                        ql_dbg(ql_dbg_dpc, vha, 0x401a,
 375                            "Loop resync scheduled.\n");
 376                        qla2x00_loop_resync(vha);
 377                        clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
 378                        ql_dbg(ql_dbg_dpc, vha, 0x401b,
 379                            "Loop resync end.\n");
 380                }
 381        }
 382
 383        ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x401c,
 384            "Exiting %s.\n", __func__);
 385        return 0;
 386}
 387
 388void
 389qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
 390{
 391        struct qla_hw_data *ha = vha->hw;
 392        scsi_qla_host_t *vp;
 393        unsigned long flags = 0;
 394
 395        if (vha->vp_idx)
 396                return;
 397        if (list_empty(&ha->vp_list))
 398                return;
 399
 400        clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
 401
 402        if (!(ha->current_topology & ISP_CFG_F))
 403                return;
 404
 405        spin_lock_irqsave(&ha->vport_slock, flags);
 406        list_for_each_entry(vp, &ha->vp_list, list) {
 407                if (vp->vp_idx) {
 408                        atomic_inc(&vp->vref_count);
 409                        spin_unlock_irqrestore(&ha->vport_slock, flags);
 410
 411                        qla2x00_do_dpc_vp(vp);
 412
 413                        spin_lock_irqsave(&ha->vport_slock, flags);
 414                        atomic_dec(&vp->vref_count);
 415                }
 416        }
 417        spin_unlock_irqrestore(&ha->vport_slock, flags);
 418}
 419
 420int
 421qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
 422{
 423        scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
 424        struct qla_hw_data *ha = base_vha->hw;
 425        scsi_qla_host_t *vha;
 426        uint8_t port_name[WWN_SIZE];
 427
 428        if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
 429                return VPCERR_UNSUPPORTED;
 430
 431        /* Check up the F/W and H/W support NPIV */
 432        if (!ha->flags.npiv_supported)
 433                return VPCERR_UNSUPPORTED;
 434
 435        /* Check up whether npiv supported switch presented */
 436        if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
 437                return VPCERR_NO_FABRIC_SUPP;
 438
 439        /* Check up unique WWPN */
 440        u64_to_wwn(fc_vport->port_name, port_name);
 441        if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
 442                return VPCERR_BAD_WWN;
 443        vha = qla24xx_find_vhost_by_name(ha, port_name);
 444        if (vha)
 445                return VPCERR_BAD_WWN;
 446
 447        /* Check up max-npiv-supports */
 448        if (ha->num_vhosts > ha->max_npiv_vports) {
 449                ql_dbg(ql_dbg_vport, vha, 0xa004,
 450                    "num_vhosts %ud is bigger "
 451                    "than max_npiv_vports %ud.\n",
 452                    ha->num_vhosts, ha->max_npiv_vports);
 453                return VPCERR_UNSUPPORTED;
 454        }
 455        return 0;
 456}
 457
 458scsi_qla_host_t *
 459qla24xx_create_vhost(struct fc_vport *fc_vport)
 460{
 461        scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
 462        struct qla_hw_data *ha = base_vha->hw;
 463        scsi_qla_host_t *vha;
 464        struct scsi_host_template *sht = &qla2xxx_driver_template;
 465        struct Scsi_Host *host;
 466
 467        vha = qla2x00_create_host(sht, ha);
 468        if (!vha) {
 469                ql_log(ql_log_warn, vha, 0xa005,
 470                    "scsi_host_alloc() failed for vport.\n");
 471                return(NULL);
 472        }
 473
 474        host = vha->host;
 475        fc_vport->dd_data = vha;
 476        /* New host info */
 477        u64_to_wwn(fc_vport->node_name, vha->node_name);
 478        u64_to_wwn(fc_vport->port_name, vha->port_name);
 479
 480        vha->fc_vport = fc_vport;
 481        vha->device_flags = 0;
 482        vha->vp_idx = qla24xx_allocate_vp_id(vha);
 483        if (vha->vp_idx > ha->max_npiv_vports) {
 484                ql_dbg(ql_dbg_vport, vha, 0xa006,
 485                    "Couldn't allocate vp_id.\n");
 486                goto create_vhost_failed;
 487        }
 488        vha->mgmt_svr_loop_id = NPH_MGMT_SERVER;
 489
 490        vha->dpc_flags = 0L;
 491
 492        /*
 493         * To fix the issue of processing a parent's RSCN for the vport before
 494         * its SCR is complete.
 495         */
 496        set_bit(VP_SCR_NEEDED, &vha->vp_flags);
 497        atomic_set(&vha->loop_state, LOOP_DOWN);
 498        atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
 499
 500        qla2x00_start_timer(vha, WATCH_INTERVAL);
 501
 502        vha->req = base_vha->req;
 503        host->can_queue = base_vha->req->length + 128;
 504        host->cmd_per_lun = 3;
 505        if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif)
 506                host->max_cmd_len = 32;
 507        else
 508                host->max_cmd_len = MAX_CMDSZ;
 509        host->max_channel = MAX_BUSES - 1;
 510        host->max_lun = ql2xmaxlun;
 511        host->unique_id = host->host_no;
 512        host->max_id = ha->max_fibre_devices;
 513        host->transportt = qla2xxx_transport_vport_template;
 514
 515        ql_dbg(ql_dbg_vport, vha, 0xa007,
 516            "Detect vport hba %ld at address = %p.\n",
 517            vha->host_no, vha);
 518
 519        vha->flags.init_done = 1;
 520
 521        mutex_lock(&ha->vport_lock);
 522        set_bit(vha->vp_idx, ha->vp_idx_map);
 523        ha->cur_vport_count++;
 524        mutex_unlock(&ha->vport_lock);
 525
 526        return vha;
 527
 528create_vhost_failed:
 529        return NULL;
 530}
 531
 532static void
 533qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
 534{
 535        struct qla_hw_data *ha = vha->hw;
 536        uint16_t que_id = req->id;
 537
 538        dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
 539                sizeof(request_t), req->ring, req->dma);
 540        req->ring = NULL;
 541        req->dma = 0;
 542        if (que_id) {
 543                ha->req_q_map[que_id] = NULL;
 544                mutex_lock(&ha->vport_lock);
 545                clear_bit(que_id, ha->req_qid_map);
 546                mutex_unlock(&ha->vport_lock);
 547        }
 548        kfree(req->outstanding_cmds);
 549        kfree(req);
 550        req = NULL;
 551}
 552
 553static void
 554qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
 555{
 556        struct qla_hw_data *ha = vha->hw;
 557        uint16_t que_id = rsp->id;
 558
 559        if (rsp->msix && rsp->msix->have_irq) {
 560                free_irq(rsp->msix->vector, rsp->msix->handle);
 561                rsp->msix->have_irq = 0;
 562                rsp->msix->in_use = 0;
 563                rsp->msix->handle = NULL;
 564        }
 565        dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
 566                sizeof(response_t), rsp->ring, rsp->dma);
 567        rsp->ring = NULL;
 568        rsp->dma = 0;
 569        if (que_id) {
 570                ha->rsp_q_map[que_id] = NULL;
 571                mutex_lock(&ha->vport_lock);
 572                clear_bit(que_id, ha->rsp_qid_map);
 573                mutex_unlock(&ha->vport_lock);
 574        }
 575        kfree(rsp);
 576        rsp = NULL;
 577}
 578
 579int
 580qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
 581{
 582        int ret = QLA_SUCCESS;
 583
 584        if (req && vha->flags.qpairs_req_created) {
 585                req->options |= BIT_0;
 586                ret = qla25xx_init_req_que(vha, req);
 587                if (ret != QLA_SUCCESS)
 588                        return QLA_FUNCTION_FAILED;
 589
 590                qla25xx_free_req_que(vha, req);
 591        }
 592
 593        return ret;
 594}
 595
 596int
 597qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
 598{
 599        int ret = QLA_SUCCESS;
 600
 601        if (rsp && vha->flags.qpairs_rsp_created) {
 602                rsp->options |= BIT_0;
 603                ret = qla25xx_init_rsp_que(vha, rsp);
 604                if (ret != QLA_SUCCESS)
 605                        return QLA_FUNCTION_FAILED;
 606
 607                qla25xx_free_rsp_que(vha, rsp);
 608        }
 609
 610        return ret;
 611}
 612
 613/* Delete all queues for a given vhost */
 614int
 615qla25xx_delete_queues(struct scsi_qla_host *vha)
 616{
 617        int cnt, ret = 0;
 618        struct req_que *req = NULL;
 619        struct rsp_que *rsp = NULL;
 620        struct qla_hw_data *ha = vha->hw;
 621        struct qla_qpair *qpair, *tqpair;
 622
 623        if (ql2xmqsupport || ql2xnvmeenable) {
 624                list_for_each_entry_safe(qpair, tqpair, &vha->qp_list,
 625                    qp_list_elem)
 626                        qla2xxx_delete_qpair(vha, qpair);
 627        } else {
 628                /* Delete request queues */
 629                for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
 630                        req = ha->req_q_map[cnt];
 631                        if (req && test_bit(cnt, ha->req_qid_map)) {
 632                                ret = qla25xx_delete_req_que(vha, req);
 633                                if (ret != QLA_SUCCESS) {
 634                                        ql_log(ql_log_warn, vha, 0x00ea,
 635                                            "Couldn't delete req que %d.\n",
 636                                            req->id);
 637                                        return ret;
 638                                }
 639                        }
 640                }
 641
 642                /* Delete response queues */
 643                for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
 644                        rsp = ha->rsp_q_map[cnt];
 645                        if (rsp && test_bit(cnt, ha->rsp_qid_map)) {
 646                                ret = qla25xx_delete_rsp_que(vha, rsp);
 647                                if (ret != QLA_SUCCESS) {
 648                                        ql_log(ql_log_warn, vha, 0x00eb,
 649                                            "Couldn't delete rsp que %d.\n",
 650                                            rsp->id);
 651                                        return ret;
 652                                }
 653                        }
 654                }
 655        }
 656
 657        return ret;
 658}
 659
 660int
 661qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
 662    uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos, bool startqp)
 663{
 664        int ret = 0;
 665        struct req_que *req = NULL;
 666        struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
 667        struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
 668        uint16_t que_id = 0;
 669        device_reg_t *reg;
 670        uint32_t cnt;
 671
 672        req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
 673        if (req == NULL) {
 674                ql_log(ql_log_fatal, base_vha, 0x00d9,
 675                    "Failed to allocate memory for request queue.\n");
 676                goto failed;
 677        }
 678
 679        req->length = REQUEST_ENTRY_CNT_24XX;
 680        req->ring = dma_alloc_coherent(&ha->pdev->dev,
 681                        (req->length + 1) * sizeof(request_t),
 682                        &req->dma, GFP_KERNEL);
 683        if (req->ring == NULL) {
 684                ql_log(ql_log_fatal, base_vha, 0x00da,
 685                    "Failed to allocate memory for request_ring.\n");
 686                goto que_failed;
 687        }
 688
 689        ret = qla2x00_alloc_outstanding_cmds(ha, req);
 690        if (ret != QLA_SUCCESS)
 691                goto que_failed;
 692
 693        mutex_lock(&ha->mq_lock);
 694        que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
 695        if (que_id >= ha->max_req_queues) {
 696                mutex_unlock(&ha->mq_lock);
 697                ql_log(ql_log_warn, base_vha, 0x00db,
 698                    "No resources to create additional request queue.\n");
 699                goto que_failed;
 700        }
 701        set_bit(que_id, ha->req_qid_map);
 702        ha->req_q_map[que_id] = req;
 703        req->rid = rid;
 704        req->vp_idx = vp_idx;
 705        req->qos = qos;
 706
 707        ql_dbg(ql_dbg_multiq, base_vha, 0xc002,
 708            "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
 709            que_id, req->rid, req->vp_idx, req->qos);
 710        ql_dbg(ql_dbg_init, base_vha, 0x00dc,
 711            "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
 712            que_id, req->rid, req->vp_idx, req->qos);
 713        if (rsp_que < 0)
 714                req->rsp = NULL;
 715        else
 716                req->rsp = ha->rsp_q_map[rsp_que];
 717        /* Use alternate PCI bus number */
 718        if (MSB(req->rid))
 719                options |= BIT_4;
 720        /* Use alternate PCI devfn */
 721        if (LSB(req->rid))
 722                options |= BIT_5;
 723        req->options = options;
 724
 725        ql_dbg(ql_dbg_multiq, base_vha, 0xc003,
 726            "options=0x%x.\n", req->options);
 727        ql_dbg(ql_dbg_init, base_vha, 0x00dd,
 728            "options=0x%x.\n", req->options);
 729        for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
 730                req->outstanding_cmds[cnt] = NULL;
 731        req->current_outstanding_cmd = 1;
 732
 733        req->ring_ptr = req->ring;
 734        req->ring_index = 0;
 735        req->cnt = req->length;
 736        req->id = que_id;
 737        reg = ISP_QUE_REG(ha, que_id);
 738        req->req_q_in = &reg->isp25mq.req_q_in;
 739        req->req_q_out = &reg->isp25mq.req_q_out;
 740        req->max_q_depth = ha->req_q_map[0]->max_q_depth;
 741        req->out_ptr = (void *)(req->ring + req->length);
 742        mutex_unlock(&ha->mq_lock);
 743        ql_dbg(ql_dbg_multiq, base_vha, 0xc004,
 744            "ring_ptr=%p ring_index=%d, "
 745            "cnt=%d id=%d max_q_depth=%d.\n",
 746            req->ring_ptr, req->ring_index,
 747            req->cnt, req->id, req->max_q_depth);
 748        ql_dbg(ql_dbg_init, base_vha, 0x00de,
 749            "ring_ptr=%p ring_index=%d, "
 750            "cnt=%d id=%d max_q_depth=%d.\n",
 751            req->ring_ptr, req->ring_index, req->cnt,
 752            req->id, req->max_q_depth);
 753
 754        if (startqp) {
 755                ret = qla25xx_init_req_que(base_vha, req);
 756                if (ret != QLA_SUCCESS) {
 757                        ql_log(ql_log_fatal, base_vha, 0x00df,
 758                            "%s failed.\n", __func__);
 759                        mutex_lock(&ha->mq_lock);
 760                        clear_bit(que_id, ha->req_qid_map);
 761                        mutex_unlock(&ha->mq_lock);
 762                        goto que_failed;
 763                }
 764                vha->flags.qpairs_req_created = 1;
 765        }
 766
 767        return req->id;
 768
 769que_failed:
 770        qla25xx_free_req_que(base_vha, req);
 771failed:
 772        return 0;
 773}
 774
 775static void qla_do_work(struct work_struct *work)
 776{
 777        unsigned long flags;
 778        struct qla_qpair *qpair = container_of(work, struct qla_qpair, q_work);
 779        struct scsi_qla_host *vha;
 780        struct qla_hw_data *ha = qpair->hw;
 781
 782        spin_lock_irqsave(&qpair->qp_lock, flags);
 783        vha = pci_get_drvdata(ha->pdev);
 784        qla24xx_process_response_queue(vha, qpair->rsp);
 785        spin_unlock_irqrestore(&qpair->qp_lock, flags);
 786
 787}
 788
 789/* create response queue */
 790int
 791qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
 792    uint8_t vp_idx, uint16_t rid, struct qla_qpair *qpair, bool startqp)
 793{
 794        int ret = 0;
 795        struct rsp_que *rsp = NULL;
 796        struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
 797        struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
 798        uint16_t que_id = 0;
 799        device_reg_t *reg;
 800
 801        rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
 802        if (rsp == NULL) {
 803                ql_log(ql_log_warn, base_vha, 0x0066,
 804                    "Failed to allocate memory for response queue.\n");
 805                goto failed;
 806        }
 807
 808        rsp->length = RESPONSE_ENTRY_CNT_MQ;
 809        rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
 810                        (rsp->length + 1) * sizeof(response_t),
 811                        &rsp->dma, GFP_KERNEL);
 812        if (rsp->ring == NULL) {
 813                ql_log(ql_log_warn, base_vha, 0x00e1,
 814                    "Failed to allocate memory for response ring.\n");
 815                goto que_failed;
 816        }
 817
 818        mutex_lock(&ha->mq_lock);
 819        que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
 820        if (que_id >= ha->max_rsp_queues) {
 821                mutex_unlock(&ha->mq_lock);
 822                ql_log(ql_log_warn, base_vha, 0x00e2,
 823                    "No resources to create additional request queue.\n");
 824                goto que_failed;
 825        }
 826        set_bit(que_id, ha->rsp_qid_map);
 827
 828        rsp->msix = qpair->msix;
 829
 830        ha->rsp_q_map[que_id] = rsp;
 831        rsp->rid = rid;
 832        rsp->vp_idx = vp_idx;
 833        rsp->hw = ha;
 834        ql_dbg(ql_dbg_init, base_vha, 0x00e4,
 835            "rsp queue_id=%d rid=%d vp_idx=%d hw=%p.\n",
 836            que_id, rsp->rid, rsp->vp_idx, rsp->hw);
 837        /* Use alternate PCI bus number */
 838        if (MSB(rsp->rid))
 839                options |= BIT_4;
 840        /* Use alternate PCI devfn */
 841        if (LSB(rsp->rid))
 842                options |= BIT_5;
 843        /* Enable MSIX handshake mode on for uncapable adapters */
 844        if (!IS_MSIX_NACK_CAPABLE(ha))
 845                options |= BIT_6;
 846
 847        /* Set option to indicate response queue creation */
 848        options |= BIT_1;
 849
 850        rsp->options = options;
 851        rsp->id = que_id;
 852        reg = ISP_QUE_REG(ha, que_id);
 853        rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
 854        rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
 855        rsp->in_ptr = (void *)(rsp->ring + rsp->length);
 856        mutex_unlock(&ha->mq_lock);
 857        ql_dbg(ql_dbg_multiq, base_vha, 0xc00b,
 858            "options=%x id=%d rsp_q_in=%p rsp_q_out=%p\n",
 859            rsp->options, rsp->id, rsp->rsp_q_in,
 860            rsp->rsp_q_out);
 861        ql_dbg(ql_dbg_init, base_vha, 0x00e5,
 862            "options=%x id=%d rsp_q_in=%p rsp_q_out=%p\n",
 863            rsp->options, rsp->id, rsp->rsp_q_in,
 864            rsp->rsp_q_out);
 865
 866        ret = qla25xx_request_irq(ha, qpair, qpair->msix,
 867            QLA_MSIX_QPAIR_MULTIQ_RSP_Q);
 868        if (ret)
 869                goto que_failed;
 870
 871        if (startqp) {
 872                ret = qla25xx_init_rsp_que(base_vha, rsp);
 873                if (ret != QLA_SUCCESS) {
 874                        ql_log(ql_log_fatal, base_vha, 0x00e7,
 875                            "%s failed.\n", __func__);
 876                        mutex_lock(&ha->mq_lock);
 877                        clear_bit(que_id, ha->rsp_qid_map);
 878                        mutex_unlock(&ha->mq_lock);
 879                        goto que_failed;
 880                }
 881                vha->flags.qpairs_rsp_created = 1;
 882        }
 883        rsp->req = NULL;
 884
 885        qla2x00_init_response_q_entries(rsp);
 886        if (qpair->hw->wq)
 887                INIT_WORK(&qpair->q_work, qla_do_work);
 888        return rsp->id;
 889
 890que_failed:
 891        qla25xx_free_rsp_que(base_vha, rsp);
 892failed:
 893        return 0;
 894}
 895
 896static void qla_ctrlvp_sp_done(void *s, int res)
 897{
 898        struct srb *sp = s;
 899
 900        complete(&sp->comp);
 901        /* don't free sp here. Let the caller do the free */
 902}
 903
 904/**
 905 * qla24xx_control_vp() - Enable a virtual port for given host
 906 * @vha:        adapter block pointer
 907 * @cmd:        command type to be sent for enable virtual port
 908 *
 909 * Return:      qla2xxx local function return status code.
 910 */
 911int qla24xx_control_vp(scsi_qla_host_t *vha, int cmd)
 912{
 913        int rval = QLA_MEMORY_ALLOC_FAILED;
 914        struct qla_hw_data *ha = vha->hw;
 915        int     vp_index = vha->vp_idx;
 916        struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
 917        srb_t *sp;
 918
 919        ql_dbg(ql_dbg_vport, vha, 0x10c1,
 920            "Entered %s cmd %x index %d.\n", __func__, cmd, vp_index);
 921
 922        if (vp_index == 0 || vp_index >= ha->max_npiv_vports)
 923                return QLA_PARAMETER_ERROR;
 924
 925        sp = qla2x00_get_sp(base_vha, NULL, GFP_KERNEL);
 926        if (!sp)
 927                goto done;
 928
 929        sp->type = SRB_CTRL_VP;
 930        sp->name = "ctrl_vp";
 931        sp->done = qla_ctrlvp_sp_done;
 932        sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
 933        qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
 934        sp->u.iocb_cmd.u.ctrlvp.cmd = cmd;
 935        sp->u.iocb_cmd.u.ctrlvp.vp_index = vp_index;
 936
 937        rval = qla2x00_start_sp(sp);
 938        if (rval != QLA_SUCCESS) {
 939                ql_dbg(ql_dbg_async, vha, 0xffff,
 940                    "%s: %s Failed submission. %x.\n",
 941                    __func__, sp->name, rval);
 942                goto done_free_sp;
 943        }
 944
 945        ql_dbg(ql_dbg_vport, vha, 0x113f, "%s hndl %x submitted\n",
 946            sp->name, sp->handle);
 947
 948        wait_for_completion(&sp->comp);
 949        rval = sp->rc;
 950        switch (rval) {
 951        case QLA_FUNCTION_TIMEOUT:
 952                ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s Timeout. %x.\n",
 953                    __func__, sp->name, rval);
 954                break;
 955        case QLA_SUCCESS:
 956                ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s done.\n",
 957                    __func__, sp->name);
 958                goto done_free_sp;
 959        default:
 960                ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s Failed. %x.\n",
 961                    __func__, sp->name, rval);
 962                goto done_free_sp;
 963        }
 964done:
 965        return rval;
 966
 967done_free_sp:
 968        sp->free(sp);
 969        return rval;
 970}
 971