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