Linux-libre 3.16.85-gnu
[librecmc/linux-libre.git] / drivers / target / iscsi / iscsi_target_configfs.c
1 /*******************************************************************************
2  * This file contains the configfs implementation for iSCSI Target mode
3  * from the LIO-Target Project.
4  *
5  * (c) Copyright 2007-2013 Datera, Inc.
6  *
7  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  ****************************************************************************/
19
20 #include <linux/configfs.h>
21 #include <linux/ctype.h>
22 #include <linux/export.h>
23 #include <linux/inet.h>
24 #include <target/target_core_base.h>
25 #include <target/target_core_fabric.h>
26 #include <target/target_core_fabric_configfs.h>
27 #include <target/target_core_configfs.h>
28 #include <target/configfs_macros.h>
29 #include <target/iscsi/iscsi_transport.h>
30
31 #include "iscsi_target_core.h"
32 #include "iscsi_target_parameters.h"
33 #include "iscsi_target_device.h"
34 #include "iscsi_target_erl0.h"
35 #include "iscsi_target_nodeattrib.h"
36 #include "iscsi_target_tpg.h"
37 #include "iscsi_target_util.h"
38 #include "iscsi_target.h"
39 #include "iscsi_target_stat.h"
40 #include "iscsi_target_configfs.h"
41
42 struct target_fabric_configfs *lio_target_fabric_configfs;
43
44 struct lio_target_configfs_attribute {
45         struct configfs_attribute attr;
46         ssize_t (*show)(void *, char *);
47         ssize_t (*store)(void *, const char *, size_t);
48 };
49
50 /* Start items for lio_target_portal_cit */
51
52 static ssize_t lio_target_np_show_sctp(
53         struct se_tpg_np *se_tpg_np,
54         char *page)
55 {
56         struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
57                                 struct iscsi_tpg_np, se_tpg_np);
58         struct iscsi_tpg_np *tpg_np_sctp;
59         ssize_t rb;
60
61         tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
62         if (tpg_np_sctp)
63                 rb = sprintf(page, "1\n");
64         else
65                 rb = sprintf(page, "0\n");
66
67         return rb;
68 }
69
70 static ssize_t lio_target_np_store_sctp(
71         struct se_tpg_np *se_tpg_np,
72         const char *page,
73         size_t count)
74 {
75         struct iscsi_np *np;
76         struct iscsi_portal_group *tpg;
77         struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
78                                 struct iscsi_tpg_np, se_tpg_np);
79         struct iscsi_tpg_np *tpg_np_sctp = NULL;
80         u32 op;
81         int ret;
82
83         ret = kstrtou32(page, 0, &op);
84         if (ret)
85                 return ret;
86         if ((op != 1) && (op != 0)) {
87                 pr_err("Illegal value for tpg_enable: %u\n", op);
88                 return -EINVAL;
89         }
90         np = tpg_np->tpg_np;
91         if (!np) {
92                 pr_err("Unable to locate struct iscsi_np from"
93                                 " struct iscsi_tpg_np\n");
94                 return -EINVAL;
95         }
96
97         tpg = tpg_np->tpg;
98         if (iscsit_get_tpg(tpg) < 0)
99                 return -EINVAL;
100
101         if (op) {
102                 /*
103                  * Use existing np->np_sockaddr for SCTP network portal reference
104                  */
105                 tpg_np_sctp = iscsit_tpg_add_network_portal(tpg, &np->np_sockaddr,
106                                         np->np_ip, tpg_np, ISCSI_SCTP_TCP);
107                 if (!tpg_np_sctp || IS_ERR(tpg_np_sctp))
108                         goto out;
109         } else {
110                 tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
111                 if (!tpg_np_sctp)
112                         goto out;
113
114                 ret = iscsit_tpg_del_network_portal(tpg, tpg_np_sctp);
115                 if (ret < 0)
116                         goto out;
117         }
118
119         iscsit_put_tpg(tpg);
120         return count;
121 out:
122         iscsit_put_tpg(tpg);
123         return -EINVAL;
124 }
125
126 TF_NP_BASE_ATTR(lio_target, sctp, S_IRUGO | S_IWUSR);
127
128 static ssize_t lio_target_np_show_iser(
129         struct se_tpg_np *se_tpg_np,
130         char *page)
131 {
132         struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
133                                 struct iscsi_tpg_np, se_tpg_np);
134         struct iscsi_tpg_np *tpg_np_iser;
135         ssize_t rb;
136
137         tpg_np_iser = iscsit_tpg_locate_child_np(tpg_np, ISCSI_INFINIBAND);
138         if (tpg_np_iser)
139                 rb = sprintf(page, "1\n");
140         else
141                 rb = sprintf(page, "0\n");
142
143         return rb;
144 }
145
146 static ssize_t lio_target_np_store_iser(
147         struct se_tpg_np *se_tpg_np,
148         const char *page,
149         size_t count)
150 {
151         struct iscsi_np *np;
152         struct iscsi_portal_group *tpg;
153         struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
154                                 struct iscsi_tpg_np, se_tpg_np);
155         struct iscsi_tpg_np *tpg_np_iser = NULL;
156         char *endptr;
157         u32 op;
158         int rc = 0;
159
160         op = simple_strtoul(page, &endptr, 0);
161         if ((op != 1) && (op != 0)) {
162                 pr_err("Illegal value for tpg_enable: %u\n", op);
163                 return -EINVAL;
164         }
165         np = tpg_np->tpg_np;
166         if (!np) {
167                 pr_err("Unable to locate struct iscsi_np from"
168                                 " struct iscsi_tpg_np\n");
169                 return -EINVAL;
170         }
171
172         tpg = tpg_np->tpg;
173         if (iscsit_get_tpg(tpg) < 0)
174                 return -EINVAL;
175
176         if (op) {
177                 rc = request_module("ib_isert");
178                 if (rc != 0) {
179                         pr_warn("Unable to request_module for ib_isert\n");
180                         rc = 0;
181                 }
182
183                 tpg_np_iser = iscsit_tpg_add_network_portal(tpg, &np->np_sockaddr,
184                                 np->np_ip, tpg_np, ISCSI_INFINIBAND);
185                 if (IS_ERR(tpg_np_iser)) {
186                         rc = PTR_ERR(tpg_np_iser);
187                         goto out;
188                 }
189         } else {
190                 tpg_np_iser = iscsit_tpg_locate_child_np(tpg_np, ISCSI_INFINIBAND);
191                 if (tpg_np_iser) {
192                         rc = iscsit_tpg_del_network_portal(tpg, tpg_np_iser);
193                         if (rc < 0)
194                                 goto out;
195                 }
196         }
197
198         iscsit_put_tpg(tpg);
199         return count;
200 out:
201         iscsit_put_tpg(tpg);
202         return rc;
203 }
204
205 TF_NP_BASE_ATTR(lio_target, iser, S_IRUGO | S_IWUSR);
206
207 static struct configfs_attribute *lio_target_portal_attrs[] = {
208         &lio_target_np_sctp.attr,
209         &lio_target_np_iser.attr,
210         NULL,
211 };
212
213 /* Stop items for lio_target_portal_cit */
214
215 /* Start items for lio_target_np_cit */
216
217 #define MAX_PORTAL_LEN          256
218
219 static struct se_tpg_np *lio_target_call_addnptotpg(
220         struct se_portal_group *se_tpg,
221         struct config_group *group,
222         const char *name)
223 {
224         struct iscsi_portal_group *tpg;
225         struct iscsi_tpg_np *tpg_np;
226         char *str, *str2, *ip_str, *port_str;
227         struct __kernel_sockaddr_storage sockaddr;
228         struct sockaddr_in *sock_in;
229         struct sockaddr_in6 *sock_in6;
230         unsigned long port;
231         int ret;
232         char buf[MAX_PORTAL_LEN + 1];
233
234         if (strlen(name) > MAX_PORTAL_LEN) {
235                 pr_err("strlen(name): %d exceeds MAX_PORTAL_LEN: %d\n",
236                         (int)strlen(name), MAX_PORTAL_LEN);
237                 return ERR_PTR(-EOVERFLOW);
238         }
239         memset(buf, 0, MAX_PORTAL_LEN + 1);
240         snprintf(buf, MAX_PORTAL_LEN + 1, "%s", name);
241
242         memset(&sockaddr, 0, sizeof(struct __kernel_sockaddr_storage));
243
244         str = strstr(buf, "[");
245         if (str) {
246                 const char *end;
247
248                 str2 = strstr(str, "]");
249                 if (!str2) {
250                         pr_err("Unable to locate trailing \"]\""
251                                 " in IPv6 iSCSI network portal address\n");
252                         return ERR_PTR(-EINVAL);
253                 }
254                 str++; /* Skip over leading "[" */
255                 *str2 = '\0'; /* Terminate the IPv6 address */
256                 str2++; /* Skip over the "]" */
257                 port_str = strstr(str2, ":");
258                 if (!port_str) {
259                         pr_err("Unable to locate \":port\""
260                                 " in IPv6 iSCSI network portal address\n");
261                         return ERR_PTR(-EINVAL);
262                 }
263                 *port_str = '\0'; /* Terminate string for IP */
264                 port_str++; /* Skip over ":" */
265
266                 ret = kstrtoul(port_str, 0, &port);
267                 if (ret < 0) {
268                         pr_err("kstrtoul() failed for port_str: %d\n", ret);
269                         return ERR_PTR(ret);
270                 }
271                 sock_in6 = (struct sockaddr_in6 *)&sockaddr;
272                 sock_in6->sin6_family = AF_INET6;
273                 sock_in6->sin6_port = htons((unsigned short)port);
274                 ret = in6_pton(str, IPV6_ADDRESS_SPACE,
275                                 (void *)&sock_in6->sin6_addr.in6_u, -1, &end);
276                 if (ret <= 0) {
277                         pr_err("in6_pton returned: %d\n", ret);
278                         return ERR_PTR(-EINVAL);
279                 }
280         } else {
281                 str = ip_str = &buf[0];
282                 port_str = strstr(ip_str, ":");
283                 if (!port_str) {
284                         pr_err("Unable to locate \":port\""
285                                 " in IPv4 iSCSI network portal address\n");
286                         return ERR_PTR(-EINVAL);
287                 }
288                 *port_str = '\0'; /* Terminate string for IP */
289                 port_str++; /* Skip over ":" */
290
291                 ret = kstrtoul(port_str, 0, &port);
292                 if (ret < 0) {
293                         pr_err("kstrtoul() failed for port_str: %d\n", ret);
294                         return ERR_PTR(ret);
295                 }
296                 sock_in = (struct sockaddr_in *)&sockaddr;
297                 sock_in->sin_family = AF_INET;
298                 sock_in->sin_port = htons((unsigned short)port);
299                 sock_in->sin_addr.s_addr = in_aton(ip_str);
300         }
301         tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
302         ret = iscsit_get_tpg(tpg);
303         if (ret < 0)
304                 return ERR_PTR(-EINVAL);
305
306         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
307                 " PORTAL: %s\n",
308                 config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
309                 tpg->tpgt, name);
310         /*
311          * Assume ISCSI_TCP by default.  Other network portals for other
312          * iSCSI fabrics:
313          *
314          * Traditional iSCSI over SCTP (initial support)
315          * iSER/TCP (TODO, hardware available)
316          * iSER/SCTP (TODO, software emulation with osc-iwarp)
317          * iSER/IB (TODO, hardware available)
318          *
319          * can be enabled with attributes under
320          * sys/kernel/config/iscsi/$IQN/$TPG/np/$IP:$PORT/
321          *
322          */
323         tpg_np = iscsit_tpg_add_network_portal(tpg, &sockaddr, str, NULL,
324                                 ISCSI_TCP);
325         if (IS_ERR(tpg_np)) {
326                 iscsit_put_tpg(tpg);
327                 return ERR_CAST(tpg_np);
328         }
329         pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
330
331         iscsit_put_tpg(tpg);
332         return &tpg_np->se_tpg_np;
333 }
334
335 static void lio_target_call_delnpfromtpg(
336         struct se_tpg_np *se_tpg_np)
337 {
338         struct iscsi_portal_group *tpg;
339         struct iscsi_tpg_np *tpg_np;
340         struct se_portal_group *se_tpg;
341         int ret;
342
343         tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
344         tpg = tpg_np->tpg;
345         ret = iscsit_get_tpg(tpg);
346         if (ret < 0)
347                 return;
348
349         se_tpg = &tpg->tpg_se_tpg;
350         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s TPGT: %hu"
351                 " PORTAL: %s:%hu\n", config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
352                 tpg->tpgt, tpg_np->tpg_np->np_ip, tpg_np->tpg_np->np_port);
353
354         ret = iscsit_tpg_del_network_portal(tpg, tpg_np);
355         if (ret < 0)
356                 goto out;
357
358         pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
359 out:
360         iscsit_put_tpg(tpg);
361 }
362
363 /* End items for lio_target_np_cit */
364
365 /* Start items for lio_target_nacl_attrib_cit */
366
367 #define DEF_NACL_ATTRIB(name)                                           \
368 static ssize_t iscsi_nacl_attrib_show_##name(                           \
369         struct se_node_acl *se_nacl,                                    \
370         char *page)                                                     \
371 {                                                                       \
372         struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
373                                         se_node_acl);                   \
374                                                                         \
375         return sprintf(page, "%u\n", nacl->node_attrib.name);           \
376 }                                                                       \
377                                                                         \
378 static ssize_t iscsi_nacl_attrib_store_##name(                          \
379         struct se_node_acl *se_nacl,                                    \
380         const char *page,                                               \
381         size_t count)                                                   \
382 {                                                                       \
383         struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
384                                         se_node_acl);                   \
385         u32 val;                                                        \
386         int ret;                                                        \
387                                                                         \
388         ret = kstrtou32(page, 0, &val);                                 \
389         if (ret)                                                        \
390                 return ret;                                             \
391         ret = iscsit_na_##name(nacl, val);                              \
392         if (ret < 0)                                                    \
393                 return ret;                                             \
394                                                                         \
395         return count;                                                   \
396 }
397
398 #define NACL_ATTR(_name, _mode) TF_NACL_ATTRIB_ATTR(iscsi, _name, _mode);
399 /*
400  * Define iscsi_node_attrib_s_dataout_timeout
401  */
402 DEF_NACL_ATTRIB(dataout_timeout);
403 NACL_ATTR(dataout_timeout, S_IRUGO | S_IWUSR);
404 /*
405  * Define iscsi_node_attrib_s_dataout_timeout_retries
406  */
407 DEF_NACL_ATTRIB(dataout_timeout_retries);
408 NACL_ATTR(dataout_timeout_retries, S_IRUGO | S_IWUSR);
409 /*
410  * Define iscsi_node_attrib_s_default_erl
411  */
412 DEF_NACL_ATTRIB(default_erl);
413 NACL_ATTR(default_erl, S_IRUGO | S_IWUSR);
414 /*
415  * Define iscsi_node_attrib_s_nopin_timeout
416  */
417 DEF_NACL_ATTRIB(nopin_timeout);
418 NACL_ATTR(nopin_timeout, S_IRUGO | S_IWUSR);
419 /*
420  * Define iscsi_node_attrib_s_nopin_response_timeout
421  */
422 DEF_NACL_ATTRIB(nopin_response_timeout);
423 NACL_ATTR(nopin_response_timeout, S_IRUGO | S_IWUSR);
424 /*
425  * Define iscsi_node_attrib_s_random_datain_pdu_offsets
426  */
427 DEF_NACL_ATTRIB(random_datain_pdu_offsets);
428 NACL_ATTR(random_datain_pdu_offsets, S_IRUGO | S_IWUSR);
429 /*
430  * Define iscsi_node_attrib_s_random_datain_seq_offsets
431  */
432 DEF_NACL_ATTRIB(random_datain_seq_offsets);
433 NACL_ATTR(random_datain_seq_offsets, S_IRUGO | S_IWUSR);
434 /*
435  * Define iscsi_node_attrib_s_random_r2t_offsets
436  */
437 DEF_NACL_ATTRIB(random_r2t_offsets);
438 NACL_ATTR(random_r2t_offsets, S_IRUGO | S_IWUSR);
439
440 static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
441         &iscsi_nacl_attrib_dataout_timeout.attr,
442         &iscsi_nacl_attrib_dataout_timeout_retries.attr,
443         &iscsi_nacl_attrib_default_erl.attr,
444         &iscsi_nacl_attrib_nopin_timeout.attr,
445         &iscsi_nacl_attrib_nopin_response_timeout.attr,
446         &iscsi_nacl_attrib_random_datain_pdu_offsets.attr,
447         &iscsi_nacl_attrib_random_datain_seq_offsets.attr,
448         &iscsi_nacl_attrib_random_r2t_offsets.attr,
449         NULL,
450 };
451
452 /* End items for lio_target_nacl_attrib_cit */
453
454 /* Start items for lio_target_nacl_auth_cit */
455
456 #define __DEF_NACL_AUTH_STR(prefix, name, flags)                        \
457 static ssize_t __iscsi_##prefix##_show_##name(                          \
458         struct iscsi_node_acl *nacl,                                    \
459         char *page)                                                     \
460 {                                                                       \
461         struct iscsi_node_auth *auth = &nacl->node_auth;                \
462                                                                         \
463         if (!capable(CAP_SYS_ADMIN))                                    \
464                 return -EPERM;                                          \
465         return snprintf(page, PAGE_SIZE, "%s\n", auth->name);           \
466 }                                                                       \
467                                                                         \
468 static ssize_t __iscsi_##prefix##_store_##name(                         \
469         struct iscsi_node_acl *nacl,                                    \
470         const char *page,                                               \
471         size_t count)                                                   \
472 {                                                                       \
473         struct iscsi_node_auth *auth = &nacl->node_auth;                \
474                                                                         \
475         if (!capable(CAP_SYS_ADMIN))                                    \
476                 return -EPERM;                                          \
477         if (count >= sizeof(auth->name))                                \
478                 return -EINVAL;                                         \
479         snprintf(auth->name, sizeof(auth->name), "%s", page);           \
480         if (!strncmp("NULL", auth->name, 4))                            \
481                 auth->naf_flags &= ~flags;                              \
482         else                                                            \
483                 auth->naf_flags |= flags;                               \
484                                                                         \
485         if ((auth->naf_flags & NAF_USERID_IN_SET) &&                    \
486             (auth->naf_flags & NAF_PASSWORD_IN_SET))                    \
487                 auth->authenticate_target = 1;                          \
488         else                                                            \
489                 auth->authenticate_target = 0;                          \
490                                                                         \
491         return count;                                                   \
492 }
493
494 #define __DEF_NACL_AUTH_INT(prefix, name)                               \
495 static ssize_t __iscsi_##prefix##_show_##name(                          \
496         struct iscsi_node_acl *nacl,                                    \
497         char *page)                                                     \
498 {                                                                       \
499         struct iscsi_node_auth *auth = &nacl->node_auth;                \
500                                                                         \
501         if (!capable(CAP_SYS_ADMIN))                                    \
502                 return -EPERM;                                          \
503                                                                         \
504         return snprintf(page, PAGE_SIZE, "%d\n", auth->name);           \
505 }
506
507 #define DEF_NACL_AUTH_STR(name, flags)                                  \
508         __DEF_NACL_AUTH_STR(nacl_auth, name, flags)                     \
509 static ssize_t iscsi_nacl_auth_show_##name(                             \
510         struct se_node_acl *nacl,                                       \
511         char *page)                                                     \
512 {                                                                       \
513         return __iscsi_nacl_auth_show_##name(container_of(nacl,         \
514                         struct iscsi_node_acl, se_node_acl), page);             \
515 }                                                                       \
516 static ssize_t iscsi_nacl_auth_store_##name(                            \
517         struct se_node_acl *nacl,                                       \
518         const char *page,                                               \
519         size_t count)                                                   \
520 {                                                                       \
521         return __iscsi_nacl_auth_store_##name(container_of(nacl,        \
522                         struct iscsi_node_acl, se_node_acl), page, count);      \
523 }
524
525 #define DEF_NACL_AUTH_INT(name)                                         \
526         __DEF_NACL_AUTH_INT(nacl_auth, name)                            \
527 static ssize_t iscsi_nacl_auth_show_##name(                             \
528         struct se_node_acl *nacl,                                       \
529         char *page)                                                     \
530 {                                                                       \
531         return __iscsi_nacl_auth_show_##name(container_of(nacl,         \
532                         struct iscsi_node_acl, se_node_acl), page);             \
533 }
534
535 #define AUTH_ATTR(_name, _mode) TF_NACL_AUTH_ATTR(iscsi, _name, _mode);
536 #define AUTH_ATTR_RO(_name) TF_NACL_AUTH_ATTR_RO(iscsi, _name);
537
538 /*
539  * One-way authentication userid
540  */
541 DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
542 AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
543 /*
544  * One-way authentication password
545  */
546 DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
547 AUTH_ATTR(password, S_IRUGO | S_IWUSR);
548 /*
549  * Enforce mutual authentication
550  */
551 DEF_NACL_AUTH_INT(authenticate_target);
552 AUTH_ATTR_RO(authenticate_target);
553 /*
554  * Mutual authentication userid
555  */
556 DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
557 AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
558 /*
559  * Mutual authentication password
560  */
561 DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
562 AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
563
564 static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
565         &iscsi_nacl_auth_userid.attr,
566         &iscsi_nacl_auth_password.attr,
567         &iscsi_nacl_auth_authenticate_target.attr,
568         &iscsi_nacl_auth_userid_mutual.attr,
569         &iscsi_nacl_auth_password_mutual.attr,
570         NULL,
571 };
572
573 /* End items for lio_target_nacl_auth_cit */
574
575 /* Start items for lio_target_nacl_param_cit */
576
577 #define DEF_NACL_PARAM(name)                                            \
578 static ssize_t iscsi_nacl_param_show_##name(                            \
579         struct se_node_acl *se_nacl,                                    \
580         char *page)                                                     \
581 {                                                                       \
582         struct iscsi_session *sess;                                     \
583         struct se_session *se_sess;                                     \
584         ssize_t rb;                                                     \
585                                                                         \
586         spin_lock_bh(&se_nacl->nacl_sess_lock);                         \
587         se_sess = se_nacl->nacl_sess;                                   \
588         if (!se_sess) {                                                 \
589                 rb = snprintf(page, PAGE_SIZE,                          \
590                         "No Active iSCSI Session\n");                   \
591         } else {                                                        \
592                 sess = se_sess->fabric_sess_ptr;                        \
593                 rb = snprintf(page, PAGE_SIZE, "%u\n",                  \
594                         (u32)sess->sess_ops->name);                     \
595         }                                                               \
596         spin_unlock_bh(&se_nacl->nacl_sess_lock);                       \
597                                                                         \
598         return rb;                                                      \
599 }
600
601 #define NACL_PARAM_ATTR(_name) TF_NACL_PARAM_ATTR_RO(iscsi, _name);
602
603 DEF_NACL_PARAM(MaxConnections);
604 NACL_PARAM_ATTR(MaxConnections);
605
606 DEF_NACL_PARAM(InitialR2T);
607 NACL_PARAM_ATTR(InitialR2T);
608
609 DEF_NACL_PARAM(ImmediateData);
610 NACL_PARAM_ATTR(ImmediateData);
611
612 DEF_NACL_PARAM(MaxBurstLength);
613 NACL_PARAM_ATTR(MaxBurstLength);
614
615 DEF_NACL_PARAM(FirstBurstLength);
616 NACL_PARAM_ATTR(FirstBurstLength);
617
618 DEF_NACL_PARAM(DefaultTime2Wait);
619 NACL_PARAM_ATTR(DefaultTime2Wait);
620
621 DEF_NACL_PARAM(DefaultTime2Retain);
622 NACL_PARAM_ATTR(DefaultTime2Retain);
623
624 DEF_NACL_PARAM(MaxOutstandingR2T);
625 NACL_PARAM_ATTR(MaxOutstandingR2T);
626
627 DEF_NACL_PARAM(DataPDUInOrder);
628 NACL_PARAM_ATTR(DataPDUInOrder);
629
630 DEF_NACL_PARAM(DataSequenceInOrder);
631 NACL_PARAM_ATTR(DataSequenceInOrder);
632
633 DEF_NACL_PARAM(ErrorRecoveryLevel);
634 NACL_PARAM_ATTR(ErrorRecoveryLevel);
635
636 static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
637         &iscsi_nacl_param_MaxConnections.attr,
638         &iscsi_nacl_param_InitialR2T.attr,
639         &iscsi_nacl_param_ImmediateData.attr,
640         &iscsi_nacl_param_MaxBurstLength.attr,
641         &iscsi_nacl_param_FirstBurstLength.attr,
642         &iscsi_nacl_param_DefaultTime2Wait.attr,
643         &iscsi_nacl_param_DefaultTime2Retain.attr,
644         &iscsi_nacl_param_MaxOutstandingR2T.attr,
645         &iscsi_nacl_param_DataPDUInOrder.attr,
646         &iscsi_nacl_param_DataSequenceInOrder.attr,
647         &iscsi_nacl_param_ErrorRecoveryLevel.attr,
648         NULL,
649 };
650
651 /* End items for lio_target_nacl_param_cit */
652
653 /* Start items for lio_target_acl_cit */
654
655 static ssize_t lio_target_nacl_show_info(
656         struct se_node_acl *se_nacl,
657         char *page)
658 {
659         struct iscsi_session *sess;
660         struct iscsi_conn *conn;
661         struct se_session *se_sess;
662         ssize_t rb = 0;
663
664         spin_lock_bh(&se_nacl->nacl_sess_lock);
665         se_sess = se_nacl->nacl_sess;
666         if (!se_sess) {
667                 rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
668                         " Endpoint: %s\n", se_nacl->initiatorname);
669         } else {
670                 sess = se_sess->fabric_sess_ptr;
671
672                 if (sess->sess_ops->InitiatorName)
673                         rb += sprintf(page+rb, "InitiatorName: %s\n",
674                                 sess->sess_ops->InitiatorName);
675                 if (sess->sess_ops->InitiatorAlias)
676                         rb += sprintf(page+rb, "InitiatorAlias: %s\n",
677                                 sess->sess_ops->InitiatorAlias);
678
679                 rb += sprintf(page+rb, "LIO Session ID: %u   "
680                         "ISID: 0x%02x %02x %02x %02x %02x %02x  "
681                         "TSIH: %hu  ", sess->sid,
682                         sess->isid[0], sess->isid[1], sess->isid[2],
683                         sess->isid[3], sess->isid[4], sess->isid[5],
684                         sess->tsih);
685                 rb += sprintf(page+rb, "SessionType: %s\n",
686                                 (sess->sess_ops->SessionType) ?
687                                 "Discovery" : "Normal");
688                 rb += sprintf(page+rb, "Session State: ");
689                 switch (sess->session_state) {
690                 case TARG_SESS_STATE_FREE:
691                         rb += sprintf(page+rb, "TARG_SESS_FREE\n");
692                         break;
693                 case TARG_SESS_STATE_ACTIVE:
694                         rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
695                         break;
696                 case TARG_SESS_STATE_LOGGED_IN:
697                         rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
698                         break;
699                 case TARG_SESS_STATE_FAILED:
700                         rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
701                         break;
702                 case TARG_SESS_STATE_IN_CONTINUE:
703                         rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
704                         break;
705                 default:
706                         rb += sprintf(page+rb, "ERROR: Unknown Session"
707                                         " State!\n");
708                         break;
709                 }
710
711                 rb += sprintf(page+rb, "---------------------[iSCSI Session"
712                                 " Values]-----------------------\n");
713                 rb += sprintf(page+rb, "  CmdSN/WR  :  CmdSN/WC  :  ExpCmdSN"
714                                 "  :  MaxCmdSN  :     ITT    :     TTT\n");
715                 rb += sprintf(page+rb, " 0x%08x   0x%08x   0x%08x   0x%08x"
716                                 "   0x%08x   0x%08x\n",
717                         sess->cmdsn_window,
718                         (sess->max_cmd_sn - sess->exp_cmd_sn) + 1,
719                         sess->exp_cmd_sn, sess->max_cmd_sn,
720                         sess->init_task_tag, sess->targ_xfer_tag);
721                 rb += sprintf(page+rb, "----------------------[iSCSI"
722                                 " Connections]-------------------------\n");
723
724                 spin_lock(&sess->conn_lock);
725                 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
726                         rb += sprintf(page+rb, "CID: %hu  Connection"
727                                         " State: ", conn->cid);
728                         switch (conn->conn_state) {
729                         case TARG_CONN_STATE_FREE:
730                                 rb += sprintf(page+rb,
731                                         "TARG_CONN_STATE_FREE\n");
732                                 break;
733                         case TARG_CONN_STATE_XPT_UP:
734                                 rb += sprintf(page+rb,
735                                         "TARG_CONN_STATE_XPT_UP\n");
736                                 break;
737                         case TARG_CONN_STATE_IN_LOGIN:
738                                 rb += sprintf(page+rb,
739                                         "TARG_CONN_STATE_IN_LOGIN\n");
740                                 break;
741                         case TARG_CONN_STATE_LOGGED_IN:
742                                 rb += sprintf(page+rb,
743                                         "TARG_CONN_STATE_LOGGED_IN\n");
744                                 break;
745                         case TARG_CONN_STATE_IN_LOGOUT:
746                                 rb += sprintf(page+rb,
747                                         "TARG_CONN_STATE_IN_LOGOUT\n");
748                                 break;
749                         case TARG_CONN_STATE_LOGOUT_REQUESTED:
750                                 rb += sprintf(page+rb,
751                                         "TARG_CONN_STATE_LOGOUT_REQUESTED\n");
752                                 break;
753                         case TARG_CONN_STATE_CLEANUP_WAIT:
754                                 rb += sprintf(page+rb,
755                                         "TARG_CONN_STATE_CLEANUP_WAIT\n");
756                                 break;
757                         default:
758                                 rb += sprintf(page+rb,
759                                         "ERROR: Unknown Connection State!\n");
760                                 break;
761                         }
762
763                         rb += sprintf(page+rb, "   Address %s %s", conn->login_ip,
764                                 (conn->network_transport == ISCSI_TCP) ?
765                                 "TCP" : "SCTP");
766                         rb += sprintf(page+rb, "  StatSN: 0x%08x\n",
767                                 conn->stat_sn);
768                 }
769                 spin_unlock(&sess->conn_lock);
770         }
771         spin_unlock_bh(&se_nacl->nacl_sess_lock);
772
773         return rb;
774 }
775
776 TF_NACL_BASE_ATTR_RO(lio_target, info);
777
778 static ssize_t lio_target_nacl_show_cmdsn_depth(
779         struct se_node_acl *se_nacl,
780         char *page)
781 {
782         return sprintf(page, "%u\n", se_nacl->queue_depth);
783 }
784
785 static ssize_t lio_target_nacl_store_cmdsn_depth(
786         struct se_node_acl *se_nacl,
787         const char *page,
788         size_t count)
789 {
790         struct se_portal_group *se_tpg = se_nacl->se_tpg;
791         struct iscsi_portal_group *tpg = container_of(se_tpg,
792                         struct iscsi_portal_group, tpg_se_tpg);
793         struct config_item *acl_ci, *tpg_ci, *wwn_ci;
794         u32 cmdsn_depth = 0;
795         int ret;
796
797         ret = kstrtou32(page, 0, &cmdsn_depth);
798         if (ret)
799                 return ret;
800         if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
801                 pr_err("Passed cmdsn_depth: %u exceeds"
802                         " TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
803                         TA_DEFAULT_CMDSN_DEPTH_MAX);
804                 return -EINVAL;
805         }
806         acl_ci = &se_nacl->acl_group.cg_item;
807         if (!acl_ci) {
808                 pr_err("Unable to locatel acl_ci\n");
809                 return -EINVAL;
810         }
811         tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
812         if (!tpg_ci) {
813                 pr_err("Unable to locate tpg_ci\n");
814                 return -EINVAL;
815         }
816         wwn_ci = &tpg_ci->ci_group->cg_item;
817         if (!wwn_ci) {
818                 pr_err("Unable to locate config_item wwn_ci\n");
819                 return -EINVAL;
820         }
821
822         if (iscsit_get_tpg(tpg) < 0)
823                 return -EINVAL;
824         /*
825          * iscsit_tpg_set_initiator_node_queue_depth() assumes force=1
826          */
827         ret = iscsit_tpg_set_initiator_node_queue_depth(tpg,
828                                 config_item_name(acl_ci), cmdsn_depth, 1);
829
830         pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
831                 "InitiatorName: %s\n", config_item_name(wwn_ci),
832                 config_item_name(tpg_ci), cmdsn_depth,
833                 config_item_name(acl_ci));
834
835         iscsit_put_tpg(tpg);
836         return (!ret) ? count : (ssize_t)ret;
837 }
838
839 TF_NACL_BASE_ATTR(lio_target, cmdsn_depth, S_IRUGO | S_IWUSR);
840
841 static ssize_t lio_target_nacl_show_tag(
842         struct se_node_acl *se_nacl,
843         char *page)
844 {
845         return snprintf(page, PAGE_SIZE, "%s", se_nacl->acl_tag);
846 }
847
848 static ssize_t lio_target_nacl_store_tag(
849         struct se_node_acl *se_nacl,
850         const char *page,
851         size_t count)
852 {
853         int ret;
854
855         ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
856
857         if (ret < 0)
858                 return ret;
859         return count;
860 }
861
862 TF_NACL_BASE_ATTR(lio_target, tag, S_IRUGO | S_IWUSR);
863
864 static struct configfs_attribute *lio_target_initiator_attrs[] = {
865         &lio_target_nacl_info.attr,
866         &lio_target_nacl_cmdsn_depth.attr,
867         &lio_target_nacl_tag.attr,
868         NULL,
869 };
870
871 static struct se_node_acl *lio_tpg_alloc_fabric_acl(
872         struct se_portal_group *se_tpg)
873 {
874         struct iscsi_node_acl *acl;
875
876         acl = kzalloc(sizeof(struct iscsi_node_acl), GFP_KERNEL);
877         if (!acl) {
878                 pr_err("Unable to allocate memory for struct iscsi_node_acl\n");
879                 return NULL;
880         }
881
882         return &acl->se_node_acl;
883 }
884
885 static struct se_node_acl *lio_target_make_nodeacl(
886         struct se_portal_group *se_tpg,
887         struct config_group *group,
888         const char *name)
889 {
890         struct config_group *stats_cg;
891         struct iscsi_node_acl *acl;
892         struct se_node_acl *se_nacl_new, *se_nacl;
893         struct iscsi_portal_group *tpg = container_of(se_tpg,
894                         struct iscsi_portal_group, tpg_se_tpg);
895         u32 cmdsn_depth;
896
897         se_nacl_new = lio_tpg_alloc_fabric_acl(se_tpg);
898         if (!se_nacl_new)
899                 return ERR_PTR(-ENOMEM);
900
901         cmdsn_depth = tpg->tpg_attrib.default_cmdsn_depth;
902         /*
903          * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
904          * when converting a NdoeACL from demo mode -> explict
905          */
906         se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
907                                 name, cmdsn_depth);
908         if (IS_ERR(se_nacl))
909                 return se_nacl;
910
911         acl = container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
912         stats_cg = &se_nacl->acl_fabric_stat_group;
913
914         stats_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
915                                 GFP_KERNEL);
916         if (!stats_cg->default_groups) {
917                 pr_err("Unable to allocate memory for"
918                                 " stats_cg->default_groups\n");
919                 core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
920                 kfree(acl);
921                 return ERR_PTR(-ENOMEM);
922         }
923
924         stats_cg->default_groups[0] = &acl->node_stat_grps.iscsi_sess_stats_group;
925         stats_cg->default_groups[1] = NULL;
926         config_group_init_type_name(&acl->node_stat_grps.iscsi_sess_stats_group,
927                         "iscsi_sess_stats", &iscsi_stat_sess_cit);
928
929         return se_nacl;
930 }
931
932 static void lio_target_drop_nodeacl(
933         struct se_node_acl *se_nacl)
934 {
935         struct se_portal_group *se_tpg = se_nacl->se_tpg;
936         struct iscsi_node_acl *acl = container_of(se_nacl,
937                         struct iscsi_node_acl, se_node_acl);
938         struct config_item *df_item;
939         struct config_group *stats_cg;
940         int i;
941
942         stats_cg = &acl->se_node_acl.acl_fabric_stat_group;
943         for (i = 0; stats_cg->default_groups[i]; i++) {
944                 df_item = &stats_cg->default_groups[i]->cg_item;
945                 stats_cg->default_groups[i] = NULL;
946                 config_item_put(df_item);
947         }
948         kfree(stats_cg->default_groups);
949
950         core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
951         kfree(acl);
952 }
953
954 /* End items for lio_target_acl_cit */
955
956 /* Start items for lio_target_tpg_attrib_cit */
957
958 #define DEF_TPG_ATTRIB(name)                                            \
959                                                                         \
960 static ssize_t iscsi_tpg_attrib_show_##name(                            \
961         struct se_portal_group *se_tpg,                         \
962         char *page)                                                     \
963 {                                                                       \
964         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
965                         struct iscsi_portal_group, tpg_se_tpg); \
966         ssize_t rb;                                                     \
967                                                                         \
968         if (iscsit_get_tpg(tpg) < 0)                                    \
969                 return -EINVAL;                                         \
970                                                                         \
971         rb = sprintf(page, "%u\n", tpg->tpg_attrib.name);               \
972         iscsit_put_tpg(tpg);                                            \
973         return rb;                                                      \
974 }                                                                       \
975                                                                         \
976 static ssize_t iscsi_tpg_attrib_store_##name(                           \
977         struct se_portal_group *se_tpg,                         \
978         const char *page,                                               \
979         size_t count)                                                   \
980 {                                                                       \
981         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
982                         struct iscsi_portal_group, tpg_se_tpg); \
983         u32 val;                                                        \
984         int ret;                                                        \
985                                                                         \
986         if (iscsit_get_tpg(tpg) < 0)                                    \
987                 return -EINVAL;                                         \
988                                                                         \
989         ret = kstrtou32(page, 0, &val);                                 \
990         if (ret)                                                        \
991                 goto out;                                               \
992         ret = iscsit_ta_##name(tpg, val);                               \
993         if (ret < 0)                                                    \
994                 goto out;                                               \
995                                                                         \
996         iscsit_put_tpg(tpg);                                            \
997         return count;                                                   \
998 out:                                                                    \
999         iscsit_put_tpg(tpg);                                            \
1000         return ret;                                                     \
1001 }
1002
1003 #define TPG_ATTR(_name, _mode) TF_TPG_ATTRIB_ATTR(iscsi, _name, _mode);
1004
1005 /*
1006  * Define iscsi_tpg_attrib_s_authentication
1007  */
1008 DEF_TPG_ATTRIB(authentication);
1009 TPG_ATTR(authentication, S_IRUGO | S_IWUSR);
1010 /*
1011  * Define iscsi_tpg_attrib_s_login_timeout
1012  */
1013 DEF_TPG_ATTRIB(login_timeout);
1014 TPG_ATTR(login_timeout, S_IRUGO | S_IWUSR);
1015 /*
1016  * Define iscsi_tpg_attrib_s_netif_timeout
1017  */
1018 DEF_TPG_ATTRIB(netif_timeout);
1019 TPG_ATTR(netif_timeout, S_IRUGO | S_IWUSR);
1020 /*
1021  * Define iscsi_tpg_attrib_s_generate_node_acls
1022  */
1023 DEF_TPG_ATTRIB(generate_node_acls);
1024 TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
1025 /*
1026  * Define iscsi_tpg_attrib_s_default_cmdsn_depth
1027  */
1028 DEF_TPG_ATTRIB(default_cmdsn_depth);
1029 TPG_ATTR(default_cmdsn_depth, S_IRUGO | S_IWUSR);
1030 /*
1031  Define iscsi_tpg_attrib_s_cache_dynamic_acls
1032  */
1033 DEF_TPG_ATTRIB(cache_dynamic_acls);
1034 TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
1035 /*
1036  * Define iscsi_tpg_attrib_s_demo_mode_write_protect
1037  */
1038 DEF_TPG_ATTRIB(demo_mode_write_protect);
1039 TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
1040 /*
1041  * Define iscsi_tpg_attrib_s_prod_mode_write_protect
1042  */
1043 DEF_TPG_ATTRIB(prod_mode_write_protect);
1044 TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
1045 /*
1046  * Define iscsi_tpg_attrib_s_demo_mode_discovery,
1047  */
1048 DEF_TPG_ATTRIB(demo_mode_discovery);
1049 TPG_ATTR(demo_mode_discovery, S_IRUGO | S_IWUSR);
1050 /*
1051  * Define iscsi_tpg_attrib_s_default_erl
1052  */
1053 DEF_TPG_ATTRIB(default_erl);
1054 TPG_ATTR(default_erl, S_IRUGO | S_IWUSR);
1055 /*
1056  * Define iscsi_tpg_attrib_s_t10_pi
1057  */
1058 DEF_TPG_ATTRIB(t10_pi);
1059 TPG_ATTR(t10_pi, S_IRUGO | S_IWUSR);
1060 DEF_TPG_ATTRIB(login_keys_workaround);
1061 TPG_ATTR(login_keys_workaround, S_IRUGO | S_IWUSR);
1062
1063 static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
1064         &iscsi_tpg_attrib_authentication.attr,
1065         &iscsi_tpg_attrib_login_timeout.attr,
1066         &iscsi_tpg_attrib_netif_timeout.attr,
1067         &iscsi_tpg_attrib_generate_node_acls.attr,
1068         &iscsi_tpg_attrib_default_cmdsn_depth.attr,
1069         &iscsi_tpg_attrib_cache_dynamic_acls.attr,
1070         &iscsi_tpg_attrib_demo_mode_write_protect.attr,
1071         &iscsi_tpg_attrib_prod_mode_write_protect.attr,
1072         &iscsi_tpg_attrib_demo_mode_discovery.attr,
1073         &iscsi_tpg_attrib_default_erl.attr,
1074         &iscsi_tpg_attrib_t10_pi.attr,
1075         &iscsi_tpg_attrib_login_keys_workaround.attr,
1076         NULL,
1077 };
1078
1079 /* End items for lio_target_tpg_attrib_cit */
1080
1081 /* Start items for lio_target_tpg_auth_cit */
1082
1083 #define __DEF_TPG_AUTH_STR(prefix, name, flags)                                 \
1084 static ssize_t __iscsi_##prefix##_show_##name(                                  \
1085         struct se_portal_group *se_tpg,                                         \
1086         char *page)                                                             \
1087 {                                                                               \
1088         struct iscsi_portal_group *tpg = container_of(se_tpg,                   \
1089                                 struct iscsi_portal_group, tpg_se_tpg);         \
1090         struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;                     \
1091                                                                                 \
1092         if (!capable(CAP_SYS_ADMIN))                                            \
1093                 return -EPERM;                                                  \
1094                                                                                 \
1095         return snprintf(page, PAGE_SIZE, "%s\n", auth->name);                   \
1096 }                                                                               \
1097                                                                                 \
1098 static ssize_t __iscsi_##prefix##_store_##name(                                 \
1099         struct se_portal_group *se_tpg,                                         \
1100         const char *page,                                                       \
1101         size_t count)                                                           \
1102 {                                                                               \
1103         struct iscsi_portal_group *tpg = container_of(se_tpg,                   \
1104                                 struct iscsi_portal_group, tpg_se_tpg);         \
1105         struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;                     \
1106                                                                                 \
1107         if (!capable(CAP_SYS_ADMIN))                                            \
1108                 return -EPERM;                                                  \
1109                                                                                 \
1110         snprintf(auth->name, sizeof(auth->name), "%s", page);                   \
1111         if (!(strncmp("NULL", auth->name, 4)))                                  \
1112                 auth->naf_flags &= ~flags;                                      \
1113         else                                                                    \
1114                 auth->naf_flags |= flags;                                       \
1115                                                                                 \
1116         if ((auth->naf_flags & NAF_USERID_IN_SET) &&                            \
1117             (auth->naf_flags & NAF_PASSWORD_IN_SET))                            \
1118                 auth->authenticate_target = 1;                                  \
1119         else                                                                    \
1120                 auth->authenticate_target = 0;                                  \
1121                                                                                 \
1122         return count;                                                           \
1123 }
1124
1125 #define __DEF_TPG_AUTH_INT(prefix, name)                                        \
1126 static ssize_t __iscsi_##prefix##_show_##name(                                  \
1127         struct se_portal_group *se_tpg,                                         \
1128         char *page)                                                             \
1129 {                                                                               \
1130         struct iscsi_portal_group *tpg = container_of(se_tpg,                   \
1131                                 struct iscsi_portal_group, tpg_se_tpg);         \
1132         struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;                     \
1133                                                                                 \
1134         if (!capable(CAP_SYS_ADMIN))                                            \
1135                 return -EPERM;                                                  \
1136                                                                                 \
1137         return snprintf(page, PAGE_SIZE, "%d\n", auth->name);                   \
1138 }
1139
1140 #define DEF_TPG_AUTH_STR(name, flags)                                           \
1141         __DEF_TPG_AUTH_STR(tpg_auth, name, flags)                               \
1142 static ssize_t iscsi_tpg_auth_show_##name(                                      \
1143         struct se_portal_group *se_tpg,                                         \
1144         char *page)                                                             \
1145 {                                                                               \
1146         return __iscsi_tpg_auth_show_##name(se_tpg, page);                      \
1147 }                                                                               \
1148                                                                                 \
1149 static ssize_t iscsi_tpg_auth_store_##name(                                     \
1150         struct se_portal_group *se_tpg,                                         \
1151         const char *page,                                                       \
1152         size_t count)                                                           \
1153 {                                                                               \
1154         return __iscsi_tpg_auth_store_##name(se_tpg, page, count);              \
1155 }
1156
1157 #define DEF_TPG_AUTH_INT(name)                                                  \
1158         __DEF_TPG_AUTH_INT(tpg_auth, name)                                      \
1159 static ssize_t iscsi_tpg_auth_show_##name(                                      \
1160         struct se_portal_group *se_tpg,                                         \
1161         char *page)                                                             \
1162 {                                                                               \
1163         return __iscsi_tpg_auth_show_##name(se_tpg, page);                      \
1164 }
1165
1166 #define TPG_AUTH_ATTR(_name, _mode) TF_TPG_AUTH_ATTR(iscsi, _name, _mode);
1167 #define TPG_AUTH_ATTR_RO(_name) TF_TPG_AUTH_ATTR_RO(iscsi, _name);
1168
1169 /*
1170  *  * One-way authentication userid
1171  *   */
1172 DEF_TPG_AUTH_STR(userid, NAF_USERID_SET);
1173 TPG_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
1174 /*
1175  *  * One-way authentication password
1176  *   */
1177 DEF_TPG_AUTH_STR(password, NAF_PASSWORD_SET);
1178 TPG_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
1179 /*
1180  *  * Enforce mutual authentication
1181  *   */
1182 DEF_TPG_AUTH_INT(authenticate_target);
1183 TPG_AUTH_ATTR_RO(authenticate_target);
1184 /*
1185  *  * Mutual authentication userid
1186  *   */
1187 DEF_TPG_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1188 TPG_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
1189 /*
1190  *  * Mutual authentication password
1191  *   */
1192 DEF_TPG_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1193 TPG_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
1194
1195 static struct configfs_attribute *lio_target_tpg_auth_attrs[] = {
1196         &iscsi_tpg_auth_userid.attr,
1197         &iscsi_tpg_auth_password.attr,
1198         &iscsi_tpg_auth_authenticate_target.attr,
1199         &iscsi_tpg_auth_userid_mutual.attr,
1200         &iscsi_tpg_auth_password_mutual.attr,
1201         NULL,
1202 };
1203
1204 /* End items for lio_target_tpg_auth_cit */
1205
1206 /* Start items for lio_target_tpg_param_cit */
1207
1208 #define DEF_TPG_PARAM(name)                                             \
1209 static ssize_t iscsi_tpg_param_show_##name(                             \
1210         struct se_portal_group *se_tpg,                                 \
1211         char *page)                                                     \
1212 {                                                                       \
1213         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
1214                         struct iscsi_portal_group, tpg_se_tpg);         \
1215         struct iscsi_param *param;                                      \
1216         ssize_t rb;                                                     \
1217                                                                         \
1218         if (iscsit_get_tpg(tpg) < 0)                                    \
1219                 return -EINVAL;                                         \
1220                                                                         \
1221         param = iscsi_find_param_from_key(__stringify(name),            \
1222                                 tpg->param_list);                       \
1223         if (!param) {                                                   \
1224                 iscsit_put_tpg(tpg);                                    \
1225                 return -EINVAL;                                         \
1226         }                                                               \
1227         rb = snprintf(page, PAGE_SIZE, "%s\n", param->value);           \
1228                                                                         \
1229         iscsit_put_tpg(tpg);                                            \
1230         return rb;                                                      \
1231 }                                                                       \
1232 static ssize_t iscsi_tpg_param_store_##name(                            \
1233         struct se_portal_group *se_tpg,                         \
1234         const char *page,                                               \
1235         size_t count)                                                   \
1236 {                                                                       \
1237         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
1238                         struct iscsi_portal_group, tpg_se_tpg);         \
1239         char *buf;                                                      \
1240         int ret, len;                                                   \
1241                                                                         \
1242         buf = kzalloc(PAGE_SIZE, GFP_KERNEL);                           \
1243         if (!buf)                                                       \
1244                 return -ENOMEM;                                         \
1245         len = snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page);       \
1246         if (isspace(buf[len-1]))                                        \
1247                 buf[len-1] = '\0'; /* Kill newline */                   \
1248                                                                         \
1249         if (iscsit_get_tpg(tpg) < 0) {                                  \
1250                 kfree(buf);                                             \
1251                 return -EINVAL;                                         \
1252         }                                                               \
1253                                                                         \
1254         ret = iscsi_change_param_value(buf, tpg->param_list, 1);        \
1255         if (ret < 0)                                                    \
1256                 goto out;                                               \
1257                                                                         \
1258         kfree(buf);                                                     \
1259         iscsit_put_tpg(tpg);                                            \
1260         return count;                                                   \
1261 out:                                                                    \
1262         kfree(buf);                                                     \
1263         iscsit_put_tpg(tpg);                                            \
1264         return -EINVAL;                                         \
1265 }
1266
1267 #define TPG_PARAM_ATTR(_name, _mode) TF_TPG_PARAM_ATTR(iscsi, _name, _mode);
1268
1269 DEF_TPG_PARAM(AuthMethod);
1270 TPG_PARAM_ATTR(AuthMethod, S_IRUGO | S_IWUSR);
1271
1272 DEF_TPG_PARAM(HeaderDigest);
1273 TPG_PARAM_ATTR(HeaderDigest, S_IRUGO | S_IWUSR);
1274
1275 DEF_TPG_PARAM(DataDigest);
1276 TPG_PARAM_ATTR(DataDigest, S_IRUGO | S_IWUSR);
1277
1278 DEF_TPG_PARAM(MaxConnections);
1279 TPG_PARAM_ATTR(MaxConnections, S_IRUGO | S_IWUSR);
1280
1281 DEF_TPG_PARAM(TargetAlias);
1282 TPG_PARAM_ATTR(TargetAlias, S_IRUGO | S_IWUSR);
1283
1284 DEF_TPG_PARAM(InitialR2T);
1285 TPG_PARAM_ATTR(InitialR2T, S_IRUGO | S_IWUSR);
1286
1287 DEF_TPG_PARAM(ImmediateData);
1288 TPG_PARAM_ATTR(ImmediateData, S_IRUGO | S_IWUSR);
1289
1290 DEF_TPG_PARAM(MaxRecvDataSegmentLength);
1291 TPG_PARAM_ATTR(MaxRecvDataSegmentLength, S_IRUGO | S_IWUSR);
1292
1293 DEF_TPG_PARAM(MaxXmitDataSegmentLength);
1294 TPG_PARAM_ATTR(MaxXmitDataSegmentLength, S_IRUGO | S_IWUSR);
1295
1296 DEF_TPG_PARAM(MaxBurstLength);
1297 TPG_PARAM_ATTR(MaxBurstLength, S_IRUGO | S_IWUSR);
1298
1299 DEF_TPG_PARAM(FirstBurstLength);
1300 TPG_PARAM_ATTR(FirstBurstLength, S_IRUGO | S_IWUSR);
1301
1302 DEF_TPG_PARAM(DefaultTime2Wait);
1303 TPG_PARAM_ATTR(DefaultTime2Wait, S_IRUGO | S_IWUSR);
1304
1305 DEF_TPG_PARAM(DefaultTime2Retain);
1306 TPG_PARAM_ATTR(DefaultTime2Retain, S_IRUGO | S_IWUSR);
1307
1308 DEF_TPG_PARAM(MaxOutstandingR2T);
1309 TPG_PARAM_ATTR(MaxOutstandingR2T, S_IRUGO | S_IWUSR);
1310
1311 DEF_TPG_PARAM(DataPDUInOrder);
1312 TPG_PARAM_ATTR(DataPDUInOrder, S_IRUGO | S_IWUSR);
1313
1314 DEF_TPG_PARAM(DataSequenceInOrder);
1315 TPG_PARAM_ATTR(DataSequenceInOrder, S_IRUGO | S_IWUSR);
1316
1317 DEF_TPG_PARAM(ErrorRecoveryLevel);
1318 TPG_PARAM_ATTR(ErrorRecoveryLevel, S_IRUGO | S_IWUSR);
1319
1320 DEF_TPG_PARAM(IFMarker);
1321 TPG_PARAM_ATTR(IFMarker, S_IRUGO | S_IWUSR);
1322
1323 DEF_TPG_PARAM(OFMarker);
1324 TPG_PARAM_ATTR(OFMarker, S_IRUGO | S_IWUSR);
1325
1326 DEF_TPG_PARAM(IFMarkInt);
1327 TPG_PARAM_ATTR(IFMarkInt, S_IRUGO | S_IWUSR);
1328
1329 DEF_TPG_PARAM(OFMarkInt);
1330 TPG_PARAM_ATTR(OFMarkInt, S_IRUGO | S_IWUSR);
1331
1332 static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
1333         &iscsi_tpg_param_AuthMethod.attr,
1334         &iscsi_tpg_param_HeaderDigest.attr,
1335         &iscsi_tpg_param_DataDigest.attr,
1336         &iscsi_tpg_param_MaxConnections.attr,
1337         &iscsi_tpg_param_TargetAlias.attr,
1338         &iscsi_tpg_param_InitialR2T.attr,
1339         &iscsi_tpg_param_ImmediateData.attr,
1340         &iscsi_tpg_param_MaxRecvDataSegmentLength.attr,
1341         &iscsi_tpg_param_MaxXmitDataSegmentLength.attr,
1342         &iscsi_tpg_param_MaxBurstLength.attr,
1343         &iscsi_tpg_param_FirstBurstLength.attr,
1344         &iscsi_tpg_param_DefaultTime2Wait.attr,
1345         &iscsi_tpg_param_DefaultTime2Retain.attr,
1346         &iscsi_tpg_param_MaxOutstandingR2T.attr,
1347         &iscsi_tpg_param_DataPDUInOrder.attr,
1348         &iscsi_tpg_param_DataSequenceInOrder.attr,
1349         &iscsi_tpg_param_ErrorRecoveryLevel.attr,
1350         &iscsi_tpg_param_IFMarker.attr,
1351         &iscsi_tpg_param_OFMarker.attr,
1352         &iscsi_tpg_param_IFMarkInt.attr,
1353         &iscsi_tpg_param_OFMarkInt.attr,
1354         NULL,
1355 };
1356
1357 /* End items for lio_target_tpg_param_cit */
1358
1359 /* Start items for lio_target_tpg_cit */
1360
1361 static ssize_t lio_target_tpg_show_enable(
1362         struct se_portal_group *se_tpg,
1363         char *page)
1364 {
1365         struct iscsi_portal_group *tpg = container_of(se_tpg,
1366                         struct iscsi_portal_group, tpg_se_tpg);
1367         ssize_t len;
1368
1369         spin_lock(&tpg->tpg_state_lock);
1370         len = sprintf(page, "%d\n",
1371                         (tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1372         spin_unlock(&tpg->tpg_state_lock);
1373
1374         return len;
1375 }
1376
1377 static ssize_t lio_target_tpg_store_enable(
1378         struct se_portal_group *se_tpg,
1379         const char *page,
1380         size_t count)
1381 {
1382         struct iscsi_portal_group *tpg = container_of(se_tpg,
1383                         struct iscsi_portal_group, tpg_se_tpg);
1384         u32 op;
1385         int ret;
1386
1387         ret = kstrtou32(page, 0, &op);
1388         if (ret)
1389                 return ret;
1390         if ((op != 1) && (op != 0)) {
1391                 pr_err("Illegal value for tpg_enable: %u\n", op);
1392                 return -EINVAL;
1393         }
1394
1395         ret = iscsit_get_tpg(tpg);
1396         if (ret < 0)
1397                 return -EINVAL;
1398
1399         if (op) {
1400                 ret = iscsit_tpg_enable_portal_group(tpg);
1401                 if (ret < 0)
1402                         goto out;
1403         } else {
1404                 /*
1405                  * iscsit_tpg_disable_portal_group() assumes force=1
1406                  */
1407                 ret = iscsit_tpg_disable_portal_group(tpg, 1);
1408                 if (ret < 0)
1409                         goto out;
1410         }
1411
1412         iscsit_put_tpg(tpg);
1413         return count;
1414 out:
1415         iscsit_put_tpg(tpg);
1416         return -EINVAL;
1417 }
1418
1419 TF_TPG_BASE_ATTR(lio_target, enable, S_IRUGO | S_IWUSR);
1420
1421 static struct configfs_attribute *lio_target_tpg_attrs[] = {
1422         &lio_target_tpg_enable.attr,
1423         NULL,
1424 };
1425
1426 /* End items for lio_target_tpg_cit */
1427
1428 /* Start items for lio_target_tiqn_cit */
1429
1430 static struct se_portal_group *lio_target_tiqn_addtpg(
1431         struct se_wwn *wwn,
1432         struct config_group *group,
1433         const char *name)
1434 {
1435         struct iscsi_portal_group *tpg;
1436         struct iscsi_tiqn *tiqn;
1437         char *tpgt_str;
1438         int ret;
1439         u16 tpgt;
1440
1441         tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1442         /*
1443          * Only tpgt_# directory groups can be created below
1444          * target/iscsi/iqn.superturodiskarry/
1445          */
1446         tpgt_str = strstr(name, "tpgt_");
1447         if (!tpgt_str) {
1448                 pr_err("Unable to locate \"tpgt_#\" directory"
1449                                 " group\n");
1450                 return NULL;
1451         }
1452         tpgt_str += 5; /* Skip ahead of "tpgt_" */
1453         ret = kstrtou16(tpgt_str, 0, &tpgt);
1454         if (ret)
1455                 return NULL;
1456
1457         tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1458         if (!tpg)
1459                 return NULL;
1460
1461         ret = core_tpg_register(
1462                         &lio_target_fabric_configfs->tf_ops,
1463                         wwn, &tpg->tpg_se_tpg, tpg,
1464                         TRANSPORT_TPG_TYPE_NORMAL);
1465         if (ret < 0)
1466                 return NULL;
1467
1468         ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1469         if (ret != 0)
1470                 goto out;
1471
1472         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1473         pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1474                         name);
1475         return &tpg->tpg_se_tpg;
1476 out:
1477         core_tpg_deregister(&tpg->tpg_se_tpg);
1478         kfree(tpg);
1479         return NULL;
1480 }
1481
1482 static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1483 {
1484         struct iscsi_portal_group *tpg;
1485         struct iscsi_tiqn *tiqn;
1486
1487         tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1488         tiqn = tpg->tpg_tiqn;
1489         /*
1490          * iscsit_tpg_del_portal_group() assumes force=1
1491          */
1492         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1493         iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1494 }
1495
1496 /* End items for lio_target_tiqn_cit */
1497
1498 /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1499
1500 static ssize_t lio_target_wwn_show_attr_lio_version(
1501         struct target_fabric_configfs *tf,
1502         char *page)
1503 {
1504         return sprintf(page, "Datera Inc. iSCSI Target "ISCSIT_VERSION"\n");
1505 }
1506
1507 TF_WWN_ATTR_RO(lio_target, lio_version);
1508
1509 static struct configfs_attribute *lio_target_wwn_attrs[] = {
1510         &lio_target_wwn_lio_version.attr,
1511         NULL,
1512 };
1513
1514 static struct se_wwn *lio_target_call_coreaddtiqn(
1515         struct target_fabric_configfs *tf,
1516         struct config_group *group,
1517         const char *name)
1518 {
1519         struct config_group *stats_cg;
1520         struct iscsi_tiqn *tiqn;
1521
1522         tiqn = iscsit_add_tiqn((unsigned char *)name);
1523         if (IS_ERR(tiqn))
1524                 return ERR_CAST(tiqn);
1525         /*
1526          * Setup struct iscsi_wwn_stat_grps for se_wwn->fabric_stat_group.
1527          */
1528         stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1529
1530         stats_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
1531                                 GFP_KERNEL);
1532         if (!stats_cg->default_groups) {
1533                 pr_err("Unable to allocate memory for"
1534                                 " stats_cg->default_groups\n");
1535                 iscsit_del_tiqn(tiqn);
1536                 return ERR_PTR(-ENOMEM);
1537         }
1538
1539         stats_cg->default_groups[0] = &tiqn->tiqn_stat_grps.iscsi_instance_group;
1540         stats_cg->default_groups[1] = &tiqn->tiqn_stat_grps.iscsi_sess_err_group;
1541         stats_cg->default_groups[2] = &tiqn->tiqn_stat_grps.iscsi_tgt_attr_group;
1542         stats_cg->default_groups[3] = &tiqn->tiqn_stat_grps.iscsi_login_stats_group;
1543         stats_cg->default_groups[4] = &tiqn->tiqn_stat_grps.iscsi_logout_stats_group;
1544         stats_cg->default_groups[5] = NULL;
1545         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1546                         "iscsi_instance", &iscsi_stat_instance_cit);
1547         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1548                         "iscsi_sess_err", &iscsi_stat_sess_err_cit);
1549         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1550                         "iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
1551         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1552                         "iscsi_login_stats", &iscsi_stat_login_cit);
1553         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1554                         "iscsi_logout_stats", &iscsi_stat_logout_cit);
1555
1556         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1557         pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1558                         " %s\n", name);
1559         return &tiqn->tiqn_wwn;
1560 }
1561
1562 static void lio_target_call_coredeltiqn(
1563         struct se_wwn *wwn)
1564 {
1565         struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1566         struct config_item *df_item;
1567         struct config_group *stats_cg;
1568         int i;
1569
1570         stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1571         for (i = 0; stats_cg->default_groups[i]; i++) {
1572                 df_item = &stats_cg->default_groups[i]->cg_item;
1573                 stats_cg->default_groups[i] = NULL;
1574                 config_item_put(df_item);
1575         }
1576         kfree(stats_cg->default_groups);
1577
1578         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1579                         tiqn->tiqn);
1580         iscsit_del_tiqn(tiqn);
1581 }
1582
1583 /* End LIO-Target TIQN struct contig_lio_target_cit */
1584
1585 /* Start lio_target_discovery_auth_cit */
1586
1587 #define DEF_DISC_AUTH_STR(name, flags)                                  \
1588         __DEF_NACL_AUTH_STR(disc, name, flags)                          \
1589 static ssize_t iscsi_disc_show_##name(                                  \
1590         struct target_fabric_configfs *tf,                              \
1591         char *page)                                                     \
1592 {                                                                       \
1593         return __iscsi_disc_show_##name(&iscsit_global->discovery_acl,  \
1594                 page);                                                  \
1595 }                                                                       \
1596 static ssize_t iscsi_disc_store_##name(                                 \
1597         struct target_fabric_configfs *tf,                              \
1598         const char *page,                                               \
1599         size_t count)                                                   \
1600 {                                                                       \
1601         return __iscsi_disc_store_##name(&iscsit_global->discovery_acl, \
1602                 page, count);                                           \
1603 }
1604
1605 #define DEF_DISC_AUTH_INT(name)                                         \
1606         __DEF_NACL_AUTH_INT(disc, name)                                 \
1607 static ssize_t iscsi_disc_show_##name(                                  \
1608         struct target_fabric_configfs *tf,                              \
1609         char *page)                                                     \
1610 {                                                                       \
1611         return __iscsi_disc_show_##name(&iscsit_global->discovery_acl,  \
1612                         page);                                          \
1613 }
1614
1615 #define DISC_AUTH_ATTR(_name, _mode) TF_DISC_ATTR(iscsi, _name, _mode)
1616 #define DISC_AUTH_ATTR_RO(_name) TF_DISC_ATTR_RO(iscsi, _name)
1617
1618 /*
1619  * One-way authentication userid
1620  */
1621 DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
1622 DISC_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
1623 /*
1624  * One-way authentication password
1625  */
1626 DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
1627 DISC_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
1628 /*
1629  * Enforce mutual authentication
1630  */
1631 DEF_DISC_AUTH_INT(authenticate_target);
1632 DISC_AUTH_ATTR_RO(authenticate_target);
1633 /*
1634  * Mutual authentication userid
1635  */
1636 DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1637 DISC_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
1638 /*
1639  * Mutual authentication password
1640  */
1641 DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1642 DISC_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
1643
1644 /*
1645  * enforce_discovery_auth
1646  */
1647 static ssize_t iscsi_disc_show_enforce_discovery_auth(
1648         struct target_fabric_configfs *tf,
1649         char *page)
1650 {
1651         struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1652
1653         return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1654 }
1655
1656 static ssize_t iscsi_disc_store_enforce_discovery_auth(
1657         struct target_fabric_configfs *tf,
1658         const char *page,
1659         size_t count)
1660 {
1661         struct iscsi_param *param;
1662         struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1663         u32 op;
1664         int err;
1665
1666         err = kstrtou32(page, 0, &op);
1667         if (err)
1668                 return -EINVAL;
1669         if ((op != 1) && (op != 0)) {
1670                 pr_err("Illegal value for enforce_discovery_auth:"
1671                                 " %u\n", op);
1672                 return -EINVAL;
1673         }
1674
1675         if (!discovery_tpg) {
1676                 pr_err("iscsit_global->discovery_tpg is NULL\n");
1677                 return -EINVAL;
1678         }
1679
1680         param = iscsi_find_param_from_key(AUTHMETHOD,
1681                                 discovery_tpg->param_list);
1682         if (!param)
1683                 return -EINVAL;
1684
1685         if (op) {
1686                 /*
1687                  * Reset the AuthMethod key to CHAP.
1688                  */
1689                 if (iscsi_update_param_value(param, CHAP) < 0)
1690                         return -EINVAL;
1691
1692                 discovery_tpg->tpg_attrib.authentication = 1;
1693                 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1694                 pr_debug("LIO-CORE[0] Successfully enabled"
1695                         " authentication enforcement for iSCSI"
1696                         " Discovery TPG\n");
1697         } else {
1698                 /*
1699                  * Reset the AuthMethod key to CHAP,None
1700                  */
1701                 if (iscsi_update_param_value(param, "CHAP,None") < 0)
1702                         return -EINVAL;
1703
1704                 discovery_tpg->tpg_attrib.authentication = 0;
1705                 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1706                 pr_debug("LIO-CORE[0] Successfully disabled"
1707                         " authentication enforcement for iSCSI"
1708                         " Discovery TPG\n");
1709         }
1710
1711         return count;
1712 }
1713
1714 DISC_AUTH_ATTR(enforce_discovery_auth, S_IRUGO | S_IWUSR);
1715
1716 static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
1717         &iscsi_disc_userid.attr,
1718         &iscsi_disc_password.attr,
1719         &iscsi_disc_authenticate_target.attr,
1720         &iscsi_disc_userid_mutual.attr,
1721         &iscsi_disc_password_mutual.attr,
1722         &iscsi_disc_enforce_discovery_auth.attr,
1723         NULL,
1724 };
1725
1726 /* End lio_target_discovery_auth_cit */
1727
1728 /* Start functions for target_core_fabric_ops */
1729
1730 static char *iscsi_get_fabric_name(void)
1731 {
1732         return "iSCSI";
1733 }
1734
1735 static u32 iscsi_get_task_tag(struct se_cmd *se_cmd)
1736 {
1737         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1738
1739         /* only used for printks or comparism with ->ref_task_tag */
1740         return (__force u32)cmd->init_task_tag;
1741 }
1742
1743 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1744 {
1745         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1746
1747         return cmd->i_state;
1748 }
1749
1750 static u32 lio_sess_get_index(struct se_session *se_sess)
1751 {
1752         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1753
1754         return sess->session_index;
1755 }
1756
1757 static u32 lio_sess_get_initiator_sid(
1758         struct se_session *se_sess,
1759         unsigned char *buf,
1760         u32 size)
1761 {
1762         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1763         /*
1764          * iSCSI Initiator Session Identifier from RFC-3720.
1765          */
1766         return snprintf(buf, size, "%02x%02x%02x%02x%02x%02x",
1767                 sess->isid[0], sess->isid[1], sess->isid[2],
1768                 sess->isid[3], sess->isid[4], sess->isid[5]);
1769 }
1770
1771 static int lio_queue_data_in(struct se_cmd *se_cmd)
1772 {
1773         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1774
1775         cmd->i_state = ISTATE_SEND_DATAIN;
1776         cmd->conn->conn_transport->iscsit_queue_data_in(cmd->conn, cmd);
1777
1778         return 0;
1779 }
1780
1781 static int lio_write_pending(struct se_cmd *se_cmd)
1782 {
1783         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1784         struct iscsi_conn *conn = cmd->conn;
1785
1786         if (!cmd->immediate_data && !cmd->unsolicited_data)
1787                 return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1788
1789         return 0;
1790 }
1791
1792 static int lio_write_pending_status(struct se_cmd *se_cmd)
1793 {
1794         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1795         int ret;
1796
1797         spin_lock_bh(&cmd->istate_lock);
1798         ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1799         spin_unlock_bh(&cmd->istate_lock);
1800
1801         return ret;
1802 }
1803
1804 static int lio_queue_status(struct se_cmd *se_cmd)
1805 {
1806         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1807
1808         cmd->i_state = ISTATE_SEND_STATUS;
1809
1810         if (cmd->se_cmd.scsi_status || cmd->sense_reason) {
1811                 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1812                 return 0;
1813         }
1814         cmd->conn->conn_transport->iscsit_queue_status(cmd->conn, cmd);
1815
1816         return 0;
1817 }
1818
1819 static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
1820 {
1821         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1822
1823         cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1824         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1825 }
1826
1827 static void lio_aborted_task(struct se_cmd *se_cmd)
1828 {
1829         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1830
1831         cmd->conn->conn_transport->iscsit_aborted_task(cmd->conn, cmd);
1832 }
1833
1834 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1835 {
1836         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1837
1838         return &tpg->tpg_tiqn->tiqn[0];
1839 }
1840
1841 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1842 {
1843         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1844
1845         return tpg->tpgt;
1846 }
1847
1848 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1849 {
1850         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1851
1852         return tpg->tpg_attrib.default_cmdsn_depth;
1853 }
1854
1855 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1856 {
1857         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1858
1859         return tpg->tpg_attrib.generate_node_acls;
1860 }
1861
1862 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1863 {
1864         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1865
1866         return tpg->tpg_attrib.cache_dynamic_acls;
1867 }
1868
1869 static int lio_tpg_check_demo_mode_write_protect(
1870         struct se_portal_group *se_tpg)
1871 {
1872         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1873
1874         return tpg->tpg_attrib.demo_mode_write_protect;
1875 }
1876
1877 static int lio_tpg_check_prod_mode_write_protect(
1878         struct se_portal_group *se_tpg)
1879 {
1880         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1881
1882         return tpg->tpg_attrib.prod_mode_write_protect;
1883 }
1884
1885 static void lio_tpg_release_fabric_acl(
1886         struct se_portal_group *se_tpg,
1887         struct se_node_acl *se_acl)
1888 {
1889         struct iscsi_node_acl *acl = container_of(se_acl,
1890                                 struct iscsi_node_acl, se_node_acl);
1891         kfree(acl);
1892 }
1893
1894 /*
1895  * Called with spin_lock_irq(struct se_portal_group->session_lock) held
1896  * or not held.
1897  *
1898  * Also, this function calls iscsit_inc_session_usage_count() on the
1899  * struct iscsi_session in question.
1900  */
1901 static int lio_tpg_shutdown_session(struct se_session *se_sess)
1902 {
1903         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1904         struct se_portal_group *se_tpg = se_sess->se_tpg;
1905         bool local_lock = false;
1906
1907         if (!spin_is_locked(&se_tpg->session_lock)) {
1908                 spin_lock_irq(&se_tpg->session_lock);
1909                 local_lock = true;
1910         }
1911
1912         spin_lock(&sess->conn_lock);
1913         if (atomic_read(&sess->session_fall_back_to_erl0) ||
1914             atomic_read(&sess->session_logout) ||
1915             (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1916                 spin_unlock(&sess->conn_lock);
1917                 if (local_lock)
1918                         spin_unlock_irq(&sess->conn_lock);
1919                 return 0;
1920         }
1921         atomic_set(&sess->session_reinstatement, 1);
1922         spin_unlock(&sess->conn_lock);
1923
1924         iscsit_stop_time2retain_timer(sess);
1925         spin_unlock_irq(&se_tpg->session_lock);
1926
1927         iscsit_stop_session(sess, 1, 1);
1928         if (!local_lock)
1929                 spin_lock_irq(&se_tpg->session_lock);
1930
1931         return 1;
1932 }
1933
1934 /*
1935  * Calls iscsit_dec_session_usage_count() as inverse of
1936  * lio_tpg_shutdown_session()
1937  */
1938 static void lio_tpg_close_session(struct se_session *se_sess)
1939 {
1940         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1941         /*
1942          * If the iSCSI Session for the iSCSI Initiator Node exists,
1943          * forcefully shutdown the iSCSI NEXUS.
1944          */
1945         iscsit_close_session(sess);
1946 }
1947
1948 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1949 {
1950         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1951
1952         return tpg->tpg_tiqn->tiqn_index;
1953 }
1954
1955 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1956 {
1957         struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1958                                 se_node_acl);
1959         struct se_portal_group *se_tpg = se_acl->se_tpg;
1960         struct iscsi_portal_group *tpg = container_of(se_tpg,
1961                                 struct iscsi_portal_group, tpg_se_tpg);
1962
1963         acl->node_attrib.nacl = acl;
1964         iscsit_set_default_node_attribues(acl, tpg);
1965 }
1966
1967 static int lio_check_stop_free(struct se_cmd *se_cmd)
1968 {
1969         return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1970 }
1971
1972 static void lio_release_cmd(struct se_cmd *se_cmd)
1973 {
1974         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1975
1976         pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
1977         iscsit_release_cmd(cmd);
1978 }
1979
1980 /* End functions for target_core_fabric_ops */
1981
1982 int iscsi_target_register_configfs(void)
1983 {
1984         struct target_fabric_configfs *fabric;
1985         int ret;
1986
1987         lio_target_fabric_configfs = NULL;
1988         fabric = target_fabric_configfs_init(THIS_MODULE, "iscsi");
1989         if (IS_ERR(fabric)) {
1990                 pr_err("target_fabric_configfs_init() for"
1991                                 " LIO-Target failed!\n");
1992                 return PTR_ERR(fabric);
1993         }
1994         /*
1995          * Setup the fabric API of function pointers used by target_core_mod..
1996          */
1997         fabric->tf_ops.get_fabric_name = &iscsi_get_fabric_name;
1998         fabric->tf_ops.get_fabric_proto_ident = &iscsi_get_fabric_proto_ident;
1999         fabric->tf_ops.tpg_get_wwn = &lio_tpg_get_endpoint_wwn;
2000         fabric->tf_ops.tpg_get_tag = &lio_tpg_get_tag;
2001         fabric->tf_ops.tpg_get_default_depth = &lio_tpg_get_default_depth;
2002         fabric->tf_ops.tpg_get_pr_transport_id = &iscsi_get_pr_transport_id;
2003         fabric->tf_ops.tpg_get_pr_transport_id_len =
2004                                 &iscsi_get_pr_transport_id_len;
2005         fabric->tf_ops.tpg_parse_pr_out_transport_id =
2006                                 &iscsi_parse_pr_out_transport_id;
2007         fabric->tf_ops.tpg_check_demo_mode = &lio_tpg_check_demo_mode;
2008         fabric->tf_ops.tpg_check_demo_mode_cache =
2009                                 &lio_tpg_check_demo_mode_cache;
2010         fabric->tf_ops.tpg_check_demo_mode_write_protect =
2011                                 &lio_tpg_check_demo_mode_write_protect;
2012         fabric->tf_ops.tpg_check_prod_mode_write_protect =
2013                                 &lio_tpg_check_prod_mode_write_protect;
2014         fabric->tf_ops.tpg_alloc_fabric_acl = &lio_tpg_alloc_fabric_acl;
2015         fabric->tf_ops.tpg_release_fabric_acl = &lio_tpg_release_fabric_acl;
2016         fabric->tf_ops.tpg_get_inst_index = &lio_tpg_get_inst_index;
2017         fabric->tf_ops.check_stop_free = &lio_check_stop_free,
2018         fabric->tf_ops.release_cmd = &lio_release_cmd;
2019         fabric->tf_ops.shutdown_session = &lio_tpg_shutdown_session;
2020         fabric->tf_ops.close_session = &lio_tpg_close_session;
2021         fabric->tf_ops.sess_get_index = &lio_sess_get_index;
2022         fabric->tf_ops.sess_get_initiator_sid = &lio_sess_get_initiator_sid;
2023         fabric->tf_ops.write_pending = &lio_write_pending;
2024         fabric->tf_ops.write_pending_status = &lio_write_pending_status;
2025         fabric->tf_ops.set_default_node_attributes =
2026                                 &lio_set_default_node_attributes;
2027         fabric->tf_ops.get_task_tag = &iscsi_get_task_tag;
2028         fabric->tf_ops.get_cmd_state = &iscsi_get_cmd_state;
2029         fabric->tf_ops.queue_data_in = &lio_queue_data_in;
2030         fabric->tf_ops.queue_status = &lio_queue_status;
2031         fabric->tf_ops.queue_tm_rsp = &lio_queue_tm_rsp;
2032         fabric->tf_ops.aborted_task = &lio_aborted_task;
2033         /*
2034          * Setup function pointers for generic logic in target_core_fabric_configfs.c
2035          */
2036         fabric->tf_ops.fabric_make_wwn = &lio_target_call_coreaddtiqn;
2037         fabric->tf_ops.fabric_drop_wwn = &lio_target_call_coredeltiqn;
2038         fabric->tf_ops.fabric_make_tpg = &lio_target_tiqn_addtpg;
2039         fabric->tf_ops.fabric_drop_tpg = &lio_target_tiqn_deltpg;
2040         fabric->tf_ops.fabric_post_link = NULL;
2041         fabric->tf_ops.fabric_pre_unlink = NULL;
2042         fabric->tf_ops.fabric_make_np = &lio_target_call_addnptotpg;
2043         fabric->tf_ops.fabric_drop_np = &lio_target_call_delnpfromtpg;
2044         fabric->tf_ops.fabric_make_nodeacl = &lio_target_make_nodeacl;
2045         fabric->tf_ops.fabric_drop_nodeacl = &lio_target_drop_nodeacl;
2046         /*
2047          * Setup default attribute lists for various fabric->tf_cit_tmpl
2048          * sturct config_item_type's
2049          */
2050         fabric->tf_cit_tmpl.tfc_discovery_cit.ct_attrs = lio_target_discovery_auth_attrs;
2051         fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = lio_target_wwn_attrs;
2052         fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = lio_target_tpg_attrs;
2053         fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = lio_target_tpg_attrib_attrs;
2054         fabric->tf_cit_tmpl.tfc_tpg_auth_cit.ct_attrs = lio_target_tpg_auth_attrs;
2055         fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = lio_target_tpg_param_attrs;
2056         fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = lio_target_portal_attrs;
2057         fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = lio_target_initiator_attrs;
2058         fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = lio_target_nacl_attrib_attrs;
2059         fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = lio_target_nacl_auth_attrs;
2060         fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = lio_target_nacl_param_attrs;
2061
2062         ret = target_fabric_configfs_register(fabric);
2063         if (ret < 0) {
2064                 pr_err("target_fabric_configfs_register() for"
2065                                 " LIO-Target failed!\n");
2066                 target_fabric_configfs_free(fabric);
2067                 return ret;
2068         }
2069
2070         lio_target_fabric_configfs = fabric;
2071         pr_debug("LIO_TARGET[0] - Set fabric ->"
2072                         " lio_target_fabric_configfs\n");
2073         return 0;
2074 }
2075
2076
2077 void iscsi_target_deregister_configfs(void)
2078 {
2079         if (!lio_target_fabric_configfs)
2080                 return;
2081         /*
2082          * Shutdown discovery sessions and disable discovery TPG
2083          */
2084         if (iscsit_global->discovery_tpg)
2085                 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
2086
2087         target_fabric_configfs_deregister(lio_target_fabric_configfs);
2088         lio_target_fabric_configfs = NULL;
2089         pr_debug("LIO_TARGET[0] - Cleared"
2090                                 " lio_target_fabric_configfs\n");
2091 }