18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*******************************************************************************
38c2ecf20Sopenharmony_ci * This file contains tcm implementation using v4 configfs fabric infrastructure
48c2ecf20Sopenharmony_ci * for QLogic target mode HBAs
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * (c) Copyright 2010-2013 Datera, Inc.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Author: Nicholas A. Bellinger <nab@daterainc.com>
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * tcm_qla2xxx_parse_wwn() and tcm_qla2xxx_format_wwn() contains code from
118c2ecf20Sopenharmony_ci * the TCM_FC / Open-FCoE.org fabric module.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Copyright (c) 2010 Cisco Systems, Inc
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci ****************************************************************************/
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/utsname.h>
208c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
218c2ecf20Sopenharmony_ci#include <linux/list.h>
228c2ecf20Sopenharmony_ci#include <linux/slab.h>
238c2ecf20Sopenharmony_ci#include <linux/types.h>
248c2ecf20Sopenharmony_ci#include <linux/string.h>
258c2ecf20Sopenharmony_ci#include <linux/configfs.h>
268c2ecf20Sopenharmony_ci#include <linux/ctype.h>
278c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
288c2ecf20Sopenharmony_ci#include <scsi/scsi_host.h>
298c2ecf20Sopenharmony_ci#include <target/target_core_base.h>
308c2ecf20Sopenharmony_ci#include <target/target_core_fabric.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "qla_def.h"
338c2ecf20Sopenharmony_ci#include "qla_target.h"
348c2ecf20Sopenharmony_ci#include "tcm_qla2xxx.h"
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic struct workqueue_struct *tcm_qla2xxx_free_wq;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/*
398c2ecf20Sopenharmony_ci * Parse WWN.
408c2ecf20Sopenharmony_ci * If strict, we require lower-case hex and colon separators to be sure
418c2ecf20Sopenharmony_ci * the name is the same as what would be generated by ft_format_wwn()
428c2ecf20Sopenharmony_ci * so the name and wwn are mapped one-to-one.
438c2ecf20Sopenharmony_ci */
448c2ecf20Sopenharmony_cistatic ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	const char *cp;
478c2ecf20Sopenharmony_ci	char c;
488c2ecf20Sopenharmony_ci	u32 nibble;
498c2ecf20Sopenharmony_ci	u32 byte = 0;
508c2ecf20Sopenharmony_ci	u32 pos = 0;
518c2ecf20Sopenharmony_ci	u32 err;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	*wwn = 0;
548c2ecf20Sopenharmony_ci	for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
558c2ecf20Sopenharmony_ci		c = *cp;
568c2ecf20Sopenharmony_ci		if (c == '\n' && cp[1] == '\0')
578c2ecf20Sopenharmony_ci			continue;
588c2ecf20Sopenharmony_ci		if (strict && pos++ == 2 && byte++ < 7) {
598c2ecf20Sopenharmony_ci			pos = 0;
608c2ecf20Sopenharmony_ci			if (c == ':')
618c2ecf20Sopenharmony_ci				continue;
628c2ecf20Sopenharmony_ci			err = 1;
638c2ecf20Sopenharmony_ci			goto fail;
648c2ecf20Sopenharmony_ci		}
658c2ecf20Sopenharmony_ci		if (c == '\0') {
668c2ecf20Sopenharmony_ci			err = 2;
678c2ecf20Sopenharmony_ci			if (strict && byte != 8)
688c2ecf20Sopenharmony_ci				goto fail;
698c2ecf20Sopenharmony_ci			return cp - name;
708c2ecf20Sopenharmony_ci		}
718c2ecf20Sopenharmony_ci		err = 3;
728c2ecf20Sopenharmony_ci		if (isdigit(c))
738c2ecf20Sopenharmony_ci			nibble = c - '0';
748c2ecf20Sopenharmony_ci		else if (isxdigit(c) && (islower(c) || !strict))
758c2ecf20Sopenharmony_ci			nibble = tolower(c) - 'a' + 10;
768c2ecf20Sopenharmony_ci		else
778c2ecf20Sopenharmony_ci			goto fail;
788c2ecf20Sopenharmony_ci		*wwn = (*wwn << 4) | nibble;
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci	err = 4;
818c2ecf20Sopenharmony_cifail:
828c2ecf20Sopenharmony_ci	pr_debug("err %u len %zu pos %u byte %u\n",
838c2ecf20Sopenharmony_ci			err, cp - name, pos, byte);
848c2ecf20Sopenharmony_ci	return -1;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	u8 b[8];
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	put_unaligned_be64(wwn, b);
928c2ecf20Sopenharmony_ci	return snprintf(buf, len,
938c2ecf20Sopenharmony_ci		"%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
948c2ecf20Sopenharmony_ci		b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/*
988c2ecf20Sopenharmony_ci * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
998c2ecf20Sopenharmony_ci */
1008c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	unsigned int i, j;
1038c2ecf20Sopenharmony_ci	u8 wwn[8];
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	memset(wwn, 0, sizeof(wwn));
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	/* Validate and store the new name */
1088c2ecf20Sopenharmony_ci	for (i = 0, j = 0; i < 16; i++) {
1098c2ecf20Sopenharmony_ci		int value;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci		value = hex_to_bin(*ns++);
1128c2ecf20Sopenharmony_ci		if (value >= 0)
1138c2ecf20Sopenharmony_ci			j = (j << 4) | value;
1148c2ecf20Sopenharmony_ci		else
1158c2ecf20Sopenharmony_ci			return -EINVAL;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci		if (i % 2) {
1188c2ecf20Sopenharmony_ci			wwn[i/2] = j & 0xff;
1198c2ecf20Sopenharmony_ci			j = 0;
1208c2ecf20Sopenharmony_ci		}
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	*nm = wwn_to_u64(wwn);
1248c2ecf20Sopenharmony_ci	return 0;
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/*
1288c2ecf20Sopenharmony_ci * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
1298c2ecf20Sopenharmony_ci * store_fc_host_vport_create()
1308c2ecf20Sopenharmony_ci */
1318c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_npiv_parse_wwn(
1328c2ecf20Sopenharmony_ci	const char *name,
1338c2ecf20Sopenharmony_ci	size_t count,
1348c2ecf20Sopenharmony_ci	u64 *wwpn,
1358c2ecf20Sopenharmony_ci	u64 *wwnn)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	unsigned int cnt = count;
1388c2ecf20Sopenharmony_ci	int rc;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	*wwpn = 0;
1418c2ecf20Sopenharmony_ci	*wwnn = 0;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	/* count may include a LF at end of string */
1448c2ecf20Sopenharmony_ci	if (name[cnt-1] == '\n' || name[cnt-1] == 0)
1458c2ecf20Sopenharmony_ci		cnt--;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	/* validate we have enough characters for WWPN */
1488c2ecf20Sopenharmony_ci	if ((cnt != (16+1+16)) || (name[16] != ':'))
1498c2ecf20Sopenharmony_ci		return -EINVAL;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
1528c2ecf20Sopenharmony_ci	if (rc != 0)
1538c2ecf20Sopenharmony_ci		return rc;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
1568c2ecf20Sopenharmony_ci	if (rc != 0)
1578c2ecf20Sopenharmony_ci		return rc;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	return 0;
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1658c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_tpg, se_tpg);
1668c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport = tpg->lport;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	return lport->lport_naa_name;
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1748c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_tpg, se_tpg);
1758c2ecf20Sopenharmony_ci	return tpg->lport_tpgt;
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1818c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_tpg, se_tpg);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	return tpg->tpg_attrib.generate_node_acls;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1898c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_tpg, se_tpg);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	return tpg->tpg_attrib.cache_dynamic_acls;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1978c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_tpg, se_tpg);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	return tpg->tpg_attrib.demo_mode_write_protect;
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
2058c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_tpg, se_tpg);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	return tpg->tpg_attrib.prod_mode_write_protect;
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_check_demo_mode_login_only(struct se_portal_group *se_tpg)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
2138c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_tpg, se_tpg);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	return tpg->tpg_attrib.demo_mode_login_only;
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_check_prot_fabric_only(struct se_portal_group *se_tpg)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
2218c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_tpg, se_tpg);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	return tpg->tpg_attrib.fabric_prot_type;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
2298c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_tpg, se_tpg);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	return tpg->lport_tpgt;
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
2378c2ecf20Sopenharmony_ci			struct qla_tgt_mgmt_cmd, free_work);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	transport_generic_free_cmd(&mcmd->se_cmd, 0);
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci/*
2438c2ecf20Sopenharmony_ci * Called from qla_target_template->free_mcmd(), and will call
2448c2ecf20Sopenharmony_ci * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
2458c2ecf20Sopenharmony_ci * release callback.  qla_hw_data->hardware_lock is expected to be held
2468c2ecf20Sopenharmony_ci */
2478c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	if (!mcmd)
2508c2ecf20Sopenharmony_ci		return;
2518c2ecf20Sopenharmony_ci	INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
2528c2ecf20Sopenharmony_ci	queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_complete_free(struct work_struct *work)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	cmd->cmd_in_wq = 0;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	WARN_ON(cmd->trc_flags & TRC_CMD_FREE);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	/* To do: protect all tgt_counters manipulations with proper locking. */
2648c2ecf20Sopenharmony_ci	cmd->qpair->tgt_counters.qla_core_ret_sta_ctio++;
2658c2ecf20Sopenharmony_ci	cmd->trc_flags |= TRC_CMD_FREE;
2668c2ecf20Sopenharmony_ci	cmd->cmd_sent_to_fw = 0;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	transport_generic_free_cmd(&cmd->se_cmd, 0);
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic struct qla_tgt_cmd *tcm_qla2xxx_get_cmd(struct fc_port *sess)
2728c2ecf20Sopenharmony_ci{
2738c2ecf20Sopenharmony_ci	struct se_session *se_sess = sess->se_sess;
2748c2ecf20Sopenharmony_ci	struct qla_tgt_cmd *cmd;
2758c2ecf20Sopenharmony_ci	int tag, cpu;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
2788c2ecf20Sopenharmony_ci	if (tag < 0)
2798c2ecf20Sopenharmony_ci		return NULL;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
2828c2ecf20Sopenharmony_ci	memset(cmd, 0, sizeof(struct qla_tgt_cmd));
2838c2ecf20Sopenharmony_ci	cmd->se_cmd.map_tag = tag;
2848c2ecf20Sopenharmony_ci	cmd->se_cmd.map_cpu = cpu;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	return cmd;
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_rel_cmd(struct qla_tgt_cmd *cmd)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	target_free_tag(cmd->sess->se_sess, &cmd->se_cmd);
2928c2ecf20Sopenharmony_ci}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci/*
2958c2ecf20Sopenharmony_ci * Called from qla_target_template->free_cmd(), and will call
2968c2ecf20Sopenharmony_ci * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
2978c2ecf20Sopenharmony_ci * release callback.  qla_hw_data->hardware_lock is expected to be held
2988c2ecf20Sopenharmony_ci */
2998c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	cmd->qpair->tgt_counters.core_qla_free_cmd++;
3028c2ecf20Sopenharmony_ci	cmd->cmd_in_wq = 1;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	WARN_ON(cmd->trc_flags & TRC_CMD_DONE);
3058c2ecf20Sopenharmony_ci	cmd->trc_flags |= TRC_CMD_DONE;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
3088c2ecf20Sopenharmony_ci	queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci/*
3128c2ecf20Sopenharmony_ci * Called from struct target_core_fabric_ops->check_stop_free() context
3138c2ecf20Sopenharmony_ci */
3148c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	struct qla_tgt_cmd *cmd;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	if ((se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) == 0) {
3198c2ecf20Sopenharmony_ci		cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
3208c2ecf20Sopenharmony_ci		cmd->trc_flags |= TRC_CMD_CHK_STOP;
3218c2ecf20Sopenharmony_ci	}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	return target_put_sess_cmd(se_cmd);
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci/* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
3278c2ecf20Sopenharmony_ci * fabric descriptor @se_cmd command to release
3288c2ecf20Sopenharmony_ci */
3298c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	struct qla_tgt_cmd *cmd;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
3348c2ecf20Sopenharmony_ci		struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
3358c2ecf20Sopenharmony_ci				struct qla_tgt_mgmt_cmd, se_cmd);
3368c2ecf20Sopenharmony_ci		qlt_free_mcmd(mcmd);
3378c2ecf20Sopenharmony_ci		return;
3388c2ecf20Sopenharmony_ci	}
3398c2ecf20Sopenharmony_ci	cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	if (WARN_ON(cmd->cmd_sent_to_fw))
3428c2ecf20Sopenharmony_ci		return;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	qlt_free_cmd(cmd);
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_release_session(struct kref *kref)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	struct fc_port  *sess = container_of(kref,
3508c2ecf20Sopenharmony_ci	    struct fc_port, sess_kref);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	qlt_unreg_sess(sess);
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_put_sess(struct fc_port *sess)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	if (!sess)
3588c2ecf20Sopenharmony_ci		return;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	kref_put(&sess->sess_kref, tcm_qla2xxx_release_session);
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_close_session(struct se_session *se_sess)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	struct fc_port *sess = se_sess->fabric_sess_ptr;
3668c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha;
3678c2ecf20Sopenharmony_ci	unsigned long flags;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	BUG_ON(!sess);
3708c2ecf20Sopenharmony_ci	vha = sess->vha;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
3738c2ecf20Sopenharmony_ci	target_sess_cmd_list_set_waiting(se_sess);
3748c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	sess->explicit_logout = 1;
3778c2ecf20Sopenharmony_ci	tcm_qla2xxx_put_sess(sess);
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	return 0;
3838c2ecf20Sopenharmony_ci}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	struct qla_tgt_cmd *cmd = container_of(se_cmd,
3888c2ecf20Sopenharmony_ci				struct qla_tgt_cmd, se_cmd);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	if (cmd->aborted) {
3918c2ecf20Sopenharmony_ci		/* Cmd can loop during Q-full.  tcm_qla2xxx_aborted_task
3928c2ecf20Sopenharmony_ci		 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
3938c2ecf20Sopenharmony_ci		 * already kick start the free.
3948c2ecf20Sopenharmony_ci		 */
3958c2ecf20Sopenharmony_ci		pr_debug("write_pending aborted cmd[%p] refcount %d "
3968c2ecf20Sopenharmony_ci			"transport_state %x, t_state %x, se_cmd_flags %x\n",
3978c2ecf20Sopenharmony_ci			cmd, kref_read(&cmd->se_cmd.cmd_kref),
3988c2ecf20Sopenharmony_ci			cmd->se_cmd.transport_state,
3998c2ecf20Sopenharmony_ci			cmd->se_cmd.t_state,
4008c2ecf20Sopenharmony_ci			cmd->se_cmd.se_cmd_flags);
4018c2ecf20Sopenharmony_ci		transport_generic_request_failure(&cmd->se_cmd,
4028c2ecf20Sopenharmony_ci			TCM_CHECK_CONDITION_ABORT_CMD);
4038c2ecf20Sopenharmony_ci		return 0;
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci	cmd->trc_flags |= TRC_XFR_RDY;
4068c2ecf20Sopenharmony_ci	cmd->bufflen = se_cmd->data_length;
4078c2ecf20Sopenharmony_ci	cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	cmd->sg_cnt = se_cmd->t_data_nents;
4108c2ecf20Sopenharmony_ci	cmd->sg = se_cmd->t_data_sg;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	cmd->prot_sg_cnt = se_cmd->t_prot_nents;
4138c2ecf20Sopenharmony_ci	cmd->prot_sg = se_cmd->t_prot_sg;
4148c2ecf20Sopenharmony_ci	cmd->blk_sz  = se_cmd->se_dev->dev_attrib.block_size;
4158c2ecf20Sopenharmony_ci	se_cmd->pi_err = 0;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	/*
4188c2ecf20Sopenharmony_ci	 * qla_target.c:qlt_rdy_to_xfer() will call dma_map_sg() to setup
4198c2ecf20Sopenharmony_ci	 * the SGL mappings into PCIe memory for incoming FCP WRITE data.
4208c2ecf20Sopenharmony_ci	 */
4218c2ecf20Sopenharmony_ci	return qlt_rdy_to_xfer(cmd);
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	return;
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	if (!(se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
4328c2ecf20Sopenharmony_ci		struct qla_tgt_cmd *cmd = container_of(se_cmd,
4338c2ecf20Sopenharmony_ci				struct qla_tgt_cmd, se_cmd);
4348c2ecf20Sopenharmony_ci		return cmd->state;
4358c2ecf20Sopenharmony_ci	}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	return 0;
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci/*
4418c2ecf20Sopenharmony_ci * Called from process context in qla_target.c:qlt_do_work() code
4428c2ecf20Sopenharmony_ci */
4438c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
4448c2ecf20Sopenharmony_ci	unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
4458c2ecf20Sopenharmony_ci	int data_dir, int bidi)
4468c2ecf20Sopenharmony_ci{
4478c2ecf20Sopenharmony_ci	struct se_cmd *se_cmd = &cmd->se_cmd;
4488c2ecf20Sopenharmony_ci	struct se_session *se_sess;
4498c2ecf20Sopenharmony_ci	struct fc_port *sess;
4508c2ecf20Sopenharmony_ci#ifdef CONFIG_TCM_QLA2XXX_DEBUG
4518c2ecf20Sopenharmony_ci	struct se_portal_group *se_tpg;
4528c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg;
4538c2ecf20Sopenharmony_ci#endif
4548c2ecf20Sopenharmony_ci	int flags = TARGET_SCF_ACK_KREF;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	if (bidi)
4578c2ecf20Sopenharmony_ci		flags |= TARGET_SCF_BIDI_OP;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	if (se_cmd->cpuid != WORK_CPU_UNBOUND)
4608c2ecf20Sopenharmony_ci		flags |= TARGET_SCF_USE_CPUID;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	sess = cmd->sess;
4638c2ecf20Sopenharmony_ci	if (!sess) {
4648c2ecf20Sopenharmony_ci		pr_err("Unable to locate struct fc_port from qla_tgt_cmd\n");
4658c2ecf20Sopenharmony_ci		return -EINVAL;
4668c2ecf20Sopenharmony_ci	}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	se_sess = sess->se_sess;
4698c2ecf20Sopenharmony_ci	if (!se_sess) {
4708c2ecf20Sopenharmony_ci		pr_err("Unable to locate active struct se_session\n");
4718c2ecf20Sopenharmony_ci		return -EINVAL;
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci#ifdef CONFIG_TCM_QLA2XXX_DEBUG
4758c2ecf20Sopenharmony_ci	se_tpg = se_sess->se_tpg;
4768c2ecf20Sopenharmony_ci	tpg = container_of(se_tpg, struct tcm_qla2xxx_tpg, se_tpg);
4778c2ecf20Sopenharmony_ci	if (unlikely(tpg->tpg_attrib.jam_host)) {
4788c2ecf20Sopenharmony_ci		/* return, and dont run target_submit_cmd,discarding command */
4798c2ecf20Sopenharmony_ci		return 0;
4808c2ecf20Sopenharmony_ci	}
4818c2ecf20Sopenharmony_ci#endif
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	cmd->qpair->tgt_counters.qla_core_sbt_cmd++;
4848c2ecf20Sopenharmony_ci	return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
4858c2ecf20Sopenharmony_ci				cmd->unpacked_lun, data_length, fcp_task_attr,
4868c2ecf20Sopenharmony_ci				data_dir, flags);
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_handle_data_work(struct work_struct *work)
4908c2ecf20Sopenharmony_ci{
4918c2ecf20Sopenharmony_ci	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	/*
4948c2ecf20Sopenharmony_ci	 * Ensure that the complete FCP WRITE payload has been received.
4958c2ecf20Sopenharmony_ci	 * Otherwise return an exception via CHECK_CONDITION status.
4968c2ecf20Sopenharmony_ci	 */
4978c2ecf20Sopenharmony_ci	cmd->cmd_in_wq = 0;
4988c2ecf20Sopenharmony_ci	cmd->cmd_sent_to_fw = 0;
4998c2ecf20Sopenharmony_ci	if (cmd->aborted) {
5008c2ecf20Sopenharmony_ci		transport_generic_request_failure(&cmd->se_cmd,
5018c2ecf20Sopenharmony_ci			TCM_CHECK_CONDITION_ABORT_CMD);
5028c2ecf20Sopenharmony_ci		return;
5038c2ecf20Sopenharmony_ci	}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	cmd->qpair->tgt_counters.qla_core_ret_ctio++;
5068c2ecf20Sopenharmony_ci	if (!cmd->write_data_transferred) {
5078c2ecf20Sopenharmony_ci		switch (cmd->dif_err_code) {
5088c2ecf20Sopenharmony_ci		case DIF_ERR_GRD:
5098c2ecf20Sopenharmony_ci			cmd->se_cmd.pi_err =
5108c2ecf20Sopenharmony_ci			    TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
5118c2ecf20Sopenharmony_ci			break;
5128c2ecf20Sopenharmony_ci		case DIF_ERR_REF:
5138c2ecf20Sopenharmony_ci			cmd->se_cmd.pi_err =
5148c2ecf20Sopenharmony_ci			    TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
5158c2ecf20Sopenharmony_ci			break;
5168c2ecf20Sopenharmony_ci		case DIF_ERR_APP:
5178c2ecf20Sopenharmony_ci			cmd->se_cmd.pi_err =
5188c2ecf20Sopenharmony_ci			    TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
5198c2ecf20Sopenharmony_ci			break;
5208c2ecf20Sopenharmony_ci		case DIF_ERR_NONE:
5218c2ecf20Sopenharmony_ci		default:
5228c2ecf20Sopenharmony_ci			break;
5238c2ecf20Sopenharmony_ci		}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci		if (cmd->se_cmd.pi_err)
5268c2ecf20Sopenharmony_ci			transport_generic_request_failure(&cmd->se_cmd,
5278c2ecf20Sopenharmony_ci				cmd->se_cmd.pi_err);
5288c2ecf20Sopenharmony_ci		else
5298c2ecf20Sopenharmony_ci			transport_generic_request_failure(&cmd->se_cmd,
5308c2ecf20Sopenharmony_ci				TCM_CHECK_CONDITION_ABORT_CMD);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci		return;
5338c2ecf20Sopenharmony_ci	}
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	return target_execute_cmd(&cmd->se_cmd);
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci/*
5398c2ecf20Sopenharmony_ci * Called from qla_target.c:qlt_do_ctio_completion()
5408c2ecf20Sopenharmony_ci */
5418c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
5428c2ecf20Sopenharmony_ci{
5438c2ecf20Sopenharmony_ci	cmd->trc_flags |= TRC_DATA_IN;
5448c2ecf20Sopenharmony_ci	cmd->cmd_in_wq = 1;
5458c2ecf20Sopenharmony_ci	INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
5468c2ecf20Sopenharmony_ci	queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
5478c2ecf20Sopenharmony_ci}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_chk_dif_tags(uint32_t tag)
5508c2ecf20Sopenharmony_ci{
5518c2ecf20Sopenharmony_ci	return 0;
5528c2ecf20Sopenharmony_ci}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_dif_tags(struct qla_tgt_cmd *cmd,
5558c2ecf20Sopenharmony_ci    uint16_t *pfw_prot_opts)
5568c2ecf20Sopenharmony_ci{
5578c2ecf20Sopenharmony_ci	struct se_cmd *se_cmd = &cmd->se_cmd;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD))
5608c2ecf20Sopenharmony_ci		*pfw_prot_opts |= PO_DISABLE_GUARD_CHECK;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG))
5638c2ecf20Sopenharmony_ci		*pfw_prot_opts |= PO_DIS_APP_TAG_VALD;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	return 0;
5668c2ecf20Sopenharmony_ci}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci/*
5698c2ecf20Sopenharmony_ci * Called from qla_target.c:qlt_issue_task_mgmt()
5708c2ecf20Sopenharmony_ci */
5718c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, u64 lun,
5728c2ecf20Sopenharmony_ci	uint16_t tmr_func, uint32_t tag)
5738c2ecf20Sopenharmony_ci{
5748c2ecf20Sopenharmony_ci	struct fc_port *sess = mcmd->sess;
5758c2ecf20Sopenharmony_ci	struct se_cmd *se_cmd = &mcmd->se_cmd;
5768c2ecf20Sopenharmony_ci	int transl_tmr_func = 0;
5778c2ecf20Sopenharmony_ci	int flags = TARGET_SCF_ACK_KREF;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	switch (tmr_func) {
5808c2ecf20Sopenharmony_ci	case QLA_TGT_ABTS:
5818c2ecf20Sopenharmony_ci		pr_debug("%ld: ABTS received\n", sess->vha->host_no);
5828c2ecf20Sopenharmony_ci		transl_tmr_func = TMR_ABORT_TASK;
5838c2ecf20Sopenharmony_ci		flags |= TARGET_SCF_LOOKUP_LUN_FROM_TAG;
5848c2ecf20Sopenharmony_ci		break;
5858c2ecf20Sopenharmony_ci	case QLA_TGT_2G_ABORT_TASK:
5868c2ecf20Sopenharmony_ci		pr_debug("%ld: 2G Abort Task received\n", sess->vha->host_no);
5878c2ecf20Sopenharmony_ci		transl_tmr_func = TMR_ABORT_TASK;
5888c2ecf20Sopenharmony_ci		break;
5898c2ecf20Sopenharmony_ci	case QLA_TGT_CLEAR_ACA:
5908c2ecf20Sopenharmony_ci		pr_debug("%ld: CLEAR_ACA received\n", sess->vha->host_no);
5918c2ecf20Sopenharmony_ci		transl_tmr_func = TMR_CLEAR_ACA;
5928c2ecf20Sopenharmony_ci		break;
5938c2ecf20Sopenharmony_ci	case QLA_TGT_TARGET_RESET:
5948c2ecf20Sopenharmony_ci		pr_debug("%ld: TARGET_RESET received\n", sess->vha->host_no);
5958c2ecf20Sopenharmony_ci		transl_tmr_func = TMR_TARGET_WARM_RESET;
5968c2ecf20Sopenharmony_ci		break;
5978c2ecf20Sopenharmony_ci	case QLA_TGT_LUN_RESET:
5988c2ecf20Sopenharmony_ci		pr_debug("%ld: LUN_RESET received\n", sess->vha->host_no);
5998c2ecf20Sopenharmony_ci		transl_tmr_func = TMR_LUN_RESET;
6008c2ecf20Sopenharmony_ci		break;
6018c2ecf20Sopenharmony_ci	case QLA_TGT_CLEAR_TS:
6028c2ecf20Sopenharmony_ci		pr_debug("%ld: CLEAR_TS received\n", sess->vha->host_no);
6038c2ecf20Sopenharmony_ci		transl_tmr_func = TMR_CLEAR_TASK_SET;
6048c2ecf20Sopenharmony_ci		break;
6058c2ecf20Sopenharmony_ci	case QLA_TGT_ABORT_TS:
6068c2ecf20Sopenharmony_ci		pr_debug("%ld: ABORT_TS received\n", sess->vha->host_no);
6078c2ecf20Sopenharmony_ci		transl_tmr_func = TMR_ABORT_TASK_SET;
6088c2ecf20Sopenharmony_ci		break;
6098c2ecf20Sopenharmony_ci	default:
6108c2ecf20Sopenharmony_ci		pr_debug("%ld: Unknown task mgmt fn 0x%x\n",
6118c2ecf20Sopenharmony_ci		    sess->vha->host_no, tmr_func);
6128c2ecf20Sopenharmony_ci		return -ENOSYS;
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
6168c2ecf20Sopenharmony_ci	    transl_tmr_func, GFP_ATOMIC, tag, flags);
6178c2ecf20Sopenharmony_ci}
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_cistatic struct qla_tgt_cmd *tcm_qla2xxx_find_cmd_by_tag(struct fc_port *sess,
6208c2ecf20Sopenharmony_ci    uint64_t tag)
6218c2ecf20Sopenharmony_ci{
6228c2ecf20Sopenharmony_ci	struct qla_tgt_cmd *cmd = NULL;
6238c2ecf20Sopenharmony_ci	struct se_cmd *secmd;
6248c2ecf20Sopenharmony_ci	unsigned long flags;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	if (!sess->se_sess)
6278c2ecf20Sopenharmony_ci		return NULL;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	spin_lock_irqsave(&sess->se_sess->sess_cmd_lock, flags);
6308c2ecf20Sopenharmony_ci	list_for_each_entry(secmd, &sess->se_sess->sess_cmd_list, se_cmd_list) {
6318c2ecf20Sopenharmony_ci		/* skip task management functions, including tmr->task_cmd */
6328c2ecf20Sopenharmony_ci		if (secmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
6338c2ecf20Sopenharmony_ci			continue;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci		if (secmd->tag == tag) {
6368c2ecf20Sopenharmony_ci			cmd = container_of(secmd, struct qla_tgt_cmd, se_cmd);
6378c2ecf20Sopenharmony_ci			break;
6388c2ecf20Sopenharmony_ci		}
6398c2ecf20Sopenharmony_ci	}
6408c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&sess->se_sess->sess_cmd_lock, flags);
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	return cmd;
6438c2ecf20Sopenharmony_ci}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
6468c2ecf20Sopenharmony_ci{
6478c2ecf20Sopenharmony_ci	struct qla_tgt_cmd *cmd = container_of(se_cmd,
6488c2ecf20Sopenharmony_ci				struct qla_tgt_cmd, se_cmd);
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	if (cmd->aborted) {
6518c2ecf20Sopenharmony_ci		/* Cmd can loop during Q-full.  tcm_qla2xxx_aborted_task
6528c2ecf20Sopenharmony_ci		 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
6538c2ecf20Sopenharmony_ci		 * already kick start the free.
6548c2ecf20Sopenharmony_ci		 */
6558c2ecf20Sopenharmony_ci		pr_debug("queue_data_in aborted cmd[%p] refcount %d "
6568c2ecf20Sopenharmony_ci			"transport_state %x, t_state %x, se_cmd_flags %x\n",
6578c2ecf20Sopenharmony_ci			cmd, kref_read(&cmd->se_cmd.cmd_kref),
6588c2ecf20Sopenharmony_ci			cmd->se_cmd.transport_state,
6598c2ecf20Sopenharmony_ci			cmd->se_cmd.t_state,
6608c2ecf20Sopenharmony_ci			cmd->se_cmd.se_cmd_flags);
6618c2ecf20Sopenharmony_ci		return 0;
6628c2ecf20Sopenharmony_ci	}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	cmd->trc_flags |= TRC_XMIT_DATA;
6658c2ecf20Sopenharmony_ci	cmd->bufflen = se_cmd->data_length;
6668c2ecf20Sopenharmony_ci	cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	cmd->sg_cnt = se_cmd->t_data_nents;
6698c2ecf20Sopenharmony_ci	cmd->sg = se_cmd->t_data_sg;
6708c2ecf20Sopenharmony_ci	cmd->offset = 0;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	cmd->prot_sg_cnt = se_cmd->t_prot_nents;
6738c2ecf20Sopenharmony_ci	cmd->prot_sg = se_cmd->t_prot_sg;
6748c2ecf20Sopenharmony_ci	cmd->blk_sz  = se_cmd->se_dev->dev_attrib.block_size;
6758c2ecf20Sopenharmony_ci	se_cmd->pi_err = 0;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	/*
6788c2ecf20Sopenharmony_ci	 * Now queue completed DATA_IN the qla2xxx LLD and response ring
6798c2ecf20Sopenharmony_ci	 */
6808c2ecf20Sopenharmony_ci	return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
6818c2ecf20Sopenharmony_ci				se_cmd->scsi_status);
6828c2ecf20Sopenharmony_ci}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
6858c2ecf20Sopenharmony_ci{
6868c2ecf20Sopenharmony_ci	struct qla_tgt_cmd *cmd = container_of(se_cmd,
6878c2ecf20Sopenharmony_ci				struct qla_tgt_cmd, se_cmd);
6888c2ecf20Sopenharmony_ci	int xmit_type = QLA_TGT_XMIT_STATUS;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	if (cmd->aborted) {
6918c2ecf20Sopenharmony_ci		/*
6928c2ecf20Sopenharmony_ci		 * Cmd can loop during Q-full. tcm_qla2xxx_aborted_task
6938c2ecf20Sopenharmony_ci		 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
6948c2ecf20Sopenharmony_ci		 * already kick start the free.
6958c2ecf20Sopenharmony_ci		 */
6968c2ecf20Sopenharmony_ci		pr_debug(
6978c2ecf20Sopenharmony_ci		    "queue_data_in aborted cmd[%p] refcount %d transport_state %x, t_state %x, se_cmd_flags %x\n",
6988c2ecf20Sopenharmony_ci		    cmd, kref_read(&cmd->se_cmd.cmd_kref),
6998c2ecf20Sopenharmony_ci		    cmd->se_cmd.transport_state, cmd->se_cmd.t_state,
7008c2ecf20Sopenharmony_ci		    cmd->se_cmd.se_cmd_flags);
7018c2ecf20Sopenharmony_ci		return 0;
7028c2ecf20Sopenharmony_ci	}
7038c2ecf20Sopenharmony_ci	cmd->bufflen = se_cmd->data_length;
7048c2ecf20Sopenharmony_ci	cmd->sg = NULL;
7058c2ecf20Sopenharmony_ci	cmd->sg_cnt = 0;
7068c2ecf20Sopenharmony_ci	cmd->offset = 0;
7078c2ecf20Sopenharmony_ci	cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
7088c2ecf20Sopenharmony_ci	cmd->trc_flags |= TRC_XMIT_STATUS;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	if (se_cmd->data_direction == DMA_FROM_DEVICE) {
7118c2ecf20Sopenharmony_ci		/*
7128c2ecf20Sopenharmony_ci		 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
7138c2ecf20Sopenharmony_ci		 * for qla_tgt_xmit_response LLD code
7148c2ecf20Sopenharmony_ci		 */
7158c2ecf20Sopenharmony_ci		if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
7168c2ecf20Sopenharmony_ci			se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT;
7178c2ecf20Sopenharmony_ci			se_cmd->residual_count = 0;
7188c2ecf20Sopenharmony_ci		}
7198c2ecf20Sopenharmony_ci		se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
7208c2ecf20Sopenharmony_ci		se_cmd->residual_count += se_cmd->data_length;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci		cmd->bufflen = 0;
7238c2ecf20Sopenharmony_ci	}
7248c2ecf20Sopenharmony_ci	/*
7258c2ecf20Sopenharmony_ci	 * Now queue status response to qla2xxx LLD code and response ring
7268c2ecf20Sopenharmony_ci	 */
7278c2ecf20Sopenharmony_ci	return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
7288c2ecf20Sopenharmony_ci}
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
7318c2ecf20Sopenharmony_ci{
7328c2ecf20Sopenharmony_ci	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
7338c2ecf20Sopenharmony_ci	struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
7348c2ecf20Sopenharmony_ci				struct qla_tgt_mgmt_cmd, se_cmd);
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
7378c2ecf20Sopenharmony_ci			mcmd, se_tmr->function, se_tmr->response);
7388c2ecf20Sopenharmony_ci	/*
7398c2ecf20Sopenharmony_ci	 * Do translation between TCM TM response codes and
7408c2ecf20Sopenharmony_ci	 * QLA2xxx FC TM response codes.
7418c2ecf20Sopenharmony_ci	 */
7428c2ecf20Sopenharmony_ci	switch (se_tmr->response) {
7438c2ecf20Sopenharmony_ci	case TMR_FUNCTION_COMPLETE:
7448c2ecf20Sopenharmony_ci		mcmd->fc_tm_rsp = FC_TM_SUCCESS;
7458c2ecf20Sopenharmony_ci		break;
7468c2ecf20Sopenharmony_ci	case TMR_TASK_DOES_NOT_EXIST:
7478c2ecf20Sopenharmony_ci		mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
7488c2ecf20Sopenharmony_ci		break;
7498c2ecf20Sopenharmony_ci	case TMR_FUNCTION_REJECTED:
7508c2ecf20Sopenharmony_ci		mcmd->fc_tm_rsp = FC_TM_REJECT;
7518c2ecf20Sopenharmony_ci		break;
7528c2ecf20Sopenharmony_ci	case TMR_LUN_DOES_NOT_EXIST:
7538c2ecf20Sopenharmony_ci	default:
7548c2ecf20Sopenharmony_ci		mcmd->fc_tm_rsp = FC_TM_FAILED;
7558c2ecf20Sopenharmony_ci		break;
7568c2ecf20Sopenharmony_ci	}
7578c2ecf20Sopenharmony_ci	/*
7588c2ecf20Sopenharmony_ci	 * Queue the TM response to QLA2xxx LLD to build a
7598c2ecf20Sopenharmony_ci	 * CTIO response packet.
7608c2ecf20Sopenharmony_ci	 */
7618c2ecf20Sopenharmony_ci	qlt_xmit_tm_rsp(mcmd);
7628c2ecf20Sopenharmony_ci}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_aborted_task(struct se_cmd *se_cmd)
7658c2ecf20Sopenharmony_ci{
7668c2ecf20Sopenharmony_ci	struct qla_tgt_cmd *cmd = container_of(se_cmd,
7678c2ecf20Sopenharmony_ci				struct qla_tgt_cmd, se_cmd);
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	if (qlt_abort_cmd(cmd))
7708c2ecf20Sopenharmony_ci		return;
7718c2ecf20Sopenharmony_ci}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
7748c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_nacl *, struct fc_port *);
7758c2ecf20Sopenharmony_ci/*
7768c2ecf20Sopenharmony_ci * Expected to be called with struct qla_hw_data->tgt.sess_lock held
7778c2ecf20Sopenharmony_ci */
7788c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_clear_nacl_from_fcport_map(struct fc_port *sess)
7798c2ecf20Sopenharmony_ci{
7808c2ecf20Sopenharmony_ci	struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
7818c2ecf20Sopenharmony_ci	struct se_portal_group *se_tpg = se_nacl->se_tpg;
7828c2ecf20Sopenharmony_ci	struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
7838c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
7848c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_lport, lport_wwn);
7858c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
7868c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_nacl, se_node_acl);
7878c2ecf20Sopenharmony_ci	void *node;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
7928c2ecf20Sopenharmony_ci	if (WARN_ON(node && (node != se_nacl))) {
7938c2ecf20Sopenharmony_ci		/*
7948c2ecf20Sopenharmony_ci		 * The nacl no longer matches what we think it should be.
7958c2ecf20Sopenharmony_ci		 * Most likely a new dynamic acl has been added while
7968c2ecf20Sopenharmony_ci		 * someone dropped the hardware lock.  It clearly is a
7978c2ecf20Sopenharmony_ci		 * bug elsewhere, but this bit can't make things worse.
7988c2ecf20Sopenharmony_ci		 */
7998c2ecf20Sopenharmony_ci		btree_insert32(&lport->lport_fcport_map, nacl->nport_id,
8008c2ecf20Sopenharmony_ci			       node, GFP_ATOMIC);
8018c2ecf20Sopenharmony_ci	}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
8048c2ecf20Sopenharmony_ci	    se_nacl, nacl->nport_wwnn, nacl->nport_id);
8058c2ecf20Sopenharmony_ci	/*
8068c2ecf20Sopenharmony_ci	 * Now clear the se_nacl and session pointers from our HW lport lookup
8078c2ecf20Sopenharmony_ci	 * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
8088c2ecf20Sopenharmony_ci	 *
8098c2ecf20Sopenharmony_ci	 * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
8108c2ecf20Sopenharmony_ci	 * target_wait_for_sess_cmds() before the session waits for outstanding
8118c2ecf20Sopenharmony_ci	 * I/O to complete, to avoid a race between session shutdown execution
8128c2ecf20Sopenharmony_ci	 * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
8138c2ecf20Sopenharmony_ci	 */
8148c2ecf20Sopenharmony_ci	tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
8158c2ecf20Sopenharmony_ci}
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_shutdown_sess(struct fc_port *sess)
8188c2ecf20Sopenharmony_ci{
8198c2ecf20Sopenharmony_ci	target_sess_cmd_list_set_waiting(sess->se_sess);
8208c2ecf20Sopenharmony_ci}
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_init_nodeacl(struct se_node_acl *se_nacl,
8238c2ecf20Sopenharmony_ci		const char *name)
8248c2ecf20Sopenharmony_ci{
8258c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_nacl *nacl =
8268c2ecf20Sopenharmony_ci		container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
8278c2ecf20Sopenharmony_ci	u64 wwnn;
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
8308c2ecf20Sopenharmony_ci		return -EINVAL;
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	nacl->nport_wwnn = wwnn;
8338c2ecf20Sopenharmony_ci	tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	return 0;
8368c2ecf20Sopenharmony_ci}
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci/* Start items for tcm_qla2xxx_tpg_attrib_cit */
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci#define DEF_QLA_TPG_ATTRIB(name)					\
8418c2ecf20Sopenharmony_ci									\
8428c2ecf20Sopenharmony_cistatic ssize_t tcm_qla2xxx_tpg_attrib_##name##_show(			\
8438c2ecf20Sopenharmony_ci		struct config_item *item, char *page)			\
8448c2ecf20Sopenharmony_ci{									\
8458c2ecf20Sopenharmony_ci	struct se_portal_group *se_tpg = attrib_to_tpg(item);		\
8468c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,		\
8478c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_tpg, se_tpg);		\
8488c2ecf20Sopenharmony_ci									\
8498c2ecf20Sopenharmony_ci	return sprintf(page, "%d\n", tpg->tpg_attrib.name);	\
8508c2ecf20Sopenharmony_ci}									\
8518c2ecf20Sopenharmony_ci									\
8528c2ecf20Sopenharmony_cistatic ssize_t tcm_qla2xxx_tpg_attrib_##name##_store(			\
8538c2ecf20Sopenharmony_ci		struct config_item *item, const char *page, size_t count) \
8548c2ecf20Sopenharmony_ci{									\
8558c2ecf20Sopenharmony_ci	struct se_portal_group *se_tpg = attrib_to_tpg(item);		\
8568c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,		\
8578c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_tpg, se_tpg);		\
8588c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib;		\
8598c2ecf20Sopenharmony_ci	unsigned long val;						\
8608c2ecf20Sopenharmony_ci	int ret;							\
8618c2ecf20Sopenharmony_ci									\
8628c2ecf20Sopenharmony_ci	ret = kstrtoul(page, 0, &val);					\
8638c2ecf20Sopenharmony_ci	if (ret < 0) {							\
8648c2ecf20Sopenharmony_ci		pr_err("kstrtoul() failed with"				\
8658c2ecf20Sopenharmony_ci				" ret: %d\n", ret);			\
8668c2ecf20Sopenharmony_ci		return -EINVAL;						\
8678c2ecf20Sopenharmony_ci	}								\
8688c2ecf20Sopenharmony_ci									\
8698c2ecf20Sopenharmony_ci	if ((val != 0) && (val != 1)) {					\
8708c2ecf20Sopenharmony_ci		pr_err("Illegal boolean value %lu\n", val);		\
8718c2ecf20Sopenharmony_ci		return -EINVAL;						\
8728c2ecf20Sopenharmony_ci	}								\
8738c2ecf20Sopenharmony_ci									\
8748c2ecf20Sopenharmony_ci	a->name = val;							\
8758c2ecf20Sopenharmony_ci									\
8768c2ecf20Sopenharmony_ci	return count;							\
8778c2ecf20Sopenharmony_ci}									\
8788c2ecf20Sopenharmony_ciCONFIGFS_ATTR(tcm_qla2xxx_tpg_attrib_, name)
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ciDEF_QLA_TPG_ATTRIB(generate_node_acls);
8818c2ecf20Sopenharmony_ciDEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
8828c2ecf20Sopenharmony_ciDEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
8838c2ecf20Sopenharmony_ciDEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
8848c2ecf20Sopenharmony_ciDEF_QLA_TPG_ATTRIB(demo_mode_login_only);
8858c2ecf20Sopenharmony_ci#ifdef CONFIG_TCM_QLA2XXX_DEBUG
8868c2ecf20Sopenharmony_ciDEF_QLA_TPG_ATTRIB(jam_host);
8878c2ecf20Sopenharmony_ci#endif
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_cistatic struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
8908c2ecf20Sopenharmony_ci	&tcm_qla2xxx_tpg_attrib_attr_generate_node_acls,
8918c2ecf20Sopenharmony_ci	&tcm_qla2xxx_tpg_attrib_attr_cache_dynamic_acls,
8928c2ecf20Sopenharmony_ci	&tcm_qla2xxx_tpg_attrib_attr_demo_mode_write_protect,
8938c2ecf20Sopenharmony_ci	&tcm_qla2xxx_tpg_attrib_attr_prod_mode_write_protect,
8948c2ecf20Sopenharmony_ci	&tcm_qla2xxx_tpg_attrib_attr_demo_mode_login_only,
8958c2ecf20Sopenharmony_ci#ifdef CONFIG_TCM_QLA2XXX_DEBUG
8968c2ecf20Sopenharmony_ci	&tcm_qla2xxx_tpg_attrib_attr_jam_host,
8978c2ecf20Sopenharmony_ci#endif
8988c2ecf20Sopenharmony_ci	NULL,
8998c2ecf20Sopenharmony_ci};
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci/* End items for tcm_qla2xxx_tpg_attrib_cit */
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_cistatic ssize_t tcm_qla2xxx_tpg_enable_show(struct config_item *item,
9048c2ecf20Sopenharmony_ci		char *page)
9058c2ecf20Sopenharmony_ci{
9068c2ecf20Sopenharmony_ci	struct se_portal_group *se_tpg = to_tpg(item);
9078c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
9088c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_tpg, se_tpg);
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	return snprintf(page, PAGE_SIZE, "%d\n",
9118c2ecf20Sopenharmony_ci			atomic_read(&tpg->lport_tpg_enabled));
9128c2ecf20Sopenharmony_ci}
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_cistatic ssize_t tcm_qla2xxx_tpg_enable_store(struct config_item *item,
9158c2ecf20Sopenharmony_ci		const char *page, size_t count)
9168c2ecf20Sopenharmony_ci{
9178c2ecf20Sopenharmony_ci	struct se_portal_group *se_tpg = to_tpg(item);
9188c2ecf20Sopenharmony_ci	struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
9198c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
9208c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_lport, lport_wwn);
9218c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha = lport->qla_vha;
9228c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
9238c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_tpg, se_tpg);
9248c2ecf20Sopenharmony_ci	unsigned long op;
9258c2ecf20Sopenharmony_ci	int rc;
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	rc = kstrtoul(page, 0, &op);
9288c2ecf20Sopenharmony_ci	if (rc < 0) {
9298c2ecf20Sopenharmony_ci		pr_err("kstrtoul() returned %d\n", rc);
9308c2ecf20Sopenharmony_ci		return -EINVAL;
9318c2ecf20Sopenharmony_ci	}
9328c2ecf20Sopenharmony_ci	if ((op != 1) && (op != 0)) {
9338c2ecf20Sopenharmony_ci		pr_err("Illegal value for tpg_enable: %lu\n", op);
9348c2ecf20Sopenharmony_ci		return -EINVAL;
9358c2ecf20Sopenharmony_ci	}
9368c2ecf20Sopenharmony_ci	if (op) {
9378c2ecf20Sopenharmony_ci		if (atomic_read(&tpg->lport_tpg_enabled))
9388c2ecf20Sopenharmony_ci			return -EEXIST;
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci		atomic_set(&tpg->lport_tpg_enabled, 1);
9418c2ecf20Sopenharmony_ci		qlt_enable_vha(vha);
9428c2ecf20Sopenharmony_ci	} else {
9438c2ecf20Sopenharmony_ci		if (!atomic_read(&tpg->lport_tpg_enabled))
9448c2ecf20Sopenharmony_ci			return count;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci		atomic_set(&tpg->lport_tpg_enabled, 0);
9478c2ecf20Sopenharmony_ci		qlt_stop_phase1(vha->vha_tgt.qla_tgt);
9488c2ecf20Sopenharmony_ci		qlt_stop_phase2(vha->vha_tgt.qla_tgt);
9498c2ecf20Sopenharmony_ci	}
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	return count;
9528c2ecf20Sopenharmony_ci}
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_cistatic ssize_t tcm_qla2xxx_tpg_dynamic_sessions_show(struct config_item *item,
9558c2ecf20Sopenharmony_ci		char *page)
9568c2ecf20Sopenharmony_ci{
9578c2ecf20Sopenharmony_ci	return target_show_dynamic_sessions(to_tpg(item), page);
9588c2ecf20Sopenharmony_ci}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_cistatic ssize_t tcm_qla2xxx_tpg_fabric_prot_type_store(struct config_item *item,
9618c2ecf20Sopenharmony_ci		const char *page, size_t count)
9628c2ecf20Sopenharmony_ci{
9638c2ecf20Sopenharmony_ci	struct se_portal_group *se_tpg = to_tpg(item);
9648c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
9658c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_tpg, se_tpg);
9668c2ecf20Sopenharmony_ci	unsigned long val;
9678c2ecf20Sopenharmony_ci	int ret = kstrtoul(page, 0, &val);
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	if (ret) {
9708c2ecf20Sopenharmony_ci		pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
9718c2ecf20Sopenharmony_ci		return ret;
9728c2ecf20Sopenharmony_ci	}
9738c2ecf20Sopenharmony_ci	if (val != 0 && val != 1 && val != 3) {
9748c2ecf20Sopenharmony_ci		pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
9758c2ecf20Sopenharmony_ci		return -EINVAL;
9768c2ecf20Sopenharmony_ci	}
9778c2ecf20Sopenharmony_ci	tpg->tpg_attrib.fabric_prot_type = val;
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci	return count;
9808c2ecf20Sopenharmony_ci}
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_cistatic ssize_t tcm_qla2xxx_tpg_fabric_prot_type_show(struct config_item *item,
9838c2ecf20Sopenharmony_ci		char *page)
9848c2ecf20Sopenharmony_ci{
9858c2ecf20Sopenharmony_ci	struct se_portal_group *se_tpg = to_tpg(item);
9868c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
9878c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_tpg, se_tpg);
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	return sprintf(page, "%d\n", tpg->tpg_attrib.fabric_prot_type);
9908c2ecf20Sopenharmony_ci}
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ciCONFIGFS_ATTR(tcm_qla2xxx_tpg_, enable);
9938c2ecf20Sopenharmony_ciCONFIGFS_ATTR_RO(tcm_qla2xxx_tpg_, dynamic_sessions);
9948c2ecf20Sopenharmony_ciCONFIGFS_ATTR(tcm_qla2xxx_tpg_, fabric_prot_type);
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_cistatic struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
9978c2ecf20Sopenharmony_ci	&tcm_qla2xxx_tpg_attr_enable,
9988c2ecf20Sopenharmony_ci	&tcm_qla2xxx_tpg_attr_dynamic_sessions,
9998c2ecf20Sopenharmony_ci	&tcm_qla2xxx_tpg_attr_fabric_prot_type,
10008c2ecf20Sopenharmony_ci	NULL,
10018c2ecf20Sopenharmony_ci};
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_cistatic struct se_portal_group *tcm_qla2xxx_make_tpg(struct se_wwn *wwn,
10048c2ecf20Sopenharmony_ci						    const char *name)
10058c2ecf20Sopenharmony_ci{
10068c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport = container_of(wwn,
10078c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_lport, lport_wwn);
10088c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg;
10098c2ecf20Sopenharmony_ci	unsigned long tpgt;
10108c2ecf20Sopenharmony_ci	int ret;
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	if (strstr(name, "tpgt_") != name)
10138c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
10148c2ecf20Sopenharmony_ci	if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
10158c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ci	if ((tpgt != 1)) {
10188c2ecf20Sopenharmony_ci		pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
10198c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOSYS);
10208c2ecf20Sopenharmony_ci	}
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
10238c2ecf20Sopenharmony_ci	if (!tpg) {
10248c2ecf20Sopenharmony_ci		pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
10258c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
10268c2ecf20Sopenharmony_ci	}
10278c2ecf20Sopenharmony_ci	tpg->lport = lport;
10288c2ecf20Sopenharmony_ci	tpg->lport_tpgt = tpgt;
10298c2ecf20Sopenharmony_ci	/*
10308c2ecf20Sopenharmony_ci	 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
10318c2ecf20Sopenharmony_ci	 * NodeACLs
10328c2ecf20Sopenharmony_ci	 */
10338c2ecf20Sopenharmony_ci	tpg->tpg_attrib.generate_node_acls = 1;
10348c2ecf20Sopenharmony_ci	tpg->tpg_attrib.demo_mode_write_protect = 1;
10358c2ecf20Sopenharmony_ci	tpg->tpg_attrib.cache_dynamic_acls = 1;
10368c2ecf20Sopenharmony_ci	tpg->tpg_attrib.demo_mode_login_only = 1;
10378c2ecf20Sopenharmony_ci	tpg->tpg_attrib.jam_host = 0;
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
10408c2ecf20Sopenharmony_ci	if (ret < 0) {
10418c2ecf20Sopenharmony_ci		kfree(tpg);
10428c2ecf20Sopenharmony_ci		return NULL;
10438c2ecf20Sopenharmony_ci	}
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	lport->tpg_1 = tpg;
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_ci	return &tpg->se_tpg;
10488c2ecf20Sopenharmony_ci}
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
10518c2ecf20Sopenharmony_ci{
10528c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
10538c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_tpg, se_tpg);
10548c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport = tpg->lport;
10558c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha = lport->qla_vha;
10568c2ecf20Sopenharmony_ci	/*
10578c2ecf20Sopenharmony_ci	 * Call into qla2x_target.c LLD logic to shutdown the active
10588c2ecf20Sopenharmony_ci	 * FC Nexuses and disable target mode operation for this qla_hw_data
10598c2ecf20Sopenharmony_ci	 */
10608c2ecf20Sopenharmony_ci	if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stop)
10618c2ecf20Sopenharmony_ci		qlt_stop_phase1(vha->vha_tgt.qla_tgt);
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci	core_tpg_deregister(se_tpg);
10648c2ecf20Sopenharmony_ci	/*
10658c2ecf20Sopenharmony_ci	 * Clear local TPG=1 pointer for non NPIV mode.
10668c2ecf20Sopenharmony_ci	 */
10678c2ecf20Sopenharmony_ci	lport->tpg_1 = NULL;
10688c2ecf20Sopenharmony_ci	kfree(tpg);
10698c2ecf20Sopenharmony_ci}
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_cistatic ssize_t tcm_qla2xxx_npiv_tpg_enable_show(struct config_item *item,
10728c2ecf20Sopenharmony_ci		char *page)
10738c2ecf20Sopenharmony_ci{
10748c2ecf20Sopenharmony_ci	return tcm_qla2xxx_tpg_enable_show(item, page);
10758c2ecf20Sopenharmony_ci}
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_cistatic ssize_t tcm_qla2xxx_npiv_tpg_enable_store(struct config_item *item,
10788c2ecf20Sopenharmony_ci		const char *page, size_t count)
10798c2ecf20Sopenharmony_ci{
10808c2ecf20Sopenharmony_ci	struct se_portal_group *se_tpg = to_tpg(item);
10818c2ecf20Sopenharmony_ci	struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
10828c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
10838c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_lport, lport_wwn);
10848c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha = lport->qla_vha;
10858c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
10868c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_tpg, se_tpg);
10878c2ecf20Sopenharmony_ci	unsigned long op;
10888c2ecf20Sopenharmony_ci	int rc;
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	rc = kstrtoul(page, 0, &op);
10918c2ecf20Sopenharmony_ci	if (rc < 0) {
10928c2ecf20Sopenharmony_ci		pr_err("kstrtoul() returned %d\n", rc);
10938c2ecf20Sopenharmony_ci		return -EINVAL;
10948c2ecf20Sopenharmony_ci	}
10958c2ecf20Sopenharmony_ci	if ((op != 1) && (op != 0)) {
10968c2ecf20Sopenharmony_ci		pr_err("Illegal value for tpg_enable: %lu\n", op);
10978c2ecf20Sopenharmony_ci		return -EINVAL;
10988c2ecf20Sopenharmony_ci	}
10998c2ecf20Sopenharmony_ci	if (op) {
11008c2ecf20Sopenharmony_ci		if (atomic_read(&tpg->lport_tpg_enabled))
11018c2ecf20Sopenharmony_ci			return -EEXIST;
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci		atomic_set(&tpg->lport_tpg_enabled, 1);
11048c2ecf20Sopenharmony_ci		qlt_enable_vha(vha);
11058c2ecf20Sopenharmony_ci	} else {
11068c2ecf20Sopenharmony_ci		if (!atomic_read(&tpg->lport_tpg_enabled))
11078c2ecf20Sopenharmony_ci			return count;
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci		atomic_set(&tpg->lport_tpg_enabled, 0);
11108c2ecf20Sopenharmony_ci		qlt_stop_phase1(vha->vha_tgt.qla_tgt);
11118c2ecf20Sopenharmony_ci		qlt_stop_phase2(vha->vha_tgt.qla_tgt);
11128c2ecf20Sopenharmony_ci	}
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci	return count;
11158c2ecf20Sopenharmony_ci}
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ciCONFIGFS_ATTR(tcm_qla2xxx_npiv_tpg_, enable);
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_cistatic struct configfs_attribute *tcm_qla2xxx_npiv_tpg_attrs[] = {
11208c2ecf20Sopenharmony_ci        &tcm_qla2xxx_npiv_tpg_attr_enable,
11218c2ecf20Sopenharmony_ci        NULL,
11228c2ecf20Sopenharmony_ci};
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_cistatic struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(struct se_wwn *wwn,
11258c2ecf20Sopenharmony_ci							 const char *name)
11268c2ecf20Sopenharmony_ci{
11278c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport = container_of(wwn,
11288c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_lport, lport_wwn);
11298c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg;
11308c2ecf20Sopenharmony_ci	unsigned long tpgt;
11318c2ecf20Sopenharmony_ci	int ret;
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	if (strstr(name, "tpgt_") != name)
11348c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
11358c2ecf20Sopenharmony_ci	if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
11368c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci	tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
11398c2ecf20Sopenharmony_ci	if (!tpg) {
11408c2ecf20Sopenharmony_ci		pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
11418c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
11428c2ecf20Sopenharmony_ci	}
11438c2ecf20Sopenharmony_ci	tpg->lport = lport;
11448c2ecf20Sopenharmony_ci	tpg->lport_tpgt = tpgt;
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	/*
11478c2ecf20Sopenharmony_ci	 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
11488c2ecf20Sopenharmony_ci	 * NodeACLs
11498c2ecf20Sopenharmony_ci	 */
11508c2ecf20Sopenharmony_ci	tpg->tpg_attrib.generate_node_acls = 1;
11518c2ecf20Sopenharmony_ci	tpg->tpg_attrib.demo_mode_write_protect = 1;
11528c2ecf20Sopenharmony_ci	tpg->tpg_attrib.cache_dynamic_acls = 1;
11538c2ecf20Sopenharmony_ci	tpg->tpg_attrib.demo_mode_login_only = 1;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
11568c2ecf20Sopenharmony_ci	if (ret < 0) {
11578c2ecf20Sopenharmony_ci		kfree(tpg);
11588c2ecf20Sopenharmony_ci		return NULL;
11598c2ecf20Sopenharmony_ci	}
11608c2ecf20Sopenharmony_ci	lport->tpg_1 = tpg;
11618c2ecf20Sopenharmony_ci	return &tpg->se_tpg;
11628c2ecf20Sopenharmony_ci}
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci/*
11658c2ecf20Sopenharmony_ci * Expected to be called with struct qla_hw_data->tgt.sess_lock held
11668c2ecf20Sopenharmony_ci */
11678c2ecf20Sopenharmony_cistatic struct fc_port *tcm_qla2xxx_find_sess_by_s_id(scsi_qla_host_t *vha,
11688c2ecf20Sopenharmony_ci						     const be_id_t s_id)
11698c2ecf20Sopenharmony_ci{
11708c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport;
11718c2ecf20Sopenharmony_ci	struct se_node_acl *se_nacl;
11728c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_nacl *nacl;
11738c2ecf20Sopenharmony_ci	u32 key;
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	lport = vha->vha_tgt.target_lport_ptr;
11768c2ecf20Sopenharmony_ci	if (!lport) {
11778c2ecf20Sopenharmony_ci		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
11788c2ecf20Sopenharmony_ci		dump_stack();
11798c2ecf20Sopenharmony_ci		return NULL;
11808c2ecf20Sopenharmony_ci	}
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	key = sid_to_key(s_id);
11838c2ecf20Sopenharmony_ci	pr_debug("find_sess_by_s_id: 0x%06x\n", key);
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
11868c2ecf20Sopenharmony_ci	if (!se_nacl) {
11878c2ecf20Sopenharmony_ci		pr_debug("Unable to locate s_id: 0x%06x\n", key);
11888c2ecf20Sopenharmony_ci		return NULL;
11898c2ecf20Sopenharmony_ci	}
11908c2ecf20Sopenharmony_ci	pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
11918c2ecf20Sopenharmony_ci	    se_nacl, se_nacl->initiatorname);
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci	nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
11948c2ecf20Sopenharmony_ci	if (!nacl->fc_port) {
11958c2ecf20Sopenharmony_ci		pr_err("Unable to locate struct fc_port\n");
11968c2ecf20Sopenharmony_ci		return NULL;
11978c2ecf20Sopenharmony_ci	}
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci	return nacl->fc_port;
12008c2ecf20Sopenharmony_ci}
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci/*
12038c2ecf20Sopenharmony_ci * Expected to be called with struct qla_hw_data->tgt.sess_lock held
12048c2ecf20Sopenharmony_ci */
12058c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_set_sess_by_s_id(
12068c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport,
12078c2ecf20Sopenharmony_ci	struct se_node_acl *new_se_nacl,
12088c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_nacl *nacl,
12098c2ecf20Sopenharmony_ci	struct se_session *se_sess,
12108c2ecf20Sopenharmony_ci	struct fc_port *fc_port,
12118c2ecf20Sopenharmony_ci	be_id_t s_id)
12128c2ecf20Sopenharmony_ci{
12138c2ecf20Sopenharmony_ci	u32 key;
12148c2ecf20Sopenharmony_ci	void *slot;
12158c2ecf20Sopenharmony_ci	int rc;
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci	key = sid_to_key(s_id);
12188c2ecf20Sopenharmony_ci	pr_debug("set_sess_by_s_id: %06x\n", key);
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	slot = btree_lookup32(&lport->lport_fcport_map, key);
12218c2ecf20Sopenharmony_ci	if (!slot) {
12228c2ecf20Sopenharmony_ci		if (new_se_nacl) {
12238c2ecf20Sopenharmony_ci			pr_debug("Setting up new fc_port entry to new_se_nacl\n");
12248c2ecf20Sopenharmony_ci			nacl->nport_id = key;
12258c2ecf20Sopenharmony_ci			rc = btree_insert32(&lport->lport_fcport_map, key,
12268c2ecf20Sopenharmony_ci					new_se_nacl, GFP_ATOMIC);
12278c2ecf20Sopenharmony_ci			if (rc)
12288c2ecf20Sopenharmony_ci				printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
12298c2ecf20Sopenharmony_ci				    (int)key);
12308c2ecf20Sopenharmony_ci		} else {
12318c2ecf20Sopenharmony_ci			pr_debug("Wiping nonexisting fc_port entry\n");
12328c2ecf20Sopenharmony_ci		}
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci		fc_port->se_sess = se_sess;
12358c2ecf20Sopenharmony_ci		nacl->fc_port = fc_port;
12368c2ecf20Sopenharmony_ci		return;
12378c2ecf20Sopenharmony_ci	}
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	if (nacl->fc_port) {
12408c2ecf20Sopenharmony_ci		if (new_se_nacl == NULL) {
12418c2ecf20Sopenharmony_ci			pr_debug("Clearing existing nacl->fc_port and fc_port entry\n");
12428c2ecf20Sopenharmony_ci			btree_remove32(&lport->lport_fcport_map, key);
12438c2ecf20Sopenharmony_ci			nacl->fc_port = NULL;
12448c2ecf20Sopenharmony_ci			return;
12458c2ecf20Sopenharmony_ci		}
12468c2ecf20Sopenharmony_ci		pr_debug("Replacing existing nacl->fc_port and fc_port entry\n");
12478c2ecf20Sopenharmony_ci		btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
12488c2ecf20Sopenharmony_ci		fc_port->se_sess = se_sess;
12498c2ecf20Sopenharmony_ci		nacl->fc_port = fc_port;
12508c2ecf20Sopenharmony_ci		return;
12518c2ecf20Sopenharmony_ci	}
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	if (new_se_nacl == NULL) {
12548c2ecf20Sopenharmony_ci		pr_debug("Clearing existing fc_port entry\n");
12558c2ecf20Sopenharmony_ci		btree_remove32(&lport->lport_fcport_map, key);
12568c2ecf20Sopenharmony_ci		return;
12578c2ecf20Sopenharmony_ci	}
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	pr_debug("Replacing existing fc_port entry w/o active nacl->fc_port\n");
12608c2ecf20Sopenharmony_ci	btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
12618c2ecf20Sopenharmony_ci	fc_port->se_sess = se_sess;
12628c2ecf20Sopenharmony_ci	nacl->fc_port = fc_port;
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci	pr_debug("Setup nacl->fc_port %p by s_id for se_nacl: %p, initiatorname: %s\n",
12658c2ecf20Sopenharmony_ci	    nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
12668c2ecf20Sopenharmony_ci}
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci/*
12698c2ecf20Sopenharmony_ci * Expected to be called with struct qla_hw_data->tgt.sess_lock held
12708c2ecf20Sopenharmony_ci */
12718c2ecf20Sopenharmony_cistatic struct fc_port *tcm_qla2xxx_find_sess_by_loop_id(
12728c2ecf20Sopenharmony_ci	scsi_qla_host_t *vha,
12738c2ecf20Sopenharmony_ci	const uint16_t loop_id)
12748c2ecf20Sopenharmony_ci{
12758c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport;
12768c2ecf20Sopenharmony_ci	struct se_node_acl *se_nacl;
12778c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_nacl *nacl;
12788c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_fc_loopid *fc_loopid;
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	lport = vha->vha_tgt.target_lport_ptr;
12818c2ecf20Sopenharmony_ci	if (!lport) {
12828c2ecf20Sopenharmony_ci		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
12838c2ecf20Sopenharmony_ci		dump_stack();
12848c2ecf20Sopenharmony_ci		return NULL;
12858c2ecf20Sopenharmony_ci	}
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_ci	pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci	fc_loopid = lport->lport_loopid_map + loop_id;
12908c2ecf20Sopenharmony_ci	se_nacl = fc_loopid->se_nacl;
12918c2ecf20Sopenharmony_ci	if (!se_nacl) {
12928c2ecf20Sopenharmony_ci		pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
12938c2ecf20Sopenharmony_ci		    loop_id);
12948c2ecf20Sopenharmony_ci		return NULL;
12958c2ecf20Sopenharmony_ci	}
12968c2ecf20Sopenharmony_ci
12978c2ecf20Sopenharmony_ci	nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci	if (!nacl->fc_port) {
13008c2ecf20Sopenharmony_ci		pr_err("Unable to locate struct fc_port\n");
13018c2ecf20Sopenharmony_ci		return NULL;
13028c2ecf20Sopenharmony_ci	}
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_ci	return nacl->fc_port;
13058c2ecf20Sopenharmony_ci}
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci/*
13088c2ecf20Sopenharmony_ci * Expected to be called with struct qla_hw_data->tgt.sess_lock held
13098c2ecf20Sopenharmony_ci */
13108c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_set_sess_by_loop_id(
13118c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport,
13128c2ecf20Sopenharmony_ci	struct se_node_acl *new_se_nacl,
13138c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_nacl *nacl,
13148c2ecf20Sopenharmony_ci	struct se_session *se_sess,
13158c2ecf20Sopenharmony_ci	struct fc_port *fc_port,
13168c2ecf20Sopenharmony_ci	uint16_t loop_id)
13178c2ecf20Sopenharmony_ci{
13188c2ecf20Sopenharmony_ci	struct se_node_acl *saved_nacl;
13198c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_fc_loopid *fc_loopid;
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci	pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
13228c2ecf20Sopenharmony_ci
13238c2ecf20Sopenharmony_ci	fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
13248c2ecf20Sopenharmony_ci			lport->lport_loopid_map)[loop_id];
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	saved_nacl = fc_loopid->se_nacl;
13278c2ecf20Sopenharmony_ci	if (!saved_nacl) {
13288c2ecf20Sopenharmony_ci		pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
13298c2ecf20Sopenharmony_ci		fc_loopid->se_nacl = new_se_nacl;
13308c2ecf20Sopenharmony_ci		if (fc_port->se_sess != se_sess)
13318c2ecf20Sopenharmony_ci			fc_port->se_sess = se_sess;
13328c2ecf20Sopenharmony_ci		if (nacl->fc_port != fc_port)
13338c2ecf20Sopenharmony_ci			nacl->fc_port = fc_port;
13348c2ecf20Sopenharmony_ci		return;
13358c2ecf20Sopenharmony_ci	}
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci	if (nacl->fc_port) {
13388c2ecf20Sopenharmony_ci		if (new_se_nacl == NULL) {
13398c2ecf20Sopenharmony_ci			pr_debug("Clearing nacl->fc_port and fc_loopid->se_nacl\n");
13408c2ecf20Sopenharmony_ci			fc_loopid->se_nacl = NULL;
13418c2ecf20Sopenharmony_ci			nacl->fc_port = NULL;
13428c2ecf20Sopenharmony_ci			return;
13438c2ecf20Sopenharmony_ci		}
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ci		pr_debug("Replacing existing nacl->fc_port and fc_loopid->se_nacl\n");
13468c2ecf20Sopenharmony_ci		fc_loopid->se_nacl = new_se_nacl;
13478c2ecf20Sopenharmony_ci		if (fc_port->se_sess != se_sess)
13488c2ecf20Sopenharmony_ci			fc_port->se_sess = se_sess;
13498c2ecf20Sopenharmony_ci		if (nacl->fc_port != fc_port)
13508c2ecf20Sopenharmony_ci			nacl->fc_port = fc_port;
13518c2ecf20Sopenharmony_ci		return;
13528c2ecf20Sopenharmony_ci	}
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_ci	if (new_se_nacl == NULL) {
13558c2ecf20Sopenharmony_ci		pr_debug("Clearing fc_loopid->se_nacl\n");
13568c2ecf20Sopenharmony_ci		fc_loopid->se_nacl = NULL;
13578c2ecf20Sopenharmony_ci		return;
13588c2ecf20Sopenharmony_ci	}
13598c2ecf20Sopenharmony_ci
13608c2ecf20Sopenharmony_ci	pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->fc_port\n");
13618c2ecf20Sopenharmony_ci	fc_loopid->se_nacl = new_se_nacl;
13628c2ecf20Sopenharmony_ci	if (fc_port->se_sess != se_sess)
13638c2ecf20Sopenharmony_ci		fc_port->se_sess = se_sess;
13648c2ecf20Sopenharmony_ci	if (nacl->fc_port != fc_port)
13658c2ecf20Sopenharmony_ci		nacl->fc_port = fc_port;
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_ci	pr_debug("Setup nacl->fc_port %p by loop_id for se_nacl: %p, initiatorname: %s\n",
13688c2ecf20Sopenharmony_ci	    nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
13698c2ecf20Sopenharmony_ci}
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci/*
13728c2ecf20Sopenharmony_ci * Should always be called with qla_hw_data->tgt.sess_lock held.
13738c2ecf20Sopenharmony_ci */
13748c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
13758c2ecf20Sopenharmony_ci		struct tcm_qla2xxx_nacl *nacl, struct fc_port *sess)
13768c2ecf20Sopenharmony_ci{
13778c2ecf20Sopenharmony_ci	struct se_session *se_sess = sess->se_sess;
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_ci	tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
13808c2ecf20Sopenharmony_ci				     sess, port_id_to_be_id(sess->d_id));
13818c2ecf20Sopenharmony_ci	tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
13828c2ecf20Sopenharmony_ci				sess, sess->loop_id);
13838c2ecf20Sopenharmony_ci}
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_free_session(struct fc_port *sess)
13868c2ecf20Sopenharmony_ci{
13878c2ecf20Sopenharmony_ci	struct qla_tgt *tgt = sess->tgt;
13888c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = tgt->ha;
13898c2ecf20Sopenharmony_ci	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
13908c2ecf20Sopenharmony_ci	struct se_session *se_sess;
13918c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport;
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci	BUG_ON(in_interrupt());
13948c2ecf20Sopenharmony_ci
13958c2ecf20Sopenharmony_ci	se_sess = sess->se_sess;
13968c2ecf20Sopenharmony_ci	if (!se_sess) {
13978c2ecf20Sopenharmony_ci		pr_err("struct fc_port->se_sess is NULL\n");
13988c2ecf20Sopenharmony_ci		dump_stack();
13998c2ecf20Sopenharmony_ci		return;
14008c2ecf20Sopenharmony_ci	}
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_ci	lport = vha->vha_tgt.target_lport_ptr;
14038c2ecf20Sopenharmony_ci	if (!lport) {
14048c2ecf20Sopenharmony_ci		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
14058c2ecf20Sopenharmony_ci		dump_stack();
14068c2ecf20Sopenharmony_ci		return;
14078c2ecf20Sopenharmony_ci	}
14088c2ecf20Sopenharmony_ci	target_wait_for_sess_cmds(se_sess);
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci	target_remove_session(se_sess);
14118c2ecf20Sopenharmony_ci}
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_session_cb(struct se_portal_group *se_tpg,
14148c2ecf20Sopenharmony_ci				  struct se_session *se_sess, void *p)
14158c2ecf20Sopenharmony_ci{
14168c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
14178c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_tpg, se_tpg);
14188c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport = tpg->lport;
14198c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = lport->qla_vha->hw;
14208c2ecf20Sopenharmony_ci	struct se_node_acl *se_nacl = se_sess->se_node_acl;
14218c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
14228c2ecf20Sopenharmony_ci				struct tcm_qla2xxx_nacl, se_node_acl);
14238c2ecf20Sopenharmony_ci	struct fc_port *qlat_sess = p;
14248c2ecf20Sopenharmony_ci	uint16_t loop_id = qlat_sess->loop_id;
14258c2ecf20Sopenharmony_ci	unsigned long flags;
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci	/*
14288c2ecf20Sopenharmony_ci	 * And now setup se_nacl and session pointers into HW lport internal
14298c2ecf20Sopenharmony_ci	 * mappings for fabric S_ID and LOOP_ID.
14308c2ecf20Sopenharmony_ci	 */
14318c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
14328c2ecf20Sopenharmony_ci	tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess, qlat_sess,
14338c2ecf20Sopenharmony_ci				     port_id_to_be_id(qlat_sess->d_id));
14348c2ecf20Sopenharmony_ci	tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl,
14358c2ecf20Sopenharmony_ci					se_sess, qlat_sess, loop_id);
14368c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_ci	return 0;
14398c2ecf20Sopenharmony_ci}
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci/*
14428c2ecf20Sopenharmony_ci * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
14438c2ecf20Sopenharmony_ci * to locate struct se_node_acl
14448c2ecf20Sopenharmony_ci */
14458c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_check_initiator_node_acl(
14468c2ecf20Sopenharmony_ci	scsi_qla_host_t *vha,
14478c2ecf20Sopenharmony_ci	unsigned char *fc_wwpn,
14488c2ecf20Sopenharmony_ci	struct fc_port *qlat_sess)
14498c2ecf20Sopenharmony_ci{
14508c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
14518c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport;
14528c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_tpg *tpg;
14538c2ecf20Sopenharmony_ci	struct se_session *se_sess;
14548c2ecf20Sopenharmony_ci	unsigned char port_name[36];
14558c2ecf20Sopenharmony_ci	int num_tags = (ha->cur_fw_xcb_count) ? ha->cur_fw_xcb_count :
14568c2ecf20Sopenharmony_ci		       TCM_QLA2XXX_DEFAULT_TAGS;
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_ci	lport = vha->vha_tgt.target_lport_ptr;
14598c2ecf20Sopenharmony_ci	if (!lport) {
14608c2ecf20Sopenharmony_ci		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
14618c2ecf20Sopenharmony_ci		dump_stack();
14628c2ecf20Sopenharmony_ci		return -EINVAL;
14638c2ecf20Sopenharmony_ci	}
14648c2ecf20Sopenharmony_ci	/*
14658c2ecf20Sopenharmony_ci	 * Locate the TPG=1 reference..
14668c2ecf20Sopenharmony_ci	 */
14678c2ecf20Sopenharmony_ci	tpg = lport->tpg_1;
14688c2ecf20Sopenharmony_ci	if (!tpg) {
14698c2ecf20Sopenharmony_ci		pr_err("Unable to locate struct tcm_qla2xxx_lport->tpg_1\n");
14708c2ecf20Sopenharmony_ci		return -EINVAL;
14718c2ecf20Sopenharmony_ci	}
14728c2ecf20Sopenharmony_ci	/*
14738c2ecf20Sopenharmony_ci	 * Format the FCP Initiator port_name into colon seperated values to
14748c2ecf20Sopenharmony_ci	 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
14758c2ecf20Sopenharmony_ci	 */
14768c2ecf20Sopenharmony_ci	memset(&port_name, 0, 36);
14778c2ecf20Sopenharmony_ci	snprintf(port_name, sizeof(port_name), "%8phC", fc_wwpn);
14788c2ecf20Sopenharmony_ci	/*
14798c2ecf20Sopenharmony_ci	 * Locate our struct se_node_acl either from an explict NodeACL created
14808c2ecf20Sopenharmony_ci	 * via ConfigFS, or via running in TPG demo mode.
14818c2ecf20Sopenharmony_ci	 */
14828c2ecf20Sopenharmony_ci	se_sess = target_setup_session(&tpg->se_tpg, num_tags,
14838c2ecf20Sopenharmony_ci				       sizeof(struct qla_tgt_cmd),
14848c2ecf20Sopenharmony_ci				       TARGET_PROT_ALL, port_name,
14858c2ecf20Sopenharmony_ci				       qlat_sess, tcm_qla2xxx_session_cb);
14868c2ecf20Sopenharmony_ci	if (IS_ERR(se_sess))
14878c2ecf20Sopenharmony_ci		return PTR_ERR(se_sess);
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_ci	return 0;
14908c2ecf20Sopenharmony_ci}
14918c2ecf20Sopenharmony_ci
14928c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_update_sess(struct fc_port *sess, port_id_t s_id,
14938c2ecf20Sopenharmony_ci				    uint16_t loop_id, bool conf_compl_supported)
14948c2ecf20Sopenharmony_ci{
14958c2ecf20Sopenharmony_ci	struct qla_tgt *tgt = sess->tgt;
14968c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = tgt->ha;
14978c2ecf20Sopenharmony_ci	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
14988c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport = vha->vha_tgt.target_lport_ptr;
14998c2ecf20Sopenharmony_ci	struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
15008c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
15018c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_nacl, se_node_acl);
15028c2ecf20Sopenharmony_ci	u32 key;
15038c2ecf20Sopenharmony_ci
15048c2ecf20Sopenharmony_ci
15058c2ecf20Sopenharmony_ci	if (sess->loop_id != loop_id || sess->d_id.b24 != s_id.b24)
15068c2ecf20Sopenharmony_ci		pr_info("Updating session %p from port %8phC loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
15078c2ecf20Sopenharmony_ci		    sess, sess->port_name,
15088c2ecf20Sopenharmony_ci		    sess->loop_id, loop_id, sess->d_id.b.domain,
15098c2ecf20Sopenharmony_ci		    sess->d_id.b.area, sess->d_id.b.al_pa, s_id.b.domain,
15108c2ecf20Sopenharmony_ci		    s_id.b.area, s_id.b.al_pa);
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	if (sess->loop_id != loop_id) {
15138c2ecf20Sopenharmony_ci		/*
15148c2ecf20Sopenharmony_ci		 * Because we can shuffle loop IDs around and we
15158c2ecf20Sopenharmony_ci		 * update different sessions non-atomically, we might
15168c2ecf20Sopenharmony_ci		 * have overwritten this session's old loop ID
15178c2ecf20Sopenharmony_ci		 * already, and we might end up overwriting some other
15188c2ecf20Sopenharmony_ci		 * session that will be updated later.  So we have to
15198c2ecf20Sopenharmony_ci		 * be extra careful and we can't warn about those things...
15208c2ecf20Sopenharmony_ci		 */
15218c2ecf20Sopenharmony_ci		if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
15228c2ecf20Sopenharmony_ci			lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
15238c2ecf20Sopenharmony_ci
15248c2ecf20Sopenharmony_ci		lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
15258c2ecf20Sopenharmony_ci
15268c2ecf20Sopenharmony_ci		sess->loop_id = loop_id;
15278c2ecf20Sopenharmony_ci	}
15288c2ecf20Sopenharmony_ci
15298c2ecf20Sopenharmony_ci	if (sess->d_id.b24 != s_id.b24) {
15308c2ecf20Sopenharmony_ci		key = (((u32) sess->d_id.b.domain << 16) |
15318c2ecf20Sopenharmony_ci		       ((u32) sess->d_id.b.area   <<  8) |
15328c2ecf20Sopenharmony_ci		       ((u32) sess->d_id.b.al_pa));
15338c2ecf20Sopenharmony_ci
15348c2ecf20Sopenharmony_ci		if (btree_lookup32(&lport->lport_fcport_map, key))
15358c2ecf20Sopenharmony_ci			WARN(btree_remove32(&lport->lport_fcport_map, key) !=
15368c2ecf20Sopenharmony_ci			    se_nacl, "Found wrong se_nacl when updating s_id %x:%x:%x\n",
15378c2ecf20Sopenharmony_ci			    sess->d_id.b.domain, sess->d_id.b.area,
15388c2ecf20Sopenharmony_ci			    sess->d_id.b.al_pa);
15398c2ecf20Sopenharmony_ci		else
15408c2ecf20Sopenharmony_ci			WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
15418c2ecf20Sopenharmony_ci			     sess->d_id.b.domain, sess->d_id.b.area,
15428c2ecf20Sopenharmony_ci			     sess->d_id.b.al_pa);
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_ci		key = (((u32) s_id.b.domain << 16) |
15458c2ecf20Sopenharmony_ci		       ((u32) s_id.b.area   <<  8) |
15468c2ecf20Sopenharmony_ci		       ((u32) s_id.b.al_pa));
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci		if (btree_lookup32(&lport->lport_fcport_map, key)) {
15498c2ecf20Sopenharmony_ci			WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
15508c2ecf20Sopenharmony_ci			     s_id.b.domain, s_id.b.area, s_id.b.al_pa);
15518c2ecf20Sopenharmony_ci			btree_update32(&lport->lport_fcport_map, key, se_nacl);
15528c2ecf20Sopenharmony_ci		} else {
15538c2ecf20Sopenharmony_ci			btree_insert32(&lport->lport_fcport_map, key, se_nacl,
15548c2ecf20Sopenharmony_ci			    GFP_ATOMIC);
15558c2ecf20Sopenharmony_ci		}
15568c2ecf20Sopenharmony_ci
15578c2ecf20Sopenharmony_ci		sess->d_id = s_id;
15588c2ecf20Sopenharmony_ci		nacl->nport_id = key;
15598c2ecf20Sopenharmony_ci	}
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ci	sess->conf_compl_supported = conf_compl_supported;
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_ci}
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ci/*
15668c2ecf20Sopenharmony_ci * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
15678c2ecf20Sopenharmony_ci */
15688c2ecf20Sopenharmony_cistatic struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
15698c2ecf20Sopenharmony_ci	.find_cmd_by_tag	= tcm_qla2xxx_find_cmd_by_tag,
15708c2ecf20Sopenharmony_ci	.handle_cmd		= tcm_qla2xxx_handle_cmd,
15718c2ecf20Sopenharmony_ci	.handle_data		= tcm_qla2xxx_handle_data,
15728c2ecf20Sopenharmony_ci	.handle_tmr		= tcm_qla2xxx_handle_tmr,
15738c2ecf20Sopenharmony_ci	.get_cmd		= tcm_qla2xxx_get_cmd,
15748c2ecf20Sopenharmony_ci	.rel_cmd		= tcm_qla2xxx_rel_cmd,
15758c2ecf20Sopenharmony_ci	.free_cmd		= tcm_qla2xxx_free_cmd,
15768c2ecf20Sopenharmony_ci	.free_mcmd		= tcm_qla2xxx_free_mcmd,
15778c2ecf20Sopenharmony_ci	.free_session		= tcm_qla2xxx_free_session,
15788c2ecf20Sopenharmony_ci	.update_sess		= tcm_qla2xxx_update_sess,
15798c2ecf20Sopenharmony_ci	.check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
15808c2ecf20Sopenharmony_ci	.find_sess_by_s_id	= tcm_qla2xxx_find_sess_by_s_id,
15818c2ecf20Sopenharmony_ci	.find_sess_by_loop_id	= tcm_qla2xxx_find_sess_by_loop_id,
15828c2ecf20Sopenharmony_ci	.clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
15838c2ecf20Sopenharmony_ci	.put_sess		= tcm_qla2xxx_put_sess,
15848c2ecf20Sopenharmony_ci	.shutdown_sess		= tcm_qla2xxx_shutdown_sess,
15858c2ecf20Sopenharmony_ci	.get_dif_tags		= tcm_qla2xxx_dif_tags,
15868c2ecf20Sopenharmony_ci	.chk_dif_tags		= tcm_qla2xxx_chk_dif_tags,
15878c2ecf20Sopenharmony_ci};
15888c2ecf20Sopenharmony_ci
15898c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
15908c2ecf20Sopenharmony_ci{
15918c2ecf20Sopenharmony_ci	int rc;
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ci	rc = btree_init32(&lport->lport_fcport_map);
15948c2ecf20Sopenharmony_ci	if (rc) {
15958c2ecf20Sopenharmony_ci		pr_err("Unable to initialize lport->lport_fcport_map btree\n");
15968c2ecf20Sopenharmony_ci		return rc;
15978c2ecf20Sopenharmony_ci	}
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci	lport->lport_loopid_map =
16008c2ecf20Sopenharmony_ci		vzalloc(array_size(65536,
16018c2ecf20Sopenharmony_ci				   sizeof(struct tcm_qla2xxx_fc_loopid)));
16028c2ecf20Sopenharmony_ci	if (!lport->lport_loopid_map) {
16038c2ecf20Sopenharmony_ci		pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
16048c2ecf20Sopenharmony_ci		    sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
16058c2ecf20Sopenharmony_ci		btree_destroy32(&lport->lport_fcport_map);
16068c2ecf20Sopenharmony_ci		return -ENOMEM;
16078c2ecf20Sopenharmony_ci	}
16088c2ecf20Sopenharmony_ci	pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
16098c2ecf20Sopenharmony_ci	       sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
16108c2ecf20Sopenharmony_ci	return 0;
16118c2ecf20Sopenharmony_ci}
16128c2ecf20Sopenharmony_ci
16138c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha,
16148c2ecf20Sopenharmony_ci					 void *target_lport_ptr,
16158c2ecf20Sopenharmony_ci					 u64 npiv_wwpn, u64 npiv_wwnn)
16168c2ecf20Sopenharmony_ci{
16178c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = vha->hw;
16188c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport =
16198c2ecf20Sopenharmony_ci			(struct tcm_qla2xxx_lport *)target_lport_ptr;
16208c2ecf20Sopenharmony_ci	/*
16218c2ecf20Sopenharmony_ci	 * Setup tgt_ops, local pointer to vha and target_lport_ptr
16228c2ecf20Sopenharmony_ci	 */
16238c2ecf20Sopenharmony_ci	ha->tgt.tgt_ops = &tcm_qla2xxx_template;
16248c2ecf20Sopenharmony_ci	vha->vha_tgt.target_lport_ptr = target_lport_ptr;
16258c2ecf20Sopenharmony_ci	lport->qla_vha = vha;
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci	return 0;
16288c2ecf20Sopenharmony_ci}
16298c2ecf20Sopenharmony_ci
16308c2ecf20Sopenharmony_cistatic struct se_wwn *tcm_qla2xxx_make_lport(
16318c2ecf20Sopenharmony_ci	struct target_fabric_configfs *tf,
16328c2ecf20Sopenharmony_ci	struct config_group *group,
16338c2ecf20Sopenharmony_ci	const char *name)
16348c2ecf20Sopenharmony_ci{
16358c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport;
16368c2ecf20Sopenharmony_ci	u64 wwpn;
16378c2ecf20Sopenharmony_ci	int ret = -ENODEV;
16388c2ecf20Sopenharmony_ci
16398c2ecf20Sopenharmony_ci	if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
16408c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_ci	lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
16438c2ecf20Sopenharmony_ci	if (!lport) {
16448c2ecf20Sopenharmony_ci		pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
16458c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
16468c2ecf20Sopenharmony_ci	}
16478c2ecf20Sopenharmony_ci	lport->lport_wwpn = wwpn;
16488c2ecf20Sopenharmony_ci	tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
16498c2ecf20Sopenharmony_ci				wwpn);
16508c2ecf20Sopenharmony_ci	sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
16518c2ecf20Sopenharmony_ci
16528c2ecf20Sopenharmony_ci	ret = tcm_qla2xxx_init_lport(lport);
16538c2ecf20Sopenharmony_ci	if (ret != 0)
16548c2ecf20Sopenharmony_ci		goto out;
16558c2ecf20Sopenharmony_ci
16568c2ecf20Sopenharmony_ci	ret = qlt_lport_register(lport, wwpn, 0, 0,
16578c2ecf20Sopenharmony_ci				 tcm_qla2xxx_lport_register_cb);
16588c2ecf20Sopenharmony_ci	if (ret != 0)
16598c2ecf20Sopenharmony_ci		goto out_lport;
16608c2ecf20Sopenharmony_ci
16618c2ecf20Sopenharmony_ci	return &lport->lport_wwn;
16628c2ecf20Sopenharmony_ciout_lport:
16638c2ecf20Sopenharmony_ci	vfree(lport->lport_loopid_map);
16648c2ecf20Sopenharmony_ci	btree_destroy32(&lport->lport_fcport_map);
16658c2ecf20Sopenharmony_ciout:
16668c2ecf20Sopenharmony_ci	kfree(lport);
16678c2ecf20Sopenharmony_ci	return ERR_PTR(ret);
16688c2ecf20Sopenharmony_ci}
16698c2ecf20Sopenharmony_ci
16708c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
16718c2ecf20Sopenharmony_ci{
16728c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport = container_of(wwn,
16738c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_lport, lport_wwn);
16748c2ecf20Sopenharmony_ci	struct scsi_qla_host *vha = lport->qla_vha;
16758c2ecf20Sopenharmony_ci	struct se_node_acl *node;
16768c2ecf20Sopenharmony_ci	u32 key = 0;
16778c2ecf20Sopenharmony_ci
16788c2ecf20Sopenharmony_ci	/*
16798c2ecf20Sopenharmony_ci	 * Call into qla2x_target.c LLD logic to complete the
16808c2ecf20Sopenharmony_ci	 * shutdown of struct qla_tgt after the call to
16818c2ecf20Sopenharmony_ci	 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
16828c2ecf20Sopenharmony_ci	 */
16838c2ecf20Sopenharmony_ci	if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stopped)
16848c2ecf20Sopenharmony_ci		qlt_stop_phase2(vha->vha_tgt.qla_tgt);
16858c2ecf20Sopenharmony_ci
16868c2ecf20Sopenharmony_ci	qlt_lport_deregister(vha);
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_ci	vfree(lport->lport_loopid_map);
16898c2ecf20Sopenharmony_ci	btree_for_each_safe32(&lport->lport_fcport_map, key, node)
16908c2ecf20Sopenharmony_ci		btree_remove32(&lport->lport_fcport_map, key);
16918c2ecf20Sopenharmony_ci	btree_destroy32(&lport->lport_fcport_map);
16928c2ecf20Sopenharmony_ci	kfree(lport);
16938c2ecf20Sopenharmony_ci}
16948c2ecf20Sopenharmony_ci
16958c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_lport_register_npiv_cb(struct scsi_qla_host *base_vha,
16968c2ecf20Sopenharmony_ci					      void *target_lport_ptr,
16978c2ecf20Sopenharmony_ci					      u64 npiv_wwpn, u64 npiv_wwnn)
16988c2ecf20Sopenharmony_ci{
16998c2ecf20Sopenharmony_ci	struct fc_vport *vport;
17008c2ecf20Sopenharmony_ci	struct Scsi_Host *sh = base_vha->host;
17018c2ecf20Sopenharmony_ci	struct scsi_qla_host *npiv_vha;
17028c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport =
17038c2ecf20Sopenharmony_ci			(struct tcm_qla2xxx_lport *)target_lport_ptr;
17048c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *base_lport =
17058c2ecf20Sopenharmony_ci			(struct tcm_qla2xxx_lport *)base_vha->vha_tgt.target_lport_ptr;
17068c2ecf20Sopenharmony_ci	struct fc_vport_identifiers vport_id;
17078c2ecf20Sopenharmony_ci
17088c2ecf20Sopenharmony_ci	if (qla_ini_mode_enabled(base_vha)) {
17098c2ecf20Sopenharmony_ci		pr_err("qla2xxx base_vha not enabled for target mode\n");
17108c2ecf20Sopenharmony_ci		return -EPERM;
17118c2ecf20Sopenharmony_ci	}
17128c2ecf20Sopenharmony_ci
17138c2ecf20Sopenharmony_ci	if (!base_lport || !base_lport->tpg_1 ||
17148c2ecf20Sopenharmony_ci	    !atomic_read(&base_lport->tpg_1->lport_tpg_enabled)) {
17158c2ecf20Sopenharmony_ci		pr_err("qla2xxx base_lport or tpg_1 not available\n");
17168c2ecf20Sopenharmony_ci		return -EPERM;
17178c2ecf20Sopenharmony_ci	}
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_ci	memset(&vport_id, 0, sizeof(vport_id));
17208c2ecf20Sopenharmony_ci	vport_id.port_name = npiv_wwpn;
17218c2ecf20Sopenharmony_ci	vport_id.node_name = npiv_wwnn;
17228c2ecf20Sopenharmony_ci	vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
17238c2ecf20Sopenharmony_ci	vport_id.vport_type = FC_PORTTYPE_NPIV;
17248c2ecf20Sopenharmony_ci	vport_id.disable = false;
17258c2ecf20Sopenharmony_ci
17268c2ecf20Sopenharmony_ci	vport = fc_vport_create(sh, 0, &vport_id);
17278c2ecf20Sopenharmony_ci	if (!vport) {
17288c2ecf20Sopenharmony_ci		pr_err("fc_vport_create failed for qla2xxx_npiv\n");
17298c2ecf20Sopenharmony_ci		return -ENODEV;
17308c2ecf20Sopenharmony_ci	}
17318c2ecf20Sopenharmony_ci	/*
17328c2ecf20Sopenharmony_ci	 * Setup local pointer to NPIV vhba + target_lport_ptr
17338c2ecf20Sopenharmony_ci	 */
17348c2ecf20Sopenharmony_ci	npiv_vha = (struct scsi_qla_host *)vport->dd_data;
17358c2ecf20Sopenharmony_ci	npiv_vha->vha_tgt.target_lport_ptr = target_lport_ptr;
17368c2ecf20Sopenharmony_ci	lport->qla_vha = npiv_vha;
17378c2ecf20Sopenharmony_ci	scsi_host_get(npiv_vha->host);
17388c2ecf20Sopenharmony_ci	return 0;
17398c2ecf20Sopenharmony_ci}
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_ci
17428c2ecf20Sopenharmony_cistatic struct se_wwn *tcm_qla2xxx_npiv_make_lport(
17438c2ecf20Sopenharmony_ci	struct target_fabric_configfs *tf,
17448c2ecf20Sopenharmony_ci	struct config_group *group,
17458c2ecf20Sopenharmony_ci	const char *name)
17468c2ecf20Sopenharmony_ci{
17478c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport;
17488c2ecf20Sopenharmony_ci	u64 phys_wwpn, npiv_wwpn, npiv_wwnn;
17498c2ecf20Sopenharmony_ci	char *p, tmp[128];
17508c2ecf20Sopenharmony_ci	int ret;
17518c2ecf20Sopenharmony_ci
17528c2ecf20Sopenharmony_ci	snprintf(tmp, 128, "%s", name);
17538c2ecf20Sopenharmony_ci
17548c2ecf20Sopenharmony_ci	p = strchr(tmp, '@');
17558c2ecf20Sopenharmony_ci	if (!p) {
17568c2ecf20Sopenharmony_ci		pr_err("Unable to locate NPIV '@' separator\n");
17578c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
17588c2ecf20Sopenharmony_ci	}
17598c2ecf20Sopenharmony_ci	*p++ = '\0';
17608c2ecf20Sopenharmony_ci
17618c2ecf20Sopenharmony_ci	if (tcm_qla2xxx_parse_wwn(tmp, &phys_wwpn, 1) < 0)
17628c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
17638c2ecf20Sopenharmony_ci
17648c2ecf20Sopenharmony_ci	if (tcm_qla2xxx_npiv_parse_wwn(p, strlen(p)+1,
17658c2ecf20Sopenharmony_ci				       &npiv_wwpn, &npiv_wwnn) < 0)
17668c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
17678c2ecf20Sopenharmony_ci
17688c2ecf20Sopenharmony_ci	lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
17698c2ecf20Sopenharmony_ci	if (!lport) {
17708c2ecf20Sopenharmony_ci		pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
17718c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
17728c2ecf20Sopenharmony_ci	}
17738c2ecf20Sopenharmony_ci	lport->lport_npiv_wwpn = npiv_wwpn;
17748c2ecf20Sopenharmony_ci	lport->lport_npiv_wwnn = npiv_wwnn;
17758c2ecf20Sopenharmony_ci	sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
17768c2ecf20Sopenharmony_ci
17778c2ecf20Sopenharmony_ci	ret = tcm_qla2xxx_init_lport(lport);
17788c2ecf20Sopenharmony_ci	if (ret != 0)
17798c2ecf20Sopenharmony_ci		goto out;
17808c2ecf20Sopenharmony_ci
17818c2ecf20Sopenharmony_ci	ret = qlt_lport_register(lport, phys_wwpn, npiv_wwpn, npiv_wwnn,
17828c2ecf20Sopenharmony_ci				 tcm_qla2xxx_lport_register_npiv_cb);
17838c2ecf20Sopenharmony_ci	if (ret != 0)
17848c2ecf20Sopenharmony_ci		goto out_lport;
17858c2ecf20Sopenharmony_ci
17868c2ecf20Sopenharmony_ci	return &lport->lport_wwn;
17878c2ecf20Sopenharmony_ciout_lport:
17888c2ecf20Sopenharmony_ci	vfree(lport->lport_loopid_map);
17898c2ecf20Sopenharmony_ci	btree_destroy32(&lport->lport_fcport_map);
17908c2ecf20Sopenharmony_ciout:
17918c2ecf20Sopenharmony_ci	kfree(lport);
17928c2ecf20Sopenharmony_ci	return ERR_PTR(ret);
17938c2ecf20Sopenharmony_ci}
17948c2ecf20Sopenharmony_ci
17958c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
17968c2ecf20Sopenharmony_ci{
17978c2ecf20Sopenharmony_ci	struct tcm_qla2xxx_lport *lport = container_of(wwn,
17988c2ecf20Sopenharmony_ci			struct tcm_qla2xxx_lport, lport_wwn);
17998c2ecf20Sopenharmony_ci	struct scsi_qla_host *npiv_vha = lport->qla_vha;
18008c2ecf20Sopenharmony_ci	struct qla_hw_data *ha = npiv_vha->hw;
18018c2ecf20Sopenharmony_ci	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
18028c2ecf20Sopenharmony_ci
18038c2ecf20Sopenharmony_ci	scsi_host_put(npiv_vha->host);
18048c2ecf20Sopenharmony_ci	/*
18058c2ecf20Sopenharmony_ci	 * Notify libfc that we want to release the vha->fc_vport
18068c2ecf20Sopenharmony_ci	 */
18078c2ecf20Sopenharmony_ci	fc_vport_terminate(npiv_vha->fc_vport);
18088c2ecf20Sopenharmony_ci	scsi_host_put(base_vha->host);
18098c2ecf20Sopenharmony_ci	kfree(lport);
18108c2ecf20Sopenharmony_ci}
18118c2ecf20Sopenharmony_ci
18128c2ecf20Sopenharmony_ci
18138c2ecf20Sopenharmony_cistatic ssize_t tcm_qla2xxx_wwn_version_show(struct config_item *item,
18148c2ecf20Sopenharmony_ci		char *page)
18158c2ecf20Sopenharmony_ci{
18168c2ecf20Sopenharmony_ci	return sprintf(page,
18178c2ecf20Sopenharmony_ci	    "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on %s\n",
18188c2ecf20Sopenharmony_ci	    QLA2XXX_VERSION, utsname()->sysname,
18198c2ecf20Sopenharmony_ci	    utsname()->machine, utsname()->release);
18208c2ecf20Sopenharmony_ci}
18218c2ecf20Sopenharmony_ci
18228c2ecf20Sopenharmony_ciCONFIGFS_ATTR_RO(tcm_qla2xxx_wwn_, version);
18238c2ecf20Sopenharmony_ci
18248c2ecf20Sopenharmony_cistatic struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
18258c2ecf20Sopenharmony_ci	&tcm_qla2xxx_wwn_attr_version,
18268c2ecf20Sopenharmony_ci	NULL,
18278c2ecf20Sopenharmony_ci};
18288c2ecf20Sopenharmony_ci
18298c2ecf20Sopenharmony_cistatic const struct target_core_fabric_ops tcm_qla2xxx_ops = {
18308c2ecf20Sopenharmony_ci	.module				= THIS_MODULE,
18318c2ecf20Sopenharmony_ci	.fabric_name			= "qla2xxx",
18328c2ecf20Sopenharmony_ci	.node_acl_size			= sizeof(struct tcm_qla2xxx_nacl),
18338c2ecf20Sopenharmony_ci	/*
18348c2ecf20Sopenharmony_ci	 * XXX: Limit assumes single page per scatter-gather-list entry.
18358c2ecf20Sopenharmony_ci	 * Current maximum is ~4.9 MB per se_cmd->t_data_sg with PAGE_SIZE=4096
18368c2ecf20Sopenharmony_ci	 */
18378c2ecf20Sopenharmony_ci	.max_data_sg_nents		= 1200,
18388c2ecf20Sopenharmony_ci	.tpg_get_wwn			= tcm_qla2xxx_get_fabric_wwn,
18398c2ecf20Sopenharmony_ci	.tpg_get_tag			= tcm_qla2xxx_get_tag,
18408c2ecf20Sopenharmony_ci	.tpg_check_demo_mode		= tcm_qla2xxx_check_demo_mode,
18418c2ecf20Sopenharmony_ci	.tpg_check_demo_mode_cache	= tcm_qla2xxx_check_demo_mode_cache,
18428c2ecf20Sopenharmony_ci	.tpg_check_demo_mode_write_protect =
18438c2ecf20Sopenharmony_ci					tcm_qla2xxx_check_demo_write_protect,
18448c2ecf20Sopenharmony_ci	.tpg_check_prod_mode_write_protect =
18458c2ecf20Sopenharmony_ci					tcm_qla2xxx_check_prod_write_protect,
18468c2ecf20Sopenharmony_ci	.tpg_check_prot_fabric_only	= tcm_qla2xxx_check_prot_fabric_only,
18478c2ecf20Sopenharmony_ci	.tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
18488c2ecf20Sopenharmony_ci	.tpg_get_inst_index		= tcm_qla2xxx_tpg_get_inst_index,
18498c2ecf20Sopenharmony_ci	.check_stop_free		= tcm_qla2xxx_check_stop_free,
18508c2ecf20Sopenharmony_ci	.release_cmd			= tcm_qla2xxx_release_cmd,
18518c2ecf20Sopenharmony_ci	.close_session			= tcm_qla2xxx_close_session,
18528c2ecf20Sopenharmony_ci	.sess_get_index			= tcm_qla2xxx_sess_get_index,
18538c2ecf20Sopenharmony_ci	.sess_get_initiator_sid		= NULL,
18548c2ecf20Sopenharmony_ci	.write_pending			= tcm_qla2xxx_write_pending,
18558c2ecf20Sopenharmony_ci	.set_default_node_attributes	= tcm_qla2xxx_set_default_node_attrs,
18568c2ecf20Sopenharmony_ci	.get_cmd_state			= tcm_qla2xxx_get_cmd_state,
18578c2ecf20Sopenharmony_ci	.queue_data_in			= tcm_qla2xxx_queue_data_in,
18588c2ecf20Sopenharmony_ci	.queue_status			= tcm_qla2xxx_queue_status,
18598c2ecf20Sopenharmony_ci	.queue_tm_rsp			= tcm_qla2xxx_queue_tm_rsp,
18608c2ecf20Sopenharmony_ci	.aborted_task			= tcm_qla2xxx_aborted_task,
18618c2ecf20Sopenharmony_ci	/*
18628c2ecf20Sopenharmony_ci	 * Setup function pointers for generic logic in
18638c2ecf20Sopenharmony_ci	 * target_core_fabric_configfs.c
18648c2ecf20Sopenharmony_ci	 */
18658c2ecf20Sopenharmony_ci	.fabric_make_wwn		= tcm_qla2xxx_make_lport,
18668c2ecf20Sopenharmony_ci	.fabric_drop_wwn		= tcm_qla2xxx_drop_lport,
18678c2ecf20Sopenharmony_ci	.fabric_make_tpg		= tcm_qla2xxx_make_tpg,
18688c2ecf20Sopenharmony_ci	.fabric_drop_tpg		= tcm_qla2xxx_drop_tpg,
18698c2ecf20Sopenharmony_ci	.fabric_init_nodeacl		= tcm_qla2xxx_init_nodeacl,
18708c2ecf20Sopenharmony_ci
18718c2ecf20Sopenharmony_ci	.tfc_wwn_attrs			= tcm_qla2xxx_wwn_attrs,
18728c2ecf20Sopenharmony_ci	.tfc_tpg_base_attrs		= tcm_qla2xxx_tpg_attrs,
18738c2ecf20Sopenharmony_ci	.tfc_tpg_attrib_attrs		= tcm_qla2xxx_tpg_attrib_attrs,
18748c2ecf20Sopenharmony_ci};
18758c2ecf20Sopenharmony_ci
18768c2ecf20Sopenharmony_cistatic const struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
18778c2ecf20Sopenharmony_ci	.module				= THIS_MODULE,
18788c2ecf20Sopenharmony_ci	.fabric_name			= "qla2xxx_npiv",
18798c2ecf20Sopenharmony_ci	.node_acl_size			= sizeof(struct tcm_qla2xxx_nacl),
18808c2ecf20Sopenharmony_ci	.tpg_get_wwn			= tcm_qla2xxx_get_fabric_wwn,
18818c2ecf20Sopenharmony_ci	.tpg_get_tag			= tcm_qla2xxx_get_tag,
18828c2ecf20Sopenharmony_ci	.tpg_check_demo_mode		= tcm_qla2xxx_check_demo_mode,
18838c2ecf20Sopenharmony_ci	.tpg_check_demo_mode_cache	= tcm_qla2xxx_check_demo_mode_cache,
18848c2ecf20Sopenharmony_ci	.tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_demo_mode,
18858c2ecf20Sopenharmony_ci	.tpg_check_prod_mode_write_protect =
18868c2ecf20Sopenharmony_ci	    tcm_qla2xxx_check_prod_write_protect,
18878c2ecf20Sopenharmony_ci	.tpg_check_demo_mode_login_only	= tcm_qla2xxx_check_demo_mode_login_only,
18888c2ecf20Sopenharmony_ci	.tpg_get_inst_index		= tcm_qla2xxx_tpg_get_inst_index,
18898c2ecf20Sopenharmony_ci	.check_stop_free                = tcm_qla2xxx_check_stop_free,
18908c2ecf20Sopenharmony_ci	.release_cmd			= tcm_qla2xxx_release_cmd,
18918c2ecf20Sopenharmony_ci	.close_session			= tcm_qla2xxx_close_session,
18928c2ecf20Sopenharmony_ci	.sess_get_index			= tcm_qla2xxx_sess_get_index,
18938c2ecf20Sopenharmony_ci	.sess_get_initiator_sid		= NULL,
18948c2ecf20Sopenharmony_ci	.write_pending			= tcm_qla2xxx_write_pending,
18958c2ecf20Sopenharmony_ci	.set_default_node_attributes	= tcm_qla2xxx_set_default_node_attrs,
18968c2ecf20Sopenharmony_ci	.get_cmd_state			= tcm_qla2xxx_get_cmd_state,
18978c2ecf20Sopenharmony_ci	.queue_data_in			= tcm_qla2xxx_queue_data_in,
18988c2ecf20Sopenharmony_ci	.queue_status			= tcm_qla2xxx_queue_status,
18998c2ecf20Sopenharmony_ci	.queue_tm_rsp			= tcm_qla2xxx_queue_tm_rsp,
19008c2ecf20Sopenharmony_ci	.aborted_task			= tcm_qla2xxx_aborted_task,
19018c2ecf20Sopenharmony_ci	/*
19028c2ecf20Sopenharmony_ci	 * Setup function pointers for generic logic in
19038c2ecf20Sopenharmony_ci	 * target_core_fabric_configfs.c
19048c2ecf20Sopenharmony_ci	 */
19058c2ecf20Sopenharmony_ci	.fabric_make_wwn		= tcm_qla2xxx_npiv_make_lport,
19068c2ecf20Sopenharmony_ci	.fabric_drop_wwn		= tcm_qla2xxx_npiv_drop_lport,
19078c2ecf20Sopenharmony_ci	.fabric_make_tpg		= tcm_qla2xxx_npiv_make_tpg,
19088c2ecf20Sopenharmony_ci	.fabric_drop_tpg		= tcm_qla2xxx_drop_tpg,
19098c2ecf20Sopenharmony_ci	.fabric_init_nodeacl		= tcm_qla2xxx_init_nodeacl,
19108c2ecf20Sopenharmony_ci
19118c2ecf20Sopenharmony_ci	.tfc_wwn_attrs			= tcm_qla2xxx_wwn_attrs,
19128c2ecf20Sopenharmony_ci	.tfc_tpg_base_attrs		= tcm_qla2xxx_npiv_tpg_attrs,
19138c2ecf20Sopenharmony_ci};
19148c2ecf20Sopenharmony_ci
19158c2ecf20Sopenharmony_cistatic int tcm_qla2xxx_register_configfs(void)
19168c2ecf20Sopenharmony_ci{
19178c2ecf20Sopenharmony_ci	int ret;
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_ci	pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on %s\n",
19208c2ecf20Sopenharmony_ci	    QLA2XXX_VERSION, utsname()->sysname,
19218c2ecf20Sopenharmony_ci	    utsname()->machine, utsname()->release);
19228c2ecf20Sopenharmony_ci
19238c2ecf20Sopenharmony_ci	ret = target_register_template(&tcm_qla2xxx_ops);
19248c2ecf20Sopenharmony_ci	if (ret)
19258c2ecf20Sopenharmony_ci		return ret;
19268c2ecf20Sopenharmony_ci
19278c2ecf20Sopenharmony_ci	ret = target_register_template(&tcm_qla2xxx_npiv_ops);
19288c2ecf20Sopenharmony_ci	if (ret)
19298c2ecf20Sopenharmony_ci		goto out_fabric;
19308c2ecf20Sopenharmony_ci
19318c2ecf20Sopenharmony_ci	tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
19328c2ecf20Sopenharmony_ci						WQ_MEM_RECLAIM, 0);
19338c2ecf20Sopenharmony_ci	if (!tcm_qla2xxx_free_wq) {
19348c2ecf20Sopenharmony_ci		ret = -ENOMEM;
19358c2ecf20Sopenharmony_ci		goto out_fabric_npiv;
19368c2ecf20Sopenharmony_ci	}
19378c2ecf20Sopenharmony_ci
19388c2ecf20Sopenharmony_ci	return 0;
19398c2ecf20Sopenharmony_ci
19408c2ecf20Sopenharmony_ciout_fabric_npiv:
19418c2ecf20Sopenharmony_ci	target_unregister_template(&tcm_qla2xxx_npiv_ops);
19428c2ecf20Sopenharmony_ciout_fabric:
19438c2ecf20Sopenharmony_ci	target_unregister_template(&tcm_qla2xxx_ops);
19448c2ecf20Sopenharmony_ci	return ret;
19458c2ecf20Sopenharmony_ci}
19468c2ecf20Sopenharmony_ci
19478c2ecf20Sopenharmony_cistatic void tcm_qla2xxx_deregister_configfs(void)
19488c2ecf20Sopenharmony_ci{
19498c2ecf20Sopenharmony_ci	destroy_workqueue(tcm_qla2xxx_free_wq);
19508c2ecf20Sopenharmony_ci
19518c2ecf20Sopenharmony_ci	target_unregister_template(&tcm_qla2xxx_ops);
19528c2ecf20Sopenharmony_ci	target_unregister_template(&tcm_qla2xxx_npiv_ops);
19538c2ecf20Sopenharmony_ci}
19548c2ecf20Sopenharmony_ci
19558c2ecf20Sopenharmony_cistatic int __init tcm_qla2xxx_init(void)
19568c2ecf20Sopenharmony_ci{
19578c2ecf20Sopenharmony_ci	int ret;
19588c2ecf20Sopenharmony_ci
19598c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct abts_recv_from_24xx) != 64);
19608c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct abts_resp_from_24xx_fw) != 64);
19618c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct atio7_fcp_cmnd) != 32);
19628c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct atio_from_isp) != 64);
19638c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct ba_acc_le) != 12);
19648c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct ba_rjt_le) != 4);
19658c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct ctio7_from_24xx) != 64);
19668c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
19678c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct ctio_crc2_to_fw) != 64);
19688c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct ctio_crc_from_fw) != 64);
19698c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
19708c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct fcp_hdr) != 24);
19718c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct fcp_hdr_le) != 24);
19728c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct nack_to_isp) != 64);
19738c2ecf20Sopenharmony_ci
19748c2ecf20Sopenharmony_ci	ret = tcm_qla2xxx_register_configfs();
19758c2ecf20Sopenharmony_ci	if (ret < 0)
19768c2ecf20Sopenharmony_ci		return ret;
19778c2ecf20Sopenharmony_ci
19788c2ecf20Sopenharmony_ci	return 0;
19798c2ecf20Sopenharmony_ci}
19808c2ecf20Sopenharmony_ci
19818c2ecf20Sopenharmony_cistatic void __exit tcm_qla2xxx_exit(void)
19828c2ecf20Sopenharmony_ci{
19838c2ecf20Sopenharmony_ci	tcm_qla2xxx_deregister_configfs();
19848c2ecf20Sopenharmony_ci}
19858c2ecf20Sopenharmony_ci
19868c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TCM QLA24XX+ series NPIV enabled fabric driver");
19878c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
19888c2ecf20Sopenharmony_cimodule_init(tcm_qla2xxx_init);
19898c2ecf20Sopenharmony_cimodule_exit(tcm_qla2xxx_exit);
1990