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