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