xref: /linux/drivers/target/iscsi/iscsi_target_tpg.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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->default_cmdsn_depth = TA_DEFAULT_CMDSN_DEPTH;
215 	a->generate_node_acls = TA_GENERATE_NODE_ACLS;
216 	a->cache_dynamic_acls = TA_CACHE_DYNAMIC_ACLS;
217 	a->demo_mode_write_protect = TA_DEMO_MODE_WRITE_PROTECT;
218 	a->prod_mode_write_protect = TA_PROD_MODE_WRITE_PROTECT;
219 	a->demo_mode_discovery = TA_DEMO_MODE_DISCOVERY;
220 	a->default_erl = TA_DEFAULT_ERL;
221 	a->t10_pi = TA_DEFAULT_T10_PI;
222 	a->fabric_prot_type = TA_DEFAULT_FABRIC_PROT_TYPE;
223 	a->tpg_enabled_sendtargets = TA_DEFAULT_TPG_ENABLED_SENDTARGETS;
224 	a->login_keys_workaround = TA_DEFAULT_LOGIN_KEYS_WORKAROUND;
225 }
226 
227 int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
228 {
229 	if (tpg->tpg_state != TPG_STATE_FREE) {
230 		pr_err("Unable to add iSCSI Target Portal Group: %d"
231 			" while not in TPG_STATE_FREE state.\n", tpg->tpgt);
232 		return -EEXIST;
233 	}
234 	iscsit_set_default_tpg_attribs(tpg);
235 
236 	if (iscsi_create_default_params(&tpg->param_list) < 0)
237 		goto err_out;
238 
239 	tpg->tpg_attrib.tpg = tpg;
240 
241 	spin_lock(&tpg->tpg_state_lock);
242 	tpg->tpg_state	= TPG_STATE_INACTIVE;
243 	spin_unlock(&tpg->tpg_state_lock);
244 
245 	spin_lock(&tiqn->tiqn_tpg_lock);
246 	list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
247 	tiqn->tiqn_ntpgs++;
248 	pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
249 			tiqn->tiqn, tpg->tpgt);
250 	spin_unlock(&tiqn->tiqn_tpg_lock);
251 
252 	return 0;
253 err_out:
254 	if (tpg->param_list) {
255 		iscsi_release_param_list(tpg->param_list);
256 		tpg->param_list = NULL;
257 	}
258 	return -ENOMEM;
259 }
260 
261 int iscsit_tpg_del_portal_group(
262 	struct iscsi_tiqn *tiqn,
263 	struct iscsi_portal_group *tpg,
264 	int force)
265 {
266 	u8 old_state = tpg->tpg_state;
267 
268 	spin_lock(&tpg->tpg_state_lock);
269 	tpg->tpg_state = TPG_STATE_INACTIVE;
270 	spin_unlock(&tpg->tpg_state_lock);
271 
272 	if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
273 		pr_err("Unable to delete iSCSI Target Portal Group:"
274 			" %hu while active sessions exist, and force=0\n",
275 			tpg->tpgt);
276 		tpg->tpg_state = old_state;
277 		return -EPERM;
278 	}
279 
280 	if (tpg->param_list) {
281 		iscsi_release_param_list(tpg->param_list);
282 		tpg->param_list = NULL;
283 	}
284 
285 	core_tpg_deregister(&tpg->tpg_se_tpg);
286 
287 	spin_lock(&tpg->tpg_state_lock);
288 	tpg->tpg_state = TPG_STATE_FREE;
289 	spin_unlock(&tpg->tpg_state_lock);
290 
291 	spin_lock(&tiqn->tiqn_tpg_lock);
292 	tiqn->tiqn_ntpgs--;
293 	list_del(&tpg->tpg_list);
294 	spin_unlock(&tiqn->tiqn_tpg_lock);
295 
296 	pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
297 			tiqn->tiqn, tpg->tpgt);
298 
299 	kfree(tpg);
300 	return 0;
301 }
302 
303 int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
304 {
305 	struct iscsi_param *param;
306 	struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
307 	int ret;
308 
309 	if (tpg->tpg_state == TPG_STATE_ACTIVE) {
310 		pr_err("iSCSI target portal group: %hu is already"
311 			" active, ignoring request.\n", tpg->tpgt);
312 		return -EINVAL;
313 	}
314 	/*
315 	 * Make sure that AuthMethod does not contain None as an option
316 	 * unless explictly disabled.  Set the default to CHAP if authentication
317 	 * is enforced (as per default), and remove the NONE option.
318 	 */
319 	param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
320 	if (!param)
321 		return -EINVAL;
322 
323 	if (tpg->tpg_attrib.authentication) {
324 		if (!strcmp(param->value, NONE)) {
325 			ret = iscsi_update_param_value(param, CHAP);
326 			if (ret)
327 				goto err;
328 		}
329 
330 		ret = iscsit_ta_authentication(tpg, 1);
331 		if (ret < 0)
332 			goto err;
333 	}
334 
335 	spin_lock(&tpg->tpg_state_lock);
336 	tpg->tpg_state = TPG_STATE_ACTIVE;
337 	spin_unlock(&tpg->tpg_state_lock);
338 
339 	spin_lock(&tiqn->tiqn_tpg_lock);
340 	tiqn->tiqn_active_tpgs++;
341 	pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
342 			tpg->tpgt);
343 	spin_unlock(&tiqn->tiqn_tpg_lock);
344 
345 	return 0;
346 
347 err:
348 	return ret;
349 }
350 
351 int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
352 {
353 	struct iscsi_tiqn *tiqn;
354 	u8 old_state = tpg->tpg_state;
355 
356 	spin_lock(&tpg->tpg_state_lock);
357 	if (tpg->tpg_state == TPG_STATE_INACTIVE) {
358 		pr_err("iSCSI Target Portal Group: %hu is already"
359 			" inactive, ignoring request.\n", tpg->tpgt);
360 		spin_unlock(&tpg->tpg_state_lock);
361 		return -EINVAL;
362 	}
363 	tpg->tpg_state = TPG_STATE_INACTIVE;
364 	spin_unlock(&tpg->tpg_state_lock);
365 
366 	iscsit_clear_tpg_np_login_threads(tpg, false);
367 
368 	if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
369 		spin_lock(&tpg->tpg_state_lock);
370 		tpg->tpg_state = old_state;
371 		spin_unlock(&tpg->tpg_state_lock);
372 		pr_err("Unable to disable iSCSI Target Portal Group:"
373 			" %hu while active sessions exist, and force=0\n",
374 			tpg->tpgt);
375 		return -EPERM;
376 	}
377 
378 	tiqn = tpg->tpg_tiqn;
379 	if (!tiqn || (tpg == iscsit_global->discovery_tpg))
380 		return 0;
381 
382 	spin_lock(&tiqn->tiqn_tpg_lock);
383 	tiqn->tiqn_active_tpgs--;
384 	pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
385 			tpg->tpgt);
386 	spin_unlock(&tiqn->tiqn_tpg_lock);
387 
388 	return 0;
389 }
390 
391 struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
392 	struct iscsit_session *sess)
393 {
394 	struct se_session *se_sess = sess->se_sess;
395 	struct se_node_acl *se_nacl = se_sess->se_node_acl;
396 	struct iscsi_node_acl *acl = to_iscsi_nacl(se_nacl);
397 
398 	return &acl->node_attrib;
399 }
400 
401 struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
402 	struct iscsi_tpg_np *tpg_np,
403 	int network_transport)
404 {
405 	struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
406 
407 	spin_lock(&tpg_np->tpg_np_parent_lock);
408 	list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
409 			&tpg_np->tpg_np_parent_list, tpg_np_child_list) {
410 		if (tpg_np_child->tpg_np->np_network_transport ==
411 				network_transport) {
412 			spin_unlock(&tpg_np->tpg_np_parent_lock);
413 			return tpg_np_child;
414 		}
415 	}
416 	spin_unlock(&tpg_np->tpg_np_parent_lock);
417 
418 	return NULL;
419 }
420 
421 static bool iscsit_tpg_check_network_portal(
422 	struct iscsi_tiqn *tiqn,
423 	struct sockaddr_storage *sockaddr,
424 	int network_transport)
425 {
426 	struct iscsi_portal_group *tpg;
427 	struct iscsi_tpg_np *tpg_np;
428 	struct iscsi_np *np;
429 	bool match = false;
430 
431 	spin_lock(&tiqn->tiqn_tpg_lock);
432 	list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
433 
434 		spin_lock(&tpg->tpg_np_lock);
435 		list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
436 			np = tpg_np->tpg_np;
437 
438 			match = iscsit_check_np_match(sockaddr, np,
439 						network_transport);
440 			if (match)
441 				break;
442 		}
443 		spin_unlock(&tpg->tpg_np_lock);
444 
445 		if (match)
446 			break;
447 	}
448 	spin_unlock(&tiqn->tiqn_tpg_lock);
449 
450 	return match;
451 }
452 
453 struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
454 	struct iscsi_portal_group *tpg,
455 	struct sockaddr_storage *sockaddr,
456 	struct iscsi_tpg_np *tpg_np_parent,
457 	int network_transport)
458 {
459 	struct iscsi_np *np;
460 	struct iscsi_tpg_np *tpg_np;
461 
462 	if (!tpg_np_parent) {
463 		if (iscsit_tpg_check_network_portal(tpg->tpg_tiqn, sockaddr,
464 				network_transport)) {
465 			pr_err("Network Portal: %pISc already exists on a"
466 				" different TPG on %s\n", sockaddr,
467 				tpg->tpg_tiqn->tiqn);
468 			return ERR_PTR(-EEXIST);
469 		}
470 	}
471 
472 	tpg_np = kzalloc(sizeof(struct iscsi_tpg_np), GFP_KERNEL);
473 	if (!tpg_np) {
474 		pr_err("Unable to allocate memory for"
475 				" struct iscsi_tpg_np.\n");
476 		return ERR_PTR(-ENOMEM);
477 	}
478 
479 	np = iscsit_add_np(sockaddr, network_transport);
480 	if (IS_ERR(np)) {
481 		kfree(tpg_np);
482 		return ERR_CAST(np);
483 	}
484 
485 	INIT_LIST_HEAD(&tpg_np->tpg_np_list);
486 	INIT_LIST_HEAD(&tpg_np->tpg_np_child_list);
487 	INIT_LIST_HEAD(&tpg_np->tpg_np_parent_list);
488 	spin_lock_init(&tpg_np->tpg_np_parent_lock);
489 	init_completion(&tpg_np->tpg_np_comp);
490 	kref_init(&tpg_np->tpg_np_kref);
491 	tpg_np->tpg_np		= np;
492 	tpg_np->tpg		= tpg;
493 
494 	spin_lock(&tpg->tpg_np_lock);
495 	list_add_tail(&tpg_np->tpg_np_list, &tpg->tpg_gnp_list);
496 	tpg->num_tpg_nps++;
497 	if (tpg->tpg_tiqn)
498 		tpg->tpg_tiqn->tiqn_num_tpg_nps++;
499 	spin_unlock(&tpg->tpg_np_lock);
500 
501 	if (tpg_np_parent) {
502 		tpg_np->tpg_np_parent = tpg_np_parent;
503 		spin_lock(&tpg_np_parent->tpg_np_parent_lock);
504 		list_add_tail(&tpg_np->tpg_np_child_list,
505 			&tpg_np_parent->tpg_np_parent_list);
506 		spin_unlock(&tpg_np_parent->tpg_np_parent_lock);
507 	}
508 
509 	pr_debug("CORE[%s] - Added Network Portal: %pISpc,%hu on %s\n",
510 		tpg->tpg_tiqn->tiqn, &np->np_sockaddr, tpg->tpgt,
511 		np->np_transport->name);
512 
513 	return tpg_np;
514 }
515 
516 static int iscsit_tpg_release_np(
517 	struct iscsi_tpg_np *tpg_np,
518 	struct iscsi_portal_group *tpg,
519 	struct iscsi_np *np)
520 {
521 	iscsit_clear_tpg_np_login_thread(tpg_np, tpg, true);
522 
523 	pr_debug("CORE[%s] - Removed Network Portal: %pISpc,%hu on %s\n",
524 		tpg->tpg_tiqn->tiqn, &np->np_sockaddr, tpg->tpgt,
525 		np->np_transport->name);
526 
527 	tpg_np->tpg_np = NULL;
528 	tpg_np->tpg = NULL;
529 	kfree(tpg_np);
530 	/*
531 	 * iscsit_del_np() will shutdown struct iscsi_np when last TPG reference is released.
532 	 */
533 	return iscsit_del_np(np);
534 }
535 
536 int iscsit_tpg_del_network_portal(
537 	struct iscsi_portal_group *tpg,
538 	struct iscsi_tpg_np *tpg_np)
539 {
540 	struct iscsi_np *np;
541 	struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
542 	int ret = 0;
543 
544 	np = tpg_np->tpg_np;
545 	if (!np) {
546 		pr_err("Unable to locate struct iscsi_np from"
547 				" struct iscsi_tpg_np\n");
548 		return -EINVAL;
549 	}
550 
551 	if (!tpg_np->tpg_np_parent) {
552 		/*
553 		 * We are the parent tpg network portal.  Release all of the
554 		 * child tpg_np's (eg: the non ISCSI_TCP ones) on our parent
555 		 * list first.
556 		 */
557 		list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
558 				&tpg_np->tpg_np_parent_list,
559 				tpg_np_child_list) {
560 			ret = iscsit_tpg_del_network_portal(tpg, tpg_np_child);
561 			if (ret < 0)
562 				pr_err("iscsit_tpg_del_network_portal()"
563 					" failed: %d\n", ret);
564 		}
565 	} else {
566 		/*
567 		 * We are not the parent ISCSI_TCP tpg network portal.  Release
568 		 * our own network portals from the child list.
569 		 */
570 		spin_lock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
571 		list_del(&tpg_np->tpg_np_child_list);
572 		spin_unlock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
573 	}
574 
575 	spin_lock(&tpg->tpg_np_lock);
576 	list_del(&tpg_np->tpg_np_list);
577 	tpg->num_tpg_nps--;
578 	if (tpg->tpg_tiqn)
579 		tpg->tpg_tiqn->tiqn_num_tpg_nps--;
580 	spin_unlock(&tpg->tpg_np_lock);
581 
582 	return iscsit_tpg_release_np(tpg_np, tpg, np);
583 }
584 
585 int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
586 {
587 	unsigned char buf1[256], buf2[256], *none = NULL;
588 	int len;
589 	struct iscsi_param *param;
590 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
591 
592 	if ((authentication != 1) && (authentication != 0)) {
593 		pr_err("Illegal value for authentication parameter:"
594 			" %u, ignoring request.\n", authentication);
595 		return -EINVAL;
596 	}
597 
598 	memset(buf1, 0, sizeof(buf1));
599 	memset(buf2, 0, sizeof(buf2));
600 
601 	param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
602 	if (!param)
603 		return -EINVAL;
604 
605 	if (authentication) {
606 		snprintf(buf1, sizeof(buf1), "%s", param->value);
607 		none = strstr(buf1, NONE);
608 		if (!none)
609 			goto out;
610 		if (!strncmp(none + 4, ",", 1)) {
611 			if (!strcmp(buf1, none))
612 				sprintf(buf2, "%s", none+5);
613 			else {
614 				none--;
615 				*none = '\0';
616 				len = sprintf(buf2, "%s", buf1);
617 				none += 5;
618 				sprintf(buf2 + len, "%s", none);
619 			}
620 		} else {
621 			none--;
622 			*none = '\0';
623 			sprintf(buf2, "%s", buf1);
624 		}
625 		if (iscsi_update_param_value(param, buf2) < 0)
626 			return -EINVAL;
627 	} else {
628 		snprintf(buf1, sizeof(buf1), "%s", param->value);
629 		none = strstr(buf1, NONE);
630 		if (none)
631 			goto out;
632 		strlcat(buf1, "," NONE, sizeof(buf1));
633 		if (iscsi_update_param_value(param, buf1) < 0)
634 			return -EINVAL;
635 	}
636 
637 out:
638 	a->authentication = authentication;
639 	pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
640 		a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
641 
642 	return 0;
643 }
644 
645 int iscsit_ta_login_timeout(
646 	struct iscsi_portal_group *tpg,
647 	u32 login_timeout)
648 {
649 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
650 
651 	if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
652 		pr_err("Requested Login Timeout %u larger than maximum"
653 			" %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
654 		return -EINVAL;
655 	} else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
656 		pr_err("Requested Logout Timeout %u smaller than"
657 			" minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
658 		return -EINVAL;
659 	}
660 
661 	a->login_timeout = login_timeout;
662 	pr_debug("Set Logout Timeout to %u for Target Portal Group"
663 		" %hu\n", a->login_timeout, tpg->tpgt);
664 
665 	return 0;
666 }
667 
668 int iscsit_ta_generate_node_acls(
669 	struct iscsi_portal_group *tpg,
670 	u32 flag)
671 {
672 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
673 
674 	if ((flag != 0) && (flag != 1)) {
675 		pr_err("Illegal value %d\n", flag);
676 		return -EINVAL;
677 	}
678 
679 	a->generate_node_acls = flag;
680 	pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
681 		tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
682 
683 	if (flag == 1 && a->cache_dynamic_acls == 0) {
684 		pr_debug("Explicitly setting cache_dynamic_acls=1 when "
685 			"generate_node_acls=1\n");
686 		a->cache_dynamic_acls = 1;
687 	}
688 
689 	return 0;
690 }
691 
692 int iscsit_ta_default_cmdsn_depth(
693 	struct iscsi_portal_group *tpg,
694 	u32 tcq_depth)
695 {
696 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
697 
698 	if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
699 		pr_err("Requested Default Queue Depth: %u larger"
700 			" than maximum %u\n", tcq_depth,
701 				TA_DEFAULT_CMDSN_DEPTH_MAX);
702 		return -EINVAL;
703 	} else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
704 		pr_err("Requested Default Queue Depth: %u smaller"
705 			" than minimum %u\n", tcq_depth,
706 				TA_DEFAULT_CMDSN_DEPTH_MIN);
707 		return -EINVAL;
708 	}
709 
710 	a->default_cmdsn_depth = tcq_depth;
711 	pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
712 		tpg->tpgt, a->default_cmdsn_depth);
713 
714 	return 0;
715 }
716 
717 int iscsit_ta_cache_dynamic_acls(
718 	struct iscsi_portal_group *tpg,
719 	u32 flag)
720 {
721 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
722 
723 	if ((flag != 0) && (flag != 1)) {
724 		pr_err("Illegal value %d\n", flag);
725 		return -EINVAL;
726 	}
727 
728 	if (a->generate_node_acls == 1 && flag == 0) {
729 		pr_debug("Skipping cache_dynamic_acls=0 when"
730 			" generate_node_acls=1\n");
731 		return 0;
732 	}
733 
734 	a->cache_dynamic_acls = flag;
735 	pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
736 		" ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
737 		"Enabled" : "Disabled");
738 
739 	return 0;
740 }
741 
742 int iscsit_ta_demo_mode_write_protect(
743 	struct iscsi_portal_group *tpg,
744 	u32 flag)
745 {
746 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
747 
748 	if ((flag != 0) && (flag != 1)) {
749 		pr_err("Illegal value %d\n", flag);
750 		return -EINVAL;
751 	}
752 
753 	a->demo_mode_write_protect = flag;
754 	pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
755 		tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
756 
757 	return 0;
758 }
759 
760 int iscsit_ta_prod_mode_write_protect(
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 	a->prod_mode_write_protect = flag;
772 	pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
773 		" %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
774 		"ON" : "OFF");
775 
776 	return 0;
777 }
778 
779 int iscsit_ta_demo_mode_discovery(
780 	struct iscsi_portal_group *tpg,
781 	u32 flag)
782 {
783 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
784 
785 	if ((flag != 0) && (flag != 1)) {
786 		pr_err("Illegal value %d\n", flag);
787 		return -EINVAL;
788 	}
789 
790 	a->demo_mode_discovery = flag;
791 	pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
792 		" %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
793 		"ON" : "OFF");
794 
795 	return 0;
796 }
797 
798 int iscsit_ta_default_erl(
799 	struct iscsi_portal_group *tpg,
800 	u32 default_erl)
801 {
802 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
803 
804 	if ((default_erl != 0) && (default_erl != 1) && (default_erl != 2)) {
805 		pr_err("Illegal value for default_erl: %u\n", default_erl);
806 		return -EINVAL;
807 	}
808 
809 	a->default_erl = default_erl;
810 	pr_debug("iSCSI_TPG[%hu] - DefaultERL: %u\n", tpg->tpgt, a->default_erl);
811 
812 	return 0;
813 }
814 
815 int iscsit_ta_t10_pi(
816 	struct iscsi_portal_group *tpg,
817 	u32 flag)
818 {
819 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
820 
821 	if ((flag != 0) && (flag != 1)) {
822 		pr_err("Illegal value %d\n", flag);
823 		return -EINVAL;
824 	}
825 
826 	a->t10_pi = flag;
827 	pr_debug("iSCSI_TPG[%hu] - T10 Protection information bit:"
828 		" %s\n", tpg->tpgt, (a->t10_pi) ?
829 		"ON" : "OFF");
830 
831 	return 0;
832 }
833 
834 int iscsit_ta_fabric_prot_type(
835 	struct iscsi_portal_group *tpg,
836 	u32 prot_type)
837 {
838 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
839 
840 	if ((prot_type != 0) && (prot_type != 1) && (prot_type != 3)) {
841 		pr_err("Illegal value for fabric_prot_type: %u\n", prot_type);
842 		return -EINVAL;
843 	}
844 
845 	a->fabric_prot_type = prot_type;
846 	pr_debug("iSCSI_TPG[%hu] - T10 Fabric Protection Type: %u\n",
847 		 tpg->tpgt, prot_type);
848 
849 	return 0;
850 }
851 
852 int iscsit_ta_tpg_enabled_sendtargets(
853 	struct iscsi_portal_group *tpg,
854 	u32 flag)
855 {
856 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
857 
858 	if ((flag != 0) && (flag != 1)) {
859 		pr_err("Illegal value %d\n", flag);
860 		return -EINVAL;
861 	}
862 
863 	a->tpg_enabled_sendtargets = flag;
864 	pr_debug("iSCSI_TPG[%hu] - TPG enabled bit required for SendTargets:"
865 		" %s\n", tpg->tpgt, (a->tpg_enabled_sendtargets) ? "ON" : "OFF");
866 
867 	return 0;
868 }
869 
870 int iscsit_ta_login_keys_workaround(
871 	struct iscsi_portal_group *tpg,
872 	u32 flag)
873 {
874 	struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
875 
876 	if ((flag != 0) && (flag != 1)) {
877 		pr_err("Illegal value %d\n", flag);
878 		return -EINVAL;
879 	}
880 
881 	a->login_keys_workaround = flag;
882 	pr_debug("iSCSI_TPG[%hu] - TPG enabled bit for login keys workaround: %s ",
883 		tpg->tpgt, (a->login_keys_workaround) ? "ON" : "OFF");
884 
885 	return 0;
886 }
887