xref: /linux/drivers/target/iscsi/iscsi_target_configfs.c (revision 97884ca8c2925d14c32188e865069f21378b4b4f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3  * This file contains the configfs implementation for iSCSI Target mode
4  * from the LIO-Target Project.
5  *
6  * (c) Copyright 2007-2013 Datera, Inc.
7  *
8  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9  *
10  ****************************************************************************/
11 
12 #include <linux/configfs.h>
13 #include <linux/ctype.h>
14 #include <linux/export.h>
15 #include <linux/inet.h>
16 #include <linux/module.h>
17 #include <net/ipv6.h>
18 #include <target/target_core_base.h>
19 #include <target/target_core_fabric.h>
20 #include <target/iscsi/iscsi_transport.h>
21 #include <target/iscsi/iscsi_target_core.h>
22 #include "iscsi_target_parameters.h"
23 #include "iscsi_target_device.h"
24 #include "iscsi_target_erl0.h"
25 #include "iscsi_target_nodeattrib.h"
26 #include "iscsi_target_tpg.h"
27 #include "iscsi_target_util.h"
28 #include "iscsi_target.h"
29 #include <target/iscsi/iscsi_target_stat.h>
30 
31 
32 /* Start items for lio_target_portal_cit */
33 
34 static inline struct iscsi_tpg_np *to_iscsi_tpg_np(struct config_item *item)
35 {
36 	return container_of(to_tpg_np(item), struct iscsi_tpg_np, se_tpg_np);
37 }
38 
39 static ssize_t lio_target_np_driver_show(struct config_item *item, char *page,
40 					 enum iscsit_transport_type type)
41 {
42 	struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
43 	struct iscsi_tpg_np *tpg_np_new;
44 	ssize_t rb;
45 
46 	tpg_np_new = iscsit_tpg_locate_child_np(tpg_np, type);
47 	if (tpg_np_new)
48 		rb = sprintf(page, "1\n");
49 	else
50 		rb = sprintf(page, "0\n");
51 
52 	return rb;
53 }
54 
55 static ssize_t lio_target_np_driver_store(struct config_item *item,
56 		const char *page, size_t count, enum iscsit_transport_type type,
57 		const char *mod_name)
58 {
59 	struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
60 	struct iscsi_np *np;
61 	struct iscsi_portal_group *tpg;
62 	struct iscsi_tpg_np *tpg_np_new = NULL;
63 	u32 op;
64 	int rc;
65 
66 	rc = kstrtou32(page, 0, &op);
67 	if (rc)
68 		return rc;
69 	if ((op != 1) && (op != 0)) {
70 		pr_err("Illegal value for tpg_enable: %u\n", op);
71 		return -EINVAL;
72 	}
73 	np = tpg_np->tpg_np;
74 	if (!np) {
75 		pr_err("Unable to locate struct iscsi_np from"
76 				" struct iscsi_tpg_np\n");
77 		return -EINVAL;
78 	}
79 
80 	tpg = tpg_np->tpg;
81 	if (iscsit_get_tpg(tpg) < 0)
82 		return -EINVAL;
83 
84 	if (op) {
85 		if (strlen(mod_name)) {
86 			rc = request_module(mod_name);
87 			if (rc != 0) {
88 				pr_warn("Unable to request_module for %s\n",
89 					mod_name);
90 				rc = 0;
91 			}
92 		}
93 
94 		tpg_np_new = iscsit_tpg_add_network_portal(tpg,
95 					&np->np_sockaddr, tpg_np, type);
96 		if (IS_ERR(tpg_np_new)) {
97 			rc = PTR_ERR(tpg_np_new);
98 			goto out;
99 		}
100 	} else {
101 		tpg_np_new = iscsit_tpg_locate_child_np(tpg_np, type);
102 		if (tpg_np_new) {
103 			rc = iscsit_tpg_del_network_portal(tpg, tpg_np_new);
104 			if (rc < 0)
105 				goto out;
106 		}
107 	}
108 
109 	iscsit_put_tpg(tpg);
110 	return count;
111 out:
112 	iscsit_put_tpg(tpg);
113 	return rc;
114 }
115 
116 static ssize_t lio_target_np_iser_show(struct config_item *item, char *page)
117 {
118 	return lio_target_np_driver_show(item, page, ISCSI_INFINIBAND);
119 }
120 
121 static ssize_t lio_target_np_iser_store(struct config_item *item,
122 					const char *page, size_t count)
123 {
124 	return lio_target_np_driver_store(item, page, count,
125 					  ISCSI_INFINIBAND, "ib_isert");
126 }
127 CONFIGFS_ATTR(lio_target_np_, iser);
128 
129 static ssize_t lio_target_np_cxgbit_show(struct config_item *item, char *page)
130 {
131 	return lio_target_np_driver_show(item, page, ISCSI_CXGBIT);
132 }
133 
134 static ssize_t lio_target_np_cxgbit_store(struct config_item *item,
135 					  const char *page, size_t count)
136 {
137 	return lio_target_np_driver_store(item, page, count,
138 					  ISCSI_CXGBIT, "cxgbit");
139 }
140 CONFIGFS_ATTR(lio_target_np_, cxgbit);
141 
142 static struct configfs_attribute *lio_target_portal_attrs[] = {
143 	&lio_target_np_attr_iser,
144 	&lio_target_np_attr_cxgbit,
145 	NULL,
146 };
147 
148 /* Stop items for lio_target_portal_cit */
149 
150 /* Start items for lio_target_np_cit */
151 
152 #define MAX_PORTAL_LEN		256
153 
154 static struct se_tpg_np *lio_target_call_addnptotpg(
155 	struct se_portal_group *se_tpg,
156 	struct config_group *group,
157 	const char *name)
158 {
159 	struct iscsi_portal_group *tpg;
160 	struct iscsi_tpg_np *tpg_np;
161 	char *str, *str2, *ip_str, *port_str;
162 	struct sockaddr_storage sockaddr = { };
163 	int ret;
164 	char buf[MAX_PORTAL_LEN + 1];
165 
166 	if (strlen(name) > MAX_PORTAL_LEN) {
167 		pr_err("strlen(name): %d exceeds MAX_PORTAL_LEN: %d\n",
168 			(int)strlen(name), MAX_PORTAL_LEN);
169 		return ERR_PTR(-EOVERFLOW);
170 	}
171 	memset(buf, 0, MAX_PORTAL_LEN + 1);
172 	snprintf(buf, MAX_PORTAL_LEN + 1, "%s", name);
173 
174 	str = strstr(buf, "[");
175 	if (str) {
176 		str2 = strstr(str, "]");
177 		if (!str2) {
178 			pr_err("Unable to locate trailing \"]\""
179 				" in IPv6 iSCSI network portal address\n");
180 			return ERR_PTR(-EINVAL);
181 		}
182 
183 		ip_str = str + 1; /* Skip over leading "[" */
184 		*str2 = '\0'; /* Terminate the unbracketed IPv6 address */
185 		str2++; /* Skip over the \0 */
186 
187 		port_str = strstr(str2, ":");
188 		if (!port_str) {
189 			pr_err("Unable to locate \":port\""
190 				" in IPv6 iSCSI network portal address\n");
191 			return ERR_PTR(-EINVAL);
192 		}
193 		*port_str = '\0'; /* Terminate string for IP */
194 		port_str++; /* Skip over ":" */
195 	} else {
196 		ip_str = &buf[0];
197 		port_str = strstr(ip_str, ":");
198 		if (!port_str) {
199 			pr_err("Unable to locate \":port\""
200 				" in IPv4 iSCSI network portal address\n");
201 			return ERR_PTR(-EINVAL);
202 		}
203 		*port_str = '\0'; /* Terminate string for IP */
204 		port_str++; /* Skip over ":" */
205 	}
206 
207 	ret = inet_pton_with_scope(&init_net, AF_UNSPEC, ip_str,
208 			port_str, &sockaddr);
209 	if (ret) {
210 		pr_err("malformed ip/port passed: %s\n", name);
211 		return ERR_PTR(ret);
212 	}
213 
214 	tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
215 	ret = iscsit_get_tpg(tpg);
216 	if (ret < 0)
217 		return ERR_PTR(-EINVAL);
218 
219 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
220 		" PORTAL: %s\n",
221 		config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
222 		tpg->tpgt, name);
223 	/*
224 	 * Assume ISCSI_TCP by default.  Other network portals for other
225 	 * iSCSI fabrics:
226 	 *
227 	 * Traditional iSCSI over SCTP (initial support)
228 	 * iSER/TCP (TODO, hardware available)
229 	 * iSER/SCTP (TODO, software emulation with osc-iwarp)
230 	 * iSER/IB (TODO, hardware available)
231 	 *
232 	 * can be enabled with attributes under
233 	 * sys/kernel/config/iscsi/$IQN/$TPG/np/$IP:$PORT/
234 	 *
235 	 */
236 	tpg_np = iscsit_tpg_add_network_portal(tpg, &sockaddr, NULL,
237 				ISCSI_TCP);
238 	if (IS_ERR(tpg_np)) {
239 		iscsit_put_tpg(tpg);
240 		return ERR_CAST(tpg_np);
241 	}
242 	pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
243 
244 	iscsit_put_tpg(tpg);
245 	return &tpg_np->se_tpg_np;
246 }
247 
248 static void lio_target_call_delnpfromtpg(
249 	struct se_tpg_np *se_tpg_np)
250 {
251 	struct iscsi_portal_group *tpg;
252 	struct iscsi_tpg_np *tpg_np;
253 	struct se_portal_group *se_tpg;
254 	int ret;
255 
256 	tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
257 	tpg = tpg_np->tpg;
258 	ret = iscsit_get_tpg(tpg);
259 	if (ret < 0)
260 		return;
261 
262 	se_tpg = &tpg->tpg_se_tpg;
263 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s TPGT: %hu"
264 		" PORTAL: %pISpc\n", config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
265 		tpg->tpgt, &tpg_np->tpg_np->np_sockaddr);
266 
267 	ret = iscsit_tpg_del_network_portal(tpg, tpg_np);
268 	if (ret < 0)
269 		goto out;
270 
271 	pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
272 out:
273 	iscsit_put_tpg(tpg);
274 }
275 
276 /* End items for lio_target_np_cit */
277 
278 /* Start items for lio_target_nacl_attrib_cit */
279 
280 #define ISCSI_NACL_ATTR(name)						\
281 static ssize_t iscsi_nacl_attrib_##name##_show(struct config_item *item,\
282 		char *page)						\
283 {									\
284 	struct se_node_acl *se_nacl = attrib_to_nacl(item);		\
285 	struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
286 					se_node_acl);			\
287 									\
288 	return sprintf(page, "%u\n", nacl->node_attrib.name);		\
289 }									\
290 									\
291 static ssize_t iscsi_nacl_attrib_##name##_store(struct config_item *item,\
292 		const char *page, size_t count)				\
293 {									\
294 	struct se_node_acl *se_nacl = attrib_to_nacl(item);		\
295 	struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
296 					se_node_acl);			\
297 	u32 val;							\
298 	int ret;							\
299 									\
300 	ret = kstrtou32(page, 0, &val);					\
301 	if (ret)							\
302 		return ret;						\
303 	ret = iscsit_na_##name(nacl, val);				\
304 	if (ret < 0)							\
305 		return ret;						\
306 									\
307 	return count;							\
308 }									\
309 									\
310 CONFIGFS_ATTR(iscsi_nacl_attrib_, name)
311 
312 ISCSI_NACL_ATTR(dataout_timeout);
313 ISCSI_NACL_ATTR(dataout_timeout_retries);
314 ISCSI_NACL_ATTR(default_erl);
315 ISCSI_NACL_ATTR(nopin_timeout);
316 ISCSI_NACL_ATTR(nopin_response_timeout);
317 ISCSI_NACL_ATTR(random_datain_pdu_offsets);
318 ISCSI_NACL_ATTR(random_datain_seq_offsets);
319 ISCSI_NACL_ATTR(random_r2t_offsets);
320 
321 static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
322 	&iscsi_nacl_attrib_attr_dataout_timeout,
323 	&iscsi_nacl_attrib_attr_dataout_timeout_retries,
324 	&iscsi_nacl_attrib_attr_default_erl,
325 	&iscsi_nacl_attrib_attr_nopin_timeout,
326 	&iscsi_nacl_attrib_attr_nopin_response_timeout,
327 	&iscsi_nacl_attrib_attr_random_datain_pdu_offsets,
328 	&iscsi_nacl_attrib_attr_random_datain_seq_offsets,
329 	&iscsi_nacl_attrib_attr_random_r2t_offsets,
330 	NULL,
331 };
332 
333 /* End items for lio_target_nacl_attrib_cit */
334 
335 /* Start items for lio_target_nacl_auth_cit */
336 
337 #define __DEF_NACL_AUTH_STR(prefix, name, flags)			\
338 static ssize_t __iscsi_##prefix##_##name##_show(			\
339 	struct iscsi_node_acl *nacl,					\
340 	char *page)							\
341 {									\
342 	struct iscsi_node_auth *auth = &nacl->node_auth;		\
343 									\
344 	if (!capable(CAP_SYS_ADMIN))					\
345 		return -EPERM;						\
346 	return snprintf(page, PAGE_SIZE, "%s\n", auth->name);		\
347 }									\
348 									\
349 static ssize_t __iscsi_##prefix##_##name##_store(			\
350 	struct iscsi_node_acl *nacl,					\
351 	const char *page,						\
352 	size_t count)							\
353 {									\
354 	struct iscsi_node_auth *auth = &nacl->node_auth;		\
355 									\
356 	if (!capable(CAP_SYS_ADMIN))					\
357 		return -EPERM;						\
358 	if (count >= sizeof(auth->name))				\
359 		return -EINVAL;						\
360 	snprintf(auth->name, sizeof(auth->name), "%s", page);		\
361 	if (!strncmp("NULL", auth->name, 4))				\
362 		auth->naf_flags &= ~flags;				\
363 	else								\
364 		auth->naf_flags |= flags;				\
365 									\
366 	if ((auth->naf_flags & NAF_USERID_IN_SET) &&			\
367 	    (auth->naf_flags & NAF_PASSWORD_IN_SET))			\
368 		auth->authenticate_target = 1;				\
369 	else								\
370 		auth->authenticate_target = 0;				\
371 									\
372 	return count;							\
373 }
374 
375 #define DEF_NACL_AUTH_STR(name, flags)					\
376 	__DEF_NACL_AUTH_STR(nacl_auth, name, flags)			\
377 static ssize_t iscsi_nacl_auth_##name##_show(struct config_item *item,	\
378 		char *page)						\
379 {									\
380 	struct se_node_acl *nacl = auth_to_nacl(item);			\
381 	return __iscsi_nacl_auth_##name##_show(container_of(nacl,	\
382 			struct iscsi_node_acl, se_node_acl), page);	\
383 }									\
384 static ssize_t iscsi_nacl_auth_##name##_store(struct config_item *item,	\
385 		const char *page, size_t count)				\
386 {									\
387 	struct se_node_acl *nacl = auth_to_nacl(item);			\
388 	return __iscsi_nacl_auth_##name##_store(container_of(nacl,	\
389 			struct iscsi_node_acl, se_node_acl), page, count); \
390 }									\
391 									\
392 CONFIGFS_ATTR(iscsi_nacl_auth_, name)
393 
394 /*
395  * One-way authentication userid
396  */
397 DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
398 DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
399 DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
400 DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
401 
402 #define __DEF_NACL_AUTH_INT(prefix, name)				\
403 static ssize_t __iscsi_##prefix##_##name##_show(				\
404 	struct iscsi_node_acl *nacl,					\
405 	char *page)							\
406 {									\
407 	struct iscsi_node_auth *auth = &nacl->node_auth;		\
408 									\
409 	if (!capable(CAP_SYS_ADMIN))					\
410 		return -EPERM;						\
411 									\
412 	return snprintf(page, PAGE_SIZE, "%d\n", auth->name);		\
413 }
414 
415 #define DEF_NACL_AUTH_INT(name)						\
416 	__DEF_NACL_AUTH_INT(nacl_auth, name)				\
417 static ssize_t iscsi_nacl_auth_##name##_show(struct config_item *item,	\
418 		char *page)						\
419 {									\
420 	struct se_node_acl *nacl = auth_to_nacl(item);			\
421 	return __iscsi_nacl_auth_##name##_show(container_of(nacl,	\
422 			struct iscsi_node_acl, se_node_acl), page);	\
423 }									\
424 									\
425 CONFIGFS_ATTR_RO(iscsi_nacl_auth_, name)
426 
427 DEF_NACL_AUTH_INT(authenticate_target);
428 
429 static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
430 	&iscsi_nacl_auth_attr_userid,
431 	&iscsi_nacl_auth_attr_password,
432 	&iscsi_nacl_auth_attr_authenticate_target,
433 	&iscsi_nacl_auth_attr_userid_mutual,
434 	&iscsi_nacl_auth_attr_password_mutual,
435 	NULL,
436 };
437 
438 /* End items for lio_target_nacl_auth_cit */
439 
440 /* Start items for lio_target_nacl_param_cit */
441 
442 #define ISCSI_NACL_PARAM(name)						\
443 static ssize_t iscsi_nacl_param_##name##_show(struct config_item *item,	\
444 		char *page)						\
445 {									\
446 	struct se_node_acl *se_nacl = param_to_nacl(item);		\
447 	struct iscsi_session *sess;					\
448 	struct se_session *se_sess;					\
449 	ssize_t rb;							\
450 									\
451 	spin_lock_bh(&se_nacl->nacl_sess_lock);				\
452 	se_sess = se_nacl->nacl_sess;					\
453 	if (!se_sess) {							\
454 		rb = snprintf(page, PAGE_SIZE,				\
455 			"No Active iSCSI Session\n");			\
456 	} else {							\
457 		sess = se_sess->fabric_sess_ptr;			\
458 		rb = snprintf(page, PAGE_SIZE, "%u\n",			\
459 			(u32)sess->sess_ops->name);			\
460 	}								\
461 	spin_unlock_bh(&se_nacl->nacl_sess_lock);			\
462 									\
463 	return rb;							\
464 }									\
465 									\
466 CONFIGFS_ATTR_RO(iscsi_nacl_param_, name)
467 
468 ISCSI_NACL_PARAM(MaxConnections);
469 ISCSI_NACL_PARAM(InitialR2T);
470 ISCSI_NACL_PARAM(ImmediateData);
471 ISCSI_NACL_PARAM(MaxBurstLength);
472 ISCSI_NACL_PARAM(FirstBurstLength);
473 ISCSI_NACL_PARAM(DefaultTime2Wait);
474 ISCSI_NACL_PARAM(DefaultTime2Retain);
475 ISCSI_NACL_PARAM(MaxOutstandingR2T);
476 ISCSI_NACL_PARAM(DataPDUInOrder);
477 ISCSI_NACL_PARAM(DataSequenceInOrder);
478 ISCSI_NACL_PARAM(ErrorRecoveryLevel);
479 
480 static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
481 	&iscsi_nacl_param_attr_MaxConnections,
482 	&iscsi_nacl_param_attr_InitialR2T,
483 	&iscsi_nacl_param_attr_ImmediateData,
484 	&iscsi_nacl_param_attr_MaxBurstLength,
485 	&iscsi_nacl_param_attr_FirstBurstLength,
486 	&iscsi_nacl_param_attr_DefaultTime2Wait,
487 	&iscsi_nacl_param_attr_DefaultTime2Retain,
488 	&iscsi_nacl_param_attr_MaxOutstandingR2T,
489 	&iscsi_nacl_param_attr_DataPDUInOrder,
490 	&iscsi_nacl_param_attr_DataSequenceInOrder,
491 	&iscsi_nacl_param_attr_ErrorRecoveryLevel,
492 	NULL,
493 };
494 
495 /* End items for lio_target_nacl_param_cit */
496 
497 /* Start items for lio_target_acl_cit */
498 
499 static ssize_t lio_target_nacl_info_show(struct config_item *item, char *page)
500 {
501 	struct se_node_acl *se_nacl = acl_to_nacl(item);
502 	struct iscsi_session *sess;
503 	struct iscsi_conn *conn;
504 	struct se_session *se_sess;
505 	ssize_t rb = 0;
506 	u32 max_cmd_sn;
507 
508 	spin_lock_bh(&se_nacl->nacl_sess_lock);
509 	se_sess = se_nacl->nacl_sess;
510 	if (!se_sess) {
511 		rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
512 			" Endpoint: %s\n", se_nacl->initiatorname);
513 	} else {
514 		sess = se_sess->fabric_sess_ptr;
515 
516 		rb += sprintf(page+rb, "InitiatorName: %s\n",
517 			sess->sess_ops->InitiatorName);
518 		rb += sprintf(page+rb, "InitiatorAlias: %s\n",
519 			sess->sess_ops->InitiatorAlias);
520 
521 		rb += sprintf(page+rb,
522 			      "LIO Session ID: %u   ISID: 0x%6ph  TSIH: %hu  ",
523 			      sess->sid, sess->isid, sess->tsih);
524 		rb += sprintf(page+rb, "SessionType: %s\n",
525 				(sess->sess_ops->SessionType) ?
526 				"Discovery" : "Normal");
527 		rb += sprintf(page+rb, "Session State: ");
528 		switch (sess->session_state) {
529 		case TARG_SESS_STATE_FREE:
530 			rb += sprintf(page+rb, "TARG_SESS_FREE\n");
531 			break;
532 		case TARG_SESS_STATE_ACTIVE:
533 			rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
534 			break;
535 		case TARG_SESS_STATE_LOGGED_IN:
536 			rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
537 			break;
538 		case TARG_SESS_STATE_FAILED:
539 			rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
540 			break;
541 		case TARG_SESS_STATE_IN_CONTINUE:
542 			rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
543 			break;
544 		default:
545 			rb += sprintf(page+rb, "ERROR: Unknown Session"
546 					" State!\n");
547 			break;
548 		}
549 
550 		rb += sprintf(page+rb, "---------------------[iSCSI Session"
551 				" Values]-----------------------\n");
552 		rb += sprintf(page+rb, "  CmdSN/WR  :  CmdSN/WC  :  ExpCmdSN"
553 				"  :  MaxCmdSN  :     ITT    :     TTT\n");
554 		max_cmd_sn = (u32) atomic_read(&sess->max_cmd_sn);
555 		rb += sprintf(page+rb, " 0x%08x   0x%08x   0x%08x   0x%08x"
556 				"   0x%08x   0x%08x\n",
557 			sess->cmdsn_window,
558 			(max_cmd_sn - sess->exp_cmd_sn) + 1,
559 			sess->exp_cmd_sn, max_cmd_sn,
560 			sess->init_task_tag, sess->targ_xfer_tag);
561 		rb += sprintf(page+rb, "----------------------[iSCSI"
562 				" Connections]-------------------------\n");
563 
564 		spin_lock(&sess->conn_lock);
565 		list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
566 			rb += sprintf(page+rb, "CID: %hu  Connection"
567 					" State: ", conn->cid);
568 			switch (conn->conn_state) {
569 			case TARG_CONN_STATE_FREE:
570 				rb += sprintf(page+rb,
571 					"TARG_CONN_STATE_FREE\n");
572 				break;
573 			case TARG_CONN_STATE_XPT_UP:
574 				rb += sprintf(page+rb,
575 					"TARG_CONN_STATE_XPT_UP\n");
576 				break;
577 			case TARG_CONN_STATE_IN_LOGIN:
578 				rb += sprintf(page+rb,
579 					"TARG_CONN_STATE_IN_LOGIN\n");
580 				break;
581 			case TARG_CONN_STATE_LOGGED_IN:
582 				rb += sprintf(page+rb,
583 					"TARG_CONN_STATE_LOGGED_IN\n");
584 				break;
585 			case TARG_CONN_STATE_IN_LOGOUT:
586 				rb += sprintf(page+rb,
587 					"TARG_CONN_STATE_IN_LOGOUT\n");
588 				break;
589 			case TARG_CONN_STATE_LOGOUT_REQUESTED:
590 				rb += sprintf(page+rb,
591 					"TARG_CONN_STATE_LOGOUT_REQUESTED\n");
592 				break;
593 			case TARG_CONN_STATE_CLEANUP_WAIT:
594 				rb += sprintf(page+rb,
595 					"TARG_CONN_STATE_CLEANUP_WAIT\n");
596 				break;
597 			default:
598 				rb += sprintf(page+rb,
599 					"ERROR: Unknown Connection State!\n");
600 				break;
601 			}
602 
603 			rb += sprintf(page+rb, "   Address %pISc %s", &conn->login_sockaddr,
604 				(conn->network_transport == ISCSI_TCP) ?
605 				"TCP" : "SCTP");
606 			rb += sprintf(page+rb, "  StatSN: 0x%08x\n",
607 				conn->stat_sn);
608 		}
609 		spin_unlock(&sess->conn_lock);
610 	}
611 	spin_unlock_bh(&se_nacl->nacl_sess_lock);
612 
613 	return rb;
614 }
615 
616 static ssize_t lio_target_nacl_cmdsn_depth_show(struct config_item *item,
617 		char *page)
618 {
619 	return sprintf(page, "%u\n", acl_to_nacl(item)->queue_depth);
620 }
621 
622 static ssize_t lio_target_nacl_cmdsn_depth_store(struct config_item *item,
623 		const char *page, size_t count)
624 {
625 	struct se_node_acl *se_nacl = acl_to_nacl(item);
626 	struct se_portal_group *se_tpg = se_nacl->se_tpg;
627 	struct iscsi_portal_group *tpg = container_of(se_tpg,
628 			struct iscsi_portal_group, tpg_se_tpg);
629 	struct config_item *acl_ci, *tpg_ci, *wwn_ci;
630 	u32 cmdsn_depth = 0;
631 	int ret;
632 
633 	ret = kstrtou32(page, 0, &cmdsn_depth);
634 	if (ret)
635 		return ret;
636 	if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
637 		pr_err("Passed cmdsn_depth: %u exceeds"
638 			" TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
639 			TA_DEFAULT_CMDSN_DEPTH_MAX);
640 		return -EINVAL;
641 	}
642 	acl_ci = &se_nacl->acl_group.cg_item;
643 	if (!acl_ci) {
644 		pr_err("Unable to locatel acl_ci\n");
645 		return -EINVAL;
646 	}
647 	tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
648 	if (!tpg_ci) {
649 		pr_err("Unable to locate tpg_ci\n");
650 		return -EINVAL;
651 	}
652 	wwn_ci = &tpg_ci->ci_group->cg_item;
653 	if (!wwn_ci) {
654 		pr_err("Unable to locate config_item wwn_ci\n");
655 		return -EINVAL;
656 	}
657 
658 	if (iscsit_get_tpg(tpg) < 0)
659 		return -EINVAL;
660 
661 	ret = core_tpg_set_initiator_node_queue_depth(se_nacl, cmdsn_depth);
662 
663 	pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
664 		"InitiatorName: %s\n", config_item_name(wwn_ci),
665 		config_item_name(tpg_ci), cmdsn_depth,
666 		config_item_name(acl_ci));
667 
668 	iscsit_put_tpg(tpg);
669 	return (!ret) ? count : (ssize_t)ret;
670 }
671 
672 static ssize_t lio_target_nacl_tag_show(struct config_item *item, char *page)
673 {
674 	return snprintf(page, PAGE_SIZE, "%s", acl_to_nacl(item)->acl_tag);
675 }
676 
677 static ssize_t lio_target_nacl_tag_store(struct config_item *item,
678 		const char *page, size_t count)
679 {
680 	struct se_node_acl *se_nacl = acl_to_nacl(item);
681 	int ret;
682 
683 	ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
684 
685 	if (ret < 0)
686 		return ret;
687 	return count;
688 }
689 
690 CONFIGFS_ATTR_RO(lio_target_nacl_, info);
691 CONFIGFS_ATTR(lio_target_nacl_, cmdsn_depth);
692 CONFIGFS_ATTR(lio_target_nacl_, tag);
693 
694 static struct configfs_attribute *lio_target_initiator_attrs[] = {
695 	&lio_target_nacl_attr_info,
696 	&lio_target_nacl_attr_cmdsn_depth,
697 	&lio_target_nacl_attr_tag,
698 	NULL,
699 };
700 
701 static int lio_target_init_nodeacl(struct se_node_acl *se_nacl,
702 		const char *name)
703 {
704 	struct iscsi_node_acl *acl =
705 		container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
706 
707 	config_group_init_type_name(&acl->node_stat_grps.iscsi_sess_stats_group,
708 			"iscsi_sess_stats", &iscsi_stat_sess_cit);
709 	configfs_add_default_group(&acl->node_stat_grps.iscsi_sess_stats_group,
710 			&se_nacl->acl_fabric_stat_group);
711 	return 0;
712 }
713 
714 /* End items for lio_target_acl_cit */
715 
716 /* Start items for lio_target_tpg_attrib_cit */
717 
718 #define DEF_TPG_ATTRIB(name)						\
719 									\
720 static ssize_t iscsi_tpg_attrib_##name##_show(struct config_item *item,	\
721 		char *page)						\
722 {									\
723 	struct se_portal_group *se_tpg = attrib_to_tpg(item);		\
724 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
725 			struct iscsi_portal_group, tpg_se_tpg);	\
726 	ssize_t rb;							\
727 									\
728 	if (iscsit_get_tpg(tpg) < 0)					\
729 		return -EINVAL;						\
730 									\
731 	rb = sprintf(page, "%u\n", tpg->tpg_attrib.name);		\
732 	iscsit_put_tpg(tpg);						\
733 	return rb;							\
734 }									\
735 									\
736 static ssize_t iscsi_tpg_attrib_##name##_store(struct config_item *item,\
737 		const char *page, size_t count)				\
738 {									\
739 	struct se_portal_group *se_tpg = attrib_to_tpg(item);		\
740 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
741 			struct iscsi_portal_group, tpg_se_tpg);	\
742 	u32 val;							\
743 	int ret;							\
744 									\
745 	if (iscsit_get_tpg(tpg) < 0)					\
746 		return -EINVAL;						\
747 									\
748 	ret = kstrtou32(page, 0, &val);					\
749 	if (ret)							\
750 		goto out;						\
751 	ret = iscsit_ta_##name(tpg, val);				\
752 	if (ret < 0)							\
753 		goto out;						\
754 									\
755 	iscsit_put_tpg(tpg);						\
756 	return count;							\
757 out:									\
758 	iscsit_put_tpg(tpg);						\
759 	return ret;							\
760 }									\
761 CONFIGFS_ATTR(iscsi_tpg_attrib_, name)
762 
763 DEF_TPG_ATTRIB(authentication);
764 DEF_TPG_ATTRIB(login_timeout);
765 DEF_TPG_ATTRIB(netif_timeout);
766 DEF_TPG_ATTRIB(generate_node_acls);
767 DEF_TPG_ATTRIB(default_cmdsn_depth);
768 DEF_TPG_ATTRIB(cache_dynamic_acls);
769 DEF_TPG_ATTRIB(demo_mode_write_protect);
770 DEF_TPG_ATTRIB(prod_mode_write_protect);
771 DEF_TPG_ATTRIB(demo_mode_discovery);
772 DEF_TPG_ATTRIB(default_erl);
773 DEF_TPG_ATTRIB(t10_pi);
774 DEF_TPG_ATTRIB(fabric_prot_type);
775 DEF_TPG_ATTRIB(tpg_enabled_sendtargets);
776 DEF_TPG_ATTRIB(login_keys_workaround);
777 
778 static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
779 	&iscsi_tpg_attrib_attr_authentication,
780 	&iscsi_tpg_attrib_attr_login_timeout,
781 	&iscsi_tpg_attrib_attr_netif_timeout,
782 	&iscsi_tpg_attrib_attr_generate_node_acls,
783 	&iscsi_tpg_attrib_attr_default_cmdsn_depth,
784 	&iscsi_tpg_attrib_attr_cache_dynamic_acls,
785 	&iscsi_tpg_attrib_attr_demo_mode_write_protect,
786 	&iscsi_tpg_attrib_attr_prod_mode_write_protect,
787 	&iscsi_tpg_attrib_attr_demo_mode_discovery,
788 	&iscsi_tpg_attrib_attr_default_erl,
789 	&iscsi_tpg_attrib_attr_t10_pi,
790 	&iscsi_tpg_attrib_attr_fabric_prot_type,
791 	&iscsi_tpg_attrib_attr_tpg_enabled_sendtargets,
792 	&iscsi_tpg_attrib_attr_login_keys_workaround,
793 	NULL,
794 };
795 
796 /* End items for lio_target_tpg_attrib_cit */
797 
798 /* Start items for lio_target_tpg_auth_cit */
799 
800 #define __DEF_TPG_AUTH_STR(prefix, name, flags)					\
801 static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg,	\
802 		char *page)							\
803 {										\
804 	struct iscsi_portal_group *tpg = container_of(se_tpg,			\
805 				struct iscsi_portal_group, tpg_se_tpg);		\
806 	struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;			\
807 										\
808 	if (!capable(CAP_SYS_ADMIN))						\
809 		return -EPERM;							\
810 										\
811 	return snprintf(page, PAGE_SIZE, "%s\n", auth->name);			\
812 }										\
813 										\
814 static ssize_t __iscsi_##prefix##_##name##_store(struct se_portal_group *se_tpg,\
815 		const char *page, size_t count)					\
816 {										\
817 	struct iscsi_portal_group *tpg = container_of(se_tpg,			\
818 				struct iscsi_portal_group, tpg_se_tpg);		\
819 	struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;			\
820 										\
821 	if (!capable(CAP_SYS_ADMIN))						\
822 		return -EPERM;							\
823 										\
824 	snprintf(auth->name, sizeof(auth->name), "%s", page);			\
825 	if (!(strncmp("NULL", auth->name, 4)))					\
826 		auth->naf_flags &= ~flags;					\
827 	else									\
828 		auth->naf_flags |= flags;					\
829 										\
830 	if ((auth->naf_flags & NAF_USERID_IN_SET) &&				\
831 	    (auth->naf_flags & NAF_PASSWORD_IN_SET))				\
832 		auth->authenticate_target = 1;					\
833 	else									\
834 		auth->authenticate_target = 0;					\
835 										\
836 	return count;								\
837 }
838 
839 #define DEF_TPG_AUTH_STR(name, flags)						\
840 	__DEF_TPG_AUTH_STR(tpg_auth, name, flags)				\
841 static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item,		\
842 		char *page)							\
843 {										\
844 	return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page);		\
845 }										\
846 										\
847 static ssize_t iscsi_tpg_auth_##name##_store(struct config_item *item,		\
848 		const char *page, size_t count)					\
849 {										\
850 	return __iscsi_tpg_auth_##name##_store(auth_to_tpg(item), page, count);	\
851 }										\
852 										\
853 CONFIGFS_ATTR(iscsi_tpg_auth_, name);
854 
855 
856 DEF_TPG_AUTH_STR(userid, NAF_USERID_SET);
857 DEF_TPG_AUTH_STR(password, NAF_PASSWORD_SET);
858 DEF_TPG_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
859 DEF_TPG_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
860 
861 #define __DEF_TPG_AUTH_INT(prefix, name)					\
862 static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg,	\
863 		char *page)								\
864 {										\
865 	struct iscsi_portal_group *tpg = container_of(se_tpg,			\
866 				struct iscsi_portal_group, tpg_se_tpg);		\
867 	struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;			\
868 										\
869 	if (!capable(CAP_SYS_ADMIN))						\
870 		return -EPERM;							\
871 										\
872 	return snprintf(page, PAGE_SIZE, "%d\n", auth->name);			\
873 }
874 
875 #define DEF_TPG_AUTH_INT(name)							\
876 	__DEF_TPG_AUTH_INT(tpg_auth, name)					\
877 static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item,		\
878 		char *page) \
879 {										\
880 	return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page);		\
881 }										\
882 CONFIGFS_ATTR_RO(iscsi_tpg_auth_, name);
883 
884 DEF_TPG_AUTH_INT(authenticate_target);
885 
886 static struct configfs_attribute *lio_target_tpg_auth_attrs[] = {
887 	&iscsi_tpg_auth_attr_userid,
888 	&iscsi_tpg_auth_attr_password,
889 	&iscsi_tpg_auth_attr_authenticate_target,
890 	&iscsi_tpg_auth_attr_userid_mutual,
891 	&iscsi_tpg_auth_attr_password_mutual,
892 	NULL,
893 };
894 
895 /* End items for lio_target_tpg_auth_cit */
896 
897 /* Start items for lio_target_tpg_param_cit */
898 
899 #define DEF_TPG_PARAM(name)						\
900 static ssize_t iscsi_tpg_param_##name##_show(struct config_item *item,	\
901 		char *page)						\
902 {									\
903 	struct se_portal_group *se_tpg = param_to_tpg(item);		\
904 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
905 			struct iscsi_portal_group, tpg_se_tpg);		\
906 	struct iscsi_param *param;					\
907 	ssize_t rb;							\
908 									\
909 	if (iscsit_get_tpg(tpg) < 0)					\
910 		return -EINVAL;						\
911 									\
912 	param = iscsi_find_param_from_key(__stringify(name),		\
913 				tpg->param_list);			\
914 	if (!param) {							\
915 		iscsit_put_tpg(tpg);					\
916 		return -EINVAL;						\
917 	}								\
918 	rb = snprintf(page, PAGE_SIZE, "%s\n", param->value);		\
919 									\
920 	iscsit_put_tpg(tpg);						\
921 	return rb;							\
922 }									\
923 static ssize_t iscsi_tpg_param_##name##_store(struct config_item *item, \
924 		const char *page, size_t count)				\
925 {									\
926 	struct se_portal_group *se_tpg = param_to_tpg(item);		\
927 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
928 			struct iscsi_portal_group, tpg_se_tpg);		\
929 	char *buf;							\
930 	int ret, len;							\
931 									\
932 	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);				\
933 	if (!buf)							\
934 		return -ENOMEM;						\
935 	len = snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page);	\
936 	if (isspace(buf[len-1]))					\
937 		buf[len-1] = '\0'; /* Kill newline */			\
938 									\
939 	if (iscsit_get_tpg(tpg) < 0) {					\
940 		kfree(buf);						\
941 		return -EINVAL;						\
942 	}								\
943 									\
944 	ret = iscsi_change_param_value(buf, tpg->param_list, 1);	\
945 	if (ret < 0)							\
946 		goto out;						\
947 									\
948 	kfree(buf);							\
949 	iscsit_put_tpg(tpg);						\
950 	return count;							\
951 out:									\
952 	kfree(buf);							\
953 	iscsit_put_tpg(tpg);						\
954 	return -EINVAL;							\
955 }									\
956 CONFIGFS_ATTR(iscsi_tpg_param_, name)
957 
958 DEF_TPG_PARAM(AuthMethod);
959 DEF_TPG_PARAM(HeaderDigest);
960 DEF_TPG_PARAM(DataDigest);
961 DEF_TPG_PARAM(MaxConnections);
962 DEF_TPG_PARAM(TargetAlias);
963 DEF_TPG_PARAM(InitialR2T);
964 DEF_TPG_PARAM(ImmediateData);
965 DEF_TPG_PARAM(MaxRecvDataSegmentLength);
966 DEF_TPG_PARAM(MaxXmitDataSegmentLength);
967 DEF_TPG_PARAM(MaxBurstLength);
968 DEF_TPG_PARAM(FirstBurstLength);
969 DEF_TPG_PARAM(DefaultTime2Wait);
970 DEF_TPG_PARAM(DefaultTime2Retain);
971 DEF_TPG_PARAM(MaxOutstandingR2T);
972 DEF_TPG_PARAM(DataPDUInOrder);
973 DEF_TPG_PARAM(DataSequenceInOrder);
974 DEF_TPG_PARAM(ErrorRecoveryLevel);
975 DEF_TPG_PARAM(IFMarker);
976 DEF_TPG_PARAM(OFMarker);
977 DEF_TPG_PARAM(IFMarkInt);
978 DEF_TPG_PARAM(OFMarkInt);
979 
980 static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
981 	&iscsi_tpg_param_attr_AuthMethod,
982 	&iscsi_tpg_param_attr_HeaderDigest,
983 	&iscsi_tpg_param_attr_DataDigest,
984 	&iscsi_tpg_param_attr_MaxConnections,
985 	&iscsi_tpg_param_attr_TargetAlias,
986 	&iscsi_tpg_param_attr_InitialR2T,
987 	&iscsi_tpg_param_attr_ImmediateData,
988 	&iscsi_tpg_param_attr_MaxRecvDataSegmentLength,
989 	&iscsi_tpg_param_attr_MaxXmitDataSegmentLength,
990 	&iscsi_tpg_param_attr_MaxBurstLength,
991 	&iscsi_tpg_param_attr_FirstBurstLength,
992 	&iscsi_tpg_param_attr_DefaultTime2Wait,
993 	&iscsi_tpg_param_attr_DefaultTime2Retain,
994 	&iscsi_tpg_param_attr_MaxOutstandingR2T,
995 	&iscsi_tpg_param_attr_DataPDUInOrder,
996 	&iscsi_tpg_param_attr_DataSequenceInOrder,
997 	&iscsi_tpg_param_attr_ErrorRecoveryLevel,
998 	&iscsi_tpg_param_attr_IFMarker,
999 	&iscsi_tpg_param_attr_OFMarker,
1000 	&iscsi_tpg_param_attr_IFMarkInt,
1001 	&iscsi_tpg_param_attr_OFMarkInt,
1002 	NULL,
1003 };
1004 
1005 /* End items for lio_target_tpg_param_cit */
1006 
1007 /* Start items for lio_target_tpg_cit */
1008 
1009 static ssize_t lio_target_tpg_enable_show(struct config_item *item, char *page)
1010 {
1011 	struct se_portal_group *se_tpg = to_tpg(item);
1012 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1013 			struct iscsi_portal_group, tpg_se_tpg);
1014 	ssize_t len;
1015 
1016 	spin_lock(&tpg->tpg_state_lock);
1017 	len = sprintf(page, "%d\n",
1018 			(tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1019 	spin_unlock(&tpg->tpg_state_lock);
1020 
1021 	return len;
1022 }
1023 
1024 static ssize_t lio_target_tpg_enable_store(struct config_item *item,
1025 		const char *page, size_t count)
1026 {
1027 	struct se_portal_group *se_tpg = to_tpg(item);
1028 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1029 			struct iscsi_portal_group, tpg_se_tpg);
1030 	u32 op;
1031 	int ret;
1032 
1033 	ret = kstrtou32(page, 0, &op);
1034 	if (ret)
1035 		return ret;
1036 	if ((op != 1) && (op != 0)) {
1037 		pr_err("Illegal value for tpg_enable: %u\n", op);
1038 		return -EINVAL;
1039 	}
1040 
1041 	ret = iscsit_get_tpg(tpg);
1042 	if (ret < 0)
1043 		return -EINVAL;
1044 
1045 	if (op) {
1046 		ret = iscsit_tpg_enable_portal_group(tpg);
1047 		if (ret < 0)
1048 			goto out;
1049 	} else {
1050 		/*
1051 		 * iscsit_tpg_disable_portal_group() assumes force=1
1052 		 */
1053 		ret = iscsit_tpg_disable_portal_group(tpg, 1);
1054 		if (ret < 0)
1055 			goto out;
1056 	}
1057 
1058 	iscsit_put_tpg(tpg);
1059 	return count;
1060 out:
1061 	iscsit_put_tpg(tpg);
1062 	return -EINVAL;
1063 }
1064 
1065 
1066 static ssize_t lio_target_tpg_dynamic_sessions_show(struct config_item *item,
1067 		char *page)
1068 {
1069 	return target_show_dynamic_sessions(to_tpg(item), page);
1070 }
1071 
1072 CONFIGFS_ATTR(lio_target_tpg_, enable);
1073 CONFIGFS_ATTR_RO(lio_target_tpg_, dynamic_sessions);
1074 
1075 static struct configfs_attribute *lio_target_tpg_attrs[] = {
1076 	&lio_target_tpg_attr_enable,
1077 	&lio_target_tpg_attr_dynamic_sessions,
1078 	NULL,
1079 };
1080 
1081 /* End items for lio_target_tpg_cit */
1082 
1083 /* Start items for lio_target_tiqn_cit */
1084 
1085 static struct se_portal_group *lio_target_tiqn_addtpg(struct se_wwn *wwn,
1086 						      const char *name)
1087 {
1088 	struct iscsi_portal_group *tpg;
1089 	struct iscsi_tiqn *tiqn;
1090 	char *tpgt_str;
1091 	int ret;
1092 	u16 tpgt;
1093 
1094 	tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1095 	/*
1096 	 * Only tpgt_# directory groups can be created below
1097 	 * target/iscsi/iqn.superturodiskarry/
1098 	 */
1099 	tpgt_str = strstr(name, "tpgt_");
1100 	if (!tpgt_str) {
1101 		pr_err("Unable to locate \"tpgt_#\" directory"
1102 				" group\n");
1103 		return NULL;
1104 	}
1105 	tpgt_str += 5; /* Skip ahead of "tpgt_" */
1106 	ret = kstrtou16(tpgt_str, 0, &tpgt);
1107 	if (ret)
1108 		return NULL;
1109 
1110 	tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1111 	if (!tpg)
1112 		return NULL;
1113 
1114 	ret = core_tpg_register(wwn, &tpg->tpg_se_tpg, SCSI_PROTOCOL_ISCSI);
1115 	if (ret < 0)
1116 		goto free_out;
1117 
1118 	ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1119 	if (ret != 0)
1120 		goto out;
1121 
1122 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1123 	pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1124 			name);
1125 	return &tpg->tpg_se_tpg;
1126 out:
1127 	core_tpg_deregister(&tpg->tpg_se_tpg);
1128 free_out:
1129 	kfree(tpg);
1130 	return NULL;
1131 }
1132 
1133 static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1134 {
1135 	struct iscsi_portal_group *tpg;
1136 	struct iscsi_tiqn *tiqn;
1137 
1138 	tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1139 	tiqn = tpg->tpg_tiqn;
1140 	/*
1141 	 * iscsit_tpg_del_portal_group() assumes force=1
1142 	 */
1143 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1144 	iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1145 }
1146 
1147 /* End items for lio_target_tiqn_cit */
1148 
1149 /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1150 
1151 static ssize_t lio_target_wwn_lio_version_show(struct config_item *item,
1152 		char *page)
1153 {
1154 	return sprintf(page, "Datera Inc. iSCSI Target "ISCSIT_VERSION"\n");
1155 }
1156 
1157 CONFIGFS_ATTR_RO(lio_target_wwn_, lio_version);
1158 
1159 static struct configfs_attribute *lio_target_wwn_attrs[] = {
1160 	&lio_target_wwn_attr_lio_version,
1161 	NULL,
1162 };
1163 
1164 static struct se_wwn *lio_target_call_coreaddtiqn(
1165 	struct target_fabric_configfs *tf,
1166 	struct config_group *group,
1167 	const char *name)
1168 {
1169 	struct iscsi_tiqn *tiqn;
1170 
1171 	tiqn = iscsit_add_tiqn((unsigned char *)name);
1172 	if (IS_ERR(tiqn))
1173 		return ERR_CAST(tiqn);
1174 
1175 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1176 	pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1177 			" %s\n", name);
1178 	return &tiqn->tiqn_wwn;
1179 }
1180 
1181 static void lio_target_add_wwn_groups(struct se_wwn *wwn)
1182 {
1183 	struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1184 
1185 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1186 			"iscsi_instance", &iscsi_stat_instance_cit);
1187 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1188 			&tiqn->tiqn_wwn.fabric_stat_group);
1189 
1190 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1191 			"iscsi_sess_err", &iscsi_stat_sess_err_cit);
1192 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1193 			&tiqn->tiqn_wwn.fabric_stat_group);
1194 
1195 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1196 			"iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
1197 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1198 			&tiqn->tiqn_wwn.fabric_stat_group);
1199 
1200 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1201 			"iscsi_login_stats", &iscsi_stat_login_cit);
1202 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1203 			&tiqn->tiqn_wwn.fabric_stat_group);
1204 
1205 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1206 			"iscsi_logout_stats", &iscsi_stat_logout_cit);
1207 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1208 			&tiqn->tiqn_wwn.fabric_stat_group);
1209 }
1210 
1211 static void lio_target_call_coredeltiqn(
1212 	struct se_wwn *wwn)
1213 {
1214 	struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1215 
1216 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1217 			tiqn->tiqn);
1218 	iscsit_del_tiqn(tiqn);
1219 }
1220 
1221 /* End LIO-Target TIQN struct contig_lio_target_cit */
1222 
1223 /* Start lio_target_discovery_auth_cit */
1224 
1225 #define DEF_DISC_AUTH_STR(name, flags)					\
1226 	__DEF_NACL_AUTH_STR(disc, name, flags)				\
1227 static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1228 {									\
1229 	return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl,\
1230 		page);							\
1231 }									\
1232 static ssize_t iscsi_disc_##name##_store(struct config_item *item,	\
1233 		const char *page, size_t count)				\
1234 {									\
1235 	return __iscsi_disc_##name##_store(&iscsit_global->discovery_acl,	\
1236 		page, count);						\
1237 									\
1238 }									\
1239 CONFIGFS_ATTR(iscsi_disc_, name)
1240 
1241 DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
1242 DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
1243 DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1244 DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1245 
1246 #define DEF_DISC_AUTH_INT(name)						\
1247 	__DEF_NACL_AUTH_INT(disc, name)					\
1248 static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1249 {									\
1250 	return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl, \
1251 			page);						\
1252 }									\
1253 CONFIGFS_ATTR_RO(iscsi_disc_, name)
1254 
1255 DEF_DISC_AUTH_INT(authenticate_target);
1256 
1257 
1258 static ssize_t iscsi_disc_enforce_discovery_auth_show(struct config_item *item,
1259 		char *page)
1260 {
1261 	struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1262 
1263 	return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1264 }
1265 
1266 static ssize_t iscsi_disc_enforce_discovery_auth_store(struct config_item *item,
1267 		const char *page, size_t count)
1268 {
1269 	struct iscsi_param *param;
1270 	struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1271 	u32 op;
1272 	int err;
1273 
1274 	err = kstrtou32(page, 0, &op);
1275 	if (err)
1276 		return -EINVAL;
1277 	if ((op != 1) && (op != 0)) {
1278 		pr_err("Illegal value for enforce_discovery_auth:"
1279 				" %u\n", op);
1280 		return -EINVAL;
1281 	}
1282 
1283 	if (!discovery_tpg) {
1284 		pr_err("iscsit_global->discovery_tpg is NULL\n");
1285 		return -EINVAL;
1286 	}
1287 
1288 	param = iscsi_find_param_from_key(AUTHMETHOD,
1289 				discovery_tpg->param_list);
1290 	if (!param)
1291 		return -EINVAL;
1292 
1293 	if (op) {
1294 		/*
1295 		 * Reset the AuthMethod key to CHAP.
1296 		 */
1297 		if (iscsi_update_param_value(param, CHAP) < 0)
1298 			return -EINVAL;
1299 
1300 		discovery_tpg->tpg_attrib.authentication = 1;
1301 		iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1302 		pr_debug("LIO-CORE[0] Successfully enabled"
1303 			" authentication enforcement for iSCSI"
1304 			" Discovery TPG\n");
1305 	} else {
1306 		/*
1307 		 * Reset the AuthMethod key to CHAP,None
1308 		 */
1309 		if (iscsi_update_param_value(param, "CHAP,None") < 0)
1310 			return -EINVAL;
1311 
1312 		discovery_tpg->tpg_attrib.authentication = 0;
1313 		iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1314 		pr_debug("LIO-CORE[0] Successfully disabled"
1315 			" authentication enforcement for iSCSI"
1316 			" Discovery TPG\n");
1317 	}
1318 
1319 	return count;
1320 }
1321 
1322 CONFIGFS_ATTR(iscsi_disc_, enforce_discovery_auth);
1323 
1324 static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
1325 	&iscsi_disc_attr_userid,
1326 	&iscsi_disc_attr_password,
1327 	&iscsi_disc_attr_authenticate_target,
1328 	&iscsi_disc_attr_userid_mutual,
1329 	&iscsi_disc_attr_password_mutual,
1330 	&iscsi_disc_attr_enforce_discovery_auth,
1331 	NULL,
1332 };
1333 
1334 /* End lio_target_discovery_auth_cit */
1335 
1336 /* Start functions for target_core_fabric_ops */
1337 
1338 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1339 {
1340 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1341 
1342 	return cmd->i_state;
1343 }
1344 
1345 static u32 lio_sess_get_index(struct se_session *se_sess)
1346 {
1347 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1348 
1349 	return sess->session_index;
1350 }
1351 
1352 static u32 lio_sess_get_initiator_sid(
1353 	struct se_session *se_sess,
1354 	unsigned char *buf,
1355 	u32 size)
1356 {
1357 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1358 	/*
1359 	 * iSCSI Initiator Session Identifier from RFC-3720.
1360 	 */
1361 	return snprintf(buf, size, "%6phN", sess->isid);
1362 }
1363 
1364 static int lio_queue_data_in(struct se_cmd *se_cmd)
1365 {
1366 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1367 	struct iscsi_conn *conn = cmd->conn;
1368 
1369 	cmd->i_state = ISTATE_SEND_DATAIN;
1370 	return conn->conn_transport->iscsit_queue_data_in(conn, cmd);
1371 }
1372 
1373 static int lio_write_pending(struct se_cmd *se_cmd)
1374 {
1375 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1376 	struct iscsi_conn *conn = cmd->conn;
1377 
1378 	if (!cmd->immediate_data && !cmd->unsolicited_data)
1379 		return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1380 
1381 	return 0;
1382 }
1383 
1384 static int lio_queue_status(struct se_cmd *se_cmd)
1385 {
1386 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1387 	struct iscsi_conn *conn = cmd->conn;
1388 
1389 	cmd->i_state = ISTATE_SEND_STATUS;
1390 
1391 	if (cmd->se_cmd.scsi_status || cmd->sense_reason) {
1392 		return iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1393 	}
1394 	return conn->conn_transport->iscsit_queue_status(conn, cmd);
1395 }
1396 
1397 static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
1398 {
1399 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1400 
1401 	cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1402 	iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1403 }
1404 
1405 static void lio_aborted_task(struct se_cmd *se_cmd)
1406 {
1407 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1408 
1409 	cmd->conn->conn_transport->iscsit_aborted_task(cmd->conn, cmd);
1410 }
1411 
1412 static inline struct iscsi_portal_group *iscsi_tpg(struct se_portal_group *se_tpg)
1413 {
1414 	return container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1415 }
1416 
1417 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1418 {
1419 	return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn;
1420 }
1421 
1422 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1423 {
1424 	return iscsi_tpg(se_tpg)->tpgt;
1425 }
1426 
1427 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1428 {
1429 	return iscsi_tpg(se_tpg)->tpg_attrib.default_cmdsn_depth;
1430 }
1431 
1432 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1433 {
1434 	return iscsi_tpg(se_tpg)->tpg_attrib.generate_node_acls;
1435 }
1436 
1437 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1438 {
1439 	return iscsi_tpg(se_tpg)->tpg_attrib.cache_dynamic_acls;
1440 }
1441 
1442 static int lio_tpg_check_demo_mode_write_protect(
1443 	struct se_portal_group *se_tpg)
1444 {
1445 	return iscsi_tpg(se_tpg)->tpg_attrib.demo_mode_write_protect;
1446 }
1447 
1448 static int lio_tpg_check_prod_mode_write_protect(
1449 	struct se_portal_group *se_tpg)
1450 {
1451 	return iscsi_tpg(se_tpg)->tpg_attrib.prod_mode_write_protect;
1452 }
1453 
1454 static int lio_tpg_check_prot_fabric_only(
1455 	struct se_portal_group *se_tpg)
1456 {
1457 	/*
1458 	 * Only report fabric_prot_type if t10_pi has also been enabled
1459 	 * for incoming ib_isert sessions.
1460 	 */
1461 	if (!iscsi_tpg(se_tpg)->tpg_attrib.t10_pi)
1462 		return 0;
1463 	return iscsi_tpg(se_tpg)->tpg_attrib.fabric_prot_type;
1464 }
1465 
1466 /*
1467  * This function calls iscsit_inc_session_usage_count() on the
1468  * struct iscsi_session in question.
1469  */
1470 static void lio_tpg_close_session(struct se_session *se_sess)
1471 {
1472 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1473 	struct se_portal_group *se_tpg = &sess->tpg->tpg_se_tpg;
1474 
1475 	spin_lock_bh(&se_tpg->session_lock);
1476 	spin_lock(&sess->conn_lock);
1477 	if (atomic_read(&sess->session_fall_back_to_erl0) ||
1478 	    atomic_read(&sess->session_logout) ||
1479 	    atomic_read(&sess->session_close) ||
1480 	    (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1481 		spin_unlock(&sess->conn_lock);
1482 		spin_unlock_bh(&se_tpg->session_lock);
1483 		return;
1484 	}
1485 	iscsit_inc_session_usage_count(sess);
1486 	atomic_set(&sess->session_reinstatement, 1);
1487 	atomic_set(&sess->session_fall_back_to_erl0, 1);
1488 	atomic_set(&sess->session_close, 1);
1489 	spin_unlock(&sess->conn_lock);
1490 
1491 	iscsit_stop_time2retain_timer(sess);
1492 	spin_unlock_bh(&se_tpg->session_lock);
1493 
1494 	iscsit_stop_session(sess, 1, 1);
1495 	iscsit_dec_session_usage_count(sess);
1496 }
1497 
1498 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1499 {
1500 	return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn_index;
1501 }
1502 
1503 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1504 {
1505 	struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1506 				se_node_acl);
1507 	struct se_portal_group *se_tpg = se_acl->se_tpg;
1508 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1509 				struct iscsi_portal_group, tpg_se_tpg);
1510 
1511 	acl->node_attrib.nacl = acl;
1512 	iscsit_set_default_node_attribues(acl, tpg);
1513 }
1514 
1515 static int lio_check_stop_free(struct se_cmd *se_cmd)
1516 {
1517 	return target_put_sess_cmd(se_cmd);
1518 }
1519 
1520 static void lio_release_cmd(struct se_cmd *se_cmd)
1521 {
1522 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1523 
1524 	pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
1525 	iscsit_release_cmd(cmd);
1526 }
1527 
1528 const struct target_core_fabric_ops iscsi_ops = {
1529 	.module				= THIS_MODULE,
1530 	.fabric_alias			= "iscsi",
1531 	.fabric_name			= "iSCSI",
1532 	.node_acl_size			= sizeof(struct iscsi_node_acl),
1533 	.tpg_get_wwn			= lio_tpg_get_endpoint_wwn,
1534 	.tpg_get_tag			= lio_tpg_get_tag,
1535 	.tpg_get_default_depth		= lio_tpg_get_default_depth,
1536 	.tpg_check_demo_mode		= lio_tpg_check_demo_mode,
1537 	.tpg_check_demo_mode_cache	= lio_tpg_check_demo_mode_cache,
1538 	.tpg_check_demo_mode_write_protect =
1539 			lio_tpg_check_demo_mode_write_protect,
1540 	.tpg_check_prod_mode_write_protect =
1541 			lio_tpg_check_prod_mode_write_protect,
1542 	.tpg_check_prot_fabric_only	= &lio_tpg_check_prot_fabric_only,
1543 	.tpg_get_inst_index		= lio_tpg_get_inst_index,
1544 	.check_stop_free		= lio_check_stop_free,
1545 	.release_cmd			= lio_release_cmd,
1546 	.close_session			= lio_tpg_close_session,
1547 	.sess_get_index			= lio_sess_get_index,
1548 	.sess_get_initiator_sid		= lio_sess_get_initiator_sid,
1549 	.write_pending			= lio_write_pending,
1550 	.set_default_node_attributes	= lio_set_default_node_attributes,
1551 	.get_cmd_state			= iscsi_get_cmd_state,
1552 	.queue_data_in			= lio_queue_data_in,
1553 	.queue_status			= lio_queue_status,
1554 	.queue_tm_rsp			= lio_queue_tm_rsp,
1555 	.aborted_task			= lio_aborted_task,
1556 	.fabric_make_wwn		= lio_target_call_coreaddtiqn,
1557 	.fabric_drop_wwn		= lio_target_call_coredeltiqn,
1558 	.add_wwn_groups			= lio_target_add_wwn_groups,
1559 	.fabric_make_tpg		= lio_target_tiqn_addtpg,
1560 	.fabric_drop_tpg		= lio_target_tiqn_deltpg,
1561 	.fabric_make_np			= lio_target_call_addnptotpg,
1562 	.fabric_drop_np			= lio_target_call_delnpfromtpg,
1563 	.fabric_init_nodeacl		= lio_target_init_nodeacl,
1564 
1565 	.tfc_discovery_attrs		= lio_target_discovery_auth_attrs,
1566 	.tfc_wwn_attrs			= lio_target_wwn_attrs,
1567 	.tfc_tpg_base_attrs		= lio_target_tpg_attrs,
1568 	.tfc_tpg_attrib_attrs		= lio_target_tpg_attrib_attrs,
1569 	.tfc_tpg_auth_attrs		= lio_target_tpg_auth_attrs,
1570 	.tfc_tpg_param_attrs		= lio_target_tpg_param_attrs,
1571 	.tfc_tpg_np_base_attrs		= lio_target_portal_attrs,
1572 	.tfc_tpg_nacl_base_attrs	= lio_target_initiator_attrs,
1573 	.tfc_tpg_nacl_attrib_attrs	= lio_target_nacl_attrib_attrs,
1574 	.tfc_tpg_nacl_auth_attrs	= lio_target_nacl_auth_attrs,
1575 	.tfc_tpg_nacl_param_attrs	= lio_target_nacl_param_attrs,
1576 
1577 	.write_pending_must_be_called	= true,
1578 };
1579