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>
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;
54 tpg_np_new = iscsit_tpg_locate_child_np(tpg_np, type);
56 rb = sprintf(page, "1\n");
58 rb = sprintf(page, "0\n");
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,
67 struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
69 struct iscsi_portal_group *tpg;
70 struct iscsi_tpg_np *tpg_np_new = NULL;
74 rc = kstrtou32(page, 0, &op);
77 if ((op != 1) && (op != 0)) {
78 pr_err("Illegal value for tpg_enable: %u\n", op);
83 pr_err("Unable to locate struct iscsi_np from"
84 " struct iscsi_tpg_np\n");
89 if (iscsit_get_tpg(tpg) < 0)
93 if (strlen(mod_name)) {
94 rc = request_module(mod_name);
96 pr_warn("Unable to request_module for %s\n",
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);
109 tpg_np_new = iscsit_tpg_locate_child_np(tpg_np, type);
111 rc = iscsit_tpg_del_network_portal(tpg, tpg_np_new);
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,
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,
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;
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, "[");
191 str2 = strstr(str, "]");
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, ":");
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);
211 pr_err("kstrtoul() failed for port_str: %d\n", 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);
220 pr_err("in6_pton returned: %d\n", ret);
221 return ERR_PTR(-EINVAL);
224 str = ip_str = &buf[0];
225 port_str = strstr(ip_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);
236 pr_err("kstrtoul() failed for port_str: %d\n", 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);
247 return ERR_PTR(-EINVAL);
249 pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
251 config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
254 * Assume ISCSI_TCP by default. Other network portals for other
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,
268 if (IS_ERR(tpg_np)) {
270 return ERR_CAST(tpg_np);
272 pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
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;
286 tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
288 ret = iscsit_get_tpg(tpg);
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);
301 pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
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,\
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, \
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, \
330 ret = kstrtou32(page, 0, &val); \
333 ret = iscsit_na_##name(nacl, val); \
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,
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, \
372 struct iscsi_node_auth *auth = &nacl->node_auth; \
374 if (!capable(CAP_SYS_ADMIN)) \
376 return snprintf(page, PAGE_SIZE, "%s\n", auth->name); \
379 static ssize_t __iscsi_##prefix##_##name##_store( \
380 struct iscsi_node_acl *nacl, \
384 struct iscsi_node_auth *auth = &nacl->node_auth; \
386 if (!capable(CAP_SYS_ADMIN)) \
388 if (count >= sizeof(auth->name)) \
390 snprintf(auth->name, sizeof(auth->name), "%s", page); \
391 if (!strncmp("NULL", auth->name, 4)) \
392 auth->naf_flags &= ~flags; \
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; \
400 auth->authenticate_target = 0; \
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, \
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, \
437 struct iscsi_node_auth *auth = &nacl->node_auth; \
439 if (!capable(CAP_SYS_ADMIN)) \
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, \
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,
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, \
476 struct se_node_acl *se_nacl = param_to_nacl(item); \
477 struct iscsi_session *sess; \
478 struct se_session *se_sess; \
481 spin_lock_bh(&se_nacl->nacl_sess_lock); \
482 se_sess = se_nacl->nacl_sess; \
484 rb = snprintf(page, PAGE_SIZE, \
485 "No Active iSCSI Session\n"); \
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); \
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,
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;
538 spin_lock_bh(&se_nacl->nacl_sess_lock);
539 se_sess = se_nacl->nacl_sess;
541 rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
542 " Endpoint: %s\n", se_nacl->initiatorname);
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");
562 case TARG_SESS_STATE_ACTIVE:
563 rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
565 case TARG_SESS_STATE_LOGGED_IN:
566 rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
568 case TARG_SESS_STATE_FAILED:
569 rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
571 case TARG_SESS_STATE_IN_CONTINUE:
572 rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
575 rb += sprintf(page+rb, "ERROR: Unknown Session"
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"
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");
603 case TARG_CONN_STATE_XPT_UP:
604 rb += sprintf(page+rb,
605 "TARG_CONN_STATE_XPT_UP\n");
607 case TARG_CONN_STATE_IN_LOGIN:
608 rb += sprintf(page+rb,
609 "TARG_CONN_STATE_IN_LOGIN\n");
611 case TARG_CONN_STATE_LOGGED_IN:
612 rb += sprintf(page+rb,
613 "TARG_CONN_STATE_LOGGED_IN\n");
615 case TARG_CONN_STATE_IN_LOGOUT:
616 rb += sprintf(page+rb,
617 "TARG_CONN_STATE_IN_LOGOUT\n");
619 case TARG_CONN_STATE_LOGOUT_REQUESTED:
620 rb += sprintf(page+rb,
621 "TARG_CONN_STATE_LOGOUT_REQUESTED\n");
623 case TARG_CONN_STATE_CLEANUP_WAIT:
624 rb += sprintf(page+rb,
625 "TARG_CONN_STATE_CLEANUP_WAIT\n");
628 rb += sprintf(page+rb,
629 "ERROR: Unknown Connection State!\n");
633 rb += sprintf(page+rb, " Address %pISc %s", &conn->login_sockaddr,
634 (conn->network_transport == ISCSI_TCP) ?
636 rb += sprintf(page+rb, " StatSN: 0x%08x\n",
639 spin_unlock(&sess->conn_lock);
641 spin_unlock_bh(&se_nacl->nacl_sess_lock);
646 static ssize_t lio_target_nacl_cmdsn_depth_show(struct config_item *item,
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;
663 ret = kstrtou32(page, 0, &cmdsn_depth);
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);
672 acl_ci = &se_nacl->acl_group.cg_item;
674 pr_err("Unable to locatel acl_ci\n");
677 tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
679 pr_err("Unable to locate tpg_ci\n");
682 wwn_ci = &tpg_ci->ci_group->cg_item;
684 pr_err("Unable to locate config_item wwn_ci\n");
688 if (iscsit_get_tpg(tpg) < 0)
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));
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);
713 ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
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,
731 static int lio_target_init_nodeacl(struct se_node_acl *se_nacl,
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);
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, \
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); \
758 if (iscsit_get_tpg(tpg) < 0) \
761 rb = sprintf(page, "%u\n", tpg->tpg_attrib.name); \
762 iscsit_put_tpg(tpg); \
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); \
775 if (iscsit_get_tpg(tpg) < 0) \
778 ret = kstrtou32(page, 0, &val); \
781 ret = iscsit_ta_##name(tpg, val); \
785 iscsit_put_tpg(tpg); \
788 iscsit_put_tpg(tpg); \
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,
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, \
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)) \
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)) \
852 snprintf(auth->name, sizeof(auth->name), "%s", page); \
853 if (!(strncmp("NULL", auth->name, 4))) \
854 auth->naf_flags &= ~flags; \
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; \
862 auth->authenticate_target = 0; \
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, \
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, \
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)) \
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, \
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,
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, \
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; \
937 if (iscsit_get_tpg(tpg) < 0) \
940 param = iscsi_find_param_from_key(__stringify(name), \
943 iscsit_put_tpg(tpg); \
946 rb = snprintf(page, PAGE_SIZE, "%s\n", param->value); \
948 iscsit_put_tpg(tpg); \
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); \
960 buf = kzalloc(PAGE_SIZE, GFP_KERNEL); \
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) { \
972 ret = iscsi_change_param_value(buf, tpg->param_list, 1); \
977 iscsit_put_tpg(tpg); \
981 iscsit_put_tpg(tpg); \
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,
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);
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);
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);
1061 ret = kstrtou32(page, 0, &op);
1064 if ((op != 1) && (op != 0)) {
1065 pr_err("Illegal value for tpg_enable: %u\n", op);
1069 ret = iscsit_get_tpg(tpg);
1074 ret = iscsit_tpg_enable_portal_group(tpg);
1079 * iscsit_tpg_disable_portal_group() assumes force=1
1081 ret = iscsit_tpg_disable_portal_group(tpg, 1);
1086 iscsit_put_tpg(tpg);
1089 iscsit_put_tpg(tpg);
1094 static ssize_t lio_target_tpg_dynamic_sessions_show(struct config_item *item,
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,
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(
1115 struct config_group *group,
1118 struct iscsi_portal_group *tpg;
1119 struct iscsi_tiqn *tiqn;
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_");
1131 pr_err("Unable to locate \"tpgt_#\" directory"
1135 tpgt_str += 5; /* Skip ahead of "tpgt_" */
1136 ret = kstrtou16(tpgt_str, 0, &tpgt);
1140 tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1144 ret = core_tpg_register(wwn, &tpg->tpg_se_tpg, SCSI_PROTOCOL_ISCSI);
1148 ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1152 pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1153 pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1155 return &tpg->tpg_se_tpg;
1157 core_tpg_deregister(&tpg->tpg_se_tpg);
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,
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,
1193 static struct se_wwn *lio_target_call_coreaddtiqn(
1194 struct target_fabric_configfs *tf,
1195 struct config_group *group,
1198 struct iscsi_tiqn *tiqn;
1200 tiqn = iscsit_add_tiqn((unsigned char *)name);
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:"
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(
1243 struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1245 pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
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,\
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, \
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, \
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,
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;
1303 err = kstrtou32(page, 0, &op);
1306 if ((op != 1) && (op != 0)) {
1307 pr_err("Illegal value for enforce_discovery_auth:"
1312 if (!discovery_tpg) {
1313 pr_err("iscsit_global->discovery_tpg is NULL\n");
1317 param = iscsi_find_param_from_key(AUTHMETHOD,
1318 discovery_tpg->param_list);
1324 * Reset the AuthMethod key to CHAP.
1326 if (iscsi_update_param_value(param, CHAP) < 0)
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");
1336 * Reset the AuthMethod key to CHAP,None
1338 if (iscsi_update_param_value(param, "CHAP,None") < 0)
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");
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,
1363 /* End lio_target_discovery_auth_cit */
1365 /* Start functions for target_core_fabric_ops */
1367 static char *iscsi_get_fabric_name(void)
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,
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);
1401 struct iscsi_conn *conn = cmd->conn;
1403 cmd->i_state = ISTATE_SEND_DATAIN;
1404 return conn->conn_transport->iscsit_queue_data_in(conn, cmd);
1407 static int lio_write_pending(struct se_cmd *se_cmd)
1409 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1410 struct iscsi_conn *conn = cmd->conn;
1412 if (!cmd->immediate_data && !cmd->unsolicited_data)
1413 return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1418 static int lio_write_pending_status(struct se_cmd *se_cmd)
1420 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1423 spin_lock_bh(&cmd->istate_lock);
1424 ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1425 spin_unlock_bh(&cmd->istate_lock);
1430 static int lio_queue_status(struct se_cmd *se_cmd)
1432 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1433 struct iscsi_conn *conn = cmd->conn;
1435 cmd->i_state = ISTATE_SEND_STATUS;
1437 if (cmd->se_cmd.scsi_status || cmd->sense_reason) {
1438 return iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1440 return conn->conn_transport->iscsit_queue_status(conn, cmd);
1443 static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
1445 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1447 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1448 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1451 static void lio_aborted_task(struct se_cmd *se_cmd)
1453 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1455 cmd->conn->conn_transport->iscsit_aborted_task(cmd->conn, cmd);
1458 static inline struct iscsi_portal_group *iscsi_tpg(struct se_portal_group *se_tpg)
1460 return container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1463 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1465 return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn;
1468 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1470 return iscsi_tpg(se_tpg)->tpgt;
1473 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1475 return iscsi_tpg(se_tpg)->tpg_attrib.default_cmdsn_depth;
1478 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1480 return iscsi_tpg(se_tpg)->tpg_attrib.generate_node_acls;
1483 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1485 return iscsi_tpg(se_tpg)->tpg_attrib.cache_dynamic_acls;
1488 static int lio_tpg_check_demo_mode_write_protect(
1489 struct se_portal_group *se_tpg)
1491 return iscsi_tpg(se_tpg)->tpg_attrib.demo_mode_write_protect;
1494 static int lio_tpg_check_prod_mode_write_protect(
1495 struct se_portal_group *se_tpg)
1497 return iscsi_tpg(se_tpg)->tpg_attrib.prod_mode_write_protect;
1500 static int lio_tpg_check_prot_fabric_only(
1501 struct se_portal_group *se_tpg)
1504 * Only report fabric_prot_type if t10_pi has also been enabled
1505 * for incoming ib_isert sessions.
1507 if (!iscsi_tpg(se_tpg)->tpg_attrib.t10_pi)
1509 return iscsi_tpg(se_tpg)->tpg_attrib.fabric_prot_type;
1513 * This function calls iscsit_inc_session_usage_count() on the
1514 * struct iscsi_session in question.
1516 static void lio_tpg_close_session(struct se_session *se_sess)
1518 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1519 struct se_portal_group *se_tpg = &sess->tpg->tpg_se_tpg;
1521 spin_lock_bh(&se_tpg->session_lock);
1522 spin_lock(&sess->conn_lock);
1523 if (atomic_read(&sess->session_fall_back_to_erl0) ||
1524 atomic_read(&sess->session_logout) ||
1525 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1526 spin_unlock(&sess->conn_lock);
1527 spin_unlock_bh(&se_tpg->session_lock);
1530 atomic_set(&sess->session_reinstatement, 1);
1531 spin_unlock(&sess->conn_lock);
1533 iscsit_stop_time2retain_timer(sess);
1534 spin_unlock_bh(&se_tpg->session_lock);
1536 iscsit_stop_session(sess, 1, 1);
1537 iscsit_close_session(sess);
1540 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1542 return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn_index;
1545 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1547 struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1549 struct se_portal_group *se_tpg = se_acl->se_tpg;
1550 struct iscsi_portal_group *tpg = container_of(se_tpg,
1551 struct iscsi_portal_group, tpg_se_tpg);
1553 acl->node_attrib.nacl = acl;
1554 iscsit_set_default_node_attribues(acl, tpg);
1557 static int lio_check_stop_free(struct se_cmd *se_cmd)
1559 return target_put_sess_cmd(se_cmd);
1562 static void lio_release_cmd(struct se_cmd *se_cmd)
1564 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1566 pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
1567 iscsit_release_cmd(cmd);
1570 const struct target_core_fabric_ops iscsi_ops = {
1571 .module = THIS_MODULE,
1573 .node_acl_size = sizeof(struct iscsi_node_acl),
1574 .get_fabric_name = iscsi_get_fabric_name,
1575 .tpg_get_wwn = lio_tpg_get_endpoint_wwn,
1576 .tpg_get_tag = lio_tpg_get_tag,
1577 .tpg_get_default_depth = lio_tpg_get_default_depth,
1578 .tpg_check_demo_mode = lio_tpg_check_demo_mode,
1579 .tpg_check_demo_mode_cache = lio_tpg_check_demo_mode_cache,
1580 .tpg_check_demo_mode_write_protect =
1581 lio_tpg_check_demo_mode_write_protect,
1582 .tpg_check_prod_mode_write_protect =
1583 lio_tpg_check_prod_mode_write_protect,
1584 .tpg_check_prot_fabric_only = &lio_tpg_check_prot_fabric_only,
1585 .tpg_get_inst_index = lio_tpg_get_inst_index,
1586 .check_stop_free = lio_check_stop_free,
1587 .release_cmd = lio_release_cmd,
1588 .close_session = lio_tpg_close_session,
1589 .sess_get_index = lio_sess_get_index,
1590 .sess_get_initiator_sid = lio_sess_get_initiator_sid,
1591 .write_pending = lio_write_pending,
1592 .write_pending_status = lio_write_pending_status,
1593 .set_default_node_attributes = lio_set_default_node_attributes,
1594 .get_cmd_state = iscsi_get_cmd_state,
1595 .queue_data_in = lio_queue_data_in,
1596 .queue_status = lio_queue_status,
1597 .queue_tm_rsp = lio_queue_tm_rsp,
1598 .aborted_task = lio_aborted_task,
1599 .fabric_make_wwn = lio_target_call_coreaddtiqn,
1600 .fabric_drop_wwn = lio_target_call_coredeltiqn,
1601 .add_wwn_groups = lio_target_add_wwn_groups,
1602 .fabric_make_tpg = lio_target_tiqn_addtpg,
1603 .fabric_drop_tpg = lio_target_tiqn_deltpg,
1604 .fabric_make_np = lio_target_call_addnptotpg,
1605 .fabric_drop_np = lio_target_call_delnpfromtpg,
1606 .fabric_init_nodeacl = lio_target_init_nodeacl,
1608 .tfc_discovery_attrs = lio_target_discovery_auth_attrs,
1609 .tfc_wwn_attrs = lio_target_wwn_attrs,
1610 .tfc_tpg_base_attrs = lio_target_tpg_attrs,
1611 .tfc_tpg_attrib_attrs = lio_target_tpg_attrib_attrs,
1612 .tfc_tpg_auth_attrs = lio_target_tpg_auth_attrs,
1613 .tfc_tpg_param_attrs = lio_target_tpg_param_attrs,
1614 .tfc_tpg_np_base_attrs = lio_target_portal_attrs,
1615 .tfc_tpg_nacl_base_attrs = lio_target_initiator_attrs,
1616 .tfc_tpg_nacl_attrib_attrs = lio_target_nacl_attrib_attrs,
1617 .tfc_tpg_nacl_auth_attrs = lio_target_nacl_auth_attrs,
1618 .tfc_tpg_nacl_param_attrs = lio_target_nacl_param_attrs,