]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/target/iscsi/iscsi_target_tpg.c
arm: imx6: defconfig: update tx6 defconfigs
[karo-tx-linux.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 #include <target/target_core_configfs.h>
22
23 #include "iscsi_target_core.h"
24 #include "iscsi_target_erl0.h"
25 #include "iscsi_target_login.h"
26 #include "iscsi_target_nodeattrib.h"
27 #include "iscsi_target_tpg.h"
28 #include "iscsi_target_util.h"
29 #include "iscsi_target.h"
30 #include "iscsi_target_parameters.h"
31
32 #include <target/iscsi/iscsi_transport.h>
33
34 struct iscsi_portal_group *iscsit_alloc_portal_group(struct iscsi_tiqn *tiqn, u16 tpgt)
35 {
36         struct iscsi_portal_group *tpg;
37
38         tpg = kzalloc(sizeof(struct iscsi_portal_group), GFP_KERNEL);
39         if (!tpg) {
40                 pr_err("Unable to allocate struct iscsi_portal_group\n");
41                 return NULL;
42         }
43
44         tpg->tpgt = tpgt;
45         tpg->tpg_state = TPG_STATE_FREE;
46         tpg->tpg_tiqn = tiqn;
47         INIT_LIST_HEAD(&tpg->tpg_gnp_list);
48         INIT_LIST_HEAD(&tpg->tpg_list);
49         mutex_init(&tpg->tpg_access_lock);
50         sema_init(&tpg->np_login_sem, 1);
51         spin_lock_init(&tpg->tpg_state_lock);
52         spin_lock_init(&tpg->tpg_np_lock);
53
54         return tpg;
55 }
56
57 static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);
58
59 int iscsit_load_discovery_tpg(void)
60 {
61         struct iscsi_param *param;
62         struct iscsi_portal_group *tpg;
63         int ret;
64
65         tpg = iscsit_alloc_portal_group(NULL, 1);
66         if (!tpg) {
67                 pr_err("Unable to allocate struct iscsi_portal_group\n");
68                 return -1;
69         }
70
71         ret = core_tpg_register(
72                         &lio_target_fabric_configfs->tf_ops,
73                         NULL, &tpg->tpg_se_tpg, tpg,
74                         TRANSPORT_TPG_TYPE_DISCOVERY);
75         if (ret < 0) {
76                 kfree(tpg);
77                 return -1;
78         }
79
80         tpg->sid = 1; /* First Assigned LIO Session ID */
81         iscsit_set_default_tpg_attribs(tpg);
82
83         if (iscsi_create_default_params(&tpg->param_list) < 0)
84                 goto out;
85         /*
86          * By default we disable authentication for discovery sessions,
87          * this can be changed with:
88          *
89          * /sys/kernel/config/target/iscsi/discovery_auth/enforce_discovery_auth
90          */
91         param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
92         if (!param)
93                 goto out;
94
95         if (iscsi_update_param_value(param, "CHAP,None") < 0)
96                 goto out;
97
98         tpg->tpg_attrib.authentication = 0;
99
100         spin_lock(&tpg->tpg_state_lock);
101         tpg->tpg_state  = TPG_STATE_ACTIVE;
102         spin_unlock(&tpg->tpg_state_lock);
103
104         iscsit_global->discovery_tpg = tpg;
105         pr_debug("CORE[0] - Allocated Discovery TPG\n");
106
107         return 0;
108 out:
109         if (tpg->sid == 1)
110                 core_tpg_deregister(&tpg->tpg_se_tpg);
111         kfree(tpg);
112         return -1;
113 }
114
115 void iscsit_release_discovery_tpg(void)
116 {
117         struct iscsi_portal_group *tpg = iscsit_global->discovery_tpg;
118
119         if (!tpg)
120                 return;
121
122         core_tpg_deregister(&tpg->tpg_se_tpg);
123
124         kfree(tpg);
125         iscsit_global->discovery_tpg = NULL;
126 }
127
128 struct iscsi_portal_group *iscsit_get_tpg_from_np(
129         struct iscsi_tiqn *tiqn,
130         struct iscsi_np *np,
131         struct iscsi_tpg_np **tpg_np_out)
132 {
133         struct iscsi_portal_group *tpg = NULL;
134         struct iscsi_tpg_np *tpg_np;
135
136         spin_lock(&tiqn->tiqn_tpg_lock);
137         list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
138
139                 spin_lock(&tpg->tpg_state_lock);
140                 if (tpg->tpg_state == TPG_STATE_FREE) {
141                         spin_unlock(&tpg->tpg_state_lock);
142                         continue;
143                 }
144                 spin_unlock(&tpg->tpg_state_lock);
145
146                 spin_lock(&tpg->tpg_np_lock);
147                 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
148                         if (tpg_np->tpg_np == np) {
149                                 *tpg_np_out = tpg_np;
150                                 kref_get(&tpg_np->tpg_np_kref);
151                                 spin_unlock(&tpg->tpg_np_lock);
152                                 spin_unlock(&tiqn->tiqn_tpg_lock);
153                                 return tpg;
154                         }
155                 }
156                 spin_unlock(&tpg->tpg_np_lock);
157         }
158         spin_unlock(&tiqn->tiqn_tpg_lock);
159
160         return NULL;
161 }
162
163 int iscsit_get_tpg(
164         struct iscsi_portal_group *tpg)
165 {
166         int ret;
167
168         ret = mutex_lock_interruptible(&tpg->tpg_access_lock);
169         return ((ret != 0) || signal_pending(current)) ? -1 : 0;
170 }
171
172 void iscsit_put_tpg(struct iscsi_portal_group *tpg)
173 {
174         mutex_unlock(&tpg->tpg_access_lock);
175 }
176
177 static void iscsit_clear_tpg_np_login_thread(
178         struct iscsi_tpg_np *tpg_np,
179         struct iscsi_portal_group *tpg,
180         bool shutdown)
181 {
182         if (!tpg_np->tpg_np) {
183                 pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
184                 return;
185         }
186
187         iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg, shutdown);
188 }
189
190 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 }
228
229 int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
230 {
231         if (tpg->tpg_state != TPG_STATE_FREE) {
232                 pr_err("Unable to add iSCSI Target Portal Group: %d"
233                         " while not in TPG_STATE_FREE state.\n", tpg->tpgt);
234                 return -EEXIST;
235         }
236         iscsit_set_default_tpg_attribs(tpg);
237
238         if (iscsi_create_default_params(&tpg->param_list) < 0)
239                 goto err_out;
240
241         tpg->tpg_attrib.tpg = tpg;
242
243         spin_lock(&tpg->tpg_state_lock);
244         tpg->tpg_state  = TPG_STATE_INACTIVE;
245         spin_unlock(&tpg->tpg_state_lock);
246
247         spin_lock(&tiqn->tiqn_tpg_lock);
248         list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
249         tiqn->tiqn_ntpgs++;
250         pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
251                         tiqn->tiqn, tpg->tpgt);
252         spin_unlock(&tiqn->tiqn_tpg_lock);
253
254         return 0;
255 err_out:
256         if (tpg->param_list) {
257                 iscsi_release_param_list(tpg->param_list);
258                 tpg->param_list = NULL;
259         }
260         kfree(tpg);
261         return -ENOMEM;
262 }
263
264 int iscsit_tpg_del_portal_group(
265         struct iscsi_tiqn *tiqn,
266         struct iscsi_portal_group *tpg,
267         int force)
268 {
269         u8 old_state = tpg->tpg_state;
270
271         spin_lock(&tpg->tpg_state_lock);
272         tpg->tpg_state = TPG_STATE_INACTIVE;
273         spin_unlock(&tpg->tpg_state_lock);
274
275         iscsit_clear_tpg_np_login_threads(tpg, true);
276
277         if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
278                 pr_err("Unable to delete iSCSI Target Portal Group:"
279                         " %hu while active sessions exist, and force=0\n",
280                         tpg->tpgt);
281                 tpg->tpg_state = old_state;
282                 return -EPERM;
283         }
284
285         core_tpg_clear_object_luns(&tpg->tpg_se_tpg);
286
287         if (tpg->param_list) {
288                 iscsi_release_param_list(tpg->param_list);
289                 tpg->param_list = NULL;
290         }
291
292         core_tpg_deregister(&tpg->tpg_se_tpg);
293
294         spin_lock(&tpg->tpg_state_lock);
295         tpg->tpg_state = TPG_STATE_FREE;
296         spin_unlock(&tpg->tpg_state_lock);
297
298         spin_lock(&tiqn->tiqn_tpg_lock);
299         tiqn->tiqn_ntpgs--;
300         list_del(&tpg->tpg_list);
301         spin_unlock(&tiqn->tiqn_tpg_lock);
302
303         pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
304                         tiqn->tiqn, tpg->tpgt);
305
306         kfree(tpg);
307         return 0;
308 }
309
310 int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
311 {
312         struct iscsi_param *param;
313         struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
314         int ret;
315
316         spin_lock(&tpg->tpg_state_lock);
317         if (tpg->tpg_state == TPG_STATE_ACTIVE) {
318                 pr_err("iSCSI target portal group: %hu is already"
319                         " active, ignoring request.\n", tpg->tpgt);
320                 spin_unlock(&tpg->tpg_state_lock);
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                 spin_unlock(&tpg->tpg_state_lock);
331                 return -EINVAL;
332         }
333
334         if (tpg->tpg_attrib.authentication) {
335                 if (!strcmp(param->value, NONE)) {
336                         ret = iscsi_update_param_value(param, CHAP);
337                         if (ret)
338                                 goto err;
339                 }
340
341                 ret = iscsit_ta_authentication(tpg, 1);
342                 if (ret < 0)
343                         goto err;
344         }
345
346         tpg->tpg_state = TPG_STATE_ACTIVE;
347         spin_unlock(&tpg->tpg_state_lock);
348
349         spin_lock(&tiqn->tiqn_tpg_lock);
350         tiqn->tiqn_active_tpgs++;
351         pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
352                         tpg->tpgt);
353         spin_unlock(&tiqn->tiqn_tpg_lock);
354
355         return 0;
356
357 err:
358         spin_unlock(&tpg->tpg_state_lock);
359         return ret;
360 }
361
362 int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
363 {
364         struct iscsi_tiqn *tiqn;
365         u8 old_state = tpg->tpg_state;
366
367         spin_lock(&tpg->tpg_state_lock);
368         if (tpg->tpg_state == TPG_STATE_INACTIVE) {
369                 pr_err("iSCSI Target Portal Group: %hu is already"
370                         " inactive, ignoring request.\n", tpg->tpgt);
371                 spin_unlock(&tpg->tpg_state_lock);
372                 return -EINVAL;
373         }
374         tpg->tpg_state = TPG_STATE_INACTIVE;
375         spin_unlock(&tpg->tpg_state_lock);
376
377         iscsit_clear_tpg_np_login_threads(tpg, false);
378
379         if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
380                 spin_lock(&tpg->tpg_state_lock);
381                 tpg->tpg_state = old_state;
382                 spin_unlock(&tpg->tpg_state_lock);
383                 pr_err("Unable to disable iSCSI Target Portal Group:"
384                         " %hu while active sessions exist, and force=0\n",
385                         tpg->tpgt);
386                 return -EPERM;
387         }
388
389         tiqn = tpg->tpg_tiqn;
390         if (!tiqn || (tpg == iscsit_global->discovery_tpg))
391                 return 0;
392
393         spin_lock(&tiqn->tiqn_tpg_lock);
394         tiqn->tiqn_active_tpgs--;
395         pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
396                         tpg->tpgt);
397         spin_unlock(&tiqn->tiqn_tpg_lock);
398
399         return 0;
400 }
401
402 struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
403         struct iscsi_session *sess)
404 {
405         struct se_session *se_sess = sess->se_sess;
406         struct se_node_acl *se_nacl = se_sess->se_node_acl;
407         struct iscsi_node_acl *acl = container_of(se_nacl, struct iscsi_node_acl,
408                                         se_node_acl);
409
410         return &acl->node_attrib;
411 }
412
413 struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
414         struct iscsi_tpg_np *tpg_np,
415         int network_transport)
416 {
417         struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
418
419         spin_lock(&tpg_np->tpg_np_parent_lock);
420         list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
421                         &tpg_np->tpg_np_parent_list, tpg_np_child_list) {
422                 if (tpg_np_child->tpg_np->np_network_transport ==
423                                 network_transport) {
424                         spin_unlock(&tpg_np->tpg_np_parent_lock);
425                         return tpg_np_child;
426                 }
427         }
428         spin_unlock(&tpg_np->tpg_np_parent_lock);
429
430         return NULL;
431 }
432
433 static bool iscsit_tpg_check_network_portal(
434         struct iscsi_tiqn *tiqn,
435         struct __kernel_sockaddr_storage *sockaddr,
436         int network_transport)
437 {
438         struct iscsi_portal_group *tpg;
439         struct iscsi_tpg_np *tpg_np;
440         struct iscsi_np *np;
441         bool match = false;
442
443         spin_lock(&tiqn->tiqn_tpg_lock);
444         list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
445
446                 spin_lock(&tpg->tpg_np_lock);
447                 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
448                         np = tpg_np->tpg_np;
449
450                         match = iscsit_check_np_match(sockaddr, np,
451                                                 network_transport);
452                         if (match == true)
453                                 break;
454                 }
455                 spin_unlock(&tpg->tpg_np_lock);
456         }
457         spin_unlock(&tiqn->tiqn_tpg_lock);
458
459         return match;
460 }
461
462 struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
463         struct iscsi_portal_group *tpg,
464         struct __kernel_sockaddr_storage *sockaddr,
465         char *ip_str,
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) == true) {
475                         pr_err("Network Portal: %s already exists on a"
476                                 " different TPG on %s\n", ip_str,
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, ip_str, 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: %s:%hu,%hu on %s\n",
520                 tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, 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: %s:%hu,%hu on %s\n",
534                 tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, 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_tpg_set_initiator_node_queue_depth(
596         struct iscsi_portal_group *tpg,
597         unsigned char *initiatorname,
598         u32 queue_depth,
599         int force)
600 {
601         return core_tpg_set_initiator_node_queue_depth(&tpg->tpg_se_tpg,
602                 initiatorname, queue_depth, force);
603 }
604
605 int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
606 {
607         unsigned char buf1[256], buf2[256], *none = NULL;
608         int len;
609         struct iscsi_param *param;
610         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
611
612         if ((authentication != 1) && (authentication != 0)) {
613                 pr_err("Illegal value for authentication parameter:"
614                         " %u, ignoring request.\n", authentication);
615                 return -EINVAL;
616         }
617
618         memset(buf1, 0, sizeof(buf1));
619         memset(buf2, 0, sizeof(buf2));
620
621         param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
622         if (!param)
623                 return -EINVAL;
624
625         if (authentication) {
626                 snprintf(buf1, sizeof(buf1), "%s", param->value);
627                 none = strstr(buf1, NONE);
628                 if (!none)
629                         goto out;
630                 if (!strncmp(none + 4, ",", 1)) {
631                         if (!strcmp(buf1, none))
632                                 sprintf(buf2, "%s", none+5);
633                         else {
634                                 none--;
635                                 *none = '\0';
636                                 len = sprintf(buf2, "%s", buf1);
637                                 none += 5;
638                                 sprintf(buf2 + len, "%s", none);
639                         }
640                 } else {
641                         none--;
642                         *none = '\0';
643                         sprintf(buf2, "%s", buf1);
644                 }
645                 if (iscsi_update_param_value(param, buf2) < 0)
646                         return -EINVAL;
647         } else {
648                 snprintf(buf1, sizeof(buf1), "%s", param->value);
649                 none = strstr(buf1, NONE);
650                 if (none)
651                         goto out;
652                 strncat(buf1, ",", strlen(","));
653                 strncat(buf1, NONE, strlen(NONE));
654                 if (iscsi_update_param_value(param, buf1) < 0)
655                         return -EINVAL;
656         }
657
658 out:
659         a->authentication = authentication;
660         pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
661                 a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
662
663         return 0;
664 }
665
666 int iscsit_ta_login_timeout(
667         struct iscsi_portal_group *tpg,
668         u32 login_timeout)
669 {
670         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
671
672         if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
673                 pr_err("Requested Login Timeout %u larger than maximum"
674                         " %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
675                 return -EINVAL;
676         } else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
677                 pr_err("Requested Logout Timeout %u smaller than"
678                         " minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
679                 return -EINVAL;
680         }
681
682         a->login_timeout = login_timeout;
683         pr_debug("Set Logout Timeout to %u for Target Portal Group"
684                 " %hu\n", a->login_timeout, tpg->tpgt);
685
686         return 0;
687 }
688
689 int iscsit_ta_netif_timeout(
690         struct iscsi_portal_group *tpg,
691         u32 netif_timeout)
692 {
693         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
694
695         if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
696                 pr_err("Requested Network Interface Timeout %u larger"
697                         " than maximum %u\n", netif_timeout,
698                                 TA_NETIF_TIMEOUT_MAX);
699                 return -EINVAL;
700         } else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
701                 pr_err("Requested Network Interface Timeout %u smaller"
702                         " than minimum %u\n", netif_timeout,
703                                 TA_NETIF_TIMEOUT_MIN);
704                 return -EINVAL;
705         }
706
707         a->netif_timeout = netif_timeout;
708         pr_debug("Set Network Interface Timeout to %u for"
709                 " Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
710
711         return 0;
712 }
713
714 int iscsit_ta_generate_node_acls(
715         struct iscsi_portal_group *tpg,
716         u32 flag)
717 {
718         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
719
720         if ((flag != 0) && (flag != 1)) {
721                 pr_err("Illegal value %d\n", flag);
722                 return -EINVAL;
723         }
724
725         a->generate_node_acls = flag;
726         pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
727                 tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
728
729         if (flag == 1 && a->cache_dynamic_acls == 0) {
730                 pr_debug("Explicitly setting cache_dynamic_acls=1 when "
731                         "generate_node_acls=1\n");
732                 a->cache_dynamic_acls = 1;
733         }
734
735         return 0;
736 }
737
738 int iscsit_ta_default_cmdsn_depth(
739         struct iscsi_portal_group *tpg,
740         u32 tcq_depth)
741 {
742         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
743
744         if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
745                 pr_err("Requested Default Queue Depth: %u larger"
746                         " than maximum %u\n", tcq_depth,
747                                 TA_DEFAULT_CMDSN_DEPTH_MAX);
748                 return -EINVAL;
749         } else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
750                 pr_err("Requested Default Queue Depth: %u smaller"
751                         " than minimum %u\n", tcq_depth,
752                                 TA_DEFAULT_CMDSN_DEPTH_MIN);
753                 return -EINVAL;
754         }
755
756         a->default_cmdsn_depth = tcq_depth;
757         pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
758                 tpg->tpgt, a->default_cmdsn_depth);
759
760         return 0;
761 }
762
763 int iscsit_ta_cache_dynamic_acls(
764         struct iscsi_portal_group *tpg,
765         u32 flag)
766 {
767         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
768
769         if ((flag != 0) && (flag != 1)) {
770                 pr_err("Illegal value %d\n", flag);
771                 return -EINVAL;
772         }
773
774         if (a->generate_node_acls == 1 && flag == 0) {
775                 pr_debug("Skipping cache_dynamic_acls=0 when"
776                         " generate_node_acls=1\n");
777                 return 0;
778         }
779
780         a->cache_dynamic_acls = flag;
781         pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
782                 " ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
783                 "Enabled" : "Disabled");
784
785         return 0;
786 }
787
788 int iscsit_ta_demo_mode_write_protect(
789         struct iscsi_portal_group *tpg,
790         u32 flag)
791 {
792         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
793
794         if ((flag != 0) && (flag != 1)) {
795                 pr_err("Illegal value %d\n", flag);
796                 return -EINVAL;
797         }
798
799         a->demo_mode_write_protect = flag;
800         pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
801                 tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
802
803         return 0;
804 }
805
806 int iscsit_ta_prod_mode_write_protect(
807         struct iscsi_portal_group *tpg,
808         u32 flag)
809 {
810         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
811
812         if ((flag != 0) && (flag != 1)) {
813                 pr_err("Illegal value %d\n", flag);
814                 return -EINVAL;
815         }
816
817         a->prod_mode_write_protect = flag;
818         pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
819                 " %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
820                 "ON" : "OFF");
821
822         return 0;
823 }
824
825 int iscsit_ta_demo_mode_discovery(
826         struct iscsi_portal_group *tpg,
827         u32 flag)
828 {
829         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
830
831         if ((flag != 0) && (flag != 1)) {
832                 pr_err("Illegal value %d\n", flag);
833                 return -EINVAL;
834         }
835
836         a->demo_mode_discovery = flag;
837         pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
838                 " %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
839                 "ON" : "OFF");
840
841         return 0;
842 }