GNU Linux-libre 4.9.337-gnu1
[releases.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/iscsi/iscsi_transport.h>
27
28 #include <target/iscsi/iscsi_target_core.h>
29 #include "iscsi_target_parameters.h"
30 #include "iscsi_target_device.h"
31 #include "iscsi_target_erl0.h"
32 #include "iscsi_target_nodeattrib.h"
33 #include "iscsi_target_tpg.h"
34 #include "iscsi_target_util.h"
35 #include "iscsi_target.h"
36 #include <target/iscsi/iscsi_target_stat.h>
37
38
39 /* Start items for lio_target_portal_cit */
40
41 static inline struct iscsi_tpg_np *to_iscsi_tpg_np(struct config_item *item)
42 {
43         return container_of(to_tpg_np(item), struct iscsi_tpg_np, se_tpg_np);
44 }
45
46 static ssize_t lio_target_np_driver_show(struct config_item *item, char *page,
47                                          enum iscsit_transport_type type)
48 {
49         struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
50         struct iscsi_tpg_np *tpg_np_new;
51         ssize_t rb;
52
53         tpg_np_new = iscsit_tpg_locate_child_np(tpg_np, type);
54         if (tpg_np_new)
55                 rb = sprintf(page, "1\n");
56         else
57                 rb = sprintf(page, "0\n");
58
59         return rb;
60 }
61
62 static ssize_t lio_target_np_driver_store(struct config_item *item,
63                 const char *page, size_t count, enum iscsit_transport_type type,
64                 const char *mod_name)
65 {
66         struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
67         struct iscsi_np *np;
68         struct iscsi_portal_group *tpg;
69         struct iscsi_tpg_np *tpg_np_new = NULL;
70         u32 op;
71         int rc;
72
73         rc = kstrtou32(page, 0, &op);
74         if (rc)
75                 return rc;
76         if ((op != 1) && (op != 0)) {
77                 pr_err("Illegal value for tpg_enable: %u\n", op);
78                 return -EINVAL;
79         }
80         np = tpg_np->tpg_np;
81         if (!np) {
82                 pr_err("Unable to locate struct iscsi_np from"
83                                 " struct iscsi_tpg_np\n");
84                 return -EINVAL;
85         }
86
87         tpg = tpg_np->tpg;
88         if (iscsit_get_tpg(tpg) < 0)
89                 return -EINVAL;
90
91         if (op) {
92                 if (strlen(mod_name)) {
93                         rc = request_module(mod_name);
94                         if (rc != 0) {
95                                 pr_warn("Unable to request_module for %s\n",
96                                         mod_name);
97                                 rc = 0;
98                         }
99                 }
100
101                 tpg_np_new = iscsit_tpg_add_network_portal(tpg,
102                                         &np->np_sockaddr, tpg_np, type);
103                 if (IS_ERR(tpg_np_new)) {
104                         rc = PTR_ERR(tpg_np_new);
105                         goto out;
106                 }
107         } else {
108                 tpg_np_new = iscsit_tpg_locate_child_np(tpg_np, type);
109                 if (tpg_np_new) {
110                         rc = iscsit_tpg_del_network_portal(tpg, tpg_np_new);
111                         if (rc < 0)
112                                 goto out;
113                 }
114         }
115
116         iscsit_put_tpg(tpg);
117         return count;
118 out:
119         iscsit_put_tpg(tpg);
120         return rc;
121 }
122
123 static ssize_t lio_target_np_iser_show(struct config_item *item, char *page)
124 {
125         return lio_target_np_driver_show(item, page, ISCSI_INFINIBAND);
126 }
127
128 static ssize_t lio_target_np_iser_store(struct config_item *item,
129                                         const char *page, size_t count)
130 {
131         return lio_target_np_driver_store(item, page, count,
132                                           ISCSI_INFINIBAND, "ib_isert");
133 }
134 CONFIGFS_ATTR(lio_target_np_, iser);
135
136 static ssize_t lio_target_np_cxgbit_show(struct config_item *item, char *page)
137 {
138         return lio_target_np_driver_show(item, page, ISCSI_CXGBIT);
139 }
140
141 static ssize_t lio_target_np_cxgbit_store(struct config_item *item,
142                                           const char *page, size_t count)
143 {
144         return lio_target_np_driver_store(item, page, count,
145                                           ISCSI_CXGBIT, "cxgbit");
146 }
147 CONFIGFS_ATTR(lio_target_np_, cxgbit);
148
149 static struct configfs_attribute *lio_target_portal_attrs[] = {
150         &lio_target_np_attr_iser,
151         &lio_target_np_attr_cxgbit,
152         NULL,
153 };
154
155 /* Stop items for lio_target_portal_cit */
156
157 /* Start items for lio_target_np_cit */
158
159 #define MAX_PORTAL_LEN          256
160
161 static struct se_tpg_np *lio_target_call_addnptotpg(
162         struct se_portal_group *se_tpg,
163         struct config_group *group,
164         const char *name)
165 {
166         struct iscsi_portal_group *tpg;
167         struct iscsi_tpg_np *tpg_np;
168         char *str, *str2, *ip_str, *port_str;
169         struct sockaddr_storage sockaddr;
170         struct sockaddr_in *sock_in;
171         struct sockaddr_in6 *sock_in6;
172         unsigned long port;
173         int ret;
174         char buf[MAX_PORTAL_LEN + 1];
175
176         if (strlen(name) > MAX_PORTAL_LEN) {
177                 pr_err("strlen(name): %d exceeds MAX_PORTAL_LEN: %d\n",
178                         (int)strlen(name), MAX_PORTAL_LEN);
179                 return ERR_PTR(-EOVERFLOW);
180         }
181         memset(buf, 0, MAX_PORTAL_LEN + 1);
182         snprintf(buf, MAX_PORTAL_LEN + 1, "%s", name);
183
184         memset(&sockaddr, 0, sizeof(struct sockaddr_storage));
185
186         str = strstr(buf, "[");
187         if (str) {
188                 const char *end;
189
190                 str2 = strstr(str, "]");
191                 if (!str2) {
192                         pr_err("Unable to locate trailing \"]\""
193                                 " in IPv6 iSCSI network portal address\n");
194                         return ERR_PTR(-EINVAL);
195                 }
196                 str++; /* Skip over leading "[" */
197                 *str2 = '\0'; /* Terminate the unbracketed IPv6 address */
198                 str2++; /* Skip over the \0 */
199                 port_str = strstr(str2, ":");
200                 if (!port_str) {
201                         pr_err("Unable to locate \":port\""
202                                 " in IPv6 iSCSI network portal address\n");
203                         return ERR_PTR(-EINVAL);
204                 }
205                 *port_str = '\0'; /* Terminate string for IP */
206                 port_str++; /* Skip over ":" */
207
208                 ret = kstrtoul(port_str, 0, &port);
209                 if (ret < 0) {
210                         pr_err("kstrtoul() failed for port_str: %d\n", ret);
211                         return ERR_PTR(ret);
212                 }
213                 sock_in6 = (struct sockaddr_in6 *)&sockaddr;
214                 sock_in6->sin6_family = AF_INET6;
215                 sock_in6->sin6_port = htons((unsigned short)port);
216                 ret = in6_pton(str, -1,
217                                 (void *)&sock_in6->sin6_addr.in6_u, -1, &end);
218                 if (ret <= 0) {
219                         pr_err("in6_pton returned: %d\n", ret);
220                         return ERR_PTR(-EINVAL);
221                 }
222         } else {
223                 str = ip_str = &buf[0];
224                 port_str = strstr(ip_str, ":");
225                 if (!port_str) {
226                         pr_err("Unable to locate \":port\""
227                                 " in IPv4 iSCSI network portal address\n");
228                         return ERR_PTR(-EINVAL);
229                 }
230                 *port_str = '\0'; /* Terminate string for IP */
231                 port_str++; /* Skip over ":" */
232
233                 ret = kstrtoul(port_str, 0, &port);
234                 if (ret < 0) {
235                         pr_err("kstrtoul() failed for port_str: %d\n", ret);
236                         return ERR_PTR(ret);
237                 }
238                 sock_in = (struct sockaddr_in *)&sockaddr;
239                 sock_in->sin_family = AF_INET;
240                 sock_in->sin_port = htons((unsigned short)port);
241                 sock_in->sin_addr.s_addr = in_aton(ip_str);
242         }
243         tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
244         ret = iscsit_get_tpg(tpg);
245         if (ret < 0)
246                 return ERR_PTR(-EINVAL);
247
248         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
249                 " PORTAL: %s\n",
250                 config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
251                 tpg->tpgt, name);
252         /*
253          * Assume ISCSI_TCP by default.  Other network portals for other
254          * iSCSI fabrics:
255          *
256          * Traditional iSCSI over SCTP (initial support)
257          * iSER/TCP (TODO, hardware available)
258          * iSER/SCTP (TODO, software emulation with osc-iwarp)
259          * iSER/IB (TODO, hardware available)
260          *
261          * can be enabled with attributes under
262          * sys/kernel/config/iscsi/$IQN/$TPG/np/$IP:$PORT/
263          *
264          */
265         tpg_np = iscsit_tpg_add_network_portal(tpg, &sockaddr, NULL,
266                                 ISCSI_TCP);
267         if (IS_ERR(tpg_np)) {
268                 iscsit_put_tpg(tpg);
269                 return ERR_CAST(tpg_np);
270         }
271         pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
272
273         iscsit_put_tpg(tpg);
274         return &tpg_np->se_tpg_np;
275 }
276
277 static void lio_target_call_delnpfromtpg(
278         struct se_tpg_np *se_tpg_np)
279 {
280         struct iscsi_portal_group *tpg;
281         struct iscsi_tpg_np *tpg_np;
282         struct se_portal_group *se_tpg;
283         int ret;
284
285         tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
286         tpg = tpg_np->tpg;
287         ret = iscsit_get_tpg(tpg);
288         if (ret < 0)
289                 return;
290
291         se_tpg = &tpg->tpg_se_tpg;
292         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s TPGT: %hu"
293                 " PORTAL: %pISpc\n", config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
294                 tpg->tpgt, &tpg_np->tpg_np->np_sockaddr);
295
296         ret = iscsit_tpg_del_network_portal(tpg, tpg_np);
297         if (ret < 0)
298                 goto out;
299
300         pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
301 out:
302         iscsit_put_tpg(tpg);
303 }
304
305 /* End items for lio_target_np_cit */
306
307 /* Start items for lio_target_nacl_attrib_cit */
308
309 #define ISCSI_NACL_ATTR(name)                                           \
310 static ssize_t iscsi_nacl_attrib_##name##_show(struct config_item *item,\
311                 char *page)                                             \
312 {                                                                       \
313         struct se_node_acl *se_nacl = attrib_to_nacl(item);             \
314         struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
315                                         se_node_acl);                   \
316                                                                         \
317         return sprintf(page, "%u\n", nacl->node_attrib.name);           \
318 }                                                                       \
319                                                                         \
320 static ssize_t iscsi_nacl_attrib_##name##_store(struct config_item *item,\
321                 const char *page, size_t count)                         \
322 {                                                                       \
323         struct se_node_acl *se_nacl = attrib_to_nacl(item);             \
324         struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
325                                         se_node_acl);                   \
326         u32 val;                                                        \
327         int ret;                                                        \
328                                                                         \
329         ret = kstrtou32(page, 0, &val);                                 \
330         if (ret)                                                        \
331                 return ret;                                             \
332         ret = iscsit_na_##name(nacl, val);                              \
333         if (ret < 0)                                                    \
334                 return ret;                                             \
335                                                                         \
336         return count;                                                   \
337 }                                                                       \
338                                                                         \
339 CONFIGFS_ATTR(iscsi_nacl_attrib_, name)
340
341 ISCSI_NACL_ATTR(dataout_timeout);
342 ISCSI_NACL_ATTR(dataout_timeout_retries);
343 ISCSI_NACL_ATTR(default_erl);
344 ISCSI_NACL_ATTR(nopin_timeout);
345 ISCSI_NACL_ATTR(nopin_response_timeout);
346 ISCSI_NACL_ATTR(random_datain_pdu_offsets);
347 ISCSI_NACL_ATTR(random_datain_seq_offsets);
348 ISCSI_NACL_ATTR(random_r2t_offsets);
349
350 static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
351         &iscsi_nacl_attrib_attr_dataout_timeout,
352         &iscsi_nacl_attrib_attr_dataout_timeout_retries,
353         &iscsi_nacl_attrib_attr_default_erl,
354         &iscsi_nacl_attrib_attr_nopin_timeout,
355         &iscsi_nacl_attrib_attr_nopin_response_timeout,
356         &iscsi_nacl_attrib_attr_random_datain_pdu_offsets,
357         &iscsi_nacl_attrib_attr_random_datain_seq_offsets,
358         &iscsi_nacl_attrib_attr_random_r2t_offsets,
359         NULL,
360 };
361
362 /* End items for lio_target_nacl_attrib_cit */
363
364 /* Start items for lio_target_nacl_auth_cit */
365
366 #define __DEF_NACL_AUTH_STR(prefix, name, flags)                        \
367 static ssize_t __iscsi_##prefix##_##name##_show(                        \
368         struct iscsi_node_acl *nacl,                                    \
369         char *page)                                                     \
370 {                                                                       \
371         struct iscsi_node_auth *auth = &nacl->node_auth;                \
372                                                                         \
373         if (!capable(CAP_SYS_ADMIN))                                    \
374                 return -EPERM;                                          \
375         return snprintf(page, PAGE_SIZE, "%s\n", auth->name);           \
376 }                                                                       \
377                                                                         \
378 static ssize_t __iscsi_##prefix##_##name##_store(                       \
379         struct iscsi_node_acl *nacl,                                    \
380         const char *page,                                               \
381         size_t count)                                                   \
382 {                                                                       \
383         struct iscsi_node_auth *auth = &nacl->node_auth;                \
384                                                                         \
385         if (!capable(CAP_SYS_ADMIN))                                    \
386                 return -EPERM;                                          \
387         if (count >= sizeof(auth->name))                                \
388                 return -EINVAL;                                         \
389         snprintf(auth->name, sizeof(auth->name), "%s", page);           \
390         if (!strncmp("NULL", auth->name, 4))                            \
391                 auth->naf_flags &= ~flags;                              \
392         else                                                            \
393                 auth->naf_flags |= flags;                               \
394                                                                         \
395         if ((auth->naf_flags & NAF_USERID_IN_SET) &&                    \
396             (auth->naf_flags & NAF_PASSWORD_IN_SET))                    \
397                 auth->authenticate_target = 1;                          \
398         else                                                            \
399                 auth->authenticate_target = 0;                          \
400                                                                         \
401         return count;                                                   \
402 }
403
404 #define DEF_NACL_AUTH_STR(name, flags)                                  \
405         __DEF_NACL_AUTH_STR(nacl_auth, name, flags)                     \
406 static ssize_t iscsi_nacl_auth_##name##_show(struct config_item *item,  \
407                 char *page)                                             \
408 {                                                                       \
409         struct se_node_acl *nacl = auth_to_nacl(item);                  \
410         return __iscsi_nacl_auth_##name##_show(container_of(nacl,       \
411                         struct iscsi_node_acl, se_node_acl), page);     \
412 }                                                                       \
413 static ssize_t iscsi_nacl_auth_##name##_store(struct config_item *item, \
414                 const char *page, size_t count)                         \
415 {                                                                       \
416         struct se_node_acl *nacl = auth_to_nacl(item);                  \
417         return __iscsi_nacl_auth_##name##_store(container_of(nacl,      \
418                         struct iscsi_node_acl, se_node_acl), page, count); \
419 }                                                                       \
420                                                                         \
421 CONFIGFS_ATTR(iscsi_nacl_auth_, name)
422
423 /*
424  * One-way authentication userid
425  */
426 DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
427 DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
428 DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
429 DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
430
431 #define __DEF_NACL_AUTH_INT(prefix, name)                               \
432 static ssize_t __iscsi_##prefix##_##name##_show(                                \
433         struct iscsi_node_acl *nacl,                                    \
434         char *page)                                                     \
435 {                                                                       \
436         struct iscsi_node_auth *auth = &nacl->node_auth;                \
437                                                                         \
438         if (!capable(CAP_SYS_ADMIN))                                    \
439                 return -EPERM;                                          \
440                                                                         \
441         return snprintf(page, PAGE_SIZE, "%d\n", auth->name);           \
442 }
443
444 #define DEF_NACL_AUTH_INT(name)                                         \
445         __DEF_NACL_AUTH_INT(nacl_auth, name)                            \
446 static ssize_t iscsi_nacl_auth_##name##_show(struct config_item *item,  \
447                 char *page)                                             \
448 {                                                                       \
449         struct se_node_acl *nacl = auth_to_nacl(item);                  \
450         return __iscsi_nacl_auth_##name##_show(container_of(nacl,       \
451                         struct iscsi_node_acl, se_node_acl), page);     \
452 }                                                                       \
453                                                                         \
454 CONFIGFS_ATTR_RO(iscsi_nacl_auth_, name)
455
456 DEF_NACL_AUTH_INT(authenticate_target);
457
458 static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
459         &iscsi_nacl_auth_attr_userid,
460         &iscsi_nacl_auth_attr_password,
461         &iscsi_nacl_auth_attr_authenticate_target,
462         &iscsi_nacl_auth_attr_userid_mutual,
463         &iscsi_nacl_auth_attr_password_mutual,
464         NULL,
465 };
466
467 /* End items for lio_target_nacl_auth_cit */
468
469 /* Start items for lio_target_nacl_param_cit */
470
471 #define ISCSI_NACL_PARAM(name)                                          \
472 static ssize_t iscsi_nacl_param_##name##_show(struct config_item *item, \
473                 char *page)                                             \
474 {                                                                       \
475         struct se_node_acl *se_nacl = param_to_nacl(item);              \
476         struct iscsi_session *sess;                                     \
477         struct se_session *se_sess;                                     \
478         ssize_t rb;                                                     \
479                                                                         \
480         spin_lock_bh(&se_nacl->nacl_sess_lock);                         \
481         se_sess = se_nacl->nacl_sess;                                   \
482         if (!se_sess) {                                                 \
483                 rb = snprintf(page, PAGE_SIZE,                          \
484                         "No Active iSCSI Session\n");                   \
485         } else {                                                        \
486                 sess = se_sess->fabric_sess_ptr;                        \
487                 rb = snprintf(page, PAGE_SIZE, "%u\n",                  \
488                         (u32)sess->sess_ops->name);                     \
489         }                                                               \
490         spin_unlock_bh(&se_nacl->nacl_sess_lock);                       \
491                                                                         \
492         return rb;                                                      \
493 }                                                                       \
494                                                                         \
495 CONFIGFS_ATTR_RO(iscsi_nacl_param_, name)
496
497 ISCSI_NACL_PARAM(MaxConnections);
498 ISCSI_NACL_PARAM(InitialR2T);
499 ISCSI_NACL_PARAM(ImmediateData);
500 ISCSI_NACL_PARAM(MaxBurstLength);
501 ISCSI_NACL_PARAM(FirstBurstLength);
502 ISCSI_NACL_PARAM(DefaultTime2Wait);
503 ISCSI_NACL_PARAM(DefaultTime2Retain);
504 ISCSI_NACL_PARAM(MaxOutstandingR2T);
505 ISCSI_NACL_PARAM(DataPDUInOrder);
506 ISCSI_NACL_PARAM(DataSequenceInOrder);
507 ISCSI_NACL_PARAM(ErrorRecoveryLevel);
508
509 static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
510         &iscsi_nacl_param_attr_MaxConnections,
511         &iscsi_nacl_param_attr_InitialR2T,
512         &iscsi_nacl_param_attr_ImmediateData,
513         &iscsi_nacl_param_attr_MaxBurstLength,
514         &iscsi_nacl_param_attr_FirstBurstLength,
515         &iscsi_nacl_param_attr_DefaultTime2Wait,
516         &iscsi_nacl_param_attr_DefaultTime2Retain,
517         &iscsi_nacl_param_attr_MaxOutstandingR2T,
518         &iscsi_nacl_param_attr_DataPDUInOrder,
519         &iscsi_nacl_param_attr_DataSequenceInOrder,
520         &iscsi_nacl_param_attr_ErrorRecoveryLevel,
521         NULL,
522 };
523
524 /* End items for lio_target_nacl_param_cit */
525
526 /* Start items for lio_target_acl_cit */
527
528 static ssize_t lio_target_nacl_info_show(struct config_item *item, char *page)
529 {
530         struct se_node_acl *se_nacl = acl_to_nacl(item);
531         struct iscsi_session *sess;
532         struct iscsi_conn *conn;
533         struct se_session *se_sess;
534         ssize_t rb = 0;
535         u32 max_cmd_sn;
536
537         spin_lock_bh(&se_nacl->nacl_sess_lock);
538         se_sess = se_nacl->nacl_sess;
539         if (!se_sess) {
540                 rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
541                         " Endpoint: %s\n", se_nacl->initiatorname);
542         } else {
543                 sess = se_sess->fabric_sess_ptr;
544
545                 rb += sprintf(page+rb, "InitiatorName: %s\n",
546                         sess->sess_ops->InitiatorName);
547                 rb += sprintf(page+rb, "InitiatorAlias: %s\n",
548                         sess->sess_ops->InitiatorAlias);
549
550                 rb += sprintf(page+rb,
551                               "LIO Session ID: %u   ISID: 0x%6ph  TSIH: %hu  ",
552                               sess->sid, sess->isid, sess->tsih);
553                 rb += sprintf(page+rb, "SessionType: %s\n",
554                                 (sess->sess_ops->SessionType) ?
555                                 "Discovery" : "Normal");
556                 rb += sprintf(page+rb, "Session State: ");
557                 switch (sess->session_state) {
558                 case TARG_SESS_STATE_FREE:
559                         rb += sprintf(page+rb, "TARG_SESS_FREE\n");
560                         break;
561                 case TARG_SESS_STATE_ACTIVE:
562                         rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
563                         break;
564                 case TARG_SESS_STATE_LOGGED_IN:
565                         rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
566                         break;
567                 case TARG_SESS_STATE_FAILED:
568                         rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
569                         break;
570                 case TARG_SESS_STATE_IN_CONTINUE:
571                         rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
572                         break;
573                 default:
574                         rb += sprintf(page+rb, "ERROR: Unknown Session"
575                                         " State!\n");
576                         break;
577                 }
578
579                 rb += sprintf(page+rb, "---------------------[iSCSI Session"
580                                 " Values]-----------------------\n");
581                 rb += sprintf(page+rb, "  CmdSN/WR  :  CmdSN/WC  :  ExpCmdSN"
582                                 "  :  MaxCmdSN  :     ITT    :     TTT\n");
583                 max_cmd_sn = (u32) atomic_read(&sess->max_cmd_sn);
584                 rb += sprintf(page+rb, " 0x%08x   0x%08x   0x%08x   0x%08x"
585                                 "   0x%08x   0x%08x\n",
586                         sess->cmdsn_window,
587                         (max_cmd_sn - sess->exp_cmd_sn) + 1,
588                         sess->exp_cmd_sn, max_cmd_sn,
589                         sess->init_task_tag, sess->targ_xfer_tag);
590                 rb += sprintf(page+rb, "----------------------[iSCSI"
591                                 " Connections]-------------------------\n");
592
593                 spin_lock(&sess->conn_lock);
594                 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
595                         rb += sprintf(page+rb, "CID: %hu  Connection"
596                                         " State: ", conn->cid);
597                         switch (conn->conn_state) {
598                         case TARG_CONN_STATE_FREE:
599                                 rb += sprintf(page+rb,
600                                         "TARG_CONN_STATE_FREE\n");
601                                 break;
602                         case TARG_CONN_STATE_XPT_UP:
603                                 rb += sprintf(page+rb,
604                                         "TARG_CONN_STATE_XPT_UP\n");
605                                 break;
606                         case TARG_CONN_STATE_IN_LOGIN:
607                                 rb += sprintf(page+rb,
608                                         "TARG_CONN_STATE_IN_LOGIN\n");
609                                 break;
610                         case TARG_CONN_STATE_LOGGED_IN:
611                                 rb += sprintf(page+rb,
612                                         "TARG_CONN_STATE_LOGGED_IN\n");
613                                 break;
614                         case TARG_CONN_STATE_IN_LOGOUT:
615                                 rb += sprintf(page+rb,
616                                         "TARG_CONN_STATE_IN_LOGOUT\n");
617                                 break;
618                         case TARG_CONN_STATE_LOGOUT_REQUESTED:
619                                 rb += sprintf(page+rb,
620                                         "TARG_CONN_STATE_LOGOUT_REQUESTED\n");
621                                 break;
622                         case TARG_CONN_STATE_CLEANUP_WAIT:
623                                 rb += sprintf(page+rb,
624                                         "TARG_CONN_STATE_CLEANUP_WAIT\n");
625                                 break;
626                         default:
627                                 rb += sprintf(page+rb,
628                                         "ERROR: Unknown Connection State!\n");
629                                 break;
630                         }
631
632                         rb += sprintf(page+rb, "   Address %pISc %s", &conn->login_sockaddr,
633                                 (conn->network_transport == ISCSI_TCP) ?
634                                 "TCP" : "SCTP");
635                         rb += sprintf(page+rb, "  StatSN: 0x%08x\n",
636                                 conn->stat_sn);
637                 }
638                 spin_unlock(&sess->conn_lock);
639         }
640         spin_unlock_bh(&se_nacl->nacl_sess_lock);
641
642         return rb;
643 }
644
645 static ssize_t lio_target_nacl_cmdsn_depth_show(struct config_item *item,
646                 char *page)
647 {
648         return sprintf(page, "%u\n", acl_to_nacl(item)->queue_depth);
649 }
650
651 static ssize_t lio_target_nacl_cmdsn_depth_store(struct config_item *item,
652                 const char *page, size_t count)
653 {
654         struct se_node_acl *se_nacl = acl_to_nacl(item);
655         struct se_portal_group *se_tpg = se_nacl->se_tpg;
656         struct iscsi_portal_group *tpg = container_of(se_tpg,
657                         struct iscsi_portal_group, tpg_se_tpg);
658         struct config_item *acl_ci, *tpg_ci, *wwn_ci;
659         u32 cmdsn_depth = 0;
660         int ret;
661
662         ret = kstrtou32(page, 0, &cmdsn_depth);
663         if (ret)
664                 return ret;
665         if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
666                 pr_err("Passed cmdsn_depth: %u exceeds"
667                         " TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
668                         TA_DEFAULT_CMDSN_DEPTH_MAX);
669                 return -EINVAL;
670         }
671         acl_ci = &se_nacl->acl_group.cg_item;
672         if (!acl_ci) {
673                 pr_err("Unable to locatel acl_ci\n");
674                 return -EINVAL;
675         }
676         tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
677         if (!tpg_ci) {
678                 pr_err("Unable to locate tpg_ci\n");
679                 return -EINVAL;
680         }
681         wwn_ci = &tpg_ci->ci_group->cg_item;
682         if (!wwn_ci) {
683                 pr_err("Unable to locate config_item wwn_ci\n");
684                 return -EINVAL;
685         }
686
687         if (iscsit_get_tpg(tpg) < 0)
688                 return -EINVAL;
689
690         ret = core_tpg_set_initiator_node_queue_depth(se_nacl, cmdsn_depth);
691
692         pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
693                 "InitiatorName: %s\n", config_item_name(wwn_ci),
694                 config_item_name(tpg_ci), cmdsn_depth,
695                 config_item_name(acl_ci));
696
697         iscsit_put_tpg(tpg);
698         return (!ret) ? count : (ssize_t)ret;
699 }
700
701 static ssize_t lio_target_nacl_tag_show(struct config_item *item, char *page)
702 {
703         return snprintf(page, PAGE_SIZE, "%s", acl_to_nacl(item)->acl_tag);
704 }
705
706 static ssize_t lio_target_nacl_tag_store(struct config_item *item,
707                 const char *page, size_t count)
708 {
709         struct se_node_acl *se_nacl = acl_to_nacl(item);
710         int ret;
711
712         ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
713
714         if (ret < 0)
715                 return ret;
716         return count;
717 }
718
719 CONFIGFS_ATTR_RO(lio_target_nacl_, info);
720 CONFIGFS_ATTR(lio_target_nacl_, cmdsn_depth);
721 CONFIGFS_ATTR(lio_target_nacl_, tag);
722
723 static struct configfs_attribute *lio_target_initiator_attrs[] = {
724         &lio_target_nacl_attr_info,
725         &lio_target_nacl_attr_cmdsn_depth,
726         &lio_target_nacl_attr_tag,
727         NULL,
728 };
729
730 static int lio_target_init_nodeacl(struct se_node_acl *se_nacl,
731                 const char *name)
732 {
733         struct iscsi_node_acl *acl =
734                 container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
735
736         config_group_init_type_name(&acl->node_stat_grps.iscsi_sess_stats_group,
737                         "iscsi_sess_stats", &iscsi_stat_sess_cit);
738         configfs_add_default_group(&acl->node_stat_grps.iscsi_sess_stats_group,
739                         &se_nacl->acl_fabric_stat_group);
740         return 0;
741 }
742
743 /* End items for lio_target_acl_cit */
744
745 /* Start items for lio_target_tpg_attrib_cit */
746
747 #define DEF_TPG_ATTRIB(name)                                            \
748                                                                         \
749 static ssize_t iscsi_tpg_attrib_##name##_show(struct config_item *item, \
750                 char *page)                                             \
751 {                                                                       \
752         struct se_portal_group *se_tpg = attrib_to_tpg(item);           \
753         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
754                         struct iscsi_portal_group, tpg_se_tpg); \
755         ssize_t rb;                                                     \
756                                                                         \
757         if (iscsit_get_tpg(tpg) < 0)                                    \
758                 return -EINVAL;                                         \
759                                                                         \
760         rb = sprintf(page, "%u\n", tpg->tpg_attrib.name);               \
761         iscsit_put_tpg(tpg);                                            \
762         return rb;                                                      \
763 }                                                                       \
764                                                                         \
765 static ssize_t iscsi_tpg_attrib_##name##_store(struct config_item *item,\
766                 const char *page, size_t count)                         \
767 {                                                                       \
768         struct se_portal_group *se_tpg = attrib_to_tpg(item);           \
769         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
770                         struct iscsi_portal_group, tpg_se_tpg); \
771         u32 val;                                                        \
772         int ret;                                                        \
773                                                                         \
774         if (iscsit_get_tpg(tpg) < 0)                                    \
775                 return -EINVAL;                                         \
776                                                                         \
777         ret = kstrtou32(page, 0, &val);                                 \
778         if (ret)                                                        \
779                 goto out;                                               \
780         ret = iscsit_ta_##name(tpg, val);                               \
781         if (ret < 0)                                                    \
782                 goto out;                                               \
783                                                                         \
784         iscsit_put_tpg(tpg);                                            \
785         return count;                                                   \
786 out:                                                                    \
787         iscsit_put_tpg(tpg);                                            \
788         return ret;                                                     \
789 }                                                                       \
790 CONFIGFS_ATTR(iscsi_tpg_attrib_, name)
791
792 DEF_TPG_ATTRIB(authentication);
793 DEF_TPG_ATTRIB(login_timeout);
794 DEF_TPG_ATTRIB(netif_timeout);
795 DEF_TPG_ATTRIB(generate_node_acls);
796 DEF_TPG_ATTRIB(default_cmdsn_depth);
797 DEF_TPG_ATTRIB(cache_dynamic_acls);
798 DEF_TPG_ATTRIB(demo_mode_write_protect);
799 DEF_TPG_ATTRIB(prod_mode_write_protect);
800 DEF_TPG_ATTRIB(demo_mode_discovery);
801 DEF_TPG_ATTRIB(default_erl);
802 DEF_TPG_ATTRIB(t10_pi);
803 DEF_TPG_ATTRIB(fabric_prot_type);
804 DEF_TPG_ATTRIB(tpg_enabled_sendtargets);
805 DEF_TPG_ATTRIB(login_keys_workaround);
806
807 static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
808         &iscsi_tpg_attrib_attr_authentication,
809         &iscsi_tpg_attrib_attr_login_timeout,
810         &iscsi_tpg_attrib_attr_netif_timeout,
811         &iscsi_tpg_attrib_attr_generate_node_acls,
812         &iscsi_tpg_attrib_attr_default_cmdsn_depth,
813         &iscsi_tpg_attrib_attr_cache_dynamic_acls,
814         &iscsi_tpg_attrib_attr_demo_mode_write_protect,
815         &iscsi_tpg_attrib_attr_prod_mode_write_protect,
816         &iscsi_tpg_attrib_attr_demo_mode_discovery,
817         &iscsi_tpg_attrib_attr_default_erl,
818         &iscsi_tpg_attrib_attr_t10_pi,
819         &iscsi_tpg_attrib_attr_fabric_prot_type,
820         &iscsi_tpg_attrib_attr_tpg_enabled_sendtargets,
821         &iscsi_tpg_attrib_attr_login_keys_workaround,
822         NULL,
823 };
824
825 /* End items for lio_target_tpg_attrib_cit */
826
827 /* Start items for lio_target_tpg_auth_cit */
828
829 #define __DEF_TPG_AUTH_STR(prefix, name, flags)                                 \
830 static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg, \
831                 char *page)                                                     \
832 {                                                                               \
833         struct iscsi_portal_group *tpg = container_of(se_tpg,                   \
834                                 struct iscsi_portal_group, tpg_se_tpg);         \
835         struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;                     \
836                                                                                 \
837         if (!capable(CAP_SYS_ADMIN))                                            \
838                 return -EPERM;                                                  \
839                                                                                 \
840         return snprintf(page, PAGE_SIZE, "%s\n", auth->name);                   \
841 }                                                                               \
842                                                                                 \
843 static ssize_t __iscsi_##prefix##_##name##_store(struct se_portal_group *se_tpg,\
844                 const char *page, size_t count)                                 \
845 {                                                                               \
846         struct iscsi_portal_group *tpg = container_of(se_tpg,                   \
847                                 struct iscsi_portal_group, tpg_se_tpg);         \
848         struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;                     \
849                                                                                 \
850         if (!capable(CAP_SYS_ADMIN))                                            \
851                 return -EPERM;                                                  \
852                                                                                 \
853         snprintf(auth->name, sizeof(auth->name), "%s", page);                   \
854         if (!(strncmp("NULL", auth->name, 4)))                                  \
855                 auth->naf_flags &= ~flags;                                      \
856         else                                                                    \
857                 auth->naf_flags |= flags;                                       \
858                                                                                 \
859         if ((auth->naf_flags & NAF_USERID_IN_SET) &&                            \
860             (auth->naf_flags & NAF_PASSWORD_IN_SET))                            \
861                 auth->authenticate_target = 1;                                  \
862         else                                                                    \
863                 auth->authenticate_target = 0;                                  \
864                                                                                 \
865         return count;                                                           \
866 }
867
868 #define DEF_TPG_AUTH_STR(name, flags)                                           \
869         __DEF_TPG_AUTH_STR(tpg_auth, name, flags)                               \
870 static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item,           \
871                 char *page)                                                     \
872 {                                                                               \
873         return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page);         \
874 }                                                                               \
875                                                                                 \
876 static ssize_t iscsi_tpg_auth_##name##_store(struct config_item *item,          \
877                 const char *page, size_t count)                                 \
878 {                                                                               \
879         return __iscsi_tpg_auth_##name##_store(auth_to_tpg(item), page, count); \
880 }                                                                               \
881                                                                                 \
882 CONFIGFS_ATTR(iscsi_tpg_auth_, name);
883
884
885 DEF_TPG_AUTH_STR(userid, NAF_USERID_SET);
886 DEF_TPG_AUTH_STR(password, NAF_PASSWORD_SET);
887 DEF_TPG_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
888 DEF_TPG_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
889
890 #define __DEF_TPG_AUTH_INT(prefix, name)                                        \
891 static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg, \
892                 char *page)                                                             \
893 {                                                                               \
894         struct iscsi_portal_group *tpg = container_of(se_tpg,                   \
895                                 struct iscsi_portal_group, tpg_se_tpg);         \
896         struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;                     \
897                                                                                 \
898         if (!capable(CAP_SYS_ADMIN))                                            \
899                 return -EPERM;                                                  \
900                                                                                 \
901         return snprintf(page, PAGE_SIZE, "%d\n", auth->name);                   \
902 }
903
904 #define DEF_TPG_AUTH_INT(name)                                                  \
905         __DEF_TPG_AUTH_INT(tpg_auth, name)                                      \
906 static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item,           \
907                 char *page) \
908 {                                                                               \
909         return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page);         \
910 }                                                                               \
911 CONFIGFS_ATTR_RO(iscsi_tpg_auth_, name);
912
913 DEF_TPG_AUTH_INT(authenticate_target);
914
915 static struct configfs_attribute *lio_target_tpg_auth_attrs[] = {
916         &iscsi_tpg_auth_attr_userid,
917         &iscsi_tpg_auth_attr_password,
918         &iscsi_tpg_auth_attr_authenticate_target,
919         &iscsi_tpg_auth_attr_userid_mutual,
920         &iscsi_tpg_auth_attr_password_mutual,
921         NULL,
922 };
923
924 /* End items for lio_target_tpg_auth_cit */
925
926 /* Start items for lio_target_tpg_param_cit */
927
928 #define DEF_TPG_PARAM(name)                                             \
929 static ssize_t iscsi_tpg_param_##name##_show(struct config_item *item,  \
930                 char *page)                                             \
931 {                                                                       \
932         struct se_portal_group *se_tpg = param_to_tpg(item);            \
933         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
934                         struct iscsi_portal_group, tpg_se_tpg);         \
935         struct iscsi_param *param;                                      \
936         ssize_t rb;                                                     \
937                                                                         \
938         if (iscsit_get_tpg(tpg) < 0)                                    \
939                 return -EINVAL;                                         \
940                                                                         \
941         param = iscsi_find_param_from_key(__stringify(name),            \
942                                 tpg->param_list);                       \
943         if (!param) {                                                   \
944                 iscsit_put_tpg(tpg);                                    \
945                 return -EINVAL;                                         \
946         }                                                               \
947         rb = snprintf(page, PAGE_SIZE, "%s\n", param->value);           \
948                                                                         \
949         iscsit_put_tpg(tpg);                                            \
950         return rb;                                                      \
951 }                                                                       \
952 static ssize_t iscsi_tpg_param_##name##_store(struct config_item *item, \
953                 const char *page, size_t count)                         \
954 {                                                                       \
955         struct se_portal_group *se_tpg = param_to_tpg(item);            \
956         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
957                         struct iscsi_portal_group, tpg_se_tpg);         \
958         char *buf;                                                      \
959         int ret, len;                                                   \
960                                                                         \
961         buf = kzalloc(PAGE_SIZE, GFP_KERNEL);                           \
962         if (!buf)                                                       \
963                 return -ENOMEM;                                         \
964         len = snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page);       \
965         if (isspace(buf[len-1]))                                        \
966                 buf[len-1] = '\0'; /* Kill newline */                   \
967                                                                         \
968         if (iscsit_get_tpg(tpg) < 0) {                                  \
969                 kfree(buf);                                             \
970                 return -EINVAL;                                         \
971         }                                                               \
972                                                                         \
973         ret = iscsi_change_param_value(buf, tpg->param_list, 1);        \
974         if (ret < 0)                                                    \
975                 goto out;                                               \
976                                                                         \
977         kfree(buf);                                                     \
978         iscsit_put_tpg(tpg);                                            \
979         return count;                                                   \
980 out:                                                                    \
981         kfree(buf);                                                     \
982         iscsit_put_tpg(tpg);                                            \
983         return -EINVAL;                                                 \
984 }                                                                       \
985 CONFIGFS_ATTR(iscsi_tpg_param_, name)
986
987 DEF_TPG_PARAM(AuthMethod);
988 DEF_TPG_PARAM(HeaderDigest);
989 DEF_TPG_PARAM(DataDigest);
990 DEF_TPG_PARAM(MaxConnections);
991 DEF_TPG_PARAM(TargetAlias);
992 DEF_TPG_PARAM(InitialR2T);
993 DEF_TPG_PARAM(ImmediateData);
994 DEF_TPG_PARAM(MaxRecvDataSegmentLength);
995 DEF_TPG_PARAM(MaxXmitDataSegmentLength);
996 DEF_TPG_PARAM(MaxBurstLength);
997 DEF_TPG_PARAM(FirstBurstLength);
998 DEF_TPG_PARAM(DefaultTime2Wait);
999 DEF_TPG_PARAM(DefaultTime2Retain);
1000 DEF_TPG_PARAM(MaxOutstandingR2T);
1001 DEF_TPG_PARAM(DataPDUInOrder);
1002 DEF_TPG_PARAM(DataSequenceInOrder);
1003 DEF_TPG_PARAM(ErrorRecoveryLevel);
1004 DEF_TPG_PARAM(IFMarker);
1005 DEF_TPG_PARAM(OFMarker);
1006 DEF_TPG_PARAM(IFMarkInt);
1007 DEF_TPG_PARAM(OFMarkInt);
1008
1009 static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
1010         &iscsi_tpg_param_attr_AuthMethod,
1011         &iscsi_tpg_param_attr_HeaderDigest,
1012         &iscsi_tpg_param_attr_DataDigest,
1013         &iscsi_tpg_param_attr_MaxConnections,
1014         &iscsi_tpg_param_attr_TargetAlias,
1015         &iscsi_tpg_param_attr_InitialR2T,
1016         &iscsi_tpg_param_attr_ImmediateData,
1017         &iscsi_tpg_param_attr_MaxRecvDataSegmentLength,
1018         &iscsi_tpg_param_attr_MaxXmitDataSegmentLength,
1019         &iscsi_tpg_param_attr_MaxBurstLength,
1020         &iscsi_tpg_param_attr_FirstBurstLength,
1021         &iscsi_tpg_param_attr_DefaultTime2Wait,
1022         &iscsi_tpg_param_attr_DefaultTime2Retain,
1023         &iscsi_tpg_param_attr_MaxOutstandingR2T,
1024         &iscsi_tpg_param_attr_DataPDUInOrder,
1025         &iscsi_tpg_param_attr_DataSequenceInOrder,
1026         &iscsi_tpg_param_attr_ErrorRecoveryLevel,
1027         &iscsi_tpg_param_attr_IFMarker,
1028         &iscsi_tpg_param_attr_OFMarker,
1029         &iscsi_tpg_param_attr_IFMarkInt,
1030         &iscsi_tpg_param_attr_OFMarkInt,
1031         NULL,
1032 };
1033
1034 /* End items for lio_target_tpg_param_cit */
1035
1036 /* Start items for lio_target_tpg_cit */
1037
1038 static ssize_t lio_target_tpg_enable_show(struct config_item *item, char *page)
1039 {
1040         struct se_portal_group *se_tpg = to_tpg(item);
1041         struct iscsi_portal_group *tpg = container_of(se_tpg,
1042                         struct iscsi_portal_group, tpg_se_tpg);
1043         ssize_t len;
1044
1045         spin_lock(&tpg->tpg_state_lock);
1046         len = sprintf(page, "%d\n",
1047                         (tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1048         spin_unlock(&tpg->tpg_state_lock);
1049
1050         return len;
1051 }
1052
1053 static ssize_t lio_target_tpg_enable_store(struct config_item *item,
1054                 const char *page, size_t count)
1055 {
1056         struct se_portal_group *se_tpg = to_tpg(item);
1057         struct iscsi_portal_group *tpg = container_of(se_tpg,
1058                         struct iscsi_portal_group, tpg_se_tpg);
1059         u32 op;
1060         int ret;
1061
1062         ret = kstrtou32(page, 0, &op);
1063         if (ret)
1064                 return ret;
1065         if ((op != 1) && (op != 0)) {
1066                 pr_err("Illegal value for tpg_enable: %u\n", op);
1067                 return -EINVAL;
1068         }
1069
1070         ret = iscsit_get_tpg(tpg);
1071         if (ret < 0)
1072                 return -EINVAL;
1073
1074         if (op) {
1075                 ret = iscsit_tpg_enable_portal_group(tpg);
1076                 if (ret < 0)
1077                         goto out;
1078         } else {
1079                 /*
1080                  * iscsit_tpg_disable_portal_group() assumes force=1
1081                  */
1082                 ret = iscsit_tpg_disable_portal_group(tpg, 1);
1083                 if (ret < 0)
1084                         goto out;
1085         }
1086
1087         iscsit_put_tpg(tpg);
1088         return count;
1089 out:
1090         iscsit_put_tpg(tpg);
1091         return -EINVAL;
1092 }
1093
1094
1095 static ssize_t lio_target_tpg_dynamic_sessions_show(struct config_item *item,
1096                 char *page)
1097 {
1098         return target_show_dynamic_sessions(to_tpg(item), page);
1099 }
1100
1101 CONFIGFS_ATTR(lio_target_tpg_, enable);
1102 CONFIGFS_ATTR_RO(lio_target_tpg_, dynamic_sessions);
1103
1104 static struct configfs_attribute *lio_target_tpg_attrs[] = {
1105         &lio_target_tpg_attr_enable,
1106         &lio_target_tpg_attr_dynamic_sessions,
1107         NULL,
1108 };
1109
1110 /* End items for lio_target_tpg_cit */
1111
1112 /* Start items for lio_target_tiqn_cit */
1113
1114 static struct se_portal_group *lio_target_tiqn_addtpg(
1115         struct se_wwn *wwn,
1116         struct config_group *group,
1117         const char *name)
1118 {
1119         struct iscsi_portal_group *tpg;
1120         struct iscsi_tiqn *tiqn;
1121         char *tpgt_str;
1122         int ret;
1123         u16 tpgt;
1124
1125         tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1126         /*
1127          * Only tpgt_# directory groups can be created below
1128          * target/iscsi/iqn.superturodiskarry/
1129          */
1130         tpgt_str = strstr(name, "tpgt_");
1131         if (!tpgt_str) {
1132                 pr_err("Unable to locate \"tpgt_#\" directory"
1133                                 " group\n");
1134                 return NULL;
1135         }
1136         tpgt_str += 5; /* Skip ahead of "tpgt_" */
1137         ret = kstrtou16(tpgt_str, 0, &tpgt);
1138         if (ret)
1139                 return NULL;
1140
1141         tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1142         if (!tpg)
1143                 return NULL;
1144
1145         ret = core_tpg_register(wwn, &tpg->tpg_se_tpg, SCSI_PROTOCOL_ISCSI);
1146         if (ret < 0)
1147                 goto free_out;
1148
1149         ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1150         if (ret != 0)
1151                 goto out;
1152
1153         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1154         pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1155                         name);
1156         return &tpg->tpg_se_tpg;
1157 out:
1158         core_tpg_deregister(&tpg->tpg_se_tpg);
1159 free_out:
1160         kfree(tpg);
1161         return NULL;
1162 }
1163
1164 static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1165 {
1166         struct iscsi_portal_group *tpg;
1167         struct iscsi_tiqn *tiqn;
1168
1169         tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1170         tiqn = tpg->tpg_tiqn;
1171         /*
1172          * iscsit_tpg_del_portal_group() assumes force=1
1173          */
1174         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1175         iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1176 }
1177
1178 /* End items for lio_target_tiqn_cit */
1179
1180 /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1181
1182 static ssize_t lio_target_wwn_lio_version_show(struct config_item *item,
1183                 char *page)
1184 {
1185         return sprintf(page, "Datera Inc. iSCSI Target "ISCSIT_VERSION"\n");
1186 }
1187
1188 CONFIGFS_ATTR_RO(lio_target_wwn_, lio_version);
1189
1190 static struct configfs_attribute *lio_target_wwn_attrs[] = {
1191         &lio_target_wwn_attr_lio_version,
1192         NULL,
1193 };
1194
1195 static struct se_wwn *lio_target_call_coreaddtiqn(
1196         struct target_fabric_configfs *tf,
1197         struct config_group *group,
1198         const char *name)
1199 {
1200         struct iscsi_tiqn *tiqn;
1201
1202         tiqn = iscsit_add_tiqn((unsigned char *)name);
1203         if (IS_ERR(tiqn))
1204                 return ERR_CAST(tiqn);
1205
1206         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1207         pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1208                         " %s\n", name);
1209         return &tiqn->tiqn_wwn;
1210 }
1211
1212 static void lio_target_add_wwn_groups(struct se_wwn *wwn)
1213 {
1214         struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1215
1216         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1217                         "iscsi_instance", &iscsi_stat_instance_cit);
1218         configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1219                         &tiqn->tiqn_wwn.fabric_stat_group);
1220
1221         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1222                         "iscsi_sess_err", &iscsi_stat_sess_err_cit);
1223         configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1224                         &tiqn->tiqn_wwn.fabric_stat_group);
1225
1226         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1227                         "iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
1228         configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1229                         &tiqn->tiqn_wwn.fabric_stat_group);
1230
1231         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1232                         "iscsi_login_stats", &iscsi_stat_login_cit);
1233         configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1234                         &tiqn->tiqn_wwn.fabric_stat_group);
1235
1236         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1237                         "iscsi_logout_stats", &iscsi_stat_logout_cit);
1238         configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1239                         &tiqn->tiqn_wwn.fabric_stat_group);
1240 }
1241
1242 static void lio_target_call_coredeltiqn(
1243         struct se_wwn *wwn)
1244 {
1245         struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1246
1247         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1248                         tiqn->tiqn);
1249         iscsit_del_tiqn(tiqn);
1250 }
1251
1252 /* End LIO-Target TIQN struct contig_lio_target_cit */
1253
1254 /* Start lio_target_discovery_auth_cit */
1255
1256 #define DEF_DISC_AUTH_STR(name, flags)                                  \
1257         __DEF_NACL_AUTH_STR(disc, name, flags)                          \
1258 static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1259 {                                                                       \
1260         return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl,\
1261                 page);                                                  \
1262 }                                                                       \
1263 static ssize_t iscsi_disc_##name##_store(struct config_item *item,      \
1264                 const char *page, size_t count)                         \
1265 {                                                                       \
1266         return __iscsi_disc_##name##_store(&iscsit_global->discovery_acl,       \
1267                 page, count);                                           \
1268                                                                         \
1269 }                                                                       \
1270 CONFIGFS_ATTR(iscsi_disc_, name)
1271
1272 DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
1273 DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
1274 DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1275 DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1276
1277 #define DEF_DISC_AUTH_INT(name)                                         \
1278         __DEF_NACL_AUTH_INT(disc, name)                                 \
1279 static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1280 {                                                                       \
1281         return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl, \
1282                         page);                                          \
1283 }                                                                       \
1284 CONFIGFS_ATTR_RO(iscsi_disc_, name)
1285
1286 DEF_DISC_AUTH_INT(authenticate_target);
1287
1288
1289 static ssize_t iscsi_disc_enforce_discovery_auth_show(struct config_item *item,
1290                 char *page)
1291 {
1292         struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1293
1294         return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1295 }
1296
1297 static ssize_t iscsi_disc_enforce_discovery_auth_store(struct config_item *item,
1298                 const char *page, size_t count)
1299 {
1300         struct iscsi_param *param;
1301         struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1302         u32 op;
1303         int err;
1304
1305         err = kstrtou32(page, 0, &op);
1306         if (err)
1307                 return -EINVAL;
1308         if ((op != 1) && (op != 0)) {
1309                 pr_err("Illegal value for enforce_discovery_auth:"
1310                                 " %u\n", op);
1311                 return -EINVAL;
1312         }
1313
1314         if (!discovery_tpg) {
1315                 pr_err("iscsit_global->discovery_tpg is NULL\n");
1316                 return -EINVAL;
1317         }
1318
1319         param = iscsi_find_param_from_key(AUTHMETHOD,
1320                                 discovery_tpg->param_list);
1321         if (!param)
1322                 return -EINVAL;
1323
1324         if (op) {
1325                 /*
1326                  * Reset the AuthMethod key to CHAP.
1327                  */
1328                 if (iscsi_update_param_value(param, CHAP) < 0)
1329                         return -EINVAL;
1330
1331                 discovery_tpg->tpg_attrib.authentication = 1;
1332                 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1333                 pr_debug("LIO-CORE[0] Successfully enabled"
1334                         " authentication enforcement for iSCSI"
1335                         " Discovery TPG\n");
1336         } else {
1337                 /*
1338                  * Reset the AuthMethod key to CHAP,None
1339                  */
1340                 if (iscsi_update_param_value(param, "CHAP,None") < 0)
1341                         return -EINVAL;
1342
1343                 discovery_tpg->tpg_attrib.authentication = 0;
1344                 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1345                 pr_debug("LIO-CORE[0] Successfully disabled"
1346                         " authentication enforcement for iSCSI"
1347                         " Discovery TPG\n");
1348         }
1349
1350         return count;
1351 }
1352
1353 CONFIGFS_ATTR(iscsi_disc_, enforce_discovery_auth);
1354
1355 static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
1356         &iscsi_disc_attr_userid,
1357         &iscsi_disc_attr_password,
1358         &iscsi_disc_attr_authenticate_target,
1359         &iscsi_disc_attr_userid_mutual,
1360         &iscsi_disc_attr_password_mutual,
1361         &iscsi_disc_attr_enforce_discovery_auth,
1362         NULL,
1363 };
1364
1365 /* End lio_target_discovery_auth_cit */
1366
1367 /* Start functions for target_core_fabric_ops */
1368
1369 static char *iscsi_get_fabric_name(void)
1370 {
1371         return "iSCSI";
1372 }
1373
1374 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1375 {
1376         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1377
1378         return cmd->i_state;
1379 }
1380
1381 static u32 lio_sess_get_index(struct se_session *se_sess)
1382 {
1383         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1384
1385         return sess->session_index;
1386 }
1387
1388 static u32 lio_sess_get_initiator_sid(
1389         struct se_session *se_sess,
1390         unsigned char *buf,
1391         u32 size)
1392 {
1393         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1394         /*
1395          * iSCSI Initiator Session Identifier from RFC-3720.
1396          */
1397         return snprintf(buf, size, "%6phN", sess->isid);
1398 }
1399
1400 static int lio_queue_data_in(struct se_cmd *se_cmd)
1401 {
1402         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1403
1404         cmd->i_state = ISTATE_SEND_DATAIN;
1405         cmd->conn->conn_transport->iscsit_queue_data_in(cmd->conn, cmd);
1406
1407         return 0;
1408 }
1409
1410 static int lio_write_pending(struct se_cmd *se_cmd)
1411 {
1412         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1413         struct iscsi_conn *conn = cmd->conn;
1414
1415         if (!cmd->immediate_data && !cmd->unsolicited_data)
1416                 return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1417
1418         return 0;
1419 }
1420
1421 static int lio_write_pending_status(struct se_cmd *se_cmd)
1422 {
1423         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1424         int ret;
1425
1426         spin_lock_bh(&cmd->istate_lock);
1427         ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1428         spin_unlock_bh(&cmd->istate_lock);
1429
1430         return ret;
1431 }
1432
1433 static int lio_queue_status(struct se_cmd *se_cmd)
1434 {
1435         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1436
1437         cmd->i_state = ISTATE_SEND_STATUS;
1438
1439         if (cmd->se_cmd.scsi_status || cmd->sense_reason) {
1440                 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1441                 return 0;
1442         }
1443         cmd->conn->conn_transport->iscsit_queue_status(cmd->conn, cmd);
1444
1445         return 0;
1446 }
1447
1448 static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
1449 {
1450         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1451
1452         cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1453         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1454 }
1455
1456 static void lio_aborted_task(struct se_cmd *se_cmd)
1457 {
1458         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1459
1460         cmd->conn->conn_transport->iscsit_aborted_task(cmd->conn, cmd);
1461 }
1462
1463 static inline struct iscsi_portal_group *iscsi_tpg(struct se_portal_group *se_tpg)
1464 {
1465         return container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1466 }
1467
1468 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1469 {
1470         return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn;
1471 }
1472
1473 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1474 {
1475         return iscsi_tpg(se_tpg)->tpgt;
1476 }
1477
1478 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1479 {
1480         return iscsi_tpg(se_tpg)->tpg_attrib.default_cmdsn_depth;
1481 }
1482
1483 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1484 {
1485         return iscsi_tpg(se_tpg)->tpg_attrib.generate_node_acls;
1486 }
1487
1488 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1489 {
1490         return iscsi_tpg(se_tpg)->tpg_attrib.cache_dynamic_acls;
1491 }
1492
1493 static int lio_tpg_check_demo_mode_write_protect(
1494         struct se_portal_group *se_tpg)
1495 {
1496         return iscsi_tpg(se_tpg)->tpg_attrib.demo_mode_write_protect;
1497 }
1498
1499 static int lio_tpg_check_prod_mode_write_protect(
1500         struct se_portal_group *se_tpg)
1501 {
1502         return iscsi_tpg(se_tpg)->tpg_attrib.prod_mode_write_protect;
1503 }
1504
1505 static int lio_tpg_check_prot_fabric_only(
1506         struct se_portal_group *se_tpg)
1507 {
1508         /*
1509          * Only report fabric_prot_type if t10_pi has also been enabled
1510          * for incoming ib_isert sessions.
1511          */
1512         if (!iscsi_tpg(se_tpg)->tpg_attrib.t10_pi)
1513                 return 0;
1514         return iscsi_tpg(se_tpg)->tpg_attrib.fabric_prot_type;
1515 }
1516
1517 /*
1518  * This function calls iscsit_inc_session_usage_count() on the
1519  * struct iscsi_session in question.
1520  */
1521 static void lio_tpg_close_session(struct se_session *se_sess)
1522 {
1523         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1524         struct se_portal_group *se_tpg = &sess->tpg->tpg_se_tpg;
1525
1526         spin_lock_bh(&se_tpg->session_lock);
1527         spin_lock(&sess->conn_lock);
1528         if (atomic_read(&sess->session_fall_back_to_erl0) ||
1529             atomic_read(&sess->session_logout) ||
1530             atomic_read(&sess->session_close) ||
1531             (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1532                 spin_unlock(&sess->conn_lock);
1533                 spin_unlock_bh(&se_tpg->session_lock);
1534                 return;
1535         }
1536         iscsit_inc_session_usage_count(sess);
1537         atomic_set(&sess->session_reinstatement, 1);
1538         atomic_set(&sess->session_fall_back_to_erl0, 1);
1539         atomic_set(&sess->session_close, 1);
1540         spin_unlock(&sess->conn_lock);
1541
1542         iscsit_stop_time2retain_timer(sess);
1543         spin_unlock_bh(&se_tpg->session_lock);
1544
1545         iscsit_stop_session(sess, 1, 1);
1546         iscsit_dec_session_usage_count(sess);
1547 }
1548
1549 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1550 {
1551         return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn_index;
1552 }
1553
1554 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1555 {
1556         struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1557                                 se_node_acl);
1558         struct se_portal_group *se_tpg = se_acl->se_tpg;
1559         struct iscsi_portal_group *tpg = container_of(se_tpg,
1560                                 struct iscsi_portal_group, tpg_se_tpg);
1561
1562         acl->node_attrib.nacl = acl;
1563         iscsit_set_default_node_attribues(acl, tpg);
1564 }
1565
1566 static int lio_check_stop_free(struct se_cmd *se_cmd)
1567 {
1568         return target_put_sess_cmd(se_cmd);
1569 }
1570
1571 static void lio_release_cmd(struct se_cmd *se_cmd)
1572 {
1573         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1574
1575         pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
1576         iscsit_release_cmd(cmd);
1577 }
1578
1579 const struct target_core_fabric_ops iscsi_ops = {
1580         .module                         = THIS_MODULE,
1581         .name                           = "iscsi",
1582         .node_acl_size                  = sizeof(struct iscsi_node_acl),
1583         .get_fabric_name                = iscsi_get_fabric_name,
1584         .tpg_get_wwn                    = lio_tpg_get_endpoint_wwn,
1585         .tpg_get_tag                    = lio_tpg_get_tag,
1586         .tpg_get_default_depth          = lio_tpg_get_default_depth,
1587         .tpg_check_demo_mode            = lio_tpg_check_demo_mode,
1588         .tpg_check_demo_mode_cache      = lio_tpg_check_demo_mode_cache,
1589         .tpg_check_demo_mode_write_protect =
1590                         lio_tpg_check_demo_mode_write_protect,
1591         .tpg_check_prod_mode_write_protect =
1592                         lio_tpg_check_prod_mode_write_protect,
1593         .tpg_check_prot_fabric_only     = &lio_tpg_check_prot_fabric_only,
1594         .tpg_get_inst_index             = lio_tpg_get_inst_index,
1595         .check_stop_free                = lio_check_stop_free,
1596         .release_cmd                    = lio_release_cmd,
1597         .close_session                  = lio_tpg_close_session,
1598         .sess_get_index                 = lio_sess_get_index,
1599         .sess_get_initiator_sid         = lio_sess_get_initiator_sid,
1600         .write_pending                  = lio_write_pending,
1601         .write_pending_status           = lio_write_pending_status,
1602         .set_default_node_attributes    = lio_set_default_node_attributes,
1603         .get_cmd_state                  = iscsi_get_cmd_state,
1604         .queue_data_in                  = lio_queue_data_in,
1605         .queue_status                   = lio_queue_status,
1606         .queue_tm_rsp                   = lio_queue_tm_rsp,
1607         .aborted_task                   = lio_aborted_task,
1608         .fabric_make_wwn                = lio_target_call_coreaddtiqn,
1609         .fabric_drop_wwn                = lio_target_call_coredeltiqn,
1610         .add_wwn_groups                 = lio_target_add_wwn_groups,
1611         .fabric_make_tpg                = lio_target_tiqn_addtpg,
1612         .fabric_drop_tpg                = lio_target_tiqn_deltpg,
1613         .fabric_make_np                 = lio_target_call_addnptotpg,
1614         .fabric_drop_np                 = lio_target_call_delnpfromtpg,
1615         .fabric_init_nodeacl            = lio_target_init_nodeacl,
1616
1617         .tfc_discovery_attrs            = lio_target_discovery_auth_attrs,
1618         .tfc_wwn_attrs                  = lio_target_wwn_attrs,
1619         .tfc_tpg_base_attrs             = lio_target_tpg_attrs,
1620         .tfc_tpg_attrib_attrs           = lio_target_tpg_attrib_attrs,
1621         .tfc_tpg_auth_attrs             = lio_target_tpg_auth_attrs,
1622         .tfc_tpg_param_attrs            = lio_target_tpg_param_attrs,
1623         .tfc_tpg_np_base_attrs          = lio_target_portal_attrs,
1624         .tfc_tpg_nacl_base_attrs        = lio_target_initiator_attrs,
1625         .tfc_tpg_nacl_attrib_attrs      = lio_target_nacl_attrib_attrs,
1626         .tfc_tpg_nacl_auth_attrs        = lio_target_nacl_auth_attrs,
1627         .tfc_tpg_nacl_param_attrs       = lio_target_nacl_param_attrs,
1628 };