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