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