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 ssize_t lio_target_wwn_cpus_allowed_list_show(
1131 		struct config_item *item, char *page)
1132 {
1133 	return sprintf(page, "%*pbl\n",
1134 		       cpumask_pr_args(iscsit_global->allowed_cpumask));
1135 }
1136 
1137 static ssize_t lio_target_wwn_cpus_allowed_list_store(
1138 		struct config_item *item, const char *page, size_t count)
1139 {
1140 	int ret = -ENOMEM;
1141 	char *orig;
1142 	cpumask_var_t new_allowed_cpumask;
1143 
1144 	if (!zalloc_cpumask_var(&new_allowed_cpumask, GFP_KERNEL))
1145 		goto out;
1146 
1147 	orig = kstrdup(page, GFP_KERNEL);
1148 	if (!orig)
1149 		goto out_free_cpumask;
1150 
1151 	ret = cpulist_parse(orig, new_allowed_cpumask);
1152 	if (!ret)
1153 		cpumask_copy(iscsit_global->allowed_cpumask,
1154 			     new_allowed_cpumask);
1155 
1156 	kfree(orig);
1157 out_free_cpumask:
1158 	free_cpumask_var(new_allowed_cpumask);
1159 out:
1160 	return ret ? ret : count;
1161 }
1162 
1163 CONFIGFS_ATTR(lio_target_wwn_, cpus_allowed_list);
1164 
1165 static struct configfs_attribute *lio_target_wwn_attrs[] = {
1166 	&lio_target_wwn_attr_lio_version,
1167 	&lio_target_wwn_attr_cpus_allowed_list,
1168 	NULL,
1169 };
1170 
1171 static struct se_wwn *lio_target_call_coreaddtiqn(
1172 	struct target_fabric_configfs *tf,
1173 	struct config_group *group,
1174 	const char *name)
1175 {
1176 	struct iscsi_tiqn *tiqn;
1177 
1178 	tiqn = iscsit_add_tiqn((unsigned char *)name);
1179 	if (IS_ERR(tiqn))
1180 		return ERR_CAST(tiqn);
1181 
1182 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1183 	pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1184 			" %s\n", name);
1185 	return &tiqn->tiqn_wwn;
1186 }
1187 
1188 static void lio_target_add_wwn_groups(struct se_wwn *wwn)
1189 {
1190 	struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1191 
1192 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1193 			"iscsi_instance", &iscsi_stat_instance_cit);
1194 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1195 			&tiqn->tiqn_wwn.fabric_stat_group);
1196 
1197 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1198 			"iscsi_sess_err", &iscsi_stat_sess_err_cit);
1199 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1200 			&tiqn->tiqn_wwn.fabric_stat_group);
1201 
1202 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1203 			"iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
1204 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1205 			&tiqn->tiqn_wwn.fabric_stat_group);
1206 
1207 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1208 			"iscsi_login_stats", &iscsi_stat_login_cit);
1209 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1210 			&tiqn->tiqn_wwn.fabric_stat_group);
1211 
1212 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1213 			"iscsi_logout_stats", &iscsi_stat_logout_cit);
1214 	configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1215 			&tiqn->tiqn_wwn.fabric_stat_group);
1216 }
1217 
1218 static void lio_target_call_coredeltiqn(
1219 	struct se_wwn *wwn)
1220 {
1221 	struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1222 
1223 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1224 			tiqn->tiqn);
1225 	iscsit_del_tiqn(tiqn);
1226 }
1227 
1228 /* End LIO-Target TIQN struct contig_lio_target_cit */
1229 
1230 /* Start lio_target_discovery_auth_cit */
1231 
1232 #define DEF_DISC_AUTH_STR(name, flags)					\
1233 	__DEF_NACL_AUTH_STR(disc, name, flags)				\
1234 static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1235 {									\
1236 	return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl,\
1237 		page);							\
1238 }									\
1239 static ssize_t iscsi_disc_##name##_store(struct config_item *item,	\
1240 		const char *page, size_t count)				\
1241 {									\
1242 	return __iscsi_disc_##name##_store(&iscsit_global->discovery_acl,	\
1243 		page, count);						\
1244 									\
1245 }									\
1246 CONFIGFS_ATTR(iscsi_disc_, name)
1247 
1248 DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
1249 DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
1250 DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1251 DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1252 
1253 #define DEF_DISC_AUTH_INT(name)						\
1254 	__DEF_NACL_AUTH_INT(disc, name)					\
1255 static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1256 {									\
1257 	return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl, \
1258 			page);						\
1259 }									\
1260 CONFIGFS_ATTR_RO(iscsi_disc_, name)
1261 
1262 DEF_DISC_AUTH_INT(authenticate_target);
1263 
1264 
1265 static ssize_t iscsi_disc_enforce_discovery_auth_show(struct config_item *item,
1266 		char *page)
1267 {
1268 	struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1269 
1270 	return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1271 }
1272 
1273 static ssize_t iscsi_disc_enforce_discovery_auth_store(struct config_item *item,
1274 		const char *page, size_t count)
1275 {
1276 	struct iscsi_param *param;
1277 	struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1278 	u32 op;
1279 	int err;
1280 
1281 	err = kstrtou32(page, 0, &op);
1282 	if (err)
1283 		return -EINVAL;
1284 	if ((op != 1) && (op != 0)) {
1285 		pr_err("Illegal value for enforce_discovery_auth:"
1286 				" %u\n", op);
1287 		return -EINVAL;
1288 	}
1289 
1290 	if (!discovery_tpg) {
1291 		pr_err("iscsit_global->discovery_tpg is NULL\n");
1292 		return -EINVAL;
1293 	}
1294 
1295 	param = iscsi_find_param_from_key(AUTHMETHOD,
1296 				discovery_tpg->param_list);
1297 	if (!param)
1298 		return -EINVAL;
1299 
1300 	if (op) {
1301 		/*
1302 		 * Reset the AuthMethod key to CHAP.
1303 		 */
1304 		if (iscsi_update_param_value(param, CHAP) < 0)
1305 			return -EINVAL;
1306 
1307 		discovery_tpg->tpg_attrib.authentication = 1;
1308 		iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1309 		pr_debug("LIO-CORE[0] Successfully enabled"
1310 			" authentication enforcement for iSCSI"
1311 			" Discovery TPG\n");
1312 	} else {
1313 		/*
1314 		 * Reset the AuthMethod key to CHAP,None
1315 		 */
1316 		if (iscsi_update_param_value(param, "CHAP,None") < 0)
1317 			return -EINVAL;
1318 
1319 		discovery_tpg->tpg_attrib.authentication = 0;
1320 		iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1321 		pr_debug("LIO-CORE[0] Successfully disabled"
1322 			" authentication enforcement for iSCSI"
1323 			" Discovery TPG\n");
1324 	}
1325 
1326 	return count;
1327 }
1328 
1329 CONFIGFS_ATTR(iscsi_disc_, enforce_discovery_auth);
1330 
1331 static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
1332 	&iscsi_disc_attr_userid,
1333 	&iscsi_disc_attr_password,
1334 	&iscsi_disc_attr_authenticate_target,
1335 	&iscsi_disc_attr_userid_mutual,
1336 	&iscsi_disc_attr_password_mutual,
1337 	&iscsi_disc_attr_enforce_discovery_auth,
1338 	NULL,
1339 };
1340 
1341 /* End lio_target_discovery_auth_cit */
1342 
1343 /* Start functions for target_core_fabric_ops */
1344 
1345 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1346 {
1347 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1348 
1349 	return cmd->i_state;
1350 }
1351 
1352 static u32 lio_sess_get_index(struct se_session *se_sess)
1353 {
1354 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1355 
1356 	return sess->session_index;
1357 }
1358 
1359 static u32 lio_sess_get_initiator_sid(
1360 	struct se_session *se_sess,
1361 	unsigned char *buf,
1362 	u32 size)
1363 {
1364 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1365 	/*
1366 	 * iSCSI Initiator Session Identifier from RFC-3720.
1367 	 */
1368 	return snprintf(buf, size, "%6phN", sess->isid);
1369 }
1370 
1371 static int lio_queue_data_in(struct se_cmd *se_cmd)
1372 {
1373 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1374 	struct iscsi_conn *conn = cmd->conn;
1375 
1376 	cmd->i_state = ISTATE_SEND_DATAIN;
1377 	return conn->conn_transport->iscsit_queue_data_in(conn, cmd);
1378 }
1379 
1380 static int lio_write_pending(struct se_cmd *se_cmd)
1381 {
1382 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1383 	struct iscsi_conn *conn = cmd->conn;
1384 
1385 	if (!cmd->immediate_data && !cmd->unsolicited_data)
1386 		return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1387 
1388 	return 0;
1389 }
1390 
1391 static int lio_queue_status(struct se_cmd *se_cmd)
1392 {
1393 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1394 	struct iscsi_conn *conn = cmd->conn;
1395 
1396 	cmd->i_state = ISTATE_SEND_STATUS;
1397 
1398 	if (cmd->se_cmd.scsi_status || cmd->sense_reason) {
1399 		return iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1400 	}
1401 	return conn->conn_transport->iscsit_queue_status(conn, cmd);
1402 }
1403 
1404 static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
1405 {
1406 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1407 
1408 	cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1409 	iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1410 }
1411 
1412 static void lio_aborted_task(struct se_cmd *se_cmd)
1413 {
1414 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1415 
1416 	cmd->conn->conn_transport->iscsit_aborted_task(cmd->conn, cmd);
1417 }
1418 
1419 static inline struct iscsi_portal_group *iscsi_tpg(struct se_portal_group *se_tpg)
1420 {
1421 	return container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1422 }
1423 
1424 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1425 {
1426 	return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn;
1427 }
1428 
1429 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1430 {
1431 	return iscsi_tpg(se_tpg)->tpgt;
1432 }
1433 
1434 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1435 {
1436 	return iscsi_tpg(se_tpg)->tpg_attrib.default_cmdsn_depth;
1437 }
1438 
1439 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1440 {
1441 	return iscsi_tpg(se_tpg)->tpg_attrib.generate_node_acls;
1442 }
1443 
1444 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1445 {
1446 	return iscsi_tpg(se_tpg)->tpg_attrib.cache_dynamic_acls;
1447 }
1448 
1449 static int lio_tpg_check_demo_mode_write_protect(
1450 	struct se_portal_group *se_tpg)
1451 {
1452 	return iscsi_tpg(se_tpg)->tpg_attrib.demo_mode_write_protect;
1453 }
1454 
1455 static int lio_tpg_check_prod_mode_write_protect(
1456 	struct se_portal_group *se_tpg)
1457 {
1458 	return iscsi_tpg(se_tpg)->tpg_attrib.prod_mode_write_protect;
1459 }
1460 
1461 static int lio_tpg_check_prot_fabric_only(
1462 	struct se_portal_group *se_tpg)
1463 {
1464 	/*
1465 	 * Only report fabric_prot_type if t10_pi has also been enabled
1466 	 * for incoming ib_isert sessions.
1467 	 */
1468 	if (!iscsi_tpg(se_tpg)->tpg_attrib.t10_pi)
1469 		return 0;
1470 	return iscsi_tpg(se_tpg)->tpg_attrib.fabric_prot_type;
1471 }
1472 
1473 /*
1474  * This function calls iscsit_inc_session_usage_count() on the
1475  * struct iscsi_session in question.
1476  */
1477 static void lio_tpg_close_session(struct se_session *se_sess)
1478 {
1479 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1480 	struct se_portal_group *se_tpg = &sess->tpg->tpg_se_tpg;
1481 
1482 	spin_lock_bh(&se_tpg->session_lock);
1483 	spin_lock(&sess->conn_lock);
1484 	if (atomic_read(&sess->session_fall_back_to_erl0) ||
1485 	    atomic_read(&sess->session_logout) ||
1486 	    atomic_read(&sess->session_close) ||
1487 	    (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1488 		spin_unlock(&sess->conn_lock);
1489 		spin_unlock_bh(&se_tpg->session_lock);
1490 		return;
1491 	}
1492 	iscsit_inc_session_usage_count(sess);
1493 	atomic_set(&sess->session_reinstatement, 1);
1494 	atomic_set(&sess->session_fall_back_to_erl0, 1);
1495 	atomic_set(&sess->session_close, 1);
1496 	spin_unlock(&sess->conn_lock);
1497 
1498 	iscsit_stop_time2retain_timer(sess);
1499 	spin_unlock_bh(&se_tpg->session_lock);
1500 
1501 	iscsit_stop_session(sess, 1, 1);
1502 	iscsit_dec_session_usage_count(sess);
1503 }
1504 
1505 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1506 {
1507 	return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn_index;
1508 }
1509 
1510 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1511 {
1512 	struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1513 				se_node_acl);
1514 	struct se_portal_group *se_tpg = se_acl->se_tpg;
1515 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1516 				struct iscsi_portal_group, tpg_se_tpg);
1517 
1518 	acl->node_attrib.nacl = acl;
1519 	iscsit_set_default_node_attribues(acl, tpg);
1520 }
1521 
1522 static int lio_check_stop_free(struct se_cmd *se_cmd)
1523 {
1524 	return target_put_sess_cmd(se_cmd);
1525 }
1526 
1527 static void lio_release_cmd(struct se_cmd *se_cmd)
1528 {
1529 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1530 
1531 	pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
1532 	iscsit_release_cmd(cmd);
1533 }
1534 
1535 const struct target_core_fabric_ops iscsi_ops = {
1536 	.module				= THIS_MODULE,
1537 	.fabric_alias			= "iscsi",
1538 	.fabric_name			= "iSCSI",
1539 	.node_acl_size			= sizeof(struct iscsi_node_acl),
1540 	.tpg_get_wwn			= lio_tpg_get_endpoint_wwn,
1541 	.tpg_get_tag			= lio_tpg_get_tag,
1542 	.tpg_get_default_depth		= lio_tpg_get_default_depth,
1543 	.tpg_check_demo_mode		= lio_tpg_check_demo_mode,
1544 	.tpg_check_demo_mode_cache	= lio_tpg_check_demo_mode_cache,
1545 	.tpg_check_demo_mode_write_protect =
1546 			lio_tpg_check_demo_mode_write_protect,
1547 	.tpg_check_prod_mode_write_protect =
1548 			lio_tpg_check_prod_mode_write_protect,
1549 	.tpg_check_prot_fabric_only	= &lio_tpg_check_prot_fabric_only,
1550 	.tpg_get_inst_index		= lio_tpg_get_inst_index,
1551 	.check_stop_free		= lio_check_stop_free,
1552 	.release_cmd			= lio_release_cmd,
1553 	.close_session			= lio_tpg_close_session,
1554 	.sess_get_index			= lio_sess_get_index,
1555 	.sess_get_initiator_sid		= lio_sess_get_initiator_sid,
1556 	.write_pending			= lio_write_pending,
1557 	.set_default_node_attributes	= lio_set_default_node_attributes,
1558 	.get_cmd_state			= iscsi_get_cmd_state,
1559 	.queue_data_in			= lio_queue_data_in,
1560 	.queue_status			= lio_queue_status,
1561 	.queue_tm_rsp			= lio_queue_tm_rsp,
1562 	.aborted_task			= lio_aborted_task,
1563 	.fabric_make_wwn		= lio_target_call_coreaddtiqn,
1564 	.fabric_drop_wwn		= lio_target_call_coredeltiqn,
1565 	.add_wwn_groups			= lio_target_add_wwn_groups,
1566 	.fabric_make_tpg		= lio_target_tiqn_addtpg,
1567 	.fabric_enable_tpg		= lio_target_tiqn_enabletpg,
1568 	.fabric_drop_tpg		= lio_target_tiqn_deltpg,
1569 	.fabric_make_np			= lio_target_call_addnptotpg,
1570 	.fabric_drop_np			= lio_target_call_delnpfromtpg,
1571 	.fabric_init_nodeacl		= lio_target_init_nodeacl,
1572 
1573 	.tfc_discovery_attrs		= lio_target_discovery_auth_attrs,
1574 	.tfc_wwn_attrs			= lio_target_wwn_attrs,
1575 	.tfc_tpg_base_attrs		= lio_target_tpg_attrs,
1576 	.tfc_tpg_attrib_attrs		= lio_target_tpg_attrib_attrs,
1577 	.tfc_tpg_auth_attrs		= lio_target_tpg_auth_attrs,
1578 	.tfc_tpg_param_attrs		= lio_target_tpg_param_attrs,
1579 	.tfc_tpg_np_base_attrs		= lio_target_portal_attrs,
1580 	.tfc_tpg_nacl_base_attrs	= lio_target_initiator_attrs,
1581 	.tfc_tpg_nacl_attrib_attrs	= lio_target_nacl_attrib_attrs,
1582 	.tfc_tpg_nacl_auth_attrs	= lio_target_nacl_auth_attrs,
1583 	.tfc_tpg_nacl_param_attrs	= lio_target_nacl_param_attrs,
1584 
1585 	.write_pending_must_be_called	= true,
1586 };
1587