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 
782c7d6a803SChristoph Hellwig static void lio_target_cleanup_nodeacl( struct se_node_acl *se_nacl)
783e48354ceSNicholas Bellinger {
784e48354ceSNicholas Bellinger 	struct iscsi_node_acl *acl = container_of(se_nacl,
785e48354ceSNicholas Bellinger 			struct iscsi_node_acl, se_node_acl);
786e48354ceSNicholas Bellinger 
7871ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&acl->se_node_acl.acl_fabric_stat_group);
788e48354ceSNicholas Bellinger }
789e48354ceSNicholas Bellinger 
790e48354ceSNicholas Bellinger /* End items for lio_target_acl_cit */
791e48354ceSNicholas Bellinger 
792e48354ceSNicholas Bellinger /* Start items for lio_target_tpg_attrib_cit */
793e48354ceSNicholas Bellinger 
794e48354ceSNicholas Bellinger #define DEF_TPG_ATTRIB(name)						\
795e48354ceSNicholas Bellinger 									\
7962eafd729SChristoph Hellwig static ssize_t iscsi_tpg_attrib_##name##_show(struct config_item *item,	\
797e48354ceSNicholas Bellinger 		char *page)						\
798e48354ceSNicholas Bellinger {									\
7992eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = attrib_to_tpg(item);		\
800e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
801e48354ceSNicholas Bellinger 			struct iscsi_portal_group, tpg_se_tpg);	\
802e48354ceSNicholas Bellinger 	ssize_t rb;							\
803e48354ceSNicholas Bellinger 									\
804e48354ceSNicholas Bellinger 	if (iscsit_get_tpg(tpg) < 0)					\
805e48354ceSNicholas Bellinger 		return -EINVAL;						\
806e48354ceSNicholas Bellinger 									\
807b7eec2cdSAndy Grover 	rb = sprintf(page, "%u\n", tpg->tpg_attrib.name);		\
808e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);						\
809e48354ceSNicholas Bellinger 	return rb;							\
810e48354ceSNicholas Bellinger }									\
811e48354ceSNicholas Bellinger 									\
8122eafd729SChristoph Hellwig static ssize_t iscsi_tpg_attrib_##name##_store(struct config_item *item,\
8132eafd729SChristoph Hellwig 		const char *page, size_t count)				\
814e48354ceSNicholas Bellinger {									\
8152eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = attrib_to_tpg(item);		\
816e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
817e48354ceSNicholas Bellinger 			struct iscsi_portal_group, tpg_se_tpg);	\
818e48354ceSNicholas Bellinger 	u32 val;							\
819e48354ceSNicholas Bellinger 	int ret;							\
820e48354ceSNicholas Bellinger 									\
821e48354ceSNicholas Bellinger 	if (iscsit_get_tpg(tpg) < 0)					\
822e48354ceSNicholas Bellinger 		return -EINVAL;						\
823e48354ceSNicholas Bellinger 									\
824ad7babd2SJörn Engel 	ret = kstrtou32(page, 0, &val);					\
825ad7babd2SJörn Engel 	if (ret)							\
826ad7babd2SJörn Engel 		goto out;						\
827e48354ceSNicholas Bellinger 	ret = iscsit_ta_##name(tpg, val);				\
828e48354ceSNicholas Bellinger 	if (ret < 0)							\
829e48354ceSNicholas Bellinger 		goto out;						\
830e48354ceSNicholas Bellinger 									\
831e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);						\
832e48354ceSNicholas Bellinger 	return count;							\
833e48354ceSNicholas Bellinger out:									\
834e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);						\
835e48354ceSNicholas Bellinger 	return ret;							\
8362eafd729SChristoph Hellwig }									\
8372eafd729SChristoph Hellwig CONFIGFS_ATTR(iscsi_tpg_attrib_, name)
838e48354ceSNicholas Bellinger 
839e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(authentication);
840e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(login_timeout);
841e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(netif_timeout);
842e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(generate_node_acls);
843e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(default_cmdsn_depth);
844e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(cache_dynamic_acls);
845e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(demo_mode_write_protect);
846e48354ceSNicholas Bellinger DEF_TPG_ATTRIB(prod_mode_write_protect);
8474c54b6cfSThomas Glanzmann DEF_TPG_ATTRIB(demo_mode_discovery);
848d1fa7a1dSNicholas Bellinger DEF_TPG_ATTRIB(default_erl);
8498085176fSSagi Grimberg DEF_TPG_ATTRIB(t10_pi);
850901c04a3SNicholas Bellinger DEF_TPG_ATTRIB(fabric_prot_type);
851a6415cddSDavid Disseldorp DEF_TPG_ATTRIB(tpg_enabled_sendtargets);
852e48354ceSNicholas Bellinger 
853e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
8542eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_authentication,
8552eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_login_timeout,
8562eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_netif_timeout,
8572eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_generate_node_acls,
8582eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_default_cmdsn_depth,
8592eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_cache_dynamic_acls,
8602eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_demo_mode_write_protect,
8612eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_prod_mode_write_protect,
8622eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_demo_mode_discovery,
8632eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_default_erl,
8642eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_t10_pi,
8652eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_fabric_prot_type,
8662eafd729SChristoph Hellwig 	&iscsi_tpg_attrib_attr_tpg_enabled_sendtargets,
867e48354ceSNicholas Bellinger 	NULL,
868e48354ceSNicholas Bellinger };
869e48354ceSNicholas Bellinger 
870e48354ceSNicholas Bellinger /* End items for lio_target_tpg_attrib_cit */
871e48354ceSNicholas Bellinger 
872c3e51442SNicholas Bellinger /* Start items for lio_target_tpg_auth_cit */
873c3e51442SNicholas Bellinger 
874c3e51442SNicholas Bellinger #define __DEF_TPG_AUTH_STR(prefix, name, flags)					\
8752eafd729SChristoph Hellwig static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg,	\
876c3e51442SNicholas Bellinger 		char *page)							\
877c3e51442SNicholas Bellinger {										\
878c3e51442SNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,			\
879c3e51442SNicholas Bellinger 				struct iscsi_portal_group, tpg_se_tpg);		\
880c3e51442SNicholas Bellinger 	struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;			\
881c3e51442SNicholas Bellinger 										\
882c3e51442SNicholas Bellinger 	if (!capable(CAP_SYS_ADMIN))						\
883c3e51442SNicholas Bellinger 		return -EPERM;							\
884c3e51442SNicholas Bellinger 										\
885c3e51442SNicholas Bellinger 	return snprintf(page, PAGE_SIZE, "%s\n", auth->name);			\
886c3e51442SNicholas Bellinger }										\
887c3e51442SNicholas Bellinger 										\
8882eafd729SChristoph Hellwig static ssize_t __iscsi_##prefix##_##name##_store(struct se_portal_group *se_tpg,\
8892eafd729SChristoph Hellwig 		const char *page, size_t count)					\
890c3e51442SNicholas Bellinger {										\
891c3e51442SNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,			\
892c3e51442SNicholas Bellinger 				struct iscsi_portal_group, tpg_se_tpg);		\
893c3e51442SNicholas Bellinger 	struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;			\
894c3e51442SNicholas Bellinger 										\
895c3e51442SNicholas Bellinger 	if (!capable(CAP_SYS_ADMIN))						\
896c3e51442SNicholas Bellinger 		return -EPERM;							\
897c3e51442SNicholas Bellinger 										\
89837b32c6fSNicholas Bellinger 	snprintf(auth->name, sizeof(auth->name), "%s", page);			\
899c3e51442SNicholas Bellinger 	if (!(strncmp("NULL", auth->name, 4)))					\
900c3e51442SNicholas Bellinger 		auth->naf_flags &= ~flags;					\
901c3e51442SNicholas Bellinger 	else									\
902c3e51442SNicholas Bellinger 		auth->naf_flags |= flags;					\
903c3e51442SNicholas Bellinger 										\
904c3e51442SNicholas Bellinger 	if ((auth->naf_flags & NAF_USERID_IN_SET) &&				\
905c3e51442SNicholas Bellinger 	    (auth->naf_flags & NAF_PASSWORD_IN_SET))				\
906c3e51442SNicholas Bellinger 		auth->authenticate_target = 1;					\
907c3e51442SNicholas Bellinger 	else									\
908c3e51442SNicholas Bellinger 		auth->authenticate_target = 0;					\
909c3e51442SNicholas Bellinger 										\
910c3e51442SNicholas Bellinger 	return count;								\
911c3e51442SNicholas Bellinger }
912c3e51442SNicholas Bellinger 
9132eafd729SChristoph Hellwig #define DEF_TPG_AUTH_STR(name, flags)						\
9142eafd729SChristoph Hellwig 	__DEF_TPG_AUTH_STR(tpg_auth, name, flags)				\
9152eafd729SChristoph Hellwig static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item,		\
9162eafd729SChristoph Hellwig 		char *page)							\
9172eafd729SChristoph Hellwig {										\
9182eafd729SChristoph Hellwig 	return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page);		\
9192eafd729SChristoph Hellwig }										\
9202eafd729SChristoph Hellwig 										\
9212eafd729SChristoph Hellwig static ssize_t iscsi_tpg_auth_##name##_store(struct config_item *item,		\
9222eafd729SChristoph Hellwig 		const char *page, size_t count)					\
9232eafd729SChristoph Hellwig {										\
9242eafd729SChristoph Hellwig 	return __iscsi_tpg_auth_##name##_store(auth_to_tpg(item), page, count);	\
9252eafd729SChristoph Hellwig }										\
9262eafd729SChristoph Hellwig 										\
9272eafd729SChristoph Hellwig CONFIGFS_ATTR(iscsi_tpg_auth_, name);
9282eafd729SChristoph Hellwig 
9292eafd729SChristoph Hellwig 
9302eafd729SChristoph Hellwig DEF_TPG_AUTH_STR(userid, NAF_USERID_SET);
9312eafd729SChristoph Hellwig DEF_TPG_AUTH_STR(password, NAF_PASSWORD_SET);
9322eafd729SChristoph Hellwig DEF_TPG_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
9332eafd729SChristoph Hellwig DEF_TPG_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
9342eafd729SChristoph Hellwig 
935c3e51442SNicholas Bellinger #define __DEF_TPG_AUTH_INT(prefix, name)					\
9362eafd729SChristoph Hellwig static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg,	\
937c3e51442SNicholas Bellinger 		char *page)								\
938c3e51442SNicholas Bellinger {										\
939c3e51442SNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,			\
940c3e51442SNicholas Bellinger 				struct iscsi_portal_group, tpg_se_tpg);		\
941c3e51442SNicholas Bellinger 	struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;			\
942c3e51442SNicholas Bellinger 										\
943c3e51442SNicholas Bellinger 	if (!capable(CAP_SYS_ADMIN))						\
944c3e51442SNicholas Bellinger 		return -EPERM;							\
945c3e51442SNicholas Bellinger 										\
946c3e51442SNicholas Bellinger 	return snprintf(page, PAGE_SIZE, "%d\n", auth->name);			\
947c3e51442SNicholas Bellinger }
948c3e51442SNicholas Bellinger 
949c3e51442SNicholas Bellinger #define DEF_TPG_AUTH_INT(name)							\
950c3e51442SNicholas Bellinger 	__DEF_TPG_AUTH_INT(tpg_auth, name)					\
9512eafd729SChristoph Hellwig static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item,		\
952c3e51442SNicholas Bellinger 		char *page) \
953c3e51442SNicholas Bellinger {										\
9542eafd729SChristoph Hellwig 	return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page);		\
9552eafd729SChristoph Hellwig }										\
9562eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(iscsi_tpg_auth_, name);
957c3e51442SNicholas Bellinger 
958c3e51442SNicholas Bellinger DEF_TPG_AUTH_INT(authenticate_target);
959c3e51442SNicholas Bellinger 
960c3e51442SNicholas Bellinger static struct configfs_attribute *lio_target_tpg_auth_attrs[] = {
9612eafd729SChristoph Hellwig 	&iscsi_tpg_auth_attr_userid,
9622eafd729SChristoph Hellwig 	&iscsi_tpg_auth_attr_password,
9632eafd729SChristoph Hellwig 	&iscsi_tpg_auth_attr_authenticate_target,
9642eafd729SChristoph Hellwig 	&iscsi_tpg_auth_attr_userid_mutual,
9652eafd729SChristoph Hellwig 	&iscsi_tpg_auth_attr_password_mutual,
966c3e51442SNicholas Bellinger 	NULL,
967c3e51442SNicholas Bellinger };
968c3e51442SNicholas Bellinger 
969c3e51442SNicholas Bellinger /* End items for lio_target_tpg_auth_cit */
970c3e51442SNicholas Bellinger 
971e48354ceSNicholas Bellinger /* Start items for lio_target_tpg_param_cit */
972e48354ceSNicholas Bellinger 
973e48354ceSNicholas Bellinger #define DEF_TPG_PARAM(name)						\
9742eafd729SChristoph Hellwig static ssize_t iscsi_tpg_param_##name##_show(struct config_item *item,	\
975e48354ceSNicholas Bellinger 		char *page)						\
976e48354ceSNicholas Bellinger {									\
9772eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = param_to_tpg(item);		\
978e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
979e48354ceSNicholas Bellinger 			struct iscsi_portal_group, tpg_se_tpg);		\
980e48354ceSNicholas Bellinger 	struct iscsi_param *param;					\
981e48354ceSNicholas Bellinger 	ssize_t rb;							\
982e48354ceSNicholas Bellinger 									\
983e48354ceSNicholas Bellinger 	if (iscsit_get_tpg(tpg) < 0)					\
984e48354ceSNicholas Bellinger 		return -EINVAL;						\
985e48354ceSNicholas Bellinger 									\
986e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(__stringify(name),		\
987e48354ceSNicholas Bellinger 				tpg->param_list);			\
988e48354ceSNicholas Bellinger 	if (!param) {							\
989e48354ceSNicholas Bellinger 		iscsit_put_tpg(tpg);					\
990e48354ceSNicholas Bellinger 		return -EINVAL;						\
991e48354ceSNicholas Bellinger 	}								\
992e48354ceSNicholas Bellinger 	rb = snprintf(page, PAGE_SIZE, "%s\n", param->value);		\
993e48354ceSNicholas Bellinger 									\
994e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);						\
995e48354ceSNicholas Bellinger 	return rb;							\
996e48354ceSNicholas Bellinger }									\
9972eafd729SChristoph Hellwig static ssize_t iscsi_tpg_param_##name##_store(struct config_item *item, \
9982eafd729SChristoph Hellwig 		const char *page, size_t count)				\
999e48354ceSNicholas Bellinger {									\
10002eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = param_to_tpg(item);		\
1001e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
1002e48354ceSNicholas Bellinger 			struct iscsi_portal_group, tpg_se_tpg);		\
1003e48354ceSNicholas Bellinger 	char *buf;							\
1004ad7babd2SJörn Engel 	int ret, len;							\
1005e48354ceSNicholas Bellinger 									\
1006e48354ceSNicholas Bellinger 	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);				\
1007e48354ceSNicholas Bellinger 	if (!buf)							\
1008e48354ceSNicholas Bellinger 		return -ENOMEM;						\
1009ad7babd2SJörn Engel 	len = snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page);	\
1010ad7babd2SJörn Engel 	if (isspace(buf[len-1]))					\
1011ad7babd2SJörn Engel 		buf[len-1] = '\0'; /* Kill newline */			\
1012e48354ceSNicholas Bellinger 									\
1013e48354ceSNicholas Bellinger 	if (iscsit_get_tpg(tpg) < 0) {					\
1014e48354ceSNicholas Bellinger 		kfree(buf);						\
1015e48354ceSNicholas Bellinger 		return -EINVAL;						\
1016e48354ceSNicholas Bellinger 	}								\
1017e48354ceSNicholas Bellinger 									\
1018e48354ceSNicholas Bellinger 	ret = iscsi_change_param_value(buf, tpg->param_list, 1);	\
1019e48354ceSNicholas Bellinger 	if (ret < 0)							\
1020e48354ceSNicholas Bellinger 		goto out;						\
1021e48354ceSNicholas Bellinger 									\
1022e48354ceSNicholas Bellinger 	kfree(buf);							\
1023e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);						\
1024e48354ceSNicholas Bellinger 	return count;							\
1025e48354ceSNicholas Bellinger out:									\
1026e48354ceSNicholas Bellinger 	kfree(buf);							\
1027e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);						\
1028e48354ceSNicholas Bellinger 	return -EINVAL;							\
10292eafd729SChristoph Hellwig }									\
10302eafd729SChristoph Hellwig CONFIGFS_ATTR(iscsi_tpg_param_, name)
1031e48354ceSNicholas Bellinger 
1032e48354ceSNicholas Bellinger DEF_TPG_PARAM(AuthMethod);
1033e48354ceSNicholas Bellinger DEF_TPG_PARAM(HeaderDigest);
1034e48354ceSNicholas Bellinger DEF_TPG_PARAM(DataDigest);
1035e48354ceSNicholas Bellinger DEF_TPG_PARAM(MaxConnections);
1036e48354ceSNicholas Bellinger DEF_TPG_PARAM(TargetAlias);
1037e48354ceSNicholas Bellinger DEF_TPG_PARAM(InitialR2T);
1038e48354ceSNicholas Bellinger DEF_TPG_PARAM(ImmediateData);
1039e48354ceSNicholas Bellinger DEF_TPG_PARAM(MaxRecvDataSegmentLength);
1040e004cb25SNicholas Bellinger DEF_TPG_PARAM(MaxXmitDataSegmentLength);
1041e48354ceSNicholas Bellinger DEF_TPG_PARAM(MaxBurstLength);
1042e48354ceSNicholas Bellinger DEF_TPG_PARAM(FirstBurstLength);
1043e48354ceSNicholas Bellinger DEF_TPG_PARAM(DefaultTime2Wait);
1044e48354ceSNicholas Bellinger DEF_TPG_PARAM(DefaultTime2Retain);
1045e48354ceSNicholas Bellinger DEF_TPG_PARAM(MaxOutstandingR2T);
1046e48354ceSNicholas Bellinger DEF_TPG_PARAM(DataPDUInOrder);
1047e48354ceSNicholas Bellinger DEF_TPG_PARAM(DataSequenceInOrder);
1048e48354ceSNicholas Bellinger DEF_TPG_PARAM(ErrorRecoveryLevel);
1049e48354ceSNicholas Bellinger DEF_TPG_PARAM(IFMarker);
1050e48354ceSNicholas Bellinger DEF_TPG_PARAM(OFMarker);
1051e48354ceSNicholas Bellinger DEF_TPG_PARAM(IFMarkInt);
1052e48354ceSNicholas Bellinger DEF_TPG_PARAM(OFMarkInt);
1053e48354ceSNicholas Bellinger 
1054e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
10552eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_AuthMethod,
10562eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_HeaderDigest,
10572eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_DataDigest,
10582eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_MaxConnections,
10592eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_TargetAlias,
10602eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_InitialR2T,
10612eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_ImmediateData,
10622eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_MaxRecvDataSegmentLength,
10632eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_MaxXmitDataSegmentLength,
10642eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_MaxBurstLength,
10652eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_FirstBurstLength,
10662eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_DefaultTime2Wait,
10672eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_DefaultTime2Retain,
10682eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_MaxOutstandingR2T,
10692eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_DataPDUInOrder,
10702eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_DataSequenceInOrder,
10712eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_ErrorRecoveryLevel,
10722eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_IFMarker,
10732eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_OFMarker,
10742eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_IFMarkInt,
10752eafd729SChristoph Hellwig 	&iscsi_tpg_param_attr_OFMarkInt,
1076e48354ceSNicholas Bellinger 	NULL,
1077e48354ceSNicholas Bellinger };
1078e48354ceSNicholas Bellinger 
1079e48354ceSNicholas Bellinger /* End items for lio_target_tpg_param_cit */
1080e48354ceSNicholas Bellinger 
1081e48354ceSNicholas Bellinger /* Start items for lio_target_tpg_cit */
1082e48354ceSNicholas Bellinger 
10832eafd729SChristoph Hellwig static ssize_t lio_target_tpg_enable_show(struct config_item *item, char *page)
1084e48354ceSNicholas Bellinger {
10852eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = to_tpg(item);
1086e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1087e48354ceSNicholas Bellinger 			struct iscsi_portal_group, tpg_se_tpg);
1088e48354ceSNicholas Bellinger 	ssize_t len;
1089e48354ceSNicholas Bellinger 
1090e48354ceSNicholas Bellinger 	spin_lock(&tpg->tpg_state_lock);
1091e48354ceSNicholas Bellinger 	len = sprintf(page, "%d\n",
1092e48354ceSNicholas Bellinger 			(tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1093e48354ceSNicholas Bellinger 	spin_unlock(&tpg->tpg_state_lock);
1094e48354ceSNicholas Bellinger 
1095e48354ceSNicholas Bellinger 	return len;
1096e48354ceSNicholas Bellinger }
1097e48354ceSNicholas Bellinger 
10982eafd729SChristoph Hellwig static ssize_t lio_target_tpg_enable_store(struct config_item *item,
10992eafd729SChristoph Hellwig 		const char *page, size_t count)
1100e48354ceSNicholas Bellinger {
11012eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = to_tpg(item);
1102e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1103e48354ceSNicholas Bellinger 			struct iscsi_portal_group, tpg_se_tpg);
1104e48354ceSNicholas Bellinger 	u32 op;
1105ad7babd2SJörn Engel 	int ret;
1106e48354ceSNicholas Bellinger 
1107ad7babd2SJörn Engel 	ret = kstrtou32(page, 0, &op);
1108ad7babd2SJörn Engel 	if (ret)
1109ad7babd2SJörn Engel 		return ret;
1110e48354ceSNicholas Bellinger 	if ((op != 1) && (op != 0)) {
1111e48354ceSNicholas Bellinger 		pr_err("Illegal value for tpg_enable: %u\n", op);
1112e48354ceSNicholas Bellinger 		return -EINVAL;
1113e48354ceSNicholas Bellinger 	}
1114e48354ceSNicholas Bellinger 
1115e48354ceSNicholas Bellinger 	ret = iscsit_get_tpg(tpg);
1116e48354ceSNicholas Bellinger 	if (ret < 0)
1117e48354ceSNicholas Bellinger 		return -EINVAL;
1118e48354ceSNicholas Bellinger 
1119e48354ceSNicholas Bellinger 	if (op) {
1120e48354ceSNicholas Bellinger 		ret = iscsit_tpg_enable_portal_group(tpg);
1121e48354ceSNicholas Bellinger 		if (ret < 0)
1122e48354ceSNicholas Bellinger 			goto out;
1123e48354ceSNicholas Bellinger 	} else {
1124e48354ceSNicholas Bellinger 		/*
1125e48354ceSNicholas Bellinger 		 * iscsit_tpg_disable_portal_group() assumes force=1
1126e48354ceSNicholas Bellinger 		 */
1127e48354ceSNicholas Bellinger 		ret = iscsit_tpg_disable_portal_group(tpg, 1);
1128e48354ceSNicholas Bellinger 		if (ret < 0)
1129e48354ceSNicholas Bellinger 			goto out;
1130e48354ceSNicholas Bellinger 	}
1131e48354ceSNicholas Bellinger 
1132e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);
1133e48354ceSNicholas Bellinger 	return count;
1134e48354ceSNicholas Bellinger out:
1135e48354ceSNicholas Bellinger 	iscsit_put_tpg(tpg);
1136e48354ceSNicholas Bellinger 	return -EINVAL;
1137e48354ceSNicholas Bellinger }
1138e48354ceSNicholas Bellinger 
1139e48354ceSNicholas Bellinger 
11402eafd729SChristoph Hellwig static ssize_t lio_target_tpg_dynamic_sessions_show(struct config_item *item,
1141d4ee46ffSNicholas Bellinger 		char *page)
1142d4ee46ffSNicholas Bellinger {
11432eafd729SChristoph Hellwig 	return target_show_dynamic_sessions(to_tpg(item), page);
1144d4ee46ffSNicholas Bellinger }
1145d4ee46ffSNicholas Bellinger 
11462eafd729SChristoph Hellwig CONFIGFS_ATTR(lio_target_tpg_, enable);
11472eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(lio_target_tpg_, dynamic_sessions);
1148d4ee46ffSNicholas Bellinger 
1149e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_tpg_attrs[] = {
11502eafd729SChristoph Hellwig 	&lio_target_tpg_attr_enable,
11512eafd729SChristoph Hellwig 	&lio_target_tpg_attr_dynamic_sessions,
1152e48354ceSNicholas Bellinger 	NULL,
1153e48354ceSNicholas Bellinger };
1154e48354ceSNicholas Bellinger 
1155e48354ceSNicholas Bellinger /* End items for lio_target_tpg_cit */
1156e48354ceSNicholas Bellinger 
1157e48354ceSNicholas Bellinger /* Start items for lio_target_tiqn_cit */
1158e48354ceSNicholas Bellinger 
1159fceb5bc7SChristoph Hellwig static struct se_portal_group *lio_target_tiqn_addtpg(
1160e48354ceSNicholas Bellinger 	struct se_wwn *wwn,
1161e48354ceSNicholas Bellinger 	struct config_group *group,
1162e48354ceSNicholas Bellinger 	const char *name)
1163e48354ceSNicholas Bellinger {
1164e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg;
1165e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
1166ad7babd2SJörn Engel 	char *tpgt_str;
1167ad7babd2SJörn Engel 	int ret;
1168ad7babd2SJörn Engel 	u16 tpgt;
1169e48354ceSNicholas Bellinger 
1170e48354ceSNicholas Bellinger 	tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1171e48354ceSNicholas Bellinger 	/*
1172e48354ceSNicholas Bellinger 	 * Only tpgt_# directory groups can be created below
1173e48354ceSNicholas Bellinger 	 * target/iscsi/iqn.superturodiskarry/
1174e48354ceSNicholas Bellinger 	 */
1175e48354ceSNicholas Bellinger 	tpgt_str = strstr(name, "tpgt_");
1176e48354ceSNicholas Bellinger 	if (!tpgt_str) {
1177e48354ceSNicholas Bellinger 		pr_err("Unable to locate \"tpgt_#\" directory"
1178e48354ceSNicholas Bellinger 				" group\n");
1179e48354ceSNicholas Bellinger 		return NULL;
1180e48354ceSNicholas Bellinger 	}
1181e48354ceSNicholas Bellinger 	tpgt_str += 5; /* Skip ahead of "tpgt_" */
1182ad7babd2SJörn Engel 	ret = kstrtou16(tpgt_str, 0, &tpgt);
1183ad7babd2SJörn Engel 	if (ret)
1184ad7babd2SJörn Engel 		return NULL;
1185e48354ceSNicholas Bellinger 
1186e48354ceSNicholas Bellinger 	tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1187e48354ceSNicholas Bellinger 	if (!tpg)
1188e48354ceSNicholas Bellinger 		return NULL;
1189e48354ceSNicholas Bellinger 
1190bc0c94b1SNicholas Bellinger 	ret = core_tpg_register(wwn, &tpg->tpg_se_tpg, SCSI_PROTOCOL_ISCSI);
1191e48354ceSNicholas Bellinger 	if (ret < 0)
1192e48354ceSNicholas Bellinger 		return NULL;
1193e48354ceSNicholas Bellinger 
1194e48354ceSNicholas Bellinger 	ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1195e48354ceSNicholas Bellinger 	if (ret != 0)
1196e48354ceSNicholas Bellinger 		goto out;
1197e48354ceSNicholas Bellinger 
1198e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1199e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1200e48354ceSNicholas Bellinger 			name);
1201e48354ceSNicholas Bellinger 	return &tpg->tpg_se_tpg;
1202e48354ceSNicholas Bellinger out:
1203e48354ceSNicholas Bellinger 	core_tpg_deregister(&tpg->tpg_se_tpg);
1204e48354ceSNicholas Bellinger 	kfree(tpg);
1205e48354ceSNicholas Bellinger 	return NULL;
1206e48354ceSNicholas Bellinger }
1207e48354ceSNicholas Bellinger 
1208fceb5bc7SChristoph Hellwig static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1209e48354ceSNicholas Bellinger {
1210e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg;
1211e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
1212e48354ceSNicholas Bellinger 
1213e48354ceSNicholas Bellinger 	tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1214e48354ceSNicholas Bellinger 	tiqn = tpg->tpg_tiqn;
1215e48354ceSNicholas Bellinger 	/*
1216e48354ceSNicholas Bellinger 	 * iscsit_tpg_del_portal_group() assumes force=1
1217e48354ceSNicholas Bellinger 	 */
1218e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1219e48354ceSNicholas Bellinger 	iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1220e48354ceSNicholas Bellinger }
1221e48354ceSNicholas Bellinger 
1222e48354ceSNicholas Bellinger /* End items for lio_target_tiqn_cit */
1223e48354ceSNicholas Bellinger 
1224e48354ceSNicholas Bellinger /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1225e48354ceSNicholas Bellinger 
12262eafd729SChristoph Hellwig static ssize_t lio_target_wwn_lio_version_show(struct config_item *item,
1227e48354ceSNicholas Bellinger 		char *page)
1228e48354ceSNicholas Bellinger {
12294c76251eSNicholas Bellinger 	return sprintf(page, "Datera Inc. iSCSI Target "ISCSIT_VERSION"\n");
1230e48354ceSNicholas Bellinger }
1231e48354ceSNicholas Bellinger 
12322eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(lio_target_wwn_, lio_version);
1233e48354ceSNicholas Bellinger 
1234e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_wwn_attrs[] = {
12352eafd729SChristoph Hellwig 	&lio_target_wwn_attr_lio_version,
1236e48354ceSNicholas Bellinger 	NULL,
1237e48354ceSNicholas Bellinger };
1238e48354ceSNicholas Bellinger 
1239fceb5bc7SChristoph Hellwig static struct se_wwn *lio_target_call_coreaddtiqn(
1240e48354ceSNicholas Bellinger 	struct target_fabric_configfs *tf,
1241e48354ceSNicholas Bellinger 	struct config_group *group,
1242e48354ceSNicholas Bellinger 	const char *name)
1243e48354ceSNicholas Bellinger {
1244e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
1245e48354ceSNicholas Bellinger 
1246e48354ceSNicholas Bellinger 	tiqn = iscsit_add_tiqn((unsigned char *)name);
1247e48354ceSNicholas Bellinger 	if (IS_ERR(tiqn))
1248e1750ba2SThomas Meyer 		return ERR_CAST(tiqn);
1249e48354ceSNicholas Bellinger 
1250b7eec2cdSAndy Grover 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1251e48354ceSNicholas Bellinger 			"iscsi_instance", &iscsi_stat_instance_cit);
12521ae1602dSChristoph Hellwig 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_instance_group,
12531ae1602dSChristoph Hellwig 			&tiqn->tiqn_wwn.fabric_stat_group);
12541ae1602dSChristoph Hellwig 
1255b7eec2cdSAndy Grover 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1256e48354ceSNicholas Bellinger 			"iscsi_sess_err", &iscsi_stat_sess_err_cit);
12571ae1602dSChristoph Hellwig 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
12581ae1602dSChristoph Hellwig 			&tiqn->tiqn_wwn.fabric_stat_group);
12591ae1602dSChristoph Hellwig 
1260b7eec2cdSAndy Grover 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1261e48354ceSNicholas Bellinger 			"iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
12621ae1602dSChristoph Hellwig 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
12631ae1602dSChristoph Hellwig 			&tiqn->tiqn_wwn.fabric_stat_group);
12641ae1602dSChristoph Hellwig 
1265b7eec2cdSAndy Grover 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1266e48354ceSNicholas Bellinger 			"iscsi_login_stats", &iscsi_stat_login_cit);
12671ae1602dSChristoph Hellwig 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
12681ae1602dSChristoph Hellwig 			&tiqn->tiqn_wwn.fabric_stat_group);
12691ae1602dSChristoph Hellwig 
1270b7eec2cdSAndy Grover 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1271e48354ceSNicholas Bellinger 			"iscsi_logout_stats", &iscsi_stat_logout_cit);
12721ae1602dSChristoph Hellwig 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
12731ae1602dSChristoph Hellwig 			&tiqn->tiqn_wwn.fabric_stat_group);
12741ae1602dSChristoph Hellwig 
1275e48354ceSNicholas Bellinger 
1276e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1277e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1278e48354ceSNicholas Bellinger 			" %s\n", name);
1279e48354ceSNicholas Bellinger 	return &tiqn->tiqn_wwn;
1280e48354ceSNicholas Bellinger }
1281e48354ceSNicholas Bellinger 
1282fceb5bc7SChristoph Hellwig static void lio_target_call_coredeltiqn(
1283e48354ceSNicholas Bellinger 	struct se_wwn *wwn)
1284e48354ceSNicholas Bellinger {
1285e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1286e48354ceSNicholas Bellinger 
12871ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&tiqn->tiqn_wwn.fabric_stat_group);
1288e48354ceSNicholas Bellinger 
1289e48354ceSNicholas Bellinger 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1290e48354ceSNicholas Bellinger 			tiqn->tiqn);
1291e48354ceSNicholas Bellinger 	iscsit_del_tiqn(tiqn);
1292e48354ceSNicholas Bellinger }
1293e48354ceSNicholas Bellinger 
1294e48354ceSNicholas Bellinger /* End LIO-Target TIQN struct contig_lio_target_cit */
1295e48354ceSNicholas Bellinger 
1296e48354ceSNicholas Bellinger /* Start lio_target_discovery_auth_cit */
1297e48354ceSNicholas Bellinger 
1298e48354ceSNicholas Bellinger #define DEF_DISC_AUTH_STR(name, flags)					\
1299e48354ceSNicholas Bellinger 	__DEF_NACL_AUTH_STR(disc, name, flags)				\
13002eafd729SChristoph Hellwig static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1301e48354ceSNicholas Bellinger {									\
13022eafd729SChristoph Hellwig 	return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl,\
1303e48354ceSNicholas Bellinger 		page);							\
1304e48354ceSNicholas Bellinger }									\
13052eafd729SChristoph Hellwig static ssize_t iscsi_disc_##name##_store(struct config_item *item,	\
13062eafd729SChristoph Hellwig 		const char *page, size_t count)				\
1307e48354ceSNicholas Bellinger {									\
13082eafd729SChristoph Hellwig 	return __iscsi_disc_##name##_store(&iscsit_global->discovery_acl,	\
1309e48354ceSNicholas Bellinger 		page, count);						\
13102eafd729SChristoph Hellwig 									\
13112eafd729SChristoph Hellwig }									\
13122eafd729SChristoph Hellwig CONFIGFS_ATTR(iscsi_disc_, name)
13132eafd729SChristoph Hellwig 
13142eafd729SChristoph Hellwig DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
13152eafd729SChristoph Hellwig DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
13162eafd729SChristoph Hellwig DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
13172eafd729SChristoph Hellwig DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1318e48354ceSNicholas Bellinger 
1319e48354ceSNicholas Bellinger #define DEF_DISC_AUTH_INT(name)						\
1320e48354ceSNicholas Bellinger 	__DEF_NACL_AUTH_INT(disc, name)					\
13212eafd729SChristoph Hellwig static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1322e48354ceSNicholas Bellinger {									\
13232eafd729SChristoph Hellwig 	return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl, \
1324e48354ceSNicholas Bellinger 			page);						\
13252eafd729SChristoph Hellwig }									\
13262eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(iscsi_disc_, name)
1327e48354ceSNicholas Bellinger 
1328e48354ceSNicholas Bellinger DEF_DISC_AUTH_INT(authenticate_target);
1329e48354ceSNicholas Bellinger 
13302eafd729SChristoph Hellwig 
13312eafd729SChristoph Hellwig static ssize_t iscsi_disc_enforce_discovery_auth_show(struct config_item *item,
1332e48354ceSNicholas Bellinger 		char *page)
1333e48354ceSNicholas Bellinger {
1334e48354ceSNicholas Bellinger 	struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1335e48354ceSNicholas Bellinger 
1336e48354ceSNicholas Bellinger 	return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1337e48354ceSNicholas Bellinger }
1338e48354ceSNicholas Bellinger 
13392eafd729SChristoph Hellwig static ssize_t iscsi_disc_enforce_discovery_auth_store(struct config_item *item,
13402eafd729SChristoph Hellwig 		const char *page, size_t count)
1341e48354ceSNicholas Bellinger {
1342e48354ceSNicholas Bellinger 	struct iscsi_param *param;
1343e48354ceSNicholas Bellinger 	struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1344e48354ceSNicholas Bellinger 	u32 op;
1345ad7babd2SJörn Engel 	int err;
1346e48354ceSNicholas Bellinger 
1347ad7babd2SJörn Engel 	err = kstrtou32(page, 0, &op);
1348ad7babd2SJörn Engel 	if (err)
1349ad7babd2SJörn Engel 		return -EINVAL;
1350e48354ceSNicholas Bellinger 	if ((op != 1) && (op != 0)) {
1351e48354ceSNicholas Bellinger 		pr_err("Illegal value for enforce_discovery_auth:"
1352e48354ceSNicholas Bellinger 				" %u\n", op);
1353e48354ceSNicholas Bellinger 		return -EINVAL;
1354e48354ceSNicholas Bellinger 	}
1355e48354ceSNicholas Bellinger 
1356e48354ceSNicholas Bellinger 	if (!discovery_tpg) {
1357e48354ceSNicholas Bellinger 		pr_err("iscsit_global->discovery_tpg is NULL\n");
1358e48354ceSNicholas Bellinger 		return -EINVAL;
1359e48354ceSNicholas Bellinger 	}
1360e48354ceSNicholas Bellinger 
1361e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD,
1362e48354ceSNicholas Bellinger 				discovery_tpg->param_list);
1363e48354ceSNicholas Bellinger 	if (!param)
1364e48354ceSNicholas Bellinger 		return -EINVAL;
1365e48354ceSNicholas Bellinger 
1366e48354ceSNicholas Bellinger 	if (op) {
1367e48354ceSNicholas Bellinger 		/*
1368e48354ceSNicholas Bellinger 		 * Reset the AuthMethod key to CHAP.
1369e48354ceSNicholas Bellinger 		 */
1370e48354ceSNicholas Bellinger 		if (iscsi_update_param_value(param, CHAP) < 0)
1371e48354ceSNicholas Bellinger 			return -EINVAL;
1372e48354ceSNicholas Bellinger 
1373e48354ceSNicholas Bellinger 		discovery_tpg->tpg_attrib.authentication = 1;
1374e48354ceSNicholas Bellinger 		iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1375e48354ceSNicholas Bellinger 		pr_debug("LIO-CORE[0] Successfully enabled"
1376e48354ceSNicholas Bellinger 			" authentication enforcement for iSCSI"
1377e48354ceSNicholas Bellinger 			" Discovery TPG\n");
1378e48354ceSNicholas Bellinger 	} else {
1379e48354ceSNicholas Bellinger 		/*
1380e48354ceSNicholas Bellinger 		 * Reset the AuthMethod key to CHAP,None
1381e48354ceSNicholas Bellinger 		 */
1382e48354ceSNicholas Bellinger 		if (iscsi_update_param_value(param, "CHAP,None") < 0)
1383e48354ceSNicholas Bellinger 			return -EINVAL;
1384e48354ceSNicholas Bellinger 
1385e48354ceSNicholas Bellinger 		discovery_tpg->tpg_attrib.authentication = 0;
1386e48354ceSNicholas Bellinger 		iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1387e48354ceSNicholas Bellinger 		pr_debug("LIO-CORE[0] Successfully disabled"
1388e48354ceSNicholas Bellinger 			" authentication enforcement for iSCSI"
1389e48354ceSNicholas Bellinger 			" Discovery TPG\n");
1390e48354ceSNicholas Bellinger 	}
1391e48354ceSNicholas Bellinger 
1392e48354ceSNicholas Bellinger 	return count;
1393e48354ceSNicholas Bellinger }
1394e48354ceSNicholas Bellinger 
13952eafd729SChristoph Hellwig CONFIGFS_ATTR(iscsi_disc_, enforce_discovery_auth);
1396e48354ceSNicholas Bellinger 
1397e48354ceSNicholas Bellinger static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
13982eafd729SChristoph Hellwig 	&iscsi_disc_attr_userid,
13992eafd729SChristoph Hellwig 	&iscsi_disc_attr_password,
14002eafd729SChristoph Hellwig 	&iscsi_disc_attr_authenticate_target,
14012eafd729SChristoph Hellwig 	&iscsi_disc_attr_userid_mutual,
14022eafd729SChristoph Hellwig 	&iscsi_disc_attr_password_mutual,
14032eafd729SChristoph Hellwig 	&iscsi_disc_attr_enforce_discovery_auth,
1404e48354ceSNicholas Bellinger 	NULL,
1405e48354ceSNicholas Bellinger };
1406e48354ceSNicholas Bellinger 
1407e48354ceSNicholas Bellinger /* End lio_target_discovery_auth_cit */
1408e48354ceSNicholas Bellinger 
1409e48354ceSNicholas Bellinger /* Start functions for target_core_fabric_ops */
1410e48354ceSNicholas Bellinger 
1411e48354ceSNicholas Bellinger static char *iscsi_get_fabric_name(void)
1412e48354ceSNicholas Bellinger {
1413e48354ceSNicholas Bellinger 	return "iSCSI";
1414e48354ceSNicholas Bellinger }
1415e48354ceSNicholas Bellinger 
1416e48354ceSNicholas Bellinger static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1417e48354ceSNicholas Bellinger {
1418e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1419e48354ceSNicholas Bellinger 
1420e48354ceSNicholas Bellinger 	return cmd->i_state;
1421e48354ceSNicholas Bellinger }
1422e48354ceSNicholas Bellinger 
1423e48354ceSNicholas Bellinger static u32 lio_sess_get_index(struct se_session *se_sess)
1424e48354ceSNicholas Bellinger {
1425e48354ceSNicholas Bellinger 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1426e48354ceSNicholas Bellinger 
1427e48354ceSNicholas Bellinger 	return sess->session_index;
1428e48354ceSNicholas Bellinger }
1429e48354ceSNicholas Bellinger 
1430e48354ceSNicholas Bellinger static u32 lio_sess_get_initiator_sid(
1431e48354ceSNicholas Bellinger 	struct se_session *se_sess,
1432e48354ceSNicholas Bellinger 	unsigned char *buf,
1433e48354ceSNicholas Bellinger 	u32 size)
1434e48354ceSNicholas Bellinger {
1435e48354ceSNicholas Bellinger 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1436e48354ceSNicholas Bellinger 	/*
1437e48354ceSNicholas Bellinger 	 * iSCSI Initiator Session Identifier from RFC-3720.
1438e48354ceSNicholas Bellinger 	 */
14395cdf5a87SAndy Shevchenko 	return snprintf(buf, size, "%6phN", sess->isid);
1440e48354ceSNicholas Bellinger }
1441e48354ceSNicholas Bellinger 
1442e48354ceSNicholas Bellinger static int lio_queue_data_in(struct se_cmd *se_cmd)
1443e48354ceSNicholas Bellinger {
1444e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1445e48354ceSNicholas Bellinger 
1446e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_DATAIN;
14472ec5a8c1SNicholas Bellinger 	cmd->conn->conn_transport->iscsit_queue_data_in(cmd->conn, cmd);
14482ec5a8c1SNicholas Bellinger 
1449e48354ceSNicholas Bellinger 	return 0;
1450e48354ceSNicholas Bellinger }
1451e48354ceSNicholas Bellinger 
1452e48354ceSNicholas Bellinger static int lio_write_pending(struct se_cmd *se_cmd)
1453e48354ceSNicholas Bellinger {
1454e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
14553e1c81a9SNicholas Bellinger 	struct iscsi_conn *conn = cmd->conn;
1456e48354ceSNicholas Bellinger 
1457e48354ceSNicholas Bellinger 	if (!cmd->immediate_data && !cmd->unsolicited_data)
14583e1c81a9SNicholas Bellinger 		return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1459e48354ceSNicholas Bellinger 
1460e48354ceSNicholas Bellinger 	return 0;
1461e48354ceSNicholas Bellinger }
1462e48354ceSNicholas Bellinger 
1463e48354ceSNicholas Bellinger static int lio_write_pending_status(struct se_cmd *se_cmd)
1464e48354ceSNicholas Bellinger {
1465e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1466e48354ceSNicholas Bellinger 	int ret;
1467e48354ceSNicholas Bellinger 
1468e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->istate_lock);
1469e48354ceSNicholas Bellinger 	ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1470e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->istate_lock);
1471e48354ceSNicholas Bellinger 
1472e48354ceSNicholas Bellinger 	return ret;
1473e48354ceSNicholas Bellinger }
1474e48354ceSNicholas Bellinger 
1475e48354ceSNicholas Bellinger static int lio_queue_status(struct se_cmd *se_cmd)
1476e48354ceSNicholas Bellinger {
1477e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1478e48354ceSNicholas Bellinger 
1479e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_STATUS;
14805e8e6b4bSNicholas Bellinger 
14815e8e6b4bSNicholas Bellinger 	if (cmd->se_cmd.scsi_status || cmd->sense_reason) {
14825e8e6b4bSNicholas Bellinger 		iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
14835e8e6b4bSNicholas Bellinger 		return 0;
14845e8e6b4bSNicholas Bellinger 	}
14852ec5a8c1SNicholas Bellinger 	cmd->conn->conn_transport->iscsit_queue_status(cmd->conn, cmd);
14862ec5a8c1SNicholas Bellinger 
1487e48354ceSNicholas Bellinger 	return 0;
1488e48354ceSNicholas Bellinger }
1489e48354ceSNicholas Bellinger 
1490b79fafacSJoern Engel static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
1491e48354ceSNicholas Bellinger {
1492e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1493e48354ceSNicholas Bellinger 
1494e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1495e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1496e48354ceSNicholas Bellinger }
1497e48354ceSNicholas Bellinger 
1498131e6abcSNicholas Bellinger static void lio_aborted_task(struct se_cmd *se_cmd)
1499131e6abcSNicholas Bellinger {
1500131e6abcSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1501131e6abcSNicholas Bellinger 
1502131e6abcSNicholas Bellinger 	cmd->conn->conn_transport->iscsit_aborted_task(cmd->conn, cmd);
1503131e6abcSNicholas Bellinger }
1504131e6abcSNicholas Bellinger 
15052b6eb609SChristoph Hellwig static inline struct iscsi_portal_group *iscsi_tpg(struct se_portal_group *se_tpg)
15062b6eb609SChristoph Hellwig {
15072b6eb609SChristoph Hellwig 	return container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
15082b6eb609SChristoph Hellwig }
15092b6eb609SChristoph Hellwig 
1510e48354ceSNicholas Bellinger static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1511e48354ceSNicholas Bellinger {
15122b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn;
1513e48354ceSNicholas Bellinger }
1514e48354ceSNicholas Bellinger 
1515e48354ceSNicholas Bellinger static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1516e48354ceSNicholas Bellinger {
15172b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpgt;
1518e48354ceSNicholas Bellinger }
1519e48354ceSNicholas Bellinger 
1520e48354ceSNicholas Bellinger static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1521e48354ceSNicholas Bellinger {
15222b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_attrib.default_cmdsn_depth;
1523e48354ceSNicholas Bellinger }
1524e48354ceSNicholas Bellinger 
1525e48354ceSNicholas Bellinger static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1526e48354ceSNicholas Bellinger {
15272b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_attrib.generate_node_acls;
1528e48354ceSNicholas Bellinger }
1529e48354ceSNicholas Bellinger 
1530e48354ceSNicholas Bellinger static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1531e48354ceSNicholas Bellinger {
15322b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_attrib.cache_dynamic_acls;
1533e48354ceSNicholas Bellinger }
1534e48354ceSNicholas Bellinger 
1535e48354ceSNicholas Bellinger static int lio_tpg_check_demo_mode_write_protect(
1536e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg)
1537e48354ceSNicholas Bellinger {
15382b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_attrib.demo_mode_write_protect;
1539e48354ceSNicholas Bellinger }
1540e48354ceSNicholas Bellinger 
1541e48354ceSNicholas Bellinger static int lio_tpg_check_prod_mode_write_protect(
1542e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg)
1543e48354ceSNicholas Bellinger {
15442b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_attrib.prod_mode_write_protect;
1545e48354ceSNicholas Bellinger }
1546e48354ceSNicholas Bellinger 
1547901c04a3SNicholas Bellinger static int lio_tpg_check_prot_fabric_only(
1548901c04a3SNicholas Bellinger 	struct se_portal_group *se_tpg)
1549901c04a3SNicholas Bellinger {
1550901c04a3SNicholas Bellinger 	/*
1551901c04a3SNicholas Bellinger 	 * Only report fabric_prot_type if t10_pi has also been enabled
1552901c04a3SNicholas Bellinger 	 * for incoming ib_isert sessions.
1553901c04a3SNicholas Bellinger 	 */
15542b6eb609SChristoph Hellwig 	if (!iscsi_tpg(se_tpg)->tpg_attrib.t10_pi)
1555901c04a3SNicholas Bellinger 		return 0;
15562b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_attrib.fabric_prot_type;
1557901c04a3SNicholas Bellinger }
1558901c04a3SNicholas Bellinger 
1559e48354ceSNicholas Bellinger /*
1560d36ad77fSNicholas Bellinger  * This function calls iscsit_inc_session_usage_count() on the
1561e48354ceSNicholas Bellinger  * struct iscsi_session in question.
1562e48354ceSNicholas Bellinger  */
1563e48354ceSNicholas Bellinger static int lio_tpg_shutdown_session(struct se_session *se_sess)
1564e48354ceSNicholas Bellinger {
1565e48354ceSNicholas Bellinger 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1566d36ad77fSNicholas Bellinger 	struct se_portal_group *se_tpg = &sess->tpg->tpg_se_tpg;
156726a99c19SNicholas Bellinger 
1568d36ad77fSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
1569e48354ceSNicholas Bellinger 	spin_lock(&sess->conn_lock);
1570e48354ceSNicholas Bellinger 	if (atomic_read(&sess->session_fall_back_to_erl0) ||
1571e48354ceSNicholas Bellinger 	    atomic_read(&sess->session_logout) ||
1572e48354ceSNicholas Bellinger 	    (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1573e48354ceSNicholas Bellinger 		spin_unlock(&sess->conn_lock);
1574d36ad77fSNicholas Bellinger 		spin_unlock_bh(&se_tpg->session_lock);
1575e48354ceSNicholas Bellinger 		return 0;
1576e48354ceSNicholas Bellinger 	}
1577e48354ceSNicholas Bellinger 	atomic_set(&sess->session_reinstatement, 1);
1578e48354ceSNicholas Bellinger 	spin_unlock(&sess->conn_lock);
1579e48354ceSNicholas Bellinger 
1580e48354ceSNicholas Bellinger 	iscsit_stop_time2retain_timer(sess);
1581d36ad77fSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
158226a99c19SNicholas Bellinger 
158399367f01SNicholas Bellinger 	iscsit_stop_session(sess, 1, 1);
1584e48354ceSNicholas Bellinger 	return 1;
1585e48354ceSNicholas Bellinger }
1586e48354ceSNicholas Bellinger 
1587e48354ceSNicholas Bellinger /*
1588e48354ceSNicholas Bellinger  * Calls iscsit_dec_session_usage_count() as inverse of
1589e48354ceSNicholas Bellinger  * lio_tpg_shutdown_session()
1590e48354ceSNicholas Bellinger  */
1591e48354ceSNicholas Bellinger static void lio_tpg_close_session(struct se_session *se_sess)
1592e48354ceSNicholas Bellinger {
1593e48354ceSNicholas Bellinger 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1594e48354ceSNicholas Bellinger 	/*
1595e48354ceSNicholas Bellinger 	 * If the iSCSI Session for the iSCSI Initiator Node exists,
1596e48354ceSNicholas Bellinger 	 * forcefully shutdown the iSCSI NEXUS.
1597e48354ceSNicholas Bellinger 	 */
1598e48354ceSNicholas Bellinger 	iscsit_close_session(sess);
1599e48354ceSNicholas Bellinger }
1600e48354ceSNicholas Bellinger 
1601e48354ceSNicholas Bellinger static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1602e48354ceSNicholas Bellinger {
16032b6eb609SChristoph Hellwig 	return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn_index;
1604e48354ceSNicholas Bellinger }
1605e48354ceSNicholas Bellinger 
1606e48354ceSNicholas Bellinger static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1607e48354ceSNicholas Bellinger {
1608e48354ceSNicholas Bellinger 	struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1609e48354ceSNicholas Bellinger 				se_node_acl);
1610d1fa7a1dSNicholas Bellinger 	struct se_portal_group *se_tpg = se_acl->se_tpg;
1611d1fa7a1dSNicholas Bellinger 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1612d1fa7a1dSNicholas Bellinger 				struct iscsi_portal_group, tpg_se_tpg);
1613e48354ceSNicholas Bellinger 
1614b7eec2cdSAndy Grover 	acl->node_attrib.nacl = acl;
1615d1fa7a1dSNicholas Bellinger 	iscsit_set_default_node_attribues(acl, tpg);
1616e48354ceSNicholas Bellinger }
1617e48354ceSNicholas Bellinger 
16183e1c81a9SNicholas Bellinger static int lio_check_stop_free(struct se_cmd *se_cmd)
16193e1c81a9SNicholas Bellinger {
1620afc16604SBart Van Assche 	return target_put_sess_cmd(se_cmd);
16213e1c81a9SNicholas Bellinger }
16223e1c81a9SNicholas Bellinger 
1623e48354ceSNicholas Bellinger static void lio_release_cmd(struct se_cmd *se_cmd)
1624e48354ceSNicholas Bellinger {
1625e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1626e48354ceSNicholas Bellinger 
1627cdb72665SNicholas Bellinger 	pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
1628d703ce2fSNicholas Bellinger 	iscsit_release_cmd(cmd);
1629e48354ceSNicholas Bellinger }
1630e48354ceSNicholas Bellinger 
16319ac8928eSChristoph Hellwig const struct target_core_fabric_ops iscsi_ops = {
16329ac8928eSChristoph Hellwig 	.module				= THIS_MODULE,
16339ac8928eSChristoph Hellwig 	.name				= "iscsi",
1634144bc4c2SChristoph Hellwig 	.node_acl_size			= sizeof(struct iscsi_node_acl),
16359ac8928eSChristoph Hellwig 	.get_fabric_name		= iscsi_get_fabric_name,
16369ac8928eSChristoph Hellwig 	.tpg_get_wwn			= lio_tpg_get_endpoint_wwn,
16379ac8928eSChristoph Hellwig 	.tpg_get_tag			= lio_tpg_get_tag,
16389ac8928eSChristoph Hellwig 	.tpg_get_default_depth		= lio_tpg_get_default_depth,
16399ac8928eSChristoph Hellwig 	.tpg_check_demo_mode		= lio_tpg_check_demo_mode,
16409ac8928eSChristoph Hellwig 	.tpg_check_demo_mode_cache	= lio_tpg_check_demo_mode_cache,
16419ac8928eSChristoph Hellwig 	.tpg_check_demo_mode_write_protect =
16429ac8928eSChristoph Hellwig 			lio_tpg_check_demo_mode_write_protect,
16439ac8928eSChristoph Hellwig 	.tpg_check_prod_mode_write_protect =
16449ac8928eSChristoph Hellwig 			lio_tpg_check_prod_mode_write_protect,
16459ac8928eSChristoph Hellwig 	.tpg_check_prot_fabric_only	= &lio_tpg_check_prot_fabric_only,
16469ac8928eSChristoph Hellwig 	.tpg_get_inst_index		= lio_tpg_get_inst_index,
16479ac8928eSChristoph Hellwig 	.check_stop_free		= lio_check_stop_free,
16489ac8928eSChristoph Hellwig 	.release_cmd			= lio_release_cmd,
16499ac8928eSChristoph Hellwig 	.shutdown_session		= lio_tpg_shutdown_session,
16509ac8928eSChristoph Hellwig 	.close_session			= lio_tpg_close_session,
16519ac8928eSChristoph Hellwig 	.sess_get_index			= lio_sess_get_index,
16529ac8928eSChristoph Hellwig 	.sess_get_initiator_sid		= lio_sess_get_initiator_sid,
16539ac8928eSChristoph Hellwig 	.write_pending			= lio_write_pending,
16549ac8928eSChristoph Hellwig 	.write_pending_status		= lio_write_pending_status,
16559ac8928eSChristoph Hellwig 	.set_default_node_attributes	= lio_set_default_node_attributes,
16569ac8928eSChristoph Hellwig 	.get_cmd_state			= iscsi_get_cmd_state,
16579ac8928eSChristoph Hellwig 	.queue_data_in			= lio_queue_data_in,
16589ac8928eSChristoph Hellwig 	.queue_status			= lio_queue_status,
16599ac8928eSChristoph Hellwig 	.queue_tm_rsp			= lio_queue_tm_rsp,
16609ac8928eSChristoph Hellwig 	.aborted_task			= lio_aborted_task,
16619ac8928eSChristoph Hellwig 	.fabric_make_wwn		= lio_target_call_coreaddtiqn,
16629ac8928eSChristoph Hellwig 	.fabric_drop_wwn		= lio_target_call_coredeltiqn,
16639ac8928eSChristoph Hellwig 	.fabric_make_tpg		= lio_target_tiqn_addtpg,
16649ac8928eSChristoph Hellwig 	.fabric_drop_tpg		= lio_target_tiqn_deltpg,
16659ac8928eSChristoph Hellwig 	.fabric_make_np			= lio_target_call_addnptotpg,
16669ac8928eSChristoph Hellwig 	.fabric_drop_np			= lio_target_call_delnpfromtpg,
1667c7d6a803SChristoph Hellwig 	.fabric_init_nodeacl		= lio_target_init_nodeacl,
1668c7d6a803SChristoph Hellwig 	.fabric_cleanup_nodeacl		= lio_target_cleanup_nodeacl,
1669e48354ceSNicholas Bellinger 
16709ac8928eSChristoph Hellwig 	.tfc_discovery_attrs		= lio_target_discovery_auth_attrs,
16719ac8928eSChristoph Hellwig 	.tfc_wwn_attrs			= lio_target_wwn_attrs,
16729ac8928eSChristoph Hellwig 	.tfc_tpg_base_attrs		= lio_target_tpg_attrs,
16739ac8928eSChristoph Hellwig 	.tfc_tpg_attrib_attrs		= lio_target_tpg_attrib_attrs,
16749ac8928eSChristoph Hellwig 	.tfc_tpg_auth_attrs		= lio_target_tpg_auth_attrs,
16759ac8928eSChristoph Hellwig 	.tfc_tpg_param_attrs		= lio_target_tpg_param_attrs,
16769ac8928eSChristoph Hellwig 	.tfc_tpg_np_base_attrs		= lio_target_portal_attrs,
16779ac8928eSChristoph Hellwig 	.tfc_tpg_nacl_base_attrs	= lio_target_initiator_attrs,
16789ac8928eSChristoph Hellwig 	.tfc_tpg_nacl_attrib_attrs	= lio_target_nacl_attrib_attrs,
16799ac8928eSChristoph Hellwig 	.tfc_tpg_nacl_auth_attrs	= lio_target_nacl_auth_attrs,
16809ac8928eSChristoph Hellwig 	.tfc_tpg_nacl_param_attrs	= lio_target_nacl_param_attrs,
16819ac8928eSChristoph Hellwig };
1682