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