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