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