1e48354ceSNicholas Bellinger /*******************************************************************************
2e48354ceSNicholas Bellinger  * This file contains the configfs implementation for iSCSI Target mode
3e48354ceSNicholas Bellinger  * from the LIO-Target Project.
4e48354ceSNicholas Bellinger  *
54c76251eSNicholas Bellinger  * (c) Copyright 2007-2013 Datera, Inc.
6e48354ceSNicholas Bellinger  *
7e48354ceSNicholas Bellinger  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8e48354ceSNicholas Bellinger  *
9e48354ceSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
10e48354ceSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
11e48354ceSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
12e48354ceSNicholas Bellinger  * (at your option) any later version.
13e48354ceSNicholas Bellinger  *
14e48354ceSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
15e48354ceSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16e48354ceSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17e48354ceSNicholas Bellinger  * GNU General Public License for more details.
18e48354ceSNicholas Bellinger  ****************************************************************************/
19e48354ceSNicholas Bellinger 
20e48354ceSNicholas Bellinger #include <linux/configfs.h>
21ad7babd2SJörn Engel #include <linux/ctype.h>
22c53181afSPaul Gortmaker #include <linux/export.h>
23c3bc93daSStephen Rothwell #include <linux/inet.h>
24e48354ceSNicholas Bellinger #include <target/target_core_base.h>
25c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
262ec5a8c1SNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
27e48354ceSNicholas Bellinger 
2867f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h>
29e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
30e48354ceSNicholas Bellinger #include "iscsi_target_device.h"
31e48354ceSNicholas Bellinger #include "iscsi_target_erl0.h"
32e48354ceSNicholas Bellinger #include "iscsi_target_nodeattrib.h"
33e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
34e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
35e48354ceSNicholas Bellinger #include "iscsi_target.h"
3667f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_stat.h>
37e48354ceSNicholas Bellinger 
38e48354ceSNicholas Bellinger 
39e48354ceSNicholas Bellinger /* Start items for lio_target_portal_cit */
40e48354ceSNicholas Bellinger 
412eafd729SChristoph Hellwig static inline struct iscsi_tpg_np *to_iscsi_tpg_np(struct config_item *item)
42e48354ceSNicholas Bellinger {
432eafd729SChristoph Hellwig 	return container_of(to_tpg_np(item), struct iscsi_tpg_np, se_tpg_np);
442eafd729SChristoph Hellwig }
452eafd729SChristoph Hellwig 
462eafd729SChristoph Hellwig static ssize_t lio_target_np_sctp_show(struct config_item *item, char *page)
472eafd729SChristoph Hellwig {
482eafd729SChristoph Hellwig 	struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
49e48354ceSNicholas Bellinger 	struct iscsi_tpg_np *tpg_np_sctp;
50e48354ceSNicholas Bellinger 	ssize_t rb;
51e48354ceSNicholas Bellinger 
52e48354ceSNicholas Bellinger 	tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
53e48354ceSNicholas Bellinger 	if (tpg_np_sctp)
54e48354ceSNicholas Bellinger 		rb = sprintf(page, "1\n");
55e48354ceSNicholas Bellinger 	else
56e48354ceSNicholas Bellinger 		rb = sprintf(page, "0\n");
57e48354ceSNicholas Bellinger 
58e48354ceSNicholas Bellinger 	return rb;
59e48354ceSNicholas Bellinger }
60e48354ceSNicholas Bellinger 
612eafd729SChristoph Hellwig static ssize_t lio_target_np_sctp_store(struct config_item *item,
622eafd729SChristoph Hellwig 		const char *page, size_t count)
63e48354ceSNicholas Bellinger {
642eafd729SChristoph Hellwig 	struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
65e48354ceSNicholas Bellinger 	struct iscsi_np *np;
66e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg;
67e48354ceSNicholas Bellinger 	struct iscsi_tpg_np *tpg_np_sctp = NULL;
68e48354ceSNicholas Bellinger 	u32 op;
69e48354ceSNicholas Bellinger 	int ret;
70e48354ceSNicholas Bellinger 
71ad7babd2SJörn Engel 	ret = kstrtou32(page, 0, &op);
72ad7babd2SJörn Engel 	if (ret)
73ad7babd2SJörn Engel 		return ret;
74e48354ceSNicholas Bellinger 	if ((op != 1) && (op != 0)) {
75e48354ceSNicholas Bellinger 		pr_err("Illegal value for tpg_enable: %u\n", op);
76e48354ceSNicholas Bellinger 		return -EINVAL;
77e48354ceSNicholas Bellinger 	}
78e48354ceSNicholas Bellinger 	np = tpg_np->tpg_np;
79e48354ceSNicholas Bellinger 	if (!np) {
80e48354ceSNicholas Bellinger 		pr_err("Unable to locate struct iscsi_np from"
81e48354ceSNicholas Bellinger 				" struct iscsi_tpg_np\n");
82e48354ceSNicholas Bellinger 		return -EINVAL;
83e48354ceSNicholas Bellinger 	}
84e48354ceSNicholas Bellinger 
85e48354ceSNicholas Bellinger 	tpg = tpg_np->tpg;
86e48354ceSNicholas Bellinger 	if (iscsit_get_tpg(tpg) < 0)
87e48354ceSNicholas Bellinger 		return -EINVAL;
88e48354ceSNicholas Bellinger 
89e48354ceSNicholas Bellinger 	if (op) {
90e48354ceSNicholas Bellinger 		/*
91e48354ceSNicholas Bellinger 		 * Use existing np->np_sockaddr for SCTP network portal reference
92e48354ceSNicholas Bellinger 		 */
93e48354ceSNicholas Bellinger 		tpg_np_sctp = iscsit_tpg_add_network_portal(tpg, &np->np_sockaddr,
9476c28f1fSAndy Grover 					tpg_np, ISCSI_SCTP_TCP);
95e48354ceSNicholas Bellinger 		if (!tpg_np_sctp || IS_ERR(tpg_np_sctp))
96e48354ceSNicholas Bellinger 			goto out;
97e48354ceSNicholas Bellinger 	} else {
98e48354ceSNicholas Bellinger 		tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
99e48354ceSNicholas Bellinger 		if (!tpg_np_sctp)
100e48354ceSNicholas Bellinger 			goto out;
101e48354ceSNicholas Bellinger 
102e48354ceSNicholas Bellinger 		ret = iscsit_tpg_del_network_portal(tpg, tpg_np_sctp);
103e48354ceSNicholas Bellinger 		if (ret < 0)
104e48354ceSNicholas Bellinger 			goto out;
105e48354ceSNicholas Bellinger 	}
106e48354ceSNicholas Bellinger 
107e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);
108e48354ceSNicholas Bellinger 	return count;
109e48354ceSNicholas Bellinger out:
110e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);
111e48354ceSNicholas Bellinger 	return -EINVAL;
112e48354ceSNicholas Bellinger }
113e48354ceSNicholas Bellinger 
1142eafd729SChristoph Hellwig static ssize_t lio_target_np_iser_show(struct config_item *item, char *page)
11572438cddSNicholas Bellinger {
1162eafd729SChristoph Hellwig 	struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
11772438cddSNicholas Bellinger 	struct iscsi_tpg_np *tpg_np_iser;
11872438cddSNicholas Bellinger 	ssize_t rb;
11972438cddSNicholas Bellinger 
12072438cddSNicholas Bellinger 	tpg_np_iser = iscsit_tpg_locate_child_np(tpg_np, ISCSI_INFINIBAND);
12172438cddSNicholas Bellinger 	if (tpg_np_iser)
12272438cddSNicholas Bellinger 		rb = sprintf(page, "1\n");
12372438cddSNicholas Bellinger 	else
12472438cddSNicholas Bellinger 		rb = sprintf(page, "0\n");
12572438cddSNicholas Bellinger 
12672438cddSNicholas Bellinger 	return rb;
12772438cddSNicholas Bellinger }
12872438cddSNicholas Bellinger 
1292eafd729SChristoph Hellwig static ssize_t lio_target_np_iser_store(struct config_item *item,
1302eafd729SChristoph Hellwig 		const char *page, size_t count)
13172438cddSNicholas Bellinger {
1322eafd729SChristoph Hellwig 	struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
13372438cddSNicholas Bellinger 	struct iscsi_np *np;
13472438cddSNicholas Bellinger 	struct iscsi_portal_group *tpg;
13572438cddSNicholas Bellinger 	struct iscsi_tpg_np *tpg_np_iser = NULL;
13672438cddSNicholas Bellinger 	char *endptr;
13772438cddSNicholas Bellinger 	u32 op;
13858bd0c69SAndy Grover 	int rc = 0;
13972438cddSNicholas Bellinger 
14072438cddSNicholas Bellinger 	op = simple_strtoul(page, &endptr, 0);
14172438cddSNicholas Bellinger 	if ((op != 1) && (op != 0)) {
14272438cddSNicholas Bellinger 		pr_err("Illegal value for tpg_enable: %u\n", op);
14372438cddSNicholas Bellinger 		return -EINVAL;
14472438cddSNicholas Bellinger 	}
14572438cddSNicholas Bellinger 	np = tpg_np->tpg_np;
14672438cddSNicholas Bellinger 	if (!np) {
14772438cddSNicholas Bellinger 		pr_err("Unable to locate struct iscsi_np from"
14872438cddSNicholas Bellinger 				" struct iscsi_tpg_np\n");
14972438cddSNicholas Bellinger 		return -EINVAL;
15072438cddSNicholas Bellinger 	}
15172438cddSNicholas Bellinger 
15272438cddSNicholas Bellinger 	tpg = tpg_np->tpg;
15372438cddSNicholas Bellinger 	if (iscsit_get_tpg(tpg) < 0)
15472438cddSNicholas Bellinger 		return -EINVAL;
15572438cddSNicholas Bellinger 
15672438cddSNicholas Bellinger 	if (op) {
15758bd0c69SAndy Grover 		rc = request_module("ib_isert");
15858bd0c69SAndy Grover 		if (rc != 0) {
15972438cddSNicholas Bellinger 			pr_warn("Unable to request_module for ib_isert\n");
16058bd0c69SAndy Grover 			rc = 0;
16158bd0c69SAndy Grover 		}
16272438cddSNicholas Bellinger 
16372438cddSNicholas Bellinger 		tpg_np_iser = iscsit_tpg_add_network_portal(tpg, &np->np_sockaddr,
16476c28f1fSAndy Grover 				tpg_np, ISCSI_INFINIBAND);
16558bd0c69SAndy Grover 		if (IS_ERR(tpg_np_iser)) {
16658bd0c69SAndy Grover 			rc = PTR_ERR(tpg_np_iser);
16772438cddSNicholas Bellinger 			goto out;
16858bd0c69SAndy Grover 		}
16972438cddSNicholas Bellinger 	} else {
17072438cddSNicholas Bellinger 		tpg_np_iser = iscsit_tpg_locate_child_np(tpg_np, ISCSI_INFINIBAND);
17158bd0c69SAndy Grover 		if (tpg_np_iser) {
17272438cddSNicholas Bellinger 			rc = iscsit_tpg_del_network_portal(tpg, tpg_np_iser);
17372438cddSNicholas Bellinger 			if (rc < 0)
17472438cddSNicholas Bellinger 				goto out;
17572438cddSNicholas Bellinger 		}
17658bd0c69SAndy Grover 	}
17772438cddSNicholas Bellinger 
17872438cddSNicholas Bellinger 	iscsit_put_tpg(tpg);
17972438cddSNicholas Bellinger 	return count;
18072438cddSNicholas Bellinger out:
18172438cddSNicholas Bellinger 	iscsit_put_tpg(tpg);
18258bd0c69SAndy Grover 	return rc;
18372438cddSNicholas Bellinger }
18472438cddSNicholas Bellinger 
1852eafd729SChristoph Hellwig CONFIGFS_ATTR(lio_target_np_, sctp);
1862eafd729SChristoph Hellwig CONFIGFS_ATTR(lio_target_np_, iser);
18772438cddSNicholas Bellinger 
188e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_portal_attrs[] = {
1892eafd729SChristoph Hellwig 	&lio_target_np_attr_sctp,
1902eafd729SChristoph Hellwig 	&lio_target_np_attr_iser,
191e48354ceSNicholas Bellinger 	NULL,
192e48354ceSNicholas Bellinger };
193e48354ceSNicholas Bellinger 
194e48354ceSNicholas Bellinger /* Stop items for lio_target_portal_cit */
195e48354ceSNicholas Bellinger 
196e48354ceSNicholas Bellinger /* Start items for lio_target_np_cit */
197e48354ceSNicholas Bellinger 
198e48354ceSNicholas Bellinger #define MAX_PORTAL_LEN		256
199e48354ceSNicholas Bellinger 
200fceb5bc7SChristoph Hellwig static struct se_tpg_np *lio_target_call_addnptotpg(
201e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg,
202e48354ceSNicholas Bellinger 	struct config_group *group,
203e48354ceSNicholas Bellinger 	const char *name)
204e48354ceSNicholas Bellinger {
205e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg;
206e48354ceSNicholas Bellinger 	struct iscsi_tpg_np *tpg_np;
207e48354ceSNicholas Bellinger 	char *str, *str2, *ip_str, *port_str;
20813a3cf08SAndy Grover 	struct sockaddr_storage sockaddr;
209e48354ceSNicholas Bellinger 	struct sockaddr_in *sock_in;
210e48354ceSNicholas Bellinger 	struct sockaddr_in6 *sock_in6;
211e48354ceSNicholas Bellinger 	unsigned long port;
212e48354ceSNicholas Bellinger 	int ret;
213e48354ceSNicholas Bellinger 	char buf[MAX_PORTAL_LEN + 1];
214e48354ceSNicholas Bellinger 
215e48354ceSNicholas Bellinger 	if (strlen(name) > MAX_PORTAL_LEN) {
216e48354ceSNicholas Bellinger 		pr_err("strlen(name): %d exceeds MAX_PORTAL_LEN: %d\n",
217e48354ceSNicholas Bellinger 			(int)strlen(name), MAX_PORTAL_LEN);
218e48354ceSNicholas Bellinger 		return ERR_PTR(-EOVERFLOW);
219e48354ceSNicholas Bellinger 	}
220e48354ceSNicholas Bellinger 	memset(buf, 0, MAX_PORTAL_LEN + 1);
2217bbb654eSNicholas Bellinger 	snprintf(buf, MAX_PORTAL_LEN + 1, "%s", name);
222e48354ceSNicholas Bellinger 
22313a3cf08SAndy Grover 	memset(&sockaddr, 0, sizeof(struct sockaddr_storage));
224e48354ceSNicholas Bellinger 
225e48354ceSNicholas Bellinger 	str = strstr(buf, "[");
226e48354ceSNicholas Bellinger 	if (str) {
227e48354ceSNicholas Bellinger 		const char *end;
228e48354ceSNicholas Bellinger 
229e48354ceSNicholas Bellinger 		str2 = strstr(str, "]");
230e48354ceSNicholas Bellinger 		if (!str2) {
231e48354ceSNicholas Bellinger 			pr_err("Unable to locate trailing \"]\""
232e48354ceSNicholas Bellinger 				" in IPv6 iSCSI network portal address\n");
233e48354ceSNicholas Bellinger 			return ERR_PTR(-EINVAL);
234e48354ceSNicholas Bellinger 		}
235e48354ceSNicholas Bellinger 		str++; /* Skip over leading "[" */
23676c28f1fSAndy Grover 		*str2 = '\0'; /* Terminate the unbracketed IPv6 address */
23776c28f1fSAndy Grover 		str2++; /* Skip over the \0 */
238e48354ceSNicholas Bellinger 		port_str = strstr(str2, ":");
239e48354ceSNicholas Bellinger 		if (!port_str) {
240e48354ceSNicholas Bellinger 			pr_err("Unable to locate \":port\""
241e48354ceSNicholas Bellinger 				" in IPv6 iSCSI network portal address\n");
242e48354ceSNicholas Bellinger 			return ERR_PTR(-EINVAL);
243e48354ceSNicholas Bellinger 		}
244e48354ceSNicholas Bellinger 		*port_str = '\0'; /* Terminate string for IP */
245e48354ceSNicholas Bellinger 		port_str++; /* Skip over ":" */
246e48354ceSNicholas Bellinger 
24757103d7fSJingoo Han 		ret = kstrtoul(port_str, 0, &port);
248e48354ceSNicholas Bellinger 		if (ret < 0) {
24957103d7fSJingoo Han 			pr_err("kstrtoul() failed for port_str: %d\n", ret);
250e48354ceSNicholas Bellinger 			return ERR_PTR(ret);
251e48354ceSNicholas Bellinger 		}
252e48354ceSNicholas Bellinger 		sock_in6 = (struct sockaddr_in6 *)&sockaddr;
253e48354ceSNicholas Bellinger 		sock_in6->sin6_family = AF_INET6;
254e48354ceSNicholas Bellinger 		sock_in6->sin6_port = htons((unsigned short)port);
255dc58f760SAndy Grover 		ret = in6_pton(str, -1,
256e48354ceSNicholas Bellinger 				(void *)&sock_in6->sin6_addr.in6_u, -1, &end);
257e48354ceSNicholas Bellinger 		if (ret <= 0) {
258e48354ceSNicholas Bellinger 			pr_err("in6_pton returned: %d\n", ret);
259e48354ceSNicholas Bellinger 			return ERR_PTR(-EINVAL);
260e48354ceSNicholas Bellinger 		}
261e48354ceSNicholas Bellinger 	} else {
262e48354ceSNicholas Bellinger 		str = ip_str = &buf[0];
263e48354ceSNicholas Bellinger 		port_str = strstr(ip_str, ":");
264e48354ceSNicholas Bellinger 		if (!port_str) {
265e48354ceSNicholas Bellinger 			pr_err("Unable to locate \":port\""
266e48354ceSNicholas Bellinger 				" in IPv4 iSCSI network portal address\n");
267e48354ceSNicholas Bellinger 			return ERR_PTR(-EINVAL);
268e48354ceSNicholas Bellinger 		}
269e48354ceSNicholas Bellinger 		*port_str = '\0'; /* Terminate string for IP */
270e48354ceSNicholas Bellinger 		port_str++; /* Skip over ":" */
271e48354ceSNicholas Bellinger 
27257103d7fSJingoo Han 		ret = kstrtoul(port_str, 0, &port);
273e48354ceSNicholas Bellinger 		if (ret < 0) {
27457103d7fSJingoo Han 			pr_err("kstrtoul() failed for port_str: %d\n", ret);
275e48354ceSNicholas Bellinger 			return ERR_PTR(ret);
276e48354ceSNicholas Bellinger 		}
277e48354ceSNicholas Bellinger 		sock_in = (struct sockaddr_in *)&sockaddr;
278e48354ceSNicholas Bellinger 		sock_in->sin_family = AF_INET;
279e48354ceSNicholas Bellinger 		sock_in->sin_port = htons((unsigned short)port);
280e48354ceSNicholas Bellinger 		sock_in->sin_addr.s_addr = in_aton(ip_str);
281e48354ceSNicholas Bellinger 	}
282e48354ceSNicholas Bellinger 	tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
283e48354ceSNicholas Bellinger 	ret = iscsit_get_tpg(tpg);
284e48354ceSNicholas Bellinger 	if (ret < 0)
285e48354ceSNicholas Bellinger 		return ERR_PTR(-EINVAL);
286e48354ceSNicholas Bellinger 
287e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
288e48354ceSNicholas Bellinger 		" PORTAL: %s\n",
289e48354ceSNicholas Bellinger 		config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
290e48354ceSNicholas Bellinger 		tpg->tpgt, name);
291e48354ceSNicholas Bellinger 	/*
292e48354ceSNicholas Bellinger 	 * Assume ISCSI_TCP by default.  Other network portals for other
293e48354ceSNicholas Bellinger 	 * iSCSI fabrics:
294e48354ceSNicholas Bellinger 	 *
295e48354ceSNicholas Bellinger 	 * Traditional iSCSI over SCTP (initial support)
296e48354ceSNicholas Bellinger 	 * iSER/TCP (TODO, hardware available)
297e48354ceSNicholas Bellinger 	 * iSER/SCTP (TODO, software emulation with osc-iwarp)
298e48354ceSNicholas Bellinger 	 * iSER/IB (TODO, hardware available)
299e48354ceSNicholas Bellinger 	 *
30020879696SMasanari Iida 	 * can be enabled with attributes under
301e48354ceSNicholas Bellinger 	 * sys/kernel/config/iscsi/$IQN/$TPG/np/$IP:$PORT/
302e48354ceSNicholas Bellinger 	 *
303e48354ceSNicholas Bellinger 	 */
30476c28f1fSAndy Grover 	tpg_np = iscsit_tpg_add_network_portal(tpg, &sockaddr, NULL,
305e48354ceSNicholas Bellinger 				ISCSI_TCP);
306e48354ceSNicholas Bellinger 	if (IS_ERR(tpg_np)) {
307e48354ceSNicholas Bellinger 		iscsit_put_tpg(tpg);
308e1750ba2SThomas Meyer 		return ERR_CAST(tpg_np);
309e48354ceSNicholas Bellinger 	}
310e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
311e48354ceSNicholas Bellinger 
312e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);
313e48354ceSNicholas Bellinger 	return &tpg_np->se_tpg_np;
314e48354ceSNicholas Bellinger }
315e48354ceSNicholas Bellinger 
316e48354ceSNicholas Bellinger static void lio_target_call_delnpfromtpg(
317e48354ceSNicholas Bellinger 	struct se_tpg_np *se_tpg_np)
318e48354ceSNicholas Bellinger {
319e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg;
320e48354ceSNicholas Bellinger 	struct iscsi_tpg_np *tpg_np;
321e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg;
322e48354ceSNicholas Bellinger 	int ret;
323e48354ceSNicholas Bellinger 
324e48354ceSNicholas Bellinger 	tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
325e48354ceSNicholas Bellinger 	tpg = tpg_np->tpg;
326e48354ceSNicholas Bellinger 	ret = iscsit_get_tpg(tpg);
327e48354ceSNicholas Bellinger 	if (ret < 0)
328e48354ceSNicholas Bellinger 		return;
329e48354ceSNicholas Bellinger 
330e48354ceSNicholas Bellinger 	se_tpg = &tpg->tpg_se_tpg;
331e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s TPGT: %hu"
33269d75574SAndy Grover 		" PORTAL: %pISpc\n", config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
33369d75574SAndy Grover 		tpg->tpgt, &tpg_np->tpg_np->np_sockaddr);
334e48354ceSNicholas Bellinger 
335e48354ceSNicholas Bellinger 	ret = iscsit_tpg_del_network_portal(tpg, tpg_np);
336e48354ceSNicholas Bellinger 	if (ret < 0)
337e48354ceSNicholas Bellinger 		goto out;
338e48354ceSNicholas Bellinger 
339e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
340e48354ceSNicholas Bellinger out:
341e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);
342e48354ceSNicholas Bellinger }
343e48354ceSNicholas Bellinger 
344e48354ceSNicholas Bellinger /* End items for lio_target_np_cit */
345e48354ceSNicholas Bellinger 
346e48354ceSNicholas Bellinger /* Start items for lio_target_nacl_attrib_cit */
347e48354ceSNicholas Bellinger 
3482eafd729SChristoph Hellwig #define ISCSI_NACL_ATTR(name)						\
3492eafd729SChristoph Hellwig static ssize_t iscsi_nacl_attrib_##name##_show(struct config_item *item,\
350e48354ceSNicholas Bellinger 		char *page)						\
351e48354ceSNicholas Bellinger {									\
3522eafd729SChristoph Hellwig 	struct se_node_acl *se_nacl = attrib_to_nacl(item);		\
353e48354ceSNicholas Bellinger 	struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
354e48354ceSNicholas Bellinger 					se_node_acl);			\
355e48354ceSNicholas Bellinger 									\
356b7eec2cdSAndy Grover 	return sprintf(page, "%u\n", nacl->node_attrib.name);		\
357e48354ceSNicholas Bellinger }									\
358e48354ceSNicholas Bellinger 									\
3592eafd729SChristoph Hellwig static ssize_t iscsi_nacl_attrib_##name##_store(struct config_item *item,\
3602eafd729SChristoph Hellwig 		const char *page, size_t count)				\
361e48354ceSNicholas Bellinger {									\
3622eafd729SChristoph Hellwig 	struct se_node_acl *se_nacl = attrib_to_nacl(item);		\
363e48354ceSNicholas Bellinger 	struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
364e48354ceSNicholas Bellinger 					se_node_acl);			\
365e48354ceSNicholas Bellinger 	u32 val;							\
366e48354ceSNicholas Bellinger 	int ret;							\
367e48354ceSNicholas Bellinger 									\
368ad7babd2SJörn Engel 	ret = kstrtou32(page, 0, &val);					\
369ad7babd2SJörn Engel 	if (ret)							\
370ad7babd2SJörn Engel 		return ret;						\
371e48354ceSNicholas Bellinger 	ret = iscsit_na_##name(nacl, val);				\
372e48354ceSNicholas Bellinger 	if (ret < 0)							\
373e48354ceSNicholas Bellinger 		return ret;						\
374e48354ceSNicholas Bellinger 									\
375e48354ceSNicholas Bellinger 	return count;							\
3762eafd729SChristoph Hellwig }									\
3772eafd729SChristoph Hellwig 									\
3782eafd729SChristoph Hellwig CONFIGFS_ATTR(iscsi_nacl_attrib_, name)
379e48354ceSNicholas Bellinger 
3802eafd729SChristoph Hellwig ISCSI_NACL_ATTR(dataout_timeout);
3812eafd729SChristoph Hellwig ISCSI_NACL_ATTR(dataout_timeout_retries);
3822eafd729SChristoph Hellwig ISCSI_NACL_ATTR(default_erl);
3832eafd729SChristoph Hellwig ISCSI_NACL_ATTR(nopin_timeout);
3842eafd729SChristoph Hellwig ISCSI_NACL_ATTR(nopin_response_timeout);
3852eafd729SChristoph Hellwig ISCSI_NACL_ATTR(random_datain_pdu_offsets);
3862eafd729SChristoph Hellwig ISCSI_NACL_ATTR(random_datain_seq_offsets);
3872eafd729SChristoph Hellwig ISCSI_NACL_ATTR(random_r2t_offsets);
388e48354ceSNicholas Bellinger 
389e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
3902eafd729SChristoph Hellwig 	&iscsi_nacl_attrib_attr_dataout_timeout,
3912eafd729SChristoph Hellwig 	&iscsi_nacl_attrib_attr_dataout_timeout_retries,
3922eafd729SChristoph Hellwig 	&iscsi_nacl_attrib_attr_default_erl,
3932eafd729SChristoph Hellwig 	&iscsi_nacl_attrib_attr_nopin_timeout,
3942eafd729SChristoph Hellwig 	&iscsi_nacl_attrib_attr_nopin_response_timeout,
3952eafd729SChristoph Hellwig 	&iscsi_nacl_attrib_attr_random_datain_pdu_offsets,
3962eafd729SChristoph Hellwig 	&iscsi_nacl_attrib_attr_random_datain_seq_offsets,
3972eafd729SChristoph Hellwig 	&iscsi_nacl_attrib_attr_random_r2t_offsets,
398e48354ceSNicholas Bellinger 	NULL,
399e48354ceSNicholas Bellinger };
400e48354ceSNicholas Bellinger 
401e48354ceSNicholas Bellinger /* End items for lio_target_nacl_attrib_cit */
402e48354ceSNicholas Bellinger 
403e48354ceSNicholas Bellinger /* Start items for lio_target_nacl_auth_cit */
404e48354ceSNicholas Bellinger 
405e48354ceSNicholas Bellinger #define __DEF_NACL_AUTH_STR(prefix, name, flags)			\
4062eafd729SChristoph Hellwig static ssize_t __iscsi_##prefix##_##name##_show(			\
407e48354ceSNicholas Bellinger 	struct iscsi_node_acl *nacl,					\
408e48354ceSNicholas Bellinger 	char *page)							\
409e48354ceSNicholas Bellinger {									\
410e48354ceSNicholas Bellinger 	struct iscsi_node_auth *auth = &nacl->node_auth;		\
411e48354ceSNicholas Bellinger 									\
412e48354ceSNicholas Bellinger 	if (!capable(CAP_SYS_ADMIN))					\
413e48354ceSNicholas Bellinger 		return -EPERM;						\
414e48354ceSNicholas Bellinger 	return snprintf(page, PAGE_SIZE, "%s\n", auth->name);		\
415e48354ceSNicholas Bellinger }									\
416e48354ceSNicholas Bellinger 									\
4172eafd729SChristoph Hellwig static ssize_t __iscsi_##prefix##_##name##_store(			\
418e48354ceSNicholas Bellinger 	struct iscsi_node_acl *nacl,					\
419e48354ceSNicholas Bellinger 	const char *page,						\
420e48354ceSNicholas Bellinger 	size_t count)							\
421e48354ceSNicholas Bellinger {									\
422e48354ceSNicholas Bellinger 	struct iscsi_node_auth *auth = &nacl->node_auth;		\
423e48354ceSNicholas Bellinger 									\
424e48354ceSNicholas Bellinger 	if (!capable(CAP_SYS_ADMIN))					\
425e48354ceSNicholas Bellinger 		return -EPERM;						\
4262306bfb2SEric Seppanen 	if (count >= sizeof(auth->name))				\
4272306bfb2SEric Seppanen 		return -EINVAL;						\
4280fbfc46fSJörn Engel 	snprintf(auth->name, sizeof(auth->name), "%s", page);		\
429e48354ceSNicholas Bellinger 	if (!strncmp("NULL", auth->name, 4))				\
430e48354ceSNicholas Bellinger 		auth->naf_flags &= ~flags;				\
431e48354ceSNicholas Bellinger 	else								\
432e48354ceSNicholas Bellinger 		auth->naf_flags |= flags;				\
433e48354ceSNicholas Bellinger 									\
434e48354ceSNicholas Bellinger 	if ((auth->naf_flags & NAF_USERID_IN_SET) &&			\
435e48354ceSNicholas Bellinger 	    (auth->naf_flags & NAF_PASSWORD_IN_SET))			\
436e48354ceSNicholas Bellinger 		auth->authenticate_target = 1;				\
437e48354ceSNicholas Bellinger 	else								\
438e48354ceSNicholas Bellinger 		auth->authenticate_target = 0;				\
439e48354ceSNicholas Bellinger 									\
440e48354ceSNicholas Bellinger 	return count;							\
441e48354ceSNicholas Bellinger }
442e48354ceSNicholas Bellinger 
4432eafd729SChristoph Hellwig #define DEF_NACL_AUTH_STR(name, flags)					\
4442eafd729SChristoph Hellwig 	__DEF_NACL_AUTH_STR(nacl_auth, name, flags)			\
4452eafd729SChristoph Hellwig static ssize_t iscsi_nacl_auth_##name##_show(struct config_item *item,	\
4462eafd729SChristoph Hellwig 		char *page)						\
4472eafd729SChristoph Hellwig {									\
4482eafd729SChristoph Hellwig 	struct se_node_acl *nacl = auth_to_nacl(item);			\
4492eafd729SChristoph Hellwig 	return __iscsi_nacl_auth_##name##_show(container_of(nacl,	\
4502eafd729SChristoph Hellwig 			struct iscsi_node_acl, se_node_acl), page);	\
4512eafd729SChristoph Hellwig }									\
4522eafd729SChristoph Hellwig static ssize_t iscsi_nacl_auth_##name##_store(struct config_item *item,	\
4532eafd729SChristoph Hellwig 		const char *page, size_t count)				\
4542eafd729SChristoph Hellwig {									\
4552eafd729SChristoph Hellwig 	struct se_node_acl *nacl = auth_to_nacl(item);			\
4562eafd729SChristoph Hellwig 	return __iscsi_nacl_auth_##name##_store(container_of(nacl,	\
4572eafd729SChristoph Hellwig 			struct iscsi_node_acl, se_node_acl), page, count); \
4582eafd729SChristoph Hellwig }									\
4592eafd729SChristoph Hellwig 									\
4602eafd729SChristoph Hellwig CONFIGFS_ATTR(iscsi_nacl_auth_, name)
4612eafd729SChristoph Hellwig 
4622eafd729SChristoph Hellwig /*
4632eafd729SChristoph Hellwig  * One-way authentication userid
4642eafd729SChristoph Hellwig  */
4652eafd729SChristoph Hellwig DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
4662eafd729SChristoph Hellwig DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
4672eafd729SChristoph Hellwig DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
4682eafd729SChristoph Hellwig DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
4692eafd729SChristoph Hellwig 
470e48354ceSNicholas Bellinger #define __DEF_NACL_AUTH_INT(prefix, name)				\
4712eafd729SChristoph Hellwig static ssize_t __iscsi_##prefix##_##name##_show(				\
472e48354ceSNicholas Bellinger 	struct iscsi_node_acl *nacl,					\
473e48354ceSNicholas Bellinger 	char *page)							\
474e48354ceSNicholas Bellinger {									\
475e48354ceSNicholas Bellinger 	struct iscsi_node_auth *auth = &nacl->node_auth;		\
476e48354ceSNicholas Bellinger 									\
477e48354ceSNicholas Bellinger 	if (!capable(CAP_SYS_ADMIN))					\
478e48354ceSNicholas Bellinger 		return -EPERM;						\
479e48354ceSNicholas Bellinger 									\
480e48354ceSNicholas Bellinger 	return snprintf(page, PAGE_SIZE, "%d\n", auth->name);		\
481e48354ceSNicholas Bellinger }
482e48354ceSNicholas Bellinger 
483e48354ceSNicholas Bellinger #define DEF_NACL_AUTH_INT(name)						\
484e48354ceSNicholas Bellinger 	__DEF_NACL_AUTH_INT(nacl_auth, name)				\
4852eafd729SChristoph Hellwig static ssize_t iscsi_nacl_auth_##name##_show(struct config_item *item,	\
486e48354ceSNicholas Bellinger 		char *page)						\
487e48354ceSNicholas Bellinger {									\
4882eafd729SChristoph Hellwig 	struct se_node_acl *nacl = auth_to_nacl(item);			\
4892eafd729SChristoph Hellwig 	return __iscsi_nacl_auth_##name##_show(container_of(nacl,	\
490e48354ceSNicholas Bellinger 			struct iscsi_node_acl, se_node_acl), page);	\
4912eafd729SChristoph Hellwig }									\
4922eafd729SChristoph Hellwig 									\
4932eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(iscsi_nacl_auth_, name)
494e48354ceSNicholas Bellinger 
495e48354ceSNicholas Bellinger DEF_NACL_AUTH_INT(authenticate_target);
496e48354ceSNicholas Bellinger 
497e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
4982eafd729SChristoph Hellwig 	&iscsi_nacl_auth_attr_userid,
4992eafd729SChristoph Hellwig 	&iscsi_nacl_auth_attr_password,
5002eafd729SChristoph Hellwig 	&iscsi_nacl_auth_attr_authenticate_target,
5012eafd729SChristoph Hellwig 	&iscsi_nacl_auth_attr_userid_mutual,
5022eafd729SChristoph Hellwig 	&iscsi_nacl_auth_attr_password_mutual,
503e48354ceSNicholas Bellinger 	NULL,
504e48354ceSNicholas Bellinger };
505e48354ceSNicholas Bellinger 
506e48354ceSNicholas Bellinger /* End items for lio_target_nacl_auth_cit */
507e48354ceSNicholas Bellinger 
508e48354ceSNicholas Bellinger /* Start items for lio_target_nacl_param_cit */
509e48354ceSNicholas Bellinger 
5102eafd729SChristoph Hellwig #define ISCSI_NACL_PARAM(name)						\
5112eafd729SChristoph Hellwig static ssize_t iscsi_nacl_param_##name##_show(struct config_item *item,	\
512e48354ceSNicholas Bellinger 		char *page)						\
513e48354ceSNicholas Bellinger {									\
5142eafd729SChristoph Hellwig 	struct se_node_acl *se_nacl = param_to_nacl(item);		\
515e48354ceSNicholas Bellinger 	struct iscsi_session *sess;					\
516e48354ceSNicholas Bellinger 	struct se_session *se_sess;					\
517e48354ceSNicholas Bellinger 	ssize_t rb;							\
518e48354ceSNicholas Bellinger 									\
519e48354ceSNicholas Bellinger 	spin_lock_bh(&se_nacl->nacl_sess_lock);				\
520e48354ceSNicholas Bellinger 	se_sess = se_nacl->nacl_sess;					\
521e48354ceSNicholas Bellinger 	if (!se_sess) {							\
522e48354ceSNicholas Bellinger 		rb = snprintf(page, PAGE_SIZE,				\
523e48354ceSNicholas Bellinger 			"No Active iSCSI Session\n");			\
524e48354ceSNicholas Bellinger 	} else {							\
525e48354ceSNicholas Bellinger 		sess = se_sess->fabric_sess_ptr;			\
526e48354ceSNicholas Bellinger 		rb = snprintf(page, PAGE_SIZE, "%u\n",			\
527e48354ceSNicholas Bellinger 			(u32)sess->sess_ops->name);			\
528e48354ceSNicholas Bellinger 	}								\
529e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_nacl->nacl_sess_lock);			\
530e48354ceSNicholas Bellinger 									\
531e48354ceSNicholas Bellinger 	return rb;							\
5322eafd729SChristoph Hellwig }									\
5332eafd729SChristoph Hellwig 									\
5342eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(iscsi_nacl_param_, name)
535e48354ceSNicholas Bellinger 
5362eafd729SChristoph Hellwig ISCSI_NACL_PARAM(MaxConnections);
5372eafd729SChristoph Hellwig ISCSI_NACL_PARAM(InitialR2T);
5382eafd729SChristoph Hellwig ISCSI_NACL_PARAM(ImmediateData);
5392eafd729SChristoph Hellwig ISCSI_NACL_PARAM(MaxBurstLength);
5402eafd729SChristoph Hellwig ISCSI_NACL_PARAM(FirstBurstLength);
5412eafd729SChristoph Hellwig ISCSI_NACL_PARAM(DefaultTime2Wait);
5422eafd729SChristoph Hellwig ISCSI_NACL_PARAM(DefaultTime2Retain);
5432eafd729SChristoph Hellwig ISCSI_NACL_PARAM(MaxOutstandingR2T);
5442eafd729SChristoph Hellwig ISCSI_NACL_PARAM(DataPDUInOrder);
5452eafd729SChristoph Hellwig ISCSI_NACL_PARAM(DataSequenceInOrder);
5462eafd729SChristoph Hellwig ISCSI_NACL_PARAM(ErrorRecoveryLevel);
547e48354ceSNicholas Bellinger 
548e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
5492eafd729SChristoph Hellwig 	&iscsi_nacl_param_attr_MaxConnections,
5502eafd729SChristoph Hellwig 	&iscsi_nacl_param_attr_InitialR2T,
5512eafd729SChristoph Hellwig 	&iscsi_nacl_param_attr_ImmediateData,
5522eafd729SChristoph Hellwig 	&iscsi_nacl_param_attr_MaxBurstLength,
5532eafd729SChristoph Hellwig 	&iscsi_nacl_param_attr_FirstBurstLength,
5542eafd729SChristoph Hellwig 	&iscsi_nacl_param_attr_DefaultTime2Wait,
5552eafd729SChristoph Hellwig 	&iscsi_nacl_param_attr_DefaultTime2Retain,
5562eafd729SChristoph Hellwig 	&iscsi_nacl_param_attr_MaxOutstandingR2T,
5572eafd729SChristoph Hellwig 	&iscsi_nacl_param_attr_DataPDUInOrder,
5582eafd729SChristoph Hellwig 	&iscsi_nacl_param_attr_DataSequenceInOrder,
5592eafd729SChristoph Hellwig 	&iscsi_nacl_param_attr_ErrorRecoveryLevel,
560e48354ceSNicholas Bellinger 	NULL,
561e48354ceSNicholas Bellinger };
562e48354ceSNicholas Bellinger 
563e48354ceSNicholas Bellinger /* End items for lio_target_nacl_param_cit */
564e48354ceSNicholas Bellinger 
565e48354ceSNicholas Bellinger /* Start items for lio_target_acl_cit */
566e48354ceSNicholas Bellinger 
5672eafd729SChristoph Hellwig static ssize_t lio_target_nacl_info_show(struct config_item *item, char *page)
568e48354ceSNicholas Bellinger {
5692eafd729SChristoph Hellwig 	struct se_node_acl *se_nacl = acl_to_nacl(item);
570e48354ceSNicholas Bellinger 	struct iscsi_session *sess;
571e48354ceSNicholas Bellinger 	struct iscsi_conn *conn;
572e48354ceSNicholas Bellinger 	struct se_session *se_sess;
573e48354ceSNicholas Bellinger 	ssize_t rb = 0;
574109e2381SRoland Dreier 	u32 max_cmd_sn;
575e48354ceSNicholas Bellinger 
576e48354ceSNicholas Bellinger 	spin_lock_bh(&se_nacl->nacl_sess_lock);
577e48354ceSNicholas Bellinger 	se_sess = se_nacl->nacl_sess;
578e48354ceSNicholas Bellinger 	if (!se_sess) {
579e48354ceSNicholas Bellinger 		rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
580e48354ceSNicholas Bellinger 			" Endpoint: %s\n", se_nacl->initiatorname);
581e48354ceSNicholas Bellinger 	} else {
582e48354ceSNicholas Bellinger 		sess = se_sess->fabric_sess_ptr;
583e48354ceSNicholas Bellinger 
584e48354ceSNicholas Bellinger 		rb += sprintf(page+rb, "InitiatorName: %s\n",
585e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
586e48354ceSNicholas Bellinger 		rb += sprintf(page+rb, "InitiatorAlias: %s\n",
587e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorAlias);
588e48354ceSNicholas Bellinger 
5895cdf5a87SAndy Shevchenko 		rb += sprintf(page+rb,
5905cdf5a87SAndy Shevchenko 			      "LIO Session ID: %u   ISID: 0x%6ph  TSIH: %hu  ",
5915cdf5a87SAndy Shevchenko 			      sess->sid, sess->isid, sess->tsih);
592e48354ceSNicholas Bellinger 		rb += sprintf(page+rb, "SessionType: %s\n",
593e48354ceSNicholas Bellinger 				(sess->sess_ops->SessionType) ?
594e48354ceSNicholas Bellinger 				"Discovery" : "Normal");
595e48354ceSNicholas Bellinger 		rb += sprintf(page+rb, "Session State: ");
596e48354ceSNicholas Bellinger 		switch (sess->session_state) {
597e48354ceSNicholas Bellinger 		case TARG_SESS_STATE_FREE:
598e48354ceSNicholas Bellinger 			rb += sprintf(page+rb, "TARG_SESS_FREE\n");
599e48354ceSNicholas Bellinger 			break;
600e48354ceSNicholas Bellinger 		case TARG_SESS_STATE_ACTIVE:
601e48354ceSNicholas Bellinger 			rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
602e48354ceSNicholas Bellinger 			break;
603e48354ceSNicholas Bellinger 		case TARG_SESS_STATE_LOGGED_IN:
604e48354ceSNicholas Bellinger 			rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
605e48354ceSNicholas Bellinger 			break;
606e48354ceSNicholas Bellinger 		case TARG_SESS_STATE_FAILED:
607e48354ceSNicholas Bellinger 			rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
608e48354ceSNicholas Bellinger 			break;
609e48354ceSNicholas Bellinger 		case TARG_SESS_STATE_IN_CONTINUE:
610e48354ceSNicholas Bellinger 			rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
611e48354ceSNicholas Bellinger 			break;
612e48354ceSNicholas Bellinger 		default:
613e48354ceSNicholas Bellinger 			rb += sprintf(page+rb, "ERROR: Unknown Session"
614e48354ceSNicholas Bellinger 					" State!\n");
615e48354ceSNicholas Bellinger 			break;
616e48354ceSNicholas Bellinger 		}
617e48354ceSNicholas Bellinger 
618e48354ceSNicholas Bellinger 		rb += sprintf(page+rb, "---------------------[iSCSI Session"
619e48354ceSNicholas Bellinger 				" Values]-----------------------\n");
620e48354ceSNicholas Bellinger 		rb += sprintf(page+rb, "  CmdSN/WR  :  CmdSN/WC  :  ExpCmdSN"
621e48354ceSNicholas Bellinger 				"  :  MaxCmdSN  :     ITT    :     TTT\n");
622109e2381SRoland Dreier 		max_cmd_sn = (u32) atomic_read(&sess->max_cmd_sn);
623e48354ceSNicholas Bellinger 		rb += sprintf(page+rb, " 0x%08x   0x%08x   0x%08x   0x%08x"
624e48354ceSNicholas Bellinger 				"   0x%08x   0x%08x\n",
625e48354ceSNicholas Bellinger 			sess->cmdsn_window,
626109e2381SRoland Dreier 			(max_cmd_sn - sess->exp_cmd_sn) + 1,
627109e2381SRoland Dreier 			sess->exp_cmd_sn, max_cmd_sn,
628e48354ceSNicholas Bellinger 			sess->init_task_tag, sess->targ_xfer_tag);
629e48354ceSNicholas Bellinger 		rb += sprintf(page+rb, "----------------------[iSCSI"
630e48354ceSNicholas Bellinger 				" Connections]-------------------------\n");
631e48354ceSNicholas Bellinger 
632e48354ceSNicholas Bellinger 		spin_lock(&sess->conn_lock);
633e48354ceSNicholas Bellinger 		list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
634e48354ceSNicholas Bellinger 			rb += sprintf(page+rb, "CID: %hu  Connection"
635e48354ceSNicholas Bellinger 					" State: ", conn->cid);
636e48354ceSNicholas Bellinger 			switch (conn->conn_state) {
637e48354ceSNicholas Bellinger 			case TARG_CONN_STATE_FREE:
638e48354ceSNicholas Bellinger 				rb += sprintf(page+rb,
639e48354ceSNicholas Bellinger 					"TARG_CONN_STATE_FREE\n");
640e48354ceSNicholas Bellinger 				break;
641e48354ceSNicholas Bellinger 			case TARG_CONN_STATE_XPT_UP:
642e48354ceSNicholas Bellinger 				rb += sprintf(page+rb,
643e48354ceSNicholas Bellinger 					"TARG_CONN_STATE_XPT_UP\n");
644e48354ceSNicholas Bellinger 				break;
645e48354ceSNicholas Bellinger 			case TARG_CONN_STATE_IN_LOGIN:
646e48354ceSNicholas Bellinger 				rb += sprintf(page+rb,
647e48354ceSNicholas Bellinger 					"TARG_CONN_STATE_IN_LOGIN\n");
648e48354ceSNicholas Bellinger 				break;
649e48354ceSNicholas Bellinger 			case TARG_CONN_STATE_LOGGED_IN:
650e48354ceSNicholas Bellinger 				rb += sprintf(page+rb,
651e48354ceSNicholas Bellinger 					"TARG_CONN_STATE_LOGGED_IN\n");
652e48354ceSNicholas Bellinger 				break;
653e48354ceSNicholas Bellinger 			case TARG_CONN_STATE_IN_LOGOUT:
654e48354ceSNicholas Bellinger 				rb += sprintf(page+rb,
655e48354ceSNicholas Bellinger 					"TARG_CONN_STATE_IN_LOGOUT\n");
656e48354ceSNicholas Bellinger 				break;
657e48354ceSNicholas Bellinger 			case TARG_CONN_STATE_LOGOUT_REQUESTED:
658e48354ceSNicholas Bellinger 				rb += sprintf(page+rb,
659e48354ceSNicholas Bellinger 					"TARG_CONN_STATE_LOGOUT_REQUESTED\n");
660e48354ceSNicholas Bellinger 				break;
661e48354ceSNicholas Bellinger 			case TARG_CONN_STATE_CLEANUP_WAIT:
662e48354ceSNicholas Bellinger 				rb += sprintf(page+rb,
663e48354ceSNicholas Bellinger 					"TARG_CONN_STATE_CLEANUP_WAIT\n");
664e48354ceSNicholas Bellinger 				break;
665e48354ceSNicholas Bellinger 			default:
666e48354ceSNicholas Bellinger 				rb += sprintf(page+rb,
667e48354ceSNicholas Bellinger 					"ERROR: Unknown Connection State!\n");
668e48354ceSNicholas Bellinger 				break;
669e48354ceSNicholas Bellinger 			}
670e48354ceSNicholas Bellinger 
671dc58f760SAndy Grover 			rb += sprintf(page+rb, "   Address %pISc %s", &conn->login_sockaddr,
672e48354ceSNicholas Bellinger 				(conn->network_transport == ISCSI_TCP) ?
673e48354ceSNicholas Bellinger 				"TCP" : "SCTP");
674e48354ceSNicholas Bellinger 			rb += sprintf(page+rb, "  StatSN: 0x%08x\n",
675e48354ceSNicholas Bellinger 				conn->stat_sn);
676e48354ceSNicholas Bellinger 		}
677e48354ceSNicholas Bellinger 		spin_unlock(&sess->conn_lock);
678e48354ceSNicholas Bellinger 	}
679e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_nacl->nacl_sess_lock);
680e48354ceSNicholas Bellinger 
681e48354ceSNicholas Bellinger 	return rb;
682e48354ceSNicholas Bellinger }
683e48354ceSNicholas Bellinger 
6842eafd729SChristoph Hellwig static ssize_t lio_target_nacl_cmdsn_depth_show(struct config_item *item,
685e48354ceSNicholas Bellinger 		char *page)
686e48354ceSNicholas Bellinger {
6872eafd729SChristoph Hellwig 	return sprintf(page, "%u\n", acl_to_nacl(item)->queue_depth);
688e48354ceSNicholas Bellinger }
689e48354ceSNicholas Bellinger 
6902eafd729SChristoph Hellwig static ssize_t lio_target_nacl_cmdsn_depth_store(struct config_item *item,
6912eafd729SChristoph Hellwig 		const char *page, size_t count)
692e48354ceSNicholas Bellinger {
6932eafd729SChristoph Hellwig 	struct se_node_acl *se_nacl = acl_to_nacl(item);
694e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = se_nacl->se_tpg;
695e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,
696e48354ceSNicholas Bellinger 			struct iscsi_portal_group, tpg_se_tpg);
697e48354ceSNicholas Bellinger 	struct config_item *acl_ci, *tpg_ci, *wwn_ci;
698e48354ceSNicholas Bellinger 	u32 cmdsn_depth = 0;
699e48354ceSNicholas Bellinger 	int ret;
700e48354ceSNicholas Bellinger 
701ad7babd2SJörn Engel 	ret = kstrtou32(page, 0, &cmdsn_depth);
702ad7babd2SJörn Engel 	if (ret)
703ad7babd2SJörn Engel 		return ret;
704e48354ceSNicholas Bellinger 	if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
705e48354ceSNicholas Bellinger 		pr_err("Passed cmdsn_depth: %u exceeds"
706e48354ceSNicholas Bellinger 			" TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
707e48354ceSNicholas Bellinger 			TA_DEFAULT_CMDSN_DEPTH_MAX);
708e48354ceSNicholas Bellinger 		return -EINVAL;
709e48354ceSNicholas Bellinger 	}
710e48354ceSNicholas Bellinger 	acl_ci = &se_nacl->acl_group.cg_item;
711e48354ceSNicholas Bellinger 	if (!acl_ci) {
712e48354ceSNicholas Bellinger 		pr_err("Unable to locatel acl_ci\n");
713e48354ceSNicholas Bellinger 		return -EINVAL;
714e48354ceSNicholas Bellinger 	}
715e48354ceSNicholas Bellinger 	tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
716e48354ceSNicholas Bellinger 	if (!tpg_ci) {
717e48354ceSNicholas Bellinger 		pr_err("Unable to locate tpg_ci\n");
718e48354ceSNicholas Bellinger 		return -EINVAL;
719e48354ceSNicholas Bellinger 	}
720e48354ceSNicholas Bellinger 	wwn_ci = &tpg_ci->ci_group->cg_item;
721e48354ceSNicholas Bellinger 	if (!wwn_ci) {
722e48354ceSNicholas Bellinger 		pr_err("Unable to locate config_item wwn_ci\n");
723e48354ceSNicholas Bellinger 		return -EINVAL;
724e48354ceSNicholas Bellinger 	}
725e48354ceSNicholas Bellinger 
726e48354ceSNicholas Bellinger 	if (iscsit_get_tpg(tpg) < 0)
727e48354ceSNicholas Bellinger 		return -EINVAL;
728d36ad77fSNicholas Bellinger 
729d36ad77fSNicholas Bellinger 	ret = core_tpg_set_initiator_node_queue_depth(se_nacl, cmdsn_depth);
730e48354ceSNicholas Bellinger 
731e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
732e48354ceSNicholas Bellinger 		"InitiatorName: %s\n", config_item_name(wwn_ci),
733e48354ceSNicholas Bellinger 		config_item_name(tpg_ci), cmdsn_depth,
734e48354ceSNicholas Bellinger 		config_item_name(acl_ci));
735e48354ceSNicholas Bellinger 
736e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);
737e48354ceSNicholas Bellinger 	return (!ret) ? count : (ssize_t)ret;
738e48354ceSNicholas Bellinger }
739e48354ceSNicholas Bellinger 
7402eafd729SChristoph Hellwig static ssize_t lio_target_nacl_tag_show(struct config_item *item, char *page)
74179e62fc3SAndy Grover {
7422eafd729SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s", acl_to_nacl(item)->acl_tag);
74379e62fc3SAndy Grover }
74479e62fc3SAndy Grover 
7452eafd729SChristoph Hellwig static ssize_t lio_target_nacl_tag_store(struct config_item *item,
7462eafd729SChristoph Hellwig 		const char *page, size_t count)
74779e62fc3SAndy Grover {
7482eafd729SChristoph Hellwig 	struct se_node_acl *se_nacl = acl_to_nacl(item);
74979e62fc3SAndy Grover 	int ret;
75079e62fc3SAndy Grover 
75179e62fc3SAndy Grover 	ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
75279e62fc3SAndy Grover 
75379e62fc3SAndy Grover 	if (ret < 0)
75479e62fc3SAndy Grover 		return ret;
75579e62fc3SAndy Grover 	return count;
75679e62fc3SAndy Grover }
75779e62fc3SAndy Grover 
7582eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(lio_target_nacl_, info);
7592eafd729SChristoph Hellwig CONFIGFS_ATTR(lio_target_nacl_, cmdsn_depth);
7602eafd729SChristoph Hellwig CONFIGFS_ATTR(lio_target_nacl_, tag);
76179e62fc3SAndy Grover 
762e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_initiator_attrs[] = {
7632eafd729SChristoph Hellwig 	&lio_target_nacl_attr_info,
7642eafd729SChristoph Hellwig 	&lio_target_nacl_attr_cmdsn_depth,
7652eafd729SChristoph Hellwig 	&lio_target_nacl_attr_tag,
766e48354ceSNicholas Bellinger 	NULL,
767e48354ceSNicholas Bellinger };
768e48354ceSNicholas Bellinger 
769c7d6a803SChristoph Hellwig static int lio_target_init_nodeacl(struct se_node_acl *se_nacl,
770e48354ceSNicholas Bellinger 		const char *name)
771e48354ceSNicholas Bellinger {
772c7d6a803SChristoph Hellwig 	struct iscsi_node_acl *acl =
773c7d6a803SChristoph Hellwig 		container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
774e48354ceSNicholas Bellinger 
775b7eec2cdSAndy Grover 	config_group_init_type_name(&acl->node_stat_grps.iscsi_sess_stats_group,
776e48354ceSNicholas Bellinger 			"iscsi_sess_stats", &iscsi_stat_sess_cit);
7771ae1602dSChristoph Hellwig 	configfs_add_default_group(&acl->node_stat_grps.iscsi_sess_stats_group,
7781ae1602dSChristoph Hellwig 			&se_nacl->acl_fabric_stat_group);
779c7d6a803SChristoph Hellwig 	return 0;
780e48354ceSNicholas Bellinger }
781e48354ceSNicholas Bellinger 
782e48354ceSNicholas Bellinger /* End items for lio_target_acl_cit */
783e48354ceSNicholas Bellinger 
784e48354ceSNicholas Bellinger /* Start items for lio_target_tpg_attrib_cit */
785e48354ceSNicholas Bellinger 
786e48354ceSNicholas Bellinger #define DEF_TPG_ATTRIB(name)						\
787e48354ceSNicholas Bellinger 									\
7882eafd729SChristoph Hellwig static ssize_t iscsi_tpg_attrib_##name##_show(struct config_item *item,	\
789e48354ceSNicholas Bellinger 		char *page)						\
790e48354ceSNicholas Bellinger {									\
7912eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = attrib_to_tpg(item);		\
792e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
793e48354ceSNicholas Bellinger 			struct iscsi_portal_group, tpg_se_tpg);	\
794e48354ceSNicholas Bellinger 	ssize_t rb;							\
795e48354ceSNicholas Bellinger 									\
796e48354ceSNicholas Bellinger 	if (iscsit_get_tpg(tpg) < 0)					\
797e48354ceSNicholas Bellinger 		return -EINVAL;						\
798e48354ceSNicholas Bellinger 									\
799b7eec2cdSAndy Grover 	rb = sprintf(page, "%u\n", tpg->tpg_attrib.name);		\
800e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);						\
801e48354ceSNicholas Bellinger 	return rb;							\
802e48354ceSNicholas Bellinger }									\
803e48354ceSNicholas Bellinger 									\
8042eafd729SChristoph Hellwig static ssize_t iscsi_tpg_attrib_##name##_store(struct config_item *item,\
8052eafd729SChristoph Hellwig 		const char *page, size_t count)				\
806e48354ceSNicholas Bellinger {									\
8072eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = attrib_to_tpg(item);		\
808e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
809e48354ceSNicholas Bellinger 			struct iscsi_portal_group, tpg_se_tpg);	\
810e48354ceSNicholas Bellinger 	u32 val;							\
811e48354ceSNicholas Bellinger 	int ret;							\
812e48354ceSNicholas Bellinger 									\
813e48354ceSNicholas Bellinger 	if (iscsit_get_tpg(tpg) < 0)					\
814e48354ceSNicholas Bellinger 		return -EINVAL;						\
815e48354ceSNicholas Bellinger 									\
816ad7babd2SJörn Engel 	ret = kstrtou32(page, 0, &val);					\
817ad7babd2SJörn Engel 	if (ret)							\
818ad7babd2SJörn Engel 		goto out;						\
819e48354ceSNicholas Bellinger 	ret = iscsit_ta_##name(tpg, val);				\
820e48354ceSNicholas Bellinger 	if (ret < 0)							\
821e48354ceSNicholas Bellinger 		goto out;						\
822e48354ceSNicholas Bellinger 									\
823e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);						\
824e48354ceSNicholas Bellinger 	return count;							\
825e48354ceSNicholas Bellinger out:									\
826e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);						\
827e48354ceSNicholas Bellinger 	return ret;							\
8282eafd729SChristoph Hellwig }									\
8292eafd729SChristoph Hellwig CONFIGFS_ATTR(iscsi_tpg_attrib_, name)
830e48354ceSNicholas Bellinger 
831e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(authentication);
832e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(login_timeout);
833e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(netif_timeout);
834e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(generate_node_acls);
835e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(default_cmdsn_depth);
836e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(cache_dynamic_acls);
837e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(demo_mode_write_protect);
838e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(prod_mode_write_protect);
8394c54b6cfSThomas Glanzmann DEF_TPG_ATTRIB(demo_mode_discovery);
840d1fa7a1dSNicholas Bellinger DEF_TPG_ATTRIB(default_erl);
8418085176fSSagi Grimberg DEF_TPG_ATTRIB(t10_pi);
842901c04a3SNicholas Bellinger DEF_TPG_ATTRIB(fabric_prot_type);
843a6415cddSDavid Disseldorp DEF_TPG_ATTRIB(tpg_enabled_sendtargets);
844e48354ceSNicholas Bellinger 
845e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
8462eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_authentication,
8472eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_login_timeout,
8482eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_netif_timeout,
8492eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_generate_node_acls,
8502eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_default_cmdsn_depth,
8512eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_cache_dynamic_acls,
8522eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_demo_mode_write_protect,
8532eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_prod_mode_write_protect,
8542eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_demo_mode_discovery,
8552eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_default_erl,
8562eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_t10_pi,
8572eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_fabric_prot_type,
8582eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_tpg_enabled_sendtargets,
859e48354ceSNicholas Bellinger 	NULL,
860e48354ceSNicholas Bellinger };
861e48354ceSNicholas Bellinger 
862e48354ceSNicholas Bellinger /* End items for lio_target_tpg_attrib_cit */
863e48354ceSNicholas Bellinger 
864c3e51442SNicholas Bellinger /* Start items for lio_target_tpg_auth_cit */
865c3e51442SNicholas Bellinger 
866c3e51442SNicholas Bellinger #define __DEF_TPG_AUTH_STR(prefix, name, flags)					\
8672eafd729SChristoph Hellwig static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg,	\
868c3e51442SNicholas Bellinger 		char *page)							\
869c3e51442SNicholas Bellinger {										\
870c3e51442SNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,			\
871c3e51442SNicholas Bellinger 				struct iscsi_portal_group, tpg_se_tpg);		\
872c3e51442SNicholas Bellinger 	struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;			\
873c3e51442SNicholas Bellinger 										\
874c3e51442SNicholas Bellinger 	if (!capable(CAP_SYS_ADMIN))						\
875c3e51442SNicholas Bellinger 		return -EPERM;							\
876c3e51442SNicholas Bellinger 										\
877c3e51442SNicholas Bellinger 	return snprintf(page, PAGE_SIZE, "%s\n", auth->name);			\
878c3e51442SNicholas Bellinger }										\
879c3e51442SNicholas Bellinger 										\
8802eafd729SChristoph Hellwig static ssize_t __iscsi_##prefix##_##name##_store(struct se_portal_group *se_tpg,\
8812eafd729SChristoph Hellwig 		const char *page, size_t count)					\
882c3e51442SNicholas Bellinger {										\
883c3e51442SNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,			\
884c3e51442SNicholas Bellinger 				struct iscsi_portal_group, tpg_se_tpg);		\
885c3e51442SNicholas Bellinger 	struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;			\
886c3e51442SNicholas Bellinger 										\
887c3e51442SNicholas Bellinger 	if (!capable(CAP_SYS_ADMIN))						\
888c3e51442SNicholas Bellinger 		return -EPERM;							\
889c3e51442SNicholas Bellinger 										\
89037b32c6fSNicholas Bellinger 	snprintf(auth->name, sizeof(auth->name), "%s", page);			\
891c3e51442SNicholas Bellinger 	if (!(strncmp("NULL", auth->name, 4)))					\
892c3e51442SNicholas Bellinger 		auth->naf_flags &= ~flags;					\
893c3e51442SNicholas Bellinger 	else									\
894c3e51442SNicholas Bellinger 		auth->naf_flags |= flags;					\
895c3e51442SNicholas Bellinger 										\
896c3e51442SNicholas Bellinger 	if ((auth->naf_flags & NAF_USERID_IN_SET) &&				\
897c3e51442SNicholas Bellinger 	    (auth->naf_flags & NAF_PASSWORD_IN_SET))				\
898c3e51442SNicholas Bellinger 		auth->authenticate_target = 1;					\
899c3e51442SNicholas Bellinger 	else									\
900c3e51442SNicholas Bellinger 		auth->authenticate_target = 0;					\
901c3e51442SNicholas Bellinger 										\
902c3e51442SNicholas Bellinger 	return count;								\
903c3e51442SNicholas Bellinger }
904c3e51442SNicholas Bellinger 
9052eafd729SChristoph Hellwig #define DEF_TPG_AUTH_STR(name, flags)						\
9062eafd729SChristoph Hellwig 	__DEF_TPG_AUTH_STR(tpg_auth, name, flags)				\
9072eafd729SChristoph Hellwig static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item,		\
9082eafd729SChristoph Hellwig 		char *page)							\
9092eafd729SChristoph Hellwig {										\
9102eafd729SChristoph Hellwig 	return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page);		\
9112eafd729SChristoph Hellwig }										\
9122eafd729SChristoph Hellwig 										\
9132eafd729SChristoph Hellwig static ssize_t iscsi_tpg_auth_##name##_store(struct config_item *item,		\
9142eafd729SChristoph Hellwig 		const char *page, size_t count)					\
9152eafd729SChristoph Hellwig {										\
9162eafd729SChristoph Hellwig 	return __iscsi_tpg_auth_##name##_store(auth_to_tpg(item), page, count);	\
9172eafd729SChristoph Hellwig }										\
9182eafd729SChristoph Hellwig 										\
9192eafd729SChristoph Hellwig CONFIGFS_ATTR(iscsi_tpg_auth_, name);
9202eafd729SChristoph Hellwig 
9212eafd729SChristoph Hellwig 
9222eafd729SChristoph Hellwig DEF_TPG_AUTH_STR(userid, NAF_USERID_SET);
9232eafd729SChristoph Hellwig DEF_TPG_AUTH_STR(password, NAF_PASSWORD_SET);
9242eafd729SChristoph Hellwig DEF_TPG_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
9252eafd729SChristoph Hellwig DEF_TPG_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
9262eafd729SChristoph Hellwig 
927c3e51442SNicholas Bellinger #define __DEF_TPG_AUTH_INT(prefix, name)					\
9282eafd729SChristoph Hellwig static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg,	\
929c3e51442SNicholas Bellinger 		char *page)								\
930c3e51442SNicholas Bellinger {										\
931c3e51442SNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,			\
932c3e51442SNicholas Bellinger 				struct iscsi_portal_group, tpg_se_tpg);		\
933c3e51442SNicholas Bellinger 	struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;			\
934c3e51442SNicholas Bellinger 										\
935c3e51442SNicholas Bellinger 	if (!capable(CAP_SYS_ADMIN))						\
936c3e51442SNicholas Bellinger 		return -EPERM;							\
937c3e51442SNicholas Bellinger 										\
938c3e51442SNicholas Bellinger 	return snprintf(page, PAGE_SIZE, "%d\n", auth->name);			\
939c3e51442SNicholas Bellinger }
940c3e51442SNicholas Bellinger 
941c3e51442SNicholas Bellinger #define DEF_TPG_AUTH_INT(name)							\
942c3e51442SNicholas Bellinger 	__DEF_TPG_AUTH_INT(tpg_auth, name)					\
9432eafd729SChristoph Hellwig static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item,		\
944c3e51442SNicholas Bellinger 		char *page) \
945c3e51442SNicholas Bellinger {										\
9462eafd729SChristoph Hellwig 	return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page);		\
9472eafd729SChristoph Hellwig }										\
9482eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(iscsi_tpg_auth_, name);
949c3e51442SNicholas Bellinger 
950c3e51442SNicholas Bellinger DEF_TPG_AUTH_INT(authenticate_target);
951c3e51442SNicholas Bellinger 
952c3e51442SNicholas Bellinger static struct configfs_attribute *lio_target_tpg_auth_attrs[] = {
9532eafd729SChristoph Hellwig 	&iscsi_tpg_auth_attr_userid,
9542eafd729SChristoph Hellwig 	&iscsi_tpg_auth_attr_password,
9552eafd729SChristoph Hellwig 	&iscsi_tpg_auth_attr_authenticate_target,
9562eafd729SChristoph Hellwig 	&iscsi_tpg_auth_attr_userid_mutual,
9572eafd729SChristoph Hellwig 	&iscsi_tpg_auth_attr_password_mutual,
958c3e51442SNicholas Bellinger 	NULL,
959c3e51442SNicholas Bellinger };
960c3e51442SNicholas Bellinger 
961c3e51442SNicholas Bellinger /* End items for lio_target_tpg_auth_cit */
962c3e51442SNicholas Bellinger 
963e48354ceSNicholas Bellinger /* Start items for lio_target_tpg_param_cit */
964e48354ceSNicholas Bellinger 
965e48354ceSNicholas Bellinger #define DEF_TPG_PARAM(name)						\
9662eafd729SChristoph Hellwig static ssize_t iscsi_tpg_param_##name##_show(struct config_item *item,	\
967e48354ceSNicholas Bellinger 		char *page)						\
968e48354ceSNicholas Bellinger {									\
9692eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = param_to_tpg(item);		\
970e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
971e48354ceSNicholas Bellinger 			struct iscsi_portal_group, tpg_se_tpg);		\
972e48354ceSNicholas Bellinger 	struct iscsi_param *param;					\
973e48354ceSNicholas Bellinger 	ssize_t rb;							\
974e48354ceSNicholas Bellinger 									\
975e48354ceSNicholas Bellinger 	if (iscsit_get_tpg(tpg) < 0)					\
976e48354ceSNicholas Bellinger 		return -EINVAL;						\
977e48354ceSNicholas Bellinger 									\
978e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(__stringify(name),		\
979e48354ceSNicholas Bellinger 				tpg->param_list);			\
980e48354ceSNicholas Bellinger 	if (!param) {							\
981e48354ceSNicholas Bellinger 		iscsit_put_tpg(tpg);					\
982e48354ceSNicholas Bellinger 		return -EINVAL;						\
983e48354ceSNicholas Bellinger 	}								\
984e48354ceSNicholas Bellinger 	rb = snprintf(page, PAGE_SIZE, "%s\n", param->value);		\
985e48354ceSNicholas Bellinger 									\
986e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);						\
987e48354ceSNicholas Bellinger 	return rb;							\
988e48354ceSNicholas Bellinger }									\
9892eafd729SChristoph Hellwig static ssize_t iscsi_tpg_param_##name##_store(struct config_item *item, \
9902eafd729SChristoph Hellwig 		const char *page, size_t count)				\
991e48354ceSNicholas Bellinger {									\
9922eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = param_to_tpg(item);		\
993e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
994e48354ceSNicholas Bellinger 			struct iscsi_portal_group, tpg_se_tpg);		\
995e48354ceSNicholas Bellinger 	char *buf;							\
996ad7babd2SJörn Engel 	int ret, len;							\
997e48354ceSNicholas Bellinger 									\
998e48354ceSNicholas Bellinger 	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);				\
999e48354ceSNicholas Bellinger 	if (!buf)							\
1000e48354ceSNicholas Bellinger 		return -ENOMEM;						\
1001ad7babd2SJörn Engel 	len = snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page);	\
1002ad7babd2SJörn Engel 	if (isspace(buf[len-1]))					\
1003ad7babd2SJörn Engel 		buf[len-1] = '\0'; /* Kill newline */			\
1004e48354ceSNicholas Bellinger 									\
1005e48354ceSNicholas Bellinger 	if (iscsit_get_tpg(tpg) < 0) {					\
1006e48354ceSNicholas Bellinger 		kfree(buf);						\
1007e48354ceSNicholas Bellinger 		return -EINVAL;						\
1008e48354ceSNicholas Bellinger 	}								\
1009e48354ceSNicholas Bellinger 									\
1010e48354ceSNicholas Bellinger 	ret = iscsi_change_param_value(buf, tpg->param_list, 1);	\
1011e48354ceSNicholas Bellinger 	if (ret < 0)							\
1012e48354ceSNicholas Bellinger 		goto out;						\
1013e48354ceSNicholas Bellinger 									\
1014e48354ceSNicholas Bellinger 	kfree(buf);							\
1015e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);						\
1016e48354ceSNicholas Bellinger 	return count;							\
1017e48354ceSNicholas Bellinger out:									\
1018e48354ceSNicholas Bellinger 	kfree(buf);							\
1019e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);						\
1020e48354ceSNicholas Bellinger 	return -EINVAL;							\
10212eafd729SChristoph Hellwig }									\
10222eafd729SChristoph Hellwig CONFIGFS_ATTR(iscsi_tpg_param_, name)
1023e48354ceSNicholas Bellinger 
1024e48354ceSNicholas Bellinger DEF_TPG_PARAM(AuthMethod);
1025e48354ceSNicholas Bellinger DEF_TPG_PARAM(HeaderDigest);
1026e48354ceSNicholas Bellinger DEF_TPG_PARAM(DataDigest);
1027e48354ceSNicholas Bellinger DEF_TPG_PARAM(MaxConnections);
1028e48354ceSNicholas Bellinger DEF_TPG_PARAM(TargetAlias);
1029e48354ceSNicholas Bellinger DEF_TPG_PARAM(InitialR2T);
1030e48354ceSNicholas Bellinger DEF_TPG_PARAM(ImmediateData);
1031e48354ceSNicholas Bellinger DEF_TPG_PARAM(MaxRecvDataSegmentLength);
1032e004cb25SNicholas Bellinger DEF_TPG_PARAM(MaxXmitDataSegmentLength);
1033e48354ceSNicholas Bellinger DEF_TPG_PARAM(MaxBurstLength);
1034e48354ceSNicholas Bellinger DEF_TPG_PARAM(FirstBurstLength);
1035e48354ceSNicholas Bellinger DEF_TPG_PARAM(DefaultTime2Wait);
1036e48354ceSNicholas Bellinger DEF_TPG_PARAM(DefaultTime2Retain);
1037e48354ceSNicholas Bellinger DEF_TPG_PARAM(MaxOutstandingR2T);
1038e48354ceSNicholas Bellinger DEF_TPG_PARAM(DataPDUInOrder);
1039e48354ceSNicholas Bellinger DEF_TPG_PARAM(DataSequenceInOrder);
1040e48354ceSNicholas Bellinger DEF_TPG_PARAM(ErrorRecoveryLevel);
1041e48354ceSNicholas Bellinger DEF_TPG_PARAM(IFMarker);
1042e48354ceSNicholas Bellinger DEF_TPG_PARAM(OFMarker);
1043e48354ceSNicholas Bellinger DEF_TPG_PARAM(IFMarkInt);
1044e48354ceSNicholas Bellinger DEF_TPG_PARAM(OFMarkInt);
1045e48354ceSNicholas Bellinger 
1046e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
10472eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_AuthMethod,
10482eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_HeaderDigest,
10492eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_DataDigest,
10502eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_MaxConnections,
10512eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_TargetAlias,
10522eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_InitialR2T,
10532eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_ImmediateData,
10542eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_MaxRecvDataSegmentLength,
10552eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_MaxXmitDataSegmentLength,
10562eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_MaxBurstLength,
10572eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_FirstBurstLength,
10582eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_DefaultTime2Wait,
10592eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_DefaultTime2Retain,
10602eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_MaxOutstandingR2T,
10612eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_DataPDUInOrder,
10622eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_DataSequenceInOrder,
10632eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_ErrorRecoveryLevel,
10642eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_IFMarker,
10652eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_OFMarker,
10662eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_IFMarkInt,
10672eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_OFMarkInt,
1068e48354ceSNicholas Bellinger 	NULL,
1069e48354ceSNicholas Bellinger };
1070e48354ceSNicholas Bellinger 
1071e48354ceSNicholas Bellinger /* End items for lio_target_tpg_param_cit */
1072e48354ceSNicholas Bellinger 
1073e48354ceSNicholas Bellinger /* Start items for lio_target_tpg_cit */
1074e48354ceSNicholas Bellinger 
10752eafd729SChristoph Hellwig static ssize_t lio_target_tpg_enable_show(struct config_item *item, char *page)
1076e48354ceSNicholas Bellinger {
10772eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = to_tpg(item);
1078e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1079e48354ceSNicholas Bellinger 			struct iscsi_portal_group, tpg_se_tpg);
1080e48354ceSNicholas Bellinger 	ssize_t len;
1081e48354ceSNicholas Bellinger 
1082e48354ceSNicholas Bellinger 	spin_lock(&tpg->tpg_state_lock);
1083e48354ceSNicholas Bellinger 	len = sprintf(page, "%d\n",
1084e48354ceSNicholas Bellinger 			(tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1085e48354ceSNicholas Bellinger 	spin_unlock(&tpg->tpg_state_lock);
1086e48354ceSNicholas Bellinger 
1087e48354ceSNicholas Bellinger 	return len;
1088e48354ceSNicholas Bellinger }
1089e48354ceSNicholas Bellinger 
10902eafd729SChristoph Hellwig static ssize_t lio_target_tpg_enable_store(struct config_item *item,
10912eafd729SChristoph Hellwig 		const char *page, size_t count)
1092e48354ceSNicholas Bellinger {
10932eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = to_tpg(item);
1094e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1095e48354ceSNicholas Bellinger 			struct iscsi_portal_group, tpg_se_tpg);
1096e48354ceSNicholas Bellinger 	u32 op;
1097ad7babd2SJörn Engel 	int ret;
1098e48354ceSNicholas Bellinger 
1099ad7babd2SJörn Engel 	ret = kstrtou32(page, 0, &op);
1100ad7babd2SJörn Engel 	if (ret)
1101ad7babd2SJörn Engel 		return ret;
1102e48354ceSNicholas Bellinger 	if ((op != 1) && (op != 0)) {
1103e48354ceSNicholas Bellinger 		pr_err("Illegal value for tpg_enable: %u\n", op);
1104e48354ceSNicholas Bellinger 		return -EINVAL;
1105e48354ceSNicholas Bellinger 	}
1106e48354ceSNicholas Bellinger 
1107e48354ceSNicholas Bellinger 	ret = iscsit_get_tpg(tpg);
1108e48354ceSNicholas Bellinger 	if (ret < 0)
1109e48354ceSNicholas Bellinger 		return -EINVAL;
1110e48354ceSNicholas Bellinger 
1111e48354ceSNicholas Bellinger 	if (op) {
1112e48354ceSNicholas Bellinger 		ret = iscsit_tpg_enable_portal_group(tpg);
1113e48354ceSNicholas Bellinger 		if (ret < 0)
1114e48354ceSNicholas Bellinger 			goto out;
1115e48354ceSNicholas Bellinger 	} else {
1116e48354ceSNicholas Bellinger 		/*
1117e48354ceSNicholas Bellinger 		 * iscsit_tpg_disable_portal_group() assumes force=1
1118e48354ceSNicholas Bellinger 		 */
1119e48354ceSNicholas Bellinger 		ret = iscsit_tpg_disable_portal_group(tpg, 1);
1120e48354ceSNicholas Bellinger 		if (ret < 0)
1121e48354ceSNicholas Bellinger 			goto out;
1122e48354ceSNicholas Bellinger 	}
1123e48354ceSNicholas Bellinger 
1124e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);
1125e48354ceSNicholas Bellinger 	return count;
1126e48354ceSNicholas Bellinger out:
1127e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);
1128e48354ceSNicholas Bellinger 	return -EINVAL;
1129e48354ceSNicholas Bellinger }
1130e48354ceSNicholas Bellinger 
1131e48354ceSNicholas Bellinger 
11322eafd729SChristoph Hellwig static ssize_t lio_target_tpg_dynamic_sessions_show(struct config_item *item,
1133d4ee46ffSNicholas Bellinger 		char *page)
1134d4ee46ffSNicholas Bellinger {
11352eafd729SChristoph Hellwig 	return target_show_dynamic_sessions(to_tpg(item), page);
1136d4ee46ffSNicholas Bellinger }
1137d4ee46ffSNicholas Bellinger 
11382eafd729SChristoph Hellwig CONFIGFS_ATTR(lio_target_tpg_, enable);
11392eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(lio_target_tpg_, dynamic_sessions);
1140d4ee46ffSNicholas Bellinger 
1141e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_tpg_attrs[] = {
11422eafd729SChristoph Hellwig 	&lio_target_tpg_attr_enable,
11432eafd729SChristoph Hellwig 	&lio_target_tpg_attr_dynamic_sessions,
1144e48354ceSNicholas Bellinger 	NULL,
1145e48354ceSNicholas Bellinger };
1146e48354ceSNicholas Bellinger 
1147e48354ceSNicholas Bellinger /* End items for lio_target_tpg_cit */
1148e48354ceSNicholas Bellinger 
1149e48354ceSNicholas Bellinger /* Start items for lio_target_tiqn_cit */
1150e48354ceSNicholas Bellinger 
1151fceb5bc7SChristoph Hellwig static struct se_portal_group *lio_target_tiqn_addtpg(
1152e48354ceSNicholas Bellinger 	struct se_wwn *wwn,
1153e48354ceSNicholas Bellinger 	struct config_group *group,
1154e48354ceSNicholas Bellinger 	const char *name)
1155e48354ceSNicholas Bellinger {
1156e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg;
1157e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
1158ad7babd2SJörn Engel 	char *tpgt_str;
1159ad7babd2SJörn Engel 	int ret;
1160ad7babd2SJörn Engel 	u16 tpgt;
1161e48354ceSNicholas Bellinger 
1162e48354ceSNicholas Bellinger 	tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1163e48354ceSNicholas Bellinger 	/*
1164e48354ceSNicholas Bellinger 	 * Only tpgt_# directory groups can be created below
1165e48354ceSNicholas Bellinger 	 * target/iscsi/iqn.superturodiskarry/
1166e48354ceSNicholas Bellinger 	 */
1167e48354ceSNicholas Bellinger 	tpgt_str = strstr(name, "tpgt_");
1168e48354ceSNicholas Bellinger 	if (!tpgt_str) {
1169e48354ceSNicholas Bellinger 		pr_err("Unable to locate \"tpgt_#\" directory"
1170e48354ceSNicholas Bellinger 				" group\n");
1171e48354ceSNicholas Bellinger 		return NULL;
1172e48354ceSNicholas Bellinger 	}
1173e48354ceSNicholas Bellinger 	tpgt_str += 5; /* Skip ahead of "tpgt_" */
1174ad7babd2SJörn Engel 	ret = kstrtou16(tpgt_str, 0, &tpgt);
1175ad7babd2SJörn Engel 	if (ret)
1176ad7babd2SJörn Engel 		return NULL;
1177e48354ceSNicholas Bellinger 
1178e48354ceSNicholas Bellinger 	tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1179e48354ceSNicholas Bellinger 	if (!tpg)
1180e48354ceSNicholas Bellinger 		return NULL;
1181e48354ceSNicholas Bellinger 
1182bc0c94b1SNicholas Bellinger 	ret = core_tpg_register(wwn, &tpg->tpg_se_tpg, SCSI_PROTOCOL_ISCSI);
1183e48354ceSNicholas Bellinger 	if (ret < 0)
1184e48354ceSNicholas Bellinger 		return NULL;
1185e48354ceSNicholas Bellinger 
1186e48354ceSNicholas Bellinger 	ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1187e48354ceSNicholas Bellinger 	if (ret != 0)
1188e48354ceSNicholas Bellinger 		goto out;
1189e48354ceSNicholas Bellinger 
1190e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1191e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1192e48354ceSNicholas Bellinger 			name);
1193e48354ceSNicholas Bellinger 	return &tpg->tpg_se_tpg;
1194e48354ceSNicholas Bellinger out:
1195e48354ceSNicholas Bellinger 	core_tpg_deregister(&tpg->tpg_se_tpg);
1196e48354ceSNicholas Bellinger 	kfree(tpg);
1197e48354ceSNicholas Bellinger 	return NULL;
1198e48354ceSNicholas Bellinger }
1199e48354ceSNicholas Bellinger 
1200fceb5bc7SChristoph Hellwig static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1201e48354ceSNicholas Bellinger {
1202e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg;
1203e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
1204e48354ceSNicholas Bellinger 
1205e48354ceSNicholas Bellinger 	tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1206e48354ceSNicholas Bellinger 	tiqn = tpg->tpg_tiqn;
1207e48354ceSNicholas Bellinger 	/*
1208e48354ceSNicholas Bellinger 	 * iscsit_tpg_del_portal_group() assumes force=1
1209e48354ceSNicholas Bellinger 	 */
1210e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1211e48354ceSNicholas Bellinger 	iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1212e48354ceSNicholas Bellinger }
1213e48354ceSNicholas Bellinger 
1214e48354ceSNicholas Bellinger /* End items for lio_target_tiqn_cit */
1215e48354ceSNicholas Bellinger 
1216e48354ceSNicholas Bellinger /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1217e48354ceSNicholas Bellinger 
12182eafd729SChristoph Hellwig static ssize_t lio_target_wwn_lio_version_show(struct config_item *item,
1219e48354ceSNicholas Bellinger 		char *page)
1220e48354ceSNicholas Bellinger {
12214c76251eSNicholas Bellinger 	return sprintf(page, "Datera Inc. iSCSI Target "ISCSIT_VERSION"\n");
1222e48354ceSNicholas Bellinger }
1223e48354ceSNicholas Bellinger 
12242eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(lio_target_wwn_, lio_version);
1225e48354ceSNicholas Bellinger 
1226e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_wwn_attrs[] = {
12272eafd729SChristoph Hellwig 	&lio_target_wwn_attr_lio_version,
1228e48354ceSNicholas Bellinger 	NULL,
1229e48354ceSNicholas Bellinger };
1230e48354ceSNicholas Bellinger 
1231fceb5bc7SChristoph Hellwig static struct se_wwn *lio_target_call_coreaddtiqn(
1232e48354ceSNicholas Bellinger 	struct target_fabric_configfs *tf,
1233e48354ceSNicholas Bellinger 	struct config_group *group,
1234e48354ceSNicholas Bellinger 	const char *name)
1235e48354ceSNicholas Bellinger {
1236e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
1237e48354ceSNicholas Bellinger 
1238e48354ceSNicholas Bellinger 	tiqn = iscsit_add_tiqn((unsigned char *)name);
1239e48354ceSNicholas Bellinger 	if (IS_ERR(tiqn))
1240e1750ba2SThomas Meyer 		return ERR_CAST(tiqn);
1241e48354ceSNicholas Bellinger 
1242839559e1SChristoph Hellwig 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1243839559e1SChristoph Hellwig 	pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1244839559e1SChristoph Hellwig 			" %s\n", name);
1245839559e1SChristoph Hellwig 	return &tiqn->tiqn_wwn;
1246839559e1SChristoph Hellwig }
1247839559e1SChristoph Hellwig 
1248839559e1SChristoph Hellwig static void lio_target_add_wwn_groups(struct se_wwn *wwn)
1249839559e1SChristoph Hellwig {
1250839559e1SChristoph Hellwig 	struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1251839559e1SChristoph Hellwig 
1252b7eec2cdSAndy Grover 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1253e48354ceSNicholas Bellinger 			"iscsi_instance", &iscsi_stat_instance_cit);
12541ae1602dSChristoph Hellwig 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_instance_group,
12551ae1602dSChristoph Hellwig 			&tiqn->tiqn_wwn.fabric_stat_group);
12561ae1602dSChristoph Hellwig 
1257b7eec2cdSAndy Grover 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1258e48354ceSNicholas Bellinger 			"iscsi_sess_err", &iscsi_stat_sess_err_cit);
12591ae1602dSChristoph Hellwig 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
12601ae1602dSChristoph Hellwig 			&tiqn->tiqn_wwn.fabric_stat_group);
12611ae1602dSChristoph Hellwig 
1262b7eec2cdSAndy Grover 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1263e48354ceSNicholas Bellinger 			"iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
12641ae1602dSChristoph Hellwig 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
12651ae1602dSChristoph Hellwig 			&tiqn->tiqn_wwn.fabric_stat_group);
12661ae1602dSChristoph Hellwig 
1267b7eec2cdSAndy Grover 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1268e48354ceSNicholas Bellinger 			"iscsi_login_stats", &iscsi_stat_login_cit);
12691ae1602dSChristoph Hellwig 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
12701ae1602dSChristoph Hellwig 			&tiqn->tiqn_wwn.fabric_stat_group);
12711ae1602dSChristoph Hellwig 
1272b7eec2cdSAndy Grover 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1273e48354ceSNicholas Bellinger 			"iscsi_logout_stats", &iscsi_stat_logout_cit);
12741ae1602dSChristoph Hellwig 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
12751ae1602dSChristoph Hellwig 			&tiqn->tiqn_wwn.fabric_stat_group);
1276e48354ceSNicholas Bellinger }
1277e48354ceSNicholas Bellinger 
1278fceb5bc7SChristoph Hellwig static void lio_target_call_coredeltiqn(
1279e48354ceSNicholas Bellinger 	struct se_wwn *wwn)
1280e48354ceSNicholas Bellinger {
1281e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1282e48354ceSNicholas Bellinger 
1283e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1284e48354ceSNicholas Bellinger 			tiqn->tiqn);
1285e48354ceSNicholas Bellinger 	iscsit_del_tiqn(tiqn);
1286e48354ceSNicholas Bellinger }
1287e48354ceSNicholas Bellinger 
1288e48354ceSNicholas Bellinger /* End LIO-Target TIQN struct contig_lio_target_cit */
1289e48354ceSNicholas Bellinger 
1290e48354ceSNicholas Bellinger /* Start lio_target_discovery_auth_cit */
1291e48354ceSNicholas Bellinger 
1292e48354ceSNicholas Bellinger #define DEF_DISC_AUTH_STR(name, flags)					\
1293e48354ceSNicholas Bellinger 	__DEF_NACL_AUTH_STR(disc, name, flags)				\
12942eafd729SChristoph Hellwig static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1295e48354ceSNicholas Bellinger {									\
12962eafd729SChristoph Hellwig 	return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl,\
1297e48354ceSNicholas Bellinger 		page);							\
1298e48354ceSNicholas Bellinger }									\
12992eafd729SChristoph Hellwig static ssize_t iscsi_disc_##name##_store(struct config_item *item,	\
13002eafd729SChristoph Hellwig 		const char *page, size_t count)				\
1301e48354ceSNicholas Bellinger {									\
13022eafd729SChristoph Hellwig 	return __iscsi_disc_##name##_store(&iscsit_global->discovery_acl,	\
1303e48354ceSNicholas Bellinger 		page, count);						\
13042eafd729SChristoph Hellwig 									\
13052eafd729SChristoph Hellwig }									\
13062eafd729SChristoph Hellwig CONFIGFS_ATTR(iscsi_disc_, name)
13072eafd729SChristoph Hellwig 
13082eafd729SChristoph Hellwig DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
13092eafd729SChristoph Hellwig DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
13102eafd729SChristoph Hellwig DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
13112eafd729SChristoph Hellwig DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1312e48354ceSNicholas Bellinger 
1313e48354ceSNicholas Bellinger #define DEF_DISC_AUTH_INT(name)						\
1314e48354ceSNicholas Bellinger 	__DEF_NACL_AUTH_INT(disc, name)					\
13152eafd729SChristoph Hellwig static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1316e48354ceSNicholas Bellinger {									\
13172eafd729SChristoph Hellwig 	return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl, \
1318e48354ceSNicholas Bellinger 			page);						\
13192eafd729SChristoph Hellwig }									\
13202eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(iscsi_disc_, name)
1321e48354ceSNicholas Bellinger 
1322e48354ceSNicholas Bellinger DEF_DISC_AUTH_INT(authenticate_target);
1323e48354ceSNicholas Bellinger 
13242eafd729SChristoph Hellwig 
13252eafd729SChristoph Hellwig static ssize_t iscsi_disc_enforce_discovery_auth_show(struct config_item *item,
1326e48354ceSNicholas Bellinger 		char *page)
1327e48354ceSNicholas Bellinger {
1328e48354ceSNicholas Bellinger 	struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1329e48354ceSNicholas Bellinger 
1330e48354ceSNicholas Bellinger 	return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1331e48354ceSNicholas Bellinger }
1332e48354ceSNicholas Bellinger 
13332eafd729SChristoph Hellwig static ssize_t iscsi_disc_enforce_discovery_auth_store(struct config_item *item,
13342eafd729SChristoph Hellwig 		const char *page, size_t count)
1335e48354ceSNicholas Bellinger {
1336e48354ceSNicholas Bellinger 	struct iscsi_param *param;
1337e48354ceSNicholas Bellinger 	struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1338e48354ceSNicholas Bellinger 	u32 op;
1339ad7babd2SJörn Engel 	int err;
1340e48354ceSNicholas Bellinger 
1341ad7babd2SJörn Engel 	err = kstrtou32(page, 0, &op);
1342ad7babd2SJörn Engel 	if (err)
1343ad7babd2SJörn Engel 		return -EINVAL;
1344e48354ceSNicholas Bellinger 	if ((op != 1) && (op != 0)) {
1345e48354ceSNicholas Bellinger 		pr_err("Illegal value for enforce_discovery_auth:"
1346e48354ceSNicholas Bellinger 				" %u\n", op);
1347e48354ceSNicholas Bellinger 		return -EINVAL;
1348e48354ceSNicholas Bellinger 	}
1349e48354ceSNicholas Bellinger 
1350e48354ceSNicholas Bellinger 	if (!discovery_tpg) {
1351e48354ceSNicholas Bellinger 		pr_err("iscsit_global->discovery_tpg is NULL\n");
1352e48354ceSNicholas Bellinger 		return -EINVAL;
1353e48354ceSNicholas Bellinger 	}
1354e48354ceSNicholas Bellinger 
1355e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD,
1356e48354ceSNicholas Bellinger 				discovery_tpg->param_list);
1357e48354ceSNicholas Bellinger 	if (!param)
1358e48354ceSNicholas Bellinger 		return -EINVAL;
1359e48354ceSNicholas Bellinger 
1360e48354ceSNicholas Bellinger 	if (op) {
1361e48354ceSNicholas Bellinger 		/*
1362e48354ceSNicholas Bellinger 		 * Reset the AuthMethod key to CHAP.
1363e48354ceSNicholas Bellinger 		 */
1364e48354ceSNicholas Bellinger 		if (iscsi_update_param_value(param, CHAP) < 0)
1365e48354ceSNicholas Bellinger 			return -EINVAL;
1366e48354ceSNicholas Bellinger 
1367e48354ceSNicholas Bellinger 		discovery_tpg->tpg_attrib.authentication = 1;
1368e48354ceSNicholas Bellinger 		iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1369e48354ceSNicholas Bellinger 		pr_debug("LIO-CORE[0] Successfully enabled"
1370e48354ceSNicholas Bellinger 			" authentication enforcement for iSCSI"
1371e48354ceSNicholas Bellinger 			" Discovery TPG\n");
1372e48354ceSNicholas Bellinger 	} else {
1373e48354ceSNicholas Bellinger 		/*
1374e48354ceSNicholas Bellinger 		 * Reset the AuthMethod key to CHAP,None
1375e48354ceSNicholas Bellinger 		 */
1376e48354ceSNicholas Bellinger 		if (iscsi_update_param_value(param, "CHAP,None") < 0)
1377e48354ceSNicholas Bellinger 			return -EINVAL;
1378e48354ceSNicholas Bellinger 
1379e48354ceSNicholas Bellinger 		discovery_tpg->tpg_attrib.authentication = 0;
1380e48354ceSNicholas Bellinger 		iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1381e48354ceSNicholas Bellinger 		pr_debug("LIO-CORE[0] Successfully disabled"
1382e48354ceSNicholas Bellinger 			" authentication enforcement for iSCSI"
1383e48354ceSNicholas Bellinger 			" Discovery TPG\n");
1384e48354ceSNicholas Bellinger 	}
1385e48354ceSNicholas Bellinger 
1386e48354ceSNicholas Bellinger 	return count;
1387e48354ceSNicholas Bellinger }
1388e48354ceSNicholas Bellinger 
13892eafd729SChristoph Hellwig CONFIGFS_ATTR(iscsi_disc_, enforce_discovery_auth);
1390e48354ceSNicholas Bellinger 
1391e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
13922eafd729SChristoph Hellwig 	&iscsi_disc_attr_userid,
13932eafd729SChristoph Hellwig 	&iscsi_disc_attr_password,
13942eafd729SChristoph Hellwig 	&iscsi_disc_attr_authenticate_target,
13952eafd729SChristoph Hellwig 	&iscsi_disc_attr_userid_mutual,
13962eafd729SChristoph Hellwig 	&iscsi_disc_attr_password_mutual,
13972eafd729SChristoph Hellwig 	&iscsi_disc_attr_enforce_discovery_auth,
1398e48354ceSNicholas Bellinger 	NULL,
1399e48354ceSNicholas Bellinger };
1400e48354ceSNicholas Bellinger 
1401e48354ceSNicholas Bellinger /* End lio_target_discovery_auth_cit */
1402e48354ceSNicholas Bellinger 
1403e48354ceSNicholas Bellinger /* Start functions for target_core_fabric_ops */
1404e48354ceSNicholas Bellinger 
1405e48354ceSNicholas Bellinger static char *iscsi_get_fabric_name(void)
1406e48354ceSNicholas Bellinger {
1407e48354ceSNicholas Bellinger 	return "iSCSI";
1408e48354ceSNicholas Bellinger }
1409e48354ceSNicholas Bellinger 
1410e48354ceSNicholas Bellinger static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1411e48354ceSNicholas Bellinger {
1412e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1413e48354ceSNicholas Bellinger 
1414e48354ceSNicholas Bellinger 	return cmd->i_state;
1415e48354ceSNicholas Bellinger }
1416e48354ceSNicholas Bellinger 
1417e48354ceSNicholas Bellinger static u32 lio_sess_get_index(struct se_session *se_sess)
1418e48354ceSNicholas Bellinger {
1419e48354ceSNicholas Bellinger 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1420e48354ceSNicholas Bellinger 
1421e48354ceSNicholas Bellinger 	return sess->session_index;
1422e48354ceSNicholas Bellinger }
1423e48354ceSNicholas Bellinger 
1424e48354ceSNicholas Bellinger static u32 lio_sess_get_initiator_sid(
1425e48354ceSNicholas Bellinger 	struct se_session *se_sess,
1426e48354ceSNicholas Bellinger 	unsigned char *buf,
1427e48354ceSNicholas Bellinger 	u32 size)
1428e48354ceSNicholas Bellinger {
1429e48354ceSNicholas Bellinger 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1430e48354ceSNicholas Bellinger 	/*
1431e48354ceSNicholas Bellinger 	 * iSCSI Initiator Session Identifier from RFC-3720.
1432e48354ceSNicholas Bellinger 	 */
14335cdf5a87SAndy Shevchenko 	return snprintf(buf, size, "%6phN", sess->isid);
1434e48354ceSNicholas Bellinger }
1435e48354ceSNicholas Bellinger 
1436e48354ceSNicholas Bellinger static int lio_queue_data_in(struct se_cmd *se_cmd)
1437e48354ceSNicholas Bellinger {
1438e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1439e48354ceSNicholas Bellinger 
1440e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_DATAIN;
14412ec5a8c1SNicholas Bellinger 	cmd->conn->conn_transport->iscsit_queue_data_in(cmd->conn, cmd);
14422ec5a8c1SNicholas Bellinger 
1443e48354ceSNicholas Bellinger 	return 0;
1444e48354ceSNicholas Bellinger }
1445e48354ceSNicholas Bellinger 
1446e48354ceSNicholas Bellinger static int lio_write_pending(struct se_cmd *se_cmd)
1447e48354ceSNicholas Bellinger {
1448e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
14493e1c81a9SNicholas Bellinger 	struct iscsi_conn *conn = cmd->conn;
1450e48354ceSNicholas Bellinger 
1451e48354ceSNicholas Bellinger 	if (!cmd->immediate_data && !cmd->unsolicited_data)
14523e1c81a9SNicholas Bellinger 		return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1453e48354ceSNicholas Bellinger 
1454e48354ceSNicholas Bellinger 	return 0;
1455e48354ceSNicholas Bellinger }
1456e48354ceSNicholas Bellinger 
1457e48354ceSNicholas Bellinger static int lio_write_pending_status(struct se_cmd *se_cmd)
1458e48354ceSNicholas Bellinger {
1459e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1460e48354ceSNicholas Bellinger 	int ret;
1461e48354ceSNicholas Bellinger 
1462e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1463e48354ceSNicholas Bellinger 	ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1464e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1465e48354ceSNicholas Bellinger 
1466e48354ceSNicholas Bellinger 	return ret;
1467e48354ceSNicholas Bellinger }
1468e48354ceSNicholas Bellinger 
1469e48354ceSNicholas Bellinger static int lio_queue_status(struct se_cmd *se_cmd)
1470e48354ceSNicholas Bellinger {
1471e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1472e48354ceSNicholas Bellinger 
1473e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_STATUS;
14745e8e6b4bSNicholas Bellinger 
14755e8e6b4bSNicholas Bellinger 	if (cmd->se_cmd.scsi_status || cmd->sense_reason) {
14765e8e6b4bSNicholas Bellinger 		iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
14775e8e6b4bSNicholas Bellinger 		return 0;
14785e8e6b4bSNicholas Bellinger 	}
14792ec5a8c1SNicholas Bellinger 	cmd->conn->conn_transport->iscsit_queue_status(cmd->conn, cmd);
14802ec5a8c1SNicholas Bellinger 
1481e48354ceSNicholas Bellinger 	return 0;
1482e48354ceSNicholas Bellinger }
1483e48354ceSNicholas Bellinger 
1484b79fafacSJoern Engel static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
1485e48354ceSNicholas Bellinger {
1486e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1487e48354ceSNicholas Bellinger 
1488e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1489e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1490e48354ceSNicholas Bellinger }
1491e48354ceSNicholas Bellinger 
1492131e6abcSNicholas Bellinger static void lio_aborted_task(struct se_cmd *se_cmd)
1493131e6abcSNicholas Bellinger {
1494131e6abcSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1495131e6abcSNicholas Bellinger 
1496131e6abcSNicholas Bellinger 	cmd->conn->conn_transport->iscsit_aborted_task(cmd->conn, cmd);
1497131e6abcSNicholas Bellinger }
1498131e6abcSNicholas Bellinger 
14992b6eb609SChristoph Hellwig static inline struct iscsi_portal_group *iscsi_tpg(struct se_portal_group *se_tpg)
15002b6eb609SChristoph Hellwig {
15012b6eb609SChristoph Hellwig 	return container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
15022b6eb609SChristoph Hellwig }
15032b6eb609SChristoph Hellwig 
1504e48354ceSNicholas Bellinger static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1505e48354ceSNicholas Bellinger {
15062b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn;
1507e48354ceSNicholas Bellinger }
1508e48354ceSNicholas Bellinger 
1509e48354ceSNicholas Bellinger static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1510e48354ceSNicholas Bellinger {
15112b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpgt;
1512e48354ceSNicholas Bellinger }
1513e48354ceSNicholas Bellinger 
1514e48354ceSNicholas Bellinger static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1515e48354ceSNicholas Bellinger {
15162b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_attrib.default_cmdsn_depth;
1517e48354ceSNicholas Bellinger }
1518e48354ceSNicholas Bellinger 
1519e48354ceSNicholas Bellinger static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1520e48354ceSNicholas Bellinger {
15212b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_attrib.generate_node_acls;
1522e48354ceSNicholas Bellinger }
1523e48354ceSNicholas Bellinger 
1524e48354ceSNicholas Bellinger static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1525e48354ceSNicholas Bellinger {
15262b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_attrib.cache_dynamic_acls;
1527e48354ceSNicholas Bellinger }
1528e48354ceSNicholas Bellinger 
1529e48354ceSNicholas Bellinger static int lio_tpg_check_demo_mode_write_protect(
1530e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg)
1531e48354ceSNicholas Bellinger {
15322b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_attrib.demo_mode_write_protect;
1533e48354ceSNicholas Bellinger }
1534e48354ceSNicholas Bellinger 
1535e48354ceSNicholas Bellinger static int lio_tpg_check_prod_mode_write_protect(
1536e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg)
1537e48354ceSNicholas Bellinger {
15382b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_attrib.prod_mode_write_protect;
1539e48354ceSNicholas Bellinger }
1540e48354ceSNicholas Bellinger 
1541901c04a3SNicholas Bellinger static int lio_tpg_check_prot_fabric_only(
1542901c04a3SNicholas Bellinger 	struct se_portal_group *se_tpg)
1543901c04a3SNicholas Bellinger {
1544901c04a3SNicholas Bellinger 	/*
1545901c04a3SNicholas Bellinger 	 * Only report fabric_prot_type if t10_pi has also been enabled
1546901c04a3SNicholas Bellinger 	 * for incoming ib_isert sessions.
1547901c04a3SNicholas Bellinger 	 */
15482b6eb609SChristoph Hellwig 	if (!iscsi_tpg(se_tpg)->tpg_attrib.t10_pi)
1549901c04a3SNicholas Bellinger 		return 0;
15502b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_attrib.fabric_prot_type;
1551901c04a3SNicholas Bellinger }
1552901c04a3SNicholas Bellinger 
1553e48354ceSNicholas Bellinger /*
1554d36ad77fSNicholas Bellinger  * This function calls iscsit_inc_session_usage_count() on the
1555e48354ceSNicholas Bellinger  * struct iscsi_session in question.
1556e48354ceSNicholas Bellinger  */
1557e48354ceSNicholas Bellinger static int lio_tpg_shutdown_session(struct se_session *se_sess)
1558e48354ceSNicholas Bellinger {
1559e48354ceSNicholas Bellinger 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1560d36ad77fSNicholas Bellinger 	struct se_portal_group *se_tpg = &sess->tpg->tpg_se_tpg;
156126a99c19SNicholas Bellinger 
1562d36ad77fSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
1563e48354ceSNicholas Bellinger 	spin_lock(&sess->conn_lock);
1564e48354ceSNicholas Bellinger 	if (atomic_read(&sess->session_fall_back_to_erl0) ||
1565e48354ceSNicholas Bellinger 	    atomic_read(&sess->session_logout) ||
1566e48354ceSNicholas Bellinger 	    (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1567e48354ceSNicholas Bellinger 		spin_unlock(&sess->conn_lock);
1568d36ad77fSNicholas Bellinger 		spin_unlock_bh(&se_tpg->session_lock);
1569e48354ceSNicholas Bellinger 		return 0;
1570e48354ceSNicholas Bellinger 	}
1571e48354ceSNicholas Bellinger 	atomic_set(&sess->session_reinstatement, 1);
1572e48354ceSNicholas Bellinger 	spin_unlock(&sess->conn_lock);
1573e48354ceSNicholas Bellinger 
1574e48354ceSNicholas Bellinger 	iscsit_stop_time2retain_timer(sess);
1575d36ad77fSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
157626a99c19SNicholas Bellinger 
157799367f01SNicholas Bellinger 	iscsit_stop_session(sess, 1, 1);
1578e48354ceSNicholas Bellinger 	return 1;
1579e48354ceSNicholas Bellinger }
1580e48354ceSNicholas Bellinger 
1581e48354ceSNicholas Bellinger /*
1582e48354ceSNicholas Bellinger  * Calls iscsit_dec_session_usage_count() as inverse of
1583e48354ceSNicholas Bellinger  * lio_tpg_shutdown_session()
1584e48354ceSNicholas Bellinger  */
1585e48354ceSNicholas Bellinger static void lio_tpg_close_session(struct se_session *se_sess)
1586e48354ceSNicholas Bellinger {
1587e48354ceSNicholas Bellinger 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1588e48354ceSNicholas Bellinger 	/*
1589e48354ceSNicholas Bellinger 	 * If the iSCSI Session for the iSCSI Initiator Node exists,
1590e48354ceSNicholas Bellinger 	 * forcefully shutdown the iSCSI NEXUS.
1591e48354ceSNicholas Bellinger 	 */
1592e48354ceSNicholas Bellinger 	iscsit_close_session(sess);
1593e48354ceSNicholas Bellinger }
1594e48354ceSNicholas Bellinger 
1595e48354ceSNicholas Bellinger static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1596e48354ceSNicholas Bellinger {
15972b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn_index;
1598e48354ceSNicholas Bellinger }
1599e48354ceSNicholas Bellinger 
1600e48354ceSNicholas Bellinger static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1601e48354ceSNicholas Bellinger {
1602e48354ceSNicholas Bellinger 	struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1603e48354ceSNicholas Bellinger 				se_node_acl);
1604d1fa7a1dSNicholas Bellinger 	struct se_portal_group *se_tpg = se_acl->se_tpg;
1605d1fa7a1dSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1606d1fa7a1dSNicholas Bellinger 				struct iscsi_portal_group, tpg_se_tpg);
1607e48354ceSNicholas Bellinger 
1608b7eec2cdSAndy Grover 	acl->node_attrib.nacl = acl;
1609d1fa7a1dSNicholas Bellinger 	iscsit_set_default_node_attribues(acl, tpg);
1610e48354ceSNicholas Bellinger }
1611e48354ceSNicholas Bellinger 
16123e1c81a9SNicholas Bellinger static int lio_check_stop_free(struct se_cmd *se_cmd)
16133e1c81a9SNicholas Bellinger {
1614afc16604SBart Van Assche 	return target_put_sess_cmd(se_cmd);
16153e1c81a9SNicholas Bellinger }
16163e1c81a9SNicholas Bellinger 
1617e48354ceSNicholas Bellinger static void lio_release_cmd(struct se_cmd *se_cmd)
1618e48354ceSNicholas Bellinger {
1619e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1620e48354ceSNicholas Bellinger 
1621cdb72665SNicholas Bellinger 	pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
1622d703ce2fSNicholas Bellinger 	iscsit_release_cmd(cmd);
1623e48354ceSNicholas Bellinger }
1624e48354ceSNicholas Bellinger 
16259ac8928eSChristoph Hellwig const struct target_core_fabric_ops iscsi_ops = {
16269ac8928eSChristoph Hellwig 	.module				= THIS_MODULE,
16279ac8928eSChristoph Hellwig 	.name				= "iscsi",
1628144bc4c2SChristoph Hellwig 	.node_acl_size			= sizeof(struct iscsi_node_acl),
16299ac8928eSChristoph Hellwig 	.get_fabric_name		= iscsi_get_fabric_name,
16309ac8928eSChristoph Hellwig 	.tpg_get_wwn			= lio_tpg_get_endpoint_wwn,
16319ac8928eSChristoph Hellwig 	.tpg_get_tag			= lio_tpg_get_tag,
16329ac8928eSChristoph Hellwig 	.tpg_get_default_depth		= lio_tpg_get_default_depth,
16339ac8928eSChristoph Hellwig 	.tpg_check_demo_mode		= lio_tpg_check_demo_mode,
16349ac8928eSChristoph Hellwig 	.tpg_check_demo_mode_cache	= lio_tpg_check_demo_mode_cache,
16359ac8928eSChristoph Hellwig 	.tpg_check_demo_mode_write_protect =
16369ac8928eSChristoph Hellwig 			lio_tpg_check_demo_mode_write_protect,
16379ac8928eSChristoph Hellwig 	.tpg_check_prod_mode_write_protect =
16389ac8928eSChristoph Hellwig 			lio_tpg_check_prod_mode_write_protect,
16399ac8928eSChristoph Hellwig 	.tpg_check_prot_fabric_only	= &lio_tpg_check_prot_fabric_only,
16409ac8928eSChristoph Hellwig 	.tpg_get_inst_index		= lio_tpg_get_inst_index,
16419ac8928eSChristoph Hellwig 	.check_stop_free		= lio_check_stop_free,
16429ac8928eSChristoph Hellwig 	.release_cmd			= lio_release_cmd,
16439ac8928eSChristoph Hellwig 	.shutdown_session		= lio_tpg_shutdown_session,
16449ac8928eSChristoph Hellwig 	.close_session			= lio_tpg_close_session,
16459ac8928eSChristoph Hellwig 	.sess_get_index			= lio_sess_get_index,
16469ac8928eSChristoph Hellwig 	.sess_get_initiator_sid		= lio_sess_get_initiator_sid,
16479ac8928eSChristoph Hellwig 	.write_pending			= lio_write_pending,
16489ac8928eSChristoph Hellwig 	.write_pending_status		= lio_write_pending_status,
16499ac8928eSChristoph Hellwig 	.set_default_node_attributes	= lio_set_default_node_attributes,
16509ac8928eSChristoph Hellwig 	.get_cmd_state			= iscsi_get_cmd_state,
16519ac8928eSChristoph Hellwig 	.queue_data_in			= lio_queue_data_in,
16529ac8928eSChristoph Hellwig 	.queue_status			= lio_queue_status,
16539ac8928eSChristoph Hellwig 	.queue_tm_rsp			= lio_queue_tm_rsp,
16549ac8928eSChristoph Hellwig 	.aborted_task			= lio_aborted_task,
16559ac8928eSChristoph Hellwig 	.fabric_make_wwn		= lio_target_call_coreaddtiqn,
16569ac8928eSChristoph Hellwig 	.fabric_drop_wwn		= lio_target_call_coredeltiqn,
1657839559e1SChristoph Hellwig 	.add_wwn_groups			= lio_target_add_wwn_groups,
16589ac8928eSChristoph Hellwig 	.fabric_make_tpg		= lio_target_tiqn_addtpg,
16599ac8928eSChristoph Hellwig 	.fabric_drop_tpg		= lio_target_tiqn_deltpg,
16609ac8928eSChristoph Hellwig 	.fabric_make_np			= lio_target_call_addnptotpg,
16619ac8928eSChristoph Hellwig 	.fabric_drop_np			= lio_target_call_delnpfromtpg,
1662c7d6a803SChristoph Hellwig 	.fabric_init_nodeacl		= lio_target_init_nodeacl,
1663e48354ceSNicholas Bellinger 
16649ac8928eSChristoph Hellwig 	.tfc_discovery_attrs		= lio_target_discovery_auth_attrs,
16659ac8928eSChristoph Hellwig 	.tfc_wwn_attrs			= lio_target_wwn_attrs,
16669ac8928eSChristoph Hellwig 	.tfc_tpg_base_attrs		= lio_target_tpg_attrs,
16679ac8928eSChristoph Hellwig 	.tfc_tpg_attrib_attrs		= lio_target_tpg_attrib_attrs,
16689ac8928eSChristoph Hellwig 	.tfc_tpg_auth_attrs		= lio_target_tpg_auth_attrs,
16699ac8928eSChristoph Hellwig 	.tfc_tpg_param_attrs		= lio_target_tpg_param_attrs,
16709ac8928eSChristoph Hellwig 	.tfc_tpg_np_base_attrs		= lio_target_portal_attrs,
16719ac8928eSChristoph Hellwig 	.tfc_tpg_nacl_base_attrs	= lio_target_initiator_attrs,
16729ac8928eSChristoph Hellwig 	.tfc_tpg_nacl_attrib_attrs	= lio_target_nacl_attrib_attrs,
16739ac8928eSChristoph Hellwig 	.tfc_tpg_nacl_auth_attrs	= lio_target_nacl_auth_attrs,
16749ac8928eSChristoph Hellwig 	.tfc_tpg_nacl_param_attrs	= lio_target_nacl_param_attrs,
16759ac8928eSChristoph Hellwig };
1676