1/*******************************************************************************
2 * This file contains iSCSI Target Portal Group related functions.
3 *
4 * (c) Copyright 2007-2013 Datera, Inc.
5 *
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
18
19#include <target/target_core_base.h>
20#include <target/target_core_fabric.h>
21#include <target/target_core_configfs.h>
22
23#include <target/iscsi/iscsi_target_core.h>
24#include "iscsi_target_erl0.h"
25#include "iscsi_target_login.h"
26#include "iscsi_target_nodeattrib.h"
27#include "iscsi_target_tpg.h"
28#include "iscsi_target_util.h"
29#include "iscsi_target.h"
30#include "iscsi_target_parameters.h"
31
32#include <target/iscsi/iscsi_transport.h>
33
34struct iscsi_portal_group *iscsit_alloc_portal_group(struct iscsi_tiqn *tiqn, u16 tpgt)
35{
36	struct iscsi_portal_group *tpg;
37
38	tpg = kzalloc(sizeof(struct iscsi_portal_group), GFP_KERNEL);
39	if (!tpg) {
40		pr_err("Unable to allocate struct iscsi_portal_group\n");
41		return NULL;
42	}
43
44	tpg->tpgt = tpgt;
45	tpg->tpg_state = TPG_STATE_FREE;
46	tpg->tpg_tiqn = tiqn;
47	INIT_LIST_HEAD(&tpg->tpg_gnp_list);
48	INIT_LIST_HEAD(&tpg->tpg_list);
49	mutex_init(&tpg->tpg_access_lock);
50	sema_init(&tpg->np_login_sem, 1);
51	spin_lock_init(&tpg->tpg_state_lock);
52	spin_lock_init(&tpg->tpg_np_lock);
53
54	return tpg;
55}
56
57static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);
58
59int iscsit_load_discovery_tpg(void)
60{
61	struct iscsi_param *param;
62	struct iscsi_portal_group *tpg;
63	int ret;
64
65	tpg = iscsit_alloc_portal_group(NULL, 1);
66	if (!tpg) {
67		pr_err("Unable to allocate struct iscsi_portal_group\n");
68		return -1;
69	}
70
71	ret = core_tpg_register(&iscsi_ops, NULL, &tpg->tpg_se_tpg,
72				tpg, TRANSPORT_TPG_TYPE_DISCOVERY);
73	if (ret < 0) {
74		kfree(tpg);
75		return -1;
76	}
77
78	tpg->sid = 1; /* First Assigned LIO Session ID */
79	iscsit_set_default_tpg_attribs(tpg);
80
81	if (iscsi_create_default_params(&tpg->param_list) < 0)
82		goto out;
83	/*
84	 * By default we disable authentication for discovery sessions,
85	 * this can be changed with:
86	 *
87	 * /sys/kernel/config/target/iscsi/discovery_auth/enforce_discovery_auth
88	 */
89	param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
90	if (!param)
91		goto out;
92
93	if (iscsi_update_param_value(param, "CHAP,None") < 0)
94		goto out;
95
96	tpg->tpg_attrib.authentication = 0;
97
98	spin_lock(&tpg->tpg_state_lock);
99	tpg->tpg_state  = TPG_STATE_ACTIVE;
100	spin_unlock(&tpg->tpg_state_lock);
101
102	iscsit_global->discovery_tpg = tpg;
103	pr_debug("CORE[0] - Allocated Discovery TPG\n");
104
105	return 0;
106out:
107	if (tpg->sid == 1)
108		core_tpg_deregister(&tpg->tpg_se_tpg);
109	kfree(tpg);
110	return -1;
111}
112
113void iscsit_release_discovery_tpg(void)
114{
115	struct iscsi_portal_group *tpg = iscsit_global->discovery_tpg;
116
117	if (!tpg)
118		return;
119
120	core_tpg_deregister(&tpg->tpg_se_tpg);
121
122	kfree(tpg);
123	iscsit_global->discovery_tpg = NULL;
124}
125
126struct iscsi_portal_group *iscsit_get_tpg_from_np(
127	struct iscsi_tiqn *tiqn,
128	struct iscsi_np *np,
129	struct iscsi_tpg_np **tpg_np_out)
130{
131	struct iscsi_portal_group *tpg = NULL;
132	struct iscsi_tpg_np *tpg_np;
133
134	spin_lock(&tiqn->tiqn_tpg_lock);
135	list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
136
137		spin_lock(&tpg->tpg_state_lock);
138		if (tpg->tpg_state != TPG_STATE_ACTIVE) {
139			spin_unlock(&tpg->tpg_state_lock);
140			continue;
141		}
142		spin_unlock(&tpg->tpg_state_lock);
143
144		spin_lock(&tpg->tpg_np_lock);
145		list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
146			if (tpg_np->tpg_np == np) {
147				*tpg_np_out = tpg_np;
148				kref_get(&tpg_np->tpg_np_kref);
149				spin_unlock(&tpg->tpg_np_lock);
150				spin_unlock(&tiqn->tiqn_tpg_lock);
151				return tpg;
152			}
153		}
154		spin_unlock(&tpg->tpg_np_lock);
155	}
156	spin_unlock(&tiqn->tiqn_tpg_lock);
157
158	return NULL;
159}
160
161int iscsit_get_tpg(
162	struct iscsi_portal_group *tpg)
163{
164	return mutex_lock_interruptible(&tpg->tpg_access_lock);
165}
166
167void iscsit_put_tpg(struct iscsi_portal_group *tpg)
168{
169	mutex_unlock(&tpg->tpg_access_lock);
170}
171
172static void iscsit_clear_tpg_np_login_thread(
173	struct iscsi_tpg_np *tpg_np,
174	struct iscsi_portal_group *tpg,
175	bool shutdown)
176{
177	if (!tpg_np->tpg_np) {
178		pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
179		return;
180	}
181
182	if (shutdown)
183		tpg_np->tpg_np->enabled = false;
184	iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg, shutdown);
185}
186
187static void iscsit_clear_tpg_np_login_threads(
188	struct iscsi_portal_group *tpg,
189	bool shutdown)
190{
191	struct iscsi_tpg_np *tpg_np;
192
193	spin_lock(&tpg->tpg_np_lock);
194	list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
195		if (!tpg_np->tpg_np) {
196			pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
197			continue;
198		}
199		spin_unlock(&tpg->tpg_np_lock);
200		iscsit_clear_tpg_np_login_thread(tpg_np, tpg, shutdown);
201		spin_lock(&tpg->tpg_np_lock);
202	}
203	spin_unlock(&tpg->tpg_np_lock);
204}
205
206void iscsit_tpg_dump_params(struct iscsi_portal_group *tpg)
207{
208	iscsi_print_params(tpg->param_list);
209}
210
211static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *tpg)
212{
213	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
214
215	a->authentication = TA_AUTHENTICATION;
216	a->login_timeout = TA_LOGIN_TIMEOUT;
217	a->netif_timeout = TA_NETIF_TIMEOUT;
218	a->default_cmdsn_depth = TA_DEFAULT_CMDSN_DEPTH;
219	a->generate_node_acls = TA_GENERATE_NODE_ACLS;
220	a->cache_dynamic_acls = TA_CACHE_DYNAMIC_ACLS;
221	a->demo_mode_write_protect = TA_DEMO_MODE_WRITE_PROTECT;
222	a->prod_mode_write_protect = TA_PROD_MODE_WRITE_PROTECT;
223	a->demo_mode_discovery = TA_DEMO_MODE_DISCOVERY;
224	a->default_erl = TA_DEFAULT_ERL;
225	a->t10_pi = TA_DEFAULT_T10_PI;
226	a->fabric_prot_type = TA_DEFAULT_FABRIC_PROT_TYPE;
227}
228
229int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
230{
231	if (tpg->tpg_state != TPG_STATE_FREE) {
232		pr_err("Unable to add iSCSI Target Portal Group: %d"
233			" while not in TPG_STATE_FREE state.\n", tpg->tpgt);
234		return -EEXIST;
235	}
236	iscsit_set_default_tpg_attribs(tpg);
237
238	if (iscsi_create_default_params(&tpg->param_list) < 0)
239		goto err_out;
240
241	tpg->tpg_attrib.tpg = tpg;
242
243	spin_lock(&tpg->tpg_state_lock);
244	tpg->tpg_state	= TPG_STATE_INACTIVE;
245	spin_unlock(&tpg->tpg_state_lock);
246
247	spin_lock(&tiqn->tiqn_tpg_lock);
248	list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
249	tiqn->tiqn_ntpgs++;
250	pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
251			tiqn->tiqn, tpg->tpgt);
252	spin_unlock(&tiqn->tiqn_tpg_lock);
253
254	return 0;
255err_out:
256	if (tpg->param_list) {
257		iscsi_release_param_list(tpg->param_list);
258		tpg->param_list = NULL;
259	}
260	kfree(tpg);
261	return -ENOMEM;
262}
263
264int iscsit_tpg_del_portal_group(
265	struct iscsi_tiqn *tiqn,
266	struct iscsi_portal_group *tpg,
267	int force)
268{
269	u8 old_state = tpg->tpg_state;
270
271	spin_lock(&tpg->tpg_state_lock);
272	tpg->tpg_state = TPG_STATE_INACTIVE;
273	spin_unlock(&tpg->tpg_state_lock);
274
275	if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
276		pr_err("Unable to delete iSCSI Target Portal Group:"
277			" %hu while active sessions exist, and force=0\n",
278			tpg->tpgt);
279		tpg->tpg_state = old_state;
280		return -EPERM;
281	}
282
283	core_tpg_clear_object_luns(&tpg->tpg_se_tpg);
284
285	if (tpg->param_list) {
286		iscsi_release_param_list(tpg->param_list);
287		tpg->param_list = NULL;
288	}
289
290	core_tpg_deregister(&tpg->tpg_se_tpg);
291
292	spin_lock(&tpg->tpg_state_lock);
293	tpg->tpg_state = TPG_STATE_FREE;
294	spin_unlock(&tpg->tpg_state_lock);
295
296	spin_lock(&tiqn->tiqn_tpg_lock);
297	tiqn->tiqn_ntpgs--;
298	list_del(&tpg->tpg_list);
299	spin_unlock(&tiqn->tiqn_tpg_lock);
300
301	pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
302			tiqn->tiqn, tpg->tpgt);
303
304	kfree(tpg);
305	return 0;
306}
307
308int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
309{
310	struct iscsi_param *param;
311	struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
312	int ret;
313
314	spin_lock(&tpg->tpg_state_lock);
315	if (tpg->tpg_state == TPG_STATE_ACTIVE) {
316		pr_err("iSCSI target portal group: %hu is already"
317			" active, ignoring request.\n", tpg->tpgt);
318		spin_unlock(&tpg->tpg_state_lock);
319		return -EINVAL;
320	}
321	/*
322	 * Make sure that AuthMethod does not contain None as an option
323	 * unless explictly disabled.  Set the default to CHAP if authentication
324	 * is enforced (as per default), and remove the NONE option.
325	 */
326	param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
327	if (!param) {
328		spin_unlock(&tpg->tpg_state_lock);
329		return -EINVAL;
330	}
331
332	if (tpg->tpg_attrib.authentication) {
333		if (!strcmp(param->value, NONE)) {
334			ret = iscsi_update_param_value(param, CHAP);
335			if (ret)
336				goto err;
337		}
338
339		ret = iscsit_ta_authentication(tpg, 1);
340		if (ret < 0)
341			goto err;
342	}
343
344	tpg->tpg_state = TPG_STATE_ACTIVE;
345	spin_unlock(&tpg->tpg_state_lock);
346
347	spin_lock(&tiqn->tiqn_tpg_lock);
348	tiqn->tiqn_active_tpgs++;
349	pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
350			tpg->tpgt);
351	spin_unlock(&tiqn->tiqn_tpg_lock);
352
353	return 0;
354
355err:
356	spin_unlock(&tpg->tpg_state_lock);
357	return ret;
358}
359
360int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
361{
362	struct iscsi_tiqn *tiqn;
363	u8 old_state = tpg->tpg_state;
364
365	spin_lock(&tpg->tpg_state_lock);
366	if (tpg->tpg_state == TPG_STATE_INACTIVE) {
367		pr_err("iSCSI Target Portal Group: %hu is already"
368			" inactive, ignoring request.\n", tpg->tpgt);
369		spin_unlock(&tpg->tpg_state_lock);
370		return -EINVAL;
371	}
372	tpg->tpg_state = TPG_STATE_INACTIVE;
373	spin_unlock(&tpg->tpg_state_lock);
374
375	iscsit_clear_tpg_np_login_threads(tpg, false);
376
377	if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
378		spin_lock(&tpg->tpg_state_lock);
379		tpg->tpg_state = old_state;
380		spin_unlock(&tpg->tpg_state_lock);
381		pr_err("Unable to disable iSCSI Target Portal Group:"
382			" %hu while active sessions exist, and force=0\n",
383			tpg->tpgt);
384		return -EPERM;
385	}
386
387	tiqn = tpg->tpg_tiqn;
388	if (!tiqn || (tpg == iscsit_global->discovery_tpg))
389		return 0;
390
391	spin_lock(&tiqn->tiqn_tpg_lock);
392	tiqn->tiqn_active_tpgs--;
393	pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
394			tpg->tpgt);
395	spin_unlock(&tiqn->tiqn_tpg_lock);
396
397	return 0;
398}
399
400struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
401	struct iscsi_session *sess)
402{
403	struct se_session *se_sess = sess->se_sess;
404	struct se_node_acl *se_nacl = se_sess->se_node_acl;
405	struct iscsi_node_acl *acl = container_of(se_nacl, struct iscsi_node_acl,
406					se_node_acl);
407
408	return &acl->node_attrib;
409}
410
411struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
412	struct iscsi_tpg_np *tpg_np,
413	int network_transport)
414{
415	struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
416
417	spin_lock(&tpg_np->tpg_np_parent_lock);
418	list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
419			&tpg_np->tpg_np_parent_list, tpg_np_child_list) {
420		if (tpg_np_child->tpg_np->np_network_transport ==
421				network_transport) {
422			spin_unlock(&tpg_np->tpg_np_parent_lock);
423			return tpg_np_child;
424		}
425	}
426	spin_unlock(&tpg_np->tpg_np_parent_lock);
427
428	return NULL;
429}
430
431static bool iscsit_tpg_check_network_portal(
432	struct iscsi_tiqn *tiqn,
433	struct __kernel_sockaddr_storage *sockaddr,
434	int network_transport)
435{
436	struct iscsi_portal_group *tpg;
437	struct iscsi_tpg_np *tpg_np;
438	struct iscsi_np *np;
439	bool match = false;
440
441	spin_lock(&tiqn->tiqn_tpg_lock);
442	list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
443
444		spin_lock(&tpg->tpg_np_lock);
445		list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
446			np = tpg_np->tpg_np;
447
448			match = iscsit_check_np_match(sockaddr, np,
449						network_transport);
450			if (match)
451				break;
452		}
453		spin_unlock(&tpg->tpg_np_lock);
454	}
455	spin_unlock(&tiqn->tiqn_tpg_lock);
456
457	return match;
458}
459
460struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
461	struct iscsi_portal_group *tpg,
462	struct __kernel_sockaddr_storage *sockaddr,
463	struct iscsi_tpg_np *tpg_np_parent,
464	int network_transport)
465{
466	struct iscsi_np *np;
467	struct iscsi_tpg_np *tpg_np;
468
469	if (!tpg_np_parent) {
470		if (iscsit_tpg_check_network_portal(tpg->tpg_tiqn, sockaddr,
471				network_transport)) {
472			pr_err("Network Portal: %pISc already exists on a"
473				" different TPG on %s\n", sockaddr,
474				tpg->tpg_tiqn->tiqn);
475			return ERR_PTR(-EEXIST);
476		}
477	}
478
479	tpg_np = kzalloc(sizeof(struct iscsi_tpg_np), GFP_KERNEL);
480	if (!tpg_np) {
481		pr_err("Unable to allocate memory for"
482				" struct iscsi_tpg_np.\n");
483		return ERR_PTR(-ENOMEM);
484	}
485
486	np = iscsit_add_np(sockaddr, network_transport);
487	if (IS_ERR(np)) {
488		kfree(tpg_np);
489		return ERR_CAST(np);
490	}
491
492	INIT_LIST_HEAD(&tpg_np->tpg_np_list);
493	INIT_LIST_HEAD(&tpg_np->tpg_np_child_list);
494	INIT_LIST_HEAD(&tpg_np->tpg_np_parent_list);
495	spin_lock_init(&tpg_np->tpg_np_parent_lock);
496	init_completion(&tpg_np->tpg_np_comp);
497	kref_init(&tpg_np->tpg_np_kref);
498	tpg_np->tpg_np		= np;
499	tpg_np->tpg		= tpg;
500
501	spin_lock(&tpg->tpg_np_lock);
502	list_add_tail(&tpg_np->tpg_np_list, &tpg->tpg_gnp_list);
503	tpg->num_tpg_nps++;
504	if (tpg->tpg_tiqn)
505		tpg->tpg_tiqn->tiqn_num_tpg_nps++;
506	spin_unlock(&tpg->tpg_np_lock);
507
508	if (tpg_np_parent) {
509		tpg_np->tpg_np_parent = tpg_np_parent;
510		spin_lock(&tpg_np_parent->tpg_np_parent_lock);
511		list_add_tail(&tpg_np->tpg_np_child_list,
512			&tpg_np_parent->tpg_np_parent_list);
513		spin_unlock(&tpg_np_parent->tpg_np_parent_lock);
514	}
515
516	pr_debug("CORE[%s] - Added Network Portal: %pISc:%hu,%hu on %s\n",
517		tpg->tpg_tiqn->tiqn, &np->np_sockaddr, np->np_port, tpg->tpgt,
518		np->np_transport->name);
519
520	return tpg_np;
521}
522
523static int iscsit_tpg_release_np(
524	struct iscsi_tpg_np *tpg_np,
525	struct iscsi_portal_group *tpg,
526	struct iscsi_np *np)
527{
528	iscsit_clear_tpg_np_login_thread(tpg_np, tpg, true);
529
530	pr_debug("CORE[%s] - Removed Network Portal: %pISc:%hu,%hu on %s\n",
531		tpg->tpg_tiqn->tiqn, &np->np_sockaddr, np->np_port, tpg->tpgt,
532		np->np_transport->name);
533
534	tpg_np->tpg_np = NULL;
535	tpg_np->tpg = NULL;
536	kfree(tpg_np);
537	/*
538	 * iscsit_del_np() will shutdown struct iscsi_np when last TPG reference is released.
539	 */
540	return iscsit_del_np(np);
541}
542
543int iscsit_tpg_del_network_portal(
544	struct iscsi_portal_group *tpg,
545	struct iscsi_tpg_np *tpg_np)
546{
547	struct iscsi_np *np;
548	struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
549	int ret = 0;
550
551	np = tpg_np->tpg_np;
552	if (!np) {
553		pr_err("Unable to locate struct iscsi_np from"
554				" struct iscsi_tpg_np\n");
555		return -EINVAL;
556	}
557
558	if (!tpg_np->tpg_np_parent) {
559		/*
560		 * We are the parent tpg network portal.  Release all of the
561		 * child tpg_np's (eg: the non ISCSI_TCP ones) on our parent
562		 * list first.
563		 */
564		list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
565				&tpg_np->tpg_np_parent_list,
566				tpg_np_child_list) {
567			ret = iscsit_tpg_del_network_portal(tpg, tpg_np_child);
568			if (ret < 0)
569				pr_err("iscsit_tpg_del_network_portal()"
570					" failed: %d\n", ret);
571		}
572	} else {
573		/*
574		 * We are not the parent ISCSI_TCP tpg network portal.  Release
575		 * our own network portals from the child list.
576		 */
577		spin_lock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
578		list_del(&tpg_np->tpg_np_child_list);
579		spin_unlock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
580	}
581
582	spin_lock(&tpg->tpg_np_lock);
583	list_del(&tpg_np->tpg_np_list);
584	tpg->num_tpg_nps--;
585	if (tpg->tpg_tiqn)
586		tpg->tpg_tiqn->tiqn_num_tpg_nps--;
587	spin_unlock(&tpg->tpg_np_lock);
588
589	return iscsit_tpg_release_np(tpg_np, tpg, np);
590}
591
592int iscsit_tpg_set_initiator_node_queue_depth(
593	struct iscsi_portal_group *tpg,
594	unsigned char *initiatorname,
595	u32 queue_depth,
596	int force)
597{
598	return core_tpg_set_initiator_node_queue_depth(&tpg->tpg_se_tpg,
599		initiatorname, queue_depth, force);
600}
601
602int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
603{
604	unsigned char buf1[256], buf2[256], *none = NULL;
605	int len;
606	struct iscsi_param *param;
607	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
608
609	if ((authentication != 1) && (authentication != 0)) {
610		pr_err("Illegal value for authentication parameter:"
611			" %u, ignoring request.\n", authentication);
612		return -EINVAL;
613	}
614
615	memset(buf1, 0, sizeof(buf1));
616	memset(buf2, 0, sizeof(buf2));
617
618	param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
619	if (!param)
620		return -EINVAL;
621
622	if (authentication) {
623		snprintf(buf1, sizeof(buf1), "%s", param->value);
624		none = strstr(buf1, NONE);
625		if (!none)
626			goto out;
627		if (!strncmp(none + 4, ",", 1)) {
628			if (!strcmp(buf1, none))
629				sprintf(buf2, "%s", none+5);
630			else {
631				none--;
632				*none = '\0';
633				len = sprintf(buf2, "%s", buf1);
634				none += 5;
635				sprintf(buf2 + len, "%s", none);
636			}
637		} else {
638			none--;
639			*none = '\0';
640			sprintf(buf2, "%s", buf1);
641		}
642		if (iscsi_update_param_value(param, buf2) < 0)
643			return -EINVAL;
644	} else {
645		snprintf(buf1, sizeof(buf1), "%s", param->value);
646		none = strstr(buf1, NONE);
647		if (none)
648			goto out;
649		strncat(buf1, ",", strlen(","));
650		strncat(buf1, NONE, strlen(NONE));
651		if (iscsi_update_param_value(param, buf1) < 0)
652			return -EINVAL;
653	}
654
655out:
656	a->authentication = authentication;
657	pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
658		a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
659
660	return 0;
661}
662
663int iscsit_ta_login_timeout(
664	struct iscsi_portal_group *tpg,
665	u32 login_timeout)
666{
667	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
668
669	if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
670		pr_err("Requested Login Timeout %u larger than maximum"
671			" %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
672		return -EINVAL;
673	} else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
674		pr_err("Requested Logout Timeout %u smaller than"
675			" minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
676		return -EINVAL;
677	}
678
679	a->login_timeout = login_timeout;
680	pr_debug("Set Logout Timeout to %u for Target Portal Group"
681		" %hu\n", a->login_timeout, tpg->tpgt);
682
683	return 0;
684}
685
686int iscsit_ta_netif_timeout(
687	struct iscsi_portal_group *tpg,
688	u32 netif_timeout)
689{
690	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
691
692	if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
693		pr_err("Requested Network Interface Timeout %u larger"
694			" than maximum %u\n", netif_timeout,
695				TA_NETIF_TIMEOUT_MAX);
696		return -EINVAL;
697	} else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
698		pr_err("Requested Network Interface Timeout %u smaller"
699			" than minimum %u\n", netif_timeout,
700				TA_NETIF_TIMEOUT_MIN);
701		return -EINVAL;
702	}
703
704	a->netif_timeout = netif_timeout;
705	pr_debug("Set Network Interface Timeout to %u for"
706		" Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
707
708	return 0;
709}
710
711int iscsit_ta_generate_node_acls(
712	struct iscsi_portal_group *tpg,
713	u32 flag)
714{
715	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
716
717	if ((flag != 0) && (flag != 1)) {
718		pr_err("Illegal value %d\n", flag);
719		return -EINVAL;
720	}
721
722	a->generate_node_acls = flag;
723	pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
724		tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
725
726	if (flag == 1 && a->cache_dynamic_acls == 0) {
727		pr_debug("Explicitly setting cache_dynamic_acls=1 when "
728			"generate_node_acls=1\n");
729		a->cache_dynamic_acls = 1;
730	}
731
732	return 0;
733}
734
735int iscsit_ta_default_cmdsn_depth(
736	struct iscsi_portal_group *tpg,
737	u32 tcq_depth)
738{
739	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
740
741	if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
742		pr_err("Requested Default Queue Depth: %u larger"
743			" than maximum %u\n", tcq_depth,
744				TA_DEFAULT_CMDSN_DEPTH_MAX);
745		return -EINVAL;
746	} else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
747		pr_err("Requested Default Queue Depth: %u smaller"
748			" than minimum %u\n", tcq_depth,
749				TA_DEFAULT_CMDSN_DEPTH_MIN);
750		return -EINVAL;
751	}
752
753	a->default_cmdsn_depth = tcq_depth;
754	pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
755		tpg->tpgt, a->default_cmdsn_depth);
756
757	return 0;
758}
759
760int iscsit_ta_cache_dynamic_acls(
761	struct iscsi_portal_group *tpg,
762	u32 flag)
763{
764	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
765
766	if ((flag != 0) && (flag != 1)) {
767		pr_err("Illegal value %d\n", flag);
768		return -EINVAL;
769	}
770
771	if (a->generate_node_acls == 1 && flag == 0) {
772		pr_debug("Skipping cache_dynamic_acls=0 when"
773			" generate_node_acls=1\n");
774		return 0;
775	}
776
777	a->cache_dynamic_acls = flag;
778	pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
779		" ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
780		"Enabled" : "Disabled");
781
782	return 0;
783}
784
785int iscsit_ta_demo_mode_write_protect(
786	struct iscsi_portal_group *tpg,
787	u32 flag)
788{
789	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
790
791	if ((flag != 0) && (flag != 1)) {
792		pr_err("Illegal value %d\n", flag);
793		return -EINVAL;
794	}
795
796	a->demo_mode_write_protect = flag;
797	pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
798		tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
799
800	return 0;
801}
802
803int iscsit_ta_prod_mode_write_protect(
804	struct iscsi_portal_group *tpg,
805	u32 flag)
806{
807	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
808
809	if ((flag != 0) && (flag != 1)) {
810		pr_err("Illegal value %d\n", flag);
811		return -EINVAL;
812	}
813
814	a->prod_mode_write_protect = flag;
815	pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
816		" %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
817		"ON" : "OFF");
818
819	return 0;
820}
821
822int iscsit_ta_demo_mode_discovery(
823	struct iscsi_portal_group *tpg,
824	u32 flag)
825{
826	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
827
828	if ((flag != 0) && (flag != 1)) {
829		pr_err("Illegal value %d\n", flag);
830		return -EINVAL;
831	}
832
833	a->demo_mode_discovery = flag;
834	pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
835		" %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
836		"ON" : "OFF");
837
838	return 0;
839}
840
841int iscsit_ta_default_erl(
842	struct iscsi_portal_group *tpg,
843	u32 default_erl)
844{
845	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
846
847	if ((default_erl != 0) && (default_erl != 1) && (default_erl != 2)) {
848		pr_err("Illegal value for default_erl: %u\n", default_erl);
849		return -EINVAL;
850	}
851
852	a->default_erl = default_erl;
853	pr_debug("iSCSI_TPG[%hu] - DefaultERL: %u\n", tpg->tpgt, a->default_erl);
854
855	return 0;
856}
857
858int iscsit_ta_t10_pi(
859	struct iscsi_portal_group *tpg,
860	u32 flag)
861{
862	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
863
864	if ((flag != 0) && (flag != 1)) {
865		pr_err("Illegal value %d\n", flag);
866		return -EINVAL;
867	}
868
869	a->t10_pi = flag;
870	pr_debug("iSCSI_TPG[%hu] - T10 Protection information bit:"
871		" %s\n", tpg->tpgt, (a->t10_pi) ?
872		"ON" : "OFF");
873
874	return 0;
875}
876
877int iscsit_ta_fabric_prot_type(
878	struct iscsi_portal_group *tpg,
879	u32 prot_type)
880{
881	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
882
883	if ((prot_type != 0) && (prot_type != 1) && (prot_type != 3)) {
884		pr_err("Illegal value for fabric_prot_type: %u\n", prot_type);
885		return -EINVAL;
886	}
887
888	a->fabric_prot_type = prot_type;
889	pr_debug("iSCSI_TPG[%hu] - T10 Fabric Protection Type: %u\n",
890		 tpg->tpgt, prot_type);
891
892	return 0;
893}
894