1c66ac9dbSNicholas Bellinger /*******************************************************************************
2c66ac9dbSNicholas Bellinger  * Filename:  target_core_configfs.c
3c66ac9dbSNicholas Bellinger  *
4c66ac9dbSNicholas Bellinger  * This file contains ConfigFS logic for the Generic Target Engine project.
5c66ac9dbSNicholas Bellinger  *
64c76251eSNicholas Bellinger  * (c) Copyright 2008-2013 Datera, Inc.
7c66ac9dbSNicholas Bellinger  *
8c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
9c66ac9dbSNicholas Bellinger  *
10c66ac9dbSNicholas Bellinger  * based on configfs Copyright (C) 2005 Oracle.  All rights reserved.
11c66ac9dbSNicholas Bellinger  *
12c66ac9dbSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
13c66ac9dbSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
14c66ac9dbSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
15c66ac9dbSNicholas Bellinger  * (at your option) any later version.
16c66ac9dbSNicholas Bellinger  *
17c66ac9dbSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
18c66ac9dbSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19c66ac9dbSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c66ac9dbSNicholas Bellinger  * GNU General Public License for more details.
21c66ac9dbSNicholas Bellinger  ****************************************************************************/
22c66ac9dbSNicholas Bellinger 
23c66ac9dbSNicholas Bellinger #include <linux/module.h>
24c66ac9dbSNicholas Bellinger #include <linux/moduleparam.h>
25c66ac9dbSNicholas Bellinger #include <generated/utsrelease.h>
26c66ac9dbSNicholas Bellinger #include <linux/utsname.h>
27c66ac9dbSNicholas Bellinger #include <linux/init.h>
28c66ac9dbSNicholas Bellinger #include <linux/fs.h>
29c66ac9dbSNicholas Bellinger #include <linux/namei.h>
30c66ac9dbSNicholas Bellinger #include <linux/slab.h>
31c66ac9dbSNicholas Bellinger #include <linux/types.h>
32c66ac9dbSNicholas Bellinger #include <linux/delay.h>
33c66ac9dbSNicholas Bellinger #include <linux/unistd.h>
34c66ac9dbSNicholas Bellinger #include <linux/string.h>
35c66ac9dbSNicholas Bellinger #include <linux/parser.h>
36c66ac9dbSNicholas Bellinger #include <linux/syscalls.h>
37c66ac9dbSNicholas Bellinger #include <linux/configfs.h>
38e3d6f909SAndy Grover #include <linux/spinlock.h>
39c66ac9dbSNicholas Bellinger 
40c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
41c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
42c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
43c66ac9dbSNicholas Bellinger 
44e26d99aeSChristoph Hellwig #include "target_core_internal.h"
45c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
46c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
47c66ac9dbSNicholas Bellinger #include "target_core_rd.h"
48f99715acSNicholas Bellinger #include "target_core_xcopy.h"
49c66ac9dbSNicholas Bellinger 
5073112edcSNicholas Bellinger #define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs)		\
510a06d430SChristoph Hellwig static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
5273112edcSNicholas Bellinger {									\
530a06d430SChristoph Hellwig 	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
5473112edcSNicholas Bellinger 									\
5573112edcSNicholas Bellinger 	cit->ct_item_ops = _item_ops;					\
5673112edcSNicholas Bellinger 	cit->ct_group_ops = _group_ops;					\
5773112edcSNicholas Bellinger 	cit->ct_attrs = _attrs;						\
580a06d430SChristoph Hellwig 	cit->ct_owner = tb->ops->owner;					\
590a06d430SChristoph Hellwig 	pr_debug("Setup generic %s\n", __stringify(_name));		\
600a06d430SChristoph Hellwig }
610a06d430SChristoph Hellwig 
620a06d430SChristoph Hellwig #define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops)			\
630a06d430SChristoph Hellwig static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
640a06d430SChristoph Hellwig {									\
650a06d430SChristoph Hellwig 	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
660a06d430SChristoph Hellwig 									\
670a06d430SChristoph Hellwig 	cit->ct_item_ops = _item_ops;					\
680a06d430SChristoph Hellwig 	cit->ct_group_ops = _group_ops;					\
690a06d430SChristoph Hellwig 	cit->ct_attrs = tb->ops->tb_##_name##_attrs;			\
700a06d430SChristoph Hellwig 	cit->ct_owner = tb->ops->owner;					\
7173112edcSNicholas Bellinger 	pr_debug("Setup generic %s\n", __stringify(_name));		\
7273112edcSNicholas Bellinger }
7373112edcSNicholas Bellinger 
74e3d6f909SAndy Grover extern struct t10_alua_lu_gp *default_lu_gp;
75e3d6f909SAndy Grover 
76d0f474e5SRoland Dreier static LIST_HEAD(g_tf_list);
77d0f474e5SRoland Dreier static DEFINE_MUTEX(g_tf_lock);
78c66ac9dbSNicholas Bellinger 
79e3d6f909SAndy Grover static struct config_group target_core_hbagroup;
80e3d6f909SAndy Grover static struct config_group alua_group;
81e3d6f909SAndy Grover static struct config_group alua_lu_gps_group;
82e3d6f909SAndy Grover 
83c66ac9dbSNicholas Bellinger static inline struct se_hba *
84c66ac9dbSNicholas Bellinger item_to_hba(struct config_item *item)
85c66ac9dbSNicholas Bellinger {
86c66ac9dbSNicholas Bellinger 	return container_of(to_config_group(item), struct se_hba, hba_group);
87c66ac9dbSNicholas Bellinger }
88c66ac9dbSNicholas Bellinger 
89c66ac9dbSNicholas Bellinger /*
90c66ac9dbSNicholas Bellinger  * Attributes for /sys/kernel/config/target/
91c66ac9dbSNicholas Bellinger  */
922eafd729SChristoph Hellwig static ssize_t target_core_item_version_show(struct config_item *item,
93c66ac9dbSNicholas Bellinger 		char *page)
94c66ac9dbSNicholas Bellinger {
95c66ac9dbSNicholas Bellinger 	return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
96ce8dd25dSChristoph Hellwig 		" on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
97c66ac9dbSNicholas Bellinger 		utsname()->sysname, utsname()->machine);
98c66ac9dbSNicholas Bellinger }
99c66ac9dbSNicholas Bellinger 
1002eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_core_item_, version);
101c66ac9dbSNicholas Bellinger 
102a96e9783SLee Duncan char db_root[DB_ROOT_LEN] = DB_ROOT_DEFAULT;
103a96e9783SLee Duncan static char db_root_stage[DB_ROOT_LEN];
104a96e9783SLee Duncan 
105a96e9783SLee Duncan static ssize_t target_core_item_dbroot_show(struct config_item *item,
106a96e9783SLee Duncan 					    char *page)
107a96e9783SLee Duncan {
108a96e9783SLee Duncan 	return sprintf(page, "%s\n", db_root);
109a96e9783SLee Duncan }
110a96e9783SLee Duncan 
111a96e9783SLee Duncan static ssize_t target_core_item_dbroot_store(struct config_item *item,
112a96e9783SLee Duncan 					const char *page, size_t count)
113a96e9783SLee Duncan {
114a96e9783SLee Duncan 	ssize_t read_bytes;
115a96e9783SLee Duncan 	struct file *fp;
116a96e9783SLee Duncan 
117a96e9783SLee Duncan 	mutex_lock(&g_tf_lock);
118a96e9783SLee Duncan 	if (!list_empty(&g_tf_list)) {
119a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
120a96e9783SLee Duncan 		pr_err("db_root: cannot be changed: target drivers registered");
121a96e9783SLee Duncan 		return -EINVAL;
122a96e9783SLee Duncan 	}
123a96e9783SLee Duncan 
124a96e9783SLee Duncan 	if (count > (DB_ROOT_LEN - 1)) {
125a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
126a96e9783SLee Duncan 		pr_err("db_root: count %d exceeds DB_ROOT_LEN-1: %u\n",
127a96e9783SLee Duncan 		       (int)count, DB_ROOT_LEN - 1);
128a96e9783SLee Duncan 		return -EINVAL;
129a96e9783SLee Duncan 	}
130a96e9783SLee Duncan 
131a96e9783SLee Duncan 	read_bytes = snprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
132a96e9783SLee Duncan 	if (!read_bytes) {
133a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
134a96e9783SLee Duncan 		return -EINVAL;
135a96e9783SLee Duncan 	}
136a96e9783SLee Duncan 	if (db_root_stage[read_bytes - 1] == '\n')
137a96e9783SLee Duncan 		db_root_stage[read_bytes - 1] = '\0';
138a96e9783SLee Duncan 
139a96e9783SLee Duncan 	/* validate new db root before accepting it */
140a96e9783SLee Duncan 	fp = filp_open(db_root_stage, O_RDONLY, 0);
141a96e9783SLee Duncan 	if (IS_ERR(fp)) {
142a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
143a96e9783SLee Duncan 		pr_err("db_root: cannot open: %s\n", db_root_stage);
144a96e9783SLee Duncan 		return -EINVAL;
145a96e9783SLee Duncan 	}
14645063097SAl Viro 	if (!S_ISDIR(file_inode(fp)->i_mode)) {
1478cc3bb07SBart Van Assche 		filp_close(fp, NULL);
148a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
149a96e9783SLee Duncan 		pr_err("db_root: not a directory: %s\n", db_root_stage);
150a96e9783SLee Duncan 		return -EINVAL;
151a96e9783SLee Duncan 	}
1528cc3bb07SBart Van Assche 	filp_close(fp, NULL);
153a96e9783SLee Duncan 
154a96e9783SLee Duncan 	strncpy(db_root, db_root_stage, read_bytes);
155a96e9783SLee Duncan 
156a96e9783SLee Duncan 	mutex_unlock(&g_tf_lock);
157a96e9783SLee Duncan 
15878a6295cSLee Duncan 	pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
15978a6295cSLee Duncan 
160a96e9783SLee Duncan 	return read_bytes;
161a96e9783SLee Duncan }
162a96e9783SLee Duncan 
163a96e9783SLee Duncan CONFIGFS_ATTR(target_core_item_, dbroot);
164a96e9783SLee Duncan 
165c66ac9dbSNicholas Bellinger static struct target_fabric_configfs *target_core_get_fabric(
166c66ac9dbSNicholas Bellinger 	const char *name)
167c66ac9dbSNicholas Bellinger {
168c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
169c66ac9dbSNicholas Bellinger 
1706708bb27SAndy Grover 	if (!name)
171c66ac9dbSNicholas Bellinger 		return NULL;
172c66ac9dbSNicholas Bellinger 
173c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
174c66ac9dbSNicholas Bellinger 	list_for_each_entry(tf, &g_tf_list, tf_list) {
17559a206b4SDavid Disseldorp 		const char *cmp_name = tf->tf_ops->fabric_alias;
17659a206b4SDavid Disseldorp 		if (!cmp_name)
17759a206b4SDavid Disseldorp 			cmp_name = tf->tf_ops->fabric_name;
17859a206b4SDavid Disseldorp 		if (!strcmp(cmp_name, name)) {
179c66ac9dbSNicholas Bellinger 			atomic_inc(&tf->tf_access_cnt);
180c66ac9dbSNicholas Bellinger 			mutex_unlock(&g_tf_lock);
181c66ac9dbSNicholas Bellinger 			return tf;
182c66ac9dbSNicholas Bellinger 		}
183c66ac9dbSNicholas Bellinger 	}
184c66ac9dbSNicholas Bellinger 	mutex_unlock(&g_tf_lock);
185c66ac9dbSNicholas Bellinger 
186c66ac9dbSNicholas Bellinger 	return NULL;
187c66ac9dbSNicholas Bellinger }
188c66ac9dbSNicholas Bellinger 
189c66ac9dbSNicholas Bellinger /*
190c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->make_group()
191c66ac9dbSNicholas Bellinger  */
192c66ac9dbSNicholas Bellinger static struct config_group *target_core_register_fabric(
193c66ac9dbSNicholas Bellinger 	struct config_group *group,
194c66ac9dbSNicholas Bellinger 	const char *name)
195c66ac9dbSNicholas Bellinger {
196c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
197c66ac9dbSNicholas Bellinger 	int ret;
198c66ac9dbSNicholas Bellinger 
1996708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
200c66ac9dbSNicholas Bellinger 			" %s\n", group, name);
201e7b7af6eSRoland Dreier 
202e7b7af6eSRoland Dreier 	tf = target_core_get_fabric(name);
203e7b7af6eSRoland Dreier 	if (!tf) {
20462554910SNicholas Bellinger 		pr_debug("target_core_register_fabric() trying autoload for %s\n",
205e7b7af6eSRoland Dreier 			 name);
206e7b7af6eSRoland Dreier 
207c66ac9dbSNicholas Bellinger 		/*
208c66ac9dbSNicholas Bellinger 		 * Below are some hardcoded request_module() calls to automatically
209c66ac9dbSNicholas Bellinger 		 * local fabric modules when the following is called:
210c66ac9dbSNicholas Bellinger 		 *
211c66ac9dbSNicholas Bellinger 		 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
212c66ac9dbSNicholas Bellinger 		 *
213c66ac9dbSNicholas Bellinger 		 * Note that this does not limit which TCM fabric module can be
214c66ac9dbSNicholas Bellinger 		 * registered, but simply provids auto loading logic for modules with
215c66ac9dbSNicholas Bellinger 		 * mkdir(2) system calls with known TCM fabric modules.
216c66ac9dbSNicholas Bellinger 		 */
217e7b7af6eSRoland Dreier 
2186708bb27SAndy Grover 		if (!strncmp(name, "iscsi", 5)) {
219c66ac9dbSNicholas Bellinger 			/*
220c66ac9dbSNicholas Bellinger 			 * Automatically load the LIO Target fabric module when the
221c66ac9dbSNicholas Bellinger 			 * following is called:
222c66ac9dbSNicholas Bellinger 			 *
223c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/iscsi
224c66ac9dbSNicholas Bellinger 			 */
225c66ac9dbSNicholas Bellinger 			ret = request_module("iscsi_target_mod");
226c66ac9dbSNicholas Bellinger 			if (ret < 0) {
22762554910SNicholas Bellinger 				pr_debug("request_module() failed for"
228c66ac9dbSNicholas Bellinger 				         " iscsi_target_mod.ko: %d\n", ret);
229c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
230c66ac9dbSNicholas Bellinger 			}
2316708bb27SAndy Grover 		} else if (!strncmp(name, "loopback", 8)) {
232c66ac9dbSNicholas Bellinger 			/*
233c66ac9dbSNicholas Bellinger 			 * Automatically load the tcm_loop fabric module when the
234c66ac9dbSNicholas Bellinger 			 * following is called:
235c66ac9dbSNicholas Bellinger 			 *
236c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/loopback
237c66ac9dbSNicholas Bellinger 			 */
238c66ac9dbSNicholas Bellinger 			ret = request_module("tcm_loop");
239c66ac9dbSNicholas Bellinger 			if (ret < 0) {
24062554910SNicholas Bellinger 				pr_debug("request_module() failed for"
241c66ac9dbSNicholas Bellinger 				         " tcm_loop.ko: %d\n", ret);
242c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
243c66ac9dbSNicholas Bellinger 			}
244c66ac9dbSNicholas Bellinger 		}
245c66ac9dbSNicholas Bellinger 
246c66ac9dbSNicholas Bellinger 		tf = target_core_get_fabric(name);
247e7b7af6eSRoland Dreier 	}
248e7b7af6eSRoland Dreier 
2496708bb27SAndy Grover 	if (!tf) {
25062554910SNicholas Bellinger 		pr_debug("target_core_get_fabric() failed for %s\n",
251c66ac9dbSNicholas Bellinger 		         name);
252c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
253c66ac9dbSNicholas Bellinger 	}
2546708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
25559a206b4SDavid Disseldorp 			" %s\n", tf->tf_ops->fabric_name);
256c66ac9dbSNicholas Bellinger 	/*
257c66ac9dbSNicholas Bellinger 	 * On a successful target_core_get_fabric() look, the returned
258c66ac9dbSNicholas Bellinger 	 * struct target_fabric_configfs *tf will contain a usage reference.
259c66ac9dbSNicholas Bellinger 	 */
2606708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
261968ebe75SChristoph Hellwig 			&tf->tf_wwn_cit);
262c66ac9dbSNicholas Bellinger 
263968ebe75SChristoph Hellwig 	config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
2641ae1602dSChristoph Hellwig 
265c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
266968ebe75SChristoph Hellwig 			&tf->tf_discovery_cit);
2671ae1602dSChristoph Hellwig 	configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
268c66ac9dbSNicholas Bellinger 
2696f3bf5a2SBart Van Assche 	pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric: %s\n",
2706f3bf5a2SBart Van Assche 		 config_item_name(&tf->tf_group.cg_item));
271c66ac9dbSNicholas Bellinger 	return &tf->tf_group;
272c66ac9dbSNicholas Bellinger }
273c66ac9dbSNicholas Bellinger 
274c66ac9dbSNicholas Bellinger /*
275c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->drop_item()
276c66ac9dbSNicholas Bellinger  */
277c66ac9dbSNicholas Bellinger static void target_core_deregister_fabric(
278c66ac9dbSNicholas Bellinger 	struct config_group *group,
279c66ac9dbSNicholas Bellinger 	struct config_item *item)
280c66ac9dbSNicholas Bellinger {
281c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf = container_of(
282c66ac9dbSNicholas Bellinger 		to_config_group(item), struct target_fabric_configfs, tf_group);
283c66ac9dbSNicholas Bellinger 
2846708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
285c66ac9dbSNicholas Bellinger 		" tf list\n", config_item_name(item));
286c66ac9dbSNicholas Bellinger 
2876708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
28859a206b4SDavid Disseldorp 			" %s\n", tf->tf_ops->fabric_name);
289c66ac9dbSNicholas Bellinger 	atomic_dec(&tf->tf_access_cnt);
290c66ac9dbSNicholas Bellinger 
2916708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
292c66ac9dbSNicholas Bellinger 			" %s\n", config_item_name(item));
293c66ac9dbSNicholas Bellinger 
2941ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&tf->tf_group);
295c66ac9dbSNicholas Bellinger 	config_item_put(item);
296c66ac9dbSNicholas Bellinger }
297c66ac9dbSNicholas Bellinger 
298c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_fabric_group_ops = {
299c66ac9dbSNicholas Bellinger 	.make_group	= &target_core_register_fabric,
300c66ac9dbSNicholas Bellinger 	.drop_item	= &target_core_deregister_fabric,
301c66ac9dbSNicholas Bellinger };
302c66ac9dbSNicholas Bellinger 
303c66ac9dbSNicholas Bellinger /*
304c66ac9dbSNicholas Bellinger  * All item attributes appearing in /sys/kernel/target/ appear here.
305c66ac9dbSNicholas Bellinger  */
306c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_fabric_item_attrs[] = {
307c66ac9dbSNicholas Bellinger 	&target_core_item_attr_version,
308a96e9783SLee Duncan 	&target_core_item_attr_dbroot,
309c66ac9dbSNicholas Bellinger 	NULL,
310c66ac9dbSNicholas Bellinger };
311c66ac9dbSNicholas Bellinger 
312c66ac9dbSNicholas Bellinger /*
313c66ac9dbSNicholas Bellinger  * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
314c66ac9dbSNicholas Bellinger  */
315ece550b5SBhumika Goyal static const struct config_item_type target_core_fabrics_item = {
316c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_fabric_group_ops,
317c66ac9dbSNicholas Bellinger 	.ct_attrs	= target_core_fabric_item_attrs,
318c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
319c66ac9dbSNicholas Bellinger };
320c66ac9dbSNicholas Bellinger 
321c66ac9dbSNicholas Bellinger static struct configfs_subsystem target_core_fabrics = {
322c66ac9dbSNicholas Bellinger 	.su_group = {
323c66ac9dbSNicholas Bellinger 		.cg_item = {
324c66ac9dbSNicholas Bellinger 			.ci_namebuf = "target",
325c66ac9dbSNicholas Bellinger 			.ci_type = &target_core_fabrics_item,
326c66ac9dbSNicholas Bellinger 		},
327c66ac9dbSNicholas Bellinger 	},
328c66ac9dbSNicholas Bellinger };
329c66ac9dbSNicholas Bellinger 
330d588cf8fSChristoph Hellwig int target_depend_item(struct config_item *item)
331d588cf8fSChristoph Hellwig {
332d588cf8fSChristoph Hellwig 	return configfs_depend_item(&target_core_fabrics, item);
333d588cf8fSChristoph Hellwig }
334d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_depend_item);
335d588cf8fSChristoph Hellwig 
336d588cf8fSChristoph Hellwig void target_undepend_item(struct config_item *item)
337d588cf8fSChristoph Hellwig {
3389a9e3415SKrzysztof Opasiak 	return configfs_undepend_item(item);
339d588cf8fSChristoph Hellwig }
340d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_undepend_item);
341c66ac9dbSNicholas Bellinger 
342c66ac9dbSNicholas Bellinger /*##############################################################################
343c66ac9dbSNicholas Bellinger // Start functions called by external Target Fabrics Modules
344c66ac9dbSNicholas Bellinger //############################################################################*/
345c66ac9dbSNicholas Bellinger 
3469ac8928eSChristoph Hellwig static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
347c66ac9dbSNicholas Bellinger {
34859a206b4SDavid Disseldorp 	if (tfo->fabric_alias) {
34959a206b4SDavid Disseldorp 		if (strlen(tfo->fabric_alias) >= TARGET_FABRIC_NAME_SIZE) {
35059a206b4SDavid Disseldorp 			pr_err("Passed alias: %s exceeds "
35159a206b4SDavid Disseldorp 				"TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_alias);
3529ac8928eSChristoph Hellwig 			return -EINVAL;
353c66ac9dbSNicholas Bellinger 		}
354c66ac9dbSNicholas Bellinger 	}
35530c7ca93SDavid Disseldorp 	if (!tfo->fabric_name) {
35630c7ca93SDavid Disseldorp 		pr_err("Missing tfo->fabric_name\n");
357c66ac9dbSNicholas Bellinger 		return -EINVAL;
358c66ac9dbSNicholas Bellinger 	}
35959a206b4SDavid Disseldorp 	if (strlen(tfo->fabric_name) >= TARGET_FABRIC_NAME_SIZE) {
36059a206b4SDavid Disseldorp 		pr_err("Passed name: %s exceeds "
36159a206b4SDavid Disseldorp 			"TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_name);
36259a206b4SDavid Disseldorp 		return -EINVAL;
36359a206b4SDavid Disseldorp 	}
3646708bb27SAndy Grover 	if (!tfo->tpg_get_wwn) {
3656708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_wwn()\n");
366c66ac9dbSNicholas Bellinger 		return -EINVAL;
367c66ac9dbSNicholas Bellinger 	}
3686708bb27SAndy Grover 	if (!tfo->tpg_get_tag) {
3696708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_tag()\n");
370c66ac9dbSNicholas Bellinger 		return -EINVAL;
371c66ac9dbSNicholas Bellinger 	}
3726708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode) {
3736708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode()\n");
374c66ac9dbSNicholas Bellinger 		return -EINVAL;
375c66ac9dbSNicholas Bellinger 	}
3766708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_cache) {
3776708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
378c66ac9dbSNicholas Bellinger 		return -EINVAL;
379c66ac9dbSNicholas Bellinger 	}
3806708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_write_protect) {
3816708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
382c66ac9dbSNicholas Bellinger 		return -EINVAL;
383c66ac9dbSNicholas Bellinger 	}
3846708bb27SAndy Grover 	if (!tfo->tpg_check_prod_mode_write_protect) {
3856708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
386c66ac9dbSNicholas Bellinger 		return -EINVAL;
387c66ac9dbSNicholas Bellinger 	}
3886708bb27SAndy Grover 	if (!tfo->tpg_get_inst_index) {
3896708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_inst_index()\n");
390c66ac9dbSNicholas Bellinger 		return -EINVAL;
391c66ac9dbSNicholas Bellinger 	}
39235462975SChristoph Hellwig 	if (!tfo->release_cmd) {
3936708bb27SAndy Grover 		pr_err("Missing tfo->release_cmd()\n");
394c66ac9dbSNicholas Bellinger 		return -EINVAL;
395c66ac9dbSNicholas Bellinger 	}
3966708bb27SAndy Grover 	if (!tfo->sess_get_index) {
3976708bb27SAndy Grover 		pr_err("Missing tfo->sess_get_index()\n");
398c66ac9dbSNicholas Bellinger 		return -EINVAL;
399c66ac9dbSNicholas Bellinger 	}
4006708bb27SAndy Grover 	if (!tfo->write_pending) {
4016708bb27SAndy Grover 		pr_err("Missing tfo->write_pending()\n");
402c66ac9dbSNicholas Bellinger 		return -EINVAL;
403c66ac9dbSNicholas Bellinger 	}
4046708bb27SAndy Grover 	if (!tfo->write_pending_status) {
4056708bb27SAndy Grover 		pr_err("Missing tfo->write_pending_status()\n");
406c66ac9dbSNicholas Bellinger 		return -EINVAL;
407c66ac9dbSNicholas Bellinger 	}
4086708bb27SAndy Grover 	if (!tfo->set_default_node_attributes) {
4096708bb27SAndy Grover 		pr_err("Missing tfo->set_default_node_attributes()\n");
410c66ac9dbSNicholas Bellinger 		return -EINVAL;
411c66ac9dbSNicholas Bellinger 	}
4126708bb27SAndy Grover 	if (!tfo->get_cmd_state) {
4136708bb27SAndy Grover 		pr_err("Missing tfo->get_cmd_state()\n");
414c66ac9dbSNicholas Bellinger 		return -EINVAL;
415c66ac9dbSNicholas Bellinger 	}
4166708bb27SAndy Grover 	if (!tfo->queue_data_in) {
4176708bb27SAndy Grover 		pr_err("Missing tfo->queue_data_in()\n");
418c66ac9dbSNicholas Bellinger 		return -EINVAL;
419c66ac9dbSNicholas Bellinger 	}
4206708bb27SAndy Grover 	if (!tfo->queue_status) {
4216708bb27SAndy Grover 		pr_err("Missing tfo->queue_status()\n");
422c66ac9dbSNicholas Bellinger 		return -EINVAL;
423c66ac9dbSNicholas Bellinger 	}
4246708bb27SAndy Grover 	if (!tfo->queue_tm_rsp) {
4256708bb27SAndy Grover 		pr_err("Missing tfo->queue_tm_rsp()\n");
426c66ac9dbSNicholas Bellinger 		return -EINVAL;
427c66ac9dbSNicholas Bellinger 	}
428131e6abcSNicholas Bellinger 	if (!tfo->aborted_task) {
429131e6abcSNicholas Bellinger 		pr_err("Missing tfo->aborted_task()\n");
430131e6abcSNicholas Bellinger 		return -EINVAL;
431131e6abcSNicholas Bellinger 	}
4329c28ca4fSNicholas Bellinger 	if (!tfo->check_stop_free) {
4339c28ca4fSNicholas Bellinger 		pr_err("Missing tfo->check_stop_free()\n");
4349c28ca4fSNicholas Bellinger 		return -EINVAL;
4359c28ca4fSNicholas Bellinger 	}
436c66ac9dbSNicholas Bellinger 	/*
437c66ac9dbSNicholas Bellinger 	 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
438c66ac9dbSNicholas Bellinger 	 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
439c66ac9dbSNicholas Bellinger 	 * target_core_fabric_configfs.c WWN+TPG group context code.
440c66ac9dbSNicholas Bellinger 	 */
4416708bb27SAndy Grover 	if (!tfo->fabric_make_wwn) {
4426708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_wwn()\n");
443c66ac9dbSNicholas Bellinger 		return -EINVAL;
444c66ac9dbSNicholas Bellinger 	}
4456708bb27SAndy Grover 	if (!tfo->fabric_drop_wwn) {
4466708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_wwn()\n");
447c66ac9dbSNicholas Bellinger 		return -EINVAL;
448c66ac9dbSNicholas Bellinger 	}
4496708bb27SAndy Grover 	if (!tfo->fabric_make_tpg) {
4506708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_tpg()\n");
451c66ac9dbSNicholas Bellinger 		return -EINVAL;
452c66ac9dbSNicholas Bellinger 	}
4536708bb27SAndy Grover 	if (!tfo->fabric_drop_tpg) {
4546708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_tpg()\n");
455c66ac9dbSNicholas Bellinger 		return -EINVAL;
456c66ac9dbSNicholas Bellinger 	}
457c66ac9dbSNicholas Bellinger 
458c66ac9dbSNicholas Bellinger 	return 0;
459c66ac9dbSNicholas Bellinger }
460c66ac9dbSNicholas Bellinger 
4619ac8928eSChristoph Hellwig int target_register_template(const struct target_core_fabric_ops *fo)
462c66ac9dbSNicholas Bellinger {
4639ac8928eSChristoph Hellwig 	struct target_fabric_configfs *tf;
464c66ac9dbSNicholas Bellinger 	int ret;
465c66ac9dbSNicholas Bellinger 
4669ac8928eSChristoph Hellwig 	ret = target_fabric_tf_ops_check(fo);
4679ac8928eSChristoph Hellwig 	if (ret)
468c66ac9dbSNicholas Bellinger 		return ret;
469c66ac9dbSNicholas Bellinger 
4709ac8928eSChristoph Hellwig 	tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
4719ac8928eSChristoph Hellwig 	if (!tf) {
4729ac8928eSChristoph Hellwig 		pr_err("%s: could not allocate memory!\n", __func__);
4739ac8928eSChristoph Hellwig 		return -ENOMEM;
4749ac8928eSChristoph Hellwig 	}
4759ac8928eSChristoph Hellwig 
4769ac8928eSChristoph Hellwig 	INIT_LIST_HEAD(&tf->tf_list);
4779ac8928eSChristoph Hellwig 	atomic_set(&tf->tf_access_cnt, 0);
478ef0caf8dSChristoph Hellwig 	tf->tf_ops = fo;
4799ac8928eSChristoph Hellwig 	target_fabric_setup_cits(tf);
4809ac8928eSChristoph Hellwig 
4819ac8928eSChristoph Hellwig 	mutex_lock(&g_tf_lock);
4829ac8928eSChristoph Hellwig 	list_add_tail(&tf->tf_list, &g_tf_list);
4839ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4849ac8928eSChristoph Hellwig 
485c66ac9dbSNicholas Bellinger 	return 0;
486c66ac9dbSNicholas Bellinger }
4879ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_register_template);
488c66ac9dbSNicholas Bellinger 
4899ac8928eSChristoph Hellwig void target_unregister_template(const struct target_core_fabric_ops *fo)
490c66ac9dbSNicholas Bellinger {
4919ac8928eSChristoph Hellwig 	struct target_fabric_configfs *t;
492c66ac9dbSNicholas Bellinger 
493c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
4949ac8928eSChristoph Hellwig 	list_for_each_entry(t, &g_tf_list, tf_list) {
49559a206b4SDavid Disseldorp 		if (!strcmp(t->tf_ops->fabric_name, fo->fabric_name)) {
4969ac8928eSChristoph Hellwig 			BUG_ON(atomic_read(&t->tf_access_cnt));
4979ac8928eSChristoph Hellwig 			list_del(&t->tf_list);
49894509182SNicholas Bellinger 			mutex_unlock(&g_tf_lock);
49994509182SNicholas Bellinger 			/*
50094509182SNicholas Bellinger 			 * Wait for any outstanding fabric se_deve_entry->rcu_head
50194509182SNicholas Bellinger 			 * callbacks to complete post kfree_rcu(), before allowing
50294509182SNicholas Bellinger 			 * fabric driver unload of TFO->module to proceed.
50394509182SNicholas Bellinger 			 */
50494509182SNicholas Bellinger 			rcu_barrier();
5059ac8928eSChristoph Hellwig 			kfree(t);
50694509182SNicholas Bellinger 			return;
507c66ac9dbSNicholas Bellinger 		}
508c66ac9dbSNicholas Bellinger 	}
5099ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
5109ac8928eSChristoph Hellwig }
5119ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_unregister_template);
512c66ac9dbSNicholas Bellinger 
513c66ac9dbSNicholas Bellinger /*##############################################################################
514c66ac9dbSNicholas Bellinger // Stop functions called by external Target Fabrics Modules
515c66ac9dbSNicholas Bellinger //############################################################################*/
516c66ac9dbSNicholas Bellinger 
5172eafd729SChristoph Hellwig static inline struct se_dev_attrib *to_attrib(struct config_item *item)
5182eafd729SChristoph Hellwig {
5192eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_dev_attrib,
5202eafd729SChristoph Hellwig 			da_group);
5215873c4d1SChristoph Hellwig }
5225873c4d1SChristoph Hellwig 
5232eafd729SChristoph Hellwig /* Start functions for struct config_item_type tb_dev_attrib_cit */
5242eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_SHOW(_name)					\
5252eafd729SChristoph Hellwig static ssize_t _name##_show(struct config_item *item, char *page)	\
5262eafd729SChristoph Hellwig {									\
5272eafd729SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
5282eafd729SChristoph Hellwig }
5293effdb90SChristoph Hellwig 
5302eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
5312eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
5322eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
5332eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
5342eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
5352eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
5362eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
5372eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
5382eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
5392eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
5402eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
541b49d6f78SDavid Disseldorp DEF_CONFIGFS_ATTRIB_SHOW(emulate_pr);
5422eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
5432eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
544056e8924SDmitry Monakhov DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_verify);
5452eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
5462eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
5472eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
5482eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
5492eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
5502eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(block_size);
5512eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
5522eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
5532eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
5542eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
5552eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
5562eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
5572eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
5582eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
559e6f41633SJamie Pocas DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
5602eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
5612eafd729SChristoph Hellwig 
5622eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_U32(_name)				\
5632eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
5645873c4d1SChristoph Hellwig 		size_t count)						\
5655873c4d1SChristoph Hellwig {									\
5662eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5673effdb90SChristoph Hellwig 	u32 val;							\
5685873c4d1SChristoph Hellwig 	int ret;							\
5695873c4d1SChristoph Hellwig 									\
5703effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);					\
5713effdb90SChristoph Hellwig 	if (ret < 0)							\
5723effdb90SChristoph Hellwig 		return ret;						\
5733effdb90SChristoph Hellwig 	da->_name = val;						\
5743effdb90SChristoph Hellwig 	return count;							\
5755873c4d1SChristoph Hellwig }
5765873c4d1SChristoph Hellwig 
5772eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
5782eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
5792eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
5802eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
5812eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
5825873c4d1SChristoph Hellwig 
5832eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name)				\
5842eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,	\
5853effdb90SChristoph Hellwig 		size_t count)						\
5863effdb90SChristoph Hellwig {									\
5872eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5883effdb90SChristoph Hellwig 	bool flag;							\
5893effdb90SChristoph Hellwig 	int ret;							\
5903effdb90SChristoph Hellwig 									\
5913effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);					\
5923effdb90SChristoph Hellwig 	if (ret < 0)							\
5933effdb90SChristoph Hellwig 		return ret;						\
5943effdb90SChristoph Hellwig 	da->_name = flag;						\
5953effdb90SChristoph Hellwig 	return count;							\
5963effdb90SChristoph Hellwig }
5973effdb90SChristoph Hellwig 
5982eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
5992eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
6002eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
601b49d6f78SDavid Disseldorp DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_pr);
6022eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
6032eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
6043effdb90SChristoph Hellwig 
6052eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name)				\
6062eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
6073effdb90SChristoph Hellwig 		size_t count)						\
6083effdb90SChristoph Hellwig {									\
6093effdb90SChristoph Hellwig 	printk_once(KERN_WARNING					\
610234bdbc4SChristophe Vu-Brugier 		"ignoring deprecated %s attribute\n",			\
611234bdbc4SChristophe Vu-Brugier 		__stringify(_name));					\
6123effdb90SChristoph Hellwig 	return count;							\
6133effdb90SChristoph Hellwig }
6143effdb90SChristoph Hellwig 
6152eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
6162eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
6173effdb90SChristoph Hellwig 
6183effdb90SChristoph Hellwig static void dev_set_t10_wwn_model_alias(struct se_device *dev)
6193effdb90SChristoph Hellwig {
6203effdb90SChristoph Hellwig 	const char *configname;
6213effdb90SChristoph Hellwig 
6223effdb90SChristoph Hellwig 	configname = config_item_name(&dev->dev_group.cg_item);
623b2da4abfSDavid Disseldorp 	if (strlen(configname) >= INQUIRY_MODEL_LEN) {
6243effdb90SChristoph Hellwig 		pr_warn("dev[%p]: Backstore name '%s' is too long for "
625b2da4abfSDavid Disseldorp 			"INQUIRY_MODEL, truncating to 15 characters\n", dev,
6263effdb90SChristoph Hellwig 			configname);
6273effdb90SChristoph Hellwig 	}
628b2da4abfSDavid Disseldorp 	/*
629b2da4abfSDavid Disseldorp 	 * XXX We can't use sizeof(dev->t10_wwn.model) (INQUIRY_MODEL_LEN + 1)
630b2da4abfSDavid Disseldorp 	 * here without potentially breaking existing setups, so continue to
631b2da4abfSDavid Disseldorp 	 * truncate one byte shorter than what can be carried in INQUIRY.
632b2da4abfSDavid Disseldorp 	 */
633b2da4abfSDavid Disseldorp 	strlcpy(dev->t10_wwn.model, configname, INQUIRY_MODEL_LEN);
6343effdb90SChristoph Hellwig }
6353effdb90SChristoph Hellwig 
6362eafd729SChristoph Hellwig static ssize_t emulate_model_alias_store(struct config_item *item,
6373effdb90SChristoph Hellwig 		const char *page, size_t count)
6383effdb90SChristoph Hellwig {
6392eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6403effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
6413effdb90SChristoph Hellwig 	bool flag;
6423effdb90SChristoph Hellwig 	int ret;
6433effdb90SChristoph Hellwig 
6443effdb90SChristoph Hellwig 	if (dev->export_count) {
6453effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change model alias"
6463effdb90SChristoph Hellwig 			" while export_count is %d\n",
6473effdb90SChristoph Hellwig 			dev, dev->export_count);
6483effdb90SChristoph Hellwig 		return -EINVAL;
6493effdb90SChristoph Hellwig 	}
6503effdb90SChristoph Hellwig 
6513effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6523effdb90SChristoph Hellwig 	if (ret < 0)
6533effdb90SChristoph Hellwig 		return ret;
6543effdb90SChristoph Hellwig 
655b2da4abfSDavid Disseldorp 	BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
6563effdb90SChristoph Hellwig 	if (flag) {
6573effdb90SChristoph Hellwig 		dev_set_t10_wwn_model_alias(dev);
6583effdb90SChristoph Hellwig 	} else {
659b2da4abfSDavid Disseldorp 		strlcpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
660b2da4abfSDavid Disseldorp 			sizeof(dev->t10_wwn.model));
6613effdb90SChristoph Hellwig 	}
6623effdb90SChristoph Hellwig 	da->emulate_model_alias = flag;
6633effdb90SChristoph Hellwig 	return count;
6643effdb90SChristoph Hellwig }
6653effdb90SChristoph Hellwig 
6662eafd729SChristoph Hellwig static ssize_t emulate_write_cache_store(struct config_item *item,
6673effdb90SChristoph Hellwig 		const char *page, size_t count)
6683effdb90SChristoph Hellwig {
6692eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6703effdb90SChristoph Hellwig 	bool flag;
6713effdb90SChristoph Hellwig 	int ret;
6723effdb90SChristoph Hellwig 
6733effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6743effdb90SChristoph Hellwig 	if (ret < 0)
6753effdb90SChristoph Hellwig 		return ret;
6763effdb90SChristoph Hellwig 
6773effdb90SChristoph Hellwig 	if (flag && da->da_dev->transport->get_write_cache) {
6783effdb90SChristoph Hellwig 		pr_err("emulate_write_cache not supported for this device\n");
6793effdb90SChristoph Hellwig 		return -EINVAL;
6803effdb90SChristoph Hellwig 	}
6813effdb90SChristoph Hellwig 
6823effdb90SChristoph Hellwig 	da->emulate_write_cache = flag;
6833effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
6843effdb90SChristoph Hellwig 			da->da_dev, flag);
6853effdb90SChristoph Hellwig 	return count;
6863effdb90SChristoph Hellwig }
6873effdb90SChristoph Hellwig 
6882eafd729SChristoph Hellwig static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
6893effdb90SChristoph Hellwig 		const char *page, size_t count)
6903effdb90SChristoph Hellwig {
6912eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6923effdb90SChristoph Hellwig 	u32 val;
6933effdb90SChristoph Hellwig 	int ret;
6943effdb90SChristoph Hellwig 
6953effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
6963effdb90SChristoph Hellwig 	if (ret < 0)
6973effdb90SChristoph Hellwig 		return ret;
6983effdb90SChristoph Hellwig 
6993effdb90SChristoph Hellwig 	if (val != 0 && val != 1 && val != 2) {
7003effdb90SChristoph Hellwig 		pr_err("Illegal value %d\n", val);
7013effdb90SChristoph Hellwig 		return -EINVAL;
7023effdb90SChristoph Hellwig 	}
7033effdb90SChristoph Hellwig 
7043effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
7053effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
7063effdb90SChristoph Hellwig 			" UA_INTRLCK_CTRL while export_count is %d\n",
7073effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
7083effdb90SChristoph Hellwig 		return -EINVAL;
7093effdb90SChristoph Hellwig 	}
7103effdb90SChristoph Hellwig 	da->emulate_ua_intlck_ctrl = val;
7113effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
7123effdb90SChristoph Hellwig 		da->da_dev, val);
7133effdb90SChristoph Hellwig 	return count;
7143effdb90SChristoph Hellwig }
7153effdb90SChristoph Hellwig 
7162eafd729SChristoph Hellwig static ssize_t emulate_tas_store(struct config_item *item,
7173effdb90SChristoph Hellwig 		const char *page, size_t count)
7183effdb90SChristoph Hellwig {
7192eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7203effdb90SChristoph Hellwig 	bool flag;
7213effdb90SChristoph Hellwig 	int ret;
7223effdb90SChristoph Hellwig 
7233effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7243effdb90SChristoph Hellwig 	if (ret < 0)
7253effdb90SChristoph Hellwig 		return ret;
7263effdb90SChristoph Hellwig 
7273effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
7283effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TAS while"
7293effdb90SChristoph Hellwig 			" export_count is %d\n",
7303effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
7313effdb90SChristoph Hellwig 		return -EINVAL;
7323effdb90SChristoph Hellwig 	}
7333effdb90SChristoph Hellwig 	da->emulate_tas = flag;
7343effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
7353effdb90SChristoph Hellwig 		da->da_dev, flag ? "Enabled" : "Disabled");
7363effdb90SChristoph Hellwig 
7373effdb90SChristoph Hellwig 	return count;
7383effdb90SChristoph Hellwig }
7393effdb90SChristoph Hellwig 
7402eafd729SChristoph Hellwig static ssize_t emulate_tpu_store(struct config_item *item,
7413effdb90SChristoph Hellwig 		const char *page, size_t count)
7423effdb90SChristoph Hellwig {
7432eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7443effdb90SChristoph Hellwig 	bool flag;
7453effdb90SChristoph Hellwig 	int ret;
7463effdb90SChristoph Hellwig 
7473effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7483effdb90SChristoph Hellwig 	if (ret < 0)
7493effdb90SChristoph Hellwig 		return ret;
7503effdb90SChristoph Hellwig 
7513effdb90SChristoph Hellwig 	/*
7523effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
7533effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
7543effdb90SChristoph Hellwig 	 */
7553effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
7563effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
7573effdb90SChristoph Hellwig 		return -ENOSYS;
7583effdb90SChristoph Hellwig 	}
7593effdb90SChristoph Hellwig 
7603effdb90SChristoph Hellwig 	da->emulate_tpu = flag;
7613effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
7623effdb90SChristoph Hellwig 		da->da_dev, flag);
7633effdb90SChristoph Hellwig 	return count;
7643effdb90SChristoph Hellwig }
7653effdb90SChristoph Hellwig 
7662eafd729SChristoph Hellwig static ssize_t emulate_tpws_store(struct config_item *item,
7673effdb90SChristoph Hellwig 		const char *page, size_t count)
7683effdb90SChristoph Hellwig {
7692eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7703effdb90SChristoph Hellwig 	bool flag;
7713effdb90SChristoph Hellwig 	int ret;
7723effdb90SChristoph Hellwig 
7733effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7743effdb90SChristoph Hellwig 	if (ret < 0)
7753effdb90SChristoph Hellwig 		return ret;
7763effdb90SChristoph Hellwig 
7773effdb90SChristoph Hellwig 	/*
7783effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
7793effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
7803effdb90SChristoph Hellwig 	 */
7813effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
7823effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
7833effdb90SChristoph Hellwig 		return -ENOSYS;
7843effdb90SChristoph Hellwig 	}
7853effdb90SChristoph Hellwig 
7863effdb90SChristoph Hellwig 	da->emulate_tpws = flag;
7873effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
7883effdb90SChristoph Hellwig 				da->da_dev, flag);
7893effdb90SChristoph Hellwig 	return count;
7903effdb90SChristoph Hellwig }
7913effdb90SChristoph Hellwig 
7922eafd729SChristoph Hellwig static ssize_t pi_prot_type_store(struct config_item *item,
7933effdb90SChristoph Hellwig 		const char *page, size_t count)
7943effdb90SChristoph Hellwig {
7952eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7963effdb90SChristoph Hellwig 	int old_prot = da->pi_prot_type, ret;
7973effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
7983effdb90SChristoph Hellwig 	u32 flag;
7993effdb90SChristoph Hellwig 
8003effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &flag);
8013effdb90SChristoph Hellwig 	if (ret < 0)
8023effdb90SChristoph Hellwig 		return ret;
8033effdb90SChristoph Hellwig 
8043effdb90SChristoph Hellwig 	if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
8053effdb90SChristoph Hellwig 		pr_err("Illegal value %d for pi_prot_type\n", flag);
8063effdb90SChristoph Hellwig 		return -EINVAL;
8073effdb90SChristoph Hellwig 	}
8083effdb90SChristoph Hellwig 	if (flag == 2) {
8093effdb90SChristoph Hellwig 		pr_err("DIF TYPE2 protection currently not supported\n");
8103effdb90SChristoph Hellwig 		return -ENOSYS;
8113effdb90SChristoph Hellwig 	}
8123effdb90SChristoph Hellwig 	if (da->hw_pi_prot_type) {
8133effdb90SChristoph Hellwig 		pr_warn("DIF protection enabled on underlying hardware,"
8143effdb90SChristoph Hellwig 			" ignoring\n");
8153effdb90SChristoph Hellwig 		return count;
8163effdb90SChristoph Hellwig 	}
8173effdb90SChristoph Hellwig 	if (!dev->transport->init_prot || !dev->transport->free_prot) {
8183effdb90SChristoph Hellwig 		/* 0 is only allowed value for non-supporting backends */
8193effdb90SChristoph Hellwig 		if (flag == 0)
820bc1a7d6aSAndy Grover 			return count;
8213effdb90SChristoph Hellwig 
8223effdb90SChristoph Hellwig 		pr_err("DIF protection not supported by backend: %s\n",
8233effdb90SChristoph Hellwig 		       dev->transport->name);
8243effdb90SChristoph Hellwig 		return -ENOSYS;
8253effdb90SChristoph Hellwig 	}
826cb0f32e1SMike Christie 	if (!target_dev_configured(dev)) {
8273effdb90SChristoph Hellwig 		pr_err("DIF protection requires device to be configured\n");
8283effdb90SChristoph Hellwig 		return -ENODEV;
8293effdb90SChristoph Hellwig 	}
8303effdb90SChristoph Hellwig 	if (dev->export_count) {
8313effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device PROT type while"
8323effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
8333effdb90SChristoph Hellwig 		return -EINVAL;
8343effdb90SChristoph Hellwig 	}
8353effdb90SChristoph Hellwig 
8363effdb90SChristoph Hellwig 	da->pi_prot_type = flag;
8373effdb90SChristoph Hellwig 
8383effdb90SChristoph Hellwig 	if (flag && !old_prot) {
8393effdb90SChristoph Hellwig 		ret = dev->transport->init_prot(dev);
8403effdb90SChristoph Hellwig 		if (ret) {
8413effdb90SChristoph Hellwig 			da->pi_prot_type = old_prot;
842056e8924SDmitry Monakhov 			da->pi_prot_verify = (bool) da->pi_prot_type;
8433effdb90SChristoph Hellwig 			return ret;
8443effdb90SChristoph Hellwig 		}
8453effdb90SChristoph Hellwig 
8463effdb90SChristoph Hellwig 	} else if (!flag && old_prot) {
8473effdb90SChristoph Hellwig 		dev->transport->free_prot(dev);
8483effdb90SChristoph Hellwig 	}
8493effdb90SChristoph Hellwig 
850056e8924SDmitry Monakhov 	da->pi_prot_verify = (bool) da->pi_prot_type;
8513effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
8523effdb90SChristoph Hellwig 	return count;
8533effdb90SChristoph Hellwig }
8543effdb90SChristoph Hellwig 
8552eafd729SChristoph Hellwig static ssize_t pi_prot_format_store(struct config_item *item,
8563effdb90SChristoph Hellwig 		const char *page, size_t count)
8573effdb90SChristoph Hellwig {
8582eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
8593effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
8603effdb90SChristoph Hellwig 	bool flag;
8613effdb90SChristoph Hellwig 	int ret;
8623effdb90SChristoph Hellwig 
8633effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
8643effdb90SChristoph Hellwig 	if (ret < 0)
8653effdb90SChristoph Hellwig 		return ret;
8663effdb90SChristoph Hellwig 
8673effdb90SChristoph Hellwig 	if (!flag)
8683effdb90SChristoph Hellwig 		return count;
8693effdb90SChristoph Hellwig 
8703effdb90SChristoph Hellwig 	if (!dev->transport->format_prot) {
8713effdb90SChristoph Hellwig 		pr_err("DIF protection format not supported by backend %s\n",
8723effdb90SChristoph Hellwig 		       dev->transport->name);
8733effdb90SChristoph Hellwig 		return -ENOSYS;
8743effdb90SChristoph Hellwig 	}
875cb0f32e1SMike Christie 	if (!target_dev_configured(dev)) {
8763effdb90SChristoph Hellwig 		pr_err("DIF protection format requires device to be configured\n");
8773effdb90SChristoph Hellwig 		return -ENODEV;
8783effdb90SChristoph Hellwig 	}
8793effdb90SChristoph Hellwig 	if (dev->export_count) {
8803effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to format SE Device PROT type while"
8813effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
8823effdb90SChristoph Hellwig 		return -EINVAL;
8833effdb90SChristoph Hellwig 	}
8843effdb90SChristoph Hellwig 
8853effdb90SChristoph Hellwig 	ret = dev->transport->format_prot(dev);
8863effdb90SChristoph Hellwig 	if (ret)
8873effdb90SChristoph Hellwig 		return ret;
8883effdb90SChristoph Hellwig 
8893effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
8903effdb90SChristoph Hellwig 	return count;
8913effdb90SChristoph Hellwig }
8923effdb90SChristoph Hellwig 
893056e8924SDmitry Monakhov static ssize_t pi_prot_verify_store(struct config_item *item,
894056e8924SDmitry Monakhov 		const char *page, size_t count)
895056e8924SDmitry Monakhov {
896056e8924SDmitry Monakhov 	struct se_dev_attrib *da = to_attrib(item);
897056e8924SDmitry Monakhov 	bool flag;
898056e8924SDmitry Monakhov 	int ret;
899056e8924SDmitry Monakhov 
900056e8924SDmitry Monakhov 	ret = strtobool(page, &flag);
901056e8924SDmitry Monakhov 	if (ret < 0)
902056e8924SDmitry Monakhov 		return ret;
903056e8924SDmitry Monakhov 
904056e8924SDmitry Monakhov 	if (!flag) {
905056e8924SDmitry Monakhov 		da->pi_prot_verify = flag;
906056e8924SDmitry Monakhov 		return count;
907056e8924SDmitry Monakhov 	}
908056e8924SDmitry Monakhov 	if (da->hw_pi_prot_type) {
909056e8924SDmitry Monakhov 		pr_warn("DIF protection enabled on underlying hardware,"
910056e8924SDmitry Monakhov 			" ignoring\n");
911056e8924SDmitry Monakhov 		return count;
912056e8924SDmitry Monakhov 	}
913056e8924SDmitry Monakhov 	if (!da->pi_prot_type) {
914056e8924SDmitry Monakhov 		pr_warn("DIF protection not supported by backend, ignoring\n");
915056e8924SDmitry Monakhov 		return count;
916056e8924SDmitry Monakhov 	}
917056e8924SDmitry Monakhov 	da->pi_prot_verify = flag;
918056e8924SDmitry Monakhov 
919056e8924SDmitry Monakhov 	return count;
920056e8924SDmitry Monakhov }
921056e8924SDmitry Monakhov 
9222eafd729SChristoph Hellwig static ssize_t force_pr_aptpl_store(struct config_item *item,
9233effdb90SChristoph Hellwig 		const char *page, size_t count)
9243effdb90SChristoph Hellwig {
9252eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9263effdb90SChristoph Hellwig 	bool flag;
9273effdb90SChristoph Hellwig 	int ret;
9283effdb90SChristoph Hellwig 
9293effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
9303effdb90SChristoph Hellwig 	if (ret < 0)
9313effdb90SChristoph Hellwig 		return ret;
9323effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
9333effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to set force_pr_aptpl while"
9343effdb90SChristoph Hellwig 		       " export_count is %d\n",
9353effdb90SChristoph Hellwig 		       da->da_dev, da->da_dev->export_count);
9363effdb90SChristoph Hellwig 		return -EINVAL;
9373effdb90SChristoph Hellwig 	}
9383effdb90SChristoph Hellwig 
9393effdb90SChristoph Hellwig 	da->force_pr_aptpl = flag;
9403effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
9413effdb90SChristoph Hellwig 	return count;
9423effdb90SChristoph Hellwig }
9433effdb90SChristoph Hellwig 
9442eafd729SChristoph Hellwig static ssize_t emulate_rest_reord_store(struct config_item *item,
9453effdb90SChristoph Hellwig 		const char *page, size_t count)
9463effdb90SChristoph Hellwig {
9472eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9483effdb90SChristoph Hellwig 	bool flag;
9493effdb90SChristoph Hellwig 	int ret;
9503effdb90SChristoph Hellwig 
9513effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
9523effdb90SChristoph Hellwig 	if (ret < 0)
9533effdb90SChristoph Hellwig 		return ret;
9543effdb90SChristoph Hellwig 
9553effdb90SChristoph Hellwig 	if (flag != 0) {
9563effdb90SChristoph Hellwig 		printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
9573effdb90SChristoph Hellwig 			" reordering not implemented\n", da->da_dev);
9583effdb90SChristoph Hellwig 		return -ENOSYS;
9593effdb90SChristoph Hellwig 	}
9603effdb90SChristoph Hellwig 	da->emulate_rest_reord = flag;
9613effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
9623effdb90SChristoph Hellwig 		da->da_dev, flag);
9633effdb90SChristoph Hellwig 	return count;
9643effdb90SChristoph Hellwig }
9653effdb90SChristoph Hellwig 
966e6f41633SJamie Pocas static ssize_t unmap_zeroes_data_store(struct config_item *item,
967e6f41633SJamie Pocas 		const char *page, size_t count)
968e6f41633SJamie Pocas {
969e6f41633SJamie Pocas 	struct se_dev_attrib *da = to_attrib(item);
970e6f41633SJamie Pocas 	bool flag;
971e6f41633SJamie Pocas 	int ret;
972e6f41633SJamie Pocas 
973e6f41633SJamie Pocas 	ret = strtobool(page, &flag);
974e6f41633SJamie Pocas 	if (ret < 0)
975e6f41633SJamie Pocas 		return ret;
976e6f41633SJamie Pocas 
977e6f41633SJamie Pocas 	if (da->da_dev->export_count) {
978e6f41633SJamie Pocas 		pr_err("dev[%p]: Unable to change SE Device"
979e6f41633SJamie Pocas 		       " unmap_zeroes_data while export_count is %d\n",
980e6f41633SJamie Pocas 		       da->da_dev, da->da_dev->export_count);
981e6f41633SJamie Pocas 		return -EINVAL;
982e6f41633SJamie Pocas 	}
983e6f41633SJamie Pocas 	/*
984e6f41633SJamie Pocas 	 * We expect this value to be non-zero when generic Block Layer
985e6f41633SJamie Pocas 	 * Discard supported is detected iblock_configure_device().
986e6f41633SJamie Pocas 	 */
987e6f41633SJamie Pocas 	if (flag && !da->max_unmap_block_desc_count) {
988e6f41633SJamie Pocas 		pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
989e6f41633SJamie Pocas 		       " because max_unmap_block_desc_count is zero\n",
990e6f41633SJamie Pocas 		       da->da_dev);
991e6f41633SJamie Pocas 		return -ENOSYS;
992e6f41633SJamie Pocas 	}
993e6f41633SJamie Pocas 	da->unmap_zeroes_data = flag;
994e6f41633SJamie Pocas 	pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
995e6f41633SJamie Pocas 		 da->da_dev, flag);
9962e498f25SNicholas Bellinger 	return count;
997e6f41633SJamie Pocas }
998e6f41633SJamie Pocas 
9993effdb90SChristoph Hellwig /*
10003effdb90SChristoph Hellwig  * Note, this can only be called on unexported SE Device Object.
10013effdb90SChristoph Hellwig  */
10022eafd729SChristoph Hellwig static ssize_t queue_depth_store(struct config_item *item,
10033effdb90SChristoph Hellwig 		const char *page, size_t count)
10043effdb90SChristoph Hellwig {
10052eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
10063effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
10073effdb90SChristoph Hellwig 	u32 val;
10083effdb90SChristoph Hellwig 	int ret;
10093effdb90SChristoph Hellwig 
10103effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
10113effdb90SChristoph Hellwig 	if (ret < 0)
10123effdb90SChristoph Hellwig 		return ret;
10133effdb90SChristoph Hellwig 
10143effdb90SChristoph Hellwig 	if (dev->export_count) {
10153effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TCQ while"
10163effdb90SChristoph Hellwig 			" export_count is %d\n",
10173effdb90SChristoph Hellwig 			dev, dev->export_count);
10183effdb90SChristoph Hellwig 		return -EINVAL;
10193effdb90SChristoph Hellwig 	}
10203effdb90SChristoph Hellwig 	if (!val) {
10213effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
10223effdb90SChristoph Hellwig 		return -EINVAL;
10233effdb90SChristoph Hellwig 	}
10243effdb90SChristoph Hellwig 
10253effdb90SChristoph Hellwig 	if (val > dev->dev_attrib.queue_depth) {
10263effdb90SChristoph Hellwig 		if (val > dev->dev_attrib.hw_queue_depth) {
10273effdb90SChristoph Hellwig 			pr_err("dev[%p]: Passed queue_depth:"
10283effdb90SChristoph Hellwig 				" %u exceeds TCM/SE_Device MAX"
10293effdb90SChristoph Hellwig 				" TCQ: %u\n", dev, val,
10303effdb90SChristoph Hellwig 				dev->dev_attrib.hw_queue_depth);
10313effdb90SChristoph Hellwig 			return -EINVAL;
10323effdb90SChristoph Hellwig 		}
10333effdb90SChristoph Hellwig 	}
10343effdb90SChristoph Hellwig 	da->queue_depth = dev->queue_depth = val;
10353effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
10363effdb90SChristoph Hellwig 	return count;
10373effdb90SChristoph Hellwig }
10383effdb90SChristoph Hellwig 
10392eafd729SChristoph Hellwig static ssize_t optimal_sectors_store(struct config_item *item,
10403effdb90SChristoph Hellwig 		const char *page, size_t count)
10413effdb90SChristoph Hellwig {
10422eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
10433effdb90SChristoph Hellwig 	u32 val;
10443effdb90SChristoph Hellwig 	int ret;
10453effdb90SChristoph Hellwig 
10463effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
10473effdb90SChristoph Hellwig 	if (ret < 0)
10483effdb90SChristoph Hellwig 		return ret;
10493effdb90SChristoph Hellwig 
10503effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
10513effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
10523effdb90SChristoph Hellwig 			" optimal_sectors while export_count is %d\n",
10533effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
10543effdb90SChristoph Hellwig 		return -EINVAL;
10553effdb90SChristoph Hellwig 	}
10563effdb90SChristoph Hellwig 	if (val > da->hw_max_sectors) {
10573effdb90SChristoph Hellwig 		pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
10583effdb90SChristoph Hellwig 			" greater than hw_max_sectors: %u\n",
10593effdb90SChristoph Hellwig 			da->da_dev, val, da->hw_max_sectors);
10603effdb90SChristoph Hellwig 		return -EINVAL;
10613effdb90SChristoph Hellwig 	}
10623effdb90SChristoph Hellwig 
10633effdb90SChristoph Hellwig 	da->optimal_sectors = val;
10643effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
10653effdb90SChristoph Hellwig 			da->da_dev, val);
10663effdb90SChristoph Hellwig 	return count;
10673effdb90SChristoph Hellwig }
10683effdb90SChristoph Hellwig 
10692eafd729SChristoph Hellwig static ssize_t block_size_store(struct config_item *item,
10703effdb90SChristoph Hellwig 		const char *page, size_t count)
10713effdb90SChristoph Hellwig {
10722eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
10733effdb90SChristoph Hellwig 	u32 val;
10743effdb90SChristoph Hellwig 	int ret;
10753effdb90SChristoph Hellwig 
10763effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
10773effdb90SChristoph Hellwig 	if (ret < 0)
10783effdb90SChristoph Hellwig 		return ret;
10793effdb90SChristoph Hellwig 
10803effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
10813effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device block_size"
10823effdb90SChristoph Hellwig 			" while export_count is %d\n",
10833effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
10843effdb90SChristoph Hellwig 		return -EINVAL;
10853effdb90SChristoph Hellwig 	}
10863effdb90SChristoph Hellwig 
10873effdb90SChristoph Hellwig 	if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
10883effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal value for block_device: %u"
10893effdb90SChristoph Hellwig 			" for SE device, must be 512, 1024, 2048 or 4096\n",
10903effdb90SChristoph Hellwig 			da->da_dev, val);
10913effdb90SChristoph Hellwig 		return -EINVAL;
10923effdb90SChristoph Hellwig 	}
10933effdb90SChristoph Hellwig 
10943effdb90SChristoph Hellwig 	da->block_size = val;
10953effdb90SChristoph Hellwig 	if (da->max_bytes_per_io)
10963effdb90SChristoph Hellwig 		da->hw_max_sectors = da->max_bytes_per_io / val;
10973effdb90SChristoph Hellwig 
10983effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device block_size changed to %u\n",
10993effdb90SChristoph Hellwig 			da->da_dev, val);
11003effdb90SChristoph Hellwig 	return count;
11013effdb90SChristoph Hellwig }
11025873c4d1SChristoph Hellwig 
1103c17d5d5fSMike Christie static ssize_t alua_support_show(struct config_item *item, char *page)
1104c17d5d5fSMike Christie {
1105c17d5d5fSMike Christie 	struct se_dev_attrib *da = to_attrib(item);
1106c17d5d5fSMike Christie 	u8 flags = da->da_dev->transport->transport_flags;
1107c17d5d5fSMike Christie 
1108c17d5d5fSMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n",
1109c17d5d5fSMike Christie 			flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ? 0 : 1);
1110c17d5d5fSMike Christie }
1111c17d5d5fSMike Christie 
1112c17d5d5fSMike Christie static ssize_t pgr_support_show(struct config_item *item, char *page)
1113c17d5d5fSMike Christie {
1114c17d5d5fSMike Christie 	struct se_dev_attrib *da = to_attrib(item);
1115c17d5d5fSMike Christie 	u8 flags = da->da_dev->transport->transport_flags;
1116c17d5d5fSMike Christie 
1117c17d5d5fSMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n",
1118c17d5d5fSMike Christie 			flags & TRANSPORT_FLAG_PASSTHROUGH_PGR ? 0 : 1);
1119c17d5d5fSMike Christie }
1120c17d5d5fSMike Christie 
11212eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_model_alias);
11222eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_dpo);
11232eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_write);
11242eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_read);
11252eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_write_cache);
11262eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
11272eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tas);
11282eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpu);
11292eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpws);
11302eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_caw);
11312eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_3pc);
1132b49d6f78SDavid Disseldorp CONFIGFS_ATTR(, emulate_pr);
11332eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_type);
11342eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_pi_prot_type);
11356baca760SDavid Disseldorp CONFIGFS_ATTR_WO(, pi_prot_format);
1136056e8924SDmitry Monakhov CONFIGFS_ATTR(, pi_prot_verify);
11372eafd729SChristoph Hellwig CONFIGFS_ATTR(, enforce_pr_isids);
11382eafd729SChristoph Hellwig CONFIGFS_ATTR(, is_nonrot);
11392eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_rest_reord);
11402eafd729SChristoph Hellwig CONFIGFS_ATTR(, force_pr_aptpl);
11412eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_block_size);
11422eafd729SChristoph Hellwig CONFIGFS_ATTR(, block_size);
11432eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_max_sectors);
11442eafd729SChristoph Hellwig CONFIGFS_ATTR(, optimal_sectors);
11452eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_queue_depth);
11462eafd729SChristoph Hellwig CONFIGFS_ATTR(, queue_depth);
11472eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_lba_count);
11482eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_block_desc_count);
11492eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity);
11502eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity_alignment);
1151e6f41633SJamie Pocas CONFIGFS_ATTR(, unmap_zeroes_data);
11522eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_write_same_len);
1153c17d5d5fSMike Christie CONFIGFS_ATTR_RO(, alua_support);
1154c17d5d5fSMike Christie CONFIGFS_ATTR_RO(, pgr_support);
1155c66ac9dbSNicholas Bellinger 
11565873c4d1SChristoph Hellwig /*
11575873c4d1SChristoph Hellwig  * dev_attrib attributes for devices using the target core SBC/SPC
11585873c4d1SChristoph Hellwig  * interpreter.  Any backend using spc_parse_cdb should be using
11595873c4d1SChristoph Hellwig  * these.
11605873c4d1SChristoph Hellwig  */
11615873c4d1SChristoph Hellwig struct configfs_attribute *sbc_attrib_attrs[] = {
11622eafd729SChristoph Hellwig 	&attr_emulate_model_alias,
11632eafd729SChristoph Hellwig 	&attr_emulate_dpo,
11642eafd729SChristoph Hellwig 	&attr_emulate_fua_write,
11652eafd729SChristoph Hellwig 	&attr_emulate_fua_read,
11662eafd729SChristoph Hellwig 	&attr_emulate_write_cache,
11672eafd729SChristoph Hellwig 	&attr_emulate_ua_intlck_ctrl,
11682eafd729SChristoph Hellwig 	&attr_emulate_tas,
11692eafd729SChristoph Hellwig 	&attr_emulate_tpu,
11702eafd729SChristoph Hellwig 	&attr_emulate_tpws,
11712eafd729SChristoph Hellwig 	&attr_emulate_caw,
11722eafd729SChristoph Hellwig 	&attr_emulate_3pc,
1173b49d6f78SDavid Disseldorp 	&attr_emulate_pr,
11742eafd729SChristoph Hellwig 	&attr_pi_prot_type,
11752eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
11762eafd729SChristoph Hellwig 	&attr_pi_prot_format,
1177056e8924SDmitry Monakhov 	&attr_pi_prot_verify,
11782eafd729SChristoph Hellwig 	&attr_enforce_pr_isids,
11792eafd729SChristoph Hellwig 	&attr_is_nonrot,
11802eafd729SChristoph Hellwig 	&attr_emulate_rest_reord,
11812eafd729SChristoph Hellwig 	&attr_force_pr_aptpl,
11822eafd729SChristoph Hellwig 	&attr_hw_block_size,
11832eafd729SChristoph Hellwig 	&attr_block_size,
11842eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
11852eafd729SChristoph Hellwig 	&attr_optimal_sectors,
11862eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
11872eafd729SChristoph Hellwig 	&attr_queue_depth,
11882eafd729SChristoph Hellwig 	&attr_max_unmap_lba_count,
11892eafd729SChristoph Hellwig 	&attr_max_unmap_block_desc_count,
11902eafd729SChristoph Hellwig 	&attr_unmap_granularity,
11912eafd729SChristoph Hellwig 	&attr_unmap_granularity_alignment,
1192e6f41633SJamie Pocas 	&attr_unmap_zeroes_data,
11932eafd729SChristoph Hellwig 	&attr_max_write_same_len,
1194c17d5d5fSMike Christie 	&attr_alua_support,
1195c17d5d5fSMike Christie 	&attr_pgr_support,
11965873c4d1SChristoph Hellwig 	NULL,
11975873c4d1SChristoph Hellwig };
11985873c4d1SChristoph Hellwig EXPORT_SYMBOL(sbc_attrib_attrs);
11995873c4d1SChristoph Hellwig 
12005873c4d1SChristoph Hellwig /*
12015873c4d1SChristoph Hellwig  * Minimal dev_attrib attributes for devices passing through CDBs.
12025873c4d1SChristoph Hellwig  * In this case we only provide a few read-only attributes for
12035873c4d1SChristoph Hellwig  * backwards compatibility.
12045873c4d1SChristoph Hellwig  */
12055873c4d1SChristoph Hellwig struct configfs_attribute *passthrough_attrib_attrs[] = {
12062eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
12072eafd729SChristoph Hellwig 	&attr_hw_block_size,
12082eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
12092eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
1210c17d5d5fSMike Christie 	&attr_alua_support,
1211c17d5d5fSMike Christie 	&attr_pgr_support,
12125873c4d1SChristoph Hellwig 	NULL,
12135873c4d1SChristoph Hellwig };
12145873c4d1SChristoph Hellwig EXPORT_SYMBOL(passthrough_attrib_attrs);
12155873c4d1SChristoph Hellwig 
12162eafd729SChristoph Hellwig TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
12178dc31ff9SMike Christie TB_CIT_SETUP_DRV(dev_action, NULL, NULL);
1218c66ac9dbSNicholas Bellinger 
1219f79a897eSNicholas Bellinger /* End functions for struct config_item_type tb_dev_attrib_cit */
1220c66ac9dbSNicholas Bellinger 
1221f8d389c6SNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_wwn_cit */
1222c66ac9dbSNicholas Bellinger 
12232eafd729SChristoph Hellwig static struct t10_wwn *to_t10_wwn(struct config_item *item)
12242eafd729SChristoph Hellwig {
12252eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
12262eafd729SChristoph Hellwig }
1227c66ac9dbSNicholas Bellinger 
1228c66ac9dbSNicholas Bellinger /*
1229c66ac9dbSNicholas Bellinger  * VPD page 0x80 Unit serial
1230c66ac9dbSNicholas Bellinger  */
12312eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1232c66ac9dbSNicholas Bellinger 		char *page)
1233c66ac9dbSNicholas Bellinger {
1234c66ac9dbSNicholas Bellinger 	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
12352eafd729SChristoph Hellwig 		&to_t10_wwn(item)->unit_serial[0]);
1236c66ac9dbSNicholas Bellinger }
1237c66ac9dbSNicholas Bellinger 
12382eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
12392eafd729SChristoph Hellwig 		const char *page, size_t count)
1240c66ac9dbSNicholas Bellinger {
12412eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
12420fd97ccfSChristoph Hellwig 	struct se_device *dev = t10_wwn->t10_dev;
1243c66ac9dbSNicholas Bellinger 	unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1244c66ac9dbSNicholas Bellinger 
1245c66ac9dbSNicholas Bellinger 	/*
1246c66ac9dbSNicholas Bellinger 	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1247c66ac9dbSNicholas Bellinger 	 * from the struct scsi_device level firmware, do not allow
1248c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial to be emulated.
1249c66ac9dbSNicholas Bellinger 	 *
1250c66ac9dbSNicholas Bellinger 	 * Note this struct scsi_device could also be emulating VPD
1251c66ac9dbSNicholas Bellinger 	 * information from its drivers/scsi LLD.  But for now we assume
1252c66ac9dbSNicholas Bellinger 	 * it is doing 'the right thing' wrt a world wide unique
1253c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial Number that OS dependent multipath can depend on.
1254c66ac9dbSNicholas Bellinger 	 */
12550fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
12566708bb27SAndy Grover 		pr_err("Underlying SCSI device firmware provided VPD"
1257c66ac9dbSNicholas Bellinger 			" Unit Serial, ignoring request\n");
1258c66ac9dbSNicholas Bellinger 		return -EOPNOTSUPP;
1259c66ac9dbSNicholas Bellinger 	}
1260c66ac9dbSNicholas Bellinger 
126160d645a4SDan Carpenter 	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
12626708bb27SAndy Grover 		pr_err("Emulated VPD Unit Serial exceeds"
1263c66ac9dbSNicholas Bellinger 		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1264c66ac9dbSNicholas Bellinger 		return -EOVERFLOW;
1265c66ac9dbSNicholas Bellinger 	}
1266c66ac9dbSNicholas Bellinger 	/*
1267c66ac9dbSNicholas Bellinger 	 * Check to see if any active $FABRIC_MOD exports exist.  If they
1268c66ac9dbSNicholas Bellinger 	 * do exist, fail here as changing this information on the fly
1269c66ac9dbSNicholas Bellinger 	 * (underneath the initiator side OS dependent multipath code)
1270c66ac9dbSNicholas Bellinger 	 * could cause negative effects.
1271c66ac9dbSNicholas Bellinger 	 */
12720fd97ccfSChristoph Hellwig 	if (dev->export_count) {
12736708bb27SAndy Grover 		pr_err("Unable to set VPD Unit Serial while"
1274c66ac9dbSNicholas Bellinger 			" active %d $FABRIC_MOD exports exist\n",
12750fd97ccfSChristoph Hellwig 			dev->export_count);
1276c66ac9dbSNicholas Bellinger 		return -EINVAL;
1277c66ac9dbSNicholas Bellinger 	}
12780fd97ccfSChristoph Hellwig 
1279c66ac9dbSNicholas Bellinger 	/*
1280c66ac9dbSNicholas Bellinger 	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1281c66ac9dbSNicholas Bellinger 	 *
1282c66ac9dbSNicholas Bellinger 	 * Also, strip any newline added from the userspace
1283c66ac9dbSNicholas Bellinger 	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1284c66ac9dbSNicholas Bellinger 	 */
1285c66ac9dbSNicholas Bellinger 	memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1286c66ac9dbSNicholas Bellinger 	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
12870fd97ccfSChristoph Hellwig 	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1288c66ac9dbSNicholas Bellinger 			"%s", strstrip(buf));
12890fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1290c66ac9dbSNicholas Bellinger 
12916708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
12920fd97ccfSChristoph Hellwig 			" %s\n", dev->t10_wwn.unit_serial);
1293c66ac9dbSNicholas Bellinger 
1294c66ac9dbSNicholas Bellinger 	return count;
1295c66ac9dbSNicholas Bellinger }
1296c66ac9dbSNicholas Bellinger 
1297c66ac9dbSNicholas Bellinger /*
1298c66ac9dbSNicholas Bellinger  * VPD page 0x83 Protocol Identifier
1299c66ac9dbSNicholas Bellinger  */
13002eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1301c66ac9dbSNicholas Bellinger 		char *page)
1302c66ac9dbSNicholas Bellinger {
13032eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1304c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;
1305c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];
1306c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1307c66ac9dbSNicholas Bellinger 
1308c66ac9dbSNicholas Bellinger 	memset(buf, 0, VPD_TMP_BUF_SIZE);
1309c66ac9dbSNicholas Bellinger 
1310c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);
1311c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
13126708bb27SAndy Grover 		if (!vpd->protocol_identifier_set)
1313c66ac9dbSNicholas Bellinger 			continue;
1314c66ac9dbSNicholas Bellinger 
1315c66ac9dbSNicholas Bellinger 		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1316c66ac9dbSNicholas Bellinger 
13176708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1318c66ac9dbSNicholas Bellinger 			break;
1319c66ac9dbSNicholas Bellinger 
1320c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1321c66ac9dbSNicholas Bellinger 	}
1322c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);
1323c66ac9dbSNicholas Bellinger 
1324c66ac9dbSNicholas Bellinger 	return len;
1325c66ac9dbSNicholas Bellinger }
1326c66ac9dbSNicholas Bellinger 
1327c66ac9dbSNicholas Bellinger /*
1328c66ac9dbSNicholas Bellinger  * Generic wrapper for dumping VPD identifiers by association.
1329c66ac9dbSNicholas Bellinger  */
1330c66ac9dbSNicholas Bellinger #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
13312eafd729SChristoph Hellwig static ssize_t target_wwn_##_name##_show(struct config_item *item,	\
1332c66ac9dbSNicholas Bellinger 		char *page)						\
1333c66ac9dbSNicholas Bellinger {									\
13342eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);			\
1335c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;						\
1336c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];				\
1337c66ac9dbSNicholas Bellinger 	ssize_t len = 0;						\
1338c66ac9dbSNicholas Bellinger 									\
1339c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);				\
1340c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
1341c66ac9dbSNicholas Bellinger 		if (vpd->association != _assoc)				\
1342c66ac9dbSNicholas Bellinger 			continue;					\
1343c66ac9dbSNicholas Bellinger 									\
1344c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1345c66ac9dbSNicholas Bellinger 		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
13466708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1347c66ac9dbSNicholas Bellinger 			break;						\
1348c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1349c66ac9dbSNicholas Bellinger 									\
1350c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1351c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
13526708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1353c66ac9dbSNicholas Bellinger 			break;						\
1354c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1355c66ac9dbSNicholas Bellinger 									\
1356c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1357c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
13586708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1359c66ac9dbSNicholas Bellinger 			break;						\
1360c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1361c66ac9dbSNicholas Bellinger 	}								\
1362c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);				\
1363c66ac9dbSNicholas Bellinger 									\
1364c66ac9dbSNicholas Bellinger 	return len;							\
1365c66ac9dbSNicholas Bellinger }
1366c66ac9dbSNicholas Bellinger 
13672eafd729SChristoph Hellwig /* VPD page 0x83 Association: Logical Unit */
1368c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
13692eafd729SChristoph Hellwig /* VPD page 0x83 Association: Target Port */
1370c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
13712eafd729SChristoph Hellwig /* VPD page 0x83 Association: SCSI Target Device */
1372c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1373c66ac9dbSNicholas Bellinger 
13742eafd729SChristoph Hellwig CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
13752eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
13762eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
13772eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
13782eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1379c66ac9dbSNicholas Bellinger 
1380c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
13812eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_unit_serial,
13822eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_protocol_identifier,
13832eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_logical_unit,
13842eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_target_port,
13852eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_scsi_target_device,
1386c66ac9dbSNicholas Bellinger 	NULL,
1387c66ac9dbSNicholas Bellinger };
1388c66ac9dbSNicholas Bellinger 
13892eafd729SChristoph Hellwig TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1390c66ac9dbSNicholas Bellinger 
1391f8d389c6SNicholas Bellinger /*  End functions for struct config_item_type tb_dev_wwn_cit */
1392c66ac9dbSNicholas Bellinger 
139391e2e39bSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_pr_cit */
1394c66ac9dbSNicholas Bellinger 
13952eafd729SChristoph Hellwig static struct se_device *pr_to_dev(struct config_item *item)
13962eafd729SChristoph Hellwig {
13972eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device,
13982eafd729SChristoph Hellwig 			dev_pr_group);
13992eafd729SChristoph Hellwig }
1400c66ac9dbSNicholas Bellinger 
1401d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1402d977f437SChristoph Hellwig 		char *page)
1403c66ac9dbSNicholas Bellinger {
1404c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1405c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1406c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1407c66ac9dbSNicholas Bellinger 
1408c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1409c66ac9dbSNicholas Bellinger 
1410c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1411d977f437SChristoph Hellwig 	if (!pr_reg)
1412d977f437SChristoph Hellwig 		return sprintf(page, "No SPC-3 Reservation holder\n");
1413d977f437SChristoph Hellwig 
1414c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1415d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1416c66ac9dbSNicholas Bellinger 
1417d977f437SChristoph Hellwig 	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
141830c7ca93SDavid Disseldorp 		se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1419d2843c17SAndy Grover 		se_nacl->initiatorname, i_buf);
1420c66ac9dbSNicholas Bellinger }
1421c66ac9dbSNicholas Bellinger 
1422d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1423d977f437SChristoph Hellwig 		char *page)
1424c66ac9dbSNicholas Bellinger {
1425c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1426d977f437SChristoph Hellwig 	ssize_t len;
1427c66ac9dbSNicholas Bellinger 
1428c66ac9dbSNicholas Bellinger 	se_nacl = dev->dev_reserved_node_acl;
1429d977f437SChristoph Hellwig 	if (se_nacl) {
1430d977f437SChristoph Hellwig 		len = sprintf(page,
1431d977f437SChristoph Hellwig 			      "SPC-2 Reservation: %s Initiator: %s\n",
143230c7ca93SDavid Disseldorp 			      se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1433c66ac9dbSNicholas Bellinger 			      se_nacl->initiatorname);
1434d977f437SChristoph Hellwig 	} else {
1435d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-2 Reservation holder\n");
1436d977f437SChristoph Hellwig 	}
1437d977f437SChristoph Hellwig 	return len;
1438c66ac9dbSNicholas Bellinger }
1439c66ac9dbSNicholas Bellinger 
14402eafd729SChristoph Hellwig static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1441c66ac9dbSNicholas Bellinger {
14422eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1443d977f437SChristoph Hellwig 	int ret;
1444c66ac9dbSNicholas Bellinger 
1445b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr)
1446b49d6f78SDavid Disseldorp 		return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1447b49d6f78SDavid Disseldorp 
14484ec5bf0eSBryant G. Ly 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1449d977f437SChristoph Hellwig 		return sprintf(page, "Passthrough\n");
1450c66ac9dbSNicholas Bellinger 
1451d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
1452d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1453d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc2_res(dev, page);
1454d977f437SChristoph Hellwig 	else
1455d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc3_res(dev, page);
1456d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1457d977f437SChristoph Hellwig 	return ret;
1458c66ac9dbSNicholas Bellinger }
1459c66ac9dbSNicholas Bellinger 
14602eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
14612eafd729SChristoph Hellwig 		char *page)
1462c66ac9dbSNicholas Bellinger {
14632eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1464c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1465c66ac9dbSNicholas Bellinger 
1466c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1467d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder) {
1468c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1469d977f437SChristoph Hellwig 	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1470c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: All Target"
1471c66ac9dbSNicholas Bellinger 			" Ports registration\n");
1472d977f437SChristoph Hellwig 	} else {
1473c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: Single"
1474c66ac9dbSNicholas Bellinger 			" Target Port registration\n");
1475d977f437SChristoph Hellwig 	}
1476c66ac9dbSNicholas Bellinger 
1477d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1478c66ac9dbSNicholas Bellinger 	return len;
1479c66ac9dbSNicholas Bellinger }
1480c66ac9dbSNicholas Bellinger 
14812eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
14822eafd729SChristoph Hellwig 		char *page)
1483c66ac9dbSNicholas Bellinger {
14842eafd729SChristoph Hellwig 	return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1485c66ac9dbSNicholas Bellinger }
1486c66ac9dbSNicholas Bellinger 
1487c66ac9dbSNicholas Bellinger 
14882eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
14892eafd729SChristoph Hellwig 		char *page)
1490c66ac9dbSNicholas Bellinger {
14912eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1492c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1493c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
1494c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
14959ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1496c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1497c66ac9dbSNicholas Bellinger 
1498c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1499c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
15006708bb27SAndy Grover 	if (!pr_reg) {
1501c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1502d977f437SChristoph Hellwig 		goto out_unlock;
1503c66ac9dbSNicholas Bellinger 	}
1504d977f437SChristoph Hellwig 
1505c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1506c66ac9dbSNicholas Bellinger 	se_tpg = se_nacl->se_tpg;
1507e3d6f909SAndy Grover 	tfo = se_tpg->se_tpg_tfo;
1508c66ac9dbSNicholas Bellinger 
1509c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: %s"
151030c7ca93SDavid Disseldorp 		" Target Node Endpoint: %s\n", tfo->fabric_name,
1511c66ac9dbSNicholas Bellinger 		tfo->tpg_get_wwn(se_tpg));
1512c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
151335d1efe8SMasanari Iida 		" Identifier Tag: %hu %s Portal Group Tag: %hu"
1514f2d30680SHannes Reinecke 		" %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
151530c7ca93SDavid Disseldorp 		tfo->fabric_name, tfo->tpg_get_tag(se_tpg),
151630c7ca93SDavid Disseldorp 		tfo->fabric_name, pr_reg->pr_aptpl_target_lun);
1517c66ac9dbSNicholas Bellinger 
1518d977f437SChristoph Hellwig out_unlock:
1519d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1520c66ac9dbSNicholas Bellinger 	return len;
1521c66ac9dbSNicholas Bellinger }
1522c66ac9dbSNicholas Bellinger 
1523c66ac9dbSNicholas Bellinger 
15242eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
15252eafd729SChristoph Hellwig 		char *page)
1526c66ac9dbSNicholas Bellinger {
15272eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
15289ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1529c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1530c66ac9dbSNicholas Bellinger 	unsigned char buf[384];
1531c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1532c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1533d2843c17SAndy Grover 	int reg_count = 0;
1534c66ac9dbSNicholas Bellinger 
1535c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1536c66ac9dbSNicholas Bellinger 
15370fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
15380fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1539c66ac9dbSNicholas Bellinger 			pr_reg_list) {
1540c66ac9dbSNicholas Bellinger 
1541c66ac9dbSNicholas Bellinger 		memset(buf, 0, 384);
1542c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1543c66ac9dbSNicholas Bellinger 		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1544d2843c17SAndy Grover 		core_pr_dump_initiator_port(pr_reg, i_buf,
1545c66ac9dbSNicholas Bellinger 					PR_REG_ISID_ID_LEN);
1546c66ac9dbSNicholas Bellinger 		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
154730c7ca93SDavid Disseldorp 			tfo->fabric_name,
1548d2843c17SAndy Grover 			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1549c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_generation);
1550c66ac9dbSNicholas Bellinger 
15516708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1552c66ac9dbSNicholas Bellinger 			break;
1553c66ac9dbSNicholas Bellinger 
1554c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1555c66ac9dbSNicholas Bellinger 		reg_count++;
1556c66ac9dbSNicholas Bellinger 	}
15570fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
1558c66ac9dbSNicholas Bellinger 
15596708bb27SAndy Grover 	if (!reg_count)
1560c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "None\n");
1561c66ac9dbSNicholas Bellinger 
1562c66ac9dbSNicholas Bellinger 	return len;
1563c66ac9dbSNicholas Bellinger }
1564c66ac9dbSNicholas Bellinger 
15652eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1566c66ac9dbSNicholas Bellinger {
15672eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1568c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1569c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1570c66ac9dbSNicholas Bellinger 
1571c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1572c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1573d977f437SChristoph Hellwig 	if (pr_reg) {
1574c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1575c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1576d977f437SChristoph Hellwig 	} else {
1577d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1578d977f437SChristoph Hellwig 	}
1579c66ac9dbSNicholas Bellinger 
1580d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1581c66ac9dbSNicholas Bellinger 	return len;
1582c66ac9dbSNicholas Bellinger }
1583c66ac9dbSNicholas Bellinger 
15842eafd729SChristoph Hellwig static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
1585c66ac9dbSNicholas Bellinger {
15862eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
15872eafd729SChristoph Hellwig 
1588b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr)
1589b49d6f78SDavid Disseldorp 		return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
15904ec5bf0eSBryant G. Ly 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1591d977f437SChristoph Hellwig 		return sprintf(page, "SPC_PASSTHROUGH\n");
1592b49d6f78SDavid Disseldorp 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1593d977f437SChristoph Hellwig 		return sprintf(page, "SPC2_RESERVATIONS\n");
1594b49d6f78SDavid Disseldorp 
1595d977f437SChristoph Hellwig 	return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1596c66ac9dbSNicholas Bellinger }
1597c66ac9dbSNicholas Bellinger 
15982eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
15992eafd729SChristoph Hellwig 		char *page)
1600c66ac9dbSNicholas Bellinger {
16012eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
16022eafd729SChristoph Hellwig 
1603b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
1604b49d6f78SDavid Disseldorp 	    (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1605c66ac9dbSNicholas Bellinger 		return 0;
1606c66ac9dbSNicholas Bellinger 
1607c66ac9dbSNicholas Bellinger 	return sprintf(page, "APTPL Bit Status: %s\n",
16080fd97ccfSChristoph Hellwig 		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1609c66ac9dbSNicholas Bellinger }
1610c66ac9dbSNicholas Bellinger 
16112eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
16122eafd729SChristoph Hellwig 		char *page)
1613c66ac9dbSNicholas Bellinger {
16142eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
16152eafd729SChristoph Hellwig 
1616b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
1617b49d6f78SDavid Disseldorp 	    (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1618c66ac9dbSNicholas Bellinger 		return 0;
1619c66ac9dbSNicholas Bellinger 
1620c66ac9dbSNicholas Bellinger 	return sprintf(page, "Ready to process PR APTPL metadata..\n");
1621c66ac9dbSNicholas Bellinger }
1622c66ac9dbSNicholas Bellinger 
1623c66ac9dbSNicholas Bellinger enum {
1624c66ac9dbSNicholas Bellinger 	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1625c66ac9dbSNicholas Bellinger 	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1626c66ac9dbSNicholas Bellinger 	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1627c66ac9dbSNicholas Bellinger 	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1628c66ac9dbSNicholas Bellinger };
1629c66ac9dbSNicholas Bellinger 
1630c66ac9dbSNicholas Bellinger static match_table_t tokens = {
1631c66ac9dbSNicholas Bellinger 	{Opt_initiator_fabric, "initiator_fabric=%s"},
1632c66ac9dbSNicholas Bellinger 	{Opt_initiator_node, "initiator_node=%s"},
1633c66ac9dbSNicholas Bellinger 	{Opt_initiator_sid, "initiator_sid=%s"},
1634c66ac9dbSNicholas Bellinger 	{Opt_sa_res_key, "sa_res_key=%s"},
1635c66ac9dbSNicholas Bellinger 	{Opt_res_holder, "res_holder=%d"},
1636c66ac9dbSNicholas Bellinger 	{Opt_res_type, "res_type=%d"},
1637c66ac9dbSNicholas Bellinger 	{Opt_res_scope, "res_scope=%d"},
1638c66ac9dbSNicholas Bellinger 	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1639a2db857bStangwenji 	{Opt_mapped_lun, "mapped_lun=%u"},
1640c66ac9dbSNicholas Bellinger 	{Opt_target_fabric, "target_fabric=%s"},
1641c66ac9dbSNicholas Bellinger 	{Opt_target_node, "target_node=%s"},
1642c66ac9dbSNicholas Bellinger 	{Opt_tpgt, "tpgt=%d"},
1643c66ac9dbSNicholas Bellinger 	{Opt_port_rtpi, "port_rtpi=%d"},
1644a2db857bStangwenji 	{Opt_target_lun, "target_lun=%u"},
1645c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
1646c66ac9dbSNicholas Bellinger };
1647c66ac9dbSNicholas Bellinger 
16482eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
16492eafd729SChristoph Hellwig 		const char *page, size_t count)
1650c66ac9dbSNicholas Bellinger {
16512eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
16526d180253SJesper Juhl 	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
16536d180253SJesper Juhl 	unsigned char *t_fabric = NULL, *t_port = NULL;
16548d213559SJoern Engel 	char *orig, *ptr, *opts;
1655c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
1656c66ac9dbSNicholas Bellinger 	unsigned long long tmp_ll;
1657c66ac9dbSNicholas Bellinger 	u64 sa_res_key = 0;
1658f2d30680SHannes Reinecke 	u64 mapped_lun = 0, target_lun = 0;
1659c66ac9dbSNicholas Bellinger 	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
166045fb94c2SBart Van Assche 	u16 tpgt = 0;
166145fb94c2SBart Van Assche 	u8 type = 0;
1662c66ac9dbSNicholas Bellinger 
1663b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
1664b49d6f78SDavid Disseldorp 	    (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
16659105bfc0SAndy Grover 		return count;
1666d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
16679105bfc0SAndy Grover 		return count;
1668c66ac9dbSNicholas Bellinger 
16690fd97ccfSChristoph Hellwig 	if (dev->export_count) {
16706708bb27SAndy Grover 		pr_debug("Unable to process APTPL metadata while"
1671c66ac9dbSNicholas Bellinger 			" active fabric exports exist\n");
1672c66ac9dbSNicholas Bellinger 		return -EINVAL;
1673c66ac9dbSNicholas Bellinger 	}
1674c66ac9dbSNicholas Bellinger 
1675c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
1676c66ac9dbSNicholas Bellinger 	if (!opts)
1677c66ac9dbSNicholas Bellinger 		return -ENOMEM;
1678c66ac9dbSNicholas Bellinger 
1679c66ac9dbSNicholas Bellinger 	orig = opts;
168090c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
1681c66ac9dbSNicholas Bellinger 		if (!*ptr)
1682c66ac9dbSNicholas Bellinger 			continue;
1683c66ac9dbSNicholas Bellinger 
1684c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
1685c66ac9dbSNicholas Bellinger 		switch (token) {
1686c66ac9dbSNicholas Bellinger 		case Opt_initiator_fabric:
16878d213559SJoern Engel 			i_fabric = match_strdup(args);
16886d180253SJesper Juhl 			if (!i_fabric) {
16896d180253SJesper Juhl 				ret = -ENOMEM;
16906d180253SJesper Juhl 				goto out;
16916d180253SJesper Juhl 			}
1692c66ac9dbSNicholas Bellinger 			break;
1693c66ac9dbSNicholas Bellinger 		case Opt_initiator_node:
16948d213559SJoern Engel 			i_port = match_strdup(args);
16956d180253SJesper Juhl 			if (!i_port) {
16966d180253SJesper Juhl 				ret = -ENOMEM;
16976d180253SJesper Juhl 				goto out;
16986d180253SJesper Juhl 			}
169960d645a4SDan Carpenter 			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
17006708bb27SAndy Grover 				pr_err("APTPL metadata initiator_node="
1701c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1702c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_IPORT_LEN);
1703c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1704c66ac9dbSNicholas Bellinger 				break;
1705c66ac9dbSNicholas Bellinger 			}
1706c66ac9dbSNicholas Bellinger 			break;
1707c66ac9dbSNicholas Bellinger 		case Opt_initiator_sid:
17088d213559SJoern Engel 			isid = match_strdup(args);
17096d180253SJesper Juhl 			if (!isid) {
17106d180253SJesper Juhl 				ret = -ENOMEM;
17116d180253SJesper Juhl 				goto out;
17126d180253SJesper Juhl 			}
171360d645a4SDan Carpenter 			if (strlen(isid) >= PR_REG_ISID_LEN) {
17146708bb27SAndy Grover 				pr_err("APTPL metadata initiator_isid"
1715c66ac9dbSNicholas Bellinger 					"= exceeds PR_REG_ISID_LEN: %d\n",
1716c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
1717c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1718c66ac9dbSNicholas Bellinger 				break;
1719c66ac9dbSNicholas Bellinger 			}
1720c66ac9dbSNicholas Bellinger 			break;
1721c66ac9dbSNicholas Bellinger 		case Opt_sa_res_key:
1722a2db857bStangwenji 			ret = match_u64(args,  &tmp_ll);
1723c66ac9dbSNicholas Bellinger 			if (ret < 0) {
17248d213559SJoern Engel 				pr_err("kstrtoull() failed for sa_res_key=\n");
1725c66ac9dbSNicholas Bellinger 				goto out;
1726c66ac9dbSNicholas Bellinger 			}
1727c66ac9dbSNicholas Bellinger 			sa_res_key = (u64)tmp_ll;
1728c66ac9dbSNicholas Bellinger 			break;
1729c66ac9dbSNicholas Bellinger 		/*
1730c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Reservation
1731c66ac9dbSNicholas Bellinger 		 */
1732c66ac9dbSNicholas Bellinger 		case Opt_res_holder:
1733c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1734c2091026SDavid Disseldorp 			if (ret)
1735c2091026SDavid Disseldorp 				goto out;
1736c66ac9dbSNicholas Bellinger 			res_holder = arg;
1737c66ac9dbSNicholas Bellinger 			break;
1738c66ac9dbSNicholas Bellinger 		case Opt_res_type:
1739c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1740c2091026SDavid Disseldorp 			if (ret)
1741c2091026SDavid Disseldorp 				goto out;
1742c66ac9dbSNicholas Bellinger 			type = (u8)arg;
1743c66ac9dbSNicholas Bellinger 			break;
1744c66ac9dbSNicholas Bellinger 		case Opt_res_scope:
1745c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1746c2091026SDavid Disseldorp 			if (ret)
1747c2091026SDavid Disseldorp 				goto out;
1748c66ac9dbSNicholas Bellinger 			break;
1749c66ac9dbSNicholas Bellinger 		case Opt_res_all_tg_pt:
1750c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1751c2091026SDavid Disseldorp 			if (ret)
1752c2091026SDavid Disseldorp 				goto out;
1753c66ac9dbSNicholas Bellinger 			all_tg_pt = (int)arg;
1754c66ac9dbSNicholas Bellinger 			break;
1755c66ac9dbSNicholas Bellinger 		case Opt_mapped_lun:
1756a2db857bStangwenji 			ret = match_u64(args, &tmp_ll);
1757c2091026SDavid Disseldorp 			if (ret)
1758c2091026SDavid Disseldorp 				goto out;
1759a2db857bStangwenji 			mapped_lun = (u64)tmp_ll;
1760c66ac9dbSNicholas Bellinger 			break;
1761c66ac9dbSNicholas Bellinger 		/*
1762c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Target Port
1763c66ac9dbSNicholas Bellinger 		 */
1764c66ac9dbSNicholas Bellinger 		case Opt_target_fabric:
17658d213559SJoern Engel 			t_fabric = match_strdup(args);
17666d180253SJesper Juhl 			if (!t_fabric) {
17676d180253SJesper Juhl 				ret = -ENOMEM;
17686d180253SJesper Juhl 				goto out;
17696d180253SJesper Juhl 			}
1770c66ac9dbSNicholas Bellinger 			break;
1771c66ac9dbSNicholas Bellinger 		case Opt_target_node:
17728d213559SJoern Engel 			t_port = match_strdup(args);
17736d180253SJesper Juhl 			if (!t_port) {
17746d180253SJesper Juhl 				ret = -ENOMEM;
17756d180253SJesper Juhl 				goto out;
17766d180253SJesper Juhl 			}
177760d645a4SDan Carpenter 			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
17786708bb27SAndy Grover 				pr_err("APTPL metadata target_node="
1779c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1780c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_TPORT_LEN);
1781c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1782c66ac9dbSNicholas Bellinger 				break;
1783c66ac9dbSNicholas Bellinger 			}
1784c66ac9dbSNicholas Bellinger 			break;
1785c66ac9dbSNicholas Bellinger 		case Opt_tpgt:
1786c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1787c2091026SDavid Disseldorp 			if (ret)
1788c2091026SDavid Disseldorp 				goto out;
1789c66ac9dbSNicholas Bellinger 			tpgt = (u16)arg;
1790c66ac9dbSNicholas Bellinger 			break;
1791c66ac9dbSNicholas Bellinger 		case Opt_port_rtpi:
1792c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1793c2091026SDavid Disseldorp 			if (ret)
1794c2091026SDavid Disseldorp 				goto out;
1795c66ac9dbSNicholas Bellinger 			break;
1796c66ac9dbSNicholas Bellinger 		case Opt_target_lun:
1797a2db857bStangwenji 			ret = match_u64(args, &tmp_ll);
1798c2091026SDavid Disseldorp 			if (ret)
1799c2091026SDavid Disseldorp 				goto out;
1800a2db857bStangwenji 			target_lun = (u64)tmp_ll;
1801c66ac9dbSNicholas Bellinger 			break;
1802c66ac9dbSNicholas Bellinger 		default:
1803c66ac9dbSNicholas Bellinger 			break;
1804c66ac9dbSNicholas Bellinger 		}
1805c66ac9dbSNicholas Bellinger 	}
1806c66ac9dbSNicholas Bellinger 
18076708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
18086708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
1809c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1810c66ac9dbSNicholas Bellinger 		goto out;
1811c66ac9dbSNicholas Bellinger 	}
1812c66ac9dbSNicholas Bellinger 
1813c66ac9dbSNicholas Bellinger 	if (res_holder && !(type)) {
18146708bb27SAndy Grover 		pr_err("Illegal PR type: 0x%02x for reservation"
1815c66ac9dbSNicholas Bellinger 				" holder\n", type);
1816c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1817c66ac9dbSNicholas Bellinger 		goto out;
1818c66ac9dbSNicholas Bellinger 	}
1819c66ac9dbSNicholas Bellinger 
18200fd97ccfSChristoph Hellwig 	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1821c66ac9dbSNicholas Bellinger 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1822c66ac9dbSNicholas Bellinger 			res_holder, all_tg_pt, type);
1823c66ac9dbSNicholas Bellinger out:
18246d180253SJesper Juhl 	kfree(i_fabric);
18256d180253SJesper Juhl 	kfree(i_port);
18266d180253SJesper Juhl 	kfree(isid);
18276d180253SJesper Juhl 	kfree(t_fabric);
18286d180253SJesper Juhl 	kfree(t_port);
1829c66ac9dbSNicholas Bellinger 	kfree(orig);
1830c66ac9dbSNicholas Bellinger 	return (ret == 0) ? count : ret;
1831c66ac9dbSNicholas Bellinger }
1832c66ac9dbSNicholas Bellinger 
1833c66ac9dbSNicholas Bellinger 
18342eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_holder);
18352eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
18362eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
18372eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
18382eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
18392eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
18402eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_type);
18412eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
18422eafd729SChristoph Hellwig CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
1843c66ac9dbSNicholas Bellinger 
1844c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_pr_attrs[] = {
18452eafd729SChristoph Hellwig 	&target_pr_attr_res_holder,
18462eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_all_tgt_pts,
18472eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_generation,
18482eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_holder_tg_port,
18492eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_registered_i_pts,
18502eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_type,
18512eafd729SChristoph Hellwig 	&target_pr_attr_res_type,
18522eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_active,
18532eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_metadata,
1854c66ac9dbSNicholas Bellinger 	NULL,
1855c66ac9dbSNicholas Bellinger };
1856c66ac9dbSNicholas Bellinger 
18572eafd729SChristoph Hellwig TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
1858c66ac9dbSNicholas Bellinger 
185991e2e39bSNicholas Bellinger /*  End functions for struct config_item_type tb_dev_pr_cit */
1860c66ac9dbSNicholas Bellinger 
186173112edcSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_cit */
1862c66ac9dbSNicholas Bellinger 
18632eafd729SChristoph Hellwig static inline struct se_device *to_device(struct config_item *item)
1864c66ac9dbSNicholas Bellinger {
18652eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device, dev_group);
18662eafd729SChristoph Hellwig }
18672eafd729SChristoph Hellwig 
18682eafd729SChristoph Hellwig static ssize_t target_dev_info_show(struct config_item *item, char *page)
18692eafd729SChristoph Hellwig {
18702eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1871c66ac9dbSNicholas Bellinger 	int bl = 0;
1872c66ac9dbSNicholas Bellinger 	ssize_t read_bytes = 0;
1873c66ac9dbSNicholas Bellinger 
18740fd97ccfSChristoph Hellwig 	transport_dump_dev_state(dev, page, &bl);
1875c66ac9dbSNicholas Bellinger 	read_bytes += bl;
18760a06d430SChristoph Hellwig 	read_bytes += dev->transport->show_configfs_dev_params(dev,
18770a06d430SChristoph Hellwig 			page+read_bytes);
1878c66ac9dbSNicholas Bellinger 	return read_bytes;
1879c66ac9dbSNicholas Bellinger }
1880c66ac9dbSNicholas Bellinger 
18812eafd729SChristoph Hellwig static ssize_t target_dev_control_store(struct config_item *item,
18822eafd729SChristoph Hellwig 		const char *page, size_t count)
1883c66ac9dbSNicholas Bellinger {
18842eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1885c66ac9dbSNicholas Bellinger 
18860a06d430SChristoph Hellwig 	return dev->transport->set_configfs_dev_params(dev, page, count);
1887c66ac9dbSNicholas Bellinger }
1888c66ac9dbSNicholas Bellinger 
18892eafd729SChristoph Hellwig static ssize_t target_dev_alias_show(struct config_item *item, char *page)
1890c66ac9dbSNicholas Bellinger {
18912eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1892c66ac9dbSNicholas Bellinger 
18930fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_ALIAS))
1894c66ac9dbSNicholas Bellinger 		return 0;
1895c66ac9dbSNicholas Bellinger 
18960fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1897c66ac9dbSNicholas Bellinger }
1898c66ac9dbSNicholas Bellinger 
18992eafd729SChristoph Hellwig static ssize_t target_dev_alias_store(struct config_item *item,
19002eafd729SChristoph Hellwig 		const char *page, size_t count)
1901c66ac9dbSNicholas Bellinger {
19022eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
19030fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1904c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1905c66ac9dbSNicholas Bellinger 
1906c66ac9dbSNicholas Bellinger 	if (count > (SE_DEV_ALIAS_LEN-1)) {
19076708bb27SAndy Grover 		pr_err("alias count: %d exceeds"
1908c66ac9dbSNicholas Bellinger 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1909c66ac9dbSNicholas Bellinger 			SE_DEV_ALIAS_LEN-1);
1910c66ac9dbSNicholas Bellinger 		return -EINVAL;
1911c66ac9dbSNicholas Bellinger 	}
1912c66ac9dbSNicholas Bellinger 
19130fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
19143011684cSDan Carpenter 	if (!read_bytes)
19153011684cSDan Carpenter 		return -EINVAL;
19160fd97ccfSChristoph Hellwig 	if (dev->dev_alias[read_bytes - 1] == '\n')
19170fd97ccfSChristoph Hellwig 		dev->dev_alias[read_bytes - 1] = '\0';
19180877eafdSSebastian Andrzej Siewior 
19190fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_ALIAS;
19203011684cSDan Carpenter 
19216708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1922c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
19230fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
19240fd97ccfSChristoph Hellwig 		dev->dev_alias);
1925c66ac9dbSNicholas Bellinger 
1926c66ac9dbSNicholas Bellinger 	return read_bytes;
1927c66ac9dbSNicholas Bellinger }
1928c66ac9dbSNicholas Bellinger 
19292eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
1930c66ac9dbSNicholas Bellinger {
19312eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1932c66ac9dbSNicholas Bellinger 
19330fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1934c66ac9dbSNicholas Bellinger 		return 0;
1935c66ac9dbSNicholas Bellinger 
19360fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1937c66ac9dbSNicholas Bellinger }
1938c66ac9dbSNicholas Bellinger 
19392eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_store(struct config_item *item,
19402eafd729SChristoph Hellwig 		const char *page, size_t count)
1941c66ac9dbSNicholas Bellinger {
19422eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
19430fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1944c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1945c66ac9dbSNicholas Bellinger 
1946c66ac9dbSNicholas Bellinger 	if (count > (SE_UDEV_PATH_LEN-1)) {
19476708bb27SAndy Grover 		pr_err("udev_path count: %d exceeds"
1948c66ac9dbSNicholas Bellinger 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1949c66ac9dbSNicholas Bellinger 			SE_UDEV_PATH_LEN-1);
1950c66ac9dbSNicholas Bellinger 		return -EINVAL;
1951c66ac9dbSNicholas Bellinger 	}
1952c66ac9dbSNicholas Bellinger 
19530fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1954c66ac9dbSNicholas Bellinger 			"%s", page);
19553011684cSDan Carpenter 	if (!read_bytes)
19563011684cSDan Carpenter 		return -EINVAL;
19570fd97ccfSChristoph Hellwig 	if (dev->udev_path[read_bytes - 1] == '\n')
19580fd97ccfSChristoph Hellwig 		dev->udev_path[read_bytes - 1] = '\0';
19590877eafdSSebastian Andrzej Siewior 
19600fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_UDEV_PATH;
19613011684cSDan Carpenter 
19626708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1963c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
19640fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
19650fd97ccfSChristoph Hellwig 		dev->udev_path);
1966c66ac9dbSNicholas Bellinger 
1967c66ac9dbSNicholas Bellinger 	return read_bytes;
1968c66ac9dbSNicholas Bellinger }
1969c66ac9dbSNicholas Bellinger 
19702eafd729SChristoph Hellwig static ssize_t target_dev_enable_show(struct config_item *item, char *page)
197164146db7SAndy Grover {
19722eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
197364146db7SAndy Grover 
1974cb0f32e1SMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n", target_dev_configured(dev));
197564146db7SAndy Grover }
197664146db7SAndy Grover 
19772eafd729SChristoph Hellwig static ssize_t target_dev_enable_store(struct config_item *item,
19782eafd729SChristoph Hellwig 		const char *page, size_t count)
1979c66ac9dbSNicholas Bellinger {
19802eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1981c66ac9dbSNicholas Bellinger 	char *ptr;
19820fd97ccfSChristoph Hellwig 	int ret;
1983c66ac9dbSNicholas Bellinger 
1984c66ac9dbSNicholas Bellinger 	ptr = strstr(page, "1");
19856708bb27SAndy Grover 	if (!ptr) {
19866708bb27SAndy Grover 		pr_err("For dev_enable ops, only valid value"
1987c66ac9dbSNicholas Bellinger 				" is \"1\"\n");
1988c66ac9dbSNicholas Bellinger 		return -EINVAL;
1989c66ac9dbSNicholas Bellinger 	}
1990c66ac9dbSNicholas Bellinger 
19910fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
19920fd97ccfSChristoph Hellwig 	if (ret)
19930fd97ccfSChristoph Hellwig 		return ret;
1994c66ac9dbSNicholas Bellinger 	return count;
1995c66ac9dbSNicholas Bellinger }
1996c66ac9dbSNicholas Bellinger 
19972eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
1998c66ac9dbSNicholas Bellinger {
19992eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2000c66ac9dbSNicholas Bellinger 	struct config_item *lu_ci;
2001c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2002c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2003c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
2004c66ac9dbSNicholas Bellinger 
2005c66ac9dbSNicholas Bellinger 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
2006c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
2007c87fbd56SChristoph Hellwig 		return 0;
2008c66ac9dbSNicholas Bellinger 
2009c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2010c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
20116708bb27SAndy Grover 	if (lu_gp) {
2012c66ac9dbSNicholas Bellinger 		lu_ci = &lu_gp->lu_gp_group.cg_item;
2013c66ac9dbSNicholas Bellinger 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
2014c66ac9dbSNicholas Bellinger 			config_item_name(lu_ci), lu_gp->lu_gp_id);
2015c66ac9dbSNicholas Bellinger 	}
2016c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2017c66ac9dbSNicholas Bellinger 
2018c66ac9dbSNicholas Bellinger 	return len;
2019c66ac9dbSNicholas Bellinger }
2020c66ac9dbSNicholas Bellinger 
20212eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
20222eafd729SChristoph Hellwig 		const char *page, size_t count)
2023c66ac9dbSNicholas Bellinger {
20242eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
20250fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
2026c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
2027c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2028c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
2029c66ac9dbSNicholas Bellinger 	int move = 0;
2030c66ac9dbSNicholas Bellinger 
2031c87fbd56SChristoph Hellwig 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
2032c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
20339105bfc0SAndy Grover 		return count;
2034c87fbd56SChristoph Hellwig 
2035c66ac9dbSNicholas Bellinger 	if (count > LU_GROUP_NAME_BUF) {
20366708bb27SAndy Grover 		pr_err("ALUA LU Group Alias too large!\n");
2037c66ac9dbSNicholas Bellinger 		return -EINVAL;
2038c66ac9dbSNicholas Bellinger 	}
2039c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
2040c66ac9dbSNicholas Bellinger 	memcpy(buf, page, count);
2041c66ac9dbSNicholas Bellinger 	/*
2042c66ac9dbSNicholas Bellinger 	 * Any ALUA logical unit alias besides "NULL" means we will be
2043c66ac9dbSNicholas Bellinger 	 * making a new group association.
2044c66ac9dbSNicholas Bellinger 	 */
2045c66ac9dbSNicholas Bellinger 	if (strcmp(strstrip(buf), "NULL")) {
2046c66ac9dbSNicholas Bellinger 		/*
2047c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name() will increment reference to
2048c66ac9dbSNicholas Bellinger 		 * struct t10_alua_lu_gp.  This reference is released with
2049c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name below().
2050c66ac9dbSNicholas Bellinger 		 */
2051c66ac9dbSNicholas Bellinger 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
20526708bb27SAndy Grover 		if (!lu_gp_new)
2053c66ac9dbSNicholas Bellinger 			return -ENODEV;
2054c66ac9dbSNicholas Bellinger 	}
2055c66ac9dbSNicholas Bellinger 
2056c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2057c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
20586708bb27SAndy Grover 	if (lu_gp) {
2059c66ac9dbSNicholas Bellinger 		/*
2060c66ac9dbSNicholas Bellinger 		 * Clearing an existing lu_gp association, and replacing
2061c66ac9dbSNicholas Bellinger 		 * with NULL
2062c66ac9dbSNicholas Bellinger 		 */
20636708bb27SAndy Grover 		if (!lu_gp_new) {
20646708bb27SAndy Grover 			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
2065c66ac9dbSNicholas Bellinger 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2066c66ac9dbSNicholas Bellinger 				" %hu\n",
2067c66ac9dbSNicholas Bellinger 				config_item_name(&hba->hba_group.cg_item),
20680fd97ccfSChristoph Hellwig 				config_item_name(&dev->dev_group.cg_item),
2069c66ac9dbSNicholas Bellinger 				config_item_name(&lu_gp->lu_gp_group.cg_item),
2070c66ac9dbSNicholas Bellinger 				lu_gp->lu_gp_id);
2071c66ac9dbSNicholas Bellinger 
2072c66ac9dbSNicholas Bellinger 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2073c66ac9dbSNicholas Bellinger 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2074c66ac9dbSNicholas Bellinger 
2075c66ac9dbSNicholas Bellinger 			return count;
2076c66ac9dbSNicholas Bellinger 		}
2077c66ac9dbSNicholas Bellinger 		/*
2078c66ac9dbSNicholas Bellinger 		 * Removing existing association of lu_gp_mem with lu_gp
2079c66ac9dbSNicholas Bellinger 		 */
2080c66ac9dbSNicholas Bellinger 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2081c66ac9dbSNicholas Bellinger 		move = 1;
2082c66ac9dbSNicholas Bellinger 	}
2083c66ac9dbSNicholas Bellinger 	/*
2084c66ac9dbSNicholas Bellinger 	 * Associate lu_gp_mem with lu_gp_new.
2085c66ac9dbSNicholas Bellinger 	 */
2086c66ac9dbSNicholas Bellinger 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2087c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2088c66ac9dbSNicholas Bellinger 
20896708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
2090c66ac9dbSNicholas Bellinger 		" core/alua/lu_gps/%s, ID: %hu\n",
2091c66ac9dbSNicholas Bellinger 		(move) ? "Moving" : "Adding",
2092c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
20930fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
2094c66ac9dbSNicholas Bellinger 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2095c66ac9dbSNicholas Bellinger 		lu_gp_new->lu_gp_id);
2096c66ac9dbSNicholas Bellinger 
2097c66ac9dbSNicholas Bellinger 	core_alua_put_lu_gp_from_name(lu_gp_new);
2098c66ac9dbSNicholas Bellinger 	return count;
2099c66ac9dbSNicholas Bellinger }
2100c66ac9dbSNicholas Bellinger 
21012eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
2102229d4f11SHannes Reinecke {
21032eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2104229d4f11SHannes Reinecke 	struct t10_alua_lba_map *map;
2105229d4f11SHannes Reinecke 	struct t10_alua_lba_map_member *mem;
2106229d4f11SHannes Reinecke 	char *b = page;
2107229d4f11SHannes Reinecke 	int bl = 0;
2108229d4f11SHannes Reinecke 	char state;
2109229d4f11SHannes Reinecke 
2110229d4f11SHannes Reinecke 	spin_lock(&dev->t10_alua.lba_map_lock);
2111229d4f11SHannes Reinecke 	if (!list_empty(&dev->t10_alua.lba_map_list))
2112229d4f11SHannes Reinecke 	    bl += sprintf(b + bl, "%u %u\n",
2113229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_size,
2114229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_multiplier);
2115229d4f11SHannes Reinecke 	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2116229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "%llu %llu",
2117229d4f11SHannes Reinecke 			      map->lba_map_first_lba, map->lba_map_last_lba);
2118229d4f11SHannes Reinecke 		list_for_each_entry(mem, &map->lba_map_mem_list,
2119229d4f11SHannes Reinecke 				    lba_map_mem_list) {
2120229d4f11SHannes Reinecke 			switch (mem->lba_map_mem_alua_state) {
2121229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2122229d4f11SHannes Reinecke 				state = 'O';
2123229d4f11SHannes Reinecke 				break;
2124229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2125229d4f11SHannes Reinecke 				state = 'A';
2126229d4f11SHannes Reinecke 				break;
2127229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_STANDBY:
2128229d4f11SHannes Reinecke 				state = 'S';
2129229d4f11SHannes Reinecke 				break;
2130229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_UNAVAILABLE:
2131229d4f11SHannes Reinecke 				state = 'U';
2132229d4f11SHannes Reinecke 				break;
2133229d4f11SHannes Reinecke 			default:
2134229d4f11SHannes Reinecke 				state = '.';
2135229d4f11SHannes Reinecke 				break;
2136229d4f11SHannes Reinecke 			}
2137229d4f11SHannes Reinecke 			bl += sprintf(b + bl, " %d:%c",
2138229d4f11SHannes Reinecke 				      mem->lba_map_mem_alua_pg_id, state);
2139229d4f11SHannes Reinecke 		}
2140229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "\n");
2141229d4f11SHannes Reinecke 	}
2142229d4f11SHannes Reinecke 	spin_unlock(&dev->t10_alua.lba_map_lock);
2143229d4f11SHannes Reinecke 	return bl;
2144229d4f11SHannes Reinecke }
2145229d4f11SHannes Reinecke 
21462eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_store(struct config_item *item,
21472eafd729SChristoph Hellwig 		const char *page, size_t count)
2148229d4f11SHannes Reinecke {
21492eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2150229d4f11SHannes Reinecke 	struct t10_alua_lba_map *lba_map = NULL;
2151229d4f11SHannes Reinecke 	struct list_head lba_list;
2152f0a8afecSBart Van Assche 	char *map_entries, *orig, *ptr;
2153229d4f11SHannes Reinecke 	char state;
2154229d4f11SHannes Reinecke 	int pg_num = -1, pg;
2155229d4f11SHannes Reinecke 	int ret = 0, num = 0, pg_id, alua_state;
2156229d4f11SHannes Reinecke 	unsigned long start_lba = -1, end_lba = -1;
2157229d4f11SHannes Reinecke 	unsigned long segment_size = -1, segment_mult = -1;
2158229d4f11SHannes Reinecke 
2159f0a8afecSBart Van Assche 	orig = map_entries = kstrdup(page, GFP_KERNEL);
2160229d4f11SHannes Reinecke 	if (!map_entries)
2161229d4f11SHannes Reinecke 		return -ENOMEM;
2162229d4f11SHannes Reinecke 
2163229d4f11SHannes Reinecke 	INIT_LIST_HEAD(&lba_list);
2164229d4f11SHannes Reinecke 	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2165229d4f11SHannes Reinecke 		if (!*ptr)
2166229d4f11SHannes Reinecke 			continue;
2167229d4f11SHannes Reinecke 
2168229d4f11SHannes Reinecke 		if (num == 0) {
2169229d4f11SHannes Reinecke 			if (sscanf(ptr, "%lu %lu\n",
2170229d4f11SHannes Reinecke 				   &segment_size, &segment_mult) != 2) {
2171229d4f11SHannes Reinecke 				pr_err("Invalid line %d\n", num);
2172229d4f11SHannes Reinecke 				ret = -EINVAL;
2173229d4f11SHannes Reinecke 				break;
2174229d4f11SHannes Reinecke 			}
2175229d4f11SHannes Reinecke 			num++;
2176229d4f11SHannes Reinecke 			continue;
2177229d4f11SHannes Reinecke 		}
2178229d4f11SHannes Reinecke 		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2179229d4f11SHannes Reinecke 			pr_err("Invalid line %d\n", num);
2180229d4f11SHannes Reinecke 			ret = -EINVAL;
2181229d4f11SHannes Reinecke 			break;
2182229d4f11SHannes Reinecke 		}
2183229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2184229d4f11SHannes Reinecke 		if (!ptr) {
2185229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing end lba\n", num);
2186229d4f11SHannes Reinecke 			ret = -EINVAL;
2187229d4f11SHannes Reinecke 			break;
2188229d4f11SHannes Reinecke 		}
2189229d4f11SHannes Reinecke 		ptr++;
2190229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2191229d4f11SHannes Reinecke 		if (!ptr) {
2192229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing state definitions\n",
2193229d4f11SHannes Reinecke 			       num);
2194229d4f11SHannes Reinecke 			ret = -EINVAL;
2195229d4f11SHannes Reinecke 			break;
2196229d4f11SHannes Reinecke 		}
2197229d4f11SHannes Reinecke 		ptr++;
2198229d4f11SHannes Reinecke 		lba_map = core_alua_allocate_lba_map(&lba_list,
2199229d4f11SHannes Reinecke 						     start_lba, end_lba);
2200229d4f11SHannes Reinecke 		if (IS_ERR(lba_map)) {
2201229d4f11SHannes Reinecke 			ret = PTR_ERR(lba_map);
2202229d4f11SHannes Reinecke 			break;
2203229d4f11SHannes Reinecke 		}
2204229d4f11SHannes Reinecke 		pg = 0;
2205229d4f11SHannes Reinecke 		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2206229d4f11SHannes Reinecke 			switch (state) {
2207229d4f11SHannes Reinecke 			case 'O':
2208229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2209229d4f11SHannes Reinecke 				break;
2210229d4f11SHannes Reinecke 			case 'A':
2211229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2212229d4f11SHannes Reinecke 				break;
2213229d4f11SHannes Reinecke 			case 'S':
2214229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_STANDBY;
2215229d4f11SHannes Reinecke 				break;
2216229d4f11SHannes Reinecke 			case 'U':
2217229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2218229d4f11SHannes Reinecke 				break;
2219229d4f11SHannes Reinecke 			default:
2220229d4f11SHannes Reinecke 				pr_err("Invalid ALUA state '%c'\n", state);
2221229d4f11SHannes Reinecke 				ret = -EINVAL;
2222229d4f11SHannes Reinecke 				goto out;
2223229d4f11SHannes Reinecke 			}
2224229d4f11SHannes Reinecke 
2225229d4f11SHannes Reinecke 			ret = core_alua_allocate_lba_map_mem(lba_map,
2226229d4f11SHannes Reinecke 							     pg_id, alua_state);
2227229d4f11SHannes Reinecke 			if (ret) {
2228229d4f11SHannes Reinecke 				pr_err("Invalid target descriptor %d:%c "
2229229d4f11SHannes Reinecke 				       "at line %d\n",
2230229d4f11SHannes Reinecke 				       pg_id, state, num);
2231229d4f11SHannes Reinecke 				break;
2232229d4f11SHannes Reinecke 			}
2233229d4f11SHannes Reinecke 			pg++;
2234229d4f11SHannes Reinecke 			ptr = strchr(ptr, ' ');
2235229d4f11SHannes Reinecke 			if (ptr)
2236229d4f11SHannes Reinecke 				ptr++;
2237229d4f11SHannes Reinecke 			else
2238229d4f11SHannes Reinecke 				break;
2239229d4f11SHannes Reinecke 		}
2240229d4f11SHannes Reinecke 		if (pg_num == -1)
2241229d4f11SHannes Reinecke 		    pg_num = pg;
2242229d4f11SHannes Reinecke 		else if (pg != pg_num) {
2243229d4f11SHannes Reinecke 			pr_err("Only %d from %d port groups definitions "
2244229d4f11SHannes Reinecke 			       "at line %d\n", pg, pg_num, num);
2245229d4f11SHannes Reinecke 			ret = -EINVAL;
2246229d4f11SHannes Reinecke 			break;
2247229d4f11SHannes Reinecke 		}
2248229d4f11SHannes Reinecke 		num++;
2249229d4f11SHannes Reinecke 	}
2250229d4f11SHannes Reinecke out:
2251229d4f11SHannes Reinecke 	if (ret) {
2252229d4f11SHannes Reinecke 		core_alua_free_lba_map(&lba_list);
2253229d4f11SHannes Reinecke 		count = ret;
2254229d4f11SHannes Reinecke 	} else
2255229d4f11SHannes Reinecke 		core_alua_set_lba_map(dev, &lba_list,
2256229d4f11SHannes Reinecke 				      segment_size, segment_mult);
2257f0a8afecSBart Van Assche 	kfree(orig);
2258229d4f11SHannes Reinecke 	return count;
2259229d4f11SHannes Reinecke }
2260229d4f11SHannes Reinecke 
22612eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_dev_, info);
22622eafd729SChristoph Hellwig CONFIGFS_ATTR_WO(target_dev_, control);
22632eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alias);
22642eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, udev_path);
22652eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, enable);
22662eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alua_lu_gp);
22672eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, lba_map);
2268229d4f11SHannes Reinecke 
226973112edcSNicholas Bellinger static struct configfs_attribute *target_core_dev_attrs[] = {
22702eafd729SChristoph Hellwig 	&target_dev_attr_info,
22712eafd729SChristoph Hellwig 	&target_dev_attr_control,
22722eafd729SChristoph Hellwig 	&target_dev_attr_alias,
22732eafd729SChristoph Hellwig 	&target_dev_attr_udev_path,
22742eafd729SChristoph Hellwig 	&target_dev_attr_enable,
22752eafd729SChristoph Hellwig 	&target_dev_attr_alua_lu_gp,
22762eafd729SChristoph Hellwig 	&target_dev_attr_lba_map,
2277c66ac9dbSNicholas Bellinger 	NULL,
2278c66ac9dbSNicholas Bellinger };
2279c66ac9dbSNicholas Bellinger 
2280c66ac9dbSNicholas Bellinger static void target_core_dev_release(struct config_item *item)
2281c66ac9dbSNicholas Bellinger {
22820fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
22830fd97ccfSChristoph Hellwig 	struct se_device *dev =
22840fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2285c66ac9dbSNicholas Bellinger 
22860fd97ccfSChristoph Hellwig 	target_free_device(dev);
2287c66ac9dbSNicholas Bellinger }
2288c66ac9dbSNicholas Bellinger 
2289c17cd249SNicholas Bellinger /*
2290c17cd249SNicholas Bellinger  * Used in target_core_fabric_configfs.c to verify valid se_device symlink
2291c17cd249SNicholas Bellinger  * within target_fabric_port_link()
2292c17cd249SNicholas Bellinger  */
2293c17cd249SNicholas Bellinger struct configfs_item_operations target_core_dev_item_ops = {
2294c66ac9dbSNicholas Bellinger 	.release		= target_core_dev_release,
2295c66ac9dbSNicholas Bellinger };
2296c66ac9dbSNicholas Bellinger 
229773112edcSNicholas Bellinger TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2298c66ac9dbSNicholas Bellinger 
229973112edcSNicholas Bellinger /* End functions for struct config_item_type tb_dev_cit */
2300c66ac9dbSNicholas Bellinger 
2301c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2302c66ac9dbSNicholas Bellinger 
23032eafd729SChristoph Hellwig static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2304c66ac9dbSNicholas Bellinger {
23052eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_lu_gp,
23062eafd729SChristoph Hellwig 			lu_gp_group);
23072eafd729SChristoph Hellwig }
23082eafd729SChristoph Hellwig 
23092eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
23102eafd729SChristoph Hellwig {
23112eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
23122eafd729SChristoph Hellwig 
23136708bb27SAndy Grover 	if (!lu_gp->lu_gp_valid_id)
2314c66ac9dbSNicholas Bellinger 		return 0;
2315c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2316c66ac9dbSNicholas Bellinger }
2317c66ac9dbSNicholas Bellinger 
23182eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
23192eafd729SChristoph Hellwig 		const char *page, size_t count)
2320c66ac9dbSNicholas Bellinger {
23212eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2322c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2323c66ac9dbSNicholas Bellinger 	unsigned long lu_gp_id;
2324c66ac9dbSNicholas Bellinger 	int ret;
2325c66ac9dbSNicholas Bellinger 
232657103d7fSJingoo Han 	ret = kstrtoul(page, 0, &lu_gp_id);
2327c66ac9dbSNicholas Bellinger 	if (ret < 0) {
232857103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2329c66ac9dbSNicholas Bellinger 			" lu_gp_id\n", ret);
233057103d7fSJingoo Han 		return ret;
2331c66ac9dbSNicholas Bellinger 	}
2332c66ac9dbSNicholas Bellinger 	if (lu_gp_id > 0x0000ffff) {
23336708bb27SAndy Grover 		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2334c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", lu_gp_id);
2335c66ac9dbSNicholas Bellinger 		return -EINVAL;
2336c66ac9dbSNicholas Bellinger 	}
2337c66ac9dbSNicholas Bellinger 
2338c66ac9dbSNicholas Bellinger 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2339c66ac9dbSNicholas Bellinger 	if (ret < 0)
2340c66ac9dbSNicholas Bellinger 		return -EINVAL;
2341c66ac9dbSNicholas Bellinger 
23426708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2343c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
2344c66ac9dbSNicholas Bellinger 		config_item_name(&alua_lu_gp_cg->cg_item),
2345c66ac9dbSNicholas Bellinger 		lu_gp->lu_gp_id);
2346c66ac9dbSNicholas Bellinger 
2347c66ac9dbSNicholas Bellinger 	return count;
2348c66ac9dbSNicholas Bellinger }
2349c66ac9dbSNicholas Bellinger 
23502eafd729SChristoph Hellwig static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2351c66ac9dbSNicholas Bellinger {
23522eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2353c66ac9dbSNicholas Bellinger 	struct se_device *dev;
2354c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2355c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2356c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2357c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
2358c66ac9dbSNicholas Bellinger 
2359c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
2360c66ac9dbSNicholas Bellinger 
2361c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp->lu_gp_lock);
2362c66ac9dbSNicholas Bellinger 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2363c66ac9dbSNicholas Bellinger 		dev = lu_gp_mem->lu_gp_mem_dev;
23640fd97ccfSChristoph Hellwig 		hba = dev->se_hba;
2365c66ac9dbSNicholas Bellinger 
2366c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2367c66ac9dbSNicholas Bellinger 			config_item_name(&hba->hba_group.cg_item),
23680fd97ccfSChristoph Hellwig 			config_item_name(&dev->dev_group.cg_item));
2369c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2370c66ac9dbSNicholas Bellinger 
2371c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
23726708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2373c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2374c66ac9dbSNicholas Bellinger 			break;
2375c66ac9dbSNicholas Bellinger 		}
2376c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2377c66ac9dbSNicholas Bellinger 		len += cur_len;
2378c66ac9dbSNicholas Bellinger 	}
2379c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp->lu_gp_lock);
2380c66ac9dbSNicholas Bellinger 
2381c66ac9dbSNicholas Bellinger 	return len;
2382c66ac9dbSNicholas Bellinger }
2383c66ac9dbSNicholas Bellinger 
23842eafd729SChristoph Hellwig CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
23852eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_lu_gp_, members);
2386c66ac9dbSNicholas Bellinger 
2387c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
23882eafd729SChristoph Hellwig 	&target_lu_gp_attr_lu_gp_id,
23892eafd729SChristoph Hellwig 	&target_lu_gp_attr_members,
2390c66ac9dbSNicholas Bellinger 	NULL,
2391c66ac9dbSNicholas Bellinger };
2392c66ac9dbSNicholas Bellinger 
23931f6fe7cbSNicholas Bellinger static void target_core_alua_lu_gp_release(struct config_item *item)
23941f6fe7cbSNicholas Bellinger {
23951f6fe7cbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
23961f6fe7cbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
23971f6fe7cbSNicholas Bellinger 
23981f6fe7cbSNicholas Bellinger 	core_alua_free_lu_gp(lu_gp);
23991f6fe7cbSNicholas Bellinger }
24001f6fe7cbSNicholas Bellinger 
2401c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_lu_gp_ops = {
24021f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_lu_gp_release,
2403c66ac9dbSNicholas Bellinger };
2404c66ac9dbSNicholas Bellinger 
2405ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_lu_gp_cit = {
2406c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
2407c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_lu_gp_attrs,
2408c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2409c66ac9dbSNicholas Bellinger };
2410c66ac9dbSNicholas Bellinger 
2411c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2412c66ac9dbSNicholas Bellinger 
2413c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2414c66ac9dbSNicholas Bellinger 
2415c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_lu_gp(
2416c66ac9dbSNicholas Bellinger 	struct config_group *group,
2417c66ac9dbSNicholas Bellinger 	const char *name)
2418c66ac9dbSNicholas Bellinger {
2419c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2420c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = NULL;
2421c66ac9dbSNicholas Bellinger 	struct config_item *alua_lu_gp_ci = NULL;
2422c66ac9dbSNicholas Bellinger 
2423c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp(name, 0);
2424c66ac9dbSNicholas Bellinger 	if (IS_ERR(lu_gp))
2425c66ac9dbSNicholas Bellinger 		return NULL;
2426c66ac9dbSNicholas Bellinger 
2427c66ac9dbSNicholas Bellinger 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
2428c66ac9dbSNicholas Bellinger 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2429c66ac9dbSNicholas Bellinger 
2430c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_lu_gp_cg, name,
2431c66ac9dbSNicholas Bellinger 			&target_core_alua_lu_gp_cit);
2432c66ac9dbSNicholas Bellinger 
24336708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2434c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s\n",
2435c66ac9dbSNicholas Bellinger 		config_item_name(alua_lu_gp_ci));
2436c66ac9dbSNicholas Bellinger 
2437c66ac9dbSNicholas Bellinger 	return alua_lu_gp_cg;
2438c66ac9dbSNicholas Bellinger 
2439c66ac9dbSNicholas Bellinger }
2440c66ac9dbSNicholas Bellinger 
2441c66ac9dbSNicholas Bellinger static void target_core_alua_drop_lu_gp(
2442c66ac9dbSNicholas Bellinger 	struct config_group *group,
2443c66ac9dbSNicholas Bellinger 	struct config_item *item)
2444c66ac9dbSNicholas Bellinger {
2445c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2446c66ac9dbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
2447c66ac9dbSNicholas Bellinger 
24486708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2449c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
2450c66ac9dbSNicholas Bellinger 		config_item_name(item), lu_gp->lu_gp_id);
24511f6fe7cbSNicholas Bellinger 	/*
24521f6fe7cbSNicholas Bellinger 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
24531f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_lu_gp_release()
24541f6fe7cbSNicholas Bellinger 	 */
2455c66ac9dbSNicholas Bellinger 	config_item_put(item);
2456c66ac9dbSNicholas Bellinger }
2457c66ac9dbSNicholas Bellinger 
2458c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2459c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_lu_gp,
2460c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_lu_gp,
2461c66ac9dbSNicholas Bellinger };
2462c66ac9dbSNicholas Bellinger 
2463ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_lu_gps_cit = {
2464c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2465c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
2466c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2467c66ac9dbSNicholas Bellinger };
2468c66ac9dbSNicholas Bellinger 
2469c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2470c66ac9dbSNicholas Bellinger 
2471c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2472c66ac9dbSNicholas Bellinger 
24732eafd729SChristoph Hellwig static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
24742eafd729SChristoph Hellwig {
24752eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
24762eafd729SChristoph Hellwig 			tg_pt_gp_group);
24772eafd729SChristoph Hellwig }
2478c66ac9dbSNicholas Bellinger 
24792eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2480c66ac9dbSNicholas Bellinger 		char *page)
2481c66ac9dbSNicholas Bellinger {
2482c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n",
2483d19c4643SMike Christie 		       to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
2484c66ac9dbSNicholas Bellinger }
2485c66ac9dbSNicholas Bellinger 
24862eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
24872eafd729SChristoph Hellwig 		const char *page, size_t count)
2488c66ac9dbSNicholas Bellinger {
24892eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
24900fd97ccfSChristoph Hellwig 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2491c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2492c66ac9dbSNicholas Bellinger 	int new_state, ret;
2493c66ac9dbSNicholas Bellinger 
24946708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2495125d0119SHannes Reinecke 		pr_err("Unable to do implicit ALUA on non valid"
2496c66ac9dbSNicholas Bellinger 			" tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2497c66ac9dbSNicholas Bellinger 		return -EINVAL;
2498c66ac9dbSNicholas Bellinger 	}
2499cb0f32e1SMike Christie 	if (!target_dev_configured(dev)) {
2500f1453773SNicholas Bellinger 		pr_err("Unable to set alua_access_state while device is"
2501f1453773SNicholas Bellinger 		       " not configured\n");
2502f1453773SNicholas Bellinger 		return -ENODEV;
2503f1453773SNicholas Bellinger 	}
2504c66ac9dbSNicholas Bellinger 
250557103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2506c66ac9dbSNicholas Bellinger 	if (ret < 0) {
25076708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access state from"
2508c66ac9dbSNicholas Bellinger 				" %s\n", page);
250957103d7fSJingoo Han 		return ret;
2510c66ac9dbSNicholas Bellinger 	}
2511c66ac9dbSNicholas Bellinger 	new_state = (int)tmp;
2512c66ac9dbSNicholas Bellinger 
2513125d0119SHannes Reinecke 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2514125d0119SHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA"
2515125d0119SHannes Reinecke 			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
2516c66ac9dbSNicholas Bellinger 		return -EINVAL;
2517c66ac9dbSNicholas Bellinger 	}
2518c66094bfSHannes Reinecke 	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2519c66094bfSHannes Reinecke 	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2520c66094bfSHannes Reinecke 		/* LBA DEPENDENT is only allowed with implicit ALUA */
2521c66094bfSHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA transition"
2522c66094bfSHannes Reinecke 		       " while explicit ALUA management is enabled\n");
2523c66094bfSHannes Reinecke 		return -EINVAL;
2524c66094bfSHannes Reinecke 	}
2525c66ac9dbSNicholas Bellinger 
25260fd97ccfSChristoph Hellwig 	ret = core_alua_do_port_transition(tg_pt_gp, dev,
2527c66ac9dbSNicholas Bellinger 					NULL, NULL, new_state, 0);
2528c66ac9dbSNicholas Bellinger 	return (!ret) ? count : -EINVAL;
2529c66ac9dbSNicholas Bellinger }
2530c66ac9dbSNicholas Bellinger 
25312eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2532c66ac9dbSNicholas Bellinger 		char *page)
2533c66ac9dbSNicholas Bellinger {
25342eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2535c66ac9dbSNicholas Bellinger 	return sprintf(page, "%s\n",
2536c66ac9dbSNicholas Bellinger 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2537c66ac9dbSNicholas Bellinger }
2538c66ac9dbSNicholas Bellinger 
25392eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_store(
25402eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2541c66ac9dbSNicholas Bellinger {
25422eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2543c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2544c66ac9dbSNicholas Bellinger 	int new_status, ret;
2545c66ac9dbSNicholas Bellinger 
25466708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
25476708bb27SAndy Grover 		pr_err("Unable to do set ALUA access status on non"
2548c66ac9dbSNicholas Bellinger 			" valid tg_pt_gp ID: %hu\n",
2549c66ac9dbSNicholas Bellinger 			tg_pt_gp->tg_pt_gp_valid_id);
2550c66ac9dbSNicholas Bellinger 		return -EINVAL;
2551c66ac9dbSNicholas Bellinger 	}
2552c66ac9dbSNicholas Bellinger 
255357103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2554c66ac9dbSNicholas Bellinger 	if (ret < 0) {
25556708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access status"
2556c66ac9dbSNicholas Bellinger 				" from %s\n", page);
255757103d7fSJingoo Han 		return ret;
2558c66ac9dbSNicholas Bellinger 	}
2559c66ac9dbSNicholas Bellinger 	new_status = (int)tmp;
2560c66ac9dbSNicholas Bellinger 
2561c66ac9dbSNicholas Bellinger 	if ((new_status != ALUA_STATUS_NONE) &&
2562125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2563125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
25646708bb27SAndy Grover 		pr_err("Illegal ALUA access status: 0x%02x\n",
2565c66ac9dbSNicholas Bellinger 				new_status);
2566c66ac9dbSNicholas Bellinger 		return -EINVAL;
2567c66ac9dbSNicholas Bellinger 	}
2568c66ac9dbSNicholas Bellinger 
2569c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2570c66ac9dbSNicholas Bellinger 	return count;
2571c66ac9dbSNicholas Bellinger }
2572c66ac9dbSNicholas Bellinger 
25732eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2574c66ac9dbSNicholas Bellinger 		char *page)
2575c66ac9dbSNicholas Bellinger {
25762eafd729SChristoph Hellwig 	return core_alua_show_access_type(to_tg_pt_gp(item), page);
2577c66ac9dbSNicholas Bellinger }
2578c66ac9dbSNicholas Bellinger 
25792eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
25802eafd729SChristoph Hellwig 		const char *page, size_t count)
2581c66ac9dbSNicholas Bellinger {
25822eafd729SChristoph Hellwig 	return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2583c66ac9dbSNicholas Bellinger }
2584c66ac9dbSNicholas Bellinger 
25852eafd729SChristoph Hellwig #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit)				\
25862eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_show(		\
25872eafd729SChristoph Hellwig 		struct config_item *item, char *p)			\
2588b0a382c5SHannes Reinecke {									\
25892eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
25902eafd729SChristoph Hellwig 	return sprintf(p, "%d\n",					\
25912eafd729SChristoph Hellwig 		!!(t->tg_pt_gp_alua_supported_states & _bit));		\
25922eafd729SChristoph Hellwig }									\
25932eafd729SChristoph Hellwig 									\
25942eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_store(		\
25952eafd729SChristoph Hellwig 		struct config_item *item, const char *p, size_t c)	\
2596b0a382c5SHannes Reinecke {									\
25972eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
2598b0a382c5SHannes Reinecke 	unsigned long tmp;						\
2599b0a382c5SHannes Reinecke 	int ret;							\
2600b0a382c5SHannes Reinecke 									\
2601b0a382c5SHannes Reinecke 	if (!t->tg_pt_gp_valid_id) {					\
2602c0dcafd8SHannes Reinecke 		pr_err("Unable to do set " #_name " ALUA state on non"	\
2603b0a382c5SHannes Reinecke 		       " valid tg_pt_gp ID: %hu\n",			\
2604b0a382c5SHannes Reinecke 		       t->tg_pt_gp_valid_id);				\
2605b0a382c5SHannes Reinecke 		return -EINVAL;						\
2606b0a382c5SHannes Reinecke 	}								\
2607b0a382c5SHannes Reinecke 									\
2608b0a382c5SHannes Reinecke 	ret = kstrtoul(p, 0, &tmp);					\
2609b0a382c5SHannes Reinecke 	if (ret < 0) {							\
2610b0a382c5SHannes Reinecke 		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
2611b0a382c5SHannes Reinecke 		return -EINVAL;						\
2612b0a382c5SHannes Reinecke 	}								\
2613b0a382c5SHannes Reinecke 	if (tmp > 1) {							\
2614b0a382c5SHannes Reinecke 		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2615b0a382c5SHannes Reinecke 		return -EINVAL;						\
2616b0a382c5SHannes Reinecke 	}								\
26171f0b030cSSebastian Herbszt 	if (tmp)							\
26182eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states |= _bit;		\
2619b0a382c5SHannes Reinecke 	else								\
26202eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states &= ~_bit;		\
2621b0a382c5SHannes Reinecke 									\
2622b0a382c5SHannes Reinecke 	return c;							\
26236be526c4SHannes Reinecke }
26246be526c4SHannes Reinecke 
26252eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
26262eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
26272eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
26282eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
26292eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
26302eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
26312eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
26326be526c4SHannes Reinecke 
26332eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_show(
26342eafd729SChristoph Hellwig 		struct config_item *item, char *page)
2635c66ac9dbSNicholas Bellinger {
26362eafd729SChristoph Hellwig 	return sprintf(page, "%d\n",
26372eafd729SChristoph Hellwig 		to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
2638c66ac9dbSNicholas Bellinger }
2639c66ac9dbSNicholas Bellinger 
26402eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_store(
26412eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2642c66ac9dbSNicholas Bellinger {
26432eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2644c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2645c66ac9dbSNicholas Bellinger 	int ret;
2646c66ac9dbSNicholas Bellinger 
264757103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2648c66ac9dbSNicholas Bellinger 	if (ret < 0) {
26496708bb27SAndy Grover 		pr_err("Unable to extract alua_write_metadata\n");
265057103d7fSJingoo Han 		return ret;
2651c66ac9dbSNicholas Bellinger 	}
2652c66ac9dbSNicholas Bellinger 
2653c66ac9dbSNicholas Bellinger 	if ((tmp != 0) && (tmp != 1)) {
26546708bb27SAndy Grover 		pr_err("Illegal value for alua_write_metadata:"
2655c66ac9dbSNicholas Bellinger 			" %lu\n", tmp);
2656c66ac9dbSNicholas Bellinger 		return -EINVAL;
2657c66ac9dbSNicholas Bellinger 	}
2658c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2659c66ac9dbSNicholas Bellinger 
2660c66ac9dbSNicholas Bellinger 	return count;
2661c66ac9dbSNicholas Bellinger }
2662c66ac9dbSNicholas Bellinger 
26632eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2664c66ac9dbSNicholas Bellinger 		char *page)
2665c66ac9dbSNicholas Bellinger {
26662eafd729SChristoph Hellwig 	return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
2667c66ac9dbSNicholas Bellinger }
2668c66ac9dbSNicholas Bellinger 
26692eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
26702eafd729SChristoph Hellwig 		const char *page, size_t count)
2671c66ac9dbSNicholas Bellinger {
26722eafd729SChristoph Hellwig 	return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
26732eafd729SChristoph Hellwig 			count);
2674c66ac9dbSNicholas Bellinger }
2675c66ac9dbSNicholas Bellinger 
26762eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2677c66ac9dbSNicholas Bellinger 		char *page)
2678c66ac9dbSNicholas Bellinger {
26792eafd729SChristoph Hellwig 	return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
2680c66ac9dbSNicholas Bellinger }
2681c66ac9dbSNicholas Bellinger 
26822eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
26832eafd729SChristoph Hellwig 		const char *page, size_t count)
2684c66ac9dbSNicholas Bellinger {
26852eafd729SChristoph Hellwig 	return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
26862eafd729SChristoph Hellwig 			count);
2687c66ac9dbSNicholas Bellinger }
2688c66ac9dbSNicholas Bellinger 
26892eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
26902eafd729SChristoph Hellwig 		struct config_item *item, char *page)
26912eafd729SChristoph Hellwig {
26922eafd729SChristoph Hellwig 	return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
26932eafd729SChristoph Hellwig }
2694c66ac9dbSNicholas Bellinger 
26952eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
26962eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
26972eafd729SChristoph Hellwig {
26982eafd729SChristoph Hellwig 	return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
26992eafd729SChristoph Hellwig 			count);
27002eafd729SChristoph Hellwig }
27012eafd729SChristoph Hellwig 
27022eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
27035b9a4d72SNicholas Bellinger 		char *page)
27045b9a4d72SNicholas Bellinger {
27052eafd729SChristoph Hellwig 	return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
27065b9a4d72SNicholas Bellinger }
27075b9a4d72SNicholas Bellinger 
27082eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
27092eafd729SChristoph Hellwig 		const char *page, size_t count)
27105b9a4d72SNicholas Bellinger {
27112eafd729SChristoph Hellwig 	return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
27125b9a4d72SNicholas Bellinger }
27135b9a4d72SNicholas Bellinger 
27142eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2715c66ac9dbSNicholas Bellinger 		char *page)
2716c66ac9dbSNicholas Bellinger {
27172eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2718c66ac9dbSNicholas Bellinger 
27196708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id)
2720c66ac9dbSNicholas Bellinger 		return 0;
2721c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2722c66ac9dbSNicholas Bellinger }
2723c66ac9dbSNicholas Bellinger 
27242eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
27252eafd729SChristoph Hellwig 		const char *page, size_t count)
2726c66ac9dbSNicholas Bellinger {
27272eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2728c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2729c66ac9dbSNicholas Bellinger 	unsigned long tg_pt_gp_id;
2730c66ac9dbSNicholas Bellinger 	int ret;
2731c66ac9dbSNicholas Bellinger 
273257103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tg_pt_gp_id);
2733c66ac9dbSNicholas Bellinger 	if (ret < 0) {
27343d035237SHannes Reinecke 		pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
27353d035237SHannes Reinecke 		       page);
273657103d7fSJingoo Han 		return ret;
2737c66ac9dbSNicholas Bellinger 	}
2738c66ac9dbSNicholas Bellinger 	if (tg_pt_gp_id > 0x0000ffff) {
27393d035237SHannes Reinecke 		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
27403d035237SHannes Reinecke 		       tg_pt_gp_id);
2741c66ac9dbSNicholas Bellinger 		return -EINVAL;
2742c66ac9dbSNicholas Bellinger 	}
2743c66ac9dbSNicholas Bellinger 
2744c66ac9dbSNicholas Bellinger 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2745c66ac9dbSNicholas Bellinger 	if (ret < 0)
2746c66ac9dbSNicholas Bellinger 		return -EINVAL;
2747c66ac9dbSNicholas Bellinger 
27486708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2749c66ac9dbSNicholas Bellinger 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
2750c66ac9dbSNicholas Bellinger 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
2751c66ac9dbSNicholas Bellinger 		tg_pt_gp->tg_pt_gp_id);
2752c66ac9dbSNicholas Bellinger 
2753c66ac9dbSNicholas Bellinger 	return count;
2754c66ac9dbSNicholas Bellinger }
2755c66ac9dbSNicholas Bellinger 
27562eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2757c66ac9dbSNicholas Bellinger 		char *page)
2758c66ac9dbSNicholas Bellinger {
27592eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2760c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
2761c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2762c66ac9dbSNicholas Bellinger 	unsigned char buf[TG_PT_GROUP_NAME_BUF];
2763c66ac9dbSNicholas Bellinger 
2764c66ac9dbSNicholas Bellinger 	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2765c66ac9dbSNicholas Bellinger 
2766c66ac9dbSNicholas Bellinger 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2767adf653f9SChristoph Hellwig 	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2768adf653f9SChristoph Hellwig 			lun_tg_pt_gp_link) {
2769adf653f9SChristoph Hellwig 		struct se_portal_group *tpg = lun->lun_tpg;
2770c66ac9dbSNicholas Bellinger 
2771c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
277230c7ca93SDavid Disseldorp 			"/%s\n", tpg->se_tpg_tfo->fabric_name,
2773e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2774e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
2775c66ac9dbSNicholas Bellinger 			config_item_name(&lun->lun_group.cg_item));
2776c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2777c66ac9dbSNicholas Bellinger 
2778c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
27796708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2780c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2781c66ac9dbSNicholas Bellinger 			break;
2782c66ac9dbSNicholas Bellinger 		}
2783c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2784c66ac9dbSNicholas Bellinger 		len += cur_len;
2785c66ac9dbSNicholas Bellinger 	}
2786c66ac9dbSNicholas Bellinger 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2787c66ac9dbSNicholas Bellinger 
2788c66ac9dbSNicholas Bellinger 	return len;
2789c66ac9dbSNicholas Bellinger }
2790c66ac9dbSNicholas Bellinger 
27912eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
27922eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
27932eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
27942eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
27952eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
27962eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
27972eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
27982eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
27992eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
28002eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
28012eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
28022eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
28032eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
28042eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
28052eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
28062eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
28072eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
2808c66ac9dbSNicholas Bellinger 
2809c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
28102eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_state,
28112eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_status,
28122eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_type,
28132eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_transitioning,
28142eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_offline,
28152eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_lba_dependent,
28162eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_unavailable,
28172eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_standby,
28182eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_nonoptimized,
28192eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_optimized,
28202eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_write_metadata,
28212eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_nonop_delay_msecs,
28222eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_trans_delay_msecs,
28232eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_implicit_trans_secs,
28242eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_preferred,
28252eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_tg_pt_gp_id,
28262eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_members,
2827c66ac9dbSNicholas Bellinger 	NULL,
2828c66ac9dbSNicholas Bellinger };
2829c66ac9dbSNicholas Bellinger 
28301f6fe7cbSNicholas Bellinger static void target_core_alua_tg_pt_gp_release(struct config_item *item)
28311f6fe7cbSNicholas Bellinger {
28321f6fe7cbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
28331f6fe7cbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
28341f6fe7cbSNicholas Bellinger 
28351f6fe7cbSNicholas Bellinger 	core_alua_free_tg_pt_gp(tg_pt_gp);
28361f6fe7cbSNicholas Bellinger }
28371f6fe7cbSNicholas Bellinger 
2838c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
28391f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_tg_pt_gp_release,
2840c66ac9dbSNicholas Bellinger };
2841c66ac9dbSNicholas Bellinger 
2842ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_tg_pt_gp_cit = {
2843c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
2844c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
2845c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2846c66ac9dbSNicholas Bellinger };
2847c66ac9dbSNicholas Bellinger 
2848c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2849c66ac9dbSNicholas Bellinger 
285072aca57bSNicholas Bellinger /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2851c66ac9dbSNicholas Bellinger 
2852c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_tg_pt_gp(
2853c66ac9dbSNicholas Bellinger 	struct config_group *group,
2854c66ac9dbSNicholas Bellinger 	const char *name)
2855c66ac9dbSNicholas Bellinger {
2856c66ac9dbSNicholas Bellinger 	struct t10_alua *alua = container_of(group, struct t10_alua,
2857c66ac9dbSNicholas Bellinger 					alua_tg_pt_gps_group);
2858c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2859c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = NULL;
2860c66ac9dbSNicholas Bellinger 	struct config_item *alua_tg_pt_gp_ci = NULL;
2861c66ac9dbSNicholas Bellinger 
28620fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
28636708bb27SAndy Grover 	if (!tg_pt_gp)
2864c66ac9dbSNicholas Bellinger 		return NULL;
2865c66ac9dbSNicholas Bellinger 
2866c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2867c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2868c66ac9dbSNicholas Bellinger 
2869c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
2870c66ac9dbSNicholas Bellinger 			&target_core_alua_tg_pt_gp_cit);
2871c66ac9dbSNicholas Bellinger 
28726708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2873c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s\n",
2874c66ac9dbSNicholas Bellinger 		config_item_name(alua_tg_pt_gp_ci));
2875c66ac9dbSNicholas Bellinger 
2876c66ac9dbSNicholas Bellinger 	return alua_tg_pt_gp_cg;
2877c66ac9dbSNicholas Bellinger }
2878c66ac9dbSNicholas Bellinger 
2879c66ac9dbSNicholas Bellinger static void target_core_alua_drop_tg_pt_gp(
2880c66ac9dbSNicholas Bellinger 	struct config_group *group,
2881c66ac9dbSNicholas Bellinger 	struct config_item *item)
2882c66ac9dbSNicholas Bellinger {
2883c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2884c66ac9dbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2885c66ac9dbSNicholas Bellinger 
28866708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2887c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
2888c66ac9dbSNicholas Bellinger 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
28891f6fe7cbSNicholas Bellinger 	/*
28901f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
28911f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_tg_pt_gp_release().
28921f6fe7cbSNicholas Bellinger 	 */
2893c66ac9dbSNicholas Bellinger 	config_item_put(item);
2894c66ac9dbSNicholas Bellinger }
2895c66ac9dbSNicholas Bellinger 
2896c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2897c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_tg_pt_gp,
2898c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
2899c66ac9dbSNicholas Bellinger };
2900c66ac9dbSNicholas Bellinger 
290172aca57bSNicholas Bellinger TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
2902c66ac9dbSNicholas Bellinger 
290372aca57bSNicholas Bellinger /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2904c66ac9dbSNicholas Bellinger 
2905c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_cit */
2906c66ac9dbSNicholas Bellinger 
2907c66ac9dbSNicholas Bellinger /*
2908c66ac9dbSNicholas Bellinger  * target_core_alua_cit is a ConfigFS group that lives under
2909c66ac9dbSNicholas Bellinger  * /sys/kernel/config/target/core/alua.  There are default groups
2910c66ac9dbSNicholas Bellinger  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2911c66ac9dbSNicholas Bellinger  * target_core_alua_cit in target_core_init_configfs() below.
2912c66ac9dbSNicholas Bellinger  */
2913ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_cit = {
2914c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2915c66ac9dbSNicholas Bellinger 	.ct_attrs		= NULL,
2916c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2917c66ac9dbSNicholas Bellinger };
2918c66ac9dbSNicholas Bellinger 
2919c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_cit */
2920c66ac9dbSNicholas Bellinger 
2921d23ab570SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_stat_cit */
292212d23384SNicholas Bellinger 
292312d23384SNicholas Bellinger static struct config_group *target_core_stat_mkdir(
292412d23384SNicholas Bellinger 	struct config_group *group,
292512d23384SNicholas Bellinger 	const char *name)
292612d23384SNicholas Bellinger {
292712d23384SNicholas Bellinger 	return ERR_PTR(-ENOSYS);
292812d23384SNicholas Bellinger }
292912d23384SNicholas Bellinger 
293012d23384SNicholas Bellinger static void target_core_stat_rmdir(
293112d23384SNicholas Bellinger 	struct config_group *group,
293212d23384SNicholas Bellinger 	struct config_item *item)
293312d23384SNicholas Bellinger {
293412d23384SNicholas Bellinger 	return;
293512d23384SNicholas Bellinger }
293612d23384SNicholas Bellinger 
293712d23384SNicholas Bellinger static struct configfs_group_operations target_core_stat_group_ops = {
293812d23384SNicholas Bellinger 	.make_group		= &target_core_stat_mkdir,
293912d23384SNicholas Bellinger 	.drop_item		= &target_core_stat_rmdir,
294012d23384SNicholas Bellinger };
294112d23384SNicholas Bellinger 
2942d23ab570SNicholas Bellinger TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
294312d23384SNicholas Bellinger 
2944d23ab570SNicholas Bellinger /* End functions for struct config_item_type tb_dev_stat_cit */
294512d23384SNicholas Bellinger 
2946c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_hba_cit */
2947c66ac9dbSNicholas Bellinger 
2948c66ac9dbSNicholas Bellinger static struct config_group *target_core_make_subdev(
2949c66ac9dbSNicholas Bellinger 	struct config_group *group,
2950c66ac9dbSNicholas Bellinger 	const char *name)
2951c66ac9dbSNicholas Bellinger {
2952c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2953c66ac9dbSNicholas Bellinger 	struct config_item *hba_ci = &group->cg_item;
2954c66ac9dbSNicholas Bellinger 	struct se_hba *hba = item_to_hba(hba_ci);
29550a06d430SChristoph Hellwig 	struct target_backend *tb = hba->backend;
29560fd97ccfSChristoph Hellwig 	struct se_device *dev;
295712d23384SNicholas Bellinger 	int errno = -ENOMEM, ret;
2958c66ac9dbSNicholas Bellinger 
295912d23384SNicholas Bellinger 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
296012d23384SNicholas Bellinger 	if (ret)
296112d23384SNicholas Bellinger 		return ERR_PTR(ret);
2962c66ac9dbSNicholas Bellinger 
29630fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, name);
29640fd97ccfSChristoph Hellwig 	if (!dev)
29650fd97ccfSChristoph Hellwig 		goto out_unlock;
2966c66ac9dbSNicholas Bellinger 
29671ae1602dSChristoph Hellwig 	config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
2968c66ac9dbSNicholas Bellinger 
29698dc31ff9SMike Christie 	config_group_init_type_name(&dev->dev_action_group, "action",
29708dc31ff9SMike Christie 			&tb->tb_dev_action_cit);
29718dc31ff9SMike Christie 	configfs_add_default_group(&dev->dev_action_group, &dev->dev_group);
29728dc31ff9SMike Christie 
29730fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
29740a06d430SChristoph Hellwig 			&tb->tb_dev_attrib_cit);
29751ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
29761ae1602dSChristoph Hellwig 
29770fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_pr_group, "pr",
29780a06d430SChristoph Hellwig 			&tb->tb_dev_pr_cit);
29791ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
29801ae1602dSChristoph Hellwig 
29810fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
29820a06d430SChristoph Hellwig 			&tb->tb_dev_wwn_cit);
29831ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
29841ae1602dSChristoph Hellwig 			&dev->dev_group);
29851ae1602dSChristoph Hellwig 
29860fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
29870a06d430SChristoph Hellwig 			"alua", &tb->tb_dev_alua_tg_pt_gps_cit);
29881ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
29891ae1602dSChristoph Hellwig 			&dev->dev_group);
29901ae1602dSChristoph Hellwig 
29910fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
29920a06d430SChristoph Hellwig 			"statistics", &tb->tb_dev_stat_cit);
29931ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_stat_grps.stat_group,
29941ae1602dSChristoph Hellwig 			&dev->dev_group);
299512d23384SNicholas Bellinger 
2996c66ac9dbSNicholas Bellinger 	/*
299712d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2998c66ac9dbSNicholas Bellinger 	 */
29990fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
30006708bb27SAndy Grover 	if (!tg_pt_gp)
30011ae1602dSChristoph Hellwig 		goto out_free_device;
30020fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
3003c66ac9dbSNicholas Bellinger 
3004c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
3005c66ac9dbSNicholas Bellinger 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
30061ae1602dSChristoph Hellwig 	configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
30071ae1602dSChristoph Hellwig 			&dev->t10_alua.alua_tg_pt_gps_group);
30081ae1602dSChristoph Hellwig 
300912d23384SNicholas Bellinger 	/*
301012d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/statistics/ default groups
301112d23384SNicholas Bellinger 	 */
30120fd97ccfSChristoph Hellwig 	target_stat_setup_dev_default_groups(dev);
3013c66ac9dbSNicholas Bellinger 
3014c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
30151ae1602dSChristoph Hellwig 	return &dev->dev_group;
30160fd97ccfSChristoph Hellwig 
30170fd97ccfSChristoph Hellwig out_free_device:
30180fd97ccfSChristoph Hellwig 	target_free_device(dev);
30190fd97ccfSChristoph Hellwig out_unlock:
3020c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
302112d23384SNicholas Bellinger 	return ERR_PTR(errno);
3022c66ac9dbSNicholas Bellinger }
3023c66ac9dbSNicholas Bellinger 
3024c66ac9dbSNicholas Bellinger static void target_core_drop_subdev(
3025c66ac9dbSNicholas Bellinger 	struct config_group *group,
3026c66ac9dbSNicholas Bellinger 	struct config_item *item)
3027c66ac9dbSNicholas Bellinger {
30280fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
30290fd97ccfSChristoph Hellwig 	struct se_device *dev =
30300fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
3031c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
3032c66ac9dbSNicholas Bellinger 
30330fd97ccfSChristoph Hellwig 	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
3034c66ac9dbSNicholas Bellinger 
30351f6fe7cbSNicholas Bellinger 	mutex_lock(&hba->hba_access_mutex);
3036c66ac9dbSNicholas Bellinger 
30371ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
30381ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
303912d23384SNicholas Bellinger 
30401f6fe7cbSNicholas Bellinger 	/*
30411f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
30421f6fe7cbSNicholas Bellinger 	 * directly from target_core_alua_tg_pt_gp_release().
30431f6fe7cbSNicholas Bellinger 	 */
30440fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = NULL;
3045c66ac9dbSNicholas Bellinger 
30461ae1602dSChristoph Hellwig 	configfs_remove_default_groups(dev_cg);
30471ae1602dSChristoph Hellwig 
30481f6fe7cbSNicholas Bellinger 	/*
30490fd97ccfSChristoph Hellwig 	 * se_dev is released from target_core_dev_item_ops->release()
30501f6fe7cbSNicholas Bellinger 	 */
3051c66ac9dbSNicholas Bellinger 	config_item_put(item);
3052c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
3053c66ac9dbSNicholas Bellinger }
3054c66ac9dbSNicholas Bellinger 
3055c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_hba_group_ops = {
3056c66ac9dbSNicholas Bellinger 	.make_group		= target_core_make_subdev,
3057c66ac9dbSNicholas Bellinger 	.drop_item		= target_core_drop_subdev,
3058c66ac9dbSNicholas Bellinger };
3059c66ac9dbSNicholas Bellinger 
3060c66ac9dbSNicholas Bellinger 
30612eafd729SChristoph Hellwig static inline struct se_hba *to_hba(struct config_item *item)
3062c66ac9dbSNicholas Bellinger {
30632eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_hba, hba_group);
30642eafd729SChristoph Hellwig }
30652eafd729SChristoph Hellwig 
30662eafd729SChristoph Hellwig static ssize_t target_hba_info_show(struct config_item *item, char *page)
30672eafd729SChristoph Hellwig {
30682eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
30692eafd729SChristoph Hellwig 
3070c66ac9dbSNicholas Bellinger 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
30710a06d430SChristoph Hellwig 			hba->hba_id, hba->backend->ops->name,
3072ce8dd25dSChristoph Hellwig 			TARGET_CORE_VERSION);
3073c66ac9dbSNicholas Bellinger }
3074c66ac9dbSNicholas Bellinger 
30752eafd729SChristoph Hellwig static ssize_t target_hba_mode_show(struct config_item *item, char *page)
3076c66ac9dbSNicholas Bellinger {
30772eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
3078c66ac9dbSNicholas Bellinger 	int hba_mode = 0;
3079c66ac9dbSNicholas Bellinger 
3080c66ac9dbSNicholas Bellinger 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3081c66ac9dbSNicholas Bellinger 		hba_mode = 1;
3082c66ac9dbSNicholas Bellinger 
3083c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", hba_mode);
3084c66ac9dbSNicholas Bellinger }
3085c66ac9dbSNicholas Bellinger 
30862eafd729SChristoph Hellwig static ssize_t target_hba_mode_store(struct config_item *item,
3087c66ac9dbSNicholas Bellinger 		const char *page, size_t count)
3088c66ac9dbSNicholas Bellinger {
30892eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
3090c66ac9dbSNicholas Bellinger 	unsigned long mode_flag;
3091c66ac9dbSNicholas Bellinger 	int ret;
3092c66ac9dbSNicholas Bellinger 
30930a06d430SChristoph Hellwig 	if (hba->backend->ops->pmode_enable_hba == NULL)
3094c66ac9dbSNicholas Bellinger 		return -EINVAL;
3095c66ac9dbSNicholas Bellinger 
309657103d7fSJingoo Han 	ret = kstrtoul(page, 0, &mode_flag);
3097c66ac9dbSNicholas Bellinger 	if (ret < 0) {
30986708bb27SAndy Grover 		pr_err("Unable to extract hba mode flag: %d\n", ret);
309957103d7fSJingoo Han 		return ret;
3100c66ac9dbSNicholas Bellinger 	}
3101c66ac9dbSNicholas Bellinger 
31020fd97ccfSChristoph Hellwig 	if (hba->dev_count) {
31036708bb27SAndy Grover 		pr_err("Unable to set hba_mode with active devices\n");
3104c66ac9dbSNicholas Bellinger 		return -EINVAL;
3105c66ac9dbSNicholas Bellinger 	}
3106c66ac9dbSNicholas Bellinger 
31070a06d430SChristoph Hellwig 	ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3108c66ac9dbSNicholas Bellinger 	if (ret < 0)
3109c66ac9dbSNicholas Bellinger 		return -EINVAL;
3110c66ac9dbSNicholas Bellinger 	if (ret > 0)
3111c66ac9dbSNicholas Bellinger 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3112c66ac9dbSNicholas Bellinger 	else if (ret == 0)
3113c66ac9dbSNicholas Bellinger 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3114c66ac9dbSNicholas Bellinger 
3115c66ac9dbSNicholas Bellinger 	return count;
3116c66ac9dbSNicholas Bellinger }
3117c66ac9dbSNicholas Bellinger 
31182eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_, hba_info);
31192eafd729SChristoph Hellwig CONFIGFS_ATTR(target_, hba_mode);
3120c66ac9dbSNicholas Bellinger 
31211f6fe7cbSNicholas Bellinger static void target_core_hba_release(struct config_item *item)
31221f6fe7cbSNicholas Bellinger {
31231f6fe7cbSNicholas Bellinger 	struct se_hba *hba = container_of(to_config_group(item),
31241f6fe7cbSNicholas Bellinger 				struct se_hba, hba_group);
31251f6fe7cbSNicholas Bellinger 	core_delete_hba(hba);
31261f6fe7cbSNicholas Bellinger }
31271f6fe7cbSNicholas Bellinger 
3128c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_hba_attrs[] = {
31292eafd729SChristoph Hellwig 	&target_attr_hba_info,
31302eafd729SChristoph Hellwig 	&target_attr_hba_mode,
3131c66ac9dbSNicholas Bellinger 	NULL,
3132c66ac9dbSNicholas Bellinger };
3133c66ac9dbSNicholas Bellinger 
3134c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_hba_item_ops = {
31351f6fe7cbSNicholas Bellinger 	.release		= target_core_hba_release,
3136c66ac9dbSNicholas Bellinger };
3137c66ac9dbSNicholas Bellinger 
3138ece550b5SBhumika Goyal static const struct config_item_type target_core_hba_cit = {
3139c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_hba_item_ops,
3140c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_hba_group_ops,
3141c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_hba_attrs,
3142c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3143c66ac9dbSNicholas Bellinger };
3144c66ac9dbSNicholas Bellinger 
3145c66ac9dbSNicholas Bellinger static struct config_group *target_core_call_addhbatotarget(
3146c66ac9dbSNicholas Bellinger 	struct config_group *group,
3147c66ac9dbSNicholas Bellinger 	const char *name)
3148c66ac9dbSNicholas Bellinger {
3149c66ac9dbSNicholas Bellinger 	char *se_plugin_str, *str, *str2;
3150c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
3151c66ac9dbSNicholas Bellinger 	char buf[TARGET_CORE_NAME_MAX_LEN];
3152c66ac9dbSNicholas Bellinger 	unsigned long plugin_dep_id = 0;
3153c66ac9dbSNicholas Bellinger 	int ret;
3154c66ac9dbSNicholas Bellinger 
3155c66ac9dbSNicholas Bellinger 	memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
315660d645a4SDan Carpenter 	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
31576708bb27SAndy Grover 		pr_err("Passed *name strlen(): %d exceeds"
3158c66ac9dbSNicholas Bellinger 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3159c66ac9dbSNicholas Bellinger 			TARGET_CORE_NAME_MAX_LEN);
3160c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENAMETOOLONG);
3161c66ac9dbSNicholas Bellinger 	}
3162c66ac9dbSNicholas Bellinger 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3163c66ac9dbSNicholas Bellinger 
3164c66ac9dbSNicholas Bellinger 	str = strstr(buf, "_");
31656708bb27SAndy Grover 	if (!str) {
31666708bb27SAndy Grover 		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3167c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
3168c66ac9dbSNicholas Bellinger 	}
3169c66ac9dbSNicholas Bellinger 	se_plugin_str = buf;
3170c66ac9dbSNicholas Bellinger 	/*
3171c66ac9dbSNicholas Bellinger 	 * Special case for subsystem plugins that have "_" in their names.
3172c66ac9dbSNicholas Bellinger 	 * Namely rd_direct and rd_mcp..
3173c66ac9dbSNicholas Bellinger 	 */
3174c66ac9dbSNicholas Bellinger 	str2 = strstr(str+1, "_");
31756708bb27SAndy Grover 	if (str2) {
3176c66ac9dbSNicholas Bellinger 		*str2 = '\0'; /* Terminate for *se_plugin_str */
3177c66ac9dbSNicholas Bellinger 		str2++; /* Skip to start of plugin dependent ID */
3178c66ac9dbSNicholas Bellinger 		str = str2;
3179c66ac9dbSNicholas Bellinger 	} else {
3180c66ac9dbSNicholas Bellinger 		*str = '\0'; /* Terminate for *se_plugin_str */
3181c66ac9dbSNicholas Bellinger 		str++; /* Skip to start of plugin dependent ID */
3182c66ac9dbSNicholas Bellinger 	}
3183c66ac9dbSNicholas Bellinger 
318457103d7fSJingoo Han 	ret = kstrtoul(str, 0, &plugin_dep_id);
3185c66ac9dbSNicholas Bellinger 	if (ret < 0) {
318657103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
3187c66ac9dbSNicholas Bellinger 				" plugin_dep_id\n", ret);
318857103d7fSJingoo Han 		return ERR_PTR(ret);
3189c66ac9dbSNicholas Bellinger 	}
3190c66ac9dbSNicholas Bellinger 	/*
3191c66ac9dbSNicholas Bellinger 	 * Load up TCM subsystem plugins if they have not already been loaded.
3192c66ac9dbSNicholas Bellinger 	 */
3193dbc5623eSNicholas Bellinger 	transport_subsystem_check_init();
3194c66ac9dbSNicholas Bellinger 
3195c66ac9dbSNicholas Bellinger 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3196c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
3197c66ac9dbSNicholas Bellinger 		return ERR_CAST(hba);
3198c66ac9dbSNicholas Bellinger 
3199c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&hba->hba_group, name,
3200c66ac9dbSNicholas Bellinger 			&target_core_hba_cit);
3201c66ac9dbSNicholas Bellinger 
3202c66ac9dbSNicholas Bellinger 	return &hba->hba_group;
3203c66ac9dbSNicholas Bellinger }
3204c66ac9dbSNicholas Bellinger 
3205c66ac9dbSNicholas Bellinger static void target_core_call_delhbafromtarget(
3206c66ac9dbSNicholas Bellinger 	struct config_group *group,
3207c66ac9dbSNicholas Bellinger 	struct config_item *item)
3208c66ac9dbSNicholas Bellinger {
32091f6fe7cbSNicholas Bellinger 	/*
32101f6fe7cbSNicholas Bellinger 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
32111f6fe7cbSNicholas Bellinger 	 * -> target_core_hba_release()
32121f6fe7cbSNicholas Bellinger 	 */
3213c66ac9dbSNicholas Bellinger 	config_item_put(item);
3214c66ac9dbSNicholas Bellinger }
3215c66ac9dbSNicholas Bellinger 
3216c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_group_ops = {
3217c66ac9dbSNicholas Bellinger 	.make_group	= target_core_call_addhbatotarget,
3218c66ac9dbSNicholas Bellinger 	.drop_item	= target_core_call_delhbafromtarget,
3219c66ac9dbSNicholas Bellinger };
3220c66ac9dbSNicholas Bellinger 
3221ece550b5SBhumika Goyal static const struct config_item_type target_core_cit = {
3222c66ac9dbSNicholas Bellinger 	.ct_item_ops	= NULL,
3223c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_group_ops,
3224c66ac9dbSNicholas Bellinger 	.ct_attrs	= NULL,
3225c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
3226c66ac9dbSNicholas Bellinger };
3227c66ac9dbSNicholas Bellinger 
3228c66ac9dbSNicholas Bellinger /* Stop functions for struct config_item_type target_core_hba_cit */
3229c66ac9dbSNicholas Bellinger 
32300a06d430SChristoph Hellwig void target_setup_backend_cits(struct target_backend *tb)
323173112edcSNicholas Bellinger {
32320a06d430SChristoph Hellwig 	target_core_setup_dev_cit(tb);
32338dc31ff9SMike Christie 	target_core_setup_dev_action_cit(tb);
32340a06d430SChristoph Hellwig 	target_core_setup_dev_attrib_cit(tb);
32350a06d430SChristoph Hellwig 	target_core_setup_dev_pr_cit(tb);
32360a06d430SChristoph Hellwig 	target_core_setup_dev_wwn_cit(tb);
32370a06d430SChristoph Hellwig 	target_core_setup_dev_alua_tg_pt_gps_cit(tb);
32380a06d430SChristoph Hellwig 	target_core_setup_dev_stat_cit(tb);
323973112edcSNicholas Bellinger }
324073112edcSNicholas Bellinger 
324178a6295cSLee Duncan static void target_init_dbroot(void)
324278a6295cSLee Duncan {
324378a6295cSLee Duncan 	struct file *fp;
324478a6295cSLee Duncan 
324578a6295cSLee Duncan 	snprintf(db_root_stage, DB_ROOT_LEN, DB_ROOT_PREFERRED);
324678a6295cSLee Duncan 	fp = filp_open(db_root_stage, O_RDONLY, 0);
324778a6295cSLee Duncan 	if (IS_ERR(fp)) {
324878a6295cSLee Duncan 		pr_err("db_root: cannot open: %s\n", db_root_stage);
324978a6295cSLee Duncan 		return;
325078a6295cSLee Duncan 	}
325178a6295cSLee Duncan 	if (!S_ISDIR(file_inode(fp)->i_mode)) {
325278a6295cSLee Duncan 		filp_close(fp, NULL);
325378a6295cSLee Duncan 		pr_err("db_root: not a valid directory: %s\n", db_root_stage);
325478a6295cSLee Duncan 		return;
325578a6295cSLee Duncan 	}
325678a6295cSLee Duncan 	filp_close(fp, NULL);
325778a6295cSLee Duncan 
325878a6295cSLee Duncan 	strncpy(db_root, db_root_stage, DB_ROOT_LEN);
325978a6295cSLee Duncan 	pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
326078a6295cSLee Duncan }
326178a6295cSLee Duncan 
326254550fabSAxel Lin static int __init target_core_init_configfs(void)
3263c66ac9dbSNicholas Bellinger {
3264d588cf8fSChristoph Hellwig 	struct configfs_subsystem *subsys = &target_core_fabrics;
3265c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
3266c66ac9dbSNicholas Bellinger 	int ret;
3267c66ac9dbSNicholas Bellinger 
32686708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3269c66ac9dbSNicholas Bellinger 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
3270c66ac9dbSNicholas Bellinger 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3271c66ac9dbSNicholas Bellinger 
3272c66ac9dbSNicholas Bellinger 	config_group_init(&subsys->su_group);
3273c66ac9dbSNicholas Bellinger 	mutex_init(&subsys->su_mutex);
3274c66ac9dbSNicholas Bellinger 
3275e3d6f909SAndy Grover 	ret = init_se_kmem_caches();
3276c66ac9dbSNicholas Bellinger 	if (ret < 0)
3277e3d6f909SAndy Grover 		return ret;
3278c66ac9dbSNicholas Bellinger 	/*
3279c66ac9dbSNicholas Bellinger 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3280c66ac9dbSNicholas Bellinger 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3281c66ac9dbSNicholas Bellinger 	 */
32821ae1602dSChristoph Hellwig 	config_group_init_type_name(&target_core_hbagroup, "core",
32831ae1602dSChristoph Hellwig 			&target_core_cit);
32841ae1602dSChristoph Hellwig 	configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
3285c66ac9dbSNicholas Bellinger 
3286c66ac9dbSNicholas Bellinger 	/*
3287c66ac9dbSNicholas Bellinger 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3288c66ac9dbSNicholas Bellinger 	 */
32891ae1602dSChristoph Hellwig 	config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
32901ae1602dSChristoph Hellwig 	configfs_add_default_group(&alua_group, &target_core_hbagroup);
32911ae1602dSChristoph Hellwig 
3292c66ac9dbSNicholas Bellinger 	/*
3293c66ac9dbSNicholas Bellinger 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3294c66ac9dbSNicholas Bellinger 	 * groups under /sys/kernel/config/target/core/alua/
3295c66ac9dbSNicholas Bellinger 	 */
32961ae1602dSChristoph Hellwig 	config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
32971ae1602dSChristoph Hellwig 			&target_core_alua_lu_gps_cit);
32981ae1602dSChristoph Hellwig 	configfs_add_default_group(&alua_lu_gps_group, &alua_group);
3299c66ac9dbSNicholas Bellinger 
3300c66ac9dbSNicholas Bellinger 	/*
3301c66ac9dbSNicholas Bellinger 	 * Add core/alua/lu_gps/default_lu_gp
3302c66ac9dbSNicholas Bellinger 	 */
3303c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
330437bb7899SPeter Senna Tschudin 	if (IS_ERR(lu_gp)) {
330537bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3306c66ac9dbSNicholas Bellinger 		goto out_global;
330737bb7899SPeter Senna Tschudin 	}
3308c66ac9dbSNicholas Bellinger 
3309c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3310c66ac9dbSNicholas Bellinger 				&target_core_alua_lu_gp_cit);
33111ae1602dSChristoph Hellwig 	configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
33121ae1602dSChristoph Hellwig 
3313e3d6f909SAndy Grover 	default_lu_gp = lu_gp;
33141ae1602dSChristoph Hellwig 
3315c66ac9dbSNicholas Bellinger 	/*
3316c66ac9dbSNicholas Bellinger 	 * Register the target_core_mod subsystem with configfs.
3317c66ac9dbSNicholas Bellinger 	 */
3318c66ac9dbSNicholas Bellinger 	ret = configfs_register_subsystem(subsys);
3319c66ac9dbSNicholas Bellinger 	if (ret < 0) {
33206708bb27SAndy Grover 		pr_err("Error %d while registering subsystem %s\n",
3321c66ac9dbSNicholas Bellinger 			ret, subsys->su_group.cg_item.ci_namebuf);
3322c66ac9dbSNicholas Bellinger 		goto out_global;
3323c66ac9dbSNicholas Bellinger 	}
33246708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3325ce8dd25dSChristoph Hellwig 		" Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3326c66ac9dbSNicholas Bellinger 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3327c66ac9dbSNicholas Bellinger 	/*
3328c66ac9dbSNicholas Bellinger 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3329c66ac9dbSNicholas Bellinger 	 */
3330c66ac9dbSNicholas Bellinger 	ret = rd_module_init();
3331c66ac9dbSNicholas Bellinger 	if (ret < 0)
3332c66ac9dbSNicholas Bellinger 		goto out;
3333c66ac9dbSNicholas Bellinger 
33340d0f9dfbSRoland Dreier 	ret = core_dev_setup_virtual_lun0();
33350d0f9dfbSRoland Dreier 	if (ret < 0)
3336c66ac9dbSNicholas Bellinger 		goto out;
3337c66ac9dbSNicholas Bellinger 
3338f99715acSNicholas Bellinger 	ret = target_xcopy_setup_pt();
3339f99715acSNicholas Bellinger 	if (ret < 0)
3340f99715acSNicholas Bellinger 		goto out;
3341f99715acSNicholas Bellinger 
334278a6295cSLee Duncan 	target_init_dbroot();
334378a6295cSLee Duncan 
3344c66ac9dbSNicholas Bellinger 	return 0;
3345c66ac9dbSNicholas Bellinger 
3346c66ac9dbSNicholas Bellinger out:
3347c66ac9dbSNicholas Bellinger 	configfs_unregister_subsystem(subsys);
3348c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3349c66ac9dbSNicholas Bellinger 	rd_module_exit();
3350c66ac9dbSNicholas Bellinger out_global:
3351e3d6f909SAndy Grover 	if (default_lu_gp) {
3352e3d6f909SAndy Grover 		core_alua_free_lu_gp(default_lu_gp);
3353e3d6f909SAndy Grover 		default_lu_gp = NULL;
3354c66ac9dbSNicholas Bellinger 	}
3355e3d6f909SAndy Grover 	release_se_kmem_caches();
3356e3d6f909SAndy Grover 	return ret;
3357c66ac9dbSNicholas Bellinger }
3358c66ac9dbSNicholas Bellinger 
335954550fabSAxel Lin static void __exit target_core_exit_configfs(void)
3360c66ac9dbSNicholas Bellinger {
33611ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&alua_lu_gps_group);
33621ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&alua_group);
33631ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&target_core_hbagroup);
3364c66ac9dbSNicholas Bellinger 
33657c2bf6e9SNicholas Bellinger 	/*
33667c2bf6e9SNicholas Bellinger 	 * We expect subsys->su_group.default_groups to be released
33677c2bf6e9SNicholas Bellinger 	 * by configfs subsystem provider logic..
33687c2bf6e9SNicholas Bellinger 	 */
3369d588cf8fSChristoph Hellwig 	configfs_unregister_subsystem(&target_core_fabrics);
3370c66ac9dbSNicholas Bellinger 
3371e3d6f909SAndy Grover 	core_alua_free_lu_gp(default_lu_gp);
3372e3d6f909SAndy Grover 	default_lu_gp = NULL;
33737c2bf6e9SNicholas Bellinger 
33746708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3375c66ac9dbSNicholas Bellinger 			" Infrastructure\n");
3376c66ac9dbSNicholas Bellinger 
3377c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3378c66ac9dbSNicholas Bellinger 	rd_module_exit();
3379f99715acSNicholas Bellinger 	target_xcopy_release_pt();
3380e3d6f909SAndy Grover 	release_se_kmem_caches();
3381c66ac9dbSNicholas Bellinger }
3382c66ac9dbSNicholas Bellinger 
3383c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3384c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3385c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
3386c66ac9dbSNicholas Bellinger 
3387c66ac9dbSNicholas Bellinger module_init(target_core_init_configfs);
3388c66ac9dbSNicholas Bellinger module_exit(target_core_exit_configfs);
3389