target: Convert se_portal_group->tpg_lun_list[] to RCU hlist
[pandora-kernel.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         ret = core_tpg_register(&iscsi_ops, NULL, &tpg->tpg_se_tpg, -1);
71         if (ret < 0) {
72                 kfree(tpg);
73                 return -1;
74         }
75
76         tpg->sid = 1; /* First Assigned LIO Session ID */
77         iscsit_set_default_tpg_attribs(tpg);
78
79         if (iscsi_create_default_params(&tpg->param_list) < 0)
80                 goto out;
81         /*
82          * By default we disable authentication for discovery sessions,
83          * this can be changed with:
84          *
85          * /sys/kernel/config/target/iscsi/discovery_auth/enforce_discovery_auth
86          */
87         param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
88         if (!param)
89                 goto out;
90
91         if (iscsi_update_param_value(param, "CHAP,None") < 0)
92                 goto out;
93
94         tpg->tpg_attrib.authentication = 0;
95
96         spin_lock(&tpg->tpg_state_lock);
97         tpg->tpg_state  = TPG_STATE_ACTIVE;
98         spin_unlock(&tpg->tpg_state_lock);
99
100         iscsit_global->discovery_tpg = tpg;
101         pr_debug("CORE[0] - Allocated Discovery TPG\n");
102
103         return 0;
104 out:
105         if (tpg->sid == 1)
106                 core_tpg_deregister(&tpg->tpg_se_tpg);
107         kfree(tpg);
108         return -1;
109 }
110
111 void iscsit_release_discovery_tpg(void)
112 {
113         struct iscsi_portal_group *tpg = iscsit_global->discovery_tpg;
114
115         if (!tpg)
116                 return;
117
118         core_tpg_deregister(&tpg->tpg_se_tpg);
119
120         kfree(tpg);
121         iscsit_global->discovery_tpg = NULL;
122 }
123
124 struct iscsi_portal_group *iscsit_get_tpg_from_np(
125         struct iscsi_tiqn *tiqn,
126         struct iscsi_np *np,
127         struct iscsi_tpg_np **tpg_np_out)
128 {
129         struct iscsi_portal_group *tpg = NULL;
130         struct iscsi_tpg_np *tpg_np;
131
132         spin_lock(&tiqn->tiqn_tpg_lock);
133         list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
134
135                 spin_lock(&tpg->tpg_state_lock);
136                 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
137                         spin_unlock(&tpg->tpg_state_lock);
138                         continue;
139                 }
140                 spin_unlock(&tpg->tpg_state_lock);
141
142                 spin_lock(&tpg->tpg_np_lock);
143                 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
144                         if (tpg_np->tpg_np == np) {
145                                 *tpg_np_out = tpg_np;
146                                 kref_get(&tpg_np->tpg_np_kref);
147                                 spin_unlock(&tpg->tpg_np_lock);
148                                 spin_unlock(&tiqn->tiqn_tpg_lock);
149                                 return tpg;
150                         }
151                 }
152                 spin_unlock(&tpg->tpg_np_lock);
153         }
154         spin_unlock(&tiqn->tiqn_tpg_lock);
155
156         return NULL;
157 }
158
159 int iscsit_get_tpg(
160         struct iscsi_portal_group *tpg)
161 {
162         return mutex_lock_interruptible(&tpg->tpg_access_lock);
163 }
164
165 void iscsit_put_tpg(struct iscsi_portal_group *tpg)
166 {
167         mutex_unlock(&tpg->tpg_access_lock);
168 }
169
170 static void iscsit_clear_tpg_np_login_thread(
171         struct iscsi_tpg_np *tpg_np,
172         struct iscsi_portal_group *tpg,
173         bool shutdown)
174 {
175         if (!tpg_np->tpg_np) {
176                 pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
177                 return;
178         }
179
180         if (shutdown)
181                 tpg_np->tpg_np->enabled = false;
182         iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg, shutdown);
183 }
184
185 static void iscsit_clear_tpg_np_login_threads(
186         struct iscsi_portal_group *tpg,
187         bool shutdown)
188 {
189         struct iscsi_tpg_np *tpg_np;
190
191         spin_lock(&tpg->tpg_np_lock);
192         list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
193                 if (!tpg_np->tpg_np) {
194                         pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
195                         continue;
196                 }
197                 spin_unlock(&tpg->tpg_np_lock);
198                 iscsit_clear_tpg_np_login_thread(tpg_np, tpg, shutdown);
199                 spin_lock(&tpg->tpg_np_lock);
200         }
201         spin_unlock(&tpg->tpg_np_lock);
202 }
203
204 void iscsit_tpg_dump_params(struct iscsi_portal_group *tpg)
205 {
206         iscsi_print_params(tpg->param_list);
207 }
208
209 static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *tpg)
210 {
211         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
212
213         a->authentication = TA_AUTHENTICATION;
214         a->login_timeout = TA_LOGIN_TIMEOUT;
215         a->netif_timeout = TA_NETIF_TIMEOUT;
216         a->default_cmdsn_depth = TA_DEFAULT_CMDSN_DEPTH;
217         a->generate_node_acls = TA_GENERATE_NODE_ACLS;
218         a->cache_dynamic_acls = TA_CACHE_DYNAMIC_ACLS;
219         a->demo_mode_write_protect = TA_DEMO_MODE_WRITE_PROTECT;
220         a->prod_mode_write_protect = TA_PROD_MODE_WRITE_PROTECT;
221         a->demo_mode_discovery = TA_DEMO_MODE_DISCOVERY;
222         a->default_erl = TA_DEFAULT_ERL;
223         a->t10_pi = TA_DEFAULT_T10_PI;
224         a->fabric_prot_type = TA_DEFAULT_FABRIC_PROT_TYPE;
225 }
226
227 int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
228 {
229         if (tpg->tpg_state != TPG_STATE_FREE) {
230                 pr_err("Unable to add iSCSI Target Portal Group: %d"
231                         " while not in TPG_STATE_FREE state.\n", tpg->tpgt);
232                 return -EEXIST;
233         }
234         iscsit_set_default_tpg_attribs(tpg);
235
236         if (iscsi_create_default_params(&tpg->param_list) < 0)
237                 goto err_out;
238
239         tpg->tpg_attrib.tpg = tpg;
240
241         spin_lock(&tpg->tpg_state_lock);
242         tpg->tpg_state  = TPG_STATE_INACTIVE;
243         spin_unlock(&tpg->tpg_state_lock);
244
245         spin_lock(&tiqn->tiqn_tpg_lock);
246         list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
247         tiqn->tiqn_ntpgs++;
248         pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
249                         tiqn->tiqn, tpg->tpgt);
250         spin_unlock(&tiqn->tiqn_tpg_lock);
251
252         return 0;
253 err_out:
254         if (tpg->param_list) {
255                 iscsi_release_param_list(tpg->param_list);
256                 tpg->param_list = NULL;
257         }
258         kfree(tpg);
259         return -ENOMEM;
260 }
261
262 int iscsit_tpg_del_portal_group(
263         struct iscsi_tiqn *tiqn,
264         struct iscsi_portal_group *tpg,
265         int force)
266 {
267         u8 old_state = tpg->tpg_state;
268
269         spin_lock(&tpg->tpg_state_lock);
270         tpg->tpg_state = TPG_STATE_INACTIVE;
271         spin_unlock(&tpg->tpg_state_lock);
272
273         if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
274                 pr_err("Unable to delete iSCSI Target Portal Group:"
275                         " %hu while active sessions exist, and force=0\n",
276                         tpg->tpgt);
277                 tpg->tpg_state = old_state;
278                 return -EPERM;
279         }
280
281         if (tpg->param_list) {
282                 iscsi_release_param_list(tpg->param_list);
283                 tpg->param_list = NULL;
284         }
285
286         core_tpg_deregister(&tpg->tpg_se_tpg);
287
288         spin_lock(&tpg->tpg_state_lock);
289         tpg->tpg_state = TPG_STATE_FREE;
290         spin_unlock(&tpg->tpg_state_lock);
291
292         spin_lock(&tiqn->tiqn_tpg_lock);
293         tiqn->tiqn_ntpgs--;
294         list_del(&tpg->tpg_list);
295         spin_unlock(&tiqn->tiqn_tpg_lock);
296
297         pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
298                         tiqn->tiqn, tpg->tpgt);
299
300         kfree(tpg);
301         return 0;
302 }
303
304 int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
305 {
306         struct iscsi_param *param;
307         struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
308         int ret;
309
310         spin_lock(&tpg->tpg_state_lock);
311         if (tpg->tpg_state == TPG_STATE_ACTIVE) {
312                 pr_err("iSCSI target portal group: %hu is already"
313                         " active, ignoring request.\n", tpg->tpgt);
314                 spin_unlock(&tpg->tpg_state_lock);
315                 return -EINVAL;
316         }
317         /*
318          * Make sure that AuthMethod does not contain None as an option
319          * unless explictly disabled.  Set the default to CHAP if authentication
320          * is enforced (as per default), and remove the NONE option.
321          */
322         param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
323         if (!param) {
324                 spin_unlock(&tpg->tpg_state_lock);
325                 return -EINVAL;
326         }
327
328         if (tpg->tpg_attrib.authentication) {
329                 if (!strcmp(param->value, NONE)) {
330                         ret = iscsi_update_param_value(param, CHAP);
331                         if (ret)
332                                 goto err;
333                 }
334
335                 ret = iscsit_ta_authentication(tpg, 1);
336                 if (ret < 0)
337                         goto err;
338         }
339
340         tpg->tpg_state = TPG_STATE_ACTIVE;
341         spin_unlock(&tpg->tpg_state_lock);
342
343         spin_lock(&tiqn->tiqn_tpg_lock);
344         tiqn->tiqn_active_tpgs++;
345         pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
346                         tpg->tpgt);
347         spin_unlock(&tiqn->tiqn_tpg_lock);
348
349         return 0;
350
351 err:
352         spin_unlock(&tpg->tpg_state_lock);
353         return ret;
354 }
355
356 int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
357 {
358         struct iscsi_tiqn *tiqn;
359         u8 old_state = tpg->tpg_state;
360
361         spin_lock(&tpg->tpg_state_lock);
362         if (tpg->tpg_state == TPG_STATE_INACTIVE) {
363                 pr_err("iSCSI Target Portal Group: %hu is already"
364                         " inactive, ignoring request.\n", tpg->tpgt);
365                 spin_unlock(&tpg->tpg_state_lock);
366                 return -EINVAL;
367         }
368         tpg->tpg_state = TPG_STATE_INACTIVE;
369         spin_unlock(&tpg->tpg_state_lock);
370
371         iscsit_clear_tpg_np_login_threads(tpg, false);
372
373         if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
374                 spin_lock(&tpg->tpg_state_lock);
375                 tpg->tpg_state = old_state;
376                 spin_unlock(&tpg->tpg_state_lock);
377                 pr_err("Unable to disable iSCSI Target Portal Group:"
378                         " %hu while active sessions exist, and force=0\n",
379                         tpg->tpgt);
380                 return -EPERM;
381         }
382
383         tiqn = tpg->tpg_tiqn;
384         if (!tiqn || (tpg == iscsit_global->discovery_tpg))
385                 return 0;
386
387         spin_lock(&tiqn->tiqn_tpg_lock);
388         tiqn->tiqn_active_tpgs--;
389         pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
390                         tpg->tpgt);
391         spin_unlock(&tiqn->tiqn_tpg_lock);
392
393         return 0;
394 }
395
396 struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
397         struct iscsi_session *sess)
398 {
399         struct se_session *se_sess = sess->se_sess;
400         struct se_node_acl *se_nacl = se_sess->se_node_acl;
401         struct iscsi_node_acl *acl = container_of(se_nacl, struct iscsi_node_acl,
402                                         se_node_acl);
403
404         return &acl->node_attrib;
405 }
406
407 struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
408         struct iscsi_tpg_np *tpg_np,
409         int network_transport)
410 {
411         struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
412
413         spin_lock(&tpg_np->tpg_np_parent_lock);
414         list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
415                         &tpg_np->tpg_np_parent_list, tpg_np_child_list) {
416                 if (tpg_np_child->tpg_np->np_network_transport ==
417                                 network_transport) {
418                         spin_unlock(&tpg_np->tpg_np_parent_lock);
419                         return tpg_np_child;
420                 }
421         }
422         spin_unlock(&tpg_np->tpg_np_parent_lock);
423
424         return NULL;
425 }
426
427 static bool iscsit_tpg_check_network_portal(
428         struct iscsi_tiqn *tiqn,
429         struct __kernel_sockaddr_storage *sockaddr,
430         int network_transport)
431 {
432         struct iscsi_portal_group *tpg;
433         struct iscsi_tpg_np *tpg_np;
434         struct iscsi_np *np;
435         bool match = false;
436
437         spin_lock(&tiqn->tiqn_tpg_lock);
438         list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
439
440                 spin_lock(&tpg->tpg_np_lock);
441                 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
442                         np = tpg_np->tpg_np;
443
444                         match = iscsit_check_np_match(sockaddr, np,
445                                                 network_transport);
446                         if (match)
447                                 break;
448                 }
449                 spin_unlock(&tpg->tpg_np_lock);
450         }
451         spin_unlock(&tiqn->tiqn_tpg_lock);
452
453         return match;
454 }
455
456 struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
457         struct iscsi_portal_group *tpg,
458         struct __kernel_sockaddr_storage *sockaddr,
459         char *ip_str,
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: %s already exists on a"
470                                 " different TPG on %s\n", ip_str,
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, ip_str, 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: %s:%hu,%hu on %s\n",
514                 tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, 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: %s:%hu,%hu on %s\n",
528                 tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, 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_tpg_set_initiator_node_queue_depth(
590         struct iscsi_portal_group *tpg,
591         unsigned char *initiatorname,
592         u32 queue_depth,
593         int force)
594 {
595         return core_tpg_set_initiator_node_queue_depth(&tpg->tpg_se_tpg,
596                 initiatorname, queue_depth, force);
597 }
598
599 int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
600 {
601         unsigned char buf1[256], buf2[256], *none = NULL;
602         int len;
603         struct iscsi_param *param;
604         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
605
606         if ((authentication != 1) && (authentication != 0)) {
607                 pr_err("Illegal value for authentication parameter:"
608                         " %u, ignoring request.\n", authentication);
609                 return -EINVAL;
610         }
611
612         memset(buf1, 0, sizeof(buf1));
613         memset(buf2, 0, sizeof(buf2));
614
615         param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
616         if (!param)
617                 return -EINVAL;
618
619         if (authentication) {
620                 snprintf(buf1, sizeof(buf1), "%s", param->value);
621                 none = strstr(buf1, NONE);
622                 if (!none)
623                         goto out;
624                 if (!strncmp(none + 4, ",", 1)) {
625                         if (!strcmp(buf1, none))
626                                 sprintf(buf2, "%s", none+5);
627                         else {
628                                 none--;
629                                 *none = '\0';
630                                 len = sprintf(buf2, "%s", buf1);
631                                 none += 5;
632                                 sprintf(buf2 + len, "%s", none);
633                         }
634                 } else {
635                         none--;
636                         *none = '\0';
637                         sprintf(buf2, "%s", buf1);
638                 }
639                 if (iscsi_update_param_value(param, buf2) < 0)
640                         return -EINVAL;
641         } else {
642                 snprintf(buf1, sizeof(buf1), "%s", param->value);
643                 none = strstr(buf1, NONE);
644                 if (none)
645                         goto out;
646                 strncat(buf1, ",", strlen(","));
647                 strncat(buf1, NONE, strlen(NONE));
648                 if (iscsi_update_param_value(param, buf1) < 0)
649                         return -EINVAL;
650         }
651
652 out:
653         a->authentication = authentication;
654         pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
655                 a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
656
657         return 0;
658 }
659
660 int iscsit_ta_login_timeout(
661         struct iscsi_portal_group *tpg,
662         u32 login_timeout)
663 {
664         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
665
666         if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
667                 pr_err("Requested Login Timeout %u larger than maximum"
668                         " %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
669                 return -EINVAL;
670         } else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
671                 pr_err("Requested Logout Timeout %u smaller than"
672                         " minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
673                 return -EINVAL;
674         }
675
676         a->login_timeout = login_timeout;
677         pr_debug("Set Logout Timeout to %u for Target Portal Group"
678                 " %hu\n", a->login_timeout, tpg->tpgt);
679
680         return 0;
681 }
682
683 int iscsit_ta_netif_timeout(
684         struct iscsi_portal_group *tpg,
685         u32 netif_timeout)
686 {
687         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
688
689         if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
690                 pr_err("Requested Network Interface Timeout %u larger"
691                         " than maximum %u\n", netif_timeout,
692                                 TA_NETIF_TIMEOUT_MAX);
693                 return -EINVAL;
694         } else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
695                 pr_err("Requested Network Interface Timeout %u smaller"
696                         " than minimum %u\n", netif_timeout,
697                                 TA_NETIF_TIMEOUT_MIN);
698                 return -EINVAL;
699         }
700
701         a->netif_timeout = netif_timeout;
702         pr_debug("Set Network Interface Timeout to %u for"
703                 " Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
704
705         return 0;
706 }
707
708 int iscsit_ta_generate_node_acls(
709         struct iscsi_portal_group *tpg,
710         u32 flag)
711 {
712         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
713
714         if ((flag != 0) && (flag != 1)) {
715                 pr_err("Illegal value %d\n", flag);
716                 return -EINVAL;
717         }
718
719         a->generate_node_acls = flag;
720         pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
721                 tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
722
723         if (flag == 1 && a->cache_dynamic_acls == 0) {
724                 pr_debug("Explicitly setting cache_dynamic_acls=1 when "
725                         "generate_node_acls=1\n");
726                 a->cache_dynamic_acls = 1;
727         }
728
729         return 0;
730 }
731
732 int iscsit_ta_default_cmdsn_depth(
733         struct iscsi_portal_group *tpg,
734         u32 tcq_depth)
735 {
736         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
737
738         if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
739                 pr_err("Requested Default Queue Depth: %u larger"
740                         " than maximum %u\n", tcq_depth,
741                                 TA_DEFAULT_CMDSN_DEPTH_MAX);
742                 return -EINVAL;
743         } else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
744                 pr_err("Requested Default Queue Depth: %u smaller"
745                         " than minimum %u\n", tcq_depth,
746                                 TA_DEFAULT_CMDSN_DEPTH_MIN);
747                 return -EINVAL;
748         }
749
750         a->default_cmdsn_depth = tcq_depth;
751         pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
752                 tpg->tpgt, a->default_cmdsn_depth);
753
754         return 0;
755 }
756
757 int iscsit_ta_cache_dynamic_acls(
758         struct iscsi_portal_group *tpg,
759         u32 flag)
760 {
761         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
762
763         if ((flag != 0) && (flag != 1)) {
764                 pr_err("Illegal value %d\n", flag);
765                 return -EINVAL;
766         }
767
768         if (a->generate_node_acls == 1 && flag == 0) {
769                 pr_debug("Skipping cache_dynamic_acls=0 when"
770                         " generate_node_acls=1\n");
771                 return 0;
772         }
773
774         a->cache_dynamic_acls = flag;
775         pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
776                 " ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
777                 "Enabled" : "Disabled");
778
779         return 0;
780 }
781
782 int iscsit_ta_demo_mode_write_protect(
783         struct iscsi_portal_group *tpg,
784         u32 flag)
785 {
786         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
787
788         if ((flag != 0) && (flag != 1)) {
789                 pr_err("Illegal value %d\n", flag);
790                 return -EINVAL;
791         }
792
793         a->demo_mode_write_protect = flag;
794         pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
795                 tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
796
797         return 0;
798 }
799
800 int iscsit_ta_prod_mode_write_protect(
801         struct iscsi_portal_group *tpg,
802         u32 flag)
803 {
804         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
805
806         if ((flag != 0) && (flag != 1)) {
807                 pr_err("Illegal value %d\n", flag);
808                 return -EINVAL;
809         }
810
811         a->prod_mode_write_protect = flag;
812         pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
813                 " %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
814                 "ON" : "OFF");
815
816         return 0;
817 }
818
819 int iscsit_ta_demo_mode_discovery(
820         struct iscsi_portal_group *tpg,
821         u32 flag)
822 {
823         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
824
825         if ((flag != 0) && (flag != 1)) {
826                 pr_err("Illegal value %d\n", flag);
827                 return -EINVAL;
828         }
829
830         a->demo_mode_discovery = flag;
831         pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
832                 " %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
833                 "ON" : "OFF");
834
835         return 0;
836 }
837
838 int iscsit_ta_default_erl(
839         struct iscsi_portal_group *tpg,
840         u32 default_erl)
841 {
842         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
843
844         if ((default_erl != 0) && (default_erl != 1) && (default_erl != 2)) {
845                 pr_err("Illegal value for default_erl: %u\n", default_erl);
846                 return -EINVAL;
847         }
848
849         a->default_erl = default_erl;
850         pr_debug("iSCSI_TPG[%hu] - DefaultERL: %u\n", tpg->tpgt, a->default_erl);
851
852         return 0;
853 }
854
855 int iscsit_ta_t10_pi(
856         struct iscsi_portal_group *tpg,
857         u32 flag)
858 {
859         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
860
861         if ((flag != 0) && (flag != 1)) {
862                 pr_err("Illegal value %d\n", flag);
863                 return -EINVAL;
864         }
865
866         a->t10_pi = flag;
867         pr_debug("iSCSI_TPG[%hu] - T10 Protection information bit:"
868                 " %s\n", tpg->tpgt, (a->t10_pi) ?
869                 "ON" : "OFF");
870
871         return 0;
872 }
873
874 int iscsit_ta_fabric_prot_type(
875         struct iscsi_portal_group *tpg,
876         u32 prot_type)
877 {
878         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
879
880         if ((prot_type != 0) && (prot_type != 1) && (prot_type != 3)) {
881                 pr_err("Illegal value for fabric_prot_type: %u\n", prot_type);
882                 return -EINVAL;
883         }
884
885         a->fabric_prot_type = prot_type;
886         pr_debug("iSCSI_TPG[%hu] - T10 Fabric Protection Type: %u\n",
887                  tpg->tpgt, prot_type);
888
889         return 0;
890 }