1c66ac9dbSNicholas Bellinger /*******************************************************************************
2c66ac9dbSNicholas Bellinger  * Filename:  target_core_configfs.c
3c66ac9dbSNicholas Bellinger  *
4c66ac9dbSNicholas Bellinger  * This file contains ConfigFS logic for the Generic Target Engine project.
5c66ac9dbSNicholas Bellinger  *
64c76251eSNicholas Bellinger  * (c) Copyright 2008-2013 Datera, Inc.
7c66ac9dbSNicholas Bellinger  *
8c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
9c66ac9dbSNicholas Bellinger  *
10c66ac9dbSNicholas Bellinger  * based on configfs Copyright (C) 2005 Oracle.  All rights reserved.
11c66ac9dbSNicholas Bellinger  *
12c66ac9dbSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
13c66ac9dbSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
14c66ac9dbSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
15c66ac9dbSNicholas Bellinger  * (at your option) any later version.
16c66ac9dbSNicholas Bellinger  *
17c66ac9dbSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
18c66ac9dbSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19c66ac9dbSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c66ac9dbSNicholas Bellinger  * GNU General Public License for more details.
21c66ac9dbSNicholas Bellinger  ****************************************************************************/
22c66ac9dbSNicholas Bellinger 
23c66ac9dbSNicholas Bellinger #include <linux/module.h>
24c66ac9dbSNicholas Bellinger #include <linux/moduleparam.h>
25c66ac9dbSNicholas Bellinger #include <generated/utsrelease.h>
26c66ac9dbSNicholas Bellinger #include <linux/utsname.h>
27c66ac9dbSNicholas Bellinger #include <linux/init.h>
28c66ac9dbSNicholas Bellinger #include <linux/fs.h>
29c66ac9dbSNicholas Bellinger #include <linux/namei.h>
30c66ac9dbSNicholas Bellinger #include <linux/slab.h>
31c66ac9dbSNicholas Bellinger #include <linux/types.h>
32c66ac9dbSNicholas Bellinger #include <linux/delay.h>
33c66ac9dbSNicholas Bellinger #include <linux/unistd.h>
34c66ac9dbSNicholas Bellinger #include <linux/string.h>
35c66ac9dbSNicholas Bellinger #include <linux/parser.h>
36c66ac9dbSNicholas Bellinger #include <linux/syscalls.h>
37c66ac9dbSNicholas Bellinger #include <linux/configfs.h>
38e3d6f909SAndy Grover #include <linux/spinlock.h>
39c66ac9dbSNicholas Bellinger 
40c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
41c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
42c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
43c66ac9dbSNicholas Bellinger 
44e26d99aeSChristoph Hellwig #include "target_core_internal.h"
45c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
46c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
47c66ac9dbSNicholas Bellinger #include "target_core_rd.h"
48f99715acSNicholas Bellinger #include "target_core_xcopy.h"
49c66ac9dbSNicholas Bellinger 
5073112edcSNicholas Bellinger #define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs)		\
510a06d430SChristoph Hellwig static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
5273112edcSNicholas Bellinger {									\
530a06d430SChristoph Hellwig 	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
5473112edcSNicholas Bellinger 									\
5573112edcSNicholas Bellinger 	cit->ct_item_ops = _item_ops;					\
5673112edcSNicholas Bellinger 	cit->ct_group_ops = _group_ops;					\
5773112edcSNicholas Bellinger 	cit->ct_attrs = _attrs;						\
580a06d430SChristoph Hellwig 	cit->ct_owner = tb->ops->owner;					\
590a06d430SChristoph Hellwig 	pr_debug("Setup generic %s\n", __stringify(_name));		\
600a06d430SChristoph Hellwig }
610a06d430SChristoph Hellwig 
620a06d430SChristoph Hellwig #define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops)			\
630a06d430SChristoph Hellwig static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
640a06d430SChristoph Hellwig {									\
650a06d430SChristoph Hellwig 	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
660a06d430SChristoph Hellwig 									\
670a06d430SChristoph Hellwig 	cit->ct_item_ops = _item_ops;					\
680a06d430SChristoph Hellwig 	cit->ct_group_ops = _group_ops;					\
690a06d430SChristoph Hellwig 	cit->ct_attrs = tb->ops->tb_##_name##_attrs;			\
700a06d430SChristoph Hellwig 	cit->ct_owner = tb->ops->owner;					\
7173112edcSNicholas Bellinger 	pr_debug("Setup generic %s\n", __stringify(_name));		\
7273112edcSNicholas Bellinger }
7373112edcSNicholas Bellinger 
74e3d6f909SAndy Grover extern struct t10_alua_lu_gp *default_lu_gp;
75e3d6f909SAndy Grover 
76d0f474e5SRoland Dreier static LIST_HEAD(g_tf_list);
77d0f474e5SRoland Dreier static DEFINE_MUTEX(g_tf_lock);
78c66ac9dbSNicholas Bellinger 
79e3d6f909SAndy Grover static struct config_group target_core_hbagroup;
80e3d6f909SAndy Grover static struct config_group alua_group;
81e3d6f909SAndy Grover static struct config_group alua_lu_gps_group;
82e3d6f909SAndy Grover 
83c66ac9dbSNicholas Bellinger static inline struct se_hba *
84c66ac9dbSNicholas Bellinger item_to_hba(struct config_item *item)
85c66ac9dbSNicholas Bellinger {
86c66ac9dbSNicholas Bellinger 	return container_of(to_config_group(item), struct se_hba, hba_group);
87c66ac9dbSNicholas Bellinger }
88c66ac9dbSNicholas Bellinger 
89c66ac9dbSNicholas Bellinger /*
90c66ac9dbSNicholas Bellinger  * Attributes for /sys/kernel/config/target/
91c66ac9dbSNicholas Bellinger  */
922eafd729SChristoph Hellwig static ssize_t target_core_item_version_show(struct config_item *item,
93c66ac9dbSNicholas Bellinger 		char *page)
94c66ac9dbSNicholas Bellinger {
95c66ac9dbSNicholas Bellinger 	return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
96ce8dd25dSChristoph Hellwig 		" on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
97c66ac9dbSNicholas Bellinger 		utsname()->sysname, utsname()->machine);
98c66ac9dbSNicholas Bellinger }
99c66ac9dbSNicholas Bellinger 
1002eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_core_item_, version);
101c66ac9dbSNicholas Bellinger 
102c66ac9dbSNicholas Bellinger static struct target_fabric_configfs *target_core_get_fabric(
103c66ac9dbSNicholas Bellinger 	const char *name)
104c66ac9dbSNicholas Bellinger {
105c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
106c66ac9dbSNicholas Bellinger 
1076708bb27SAndy Grover 	if (!name)
108c66ac9dbSNicholas Bellinger 		return NULL;
109c66ac9dbSNicholas Bellinger 
110c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
111c66ac9dbSNicholas Bellinger 	list_for_each_entry(tf, &g_tf_list, tf_list) {
1120dc2e8d1SChristoph Hellwig 		if (!strcmp(tf->tf_ops->name, name)) {
113c66ac9dbSNicholas Bellinger 			atomic_inc(&tf->tf_access_cnt);
114c66ac9dbSNicholas Bellinger 			mutex_unlock(&g_tf_lock);
115c66ac9dbSNicholas Bellinger 			return tf;
116c66ac9dbSNicholas Bellinger 		}
117c66ac9dbSNicholas Bellinger 	}
118c66ac9dbSNicholas Bellinger 	mutex_unlock(&g_tf_lock);
119c66ac9dbSNicholas Bellinger 
120c66ac9dbSNicholas Bellinger 	return NULL;
121c66ac9dbSNicholas Bellinger }
122c66ac9dbSNicholas Bellinger 
123c66ac9dbSNicholas Bellinger /*
124c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->make_group()
125c66ac9dbSNicholas Bellinger  */
126c66ac9dbSNicholas Bellinger static struct config_group *target_core_register_fabric(
127c66ac9dbSNicholas Bellinger 	struct config_group *group,
128c66ac9dbSNicholas Bellinger 	const char *name)
129c66ac9dbSNicholas Bellinger {
130c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
131c66ac9dbSNicholas Bellinger 	int ret;
132c66ac9dbSNicholas Bellinger 
1336708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
134c66ac9dbSNicholas Bellinger 			" %s\n", group, name);
135e7b7af6eSRoland Dreier 
136e7b7af6eSRoland Dreier 	tf = target_core_get_fabric(name);
137e7b7af6eSRoland Dreier 	if (!tf) {
13862554910SNicholas Bellinger 		pr_debug("target_core_register_fabric() trying autoload for %s\n",
139e7b7af6eSRoland Dreier 			 name);
140e7b7af6eSRoland Dreier 
141c66ac9dbSNicholas Bellinger 		/*
142c66ac9dbSNicholas Bellinger 		 * Below are some hardcoded request_module() calls to automatically
143c66ac9dbSNicholas Bellinger 		 * local fabric modules when the following is called:
144c66ac9dbSNicholas Bellinger 		 *
145c66ac9dbSNicholas Bellinger 		 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
146c66ac9dbSNicholas Bellinger 		 *
147c66ac9dbSNicholas Bellinger 		 * Note that this does not limit which TCM fabric module can be
148c66ac9dbSNicholas Bellinger 		 * registered, but simply provids auto loading logic for modules with
149c66ac9dbSNicholas Bellinger 		 * mkdir(2) system calls with known TCM fabric modules.
150c66ac9dbSNicholas Bellinger 		 */
151e7b7af6eSRoland Dreier 
1526708bb27SAndy Grover 		if (!strncmp(name, "iscsi", 5)) {
153c66ac9dbSNicholas Bellinger 			/*
154c66ac9dbSNicholas Bellinger 			 * Automatically load the LIO Target fabric module when the
155c66ac9dbSNicholas Bellinger 			 * following is called:
156c66ac9dbSNicholas Bellinger 			 *
157c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/iscsi
158c66ac9dbSNicholas Bellinger 			 */
159c66ac9dbSNicholas Bellinger 			ret = request_module("iscsi_target_mod");
160c66ac9dbSNicholas Bellinger 			if (ret < 0) {
16162554910SNicholas Bellinger 				pr_debug("request_module() failed for"
162c66ac9dbSNicholas Bellinger 				         " iscsi_target_mod.ko: %d\n", ret);
163c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
164c66ac9dbSNicholas Bellinger 			}
1656708bb27SAndy Grover 		} else if (!strncmp(name, "loopback", 8)) {
166c66ac9dbSNicholas Bellinger 			/*
167c66ac9dbSNicholas Bellinger 			 * Automatically load the tcm_loop fabric module when the
168c66ac9dbSNicholas Bellinger 			 * following is called:
169c66ac9dbSNicholas Bellinger 			 *
170c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/loopback
171c66ac9dbSNicholas Bellinger 			 */
172c66ac9dbSNicholas Bellinger 			ret = request_module("tcm_loop");
173c66ac9dbSNicholas Bellinger 			if (ret < 0) {
17462554910SNicholas Bellinger 				pr_debug("request_module() failed for"
175c66ac9dbSNicholas Bellinger 				         " tcm_loop.ko: %d\n", ret);
176c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
177c66ac9dbSNicholas Bellinger 			}
178c66ac9dbSNicholas Bellinger 		}
179c66ac9dbSNicholas Bellinger 
180c66ac9dbSNicholas Bellinger 		tf = target_core_get_fabric(name);
181e7b7af6eSRoland Dreier 	}
182e7b7af6eSRoland Dreier 
1836708bb27SAndy Grover 	if (!tf) {
18462554910SNicholas Bellinger 		pr_debug("target_core_get_fabric() failed for %s\n",
185c66ac9dbSNicholas Bellinger 		         name);
186c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
187c66ac9dbSNicholas Bellinger 	}
1886708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
1890dc2e8d1SChristoph Hellwig 			" %s\n", tf->tf_ops->name);
190c66ac9dbSNicholas Bellinger 	/*
191c66ac9dbSNicholas Bellinger 	 * On a successful target_core_get_fabric() look, the returned
192c66ac9dbSNicholas Bellinger 	 * struct target_fabric_configfs *tf will contain a usage reference.
193c66ac9dbSNicholas Bellinger 	 */
1946708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
195968ebe75SChristoph Hellwig 			&tf->tf_wwn_cit);
196c66ac9dbSNicholas Bellinger 
197c66ac9dbSNicholas Bellinger 	tf->tf_group.default_groups = tf->tf_default_groups;
198c66ac9dbSNicholas Bellinger 	tf->tf_group.default_groups[0] = &tf->tf_disc_group;
199c66ac9dbSNicholas Bellinger 	tf->tf_group.default_groups[1] = NULL;
200c66ac9dbSNicholas Bellinger 
201968ebe75SChristoph Hellwig 	config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
202c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
203968ebe75SChristoph Hellwig 			&tf->tf_discovery_cit);
204c66ac9dbSNicholas Bellinger 
2056708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
206c66ac9dbSNicholas Bellinger 			" %s\n", tf->tf_group.cg_item.ci_name);
207c66ac9dbSNicholas Bellinger 	return &tf->tf_group;
208c66ac9dbSNicholas Bellinger }
209c66ac9dbSNicholas Bellinger 
210c66ac9dbSNicholas Bellinger /*
211c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->drop_item()
212c66ac9dbSNicholas Bellinger  */
213c66ac9dbSNicholas Bellinger static void target_core_deregister_fabric(
214c66ac9dbSNicholas Bellinger 	struct config_group *group,
215c66ac9dbSNicholas Bellinger 	struct config_item *item)
216c66ac9dbSNicholas Bellinger {
217c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf = container_of(
218c66ac9dbSNicholas Bellinger 		to_config_group(item), struct target_fabric_configfs, tf_group);
219c66ac9dbSNicholas Bellinger 	struct config_group *tf_group;
220c66ac9dbSNicholas Bellinger 	struct config_item *df_item;
221c66ac9dbSNicholas Bellinger 	int i;
222c66ac9dbSNicholas Bellinger 
2236708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
224c66ac9dbSNicholas Bellinger 		" tf list\n", config_item_name(item));
225c66ac9dbSNicholas Bellinger 
2266708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
2270dc2e8d1SChristoph Hellwig 			" %s\n", tf->tf_ops->name);
228c66ac9dbSNicholas Bellinger 	atomic_dec(&tf->tf_access_cnt);
229c66ac9dbSNicholas Bellinger 
2306708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
231c66ac9dbSNicholas Bellinger 			" %s\n", config_item_name(item));
232c66ac9dbSNicholas Bellinger 
233c66ac9dbSNicholas Bellinger 	tf_group = &tf->tf_group;
234c66ac9dbSNicholas Bellinger 	for (i = 0; tf_group->default_groups[i]; i++) {
235c66ac9dbSNicholas Bellinger 		df_item = &tf_group->default_groups[i]->cg_item;
236c66ac9dbSNicholas Bellinger 		tf_group->default_groups[i] = NULL;
237c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
238c66ac9dbSNicholas Bellinger 	}
239c66ac9dbSNicholas Bellinger 	config_item_put(item);
240c66ac9dbSNicholas Bellinger }
241c66ac9dbSNicholas Bellinger 
242c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_fabric_group_ops = {
243c66ac9dbSNicholas Bellinger 	.make_group	= &target_core_register_fabric,
244c66ac9dbSNicholas Bellinger 	.drop_item	= &target_core_deregister_fabric,
245c66ac9dbSNicholas Bellinger };
246c66ac9dbSNicholas Bellinger 
247c66ac9dbSNicholas Bellinger /*
248c66ac9dbSNicholas Bellinger  * All item attributes appearing in /sys/kernel/target/ appear here.
249c66ac9dbSNicholas Bellinger  */
250c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_fabric_item_attrs[] = {
251c66ac9dbSNicholas Bellinger 	&target_core_item_attr_version,
252c66ac9dbSNicholas Bellinger 	NULL,
253c66ac9dbSNicholas Bellinger };
254c66ac9dbSNicholas Bellinger 
255c66ac9dbSNicholas Bellinger /*
256c66ac9dbSNicholas Bellinger  * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
257c66ac9dbSNicholas Bellinger  */
258c66ac9dbSNicholas Bellinger static struct config_item_type target_core_fabrics_item = {
259c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_fabric_group_ops,
260c66ac9dbSNicholas Bellinger 	.ct_attrs	= target_core_fabric_item_attrs,
261c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
262c66ac9dbSNicholas Bellinger };
263c66ac9dbSNicholas Bellinger 
264c66ac9dbSNicholas Bellinger static struct configfs_subsystem target_core_fabrics = {
265c66ac9dbSNicholas Bellinger 	.su_group = {
266c66ac9dbSNicholas Bellinger 		.cg_item = {
267c66ac9dbSNicholas Bellinger 			.ci_namebuf = "target",
268c66ac9dbSNicholas Bellinger 			.ci_type = &target_core_fabrics_item,
269c66ac9dbSNicholas Bellinger 		},
270c66ac9dbSNicholas Bellinger 	},
271c66ac9dbSNicholas Bellinger };
272c66ac9dbSNicholas Bellinger 
273d588cf8fSChristoph Hellwig int target_depend_item(struct config_item *item)
274d588cf8fSChristoph Hellwig {
275d588cf8fSChristoph Hellwig 	return configfs_depend_item(&target_core_fabrics, item);
276d588cf8fSChristoph Hellwig }
277d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_depend_item);
278d588cf8fSChristoph Hellwig 
279d588cf8fSChristoph Hellwig void target_undepend_item(struct config_item *item)
280d588cf8fSChristoph Hellwig {
281d588cf8fSChristoph Hellwig 	return configfs_undepend_item(&target_core_fabrics, item);
282d588cf8fSChristoph Hellwig }
283d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_undepend_item);
284c66ac9dbSNicholas Bellinger 
285c66ac9dbSNicholas Bellinger /*##############################################################################
286c66ac9dbSNicholas Bellinger // Start functions called by external Target Fabrics Modules
287c66ac9dbSNicholas Bellinger //############################################################################*/
288c66ac9dbSNicholas Bellinger 
2899ac8928eSChristoph Hellwig static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
290c66ac9dbSNicholas Bellinger {
2919ac8928eSChristoph Hellwig 	if (!tfo->name) {
2929ac8928eSChristoph Hellwig 		pr_err("Missing tfo->name\n");
2939ac8928eSChristoph Hellwig 		return -EINVAL;
294c66ac9dbSNicholas Bellinger 	}
2959ac8928eSChristoph Hellwig 	if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
2966708bb27SAndy Grover 		pr_err("Passed name: %s exceeds TARGET_FABRIC"
2979ac8928eSChristoph Hellwig 			"_NAME_SIZE\n", tfo->name);
2989ac8928eSChristoph Hellwig 		return -EINVAL;
299c66ac9dbSNicholas Bellinger 	}
3006708bb27SAndy Grover 	if (!tfo->get_fabric_name) {
3016708bb27SAndy Grover 		pr_err("Missing tfo->get_fabric_name()\n");
302c66ac9dbSNicholas Bellinger 		return -EINVAL;
303c66ac9dbSNicholas Bellinger 	}
3046708bb27SAndy Grover 	if (!tfo->tpg_get_wwn) {
3056708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_wwn()\n");
306c66ac9dbSNicholas Bellinger 		return -EINVAL;
307c66ac9dbSNicholas Bellinger 	}
3086708bb27SAndy Grover 	if (!tfo->tpg_get_tag) {
3096708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_tag()\n");
310c66ac9dbSNicholas Bellinger 		return -EINVAL;
311c66ac9dbSNicholas Bellinger 	}
3126708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode) {
3136708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode()\n");
314c66ac9dbSNicholas Bellinger 		return -EINVAL;
315c66ac9dbSNicholas Bellinger 	}
3166708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_cache) {
3176708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
318c66ac9dbSNicholas Bellinger 		return -EINVAL;
319c66ac9dbSNicholas Bellinger 	}
3206708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_write_protect) {
3216708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
322c66ac9dbSNicholas Bellinger 		return -EINVAL;
323c66ac9dbSNicholas Bellinger 	}
3246708bb27SAndy Grover 	if (!tfo->tpg_check_prod_mode_write_protect) {
3256708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
326c66ac9dbSNicholas Bellinger 		return -EINVAL;
327c66ac9dbSNicholas Bellinger 	}
3286708bb27SAndy Grover 	if (!tfo->tpg_get_inst_index) {
3296708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_inst_index()\n");
330c66ac9dbSNicholas Bellinger 		return -EINVAL;
331c66ac9dbSNicholas Bellinger 	}
33235462975SChristoph Hellwig 	if (!tfo->release_cmd) {
3336708bb27SAndy Grover 		pr_err("Missing tfo->release_cmd()\n");
334c66ac9dbSNicholas Bellinger 		return -EINVAL;
335c66ac9dbSNicholas Bellinger 	}
3366708bb27SAndy Grover 	if (!tfo->shutdown_session) {
3376708bb27SAndy Grover 		pr_err("Missing tfo->shutdown_session()\n");
338c66ac9dbSNicholas Bellinger 		return -EINVAL;
339c66ac9dbSNicholas Bellinger 	}
3406708bb27SAndy Grover 	if (!tfo->close_session) {
3416708bb27SAndy Grover 		pr_err("Missing tfo->close_session()\n");
342c66ac9dbSNicholas Bellinger 		return -EINVAL;
343c66ac9dbSNicholas Bellinger 	}
3446708bb27SAndy Grover 	if (!tfo->sess_get_index) {
3456708bb27SAndy Grover 		pr_err("Missing tfo->sess_get_index()\n");
346c66ac9dbSNicholas Bellinger 		return -EINVAL;
347c66ac9dbSNicholas Bellinger 	}
3486708bb27SAndy Grover 	if (!tfo->write_pending) {
3496708bb27SAndy Grover 		pr_err("Missing tfo->write_pending()\n");
350c66ac9dbSNicholas Bellinger 		return -EINVAL;
351c66ac9dbSNicholas Bellinger 	}
3526708bb27SAndy Grover 	if (!tfo->write_pending_status) {
3536708bb27SAndy Grover 		pr_err("Missing tfo->write_pending_status()\n");
354c66ac9dbSNicholas Bellinger 		return -EINVAL;
355c66ac9dbSNicholas Bellinger 	}
3566708bb27SAndy Grover 	if (!tfo->set_default_node_attributes) {
3576708bb27SAndy Grover 		pr_err("Missing tfo->set_default_node_attributes()\n");
358c66ac9dbSNicholas Bellinger 		return -EINVAL;
359c66ac9dbSNicholas Bellinger 	}
3606708bb27SAndy Grover 	if (!tfo->get_cmd_state) {
3616708bb27SAndy Grover 		pr_err("Missing tfo->get_cmd_state()\n");
362c66ac9dbSNicholas Bellinger 		return -EINVAL;
363c66ac9dbSNicholas Bellinger 	}
3646708bb27SAndy Grover 	if (!tfo->queue_data_in) {
3656708bb27SAndy Grover 		pr_err("Missing tfo->queue_data_in()\n");
366c66ac9dbSNicholas Bellinger 		return -EINVAL;
367c66ac9dbSNicholas Bellinger 	}
3686708bb27SAndy Grover 	if (!tfo->queue_status) {
3696708bb27SAndy Grover 		pr_err("Missing tfo->queue_status()\n");
370c66ac9dbSNicholas Bellinger 		return -EINVAL;
371c66ac9dbSNicholas Bellinger 	}
3726708bb27SAndy Grover 	if (!tfo->queue_tm_rsp) {
3736708bb27SAndy Grover 		pr_err("Missing tfo->queue_tm_rsp()\n");
374c66ac9dbSNicholas Bellinger 		return -EINVAL;
375c66ac9dbSNicholas Bellinger 	}
376131e6abcSNicholas Bellinger 	if (!tfo->aborted_task) {
377131e6abcSNicholas Bellinger 		pr_err("Missing tfo->aborted_task()\n");
378131e6abcSNicholas Bellinger 		return -EINVAL;
379131e6abcSNicholas Bellinger 	}
380c66ac9dbSNicholas Bellinger 	/*
381c66ac9dbSNicholas Bellinger 	 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
382c66ac9dbSNicholas Bellinger 	 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
383c66ac9dbSNicholas Bellinger 	 * target_core_fabric_configfs.c WWN+TPG group context code.
384c66ac9dbSNicholas Bellinger 	 */
3856708bb27SAndy Grover 	if (!tfo->fabric_make_wwn) {
3866708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_wwn()\n");
387c66ac9dbSNicholas Bellinger 		return -EINVAL;
388c66ac9dbSNicholas Bellinger 	}
3896708bb27SAndy Grover 	if (!tfo->fabric_drop_wwn) {
3906708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_wwn()\n");
391c66ac9dbSNicholas Bellinger 		return -EINVAL;
392c66ac9dbSNicholas Bellinger 	}
3936708bb27SAndy Grover 	if (!tfo->fabric_make_tpg) {
3946708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_tpg()\n");
395c66ac9dbSNicholas Bellinger 		return -EINVAL;
396c66ac9dbSNicholas Bellinger 	}
3976708bb27SAndy Grover 	if (!tfo->fabric_drop_tpg) {
3986708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_tpg()\n");
399c66ac9dbSNicholas Bellinger 		return -EINVAL;
400c66ac9dbSNicholas Bellinger 	}
401c66ac9dbSNicholas Bellinger 
402c66ac9dbSNicholas Bellinger 	return 0;
403c66ac9dbSNicholas Bellinger }
404c66ac9dbSNicholas Bellinger 
4059ac8928eSChristoph Hellwig int target_register_template(const struct target_core_fabric_ops *fo)
406c66ac9dbSNicholas Bellinger {
4079ac8928eSChristoph Hellwig 	struct target_fabric_configfs *tf;
408c66ac9dbSNicholas Bellinger 	int ret;
409c66ac9dbSNicholas Bellinger 
4109ac8928eSChristoph Hellwig 	ret = target_fabric_tf_ops_check(fo);
4119ac8928eSChristoph Hellwig 	if (ret)
412c66ac9dbSNicholas Bellinger 		return ret;
413c66ac9dbSNicholas Bellinger 
4149ac8928eSChristoph Hellwig 	tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
4159ac8928eSChristoph Hellwig 	if (!tf) {
4169ac8928eSChristoph Hellwig 		pr_err("%s: could not allocate memory!\n", __func__);
4179ac8928eSChristoph Hellwig 		return -ENOMEM;
4189ac8928eSChristoph Hellwig 	}
4199ac8928eSChristoph Hellwig 
4209ac8928eSChristoph Hellwig 	INIT_LIST_HEAD(&tf->tf_list);
4219ac8928eSChristoph Hellwig 	atomic_set(&tf->tf_access_cnt, 0);
422ef0caf8dSChristoph Hellwig 	tf->tf_ops = fo;
4239ac8928eSChristoph Hellwig 	target_fabric_setup_cits(tf);
4249ac8928eSChristoph Hellwig 
4259ac8928eSChristoph Hellwig 	mutex_lock(&g_tf_lock);
4269ac8928eSChristoph Hellwig 	list_add_tail(&tf->tf_list, &g_tf_list);
4279ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4289ac8928eSChristoph Hellwig 
429c66ac9dbSNicholas Bellinger 	return 0;
430c66ac9dbSNicholas Bellinger }
4319ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_register_template);
432c66ac9dbSNicholas Bellinger 
4339ac8928eSChristoph Hellwig void target_unregister_template(const struct target_core_fabric_ops *fo)
434c66ac9dbSNicholas Bellinger {
4359ac8928eSChristoph Hellwig 	struct target_fabric_configfs *t;
436c66ac9dbSNicholas Bellinger 
437c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
4389ac8928eSChristoph Hellwig 	list_for_each_entry(t, &g_tf_list, tf_list) {
4390dc2e8d1SChristoph Hellwig 		if (!strcmp(t->tf_ops->name, fo->name)) {
4409ac8928eSChristoph Hellwig 			BUG_ON(atomic_read(&t->tf_access_cnt));
4419ac8928eSChristoph Hellwig 			list_del(&t->tf_list);
44294509182SNicholas Bellinger 			mutex_unlock(&g_tf_lock);
44394509182SNicholas Bellinger 			/*
44494509182SNicholas Bellinger 			 * Wait for any outstanding fabric se_deve_entry->rcu_head
44594509182SNicholas Bellinger 			 * callbacks to complete post kfree_rcu(), before allowing
44694509182SNicholas Bellinger 			 * fabric driver unload of TFO->module to proceed.
44794509182SNicholas Bellinger 			 */
44894509182SNicholas Bellinger 			rcu_barrier();
4499ac8928eSChristoph Hellwig 			kfree(t);
45094509182SNicholas Bellinger 			return;
451c66ac9dbSNicholas Bellinger 		}
452c66ac9dbSNicholas Bellinger 	}
4539ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4549ac8928eSChristoph Hellwig }
4559ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_unregister_template);
456c66ac9dbSNicholas Bellinger 
457c66ac9dbSNicholas Bellinger /*##############################################################################
458c66ac9dbSNicholas Bellinger // Stop functions called by external Target Fabrics Modules
459c66ac9dbSNicholas Bellinger //############################################################################*/
460c66ac9dbSNicholas Bellinger 
4612eafd729SChristoph Hellwig static inline struct se_dev_attrib *to_attrib(struct config_item *item)
4622eafd729SChristoph Hellwig {
4632eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_dev_attrib,
4642eafd729SChristoph Hellwig 			da_group);
4655873c4d1SChristoph Hellwig }
4665873c4d1SChristoph Hellwig 
4672eafd729SChristoph Hellwig /* Start functions for struct config_item_type tb_dev_attrib_cit */
4682eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_SHOW(_name)					\
4692eafd729SChristoph Hellwig static ssize_t _name##_show(struct config_item *item, char *page)	\
4702eafd729SChristoph Hellwig {									\
4712eafd729SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
4722eafd729SChristoph Hellwig }
4733effdb90SChristoph Hellwig 
4742eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
4752eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
4762eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
4772eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
4782eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
4792eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
4802eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
4812eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
4822eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
4832eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
4842eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
4852eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
4862eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
4872eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_format);
4882eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
4892eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
4902eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
4912eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
4922eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
4932eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(block_size);
4942eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
4952eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
4962eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
4972eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
4982eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
4992eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
5002eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
5012eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
5022eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
5032eafd729SChristoph Hellwig 
5042eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_U32(_name)				\
5052eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
5065873c4d1SChristoph Hellwig 		size_t count)						\
5075873c4d1SChristoph Hellwig {									\
5082eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5093effdb90SChristoph Hellwig 	u32 val;							\
5105873c4d1SChristoph Hellwig 	int ret;							\
5115873c4d1SChristoph Hellwig 									\
5123effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);					\
5133effdb90SChristoph Hellwig 	if (ret < 0)							\
5143effdb90SChristoph Hellwig 		return ret;						\
5153effdb90SChristoph Hellwig 	da->_name = val;						\
5163effdb90SChristoph Hellwig 	return count;							\
5175873c4d1SChristoph Hellwig }
5185873c4d1SChristoph Hellwig 
5192eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
5202eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
5212eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
5222eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
5232eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
5245873c4d1SChristoph Hellwig 
5252eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name)				\
5262eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,	\
5273effdb90SChristoph Hellwig 		size_t count)						\
5283effdb90SChristoph Hellwig {									\
5292eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5303effdb90SChristoph Hellwig 	bool flag;							\
5313effdb90SChristoph Hellwig 	int ret;							\
5323effdb90SChristoph Hellwig 									\
5333effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);					\
5343effdb90SChristoph Hellwig 	if (ret < 0)							\
5353effdb90SChristoph Hellwig 		return ret;						\
5363effdb90SChristoph Hellwig 	da->_name = flag;						\
5373effdb90SChristoph Hellwig 	return count;							\
5383effdb90SChristoph Hellwig }
5393effdb90SChristoph Hellwig 
5402eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
5412eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
5422eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
5432eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
5442eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
5453effdb90SChristoph Hellwig 
5462eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name)				\
5472eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
5483effdb90SChristoph Hellwig 		size_t count)						\
5493effdb90SChristoph Hellwig {									\
5503effdb90SChristoph Hellwig 	printk_once(KERN_WARNING					\
5513effdb90SChristoph Hellwig 		"ignoring deprecated ##_name## attribute\n");	\
5523effdb90SChristoph Hellwig 	return count;							\
5533effdb90SChristoph Hellwig }
5543effdb90SChristoph Hellwig 
5552eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
5562eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
5573effdb90SChristoph Hellwig 
5583effdb90SChristoph Hellwig static void dev_set_t10_wwn_model_alias(struct se_device *dev)
5593effdb90SChristoph Hellwig {
5603effdb90SChristoph Hellwig 	const char *configname;
5613effdb90SChristoph Hellwig 
5623effdb90SChristoph Hellwig 	configname = config_item_name(&dev->dev_group.cg_item);
5633effdb90SChristoph Hellwig 	if (strlen(configname) >= 16) {
5643effdb90SChristoph Hellwig 		pr_warn("dev[%p]: Backstore name '%s' is too long for "
5653effdb90SChristoph Hellwig 			"INQUIRY_MODEL, truncating to 16 bytes\n", dev,
5663effdb90SChristoph Hellwig 			configname);
5673effdb90SChristoph Hellwig 	}
5683effdb90SChristoph Hellwig 	snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
5693effdb90SChristoph Hellwig }
5703effdb90SChristoph Hellwig 
5712eafd729SChristoph Hellwig static ssize_t emulate_model_alias_store(struct config_item *item,
5723effdb90SChristoph Hellwig 		const char *page, size_t count)
5733effdb90SChristoph Hellwig {
5742eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
5753effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
5763effdb90SChristoph Hellwig 	bool flag;
5773effdb90SChristoph Hellwig 	int ret;
5783effdb90SChristoph Hellwig 
5793effdb90SChristoph Hellwig 	if (dev->export_count) {
5803effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change model alias"
5813effdb90SChristoph Hellwig 			" while export_count is %d\n",
5823effdb90SChristoph Hellwig 			dev, dev->export_count);
5833effdb90SChristoph Hellwig 		return -EINVAL;
5843effdb90SChristoph Hellwig 	}
5853effdb90SChristoph Hellwig 
5863effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
5873effdb90SChristoph Hellwig 	if (ret < 0)
5883effdb90SChristoph Hellwig 		return ret;
5893effdb90SChristoph Hellwig 
5903effdb90SChristoph Hellwig 	if (flag) {
5913effdb90SChristoph Hellwig 		dev_set_t10_wwn_model_alias(dev);
5923effdb90SChristoph Hellwig 	} else {
5933effdb90SChristoph Hellwig 		strncpy(&dev->t10_wwn.model[0],
5943effdb90SChristoph Hellwig 			dev->transport->inquiry_prod, 16);
5953effdb90SChristoph Hellwig 	}
5963effdb90SChristoph Hellwig 	da->emulate_model_alias = flag;
5973effdb90SChristoph Hellwig 	return count;
5983effdb90SChristoph Hellwig }
5993effdb90SChristoph Hellwig 
6002eafd729SChristoph Hellwig static ssize_t emulate_write_cache_store(struct config_item *item,
6013effdb90SChristoph Hellwig 		const char *page, size_t count)
6023effdb90SChristoph Hellwig {
6032eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6043effdb90SChristoph Hellwig 	bool flag;
6053effdb90SChristoph Hellwig 	int ret;
6063effdb90SChristoph Hellwig 
6073effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6083effdb90SChristoph Hellwig 	if (ret < 0)
6093effdb90SChristoph Hellwig 		return ret;
6103effdb90SChristoph Hellwig 
6113effdb90SChristoph Hellwig 	if (flag && da->da_dev->transport->get_write_cache) {
6123effdb90SChristoph Hellwig 		pr_err("emulate_write_cache not supported for this device\n");
6133effdb90SChristoph Hellwig 		return -EINVAL;
6143effdb90SChristoph Hellwig 	}
6153effdb90SChristoph Hellwig 
6163effdb90SChristoph Hellwig 	da->emulate_write_cache = flag;
6173effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
6183effdb90SChristoph Hellwig 			da->da_dev, flag);
6193effdb90SChristoph Hellwig 	return count;
6203effdb90SChristoph Hellwig }
6213effdb90SChristoph Hellwig 
6222eafd729SChristoph Hellwig static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
6233effdb90SChristoph Hellwig 		const char *page, size_t count)
6243effdb90SChristoph Hellwig {
6252eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6263effdb90SChristoph Hellwig 	u32 val;
6273effdb90SChristoph Hellwig 	int ret;
6283effdb90SChristoph Hellwig 
6293effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
6303effdb90SChristoph Hellwig 	if (ret < 0)
6313effdb90SChristoph Hellwig 		return ret;
6323effdb90SChristoph Hellwig 
6333effdb90SChristoph Hellwig 	if (val != 0 && val != 1 && val != 2) {
6343effdb90SChristoph Hellwig 		pr_err("Illegal value %d\n", val);
6353effdb90SChristoph Hellwig 		return -EINVAL;
6363effdb90SChristoph Hellwig 	}
6373effdb90SChristoph Hellwig 
6383effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
6393effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
6403effdb90SChristoph Hellwig 			" UA_INTRLCK_CTRL while export_count is %d\n",
6413effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
6423effdb90SChristoph Hellwig 		return -EINVAL;
6433effdb90SChristoph Hellwig 	}
6443effdb90SChristoph Hellwig 	da->emulate_ua_intlck_ctrl = val;
6453effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
6463effdb90SChristoph Hellwig 		da->da_dev, val);
6473effdb90SChristoph Hellwig 	return count;
6483effdb90SChristoph Hellwig }
6493effdb90SChristoph Hellwig 
6502eafd729SChristoph Hellwig static ssize_t emulate_tas_store(struct config_item *item,
6513effdb90SChristoph Hellwig 		const char *page, size_t count)
6523effdb90SChristoph Hellwig {
6532eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6543effdb90SChristoph Hellwig 	bool flag;
6553effdb90SChristoph Hellwig 	int ret;
6563effdb90SChristoph Hellwig 
6573effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6583effdb90SChristoph Hellwig 	if (ret < 0)
6593effdb90SChristoph Hellwig 		return ret;
6603effdb90SChristoph Hellwig 
6613effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
6623effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TAS while"
6633effdb90SChristoph Hellwig 			" export_count is %d\n",
6643effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
6653effdb90SChristoph Hellwig 		return -EINVAL;
6663effdb90SChristoph Hellwig 	}
6673effdb90SChristoph Hellwig 	da->emulate_tas = flag;
6683effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
6693effdb90SChristoph Hellwig 		da->da_dev, flag ? "Enabled" : "Disabled");
6703effdb90SChristoph Hellwig 
6713effdb90SChristoph Hellwig 	return count;
6723effdb90SChristoph Hellwig }
6733effdb90SChristoph Hellwig 
6742eafd729SChristoph Hellwig static ssize_t emulate_tpu_store(struct config_item *item,
6753effdb90SChristoph Hellwig 		const char *page, size_t count)
6763effdb90SChristoph Hellwig {
6772eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6783effdb90SChristoph Hellwig 	bool flag;
6793effdb90SChristoph Hellwig 	int ret;
6803effdb90SChristoph Hellwig 
6813effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6823effdb90SChristoph Hellwig 	if (ret < 0)
6833effdb90SChristoph Hellwig 		return ret;
6843effdb90SChristoph Hellwig 
6853effdb90SChristoph Hellwig 	/*
6863effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
6873effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
6883effdb90SChristoph Hellwig 	 */
6893effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
6903effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
6913effdb90SChristoph Hellwig 		return -ENOSYS;
6923effdb90SChristoph Hellwig 	}
6933effdb90SChristoph Hellwig 
6943effdb90SChristoph Hellwig 	da->emulate_tpu = flag;
6953effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
6963effdb90SChristoph Hellwig 		da->da_dev, flag);
6973effdb90SChristoph Hellwig 	return count;
6983effdb90SChristoph Hellwig }
6993effdb90SChristoph Hellwig 
7002eafd729SChristoph Hellwig static ssize_t emulate_tpws_store(struct config_item *item,
7013effdb90SChristoph Hellwig 		const char *page, size_t count)
7023effdb90SChristoph Hellwig {
7032eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7043effdb90SChristoph Hellwig 	bool flag;
7053effdb90SChristoph Hellwig 	int ret;
7063effdb90SChristoph Hellwig 
7073effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7083effdb90SChristoph Hellwig 	if (ret < 0)
7093effdb90SChristoph Hellwig 		return ret;
7103effdb90SChristoph Hellwig 
7113effdb90SChristoph Hellwig 	/*
7123effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
7133effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
7143effdb90SChristoph Hellwig 	 */
7153effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
7163effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
7173effdb90SChristoph Hellwig 		return -ENOSYS;
7183effdb90SChristoph Hellwig 	}
7193effdb90SChristoph Hellwig 
7203effdb90SChristoph Hellwig 	da->emulate_tpws = flag;
7213effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
7223effdb90SChristoph Hellwig 				da->da_dev, flag);
7233effdb90SChristoph Hellwig 	return count;
7243effdb90SChristoph Hellwig }
7253effdb90SChristoph Hellwig 
7262eafd729SChristoph Hellwig static ssize_t pi_prot_type_store(struct config_item *item,
7273effdb90SChristoph Hellwig 		const char *page, size_t count)
7283effdb90SChristoph Hellwig {
7292eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7303effdb90SChristoph Hellwig 	int old_prot = da->pi_prot_type, ret;
7313effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
7323effdb90SChristoph Hellwig 	u32 flag;
7333effdb90SChristoph Hellwig 
7343effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &flag);
7353effdb90SChristoph Hellwig 	if (ret < 0)
7363effdb90SChristoph Hellwig 		return ret;
7373effdb90SChristoph Hellwig 
7383effdb90SChristoph Hellwig 	if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
7393effdb90SChristoph Hellwig 		pr_err("Illegal value %d for pi_prot_type\n", flag);
7403effdb90SChristoph Hellwig 		return -EINVAL;
7413effdb90SChristoph Hellwig 	}
7423effdb90SChristoph Hellwig 	if (flag == 2) {
7433effdb90SChristoph Hellwig 		pr_err("DIF TYPE2 protection currently not supported\n");
7443effdb90SChristoph Hellwig 		return -ENOSYS;
7453effdb90SChristoph Hellwig 	}
7463effdb90SChristoph Hellwig 	if (da->hw_pi_prot_type) {
7473effdb90SChristoph Hellwig 		pr_warn("DIF protection enabled on underlying hardware,"
7483effdb90SChristoph Hellwig 			" ignoring\n");
7493effdb90SChristoph Hellwig 		return count;
7503effdb90SChristoph Hellwig 	}
7513effdb90SChristoph Hellwig 	if (!dev->transport->init_prot || !dev->transport->free_prot) {
7523effdb90SChristoph Hellwig 		/* 0 is only allowed value for non-supporting backends */
7533effdb90SChristoph Hellwig 		if (flag == 0)
754bc1a7d6aSAndy Grover 			return count;
7553effdb90SChristoph Hellwig 
7563effdb90SChristoph Hellwig 		pr_err("DIF protection not supported by backend: %s\n",
7573effdb90SChristoph Hellwig 		       dev->transport->name);
7583effdb90SChristoph Hellwig 		return -ENOSYS;
7593effdb90SChristoph Hellwig 	}
7603effdb90SChristoph Hellwig 	if (!(dev->dev_flags & DF_CONFIGURED)) {
7613effdb90SChristoph Hellwig 		pr_err("DIF protection requires device to be configured\n");
7623effdb90SChristoph Hellwig 		return -ENODEV;
7633effdb90SChristoph Hellwig 	}
7643effdb90SChristoph Hellwig 	if (dev->export_count) {
7653effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device PROT type while"
7663effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
7673effdb90SChristoph Hellwig 		return -EINVAL;
7683effdb90SChristoph Hellwig 	}
7693effdb90SChristoph Hellwig 
7703effdb90SChristoph Hellwig 	da->pi_prot_type = flag;
7713effdb90SChristoph Hellwig 
7723effdb90SChristoph Hellwig 	if (flag && !old_prot) {
7733effdb90SChristoph Hellwig 		ret = dev->transport->init_prot(dev);
7743effdb90SChristoph Hellwig 		if (ret) {
7753effdb90SChristoph Hellwig 			da->pi_prot_type = old_prot;
7763effdb90SChristoph Hellwig 			return ret;
7773effdb90SChristoph Hellwig 		}
7783effdb90SChristoph Hellwig 
7793effdb90SChristoph Hellwig 	} else if (!flag && old_prot) {
7803effdb90SChristoph Hellwig 		dev->transport->free_prot(dev);
7813effdb90SChristoph Hellwig 	}
7823effdb90SChristoph Hellwig 
7833effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
7843effdb90SChristoph Hellwig 	return count;
7853effdb90SChristoph Hellwig }
7863effdb90SChristoph Hellwig 
7872eafd729SChristoph Hellwig static ssize_t pi_prot_format_store(struct config_item *item,
7883effdb90SChristoph Hellwig 		const char *page, size_t count)
7893effdb90SChristoph Hellwig {
7902eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7913effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
7923effdb90SChristoph Hellwig 	bool flag;
7933effdb90SChristoph Hellwig 	int ret;
7943effdb90SChristoph Hellwig 
7953effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7963effdb90SChristoph Hellwig 	if (ret < 0)
7973effdb90SChristoph Hellwig 		return ret;
7983effdb90SChristoph Hellwig 
7993effdb90SChristoph Hellwig 	if (!flag)
8003effdb90SChristoph Hellwig 		return count;
8013effdb90SChristoph Hellwig 
8023effdb90SChristoph Hellwig 	if (!dev->transport->format_prot) {
8033effdb90SChristoph Hellwig 		pr_err("DIF protection format not supported by backend %s\n",
8043effdb90SChristoph Hellwig 		       dev->transport->name);
8053effdb90SChristoph Hellwig 		return -ENOSYS;
8063effdb90SChristoph Hellwig 	}
8073effdb90SChristoph Hellwig 	if (!(dev->dev_flags & DF_CONFIGURED)) {
8083effdb90SChristoph Hellwig 		pr_err("DIF protection format requires device to be configured\n");
8093effdb90SChristoph Hellwig 		return -ENODEV;
8103effdb90SChristoph Hellwig 	}
8113effdb90SChristoph Hellwig 	if (dev->export_count) {
8123effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to format SE Device PROT type while"
8133effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
8143effdb90SChristoph Hellwig 		return -EINVAL;
8153effdb90SChristoph Hellwig 	}
8163effdb90SChristoph Hellwig 
8173effdb90SChristoph Hellwig 	ret = dev->transport->format_prot(dev);
8183effdb90SChristoph Hellwig 	if (ret)
8193effdb90SChristoph Hellwig 		return ret;
8203effdb90SChristoph Hellwig 
8213effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
8223effdb90SChristoph Hellwig 	return count;
8233effdb90SChristoph Hellwig }
8243effdb90SChristoph Hellwig 
8252eafd729SChristoph Hellwig static ssize_t force_pr_aptpl_store(struct config_item *item,
8263effdb90SChristoph Hellwig 		const char *page, size_t count)
8273effdb90SChristoph Hellwig {
8282eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
8293effdb90SChristoph Hellwig 	bool flag;
8303effdb90SChristoph Hellwig 	int ret;
8313effdb90SChristoph Hellwig 
8323effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
8333effdb90SChristoph Hellwig 	if (ret < 0)
8343effdb90SChristoph Hellwig 		return ret;
8353effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
8363effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to set force_pr_aptpl while"
8373effdb90SChristoph Hellwig 		       " export_count is %d\n",
8383effdb90SChristoph Hellwig 		       da->da_dev, da->da_dev->export_count);
8393effdb90SChristoph Hellwig 		return -EINVAL;
8403effdb90SChristoph Hellwig 	}
8413effdb90SChristoph Hellwig 
8423effdb90SChristoph Hellwig 	da->force_pr_aptpl = flag;
8433effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
8443effdb90SChristoph Hellwig 	return count;
8453effdb90SChristoph Hellwig }
8463effdb90SChristoph Hellwig 
8472eafd729SChristoph Hellwig static ssize_t emulate_rest_reord_store(struct config_item *item,
8483effdb90SChristoph Hellwig 		const char *page, size_t count)
8493effdb90SChristoph Hellwig {
8502eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
8513effdb90SChristoph Hellwig 	bool flag;
8523effdb90SChristoph Hellwig 	int ret;
8533effdb90SChristoph Hellwig 
8543effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
8553effdb90SChristoph Hellwig 	if (ret < 0)
8563effdb90SChristoph Hellwig 		return ret;
8573effdb90SChristoph Hellwig 
8583effdb90SChristoph Hellwig 	if (flag != 0) {
8593effdb90SChristoph Hellwig 		printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
8603effdb90SChristoph Hellwig 			" reordering not implemented\n", da->da_dev);
8613effdb90SChristoph Hellwig 		return -ENOSYS;
8623effdb90SChristoph Hellwig 	}
8633effdb90SChristoph Hellwig 	da->emulate_rest_reord = flag;
8643effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
8653effdb90SChristoph Hellwig 		da->da_dev, flag);
8663effdb90SChristoph Hellwig 	return count;
8673effdb90SChristoph Hellwig }
8683effdb90SChristoph Hellwig 
8693effdb90SChristoph Hellwig /*
8703effdb90SChristoph Hellwig  * Note, this can only be called on unexported SE Device Object.
8713effdb90SChristoph Hellwig  */
8722eafd729SChristoph Hellwig static ssize_t queue_depth_store(struct config_item *item,
8733effdb90SChristoph Hellwig 		const char *page, size_t count)
8743effdb90SChristoph Hellwig {
8752eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
8763effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
8773effdb90SChristoph Hellwig 	u32 val;
8783effdb90SChristoph Hellwig 	int ret;
8793effdb90SChristoph Hellwig 
8803effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
8813effdb90SChristoph Hellwig 	if (ret < 0)
8823effdb90SChristoph Hellwig 		return ret;
8833effdb90SChristoph Hellwig 
8843effdb90SChristoph Hellwig 	if (dev->export_count) {
8853effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TCQ while"
8863effdb90SChristoph Hellwig 			" export_count is %d\n",
8873effdb90SChristoph Hellwig 			dev, dev->export_count);
8883effdb90SChristoph Hellwig 		return -EINVAL;
8893effdb90SChristoph Hellwig 	}
8903effdb90SChristoph Hellwig 	if (!val) {
8913effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
8923effdb90SChristoph Hellwig 		return -EINVAL;
8933effdb90SChristoph Hellwig 	}
8943effdb90SChristoph Hellwig 
8953effdb90SChristoph Hellwig 	if (val > dev->dev_attrib.queue_depth) {
8963effdb90SChristoph Hellwig 		if (val > dev->dev_attrib.hw_queue_depth) {
8973effdb90SChristoph Hellwig 			pr_err("dev[%p]: Passed queue_depth:"
8983effdb90SChristoph Hellwig 				" %u exceeds TCM/SE_Device MAX"
8993effdb90SChristoph Hellwig 				" TCQ: %u\n", dev, val,
9003effdb90SChristoph Hellwig 				dev->dev_attrib.hw_queue_depth);
9013effdb90SChristoph Hellwig 			return -EINVAL;
9023effdb90SChristoph Hellwig 		}
9033effdb90SChristoph Hellwig 	}
9043effdb90SChristoph Hellwig 	da->queue_depth = dev->queue_depth = val;
9053effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
9063effdb90SChristoph Hellwig 	return count;
9073effdb90SChristoph Hellwig }
9083effdb90SChristoph Hellwig 
9092eafd729SChristoph Hellwig static ssize_t optimal_sectors_store(struct config_item *item,
9103effdb90SChristoph Hellwig 		const char *page, size_t count)
9113effdb90SChristoph Hellwig {
9122eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9133effdb90SChristoph Hellwig 	u32 val;
9143effdb90SChristoph Hellwig 	int ret;
9153effdb90SChristoph Hellwig 
9163effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
9173effdb90SChristoph Hellwig 	if (ret < 0)
9183effdb90SChristoph Hellwig 		return ret;
9193effdb90SChristoph Hellwig 
9203effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
9213effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
9223effdb90SChristoph Hellwig 			" optimal_sectors while export_count is %d\n",
9233effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
9243effdb90SChristoph Hellwig 		return -EINVAL;
9253effdb90SChristoph Hellwig 	}
9263effdb90SChristoph Hellwig 	if (val > da->hw_max_sectors) {
9273effdb90SChristoph Hellwig 		pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
9283effdb90SChristoph Hellwig 			" greater than hw_max_sectors: %u\n",
9293effdb90SChristoph Hellwig 			da->da_dev, val, da->hw_max_sectors);
9303effdb90SChristoph Hellwig 		return -EINVAL;
9313effdb90SChristoph Hellwig 	}
9323effdb90SChristoph Hellwig 
9333effdb90SChristoph Hellwig 	da->optimal_sectors = val;
9343effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
9353effdb90SChristoph Hellwig 			da->da_dev, val);
9363effdb90SChristoph Hellwig 	return count;
9373effdb90SChristoph Hellwig }
9383effdb90SChristoph Hellwig 
9392eafd729SChristoph Hellwig static ssize_t block_size_store(struct config_item *item,
9403effdb90SChristoph Hellwig 		const char *page, size_t count)
9413effdb90SChristoph Hellwig {
9422eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9433effdb90SChristoph Hellwig 	u32 val;
9443effdb90SChristoph Hellwig 	int ret;
9453effdb90SChristoph Hellwig 
9463effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
9473effdb90SChristoph Hellwig 	if (ret < 0)
9483effdb90SChristoph Hellwig 		return ret;
9493effdb90SChristoph Hellwig 
9503effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
9513effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device block_size"
9523effdb90SChristoph Hellwig 			" while export_count is %d\n",
9533effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
9543effdb90SChristoph Hellwig 		return -EINVAL;
9553effdb90SChristoph Hellwig 	}
9563effdb90SChristoph Hellwig 
9573effdb90SChristoph Hellwig 	if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
9583effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal value for block_device: %u"
9593effdb90SChristoph Hellwig 			" for SE device, must be 512, 1024, 2048 or 4096\n",
9603effdb90SChristoph Hellwig 			da->da_dev, val);
9613effdb90SChristoph Hellwig 		return -EINVAL;
9623effdb90SChristoph Hellwig 	}
9633effdb90SChristoph Hellwig 
9643effdb90SChristoph Hellwig 	da->block_size = val;
9653effdb90SChristoph Hellwig 	if (da->max_bytes_per_io)
9663effdb90SChristoph Hellwig 		da->hw_max_sectors = da->max_bytes_per_io / val;
9673effdb90SChristoph Hellwig 
9683effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device block_size changed to %u\n",
9693effdb90SChristoph Hellwig 			da->da_dev, val);
9703effdb90SChristoph Hellwig 	return count;
9713effdb90SChristoph Hellwig }
9725873c4d1SChristoph Hellwig 
9732eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_model_alias);
9742eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_dpo);
9752eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_write);
9762eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_read);
9772eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_write_cache);
9782eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
9792eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tas);
9802eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpu);
9812eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpws);
9822eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_caw);
9832eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_3pc);
9842eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_type);
9852eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_pi_prot_type);
9862eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_format);
9872eafd729SChristoph Hellwig CONFIGFS_ATTR(, enforce_pr_isids);
9882eafd729SChristoph Hellwig CONFIGFS_ATTR(, is_nonrot);
9892eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_rest_reord);
9902eafd729SChristoph Hellwig CONFIGFS_ATTR(, force_pr_aptpl);
9912eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_block_size);
9922eafd729SChristoph Hellwig CONFIGFS_ATTR(, block_size);
9932eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_max_sectors);
9942eafd729SChristoph Hellwig CONFIGFS_ATTR(, optimal_sectors);
9952eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_queue_depth);
9962eafd729SChristoph Hellwig CONFIGFS_ATTR(, queue_depth);
9972eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_lba_count);
9982eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_block_desc_count);
9992eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity);
10002eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity_alignment);
10012eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_write_same_len);
1002c66ac9dbSNicholas Bellinger 
10035873c4d1SChristoph Hellwig /*
10045873c4d1SChristoph Hellwig  * dev_attrib attributes for devices using the target core SBC/SPC
10055873c4d1SChristoph Hellwig  * interpreter.  Any backend using spc_parse_cdb should be using
10065873c4d1SChristoph Hellwig  * these.
10075873c4d1SChristoph Hellwig  */
10085873c4d1SChristoph Hellwig struct configfs_attribute *sbc_attrib_attrs[] = {
10092eafd729SChristoph Hellwig 	&attr_emulate_model_alias,
10102eafd729SChristoph Hellwig 	&attr_emulate_dpo,
10112eafd729SChristoph Hellwig 	&attr_emulate_fua_write,
10122eafd729SChristoph Hellwig 	&attr_emulate_fua_read,
10132eafd729SChristoph Hellwig 	&attr_emulate_write_cache,
10142eafd729SChristoph Hellwig 	&attr_emulate_ua_intlck_ctrl,
10152eafd729SChristoph Hellwig 	&attr_emulate_tas,
10162eafd729SChristoph Hellwig 	&attr_emulate_tpu,
10172eafd729SChristoph Hellwig 	&attr_emulate_tpws,
10182eafd729SChristoph Hellwig 	&attr_emulate_caw,
10192eafd729SChristoph Hellwig 	&attr_emulate_3pc,
10202eafd729SChristoph Hellwig 	&attr_pi_prot_type,
10212eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
10222eafd729SChristoph Hellwig 	&attr_pi_prot_format,
10232eafd729SChristoph Hellwig 	&attr_enforce_pr_isids,
10242eafd729SChristoph Hellwig 	&attr_is_nonrot,
10252eafd729SChristoph Hellwig 	&attr_emulate_rest_reord,
10262eafd729SChristoph Hellwig 	&attr_force_pr_aptpl,
10272eafd729SChristoph Hellwig 	&attr_hw_block_size,
10282eafd729SChristoph Hellwig 	&attr_block_size,
10292eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
10302eafd729SChristoph Hellwig 	&attr_optimal_sectors,
10312eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
10322eafd729SChristoph Hellwig 	&attr_queue_depth,
10332eafd729SChristoph Hellwig 	&attr_max_unmap_lba_count,
10342eafd729SChristoph Hellwig 	&attr_max_unmap_block_desc_count,
10352eafd729SChristoph Hellwig 	&attr_unmap_granularity,
10362eafd729SChristoph Hellwig 	&attr_unmap_granularity_alignment,
10372eafd729SChristoph Hellwig 	&attr_max_write_same_len,
10385873c4d1SChristoph Hellwig 	NULL,
10395873c4d1SChristoph Hellwig };
10405873c4d1SChristoph Hellwig EXPORT_SYMBOL(sbc_attrib_attrs);
10415873c4d1SChristoph Hellwig 
10425873c4d1SChristoph Hellwig /*
10435873c4d1SChristoph Hellwig  * Minimal dev_attrib attributes for devices passing through CDBs.
10445873c4d1SChristoph Hellwig  * In this case we only provide a few read-only attributes for
10455873c4d1SChristoph Hellwig  * backwards compatibility.
10465873c4d1SChristoph Hellwig  */
10475873c4d1SChristoph Hellwig struct configfs_attribute *passthrough_attrib_attrs[] = {
10482eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
10492eafd729SChristoph Hellwig 	&attr_hw_block_size,
10502eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
10512eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
10525873c4d1SChristoph Hellwig 	NULL,
10535873c4d1SChristoph Hellwig };
10545873c4d1SChristoph Hellwig EXPORT_SYMBOL(passthrough_attrib_attrs);
10555873c4d1SChristoph Hellwig 
10562eafd729SChristoph Hellwig TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
1057c66ac9dbSNicholas Bellinger 
1058f79a897eSNicholas Bellinger /* End functions for struct config_item_type tb_dev_attrib_cit */
1059c66ac9dbSNicholas Bellinger 
1060f8d389c6SNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_wwn_cit */
1061c66ac9dbSNicholas Bellinger 
10622eafd729SChristoph Hellwig static struct t10_wwn *to_t10_wwn(struct config_item *item)
10632eafd729SChristoph Hellwig {
10642eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
10652eafd729SChristoph Hellwig }
1066c66ac9dbSNicholas Bellinger 
1067c66ac9dbSNicholas Bellinger /*
1068c66ac9dbSNicholas Bellinger  * VPD page 0x80 Unit serial
1069c66ac9dbSNicholas Bellinger  */
10702eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1071c66ac9dbSNicholas Bellinger 		char *page)
1072c66ac9dbSNicholas Bellinger {
1073c66ac9dbSNicholas Bellinger 	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
10742eafd729SChristoph Hellwig 		&to_t10_wwn(item)->unit_serial[0]);
1075c66ac9dbSNicholas Bellinger }
1076c66ac9dbSNicholas Bellinger 
10772eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
10782eafd729SChristoph Hellwig 		const char *page, size_t count)
1079c66ac9dbSNicholas Bellinger {
10802eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
10810fd97ccfSChristoph Hellwig 	struct se_device *dev = t10_wwn->t10_dev;
1082c66ac9dbSNicholas Bellinger 	unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1083c66ac9dbSNicholas Bellinger 
1084c66ac9dbSNicholas Bellinger 	/*
1085c66ac9dbSNicholas Bellinger 	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1086c66ac9dbSNicholas Bellinger 	 * from the struct scsi_device level firmware, do not allow
1087c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial to be emulated.
1088c66ac9dbSNicholas Bellinger 	 *
1089c66ac9dbSNicholas Bellinger 	 * Note this struct scsi_device could also be emulating VPD
1090c66ac9dbSNicholas Bellinger 	 * information from its drivers/scsi LLD.  But for now we assume
1091c66ac9dbSNicholas Bellinger 	 * it is doing 'the right thing' wrt a world wide unique
1092c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial Number that OS dependent multipath can depend on.
1093c66ac9dbSNicholas Bellinger 	 */
10940fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
10956708bb27SAndy Grover 		pr_err("Underlying SCSI device firmware provided VPD"
1096c66ac9dbSNicholas Bellinger 			" Unit Serial, ignoring request\n");
1097c66ac9dbSNicholas Bellinger 		return -EOPNOTSUPP;
1098c66ac9dbSNicholas Bellinger 	}
1099c66ac9dbSNicholas Bellinger 
110060d645a4SDan Carpenter 	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
11016708bb27SAndy Grover 		pr_err("Emulated VPD Unit Serial exceeds"
1102c66ac9dbSNicholas Bellinger 		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1103c66ac9dbSNicholas Bellinger 		return -EOVERFLOW;
1104c66ac9dbSNicholas Bellinger 	}
1105c66ac9dbSNicholas Bellinger 	/*
1106c66ac9dbSNicholas Bellinger 	 * Check to see if any active $FABRIC_MOD exports exist.  If they
1107c66ac9dbSNicholas Bellinger 	 * do exist, fail here as changing this information on the fly
1108c66ac9dbSNicholas Bellinger 	 * (underneath the initiator side OS dependent multipath code)
1109c66ac9dbSNicholas Bellinger 	 * could cause negative effects.
1110c66ac9dbSNicholas Bellinger 	 */
11110fd97ccfSChristoph Hellwig 	if (dev->export_count) {
11126708bb27SAndy Grover 		pr_err("Unable to set VPD Unit Serial while"
1113c66ac9dbSNicholas Bellinger 			" active %d $FABRIC_MOD exports exist\n",
11140fd97ccfSChristoph Hellwig 			dev->export_count);
1115c66ac9dbSNicholas Bellinger 		return -EINVAL;
1116c66ac9dbSNicholas Bellinger 	}
11170fd97ccfSChristoph Hellwig 
1118c66ac9dbSNicholas Bellinger 	/*
1119c66ac9dbSNicholas Bellinger 	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1120c66ac9dbSNicholas Bellinger 	 *
1121c66ac9dbSNicholas Bellinger 	 * Also, strip any newline added from the userspace
1122c66ac9dbSNicholas Bellinger 	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1123c66ac9dbSNicholas Bellinger 	 */
1124c66ac9dbSNicholas Bellinger 	memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1125c66ac9dbSNicholas Bellinger 	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
11260fd97ccfSChristoph Hellwig 	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1127c66ac9dbSNicholas Bellinger 			"%s", strstrip(buf));
11280fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1129c66ac9dbSNicholas Bellinger 
11306708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
11310fd97ccfSChristoph Hellwig 			" %s\n", dev->t10_wwn.unit_serial);
1132c66ac9dbSNicholas Bellinger 
1133c66ac9dbSNicholas Bellinger 	return count;
1134c66ac9dbSNicholas Bellinger }
1135c66ac9dbSNicholas Bellinger 
1136c66ac9dbSNicholas Bellinger /*
1137c66ac9dbSNicholas Bellinger  * VPD page 0x83 Protocol Identifier
1138c66ac9dbSNicholas Bellinger  */
11392eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1140c66ac9dbSNicholas Bellinger 		char *page)
1141c66ac9dbSNicholas Bellinger {
11422eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1143c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;
1144c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];
1145c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1146c66ac9dbSNicholas Bellinger 
1147c66ac9dbSNicholas Bellinger 	memset(buf, 0, VPD_TMP_BUF_SIZE);
1148c66ac9dbSNicholas Bellinger 
1149c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);
1150c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
11516708bb27SAndy Grover 		if (!vpd->protocol_identifier_set)
1152c66ac9dbSNicholas Bellinger 			continue;
1153c66ac9dbSNicholas Bellinger 
1154c66ac9dbSNicholas Bellinger 		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1155c66ac9dbSNicholas Bellinger 
11566708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1157c66ac9dbSNicholas Bellinger 			break;
1158c66ac9dbSNicholas Bellinger 
1159c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1160c66ac9dbSNicholas Bellinger 	}
1161c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);
1162c66ac9dbSNicholas Bellinger 
1163c66ac9dbSNicholas Bellinger 	return len;
1164c66ac9dbSNicholas Bellinger }
1165c66ac9dbSNicholas Bellinger 
1166c66ac9dbSNicholas Bellinger /*
1167c66ac9dbSNicholas Bellinger  * Generic wrapper for dumping VPD identifiers by association.
1168c66ac9dbSNicholas Bellinger  */
1169c66ac9dbSNicholas Bellinger #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
11702eafd729SChristoph Hellwig static ssize_t target_wwn_##_name##_show(struct config_item *item,	\
1171c66ac9dbSNicholas Bellinger 		char *page)						\
1172c66ac9dbSNicholas Bellinger {									\
11732eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);			\
1174c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;						\
1175c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];				\
1176c66ac9dbSNicholas Bellinger 	ssize_t len = 0;						\
1177c66ac9dbSNicholas Bellinger 									\
1178c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);				\
1179c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
1180c66ac9dbSNicholas Bellinger 		if (vpd->association != _assoc)				\
1181c66ac9dbSNicholas Bellinger 			continue;					\
1182c66ac9dbSNicholas Bellinger 									\
1183c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1184c66ac9dbSNicholas Bellinger 		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
11856708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1186c66ac9dbSNicholas Bellinger 			break;						\
1187c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1188c66ac9dbSNicholas Bellinger 									\
1189c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1190c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
11916708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1192c66ac9dbSNicholas Bellinger 			break;						\
1193c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1194c66ac9dbSNicholas Bellinger 									\
1195c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1196c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
11976708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1198c66ac9dbSNicholas Bellinger 			break;						\
1199c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1200c66ac9dbSNicholas Bellinger 	}								\
1201c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);				\
1202c66ac9dbSNicholas Bellinger 									\
1203c66ac9dbSNicholas Bellinger 	return len;							\
1204c66ac9dbSNicholas Bellinger }
1205c66ac9dbSNicholas Bellinger 
12062eafd729SChristoph Hellwig /* VPD page 0x83 Association: Logical Unit */
1207c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
12082eafd729SChristoph Hellwig /* VPD page 0x83 Association: Target Port */
1209c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
12102eafd729SChristoph Hellwig /* VPD page 0x83 Association: SCSI Target Device */
1211c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1212c66ac9dbSNicholas Bellinger 
12132eafd729SChristoph Hellwig CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
12142eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
12152eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
12162eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
12172eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1218c66ac9dbSNicholas Bellinger 
1219c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
12202eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_unit_serial,
12212eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_protocol_identifier,
12222eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_logical_unit,
12232eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_target_port,
12242eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_scsi_target_device,
1225c66ac9dbSNicholas Bellinger 	NULL,
1226c66ac9dbSNicholas Bellinger };
1227c66ac9dbSNicholas Bellinger 
12282eafd729SChristoph Hellwig TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1229c66ac9dbSNicholas Bellinger 
1230f8d389c6SNicholas Bellinger /*  End functions for struct config_item_type tb_dev_wwn_cit */
1231c66ac9dbSNicholas Bellinger 
123291e2e39bSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_pr_cit */
1233c66ac9dbSNicholas Bellinger 
12342eafd729SChristoph Hellwig static struct se_device *pr_to_dev(struct config_item *item)
12352eafd729SChristoph Hellwig {
12362eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device,
12372eafd729SChristoph Hellwig 			dev_pr_group);
12382eafd729SChristoph Hellwig }
1239c66ac9dbSNicholas Bellinger 
1240d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1241d977f437SChristoph Hellwig 		char *page)
1242c66ac9dbSNicholas Bellinger {
1243c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1244c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1245c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1246c66ac9dbSNicholas Bellinger 
1247c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1248c66ac9dbSNicholas Bellinger 
1249c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1250d977f437SChristoph Hellwig 	if (!pr_reg)
1251d977f437SChristoph Hellwig 		return sprintf(page, "No SPC-3 Reservation holder\n");
1252d977f437SChristoph Hellwig 
1253c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1254d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1255c66ac9dbSNicholas Bellinger 
1256d977f437SChristoph Hellwig 	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
1257e3d6f909SAndy Grover 		se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1258d2843c17SAndy Grover 		se_nacl->initiatorname, i_buf);
1259c66ac9dbSNicholas Bellinger }
1260c66ac9dbSNicholas Bellinger 
1261d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1262d977f437SChristoph Hellwig 		char *page)
1263c66ac9dbSNicholas Bellinger {
1264c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1265d977f437SChristoph Hellwig 	ssize_t len;
1266c66ac9dbSNicholas Bellinger 
1267c66ac9dbSNicholas Bellinger 	se_nacl = dev->dev_reserved_node_acl;
1268d977f437SChristoph Hellwig 	if (se_nacl) {
1269d977f437SChristoph Hellwig 		len = sprintf(page,
1270d977f437SChristoph Hellwig 			      "SPC-2 Reservation: %s Initiator: %s\n",
1271e3d6f909SAndy Grover 			      se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1272c66ac9dbSNicholas Bellinger 			      se_nacl->initiatorname);
1273d977f437SChristoph Hellwig 	} else {
1274d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-2 Reservation holder\n");
1275d977f437SChristoph Hellwig 	}
1276d977f437SChristoph Hellwig 	return len;
1277c66ac9dbSNicholas Bellinger }
1278c66ac9dbSNicholas Bellinger 
12792eafd729SChristoph Hellwig static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1280c66ac9dbSNicholas Bellinger {
12812eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1282d977f437SChristoph Hellwig 	int ret;
1283c66ac9dbSNicholas Bellinger 
1284a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1285d977f437SChristoph Hellwig 		return sprintf(page, "Passthrough\n");
1286c66ac9dbSNicholas Bellinger 
1287d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
1288d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1289d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc2_res(dev, page);
1290d977f437SChristoph Hellwig 	else
1291d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc3_res(dev, page);
1292d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1293d977f437SChristoph Hellwig 	return ret;
1294c66ac9dbSNicholas Bellinger }
1295c66ac9dbSNicholas Bellinger 
12962eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
12972eafd729SChristoph Hellwig 		char *page)
1298c66ac9dbSNicholas Bellinger {
12992eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1300c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1301c66ac9dbSNicholas Bellinger 
1302c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1303d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder) {
1304c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1305d977f437SChristoph Hellwig 	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1306c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: All Target"
1307c66ac9dbSNicholas Bellinger 			" Ports registration\n");
1308d977f437SChristoph Hellwig 	} else {
1309c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: Single"
1310c66ac9dbSNicholas Bellinger 			" Target Port registration\n");
1311d977f437SChristoph Hellwig 	}
1312c66ac9dbSNicholas Bellinger 
1313d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1314c66ac9dbSNicholas Bellinger 	return len;
1315c66ac9dbSNicholas Bellinger }
1316c66ac9dbSNicholas Bellinger 
13172eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
13182eafd729SChristoph Hellwig 		char *page)
1319c66ac9dbSNicholas Bellinger {
13202eafd729SChristoph Hellwig 	return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1321c66ac9dbSNicholas Bellinger }
1322c66ac9dbSNicholas Bellinger 
1323c66ac9dbSNicholas Bellinger 
13242eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
13252eafd729SChristoph Hellwig 		char *page)
1326c66ac9dbSNicholas Bellinger {
13272eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1328c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1329c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
1330c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
13319ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1332c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1333c66ac9dbSNicholas Bellinger 
1334c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1335c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
13366708bb27SAndy Grover 	if (!pr_reg) {
1337c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1338d977f437SChristoph Hellwig 		goto out_unlock;
1339c66ac9dbSNicholas Bellinger 	}
1340d977f437SChristoph Hellwig 
1341c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1342c66ac9dbSNicholas Bellinger 	se_tpg = se_nacl->se_tpg;
1343e3d6f909SAndy Grover 	tfo = se_tpg->se_tpg_tfo;
1344c66ac9dbSNicholas Bellinger 
1345c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: %s"
1346c66ac9dbSNicholas Bellinger 		" Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1347c66ac9dbSNicholas Bellinger 		tfo->tpg_get_wwn(se_tpg));
1348c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
134935d1efe8SMasanari Iida 		" Identifier Tag: %hu %s Portal Group Tag: %hu"
1350f2d30680SHannes Reinecke 		" %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
1351c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
135279dc9c9eSNicholas Bellinger 		tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
1353c66ac9dbSNicholas Bellinger 
1354d977f437SChristoph Hellwig out_unlock:
1355d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1356c66ac9dbSNicholas Bellinger 	return len;
1357c66ac9dbSNicholas Bellinger }
1358c66ac9dbSNicholas Bellinger 
1359c66ac9dbSNicholas Bellinger 
13602eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
13612eafd729SChristoph Hellwig 		char *page)
1362c66ac9dbSNicholas Bellinger {
13632eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
13649ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1365c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1366c66ac9dbSNicholas Bellinger 	unsigned char buf[384];
1367c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1368c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1369d2843c17SAndy Grover 	int reg_count = 0;
1370c66ac9dbSNicholas Bellinger 
1371c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1372c66ac9dbSNicholas Bellinger 
13730fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
13740fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1375c66ac9dbSNicholas Bellinger 			pr_reg_list) {
1376c66ac9dbSNicholas Bellinger 
1377c66ac9dbSNicholas Bellinger 		memset(buf, 0, 384);
1378c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1379c66ac9dbSNicholas Bellinger 		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1380d2843c17SAndy Grover 		core_pr_dump_initiator_port(pr_reg, i_buf,
1381c66ac9dbSNicholas Bellinger 					PR_REG_ISID_ID_LEN);
1382c66ac9dbSNicholas Bellinger 		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1383c66ac9dbSNicholas Bellinger 			tfo->get_fabric_name(),
1384d2843c17SAndy Grover 			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1385c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_generation);
1386c66ac9dbSNicholas Bellinger 
13876708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1388c66ac9dbSNicholas Bellinger 			break;
1389c66ac9dbSNicholas Bellinger 
1390c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1391c66ac9dbSNicholas Bellinger 		reg_count++;
1392c66ac9dbSNicholas Bellinger 	}
13930fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
1394c66ac9dbSNicholas Bellinger 
13956708bb27SAndy Grover 	if (!reg_count)
1396c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "None\n");
1397c66ac9dbSNicholas Bellinger 
1398c66ac9dbSNicholas Bellinger 	return len;
1399c66ac9dbSNicholas Bellinger }
1400c66ac9dbSNicholas Bellinger 
14012eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1402c66ac9dbSNicholas Bellinger {
14032eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1404c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1405c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1406c66ac9dbSNicholas Bellinger 
1407c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1408c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1409d977f437SChristoph Hellwig 	if (pr_reg) {
1410c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1411c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1412d977f437SChristoph Hellwig 	} else {
1413d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1414d977f437SChristoph Hellwig 	}
1415c66ac9dbSNicholas Bellinger 
1416d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1417c66ac9dbSNicholas Bellinger 	return len;
1418c66ac9dbSNicholas Bellinger }
1419c66ac9dbSNicholas Bellinger 
14202eafd729SChristoph Hellwig static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
1421c66ac9dbSNicholas Bellinger {
14222eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
14232eafd729SChristoph Hellwig 
1424a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1425d977f437SChristoph Hellwig 		return sprintf(page, "SPC_PASSTHROUGH\n");
1426d977f437SChristoph Hellwig 	else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1427d977f437SChristoph Hellwig 		return sprintf(page, "SPC2_RESERVATIONS\n");
1428d977f437SChristoph Hellwig 	else
1429d977f437SChristoph Hellwig 		return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1430c66ac9dbSNicholas Bellinger }
1431c66ac9dbSNicholas Bellinger 
14322eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
14332eafd729SChristoph Hellwig 		char *page)
1434c66ac9dbSNicholas Bellinger {
14352eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
14362eafd729SChristoph Hellwig 
1437a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1438c66ac9dbSNicholas Bellinger 		return 0;
1439c66ac9dbSNicholas Bellinger 
1440c66ac9dbSNicholas Bellinger 	return sprintf(page, "APTPL Bit Status: %s\n",
14410fd97ccfSChristoph Hellwig 		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1442c66ac9dbSNicholas Bellinger }
1443c66ac9dbSNicholas Bellinger 
14442eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
14452eafd729SChristoph Hellwig 		char *page)
1446c66ac9dbSNicholas Bellinger {
14472eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
14482eafd729SChristoph Hellwig 
1449a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1450c66ac9dbSNicholas Bellinger 		return 0;
1451c66ac9dbSNicholas Bellinger 
1452c66ac9dbSNicholas Bellinger 	return sprintf(page, "Ready to process PR APTPL metadata..\n");
1453c66ac9dbSNicholas Bellinger }
1454c66ac9dbSNicholas Bellinger 
1455c66ac9dbSNicholas Bellinger enum {
1456c66ac9dbSNicholas Bellinger 	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1457c66ac9dbSNicholas Bellinger 	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1458c66ac9dbSNicholas Bellinger 	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1459c66ac9dbSNicholas Bellinger 	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1460c66ac9dbSNicholas Bellinger };
1461c66ac9dbSNicholas Bellinger 
1462c66ac9dbSNicholas Bellinger static match_table_t tokens = {
1463c66ac9dbSNicholas Bellinger 	{Opt_initiator_fabric, "initiator_fabric=%s"},
1464c66ac9dbSNicholas Bellinger 	{Opt_initiator_node, "initiator_node=%s"},
1465c66ac9dbSNicholas Bellinger 	{Opt_initiator_sid, "initiator_sid=%s"},
1466c66ac9dbSNicholas Bellinger 	{Opt_sa_res_key, "sa_res_key=%s"},
1467c66ac9dbSNicholas Bellinger 	{Opt_res_holder, "res_holder=%d"},
1468c66ac9dbSNicholas Bellinger 	{Opt_res_type, "res_type=%d"},
1469c66ac9dbSNicholas Bellinger 	{Opt_res_scope, "res_scope=%d"},
1470c66ac9dbSNicholas Bellinger 	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1471f2d30680SHannes Reinecke 	{Opt_mapped_lun, "mapped_lun=%lld"},
1472c66ac9dbSNicholas Bellinger 	{Opt_target_fabric, "target_fabric=%s"},
1473c66ac9dbSNicholas Bellinger 	{Opt_target_node, "target_node=%s"},
1474c66ac9dbSNicholas Bellinger 	{Opt_tpgt, "tpgt=%d"},
1475c66ac9dbSNicholas Bellinger 	{Opt_port_rtpi, "port_rtpi=%d"},
1476f2d30680SHannes Reinecke 	{Opt_target_lun, "target_lun=%lld"},
1477c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
1478c66ac9dbSNicholas Bellinger };
1479c66ac9dbSNicholas Bellinger 
14802eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
14812eafd729SChristoph Hellwig 		const char *page, size_t count)
1482c66ac9dbSNicholas Bellinger {
14832eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
14846d180253SJesper Juhl 	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
14856d180253SJesper Juhl 	unsigned char *t_fabric = NULL, *t_port = NULL;
14868d213559SJoern Engel 	char *orig, *ptr, *opts;
1487c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
1488c66ac9dbSNicholas Bellinger 	unsigned long long tmp_ll;
1489c66ac9dbSNicholas Bellinger 	u64 sa_res_key = 0;
1490f2d30680SHannes Reinecke 	u64 mapped_lun = 0, target_lun = 0;
1491c66ac9dbSNicholas Bellinger 	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
149245fb94c2SBart Van Assche 	u16 tpgt = 0;
149345fb94c2SBart Van Assche 	u8 type = 0;
1494c66ac9dbSNicholas Bellinger 
1495a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
14969105bfc0SAndy Grover 		return count;
1497d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
14989105bfc0SAndy Grover 		return count;
1499c66ac9dbSNicholas Bellinger 
15000fd97ccfSChristoph Hellwig 	if (dev->export_count) {
15016708bb27SAndy Grover 		pr_debug("Unable to process APTPL metadata while"
1502c66ac9dbSNicholas Bellinger 			" active fabric exports exist\n");
1503c66ac9dbSNicholas Bellinger 		return -EINVAL;
1504c66ac9dbSNicholas Bellinger 	}
1505c66ac9dbSNicholas Bellinger 
1506c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
1507c66ac9dbSNicholas Bellinger 	if (!opts)
1508c66ac9dbSNicholas Bellinger 		return -ENOMEM;
1509c66ac9dbSNicholas Bellinger 
1510c66ac9dbSNicholas Bellinger 	orig = opts;
151190c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
1512c66ac9dbSNicholas Bellinger 		if (!*ptr)
1513c66ac9dbSNicholas Bellinger 			continue;
1514c66ac9dbSNicholas Bellinger 
1515c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
1516c66ac9dbSNicholas Bellinger 		switch (token) {
1517c66ac9dbSNicholas Bellinger 		case Opt_initiator_fabric:
15188d213559SJoern Engel 			i_fabric = match_strdup(args);
15196d180253SJesper Juhl 			if (!i_fabric) {
15206d180253SJesper Juhl 				ret = -ENOMEM;
15216d180253SJesper Juhl 				goto out;
15226d180253SJesper Juhl 			}
1523c66ac9dbSNicholas Bellinger 			break;
1524c66ac9dbSNicholas Bellinger 		case Opt_initiator_node:
15258d213559SJoern Engel 			i_port = match_strdup(args);
15266d180253SJesper Juhl 			if (!i_port) {
15276d180253SJesper Juhl 				ret = -ENOMEM;
15286d180253SJesper Juhl 				goto out;
15296d180253SJesper Juhl 			}
153060d645a4SDan Carpenter 			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
15316708bb27SAndy Grover 				pr_err("APTPL metadata initiator_node="
1532c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1533c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_IPORT_LEN);
1534c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1535c66ac9dbSNicholas Bellinger 				break;
1536c66ac9dbSNicholas Bellinger 			}
1537c66ac9dbSNicholas Bellinger 			break;
1538c66ac9dbSNicholas Bellinger 		case Opt_initiator_sid:
15398d213559SJoern Engel 			isid = match_strdup(args);
15406d180253SJesper Juhl 			if (!isid) {
15416d180253SJesper Juhl 				ret = -ENOMEM;
15426d180253SJesper Juhl 				goto out;
15436d180253SJesper Juhl 			}
154460d645a4SDan Carpenter 			if (strlen(isid) >= PR_REG_ISID_LEN) {
15456708bb27SAndy Grover 				pr_err("APTPL metadata initiator_isid"
1546c66ac9dbSNicholas Bellinger 					"= exceeds PR_REG_ISID_LEN: %d\n",
1547c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
1548c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1549c66ac9dbSNicholas Bellinger 				break;
1550c66ac9dbSNicholas Bellinger 			}
1551c66ac9dbSNicholas Bellinger 			break;
1552c66ac9dbSNicholas Bellinger 		case Opt_sa_res_key:
15538d213559SJoern Engel 			ret = kstrtoull(args->from, 0, &tmp_ll);
1554c66ac9dbSNicholas Bellinger 			if (ret < 0) {
15558d213559SJoern Engel 				pr_err("kstrtoull() failed for sa_res_key=\n");
1556c66ac9dbSNicholas Bellinger 				goto out;
1557c66ac9dbSNicholas Bellinger 			}
1558c66ac9dbSNicholas Bellinger 			sa_res_key = (u64)tmp_ll;
1559c66ac9dbSNicholas Bellinger 			break;
1560c66ac9dbSNicholas Bellinger 		/*
1561c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Reservation
1562c66ac9dbSNicholas Bellinger 		 */
1563c66ac9dbSNicholas Bellinger 		case Opt_res_holder:
1564c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1565c2091026SDavid Disseldorp 			if (ret)
1566c2091026SDavid Disseldorp 				goto out;
1567c66ac9dbSNicholas Bellinger 			res_holder = arg;
1568c66ac9dbSNicholas Bellinger 			break;
1569c66ac9dbSNicholas Bellinger 		case Opt_res_type:
1570c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1571c2091026SDavid Disseldorp 			if (ret)
1572c2091026SDavid Disseldorp 				goto out;
1573c66ac9dbSNicholas Bellinger 			type = (u8)arg;
1574c66ac9dbSNicholas Bellinger 			break;
1575c66ac9dbSNicholas Bellinger 		case Opt_res_scope:
1576c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1577c2091026SDavid Disseldorp 			if (ret)
1578c2091026SDavid Disseldorp 				goto out;
1579c66ac9dbSNicholas Bellinger 			break;
1580c66ac9dbSNicholas Bellinger 		case Opt_res_all_tg_pt:
1581c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1582c2091026SDavid Disseldorp 			if (ret)
1583c2091026SDavid Disseldorp 				goto out;
1584c66ac9dbSNicholas Bellinger 			all_tg_pt = (int)arg;
1585c66ac9dbSNicholas Bellinger 			break;
1586c66ac9dbSNicholas Bellinger 		case Opt_mapped_lun:
1587c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1588c2091026SDavid Disseldorp 			if (ret)
1589c2091026SDavid Disseldorp 				goto out;
1590f2d30680SHannes Reinecke 			mapped_lun = (u64)arg;
1591c66ac9dbSNicholas Bellinger 			break;
1592c66ac9dbSNicholas Bellinger 		/*
1593c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Target Port
1594c66ac9dbSNicholas Bellinger 		 */
1595c66ac9dbSNicholas Bellinger 		case Opt_target_fabric:
15968d213559SJoern Engel 			t_fabric = match_strdup(args);
15976d180253SJesper Juhl 			if (!t_fabric) {
15986d180253SJesper Juhl 				ret = -ENOMEM;
15996d180253SJesper Juhl 				goto out;
16006d180253SJesper Juhl 			}
1601c66ac9dbSNicholas Bellinger 			break;
1602c66ac9dbSNicholas Bellinger 		case Opt_target_node:
16038d213559SJoern Engel 			t_port = match_strdup(args);
16046d180253SJesper Juhl 			if (!t_port) {
16056d180253SJesper Juhl 				ret = -ENOMEM;
16066d180253SJesper Juhl 				goto out;
16076d180253SJesper Juhl 			}
160860d645a4SDan Carpenter 			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
16096708bb27SAndy Grover 				pr_err("APTPL metadata target_node="
1610c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1611c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_TPORT_LEN);
1612c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1613c66ac9dbSNicholas Bellinger 				break;
1614c66ac9dbSNicholas Bellinger 			}
1615c66ac9dbSNicholas Bellinger 			break;
1616c66ac9dbSNicholas Bellinger 		case Opt_tpgt:
1617c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1618c2091026SDavid Disseldorp 			if (ret)
1619c2091026SDavid Disseldorp 				goto out;
1620c66ac9dbSNicholas Bellinger 			tpgt = (u16)arg;
1621c66ac9dbSNicholas Bellinger 			break;
1622c66ac9dbSNicholas Bellinger 		case Opt_port_rtpi:
1623c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1624c2091026SDavid Disseldorp 			if (ret)
1625c2091026SDavid Disseldorp 				goto out;
1626c66ac9dbSNicholas Bellinger 			break;
1627c66ac9dbSNicholas Bellinger 		case Opt_target_lun:
1628c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1629c2091026SDavid Disseldorp 			if (ret)
1630c2091026SDavid Disseldorp 				goto out;
1631f2d30680SHannes Reinecke 			target_lun = (u64)arg;
1632c66ac9dbSNicholas Bellinger 			break;
1633c66ac9dbSNicholas Bellinger 		default:
1634c66ac9dbSNicholas Bellinger 			break;
1635c66ac9dbSNicholas Bellinger 		}
1636c66ac9dbSNicholas Bellinger 	}
1637c66ac9dbSNicholas Bellinger 
16386708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
16396708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
1640c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1641c66ac9dbSNicholas Bellinger 		goto out;
1642c66ac9dbSNicholas Bellinger 	}
1643c66ac9dbSNicholas Bellinger 
1644c66ac9dbSNicholas Bellinger 	if (res_holder && !(type)) {
16456708bb27SAndy Grover 		pr_err("Illegal PR type: 0x%02x for reservation"
1646c66ac9dbSNicholas Bellinger 				" holder\n", type);
1647c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1648c66ac9dbSNicholas Bellinger 		goto out;
1649c66ac9dbSNicholas Bellinger 	}
1650c66ac9dbSNicholas Bellinger 
16510fd97ccfSChristoph Hellwig 	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1652c66ac9dbSNicholas Bellinger 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1653c66ac9dbSNicholas Bellinger 			res_holder, all_tg_pt, type);
1654c66ac9dbSNicholas Bellinger out:
16556d180253SJesper Juhl 	kfree(i_fabric);
16566d180253SJesper Juhl 	kfree(i_port);
16576d180253SJesper Juhl 	kfree(isid);
16586d180253SJesper Juhl 	kfree(t_fabric);
16596d180253SJesper Juhl 	kfree(t_port);
1660c66ac9dbSNicholas Bellinger 	kfree(orig);
1661c66ac9dbSNicholas Bellinger 	return (ret == 0) ? count : ret;
1662c66ac9dbSNicholas Bellinger }
1663c66ac9dbSNicholas Bellinger 
1664c66ac9dbSNicholas Bellinger 
16652eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_holder);
16662eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
16672eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
16682eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
16692eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
16702eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
16712eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_type);
16722eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
16732eafd729SChristoph Hellwig CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
1674c66ac9dbSNicholas Bellinger 
1675c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_pr_attrs[] = {
16762eafd729SChristoph Hellwig 	&target_pr_attr_res_holder,
16772eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_all_tgt_pts,
16782eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_generation,
16792eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_holder_tg_port,
16802eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_registered_i_pts,
16812eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_type,
16822eafd729SChristoph Hellwig 	&target_pr_attr_res_type,
16832eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_active,
16842eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_metadata,
1685c66ac9dbSNicholas Bellinger 	NULL,
1686c66ac9dbSNicholas Bellinger };
1687c66ac9dbSNicholas Bellinger 
16882eafd729SChristoph Hellwig TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
1689c66ac9dbSNicholas Bellinger 
169091e2e39bSNicholas Bellinger /*  End functions for struct config_item_type tb_dev_pr_cit */
1691c66ac9dbSNicholas Bellinger 
169273112edcSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_cit */
1693c66ac9dbSNicholas Bellinger 
16942eafd729SChristoph Hellwig static inline struct se_device *to_device(struct config_item *item)
1695c66ac9dbSNicholas Bellinger {
16962eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device, dev_group);
16972eafd729SChristoph Hellwig }
16982eafd729SChristoph Hellwig 
16992eafd729SChristoph Hellwig static ssize_t target_dev_info_show(struct config_item *item, char *page)
17002eafd729SChristoph Hellwig {
17012eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1702c66ac9dbSNicholas Bellinger 	int bl = 0;
1703c66ac9dbSNicholas Bellinger 	ssize_t read_bytes = 0;
1704c66ac9dbSNicholas Bellinger 
17050fd97ccfSChristoph Hellwig 	transport_dump_dev_state(dev, page, &bl);
1706c66ac9dbSNicholas Bellinger 	read_bytes += bl;
17070a06d430SChristoph Hellwig 	read_bytes += dev->transport->show_configfs_dev_params(dev,
17080a06d430SChristoph Hellwig 			page+read_bytes);
1709c66ac9dbSNicholas Bellinger 	return read_bytes;
1710c66ac9dbSNicholas Bellinger }
1711c66ac9dbSNicholas Bellinger 
17122eafd729SChristoph Hellwig static ssize_t target_dev_control_store(struct config_item *item,
17132eafd729SChristoph Hellwig 		const char *page, size_t count)
1714c66ac9dbSNicholas Bellinger {
17152eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1716c66ac9dbSNicholas Bellinger 
17170a06d430SChristoph Hellwig 	return dev->transport->set_configfs_dev_params(dev, page, count);
1718c66ac9dbSNicholas Bellinger }
1719c66ac9dbSNicholas Bellinger 
17202eafd729SChristoph Hellwig static ssize_t target_dev_alias_show(struct config_item *item, char *page)
1721c66ac9dbSNicholas Bellinger {
17222eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1723c66ac9dbSNicholas Bellinger 
17240fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_ALIAS))
1725c66ac9dbSNicholas Bellinger 		return 0;
1726c66ac9dbSNicholas Bellinger 
17270fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1728c66ac9dbSNicholas Bellinger }
1729c66ac9dbSNicholas Bellinger 
17302eafd729SChristoph Hellwig static ssize_t target_dev_alias_store(struct config_item *item,
17312eafd729SChristoph Hellwig 		const char *page, size_t count)
1732c66ac9dbSNicholas Bellinger {
17332eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
17340fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1735c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1736c66ac9dbSNicholas Bellinger 
1737c66ac9dbSNicholas Bellinger 	if (count > (SE_DEV_ALIAS_LEN-1)) {
17386708bb27SAndy Grover 		pr_err("alias count: %d exceeds"
1739c66ac9dbSNicholas Bellinger 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1740c66ac9dbSNicholas Bellinger 			SE_DEV_ALIAS_LEN-1);
1741c66ac9dbSNicholas Bellinger 		return -EINVAL;
1742c66ac9dbSNicholas Bellinger 	}
1743c66ac9dbSNicholas Bellinger 
17440fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
17453011684cSDan Carpenter 	if (!read_bytes)
17463011684cSDan Carpenter 		return -EINVAL;
17470fd97ccfSChristoph Hellwig 	if (dev->dev_alias[read_bytes - 1] == '\n')
17480fd97ccfSChristoph Hellwig 		dev->dev_alias[read_bytes - 1] = '\0';
17490877eafdSSebastian Andrzej Siewior 
17500fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_ALIAS;
17513011684cSDan Carpenter 
17526708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1753c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
17540fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
17550fd97ccfSChristoph Hellwig 		dev->dev_alias);
1756c66ac9dbSNicholas Bellinger 
1757c66ac9dbSNicholas Bellinger 	return read_bytes;
1758c66ac9dbSNicholas Bellinger }
1759c66ac9dbSNicholas Bellinger 
17602eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
1761c66ac9dbSNicholas Bellinger {
17622eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1763c66ac9dbSNicholas Bellinger 
17640fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1765c66ac9dbSNicholas Bellinger 		return 0;
1766c66ac9dbSNicholas Bellinger 
17670fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1768c66ac9dbSNicholas Bellinger }
1769c66ac9dbSNicholas Bellinger 
17702eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_store(struct config_item *item,
17712eafd729SChristoph Hellwig 		const char *page, size_t count)
1772c66ac9dbSNicholas Bellinger {
17732eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
17740fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1775c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1776c66ac9dbSNicholas Bellinger 
1777c66ac9dbSNicholas Bellinger 	if (count > (SE_UDEV_PATH_LEN-1)) {
17786708bb27SAndy Grover 		pr_err("udev_path count: %d exceeds"
1779c66ac9dbSNicholas Bellinger 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1780c66ac9dbSNicholas Bellinger 			SE_UDEV_PATH_LEN-1);
1781c66ac9dbSNicholas Bellinger 		return -EINVAL;
1782c66ac9dbSNicholas Bellinger 	}
1783c66ac9dbSNicholas Bellinger 
17840fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1785c66ac9dbSNicholas Bellinger 			"%s", page);
17863011684cSDan Carpenter 	if (!read_bytes)
17873011684cSDan Carpenter 		return -EINVAL;
17880fd97ccfSChristoph Hellwig 	if (dev->udev_path[read_bytes - 1] == '\n')
17890fd97ccfSChristoph Hellwig 		dev->udev_path[read_bytes - 1] = '\0';
17900877eafdSSebastian Andrzej Siewior 
17910fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_UDEV_PATH;
17923011684cSDan Carpenter 
17936708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1794c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
17950fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
17960fd97ccfSChristoph Hellwig 		dev->udev_path);
1797c66ac9dbSNicholas Bellinger 
1798c66ac9dbSNicholas Bellinger 	return read_bytes;
1799c66ac9dbSNicholas Bellinger }
1800c66ac9dbSNicholas Bellinger 
18012eafd729SChristoph Hellwig static ssize_t target_dev_enable_show(struct config_item *item, char *page)
180264146db7SAndy Grover {
18032eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
180464146db7SAndy Grover 
180564146db7SAndy Grover 	return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
180664146db7SAndy Grover }
180764146db7SAndy Grover 
18082eafd729SChristoph Hellwig static ssize_t target_dev_enable_store(struct config_item *item,
18092eafd729SChristoph Hellwig 		const char *page, size_t count)
1810c66ac9dbSNicholas Bellinger {
18112eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1812c66ac9dbSNicholas Bellinger 	char *ptr;
18130fd97ccfSChristoph Hellwig 	int ret;
1814c66ac9dbSNicholas Bellinger 
1815c66ac9dbSNicholas Bellinger 	ptr = strstr(page, "1");
18166708bb27SAndy Grover 	if (!ptr) {
18176708bb27SAndy Grover 		pr_err("For dev_enable ops, only valid value"
1818c66ac9dbSNicholas Bellinger 				" is \"1\"\n");
1819c66ac9dbSNicholas Bellinger 		return -EINVAL;
1820c66ac9dbSNicholas Bellinger 	}
1821c66ac9dbSNicholas Bellinger 
18220fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
18230fd97ccfSChristoph Hellwig 	if (ret)
18240fd97ccfSChristoph Hellwig 		return ret;
1825c66ac9dbSNicholas Bellinger 	return count;
1826c66ac9dbSNicholas Bellinger }
1827c66ac9dbSNicholas Bellinger 
18282eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
1829c66ac9dbSNicholas Bellinger {
18302eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1831c66ac9dbSNicholas Bellinger 	struct config_item *lu_ci;
1832c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
1833c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1834c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1835c66ac9dbSNicholas Bellinger 
1836c66ac9dbSNicholas Bellinger 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1837c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
1838c87fbd56SChristoph Hellwig 		return 0;
1839c66ac9dbSNicholas Bellinger 
1840c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1841c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
18426708bb27SAndy Grover 	if (lu_gp) {
1843c66ac9dbSNicholas Bellinger 		lu_ci = &lu_gp->lu_gp_group.cg_item;
1844c66ac9dbSNicholas Bellinger 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1845c66ac9dbSNicholas Bellinger 			config_item_name(lu_ci), lu_gp->lu_gp_id);
1846c66ac9dbSNicholas Bellinger 	}
1847c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1848c66ac9dbSNicholas Bellinger 
1849c66ac9dbSNicholas Bellinger 	return len;
1850c66ac9dbSNicholas Bellinger }
1851c66ac9dbSNicholas Bellinger 
18522eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
18532eafd729SChristoph Hellwig 		const char *page, size_t count)
1854c66ac9dbSNicholas Bellinger {
18552eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
18560fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1857c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1858c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1859c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
1860c66ac9dbSNicholas Bellinger 	int move = 0;
1861c66ac9dbSNicholas Bellinger 
1862c87fbd56SChristoph Hellwig 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1863c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
18649105bfc0SAndy Grover 		return count;
1865c87fbd56SChristoph Hellwig 
1866c66ac9dbSNicholas Bellinger 	if (count > LU_GROUP_NAME_BUF) {
18676708bb27SAndy Grover 		pr_err("ALUA LU Group Alias too large!\n");
1868c66ac9dbSNicholas Bellinger 		return -EINVAL;
1869c66ac9dbSNicholas Bellinger 	}
1870c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
1871c66ac9dbSNicholas Bellinger 	memcpy(buf, page, count);
1872c66ac9dbSNicholas Bellinger 	/*
1873c66ac9dbSNicholas Bellinger 	 * Any ALUA logical unit alias besides "NULL" means we will be
1874c66ac9dbSNicholas Bellinger 	 * making a new group association.
1875c66ac9dbSNicholas Bellinger 	 */
1876c66ac9dbSNicholas Bellinger 	if (strcmp(strstrip(buf), "NULL")) {
1877c66ac9dbSNicholas Bellinger 		/*
1878c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name() will increment reference to
1879c66ac9dbSNicholas Bellinger 		 * struct t10_alua_lu_gp.  This reference is released with
1880c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name below().
1881c66ac9dbSNicholas Bellinger 		 */
1882c66ac9dbSNicholas Bellinger 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
18836708bb27SAndy Grover 		if (!lu_gp_new)
1884c66ac9dbSNicholas Bellinger 			return -ENODEV;
1885c66ac9dbSNicholas Bellinger 	}
1886c66ac9dbSNicholas Bellinger 
1887c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1888c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
18896708bb27SAndy Grover 	if (lu_gp) {
1890c66ac9dbSNicholas Bellinger 		/*
1891c66ac9dbSNicholas Bellinger 		 * Clearing an existing lu_gp association, and replacing
1892c66ac9dbSNicholas Bellinger 		 * with NULL
1893c66ac9dbSNicholas Bellinger 		 */
18946708bb27SAndy Grover 		if (!lu_gp_new) {
18956708bb27SAndy Grover 			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1896c66ac9dbSNicholas Bellinger 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1897c66ac9dbSNicholas Bellinger 				" %hu\n",
1898c66ac9dbSNicholas Bellinger 				config_item_name(&hba->hba_group.cg_item),
18990fd97ccfSChristoph Hellwig 				config_item_name(&dev->dev_group.cg_item),
1900c66ac9dbSNicholas Bellinger 				config_item_name(&lu_gp->lu_gp_group.cg_item),
1901c66ac9dbSNicholas Bellinger 				lu_gp->lu_gp_id);
1902c66ac9dbSNicholas Bellinger 
1903c66ac9dbSNicholas Bellinger 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1904c66ac9dbSNicholas Bellinger 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1905c66ac9dbSNicholas Bellinger 
1906c66ac9dbSNicholas Bellinger 			return count;
1907c66ac9dbSNicholas Bellinger 		}
1908c66ac9dbSNicholas Bellinger 		/*
1909c66ac9dbSNicholas Bellinger 		 * Removing existing association of lu_gp_mem with lu_gp
1910c66ac9dbSNicholas Bellinger 		 */
1911c66ac9dbSNicholas Bellinger 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1912c66ac9dbSNicholas Bellinger 		move = 1;
1913c66ac9dbSNicholas Bellinger 	}
1914c66ac9dbSNicholas Bellinger 	/*
1915c66ac9dbSNicholas Bellinger 	 * Associate lu_gp_mem with lu_gp_new.
1916c66ac9dbSNicholas Bellinger 	 */
1917c66ac9dbSNicholas Bellinger 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1918c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1919c66ac9dbSNicholas Bellinger 
19206708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1921c66ac9dbSNicholas Bellinger 		" core/alua/lu_gps/%s, ID: %hu\n",
1922c66ac9dbSNicholas Bellinger 		(move) ? "Moving" : "Adding",
1923c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
19240fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
1925c66ac9dbSNicholas Bellinger 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1926c66ac9dbSNicholas Bellinger 		lu_gp_new->lu_gp_id);
1927c66ac9dbSNicholas Bellinger 
1928c66ac9dbSNicholas Bellinger 	core_alua_put_lu_gp_from_name(lu_gp_new);
1929c66ac9dbSNicholas Bellinger 	return count;
1930c66ac9dbSNicholas Bellinger }
1931c66ac9dbSNicholas Bellinger 
19322eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
1933229d4f11SHannes Reinecke {
19342eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1935229d4f11SHannes Reinecke 	struct t10_alua_lba_map *map;
1936229d4f11SHannes Reinecke 	struct t10_alua_lba_map_member *mem;
1937229d4f11SHannes Reinecke 	char *b = page;
1938229d4f11SHannes Reinecke 	int bl = 0;
1939229d4f11SHannes Reinecke 	char state;
1940229d4f11SHannes Reinecke 
1941229d4f11SHannes Reinecke 	spin_lock(&dev->t10_alua.lba_map_lock);
1942229d4f11SHannes Reinecke 	if (!list_empty(&dev->t10_alua.lba_map_list))
1943229d4f11SHannes Reinecke 	    bl += sprintf(b + bl, "%u %u\n",
1944229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_size,
1945229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_multiplier);
1946229d4f11SHannes Reinecke 	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1947229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "%llu %llu",
1948229d4f11SHannes Reinecke 			      map->lba_map_first_lba, map->lba_map_last_lba);
1949229d4f11SHannes Reinecke 		list_for_each_entry(mem, &map->lba_map_mem_list,
1950229d4f11SHannes Reinecke 				    lba_map_mem_list) {
1951229d4f11SHannes Reinecke 			switch (mem->lba_map_mem_alua_state) {
1952229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1953229d4f11SHannes Reinecke 				state = 'O';
1954229d4f11SHannes Reinecke 				break;
1955229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1956229d4f11SHannes Reinecke 				state = 'A';
1957229d4f11SHannes Reinecke 				break;
1958229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_STANDBY:
1959229d4f11SHannes Reinecke 				state = 'S';
1960229d4f11SHannes Reinecke 				break;
1961229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_UNAVAILABLE:
1962229d4f11SHannes Reinecke 				state = 'U';
1963229d4f11SHannes Reinecke 				break;
1964229d4f11SHannes Reinecke 			default:
1965229d4f11SHannes Reinecke 				state = '.';
1966229d4f11SHannes Reinecke 				break;
1967229d4f11SHannes Reinecke 			}
1968229d4f11SHannes Reinecke 			bl += sprintf(b + bl, " %d:%c",
1969229d4f11SHannes Reinecke 				      mem->lba_map_mem_alua_pg_id, state);
1970229d4f11SHannes Reinecke 		}
1971229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "\n");
1972229d4f11SHannes Reinecke 	}
1973229d4f11SHannes Reinecke 	spin_unlock(&dev->t10_alua.lba_map_lock);
1974229d4f11SHannes Reinecke 	return bl;
1975229d4f11SHannes Reinecke }
1976229d4f11SHannes Reinecke 
19772eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_store(struct config_item *item,
19782eafd729SChristoph Hellwig 		const char *page, size_t count)
1979229d4f11SHannes Reinecke {
19802eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1981229d4f11SHannes Reinecke 	struct t10_alua_lba_map *lba_map = NULL;
1982229d4f11SHannes Reinecke 	struct list_head lba_list;
1983229d4f11SHannes Reinecke 	char *map_entries, *ptr;
1984229d4f11SHannes Reinecke 	char state;
1985229d4f11SHannes Reinecke 	int pg_num = -1, pg;
1986229d4f11SHannes Reinecke 	int ret = 0, num = 0, pg_id, alua_state;
1987229d4f11SHannes Reinecke 	unsigned long start_lba = -1, end_lba = -1;
1988229d4f11SHannes Reinecke 	unsigned long segment_size = -1, segment_mult = -1;
1989229d4f11SHannes Reinecke 
1990229d4f11SHannes Reinecke 	map_entries = kstrdup(page, GFP_KERNEL);
1991229d4f11SHannes Reinecke 	if (!map_entries)
1992229d4f11SHannes Reinecke 		return -ENOMEM;
1993229d4f11SHannes Reinecke 
1994229d4f11SHannes Reinecke 	INIT_LIST_HEAD(&lba_list);
1995229d4f11SHannes Reinecke 	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1996229d4f11SHannes Reinecke 		if (!*ptr)
1997229d4f11SHannes Reinecke 			continue;
1998229d4f11SHannes Reinecke 
1999229d4f11SHannes Reinecke 		if (num == 0) {
2000229d4f11SHannes Reinecke 			if (sscanf(ptr, "%lu %lu\n",
2001229d4f11SHannes Reinecke 				   &segment_size, &segment_mult) != 2) {
2002229d4f11SHannes Reinecke 				pr_err("Invalid line %d\n", num);
2003229d4f11SHannes Reinecke 				ret = -EINVAL;
2004229d4f11SHannes Reinecke 				break;
2005229d4f11SHannes Reinecke 			}
2006229d4f11SHannes Reinecke 			num++;
2007229d4f11SHannes Reinecke 			continue;
2008229d4f11SHannes Reinecke 		}
2009229d4f11SHannes Reinecke 		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2010229d4f11SHannes Reinecke 			pr_err("Invalid line %d\n", num);
2011229d4f11SHannes Reinecke 			ret = -EINVAL;
2012229d4f11SHannes Reinecke 			break;
2013229d4f11SHannes Reinecke 		}
2014229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2015229d4f11SHannes Reinecke 		if (!ptr) {
2016229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing end lba\n", num);
2017229d4f11SHannes Reinecke 			ret = -EINVAL;
2018229d4f11SHannes Reinecke 			break;
2019229d4f11SHannes Reinecke 		}
2020229d4f11SHannes Reinecke 		ptr++;
2021229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2022229d4f11SHannes Reinecke 		if (!ptr) {
2023229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing state definitions\n",
2024229d4f11SHannes Reinecke 			       num);
2025229d4f11SHannes Reinecke 			ret = -EINVAL;
2026229d4f11SHannes Reinecke 			break;
2027229d4f11SHannes Reinecke 		}
2028229d4f11SHannes Reinecke 		ptr++;
2029229d4f11SHannes Reinecke 		lba_map = core_alua_allocate_lba_map(&lba_list,
2030229d4f11SHannes Reinecke 						     start_lba, end_lba);
2031229d4f11SHannes Reinecke 		if (IS_ERR(lba_map)) {
2032229d4f11SHannes Reinecke 			ret = PTR_ERR(lba_map);
2033229d4f11SHannes Reinecke 			break;
2034229d4f11SHannes Reinecke 		}
2035229d4f11SHannes Reinecke 		pg = 0;
2036229d4f11SHannes Reinecke 		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2037229d4f11SHannes Reinecke 			switch (state) {
2038229d4f11SHannes Reinecke 			case 'O':
2039229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2040229d4f11SHannes Reinecke 				break;
2041229d4f11SHannes Reinecke 			case 'A':
2042229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2043229d4f11SHannes Reinecke 				break;
2044229d4f11SHannes Reinecke 			case 'S':
2045229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_STANDBY;
2046229d4f11SHannes Reinecke 				break;
2047229d4f11SHannes Reinecke 			case 'U':
2048229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2049229d4f11SHannes Reinecke 				break;
2050229d4f11SHannes Reinecke 			default:
2051229d4f11SHannes Reinecke 				pr_err("Invalid ALUA state '%c'\n", state);
2052229d4f11SHannes Reinecke 				ret = -EINVAL;
2053229d4f11SHannes Reinecke 				goto out;
2054229d4f11SHannes Reinecke 			}
2055229d4f11SHannes Reinecke 
2056229d4f11SHannes Reinecke 			ret = core_alua_allocate_lba_map_mem(lba_map,
2057229d4f11SHannes Reinecke 							     pg_id, alua_state);
2058229d4f11SHannes Reinecke 			if (ret) {
2059229d4f11SHannes Reinecke 				pr_err("Invalid target descriptor %d:%c "
2060229d4f11SHannes Reinecke 				       "at line %d\n",
2061229d4f11SHannes Reinecke 				       pg_id, state, num);
2062229d4f11SHannes Reinecke 				break;
2063229d4f11SHannes Reinecke 			}
2064229d4f11SHannes Reinecke 			pg++;
2065229d4f11SHannes Reinecke 			ptr = strchr(ptr, ' ');
2066229d4f11SHannes Reinecke 			if (ptr)
2067229d4f11SHannes Reinecke 				ptr++;
2068229d4f11SHannes Reinecke 			else
2069229d4f11SHannes Reinecke 				break;
2070229d4f11SHannes Reinecke 		}
2071229d4f11SHannes Reinecke 		if (pg_num == -1)
2072229d4f11SHannes Reinecke 		    pg_num = pg;
2073229d4f11SHannes Reinecke 		else if (pg != pg_num) {
2074229d4f11SHannes Reinecke 			pr_err("Only %d from %d port groups definitions "
2075229d4f11SHannes Reinecke 			       "at line %d\n", pg, pg_num, num);
2076229d4f11SHannes Reinecke 			ret = -EINVAL;
2077229d4f11SHannes Reinecke 			break;
2078229d4f11SHannes Reinecke 		}
2079229d4f11SHannes Reinecke 		num++;
2080229d4f11SHannes Reinecke 	}
2081229d4f11SHannes Reinecke out:
2082229d4f11SHannes Reinecke 	if (ret) {
2083229d4f11SHannes Reinecke 		core_alua_free_lba_map(&lba_list);
2084229d4f11SHannes Reinecke 		count = ret;
2085229d4f11SHannes Reinecke 	} else
2086229d4f11SHannes Reinecke 		core_alua_set_lba_map(dev, &lba_list,
2087229d4f11SHannes Reinecke 				      segment_size, segment_mult);
2088229d4f11SHannes Reinecke 	kfree(map_entries);
2089229d4f11SHannes Reinecke 	return count;
2090229d4f11SHannes Reinecke }
2091229d4f11SHannes Reinecke 
20922eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_dev_, info);
20932eafd729SChristoph Hellwig CONFIGFS_ATTR_WO(target_dev_, control);
20942eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alias);
20952eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, udev_path);
20962eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, enable);
20972eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alua_lu_gp);
20982eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, lba_map);
2099229d4f11SHannes Reinecke 
210073112edcSNicholas Bellinger static struct configfs_attribute *target_core_dev_attrs[] = {
21012eafd729SChristoph Hellwig 	&target_dev_attr_info,
21022eafd729SChristoph Hellwig 	&target_dev_attr_control,
21032eafd729SChristoph Hellwig 	&target_dev_attr_alias,
21042eafd729SChristoph Hellwig 	&target_dev_attr_udev_path,
21052eafd729SChristoph Hellwig 	&target_dev_attr_enable,
21062eafd729SChristoph Hellwig 	&target_dev_attr_alua_lu_gp,
21072eafd729SChristoph Hellwig 	&target_dev_attr_lba_map,
2108c66ac9dbSNicholas Bellinger 	NULL,
2109c66ac9dbSNicholas Bellinger };
2110c66ac9dbSNicholas Bellinger 
2111c66ac9dbSNicholas Bellinger static void target_core_dev_release(struct config_item *item)
2112c66ac9dbSNicholas Bellinger {
21130fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
21140fd97ccfSChristoph Hellwig 	struct se_device *dev =
21150fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2116c66ac9dbSNicholas Bellinger 
2117c66ac9dbSNicholas Bellinger 	kfree(dev_cg->default_groups);
21180fd97ccfSChristoph Hellwig 	target_free_device(dev);
2119c66ac9dbSNicholas Bellinger }
2120c66ac9dbSNicholas Bellinger 
2121c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_item_ops = {
2122c66ac9dbSNicholas Bellinger 	.release		= target_core_dev_release,
2123c66ac9dbSNicholas Bellinger };
2124c66ac9dbSNicholas Bellinger 
212573112edcSNicholas Bellinger TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2126c66ac9dbSNicholas Bellinger 
212773112edcSNicholas Bellinger /* End functions for struct config_item_type tb_dev_cit */
2128c66ac9dbSNicholas Bellinger 
2129c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2130c66ac9dbSNicholas Bellinger 
21312eafd729SChristoph Hellwig static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2132c66ac9dbSNicholas Bellinger {
21332eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_lu_gp,
21342eafd729SChristoph Hellwig 			lu_gp_group);
21352eafd729SChristoph Hellwig }
21362eafd729SChristoph Hellwig 
21372eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
21382eafd729SChristoph Hellwig {
21392eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
21402eafd729SChristoph Hellwig 
21416708bb27SAndy Grover 	if (!lu_gp->lu_gp_valid_id)
2142c66ac9dbSNicholas Bellinger 		return 0;
2143c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2144c66ac9dbSNicholas Bellinger }
2145c66ac9dbSNicholas Bellinger 
21462eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
21472eafd729SChristoph Hellwig 		const char *page, size_t count)
2148c66ac9dbSNicholas Bellinger {
21492eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2150c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2151c66ac9dbSNicholas Bellinger 	unsigned long lu_gp_id;
2152c66ac9dbSNicholas Bellinger 	int ret;
2153c66ac9dbSNicholas Bellinger 
215457103d7fSJingoo Han 	ret = kstrtoul(page, 0, &lu_gp_id);
2155c66ac9dbSNicholas Bellinger 	if (ret < 0) {
215657103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2157c66ac9dbSNicholas Bellinger 			" lu_gp_id\n", ret);
215857103d7fSJingoo Han 		return ret;
2159c66ac9dbSNicholas Bellinger 	}
2160c66ac9dbSNicholas Bellinger 	if (lu_gp_id > 0x0000ffff) {
21616708bb27SAndy Grover 		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2162c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", lu_gp_id);
2163c66ac9dbSNicholas Bellinger 		return -EINVAL;
2164c66ac9dbSNicholas Bellinger 	}
2165c66ac9dbSNicholas Bellinger 
2166c66ac9dbSNicholas Bellinger 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2167c66ac9dbSNicholas Bellinger 	if (ret < 0)
2168c66ac9dbSNicholas Bellinger 		return -EINVAL;
2169c66ac9dbSNicholas Bellinger 
21706708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2171c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
2172c66ac9dbSNicholas Bellinger 		config_item_name(&alua_lu_gp_cg->cg_item),
2173c66ac9dbSNicholas Bellinger 		lu_gp->lu_gp_id);
2174c66ac9dbSNicholas Bellinger 
2175c66ac9dbSNicholas Bellinger 	return count;
2176c66ac9dbSNicholas Bellinger }
2177c66ac9dbSNicholas Bellinger 
21782eafd729SChristoph Hellwig static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2179c66ac9dbSNicholas Bellinger {
21802eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2181c66ac9dbSNicholas Bellinger 	struct se_device *dev;
2182c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2183c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2184c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2185c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
2186c66ac9dbSNicholas Bellinger 
2187c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
2188c66ac9dbSNicholas Bellinger 
2189c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp->lu_gp_lock);
2190c66ac9dbSNicholas Bellinger 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2191c66ac9dbSNicholas Bellinger 		dev = lu_gp_mem->lu_gp_mem_dev;
21920fd97ccfSChristoph Hellwig 		hba = dev->se_hba;
2193c66ac9dbSNicholas Bellinger 
2194c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2195c66ac9dbSNicholas Bellinger 			config_item_name(&hba->hba_group.cg_item),
21960fd97ccfSChristoph Hellwig 			config_item_name(&dev->dev_group.cg_item));
2197c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2198c66ac9dbSNicholas Bellinger 
2199c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
22006708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2201c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2202c66ac9dbSNicholas Bellinger 			break;
2203c66ac9dbSNicholas Bellinger 		}
2204c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2205c66ac9dbSNicholas Bellinger 		len += cur_len;
2206c66ac9dbSNicholas Bellinger 	}
2207c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp->lu_gp_lock);
2208c66ac9dbSNicholas Bellinger 
2209c66ac9dbSNicholas Bellinger 	return len;
2210c66ac9dbSNicholas Bellinger }
2211c66ac9dbSNicholas Bellinger 
22122eafd729SChristoph Hellwig CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
22132eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_lu_gp_, members);
2214c66ac9dbSNicholas Bellinger 
2215c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
22162eafd729SChristoph Hellwig 	&target_lu_gp_attr_lu_gp_id,
22172eafd729SChristoph Hellwig 	&target_lu_gp_attr_members,
2218c66ac9dbSNicholas Bellinger 	NULL,
2219c66ac9dbSNicholas Bellinger };
2220c66ac9dbSNicholas Bellinger 
22211f6fe7cbSNicholas Bellinger static void target_core_alua_lu_gp_release(struct config_item *item)
22221f6fe7cbSNicholas Bellinger {
22231f6fe7cbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
22241f6fe7cbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
22251f6fe7cbSNicholas Bellinger 
22261f6fe7cbSNicholas Bellinger 	core_alua_free_lu_gp(lu_gp);
22271f6fe7cbSNicholas Bellinger }
22281f6fe7cbSNicholas Bellinger 
2229c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_lu_gp_ops = {
22301f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_lu_gp_release,
2231c66ac9dbSNicholas Bellinger };
2232c66ac9dbSNicholas Bellinger 
2233c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gp_cit = {
2234c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
2235c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_lu_gp_attrs,
2236c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2237c66ac9dbSNicholas Bellinger };
2238c66ac9dbSNicholas Bellinger 
2239c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2240c66ac9dbSNicholas Bellinger 
2241c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2242c66ac9dbSNicholas Bellinger 
2243c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_lu_gp(
2244c66ac9dbSNicholas Bellinger 	struct config_group *group,
2245c66ac9dbSNicholas Bellinger 	const char *name)
2246c66ac9dbSNicholas Bellinger {
2247c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2248c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = NULL;
2249c66ac9dbSNicholas Bellinger 	struct config_item *alua_lu_gp_ci = NULL;
2250c66ac9dbSNicholas Bellinger 
2251c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp(name, 0);
2252c66ac9dbSNicholas Bellinger 	if (IS_ERR(lu_gp))
2253c66ac9dbSNicholas Bellinger 		return NULL;
2254c66ac9dbSNicholas Bellinger 
2255c66ac9dbSNicholas Bellinger 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
2256c66ac9dbSNicholas Bellinger 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2257c66ac9dbSNicholas Bellinger 
2258c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_lu_gp_cg, name,
2259c66ac9dbSNicholas Bellinger 			&target_core_alua_lu_gp_cit);
2260c66ac9dbSNicholas Bellinger 
22616708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2262c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s\n",
2263c66ac9dbSNicholas Bellinger 		config_item_name(alua_lu_gp_ci));
2264c66ac9dbSNicholas Bellinger 
2265c66ac9dbSNicholas Bellinger 	return alua_lu_gp_cg;
2266c66ac9dbSNicholas Bellinger 
2267c66ac9dbSNicholas Bellinger }
2268c66ac9dbSNicholas Bellinger 
2269c66ac9dbSNicholas Bellinger static void target_core_alua_drop_lu_gp(
2270c66ac9dbSNicholas Bellinger 	struct config_group *group,
2271c66ac9dbSNicholas Bellinger 	struct config_item *item)
2272c66ac9dbSNicholas Bellinger {
2273c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2274c66ac9dbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
2275c66ac9dbSNicholas Bellinger 
22766708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2277c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
2278c66ac9dbSNicholas Bellinger 		config_item_name(item), lu_gp->lu_gp_id);
22791f6fe7cbSNicholas Bellinger 	/*
22801f6fe7cbSNicholas Bellinger 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
22811f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_lu_gp_release()
22821f6fe7cbSNicholas Bellinger 	 */
2283c66ac9dbSNicholas Bellinger 	config_item_put(item);
2284c66ac9dbSNicholas Bellinger }
2285c66ac9dbSNicholas Bellinger 
2286c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2287c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_lu_gp,
2288c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_lu_gp,
2289c66ac9dbSNicholas Bellinger };
2290c66ac9dbSNicholas Bellinger 
2291c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gps_cit = {
2292c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2293c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
2294c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2295c66ac9dbSNicholas Bellinger };
2296c66ac9dbSNicholas Bellinger 
2297c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2298c66ac9dbSNicholas Bellinger 
2299c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2300c66ac9dbSNicholas Bellinger 
23012eafd729SChristoph Hellwig static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
23022eafd729SChristoph Hellwig {
23032eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
23042eafd729SChristoph Hellwig 			tg_pt_gp_group);
23052eafd729SChristoph Hellwig }
2306c66ac9dbSNicholas Bellinger 
23072eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2308c66ac9dbSNicholas Bellinger 		char *page)
2309c66ac9dbSNicholas Bellinger {
2310c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n",
23112eafd729SChristoph Hellwig 		atomic_read(&to_tg_pt_gp(item)->tg_pt_gp_alua_access_state));
2312c66ac9dbSNicholas Bellinger }
2313c66ac9dbSNicholas Bellinger 
23142eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
23152eafd729SChristoph Hellwig 		const char *page, size_t count)
2316c66ac9dbSNicholas Bellinger {
23172eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
23180fd97ccfSChristoph Hellwig 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2319c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2320c66ac9dbSNicholas Bellinger 	int new_state, ret;
2321c66ac9dbSNicholas Bellinger 
23226708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2323125d0119SHannes Reinecke 		pr_err("Unable to do implicit ALUA on non valid"
2324c66ac9dbSNicholas Bellinger 			" tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2325c66ac9dbSNicholas Bellinger 		return -EINVAL;
2326c66ac9dbSNicholas Bellinger 	}
2327f1453773SNicholas Bellinger 	if (!(dev->dev_flags & DF_CONFIGURED)) {
2328f1453773SNicholas Bellinger 		pr_err("Unable to set alua_access_state while device is"
2329f1453773SNicholas Bellinger 		       " not configured\n");
2330f1453773SNicholas Bellinger 		return -ENODEV;
2331f1453773SNicholas Bellinger 	}
2332c66ac9dbSNicholas Bellinger 
233357103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2334c66ac9dbSNicholas Bellinger 	if (ret < 0) {
23356708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access state from"
2336c66ac9dbSNicholas Bellinger 				" %s\n", page);
233757103d7fSJingoo Han 		return ret;
2338c66ac9dbSNicholas Bellinger 	}
2339c66ac9dbSNicholas Bellinger 	new_state = (int)tmp;
2340c66ac9dbSNicholas Bellinger 
2341125d0119SHannes Reinecke 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2342125d0119SHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA"
2343125d0119SHannes Reinecke 			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
2344c66ac9dbSNicholas Bellinger 		return -EINVAL;
2345c66ac9dbSNicholas Bellinger 	}
2346c66094bfSHannes Reinecke 	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2347c66094bfSHannes Reinecke 	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2348c66094bfSHannes Reinecke 		/* LBA DEPENDENT is only allowed with implicit ALUA */
2349c66094bfSHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA transition"
2350c66094bfSHannes Reinecke 		       " while explicit ALUA management is enabled\n");
2351c66094bfSHannes Reinecke 		return -EINVAL;
2352c66094bfSHannes Reinecke 	}
2353c66ac9dbSNicholas Bellinger 
23540fd97ccfSChristoph Hellwig 	ret = core_alua_do_port_transition(tg_pt_gp, dev,
2355c66ac9dbSNicholas Bellinger 					NULL, NULL, new_state, 0);
2356c66ac9dbSNicholas Bellinger 	return (!ret) ? count : -EINVAL;
2357c66ac9dbSNicholas Bellinger }
2358c66ac9dbSNicholas Bellinger 
23592eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2360c66ac9dbSNicholas Bellinger 		char *page)
2361c66ac9dbSNicholas Bellinger {
23622eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2363c66ac9dbSNicholas Bellinger 	return sprintf(page, "%s\n",
2364c66ac9dbSNicholas Bellinger 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2365c66ac9dbSNicholas Bellinger }
2366c66ac9dbSNicholas Bellinger 
23672eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_store(
23682eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2369c66ac9dbSNicholas Bellinger {
23702eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2371c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2372c66ac9dbSNicholas Bellinger 	int new_status, ret;
2373c66ac9dbSNicholas Bellinger 
23746708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
23756708bb27SAndy Grover 		pr_err("Unable to do set ALUA access status on non"
2376c66ac9dbSNicholas Bellinger 			" valid tg_pt_gp ID: %hu\n",
2377c66ac9dbSNicholas Bellinger 			tg_pt_gp->tg_pt_gp_valid_id);
2378c66ac9dbSNicholas Bellinger 		return -EINVAL;
2379c66ac9dbSNicholas Bellinger 	}
2380c66ac9dbSNicholas Bellinger 
238157103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2382c66ac9dbSNicholas Bellinger 	if (ret < 0) {
23836708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access status"
2384c66ac9dbSNicholas Bellinger 				" from %s\n", page);
238557103d7fSJingoo Han 		return ret;
2386c66ac9dbSNicholas Bellinger 	}
2387c66ac9dbSNicholas Bellinger 	new_status = (int)tmp;
2388c66ac9dbSNicholas Bellinger 
2389c66ac9dbSNicholas Bellinger 	if ((new_status != ALUA_STATUS_NONE) &&
2390125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2391125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
23926708bb27SAndy Grover 		pr_err("Illegal ALUA access status: 0x%02x\n",
2393c66ac9dbSNicholas Bellinger 				new_status);
2394c66ac9dbSNicholas Bellinger 		return -EINVAL;
2395c66ac9dbSNicholas Bellinger 	}
2396c66ac9dbSNicholas Bellinger 
2397c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2398c66ac9dbSNicholas Bellinger 	return count;
2399c66ac9dbSNicholas Bellinger }
2400c66ac9dbSNicholas Bellinger 
24012eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2402c66ac9dbSNicholas Bellinger 		char *page)
2403c66ac9dbSNicholas Bellinger {
24042eafd729SChristoph Hellwig 	return core_alua_show_access_type(to_tg_pt_gp(item), page);
2405c66ac9dbSNicholas Bellinger }
2406c66ac9dbSNicholas Bellinger 
24072eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
24082eafd729SChristoph Hellwig 		const char *page, size_t count)
2409c66ac9dbSNicholas Bellinger {
24102eafd729SChristoph Hellwig 	return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2411c66ac9dbSNicholas Bellinger }
2412c66ac9dbSNicholas Bellinger 
24132eafd729SChristoph Hellwig #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit)				\
24142eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_show(		\
24152eafd729SChristoph Hellwig 		struct config_item *item, char *p)			\
2416b0a382c5SHannes Reinecke {									\
24172eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
24182eafd729SChristoph Hellwig 	return sprintf(p, "%d\n",					\
24192eafd729SChristoph Hellwig 		!!(t->tg_pt_gp_alua_supported_states & _bit));		\
24202eafd729SChristoph Hellwig }									\
24212eafd729SChristoph Hellwig 									\
24222eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_store(		\
24232eafd729SChristoph Hellwig 		struct config_item *item, const char *p, size_t c)	\
2424b0a382c5SHannes Reinecke {									\
24252eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
2426b0a382c5SHannes Reinecke 	unsigned long tmp;						\
2427b0a382c5SHannes Reinecke 	int ret;							\
2428b0a382c5SHannes Reinecke 									\
2429b0a382c5SHannes Reinecke 	if (!t->tg_pt_gp_valid_id) {					\
2430b0a382c5SHannes Reinecke 		pr_err("Unable to do set ##_name ALUA state on non"	\
2431b0a382c5SHannes Reinecke 		       " valid tg_pt_gp ID: %hu\n",			\
2432b0a382c5SHannes Reinecke 		       t->tg_pt_gp_valid_id);				\
2433b0a382c5SHannes Reinecke 		return -EINVAL;						\
2434b0a382c5SHannes Reinecke 	}								\
2435b0a382c5SHannes Reinecke 									\
2436b0a382c5SHannes Reinecke 	ret = kstrtoul(p, 0, &tmp);					\
2437b0a382c5SHannes Reinecke 	if (ret < 0) {							\
2438b0a382c5SHannes Reinecke 		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
2439b0a382c5SHannes Reinecke 		return -EINVAL;						\
2440b0a382c5SHannes Reinecke 	}								\
2441b0a382c5SHannes Reinecke 	if (tmp > 1) {							\
2442b0a382c5SHannes Reinecke 		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2443b0a382c5SHannes Reinecke 		return -EINVAL;						\
2444b0a382c5SHannes Reinecke 	}								\
24451f0b030cSSebastian Herbszt 	if (tmp)							\
24462eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states |= _bit;		\
2447b0a382c5SHannes Reinecke 	else								\
24482eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states &= ~_bit;		\
2449b0a382c5SHannes Reinecke 									\
2450b0a382c5SHannes Reinecke 	return c;							\
24516be526c4SHannes Reinecke }
24526be526c4SHannes Reinecke 
24532eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
24542eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
24552eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
24562eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
24572eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
24582eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
24592eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
24606be526c4SHannes Reinecke 
24612eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_show(
24622eafd729SChristoph Hellwig 		struct config_item *item, char *page)
2463c66ac9dbSNicholas Bellinger {
24642eafd729SChristoph Hellwig 	return sprintf(page, "%d\n",
24652eafd729SChristoph Hellwig 		to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
2466c66ac9dbSNicholas Bellinger }
2467c66ac9dbSNicholas Bellinger 
24682eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_store(
24692eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2470c66ac9dbSNicholas Bellinger {
24712eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2472c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2473c66ac9dbSNicholas Bellinger 	int ret;
2474c66ac9dbSNicholas Bellinger 
247557103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2476c66ac9dbSNicholas Bellinger 	if (ret < 0) {
24776708bb27SAndy Grover 		pr_err("Unable to extract alua_write_metadata\n");
247857103d7fSJingoo Han 		return ret;
2479c66ac9dbSNicholas Bellinger 	}
2480c66ac9dbSNicholas Bellinger 
2481c66ac9dbSNicholas Bellinger 	if ((tmp != 0) && (tmp != 1)) {
24826708bb27SAndy Grover 		pr_err("Illegal value for alua_write_metadata:"
2483c66ac9dbSNicholas Bellinger 			" %lu\n", tmp);
2484c66ac9dbSNicholas Bellinger 		return -EINVAL;
2485c66ac9dbSNicholas Bellinger 	}
2486c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2487c66ac9dbSNicholas Bellinger 
2488c66ac9dbSNicholas Bellinger 	return count;
2489c66ac9dbSNicholas Bellinger }
2490c66ac9dbSNicholas Bellinger 
24912eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2492c66ac9dbSNicholas Bellinger 		char *page)
2493c66ac9dbSNicholas Bellinger {
24942eafd729SChristoph Hellwig 	return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
2495c66ac9dbSNicholas Bellinger }
2496c66ac9dbSNicholas Bellinger 
24972eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
24982eafd729SChristoph Hellwig 		const char *page, size_t count)
2499c66ac9dbSNicholas Bellinger {
25002eafd729SChristoph Hellwig 	return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
25012eafd729SChristoph Hellwig 			count);
2502c66ac9dbSNicholas Bellinger }
2503c66ac9dbSNicholas Bellinger 
25042eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2505c66ac9dbSNicholas Bellinger 		char *page)
2506c66ac9dbSNicholas Bellinger {
25072eafd729SChristoph Hellwig 	return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
2508c66ac9dbSNicholas Bellinger }
2509c66ac9dbSNicholas Bellinger 
25102eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
25112eafd729SChristoph Hellwig 		const char *page, size_t count)
2512c66ac9dbSNicholas Bellinger {
25132eafd729SChristoph Hellwig 	return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
25142eafd729SChristoph Hellwig 			count);
2515c66ac9dbSNicholas Bellinger }
2516c66ac9dbSNicholas Bellinger 
25172eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
25182eafd729SChristoph Hellwig 		struct config_item *item, char *page)
25192eafd729SChristoph Hellwig {
25202eafd729SChristoph Hellwig 	return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
25212eafd729SChristoph Hellwig }
2522c66ac9dbSNicholas Bellinger 
25232eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
25242eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
25252eafd729SChristoph Hellwig {
25262eafd729SChristoph Hellwig 	return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
25272eafd729SChristoph Hellwig 			count);
25282eafd729SChristoph Hellwig }
25292eafd729SChristoph Hellwig 
25302eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
25315b9a4d72SNicholas Bellinger 		char *page)
25325b9a4d72SNicholas Bellinger {
25332eafd729SChristoph Hellwig 	return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
25345b9a4d72SNicholas Bellinger }
25355b9a4d72SNicholas Bellinger 
25362eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
25372eafd729SChristoph Hellwig 		const char *page, size_t count)
25385b9a4d72SNicholas Bellinger {
25392eafd729SChristoph Hellwig 	return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
25405b9a4d72SNicholas Bellinger }
25415b9a4d72SNicholas Bellinger 
25422eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2543c66ac9dbSNicholas Bellinger 		char *page)
2544c66ac9dbSNicholas Bellinger {
25452eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2546c66ac9dbSNicholas Bellinger 
25476708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id)
2548c66ac9dbSNicholas Bellinger 		return 0;
2549c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2550c66ac9dbSNicholas Bellinger }
2551c66ac9dbSNicholas Bellinger 
25522eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
25532eafd729SChristoph Hellwig 		const char *page, size_t count)
2554c66ac9dbSNicholas Bellinger {
25552eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2556c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2557c66ac9dbSNicholas Bellinger 	unsigned long tg_pt_gp_id;
2558c66ac9dbSNicholas Bellinger 	int ret;
2559c66ac9dbSNicholas Bellinger 
256057103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tg_pt_gp_id);
2561c66ac9dbSNicholas Bellinger 	if (ret < 0) {
256257103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2563c66ac9dbSNicholas Bellinger 			" tg_pt_gp_id\n", ret);
256457103d7fSJingoo Han 		return ret;
2565c66ac9dbSNicholas Bellinger 	}
2566c66ac9dbSNicholas Bellinger 	if (tg_pt_gp_id > 0x0000ffff) {
25676708bb27SAndy Grover 		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2568c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", tg_pt_gp_id);
2569c66ac9dbSNicholas Bellinger 		return -EINVAL;
2570c66ac9dbSNicholas Bellinger 	}
2571c66ac9dbSNicholas Bellinger 
2572c66ac9dbSNicholas Bellinger 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2573c66ac9dbSNicholas Bellinger 	if (ret < 0)
2574c66ac9dbSNicholas Bellinger 		return -EINVAL;
2575c66ac9dbSNicholas Bellinger 
25766708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2577c66ac9dbSNicholas Bellinger 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
2578c66ac9dbSNicholas Bellinger 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
2579c66ac9dbSNicholas Bellinger 		tg_pt_gp->tg_pt_gp_id);
2580c66ac9dbSNicholas Bellinger 
2581c66ac9dbSNicholas Bellinger 	return count;
2582c66ac9dbSNicholas Bellinger }
2583c66ac9dbSNicholas Bellinger 
25842eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2585c66ac9dbSNicholas Bellinger 		char *page)
2586c66ac9dbSNicholas Bellinger {
25872eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2588c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
2589c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2590c66ac9dbSNicholas Bellinger 	unsigned char buf[TG_PT_GROUP_NAME_BUF];
2591c66ac9dbSNicholas Bellinger 
2592c66ac9dbSNicholas Bellinger 	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2593c66ac9dbSNicholas Bellinger 
2594c66ac9dbSNicholas Bellinger 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2595adf653f9SChristoph Hellwig 	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2596adf653f9SChristoph Hellwig 			lun_tg_pt_gp_link) {
2597adf653f9SChristoph Hellwig 		struct se_portal_group *tpg = lun->lun_tpg;
2598c66ac9dbSNicholas Bellinger 
2599c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2600e3d6f909SAndy Grover 			"/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2601e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2602e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
2603c66ac9dbSNicholas Bellinger 			config_item_name(&lun->lun_group.cg_item));
2604c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2605c66ac9dbSNicholas Bellinger 
2606c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
26076708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2608c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2609c66ac9dbSNicholas Bellinger 			break;
2610c66ac9dbSNicholas Bellinger 		}
2611c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2612c66ac9dbSNicholas Bellinger 		len += cur_len;
2613c66ac9dbSNicholas Bellinger 	}
2614c66ac9dbSNicholas Bellinger 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2615c66ac9dbSNicholas Bellinger 
2616c66ac9dbSNicholas Bellinger 	return len;
2617c66ac9dbSNicholas Bellinger }
2618c66ac9dbSNicholas Bellinger 
26192eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
26202eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
26212eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
26222eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
26232eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
26242eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
26252eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
26262eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
26272eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
26282eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
26292eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
26302eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
26312eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
26322eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
26332eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
26342eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
26352eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
2636c66ac9dbSNicholas Bellinger 
2637c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
26382eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_state,
26392eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_status,
26402eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_type,
26412eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_transitioning,
26422eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_offline,
26432eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_lba_dependent,
26442eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_unavailable,
26452eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_standby,
26462eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_nonoptimized,
26472eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_optimized,
26482eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_write_metadata,
26492eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_nonop_delay_msecs,
26502eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_trans_delay_msecs,
26512eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_implicit_trans_secs,
26522eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_preferred,
26532eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_tg_pt_gp_id,
26542eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_members,
2655c66ac9dbSNicholas Bellinger 	NULL,
2656c66ac9dbSNicholas Bellinger };
2657c66ac9dbSNicholas Bellinger 
26581f6fe7cbSNicholas Bellinger static void target_core_alua_tg_pt_gp_release(struct config_item *item)
26591f6fe7cbSNicholas Bellinger {
26601f6fe7cbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
26611f6fe7cbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
26621f6fe7cbSNicholas Bellinger 
26631f6fe7cbSNicholas Bellinger 	core_alua_free_tg_pt_gp(tg_pt_gp);
26641f6fe7cbSNicholas Bellinger }
26651f6fe7cbSNicholas Bellinger 
2666c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
26671f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_tg_pt_gp_release,
2668c66ac9dbSNicholas Bellinger };
2669c66ac9dbSNicholas Bellinger 
2670c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2671c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
2672c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
2673c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2674c66ac9dbSNicholas Bellinger };
2675c66ac9dbSNicholas Bellinger 
2676c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2677c66ac9dbSNicholas Bellinger 
267872aca57bSNicholas Bellinger /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2679c66ac9dbSNicholas Bellinger 
2680c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_tg_pt_gp(
2681c66ac9dbSNicholas Bellinger 	struct config_group *group,
2682c66ac9dbSNicholas Bellinger 	const char *name)
2683c66ac9dbSNicholas Bellinger {
2684c66ac9dbSNicholas Bellinger 	struct t10_alua *alua = container_of(group, struct t10_alua,
2685c66ac9dbSNicholas Bellinger 					alua_tg_pt_gps_group);
2686c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2687c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = NULL;
2688c66ac9dbSNicholas Bellinger 	struct config_item *alua_tg_pt_gp_ci = NULL;
2689c66ac9dbSNicholas Bellinger 
26900fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
26916708bb27SAndy Grover 	if (!tg_pt_gp)
2692c66ac9dbSNicholas Bellinger 		return NULL;
2693c66ac9dbSNicholas Bellinger 
2694c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2695c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2696c66ac9dbSNicholas Bellinger 
2697c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
2698c66ac9dbSNicholas Bellinger 			&target_core_alua_tg_pt_gp_cit);
2699c66ac9dbSNicholas Bellinger 
27006708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2701c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s\n",
2702c66ac9dbSNicholas Bellinger 		config_item_name(alua_tg_pt_gp_ci));
2703c66ac9dbSNicholas Bellinger 
2704c66ac9dbSNicholas Bellinger 	return alua_tg_pt_gp_cg;
2705c66ac9dbSNicholas Bellinger }
2706c66ac9dbSNicholas Bellinger 
2707c66ac9dbSNicholas Bellinger static void target_core_alua_drop_tg_pt_gp(
2708c66ac9dbSNicholas Bellinger 	struct config_group *group,
2709c66ac9dbSNicholas Bellinger 	struct config_item *item)
2710c66ac9dbSNicholas Bellinger {
2711c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2712c66ac9dbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2713c66ac9dbSNicholas Bellinger 
27146708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2715c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
2716c66ac9dbSNicholas Bellinger 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
27171f6fe7cbSNicholas Bellinger 	/*
27181f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
27191f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_tg_pt_gp_release().
27201f6fe7cbSNicholas Bellinger 	 */
2721c66ac9dbSNicholas Bellinger 	config_item_put(item);
2722c66ac9dbSNicholas Bellinger }
2723c66ac9dbSNicholas Bellinger 
2724c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2725c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_tg_pt_gp,
2726c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
2727c66ac9dbSNicholas Bellinger };
2728c66ac9dbSNicholas Bellinger 
272972aca57bSNicholas Bellinger TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
2730c66ac9dbSNicholas Bellinger 
273172aca57bSNicholas Bellinger /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2732c66ac9dbSNicholas Bellinger 
2733c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_cit */
2734c66ac9dbSNicholas Bellinger 
2735c66ac9dbSNicholas Bellinger /*
2736c66ac9dbSNicholas Bellinger  * target_core_alua_cit is a ConfigFS group that lives under
2737c66ac9dbSNicholas Bellinger  * /sys/kernel/config/target/core/alua.  There are default groups
2738c66ac9dbSNicholas Bellinger  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2739c66ac9dbSNicholas Bellinger  * target_core_alua_cit in target_core_init_configfs() below.
2740c66ac9dbSNicholas Bellinger  */
2741c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_cit = {
2742c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2743c66ac9dbSNicholas Bellinger 	.ct_attrs		= NULL,
2744c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2745c66ac9dbSNicholas Bellinger };
2746c66ac9dbSNicholas Bellinger 
2747c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_cit */
2748c66ac9dbSNicholas Bellinger 
2749d23ab570SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_stat_cit */
275012d23384SNicholas Bellinger 
275112d23384SNicholas Bellinger static struct config_group *target_core_stat_mkdir(
275212d23384SNicholas Bellinger 	struct config_group *group,
275312d23384SNicholas Bellinger 	const char *name)
275412d23384SNicholas Bellinger {
275512d23384SNicholas Bellinger 	return ERR_PTR(-ENOSYS);
275612d23384SNicholas Bellinger }
275712d23384SNicholas Bellinger 
275812d23384SNicholas Bellinger static void target_core_stat_rmdir(
275912d23384SNicholas Bellinger 	struct config_group *group,
276012d23384SNicholas Bellinger 	struct config_item *item)
276112d23384SNicholas Bellinger {
276212d23384SNicholas Bellinger 	return;
276312d23384SNicholas Bellinger }
276412d23384SNicholas Bellinger 
276512d23384SNicholas Bellinger static struct configfs_group_operations target_core_stat_group_ops = {
276612d23384SNicholas Bellinger 	.make_group		= &target_core_stat_mkdir,
276712d23384SNicholas Bellinger 	.drop_item		= &target_core_stat_rmdir,
276812d23384SNicholas Bellinger };
276912d23384SNicholas Bellinger 
2770d23ab570SNicholas Bellinger TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
277112d23384SNicholas Bellinger 
2772d23ab570SNicholas Bellinger /* End functions for struct config_item_type tb_dev_stat_cit */
277312d23384SNicholas Bellinger 
2774c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_hba_cit */
2775c66ac9dbSNicholas Bellinger 
2776c66ac9dbSNicholas Bellinger static struct config_group *target_core_make_subdev(
2777c66ac9dbSNicholas Bellinger 	struct config_group *group,
2778c66ac9dbSNicholas Bellinger 	const char *name)
2779c66ac9dbSNicholas Bellinger {
2780c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2781c66ac9dbSNicholas Bellinger 	struct config_item *hba_ci = &group->cg_item;
2782c66ac9dbSNicholas Bellinger 	struct se_hba *hba = item_to_hba(hba_ci);
27830a06d430SChristoph Hellwig 	struct target_backend *tb = hba->backend;
27840fd97ccfSChristoph Hellwig 	struct se_device *dev;
2785c66ac9dbSNicholas Bellinger 	struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
278612d23384SNicholas Bellinger 	struct config_group *dev_stat_grp = NULL;
278712d23384SNicholas Bellinger 	int errno = -ENOMEM, ret;
2788c66ac9dbSNicholas Bellinger 
278912d23384SNicholas Bellinger 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
279012d23384SNicholas Bellinger 	if (ret)
279112d23384SNicholas Bellinger 		return ERR_PTR(ret);
2792c66ac9dbSNicholas Bellinger 
27930fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, name);
27940fd97ccfSChristoph Hellwig 	if (!dev)
27950fd97ccfSChristoph Hellwig 		goto out_unlock;
2796c66ac9dbSNicholas Bellinger 
27970fd97ccfSChristoph Hellwig 	dev_cg = &dev->dev_group;
2798c66ac9dbSNicholas Bellinger 
279913f6a914SSebastian Andrzej Siewior 	dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
2800c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
28016708bb27SAndy Grover 	if (!dev_cg->default_groups)
28020fd97ccfSChristoph Hellwig 		goto out_free_device;
2803c66ac9dbSNicholas Bellinger 
28040a06d430SChristoph Hellwig 	config_group_init_type_name(dev_cg, name, &tb->tb_dev_cit);
28050fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
28060a06d430SChristoph Hellwig 			&tb->tb_dev_attrib_cit);
28070fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_pr_group, "pr",
28080a06d430SChristoph Hellwig 			&tb->tb_dev_pr_cit);
28090fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
28100a06d430SChristoph Hellwig 			&tb->tb_dev_wwn_cit);
28110fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
28120a06d430SChristoph Hellwig 			"alua", &tb->tb_dev_alua_tg_pt_gps_cit);
28130fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
28140a06d430SChristoph Hellwig 			"statistics", &tb->tb_dev_stat_cit);
281512d23384SNicholas Bellinger 
28160fd97ccfSChristoph Hellwig 	dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
28170fd97ccfSChristoph Hellwig 	dev_cg->default_groups[1] = &dev->dev_pr_group;
28180fd97ccfSChristoph Hellwig 	dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
28190fd97ccfSChristoph Hellwig 	dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
28200fd97ccfSChristoph Hellwig 	dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
282112d23384SNicholas Bellinger 	dev_cg->default_groups[5] = NULL;
2822c66ac9dbSNicholas Bellinger 	/*
282312d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2824c66ac9dbSNicholas Bellinger 	 */
28250fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
28266708bb27SAndy Grover 	if (!tg_pt_gp)
28270fd97ccfSChristoph Hellwig 		goto out_free_dev_cg_default_groups;
28280fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2829c66ac9dbSNicholas Bellinger 
28300fd97ccfSChristoph Hellwig 	tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
283113f6a914SSebastian Andrzej Siewior 	tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2832c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
28336708bb27SAndy Grover 	if (!tg_pt_gp_cg->default_groups) {
28346708bb27SAndy Grover 		pr_err("Unable to allocate tg_pt_gp_cg->"
2835c66ac9dbSNicholas Bellinger 				"default_groups\n");
28360fd97ccfSChristoph Hellwig 		goto out_free_tg_pt_gp;
2837c66ac9dbSNicholas Bellinger 	}
2838c66ac9dbSNicholas Bellinger 
2839c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2840c66ac9dbSNicholas Bellinger 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2841c66ac9dbSNicholas Bellinger 	tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2842c66ac9dbSNicholas Bellinger 	tg_pt_gp_cg->default_groups[1] = NULL;
284312d23384SNicholas Bellinger 	/*
284412d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/statistics/ default groups
284512d23384SNicholas Bellinger 	 */
28460fd97ccfSChristoph Hellwig 	dev_stat_grp = &dev->dev_stat_grps.stat_group;
284713f6a914SSebastian Andrzej Siewior 	dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
284812d23384SNicholas Bellinger 				GFP_KERNEL);
284912d23384SNicholas Bellinger 	if (!dev_stat_grp->default_groups) {
28506708bb27SAndy Grover 		pr_err("Unable to allocate dev_stat_grp->default_groups\n");
28510fd97ccfSChristoph Hellwig 		goto out_free_tg_pt_gp_cg_default_groups;
285212d23384SNicholas Bellinger 	}
28530fd97ccfSChristoph Hellwig 	target_stat_setup_dev_default_groups(dev);
2854c66ac9dbSNicholas Bellinger 
2855c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
28560fd97ccfSChristoph Hellwig 	return dev_cg;
28570fd97ccfSChristoph Hellwig 
28580fd97ccfSChristoph Hellwig out_free_tg_pt_gp_cg_default_groups:
2859c66ac9dbSNicholas Bellinger 	kfree(tg_pt_gp_cg->default_groups);
28600fd97ccfSChristoph Hellwig out_free_tg_pt_gp:
28610fd97ccfSChristoph Hellwig 	core_alua_free_tg_pt_gp(tg_pt_gp);
28620fd97ccfSChristoph Hellwig out_free_dev_cg_default_groups:
2863c66ac9dbSNicholas Bellinger 	kfree(dev_cg->default_groups);
28640fd97ccfSChristoph Hellwig out_free_device:
28650fd97ccfSChristoph Hellwig 	target_free_device(dev);
28660fd97ccfSChristoph Hellwig out_unlock:
2867c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
286812d23384SNicholas Bellinger 	return ERR_PTR(errno);
2869c66ac9dbSNicholas Bellinger }
2870c66ac9dbSNicholas Bellinger 
2871c66ac9dbSNicholas Bellinger static void target_core_drop_subdev(
2872c66ac9dbSNicholas Bellinger 	struct config_group *group,
2873c66ac9dbSNicholas Bellinger 	struct config_item *item)
2874c66ac9dbSNicholas Bellinger {
28750fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
28760fd97ccfSChristoph Hellwig 	struct se_device *dev =
28770fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2878c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2879c66ac9dbSNicholas Bellinger 	struct config_item *df_item;
28800fd97ccfSChristoph Hellwig 	struct config_group *tg_pt_gp_cg, *dev_stat_grp;
28811f6fe7cbSNicholas Bellinger 	int i;
2882c66ac9dbSNicholas Bellinger 
28830fd97ccfSChristoph Hellwig 	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
2884c66ac9dbSNicholas Bellinger 
28851f6fe7cbSNicholas Bellinger 	mutex_lock(&hba->hba_access_mutex);
2886c66ac9dbSNicholas Bellinger 
28870fd97ccfSChristoph Hellwig 	dev_stat_grp = &dev->dev_stat_grps.stat_group;
288812d23384SNicholas Bellinger 	for (i = 0; dev_stat_grp->default_groups[i]; i++) {
288912d23384SNicholas Bellinger 		df_item = &dev_stat_grp->default_groups[i]->cg_item;
289012d23384SNicholas Bellinger 		dev_stat_grp->default_groups[i] = NULL;
289112d23384SNicholas Bellinger 		config_item_put(df_item);
289212d23384SNicholas Bellinger 	}
289312d23384SNicholas Bellinger 	kfree(dev_stat_grp->default_groups);
289412d23384SNicholas Bellinger 
28950fd97ccfSChristoph Hellwig 	tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2896c66ac9dbSNicholas Bellinger 	for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2897c66ac9dbSNicholas Bellinger 		df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2898c66ac9dbSNicholas Bellinger 		tg_pt_gp_cg->default_groups[i] = NULL;
2899c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
2900c66ac9dbSNicholas Bellinger 	}
2901c66ac9dbSNicholas Bellinger 	kfree(tg_pt_gp_cg->default_groups);
29021f6fe7cbSNicholas Bellinger 	/*
29031f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
29041f6fe7cbSNicholas Bellinger 	 * directly from target_core_alua_tg_pt_gp_release().
29051f6fe7cbSNicholas Bellinger 	 */
29060fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = NULL;
2907c66ac9dbSNicholas Bellinger 
2908c66ac9dbSNicholas Bellinger 	for (i = 0; dev_cg->default_groups[i]; i++) {
2909c66ac9dbSNicholas Bellinger 		df_item = &dev_cg->default_groups[i]->cg_item;
2910c66ac9dbSNicholas Bellinger 		dev_cg->default_groups[i] = NULL;
2911c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
2912c66ac9dbSNicholas Bellinger 	}
29131f6fe7cbSNicholas Bellinger 	/*
29140fd97ccfSChristoph Hellwig 	 * se_dev is released from target_core_dev_item_ops->release()
29151f6fe7cbSNicholas Bellinger 	 */
2916c66ac9dbSNicholas Bellinger 	config_item_put(item);
2917c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
2918c66ac9dbSNicholas Bellinger }
2919c66ac9dbSNicholas Bellinger 
2920c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_hba_group_ops = {
2921c66ac9dbSNicholas Bellinger 	.make_group		= target_core_make_subdev,
2922c66ac9dbSNicholas Bellinger 	.drop_item		= target_core_drop_subdev,
2923c66ac9dbSNicholas Bellinger };
2924c66ac9dbSNicholas Bellinger 
2925c66ac9dbSNicholas Bellinger 
29262eafd729SChristoph Hellwig static inline struct se_hba *to_hba(struct config_item *item)
2927c66ac9dbSNicholas Bellinger {
29282eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_hba, hba_group);
29292eafd729SChristoph Hellwig }
29302eafd729SChristoph Hellwig 
29312eafd729SChristoph Hellwig static ssize_t target_hba_info_show(struct config_item *item, char *page)
29322eafd729SChristoph Hellwig {
29332eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
29342eafd729SChristoph Hellwig 
2935c66ac9dbSNicholas Bellinger 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
29360a06d430SChristoph Hellwig 			hba->hba_id, hba->backend->ops->name,
2937ce8dd25dSChristoph Hellwig 			TARGET_CORE_VERSION);
2938c66ac9dbSNicholas Bellinger }
2939c66ac9dbSNicholas Bellinger 
29402eafd729SChristoph Hellwig static ssize_t target_hba_mode_show(struct config_item *item, char *page)
2941c66ac9dbSNicholas Bellinger {
29422eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
2943c66ac9dbSNicholas Bellinger 	int hba_mode = 0;
2944c66ac9dbSNicholas Bellinger 
2945c66ac9dbSNicholas Bellinger 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2946c66ac9dbSNicholas Bellinger 		hba_mode = 1;
2947c66ac9dbSNicholas Bellinger 
2948c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", hba_mode);
2949c66ac9dbSNicholas Bellinger }
2950c66ac9dbSNicholas Bellinger 
29512eafd729SChristoph Hellwig static ssize_t target_hba_mode_store(struct config_item *item,
2952c66ac9dbSNicholas Bellinger 		const char *page, size_t count)
2953c66ac9dbSNicholas Bellinger {
29542eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
2955c66ac9dbSNicholas Bellinger 	unsigned long mode_flag;
2956c66ac9dbSNicholas Bellinger 	int ret;
2957c66ac9dbSNicholas Bellinger 
29580a06d430SChristoph Hellwig 	if (hba->backend->ops->pmode_enable_hba == NULL)
2959c66ac9dbSNicholas Bellinger 		return -EINVAL;
2960c66ac9dbSNicholas Bellinger 
296157103d7fSJingoo Han 	ret = kstrtoul(page, 0, &mode_flag);
2962c66ac9dbSNicholas Bellinger 	if (ret < 0) {
29636708bb27SAndy Grover 		pr_err("Unable to extract hba mode flag: %d\n", ret);
296457103d7fSJingoo Han 		return ret;
2965c66ac9dbSNicholas Bellinger 	}
2966c66ac9dbSNicholas Bellinger 
29670fd97ccfSChristoph Hellwig 	if (hba->dev_count) {
29686708bb27SAndy Grover 		pr_err("Unable to set hba_mode with active devices\n");
2969c66ac9dbSNicholas Bellinger 		return -EINVAL;
2970c66ac9dbSNicholas Bellinger 	}
2971c66ac9dbSNicholas Bellinger 
29720a06d430SChristoph Hellwig 	ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
2973c66ac9dbSNicholas Bellinger 	if (ret < 0)
2974c66ac9dbSNicholas Bellinger 		return -EINVAL;
2975c66ac9dbSNicholas Bellinger 	if (ret > 0)
2976c66ac9dbSNicholas Bellinger 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2977c66ac9dbSNicholas Bellinger 	else if (ret == 0)
2978c66ac9dbSNicholas Bellinger 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2979c66ac9dbSNicholas Bellinger 
2980c66ac9dbSNicholas Bellinger 	return count;
2981c66ac9dbSNicholas Bellinger }
2982c66ac9dbSNicholas Bellinger 
29832eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_, hba_info);
29842eafd729SChristoph Hellwig CONFIGFS_ATTR(target_, hba_mode);
2985c66ac9dbSNicholas Bellinger 
29861f6fe7cbSNicholas Bellinger static void target_core_hba_release(struct config_item *item)
29871f6fe7cbSNicholas Bellinger {
29881f6fe7cbSNicholas Bellinger 	struct se_hba *hba = container_of(to_config_group(item),
29891f6fe7cbSNicholas Bellinger 				struct se_hba, hba_group);
29901f6fe7cbSNicholas Bellinger 	core_delete_hba(hba);
29911f6fe7cbSNicholas Bellinger }
29921f6fe7cbSNicholas Bellinger 
2993c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_hba_attrs[] = {
29942eafd729SChristoph Hellwig 	&target_attr_hba_info,
29952eafd729SChristoph Hellwig 	&target_attr_hba_mode,
2996c66ac9dbSNicholas Bellinger 	NULL,
2997c66ac9dbSNicholas Bellinger };
2998c66ac9dbSNicholas Bellinger 
2999c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_hba_item_ops = {
30001f6fe7cbSNicholas Bellinger 	.release		= target_core_hba_release,
3001c66ac9dbSNicholas Bellinger };
3002c66ac9dbSNicholas Bellinger 
3003c66ac9dbSNicholas Bellinger static struct config_item_type target_core_hba_cit = {
3004c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_hba_item_ops,
3005c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_hba_group_ops,
3006c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_hba_attrs,
3007c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3008c66ac9dbSNicholas Bellinger };
3009c66ac9dbSNicholas Bellinger 
3010c66ac9dbSNicholas Bellinger static struct config_group *target_core_call_addhbatotarget(
3011c66ac9dbSNicholas Bellinger 	struct config_group *group,
3012c66ac9dbSNicholas Bellinger 	const char *name)
3013c66ac9dbSNicholas Bellinger {
3014c66ac9dbSNicholas Bellinger 	char *se_plugin_str, *str, *str2;
3015c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
3016c66ac9dbSNicholas Bellinger 	char buf[TARGET_CORE_NAME_MAX_LEN];
3017c66ac9dbSNicholas Bellinger 	unsigned long plugin_dep_id = 0;
3018c66ac9dbSNicholas Bellinger 	int ret;
3019c66ac9dbSNicholas Bellinger 
3020c66ac9dbSNicholas Bellinger 	memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
302160d645a4SDan Carpenter 	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
30226708bb27SAndy Grover 		pr_err("Passed *name strlen(): %d exceeds"
3023c66ac9dbSNicholas Bellinger 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3024c66ac9dbSNicholas Bellinger 			TARGET_CORE_NAME_MAX_LEN);
3025c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENAMETOOLONG);
3026c66ac9dbSNicholas Bellinger 	}
3027c66ac9dbSNicholas Bellinger 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3028c66ac9dbSNicholas Bellinger 
3029c66ac9dbSNicholas Bellinger 	str = strstr(buf, "_");
30306708bb27SAndy Grover 	if (!str) {
30316708bb27SAndy Grover 		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3032c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
3033c66ac9dbSNicholas Bellinger 	}
3034c66ac9dbSNicholas Bellinger 	se_plugin_str = buf;
3035c66ac9dbSNicholas Bellinger 	/*
3036c66ac9dbSNicholas Bellinger 	 * Special case for subsystem plugins that have "_" in their names.
3037c66ac9dbSNicholas Bellinger 	 * Namely rd_direct and rd_mcp..
3038c66ac9dbSNicholas Bellinger 	 */
3039c66ac9dbSNicholas Bellinger 	str2 = strstr(str+1, "_");
30406708bb27SAndy Grover 	if (str2) {
3041c66ac9dbSNicholas Bellinger 		*str2 = '\0'; /* Terminate for *se_plugin_str */
3042c66ac9dbSNicholas Bellinger 		str2++; /* Skip to start of plugin dependent ID */
3043c66ac9dbSNicholas Bellinger 		str = str2;
3044c66ac9dbSNicholas Bellinger 	} else {
3045c66ac9dbSNicholas Bellinger 		*str = '\0'; /* Terminate for *se_plugin_str */
3046c66ac9dbSNicholas Bellinger 		str++; /* Skip to start of plugin dependent ID */
3047c66ac9dbSNicholas Bellinger 	}
3048c66ac9dbSNicholas Bellinger 
304957103d7fSJingoo Han 	ret = kstrtoul(str, 0, &plugin_dep_id);
3050c66ac9dbSNicholas Bellinger 	if (ret < 0) {
305157103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
3052c66ac9dbSNicholas Bellinger 				" plugin_dep_id\n", ret);
305357103d7fSJingoo Han 		return ERR_PTR(ret);
3054c66ac9dbSNicholas Bellinger 	}
3055c66ac9dbSNicholas Bellinger 	/*
3056c66ac9dbSNicholas Bellinger 	 * Load up TCM subsystem plugins if they have not already been loaded.
3057c66ac9dbSNicholas Bellinger 	 */
3058dbc5623eSNicholas Bellinger 	transport_subsystem_check_init();
3059c66ac9dbSNicholas Bellinger 
3060c66ac9dbSNicholas Bellinger 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3061c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
3062c66ac9dbSNicholas Bellinger 		return ERR_CAST(hba);
3063c66ac9dbSNicholas Bellinger 
3064c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&hba->hba_group, name,
3065c66ac9dbSNicholas Bellinger 			&target_core_hba_cit);
3066c66ac9dbSNicholas Bellinger 
3067c66ac9dbSNicholas Bellinger 	return &hba->hba_group;
3068c66ac9dbSNicholas Bellinger }
3069c66ac9dbSNicholas Bellinger 
3070c66ac9dbSNicholas Bellinger static void target_core_call_delhbafromtarget(
3071c66ac9dbSNicholas Bellinger 	struct config_group *group,
3072c66ac9dbSNicholas Bellinger 	struct config_item *item)
3073c66ac9dbSNicholas Bellinger {
30741f6fe7cbSNicholas Bellinger 	/*
30751f6fe7cbSNicholas Bellinger 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
30761f6fe7cbSNicholas Bellinger 	 * -> target_core_hba_release()
30771f6fe7cbSNicholas Bellinger 	 */
3078c66ac9dbSNicholas Bellinger 	config_item_put(item);
3079c66ac9dbSNicholas Bellinger }
3080c66ac9dbSNicholas Bellinger 
3081c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_group_ops = {
3082c66ac9dbSNicholas Bellinger 	.make_group	= target_core_call_addhbatotarget,
3083c66ac9dbSNicholas Bellinger 	.drop_item	= target_core_call_delhbafromtarget,
3084c66ac9dbSNicholas Bellinger };
3085c66ac9dbSNicholas Bellinger 
3086c66ac9dbSNicholas Bellinger static struct config_item_type target_core_cit = {
3087c66ac9dbSNicholas Bellinger 	.ct_item_ops	= NULL,
3088c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_group_ops,
3089c66ac9dbSNicholas Bellinger 	.ct_attrs	= NULL,
3090c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
3091c66ac9dbSNicholas Bellinger };
3092c66ac9dbSNicholas Bellinger 
3093c66ac9dbSNicholas Bellinger /* Stop functions for struct config_item_type target_core_hba_cit */
3094c66ac9dbSNicholas Bellinger 
30950a06d430SChristoph Hellwig void target_setup_backend_cits(struct target_backend *tb)
309673112edcSNicholas Bellinger {
30970a06d430SChristoph Hellwig 	target_core_setup_dev_cit(tb);
30980a06d430SChristoph Hellwig 	target_core_setup_dev_attrib_cit(tb);
30990a06d430SChristoph Hellwig 	target_core_setup_dev_pr_cit(tb);
31000a06d430SChristoph Hellwig 	target_core_setup_dev_wwn_cit(tb);
31010a06d430SChristoph Hellwig 	target_core_setup_dev_alua_tg_pt_gps_cit(tb);
31020a06d430SChristoph Hellwig 	target_core_setup_dev_stat_cit(tb);
310373112edcSNicholas Bellinger }
310473112edcSNicholas Bellinger 
310554550fabSAxel Lin static int __init target_core_init_configfs(void)
3106c66ac9dbSNicholas Bellinger {
3107c66ac9dbSNicholas Bellinger 	struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3108c66ac9dbSNicholas Bellinger 	struct config_group *lu_gp_cg = NULL;
3109d588cf8fSChristoph Hellwig 	struct configfs_subsystem *subsys = &target_core_fabrics;
3110c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
3111c66ac9dbSNicholas Bellinger 	int ret;
3112c66ac9dbSNicholas Bellinger 
31136708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3114c66ac9dbSNicholas Bellinger 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
3115c66ac9dbSNicholas Bellinger 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3116c66ac9dbSNicholas Bellinger 
3117c66ac9dbSNicholas Bellinger 	config_group_init(&subsys->su_group);
3118c66ac9dbSNicholas Bellinger 	mutex_init(&subsys->su_mutex);
3119c66ac9dbSNicholas Bellinger 
3120e3d6f909SAndy Grover 	ret = init_se_kmem_caches();
3121c66ac9dbSNicholas Bellinger 	if (ret < 0)
3122e3d6f909SAndy Grover 		return ret;
3123c66ac9dbSNicholas Bellinger 	/*
3124c66ac9dbSNicholas Bellinger 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3125c66ac9dbSNicholas Bellinger 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3126c66ac9dbSNicholas Bellinger 	 */
3127c66ac9dbSNicholas Bellinger 	target_cg = &subsys->su_group;
3128ab6dae82SAndy Grover 	target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3129c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
31306708bb27SAndy Grover 	if (!target_cg->default_groups) {
31316708bb27SAndy Grover 		pr_err("Unable to allocate target_cg->default_groups\n");
313237bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3133c66ac9dbSNicholas Bellinger 		goto out_global;
3134c66ac9dbSNicholas Bellinger 	}
3135c66ac9dbSNicholas Bellinger 
3136e3d6f909SAndy Grover 	config_group_init_type_name(&target_core_hbagroup,
3137c66ac9dbSNicholas Bellinger 			"core", &target_core_cit);
3138e3d6f909SAndy Grover 	target_cg->default_groups[0] = &target_core_hbagroup;
3139c66ac9dbSNicholas Bellinger 	target_cg->default_groups[1] = NULL;
3140c66ac9dbSNicholas Bellinger 	/*
3141c66ac9dbSNicholas Bellinger 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3142c66ac9dbSNicholas Bellinger 	 */
3143e3d6f909SAndy Grover 	hba_cg = &target_core_hbagroup;
314413f6a914SSebastian Andrzej Siewior 	hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3145c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
31466708bb27SAndy Grover 	if (!hba_cg->default_groups) {
31476708bb27SAndy Grover 		pr_err("Unable to allocate hba_cg->default_groups\n");
314837bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3149c66ac9dbSNicholas Bellinger 		goto out_global;
3150c66ac9dbSNicholas Bellinger 	}
3151e3d6f909SAndy Grover 	config_group_init_type_name(&alua_group,
3152c66ac9dbSNicholas Bellinger 			"alua", &target_core_alua_cit);
3153e3d6f909SAndy Grover 	hba_cg->default_groups[0] = &alua_group;
3154c66ac9dbSNicholas Bellinger 	hba_cg->default_groups[1] = NULL;
3155c66ac9dbSNicholas Bellinger 	/*
3156c66ac9dbSNicholas Bellinger 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3157c66ac9dbSNicholas Bellinger 	 * groups under /sys/kernel/config/target/core/alua/
3158c66ac9dbSNicholas Bellinger 	 */
3159e3d6f909SAndy Grover 	alua_cg = &alua_group;
316013f6a914SSebastian Andrzej Siewior 	alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3161c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
31626708bb27SAndy Grover 	if (!alua_cg->default_groups) {
31636708bb27SAndy Grover 		pr_err("Unable to allocate alua_cg->default_groups\n");
316437bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3165c66ac9dbSNicholas Bellinger 		goto out_global;
3166c66ac9dbSNicholas Bellinger 	}
3167c66ac9dbSNicholas Bellinger 
3168e3d6f909SAndy Grover 	config_group_init_type_name(&alua_lu_gps_group,
3169c66ac9dbSNicholas Bellinger 			"lu_gps", &target_core_alua_lu_gps_cit);
3170e3d6f909SAndy Grover 	alua_cg->default_groups[0] = &alua_lu_gps_group;
3171c66ac9dbSNicholas Bellinger 	alua_cg->default_groups[1] = NULL;
3172c66ac9dbSNicholas Bellinger 	/*
3173c66ac9dbSNicholas Bellinger 	 * Add core/alua/lu_gps/default_lu_gp
3174c66ac9dbSNicholas Bellinger 	 */
3175c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
317637bb7899SPeter Senna Tschudin 	if (IS_ERR(lu_gp)) {
317737bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3178c66ac9dbSNicholas Bellinger 		goto out_global;
317937bb7899SPeter Senna Tschudin 	}
3180c66ac9dbSNicholas Bellinger 
3181e3d6f909SAndy Grover 	lu_gp_cg = &alua_lu_gps_group;
318213f6a914SSebastian Andrzej Siewior 	lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3183c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
31846708bb27SAndy Grover 	if (!lu_gp_cg->default_groups) {
31856708bb27SAndy Grover 		pr_err("Unable to allocate lu_gp_cg->default_groups\n");
318637bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3187c66ac9dbSNicholas Bellinger 		goto out_global;
3188c66ac9dbSNicholas Bellinger 	}
3189c66ac9dbSNicholas Bellinger 
3190c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3191c66ac9dbSNicholas Bellinger 				&target_core_alua_lu_gp_cit);
3192c66ac9dbSNicholas Bellinger 	lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3193c66ac9dbSNicholas Bellinger 	lu_gp_cg->default_groups[1] = NULL;
3194e3d6f909SAndy Grover 	default_lu_gp = lu_gp;
3195c66ac9dbSNicholas Bellinger 	/*
3196c66ac9dbSNicholas Bellinger 	 * Register the target_core_mod subsystem with configfs.
3197c66ac9dbSNicholas Bellinger 	 */
3198c66ac9dbSNicholas Bellinger 	ret = configfs_register_subsystem(subsys);
3199c66ac9dbSNicholas Bellinger 	if (ret < 0) {
32006708bb27SAndy Grover 		pr_err("Error %d while registering subsystem %s\n",
3201c66ac9dbSNicholas Bellinger 			ret, subsys->su_group.cg_item.ci_namebuf);
3202c66ac9dbSNicholas Bellinger 		goto out_global;
3203c66ac9dbSNicholas Bellinger 	}
32046708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3205ce8dd25dSChristoph Hellwig 		" Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3206c66ac9dbSNicholas Bellinger 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3207c66ac9dbSNicholas Bellinger 	/*
3208c66ac9dbSNicholas Bellinger 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3209c66ac9dbSNicholas Bellinger 	 */
3210c66ac9dbSNicholas Bellinger 	ret = rd_module_init();
3211c66ac9dbSNicholas Bellinger 	if (ret < 0)
3212c66ac9dbSNicholas Bellinger 		goto out;
3213c66ac9dbSNicholas Bellinger 
32140d0f9dfbSRoland Dreier 	ret = core_dev_setup_virtual_lun0();
32150d0f9dfbSRoland Dreier 	if (ret < 0)
3216c66ac9dbSNicholas Bellinger 		goto out;
3217c66ac9dbSNicholas Bellinger 
3218f99715acSNicholas Bellinger 	ret = target_xcopy_setup_pt();
3219f99715acSNicholas Bellinger 	if (ret < 0)
3220f99715acSNicholas Bellinger 		goto out;
3221f99715acSNicholas Bellinger 
3222c66ac9dbSNicholas Bellinger 	return 0;
3223c66ac9dbSNicholas Bellinger 
3224c66ac9dbSNicholas Bellinger out:
3225c66ac9dbSNicholas Bellinger 	configfs_unregister_subsystem(subsys);
3226c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3227c66ac9dbSNicholas Bellinger 	rd_module_exit();
3228c66ac9dbSNicholas Bellinger out_global:
3229e3d6f909SAndy Grover 	if (default_lu_gp) {
3230e3d6f909SAndy Grover 		core_alua_free_lu_gp(default_lu_gp);
3231e3d6f909SAndy Grover 		default_lu_gp = NULL;
3232c66ac9dbSNicholas Bellinger 	}
3233c66ac9dbSNicholas Bellinger 	if (lu_gp_cg)
3234c66ac9dbSNicholas Bellinger 		kfree(lu_gp_cg->default_groups);
3235c66ac9dbSNicholas Bellinger 	if (alua_cg)
3236c66ac9dbSNicholas Bellinger 		kfree(alua_cg->default_groups);
3237c66ac9dbSNicholas Bellinger 	if (hba_cg)
3238c66ac9dbSNicholas Bellinger 		kfree(hba_cg->default_groups);
3239c66ac9dbSNicholas Bellinger 	kfree(target_cg->default_groups);
3240e3d6f909SAndy Grover 	release_se_kmem_caches();
3241e3d6f909SAndy Grover 	return ret;
3242c66ac9dbSNicholas Bellinger }
3243c66ac9dbSNicholas Bellinger 
324454550fabSAxel Lin static void __exit target_core_exit_configfs(void)
3245c66ac9dbSNicholas Bellinger {
3246c66ac9dbSNicholas Bellinger 	struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3247c66ac9dbSNicholas Bellinger 	struct config_item *item;
3248c66ac9dbSNicholas Bellinger 	int i;
3249c66ac9dbSNicholas Bellinger 
3250e3d6f909SAndy Grover 	lu_gp_cg = &alua_lu_gps_group;
3251c66ac9dbSNicholas Bellinger 	for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3252c66ac9dbSNicholas Bellinger 		item = &lu_gp_cg->default_groups[i]->cg_item;
3253c66ac9dbSNicholas Bellinger 		lu_gp_cg->default_groups[i] = NULL;
3254c66ac9dbSNicholas Bellinger 		config_item_put(item);
3255c66ac9dbSNicholas Bellinger 	}
3256c66ac9dbSNicholas Bellinger 	kfree(lu_gp_cg->default_groups);
32577c2bf6e9SNicholas Bellinger 	lu_gp_cg->default_groups = NULL;
3258c66ac9dbSNicholas Bellinger 
3259e3d6f909SAndy Grover 	alua_cg = &alua_group;
3260c66ac9dbSNicholas Bellinger 	for (i = 0; alua_cg->default_groups[i]; i++) {
3261c66ac9dbSNicholas Bellinger 		item = &alua_cg->default_groups[i]->cg_item;
3262c66ac9dbSNicholas Bellinger 		alua_cg->default_groups[i] = NULL;
3263c66ac9dbSNicholas Bellinger 		config_item_put(item);
3264c66ac9dbSNicholas Bellinger 	}
3265c66ac9dbSNicholas Bellinger 	kfree(alua_cg->default_groups);
32667c2bf6e9SNicholas Bellinger 	alua_cg->default_groups = NULL;
3267c66ac9dbSNicholas Bellinger 
3268e3d6f909SAndy Grover 	hba_cg = &target_core_hbagroup;
3269c66ac9dbSNicholas Bellinger 	for (i = 0; hba_cg->default_groups[i]; i++) {
3270c66ac9dbSNicholas Bellinger 		item = &hba_cg->default_groups[i]->cg_item;
3271c66ac9dbSNicholas Bellinger 		hba_cg->default_groups[i] = NULL;
3272c66ac9dbSNicholas Bellinger 		config_item_put(item);
3273c66ac9dbSNicholas Bellinger 	}
3274c66ac9dbSNicholas Bellinger 	kfree(hba_cg->default_groups);
32757c2bf6e9SNicholas Bellinger 	hba_cg->default_groups = NULL;
32767c2bf6e9SNicholas Bellinger 	/*
32777c2bf6e9SNicholas Bellinger 	 * We expect subsys->su_group.default_groups to be released
32787c2bf6e9SNicholas Bellinger 	 * by configfs subsystem provider logic..
32797c2bf6e9SNicholas Bellinger 	 */
3280d588cf8fSChristoph Hellwig 	configfs_unregister_subsystem(&target_core_fabrics);
3281d588cf8fSChristoph Hellwig 	kfree(target_core_fabrics.su_group.default_groups);
3282c66ac9dbSNicholas Bellinger 
3283e3d6f909SAndy Grover 	core_alua_free_lu_gp(default_lu_gp);
3284e3d6f909SAndy Grover 	default_lu_gp = NULL;
32857c2bf6e9SNicholas Bellinger 
32866708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3287c66ac9dbSNicholas Bellinger 			" Infrastructure\n");
3288c66ac9dbSNicholas Bellinger 
3289c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3290c66ac9dbSNicholas Bellinger 	rd_module_exit();
3291f99715acSNicholas Bellinger 	target_xcopy_release_pt();
3292e3d6f909SAndy Grover 	release_se_kmem_caches();
3293c66ac9dbSNicholas Bellinger }
3294c66ac9dbSNicholas Bellinger 
3295c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3296c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3297c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
3298c66ac9dbSNicholas Bellinger 
3299c66ac9dbSNicholas Bellinger module_init(target_core_init_configfs);
3300c66ac9dbSNicholas Bellinger module_exit(target_core_exit_configfs);
3301