18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*******************************************************************************
38c2ecf20Sopenharmony_ci * This file contains iSCSI Target Portal Group related functions.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * (c) Copyright 2007-2013 Datera, Inc.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci ******************************************************************************/
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <target/target_core_base.h>
138c2ecf20Sopenharmony_ci#include <target/target_core_fabric.h>
148c2ecf20Sopenharmony_ci#include <target/iscsi/iscsi_target_core.h>
158c2ecf20Sopenharmony_ci#include "iscsi_target_erl0.h"
168c2ecf20Sopenharmony_ci#include "iscsi_target_login.h"
178c2ecf20Sopenharmony_ci#include "iscsi_target_nodeattrib.h"
188c2ecf20Sopenharmony_ci#include "iscsi_target_tpg.h"
198c2ecf20Sopenharmony_ci#include "iscsi_target_util.h"
208c2ecf20Sopenharmony_ci#include "iscsi_target.h"
218c2ecf20Sopenharmony_ci#include "iscsi_target_parameters.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <target/iscsi/iscsi_transport.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistruct iscsi_portal_group *iscsit_alloc_portal_group(struct iscsi_tiqn *tiqn, u16 tpgt)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	tpg = kzalloc(sizeof(struct iscsi_portal_group), GFP_KERNEL);
308c2ecf20Sopenharmony_ci	if (!tpg) {
318c2ecf20Sopenharmony_ci		pr_err("Unable to allocate struct iscsi_portal_group\n");
328c2ecf20Sopenharmony_ci		return NULL;
338c2ecf20Sopenharmony_ci	}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	tpg->tpgt = tpgt;
368c2ecf20Sopenharmony_ci	tpg->tpg_state = TPG_STATE_FREE;
378c2ecf20Sopenharmony_ci	tpg->tpg_tiqn = tiqn;
388c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&tpg->tpg_gnp_list);
398c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&tpg->tpg_list);
408c2ecf20Sopenharmony_ci	mutex_init(&tpg->tpg_access_lock);
418c2ecf20Sopenharmony_ci	sema_init(&tpg->np_login_sem, 1);
428c2ecf20Sopenharmony_ci	spin_lock_init(&tpg->tpg_state_lock);
438c2ecf20Sopenharmony_ci	spin_lock_init(&tpg->tpg_np_lock);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	return tpg;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ciint iscsit_load_discovery_tpg(void)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	struct iscsi_param *param;
538c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg;
548c2ecf20Sopenharmony_ci	int ret;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	tpg = iscsit_alloc_portal_group(NULL, 1);
578c2ecf20Sopenharmony_ci	if (!tpg) {
588c2ecf20Sopenharmony_ci		pr_err("Unable to allocate struct iscsi_portal_group\n");
598c2ecf20Sopenharmony_ci		return -1;
608c2ecf20Sopenharmony_ci	}
618c2ecf20Sopenharmony_ci	/*
628c2ecf20Sopenharmony_ci	 * Save iscsi_ops pointer for special case discovery TPG that
638c2ecf20Sopenharmony_ci	 * doesn't exist as se_wwn->wwn_group within configfs.
648c2ecf20Sopenharmony_ci	 */
658c2ecf20Sopenharmony_ci	tpg->tpg_se_tpg.se_tpg_tfo = &iscsi_ops;
668c2ecf20Sopenharmony_ci	ret = core_tpg_register(NULL, &tpg->tpg_se_tpg, -1);
678c2ecf20Sopenharmony_ci	if (ret < 0) {
688c2ecf20Sopenharmony_ci		kfree(tpg);
698c2ecf20Sopenharmony_ci		return -1;
708c2ecf20Sopenharmony_ci	}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	tpg->sid = 1; /* First Assigned LIO Session ID */
738c2ecf20Sopenharmony_ci	iscsit_set_default_tpg_attribs(tpg);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	if (iscsi_create_default_params(&tpg->param_list) < 0)
768c2ecf20Sopenharmony_ci		goto out;
778c2ecf20Sopenharmony_ci	/*
788c2ecf20Sopenharmony_ci	 * By default we disable authentication for discovery sessions,
798c2ecf20Sopenharmony_ci	 * this can be changed with:
808c2ecf20Sopenharmony_ci	 *
818c2ecf20Sopenharmony_ci	 * /sys/kernel/config/target/iscsi/discovery_auth/enforce_discovery_auth
828c2ecf20Sopenharmony_ci	 */
838c2ecf20Sopenharmony_ci	param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
848c2ecf20Sopenharmony_ci	if (!param)
858c2ecf20Sopenharmony_ci		goto free_pl_out;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	if (iscsi_update_param_value(param, "CHAP,None") < 0)
888c2ecf20Sopenharmony_ci		goto free_pl_out;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	tpg->tpg_attrib.authentication = 0;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	spin_lock(&tpg->tpg_state_lock);
938c2ecf20Sopenharmony_ci	tpg->tpg_state  = TPG_STATE_ACTIVE;
948c2ecf20Sopenharmony_ci	spin_unlock(&tpg->tpg_state_lock);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	iscsit_global->discovery_tpg = tpg;
978c2ecf20Sopenharmony_ci	pr_debug("CORE[0] - Allocated Discovery TPG\n");
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	return 0;
1008c2ecf20Sopenharmony_cifree_pl_out:
1018c2ecf20Sopenharmony_ci	iscsi_release_param_list(tpg->param_list);
1028c2ecf20Sopenharmony_ciout:
1038c2ecf20Sopenharmony_ci	if (tpg->sid == 1)
1048c2ecf20Sopenharmony_ci		core_tpg_deregister(&tpg->tpg_se_tpg);
1058c2ecf20Sopenharmony_ci	kfree(tpg);
1068c2ecf20Sopenharmony_ci	return -1;
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_civoid iscsit_release_discovery_tpg(void)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg = iscsit_global->discovery_tpg;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	if (!tpg)
1148c2ecf20Sopenharmony_ci		return;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	iscsi_release_param_list(tpg->param_list);
1178c2ecf20Sopenharmony_ci	core_tpg_deregister(&tpg->tpg_se_tpg);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	kfree(tpg);
1208c2ecf20Sopenharmony_ci	iscsit_global->discovery_tpg = NULL;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistruct iscsi_portal_group *iscsit_get_tpg_from_np(
1248c2ecf20Sopenharmony_ci	struct iscsi_tiqn *tiqn,
1258c2ecf20Sopenharmony_ci	struct iscsi_np *np,
1268c2ecf20Sopenharmony_ci	struct iscsi_tpg_np **tpg_np_out)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg = NULL;
1298c2ecf20Sopenharmony_ci	struct iscsi_tpg_np *tpg_np;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	spin_lock(&tiqn->tiqn_tpg_lock);
1328c2ecf20Sopenharmony_ci	list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		spin_lock(&tpg->tpg_state_lock);
1358c2ecf20Sopenharmony_ci		if (tpg->tpg_state != TPG_STATE_ACTIVE) {
1368c2ecf20Sopenharmony_ci			spin_unlock(&tpg->tpg_state_lock);
1378c2ecf20Sopenharmony_ci			continue;
1388c2ecf20Sopenharmony_ci		}
1398c2ecf20Sopenharmony_ci		spin_unlock(&tpg->tpg_state_lock);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci		spin_lock(&tpg->tpg_np_lock);
1428c2ecf20Sopenharmony_ci		list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
1438c2ecf20Sopenharmony_ci			if (tpg_np->tpg_np == np) {
1448c2ecf20Sopenharmony_ci				*tpg_np_out = tpg_np;
1458c2ecf20Sopenharmony_ci				kref_get(&tpg_np->tpg_np_kref);
1468c2ecf20Sopenharmony_ci				spin_unlock(&tpg->tpg_np_lock);
1478c2ecf20Sopenharmony_ci				spin_unlock(&tiqn->tiqn_tpg_lock);
1488c2ecf20Sopenharmony_ci				return tpg;
1498c2ecf20Sopenharmony_ci			}
1508c2ecf20Sopenharmony_ci		}
1518c2ecf20Sopenharmony_ci		spin_unlock(&tpg->tpg_np_lock);
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci	spin_unlock(&tiqn->tiqn_tpg_lock);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	return NULL;
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ciint iscsit_get_tpg(
1598c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	return mutex_lock_interruptible(&tpg->tpg_access_lock);
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_civoid iscsit_put_tpg(struct iscsi_portal_group *tpg)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	mutex_unlock(&tpg->tpg_access_lock);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic void iscsit_clear_tpg_np_login_thread(
1708c2ecf20Sopenharmony_ci	struct iscsi_tpg_np *tpg_np,
1718c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
1728c2ecf20Sopenharmony_ci	bool shutdown)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	if (!tpg_np->tpg_np) {
1758c2ecf20Sopenharmony_ci		pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
1768c2ecf20Sopenharmony_ci		return;
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	if (shutdown)
1808c2ecf20Sopenharmony_ci		tpg_np->tpg_np->enabled = false;
1818c2ecf20Sopenharmony_ci	iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg, shutdown);
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic void iscsit_clear_tpg_np_login_threads(
1858c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
1868c2ecf20Sopenharmony_ci	bool shutdown)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	struct iscsi_tpg_np *tpg_np;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	spin_lock(&tpg->tpg_np_lock);
1918c2ecf20Sopenharmony_ci	list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
1928c2ecf20Sopenharmony_ci		if (!tpg_np->tpg_np) {
1938c2ecf20Sopenharmony_ci			pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
1948c2ecf20Sopenharmony_ci			continue;
1958c2ecf20Sopenharmony_ci		}
1968c2ecf20Sopenharmony_ci		spin_unlock(&tpg->tpg_np_lock);
1978c2ecf20Sopenharmony_ci		iscsit_clear_tpg_np_login_thread(tpg_np, tpg, shutdown);
1988c2ecf20Sopenharmony_ci		spin_lock(&tpg->tpg_np_lock);
1998c2ecf20Sopenharmony_ci	}
2008c2ecf20Sopenharmony_ci	spin_unlock(&tpg->tpg_np_lock);
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_civoid iscsit_tpg_dump_params(struct iscsi_portal_group *tpg)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	iscsi_print_params(tpg->param_list);
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *tpg)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	a->authentication = TA_AUTHENTICATION;
2138c2ecf20Sopenharmony_ci	a->login_timeout = TA_LOGIN_TIMEOUT;
2148c2ecf20Sopenharmony_ci	a->netif_timeout = TA_NETIF_TIMEOUT;
2158c2ecf20Sopenharmony_ci	a->default_cmdsn_depth = TA_DEFAULT_CMDSN_DEPTH;
2168c2ecf20Sopenharmony_ci	a->generate_node_acls = TA_GENERATE_NODE_ACLS;
2178c2ecf20Sopenharmony_ci	a->cache_dynamic_acls = TA_CACHE_DYNAMIC_ACLS;
2188c2ecf20Sopenharmony_ci	a->demo_mode_write_protect = TA_DEMO_MODE_WRITE_PROTECT;
2198c2ecf20Sopenharmony_ci	a->prod_mode_write_protect = TA_PROD_MODE_WRITE_PROTECT;
2208c2ecf20Sopenharmony_ci	a->demo_mode_discovery = TA_DEMO_MODE_DISCOVERY;
2218c2ecf20Sopenharmony_ci	a->default_erl = TA_DEFAULT_ERL;
2228c2ecf20Sopenharmony_ci	a->t10_pi = TA_DEFAULT_T10_PI;
2238c2ecf20Sopenharmony_ci	a->fabric_prot_type = TA_DEFAULT_FABRIC_PROT_TYPE;
2248c2ecf20Sopenharmony_ci	a->tpg_enabled_sendtargets = TA_DEFAULT_TPG_ENABLED_SENDTARGETS;
2258c2ecf20Sopenharmony_ci	a->login_keys_workaround = TA_DEFAULT_LOGIN_KEYS_WORKAROUND;
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ciint iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	if (tpg->tpg_state != TPG_STATE_FREE) {
2318c2ecf20Sopenharmony_ci		pr_err("Unable to add iSCSI Target Portal Group: %d"
2328c2ecf20Sopenharmony_ci			" while not in TPG_STATE_FREE state.\n", tpg->tpgt);
2338c2ecf20Sopenharmony_ci		return -EEXIST;
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci	iscsit_set_default_tpg_attribs(tpg);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	if (iscsi_create_default_params(&tpg->param_list) < 0)
2388c2ecf20Sopenharmony_ci		goto err_out;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	tpg->tpg_attrib.tpg = tpg;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	spin_lock(&tpg->tpg_state_lock);
2438c2ecf20Sopenharmony_ci	tpg->tpg_state	= TPG_STATE_INACTIVE;
2448c2ecf20Sopenharmony_ci	spin_unlock(&tpg->tpg_state_lock);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	spin_lock(&tiqn->tiqn_tpg_lock);
2478c2ecf20Sopenharmony_ci	list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
2488c2ecf20Sopenharmony_ci	tiqn->tiqn_ntpgs++;
2498c2ecf20Sopenharmony_ci	pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
2508c2ecf20Sopenharmony_ci			tiqn->tiqn, tpg->tpgt);
2518c2ecf20Sopenharmony_ci	spin_unlock(&tiqn->tiqn_tpg_lock);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	return 0;
2548c2ecf20Sopenharmony_cierr_out:
2558c2ecf20Sopenharmony_ci	if (tpg->param_list) {
2568c2ecf20Sopenharmony_ci		iscsi_release_param_list(tpg->param_list);
2578c2ecf20Sopenharmony_ci		tpg->param_list = NULL;
2588c2ecf20Sopenharmony_ci	}
2598c2ecf20Sopenharmony_ci	return -ENOMEM;
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ciint iscsit_tpg_del_portal_group(
2638c2ecf20Sopenharmony_ci	struct iscsi_tiqn *tiqn,
2648c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
2658c2ecf20Sopenharmony_ci	int force)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	u8 old_state = tpg->tpg_state;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	spin_lock(&tpg->tpg_state_lock);
2708c2ecf20Sopenharmony_ci	tpg->tpg_state = TPG_STATE_INACTIVE;
2718c2ecf20Sopenharmony_ci	spin_unlock(&tpg->tpg_state_lock);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
2748c2ecf20Sopenharmony_ci		pr_err("Unable to delete iSCSI Target Portal Group:"
2758c2ecf20Sopenharmony_ci			" %hu while active sessions exist, and force=0\n",
2768c2ecf20Sopenharmony_ci			tpg->tpgt);
2778c2ecf20Sopenharmony_ci		tpg->tpg_state = old_state;
2788c2ecf20Sopenharmony_ci		return -EPERM;
2798c2ecf20Sopenharmony_ci	}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	if (tpg->param_list) {
2828c2ecf20Sopenharmony_ci		iscsi_release_param_list(tpg->param_list);
2838c2ecf20Sopenharmony_ci		tpg->param_list = NULL;
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	core_tpg_deregister(&tpg->tpg_se_tpg);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	spin_lock(&tpg->tpg_state_lock);
2898c2ecf20Sopenharmony_ci	tpg->tpg_state = TPG_STATE_FREE;
2908c2ecf20Sopenharmony_ci	spin_unlock(&tpg->tpg_state_lock);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	spin_lock(&tiqn->tiqn_tpg_lock);
2938c2ecf20Sopenharmony_ci	tiqn->tiqn_ntpgs--;
2948c2ecf20Sopenharmony_ci	list_del(&tpg->tpg_list);
2958c2ecf20Sopenharmony_ci	spin_unlock(&tiqn->tiqn_tpg_lock);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
2988c2ecf20Sopenharmony_ci			tiqn->tiqn, tpg->tpgt);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	kfree(tpg);
3018c2ecf20Sopenharmony_ci	return 0;
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ciint iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	struct iscsi_param *param;
3078c2ecf20Sopenharmony_ci	struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
3088c2ecf20Sopenharmony_ci	int ret;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	if (tpg->tpg_state == TPG_STATE_ACTIVE) {
3118c2ecf20Sopenharmony_ci		pr_err("iSCSI target portal group: %hu is already"
3128c2ecf20Sopenharmony_ci			" active, ignoring request.\n", tpg->tpgt);
3138c2ecf20Sopenharmony_ci		return -EINVAL;
3148c2ecf20Sopenharmony_ci	}
3158c2ecf20Sopenharmony_ci	/*
3168c2ecf20Sopenharmony_ci	 * Make sure that AuthMethod does not contain None as an option
3178c2ecf20Sopenharmony_ci	 * unless explictly disabled.  Set the default to CHAP if authentication
3188c2ecf20Sopenharmony_ci	 * is enforced (as per default), and remove the NONE option.
3198c2ecf20Sopenharmony_ci	 */
3208c2ecf20Sopenharmony_ci	param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
3218c2ecf20Sopenharmony_ci	if (!param)
3228c2ecf20Sopenharmony_ci		return -EINVAL;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (tpg->tpg_attrib.authentication) {
3258c2ecf20Sopenharmony_ci		if (!strcmp(param->value, NONE)) {
3268c2ecf20Sopenharmony_ci			ret = iscsi_update_param_value(param, CHAP);
3278c2ecf20Sopenharmony_ci			if (ret)
3288c2ecf20Sopenharmony_ci				goto err;
3298c2ecf20Sopenharmony_ci		}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci		ret = iscsit_ta_authentication(tpg, 1);
3328c2ecf20Sopenharmony_ci		if (ret < 0)
3338c2ecf20Sopenharmony_ci			goto err;
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	spin_lock(&tpg->tpg_state_lock);
3378c2ecf20Sopenharmony_ci	tpg->tpg_state = TPG_STATE_ACTIVE;
3388c2ecf20Sopenharmony_ci	spin_unlock(&tpg->tpg_state_lock);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	spin_lock(&tiqn->tiqn_tpg_lock);
3418c2ecf20Sopenharmony_ci	tiqn->tiqn_active_tpgs++;
3428c2ecf20Sopenharmony_ci	pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
3438c2ecf20Sopenharmony_ci			tpg->tpgt);
3448c2ecf20Sopenharmony_ci	spin_unlock(&tiqn->tiqn_tpg_lock);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	return 0;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cierr:
3498c2ecf20Sopenharmony_ci	return ret;
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ciint iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	struct iscsi_tiqn *tiqn;
3558c2ecf20Sopenharmony_ci	u8 old_state = tpg->tpg_state;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	spin_lock(&tpg->tpg_state_lock);
3588c2ecf20Sopenharmony_ci	if (tpg->tpg_state == TPG_STATE_INACTIVE) {
3598c2ecf20Sopenharmony_ci		pr_err("iSCSI Target Portal Group: %hu is already"
3608c2ecf20Sopenharmony_ci			" inactive, ignoring request.\n", tpg->tpgt);
3618c2ecf20Sopenharmony_ci		spin_unlock(&tpg->tpg_state_lock);
3628c2ecf20Sopenharmony_ci		return -EINVAL;
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci	tpg->tpg_state = TPG_STATE_INACTIVE;
3658c2ecf20Sopenharmony_ci	spin_unlock(&tpg->tpg_state_lock);
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	iscsit_clear_tpg_np_login_threads(tpg, false);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
3708c2ecf20Sopenharmony_ci		spin_lock(&tpg->tpg_state_lock);
3718c2ecf20Sopenharmony_ci		tpg->tpg_state = old_state;
3728c2ecf20Sopenharmony_ci		spin_unlock(&tpg->tpg_state_lock);
3738c2ecf20Sopenharmony_ci		pr_err("Unable to disable iSCSI Target Portal Group:"
3748c2ecf20Sopenharmony_ci			" %hu while active sessions exist, and force=0\n",
3758c2ecf20Sopenharmony_ci			tpg->tpgt);
3768c2ecf20Sopenharmony_ci		return -EPERM;
3778c2ecf20Sopenharmony_ci	}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	tiqn = tpg->tpg_tiqn;
3808c2ecf20Sopenharmony_ci	if (!tiqn || (tpg == iscsit_global->discovery_tpg))
3818c2ecf20Sopenharmony_ci		return 0;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	spin_lock(&tiqn->tiqn_tpg_lock);
3848c2ecf20Sopenharmony_ci	tiqn->tiqn_active_tpgs--;
3858c2ecf20Sopenharmony_ci	pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
3868c2ecf20Sopenharmony_ci			tpg->tpgt);
3878c2ecf20Sopenharmony_ci	spin_unlock(&tiqn->tiqn_tpg_lock);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	return 0;
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistruct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
3938c2ecf20Sopenharmony_ci	struct iscsi_session *sess)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	struct se_session *se_sess = sess->se_sess;
3968c2ecf20Sopenharmony_ci	struct se_node_acl *se_nacl = se_sess->se_node_acl;
3978c2ecf20Sopenharmony_ci	struct iscsi_node_acl *acl = container_of(se_nacl, struct iscsi_node_acl,
3988c2ecf20Sopenharmony_ci					se_node_acl);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	return &acl->node_attrib;
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistruct iscsi_tpg_np *iscsit_tpg_locate_child_np(
4048c2ecf20Sopenharmony_ci	struct iscsi_tpg_np *tpg_np,
4058c2ecf20Sopenharmony_ci	int network_transport)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	spin_lock(&tpg_np->tpg_np_parent_lock);
4108c2ecf20Sopenharmony_ci	list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
4118c2ecf20Sopenharmony_ci			&tpg_np->tpg_np_parent_list, tpg_np_child_list) {
4128c2ecf20Sopenharmony_ci		if (tpg_np_child->tpg_np->np_network_transport ==
4138c2ecf20Sopenharmony_ci				network_transport) {
4148c2ecf20Sopenharmony_ci			spin_unlock(&tpg_np->tpg_np_parent_lock);
4158c2ecf20Sopenharmony_ci			return tpg_np_child;
4168c2ecf20Sopenharmony_ci		}
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci	spin_unlock(&tpg_np->tpg_np_parent_lock);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	return NULL;
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cistatic bool iscsit_tpg_check_network_portal(
4248c2ecf20Sopenharmony_ci	struct iscsi_tiqn *tiqn,
4258c2ecf20Sopenharmony_ci	struct sockaddr_storage *sockaddr,
4268c2ecf20Sopenharmony_ci	int network_transport)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg;
4298c2ecf20Sopenharmony_ci	struct iscsi_tpg_np *tpg_np;
4308c2ecf20Sopenharmony_ci	struct iscsi_np *np;
4318c2ecf20Sopenharmony_ci	bool match = false;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	spin_lock(&tiqn->tiqn_tpg_lock);
4348c2ecf20Sopenharmony_ci	list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci		spin_lock(&tpg->tpg_np_lock);
4378c2ecf20Sopenharmony_ci		list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
4388c2ecf20Sopenharmony_ci			np = tpg_np->tpg_np;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci			match = iscsit_check_np_match(sockaddr, np,
4418c2ecf20Sopenharmony_ci						network_transport);
4428c2ecf20Sopenharmony_ci			if (match)
4438c2ecf20Sopenharmony_ci				break;
4448c2ecf20Sopenharmony_ci		}
4458c2ecf20Sopenharmony_ci		spin_unlock(&tpg->tpg_np_lock);
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci		if (match)
4488c2ecf20Sopenharmony_ci			break;
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci	spin_unlock(&tiqn->tiqn_tpg_lock);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	return match;
4538c2ecf20Sopenharmony_ci}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_cistruct iscsi_tpg_np *iscsit_tpg_add_network_portal(
4568c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
4578c2ecf20Sopenharmony_ci	struct sockaddr_storage *sockaddr,
4588c2ecf20Sopenharmony_ci	struct iscsi_tpg_np *tpg_np_parent,
4598c2ecf20Sopenharmony_ci	int network_transport)
4608c2ecf20Sopenharmony_ci{
4618c2ecf20Sopenharmony_ci	struct iscsi_np *np;
4628c2ecf20Sopenharmony_ci	struct iscsi_tpg_np *tpg_np;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	if (!tpg_np_parent) {
4658c2ecf20Sopenharmony_ci		if (iscsit_tpg_check_network_portal(tpg->tpg_tiqn, sockaddr,
4668c2ecf20Sopenharmony_ci				network_transport)) {
4678c2ecf20Sopenharmony_ci			pr_err("Network Portal: %pISc already exists on a"
4688c2ecf20Sopenharmony_ci				" different TPG on %s\n", sockaddr,
4698c2ecf20Sopenharmony_ci				tpg->tpg_tiqn->tiqn);
4708c2ecf20Sopenharmony_ci			return ERR_PTR(-EEXIST);
4718c2ecf20Sopenharmony_ci		}
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	tpg_np = kzalloc(sizeof(struct iscsi_tpg_np), GFP_KERNEL);
4758c2ecf20Sopenharmony_ci	if (!tpg_np) {
4768c2ecf20Sopenharmony_ci		pr_err("Unable to allocate memory for"
4778c2ecf20Sopenharmony_ci				" struct iscsi_tpg_np.\n");
4788c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
4798c2ecf20Sopenharmony_ci	}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	np = iscsit_add_np(sockaddr, network_transport);
4828c2ecf20Sopenharmony_ci	if (IS_ERR(np)) {
4838c2ecf20Sopenharmony_ci		kfree(tpg_np);
4848c2ecf20Sopenharmony_ci		return ERR_CAST(np);
4858c2ecf20Sopenharmony_ci	}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&tpg_np->tpg_np_list);
4888c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&tpg_np->tpg_np_child_list);
4898c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&tpg_np->tpg_np_parent_list);
4908c2ecf20Sopenharmony_ci	spin_lock_init(&tpg_np->tpg_np_parent_lock);
4918c2ecf20Sopenharmony_ci	init_completion(&tpg_np->tpg_np_comp);
4928c2ecf20Sopenharmony_ci	kref_init(&tpg_np->tpg_np_kref);
4938c2ecf20Sopenharmony_ci	tpg_np->tpg_np		= np;
4948c2ecf20Sopenharmony_ci	tpg_np->tpg		= tpg;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	spin_lock(&tpg->tpg_np_lock);
4978c2ecf20Sopenharmony_ci	list_add_tail(&tpg_np->tpg_np_list, &tpg->tpg_gnp_list);
4988c2ecf20Sopenharmony_ci	tpg->num_tpg_nps++;
4998c2ecf20Sopenharmony_ci	if (tpg->tpg_tiqn)
5008c2ecf20Sopenharmony_ci		tpg->tpg_tiqn->tiqn_num_tpg_nps++;
5018c2ecf20Sopenharmony_ci	spin_unlock(&tpg->tpg_np_lock);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	if (tpg_np_parent) {
5048c2ecf20Sopenharmony_ci		tpg_np->tpg_np_parent = tpg_np_parent;
5058c2ecf20Sopenharmony_ci		spin_lock(&tpg_np_parent->tpg_np_parent_lock);
5068c2ecf20Sopenharmony_ci		list_add_tail(&tpg_np->tpg_np_child_list,
5078c2ecf20Sopenharmony_ci			&tpg_np_parent->tpg_np_parent_list);
5088c2ecf20Sopenharmony_ci		spin_unlock(&tpg_np_parent->tpg_np_parent_lock);
5098c2ecf20Sopenharmony_ci	}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	pr_debug("CORE[%s] - Added Network Portal: %pISpc,%hu on %s\n",
5128c2ecf20Sopenharmony_ci		tpg->tpg_tiqn->tiqn, &np->np_sockaddr, tpg->tpgt,
5138c2ecf20Sopenharmony_ci		np->np_transport->name);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	return tpg_np;
5168c2ecf20Sopenharmony_ci}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_cistatic int iscsit_tpg_release_np(
5198c2ecf20Sopenharmony_ci	struct iscsi_tpg_np *tpg_np,
5208c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
5218c2ecf20Sopenharmony_ci	struct iscsi_np *np)
5228c2ecf20Sopenharmony_ci{
5238c2ecf20Sopenharmony_ci	iscsit_clear_tpg_np_login_thread(tpg_np, tpg, true);
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	pr_debug("CORE[%s] - Removed Network Portal: %pISpc,%hu on %s\n",
5268c2ecf20Sopenharmony_ci		tpg->tpg_tiqn->tiqn, &np->np_sockaddr, tpg->tpgt,
5278c2ecf20Sopenharmony_ci		np->np_transport->name);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	tpg_np->tpg_np = NULL;
5308c2ecf20Sopenharmony_ci	tpg_np->tpg = NULL;
5318c2ecf20Sopenharmony_ci	kfree(tpg_np);
5328c2ecf20Sopenharmony_ci	/*
5338c2ecf20Sopenharmony_ci	 * iscsit_del_np() will shutdown struct iscsi_np when last TPG reference is released.
5348c2ecf20Sopenharmony_ci	 */
5358c2ecf20Sopenharmony_ci	return iscsit_del_np(np);
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ciint iscsit_tpg_del_network_portal(
5398c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
5408c2ecf20Sopenharmony_ci	struct iscsi_tpg_np *tpg_np)
5418c2ecf20Sopenharmony_ci{
5428c2ecf20Sopenharmony_ci	struct iscsi_np *np;
5438c2ecf20Sopenharmony_ci	struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
5448c2ecf20Sopenharmony_ci	int ret = 0;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	np = tpg_np->tpg_np;
5478c2ecf20Sopenharmony_ci	if (!np) {
5488c2ecf20Sopenharmony_ci		pr_err("Unable to locate struct iscsi_np from"
5498c2ecf20Sopenharmony_ci				" struct iscsi_tpg_np\n");
5508c2ecf20Sopenharmony_ci		return -EINVAL;
5518c2ecf20Sopenharmony_ci	}
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	if (!tpg_np->tpg_np_parent) {
5548c2ecf20Sopenharmony_ci		/*
5558c2ecf20Sopenharmony_ci		 * We are the parent tpg network portal.  Release all of the
5568c2ecf20Sopenharmony_ci		 * child tpg_np's (eg: the non ISCSI_TCP ones) on our parent
5578c2ecf20Sopenharmony_ci		 * list first.
5588c2ecf20Sopenharmony_ci		 */
5598c2ecf20Sopenharmony_ci		list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
5608c2ecf20Sopenharmony_ci				&tpg_np->tpg_np_parent_list,
5618c2ecf20Sopenharmony_ci				tpg_np_child_list) {
5628c2ecf20Sopenharmony_ci			ret = iscsit_tpg_del_network_portal(tpg, tpg_np_child);
5638c2ecf20Sopenharmony_ci			if (ret < 0)
5648c2ecf20Sopenharmony_ci				pr_err("iscsit_tpg_del_network_portal()"
5658c2ecf20Sopenharmony_ci					" failed: %d\n", ret);
5668c2ecf20Sopenharmony_ci		}
5678c2ecf20Sopenharmony_ci	} else {
5688c2ecf20Sopenharmony_ci		/*
5698c2ecf20Sopenharmony_ci		 * We are not the parent ISCSI_TCP tpg network portal.  Release
5708c2ecf20Sopenharmony_ci		 * our own network portals from the child list.
5718c2ecf20Sopenharmony_ci		 */
5728c2ecf20Sopenharmony_ci		spin_lock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
5738c2ecf20Sopenharmony_ci		list_del(&tpg_np->tpg_np_child_list);
5748c2ecf20Sopenharmony_ci		spin_unlock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
5758c2ecf20Sopenharmony_ci	}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	spin_lock(&tpg->tpg_np_lock);
5788c2ecf20Sopenharmony_ci	list_del(&tpg_np->tpg_np_list);
5798c2ecf20Sopenharmony_ci	tpg->num_tpg_nps--;
5808c2ecf20Sopenharmony_ci	if (tpg->tpg_tiqn)
5818c2ecf20Sopenharmony_ci		tpg->tpg_tiqn->tiqn_num_tpg_nps--;
5828c2ecf20Sopenharmony_ci	spin_unlock(&tpg->tpg_np_lock);
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	return iscsit_tpg_release_np(tpg_np, tpg, np);
5858c2ecf20Sopenharmony_ci}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ciint iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
5888c2ecf20Sopenharmony_ci{
5898c2ecf20Sopenharmony_ci	unsigned char buf1[256], buf2[256], *none = NULL;
5908c2ecf20Sopenharmony_ci	int len;
5918c2ecf20Sopenharmony_ci	struct iscsi_param *param;
5928c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	if ((authentication != 1) && (authentication != 0)) {
5958c2ecf20Sopenharmony_ci		pr_err("Illegal value for authentication parameter:"
5968c2ecf20Sopenharmony_ci			" %u, ignoring request.\n", authentication);
5978c2ecf20Sopenharmony_ci		return -EINVAL;
5988c2ecf20Sopenharmony_ci	}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	memset(buf1, 0, sizeof(buf1));
6018c2ecf20Sopenharmony_ci	memset(buf2, 0, sizeof(buf2));
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
6048c2ecf20Sopenharmony_ci	if (!param)
6058c2ecf20Sopenharmony_ci		return -EINVAL;
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	if (authentication) {
6088c2ecf20Sopenharmony_ci		snprintf(buf1, sizeof(buf1), "%s", param->value);
6098c2ecf20Sopenharmony_ci		none = strstr(buf1, NONE);
6108c2ecf20Sopenharmony_ci		if (!none)
6118c2ecf20Sopenharmony_ci			goto out;
6128c2ecf20Sopenharmony_ci		if (!strncmp(none + 4, ",", 1)) {
6138c2ecf20Sopenharmony_ci			if (!strcmp(buf1, none))
6148c2ecf20Sopenharmony_ci				sprintf(buf2, "%s", none+5);
6158c2ecf20Sopenharmony_ci			else {
6168c2ecf20Sopenharmony_ci				none--;
6178c2ecf20Sopenharmony_ci				*none = '\0';
6188c2ecf20Sopenharmony_ci				len = sprintf(buf2, "%s", buf1);
6198c2ecf20Sopenharmony_ci				none += 5;
6208c2ecf20Sopenharmony_ci				sprintf(buf2 + len, "%s", none);
6218c2ecf20Sopenharmony_ci			}
6228c2ecf20Sopenharmony_ci		} else {
6238c2ecf20Sopenharmony_ci			none--;
6248c2ecf20Sopenharmony_ci			*none = '\0';
6258c2ecf20Sopenharmony_ci			sprintf(buf2, "%s", buf1);
6268c2ecf20Sopenharmony_ci		}
6278c2ecf20Sopenharmony_ci		if (iscsi_update_param_value(param, buf2) < 0)
6288c2ecf20Sopenharmony_ci			return -EINVAL;
6298c2ecf20Sopenharmony_ci	} else {
6308c2ecf20Sopenharmony_ci		snprintf(buf1, sizeof(buf1), "%s", param->value);
6318c2ecf20Sopenharmony_ci		none = strstr(buf1, NONE);
6328c2ecf20Sopenharmony_ci		if (none)
6338c2ecf20Sopenharmony_ci			goto out;
6348c2ecf20Sopenharmony_ci		strlcat(buf1, "," NONE, sizeof(buf1));
6358c2ecf20Sopenharmony_ci		if (iscsi_update_param_value(param, buf1) < 0)
6368c2ecf20Sopenharmony_ci			return -EINVAL;
6378c2ecf20Sopenharmony_ci	}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ciout:
6408c2ecf20Sopenharmony_ci	a->authentication = authentication;
6418c2ecf20Sopenharmony_ci	pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
6428c2ecf20Sopenharmony_ci		a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	return 0;
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ciint iscsit_ta_login_timeout(
6488c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
6498c2ecf20Sopenharmony_ci	u32 login_timeout)
6508c2ecf20Sopenharmony_ci{
6518c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
6548c2ecf20Sopenharmony_ci		pr_err("Requested Login Timeout %u larger than maximum"
6558c2ecf20Sopenharmony_ci			" %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
6568c2ecf20Sopenharmony_ci		return -EINVAL;
6578c2ecf20Sopenharmony_ci	} else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
6588c2ecf20Sopenharmony_ci		pr_err("Requested Logout Timeout %u smaller than"
6598c2ecf20Sopenharmony_ci			" minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
6608c2ecf20Sopenharmony_ci		return -EINVAL;
6618c2ecf20Sopenharmony_ci	}
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	a->login_timeout = login_timeout;
6648c2ecf20Sopenharmony_ci	pr_debug("Set Logout Timeout to %u for Target Portal Group"
6658c2ecf20Sopenharmony_ci		" %hu\n", a->login_timeout, tpg->tpgt);
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	return 0;
6688c2ecf20Sopenharmony_ci}
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ciint iscsit_ta_netif_timeout(
6718c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
6728c2ecf20Sopenharmony_ci	u32 netif_timeout)
6738c2ecf20Sopenharmony_ci{
6748c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
6778c2ecf20Sopenharmony_ci		pr_err("Requested Network Interface Timeout %u larger"
6788c2ecf20Sopenharmony_ci			" than maximum %u\n", netif_timeout,
6798c2ecf20Sopenharmony_ci				TA_NETIF_TIMEOUT_MAX);
6808c2ecf20Sopenharmony_ci		return -EINVAL;
6818c2ecf20Sopenharmony_ci	} else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
6828c2ecf20Sopenharmony_ci		pr_err("Requested Network Interface Timeout %u smaller"
6838c2ecf20Sopenharmony_ci			" than minimum %u\n", netif_timeout,
6848c2ecf20Sopenharmony_ci				TA_NETIF_TIMEOUT_MIN);
6858c2ecf20Sopenharmony_ci		return -EINVAL;
6868c2ecf20Sopenharmony_ci	}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	a->netif_timeout = netif_timeout;
6898c2ecf20Sopenharmony_ci	pr_debug("Set Network Interface Timeout to %u for"
6908c2ecf20Sopenharmony_ci		" Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	return 0;
6938c2ecf20Sopenharmony_ci}
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ciint iscsit_ta_generate_node_acls(
6968c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
6978c2ecf20Sopenharmony_ci	u32 flag)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	if ((flag != 0) && (flag != 1)) {
7028c2ecf20Sopenharmony_ci		pr_err("Illegal value %d\n", flag);
7038c2ecf20Sopenharmony_ci		return -EINVAL;
7048c2ecf20Sopenharmony_ci	}
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	a->generate_node_acls = flag;
7078c2ecf20Sopenharmony_ci	pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
7088c2ecf20Sopenharmony_ci		tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	if (flag == 1 && a->cache_dynamic_acls == 0) {
7118c2ecf20Sopenharmony_ci		pr_debug("Explicitly setting cache_dynamic_acls=1 when "
7128c2ecf20Sopenharmony_ci			"generate_node_acls=1\n");
7138c2ecf20Sopenharmony_ci		a->cache_dynamic_acls = 1;
7148c2ecf20Sopenharmony_ci	}
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	return 0;
7178c2ecf20Sopenharmony_ci}
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ciint iscsit_ta_default_cmdsn_depth(
7208c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
7218c2ecf20Sopenharmony_ci	u32 tcq_depth)
7228c2ecf20Sopenharmony_ci{
7238c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
7268c2ecf20Sopenharmony_ci		pr_err("Requested Default Queue Depth: %u larger"
7278c2ecf20Sopenharmony_ci			" than maximum %u\n", tcq_depth,
7288c2ecf20Sopenharmony_ci				TA_DEFAULT_CMDSN_DEPTH_MAX);
7298c2ecf20Sopenharmony_ci		return -EINVAL;
7308c2ecf20Sopenharmony_ci	} else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
7318c2ecf20Sopenharmony_ci		pr_err("Requested Default Queue Depth: %u smaller"
7328c2ecf20Sopenharmony_ci			" than minimum %u\n", tcq_depth,
7338c2ecf20Sopenharmony_ci				TA_DEFAULT_CMDSN_DEPTH_MIN);
7348c2ecf20Sopenharmony_ci		return -EINVAL;
7358c2ecf20Sopenharmony_ci	}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	a->default_cmdsn_depth = tcq_depth;
7388c2ecf20Sopenharmony_ci	pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
7398c2ecf20Sopenharmony_ci		tpg->tpgt, a->default_cmdsn_depth);
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	return 0;
7428c2ecf20Sopenharmony_ci}
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ciint iscsit_ta_cache_dynamic_acls(
7458c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
7468c2ecf20Sopenharmony_ci	u32 flag)
7478c2ecf20Sopenharmony_ci{
7488c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	if ((flag != 0) && (flag != 1)) {
7518c2ecf20Sopenharmony_ci		pr_err("Illegal value %d\n", flag);
7528c2ecf20Sopenharmony_ci		return -EINVAL;
7538c2ecf20Sopenharmony_ci	}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	if (a->generate_node_acls == 1 && flag == 0) {
7568c2ecf20Sopenharmony_ci		pr_debug("Skipping cache_dynamic_acls=0 when"
7578c2ecf20Sopenharmony_ci			" generate_node_acls=1\n");
7588c2ecf20Sopenharmony_ci		return 0;
7598c2ecf20Sopenharmony_ci	}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	a->cache_dynamic_acls = flag;
7628c2ecf20Sopenharmony_ci	pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
7638c2ecf20Sopenharmony_ci		" ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
7648c2ecf20Sopenharmony_ci		"Enabled" : "Disabled");
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	return 0;
7678c2ecf20Sopenharmony_ci}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ciint iscsit_ta_demo_mode_write_protect(
7708c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
7718c2ecf20Sopenharmony_ci	u32 flag)
7728c2ecf20Sopenharmony_ci{
7738c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	if ((flag != 0) && (flag != 1)) {
7768c2ecf20Sopenharmony_ci		pr_err("Illegal value %d\n", flag);
7778c2ecf20Sopenharmony_ci		return -EINVAL;
7788c2ecf20Sopenharmony_ci	}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	a->demo_mode_write_protect = flag;
7818c2ecf20Sopenharmony_ci	pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
7828c2ecf20Sopenharmony_ci		tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	return 0;
7858c2ecf20Sopenharmony_ci}
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ciint iscsit_ta_prod_mode_write_protect(
7888c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
7898c2ecf20Sopenharmony_ci	u32 flag)
7908c2ecf20Sopenharmony_ci{
7918c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	if ((flag != 0) && (flag != 1)) {
7948c2ecf20Sopenharmony_ci		pr_err("Illegal value %d\n", flag);
7958c2ecf20Sopenharmony_ci		return -EINVAL;
7968c2ecf20Sopenharmony_ci	}
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	a->prod_mode_write_protect = flag;
7998c2ecf20Sopenharmony_ci	pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
8008c2ecf20Sopenharmony_ci		" %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
8018c2ecf20Sopenharmony_ci		"ON" : "OFF");
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	return 0;
8048c2ecf20Sopenharmony_ci}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ciint iscsit_ta_demo_mode_discovery(
8078c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
8088c2ecf20Sopenharmony_ci	u32 flag)
8098c2ecf20Sopenharmony_ci{
8108c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	if ((flag != 0) && (flag != 1)) {
8138c2ecf20Sopenharmony_ci		pr_err("Illegal value %d\n", flag);
8148c2ecf20Sopenharmony_ci		return -EINVAL;
8158c2ecf20Sopenharmony_ci	}
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	a->demo_mode_discovery = flag;
8188c2ecf20Sopenharmony_ci	pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
8198c2ecf20Sopenharmony_ci		" %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
8208c2ecf20Sopenharmony_ci		"ON" : "OFF");
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	return 0;
8238c2ecf20Sopenharmony_ci}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ciint iscsit_ta_default_erl(
8268c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
8278c2ecf20Sopenharmony_ci	u32 default_erl)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	if ((default_erl != 0) && (default_erl != 1) && (default_erl != 2)) {
8328c2ecf20Sopenharmony_ci		pr_err("Illegal value for default_erl: %u\n", default_erl);
8338c2ecf20Sopenharmony_ci		return -EINVAL;
8348c2ecf20Sopenharmony_ci	}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	a->default_erl = default_erl;
8378c2ecf20Sopenharmony_ci	pr_debug("iSCSI_TPG[%hu] - DefaultERL: %u\n", tpg->tpgt, a->default_erl);
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	return 0;
8408c2ecf20Sopenharmony_ci}
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ciint iscsit_ta_t10_pi(
8438c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
8448c2ecf20Sopenharmony_ci	u32 flag)
8458c2ecf20Sopenharmony_ci{
8468c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	if ((flag != 0) && (flag != 1)) {
8498c2ecf20Sopenharmony_ci		pr_err("Illegal value %d\n", flag);
8508c2ecf20Sopenharmony_ci		return -EINVAL;
8518c2ecf20Sopenharmony_ci	}
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	a->t10_pi = flag;
8548c2ecf20Sopenharmony_ci	pr_debug("iSCSI_TPG[%hu] - T10 Protection information bit:"
8558c2ecf20Sopenharmony_ci		" %s\n", tpg->tpgt, (a->t10_pi) ?
8568c2ecf20Sopenharmony_ci		"ON" : "OFF");
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	return 0;
8598c2ecf20Sopenharmony_ci}
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ciint iscsit_ta_fabric_prot_type(
8628c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
8638c2ecf20Sopenharmony_ci	u32 prot_type)
8648c2ecf20Sopenharmony_ci{
8658c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	if ((prot_type != 0) && (prot_type != 1) && (prot_type != 3)) {
8688c2ecf20Sopenharmony_ci		pr_err("Illegal value for fabric_prot_type: %u\n", prot_type);
8698c2ecf20Sopenharmony_ci		return -EINVAL;
8708c2ecf20Sopenharmony_ci	}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	a->fabric_prot_type = prot_type;
8738c2ecf20Sopenharmony_ci	pr_debug("iSCSI_TPG[%hu] - T10 Fabric Protection Type: %u\n",
8748c2ecf20Sopenharmony_ci		 tpg->tpgt, prot_type);
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	return 0;
8778c2ecf20Sopenharmony_ci}
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ciint iscsit_ta_tpg_enabled_sendtargets(
8808c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
8818c2ecf20Sopenharmony_ci	u32 flag)
8828c2ecf20Sopenharmony_ci{
8838c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	if ((flag != 0) && (flag != 1)) {
8868c2ecf20Sopenharmony_ci		pr_err("Illegal value %d\n", flag);
8878c2ecf20Sopenharmony_ci		return -EINVAL;
8888c2ecf20Sopenharmony_ci	}
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	a->tpg_enabled_sendtargets = flag;
8918c2ecf20Sopenharmony_ci	pr_debug("iSCSI_TPG[%hu] - TPG enabled bit required for SendTargets:"
8928c2ecf20Sopenharmony_ci		" %s\n", tpg->tpgt, (a->tpg_enabled_sendtargets) ? "ON" : "OFF");
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	return 0;
8958c2ecf20Sopenharmony_ci}
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ciint iscsit_ta_login_keys_workaround(
8988c2ecf20Sopenharmony_ci	struct iscsi_portal_group *tpg,
8998c2ecf20Sopenharmony_ci	u32 flag)
9008c2ecf20Sopenharmony_ci{
9018c2ecf20Sopenharmony_ci	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	if ((flag != 0) && (flag != 1)) {
9048c2ecf20Sopenharmony_ci		pr_err("Illegal value %d\n", flag);
9058c2ecf20Sopenharmony_ci		return -EINVAL;
9068c2ecf20Sopenharmony_ci	}
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	a->login_keys_workaround = flag;
9098c2ecf20Sopenharmony_ci	pr_debug("iSCSI_TPG[%hu] - TPG enabled bit for login keys workaround: %s ",
9108c2ecf20Sopenharmony_ci		tpg->tpgt, (a->login_keys_workaround) ? "ON" : "OFF");
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	return 0;
9138c2ecf20Sopenharmony_ci}
914