linux/drivers/scsi/qla2xxx/tcm_qla2xxx.c
<<
>>
Prefs
   1/*******************************************************************************
   2 * This file contains tcm implementation using v4 configfs fabric infrastructure
   3 * for QLogic target mode HBAs
   4 *
   5 * ?? Copyright 2010-2011 RisingTide Systems LLC.
   6 *
   7 * Licensed to the Linux Foundation under the General Public License (GPL)
   8 * version 2.
   9 *
  10 * Author: Nicholas A. Bellinger <nab@risingtidesystems.com>
  11 *
  12 * tcm_qla2xxx_parse_wwn() and tcm_qla2xxx_format_wwn() contains code from
  13 * the TCM_FC / Open-FCoE.org fabric module.
  14 *
  15 * Copyright (c) 2010 Cisco Systems, Inc
  16 *
  17 * This program is free software; you can redistribute it and/or modify
  18 * it under the terms of the GNU General Public License as published by
  19 * the Free Software Foundation; either version 2 of the License, or
  20 * (at your option) any later version.
  21 *
  22 * This program is distributed in the hope that it will be useful,
  23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25 * GNU General Public License for more details.
  26 ****************************************************************************/
  27
  28
  29#include <linux/module.h>
  30#include <linux/moduleparam.h>
  31#include <linux/utsname.h>
  32#include <linux/vmalloc.h>
  33#include <linux/init.h>
  34#include <linux/list.h>
  35#include <linux/slab.h>
  36#include <linux/kthread.h>
  37#include <linux/types.h>
  38#include <linux/string.h>
  39#include <linux/configfs.h>
  40#include <linux/ctype.h>
  41#include <asm/unaligned.h>
  42#include <scsi/scsi.h>
  43#include <scsi/scsi_host.h>
  44#include <scsi/scsi_device.h>
  45#include <scsi/scsi_cmnd.h>
  46#include <target/target_core_base.h>
  47#include <target/target_core_fabric.h>
  48
  49#include "qla_def.h"
  50#include "qla_target.h"
  51#include "tcm_qla2xxx.h"
  52
  53static struct workqueue_struct *tcm_qla2xxx_free_wq;
  54static struct workqueue_struct *tcm_qla2xxx_cmd_wq;
  55
  56/*
  57 * Parse WWN.
  58 * If strict, we require lower-case hex and colon separators to be sure
  59 * the name is the same as what would be generated by ft_format_wwn()
  60 * so the name and wwn are mapped one-to-one.
  61 */
  62static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
  63{
  64        const char *cp;
  65        char c;
  66        u32 nibble;
  67        u32 byte = 0;
  68        u32 pos = 0;
  69        u32 err;
  70
  71        *wwn = 0;
  72        for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
  73                c = *cp;
  74                if (c == '\n' && cp[1] == '\0')
  75                        continue;
  76                if (strict && pos++ == 2 && byte++ < 7) {
  77                        pos = 0;
  78                        if (c == ':')
  79                                continue;
  80                        err = 1;
  81                        goto fail;
  82                }
  83                if (c == '\0') {
  84                        err = 2;
  85                        if (strict && byte != 8)
  86                                goto fail;
  87                        return cp - name;
  88                }
  89                err = 3;
  90                if (isdigit(c))
  91                        nibble = c - '0';
  92                else if (isxdigit(c) && (islower(c) || !strict))
  93                        nibble = tolower(c) - 'a' + 10;
  94                else
  95                        goto fail;
  96                *wwn = (*wwn << 4) | nibble;
  97        }
  98        err = 4;
  99fail:
 100        pr_debug("err %u len %zu pos %u byte %u\n",
 101                        err, cp - name, pos, byte);
 102        return -1;
 103}
 104
 105static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
 106{
 107        u8 b[8];
 108
 109        put_unaligned_be64(wwn, b);
 110        return snprintf(buf, len,
 111                "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
 112                b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
 113}
 114
 115static char *tcm_qla2xxx_get_fabric_name(void)
 116{
 117        return "qla2xxx";
 118}
 119
 120/*
 121 * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
 122 */
 123static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
 124{
 125        unsigned int i, j;
 126        u8 wwn[8];
 127
 128        memset(wwn, 0, sizeof(wwn));
 129
 130        /* Validate and store the new name */
 131        for (i = 0, j = 0; i < 16; i++) {
 132                int value;
 133
 134                value = hex_to_bin(*ns++);
 135                if (value >= 0)
 136                        j = (j << 4) | value;
 137                else
 138                        return -EINVAL;
 139
 140                if (i % 2) {
 141                        wwn[i/2] = j & 0xff;
 142                        j = 0;
 143                }
 144        }
 145
 146        *nm = wwn_to_u64(wwn);
 147        return 0;
 148}
 149
 150/*
 151 * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
 152 * store_fc_host_vport_create()
 153 */
 154static int tcm_qla2xxx_npiv_parse_wwn(
 155        const char *name,
 156        size_t count,
 157        u64 *wwpn,
 158        u64 *wwnn)
 159{
 160        unsigned int cnt = count;
 161        int rc;
 162
 163        *wwpn = 0;
 164        *wwnn = 0;
 165
 166        /* count may include a LF at end of string */
 167        if (name[cnt-1] == '\n' || name[cnt-1] == 0)
 168                cnt--;
 169
 170        /* validate we have enough characters for WWPN */
 171        if ((cnt != (16+1+16)) || (name[16] != ':'))
 172                return -EINVAL;
 173
 174        rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
 175        if (rc != 0)
 176                return rc;
 177
 178        rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
 179        if (rc != 0)
 180                return rc;
 181
 182        return 0;
 183}
 184
 185static char *tcm_qla2xxx_npiv_get_fabric_name(void)
 186{
 187        return "qla2xxx_npiv";
 188}
 189
 190static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
 191{
 192        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
 193                                struct tcm_qla2xxx_tpg, se_tpg);
 194        struct tcm_qla2xxx_lport *lport = tpg->lport;
 195
 196        return lport->lport_naa_name;
 197}
 198
 199static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
 200{
 201        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
 202                                struct tcm_qla2xxx_tpg, se_tpg);
 203        return tpg->lport_tpgt;
 204}
 205
 206static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
 207{
 208        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
 209                                struct tcm_qla2xxx_tpg, se_tpg);
 210
 211        return tpg->tpg_attrib.generate_node_acls;
 212}
 213
 214static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
 215{
 216        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
 217                                struct tcm_qla2xxx_tpg, se_tpg);
 218
 219        return tpg->tpg_attrib.cache_dynamic_acls;
 220}
 221
 222static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
 223{
 224        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
 225                                struct tcm_qla2xxx_tpg, se_tpg);
 226
 227        return tpg->tpg_attrib.demo_mode_write_protect;
 228}
 229
 230static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
 231{
 232        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
 233                                struct tcm_qla2xxx_tpg, se_tpg);
 234
 235        return tpg->tpg_attrib.prod_mode_write_protect;
 236}
 237
 238static int tcm_qla2xxx_check_demo_mode_login_only(struct se_portal_group *se_tpg)
 239{
 240        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
 241                                struct tcm_qla2xxx_tpg, se_tpg);
 242
 243        return tpg->tpg_attrib.demo_mode_login_only;
 244}
 245
 246static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
 247{
 248        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
 249                                struct tcm_qla2xxx_tpg, se_tpg);
 250
 251        return tpg->lport_tpgt;
 252}
 253
 254static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
 255{
 256        struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
 257                        struct qla_tgt_mgmt_cmd, free_work);
 258
 259        transport_generic_free_cmd(&mcmd->se_cmd, 0);
 260}
 261
 262/*
 263 * Called from qla_target_template->free_mcmd(), and will call
 264 * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
 265 * release callback.  qla_hw_data->hardware_lock is expected to be held
 266 */
 267static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
 268{
 269        INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
 270        queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
 271}
 272
 273static void tcm_qla2xxx_complete_free(struct work_struct *work)
 274{
 275        struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
 276
 277        cmd->cmd_in_wq = 0;
 278
 279        WARN_ON(cmd->trc_flags & TRC_CMD_FREE);
 280
 281        cmd->qpair->tgt_counters.qla_core_ret_sta_ctio++;
 282        cmd->trc_flags |= TRC_CMD_FREE;
 283        transport_generic_free_cmd(&cmd->se_cmd, 0);
 284}
 285
 286/*
 287 * Called from qla_target_template->free_cmd(), and will call
 288 * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
 289 * release callback.  qla_hw_data->hardware_lock is expected to be held
 290 */
 291static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
 292{
 293        cmd->qpair->tgt_counters.core_qla_free_cmd++;
 294        cmd->cmd_in_wq = 1;
 295
 296        WARN_ON(cmd->trc_flags & TRC_CMD_DONE);
 297        cmd->trc_flags |= TRC_CMD_DONE;
 298
 299        INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
 300        queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
 301}
 302
 303/*
 304 * Called from struct target_core_fabric_ops->check_stop_free() context
 305 */
 306static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
 307{
 308        struct qla_tgt_cmd *cmd;
 309
 310        if ((se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) == 0) {
 311                cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
 312                cmd->trc_flags |= TRC_CMD_CHK_STOP;
 313        }
 314
 315        return target_put_sess_cmd(se_cmd);
 316}
 317
 318/* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
 319 * fabric descriptor @se_cmd command to release
 320 */
 321static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
 322{
 323        struct qla_tgt_cmd *cmd;
 324
 325        if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
 326                struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
 327                                struct qla_tgt_mgmt_cmd, se_cmd);
 328                qlt_free_mcmd(mcmd);
 329                return;
 330        }
 331
 332        cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
 333        qlt_free_cmd(cmd);
 334}
 335
 336static int tcm_qla2xxx_shutdown_session(struct se_session *se_sess)
 337{
 338        struct fc_port *sess = se_sess->fabric_sess_ptr;
 339        struct scsi_qla_host *vha;
 340        unsigned long flags;
 341
 342        BUG_ON(!sess);
 343        vha = sess->vha;
 344
 345        spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
 346        target_sess_cmd_list_set_waiting(se_sess);
 347        spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
 348
 349        return 1;
 350}
 351
 352static void tcm_qla2xxx_close_session(struct se_session *se_sess)
 353{
 354        struct fc_port *sess = se_sess->fabric_sess_ptr;
 355        struct scsi_qla_host *vha;
 356        unsigned long flags;
 357
 358        BUG_ON(!sess);
 359        vha = sess->vha;
 360
 361        spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
 362        qlt_unreg_sess(sess);
 363        spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
 364}
 365
 366static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
 367{
 368        return 0;
 369}
 370
 371/*
 372 * The LIO target core uses DMA_TO_DEVICE to mean that data is going
 373 * to the target (eg handling a WRITE) and DMA_FROM_DEVICE to mean
 374 * that data is coming from the target (eg handling a READ).  However,
 375 * this is just the opposite of what we have to tell the DMA mapping
 376 * layer -- eg when handling a READ, the HBA will have to DMA the data
 377 * out of memory so it can send it to the initiator, which means we
 378 * need to use DMA_TO_DEVICE when we map the data.
 379 */
 380static enum dma_data_direction tcm_qla2xxx_mapping_dir(struct se_cmd *se_cmd)
 381{
 382        if (se_cmd->se_cmd_flags & SCF_BIDI)
 383                return DMA_BIDIRECTIONAL;
 384
 385        switch (se_cmd->data_direction) {
 386        case DMA_TO_DEVICE:
 387                return DMA_FROM_DEVICE;
 388        case DMA_FROM_DEVICE:
 389                return DMA_TO_DEVICE;
 390        case DMA_NONE:
 391        default:
 392                return DMA_NONE;
 393        }
 394}
 395
 396static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
 397{
 398        struct qla_tgt_cmd *cmd = container_of(se_cmd,
 399                                struct qla_tgt_cmd, se_cmd);
 400
 401        if (cmd->aborted) {
 402                /* Cmd can loop during Q-full.  tcm_qla2xxx_aborted_task
 403                 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
 404                 * already kick start the free.
 405                 */
 406                pr_debug("write_pending aborted cmd[%p] refcount %d "
 407                        "transport_state %x, t_state %x, se_cmd_flags %x\n",
 408                        cmd, kref_read(&cmd->se_cmd.cmd_kref),
 409                        cmd->se_cmd.transport_state,
 410                        cmd->se_cmd.t_state,
 411                        cmd->se_cmd.se_cmd_flags);
 412                return 0;
 413        }
 414        cmd->trc_flags |= TRC_XFR_RDY;
 415        cmd->bufflen = se_cmd->data_length;
 416        cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
 417
 418        cmd->sg_cnt = se_cmd->t_data_nents;
 419        cmd->sg = se_cmd->t_data_sg;
 420
 421        cmd->prot_sg_cnt = se_cmd->t_prot_nents;
 422        cmd->prot_sg = se_cmd->t_prot_sg;
 423        cmd->blk_sz  = se_cmd->se_dev->dev_attrib.block_size;
 424        se_cmd->pi_err = 0;
 425
 426        /*
 427         * qla_target.c:qlt_rdy_to_xfer() will call pci_map_sg() to setup
 428         * the SGL mappings into PCIe memory for incoming FCP WRITE data.
 429         */
 430        return qlt_rdy_to_xfer(cmd);
 431}
 432
 433static int tcm_qla2xxx_write_pending_status(struct se_cmd *se_cmd)
 434{
 435        unsigned long flags;
 436        /*
 437         * Check for WRITE_PENDING status to determine if we need to wait for
 438         * CTIO aborts to be posted via hardware in tcm_qla2xxx_handle_data().
 439         */
 440        spin_lock_irqsave(&se_cmd->t_state_lock, flags);
 441        if (se_cmd->t_state == TRANSPORT_WRITE_PENDING ||
 442            se_cmd->t_state == TRANSPORT_COMPLETE_QF_WP) {
 443                spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
 444                wait_for_completion_timeout(&se_cmd->t_transport_stop_comp,
 445                                                50);
 446                return 0;
 447        }
 448        spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
 449
 450        return 0;
 451}
 452
 453static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
 454{
 455        return;
 456}
 457
 458static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
 459{
 460        if (!(se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
 461                struct qla_tgt_cmd *cmd = container_of(se_cmd,
 462                                struct qla_tgt_cmd, se_cmd);
 463                return cmd->state;
 464        }
 465
 466        return 0;
 467}
 468
 469/*
 470 * Called from process context in qla_target.c:qlt_do_work() code
 471 */
 472static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
 473        unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
 474        int data_dir, int bidi)
 475{
 476        struct se_cmd *se_cmd = &cmd->se_cmd;
 477        struct se_session *se_sess;
 478        struct fc_port *sess;
 479#ifdef CONFIG_TCM_QLA2XXX_DEBUG
 480        struct se_portal_group *se_tpg;
 481        struct tcm_qla2xxx_tpg *tpg;
 482#endif
 483        int flags = TARGET_SCF_ACK_KREF;
 484
 485        if (bidi)
 486                flags |= TARGET_SCF_BIDI_OP;
 487
 488        sess = cmd->sess;
 489        if (!sess) {
 490                pr_err("Unable to locate struct fc_port from qla_tgt_cmd\n");
 491                return -EINVAL;
 492        }
 493
 494        se_sess = sess->se_sess;
 495        if (!se_sess) {
 496                pr_err("Unable to locate active struct se_session\n");
 497                return -EINVAL;
 498        }
 499
 500#ifdef CONFIG_TCM_QLA2XXX_DEBUG
 501        se_tpg = se_sess->se_tpg;
 502        tpg = container_of(se_tpg, struct tcm_qla2xxx_tpg, se_tpg);
 503        if (unlikely(tpg->tpg_attrib.jam_host)) {
 504                /* return, and dont run target_submit_cmd,discarding command */
 505                return 0;
 506        }
 507#endif
 508
 509        cmd->qpair->tgt_counters.qla_core_sbt_cmd++;
 510        return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
 511                                cmd->unpacked_lun, data_length, fcp_task_attr,
 512                                data_dir, flags);
 513}
 514
 515static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
 516{
 517        struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
 518
 519        /*
 520         * Ensure that the complete FCP WRITE payload has been received.
 521         * Otherwise return an exception via CHECK_CONDITION status.
 522         */
 523        cmd->cmd_in_wq = 0;
 524
 525        cmd->qpair->tgt_counters.qla_core_ret_ctio++;
 526        if (!cmd->write_data_transferred) {
 527                /*
 528                 * Check if se_cmd has already been aborted via LUN_RESET, and
 529                 * waiting upon completion in tcm_qla2xxx_write_pending_status()
 530                 */
 531                if (cmd->se_cmd.transport_state & CMD_T_ABORTED) {
 532                        complete(&cmd->se_cmd.t_transport_stop_comp);
 533                        return;
 534                }
 535
 536                switch (cmd->dif_err_code) {
 537                case DIF_ERR_GRD:
 538                        cmd->se_cmd.pi_err =
 539                            TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
 540                        break;
 541                case DIF_ERR_REF:
 542                        cmd->se_cmd.pi_err =
 543                            TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
 544                        break;
 545                case DIF_ERR_APP:
 546                        cmd->se_cmd.pi_err =
 547                            TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
 548                        break;
 549                case DIF_ERR_NONE:
 550                default:
 551                        break;
 552                }
 553
 554                if (cmd->se_cmd.pi_err)
 555                        transport_generic_request_failure(&cmd->se_cmd,
 556                                cmd->se_cmd.pi_err);
 557                else
 558                        transport_generic_request_failure(&cmd->se_cmd,
 559                                TCM_CHECK_CONDITION_ABORT_CMD);
 560
 561                return;
 562        }
 563
 564        return target_execute_cmd(&cmd->se_cmd);
 565}
 566
 567/*
 568 * Called from qla_target.c:qlt_do_ctio_completion()
 569 */
 570static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
 571{
 572        cmd->trc_flags |= TRC_DATA_IN;
 573        cmd->cmd_in_wq = 1;
 574        INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
 575        queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
 576}
 577
 578static int tcm_qla2xxx_chk_dif_tags(uint32_t tag)
 579{
 580        return 0;
 581}
 582
 583static int tcm_qla2xxx_dif_tags(struct qla_tgt_cmd *cmd,
 584    uint16_t *pfw_prot_opts)
 585{
 586        struct se_cmd *se_cmd = &cmd->se_cmd;
 587
 588        if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD))
 589                *pfw_prot_opts |= PO_DISABLE_GUARD_CHECK;
 590
 591        if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG))
 592                *pfw_prot_opts |= PO_DIS_APP_TAG_VALD;
 593
 594        return 0;
 595}
 596
 597/*
 598 * Called from qla_target.c:qlt_issue_task_mgmt()
 599 */
 600static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, u64 lun,
 601        uint16_t tmr_func, uint32_t tag)
 602{
 603        struct fc_port *sess = mcmd->sess;
 604        struct se_cmd *se_cmd = &mcmd->se_cmd;
 605        int transl_tmr_func = 0;
 606
 607        switch (tmr_func) {
 608        case QLA_TGT_ABTS:
 609                pr_debug("%ld: ABTS received\n", sess->vha->host_no);
 610                transl_tmr_func = TMR_ABORT_TASK;
 611                break;
 612        case QLA_TGT_2G_ABORT_TASK:
 613                pr_debug("%ld: 2G Abort Task received\n", sess->vha->host_no);
 614                transl_tmr_func = TMR_ABORT_TASK;
 615                break;
 616        case QLA_TGT_CLEAR_ACA:
 617                pr_debug("%ld: CLEAR_ACA received\n", sess->vha->host_no);
 618                transl_tmr_func = TMR_CLEAR_ACA;
 619                break;
 620        case QLA_TGT_TARGET_RESET:
 621                pr_debug("%ld: TARGET_RESET received\n", sess->vha->host_no);
 622                transl_tmr_func = TMR_TARGET_WARM_RESET;
 623                break;
 624        case QLA_TGT_LUN_RESET:
 625                pr_debug("%ld: LUN_RESET received\n", sess->vha->host_no);
 626                transl_tmr_func = TMR_LUN_RESET;
 627                break;
 628        case QLA_TGT_CLEAR_TS:
 629                pr_debug("%ld: CLEAR_TS received\n", sess->vha->host_no);
 630                transl_tmr_func = TMR_CLEAR_TASK_SET;
 631                break;
 632        case QLA_TGT_ABORT_TS:
 633                pr_debug("%ld: ABORT_TS received\n", sess->vha->host_no);
 634                transl_tmr_func = TMR_ABORT_TASK_SET;
 635                break;
 636        default:
 637                pr_debug("%ld: Unknown task mgmt fn 0x%x\n",
 638                    sess->vha->host_no, tmr_func);
 639                return -ENOSYS;
 640        }
 641
 642        return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
 643            transl_tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF);
 644}
 645
 646static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
 647{
 648        struct qla_tgt_cmd *cmd = container_of(se_cmd,
 649                                struct qla_tgt_cmd, se_cmd);
 650
 651        if (cmd->aborted) {
 652                /* Cmd can loop during Q-full.  tcm_qla2xxx_aborted_task
 653                 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
 654                 * already kick start the free.
 655                 */
 656                pr_debug("queue_data_in aborted cmd[%p] refcount %d "
 657                        "transport_state %x, t_state %x, se_cmd_flags %x\n",
 658                        cmd, kref_read(&cmd->se_cmd.cmd_kref),
 659                        cmd->se_cmd.transport_state,
 660                        cmd->se_cmd.t_state,
 661                        cmd->se_cmd.se_cmd_flags);
 662                return 0;
 663        }
 664
 665        cmd->trc_flags |= TRC_XMIT_DATA;
 666        cmd->bufflen = se_cmd->data_length;
 667        cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
 668
 669        cmd->sg_cnt = se_cmd->t_data_nents;
 670        cmd->sg = se_cmd->t_data_sg;
 671        cmd->offset = 0;
 672
 673        cmd->prot_sg_cnt = se_cmd->t_prot_nents;
 674        cmd->prot_sg = se_cmd->t_prot_sg;
 675        cmd->blk_sz  = se_cmd->se_dev->dev_attrib.block_size;
 676        se_cmd->pi_err = 0;
 677
 678        /*
 679         * Now queue completed DATA_IN the qla2xxx LLD and response ring
 680         */
 681        return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
 682                                se_cmd->scsi_status);
 683}
 684
 685static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
 686{
 687        struct qla_tgt_cmd *cmd = container_of(se_cmd,
 688                                struct qla_tgt_cmd, se_cmd);
 689        int xmit_type = QLA_TGT_XMIT_STATUS;
 690
 691        if (cmd->aborted) {
 692                /*
 693                 * Cmd can loop during Q-full. tcm_qla2xxx_aborted_task
 694                 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
 695                 * already kick start the free.
 696                 */
 697                pr_debug(
 698                    "queue_data_in aborted cmd[%p] transport_state %x, t_state %x, se_cmd_flags %x\n",
 699                    cmd, cmd->se_cmd.transport_state, cmd->se_cmd.t_state,
 700                    cmd->se_cmd.se_cmd_flags);
 701                return 0;
 702        }
 703        cmd->bufflen = se_cmd->data_length;
 704        cmd->sg = NULL;
 705        cmd->sg_cnt = 0;
 706        cmd->offset = 0;
 707        cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
 708        if (cmd->trc_flags & TRC_XMIT_STATUS) {
 709                pr_crit("Multiple calls for status = %p.\n", cmd);
 710                dump_stack();
 711        }
 712        cmd->trc_flags |= TRC_XMIT_STATUS;
 713
 714        if (se_cmd->data_direction == DMA_FROM_DEVICE) {
 715                /*
 716                 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
 717                 * for qla_tgt_xmit_response LLD code
 718                 */
 719                if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
 720                        se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT;
 721                        se_cmd->residual_count = 0;
 722                }
 723                se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
 724                se_cmd->residual_count += se_cmd->data_length;
 725
 726                cmd->bufflen = 0;
 727        }
 728        /*
 729         * Now queue status response to qla2xxx LLD code and response ring
 730         */
 731        return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
 732}
 733
 734static void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
 735{
 736        struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
 737        struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
 738                                struct qla_tgt_mgmt_cmd, se_cmd);
 739
 740        pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
 741                        mcmd, se_tmr->function, se_tmr->response);
 742        /*
 743         * Do translation between TCM TM response codes and
 744         * QLA2xxx FC TM response codes.
 745         */
 746        switch (se_tmr->response) {
 747        case TMR_FUNCTION_COMPLETE:
 748                mcmd->fc_tm_rsp = FC_TM_SUCCESS;
 749                break;
 750        case TMR_TASK_DOES_NOT_EXIST:
 751                mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
 752                break;
 753        case TMR_FUNCTION_REJECTED:
 754                mcmd->fc_tm_rsp = FC_TM_REJECT;
 755                break;
 756        case TMR_LUN_DOES_NOT_EXIST:
 757        default:
 758                mcmd->fc_tm_rsp = FC_TM_FAILED;
 759                break;
 760        }
 761        /*
 762         * Queue the TM response to QLA2xxx LLD to build a
 763         * CTIO response packet.
 764         */
 765        qlt_xmit_tm_rsp(mcmd);
 766}
 767
 768static void tcm_qla2xxx_aborted_task(struct se_cmd *se_cmd)
 769{
 770        struct qla_tgt_cmd *cmd = container_of(se_cmd,
 771                                struct qla_tgt_cmd, se_cmd);
 772
 773        if (qlt_abort_cmd(cmd))
 774                return;
 775}
 776
 777static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
 778                        struct tcm_qla2xxx_nacl *, struct fc_port *);
 779/*
 780 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
 781 */
 782static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct fc_port *sess)
 783{
 784        struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
 785        struct se_portal_group *se_tpg = se_nacl->se_tpg;
 786        struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
 787        struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
 788                                struct tcm_qla2xxx_lport, lport_wwn);
 789        struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
 790                                struct tcm_qla2xxx_nacl, se_node_acl);
 791        void *node;
 792
 793        pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
 794
 795        node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
 796        WARN_ON(node && (node != se_nacl));
 797
 798        pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
 799            se_nacl, nacl->nport_wwnn, nacl->nport_id);
 800        /*
 801         * Now clear the se_nacl and session pointers from our HW lport lookup
 802         * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
 803         *
 804         * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
 805         * target_wait_for_sess_cmds() before the session waits for outstanding
 806         * I/O to complete, to avoid a race between session shutdown execution
 807         * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
 808         */
 809        tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
 810}
 811
 812static void tcm_qla2xxx_release_session(struct kref *kref)
 813{
 814        struct se_session *se_sess = container_of(kref,
 815                        struct se_session, sess_kref);
 816
 817        qlt_unreg_sess(se_sess->fabric_sess_ptr);
 818}
 819
 820static void tcm_qla2xxx_put_sess(struct fc_port *sess)
 821{
 822        if (!sess)
 823                return;
 824
 825        assert_spin_locked(&sess->vha->hw->tgt.sess_lock);
 826        kref_put(&sess->se_sess->sess_kref, tcm_qla2xxx_release_session);
 827}
 828
 829static void tcm_qla2xxx_shutdown_sess(struct fc_port *sess)
 830{
 831        assert_spin_locked(&sess->vha->hw->tgt.sess_lock);
 832        target_sess_cmd_list_set_waiting(sess->se_sess);
 833}
 834
 835static int tcm_qla2xxx_init_nodeacl(struct se_node_acl *se_nacl,
 836                const char *name)
 837{
 838        struct tcm_qla2xxx_nacl *nacl =
 839                container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
 840        u64 wwnn;
 841
 842        if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
 843                return -EINVAL;
 844
 845        nacl->nport_wwnn = wwnn;
 846        tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
 847
 848        return 0;
 849}
 850
 851/* Start items for tcm_qla2xxx_tpg_attrib_cit */
 852
 853#define DEF_QLA_TPG_ATTRIB(name)                                        \
 854                                                                        \
 855static ssize_t tcm_qla2xxx_tpg_attrib_##name##_show(                    \
 856                struct config_item *item, char *page)                   \
 857{                                                                       \
 858        struct se_portal_group *se_tpg = attrib_to_tpg(item);           \
 859        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,              \
 860                        struct tcm_qla2xxx_tpg, se_tpg);                \
 861                                                                        \
 862        return sprintf(page, "%u\n", tpg->tpg_attrib.name);     \
 863}                                                                       \
 864                                                                        \
 865static ssize_t tcm_qla2xxx_tpg_attrib_##name##_store(                   \
 866                struct config_item *item, const char *page, size_t count) \
 867{                                                                       \
 868        struct se_portal_group *se_tpg = attrib_to_tpg(item);           \
 869        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,              \
 870                        struct tcm_qla2xxx_tpg, se_tpg);                \
 871        struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib;            \
 872        unsigned long val;                                              \
 873        int ret;                                                        \
 874                                                                        \
 875        ret = kstrtoul(page, 0, &val);                                  \
 876        if (ret < 0) {                                                  \
 877                pr_err("kstrtoul() failed with"                         \
 878                                " ret: %d\n", ret);                     \
 879                return -EINVAL;                                         \
 880        }                                                               \
 881                                                                        \
 882        if ((val != 0) && (val != 1)) {                                 \
 883                pr_err("Illegal boolean value %lu\n", val);             \
 884                return -EINVAL;                                         \
 885        }                                                               \
 886                                                                        \
 887        a->name = val;                                                  \
 888                                                                        \
 889        return count;                                                   \
 890}                                                                       \
 891CONFIGFS_ATTR(tcm_qla2xxx_tpg_attrib_, name)
 892
 893DEF_QLA_TPG_ATTRIB(generate_node_acls);
 894DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
 895DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
 896DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
 897DEF_QLA_TPG_ATTRIB(demo_mode_login_only);
 898
 899#ifdef CONFIG_TCM_QLA2XXX_DEBUG
 900DEF_QLA_TPG_ATTRIB(jam_host);
 901#endif
 902
 903static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
 904        &tcm_qla2xxx_tpg_attrib_attr_generate_node_acls,
 905        &tcm_qla2xxx_tpg_attrib_attr_cache_dynamic_acls,
 906        &tcm_qla2xxx_tpg_attrib_attr_demo_mode_write_protect,
 907        &tcm_qla2xxx_tpg_attrib_attr_prod_mode_write_protect,
 908        &tcm_qla2xxx_tpg_attrib_attr_demo_mode_login_only,
 909#ifdef CONFIG_TCM_QLA2XXX_DEBUG
 910        &tcm_qla2xxx_tpg_attrib_attr_jam_host,
 911#endif
 912        NULL,
 913};
 914
 915/* End items for tcm_qla2xxx_tpg_attrib_cit */
 916
 917static ssize_t tcm_qla2xxx_tpg_enable_show(struct config_item *item,
 918                char *page)
 919{
 920        struct se_portal_group *se_tpg = to_tpg(item);
 921        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
 922                        struct tcm_qla2xxx_tpg, se_tpg);
 923
 924        return snprintf(page, PAGE_SIZE, "%d\n",
 925                        atomic_read(&tpg->lport_tpg_enabled));
 926}
 927
 928static void tcm_qla2xxx_depend_tpg(struct work_struct *work)
 929{
 930        struct tcm_qla2xxx_tpg *base_tpg = container_of(work,
 931                                struct tcm_qla2xxx_tpg, tpg_base_work);
 932        struct se_portal_group *se_tpg = &base_tpg->se_tpg;
 933        struct scsi_qla_host *base_vha = base_tpg->lport->qla_vha;
 934
 935        if (!target_depend_item(&se_tpg->tpg_group.cg_item)) {
 936                atomic_set(&base_tpg->lport_tpg_enabled, 1);
 937                qlt_enable_vha(base_vha);
 938        }
 939        complete(&base_tpg->tpg_base_comp);
 940}
 941
 942static void tcm_qla2xxx_undepend_tpg(struct work_struct *work)
 943{
 944        struct tcm_qla2xxx_tpg *base_tpg = container_of(work,
 945                                struct tcm_qla2xxx_tpg, tpg_base_work);
 946        struct se_portal_group *se_tpg = &base_tpg->se_tpg;
 947        struct scsi_qla_host *base_vha = base_tpg->lport->qla_vha;
 948
 949        if (!qlt_stop_phase1(base_vha->vha_tgt.qla_tgt)) {
 950                atomic_set(&base_tpg->lport_tpg_enabled, 0);
 951                target_undepend_item(&se_tpg->tpg_group.cg_item);
 952        }
 953        complete(&base_tpg->tpg_base_comp);
 954}
 955
 956static ssize_t tcm_qla2xxx_tpg_enable_store(struct config_item *item,
 957                const char *page, size_t count)
 958{
 959        struct se_portal_group *se_tpg = to_tpg(item);
 960        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
 961                        struct tcm_qla2xxx_tpg, se_tpg);
 962        unsigned long op;
 963        int rc;
 964
 965        rc = kstrtoul(page, 0, &op);
 966        if (rc < 0) {
 967                pr_err("kstrtoul() returned %d\n", rc);
 968                return -EINVAL;
 969        }
 970        if ((op != 1) && (op != 0)) {
 971                pr_err("Illegal value for tpg_enable: %lu\n", op);
 972                return -EINVAL;
 973        }
 974        if (op) {
 975                if (atomic_read(&tpg->lport_tpg_enabled))
 976                        return -EEXIST;
 977
 978                INIT_WORK(&tpg->tpg_base_work, tcm_qla2xxx_depend_tpg);
 979        } else {
 980                if (!atomic_read(&tpg->lport_tpg_enabled))
 981                        return count;
 982
 983                INIT_WORK(&tpg->tpg_base_work, tcm_qla2xxx_undepend_tpg);
 984        }
 985        init_completion(&tpg->tpg_base_comp);
 986        schedule_work(&tpg->tpg_base_work);
 987        wait_for_completion(&tpg->tpg_base_comp);
 988
 989        if (op) {
 990                if (!atomic_read(&tpg->lport_tpg_enabled))
 991                        return -ENODEV;
 992        } else {
 993                if (atomic_read(&tpg->lport_tpg_enabled))
 994                        return -EPERM;
 995        }
 996        return count;
 997}
 998
 999CONFIGFS_ATTR_WO(tcm_qla2xxx_tpg_, enable);
1000
1001static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
1002        &tcm_qla2xxx_tpg_attr_enable,
1003        NULL,
1004};
1005
1006static struct se_portal_group *tcm_qla2xxx_make_tpg(
1007        struct se_wwn *wwn,
1008        struct config_group *group,
1009        const char *name)
1010{
1011        struct tcm_qla2xxx_lport *lport = container_of(wwn,
1012                        struct tcm_qla2xxx_lport, lport_wwn);
1013        struct tcm_qla2xxx_tpg *tpg;
1014        unsigned long tpgt;
1015        int ret;
1016
1017        if (strstr(name, "tpgt_") != name)
1018                return ERR_PTR(-EINVAL);
1019        if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1020                return ERR_PTR(-EINVAL);
1021
1022        if ((tpgt != 1)) {
1023                pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
1024                return ERR_PTR(-ENOSYS);
1025        }
1026
1027        tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1028        if (!tpg) {
1029                pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1030                return ERR_PTR(-ENOMEM);
1031        }
1032        tpg->lport = lport;
1033        tpg->lport_tpgt = tpgt;
1034        /*
1035         * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1036         * NodeACLs
1037         */
1038        tpg->tpg_attrib.generate_node_acls = 1;
1039        tpg->tpg_attrib.demo_mode_write_protect = 1;
1040        tpg->tpg_attrib.cache_dynamic_acls = 1;
1041        tpg->tpg_attrib.demo_mode_login_only = 1;
1042        tpg->tpg_attrib.jam_host = 0;
1043
1044        ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
1045        if (ret < 0) {
1046                kfree(tpg);
1047                return NULL;
1048        }
1049
1050        lport->tpg_1 = tpg;
1051
1052        return &tpg->se_tpg;
1053}
1054
1055static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
1056{
1057        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1058                        struct tcm_qla2xxx_tpg, se_tpg);
1059        struct tcm_qla2xxx_lport *lport = tpg->lport;
1060        struct scsi_qla_host *vha = lport->qla_vha;
1061        /*
1062         * Call into qla2x_target.c LLD logic to shutdown the active
1063         * FC Nexuses and disable target mode operation for this qla_hw_data
1064         */
1065        if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stop)
1066                qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1067
1068        core_tpg_deregister(se_tpg);
1069        /*
1070         * Clear local TPG=1 pointer for non NPIV mode.
1071         */
1072        lport->tpg_1 = NULL;
1073        kfree(tpg);
1074}
1075
1076static ssize_t tcm_qla2xxx_npiv_tpg_enable_show(struct config_item *item,
1077                char *page)
1078{
1079        return tcm_qla2xxx_tpg_enable_show(item, page);
1080}
1081
1082static ssize_t tcm_qla2xxx_npiv_tpg_enable_store(struct config_item *item,
1083                const char *page, size_t count)
1084{
1085        struct se_portal_group *se_tpg = to_tpg(item);
1086        struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
1087        struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
1088                        struct tcm_qla2xxx_lport, lport_wwn);
1089        struct scsi_qla_host *vha = lport->qla_vha;
1090        struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1091                        struct tcm_qla2xxx_tpg, se_tpg);
1092        unsigned long op;
1093        int rc;
1094
1095        rc = kstrtoul(page, 0, &op);
1096        if (rc < 0) {
1097                pr_err("kstrtoul() returned %d\n", rc);
1098                return -EINVAL;
1099        }
1100        if ((op != 1) && (op != 0)) {
1101                pr_err("Illegal value for tpg_enable: %lu\n", op);
1102                return -EINVAL;
1103        }
1104        if (op) {
1105                if (atomic_read(&tpg->lport_tpg_enabled))
1106                        return -EEXIST;
1107
1108                atomic_set(&tpg->lport_tpg_enabled, 1);
1109                qlt_enable_vha(vha);
1110        } else {
1111                if (!atomic_read(&tpg->lport_tpg_enabled))
1112                        return count;
1113
1114                atomic_set(&tpg->lport_tpg_enabled, 0);
1115                qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1116        }
1117
1118        return count;
1119}
1120
1121CONFIGFS_ATTR(tcm_qla2xxx_npiv_tpg_, enable);
1122
1123static struct configfs_attribute *tcm_qla2xxx_npiv_tpg_attrs[] = {
1124        &tcm_qla2xxx_npiv_tpg_attr_enable,
1125        NULL,
1126};
1127
1128static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(
1129        struct se_wwn *wwn,
1130        struct config_group *group,
1131        const char *name)
1132{
1133        struct tcm_qla2xxx_lport *lport = container_of(wwn,
1134                        struct tcm_qla2xxx_lport, lport_wwn);
1135        struct tcm_qla2xxx_tpg *tpg;
1136        unsigned long tpgt;
1137        int ret;
1138
1139        if (strstr(name, "tpgt_") != name)
1140                return ERR_PTR(-EINVAL);
1141        if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1142                return ERR_PTR(-EINVAL);
1143
1144        tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1145        if (!tpg) {
1146                pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1147                return ERR_PTR(-ENOMEM);
1148        }
1149        tpg->lport = lport;
1150        tpg->lport_tpgt = tpgt;
1151
1152        /*
1153         * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1154         * NodeACLs
1155         */
1156        tpg->tpg_attrib.generate_node_acls = 1;
1157        tpg->tpg_attrib.demo_mode_write_protect = 1;
1158        tpg->tpg_attrib.cache_dynamic_acls = 1;
1159        tpg->tpg_attrib.demo_mode_login_only = 1;
1160
1161        ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
1162        if (ret < 0) {
1163                kfree(tpg);
1164                return NULL;
1165        }
1166        lport->tpg_1 = tpg;
1167        return &tpg->se_tpg;
1168}
1169
1170/*
1171 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1172 */
1173static struct fc_port *tcm_qla2xxx_find_sess_by_s_id(
1174        scsi_qla_host_t *vha,
1175        const uint8_t *s_id)
1176{
1177        struct tcm_qla2xxx_lport *lport;
1178        struct se_node_acl *se_nacl;
1179        struct tcm_qla2xxx_nacl *nacl;
1180        u32 key;
1181
1182        lport = vha->vha_tgt.target_lport_ptr;
1183        if (!lport) {
1184                pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1185                dump_stack();
1186                return NULL;
1187        }
1188
1189        key = sid_to_key(s_id);
1190        pr_debug("find_sess_by_s_id: 0x%06x\n", key);
1191
1192        se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
1193        if (!se_nacl) {
1194                pr_debug("Unable to locate s_id: 0x%06x\n", key);
1195                return NULL;
1196        }
1197        pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
1198            se_nacl, se_nacl->initiatorname);
1199
1200        nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1201        if (!nacl->fc_port) {
1202                pr_err("Unable to locate struct fc_port\n");
1203                return NULL;
1204        }
1205
1206        return nacl->fc_port;
1207}
1208
1209/*
1210 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1211 */
1212static void tcm_qla2xxx_set_sess_by_s_id(
1213        struct tcm_qla2xxx_lport *lport,
1214        struct se_node_acl *new_se_nacl,
1215        struct tcm_qla2xxx_nacl *nacl,
1216        struct se_session *se_sess,
1217        struct fc_port *fc_port,
1218        uint8_t *s_id)
1219{
1220        u32 key;
1221        void *slot;
1222        int rc;
1223
1224        key = sid_to_key(s_id);
1225        pr_debug("set_sess_by_s_id: %06x\n", key);
1226
1227        slot = btree_lookup32(&lport->lport_fcport_map, key);
1228        if (!slot) {
1229                if (new_se_nacl) {
1230                        pr_debug("Setting up new fc_port entry to new_se_nacl\n");
1231                        nacl->nport_id = key;
1232                        rc = btree_insert32(&lport->lport_fcport_map, key,
1233                                        new_se_nacl, GFP_ATOMIC);
1234                        if (rc)
1235                                printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
1236                                    (int)key);
1237                } else {
1238                        pr_debug("Wiping nonexisting fc_port entry\n");
1239                }
1240
1241                fc_port->se_sess = se_sess;
1242                nacl->fc_port = fc_port;
1243                return;
1244        }
1245
1246        if (nacl->fc_port) {
1247                if (new_se_nacl == NULL) {
1248                        pr_debug("Clearing existing nacl->fc_port and fc_port entry\n");
1249                        btree_remove32(&lport->lport_fcport_map, key);
1250                        nacl->fc_port = NULL;
1251                        return;
1252                }
1253                pr_debug("Replacing existing nacl->fc_port and fc_port entry\n");
1254                btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1255                fc_port->se_sess = se_sess;
1256                nacl->fc_port = fc_port;
1257                return;
1258        }
1259
1260        if (new_se_nacl == NULL) {
1261                pr_debug("Clearing existing fc_port entry\n");
1262                btree_remove32(&lport->lport_fcport_map, key);
1263                return;
1264        }
1265
1266        pr_debug("Replacing existing fc_port entry w/o active nacl->fc_port\n");
1267        btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1268        fc_port->se_sess = se_sess;
1269        nacl->fc_port = fc_port;
1270
1271        pr_debug("Setup nacl->fc_port %p by s_id for se_nacl: %p, initiatorname: %s\n",
1272            nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
1273}
1274
1275/*
1276 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1277 */
1278static struct fc_port *tcm_qla2xxx_find_sess_by_loop_id(
1279        scsi_qla_host_t *vha,
1280        const uint16_t loop_id)
1281{
1282        struct tcm_qla2xxx_lport *lport;
1283        struct se_node_acl *se_nacl;
1284        struct tcm_qla2xxx_nacl *nacl;
1285        struct tcm_qla2xxx_fc_loopid *fc_loopid;
1286
1287        lport = vha->vha_tgt.target_lport_ptr;
1288        if (!lport) {
1289                pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1290                dump_stack();
1291                return NULL;
1292        }
1293
1294        pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1295
1296        fc_loopid = lport->lport_loopid_map + loop_id;
1297        se_nacl = fc_loopid->se_nacl;
1298        if (!se_nacl) {
1299                pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
1300                    loop_id);
1301                return NULL;
1302        }
1303
1304        nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1305
1306        if (!nacl->fc_port) {
1307                pr_err("Unable to locate struct fc_port\n");
1308                return NULL;
1309        }
1310
1311        return nacl->fc_port;
1312}
1313
1314/*
1315 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1316 */
1317static void tcm_qla2xxx_set_sess_by_loop_id(
1318        struct tcm_qla2xxx_lport *lport,
1319        struct se_node_acl *new_se_nacl,
1320        struct tcm_qla2xxx_nacl *nacl,
1321        struct se_session *se_sess,
1322        struct fc_port *fc_port,
1323        uint16_t loop_id)
1324{
1325        struct se_node_acl *saved_nacl;
1326        struct tcm_qla2xxx_fc_loopid *fc_loopid;
1327
1328        pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1329
1330        fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
1331                        lport->lport_loopid_map)[loop_id];
1332
1333        saved_nacl = fc_loopid->se_nacl;
1334        if (!saved_nacl) {
1335                pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
1336                fc_loopid->se_nacl = new_se_nacl;
1337                if (fc_port->se_sess != se_sess)
1338                        fc_port->se_sess = se_sess;
1339                if (nacl->fc_port != fc_port)
1340                        nacl->fc_port = fc_port;
1341                return;
1342        }
1343
1344        if (nacl->fc_port) {
1345                if (new_se_nacl == NULL) {
1346                        pr_debug("Clearing nacl->fc_port and fc_loopid->se_nacl\n");
1347                        fc_loopid->se_nacl = NULL;
1348                        nacl->fc_port = NULL;
1349                        return;
1350                }
1351
1352                pr_debug("Replacing existing nacl->fc_port and fc_loopid->se_nacl\n");
1353                fc_loopid->se_nacl = new_se_nacl;
1354                if (fc_port->se_sess != se_sess)
1355                        fc_port->se_sess = se_sess;
1356                if (nacl->fc_port != fc_port)
1357                        nacl->fc_port = fc_port;
1358                return;
1359        }
1360
1361        if (new_se_nacl == NULL) {
1362                pr_debug("Clearing fc_loopid->se_nacl\n");
1363                fc_loopid->se_nacl = NULL;
1364                return;
1365        }
1366
1367        pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->fc_port\n");
1368        fc_loopid->se_nacl = new_se_nacl;
1369        if (fc_port->se_sess != se_sess)
1370                fc_port->se_sess = se_sess;
1371        if (nacl->fc_port != fc_port)
1372                nacl->fc_port = fc_port;
1373
1374        pr_debug("Setup nacl->fc_port %p by loop_id for se_nacl: %p, initiatorname: %s\n",
1375            nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
1376}
1377
1378/*
1379 * Should always be called with qla_hw_data->tgt.sess_lock held.
1380 */
1381static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
1382                struct tcm_qla2xxx_nacl *nacl, struct fc_port *sess)
1383{
1384        struct se_session *se_sess = sess->se_sess;
1385        unsigned char be_sid[3];
1386
1387        be_sid[0] = sess->d_id.b.domain;
1388        be_sid[1] = sess->d_id.b.area;
1389        be_sid[2] = sess->d_id.b.al_pa;
1390
1391        tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
1392                                sess, be_sid);
1393        tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
1394                                sess, sess->loop_id);
1395}
1396
1397static void tcm_qla2xxx_free_session(struct fc_port *sess)
1398{
1399        struct qla_tgt *tgt = sess->tgt;
1400        struct qla_hw_data *ha = tgt->ha;
1401        scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1402        struct se_session *se_sess;
1403        struct se_node_acl *se_nacl;
1404        struct tcm_qla2xxx_lport *lport;
1405        struct tcm_qla2xxx_nacl *nacl;
1406
1407        BUG_ON(in_interrupt());
1408
1409        se_sess = sess->se_sess;
1410        if (!se_sess) {
1411                pr_err("struct fc_port->se_sess is NULL\n");
1412                dump_stack();
1413                return;
1414        }
1415        se_nacl = se_sess->se_node_acl;
1416        nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1417
1418        lport = vha->vha_tgt.target_lport_ptr;
1419        if (!lport) {
1420                pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1421                dump_stack();
1422                return;
1423        }
1424        target_wait_for_sess_cmds(se_sess);
1425
1426        transport_deregister_session_configfs(sess->se_sess);
1427        transport_deregister_session(sess->se_sess);
1428}
1429
1430/*
1431 * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
1432 * to locate struct se_node_acl
1433 */
1434static int tcm_qla2xxx_check_initiator_node_acl(
1435        scsi_qla_host_t *vha,
1436        unsigned char *fc_wwpn,
1437        struct fc_port *qlat_sess)
1438{
1439        struct qla_hw_data *ha = vha->hw;
1440        struct tcm_qla2xxx_lport *lport;
1441        struct tcm_qla2xxx_tpg *tpg;
1442        struct tcm_qla2xxx_nacl *nacl;
1443        struct se_portal_group *se_tpg;
1444        struct se_node_acl *se_nacl;
1445        struct se_session *se_sess;
1446        unsigned char port_name[36];
1447        unsigned long flags;
1448        int num_tags = (ha->cur_fw_xcb_count) ? ha->cur_fw_xcb_count :
1449                       TCM_QLA2XXX_DEFAULT_TAGS;
1450        unsigned char be_sid[3];
1451
1452        lport = vha->vha_tgt.target_lport_ptr;
1453        if (!lport) {
1454                pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1455                dump_stack();
1456                return -EINVAL;
1457        }
1458        /*
1459         * Locate the TPG=1 reference..
1460         */
1461        tpg = lport->tpg_1;
1462        if (!tpg) {
1463                pr_err("Unable to lcoate struct tcm_qla2xxx_lport->tpg_1\n");
1464                return -EINVAL;
1465        }
1466        se_tpg = &tpg->se_tpg;
1467
1468        se_sess = transport_init_session_tags(num_tags,
1469                                              sizeof(struct qla_tgt_cmd),
1470                                              TARGET_PROT_ALL);
1471        if (IS_ERR(se_sess)) {
1472                pr_err("Unable to initialize struct se_session\n");
1473                return PTR_ERR(se_sess);
1474        }
1475        /*
1476         * Format the FCP Initiator port_name into colon seperated values to
1477         * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
1478         */
1479        memset(&port_name, 0, 36);
1480        snprintf(port_name, 36, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1481                fc_wwpn[0], fc_wwpn[1], fc_wwpn[2], fc_wwpn[3], fc_wwpn[4],
1482                fc_wwpn[5], fc_wwpn[6], fc_wwpn[7]);
1483        /*
1484         * Locate our struct se_node_acl either from an explict NodeACL created
1485         * via ConfigFS, or via running in TPG demo mode.
1486         */
1487        se_sess->se_node_acl = core_tpg_check_initiator_node_acl(se_tpg,
1488                                        port_name);
1489        if (!se_sess->se_node_acl) {
1490                transport_free_session(se_sess);
1491                return -EINVAL;
1492        }
1493        se_nacl = se_sess->se_node_acl;
1494        nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1495        be_sid[0] = qlat_sess->d_id.b.domain;
1496        be_sid[1] = qlat_sess->d_id.b.area;
1497        be_sid[2] = qlat_sess->d_id.b.al_pa;
1498
1499        /*
1500         * And now setup the new se_nacl and session pointers into our HW lport
1501         * mappings for fabric S_ID and LOOP_ID.
1502         */
1503        spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1504        tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess,
1505                        qlat_sess, be_sid);
1506        tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, se_sess,
1507                        qlat_sess, qlat_sess->loop_id);
1508        spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1509        /*
1510         * Finally register the new FC Nexus with TCM
1511         */
1512        __transport_register_session(se_nacl->se_tpg, se_nacl, se_sess, qlat_sess);
1513
1514        return 0;
1515}
1516
1517static void tcm_qla2xxx_update_sess(struct fc_port *sess, port_id_t s_id,
1518                                    uint16_t loop_id, bool conf_compl_supported)
1519{
1520        struct qla_tgt *tgt = sess->tgt;
1521        struct qla_hw_data *ha = tgt->ha;
1522        scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1523        struct tcm_qla2xxx_lport *lport = vha->vha_tgt.target_lport_ptr;
1524        struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
1525        struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1526                        struct tcm_qla2xxx_nacl, se_node_acl);
1527        u32 key;
1528
1529
1530        if (sess->loop_id != loop_id || sess->d_id.b24 != s_id.b24)
1531                pr_info("Updating session %p from port %8phC loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
1532                    sess, sess->port_name,
1533                    sess->loop_id, loop_id, sess->d_id.b.domain,
1534                    sess->d_id.b.area, sess->d_id.b.al_pa, s_id.b.domain,
1535                    s_id.b.area, s_id.b.al_pa);
1536
1537        if (sess->loop_id != loop_id) {
1538                /*
1539                 * Because we can shuffle loop IDs around and we
1540                 * update different sessions non-atomically, we might
1541                 * have overwritten this session's old loop ID
1542                 * already, and we might end up overwriting some other
1543                 * session that will be updated later.  So we have to
1544                 * be extra careful and we can't warn about those things...
1545                 */
1546                if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
1547                        lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
1548
1549                lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
1550
1551                sess->loop_id = loop_id;
1552        }
1553
1554        if (sess->d_id.b24 != s_id.b24) {
1555                key = (((u32) sess->d_id.b.domain << 16) |
1556                       ((u32) sess->d_id.b.area   <<  8) |
1557                       ((u32) sess->d_id.b.al_pa));
1558
1559                if (btree_lookup32(&lport->lport_fcport_map, key))
1560                        WARN(btree_remove32(&lport->lport_fcport_map, key) !=
1561                            se_nacl, "Found wrong se_nacl when updating s_id %x:%x:%x\n",
1562                            sess->d_id.b.domain, sess->d_id.b.area,
1563                            sess->d_id.b.al_pa);
1564                else
1565                        WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
1566                             sess->d_id.b.domain, sess->d_id.b.area,
1567                             sess->d_id.b.al_pa);
1568
1569                key = (((u32) s_id.b.domain << 16) |
1570                       ((u32) s_id.b.area   <<  8) |
1571                       ((u32) s_id.b.al_pa));
1572
1573                if (btree_lookup32(&lport->lport_fcport_map, key)) {
1574                        WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
1575                             s_id.b.domain, s_id.b.area, s_id.b.al_pa);
1576                        btree_update32(&lport->lport_fcport_map, key, se_nacl);
1577                } else {
1578                        btree_insert32(&lport->lport_fcport_map, key, se_nacl,
1579                            GFP_ATOMIC);
1580                }
1581
1582                sess->d_id = s_id;
1583                nacl->nport_id = key;
1584        }
1585
1586        sess->conf_compl_supported = conf_compl_supported;
1587
1588        /* Reset logout parameters to default */
1589        sess->logout_on_delete = 1;
1590        sess->keep_nport_handle = 0;
1591}
1592
1593/*
1594 * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
1595 */
1596static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
1597        .handle_cmd             = tcm_qla2xxx_handle_cmd,
1598        .handle_data            = tcm_qla2xxx_handle_data,
1599        .handle_tmr             = tcm_qla2xxx_handle_tmr,
1600        .free_cmd               = tcm_qla2xxx_free_cmd,
1601        .free_mcmd              = tcm_qla2xxx_free_mcmd,
1602        .free_session           = tcm_qla2xxx_free_session,
1603        .update_sess            = tcm_qla2xxx_update_sess,
1604        .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
1605        .find_sess_by_s_id      = tcm_qla2xxx_find_sess_by_s_id,
1606        .find_sess_by_loop_id   = tcm_qla2xxx_find_sess_by_loop_id,
1607        .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
1608        .put_sess               = tcm_qla2xxx_put_sess,
1609        .shutdown_sess          = tcm_qla2xxx_shutdown_sess,
1610        .get_dif_tags           = tcm_qla2xxx_dif_tags,
1611        .chk_dif_tags           = tcm_qla2xxx_chk_dif_tags,
1612};
1613
1614static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
1615{
1616        int rc;
1617
1618        rc = btree_init32(&lport->lport_fcport_map);
1619        if (rc) {
1620                pr_err("Unable to initialize lport->lport_fcport_map btree\n");
1621                return rc;
1622        }
1623
1624        lport->lport_loopid_map = vzalloc(sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1625        if (!lport->lport_loopid_map) {
1626                pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
1627                    sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1628                btree_destroy32(&lport->lport_fcport_map);
1629                return -ENOMEM;
1630        }
1631        pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
1632               sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1633        return 0;
1634}
1635
1636static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha,
1637                                         void *target_lport_ptr,
1638                                         u64 npiv_wwpn, u64 npiv_wwnn)
1639{
1640        struct qla_hw_data *ha = vha->hw;
1641        struct tcm_qla2xxx_lport *lport =
1642                        (struct tcm_qla2xxx_lport *)target_lport_ptr;
1643        /*
1644         * Setup tgt_ops, local pointer to vha and target_lport_ptr
1645         */
1646        ha->tgt.tgt_ops = &tcm_qla2xxx_template;
1647        vha->vha_tgt.target_lport_ptr = target_lport_ptr;
1648        lport->qla_vha = vha;
1649
1650        return 0;
1651}
1652
1653static struct se_wwn *tcm_qla2xxx_make_lport(
1654        struct target_fabric_configfs *tf,
1655        struct config_group *group,
1656        const char *name)
1657{
1658        struct tcm_qla2xxx_lport *lport;
1659        u64 wwpn;
1660        int ret = -ENODEV;
1661
1662        if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
1663                return ERR_PTR(-EINVAL);
1664
1665        lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1666        if (!lport) {
1667                pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
1668                return ERR_PTR(-ENOMEM);
1669        }
1670        lport->lport_wwpn = wwpn;
1671        tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
1672                                wwpn);
1673        sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
1674
1675        ret = tcm_qla2xxx_init_lport(lport);
1676        if (ret != 0)
1677                goto out;
1678
1679        ret = qlt_lport_register(lport, wwpn, 0, 0,
1680                                 tcm_qla2xxx_lport_register_cb);
1681        if (ret != 0)
1682                goto out_lport;
1683
1684        return &lport->lport_wwn;
1685out_lport:
1686        vfree(lport->lport_loopid_map);
1687        btree_destroy32(&lport->lport_fcport_map);
1688out:
1689        kfree(lport);
1690        return ERR_PTR(ret);
1691}
1692
1693static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
1694{
1695        struct tcm_qla2xxx_lport *lport = container_of(wwn,
1696                        struct tcm_qla2xxx_lport, lport_wwn);
1697        struct scsi_qla_host *vha = lport->qla_vha;
1698        struct se_node_acl *node;
1699        u32 key = 0;
1700
1701        /*
1702         * Call into qla2x_target.c LLD logic to complete the
1703         * shutdown of struct qla_tgt after the call to
1704         * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
1705         */
1706        if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stopped)
1707                qlt_stop_phase2(vha->vha_tgt.qla_tgt);
1708
1709        qlt_lport_deregister(vha);
1710
1711        vfree(lport->lport_loopid_map);
1712        btree_for_each_safe32(&lport->lport_fcport_map, key, node)
1713                btree_remove32(&lport->lport_fcport_map, key);
1714        btree_destroy32(&lport->lport_fcport_map);
1715        kfree(lport);
1716}
1717
1718static int tcm_qla2xxx_lport_register_npiv_cb(struct scsi_qla_host *base_vha,
1719                                              void *target_lport_ptr,
1720                                              u64 npiv_wwpn, u64 npiv_wwnn)
1721{
1722        struct fc_vport *vport;
1723        struct Scsi_Host *sh = base_vha->host;
1724        struct scsi_qla_host *npiv_vha;
1725        struct tcm_qla2xxx_lport *lport =
1726                        (struct tcm_qla2xxx_lport *)target_lport_ptr;
1727        struct tcm_qla2xxx_lport *base_lport =
1728                        (struct tcm_qla2xxx_lport *)base_vha->vha_tgt.target_lport_ptr;
1729        struct tcm_qla2xxx_tpg *base_tpg;
1730        struct fc_vport_identifiers vport_id;
1731
1732        if (qla_ini_mode_enabled(base_vha)) {
1733                pr_err("qla2xxx base_vha not enabled for target mode\n");
1734                return -EPERM;
1735        }
1736
1737        if (!base_lport || !base_lport->tpg_1 ||
1738            !atomic_read(&base_lport->tpg_1->lport_tpg_enabled)) {
1739                pr_err("qla2xxx base_lport or tpg_1 not available\n");
1740                return -EPERM;
1741        }
1742        base_tpg = base_lport->tpg_1;
1743
1744        memset(&vport_id, 0, sizeof(vport_id));
1745        vport_id.port_name = npiv_wwpn;
1746        vport_id.node_name = npiv_wwnn;
1747        vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
1748        vport_id.vport_type = FC_PORTTYPE_NPIV;
1749        vport_id.disable = false;
1750
1751        vport = fc_vport_create(sh, 0, &vport_id);
1752        if (!vport) {
1753                pr_err("fc_vport_create failed for qla2xxx_npiv\n");
1754                return -ENODEV;
1755        }
1756        /*
1757         * Setup local pointer to NPIV vhba + target_lport_ptr
1758         */
1759        npiv_vha = (struct scsi_qla_host *)vport->dd_data;
1760        npiv_vha->vha_tgt.target_lport_ptr = target_lport_ptr;
1761        lport->qla_vha = npiv_vha;
1762        scsi_host_get(npiv_vha->host);
1763        return 0;
1764}
1765
1766
1767static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
1768        struct target_fabric_configfs *tf,
1769        struct config_group *group,
1770        const char *name)
1771{
1772        struct tcm_qla2xxx_lport *lport;
1773        u64 phys_wwpn, npiv_wwpn, npiv_wwnn;
1774        char *p, tmp[128];
1775        int ret;
1776
1777        snprintf(tmp, 128, "%s", name);
1778
1779        p = strchr(tmp, '@');
1780        if (!p) {
1781                pr_err("Unable to locate NPIV '@' separator\n");
1782                return ERR_PTR(-EINVAL);
1783        }
1784        *p++ = '\0';
1785
1786        if (tcm_qla2xxx_parse_wwn(tmp, &phys_wwpn, 1) < 0)
1787                return ERR_PTR(-EINVAL);
1788
1789        if (tcm_qla2xxx_npiv_parse_wwn(p, strlen(p)+1,
1790                                       &npiv_wwpn, &npiv_wwnn) < 0)
1791                return ERR_PTR(-EINVAL);
1792
1793        lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1794        if (!lport) {
1795                pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
1796                return ERR_PTR(-ENOMEM);
1797        }
1798        lport->lport_npiv_wwpn = npiv_wwpn;
1799        lport->lport_npiv_wwnn = npiv_wwnn;
1800        sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
1801
1802        ret = tcm_qla2xxx_init_lport(lport);
1803        if (ret != 0)
1804                goto out;
1805
1806        ret = qlt_lport_register(lport, phys_wwpn, npiv_wwpn, npiv_wwnn,
1807                                 tcm_qla2xxx_lport_register_npiv_cb);
1808        if (ret != 0)
1809                goto out_lport;
1810
1811        return &lport->lport_wwn;
1812out_lport:
1813        vfree(lport->lport_loopid_map);
1814        btree_destroy32(&lport->lport_fcport_map);
1815out:
1816        kfree(lport);
1817        return ERR_PTR(ret);
1818}
1819
1820static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
1821{
1822        struct tcm_qla2xxx_lport *lport = container_of(wwn,
1823                        struct tcm_qla2xxx_lport, lport_wwn);
1824        struct scsi_qla_host *npiv_vha = lport->qla_vha;
1825        struct qla_hw_data *ha = npiv_vha->hw;
1826        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1827
1828        scsi_host_put(npiv_vha->host);
1829        /*
1830         * Notify libfc that we want to release the vha->fc_vport
1831         */
1832        fc_vport_terminate(npiv_vha->fc_vport);
1833        scsi_host_put(base_vha->host);
1834        kfree(lport);
1835}
1836
1837
1838static ssize_t tcm_qla2xxx_wwn_version_show(struct config_item *item,
1839                char *page)
1840{
1841        return sprintf(page,
1842            "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on %s\n",
1843            QLA2XXX_VERSION, utsname()->sysname,
1844            utsname()->machine, utsname()->release);
1845}
1846
1847CONFIGFS_ATTR_RO(tcm_qla2xxx_wwn_, version);
1848
1849static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
1850        &tcm_qla2xxx_wwn_attr_version,
1851        NULL,
1852};
1853
1854static const struct target_core_fabric_ops tcm_qla2xxx_ops = {
1855        .module                         = THIS_MODULE,
1856        .name                           = "qla2xxx",
1857        .node_acl_size                  = sizeof(struct tcm_qla2xxx_nacl),
1858        /*
1859         * XXX: Limit assumes single page per scatter-gather-list entry.
1860         * Current maximum is ~4.9 MB per se_cmd->t_data_sg with PAGE_SIZE=4096
1861         */
1862        .max_data_sg_nents              = 1200,
1863        .get_fabric_name                = tcm_qla2xxx_get_fabric_name,
1864        .tpg_get_wwn                    = tcm_qla2xxx_get_fabric_wwn,
1865        .tpg_get_tag                    = tcm_qla2xxx_get_tag,
1866        .tpg_check_demo_mode            = tcm_qla2xxx_check_demo_mode,
1867        .tpg_check_demo_mode_cache      = tcm_qla2xxx_check_demo_mode_cache,
1868        .tpg_check_demo_mode_write_protect =
1869                                        tcm_qla2xxx_check_demo_write_protect,
1870        .tpg_check_prod_mode_write_protect =
1871            tcm_qla2xxx_check_prod_write_protect,
1872        .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
1873        .tpg_get_inst_index             = tcm_qla2xxx_tpg_get_inst_index,
1874        .check_stop_free                = tcm_qla2xxx_check_stop_free,
1875        .release_cmd                    = tcm_qla2xxx_release_cmd,
1876        .shutdown_session               = tcm_qla2xxx_shutdown_session,
1877        .close_session                  = tcm_qla2xxx_close_session,
1878        .sess_get_index                 = tcm_qla2xxx_sess_get_index,
1879        .sess_get_initiator_sid         = NULL,
1880        .write_pending                  = tcm_qla2xxx_write_pending,
1881        .write_pending_status           = tcm_qla2xxx_write_pending_status,
1882        .set_default_node_attributes    = tcm_qla2xxx_set_default_node_attrs,
1883        .get_cmd_state                  = tcm_qla2xxx_get_cmd_state,
1884        .queue_data_in                  = tcm_qla2xxx_queue_data_in,
1885        .queue_status                   = tcm_qla2xxx_queue_status,
1886        .queue_tm_rsp                   = tcm_qla2xxx_queue_tm_rsp,
1887        .aborted_task                   = tcm_qla2xxx_aborted_task,
1888        /*
1889         * Setup function pointers for generic logic in
1890         * target_core_fabric_configfs.c
1891         */
1892        .fabric_make_wwn                = tcm_qla2xxx_make_lport,
1893        .fabric_drop_wwn                = tcm_qla2xxx_drop_lport,
1894        .fabric_make_tpg                = tcm_qla2xxx_make_tpg,
1895        .fabric_drop_tpg                = tcm_qla2xxx_drop_tpg,
1896        .fabric_init_nodeacl            = tcm_qla2xxx_init_nodeacl,
1897
1898        .tfc_wwn_attrs                  = tcm_qla2xxx_wwn_attrs,
1899        .tfc_tpg_base_attrs             = tcm_qla2xxx_tpg_attrs,
1900        .tfc_tpg_attrib_attrs           = tcm_qla2xxx_tpg_attrib_attrs,
1901};
1902
1903static const struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
1904        .module                         = THIS_MODULE,
1905        .name                           = "qla2xxx_npiv",
1906        .node_acl_size                  = sizeof(struct tcm_qla2xxx_nacl),
1907        .get_fabric_name                = tcm_qla2xxx_npiv_get_fabric_name,
1908        .tpg_get_wwn                    = tcm_qla2xxx_get_fabric_wwn,
1909        .tpg_get_tag                    = tcm_qla2xxx_get_tag,
1910        .tpg_check_demo_mode            = tcm_qla2xxx_check_demo_mode,
1911        .tpg_check_demo_mode_cache      = tcm_qla2xxx_check_demo_mode_cache,
1912        .tpg_check_prod_mode_write_protect =
1913            tcm_qla2xxx_check_prod_write_protect,
1914        .tpg_check_demo_mode_write_protect =
1915                                        tcm_qla2xxx_check_demo_write_protect,
1916        .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
1917        .tpg_get_inst_index             = tcm_qla2xxx_tpg_get_inst_index,
1918        .release_cmd                    = tcm_qla2xxx_release_cmd,
1919        .shutdown_session               = tcm_qla2xxx_shutdown_session,
1920        .close_session                  = tcm_qla2xxx_close_session,
1921        .sess_get_index                 = tcm_qla2xxx_sess_get_index,
1922        .sess_get_initiator_sid         = NULL,
1923        .write_pending                  = tcm_qla2xxx_write_pending,
1924        .write_pending_status           = tcm_qla2xxx_write_pending_status,
1925        .set_default_node_attributes    = tcm_qla2xxx_set_default_node_attrs,
1926        .get_cmd_state                  = tcm_qla2xxx_get_cmd_state,
1927        .queue_data_in                  = tcm_qla2xxx_queue_data_in,
1928        .queue_status                   = tcm_qla2xxx_queue_status,
1929        .queue_tm_rsp                   = tcm_qla2xxx_queue_tm_rsp,
1930        .aborted_task                   = tcm_qla2xxx_aborted_task,
1931        /*
1932         * Setup function pointers for generic logic in
1933         * target_core_fabric_configfs.c
1934         */
1935        .fabric_make_wwn                = tcm_qla2xxx_npiv_make_lport,
1936        .fabric_drop_wwn                = tcm_qla2xxx_npiv_drop_lport,
1937        .fabric_make_tpg                = tcm_qla2xxx_npiv_make_tpg,
1938        .fabric_drop_tpg                = tcm_qla2xxx_drop_tpg,
1939        .fabric_init_nodeacl            = tcm_qla2xxx_init_nodeacl,
1940
1941        .tfc_wwn_attrs                  = tcm_qla2xxx_wwn_attrs,
1942        .tfc_tpg_base_attrs             = tcm_qla2xxx_npiv_tpg_attrs,
1943};
1944
1945static int tcm_qla2xxx_register_configfs(void)
1946{
1947        int ret;
1948
1949        pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on %s\n",
1950            QLA2XXX_VERSION, utsname()->sysname,
1951            utsname()->machine, utsname()->release);
1952
1953        ret = target_register_template(&tcm_qla2xxx_ops);
1954        if (ret)
1955                return ret;
1956        ret = target_register_template(&tcm_qla2xxx_npiv_ops);
1957        if(ret)
1958                goto out_fabric;
1959
1960        tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
1961                                                WQ_MEM_RECLAIM, 0);
1962        if (!tcm_qla2xxx_free_wq) {
1963                ret = -ENOMEM;
1964                goto out_fabric_npiv;
1965        }
1966
1967        tcm_qla2xxx_cmd_wq = alloc_workqueue("tcm_qla2xxx_cmd", 0, 0);
1968        if (!tcm_qla2xxx_cmd_wq) {
1969                ret = -ENOMEM;
1970                goto out_free_wq;
1971        }
1972
1973        return 0;
1974
1975out_free_wq:
1976        destroy_workqueue(tcm_qla2xxx_free_wq);
1977out_fabric_npiv:
1978        target_unregister_template(&tcm_qla2xxx_npiv_ops);
1979out_fabric:
1980        target_unregister_template(&tcm_qla2xxx_ops);
1981        return ret;
1982}
1983
1984static void tcm_qla2xxx_deregister_configfs(void)
1985{
1986        destroy_workqueue(tcm_qla2xxx_cmd_wq);
1987        destroy_workqueue(tcm_qla2xxx_free_wq);
1988
1989        target_unregister_template(&tcm_qla2xxx_ops);
1990        target_unregister_template(&tcm_qla2xxx_npiv_ops);
1991}
1992
1993static int __init tcm_qla2xxx_init(void)
1994{
1995        int ret;
1996
1997        mark_tech_preview("QLA2XXX Target Mode Operation", THIS_MODULE);
1998
1999        ret = tcm_qla2xxx_register_configfs();
2000        if (ret < 0)
2001                return ret;
2002
2003        return 0;
2004}
2005
2006static void __exit tcm_qla2xxx_exit(void)
2007{
2008        tcm_qla2xxx_deregister_configfs();
2009}
2010
2011MODULE_DESCRIPTION("TCM QLA2XXX series NPIV enabled fabric driver");
2012MODULE_LICENSE("GPL");
2013module_init(tcm_qla2xxx_init);
2014module_exit(tcm_qla2xxx_exit);
2015