1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c66ac9dbSNicholas Bellinger /*******************************************************************************
3c66ac9dbSNicholas Bellinger  * Filename:  target_core_configfs.c
4c66ac9dbSNicholas Bellinger  *
5c66ac9dbSNicholas Bellinger  * This file contains ConfigFS logic for the Generic Target Engine project.
6c66ac9dbSNicholas Bellinger  *
74c76251eSNicholas Bellinger  * (c) Copyright 2008-2013 Datera, Inc.
8c66ac9dbSNicholas Bellinger  *
9c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
10c66ac9dbSNicholas Bellinger  *
11c66ac9dbSNicholas Bellinger  * based on configfs Copyright (C) 2005 Oracle.  All rights reserved.
12c66ac9dbSNicholas Bellinger  *
13c66ac9dbSNicholas Bellinger  ****************************************************************************/
14c66ac9dbSNicholas Bellinger 
15c66ac9dbSNicholas Bellinger #include <linux/module.h>
16c66ac9dbSNicholas Bellinger #include <linux/moduleparam.h>
17c66ac9dbSNicholas Bellinger #include <generated/utsrelease.h>
18c66ac9dbSNicholas Bellinger #include <linux/utsname.h>
19c66ac9dbSNicholas Bellinger #include <linux/init.h>
20c66ac9dbSNicholas Bellinger #include <linux/fs.h>
21c66ac9dbSNicholas Bellinger #include <linux/namei.h>
22c66ac9dbSNicholas Bellinger #include <linux/slab.h>
23c66ac9dbSNicholas Bellinger #include <linux/types.h>
24c66ac9dbSNicholas Bellinger #include <linux/delay.h>
25c66ac9dbSNicholas Bellinger #include <linux/unistd.h>
26c66ac9dbSNicholas Bellinger #include <linux/string.h>
27c66ac9dbSNicholas Bellinger #include <linux/parser.h>
28c66ac9dbSNicholas Bellinger #include <linux/syscalls.h>
29c66ac9dbSNicholas Bellinger #include <linux/configfs.h>
30e3d6f909SAndy Grover #include <linux/spinlock.h>
31c66ac9dbSNicholas Bellinger 
32c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
33c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
34c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
35c66ac9dbSNicholas Bellinger 
36e26d99aeSChristoph Hellwig #include "target_core_internal.h"
37c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
38c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
39c66ac9dbSNicholas Bellinger #include "target_core_rd.h"
40f99715acSNicholas Bellinger #include "target_core_xcopy.h"
41c66ac9dbSNicholas Bellinger 
4273112edcSNicholas Bellinger #define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs)		\
430a06d430SChristoph Hellwig static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
4473112edcSNicholas Bellinger {									\
450a06d430SChristoph Hellwig 	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
4673112edcSNicholas Bellinger 									\
4773112edcSNicholas Bellinger 	cit->ct_item_ops = _item_ops;					\
4873112edcSNicholas Bellinger 	cit->ct_group_ops = _group_ops;					\
4973112edcSNicholas Bellinger 	cit->ct_attrs = _attrs;						\
500a06d430SChristoph Hellwig 	cit->ct_owner = tb->ops->owner;					\
510a06d430SChristoph Hellwig 	pr_debug("Setup generic %s\n", __stringify(_name));		\
520a06d430SChristoph Hellwig }
530a06d430SChristoph Hellwig 
540a06d430SChristoph Hellwig #define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops)			\
550a06d430SChristoph Hellwig static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
560a06d430SChristoph Hellwig {									\
570a06d430SChristoph Hellwig 	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
580a06d430SChristoph Hellwig 									\
590a06d430SChristoph Hellwig 	cit->ct_item_ops = _item_ops;					\
600a06d430SChristoph Hellwig 	cit->ct_group_ops = _group_ops;					\
610a06d430SChristoph Hellwig 	cit->ct_attrs = tb->ops->tb_##_name##_attrs;			\
620a06d430SChristoph Hellwig 	cit->ct_owner = tb->ops->owner;					\
6373112edcSNicholas Bellinger 	pr_debug("Setup generic %s\n", __stringify(_name));		\
6473112edcSNicholas Bellinger }
6573112edcSNicholas Bellinger 
66e3d6f909SAndy Grover extern struct t10_alua_lu_gp *default_lu_gp;
67e3d6f909SAndy Grover 
68d0f474e5SRoland Dreier static LIST_HEAD(g_tf_list);
69d0f474e5SRoland Dreier static DEFINE_MUTEX(g_tf_lock);
70c66ac9dbSNicholas Bellinger 
71e3d6f909SAndy Grover static struct config_group target_core_hbagroup;
72e3d6f909SAndy Grover static struct config_group alua_group;
73e3d6f909SAndy Grover static struct config_group alua_lu_gps_group;
74e3d6f909SAndy Grover 
75c66ac9dbSNicholas Bellinger static inline struct se_hba *
76c66ac9dbSNicholas Bellinger item_to_hba(struct config_item *item)
77c66ac9dbSNicholas Bellinger {
78c66ac9dbSNicholas Bellinger 	return container_of(to_config_group(item), struct se_hba, hba_group);
79c66ac9dbSNicholas Bellinger }
80c66ac9dbSNicholas Bellinger 
81c66ac9dbSNicholas Bellinger /*
82c66ac9dbSNicholas Bellinger  * Attributes for /sys/kernel/config/target/
83c66ac9dbSNicholas Bellinger  */
842eafd729SChristoph Hellwig static ssize_t target_core_item_version_show(struct config_item *item,
85c66ac9dbSNicholas Bellinger 		char *page)
86c66ac9dbSNicholas Bellinger {
87c66ac9dbSNicholas Bellinger 	return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
88ce8dd25dSChristoph Hellwig 		" on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
89c66ac9dbSNicholas Bellinger 		utsname()->sysname, utsname()->machine);
90c66ac9dbSNicholas Bellinger }
91c66ac9dbSNicholas Bellinger 
922eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_core_item_, version);
93c66ac9dbSNicholas Bellinger 
94a96e9783SLee Duncan char db_root[DB_ROOT_LEN] = DB_ROOT_DEFAULT;
95a96e9783SLee Duncan static char db_root_stage[DB_ROOT_LEN];
96a96e9783SLee Duncan 
97a96e9783SLee Duncan static ssize_t target_core_item_dbroot_show(struct config_item *item,
98a96e9783SLee Duncan 					    char *page)
99a96e9783SLee Duncan {
100a96e9783SLee Duncan 	return sprintf(page, "%s\n", db_root);
101a96e9783SLee Duncan }
102a96e9783SLee Duncan 
103a96e9783SLee Duncan static ssize_t target_core_item_dbroot_store(struct config_item *item,
104a96e9783SLee Duncan 					const char *page, size_t count)
105a96e9783SLee Duncan {
106a96e9783SLee Duncan 	ssize_t read_bytes;
107a96e9783SLee Duncan 	struct file *fp;
108a96e9783SLee Duncan 
109a96e9783SLee Duncan 	mutex_lock(&g_tf_lock);
110a96e9783SLee Duncan 	if (!list_empty(&g_tf_list)) {
111a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
112a96e9783SLee Duncan 		pr_err("db_root: cannot be changed: target drivers registered");
113a96e9783SLee Duncan 		return -EINVAL;
114a96e9783SLee Duncan 	}
115a96e9783SLee Duncan 
116a96e9783SLee Duncan 	if (count > (DB_ROOT_LEN - 1)) {
117a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
118a96e9783SLee Duncan 		pr_err("db_root: count %d exceeds DB_ROOT_LEN-1: %u\n",
119a96e9783SLee Duncan 		       (int)count, DB_ROOT_LEN - 1);
120a96e9783SLee Duncan 		return -EINVAL;
121a96e9783SLee Duncan 	}
122a96e9783SLee Duncan 
123a96e9783SLee Duncan 	read_bytes = snprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
124a96e9783SLee Duncan 	if (!read_bytes) {
125a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
126a96e9783SLee Duncan 		return -EINVAL;
127a96e9783SLee Duncan 	}
128a96e9783SLee Duncan 	if (db_root_stage[read_bytes - 1] == '\n')
129a96e9783SLee Duncan 		db_root_stage[read_bytes - 1] = '\0';
130a96e9783SLee Duncan 
131a96e9783SLee Duncan 	/* validate new db root before accepting it */
132a96e9783SLee Duncan 	fp = filp_open(db_root_stage, O_RDONLY, 0);
133a96e9783SLee Duncan 	if (IS_ERR(fp)) {
134a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
135a96e9783SLee Duncan 		pr_err("db_root: cannot open: %s\n", db_root_stage);
136a96e9783SLee Duncan 		return -EINVAL;
137a96e9783SLee Duncan 	}
13845063097SAl Viro 	if (!S_ISDIR(file_inode(fp)->i_mode)) {
1398cc3bb07SBart Van Assche 		filp_close(fp, NULL);
140a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
141a96e9783SLee Duncan 		pr_err("db_root: not a directory: %s\n", db_root_stage);
142a96e9783SLee Duncan 		return -EINVAL;
143a96e9783SLee Duncan 	}
1448cc3bb07SBart Van Assche 	filp_close(fp, NULL);
145a96e9783SLee Duncan 
146a96e9783SLee Duncan 	strncpy(db_root, db_root_stage, read_bytes);
147a96e9783SLee Duncan 
148a96e9783SLee Duncan 	mutex_unlock(&g_tf_lock);
149a96e9783SLee Duncan 
15078a6295cSLee Duncan 	pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
15178a6295cSLee Duncan 
152a96e9783SLee Duncan 	return read_bytes;
153a96e9783SLee Duncan }
154a96e9783SLee Duncan 
155a96e9783SLee Duncan CONFIGFS_ATTR(target_core_item_, dbroot);
156a96e9783SLee Duncan 
157c66ac9dbSNicholas Bellinger static struct target_fabric_configfs *target_core_get_fabric(
158c66ac9dbSNicholas Bellinger 	const char *name)
159c66ac9dbSNicholas Bellinger {
160c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
161c66ac9dbSNicholas Bellinger 
1626708bb27SAndy Grover 	if (!name)
163c66ac9dbSNicholas Bellinger 		return NULL;
164c66ac9dbSNicholas Bellinger 
165c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
166c66ac9dbSNicholas Bellinger 	list_for_each_entry(tf, &g_tf_list, tf_list) {
16759a206b4SDavid Disseldorp 		const char *cmp_name = tf->tf_ops->fabric_alias;
16859a206b4SDavid Disseldorp 		if (!cmp_name)
16959a206b4SDavid Disseldorp 			cmp_name = tf->tf_ops->fabric_name;
17059a206b4SDavid Disseldorp 		if (!strcmp(cmp_name, name)) {
171c66ac9dbSNicholas Bellinger 			atomic_inc(&tf->tf_access_cnt);
172c66ac9dbSNicholas Bellinger 			mutex_unlock(&g_tf_lock);
173c66ac9dbSNicholas Bellinger 			return tf;
174c66ac9dbSNicholas Bellinger 		}
175c66ac9dbSNicholas Bellinger 	}
176c66ac9dbSNicholas Bellinger 	mutex_unlock(&g_tf_lock);
177c66ac9dbSNicholas Bellinger 
178c66ac9dbSNicholas Bellinger 	return NULL;
179c66ac9dbSNicholas Bellinger }
180c66ac9dbSNicholas Bellinger 
181c66ac9dbSNicholas Bellinger /*
182c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->make_group()
183c66ac9dbSNicholas Bellinger  */
184c66ac9dbSNicholas Bellinger static struct config_group *target_core_register_fabric(
185c66ac9dbSNicholas Bellinger 	struct config_group *group,
186c66ac9dbSNicholas Bellinger 	const char *name)
187c66ac9dbSNicholas Bellinger {
188c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
189c66ac9dbSNicholas Bellinger 	int ret;
190c66ac9dbSNicholas Bellinger 
1916708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
192c66ac9dbSNicholas Bellinger 			" %s\n", group, name);
193e7b7af6eSRoland Dreier 
194e7b7af6eSRoland Dreier 	tf = target_core_get_fabric(name);
195e7b7af6eSRoland Dreier 	if (!tf) {
19662554910SNicholas Bellinger 		pr_debug("target_core_register_fabric() trying autoload for %s\n",
197e7b7af6eSRoland Dreier 			 name);
198e7b7af6eSRoland Dreier 
199c66ac9dbSNicholas Bellinger 		/*
200c66ac9dbSNicholas Bellinger 		 * Below are some hardcoded request_module() calls to automatically
201c66ac9dbSNicholas Bellinger 		 * local fabric modules when the following is called:
202c66ac9dbSNicholas Bellinger 		 *
203c66ac9dbSNicholas Bellinger 		 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
204c66ac9dbSNicholas Bellinger 		 *
205c66ac9dbSNicholas Bellinger 		 * Note that this does not limit which TCM fabric module can be
206c66ac9dbSNicholas Bellinger 		 * registered, but simply provids auto loading logic for modules with
207c66ac9dbSNicholas Bellinger 		 * mkdir(2) system calls with known TCM fabric modules.
208c66ac9dbSNicholas Bellinger 		 */
209e7b7af6eSRoland Dreier 
2106708bb27SAndy Grover 		if (!strncmp(name, "iscsi", 5)) {
211c66ac9dbSNicholas Bellinger 			/*
212c66ac9dbSNicholas Bellinger 			 * Automatically load the LIO Target fabric module when the
213c66ac9dbSNicholas Bellinger 			 * following is called:
214c66ac9dbSNicholas Bellinger 			 *
215c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/iscsi
216c66ac9dbSNicholas Bellinger 			 */
217c66ac9dbSNicholas Bellinger 			ret = request_module("iscsi_target_mod");
218c66ac9dbSNicholas Bellinger 			if (ret < 0) {
21962554910SNicholas Bellinger 				pr_debug("request_module() failed for"
220c66ac9dbSNicholas Bellinger 				         " iscsi_target_mod.ko: %d\n", ret);
221c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
222c66ac9dbSNicholas Bellinger 			}
2236708bb27SAndy Grover 		} else if (!strncmp(name, "loopback", 8)) {
224c66ac9dbSNicholas Bellinger 			/*
225c66ac9dbSNicholas Bellinger 			 * Automatically load the tcm_loop fabric module when the
226c66ac9dbSNicholas Bellinger 			 * following is called:
227c66ac9dbSNicholas Bellinger 			 *
228c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/loopback
229c66ac9dbSNicholas Bellinger 			 */
230c66ac9dbSNicholas Bellinger 			ret = request_module("tcm_loop");
231c66ac9dbSNicholas Bellinger 			if (ret < 0) {
23262554910SNicholas Bellinger 				pr_debug("request_module() failed for"
233c66ac9dbSNicholas Bellinger 				         " tcm_loop.ko: %d\n", ret);
234c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
235c66ac9dbSNicholas Bellinger 			}
236c66ac9dbSNicholas Bellinger 		}
237c66ac9dbSNicholas Bellinger 
238c66ac9dbSNicholas Bellinger 		tf = target_core_get_fabric(name);
239e7b7af6eSRoland Dreier 	}
240e7b7af6eSRoland Dreier 
2416708bb27SAndy Grover 	if (!tf) {
24262554910SNicholas Bellinger 		pr_debug("target_core_get_fabric() failed for %s\n",
243c66ac9dbSNicholas Bellinger 		         name);
244c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
245c66ac9dbSNicholas Bellinger 	}
2466708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
24759a206b4SDavid Disseldorp 			" %s\n", tf->tf_ops->fabric_name);
248c66ac9dbSNicholas Bellinger 	/*
249c66ac9dbSNicholas Bellinger 	 * On a successful target_core_get_fabric() look, the returned
250c66ac9dbSNicholas Bellinger 	 * struct target_fabric_configfs *tf will contain a usage reference.
251c66ac9dbSNicholas Bellinger 	 */
2526708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
253968ebe75SChristoph Hellwig 			&tf->tf_wwn_cit);
254c66ac9dbSNicholas Bellinger 
255968ebe75SChristoph Hellwig 	config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
2561ae1602dSChristoph Hellwig 
257c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
258968ebe75SChristoph Hellwig 			&tf->tf_discovery_cit);
2591ae1602dSChristoph Hellwig 	configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
260c66ac9dbSNicholas Bellinger 
2616f3bf5a2SBart Van Assche 	pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric: %s\n",
2626f3bf5a2SBart Van Assche 		 config_item_name(&tf->tf_group.cg_item));
263c66ac9dbSNicholas Bellinger 	return &tf->tf_group;
264c66ac9dbSNicholas Bellinger }
265c66ac9dbSNicholas Bellinger 
266c66ac9dbSNicholas Bellinger /*
267c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->drop_item()
268c66ac9dbSNicholas Bellinger  */
269c66ac9dbSNicholas Bellinger static void target_core_deregister_fabric(
270c66ac9dbSNicholas Bellinger 	struct config_group *group,
271c66ac9dbSNicholas Bellinger 	struct config_item *item)
272c66ac9dbSNicholas Bellinger {
273c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf = container_of(
274c66ac9dbSNicholas Bellinger 		to_config_group(item), struct target_fabric_configfs, tf_group);
275c66ac9dbSNicholas Bellinger 
2766708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
277c66ac9dbSNicholas Bellinger 		" tf list\n", config_item_name(item));
278c66ac9dbSNicholas Bellinger 
2796708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
28059a206b4SDavid Disseldorp 			" %s\n", tf->tf_ops->fabric_name);
281c66ac9dbSNicholas Bellinger 	atomic_dec(&tf->tf_access_cnt);
282c66ac9dbSNicholas Bellinger 
2836708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
284c66ac9dbSNicholas Bellinger 			" %s\n", config_item_name(item));
285c66ac9dbSNicholas Bellinger 
2861ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&tf->tf_group);
287c66ac9dbSNicholas Bellinger 	config_item_put(item);
288c66ac9dbSNicholas Bellinger }
289c66ac9dbSNicholas Bellinger 
290c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_fabric_group_ops = {
291c66ac9dbSNicholas Bellinger 	.make_group	= &target_core_register_fabric,
292c66ac9dbSNicholas Bellinger 	.drop_item	= &target_core_deregister_fabric,
293c66ac9dbSNicholas Bellinger };
294c66ac9dbSNicholas Bellinger 
295c66ac9dbSNicholas Bellinger /*
296c66ac9dbSNicholas Bellinger  * All item attributes appearing in /sys/kernel/target/ appear here.
297c66ac9dbSNicholas Bellinger  */
298c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_fabric_item_attrs[] = {
299c66ac9dbSNicholas Bellinger 	&target_core_item_attr_version,
300a96e9783SLee Duncan 	&target_core_item_attr_dbroot,
301c66ac9dbSNicholas Bellinger 	NULL,
302c66ac9dbSNicholas Bellinger };
303c66ac9dbSNicholas Bellinger 
304c66ac9dbSNicholas Bellinger /*
305c66ac9dbSNicholas Bellinger  * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
306c66ac9dbSNicholas Bellinger  */
307ece550b5SBhumika Goyal static const struct config_item_type target_core_fabrics_item = {
308c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_fabric_group_ops,
309c66ac9dbSNicholas Bellinger 	.ct_attrs	= target_core_fabric_item_attrs,
310c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
311c66ac9dbSNicholas Bellinger };
312c66ac9dbSNicholas Bellinger 
313c66ac9dbSNicholas Bellinger static struct configfs_subsystem target_core_fabrics = {
314c66ac9dbSNicholas Bellinger 	.su_group = {
315c66ac9dbSNicholas Bellinger 		.cg_item = {
316c66ac9dbSNicholas Bellinger 			.ci_namebuf = "target",
317c66ac9dbSNicholas Bellinger 			.ci_type = &target_core_fabrics_item,
318c66ac9dbSNicholas Bellinger 		},
319c66ac9dbSNicholas Bellinger 	},
320c66ac9dbSNicholas Bellinger };
321c66ac9dbSNicholas Bellinger 
322d588cf8fSChristoph Hellwig int target_depend_item(struct config_item *item)
323d588cf8fSChristoph Hellwig {
324d588cf8fSChristoph Hellwig 	return configfs_depend_item(&target_core_fabrics, item);
325d588cf8fSChristoph Hellwig }
326d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_depend_item);
327d588cf8fSChristoph Hellwig 
328d588cf8fSChristoph Hellwig void target_undepend_item(struct config_item *item)
329d588cf8fSChristoph Hellwig {
3309a9e3415SKrzysztof Opasiak 	return configfs_undepend_item(item);
331d588cf8fSChristoph Hellwig }
332d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_undepend_item);
333c66ac9dbSNicholas Bellinger 
334c66ac9dbSNicholas Bellinger /*##############################################################################
335c66ac9dbSNicholas Bellinger // Start functions called by external Target Fabrics Modules
336c66ac9dbSNicholas Bellinger //############################################################################*/
337c66ac9dbSNicholas Bellinger 
3389ac8928eSChristoph Hellwig static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
339c66ac9dbSNicholas Bellinger {
34059a206b4SDavid Disseldorp 	if (tfo->fabric_alias) {
34159a206b4SDavid Disseldorp 		if (strlen(tfo->fabric_alias) >= TARGET_FABRIC_NAME_SIZE) {
34259a206b4SDavid Disseldorp 			pr_err("Passed alias: %s exceeds "
34359a206b4SDavid Disseldorp 				"TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_alias);
3449ac8928eSChristoph Hellwig 			return -EINVAL;
345c66ac9dbSNicholas Bellinger 		}
346c66ac9dbSNicholas Bellinger 	}
34730c7ca93SDavid Disseldorp 	if (!tfo->fabric_name) {
34830c7ca93SDavid Disseldorp 		pr_err("Missing tfo->fabric_name\n");
349c66ac9dbSNicholas Bellinger 		return -EINVAL;
350c66ac9dbSNicholas Bellinger 	}
35159a206b4SDavid Disseldorp 	if (strlen(tfo->fabric_name) >= TARGET_FABRIC_NAME_SIZE) {
35259a206b4SDavid Disseldorp 		pr_err("Passed name: %s exceeds "
35359a206b4SDavid Disseldorp 			"TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_name);
35459a206b4SDavid Disseldorp 		return -EINVAL;
35559a206b4SDavid Disseldorp 	}
3566708bb27SAndy Grover 	if (!tfo->tpg_get_wwn) {
3576708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_wwn()\n");
358c66ac9dbSNicholas Bellinger 		return -EINVAL;
359c66ac9dbSNicholas Bellinger 	}
3606708bb27SAndy Grover 	if (!tfo->tpg_get_tag) {
3616708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_tag()\n");
362c66ac9dbSNicholas Bellinger 		return -EINVAL;
363c66ac9dbSNicholas Bellinger 	}
3646708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode) {
3656708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode()\n");
366c66ac9dbSNicholas Bellinger 		return -EINVAL;
367c66ac9dbSNicholas Bellinger 	}
3686708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_cache) {
3696708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
370c66ac9dbSNicholas Bellinger 		return -EINVAL;
371c66ac9dbSNicholas Bellinger 	}
3726708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_write_protect) {
3736708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
374c66ac9dbSNicholas Bellinger 		return -EINVAL;
375c66ac9dbSNicholas Bellinger 	}
3766708bb27SAndy Grover 	if (!tfo->tpg_check_prod_mode_write_protect) {
3776708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
378c66ac9dbSNicholas Bellinger 		return -EINVAL;
379c66ac9dbSNicholas Bellinger 	}
3806708bb27SAndy Grover 	if (!tfo->tpg_get_inst_index) {
3816708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_inst_index()\n");
382c66ac9dbSNicholas Bellinger 		return -EINVAL;
383c66ac9dbSNicholas Bellinger 	}
38435462975SChristoph Hellwig 	if (!tfo->release_cmd) {
3856708bb27SAndy Grover 		pr_err("Missing tfo->release_cmd()\n");
386c66ac9dbSNicholas Bellinger 		return -EINVAL;
387c66ac9dbSNicholas Bellinger 	}
3886708bb27SAndy Grover 	if (!tfo->sess_get_index) {
3896708bb27SAndy Grover 		pr_err("Missing tfo->sess_get_index()\n");
390c66ac9dbSNicholas Bellinger 		return -EINVAL;
391c66ac9dbSNicholas Bellinger 	}
3926708bb27SAndy Grover 	if (!tfo->write_pending) {
3936708bb27SAndy Grover 		pr_err("Missing tfo->write_pending()\n");
394c66ac9dbSNicholas Bellinger 		return -EINVAL;
395c66ac9dbSNicholas Bellinger 	}
3966708bb27SAndy Grover 	if (!tfo->set_default_node_attributes) {
3976708bb27SAndy Grover 		pr_err("Missing tfo->set_default_node_attributes()\n");
398c66ac9dbSNicholas Bellinger 		return -EINVAL;
399c66ac9dbSNicholas Bellinger 	}
4006708bb27SAndy Grover 	if (!tfo->get_cmd_state) {
4016708bb27SAndy Grover 		pr_err("Missing tfo->get_cmd_state()\n");
402c66ac9dbSNicholas Bellinger 		return -EINVAL;
403c66ac9dbSNicholas Bellinger 	}
4046708bb27SAndy Grover 	if (!tfo->queue_data_in) {
4056708bb27SAndy Grover 		pr_err("Missing tfo->queue_data_in()\n");
406c66ac9dbSNicholas Bellinger 		return -EINVAL;
407c66ac9dbSNicholas Bellinger 	}
4086708bb27SAndy Grover 	if (!tfo->queue_status) {
4096708bb27SAndy Grover 		pr_err("Missing tfo->queue_status()\n");
410c66ac9dbSNicholas Bellinger 		return -EINVAL;
411c66ac9dbSNicholas Bellinger 	}
4126708bb27SAndy Grover 	if (!tfo->queue_tm_rsp) {
4136708bb27SAndy Grover 		pr_err("Missing tfo->queue_tm_rsp()\n");
414c66ac9dbSNicholas Bellinger 		return -EINVAL;
415c66ac9dbSNicholas Bellinger 	}
416131e6abcSNicholas Bellinger 	if (!tfo->aborted_task) {
417131e6abcSNicholas Bellinger 		pr_err("Missing tfo->aborted_task()\n");
418131e6abcSNicholas Bellinger 		return -EINVAL;
419131e6abcSNicholas Bellinger 	}
4209c28ca4fSNicholas Bellinger 	if (!tfo->check_stop_free) {
4219c28ca4fSNicholas Bellinger 		pr_err("Missing tfo->check_stop_free()\n");
4229c28ca4fSNicholas Bellinger 		return -EINVAL;
4239c28ca4fSNicholas Bellinger 	}
424c66ac9dbSNicholas Bellinger 	/*
425c66ac9dbSNicholas Bellinger 	 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
426c66ac9dbSNicholas Bellinger 	 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
427c66ac9dbSNicholas Bellinger 	 * target_core_fabric_configfs.c WWN+TPG group context code.
428c66ac9dbSNicholas Bellinger 	 */
4296708bb27SAndy Grover 	if (!tfo->fabric_make_wwn) {
4306708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_wwn()\n");
431c66ac9dbSNicholas Bellinger 		return -EINVAL;
432c66ac9dbSNicholas Bellinger 	}
4336708bb27SAndy Grover 	if (!tfo->fabric_drop_wwn) {
4346708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_wwn()\n");
435c66ac9dbSNicholas Bellinger 		return -EINVAL;
436c66ac9dbSNicholas Bellinger 	}
4376708bb27SAndy Grover 	if (!tfo->fabric_make_tpg) {
4386708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_tpg()\n");
439c66ac9dbSNicholas Bellinger 		return -EINVAL;
440c66ac9dbSNicholas Bellinger 	}
4416708bb27SAndy Grover 	if (!tfo->fabric_drop_tpg) {
4426708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_tpg()\n");
443c66ac9dbSNicholas Bellinger 		return -EINVAL;
444c66ac9dbSNicholas Bellinger 	}
445c66ac9dbSNicholas Bellinger 
446c66ac9dbSNicholas Bellinger 	return 0;
447c66ac9dbSNicholas Bellinger }
448c66ac9dbSNicholas Bellinger 
4499ac8928eSChristoph Hellwig int target_register_template(const struct target_core_fabric_ops *fo)
450c66ac9dbSNicholas Bellinger {
4519ac8928eSChristoph Hellwig 	struct target_fabric_configfs *tf;
452c66ac9dbSNicholas Bellinger 	int ret;
453c66ac9dbSNicholas Bellinger 
4549ac8928eSChristoph Hellwig 	ret = target_fabric_tf_ops_check(fo);
4559ac8928eSChristoph Hellwig 	if (ret)
456c66ac9dbSNicholas Bellinger 		return ret;
457c66ac9dbSNicholas Bellinger 
4589ac8928eSChristoph Hellwig 	tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
4599ac8928eSChristoph Hellwig 	if (!tf) {
4609ac8928eSChristoph Hellwig 		pr_err("%s: could not allocate memory!\n", __func__);
4619ac8928eSChristoph Hellwig 		return -ENOMEM;
4629ac8928eSChristoph Hellwig 	}
4639ac8928eSChristoph Hellwig 
4649ac8928eSChristoph Hellwig 	INIT_LIST_HEAD(&tf->tf_list);
4659ac8928eSChristoph Hellwig 	atomic_set(&tf->tf_access_cnt, 0);
466ef0caf8dSChristoph Hellwig 	tf->tf_ops = fo;
4679ac8928eSChristoph Hellwig 	target_fabric_setup_cits(tf);
4689ac8928eSChristoph Hellwig 
4699ac8928eSChristoph Hellwig 	mutex_lock(&g_tf_lock);
4709ac8928eSChristoph Hellwig 	list_add_tail(&tf->tf_list, &g_tf_list);
4719ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4729ac8928eSChristoph Hellwig 
473c66ac9dbSNicholas Bellinger 	return 0;
474c66ac9dbSNicholas Bellinger }
4759ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_register_template);
476c66ac9dbSNicholas Bellinger 
4779ac8928eSChristoph Hellwig void target_unregister_template(const struct target_core_fabric_ops *fo)
478c66ac9dbSNicholas Bellinger {
4799ac8928eSChristoph Hellwig 	struct target_fabric_configfs *t;
480c66ac9dbSNicholas Bellinger 
481c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
4829ac8928eSChristoph Hellwig 	list_for_each_entry(t, &g_tf_list, tf_list) {
48359a206b4SDavid Disseldorp 		if (!strcmp(t->tf_ops->fabric_name, fo->fabric_name)) {
4849ac8928eSChristoph Hellwig 			BUG_ON(atomic_read(&t->tf_access_cnt));
4859ac8928eSChristoph Hellwig 			list_del(&t->tf_list);
48694509182SNicholas Bellinger 			mutex_unlock(&g_tf_lock);
48794509182SNicholas Bellinger 			/*
48894509182SNicholas Bellinger 			 * Wait for any outstanding fabric se_deve_entry->rcu_head
48994509182SNicholas Bellinger 			 * callbacks to complete post kfree_rcu(), before allowing
49094509182SNicholas Bellinger 			 * fabric driver unload of TFO->module to proceed.
49194509182SNicholas Bellinger 			 */
49294509182SNicholas Bellinger 			rcu_barrier();
4939ac8928eSChristoph Hellwig 			kfree(t);
49494509182SNicholas Bellinger 			return;
495c66ac9dbSNicholas Bellinger 		}
496c66ac9dbSNicholas Bellinger 	}
4979ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4989ac8928eSChristoph Hellwig }
4999ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_unregister_template);
500c66ac9dbSNicholas Bellinger 
501c66ac9dbSNicholas Bellinger /*##############################################################################
502c66ac9dbSNicholas Bellinger // Stop functions called by external Target Fabrics Modules
503c66ac9dbSNicholas Bellinger //############################################################################*/
504c66ac9dbSNicholas Bellinger 
5052eafd729SChristoph Hellwig static inline struct se_dev_attrib *to_attrib(struct config_item *item)
5062eafd729SChristoph Hellwig {
5072eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_dev_attrib,
5082eafd729SChristoph Hellwig 			da_group);
5095873c4d1SChristoph Hellwig }
5105873c4d1SChristoph Hellwig 
5112eafd729SChristoph Hellwig /* Start functions for struct config_item_type tb_dev_attrib_cit */
5122eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_SHOW(_name)					\
5132eafd729SChristoph Hellwig static ssize_t _name##_show(struct config_item *item, char *page)	\
5142eafd729SChristoph Hellwig {									\
5152eafd729SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
5162eafd729SChristoph Hellwig }
5173effdb90SChristoph Hellwig 
5182eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
5192eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
5202eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
5212eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
5222eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
5232eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
5242eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
5252eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
5262eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
5272eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
5282eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
529b49d6f78SDavid Disseldorp DEF_CONFIGFS_ATTRIB_SHOW(emulate_pr);
5302eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
5312eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
532056e8924SDmitry Monakhov DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_verify);
5332eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
5342eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
5352eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
5362eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
5372eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
5382eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(block_size);
5392eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
5402eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
5412eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
5422eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
5432eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
5442eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
5452eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
5462eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
547e6f41633SJamie Pocas DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
5482eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
5492eafd729SChristoph Hellwig 
5502eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_U32(_name)				\
5512eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
5525873c4d1SChristoph Hellwig 		size_t count)						\
5535873c4d1SChristoph Hellwig {									\
5542eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5553effdb90SChristoph Hellwig 	u32 val;							\
5565873c4d1SChristoph Hellwig 	int ret;							\
5575873c4d1SChristoph Hellwig 									\
5583effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);					\
5593effdb90SChristoph Hellwig 	if (ret < 0)							\
5603effdb90SChristoph Hellwig 		return ret;						\
5613effdb90SChristoph Hellwig 	da->_name = val;						\
5623effdb90SChristoph Hellwig 	return count;							\
5635873c4d1SChristoph Hellwig }
5645873c4d1SChristoph Hellwig 
5652eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
5662eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
5672eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
5682eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
5692eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
5705873c4d1SChristoph Hellwig 
5712eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name)				\
5722eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,	\
5733effdb90SChristoph Hellwig 		size_t count)						\
5743effdb90SChristoph Hellwig {									\
5752eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5763effdb90SChristoph Hellwig 	bool flag;							\
5773effdb90SChristoph Hellwig 	int ret;							\
5783effdb90SChristoph Hellwig 									\
5793effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);					\
5803effdb90SChristoph Hellwig 	if (ret < 0)							\
5813effdb90SChristoph Hellwig 		return ret;						\
5823effdb90SChristoph Hellwig 	da->_name = flag;						\
5833effdb90SChristoph Hellwig 	return count;							\
5843effdb90SChristoph Hellwig }
5853effdb90SChristoph Hellwig 
5862eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
5872eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
5882eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
589b49d6f78SDavid Disseldorp DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_pr);
5902eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
5912eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
5923effdb90SChristoph Hellwig 
5932eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name)				\
5942eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
5953effdb90SChristoph Hellwig 		size_t count)						\
5963effdb90SChristoph Hellwig {									\
5973effdb90SChristoph Hellwig 	printk_once(KERN_WARNING					\
598234bdbc4SChristophe Vu-Brugier 		"ignoring deprecated %s attribute\n",			\
599234bdbc4SChristophe Vu-Brugier 		__stringify(_name));					\
6003effdb90SChristoph Hellwig 	return count;							\
6013effdb90SChristoph Hellwig }
6023effdb90SChristoph Hellwig 
6032eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
6042eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
6053effdb90SChristoph Hellwig 
6063effdb90SChristoph Hellwig static void dev_set_t10_wwn_model_alias(struct se_device *dev)
6073effdb90SChristoph Hellwig {
6083effdb90SChristoph Hellwig 	const char *configname;
6093effdb90SChristoph Hellwig 
6103effdb90SChristoph Hellwig 	configname = config_item_name(&dev->dev_group.cg_item);
611b2da4abfSDavid Disseldorp 	if (strlen(configname) >= INQUIRY_MODEL_LEN) {
6123effdb90SChristoph Hellwig 		pr_warn("dev[%p]: Backstore name '%s' is too long for "
613b2da4abfSDavid Disseldorp 			"INQUIRY_MODEL, truncating to 15 characters\n", dev,
6143effdb90SChristoph Hellwig 			configname);
6153effdb90SChristoph Hellwig 	}
616b2da4abfSDavid Disseldorp 	/*
617b2da4abfSDavid Disseldorp 	 * XXX We can't use sizeof(dev->t10_wwn.model) (INQUIRY_MODEL_LEN + 1)
618b2da4abfSDavid Disseldorp 	 * here without potentially breaking existing setups, so continue to
619b2da4abfSDavid Disseldorp 	 * truncate one byte shorter than what can be carried in INQUIRY.
620b2da4abfSDavid Disseldorp 	 */
621b2da4abfSDavid Disseldorp 	strlcpy(dev->t10_wwn.model, configname, INQUIRY_MODEL_LEN);
6223effdb90SChristoph Hellwig }
6233effdb90SChristoph Hellwig 
6242eafd729SChristoph Hellwig static ssize_t emulate_model_alias_store(struct config_item *item,
6253effdb90SChristoph Hellwig 		const char *page, size_t count)
6263effdb90SChristoph Hellwig {
6272eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6283effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
6293effdb90SChristoph Hellwig 	bool flag;
6303effdb90SChristoph Hellwig 	int ret;
6313effdb90SChristoph Hellwig 
6323effdb90SChristoph Hellwig 	if (dev->export_count) {
6333effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change model alias"
6343effdb90SChristoph Hellwig 			" while export_count is %d\n",
6353effdb90SChristoph Hellwig 			dev, dev->export_count);
6363effdb90SChristoph Hellwig 		return -EINVAL;
6373effdb90SChristoph Hellwig 	}
6383effdb90SChristoph Hellwig 
6393effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6403effdb90SChristoph Hellwig 	if (ret < 0)
6413effdb90SChristoph Hellwig 		return ret;
6423effdb90SChristoph Hellwig 
643b2da4abfSDavid Disseldorp 	BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
6443effdb90SChristoph Hellwig 	if (flag) {
6453effdb90SChristoph Hellwig 		dev_set_t10_wwn_model_alias(dev);
6463effdb90SChristoph Hellwig 	} else {
647b2da4abfSDavid Disseldorp 		strlcpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
648b2da4abfSDavid Disseldorp 			sizeof(dev->t10_wwn.model));
6493effdb90SChristoph Hellwig 	}
6503effdb90SChristoph Hellwig 	da->emulate_model_alias = flag;
6513effdb90SChristoph Hellwig 	return count;
6523effdb90SChristoph Hellwig }
6533effdb90SChristoph Hellwig 
6542eafd729SChristoph Hellwig static ssize_t emulate_write_cache_store(struct config_item *item,
6553effdb90SChristoph Hellwig 		const char *page, size_t count)
6563effdb90SChristoph Hellwig {
6572eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6583effdb90SChristoph Hellwig 	bool flag;
6593effdb90SChristoph Hellwig 	int ret;
6603effdb90SChristoph Hellwig 
6613effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6623effdb90SChristoph Hellwig 	if (ret < 0)
6633effdb90SChristoph Hellwig 		return ret;
6643effdb90SChristoph Hellwig 
6653effdb90SChristoph Hellwig 	if (flag && da->da_dev->transport->get_write_cache) {
6663effdb90SChristoph Hellwig 		pr_err("emulate_write_cache not supported for this device\n");
6673effdb90SChristoph Hellwig 		return -EINVAL;
6683effdb90SChristoph Hellwig 	}
6693effdb90SChristoph Hellwig 
6703effdb90SChristoph Hellwig 	da->emulate_write_cache = flag;
6713effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
6723effdb90SChristoph Hellwig 			da->da_dev, flag);
6733effdb90SChristoph Hellwig 	return count;
6743effdb90SChristoph Hellwig }
6753effdb90SChristoph Hellwig 
6762eafd729SChristoph Hellwig static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
6773effdb90SChristoph Hellwig 		const char *page, size_t count)
6783effdb90SChristoph Hellwig {
6792eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6803effdb90SChristoph Hellwig 	u32 val;
6813effdb90SChristoph Hellwig 	int ret;
6823effdb90SChristoph Hellwig 
6833effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
6843effdb90SChristoph Hellwig 	if (ret < 0)
6853effdb90SChristoph Hellwig 		return ret;
6863effdb90SChristoph Hellwig 
6871bf630fdSDavid Disseldorp 	if (val != TARGET_UA_INTLCK_CTRL_CLEAR
6881bf630fdSDavid Disseldorp 	 && val != TARGET_UA_INTLCK_CTRL_NO_CLEAR
6891bf630fdSDavid Disseldorp 	 && val != TARGET_UA_INTLCK_CTRL_ESTABLISH_UA) {
6903effdb90SChristoph Hellwig 		pr_err("Illegal value %d\n", val);
6913effdb90SChristoph Hellwig 		return -EINVAL;
6923effdb90SChristoph Hellwig 	}
6933effdb90SChristoph Hellwig 
6943effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
6953effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
6963effdb90SChristoph Hellwig 			" UA_INTRLCK_CTRL while export_count is %d\n",
6973effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
6983effdb90SChristoph Hellwig 		return -EINVAL;
6993effdb90SChristoph Hellwig 	}
7003effdb90SChristoph Hellwig 	da->emulate_ua_intlck_ctrl = val;
7013effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
7023effdb90SChristoph Hellwig 		da->da_dev, val);
7033effdb90SChristoph Hellwig 	return count;
7043effdb90SChristoph Hellwig }
7053effdb90SChristoph Hellwig 
7062eafd729SChristoph Hellwig static ssize_t emulate_tas_store(struct config_item *item,
7073effdb90SChristoph Hellwig 		const char *page, size_t count)
7083effdb90SChristoph Hellwig {
7092eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7103effdb90SChristoph Hellwig 	bool flag;
7113effdb90SChristoph Hellwig 	int ret;
7123effdb90SChristoph Hellwig 
7133effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7143effdb90SChristoph Hellwig 	if (ret < 0)
7153effdb90SChristoph Hellwig 		return ret;
7163effdb90SChristoph Hellwig 
7173effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
7183effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TAS while"
7193effdb90SChristoph Hellwig 			" export_count is %d\n",
7203effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
7213effdb90SChristoph Hellwig 		return -EINVAL;
7223effdb90SChristoph Hellwig 	}
7233effdb90SChristoph Hellwig 	da->emulate_tas = flag;
7243effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
7253effdb90SChristoph Hellwig 		da->da_dev, flag ? "Enabled" : "Disabled");
7263effdb90SChristoph Hellwig 
7273effdb90SChristoph Hellwig 	return count;
7283effdb90SChristoph Hellwig }
7293effdb90SChristoph Hellwig 
7302eafd729SChristoph Hellwig static ssize_t emulate_tpu_store(struct config_item *item,
7313effdb90SChristoph Hellwig 		const char *page, size_t count)
7323effdb90SChristoph Hellwig {
7332eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7343effdb90SChristoph Hellwig 	bool flag;
7353effdb90SChristoph Hellwig 	int ret;
7363effdb90SChristoph Hellwig 
7373effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7383effdb90SChristoph Hellwig 	if (ret < 0)
7393effdb90SChristoph Hellwig 		return ret;
7403effdb90SChristoph Hellwig 
7413effdb90SChristoph Hellwig 	/*
7423effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
7433effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
7443effdb90SChristoph Hellwig 	 */
7453effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
7463effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
7473effdb90SChristoph Hellwig 		return -ENOSYS;
7483effdb90SChristoph Hellwig 	}
7493effdb90SChristoph Hellwig 
7503effdb90SChristoph Hellwig 	da->emulate_tpu = flag;
7513effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
7523effdb90SChristoph Hellwig 		da->da_dev, flag);
7533effdb90SChristoph Hellwig 	return count;
7543effdb90SChristoph Hellwig }
7553effdb90SChristoph Hellwig 
7562eafd729SChristoph Hellwig static ssize_t emulate_tpws_store(struct config_item *item,
7573effdb90SChristoph Hellwig 		const char *page, size_t count)
7583effdb90SChristoph Hellwig {
7592eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7603effdb90SChristoph Hellwig 	bool flag;
7613effdb90SChristoph Hellwig 	int ret;
7623effdb90SChristoph Hellwig 
7633effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7643effdb90SChristoph Hellwig 	if (ret < 0)
7653effdb90SChristoph Hellwig 		return ret;
7663effdb90SChristoph Hellwig 
7673effdb90SChristoph Hellwig 	/*
7683effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
7693effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
7703effdb90SChristoph Hellwig 	 */
7713effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
7723effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
7733effdb90SChristoph Hellwig 		return -ENOSYS;
7743effdb90SChristoph Hellwig 	}
7753effdb90SChristoph Hellwig 
7763effdb90SChristoph Hellwig 	da->emulate_tpws = flag;
7773effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
7783effdb90SChristoph Hellwig 				da->da_dev, flag);
7793effdb90SChristoph Hellwig 	return count;
7803effdb90SChristoph Hellwig }
7813effdb90SChristoph Hellwig 
7822eafd729SChristoph Hellwig static ssize_t pi_prot_type_store(struct config_item *item,
7833effdb90SChristoph Hellwig 		const char *page, size_t count)
7843effdb90SChristoph Hellwig {
7852eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7863effdb90SChristoph Hellwig 	int old_prot = da->pi_prot_type, ret;
7873effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
7883effdb90SChristoph Hellwig 	u32 flag;
7893effdb90SChristoph Hellwig 
7903effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &flag);
7913effdb90SChristoph Hellwig 	if (ret < 0)
7923effdb90SChristoph Hellwig 		return ret;
7933effdb90SChristoph Hellwig 
7943effdb90SChristoph Hellwig 	if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
7953effdb90SChristoph Hellwig 		pr_err("Illegal value %d for pi_prot_type\n", flag);
7963effdb90SChristoph Hellwig 		return -EINVAL;
7973effdb90SChristoph Hellwig 	}
7983effdb90SChristoph Hellwig 	if (flag == 2) {
7993effdb90SChristoph Hellwig 		pr_err("DIF TYPE2 protection currently not supported\n");
8003effdb90SChristoph Hellwig 		return -ENOSYS;
8013effdb90SChristoph Hellwig 	}
8023effdb90SChristoph Hellwig 	if (da->hw_pi_prot_type) {
8033effdb90SChristoph Hellwig 		pr_warn("DIF protection enabled on underlying hardware,"
8043effdb90SChristoph Hellwig 			" ignoring\n");
8053effdb90SChristoph Hellwig 		return count;
8063effdb90SChristoph Hellwig 	}
8073effdb90SChristoph Hellwig 	if (!dev->transport->init_prot || !dev->transport->free_prot) {
8083effdb90SChristoph Hellwig 		/* 0 is only allowed value for non-supporting backends */
8093effdb90SChristoph Hellwig 		if (flag == 0)
810bc1a7d6aSAndy Grover 			return count;
8113effdb90SChristoph Hellwig 
8123effdb90SChristoph Hellwig 		pr_err("DIF protection not supported by backend: %s\n",
8133effdb90SChristoph Hellwig 		       dev->transport->name);
8143effdb90SChristoph Hellwig 		return -ENOSYS;
8153effdb90SChristoph Hellwig 	}
816cb0f32e1SMike Christie 	if (!target_dev_configured(dev)) {
8173effdb90SChristoph Hellwig 		pr_err("DIF protection requires device to be configured\n");
8183effdb90SChristoph Hellwig 		return -ENODEV;
8193effdb90SChristoph Hellwig 	}
8203effdb90SChristoph Hellwig 	if (dev->export_count) {
8213effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device PROT type while"
8223effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
8233effdb90SChristoph Hellwig 		return -EINVAL;
8243effdb90SChristoph Hellwig 	}
8253effdb90SChristoph Hellwig 
8263effdb90SChristoph Hellwig 	da->pi_prot_type = flag;
8273effdb90SChristoph Hellwig 
8283effdb90SChristoph Hellwig 	if (flag && !old_prot) {
8293effdb90SChristoph Hellwig 		ret = dev->transport->init_prot(dev);
8303effdb90SChristoph Hellwig 		if (ret) {
8313effdb90SChristoph Hellwig 			da->pi_prot_type = old_prot;
832056e8924SDmitry Monakhov 			da->pi_prot_verify = (bool) da->pi_prot_type;
8333effdb90SChristoph Hellwig 			return ret;
8343effdb90SChristoph Hellwig 		}
8353effdb90SChristoph Hellwig 
8363effdb90SChristoph Hellwig 	} else if (!flag && old_prot) {
8373effdb90SChristoph Hellwig 		dev->transport->free_prot(dev);
8383effdb90SChristoph Hellwig 	}
8393effdb90SChristoph Hellwig 
840056e8924SDmitry Monakhov 	da->pi_prot_verify = (bool) da->pi_prot_type;
8413effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
8423effdb90SChristoph Hellwig 	return count;
8433effdb90SChristoph Hellwig }
8443effdb90SChristoph Hellwig 
845b6cd7f34SDavid Disseldorp /* always zero, but attr needs to remain RW to avoid userspace breakage */
846b6cd7f34SDavid Disseldorp static ssize_t pi_prot_format_show(struct config_item *item, char *page)
847b6cd7f34SDavid Disseldorp {
848b6cd7f34SDavid Disseldorp 	return snprintf(page, PAGE_SIZE, "0\n");
849b6cd7f34SDavid Disseldorp }
850b6cd7f34SDavid Disseldorp 
8512eafd729SChristoph Hellwig static ssize_t pi_prot_format_store(struct config_item *item,
8523effdb90SChristoph Hellwig 		const char *page, size_t count)
8533effdb90SChristoph Hellwig {
8542eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
8553effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
8563effdb90SChristoph Hellwig 	bool flag;
8573effdb90SChristoph Hellwig 	int ret;
8583effdb90SChristoph Hellwig 
8593effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
8603effdb90SChristoph Hellwig 	if (ret < 0)
8613effdb90SChristoph Hellwig 		return ret;
8623effdb90SChristoph Hellwig 
8633effdb90SChristoph Hellwig 	if (!flag)
8643effdb90SChristoph Hellwig 		return count;
8653effdb90SChristoph Hellwig 
8663effdb90SChristoph Hellwig 	if (!dev->transport->format_prot) {
8673effdb90SChristoph Hellwig 		pr_err("DIF protection format not supported by backend %s\n",
8683effdb90SChristoph Hellwig 		       dev->transport->name);
8693effdb90SChristoph Hellwig 		return -ENOSYS;
8703effdb90SChristoph Hellwig 	}
871cb0f32e1SMike Christie 	if (!target_dev_configured(dev)) {
8723effdb90SChristoph Hellwig 		pr_err("DIF protection format requires device to be configured\n");
8733effdb90SChristoph Hellwig 		return -ENODEV;
8743effdb90SChristoph Hellwig 	}
8753effdb90SChristoph Hellwig 	if (dev->export_count) {
8763effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to format SE Device PROT type while"
8773effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
8783effdb90SChristoph Hellwig 		return -EINVAL;
8793effdb90SChristoph Hellwig 	}
8803effdb90SChristoph Hellwig 
8813effdb90SChristoph Hellwig 	ret = dev->transport->format_prot(dev);
8823effdb90SChristoph Hellwig 	if (ret)
8833effdb90SChristoph Hellwig 		return ret;
8843effdb90SChristoph Hellwig 
8853effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
8863effdb90SChristoph Hellwig 	return count;
8873effdb90SChristoph Hellwig }
8883effdb90SChristoph Hellwig 
889056e8924SDmitry Monakhov static ssize_t pi_prot_verify_store(struct config_item *item,
890056e8924SDmitry Monakhov 		const char *page, size_t count)
891056e8924SDmitry Monakhov {
892056e8924SDmitry Monakhov 	struct se_dev_attrib *da = to_attrib(item);
893056e8924SDmitry Monakhov 	bool flag;
894056e8924SDmitry Monakhov 	int ret;
895056e8924SDmitry Monakhov 
896056e8924SDmitry Monakhov 	ret = strtobool(page, &flag);
897056e8924SDmitry Monakhov 	if (ret < 0)
898056e8924SDmitry Monakhov 		return ret;
899056e8924SDmitry Monakhov 
900056e8924SDmitry Monakhov 	if (!flag) {
901056e8924SDmitry Monakhov 		da->pi_prot_verify = flag;
902056e8924SDmitry Monakhov 		return count;
903056e8924SDmitry Monakhov 	}
904056e8924SDmitry Monakhov 	if (da->hw_pi_prot_type) {
905056e8924SDmitry Monakhov 		pr_warn("DIF protection enabled on underlying hardware,"
906056e8924SDmitry Monakhov 			" ignoring\n");
907056e8924SDmitry Monakhov 		return count;
908056e8924SDmitry Monakhov 	}
909056e8924SDmitry Monakhov 	if (!da->pi_prot_type) {
910056e8924SDmitry Monakhov 		pr_warn("DIF protection not supported by backend, ignoring\n");
911056e8924SDmitry Monakhov 		return count;
912056e8924SDmitry Monakhov 	}
913056e8924SDmitry Monakhov 	da->pi_prot_verify = flag;
914056e8924SDmitry Monakhov 
915056e8924SDmitry Monakhov 	return count;
916056e8924SDmitry Monakhov }
917056e8924SDmitry Monakhov 
9182eafd729SChristoph Hellwig static ssize_t force_pr_aptpl_store(struct config_item *item,
9193effdb90SChristoph Hellwig 		const char *page, size_t count)
9203effdb90SChristoph Hellwig {
9212eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9223effdb90SChristoph Hellwig 	bool flag;
9233effdb90SChristoph Hellwig 	int ret;
9243effdb90SChristoph Hellwig 
9253effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
9263effdb90SChristoph Hellwig 	if (ret < 0)
9273effdb90SChristoph Hellwig 		return ret;
9283effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
9293effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to set force_pr_aptpl while"
9303effdb90SChristoph Hellwig 		       " export_count is %d\n",
9313effdb90SChristoph Hellwig 		       da->da_dev, da->da_dev->export_count);
9323effdb90SChristoph Hellwig 		return -EINVAL;
9333effdb90SChristoph Hellwig 	}
9343effdb90SChristoph Hellwig 
9353effdb90SChristoph Hellwig 	da->force_pr_aptpl = flag;
9363effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
9373effdb90SChristoph Hellwig 	return count;
9383effdb90SChristoph Hellwig }
9393effdb90SChristoph Hellwig 
9402eafd729SChristoph Hellwig static ssize_t emulate_rest_reord_store(struct config_item *item,
9413effdb90SChristoph Hellwig 		const char *page, size_t count)
9423effdb90SChristoph Hellwig {
9432eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9443effdb90SChristoph Hellwig 	bool flag;
9453effdb90SChristoph Hellwig 	int ret;
9463effdb90SChristoph Hellwig 
9473effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
9483effdb90SChristoph Hellwig 	if (ret < 0)
9493effdb90SChristoph Hellwig 		return ret;
9503effdb90SChristoph Hellwig 
9513effdb90SChristoph Hellwig 	if (flag != 0) {
9523effdb90SChristoph Hellwig 		printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
9533effdb90SChristoph Hellwig 			" reordering not implemented\n", da->da_dev);
9543effdb90SChristoph Hellwig 		return -ENOSYS;
9553effdb90SChristoph Hellwig 	}
9563effdb90SChristoph Hellwig 	da->emulate_rest_reord = flag;
9573effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
9583effdb90SChristoph Hellwig 		da->da_dev, flag);
9593effdb90SChristoph Hellwig 	return count;
9603effdb90SChristoph Hellwig }
9613effdb90SChristoph Hellwig 
962e6f41633SJamie Pocas static ssize_t unmap_zeroes_data_store(struct config_item *item,
963e6f41633SJamie Pocas 		const char *page, size_t count)
964e6f41633SJamie Pocas {
965e6f41633SJamie Pocas 	struct se_dev_attrib *da = to_attrib(item);
966e6f41633SJamie Pocas 	bool flag;
967e6f41633SJamie Pocas 	int ret;
968e6f41633SJamie Pocas 
969e6f41633SJamie Pocas 	ret = strtobool(page, &flag);
970e6f41633SJamie Pocas 	if (ret < 0)
971e6f41633SJamie Pocas 		return ret;
972e6f41633SJamie Pocas 
973e6f41633SJamie Pocas 	if (da->da_dev->export_count) {
974e6f41633SJamie Pocas 		pr_err("dev[%p]: Unable to change SE Device"
975e6f41633SJamie Pocas 		       " unmap_zeroes_data while export_count is %d\n",
976e6f41633SJamie Pocas 		       da->da_dev, da->da_dev->export_count);
977e6f41633SJamie Pocas 		return -EINVAL;
978e6f41633SJamie Pocas 	}
979e6f41633SJamie Pocas 	/*
980e6f41633SJamie Pocas 	 * We expect this value to be non-zero when generic Block Layer
981e6f41633SJamie Pocas 	 * Discard supported is detected iblock_configure_device().
982e6f41633SJamie Pocas 	 */
983e6f41633SJamie Pocas 	if (flag && !da->max_unmap_block_desc_count) {
984e6f41633SJamie Pocas 		pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
985e6f41633SJamie Pocas 		       " because max_unmap_block_desc_count is zero\n",
986e6f41633SJamie Pocas 		       da->da_dev);
987e6f41633SJamie Pocas 		return -ENOSYS;
988e6f41633SJamie Pocas 	}
989e6f41633SJamie Pocas 	da->unmap_zeroes_data = flag;
990e6f41633SJamie Pocas 	pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
991e6f41633SJamie Pocas 		 da->da_dev, flag);
9922e498f25SNicholas Bellinger 	return count;
993e6f41633SJamie Pocas }
994e6f41633SJamie Pocas 
9953effdb90SChristoph Hellwig /*
9963effdb90SChristoph Hellwig  * Note, this can only be called on unexported SE Device Object.
9973effdb90SChristoph Hellwig  */
9982eafd729SChristoph Hellwig static ssize_t queue_depth_store(struct config_item *item,
9993effdb90SChristoph Hellwig 		const char *page, size_t count)
10003effdb90SChristoph Hellwig {
10012eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
10023effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
10033effdb90SChristoph Hellwig 	u32 val;
10043effdb90SChristoph Hellwig 	int ret;
10053effdb90SChristoph Hellwig 
10063effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
10073effdb90SChristoph Hellwig 	if (ret < 0)
10083effdb90SChristoph Hellwig 		return ret;
10093effdb90SChristoph Hellwig 
10103effdb90SChristoph Hellwig 	if (dev->export_count) {
10113effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TCQ while"
10123effdb90SChristoph Hellwig 			" export_count is %d\n",
10133effdb90SChristoph Hellwig 			dev, dev->export_count);
10143effdb90SChristoph Hellwig 		return -EINVAL;
10153effdb90SChristoph Hellwig 	}
10163effdb90SChristoph Hellwig 	if (!val) {
10173effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
10183effdb90SChristoph Hellwig 		return -EINVAL;
10193effdb90SChristoph Hellwig 	}
10203effdb90SChristoph Hellwig 
10213effdb90SChristoph Hellwig 	if (val > dev->dev_attrib.queue_depth) {
10223effdb90SChristoph Hellwig 		if (val > dev->dev_attrib.hw_queue_depth) {
10233effdb90SChristoph Hellwig 			pr_err("dev[%p]: Passed queue_depth:"
10243effdb90SChristoph Hellwig 				" %u exceeds TCM/SE_Device MAX"
10253effdb90SChristoph Hellwig 				" TCQ: %u\n", dev, val,
10263effdb90SChristoph Hellwig 				dev->dev_attrib.hw_queue_depth);
10273effdb90SChristoph Hellwig 			return -EINVAL;
10283effdb90SChristoph Hellwig 		}
10293effdb90SChristoph Hellwig 	}
10303effdb90SChristoph Hellwig 	da->queue_depth = dev->queue_depth = val;
10313effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
10323effdb90SChristoph Hellwig 	return count;
10333effdb90SChristoph Hellwig }
10343effdb90SChristoph Hellwig 
10352eafd729SChristoph Hellwig static ssize_t optimal_sectors_store(struct config_item *item,
10363effdb90SChristoph Hellwig 		const char *page, size_t count)
10373effdb90SChristoph Hellwig {
10382eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
10393effdb90SChristoph Hellwig 	u32 val;
10403effdb90SChristoph Hellwig 	int ret;
10413effdb90SChristoph Hellwig 
10423effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
10433effdb90SChristoph Hellwig 	if (ret < 0)
10443effdb90SChristoph Hellwig 		return ret;
10453effdb90SChristoph Hellwig 
10463effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
10473effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
10483effdb90SChristoph Hellwig 			" optimal_sectors while export_count is %d\n",
10493effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
10503effdb90SChristoph Hellwig 		return -EINVAL;
10513effdb90SChristoph Hellwig 	}
10523effdb90SChristoph Hellwig 	if (val > da->hw_max_sectors) {
10533effdb90SChristoph Hellwig 		pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
10543effdb90SChristoph Hellwig 			" greater than hw_max_sectors: %u\n",
10553effdb90SChristoph Hellwig 			da->da_dev, val, da->hw_max_sectors);
10563effdb90SChristoph Hellwig 		return -EINVAL;
10573effdb90SChristoph Hellwig 	}
10583effdb90SChristoph Hellwig 
10593effdb90SChristoph Hellwig 	da->optimal_sectors = val;
10603effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
10613effdb90SChristoph Hellwig 			da->da_dev, val);
10623effdb90SChristoph Hellwig 	return count;
10633effdb90SChristoph Hellwig }
10643effdb90SChristoph Hellwig 
10652eafd729SChristoph Hellwig static ssize_t block_size_store(struct config_item *item,
10663effdb90SChristoph Hellwig 		const char *page, size_t count)
10673effdb90SChristoph Hellwig {
10682eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
10693effdb90SChristoph Hellwig 	u32 val;
10703effdb90SChristoph Hellwig 	int ret;
10713effdb90SChristoph Hellwig 
10723effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
10733effdb90SChristoph Hellwig 	if (ret < 0)
10743effdb90SChristoph Hellwig 		return ret;
10753effdb90SChristoph Hellwig 
10763effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
10773effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device block_size"
10783effdb90SChristoph Hellwig 			" while export_count is %d\n",
10793effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
10803effdb90SChristoph Hellwig 		return -EINVAL;
10813effdb90SChristoph Hellwig 	}
10823effdb90SChristoph Hellwig 
10833effdb90SChristoph Hellwig 	if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
10843effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal value for block_device: %u"
10853effdb90SChristoph Hellwig 			" for SE device, must be 512, 1024, 2048 or 4096\n",
10863effdb90SChristoph Hellwig 			da->da_dev, val);
10873effdb90SChristoph Hellwig 		return -EINVAL;
10883effdb90SChristoph Hellwig 	}
10893effdb90SChristoph Hellwig 
10903effdb90SChristoph Hellwig 	da->block_size = val;
10913effdb90SChristoph Hellwig 	if (da->max_bytes_per_io)
10923effdb90SChristoph Hellwig 		da->hw_max_sectors = da->max_bytes_per_io / val;
10933effdb90SChristoph Hellwig 
10943effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device block_size changed to %u\n",
10953effdb90SChristoph Hellwig 			da->da_dev, val);
10963effdb90SChristoph Hellwig 	return count;
10973effdb90SChristoph Hellwig }
10985873c4d1SChristoph Hellwig 
1099c17d5d5fSMike Christie static ssize_t alua_support_show(struct config_item *item, char *page)
1100c17d5d5fSMike Christie {
1101c17d5d5fSMike Christie 	struct se_dev_attrib *da = to_attrib(item);
1102c17d5d5fSMike Christie 	u8 flags = da->da_dev->transport->transport_flags;
1103c17d5d5fSMike Christie 
1104c17d5d5fSMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n",
1105c17d5d5fSMike Christie 			flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ? 0 : 1);
1106c17d5d5fSMike Christie }
1107c17d5d5fSMike Christie 
1108c17d5d5fSMike Christie static ssize_t pgr_support_show(struct config_item *item, char *page)
1109c17d5d5fSMike Christie {
1110c17d5d5fSMike Christie 	struct se_dev_attrib *da = to_attrib(item);
1111c17d5d5fSMike Christie 	u8 flags = da->da_dev->transport->transport_flags;
1112c17d5d5fSMike Christie 
1113c17d5d5fSMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n",
1114c17d5d5fSMike Christie 			flags & TRANSPORT_FLAG_PASSTHROUGH_PGR ? 0 : 1);
1115c17d5d5fSMike Christie }
1116c17d5d5fSMike Christie 
11172eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_model_alias);
11182eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_dpo);
11192eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_write);
11202eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_read);
11212eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_write_cache);
11222eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
11232eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tas);
11242eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpu);
11252eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpws);
11262eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_caw);
11272eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_3pc);
1128b49d6f78SDavid Disseldorp CONFIGFS_ATTR(, emulate_pr);
11292eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_type);
11302eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_pi_prot_type);
1131b6cd7f34SDavid Disseldorp CONFIGFS_ATTR(, pi_prot_format);
1132056e8924SDmitry Monakhov CONFIGFS_ATTR(, pi_prot_verify);
11332eafd729SChristoph Hellwig CONFIGFS_ATTR(, enforce_pr_isids);
11342eafd729SChristoph Hellwig CONFIGFS_ATTR(, is_nonrot);
11352eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_rest_reord);
11362eafd729SChristoph Hellwig CONFIGFS_ATTR(, force_pr_aptpl);
11372eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_block_size);
11382eafd729SChristoph Hellwig CONFIGFS_ATTR(, block_size);
11392eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_max_sectors);
11402eafd729SChristoph Hellwig CONFIGFS_ATTR(, optimal_sectors);
11412eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_queue_depth);
11422eafd729SChristoph Hellwig CONFIGFS_ATTR(, queue_depth);
11432eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_lba_count);
11442eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_block_desc_count);
11452eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity);
11462eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity_alignment);
1147e6f41633SJamie Pocas CONFIGFS_ATTR(, unmap_zeroes_data);
11482eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_write_same_len);
1149c17d5d5fSMike Christie CONFIGFS_ATTR_RO(, alua_support);
1150c17d5d5fSMike Christie CONFIGFS_ATTR_RO(, pgr_support);
1151c66ac9dbSNicholas Bellinger 
11525873c4d1SChristoph Hellwig /*
11535873c4d1SChristoph Hellwig  * dev_attrib attributes for devices using the target core SBC/SPC
11545873c4d1SChristoph Hellwig  * interpreter.  Any backend using spc_parse_cdb should be using
11555873c4d1SChristoph Hellwig  * these.
11565873c4d1SChristoph Hellwig  */
11575873c4d1SChristoph Hellwig struct configfs_attribute *sbc_attrib_attrs[] = {
11582eafd729SChristoph Hellwig 	&attr_emulate_model_alias,
11592eafd729SChristoph Hellwig 	&attr_emulate_dpo,
11602eafd729SChristoph Hellwig 	&attr_emulate_fua_write,
11612eafd729SChristoph Hellwig 	&attr_emulate_fua_read,
11622eafd729SChristoph Hellwig 	&attr_emulate_write_cache,
11632eafd729SChristoph Hellwig 	&attr_emulate_ua_intlck_ctrl,
11642eafd729SChristoph Hellwig 	&attr_emulate_tas,
11652eafd729SChristoph Hellwig 	&attr_emulate_tpu,
11662eafd729SChristoph Hellwig 	&attr_emulate_tpws,
11672eafd729SChristoph Hellwig 	&attr_emulate_caw,
11682eafd729SChristoph Hellwig 	&attr_emulate_3pc,
1169b49d6f78SDavid Disseldorp 	&attr_emulate_pr,
11702eafd729SChristoph Hellwig 	&attr_pi_prot_type,
11712eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
11722eafd729SChristoph Hellwig 	&attr_pi_prot_format,
1173056e8924SDmitry Monakhov 	&attr_pi_prot_verify,
11742eafd729SChristoph Hellwig 	&attr_enforce_pr_isids,
11752eafd729SChristoph Hellwig 	&attr_is_nonrot,
11762eafd729SChristoph Hellwig 	&attr_emulate_rest_reord,
11772eafd729SChristoph Hellwig 	&attr_force_pr_aptpl,
11782eafd729SChristoph Hellwig 	&attr_hw_block_size,
11792eafd729SChristoph Hellwig 	&attr_block_size,
11802eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
11812eafd729SChristoph Hellwig 	&attr_optimal_sectors,
11822eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
11832eafd729SChristoph Hellwig 	&attr_queue_depth,
11842eafd729SChristoph Hellwig 	&attr_max_unmap_lba_count,
11852eafd729SChristoph Hellwig 	&attr_max_unmap_block_desc_count,
11862eafd729SChristoph Hellwig 	&attr_unmap_granularity,
11872eafd729SChristoph Hellwig 	&attr_unmap_granularity_alignment,
1188e6f41633SJamie Pocas 	&attr_unmap_zeroes_data,
11892eafd729SChristoph Hellwig 	&attr_max_write_same_len,
1190c17d5d5fSMike Christie 	&attr_alua_support,
1191c17d5d5fSMike Christie 	&attr_pgr_support,
11925873c4d1SChristoph Hellwig 	NULL,
11935873c4d1SChristoph Hellwig };
11945873c4d1SChristoph Hellwig EXPORT_SYMBOL(sbc_attrib_attrs);
11955873c4d1SChristoph Hellwig 
11965873c4d1SChristoph Hellwig /*
11975873c4d1SChristoph Hellwig  * Minimal dev_attrib attributes for devices passing through CDBs.
11985873c4d1SChristoph Hellwig  * In this case we only provide a few read-only attributes for
11995873c4d1SChristoph Hellwig  * backwards compatibility.
12005873c4d1SChristoph Hellwig  */
12015873c4d1SChristoph Hellwig struct configfs_attribute *passthrough_attrib_attrs[] = {
12022eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
12032eafd729SChristoph Hellwig 	&attr_hw_block_size,
12042eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
12052eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
120692999417SBodo Stroesser 	&attr_emulate_pr,
1207c17d5d5fSMike Christie 	&attr_alua_support,
1208c17d5d5fSMike Christie 	&attr_pgr_support,
12095873c4d1SChristoph Hellwig 	NULL,
12105873c4d1SChristoph Hellwig };
12115873c4d1SChristoph Hellwig EXPORT_SYMBOL(passthrough_attrib_attrs);
12125873c4d1SChristoph Hellwig 
12134703b625SBodo Stroesser /*
12144703b625SBodo Stroesser  * pr related dev_attrib attributes for devices passing through CDBs,
12154703b625SBodo Stroesser  * but allowing in core pr emulation.
12164703b625SBodo Stroesser  */
12174703b625SBodo Stroesser struct configfs_attribute *passthrough_pr_attrib_attrs[] = {
12184703b625SBodo Stroesser 	&attr_enforce_pr_isids,
12194703b625SBodo Stroesser 	&attr_force_pr_aptpl,
12204703b625SBodo Stroesser 	NULL,
12214703b625SBodo Stroesser };
12224703b625SBodo Stroesser EXPORT_SYMBOL(passthrough_pr_attrib_attrs);
12234703b625SBodo Stroesser 
12242eafd729SChristoph Hellwig TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
12258dc31ff9SMike Christie TB_CIT_SETUP_DRV(dev_action, NULL, NULL);
1226c66ac9dbSNicholas Bellinger 
1227f79a897eSNicholas Bellinger /* End functions for struct config_item_type tb_dev_attrib_cit */
1228c66ac9dbSNicholas Bellinger 
1229f8d389c6SNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_wwn_cit */
1230c66ac9dbSNicholas Bellinger 
12312eafd729SChristoph Hellwig static struct t10_wwn *to_t10_wwn(struct config_item *item)
12322eafd729SChristoph Hellwig {
12332eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
12342eafd729SChristoph Hellwig }
1235c66ac9dbSNicholas Bellinger 
12360322913cSAlan Adamson static ssize_t target_check_inquiry_data(char *buf)
12370322913cSAlan Adamson {
12380322913cSAlan Adamson 	size_t len;
12390322913cSAlan Adamson 	int i;
12400322913cSAlan Adamson 
12410322913cSAlan Adamson 	len = strlen(buf);
12420322913cSAlan Adamson 
12430322913cSAlan Adamson 	/*
12440322913cSAlan Adamson 	 * SPC 4.3.1:
12450322913cSAlan Adamson 	 * ASCII data fields shall contain only ASCII printable characters
12460322913cSAlan Adamson 	 * (i.e., code values 20h to 7Eh) and may be terminated with one or
12470322913cSAlan Adamson 	 * more ASCII null (00h) characters.
12480322913cSAlan Adamson 	 */
12490322913cSAlan Adamson 	for (i = 0; i < len; i++) {
12500322913cSAlan Adamson 		if (buf[i] < 0x20 || buf[i] > 0x7E) {
12510322913cSAlan Adamson 			pr_err("Emulated T10 Inquiry Data contains non-ASCII-printable characters\n");
12520322913cSAlan Adamson 			return -EINVAL;
12530322913cSAlan Adamson 		}
12540322913cSAlan Adamson 	}
12550322913cSAlan Adamson 
12560322913cSAlan Adamson 	return len;
12570322913cSAlan Adamson }
12580322913cSAlan Adamson 
1259c66ac9dbSNicholas Bellinger /*
126054a6f3f6SDavid Disseldorp  * STANDARD and VPD page 0x83 T10 Vendor Identification
126154a6f3f6SDavid Disseldorp  */
126254a6f3f6SDavid Disseldorp static ssize_t target_wwn_vendor_id_show(struct config_item *item,
126354a6f3f6SDavid Disseldorp 		char *page)
126454a6f3f6SDavid Disseldorp {
126554a6f3f6SDavid Disseldorp 	return sprintf(page, "%s\n", &to_t10_wwn(item)->vendor[0]);
126654a6f3f6SDavid Disseldorp }
126754a6f3f6SDavid Disseldorp 
126854a6f3f6SDavid Disseldorp static ssize_t target_wwn_vendor_id_store(struct config_item *item,
126954a6f3f6SDavid Disseldorp 		const char *page, size_t count)
127054a6f3f6SDavid Disseldorp {
127154a6f3f6SDavid Disseldorp 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
127254a6f3f6SDavid Disseldorp 	struct se_device *dev = t10_wwn->t10_dev;
127354a6f3f6SDavid Disseldorp 	/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
127454a6f3f6SDavid Disseldorp 	unsigned char buf[INQUIRY_VENDOR_LEN + 2];
127554a6f3f6SDavid Disseldorp 	char *stripped = NULL;
1276ee26724aSColin Ian King 	size_t len;
1277ee26724aSColin Ian King 	ssize_t ret;
127854a6f3f6SDavid Disseldorp 
127954a6f3f6SDavid Disseldorp 	len = strlcpy(buf, page, sizeof(buf));
128054a6f3f6SDavid Disseldorp 	if (len < sizeof(buf)) {
128154a6f3f6SDavid Disseldorp 		/* Strip any newline added from userspace. */
128254a6f3f6SDavid Disseldorp 		stripped = strstrip(buf);
128354a6f3f6SDavid Disseldorp 		len = strlen(stripped);
128454a6f3f6SDavid Disseldorp 	}
128554a6f3f6SDavid Disseldorp 	if (len > INQUIRY_VENDOR_LEN) {
128654a6f3f6SDavid Disseldorp 		pr_err("Emulated T10 Vendor Identification exceeds"
128754a6f3f6SDavid Disseldorp 			" INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)
128854a6f3f6SDavid Disseldorp 			"\n");
128954a6f3f6SDavid Disseldorp 		return -EOVERFLOW;
129054a6f3f6SDavid Disseldorp 	}
129154a6f3f6SDavid Disseldorp 
12920322913cSAlan Adamson 	ret = target_check_inquiry_data(stripped);
12930322913cSAlan Adamson 
12940322913cSAlan Adamson 	if (ret < 0)
12950322913cSAlan Adamson 		return ret;
129654a6f3f6SDavid Disseldorp 
129754a6f3f6SDavid Disseldorp 	/*
129854a6f3f6SDavid Disseldorp 	 * Check to see if any active exports exist.  If they do exist, fail
129954a6f3f6SDavid Disseldorp 	 * here as changing this information on the fly (underneath the
130054a6f3f6SDavid Disseldorp 	 * initiator side OS dependent multipath code) could cause negative
130154a6f3f6SDavid Disseldorp 	 * effects.
130254a6f3f6SDavid Disseldorp 	 */
130354a6f3f6SDavid Disseldorp 	if (dev->export_count) {
130454a6f3f6SDavid Disseldorp 		pr_err("Unable to set T10 Vendor Identification while"
130554a6f3f6SDavid Disseldorp 			" active %d exports exist\n", dev->export_count);
130654a6f3f6SDavid Disseldorp 		return -EINVAL;
130754a6f3f6SDavid Disseldorp 	}
130854a6f3f6SDavid Disseldorp 
130954a6f3f6SDavid Disseldorp 	BUILD_BUG_ON(sizeof(dev->t10_wwn.vendor) != INQUIRY_VENDOR_LEN + 1);
131054a6f3f6SDavid Disseldorp 	strlcpy(dev->t10_wwn.vendor, stripped, sizeof(dev->t10_wwn.vendor));
131154a6f3f6SDavid Disseldorp 
131254a6f3f6SDavid Disseldorp 	pr_debug("Target_Core_ConfigFS: Set emulated T10 Vendor Identification:"
131354a6f3f6SDavid Disseldorp 		 " %s\n", dev->t10_wwn.vendor);
131454a6f3f6SDavid Disseldorp 
131554a6f3f6SDavid Disseldorp 	return count;
131654a6f3f6SDavid Disseldorp }
131754a6f3f6SDavid Disseldorp 
13180322913cSAlan Adamson static ssize_t target_wwn_product_id_show(struct config_item *item,
13190322913cSAlan Adamson 		char *page)
13200322913cSAlan Adamson {
13210322913cSAlan Adamson 	return sprintf(page, "%s\n", &to_t10_wwn(item)->model[0]);
13220322913cSAlan Adamson }
13230322913cSAlan Adamson 
13240322913cSAlan Adamson static ssize_t target_wwn_product_id_store(struct config_item *item,
13250322913cSAlan Adamson 		const char *page, size_t count)
13260322913cSAlan Adamson {
13270322913cSAlan Adamson 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
13280322913cSAlan Adamson 	struct se_device *dev = t10_wwn->t10_dev;
13290322913cSAlan Adamson 	/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
13300322913cSAlan Adamson 	unsigned char buf[INQUIRY_MODEL_LEN + 2];
13310322913cSAlan Adamson 	char *stripped = NULL;
1332ee26724aSColin Ian King 	size_t len;
1333ee26724aSColin Ian King 	ssize_t ret;
13340322913cSAlan Adamson 
13350322913cSAlan Adamson 	len = strlcpy(buf, page, sizeof(buf));
13360322913cSAlan Adamson 	if (len < sizeof(buf)) {
13370322913cSAlan Adamson 		/* Strip any newline added from userspace. */
13380322913cSAlan Adamson 		stripped = strstrip(buf);
13390322913cSAlan Adamson 		len = strlen(stripped);
13400322913cSAlan Adamson 	}
13410322913cSAlan Adamson 	if (len > INQUIRY_MODEL_LEN) {
13420322913cSAlan Adamson 		pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: "
13430322913cSAlan Adamson 			 __stringify(INQUIRY_MODEL_LEN)
13440322913cSAlan Adamson 			"\n");
13450322913cSAlan Adamson 		return -EOVERFLOW;
13460322913cSAlan Adamson 	}
13470322913cSAlan Adamson 
13480322913cSAlan Adamson 	ret = target_check_inquiry_data(stripped);
13490322913cSAlan Adamson 
13500322913cSAlan Adamson 	if (ret < 0)
13510322913cSAlan Adamson 		return ret;
13520322913cSAlan Adamson 
13530322913cSAlan Adamson 	/*
13540322913cSAlan Adamson 	 * Check to see if any active exports exist.  If they do exist, fail
13550322913cSAlan Adamson 	 * here as changing this information on the fly (underneath the
13560322913cSAlan Adamson 	 * initiator side OS dependent multipath code) could cause negative
13570322913cSAlan Adamson 	 * effects.
13580322913cSAlan Adamson 	 */
13590322913cSAlan Adamson 	if (dev->export_count) {
13600322913cSAlan Adamson 		pr_err("Unable to set T10 Model while active %d exports exist\n",
13610322913cSAlan Adamson 			dev->export_count);
13620322913cSAlan Adamson 		return -EINVAL;
13630322913cSAlan Adamson 	}
13640322913cSAlan Adamson 
13650322913cSAlan Adamson 	BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
13660322913cSAlan Adamson 	strlcpy(dev->t10_wwn.model, stripped, sizeof(dev->t10_wwn.model));
13670322913cSAlan Adamson 
13680322913cSAlan Adamson 	pr_debug("Target_Core_ConfigFS: Set emulated T10 Model Identification: %s\n",
13690322913cSAlan Adamson 		 dev->t10_wwn.model);
13700322913cSAlan Adamson 
13710322913cSAlan Adamson 	return count;
13720322913cSAlan Adamson }
13730322913cSAlan Adamson 
13740322913cSAlan Adamson static ssize_t target_wwn_revision_show(struct config_item *item,
13750322913cSAlan Adamson 		char *page)
13760322913cSAlan Adamson {
13770322913cSAlan Adamson 	return sprintf(page, "%s\n", &to_t10_wwn(item)->revision[0]);
13780322913cSAlan Adamson }
13790322913cSAlan Adamson 
13800322913cSAlan Adamson static ssize_t target_wwn_revision_store(struct config_item *item,
13810322913cSAlan Adamson 		const char *page, size_t count)
13820322913cSAlan Adamson {
13830322913cSAlan Adamson 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
13840322913cSAlan Adamson 	struct se_device *dev = t10_wwn->t10_dev;
13850322913cSAlan Adamson 	/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
13860322913cSAlan Adamson 	unsigned char buf[INQUIRY_REVISION_LEN + 2];
13870322913cSAlan Adamson 	char *stripped = NULL;
1388ee26724aSColin Ian King 	size_t len;
1389ee26724aSColin Ian King 	ssize_t ret;
13900322913cSAlan Adamson 
13910322913cSAlan Adamson 	len = strlcpy(buf, page, sizeof(buf));
13920322913cSAlan Adamson 	if (len < sizeof(buf)) {
13930322913cSAlan Adamson 		/* Strip any newline added from userspace. */
13940322913cSAlan Adamson 		stripped = strstrip(buf);
13950322913cSAlan Adamson 		len = strlen(stripped);
13960322913cSAlan Adamson 	}
13970322913cSAlan Adamson 	if (len > INQUIRY_REVISION_LEN) {
13980322913cSAlan Adamson 		pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: "
13990322913cSAlan Adamson 			 __stringify(INQUIRY_REVISION_LEN)
14000322913cSAlan Adamson 			"\n");
14010322913cSAlan Adamson 		return -EOVERFLOW;
14020322913cSAlan Adamson 	}
14030322913cSAlan Adamson 
14040322913cSAlan Adamson 	ret = target_check_inquiry_data(stripped);
14050322913cSAlan Adamson 
14060322913cSAlan Adamson 	if (ret < 0)
14070322913cSAlan Adamson 		return ret;
14080322913cSAlan Adamson 
14090322913cSAlan Adamson 	/*
14100322913cSAlan Adamson 	 * Check to see if any active exports exist.  If they do exist, fail
14110322913cSAlan Adamson 	 * here as changing this information on the fly (underneath the
14120322913cSAlan Adamson 	 * initiator side OS dependent multipath code) could cause negative
14130322913cSAlan Adamson 	 * effects.
14140322913cSAlan Adamson 	 */
14150322913cSAlan Adamson 	if (dev->export_count) {
14160322913cSAlan Adamson 		pr_err("Unable to set T10 Revision while active %d exports exist\n",
14170322913cSAlan Adamson 			dev->export_count);
14180322913cSAlan Adamson 		return -EINVAL;
14190322913cSAlan Adamson 	}
14200322913cSAlan Adamson 
14210322913cSAlan Adamson 	BUILD_BUG_ON(sizeof(dev->t10_wwn.revision) != INQUIRY_REVISION_LEN + 1);
14220322913cSAlan Adamson 	strlcpy(dev->t10_wwn.revision, stripped, sizeof(dev->t10_wwn.revision));
14230322913cSAlan Adamson 
14240322913cSAlan Adamson 	pr_debug("Target_Core_ConfigFS: Set emulated T10 Revision: %s\n",
14250322913cSAlan Adamson 		 dev->t10_wwn.revision);
14260322913cSAlan Adamson 
14270322913cSAlan Adamson 	return count;
14280322913cSAlan Adamson }
14290322913cSAlan Adamson 
143054a6f3f6SDavid Disseldorp /*
1431c66ac9dbSNicholas Bellinger  * VPD page 0x80 Unit serial
1432c66ac9dbSNicholas Bellinger  */
14332eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1434c66ac9dbSNicholas Bellinger 		char *page)
1435c66ac9dbSNicholas Bellinger {
1436c66ac9dbSNicholas Bellinger 	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
14372eafd729SChristoph Hellwig 		&to_t10_wwn(item)->unit_serial[0]);
1438c66ac9dbSNicholas Bellinger }
1439c66ac9dbSNicholas Bellinger 
14402eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
14412eafd729SChristoph Hellwig 		const char *page, size_t count)
1442c66ac9dbSNicholas Bellinger {
14432eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
14440fd97ccfSChristoph Hellwig 	struct se_device *dev = t10_wwn->t10_dev;
1445c66ac9dbSNicholas Bellinger 	unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1446c66ac9dbSNicholas Bellinger 
1447c66ac9dbSNicholas Bellinger 	/*
1448c66ac9dbSNicholas Bellinger 	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1449c66ac9dbSNicholas Bellinger 	 * from the struct scsi_device level firmware, do not allow
1450c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial to be emulated.
1451c66ac9dbSNicholas Bellinger 	 *
1452c66ac9dbSNicholas Bellinger 	 * Note this struct scsi_device could also be emulating VPD
1453c66ac9dbSNicholas Bellinger 	 * information from its drivers/scsi LLD.  But for now we assume
1454c66ac9dbSNicholas Bellinger 	 * it is doing 'the right thing' wrt a world wide unique
1455c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial Number that OS dependent multipath can depend on.
1456c66ac9dbSNicholas Bellinger 	 */
14570fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
14586708bb27SAndy Grover 		pr_err("Underlying SCSI device firmware provided VPD"
1459c66ac9dbSNicholas Bellinger 			" Unit Serial, ignoring request\n");
1460c66ac9dbSNicholas Bellinger 		return -EOPNOTSUPP;
1461c66ac9dbSNicholas Bellinger 	}
1462c66ac9dbSNicholas Bellinger 
146360d645a4SDan Carpenter 	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
14646708bb27SAndy Grover 		pr_err("Emulated VPD Unit Serial exceeds"
1465c66ac9dbSNicholas Bellinger 		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1466c66ac9dbSNicholas Bellinger 		return -EOVERFLOW;
1467c66ac9dbSNicholas Bellinger 	}
1468c66ac9dbSNicholas Bellinger 	/*
1469c66ac9dbSNicholas Bellinger 	 * Check to see if any active $FABRIC_MOD exports exist.  If they
1470c66ac9dbSNicholas Bellinger 	 * do exist, fail here as changing this information on the fly
1471c66ac9dbSNicholas Bellinger 	 * (underneath the initiator side OS dependent multipath code)
1472c66ac9dbSNicholas Bellinger 	 * could cause negative effects.
1473c66ac9dbSNicholas Bellinger 	 */
14740fd97ccfSChristoph Hellwig 	if (dev->export_count) {
14756708bb27SAndy Grover 		pr_err("Unable to set VPD Unit Serial while"
1476c66ac9dbSNicholas Bellinger 			" active %d $FABRIC_MOD exports exist\n",
14770fd97ccfSChristoph Hellwig 			dev->export_count);
1478c66ac9dbSNicholas Bellinger 		return -EINVAL;
1479c66ac9dbSNicholas Bellinger 	}
14800fd97ccfSChristoph Hellwig 
1481c66ac9dbSNicholas Bellinger 	/*
1482c66ac9dbSNicholas Bellinger 	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1483c66ac9dbSNicholas Bellinger 	 *
1484c66ac9dbSNicholas Bellinger 	 * Also, strip any newline added from the userspace
1485c66ac9dbSNicholas Bellinger 	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1486c66ac9dbSNicholas Bellinger 	 */
1487c66ac9dbSNicholas Bellinger 	memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1488c66ac9dbSNicholas Bellinger 	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
14890fd97ccfSChristoph Hellwig 	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1490c66ac9dbSNicholas Bellinger 			"%s", strstrip(buf));
14910fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1492c66ac9dbSNicholas Bellinger 
14936708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
14940fd97ccfSChristoph Hellwig 			" %s\n", dev->t10_wwn.unit_serial);
1495c66ac9dbSNicholas Bellinger 
1496c66ac9dbSNicholas Bellinger 	return count;
1497c66ac9dbSNicholas Bellinger }
1498c66ac9dbSNicholas Bellinger 
1499c66ac9dbSNicholas Bellinger /*
1500c66ac9dbSNicholas Bellinger  * VPD page 0x83 Protocol Identifier
1501c66ac9dbSNicholas Bellinger  */
15022eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1503c66ac9dbSNicholas Bellinger 		char *page)
1504c66ac9dbSNicholas Bellinger {
15052eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1506c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;
1507c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];
1508c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1509c66ac9dbSNicholas Bellinger 
1510c66ac9dbSNicholas Bellinger 	memset(buf, 0, VPD_TMP_BUF_SIZE);
1511c66ac9dbSNicholas Bellinger 
1512c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);
1513c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
15146708bb27SAndy Grover 		if (!vpd->protocol_identifier_set)
1515c66ac9dbSNicholas Bellinger 			continue;
1516c66ac9dbSNicholas Bellinger 
1517c66ac9dbSNicholas Bellinger 		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1518c66ac9dbSNicholas Bellinger 
15196708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1520c66ac9dbSNicholas Bellinger 			break;
1521c66ac9dbSNicholas Bellinger 
1522c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1523c66ac9dbSNicholas Bellinger 	}
1524c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);
1525c66ac9dbSNicholas Bellinger 
1526c66ac9dbSNicholas Bellinger 	return len;
1527c66ac9dbSNicholas Bellinger }
1528c66ac9dbSNicholas Bellinger 
1529c66ac9dbSNicholas Bellinger /*
1530c66ac9dbSNicholas Bellinger  * Generic wrapper for dumping VPD identifiers by association.
1531c66ac9dbSNicholas Bellinger  */
1532c66ac9dbSNicholas Bellinger #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
15332eafd729SChristoph Hellwig static ssize_t target_wwn_##_name##_show(struct config_item *item,	\
1534c66ac9dbSNicholas Bellinger 		char *page)						\
1535c66ac9dbSNicholas Bellinger {									\
15362eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);			\
1537c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;						\
1538c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];				\
1539c66ac9dbSNicholas Bellinger 	ssize_t len = 0;						\
1540c66ac9dbSNicholas Bellinger 									\
1541c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);				\
1542c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
1543c66ac9dbSNicholas Bellinger 		if (vpd->association != _assoc)				\
1544c66ac9dbSNicholas Bellinger 			continue;					\
1545c66ac9dbSNicholas Bellinger 									\
1546c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1547c66ac9dbSNicholas Bellinger 		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
15486708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1549c66ac9dbSNicholas Bellinger 			break;						\
1550c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1551c66ac9dbSNicholas Bellinger 									\
1552c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1553c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
15546708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1555c66ac9dbSNicholas Bellinger 			break;						\
1556c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1557c66ac9dbSNicholas Bellinger 									\
1558c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1559c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
15606708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1561c66ac9dbSNicholas Bellinger 			break;						\
1562c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1563c66ac9dbSNicholas Bellinger 	}								\
1564c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);				\
1565c66ac9dbSNicholas Bellinger 									\
1566c66ac9dbSNicholas Bellinger 	return len;							\
1567c66ac9dbSNicholas Bellinger }
1568c66ac9dbSNicholas Bellinger 
15692eafd729SChristoph Hellwig /* VPD page 0x83 Association: Logical Unit */
1570c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
15712eafd729SChristoph Hellwig /* VPD page 0x83 Association: Target Port */
1572c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
15732eafd729SChristoph Hellwig /* VPD page 0x83 Association: SCSI Target Device */
1574c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1575c66ac9dbSNicholas Bellinger 
157654a6f3f6SDavid Disseldorp CONFIGFS_ATTR(target_wwn_, vendor_id);
15770322913cSAlan Adamson CONFIGFS_ATTR(target_wwn_, product_id);
15780322913cSAlan Adamson CONFIGFS_ATTR(target_wwn_, revision);
15792eafd729SChristoph Hellwig CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
15802eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
15812eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
15822eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
15832eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1584c66ac9dbSNicholas Bellinger 
1585c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
158654a6f3f6SDavid Disseldorp 	&target_wwn_attr_vendor_id,
15870322913cSAlan Adamson 	&target_wwn_attr_product_id,
15880322913cSAlan Adamson 	&target_wwn_attr_revision,
15892eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_unit_serial,
15902eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_protocol_identifier,
15912eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_logical_unit,
15922eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_target_port,
15932eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_scsi_target_device,
1594c66ac9dbSNicholas Bellinger 	NULL,
1595c66ac9dbSNicholas Bellinger };
1596c66ac9dbSNicholas Bellinger 
15972eafd729SChristoph Hellwig TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1598c66ac9dbSNicholas Bellinger 
1599f8d389c6SNicholas Bellinger /*  End functions for struct config_item_type tb_dev_wwn_cit */
1600c66ac9dbSNicholas Bellinger 
160191e2e39bSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_pr_cit */
1602c66ac9dbSNicholas Bellinger 
16032eafd729SChristoph Hellwig static struct se_device *pr_to_dev(struct config_item *item)
16042eafd729SChristoph Hellwig {
16052eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device,
16062eafd729SChristoph Hellwig 			dev_pr_group);
16072eafd729SChristoph Hellwig }
1608c66ac9dbSNicholas Bellinger 
1609d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1610d977f437SChristoph Hellwig 		char *page)
1611c66ac9dbSNicholas Bellinger {
1612c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1613c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1614c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1615c66ac9dbSNicholas Bellinger 
1616c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1617c66ac9dbSNicholas Bellinger 
1618c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1619d977f437SChristoph Hellwig 	if (!pr_reg)
1620d977f437SChristoph Hellwig 		return sprintf(page, "No SPC-3 Reservation holder\n");
1621d977f437SChristoph Hellwig 
1622c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1623d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1624c66ac9dbSNicholas Bellinger 
1625d977f437SChristoph Hellwig 	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
162630c7ca93SDavid Disseldorp 		se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1627d2843c17SAndy Grover 		se_nacl->initiatorname, i_buf);
1628c66ac9dbSNicholas Bellinger }
1629c66ac9dbSNicholas Bellinger 
1630d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1631d977f437SChristoph Hellwig 		char *page)
1632c66ac9dbSNicholas Bellinger {
1633fae43461SBart Van Assche 	struct se_session *sess = dev->reservation_holder;
1634c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1635d977f437SChristoph Hellwig 	ssize_t len;
1636c66ac9dbSNicholas Bellinger 
1637fae43461SBart Van Assche 	if (sess) {
1638fae43461SBart Van Assche 		se_nacl = sess->se_node_acl;
1639d977f437SChristoph Hellwig 		len = sprintf(page,
1640d977f437SChristoph Hellwig 			      "SPC-2 Reservation: %s Initiator: %s\n",
164130c7ca93SDavid Disseldorp 			      se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1642c66ac9dbSNicholas Bellinger 			      se_nacl->initiatorname);
1643d977f437SChristoph Hellwig 	} else {
1644d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-2 Reservation holder\n");
1645d977f437SChristoph Hellwig 	}
1646d977f437SChristoph Hellwig 	return len;
1647c66ac9dbSNicholas Bellinger }
1648c66ac9dbSNicholas Bellinger 
16492eafd729SChristoph Hellwig static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1650c66ac9dbSNicholas Bellinger {
16512eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1652d977f437SChristoph Hellwig 	int ret;
1653c66ac9dbSNicholas Bellinger 
1654b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr)
1655b49d6f78SDavid Disseldorp 		return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1656b49d6f78SDavid Disseldorp 
16574ec5bf0eSBryant G. Ly 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1658d977f437SChristoph Hellwig 		return sprintf(page, "Passthrough\n");
1659c66ac9dbSNicholas Bellinger 
1660d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
1661d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1662d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc2_res(dev, page);
1663d977f437SChristoph Hellwig 	else
1664d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc3_res(dev, page);
1665d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1666d977f437SChristoph Hellwig 	return ret;
1667c66ac9dbSNicholas Bellinger }
1668c66ac9dbSNicholas Bellinger 
16692eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
16702eafd729SChristoph Hellwig 		char *page)
1671c66ac9dbSNicholas Bellinger {
16722eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1673c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1674c66ac9dbSNicholas Bellinger 
1675c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1676d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder) {
1677c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1678d977f437SChristoph Hellwig 	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1679c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: All Target"
1680c66ac9dbSNicholas Bellinger 			" Ports registration\n");
1681d977f437SChristoph Hellwig 	} else {
1682c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: Single"
1683c66ac9dbSNicholas Bellinger 			" Target Port registration\n");
1684d977f437SChristoph Hellwig 	}
1685c66ac9dbSNicholas Bellinger 
1686d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1687c66ac9dbSNicholas Bellinger 	return len;
1688c66ac9dbSNicholas Bellinger }
1689c66ac9dbSNicholas Bellinger 
16902eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
16912eafd729SChristoph Hellwig 		char *page)
1692c66ac9dbSNicholas Bellinger {
16932eafd729SChristoph Hellwig 	return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1694c66ac9dbSNicholas Bellinger }
1695c66ac9dbSNicholas Bellinger 
1696c66ac9dbSNicholas Bellinger 
16972eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
16982eafd729SChristoph Hellwig 		char *page)
1699c66ac9dbSNicholas Bellinger {
17002eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1701c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1702c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
1703c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
17049ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1705c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1706c66ac9dbSNicholas Bellinger 
1707c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1708c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
17096708bb27SAndy Grover 	if (!pr_reg) {
1710c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1711d977f437SChristoph Hellwig 		goto out_unlock;
1712c66ac9dbSNicholas Bellinger 	}
1713d977f437SChristoph Hellwig 
1714c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1715c66ac9dbSNicholas Bellinger 	se_tpg = se_nacl->se_tpg;
1716e3d6f909SAndy Grover 	tfo = se_tpg->se_tpg_tfo;
1717c66ac9dbSNicholas Bellinger 
1718c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: %s"
171930c7ca93SDavid Disseldorp 		" Target Node Endpoint: %s\n", tfo->fabric_name,
1720c66ac9dbSNicholas Bellinger 		tfo->tpg_get_wwn(se_tpg));
1721c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
172235d1efe8SMasanari Iida 		" Identifier Tag: %hu %s Portal Group Tag: %hu"
1723f2d30680SHannes Reinecke 		" %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
172430c7ca93SDavid Disseldorp 		tfo->fabric_name, tfo->tpg_get_tag(se_tpg),
172530c7ca93SDavid Disseldorp 		tfo->fabric_name, pr_reg->pr_aptpl_target_lun);
1726c66ac9dbSNicholas Bellinger 
1727d977f437SChristoph Hellwig out_unlock:
1728d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1729c66ac9dbSNicholas Bellinger 	return len;
1730c66ac9dbSNicholas Bellinger }
1731c66ac9dbSNicholas Bellinger 
1732c66ac9dbSNicholas Bellinger 
17332eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
17342eafd729SChristoph Hellwig 		char *page)
1735c66ac9dbSNicholas Bellinger {
17362eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
17379ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1738c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1739c66ac9dbSNicholas Bellinger 	unsigned char buf[384];
1740c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1741c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1742d2843c17SAndy Grover 	int reg_count = 0;
1743c66ac9dbSNicholas Bellinger 
1744c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1745c66ac9dbSNicholas Bellinger 
17460fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
17470fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1748c66ac9dbSNicholas Bellinger 			pr_reg_list) {
1749c66ac9dbSNicholas Bellinger 
1750c66ac9dbSNicholas Bellinger 		memset(buf, 0, 384);
1751c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1752c66ac9dbSNicholas Bellinger 		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1753d2843c17SAndy Grover 		core_pr_dump_initiator_port(pr_reg, i_buf,
1754c66ac9dbSNicholas Bellinger 					PR_REG_ISID_ID_LEN);
1755c66ac9dbSNicholas Bellinger 		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
175630c7ca93SDavid Disseldorp 			tfo->fabric_name,
1757d2843c17SAndy Grover 			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1758c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_generation);
1759c66ac9dbSNicholas Bellinger 
17606708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1761c66ac9dbSNicholas Bellinger 			break;
1762c66ac9dbSNicholas Bellinger 
1763c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1764c66ac9dbSNicholas Bellinger 		reg_count++;
1765c66ac9dbSNicholas Bellinger 	}
17660fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
1767c66ac9dbSNicholas Bellinger 
17686708bb27SAndy Grover 	if (!reg_count)
1769c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "None\n");
1770c66ac9dbSNicholas Bellinger 
1771c66ac9dbSNicholas Bellinger 	return len;
1772c66ac9dbSNicholas Bellinger }
1773c66ac9dbSNicholas Bellinger 
17742eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1775c66ac9dbSNicholas Bellinger {
17762eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1777c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1778c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1779c66ac9dbSNicholas Bellinger 
1780c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1781c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1782d977f437SChristoph Hellwig 	if (pr_reg) {
1783c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1784c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1785d977f437SChristoph Hellwig 	} else {
1786d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1787d977f437SChristoph Hellwig 	}
1788c66ac9dbSNicholas Bellinger 
1789d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1790c66ac9dbSNicholas Bellinger 	return len;
1791c66ac9dbSNicholas Bellinger }
1792c66ac9dbSNicholas Bellinger 
17932eafd729SChristoph Hellwig static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
1794c66ac9dbSNicholas Bellinger {
17952eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
17962eafd729SChristoph Hellwig 
1797b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr)
1798b49d6f78SDavid Disseldorp 		return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
17994ec5bf0eSBryant G. Ly 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1800d977f437SChristoph Hellwig 		return sprintf(page, "SPC_PASSTHROUGH\n");
1801b49d6f78SDavid Disseldorp 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1802d977f437SChristoph Hellwig 		return sprintf(page, "SPC2_RESERVATIONS\n");
1803b49d6f78SDavid Disseldorp 
1804d977f437SChristoph Hellwig 	return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1805c66ac9dbSNicholas Bellinger }
1806c66ac9dbSNicholas Bellinger 
18072eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
18082eafd729SChristoph Hellwig 		char *page)
1809c66ac9dbSNicholas Bellinger {
18102eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
18112eafd729SChristoph Hellwig 
1812b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
1813b49d6f78SDavid Disseldorp 	    (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1814c66ac9dbSNicholas Bellinger 		return 0;
1815c66ac9dbSNicholas Bellinger 
1816c66ac9dbSNicholas Bellinger 	return sprintf(page, "APTPL Bit Status: %s\n",
18170fd97ccfSChristoph Hellwig 		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1818c66ac9dbSNicholas Bellinger }
1819c66ac9dbSNicholas Bellinger 
18202eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
18212eafd729SChristoph Hellwig 		char *page)
1822c66ac9dbSNicholas Bellinger {
18232eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
18242eafd729SChristoph Hellwig 
1825b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
1826b49d6f78SDavid Disseldorp 	    (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1827c66ac9dbSNicholas Bellinger 		return 0;
1828c66ac9dbSNicholas Bellinger 
1829c66ac9dbSNicholas Bellinger 	return sprintf(page, "Ready to process PR APTPL metadata..\n");
1830c66ac9dbSNicholas Bellinger }
1831c66ac9dbSNicholas Bellinger 
1832c66ac9dbSNicholas Bellinger enum {
1833c66ac9dbSNicholas Bellinger 	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1834c66ac9dbSNicholas Bellinger 	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1835c66ac9dbSNicholas Bellinger 	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1836c66ac9dbSNicholas Bellinger 	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1837c66ac9dbSNicholas Bellinger };
1838c66ac9dbSNicholas Bellinger 
1839c66ac9dbSNicholas Bellinger static match_table_t tokens = {
1840c66ac9dbSNicholas Bellinger 	{Opt_initiator_fabric, "initiator_fabric=%s"},
1841c66ac9dbSNicholas Bellinger 	{Opt_initiator_node, "initiator_node=%s"},
1842c66ac9dbSNicholas Bellinger 	{Opt_initiator_sid, "initiator_sid=%s"},
1843c66ac9dbSNicholas Bellinger 	{Opt_sa_res_key, "sa_res_key=%s"},
1844c66ac9dbSNicholas Bellinger 	{Opt_res_holder, "res_holder=%d"},
1845c66ac9dbSNicholas Bellinger 	{Opt_res_type, "res_type=%d"},
1846c66ac9dbSNicholas Bellinger 	{Opt_res_scope, "res_scope=%d"},
1847c66ac9dbSNicholas Bellinger 	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1848a2db857bStangwenji 	{Opt_mapped_lun, "mapped_lun=%u"},
1849c66ac9dbSNicholas Bellinger 	{Opt_target_fabric, "target_fabric=%s"},
1850c66ac9dbSNicholas Bellinger 	{Opt_target_node, "target_node=%s"},
1851c66ac9dbSNicholas Bellinger 	{Opt_tpgt, "tpgt=%d"},
1852c66ac9dbSNicholas Bellinger 	{Opt_port_rtpi, "port_rtpi=%d"},
1853a2db857bStangwenji 	{Opt_target_lun, "target_lun=%u"},
1854c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
1855c66ac9dbSNicholas Bellinger };
1856c66ac9dbSNicholas Bellinger 
18572eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
18582eafd729SChristoph Hellwig 		const char *page, size_t count)
1859c66ac9dbSNicholas Bellinger {
18602eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
18616d180253SJesper Juhl 	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
18626d180253SJesper Juhl 	unsigned char *t_fabric = NULL, *t_port = NULL;
18638d213559SJoern Engel 	char *orig, *ptr, *opts;
1864c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
1865c66ac9dbSNicholas Bellinger 	unsigned long long tmp_ll;
1866c66ac9dbSNicholas Bellinger 	u64 sa_res_key = 0;
1867f2d30680SHannes Reinecke 	u64 mapped_lun = 0, target_lun = 0;
1868c66ac9dbSNicholas Bellinger 	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
186945fb94c2SBart Van Assche 	u16 tpgt = 0;
187045fb94c2SBart Van Assche 	u8 type = 0;
1871c66ac9dbSNicholas Bellinger 
1872b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
1873b49d6f78SDavid Disseldorp 	    (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
18749105bfc0SAndy Grover 		return count;
1875d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
18769105bfc0SAndy Grover 		return count;
1877c66ac9dbSNicholas Bellinger 
18780fd97ccfSChristoph Hellwig 	if (dev->export_count) {
18796708bb27SAndy Grover 		pr_debug("Unable to process APTPL metadata while"
1880c66ac9dbSNicholas Bellinger 			" active fabric exports exist\n");
1881c66ac9dbSNicholas Bellinger 		return -EINVAL;
1882c66ac9dbSNicholas Bellinger 	}
1883c66ac9dbSNicholas Bellinger 
1884c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
1885c66ac9dbSNicholas Bellinger 	if (!opts)
1886c66ac9dbSNicholas Bellinger 		return -ENOMEM;
1887c66ac9dbSNicholas Bellinger 
1888c66ac9dbSNicholas Bellinger 	orig = opts;
188990c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
1890c66ac9dbSNicholas Bellinger 		if (!*ptr)
1891c66ac9dbSNicholas Bellinger 			continue;
1892c66ac9dbSNicholas Bellinger 
1893c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
1894c66ac9dbSNicholas Bellinger 		switch (token) {
1895c66ac9dbSNicholas Bellinger 		case Opt_initiator_fabric:
18968d213559SJoern Engel 			i_fabric = match_strdup(args);
18976d180253SJesper Juhl 			if (!i_fabric) {
18986d180253SJesper Juhl 				ret = -ENOMEM;
18996d180253SJesper Juhl 				goto out;
19006d180253SJesper Juhl 			}
1901c66ac9dbSNicholas Bellinger 			break;
1902c66ac9dbSNicholas Bellinger 		case Opt_initiator_node:
19038d213559SJoern Engel 			i_port = match_strdup(args);
19046d180253SJesper Juhl 			if (!i_port) {
19056d180253SJesper Juhl 				ret = -ENOMEM;
19066d180253SJesper Juhl 				goto out;
19076d180253SJesper Juhl 			}
190860d645a4SDan Carpenter 			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
19096708bb27SAndy Grover 				pr_err("APTPL metadata initiator_node="
1910c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1911c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_IPORT_LEN);
1912c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1913c66ac9dbSNicholas Bellinger 				break;
1914c66ac9dbSNicholas Bellinger 			}
1915c66ac9dbSNicholas Bellinger 			break;
1916c66ac9dbSNicholas Bellinger 		case Opt_initiator_sid:
19178d213559SJoern Engel 			isid = match_strdup(args);
19186d180253SJesper Juhl 			if (!isid) {
19196d180253SJesper Juhl 				ret = -ENOMEM;
19206d180253SJesper Juhl 				goto out;
19216d180253SJesper Juhl 			}
192260d645a4SDan Carpenter 			if (strlen(isid) >= PR_REG_ISID_LEN) {
19236708bb27SAndy Grover 				pr_err("APTPL metadata initiator_isid"
1924c66ac9dbSNicholas Bellinger 					"= exceeds PR_REG_ISID_LEN: %d\n",
1925c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
1926c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1927c66ac9dbSNicholas Bellinger 				break;
1928c66ac9dbSNicholas Bellinger 			}
1929c66ac9dbSNicholas Bellinger 			break;
1930c66ac9dbSNicholas Bellinger 		case Opt_sa_res_key:
1931a2db857bStangwenji 			ret = match_u64(args,  &tmp_ll);
1932c66ac9dbSNicholas Bellinger 			if (ret < 0) {
19338d213559SJoern Engel 				pr_err("kstrtoull() failed for sa_res_key=\n");
1934c66ac9dbSNicholas Bellinger 				goto out;
1935c66ac9dbSNicholas Bellinger 			}
1936c66ac9dbSNicholas Bellinger 			sa_res_key = (u64)tmp_ll;
1937c66ac9dbSNicholas Bellinger 			break;
1938c66ac9dbSNicholas Bellinger 		/*
1939c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Reservation
1940c66ac9dbSNicholas Bellinger 		 */
1941c66ac9dbSNicholas Bellinger 		case Opt_res_holder:
1942c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1943c2091026SDavid Disseldorp 			if (ret)
1944c2091026SDavid Disseldorp 				goto out;
1945c66ac9dbSNicholas Bellinger 			res_holder = arg;
1946c66ac9dbSNicholas Bellinger 			break;
1947c66ac9dbSNicholas Bellinger 		case Opt_res_type:
1948c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1949c2091026SDavid Disseldorp 			if (ret)
1950c2091026SDavid Disseldorp 				goto out;
1951c66ac9dbSNicholas Bellinger 			type = (u8)arg;
1952c66ac9dbSNicholas Bellinger 			break;
1953c66ac9dbSNicholas Bellinger 		case Opt_res_scope:
1954c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1955c2091026SDavid Disseldorp 			if (ret)
1956c2091026SDavid Disseldorp 				goto out;
1957c66ac9dbSNicholas Bellinger 			break;
1958c66ac9dbSNicholas Bellinger 		case Opt_res_all_tg_pt:
1959c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1960c2091026SDavid Disseldorp 			if (ret)
1961c2091026SDavid Disseldorp 				goto out;
1962c66ac9dbSNicholas Bellinger 			all_tg_pt = (int)arg;
1963c66ac9dbSNicholas Bellinger 			break;
1964c66ac9dbSNicholas Bellinger 		case Opt_mapped_lun:
1965a2db857bStangwenji 			ret = match_u64(args, &tmp_ll);
1966c2091026SDavid Disseldorp 			if (ret)
1967c2091026SDavid Disseldorp 				goto out;
1968a2db857bStangwenji 			mapped_lun = (u64)tmp_ll;
1969c66ac9dbSNicholas Bellinger 			break;
1970c66ac9dbSNicholas Bellinger 		/*
1971c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Target Port
1972c66ac9dbSNicholas Bellinger 		 */
1973c66ac9dbSNicholas Bellinger 		case Opt_target_fabric:
19748d213559SJoern Engel 			t_fabric = match_strdup(args);
19756d180253SJesper Juhl 			if (!t_fabric) {
19766d180253SJesper Juhl 				ret = -ENOMEM;
19776d180253SJesper Juhl 				goto out;
19786d180253SJesper Juhl 			}
1979c66ac9dbSNicholas Bellinger 			break;
1980c66ac9dbSNicholas Bellinger 		case Opt_target_node:
19818d213559SJoern Engel 			t_port = match_strdup(args);
19826d180253SJesper Juhl 			if (!t_port) {
19836d180253SJesper Juhl 				ret = -ENOMEM;
19846d180253SJesper Juhl 				goto out;
19856d180253SJesper Juhl 			}
198660d645a4SDan Carpenter 			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
19876708bb27SAndy Grover 				pr_err("APTPL metadata target_node="
1988c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1989c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_TPORT_LEN);
1990c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1991c66ac9dbSNicholas Bellinger 				break;
1992c66ac9dbSNicholas Bellinger 			}
1993c66ac9dbSNicholas Bellinger 			break;
1994c66ac9dbSNicholas Bellinger 		case Opt_tpgt:
1995c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1996c2091026SDavid Disseldorp 			if (ret)
1997c2091026SDavid Disseldorp 				goto out;
1998c66ac9dbSNicholas Bellinger 			tpgt = (u16)arg;
1999c66ac9dbSNicholas Bellinger 			break;
2000c66ac9dbSNicholas Bellinger 		case Opt_port_rtpi:
2001c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
2002c2091026SDavid Disseldorp 			if (ret)
2003c2091026SDavid Disseldorp 				goto out;
2004c66ac9dbSNicholas Bellinger 			break;
2005c66ac9dbSNicholas Bellinger 		case Opt_target_lun:
2006a2db857bStangwenji 			ret = match_u64(args, &tmp_ll);
2007c2091026SDavid Disseldorp 			if (ret)
2008c2091026SDavid Disseldorp 				goto out;
2009a2db857bStangwenji 			target_lun = (u64)tmp_ll;
2010c66ac9dbSNicholas Bellinger 			break;
2011c66ac9dbSNicholas Bellinger 		default:
2012c66ac9dbSNicholas Bellinger 			break;
2013c66ac9dbSNicholas Bellinger 		}
2014c66ac9dbSNicholas Bellinger 	}
2015c66ac9dbSNicholas Bellinger 
20166708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
20176708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
2018c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
2019c66ac9dbSNicholas Bellinger 		goto out;
2020c66ac9dbSNicholas Bellinger 	}
2021c66ac9dbSNicholas Bellinger 
2022c66ac9dbSNicholas Bellinger 	if (res_holder && !(type)) {
20236708bb27SAndy Grover 		pr_err("Illegal PR type: 0x%02x for reservation"
2024c66ac9dbSNicholas Bellinger 				" holder\n", type);
2025c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
2026c66ac9dbSNicholas Bellinger 		goto out;
2027c66ac9dbSNicholas Bellinger 	}
2028c66ac9dbSNicholas Bellinger 
20290fd97ccfSChristoph Hellwig 	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
2030c66ac9dbSNicholas Bellinger 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
2031c66ac9dbSNicholas Bellinger 			res_holder, all_tg_pt, type);
2032c66ac9dbSNicholas Bellinger out:
20336d180253SJesper Juhl 	kfree(i_fabric);
20346d180253SJesper Juhl 	kfree(i_port);
20356d180253SJesper Juhl 	kfree(isid);
20366d180253SJesper Juhl 	kfree(t_fabric);
20376d180253SJesper Juhl 	kfree(t_port);
2038c66ac9dbSNicholas Bellinger 	kfree(orig);
2039c66ac9dbSNicholas Bellinger 	return (ret == 0) ? count : ret;
2040c66ac9dbSNicholas Bellinger }
2041c66ac9dbSNicholas Bellinger 
2042c66ac9dbSNicholas Bellinger 
20432eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_holder);
20442eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
20452eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
20462eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
20472eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
20482eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
20492eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_type);
20502eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
20512eafd729SChristoph Hellwig CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
2052c66ac9dbSNicholas Bellinger 
2053c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_pr_attrs[] = {
20542eafd729SChristoph Hellwig 	&target_pr_attr_res_holder,
20552eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_all_tgt_pts,
20562eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_generation,
20572eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_holder_tg_port,
20582eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_registered_i_pts,
20592eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_type,
20602eafd729SChristoph Hellwig 	&target_pr_attr_res_type,
20612eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_active,
20622eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_metadata,
2063c66ac9dbSNicholas Bellinger 	NULL,
2064c66ac9dbSNicholas Bellinger };
2065c66ac9dbSNicholas Bellinger 
20662eafd729SChristoph Hellwig TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
2067c66ac9dbSNicholas Bellinger 
206891e2e39bSNicholas Bellinger /*  End functions for struct config_item_type tb_dev_pr_cit */
2069c66ac9dbSNicholas Bellinger 
207073112edcSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_cit */
2071c66ac9dbSNicholas Bellinger 
20722eafd729SChristoph Hellwig static inline struct se_device *to_device(struct config_item *item)
2073c66ac9dbSNicholas Bellinger {
20742eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device, dev_group);
20752eafd729SChristoph Hellwig }
20762eafd729SChristoph Hellwig 
20772eafd729SChristoph Hellwig static ssize_t target_dev_info_show(struct config_item *item, char *page)
20782eafd729SChristoph Hellwig {
20792eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2080c66ac9dbSNicholas Bellinger 	int bl = 0;
2081c66ac9dbSNicholas Bellinger 	ssize_t read_bytes = 0;
2082c66ac9dbSNicholas Bellinger 
20830fd97ccfSChristoph Hellwig 	transport_dump_dev_state(dev, page, &bl);
2084c66ac9dbSNicholas Bellinger 	read_bytes += bl;
20850a06d430SChristoph Hellwig 	read_bytes += dev->transport->show_configfs_dev_params(dev,
20860a06d430SChristoph Hellwig 			page+read_bytes);
2087c66ac9dbSNicholas Bellinger 	return read_bytes;
2088c66ac9dbSNicholas Bellinger }
2089c66ac9dbSNicholas Bellinger 
20902eafd729SChristoph Hellwig static ssize_t target_dev_control_store(struct config_item *item,
20912eafd729SChristoph Hellwig 		const char *page, size_t count)
2092c66ac9dbSNicholas Bellinger {
20932eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2094c66ac9dbSNicholas Bellinger 
20950a06d430SChristoph Hellwig 	return dev->transport->set_configfs_dev_params(dev, page, count);
2096c66ac9dbSNicholas Bellinger }
2097c66ac9dbSNicholas Bellinger 
20982eafd729SChristoph Hellwig static ssize_t target_dev_alias_show(struct config_item *item, char *page)
2099c66ac9dbSNicholas Bellinger {
21002eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2101c66ac9dbSNicholas Bellinger 
21020fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_ALIAS))
2103c66ac9dbSNicholas Bellinger 		return 0;
2104c66ac9dbSNicholas Bellinger 
21050fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
2106c66ac9dbSNicholas Bellinger }
2107c66ac9dbSNicholas Bellinger 
21082eafd729SChristoph Hellwig static ssize_t target_dev_alias_store(struct config_item *item,
21092eafd729SChristoph Hellwig 		const char *page, size_t count)
2110c66ac9dbSNicholas Bellinger {
21112eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
21120fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
2113c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
2114c66ac9dbSNicholas Bellinger 
2115c66ac9dbSNicholas Bellinger 	if (count > (SE_DEV_ALIAS_LEN-1)) {
21166708bb27SAndy Grover 		pr_err("alias count: %d exceeds"
2117c66ac9dbSNicholas Bellinger 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
2118c66ac9dbSNicholas Bellinger 			SE_DEV_ALIAS_LEN-1);
2119c66ac9dbSNicholas Bellinger 		return -EINVAL;
2120c66ac9dbSNicholas Bellinger 	}
2121c66ac9dbSNicholas Bellinger 
21220fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
21233011684cSDan Carpenter 	if (!read_bytes)
21243011684cSDan Carpenter 		return -EINVAL;
21250fd97ccfSChristoph Hellwig 	if (dev->dev_alias[read_bytes - 1] == '\n')
21260fd97ccfSChristoph Hellwig 		dev->dev_alias[read_bytes - 1] = '\0';
21270877eafdSSebastian Andrzej Siewior 
21280fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_ALIAS;
21293011684cSDan Carpenter 
21306708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
2131c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
21320fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
21330fd97ccfSChristoph Hellwig 		dev->dev_alias);
2134c66ac9dbSNicholas Bellinger 
2135c66ac9dbSNicholas Bellinger 	return read_bytes;
2136c66ac9dbSNicholas Bellinger }
2137c66ac9dbSNicholas Bellinger 
21382eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
2139c66ac9dbSNicholas Bellinger {
21402eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2141c66ac9dbSNicholas Bellinger 
21420fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
2143c66ac9dbSNicholas Bellinger 		return 0;
2144c66ac9dbSNicholas Bellinger 
21450fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
2146c66ac9dbSNicholas Bellinger }
2147c66ac9dbSNicholas Bellinger 
21482eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_store(struct config_item *item,
21492eafd729SChristoph Hellwig 		const char *page, size_t count)
2150c66ac9dbSNicholas Bellinger {
21512eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
21520fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
2153c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
2154c66ac9dbSNicholas Bellinger 
2155c66ac9dbSNicholas Bellinger 	if (count > (SE_UDEV_PATH_LEN-1)) {
21566708bb27SAndy Grover 		pr_err("udev_path count: %d exceeds"
2157c66ac9dbSNicholas Bellinger 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
2158c66ac9dbSNicholas Bellinger 			SE_UDEV_PATH_LEN-1);
2159c66ac9dbSNicholas Bellinger 		return -EINVAL;
2160c66ac9dbSNicholas Bellinger 	}
2161c66ac9dbSNicholas Bellinger 
21620fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
2163c66ac9dbSNicholas Bellinger 			"%s", page);
21643011684cSDan Carpenter 	if (!read_bytes)
21653011684cSDan Carpenter 		return -EINVAL;
21660fd97ccfSChristoph Hellwig 	if (dev->udev_path[read_bytes - 1] == '\n')
21670fd97ccfSChristoph Hellwig 		dev->udev_path[read_bytes - 1] = '\0';
21680877eafdSSebastian Andrzej Siewior 
21690fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_UDEV_PATH;
21703011684cSDan Carpenter 
21716708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
2172c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
21730fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
21740fd97ccfSChristoph Hellwig 		dev->udev_path);
2175c66ac9dbSNicholas Bellinger 
2176c66ac9dbSNicholas Bellinger 	return read_bytes;
2177c66ac9dbSNicholas Bellinger }
2178c66ac9dbSNicholas Bellinger 
21792eafd729SChristoph Hellwig static ssize_t target_dev_enable_show(struct config_item *item, char *page)
218064146db7SAndy Grover {
21812eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
218264146db7SAndy Grover 
2183cb0f32e1SMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n", target_dev_configured(dev));
218464146db7SAndy Grover }
218564146db7SAndy Grover 
21862eafd729SChristoph Hellwig static ssize_t target_dev_enable_store(struct config_item *item,
21872eafd729SChristoph Hellwig 		const char *page, size_t count)
2188c66ac9dbSNicholas Bellinger {
21892eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2190c66ac9dbSNicholas Bellinger 	char *ptr;
21910fd97ccfSChristoph Hellwig 	int ret;
2192c66ac9dbSNicholas Bellinger 
2193c66ac9dbSNicholas Bellinger 	ptr = strstr(page, "1");
21946708bb27SAndy Grover 	if (!ptr) {
21956708bb27SAndy Grover 		pr_err("For dev_enable ops, only valid value"
2196c66ac9dbSNicholas Bellinger 				" is \"1\"\n");
2197c66ac9dbSNicholas Bellinger 		return -EINVAL;
2198c66ac9dbSNicholas Bellinger 	}
2199c66ac9dbSNicholas Bellinger 
22000fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
22010fd97ccfSChristoph Hellwig 	if (ret)
22020fd97ccfSChristoph Hellwig 		return ret;
2203c66ac9dbSNicholas Bellinger 	return count;
2204c66ac9dbSNicholas Bellinger }
2205c66ac9dbSNicholas Bellinger 
22062eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
2207c66ac9dbSNicholas Bellinger {
22082eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2209c66ac9dbSNicholas Bellinger 	struct config_item *lu_ci;
2210c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2211c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2212c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
2213c66ac9dbSNicholas Bellinger 
2214c66ac9dbSNicholas Bellinger 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
2215c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
2216c87fbd56SChristoph Hellwig 		return 0;
2217c66ac9dbSNicholas Bellinger 
2218c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2219c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
22206708bb27SAndy Grover 	if (lu_gp) {
2221c66ac9dbSNicholas Bellinger 		lu_ci = &lu_gp->lu_gp_group.cg_item;
2222c66ac9dbSNicholas Bellinger 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
2223c66ac9dbSNicholas Bellinger 			config_item_name(lu_ci), lu_gp->lu_gp_id);
2224c66ac9dbSNicholas Bellinger 	}
2225c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2226c66ac9dbSNicholas Bellinger 
2227c66ac9dbSNicholas Bellinger 	return len;
2228c66ac9dbSNicholas Bellinger }
2229c66ac9dbSNicholas Bellinger 
22302eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
22312eafd729SChristoph Hellwig 		const char *page, size_t count)
2232c66ac9dbSNicholas Bellinger {
22332eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
22340fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
2235c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
2236c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2237c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
2238c66ac9dbSNicholas Bellinger 	int move = 0;
2239c66ac9dbSNicholas Bellinger 
2240c87fbd56SChristoph Hellwig 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
2241c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
22429105bfc0SAndy Grover 		return count;
2243c87fbd56SChristoph Hellwig 
2244c66ac9dbSNicholas Bellinger 	if (count > LU_GROUP_NAME_BUF) {
22456708bb27SAndy Grover 		pr_err("ALUA LU Group Alias too large!\n");
2246c66ac9dbSNicholas Bellinger 		return -EINVAL;
2247c66ac9dbSNicholas Bellinger 	}
2248c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
2249c66ac9dbSNicholas Bellinger 	memcpy(buf, page, count);
2250c66ac9dbSNicholas Bellinger 	/*
2251c66ac9dbSNicholas Bellinger 	 * Any ALUA logical unit alias besides "NULL" means we will be
2252c66ac9dbSNicholas Bellinger 	 * making a new group association.
2253c66ac9dbSNicholas Bellinger 	 */
2254c66ac9dbSNicholas Bellinger 	if (strcmp(strstrip(buf), "NULL")) {
2255c66ac9dbSNicholas Bellinger 		/*
2256c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name() will increment reference to
2257c66ac9dbSNicholas Bellinger 		 * struct t10_alua_lu_gp.  This reference is released with
2258c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name below().
2259c66ac9dbSNicholas Bellinger 		 */
2260c66ac9dbSNicholas Bellinger 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
22616708bb27SAndy Grover 		if (!lu_gp_new)
2262c66ac9dbSNicholas Bellinger 			return -ENODEV;
2263c66ac9dbSNicholas Bellinger 	}
2264c66ac9dbSNicholas Bellinger 
2265c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2266c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
22676708bb27SAndy Grover 	if (lu_gp) {
2268c66ac9dbSNicholas Bellinger 		/*
2269c66ac9dbSNicholas Bellinger 		 * Clearing an existing lu_gp association, and replacing
2270c66ac9dbSNicholas Bellinger 		 * with NULL
2271c66ac9dbSNicholas Bellinger 		 */
22726708bb27SAndy Grover 		if (!lu_gp_new) {
22736708bb27SAndy Grover 			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
2274c66ac9dbSNicholas Bellinger 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2275c66ac9dbSNicholas Bellinger 				" %hu\n",
2276c66ac9dbSNicholas Bellinger 				config_item_name(&hba->hba_group.cg_item),
22770fd97ccfSChristoph Hellwig 				config_item_name(&dev->dev_group.cg_item),
2278c66ac9dbSNicholas Bellinger 				config_item_name(&lu_gp->lu_gp_group.cg_item),
2279c66ac9dbSNicholas Bellinger 				lu_gp->lu_gp_id);
2280c66ac9dbSNicholas Bellinger 
2281c66ac9dbSNicholas Bellinger 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2282c66ac9dbSNicholas Bellinger 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2283c66ac9dbSNicholas Bellinger 
2284c66ac9dbSNicholas Bellinger 			return count;
2285c66ac9dbSNicholas Bellinger 		}
2286c66ac9dbSNicholas Bellinger 		/*
2287c66ac9dbSNicholas Bellinger 		 * Removing existing association of lu_gp_mem with lu_gp
2288c66ac9dbSNicholas Bellinger 		 */
2289c66ac9dbSNicholas Bellinger 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2290c66ac9dbSNicholas Bellinger 		move = 1;
2291c66ac9dbSNicholas Bellinger 	}
2292c66ac9dbSNicholas Bellinger 	/*
2293c66ac9dbSNicholas Bellinger 	 * Associate lu_gp_mem with lu_gp_new.
2294c66ac9dbSNicholas Bellinger 	 */
2295c66ac9dbSNicholas Bellinger 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2296c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2297c66ac9dbSNicholas Bellinger 
22986708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
2299c66ac9dbSNicholas Bellinger 		" core/alua/lu_gps/%s, ID: %hu\n",
2300c66ac9dbSNicholas Bellinger 		(move) ? "Moving" : "Adding",
2301c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
23020fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
2303c66ac9dbSNicholas Bellinger 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2304c66ac9dbSNicholas Bellinger 		lu_gp_new->lu_gp_id);
2305c66ac9dbSNicholas Bellinger 
2306c66ac9dbSNicholas Bellinger 	core_alua_put_lu_gp_from_name(lu_gp_new);
2307c66ac9dbSNicholas Bellinger 	return count;
2308c66ac9dbSNicholas Bellinger }
2309c66ac9dbSNicholas Bellinger 
23102eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
2311229d4f11SHannes Reinecke {
23122eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2313229d4f11SHannes Reinecke 	struct t10_alua_lba_map *map;
2314229d4f11SHannes Reinecke 	struct t10_alua_lba_map_member *mem;
2315229d4f11SHannes Reinecke 	char *b = page;
2316229d4f11SHannes Reinecke 	int bl = 0;
2317229d4f11SHannes Reinecke 	char state;
2318229d4f11SHannes Reinecke 
2319229d4f11SHannes Reinecke 	spin_lock(&dev->t10_alua.lba_map_lock);
2320229d4f11SHannes Reinecke 	if (!list_empty(&dev->t10_alua.lba_map_list))
2321229d4f11SHannes Reinecke 	    bl += sprintf(b + bl, "%u %u\n",
2322229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_size,
2323229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_multiplier);
2324229d4f11SHannes Reinecke 	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2325229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "%llu %llu",
2326229d4f11SHannes Reinecke 			      map->lba_map_first_lba, map->lba_map_last_lba);
2327229d4f11SHannes Reinecke 		list_for_each_entry(mem, &map->lba_map_mem_list,
2328229d4f11SHannes Reinecke 				    lba_map_mem_list) {
2329229d4f11SHannes Reinecke 			switch (mem->lba_map_mem_alua_state) {
2330229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2331229d4f11SHannes Reinecke 				state = 'O';
2332229d4f11SHannes Reinecke 				break;
2333229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2334229d4f11SHannes Reinecke 				state = 'A';
2335229d4f11SHannes Reinecke 				break;
2336229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_STANDBY:
2337229d4f11SHannes Reinecke 				state = 'S';
2338229d4f11SHannes Reinecke 				break;
2339229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_UNAVAILABLE:
2340229d4f11SHannes Reinecke 				state = 'U';
2341229d4f11SHannes Reinecke 				break;
2342229d4f11SHannes Reinecke 			default:
2343229d4f11SHannes Reinecke 				state = '.';
2344229d4f11SHannes Reinecke 				break;
2345229d4f11SHannes Reinecke 			}
2346229d4f11SHannes Reinecke 			bl += sprintf(b + bl, " %d:%c",
2347229d4f11SHannes Reinecke 				      mem->lba_map_mem_alua_pg_id, state);
2348229d4f11SHannes Reinecke 		}
2349229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "\n");
2350229d4f11SHannes Reinecke 	}
2351229d4f11SHannes Reinecke 	spin_unlock(&dev->t10_alua.lba_map_lock);
2352229d4f11SHannes Reinecke 	return bl;
2353229d4f11SHannes Reinecke }
2354229d4f11SHannes Reinecke 
23552eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_store(struct config_item *item,
23562eafd729SChristoph Hellwig 		const char *page, size_t count)
2357229d4f11SHannes Reinecke {
23582eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2359229d4f11SHannes Reinecke 	struct t10_alua_lba_map *lba_map = NULL;
2360229d4f11SHannes Reinecke 	struct list_head lba_list;
2361f0a8afecSBart Van Assche 	char *map_entries, *orig, *ptr;
2362229d4f11SHannes Reinecke 	char state;
2363229d4f11SHannes Reinecke 	int pg_num = -1, pg;
2364229d4f11SHannes Reinecke 	int ret = 0, num = 0, pg_id, alua_state;
2365229d4f11SHannes Reinecke 	unsigned long start_lba = -1, end_lba = -1;
2366229d4f11SHannes Reinecke 	unsigned long segment_size = -1, segment_mult = -1;
2367229d4f11SHannes Reinecke 
2368f0a8afecSBart Van Assche 	orig = map_entries = kstrdup(page, GFP_KERNEL);
2369229d4f11SHannes Reinecke 	if (!map_entries)
2370229d4f11SHannes Reinecke 		return -ENOMEM;
2371229d4f11SHannes Reinecke 
2372229d4f11SHannes Reinecke 	INIT_LIST_HEAD(&lba_list);
2373229d4f11SHannes Reinecke 	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2374229d4f11SHannes Reinecke 		if (!*ptr)
2375229d4f11SHannes Reinecke 			continue;
2376229d4f11SHannes Reinecke 
2377229d4f11SHannes Reinecke 		if (num == 0) {
2378229d4f11SHannes Reinecke 			if (sscanf(ptr, "%lu %lu\n",
2379229d4f11SHannes Reinecke 				   &segment_size, &segment_mult) != 2) {
2380229d4f11SHannes Reinecke 				pr_err("Invalid line %d\n", num);
2381229d4f11SHannes Reinecke 				ret = -EINVAL;
2382229d4f11SHannes Reinecke 				break;
2383229d4f11SHannes Reinecke 			}
2384229d4f11SHannes Reinecke 			num++;
2385229d4f11SHannes Reinecke 			continue;
2386229d4f11SHannes Reinecke 		}
2387229d4f11SHannes Reinecke 		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2388229d4f11SHannes Reinecke 			pr_err("Invalid line %d\n", num);
2389229d4f11SHannes Reinecke 			ret = -EINVAL;
2390229d4f11SHannes Reinecke 			break;
2391229d4f11SHannes Reinecke 		}
2392229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2393229d4f11SHannes Reinecke 		if (!ptr) {
2394229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing end lba\n", num);
2395229d4f11SHannes Reinecke 			ret = -EINVAL;
2396229d4f11SHannes Reinecke 			break;
2397229d4f11SHannes Reinecke 		}
2398229d4f11SHannes Reinecke 		ptr++;
2399229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2400229d4f11SHannes Reinecke 		if (!ptr) {
2401229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing state definitions\n",
2402229d4f11SHannes Reinecke 			       num);
2403229d4f11SHannes Reinecke 			ret = -EINVAL;
2404229d4f11SHannes Reinecke 			break;
2405229d4f11SHannes Reinecke 		}
2406229d4f11SHannes Reinecke 		ptr++;
2407229d4f11SHannes Reinecke 		lba_map = core_alua_allocate_lba_map(&lba_list,
2408229d4f11SHannes Reinecke 						     start_lba, end_lba);
2409229d4f11SHannes Reinecke 		if (IS_ERR(lba_map)) {
2410229d4f11SHannes Reinecke 			ret = PTR_ERR(lba_map);
2411229d4f11SHannes Reinecke 			break;
2412229d4f11SHannes Reinecke 		}
2413229d4f11SHannes Reinecke 		pg = 0;
2414229d4f11SHannes Reinecke 		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2415229d4f11SHannes Reinecke 			switch (state) {
2416229d4f11SHannes Reinecke 			case 'O':
2417229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2418229d4f11SHannes Reinecke 				break;
2419229d4f11SHannes Reinecke 			case 'A':
2420229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2421229d4f11SHannes Reinecke 				break;
2422229d4f11SHannes Reinecke 			case 'S':
2423229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_STANDBY;
2424229d4f11SHannes Reinecke 				break;
2425229d4f11SHannes Reinecke 			case 'U':
2426229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2427229d4f11SHannes Reinecke 				break;
2428229d4f11SHannes Reinecke 			default:
2429229d4f11SHannes Reinecke 				pr_err("Invalid ALUA state '%c'\n", state);
2430229d4f11SHannes Reinecke 				ret = -EINVAL;
2431229d4f11SHannes Reinecke 				goto out;
2432229d4f11SHannes Reinecke 			}
2433229d4f11SHannes Reinecke 
2434229d4f11SHannes Reinecke 			ret = core_alua_allocate_lba_map_mem(lba_map,
2435229d4f11SHannes Reinecke 							     pg_id, alua_state);
2436229d4f11SHannes Reinecke 			if (ret) {
2437229d4f11SHannes Reinecke 				pr_err("Invalid target descriptor %d:%c "
2438229d4f11SHannes Reinecke 				       "at line %d\n",
2439229d4f11SHannes Reinecke 				       pg_id, state, num);
2440229d4f11SHannes Reinecke 				break;
2441229d4f11SHannes Reinecke 			}
2442229d4f11SHannes Reinecke 			pg++;
2443229d4f11SHannes Reinecke 			ptr = strchr(ptr, ' ');
2444229d4f11SHannes Reinecke 			if (ptr)
2445229d4f11SHannes Reinecke 				ptr++;
2446229d4f11SHannes Reinecke 			else
2447229d4f11SHannes Reinecke 				break;
2448229d4f11SHannes Reinecke 		}
2449229d4f11SHannes Reinecke 		if (pg_num == -1)
2450229d4f11SHannes Reinecke 		    pg_num = pg;
2451229d4f11SHannes Reinecke 		else if (pg != pg_num) {
2452229d4f11SHannes Reinecke 			pr_err("Only %d from %d port groups definitions "
2453229d4f11SHannes Reinecke 			       "at line %d\n", pg, pg_num, num);
2454229d4f11SHannes Reinecke 			ret = -EINVAL;
2455229d4f11SHannes Reinecke 			break;
2456229d4f11SHannes Reinecke 		}
2457229d4f11SHannes Reinecke 		num++;
2458229d4f11SHannes Reinecke 	}
2459229d4f11SHannes Reinecke out:
2460229d4f11SHannes Reinecke 	if (ret) {
2461229d4f11SHannes Reinecke 		core_alua_free_lba_map(&lba_list);
2462229d4f11SHannes Reinecke 		count = ret;
2463229d4f11SHannes Reinecke 	} else
2464229d4f11SHannes Reinecke 		core_alua_set_lba_map(dev, &lba_list,
2465229d4f11SHannes Reinecke 				      segment_size, segment_mult);
2466f0a8afecSBart Van Assche 	kfree(orig);
2467229d4f11SHannes Reinecke 	return count;
2468229d4f11SHannes Reinecke }
2469229d4f11SHannes Reinecke 
24702eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_dev_, info);
24712eafd729SChristoph Hellwig CONFIGFS_ATTR_WO(target_dev_, control);
24722eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alias);
24732eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, udev_path);
24742eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, enable);
24752eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alua_lu_gp);
24762eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, lba_map);
2477229d4f11SHannes Reinecke 
247873112edcSNicholas Bellinger static struct configfs_attribute *target_core_dev_attrs[] = {
24792eafd729SChristoph Hellwig 	&target_dev_attr_info,
24802eafd729SChristoph Hellwig 	&target_dev_attr_control,
24812eafd729SChristoph Hellwig 	&target_dev_attr_alias,
24822eafd729SChristoph Hellwig 	&target_dev_attr_udev_path,
24832eafd729SChristoph Hellwig 	&target_dev_attr_enable,
24842eafd729SChristoph Hellwig 	&target_dev_attr_alua_lu_gp,
24852eafd729SChristoph Hellwig 	&target_dev_attr_lba_map,
2486c66ac9dbSNicholas Bellinger 	NULL,
2487c66ac9dbSNicholas Bellinger };
2488c66ac9dbSNicholas Bellinger 
2489c66ac9dbSNicholas Bellinger static void target_core_dev_release(struct config_item *item)
2490c66ac9dbSNicholas Bellinger {
24910fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
24920fd97ccfSChristoph Hellwig 	struct se_device *dev =
24930fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2494c66ac9dbSNicholas Bellinger 
24950fd97ccfSChristoph Hellwig 	target_free_device(dev);
2496c66ac9dbSNicholas Bellinger }
2497c66ac9dbSNicholas Bellinger 
2498c17cd249SNicholas Bellinger /*
2499c17cd249SNicholas Bellinger  * Used in target_core_fabric_configfs.c to verify valid se_device symlink
2500c17cd249SNicholas Bellinger  * within target_fabric_port_link()
2501c17cd249SNicholas Bellinger  */
2502c17cd249SNicholas Bellinger struct configfs_item_operations target_core_dev_item_ops = {
2503c66ac9dbSNicholas Bellinger 	.release		= target_core_dev_release,
2504c66ac9dbSNicholas Bellinger };
2505c66ac9dbSNicholas Bellinger 
250673112edcSNicholas Bellinger TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2507c66ac9dbSNicholas Bellinger 
250873112edcSNicholas Bellinger /* End functions for struct config_item_type tb_dev_cit */
2509c66ac9dbSNicholas Bellinger 
2510c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2511c66ac9dbSNicholas Bellinger 
25122eafd729SChristoph Hellwig static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2513c66ac9dbSNicholas Bellinger {
25142eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_lu_gp,
25152eafd729SChristoph Hellwig 			lu_gp_group);
25162eafd729SChristoph Hellwig }
25172eafd729SChristoph Hellwig 
25182eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
25192eafd729SChristoph Hellwig {
25202eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
25212eafd729SChristoph Hellwig 
25226708bb27SAndy Grover 	if (!lu_gp->lu_gp_valid_id)
2523c66ac9dbSNicholas Bellinger 		return 0;
2524c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2525c66ac9dbSNicholas Bellinger }
2526c66ac9dbSNicholas Bellinger 
25272eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
25282eafd729SChristoph Hellwig 		const char *page, size_t count)
2529c66ac9dbSNicholas Bellinger {
25302eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2531c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2532c66ac9dbSNicholas Bellinger 	unsigned long lu_gp_id;
2533c66ac9dbSNicholas Bellinger 	int ret;
2534c66ac9dbSNicholas Bellinger 
253557103d7fSJingoo Han 	ret = kstrtoul(page, 0, &lu_gp_id);
2536c66ac9dbSNicholas Bellinger 	if (ret < 0) {
253757103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2538c66ac9dbSNicholas Bellinger 			" lu_gp_id\n", ret);
253957103d7fSJingoo Han 		return ret;
2540c66ac9dbSNicholas Bellinger 	}
2541c66ac9dbSNicholas Bellinger 	if (lu_gp_id > 0x0000ffff) {
25426708bb27SAndy Grover 		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2543c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", lu_gp_id);
2544c66ac9dbSNicholas Bellinger 		return -EINVAL;
2545c66ac9dbSNicholas Bellinger 	}
2546c66ac9dbSNicholas Bellinger 
2547c66ac9dbSNicholas Bellinger 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2548c66ac9dbSNicholas Bellinger 	if (ret < 0)
2549c66ac9dbSNicholas Bellinger 		return -EINVAL;
2550c66ac9dbSNicholas Bellinger 
25516708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2552c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
2553c66ac9dbSNicholas Bellinger 		config_item_name(&alua_lu_gp_cg->cg_item),
2554c66ac9dbSNicholas Bellinger 		lu_gp->lu_gp_id);
2555c66ac9dbSNicholas Bellinger 
2556c66ac9dbSNicholas Bellinger 	return count;
2557c66ac9dbSNicholas Bellinger }
2558c66ac9dbSNicholas Bellinger 
25592eafd729SChristoph Hellwig static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2560c66ac9dbSNicholas Bellinger {
25612eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2562c66ac9dbSNicholas Bellinger 	struct se_device *dev;
2563c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2564c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2565c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2566c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
2567c66ac9dbSNicholas Bellinger 
2568c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
2569c66ac9dbSNicholas Bellinger 
2570c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp->lu_gp_lock);
2571c66ac9dbSNicholas Bellinger 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2572c66ac9dbSNicholas Bellinger 		dev = lu_gp_mem->lu_gp_mem_dev;
25730fd97ccfSChristoph Hellwig 		hba = dev->se_hba;
2574c66ac9dbSNicholas Bellinger 
2575c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2576c66ac9dbSNicholas Bellinger 			config_item_name(&hba->hba_group.cg_item),
25770fd97ccfSChristoph Hellwig 			config_item_name(&dev->dev_group.cg_item));
2578c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2579c66ac9dbSNicholas Bellinger 
2580c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
25816708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2582c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2583c66ac9dbSNicholas Bellinger 			break;
2584c66ac9dbSNicholas Bellinger 		}
2585c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2586c66ac9dbSNicholas Bellinger 		len += cur_len;
2587c66ac9dbSNicholas Bellinger 	}
2588c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp->lu_gp_lock);
2589c66ac9dbSNicholas Bellinger 
2590c66ac9dbSNicholas Bellinger 	return len;
2591c66ac9dbSNicholas Bellinger }
2592c66ac9dbSNicholas Bellinger 
25932eafd729SChristoph Hellwig CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
25942eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_lu_gp_, members);
2595c66ac9dbSNicholas Bellinger 
2596c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
25972eafd729SChristoph Hellwig 	&target_lu_gp_attr_lu_gp_id,
25982eafd729SChristoph Hellwig 	&target_lu_gp_attr_members,
2599c66ac9dbSNicholas Bellinger 	NULL,
2600c66ac9dbSNicholas Bellinger };
2601c66ac9dbSNicholas Bellinger 
26021f6fe7cbSNicholas Bellinger static void target_core_alua_lu_gp_release(struct config_item *item)
26031f6fe7cbSNicholas Bellinger {
26041f6fe7cbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
26051f6fe7cbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
26061f6fe7cbSNicholas Bellinger 
26071f6fe7cbSNicholas Bellinger 	core_alua_free_lu_gp(lu_gp);
26081f6fe7cbSNicholas Bellinger }
26091f6fe7cbSNicholas Bellinger 
2610c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_lu_gp_ops = {
26111f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_lu_gp_release,
2612c66ac9dbSNicholas Bellinger };
2613c66ac9dbSNicholas Bellinger 
2614ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_lu_gp_cit = {
2615c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
2616c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_lu_gp_attrs,
2617c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2618c66ac9dbSNicholas Bellinger };
2619c66ac9dbSNicholas Bellinger 
2620c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2621c66ac9dbSNicholas Bellinger 
2622c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2623c66ac9dbSNicholas Bellinger 
2624c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_lu_gp(
2625c66ac9dbSNicholas Bellinger 	struct config_group *group,
2626c66ac9dbSNicholas Bellinger 	const char *name)
2627c66ac9dbSNicholas Bellinger {
2628c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2629c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = NULL;
2630c66ac9dbSNicholas Bellinger 	struct config_item *alua_lu_gp_ci = NULL;
2631c66ac9dbSNicholas Bellinger 
2632c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp(name, 0);
2633c66ac9dbSNicholas Bellinger 	if (IS_ERR(lu_gp))
2634c66ac9dbSNicholas Bellinger 		return NULL;
2635c66ac9dbSNicholas Bellinger 
2636c66ac9dbSNicholas Bellinger 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
2637c66ac9dbSNicholas Bellinger 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2638c66ac9dbSNicholas Bellinger 
2639c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_lu_gp_cg, name,
2640c66ac9dbSNicholas Bellinger 			&target_core_alua_lu_gp_cit);
2641c66ac9dbSNicholas Bellinger 
26426708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2643c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s\n",
2644c66ac9dbSNicholas Bellinger 		config_item_name(alua_lu_gp_ci));
2645c66ac9dbSNicholas Bellinger 
2646c66ac9dbSNicholas Bellinger 	return alua_lu_gp_cg;
2647c66ac9dbSNicholas Bellinger 
2648c66ac9dbSNicholas Bellinger }
2649c66ac9dbSNicholas Bellinger 
2650c66ac9dbSNicholas Bellinger static void target_core_alua_drop_lu_gp(
2651c66ac9dbSNicholas Bellinger 	struct config_group *group,
2652c66ac9dbSNicholas Bellinger 	struct config_item *item)
2653c66ac9dbSNicholas Bellinger {
2654c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2655c66ac9dbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
2656c66ac9dbSNicholas Bellinger 
26576708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2658c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
2659c66ac9dbSNicholas Bellinger 		config_item_name(item), lu_gp->lu_gp_id);
26601f6fe7cbSNicholas Bellinger 	/*
26611f6fe7cbSNicholas Bellinger 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
26621f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_lu_gp_release()
26631f6fe7cbSNicholas Bellinger 	 */
2664c66ac9dbSNicholas Bellinger 	config_item_put(item);
2665c66ac9dbSNicholas Bellinger }
2666c66ac9dbSNicholas Bellinger 
2667c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2668c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_lu_gp,
2669c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_lu_gp,
2670c66ac9dbSNicholas Bellinger };
2671c66ac9dbSNicholas Bellinger 
2672ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_lu_gps_cit = {
2673c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2674c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
2675c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2676c66ac9dbSNicholas Bellinger };
2677c66ac9dbSNicholas Bellinger 
2678c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2679c66ac9dbSNicholas Bellinger 
2680c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2681c66ac9dbSNicholas Bellinger 
26822eafd729SChristoph Hellwig static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
26832eafd729SChristoph Hellwig {
26842eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
26852eafd729SChristoph Hellwig 			tg_pt_gp_group);
26862eafd729SChristoph Hellwig }
2687c66ac9dbSNicholas Bellinger 
26882eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2689c66ac9dbSNicholas Bellinger 		char *page)
2690c66ac9dbSNicholas Bellinger {
2691c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n",
2692d19c4643SMike Christie 		       to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
2693c66ac9dbSNicholas Bellinger }
2694c66ac9dbSNicholas Bellinger 
26952eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
26962eafd729SChristoph Hellwig 		const char *page, size_t count)
2697c66ac9dbSNicholas Bellinger {
26982eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
26990fd97ccfSChristoph Hellwig 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2700c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2701c66ac9dbSNicholas Bellinger 	int new_state, ret;
2702c66ac9dbSNicholas Bellinger 
27036708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2704125d0119SHannes Reinecke 		pr_err("Unable to do implicit ALUA on non valid"
2705c66ac9dbSNicholas Bellinger 			" tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2706c66ac9dbSNicholas Bellinger 		return -EINVAL;
2707c66ac9dbSNicholas Bellinger 	}
2708cb0f32e1SMike Christie 	if (!target_dev_configured(dev)) {
2709f1453773SNicholas Bellinger 		pr_err("Unable to set alua_access_state while device is"
2710f1453773SNicholas Bellinger 		       " not configured\n");
2711f1453773SNicholas Bellinger 		return -ENODEV;
2712f1453773SNicholas Bellinger 	}
2713c66ac9dbSNicholas Bellinger 
271457103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2715c66ac9dbSNicholas Bellinger 	if (ret < 0) {
27166708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access state from"
2717c66ac9dbSNicholas Bellinger 				" %s\n", page);
271857103d7fSJingoo Han 		return ret;
2719c66ac9dbSNicholas Bellinger 	}
2720c66ac9dbSNicholas Bellinger 	new_state = (int)tmp;
2721c66ac9dbSNicholas Bellinger 
2722125d0119SHannes Reinecke 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2723125d0119SHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA"
2724125d0119SHannes Reinecke 			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
2725c66ac9dbSNicholas Bellinger 		return -EINVAL;
2726c66ac9dbSNicholas Bellinger 	}
2727c66094bfSHannes Reinecke 	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2728c66094bfSHannes Reinecke 	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2729c66094bfSHannes Reinecke 		/* LBA DEPENDENT is only allowed with implicit ALUA */
2730c66094bfSHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA transition"
2731c66094bfSHannes Reinecke 		       " while explicit ALUA management is enabled\n");
2732c66094bfSHannes Reinecke 		return -EINVAL;
2733c66094bfSHannes Reinecke 	}
2734c66ac9dbSNicholas Bellinger 
27350fd97ccfSChristoph Hellwig 	ret = core_alua_do_port_transition(tg_pt_gp, dev,
2736c66ac9dbSNicholas Bellinger 					NULL, NULL, new_state, 0);
2737c66ac9dbSNicholas Bellinger 	return (!ret) ? count : -EINVAL;
2738c66ac9dbSNicholas Bellinger }
2739c66ac9dbSNicholas Bellinger 
27402eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2741c66ac9dbSNicholas Bellinger 		char *page)
2742c66ac9dbSNicholas Bellinger {
27432eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2744c66ac9dbSNicholas Bellinger 	return sprintf(page, "%s\n",
2745c66ac9dbSNicholas Bellinger 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2746c66ac9dbSNicholas Bellinger }
2747c66ac9dbSNicholas Bellinger 
27482eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_store(
27492eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2750c66ac9dbSNicholas Bellinger {
27512eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2752c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2753c66ac9dbSNicholas Bellinger 	int new_status, ret;
2754c66ac9dbSNicholas Bellinger 
27556708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
27566708bb27SAndy Grover 		pr_err("Unable to do set ALUA access status on non"
2757c66ac9dbSNicholas Bellinger 			" valid tg_pt_gp ID: %hu\n",
2758c66ac9dbSNicholas Bellinger 			tg_pt_gp->tg_pt_gp_valid_id);
2759c66ac9dbSNicholas Bellinger 		return -EINVAL;
2760c66ac9dbSNicholas Bellinger 	}
2761c66ac9dbSNicholas Bellinger 
276257103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2763c66ac9dbSNicholas Bellinger 	if (ret < 0) {
27646708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access status"
2765c66ac9dbSNicholas Bellinger 				" from %s\n", page);
276657103d7fSJingoo Han 		return ret;
2767c66ac9dbSNicholas Bellinger 	}
2768c66ac9dbSNicholas Bellinger 	new_status = (int)tmp;
2769c66ac9dbSNicholas Bellinger 
2770c66ac9dbSNicholas Bellinger 	if ((new_status != ALUA_STATUS_NONE) &&
2771125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2772125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
27736708bb27SAndy Grover 		pr_err("Illegal ALUA access status: 0x%02x\n",
2774c66ac9dbSNicholas Bellinger 				new_status);
2775c66ac9dbSNicholas Bellinger 		return -EINVAL;
2776c66ac9dbSNicholas Bellinger 	}
2777c66ac9dbSNicholas Bellinger 
2778c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2779c66ac9dbSNicholas Bellinger 	return count;
2780c66ac9dbSNicholas Bellinger }
2781c66ac9dbSNicholas Bellinger 
27822eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2783c66ac9dbSNicholas Bellinger 		char *page)
2784c66ac9dbSNicholas Bellinger {
27852eafd729SChristoph Hellwig 	return core_alua_show_access_type(to_tg_pt_gp(item), page);
2786c66ac9dbSNicholas Bellinger }
2787c66ac9dbSNicholas Bellinger 
27882eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
27892eafd729SChristoph Hellwig 		const char *page, size_t count)
2790c66ac9dbSNicholas Bellinger {
27912eafd729SChristoph Hellwig 	return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2792c66ac9dbSNicholas Bellinger }
2793c66ac9dbSNicholas Bellinger 
27942eafd729SChristoph Hellwig #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit)				\
27952eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_show(		\
27962eafd729SChristoph Hellwig 		struct config_item *item, char *p)			\
2797b0a382c5SHannes Reinecke {									\
27982eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
27992eafd729SChristoph Hellwig 	return sprintf(p, "%d\n",					\
28002eafd729SChristoph Hellwig 		!!(t->tg_pt_gp_alua_supported_states & _bit));		\
28012eafd729SChristoph Hellwig }									\
28022eafd729SChristoph Hellwig 									\
28032eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_store(		\
28042eafd729SChristoph Hellwig 		struct config_item *item, const char *p, size_t c)	\
2805b0a382c5SHannes Reinecke {									\
28062eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
2807b0a382c5SHannes Reinecke 	unsigned long tmp;						\
2808b0a382c5SHannes Reinecke 	int ret;							\
2809b0a382c5SHannes Reinecke 									\
2810b0a382c5SHannes Reinecke 	if (!t->tg_pt_gp_valid_id) {					\
2811c0dcafd8SHannes Reinecke 		pr_err("Unable to do set " #_name " ALUA state on non"	\
2812b0a382c5SHannes Reinecke 		       " valid tg_pt_gp ID: %hu\n",			\
2813b0a382c5SHannes Reinecke 		       t->tg_pt_gp_valid_id);				\
2814b0a382c5SHannes Reinecke 		return -EINVAL;						\
2815b0a382c5SHannes Reinecke 	}								\
2816b0a382c5SHannes Reinecke 									\
2817b0a382c5SHannes Reinecke 	ret = kstrtoul(p, 0, &tmp);					\
2818b0a382c5SHannes Reinecke 	if (ret < 0) {							\
2819b0a382c5SHannes Reinecke 		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
2820b0a382c5SHannes Reinecke 		return -EINVAL;						\
2821b0a382c5SHannes Reinecke 	}								\
2822b0a382c5SHannes Reinecke 	if (tmp > 1) {							\
2823b0a382c5SHannes Reinecke 		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2824b0a382c5SHannes Reinecke 		return -EINVAL;						\
2825b0a382c5SHannes Reinecke 	}								\
28261f0b030cSSebastian Herbszt 	if (tmp)							\
28272eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states |= _bit;		\
2828b0a382c5SHannes Reinecke 	else								\
28292eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states &= ~_bit;		\
2830b0a382c5SHannes Reinecke 									\
2831b0a382c5SHannes Reinecke 	return c;							\
28326be526c4SHannes Reinecke }
28336be526c4SHannes Reinecke 
28342eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
28352eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
28362eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
28372eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
28382eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
28392eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
28402eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
28416be526c4SHannes Reinecke 
28422eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_show(
28432eafd729SChristoph Hellwig 		struct config_item *item, char *page)
2844c66ac9dbSNicholas Bellinger {
28452eafd729SChristoph Hellwig 	return sprintf(page, "%d\n",
28462eafd729SChristoph Hellwig 		to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
2847c66ac9dbSNicholas Bellinger }
2848c66ac9dbSNicholas Bellinger 
28492eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_store(
28502eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2851c66ac9dbSNicholas Bellinger {
28522eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2853c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2854c66ac9dbSNicholas Bellinger 	int ret;
2855c66ac9dbSNicholas Bellinger 
285657103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2857c66ac9dbSNicholas Bellinger 	if (ret < 0) {
28586708bb27SAndy Grover 		pr_err("Unable to extract alua_write_metadata\n");
285957103d7fSJingoo Han 		return ret;
2860c66ac9dbSNicholas Bellinger 	}
2861c66ac9dbSNicholas Bellinger 
2862c66ac9dbSNicholas Bellinger 	if ((tmp != 0) && (tmp != 1)) {
28636708bb27SAndy Grover 		pr_err("Illegal value for alua_write_metadata:"
2864c66ac9dbSNicholas Bellinger 			" %lu\n", tmp);
2865c66ac9dbSNicholas Bellinger 		return -EINVAL;
2866c66ac9dbSNicholas Bellinger 	}
2867c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2868c66ac9dbSNicholas Bellinger 
2869c66ac9dbSNicholas Bellinger 	return count;
2870c66ac9dbSNicholas Bellinger }
2871c66ac9dbSNicholas Bellinger 
28722eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2873c66ac9dbSNicholas Bellinger 		char *page)
2874c66ac9dbSNicholas Bellinger {
28752eafd729SChristoph Hellwig 	return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
2876c66ac9dbSNicholas Bellinger }
2877c66ac9dbSNicholas Bellinger 
28782eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
28792eafd729SChristoph Hellwig 		const char *page, size_t count)
2880c66ac9dbSNicholas Bellinger {
28812eafd729SChristoph Hellwig 	return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
28822eafd729SChristoph Hellwig 			count);
2883c66ac9dbSNicholas Bellinger }
2884c66ac9dbSNicholas Bellinger 
28852eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2886c66ac9dbSNicholas Bellinger 		char *page)
2887c66ac9dbSNicholas Bellinger {
28882eafd729SChristoph Hellwig 	return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
2889c66ac9dbSNicholas Bellinger }
2890c66ac9dbSNicholas Bellinger 
28912eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
28922eafd729SChristoph Hellwig 		const char *page, size_t count)
2893c66ac9dbSNicholas Bellinger {
28942eafd729SChristoph Hellwig 	return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
28952eafd729SChristoph Hellwig 			count);
2896c66ac9dbSNicholas Bellinger }
2897c66ac9dbSNicholas Bellinger 
28982eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
28992eafd729SChristoph Hellwig 		struct config_item *item, char *page)
29002eafd729SChristoph Hellwig {
29012eafd729SChristoph Hellwig 	return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
29022eafd729SChristoph Hellwig }
2903c66ac9dbSNicholas Bellinger 
29042eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
29052eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
29062eafd729SChristoph Hellwig {
29072eafd729SChristoph Hellwig 	return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
29082eafd729SChristoph Hellwig 			count);
29092eafd729SChristoph Hellwig }
29102eafd729SChristoph Hellwig 
29112eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
29125b9a4d72SNicholas Bellinger 		char *page)
29135b9a4d72SNicholas Bellinger {
29142eafd729SChristoph Hellwig 	return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
29155b9a4d72SNicholas Bellinger }
29165b9a4d72SNicholas Bellinger 
29172eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
29182eafd729SChristoph Hellwig 		const char *page, size_t count)
29195b9a4d72SNicholas Bellinger {
29202eafd729SChristoph Hellwig 	return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
29215b9a4d72SNicholas Bellinger }
29225b9a4d72SNicholas Bellinger 
29232eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2924c66ac9dbSNicholas Bellinger 		char *page)
2925c66ac9dbSNicholas Bellinger {
29262eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2927c66ac9dbSNicholas Bellinger 
29286708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id)
2929c66ac9dbSNicholas Bellinger 		return 0;
2930c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2931c66ac9dbSNicholas Bellinger }
2932c66ac9dbSNicholas Bellinger 
29332eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
29342eafd729SChristoph Hellwig 		const char *page, size_t count)
2935c66ac9dbSNicholas Bellinger {
29362eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2937c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2938c66ac9dbSNicholas Bellinger 	unsigned long tg_pt_gp_id;
2939c66ac9dbSNicholas Bellinger 	int ret;
2940c66ac9dbSNicholas Bellinger 
294157103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tg_pt_gp_id);
2942c66ac9dbSNicholas Bellinger 	if (ret < 0) {
29433d035237SHannes Reinecke 		pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
29443d035237SHannes Reinecke 		       page);
294557103d7fSJingoo Han 		return ret;
2946c66ac9dbSNicholas Bellinger 	}
2947c66ac9dbSNicholas Bellinger 	if (tg_pt_gp_id > 0x0000ffff) {
29483d035237SHannes Reinecke 		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
29493d035237SHannes Reinecke 		       tg_pt_gp_id);
2950c66ac9dbSNicholas Bellinger 		return -EINVAL;
2951c66ac9dbSNicholas Bellinger 	}
2952c66ac9dbSNicholas Bellinger 
2953c66ac9dbSNicholas Bellinger 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2954c66ac9dbSNicholas Bellinger 	if (ret < 0)
2955c66ac9dbSNicholas Bellinger 		return -EINVAL;
2956c66ac9dbSNicholas Bellinger 
29576708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2958c66ac9dbSNicholas Bellinger 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
2959c66ac9dbSNicholas Bellinger 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
2960c66ac9dbSNicholas Bellinger 		tg_pt_gp->tg_pt_gp_id);
2961c66ac9dbSNicholas Bellinger 
2962c66ac9dbSNicholas Bellinger 	return count;
2963c66ac9dbSNicholas Bellinger }
2964c66ac9dbSNicholas Bellinger 
29652eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2966c66ac9dbSNicholas Bellinger 		char *page)
2967c66ac9dbSNicholas Bellinger {
29682eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2969c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
2970c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2971c66ac9dbSNicholas Bellinger 	unsigned char buf[TG_PT_GROUP_NAME_BUF];
2972c66ac9dbSNicholas Bellinger 
2973c66ac9dbSNicholas Bellinger 	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2974c66ac9dbSNicholas Bellinger 
2975c66ac9dbSNicholas Bellinger 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2976adf653f9SChristoph Hellwig 	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2977adf653f9SChristoph Hellwig 			lun_tg_pt_gp_link) {
2978adf653f9SChristoph Hellwig 		struct se_portal_group *tpg = lun->lun_tpg;
2979c66ac9dbSNicholas Bellinger 
2980c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
298130c7ca93SDavid Disseldorp 			"/%s\n", tpg->se_tpg_tfo->fabric_name,
2982e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2983e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
2984c66ac9dbSNicholas Bellinger 			config_item_name(&lun->lun_group.cg_item));
2985c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2986c66ac9dbSNicholas Bellinger 
2987c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
29886708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2989c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2990c66ac9dbSNicholas Bellinger 			break;
2991c66ac9dbSNicholas Bellinger 		}
2992c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2993c66ac9dbSNicholas Bellinger 		len += cur_len;
2994c66ac9dbSNicholas Bellinger 	}
2995c66ac9dbSNicholas Bellinger 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2996c66ac9dbSNicholas Bellinger 
2997c66ac9dbSNicholas Bellinger 	return len;
2998c66ac9dbSNicholas Bellinger }
2999c66ac9dbSNicholas Bellinger 
30002eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
30012eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
30022eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
30032eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
30042eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
30052eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
30062eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
30072eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
30082eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
30092eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
30102eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
30112eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
30122eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
30132eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
30142eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
30152eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
30162eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
3017c66ac9dbSNicholas Bellinger 
3018c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
30192eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_state,
30202eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_status,
30212eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_type,
30222eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_transitioning,
30232eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_offline,
30242eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_lba_dependent,
30252eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_unavailable,
30262eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_standby,
30272eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_nonoptimized,
30282eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_optimized,
30292eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_write_metadata,
30302eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_nonop_delay_msecs,
30312eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_trans_delay_msecs,
30322eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_implicit_trans_secs,
30332eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_preferred,
30342eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_tg_pt_gp_id,
30352eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_members,
3036c66ac9dbSNicholas Bellinger 	NULL,
3037c66ac9dbSNicholas Bellinger };
3038c66ac9dbSNicholas Bellinger 
30391f6fe7cbSNicholas Bellinger static void target_core_alua_tg_pt_gp_release(struct config_item *item)
30401f6fe7cbSNicholas Bellinger {
30411f6fe7cbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
30421f6fe7cbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
30431f6fe7cbSNicholas Bellinger 
30441f6fe7cbSNicholas Bellinger 	core_alua_free_tg_pt_gp(tg_pt_gp);
30451f6fe7cbSNicholas Bellinger }
30461f6fe7cbSNicholas Bellinger 
3047c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
30481f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_tg_pt_gp_release,
3049c66ac9dbSNicholas Bellinger };
3050c66ac9dbSNicholas Bellinger 
3051ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_tg_pt_gp_cit = {
3052c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
3053c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
3054c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3055c66ac9dbSNicholas Bellinger };
3056c66ac9dbSNicholas Bellinger 
3057c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
3058c66ac9dbSNicholas Bellinger 
305972aca57bSNicholas Bellinger /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3060c66ac9dbSNicholas Bellinger 
3061c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_tg_pt_gp(
3062c66ac9dbSNicholas Bellinger 	struct config_group *group,
3063c66ac9dbSNicholas Bellinger 	const char *name)
3064c66ac9dbSNicholas Bellinger {
3065c66ac9dbSNicholas Bellinger 	struct t10_alua *alua = container_of(group, struct t10_alua,
3066c66ac9dbSNicholas Bellinger 					alua_tg_pt_gps_group);
3067c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
3068c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = NULL;
3069c66ac9dbSNicholas Bellinger 	struct config_item *alua_tg_pt_gp_ci = NULL;
3070c66ac9dbSNicholas Bellinger 
30710fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
30726708bb27SAndy Grover 	if (!tg_pt_gp)
3073c66ac9dbSNicholas Bellinger 		return NULL;
3074c66ac9dbSNicholas Bellinger 
3075c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3076c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
3077c66ac9dbSNicholas Bellinger 
3078c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
3079c66ac9dbSNicholas Bellinger 			&target_core_alua_tg_pt_gp_cit);
3080c66ac9dbSNicholas Bellinger 
30816708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
3082c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s\n",
3083c66ac9dbSNicholas Bellinger 		config_item_name(alua_tg_pt_gp_ci));
3084c66ac9dbSNicholas Bellinger 
3085c66ac9dbSNicholas Bellinger 	return alua_tg_pt_gp_cg;
3086c66ac9dbSNicholas Bellinger }
3087c66ac9dbSNicholas Bellinger 
3088c66ac9dbSNicholas Bellinger static void target_core_alua_drop_tg_pt_gp(
3089c66ac9dbSNicholas Bellinger 	struct config_group *group,
3090c66ac9dbSNicholas Bellinger 	struct config_item *item)
3091c66ac9dbSNicholas Bellinger {
3092c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3093c66ac9dbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3094c66ac9dbSNicholas Bellinger 
30956708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
3096c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
3097c66ac9dbSNicholas Bellinger 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
30981f6fe7cbSNicholas Bellinger 	/*
30991f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
31001f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_tg_pt_gp_release().
31011f6fe7cbSNicholas Bellinger 	 */
3102c66ac9dbSNicholas Bellinger 	config_item_put(item);
3103c66ac9dbSNicholas Bellinger }
3104c66ac9dbSNicholas Bellinger 
3105c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
3106c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_tg_pt_gp,
3107c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
3108c66ac9dbSNicholas Bellinger };
3109c66ac9dbSNicholas Bellinger 
311072aca57bSNicholas Bellinger TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
3111c66ac9dbSNicholas Bellinger 
311272aca57bSNicholas Bellinger /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3113c66ac9dbSNicholas Bellinger 
3114c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_cit */
3115c66ac9dbSNicholas Bellinger 
3116c66ac9dbSNicholas Bellinger /*
3117c66ac9dbSNicholas Bellinger  * target_core_alua_cit is a ConfigFS group that lives under
3118c66ac9dbSNicholas Bellinger  * /sys/kernel/config/target/core/alua.  There are default groups
3119c66ac9dbSNicholas Bellinger  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
3120c66ac9dbSNicholas Bellinger  * target_core_alua_cit in target_core_init_configfs() below.
3121c66ac9dbSNicholas Bellinger  */
3122ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_cit = {
3123c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
3124c66ac9dbSNicholas Bellinger 	.ct_attrs		= NULL,
3125c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3126c66ac9dbSNicholas Bellinger };
3127c66ac9dbSNicholas Bellinger 
3128c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_cit */
3129c66ac9dbSNicholas Bellinger 
3130d23ab570SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_stat_cit */
313112d23384SNicholas Bellinger 
313212d23384SNicholas Bellinger static struct config_group *target_core_stat_mkdir(
313312d23384SNicholas Bellinger 	struct config_group *group,
313412d23384SNicholas Bellinger 	const char *name)
313512d23384SNicholas Bellinger {
313612d23384SNicholas Bellinger 	return ERR_PTR(-ENOSYS);
313712d23384SNicholas Bellinger }
313812d23384SNicholas Bellinger 
313912d23384SNicholas Bellinger static void target_core_stat_rmdir(
314012d23384SNicholas Bellinger 	struct config_group *group,
314112d23384SNicholas Bellinger 	struct config_item *item)
314212d23384SNicholas Bellinger {
314312d23384SNicholas Bellinger 	return;
314412d23384SNicholas Bellinger }
314512d23384SNicholas Bellinger 
314612d23384SNicholas Bellinger static struct configfs_group_operations target_core_stat_group_ops = {
314712d23384SNicholas Bellinger 	.make_group		= &target_core_stat_mkdir,
314812d23384SNicholas Bellinger 	.drop_item		= &target_core_stat_rmdir,
314912d23384SNicholas Bellinger };
315012d23384SNicholas Bellinger 
3151d23ab570SNicholas Bellinger TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
315212d23384SNicholas Bellinger 
3153d23ab570SNicholas Bellinger /* End functions for struct config_item_type tb_dev_stat_cit */
315412d23384SNicholas Bellinger 
3155c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_hba_cit */
3156c66ac9dbSNicholas Bellinger 
3157c66ac9dbSNicholas Bellinger static struct config_group *target_core_make_subdev(
3158c66ac9dbSNicholas Bellinger 	struct config_group *group,
3159c66ac9dbSNicholas Bellinger 	const char *name)
3160c66ac9dbSNicholas Bellinger {
3161c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
3162c66ac9dbSNicholas Bellinger 	struct config_item *hba_ci = &group->cg_item;
3163c66ac9dbSNicholas Bellinger 	struct se_hba *hba = item_to_hba(hba_ci);
31640a06d430SChristoph Hellwig 	struct target_backend *tb = hba->backend;
31650fd97ccfSChristoph Hellwig 	struct se_device *dev;
316612d23384SNicholas Bellinger 	int errno = -ENOMEM, ret;
3167c66ac9dbSNicholas Bellinger 
316812d23384SNicholas Bellinger 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
316912d23384SNicholas Bellinger 	if (ret)
317012d23384SNicholas Bellinger 		return ERR_PTR(ret);
3171c66ac9dbSNicholas Bellinger 
31720fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, name);
31730fd97ccfSChristoph Hellwig 	if (!dev)
31740fd97ccfSChristoph Hellwig 		goto out_unlock;
3175c66ac9dbSNicholas Bellinger 
31761ae1602dSChristoph Hellwig 	config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
3177c66ac9dbSNicholas Bellinger 
31788dc31ff9SMike Christie 	config_group_init_type_name(&dev->dev_action_group, "action",
31798dc31ff9SMike Christie 			&tb->tb_dev_action_cit);
31808dc31ff9SMike Christie 	configfs_add_default_group(&dev->dev_action_group, &dev->dev_group);
31818dc31ff9SMike Christie 
31820fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
31830a06d430SChristoph Hellwig 			&tb->tb_dev_attrib_cit);
31841ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
31851ae1602dSChristoph Hellwig 
31860fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_pr_group, "pr",
31870a06d430SChristoph Hellwig 			&tb->tb_dev_pr_cit);
31881ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
31891ae1602dSChristoph Hellwig 
31900fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
31910a06d430SChristoph Hellwig 			&tb->tb_dev_wwn_cit);
31921ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
31931ae1602dSChristoph Hellwig 			&dev->dev_group);
31941ae1602dSChristoph Hellwig 
31950fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
31960a06d430SChristoph Hellwig 			"alua", &tb->tb_dev_alua_tg_pt_gps_cit);
31971ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
31981ae1602dSChristoph Hellwig 			&dev->dev_group);
31991ae1602dSChristoph Hellwig 
32000fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
32010a06d430SChristoph Hellwig 			"statistics", &tb->tb_dev_stat_cit);
32021ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_stat_grps.stat_group,
32031ae1602dSChristoph Hellwig 			&dev->dev_group);
320412d23384SNicholas Bellinger 
3205c66ac9dbSNicholas Bellinger 	/*
320612d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
3207c66ac9dbSNicholas Bellinger 	 */
32080fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
32096708bb27SAndy Grover 	if (!tg_pt_gp)
32101ae1602dSChristoph Hellwig 		goto out_free_device;
32110fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
3212c66ac9dbSNicholas Bellinger 
3213c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
3214c66ac9dbSNicholas Bellinger 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
32151ae1602dSChristoph Hellwig 	configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
32161ae1602dSChristoph Hellwig 			&dev->t10_alua.alua_tg_pt_gps_group);
32171ae1602dSChristoph Hellwig 
321812d23384SNicholas Bellinger 	/*
321912d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/statistics/ default groups
322012d23384SNicholas Bellinger 	 */
32210fd97ccfSChristoph Hellwig 	target_stat_setup_dev_default_groups(dev);
3222c66ac9dbSNicholas Bellinger 
3223c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
32241ae1602dSChristoph Hellwig 	return &dev->dev_group;
32250fd97ccfSChristoph Hellwig 
32260fd97ccfSChristoph Hellwig out_free_device:
32270fd97ccfSChristoph Hellwig 	target_free_device(dev);
32280fd97ccfSChristoph Hellwig out_unlock:
3229c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
323012d23384SNicholas Bellinger 	return ERR_PTR(errno);
3231c66ac9dbSNicholas Bellinger }
3232c66ac9dbSNicholas Bellinger 
3233c66ac9dbSNicholas Bellinger static void target_core_drop_subdev(
3234c66ac9dbSNicholas Bellinger 	struct config_group *group,
3235c66ac9dbSNicholas Bellinger 	struct config_item *item)
3236c66ac9dbSNicholas Bellinger {
32370fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
32380fd97ccfSChristoph Hellwig 	struct se_device *dev =
32390fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
3240c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
3241c66ac9dbSNicholas Bellinger 
32420fd97ccfSChristoph Hellwig 	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
3243c66ac9dbSNicholas Bellinger 
32441f6fe7cbSNicholas Bellinger 	mutex_lock(&hba->hba_access_mutex);
3245c66ac9dbSNicholas Bellinger 
32461ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
32471ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
324812d23384SNicholas Bellinger 
32491f6fe7cbSNicholas Bellinger 	/*
32501f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
32511f6fe7cbSNicholas Bellinger 	 * directly from target_core_alua_tg_pt_gp_release().
32521f6fe7cbSNicholas Bellinger 	 */
32530fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = NULL;
3254c66ac9dbSNicholas Bellinger 
32551ae1602dSChristoph Hellwig 	configfs_remove_default_groups(dev_cg);
32561ae1602dSChristoph Hellwig 
32571f6fe7cbSNicholas Bellinger 	/*
32580fd97ccfSChristoph Hellwig 	 * se_dev is released from target_core_dev_item_ops->release()
32591f6fe7cbSNicholas Bellinger 	 */
3260c66ac9dbSNicholas Bellinger 	config_item_put(item);
3261c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
3262c66ac9dbSNicholas Bellinger }
3263c66ac9dbSNicholas Bellinger 
3264c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_hba_group_ops = {
3265c66ac9dbSNicholas Bellinger 	.make_group		= target_core_make_subdev,
3266c66ac9dbSNicholas Bellinger 	.drop_item		= target_core_drop_subdev,
3267c66ac9dbSNicholas Bellinger };
3268c66ac9dbSNicholas Bellinger 
3269c66ac9dbSNicholas Bellinger 
32702eafd729SChristoph Hellwig static inline struct se_hba *to_hba(struct config_item *item)
3271c66ac9dbSNicholas Bellinger {
32722eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_hba, hba_group);
32732eafd729SChristoph Hellwig }
32742eafd729SChristoph Hellwig 
32752eafd729SChristoph Hellwig static ssize_t target_hba_info_show(struct config_item *item, char *page)
32762eafd729SChristoph Hellwig {
32772eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
32782eafd729SChristoph Hellwig 
3279c66ac9dbSNicholas Bellinger 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
32800a06d430SChristoph Hellwig 			hba->hba_id, hba->backend->ops->name,
3281ce8dd25dSChristoph Hellwig 			TARGET_CORE_VERSION);
3282c66ac9dbSNicholas Bellinger }
3283c66ac9dbSNicholas Bellinger 
32842eafd729SChristoph Hellwig static ssize_t target_hba_mode_show(struct config_item *item, char *page)
3285c66ac9dbSNicholas Bellinger {
32862eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
3287c66ac9dbSNicholas Bellinger 	int hba_mode = 0;
3288c66ac9dbSNicholas Bellinger 
3289c66ac9dbSNicholas Bellinger 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3290c66ac9dbSNicholas Bellinger 		hba_mode = 1;
3291c66ac9dbSNicholas Bellinger 
3292c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", hba_mode);
3293c66ac9dbSNicholas Bellinger }
3294c66ac9dbSNicholas Bellinger 
32952eafd729SChristoph Hellwig static ssize_t target_hba_mode_store(struct config_item *item,
3296c66ac9dbSNicholas Bellinger 		const char *page, size_t count)
3297c66ac9dbSNicholas Bellinger {
32982eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
3299c66ac9dbSNicholas Bellinger 	unsigned long mode_flag;
3300c66ac9dbSNicholas Bellinger 	int ret;
3301c66ac9dbSNicholas Bellinger 
33020a06d430SChristoph Hellwig 	if (hba->backend->ops->pmode_enable_hba == NULL)
3303c66ac9dbSNicholas Bellinger 		return -EINVAL;
3304c66ac9dbSNicholas Bellinger 
330557103d7fSJingoo Han 	ret = kstrtoul(page, 0, &mode_flag);
3306c66ac9dbSNicholas Bellinger 	if (ret < 0) {
33076708bb27SAndy Grover 		pr_err("Unable to extract hba mode flag: %d\n", ret);
330857103d7fSJingoo Han 		return ret;
3309c66ac9dbSNicholas Bellinger 	}
3310c66ac9dbSNicholas Bellinger 
33110fd97ccfSChristoph Hellwig 	if (hba->dev_count) {
33126708bb27SAndy Grover 		pr_err("Unable to set hba_mode with active devices\n");
3313c66ac9dbSNicholas Bellinger 		return -EINVAL;
3314c66ac9dbSNicholas Bellinger 	}
3315c66ac9dbSNicholas Bellinger 
33160a06d430SChristoph Hellwig 	ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3317c66ac9dbSNicholas Bellinger 	if (ret < 0)
3318c66ac9dbSNicholas Bellinger 		return -EINVAL;
3319c66ac9dbSNicholas Bellinger 	if (ret > 0)
3320c66ac9dbSNicholas Bellinger 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3321c66ac9dbSNicholas Bellinger 	else if (ret == 0)
3322c66ac9dbSNicholas Bellinger 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3323c66ac9dbSNicholas Bellinger 
3324c66ac9dbSNicholas Bellinger 	return count;
3325c66ac9dbSNicholas Bellinger }
3326c66ac9dbSNicholas Bellinger 
33272eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_, hba_info);
33282eafd729SChristoph Hellwig CONFIGFS_ATTR(target_, hba_mode);
3329c66ac9dbSNicholas Bellinger 
33301f6fe7cbSNicholas Bellinger static void target_core_hba_release(struct config_item *item)
33311f6fe7cbSNicholas Bellinger {
33321f6fe7cbSNicholas Bellinger 	struct se_hba *hba = container_of(to_config_group(item),
33331f6fe7cbSNicholas Bellinger 				struct se_hba, hba_group);
33341f6fe7cbSNicholas Bellinger 	core_delete_hba(hba);
33351f6fe7cbSNicholas Bellinger }
33361f6fe7cbSNicholas Bellinger 
3337c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_hba_attrs[] = {
33382eafd729SChristoph Hellwig 	&target_attr_hba_info,
33392eafd729SChristoph Hellwig 	&target_attr_hba_mode,
3340c66ac9dbSNicholas Bellinger 	NULL,
3341c66ac9dbSNicholas Bellinger };
3342c66ac9dbSNicholas Bellinger 
3343c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_hba_item_ops = {
33441f6fe7cbSNicholas Bellinger 	.release		= target_core_hba_release,
3345c66ac9dbSNicholas Bellinger };
3346c66ac9dbSNicholas Bellinger 
3347ece550b5SBhumika Goyal static const struct config_item_type target_core_hba_cit = {
3348c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_hba_item_ops,
3349c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_hba_group_ops,
3350c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_hba_attrs,
3351c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3352c66ac9dbSNicholas Bellinger };
3353c66ac9dbSNicholas Bellinger 
3354c66ac9dbSNicholas Bellinger static struct config_group *target_core_call_addhbatotarget(
3355c66ac9dbSNicholas Bellinger 	struct config_group *group,
3356c66ac9dbSNicholas Bellinger 	const char *name)
3357c66ac9dbSNicholas Bellinger {
3358c66ac9dbSNicholas Bellinger 	char *se_plugin_str, *str, *str2;
3359c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
3360c66ac9dbSNicholas Bellinger 	char buf[TARGET_CORE_NAME_MAX_LEN];
3361c66ac9dbSNicholas Bellinger 	unsigned long plugin_dep_id = 0;
3362c66ac9dbSNicholas Bellinger 	int ret;
3363c66ac9dbSNicholas Bellinger 
3364c66ac9dbSNicholas Bellinger 	memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
336560d645a4SDan Carpenter 	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
33666708bb27SAndy Grover 		pr_err("Passed *name strlen(): %d exceeds"
3367c66ac9dbSNicholas Bellinger 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3368c66ac9dbSNicholas Bellinger 			TARGET_CORE_NAME_MAX_LEN);
3369c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENAMETOOLONG);
3370c66ac9dbSNicholas Bellinger 	}
3371c66ac9dbSNicholas Bellinger 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3372c66ac9dbSNicholas Bellinger 
3373c66ac9dbSNicholas Bellinger 	str = strstr(buf, "_");
33746708bb27SAndy Grover 	if (!str) {
33756708bb27SAndy Grover 		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3376c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
3377c66ac9dbSNicholas Bellinger 	}
3378c66ac9dbSNicholas Bellinger 	se_plugin_str = buf;
3379c66ac9dbSNicholas Bellinger 	/*
3380c66ac9dbSNicholas Bellinger 	 * Special case for subsystem plugins that have "_" in their names.
3381c66ac9dbSNicholas Bellinger 	 * Namely rd_direct and rd_mcp..
3382c66ac9dbSNicholas Bellinger 	 */
3383c66ac9dbSNicholas Bellinger 	str2 = strstr(str+1, "_");
33846708bb27SAndy Grover 	if (str2) {
3385c66ac9dbSNicholas Bellinger 		*str2 = '\0'; /* Terminate for *se_plugin_str */
3386c66ac9dbSNicholas Bellinger 		str2++; /* Skip to start of plugin dependent ID */
3387c66ac9dbSNicholas Bellinger 		str = str2;
3388c66ac9dbSNicholas Bellinger 	} else {
3389c66ac9dbSNicholas Bellinger 		*str = '\0'; /* Terminate for *se_plugin_str */
3390c66ac9dbSNicholas Bellinger 		str++; /* Skip to start of plugin dependent ID */
3391c66ac9dbSNicholas Bellinger 	}
3392c66ac9dbSNicholas Bellinger 
339357103d7fSJingoo Han 	ret = kstrtoul(str, 0, &plugin_dep_id);
3394c66ac9dbSNicholas Bellinger 	if (ret < 0) {
339557103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
3396c66ac9dbSNicholas Bellinger 				" plugin_dep_id\n", ret);
339757103d7fSJingoo Han 		return ERR_PTR(ret);
3398c66ac9dbSNicholas Bellinger 	}
3399c66ac9dbSNicholas Bellinger 	/*
3400c66ac9dbSNicholas Bellinger 	 * Load up TCM subsystem plugins if they have not already been loaded.
3401c66ac9dbSNicholas Bellinger 	 */
3402dbc5623eSNicholas Bellinger 	transport_subsystem_check_init();
3403c66ac9dbSNicholas Bellinger 
3404c66ac9dbSNicholas Bellinger 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3405c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
3406c66ac9dbSNicholas Bellinger 		return ERR_CAST(hba);
3407c66ac9dbSNicholas Bellinger 
3408c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&hba->hba_group, name,
3409c66ac9dbSNicholas Bellinger 			&target_core_hba_cit);
3410c66ac9dbSNicholas Bellinger 
3411c66ac9dbSNicholas Bellinger 	return &hba->hba_group;
3412c66ac9dbSNicholas Bellinger }
3413c66ac9dbSNicholas Bellinger 
3414c66ac9dbSNicholas Bellinger static void target_core_call_delhbafromtarget(
3415c66ac9dbSNicholas Bellinger 	struct config_group *group,
3416c66ac9dbSNicholas Bellinger 	struct config_item *item)
3417c66ac9dbSNicholas Bellinger {
34181f6fe7cbSNicholas Bellinger 	/*
34191f6fe7cbSNicholas Bellinger 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
34201f6fe7cbSNicholas Bellinger 	 * -> target_core_hba_release()
34211f6fe7cbSNicholas Bellinger 	 */
3422c66ac9dbSNicholas Bellinger 	config_item_put(item);
3423c66ac9dbSNicholas Bellinger }
3424c66ac9dbSNicholas Bellinger 
3425c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_group_ops = {
3426c66ac9dbSNicholas Bellinger 	.make_group	= target_core_call_addhbatotarget,
3427c66ac9dbSNicholas Bellinger 	.drop_item	= target_core_call_delhbafromtarget,
3428c66ac9dbSNicholas Bellinger };
3429c66ac9dbSNicholas Bellinger 
3430ece550b5SBhumika Goyal static const struct config_item_type target_core_cit = {
3431c66ac9dbSNicholas Bellinger 	.ct_item_ops	= NULL,
3432c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_group_ops,
3433c66ac9dbSNicholas Bellinger 	.ct_attrs	= NULL,
3434c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
3435c66ac9dbSNicholas Bellinger };
3436c66ac9dbSNicholas Bellinger 
3437c66ac9dbSNicholas Bellinger /* Stop functions for struct config_item_type target_core_hba_cit */
3438c66ac9dbSNicholas Bellinger 
34390a06d430SChristoph Hellwig void target_setup_backend_cits(struct target_backend *tb)
344073112edcSNicholas Bellinger {
34410a06d430SChristoph Hellwig 	target_core_setup_dev_cit(tb);
34428dc31ff9SMike Christie 	target_core_setup_dev_action_cit(tb);
34430a06d430SChristoph Hellwig 	target_core_setup_dev_attrib_cit(tb);
34440a06d430SChristoph Hellwig 	target_core_setup_dev_pr_cit(tb);
34450a06d430SChristoph Hellwig 	target_core_setup_dev_wwn_cit(tb);
34460a06d430SChristoph Hellwig 	target_core_setup_dev_alua_tg_pt_gps_cit(tb);
34470a06d430SChristoph Hellwig 	target_core_setup_dev_stat_cit(tb);
344873112edcSNicholas Bellinger }
344973112edcSNicholas Bellinger 
345078a6295cSLee Duncan static void target_init_dbroot(void)
345178a6295cSLee Duncan {
345278a6295cSLee Duncan 	struct file *fp;
345378a6295cSLee Duncan 
345478a6295cSLee Duncan 	snprintf(db_root_stage, DB_ROOT_LEN, DB_ROOT_PREFERRED);
345578a6295cSLee Duncan 	fp = filp_open(db_root_stage, O_RDONLY, 0);
345678a6295cSLee Duncan 	if (IS_ERR(fp)) {
345778a6295cSLee Duncan 		pr_err("db_root: cannot open: %s\n", db_root_stage);
345878a6295cSLee Duncan 		return;
345978a6295cSLee Duncan 	}
346078a6295cSLee Duncan 	if (!S_ISDIR(file_inode(fp)->i_mode)) {
346178a6295cSLee Duncan 		filp_close(fp, NULL);
346278a6295cSLee Duncan 		pr_err("db_root: not a valid directory: %s\n", db_root_stage);
346378a6295cSLee Duncan 		return;
346478a6295cSLee Duncan 	}
346578a6295cSLee Duncan 	filp_close(fp, NULL);
346678a6295cSLee Duncan 
346778a6295cSLee Duncan 	strncpy(db_root, db_root_stage, DB_ROOT_LEN);
346878a6295cSLee Duncan 	pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
346978a6295cSLee Duncan }
347078a6295cSLee Duncan 
347154550fabSAxel Lin static int __init target_core_init_configfs(void)
3472c66ac9dbSNicholas Bellinger {
3473d588cf8fSChristoph Hellwig 	struct configfs_subsystem *subsys = &target_core_fabrics;
3474c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
3475c66ac9dbSNicholas Bellinger 	int ret;
3476c66ac9dbSNicholas Bellinger 
34776708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3478c66ac9dbSNicholas Bellinger 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
3479c66ac9dbSNicholas Bellinger 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3480c66ac9dbSNicholas Bellinger 
3481c66ac9dbSNicholas Bellinger 	config_group_init(&subsys->su_group);
3482c66ac9dbSNicholas Bellinger 	mutex_init(&subsys->su_mutex);
3483c66ac9dbSNicholas Bellinger 
3484e3d6f909SAndy Grover 	ret = init_se_kmem_caches();
3485c66ac9dbSNicholas Bellinger 	if (ret < 0)
3486e3d6f909SAndy Grover 		return ret;
3487c66ac9dbSNicholas Bellinger 	/*
3488c66ac9dbSNicholas Bellinger 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3489c66ac9dbSNicholas Bellinger 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3490c66ac9dbSNicholas Bellinger 	 */
34911ae1602dSChristoph Hellwig 	config_group_init_type_name(&target_core_hbagroup, "core",
34921ae1602dSChristoph Hellwig 			&target_core_cit);
34931ae1602dSChristoph Hellwig 	configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
3494c66ac9dbSNicholas Bellinger 
3495c66ac9dbSNicholas Bellinger 	/*
3496c66ac9dbSNicholas Bellinger 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3497c66ac9dbSNicholas Bellinger 	 */
34981ae1602dSChristoph Hellwig 	config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
34991ae1602dSChristoph Hellwig 	configfs_add_default_group(&alua_group, &target_core_hbagroup);
35001ae1602dSChristoph Hellwig 
3501c66ac9dbSNicholas Bellinger 	/*
3502c66ac9dbSNicholas Bellinger 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3503c66ac9dbSNicholas Bellinger 	 * groups under /sys/kernel/config/target/core/alua/
3504c66ac9dbSNicholas Bellinger 	 */
35051ae1602dSChristoph Hellwig 	config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
35061ae1602dSChristoph Hellwig 			&target_core_alua_lu_gps_cit);
35071ae1602dSChristoph Hellwig 	configfs_add_default_group(&alua_lu_gps_group, &alua_group);
3508c66ac9dbSNicholas Bellinger 
3509c66ac9dbSNicholas Bellinger 	/*
3510c66ac9dbSNicholas Bellinger 	 * Add core/alua/lu_gps/default_lu_gp
3511c66ac9dbSNicholas Bellinger 	 */
3512c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
351337bb7899SPeter Senna Tschudin 	if (IS_ERR(lu_gp)) {
351437bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3515c66ac9dbSNicholas Bellinger 		goto out_global;
351637bb7899SPeter Senna Tschudin 	}
3517c66ac9dbSNicholas Bellinger 
3518c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3519c66ac9dbSNicholas Bellinger 				&target_core_alua_lu_gp_cit);
35201ae1602dSChristoph Hellwig 	configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
35211ae1602dSChristoph Hellwig 
3522e3d6f909SAndy Grover 	default_lu_gp = lu_gp;
35231ae1602dSChristoph Hellwig 
3524c66ac9dbSNicholas Bellinger 	/*
3525c66ac9dbSNicholas Bellinger 	 * Register the target_core_mod subsystem with configfs.
3526c66ac9dbSNicholas Bellinger 	 */
3527c66ac9dbSNicholas Bellinger 	ret = configfs_register_subsystem(subsys);
3528c66ac9dbSNicholas Bellinger 	if (ret < 0) {
35296708bb27SAndy Grover 		pr_err("Error %d while registering subsystem %s\n",
3530c66ac9dbSNicholas Bellinger 			ret, subsys->su_group.cg_item.ci_namebuf);
3531c66ac9dbSNicholas Bellinger 		goto out_global;
3532c66ac9dbSNicholas Bellinger 	}
35336708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3534ce8dd25dSChristoph Hellwig 		" Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3535c66ac9dbSNicholas Bellinger 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3536c66ac9dbSNicholas Bellinger 	/*
3537c66ac9dbSNicholas Bellinger 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3538c66ac9dbSNicholas Bellinger 	 */
3539c66ac9dbSNicholas Bellinger 	ret = rd_module_init();
3540c66ac9dbSNicholas Bellinger 	if (ret < 0)
3541c66ac9dbSNicholas Bellinger 		goto out;
3542c66ac9dbSNicholas Bellinger 
35430d0f9dfbSRoland Dreier 	ret = core_dev_setup_virtual_lun0();
35440d0f9dfbSRoland Dreier 	if (ret < 0)
3545c66ac9dbSNicholas Bellinger 		goto out;
3546c66ac9dbSNicholas Bellinger 
3547f99715acSNicholas Bellinger 	ret = target_xcopy_setup_pt();
3548f99715acSNicholas Bellinger 	if (ret < 0)
3549f99715acSNicholas Bellinger 		goto out;
3550f99715acSNicholas Bellinger 
355178a6295cSLee Duncan 	target_init_dbroot();
355278a6295cSLee Duncan 
3553c66ac9dbSNicholas Bellinger 	return 0;
3554c66ac9dbSNicholas Bellinger 
3555c66ac9dbSNicholas Bellinger out:
3556c66ac9dbSNicholas Bellinger 	configfs_unregister_subsystem(subsys);
3557c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3558c66ac9dbSNicholas Bellinger 	rd_module_exit();
3559c66ac9dbSNicholas Bellinger out_global:
3560e3d6f909SAndy Grover 	if (default_lu_gp) {
3561e3d6f909SAndy Grover 		core_alua_free_lu_gp(default_lu_gp);
3562e3d6f909SAndy Grover 		default_lu_gp = NULL;
3563c66ac9dbSNicholas Bellinger 	}
3564e3d6f909SAndy Grover 	release_se_kmem_caches();
3565e3d6f909SAndy Grover 	return ret;
3566c66ac9dbSNicholas Bellinger }
3567c66ac9dbSNicholas Bellinger 
356854550fabSAxel Lin static void __exit target_core_exit_configfs(void)
3569c66ac9dbSNicholas Bellinger {
35701ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&alua_lu_gps_group);
35711ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&alua_group);
35721ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&target_core_hbagroup);
3573c66ac9dbSNicholas Bellinger 
35747c2bf6e9SNicholas Bellinger 	/*
35757c2bf6e9SNicholas Bellinger 	 * We expect subsys->su_group.default_groups to be released
35767c2bf6e9SNicholas Bellinger 	 * by configfs subsystem provider logic..
35777c2bf6e9SNicholas Bellinger 	 */
3578d588cf8fSChristoph Hellwig 	configfs_unregister_subsystem(&target_core_fabrics);
3579c66ac9dbSNicholas Bellinger 
3580e3d6f909SAndy Grover 	core_alua_free_lu_gp(default_lu_gp);
3581e3d6f909SAndy Grover 	default_lu_gp = NULL;
35827c2bf6e9SNicholas Bellinger 
35836708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3584c66ac9dbSNicholas Bellinger 			" Infrastructure\n");
3585c66ac9dbSNicholas Bellinger 
3586c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3587c66ac9dbSNicholas Bellinger 	rd_module_exit();
3588f99715acSNicholas Bellinger 	target_xcopy_release_pt();
3589e3d6f909SAndy Grover 	release_se_kmem_caches();
3590c66ac9dbSNicholas Bellinger }
3591c66ac9dbSNicholas Bellinger 
3592c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3593c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3594c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
3595c66ac9dbSNicholas Bellinger 
3596c66ac9dbSNicholas Bellinger module_init(target_core_init_configfs);
3597c66ac9dbSNicholas Bellinger module_exit(target_core_exit_configfs);
3598