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