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 
158a96e9783SLee Duncan 	return read_bytes;
159a96e9783SLee Duncan }
160a96e9783SLee Duncan 
161a96e9783SLee Duncan CONFIGFS_ATTR(target_core_item_, dbroot);
162a96e9783SLee Duncan 
163c66ac9dbSNicholas Bellinger static struct target_fabric_configfs *target_core_get_fabric(
164c66ac9dbSNicholas Bellinger 	const char *name)
165c66ac9dbSNicholas Bellinger {
166c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
167c66ac9dbSNicholas Bellinger 
1686708bb27SAndy Grover 	if (!name)
169c66ac9dbSNicholas Bellinger 		return NULL;
170c66ac9dbSNicholas Bellinger 
171c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
172c66ac9dbSNicholas Bellinger 	list_for_each_entry(tf, &g_tf_list, tf_list) {
1730dc2e8d1SChristoph Hellwig 		if (!strcmp(tf->tf_ops->name, name)) {
174c66ac9dbSNicholas Bellinger 			atomic_inc(&tf->tf_access_cnt);
175c66ac9dbSNicholas Bellinger 			mutex_unlock(&g_tf_lock);
176c66ac9dbSNicholas Bellinger 			return tf;
177c66ac9dbSNicholas Bellinger 		}
178c66ac9dbSNicholas Bellinger 	}
179c66ac9dbSNicholas Bellinger 	mutex_unlock(&g_tf_lock);
180c66ac9dbSNicholas Bellinger 
181c66ac9dbSNicholas Bellinger 	return NULL;
182c66ac9dbSNicholas Bellinger }
183c66ac9dbSNicholas Bellinger 
184c66ac9dbSNicholas Bellinger /*
185c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->make_group()
186c66ac9dbSNicholas Bellinger  */
187c66ac9dbSNicholas Bellinger static struct config_group *target_core_register_fabric(
188c66ac9dbSNicholas Bellinger 	struct config_group *group,
189c66ac9dbSNicholas Bellinger 	const char *name)
190c66ac9dbSNicholas Bellinger {
191c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
192c66ac9dbSNicholas Bellinger 	int ret;
193c66ac9dbSNicholas Bellinger 
1946708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
195c66ac9dbSNicholas Bellinger 			" %s\n", group, name);
196e7b7af6eSRoland Dreier 
197e7b7af6eSRoland Dreier 	tf = target_core_get_fabric(name);
198e7b7af6eSRoland Dreier 	if (!tf) {
19962554910SNicholas Bellinger 		pr_debug("target_core_register_fabric() trying autoload for %s\n",
200e7b7af6eSRoland Dreier 			 name);
201e7b7af6eSRoland Dreier 
202c66ac9dbSNicholas Bellinger 		/*
203c66ac9dbSNicholas Bellinger 		 * Below are some hardcoded request_module() calls to automatically
204c66ac9dbSNicholas Bellinger 		 * local fabric modules when the following is called:
205c66ac9dbSNicholas Bellinger 		 *
206c66ac9dbSNicholas Bellinger 		 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
207c66ac9dbSNicholas Bellinger 		 *
208c66ac9dbSNicholas Bellinger 		 * Note that this does not limit which TCM fabric module can be
209c66ac9dbSNicholas Bellinger 		 * registered, but simply provids auto loading logic for modules with
210c66ac9dbSNicholas Bellinger 		 * mkdir(2) system calls with known TCM fabric modules.
211c66ac9dbSNicholas Bellinger 		 */
212e7b7af6eSRoland Dreier 
2136708bb27SAndy Grover 		if (!strncmp(name, "iscsi", 5)) {
214c66ac9dbSNicholas Bellinger 			/*
215c66ac9dbSNicholas Bellinger 			 * Automatically load the LIO Target fabric module when the
216c66ac9dbSNicholas Bellinger 			 * following is called:
217c66ac9dbSNicholas Bellinger 			 *
218c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/iscsi
219c66ac9dbSNicholas Bellinger 			 */
220c66ac9dbSNicholas Bellinger 			ret = request_module("iscsi_target_mod");
221c66ac9dbSNicholas Bellinger 			if (ret < 0) {
22262554910SNicholas Bellinger 				pr_debug("request_module() failed for"
223c66ac9dbSNicholas Bellinger 				         " iscsi_target_mod.ko: %d\n", ret);
224c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
225c66ac9dbSNicholas Bellinger 			}
2266708bb27SAndy Grover 		} else if (!strncmp(name, "loopback", 8)) {
227c66ac9dbSNicholas Bellinger 			/*
228c66ac9dbSNicholas Bellinger 			 * Automatically load the tcm_loop fabric module when the
229c66ac9dbSNicholas Bellinger 			 * following is called:
230c66ac9dbSNicholas Bellinger 			 *
231c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/loopback
232c66ac9dbSNicholas Bellinger 			 */
233c66ac9dbSNicholas Bellinger 			ret = request_module("tcm_loop");
234c66ac9dbSNicholas Bellinger 			if (ret < 0) {
23562554910SNicholas Bellinger 				pr_debug("request_module() failed for"
236c66ac9dbSNicholas Bellinger 				         " tcm_loop.ko: %d\n", ret);
237c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
238c66ac9dbSNicholas Bellinger 			}
239c66ac9dbSNicholas Bellinger 		}
240c66ac9dbSNicholas Bellinger 
241c66ac9dbSNicholas Bellinger 		tf = target_core_get_fabric(name);
242e7b7af6eSRoland Dreier 	}
243e7b7af6eSRoland Dreier 
2446708bb27SAndy Grover 	if (!tf) {
24562554910SNicholas Bellinger 		pr_debug("target_core_get_fabric() failed for %s\n",
246c66ac9dbSNicholas Bellinger 		         name);
247c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
248c66ac9dbSNicholas Bellinger 	}
2496708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
2500dc2e8d1SChristoph Hellwig 			" %s\n", tf->tf_ops->name);
251c66ac9dbSNicholas Bellinger 	/*
252c66ac9dbSNicholas Bellinger 	 * On a successful target_core_get_fabric() look, the returned
253c66ac9dbSNicholas Bellinger 	 * struct target_fabric_configfs *tf will contain a usage reference.
254c66ac9dbSNicholas Bellinger 	 */
2556708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
256968ebe75SChristoph Hellwig 			&tf->tf_wwn_cit);
257c66ac9dbSNicholas Bellinger 
258968ebe75SChristoph Hellwig 	config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
2591ae1602dSChristoph Hellwig 
260c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
261968ebe75SChristoph Hellwig 			&tf->tf_discovery_cit);
2621ae1602dSChristoph Hellwig 	configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
263c66ac9dbSNicholas Bellinger 
2646708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
265c66ac9dbSNicholas Bellinger 			" %s\n", tf->tf_group.cg_item.ci_name);
266c66ac9dbSNicholas Bellinger 	return &tf->tf_group;
267c66ac9dbSNicholas Bellinger }
268c66ac9dbSNicholas Bellinger 
269c66ac9dbSNicholas Bellinger /*
270c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->drop_item()
271c66ac9dbSNicholas Bellinger  */
272c66ac9dbSNicholas Bellinger static void target_core_deregister_fabric(
273c66ac9dbSNicholas Bellinger 	struct config_group *group,
274c66ac9dbSNicholas Bellinger 	struct config_item *item)
275c66ac9dbSNicholas Bellinger {
276c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf = container_of(
277c66ac9dbSNicholas Bellinger 		to_config_group(item), struct target_fabric_configfs, tf_group);
278c66ac9dbSNicholas Bellinger 
2796708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
280c66ac9dbSNicholas Bellinger 		" tf list\n", config_item_name(item));
281c66ac9dbSNicholas Bellinger 
2826708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
2830dc2e8d1SChristoph Hellwig 			" %s\n", tf->tf_ops->name);
284c66ac9dbSNicholas Bellinger 	atomic_dec(&tf->tf_access_cnt);
285c66ac9dbSNicholas Bellinger 
2866708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
287c66ac9dbSNicholas Bellinger 			" %s\n", config_item_name(item));
288c66ac9dbSNicholas Bellinger 
2891ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&tf->tf_group);
290c66ac9dbSNicholas Bellinger 	config_item_put(item);
291c66ac9dbSNicholas Bellinger }
292c66ac9dbSNicholas Bellinger 
293c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_fabric_group_ops = {
294c66ac9dbSNicholas Bellinger 	.make_group	= &target_core_register_fabric,
295c66ac9dbSNicholas Bellinger 	.drop_item	= &target_core_deregister_fabric,
296c66ac9dbSNicholas Bellinger };
297c66ac9dbSNicholas Bellinger 
298c66ac9dbSNicholas Bellinger /*
299c66ac9dbSNicholas Bellinger  * All item attributes appearing in /sys/kernel/target/ appear here.
300c66ac9dbSNicholas Bellinger  */
301c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_fabric_item_attrs[] = {
302c66ac9dbSNicholas Bellinger 	&target_core_item_attr_version,
303a96e9783SLee Duncan 	&target_core_item_attr_dbroot,
304c66ac9dbSNicholas Bellinger 	NULL,
305c66ac9dbSNicholas Bellinger };
306c66ac9dbSNicholas Bellinger 
307c66ac9dbSNicholas Bellinger /*
308c66ac9dbSNicholas Bellinger  * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
309c66ac9dbSNicholas Bellinger  */
310c66ac9dbSNicholas Bellinger static struct config_item_type target_core_fabrics_item = {
311c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_fabric_group_ops,
312c66ac9dbSNicholas Bellinger 	.ct_attrs	= target_core_fabric_item_attrs,
313c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
314c66ac9dbSNicholas Bellinger };
315c66ac9dbSNicholas Bellinger 
316c66ac9dbSNicholas Bellinger static struct configfs_subsystem target_core_fabrics = {
317c66ac9dbSNicholas Bellinger 	.su_group = {
318c66ac9dbSNicholas Bellinger 		.cg_item = {
319c66ac9dbSNicholas Bellinger 			.ci_namebuf = "target",
320c66ac9dbSNicholas Bellinger 			.ci_type = &target_core_fabrics_item,
321c66ac9dbSNicholas Bellinger 		},
322c66ac9dbSNicholas Bellinger 	},
323c66ac9dbSNicholas Bellinger };
324c66ac9dbSNicholas Bellinger 
325d588cf8fSChristoph Hellwig int target_depend_item(struct config_item *item)
326d588cf8fSChristoph Hellwig {
327d588cf8fSChristoph Hellwig 	return configfs_depend_item(&target_core_fabrics, item);
328d588cf8fSChristoph Hellwig }
329d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_depend_item);
330d588cf8fSChristoph Hellwig 
331d588cf8fSChristoph Hellwig void target_undepend_item(struct config_item *item)
332d588cf8fSChristoph Hellwig {
3339a9e3415SKrzysztof Opasiak 	return configfs_undepend_item(item);
334d588cf8fSChristoph Hellwig }
335d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_undepend_item);
336c66ac9dbSNicholas Bellinger 
337c66ac9dbSNicholas Bellinger /*##############################################################################
338c66ac9dbSNicholas Bellinger // Start functions called by external Target Fabrics Modules
339c66ac9dbSNicholas Bellinger //############################################################################*/
340c66ac9dbSNicholas Bellinger 
3419ac8928eSChristoph Hellwig static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
342c66ac9dbSNicholas Bellinger {
3439ac8928eSChristoph Hellwig 	if (!tfo->name) {
3449ac8928eSChristoph Hellwig 		pr_err("Missing tfo->name\n");
3459ac8928eSChristoph Hellwig 		return -EINVAL;
346c66ac9dbSNicholas Bellinger 	}
3479ac8928eSChristoph Hellwig 	if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
3486708bb27SAndy Grover 		pr_err("Passed name: %s exceeds TARGET_FABRIC"
3499ac8928eSChristoph Hellwig 			"_NAME_SIZE\n", tfo->name);
3509ac8928eSChristoph Hellwig 		return -EINVAL;
351c66ac9dbSNicholas Bellinger 	}
3526708bb27SAndy Grover 	if (!tfo->get_fabric_name) {
3536708bb27SAndy Grover 		pr_err("Missing tfo->get_fabric_name()\n");
354c66ac9dbSNicholas Bellinger 		return -EINVAL;
355c66ac9dbSNicholas Bellinger 	}
3566708bb27SAndy Grover 	if (!tfo->tpg_get_wwn) {
3576708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_wwn()\n");
358c66ac9dbSNicholas Bellinger 		return -EINVAL;
359c66ac9dbSNicholas Bellinger 	}
3606708bb27SAndy Grover 	if (!tfo->tpg_get_tag) {
3616708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_tag()\n");
362c66ac9dbSNicholas Bellinger 		return -EINVAL;
363c66ac9dbSNicholas Bellinger 	}
3646708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode) {
3656708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode()\n");
366c66ac9dbSNicholas Bellinger 		return -EINVAL;
367c66ac9dbSNicholas Bellinger 	}
3686708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_cache) {
3696708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
370c66ac9dbSNicholas Bellinger 		return -EINVAL;
371c66ac9dbSNicholas Bellinger 	}
3726708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_write_protect) {
3736708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
374c66ac9dbSNicholas Bellinger 		return -EINVAL;
375c66ac9dbSNicholas Bellinger 	}
3766708bb27SAndy Grover 	if (!tfo->tpg_check_prod_mode_write_protect) {
3776708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
378c66ac9dbSNicholas Bellinger 		return -EINVAL;
379c66ac9dbSNicholas Bellinger 	}
3806708bb27SAndy Grover 	if (!tfo->tpg_get_inst_index) {
3816708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_inst_index()\n");
382c66ac9dbSNicholas Bellinger 		return -EINVAL;
383c66ac9dbSNicholas Bellinger 	}
38435462975SChristoph Hellwig 	if (!tfo->release_cmd) {
3856708bb27SAndy Grover 		pr_err("Missing tfo->release_cmd()\n");
386c66ac9dbSNicholas Bellinger 		return -EINVAL;
387c66ac9dbSNicholas Bellinger 	}
3886708bb27SAndy Grover 	if (!tfo->sess_get_index) {
3896708bb27SAndy Grover 		pr_err("Missing tfo->sess_get_index()\n");
390c66ac9dbSNicholas Bellinger 		return -EINVAL;
391c66ac9dbSNicholas Bellinger 	}
3926708bb27SAndy Grover 	if (!tfo->write_pending) {
3936708bb27SAndy Grover 		pr_err("Missing tfo->write_pending()\n");
394c66ac9dbSNicholas Bellinger 		return -EINVAL;
395c66ac9dbSNicholas Bellinger 	}
3966708bb27SAndy Grover 	if (!tfo->write_pending_status) {
3976708bb27SAndy Grover 		pr_err("Missing tfo->write_pending_status()\n");
398c66ac9dbSNicholas Bellinger 		return -EINVAL;
399c66ac9dbSNicholas Bellinger 	}
4006708bb27SAndy Grover 	if (!tfo->set_default_node_attributes) {
4016708bb27SAndy Grover 		pr_err("Missing tfo->set_default_node_attributes()\n");
402c66ac9dbSNicholas Bellinger 		return -EINVAL;
403c66ac9dbSNicholas Bellinger 	}
4046708bb27SAndy Grover 	if (!tfo->get_cmd_state) {
4056708bb27SAndy Grover 		pr_err("Missing tfo->get_cmd_state()\n");
406c66ac9dbSNicholas Bellinger 		return -EINVAL;
407c66ac9dbSNicholas Bellinger 	}
4086708bb27SAndy Grover 	if (!tfo->queue_data_in) {
4096708bb27SAndy Grover 		pr_err("Missing tfo->queue_data_in()\n");
410c66ac9dbSNicholas Bellinger 		return -EINVAL;
411c66ac9dbSNicholas Bellinger 	}
4126708bb27SAndy Grover 	if (!tfo->queue_status) {
4136708bb27SAndy Grover 		pr_err("Missing tfo->queue_status()\n");
414c66ac9dbSNicholas Bellinger 		return -EINVAL;
415c66ac9dbSNicholas Bellinger 	}
4166708bb27SAndy Grover 	if (!tfo->queue_tm_rsp) {
4176708bb27SAndy Grover 		pr_err("Missing tfo->queue_tm_rsp()\n");
418c66ac9dbSNicholas Bellinger 		return -EINVAL;
419c66ac9dbSNicholas Bellinger 	}
420131e6abcSNicholas Bellinger 	if (!tfo->aborted_task) {
421131e6abcSNicholas Bellinger 		pr_err("Missing tfo->aborted_task()\n");
422131e6abcSNicholas Bellinger 		return -EINVAL;
423131e6abcSNicholas Bellinger 	}
4249c28ca4fSNicholas Bellinger 	if (!tfo->check_stop_free) {
4259c28ca4fSNicholas Bellinger 		pr_err("Missing tfo->check_stop_free()\n");
4269c28ca4fSNicholas Bellinger 		return -EINVAL;
4279c28ca4fSNicholas Bellinger 	}
428c66ac9dbSNicholas Bellinger 	/*
429c66ac9dbSNicholas Bellinger 	 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
430c66ac9dbSNicholas Bellinger 	 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
431c66ac9dbSNicholas Bellinger 	 * target_core_fabric_configfs.c WWN+TPG group context code.
432c66ac9dbSNicholas Bellinger 	 */
4336708bb27SAndy Grover 	if (!tfo->fabric_make_wwn) {
4346708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_wwn()\n");
435c66ac9dbSNicholas Bellinger 		return -EINVAL;
436c66ac9dbSNicholas Bellinger 	}
4376708bb27SAndy Grover 	if (!tfo->fabric_drop_wwn) {
4386708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_wwn()\n");
439c66ac9dbSNicholas Bellinger 		return -EINVAL;
440c66ac9dbSNicholas Bellinger 	}
4416708bb27SAndy Grover 	if (!tfo->fabric_make_tpg) {
4426708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_tpg()\n");
443c66ac9dbSNicholas Bellinger 		return -EINVAL;
444c66ac9dbSNicholas Bellinger 	}
4456708bb27SAndy Grover 	if (!tfo->fabric_drop_tpg) {
4466708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_tpg()\n");
447c66ac9dbSNicholas Bellinger 		return -EINVAL;
448c66ac9dbSNicholas Bellinger 	}
449c66ac9dbSNicholas Bellinger 
450c66ac9dbSNicholas Bellinger 	return 0;
451c66ac9dbSNicholas Bellinger }
452c66ac9dbSNicholas Bellinger 
4539ac8928eSChristoph Hellwig int target_register_template(const struct target_core_fabric_ops *fo)
454c66ac9dbSNicholas Bellinger {
4559ac8928eSChristoph Hellwig 	struct target_fabric_configfs *tf;
456c66ac9dbSNicholas Bellinger 	int ret;
457c66ac9dbSNicholas Bellinger 
4589ac8928eSChristoph Hellwig 	ret = target_fabric_tf_ops_check(fo);
4599ac8928eSChristoph Hellwig 	if (ret)
460c66ac9dbSNicholas Bellinger 		return ret;
461c66ac9dbSNicholas Bellinger 
4629ac8928eSChristoph Hellwig 	tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
4639ac8928eSChristoph Hellwig 	if (!tf) {
4649ac8928eSChristoph Hellwig 		pr_err("%s: could not allocate memory!\n", __func__);
4659ac8928eSChristoph Hellwig 		return -ENOMEM;
4669ac8928eSChristoph Hellwig 	}
4679ac8928eSChristoph Hellwig 
4689ac8928eSChristoph Hellwig 	INIT_LIST_HEAD(&tf->tf_list);
4699ac8928eSChristoph Hellwig 	atomic_set(&tf->tf_access_cnt, 0);
470ef0caf8dSChristoph Hellwig 	tf->tf_ops = fo;
4719ac8928eSChristoph Hellwig 	target_fabric_setup_cits(tf);
4729ac8928eSChristoph Hellwig 
4739ac8928eSChristoph Hellwig 	mutex_lock(&g_tf_lock);
4749ac8928eSChristoph Hellwig 	list_add_tail(&tf->tf_list, &g_tf_list);
4759ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4769ac8928eSChristoph Hellwig 
477c66ac9dbSNicholas Bellinger 	return 0;
478c66ac9dbSNicholas Bellinger }
4799ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_register_template);
480c66ac9dbSNicholas Bellinger 
4819ac8928eSChristoph Hellwig void target_unregister_template(const struct target_core_fabric_ops *fo)
482c66ac9dbSNicholas Bellinger {
4839ac8928eSChristoph Hellwig 	struct target_fabric_configfs *t;
484c66ac9dbSNicholas Bellinger 
485c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
4869ac8928eSChristoph Hellwig 	list_for_each_entry(t, &g_tf_list, tf_list) {
4870dc2e8d1SChristoph Hellwig 		if (!strcmp(t->tf_ops->name, fo->name)) {
4889ac8928eSChristoph Hellwig 			BUG_ON(atomic_read(&t->tf_access_cnt));
4899ac8928eSChristoph Hellwig 			list_del(&t->tf_list);
49094509182SNicholas Bellinger 			mutex_unlock(&g_tf_lock);
49194509182SNicholas Bellinger 			/*
49294509182SNicholas Bellinger 			 * Wait for any outstanding fabric se_deve_entry->rcu_head
49394509182SNicholas Bellinger 			 * callbacks to complete post kfree_rcu(), before allowing
49494509182SNicholas Bellinger 			 * fabric driver unload of TFO->module to proceed.
49594509182SNicholas Bellinger 			 */
49694509182SNicholas Bellinger 			rcu_barrier();
4979ac8928eSChristoph Hellwig 			kfree(t);
49894509182SNicholas Bellinger 			return;
499c66ac9dbSNicholas Bellinger 		}
500c66ac9dbSNicholas Bellinger 	}
5019ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
5029ac8928eSChristoph Hellwig }
5039ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_unregister_template);
504c66ac9dbSNicholas Bellinger 
505c66ac9dbSNicholas Bellinger /*##############################################################################
506c66ac9dbSNicholas Bellinger // Stop functions called by external Target Fabrics Modules
507c66ac9dbSNicholas Bellinger //############################################################################*/
508c66ac9dbSNicholas Bellinger 
5092eafd729SChristoph Hellwig static inline struct se_dev_attrib *to_attrib(struct config_item *item)
5102eafd729SChristoph Hellwig {
5112eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_dev_attrib,
5122eafd729SChristoph Hellwig 			da_group);
5135873c4d1SChristoph Hellwig }
5145873c4d1SChristoph Hellwig 
5152eafd729SChristoph Hellwig /* Start functions for struct config_item_type tb_dev_attrib_cit */
5162eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_SHOW(_name)					\
5172eafd729SChristoph Hellwig static ssize_t _name##_show(struct config_item *item, char *page)	\
5182eafd729SChristoph Hellwig {									\
5192eafd729SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
5202eafd729SChristoph Hellwig }
5213effdb90SChristoph Hellwig 
5222eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
5232eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
5242eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
5252eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
5262eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
5272eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
5282eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
5292eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
5302eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
5312eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
5322eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
5332eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
5342eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
5352eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_format);
536056e8924SDmitry Monakhov DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_verify);
5372eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
5382eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
5392eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
5402eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
5412eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
5422eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(block_size);
5432eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
5442eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
5452eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
5462eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
5472eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
5482eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
5492eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
5502eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
551e6f41633SJamie Pocas DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
5522eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
5532eafd729SChristoph Hellwig 
5542eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_U32(_name)				\
5552eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
5565873c4d1SChristoph Hellwig 		size_t count)						\
5575873c4d1SChristoph Hellwig {									\
5582eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5593effdb90SChristoph Hellwig 	u32 val;							\
5605873c4d1SChristoph Hellwig 	int ret;							\
5615873c4d1SChristoph Hellwig 									\
5623effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);					\
5633effdb90SChristoph Hellwig 	if (ret < 0)							\
5643effdb90SChristoph Hellwig 		return ret;						\
5653effdb90SChristoph Hellwig 	da->_name = val;						\
5663effdb90SChristoph Hellwig 	return count;							\
5675873c4d1SChristoph Hellwig }
5685873c4d1SChristoph Hellwig 
5692eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
5702eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
5712eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
5722eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
5732eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
5745873c4d1SChristoph Hellwig 
5752eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name)				\
5762eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,	\
5773effdb90SChristoph Hellwig 		size_t count)						\
5783effdb90SChristoph Hellwig {									\
5792eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5803effdb90SChristoph Hellwig 	bool flag;							\
5813effdb90SChristoph Hellwig 	int ret;							\
5823effdb90SChristoph Hellwig 									\
5833effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);					\
5843effdb90SChristoph Hellwig 	if (ret < 0)							\
5853effdb90SChristoph Hellwig 		return ret;						\
5863effdb90SChristoph Hellwig 	da->_name = flag;						\
5873effdb90SChristoph Hellwig 	return count;							\
5883effdb90SChristoph Hellwig }
5893effdb90SChristoph Hellwig 
5902eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
5912eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
5922eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
5932eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
5942eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
5953effdb90SChristoph Hellwig 
5962eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name)				\
5972eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
5983effdb90SChristoph Hellwig 		size_t count)						\
5993effdb90SChristoph Hellwig {									\
6003effdb90SChristoph Hellwig 	printk_once(KERN_WARNING					\
601234bdbc4SChristophe Vu-Brugier 		"ignoring deprecated %s attribute\n",			\
602234bdbc4SChristophe Vu-Brugier 		__stringify(_name));					\
6033effdb90SChristoph Hellwig 	return count;							\
6043effdb90SChristoph Hellwig }
6053effdb90SChristoph Hellwig 
6062eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
6072eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
6083effdb90SChristoph Hellwig 
6093effdb90SChristoph Hellwig static void dev_set_t10_wwn_model_alias(struct se_device *dev)
6103effdb90SChristoph Hellwig {
6113effdb90SChristoph Hellwig 	const char *configname;
6123effdb90SChristoph Hellwig 
6133effdb90SChristoph Hellwig 	configname = config_item_name(&dev->dev_group.cg_item);
6143effdb90SChristoph Hellwig 	if (strlen(configname) >= 16) {
6153effdb90SChristoph Hellwig 		pr_warn("dev[%p]: Backstore name '%s' is too long for "
6163effdb90SChristoph Hellwig 			"INQUIRY_MODEL, truncating to 16 bytes\n", dev,
6173effdb90SChristoph Hellwig 			configname);
6183effdb90SChristoph Hellwig 	}
6193effdb90SChristoph Hellwig 	snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
6203effdb90SChristoph Hellwig }
6213effdb90SChristoph Hellwig 
6222eafd729SChristoph Hellwig static ssize_t emulate_model_alias_store(struct config_item *item,
6233effdb90SChristoph Hellwig 		const char *page, size_t count)
6243effdb90SChristoph Hellwig {
6252eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6263effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
6273effdb90SChristoph Hellwig 	bool flag;
6283effdb90SChristoph Hellwig 	int ret;
6293effdb90SChristoph Hellwig 
6303effdb90SChristoph Hellwig 	if (dev->export_count) {
6313effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change model alias"
6323effdb90SChristoph Hellwig 			" while export_count is %d\n",
6333effdb90SChristoph Hellwig 			dev, dev->export_count);
6343effdb90SChristoph Hellwig 		return -EINVAL;
6353effdb90SChristoph Hellwig 	}
6363effdb90SChristoph Hellwig 
6373effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6383effdb90SChristoph Hellwig 	if (ret < 0)
6393effdb90SChristoph Hellwig 		return ret;
6403effdb90SChristoph Hellwig 
6413effdb90SChristoph Hellwig 	if (flag) {
6423effdb90SChristoph Hellwig 		dev_set_t10_wwn_model_alias(dev);
6433effdb90SChristoph Hellwig 	} else {
6443effdb90SChristoph Hellwig 		strncpy(&dev->t10_wwn.model[0],
6453effdb90SChristoph Hellwig 			dev->transport->inquiry_prod, 16);
6463effdb90SChristoph Hellwig 	}
6473effdb90SChristoph Hellwig 	da->emulate_model_alias = flag;
6483effdb90SChristoph Hellwig 	return count;
6493effdb90SChristoph Hellwig }
6503effdb90SChristoph Hellwig 
6512eafd729SChristoph Hellwig static ssize_t emulate_write_cache_store(struct config_item *item,
6523effdb90SChristoph Hellwig 		const char *page, size_t count)
6533effdb90SChristoph Hellwig {
6542eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6553effdb90SChristoph Hellwig 	bool flag;
6563effdb90SChristoph Hellwig 	int ret;
6573effdb90SChristoph Hellwig 
6583effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6593effdb90SChristoph Hellwig 	if (ret < 0)
6603effdb90SChristoph Hellwig 		return ret;
6613effdb90SChristoph Hellwig 
6623effdb90SChristoph Hellwig 	if (flag && da->da_dev->transport->get_write_cache) {
6633effdb90SChristoph Hellwig 		pr_err("emulate_write_cache not supported for this device\n");
6643effdb90SChristoph Hellwig 		return -EINVAL;
6653effdb90SChristoph Hellwig 	}
6663effdb90SChristoph Hellwig 
6673effdb90SChristoph Hellwig 	da->emulate_write_cache = flag;
6683effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
6693effdb90SChristoph Hellwig 			da->da_dev, flag);
6703effdb90SChristoph Hellwig 	return count;
6713effdb90SChristoph Hellwig }
6723effdb90SChristoph Hellwig 
6732eafd729SChristoph Hellwig static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
6743effdb90SChristoph Hellwig 		const char *page, size_t count)
6753effdb90SChristoph Hellwig {
6762eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6773effdb90SChristoph Hellwig 	u32 val;
6783effdb90SChristoph Hellwig 	int ret;
6793effdb90SChristoph Hellwig 
6803effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
6813effdb90SChristoph Hellwig 	if (ret < 0)
6823effdb90SChristoph Hellwig 		return ret;
6833effdb90SChristoph Hellwig 
6843effdb90SChristoph Hellwig 	if (val != 0 && val != 1 && val != 2) {
6853effdb90SChristoph Hellwig 		pr_err("Illegal value %d\n", val);
6863effdb90SChristoph Hellwig 		return -EINVAL;
6873effdb90SChristoph Hellwig 	}
6883effdb90SChristoph Hellwig 
6893effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
6903effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
6913effdb90SChristoph Hellwig 			" UA_INTRLCK_CTRL while export_count is %d\n",
6923effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
6933effdb90SChristoph Hellwig 		return -EINVAL;
6943effdb90SChristoph Hellwig 	}
6953effdb90SChristoph Hellwig 	da->emulate_ua_intlck_ctrl = val;
6963effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
6973effdb90SChristoph Hellwig 		da->da_dev, val);
6983effdb90SChristoph Hellwig 	return count;
6993effdb90SChristoph Hellwig }
7003effdb90SChristoph Hellwig 
7012eafd729SChristoph Hellwig static ssize_t emulate_tas_store(struct config_item *item,
7023effdb90SChristoph Hellwig 		const char *page, size_t count)
7033effdb90SChristoph Hellwig {
7042eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7053effdb90SChristoph Hellwig 	bool flag;
7063effdb90SChristoph Hellwig 	int ret;
7073effdb90SChristoph Hellwig 
7083effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7093effdb90SChristoph Hellwig 	if (ret < 0)
7103effdb90SChristoph Hellwig 		return ret;
7113effdb90SChristoph Hellwig 
7123effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
7133effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TAS while"
7143effdb90SChristoph Hellwig 			" export_count is %d\n",
7153effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
7163effdb90SChristoph Hellwig 		return -EINVAL;
7173effdb90SChristoph Hellwig 	}
7183effdb90SChristoph Hellwig 	da->emulate_tas = flag;
7193effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
7203effdb90SChristoph Hellwig 		da->da_dev, flag ? "Enabled" : "Disabled");
7213effdb90SChristoph Hellwig 
7223effdb90SChristoph Hellwig 	return count;
7233effdb90SChristoph Hellwig }
7243effdb90SChristoph Hellwig 
7252eafd729SChristoph Hellwig static ssize_t emulate_tpu_store(struct config_item *item,
7263effdb90SChristoph Hellwig 		const char *page, size_t count)
7273effdb90SChristoph Hellwig {
7282eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7293effdb90SChristoph Hellwig 	bool flag;
7303effdb90SChristoph Hellwig 	int ret;
7313effdb90SChristoph Hellwig 
7323effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7333effdb90SChristoph Hellwig 	if (ret < 0)
7343effdb90SChristoph Hellwig 		return ret;
7353effdb90SChristoph Hellwig 
7363effdb90SChristoph Hellwig 	/*
7373effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
7383effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
7393effdb90SChristoph Hellwig 	 */
7403effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
7413effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
7423effdb90SChristoph Hellwig 		return -ENOSYS;
7433effdb90SChristoph Hellwig 	}
7443effdb90SChristoph Hellwig 
7453effdb90SChristoph Hellwig 	da->emulate_tpu = flag;
7463effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
7473effdb90SChristoph Hellwig 		da->da_dev, flag);
7483effdb90SChristoph Hellwig 	return count;
7493effdb90SChristoph Hellwig }
7503effdb90SChristoph Hellwig 
7512eafd729SChristoph Hellwig static ssize_t emulate_tpws_store(struct config_item *item,
7523effdb90SChristoph Hellwig 		const char *page, size_t count)
7533effdb90SChristoph Hellwig {
7542eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7553effdb90SChristoph Hellwig 	bool flag;
7563effdb90SChristoph Hellwig 	int ret;
7573effdb90SChristoph Hellwig 
7583effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7593effdb90SChristoph Hellwig 	if (ret < 0)
7603effdb90SChristoph Hellwig 		return ret;
7613effdb90SChristoph Hellwig 
7623effdb90SChristoph Hellwig 	/*
7633effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
7643effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
7653effdb90SChristoph Hellwig 	 */
7663effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
7673effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
7683effdb90SChristoph Hellwig 		return -ENOSYS;
7693effdb90SChristoph Hellwig 	}
7703effdb90SChristoph Hellwig 
7713effdb90SChristoph Hellwig 	da->emulate_tpws = flag;
7723effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
7733effdb90SChristoph Hellwig 				da->da_dev, flag);
7743effdb90SChristoph Hellwig 	return count;
7753effdb90SChristoph Hellwig }
7763effdb90SChristoph Hellwig 
7772eafd729SChristoph Hellwig static ssize_t pi_prot_type_store(struct config_item *item,
7783effdb90SChristoph Hellwig 		const char *page, size_t count)
7793effdb90SChristoph Hellwig {
7802eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7813effdb90SChristoph Hellwig 	int old_prot = da->pi_prot_type, ret;
7823effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
7833effdb90SChristoph Hellwig 	u32 flag;
7843effdb90SChristoph Hellwig 
7853effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &flag);
7863effdb90SChristoph Hellwig 	if (ret < 0)
7873effdb90SChristoph Hellwig 		return ret;
7883effdb90SChristoph Hellwig 
7893effdb90SChristoph Hellwig 	if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
7903effdb90SChristoph Hellwig 		pr_err("Illegal value %d for pi_prot_type\n", flag);
7913effdb90SChristoph Hellwig 		return -EINVAL;
7923effdb90SChristoph Hellwig 	}
7933effdb90SChristoph Hellwig 	if (flag == 2) {
7943effdb90SChristoph Hellwig 		pr_err("DIF TYPE2 protection currently not supported\n");
7953effdb90SChristoph Hellwig 		return -ENOSYS;
7963effdb90SChristoph Hellwig 	}
7973effdb90SChristoph Hellwig 	if (da->hw_pi_prot_type) {
7983effdb90SChristoph Hellwig 		pr_warn("DIF protection enabled on underlying hardware,"
7993effdb90SChristoph Hellwig 			" ignoring\n");
8003effdb90SChristoph Hellwig 		return count;
8013effdb90SChristoph Hellwig 	}
8023effdb90SChristoph Hellwig 	if (!dev->transport->init_prot || !dev->transport->free_prot) {
8033effdb90SChristoph Hellwig 		/* 0 is only allowed value for non-supporting backends */
8043effdb90SChristoph Hellwig 		if (flag == 0)
805bc1a7d6aSAndy Grover 			return count;
8063effdb90SChristoph Hellwig 
8073effdb90SChristoph Hellwig 		pr_err("DIF protection not supported by backend: %s\n",
8083effdb90SChristoph Hellwig 		       dev->transport->name);
8093effdb90SChristoph Hellwig 		return -ENOSYS;
8103effdb90SChristoph Hellwig 	}
8113effdb90SChristoph Hellwig 	if (!(dev->dev_flags & DF_CONFIGURED)) {
8123effdb90SChristoph Hellwig 		pr_err("DIF protection requires device to be configured\n");
8133effdb90SChristoph Hellwig 		return -ENODEV;
8143effdb90SChristoph Hellwig 	}
8153effdb90SChristoph Hellwig 	if (dev->export_count) {
8163effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device PROT type while"
8173effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
8183effdb90SChristoph Hellwig 		return -EINVAL;
8193effdb90SChristoph Hellwig 	}
8203effdb90SChristoph Hellwig 
8213effdb90SChristoph Hellwig 	da->pi_prot_type = flag;
8223effdb90SChristoph Hellwig 
8233effdb90SChristoph Hellwig 	if (flag && !old_prot) {
8243effdb90SChristoph Hellwig 		ret = dev->transport->init_prot(dev);
8253effdb90SChristoph Hellwig 		if (ret) {
8263effdb90SChristoph Hellwig 			da->pi_prot_type = old_prot;
827056e8924SDmitry Monakhov 			da->pi_prot_verify = (bool) da->pi_prot_type;
8283effdb90SChristoph Hellwig 			return ret;
8293effdb90SChristoph Hellwig 		}
8303effdb90SChristoph Hellwig 
8313effdb90SChristoph Hellwig 	} else if (!flag && old_prot) {
8323effdb90SChristoph Hellwig 		dev->transport->free_prot(dev);
8333effdb90SChristoph Hellwig 	}
8343effdb90SChristoph Hellwig 
835056e8924SDmitry Monakhov 	da->pi_prot_verify = (bool) da->pi_prot_type;
8363effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
8373effdb90SChristoph Hellwig 	return count;
8383effdb90SChristoph Hellwig }
8393effdb90SChristoph Hellwig 
8402eafd729SChristoph Hellwig static ssize_t pi_prot_format_store(struct config_item *item,
8413effdb90SChristoph Hellwig 		const char *page, size_t count)
8423effdb90SChristoph Hellwig {
8432eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
8443effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
8453effdb90SChristoph Hellwig 	bool flag;
8463effdb90SChristoph Hellwig 	int ret;
8473effdb90SChristoph Hellwig 
8483effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
8493effdb90SChristoph Hellwig 	if (ret < 0)
8503effdb90SChristoph Hellwig 		return ret;
8513effdb90SChristoph Hellwig 
8523effdb90SChristoph Hellwig 	if (!flag)
8533effdb90SChristoph Hellwig 		return count;
8543effdb90SChristoph Hellwig 
8553effdb90SChristoph Hellwig 	if (!dev->transport->format_prot) {
8563effdb90SChristoph Hellwig 		pr_err("DIF protection format not supported by backend %s\n",
8573effdb90SChristoph Hellwig 		       dev->transport->name);
8583effdb90SChristoph Hellwig 		return -ENOSYS;
8593effdb90SChristoph Hellwig 	}
8603effdb90SChristoph Hellwig 	if (!(dev->dev_flags & DF_CONFIGURED)) {
8613effdb90SChristoph Hellwig 		pr_err("DIF protection format requires device to be configured\n");
8623effdb90SChristoph Hellwig 		return -ENODEV;
8633effdb90SChristoph Hellwig 	}
8643effdb90SChristoph Hellwig 	if (dev->export_count) {
8653effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to format SE Device PROT type while"
8663effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
8673effdb90SChristoph Hellwig 		return -EINVAL;
8683effdb90SChristoph Hellwig 	}
8693effdb90SChristoph Hellwig 
8703effdb90SChristoph Hellwig 	ret = dev->transport->format_prot(dev);
8713effdb90SChristoph Hellwig 	if (ret)
8723effdb90SChristoph Hellwig 		return ret;
8733effdb90SChristoph Hellwig 
8743effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
8753effdb90SChristoph Hellwig 	return count;
8763effdb90SChristoph Hellwig }
8773effdb90SChristoph Hellwig 
878056e8924SDmitry Monakhov static ssize_t pi_prot_verify_store(struct config_item *item,
879056e8924SDmitry Monakhov 		const char *page, size_t count)
880056e8924SDmitry Monakhov {
881056e8924SDmitry Monakhov 	struct se_dev_attrib *da = to_attrib(item);
882056e8924SDmitry Monakhov 	bool flag;
883056e8924SDmitry Monakhov 	int ret;
884056e8924SDmitry Monakhov 
885056e8924SDmitry Monakhov 	ret = strtobool(page, &flag);
886056e8924SDmitry Monakhov 	if (ret < 0)
887056e8924SDmitry Monakhov 		return ret;
888056e8924SDmitry Monakhov 
889056e8924SDmitry Monakhov 	if (!flag) {
890056e8924SDmitry Monakhov 		da->pi_prot_verify = flag;
891056e8924SDmitry Monakhov 		return count;
892056e8924SDmitry Monakhov 	}
893056e8924SDmitry Monakhov 	if (da->hw_pi_prot_type) {
894056e8924SDmitry Monakhov 		pr_warn("DIF protection enabled on underlying hardware,"
895056e8924SDmitry Monakhov 			" ignoring\n");
896056e8924SDmitry Monakhov 		return count;
897056e8924SDmitry Monakhov 	}
898056e8924SDmitry Monakhov 	if (!da->pi_prot_type) {
899056e8924SDmitry Monakhov 		pr_warn("DIF protection not supported by backend, ignoring\n");
900056e8924SDmitry Monakhov 		return count;
901056e8924SDmitry Monakhov 	}
902056e8924SDmitry Monakhov 	da->pi_prot_verify = flag;
903056e8924SDmitry Monakhov 
904056e8924SDmitry Monakhov 	return count;
905056e8924SDmitry Monakhov }
906056e8924SDmitry Monakhov 
9072eafd729SChristoph Hellwig static ssize_t force_pr_aptpl_store(struct config_item *item,
9083effdb90SChristoph Hellwig 		const char *page, size_t count)
9093effdb90SChristoph Hellwig {
9102eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9113effdb90SChristoph Hellwig 	bool flag;
9123effdb90SChristoph Hellwig 	int ret;
9133effdb90SChristoph Hellwig 
9143effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
9153effdb90SChristoph Hellwig 	if (ret < 0)
9163effdb90SChristoph Hellwig 		return ret;
9173effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
9183effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to set force_pr_aptpl while"
9193effdb90SChristoph Hellwig 		       " export_count is %d\n",
9203effdb90SChristoph Hellwig 		       da->da_dev, da->da_dev->export_count);
9213effdb90SChristoph Hellwig 		return -EINVAL;
9223effdb90SChristoph Hellwig 	}
9233effdb90SChristoph Hellwig 
9243effdb90SChristoph Hellwig 	da->force_pr_aptpl = flag;
9253effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
9263effdb90SChristoph Hellwig 	return count;
9273effdb90SChristoph Hellwig }
9283effdb90SChristoph Hellwig 
9292eafd729SChristoph Hellwig static ssize_t emulate_rest_reord_store(struct config_item *item,
9303effdb90SChristoph Hellwig 		const char *page, size_t count)
9313effdb90SChristoph Hellwig {
9322eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9333effdb90SChristoph Hellwig 	bool flag;
9343effdb90SChristoph Hellwig 	int ret;
9353effdb90SChristoph Hellwig 
9363effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
9373effdb90SChristoph Hellwig 	if (ret < 0)
9383effdb90SChristoph Hellwig 		return ret;
9393effdb90SChristoph Hellwig 
9403effdb90SChristoph Hellwig 	if (flag != 0) {
9413effdb90SChristoph Hellwig 		printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
9423effdb90SChristoph Hellwig 			" reordering not implemented\n", da->da_dev);
9433effdb90SChristoph Hellwig 		return -ENOSYS;
9443effdb90SChristoph Hellwig 	}
9453effdb90SChristoph Hellwig 	da->emulate_rest_reord = flag;
9463effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
9473effdb90SChristoph Hellwig 		da->da_dev, flag);
9483effdb90SChristoph Hellwig 	return count;
9493effdb90SChristoph Hellwig }
9503effdb90SChristoph Hellwig 
951e6f41633SJamie Pocas static ssize_t unmap_zeroes_data_store(struct config_item *item,
952e6f41633SJamie Pocas 		const char *page, size_t count)
953e6f41633SJamie Pocas {
954e6f41633SJamie Pocas 	struct se_dev_attrib *da = to_attrib(item);
955e6f41633SJamie Pocas 	bool flag;
956e6f41633SJamie Pocas 	int ret;
957e6f41633SJamie Pocas 
958e6f41633SJamie Pocas 	ret = strtobool(page, &flag);
959e6f41633SJamie Pocas 	if (ret < 0)
960e6f41633SJamie Pocas 		return ret;
961e6f41633SJamie Pocas 
962e6f41633SJamie Pocas 	if (da->da_dev->export_count) {
963e6f41633SJamie Pocas 		pr_err("dev[%p]: Unable to change SE Device"
964e6f41633SJamie Pocas 		       " unmap_zeroes_data while export_count is %d\n",
965e6f41633SJamie Pocas 		       da->da_dev, da->da_dev->export_count);
966e6f41633SJamie Pocas 		return -EINVAL;
967e6f41633SJamie Pocas 	}
968e6f41633SJamie Pocas 	/*
969e6f41633SJamie Pocas 	 * We expect this value to be non-zero when generic Block Layer
970e6f41633SJamie Pocas 	 * Discard supported is detected iblock_configure_device().
971e6f41633SJamie Pocas 	 */
972e6f41633SJamie Pocas 	if (flag && !da->max_unmap_block_desc_count) {
973e6f41633SJamie Pocas 		pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
974e6f41633SJamie Pocas 		       " because max_unmap_block_desc_count is zero\n",
975e6f41633SJamie Pocas 		       da->da_dev);
976e6f41633SJamie Pocas 		return -ENOSYS;
977e6f41633SJamie Pocas 	}
978e6f41633SJamie Pocas 	da->unmap_zeroes_data = flag;
979e6f41633SJamie Pocas 	pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
980e6f41633SJamie Pocas 		 da->da_dev, flag);
9812e498f25SNicholas Bellinger 	return count;
982e6f41633SJamie Pocas }
983e6f41633SJamie Pocas 
9843effdb90SChristoph Hellwig /*
9853effdb90SChristoph Hellwig  * Note, this can only be called on unexported SE Device Object.
9863effdb90SChristoph Hellwig  */
9872eafd729SChristoph Hellwig static ssize_t queue_depth_store(struct config_item *item,
9883effdb90SChristoph Hellwig 		const char *page, size_t count)
9893effdb90SChristoph Hellwig {
9902eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9913effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
9923effdb90SChristoph Hellwig 	u32 val;
9933effdb90SChristoph Hellwig 	int ret;
9943effdb90SChristoph Hellwig 
9953effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
9963effdb90SChristoph Hellwig 	if (ret < 0)
9973effdb90SChristoph Hellwig 		return ret;
9983effdb90SChristoph Hellwig 
9993effdb90SChristoph Hellwig 	if (dev->export_count) {
10003effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TCQ while"
10013effdb90SChristoph Hellwig 			" export_count is %d\n",
10023effdb90SChristoph Hellwig 			dev, dev->export_count);
10033effdb90SChristoph Hellwig 		return -EINVAL;
10043effdb90SChristoph Hellwig 	}
10053effdb90SChristoph Hellwig 	if (!val) {
10063effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
10073effdb90SChristoph Hellwig 		return -EINVAL;
10083effdb90SChristoph Hellwig 	}
10093effdb90SChristoph Hellwig 
10103effdb90SChristoph Hellwig 	if (val > dev->dev_attrib.queue_depth) {
10113effdb90SChristoph Hellwig 		if (val > dev->dev_attrib.hw_queue_depth) {
10123effdb90SChristoph Hellwig 			pr_err("dev[%p]: Passed queue_depth:"
10133effdb90SChristoph Hellwig 				" %u exceeds TCM/SE_Device MAX"
10143effdb90SChristoph Hellwig 				" TCQ: %u\n", dev, val,
10153effdb90SChristoph Hellwig 				dev->dev_attrib.hw_queue_depth);
10163effdb90SChristoph Hellwig 			return -EINVAL;
10173effdb90SChristoph Hellwig 		}
10183effdb90SChristoph Hellwig 	}
10193effdb90SChristoph Hellwig 	da->queue_depth = dev->queue_depth = val;
10203effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
10213effdb90SChristoph Hellwig 	return count;
10223effdb90SChristoph Hellwig }
10233effdb90SChristoph Hellwig 
10242eafd729SChristoph Hellwig static ssize_t optimal_sectors_store(struct config_item *item,
10253effdb90SChristoph Hellwig 		const char *page, size_t count)
10263effdb90SChristoph Hellwig {
10272eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
10283effdb90SChristoph Hellwig 	u32 val;
10293effdb90SChristoph Hellwig 	int ret;
10303effdb90SChristoph Hellwig 
10313effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
10323effdb90SChristoph Hellwig 	if (ret < 0)
10333effdb90SChristoph Hellwig 		return ret;
10343effdb90SChristoph Hellwig 
10353effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
10363effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
10373effdb90SChristoph Hellwig 			" optimal_sectors while export_count is %d\n",
10383effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
10393effdb90SChristoph Hellwig 		return -EINVAL;
10403effdb90SChristoph Hellwig 	}
10413effdb90SChristoph Hellwig 	if (val > da->hw_max_sectors) {
10423effdb90SChristoph Hellwig 		pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
10433effdb90SChristoph Hellwig 			" greater than hw_max_sectors: %u\n",
10443effdb90SChristoph Hellwig 			da->da_dev, val, da->hw_max_sectors);
10453effdb90SChristoph Hellwig 		return -EINVAL;
10463effdb90SChristoph Hellwig 	}
10473effdb90SChristoph Hellwig 
10483effdb90SChristoph Hellwig 	da->optimal_sectors = val;
10493effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
10503effdb90SChristoph Hellwig 			da->da_dev, val);
10513effdb90SChristoph Hellwig 	return count;
10523effdb90SChristoph Hellwig }
10533effdb90SChristoph Hellwig 
10542eafd729SChristoph Hellwig static ssize_t block_size_store(struct config_item *item,
10553effdb90SChristoph Hellwig 		const char *page, size_t count)
10563effdb90SChristoph Hellwig {
10572eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
10583effdb90SChristoph Hellwig 	u32 val;
10593effdb90SChristoph Hellwig 	int ret;
10603effdb90SChristoph Hellwig 
10613effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
10623effdb90SChristoph Hellwig 	if (ret < 0)
10633effdb90SChristoph Hellwig 		return ret;
10643effdb90SChristoph Hellwig 
10653effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
10663effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device block_size"
10673effdb90SChristoph Hellwig 			" while export_count is %d\n",
10683effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
10693effdb90SChristoph Hellwig 		return -EINVAL;
10703effdb90SChristoph Hellwig 	}
10713effdb90SChristoph Hellwig 
10723effdb90SChristoph Hellwig 	if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
10733effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal value for block_device: %u"
10743effdb90SChristoph Hellwig 			" for SE device, must be 512, 1024, 2048 or 4096\n",
10753effdb90SChristoph Hellwig 			da->da_dev, val);
10763effdb90SChristoph Hellwig 		return -EINVAL;
10773effdb90SChristoph Hellwig 	}
10783effdb90SChristoph Hellwig 
10793effdb90SChristoph Hellwig 	da->block_size = val;
10803effdb90SChristoph Hellwig 	if (da->max_bytes_per_io)
10813effdb90SChristoph Hellwig 		da->hw_max_sectors = da->max_bytes_per_io / val;
10823effdb90SChristoph Hellwig 
10833effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device block_size changed to %u\n",
10843effdb90SChristoph Hellwig 			da->da_dev, val);
10853effdb90SChristoph Hellwig 	return count;
10863effdb90SChristoph Hellwig }
10875873c4d1SChristoph Hellwig 
10882eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_model_alias);
10892eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_dpo);
10902eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_write);
10912eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_read);
10922eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_write_cache);
10932eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
10942eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tas);
10952eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpu);
10962eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpws);
10972eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_caw);
10982eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_3pc);
10992eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_type);
11002eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_pi_prot_type);
11012eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_format);
1102056e8924SDmitry Monakhov CONFIGFS_ATTR(, pi_prot_verify);
11032eafd729SChristoph Hellwig CONFIGFS_ATTR(, enforce_pr_isids);
11042eafd729SChristoph Hellwig CONFIGFS_ATTR(, is_nonrot);
11052eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_rest_reord);
11062eafd729SChristoph Hellwig CONFIGFS_ATTR(, force_pr_aptpl);
11072eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_block_size);
11082eafd729SChristoph Hellwig CONFIGFS_ATTR(, block_size);
11092eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_max_sectors);
11102eafd729SChristoph Hellwig CONFIGFS_ATTR(, optimal_sectors);
11112eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_queue_depth);
11122eafd729SChristoph Hellwig CONFIGFS_ATTR(, queue_depth);
11132eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_lba_count);
11142eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_block_desc_count);
11152eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity);
11162eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity_alignment);
1117e6f41633SJamie Pocas CONFIGFS_ATTR(, unmap_zeroes_data);
11182eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_write_same_len);
1119c66ac9dbSNicholas Bellinger 
11205873c4d1SChristoph Hellwig /*
11215873c4d1SChristoph Hellwig  * dev_attrib attributes for devices using the target core SBC/SPC
11225873c4d1SChristoph Hellwig  * interpreter.  Any backend using spc_parse_cdb should be using
11235873c4d1SChristoph Hellwig  * these.
11245873c4d1SChristoph Hellwig  */
11255873c4d1SChristoph Hellwig struct configfs_attribute *sbc_attrib_attrs[] = {
11262eafd729SChristoph Hellwig 	&attr_emulate_model_alias,
11272eafd729SChristoph Hellwig 	&attr_emulate_dpo,
11282eafd729SChristoph Hellwig 	&attr_emulate_fua_write,
11292eafd729SChristoph Hellwig 	&attr_emulate_fua_read,
11302eafd729SChristoph Hellwig 	&attr_emulate_write_cache,
11312eafd729SChristoph Hellwig 	&attr_emulate_ua_intlck_ctrl,
11322eafd729SChristoph Hellwig 	&attr_emulate_tas,
11332eafd729SChristoph Hellwig 	&attr_emulate_tpu,
11342eafd729SChristoph Hellwig 	&attr_emulate_tpws,
11352eafd729SChristoph Hellwig 	&attr_emulate_caw,
11362eafd729SChristoph Hellwig 	&attr_emulate_3pc,
11372eafd729SChristoph Hellwig 	&attr_pi_prot_type,
11382eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
11392eafd729SChristoph Hellwig 	&attr_pi_prot_format,
1140056e8924SDmitry Monakhov 	&attr_pi_prot_verify,
11412eafd729SChristoph Hellwig 	&attr_enforce_pr_isids,
11422eafd729SChristoph Hellwig 	&attr_is_nonrot,
11432eafd729SChristoph Hellwig 	&attr_emulate_rest_reord,
11442eafd729SChristoph Hellwig 	&attr_force_pr_aptpl,
11452eafd729SChristoph Hellwig 	&attr_hw_block_size,
11462eafd729SChristoph Hellwig 	&attr_block_size,
11472eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
11482eafd729SChristoph Hellwig 	&attr_optimal_sectors,
11492eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
11502eafd729SChristoph Hellwig 	&attr_queue_depth,
11512eafd729SChristoph Hellwig 	&attr_max_unmap_lba_count,
11522eafd729SChristoph Hellwig 	&attr_max_unmap_block_desc_count,
11532eafd729SChristoph Hellwig 	&attr_unmap_granularity,
11542eafd729SChristoph Hellwig 	&attr_unmap_granularity_alignment,
1155e6f41633SJamie Pocas 	&attr_unmap_zeroes_data,
11562eafd729SChristoph Hellwig 	&attr_max_write_same_len,
11575873c4d1SChristoph Hellwig 	NULL,
11585873c4d1SChristoph Hellwig };
11595873c4d1SChristoph Hellwig EXPORT_SYMBOL(sbc_attrib_attrs);
11605873c4d1SChristoph Hellwig 
11615873c4d1SChristoph Hellwig /*
11625873c4d1SChristoph Hellwig  * Minimal dev_attrib attributes for devices passing through CDBs.
11635873c4d1SChristoph Hellwig  * In this case we only provide a few read-only attributes for
11645873c4d1SChristoph Hellwig  * backwards compatibility.
11655873c4d1SChristoph Hellwig  */
11665873c4d1SChristoph Hellwig struct configfs_attribute *passthrough_attrib_attrs[] = {
11672eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
11682eafd729SChristoph Hellwig 	&attr_hw_block_size,
11692eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
11702eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
11715873c4d1SChristoph Hellwig 	NULL,
11725873c4d1SChristoph Hellwig };
11735873c4d1SChristoph Hellwig EXPORT_SYMBOL(passthrough_attrib_attrs);
11745873c4d1SChristoph Hellwig 
11752eafd729SChristoph Hellwig TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
1176c66ac9dbSNicholas Bellinger 
1177f79a897eSNicholas Bellinger /* End functions for struct config_item_type tb_dev_attrib_cit */
1178c66ac9dbSNicholas Bellinger 
1179f8d389c6SNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_wwn_cit */
1180c66ac9dbSNicholas Bellinger 
11812eafd729SChristoph Hellwig static struct t10_wwn *to_t10_wwn(struct config_item *item)
11822eafd729SChristoph Hellwig {
11832eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
11842eafd729SChristoph Hellwig }
1185c66ac9dbSNicholas Bellinger 
1186c66ac9dbSNicholas Bellinger /*
1187c66ac9dbSNicholas Bellinger  * VPD page 0x80 Unit serial
1188c66ac9dbSNicholas Bellinger  */
11892eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1190c66ac9dbSNicholas Bellinger 		char *page)
1191c66ac9dbSNicholas Bellinger {
1192c66ac9dbSNicholas Bellinger 	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
11932eafd729SChristoph Hellwig 		&to_t10_wwn(item)->unit_serial[0]);
1194c66ac9dbSNicholas Bellinger }
1195c66ac9dbSNicholas Bellinger 
11962eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
11972eafd729SChristoph Hellwig 		const char *page, size_t count)
1198c66ac9dbSNicholas Bellinger {
11992eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
12000fd97ccfSChristoph Hellwig 	struct se_device *dev = t10_wwn->t10_dev;
1201c66ac9dbSNicholas Bellinger 	unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1202c66ac9dbSNicholas Bellinger 
1203c66ac9dbSNicholas Bellinger 	/*
1204c66ac9dbSNicholas Bellinger 	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1205c66ac9dbSNicholas Bellinger 	 * from the struct scsi_device level firmware, do not allow
1206c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial to be emulated.
1207c66ac9dbSNicholas Bellinger 	 *
1208c66ac9dbSNicholas Bellinger 	 * Note this struct scsi_device could also be emulating VPD
1209c66ac9dbSNicholas Bellinger 	 * information from its drivers/scsi LLD.  But for now we assume
1210c66ac9dbSNicholas Bellinger 	 * it is doing 'the right thing' wrt a world wide unique
1211c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial Number that OS dependent multipath can depend on.
1212c66ac9dbSNicholas Bellinger 	 */
12130fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
12146708bb27SAndy Grover 		pr_err("Underlying SCSI device firmware provided VPD"
1215c66ac9dbSNicholas Bellinger 			" Unit Serial, ignoring request\n");
1216c66ac9dbSNicholas Bellinger 		return -EOPNOTSUPP;
1217c66ac9dbSNicholas Bellinger 	}
1218c66ac9dbSNicholas Bellinger 
121960d645a4SDan Carpenter 	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
12206708bb27SAndy Grover 		pr_err("Emulated VPD Unit Serial exceeds"
1221c66ac9dbSNicholas Bellinger 		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1222c66ac9dbSNicholas Bellinger 		return -EOVERFLOW;
1223c66ac9dbSNicholas Bellinger 	}
1224c66ac9dbSNicholas Bellinger 	/*
1225c66ac9dbSNicholas Bellinger 	 * Check to see if any active $FABRIC_MOD exports exist.  If they
1226c66ac9dbSNicholas Bellinger 	 * do exist, fail here as changing this information on the fly
1227c66ac9dbSNicholas Bellinger 	 * (underneath the initiator side OS dependent multipath code)
1228c66ac9dbSNicholas Bellinger 	 * could cause negative effects.
1229c66ac9dbSNicholas Bellinger 	 */
12300fd97ccfSChristoph Hellwig 	if (dev->export_count) {
12316708bb27SAndy Grover 		pr_err("Unable to set VPD Unit Serial while"
1232c66ac9dbSNicholas Bellinger 			" active %d $FABRIC_MOD exports exist\n",
12330fd97ccfSChristoph Hellwig 			dev->export_count);
1234c66ac9dbSNicholas Bellinger 		return -EINVAL;
1235c66ac9dbSNicholas Bellinger 	}
12360fd97ccfSChristoph Hellwig 
1237c66ac9dbSNicholas Bellinger 	/*
1238c66ac9dbSNicholas Bellinger 	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1239c66ac9dbSNicholas Bellinger 	 *
1240c66ac9dbSNicholas Bellinger 	 * Also, strip any newline added from the userspace
1241c66ac9dbSNicholas Bellinger 	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1242c66ac9dbSNicholas Bellinger 	 */
1243c66ac9dbSNicholas Bellinger 	memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1244c66ac9dbSNicholas Bellinger 	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
12450fd97ccfSChristoph Hellwig 	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1246c66ac9dbSNicholas Bellinger 			"%s", strstrip(buf));
12470fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1248c66ac9dbSNicholas Bellinger 
12496708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
12500fd97ccfSChristoph Hellwig 			" %s\n", dev->t10_wwn.unit_serial);
1251c66ac9dbSNicholas Bellinger 
1252c66ac9dbSNicholas Bellinger 	return count;
1253c66ac9dbSNicholas Bellinger }
1254c66ac9dbSNicholas Bellinger 
1255c66ac9dbSNicholas Bellinger /*
1256c66ac9dbSNicholas Bellinger  * VPD page 0x83 Protocol Identifier
1257c66ac9dbSNicholas Bellinger  */
12582eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1259c66ac9dbSNicholas Bellinger 		char *page)
1260c66ac9dbSNicholas Bellinger {
12612eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1262c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;
1263c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];
1264c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1265c66ac9dbSNicholas Bellinger 
1266c66ac9dbSNicholas Bellinger 	memset(buf, 0, VPD_TMP_BUF_SIZE);
1267c66ac9dbSNicholas Bellinger 
1268c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);
1269c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
12706708bb27SAndy Grover 		if (!vpd->protocol_identifier_set)
1271c66ac9dbSNicholas Bellinger 			continue;
1272c66ac9dbSNicholas Bellinger 
1273c66ac9dbSNicholas Bellinger 		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1274c66ac9dbSNicholas Bellinger 
12756708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1276c66ac9dbSNicholas Bellinger 			break;
1277c66ac9dbSNicholas Bellinger 
1278c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1279c66ac9dbSNicholas Bellinger 	}
1280c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);
1281c66ac9dbSNicholas Bellinger 
1282c66ac9dbSNicholas Bellinger 	return len;
1283c66ac9dbSNicholas Bellinger }
1284c66ac9dbSNicholas Bellinger 
1285c66ac9dbSNicholas Bellinger /*
1286c66ac9dbSNicholas Bellinger  * Generic wrapper for dumping VPD identifiers by association.
1287c66ac9dbSNicholas Bellinger  */
1288c66ac9dbSNicholas Bellinger #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
12892eafd729SChristoph Hellwig static ssize_t target_wwn_##_name##_show(struct config_item *item,	\
1290c66ac9dbSNicholas Bellinger 		char *page)						\
1291c66ac9dbSNicholas Bellinger {									\
12922eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);			\
1293c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;						\
1294c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];				\
1295c66ac9dbSNicholas Bellinger 	ssize_t len = 0;						\
1296c66ac9dbSNicholas Bellinger 									\
1297c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);				\
1298c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
1299c66ac9dbSNicholas Bellinger 		if (vpd->association != _assoc)				\
1300c66ac9dbSNicholas Bellinger 			continue;					\
1301c66ac9dbSNicholas Bellinger 									\
1302c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1303c66ac9dbSNicholas Bellinger 		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
13046708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1305c66ac9dbSNicholas Bellinger 			break;						\
1306c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1307c66ac9dbSNicholas Bellinger 									\
1308c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1309c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
13106708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1311c66ac9dbSNicholas Bellinger 			break;						\
1312c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1313c66ac9dbSNicholas Bellinger 									\
1314c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1315c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
13166708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1317c66ac9dbSNicholas Bellinger 			break;						\
1318c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1319c66ac9dbSNicholas Bellinger 	}								\
1320c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);				\
1321c66ac9dbSNicholas Bellinger 									\
1322c66ac9dbSNicholas Bellinger 	return len;							\
1323c66ac9dbSNicholas Bellinger }
1324c66ac9dbSNicholas Bellinger 
13252eafd729SChristoph Hellwig /* VPD page 0x83 Association: Logical Unit */
1326c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
13272eafd729SChristoph Hellwig /* VPD page 0x83 Association: Target Port */
1328c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
13292eafd729SChristoph Hellwig /* VPD page 0x83 Association: SCSI Target Device */
1330c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1331c66ac9dbSNicholas Bellinger 
13322eafd729SChristoph Hellwig CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
13332eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
13342eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
13352eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
13362eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1337c66ac9dbSNicholas Bellinger 
1338c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
13392eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_unit_serial,
13402eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_protocol_identifier,
13412eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_logical_unit,
13422eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_target_port,
13432eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_scsi_target_device,
1344c66ac9dbSNicholas Bellinger 	NULL,
1345c66ac9dbSNicholas Bellinger };
1346c66ac9dbSNicholas Bellinger 
13472eafd729SChristoph Hellwig TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1348c66ac9dbSNicholas Bellinger 
1349f8d389c6SNicholas Bellinger /*  End functions for struct config_item_type tb_dev_wwn_cit */
1350c66ac9dbSNicholas Bellinger 
135191e2e39bSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_pr_cit */
1352c66ac9dbSNicholas Bellinger 
13532eafd729SChristoph Hellwig static struct se_device *pr_to_dev(struct config_item *item)
13542eafd729SChristoph Hellwig {
13552eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device,
13562eafd729SChristoph Hellwig 			dev_pr_group);
13572eafd729SChristoph Hellwig }
1358c66ac9dbSNicholas Bellinger 
1359d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1360d977f437SChristoph Hellwig 		char *page)
1361c66ac9dbSNicholas Bellinger {
1362c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1363c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1364c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1365c66ac9dbSNicholas Bellinger 
1366c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1367c66ac9dbSNicholas Bellinger 
1368c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1369d977f437SChristoph Hellwig 	if (!pr_reg)
1370d977f437SChristoph Hellwig 		return sprintf(page, "No SPC-3 Reservation holder\n");
1371d977f437SChristoph Hellwig 
1372c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1373d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1374c66ac9dbSNicholas Bellinger 
1375d977f437SChristoph Hellwig 	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
1376e3d6f909SAndy Grover 		se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1377d2843c17SAndy Grover 		se_nacl->initiatorname, i_buf);
1378c66ac9dbSNicholas Bellinger }
1379c66ac9dbSNicholas Bellinger 
1380d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1381d977f437SChristoph Hellwig 		char *page)
1382c66ac9dbSNicholas Bellinger {
1383c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1384d977f437SChristoph Hellwig 	ssize_t len;
1385c66ac9dbSNicholas Bellinger 
1386c66ac9dbSNicholas Bellinger 	se_nacl = dev->dev_reserved_node_acl;
1387d977f437SChristoph Hellwig 	if (se_nacl) {
1388d977f437SChristoph Hellwig 		len = sprintf(page,
1389d977f437SChristoph Hellwig 			      "SPC-2 Reservation: %s Initiator: %s\n",
1390e3d6f909SAndy Grover 			      se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1391c66ac9dbSNicholas Bellinger 			      se_nacl->initiatorname);
1392d977f437SChristoph Hellwig 	} else {
1393d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-2 Reservation holder\n");
1394d977f437SChristoph Hellwig 	}
1395d977f437SChristoph Hellwig 	return len;
1396c66ac9dbSNicholas Bellinger }
1397c66ac9dbSNicholas Bellinger 
13982eafd729SChristoph Hellwig static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1399c66ac9dbSNicholas Bellinger {
14002eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1401d977f437SChristoph Hellwig 	int ret;
1402c66ac9dbSNicholas Bellinger 
14034ec5bf0eSBryant G. Ly 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1404d977f437SChristoph Hellwig 		return sprintf(page, "Passthrough\n");
1405c66ac9dbSNicholas Bellinger 
1406d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
1407d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1408d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc2_res(dev, page);
1409d977f437SChristoph Hellwig 	else
1410d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc3_res(dev, page);
1411d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1412d977f437SChristoph Hellwig 	return ret;
1413c66ac9dbSNicholas Bellinger }
1414c66ac9dbSNicholas Bellinger 
14152eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
14162eafd729SChristoph Hellwig 		char *page)
1417c66ac9dbSNicholas Bellinger {
14182eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1419c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1420c66ac9dbSNicholas Bellinger 
1421c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1422d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder) {
1423c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1424d977f437SChristoph Hellwig 	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1425c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: All Target"
1426c66ac9dbSNicholas Bellinger 			" Ports registration\n");
1427d977f437SChristoph Hellwig 	} else {
1428c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: Single"
1429c66ac9dbSNicholas Bellinger 			" Target Port registration\n");
1430d977f437SChristoph Hellwig 	}
1431c66ac9dbSNicholas Bellinger 
1432d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1433c66ac9dbSNicholas Bellinger 	return len;
1434c66ac9dbSNicholas Bellinger }
1435c66ac9dbSNicholas Bellinger 
14362eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
14372eafd729SChristoph Hellwig 		char *page)
1438c66ac9dbSNicholas Bellinger {
14392eafd729SChristoph Hellwig 	return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1440c66ac9dbSNicholas Bellinger }
1441c66ac9dbSNicholas Bellinger 
1442c66ac9dbSNicholas Bellinger 
14432eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
14442eafd729SChristoph Hellwig 		char *page)
1445c66ac9dbSNicholas Bellinger {
14462eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1447c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1448c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
1449c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
14509ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1451c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1452c66ac9dbSNicholas Bellinger 
1453c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1454c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
14556708bb27SAndy Grover 	if (!pr_reg) {
1456c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1457d977f437SChristoph Hellwig 		goto out_unlock;
1458c66ac9dbSNicholas Bellinger 	}
1459d977f437SChristoph Hellwig 
1460c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1461c66ac9dbSNicholas Bellinger 	se_tpg = se_nacl->se_tpg;
1462e3d6f909SAndy Grover 	tfo = se_tpg->se_tpg_tfo;
1463c66ac9dbSNicholas Bellinger 
1464c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: %s"
1465c66ac9dbSNicholas Bellinger 		" Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1466c66ac9dbSNicholas Bellinger 		tfo->tpg_get_wwn(se_tpg));
1467c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
146835d1efe8SMasanari Iida 		" Identifier Tag: %hu %s Portal Group Tag: %hu"
1469f2d30680SHannes Reinecke 		" %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
1470c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
147179dc9c9eSNicholas Bellinger 		tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
1472c66ac9dbSNicholas Bellinger 
1473d977f437SChristoph Hellwig out_unlock:
1474d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1475c66ac9dbSNicholas Bellinger 	return len;
1476c66ac9dbSNicholas Bellinger }
1477c66ac9dbSNicholas Bellinger 
1478c66ac9dbSNicholas Bellinger 
14792eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
14802eafd729SChristoph Hellwig 		char *page)
1481c66ac9dbSNicholas Bellinger {
14822eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
14839ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1484c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1485c66ac9dbSNicholas Bellinger 	unsigned char buf[384];
1486c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1487c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1488d2843c17SAndy Grover 	int reg_count = 0;
1489c66ac9dbSNicholas Bellinger 
1490c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1491c66ac9dbSNicholas Bellinger 
14920fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
14930fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1494c66ac9dbSNicholas Bellinger 			pr_reg_list) {
1495c66ac9dbSNicholas Bellinger 
1496c66ac9dbSNicholas Bellinger 		memset(buf, 0, 384);
1497c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1498c66ac9dbSNicholas Bellinger 		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1499d2843c17SAndy Grover 		core_pr_dump_initiator_port(pr_reg, i_buf,
1500c66ac9dbSNicholas Bellinger 					PR_REG_ISID_ID_LEN);
1501c66ac9dbSNicholas Bellinger 		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1502c66ac9dbSNicholas Bellinger 			tfo->get_fabric_name(),
1503d2843c17SAndy Grover 			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1504c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_generation);
1505c66ac9dbSNicholas Bellinger 
15066708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1507c66ac9dbSNicholas Bellinger 			break;
1508c66ac9dbSNicholas Bellinger 
1509c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1510c66ac9dbSNicholas Bellinger 		reg_count++;
1511c66ac9dbSNicholas Bellinger 	}
15120fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
1513c66ac9dbSNicholas Bellinger 
15146708bb27SAndy Grover 	if (!reg_count)
1515c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "None\n");
1516c66ac9dbSNicholas Bellinger 
1517c66ac9dbSNicholas Bellinger 	return len;
1518c66ac9dbSNicholas Bellinger }
1519c66ac9dbSNicholas Bellinger 
15202eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1521c66ac9dbSNicholas Bellinger {
15222eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1523c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1524c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1525c66ac9dbSNicholas Bellinger 
1526c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1527c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1528d977f437SChristoph Hellwig 	if (pr_reg) {
1529c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1530c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1531d977f437SChristoph Hellwig 	} else {
1532d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1533d977f437SChristoph Hellwig 	}
1534c66ac9dbSNicholas Bellinger 
1535d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1536c66ac9dbSNicholas Bellinger 	return len;
1537c66ac9dbSNicholas Bellinger }
1538c66ac9dbSNicholas Bellinger 
15392eafd729SChristoph Hellwig static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
1540c66ac9dbSNicholas Bellinger {
15412eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
15422eafd729SChristoph Hellwig 
15434ec5bf0eSBryant G. Ly 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1544d977f437SChristoph Hellwig 		return sprintf(page, "SPC_PASSTHROUGH\n");
1545d977f437SChristoph Hellwig 	else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1546d977f437SChristoph Hellwig 		return sprintf(page, "SPC2_RESERVATIONS\n");
1547d977f437SChristoph Hellwig 	else
1548d977f437SChristoph Hellwig 		return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1549c66ac9dbSNicholas Bellinger }
1550c66ac9dbSNicholas Bellinger 
15512eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
15522eafd729SChristoph Hellwig 		char *page)
1553c66ac9dbSNicholas Bellinger {
15542eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
15552eafd729SChristoph Hellwig 
15564ec5bf0eSBryant G. Ly 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1557c66ac9dbSNicholas Bellinger 		return 0;
1558c66ac9dbSNicholas Bellinger 
1559c66ac9dbSNicholas Bellinger 	return sprintf(page, "APTPL Bit Status: %s\n",
15600fd97ccfSChristoph Hellwig 		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1561c66ac9dbSNicholas Bellinger }
1562c66ac9dbSNicholas Bellinger 
15632eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
15642eafd729SChristoph Hellwig 		char *page)
1565c66ac9dbSNicholas Bellinger {
15662eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
15672eafd729SChristoph Hellwig 
15684ec5bf0eSBryant G. Ly 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1569c66ac9dbSNicholas Bellinger 		return 0;
1570c66ac9dbSNicholas Bellinger 
1571c66ac9dbSNicholas Bellinger 	return sprintf(page, "Ready to process PR APTPL metadata..\n");
1572c66ac9dbSNicholas Bellinger }
1573c66ac9dbSNicholas Bellinger 
1574c66ac9dbSNicholas Bellinger enum {
1575c66ac9dbSNicholas Bellinger 	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1576c66ac9dbSNicholas Bellinger 	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1577c66ac9dbSNicholas Bellinger 	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1578c66ac9dbSNicholas Bellinger 	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1579c66ac9dbSNicholas Bellinger };
1580c66ac9dbSNicholas Bellinger 
1581c66ac9dbSNicholas Bellinger static match_table_t tokens = {
1582c66ac9dbSNicholas Bellinger 	{Opt_initiator_fabric, "initiator_fabric=%s"},
1583c66ac9dbSNicholas Bellinger 	{Opt_initiator_node, "initiator_node=%s"},
1584c66ac9dbSNicholas Bellinger 	{Opt_initiator_sid, "initiator_sid=%s"},
1585c66ac9dbSNicholas Bellinger 	{Opt_sa_res_key, "sa_res_key=%s"},
1586c66ac9dbSNicholas Bellinger 	{Opt_res_holder, "res_holder=%d"},
1587c66ac9dbSNicholas Bellinger 	{Opt_res_type, "res_type=%d"},
1588c66ac9dbSNicholas Bellinger 	{Opt_res_scope, "res_scope=%d"},
1589c66ac9dbSNicholas Bellinger 	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1590f2d30680SHannes Reinecke 	{Opt_mapped_lun, "mapped_lun=%lld"},
1591c66ac9dbSNicholas Bellinger 	{Opt_target_fabric, "target_fabric=%s"},
1592c66ac9dbSNicholas Bellinger 	{Opt_target_node, "target_node=%s"},
1593c66ac9dbSNicholas Bellinger 	{Opt_tpgt, "tpgt=%d"},
1594c66ac9dbSNicholas Bellinger 	{Opt_port_rtpi, "port_rtpi=%d"},
1595f2d30680SHannes Reinecke 	{Opt_target_lun, "target_lun=%lld"},
1596c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
1597c66ac9dbSNicholas Bellinger };
1598c66ac9dbSNicholas Bellinger 
15992eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
16002eafd729SChristoph Hellwig 		const char *page, size_t count)
1601c66ac9dbSNicholas Bellinger {
16022eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
16036d180253SJesper Juhl 	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
16046d180253SJesper Juhl 	unsigned char *t_fabric = NULL, *t_port = NULL;
16058d213559SJoern Engel 	char *orig, *ptr, *opts;
1606c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
1607c66ac9dbSNicholas Bellinger 	unsigned long long tmp_ll;
1608c66ac9dbSNicholas Bellinger 	u64 sa_res_key = 0;
1609f2d30680SHannes Reinecke 	u64 mapped_lun = 0, target_lun = 0;
1610c66ac9dbSNicholas Bellinger 	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
161145fb94c2SBart Van Assche 	u16 tpgt = 0;
161245fb94c2SBart Van Assche 	u8 type = 0;
1613c66ac9dbSNicholas Bellinger 
16144ec5bf0eSBryant G. Ly 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
16159105bfc0SAndy Grover 		return count;
1616d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
16179105bfc0SAndy Grover 		return count;
1618c66ac9dbSNicholas Bellinger 
16190fd97ccfSChristoph Hellwig 	if (dev->export_count) {
16206708bb27SAndy Grover 		pr_debug("Unable to process APTPL metadata while"
1621c66ac9dbSNicholas Bellinger 			" active fabric exports exist\n");
1622c66ac9dbSNicholas Bellinger 		return -EINVAL;
1623c66ac9dbSNicholas Bellinger 	}
1624c66ac9dbSNicholas Bellinger 
1625c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
1626c66ac9dbSNicholas Bellinger 	if (!opts)
1627c66ac9dbSNicholas Bellinger 		return -ENOMEM;
1628c66ac9dbSNicholas Bellinger 
1629c66ac9dbSNicholas Bellinger 	orig = opts;
163090c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
1631c66ac9dbSNicholas Bellinger 		if (!*ptr)
1632c66ac9dbSNicholas Bellinger 			continue;
1633c66ac9dbSNicholas Bellinger 
1634c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
1635c66ac9dbSNicholas Bellinger 		switch (token) {
1636c66ac9dbSNicholas Bellinger 		case Opt_initiator_fabric:
16378d213559SJoern Engel 			i_fabric = match_strdup(args);
16386d180253SJesper Juhl 			if (!i_fabric) {
16396d180253SJesper Juhl 				ret = -ENOMEM;
16406d180253SJesper Juhl 				goto out;
16416d180253SJesper Juhl 			}
1642c66ac9dbSNicholas Bellinger 			break;
1643c66ac9dbSNicholas Bellinger 		case Opt_initiator_node:
16448d213559SJoern Engel 			i_port = match_strdup(args);
16456d180253SJesper Juhl 			if (!i_port) {
16466d180253SJesper Juhl 				ret = -ENOMEM;
16476d180253SJesper Juhl 				goto out;
16486d180253SJesper Juhl 			}
164960d645a4SDan Carpenter 			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
16506708bb27SAndy Grover 				pr_err("APTPL metadata initiator_node="
1651c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1652c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_IPORT_LEN);
1653c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1654c66ac9dbSNicholas Bellinger 				break;
1655c66ac9dbSNicholas Bellinger 			}
1656c66ac9dbSNicholas Bellinger 			break;
1657c66ac9dbSNicholas Bellinger 		case Opt_initiator_sid:
16588d213559SJoern Engel 			isid = match_strdup(args);
16596d180253SJesper Juhl 			if (!isid) {
16606d180253SJesper Juhl 				ret = -ENOMEM;
16616d180253SJesper Juhl 				goto out;
16626d180253SJesper Juhl 			}
166360d645a4SDan Carpenter 			if (strlen(isid) >= PR_REG_ISID_LEN) {
16646708bb27SAndy Grover 				pr_err("APTPL metadata initiator_isid"
1665c66ac9dbSNicholas Bellinger 					"= exceeds PR_REG_ISID_LEN: %d\n",
1666c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
1667c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1668c66ac9dbSNicholas Bellinger 				break;
1669c66ac9dbSNicholas Bellinger 			}
1670c66ac9dbSNicholas Bellinger 			break;
1671c66ac9dbSNicholas Bellinger 		case Opt_sa_res_key:
16728d213559SJoern Engel 			ret = kstrtoull(args->from, 0, &tmp_ll);
1673c66ac9dbSNicholas Bellinger 			if (ret < 0) {
16748d213559SJoern Engel 				pr_err("kstrtoull() failed for sa_res_key=\n");
1675c66ac9dbSNicholas Bellinger 				goto out;
1676c66ac9dbSNicholas Bellinger 			}
1677c66ac9dbSNicholas Bellinger 			sa_res_key = (u64)tmp_ll;
1678c66ac9dbSNicholas Bellinger 			break;
1679c66ac9dbSNicholas Bellinger 		/*
1680c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Reservation
1681c66ac9dbSNicholas Bellinger 		 */
1682c66ac9dbSNicholas Bellinger 		case Opt_res_holder:
1683c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1684c2091026SDavid Disseldorp 			if (ret)
1685c2091026SDavid Disseldorp 				goto out;
1686c66ac9dbSNicholas Bellinger 			res_holder = arg;
1687c66ac9dbSNicholas Bellinger 			break;
1688c66ac9dbSNicholas Bellinger 		case Opt_res_type:
1689c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1690c2091026SDavid Disseldorp 			if (ret)
1691c2091026SDavid Disseldorp 				goto out;
1692c66ac9dbSNicholas Bellinger 			type = (u8)arg;
1693c66ac9dbSNicholas Bellinger 			break;
1694c66ac9dbSNicholas Bellinger 		case Opt_res_scope:
1695c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1696c2091026SDavid Disseldorp 			if (ret)
1697c2091026SDavid Disseldorp 				goto out;
1698c66ac9dbSNicholas Bellinger 			break;
1699c66ac9dbSNicholas Bellinger 		case Opt_res_all_tg_pt:
1700c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1701c2091026SDavid Disseldorp 			if (ret)
1702c2091026SDavid Disseldorp 				goto out;
1703c66ac9dbSNicholas Bellinger 			all_tg_pt = (int)arg;
1704c66ac9dbSNicholas Bellinger 			break;
1705c66ac9dbSNicholas Bellinger 		case Opt_mapped_lun:
1706c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1707c2091026SDavid Disseldorp 			if (ret)
1708c2091026SDavid Disseldorp 				goto out;
1709f2d30680SHannes Reinecke 			mapped_lun = (u64)arg;
1710c66ac9dbSNicholas Bellinger 			break;
1711c66ac9dbSNicholas Bellinger 		/*
1712c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Target Port
1713c66ac9dbSNicholas Bellinger 		 */
1714c66ac9dbSNicholas Bellinger 		case Opt_target_fabric:
17158d213559SJoern Engel 			t_fabric = match_strdup(args);
17166d180253SJesper Juhl 			if (!t_fabric) {
17176d180253SJesper Juhl 				ret = -ENOMEM;
17186d180253SJesper Juhl 				goto out;
17196d180253SJesper Juhl 			}
1720c66ac9dbSNicholas Bellinger 			break;
1721c66ac9dbSNicholas Bellinger 		case Opt_target_node:
17228d213559SJoern Engel 			t_port = match_strdup(args);
17236d180253SJesper Juhl 			if (!t_port) {
17246d180253SJesper Juhl 				ret = -ENOMEM;
17256d180253SJesper Juhl 				goto out;
17266d180253SJesper Juhl 			}
172760d645a4SDan Carpenter 			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
17286708bb27SAndy Grover 				pr_err("APTPL metadata target_node="
1729c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1730c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_TPORT_LEN);
1731c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1732c66ac9dbSNicholas Bellinger 				break;
1733c66ac9dbSNicholas Bellinger 			}
1734c66ac9dbSNicholas Bellinger 			break;
1735c66ac9dbSNicholas Bellinger 		case Opt_tpgt:
1736c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1737c2091026SDavid Disseldorp 			if (ret)
1738c2091026SDavid Disseldorp 				goto out;
1739c66ac9dbSNicholas Bellinger 			tpgt = (u16)arg;
1740c66ac9dbSNicholas Bellinger 			break;
1741c66ac9dbSNicholas Bellinger 		case Opt_port_rtpi:
1742c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1743c2091026SDavid Disseldorp 			if (ret)
1744c2091026SDavid Disseldorp 				goto out;
1745c66ac9dbSNicholas Bellinger 			break;
1746c66ac9dbSNicholas Bellinger 		case Opt_target_lun:
1747c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1748c2091026SDavid Disseldorp 			if (ret)
1749c2091026SDavid Disseldorp 				goto out;
1750f2d30680SHannes Reinecke 			target_lun = (u64)arg;
1751c66ac9dbSNicholas Bellinger 			break;
1752c66ac9dbSNicholas Bellinger 		default:
1753c66ac9dbSNicholas Bellinger 			break;
1754c66ac9dbSNicholas Bellinger 		}
1755c66ac9dbSNicholas Bellinger 	}
1756c66ac9dbSNicholas Bellinger 
17576708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
17586708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
1759c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1760c66ac9dbSNicholas Bellinger 		goto out;
1761c66ac9dbSNicholas Bellinger 	}
1762c66ac9dbSNicholas Bellinger 
1763c66ac9dbSNicholas Bellinger 	if (res_holder && !(type)) {
17646708bb27SAndy Grover 		pr_err("Illegal PR type: 0x%02x for reservation"
1765c66ac9dbSNicholas Bellinger 				" holder\n", type);
1766c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1767c66ac9dbSNicholas Bellinger 		goto out;
1768c66ac9dbSNicholas Bellinger 	}
1769c66ac9dbSNicholas Bellinger 
17700fd97ccfSChristoph Hellwig 	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1771c66ac9dbSNicholas Bellinger 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1772c66ac9dbSNicholas Bellinger 			res_holder, all_tg_pt, type);
1773c66ac9dbSNicholas Bellinger out:
17746d180253SJesper Juhl 	kfree(i_fabric);
17756d180253SJesper Juhl 	kfree(i_port);
17766d180253SJesper Juhl 	kfree(isid);
17776d180253SJesper Juhl 	kfree(t_fabric);
17786d180253SJesper Juhl 	kfree(t_port);
1779c66ac9dbSNicholas Bellinger 	kfree(orig);
1780c66ac9dbSNicholas Bellinger 	return (ret == 0) ? count : ret;
1781c66ac9dbSNicholas Bellinger }
1782c66ac9dbSNicholas Bellinger 
1783c66ac9dbSNicholas Bellinger 
17842eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_holder);
17852eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
17862eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
17872eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
17882eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
17892eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
17902eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_type);
17912eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
17922eafd729SChristoph Hellwig CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
1793c66ac9dbSNicholas Bellinger 
1794c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_pr_attrs[] = {
17952eafd729SChristoph Hellwig 	&target_pr_attr_res_holder,
17962eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_all_tgt_pts,
17972eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_generation,
17982eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_holder_tg_port,
17992eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_registered_i_pts,
18002eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_type,
18012eafd729SChristoph Hellwig 	&target_pr_attr_res_type,
18022eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_active,
18032eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_metadata,
1804c66ac9dbSNicholas Bellinger 	NULL,
1805c66ac9dbSNicholas Bellinger };
1806c66ac9dbSNicholas Bellinger 
18072eafd729SChristoph Hellwig TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
1808c66ac9dbSNicholas Bellinger 
180991e2e39bSNicholas Bellinger /*  End functions for struct config_item_type tb_dev_pr_cit */
1810c66ac9dbSNicholas Bellinger 
181173112edcSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_cit */
1812c66ac9dbSNicholas Bellinger 
18132eafd729SChristoph Hellwig static inline struct se_device *to_device(struct config_item *item)
1814c66ac9dbSNicholas Bellinger {
18152eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device, dev_group);
18162eafd729SChristoph Hellwig }
18172eafd729SChristoph Hellwig 
18182eafd729SChristoph Hellwig static ssize_t target_dev_info_show(struct config_item *item, char *page)
18192eafd729SChristoph Hellwig {
18202eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1821c66ac9dbSNicholas Bellinger 	int bl = 0;
1822c66ac9dbSNicholas Bellinger 	ssize_t read_bytes = 0;
1823c66ac9dbSNicholas Bellinger 
18240fd97ccfSChristoph Hellwig 	transport_dump_dev_state(dev, page, &bl);
1825c66ac9dbSNicholas Bellinger 	read_bytes += bl;
18260a06d430SChristoph Hellwig 	read_bytes += dev->transport->show_configfs_dev_params(dev,
18270a06d430SChristoph Hellwig 			page+read_bytes);
1828c66ac9dbSNicholas Bellinger 	return read_bytes;
1829c66ac9dbSNicholas Bellinger }
1830c66ac9dbSNicholas Bellinger 
18312eafd729SChristoph Hellwig static ssize_t target_dev_control_store(struct config_item *item,
18322eafd729SChristoph Hellwig 		const char *page, size_t count)
1833c66ac9dbSNicholas Bellinger {
18342eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1835c66ac9dbSNicholas Bellinger 
18360a06d430SChristoph Hellwig 	return dev->transport->set_configfs_dev_params(dev, page, count);
1837c66ac9dbSNicholas Bellinger }
1838c66ac9dbSNicholas Bellinger 
18392eafd729SChristoph Hellwig static ssize_t target_dev_alias_show(struct config_item *item, char *page)
1840c66ac9dbSNicholas Bellinger {
18412eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1842c66ac9dbSNicholas Bellinger 
18430fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_ALIAS))
1844c66ac9dbSNicholas Bellinger 		return 0;
1845c66ac9dbSNicholas Bellinger 
18460fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1847c66ac9dbSNicholas Bellinger }
1848c66ac9dbSNicholas Bellinger 
18492eafd729SChristoph Hellwig static ssize_t target_dev_alias_store(struct config_item *item,
18502eafd729SChristoph Hellwig 		const char *page, size_t count)
1851c66ac9dbSNicholas Bellinger {
18522eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
18530fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1854c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1855c66ac9dbSNicholas Bellinger 
1856c66ac9dbSNicholas Bellinger 	if (count > (SE_DEV_ALIAS_LEN-1)) {
18576708bb27SAndy Grover 		pr_err("alias count: %d exceeds"
1858c66ac9dbSNicholas Bellinger 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1859c66ac9dbSNicholas Bellinger 			SE_DEV_ALIAS_LEN-1);
1860c66ac9dbSNicholas Bellinger 		return -EINVAL;
1861c66ac9dbSNicholas Bellinger 	}
1862c66ac9dbSNicholas Bellinger 
18630fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
18643011684cSDan Carpenter 	if (!read_bytes)
18653011684cSDan Carpenter 		return -EINVAL;
18660fd97ccfSChristoph Hellwig 	if (dev->dev_alias[read_bytes - 1] == '\n')
18670fd97ccfSChristoph Hellwig 		dev->dev_alias[read_bytes - 1] = '\0';
18680877eafdSSebastian Andrzej Siewior 
18690fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_ALIAS;
18703011684cSDan Carpenter 
18716708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1872c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
18730fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
18740fd97ccfSChristoph Hellwig 		dev->dev_alias);
1875c66ac9dbSNicholas Bellinger 
1876c66ac9dbSNicholas Bellinger 	return read_bytes;
1877c66ac9dbSNicholas Bellinger }
1878c66ac9dbSNicholas Bellinger 
18792eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
1880c66ac9dbSNicholas Bellinger {
18812eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1882c66ac9dbSNicholas Bellinger 
18830fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1884c66ac9dbSNicholas Bellinger 		return 0;
1885c66ac9dbSNicholas Bellinger 
18860fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1887c66ac9dbSNicholas Bellinger }
1888c66ac9dbSNicholas Bellinger 
18892eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_store(struct config_item *item,
18902eafd729SChristoph Hellwig 		const char *page, size_t count)
1891c66ac9dbSNicholas Bellinger {
18922eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
18930fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1894c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1895c66ac9dbSNicholas Bellinger 
1896c66ac9dbSNicholas Bellinger 	if (count > (SE_UDEV_PATH_LEN-1)) {
18976708bb27SAndy Grover 		pr_err("udev_path count: %d exceeds"
1898c66ac9dbSNicholas Bellinger 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1899c66ac9dbSNicholas Bellinger 			SE_UDEV_PATH_LEN-1);
1900c66ac9dbSNicholas Bellinger 		return -EINVAL;
1901c66ac9dbSNicholas Bellinger 	}
1902c66ac9dbSNicholas Bellinger 
19030fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1904c66ac9dbSNicholas Bellinger 			"%s", page);
19053011684cSDan Carpenter 	if (!read_bytes)
19063011684cSDan Carpenter 		return -EINVAL;
19070fd97ccfSChristoph Hellwig 	if (dev->udev_path[read_bytes - 1] == '\n')
19080fd97ccfSChristoph Hellwig 		dev->udev_path[read_bytes - 1] = '\0';
19090877eafdSSebastian Andrzej Siewior 
19100fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_UDEV_PATH;
19113011684cSDan Carpenter 
19126708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1913c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
19140fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
19150fd97ccfSChristoph Hellwig 		dev->udev_path);
1916c66ac9dbSNicholas Bellinger 
1917c66ac9dbSNicholas Bellinger 	return read_bytes;
1918c66ac9dbSNicholas Bellinger }
1919c66ac9dbSNicholas Bellinger 
19202eafd729SChristoph Hellwig static ssize_t target_dev_enable_show(struct config_item *item, char *page)
192164146db7SAndy Grover {
19222eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
192364146db7SAndy Grover 
192464146db7SAndy Grover 	return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
192564146db7SAndy Grover }
192664146db7SAndy Grover 
19272eafd729SChristoph Hellwig static ssize_t target_dev_enable_store(struct config_item *item,
19282eafd729SChristoph Hellwig 		const char *page, size_t count)
1929c66ac9dbSNicholas Bellinger {
19302eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1931c66ac9dbSNicholas Bellinger 	char *ptr;
19320fd97ccfSChristoph Hellwig 	int ret;
1933c66ac9dbSNicholas Bellinger 
1934c66ac9dbSNicholas Bellinger 	ptr = strstr(page, "1");
19356708bb27SAndy Grover 	if (!ptr) {
19366708bb27SAndy Grover 		pr_err("For dev_enable ops, only valid value"
1937c66ac9dbSNicholas Bellinger 				" is \"1\"\n");
1938c66ac9dbSNicholas Bellinger 		return -EINVAL;
1939c66ac9dbSNicholas Bellinger 	}
1940c66ac9dbSNicholas Bellinger 
19410fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
19420fd97ccfSChristoph Hellwig 	if (ret)
19430fd97ccfSChristoph Hellwig 		return ret;
1944c66ac9dbSNicholas Bellinger 	return count;
1945c66ac9dbSNicholas Bellinger }
1946c66ac9dbSNicholas Bellinger 
19472eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
1948c66ac9dbSNicholas Bellinger {
19492eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1950c66ac9dbSNicholas Bellinger 	struct config_item *lu_ci;
1951c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
1952c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1953c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1954c66ac9dbSNicholas Bellinger 
1955c66ac9dbSNicholas Bellinger 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1956c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
1957c87fbd56SChristoph Hellwig 		return 0;
1958c66ac9dbSNicholas Bellinger 
1959c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1960c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
19616708bb27SAndy Grover 	if (lu_gp) {
1962c66ac9dbSNicholas Bellinger 		lu_ci = &lu_gp->lu_gp_group.cg_item;
1963c66ac9dbSNicholas Bellinger 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1964c66ac9dbSNicholas Bellinger 			config_item_name(lu_ci), lu_gp->lu_gp_id);
1965c66ac9dbSNicholas Bellinger 	}
1966c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1967c66ac9dbSNicholas Bellinger 
1968c66ac9dbSNicholas Bellinger 	return len;
1969c66ac9dbSNicholas Bellinger }
1970c66ac9dbSNicholas Bellinger 
19712eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
19722eafd729SChristoph Hellwig 		const char *page, size_t count)
1973c66ac9dbSNicholas Bellinger {
19742eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
19750fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1976c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1977c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1978c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
1979c66ac9dbSNicholas Bellinger 	int move = 0;
1980c66ac9dbSNicholas Bellinger 
1981c87fbd56SChristoph Hellwig 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1982c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
19839105bfc0SAndy Grover 		return count;
1984c87fbd56SChristoph Hellwig 
1985c66ac9dbSNicholas Bellinger 	if (count > LU_GROUP_NAME_BUF) {
19866708bb27SAndy Grover 		pr_err("ALUA LU Group Alias too large!\n");
1987c66ac9dbSNicholas Bellinger 		return -EINVAL;
1988c66ac9dbSNicholas Bellinger 	}
1989c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
1990c66ac9dbSNicholas Bellinger 	memcpy(buf, page, count);
1991c66ac9dbSNicholas Bellinger 	/*
1992c66ac9dbSNicholas Bellinger 	 * Any ALUA logical unit alias besides "NULL" means we will be
1993c66ac9dbSNicholas Bellinger 	 * making a new group association.
1994c66ac9dbSNicholas Bellinger 	 */
1995c66ac9dbSNicholas Bellinger 	if (strcmp(strstrip(buf), "NULL")) {
1996c66ac9dbSNicholas Bellinger 		/*
1997c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name() will increment reference to
1998c66ac9dbSNicholas Bellinger 		 * struct t10_alua_lu_gp.  This reference is released with
1999c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name below().
2000c66ac9dbSNicholas Bellinger 		 */
2001c66ac9dbSNicholas Bellinger 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
20026708bb27SAndy Grover 		if (!lu_gp_new)
2003c66ac9dbSNicholas Bellinger 			return -ENODEV;
2004c66ac9dbSNicholas Bellinger 	}
2005c66ac9dbSNicholas Bellinger 
2006c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2007c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
20086708bb27SAndy Grover 	if (lu_gp) {
2009c66ac9dbSNicholas Bellinger 		/*
2010c66ac9dbSNicholas Bellinger 		 * Clearing an existing lu_gp association, and replacing
2011c66ac9dbSNicholas Bellinger 		 * with NULL
2012c66ac9dbSNicholas Bellinger 		 */
20136708bb27SAndy Grover 		if (!lu_gp_new) {
20146708bb27SAndy Grover 			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
2015c66ac9dbSNicholas Bellinger 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2016c66ac9dbSNicholas Bellinger 				" %hu\n",
2017c66ac9dbSNicholas Bellinger 				config_item_name(&hba->hba_group.cg_item),
20180fd97ccfSChristoph Hellwig 				config_item_name(&dev->dev_group.cg_item),
2019c66ac9dbSNicholas Bellinger 				config_item_name(&lu_gp->lu_gp_group.cg_item),
2020c66ac9dbSNicholas Bellinger 				lu_gp->lu_gp_id);
2021c66ac9dbSNicholas Bellinger 
2022c66ac9dbSNicholas Bellinger 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2023c66ac9dbSNicholas Bellinger 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2024c66ac9dbSNicholas Bellinger 
2025c66ac9dbSNicholas Bellinger 			return count;
2026c66ac9dbSNicholas Bellinger 		}
2027c66ac9dbSNicholas Bellinger 		/*
2028c66ac9dbSNicholas Bellinger 		 * Removing existing association of lu_gp_mem with lu_gp
2029c66ac9dbSNicholas Bellinger 		 */
2030c66ac9dbSNicholas Bellinger 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2031c66ac9dbSNicholas Bellinger 		move = 1;
2032c66ac9dbSNicholas Bellinger 	}
2033c66ac9dbSNicholas Bellinger 	/*
2034c66ac9dbSNicholas Bellinger 	 * Associate lu_gp_mem with lu_gp_new.
2035c66ac9dbSNicholas Bellinger 	 */
2036c66ac9dbSNicholas Bellinger 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2037c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2038c66ac9dbSNicholas Bellinger 
20396708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
2040c66ac9dbSNicholas Bellinger 		" core/alua/lu_gps/%s, ID: %hu\n",
2041c66ac9dbSNicholas Bellinger 		(move) ? "Moving" : "Adding",
2042c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
20430fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
2044c66ac9dbSNicholas Bellinger 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2045c66ac9dbSNicholas Bellinger 		lu_gp_new->lu_gp_id);
2046c66ac9dbSNicholas Bellinger 
2047c66ac9dbSNicholas Bellinger 	core_alua_put_lu_gp_from_name(lu_gp_new);
2048c66ac9dbSNicholas Bellinger 	return count;
2049c66ac9dbSNicholas Bellinger }
2050c66ac9dbSNicholas Bellinger 
20512eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
2052229d4f11SHannes Reinecke {
20532eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2054229d4f11SHannes Reinecke 	struct t10_alua_lba_map *map;
2055229d4f11SHannes Reinecke 	struct t10_alua_lba_map_member *mem;
2056229d4f11SHannes Reinecke 	char *b = page;
2057229d4f11SHannes Reinecke 	int bl = 0;
2058229d4f11SHannes Reinecke 	char state;
2059229d4f11SHannes Reinecke 
2060229d4f11SHannes Reinecke 	spin_lock(&dev->t10_alua.lba_map_lock);
2061229d4f11SHannes Reinecke 	if (!list_empty(&dev->t10_alua.lba_map_list))
2062229d4f11SHannes Reinecke 	    bl += sprintf(b + bl, "%u %u\n",
2063229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_size,
2064229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_multiplier);
2065229d4f11SHannes Reinecke 	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2066229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "%llu %llu",
2067229d4f11SHannes Reinecke 			      map->lba_map_first_lba, map->lba_map_last_lba);
2068229d4f11SHannes Reinecke 		list_for_each_entry(mem, &map->lba_map_mem_list,
2069229d4f11SHannes Reinecke 				    lba_map_mem_list) {
2070229d4f11SHannes Reinecke 			switch (mem->lba_map_mem_alua_state) {
2071229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2072229d4f11SHannes Reinecke 				state = 'O';
2073229d4f11SHannes Reinecke 				break;
2074229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2075229d4f11SHannes Reinecke 				state = 'A';
2076229d4f11SHannes Reinecke 				break;
2077229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_STANDBY:
2078229d4f11SHannes Reinecke 				state = 'S';
2079229d4f11SHannes Reinecke 				break;
2080229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_UNAVAILABLE:
2081229d4f11SHannes Reinecke 				state = 'U';
2082229d4f11SHannes Reinecke 				break;
2083229d4f11SHannes Reinecke 			default:
2084229d4f11SHannes Reinecke 				state = '.';
2085229d4f11SHannes Reinecke 				break;
2086229d4f11SHannes Reinecke 			}
2087229d4f11SHannes Reinecke 			bl += sprintf(b + bl, " %d:%c",
2088229d4f11SHannes Reinecke 				      mem->lba_map_mem_alua_pg_id, state);
2089229d4f11SHannes Reinecke 		}
2090229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "\n");
2091229d4f11SHannes Reinecke 	}
2092229d4f11SHannes Reinecke 	spin_unlock(&dev->t10_alua.lba_map_lock);
2093229d4f11SHannes Reinecke 	return bl;
2094229d4f11SHannes Reinecke }
2095229d4f11SHannes Reinecke 
20962eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_store(struct config_item *item,
20972eafd729SChristoph Hellwig 		const char *page, size_t count)
2098229d4f11SHannes Reinecke {
20992eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2100229d4f11SHannes Reinecke 	struct t10_alua_lba_map *lba_map = NULL;
2101229d4f11SHannes Reinecke 	struct list_head lba_list;
2102f0a8afecSBart Van Assche 	char *map_entries, *orig, *ptr;
2103229d4f11SHannes Reinecke 	char state;
2104229d4f11SHannes Reinecke 	int pg_num = -1, pg;
2105229d4f11SHannes Reinecke 	int ret = 0, num = 0, pg_id, alua_state;
2106229d4f11SHannes Reinecke 	unsigned long start_lba = -1, end_lba = -1;
2107229d4f11SHannes Reinecke 	unsigned long segment_size = -1, segment_mult = -1;
2108229d4f11SHannes Reinecke 
2109f0a8afecSBart Van Assche 	orig = map_entries = kstrdup(page, GFP_KERNEL);
2110229d4f11SHannes Reinecke 	if (!map_entries)
2111229d4f11SHannes Reinecke 		return -ENOMEM;
2112229d4f11SHannes Reinecke 
2113229d4f11SHannes Reinecke 	INIT_LIST_HEAD(&lba_list);
2114229d4f11SHannes Reinecke 	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2115229d4f11SHannes Reinecke 		if (!*ptr)
2116229d4f11SHannes Reinecke 			continue;
2117229d4f11SHannes Reinecke 
2118229d4f11SHannes Reinecke 		if (num == 0) {
2119229d4f11SHannes Reinecke 			if (sscanf(ptr, "%lu %lu\n",
2120229d4f11SHannes Reinecke 				   &segment_size, &segment_mult) != 2) {
2121229d4f11SHannes Reinecke 				pr_err("Invalid line %d\n", num);
2122229d4f11SHannes Reinecke 				ret = -EINVAL;
2123229d4f11SHannes Reinecke 				break;
2124229d4f11SHannes Reinecke 			}
2125229d4f11SHannes Reinecke 			num++;
2126229d4f11SHannes Reinecke 			continue;
2127229d4f11SHannes Reinecke 		}
2128229d4f11SHannes Reinecke 		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2129229d4f11SHannes Reinecke 			pr_err("Invalid line %d\n", num);
2130229d4f11SHannes Reinecke 			ret = -EINVAL;
2131229d4f11SHannes Reinecke 			break;
2132229d4f11SHannes Reinecke 		}
2133229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2134229d4f11SHannes Reinecke 		if (!ptr) {
2135229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing end lba\n", num);
2136229d4f11SHannes Reinecke 			ret = -EINVAL;
2137229d4f11SHannes Reinecke 			break;
2138229d4f11SHannes Reinecke 		}
2139229d4f11SHannes Reinecke 		ptr++;
2140229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2141229d4f11SHannes Reinecke 		if (!ptr) {
2142229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing state definitions\n",
2143229d4f11SHannes Reinecke 			       num);
2144229d4f11SHannes Reinecke 			ret = -EINVAL;
2145229d4f11SHannes Reinecke 			break;
2146229d4f11SHannes Reinecke 		}
2147229d4f11SHannes Reinecke 		ptr++;
2148229d4f11SHannes Reinecke 		lba_map = core_alua_allocate_lba_map(&lba_list,
2149229d4f11SHannes Reinecke 						     start_lba, end_lba);
2150229d4f11SHannes Reinecke 		if (IS_ERR(lba_map)) {
2151229d4f11SHannes Reinecke 			ret = PTR_ERR(lba_map);
2152229d4f11SHannes Reinecke 			break;
2153229d4f11SHannes Reinecke 		}
2154229d4f11SHannes Reinecke 		pg = 0;
2155229d4f11SHannes Reinecke 		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2156229d4f11SHannes Reinecke 			switch (state) {
2157229d4f11SHannes Reinecke 			case 'O':
2158229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2159229d4f11SHannes Reinecke 				break;
2160229d4f11SHannes Reinecke 			case 'A':
2161229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2162229d4f11SHannes Reinecke 				break;
2163229d4f11SHannes Reinecke 			case 'S':
2164229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_STANDBY;
2165229d4f11SHannes Reinecke 				break;
2166229d4f11SHannes Reinecke 			case 'U':
2167229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2168229d4f11SHannes Reinecke 				break;
2169229d4f11SHannes Reinecke 			default:
2170229d4f11SHannes Reinecke 				pr_err("Invalid ALUA state '%c'\n", state);
2171229d4f11SHannes Reinecke 				ret = -EINVAL;
2172229d4f11SHannes Reinecke 				goto out;
2173229d4f11SHannes Reinecke 			}
2174229d4f11SHannes Reinecke 
2175229d4f11SHannes Reinecke 			ret = core_alua_allocate_lba_map_mem(lba_map,
2176229d4f11SHannes Reinecke 							     pg_id, alua_state);
2177229d4f11SHannes Reinecke 			if (ret) {
2178229d4f11SHannes Reinecke 				pr_err("Invalid target descriptor %d:%c "
2179229d4f11SHannes Reinecke 				       "at line %d\n",
2180229d4f11SHannes Reinecke 				       pg_id, state, num);
2181229d4f11SHannes Reinecke 				break;
2182229d4f11SHannes Reinecke 			}
2183229d4f11SHannes Reinecke 			pg++;
2184229d4f11SHannes Reinecke 			ptr = strchr(ptr, ' ');
2185229d4f11SHannes Reinecke 			if (ptr)
2186229d4f11SHannes Reinecke 				ptr++;
2187229d4f11SHannes Reinecke 			else
2188229d4f11SHannes Reinecke 				break;
2189229d4f11SHannes Reinecke 		}
2190229d4f11SHannes Reinecke 		if (pg_num == -1)
2191229d4f11SHannes Reinecke 		    pg_num = pg;
2192229d4f11SHannes Reinecke 		else if (pg != pg_num) {
2193229d4f11SHannes Reinecke 			pr_err("Only %d from %d port groups definitions "
2194229d4f11SHannes Reinecke 			       "at line %d\n", pg, pg_num, num);
2195229d4f11SHannes Reinecke 			ret = -EINVAL;
2196229d4f11SHannes Reinecke 			break;
2197229d4f11SHannes Reinecke 		}
2198229d4f11SHannes Reinecke 		num++;
2199229d4f11SHannes Reinecke 	}
2200229d4f11SHannes Reinecke out:
2201229d4f11SHannes Reinecke 	if (ret) {
2202229d4f11SHannes Reinecke 		core_alua_free_lba_map(&lba_list);
2203229d4f11SHannes Reinecke 		count = ret;
2204229d4f11SHannes Reinecke 	} else
2205229d4f11SHannes Reinecke 		core_alua_set_lba_map(dev, &lba_list,
2206229d4f11SHannes Reinecke 				      segment_size, segment_mult);
2207f0a8afecSBart Van Assche 	kfree(orig);
2208229d4f11SHannes Reinecke 	return count;
2209229d4f11SHannes Reinecke }
2210229d4f11SHannes Reinecke 
22112eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_dev_, info);
22122eafd729SChristoph Hellwig CONFIGFS_ATTR_WO(target_dev_, control);
22132eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alias);
22142eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, udev_path);
22152eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, enable);
22162eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alua_lu_gp);
22172eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, lba_map);
2218229d4f11SHannes Reinecke 
221973112edcSNicholas Bellinger static struct configfs_attribute *target_core_dev_attrs[] = {
22202eafd729SChristoph Hellwig 	&target_dev_attr_info,
22212eafd729SChristoph Hellwig 	&target_dev_attr_control,
22222eafd729SChristoph Hellwig 	&target_dev_attr_alias,
22232eafd729SChristoph Hellwig 	&target_dev_attr_udev_path,
22242eafd729SChristoph Hellwig 	&target_dev_attr_enable,
22252eafd729SChristoph Hellwig 	&target_dev_attr_alua_lu_gp,
22262eafd729SChristoph Hellwig 	&target_dev_attr_lba_map,
2227c66ac9dbSNicholas Bellinger 	NULL,
2228c66ac9dbSNicholas Bellinger };
2229c66ac9dbSNicholas Bellinger 
2230c66ac9dbSNicholas Bellinger static void target_core_dev_release(struct config_item *item)
2231c66ac9dbSNicholas Bellinger {
22320fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
22330fd97ccfSChristoph Hellwig 	struct se_device *dev =
22340fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2235c66ac9dbSNicholas Bellinger 
22360fd97ccfSChristoph Hellwig 	target_free_device(dev);
2237c66ac9dbSNicholas Bellinger }
2238c66ac9dbSNicholas Bellinger 
2239c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_item_ops = {
2240c66ac9dbSNicholas Bellinger 	.release		= target_core_dev_release,
2241c66ac9dbSNicholas Bellinger };
2242c66ac9dbSNicholas Bellinger 
224373112edcSNicholas Bellinger TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2244c66ac9dbSNicholas Bellinger 
224573112edcSNicholas Bellinger /* End functions for struct config_item_type tb_dev_cit */
2246c66ac9dbSNicholas Bellinger 
2247c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2248c66ac9dbSNicholas Bellinger 
22492eafd729SChristoph Hellwig static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2250c66ac9dbSNicholas Bellinger {
22512eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_lu_gp,
22522eafd729SChristoph Hellwig 			lu_gp_group);
22532eafd729SChristoph Hellwig }
22542eafd729SChristoph Hellwig 
22552eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
22562eafd729SChristoph Hellwig {
22572eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
22582eafd729SChristoph Hellwig 
22596708bb27SAndy Grover 	if (!lu_gp->lu_gp_valid_id)
2260c66ac9dbSNicholas Bellinger 		return 0;
2261c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2262c66ac9dbSNicholas Bellinger }
2263c66ac9dbSNicholas Bellinger 
22642eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
22652eafd729SChristoph Hellwig 		const char *page, size_t count)
2266c66ac9dbSNicholas Bellinger {
22672eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2268c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2269c66ac9dbSNicholas Bellinger 	unsigned long lu_gp_id;
2270c66ac9dbSNicholas Bellinger 	int ret;
2271c66ac9dbSNicholas Bellinger 
227257103d7fSJingoo Han 	ret = kstrtoul(page, 0, &lu_gp_id);
2273c66ac9dbSNicholas Bellinger 	if (ret < 0) {
227457103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2275c66ac9dbSNicholas Bellinger 			" lu_gp_id\n", ret);
227657103d7fSJingoo Han 		return ret;
2277c66ac9dbSNicholas Bellinger 	}
2278c66ac9dbSNicholas Bellinger 	if (lu_gp_id > 0x0000ffff) {
22796708bb27SAndy Grover 		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2280c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", lu_gp_id);
2281c66ac9dbSNicholas Bellinger 		return -EINVAL;
2282c66ac9dbSNicholas Bellinger 	}
2283c66ac9dbSNicholas Bellinger 
2284c66ac9dbSNicholas Bellinger 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2285c66ac9dbSNicholas Bellinger 	if (ret < 0)
2286c66ac9dbSNicholas Bellinger 		return -EINVAL;
2287c66ac9dbSNicholas Bellinger 
22886708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2289c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
2290c66ac9dbSNicholas Bellinger 		config_item_name(&alua_lu_gp_cg->cg_item),
2291c66ac9dbSNicholas Bellinger 		lu_gp->lu_gp_id);
2292c66ac9dbSNicholas Bellinger 
2293c66ac9dbSNicholas Bellinger 	return count;
2294c66ac9dbSNicholas Bellinger }
2295c66ac9dbSNicholas Bellinger 
22962eafd729SChristoph Hellwig static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2297c66ac9dbSNicholas Bellinger {
22982eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2299c66ac9dbSNicholas Bellinger 	struct se_device *dev;
2300c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2301c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2302c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2303c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
2304c66ac9dbSNicholas Bellinger 
2305c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
2306c66ac9dbSNicholas Bellinger 
2307c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp->lu_gp_lock);
2308c66ac9dbSNicholas Bellinger 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2309c66ac9dbSNicholas Bellinger 		dev = lu_gp_mem->lu_gp_mem_dev;
23100fd97ccfSChristoph Hellwig 		hba = dev->se_hba;
2311c66ac9dbSNicholas Bellinger 
2312c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2313c66ac9dbSNicholas Bellinger 			config_item_name(&hba->hba_group.cg_item),
23140fd97ccfSChristoph Hellwig 			config_item_name(&dev->dev_group.cg_item));
2315c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2316c66ac9dbSNicholas Bellinger 
2317c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
23186708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2319c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2320c66ac9dbSNicholas Bellinger 			break;
2321c66ac9dbSNicholas Bellinger 		}
2322c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2323c66ac9dbSNicholas Bellinger 		len += cur_len;
2324c66ac9dbSNicholas Bellinger 	}
2325c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp->lu_gp_lock);
2326c66ac9dbSNicholas Bellinger 
2327c66ac9dbSNicholas Bellinger 	return len;
2328c66ac9dbSNicholas Bellinger }
2329c66ac9dbSNicholas Bellinger 
23302eafd729SChristoph Hellwig CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
23312eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_lu_gp_, members);
2332c66ac9dbSNicholas Bellinger 
2333c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
23342eafd729SChristoph Hellwig 	&target_lu_gp_attr_lu_gp_id,
23352eafd729SChristoph Hellwig 	&target_lu_gp_attr_members,
2336c66ac9dbSNicholas Bellinger 	NULL,
2337c66ac9dbSNicholas Bellinger };
2338c66ac9dbSNicholas Bellinger 
23391f6fe7cbSNicholas Bellinger static void target_core_alua_lu_gp_release(struct config_item *item)
23401f6fe7cbSNicholas Bellinger {
23411f6fe7cbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
23421f6fe7cbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
23431f6fe7cbSNicholas Bellinger 
23441f6fe7cbSNicholas Bellinger 	core_alua_free_lu_gp(lu_gp);
23451f6fe7cbSNicholas Bellinger }
23461f6fe7cbSNicholas Bellinger 
2347c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_lu_gp_ops = {
23481f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_lu_gp_release,
2349c66ac9dbSNicholas Bellinger };
2350c66ac9dbSNicholas Bellinger 
2351c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gp_cit = {
2352c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
2353c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_lu_gp_attrs,
2354c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2355c66ac9dbSNicholas Bellinger };
2356c66ac9dbSNicholas Bellinger 
2357c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2358c66ac9dbSNicholas Bellinger 
2359c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2360c66ac9dbSNicholas Bellinger 
2361c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_lu_gp(
2362c66ac9dbSNicholas Bellinger 	struct config_group *group,
2363c66ac9dbSNicholas Bellinger 	const char *name)
2364c66ac9dbSNicholas Bellinger {
2365c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2366c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = NULL;
2367c66ac9dbSNicholas Bellinger 	struct config_item *alua_lu_gp_ci = NULL;
2368c66ac9dbSNicholas Bellinger 
2369c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp(name, 0);
2370c66ac9dbSNicholas Bellinger 	if (IS_ERR(lu_gp))
2371c66ac9dbSNicholas Bellinger 		return NULL;
2372c66ac9dbSNicholas Bellinger 
2373c66ac9dbSNicholas Bellinger 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
2374c66ac9dbSNicholas Bellinger 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2375c66ac9dbSNicholas Bellinger 
2376c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_lu_gp_cg, name,
2377c66ac9dbSNicholas Bellinger 			&target_core_alua_lu_gp_cit);
2378c66ac9dbSNicholas Bellinger 
23796708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2380c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s\n",
2381c66ac9dbSNicholas Bellinger 		config_item_name(alua_lu_gp_ci));
2382c66ac9dbSNicholas Bellinger 
2383c66ac9dbSNicholas Bellinger 	return alua_lu_gp_cg;
2384c66ac9dbSNicholas Bellinger 
2385c66ac9dbSNicholas Bellinger }
2386c66ac9dbSNicholas Bellinger 
2387c66ac9dbSNicholas Bellinger static void target_core_alua_drop_lu_gp(
2388c66ac9dbSNicholas Bellinger 	struct config_group *group,
2389c66ac9dbSNicholas Bellinger 	struct config_item *item)
2390c66ac9dbSNicholas Bellinger {
2391c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2392c66ac9dbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
2393c66ac9dbSNicholas Bellinger 
23946708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2395c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
2396c66ac9dbSNicholas Bellinger 		config_item_name(item), lu_gp->lu_gp_id);
23971f6fe7cbSNicholas Bellinger 	/*
23981f6fe7cbSNicholas Bellinger 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
23991f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_lu_gp_release()
24001f6fe7cbSNicholas Bellinger 	 */
2401c66ac9dbSNicholas Bellinger 	config_item_put(item);
2402c66ac9dbSNicholas Bellinger }
2403c66ac9dbSNicholas Bellinger 
2404c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2405c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_lu_gp,
2406c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_lu_gp,
2407c66ac9dbSNicholas Bellinger };
2408c66ac9dbSNicholas Bellinger 
2409c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gps_cit = {
2410c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2411c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
2412c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2413c66ac9dbSNicholas Bellinger };
2414c66ac9dbSNicholas Bellinger 
2415c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2416c66ac9dbSNicholas Bellinger 
2417c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2418c66ac9dbSNicholas Bellinger 
24192eafd729SChristoph Hellwig static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
24202eafd729SChristoph Hellwig {
24212eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
24222eafd729SChristoph Hellwig 			tg_pt_gp_group);
24232eafd729SChristoph Hellwig }
2424c66ac9dbSNicholas Bellinger 
24252eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2426c66ac9dbSNicholas Bellinger 		char *page)
2427c66ac9dbSNicholas Bellinger {
2428c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n",
2429d19c4643SMike Christie 		       to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
2430c66ac9dbSNicholas Bellinger }
2431c66ac9dbSNicholas Bellinger 
24322eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
24332eafd729SChristoph Hellwig 		const char *page, size_t count)
2434c66ac9dbSNicholas Bellinger {
24352eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
24360fd97ccfSChristoph Hellwig 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2437c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2438c66ac9dbSNicholas Bellinger 	int new_state, ret;
2439c66ac9dbSNicholas Bellinger 
24406708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2441125d0119SHannes Reinecke 		pr_err("Unable to do implicit ALUA on non valid"
2442c66ac9dbSNicholas Bellinger 			" tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2443c66ac9dbSNicholas Bellinger 		return -EINVAL;
2444c66ac9dbSNicholas Bellinger 	}
2445f1453773SNicholas Bellinger 	if (!(dev->dev_flags & DF_CONFIGURED)) {
2446f1453773SNicholas Bellinger 		pr_err("Unable to set alua_access_state while device is"
2447f1453773SNicholas Bellinger 		       " not configured\n");
2448f1453773SNicholas Bellinger 		return -ENODEV;
2449f1453773SNicholas Bellinger 	}
2450c66ac9dbSNicholas Bellinger 
245157103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2452c66ac9dbSNicholas Bellinger 	if (ret < 0) {
24536708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access state from"
2454c66ac9dbSNicholas Bellinger 				" %s\n", page);
245557103d7fSJingoo Han 		return ret;
2456c66ac9dbSNicholas Bellinger 	}
2457c66ac9dbSNicholas Bellinger 	new_state = (int)tmp;
2458c66ac9dbSNicholas Bellinger 
2459125d0119SHannes Reinecke 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2460125d0119SHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA"
2461125d0119SHannes Reinecke 			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
2462c66ac9dbSNicholas Bellinger 		return -EINVAL;
2463c66ac9dbSNicholas Bellinger 	}
2464c66094bfSHannes Reinecke 	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2465c66094bfSHannes Reinecke 	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2466c66094bfSHannes Reinecke 		/* LBA DEPENDENT is only allowed with implicit ALUA */
2467c66094bfSHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA transition"
2468c66094bfSHannes Reinecke 		       " while explicit ALUA management is enabled\n");
2469c66094bfSHannes Reinecke 		return -EINVAL;
2470c66094bfSHannes Reinecke 	}
2471c66ac9dbSNicholas Bellinger 
24720fd97ccfSChristoph Hellwig 	ret = core_alua_do_port_transition(tg_pt_gp, dev,
2473c66ac9dbSNicholas Bellinger 					NULL, NULL, new_state, 0);
2474c66ac9dbSNicholas Bellinger 	return (!ret) ? count : -EINVAL;
2475c66ac9dbSNicholas Bellinger }
2476c66ac9dbSNicholas Bellinger 
24772eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2478c66ac9dbSNicholas Bellinger 		char *page)
2479c66ac9dbSNicholas Bellinger {
24802eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2481c66ac9dbSNicholas Bellinger 	return sprintf(page, "%s\n",
2482c66ac9dbSNicholas Bellinger 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2483c66ac9dbSNicholas Bellinger }
2484c66ac9dbSNicholas Bellinger 
24852eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_store(
24862eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2487c66ac9dbSNicholas Bellinger {
24882eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2489c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2490c66ac9dbSNicholas Bellinger 	int new_status, ret;
2491c66ac9dbSNicholas Bellinger 
24926708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
24936708bb27SAndy Grover 		pr_err("Unable to do set ALUA access status on non"
2494c66ac9dbSNicholas Bellinger 			" valid tg_pt_gp ID: %hu\n",
2495c66ac9dbSNicholas Bellinger 			tg_pt_gp->tg_pt_gp_valid_id);
2496c66ac9dbSNicholas Bellinger 		return -EINVAL;
2497c66ac9dbSNicholas Bellinger 	}
2498c66ac9dbSNicholas Bellinger 
249957103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2500c66ac9dbSNicholas Bellinger 	if (ret < 0) {
25016708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access status"
2502c66ac9dbSNicholas Bellinger 				" from %s\n", page);
250357103d7fSJingoo Han 		return ret;
2504c66ac9dbSNicholas Bellinger 	}
2505c66ac9dbSNicholas Bellinger 	new_status = (int)tmp;
2506c66ac9dbSNicholas Bellinger 
2507c66ac9dbSNicholas Bellinger 	if ((new_status != ALUA_STATUS_NONE) &&
2508125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2509125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
25106708bb27SAndy Grover 		pr_err("Illegal ALUA access status: 0x%02x\n",
2511c66ac9dbSNicholas Bellinger 				new_status);
2512c66ac9dbSNicholas Bellinger 		return -EINVAL;
2513c66ac9dbSNicholas Bellinger 	}
2514c66ac9dbSNicholas Bellinger 
2515c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2516c66ac9dbSNicholas Bellinger 	return count;
2517c66ac9dbSNicholas Bellinger }
2518c66ac9dbSNicholas Bellinger 
25192eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2520c66ac9dbSNicholas Bellinger 		char *page)
2521c66ac9dbSNicholas Bellinger {
25222eafd729SChristoph Hellwig 	return core_alua_show_access_type(to_tg_pt_gp(item), page);
2523c66ac9dbSNicholas Bellinger }
2524c66ac9dbSNicholas Bellinger 
25252eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
25262eafd729SChristoph Hellwig 		const char *page, size_t count)
2527c66ac9dbSNicholas Bellinger {
25282eafd729SChristoph Hellwig 	return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2529c66ac9dbSNicholas Bellinger }
2530c66ac9dbSNicholas Bellinger 
25312eafd729SChristoph Hellwig #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit)				\
25322eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_show(		\
25332eafd729SChristoph Hellwig 		struct config_item *item, char *p)			\
2534b0a382c5SHannes Reinecke {									\
25352eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
25362eafd729SChristoph Hellwig 	return sprintf(p, "%d\n",					\
25372eafd729SChristoph Hellwig 		!!(t->tg_pt_gp_alua_supported_states & _bit));		\
25382eafd729SChristoph Hellwig }									\
25392eafd729SChristoph Hellwig 									\
25402eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_store(		\
25412eafd729SChristoph Hellwig 		struct config_item *item, const char *p, size_t c)	\
2542b0a382c5SHannes Reinecke {									\
25432eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
2544b0a382c5SHannes Reinecke 	unsigned long tmp;						\
2545b0a382c5SHannes Reinecke 	int ret;							\
2546b0a382c5SHannes Reinecke 									\
2547b0a382c5SHannes Reinecke 	if (!t->tg_pt_gp_valid_id) {					\
2548c0dcafd8SHannes Reinecke 		pr_err("Unable to do set " #_name " ALUA state on non"	\
2549b0a382c5SHannes Reinecke 		       " valid tg_pt_gp ID: %hu\n",			\
2550b0a382c5SHannes Reinecke 		       t->tg_pt_gp_valid_id);				\
2551b0a382c5SHannes Reinecke 		return -EINVAL;						\
2552b0a382c5SHannes Reinecke 	}								\
2553b0a382c5SHannes Reinecke 									\
2554b0a382c5SHannes Reinecke 	ret = kstrtoul(p, 0, &tmp);					\
2555b0a382c5SHannes Reinecke 	if (ret < 0) {							\
2556b0a382c5SHannes Reinecke 		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
2557b0a382c5SHannes Reinecke 		return -EINVAL;						\
2558b0a382c5SHannes Reinecke 	}								\
2559b0a382c5SHannes Reinecke 	if (tmp > 1) {							\
2560b0a382c5SHannes Reinecke 		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2561b0a382c5SHannes Reinecke 		return -EINVAL;						\
2562b0a382c5SHannes Reinecke 	}								\
25631f0b030cSSebastian Herbszt 	if (tmp)							\
25642eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states |= _bit;		\
2565b0a382c5SHannes Reinecke 	else								\
25662eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states &= ~_bit;		\
2567b0a382c5SHannes Reinecke 									\
2568b0a382c5SHannes Reinecke 	return c;							\
25696be526c4SHannes Reinecke }
25706be526c4SHannes Reinecke 
25712eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
25722eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
25732eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
25742eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
25752eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
25762eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
25772eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
25786be526c4SHannes Reinecke 
25792eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_show(
25802eafd729SChristoph Hellwig 		struct config_item *item, char *page)
2581c66ac9dbSNicholas Bellinger {
25822eafd729SChristoph Hellwig 	return sprintf(page, "%d\n",
25832eafd729SChristoph Hellwig 		to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
2584c66ac9dbSNicholas Bellinger }
2585c66ac9dbSNicholas Bellinger 
25862eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_store(
25872eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2588c66ac9dbSNicholas Bellinger {
25892eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2590c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2591c66ac9dbSNicholas Bellinger 	int ret;
2592c66ac9dbSNicholas Bellinger 
259357103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2594c66ac9dbSNicholas Bellinger 	if (ret < 0) {
25956708bb27SAndy Grover 		pr_err("Unable to extract alua_write_metadata\n");
259657103d7fSJingoo Han 		return ret;
2597c66ac9dbSNicholas Bellinger 	}
2598c66ac9dbSNicholas Bellinger 
2599c66ac9dbSNicholas Bellinger 	if ((tmp != 0) && (tmp != 1)) {
26006708bb27SAndy Grover 		pr_err("Illegal value for alua_write_metadata:"
2601c66ac9dbSNicholas Bellinger 			" %lu\n", tmp);
2602c66ac9dbSNicholas Bellinger 		return -EINVAL;
2603c66ac9dbSNicholas Bellinger 	}
2604c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2605c66ac9dbSNicholas Bellinger 
2606c66ac9dbSNicholas Bellinger 	return count;
2607c66ac9dbSNicholas Bellinger }
2608c66ac9dbSNicholas Bellinger 
26092eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2610c66ac9dbSNicholas Bellinger 		char *page)
2611c66ac9dbSNicholas Bellinger {
26122eafd729SChristoph Hellwig 	return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
2613c66ac9dbSNicholas Bellinger }
2614c66ac9dbSNicholas Bellinger 
26152eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
26162eafd729SChristoph Hellwig 		const char *page, size_t count)
2617c66ac9dbSNicholas Bellinger {
26182eafd729SChristoph Hellwig 	return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
26192eafd729SChristoph Hellwig 			count);
2620c66ac9dbSNicholas Bellinger }
2621c66ac9dbSNicholas Bellinger 
26222eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2623c66ac9dbSNicholas Bellinger 		char *page)
2624c66ac9dbSNicholas Bellinger {
26252eafd729SChristoph Hellwig 	return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
2626c66ac9dbSNicholas Bellinger }
2627c66ac9dbSNicholas Bellinger 
26282eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
26292eafd729SChristoph Hellwig 		const char *page, size_t count)
2630c66ac9dbSNicholas Bellinger {
26312eafd729SChristoph Hellwig 	return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
26322eafd729SChristoph Hellwig 			count);
2633c66ac9dbSNicholas Bellinger }
2634c66ac9dbSNicholas Bellinger 
26352eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
26362eafd729SChristoph Hellwig 		struct config_item *item, char *page)
26372eafd729SChristoph Hellwig {
26382eafd729SChristoph Hellwig 	return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
26392eafd729SChristoph Hellwig }
2640c66ac9dbSNicholas Bellinger 
26412eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
26422eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
26432eafd729SChristoph Hellwig {
26442eafd729SChristoph Hellwig 	return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
26452eafd729SChristoph Hellwig 			count);
26462eafd729SChristoph Hellwig }
26472eafd729SChristoph Hellwig 
26482eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
26495b9a4d72SNicholas Bellinger 		char *page)
26505b9a4d72SNicholas Bellinger {
26512eafd729SChristoph Hellwig 	return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
26525b9a4d72SNicholas Bellinger }
26535b9a4d72SNicholas Bellinger 
26542eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
26552eafd729SChristoph Hellwig 		const char *page, size_t count)
26565b9a4d72SNicholas Bellinger {
26572eafd729SChristoph Hellwig 	return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
26585b9a4d72SNicholas Bellinger }
26595b9a4d72SNicholas Bellinger 
26602eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2661c66ac9dbSNicholas Bellinger 		char *page)
2662c66ac9dbSNicholas Bellinger {
26632eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2664c66ac9dbSNicholas Bellinger 
26656708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id)
2666c66ac9dbSNicholas Bellinger 		return 0;
2667c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2668c66ac9dbSNicholas Bellinger }
2669c66ac9dbSNicholas Bellinger 
26702eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
26712eafd729SChristoph Hellwig 		const char *page, size_t count)
2672c66ac9dbSNicholas Bellinger {
26732eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2674c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2675c66ac9dbSNicholas Bellinger 	unsigned long tg_pt_gp_id;
2676c66ac9dbSNicholas Bellinger 	int ret;
2677c66ac9dbSNicholas Bellinger 
267857103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tg_pt_gp_id);
2679c66ac9dbSNicholas Bellinger 	if (ret < 0) {
26803d035237SHannes Reinecke 		pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
26813d035237SHannes Reinecke 		       page);
268257103d7fSJingoo Han 		return ret;
2683c66ac9dbSNicholas Bellinger 	}
2684c66ac9dbSNicholas Bellinger 	if (tg_pt_gp_id > 0x0000ffff) {
26853d035237SHannes Reinecke 		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
26863d035237SHannes Reinecke 		       tg_pt_gp_id);
2687c66ac9dbSNicholas Bellinger 		return -EINVAL;
2688c66ac9dbSNicholas Bellinger 	}
2689c66ac9dbSNicholas Bellinger 
2690c66ac9dbSNicholas Bellinger 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2691c66ac9dbSNicholas Bellinger 	if (ret < 0)
2692c66ac9dbSNicholas Bellinger 		return -EINVAL;
2693c66ac9dbSNicholas Bellinger 
26946708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2695c66ac9dbSNicholas Bellinger 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
2696c66ac9dbSNicholas Bellinger 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
2697c66ac9dbSNicholas Bellinger 		tg_pt_gp->tg_pt_gp_id);
2698c66ac9dbSNicholas Bellinger 
2699c66ac9dbSNicholas Bellinger 	return count;
2700c66ac9dbSNicholas Bellinger }
2701c66ac9dbSNicholas Bellinger 
27022eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2703c66ac9dbSNicholas Bellinger 		char *page)
2704c66ac9dbSNicholas Bellinger {
27052eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2706c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
2707c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2708c66ac9dbSNicholas Bellinger 	unsigned char buf[TG_PT_GROUP_NAME_BUF];
2709c66ac9dbSNicholas Bellinger 
2710c66ac9dbSNicholas Bellinger 	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2711c66ac9dbSNicholas Bellinger 
2712c66ac9dbSNicholas Bellinger 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2713adf653f9SChristoph Hellwig 	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2714adf653f9SChristoph Hellwig 			lun_tg_pt_gp_link) {
2715adf653f9SChristoph Hellwig 		struct se_portal_group *tpg = lun->lun_tpg;
2716c66ac9dbSNicholas Bellinger 
2717c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2718e3d6f909SAndy Grover 			"/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2719e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2720e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
2721c66ac9dbSNicholas Bellinger 			config_item_name(&lun->lun_group.cg_item));
2722c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2723c66ac9dbSNicholas Bellinger 
2724c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
27256708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2726c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2727c66ac9dbSNicholas Bellinger 			break;
2728c66ac9dbSNicholas Bellinger 		}
2729c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2730c66ac9dbSNicholas Bellinger 		len += cur_len;
2731c66ac9dbSNicholas Bellinger 	}
2732c66ac9dbSNicholas Bellinger 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2733c66ac9dbSNicholas Bellinger 
2734c66ac9dbSNicholas Bellinger 	return len;
2735c66ac9dbSNicholas Bellinger }
2736c66ac9dbSNicholas Bellinger 
27372eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
27382eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
27392eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
27402eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
27412eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
27422eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
27432eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
27442eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
27452eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
27462eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
27472eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
27482eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
27492eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
27502eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
27512eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
27522eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
27532eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
2754c66ac9dbSNicholas Bellinger 
2755c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
27562eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_state,
27572eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_status,
27582eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_type,
27592eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_transitioning,
27602eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_offline,
27612eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_lba_dependent,
27622eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_unavailable,
27632eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_standby,
27642eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_nonoptimized,
27652eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_optimized,
27662eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_write_metadata,
27672eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_nonop_delay_msecs,
27682eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_trans_delay_msecs,
27692eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_implicit_trans_secs,
27702eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_preferred,
27712eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_tg_pt_gp_id,
27722eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_members,
2773c66ac9dbSNicholas Bellinger 	NULL,
2774c66ac9dbSNicholas Bellinger };
2775c66ac9dbSNicholas Bellinger 
27761f6fe7cbSNicholas Bellinger static void target_core_alua_tg_pt_gp_release(struct config_item *item)
27771f6fe7cbSNicholas Bellinger {
27781f6fe7cbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
27791f6fe7cbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
27801f6fe7cbSNicholas Bellinger 
27811f6fe7cbSNicholas Bellinger 	core_alua_free_tg_pt_gp(tg_pt_gp);
27821f6fe7cbSNicholas Bellinger }
27831f6fe7cbSNicholas Bellinger 
2784c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
27851f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_tg_pt_gp_release,
2786c66ac9dbSNicholas Bellinger };
2787c66ac9dbSNicholas Bellinger 
2788c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2789c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
2790c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
2791c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2792c66ac9dbSNicholas Bellinger };
2793c66ac9dbSNicholas Bellinger 
2794c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2795c66ac9dbSNicholas Bellinger 
279672aca57bSNicholas Bellinger /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2797c66ac9dbSNicholas Bellinger 
2798c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_tg_pt_gp(
2799c66ac9dbSNicholas Bellinger 	struct config_group *group,
2800c66ac9dbSNicholas Bellinger 	const char *name)
2801c66ac9dbSNicholas Bellinger {
2802c66ac9dbSNicholas Bellinger 	struct t10_alua *alua = container_of(group, struct t10_alua,
2803c66ac9dbSNicholas Bellinger 					alua_tg_pt_gps_group);
2804c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2805c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = NULL;
2806c66ac9dbSNicholas Bellinger 	struct config_item *alua_tg_pt_gp_ci = NULL;
2807c66ac9dbSNicholas Bellinger 
28080fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
28096708bb27SAndy Grover 	if (!tg_pt_gp)
2810c66ac9dbSNicholas Bellinger 		return NULL;
2811c66ac9dbSNicholas Bellinger 
2812c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2813c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2814c66ac9dbSNicholas Bellinger 
2815c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
2816c66ac9dbSNicholas Bellinger 			&target_core_alua_tg_pt_gp_cit);
2817c66ac9dbSNicholas Bellinger 
28186708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2819c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s\n",
2820c66ac9dbSNicholas Bellinger 		config_item_name(alua_tg_pt_gp_ci));
2821c66ac9dbSNicholas Bellinger 
2822c66ac9dbSNicholas Bellinger 	return alua_tg_pt_gp_cg;
2823c66ac9dbSNicholas Bellinger }
2824c66ac9dbSNicholas Bellinger 
2825c66ac9dbSNicholas Bellinger static void target_core_alua_drop_tg_pt_gp(
2826c66ac9dbSNicholas Bellinger 	struct config_group *group,
2827c66ac9dbSNicholas Bellinger 	struct config_item *item)
2828c66ac9dbSNicholas Bellinger {
2829c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2830c66ac9dbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2831c66ac9dbSNicholas Bellinger 
28326708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2833c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
2834c66ac9dbSNicholas Bellinger 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
28351f6fe7cbSNicholas Bellinger 	/*
28361f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
28371f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_tg_pt_gp_release().
28381f6fe7cbSNicholas Bellinger 	 */
2839c66ac9dbSNicholas Bellinger 	config_item_put(item);
2840c66ac9dbSNicholas Bellinger }
2841c66ac9dbSNicholas Bellinger 
2842c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2843c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_tg_pt_gp,
2844c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
2845c66ac9dbSNicholas Bellinger };
2846c66ac9dbSNicholas Bellinger 
284772aca57bSNicholas Bellinger TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
2848c66ac9dbSNicholas Bellinger 
284972aca57bSNicholas Bellinger /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2850c66ac9dbSNicholas Bellinger 
2851c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_cit */
2852c66ac9dbSNicholas Bellinger 
2853c66ac9dbSNicholas Bellinger /*
2854c66ac9dbSNicholas Bellinger  * target_core_alua_cit is a ConfigFS group that lives under
2855c66ac9dbSNicholas Bellinger  * /sys/kernel/config/target/core/alua.  There are default groups
2856c66ac9dbSNicholas Bellinger  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2857c66ac9dbSNicholas Bellinger  * target_core_alua_cit in target_core_init_configfs() below.
2858c66ac9dbSNicholas Bellinger  */
2859c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_cit = {
2860c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2861c66ac9dbSNicholas Bellinger 	.ct_attrs		= NULL,
2862c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2863c66ac9dbSNicholas Bellinger };
2864c66ac9dbSNicholas Bellinger 
2865c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_cit */
2866c66ac9dbSNicholas Bellinger 
2867d23ab570SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_stat_cit */
286812d23384SNicholas Bellinger 
286912d23384SNicholas Bellinger static struct config_group *target_core_stat_mkdir(
287012d23384SNicholas Bellinger 	struct config_group *group,
287112d23384SNicholas Bellinger 	const char *name)
287212d23384SNicholas Bellinger {
287312d23384SNicholas Bellinger 	return ERR_PTR(-ENOSYS);
287412d23384SNicholas Bellinger }
287512d23384SNicholas Bellinger 
287612d23384SNicholas Bellinger static void target_core_stat_rmdir(
287712d23384SNicholas Bellinger 	struct config_group *group,
287812d23384SNicholas Bellinger 	struct config_item *item)
287912d23384SNicholas Bellinger {
288012d23384SNicholas Bellinger 	return;
288112d23384SNicholas Bellinger }
288212d23384SNicholas Bellinger 
288312d23384SNicholas Bellinger static struct configfs_group_operations target_core_stat_group_ops = {
288412d23384SNicholas Bellinger 	.make_group		= &target_core_stat_mkdir,
288512d23384SNicholas Bellinger 	.drop_item		= &target_core_stat_rmdir,
288612d23384SNicholas Bellinger };
288712d23384SNicholas Bellinger 
2888d23ab570SNicholas Bellinger TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
288912d23384SNicholas Bellinger 
2890d23ab570SNicholas Bellinger /* End functions for struct config_item_type tb_dev_stat_cit */
289112d23384SNicholas Bellinger 
2892c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_hba_cit */
2893c66ac9dbSNicholas Bellinger 
2894c66ac9dbSNicholas Bellinger static struct config_group *target_core_make_subdev(
2895c66ac9dbSNicholas Bellinger 	struct config_group *group,
2896c66ac9dbSNicholas Bellinger 	const char *name)
2897c66ac9dbSNicholas Bellinger {
2898c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2899c66ac9dbSNicholas Bellinger 	struct config_item *hba_ci = &group->cg_item;
2900c66ac9dbSNicholas Bellinger 	struct se_hba *hba = item_to_hba(hba_ci);
29010a06d430SChristoph Hellwig 	struct target_backend *tb = hba->backend;
29020fd97ccfSChristoph Hellwig 	struct se_device *dev;
290312d23384SNicholas Bellinger 	int errno = -ENOMEM, ret;
2904c66ac9dbSNicholas Bellinger 
290512d23384SNicholas Bellinger 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
290612d23384SNicholas Bellinger 	if (ret)
290712d23384SNicholas Bellinger 		return ERR_PTR(ret);
2908c66ac9dbSNicholas Bellinger 
29090fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, name);
29100fd97ccfSChristoph Hellwig 	if (!dev)
29110fd97ccfSChristoph Hellwig 		goto out_unlock;
2912c66ac9dbSNicholas Bellinger 
29131ae1602dSChristoph Hellwig 	config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
2914c66ac9dbSNicholas Bellinger 
29150fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
29160a06d430SChristoph Hellwig 			&tb->tb_dev_attrib_cit);
29171ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
29181ae1602dSChristoph Hellwig 
29190fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_pr_group, "pr",
29200a06d430SChristoph Hellwig 			&tb->tb_dev_pr_cit);
29211ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
29221ae1602dSChristoph Hellwig 
29230fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
29240a06d430SChristoph Hellwig 			&tb->tb_dev_wwn_cit);
29251ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
29261ae1602dSChristoph Hellwig 			&dev->dev_group);
29271ae1602dSChristoph Hellwig 
29280fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
29290a06d430SChristoph Hellwig 			"alua", &tb->tb_dev_alua_tg_pt_gps_cit);
29301ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
29311ae1602dSChristoph Hellwig 			&dev->dev_group);
29321ae1602dSChristoph Hellwig 
29330fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
29340a06d430SChristoph Hellwig 			"statistics", &tb->tb_dev_stat_cit);
29351ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_stat_grps.stat_group,
29361ae1602dSChristoph Hellwig 			&dev->dev_group);
293712d23384SNicholas Bellinger 
2938c66ac9dbSNicholas Bellinger 	/*
293912d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2940c66ac9dbSNicholas Bellinger 	 */
29410fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
29426708bb27SAndy Grover 	if (!tg_pt_gp)
29431ae1602dSChristoph Hellwig 		goto out_free_device;
29440fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2945c66ac9dbSNicholas Bellinger 
2946c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2947c66ac9dbSNicholas Bellinger 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
29481ae1602dSChristoph Hellwig 	configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
29491ae1602dSChristoph Hellwig 			&dev->t10_alua.alua_tg_pt_gps_group);
29501ae1602dSChristoph Hellwig 
295112d23384SNicholas Bellinger 	/*
295212d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/statistics/ default groups
295312d23384SNicholas Bellinger 	 */
29540fd97ccfSChristoph Hellwig 	target_stat_setup_dev_default_groups(dev);
2955c66ac9dbSNicholas Bellinger 
2956c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
29571ae1602dSChristoph Hellwig 	return &dev->dev_group;
29580fd97ccfSChristoph Hellwig 
29590fd97ccfSChristoph Hellwig out_free_device:
29600fd97ccfSChristoph Hellwig 	target_free_device(dev);
29610fd97ccfSChristoph Hellwig out_unlock:
2962c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
296312d23384SNicholas Bellinger 	return ERR_PTR(errno);
2964c66ac9dbSNicholas Bellinger }
2965c66ac9dbSNicholas Bellinger 
2966c66ac9dbSNicholas Bellinger static void target_core_drop_subdev(
2967c66ac9dbSNicholas Bellinger 	struct config_group *group,
2968c66ac9dbSNicholas Bellinger 	struct config_item *item)
2969c66ac9dbSNicholas Bellinger {
29700fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
29710fd97ccfSChristoph Hellwig 	struct se_device *dev =
29720fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2973c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2974c66ac9dbSNicholas Bellinger 
29750fd97ccfSChristoph Hellwig 	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
2976c66ac9dbSNicholas Bellinger 
29771f6fe7cbSNicholas Bellinger 	mutex_lock(&hba->hba_access_mutex);
2978c66ac9dbSNicholas Bellinger 
29791ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
29801ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
298112d23384SNicholas Bellinger 
29821f6fe7cbSNicholas Bellinger 	/*
29831f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
29841f6fe7cbSNicholas Bellinger 	 * directly from target_core_alua_tg_pt_gp_release().
29851f6fe7cbSNicholas Bellinger 	 */
29860fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = NULL;
2987c66ac9dbSNicholas Bellinger 
29881ae1602dSChristoph Hellwig 	configfs_remove_default_groups(dev_cg);
29891ae1602dSChristoph Hellwig 
29901f6fe7cbSNicholas Bellinger 	/*
29910fd97ccfSChristoph Hellwig 	 * se_dev is released from target_core_dev_item_ops->release()
29921f6fe7cbSNicholas Bellinger 	 */
2993c66ac9dbSNicholas Bellinger 	config_item_put(item);
2994c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
2995c66ac9dbSNicholas Bellinger }
2996c66ac9dbSNicholas Bellinger 
2997c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_hba_group_ops = {
2998c66ac9dbSNicholas Bellinger 	.make_group		= target_core_make_subdev,
2999c66ac9dbSNicholas Bellinger 	.drop_item		= target_core_drop_subdev,
3000c66ac9dbSNicholas Bellinger };
3001c66ac9dbSNicholas Bellinger 
3002c66ac9dbSNicholas Bellinger 
30032eafd729SChristoph Hellwig static inline struct se_hba *to_hba(struct config_item *item)
3004c66ac9dbSNicholas Bellinger {
30052eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_hba, hba_group);
30062eafd729SChristoph Hellwig }
30072eafd729SChristoph Hellwig 
30082eafd729SChristoph Hellwig static ssize_t target_hba_info_show(struct config_item *item, char *page)
30092eafd729SChristoph Hellwig {
30102eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
30112eafd729SChristoph Hellwig 
3012c66ac9dbSNicholas Bellinger 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
30130a06d430SChristoph Hellwig 			hba->hba_id, hba->backend->ops->name,
3014ce8dd25dSChristoph Hellwig 			TARGET_CORE_VERSION);
3015c66ac9dbSNicholas Bellinger }
3016c66ac9dbSNicholas Bellinger 
30172eafd729SChristoph Hellwig static ssize_t target_hba_mode_show(struct config_item *item, char *page)
3018c66ac9dbSNicholas Bellinger {
30192eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
3020c66ac9dbSNicholas Bellinger 	int hba_mode = 0;
3021c66ac9dbSNicholas Bellinger 
3022c66ac9dbSNicholas Bellinger 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3023c66ac9dbSNicholas Bellinger 		hba_mode = 1;
3024c66ac9dbSNicholas Bellinger 
3025c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", hba_mode);
3026c66ac9dbSNicholas Bellinger }
3027c66ac9dbSNicholas Bellinger 
30282eafd729SChristoph Hellwig static ssize_t target_hba_mode_store(struct config_item *item,
3029c66ac9dbSNicholas Bellinger 		const char *page, size_t count)
3030c66ac9dbSNicholas Bellinger {
30312eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
3032c66ac9dbSNicholas Bellinger 	unsigned long mode_flag;
3033c66ac9dbSNicholas Bellinger 	int ret;
3034c66ac9dbSNicholas Bellinger 
30350a06d430SChristoph Hellwig 	if (hba->backend->ops->pmode_enable_hba == NULL)
3036c66ac9dbSNicholas Bellinger 		return -EINVAL;
3037c66ac9dbSNicholas Bellinger 
303857103d7fSJingoo Han 	ret = kstrtoul(page, 0, &mode_flag);
3039c66ac9dbSNicholas Bellinger 	if (ret < 0) {
30406708bb27SAndy Grover 		pr_err("Unable to extract hba mode flag: %d\n", ret);
304157103d7fSJingoo Han 		return ret;
3042c66ac9dbSNicholas Bellinger 	}
3043c66ac9dbSNicholas Bellinger 
30440fd97ccfSChristoph Hellwig 	if (hba->dev_count) {
30456708bb27SAndy Grover 		pr_err("Unable to set hba_mode with active devices\n");
3046c66ac9dbSNicholas Bellinger 		return -EINVAL;
3047c66ac9dbSNicholas Bellinger 	}
3048c66ac9dbSNicholas Bellinger 
30490a06d430SChristoph Hellwig 	ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3050c66ac9dbSNicholas Bellinger 	if (ret < 0)
3051c66ac9dbSNicholas Bellinger 		return -EINVAL;
3052c66ac9dbSNicholas Bellinger 	if (ret > 0)
3053c66ac9dbSNicholas Bellinger 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3054c66ac9dbSNicholas Bellinger 	else if (ret == 0)
3055c66ac9dbSNicholas Bellinger 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3056c66ac9dbSNicholas Bellinger 
3057c66ac9dbSNicholas Bellinger 	return count;
3058c66ac9dbSNicholas Bellinger }
3059c66ac9dbSNicholas Bellinger 
30602eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_, hba_info);
30612eafd729SChristoph Hellwig CONFIGFS_ATTR(target_, hba_mode);
3062c66ac9dbSNicholas Bellinger 
30631f6fe7cbSNicholas Bellinger static void target_core_hba_release(struct config_item *item)
30641f6fe7cbSNicholas Bellinger {
30651f6fe7cbSNicholas Bellinger 	struct se_hba *hba = container_of(to_config_group(item),
30661f6fe7cbSNicholas Bellinger 				struct se_hba, hba_group);
30671f6fe7cbSNicholas Bellinger 	core_delete_hba(hba);
30681f6fe7cbSNicholas Bellinger }
30691f6fe7cbSNicholas Bellinger 
3070c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_hba_attrs[] = {
30712eafd729SChristoph Hellwig 	&target_attr_hba_info,
30722eafd729SChristoph Hellwig 	&target_attr_hba_mode,
3073c66ac9dbSNicholas Bellinger 	NULL,
3074c66ac9dbSNicholas Bellinger };
3075c66ac9dbSNicholas Bellinger 
3076c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_hba_item_ops = {
30771f6fe7cbSNicholas Bellinger 	.release		= target_core_hba_release,
3078c66ac9dbSNicholas Bellinger };
3079c66ac9dbSNicholas Bellinger 
3080c66ac9dbSNicholas Bellinger static struct config_item_type target_core_hba_cit = {
3081c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_hba_item_ops,
3082c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_hba_group_ops,
3083c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_hba_attrs,
3084c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3085c66ac9dbSNicholas Bellinger };
3086c66ac9dbSNicholas Bellinger 
3087c66ac9dbSNicholas Bellinger static struct config_group *target_core_call_addhbatotarget(
3088c66ac9dbSNicholas Bellinger 	struct config_group *group,
3089c66ac9dbSNicholas Bellinger 	const char *name)
3090c66ac9dbSNicholas Bellinger {
3091c66ac9dbSNicholas Bellinger 	char *se_plugin_str, *str, *str2;
3092c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
3093c66ac9dbSNicholas Bellinger 	char buf[TARGET_CORE_NAME_MAX_LEN];
3094c66ac9dbSNicholas Bellinger 	unsigned long plugin_dep_id = 0;
3095c66ac9dbSNicholas Bellinger 	int ret;
3096c66ac9dbSNicholas Bellinger 
3097c66ac9dbSNicholas Bellinger 	memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
309860d645a4SDan Carpenter 	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
30996708bb27SAndy Grover 		pr_err("Passed *name strlen(): %d exceeds"
3100c66ac9dbSNicholas Bellinger 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3101c66ac9dbSNicholas Bellinger 			TARGET_CORE_NAME_MAX_LEN);
3102c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENAMETOOLONG);
3103c66ac9dbSNicholas Bellinger 	}
3104c66ac9dbSNicholas Bellinger 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3105c66ac9dbSNicholas Bellinger 
3106c66ac9dbSNicholas Bellinger 	str = strstr(buf, "_");
31076708bb27SAndy Grover 	if (!str) {
31086708bb27SAndy Grover 		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3109c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
3110c66ac9dbSNicholas Bellinger 	}
3111c66ac9dbSNicholas Bellinger 	se_plugin_str = buf;
3112c66ac9dbSNicholas Bellinger 	/*
3113c66ac9dbSNicholas Bellinger 	 * Special case for subsystem plugins that have "_" in their names.
3114c66ac9dbSNicholas Bellinger 	 * Namely rd_direct and rd_mcp..
3115c66ac9dbSNicholas Bellinger 	 */
3116c66ac9dbSNicholas Bellinger 	str2 = strstr(str+1, "_");
31176708bb27SAndy Grover 	if (str2) {
3118c66ac9dbSNicholas Bellinger 		*str2 = '\0'; /* Terminate for *se_plugin_str */
3119c66ac9dbSNicholas Bellinger 		str2++; /* Skip to start of plugin dependent ID */
3120c66ac9dbSNicholas Bellinger 		str = str2;
3121c66ac9dbSNicholas Bellinger 	} else {
3122c66ac9dbSNicholas Bellinger 		*str = '\0'; /* Terminate for *se_plugin_str */
3123c66ac9dbSNicholas Bellinger 		str++; /* Skip to start of plugin dependent ID */
3124c66ac9dbSNicholas Bellinger 	}
3125c66ac9dbSNicholas Bellinger 
312657103d7fSJingoo Han 	ret = kstrtoul(str, 0, &plugin_dep_id);
3127c66ac9dbSNicholas Bellinger 	if (ret < 0) {
312857103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
3129c66ac9dbSNicholas Bellinger 				" plugin_dep_id\n", ret);
313057103d7fSJingoo Han 		return ERR_PTR(ret);
3131c66ac9dbSNicholas Bellinger 	}
3132c66ac9dbSNicholas Bellinger 	/*
3133c66ac9dbSNicholas Bellinger 	 * Load up TCM subsystem plugins if they have not already been loaded.
3134c66ac9dbSNicholas Bellinger 	 */
3135dbc5623eSNicholas Bellinger 	transport_subsystem_check_init();
3136c66ac9dbSNicholas Bellinger 
3137c66ac9dbSNicholas Bellinger 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3138c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
3139c66ac9dbSNicholas Bellinger 		return ERR_CAST(hba);
3140c66ac9dbSNicholas Bellinger 
3141c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&hba->hba_group, name,
3142c66ac9dbSNicholas Bellinger 			&target_core_hba_cit);
3143c66ac9dbSNicholas Bellinger 
3144c66ac9dbSNicholas Bellinger 	return &hba->hba_group;
3145c66ac9dbSNicholas Bellinger }
3146c66ac9dbSNicholas Bellinger 
3147c66ac9dbSNicholas Bellinger static void target_core_call_delhbafromtarget(
3148c66ac9dbSNicholas Bellinger 	struct config_group *group,
3149c66ac9dbSNicholas Bellinger 	struct config_item *item)
3150c66ac9dbSNicholas Bellinger {
31511f6fe7cbSNicholas Bellinger 	/*
31521f6fe7cbSNicholas Bellinger 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
31531f6fe7cbSNicholas Bellinger 	 * -> target_core_hba_release()
31541f6fe7cbSNicholas Bellinger 	 */
3155c66ac9dbSNicholas Bellinger 	config_item_put(item);
3156c66ac9dbSNicholas Bellinger }
3157c66ac9dbSNicholas Bellinger 
3158c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_group_ops = {
3159c66ac9dbSNicholas Bellinger 	.make_group	= target_core_call_addhbatotarget,
3160c66ac9dbSNicholas Bellinger 	.drop_item	= target_core_call_delhbafromtarget,
3161c66ac9dbSNicholas Bellinger };
3162c66ac9dbSNicholas Bellinger 
3163c66ac9dbSNicholas Bellinger static struct config_item_type target_core_cit = {
3164c66ac9dbSNicholas Bellinger 	.ct_item_ops	= NULL,
3165c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_group_ops,
3166c66ac9dbSNicholas Bellinger 	.ct_attrs	= NULL,
3167c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
3168c66ac9dbSNicholas Bellinger };
3169c66ac9dbSNicholas Bellinger 
3170c66ac9dbSNicholas Bellinger /* Stop functions for struct config_item_type target_core_hba_cit */
3171c66ac9dbSNicholas Bellinger 
31720a06d430SChristoph Hellwig void target_setup_backend_cits(struct target_backend *tb)
317373112edcSNicholas Bellinger {
31740a06d430SChristoph Hellwig 	target_core_setup_dev_cit(tb);
31750a06d430SChristoph Hellwig 	target_core_setup_dev_attrib_cit(tb);
31760a06d430SChristoph Hellwig 	target_core_setup_dev_pr_cit(tb);
31770a06d430SChristoph Hellwig 	target_core_setup_dev_wwn_cit(tb);
31780a06d430SChristoph Hellwig 	target_core_setup_dev_alua_tg_pt_gps_cit(tb);
31790a06d430SChristoph Hellwig 	target_core_setup_dev_stat_cit(tb);
318073112edcSNicholas Bellinger }
318173112edcSNicholas Bellinger 
318254550fabSAxel Lin static int __init target_core_init_configfs(void)
3183c66ac9dbSNicholas Bellinger {
3184d588cf8fSChristoph Hellwig 	struct configfs_subsystem *subsys = &target_core_fabrics;
3185c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
3186c66ac9dbSNicholas Bellinger 	int ret;
3187c66ac9dbSNicholas Bellinger 
31886708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3189c66ac9dbSNicholas Bellinger 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
3190c66ac9dbSNicholas Bellinger 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3191c66ac9dbSNicholas Bellinger 
3192c66ac9dbSNicholas Bellinger 	config_group_init(&subsys->su_group);
3193c66ac9dbSNicholas Bellinger 	mutex_init(&subsys->su_mutex);
3194c66ac9dbSNicholas Bellinger 
3195e3d6f909SAndy Grover 	ret = init_se_kmem_caches();
3196c66ac9dbSNicholas Bellinger 	if (ret < 0)
3197e3d6f909SAndy Grover 		return ret;
3198c66ac9dbSNicholas Bellinger 	/*
3199c66ac9dbSNicholas Bellinger 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3200c66ac9dbSNicholas Bellinger 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3201c66ac9dbSNicholas Bellinger 	 */
32021ae1602dSChristoph Hellwig 	config_group_init_type_name(&target_core_hbagroup, "core",
32031ae1602dSChristoph Hellwig 			&target_core_cit);
32041ae1602dSChristoph Hellwig 	configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
3205c66ac9dbSNicholas Bellinger 
3206c66ac9dbSNicholas Bellinger 	/*
3207c66ac9dbSNicholas Bellinger 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3208c66ac9dbSNicholas Bellinger 	 */
32091ae1602dSChristoph Hellwig 	config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
32101ae1602dSChristoph Hellwig 	configfs_add_default_group(&alua_group, &target_core_hbagroup);
32111ae1602dSChristoph Hellwig 
3212c66ac9dbSNicholas Bellinger 	/*
3213c66ac9dbSNicholas Bellinger 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3214c66ac9dbSNicholas Bellinger 	 * groups under /sys/kernel/config/target/core/alua/
3215c66ac9dbSNicholas Bellinger 	 */
32161ae1602dSChristoph Hellwig 	config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
32171ae1602dSChristoph Hellwig 			&target_core_alua_lu_gps_cit);
32181ae1602dSChristoph Hellwig 	configfs_add_default_group(&alua_lu_gps_group, &alua_group);
3219c66ac9dbSNicholas Bellinger 
3220c66ac9dbSNicholas Bellinger 	/*
3221c66ac9dbSNicholas Bellinger 	 * Add core/alua/lu_gps/default_lu_gp
3222c66ac9dbSNicholas Bellinger 	 */
3223c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
322437bb7899SPeter Senna Tschudin 	if (IS_ERR(lu_gp)) {
322537bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3226c66ac9dbSNicholas Bellinger 		goto out_global;
322737bb7899SPeter Senna Tschudin 	}
3228c66ac9dbSNicholas Bellinger 
3229c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3230c66ac9dbSNicholas Bellinger 				&target_core_alua_lu_gp_cit);
32311ae1602dSChristoph Hellwig 	configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
32321ae1602dSChristoph Hellwig 
3233e3d6f909SAndy Grover 	default_lu_gp = lu_gp;
32341ae1602dSChristoph Hellwig 
3235c66ac9dbSNicholas Bellinger 	/*
3236c66ac9dbSNicholas Bellinger 	 * Register the target_core_mod subsystem with configfs.
3237c66ac9dbSNicholas Bellinger 	 */
3238c66ac9dbSNicholas Bellinger 	ret = configfs_register_subsystem(subsys);
3239c66ac9dbSNicholas Bellinger 	if (ret < 0) {
32406708bb27SAndy Grover 		pr_err("Error %d while registering subsystem %s\n",
3241c66ac9dbSNicholas Bellinger 			ret, subsys->su_group.cg_item.ci_namebuf);
3242c66ac9dbSNicholas Bellinger 		goto out_global;
3243c66ac9dbSNicholas Bellinger 	}
32446708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3245ce8dd25dSChristoph Hellwig 		" Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3246c66ac9dbSNicholas Bellinger 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3247c66ac9dbSNicholas Bellinger 	/*
3248c66ac9dbSNicholas Bellinger 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3249c66ac9dbSNicholas Bellinger 	 */
3250c66ac9dbSNicholas Bellinger 	ret = rd_module_init();
3251c66ac9dbSNicholas Bellinger 	if (ret < 0)
3252c66ac9dbSNicholas Bellinger 		goto out;
3253c66ac9dbSNicholas Bellinger 
32540d0f9dfbSRoland Dreier 	ret = core_dev_setup_virtual_lun0();
32550d0f9dfbSRoland Dreier 	if (ret < 0)
3256c66ac9dbSNicholas Bellinger 		goto out;
3257c66ac9dbSNicholas Bellinger 
3258f99715acSNicholas Bellinger 	ret = target_xcopy_setup_pt();
3259f99715acSNicholas Bellinger 	if (ret < 0)
3260f99715acSNicholas Bellinger 		goto out;
3261f99715acSNicholas Bellinger 
3262c66ac9dbSNicholas Bellinger 	return 0;
3263c66ac9dbSNicholas Bellinger 
3264c66ac9dbSNicholas Bellinger out:
3265c66ac9dbSNicholas Bellinger 	configfs_unregister_subsystem(subsys);
3266c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3267c66ac9dbSNicholas Bellinger 	rd_module_exit();
3268c66ac9dbSNicholas Bellinger out_global:
3269e3d6f909SAndy Grover 	if (default_lu_gp) {
3270e3d6f909SAndy Grover 		core_alua_free_lu_gp(default_lu_gp);
3271e3d6f909SAndy Grover 		default_lu_gp = NULL;
3272c66ac9dbSNicholas Bellinger 	}
3273e3d6f909SAndy Grover 	release_se_kmem_caches();
3274e3d6f909SAndy Grover 	return ret;
3275c66ac9dbSNicholas Bellinger }
3276c66ac9dbSNicholas Bellinger 
327754550fabSAxel Lin static void __exit target_core_exit_configfs(void)
3278c66ac9dbSNicholas Bellinger {
32791ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&alua_lu_gps_group);
32801ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&alua_group);
32811ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&target_core_hbagroup);
3282c66ac9dbSNicholas Bellinger 
32837c2bf6e9SNicholas Bellinger 	/*
32847c2bf6e9SNicholas Bellinger 	 * We expect subsys->su_group.default_groups to be released
32857c2bf6e9SNicholas Bellinger 	 * by configfs subsystem provider logic..
32867c2bf6e9SNicholas Bellinger 	 */
3287d588cf8fSChristoph Hellwig 	configfs_unregister_subsystem(&target_core_fabrics);
3288c66ac9dbSNicholas Bellinger 
3289e3d6f909SAndy Grover 	core_alua_free_lu_gp(default_lu_gp);
3290e3d6f909SAndy Grover 	default_lu_gp = NULL;
32917c2bf6e9SNicholas Bellinger 
32926708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3293c66ac9dbSNicholas Bellinger 			" Infrastructure\n");
3294c66ac9dbSNicholas Bellinger 
3295c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3296c66ac9dbSNicholas Bellinger 	rd_module_exit();
3297f99715acSNicholas Bellinger 	target_xcopy_release_pt();
3298e3d6f909SAndy Grover 	release_se_kmem_caches();
3299c66ac9dbSNicholas Bellinger }
3300c66ac9dbSNicholas Bellinger 
3301c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3302c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3303c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
3304c66ac9dbSNicholas Bellinger 
3305c66ac9dbSNicholas Bellinger module_init(target_core_init_configfs);
3306c66ac9dbSNicholas Bellinger module_exit(target_core_exit_configfs);
3307