GNU Linux-libre 4.9.337-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 <target/target_core_base.h>
20 #include <target/target_core_fabric.h>
21
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 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         return mutex_lock_interruptible(&tpg->tpg_access_lock);
167 }
168
169 void iscsit_put_tpg(struct iscsi_portal_group *tpg)
170 {
171         mutex_unlock(&tpg->tpg_access_lock);
172 }
173
174 static void iscsit_clear_tpg_np_login_thread(
175         struct iscsi_tpg_np *tpg_np,
176         struct iscsi_portal_group *tpg,
177         bool shutdown)
178 {
179         if (!tpg_np->tpg_np) {
180                 pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
181                 return;
182         }
183
184         if (shutdown)
185                 tpg_np->tpg_np->enabled = false;
186         iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg, shutdown);
187 }
188
189 static void iscsit_clear_tpg_np_login_threads(
190         struct iscsi_portal_group *tpg,
191         bool shutdown)
192 {
193         struct iscsi_tpg_np *tpg_np;
194
195         spin_lock(&tpg->tpg_np_lock);
196         list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
197                 if (!tpg_np->tpg_np) {
198                         pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
199                         continue;
200                 }
201                 spin_unlock(&tpg->tpg_np_lock);
202                 iscsit_clear_tpg_np_login_thread(tpg_np, tpg, shutdown);
203                 spin_lock(&tpg->tpg_np_lock);
204         }
205         spin_unlock(&tpg->tpg_np_lock);
206 }
207
208 void iscsit_tpg_dump_params(struct iscsi_portal_group *tpg)
209 {
210         iscsi_print_params(tpg->param_list);
211 }
212
213 static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *tpg)
214 {
215         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
216
217         a->authentication = TA_AUTHENTICATION;
218         a->login_timeout = TA_LOGIN_TIMEOUT;
219         a->netif_timeout = TA_NETIF_TIMEOUT;
220         a->default_cmdsn_depth = TA_DEFAULT_CMDSN_DEPTH;
221         a->generate_node_acls = TA_GENERATE_NODE_ACLS;
222         a->cache_dynamic_acls = TA_CACHE_DYNAMIC_ACLS;
223         a->demo_mode_write_protect = TA_DEMO_MODE_WRITE_PROTECT;
224         a->prod_mode_write_protect = TA_PROD_MODE_WRITE_PROTECT;
225         a->demo_mode_discovery = TA_DEMO_MODE_DISCOVERY;
226         a->default_erl = TA_DEFAULT_ERL;
227         a->t10_pi = TA_DEFAULT_T10_PI;
228         a->fabric_prot_type = TA_DEFAULT_FABRIC_PROT_TYPE;
229         a->tpg_enabled_sendtargets = TA_DEFAULT_TPG_ENABLED_SENDTARGETS;
230         a->login_keys_workaround = TA_DEFAULT_LOGIN_KEYS_WORKAROUND;
231 }
232
233 int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
234 {
235         if (tpg->tpg_state != TPG_STATE_FREE) {
236                 pr_err("Unable to add iSCSI Target Portal Group: %d"
237                         " while not in TPG_STATE_FREE state.\n", tpg->tpgt);
238                 return -EEXIST;
239         }
240         iscsit_set_default_tpg_attribs(tpg);
241
242         if (iscsi_create_default_params(&tpg->param_list) < 0)
243                 goto err_out;
244
245         tpg->tpg_attrib.tpg = tpg;
246
247         spin_lock(&tpg->tpg_state_lock);
248         tpg->tpg_state  = TPG_STATE_INACTIVE;
249         spin_unlock(&tpg->tpg_state_lock);
250
251         spin_lock(&tiqn->tiqn_tpg_lock);
252         list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
253         tiqn->tiqn_ntpgs++;
254         pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
255                         tiqn->tiqn, tpg->tpgt);
256         spin_unlock(&tiqn->tiqn_tpg_lock);
257
258         return 0;
259 err_out:
260         if (tpg->param_list) {
261                 iscsi_release_param_list(tpg->param_list);
262                 tpg->param_list = NULL;
263         }
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         if (tpg->param_list) {
287                 iscsi_release_param_list(tpg->param_list);
288                 tpg->param_list = NULL;
289         }
290
291         core_tpg_deregister(&tpg->tpg_se_tpg);
292
293         spin_lock(&tpg->tpg_state_lock);
294         tpg->tpg_state = TPG_STATE_FREE;
295         spin_unlock(&tpg->tpg_state_lock);
296
297         spin_lock(&tiqn->tiqn_tpg_lock);
298         tiqn->tiqn_ntpgs--;
299         list_del(&tpg->tpg_list);
300         spin_unlock(&tiqn->tiqn_tpg_lock);
301
302         pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
303                         tiqn->tiqn, tpg->tpgt);
304
305         kfree(tpg);
306         return 0;
307 }
308
309 int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
310 {
311         struct iscsi_param *param;
312         struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
313         int ret;
314
315         spin_lock(&tpg->tpg_state_lock);
316         if (tpg->tpg_state == TPG_STATE_ACTIVE) {
317                 pr_err("iSCSI target portal group: %hu is already"
318                         " active, ignoring request.\n", tpg->tpgt);
319                 spin_unlock(&tpg->tpg_state_lock);
320                 return -EINVAL;
321         }
322         /*
323          * Make sure that AuthMethod does not contain None as an option
324          * unless explictly disabled.  Set the default to CHAP if authentication
325          * is enforced (as per default), and remove the NONE option.
326          */
327         param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
328         if (!param) {
329                 spin_unlock(&tpg->tpg_state_lock);
330                 return -EINVAL;
331         }
332
333         if (tpg->tpg_attrib.authentication) {
334                 if (!strcmp(param->value, NONE)) {
335                         ret = iscsi_update_param_value(param, CHAP);
336                         if (ret)
337                                 goto err;
338                 }
339
340                 ret = iscsit_ta_authentication(tpg, 1);
341                 if (ret < 0)
342                         goto err;
343         }
344
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         spin_unlock(&tpg->tpg_state_lock);
358         return ret;
359 }
360
361 int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
362 {
363         struct iscsi_tiqn *tiqn;
364         u8 old_state = tpg->tpg_state;
365
366         spin_lock(&tpg->tpg_state_lock);
367         if (tpg->tpg_state == TPG_STATE_INACTIVE) {
368                 pr_err("iSCSI Target Portal Group: %hu is already"
369                         " inactive, ignoring request.\n", tpg->tpgt);
370                 spin_unlock(&tpg->tpg_state_lock);
371                 return -EINVAL;
372         }
373         tpg->tpg_state = TPG_STATE_INACTIVE;
374         spin_unlock(&tpg->tpg_state_lock);
375
376         iscsit_clear_tpg_np_login_threads(tpg, false);
377
378         if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
379                 spin_lock(&tpg->tpg_state_lock);
380                 tpg->tpg_state = old_state;
381                 spin_unlock(&tpg->tpg_state_lock);
382                 pr_err("Unable to disable iSCSI Target Portal Group:"
383                         " %hu while active sessions exist, and force=0\n",
384                         tpg->tpgt);
385                 return -EPERM;
386         }
387
388         tiqn = tpg->tpg_tiqn;
389         if (!tiqn || (tpg == iscsit_global->discovery_tpg))
390                 return 0;
391
392         spin_lock(&tiqn->tiqn_tpg_lock);
393         tiqn->tiqn_active_tpgs--;
394         pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
395                         tpg->tpgt);
396         spin_unlock(&tiqn->tiqn_tpg_lock);
397
398         return 0;
399 }
400
401 struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
402         struct iscsi_session *sess)
403 {
404         struct se_session *se_sess = sess->se_sess;
405         struct se_node_acl *se_nacl = se_sess->se_node_acl;
406         struct iscsi_node_acl *acl = container_of(se_nacl, struct iscsi_node_acl,
407                                         se_node_acl);
408
409         return &acl->node_attrib;
410 }
411
412 struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
413         struct iscsi_tpg_np *tpg_np,
414         int network_transport)
415 {
416         struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
417
418         spin_lock(&tpg_np->tpg_np_parent_lock);
419         list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
420                         &tpg_np->tpg_np_parent_list, tpg_np_child_list) {
421                 if (tpg_np_child->tpg_np->np_network_transport ==
422                                 network_transport) {
423                         spin_unlock(&tpg_np->tpg_np_parent_lock);
424                         return tpg_np_child;
425                 }
426         }
427         spin_unlock(&tpg_np->tpg_np_parent_lock);
428
429         return NULL;
430 }
431
432 static bool iscsit_tpg_check_network_portal(
433         struct iscsi_tiqn *tiqn,
434         struct sockaddr_storage *sockaddr,
435         int network_transport)
436 {
437         struct iscsi_portal_group *tpg;
438         struct iscsi_tpg_np *tpg_np;
439         struct iscsi_np *np;
440         bool match = false;
441
442         spin_lock(&tiqn->tiqn_tpg_lock);
443         list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
444
445                 spin_lock(&tpg->tpg_np_lock);
446                 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
447                         np = tpg_np->tpg_np;
448
449                         match = iscsit_check_np_match(sockaddr, np,
450                                                 network_transport);
451                         if (match)
452                                 break;
453                 }
454                 spin_unlock(&tpg->tpg_np_lock);
455
456                 if (match)
457                         break;
458         }
459         spin_unlock(&tiqn->tiqn_tpg_lock);
460
461         return match;
462 }
463
464 struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
465         struct iscsi_portal_group *tpg,
466         struct sockaddr_storage *sockaddr,
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: %pISc already exists on a"
477                                 " different TPG on %s\n", sockaddr,
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, 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: %pISpc,%hu on %s\n",
521                 tpg->tpg_tiqn->tiqn, &np->np_sockaddr, 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: %pISpc,%hu on %s\n",
535                 tpg->tpg_tiqn->tiqn, &np->np_sockaddr, 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_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
597 {
598         unsigned char buf1[256], buf2[256], *none = NULL;
599         int len;
600         struct iscsi_param *param;
601         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
602
603         if ((authentication != 1) && (authentication != 0)) {
604                 pr_err("Illegal value for authentication parameter:"
605                         " %u, ignoring request.\n", authentication);
606                 return -EINVAL;
607         }
608
609         memset(buf1, 0, sizeof(buf1));
610         memset(buf2, 0, sizeof(buf2));
611
612         param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
613         if (!param)
614                 return -EINVAL;
615
616         if (authentication) {
617                 snprintf(buf1, sizeof(buf1), "%s", param->value);
618                 none = strstr(buf1, NONE);
619                 if (!none)
620                         goto out;
621                 if (!strncmp(none + 4, ",", 1)) {
622                         if (!strcmp(buf1, none))
623                                 sprintf(buf2, "%s", none+5);
624                         else {
625                                 none--;
626                                 *none = '\0';
627                                 len = sprintf(buf2, "%s", buf1);
628                                 none += 5;
629                                 sprintf(buf2 + len, "%s", none);
630                         }
631                 } else {
632                         none--;
633                         *none = '\0';
634                         sprintf(buf2, "%s", buf1);
635                 }
636                 if (iscsi_update_param_value(param, buf2) < 0)
637                         return -EINVAL;
638         } else {
639                 snprintf(buf1, sizeof(buf1), "%s", param->value);
640                 none = strstr(buf1, NONE);
641                 if (none)
642                         goto out;
643                 strlcat(buf1, "," NONE, sizeof(buf1));
644                 if (iscsi_update_param_value(param, buf1) < 0)
645                         return -EINVAL;
646         }
647
648 out:
649         a->authentication = authentication;
650         pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
651                 a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
652
653         return 0;
654 }
655
656 int iscsit_ta_login_timeout(
657         struct iscsi_portal_group *tpg,
658         u32 login_timeout)
659 {
660         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
661
662         if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
663                 pr_err("Requested Login Timeout %u larger than maximum"
664                         " %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
665                 return -EINVAL;
666         } else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
667                 pr_err("Requested Logout Timeout %u smaller than"
668                         " minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
669                 return -EINVAL;
670         }
671
672         a->login_timeout = login_timeout;
673         pr_debug("Set Logout Timeout to %u for Target Portal Group"
674                 " %hu\n", a->login_timeout, tpg->tpgt);
675
676         return 0;
677 }
678
679 int iscsit_ta_netif_timeout(
680         struct iscsi_portal_group *tpg,
681         u32 netif_timeout)
682 {
683         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
684
685         if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
686                 pr_err("Requested Network Interface Timeout %u larger"
687                         " than maximum %u\n", netif_timeout,
688                                 TA_NETIF_TIMEOUT_MAX);
689                 return -EINVAL;
690         } else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
691                 pr_err("Requested Network Interface Timeout %u smaller"
692                         " than minimum %u\n", netif_timeout,
693                                 TA_NETIF_TIMEOUT_MIN);
694                 return -EINVAL;
695         }
696
697         a->netif_timeout = netif_timeout;
698         pr_debug("Set Network Interface Timeout to %u for"
699                 " Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
700
701         return 0;
702 }
703
704 int iscsit_ta_generate_node_acls(
705         struct iscsi_portal_group *tpg,
706         u32 flag)
707 {
708         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
709
710         if ((flag != 0) && (flag != 1)) {
711                 pr_err("Illegal value %d\n", flag);
712                 return -EINVAL;
713         }
714
715         a->generate_node_acls = flag;
716         pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
717                 tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
718
719         if (flag == 1 && a->cache_dynamic_acls == 0) {
720                 pr_debug("Explicitly setting cache_dynamic_acls=1 when "
721                         "generate_node_acls=1\n");
722                 a->cache_dynamic_acls = 1;
723         }
724
725         return 0;
726 }
727
728 int iscsit_ta_default_cmdsn_depth(
729         struct iscsi_portal_group *tpg,
730         u32 tcq_depth)
731 {
732         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
733
734         if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
735                 pr_err("Requested Default Queue Depth: %u larger"
736                         " than maximum %u\n", tcq_depth,
737                                 TA_DEFAULT_CMDSN_DEPTH_MAX);
738                 return -EINVAL;
739         } else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
740                 pr_err("Requested Default Queue Depth: %u smaller"
741                         " than minimum %u\n", tcq_depth,
742                                 TA_DEFAULT_CMDSN_DEPTH_MIN);
743                 return -EINVAL;
744         }
745
746         a->default_cmdsn_depth = tcq_depth;
747         pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
748                 tpg->tpgt, a->default_cmdsn_depth);
749
750         return 0;
751 }
752
753 int iscsit_ta_cache_dynamic_acls(
754         struct iscsi_portal_group *tpg,
755         u32 flag)
756 {
757         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
758
759         if ((flag != 0) && (flag != 1)) {
760                 pr_err("Illegal value %d\n", flag);
761                 return -EINVAL;
762         }
763
764         if (a->generate_node_acls == 1 && flag == 0) {
765                 pr_debug("Skipping cache_dynamic_acls=0 when"
766                         " generate_node_acls=1\n");
767                 return 0;
768         }
769
770         a->cache_dynamic_acls = flag;
771         pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
772                 " ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
773                 "Enabled" : "Disabled");
774
775         return 0;
776 }
777
778 int iscsit_ta_demo_mode_write_protect(
779         struct iscsi_portal_group *tpg,
780         u32 flag)
781 {
782         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
783
784         if ((flag != 0) && (flag != 1)) {
785                 pr_err("Illegal value %d\n", flag);
786                 return -EINVAL;
787         }
788
789         a->demo_mode_write_protect = flag;
790         pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
791                 tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
792
793         return 0;
794 }
795
796 int iscsit_ta_prod_mode_write_protect(
797         struct iscsi_portal_group *tpg,
798         u32 flag)
799 {
800         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
801
802         if ((flag != 0) && (flag != 1)) {
803                 pr_err("Illegal value %d\n", flag);
804                 return -EINVAL;
805         }
806
807         a->prod_mode_write_protect = flag;
808         pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
809                 " %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
810                 "ON" : "OFF");
811
812         return 0;
813 }
814
815 int iscsit_ta_demo_mode_discovery(
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->demo_mode_discovery = flag;
827         pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
828                 " %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
829                 "ON" : "OFF");
830
831         return 0;
832 }
833
834 int iscsit_ta_default_erl(
835         struct iscsi_portal_group *tpg,
836         u32 default_erl)
837 {
838         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
839
840         if ((default_erl != 0) && (default_erl != 1) && (default_erl != 2)) {
841                 pr_err("Illegal value for default_erl: %u\n", default_erl);
842                 return -EINVAL;
843         }
844
845         a->default_erl = default_erl;
846         pr_debug("iSCSI_TPG[%hu] - DefaultERL: %u\n", tpg->tpgt, a->default_erl);
847
848         return 0;
849 }
850
851 int iscsit_ta_t10_pi(
852         struct iscsi_portal_group *tpg,
853         u32 flag)
854 {
855         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
856
857         if ((flag != 0) && (flag != 1)) {
858                 pr_err("Illegal value %d\n", flag);
859                 return -EINVAL;
860         }
861
862         a->t10_pi = flag;
863         pr_debug("iSCSI_TPG[%hu] - T10 Protection information bit:"
864                 " %s\n", tpg->tpgt, (a->t10_pi) ?
865                 "ON" : "OFF");
866
867         return 0;
868 }
869
870 int iscsit_ta_fabric_prot_type(
871         struct iscsi_portal_group *tpg,
872         u32 prot_type)
873 {
874         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
875
876         if ((prot_type != 0) && (prot_type != 1) && (prot_type != 3)) {
877                 pr_err("Illegal value for fabric_prot_type: %u\n", prot_type);
878                 return -EINVAL;
879         }
880
881         a->fabric_prot_type = prot_type;
882         pr_debug("iSCSI_TPG[%hu] - T10 Fabric Protection Type: %u\n",
883                  tpg->tpgt, prot_type);
884
885         return 0;
886 }
887
888 int iscsit_ta_tpg_enabled_sendtargets(
889         struct iscsi_portal_group *tpg,
890         u32 flag)
891 {
892         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
893
894         if ((flag != 0) && (flag != 1)) {
895                 pr_err("Illegal value %d\n", flag);
896                 return -EINVAL;
897         }
898
899         a->tpg_enabled_sendtargets = flag;
900         pr_debug("iSCSI_TPG[%hu] - TPG enabled bit required for SendTargets:"
901                 " %s\n", tpg->tpgt, (a->tpg_enabled_sendtargets) ? "ON" : "OFF");
902
903         return 0;
904 }
905
906 int iscsit_ta_login_keys_workaround(
907         struct iscsi_portal_group *tpg,
908         u32 flag)
909 {
910         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
911
912         if ((flag != 0) && (flag != 1)) {
913                 pr_err("Illegal value %d\n", flag);
914                 return -EINVAL;
915         }
916
917         a->login_keys_workaround = flag;
918         pr_debug("iSCSI_TPG[%hu] - TPG enabled bit for login keys workaround: %s ",
919                 tpg->tpgt, (a->login_keys_workaround) ? "ON" : "OFF");
920
921         return 0;
922 }