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