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