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);
110269088a04SBodo Stroesser 	u8 flags = da->da_dev->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 
1108356ba2a8SBodo Stroesser static ssize_t alua_support_store(struct config_item *item,
1109356ba2a8SBodo Stroesser 		const char *page, size_t count)
1110356ba2a8SBodo Stroesser {
1111356ba2a8SBodo Stroesser 	struct se_dev_attrib *da = to_attrib(item);
1112356ba2a8SBodo Stroesser 	struct se_device *dev = da->da_dev;
1113*ef7ae7f7SMaurizio Lombardi 	bool flag, oldflag;
1114356ba2a8SBodo Stroesser 	int ret;
1115356ba2a8SBodo Stroesser 
1116*ef7ae7f7SMaurizio Lombardi 	ret = strtobool(page, &flag);
1117*ef7ae7f7SMaurizio Lombardi 	if (ret < 0)
1118*ef7ae7f7SMaurizio Lombardi 		return ret;
1119*ef7ae7f7SMaurizio Lombardi 
1120*ef7ae7f7SMaurizio Lombardi 	oldflag = !(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA);
1121*ef7ae7f7SMaurizio Lombardi 	if (flag == oldflag)
1122*ef7ae7f7SMaurizio Lombardi 		return count;
1123*ef7ae7f7SMaurizio Lombardi 
1124356ba2a8SBodo Stroesser 	if (!(dev->transport->transport_flags_changeable &
1125356ba2a8SBodo Stroesser 	      TRANSPORT_FLAG_PASSTHROUGH_ALUA)) {
1126356ba2a8SBodo Stroesser 		pr_err("dev[%p]: Unable to change SE Device alua_support:"
1127356ba2a8SBodo Stroesser 			" alua_support has fixed value\n", dev);
1128*ef7ae7f7SMaurizio Lombardi 		return -ENOSYS;
1129356ba2a8SBodo Stroesser 	}
1130356ba2a8SBodo Stroesser 
1131356ba2a8SBodo Stroesser 	if (flag)
1132356ba2a8SBodo Stroesser 		dev->transport_flags &= ~TRANSPORT_FLAG_PASSTHROUGH_ALUA;
1133356ba2a8SBodo Stroesser 	else
1134356ba2a8SBodo Stroesser 		dev->transport_flags |= TRANSPORT_FLAG_PASSTHROUGH_ALUA;
1135356ba2a8SBodo Stroesser 	return count;
1136356ba2a8SBodo Stroesser }
1137356ba2a8SBodo Stroesser 
1138c17d5d5fSMike Christie static ssize_t pgr_support_show(struct config_item *item, char *page)
1139c17d5d5fSMike Christie {
1140c17d5d5fSMike Christie 	struct se_dev_attrib *da = to_attrib(item);
114169088a04SBodo Stroesser 	u8 flags = da->da_dev->transport_flags;
1142c17d5d5fSMike Christie 
1143c17d5d5fSMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n",
1144c17d5d5fSMike Christie 			flags & TRANSPORT_FLAG_PASSTHROUGH_PGR ? 0 : 1);
1145c17d5d5fSMike Christie }
1146c17d5d5fSMike Christie 
1147356ba2a8SBodo Stroesser static ssize_t pgr_support_store(struct config_item *item,
1148356ba2a8SBodo Stroesser 		const char *page, size_t count)
1149356ba2a8SBodo Stroesser {
1150356ba2a8SBodo Stroesser 	struct se_dev_attrib *da = to_attrib(item);
1151356ba2a8SBodo Stroesser 	struct se_device *dev = da->da_dev;
1152*ef7ae7f7SMaurizio Lombardi 	bool flag, oldflag;
1153356ba2a8SBodo Stroesser 	int ret;
1154356ba2a8SBodo Stroesser 
1155*ef7ae7f7SMaurizio Lombardi 	ret = strtobool(page, &flag);
1156*ef7ae7f7SMaurizio Lombardi 	if (ret < 0)
1157*ef7ae7f7SMaurizio Lombardi 		return ret;
1158*ef7ae7f7SMaurizio Lombardi 
1159*ef7ae7f7SMaurizio Lombardi 	oldflag = !(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR);
1160*ef7ae7f7SMaurizio Lombardi 	if (flag == oldflag)
1161*ef7ae7f7SMaurizio Lombardi 		return count;
1162*ef7ae7f7SMaurizio Lombardi 
1163356ba2a8SBodo Stroesser 	if (!(dev->transport->transport_flags_changeable &
1164356ba2a8SBodo Stroesser 	      TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
1165356ba2a8SBodo Stroesser 		pr_err("dev[%p]: Unable to change SE Device pgr_support:"
1166356ba2a8SBodo Stroesser 			" pgr_support has fixed value\n", dev);
1167*ef7ae7f7SMaurizio Lombardi 		return -ENOSYS;
1168356ba2a8SBodo Stroesser 	}
1169356ba2a8SBodo Stroesser 
1170356ba2a8SBodo Stroesser 	if (flag)
1171356ba2a8SBodo Stroesser 		dev->transport_flags &= ~TRANSPORT_FLAG_PASSTHROUGH_PGR;
1172356ba2a8SBodo Stroesser 	else
1173356ba2a8SBodo Stroesser 		dev->transport_flags |= TRANSPORT_FLAG_PASSTHROUGH_PGR;
1174356ba2a8SBodo Stroesser 	return count;
1175356ba2a8SBodo Stroesser }
1176356ba2a8SBodo Stroesser 
11772eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_model_alias);
11782eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_dpo);
11792eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_write);
11802eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_read);
11812eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_write_cache);
11822eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
11832eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tas);
11842eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpu);
11852eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpws);
11862eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_caw);
11872eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_3pc);
1188b49d6f78SDavid Disseldorp CONFIGFS_ATTR(, emulate_pr);
11892eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_type);
11902eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_pi_prot_type);
1191b6cd7f34SDavid Disseldorp CONFIGFS_ATTR(, pi_prot_format);
1192056e8924SDmitry Monakhov CONFIGFS_ATTR(, pi_prot_verify);
11932eafd729SChristoph Hellwig CONFIGFS_ATTR(, enforce_pr_isids);
11942eafd729SChristoph Hellwig CONFIGFS_ATTR(, is_nonrot);
11952eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_rest_reord);
11962eafd729SChristoph Hellwig CONFIGFS_ATTR(, force_pr_aptpl);
11972eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_block_size);
11982eafd729SChristoph Hellwig CONFIGFS_ATTR(, block_size);
11992eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_max_sectors);
12002eafd729SChristoph Hellwig CONFIGFS_ATTR(, optimal_sectors);
12012eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_queue_depth);
12022eafd729SChristoph Hellwig CONFIGFS_ATTR(, queue_depth);
12032eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_lba_count);
12042eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_block_desc_count);
12052eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity);
12062eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity_alignment);
1207e6f41633SJamie Pocas CONFIGFS_ATTR(, unmap_zeroes_data);
12082eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_write_same_len);
1209356ba2a8SBodo Stroesser CONFIGFS_ATTR(, alua_support);
1210356ba2a8SBodo Stroesser CONFIGFS_ATTR(, pgr_support);
1211c66ac9dbSNicholas Bellinger 
12125873c4d1SChristoph Hellwig /*
12135873c4d1SChristoph Hellwig  * dev_attrib attributes for devices using the target core SBC/SPC
12145873c4d1SChristoph Hellwig  * interpreter.  Any backend using spc_parse_cdb should be using
12155873c4d1SChristoph Hellwig  * these.
12165873c4d1SChristoph Hellwig  */
12175873c4d1SChristoph Hellwig struct configfs_attribute *sbc_attrib_attrs[] = {
12182eafd729SChristoph Hellwig 	&attr_emulate_model_alias,
12192eafd729SChristoph Hellwig 	&attr_emulate_dpo,
12202eafd729SChristoph Hellwig 	&attr_emulate_fua_write,
12212eafd729SChristoph Hellwig 	&attr_emulate_fua_read,
12222eafd729SChristoph Hellwig 	&attr_emulate_write_cache,
12232eafd729SChristoph Hellwig 	&attr_emulate_ua_intlck_ctrl,
12242eafd729SChristoph Hellwig 	&attr_emulate_tas,
12252eafd729SChristoph Hellwig 	&attr_emulate_tpu,
12262eafd729SChristoph Hellwig 	&attr_emulate_tpws,
12272eafd729SChristoph Hellwig 	&attr_emulate_caw,
12282eafd729SChristoph Hellwig 	&attr_emulate_3pc,
1229b49d6f78SDavid Disseldorp 	&attr_emulate_pr,
12302eafd729SChristoph Hellwig 	&attr_pi_prot_type,
12312eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
12322eafd729SChristoph Hellwig 	&attr_pi_prot_format,
1233056e8924SDmitry Monakhov 	&attr_pi_prot_verify,
12342eafd729SChristoph Hellwig 	&attr_enforce_pr_isids,
12352eafd729SChristoph Hellwig 	&attr_is_nonrot,
12362eafd729SChristoph Hellwig 	&attr_emulate_rest_reord,
12372eafd729SChristoph Hellwig 	&attr_force_pr_aptpl,
12382eafd729SChristoph Hellwig 	&attr_hw_block_size,
12392eafd729SChristoph Hellwig 	&attr_block_size,
12402eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
12412eafd729SChristoph Hellwig 	&attr_optimal_sectors,
12422eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
12432eafd729SChristoph Hellwig 	&attr_queue_depth,
12442eafd729SChristoph Hellwig 	&attr_max_unmap_lba_count,
12452eafd729SChristoph Hellwig 	&attr_max_unmap_block_desc_count,
12462eafd729SChristoph Hellwig 	&attr_unmap_granularity,
12472eafd729SChristoph Hellwig 	&attr_unmap_granularity_alignment,
1248e6f41633SJamie Pocas 	&attr_unmap_zeroes_data,
12492eafd729SChristoph Hellwig 	&attr_max_write_same_len,
1250c17d5d5fSMike Christie 	&attr_alua_support,
1251c17d5d5fSMike Christie 	&attr_pgr_support,
12525873c4d1SChristoph Hellwig 	NULL,
12535873c4d1SChristoph Hellwig };
12545873c4d1SChristoph Hellwig EXPORT_SYMBOL(sbc_attrib_attrs);
12555873c4d1SChristoph Hellwig 
12565873c4d1SChristoph Hellwig /*
12575873c4d1SChristoph Hellwig  * Minimal dev_attrib attributes for devices passing through CDBs.
12585873c4d1SChristoph Hellwig  * In this case we only provide a few read-only attributes for
12595873c4d1SChristoph Hellwig  * backwards compatibility.
12605873c4d1SChristoph Hellwig  */
12615873c4d1SChristoph Hellwig struct configfs_attribute *passthrough_attrib_attrs[] = {
12622eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
12632eafd729SChristoph Hellwig 	&attr_hw_block_size,
12642eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
12652eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
126692999417SBodo Stroesser 	&attr_emulate_pr,
1267c17d5d5fSMike Christie 	&attr_alua_support,
1268c17d5d5fSMike Christie 	&attr_pgr_support,
12695873c4d1SChristoph Hellwig 	NULL,
12705873c4d1SChristoph Hellwig };
12715873c4d1SChristoph Hellwig EXPORT_SYMBOL(passthrough_attrib_attrs);
12725873c4d1SChristoph Hellwig 
12734703b625SBodo Stroesser /*
12744703b625SBodo Stroesser  * pr related dev_attrib attributes for devices passing through CDBs,
12754703b625SBodo Stroesser  * but allowing in core pr emulation.
12764703b625SBodo Stroesser  */
12774703b625SBodo Stroesser struct configfs_attribute *passthrough_pr_attrib_attrs[] = {
12784703b625SBodo Stroesser 	&attr_enforce_pr_isids,
12794703b625SBodo Stroesser 	&attr_force_pr_aptpl,
12804703b625SBodo Stroesser 	NULL,
12814703b625SBodo Stroesser };
12824703b625SBodo Stroesser EXPORT_SYMBOL(passthrough_pr_attrib_attrs);
12834703b625SBodo Stroesser 
12842eafd729SChristoph Hellwig TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
12858dc31ff9SMike Christie TB_CIT_SETUP_DRV(dev_action, NULL, NULL);
1286c66ac9dbSNicholas Bellinger 
1287f79a897eSNicholas Bellinger /* End functions for struct config_item_type tb_dev_attrib_cit */
1288c66ac9dbSNicholas Bellinger 
1289f8d389c6SNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_wwn_cit */
1290c66ac9dbSNicholas Bellinger 
12912eafd729SChristoph Hellwig static struct t10_wwn *to_t10_wwn(struct config_item *item)
12922eafd729SChristoph Hellwig {
12932eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
12942eafd729SChristoph Hellwig }
1295c66ac9dbSNicholas Bellinger 
12960322913cSAlan Adamson static ssize_t target_check_inquiry_data(char *buf)
12970322913cSAlan Adamson {
12980322913cSAlan Adamson 	size_t len;
12990322913cSAlan Adamson 	int i;
13000322913cSAlan Adamson 
13010322913cSAlan Adamson 	len = strlen(buf);
13020322913cSAlan Adamson 
13030322913cSAlan Adamson 	/*
13040322913cSAlan Adamson 	 * SPC 4.3.1:
13050322913cSAlan Adamson 	 * ASCII data fields shall contain only ASCII printable characters
13060322913cSAlan Adamson 	 * (i.e., code values 20h to 7Eh) and may be terminated with one or
13070322913cSAlan Adamson 	 * more ASCII null (00h) characters.
13080322913cSAlan Adamson 	 */
13090322913cSAlan Adamson 	for (i = 0; i < len; i++) {
13100322913cSAlan Adamson 		if (buf[i] < 0x20 || buf[i] > 0x7E) {
13110322913cSAlan Adamson 			pr_err("Emulated T10 Inquiry Data contains non-ASCII-printable characters\n");
13120322913cSAlan Adamson 			return -EINVAL;
13130322913cSAlan Adamson 		}
13140322913cSAlan Adamson 	}
13150322913cSAlan Adamson 
13160322913cSAlan Adamson 	return len;
13170322913cSAlan Adamson }
13180322913cSAlan Adamson 
1319c66ac9dbSNicholas Bellinger /*
132054a6f3f6SDavid Disseldorp  * STANDARD and VPD page 0x83 T10 Vendor Identification
132154a6f3f6SDavid Disseldorp  */
132254a6f3f6SDavid Disseldorp static ssize_t target_wwn_vendor_id_show(struct config_item *item,
132354a6f3f6SDavid Disseldorp 		char *page)
132454a6f3f6SDavid Disseldorp {
132554a6f3f6SDavid Disseldorp 	return sprintf(page, "%s\n", &to_t10_wwn(item)->vendor[0]);
132654a6f3f6SDavid Disseldorp }
132754a6f3f6SDavid Disseldorp 
132854a6f3f6SDavid Disseldorp static ssize_t target_wwn_vendor_id_store(struct config_item *item,
132954a6f3f6SDavid Disseldorp 		const char *page, size_t count)
133054a6f3f6SDavid Disseldorp {
133154a6f3f6SDavid Disseldorp 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
133254a6f3f6SDavid Disseldorp 	struct se_device *dev = t10_wwn->t10_dev;
133354a6f3f6SDavid Disseldorp 	/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
133454a6f3f6SDavid Disseldorp 	unsigned char buf[INQUIRY_VENDOR_LEN + 2];
133554a6f3f6SDavid Disseldorp 	char *stripped = NULL;
1336ee26724aSColin Ian King 	size_t len;
1337ee26724aSColin Ian King 	ssize_t ret;
133854a6f3f6SDavid Disseldorp 
133954a6f3f6SDavid Disseldorp 	len = strlcpy(buf, page, sizeof(buf));
134054a6f3f6SDavid Disseldorp 	if (len < sizeof(buf)) {
134154a6f3f6SDavid Disseldorp 		/* Strip any newline added from userspace. */
134254a6f3f6SDavid Disseldorp 		stripped = strstrip(buf);
134354a6f3f6SDavid Disseldorp 		len = strlen(stripped);
134454a6f3f6SDavid Disseldorp 	}
134554a6f3f6SDavid Disseldorp 	if (len > INQUIRY_VENDOR_LEN) {
134654a6f3f6SDavid Disseldorp 		pr_err("Emulated T10 Vendor Identification exceeds"
134754a6f3f6SDavid Disseldorp 			" INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)
134854a6f3f6SDavid Disseldorp 			"\n");
134954a6f3f6SDavid Disseldorp 		return -EOVERFLOW;
135054a6f3f6SDavid Disseldorp 	}
135154a6f3f6SDavid Disseldorp 
13520322913cSAlan Adamson 	ret = target_check_inquiry_data(stripped);
13530322913cSAlan Adamson 
13540322913cSAlan Adamson 	if (ret < 0)
13550322913cSAlan Adamson 		return ret;
135654a6f3f6SDavid Disseldorp 
135754a6f3f6SDavid Disseldorp 	/*
135854a6f3f6SDavid Disseldorp 	 * Check to see if any active exports exist.  If they do exist, fail
135954a6f3f6SDavid Disseldorp 	 * here as changing this information on the fly (underneath the
136054a6f3f6SDavid Disseldorp 	 * initiator side OS dependent multipath code) could cause negative
136154a6f3f6SDavid Disseldorp 	 * effects.
136254a6f3f6SDavid Disseldorp 	 */
136354a6f3f6SDavid Disseldorp 	if (dev->export_count) {
136454a6f3f6SDavid Disseldorp 		pr_err("Unable to set T10 Vendor Identification while"
136554a6f3f6SDavid Disseldorp 			" active %d exports exist\n", dev->export_count);
136654a6f3f6SDavid Disseldorp 		return -EINVAL;
136754a6f3f6SDavid Disseldorp 	}
136854a6f3f6SDavid Disseldorp 
136954a6f3f6SDavid Disseldorp 	BUILD_BUG_ON(sizeof(dev->t10_wwn.vendor) != INQUIRY_VENDOR_LEN + 1);
137054a6f3f6SDavid Disseldorp 	strlcpy(dev->t10_wwn.vendor, stripped, sizeof(dev->t10_wwn.vendor));
137154a6f3f6SDavid Disseldorp 
137254a6f3f6SDavid Disseldorp 	pr_debug("Target_Core_ConfigFS: Set emulated T10 Vendor Identification:"
137354a6f3f6SDavid Disseldorp 		 " %s\n", dev->t10_wwn.vendor);
137454a6f3f6SDavid Disseldorp 
137554a6f3f6SDavid Disseldorp 	return count;
137654a6f3f6SDavid Disseldorp }
137754a6f3f6SDavid Disseldorp 
13780322913cSAlan Adamson static ssize_t target_wwn_product_id_show(struct config_item *item,
13790322913cSAlan Adamson 		char *page)
13800322913cSAlan Adamson {
13810322913cSAlan Adamson 	return sprintf(page, "%s\n", &to_t10_wwn(item)->model[0]);
13820322913cSAlan Adamson }
13830322913cSAlan Adamson 
13840322913cSAlan Adamson static ssize_t target_wwn_product_id_store(struct config_item *item,
13850322913cSAlan Adamson 		const char *page, size_t count)
13860322913cSAlan Adamson {
13870322913cSAlan Adamson 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
13880322913cSAlan Adamson 	struct se_device *dev = t10_wwn->t10_dev;
13890322913cSAlan Adamson 	/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
13900322913cSAlan Adamson 	unsigned char buf[INQUIRY_MODEL_LEN + 2];
13910322913cSAlan Adamson 	char *stripped = NULL;
1392ee26724aSColin Ian King 	size_t len;
1393ee26724aSColin Ian King 	ssize_t ret;
13940322913cSAlan Adamson 
13950322913cSAlan Adamson 	len = strlcpy(buf, page, sizeof(buf));
13960322913cSAlan Adamson 	if (len < sizeof(buf)) {
13970322913cSAlan Adamson 		/* Strip any newline added from userspace. */
13980322913cSAlan Adamson 		stripped = strstrip(buf);
13990322913cSAlan Adamson 		len = strlen(stripped);
14000322913cSAlan Adamson 	}
14010322913cSAlan Adamson 	if (len > INQUIRY_MODEL_LEN) {
14020322913cSAlan Adamson 		pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: "
14030322913cSAlan Adamson 			 __stringify(INQUIRY_MODEL_LEN)
14040322913cSAlan Adamson 			"\n");
14050322913cSAlan Adamson 		return -EOVERFLOW;
14060322913cSAlan Adamson 	}
14070322913cSAlan Adamson 
14080322913cSAlan Adamson 	ret = target_check_inquiry_data(stripped);
14090322913cSAlan Adamson 
14100322913cSAlan Adamson 	if (ret < 0)
14110322913cSAlan Adamson 		return ret;
14120322913cSAlan Adamson 
14130322913cSAlan Adamson 	/*
14140322913cSAlan Adamson 	 * Check to see if any active exports exist.  If they do exist, fail
14150322913cSAlan Adamson 	 * here as changing this information on the fly (underneath the
14160322913cSAlan Adamson 	 * initiator side OS dependent multipath code) could cause negative
14170322913cSAlan Adamson 	 * effects.
14180322913cSAlan Adamson 	 */
14190322913cSAlan Adamson 	if (dev->export_count) {
14200322913cSAlan Adamson 		pr_err("Unable to set T10 Model while active %d exports exist\n",
14210322913cSAlan Adamson 			dev->export_count);
14220322913cSAlan Adamson 		return -EINVAL;
14230322913cSAlan Adamson 	}
14240322913cSAlan Adamson 
14250322913cSAlan Adamson 	BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
14260322913cSAlan Adamson 	strlcpy(dev->t10_wwn.model, stripped, sizeof(dev->t10_wwn.model));
14270322913cSAlan Adamson 
14280322913cSAlan Adamson 	pr_debug("Target_Core_ConfigFS: Set emulated T10 Model Identification: %s\n",
14290322913cSAlan Adamson 		 dev->t10_wwn.model);
14300322913cSAlan Adamson 
14310322913cSAlan Adamson 	return count;
14320322913cSAlan Adamson }
14330322913cSAlan Adamson 
14340322913cSAlan Adamson static ssize_t target_wwn_revision_show(struct config_item *item,
14350322913cSAlan Adamson 		char *page)
14360322913cSAlan Adamson {
14370322913cSAlan Adamson 	return sprintf(page, "%s\n", &to_t10_wwn(item)->revision[0]);
14380322913cSAlan Adamson }
14390322913cSAlan Adamson 
14400322913cSAlan Adamson static ssize_t target_wwn_revision_store(struct config_item *item,
14410322913cSAlan Adamson 		const char *page, size_t count)
14420322913cSAlan Adamson {
14430322913cSAlan Adamson 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
14440322913cSAlan Adamson 	struct se_device *dev = t10_wwn->t10_dev;
14450322913cSAlan Adamson 	/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
14460322913cSAlan Adamson 	unsigned char buf[INQUIRY_REVISION_LEN + 2];
14470322913cSAlan Adamson 	char *stripped = NULL;
1448ee26724aSColin Ian King 	size_t len;
1449ee26724aSColin Ian King 	ssize_t ret;
14500322913cSAlan Adamson 
14510322913cSAlan Adamson 	len = strlcpy(buf, page, sizeof(buf));
14520322913cSAlan Adamson 	if (len < sizeof(buf)) {
14530322913cSAlan Adamson 		/* Strip any newline added from userspace. */
14540322913cSAlan Adamson 		stripped = strstrip(buf);
14550322913cSAlan Adamson 		len = strlen(stripped);
14560322913cSAlan Adamson 	}
14570322913cSAlan Adamson 	if (len > INQUIRY_REVISION_LEN) {
14580322913cSAlan Adamson 		pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: "
14590322913cSAlan Adamson 			 __stringify(INQUIRY_REVISION_LEN)
14600322913cSAlan Adamson 			"\n");
14610322913cSAlan Adamson 		return -EOVERFLOW;
14620322913cSAlan Adamson 	}
14630322913cSAlan Adamson 
14640322913cSAlan Adamson 	ret = target_check_inquiry_data(stripped);
14650322913cSAlan Adamson 
14660322913cSAlan Adamson 	if (ret < 0)
14670322913cSAlan Adamson 		return ret;
14680322913cSAlan Adamson 
14690322913cSAlan Adamson 	/*
14700322913cSAlan Adamson 	 * Check to see if any active exports exist.  If they do exist, fail
14710322913cSAlan Adamson 	 * here as changing this information on the fly (underneath the
14720322913cSAlan Adamson 	 * initiator side OS dependent multipath code) could cause negative
14730322913cSAlan Adamson 	 * effects.
14740322913cSAlan Adamson 	 */
14750322913cSAlan Adamson 	if (dev->export_count) {
14760322913cSAlan Adamson 		pr_err("Unable to set T10 Revision while active %d exports exist\n",
14770322913cSAlan Adamson 			dev->export_count);
14780322913cSAlan Adamson 		return -EINVAL;
14790322913cSAlan Adamson 	}
14800322913cSAlan Adamson 
14810322913cSAlan Adamson 	BUILD_BUG_ON(sizeof(dev->t10_wwn.revision) != INQUIRY_REVISION_LEN + 1);
14820322913cSAlan Adamson 	strlcpy(dev->t10_wwn.revision, stripped, sizeof(dev->t10_wwn.revision));
14830322913cSAlan Adamson 
14840322913cSAlan Adamson 	pr_debug("Target_Core_ConfigFS: Set emulated T10 Revision: %s\n",
14850322913cSAlan Adamson 		 dev->t10_wwn.revision);
14860322913cSAlan Adamson 
14870322913cSAlan Adamson 	return count;
14880322913cSAlan Adamson }
14890322913cSAlan Adamson 
14902469f1e0SSergey Samoylenko static ssize_t
14912469f1e0SSergey Samoylenko target_wwn_company_id_show(struct config_item *item,
14922469f1e0SSergey Samoylenko 				char *page)
14932469f1e0SSergey Samoylenko {
14942469f1e0SSergey Samoylenko 	return snprintf(page, PAGE_SIZE, "%#08x\n",
14952469f1e0SSergey Samoylenko 			to_t10_wwn(item)->company_id);
14962469f1e0SSergey Samoylenko }
14972469f1e0SSergey Samoylenko 
14982469f1e0SSergey Samoylenko static ssize_t
14992469f1e0SSergey Samoylenko target_wwn_company_id_store(struct config_item *item,
15002469f1e0SSergey Samoylenko 				 const char *page, size_t count)
15012469f1e0SSergey Samoylenko {
15022469f1e0SSergey Samoylenko 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
15032469f1e0SSergey Samoylenko 	struct se_device *dev = t10_wwn->t10_dev;
15042469f1e0SSergey Samoylenko 	u32 val;
15052469f1e0SSergey Samoylenko 	int ret;
15062469f1e0SSergey Samoylenko 
15072469f1e0SSergey Samoylenko 	/*
15082469f1e0SSergey Samoylenko 	 * The IEEE COMPANY_ID field should contain a 24-bit canonical
15092469f1e0SSergey Samoylenko 	 * form OUI assigned by the IEEE.
15102469f1e0SSergey Samoylenko 	 */
15112469f1e0SSergey Samoylenko 	ret = kstrtou32(page, 0, &val);
15122469f1e0SSergey Samoylenko 	if (ret < 0)
15132469f1e0SSergey Samoylenko 		return ret;
15142469f1e0SSergey Samoylenko 
15152469f1e0SSergey Samoylenko 	if (val >= 0x1000000)
15162469f1e0SSergey Samoylenko 		return -EOVERFLOW;
15172469f1e0SSergey Samoylenko 
15182469f1e0SSergey Samoylenko 	/*
15192469f1e0SSergey Samoylenko 	 * Check to see if any active exports exist. If they do exist, fail
15202469f1e0SSergey Samoylenko 	 * here as changing this information on the fly (underneath the
15212469f1e0SSergey Samoylenko 	 * initiator side OS dependent multipath code) could cause negative
15222469f1e0SSergey Samoylenko 	 * effects.
15232469f1e0SSergey Samoylenko 	 */
15242469f1e0SSergey Samoylenko 	if (dev->export_count) {
15252469f1e0SSergey Samoylenko 		pr_err("Unable to set Company ID while %u exports exist\n",
15262469f1e0SSergey Samoylenko 		       dev->export_count);
15272469f1e0SSergey Samoylenko 		return -EINVAL;
15282469f1e0SSergey Samoylenko 	}
15292469f1e0SSergey Samoylenko 
15302469f1e0SSergey Samoylenko 	t10_wwn->company_id = val;
15312469f1e0SSergey Samoylenko 
15322469f1e0SSergey Samoylenko 	pr_debug("Target_Core_ConfigFS: Set IEEE Company ID: %#08x\n",
15332469f1e0SSergey Samoylenko 		 t10_wwn->company_id);
15342469f1e0SSergey Samoylenko 
15352469f1e0SSergey Samoylenko 	return count;
15362469f1e0SSergey Samoylenko }
15372469f1e0SSergey Samoylenko 
153854a6f3f6SDavid Disseldorp /*
1539c66ac9dbSNicholas Bellinger  * VPD page 0x80 Unit serial
1540c66ac9dbSNicholas Bellinger  */
15412eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1542c66ac9dbSNicholas Bellinger 		char *page)
1543c66ac9dbSNicholas Bellinger {
1544c66ac9dbSNicholas Bellinger 	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
15452eafd729SChristoph Hellwig 		&to_t10_wwn(item)->unit_serial[0]);
1546c66ac9dbSNicholas Bellinger }
1547c66ac9dbSNicholas Bellinger 
15482eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
15492eafd729SChristoph Hellwig 		const char *page, size_t count)
1550c66ac9dbSNicholas Bellinger {
15512eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
15520fd97ccfSChristoph Hellwig 	struct se_device *dev = t10_wwn->t10_dev;
15532d4e2dafSChaitanya Kulkarni 	unsigned char buf[INQUIRY_VPD_SERIAL_LEN] = { };
1554c66ac9dbSNicholas Bellinger 
1555c66ac9dbSNicholas Bellinger 	/*
1556c66ac9dbSNicholas Bellinger 	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1557c66ac9dbSNicholas Bellinger 	 * from the struct scsi_device level firmware, do not allow
1558c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial to be emulated.
1559c66ac9dbSNicholas Bellinger 	 *
1560c66ac9dbSNicholas Bellinger 	 * Note this struct scsi_device could also be emulating VPD
1561c66ac9dbSNicholas Bellinger 	 * information from its drivers/scsi LLD.  But for now we assume
1562c66ac9dbSNicholas Bellinger 	 * it is doing 'the right thing' wrt a world wide unique
1563c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial Number that OS dependent multipath can depend on.
1564c66ac9dbSNicholas Bellinger 	 */
15650fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
15666708bb27SAndy Grover 		pr_err("Underlying SCSI device firmware provided VPD"
1567c66ac9dbSNicholas Bellinger 			" Unit Serial, ignoring request\n");
1568c66ac9dbSNicholas Bellinger 		return -EOPNOTSUPP;
1569c66ac9dbSNicholas Bellinger 	}
1570c66ac9dbSNicholas Bellinger 
157160d645a4SDan Carpenter 	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
15726708bb27SAndy Grover 		pr_err("Emulated VPD Unit Serial exceeds"
1573c66ac9dbSNicholas Bellinger 		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1574c66ac9dbSNicholas Bellinger 		return -EOVERFLOW;
1575c66ac9dbSNicholas Bellinger 	}
1576c66ac9dbSNicholas Bellinger 	/*
1577c66ac9dbSNicholas Bellinger 	 * Check to see if any active $FABRIC_MOD exports exist.  If they
1578c66ac9dbSNicholas Bellinger 	 * do exist, fail here as changing this information on the fly
1579c66ac9dbSNicholas Bellinger 	 * (underneath the initiator side OS dependent multipath code)
1580c66ac9dbSNicholas Bellinger 	 * could cause negative effects.
1581c66ac9dbSNicholas Bellinger 	 */
15820fd97ccfSChristoph Hellwig 	if (dev->export_count) {
15836708bb27SAndy Grover 		pr_err("Unable to set VPD Unit Serial while"
1584c66ac9dbSNicholas Bellinger 			" active %d $FABRIC_MOD exports exist\n",
15850fd97ccfSChristoph Hellwig 			dev->export_count);
1586c66ac9dbSNicholas Bellinger 		return -EINVAL;
1587c66ac9dbSNicholas Bellinger 	}
15880fd97ccfSChristoph Hellwig 
1589c66ac9dbSNicholas Bellinger 	/*
1590c66ac9dbSNicholas Bellinger 	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1591c66ac9dbSNicholas Bellinger 	 *
1592c66ac9dbSNicholas Bellinger 	 * Also, strip any newline added from the userspace
1593c66ac9dbSNicholas Bellinger 	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1594c66ac9dbSNicholas Bellinger 	 */
1595c66ac9dbSNicholas Bellinger 	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
15960fd97ccfSChristoph Hellwig 	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1597c66ac9dbSNicholas Bellinger 			"%s", strstrip(buf));
15980fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1599c66ac9dbSNicholas Bellinger 
16006708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
16010fd97ccfSChristoph Hellwig 			" %s\n", dev->t10_wwn.unit_serial);
1602c66ac9dbSNicholas Bellinger 
1603c66ac9dbSNicholas Bellinger 	return count;
1604c66ac9dbSNicholas Bellinger }
1605c66ac9dbSNicholas Bellinger 
1606c66ac9dbSNicholas Bellinger /*
1607c66ac9dbSNicholas Bellinger  * VPD page 0x83 Protocol Identifier
1608c66ac9dbSNicholas Bellinger  */
16092eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1610c66ac9dbSNicholas Bellinger 		char *page)
1611c66ac9dbSNicholas Bellinger {
16122eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1613c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;
16142d4e2dafSChaitanya Kulkarni 	unsigned char buf[VPD_TMP_BUF_SIZE] = { };
1615c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1616c66ac9dbSNicholas Bellinger 
1617c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);
1618c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
16196708bb27SAndy Grover 		if (!vpd->protocol_identifier_set)
1620c66ac9dbSNicholas Bellinger 			continue;
1621c66ac9dbSNicholas Bellinger 
1622c66ac9dbSNicholas Bellinger 		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1623c66ac9dbSNicholas Bellinger 
16246708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1625c66ac9dbSNicholas Bellinger 			break;
1626c66ac9dbSNicholas Bellinger 
1627c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1628c66ac9dbSNicholas Bellinger 	}
1629c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);
1630c66ac9dbSNicholas Bellinger 
1631c66ac9dbSNicholas Bellinger 	return len;
1632c66ac9dbSNicholas Bellinger }
1633c66ac9dbSNicholas Bellinger 
1634c66ac9dbSNicholas Bellinger /*
1635c66ac9dbSNicholas Bellinger  * Generic wrapper for dumping VPD identifiers by association.
1636c66ac9dbSNicholas Bellinger  */
1637c66ac9dbSNicholas Bellinger #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
16382eafd729SChristoph Hellwig static ssize_t target_wwn_##_name##_show(struct config_item *item,	\
1639c66ac9dbSNicholas Bellinger 		char *page)						\
1640c66ac9dbSNicholas Bellinger {									\
16412eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);			\
1642c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;						\
1643c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];				\
1644c66ac9dbSNicholas Bellinger 	ssize_t len = 0;						\
1645c66ac9dbSNicholas Bellinger 									\
1646c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);				\
1647c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
1648c66ac9dbSNicholas Bellinger 		if (vpd->association != _assoc)				\
1649c66ac9dbSNicholas Bellinger 			continue;					\
1650c66ac9dbSNicholas Bellinger 									\
1651c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1652c66ac9dbSNicholas Bellinger 		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
16536708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1654c66ac9dbSNicholas Bellinger 			break;						\
1655c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1656c66ac9dbSNicholas Bellinger 									\
1657c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1658c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
16596708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1660c66ac9dbSNicholas Bellinger 			break;						\
1661c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1662c66ac9dbSNicholas Bellinger 									\
1663c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1664c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
16656708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1666c66ac9dbSNicholas Bellinger 			break;						\
1667c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1668c66ac9dbSNicholas Bellinger 	}								\
1669c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);				\
1670c66ac9dbSNicholas Bellinger 									\
1671c66ac9dbSNicholas Bellinger 	return len;							\
1672c66ac9dbSNicholas Bellinger }
1673c66ac9dbSNicholas Bellinger 
16742eafd729SChristoph Hellwig /* VPD page 0x83 Association: Logical Unit */
1675c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
16762eafd729SChristoph Hellwig /* VPD page 0x83 Association: Target Port */
1677c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
16782eafd729SChristoph Hellwig /* VPD page 0x83 Association: SCSI Target Device */
1679c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1680c66ac9dbSNicholas Bellinger 
168154a6f3f6SDavid Disseldorp CONFIGFS_ATTR(target_wwn_, vendor_id);
16820322913cSAlan Adamson CONFIGFS_ATTR(target_wwn_, product_id);
16830322913cSAlan Adamson CONFIGFS_ATTR(target_wwn_, revision);
16842469f1e0SSergey Samoylenko CONFIGFS_ATTR(target_wwn_, company_id);
16852eafd729SChristoph Hellwig CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
16862eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
16872eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
16882eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
16892eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1690c66ac9dbSNicholas Bellinger 
1691c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
169254a6f3f6SDavid Disseldorp 	&target_wwn_attr_vendor_id,
16930322913cSAlan Adamson 	&target_wwn_attr_product_id,
16940322913cSAlan Adamson 	&target_wwn_attr_revision,
16952469f1e0SSergey Samoylenko 	&target_wwn_attr_company_id,
16962eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_unit_serial,
16972eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_protocol_identifier,
16982eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_logical_unit,
16992eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_target_port,
17002eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_scsi_target_device,
1701c66ac9dbSNicholas Bellinger 	NULL,
1702c66ac9dbSNicholas Bellinger };
1703c66ac9dbSNicholas Bellinger 
17042eafd729SChristoph Hellwig TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1705c66ac9dbSNicholas Bellinger 
1706f8d389c6SNicholas Bellinger /*  End functions for struct config_item_type tb_dev_wwn_cit */
1707c66ac9dbSNicholas Bellinger 
170891e2e39bSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_pr_cit */
1709c66ac9dbSNicholas Bellinger 
17102eafd729SChristoph Hellwig static struct se_device *pr_to_dev(struct config_item *item)
17112eafd729SChristoph Hellwig {
17122eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device,
17132eafd729SChristoph Hellwig 			dev_pr_group);
17142eafd729SChristoph Hellwig }
1715c66ac9dbSNicholas Bellinger 
1716d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1717d977f437SChristoph Hellwig 		char *page)
1718c66ac9dbSNicholas Bellinger {
1719c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1720c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
17212d4e2dafSChaitanya Kulkarni 	char i_buf[PR_REG_ISID_ID_LEN] = { };
1722c66ac9dbSNicholas Bellinger 
1723c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1724d977f437SChristoph Hellwig 	if (!pr_reg)
1725d977f437SChristoph Hellwig 		return sprintf(page, "No SPC-3 Reservation holder\n");
1726d977f437SChristoph Hellwig 
1727c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1728d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1729c66ac9dbSNicholas Bellinger 
1730d977f437SChristoph Hellwig 	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
173130c7ca93SDavid Disseldorp 		se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1732d2843c17SAndy Grover 		se_nacl->initiatorname, i_buf);
1733c66ac9dbSNicholas Bellinger }
1734c66ac9dbSNicholas Bellinger 
1735d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1736d977f437SChristoph Hellwig 		char *page)
1737c66ac9dbSNicholas Bellinger {
1738fae43461SBart Van Assche 	struct se_session *sess = dev->reservation_holder;
1739c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1740d977f437SChristoph Hellwig 	ssize_t len;
1741c66ac9dbSNicholas Bellinger 
1742fae43461SBart Van Assche 	if (sess) {
1743fae43461SBart Van Assche 		se_nacl = sess->se_node_acl;
1744d977f437SChristoph Hellwig 		len = sprintf(page,
1745d977f437SChristoph Hellwig 			      "SPC-2 Reservation: %s Initiator: %s\n",
174630c7ca93SDavid Disseldorp 			      se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1747c66ac9dbSNicholas Bellinger 			      se_nacl->initiatorname);
1748d977f437SChristoph Hellwig 	} else {
1749d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-2 Reservation holder\n");
1750d977f437SChristoph Hellwig 	}
1751d977f437SChristoph Hellwig 	return len;
1752c66ac9dbSNicholas Bellinger }
1753c66ac9dbSNicholas Bellinger 
17542eafd729SChristoph Hellwig static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1755c66ac9dbSNicholas Bellinger {
17562eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1757d977f437SChristoph Hellwig 	int ret;
1758c66ac9dbSNicholas Bellinger 
1759b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr)
1760b49d6f78SDavid Disseldorp 		return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1761b49d6f78SDavid Disseldorp 
176269088a04SBodo Stroesser 	if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1763d977f437SChristoph Hellwig 		return sprintf(page, "Passthrough\n");
1764c66ac9dbSNicholas Bellinger 
1765d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
1766d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1767d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc2_res(dev, page);
1768d977f437SChristoph Hellwig 	else
1769d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc3_res(dev, page);
1770d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1771d977f437SChristoph Hellwig 	return ret;
1772c66ac9dbSNicholas Bellinger }
1773c66ac9dbSNicholas Bellinger 
17742eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
17752eafd729SChristoph Hellwig 		char *page)
1776c66ac9dbSNicholas Bellinger {
17772eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1778c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1779c66ac9dbSNicholas Bellinger 
1780c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1781d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder) {
1782c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1783d977f437SChristoph Hellwig 	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1784c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: All Target"
1785c66ac9dbSNicholas Bellinger 			" Ports registration\n");
1786d977f437SChristoph Hellwig 	} else {
1787c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: Single"
1788c66ac9dbSNicholas Bellinger 			" Target Port registration\n");
1789d977f437SChristoph Hellwig 	}
1790c66ac9dbSNicholas Bellinger 
1791d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1792c66ac9dbSNicholas Bellinger 	return len;
1793c66ac9dbSNicholas Bellinger }
1794c66ac9dbSNicholas Bellinger 
17952eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
17962eafd729SChristoph Hellwig 		char *page)
1797c66ac9dbSNicholas Bellinger {
17982eafd729SChristoph Hellwig 	return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1799c66ac9dbSNicholas Bellinger }
1800c66ac9dbSNicholas Bellinger 
1801c66ac9dbSNicholas Bellinger 
18022eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
18032eafd729SChristoph Hellwig 		char *page)
1804c66ac9dbSNicholas Bellinger {
18052eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1806c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1807c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
1808c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
18099ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1810c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1811c66ac9dbSNicholas Bellinger 
1812c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1813c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
18146708bb27SAndy Grover 	if (!pr_reg) {
1815c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1816d977f437SChristoph Hellwig 		goto out_unlock;
1817c66ac9dbSNicholas Bellinger 	}
1818d977f437SChristoph Hellwig 
1819c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1820c66ac9dbSNicholas Bellinger 	se_tpg = se_nacl->se_tpg;
1821e3d6f909SAndy Grover 	tfo = se_tpg->se_tpg_tfo;
1822c66ac9dbSNicholas Bellinger 
1823c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: %s"
182430c7ca93SDavid Disseldorp 		" Target Node Endpoint: %s\n", tfo->fabric_name,
1825c66ac9dbSNicholas Bellinger 		tfo->tpg_get_wwn(se_tpg));
1826c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
182735d1efe8SMasanari Iida 		" Identifier Tag: %hu %s Portal Group Tag: %hu"
1828f2d30680SHannes Reinecke 		" %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
182930c7ca93SDavid Disseldorp 		tfo->fabric_name, tfo->tpg_get_tag(se_tpg),
183030c7ca93SDavid Disseldorp 		tfo->fabric_name, pr_reg->pr_aptpl_target_lun);
1831c66ac9dbSNicholas Bellinger 
1832d977f437SChristoph Hellwig out_unlock:
1833d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1834c66ac9dbSNicholas Bellinger 	return len;
1835c66ac9dbSNicholas Bellinger }
1836c66ac9dbSNicholas Bellinger 
1837c66ac9dbSNicholas Bellinger 
18382eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
18392eafd729SChristoph Hellwig 		char *page)
1840c66ac9dbSNicholas Bellinger {
18412eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
18429ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1843c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1844c66ac9dbSNicholas Bellinger 	unsigned char buf[384];
1845c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1846c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1847d2843c17SAndy Grover 	int reg_count = 0;
1848c66ac9dbSNicholas Bellinger 
1849c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1850c66ac9dbSNicholas Bellinger 
18510fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
18520fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1853c66ac9dbSNicholas Bellinger 			pr_reg_list) {
1854c66ac9dbSNicholas Bellinger 
1855c66ac9dbSNicholas Bellinger 		memset(buf, 0, 384);
1856c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1857c66ac9dbSNicholas Bellinger 		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1858d2843c17SAndy Grover 		core_pr_dump_initiator_port(pr_reg, i_buf,
1859c66ac9dbSNicholas Bellinger 					PR_REG_ISID_ID_LEN);
1860c66ac9dbSNicholas Bellinger 		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
186130c7ca93SDavid Disseldorp 			tfo->fabric_name,
1862d2843c17SAndy Grover 			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1863c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_generation);
1864c66ac9dbSNicholas Bellinger 
18656708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1866c66ac9dbSNicholas Bellinger 			break;
1867c66ac9dbSNicholas Bellinger 
1868c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1869c66ac9dbSNicholas Bellinger 		reg_count++;
1870c66ac9dbSNicholas Bellinger 	}
18710fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
1872c66ac9dbSNicholas Bellinger 
18736708bb27SAndy Grover 	if (!reg_count)
1874c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "None\n");
1875c66ac9dbSNicholas Bellinger 
1876c66ac9dbSNicholas Bellinger 	return len;
1877c66ac9dbSNicholas Bellinger }
1878c66ac9dbSNicholas Bellinger 
18792eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1880c66ac9dbSNicholas Bellinger {
18812eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1882c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1883c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1884c66ac9dbSNicholas Bellinger 
1885c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1886c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1887d977f437SChristoph Hellwig 	if (pr_reg) {
1888c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1889c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1890d977f437SChristoph Hellwig 	} else {
1891d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1892d977f437SChristoph Hellwig 	}
1893c66ac9dbSNicholas Bellinger 
1894d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1895c66ac9dbSNicholas Bellinger 	return len;
1896c66ac9dbSNicholas Bellinger }
1897c66ac9dbSNicholas Bellinger 
18982eafd729SChristoph Hellwig static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
1899c66ac9dbSNicholas Bellinger {
19002eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
19012eafd729SChristoph Hellwig 
1902b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr)
1903b49d6f78SDavid Disseldorp 		return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
190469088a04SBodo Stroesser 	if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1905d977f437SChristoph Hellwig 		return sprintf(page, "SPC_PASSTHROUGH\n");
1906b49d6f78SDavid Disseldorp 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1907d977f437SChristoph Hellwig 		return sprintf(page, "SPC2_RESERVATIONS\n");
1908b49d6f78SDavid Disseldorp 
1909d977f437SChristoph Hellwig 	return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1910c66ac9dbSNicholas Bellinger }
1911c66ac9dbSNicholas Bellinger 
19122eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
19132eafd729SChristoph Hellwig 		char *page)
1914c66ac9dbSNicholas Bellinger {
19152eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
19162eafd729SChristoph Hellwig 
1917b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
191869088a04SBodo Stroesser 	    (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1919c66ac9dbSNicholas Bellinger 		return 0;
1920c66ac9dbSNicholas Bellinger 
1921c66ac9dbSNicholas Bellinger 	return sprintf(page, "APTPL Bit Status: %s\n",
19220fd97ccfSChristoph Hellwig 		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1923c66ac9dbSNicholas Bellinger }
1924c66ac9dbSNicholas Bellinger 
19252eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
19262eafd729SChristoph Hellwig 		char *page)
1927c66ac9dbSNicholas Bellinger {
19282eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
19292eafd729SChristoph Hellwig 
1930b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
193169088a04SBodo Stroesser 	    (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1932c66ac9dbSNicholas Bellinger 		return 0;
1933c66ac9dbSNicholas Bellinger 
1934c66ac9dbSNicholas Bellinger 	return sprintf(page, "Ready to process PR APTPL metadata..\n");
1935c66ac9dbSNicholas Bellinger }
1936c66ac9dbSNicholas Bellinger 
1937c66ac9dbSNicholas Bellinger enum {
1938c66ac9dbSNicholas Bellinger 	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1939c66ac9dbSNicholas Bellinger 	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1940c66ac9dbSNicholas Bellinger 	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1941c66ac9dbSNicholas Bellinger 	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1942c66ac9dbSNicholas Bellinger };
1943c66ac9dbSNicholas Bellinger 
1944c66ac9dbSNicholas Bellinger static match_table_t tokens = {
1945c66ac9dbSNicholas Bellinger 	{Opt_initiator_fabric, "initiator_fabric=%s"},
1946c66ac9dbSNicholas Bellinger 	{Opt_initiator_node, "initiator_node=%s"},
1947c66ac9dbSNicholas Bellinger 	{Opt_initiator_sid, "initiator_sid=%s"},
1948c66ac9dbSNicholas Bellinger 	{Opt_sa_res_key, "sa_res_key=%s"},
1949c66ac9dbSNicholas Bellinger 	{Opt_res_holder, "res_holder=%d"},
1950c66ac9dbSNicholas Bellinger 	{Opt_res_type, "res_type=%d"},
1951c66ac9dbSNicholas Bellinger 	{Opt_res_scope, "res_scope=%d"},
1952c66ac9dbSNicholas Bellinger 	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1953a2db857bStangwenji 	{Opt_mapped_lun, "mapped_lun=%u"},
1954c66ac9dbSNicholas Bellinger 	{Opt_target_fabric, "target_fabric=%s"},
1955c66ac9dbSNicholas Bellinger 	{Opt_target_node, "target_node=%s"},
1956c66ac9dbSNicholas Bellinger 	{Opt_tpgt, "tpgt=%d"},
1957c66ac9dbSNicholas Bellinger 	{Opt_port_rtpi, "port_rtpi=%d"},
1958a2db857bStangwenji 	{Opt_target_lun, "target_lun=%u"},
1959c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
1960c66ac9dbSNicholas Bellinger };
1961c66ac9dbSNicholas Bellinger 
19622eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
19632eafd729SChristoph Hellwig 		const char *page, size_t count)
1964c66ac9dbSNicholas Bellinger {
19652eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
19666d180253SJesper Juhl 	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
19676d180253SJesper Juhl 	unsigned char *t_fabric = NULL, *t_port = NULL;
19688d213559SJoern Engel 	char *orig, *ptr, *opts;
1969c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
1970c66ac9dbSNicholas Bellinger 	unsigned long long tmp_ll;
1971c66ac9dbSNicholas Bellinger 	u64 sa_res_key = 0;
1972f2d30680SHannes Reinecke 	u64 mapped_lun = 0, target_lun = 0;
1973c66ac9dbSNicholas Bellinger 	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
197445fb94c2SBart Van Assche 	u16 tpgt = 0;
197545fb94c2SBart Van Assche 	u8 type = 0;
1976c66ac9dbSNicholas Bellinger 
1977b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
197869088a04SBodo Stroesser 	    (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
19799105bfc0SAndy Grover 		return count;
1980d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
19819105bfc0SAndy Grover 		return count;
1982c66ac9dbSNicholas Bellinger 
19830fd97ccfSChristoph Hellwig 	if (dev->export_count) {
19846708bb27SAndy Grover 		pr_debug("Unable to process APTPL metadata while"
1985c66ac9dbSNicholas Bellinger 			" active fabric exports exist\n");
1986c66ac9dbSNicholas Bellinger 		return -EINVAL;
1987c66ac9dbSNicholas Bellinger 	}
1988c66ac9dbSNicholas Bellinger 
1989c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
1990c66ac9dbSNicholas Bellinger 	if (!opts)
1991c66ac9dbSNicholas Bellinger 		return -ENOMEM;
1992c66ac9dbSNicholas Bellinger 
1993c66ac9dbSNicholas Bellinger 	orig = opts;
199490c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
1995c66ac9dbSNicholas Bellinger 		if (!*ptr)
1996c66ac9dbSNicholas Bellinger 			continue;
1997c66ac9dbSNicholas Bellinger 
1998c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
1999c66ac9dbSNicholas Bellinger 		switch (token) {
2000c66ac9dbSNicholas Bellinger 		case Opt_initiator_fabric:
20018d213559SJoern Engel 			i_fabric = match_strdup(args);
20026d180253SJesper Juhl 			if (!i_fabric) {
20036d180253SJesper Juhl 				ret = -ENOMEM;
20046d180253SJesper Juhl 				goto out;
20056d180253SJesper Juhl 			}
2006c66ac9dbSNicholas Bellinger 			break;
2007c66ac9dbSNicholas Bellinger 		case Opt_initiator_node:
20088d213559SJoern Engel 			i_port = match_strdup(args);
20096d180253SJesper Juhl 			if (!i_port) {
20106d180253SJesper Juhl 				ret = -ENOMEM;
20116d180253SJesper Juhl 				goto out;
20126d180253SJesper Juhl 			}
201360d645a4SDan Carpenter 			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
20146708bb27SAndy Grover 				pr_err("APTPL metadata initiator_node="
2015c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
2016c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_IPORT_LEN);
2017c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
2018c66ac9dbSNicholas Bellinger 				break;
2019c66ac9dbSNicholas Bellinger 			}
2020c66ac9dbSNicholas Bellinger 			break;
2021c66ac9dbSNicholas Bellinger 		case Opt_initiator_sid:
20228d213559SJoern Engel 			isid = match_strdup(args);
20236d180253SJesper Juhl 			if (!isid) {
20246d180253SJesper Juhl 				ret = -ENOMEM;
20256d180253SJesper Juhl 				goto out;
20266d180253SJesper Juhl 			}
202760d645a4SDan Carpenter 			if (strlen(isid) >= PR_REG_ISID_LEN) {
20286708bb27SAndy Grover 				pr_err("APTPL metadata initiator_isid"
2029c66ac9dbSNicholas Bellinger 					"= exceeds PR_REG_ISID_LEN: %d\n",
2030c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
2031c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
2032c66ac9dbSNicholas Bellinger 				break;
2033c66ac9dbSNicholas Bellinger 			}
2034c66ac9dbSNicholas Bellinger 			break;
2035c66ac9dbSNicholas Bellinger 		case Opt_sa_res_key:
2036a2db857bStangwenji 			ret = match_u64(args,  &tmp_ll);
2037c66ac9dbSNicholas Bellinger 			if (ret < 0) {
20388d213559SJoern Engel 				pr_err("kstrtoull() failed for sa_res_key=\n");
2039c66ac9dbSNicholas Bellinger 				goto out;
2040c66ac9dbSNicholas Bellinger 			}
2041c66ac9dbSNicholas Bellinger 			sa_res_key = (u64)tmp_ll;
2042c66ac9dbSNicholas Bellinger 			break;
2043c66ac9dbSNicholas Bellinger 		/*
2044c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Reservation
2045c66ac9dbSNicholas Bellinger 		 */
2046c66ac9dbSNicholas Bellinger 		case Opt_res_holder:
2047c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
2048c2091026SDavid Disseldorp 			if (ret)
2049c2091026SDavid Disseldorp 				goto out;
2050c66ac9dbSNicholas Bellinger 			res_holder = arg;
2051c66ac9dbSNicholas Bellinger 			break;
2052c66ac9dbSNicholas Bellinger 		case Opt_res_type:
2053c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
2054c2091026SDavid Disseldorp 			if (ret)
2055c2091026SDavid Disseldorp 				goto out;
2056c66ac9dbSNicholas Bellinger 			type = (u8)arg;
2057c66ac9dbSNicholas Bellinger 			break;
2058c66ac9dbSNicholas Bellinger 		case Opt_res_scope:
2059c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
2060c2091026SDavid Disseldorp 			if (ret)
2061c2091026SDavid Disseldorp 				goto out;
2062c66ac9dbSNicholas Bellinger 			break;
2063c66ac9dbSNicholas Bellinger 		case Opt_res_all_tg_pt:
2064c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
2065c2091026SDavid Disseldorp 			if (ret)
2066c2091026SDavid Disseldorp 				goto out;
2067c66ac9dbSNicholas Bellinger 			all_tg_pt = (int)arg;
2068c66ac9dbSNicholas Bellinger 			break;
2069c66ac9dbSNicholas Bellinger 		case Opt_mapped_lun:
2070a2db857bStangwenji 			ret = match_u64(args, &tmp_ll);
2071c2091026SDavid Disseldorp 			if (ret)
2072c2091026SDavid Disseldorp 				goto out;
2073a2db857bStangwenji 			mapped_lun = (u64)tmp_ll;
2074c66ac9dbSNicholas Bellinger 			break;
2075c66ac9dbSNicholas Bellinger 		/*
2076c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Target Port
2077c66ac9dbSNicholas Bellinger 		 */
2078c66ac9dbSNicholas Bellinger 		case Opt_target_fabric:
20798d213559SJoern Engel 			t_fabric = match_strdup(args);
20806d180253SJesper Juhl 			if (!t_fabric) {
20816d180253SJesper Juhl 				ret = -ENOMEM;
20826d180253SJesper Juhl 				goto out;
20836d180253SJesper Juhl 			}
2084c66ac9dbSNicholas Bellinger 			break;
2085c66ac9dbSNicholas Bellinger 		case Opt_target_node:
20868d213559SJoern Engel 			t_port = match_strdup(args);
20876d180253SJesper Juhl 			if (!t_port) {
20886d180253SJesper Juhl 				ret = -ENOMEM;
20896d180253SJesper Juhl 				goto out;
20906d180253SJesper Juhl 			}
209160d645a4SDan Carpenter 			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
20926708bb27SAndy Grover 				pr_err("APTPL metadata target_node="
2093c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
2094c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_TPORT_LEN);
2095c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
2096c66ac9dbSNicholas Bellinger 				break;
2097c66ac9dbSNicholas Bellinger 			}
2098c66ac9dbSNicholas Bellinger 			break;
2099c66ac9dbSNicholas Bellinger 		case Opt_tpgt:
2100c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
2101c2091026SDavid Disseldorp 			if (ret)
2102c2091026SDavid Disseldorp 				goto out;
2103c66ac9dbSNicholas Bellinger 			tpgt = (u16)arg;
2104c66ac9dbSNicholas Bellinger 			break;
2105c66ac9dbSNicholas Bellinger 		case Opt_port_rtpi:
2106c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
2107c2091026SDavid Disseldorp 			if (ret)
2108c2091026SDavid Disseldorp 				goto out;
2109c66ac9dbSNicholas Bellinger 			break;
2110c66ac9dbSNicholas Bellinger 		case Opt_target_lun:
2111a2db857bStangwenji 			ret = match_u64(args, &tmp_ll);
2112c2091026SDavid Disseldorp 			if (ret)
2113c2091026SDavid Disseldorp 				goto out;
2114a2db857bStangwenji 			target_lun = (u64)tmp_ll;
2115c66ac9dbSNicholas Bellinger 			break;
2116c66ac9dbSNicholas Bellinger 		default:
2117c66ac9dbSNicholas Bellinger 			break;
2118c66ac9dbSNicholas Bellinger 		}
2119c66ac9dbSNicholas Bellinger 	}
2120c66ac9dbSNicholas Bellinger 
21216708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
21226708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
2123c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
2124c66ac9dbSNicholas Bellinger 		goto out;
2125c66ac9dbSNicholas Bellinger 	}
2126c66ac9dbSNicholas Bellinger 
2127c66ac9dbSNicholas Bellinger 	if (res_holder && !(type)) {
21286708bb27SAndy Grover 		pr_err("Illegal PR type: 0x%02x for reservation"
2129c66ac9dbSNicholas Bellinger 				" holder\n", type);
2130c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
2131c66ac9dbSNicholas Bellinger 		goto out;
2132c66ac9dbSNicholas Bellinger 	}
2133c66ac9dbSNicholas Bellinger 
21340fd97ccfSChristoph Hellwig 	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
2135c66ac9dbSNicholas Bellinger 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
2136c66ac9dbSNicholas Bellinger 			res_holder, all_tg_pt, type);
2137c66ac9dbSNicholas Bellinger out:
21386d180253SJesper Juhl 	kfree(i_fabric);
21396d180253SJesper Juhl 	kfree(i_port);
21406d180253SJesper Juhl 	kfree(isid);
21416d180253SJesper Juhl 	kfree(t_fabric);
21426d180253SJesper Juhl 	kfree(t_port);
2143c66ac9dbSNicholas Bellinger 	kfree(orig);
2144c66ac9dbSNicholas Bellinger 	return (ret == 0) ? count : ret;
2145c66ac9dbSNicholas Bellinger }
2146c66ac9dbSNicholas Bellinger 
2147c66ac9dbSNicholas Bellinger 
21482eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_holder);
21492eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
21502eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
21512eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
21522eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
21532eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
21542eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_type);
21552eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
21562eafd729SChristoph Hellwig CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
2157c66ac9dbSNicholas Bellinger 
2158c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_pr_attrs[] = {
21592eafd729SChristoph Hellwig 	&target_pr_attr_res_holder,
21602eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_all_tgt_pts,
21612eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_generation,
21622eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_holder_tg_port,
21632eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_registered_i_pts,
21642eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_type,
21652eafd729SChristoph Hellwig 	&target_pr_attr_res_type,
21662eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_active,
21672eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_metadata,
2168c66ac9dbSNicholas Bellinger 	NULL,
2169c66ac9dbSNicholas Bellinger };
2170c66ac9dbSNicholas Bellinger 
21712eafd729SChristoph Hellwig TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
2172c66ac9dbSNicholas Bellinger 
217391e2e39bSNicholas Bellinger /*  End functions for struct config_item_type tb_dev_pr_cit */
2174c66ac9dbSNicholas Bellinger 
217573112edcSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_cit */
2176c66ac9dbSNicholas Bellinger 
21772eafd729SChristoph Hellwig static inline struct se_device *to_device(struct config_item *item)
2178c66ac9dbSNicholas Bellinger {
21792eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device, dev_group);
21802eafd729SChristoph Hellwig }
21812eafd729SChristoph Hellwig 
21822eafd729SChristoph Hellwig static ssize_t target_dev_info_show(struct config_item *item, char *page)
21832eafd729SChristoph Hellwig {
21842eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2185c66ac9dbSNicholas Bellinger 	int bl = 0;
2186c66ac9dbSNicholas Bellinger 	ssize_t read_bytes = 0;
2187c66ac9dbSNicholas Bellinger 
21880fd97ccfSChristoph Hellwig 	transport_dump_dev_state(dev, page, &bl);
2189c66ac9dbSNicholas Bellinger 	read_bytes += bl;
21900a06d430SChristoph Hellwig 	read_bytes += dev->transport->show_configfs_dev_params(dev,
21910a06d430SChristoph Hellwig 			page+read_bytes);
2192c66ac9dbSNicholas Bellinger 	return read_bytes;
2193c66ac9dbSNicholas Bellinger }
2194c66ac9dbSNicholas Bellinger 
21952eafd729SChristoph Hellwig static ssize_t target_dev_control_store(struct config_item *item,
21962eafd729SChristoph Hellwig 		const char *page, size_t count)
2197c66ac9dbSNicholas Bellinger {
21982eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2199c66ac9dbSNicholas Bellinger 
22000a06d430SChristoph Hellwig 	return dev->transport->set_configfs_dev_params(dev, page, count);
2201c66ac9dbSNicholas Bellinger }
2202c66ac9dbSNicholas Bellinger 
22032eafd729SChristoph Hellwig static ssize_t target_dev_alias_show(struct config_item *item, char *page)
2204c66ac9dbSNicholas Bellinger {
22052eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2206c66ac9dbSNicholas Bellinger 
22070fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_ALIAS))
2208c66ac9dbSNicholas Bellinger 		return 0;
2209c66ac9dbSNicholas Bellinger 
22100fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
2211c66ac9dbSNicholas Bellinger }
2212c66ac9dbSNicholas Bellinger 
22132eafd729SChristoph Hellwig static ssize_t target_dev_alias_store(struct config_item *item,
22142eafd729SChristoph Hellwig 		const char *page, size_t count)
2215c66ac9dbSNicholas Bellinger {
22162eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
22170fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
2218c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
2219c66ac9dbSNicholas Bellinger 
2220c66ac9dbSNicholas Bellinger 	if (count > (SE_DEV_ALIAS_LEN-1)) {
22216708bb27SAndy Grover 		pr_err("alias count: %d exceeds"
2222c66ac9dbSNicholas Bellinger 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
2223c66ac9dbSNicholas Bellinger 			SE_DEV_ALIAS_LEN-1);
2224c66ac9dbSNicholas Bellinger 		return -EINVAL;
2225c66ac9dbSNicholas Bellinger 	}
2226c66ac9dbSNicholas Bellinger 
22270fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
22283011684cSDan Carpenter 	if (!read_bytes)
22293011684cSDan Carpenter 		return -EINVAL;
22300fd97ccfSChristoph Hellwig 	if (dev->dev_alias[read_bytes - 1] == '\n')
22310fd97ccfSChristoph Hellwig 		dev->dev_alias[read_bytes - 1] = '\0';
22320877eafdSSebastian Andrzej Siewior 
22330fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_ALIAS;
22343011684cSDan Carpenter 
22356708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
2236c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
22370fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
22380fd97ccfSChristoph Hellwig 		dev->dev_alias);
2239c66ac9dbSNicholas Bellinger 
2240c66ac9dbSNicholas Bellinger 	return read_bytes;
2241c66ac9dbSNicholas Bellinger }
2242c66ac9dbSNicholas Bellinger 
22432eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
2244c66ac9dbSNicholas Bellinger {
22452eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2246c66ac9dbSNicholas Bellinger 
22470fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
2248c66ac9dbSNicholas Bellinger 		return 0;
2249c66ac9dbSNicholas Bellinger 
22500fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
2251c66ac9dbSNicholas Bellinger }
2252c66ac9dbSNicholas Bellinger 
22532eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_store(struct config_item *item,
22542eafd729SChristoph Hellwig 		const char *page, size_t count)
2255c66ac9dbSNicholas Bellinger {
22562eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
22570fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
2258c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
2259c66ac9dbSNicholas Bellinger 
2260c66ac9dbSNicholas Bellinger 	if (count > (SE_UDEV_PATH_LEN-1)) {
22616708bb27SAndy Grover 		pr_err("udev_path count: %d exceeds"
2262c66ac9dbSNicholas Bellinger 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
2263c66ac9dbSNicholas Bellinger 			SE_UDEV_PATH_LEN-1);
2264c66ac9dbSNicholas Bellinger 		return -EINVAL;
2265c66ac9dbSNicholas Bellinger 	}
2266c66ac9dbSNicholas Bellinger 
22670fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
2268c66ac9dbSNicholas Bellinger 			"%s", page);
22693011684cSDan Carpenter 	if (!read_bytes)
22703011684cSDan Carpenter 		return -EINVAL;
22710fd97ccfSChristoph Hellwig 	if (dev->udev_path[read_bytes - 1] == '\n')
22720fd97ccfSChristoph Hellwig 		dev->udev_path[read_bytes - 1] = '\0';
22730877eafdSSebastian Andrzej Siewior 
22740fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_UDEV_PATH;
22753011684cSDan Carpenter 
22766708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
2277c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
22780fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
22790fd97ccfSChristoph Hellwig 		dev->udev_path);
2280c66ac9dbSNicholas Bellinger 
2281c66ac9dbSNicholas Bellinger 	return read_bytes;
2282c66ac9dbSNicholas Bellinger }
2283c66ac9dbSNicholas Bellinger 
22842eafd729SChristoph Hellwig static ssize_t target_dev_enable_show(struct config_item *item, char *page)
228564146db7SAndy Grover {
22862eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
228764146db7SAndy Grover 
2288cb0f32e1SMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n", target_dev_configured(dev));
228964146db7SAndy Grover }
229064146db7SAndy Grover 
22912eafd729SChristoph Hellwig static ssize_t target_dev_enable_store(struct config_item *item,
22922eafd729SChristoph Hellwig 		const char *page, size_t count)
2293c66ac9dbSNicholas Bellinger {
22942eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2295c66ac9dbSNicholas Bellinger 	char *ptr;
22960fd97ccfSChristoph Hellwig 	int ret;
2297c66ac9dbSNicholas Bellinger 
2298c66ac9dbSNicholas Bellinger 	ptr = strstr(page, "1");
22996708bb27SAndy Grover 	if (!ptr) {
23006708bb27SAndy Grover 		pr_err("For dev_enable ops, only valid value"
2301c66ac9dbSNicholas Bellinger 				" is \"1\"\n");
2302c66ac9dbSNicholas Bellinger 		return -EINVAL;
2303c66ac9dbSNicholas Bellinger 	}
2304c66ac9dbSNicholas Bellinger 
23050fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
23060fd97ccfSChristoph Hellwig 	if (ret)
23070fd97ccfSChristoph Hellwig 		return ret;
2308c66ac9dbSNicholas Bellinger 	return count;
2309c66ac9dbSNicholas Bellinger }
2310c66ac9dbSNicholas Bellinger 
23112eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
2312c66ac9dbSNicholas Bellinger {
23132eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2314c66ac9dbSNicholas Bellinger 	struct config_item *lu_ci;
2315c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2316c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2317c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
2318c66ac9dbSNicholas Bellinger 
2319c66ac9dbSNicholas Bellinger 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
2320c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
2321c87fbd56SChristoph Hellwig 		return 0;
2322c66ac9dbSNicholas Bellinger 
2323c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2324c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
23256708bb27SAndy Grover 	if (lu_gp) {
2326c66ac9dbSNicholas Bellinger 		lu_ci = &lu_gp->lu_gp_group.cg_item;
2327c66ac9dbSNicholas Bellinger 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
2328c66ac9dbSNicholas Bellinger 			config_item_name(lu_ci), lu_gp->lu_gp_id);
2329c66ac9dbSNicholas Bellinger 	}
2330c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2331c66ac9dbSNicholas Bellinger 
2332c66ac9dbSNicholas Bellinger 	return len;
2333c66ac9dbSNicholas Bellinger }
2334c66ac9dbSNicholas Bellinger 
23352eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
23362eafd729SChristoph Hellwig 		const char *page, size_t count)
2337c66ac9dbSNicholas Bellinger {
23382eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
23390fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
2340c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
2341c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
23422d4e2dafSChaitanya Kulkarni 	unsigned char buf[LU_GROUP_NAME_BUF] = { };
2343c66ac9dbSNicholas Bellinger 	int move = 0;
2344c66ac9dbSNicholas Bellinger 
2345c87fbd56SChristoph Hellwig 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
2346c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
23479105bfc0SAndy Grover 		return count;
2348c87fbd56SChristoph Hellwig 
2349c66ac9dbSNicholas Bellinger 	if (count > LU_GROUP_NAME_BUF) {
23506708bb27SAndy Grover 		pr_err("ALUA LU Group Alias too large!\n");
2351c66ac9dbSNicholas Bellinger 		return -EINVAL;
2352c66ac9dbSNicholas Bellinger 	}
2353c66ac9dbSNicholas Bellinger 	memcpy(buf, page, count);
2354c66ac9dbSNicholas Bellinger 	/*
2355c66ac9dbSNicholas Bellinger 	 * Any ALUA logical unit alias besides "NULL" means we will be
2356c66ac9dbSNicholas Bellinger 	 * making a new group association.
2357c66ac9dbSNicholas Bellinger 	 */
2358c66ac9dbSNicholas Bellinger 	if (strcmp(strstrip(buf), "NULL")) {
2359c66ac9dbSNicholas Bellinger 		/*
2360c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name() will increment reference to
2361c66ac9dbSNicholas Bellinger 		 * struct t10_alua_lu_gp.  This reference is released with
2362c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name below().
2363c66ac9dbSNicholas Bellinger 		 */
2364c66ac9dbSNicholas Bellinger 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
23656708bb27SAndy Grover 		if (!lu_gp_new)
2366c66ac9dbSNicholas Bellinger 			return -ENODEV;
2367c66ac9dbSNicholas Bellinger 	}
2368c66ac9dbSNicholas Bellinger 
2369c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2370c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
23716708bb27SAndy Grover 	if (lu_gp) {
2372c66ac9dbSNicholas Bellinger 		/*
2373c66ac9dbSNicholas Bellinger 		 * Clearing an existing lu_gp association, and replacing
2374c66ac9dbSNicholas Bellinger 		 * with NULL
2375c66ac9dbSNicholas Bellinger 		 */
23766708bb27SAndy Grover 		if (!lu_gp_new) {
23776708bb27SAndy Grover 			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
2378c66ac9dbSNicholas Bellinger 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2379c66ac9dbSNicholas Bellinger 				" %hu\n",
2380c66ac9dbSNicholas Bellinger 				config_item_name(&hba->hba_group.cg_item),
23810fd97ccfSChristoph Hellwig 				config_item_name(&dev->dev_group.cg_item),
2382c66ac9dbSNicholas Bellinger 				config_item_name(&lu_gp->lu_gp_group.cg_item),
2383c66ac9dbSNicholas Bellinger 				lu_gp->lu_gp_id);
2384c66ac9dbSNicholas Bellinger 
2385c66ac9dbSNicholas Bellinger 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2386c66ac9dbSNicholas Bellinger 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2387c66ac9dbSNicholas Bellinger 
2388c66ac9dbSNicholas Bellinger 			return count;
2389c66ac9dbSNicholas Bellinger 		}
2390c66ac9dbSNicholas Bellinger 		/*
2391c66ac9dbSNicholas Bellinger 		 * Removing existing association of lu_gp_mem with lu_gp
2392c66ac9dbSNicholas Bellinger 		 */
2393c66ac9dbSNicholas Bellinger 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2394c66ac9dbSNicholas Bellinger 		move = 1;
2395c66ac9dbSNicholas Bellinger 	}
2396c66ac9dbSNicholas Bellinger 	/*
2397c66ac9dbSNicholas Bellinger 	 * Associate lu_gp_mem with lu_gp_new.
2398c66ac9dbSNicholas Bellinger 	 */
2399c66ac9dbSNicholas Bellinger 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2400c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2401c66ac9dbSNicholas Bellinger 
24026708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
2403c66ac9dbSNicholas Bellinger 		" core/alua/lu_gps/%s, ID: %hu\n",
2404c66ac9dbSNicholas Bellinger 		(move) ? "Moving" : "Adding",
2405c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
24060fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
2407c66ac9dbSNicholas Bellinger 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2408c66ac9dbSNicholas Bellinger 		lu_gp_new->lu_gp_id);
2409c66ac9dbSNicholas Bellinger 
2410c66ac9dbSNicholas Bellinger 	core_alua_put_lu_gp_from_name(lu_gp_new);
2411c66ac9dbSNicholas Bellinger 	return count;
2412c66ac9dbSNicholas Bellinger }
2413c66ac9dbSNicholas Bellinger 
24142eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
2415229d4f11SHannes Reinecke {
24162eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2417229d4f11SHannes Reinecke 	struct t10_alua_lba_map *map;
2418229d4f11SHannes Reinecke 	struct t10_alua_lba_map_member *mem;
2419229d4f11SHannes Reinecke 	char *b = page;
2420229d4f11SHannes Reinecke 	int bl = 0;
2421229d4f11SHannes Reinecke 	char state;
2422229d4f11SHannes Reinecke 
2423229d4f11SHannes Reinecke 	spin_lock(&dev->t10_alua.lba_map_lock);
2424229d4f11SHannes Reinecke 	if (!list_empty(&dev->t10_alua.lba_map_list))
2425229d4f11SHannes Reinecke 	    bl += sprintf(b + bl, "%u %u\n",
2426229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_size,
2427229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_multiplier);
2428229d4f11SHannes Reinecke 	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2429229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "%llu %llu",
2430229d4f11SHannes Reinecke 			      map->lba_map_first_lba, map->lba_map_last_lba);
2431229d4f11SHannes Reinecke 		list_for_each_entry(mem, &map->lba_map_mem_list,
2432229d4f11SHannes Reinecke 				    lba_map_mem_list) {
2433229d4f11SHannes Reinecke 			switch (mem->lba_map_mem_alua_state) {
2434229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2435229d4f11SHannes Reinecke 				state = 'O';
2436229d4f11SHannes Reinecke 				break;
2437229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2438229d4f11SHannes Reinecke 				state = 'A';
2439229d4f11SHannes Reinecke 				break;
2440229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_STANDBY:
2441229d4f11SHannes Reinecke 				state = 'S';
2442229d4f11SHannes Reinecke 				break;
2443229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_UNAVAILABLE:
2444229d4f11SHannes Reinecke 				state = 'U';
2445229d4f11SHannes Reinecke 				break;
2446229d4f11SHannes Reinecke 			default:
2447229d4f11SHannes Reinecke 				state = '.';
2448229d4f11SHannes Reinecke 				break;
2449229d4f11SHannes Reinecke 			}
2450229d4f11SHannes Reinecke 			bl += sprintf(b + bl, " %d:%c",
2451229d4f11SHannes Reinecke 				      mem->lba_map_mem_alua_pg_id, state);
2452229d4f11SHannes Reinecke 		}
2453229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "\n");
2454229d4f11SHannes Reinecke 	}
2455229d4f11SHannes Reinecke 	spin_unlock(&dev->t10_alua.lba_map_lock);
2456229d4f11SHannes Reinecke 	return bl;
2457229d4f11SHannes Reinecke }
2458229d4f11SHannes Reinecke 
24592eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_store(struct config_item *item,
24602eafd729SChristoph Hellwig 		const char *page, size_t count)
2461229d4f11SHannes Reinecke {
24622eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2463229d4f11SHannes Reinecke 	struct t10_alua_lba_map *lba_map = NULL;
2464229d4f11SHannes Reinecke 	struct list_head lba_list;
2465f0a8afecSBart Van Assche 	char *map_entries, *orig, *ptr;
2466229d4f11SHannes Reinecke 	char state;
2467229d4f11SHannes Reinecke 	int pg_num = -1, pg;
2468229d4f11SHannes Reinecke 	int ret = 0, num = 0, pg_id, alua_state;
2469229d4f11SHannes Reinecke 	unsigned long start_lba = -1, end_lba = -1;
2470229d4f11SHannes Reinecke 	unsigned long segment_size = -1, segment_mult = -1;
2471229d4f11SHannes Reinecke 
2472f0a8afecSBart Van Assche 	orig = map_entries = kstrdup(page, GFP_KERNEL);
2473229d4f11SHannes Reinecke 	if (!map_entries)
2474229d4f11SHannes Reinecke 		return -ENOMEM;
2475229d4f11SHannes Reinecke 
2476229d4f11SHannes Reinecke 	INIT_LIST_HEAD(&lba_list);
2477229d4f11SHannes Reinecke 	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2478229d4f11SHannes Reinecke 		if (!*ptr)
2479229d4f11SHannes Reinecke 			continue;
2480229d4f11SHannes Reinecke 
2481229d4f11SHannes Reinecke 		if (num == 0) {
2482229d4f11SHannes Reinecke 			if (sscanf(ptr, "%lu %lu\n",
2483229d4f11SHannes Reinecke 				   &segment_size, &segment_mult) != 2) {
2484229d4f11SHannes Reinecke 				pr_err("Invalid line %d\n", num);
2485229d4f11SHannes Reinecke 				ret = -EINVAL;
2486229d4f11SHannes Reinecke 				break;
2487229d4f11SHannes Reinecke 			}
2488229d4f11SHannes Reinecke 			num++;
2489229d4f11SHannes Reinecke 			continue;
2490229d4f11SHannes Reinecke 		}
2491229d4f11SHannes Reinecke 		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2492229d4f11SHannes Reinecke 			pr_err("Invalid line %d\n", num);
2493229d4f11SHannes Reinecke 			ret = -EINVAL;
2494229d4f11SHannes Reinecke 			break;
2495229d4f11SHannes Reinecke 		}
2496229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2497229d4f11SHannes Reinecke 		if (!ptr) {
2498229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing end lba\n", num);
2499229d4f11SHannes Reinecke 			ret = -EINVAL;
2500229d4f11SHannes Reinecke 			break;
2501229d4f11SHannes Reinecke 		}
2502229d4f11SHannes Reinecke 		ptr++;
2503229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2504229d4f11SHannes Reinecke 		if (!ptr) {
2505229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing state definitions\n",
2506229d4f11SHannes Reinecke 			       num);
2507229d4f11SHannes Reinecke 			ret = -EINVAL;
2508229d4f11SHannes Reinecke 			break;
2509229d4f11SHannes Reinecke 		}
2510229d4f11SHannes Reinecke 		ptr++;
2511229d4f11SHannes Reinecke 		lba_map = core_alua_allocate_lba_map(&lba_list,
2512229d4f11SHannes Reinecke 						     start_lba, end_lba);
2513229d4f11SHannes Reinecke 		if (IS_ERR(lba_map)) {
2514229d4f11SHannes Reinecke 			ret = PTR_ERR(lba_map);
2515229d4f11SHannes Reinecke 			break;
2516229d4f11SHannes Reinecke 		}
2517229d4f11SHannes Reinecke 		pg = 0;
2518229d4f11SHannes Reinecke 		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2519229d4f11SHannes Reinecke 			switch (state) {
2520229d4f11SHannes Reinecke 			case 'O':
2521229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2522229d4f11SHannes Reinecke 				break;
2523229d4f11SHannes Reinecke 			case 'A':
2524229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2525229d4f11SHannes Reinecke 				break;
2526229d4f11SHannes Reinecke 			case 'S':
2527229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_STANDBY;
2528229d4f11SHannes Reinecke 				break;
2529229d4f11SHannes Reinecke 			case 'U':
2530229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2531229d4f11SHannes Reinecke 				break;
2532229d4f11SHannes Reinecke 			default:
2533229d4f11SHannes Reinecke 				pr_err("Invalid ALUA state '%c'\n", state);
2534229d4f11SHannes Reinecke 				ret = -EINVAL;
2535229d4f11SHannes Reinecke 				goto out;
2536229d4f11SHannes Reinecke 			}
2537229d4f11SHannes Reinecke 
2538229d4f11SHannes Reinecke 			ret = core_alua_allocate_lba_map_mem(lba_map,
2539229d4f11SHannes Reinecke 							     pg_id, alua_state);
2540229d4f11SHannes Reinecke 			if (ret) {
2541229d4f11SHannes Reinecke 				pr_err("Invalid target descriptor %d:%c "
2542229d4f11SHannes Reinecke 				       "at line %d\n",
2543229d4f11SHannes Reinecke 				       pg_id, state, num);
2544229d4f11SHannes Reinecke 				break;
2545229d4f11SHannes Reinecke 			}
2546229d4f11SHannes Reinecke 			pg++;
2547229d4f11SHannes Reinecke 			ptr = strchr(ptr, ' ');
2548229d4f11SHannes Reinecke 			if (ptr)
2549229d4f11SHannes Reinecke 				ptr++;
2550229d4f11SHannes Reinecke 			else
2551229d4f11SHannes Reinecke 				break;
2552229d4f11SHannes Reinecke 		}
2553229d4f11SHannes Reinecke 		if (pg_num == -1)
2554229d4f11SHannes Reinecke 		    pg_num = pg;
2555229d4f11SHannes Reinecke 		else if (pg != pg_num) {
2556229d4f11SHannes Reinecke 			pr_err("Only %d from %d port groups definitions "
2557229d4f11SHannes Reinecke 			       "at line %d\n", pg, pg_num, num);
2558229d4f11SHannes Reinecke 			ret = -EINVAL;
2559229d4f11SHannes Reinecke 			break;
2560229d4f11SHannes Reinecke 		}
2561229d4f11SHannes Reinecke 		num++;
2562229d4f11SHannes Reinecke 	}
2563229d4f11SHannes Reinecke out:
2564229d4f11SHannes Reinecke 	if (ret) {
2565229d4f11SHannes Reinecke 		core_alua_free_lba_map(&lba_list);
2566229d4f11SHannes Reinecke 		count = ret;
2567229d4f11SHannes Reinecke 	} else
2568229d4f11SHannes Reinecke 		core_alua_set_lba_map(dev, &lba_list,
2569229d4f11SHannes Reinecke 				      segment_size, segment_mult);
2570f0a8afecSBart Van Assche 	kfree(orig);
2571229d4f11SHannes Reinecke 	return count;
2572229d4f11SHannes Reinecke }
2573229d4f11SHannes Reinecke 
25742eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_dev_, info);
25752eafd729SChristoph Hellwig CONFIGFS_ATTR_WO(target_dev_, control);
25762eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alias);
25772eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, udev_path);
25782eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, enable);
25792eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alua_lu_gp);
25802eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, lba_map);
2581229d4f11SHannes Reinecke 
258273112edcSNicholas Bellinger static struct configfs_attribute *target_core_dev_attrs[] = {
25832eafd729SChristoph Hellwig 	&target_dev_attr_info,
25842eafd729SChristoph Hellwig 	&target_dev_attr_control,
25852eafd729SChristoph Hellwig 	&target_dev_attr_alias,
25862eafd729SChristoph Hellwig 	&target_dev_attr_udev_path,
25872eafd729SChristoph Hellwig 	&target_dev_attr_enable,
25882eafd729SChristoph Hellwig 	&target_dev_attr_alua_lu_gp,
25892eafd729SChristoph Hellwig 	&target_dev_attr_lba_map,
2590c66ac9dbSNicholas Bellinger 	NULL,
2591c66ac9dbSNicholas Bellinger };
2592c66ac9dbSNicholas Bellinger 
2593c66ac9dbSNicholas Bellinger static void target_core_dev_release(struct config_item *item)
2594c66ac9dbSNicholas Bellinger {
25950fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
25960fd97ccfSChristoph Hellwig 	struct se_device *dev =
25970fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2598c66ac9dbSNicholas Bellinger 
25990fd97ccfSChristoph Hellwig 	target_free_device(dev);
2600c66ac9dbSNicholas Bellinger }
2601c66ac9dbSNicholas Bellinger 
2602c17cd249SNicholas Bellinger /*
2603c17cd249SNicholas Bellinger  * Used in target_core_fabric_configfs.c to verify valid se_device symlink
2604c17cd249SNicholas Bellinger  * within target_fabric_port_link()
2605c17cd249SNicholas Bellinger  */
2606c17cd249SNicholas Bellinger struct configfs_item_operations target_core_dev_item_ops = {
2607c66ac9dbSNicholas Bellinger 	.release		= target_core_dev_release,
2608c66ac9dbSNicholas Bellinger };
2609c66ac9dbSNicholas Bellinger 
261073112edcSNicholas Bellinger TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2611c66ac9dbSNicholas Bellinger 
261273112edcSNicholas Bellinger /* End functions for struct config_item_type tb_dev_cit */
2613c66ac9dbSNicholas Bellinger 
2614c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2615c66ac9dbSNicholas Bellinger 
26162eafd729SChristoph Hellwig static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2617c66ac9dbSNicholas Bellinger {
26182eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_lu_gp,
26192eafd729SChristoph Hellwig 			lu_gp_group);
26202eafd729SChristoph Hellwig }
26212eafd729SChristoph Hellwig 
26222eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
26232eafd729SChristoph Hellwig {
26242eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
26252eafd729SChristoph Hellwig 
26266708bb27SAndy Grover 	if (!lu_gp->lu_gp_valid_id)
2627c66ac9dbSNicholas Bellinger 		return 0;
2628c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2629c66ac9dbSNicholas Bellinger }
2630c66ac9dbSNicholas Bellinger 
26312eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
26322eafd729SChristoph Hellwig 		const char *page, size_t count)
2633c66ac9dbSNicholas Bellinger {
26342eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2635c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2636c66ac9dbSNicholas Bellinger 	unsigned long lu_gp_id;
2637c66ac9dbSNicholas Bellinger 	int ret;
2638c66ac9dbSNicholas Bellinger 
263957103d7fSJingoo Han 	ret = kstrtoul(page, 0, &lu_gp_id);
2640c66ac9dbSNicholas Bellinger 	if (ret < 0) {
264157103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2642c66ac9dbSNicholas Bellinger 			" lu_gp_id\n", ret);
264357103d7fSJingoo Han 		return ret;
2644c66ac9dbSNicholas Bellinger 	}
2645c66ac9dbSNicholas Bellinger 	if (lu_gp_id > 0x0000ffff) {
26466708bb27SAndy Grover 		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2647c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", lu_gp_id);
2648c66ac9dbSNicholas Bellinger 		return -EINVAL;
2649c66ac9dbSNicholas Bellinger 	}
2650c66ac9dbSNicholas Bellinger 
2651c66ac9dbSNicholas Bellinger 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2652c66ac9dbSNicholas Bellinger 	if (ret < 0)
2653c66ac9dbSNicholas Bellinger 		return -EINVAL;
2654c66ac9dbSNicholas Bellinger 
26556708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2656c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
2657c66ac9dbSNicholas Bellinger 		config_item_name(&alua_lu_gp_cg->cg_item),
2658c66ac9dbSNicholas Bellinger 		lu_gp->lu_gp_id);
2659c66ac9dbSNicholas Bellinger 
2660c66ac9dbSNicholas Bellinger 	return count;
2661c66ac9dbSNicholas Bellinger }
2662c66ac9dbSNicholas Bellinger 
26632eafd729SChristoph Hellwig static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2664c66ac9dbSNicholas Bellinger {
26652eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2666c66ac9dbSNicholas Bellinger 	struct se_device *dev;
2667c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2668c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2669c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
26702d4e2dafSChaitanya Kulkarni 	unsigned char buf[LU_GROUP_NAME_BUF] = { };
2671c66ac9dbSNicholas Bellinger 
2672c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp->lu_gp_lock);
2673c66ac9dbSNicholas Bellinger 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2674c66ac9dbSNicholas Bellinger 		dev = lu_gp_mem->lu_gp_mem_dev;
26750fd97ccfSChristoph Hellwig 		hba = dev->se_hba;
2676c66ac9dbSNicholas Bellinger 
2677c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2678c66ac9dbSNicholas Bellinger 			config_item_name(&hba->hba_group.cg_item),
26790fd97ccfSChristoph Hellwig 			config_item_name(&dev->dev_group.cg_item));
2680c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2681c66ac9dbSNicholas Bellinger 
2682c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
26836708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2684c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2685c66ac9dbSNicholas Bellinger 			break;
2686c66ac9dbSNicholas Bellinger 		}
2687c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2688c66ac9dbSNicholas Bellinger 		len += cur_len;
2689c66ac9dbSNicholas Bellinger 	}
2690c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp->lu_gp_lock);
2691c66ac9dbSNicholas Bellinger 
2692c66ac9dbSNicholas Bellinger 	return len;
2693c66ac9dbSNicholas Bellinger }
2694c66ac9dbSNicholas Bellinger 
26952eafd729SChristoph Hellwig CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
26962eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_lu_gp_, members);
2697c66ac9dbSNicholas Bellinger 
2698c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
26992eafd729SChristoph Hellwig 	&target_lu_gp_attr_lu_gp_id,
27002eafd729SChristoph Hellwig 	&target_lu_gp_attr_members,
2701c66ac9dbSNicholas Bellinger 	NULL,
2702c66ac9dbSNicholas Bellinger };
2703c66ac9dbSNicholas Bellinger 
27041f6fe7cbSNicholas Bellinger static void target_core_alua_lu_gp_release(struct config_item *item)
27051f6fe7cbSNicholas Bellinger {
27061f6fe7cbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
27071f6fe7cbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
27081f6fe7cbSNicholas Bellinger 
27091f6fe7cbSNicholas Bellinger 	core_alua_free_lu_gp(lu_gp);
27101f6fe7cbSNicholas Bellinger }
27111f6fe7cbSNicholas Bellinger 
2712c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_lu_gp_ops = {
27131f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_lu_gp_release,
2714c66ac9dbSNicholas Bellinger };
2715c66ac9dbSNicholas Bellinger 
2716ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_lu_gp_cit = {
2717c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
2718c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_lu_gp_attrs,
2719c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2720c66ac9dbSNicholas Bellinger };
2721c66ac9dbSNicholas Bellinger 
2722c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2723c66ac9dbSNicholas Bellinger 
2724c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2725c66ac9dbSNicholas Bellinger 
2726c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_lu_gp(
2727c66ac9dbSNicholas Bellinger 	struct config_group *group,
2728c66ac9dbSNicholas Bellinger 	const char *name)
2729c66ac9dbSNicholas Bellinger {
2730c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2731c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = NULL;
2732c66ac9dbSNicholas Bellinger 	struct config_item *alua_lu_gp_ci = NULL;
2733c66ac9dbSNicholas Bellinger 
2734c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp(name, 0);
2735c66ac9dbSNicholas Bellinger 	if (IS_ERR(lu_gp))
2736c66ac9dbSNicholas Bellinger 		return NULL;
2737c66ac9dbSNicholas Bellinger 
2738c66ac9dbSNicholas Bellinger 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
2739c66ac9dbSNicholas Bellinger 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2740c66ac9dbSNicholas Bellinger 
2741c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_lu_gp_cg, name,
2742c66ac9dbSNicholas Bellinger 			&target_core_alua_lu_gp_cit);
2743c66ac9dbSNicholas Bellinger 
27446708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2745c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s\n",
2746c66ac9dbSNicholas Bellinger 		config_item_name(alua_lu_gp_ci));
2747c66ac9dbSNicholas Bellinger 
2748c66ac9dbSNicholas Bellinger 	return alua_lu_gp_cg;
2749c66ac9dbSNicholas Bellinger 
2750c66ac9dbSNicholas Bellinger }
2751c66ac9dbSNicholas Bellinger 
2752c66ac9dbSNicholas Bellinger static void target_core_alua_drop_lu_gp(
2753c66ac9dbSNicholas Bellinger 	struct config_group *group,
2754c66ac9dbSNicholas Bellinger 	struct config_item *item)
2755c66ac9dbSNicholas Bellinger {
2756c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2757c66ac9dbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
2758c66ac9dbSNicholas Bellinger 
27596708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2760c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
2761c66ac9dbSNicholas Bellinger 		config_item_name(item), lu_gp->lu_gp_id);
27621f6fe7cbSNicholas Bellinger 	/*
27631f6fe7cbSNicholas Bellinger 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
27641f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_lu_gp_release()
27651f6fe7cbSNicholas Bellinger 	 */
2766c66ac9dbSNicholas Bellinger 	config_item_put(item);
2767c66ac9dbSNicholas Bellinger }
2768c66ac9dbSNicholas Bellinger 
2769c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2770c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_lu_gp,
2771c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_lu_gp,
2772c66ac9dbSNicholas Bellinger };
2773c66ac9dbSNicholas Bellinger 
2774ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_lu_gps_cit = {
2775c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2776c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
2777c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2778c66ac9dbSNicholas Bellinger };
2779c66ac9dbSNicholas Bellinger 
2780c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2781c66ac9dbSNicholas Bellinger 
2782c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2783c66ac9dbSNicholas Bellinger 
27842eafd729SChristoph Hellwig static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
27852eafd729SChristoph Hellwig {
27862eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
27872eafd729SChristoph Hellwig 			tg_pt_gp_group);
27882eafd729SChristoph Hellwig }
2789c66ac9dbSNicholas Bellinger 
27902eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2791c66ac9dbSNicholas Bellinger 		char *page)
2792c66ac9dbSNicholas Bellinger {
2793c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n",
2794d19c4643SMike Christie 		       to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
2795c66ac9dbSNicholas Bellinger }
2796c66ac9dbSNicholas Bellinger 
27972eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
27982eafd729SChristoph Hellwig 		const char *page, size_t count)
2799c66ac9dbSNicholas Bellinger {
28002eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
28010fd97ccfSChristoph Hellwig 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2802c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2803c66ac9dbSNicholas Bellinger 	int new_state, ret;
2804c66ac9dbSNicholas Bellinger 
28056708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2806baa75afdSBart Van Assche 		pr_err("Unable to do implicit ALUA on invalid tg_pt_gp ID\n");
2807c66ac9dbSNicholas Bellinger 		return -EINVAL;
2808c66ac9dbSNicholas Bellinger 	}
2809cb0f32e1SMike Christie 	if (!target_dev_configured(dev)) {
2810f1453773SNicholas Bellinger 		pr_err("Unable to set alua_access_state while device is"
2811f1453773SNicholas Bellinger 		       " not configured\n");
2812f1453773SNicholas Bellinger 		return -ENODEV;
2813f1453773SNicholas Bellinger 	}
2814c66ac9dbSNicholas Bellinger 
281557103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2816c66ac9dbSNicholas Bellinger 	if (ret < 0) {
28176708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access state from"
2818c66ac9dbSNicholas Bellinger 				" %s\n", page);
281957103d7fSJingoo Han 		return ret;
2820c66ac9dbSNicholas Bellinger 	}
2821c66ac9dbSNicholas Bellinger 	new_state = (int)tmp;
2822c66ac9dbSNicholas Bellinger 
2823125d0119SHannes Reinecke 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2824125d0119SHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA"
2825125d0119SHannes Reinecke 			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
2826c66ac9dbSNicholas Bellinger 		return -EINVAL;
2827c66ac9dbSNicholas Bellinger 	}
2828c66094bfSHannes Reinecke 	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2829c66094bfSHannes Reinecke 	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2830c66094bfSHannes Reinecke 		/* LBA DEPENDENT is only allowed with implicit ALUA */
2831c66094bfSHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA transition"
2832c66094bfSHannes Reinecke 		       " while explicit ALUA management is enabled\n");
2833c66094bfSHannes Reinecke 		return -EINVAL;
2834c66094bfSHannes Reinecke 	}
2835c66ac9dbSNicholas Bellinger 
28360fd97ccfSChristoph Hellwig 	ret = core_alua_do_port_transition(tg_pt_gp, dev,
2837c66ac9dbSNicholas Bellinger 					NULL, NULL, new_state, 0);
2838c66ac9dbSNicholas Bellinger 	return (!ret) ? count : -EINVAL;
2839c66ac9dbSNicholas Bellinger }
2840c66ac9dbSNicholas Bellinger 
28412eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2842c66ac9dbSNicholas Bellinger 		char *page)
2843c66ac9dbSNicholas Bellinger {
28442eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2845c66ac9dbSNicholas Bellinger 	return sprintf(page, "%s\n",
2846c66ac9dbSNicholas Bellinger 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2847c66ac9dbSNicholas Bellinger }
2848c66ac9dbSNicholas Bellinger 
28492eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_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 new_status, ret;
2855c66ac9dbSNicholas Bellinger 
28566708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2857baa75afdSBart Van Assche 		pr_err("Unable to set ALUA access status on invalid tg_pt_gp ID\n");
2858c66ac9dbSNicholas Bellinger 		return -EINVAL;
2859c66ac9dbSNicholas Bellinger 	}
2860c66ac9dbSNicholas Bellinger 
286157103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2862c66ac9dbSNicholas Bellinger 	if (ret < 0) {
28636708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access status"
2864c66ac9dbSNicholas Bellinger 				" from %s\n", page);
286557103d7fSJingoo Han 		return ret;
2866c66ac9dbSNicholas Bellinger 	}
2867c66ac9dbSNicholas Bellinger 	new_status = (int)tmp;
2868c66ac9dbSNicholas Bellinger 
2869c66ac9dbSNicholas Bellinger 	if ((new_status != ALUA_STATUS_NONE) &&
2870125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2871125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
28726708bb27SAndy Grover 		pr_err("Illegal ALUA access status: 0x%02x\n",
2873c66ac9dbSNicholas Bellinger 				new_status);
2874c66ac9dbSNicholas Bellinger 		return -EINVAL;
2875c66ac9dbSNicholas Bellinger 	}
2876c66ac9dbSNicholas Bellinger 
2877c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2878c66ac9dbSNicholas Bellinger 	return count;
2879c66ac9dbSNicholas Bellinger }
2880c66ac9dbSNicholas Bellinger 
28812eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2882c66ac9dbSNicholas Bellinger 		char *page)
2883c66ac9dbSNicholas Bellinger {
28842eafd729SChristoph Hellwig 	return core_alua_show_access_type(to_tg_pt_gp(item), page);
2885c66ac9dbSNicholas Bellinger }
2886c66ac9dbSNicholas Bellinger 
28872eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
28882eafd729SChristoph Hellwig 		const char *page, size_t count)
2889c66ac9dbSNicholas Bellinger {
28902eafd729SChristoph Hellwig 	return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2891c66ac9dbSNicholas Bellinger }
2892c66ac9dbSNicholas Bellinger 
28932eafd729SChristoph Hellwig #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit)				\
28942eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_show(		\
28952eafd729SChristoph Hellwig 		struct config_item *item, char *p)			\
2896b0a382c5SHannes Reinecke {									\
28972eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
28982eafd729SChristoph Hellwig 	return sprintf(p, "%d\n",					\
28992eafd729SChristoph Hellwig 		!!(t->tg_pt_gp_alua_supported_states & _bit));		\
29002eafd729SChristoph Hellwig }									\
29012eafd729SChristoph Hellwig 									\
29022eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_store(		\
29032eafd729SChristoph Hellwig 		struct config_item *item, const char *p, size_t c)	\
2904b0a382c5SHannes Reinecke {									\
29052eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
2906b0a382c5SHannes Reinecke 	unsigned long tmp;						\
2907b0a382c5SHannes Reinecke 	int ret;							\
2908b0a382c5SHannes Reinecke 									\
2909b0a382c5SHannes Reinecke 	if (!t->tg_pt_gp_valid_id) {					\
2910baa75afdSBart Van Assche 		pr_err("Unable to set " #_name " ALUA state on invalid tg_pt_gp ID\n"); \
2911b0a382c5SHannes Reinecke 		return -EINVAL;						\
2912b0a382c5SHannes Reinecke 	}								\
2913b0a382c5SHannes Reinecke 									\
2914b0a382c5SHannes Reinecke 	ret = kstrtoul(p, 0, &tmp);					\
2915b0a382c5SHannes Reinecke 	if (ret < 0) {							\
2916b0a382c5SHannes Reinecke 		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
2917b0a382c5SHannes Reinecke 		return -EINVAL;						\
2918b0a382c5SHannes Reinecke 	}								\
2919b0a382c5SHannes Reinecke 	if (tmp > 1) {							\
2920b0a382c5SHannes Reinecke 		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2921b0a382c5SHannes Reinecke 		return -EINVAL;						\
2922b0a382c5SHannes Reinecke 	}								\
29231f0b030cSSebastian Herbszt 	if (tmp)							\
29242eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states |= _bit;		\
2925b0a382c5SHannes Reinecke 	else								\
29262eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states &= ~_bit;		\
2927b0a382c5SHannes Reinecke 									\
2928b0a382c5SHannes Reinecke 	return c;							\
29296be526c4SHannes Reinecke }
29306be526c4SHannes Reinecke 
29312eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
29322eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
29332eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
29342eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
29352eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
29362eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
29372eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
29386be526c4SHannes Reinecke 
29392eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_show(
29402eafd729SChristoph Hellwig 		struct config_item *item, char *page)
2941c66ac9dbSNicholas Bellinger {
29422eafd729SChristoph Hellwig 	return sprintf(page, "%d\n",
29432eafd729SChristoph Hellwig 		to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
2944c66ac9dbSNicholas Bellinger }
2945c66ac9dbSNicholas Bellinger 
29462eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_store(
29472eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2948c66ac9dbSNicholas Bellinger {
29492eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2950c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2951c66ac9dbSNicholas Bellinger 	int ret;
2952c66ac9dbSNicholas Bellinger 
295357103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2954c66ac9dbSNicholas Bellinger 	if (ret < 0) {
29556708bb27SAndy Grover 		pr_err("Unable to extract alua_write_metadata\n");
295657103d7fSJingoo Han 		return ret;
2957c66ac9dbSNicholas Bellinger 	}
2958c66ac9dbSNicholas Bellinger 
2959c66ac9dbSNicholas Bellinger 	if ((tmp != 0) && (tmp != 1)) {
29606708bb27SAndy Grover 		pr_err("Illegal value for alua_write_metadata:"
2961c66ac9dbSNicholas Bellinger 			" %lu\n", tmp);
2962c66ac9dbSNicholas Bellinger 		return -EINVAL;
2963c66ac9dbSNicholas Bellinger 	}
2964c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2965c66ac9dbSNicholas Bellinger 
2966c66ac9dbSNicholas Bellinger 	return count;
2967c66ac9dbSNicholas Bellinger }
2968c66ac9dbSNicholas Bellinger 
29692eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2970c66ac9dbSNicholas Bellinger 		char *page)
2971c66ac9dbSNicholas Bellinger {
29722eafd729SChristoph Hellwig 	return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
2973c66ac9dbSNicholas Bellinger }
2974c66ac9dbSNicholas Bellinger 
29752eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
29762eafd729SChristoph Hellwig 		const char *page, size_t count)
2977c66ac9dbSNicholas Bellinger {
29782eafd729SChristoph Hellwig 	return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
29792eafd729SChristoph Hellwig 			count);
2980c66ac9dbSNicholas Bellinger }
2981c66ac9dbSNicholas Bellinger 
29822eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2983c66ac9dbSNicholas Bellinger 		char *page)
2984c66ac9dbSNicholas Bellinger {
29852eafd729SChristoph Hellwig 	return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
2986c66ac9dbSNicholas Bellinger }
2987c66ac9dbSNicholas Bellinger 
29882eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
29892eafd729SChristoph Hellwig 		const char *page, size_t count)
2990c66ac9dbSNicholas Bellinger {
29912eafd729SChristoph Hellwig 	return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
29922eafd729SChristoph Hellwig 			count);
2993c66ac9dbSNicholas Bellinger }
2994c66ac9dbSNicholas Bellinger 
29952eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
29962eafd729SChristoph Hellwig 		struct config_item *item, char *page)
29972eafd729SChristoph Hellwig {
29982eafd729SChristoph Hellwig 	return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
29992eafd729SChristoph Hellwig }
3000c66ac9dbSNicholas Bellinger 
30012eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
30022eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
30032eafd729SChristoph Hellwig {
30042eafd729SChristoph Hellwig 	return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
30052eafd729SChristoph Hellwig 			count);
30062eafd729SChristoph Hellwig }
30072eafd729SChristoph Hellwig 
30082eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
30095b9a4d72SNicholas Bellinger 		char *page)
30105b9a4d72SNicholas Bellinger {
30112eafd729SChristoph Hellwig 	return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
30125b9a4d72SNicholas Bellinger }
30135b9a4d72SNicholas Bellinger 
30142eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
30152eafd729SChristoph Hellwig 		const char *page, size_t count)
30165b9a4d72SNicholas Bellinger {
30172eafd729SChristoph Hellwig 	return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
30185b9a4d72SNicholas Bellinger }
30195b9a4d72SNicholas Bellinger 
30202eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
3021c66ac9dbSNicholas Bellinger 		char *page)
3022c66ac9dbSNicholas Bellinger {
30232eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3024c66ac9dbSNicholas Bellinger 
30256708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id)
3026c66ac9dbSNicholas Bellinger 		return 0;
3027c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
3028c66ac9dbSNicholas Bellinger }
3029c66ac9dbSNicholas Bellinger 
30302eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
30312eafd729SChristoph Hellwig 		const char *page, size_t count)
3032c66ac9dbSNicholas Bellinger {
30332eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3034c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3035c66ac9dbSNicholas Bellinger 	unsigned long tg_pt_gp_id;
3036c66ac9dbSNicholas Bellinger 	int ret;
3037c66ac9dbSNicholas Bellinger 
303857103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tg_pt_gp_id);
3039c66ac9dbSNicholas Bellinger 	if (ret < 0) {
30403d035237SHannes Reinecke 		pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
30413d035237SHannes Reinecke 		       page);
304257103d7fSJingoo Han 		return ret;
3043c66ac9dbSNicholas Bellinger 	}
3044c66ac9dbSNicholas Bellinger 	if (tg_pt_gp_id > 0x0000ffff) {
30453d035237SHannes Reinecke 		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
30463d035237SHannes Reinecke 		       tg_pt_gp_id);
3047c66ac9dbSNicholas Bellinger 		return -EINVAL;
3048c66ac9dbSNicholas Bellinger 	}
3049c66ac9dbSNicholas Bellinger 
3050c66ac9dbSNicholas Bellinger 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
3051c66ac9dbSNicholas Bellinger 	if (ret < 0)
3052c66ac9dbSNicholas Bellinger 		return -EINVAL;
3053c66ac9dbSNicholas Bellinger 
30546708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
3055c66ac9dbSNicholas Bellinger 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
3056c66ac9dbSNicholas Bellinger 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
3057c66ac9dbSNicholas Bellinger 		tg_pt_gp->tg_pt_gp_id);
3058c66ac9dbSNicholas Bellinger 
3059c66ac9dbSNicholas Bellinger 	return count;
3060c66ac9dbSNicholas Bellinger }
3061c66ac9dbSNicholas Bellinger 
30622eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
3063c66ac9dbSNicholas Bellinger 		char *page)
3064c66ac9dbSNicholas Bellinger {
30652eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3066c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
3067c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
30682d4e2dafSChaitanya Kulkarni 	unsigned char buf[TG_PT_GROUP_NAME_BUF] = { };
3069c66ac9dbSNicholas Bellinger 
3070c66ac9dbSNicholas Bellinger 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
3071adf653f9SChristoph Hellwig 	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
3072adf653f9SChristoph Hellwig 			lun_tg_pt_gp_link) {
3073adf653f9SChristoph Hellwig 		struct se_portal_group *tpg = lun->lun_tpg;
3074c66ac9dbSNicholas Bellinger 
3075c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
307630c7ca93SDavid Disseldorp 			"/%s\n", tpg->se_tpg_tfo->fabric_name,
3077e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
3078e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
3079c66ac9dbSNicholas Bellinger 			config_item_name(&lun->lun_group.cg_item));
3080c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
3081c66ac9dbSNicholas Bellinger 
3082c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
30836708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
3084c66ac9dbSNicholas Bellinger 				"_members buffer\n");
3085c66ac9dbSNicholas Bellinger 			break;
3086c66ac9dbSNicholas Bellinger 		}
3087c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
3088c66ac9dbSNicholas Bellinger 		len += cur_len;
3089c66ac9dbSNicholas Bellinger 	}
3090c66ac9dbSNicholas Bellinger 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
3091c66ac9dbSNicholas Bellinger 
3092c66ac9dbSNicholas Bellinger 	return len;
3093c66ac9dbSNicholas Bellinger }
3094c66ac9dbSNicholas Bellinger 
30952eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
30962eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
30972eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
30982eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
30992eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
31002eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
31012eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
31022eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
31032eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
31042eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
31052eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
31062eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
31072eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
31082eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
31092eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
31102eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
31112eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
3112c66ac9dbSNicholas Bellinger 
3113c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
31142eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_state,
31152eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_status,
31162eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_type,
31172eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_transitioning,
31182eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_offline,
31192eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_lba_dependent,
31202eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_unavailable,
31212eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_standby,
31222eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_nonoptimized,
31232eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_optimized,
31242eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_write_metadata,
31252eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_nonop_delay_msecs,
31262eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_trans_delay_msecs,
31272eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_implicit_trans_secs,
31282eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_preferred,
31292eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_tg_pt_gp_id,
31302eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_members,
3131c66ac9dbSNicholas Bellinger 	NULL,
3132c66ac9dbSNicholas Bellinger };
3133c66ac9dbSNicholas Bellinger 
31341f6fe7cbSNicholas Bellinger static void target_core_alua_tg_pt_gp_release(struct config_item *item)
31351f6fe7cbSNicholas Bellinger {
31361f6fe7cbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
31371f6fe7cbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
31381f6fe7cbSNicholas Bellinger 
31391f6fe7cbSNicholas Bellinger 	core_alua_free_tg_pt_gp(tg_pt_gp);
31401f6fe7cbSNicholas Bellinger }
31411f6fe7cbSNicholas Bellinger 
3142c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
31431f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_tg_pt_gp_release,
3144c66ac9dbSNicholas Bellinger };
3145c66ac9dbSNicholas Bellinger 
3146ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_tg_pt_gp_cit = {
3147c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
3148c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
3149c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3150c66ac9dbSNicholas Bellinger };
3151c66ac9dbSNicholas Bellinger 
3152c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
3153c66ac9dbSNicholas Bellinger 
315472aca57bSNicholas Bellinger /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3155c66ac9dbSNicholas Bellinger 
3156c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_tg_pt_gp(
3157c66ac9dbSNicholas Bellinger 	struct config_group *group,
3158c66ac9dbSNicholas Bellinger 	const char *name)
3159c66ac9dbSNicholas Bellinger {
3160c66ac9dbSNicholas Bellinger 	struct t10_alua *alua = container_of(group, struct t10_alua,
3161c66ac9dbSNicholas Bellinger 					alua_tg_pt_gps_group);
3162c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
3163c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = NULL;
3164c66ac9dbSNicholas Bellinger 	struct config_item *alua_tg_pt_gp_ci = NULL;
3165c66ac9dbSNicholas Bellinger 
31660fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
31676708bb27SAndy Grover 	if (!tg_pt_gp)
3168c66ac9dbSNicholas Bellinger 		return NULL;
3169c66ac9dbSNicholas Bellinger 
3170c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3171c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
3172c66ac9dbSNicholas Bellinger 
3173c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
3174c66ac9dbSNicholas Bellinger 			&target_core_alua_tg_pt_gp_cit);
3175c66ac9dbSNicholas Bellinger 
31766708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
3177c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s\n",
3178c66ac9dbSNicholas Bellinger 		config_item_name(alua_tg_pt_gp_ci));
3179c66ac9dbSNicholas Bellinger 
3180c66ac9dbSNicholas Bellinger 	return alua_tg_pt_gp_cg;
3181c66ac9dbSNicholas Bellinger }
3182c66ac9dbSNicholas Bellinger 
3183c66ac9dbSNicholas Bellinger static void target_core_alua_drop_tg_pt_gp(
3184c66ac9dbSNicholas Bellinger 	struct config_group *group,
3185c66ac9dbSNicholas Bellinger 	struct config_item *item)
3186c66ac9dbSNicholas Bellinger {
3187c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3188c66ac9dbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3189c66ac9dbSNicholas Bellinger 
31906708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
3191c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
3192c66ac9dbSNicholas Bellinger 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
31931f6fe7cbSNicholas Bellinger 	/*
31941f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
31951f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_tg_pt_gp_release().
31961f6fe7cbSNicholas Bellinger 	 */
3197c66ac9dbSNicholas Bellinger 	config_item_put(item);
3198c66ac9dbSNicholas Bellinger }
3199c66ac9dbSNicholas Bellinger 
3200c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
3201c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_tg_pt_gp,
3202c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
3203c66ac9dbSNicholas Bellinger };
3204c66ac9dbSNicholas Bellinger 
320572aca57bSNicholas Bellinger TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
3206c66ac9dbSNicholas Bellinger 
320772aca57bSNicholas Bellinger /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3208c66ac9dbSNicholas Bellinger 
3209c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_cit */
3210c66ac9dbSNicholas Bellinger 
3211c66ac9dbSNicholas Bellinger /*
3212c66ac9dbSNicholas Bellinger  * target_core_alua_cit is a ConfigFS group that lives under
3213c66ac9dbSNicholas Bellinger  * /sys/kernel/config/target/core/alua.  There are default groups
3214c66ac9dbSNicholas Bellinger  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
3215c66ac9dbSNicholas Bellinger  * target_core_alua_cit in target_core_init_configfs() below.
3216c66ac9dbSNicholas Bellinger  */
3217ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_cit = {
3218c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
3219c66ac9dbSNicholas Bellinger 	.ct_attrs		= NULL,
3220c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3221c66ac9dbSNicholas Bellinger };
3222c66ac9dbSNicholas Bellinger 
3223c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_cit */
3224c66ac9dbSNicholas Bellinger 
3225d23ab570SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_stat_cit */
322612d23384SNicholas Bellinger 
322712d23384SNicholas Bellinger static struct config_group *target_core_stat_mkdir(
322812d23384SNicholas Bellinger 	struct config_group *group,
322912d23384SNicholas Bellinger 	const char *name)
323012d23384SNicholas Bellinger {
323112d23384SNicholas Bellinger 	return ERR_PTR(-ENOSYS);
323212d23384SNicholas Bellinger }
323312d23384SNicholas Bellinger 
323412d23384SNicholas Bellinger static void target_core_stat_rmdir(
323512d23384SNicholas Bellinger 	struct config_group *group,
323612d23384SNicholas Bellinger 	struct config_item *item)
323712d23384SNicholas Bellinger {
323812d23384SNicholas Bellinger 	return;
323912d23384SNicholas Bellinger }
324012d23384SNicholas Bellinger 
324112d23384SNicholas Bellinger static struct configfs_group_operations target_core_stat_group_ops = {
324212d23384SNicholas Bellinger 	.make_group		= &target_core_stat_mkdir,
324312d23384SNicholas Bellinger 	.drop_item		= &target_core_stat_rmdir,
324412d23384SNicholas Bellinger };
324512d23384SNicholas Bellinger 
3246d23ab570SNicholas Bellinger TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
324712d23384SNicholas Bellinger 
3248d23ab570SNicholas Bellinger /* End functions for struct config_item_type tb_dev_stat_cit */
324912d23384SNicholas Bellinger 
3250c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_hba_cit */
3251c66ac9dbSNicholas Bellinger 
3252c66ac9dbSNicholas Bellinger static struct config_group *target_core_make_subdev(
3253c66ac9dbSNicholas Bellinger 	struct config_group *group,
3254c66ac9dbSNicholas Bellinger 	const char *name)
3255c66ac9dbSNicholas Bellinger {
3256c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
3257c66ac9dbSNicholas Bellinger 	struct config_item *hba_ci = &group->cg_item;
3258c66ac9dbSNicholas Bellinger 	struct se_hba *hba = item_to_hba(hba_ci);
32590a06d430SChristoph Hellwig 	struct target_backend *tb = hba->backend;
32600fd97ccfSChristoph Hellwig 	struct se_device *dev;
326112d23384SNicholas Bellinger 	int errno = -ENOMEM, ret;
3262c66ac9dbSNicholas Bellinger 
326312d23384SNicholas Bellinger 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
326412d23384SNicholas Bellinger 	if (ret)
326512d23384SNicholas Bellinger 		return ERR_PTR(ret);
3266c66ac9dbSNicholas Bellinger 
32670fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, name);
32680fd97ccfSChristoph Hellwig 	if (!dev)
32690fd97ccfSChristoph Hellwig 		goto out_unlock;
3270c66ac9dbSNicholas Bellinger 
32711ae1602dSChristoph Hellwig 	config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
3272c66ac9dbSNicholas Bellinger 
32738dc31ff9SMike Christie 	config_group_init_type_name(&dev->dev_action_group, "action",
32748dc31ff9SMike Christie 			&tb->tb_dev_action_cit);
32758dc31ff9SMike Christie 	configfs_add_default_group(&dev->dev_action_group, &dev->dev_group);
32768dc31ff9SMike Christie 
32770fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
32780a06d430SChristoph Hellwig 			&tb->tb_dev_attrib_cit);
32791ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
32801ae1602dSChristoph Hellwig 
32810fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_pr_group, "pr",
32820a06d430SChristoph Hellwig 			&tb->tb_dev_pr_cit);
32831ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
32841ae1602dSChristoph Hellwig 
32850fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
32860a06d430SChristoph Hellwig 			&tb->tb_dev_wwn_cit);
32871ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
32881ae1602dSChristoph Hellwig 			&dev->dev_group);
32891ae1602dSChristoph Hellwig 
32900fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
32910a06d430SChristoph Hellwig 			"alua", &tb->tb_dev_alua_tg_pt_gps_cit);
32921ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
32931ae1602dSChristoph Hellwig 			&dev->dev_group);
32941ae1602dSChristoph Hellwig 
32950fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
32960a06d430SChristoph Hellwig 			"statistics", &tb->tb_dev_stat_cit);
32971ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_stat_grps.stat_group,
32981ae1602dSChristoph Hellwig 			&dev->dev_group);
329912d23384SNicholas Bellinger 
3300c66ac9dbSNicholas Bellinger 	/*
330112d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
3302c66ac9dbSNicholas Bellinger 	 */
33030fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
33046708bb27SAndy Grover 	if (!tg_pt_gp)
33051ae1602dSChristoph Hellwig 		goto out_free_device;
33060fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
3307c66ac9dbSNicholas Bellinger 
3308c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
3309c66ac9dbSNicholas Bellinger 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
33101ae1602dSChristoph Hellwig 	configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
33111ae1602dSChristoph Hellwig 			&dev->t10_alua.alua_tg_pt_gps_group);
33121ae1602dSChristoph Hellwig 
331312d23384SNicholas Bellinger 	/*
331412d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/statistics/ default groups
331512d23384SNicholas Bellinger 	 */
33160fd97ccfSChristoph Hellwig 	target_stat_setup_dev_default_groups(dev);
3317c66ac9dbSNicholas Bellinger 
3318c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
33191ae1602dSChristoph Hellwig 	return &dev->dev_group;
33200fd97ccfSChristoph Hellwig 
33210fd97ccfSChristoph Hellwig out_free_device:
33220fd97ccfSChristoph Hellwig 	target_free_device(dev);
33230fd97ccfSChristoph Hellwig out_unlock:
3324c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
332512d23384SNicholas Bellinger 	return ERR_PTR(errno);
3326c66ac9dbSNicholas Bellinger }
3327c66ac9dbSNicholas Bellinger 
3328c66ac9dbSNicholas Bellinger static void target_core_drop_subdev(
3329c66ac9dbSNicholas Bellinger 	struct config_group *group,
3330c66ac9dbSNicholas Bellinger 	struct config_item *item)
3331c66ac9dbSNicholas Bellinger {
33320fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
33330fd97ccfSChristoph Hellwig 	struct se_device *dev =
33340fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
3335c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
3336c66ac9dbSNicholas Bellinger 
33370fd97ccfSChristoph Hellwig 	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
3338c66ac9dbSNicholas Bellinger 
33391f6fe7cbSNicholas Bellinger 	mutex_lock(&hba->hba_access_mutex);
3340c66ac9dbSNicholas Bellinger 
33411ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
33421ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
334312d23384SNicholas Bellinger 
33441f6fe7cbSNicholas Bellinger 	/*
33451f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
33461f6fe7cbSNicholas Bellinger 	 * directly from target_core_alua_tg_pt_gp_release().
33471f6fe7cbSNicholas Bellinger 	 */
33480fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = NULL;
3349c66ac9dbSNicholas Bellinger 
33501ae1602dSChristoph Hellwig 	configfs_remove_default_groups(dev_cg);
33511ae1602dSChristoph Hellwig 
33521f6fe7cbSNicholas Bellinger 	/*
33530fd97ccfSChristoph Hellwig 	 * se_dev is released from target_core_dev_item_ops->release()
33541f6fe7cbSNicholas Bellinger 	 */
3355c66ac9dbSNicholas Bellinger 	config_item_put(item);
3356c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
3357c66ac9dbSNicholas Bellinger }
3358c66ac9dbSNicholas Bellinger 
3359c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_hba_group_ops = {
3360c66ac9dbSNicholas Bellinger 	.make_group		= target_core_make_subdev,
3361c66ac9dbSNicholas Bellinger 	.drop_item		= target_core_drop_subdev,
3362c66ac9dbSNicholas Bellinger };
3363c66ac9dbSNicholas Bellinger 
3364c66ac9dbSNicholas Bellinger 
33652eafd729SChristoph Hellwig static inline struct se_hba *to_hba(struct config_item *item)
3366c66ac9dbSNicholas Bellinger {
33672eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_hba, hba_group);
33682eafd729SChristoph Hellwig }
33692eafd729SChristoph Hellwig 
33702eafd729SChristoph Hellwig static ssize_t target_hba_info_show(struct config_item *item, char *page)
33712eafd729SChristoph Hellwig {
33722eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
33732eafd729SChristoph Hellwig 
3374c66ac9dbSNicholas Bellinger 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
33750a06d430SChristoph Hellwig 			hba->hba_id, hba->backend->ops->name,
3376ce8dd25dSChristoph Hellwig 			TARGET_CORE_VERSION);
3377c66ac9dbSNicholas Bellinger }
3378c66ac9dbSNicholas Bellinger 
33792eafd729SChristoph Hellwig static ssize_t target_hba_mode_show(struct config_item *item, char *page)
3380c66ac9dbSNicholas Bellinger {
33812eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
3382c66ac9dbSNicholas Bellinger 	int hba_mode = 0;
3383c66ac9dbSNicholas Bellinger 
3384c66ac9dbSNicholas Bellinger 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3385c66ac9dbSNicholas Bellinger 		hba_mode = 1;
3386c66ac9dbSNicholas Bellinger 
3387c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", hba_mode);
3388c66ac9dbSNicholas Bellinger }
3389c66ac9dbSNicholas Bellinger 
33902eafd729SChristoph Hellwig static ssize_t target_hba_mode_store(struct config_item *item,
3391c66ac9dbSNicholas Bellinger 		const char *page, size_t count)
3392c66ac9dbSNicholas Bellinger {
33932eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
3394c66ac9dbSNicholas Bellinger 	unsigned long mode_flag;
3395c66ac9dbSNicholas Bellinger 	int ret;
3396c66ac9dbSNicholas Bellinger 
33970a06d430SChristoph Hellwig 	if (hba->backend->ops->pmode_enable_hba == NULL)
3398c66ac9dbSNicholas Bellinger 		return -EINVAL;
3399c66ac9dbSNicholas Bellinger 
340057103d7fSJingoo Han 	ret = kstrtoul(page, 0, &mode_flag);
3401c66ac9dbSNicholas Bellinger 	if (ret < 0) {
34026708bb27SAndy Grover 		pr_err("Unable to extract hba mode flag: %d\n", ret);
340357103d7fSJingoo Han 		return ret;
3404c66ac9dbSNicholas Bellinger 	}
3405c66ac9dbSNicholas Bellinger 
34060fd97ccfSChristoph Hellwig 	if (hba->dev_count) {
34076708bb27SAndy Grover 		pr_err("Unable to set hba_mode with active devices\n");
3408c66ac9dbSNicholas Bellinger 		return -EINVAL;
3409c66ac9dbSNicholas Bellinger 	}
3410c66ac9dbSNicholas Bellinger 
34110a06d430SChristoph Hellwig 	ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3412c66ac9dbSNicholas Bellinger 	if (ret < 0)
3413c66ac9dbSNicholas Bellinger 		return -EINVAL;
3414c66ac9dbSNicholas Bellinger 	if (ret > 0)
3415c66ac9dbSNicholas Bellinger 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3416c66ac9dbSNicholas Bellinger 	else if (ret == 0)
3417c66ac9dbSNicholas Bellinger 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3418c66ac9dbSNicholas Bellinger 
3419c66ac9dbSNicholas Bellinger 	return count;
3420c66ac9dbSNicholas Bellinger }
3421c66ac9dbSNicholas Bellinger 
34222eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_, hba_info);
34232eafd729SChristoph Hellwig CONFIGFS_ATTR(target_, hba_mode);
3424c66ac9dbSNicholas Bellinger 
34251f6fe7cbSNicholas Bellinger static void target_core_hba_release(struct config_item *item)
34261f6fe7cbSNicholas Bellinger {
34271f6fe7cbSNicholas Bellinger 	struct se_hba *hba = container_of(to_config_group(item),
34281f6fe7cbSNicholas Bellinger 				struct se_hba, hba_group);
34291f6fe7cbSNicholas Bellinger 	core_delete_hba(hba);
34301f6fe7cbSNicholas Bellinger }
34311f6fe7cbSNicholas Bellinger 
3432c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_hba_attrs[] = {
34332eafd729SChristoph Hellwig 	&target_attr_hba_info,
34342eafd729SChristoph Hellwig 	&target_attr_hba_mode,
3435c66ac9dbSNicholas Bellinger 	NULL,
3436c66ac9dbSNicholas Bellinger };
3437c66ac9dbSNicholas Bellinger 
3438c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_hba_item_ops = {
34391f6fe7cbSNicholas Bellinger 	.release		= target_core_hba_release,
3440c66ac9dbSNicholas Bellinger };
3441c66ac9dbSNicholas Bellinger 
3442ece550b5SBhumika Goyal static const struct config_item_type target_core_hba_cit = {
3443c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_hba_item_ops,
3444c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_hba_group_ops,
3445c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_hba_attrs,
3446c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3447c66ac9dbSNicholas Bellinger };
3448c66ac9dbSNicholas Bellinger 
3449c66ac9dbSNicholas Bellinger static struct config_group *target_core_call_addhbatotarget(
3450c66ac9dbSNicholas Bellinger 	struct config_group *group,
3451c66ac9dbSNicholas Bellinger 	const char *name)
3452c66ac9dbSNicholas Bellinger {
3453c66ac9dbSNicholas Bellinger 	char *se_plugin_str, *str, *str2;
3454c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
34552d4e2dafSChaitanya Kulkarni 	char buf[TARGET_CORE_NAME_MAX_LEN] = { };
3456c66ac9dbSNicholas Bellinger 	unsigned long plugin_dep_id = 0;
3457c66ac9dbSNicholas Bellinger 	int ret;
3458c66ac9dbSNicholas Bellinger 
345960d645a4SDan Carpenter 	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
34606708bb27SAndy Grover 		pr_err("Passed *name strlen(): %d exceeds"
3461c66ac9dbSNicholas Bellinger 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3462c66ac9dbSNicholas Bellinger 			TARGET_CORE_NAME_MAX_LEN);
3463c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENAMETOOLONG);
3464c66ac9dbSNicholas Bellinger 	}
3465c66ac9dbSNicholas Bellinger 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3466c66ac9dbSNicholas Bellinger 
3467c66ac9dbSNicholas Bellinger 	str = strstr(buf, "_");
34686708bb27SAndy Grover 	if (!str) {
34696708bb27SAndy Grover 		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3470c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
3471c66ac9dbSNicholas Bellinger 	}
3472c66ac9dbSNicholas Bellinger 	se_plugin_str = buf;
3473c66ac9dbSNicholas Bellinger 	/*
3474c66ac9dbSNicholas Bellinger 	 * Special case for subsystem plugins that have "_" in their names.
3475c66ac9dbSNicholas Bellinger 	 * Namely rd_direct and rd_mcp..
3476c66ac9dbSNicholas Bellinger 	 */
3477c66ac9dbSNicholas Bellinger 	str2 = strstr(str+1, "_");
34786708bb27SAndy Grover 	if (str2) {
3479c66ac9dbSNicholas Bellinger 		*str2 = '\0'; /* Terminate for *se_plugin_str */
3480c66ac9dbSNicholas Bellinger 		str2++; /* Skip to start of plugin dependent ID */
3481c66ac9dbSNicholas Bellinger 		str = str2;
3482c66ac9dbSNicholas Bellinger 	} else {
3483c66ac9dbSNicholas Bellinger 		*str = '\0'; /* Terminate for *se_plugin_str */
3484c66ac9dbSNicholas Bellinger 		str++; /* Skip to start of plugin dependent ID */
3485c66ac9dbSNicholas Bellinger 	}
3486c66ac9dbSNicholas Bellinger 
348757103d7fSJingoo Han 	ret = kstrtoul(str, 0, &plugin_dep_id);
3488c66ac9dbSNicholas Bellinger 	if (ret < 0) {
348957103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
3490c66ac9dbSNicholas Bellinger 				" plugin_dep_id\n", ret);
349157103d7fSJingoo Han 		return ERR_PTR(ret);
3492c66ac9dbSNicholas Bellinger 	}
3493c66ac9dbSNicholas Bellinger 	/*
3494c66ac9dbSNicholas Bellinger 	 * Load up TCM subsystem plugins if they have not already been loaded.
3495c66ac9dbSNicholas Bellinger 	 */
3496dbc5623eSNicholas Bellinger 	transport_subsystem_check_init();
3497c66ac9dbSNicholas Bellinger 
3498c66ac9dbSNicholas Bellinger 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3499c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
3500c66ac9dbSNicholas Bellinger 		return ERR_CAST(hba);
3501c66ac9dbSNicholas Bellinger 
3502c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&hba->hba_group, name,
3503c66ac9dbSNicholas Bellinger 			&target_core_hba_cit);
3504c66ac9dbSNicholas Bellinger 
3505c66ac9dbSNicholas Bellinger 	return &hba->hba_group;
3506c66ac9dbSNicholas Bellinger }
3507c66ac9dbSNicholas Bellinger 
3508c66ac9dbSNicholas Bellinger static void target_core_call_delhbafromtarget(
3509c66ac9dbSNicholas Bellinger 	struct config_group *group,
3510c66ac9dbSNicholas Bellinger 	struct config_item *item)
3511c66ac9dbSNicholas Bellinger {
35121f6fe7cbSNicholas Bellinger 	/*
35131f6fe7cbSNicholas Bellinger 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
35141f6fe7cbSNicholas Bellinger 	 * -> target_core_hba_release()
35151f6fe7cbSNicholas Bellinger 	 */
3516c66ac9dbSNicholas Bellinger 	config_item_put(item);
3517c66ac9dbSNicholas Bellinger }
3518c66ac9dbSNicholas Bellinger 
3519c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_group_ops = {
3520c66ac9dbSNicholas Bellinger 	.make_group	= target_core_call_addhbatotarget,
3521c66ac9dbSNicholas Bellinger 	.drop_item	= target_core_call_delhbafromtarget,
3522c66ac9dbSNicholas Bellinger };
3523c66ac9dbSNicholas Bellinger 
3524ece550b5SBhumika Goyal static const struct config_item_type target_core_cit = {
3525c66ac9dbSNicholas Bellinger 	.ct_item_ops	= NULL,
3526c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_group_ops,
3527c66ac9dbSNicholas Bellinger 	.ct_attrs	= NULL,
3528c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
3529c66ac9dbSNicholas Bellinger };
3530c66ac9dbSNicholas Bellinger 
3531c66ac9dbSNicholas Bellinger /* Stop functions for struct config_item_type target_core_hba_cit */
3532c66ac9dbSNicholas Bellinger 
35330a06d430SChristoph Hellwig void target_setup_backend_cits(struct target_backend *tb)
353473112edcSNicholas Bellinger {
35350a06d430SChristoph Hellwig 	target_core_setup_dev_cit(tb);
35368dc31ff9SMike Christie 	target_core_setup_dev_action_cit(tb);
35370a06d430SChristoph Hellwig 	target_core_setup_dev_attrib_cit(tb);
35380a06d430SChristoph Hellwig 	target_core_setup_dev_pr_cit(tb);
35390a06d430SChristoph Hellwig 	target_core_setup_dev_wwn_cit(tb);
35400a06d430SChristoph Hellwig 	target_core_setup_dev_alua_tg_pt_gps_cit(tb);
35410a06d430SChristoph Hellwig 	target_core_setup_dev_stat_cit(tb);
354273112edcSNicholas Bellinger }
354373112edcSNicholas Bellinger 
354478a6295cSLee Duncan static void target_init_dbroot(void)
354578a6295cSLee Duncan {
354678a6295cSLee Duncan 	struct file *fp;
354778a6295cSLee Duncan 
354878a6295cSLee Duncan 	snprintf(db_root_stage, DB_ROOT_LEN, DB_ROOT_PREFERRED);
354978a6295cSLee Duncan 	fp = filp_open(db_root_stage, O_RDONLY, 0);
355078a6295cSLee Duncan 	if (IS_ERR(fp)) {
355178a6295cSLee Duncan 		pr_err("db_root: cannot open: %s\n", db_root_stage);
355278a6295cSLee Duncan 		return;
355378a6295cSLee Duncan 	}
355478a6295cSLee Duncan 	if (!S_ISDIR(file_inode(fp)->i_mode)) {
355578a6295cSLee Duncan 		filp_close(fp, NULL);
355678a6295cSLee Duncan 		pr_err("db_root: not a valid directory: %s\n", db_root_stage);
355778a6295cSLee Duncan 		return;
355878a6295cSLee Duncan 	}
355978a6295cSLee Duncan 	filp_close(fp, NULL);
356078a6295cSLee Duncan 
356178a6295cSLee Duncan 	strncpy(db_root, db_root_stage, DB_ROOT_LEN);
356278a6295cSLee Duncan 	pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
356378a6295cSLee Duncan }
356478a6295cSLee Duncan 
356554550fabSAxel Lin static int __init target_core_init_configfs(void)
3566c66ac9dbSNicholas Bellinger {
3567d588cf8fSChristoph Hellwig 	struct configfs_subsystem *subsys = &target_core_fabrics;
3568c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
3569c66ac9dbSNicholas Bellinger 	int ret;
3570c66ac9dbSNicholas Bellinger 
35716708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3572c66ac9dbSNicholas Bellinger 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
3573c66ac9dbSNicholas Bellinger 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3574c66ac9dbSNicholas Bellinger 
3575c66ac9dbSNicholas Bellinger 	config_group_init(&subsys->su_group);
3576c66ac9dbSNicholas Bellinger 	mutex_init(&subsys->su_mutex);
3577c66ac9dbSNicholas Bellinger 
3578e3d6f909SAndy Grover 	ret = init_se_kmem_caches();
3579c66ac9dbSNicholas Bellinger 	if (ret < 0)
3580e3d6f909SAndy Grover 		return ret;
3581c66ac9dbSNicholas Bellinger 	/*
3582c66ac9dbSNicholas Bellinger 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3583c66ac9dbSNicholas Bellinger 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3584c66ac9dbSNicholas Bellinger 	 */
35851ae1602dSChristoph Hellwig 	config_group_init_type_name(&target_core_hbagroup, "core",
35861ae1602dSChristoph Hellwig 			&target_core_cit);
35871ae1602dSChristoph Hellwig 	configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
3588c66ac9dbSNicholas Bellinger 
3589c66ac9dbSNicholas Bellinger 	/*
3590c66ac9dbSNicholas Bellinger 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3591c66ac9dbSNicholas Bellinger 	 */
35921ae1602dSChristoph Hellwig 	config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
35931ae1602dSChristoph Hellwig 	configfs_add_default_group(&alua_group, &target_core_hbagroup);
35941ae1602dSChristoph Hellwig 
3595c66ac9dbSNicholas Bellinger 	/*
3596c66ac9dbSNicholas Bellinger 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3597c66ac9dbSNicholas Bellinger 	 * groups under /sys/kernel/config/target/core/alua/
3598c66ac9dbSNicholas Bellinger 	 */
35991ae1602dSChristoph Hellwig 	config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
36001ae1602dSChristoph Hellwig 			&target_core_alua_lu_gps_cit);
36011ae1602dSChristoph Hellwig 	configfs_add_default_group(&alua_lu_gps_group, &alua_group);
3602c66ac9dbSNicholas Bellinger 
3603c66ac9dbSNicholas Bellinger 	/*
3604c66ac9dbSNicholas Bellinger 	 * Add core/alua/lu_gps/default_lu_gp
3605c66ac9dbSNicholas Bellinger 	 */
3606c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
360737bb7899SPeter Senna Tschudin 	if (IS_ERR(lu_gp)) {
360837bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3609c66ac9dbSNicholas Bellinger 		goto out_global;
361037bb7899SPeter Senna Tschudin 	}
3611c66ac9dbSNicholas Bellinger 
3612c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3613c66ac9dbSNicholas Bellinger 				&target_core_alua_lu_gp_cit);
36141ae1602dSChristoph Hellwig 	configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
36151ae1602dSChristoph Hellwig 
3616e3d6f909SAndy Grover 	default_lu_gp = lu_gp;
36171ae1602dSChristoph Hellwig 
3618c66ac9dbSNicholas Bellinger 	/*
3619c66ac9dbSNicholas Bellinger 	 * Register the target_core_mod subsystem with configfs.
3620c66ac9dbSNicholas Bellinger 	 */
3621c66ac9dbSNicholas Bellinger 	ret = configfs_register_subsystem(subsys);
3622c66ac9dbSNicholas Bellinger 	if (ret < 0) {
36236708bb27SAndy Grover 		pr_err("Error %d while registering subsystem %s\n",
3624c66ac9dbSNicholas Bellinger 			ret, subsys->su_group.cg_item.ci_namebuf);
3625c66ac9dbSNicholas Bellinger 		goto out_global;
3626c66ac9dbSNicholas Bellinger 	}
36276708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3628ce8dd25dSChristoph Hellwig 		" Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3629c66ac9dbSNicholas Bellinger 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3630c66ac9dbSNicholas Bellinger 	/*
3631c66ac9dbSNicholas Bellinger 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3632c66ac9dbSNicholas Bellinger 	 */
3633c66ac9dbSNicholas Bellinger 	ret = rd_module_init();
3634c66ac9dbSNicholas Bellinger 	if (ret < 0)
3635c66ac9dbSNicholas Bellinger 		goto out;
3636c66ac9dbSNicholas Bellinger 
36370d0f9dfbSRoland Dreier 	ret = core_dev_setup_virtual_lun0();
36380d0f9dfbSRoland Dreier 	if (ret < 0)
3639c66ac9dbSNicholas Bellinger 		goto out;
3640c66ac9dbSNicholas Bellinger 
3641f99715acSNicholas Bellinger 	ret = target_xcopy_setup_pt();
3642f99715acSNicholas Bellinger 	if (ret < 0)
3643f99715acSNicholas Bellinger 		goto out;
3644f99715acSNicholas Bellinger 
364578a6295cSLee Duncan 	target_init_dbroot();
364678a6295cSLee Duncan 
3647c66ac9dbSNicholas Bellinger 	return 0;
3648c66ac9dbSNicholas Bellinger 
3649c66ac9dbSNicholas Bellinger out:
3650c66ac9dbSNicholas Bellinger 	configfs_unregister_subsystem(subsys);
3651c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3652c66ac9dbSNicholas Bellinger 	rd_module_exit();
3653c66ac9dbSNicholas Bellinger out_global:
3654e3d6f909SAndy Grover 	if (default_lu_gp) {
3655e3d6f909SAndy Grover 		core_alua_free_lu_gp(default_lu_gp);
3656e3d6f909SAndy Grover 		default_lu_gp = NULL;
3657c66ac9dbSNicholas Bellinger 	}
3658e3d6f909SAndy Grover 	release_se_kmem_caches();
3659e3d6f909SAndy Grover 	return ret;
3660c66ac9dbSNicholas Bellinger }
3661c66ac9dbSNicholas Bellinger 
366254550fabSAxel Lin static void __exit target_core_exit_configfs(void)
3663c66ac9dbSNicholas Bellinger {
36641ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&alua_lu_gps_group);
36651ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&alua_group);
36661ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&target_core_hbagroup);
3667c66ac9dbSNicholas Bellinger 
36687c2bf6e9SNicholas Bellinger 	/*
36697c2bf6e9SNicholas Bellinger 	 * We expect subsys->su_group.default_groups to be released
36707c2bf6e9SNicholas Bellinger 	 * by configfs subsystem provider logic..
36717c2bf6e9SNicholas Bellinger 	 */
3672d588cf8fSChristoph Hellwig 	configfs_unregister_subsystem(&target_core_fabrics);
3673c66ac9dbSNicholas Bellinger 
3674e3d6f909SAndy Grover 	core_alua_free_lu_gp(default_lu_gp);
3675e3d6f909SAndy Grover 	default_lu_gp = NULL;
36767c2bf6e9SNicholas Bellinger 
36776708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3678c66ac9dbSNicholas Bellinger 			" Infrastructure\n");
3679c66ac9dbSNicholas Bellinger 
3680c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3681c66ac9dbSNicholas Bellinger 	rd_module_exit();
3682f99715acSNicholas Bellinger 	target_xcopy_release_pt();
3683e3d6f909SAndy Grover 	release_se_kmem_caches();
3684c66ac9dbSNicholas Bellinger }
3685c66ac9dbSNicholas Bellinger 
3686c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3687c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3688c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
3689c66ac9dbSNicholas Bellinger 
3690c66ac9dbSNicholas Bellinger module_init(target_core_init_configfs);
3691c66ac9dbSNicholas Bellinger module_exit(target_core_exit_configfs);
3692