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