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