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