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