sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target_configfs.c
blobbf40f03755ddc50697652ccde864d40df840fa0b
1 /*******************************************************************************
2 * This file contains the configfs implementation for iSCSI Target mode
3 * from the LIO-Target Project.
5 * (c) Copyright 2007-2013 Datera, Inc.
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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 ****************************************************************************/
20 #include <linux/configfs.h>
21 #include <linux/ctype.h>
22 #include <linux/export.h>
23 #include <linux/inet.h>
24 #include <linux/module.h>
25 #include <net/ipv6.h>
26 #include <target/target_core_base.h>
27 #include <target/target_core_fabric.h>
28 #include <target/iscsi/iscsi_transport.h>
29 #include <target/iscsi/iscsi_target_core.h>
30 #include "iscsi_target_parameters.h"
31 #include "iscsi_target_device.h"
32 #include "iscsi_target_erl0.h"
33 #include "iscsi_target_nodeattrib.h"
34 #include "iscsi_target_tpg.h"
35 #include "iscsi_target_util.h"
36 #include "iscsi_target.h"
37 #include <target/iscsi/iscsi_target_stat.h>
40 /* Start items for lio_target_portal_cit */
42 static inline struct iscsi_tpg_np *to_iscsi_tpg_np(struct config_item *item)
44 return container_of(to_tpg_np(item), struct iscsi_tpg_np, se_tpg_np);
47 static ssize_t lio_target_np_driver_show(struct config_item *item, char *page,
48 enum iscsit_transport_type type)
50 struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
51 struct iscsi_tpg_np *tpg_np_new;
52 ssize_t rb;
54 tpg_np_new = iscsit_tpg_locate_child_np(tpg_np, type);
55 if (tpg_np_new)
56 rb = sprintf(page, "1\n");
57 else
58 rb = sprintf(page, "0\n");
60 return rb;
63 static ssize_t lio_target_np_driver_store(struct config_item *item,
64 const char *page, size_t count, enum iscsit_transport_type type,
65 const char *mod_name)
67 struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
68 struct iscsi_np *np;
69 struct iscsi_portal_group *tpg;
70 struct iscsi_tpg_np *tpg_np_new = NULL;
71 u32 op;
72 int rc;
74 rc = kstrtou32(page, 0, &op);
75 if (rc)
76 return rc;
77 if ((op != 1) && (op != 0)) {
78 pr_err("Illegal value for tpg_enable: %u\n", op);
79 return -EINVAL;
81 np = tpg_np->tpg_np;
82 if (!np) {
83 pr_err("Unable to locate struct iscsi_np from"
84 " struct iscsi_tpg_np\n");
85 return -EINVAL;
88 tpg = tpg_np->tpg;
89 if (iscsit_get_tpg(tpg) < 0)
90 return -EINVAL;
92 if (op) {
93 if (strlen(mod_name)) {
94 rc = request_module(mod_name);
95 if (rc != 0) {
96 pr_warn("Unable to request_module for %s\n",
97 mod_name);
98 rc = 0;
102 tpg_np_new = iscsit_tpg_add_network_portal(tpg,
103 &np->np_sockaddr, tpg_np, type);
104 if (IS_ERR(tpg_np_new)) {
105 rc = PTR_ERR(tpg_np_new);
106 goto out;
108 } else {
109 tpg_np_new = iscsit_tpg_locate_child_np(tpg_np, type);
110 if (tpg_np_new) {
111 rc = iscsit_tpg_del_network_portal(tpg, tpg_np_new);
112 if (rc < 0)
113 goto out;
117 iscsit_put_tpg(tpg);
118 return count;
119 out:
120 iscsit_put_tpg(tpg);
121 return rc;
124 static ssize_t lio_target_np_iser_show(struct config_item *item, char *page)
126 return lio_target_np_driver_show(item, page, ISCSI_INFINIBAND);
129 static ssize_t lio_target_np_iser_store(struct config_item *item,
130 const char *page, size_t count)
132 return lio_target_np_driver_store(item, page, count,
133 ISCSI_INFINIBAND, "ib_isert");
135 CONFIGFS_ATTR(lio_target_np_, iser);
137 static ssize_t lio_target_np_cxgbit_show(struct config_item *item, char *page)
139 return lio_target_np_driver_show(item, page, ISCSI_CXGBIT);
142 static ssize_t lio_target_np_cxgbit_store(struct config_item *item,
143 const char *page, size_t count)
145 return lio_target_np_driver_store(item, page, count,
146 ISCSI_CXGBIT, "cxgbit");
148 CONFIGFS_ATTR(lio_target_np_, cxgbit);
150 static struct configfs_attribute *lio_target_portal_attrs[] = {
151 &lio_target_np_attr_iser,
152 &lio_target_np_attr_cxgbit,
153 NULL,
156 /* Stop items for lio_target_portal_cit */
158 /* Start items for lio_target_np_cit */
160 #define MAX_PORTAL_LEN 256
162 static struct se_tpg_np *lio_target_call_addnptotpg(
163 struct se_portal_group *se_tpg,
164 struct config_group *group,
165 const char *name)
167 struct iscsi_portal_group *tpg;
168 struct iscsi_tpg_np *tpg_np;
169 char *str, *str2, *ip_str, *port_str;
170 struct sockaddr_storage sockaddr;
171 struct sockaddr_in *sock_in;
172 struct sockaddr_in6 *sock_in6;
173 unsigned long port;
174 int ret;
175 char buf[MAX_PORTAL_LEN + 1];
177 if (strlen(name) > MAX_PORTAL_LEN) {
178 pr_err("strlen(name): %d exceeds MAX_PORTAL_LEN: %d\n",
179 (int)strlen(name), MAX_PORTAL_LEN);
180 return ERR_PTR(-EOVERFLOW);
182 memset(buf, 0, MAX_PORTAL_LEN + 1);
183 snprintf(buf, MAX_PORTAL_LEN + 1, "%s", name);
185 memset(&sockaddr, 0, sizeof(struct sockaddr_storage));
187 str = strstr(buf, "[");
188 if (str) {
189 const char *end;
191 str2 = strstr(str, "]");
192 if (!str2) {
193 pr_err("Unable to locate trailing \"]\""
194 " in IPv6 iSCSI network portal address\n");
195 return ERR_PTR(-EINVAL);
197 str++; /* Skip over leading "[" */
198 *str2 = '\0'; /* Terminate the unbracketed IPv6 address */
199 str2++; /* Skip over the \0 */
200 port_str = strstr(str2, ":");
201 if (!port_str) {
202 pr_err("Unable to locate \":port\""
203 " in IPv6 iSCSI network portal address\n");
204 return ERR_PTR(-EINVAL);
206 *port_str = '\0'; /* Terminate string for IP */
207 port_str++; /* Skip over ":" */
209 ret = kstrtoul(port_str, 0, &port);
210 if (ret < 0) {
211 pr_err("kstrtoul() failed for port_str: %d\n", ret);
212 return ERR_PTR(ret);
214 sock_in6 = (struct sockaddr_in6 *)&sockaddr;
215 sock_in6->sin6_family = AF_INET6;
216 sock_in6->sin6_port = htons((unsigned short)port);
217 ret = in6_pton(str, -1,
218 (void *)&sock_in6->sin6_addr.in6_u, -1, &end);
219 if (ret <= 0) {
220 pr_err("in6_pton returned: %d\n", ret);
221 return ERR_PTR(-EINVAL);
223 } else {
224 str = ip_str = &buf[0];
225 port_str = strstr(ip_str, ":");
226 if (!port_str) {
227 pr_err("Unable to locate \":port\""
228 " in IPv4 iSCSI network portal address\n");
229 return ERR_PTR(-EINVAL);
231 *port_str = '\0'; /* Terminate string for IP */
232 port_str++; /* Skip over ":" */
234 ret = kstrtoul(port_str, 0, &port);
235 if (ret < 0) {
236 pr_err("kstrtoul() failed for port_str: %d\n", ret);
237 return ERR_PTR(ret);
239 sock_in = (struct sockaddr_in *)&sockaddr;
240 sock_in->sin_family = AF_INET;
241 sock_in->sin_port = htons((unsigned short)port);
242 sock_in->sin_addr.s_addr = in_aton(ip_str);
244 tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
245 ret = iscsit_get_tpg(tpg);
246 if (ret < 0)
247 return ERR_PTR(-EINVAL);
249 pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
250 " PORTAL: %s\n",
251 config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
252 tpg->tpgt, name);
254 * Assume ISCSI_TCP by default. Other network portals for other
255 * iSCSI fabrics:
257 * Traditional iSCSI over SCTP (initial support)
258 * iSER/TCP (TODO, hardware available)
259 * iSER/SCTP (TODO, software emulation with osc-iwarp)
260 * iSER/IB (TODO, hardware available)
262 * can be enabled with attributes under
263 * sys/kernel/config/iscsi/$IQN/$TPG/np/$IP:$PORT/
266 tpg_np = iscsit_tpg_add_network_portal(tpg, &sockaddr, NULL,
267 ISCSI_TCP);
268 if (IS_ERR(tpg_np)) {
269 iscsit_put_tpg(tpg);
270 return ERR_CAST(tpg_np);
272 pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
274 iscsit_put_tpg(tpg);
275 return &tpg_np->se_tpg_np;
278 static void lio_target_call_delnpfromtpg(
279 struct se_tpg_np *se_tpg_np)
281 struct iscsi_portal_group *tpg;
282 struct iscsi_tpg_np *tpg_np;
283 struct se_portal_group *se_tpg;
284 int ret;
286 tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
287 tpg = tpg_np->tpg;
288 ret = iscsit_get_tpg(tpg);
289 if (ret < 0)
290 return;
292 se_tpg = &tpg->tpg_se_tpg;
293 pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s TPGT: %hu"
294 " PORTAL: %pISpc\n", config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
295 tpg->tpgt, &tpg_np->tpg_np->np_sockaddr);
297 ret = iscsit_tpg_del_network_portal(tpg, tpg_np);
298 if (ret < 0)
299 goto out;
301 pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
302 out:
303 iscsit_put_tpg(tpg);
306 /* End items for lio_target_np_cit */
308 /* Start items for lio_target_nacl_attrib_cit */
310 #define ISCSI_NACL_ATTR(name) \
311 static ssize_t iscsi_nacl_attrib_##name##_show(struct config_item *item,\
312 char *page) \
314 struct se_node_acl *se_nacl = attrib_to_nacl(item); \
315 struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
316 se_node_acl); \
318 return sprintf(page, "%u\n", nacl->node_attrib.name); \
321 static ssize_t iscsi_nacl_attrib_##name##_store(struct config_item *item,\
322 const char *page, size_t count) \
324 struct se_node_acl *se_nacl = attrib_to_nacl(item); \
325 struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
326 se_node_acl); \
327 u32 val; \
328 int ret; \
330 ret = kstrtou32(page, 0, &val); \
331 if (ret) \
332 return ret; \
333 ret = iscsit_na_##name(nacl, val); \
334 if (ret < 0) \
335 return ret; \
337 return count; \
340 CONFIGFS_ATTR(iscsi_nacl_attrib_, name)
342 ISCSI_NACL_ATTR(dataout_timeout);
343 ISCSI_NACL_ATTR(dataout_timeout_retries);
344 ISCSI_NACL_ATTR(default_erl);
345 ISCSI_NACL_ATTR(nopin_timeout);
346 ISCSI_NACL_ATTR(nopin_response_timeout);
347 ISCSI_NACL_ATTR(random_datain_pdu_offsets);
348 ISCSI_NACL_ATTR(random_datain_seq_offsets);
349 ISCSI_NACL_ATTR(random_r2t_offsets);
351 static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
352 &iscsi_nacl_attrib_attr_dataout_timeout,
353 &iscsi_nacl_attrib_attr_dataout_timeout_retries,
354 &iscsi_nacl_attrib_attr_default_erl,
355 &iscsi_nacl_attrib_attr_nopin_timeout,
356 &iscsi_nacl_attrib_attr_nopin_response_timeout,
357 &iscsi_nacl_attrib_attr_random_datain_pdu_offsets,
358 &iscsi_nacl_attrib_attr_random_datain_seq_offsets,
359 &iscsi_nacl_attrib_attr_random_r2t_offsets,
360 NULL,
363 /* End items for lio_target_nacl_attrib_cit */
365 /* Start items for lio_target_nacl_auth_cit */
367 #define __DEF_NACL_AUTH_STR(prefix, name, flags) \
368 static ssize_t __iscsi_##prefix##_##name##_show( \
369 struct iscsi_node_acl *nacl, \
370 char *page) \
372 struct iscsi_node_auth *auth = &nacl->node_auth; \
374 if (!capable(CAP_SYS_ADMIN)) \
375 return -EPERM; \
376 return snprintf(page, PAGE_SIZE, "%s\n", auth->name); \
379 static ssize_t __iscsi_##prefix##_##name##_store( \
380 struct iscsi_node_acl *nacl, \
381 const char *page, \
382 size_t count) \
384 struct iscsi_node_auth *auth = &nacl->node_auth; \
386 if (!capable(CAP_SYS_ADMIN)) \
387 return -EPERM; \
388 if (count >= sizeof(auth->name)) \
389 return -EINVAL; \
390 snprintf(auth->name, sizeof(auth->name), "%s", page); \
391 if (!strncmp("NULL", auth->name, 4)) \
392 auth->naf_flags &= ~flags; \
393 else \
394 auth->naf_flags |= flags; \
396 if ((auth->naf_flags & NAF_USERID_IN_SET) && \
397 (auth->naf_flags & NAF_PASSWORD_IN_SET)) \
398 auth->authenticate_target = 1; \
399 else \
400 auth->authenticate_target = 0; \
402 return count; \
405 #define DEF_NACL_AUTH_STR(name, flags) \
406 __DEF_NACL_AUTH_STR(nacl_auth, name, flags) \
407 static ssize_t iscsi_nacl_auth_##name##_show(struct config_item *item, \
408 char *page) \
410 struct se_node_acl *nacl = auth_to_nacl(item); \
411 return __iscsi_nacl_auth_##name##_show(container_of(nacl, \
412 struct iscsi_node_acl, se_node_acl), page); \
414 static ssize_t iscsi_nacl_auth_##name##_store(struct config_item *item, \
415 const char *page, size_t count) \
417 struct se_node_acl *nacl = auth_to_nacl(item); \
418 return __iscsi_nacl_auth_##name##_store(container_of(nacl, \
419 struct iscsi_node_acl, se_node_acl), page, count); \
422 CONFIGFS_ATTR(iscsi_nacl_auth_, name)
425 * One-way authentication userid
427 DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
428 DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
429 DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
430 DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
432 #define __DEF_NACL_AUTH_INT(prefix, name) \
433 static ssize_t __iscsi_##prefix##_##name##_show( \
434 struct iscsi_node_acl *nacl, \
435 char *page) \
437 struct iscsi_node_auth *auth = &nacl->node_auth; \
439 if (!capable(CAP_SYS_ADMIN)) \
440 return -EPERM; \
442 return snprintf(page, PAGE_SIZE, "%d\n", auth->name); \
445 #define DEF_NACL_AUTH_INT(name) \
446 __DEF_NACL_AUTH_INT(nacl_auth, name) \
447 static ssize_t iscsi_nacl_auth_##name##_show(struct config_item *item, \
448 char *page) \
450 struct se_node_acl *nacl = auth_to_nacl(item); \
451 return __iscsi_nacl_auth_##name##_show(container_of(nacl, \
452 struct iscsi_node_acl, se_node_acl), page); \
455 CONFIGFS_ATTR_RO(iscsi_nacl_auth_, name)
457 DEF_NACL_AUTH_INT(authenticate_target);
459 static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
460 &iscsi_nacl_auth_attr_userid,
461 &iscsi_nacl_auth_attr_password,
462 &iscsi_nacl_auth_attr_authenticate_target,
463 &iscsi_nacl_auth_attr_userid_mutual,
464 &iscsi_nacl_auth_attr_password_mutual,
465 NULL,
468 /* End items for lio_target_nacl_auth_cit */
470 /* Start items for lio_target_nacl_param_cit */
472 #define ISCSI_NACL_PARAM(name) \
473 static ssize_t iscsi_nacl_param_##name##_show(struct config_item *item, \
474 char *page) \
476 struct se_node_acl *se_nacl = param_to_nacl(item); \
477 struct iscsi_session *sess; \
478 struct se_session *se_sess; \
479 ssize_t rb; \
481 spin_lock_bh(&se_nacl->nacl_sess_lock); \
482 se_sess = se_nacl->nacl_sess; \
483 if (!se_sess) { \
484 rb = snprintf(page, PAGE_SIZE, \
485 "No Active iSCSI Session\n"); \
486 } else { \
487 sess = se_sess->fabric_sess_ptr; \
488 rb = snprintf(page, PAGE_SIZE, "%u\n", \
489 (u32)sess->sess_ops->name); \
491 spin_unlock_bh(&se_nacl->nacl_sess_lock); \
493 return rb; \
496 CONFIGFS_ATTR_RO(iscsi_nacl_param_, name)
498 ISCSI_NACL_PARAM(MaxConnections);
499 ISCSI_NACL_PARAM(InitialR2T);
500 ISCSI_NACL_PARAM(ImmediateData);
501 ISCSI_NACL_PARAM(MaxBurstLength);
502 ISCSI_NACL_PARAM(FirstBurstLength);
503 ISCSI_NACL_PARAM(DefaultTime2Wait);
504 ISCSI_NACL_PARAM(DefaultTime2Retain);
505 ISCSI_NACL_PARAM(MaxOutstandingR2T);
506 ISCSI_NACL_PARAM(DataPDUInOrder);
507 ISCSI_NACL_PARAM(DataSequenceInOrder);
508 ISCSI_NACL_PARAM(ErrorRecoveryLevel);
510 static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
511 &iscsi_nacl_param_attr_MaxConnections,
512 &iscsi_nacl_param_attr_InitialR2T,
513 &iscsi_nacl_param_attr_ImmediateData,
514 &iscsi_nacl_param_attr_MaxBurstLength,
515 &iscsi_nacl_param_attr_FirstBurstLength,
516 &iscsi_nacl_param_attr_DefaultTime2Wait,
517 &iscsi_nacl_param_attr_DefaultTime2Retain,
518 &iscsi_nacl_param_attr_MaxOutstandingR2T,
519 &iscsi_nacl_param_attr_DataPDUInOrder,
520 &iscsi_nacl_param_attr_DataSequenceInOrder,
521 &iscsi_nacl_param_attr_ErrorRecoveryLevel,
522 NULL,
525 /* End items for lio_target_nacl_param_cit */
527 /* Start items for lio_target_acl_cit */
529 static ssize_t lio_target_nacl_info_show(struct config_item *item, char *page)
531 struct se_node_acl *se_nacl = acl_to_nacl(item);
532 struct iscsi_session *sess;
533 struct iscsi_conn *conn;
534 struct se_session *se_sess;
535 ssize_t rb = 0;
536 u32 max_cmd_sn;
538 spin_lock_bh(&se_nacl->nacl_sess_lock);
539 se_sess = se_nacl->nacl_sess;
540 if (!se_sess) {
541 rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
542 " Endpoint: %s\n", se_nacl->initiatorname);
543 } else {
544 sess = se_sess->fabric_sess_ptr;
546 rb += sprintf(page+rb, "InitiatorName: %s\n",
547 sess->sess_ops->InitiatorName);
548 rb += sprintf(page+rb, "InitiatorAlias: %s\n",
549 sess->sess_ops->InitiatorAlias);
551 rb += sprintf(page+rb,
552 "LIO Session ID: %u ISID: 0x%6ph TSIH: %hu ",
553 sess->sid, sess->isid, sess->tsih);
554 rb += sprintf(page+rb, "SessionType: %s\n",
555 (sess->sess_ops->SessionType) ?
556 "Discovery" : "Normal");
557 rb += sprintf(page+rb, "Session State: ");
558 switch (sess->session_state) {
559 case TARG_SESS_STATE_FREE:
560 rb += sprintf(page+rb, "TARG_SESS_FREE\n");
561 break;
562 case TARG_SESS_STATE_ACTIVE:
563 rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
564 break;
565 case TARG_SESS_STATE_LOGGED_IN:
566 rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
567 break;
568 case TARG_SESS_STATE_FAILED:
569 rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
570 break;
571 case TARG_SESS_STATE_IN_CONTINUE:
572 rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
573 break;
574 default:
575 rb += sprintf(page+rb, "ERROR: Unknown Session"
576 " State!\n");
577 break;
580 rb += sprintf(page+rb, "---------------------[iSCSI Session"
581 " Values]-----------------------\n");
582 rb += sprintf(page+rb, " CmdSN/WR : CmdSN/WC : ExpCmdSN"
583 " : MaxCmdSN : ITT : TTT\n");
584 max_cmd_sn = (u32) atomic_read(&sess->max_cmd_sn);
585 rb += sprintf(page+rb, " 0x%08x 0x%08x 0x%08x 0x%08x"
586 " 0x%08x 0x%08x\n",
587 sess->cmdsn_window,
588 (max_cmd_sn - sess->exp_cmd_sn) + 1,
589 sess->exp_cmd_sn, max_cmd_sn,
590 sess->init_task_tag, sess->targ_xfer_tag);
591 rb += sprintf(page+rb, "----------------------[iSCSI"
592 " Connections]-------------------------\n");
594 spin_lock(&sess->conn_lock);
595 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
596 rb += sprintf(page+rb, "CID: %hu Connection"
597 " State: ", conn->cid);
598 switch (conn->conn_state) {
599 case TARG_CONN_STATE_FREE:
600 rb += sprintf(page+rb,
601 "TARG_CONN_STATE_FREE\n");
602 break;
603 case TARG_CONN_STATE_XPT_UP:
604 rb += sprintf(page+rb,
605 "TARG_CONN_STATE_XPT_UP\n");
606 break;
607 case TARG_CONN_STATE_IN_LOGIN:
608 rb += sprintf(page+rb,
609 "TARG_CONN_STATE_IN_LOGIN\n");
610 break;
611 case TARG_CONN_STATE_LOGGED_IN:
612 rb += sprintf(page+rb,
613 "TARG_CONN_STATE_LOGGED_IN\n");
614 break;
615 case TARG_CONN_STATE_IN_LOGOUT:
616 rb += sprintf(page+rb,
617 "TARG_CONN_STATE_IN_LOGOUT\n");
618 break;
619 case TARG_CONN_STATE_LOGOUT_REQUESTED:
620 rb += sprintf(page+rb,
621 "TARG_CONN_STATE_LOGOUT_REQUESTED\n");
622 break;
623 case TARG_CONN_STATE_CLEANUP_WAIT:
624 rb += sprintf(page+rb,
625 "TARG_CONN_STATE_CLEANUP_WAIT\n");
626 break;
627 default:
628 rb += sprintf(page+rb,
629 "ERROR: Unknown Connection State!\n");
630 break;
633 rb += sprintf(page+rb, " Address %pISc %s", &conn->login_sockaddr,
634 (conn->network_transport == ISCSI_TCP) ?
635 "TCP" : "SCTP");
636 rb += sprintf(page+rb, " StatSN: 0x%08x\n",
637 conn->stat_sn);
639 spin_unlock(&sess->conn_lock);
641 spin_unlock_bh(&se_nacl->nacl_sess_lock);
643 return rb;
646 static ssize_t lio_target_nacl_cmdsn_depth_show(struct config_item *item,
647 char *page)
649 return sprintf(page, "%u\n", acl_to_nacl(item)->queue_depth);
652 static ssize_t lio_target_nacl_cmdsn_depth_store(struct config_item *item,
653 const char *page, size_t count)
655 struct se_node_acl *se_nacl = acl_to_nacl(item);
656 struct se_portal_group *se_tpg = se_nacl->se_tpg;
657 struct iscsi_portal_group *tpg = container_of(se_tpg,
658 struct iscsi_portal_group, tpg_se_tpg);
659 struct config_item *acl_ci, *tpg_ci, *wwn_ci;
660 u32 cmdsn_depth = 0;
661 int ret;
663 ret = kstrtou32(page, 0, &cmdsn_depth);
664 if (ret)
665 return ret;
666 if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
667 pr_err("Passed cmdsn_depth: %u exceeds"
668 " TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
669 TA_DEFAULT_CMDSN_DEPTH_MAX);
670 return -EINVAL;
672 acl_ci = &se_nacl->acl_group.cg_item;
673 if (!acl_ci) {
674 pr_err("Unable to locatel acl_ci\n");
675 return -EINVAL;
677 tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
678 if (!tpg_ci) {
679 pr_err("Unable to locate tpg_ci\n");
680 return -EINVAL;
682 wwn_ci = &tpg_ci->ci_group->cg_item;
683 if (!wwn_ci) {
684 pr_err("Unable to locate config_item wwn_ci\n");
685 return -EINVAL;
688 if (iscsit_get_tpg(tpg) < 0)
689 return -EINVAL;
691 ret = core_tpg_set_initiator_node_queue_depth(se_nacl, cmdsn_depth);
693 pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
694 "InitiatorName: %s\n", config_item_name(wwn_ci),
695 config_item_name(tpg_ci), cmdsn_depth,
696 config_item_name(acl_ci));
698 iscsit_put_tpg(tpg);
699 return (!ret) ? count : (ssize_t)ret;
702 static ssize_t lio_target_nacl_tag_show(struct config_item *item, char *page)
704 return snprintf(page, PAGE_SIZE, "%s", acl_to_nacl(item)->acl_tag);
707 static ssize_t lio_target_nacl_tag_store(struct config_item *item,
708 const char *page, size_t count)
710 struct se_node_acl *se_nacl = acl_to_nacl(item);
711 int ret;
713 ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
715 if (ret < 0)
716 return ret;
717 return count;
720 CONFIGFS_ATTR_RO(lio_target_nacl_, info);
721 CONFIGFS_ATTR(lio_target_nacl_, cmdsn_depth);
722 CONFIGFS_ATTR(lio_target_nacl_, tag);
724 static struct configfs_attribute *lio_target_initiator_attrs[] = {
725 &lio_target_nacl_attr_info,
726 &lio_target_nacl_attr_cmdsn_depth,
727 &lio_target_nacl_attr_tag,
728 NULL,
731 static int lio_target_init_nodeacl(struct se_node_acl *se_nacl,
732 const char *name)
734 struct iscsi_node_acl *acl =
735 container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
737 config_group_init_type_name(&acl->node_stat_grps.iscsi_sess_stats_group,
738 "iscsi_sess_stats", &iscsi_stat_sess_cit);
739 configfs_add_default_group(&acl->node_stat_grps.iscsi_sess_stats_group,
740 &se_nacl->acl_fabric_stat_group);
741 return 0;
744 /* End items for lio_target_acl_cit */
746 /* Start items for lio_target_tpg_attrib_cit */
748 #define DEF_TPG_ATTRIB(name) \
750 static ssize_t iscsi_tpg_attrib_##name##_show(struct config_item *item, \
751 char *page) \
753 struct se_portal_group *se_tpg = attrib_to_tpg(item); \
754 struct iscsi_portal_group *tpg = container_of(se_tpg, \
755 struct iscsi_portal_group, tpg_se_tpg); \
756 ssize_t rb; \
758 if (iscsit_get_tpg(tpg) < 0) \
759 return -EINVAL; \
761 rb = sprintf(page, "%u\n", tpg->tpg_attrib.name); \
762 iscsit_put_tpg(tpg); \
763 return rb; \
766 static ssize_t iscsi_tpg_attrib_##name##_store(struct config_item *item,\
767 const char *page, size_t count) \
769 struct se_portal_group *se_tpg = attrib_to_tpg(item); \
770 struct iscsi_portal_group *tpg = container_of(se_tpg, \
771 struct iscsi_portal_group, tpg_se_tpg); \
772 u32 val; \
773 int ret; \
775 if (iscsit_get_tpg(tpg) < 0) \
776 return -EINVAL; \
778 ret = kstrtou32(page, 0, &val); \
779 if (ret) \
780 goto out; \
781 ret = iscsit_ta_##name(tpg, val); \
782 if (ret < 0) \
783 goto out; \
785 iscsit_put_tpg(tpg); \
786 return count; \
787 out: \
788 iscsit_put_tpg(tpg); \
789 return ret; \
791 CONFIGFS_ATTR(iscsi_tpg_attrib_, name)
793 DEF_TPG_ATTRIB(authentication);
794 DEF_TPG_ATTRIB(login_timeout);
795 DEF_TPG_ATTRIB(netif_timeout);
796 DEF_TPG_ATTRIB(generate_node_acls);
797 DEF_TPG_ATTRIB(default_cmdsn_depth);
798 DEF_TPG_ATTRIB(cache_dynamic_acls);
799 DEF_TPG_ATTRIB(demo_mode_write_protect);
800 DEF_TPG_ATTRIB(prod_mode_write_protect);
801 DEF_TPG_ATTRIB(demo_mode_discovery);
802 DEF_TPG_ATTRIB(default_erl);
803 DEF_TPG_ATTRIB(t10_pi);
804 DEF_TPG_ATTRIB(fabric_prot_type);
805 DEF_TPG_ATTRIB(tpg_enabled_sendtargets);
807 static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
808 &iscsi_tpg_attrib_attr_authentication,
809 &iscsi_tpg_attrib_attr_login_timeout,
810 &iscsi_tpg_attrib_attr_netif_timeout,
811 &iscsi_tpg_attrib_attr_generate_node_acls,
812 &iscsi_tpg_attrib_attr_default_cmdsn_depth,
813 &iscsi_tpg_attrib_attr_cache_dynamic_acls,
814 &iscsi_tpg_attrib_attr_demo_mode_write_protect,
815 &iscsi_tpg_attrib_attr_prod_mode_write_protect,
816 &iscsi_tpg_attrib_attr_demo_mode_discovery,
817 &iscsi_tpg_attrib_attr_default_erl,
818 &iscsi_tpg_attrib_attr_t10_pi,
819 &iscsi_tpg_attrib_attr_fabric_prot_type,
820 &iscsi_tpg_attrib_attr_tpg_enabled_sendtargets,
821 NULL,
824 /* End items for lio_target_tpg_attrib_cit */
826 /* Start items for lio_target_tpg_auth_cit */
828 #define __DEF_TPG_AUTH_STR(prefix, name, flags) \
829 static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg, \
830 char *page) \
832 struct iscsi_portal_group *tpg = container_of(se_tpg, \
833 struct iscsi_portal_group, tpg_se_tpg); \
834 struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
836 if (!capable(CAP_SYS_ADMIN)) \
837 return -EPERM; \
839 return snprintf(page, PAGE_SIZE, "%s\n", auth->name); \
842 static ssize_t __iscsi_##prefix##_##name##_store(struct se_portal_group *se_tpg,\
843 const char *page, size_t count) \
845 struct iscsi_portal_group *tpg = container_of(se_tpg, \
846 struct iscsi_portal_group, tpg_se_tpg); \
847 struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
849 if (!capable(CAP_SYS_ADMIN)) \
850 return -EPERM; \
852 snprintf(auth->name, sizeof(auth->name), "%s", page); \
853 if (!(strncmp("NULL", auth->name, 4))) \
854 auth->naf_flags &= ~flags; \
855 else \
856 auth->naf_flags |= flags; \
858 if ((auth->naf_flags & NAF_USERID_IN_SET) && \
859 (auth->naf_flags & NAF_PASSWORD_IN_SET)) \
860 auth->authenticate_target = 1; \
861 else \
862 auth->authenticate_target = 0; \
864 return count; \
867 #define DEF_TPG_AUTH_STR(name, flags) \
868 __DEF_TPG_AUTH_STR(tpg_auth, name, flags) \
869 static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item, \
870 char *page) \
872 return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page); \
875 static ssize_t iscsi_tpg_auth_##name##_store(struct config_item *item, \
876 const char *page, size_t count) \
878 return __iscsi_tpg_auth_##name##_store(auth_to_tpg(item), page, count); \
881 CONFIGFS_ATTR(iscsi_tpg_auth_, name);
884 DEF_TPG_AUTH_STR(userid, NAF_USERID_SET);
885 DEF_TPG_AUTH_STR(password, NAF_PASSWORD_SET);
886 DEF_TPG_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
887 DEF_TPG_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
889 #define __DEF_TPG_AUTH_INT(prefix, name) \
890 static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg, \
891 char *page) \
893 struct iscsi_portal_group *tpg = container_of(se_tpg, \
894 struct iscsi_portal_group, tpg_se_tpg); \
895 struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
897 if (!capable(CAP_SYS_ADMIN)) \
898 return -EPERM; \
900 return snprintf(page, PAGE_SIZE, "%d\n", auth->name); \
903 #define DEF_TPG_AUTH_INT(name) \
904 __DEF_TPG_AUTH_INT(tpg_auth, name) \
905 static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item, \
906 char *page) \
908 return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page); \
910 CONFIGFS_ATTR_RO(iscsi_tpg_auth_, name);
912 DEF_TPG_AUTH_INT(authenticate_target);
914 static struct configfs_attribute *lio_target_tpg_auth_attrs[] = {
915 &iscsi_tpg_auth_attr_userid,
916 &iscsi_tpg_auth_attr_password,
917 &iscsi_tpg_auth_attr_authenticate_target,
918 &iscsi_tpg_auth_attr_userid_mutual,
919 &iscsi_tpg_auth_attr_password_mutual,
920 NULL,
923 /* End items for lio_target_tpg_auth_cit */
925 /* Start items for lio_target_tpg_param_cit */
927 #define DEF_TPG_PARAM(name) \
928 static ssize_t iscsi_tpg_param_##name##_show(struct config_item *item, \
929 char *page) \
931 struct se_portal_group *se_tpg = param_to_tpg(item); \
932 struct iscsi_portal_group *tpg = container_of(se_tpg, \
933 struct iscsi_portal_group, tpg_se_tpg); \
934 struct iscsi_param *param; \
935 ssize_t rb; \
937 if (iscsit_get_tpg(tpg) < 0) \
938 return -EINVAL; \
940 param = iscsi_find_param_from_key(__stringify(name), \
941 tpg->param_list); \
942 if (!param) { \
943 iscsit_put_tpg(tpg); \
944 return -EINVAL; \
946 rb = snprintf(page, PAGE_SIZE, "%s\n", param->value); \
948 iscsit_put_tpg(tpg); \
949 return rb; \
951 static ssize_t iscsi_tpg_param_##name##_store(struct config_item *item, \
952 const char *page, size_t count) \
954 struct se_portal_group *se_tpg = param_to_tpg(item); \
955 struct iscsi_portal_group *tpg = container_of(se_tpg, \
956 struct iscsi_portal_group, tpg_se_tpg); \
957 char *buf; \
958 int ret, len; \
960 buf = kzalloc(PAGE_SIZE, GFP_KERNEL); \
961 if (!buf) \
962 return -ENOMEM; \
963 len = snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page); \
964 if (isspace(buf[len-1])) \
965 buf[len-1] = '\0'; /* Kill newline */ \
967 if (iscsit_get_tpg(tpg) < 0) { \
968 kfree(buf); \
969 return -EINVAL; \
972 ret = iscsi_change_param_value(buf, tpg->param_list, 1); \
973 if (ret < 0) \
974 goto out; \
976 kfree(buf); \
977 iscsit_put_tpg(tpg); \
978 return count; \
979 out: \
980 kfree(buf); \
981 iscsit_put_tpg(tpg); \
982 return -EINVAL; \
984 CONFIGFS_ATTR(iscsi_tpg_param_, name)
986 DEF_TPG_PARAM(AuthMethod);
987 DEF_TPG_PARAM(HeaderDigest);
988 DEF_TPG_PARAM(DataDigest);
989 DEF_TPG_PARAM(MaxConnections);
990 DEF_TPG_PARAM(TargetAlias);
991 DEF_TPG_PARAM(InitialR2T);
992 DEF_TPG_PARAM(ImmediateData);
993 DEF_TPG_PARAM(MaxRecvDataSegmentLength);
994 DEF_TPG_PARAM(MaxXmitDataSegmentLength);
995 DEF_TPG_PARAM(MaxBurstLength);
996 DEF_TPG_PARAM(FirstBurstLength);
997 DEF_TPG_PARAM(DefaultTime2Wait);
998 DEF_TPG_PARAM(DefaultTime2Retain);
999 DEF_TPG_PARAM(MaxOutstandingR2T);
1000 DEF_TPG_PARAM(DataPDUInOrder);
1001 DEF_TPG_PARAM(DataSequenceInOrder);
1002 DEF_TPG_PARAM(ErrorRecoveryLevel);
1003 DEF_TPG_PARAM(IFMarker);
1004 DEF_TPG_PARAM(OFMarker);
1005 DEF_TPG_PARAM(IFMarkInt);
1006 DEF_TPG_PARAM(OFMarkInt);
1008 static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
1009 &iscsi_tpg_param_attr_AuthMethod,
1010 &iscsi_tpg_param_attr_HeaderDigest,
1011 &iscsi_tpg_param_attr_DataDigest,
1012 &iscsi_tpg_param_attr_MaxConnections,
1013 &iscsi_tpg_param_attr_TargetAlias,
1014 &iscsi_tpg_param_attr_InitialR2T,
1015 &iscsi_tpg_param_attr_ImmediateData,
1016 &iscsi_tpg_param_attr_MaxRecvDataSegmentLength,
1017 &iscsi_tpg_param_attr_MaxXmitDataSegmentLength,
1018 &iscsi_tpg_param_attr_MaxBurstLength,
1019 &iscsi_tpg_param_attr_FirstBurstLength,
1020 &iscsi_tpg_param_attr_DefaultTime2Wait,
1021 &iscsi_tpg_param_attr_DefaultTime2Retain,
1022 &iscsi_tpg_param_attr_MaxOutstandingR2T,
1023 &iscsi_tpg_param_attr_DataPDUInOrder,
1024 &iscsi_tpg_param_attr_DataSequenceInOrder,
1025 &iscsi_tpg_param_attr_ErrorRecoveryLevel,
1026 &iscsi_tpg_param_attr_IFMarker,
1027 &iscsi_tpg_param_attr_OFMarker,
1028 &iscsi_tpg_param_attr_IFMarkInt,
1029 &iscsi_tpg_param_attr_OFMarkInt,
1030 NULL,
1033 /* End items for lio_target_tpg_param_cit */
1035 /* Start items for lio_target_tpg_cit */
1037 static ssize_t lio_target_tpg_enable_show(struct config_item *item, char *page)
1039 struct se_portal_group *se_tpg = to_tpg(item);
1040 struct iscsi_portal_group *tpg = container_of(se_tpg,
1041 struct iscsi_portal_group, tpg_se_tpg);
1042 ssize_t len;
1044 spin_lock(&tpg->tpg_state_lock);
1045 len = sprintf(page, "%d\n",
1046 (tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1047 spin_unlock(&tpg->tpg_state_lock);
1049 return len;
1052 static ssize_t lio_target_tpg_enable_store(struct config_item *item,
1053 const char *page, size_t count)
1055 struct se_portal_group *se_tpg = to_tpg(item);
1056 struct iscsi_portal_group *tpg = container_of(se_tpg,
1057 struct iscsi_portal_group, tpg_se_tpg);
1058 u32 op;
1059 int ret;
1061 ret = kstrtou32(page, 0, &op);
1062 if (ret)
1063 return ret;
1064 if ((op != 1) && (op != 0)) {
1065 pr_err("Illegal value for tpg_enable: %u\n", op);
1066 return -EINVAL;
1069 ret = iscsit_get_tpg(tpg);
1070 if (ret < 0)
1071 return -EINVAL;
1073 if (op) {
1074 ret = iscsit_tpg_enable_portal_group(tpg);
1075 if (ret < 0)
1076 goto out;
1077 } else {
1079 * iscsit_tpg_disable_portal_group() assumes force=1
1081 ret = iscsit_tpg_disable_portal_group(tpg, 1);
1082 if (ret < 0)
1083 goto out;
1086 iscsit_put_tpg(tpg);
1087 return count;
1088 out:
1089 iscsit_put_tpg(tpg);
1090 return -EINVAL;
1094 static ssize_t lio_target_tpg_dynamic_sessions_show(struct config_item *item,
1095 char *page)
1097 return target_show_dynamic_sessions(to_tpg(item), page);
1100 CONFIGFS_ATTR(lio_target_tpg_, enable);
1101 CONFIGFS_ATTR_RO(lio_target_tpg_, dynamic_sessions);
1103 static struct configfs_attribute *lio_target_tpg_attrs[] = {
1104 &lio_target_tpg_attr_enable,
1105 &lio_target_tpg_attr_dynamic_sessions,
1106 NULL,
1109 /* End items for lio_target_tpg_cit */
1111 /* Start items for lio_target_tiqn_cit */
1113 static struct se_portal_group *lio_target_tiqn_addtpg(
1114 struct se_wwn *wwn,
1115 struct config_group *group,
1116 const char *name)
1118 struct iscsi_portal_group *tpg;
1119 struct iscsi_tiqn *tiqn;
1120 char *tpgt_str;
1121 int ret;
1122 u16 tpgt;
1124 tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1126 * Only tpgt_# directory groups can be created below
1127 * target/iscsi/iqn.superturodiskarry/
1129 tpgt_str = strstr(name, "tpgt_");
1130 if (!tpgt_str) {
1131 pr_err("Unable to locate \"tpgt_#\" directory"
1132 " group\n");
1133 return NULL;
1135 tpgt_str += 5; /* Skip ahead of "tpgt_" */
1136 ret = kstrtou16(tpgt_str, 0, &tpgt);
1137 if (ret)
1138 return NULL;
1140 tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1141 if (!tpg)
1142 return NULL;
1144 ret = core_tpg_register(wwn, &tpg->tpg_se_tpg, SCSI_PROTOCOL_ISCSI);
1145 if (ret < 0)
1146 return NULL;
1148 ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1149 if (ret != 0)
1150 goto out;
1152 pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1153 pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1154 name);
1155 return &tpg->tpg_se_tpg;
1156 out:
1157 core_tpg_deregister(&tpg->tpg_se_tpg);
1158 kfree(tpg);
1159 return NULL;
1162 static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1164 struct iscsi_portal_group *tpg;
1165 struct iscsi_tiqn *tiqn;
1167 tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1168 tiqn = tpg->tpg_tiqn;
1170 * iscsit_tpg_del_portal_group() assumes force=1
1172 pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1173 iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1176 /* End items for lio_target_tiqn_cit */
1178 /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1180 static ssize_t lio_target_wwn_lio_version_show(struct config_item *item,
1181 char *page)
1183 return sprintf(page, "Datera Inc. iSCSI Target "ISCSIT_VERSION"\n");
1186 CONFIGFS_ATTR_RO(lio_target_wwn_, lio_version);
1188 static struct configfs_attribute *lio_target_wwn_attrs[] = {
1189 &lio_target_wwn_attr_lio_version,
1190 NULL,
1193 static struct se_wwn *lio_target_call_coreaddtiqn(
1194 struct target_fabric_configfs *tf,
1195 struct config_group *group,
1196 const char *name)
1198 struct iscsi_tiqn *tiqn;
1200 tiqn = iscsit_add_tiqn((unsigned char *)name);
1201 if (IS_ERR(tiqn))
1202 return ERR_CAST(tiqn);
1204 pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1205 pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1206 " %s\n", name);
1207 return &tiqn->tiqn_wwn;
1210 static void lio_target_add_wwn_groups(struct se_wwn *wwn)
1212 struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1214 config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1215 "iscsi_instance", &iscsi_stat_instance_cit);
1216 configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1217 &tiqn->tiqn_wwn.fabric_stat_group);
1219 config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1220 "iscsi_sess_err", &iscsi_stat_sess_err_cit);
1221 configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1222 &tiqn->tiqn_wwn.fabric_stat_group);
1224 config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1225 "iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
1226 configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1227 &tiqn->tiqn_wwn.fabric_stat_group);
1229 config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1230 "iscsi_login_stats", &iscsi_stat_login_cit);
1231 configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1232 &tiqn->tiqn_wwn.fabric_stat_group);
1234 config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1235 "iscsi_logout_stats", &iscsi_stat_logout_cit);
1236 configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1237 &tiqn->tiqn_wwn.fabric_stat_group);
1240 static void lio_target_call_coredeltiqn(
1241 struct se_wwn *wwn)
1243 struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1245 pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1246 tiqn->tiqn);
1247 iscsit_del_tiqn(tiqn);
1250 /* End LIO-Target TIQN struct contig_lio_target_cit */
1252 /* Start lio_target_discovery_auth_cit */
1254 #define DEF_DISC_AUTH_STR(name, flags) \
1255 __DEF_NACL_AUTH_STR(disc, name, flags) \
1256 static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1258 return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl,\
1259 page); \
1261 static ssize_t iscsi_disc_##name##_store(struct config_item *item, \
1262 const char *page, size_t count) \
1264 return __iscsi_disc_##name##_store(&iscsit_global->discovery_acl, \
1265 page, count); \
1268 CONFIGFS_ATTR(iscsi_disc_, name)
1270 DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
1271 DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
1272 DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1273 DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1275 #define DEF_DISC_AUTH_INT(name) \
1276 __DEF_NACL_AUTH_INT(disc, name) \
1277 static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1279 return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl, \
1280 page); \
1282 CONFIGFS_ATTR_RO(iscsi_disc_, name)
1284 DEF_DISC_AUTH_INT(authenticate_target);
1287 static ssize_t iscsi_disc_enforce_discovery_auth_show(struct config_item *item,
1288 char *page)
1290 struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1292 return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1295 static ssize_t iscsi_disc_enforce_discovery_auth_store(struct config_item *item,
1296 const char *page, size_t count)
1298 struct iscsi_param *param;
1299 struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1300 u32 op;
1301 int err;
1303 err = kstrtou32(page, 0, &op);
1304 if (err)
1305 return -EINVAL;
1306 if ((op != 1) && (op != 0)) {
1307 pr_err("Illegal value for enforce_discovery_auth:"
1308 " %u\n", op);
1309 return -EINVAL;
1312 if (!discovery_tpg) {
1313 pr_err("iscsit_global->discovery_tpg is NULL\n");
1314 return -EINVAL;
1317 param = iscsi_find_param_from_key(AUTHMETHOD,
1318 discovery_tpg->param_list);
1319 if (!param)
1320 return -EINVAL;
1322 if (op) {
1324 * Reset the AuthMethod key to CHAP.
1326 if (iscsi_update_param_value(param, CHAP) < 0)
1327 return -EINVAL;
1329 discovery_tpg->tpg_attrib.authentication = 1;
1330 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1331 pr_debug("LIO-CORE[0] Successfully enabled"
1332 " authentication enforcement for iSCSI"
1333 " Discovery TPG\n");
1334 } else {
1336 * Reset the AuthMethod key to CHAP,None
1338 if (iscsi_update_param_value(param, "CHAP,None") < 0)
1339 return -EINVAL;
1341 discovery_tpg->tpg_attrib.authentication = 0;
1342 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1343 pr_debug("LIO-CORE[0] Successfully disabled"
1344 " authentication enforcement for iSCSI"
1345 " Discovery TPG\n");
1348 return count;
1351 CONFIGFS_ATTR(iscsi_disc_, enforce_discovery_auth);
1353 static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
1354 &iscsi_disc_attr_userid,
1355 &iscsi_disc_attr_password,
1356 &iscsi_disc_attr_authenticate_target,
1357 &iscsi_disc_attr_userid_mutual,
1358 &iscsi_disc_attr_password_mutual,
1359 &iscsi_disc_attr_enforce_discovery_auth,
1360 NULL,
1363 /* End lio_target_discovery_auth_cit */
1365 /* Start functions for target_core_fabric_ops */
1367 static char *iscsi_get_fabric_name(void)
1369 return "iSCSI";
1372 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1374 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1376 return cmd->i_state;
1379 static u32 lio_sess_get_index(struct se_session *se_sess)
1381 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1383 return sess->session_index;
1386 static u32 lio_sess_get_initiator_sid(
1387 struct se_session *se_sess,
1388 unsigned char *buf,
1389 u32 size)
1391 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1393 * iSCSI Initiator Session Identifier from RFC-3720.
1395 return snprintf(buf, size, "%6phN", sess->isid);
1398 static int lio_queue_data_in(struct se_cmd *se_cmd)
1400 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1402 cmd->i_state = ISTATE_SEND_DATAIN;
1403 cmd->conn->conn_transport->iscsit_queue_data_in(cmd->conn, cmd);
1405 return 0;
1408 static int lio_write_pending(struct se_cmd *se_cmd)
1410 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1411 struct iscsi_conn *conn = cmd->conn;
1413 if (!cmd->immediate_data && !cmd->unsolicited_data)
1414 return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1416 return 0;
1419 static int lio_write_pending_status(struct se_cmd *se_cmd)
1421 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1422 int ret;
1424 spin_lock_bh(&cmd->istate_lock);
1425 ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1426 spin_unlock_bh(&cmd->istate_lock);
1428 return ret;
1431 static int lio_queue_status(struct se_cmd *se_cmd)
1433 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1435 cmd->i_state = ISTATE_SEND_STATUS;
1437 if (cmd->se_cmd.scsi_status || cmd->sense_reason) {
1438 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1439 return 0;
1441 cmd->conn->conn_transport->iscsit_queue_status(cmd->conn, cmd);
1443 return 0;
1446 static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
1448 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1450 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1451 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1454 static void lio_aborted_task(struct se_cmd *se_cmd)
1456 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1458 cmd->conn->conn_transport->iscsit_aborted_task(cmd->conn, cmd);
1461 static inline struct iscsi_portal_group *iscsi_tpg(struct se_portal_group *se_tpg)
1463 return container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1466 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1468 return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn;
1471 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1473 return iscsi_tpg(se_tpg)->tpgt;
1476 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1478 return iscsi_tpg(se_tpg)->tpg_attrib.default_cmdsn_depth;
1481 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1483 return iscsi_tpg(se_tpg)->tpg_attrib.generate_node_acls;
1486 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1488 return iscsi_tpg(se_tpg)->tpg_attrib.cache_dynamic_acls;
1491 static int lio_tpg_check_demo_mode_write_protect(
1492 struct se_portal_group *se_tpg)
1494 return iscsi_tpg(se_tpg)->tpg_attrib.demo_mode_write_protect;
1497 static int lio_tpg_check_prod_mode_write_protect(
1498 struct se_portal_group *se_tpg)
1500 return iscsi_tpg(se_tpg)->tpg_attrib.prod_mode_write_protect;
1503 static int lio_tpg_check_prot_fabric_only(
1504 struct se_portal_group *se_tpg)
1507 * Only report fabric_prot_type if t10_pi has also been enabled
1508 * for incoming ib_isert sessions.
1510 if (!iscsi_tpg(se_tpg)->tpg_attrib.t10_pi)
1511 return 0;
1512 return iscsi_tpg(se_tpg)->tpg_attrib.fabric_prot_type;
1516 * This function calls iscsit_inc_session_usage_count() on the
1517 * struct iscsi_session in question.
1519 static void lio_tpg_close_session(struct se_session *se_sess)
1521 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1522 struct se_portal_group *se_tpg = &sess->tpg->tpg_se_tpg;
1524 spin_lock_bh(&se_tpg->session_lock);
1525 spin_lock(&sess->conn_lock);
1526 if (atomic_read(&sess->session_fall_back_to_erl0) ||
1527 atomic_read(&sess->session_logout) ||
1528 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1529 spin_unlock(&sess->conn_lock);
1530 spin_unlock_bh(&se_tpg->session_lock);
1531 return;
1533 atomic_set(&sess->session_reinstatement, 1);
1534 spin_unlock(&sess->conn_lock);
1536 iscsit_stop_time2retain_timer(sess);
1537 spin_unlock_bh(&se_tpg->session_lock);
1539 iscsit_stop_session(sess, 1, 1);
1540 iscsit_close_session(sess);
1543 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1545 return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn_index;
1548 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1550 struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1551 se_node_acl);
1552 struct se_portal_group *se_tpg = se_acl->se_tpg;
1553 struct iscsi_portal_group *tpg = container_of(se_tpg,
1554 struct iscsi_portal_group, tpg_se_tpg);
1556 acl->node_attrib.nacl = acl;
1557 iscsit_set_default_node_attribues(acl, tpg);
1560 static int lio_check_stop_free(struct se_cmd *se_cmd)
1562 return target_put_sess_cmd(se_cmd);
1565 static void lio_release_cmd(struct se_cmd *se_cmd)
1567 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1569 pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
1570 iscsit_release_cmd(cmd);
1573 const struct target_core_fabric_ops iscsi_ops = {
1574 .module = THIS_MODULE,
1575 .name = "iscsi",
1576 .node_acl_size = sizeof(struct iscsi_node_acl),
1577 .get_fabric_name = iscsi_get_fabric_name,
1578 .tpg_get_wwn = lio_tpg_get_endpoint_wwn,
1579 .tpg_get_tag = lio_tpg_get_tag,
1580 .tpg_get_default_depth = lio_tpg_get_default_depth,
1581 .tpg_check_demo_mode = lio_tpg_check_demo_mode,
1582 .tpg_check_demo_mode_cache = lio_tpg_check_demo_mode_cache,
1583 .tpg_check_demo_mode_write_protect =
1584 lio_tpg_check_demo_mode_write_protect,
1585 .tpg_check_prod_mode_write_protect =
1586 lio_tpg_check_prod_mode_write_protect,
1587 .tpg_check_prot_fabric_only = &lio_tpg_check_prot_fabric_only,
1588 .tpg_get_inst_index = lio_tpg_get_inst_index,
1589 .check_stop_free = lio_check_stop_free,
1590 .release_cmd = lio_release_cmd,
1591 .close_session = lio_tpg_close_session,
1592 .sess_get_index = lio_sess_get_index,
1593 .sess_get_initiator_sid = lio_sess_get_initiator_sid,
1594 .write_pending = lio_write_pending,
1595 .write_pending_status = lio_write_pending_status,
1596 .set_default_node_attributes = lio_set_default_node_attributes,
1597 .get_cmd_state = iscsi_get_cmd_state,
1598 .queue_data_in = lio_queue_data_in,
1599 .queue_status = lio_queue_status,
1600 .queue_tm_rsp = lio_queue_tm_rsp,
1601 .aborted_task = lio_aborted_task,
1602 .fabric_make_wwn = lio_target_call_coreaddtiqn,
1603 .fabric_drop_wwn = lio_target_call_coredeltiqn,
1604 .add_wwn_groups = lio_target_add_wwn_groups,
1605 .fabric_make_tpg = lio_target_tiqn_addtpg,
1606 .fabric_drop_tpg = lio_target_tiqn_deltpg,
1607 .fabric_make_np = lio_target_call_addnptotpg,
1608 .fabric_drop_np = lio_target_call_delnpfromtpg,
1609 .fabric_init_nodeacl = lio_target_init_nodeacl,
1611 .tfc_discovery_attrs = lio_target_discovery_auth_attrs,
1612 .tfc_wwn_attrs = lio_target_wwn_attrs,
1613 .tfc_tpg_base_attrs = lio_target_tpg_attrs,
1614 .tfc_tpg_attrib_attrs = lio_target_tpg_attrib_attrs,
1615 .tfc_tpg_auth_attrs = lio_target_tpg_auth_attrs,
1616 .tfc_tpg_param_attrs = lio_target_tpg_param_attrs,
1617 .tfc_tpg_np_base_attrs = lio_target_portal_attrs,
1618 .tfc_tpg_nacl_base_attrs = lio_target_initiator_attrs,
1619 .tfc_tpg_nacl_attrib_attrs = lio_target_nacl_attrib_attrs,
1620 .tfc_tpg_nacl_auth_attrs = lio_target_nacl_auth_attrs,
1621 .tfc_tpg_nacl_param_attrs = lio_target_nacl_param_attrs,