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 
15*e56ca6bcSChristophe JAILLET #include <linux/kstrtox.h>
16c66ac9dbSNicholas Bellinger #include <linux/module.h>
17c66ac9dbSNicholas Bellinger #include <linux/moduleparam.h>
18c66ac9dbSNicholas Bellinger #include <generated/utsrelease.h>
19c66ac9dbSNicholas Bellinger #include <linux/utsname.h>
20c66ac9dbSNicholas Bellinger #include <linux/init.h>
21c66ac9dbSNicholas Bellinger #include <linux/fs.h>
22c66ac9dbSNicholas Bellinger #include <linux/namei.h>
23c66ac9dbSNicholas Bellinger #include <linux/slab.h>
24c66ac9dbSNicholas Bellinger #include <linux/types.h>
25c66ac9dbSNicholas Bellinger #include <linux/delay.h>
26c66ac9dbSNicholas Bellinger #include <linux/unistd.h>
27c66ac9dbSNicholas Bellinger #include <linux/string.h>
28c66ac9dbSNicholas Bellinger #include <linux/parser.h>
29c66ac9dbSNicholas Bellinger #include <linux/syscalls.h>
30c66ac9dbSNicholas Bellinger #include <linux/configfs.h>
31e3d6f909SAndy Grover #include <linux/spinlock.h>
32c66ac9dbSNicholas Bellinger 
33c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
34c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
35c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
36c66ac9dbSNicholas Bellinger 
37e26d99aeSChristoph Hellwig #include "target_core_internal.h"
38c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
39c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
40c66ac9dbSNicholas Bellinger #include "target_core_rd.h"
41f99715acSNicholas Bellinger #include "target_core_xcopy.h"
42c66ac9dbSNicholas Bellinger 
4373112edcSNicholas Bellinger #define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs)		\
440a06d430SChristoph Hellwig static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
4573112edcSNicholas Bellinger {									\
460a06d430SChristoph Hellwig 	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
4773112edcSNicholas Bellinger 									\
4873112edcSNicholas Bellinger 	cit->ct_item_ops = _item_ops;					\
4973112edcSNicholas Bellinger 	cit->ct_group_ops = _group_ops;					\
5073112edcSNicholas Bellinger 	cit->ct_attrs = _attrs;						\
510a06d430SChristoph Hellwig 	cit->ct_owner = tb->ops->owner;					\
520a06d430SChristoph Hellwig 	pr_debug("Setup generic %s\n", __stringify(_name));		\
530a06d430SChristoph Hellwig }
540a06d430SChristoph Hellwig 
550a06d430SChristoph Hellwig #define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops)			\
560a06d430SChristoph Hellwig static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
570a06d430SChristoph Hellwig {									\
580a06d430SChristoph Hellwig 	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
590a06d430SChristoph Hellwig 									\
600a06d430SChristoph Hellwig 	cit->ct_item_ops = _item_ops;					\
610a06d430SChristoph Hellwig 	cit->ct_group_ops = _group_ops;					\
620a06d430SChristoph Hellwig 	cit->ct_attrs = tb->ops->tb_##_name##_attrs;			\
630a06d430SChristoph Hellwig 	cit->ct_owner = tb->ops->owner;					\
6473112edcSNicholas Bellinger 	pr_debug("Setup generic %s\n", __stringify(_name));		\
6573112edcSNicholas Bellinger }
6673112edcSNicholas Bellinger 
67e3d6f909SAndy Grover extern struct t10_alua_lu_gp *default_lu_gp;
68e3d6f909SAndy Grover 
69d0f474e5SRoland Dreier static LIST_HEAD(g_tf_list);
70d0f474e5SRoland Dreier static DEFINE_MUTEX(g_tf_lock);
71c66ac9dbSNicholas Bellinger 
72e3d6f909SAndy Grover static struct config_group target_core_hbagroup;
73e3d6f909SAndy Grover static struct config_group alua_group;
74e3d6f909SAndy Grover static struct config_group alua_lu_gps_group;
75e3d6f909SAndy Grover 
7680890c5eSMaurizio Lombardi static unsigned int target_devices;
7780890c5eSMaurizio Lombardi static DEFINE_MUTEX(target_devices_lock);
7880890c5eSMaurizio Lombardi 
79c66ac9dbSNicholas Bellinger static inline struct se_hba *
80c66ac9dbSNicholas Bellinger item_to_hba(struct config_item *item)
81c66ac9dbSNicholas Bellinger {
82c66ac9dbSNicholas Bellinger 	return container_of(to_config_group(item), struct se_hba, hba_group);
83c66ac9dbSNicholas Bellinger }
84c66ac9dbSNicholas Bellinger 
85c66ac9dbSNicholas Bellinger /*
86c66ac9dbSNicholas Bellinger  * Attributes for /sys/kernel/config/target/
87c66ac9dbSNicholas Bellinger  */
882eafd729SChristoph Hellwig static ssize_t target_core_item_version_show(struct config_item *item,
89c66ac9dbSNicholas Bellinger 		char *page)
90c66ac9dbSNicholas Bellinger {
91c66ac9dbSNicholas Bellinger 	return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
92ce8dd25dSChristoph Hellwig 		" on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
93c66ac9dbSNicholas Bellinger 		utsname()->sysname, utsname()->machine);
94c66ac9dbSNicholas Bellinger }
95c66ac9dbSNicholas Bellinger 
962eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_core_item_, version);
97c66ac9dbSNicholas Bellinger 
98a96e9783SLee Duncan char db_root[DB_ROOT_LEN] = DB_ROOT_DEFAULT;
99a96e9783SLee Duncan static char db_root_stage[DB_ROOT_LEN];
100a96e9783SLee Duncan 
101a96e9783SLee Duncan static ssize_t target_core_item_dbroot_show(struct config_item *item,
102a96e9783SLee Duncan 					    char *page)
103a96e9783SLee Duncan {
104a96e9783SLee Duncan 	return sprintf(page, "%s\n", db_root);
105a96e9783SLee Duncan }
106a96e9783SLee Duncan 
107a96e9783SLee Duncan static ssize_t target_core_item_dbroot_store(struct config_item *item,
108a96e9783SLee Duncan 					const char *page, size_t count)
109a96e9783SLee Duncan {
110a96e9783SLee Duncan 	ssize_t read_bytes;
111a96e9783SLee Duncan 	struct file *fp;
11280890c5eSMaurizio Lombardi 	ssize_t r = -EINVAL;
113a96e9783SLee Duncan 
11480890c5eSMaurizio Lombardi 	mutex_lock(&target_devices_lock);
11580890c5eSMaurizio Lombardi 	if (target_devices) {
11680890c5eSMaurizio Lombardi 		pr_err("db_root: cannot be changed because it's in use\n");
11780890c5eSMaurizio Lombardi 		goto unlock;
118a96e9783SLee Duncan 	}
119a96e9783SLee Duncan 
120a96e9783SLee Duncan 	if (count > (DB_ROOT_LEN - 1)) {
121a96e9783SLee Duncan 		pr_err("db_root: count %d exceeds DB_ROOT_LEN-1: %u\n",
122a96e9783SLee Duncan 		       (int)count, DB_ROOT_LEN - 1);
12380890c5eSMaurizio Lombardi 		goto unlock;
124a96e9783SLee Duncan 	}
125a96e9783SLee Duncan 
126a96e9783SLee Duncan 	read_bytes = snprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
12780890c5eSMaurizio Lombardi 	if (!read_bytes)
12880890c5eSMaurizio Lombardi 		goto unlock;
12980890c5eSMaurizio Lombardi 
130a96e9783SLee Duncan 	if (db_root_stage[read_bytes - 1] == '\n')
131a96e9783SLee Duncan 		db_root_stage[read_bytes - 1] = '\0';
132a96e9783SLee Duncan 
133a96e9783SLee Duncan 	/* validate new db root before accepting it */
134a96e9783SLee Duncan 	fp = filp_open(db_root_stage, O_RDONLY, 0);
135a96e9783SLee Duncan 	if (IS_ERR(fp)) {
136a96e9783SLee Duncan 		pr_err("db_root: cannot open: %s\n", db_root_stage);
13780890c5eSMaurizio Lombardi 		goto unlock;
138a96e9783SLee Duncan 	}
13945063097SAl Viro 	if (!S_ISDIR(file_inode(fp)->i_mode)) {
1408cc3bb07SBart Van Assche 		filp_close(fp, NULL);
141a96e9783SLee Duncan 		pr_err("db_root: not a directory: %s\n", db_root_stage);
14280890c5eSMaurizio Lombardi 		goto unlock;
143a96e9783SLee Duncan 	}
1448cc3bb07SBart Van Assche 	filp_close(fp, NULL);
145a96e9783SLee Duncan 
146a96e9783SLee Duncan 	strncpy(db_root, db_root_stage, read_bytes);
14778a6295cSLee Duncan 	pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
14878a6295cSLee Duncan 
14980890c5eSMaurizio Lombardi 	r = read_bytes;
15080890c5eSMaurizio Lombardi 
15180890c5eSMaurizio Lombardi unlock:
15280890c5eSMaurizio Lombardi 	mutex_unlock(&target_devices_lock);
15380890c5eSMaurizio Lombardi 	return r;
154a96e9783SLee Duncan }
155a96e9783SLee Duncan 
156a96e9783SLee Duncan CONFIGFS_ATTR(target_core_item_, dbroot);
157a96e9783SLee Duncan 
158c66ac9dbSNicholas Bellinger static struct target_fabric_configfs *target_core_get_fabric(
159c66ac9dbSNicholas Bellinger 	const char *name)
160c66ac9dbSNicholas Bellinger {
161c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
162c66ac9dbSNicholas Bellinger 
1636708bb27SAndy Grover 	if (!name)
164c66ac9dbSNicholas Bellinger 		return NULL;
165c66ac9dbSNicholas Bellinger 
166c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
167c66ac9dbSNicholas Bellinger 	list_for_each_entry(tf, &g_tf_list, tf_list) {
16859a206b4SDavid Disseldorp 		const char *cmp_name = tf->tf_ops->fabric_alias;
16959a206b4SDavid Disseldorp 		if (!cmp_name)
17059a206b4SDavid Disseldorp 			cmp_name = tf->tf_ops->fabric_name;
17159a206b4SDavid Disseldorp 		if (!strcmp(cmp_name, name)) {
172c66ac9dbSNicholas Bellinger 			atomic_inc(&tf->tf_access_cnt);
173c66ac9dbSNicholas Bellinger 			mutex_unlock(&g_tf_lock);
174c66ac9dbSNicholas Bellinger 			return tf;
175c66ac9dbSNicholas Bellinger 		}
176c66ac9dbSNicholas Bellinger 	}
177c66ac9dbSNicholas Bellinger 	mutex_unlock(&g_tf_lock);
178c66ac9dbSNicholas Bellinger 
179c66ac9dbSNicholas Bellinger 	return NULL;
180c66ac9dbSNicholas Bellinger }
181c66ac9dbSNicholas Bellinger 
182c66ac9dbSNicholas Bellinger /*
183c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->make_group()
184c66ac9dbSNicholas Bellinger  */
185c66ac9dbSNicholas Bellinger static struct config_group *target_core_register_fabric(
186c66ac9dbSNicholas Bellinger 	struct config_group *group,
187c66ac9dbSNicholas Bellinger 	const char *name)
188c66ac9dbSNicholas Bellinger {
189c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
190c66ac9dbSNicholas Bellinger 	int ret;
191c66ac9dbSNicholas Bellinger 
1926708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
193c66ac9dbSNicholas Bellinger 			" %s\n", group, name);
194e7b7af6eSRoland Dreier 
195e7b7af6eSRoland Dreier 	tf = target_core_get_fabric(name);
196e7b7af6eSRoland Dreier 	if (!tf) {
19762554910SNicholas Bellinger 		pr_debug("target_core_register_fabric() trying autoload for %s\n",
198e7b7af6eSRoland Dreier 			 name);
199e7b7af6eSRoland Dreier 
200c66ac9dbSNicholas Bellinger 		/*
201c66ac9dbSNicholas Bellinger 		 * Below are some hardcoded request_module() calls to automatically
202c66ac9dbSNicholas Bellinger 		 * local fabric modules when the following is called:
203c66ac9dbSNicholas Bellinger 		 *
204c66ac9dbSNicholas Bellinger 		 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
205c66ac9dbSNicholas Bellinger 		 *
206c66ac9dbSNicholas Bellinger 		 * Note that this does not limit which TCM fabric module can be
207c66ac9dbSNicholas Bellinger 		 * registered, but simply provids auto loading logic for modules with
208c66ac9dbSNicholas Bellinger 		 * mkdir(2) system calls with known TCM fabric modules.
209c66ac9dbSNicholas Bellinger 		 */
210e7b7af6eSRoland Dreier 
2116708bb27SAndy Grover 		if (!strncmp(name, "iscsi", 5)) {
212c66ac9dbSNicholas Bellinger 			/*
213c66ac9dbSNicholas Bellinger 			 * Automatically load the LIO Target fabric module when the
214c66ac9dbSNicholas Bellinger 			 * following is called:
215c66ac9dbSNicholas Bellinger 			 *
216c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/iscsi
217c66ac9dbSNicholas Bellinger 			 */
218c66ac9dbSNicholas Bellinger 			ret = request_module("iscsi_target_mod");
219c66ac9dbSNicholas Bellinger 			if (ret < 0) {
22062554910SNicholas Bellinger 				pr_debug("request_module() failed for"
221c66ac9dbSNicholas Bellinger 				         " iscsi_target_mod.ko: %d\n", ret);
222c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
223c66ac9dbSNicholas Bellinger 			}
2246708bb27SAndy Grover 		} else if (!strncmp(name, "loopback", 8)) {
225c66ac9dbSNicholas Bellinger 			/*
226c66ac9dbSNicholas Bellinger 			 * Automatically load the tcm_loop fabric module when the
227c66ac9dbSNicholas Bellinger 			 * following is called:
228c66ac9dbSNicholas Bellinger 			 *
229c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/loopback
230c66ac9dbSNicholas Bellinger 			 */
231c66ac9dbSNicholas Bellinger 			ret = request_module("tcm_loop");
232c66ac9dbSNicholas Bellinger 			if (ret < 0) {
23362554910SNicholas Bellinger 				pr_debug("request_module() failed for"
234c66ac9dbSNicholas Bellinger 				         " tcm_loop.ko: %d\n", ret);
235c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
236c66ac9dbSNicholas Bellinger 			}
237c66ac9dbSNicholas Bellinger 		}
238c66ac9dbSNicholas Bellinger 
239c66ac9dbSNicholas Bellinger 		tf = target_core_get_fabric(name);
240e7b7af6eSRoland Dreier 	}
241e7b7af6eSRoland Dreier 
2426708bb27SAndy Grover 	if (!tf) {
24362554910SNicholas Bellinger 		pr_debug("target_core_get_fabric() failed for %s\n",
244c66ac9dbSNicholas Bellinger 		         name);
245c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
246c66ac9dbSNicholas Bellinger 	}
2476708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
24859a206b4SDavid Disseldorp 			" %s\n", tf->tf_ops->fabric_name);
249c66ac9dbSNicholas Bellinger 	/*
250c66ac9dbSNicholas Bellinger 	 * On a successful target_core_get_fabric() look, the returned
251c66ac9dbSNicholas Bellinger 	 * struct target_fabric_configfs *tf will contain a usage reference.
252c66ac9dbSNicholas Bellinger 	 */
2536708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
254968ebe75SChristoph Hellwig 			&tf->tf_wwn_cit);
255c66ac9dbSNicholas Bellinger 
256968ebe75SChristoph Hellwig 	config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
2571ae1602dSChristoph Hellwig 
258c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
259968ebe75SChristoph Hellwig 			&tf->tf_discovery_cit);
2601ae1602dSChristoph Hellwig 	configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
261c66ac9dbSNicholas Bellinger 
2626f3bf5a2SBart Van Assche 	pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric: %s\n",
2636f3bf5a2SBart Van Assche 		 config_item_name(&tf->tf_group.cg_item));
264c66ac9dbSNicholas Bellinger 	return &tf->tf_group;
265c66ac9dbSNicholas Bellinger }
266c66ac9dbSNicholas Bellinger 
267c66ac9dbSNicholas Bellinger /*
268c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->drop_item()
269c66ac9dbSNicholas Bellinger  */
270c66ac9dbSNicholas Bellinger static void target_core_deregister_fabric(
271c66ac9dbSNicholas Bellinger 	struct config_group *group,
272c66ac9dbSNicholas Bellinger 	struct config_item *item)
273c66ac9dbSNicholas Bellinger {
274c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf = container_of(
275c66ac9dbSNicholas Bellinger 		to_config_group(item), struct target_fabric_configfs, tf_group);
276c66ac9dbSNicholas Bellinger 
2776708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
278c66ac9dbSNicholas Bellinger 		" tf list\n", config_item_name(item));
279c66ac9dbSNicholas Bellinger 
2806708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
28159a206b4SDavid Disseldorp 			" %s\n", tf->tf_ops->fabric_name);
282c66ac9dbSNicholas Bellinger 	atomic_dec(&tf->tf_access_cnt);
283c66ac9dbSNicholas Bellinger 
2846708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
285c66ac9dbSNicholas Bellinger 			" %s\n", config_item_name(item));
286c66ac9dbSNicholas Bellinger 
2871ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&tf->tf_group);
288c66ac9dbSNicholas Bellinger 	config_item_put(item);
289c66ac9dbSNicholas Bellinger }
290c66ac9dbSNicholas Bellinger 
291c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_fabric_group_ops = {
292c66ac9dbSNicholas Bellinger 	.make_group	= &target_core_register_fabric,
293c66ac9dbSNicholas Bellinger 	.drop_item	= &target_core_deregister_fabric,
294c66ac9dbSNicholas Bellinger };
295c66ac9dbSNicholas Bellinger 
296c66ac9dbSNicholas Bellinger /*
297c66ac9dbSNicholas Bellinger  * All item attributes appearing in /sys/kernel/target/ appear here.
298c66ac9dbSNicholas Bellinger  */
299c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_fabric_item_attrs[] = {
300c66ac9dbSNicholas Bellinger 	&target_core_item_attr_version,
301a96e9783SLee Duncan 	&target_core_item_attr_dbroot,
302c66ac9dbSNicholas Bellinger 	NULL,
303c66ac9dbSNicholas Bellinger };
304c66ac9dbSNicholas Bellinger 
305c66ac9dbSNicholas Bellinger /*
306c66ac9dbSNicholas Bellinger  * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
307c66ac9dbSNicholas Bellinger  */
308ece550b5SBhumika Goyal static const struct config_item_type target_core_fabrics_item = {
309c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_fabric_group_ops,
310c66ac9dbSNicholas Bellinger 	.ct_attrs	= target_core_fabric_item_attrs,
311c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
312c66ac9dbSNicholas Bellinger };
313c66ac9dbSNicholas Bellinger 
314c66ac9dbSNicholas Bellinger static struct configfs_subsystem target_core_fabrics = {
315c66ac9dbSNicholas Bellinger 	.su_group = {
316c66ac9dbSNicholas Bellinger 		.cg_item = {
317c66ac9dbSNicholas Bellinger 			.ci_namebuf = "target",
318c66ac9dbSNicholas Bellinger 			.ci_type = &target_core_fabrics_item,
319c66ac9dbSNicholas Bellinger 		},
320c66ac9dbSNicholas Bellinger 	},
321c66ac9dbSNicholas Bellinger };
322c66ac9dbSNicholas Bellinger 
323d588cf8fSChristoph Hellwig int target_depend_item(struct config_item *item)
324d588cf8fSChristoph Hellwig {
325d588cf8fSChristoph Hellwig 	return configfs_depend_item(&target_core_fabrics, item);
326d588cf8fSChristoph Hellwig }
327d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_depend_item);
328d588cf8fSChristoph Hellwig 
329d588cf8fSChristoph Hellwig void target_undepend_item(struct config_item *item)
330d588cf8fSChristoph Hellwig {
3319a9e3415SKrzysztof Opasiak 	return configfs_undepend_item(item);
332d588cf8fSChristoph Hellwig }
333d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_undepend_item);
334c66ac9dbSNicholas Bellinger 
335c66ac9dbSNicholas Bellinger /*##############################################################################
336c66ac9dbSNicholas Bellinger // Start functions called by external Target Fabrics Modules
337c66ac9dbSNicholas Bellinger //############################################################################*/
338c66ac9dbSNicholas Bellinger 
3399ac8928eSChristoph Hellwig static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
340c66ac9dbSNicholas Bellinger {
34159a206b4SDavid Disseldorp 	if (tfo->fabric_alias) {
34259a206b4SDavid Disseldorp 		if (strlen(tfo->fabric_alias) >= TARGET_FABRIC_NAME_SIZE) {
34359a206b4SDavid Disseldorp 			pr_err("Passed alias: %s exceeds "
34459a206b4SDavid Disseldorp 				"TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_alias);
3459ac8928eSChristoph Hellwig 			return -EINVAL;
346c66ac9dbSNicholas Bellinger 		}
347c66ac9dbSNicholas Bellinger 	}
34830c7ca93SDavid Disseldorp 	if (!tfo->fabric_name) {
34930c7ca93SDavid Disseldorp 		pr_err("Missing tfo->fabric_name\n");
350c66ac9dbSNicholas Bellinger 		return -EINVAL;
351c66ac9dbSNicholas Bellinger 	}
35259a206b4SDavid Disseldorp 	if (strlen(tfo->fabric_name) >= TARGET_FABRIC_NAME_SIZE) {
35359a206b4SDavid Disseldorp 		pr_err("Passed name: %s exceeds "
35459a206b4SDavid Disseldorp 			"TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_name);
35559a206b4SDavid Disseldorp 		return -EINVAL;
35659a206b4SDavid Disseldorp 	}
3576708bb27SAndy Grover 	if (!tfo->tpg_get_wwn) {
3586708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_wwn()\n");
359c66ac9dbSNicholas Bellinger 		return -EINVAL;
360c66ac9dbSNicholas Bellinger 	}
3616708bb27SAndy Grover 	if (!tfo->tpg_get_tag) {
3626708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_tag()\n");
363c66ac9dbSNicholas Bellinger 		return -EINVAL;
364c66ac9dbSNicholas Bellinger 	}
3656708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode) {
3666708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode()\n");
367c66ac9dbSNicholas Bellinger 		return -EINVAL;
368c66ac9dbSNicholas Bellinger 	}
3696708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_cache) {
3706708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
371c66ac9dbSNicholas Bellinger 		return -EINVAL;
372c66ac9dbSNicholas Bellinger 	}
3736708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_write_protect) {
3746708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
375c66ac9dbSNicholas Bellinger 		return -EINVAL;
376c66ac9dbSNicholas Bellinger 	}
3776708bb27SAndy Grover 	if (!tfo->tpg_check_prod_mode_write_protect) {
3786708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
379c66ac9dbSNicholas Bellinger 		return -EINVAL;
380c66ac9dbSNicholas Bellinger 	}
3816708bb27SAndy Grover 	if (!tfo->tpg_get_inst_index) {
3826708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_inst_index()\n");
383c66ac9dbSNicholas Bellinger 		return -EINVAL;
384c66ac9dbSNicholas Bellinger 	}
38535462975SChristoph Hellwig 	if (!tfo->release_cmd) {
3866708bb27SAndy Grover 		pr_err("Missing tfo->release_cmd()\n");
387c66ac9dbSNicholas Bellinger 		return -EINVAL;
388c66ac9dbSNicholas Bellinger 	}
3896708bb27SAndy Grover 	if (!tfo->sess_get_index) {
3906708bb27SAndy Grover 		pr_err("Missing tfo->sess_get_index()\n");
391c66ac9dbSNicholas Bellinger 		return -EINVAL;
392c66ac9dbSNicholas Bellinger 	}
3936708bb27SAndy Grover 	if (!tfo->write_pending) {
3946708bb27SAndy Grover 		pr_err("Missing tfo->write_pending()\n");
395c66ac9dbSNicholas Bellinger 		return -EINVAL;
396c66ac9dbSNicholas Bellinger 	}
3976708bb27SAndy Grover 	if (!tfo->set_default_node_attributes) {
3986708bb27SAndy Grover 		pr_err("Missing tfo->set_default_node_attributes()\n");
399c66ac9dbSNicholas Bellinger 		return -EINVAL;
400c66ac9dbSNicholas Bellinger 	}
4016708bb27SAndy Grover 	if (!tfo->get_cmd_state) {
4026708bb27SAndy Grover 		pr_err("Missing tfo->get_cmd_state()\n");
403c66ac9dbSNicholas Bellinger 		return -EINVAL;
404c66ac9dbSNicholas Bellinger 	}
4056708bb27SAndy Grover 	if (!tfo->queue_data_in) {
4066708bb27SAndy Grover 		pr_err("Missing tfo->queue_data_in()\n");
407c66ac9dbSNicholas Bellinger 		return -EINVAL;
408c66ac9dbSNicholas Bellinger 	}
4096708bb27SAndy Grover 	if (!tfo->queue_status) {
4106708bb27SAndy Grover 		pr_err("Missing tfo->queue_status()\n");
411c66ac9dbSNicholas Bellinger 		return -EINVAL;
412c66ac9dbSNicholas Bellinger 	}
4136708bb27SAndy Grover 	if (!tfo->queue_tm_rsp) {
4146708bb27SAndy Grover 		pr_err("Missing tfo->queue_tm_rsp()\n");
415c66ac9dbSNicholas Bellinger 		return -EINVAL;
416c66ac9dbSNicholas Bellinger 	}
417131e6abcSNicholas Bellinger 	if (!tfo->aborted_task) {
418131e6abcSNicholas Bellinger 		pr_err("Missing tfo->aborted_task()\n");
419131e6abcSNicholas Bellinger 		return -EINVAL;
420131e6abcSNicholas Bellinger 	}
4219c28ca4fSNicholas Bellinger 	if (!tfo->check_stop_free) {
4229c28ca4fSNicholas Bellinger 		pr_err("Missing tfo->check_stop_free()\n");
4239c28ca4fSNicholas Bellinger 		return -EINVAL;
4249c28ca4fSNicholas Bellinger 	}
425c66ac9dbSNicholas Bellinger 	/*
426c66ac9dbSNicholas Bellinger 	 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
427c66ac9dbSNicholas Bellinger 	 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
428c66ac9dbSNicholas Bellinger 	 * target_core_fabric_configfs.c WWN+TPG group context code.
429c66ac9dbSNicholas Bellinger 	 */
4306708bb27SAndy Grover 	if (!tfo->fabric_make_wwn) {
4316708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_wwn()\n");
432c66ac9dbSNicholas Bellinger 		return -EINVAL;
433c66ac9dbSNicholas Bellinger 	}
4346708bb27SAndy Grover 	if (!tfo->fabric_drop_wwn) {
4356708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_wwn()\n");
436c66ac9dbSNicholas Bellinger 		return -EINVAL;
437c66ac9dbSNicholas Bellinger 	}
4386708bb27SAndy Grover 	if (!tfo->fabric_make_tpg) {
4396708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_tpg()\n");
440c66ac9dbSNicholas Bellinger 		return -EINVAL;
441c66ac9dbSNicholas Bellinger 	}
4426708bb27SAndy Grover 	if (!tfo->fabric_drop_tpg) {
4436708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_tpg()\n");
444c66ac9dbSNicholas Bellinger 		return -EINVAL;
445c66ac9dbSNicholas Bellinger 	}
446c66ac9dbSNicholas Bellinger 
447c66ac9dbSNicholas Bellinger 	return 0;
448c66ac9dbSNicholas Bellinger }
449c66ac9dbSNicholas Bellinger 
4509ac8928eSChristoph Hellwig int target_register_template(const struct target_core_fabric_ops *fo)
451c66ac9dbSNicholas Bellinger {
4529ac8928eSChristoph Hellwig 	struct target_fabric_configfs *tf;
453c66ac9dbSNicholas Bellinger 	int ret;
454c66ac9dbSNicholas Bellinger 
4559ac8928eSChristoph Hellwig 	ret = target_fabric_tf_ops_check(fo);
4569ac8928eSChristoph Hellwig 	if (ret)
457c66ac9dbSNicholas Bellinger 		return ret;
458c66ac9dbSNicholas Bellinger 
4599ac8928eSChristoph Hellwig 	tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
4609ac8928eSChristoph Hellwig 	if (!tf) {
4619ac8928eSChristoph Hellwig 		pr_err("%s: could not allocate memory!\n", __func__);
4629ac8928eSChristoph Hellwig 		return -ENOMEM;
4639ac8928eSChristoph Hellwig 	}
4649ac8928eSChristoph Hellwig 
4659ac8928eSChristoph Hellwig 	INIT_LIST_HEAD(&tf->tf_list);
4669ac8928eSChristoph Hellwig 	atomic_set(&tf->tf_access_cnt, 0);
467ef0caf8dSChristoph Hellwig 	tf->tf_ops = fo;
4689ac8928eSChristoph Hellwig 	target_fabric_setup_cits(tf);
4699ac8928eSChristoph Hellwig 
4709ac8928eSChristoph Hellwig 	mutex_lock(&g_tf_lock);
4719ac8928eSChristoph Hellwig 	list_add_tail(&tf->tf_list, &g_tf_list);
4729ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4739ac8928eSChristoph Hellwig 
474c66ac9dbSNicholas Bellinger 	return 0;
475c66ac9dbSNicholas Bellinger }
4769ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_register_template);
477c66ac9dbSNicholas Bellinger 
4789ac8928eSChristoph Hellwig void target_unregister_template(const struct target_core_fabric_ops *fo)
479c66ac9dbSNicholas Bellinger {
4809ac8928eSChristoph Hellwig 	struct target_fabric_configfs *t;
481c66ac9dbSNicholas Bellinger 
482c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
4839ac8928eSChristoph Hellwig 	list_for_each_entry(t, &g_tf_list, tf_list) {
48459a206b4SDavid Disseldorp 		if (!strcmp(t->tf_ops->fabric_name, fo->fabric_name)) {
4859ac8928eSChristoph Hellwig 			BUG_ON(atomic_read(&t->tf_access_cnt));
4869ac8928eSChristoph Hellwig 			list_del(&t->tf_list);
48794509182SNicholas Bellinger 			mutex_unlock(&g_tf_lock);
48894509182SNicholas Bellinger 			/*
48994509182SNicholas Bellinger 			 * Wait for any outstanding fabric se_deve_entry->rcu_head
49094509182SNicholas Bellinger 			 * callbacks to complete post kfree_rcu(), before allowing
49194509182SNicholas Bellinger 			 * fabric driver unload of TFO->module to proceed.
49294509182SNicholas Bellinger 			 */
49394509182SNicholas Bellinger 			rcu_barrier();
49480ed33c8SDmitry Bogdanov 			kfree(t->tf_tpg_base_cit.ct_attrs);
4959ac8928eSChristoph Hellwig 			kfree(t);
49694509182SNicholas Bellinger 			return;
497c66ac9dbSNicholas Bellinger 		}
498c66ac9dbSNicholas Bellinger 	}
4999ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
5009ac8928eSChristoph Hellwig }
5019ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_unregister_template);
502c66ac9dbSNicholas Bellinger 
503c66ac9dbSNicholas Bellinger /*##############################################################################
504c66ac9dbSNicholas Bellinger // Stop functions called by external Target Fabrics Modules
505c66ac9dbSNicholas Bellinger //############################################################################*/
506c66ac9dbSNicholas Bellinger 
5072eafd729SChristoph Hellwig static inline struct se_dev_attrib *to_attrib(struct config_item *item)
5082eafd729SChristoph Hellwig {
5092eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_dev_attrib,
5102eafd729SChristoph Hellwig 			da_group);
5115873c4d1SChristoph Hellwig }
5125873c4d1SChristoph Hellwig 
5132eafd729SChristoph Hellwig /* Start functions for struct config_item_type tb_dev_attrib_cit */
5142eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_SHOW(_name)					\
5152eafd729SChristoph Hellwig static ssize_t _name##_show(struct config_item *item, char *page)	\
5162eafd729SChristoph Hellwig {									\
5172eafd729SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
5182eafd729SChristoph Hellwig }
5193effdb90SChristoph Hellwig 
5202eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
5212eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
5222eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
5232eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
5242eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
5252eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
5262eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
5272eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
5282eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
5292eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
5302eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
531b49d6f78SDavid Disseldorp DEF_CONFIGFS_ATTRIB_SHOW(emulate_pr);
5322eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
5332eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
534056e8924SDmitry Monakhov DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_verify);
5352eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
5362eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
5372eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
5382eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
5392eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
5402eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(block_size);
5412eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
5422eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
5432eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
5442eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
5452eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
5462eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
5472eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
5482eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
549e6f41633SJamie Pocas DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
5502eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
551bd217b8cSDmitry Bogdanov DEF_CONFIGFS_ATTRIB_SHOW(emulate_rsoc);
5522eafd729SChristoph Hellwig 
5532eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_U32(_name)				\
5542eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
5555873c4d1SChristoph Hellwig 		size_t count)						\
5565873c4d1SChristoph Hellwig {									\
5572eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5583effdb90SChristoph Hellwig 	u32 val;							\
5595873c4d1SChristoph Hellwig 	int ret;							\
5605873c4d1SChristoph Hellwig 									\
5613effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);					\
5623effdb90SChristoph Hellwig 	if (ret < 0)							\
5633effdb90SChristoph Hellwig 		return ret;						\
5643effdb90SChristoph Hellwig 	da->_name = val;						\
5653effdb90SChristoph Hellwig 	return count;							\
5665873c4d1SChristoph Hellwig }
5675873c4d1SChristoph Hellwig 
5682eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
5692eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
5702eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
5712eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
5722eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
5735873c4d1SChristoph Hellwig 
5742eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name)				\
5752eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,	\
5763effdb90SChristoph Hellwig 		size_t count)						\
5773effdb90SChristoph Hellwig {									\
5782eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5793effdb90SChristoph Hellwig 	bool flag;							\
5803effdb90SChristoph Hellwig 	int ret;							\
5813effdb90SChristoph Hellwig 									\
582*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);					\
5833effdb90SChristoph Hellwig 	if (ret < 0)							\
5843effdb90SChristoph Hellwig 		return ret;						\
5853effdb90SChristoph Hellwig 	da->_name = flag;						\
5863effdb90SChristoph Hellwig 	return count;							\
5873effdb90SChristoph Hellwig }
5883effdb90SChristoph Hellwig 
5892eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
5902eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
5912eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
592b49d6f78SDavid Disseldorp DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_pr);
5932eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
5942eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
5953effdb90SChristoph Hellwig 
5962eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name)				\
5972eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
5983effdb90SChristoph Hellwig 		size_t count)						\
5993effdb90SChristoph Hellwig {									\
6003effdb90SChristoph Hellwig 	printk_once(KERN_WARNING					\
601234bdbc4SChristophe Vu-Brugier 		"ignoring deprecated %s attribute\n",			\
602234bdbc4SChristophe Vu-Brugier 		__stringify(_name));					\
6033effdb90SChristoph Hellwig 	return count;							\
6043effdb90SChristoph Hellwig }
6053effdb90SChristoph Hellwig 
6062eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
6072eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
6083effdb90SChristoph Hellwig 
6093effdb90SChristoph Hellwig static void dev_set_t10_wwn_model_alias(struct se_device *dev)
6103effdb90SChristoph Hellwig {
6113effdb90SChristoph Hellwig 	const char *configname;
6123effdb90SChristoph Hellwig 
6133effdb90SChristoph Hellwig 	configname = config_item_name(&dev->dev_group.cg_item);
614b2da4abfSDavid Disseldorp 	if (strlen(configname) >= INQUIRY_MODEL_LEN) {
6153effdb90SChristoph Hellwig 		pr_warn("dev[%p]: Backstore name '%s' is too long for "
616b2da4abfSDavid Disseldorp 			"INQUIRY_MODEL, truncating to 15 characters\n", dev,
6173effdb90SChristoph Hellwig 			configname);
6183effdb90SChristoph Hellwig 	}
619b2da4abfSDavid Disseldorp 	/*
620b2da4abfSDavid Disseldorp 	 * XXX We can't use sizeof(dev->t10_wwn.model) (INQUIRY_MODEL_LEN + 1)
621b2da4abfSDavid Disseldorp 	 * here without potentially breaking existing setups, so continue to
622b2da4abfSDavid Disseldorp 	 * truncate one byte shorter than what can be carried in INQUIRY.
623b2da4abfSDavid Disseldorp 	 */
624b2da4abfSDavid Disseldorp 	strlcpy(dev->t10_wwn.model, configname, INQUIRY_MODEL_LEN);
6253effdb90SChristoph Hellwig }
6263effdb90SChristoph Hellwig 
6272eafd729SChristoph Hellwig static ssize_t emulate_model_alias_store(struct config_item *item,
6283effdb90SChristoph Hellwig 		const char *page, size_t count)
6293effdb90SChristoph Hellwig {
6302eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6313effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
6323effdb90SChristoph Hellwig 	bool flag;
6333effdb90SChristoph Hellwig 	int ret;
6343effdb90SChristoph Hellwig 
6353effdb90SChristoph Hellwig 	if (dev->export_count) {
6363effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change model alias"
6373effdb90SChristoph Hellwig 			" while export_count is %d\n",
6383effdb90SChristoph Hellwig 			dev, dev->export_count);
6393effdb90SChristoph Hellwig 		return -EINVAL;
6403effdb90SChristoph Hellwig 	}
6413effdb90SChristoph Hellwig 
642*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);
6433effdb90SChristoph Hellwig 	if (ret < 0)
6443effdb90SChristoph Hellwig 		return ret;
6453effdb90SChristoph Hellwig 
646b2da4abfSDavid Disseldorp 	BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
6473effdb90SChristoph Hellwig 	if (flag) {
6483effdb90SChristoph Hellwig 		dev_set_t10_wwn_model_alias(dev);
6493effdb90SChristoph Hellwig 	} else {
650b2da4abfSDavid Disseldorp 		strlcpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
651b2da4abfSDavid Disseldorp 			sizeof(dev->t10_wwn.model));
6523effdb90SChristoph Hellwig 	}
6533effdb90SChristoph Hellwig 	da->emulate_model_alias = flag;
6543effdb90SChristoph Hellwig 	return count;
6553effdb90SChristoph Hellwig }
6563effdb90SChristoph Hellwig 
6572eafd729SChristoph Hellwig static ssize_t emulate_write_cache_store(struct config_item *item,
6583effdb90SChristoph Hellwig 		const char *page, size_t count)
6593effdb90SChristoph Hellwig {
6602eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6613effdb90SChristoph Hellwig 	bool flag;
6623effdb90SChristoph Hellwig 	int ret;
6633effdb90SChristoph Hellwig 
664*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);
6653effdb90SChristoph Hellwig 	if (ret < 0)
6663effdb90SChristoph Hellwig 		return ret;
6673effdb90SChristoph Hellwig 
6683effdb90SChristoph Hellwig 	if (flag && da->da_dev->transport->get_write_cache) {
6693effdb90SChristoph Hellwig 		pr_err("emulate_write_cache not supported for this device\n");
6703effdb90SChristoph Hellwig 		return -EINVAL;
6713effdb90SChristoph Hellwig 	}
6723effdb90SChristoph Hellwig 
6733effdb90SChristoph Hellwig 	da->emulate_write_cache = flag;
6743effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
6753effdb90SChristoph Hellwig 			da->da_dev, flag);
6763effdb90SChristoph Hellwig 	return count;
6773effdb90SChristoph Hellwig }
6783effdb90SChristoph Hellwig 
6792eafd729SChristoph Hellwig static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
6803effdb90SChristoph Hellwig 		const char *page, size_t count)
6813effdb90SChristoph Hellwig {
6822eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6833effdb90SChristoph Hellwig 	u32 val;
6843effdb90SChristoph Hellwig 	int ret;
6853effdb90SChristoph Hellwig 
6863effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
6873effdb90SChristoph Hellwig 	if (ret < 0)
6883effdb90SChristoph Hellwig 		return ret;
6893effdb90SChristoph Hellwig 
6901bf630fdSDavid Disseldorp 	if (val != TARGET_UA_INTLCK_CTRL_CLEAR
6911bf630fdSDavid Disseldorp 	 && val != TARGET_UA_INTLCK_CTRL_NO_CLEAR
6921bf630fdSDavid Disseldorp 	 && val != TARGET_UA_INTLCK_CTRL_ESTABLISH_UA) {
6933effdb90SChristoph Hellwig 		pr_err("Illegal value %d\n", val);
6943effdb90SChristoph Hellwig 		return -EINVAL;
6953effdb90SChristoph Hellwig 	}
6963effdb90SChristoph Hellwig 
6973effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
6983effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
6993effdb90SChristoph Hellwig 			" UA_INTRLCK_CTRL while export_count is %d\n",
7003effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
7013effdb90SChristoph Hellwig 		return -EINVAL;
7023effdb90SChristoph Hellwig 	}
7033effdb90SChristoph Hellwig 	da->emulate_ua_intlck_ctrl = val;
7043effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
7053effdb90SChristoph Hellwig 		da->da_dev, val);
7063effdb90SChristoph Hellwig 	return count;
7073effdb90SChristoph Hellwig }
7083effdb90SChristoph Hellwig 
7092eafd729SChristoph Hellwig static ssize_t emulate_tas_store(struct config_item *item,
7103effdb90SChristoph Hellwig 		const char *page, size_t count)
7113effdb90SChristoph Hellwig {
7122eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7133effdb90SChristoph Hellwig 	bool flag;
7143effdb90SChristoph Hellwig 	int ret;
7153effdb90SChristoph Hellwig 
716*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);
7173effdb90SChristoph Hellwig 	if (ret < 0)
7183effdb90SChristoph Hellwig 		return ret;
7193effdb90SChristoph Hellwig 
7203effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
7213effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TAS while"
7223effdb90SChristoph Hellwig 			" export_count is %d\n",
7233effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
7243effdb90SChristoph Hellwig 		return -EINVAL;
7253effdb90SChristoph Hellwig 	}
7263effdb90SChristoph Hellwig 	da->emulate_tas = flag;
7273effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
7283effdb90SChristoph Hellwig 		da->da_dev, flag ? "Enabled" : "Disabled");
7293effdb90SChristoph Hellwig 
7303effdb90SChristoph Hellwig 	return count;
7313effdb90SChristoph Hellwig }
7323effdb90SChristoph Hellwig 
7332eafd729SChristoph Hellwig static ssize_t emulate_tpu_store(struct config_item *item,
7343effdb90SChristoph Hellwig 		const char *page, size_t count)
7353effdb90SChristoph Hellwig {
7362eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
73734bd1dcaSMike Christie 	struct se_device *dev = da->da_dev;
7383effdb90SChristoph Hellwig 	bool flag;
7393effdb90SChristoph Hellwig 	int ret;
7403effdb90SChristoph Hellwig 
741*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);
7423effdb90SChristoph Hellwig 	if (ret < 0)
7433effdb90SChristoph Hellwig 		return ret;
7443effdb90SChristoph Hellwig 
7453effdb90SChristoph Hellwig 	/*
7463effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
7473effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
7483effdb90SChristoph Hellwig 	 */
7493effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
75034bd1dcaSMike Christie 		if (!dev->transport->configure_unmap ||
75134bd1dcaSMike Christie 		    !dev->transport->configure_unmap(dev)) {
7523effdb90SChristoph Hellwig 			pr_err("Generic Block Discard not supported\n");
7533effdb90SChristoph Hellwig 			return -ENOSYS;
7543effdb90SChristoph Hellwig 		}
75534bd1dcaSMike Christie 	}
7563effdb90SChristoph Hellwig 
7573effdb90SChristoph Hellwig 	da->emulate_tpu = flag;
7583effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
7593effdb90SChristoph Hellwig 		da->da_dev, flag);
7603effdb90SChristoph Hellwig 	return count;
7613effdb90SChristoph Hellwig }
7623effdb90SChristoph Hellwig 
7632eafd729SChristoph Hellwig static ssize_t emulate_tpws_store(struct config_item *item,
7643effdb90SChristoph Hellwig 		const char *page, size_t count)
7653effdb90SChristoph Hellwig {
7662eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
76734bd1dcaSMike Christie 	struct se_device *dev = da->da_dev;
7683effdb90SChristoph Hellwig 	bool flag;
7693effdb90SChristoph Hellwig 	int ret;
7703effdb90SChristoph Hellwig 
771*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);
7723effdb90SChristoph Hellwig 	if (ret < 0)
7733effdb90SChristoph Hellwig 		return ret;
7743effdb90SChristoph Hellwig 
7753effdb90SChristoph Hellwig 	/*
7763effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
7773effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
7783effdb90SChristoph Hellwig 	 */
7793effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
78034bd1dcaSMike Christie 		if (!dev->transport->configure_unmap ||
78134bd1dcaSMike Christie 		    !dev->transport->configure_unmap(dev)) {
7823effdb90SChristoph Hellwig 			pr_err("Generic Block Discard not supported\n");
7833effdb90SChristoph Hellwig 			return -ENOSYS;
7843effdb90SChristoph Hellwig 		}
78534bd1dcaSMike Christie 	}
7863effdb90SChristoph Hellwig 
7873effdb90SChristoph Hellwig 	da->emulate_tpws = flag;
7883effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
7893effdb90SChristoph Hellwig 				da->da_dev, flag);
7903effdb90SChristoph Hellwig 	return count;
7913effdb90SChristoph Hellwig }
7923effdb90SChristoph Hellwig 
7932eafd729SChristoph Hellwig static ssize_t pi_prot_type_store(struct config_item *item,
7943effdb90SChristoph Hellwig 		const char *page, size_t count)
7953effdb90SChristoph Hellwig {
7962eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7973effdb90SChristoph Hellwig 	int old_prot = da->pi_prot_type, ret;
7983effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
7993effdb90SChristoph Hellwig 	u32 flag;
8003effdb90SChristoph Hellwig 
8013effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &flag);
8023effdb90SChristoph Hellwig 	if (ret < 0)
8033effdb90SChristoph Hellwig 		return ret;
8043effdb90SChristoph Hellwig 
8053effdb90SChristoph Hellwig 	if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
8063effdb90SChristoph Hellwig 		pr_err("Illegal value %d for pi_prot_type\n", flag);
8073effdb90SChristoph Hellwig 		return -EINVAL;
8083effdb90SChristoph Hellwig 	}
8093effdb90SChristoph Hellwig 	if (flag == 2) {
8103effdb90SChristoph Hellwig 		pr_err("DIF TYPE2 protection currently not supported\n");
8113effdb90SChristoph Hellwig 		return -ENOSYS;
8123effdb90SChristoph Hellwig 	}
8133effdb90SChristoph Hellwig 	if (da->hw_pi_prot_type) {
8143effdb90SChristoph Hellwig 		pr_warn("DIF protection enabled on underlying hardware,"
8153effdb90SChristoph Hellwig 			" ignoring\n");
8163effdb90SChristoph Hellwig 		return count;
8173effdb90SChristoph Hellwig 	}
8183effdb90SChristoph Hellwig 	if (!dev->transport->init_prot || !dev->transport->free_prot) {
8193effdb90SChristoph Hellwig 		/* 0 is only allowed value for non-supporting backends */
8203effdb90SChristoph Hellwig 		if (flag == 0)
821bc1a7d6aSAndy Grover 			return count;
8223effdb90SChristoph Hellwig 
8233effdb90SChristoph Hellwig 		pr_err("DIF protection not supported by backend: %s\n",
8243effdb90SChristoph Hellwig 		       dev->transport->name);
8253effdb90SChristoph Hellwig 		return -ENOSYS;
8263effdb90SChristoph Hellwig 	}
827cb0f32e1SMike Christie 	if (!target_dev_configured(dev)) {
8283effdb90SChristoph Hellwig 		pr_err("DIF protection requires device to be configured\n");
8293effdb90SChristoph Hellwig 		return -ENODEV;
8303effdb90SChristoph Hellwig 	}
8313effdb90SChristoph Hellwig 	if (dev->export_count) {
8323effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device PROT type while"
8333effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
8343effdb90SChristoph Hellwig 		return -EINVAL;
8353effdb90SChristoph Hellwig 	}
8363effdb90SChristoph Hellwig 
8373effdb90SChristoph Hellwig 	da->pi_prot_type = flag;
8383effdb90SChristoph Hellwig 
8393effdb90SChristoph Hellwig 	if (flag && !old_prot) {
8403effdb90SChristoph Hellwig 		ret = dev->transport->init_prot(dev);
8413effdb90SChristoph Hellwig 		if (ret) {
8423effdb90SChristoph Hellwig 			da->pi_prot_type = old_prot;
843056e8924SDmitry Monakhov 			da->pi_prot_verify = (bool) da->pi_prot_type;
8443effdb90SChristoph Hellwig 			return ret;
8453effdb90SChristoph Hellwig 		}
8463effdb90SChristoph Hellwig 
8473effdb90SChristoph Hellwig 	} else if (!flag && old_prot) {
8483effdb90SChristoph Hellwig 		dev->transport->free_prot(dev);
8493effdb90SChristoph Hellwig 	}
8503effdb90SChristoph Hellwig 
851056e8924SDmitry Monakhov 	da->pi_prot_verify = (bool) da->pi_prot_type;
8523effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
8533effdb90SChristoph Hellwig 	return count;
8543effdb90SChristoph Hellwig }
8553effdb90SChristoph Hellwig 
856b6cd7f34SDavid Disseldorp /* always zero, but attr needs to remain RW to avoid userspace breakage */
857b6cd7f34SDavid Disseldorp static ssize_t pi_prot_format_show(struct config_item *item, char *page)
858b6cd7f34SDavid Disseldorp {
859b6cd7f34SDavid Disseldorp 	return snprintf(page, PAGE_SIZE, "0\n");
860b6cd7f34SDavid Disseldorp }
861b6cd7f34SDavid Disseldorp 
8622eafd729SChristoph Hellwig static ssize_t pi_prot_format_store(struct config_item *item,
8633effdb90SChristoph Hellwig 		const char *page, size_t count)
8643effdb90SChristoph Hellwig {
8652eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
8663effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
8673effdb90SChristoph Hellwig 	bool flag;
8683effdb90SChristoph Hellwig 	int ret;
8693effdb90SChristoph Hellwig 
870*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);
8713effdb90SChristoph Hellwig 	if (ret < 0)
8723effdb90SChristoph Hellwig 		return ret;
8733effdb90SChristoph Hellwig 
8743effdb90SChristoph Hellwig 	if (!flag)
8753effdb90SChristoph Hellwig 		return count;
8763effdb90SChristoph Hellwig 
8773effdb90SChristoph Hellwig 	if (!dev->transport->format_prot) {
8783effdb90SChristoph Hellwig 		pr_err("DIF protection format not supported by backend %s\n",
8793effdb90SChristoph Hellwig 		       dev->transport->name);
8803effdb90SChristoph Hellwig 		return -ENOSYS;
8813effdb90SChristoph Hellwig 	}
882cb0f32e1SMike Christie 	if (!target_dev_configured(dev)) {
8833effdb90SChristoph Hellwig 		pr_err("DIF protection format requires device to be configured\n");
8843effdb90SChristoph Hellwig 		return -ENODEV;
8853effdb90SChristoph Hellwig 	}
8863effdb90SChristoph Hellwig 	if (dev->export_count) {
8873effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to format SE Device PROT type while"
8883effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
8893effdb90SChristoph Hellwig 		return -EINVAL;
8903effdb90SChristoph Hellwig 	}
8913effdb90SChristoph Hellwig 
8923effdb90SChristoph Hellwig 	ret = dev->transport->format_prot(dev);
8933effdb90SChristoph Hellwig 	if (ret)
8943effdb90SChristoph Hellwig 		return ret;
8953effdb90SChristoph Hellwig 
8963effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
8973effdb90SChristoph Hellwig 	return count;
8983effdb90SChristoph Hellwig }
8993effdb90SChristoph Hellwig 
900056e8924SDmitry Monakhov static ssize_t pi_prot_verify_store(struct config_item *item,
901056e8924SDmitry Monakhov 		const char *page, size_t count)
902056e8924SDmitry Monakhov {
903056e8924SDmitry Monakhov 	struct se_dev_attrib *da = to_attrib(item);
904056e8924SDmitry Monakhov 	bool flag;
905056e8924SDmitry Monakhov 	int ret;
906056e8924SDmitry Monakhov 
907*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);
908056e8924SDmitry Monakhov 	if (ret < 0)
909056e8924SDmitry Monakhov 		return ret;
910056e8924SDmitry Monakhov 
911056e8924SDmitry Monakhov 	if (!flag) {
912056e8924SDmitry Monakhov 		da->pi_prot_verify = flag;
913056e8924SDmitry Monakhov 		return count;
914056e8924SDmitry Monakhov 	}
915056e8924SDmitry Monakhov 	if (da->hw_pi_prot_type) {
916056e8924SDmitry Monakhov 		pr_warn("DIF protection enabled on underlying hardware,"
917056e8924SDmitry Monakhov 			" ignoring\n");
918056e8924SDmitry Monakhov 		return count;
919056e8924SDmitry Monakhov 	}
920056e8924SDmitry Monakhov 	if (!da->pi_prot_type) {
921056e8924SDmitry Monakhov 		pr_warn("DIF protection not supported by backend, ignoring\n");
922056e8924SDmitry Monakhov 		return count;
923056e8924SDmitry Monakhov 	}
924056e8924SDmitry Monakhov 	da->pi_prot_verify = flag;
925056e8924SDmitry Monakhov 
926056e8924SDmitry Monakhov 	return count;
927056e8924SDmitry Monakhov }
928056e8924SDmitry Monakhov 
9292eafd729SChristoph Hellwig static ssize_t force_pr_aptpl_store(struct config_item *item,
9303effdb90SChristoph Hellwig 		const char *page, size_t count)
9313effdb90SChristoph Hellwig {
9322eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9333effdb90SChristoph Hellwig 	bool flag;
9343effdb90SChristoph Hellwig 	int ret;
9353effdb90SChristoph Hellwig 
936*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);
9373effdb90SChristoph Hellwig 	if (ret < 0)
9383effdb90SChristoph Hellwig 		return ret;
9393effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
9403effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to set force_pr_aptpl while"
9413effdb90SChristoph Hellwig 		       " export_count is %d\n",
9423effdb90SChristoph Hellwig 		       da->da_dev, da->da_dev->export_count);
9433effdb90SChristoph Hellwig 		return -EINVAL;
9443effdb90SChristoph Hellwig 	}
9453effdb90SChristoph Hellwig 
9463effdb90SChristoph Hellwig 	da->force_pr_aptpl = flag;
9473effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
9483effdb90SChristoph Hellwig 	return count;
9493effdb90SChristoph Hellwig }
9503effdb90SChristoph Hellwig 
9512eafd729SChristoph Hellwig static ssize_t emulate_rest_reord_store(struct config_item *item,
9523effdb90SChristoph Hellwig 		const char *page, size_t count)
9533effdb90SChristoph Hellwig {
9542eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9553effdb90SChristoph Hellwig 	bool flag;
9563effdb90SChristoph Hellwig 	int ret;
9573effdb90SChristoph Hellwig 
958*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);
9593effdb90SChristoph Hellwig 	if (ret < 0)
9603effdb90SChristoph Hellwig 		return ret;
9613effdb90SChristoph Hellwig 
9623effdb90SChristoph Hellwig 	if (flag != 0) {
9633effdb90SChristoph Hellwig 		printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
9643effdb90SChristoph Hellwig 			" reordering not implemented\n", da->da_dev);
9653effdb90SChristoph Hellwig 		return -ENOSYS;
9663effdb90SChristoph Hellwig 	}
9673effdb90SChristoph Hellwig 	da->emulate_rest_reord = flag;
9683effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
9693effdb90SChristoph Hellwig 		da->da_dev, flag);
9703effdb90SChristoph Hellwig 	return count;
9713effdb90SChristoph Hellwig }
9723effdb90SChristoph Hellwig 
973e6f41633SJamie Pocas static ssize_t unmap_zeroes_data_store(struct config_item *item,
974e6f41633SJamie Pocas 		const char *page, size_t count)
975e6f41633SJamie Pocas {
976e6f41633SJamie Pocas 	struct se_dev_attrib *da = to_attrib(item);
97734bd1dcaSMike Christie 	struct se_device *dev = da->da_dev;
978e6f41633SJamie Pocas 	bool flag;
979e6f41633SJamie Pocas 	int ret;
980e6f41633SJamie Pocas 
981*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);
982e6f41633SJamie Pocas 	if (ret < 0)
983e6f41633SJamie Pocas 		return ret;
984e6f41633SJamie Pocas 
985e6f41633SJamie Pocas 	if (da->da_dev->export_count) {
986e6f41633SJamie Pocas 		pr_err("dev[%p]: Unable to change SE Device"
987e6f41633SJamie Pocas 		       " unmap_zeroes_data while export_count is %d\n",
988e6f41633SJamie Pocas 		       da->da_dev, da->da_dev->export_count);
989e6f41633SJamie Pocas 		return -EINVAL;
990e6f41633SJamie Pocas 	}
991e6f41633SJamie Pocas 	/*
992e6f41633SJamie Pocas 	 * We expect this value to be non-zero when generic Block Layer
993e6f41633SJamie Pocas 	 * Discard supported is detected iblock_configure_device().
994e6f41633SJamie Pocas 	 */
995e6f41633SJamie Pocas 	if (flag && !da->max_unmap_block_desc_count) {
99634bd1dcaSMike Christie 		if (!dev->transport->configure_unmap ||
99734bd1dcaSMike Christie 		    !dev->transport->configure_unmap(dev)) {
99834bd1dcaSMike Christie 			pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set because max_unmap_block_desc_count is zero\n",
999e6f41633SJamie Pocas 			       da->da_dev);
1000e6f41633SJamie Pocas 			return -ENOSYS;
1001e6f41633SJamie Pocas 		}
100234bd1dcaSMike Christie 	}
1003e6f41633SJamie Pocas 	da->unmap_zeroes_data = flag;
1004e6f41633SJamie Pocas 	pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
1005e6f41633SJamie Pocas 		 da->da_dev, flag);
10062e498f25SNicholas Bellinger 	return count;
1007e6f41633SJamie Pocas }
1008e6f41633SJamie Pocas 
10093effdb90SChristoph Hellwig /*
10103effdb90SChristoph Hellwig  * Note, this can only be called on unexported SE Device Object.
10113effdb90SChristoph Hellwig  */
10122eafd729SChristoph Hellwig static ssize_t queue_depth_store(struct config_item *item,
10133effdb90SChristoph Hellwig 		const char *page, size_t count)
10143effdb90SChristoph Hellwig {
10152eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
10163effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
10173effdb90SChristoph Hellwig 	u32 val;
10183effdb90SChristoph Hellwig 	int ret;
10193effdb90SChristoph Hellwig 
10203effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
10213effdb90SChristoph Hellwig 	if (ret < 0)
10223effdb90SChristoph Hellwig 		return ret;
10233effdb90SChristoph Hellwig 
10243effdb90SChristoph Hellwig 	if (dev->export_count) {
10253effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TCQ while"
10263effdb90SChristoph Hellwig 			" export_count is %d\n",
10273effdb90SChristoph Hellwig 			dev, dev->export_count);
10283effdb90SChristoph Hellwig 		return -EINVAL;
10293effdb90SChristoph Hellwig 	}
10303effdb90SChristoph Hellwig 	if (!val) {
10313effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
10323effdb90SChristoph Hellwig 		return -EINVAL;
10333effdb90SChristoph Hellwig 	}
10343effdb90SChristoph Hellwig 
10353effdb90SChristoph Hellwig 	if (val > dev->dev_attrib.queue_depth) {
10363effdb90SChristoph Hellwig 		if (val > dev->dev_attrib.hw_queue_depth) {
10373effdb90SChristoph Hellwig 			pr_err("dev[%p]: Passed queue_depth:"
10383effdb90SChristoph Hellwig 				" %u exceeds TCM/SE_Device MAX"
10393effdb90SChristoph Hellwig 				" TCQ: %u\n", dev, val,
10403effdb90SChristoph Hellwig 				dev->dev_attrib.hw_queue_depth);
10413effdb90SChristoph Hellwig 			return -EINVAL;
10423effdb90SChristoph Hellwig 		}
10433effdb90SChristoph Hellwig 	}
10443effdb90SChristoph Hellwig 	da->queue_depth = dev->queue_depth = val;
10453effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
10463effdb90SChristoph Hellwig 	return count;
10473effdb90SChristoph Hellwig }
10483effdb90SChristoph Hellwig 
10492eafd729SChristoph Hellwig static ssize_t optimal_sectors_store(struct config_item *item,
10503effdb90SChristoph Hellwig 		const char *page, size_t count)
10513effdb90SChristoph Hellwig {
10522eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
10533effdb90SChristoph Hellwig 	u32 val;
10543effdb90SChristoph Hellwig 	int ret;
10553effdb90SChristoph Hellwig 
10563effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
10573effdb90SChristoph Hellwig 	if (ret < 0)
10583effdb90SChristoph Hellwig 		return ret;
10593effdb90SChristoph Hellwig 
10603effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
10613effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
10623effdb90SChristoph Hellwig 			" optimal_sectors while export_count is %d\n",
10633effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
10643effdb90SChristoph Hellwig 		return -EINVAL;
10653effdb90SChristoph Hellwig 	}
10663effdb90SChristoph Hellwig 	if (val > da->hw_max_sectors) {
10673effdb90SChristoph Hellwig 		pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
10683effdb90SChristoph Hellwig 			" greater than hw_max_sectors: %u\n",
10693effdb90SChristoph Hellwig 			da->da_dev, val, da->hw_max_sectors);
10703effdb90SChristoph Hellwig 		return -EINVAL;
10713effdb90SChristoph Hellwig 	}
10723effdb90SChristoph Hellwig 
10733effdb90SChristoph Hellwig 	da->optimal_sectors = val;
10743effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
10753effdb90SChristoph Hellwig 			da->da_dev, val);
10763effdb90SChristoph Hellwig 	return count;
10773effdb90SChristoph Hellwig }
10783effdb90SChristoph Hellwig 
10792eafd729SChristoph Hellwig static ssize_t block_size_store(struct config_item *item,
10803effdb90SChristoph Hellwig 		const char *page, size_t count)
10813effdb90SChristoph Hellwig {
10822eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
10833effdb90SChristoph Hellwig 	u32 val;
10843effdb90SChristoph Hellwig 	int ret;
10853effdb90SChristoph Hellwig 
10863effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
10873effdb90SChristoph Hellwig 	if (ret < 0)
10883effdb90SChristoph Hellwig 		return ret;
10893effdb90SChristoph Hellwig 
10903effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
10913effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device block_size"
10923effdb90SChristoph Hellwig 			" while export_count is %d\n",
10933effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
10943effdb90SChristoph Hellwig 		return -EINVAL;
10953effdb90SChristoph Hellwig 	}
10963effdb90SChristoph Hellwig 
10973effdb90SChristoph Hellwig 	if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
10983effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal value for block_device: %u"
10993effdb90SChristoph Hellwig 			" for SE device, must be 512, 1024, 2048 or 4096\n",
11003effdb90SChristoph Hellwig 			da->da_dev, val);
11013effdb90SChristoph Hellwig 		return -EINVAL;
11023effdb90SChristoph Hellwig 	}
11033effdb90SChristoph Hellwig 
11043effdb90SChristoph Hellwig 	da->block_size = val;
11053effdb90SChristoph Hellwig 	if (da->max_bytes_per_io)
11063effdb90SChristoph Hellwig 		da->hw_max_sectors = da->max_bytes_per_io / val;
11073effdb90SChristoph Hellwig 
11083effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device block_size changed to %u\n",
11093effdb90SChristoph Hellwig 			da->da_dev, val);
11103effdb90SChristoph Hellwig 	return count;
11113effdb90SChristoph Hellwig }
11125873c4d1SChristoph Hellwig 
1113c17d5d5fSMike Christie static ssize_t alua_support_show(struct config_item *item, char *page)
1114c17d5d5fSMike Christie {
1115c17d5d5fSMike Christie 	struct se_dev_attrib *da = to_attrib(item);
111669088a04SBodo Stroesser 	u8 flags = da->da_dev->transport_flags;
1117c17d5d5fSMike Christie 
1118c17d5d5fSMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n",
1119c17d5d5fSMike Christie 			flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ? 0 : 1);
1120c17d5d5fSMike Christie }
1121c17d5d5fSMike Christie 
1122356ba2a8SBodo Stroesser static ssize_t alua_support_store(struct config_item *item,
1123356ba2a8SBodo Stroesser 		const char *page, size_t count)
1124356ba2a8SBodo Stroesser {
1125356ba2a8SBodo Stroesser 	struct se_dev_attrib *da = to_attrib(item);
1126356ba2a8SBodo Stroesser 	struct se_device *dev = da->da_dev;
1127ef7ae7f7SMaurizio Lombardi 	bool flag, oldflag;
1128356ba2a8SBodo Stroesser 	int ret;
1129356ba2a8SBodo Stroesser 
1130*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);
1131ef7ae7f7SMaurizio Lombardi 	if (ret < 0)
1132ef7ae7f7SMaurizio Lombardi 		return ret;
1133ef7ae7f7SMaurizio Lombardi 
1134ef7ae7f7SMaurizio Lombardi 	oldflag = !(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA);
1135ef7ae7f7SMaurizio Lombardi 	if (flag == oldflag)
1136ef7ae7f7SMaurizio Lombardi 		return count;
1137ef7ae7f7SMaurizio Lombardi 
1138356ba2a8SBodo Stroesser 	if (!(dev->transport->transport_flags_changeable &
1139356ba2a8SBodo Stroesser 	      TRANSPORT_FLAG_PASSTHROUGH_ALUA)) {
1140356ba2a8SBodo Stroesser 		pr_err("dev[%p]: Unable to change SE Device alua_support:"
1141356ba2a8SBodo Stroesser 			" alua_support has fixed value\n", dev);
1142ef7ae7f7SMaurizio Lombardi 		return -ENOSYS;
1143356ba2a8SBodo Stroesser 	}
1144356ba2a8SBodo Stroesser 
1145356ba2a8SBodo Stroesser 	if (flag)
1146356ba2a8SBodo Stroesser 		dev->transport_flags &= ~TRANSPORT_FLAG_PASSTHROUGH_ALUA;
1147356ba2a8SBodo Stroesser 	else
1148356ba2a8SBodo Stroesser 		dev->transport_flags |= TRANSPORT_FLAG_PASSTHROUGH_ALUA;
1149356ba2a8SBodo Stroesser 	return count;
1150356ba2a8SBodo Stroesser }
1151356ba2a8SBodo Stroesser 
1152c17d5d5fSMike Christie static ssize_t pgr_support_show(struct config_item *item, char *page)
1153c17d5d5fSMike Christie {
1154c17d5d5fSMike Christie 	struct se_dev_attrib *da = to_attrib(item);
115569088a04SBodo Stroesser 	u8 flags = da->da_dev->transport_flags;
1156c17d5d5fSMike Christie 
1157c17d5d5fSMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n",
1158c17d5d5fSMike Christie 			flags & TRANSPORT_FLAG_PASSTHROUGH_PGR ? 0 : 1);
1159c17d5d5fSMike Christie }
1160c17d5d5fSMike Christie 
1161356ba2a8SBodo Stroesser static ssize_t pgr_support_store(struct config_item *item,
1162356ba2a8SBodo Stroesser 		const char *page, size_t count)
1163356ba2a8SBodo Stroesser {
1164356ba2a8SBodo Stroesser 	struct se_dev_attrib *da = to_attrib(item);
1165356ba2a8SBodo Stroesser 	struct se_device *dev = da->da_dev;
1166ef7ae7f7SMaurizio Lombardi 	bool flag, oldflag;
1167356ba2a8SBodo Stroesser 	int ret;
1168356ba2a8SBodo Stroesser 
1169*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);
1170ef7ae7f7SMaurizio Lombardi 	if (ret < 0)
1171ef7ae7f7SMaurizio Lombardi 		return ret;
1172ef7ae7f7SMaurizio Lombardi 
1173ef7ae7f7SMaurizio Lombardi 	oldflag = !(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR);
1174ef7ae7f7SMaurizio Lombardi 	if (flag == oldflag)
1175ef7ae7f7SMaurizio Lombardi 		return count;
1176ef7ae7f7SMaurizio Lombardi 
1177356ba2a8SBodo Stroesser 	if (!(dev->transport->transport_flags_changeable &
1178356ba2a8SBodo Stroesser 	      TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
1179356ba2a8SBodo Stroesser 		pr_err("dev[%p]: Unable to change SE Device pgr_support:"
1180356ba2a8SBodo Stroesser 			" pgr_support has fixed value\n", dev);
1181ef7ae7f7SMaurizio Lombardi 		return -ENOSYS;
1182356ba2a8SBodo Stroesser 	}
1183356ba2a8SBodo Stroesser 
1184356ba2a8SBodo Stroesser 	if (flag)
1185356ba2a8SBodo Stroesser 		dev->transport_flags &= ~TRANSPORT_FLAG_PASSTHROUGH_PGR;
1186356ba2a8SBodo Stroesser 	else
1187356ba2a8SBodo Stroesser 		dev->transport_flags |= TRANSPORT_FLAG_PASSTHROUGH_PGR;
1188356ba2a8SBodo Stroesser 	return count;
1189356ba2a8SBodo Stroesser }
1190356ba2a8SBodo Stroesser 
1191bd217b8cSDmitry Bogdanov static ssize_t emulate_rsoc_store(struct config_item *item,
1192bd217b8cSDmitry Bogdanov 		const char *page, size_t count)
1193bd217b8cSDmitry Bogdanov {
1194bd217b8cSDmitry Bogdanov 	struct se_dev_attrib *da = to_attrib(item);
1195bd217b8cSDmitry Bogdanov 	bool flag;
1196bd217b8cSDmitry Bogdanov 	int ret;
1197bd217b8cSDmitry Bogdanov 
1198*e56ca6bcSChristophe JAILLET 	ret = kstrtobool(page, &flag);
1199bd217b8cSDmitry Bogdanov 	if (ret < 0)
1200bd217b8cSDmitry Bogdanov 		return ret;
1201bd217b8cSDmitry Bogdanov 
1202bd217b8cSDmitry Bogdanov 	da->emulate_rsoc = flag;
1203bd217b8cSDmitry Bogdanov 	pr_debug("dev[%p]: SE Device REPORT_SUPPORTED_OPERATION_CODES_EMULATION flag: %d\n",
1204bd217b8cSDmitry Bogdanov 			da->da_dev, flag);
1205bd217b8cSDmitry Bogdanov 	return count;
1206bd217b8cSDmitry Bogdanov }
1207bd217b8cSDmitry Bogdanov 
12082eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_model_alias);
12092eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_dpo);
12102eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_write);
12112eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_read);
12122eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_write_cache);
12132eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
12142eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tas);
12152eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpu);
12162eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpws);
12172eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_caw);
12182eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_3pc);
1219b49d6f78SDavid Disseldorp CONFIGFS_ATTR(, emulate_pr);
1220bd217b8cSDmitry Bogdanov CONFIGFS_ATTR(, emulate_rsoc);
12212eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_type);
12222eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_pi_prot_type);
1223b6cd7f34SDavid Disseldorp CONFIGFS_ATTR(, pi_prot_format);
1224056e8924SDmitry Monakhov CONFIGFS_ATTR(, pi_prot_verify);
12252eafd729SChristoph Hellwig CONFIGFS_ATTR(, enforce_pr_isids);
12262eafd729SChristoph Hellwig CONFIGFS_ATTR(, is_nonrot);
12272eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_rest_reord);
12282eafd729SChristoph Hellwig CONFIGFS_ATTR(, force_pr_aptpl);
12292eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_block_size);
12302eafd729SChristoph Hellwig CONFIGFS_ATTR(, block_size);
12312eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_max_sectors);
12322eafd729SChristoph Hellwig CONFIGFS_ATTR(, optimal_sectors);
12332eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_queue_depth);
12342eafd729SChristoph Hellwig CONFIGFS_ATTR(, queue_depth);
12352eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_lba_count);
12362eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_block_desc_count);
12372eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity);
12382eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity_alignment);
1239e6f41633SJamie Pocas CONFIGFS_ATTR(, unmap_zeroes_data);
12402eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_write_same_len);
1241356ba2a8SBodo Stroesser CONFIGFS_ATTR(, alua_support);
1242356ba2a8SBodo Stroesser CONFIGFS_ATTR(, pgr_support);
1243c66ac9dbSNicholas Bellinger 
12445873c4d1SChristoph Hellwig /*
12455873c4d1SChristoph Hellwig  * dev_attrib attributes for devices using the target core SBC/SPC
12465873c4d1SChristoph Hellwig  * interpreter.  Any backend using spc_parse_cdb should be using
12475873c4d1SChristoph Hellwig  * these.
12485873c4d1SChristoph Hellwig  */
12495873c4d1SChristoph Hellwig struct configfs_attribute *sbc_attrib_attrs[] = {
12502eafd729SChristoph Hellwig 	&attr_emulate_model_alias,
12512eafd729SChristoph Hellwig 	&attr_emulate_dpo,
12522eafd729SChristoph Hellwig 	&attr_emulate_fua_write,
12532eafd729SChristoph Hellwig 	&attr_emulate_fua_read,
12542eafd729SChristoph Hellwig 	&attr_emulate_write_cache,
12552eafd729SChristoph Hellwig 	&attr_emulate_ua_intlck_ctrl,
12562eafd729SChristoph Hellwig 	&attr_emulate_tas,
12572eafd729SChristoph Hellwig 	&attr_emulate_tpu,
12582eafd729SChristoph Hellwig 	&attr_emulate_tpws,
12592eafd729SChristoph Hellwig 	&attr_emulate_caw,
12602eafd729SChristoph Hellwig 	&attr_emulate_3pc,
1261b49d6f78SDavid Disseldorp 	&attr_emulate_pr,
12622eafd729SChristoph Hellwig 	&attr_pi_prot_type,
12632eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
12642eafd729SChristoph Hellwig 	&attr_pi_prot_format,
1265056e8924SDmitry Monakhov 	&attr_pi_prot_verify,
12662eafd729SChristoph Hellwig 	&attr_enforce_pr_isids,
12672eafd729SChristoph Hellwig 	&attr_is_nonrot,
12682eafd729SChristoph Hellwig 	&attr_emulate_rest_reord,
12692eafd729SChristoph Hellwig 	&attr_force_pr_aptpl,
12702eafd729SChristoph Hellwig 	&attr_hw_block_size,
12712eafd729SChristoph Hellwig 	&attr_block_size,
12722eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
12732eafd729SChristoph Hellwig 	&attr_optimal_sectors,
12742eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
12752eafd729SChristoph Hellwig 	&attr_queue_depth,
12762eafd729SChristoph Hellwig 	&attr_max_unmap_lba_count,
12772eafd729SChristoph Hellwig 	&attr_max_unmap_block_desc_count,
12782eafd729SChristoph Hellwig 	&attr_unmap_granularity,
12792eafd729SChristoph Hellwig 	&attr_unmap_granularity_alignment,
1280e6f41633SJamie Pocas 	&attr_unmap_zeroes_data,
12812eafd729SChristoph Hellwig 	&attr_max_write_same_len,
1282c17d5d5fSMike Christie 	&attr_alua_support,
1283c17d5d5fSMike Christie 	&attr_pgr_support,
1284bd217b8cSDmitry Bogdanov 	&attr_emulate_rsoc,
12855873c4d1SChristoph Hellwig 	NULL,
12865873c4d1SChristoph Hellwig };
12875873c4d1SChristoph Hellwig EXPORT_SYMBOL(sbc_attrib_attrs);
12885873c4d1SChristoph Hellwig 
12895873c4d1SChristoph Hellwig /*
12905873c4d1SChristoph Hellwig  * Minimal dev_attrib attributes for devices passing through CDBs.
12915873c4d1SChristoph Hellwig  * In this case we only provide a few read-only attributes for
12925873c4d1SChristoph Hellwig  * backwards compatibility.
12935873c4d1SChristoph Hellwig  */
12945873c4d1SChristoph Hellwig struct configfs_attribute *passthrough_attrib_attrs[] = {
12952eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
12962eafd729SChristoph Hellwig 	&attr_hw_block_size,
12972eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
12982eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
129992999417SBodo Stroesser 	&attr_emulate_pr,
1300c17d5d5fSMike Christie 	&attr_alua_support,
1301c17d5d5fSMike Christie 	&attr_pgr_support,
13025873c4d1SChristoph Hellwig 	NULL,
13035873c4d1SChristoph Hellwig };
13045873c4d1SChristoph Hellwig EXPORT_SYMBOL(passthrough_attrib_attrs);
13055873c4d1SChristoph Hellwig 
13064703b625SBodo Stroesser /*
13074703b625SBodo Stroesser  * pr related dev_attrib attributes for devices passing through CDBs,
13084703b625SBodo Stroesser  * but allowing in core pr emulation.
13094703b625SBodo Stroesser  */
13104703b625SBodo Stroesser struct configfs_attribute *passthrough_pr_attrib_attrs[] = {
13114703b625SBodo Stroesser 	&attr_enforce_pr_isids,
13124703b625SBodo Stroesser 	&attr_force_pr_aptpl,
13134703b625SBodo Stroesser 	NULL,
13144703b625SBodo Stroesser };
13154703b625SBodo Stroesser EXPORT_SYMBOL(passthrough_pr_attrib_attrs);
13164703b625SBodo Stroesser 
13172eafd729SChristoph Hellwig TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
13188dc31ff9SMike Christie TB_CIT_SETUP_DRV(dev_action, NULL, NULL);
1319c66ac9dbSNicholas Bellinger 
1320f79a897eSNicholas Bellinger /* End functions for struct config_item_type tb_dev_attrib_cit */
1321c66ac9dbSNicholas Bellinger 
1322f8d389c6SNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_wwn_cit */
1323c66ac9dbSNicholas Bellinger 
13242eafd729SChristoph Hellwig static struct t10_wwn *to_t10_wwn(struct config_item *item)
13252eafd729SChristoph Hellwig {
13262eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
13272eafd729SChristoph Hellwig }
1328c66ac9dbSNicholas Bellinger 
13290322913cSAlan Adamson static ssize_t target_check_inquiry_data(char *buf)
13300322913cSAlan Adamson {
13310322913cSAlan Adamson 	size_t len;
13320322913cSAlan Adamson 	int i;
13330322913cSAlan Adamson 
13340322913cSAlan Adamson 	len = strlen(buf);
13350322913cSAlan Adamson 
13360322913cSAlan Adamson 	/*
13370322913cSAlan Adamson 	 * SPC 4.3.1:
13380322913cSAlan Adamson 	 * ASCII data fields shall contain only ASCII printable characters
13390322913cSAlan Adamson 	 * (i.e., code values 20h to 7Eh) and may be terminated with one or
13400322913cSAlan Adamson 	 * more ASCII null (00h) characters.
13410322913cSAlan Adamson 	 */
13420322913cSAlan Adamson 	for (i = 0; i < len; i++) {
13430322913cSAlan Adamson 		if (buf[i] < 0x20 || buf[i] > 0x7E) {
13440322913cSAlan Adamson 			pr_err("Emulated T10 Inquiry Data contains non-ASCII-printable characters\n");
13450322913cSAlan Adamson 			return -EINVAL;
13460322913cSAlan Adamson 		}
13470322913cSAlan Adamson 	}
13480322913cSAlan Adamson 
13490322913cSAlan Adamson 	return len;
13500322913cSAlan Adamson }
13510322913cSAlan Adamson 
1352c66ac9dbSNicholas Bellinger /*
135354a6f3f6SDavid Disseldorp  * STANDARD and VPD page 0x83 T10 Vendor Identification
135454a6f3f6SDavid Disseldorp  */
135554a6f3f6SDavid Disseldorp static ssize_t target_wwn_vendor_id_show(struct config_item *item,
135654a6f3f6SDavid Disseldorp 		char *page)
135754a6f3f6SDavid Disseldorp {
135854a6f3f6SDavid Disseldorp 	return sprintf(page, "%s\n", &to_t10_wwn(item)->vendor[0]);
135954a6f3f6SDavid Disseldorp }
136054a6f3f6SDavid Disseldorp 
136154a6f3f6SDavid Disseldorp static ssize_t target_wwn_vendor_id_store(struct config_item *item,
136254a6f3f6SDavid Disseldorp 		const char *page, size_t count)
136354a6f3f6SDavid Disseldorp {
136454a6f3f6SDavid Disseldorp 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
136554a6f3f6SDavid Disseldorp 	struct se_device *dev = t10_wwn->t10_dev;
136654a6f3f6SDavid Disseldorp 	/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
136754a6f3f6SDavid Disseldorp 	unsigned char buf[INQUIRY_VENDOR_LEN + 2];
136854a6f3f6SDavid Disseldorp 	char *stripped = NULL;
1369ee26724aSColin Ian King 	size_t len;
1370ee26724aSColin Ian King 	ssize_t ret;
137154a6f3f6SDavid Disseldorp 
137254a6f3f6SDavid Disseldorp 	len = strlcpy(buf, page, sizeof(buf));
137354a6f3f6SDavid Disseldorp 	if (len < sizeof(buf)) {
137454a6f3f6SDavid Disseldorp 		/* Strip any newline added from userspace. */
137554a6f3f6SDavid Disseldorp 		stripped = strstrip(buf);
137654a6f3f6SDavid Disseldorp 		len = strlen(stripped);
137754a6f3f6SDavid Disseldorp 	}
137854a6f3f6SDavid Disseldorp 	if (len > INQUIRY_VENDOR_LEN) {
137954a6f3f6SDavid Disseldorp 		pr_err("Emulated T10 Vendor Identification exceeds"
138054a6f3f6SDavid Disseldorp 			" INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)
138154a6f3f6SDavid Disseldorp 			"\n");
138254a6f3f6SDavid Disseldorp 		return -EOVERFLOW;
138354a6f3f6SDavid Disseldorp 	}
138454a6f3f6SDavid Disseldorp 
13850322913cSAlan Adamson 	ret = target_check_inquiry_data(stripped);
13860322913cSAlan Adamson 
13870322913cSAlan Adamson 	if (ret < 0)
13880322913cSAlan Adamson 		return ret;
138954a6f3f6SDavid Disseldorp 
139054a6f3f6SDavid Disseldorp 	/*
139154a6f3f6SDavid Disseldorp 	 * Check to see if any active exports exist.  If they do exist, fail
139254a6f3f6SDavid Disseldorp 	 * here as changing this information on the fly (underneath the
139354a6f3f6SDavid Disseldorp 	 * initiator side OS dependent multipath code) could cause negative
139454a6f3f6SDavid Disseldorp 	 * effects.
139554a6f3f6SDavid Disseldorp 	 */
139654a6f3f6SDavid Disseldorp 	if (dev->export_count) {
139754a6f3f6SDavid Disseldorp 		pr_err("Unable to set T10 Vendor Identification while"
139854a6f3f6SDavid Disseldorp 			" active %d exports exist\n", dev->export_count);
139954a6f3f6SDavid Disseldorp 		return -EINVAL;
140054a6f3f6SDavid Disseldorp 	}
140154a6f3f6SDavid Disseldorp 
140254a6f3f6SDavid Disseldorp 	BUILD_BUG_ON(sizeof(dev->t10_wwn.vendor) != INQUIRY_VENDOR_LEN + 1);
140354a6f3f6SDavid Disseldorp 	strlcpy(dev->t10_wwn.vendor, stripped, sizeof(dev->t10_wwn.vendor));
140454a6f3f6SDavid Disseldorp 
140554a6f3f6SDavid Disseldorp 	pr_debug("Target_Core_ConfigFS: Set emulated T10 Vendor Identification:"
140654a6f3f6SDavid Disseldorp 		 " %s\n", dev->t10_wwn.vendor);
140754a6f3f6SDavid Disseldorp 
140854a6f3f6SDavid Disseldorp 	return count;
140954a6f3f6SDavid Disseldorp }
141054a6f3f6SDavid Disseldorp 
14110322913cSAlan Adamson static ssize_t target_wwn_product_id_show(struct config_item *item,
14120322913cSAlan Adamson 		char *page)
14130322913cSAlan Adamson {
14140322913cSAlan Adamson 	return sprintf(page, "%s\n", &to_t10_wwn(item)->model[0]);
14150322913cSAlan Adamson }
14160322913cSAlan Adamson 
14170322913cSAlan Adamson static ssize_t target_wwn_product_id_store(struct config_item *item,
14180322913cSAlan Adamson 		const char *page, size_t count)
14190322913cSAlan Adamson {
14200322913cSAlan Adamson 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
14210322913cSAlan Adamson 	struct se_device *dev = t10_wwn->t10_dev;
14220322913cSAlan Adamson 	/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
14230322913cSAlan Adamson 	unsigned char buf[INQUIRY_MODEL_LEN + 2];
14240322913cSAlan Adamson 	char *stripped = NULL;
1425ee26724aSColin Ian King 	size_t len;
1426ee26724aSColin Ian King 	ssize_t ret;
14270322913cSAlan Adamson 
14280322913cSAlan Adamson 	len = strlcpy(buf, page, sizeof(buf));
14290322913cSAlan Adamson 	if (len < sizeof(buf)) {
14300322913cSAlan Adamson 		/* Strip any newline added from userspace. */
14310322913cSAlan Adamson 		stripped = strstrip(buf);
14320322913cSAlan Adamson 		len = strlen(stripped);
14330322913cSAlan Adamson 	}
14340322913cSAlan Adamson 	if (len > INQUIRY_MODEL_LEN) {
14350322913cSAlan Adamson 		pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: "
14360322913cSAlan Adamson 			 __stringify(INQUIRY_MODEL_LEN)
14370322913cSAlan Adamson 			"\n");
14380322913cSAlan Adamson 		return -EOVERFLOW;
14390322913cSAlan Adamson 	}
14400322913cSAlan Adamson 
14410322913cSAlan Adamson 	ret = target_check_inquiry_data(stripped);
14420322913cSAlan Adamson 
14430322913cSAlan Adamson 	if (ret < 0)
14440322913cSAlan Adamson 		return ret;
14450322913cSAlan Adamson 
14460322913cSAlan Adamson 	/*
14470322913cSAlan Adamson 	 * Check to see if any active exports exist.  If they do exist, fail
14480322913cSAlan Adamson 	 * here as changing this information on the fly (underneath the
14490322913cSAlan Adamson 	 * initiator side OS dependent multipath code) could cause negative
14500322913cSAlan Adamson 	 * effects.
14510322913cSAlan Adamson 	 */
14520322913cSAlan Adamson 	if (dev->export_count) {
14530322913cSAlan Adamson 		pr_err("Unable to set T10 Model while active %d exports exist\n",
14540322913cSAlan Adamson 			dev->export_count);
14550322913cSAlan Adamson 		return -EINVAL;
14560322913cSAlan Adamson 	}
14570322913cSAlan Adamson 
14580322913cSAlan Adamson 	BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
14590322913cSAlan Adamson 	strlcpy(dev->t10_wwn.model, stripped, sizeof(dev->t10_wwn.model));
14600322913cSAlan Adamson 
14610322913cSAlan Adamson 	pr_debug("Target_Core_ConfigFS: Set emulated T10 Model Identification: %s\n",
14620322913cSAlan Adamson 		 dev->t10_wwn.model);
14630322913cSAlan Adamson 
14640322913cSAlan Adamson 	return count;
14650322913cSAlan Adamson }
14660322913cSAlan Adamson 
14670322913cSAlan Adamson static ssize_t target_wwn_revision_show(struct config_item *item,
14680322913cSAlan Adamson 		char *page)
14690322913cSAlan Adamson {
14700322913cSAlan Adamson 	return sprintf(page, "%s\n", &to_t10_wwn(item)->revision[0]);
14710322913cSAlan Adamson }
14720322913cSAlan Adamson 
14730322913cSAlan Adamson static ssize_t target_wwn_revision_store(struct config_item *item,
14740322913cSAlan Adamson 		const char *page, size_t count)
14750322913cSAlan Adamson {
14760322913cSAlan Adamson 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
14770322913cSAlan Adamson 	struct se_device *dev = t10_wwn->t10_dev;
14780322913cSAlan Adamson 	/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
14790322913cSAlan Adamson 	unsigned char buf[INQUIRY_REVISION_LEN + 2];
14800322913cSAlan Adamson 	char *stripped = NULL;
1481ee26724aSColin Ian King 	size_t len;
1482ee26724aSColin Ian King 	ssize_t ret;
14830322913cSAlan Adamson 
14840322913cSAlan Adamson 	len = strlcpy(buf, page, sizeof(buf));
14850322913cSAlan Adamson 	if (len < sizeof(buf)) {
14860322913cSAlan Adamson 		/* Strip any newline added from userspace. */
14870322913cSAlan Adamson 		stripped = strstrip(buf);
14880322913cSAlan Adamson 		len = strlen(stripped);
14890322913cSAlan Adamson 	}
14900322913cSAlan Adamson 	if (len > INQUIRY_REVISION_LEN) {
14910322913cSAlan Adamson 		pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: "
14920322913cSAlan Adamson 			 __stringify(INQUIRY_REVISION_LEN)
14930322913cSAlan Adamson 			"\n");
14940322913cSAlan Adamson 		return -EOVERFLOW;
14950322913cSAlan Adamson 	}
14960322913cSAlan Adamson 
14970322913cSAlan Adamson 	ret = target_check_inquiry_data(stripped);
14980322913cSAlan Adamson 
14990322913cSAlan Adamson 	if (ret < 0)
15000322913cSAlan Adamson 		return ret;
15010322913cSAlan Adamson 
15020322913cSAlan Adamson 	/*
15030322913cSAlan Adamson 	 * Check to see if any active exports exist.  If they do exist, fail
15040322913cSAlan Adamson 	 * here as changing this information on the fly (underneath the
15050322913cSAlan Adamson 	 * initiator side OS dependent multipath code) could cause negative
15060322913cSAlan Adamson 	 * effects.
15070322913cSAlan Adamson 	 */
15080322913cSAlan Adamson 	if (dev->export_count) {
15090322913cSAlan Adamson 		pr_err("Unable to set T10 Revision while active %d exports exist\n",
15100322913cSAlan Adamson 			dev->export_count);
15110322913cSAlan Adamson 		return -EINVAL;
15120322913cSAlan Adamson 	}
15130322913cSAlan Adamson 
15140322913cSAlan Adamson 	BUILD_BUG_ON(sizeof(dev->t10_wwn.revision) != INQUIRY_REVISION_LEN + 1);
15150322913cSAlan Adamson 	strlcpy(dev->t10_wwn.revision, stripped, sizeof(dev->t10_wwn.revision));
15160322913cSAlan Adamson 
15170322913cSAlan Adamson 	pr_debug("Target_Core_ConfigFS: Set emulated T10 Revision: %s\n",
15180322913cSAlan Adamson 		 dev->t10_wwn.revision);
15190322913cSAlan Adamson 
15200322913cSAlan Adamson 	return count;
15210322913cSAlan Adamson }
15220322913cSAlan Adamson 
15232469f1e0SSergey Samoylenko static ssize_t
15242469f1e0SSergey Samoylenko target_wwn_company_id_show(struct config_item *item,
15252469f1e0SSergey Samoylenko 				char *page)
15262469f1e0SSergey Samoylenko {
15272469f1e0SSergey Samoylenko 	return snprintf(page, PAGE_SIZE, "%#08x\n",
15282469f1e0SSergey Samoylenko 			to_t10_wwn(item)->company_id);
15292469f1e0SSergey Samoylenko }
15302469f1e0SSergey Samoylenko 
15312469f1e0SSergey Samoylenko static ssize_t
15322469f1e0SSergey Samoylenko target_wwn_company_id_store(struct config_item *item,
15332469f1e0SSergey Samoylenko 				 const char *page, size_t count)
15342469f1e0SSergey Samoylenko {
15352469f1e0SSergey Samoylenko 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
15362469f1e0SSergey Samoylenko 	struct se_device *dev = t10_wwn->t10_dev;
15372469f1e0SSergey Samoylenko 	u32 val;
15382469f1e0SSergey Samoylenko 	int ret;
15392469f1e0SSergey Samoylenko 
15402469f1e0SSergey Samoylenko 	/*
15412469f1e0SSergey Samoylenko 	 * The IEEE COMPANY_ID field should contain a 24-bit canonical
15422469f1e0SSergey Samoylenko 	 * form OUI assigned by the IEEE.
15432469f1e0SSergey Samoylenko 	 */
15442469f1e0SSergey Samoylenko 	ret = kstrtou32(page, 0, &val);
15452469f1e0SSergey Samoylenko 	if (ret < 0)
15462469f1e0SSergey Samoylenko 		return ret;
15472469f1e0SSergey Samoylenko 
15482469f1e0SSergey Samoylenko 	if (val >= 0x1000000)
15492469f1e0SSergey Samoylenko 		return -EOVERFLOW;
15502469f1e0SSergey Samoylenko 
15512469f1e0SSergey Samoylenko 	/*
15522469f1e0SSergey Samoylenko 	 * Check to see if any active exports exist. If they do exist, fail
15532469f1e0SSergey Samoylenko 	 * here as changing this information on the fly (underneath the
15542469f1e0SSergey Samoylenko 	 * initiator side OS dependent multipath code) could cause negative
15552469f1e0SSergey Samoylenko 	 * effects.
15562469f1e0SSergey Samoylenko 	 */
15572469f1e0SSergey Samoylenko 	if (dev->export_count) {
15582469f1e0SSergey Samoylenko 		pr_err("Unable to set Company ID while %u exports exist\n",
15592469f1e0SSergey Samoylenko 		       dev->export_count);
15602469f1e0SSergey Samoylenko 		return -EINVAL;
15612469f1e0SSergey Samoylenko 	}
15622469f1e0SSergey Samoylenko 
15632469f1e0SSergey Samoylenko 	t10_wwn->company_id = val;
15642469f1e0SSergey Samoylenko 
15652469f1e0SSergey Samoylenko 	pr_debug("Target_Core_ConfigFS: Set IEEE Company ID: %#08x\n",
15662469f1e0SSergey Samoylenko 		 t10_wwn->company_id);
15672469f1e0SSergey Samoylenko 
15682469f1e0SSergey Samoylenko 	return count;
15692469f1e0SSergey Samoylenko }
15702469f1e0SSergey Samoylenko 
157154a6f3f6SDavid Disseldorp /*
1572c66ac9dbSNicholas Bellinger  * VPD page 0x80 Unit serial
1573c66ac9dbSNicholas Bellinger  */
15742eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1575c66ac9dbSNicholas Bellinger 		char *page)
1576c66ac9dbSNicholas Bellinger {
1577c66ac9dbSNicholas Bellinger 	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
15782eafd729SChristoph Hellwig 		&to_t10_wwn(item)->unit_serial[0]);
1579c66ac9dbSNicholas Bellinger }
1580c66ac9dbSNicholas Bellinger 
15812eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
15822eafd729SChristoph Hellwig 		const char *page, size_t count)
1583c66ac9dbSNicholas Bellinger {
15842eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
15850fd97ccfSChristoph Hellwig 	struct se_device *dev = t10_wwn->t10_dev;
15862d4e2dafSChaitanya Kulkarni 	unsigned char buf[INQUIRY_VPD_SERIAL_LEN] = { };
1587c66ac9dbSNicholas Bellinger 
1588c66ac9dbSNicholas Bellinger 	/*
1589c66ac9dbSNicholas Bellinger 	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1590c66ac9dbSNicholas Bellinger 	 * from the struct scsi_device level firmware, do not allow
1591c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial to be emulated.
1592c66ac9dbSNicholas Bellinger 	 *
1593c66ac9dbSNicholas Bellinger 	 * Note this struct scsi_device could also be emulating VPD
1594c66ac9dbSNicholas Bellinger 	 * information from its drivers/scsi LLD.  But for now we assume
1595c66ac9dbSNicholas Bellinger 	 * it is doing 'the right thing' wrt a world wide unique
1596c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial Number that OS dependent multipath can depend on.
1597c66ac9dbSNicholas Bellinger 	 */
15980fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
15996708bb27SAndy Grover 		pr_err("Underlying SCSI device firmware provided VPD"
1600c66ac9dbSNicholas Bellinger 			" Unit Serial, ignoring request\n");
1601c66ac9dbSNicholas Bellinger 		return -EOPNOTSUPP;
1602c66ac9dbSNicholas Bellinger 	}
1603c66ac9dbSNicholas Bellinger 
160460d645a4SDan Carpenter 	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
16056708bb27SAndy Grover 		pr_err("Emulated VPD Unit Serial exceeds"
1606c66ac9dbSNicholas Bellinger 		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1607c66ac9dbSNicholas Bellinger 		return -EOVERFLOW;
1608c66ac9dbSNicholas Bellinger 	}
1609c66ac9dbSNicholas Bellinger 	/*
1610c66ac9dbSNicholas Bellinger 	 * Check to see if any active $FABRIC_MOD exports exist.  If they
1611c66ac9dbSNicholas Bellinger 	 * do exist, fail here as changing this information on the fly
1612c66ac9dbSNicholas Bellinger 	 * (underneath the initiator side OS dependent multipath code)
1613c66ac9dbSNicholas Bellinger 	 * could cause negative effects.
1614c66ac9dbSNicholas Bellinger 	 */
16150fd97ccfSChristoph Hellwig 	if (dev->export_count) {
16166708bb27SAndy Grover 		pr_err("Unable to set VPD Unit Serial while"
1617c66ac9dbSNicholas Bellinger 			" active %d $FABRIC_MOD exports exist\n",
16180fd97ccfSChristoph Hellwig 			dev->export_count);
1619c66ac9dbSNicholas Bellinger 		return -EINVAL;
1620c66ac9dbSNicholas Bellinger 	}
16210fd97ccfSChristoph Hellwig 
1622c66ac9dbSNicholas Bellinger 	/*
1623c66ac9dbSNicholas Bellinger 	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1624c66ac9dbSNicholas Bellinger 	 *
1625c66ac9dbSNicholas Bellinger 	 * Also, strip any newline added from the userspace
1626c66ac9dbSNicholas Bellinger 	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1627c66ac9dbSNicholas Bellinger 	 */
1628c66ac9dbSNicholas Bellinger 	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
16290fd97ccfSChristoph Hellwig 	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1630c66ac9dbSNicholas Bellinger 			"%s", strstrip(buf));
16310fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1632c66ac9dbSNicholas Bellinger 
16336708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
16340fd97ccfSChristoph Hellwig 			" %s\n", dev->t10_wwn.unit_serial);
1635c66ac9dbSNicholas Bellinger 
1636c66ac9dbSNicholas Bellinger 	return count;
1637c66ac9dbSNicholas Bellinger }
1638c66ac9dbSNicholas Bellinger 
1639c66ac9dbSNicholas Bellinger /*
1640c66ac9dbSNicholas Bellinger  * VPD page 0x83 Protocol Identifier
1641c66ac9dbSNicholas Bellinger  */
16422eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1643c66ac9dbSNicholas Bellinger 		char *page)
1644c66ac9dbSNicholas Bellinger {
16452eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1646c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;
16472d4e2dafSChaitanya Kulkarni 	unsigned char buf[VPD_TMP_BUF_SIZE] = { };
1648c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1649c66ac9dbSNicholas Bellinger 
1650c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);
1651c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
16526708bb27SAndy Grover 		if (!vpd->protocol_identifier_set)
1653c66ac9dbSNicholas Bellinger 			continue;
1654c66ac9dbSNicholas Bellinger 
1655c66ac9dbSNicholas Bellinger 		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1656c66ac9dbSNicholas Bellinger 
16576708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1658c66ac9dbSNicholas Bellinger 			break;
1659c66ac9dbSNicholas Bellinger 
1660c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1661c66ac9dbSNicholas Bellinger 	}
1662c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);
1663c66ac9dbSNicholas Bellinger 
1664c66ac9dbSNicholas Bellinger 	return len;
1665c66ac9dbSNicholas Bellinger }
1666c66ac9dbSNicholas Bellinger 
1667c66ac9dbSNicholas Bellinger /*
1668c66ac9dbSNicholas Bellinger  * Generic wrapper for dumping VPD identifiers by association.
1669c66ac9dbSNicholas Bellinger  */
1670c66ac9dbSNicholas Bellinger #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
16712eafd729SChristoph Hellwig static ssize_t target_wwn_##_name##_show(struct config_item *item,	\
1672c66ac9dbSNicholas Bellinger 		char *page)						\
1673c66ac9dbSNicholas Bellinger {									\
16742eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);			\
1675c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;						\
1676c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];				\
1677c66ac9dbSNicholas Bellinger 	ssize_t len = 0;						\
1678c66ac9dbSNicholas Bellinger 									\
1679c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);				\
1680c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
1681c66ac9dbSNicholas Bellinger 		if (vpd->association != _assoc)				\
1682c66ac9dbSNicholas Bellinger 			continue;					\
1683c66ac9dbSNicholas Bellinger 									\
1684c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1685c66ac9dbSNicholas Bellinger 		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
16866708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1687c66ac9dbSNicholas Bellinger 			break;						\
1688c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1689c66ac9dbSNicholas Bellinger 									\
1690c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1691c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
16926708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1693c66ac9dbSNicholas Bellinger 			break;						\
1694c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1695c66ac9dbSNicholas Bellinger 									\
1696c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1697c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
16986708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1699c66ac9dbSNicholas Bellinger 			break;						\
1700c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1701c66ac9dbSNicholas Bellinger 	}								\
1702c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);				\
1703c66ac9dbSNicholas Bellinger 									\
1704c66ac9dbSNicholas Bellinger 	return len;							\
1705c66ac9dbSNicholas Bellinger }
1706c66ac9dbSNicholas Bellinger 
17072eafd729SChristoph Hellwig /* VPD page 0x83 Association: Logical Unit */
1708c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
17092eafd729SChristoph Hellwig /* VPD page 0x83 Association: Target Port */
1710c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
17112eafd729SChristoph Hellwig /* VPD page 0x83 Association: SCSI Target Device */
1712c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1713c66ac9dbSNicholas Bellinger 
171454a6f3f6SDavid Disseldorp CONFIGFS_ATTR(target_wwn_, vendor_id);
17150322913cSAlan Adamson CONFIGFS_ATTR(target_wwn_, product_id);
17160322913cSAlan Adamson CONFIGFS_ATTR(target_wwn_, revision);
17172469f1e0SSergey Samoylenko CONFIGFS_ATTR(target_wwn_, company_id);
17182eafd729SChristoph Hellwig CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
17192eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
17202eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
17212eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
17222eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1723c66ac9dbSNicholas Bellinger 
1724c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
172554a6f3f6SDavid Disseldorp 	&target_wwn_attr_vendor_id,
17260322913cSAlan Adamson 	&target_wwn_attr_product_id,
17270322913cSAlan Adamson 	&target_wwn_attr_revision,
17282469f1e0SSergey Samoylenko 	&target_wwn_attr_company_id,
17292eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_unit_serial,
17302eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_protocol_identifier,
17312eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_logical_unit,
17322eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_target_port,
17332eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_scsi_target_device,
1734c66ac9dbSNicholas Bellinger 	NULL,
1735c66ac9dbSNicholas Bellinger };
1736c66ac9dbSNicholas Bellinger 
17372eafd729SChristoph Hellwig TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1738c66ac9dbSNicholas Bellinger 
1739f8d389c6SNicholas Bellinger /*  End functions for struct config_item_type tb_dev_wwn_cit */
1740c66ac9dbSNicholas Bellinger 
174191e2e39bSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_pr_cit */
1742c66ac9dbSNicholas Bellinger 
17432eafd729SChristoph Hellwig static struct se_device *pr_to_dev(struct config_item *item)
17442eafd729SChristoph Hellwig {
17452eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device,
17462eafd729SChristoph Hellwig 			dev_pr_group);
17472eafd729SChristoph Hellwig }
1748c66ac9dbSNicholas Bellinger 
1749d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1750d977f437SChristoph Hellwig 		char *page)
1751c66ac9dbSNicholas Bellinger {
1752c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1753c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
17542d4e2dafSChaitanya Kulkarni 	char i_buf[PR_REG_ISID_ID_LEN] = { };
1755c66ac9dbSNicholas Bellinger 
1756c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1757d977f437SChristoph Hellwig 	if (!pr_reg)
1758d977f437SChristoph Hellwig 		return sprintf(page, "No SPC-3 Reservation holder\n");
1759d977f437SChristoph Hellwig 
1760c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1761d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1762c66ac9dbSNicholas Bellinger 
1763d977f437SChristoph Hellwig 	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
176430c7ca93SDavid Disseldorp 		se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1765d2843c17SAndy Grover 		se_nacl->initiatorname, i_buf);
1766c66ac9dbSNicholas Bellinger }
1767c66ac9dbSNicholas Bellinger 
1768d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1769d977f437SChristoph Hellwig 		char *page)
1770c66ac9dbSNicholas Bellinger {
1771fae43461SBart Van Assche 	struct se_session *sess = dev->reservation_holder;
1772c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1773d977f437SChristoph Hellwig 	ssize_t len;
1774c66ac9dbSNicholas Bellinger 
1775fae43461SBart Van Assche 	if (sess) {
1776fae43461SBart Van Assche 		se_nacl = sess->se_node_acl;
1777d977f437SChristoph Hellwig 		len = sprintf(page,
1778d977f437SChristoph Hellwig 			      "SPC-2 Reservation: %s Initiator: %s\n",
177930c7ca93SDavid Disseldorp 			      se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1780c66ac9dbSNicholas Bellinger 			      se_nacl->initiatorname);
1781d977f437SChristoph Hellwig 	} else {
1782d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-2 Reservation holder\n");
1783d977f437SChristoph Hellwig 	}
1784d977f437SChristoph Hellwig 	return len;
1785c66ac9dbSNicholas Bellinger }
1786c66ac9dbSNicholas Bellinger 
17872eafd729SChristoph Hellwig static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1788c66ac9dbSNicholas Bellinger {
17892eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1790d977f437SChristoph Hellwig 	int ret;
1791c66ac9dbSNicholas Bellinger 
1792b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr)
1793b49d6f78SDavid Disseldorp 		return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1794b49d6f78SDavid Disseldorp 
179569088a04SBodo Stroesser 	if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1796d977f437SChristoph Hellwig 		return sprintf(page, "Passthrough\n");
1797c66ac9dbSNicholas Bellinger 
1798d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
1799d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1800d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc2_res(dev, page);
1801d977f437SChristoph Hellwig 	else
1802d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc3_res(dev, page);
1803d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1804d977f437SChristoph Hellwig 	return ret;
1805c66ac9dbSNicholas Bellinger }
1806c66ac9dbSNicholas Bellinger 
18072eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
18082eafd729SChristoph Hellwig 		char *page)
1809c66ac9dbSNicholas Bellinger {
18102eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1811c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1812c66ac9dbSNicholas Bellinger 
1813c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1814d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder) {
1815c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1816d977f437SChristoph Hellwig 	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1817c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: All Target"
1818c66ac9dbSNicholas Bellinger 			" Ports registration\n");
1819d977f437SChristoph Hellwig 	} else {
1820c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: Single"
1821c66ac9dbSNicholas Bellinger 			" Target Port registration\n");
1822d977f437SChristoph Hellwig 	}
1823c66ac9dbSNicholas Bellinger 
1824d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1825c66ac9dbSNicholas Bellinger 	return len;
1826c66ac9dbSNicholas Bellinger }
1827c66ac9dbSNicholas Bellinger 
18282eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
18292eafd729SChristoph Hellwig 		char *page)
1830c66ac9dbSNicholas Bellinger {
18312eafd729SChristoph Hellwig 	return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1832c66ac9dbSNicholas Bellinger }
1833c66ac9dbSNicholas Bellinger 
1834c66ac9dbSNicholas Bellinger 
18352eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
18362eafd729SChristoph Hellwig 		char *page)
1837c66ac9dbSNicholas Bellinger {
18382eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1839c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1840c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
1841c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
18429ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1843c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1844c66ac9dbSNicholas Bellinger 
1845c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1846c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
18476708bb27SAndy Grover 	if (!pr_reg) {
1848c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1849d977f437SChristoph Hellwig 		goto out_unlock;
1850c66ac9dbSNicholas Bellinger 	}
1851d977f437SChristoph Hellwig 
1852c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1853c66ac9dbSNicholas Bellinger 	se_tpg = se_nacl->se_tpg;
1854e3d6f909SAndy Grover 	tfo = se_tpg->se_tpg_tfo;
1855c66ac9dbSNicholas Bellinger 
1856c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: %s"
185730c7ca93SDavid Disseldorp 		" Target Node Endpoint: %s\n", tfo->fabric_name,
1858c66ac9dbSNicholas Bellinger 		tfo->tpg_get_wwn(se_tpg));
1859c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
186035d1efe8SMasanari Iida 		" Identifier Tag: %hu %s Portal Group Tag: %hu"
1861f2d30680SHannes Reinecke 		" %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
186230c7ca93SDavid Disseldorp 		tfo->fabric_name, tfo->tpg_get_tag(se_tpg),
186330c7ca93SDavid Disseldorp 		tfo->fabric_name, pr_reg->pr_aptpl_target_lun);
1864c66ac9dbSNicholas Bellinger 
1865d977f437SChristoph Hellwig out_unlock:
1866d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1867c66ac9dbSNicholas Bellinger 	return len;
1868c66ac9dbSNicholas Bellinger }
1869c66ac9dbSNicholas Bellinger 
1870c66ac9dbSNicholas Bellinger 
18712eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
18722eafd729SChristoph Hellwig 		char *page)
1873c66ac9dbSNicholas Bellinger {
18742eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
18759ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1876c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1877c66ac9dbSNicholas Bellinger 	unsigned char buf[384];
1878c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1879c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1880d2843c17SAndy Grover 	int reg_count = 0;
1881c66ac9dbSNicholas Bellinger 
1882c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1883c66ac9dbSNicholas Bellinger 
18840fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
18850fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1886c66ac9dbSNicholas Bellinger 			pr_reg_list) {
1887c66ac9dbSNicholas Bellinger 
1888c66ac9dbSNicholas Bellinger 		memset(buf, 0, 384);
1889c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1890c66ac9dbSNicholas Bellinger 		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1891d2843c17SAndy Grover 		core_pr_dump_initiator_port(pr_reg, i_buf,
1892c66ac9dbSNicholas Bellinger 					PR_REG_ISID_ID_LEN);
1893c66ac9dbSNicholas Bellinger 		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
189430c7ca93SDavid Disseldorp 			tfo->fabric_name,
1895d2843c17SAndy Grover 			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1896c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_generation);
1897c66ac9dbSNicholas Bellinger 
18986708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1899c66ac9dbSNicholas Bellinger 			break;
1900c66ac9dbSNicholas Bellinger 
1901c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1902c66ac9dbSNicholas Bellinger 		reg_count++;
1903c66ac9dbSNicholas Bellinger 	}
19040fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
1905c66ac9dbSNicholas Bellinger 
19066708bb27SAndy Grover 	if (!reg_count)
1907c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "None\n");
1908c66ac9dbSNicholas Bellinger 
1909c66ac9dbSNicholas Bellinger 	return len;
1910c66ac9dbSNicholas Bellinger }
1911c66ac9dbSNicholas Bellinger 
19122eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1913c66ac9dbSNicholas Bellinger {
19142eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1915c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1916c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1917c66ac9dbSNicholas Bellinger 
1918c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1919c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1920d977f437SChristoph Hellwig 	if (pr_reg) {
1921c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1922c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1923d977f437SChristoph Hellwig 	} else {
1924d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1925d977f437SChristoph Hellwig 	}
1926c66ac9dbSNicholas Bellinger 
1927d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1928c66ac9dbSNicholas Bellinger 	return len;
1929c66ac9dbSNicholas Bellinger }
1930c66ac9dbSNicholas Bellinger 
19312eafd729SChristoph Hellwig static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
1932c66ac9dbSNicholas Bellinger {
19332eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
19342eafd729SChristoph Hellwig 
1935b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr)
1936b49d6f78SDavid Disseldorp 		return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
193769088a04SBodo Stroesser 	if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1938d977f437SChristoph Hellwig 		return sprintf(page, "SPC_PASSTHROUGH\n");
1939b49d6f78SDavid Disseldorp 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1940d977f437SChristoph Hellwig 		return sprintf(page, "SPC2_RESERVATIONS\n");
1941b49d6f78SDavid Disseldorp 
1942d977f437SChristoph Hellwig 	return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1943c66ac9dbSNicholas Bellinger }
1944c66ac9dbSNicholas Bellinger 
19452eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
19462eafd729SChristoph Hellwig 		char *page)
1947c66ac9dbSNicholas Bellinger {
19482eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
19492eafd729SChristoph Hellwig 
1950b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
195169088a04SBodo Stroesser 	    (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1952c66ac9dbSNicholas Bellinger 		return 0;
1953c66ac9dbSNicholas Bellinger 
1954c66ac9dbSNicholas Bellinger 	return sprintf(page, "APTPL Bit Status: %s\n",
19550fd97ccfSChristoph Hellwig 		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1956c66ac9dbSNicholas Bellinger }
1957c66ac9dbSNicholas Bellinger 
19582eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
19592eafd729SChristoph Hellwig 		char *page)
1960c66ac9dbSNicholas Bellinger {
19612eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
19622eafd729SChristoph Hellwig 
1963b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
196469088a04SBodo Stroesser 	    (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1965c66ac9dbSNicholas Bellinger 		return 0;
1966c66ac9dbSNicholas Bellinger 
1967c66ac9dbSNicholas Bellinger 	return sprintf(page, "Ready to process PR APTPL metadata..\n");
1968c66ac9dbSNicholas Bellinger }
1969c66ac9dbSNicholas Bellinger 
1970c66ac9dbSNicholas Bellinger enum {
1971c66ac9dbSNicholas Bellinger 	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1972c66ac9dbSNicholas Bellinger 	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1973c66ac9dbSNicholas Bellinger 	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1974c66ac9dbSNicholas Bellinger 	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1975c66ac9dbSNicholas Bellinger };
1976c66ac9dbSNicholas Bellinger 
1977c66ac9dbSNicholas Bellinger static match_table_t tokens = {
1978c66ac9dbSNicholas Bellinger 	{Opt_initiator_fabric, "initiator_fabric=%s"},
1979c66ac9dbSNicholas Bellinger 	{Opt_initiator_node, "initiator_node=%s"},
1980c66ac9dbSNicholas Bellinger 	{Opt_initiator_sid, "initiator_sid=%s"},
1981c66ac9dbSNicholas Bellinger 	{Opt_sa_res_key, "sa_res_key=%s"},
1982c66ac9dbSNicholas Bellinger 	{Opt_res_holder, "res_holder=%d"},
1983c66ac9dbSNicholas Bellinger 	{Opt_res_type, "res_type=%d"},
1984c66ac9dbSNicholas Bellinger 	{Opt_res_scope, "res_scope=%d"},
1985c66ac9dbSNicholas Bellinger 	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1986a2db857bStangwenji 	{Opt_mapped_lun, "mapped_lun=%u"},
1987c66ac9dbSNicholas Bellinger 	{Opt_target_fabric, "target_fabric=%s"},
1988c66ac9dbSNicholas Bellinger 	{Opt_target_node, "target_node=%s"},
1989c66ac9dbSNicholas Bellinger 	{Opt_tpgt, "tpgt=%d"},
1990c66ac9dbSNicholas Bellinger 	{Opt_port_rtpi, "port_rtpi=%d"},
1991a2db857bStangwenji 	{Opt_target_lun, "target_lun=%u"},
1992c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
1993c66ac9dbSNicholas Bellinger };
1994c66ac9dbSNicholas Bellinger 
19952eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
19962eafd729SChristoph Hellwig 		const char *page, size_t count)
1997c66ac9dbSNicholas Bellinger {
19982eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
19996d180253SJesper Juhl 	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
20006d180253SJesper Juhl 	unsigned char *t_fabric = NULL, *t_port = NULL;
20018d213559SJoern Engel 	char *orig, *ptr, *opts;
2002c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
2003c66ac9dbSNicholas Bellinger 	unsigned long long tmp_ll;
2004c66ac9dbSNicholas Bellinger 	u64 sa_res_key = 0;
2005f2d30680SHannes Reinecke 	u64 mapped_lun = 0, target_lun = 0;
2006c66ac9dbSNicholas Bellinger 	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
200745fb94c2SBart Van Assche 	u16 tpgt = 0;
200845fb94c2SBart Van Assche 	u8 type = 0;
2009c66ac9dbSNicholas Bellinger 
2010b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
201169088a04SBodo Stroesser 	    (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
20129105bfc0SAndy Grover 		return count;
2013d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
20149105bfc0SAndy Grover 		return count;
2015c66ac9dbSNicholas Bellinger 
20160fd97ccfSChristoph Hellwig 	if (dev->export_count) {
20176708bb27SAndy Grover 		pr_debug("Unable to process APTPL metadata while"
2018c66ac9dbSNicholas Bellinger 			" active fabric exports exist\n");
2019c66ac9dbSNicholas Bellinger 		return -EINVAL;
2020c66ac9dbSNicholas Bellinger 	}
2021c66ac9dbSNicholas Bellinger 
2022c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
2023c66ac9dbSNicholas Bellinger 	if (!opts)
2024c66ac9dbSNicholas Bellinger 		return -ENOMEM;
2025c66ac9dbSNicholas Bellinger 
2026c66ac9dbSNicholas Bellinger 	orig = opts;
202790c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
2028c66ac9dbSNicholas Bellinger 		if (!*ptr)
2029c66ac9dbSNicholas Bellinger 			continue;
2030c66ac9dbSNicholas Bellinger 
2031c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
2032c66ac9dbSNicholas Bellinger 		switch (token) {
2033c66ac9dbSNicholas Bellinger 		case Opt_initiator_fabric:
20348d213559SJoern Engel 			i_fabric = match_strdup(args);
20356d180253SJesper Juhl 			if (!i_fabric) {
20366d180253SJesper Juhl 				ret = -ENOMEM;
20376d180253SJesper Juhl 				goto out;
20386d180253SJesper Juhl 			}
2039c66ac9dbSNicholas Bellinger 			break;
2040c66ac9dbSNicholas Bellinger 		case Opt_initiator_node:
20418d213559SJoern Engel 			i_port = match_strdup(args);
20426d180253SJesper Juhl 			if (!i_port) {
20436d180253SJesper Juhl 				ret = -ENOMEM;
20446d180253SJesper Juhl 				goto out;
20456d180253SJesper Juhl 			}
204660d645a4SDan Carpenter 			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
20476708bb27SAndy Grover 				pr_err("APTPL metadata initiator_node="
2048c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
2049c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_IPORT_LEN);
2050c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
2051c66ac9dbSNicholas Bellinger 				break;
2052c66ac9dbSNicholas Bellinger 			}
2053c66ac9dbSNicholas Bellinger 			break;
2054c66ac9dbSNicholas Bellinger 		case Opt_initiator_sid:
20558d213559SJoern Engel 			isid = match_strdup(args);
20566d180253SJesper Juhl 			if (!isid) {
20576d180253SJesper Juhl 				ret = -ENOMEM;
20586d180253SJesper Juhl 				goto out;
20596d180253SJesper Juhl 			}
206060d645a4SDan Carpenter 			if (strlen(isid) >= PR_REG_ISID_LEN) {
20616708bb27SAndy Grover 				pr_err("APTPL metadata initiator_isid"
2062c66ac9dbSNicholas Bellinger 					"= exceeds PR_REG_ISID_LEN: %d\n",
2063c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
2064c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
2065c66ac9dbSNicholas Bellinger 				break;
2066c66ac9dbSNicholas Bellinger 			}
2067c66ac9dbSNicholas Bellinger 			break;
2068c66ac9dbSNicholas Bellinger 		case Opt_sa_res_key:
2069a2db857bStangwenji 			ret = match_u64(args,  &tmp_ll);
2070c66ac9dbSNicholas Bellinger 			if (ret < 0) {
20718d213559SJoern Engel 				pr_err("kstrtoull() failed for sa_res_key=\n");
2072c66ac9dbSNicholas Bellinger 				goto out;
2073c66ac9dbSNicholas Bellinger 			}
2074c66ac9dbSNicholas Bellinger 			sa_res_key = (u64)tmp_ll;
2075c66ac9dbSNicholas Bellinger 			break;
2076c66ac9dbSNicholas Bellinger 		/*
2077c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Reservation
2078c66ac9dbSNicholas Bellinger 		 */
2079c66ac9dbSNicholas Bellinger 		case Opt_res_holder:
2080c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
2081c2091026SDavid Disseldorp 			if (ret)
2082c2091026SDavid Disseldorp 				goto out;
2083c66ac9dbSNicholas Bellinger 			res_holder = arg;
2084c66ac9dbSNicholas Bellinger 			break;
2085c66ac9dbSNicholas Bellinger 		case Opt_res_type:
2086c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
2087c2091026SDavid Disseldorp 			if (ret)
2088c2091026SDavid Disseldorp 				goto out;
2089c66ac9dbSNicholas Bellinger 			type = (u8)arg;
2090c66ac9dbSNicholas Bellinger 			break;
2091c66ac9dbSNicholas Bellinger 		case Opt_res_scope:
2092c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
2093c2091026SDavid Disseldorp 			if (ret)
2094c2091026SDavid Disseldorp 				goto out;
2095c66ac9dbSNicholas Bellinger 			break;
2096c66ac9dbSNicholas Bellinger 		case Opt_res_all_tg_pt:
2097c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
2098c2091026SDavid Disseldorp 			if (ret)
2099c2091026SDavid Disseldorp 				goto out;
2100c66ac9dbSNicholas Bellinger 			all_tg_pt = (int)arg;
2101c66ac9dbSNicholas Bellinger 			break;
2102c66ac9dbSNicholas Bellinger 		case Opt_mapped_lun:
2103a2db857bStangwenji 			ret = match_u64(args, &tmp_ll);
2104c2091026SDavid Disseldorp 			if (ret)
2105c2091026SDavid Disseldorp 				goto out;
2106a2db857bStangwenji 			mapped_lun = (u64)tmp_ll;
2107c66ac9dbSNicholas Bellinger 			break;
2108c66ac9dbSNicholas Bellinger 		/*
2109c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Target Port
2110c66ac9dbSNicholas Bellinger 		 */
2111c66ac9dbSNicholas Bellinger 		case Opt_target_fabric:
21128d213559SJoern Engel 			t_fabric = match_strdup(args);
21136d180253SJesper Juhl 			if (!t_fabric) {
21146d180253SJesper Juhl 				ret = -ENOMEM;
21156d180253SJesper Juhl 				goto out;
21166d180253SJesper Juhl 			}
2117c66ac9dbSNicholas Bellinger 			break;
2118c66ac9dbSNicholas Bellinger 		case Opt_target_node:
21198d213559SJoern Engel 			t_port = match_strdup(args);
21206d180253SJesper Juhl 			if (!t_port) {
21216d180253SJesper Juhl 				ret = -ENOMEM;
21226d180253SJesper Juhl 				goto out;
21236d180253SJesper Juhl 			}
212460d645a4SDan Carpenter 			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
21256708bb27SAndy Grover 				pr_err("APTPL metadata target_node="
2126c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
2127c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_TPORT_LEN);
2128c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
2129c66ac9dbSNicholas Bellinger 				break;
2130c66ac9dbSNicholas Bellinger 			}
2131c66ac9dbSNicholas Bellinger 			break;
2132c66ac9dbSNicholas Bellinger 		case Opt_tpgt:
2133c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
2134c2091026SDavid Disseldorp 			if (ret)
2135c2091026SDavid Disseldorp 				goto out;
2136c66ac9dbSNicholas Bellinger 			tpgt = (u16)arg;
2137c66ac9dbSNicholas Bellinger 			break;
2138c66ac9dbSNicholas Bellinger 		case Opt_port_rtpi:
2139c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
2140c2091026SDavid Disseldorp 			if (ret)
2141c2091026SDavid Disseldorp 				goto out;
2142c66ac9dbSNicholas Bellinger 			break;
2143c66ac9dbSNicholas Bellinger 		case Opt_target_lun:
2144a2db857bStangwenji 			ret = match_u64(args, &tmp_ll);
2145c2091026SDavid Disseldorp 			if (ret)
2146c2091026SDavid Disseldorp 				goto out;
2147a2db857bStangwenji 			target_lun = (u64)tmp_ll;
2148c66ac9dbSNicholas Bellinger 			break;
2149c66ac9dbSNicholas Bellinger 		default:
2150c66ac9dbSNicholas Bellinger 			break;
2151c66ac9dbSNicholas Bellinger 		}
2152c66ac9dbSNicholas Bellinger 	}
2153c66ac9dbSNicholas Bellinger 
21546708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
21556708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
2156c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
2157c66ac9dbSNicholas Bellinger 		goto out;
2158c66ac9dbSNicholas Bellinger 	}
2159c66ac9dbSNicholas Bellinger 
2160c66ac9dbSNicholas Bellinger 	if (res_holder && !(type)) {
21616708bb27SAndy Grover 		pr_err("Illegal PR type: 0x%02x for reservation"
2162c66ac9dbSNicholas Bellinger 				" holder\n", type);
2163c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
2164c66ac9dbSNicholas Bellinger 		goto out;
2165c66ac9dbSNicholas Bellinger 	}
2166c66ac9dbSNicholas Bellinger 
21670fd97ccfSChristoph Hellwig 	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
2168c66ac9dbSNicholas Bellinger 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
2169c66ac9dbSNicholas Bellinger 			res_holder, all_tg_pt, type);
2170c66ac9dbSNicholas Bellinger out:
21716d180253SJesper Juhl 	kfree(i_fabric);
21726d180253SJesper Juhl 	kfree(i_port);
21736d180253SJesper Juhl 	kfree(isid);
21746d180253SJesper Juhl 	kfree(t_fabric);
21756d180253SJesper Juhl 	kfree(t_port);
2176c66ac9dbSNicholas Bellinger 	kfree(orig);
2177c66ac9dbSNicholas Bellinger 	return (ret == 0) ? count : ret;
2178c66ac9dbSNicholas Bellinger }
2179c66ac9dbSNicholas Bellinger 
2180c66ac9dbSNicholas Bellinger 
21812eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_holder);
21822eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
21832eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
21842eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
21852eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
21862eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
21872eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_type);
21882eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
21892eafd729SChristoph Hellwig CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
2190c66ac9dbSNicholas Bellinger 
2191c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_pr_attrs[] = {
21922eafd729SChristoph Hellwig 	&target_pr_attr_res_holder,
21932eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_all_tgt_pts,
21942eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_generation,
21952eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_holder_tg_port,
21962eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_registered_i_pts,
21972eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_type,
21982eafd729SChristoph Hellwig 	&target_pr_attr_res_type,
21992eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_active,
22002eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_metadata,
2201c66ac9dbSNicholas Bellinger 	NULL,
2202c66ac9dbSNicholas Bellinger };
2203c66ac9dbSNicholas Bellinger 
22042eafd729SChristoph Hellwig TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
2205c66ac9dbSNicholas Bellinger 
220691e2e39bSNicholas Bellinger /*  End functions for struct config_item_type tb_dev_pr_cit */
2207c66ac9dbSNicholas Bellinger 
220873112edcSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_cit */
2209c66ac9dbSNicholas Bellinger 
22102eafd729SChristoph Hellwig static inline struct se_device *to_device(struct config_item *item)
2211c66ac9dbSNicholas Bellinger {
22122eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device, dev_group);
22132eafd729SChristoph Hellwig }
22142eafd729SChristoph Hellwig 
22152eafd729SChristoph Hellwig static ssize_t target_dev_info_show(struct config_item *item, char *page)
22162eafd729SChristoph Hellwig {
22172eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2218c66ac9dbSNicholas Bellinger 	int bl = 0;
2219c66ac9dbSNicholas Bellinger 	ssize_t read_bytes = 0;
2220c66ac9dbSNicholas Bellinger 
22210fd97ccfSChristoph Hellwig 	transport_dump_dev_state(dev, page, &bl);
2222c66ac9dbSNicholas Bellinger 	read_bytes += bl;
22230a06d430SChristoph Hellwig 	read_bytes += dev->transport->show_configfs_dev_params(dev,
22240a06d430SChristoph Hellwig 			page+read_bytes);
2225c66ac9dbSNicholas Bellinger 	return read_bytes;
2226c66ac9dbSNicholas Bellinger }
2227c66ac9dbSNicholas Bellinger 
22282eafd729SChristoph Hellwig static ssize_t target_dev_control_store(struct config_item *item,
22292eafd729SChristoph Hellwig 		const char *page, size_t count)
2230c66ac9dbSNicholas Bellinger {
22312eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2232c66ac9dbSNicholas Bellinger 
22330a06d430SChristoph Hellwig 	return dev->transport->set_configfs_dev_params(dev, page, count);
2234c66ac9dbSNicholas Bellinger }
2235c66ac9dbSNicholas Bellinger 
22362eafd729SChristoph Hellwig static ssize_t target_dev_alias_show(struct config_item *item, char *page)
2237c66ac9dbSNicholas Bellinger {
22382eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2239c66ac9dbSNicholas Bellinger 
22400fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_ALIAS))
2241c66ac9dbSNicholas Bellinger 		return 0;
2242c66ac9dbSNicholas Bellinger 
22430fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
2244c66ac9dbSNicholas Bellinger }
2245c66ac9dbSNicholas Bellinger 
22462eafd729SChristoph Hellwig static ssize_t target_dev_alias_store(struct config_item *item,
22472eafd729SChristoph Hellwig 		const char *page, size_t count)
2248c66ac9dbSNicholas Bellinger {
22492eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
22500fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
2251c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
2252c66ac9dbSNicholas Bellinger 
2253c66ac9dbSNicholas Bellinger 	if (count > (SE_DEV_ALIAS_LEN-1)) {
22546708bb27SAndy Grover 		pr_err("alias count: %d exceeds"
2255c66ac9dbSNicholas Bellinger 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
2256c66ac9dbSNicholas Bellinger 			SE_DEV_ALIAS_LEN-1);
2257c66ac9dbSNicholas Bellinger 		return -EINVAL;
2258c66ac9dbSNicholas Bellinger 	}
2259c66ac9dbSNicholas Bellinger 
22600fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
22613011684cSDan Carpenter 	if (!read_bytes)
22623011684cSDan Carpenter 		return -EINVAL;
22630fd97ccfSChristoph Hellwig 	if (dev->dev_alias[read_bytes - 1] == '\n')
22640fd97ccfSChristoph Hellwig 		dev->dev_alias[read_bytes - 1] = '\0';
22650877eafdSSebastian Andrzej Siewior 
22660fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_ALIAS;
22673011684cSDan Carpenter 
22686708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
2269c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
22700fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
22710fd97ccfSChristoph Hellwig 		dev->dev_alias);
2272c66ac9dbSNicholas Bellinger 
2273c66ac9dbSNicholas Bellinger 	return read_bytes;
2274c66ac9dbSNicholas Bellinger }
2275c66ac9dbSNicholas Bellinger 
22762eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
2277c66ac9dbSNicholas Bellinger {
22782eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2279c66ac9dbSNicholas Bellinger 
22800fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
2281c66ac9dbSNicholas Bellinger 		return 0;
2282c66ac9dbSNicholas Bellinger 
22830fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
2284c66ac9dbSNicholas Bellinger }
2285c66ac9dbSNicholas Bellinger 
22862eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_store(struct config_item *item,
22872eafd729SChristoph Hellwig 		const char *page, size_t count)
2288c66ac9dbSNicholas Bellinger {
22892eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
22900fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
2291c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
2292c66ac9dbSNicholas Bellinger 
2293c66ac9dbSNicholas Bellinger 	if (count > (SE_UDEV_PATH_LEN-1)) {
22946708bb27SAndy Grover 		pr_err("udev_path count: %d exceeds"
2295c66ac9dbSNicholas Bellinger 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
2296c66ac9dbSNicholas Bellinger 			SE_UDEV_PATH_LEN-1);
2297c66ac9dbSNicholas Bellinger 		return -EINVAL;
2298c66ac9dbSNicholas Bellinger 	}
2299c66ac9dbSNicholas Bellinger 
23000fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
2301c66ac9dbSNicholas Bellinger 			"%s", page);
23023011684cSDan Carpenter 	if (!read_bytes)
23033011684cSDan Carpenter 		return -EINVAL;
23040fd97ccfSChristoph Hellwig 	if (dev->udev_path[read_bytes - 1] == '\n')
23050fd97ccfSChristoph Hellwig 		dev->udev_path[read_bytes - 1] = '\0';
23060877eafdSSebastian Andrzej Siewior 
23070fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_UDEV_PATH;
23083011684cSDan Carpenter 
23096708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
2310c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
23110fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
23120fd97ccfSChristoph Hellwig 		dev->udev_path);
2313c66ac9dbSNicholas Bellinger 
2314c66ac9dbSNicholas Bellinger 	return read_bytes;
2315c66ac9dbSNicholas Bellinger }
2316c66ac9dbSNicholas Bellinger 
23172eafd729SChristoph Hellwig static ssize_t target_dev_enable_show(struct config_item *item, char *page)
231864146db7SAndy Grover {
23192eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
232064146db7SAndy Grover 
2321cb0f32e1SMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n", target_dev_configured(dev));
232264146db7SAndy Grover }
232364146db7SAndy Grover 
23242eafd729SChristoph Hellwig static ssize_t target_dev_enable_store(struct config_item *item,
23252eafd729SChristoph Hellwig 		const char *page, size_t count)
2326c66ac9dbSNicholas Bellinger {
23272eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2328c66ac9dbSNicholas Bellinger 	char *ptr;
23290fd97ccfSChristoph Hellwig 	int ret;
2330c66ac9dbSNicholas Bellinger 
2331c66ac9dbSNicholas Bellinger 	ptr = strstr(page, "1");
23326708bb27SAndy Grover 	if (!ptr) {
23336708bb27SAndy Grover 		pr_err("For dev_enable ops, only valid value"
2334c66ac9dbSNicholas Bellinger 				" is \"1\"\n");
2335c66ac9dbSNicholas Bellinger 		return -EINVAL;
2336c66ac9dbSNicholas Bellinger 	}
2337c66ac9dbSNicholas Bellinger 
23380fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
23390fd97ccfSChristoph Hellwig 	if (ret)
23400fd97ccfSChristoph Hellwig 		return ret;
2341c66ac9dbSNicholas Bellinger 	return count;
2342c66ac9dbSNicholas Bellinger }
2343c66ac9dbSNicholas Bellinger 
23442eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
2345c66ac9dbSNicholas Bellinger {
23462eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2347c66ac9dbSNicholas Bellinger 	struct config_item *lu_ci;
2348c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2349c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2350c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
2351c66ac9dbSNicholas Bellinger 
2352c66ac9dbSNicholas Bellinger 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
2353c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
2354c87fbd56SChristoph Hellwig 		return 0;
2355c66ac9dbSNicholas Bellinger 
2356c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2357c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
23586708bb27SAndy Grover 	if (lu_gp) {
2359c66ac9dbSNicholas Bellinger 		lu_ci = &lu_gp->lu_gp_group.cg_item;
2360c66ac9dbSNicholas Bellinger 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
2361c66ac9dbSNicholas Bellinger 			config_item_name(lu_ci), lu_gp->lu_gp_id);
2362c66ac9dbSNicholas Bellinger 	}
2363c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2364c66ac9dbSNicholas Bellinger 
2365c66ac9dbSNicholas Bellinger 	return len;
2366c66ac9dbSNicholas Bellinger }
2367c66ac9dbSNicholas Bellinger 
23682eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
23692eafd729SChristoph Hellwig 		const char *page, size_t count)
2370c66ac9dbSNicholas Bellinger {
23712eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
23720fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
2373c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
2374c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
23752d4e2dafSChaitanya Kulkarni 	unsigned char buf[LU_GROUP_NAME_BUF] = { };
2376c66ac9dbSNicholas Bellinger 	int move = 0;
2377c66ac9dbSNicholas Bellinger 
2378c87fbd56SChristoph Hellwig 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
2379c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
23809105bfc0SAndy Grover 		return count;
2381c87fbd56SChristoph Hellwig 
2382c66ac9dbSNicholas Bellinger 	if (count > LU_GROUP_NAME_BUF) {
23836708bb27SAndy Grover 		pr_err("ALUA LU Group Alias too large!\n");
2384c66ac9dbSNicholas Bellinger 		return -EINVAL;
2385c66ac9dbSNicholas Bellinger 	}
2386c66ac9dbSNicholas Bellinger 	memcpy(buf, page, count);
2387c66ac9dbSNicholas Bellinger 	/*
2388c66ac9dbSNicholas Bellinger 	 * Any ALUA logical unit alias besides "NULL" means we will be
2389c66ac9dbSNicholas Bellinger 	 * making a new group association.
2390c66ac9dbSNicholas Bellinger 	 */
2391c66ac9dbSNicholas Bellinger 	if (strcmp(strstrip(buf), "NULL")) {
2392c66ac9dbSNicholas Bellinger 		/*
2393c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name() will increment reference to
2394c66ac9dbSNicholas Bellinger 		 * struct t10_alua_lu_gp.  This reference is released with
2395c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name below().
2396c66ac9dbSNicholas Bellinger 		 */
2397c66ac9dbSNicholas Bellinger 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
23986708bb27SAndy Grover 		if (!lu_gp_new)
2399c66ac9dbSNicholas Bellinger 			return -ENODEV;
2400c66ac9dbSNicholas Bellinger 	}
2401c66ac9dbSNicholas Bellinger 
2402c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2403c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
24046708bb27SAndy Grover 	if (lu_gp) {
2405c66ac9dbSNicholas Bellinger 		/*
2406c66ac9dbSNicholas Bellinger 		 * Clearing an existing lu_gp association, and replacing
2407c66ac9dbSNicholas Bellinger 		 * with NULL
2408c66ac9dbSNicholas Bellinger 		 */
24096708bb27SAndy Grover 		if (!lu_gp_new) {
24106708bb27SAndy Grover 			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
2411c66ac9dbSNicholas Bellinger 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2412c66ac9dbSNicholas Bellinger 				" %hu\n",
2413c66ac9dbSNicholas Bellinger 				config_item_name(&hba->hba_group.cg_item),
24140fd97ccfSChristoph Hellwig 				config_item_name(&dev->dev_group.cg_item),
2415c66ac9dbSNicholas Bellinger 				config_item_name(&lu_gp->lu_gp_group.cg_item),
2416c66ac9dbSNicholas Bellinger 				lu_gp->lu_gp_id);
2417c66ac9dbSNicholas Bellinger 
2418c66ac9dbSNicholas Bellinger 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2419c66ac9dbSNicholas Bellinger 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2420c66ac9dbSNicholas Bellinger 
2421c66ac9dbSNicholas Bellinger 			return count;
2422c66ac9dbSNicholas Bellinger 		}
2423c66ac9dbSNicholas Bellinger 		/*
2424c66ac9dbSNicholas Bellinger 		 * Removing existing association of lu_gp_mem with lu_gp
2425c66ac9dbSNicholas Bellinger 		 */
2426c66ac9dbSNicholas Bellinger 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2427c66ac9dbSNicholas Bellinger 		move = 1;
2428c66ac9dbSNicholas Bellinger 	}
2429c66ac9dbSNicholas Bellinger 	/*
2430c66ac9dbSNicholas Bellinger 	 * Associate lu_gp_mem with lu_gp_new.
2431c66ac9dbSNicholas Bellinger 	 */
2432c66ac9dbSNicholas Bellinger 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2433c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2434c66ac9dbSNicholas Bellinger 
24356708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
2436c66ac9dbSNicholas Bellinger 		" core/alua/lu_gps/%s, ID: %hu\n",
2437c66ac9dbSNicholas Bellinger 		(move) ? "Moving" : "Adding",
2438c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
24390fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
2440c66ac9dbSNicholas Bellinger 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2441c66ac9dbSNicholas Bellinger 		lu_gp_new->lu_gp_id);
2442c66ac9dbSNicholas Bellinger 
2443c66ac9dbSNicholas Bellinger 	core_alua_put_lu_gp_from_name(lu_gp_new);
2444c66ac9dbSNicholas Bellinger 	return count;
2445c66ac9dbSNicholas Bellinger }
2446c66ac9dbSNicholas Bellinger 
24472eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
2448229d4f11SHannes Reinecke {
24492eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2450229d4f11SHannes Reinecke 	struct t10_alua_lba_map *map;
2451229d4f11SHannes Reinecke 	struct t10_alua_lba_map_member *mem;
2452229d4f11SHannes Reinecke 	char *b = page;
2453229d4f11SHannes Reinecke 	int bl = 0;
2454229d4f11SHannes Reinecke 	char state;
2455229d4f11SHannes Reinecke 
2456229d4f11SHannes Reinecke 	spin_lock(&dev->t10_alua.lba_map_lock);
2457229d4f11SHannes Reinecke 	if (!list_empty(&dev->t10_alua.lba_map_list))
2458229d4f11SHannes Reinecke 	    bl += sprintf(b + bl, "%u %u\n",
2459229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_size,
2460229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_multiplier);
2461229d4f11SHannes Reinecke 	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2462229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "%llu %llu",
2463229d4f11SHannes Reinecke 			      map->lba_map_first_lba, map->lba_map_last_lba);
2464229d4f11SHannes Reinecke 		list_for_each_entry(mem, &map->lba_map_mem_list,
2465229d4f11SHannes Reinecke 				    lba_map_mem_list) {
2466229d4f11SHannes Reinecke 			switch (mem->lba_map_mem_alua_state) {
2467229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2468229d4f11SHannes Reinecke 				state = 'O';
2469229d4f11SHannes Reinecke 				break;
2470229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2471229d4f11SHannes Reinecke 				state = 'A';
2472229d4f11SHannes Reinecke 				break;
2473229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_STANDBY:
2474229d4f11SHannes Reinecke 				state = 'S';
2475229d4f11SHannes Reinecke 				break;
2476229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_UNAVAILABLE:
2477229d4f11SHannes Reinecke 				state = 'U';
2478229d4f11SHannes Reinecke 				break;
2479229d4f11SHannes Reinecke 			default:
2480229d4f11SHannes Reinecke 				state = '.';
2481229d4f11SHannes Reinecke 				break;
2482229d4f11SHannes Reinecke 			}
2483229d4f11SHannes Reinecke 			bl += sprintf(b + bl, " %d:%c",
2484229d4f11SHannes Reinecke 				      mem->lba_map_mem_alua_pg_id, state);
2485229d4f11SHannes Reinecke 		}
2486229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "\n");
2487229d4f11SHannes Reinecke 	}
2488229d4f11SHannes Reinecke 	spin_unlock(&dev->t10_alua.lba_map_lock);
2489229d4f11SHannes Reinecke 	return bl;
2490229d4f11SHannes Reinecke }
2491229d4f11SHannes Reinecke 
24922eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_store(struct config_item *item,
24932eafd729SChristoph Hellwig 		const char *page, size_t count)
2494229d4f11SHannes Reinecke {
24952eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2496229d4f11SHannes Reinecke 	struct t10_alua_lba_map *lba_map = NULL;
2497229d4f11SHannes Reinecke 	struct list_head lba_list;
2498f0a8afecSBart Van Assche 	char *map_entries, *orig, *ptr;
2499229d4f11SHannes Reinecke 	char state;
2500229d4f11SHannes Reinecke 	int pg_num = -1, pg;
2501229d4f11SHannes Reinecke 	int ret = 0, num = 0, pg_id, alua_state;
2502229d4f11SHannes Reinecke 	unsigned long start_lba = -1, end_lba = -1;
2503229d4f11SHannes Reinecke 	unsigned long segment_size = -1, segment_mult = -1;
2504229d4f11SHannes Reinecke 
2505f0a8afecSBart Van Assche 	orig = map_entries = kstrdup(page, GFP_KERNEL);
2506229d4f11SHannes Reinecke 	if (!map_entries)
2507229d4f11SHannes Reinecke 		return -ENOMEM;
2508229d4f11SHannes Reinecke 
2509229d4f11SHannes Reinecke 	INIT_LIST_HEAD(&lba_list);
2510229d4f11SHannes Reinecke 	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2511229d4f11SHannes Reinecke 		if (!*ptr)
2512229d4f11SHannes Reinecke 			continue;
2513229d4f11SHannes Reinecke 
2514229d4f11SHannes Reinecke 		if (num == 0) {
2515229d4f11SHannes Reinecke 			if (sscanf(ptr, "%lu %lu\n",
2516229d4f11SHannes Reinecke 				   &segment_size, &segment_mult) != 2) {
2517229d4f11SHannes Reinecke 				pr_err("Invalid line %d\n", num);
2518229d4f11SHannes Reinecke 				ret = -EINVAL;
2519229d4f11SHannes Reinecke 				break;
2520229d4f11SHannes Reinecke 			}
2521229d4f11SHannes Reinecke 			num++;
2522229d4f11SHannes Reinecke 			continue;
2523229d4f11SHannes Reinecke 		}
2524229d4f11SHannes Reinecke 		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2525229d4f11SHannes Reinecke 			pr_err("Invalid line %d\n", num);
2526229d4f11SHannes Reinecke 			ret = -EINVAL;
2527229d4f11SHannes Reinecke 			break;
2528229d4f11SHannes Reinecke 		}
2529229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2530229d4f11SHannes Reinecke 		if (!ptr) {
2531229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing end lba\n", num);
2532229d4f11SHannes Reinecke 			ret = -EINVAL;
2533229d4f11SHannes Reinecke 			break;
2534229d4f11SHannes Reinecke 		}
2535229d4f11SHannes Reinecke 		ptr++;
2536229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2537229d4f11SHannes Reinecke 		if (!ptr) {
2538229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing state definitions\n",
2539229d4f11SHannes Reinecke 			       num);
2540229d4f11SHannes Reinecke 			ret = -EINVAL;
2541229d4f11SHannes Reinecke 			break;
2542229d4f11SHannes Reinecke 		}
2543229d4f11SHannes Reinecke 		ptr++;
2544229d4f11SHannes Reinecke 		lba_map = core_alua_allocate_lba_map(&lba_list,
2545229d4f11SHannes Reinecke 						     start_lba, end_lba);
2546229d4f11SHannes Reinecke 		if (IS_ERR(lba_map)) {
2547229d4f11SHannes Reinecke 			ret = PTR_ERR(lba_map);
2548229d4f11SHannes Reinecke 			break;
2549229d4f11SHannes Reinecke 		}
2550229d4f11SHannes Reinecke 		pg = 0;
2551229d4f11SHannes Reinecke 		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2552229d4f11SHannes Reinecke 			switch (state) {
2553229d4f11SHannes Reinecke 			case 'O':
2554229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2555229d4f11SHannes Reinecke 				break;
2556229d4f11SHannes Reinecke 			case 'A':
2557229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2558229d4f11SHannes Reinecke 				break;
2559229d4f11SHannes Reinecke 			case 'S':
2560229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_STANDBY;
2561229d4f11SHannes Reinecke 				break;
2562229d4f11SHannes Reinecke 			case 'U':
2563229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2564229d4f11SHannes Reinecke 				break;
2565229d4f11SHannes Reinecke 			default:
2566229d4f11SHannes Reinecke 				pr_err("Invalid ALUA state '%c'\n", state);
2567229d4f11SHannes Reinecke 				ret = -EINVAL;
2568229d4f11SHannes Reinecke 				goto out;
2569229d4f11SHannes Reinecke 			}
2570229d4f11SHannes Reinecke 
2571229d4f11SHannes Reinecke 			ret = core_alua_allocate_lba_map_mem(lba_map,
2572229d4f11SHannes Reinecke 							     pg_id, alua_state);
2573229d4f11SHannes Reinecke 			if (ret) {
2574229d4f11SHannes Reinecke 				pr_err("Invalid target descriptor %d:%c "
2575229d4f11SHannes Reinecke 				       "at line %d\n",
2576229d4f11SHannes Reinecke 				       pg_id, state, num);
2577229d4f11SHannes Reinecke 				break;
2578229d4f11SHannes Reinecke 			}
2579229d4f11SHannes Reinecke 			pg++;
2580229d4f11SHannes Reinecke 			ptr = strchr(ptr, ' ');
2581229d4f11SHannes Reinecke 			if (ptr)
2582229d4f11SHannes Reinecke 				ptr++;
2583229d4f11SHannes Reinecke 			else
2584229d4f11SHannes Reinecke 				break;
2585229d4f11SHannes Reinecke 		}
2586229d4f11SHannes Reinecke 		if (pg_num == -1)
2587229d4f11SHannes Reinecke 		    pg_num = pg;
2588229d4f11SHannes Reinecke 		else if (pg != pg_num) {
2589229d4f11SHannes Reinecke 			pr_err("Only %d from %d port groups definitions "
2590229d4f11SHannes Reinecke 			       "at line %d\n", pg, pg_num, num);
2591229d4f11SHannes Reinecke 			ret = -EINVAL;
2592229d4f11SHannes Reinecke 			break;
2593229d4f11SHannes Reinecke 		}
2594229d4f11SHannes Reinecke 		num++;
2595229d4f11SHannes Reinecke 	}
2596229d4f11SHannes Reinecke out:
2597229d4f11SHannes Reinecke 	if (ret) {
2598229d4f11SHannes Reinecke 		core_alua_free_lba_map(&lba_list);
2599229d4f11SHannes Reinecke 		count = ret;
2600229d4f11SHannes Reinecke 	} else
2601229d4f11SHannes Reinecke 		core_alua_set_lba_map(dev, &lba_list,
2602229d4f11SHannes Reinecke 				      segment_size, segment_mult);
2603f0a8afecSBart Van Assche 	kfree(orig);
2604229d4f11SHannes Reinecke 	return count;
2605229d4f11SHannes Reinecke }
2606229d4f11SHannes Reinecke 
26072eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_dev_, info);
26082eafd729SChristoph Hellwig CONFIGFS_ATTR_WO(target_dev_, control);
26092eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alias);
26102eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, udev_path);
26112eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, enable);
26122eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alua_lu_gp);
26132eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, lba_map);
2614229d4f11SHannes Reinecke 
261573112edcSNicholas Bellinger static struct configfs_attribute *target_core_dev_attrs[] = {
26162eafd729SChristoph Hellwig 	&target_dev_attr_info,
26172eafd729SChristoph Hellwig 	&target_dev_attr_control,
26182eafd729SChristoph Hellwig 	&target_dev_attr_alias,
26192eafd729SChristoph Hellwig 	&target_dev_attr_udev_path,
26202eafd729SChristoph Hellwig 	&target_dev_attr_enable,
26212eafd729SChristoph Hellwig 	&target_dev_attr_alua_lu_gp,
26222eafd729SChristoph Hellwig 	&target_dev_attr_lba_map,
2623c66ac9dbSNicholas Bellinger 	NULL,
2624c66ac9dbSNicholas Bellinger };
2625c66ac9dbSNicholas Bellinger 
2626c66ac9dbSNicholas Bellinger static void target_core_dev_release(struct config_item *item)
2627c66ac9dbSNicholas Bellinger {
26280fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
26290fd97ccfSChristoph Hellwig 	struct se_device *dev =
26300fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2631c66ac9dbSNicholas Bellinger 
26320fd97ccfSChristoph Hellwig 	target_free_device(dev);
2633c66ac9dbSNicholas Bellinger }
2634c66ac9dbSNicholas Bellinger 
2635c17cd249SNicholas Bellinger /*
2636c17cd249SNicholas Bellinger  * Used in target_core_fabric_configfs.c to verify valid se_device symlink
2637c17cd249SNicholas Bellinger  * within target_fabric_port_link()
2638c17cd249SNicholas Bellinger  */
2639c17cd249SNicholas Bellinger struct configfs_item_operations target_core_dev_item_ops = {
2640c66ac9dbSNicholas Bellinger 	.release		= target_core_dev_release,
2641c66ac9dbSNicholas Bellinger };
2642c66ac9dbSNicholas Bellinger 
264373112edcSNicholas Bellinger TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2644c66ac9dbSNicholas Bellinger 
264573112edcSNicholas Bellinger /* End functions for struct config_item_type tb_dev_cit */
2646c66ac9dbSNicholas Bellinger 
2647c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2648c66ac9dbSNicholas Bellinger 
26492eafd729SChristoph Hellwig static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2650c66ac9dbSNicholas Bellinger {
26512eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_lu_gp,
26522eafd729SChristoph Hellwig 			lu_gp_group);
26532eafd729SChristoph Hellwig }
26542eafd729SChristoph Hellwig 
26552eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
26562eafd729SChristoph Hellwig {
26572eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
26582eafd729SChristoph Hellwig 
26596708bb27SAndy Grover 	if (!lu_gp->lu_gp_valid_id)
2660c66ac9dbSNicholas Bellinger 		return 0;
2661c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2662c66ac9dbSNicholas Bellinger }
2663c66ac9dbSNicholas Bellinger 
26642eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
26652eafd729SChristoph Hellwig 		const char *page, size_t count)
2666c66ac9dbSNicholas Bellinger {
26672eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2668c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2669c66ac9dbSNicholas Bellinger 	unsigned long lu_gp_id;
2670c66ac9dbSNicholas Bellinger 	int ret;
2671c66ac9dbSNicholas Bellinger 
267257103d7fSJingoo Han 	ret = kstrtoul(page, 0, &lu_gp_id);
2673c66ac9dbSNicholas Bellinger 	if (ret < 0) {
267457103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2675c66ac9dbSNicholas Bellinger 			" lu_gp_id\n", ret);
267657103d7fSJingoo Han 		return ret;
2677c66ac9dbSNicholas Bellinger 	}
2678c66ac9dbSNicholas Bellinger 	if (lu_gp_id > 0x0000ffff) {
26796708bb27SAndy Grover 		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2680c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", lu_gp_id);
2681c66ac9dbSNicholas Bellinger 		return -EINVAL;
2682c66ac9dbSNicholas Bellinger 	}
2683c66ac9dbSNicholas Bellinger 
2684c66ac9dbSNicholas Bellinger 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2685c66ac9dbSNicholas Bellinger 	if (ret < 0)
2686c66ac9dbSNicholas Bellinger 		return -EINVAL;
2687c66ac9dbSNicholas Bellinger 
26886708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2689c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
2690c66ac9dbSNicholas Bellinger 		config_item_name(&alua_lu_gp_cg->cg_item),
2691c66ac9dbSNicholas Bellinger 		lu_gp->lu_gp_id);
2692c66ac9dbSNicholas Bellinger 
2693c66ac9dbSNicholas Bellinger 	return count;
2694c66ac9dbSNicholas Bellinger }
2695c66ac9dbSNicholas Bellinger 
26962eafd729SChristoph Hellwig static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2697c66ac9dbSNicholas Bellinger {
26982eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2699c66ac9dbSNicholas Bellinger 	struct se_device *dev;
2700c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2701c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2702c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
27032d4e2dafSChaitanya Kulkarni 	unsigned char buf[LU_GROUP_NAME_BUF] = { };
2704c66ac9dbSNicholas Bellinger 
2705c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp->lu_gp_lock);
2706c66ac9dbSNicholas Bellinger 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2707c66ac9dbSNicholas Bellinger 		dev = lu_gp_mem->lu_gp_mem_dev;
27080fd97ccfSChristoph Hellwig 		hba = dev->se_hba;
2709c66ac9dbSNicholas Bellinger 
2710c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2711c66ac9dbSNicholas Bellinger 			config_item_name(&hba->hba_group.cg_item),
27120fd97ccfSChristoph Hellwig 			config_item_name(&dev->dev_group.cg_item));
2713c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2714c66ac9dbSNicholas Bellinger 
2715c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
27166708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2717c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2718c66ac9dbSNicholas Bellinger 			break;
2719c66ac9dbSNicholas Bellinger 		}
2720c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2721c66ac9dbSNicholas Bellinger 		len += cur_len;
2722c66ac9dbSNicholas Bellinger 	}
2723c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp->lu_gp_lock);
2724c66ac9dbSNicholas Bellinger 
2725c66ac9dbSNicholas Bellinger 	return len;
2726c66ac9dbSNicholas Bellinger }
2727c66ac9dbSNicholas Bellinger 
27282eafd729SChristoph Hellwig CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
27292eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_lu_gp_, members);
2730c66ac9dbSNicholas Bellinger 
2731c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
27322eafd729SChristoph Hellwig 	&target_lu_gp_attr_lu_gp_id,
27332eafd729SChristoph Hellwig 	&target_lu_gp_attr_members,
2734c66ac9dbSNicholas Bellinger 	NULL,
2735c66ac9dbSNicholas Bellinger };
2736c66ac9dbSNicholas Bellinger 
27371f6fe7cbSNicholas Bellinger static void target_core_alua_lu_gp_release(struct config_item *item)
27381f6fe7cbSNicholas Bellinger {
27391f6fe7cbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
27401f6fe7cbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
27411f6fe7cbSNicholas Bellinger 
27421f6fe7cbSNicholas Bellinger 	core_alua_free_lu_gp(lu_gp);
27431f6fe7cbSNicholas Bellinger }
27441f6fe7cbSNicholas Bellinger 
2745c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_lu_gp_ops = {
27461f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_lu_gp_release,
2747c66ac9dbSNicholas Bellinger };
2748c66ac9dbSNicholas Bellinger 
2749ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_lu_gp_cit = {
2750c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
2751c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_lu_gp_attrs,
2752c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2753c66ac9dbSNicholas Bellinger };
2754c66ac9dbSNicholas Bellinger 
2755c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2756c66ac9dbSNicholas Bellinger 
2757c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2758c66ac9dbSNicholas Bellinger 
2759c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_lu_gp(
2760c66ac9dbSNicholas Bellinger 	struct config_group *group,
2761c66ac9dbSNicholas Bellinger 	const char *name)
2762c66ac9dbSNicholas Bellinger {
2763c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2764c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = NULL;
2765c66ac9dbSNicholas Bellinger 	struct config_item *alua_lu_gp_ci = NULL;
2766c66ac9dbSNicholas Bellinger 
2767c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp(name, 0);
2768c66ac9dbSNicholas Bellinger 	if (IS_ERR(lu_gp))
2769c66ac9dbSNicholas Bellinger 		return NULL;
2770c66ac9dbSNicholas Bellinger 
2771c66ac9dbSNicholas Bellinger 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
2772c66ac9dbSNicholas Bellinger 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2773c66ac9dbSNicholas Bellinger 
2774c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_lu_gp_cg, name,
2775c66ac9dbSNicholas Bellinger 			&target_core_alua_lu_gp_cit);
2776c66ac9dbSNicholas Bellinger 
27776708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2778c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s\n",
2779c66ac9dbSNicholas Bellinger 		config_item_name(alua_lu_gp_ci));
2780c66ac9dbSNicholas Bellinger 
2781c66ac9dbSNicholas Bellinger 	return alua_lu_gp_cg;
2782c66ac9dbSNicholas Bellinger 
2783c66ac9dbSNicholas Bellinger }
2784c66ac9dbSNicholas Bellinger 
2785c66ac9dbSNicholas Bellinger static void target_core_alua_drop_lu_gp(
2786c66ac9dbSNicholas Bellinger 	struct config_group *group,
2787c66ac9dbSNicholas Bellinger 	struct config_item *item)
2788c66ac9dbSNicholas Bellinger {
2789c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2790c66ac9dbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
2791c66ac9dbSNicholas Bellinger 
27926708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2793c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
2794c66ac9dbSNicholas Bellinger 		config_item_name(item), lu_gp->lu_gp_id);
27951f6fe7cbSNicholas Bellinger 	/*
27961f6fe7cbSNicholas Bellinger 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
27971f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_lu_gp_release()
27981f6fe7cbSNicholas Bellinger 	 */
2799c66ac9dbSNicholas Bellinger 	config_item_put(item);
2800c66ac9dbSNicholas Bellinger }
2801c66ac9dbSNicholas Bellinger 
2802c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2803c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_lu_gp,
2804c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_lu_gp,
2805c66ac9dbSNicholas Bellinger };
2806c66ac9dbSNicholas Bellinger 
2807ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_lu_gps_cit = {
2808c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2809c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
2810c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2811c66ac9dbSNicholas Bellinger };
2812c66ac9dbSNicholas Bellinger 
2813c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2814c66ac9dbSNicholas Bellinger 
2815c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2816c66ac9dbSNicholas Bellinger 
28172eafd729SChristoph Hellwig static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
28182eafd729SChristoph Hellwig {
28192eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
28202eafd729SChristoph Hellwig 			tg_pt_gp_group);
28212eafd729SChristoph Hellwig }
2822c66ac9dbSNicholas Bellinger 
28232eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2824c66ac9dbSNicholas Bellinger 		char *page)
2825c66ac9dbSNicholas Bellinger {
2826c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n",
2827d19c4643SMike Christie 		       to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
2828c66ac9dbSNicholas Bellinger }
2829c66ac9dbSNicholas Bellinger 
28302eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
28312eafd729SChristoph Hellwig 		const char *page, size_t count)
2832c66ac9dbSNicholas Bellinger {
28332eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
28340fd97ccfSChristoph Hellwig 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2835c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2836c66ac9dbSNicholas Bellinger 	int new_state, ret;
2837c66ac9dbSNicholas Bellinger 
28386708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2839baa75afdSBart Van Assche 		pr_err("Unable to do implicit ALUA on invalid tg_pt_gp ID\n");
2840c66ac9dbSNicholas Bellinger 		return -EINVAL;
2841c66ac9dbSNicholas Bellinger 	}
2842cb0f32e1SMike Christie 	if (!target_dev_configured(dev)) {
2843f1453773SNicholas Bellinger 		pr_err("Unable to set alua_access_state while device is"
2844f1453773SNicholas Bellinger 		       " not configured\n");
2845f1453773SNicholas Bellinger 		return -ENODEV;
2846f1453773SNicholas Bellinger 	}
2847c66ac9dbSNicholas Bellinger 
284857103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2849c66ac9dbSNicholas Bellinger 	if (ret < 0) {
28506708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access state from"
2851c66ac9dbSNicholas Bellinger 				" %s\n", page);
285257103d7fSJingoo Han 		return ret;
2853c66ac9dbSNicholas Bellinger 	}
2854c66ac9dbSNicholas Bellinger 	new_state = (int)tmp;
2855c66ac9dbSNicholas Bellinger 
2856125d0119SHannes Reinecke 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2857125d0119SHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA"
2858125d0119SHannes Reinecke 			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
2859c66ac9dbSNicholas Bellinger 		return -EINVAL;
2860c66ac9dbSNicholas Bellinger 	}
2861c66094bfSHannes Reinecke 	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2862c66094bfSHannes Reinecke 	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2863c66094bfSHannes Reinecke 		/* LBA DEPENDENT is only allowed with implicit ALUA */
2864c66094bfSHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA transition"
2865c66094bfSHannes Reinecke 		       " while explicit ALUA management is enabled\n");
2866c66094bfSHannes Reinecke 		return -EINVAL;
2867c66094bfSHannes Reinecke 	}
2868c66ac9dbSNicholas Bellinger 
28690fd97ccfSChristoph Hellwig 	ret = core_alua_do_port_transition(tg_pt_gp, dev,
2870c66ac9dbSNicholas Bellinger 					NULL, NULL, new_state, 0);
2871c66ac9dbSNicholas Bellinger 	return (!ret) ? count : -EINVAL;
2872c66ac9dbSNicholas Bellinger }
2873c66ac9dbSNicholas Bellinger 
28742eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2875c66ac9dbSNicholas Bellinger 		char *page)
2876c66ac9dbSNicholas Bellinger {
28772eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2878c66ac9dbSNicholas Bellinger 	return sprintf(page, "%s\n",
2879c66ac9dbSNicholas Bellinger 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2880c66ac9dbSNicholas Bellinger }
2881c66ac9dbSNicholas Bellinger 
28822eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_store(
28832eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2884c66ac9dbSNicholas Bellinger {
28852eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2886c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2887c66ac9dbSNicholas Bellinger 	int new_status, ret;
2888c66ac9dbSNicholas Bellinger 
28896708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2890baa75afdSBart Van Assche 		pr_err("Unable to set ALUA access status on invalid tg_pt_gp ID\n");
2891c66ac9dbSNicholas Bellinger 		return -EINVAL;
2892c66ac9dbSNicholas Bellinger 	}
2893c66ac9dbSNicholas Bellinger 
289457103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2895c66ac9dbSNicholas Bellinger 	if (ret < 0) {
28966708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access status"
2897c66ac9dbSNicholas Bellinger 				" from %s\n", page);
289857103d7fSJingoo Han 		return ret;
2899c66ac9dbSNicholas Bellinger 	}
2900c66ac9dbSNicholas Bellinger 	new_status = (int)tmp;
2901c66ac9dbSNicholas Bellinger 
2902c66ac9dbSNicholas Bellinger 	if ((new_status != ALUA_STATUS_NONE) &&
2903125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2904125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
29056708bb27SAndy Grover 		pr_err("Illegal ALUA access status: 0x%02x\n",
2906c66ac9dbSNicholas Bellinger 				new_status);
2907c66ac9dbSNicholas Bellinger 		return -EINVAL;
2908c66ac9dbSNicholas Bellinger 	}
2909c66ac9dbSNicholas Bellinger 
2910c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2911c66ac9dbSNicholas Bellinger 	return count;
2912c66ac9dbSNicholas Bellinger }
2913c66ac9dbSNicholas Bellinger 
29142eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2915c66ac9dbSNicholas Bellinger 		char *page)
2916c66ac9dbSNicholas Bellinger {
29172eafd729SChristoph Hellwig 	return core_alua_show_access_type(to_tg_pt_gp(item), page);
2918c66ac9dbSNicholas Bellinger }
2919c66ac9dbSNicholas Bellinger 
29202eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
29212eafd729SChristoph Hellwig 		const char *page, size_t count)
2922c66ac9dbSNicholas Bellinger {
29232eafd729SChristoph Hellwig 	return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2924c66ac9dbSNicholas Bellinger }
2925c66ac9dbSNicholas Bellinger 
29262eafd729SChristoph Hellwig #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit)				\
29272eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_show(		\
29282eafd729SChristoph Hellwig 		struct config_item *item, char *p)			\
2929b0a382c5SHannes Reinecke {									\
29302eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
29312eafd729SChristoph Hellwig 	return sprintf(p, "%d\n",					\
29322eafd729SChristoph Hellwig 		!!(t->tg_pt_gp_alua_supported_states & _bit));		\
29332eafd729SChristoph Hellwig }									\
29342eafd729SChristoph Hellwig 									\
29352eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_store(		\
29362eafd729SChristoph Hellwig 		struct config_item *item, const char *p, size_t c)	\
2937b0a382c5SHannes Reinecke {									\
29382eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
2939b0a382c5SHannes Reinecke 	unsigned long tmp;						\
2940b0a382c5SHannes Reinecke 	int ret;							\
2941b0a382c5SHannes Reinecke 									\
2942b0a382c5SHannes Reinecke 	if (!t->tg_pt_gp_valid_id) {					\
2943baa75afdSBart Van Assche 		pr_err("Unable to set " #_name " ALUA state on invalid tg_pt_gp ID\n"); \
2944b0a382c5SHannes Reinecke 		return -EINVAL;						\
2945b0a382c5SHannes Reinecke 	}								\
2946b0a382c5SHannes Reinecke 									\
2947b0a382c5SHannes Reinecke 	ret = kstrtoul(p, 0, &tmp);					\
2948b0a382c5SHannes Reinecke 	if (ret < 0) {							\
2949b0a382c5SHannes Reinecke 		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
2950b0a382c5SHannes Reinecke 		return -EINVAL;						\
2951b0a382c5SHannes Reinecke 	}								\
2952b0a382c5SHannes Reinecke 	if (tmp > 1) {							\
2953b0a382c5SHannes Reinecke 		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2954b0a382c5SHannes Reinecke 		return -EINVAL;						\
2955b0a382c5SHannes Reinecke 	}								\
29561f0b030cSSebastian Herbszt 	if (tmp)							\
29572eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states |= _bit;		\
2958b0a382c5SHannes Reinecke 	else								\
29592eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states &= ~_bit;		\
2960b0a382c5SHannes Reinecke 									\
2961b0a382c5SHannes Reinecke 	return c;							\
29626be526c4SHannes Reinecke }
29636be526c4SHannes Reinecke 
29642eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
29652eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
29662eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
29672eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
29682eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
29692eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
29702eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
29716be526c4SHannes Reinecke 
29722eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_show(
29732eafd729SChristoph Hellwig 		struct config_item *item, char *page)
2974c66ac9dbSNicholas Bellinger {
29752eafd729SChristoph Hellwig 	return sprintf(page, "%d\n",
29762eafd729SChristoph Hellwig 		to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
2977c66ac9dbSNicholas Bellinger }
2978c66ac9dbSNicholas Bellinger 
29792eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_store(
29802eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2981c66ac9dbSNicholas Bellinger {
29822eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2983c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2984c66ac9dbSNicholas Bellinger 	int ret;
2985c66ac9dbSNicholas Bellinger 
298657103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2987c66ac9dbSNicholas Bellinger 	if (ret < 0) {
29886708bb27SAndy Grover 		pr_err("Unable to extract alua_write_metadata\n");
298957103d7fSJingoo Han 		return ret;
2990c66ac9dbSNicholas Bellinger 	}
2991c66ac9dbSNicholas Bellinger 
2992c66ac9dbSNicholas Bellinger 	if ((tmp != 0) && (tmp != 1)) {
29936708bb27SAndy Grover 		pr_err("Illegal value for alua_write_metadata:"
2994c66ac9dbSNicholas Bellinger 			" %lu\n", tmp);
2995c66ac9dbSNicholas Bellinger 		return -EINVAL;
2996c66ac9dbSNicholas Bellinger 	}
2997c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2998c66ac9dbSNicholas Bellinger 
2999c66ac9dbSNicholas Bellinger 	return count;
3000c66ac9dbSNicholas Bellinger }
3001c66ac9dbSNicholas Bellinger 
30022eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
3003c66ac9dbSNicholas Bellinger 		char *page)
3004c66ac9dbSNicholas Bellinger {
30052eafd729SChristoph Hellwig 	return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
3006c66ac9dbSNicholas Bellinger }
3007c66ac9dbSNicholas Bellinger 
30082eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
30092eafd729SChristoph Hellwig 		const char *page, size_t count)
3010c66ac9dbSNicholas Bellinger {
30112eafd729SChristoph Hellwig 	return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
30122eafd729SChristoph Hellwig 			count);
3013c66ac9dbSNicholas Bellinger }
3014c66ac9dbSNicholas Bellinger 
30152eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
3016c66ac9dbSNicholas Bellinger 		char *page)
3017c66ac9dbSNicholas Bellinger {
30182eafd729SChristoph Hellwig 	return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
3019c66ac9dbSNicholas Bellinger }
3020c66ac9dbSNicholas Bellinger 
30212eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
30222eafd729SChristoph Hellwig 		const char *page, size_t count)
3023c66ac9dbSNicholas Bellinger {
30242eafd729SChristoph Hellwig 	return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
30252eafd729SChristoph Hellwig 			count);
3026c66ac9dbSNicholas Bellinger }
3027c66ac9dbSNicholas Bellinger 
30282eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
30292eafd729SChristoph Hellwig 		struct config_item *item, char *page)
30302eafd729SChristoph Hellwig {
30312eafd729SChristoph Hellwig 	return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
30322eafd729SChristoph Hellwig }
3033c66ac9dbSNicholas Bellinger 
30342eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
30352eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
30362eafd729SChristoph Hellwig {
30372eafd729SChristoph Hellwig 	return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
30382eafd729SChristoph Hellwig 			count);
30392eafd729SChristoph Hellwig }
30402eafd729SChristoph Hellwig 
30412eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
30425b9a4d72SNicholas Bellinger 		char *page)
30435b9a4d72SNicholas Bellinger {
30442eafd729SChristoph Hellwig 	return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
30455b9a4d72SNicholas Bellinger }
30465b9a4d72SNicholas Bellinger 
30472eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
30482eafd729SChristoph Hellwig 		const char *page, size_t count)
30495b9a4d72SNicholas Bellinger {
30502eafd729SChristoph Hellwig 	return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
30515b9a4d72SNicholas Bellinger }
30525b9a4d72SNicholas Bellinger 
30532eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
3054c66ac9dbSNicholas Bellinger 		char *page)
3055c66ac9dbSNicholas Bellinger {
30562eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3057c66ac9dbSNicholas Bellinger 
30586708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id)
3059c66ac9dbSNicholas Bellinger 		return 0;
3060c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
3061c66ac9dbSNicholas Bellinger }
3062c66ac9dbSNicholas Bellinger 
30632eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
30642eafd729SChristoph Hellwig 		const char *page, size_t count)
3065c66ac9dbSNicholas Bellinger {
30662eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3067c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3068c66ac9dbSNicholas Bellinger 	unsigned long tg_pt_gp_id;
3069c66ac9dbSNicholas Bellinger 	int ret;
3070c66ac9dbSNicholas Bellinger 
307157103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tg_pt_gp_id);
3072c66ac9dbSNicholas Bellinger 	if (ret < 0) {
30733d035237SHannes Reinecke 		pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
30743d035237SHannes Reinecke 		       page);
307557103d7fSJingoo Han 		return ret;
3076c66ac9dbSNicholas Bellinger 	}
3077c66ac9dbSNicholas Bellinger 	if (tg_pt_gp_id > 0x0000ffff) {
30783d035237SHannes Reinecke 		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
30793d035237SHannes Reinecke 		       tg_pt_gp_id);
3080c66ac9dbSNicholas Bellinger 		return -EINVAL;
3081c66ac9dbSNicholas Bellinger 	}
3082c66ac9dbSNicholas Bellinger 
3083c66ac9dbSNicholas Bellinger 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
3084c66ac9dbSNicholas Bellinger 	if (ret < 0)
3085c66ac9dbSNicholas Bellinger 		return -EINVAL;
3086c66ac9dbSNicholas Bellinger 
30876708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
3088c66ac9dbSNicholas Bellinger 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
3089c66ac9dbSNicholas Bellinger 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
3090c66ac9dbSNicholas Bellinger 		tg_pt_gp->tg_pt_gp_id);
3091c66ac9dbSNicholas Bellinger 
3092c66ac9dbSNicholas Bellinger 	return count;
3093c66ac9dbSNicholas Bellinger }
3094c66ac9dbSNicholas Bellinger 
30952eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
3096c66ac9dbSNicholas Bellinger 		char *page)
3097c66ac9dbSNicholas Bellinger {
30982eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3099c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
3100c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
31012d4e2dafSChaitanya Kulkarni 	unsigned char buf[TG_PT_GROUP_NAME_BUF] = { };
3102c66ac9dbSNicholas Bellinger 
3103c66ac9dbSNicholas Bellinger 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
3104adf653f9SChristoph Hellwig 	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
3105adf653f9SChristoph Hellwig 			lun_tg_pt_gp_link) {
3106adf653f9SChristoph Hellwig 		struct se_portal_group *tpg = lun->lun_tpg;
3107c66ac9dbSNicholas Bellinger 
3108c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
310930c7ca93SDavid Disseldorp 			"/%s\n", tpg->se_tpg_tfo->fabric_name,
3110e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
3111e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
3112c66ac9dbSNicholas Bellinger 			config_item_name(&lun->lun_group.cg_item));
3113c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
3114c66ac9dbSNicholas Bellinger 
3115c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
31166708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
3117c66ac9dbSNicholas Bellinger 				"_members buffer\n");
3118c66ac9dbSNicholas Bellinger 			break;
3119c66ac9dbSNicholas Bellinger 		}
3120c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
3121c66ac9dbSNicholas Bellinger 		len += cur_len;
3122c66ac9dbSNicholas Bellinger 	}
3123c66ac9dbSNicholas Bellinger 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
3124c66ac9dbSNicholas Bellinger 
3125c66ac9dbSNicholas Bellinger 	return len;
3126c66ac9dbSNicholas Bellinger }
3127c66ac9dbSNicholas Bellinger 
31282eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
31292eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
31302eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
31312eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
31322eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
31332eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
31342eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
31352eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
31362eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
31372eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
31382eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
31392eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
31402eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
31412eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
31422eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
31432eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
31442eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
3145c66ac9dbSNicholas Bellinger 
3146c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
31472eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_state,
31482eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_status,
31492eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_type,
31502eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_transitioning,
31512eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_offline,
31522eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_lba_dependent,
31532eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_unavailable,
31542eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_standby,
31552eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_nonoptimized,
31562eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_optimized,
31572eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_write_metadata,
31582eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_nonop_delay_msecs,
31592eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_trans_delay_msecs,
31602eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_implicit_trans_secs,
31612eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_preferred,
31622eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_tg_pt_gp_id,
31632eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_members,
3164c66ac9dbSNicholas Bellinger 	NULL,
3165c66ac9dbSNicholas Bellinger };
3166c66ac9dbSNicholas Bellinger 
31671f6fe7cbSNicholas Bellinger static void target_core_alua_tg_pt_gp_release(struct config_item *item)
31681f6fe7cbSNicholas Bellinger {
31691f6fe7cbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
31701f6fe7cbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
31711f6fe7cbSNicholas Bellinger 
31721f6fe7cbSNicholas Bellinger 	core_alua_free_tg_pt_gp(tg_pt_gp);
31731f6fe7cbSNicholas Bellinger }
31741f6fe7cbSNicholas Bellinger 
3175c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
31761f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_tg_pt_gp_release,
3177c66ac9dbSNicholas Bellinger };
3178c66ac9dbSNicholas Bellinger 
3179ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_tg_pt_gp_cit = {
3180c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
3181c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
3182c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3183c66ac9dbSNicholas Bellinger };
3184c66ac9dbSNicholas Bellinger 
3185c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
3186c66ac9dbSNicholas Bellinger 
318772aca57bSNicholas Bellinger /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3188c66ac9dbSNicholas Bellinger 
3189c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_tg_pt_gp(
3190c66ac9dbSNicholas Bellinger 	struct config_group *group,
3191c66ac9dbSNicholas Bellinger 	const char *name)
3192c66ac9dbSNicholas Bellinger {
3193c66ac9dbSNicholas Bellinger 	struct t10_alua *alua = container_of(group, struct t10_alua,
3194c66ac9dbSNicholas Bellinger 					alua_tg_pt_gps_group);
3195c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
3196c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = NULL;
3197c66ac9dbSNicholas Bellinger 	struct config_item *alua_tg_pt_gp_ci = NULL;
3198c66ac9dbSNicholas Bellinger 
31990fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
32006708bb27SAndy Grover 	if (!tg_pt_gp)
3201c66ac9dbSNicholas Bellinger 		return NULL;
3202c66ac9dbSNicholas Bellinger 
3203c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3204c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
3205c66ac9dbSNicholas Bellinger 
3206c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
3207c66ac9dbSNicholas Bellinger 			&target_core_alua_tg_pt_gp_cit);
3208c66ac9dbSNicholas Bellinger 
32096708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
3210c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s\n",
3211c66ac9dbSNicholas Bellinger 		config_item_name(alua_tg_pt_gp_ci));
3212c66ac9dbSNicholas Bellinger 
3213c66ac9dbSNicholas Bellinger 	return alua_tg_pt_gp_cg;
3214c66ac9dbSNicholas Bellinger }
3215c66ac9dbSNicholas Bellinger 
3216c66ac9dbSNicholas Bellinger static void target_core_alua_drop_tg_pt_gp(
3217c66ac9dbSNicholas Bellinger 	struct config_group *group,
3218c66ac9dbSNicholas Bellinger 	struct config_item *item)
3219c66ac9dbSNicholas Bellinger {
3220c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3221c66ac9dbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3222c66ac9dbSNicholas Bellinger 
32236708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
3224c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
3225c66ac9dbSNicholas Bellinger 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
32261f6fe7cbSNicholas Bellinger 	/*
32271f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
32281f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_tg_pt_gp_release().
32291f6fe7cbSNicholas Bellinger 	 */
3230c66ac9dbSNicholas Bellinger 	config_item_put(item);
3231c66ac9dbSNicholas Bellinger }
3232c66ac9dbSNicholas Bellinger 
3233c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
3234c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_tg_pt_gp,
3235c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
3236c66ac9dbSNicholas Bellinger };
3237c66ac9dbSNicholas Bellinger 
323872aca57bSNicholas Bellinger TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
3239c66ac9dbSNicholas Bellinger 
324072aca57bSNicholas Bellinger /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3241c66ac9dbSNicholas Bellinger 
3242c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_cit */
3243c66ac9dbSNicholas Bellinger 
3244c66ac9dbSNicholas Bellinger /*
3245c66ac9dbSNicholas Bellinger  * target_core_alua_cit is a ConfigFS group that lives under
3246c66ac9dbSNicholas Bellinger  * /sys/kernel/config/target/core/alua.  There are default groups
3247c66ac9dbSNicholas Bellinger  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
3248c66ac9dbSNicholas Bellinger  * target_core_alua_cit in target_core_init_configfs() below.
3249c66ac9dbSNicholas Bellinger  */
3250ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_cit = {
3251c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
3252c66ac9dbSNicholas Bellinger 	.ct_attrs		= NULL,
3253c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3254c66ac9dbSNicholas Bellinger };
3255c66ac9dbSNicholas Bellinger 
3256c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_cit */
3257c66ac9dbSNicholas Bellinger 
3258d23ab570SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_stat_cit */
325912d23384SNicholas Bellinger 
326012d23384SNicholas Bellinger static struct config_group *target_core_stat_mkdir(
326112d23384SNicholas Bellinger 	struct config_group *group,
326212d23384SNicholas Bellinger 	const char *name)
326312d23384SNicholas Bellinger {
326412d23384SNicholas Bellinger 	return ERR_PTR(-ENOSYS);
326512d23384SNicholas Bellinger }
326612d23384SNicholas Bellinger 
326712d23384SNicholas Bellinger static void target_core_stat_rmdir(
326812d23384SNicholas Bellinger 	struct config_group *group,
326912d23384SNicholas Bellinger 	struct config_item *item)
327012d23384SNicholas Bellinger {
327112d23384SNicholas Bellinger 	return;
327212d23384SNicholas Bellinger }
327312d23384SNicholas Bellinger 
327412d23384SNicholas Bellinger static struct configfs_group_operations target_core_stat_group_ops = {
327512d23384SNicholas Bellinger 	.make_group		= &target_core_stat_mkdir,
327612d23384SNicholas Bellinger 	.drop_item		= &target_core_stat_rmdir,
327712d23384SNicholas Bellinger };
327812d23384SNicholas Bellinger 
3279d23ab570SNicholas Bellinger TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
328012d23384SNicholas Bellinger 
3281d23ab570SNicholas Bellinger /* End functions for struct config_item_type tb_dev_stat_cit */
328212d23384SNicholas Bellinger 
3283c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_hba_cit */
3284c66ac9dbSNicholas Bellinger 
3285c66ac9dbSNicholas Bellinger static struct config_group *target_core_make_subdev(
3286c66ac9dbSNicholas Bellinger 	struct config_group *group,
3287c66ac9dbSNicholas Bellinger 	const char *name)
3288c66ac9dbSNicholas Bellinger {
3289c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
3290c66ac9dbSNicholas Bellinger 	struct config_item *hba_ci = &group->cg_item;
3291c66ac9dbSNicholas Bellinger 	struct se_hba *hba = item_to_hba(hba_ci);
32920a06d430SChristoph Hellwig 	struct target_backend *tb = hba->backend;
32930fd97ccfSChristoph Hellwig 	struct se_device *dev;
329412d23384SNicholas Bellinger 	int errno = -ENOMEM, ret;
3295c66ac9dbSNicholas Bellinger 
329612d23384SNicholas Bellinger 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
329712d23384SNicholas Bellinger 	if (ret)
329812d23384SNicholas Bellinger 		return ERR_PTR(ret);
3299c66ac9dbSNicholas Bellinger 
33000fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, name);
33010fd97ccfSChristoph Hellwig 	if (!dev)
33020fd97ccfSChristoph Hellwig 		goto out_unlock;
3303c66ac9dbSNicholas Bellinger 
33041ae1602dSChristoph Hellwig 	config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
3305c66ac9dbSNicholas Bellinger 
33068dc31ff9SMike Christie 	config_group_init_type_name(&dev->dev_action_group, "action",
33078dc31ff9SMike Christie 			&tb->tb_dev_action_cit);
33088dc31ff9SMike Christie 	configfs_add_default_group(&dev->dev_action_group, &dev->dev_group);
33098dc31ff9SMike Christie 
33100fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
33110a06d430SChristoph Hellwig 			&tb->tb_dev_attrib_cit);
33121ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
33131ae1602dSChristoph Hellwig 
33140fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_pr_group, "pr",
33150a06d430SChristoph Hellwig 			&tb->tb_dev_pr_cit);
33161ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
33171ae1602dSChristoph Hellwig 
33180fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
33190a06d430SChristoph Hellwig 			&tb->tb_dev_wwn_cit);
33201ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
33211ae1602dSChristoph Hellwig 			&dev->dev_group);
33221ae1602dSChristoph Hellwig 
33230fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
33240a06d430SChristoph Hellwig 			"alua", &tb->tb_dev_alua_tg_pt_gps_cit);
33251ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
33261ae1602dSChristoph Hellwig 			&dev->dev_group);
33271ae1602dSChristoph Hellwig 
33280fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
33290a06d430SChristoph Hellwig 			"statistics", &tb->tb_dev_stat_cit);
33301ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_stat_grps.stat_group,
33311ae1602dSChristoph Hellwig 			&dev->dev_group);
333212d23384SNicholas Bellinger 
3333c66ac9dbSNicholas Bellinger 	/*
333412d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
3335c66ac9dbSNicholas Bellinger 	 */
33360fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
33376708bb27SAndy Grover 	if (!tg_pt_gp)
33381ae1602dSChristoph Hellwig 		goto out_free_device;
33390fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
3340c66ac9dbSNicholas Bellinger 
3341c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
3342c66ac9dbSNicholas Bellinger 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
33431ae1602dSChristoph Hellwig 	configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
33441ae1602dSChristoph Hellwig 			&dev->t10_alua.alua_tg_pt_gps_group);
33451ae1602dSChristoph Hellwig 
334612d23384SNicholas Bellinger 	/*
334712d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/statistics/ default groups
334812d23384SNicholas Bellinger 	 */
33490fd97ccfSChristoph Hellwig 	target_stat_setup_dev_default_groups(dev);
3350c66ac9dbSNicholas Bellinger 
335180890c5eSMaurizio Lombardi 	mutex_lock(&target_devices_lock);
335280890c5eSMaurizio Lombardi 	target_devices++;
335380890c5eSMaurizio Lombardi 	mutex_unlock(&target_devices_lock);
335480890c5eSMaurizio Lombardi 
3355c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
33561ae1602dSChristoph Hellwig 	return &dev->dev_group;
33570fd97ccfSChristoph Hellwig 
33580fd97ccfSChristoph Hellwig out_free_device:
33590fd97ccfSChristoph Hellwig 	target_free_device(dev);
33600fd97ccfSChristoph Hellwig out_unlock:
3361c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
336212d23384SNicholas Bellinger 	return ERR_PTR(errno);
3363c66ac9dbSNicholas Bellinger }
3364c66ac9dbSNicholas Bellinger 
3365c66ac9dbSNicholas Bellinger static void target_core_drop_subdev(
3366c66ac9dbSNicholas Bellinger 	struct config_group *group,
3367c66ac9dbSNicholas Bellinger 	struct config_item *item)
3368c66ac9dbSNicholas Bellinger {
33690fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
33700fd97ccfSChristoph Hellwig 	struct se_device *dev =
33710fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
3372c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
3373c66ac9dbSNicholas Bellinger 
33740fd97ccfSChristoph Hellwig 	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
3375c66ac9dbSNicholas Bellinger 
33761f6fe7cbSNicholas Bellinger 	mutex_lock(&hba->hba_access_mutex);
3377c66ac9dbSNicholas Bellinger 
33781ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
33791ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
338012d23384SNicholas Bellinger 
33811f6fe7cbSNicholas Bellinger 	/*
33821f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
33831f6fe7cbSNicholas Bellinger 	 * directly from target_core_alua_tg_pt_gp_release().
33841f6fe7cbSNicholas Bellinger 	 */
33850fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = NULL;
3386c66ac9dbSNicholas Bellinger 
33871ae1602dSChristoph Hellwig 	configfs_remove_default_groups(dev_cg);
33881ae1602dSChristoph Hellwig 
33891f6fe7cbSNicholas Bellinger 	/*
33900fd97ccfSChristoph Hellwig 	 * se_dev is released from target_core_dev_item_ops->release()
33911f6fe7cbSNicholas Bellinger 	 */
3392c66ac9dbSNicholas Bellinger 	config_item_put(item);
339380890c5eSMaurizio Lombardi 
339480890c5eSMaurizio Lombardi 	mutex_lock(&target_devices_lock);
339580890c5eSMaurizio Lombardi 	target_devices--;
339680890c5eSMaurizio Lombardi 	mutex_unlock(&target_devices_lock);
339780890c5eSMaurizio Lombardi 
3398c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
3399c66ac9dbSNicholas Bellinger }
3400c66ac9dbSNicholas Bellinger 
3401c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_hba_group_ops = {
3402c66ac9dbSNicholas Bellinger 	.make_group		= target_core_make_subdev,
3403c66ac9dbSNicholas Bellinger 	.drop_item		= target_core_drop_subdev,
3404c66ac9dbSNicholas Bellinger };
3405c66ac9dbSNicholas Bellinger 
3406c66ac9dbSNicholas Bellinger 
34072eafd729SChristoph Hellwig static inline struct se_hba *to_hba(struct config_item *item)
3408c66ac9dbSNicholas Bellinger {
34092eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_hba, hba_group);
34102eafd729SChristoph Hellwig }
34112eafd729SChristoph Hellwig 
34122eafd729SChristoph Hellwig static ssize_t target_hba_info_show(struct config_item *item, char *page)
34132eafd729SChristoph Hellwig {
34142eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
34152eafd729SChristoph Hellwig 
3416c66ac9dbSNicholas Bellinger 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
34170a06d430SChristoph Hellwig 			hba->hba_id, hba->backend->ops->name,
3418ce8dd25dSChristoph Hellwig 			TARGET_CORE_VERSION);
3419c66ac9dbSNicholas Bellinger }
3420c66ac9dbSNicholas Bellinger 
34212eafd729SChristoph Hellwig static ssize_t target_hba_mode_show(struct config_item *item, char *page)
3422c66ac9dbSNicholas Bellinger {
34232eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
3424c66ac9dbSNicholas Bellinger 	int hba_mode = 0;
3425c66ac9dbSNicholas Bellinger 
3426c66ac9dbSNicholas Bellinger 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3427c66ac9dbSNicholas Bellinger 		hba_mode = 1;
3428c66ac9dbSNicholas Bellinger 
3429c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", hba_mode);
3430c66ac9dbSNicholas Bellinger }
3431c66ac9dbSNicholas Bellinger 
34322eafd729SChristoph Hellwig static ssize_t target_hba_mode_store(struct config_item *item,
3433c66ac9dbSNicholas Bellinger 		const char *page, size_t count)
3434c66ac9dbSNicholas Bellinger {
34352eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
3436c66ac9dbSNicholas Bellinger 	unsigned long mode_flag;
3437c66ac9dbSNicholas Bellinger 	int ret;
3438c66ac9dbSNicholas Bellinger 
34390a06d430SChristoph Hellwig 	if (hba->backend->ops->pmode_enable_hba == NULL)
3440c66ac9dbSNicholas Bellinger 		return -EINVAL;
3441c66ac9dbSNicholas Bellinger 
344257103d7fSJingoo Han 	ret = kstrtoul(page, 0, &mode_flag);
3443c66ac9dbSNicholas Bellinger 	if (ret < 0) {
34446708bb27SAndy Grover 		pr_err("Unable to extract hba mode flag: %d\n", ret);
344557103d7fSJingoo Han 		return ret;
3446c66ac9dbSNicholas Bellinger 	}
3447c66ac9dbSNicholas Bellinger 
34480fd97ccfSChristoph Hellwig 	if (hba->dev_count) {
34496708bb27SAndy Grover 		pr_err("Unable to set hba_mode with active devices\n");
3450c66ac9dbSNicholas Bellinger 		return -EINVAL;
3451c66ac9dbSNicholas Bellinger 	}
3452c66ac9dbSNicholas Bellinger 
34530a06d430SChristoph Hellwig 	ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3454c66ac9dbSNicholas Bellinger 	if (ret < 0)
3455c66ac9dbSNicholas Bellinger 		return -EINVAL;
3456c66ac9dbSNicholas Bellinger 	if (ret > 0)
3457c66ac9dbSNicholas Bellinger 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3458c66ac9dbSNicholas Bellinger 	else if (ret == 0)
3459c66ac9dbSNicholas Bellinger 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3460c66ac9dbSNicholas Bellinger 
3461c66ac9dbSNicholas Bellinger 	return count;
3462c66ac9dbSNicholas Bellinger }
3463c66ac9dbSNicholas Bellinger 
34642eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_, hba_info);
34652eafd729SChristoph Hellwig CONFIGFS_ATTR(target_, hba_mode);
3466c66ac9dbSNicholas Bellinger 
34671f6fe7cbSNicholas Bellinger static void target_core_hba_release(struct config_item *item)
34681f6fe7cbSNicholas Bellinger {
34691f6fe7cbSNicholas Bellinger 	struct se_hba *hba = container_of(to_config_group(item),
34701f6fe7cbSNicholas Bellinger 				struct se_hba, hba_group);
34711f6fe7cbSNicholas Bellinger 	core_delete_hba(hba);
34721f6fe7cbSNicholas Bellinger }
34731f6fe7cbSNicholas Bellinger 
3474c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_hba_attrs[] = {
34752eafd729SChristoph Hellwig 	&target_attr_hba_info,
34762eafd729SChristoph Hellwig 	&target_attr_hba_mode,
3477c66ac9dbSNicholas Bellinger 	NULL,
3478c66ac9dbSNicholas Bellinger };
3479c66ac9dbSNicholas Bellinger 
3480c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_hba_item_ops = {
34811f6fe7cbSNicholas Bellinger 	.release		= target_core_hba_release,
3482c66ac9dbSNicholas Bellinger };
3483c66ac9dbSNicholas Bellinger 
3484ece550b5SBhumika Goyal static const struct config_item_type target_core_hba_cit = {
3485c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_hba_item_ops,
3486c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_hba_group_ops,
3487c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_hba_attrs,
3488c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3489c66ac9dbSNicholas Bellinger };
3490c66ac9dbSNicholas Bellinger 
3491c66ac9dbSNicholas Bellinger static struct config_group *target_core_call_addhbatotarget(
3492c66ac9dbSNicholas Bellinger 	struct config_group *group,
3493c66ac9dbSNicholas Bellinger 	const char *name)
3494c66ac9dbSNicholas Bellinger {
3495c66ac9dbSNicholas Bellinger 	char *se_plugin_str, *str, *str2;
3496c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
34972d4e2dafSChaitanya Kulkarni 	char buf[TARGET_CORE_NAME_MAX_LEN] = { };
3498c66ac9dbSNicholas Bellinger 	unsigned long plugin_dep_id = 0;
3499c66ac9dbSNicholas Bellinger 	int ret;
3500c66ac9dbSNicholas Bellinger 
350160d645a4SDan Carpenter 	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
35026708bb27SAndy Grover 		pr_err("Passed *name strlen(): %d exceeds"
3503c66ac9dbSNicholas Bellinger 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3504c66ac9dbSNicholas Bellinger 			TARGET_CORE_NAME_MAX_LEN);
3505c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENAMETOOLONG);
3506c66ac9dbSNicholas Bellinger 	}
3507c66ac9dbSNicholas Bellinger 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3508c66ac9dbSNicholas Bellinger 
3509c66ac9dbSNicholas Bellinger 	str = strstr(buf, "_");
35106708bb27SAndy Grover 	if (!str) {
35116708bb27SAndy Grover 		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3512c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
3513c66ac9dbSNicholas Bellinger 	}
3514c66ac9dbSNicholas Bellinger 	se_plugin_str = buf;
3515c66ac9dbSNicholas Bellinger 	/*
3516c66ac9dbSNicholas Bellinger 	 * Special case for subsystem plugins that have "_" in their names.
3517c66ac9dbSNicholas Bellinger 	 * Namely rd_direct and rd_mcp..
3518c66ac9dbSNicholas Bellinger 	 */
3519c66ac9dbSNicholas Bellinger 	str2 = strstr(str+1, "_");
35206708bb27SAndy Grover 	if (str2) {
3521c66ac9dbSNicholas Bellinger 		*str2 = '\0'; /* Terminate for *se_plugin_str */
3522c66ac9dbSNicholas Bellinger 		str2++; /* Skip to start of plugin dependent ID */
3523c66ac9dbSNicholas Bellinger 		str = str2;
3524c66ac9dbSNicholas Bellinger 	} else {
3525c66ac9dbSNicholas Bellinger 		*str = '\0'; /* Terminate for *se_plugin_str */
3526c66ac9dbSNicholas Bellinger 		str++; /* Skip to start of plugin dependent ID */
3527c66ac9dbSNicholas Bellinger 	}
3528c66ac9dbSNicholas Bellinger 
352957103d7fSJingoo Han 	ret = kstrtoul(str, 0, &plugin_dep_id);
3530c66ac9dbSNicholas Bellinger 	if (ret < 0) {
353157103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
3532c66ac9dbSNicholas Bellinger 				" plugin_dep_id\n", ret);
353357103d7fSJingoo Han 		return ERR_PTR(ret);
3534c66ac9dbSNicholas Bellinger 	}
3535c66ac9dbSNicholas Bellinger 	/*
3536c66ac9dbSNicholas Bellinger 	 * Load up TCM subsystem plugins if they have not already been loaded.
3537c66ac9dbSNicholas Bellinger 	 */
3538dbc5623eSNicholas Bellinger 	transport_subsystem_check_init();
3539c66ac9dbSNicholas Bellinger 
3540c66ac9dbSNicholas Bellinger 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3541c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
3542c66ac9dbSNicholas Bellinger 		return ERR_CAST(hba);
3543c66ac9dbSNicholas Bellinger 
3544c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&hba->hba_group, name,
3545c66ac9dbSNicholas Bellinger 			&target_core_hba_cit);
3546c66ac9dbSNicholas Bellinger 
3547c66ac9dbSNicholas Bellinger 	return &hba->hba_group;
3548c66ac9dbSNicholas Bellinger }
3549c66ac9dbSNicholas Bellinger 
3550c66ac9dbSNicholas Bellinger static void target_core_call_delhbafromtarget(
3551c66ac9dbSNicholas Bellinger 	struct config_group *group,
3552c66ac9dbSNicholas Bellinger 	struct config_item *item)
3553c66ac9dbSNicholas Bellinger {
35541f6fe7cbSNicholas Bellinger 	/*
35551f6fe7cbSNicholas Bellinger 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
35561f6fe7cbSNicholas Bellinger 	 * -> target_core_hba_release()
35571f6fe7cbSNicholas Bellinger 	 */
3558c66ac9dbSNicholas Bellinger 	config_item_put(item);
3559c66ac9dbSNicholas Bellinger }
3560c66ac9dbSNicholas Bellinger 
3561c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_group_ops = {
3562c66ac9dbSNicholas Bellinger 	.make_group	= target_core_call_addhbatotarget,
3563c66ac9dbSNicholas Bellinger 	.drop_item	= target_core_call_delhbafromtarget,
3564c66ac9dbSNicholas Bellinger };
3565c66ac9dbSNicholas Bellinger 
3566ece550b5SBhumika Goyal static const struct config_item_type target_core_cit = {
3567c66ac9dbSNicholas Bellinger 	.ct_item_ops	= NULL,
3568c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_group_ops,
3569c66ac9dbSNicholas Bellinger 	.ct_attrs	= NULL,
3570c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
3571c66ac9dbSNicholas Bellinger };
3572c66ac9dbSNicholas Bellinger 
3573c66ac9dbSNicholas Bellinger /* Stop functions for struct config_item_type target_core_hba_cit */
3574c66ac9dbSNicholas Bellinger 
35750a06d430SChristoph Hellwig void target_setup_backend_cits(struct target_backend *tb)
357673112edcSNicholas Bellinger {
35770a06d430SChristoph Hellwig 	target_core_setup_dev_cit(tb);
35788dc31ff9SMike Christie 	target_core_setup_dev_action_cit(tb);
35790a06d430SChristoph Hellwig 	target_core_setup_dev_attrib_cit(tb);
35800a06d430SChristoph Hellwig 	target_core_setup_dev_pr_cit(tb);
35810a06d430SChristoph Hellwig 	target_core_setup_dev_wwn_cit(tb);
35820a06d430SChristoph Hellwig 	target_core_setup_dev_alua_tg_pt_gps_cit(tb);
35830a06d430SChristoph Hellwig 	target_core_setup_dev_stat_cit(tb);
358473112edcSNicholas Bellinger }
358573112edcSNicholas Bellinger 
358678a6295cSLee Duncan static void target_init_dbroot(void)
358778a6295cSLee Duncan {
358878a6295cSLee Duncan 	struct file *fp;
358978a6295cSLee Duncan 
359078a6295cSLee Duncan 	snprintf(db_root_stage, DB_ROOT_LEN, DB_ROOT_PREFERRED);
359178a6295cSLee Duncan 	fp = filp_open(db_root_stage, O_RDONLY, 0);
359278a6295cSLee Duncan 	if (IS_ERR(fp)) {
359378a6295cSLee Duncan 		pr_err("db_root: cannot open: %s\n", db_root_stage);
359478a6295cSLee Duncan 		return;
359578a6295cSLee Duncan 	}
359678a6295cSLee Duncan 	if (!S_ISDIR(file_inode(fp)->i_mode)) {
359778a6295cSLee Duncan 		filp_close(fp, NULL);
359878a6295cSLee Duncan 		pr_err("db_root: not a valid directory: %s\n", db_root_stage);
359978a6295cSLee Duncan 		return;
360078a6295cSLee Duncan 	}
360178a6295cSLee Duncan 	filp_close(fp, NULL);
360278a6295cSLee Duncan 
360378a6295cSLee Duncan 	strncpy(db_root, db_root_stage, DB_ROOT_LEN);
360478a6295cSLee Duncan 	pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
360578a6295cSLee Duncan }
360678a6295cSLee Duncan 
360754550fabSAxel Lin static int __init target_core_init_configfs(void)
3608c66ac9dbSNicholas Bellinger {
3609d588cf8fSChristoph Hellwig 	struct configfs_subsystem *subsys = &target_core_fabrics;
3610c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
3611c66ac9dbSNicholas Bellinger 	int ret;
3612c66ac9dbSNicholas Bellinger 
36136708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3614c66ac9dbSNicholas Bellinger 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
3615c66ac9dbSNicholas Bellinger 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3616c66ac9dbSNicholas Bellinger 
3617c66ac9dbSNicholas Bellinger 	config_group_init(&subsys->su_group);
3618c66ac9dbSNicholas Bellinger 	mutex_init(&subsys->su_mutex);
3619c66ac9dbSNicholas Bellinger 
3620e3d6f909SAndy Grover 	ret = init_se_kmem_caches();
3621c66ac9dbSNicholas Bellinger 	if (ret < 0)
3622e3d6f909SAndy Grover 		return ret;
3623c66ac9dbSNicholas Bellinger 	/*
3624c66ac9dbSNicholas Bellinger 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3625c66ac9dbSNicholas Bellinger 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3626c66ac9dbSNicholas Bellinger 	 */
36271ae1602dSChristoph Hellwig 	config_group_init_type_name(&target_core_hbagroup, "core",
36281ae1602dSChristoph Hellwig 			&target_core_cit);
36291ae1602dSChristoph Hellwig 	configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
3630c66ac9dbSNicholas Bellinger 
3631c66ac9dbSNicholas Bellinger 	/*
3632c66ac9dbSNicholas Bellinger 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3633c66ac9dbSNicholas Bellinger 	 */
36341ae1602dSChristoph Hellwig 	config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
36351ae1602dSChristoph Hellwig 	configfs_add_default_group(&alua_group, &target_core_hbagroup);
36361ae1602dSChristoph Hellwig 
3637c66ac9dbSNicholas Bellinger 	/*
3638c66ac9dbSNicholas Bellinger 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3639c66ac9dbSNicholas Bellinger 	 * groups under /sys/kernel/config/target/core/alua/
3640c66ac9dbSNicholas Bellinger 	 */
36411ae1602dSChristoph Hellwig 	config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
36421ae1602dSChristoph Hellwig 			&target_core_alua_lu_gps_cit);
36431ae1602dSChristoph Hellwig 	configfs_add_default_group(&alua_lu_gps_group, &alua_group);
3644c66ac9dbSNicholas Bellinger 
3645c66ac9dbSNicholas Bellinger 	/*
3646c66ac9dbSNicholas Bellinger 	 * Add core/alua/lu_gps/default_lu_gp
3647c66ac9dbSNicholas Bellinger 	 */
3648c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
364937bb7899SPeter Senna Tschudin 	if (IS_ERR(lu_gp)) {
365037bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3651c66ac9dbSNicholas Bellinger 		goto out_global;
365237bb7899SPeter Senna Tschudin 	}
3653c66ac9dbSNicholas Bellinger 
3654c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3655c66ac9dbSNicholas Bellinger 				&target_core_alua_lu_gp_cit);
36561ae1602dSChristoph Hellwig 	configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
36571ae1602dSChristoph Hellwig 
3658e3d6f909SAndy Grover 	default_lu_gp = lu_gp;
36591ae1602dSChristoph Hellwig 
3660c66ac9dbSNicholas Bellinger 	/*
3661c66ac9dbSNicholas Bellinger 	 * Register the target_core_mod subsystem with configfs.
3662c66ac9dbSNicholas Bellinger 	 */
3663c66ac9dbSNicholas Bellinger 	ret = configfs_register_subsystem(subsys);
3664c66ac9dbSNicholas Bellinger 	if (ret < 0) {
36656708bb27SAndy Grover 		pr_err("Error %d while registering subsystem %s\n",
3666c66ac9dbSNicholas Bellinger 			ret, subsys->su_group.cg_item.ci_namebuf);
3667c66ac9dbSNicholas Bellinger 		goto out_global;
3668c66ac9dbSNicholas Bellinger 	}
36696708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3670ce8dd25dSChristoph Hellwig 		" Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3671c66ac9dbSNicholas Bellinger 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3672c66ac9dbSNicholas Bellinger 	/*
3673c66ac9dbSNicholas Bellinger 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3674c66ac9dbSNicholas Bellinger 	 */
3675c66ac9dbSNicholas Bellinger 	ret = rd_module_init();
3676c66ac9dbSNicholas Bellinger 	if (ret < 0)
3677c66ac9dbSNicholas Bellinger 		goto out;
3678c66ac9dbSNicholas Bellinger 
36790d0f9dfbSRoland Dreier 	ret = core_dev_setup_virtual_lun0();
36800d0f9dfbSRoland Dreier 	if (ret < 0)
3681c66ac9dbSNicholas Bellinger 		goto out;
3682c66ac9dbSNicholas Bellinger 
3683f99715acSNicholas Bellinger 	ret = target_xcopy_setup_pt();
3684f99715acSNicholas Bellinger 	if (ret < 0)
3685f99715acSNicholas Bellinger 		goto out;
3686f99715acSNicholas Bellinger 
368778a6295cSLee Duncan 	target_init_dbroot();
368878a6295cSLee Duncan 
3689c66ac9dbSNicholas Bellinger 	return 0;
3690c66ac9dbSNicholas Bellinger 
3691c66ac9dbSNicholas Bellinger out:
3692c66ac9dbSNicholas Bellinger 	configfs_unregister_subsystem(subsys);
3693c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3694c66ac9dbSNicholas Bellinger 	rd_module_exit();
3695c66ac9dbSNicholas Bellinger out_global:
3696e3d6f909SAndy Grover 	if (default_lu_gp) {
3697e3d6f909SAndy Grover 		core_alua_free_lu_gp(default_lu_gp);
3698e3d6f909SAndy Grover 		default_lu_gp = NULL;
3699c66ac9dbSNicholas Bellinger 	}
3700e3d6f909SAndy Grover 	release_se_kmem_caches();
3701e3d6f909SAndy Grover 	return ret;
3702c66ac9dbSNicholas Bellinger }
3703c66ac9dbSNicholas Bellinger 
370454550fabSAxel Lin static void __exit target_core_exit_configfs(void)
3705c66ac9dbSNicholas Bellinger {
37061ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&alua_lu_gps_group);
37071ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&alua_group);
37081ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&target_core_hbagroup);
3709c66ac9dbSNicholas Bellinger 
37107c2bf6e9SNicholas Bellinger 	/*
37117c2bf6e9SNicholas Bellinger 	 * We expect subsys->su_group.default_groups to be released
37127c2bf6e9SNicholas Bellinger 	 * by configfs subsystem provider logic..
37137c2bf6e9SNicholas Bellinger 	 */
3714d588cf8fSChristoph Hellwig 	configfs_unregister_subsystem(&target_core_fabrics);
3715c66ac9dbSNicholas Bellinger 
3716e3d6f909SAndy Grover 	core_alua_free_lu_gp(default_lu_gp);
3717e3d6f909SAndy Grover 	default_lu_gp = NULL;
37187c2bf6e9SNicholas Bellinger 
37196708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3720c66ac9dbSNicholas Bellinger 			" Infrastructure\n");
3721c66ac9dbSNicholas Bellinger 
3722c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3723c66ac9dbSNicholas Bellinger 	rd_module_exit();
3724f99715acSNicholas Bellinger 	target_xcopy_release_pt();
3725e3d6f909SAndy Grover 	release_se_kmem_caches();
3726c66ac9dbSNicholas Bellinger }
3727c66ac9dbSNicholas Bellinger 
3728c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3729c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3730c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
3731c66ac9dbSNicholas Bellinger 
3732c66ac9dbSNicholas Bellinger module_init(target_core_init_configfs);
3733c66ac9dbSNicholas Bellinger module_exit(target_core_exit_configfs);
3734