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 
102c66ac9dbSNicholas Bellinger static struct target_fabric_configfs *target_core_get_fabric(
103c66ac9dbSNicholas Bellinger 	const char *name)
104c66ac9dbSNicholas Bellinger {
105c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
106c66ac9dbSNicholas Bellinger 
1076708bb27SAndy Grover 	if (!name)
108c66ac9dbSNicholas Bellinger 		return NULL;
109c66ac9dbSNicholas Bellinger 
110c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
111c66ac9dbSNicholas Bellinger 	list_for_each_entry(tf, &g_tf_list, tf_list) {
1120dc2e8d1SChristoph Hellwig 		if (!strcmp(tf->tf_ops->name, name)) {
113c66ac9dbSNicholas Bellinger 			atomic_inc(&tf->tf_access_cnt);
114c66ac9dbSNicholas Bellinger 			mutex_unlock(&g_tf_lock);
115c66ac9dbSNicholas Bellinger 			return tf;
116c66ac9dbSNicholas Bellinger 		}
117c66ac9dbSNicholas Bellinger 	}
118c66ac9dbSNicholas Bellinger 	mutex_unlock(&g_tf_lock);
119c66ac9dbSNicholas Bellinger 
120c66ac9dbSNicholas Bellinger 	return NULL;
121c66ac9dbSNicholas Bellinger }
122c66ac9dbSNicholas Bellinger 
123c66ac9dbSNicholas Bellinger /*
124c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->make_group()
125c66ac9dbSNicholas Bellinger  */
126c66ac9dbSNicholas Bellinger static struct config_group *target_core_register_fabric(
127c66ac9dbSNicholas Bellinger 	struct config_group *group,
128c66ac9dbSNicholas Bellinger 	const char *name)
129c66ac9dbSNicholas Bellinger {
130c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
131c66ac9dbSNicholas Bellinger 	int ret;
132c66ac9dbSNicholas Bellinger 
1336708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
134c66ac9dbSNicholas Bellinger 			" %s\n", group, name);
135e7b7af6eSRoland Dreier 
136e7b7af6eSRoland Dreier 	tf = target_core_get_fabric(name);
137e7b7af6eSRoland Dreier 	if (!tf) {
13862554910SNicholas Bellinger 		pr_debug("target_core_register_fabric() trying autoload for %s\n",
139e7b7af6eSRoland Dreier 			 name);
140e7b7af6eSRoland Dreier 
141c66ac9dbSNicholas Bellinger 		/*
142c66ac9dbSNicholas Bellinger 		 * Below are some hardcoded request_module() calls to automatically
143c66ac9dbSNicholas Bellinger 		 * local fabric modules when the following is called:
144c66ac9dbSNicholas Bellinger 		 *
145c66ac9dbSNicholas Bellinger 		 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
146c66ac9dbSNicholas Bellinger 		 *
147c66ac9dbSNicholas Bellinger 		 * Note that this does not limit which TCM fabric module can be
148c66ac9dbSNicholas Bellinger 		 * registered, but simply provids auto loading logic for modules with
149c66ac9dbSNicholas Bellinger 		 * mkdir(2) system calls with known TCM fabric modules.
150c66ac9dbSNicholas Bellinger 		 */
151e7b7af6eSRoland Dreier 
1526708bb27SAndy Grover 		if (!strncmp(name, "iscsi", 5)) {
153c66ac9dbSNicholas Bellinger 			/*
154c66ac9dbSNicholas Bellinger 			 * Automatically load the LIO Target fabric module when the
155c66ac9dbSNicholas Bellinger 			 * following is called:
156c66ac9dbSNicholas Bellinger 			 *
157c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/iscsi
158c66ac9dbSNicholas Bellinger 			 */
159c66ac9dbSNicholas Bellinger 			ret = request_module("iscsi_target_mod");
160c66ac9dbSNicholas Bellinger 			if (ret < 0) {
16162554910SNicholas Bellinger 				pr_debug("request_module() failed for"
162c66ac9dbSNicholas Bellinger 				         " iscsi_target_mod.ko: %d\n", ret);
163c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
164c66ac9dbSNicholas Bellinger 			}
1656708bb27SAndy Grover 		} else if (!strncmp(name, "loopback", 8)) {
166c66ac9dbSNicholas Bellinger 			/*
167c66ac9dbSNicholas Bellinger 			 * Automatically load the tcm_loop fabric module when the
168c66ac9dbSNicholas Bellinger 			 * following is called:
169c66ac9dbSNicholas Bellinger 			 *
170c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/loopback
171c66ac9dbSNicholas Bellinger 			 */
172c66ac9dbSNicholas Bellinger 			ret = request_module("tcm_loop");
173c66ac9dbSNicholas Bellinger 			if (ret < 0) {
17462554910SNicholas Bellinger 				pr_debug("request_module() failed for"
175c66ac9dbSNicholas Bellinger 				         " tcm_loop.ko: %d\n", ret);
176c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
177c66ac9dbSNicholas Bellinger 			}
178c66ac9dbSNicholas Bellinger 		}
179c66ac9dbSNicholas Bellinger 
180c66ac9dbSNicholas Bellinger 		tf = target_core_get_fabric(name);
181e7b7af6eSRoland Dreier 	}
182e7b7af6eSRoland Dreier 
1836708bb27SAndy Grover 	if (!tf) {
18462554910SNicholas Bellinger 		pr_debug("target_core_get_fabric() failed for %s\n",
185c66ac9dbSNicholas Bellinger 		         name);
186c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
187c66ac9dbSNicholas Bellinger 	}
1886708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
1890dc2e8d1SChristoph Hellwig 			" %s\n", tf->tf_ops->name);
190c66ac9dbSNicholas Bellinger 	/*
191c66ac9dbSNicholas Bellinger 	 * On a successful target_core_get_fabric() look, the returned
192c66ac9dbSNicholas Bellinger 	 * struct target_fabric_configfs *tf will contain a usage reference.
193c66ac9dbSNicholas Bellinger 	 */
1946708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
195968ebe75SChristoph Hellwig 			&tf->tf_wwn_cit);
196c66ac9dbSNicholas Bellinger 
197c66ac9dbSNicholas Bellinger 	tf->tf_group.default_groups = tf->tf_default_groups;
198c66ac9dbSNicholas Bellinger 	tf->tf_group.default_groups[0] = &tf->tf_disc_group;
199c66ac9dbSNicholas Bellinger 	tf->tf_group.default_groups[1] = NULL;
200c66ac9dbSNicholas Bellinger 
201968ebe75SChristoph Hellwig 	config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
202c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
203968ebe75SChristoph Hellwig 			&tf->tf_discovery_cit);
204c66ac9dbSNicholas Bellinger 
2056708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
206c66ac9dbSNicholas Bellinger 			" %s\n", tf->tf_group.cg_item.ci_name);
207c66ac9dbSNicholas Bellinger 	return &tf->tf_group;
208c66ac9dbSNicholas Bellinger }
209c66ac9dbSNicholas Bellinger 
210c66ac9dbSNicholas Bellinger /*
211c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->drop_item()
212c66ac9dbSNicholas Bellinger  */
213c66ac9dbSNicholas Bellinger static void target_core_deregister_fabric(
214c66ac9dbSNicholas Bellinger 	struct config_group *group,
215c66ac9dbSNicholas Bellinger 	struct config_item *item)
216c66ac9dbSNicholas Bellinger {
217c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf = container_of(
218c66ac9dbSNicholas Bellinger 		to_config_group(item), struct target_fabric_configfs, tf_group);
219c66ac9dbSNicholas Bellinger 	struct config_group *tf_group;
220c66ac9dbSNicholas Bellinger 	struct config_item *df_item;
221c66ac9dbSNicholas Bellinger 	int i;
222c66ac9dbSNicholas Bellinger 
2236708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
224c66ac9dbSNicholas Bellinger 		" tf list\n", config_item_name(item));
225c66ac9dbSNicholas Bellinger 
2266708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
2270dc2e8d1SChristoph Hellwig 			" %s\n", tf->tf_ops->name);
228c66ac9dbSNicholas Bellinger 	atomic_dec(&tf->tf_access_cnt);
229c66ac9dbSNicholas Bellinger 
2306708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
231c66ac9dbSNicholas Bellinger 			" %s\n", config_item_name(item));
232c66ac9dbSNicholas Bellinger 
233c66ac9dbSNicholas Bellinger 	tf_group = &tf->tf_group;
234c66ac9dbSNicholas Bellinger 	for (i = 0; tf_group->default_groups[i]; i++) {
235c66ac9dbSNicholas Bellinger 		df_item = &tf_group->default_groups[i]->cg_item;
236c66ac9dbSNicholas Bellinger 		tf_group->default_groups[i] = NULL;
237c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
238c66ac9dbSNicholas Bellinger 	}
239c66ac9dbSNicholas Bellinger 	config_item_put(item);
240c66ac9dbSNicholas Bellinger }
241c66ac9dbSNicholas Bellinger 
242c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_fabric_group_ops = {
243c66ac9dbSNicholas Bellinger 	.make_group	= &target_core_register_fabric,
244c66ac9dbSNicholas Bellinger 	.drop_item	= &target_core_deregister_fabric,
245c66ac9dbSNicholas Bellinger };
246c66ac9dbSNicholas Bellinger 
247c66ac9dbSNicholas Bellinger /*
248c66ac9dbSNicholas Bellinger  * All item attributes appearing in /sys/kernel/target/ appear here.
249c66ac9dbSNicholas Bellinger  */
250c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_fabric_item_attrs[] = {
251c66ac9dbSNicholas Bellinger 	&target_core_item_attr_version,
252c66ac9dbSNicholas Bellinger 	NULL,
253c66ac9dbSNicholas Bellinger };
254c66ac9dbSNicholas Bellinger 
255c66ac9dbSNicholas Bellinger /*
256c66ac9dbSNicholas Bellinger  * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
257c66ac9dbSNicholas Bellinger  */
258c66ac9dbSNicholas Bellinger static struct config_item_type target_core_fabrics_item = {
259c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_fabric_group_ops,
260c66ac9dbSNicholas Bellinger 	.ct_attrs	= target_core_fabric_item_attrs,
261c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
262c66ac9dbSNicholas Bellinger };
263c66ac9dbSNicholas Bellinger 
264c66ac9dbSNicholas Bellinger static struct configfs_subsystem target_core_fabrics = {
265c66ac9dbSNicholas Bellinger 	.su_group = {
266c66ac9dbSNicholas Bellinger 		.cg_item = {
267c66ac9dbSNicholas Bellinger 			.ci_namebuf = "target",
268c66ac9dbSNicholas Bellinger 			.ci_type = &target_core_fabrics_item,
269c66ac9dbSNicholas Bellinger 		},
270c66ac9dbSNicholas Bellinger 	},
271c66ac9dbSNicholas Bellinger };
272c66ac9dbSNicholas Bellinger 
273d588cf8fSChristoph Hellwig int target_depend_item(struct config_item *item)
274d588cf8fSChristoph Hellwig {
275d588cf8fSChristoph Hellwig 	return configfs_depend_item(&target_core_fabrics, item);
276d588cf8fSChristoph Hellwig }
277d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_depend_item);
278d588cf8fSChristoph Hellwig 
279d588cf8fSChristoph Hellwig void target_undepend_item(struct config_item *item)
280d588cf8fSChristoph Hellwig {
2819a9e3415SKrzysztof Opasiak 	return configfs_undepend_item(item);
282d588cf8fSChristoph Hellwig }
283d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_undepend_item);
284c66ac9dbSNicholas Bellinger 
285c66ac9dbSNicholas Bellinger /*##############################################################################
286c66ac9dbSNicholas Bellinger // Start functions called by external Target Fabrics Modules
287c66ac9dbSNicholas Bellinger //############################################################################*/
288c66ac9dbSNicholas Bellinger 
2899ac8928eSChristoph Hellwig static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
290c66ac9dbSNicholas Bellinger {
2919ac8928eSChristoph Hellwig 	if (!tfo->name) {
2929ac8928eSChristoph Hellwig 		pr_err("Missing tfo->name\n");
2939ac8928eSChristoph Hellwig 		return -EINVAL;
294c66ac9dbSNicholas Bellinger 	}
2959ac8928eSChristoph Hellwig 	if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
2966708bb27SAndy Grover 		pr_err("Passed name: %s exceeds TARGET_FABRIC"
2979ac8928eSChristoph Hellwig 			"_NAME_SIZE\n", tfo->name);
2989ac8928eSChristoph Hellwig 		return -EINVAL;
299c66ac9dbSNicholas Bellinger 	}
3006708bb27SAndy Grover 	if (!tfo->get_fabric_name) {
3016708bb27SAndy Grover 		pr_err("Missing tfo->get_fabric_name()\n");
302c66ac9dbSNicholas Bellinger 		return -EINVAL;
303c66ac9dbSNicholas Bellinger 	}
3046708bb27SAndy Grover 	if (!tfo->tpg_get_wwn) {
3056708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_wwn()\n");
306c66ac9dbSNicholas Bellinger 		return -EINVAL;
307c66ac9dbSNicholas Bellinger 	}
3086708bb27SAndy Grover 	if (!tfo->tpg_get_tag) {
3096708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_tag()\n");
310c66ac9dbSNicholas Bellinger 		return -EINVAL;
311c66ac9dbSNicholas Bellinger 	}
3126708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode) {
3136708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode()\n");
314c66ac9dbSNicholas Bellinger 		return -EINVAL;
315c66ac9dbSNicholas Bellinger 	}
3166708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_cache) {
3176708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
318c66ac9dbSNicholas Bellinger 		return -EINVAL;
319c66ac9dbSNicholas Bellinger 	}
3206708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_write_protect) {
3216708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
322c66ac9dbSNicholas Bellinger 		return -EINVAL;
323c66ac9dbSNicholas Bellinger 	}
3246708bb27SAndy Grover 	if (!tfo->tpg_check_prod_mode_write_protect) {
3256708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
326c66ac9dbSNicholas Bellinger 		return -EINVAL;
327c66ac9dbSNicholas Bellinger 	}
3286708bb27SAndy Grover 	if (!tfo->tpg_get_inst_index) {
3296708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_inst_index()\n");
330c66ac9dbSNicholas Bellinger 		return -EINVAL;
331c66ac9dbSNicholas Bellinger 	}
33235462975SChristoph Hellwig 	if (!tfo->release_cmd) {
3336708bb27SAndy Grover 		pr_err("Missing tfo->release_cmd()\n");
334c66ac9dbSNicholas Bellinger 		return -EINVAL;
335c66ac9dbSNicholas Bellinger 	}
3366708bb27SAndy Grover 	if (!tfo->shutdown_session) {
3376708bb27SAndy Grover 		pr_err("Missing tfo->shutdown_session()\n");
338c66ac9dbSNicholas Bellinger 		return -EINVAL;
339c66ac9dbSNicholas Bellinger 	}
3406708bb27SAndy Grover 	if (!tfo->close_session) {
3416708bb27SAndy Grover 		pr_err("Missing tfo->close_session()\n");
342c66ac9dbSNicholas Bellinger 		return -EINVAL;
343c66ac9dbSNicholas Bellinger 	}
3446708bb27SAndy Grover 	if (!tfo->sess_get_index) {
3456708bb27SAndy Grover 		pr_err("Missing tfo->sess_get_index()\n");
346c66ac9dbSNicholas Bellinger 		return -EINVAL;
347c66ac9dbSNicholas Bellinger 	}
3486708bb27SAndy Grover 	if (!tfo->write_pending) {
3496708bb27SAndy Grover 		pr_err("Missing tfo->write_pending()\n");
350c66ac9dbSNicholas Bellinger 		return -EINVAL;
351c66ac9dbSNicholas Bellinger 	}
3526708bb27SAndy Grover 	if (!tfo->write_pending_status) {
3536708bb27SAndy Grover 		pr_err("Missing tfo->write_pending_status()\n");
354c66ac9dbSNicholas Bellinger 		return -EINVAL;
355c66ac9dbSNicholas Bellinger 	}
3566708bb27SAndy Grover 	if (!tfo->set_default_node_attributes) {
3576708bb27SAndy Grover 		pr_err("Missing tfo->set_default_node_attributes()\n");
358c66ac9dbSNicholas Bellinger 		return -EINVAL;
359c66ac9dbSNicholas Bellinger 	}
3606708bb27SAndy Grover 	if (!tfo->get_cmd_state) {
3616708bb27SAndy Grover 		pr_err("Missing tfo->get_cmd_state()\n");
362c66ac9dbSNicholas Bellinger 		return -EINVAL;
363c66ac9dbSNicholas Bellinger 	}
3646708bb27SAndy Grover 	if (!tfo->queue_data_in) {
3656708bb27SAndy Grover 		pr_err("Missing tfo->queue_data_in()\n");
366c66ac9dbSNicholas Bellinger 		return -EINVAL;
367c66ac9dbSNicholas Bellinger 	}
3686708bb27SAndy Grover 	if (!tfo->queue_status) {
3696708bb27SAndy Grover 		pr_err("Missing tfo->queue_status()\n");
370c66ac9dbSNicholas Bellinger 		return -EINVAL;
371c66ac9dbSNicholas Bellinger 	}
3726708bb27SAndy Grover 	if (!tfo->queue_tm_rsp) {
3736708bb27SAndy Grover 		pr_err("Missing tfo->queue_tm_rsp()\n");
374c66ac9dbSNicholas Bellinger 		return -EINVAL;
375c66ac9dbSNicholas Bellinger 	}
376131e6abcSNicholas Bellinger 	if (!tfo->aborted_task) {
377131e6abcSNicholas Bellinger 		pr_err("Missing tfo->aborted_task()\n");
378131e6abcSNicholas Bellinger 		return -EINVAL;
379131e6abcSNicholas Bellinger 	}
380c66ac9dbSNicholas Bellinger 	/*
381c66ac9dbSNicholas Bellinger 	 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
382c66ac9dbSNicholas Bellinger 	 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
383c66ac9dbSNicholas Bellinger 	 * target_core_fabric_configfs.c WWN+TPG group context code.
384c66ac9dbSNicholas Bellinger 	 */
3856708bb27SAndy Grover 	if (!tfo->fabric_make_wwn) {
3866708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_wwn()\n");
387c66ac9dbSNicholas Bellinger 		return -EINVAL;
388c66ac9dbSNicholas Bellinger 	}
3896708bb27SAndy Grover 	if (!tfo->fabric_drop_wwn) {
3906708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_wwn()\n");
391c66ac9dbSNicholas Bellinger 		return -EINVAL;
392c66ac9dbSNicholas Bellinger 	}
3936708bb27SAndy Grover 	if (!tfo->fabric_make_tpg) {
3946708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_tpg()\n");
395c66ac9dbSNicholas Bellinger 		return -EINVAL;
396c66ac9dbSNicholas Bellinger 	}
3976708bb27SAndy Grover 	if (!tfo->fabric_drop_tpg) {
3986708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_tpg()\n");
399c66ac9dbSNicholas Bellinger 		return -EINVAL;
400c66ac9dbSNicholas Bellinger 	}
401c66ac9dbSNicholas Bellinger 
402c66ac9dbSNicholas Bellinger 	return 0;
403c66ac9dbSNicholas Bellinger }
404c66ac9dbSNicholas Bellinger 
4059ac8928eSChristoph Hellwig int target_register_template(const struct target_core_fabric_ops *fo)
406c66ac9dbSNicholas Bellinger {
4079ac8928eSChristoph Hellwig 	struct target_fabric_configfs *tf;
408c66ac9dbSNicholas Bellinger 	int ret;
409c66ac9dbSNicholas Bellinger 
4109ac8928eSChristoph Hellwig 	ret = target_fabric_tf_ops_check(fo);
4119ac8928eSChristoph Hellwig 	if (ret)
412c66ac9dbSNicholas Bellinger 		return ret;
413c66ac9dbSNicholas Bellinger 
4149ac8928eSChristoph Hellwig 	tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
4159ac8928eSChristoph Hellwig 	if (!tf) {
4169ac8928eSChristoph Hellwig 		pr_err("%s: could not allocate memory!\n", __func__);
4179ac8928eSChristoph Hellwig 		return -ENOMEM;
4189ac8928eSChristoph Hellwig 	}
4199ac8928eSChristoph Hellwig 
4209ac8928eSChristoph Hellwig 	INIT_LIST_HEAD(&tf->tf_list);
4219ac8928eSChristoph Hellwig 	atomic_set(&tf->tf_access_cnt, 0);
422ef0caf8dSChristoph Hellwig 	tf->tf_ops = fo;
4239ac8928eSChristoph Hellwig 	target_fabric_setup_cits(tf);
4249ac8928eSChristoph Hellwig 
4259ac8928eSChristoph Hellwig 	mutex_lock(&g_tf_lock);
4269ac8928eSChristoph Hellwig 	list_add_tail(&tf->tf_list, &g_tf_list);
4279ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4289ac8928eSChristoph Hellwig 
429c66ac9dbSNicholas Bellinger 	return 0;
430c66ac9dbSNicholas Bellinger }
4319ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_register_template);
432c66ac9dbSNicholas Bellinger 
4339ac8928eSChristoph Hellwig void target_unregister_template(const struct target_core_fabric_ops *fo)
434c66ac9dbSNicholas Bellinger {
4359ac8928eSChristoph Hellwig 	struct target_fabric_configfs *t;
436c66ac9dbSNicholas Bellinger 
437c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
4389ac8928eSChristoph Hellwig 	list_for_each_entry(t, &g_tf_list, tf_list) {
4390dc2e8d1SChristoph Hellwig 		if (!strcmp(t->tf_ops->name, fo->name)) {
4409ac8928eSChristoph Hellwig 			BUG_ON(atomic_read(&t->tf_access_cnt));
4419ac8928eSChristoph Hellwig 			list_del(&t->tf_list);
44294509182SNicholas Bellinger 			mutex_unlock(&g_tf_lock);
44394509182SNicholas Bellinger 			/*
44494509182SNicholas Bellinger 			 * Wait for any outstanding fabric se_deve_entry->rcu_head
44594509182SNicholas Bellinger 			 * callbacks to complete post kfree_rcu(), before allowing
44694509182SNicholas Bellinger 			 * fabric driver unload of TFO->module to proceed.
44794509182SNicholas Bellinger 			 */
44894509182SNicholas Bellinger 			rcu_barrier();
4499ac8928eSChristoph Hellwig 			kfree(t);
45094509182SNicholas Bellinger 			return;
451c66ac9dbSNicholas Bellinger 		}
452c66ac9dbSNicholas Bellinger 	}
4539ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4549ac8928eSChristoph Hellwig }
4559ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_unregister_template);
456c66ac9dbSNicholas Bellinger 
457c66ac9dbSNicholas Bellinger /*##############################################################################
458c66ac9dbSNicholas Bellinger // Stop functions called by external Target Fabrics Modules
459c66ac9dbSNicholas Bellinger //############################################################################*/
460c66ac9dbSNicholas Bellinger 
4612eafd729SChristoph Hellwig static inline struct se_dev_attrib *to_attrib(struct config_item *item)
4622eafd729SChristoph Hellwig {
4632eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_dev_attrib,
4642eafd729SChristoph Hellwig 			da_group);
4655873c4d1SChristoph Hellwig }
4665873c4d1SChristoph Hellwig 
4672eafd729SChristoph Hellwig /* Start functions for struct config_item_type tb_dev_attrib_cit */
4682eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_SHOW(_name)					\
4692eafd729SChristoph Hellwig static ssize_t _name##_show(struct config_item *item, char *page)	\
4702eafd729SChristoph Hellwig {									\
4712eafd729SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
4722eafd729SChristoph Hellwig }
4733effdb90SChristoph Hellwig 
4742eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
4752eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
4762eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
4772eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
4782eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
4792eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
4802eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
4812eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
4822eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
4832eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
4842eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
4852eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
4862eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
4872eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_format);
4882eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
4892eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
4902eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
4912eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
4922eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
4932eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(block_size);
4942eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
4952eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
4962eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
4972eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
4982eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
4992eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
5002eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
5012eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
502e6f41633SJamie Pocas DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
5032eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
5042eafd729SChristoph Hellwig 
5052eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_U32(_name)				\
5062eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
5075873c4d1SChristoph Hellwig 		size_t count)						\
5085873c4d1SChristoph Hellwig {									\
5092eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5103effdb90SChristoph Hellwig 	u32 val;							\
5115873c4d1SChristoph Hellwig 	int ret;							\
5125873c4d1SChristoph Hellwig 									\
5133effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);					\
5143effdb90SChristoph Hellwig 	if (ret < 0)							\
5153effdb90SChristoph Hellwig 		return ret;						\
5163effdb90SChristoph Hellwig 	da->_name = val;						\
5173effdb90SChristoph Hellwig 	return count;							\
5185873c4d1SChristoph Hellwig }
5195873c4d1SChristoph Hellwig 
5202eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
5212eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
5222eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
5232eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
5242eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
5255873c4d1SChristoph Hellwig 
5262eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name)				\
5272eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,	\
5283effdb90SChristoph Hellwig 		size_t count)						\
5293effdb90SChristoph Hellwig {									\
5302eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5313effdb90SChristoph Hellwig 	bool flag;							\
5323effdb90SChristoph Hellwig 	int ret;							\
5333effdb90SChristoph Hellwig 									\
5343effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);					\
5353effdb90SChristoph Hellwig 	if (ret < 0)							\
5363effdb90SChristoph Hellwig 		return ret;						\
5373effdb90SChristoph Hellwig 	da->_name = flag;						\
5383effdb90SChristoph Hellwig 	return count;							\
5393effdb90SChristoph Hellwig }
5403effdb90SChristoph Hellwig 
5412eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
5422eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
5432eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
5442eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
5452eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
5463effdb90SChristoph Hellwig 
5472eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name)				\
5482eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
5493effdb90SChristoph Hellwig 		size_t count)						\
5503effdb90SChristoph Hellwig {									\
5513effdb90SChristoph Hellwig 	printk_once(KERN_WARNING					\
552234bdbc4SChristophe Vu-Brugier 		"ignoring deprecated %s attribute\n",			\
553234bdbc4SChristophe Vu-Brugier 		__stringify(_name));					\
5543effdb90SChristoph Hellwig 	return count;							\
5553effdb90SChristoph Hellwig }
5563effdb90SChristoph Hellwig 
5572eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
5582eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
5593effdb90SChristoph Hellwig 
5603effdb90SChristoph Hellwig static void dev_set_t10_wwn_model_alias(struct se_device *dev)
5613effdb90SChristoph Hellwig {
5623effdb90SChristoph Hellwig 	const char *configname;
5633effdb90SChristoph Hellwig 
5643effdb90SChristoph Hellwig 	configname = config_item_name(&dev->dev_group.cg_item);
5653effdb90SChristoph Hellwig 	if (strlen(configname) >= 16) {
5663effdb90SChristoph Hellwig 		pr_warn("dev[%p]: Backstore name '%s' is too long for "
5673effdb90SChristoph Hellwig 			"INQUIRY_MODEL, truncating to 16 bytes\n", dev,
5683effdb90SChristoph Hellwig 			configname);
5693effdb90SChristoph Hellwig 	}
5703effdb90SChristoph Hellwig 	snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
5713effdb90SChristoph Hellwig }
5723effdb90SChristoph Hellwig 
5732eafd729SChristoph Hellwig static ssize_t emulate_model_alias_store(struct config_item *item,
5743effdb90SChristoph Hellwig 		const char *page, size_t count)
5753effdb90SChristoph Hellwig {
5762eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
5773effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
5783effdb90SChristoph Hellwig 	bool flag;
5793effdb90SChristoph Hellwig 	int ret;
5803effdb90SChristoph Hellwig 
5813effdb90SChristoph Hellwig 	if (dev->export_count) {
5823effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change model alias"
5833effdb90SChristoph Hellwig 			" while export_count is %d\n",
5843effdb90SChristoph Hellwig 			dev, dev->export_count);
5853effdb90SChristoph Hellwig 		return -EINVAL;
5863effdb90SChristoph Hellwig 	}
5873effdb90SChristoph Hellwig 
5883effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
5893effdb90SChristoph Hellwig 	if (ret < 0)
5903effdb90SChristoph Hellwig 		return ret;
5913effdb90SChristoph Hellwig 
5923effdb90SChristoph Hellwig 	if (flag) {
5933effdb90SChristoph Hellwig 		dev_set_t10_wwn_model_alias(dev);
5943effdb90SChristoph Hellwig 	} else {
5953effdb90SChristoph Hellwig 		strncpy(&dev->t10_wwn.model[0],
5963effdb90SChristoph Hellwig 			dev->transport->inquiry_prod, 16);
5973effdb90SChristoph Hellwig 	}
5983effdb90SChristoph Hellwig 	da->emulate_model_alias = flag;
5993effdb90SChristoph Hellwig 	return count;
6003effdb90SChristoph Hellwig }
6013effdb90SChristoph Hellwig 
6022eafd729SChristoph Hellwig static ssize_t emulate_write_cache_store(struct config_item *item,
6033effdb90SChristoph Hellwig 		const char *page, size_t count)
6043effdb90SChristoph Hellwig {
6052eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6063effdb90SChristoph Hellwig 	bool flag;
6073effdb90SChristoph Hellwig 	int ret;
6083effdb90SChristoph Hellwig 
6093effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6103effdb90SChristoph Hellwig 	if (ret < 0)
6113effdb90SChristoph Hellwig 		return ret;
6123effdb90SChristoph Hellwig 
6133effdb90SChristoph Hellwig 	if (flag && da->da_dev->transport->get_write_cache) {
6143effdb90SChristoph Hellwig 		pr_err("emulate_write_cache not supported for this device\n");
6153effdb90SChristoph Hellwig 		return -EINVAL;
6163effdb90SChristoph Hellwig 	}
6173effdb90SChristoph Hellwig 
6183effdb90SChristoph Hellwig 	da->emulate_write_cache = flag;
6193effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
6203effdb90SChristoph Hellwig 			da->da_dev, flag);
6213effdb90SChristoph Hellwig 	return count;
6223effdb90SChristoph Hellwig }
6233effdb90SChristoph Hellwig 
6242eafd729SChristoph Hellwig static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
6253effdb90SChristoph Hellwig 		const char *page, size_t count)
6263effdb90SChristoph Hellwig {
6272eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6283effdb90SChristoph Hellwig 	u32 val;
6293effdb90SChristoph Hellwig 	int ret;
6303effdb90SChristoph Hellwig 
6313effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
6323effdb90SChristoph Hellwig 	if (ret < 0)
6333effdb90SChristoph Hellwig 		return ret;
6343effdb90SChristoph Hellwig 
6353effdb90SChristoph Hellwig 	if (val != 0 && val != 1 && val != 2) {
6363effdb90SChristoph Hellwig 		pr_err("Illegal value %d\n", val);
6373effdb90SChristoph Hellwig 		return -EINVAL;
6383effdb90SChristoph Hellwig 	}
6393effdb90SChristoph Hellwig 
6403effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
6413effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
6423effdb90SChristoph Hellwig 			" UA_INTRLCK_CTRL while export_count is %d\n",
6433effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
6443effdb90SChristoph Hellwig 		return -EINVAL;
6453effdb90SChristoph Hellwig 	}
6463effdb90SChristoph Hellwig 	da->emulate_ua_intlck_ctrl = val;
6473effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
6483effdb90SChristoph Hellwig 		da->da_dev, val);
6493effdb90SChristoph Hellwig 	return count;
6503effdb90SChristoph Hellwig }
6513effdb90SChristoph Hellwig 
6522eafd729SChristoph Hellwig static ssize_t emulate_tas_store(struct config_item *item,
6533effdb90SChristoph Hellwig 		const char *page, size_t count)
6543effdb90SChristoph Hellwig {
6552eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6563effdb90SChristoph Hellwig 	bool flag;
6573effdb90SChristoph Hellwig 	int ret;
6583effdb90SChristoph Hellwig 
6593effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6603effdb90SChristoph Hellwig 	if (ret < 0)
6613effdb90SChristoph Hellwig 		return ret;
6623effdb90SChristoph Hellwig 
6633effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
6643effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TAS while"
6653effdb90SChristoph Hellwig 			" export_count is %d\n",
6663effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
6673effdb90SChristoph Hellwig 		return -EINVAL;
6683effdb90SChristoph Hellwig 	}
6693effdb90SChristoph Hellwig 	da->emulate_tas = flag;
6703effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
6713effdb90SChristoph Hellwig 		da->da_dev, flag ? "Enabled" : "Disabled");
6723effdb90SChristoph Hellwig 
6733effdb90SChristoph Hellwig 	return count;
6743effdb90SChristoph Hellwig }
6753effdb90SChristoph Hellwig 
6762eafd729SChristoph Hellwig static ssize_t emulate_tpu_store(struct config_item *item,
6773effdb90SChristoph Hellwig 		const char *page, size_t count)
6783effdb90SChristoph Hellwig {
6792eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6803effdb90SChristoph Hellwig 	bool flag;
6813effdb90SChristoph Hellwig 	int ret;
6823effdb90SChristoph Hellwig 
6833effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6843effdb90SChristoph Hellwig 	if (ret < 0)
6853effdb90SChristoph Hellwig 		return ret;
6863effdb90SChristoph Hellwig 
6873effdb90SChristoph Hellwig 	/*
6883effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
6893effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
6903effdb90SChristoph Hellwig 	 */
6913effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
6923effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
6933effdb90SChristoph Hellwig 		return -ENOSYS;
6943effdb90SChristoph Hellwig 	}
6953effdb90SChristoph Hellwig 
6963effdb90SChristoph Hellwig 	da->emulate_tpu = flag;
6973effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
6983effdb90SChristoph Hellwig 		da->da_dev, flag);
6993effdb90SChristoph Hellwig 	return count;
7003effdb90SChristoph Hellwig }
7013effdb90SChristoph Hellwig 
7022eafd729SChristoph Hellwig static ssize_t emulate_tpws_store(struct config_item *item,
7033effdb90SChristoph Hellwig 		const char *page, size_t count)
7043effdb90SChristoph Hellwig {
7052eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7063effdb90SChristoph Hellwig 	bool flag;
7073effdb90SChristoph Hellwig 	int ret;
7083effdb90SChristoph Hellwig 
7093effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7103effdb90SChristoph Hellwig 	if (ret < 0)
7113effdb90SChristoph Hellwig 		return ret;
7123effdb90SChristoph Hellwig 
7133effdb90SChristoph Hellwig 	/*
7143effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
7153effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
7163effdb90SChristoph Hellwig 	 */
7173effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
7183effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
7193effdb90SChristoph Hellwig 		return -ENOSYS;
7203effdb90SChristoph Hellwig 	}
7213effdb90SChristoph Hellwig 
7223effdb90SChristoph Hellwig 	da->emulate_tpws = flag;
7233effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
7243effdb90SChristoph Hellwig 				da->da_dev, flag);
7253effdb90SChristoph Hellwig 	return count;
7263effdb90SChristoph Hellwig }
7273effdb90SChristoph Hellwig 
7282eafd729SChristoph Hellwig static ssize_t pi_prot_type_store(struct config_item *item,
7293effdb90SChristoph Hellwig 		const char *page, size_t count)
7303effdb90SChristoph Hellwig {
7312eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7323effdb90SChristoph Hellwig 	int old_prot = da->pi_prot_type, ret;
7333effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
7343effdb90SChristoph Hellwig 	u32 flag;
7353effdb90SChristoph Hellwig 
7363effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &flag);
7373effdb90SChristoph Hellwig 	if (ret < 0)
7383effdb90SChristoph Hellwig 		return ret;
7393effdb90SChristoph Hellwig 
7403effdb90SChristoph Hellwig 	if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
7413effdb90SChristoph Hellwig 		pr_err("Illegal value %d for pi_prot_type\n", flag);
7423effdb90SChristoph Hellwig 		return -EINVAL;
7433effdb90SChristoph Hellwig 	}
7443effdb90SChristoph Hellwig 	if (flag == 2) {
7453effdb90SChristoph Hellwig 		pr_err("DIF TYPE2 protection currently not supported\n");
7463effdb90SChristoph Hellwig 		return -ENOSYS;
7473effdb90SChristoph Hellwig 	}
7483effdb90SChristoph Hellwig 	if (da->hw_pi_prot_type) {
7493effdb90SChristoph Hellwig 		pr_warn("DIF protection enabled on underlying hardware,"
7503effdb90SChristoph Hellwig 			" ignoring\n");
7513effdb90SChristoph Hellwig 		return count;
7523effdb90SChristoph Hellwig 	}
7533effdb90SChristoph Hellwig 	if (!dev->transport->init_prot || !dev->transport->free_prot) {
7543effdb90SChristoph Hellwig 		/* 0 is only allowed value for non-supporting backends */
7553effdb90SChristoph Hellwig 		if (flag == 0)
756bc1a7d6aSAndy Grover 			return count;
7573effdb90SChristoph Hellwig 
7583effdb90SChristoph Hellwig 		pr_err("DIF protection not supported by backend: %s\n",
7593effdb90SChristoph Hellwig 		       dev->transport->name);
7603effdb90SChristoph Hellwig 		return -ENOSYS;
7613effdb90SChristoph Hellwig 	}
7623effdb90SChristoph Hellwig 	if (!(dev->dev_flags & DF_CONFIGURED)) {
7633effdb90SChristoph Hellwig 		pr_err("DIF protection requires device to be configured\n");
7643effdb90SChristoph Hellwig 		return -ENODEV;
7653effdb90SChristoph Hellwig 	}
7663effdb90SChristoph Hellwig 	if (dev->export_count) {
7673effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device PROT type while"
7683effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
7693effdb90SChristoph Hellwig 		return -EINVAL;
7703effdb90SChristoph Hellwig 	}
7713effdb90SChristoph Hellwig 
7723effdb90SChristoph Hellwig 	da->pi_prot_type = flag;
7733effdb90SChristoph Hellwig 
7743effdb90SChristoph Hellwig 	if (flag && !old_prot) {
7753effdb90SChristoph Hellwig 		ret = dev->transport->init_prot(dev);
7763effdb90SChristoph Hellwig 		if (ret) {
7773effdb90SChristoph Hellwig 			da->pi_prot_type = old_prot;
7783effdb90SChristoph Hellwig 			return ret;
7793effdb90SChristoph Hellwig 		}
7803effdb90SChristoph Hellwig 
7813effdb90SChristoph Hellwig 	} else if (!flag && old_prot) {
7823effdb90SChristoph Hellwig 		dev->transport->free_prot(dev);
7833effdb90SChristoph Hellwig 	}
7843effdb90SChristoph Hellwig 
7853effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
7863effdb90SChristoph Hellwig 	return count;
7873effdb90SChristoph Hellwig }
7883effdb90SChristoph Hellwig 
7892eafd729SChristoph Hellwig static ssize_t pi_prot_format_store(struct config_item *item,
7903effdb90SChristoph Hellwig 		const char *page, size_t count)
7913effdb90SChristoph Hellwig {
7922eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7933effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
7943effdb90SChristoph Hellwig 	bool flag;
7953effdb90SChristoph Hellwig 	int ret;
7963effdb90SChristoph Hellwig 
7973effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7983effdb90SChristoph Hellwig 	if (ret < 0)
7993effdb90SChristoph Hellwig 		return ret;
8003effdb90SChristoph Hellwig 
8013effdb90SChristoph Hellwig 	if (!flag)
8023effdb90SChristoph Hellwig 		return count;
8033effdb90SChristoph Hellwig 
8043effdb90SChristoph Hellwig 	if (!dev->transport->format_prot) {
8053effdb90SChristoph Hellwig 		pr_err("DIF protection format not supported by backend %s\n",
8063effdb90SChristoph Hellwig 		       dev->transport->name);
8073effdb90SChristoph Hellwig 		return -ENOSYS;
8083effdb90SChristoph Hellwig 	}
8093effdb90SChristoph Hellwig 	if (!(dev->dev_flags & DF_CONFIGURED)) {
8103effdb90SChristoph Hellwig 		pr_err("DIF protection format requires device to be configured\n");
8113effdb90SChristoph Hellwig 		return -ENODEV;
8123effdb90SChristoph Hellwig 	}
8133effdb90SChristoph Hellwig 	if (dev->export_count) {
8143effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to format SE Device PROT type while"
8153effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
8163effdb90SChristoph Hellwig 		return -EINVAL;
8173effdb90SChristoph Hellwig 	}
8183effdb90SChristoph Hellwig 
8193effdb90SChristoph Hellwig 	ret = dev->transport->format_prot(dev);
8203effdb90SChristoph Hellwig 	if (ret)
8213effdb90SChristoph Hellwig 		return ret;
8223effdb90SChristoph Hellwig 
8233effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
8243effdb90SChristoph Hellwig 	return count;
8253effdb90SChristoph Hellwig }
8263effdb90SChristoph Hellwig 
8272eafd729SChristoph Hellwig static ssize_t force_pr_aptpl_store(struct config_item *item,
8283effdb90SChristoph Hellwig 		const char *page, size_t count)
8293effdb90SChristoph Hellwig {
8302eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
8313effdb90SChristoph Hellwig 	bool flag;
8323effdb90SChristoph Hellwig 	int ret;
8333effdb90SChristoph Hellwig 
8343effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
8353effdb90SChristoph Hellwig 	if (ret < 0)
8363effdb90SChristoph Hellwig 		return ret;
8373effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
8383effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to set force_pr_aptpl while"
8393effdb90SChristoph Hellwig 		       " export_count is %d\n",
8403effdb90SChristoph Hellwig 		       da->da_dev, da->da_dev->export_count);
8413effdb90SChristoph Hellwig 		return -EINVAL;
8423effdb90SChristoph Hellwig 	}
8433effdb90SChristoph Hellwig 
8443effdb90SChristoph Hellwig 	da->force_pr_aptpl = flag;
8453effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
8463effdb90SChristoph Hellwig 	return count;
8473effdb90SChristoph Hellwig }
8483effdb90SChristoph Hellwig 
8492eafd729SChristoph Hellwig static ssize_t emulate_rest_reord_store(struct config_item *item,
8503effdb90SChristoph Hellwig 		const char *page, size_t count)
8513effdb90SChristoph Hellwig {
8522eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
8533effdb90SChristoph Hellwig 	bool flag;
8543effdb90SChristoph Hellwig 	int ret;
8553effdb90SChristoph Hellwig 
8563effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
8573effdb90SChristoph Hellwig 	if (ret < 0)
8583effdb90SChristoph Hellwig 		return ret;
8593effdb90SChristoph Hellwig 
8603effdb90SChristoph Hellwig 	if (flag != 0) {
8613effdb90SChristoph Hellwig 		printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
8623effdb90SChristoph Hellwig 			" reordering not implemented\n", da->da_dev);
8633effdb90SChristoph Hellwig 		return -ENOSYS;
8643effdb90SChristoph Hellwig 	}
8653effdb90SChristoph Hellwig 	da->emulate_rest_reord = flag;
8663effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
8673effdb90SChristoph Hellwig 		da->da_dev, flag);
8683effdb90SChristoph Hellwig 	return count;
8693effdb90SChristoph Hellwig }
8703effdb90SChristoph Hellwig 
871e6f41633SJamie Pocas static ssize_t unmap_zeroes_data_store(struct config_item *item,
872e6f41633SJamie Pocas 		const char *page, size_t count)
873e6f41633SJamie Pocas {
874e6f41633SJamie Pocas 	struct se_dev_attrib *da = to_attrib(item);
875e6f41633SJamie Pocas 	bool flag;
876e6f41633SJamie Pocas 	int ret;
877e6f41633SJamie Pocas 
878e6f41633SJamie Pocas 	ret = strtobool(page, &flag);
879e6f41633SJamie Pocas 	if (ret < 0)
880e6f41633SJamie Pocas 		return ret;
881e6f41633SJamie Pocas 
882e6f41633SJamie Pocas 	if (da->da_dev->export_count) {
883e6f41633SJamie Pocas 		pr_err("dev[%p]: Unable to change SE Device"
884e6f41633SJamie Pocas 		       " unmap_zeroes_data while export_count is %d\n",
885e6f41633SJamie Pocas 		       da->da_dev, da->da_dev->export_count);
886e6f41633SJamie Pocas 		return -EINVAL;
887e6f41633SJamie Pocas 	}
888e6f41633SJamie Pocas 	/*
889e6f41633SJamie Pocas 	 * We expect this value to be non-zero when generic Block Layer
890e6f41633SJamie Pocas 	 * Discard supported is detected iblock_configure_device().
891e6f41633SJamie Pocas 	 */
892e6f41633SJamie Pocas 	if (flag && !da->max_unmap_block_desc_count) {
893e6f41633SJamie Pocas 		pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
894e6f41633SJamie Pocas 		       " because max_unmap_block_desc_count is zero\n",
895e6f41633SJamie Pocas 		       da->da_dev);
896e6f41633SJamie Pocas 	       return -ENOSYS;
897e6f41633SJamie Pocas 	}
898e6f41633SJamie Pocas 	da->unmap_zeroes_data = flag;
899e6f41633SJamie Pocas 	pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
900e6f41633SJamie Pocas 		 da->da_dev, flag);
901e6f41633SJamie Pocas 	return 0;
902e6f41633SJamie Pocas }
903e6f41633SJamie Pocas 
9043effdb90SChristoph Hellwig /*
9053effdb90SChristoph Hellwig  * Note, this can only be called on unexported SE Device Object.
9063effdb90SChristoph Hellwig  */
9072eafd729SChristoph Hellwig static ssize_t queue_depth_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 	struct se_device *dev = da->da_dev;
9123effdb90SChristoph Hellwig 	u32 val;
9133effdb90SChristoph Hellwig 	int ret;
9143effdb90SChristoph Hellwig 
9153effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
9163effdb90SChristoph Hellwig 	if (ret < 0)
9173effdb90SChristoph Hellwig 		return ret;
9183effdb90SChristoph Hellwig 
9193effdb90SChristoph Hellwig 	if (dev->export_count) {
9203effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TCQ while"
9213effdb90SChristoph Hellwig 			" export_count is %d\n",
9223effdb90SChristoph Hellwig 			dev, dev->export_count);
9233effdb90SChristoph Hellwig 		return -EINVAL;
9243effdb90SChristoph Hellwig 	}
9253effdb90SChristoph Hellwig 	if (!val) {
9263effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
9273effdb90SChristoph Hellwig 		return -EINVAL;
9283effdb90SChristoph Hellwig 	}
9293effdb90SChristoph Hellwig 
9303effdb90SChristoph Hellwig 	if (val > dev->dev_attrib.queue_depth) {
9313effdb90SChristoph Hellwig 		if (val > dev->dev_attrib.hw_queue_depth) {
9323effdb90SChristoph Hellwig 			pr_err("dev[%p]: Passed queue_depth:"
9333effdb90SChristoph Hellwig 				" %u exceeds TCM/SE_Device MAX"
9343effdb90SChristoph Hellwig 				" TCQ: %u\n", dev, val,
9353effdb90SChristoph Hellwig 				dev->dev_attrib.hw_queue_depth);
9363effdb90SChristoph Hellwig 			return -EINVAL;
9373effdb90SChristoph Hellwig 		}
9383effdb90SChristoph Hellwig 	}
9393effdb90SChristoph Hellwig 	da->queue_depth = dev->queue_depth = val;
9403effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
9413effdb90SChristoph Hellwig 	return count;
9423effdb90SChristoph Hellwig }
9433effdb90SChristoph Hellwig 
9442eafd729SChristoph Hellwig static ssize_t optimal_sectors_store(struct config_item *item,
9453effdb90SChristoph Hellwig 		const char *page, size_t count)
9463effdb90SChristoph Hellwig {
9472eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9483effdb90SChristoph Hellwig 	u32 val;
9493effdb90SChristoph Hellwig 	int ret;
9503effdb90SChristoph Hellwig 
9513effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
9523effdb90SChristoph Hellwig 	if (ret < 0)
9533effdb90SChristoph Hellwig 		return ret;
9543effdb90SChristoph Hellwig 
9553effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
9563effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
9573effdb90SChristoph Hellwig 			" optimal_sectors while export_count is %d\n",
9583effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
9593effdb90SChristoph Hellwig 		return -EINVAL;
9603effdb90SChristoph Hellwig 	}
9613effdb90SChristoph Hellwig 	if (val > da->hw_max_sectors) {
9623effdb90SChristoph Hellwig 		pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
9633effdb90SChristoph Hellwig 			" greater than hw_max_sectors: %u\n",
9643effdb90SChristoph Hellwig 			da->da_dev, val, da->hw_max_sectors);
9653effdb90SChristoph Hellwig 		return -EINVAL;
9663effdb90SChristoph Hellwig 	}
9673effdb90SChristoph Hellwig 
9683effdb90SChristoph Hellwig 	da->optimal_sectors = val;
9693effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
9703effdb90SChristoph Hellwig 			da->da_dev, val);
9713effdb90SChristoph Hellwig 	return count;
9723effdb90SChristoph Hellwig }
9733effdb90SChristoph Hellwig 
9742eafd729SChristoph Hellwig static ssize_t block_size_store(struct config_item *item,
9753effdb90SChristoph Hellwig 		const char *page, size_t count)
9763effdb90SChristoph Hellwig {
9772eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9783effdb90SChristoph Hellwig 	u32 val;
9793effdb90SChristoph Hellwig 	int ret;
9803effdb90SChristoph Hellwig 
9813effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
9823effdb90SChristoph Hellwig 	if (ret < 0)
9833effdb90SChristoph Hellwig 		return ret;
9843effdb90SChristoph Hellwig 
9853effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
9863effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device block_size"
9873effdb90SChristoph Hellwig 			" while export_count is %d\n",
9883effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
9893effdb90SChristoph Hellwig 		return -EINVAL;
9903effdb90SChristoph Hellwig 	}
9913effdb90SChristoph Hellwig 
9923effdb90SChristoph Hellwig 	if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
9933effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal value for block_device: %u"
9943effdb90SChristoph Hellwig 			" for SE device, must be 512, 1024, 2048 or 4096\n",
9953effdb90SChristoph Hellwig 			da->da_dev, val);
9963effdb90SChristoph Hellwig 		return -EINVAL;
9973effdb90SChristoph Hellwig 	}
9983effdb90SChristoph Hellwig 
9993effdb90SChristoph Hellwig 	da->block_size = val;
10003effdb90SChristoph Hellwig 	if (da->max_bytes_per_io)
10013effdb90SChristoph Hellwig 		da->hw_max_sectors = da->max_bytes_per_io / val;
10023effdb90SChristoph Hellwig 
10033effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device block_size changed to %u\n",
10043effdb90SChristoph Hellwig 			da->da_dev, val);
10053effdb90SChristoph Hellwig 	return count;
10063effdb90SChristoph Hellwig }
10075873c4d1SChristoph Hellwig 
10082eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_model_alias);
10092eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_dpo);
10102eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_write);
10112eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_read);
10122eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_write_cache);
10132eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
10142eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tas);
10152eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpu);
10162eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpws);
10172eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_caw);
10182eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_3pc);
10192eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_type);
10202eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_pi_prot_type);
10212eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_format);
10222eafd729SChristoph Hellwig CONFIGFS_ATTR(, enforce_pr_isids);
10232eafd729SChristoph Hellwig CONFIGFS_ATTR(, is_nonrot);
10242eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_rest_reord);
10252eafd729SChristoph Hellwig CONFIGFS_ATTR(, force_pr_aptpl);
10262eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_block_size);
10272eafd729SChristoph Hellwig CONFIGFS_ATTR(, block_size);
10282eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_max_sectors);
10292eafd729SChristoph Hellwig CONFIGFS_ATTR(, optimal_sectors);
10302eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_queue_depth);
10312eafd729SChristoph Hellwig CONFIGFS_ATTR(, queue_depth);
10322eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_lba_count);
10332eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_block_desc_count);
10342eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity);
10352eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity_alignment);
1036e6f41633SJamie Pocas CONFIGFS_ATTR(, unmap_zeroes_data);
10372eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_write_same_len);
1038c66ac9dbSNicholas Bellinger 
10395873c4d1SChristoph Hellwig /*
10405873c4d1SChristoph Hellwig  * dev_attrib attributes for devices using the target core SBC/SPC
10415873c4d1SChristoph Hellwig  * interpreter.  Any backend using spc_parse_cdb should be using
10425873c4d1SChristoph Hellwig  * these.
10435873c4d1SChristoph Hellwig  */
10445873c4d1SChristoph Hellwig struct configfs_attribute *sbc_attrib_attrs[] = {
10452eafd729SChristoph Hellwig 	&attr_emulate_model_alias,
10462eafd729SChristoph Hellwig 	&attr_emulate_dpo,
10472eafd729SChristoph Hellwig 	&attr_emulate_fua_write,
10482eafd729SChristoph Hellwig 	&attr_emulate_fua_read,
10492eafd729SChristoph Hellwig 	&attr_emulate_write_cache,
10502eafd729SChristoph Hellwig 	&attr_emulate_ua_intlck_ctrl,
10512eafd729SChristoph Hellwig 	&attr_emulate_tas,
10522eafd729SChristoph Hellwig 	&attr_emulate_tpu,
10532eafd729SChristoph Hellwig 	&attr_emulate_tpws,
10542eafd729SChristoph Hellwig 	&attr_emulate_caw,
10552eafd729SChristoph Hellwig 	&attr_emulate_3pc,
10562eafd729SChristoph Hellwig 	&attr_pi_prot_type,
10572eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
10582eafd729SChristoph Hellwig 	&attr_pi_prot_format,
10592eafd729SChristoph Hellwig 	&attr_enforce_pr_isids,
10602eafd729SChristoph Hellwig 	&attr_is_nonrot,
10612eafd729SChristoph Hellwig 	&attr_emulate_rest_reord,
10622eafd729SChristoph Hellwig 	&attr_force_pr_aptpl,
10632eafd729SChristoph Hellwig 	&attr_hw_block_size,
10642eafd729SChristoph Hellwig 	&attr_block_size,
10652eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
10662eafd729SChristoph Hellwig 	&attr_optimal_sectors,
10672eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
10682eafd729SChristoph Hellwig 	&attr_queue_depth,
10692eafd729SChristoph Hellwig 	&attr_max_unmap_lba_count,
10702eafd729SChristoph Hellwig 	&attr_max_unmap_block_desc_count,
10712eafd729SChristoph Hellwig 	&attr_unmap_granularity,
10722eafd729SChristoph Hellwig 	&attr_unmap_granularity_alignment,
1073e6f41633SJamie Pocas 	&attr_unmap_zeroes_data,
10742eafd729SChristoph Hellwig 	&attr_max_write_same_len,
10755873c4d1SChristoph Hellwig 	NULL,
10765873c4d1SChristoph Hellwig };
10775873c4d1SChristoph Hellwig EXPORT_SYMBOL(sbc_attrib_attrs);
10785873c4d1SChristoph Hellwig 
10795873c4d1SChristoph Hellwig /*
10805873c4d1SChristoph Hellwig  * Minimal dev_attrib attributes for devices passing through CDBs.
10815873c4d1SChristoph Hellwig  * In this case we only provide a few read-only attributes for
10825873c4d1SChristoph Hellwig  * backwards compatibility.
10835873c4d1SChristoph Hellwig  */
10845873c4d1SChristoph Hellwig struct configfs_attribute *passthrough_attrib_attrs[] = {
10852eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
10862eafd729SChristoph Hellwig 	&attr_hw_block_size,
10872eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
10882eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
10895873c4d1SChristoph Hellwig 	NULL,
10905873c4d1SChristoph Hellwig };
10915873c4d1SChristoph Hellwig EXPORT_SYMBOL(passthrough_attrib_attrs);
10925873c4d1SChristoph Hellwig 
10932eafd729SChristoph Hellwig TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
1094c66ac9dbSNicholas Bellinger 
1095f79a897eSNicholas Bellinger /* End functions for struct config_item_type tb_dev_attrib_cit */
1096c66ac9dbSNicholas Bellinger 
1097f8d389c6SNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_wwn_cit */
1098c66ac9dbSNicholas Bellinger 
10992eafd729SChristoph Hellwig static struct t10_wwn *to_t10_wwn(struct config_item *item)
11002eafd729SChristoph Hellwig {
11012eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
11022eafd729SChristoph Hellwig }
1103c66ac9dbSNicholas Bellinger 
1104c66ac9dbSNicholas Bellinger /*
1105c66ac9dbSNicholas Bellinger  * VPD page 0x80 Unit serial
1106c66ac9dbSNicholas Bellinger  */
11072eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1108c66ac9dbSNicholas Bellinger 		char *page)
1109c66ac9dbSNicholas Bellinger {
1110c66ac9dbSNicholas Bellinger 	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
11112eafd729SChristoph Hellwig 		&to_t10_wwn(item)->unit_serial[0]);
1112c66ac9dbSNicholas Bellinger }
1113c66ac9dbSNicholas Bellinger 
11142eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
11152eafd729SChristoph Hellwig 		const char *page, size_t count)
1116c66ac9dbSNicholas Bellinger {
11172eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
11180fd97ccfSChristoph Hellwig 	struct se_device *dev = t10_wwn->t10_dev;
1119c66ac9dbSNicholas Bellinger 	unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1120c66ac9dbSNicholas Bellinger 
1121c66ac9dbSNicholas Bellinger 	/*
1122c66ac9dbSNicholas Bellinger 	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1123c66ac9dbSNicholas Bellinger 	 * from the struct scsi_device level firmware, do not allow
1124c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial to be emulated.
1125c66ac9dbSNicholas Bellinger 	 *
1126c66ac9dbSNicholas Bellinger 	 * Note this struct scsi_device could also be emulating VPD
1127c66ac9dbSNicholas Bellinger 	 * information from its drivers/scsi LLD.  But for now we assume
1128c66ac9dbSNicholas Bellinger 	 * it is doing 'the right thing' wrt a world wide unique
1129c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial Number that OS dependent multipath can depend on.
1130c66ac9dbSNicholas Bellinger 	 */
11310fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
11326708bb27SAndy Grover 		pr_err("Underlying SCSI device firmware provided VPD"
1133c66ac9dbSNicholas Bellinger 			" Unit Serial, ignoring request\n");
1134c66ac9dbSNicholas Bellinger 		return -EOPNOTSUPP;
1135c66ac9dbSNicholas Bellinger 	}
1136c66ac9dbSNicholas Bellinger 
113760d645a4SDan Carpenter 	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
11386708bb27SAndy Grover 		pr_err("Emulated VPD Unit Serial exceeds"
1139c66ac9dbSNicholas Bellinger 		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1140c66ac9dbSNicholas Bellinger 		return -EOVERFLOW;
1141c66ac9dbSNicholas Bellinger 	}
1142c66ac9dbSNicholas Bellinger 	/*
1143c66ac9dbSNicholas Bellinger 	 * Check to see if any active $FABRIC_MOD exports exist.  If they
1144c66ac9dbSNicholas Bellinger 	 * do exist, fail here as changing this information on the fly
1145c66ac9dbSNicholas Bellinger 	 * (underneath the initiator side OS dependent multipath code)
1146c66ac9dbSNicholas Bellinger 	 * could cause negative effects.
1147c66ac9dbSNicholas Bellinger 	 */
11480fd97ccfSChristoph Hellwig 	if (dev->export_count) {
11496708bb27SAndy Grover 		pr_err("Unable to set VPD Unit Serial while"
1150c66ac9dbSNicholas Bellinger 			" active %d $FABRIC_MOD exports exist\n",
11510fd97ccfSChristoph Hellwig 			dev->export_count);
1152c66ac9dbSNicholas Bellinger 		return -EINVAL;
1153c66ac9dbSNicholas Bellinger 	}
11540fd97ccfSChristoph Hellwig 
1155c66ac9dbSNicholas Bellinger 	/*
1156c66ac9dbSNicholas Bellinger 	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1157c66ac9dbSNicholas Bellinger 	 *
1158c66ac9dbSNicholas Bellinger 	 * Also, strip any newline added from the userspace
1159c66ac9dbSNicholas Bellinger 	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1160c66ac9dbSNicholas Bellinger 	 */
1161c66ac9dbSNicholas Bellinger 	memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1162c66ac9dbSNicholas Bellinger 	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
11630fd97ccfSChristoph Hellwig 	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1164c66ac9dbSNicholas Bellinger 			"%s", strstrip(buf));
11650fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1166c66ac9dbSNicholas Bellinger 
11676708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
11680fd97ccfSChristoph Hellwig 			" %s\n", dev->t10_wwn.unit_serial);
1169c66ac9dbSNicholas Bellinger 
1170c66ac9dbSNicholas Bellinger 	return count;
1171c66ac9dbSNicholas Bellinger }
1172c66ac9dbSNicholas Bellinger 
1173c66ac9dbSNicholas Bellinger /*
1174c66ac9dbSNicholas Bellinger  * VPD page 0x83 Protocol Identifier
1175c66ac9dbSNicholas Bellinger  */
11762eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1177c66ac9dbSNicholas Bellinger 		char *page)
1178c66ac9dbSNicholas Bellinger {
11792eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1180c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;
1181c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];
1182c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1183c66ac9dbSNicholas Bellinger 
1184c66ac9dbSNicholas Bellinger 	memset(buf, 0, VPD_TMP_BUF_SIZE);
1185c66ac9dbSNicholas Bellinger 
1186c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);
1187c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
11886708bb27SAndy Grover 		if (!vpd->protocol_identifier_set)
1189c66ac9dbSNicholas Bellinger 			continue;
1190c66ac9dbSNicholas Bellinger 
1191c66ac9dbSNicholas Bellinger 		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1192c66ac9dbSNicholas Bellinger 
11936708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1194c66ac9dbSNicholas Bellinger 			break;
1195c66ac9dbSNicholas Bellinger 
1196c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1197c66ac9dbSNicholas Bellinger 	}
1198c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);
1199c66ac9dbSNicholas Bellinger 
1200c66ac9dbSNicholas Bellinger 	return len;
1201c66ac9dbSNicholas Bellinger }
1202c66ac9dbSNicholas Bellinger 
1203c66ac9dbSNicholas Bellinger /*
1204c66ac9dbSNicholas Bellinger  * Generic wrapper for dumping VPD identifiers by association.
1205c66ac9dbSNicholas Bellinger  */
1206c66ac9dbSNicholas Bellinger #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
12072eafd729SChristoph Hellwig static ssize_t target_wwn_##_name##_show(struct config_item *item,	\
1208c66ac9dbSNicholas Bellinger 		char *page)						\
1209c66ac9dbSNicholas Bellinger {									\
12102eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);			\
1211c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;						\
1212c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];				\
1213c66ac9dbSNicholas Bellinger 	ssize_t len = 0;						\
1214c66ac9dbSNicholas Bellinger 									\
1215c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);				\
1216c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
1217c66ac9dbSNicholas Bellinger 		if (vpd->association != _assoc)				\
1218c66ac9dbSNicholas Bellinger 			continue;					\
1219c66ac9dbSNicholas Bellinger 									\
1220c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1221c66ac9dbSNicholas Bellinger 		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
12226708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1223c66ac9dbSNicholas Bellinger 			break;						\
1224c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1225c66ac9dbSNicholas Bellinger 									\
1226c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1227c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
12286708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1229c66ac9dbSNicholas Bellinger 			break;						\
1230c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1231c66ac9dbSNicholas Bellinger 									\
1232c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1233c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
12346708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1235c66ac9dbSNicholas Bellinger 			break;						\
1236c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1237c66ac9dbSNicholas Bellinger 	}								\
1238c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);				\
1239c66ac9dbSNicholas Bellinger 									\
1240c66ac9dbSNicholas Bellinger 	return len;							\
1241c66ac9dbSNicholas Bellinger }
1242c66ac9dbSNicholas Bellinger 
12432eafd729SChristoph Hellwig /* VPD page 0x83 Association: Logical Unit */
1244c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
12452eafd729SChristoph Hellwig /* VPD page 0x83 Association: Target Port */
1246c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
12472eafd729SChristoph Hellwig /* VPD page 0x83 Association: SCSI Target Device */
1248c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1249c66ac9dbSNicholas Bellinger 
12502eafd729SChristoph Hellwig CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
12512eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
12522eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
12532eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
12542eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1255c66ac9dbSNicholas Bellinger 
1256c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
12572eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_unit_serial,
12582eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_protocol_identifier,
12592eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_logical_unit,
12602eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_target_port,
12612eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_scsi_target_device,
1262c66ac9dbSNicholas Bellinger 	NULL,
1263c66ac9dbSNicholas Bellinger };
1264c66ac9dbSNicholas Bellinger 
12652eafd729SChristoph Hellwig TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1266c66ac9dbSNicholas Bellinger 
1267f8d389c6SNicholas Bellinger /*  End functions for struct config_item_type tb_dev_wwn_cit */
1268c66ac9dbSNicholas Bellinger 
126991e2e39bSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_pr_cit */
1270c66ac9dbSNicholas Bellinger 
12712eafd729SChristoph Hellwig static struct se_device *pr_to_dev(struct config_item *item)
12722eafd729SChristoph Hellwig {
12732eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device,
12742eafd729SChristoph Hellwig 			dev_pr_group);
12752eafd729SChristoph Hellwig }
1276c66ac9dbSNicholas Bellinger 
1277d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1278d977f437SChristoph Hellwig 		char *page)
1279c66ac9dbSNicholas Bellinger {
1280c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1281c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1282c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1283c66ac9dbSNicholas Bellinger 
1284c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1285c66ac9dbSNicholas Bellinger 
1286c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1287d977f437SChristoph Hellwig 	if (!pr_reg)
1288d977f437SChristoph Hellwig 		return sprintf(page, "No SPC-3 Reservation holder\n");
1289d977f437SChristoph Hellwig 
1290c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1291d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1292c66ac9dbSNicholas Bellinger 
1293d977f437SChristoph Hellwig 	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
1294e3d6f909SAndy Grover 		se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1295d2843c17SAndy Grover 		se_nacl->initiatorname, i_buf);
1296c66ac9dbSNicholas Bellinger }
1297c66ac9dbSNicholas Bellinger 
1298d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1299d977f437SChristoph Hellwig 		char *page)
1300c66ac9dbSNicholas Bellinger {
1301c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1302d977f437SChristoph Hellwig 	ssize_t len;
1303c66ac9dbSNicholas Bellinger 
1304c66ac9dbSNicholas Bellinger 	se_nacl = dev->dev_reserved_node_acl;
1305d977f437SChristoph Hellwig 	if (se_nacl) {
1306d977f437SChristoph Hellwig 		len = sprintf(page,
1307d977f437SChristoph Hellwig 			      "SPC-2 Reservation: %s Initiator: %s\n",
1308e3d6f909SAndy Grover 			      se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1309c66ac9dbSNicholas Bellinger 			      se_nacl->initiatorname);
1310d977f437SChristoph Hellwig 	} else {
1311d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-2 Reservation holder\n");
1312d977f437SChristoph Hellwig 	}
1313d977f437SChristoph Hellwig 	return len;
1314c66ac9dbSNicholas Bellinger }
1315c66ac9dbSNicholas Bellinger 
13162eafd729SChristoph Hellwig static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1317c66ac9dbSNicholas Bellinger {
13182eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1319d977f437SChristoph Hellwig 	int ret;
1320c66ac9dbSNicholas Bellinger 
1321a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1322d977f437SChristoph Hellwig 		return sprintf(page, "Passthrough\n");
1323c66ac9dbSNicholas Bellinger 
1324d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
1325d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1326d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc2_res(dev, page);
1327d977f437SChristoph Hellwig 	else
1328d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc3_res(dev, page);
1329d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1330d977f437SChristoph Hellwig 	return ret;
1331c66ac9dbSNicholas Bellinger }
1332c66ac9dbSNicholas Bellinger 
13332eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
13342eafd729SChristoph Hellwig 		char *page)
1335c66ac9dbSNicholas Bellinger {
13362eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1337c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1338c66ac9dbSNicholas Bellinger 
1339c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1340d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder) {
1341c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1342d977f437SChristoph Hellwig 	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1343c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: All Target"
1344c66ac9dbSNicholas Bellinger 			" Ports registration\n");
1345d977f437SChristoph Hellwig 	} else {
1346c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: Single"
1347c66ac9dbSNicholas Bellinger 			" Target Port registration\n");
1348d977f437SChristoph Hellwig 	}
1349c66ac9dbSNicholas Bellinger 
1350d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1351c66ac9dbSNicholas Bellinger 	return len;
1352c66ac9dbSNicholas Bellinger }
1353c66ac9dbSNicholas Bellinger 
13542eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
13552eafd729SChristoph Hellwig 		char *page)
1356c66ac9dbSNicholas Bellinger {
13572eafd729SChristoph Hellwig 	return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1358c66ac9dbSNicholas Bellinger }
1359c66ac9dbSNicholas Bellinger 
1360c66ac9dbSNicholas Bellinger 
13612eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
13622eafd729SChristoph Hellwig 		char *page)
1363c66ac9dbSNicholas Bellinger {
13642eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1365c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1366c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
1367c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
13689ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1369c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1370c66ac9dbSNicholas Bellinger 
1371c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1372c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
13736708bb27SAndy Grover 	if (!pr_reg) {
1374c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1375d977f437SChristoph Hellwig 		goto out_unlock;
1376c66ac9dbSNicholas Bellinger 	}
1377d977f437SChristoph Hellwig 
1378c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1379c66ac9dbSNicholas Bellinger 	se_tpg = se_nacl->se_tpg;
1380e3d6f909SAndy Grover 	tfo = se_tpg->se_tpg_tfo;
1381c66ac9dbSNicholas Bellinger 
1382c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: %s"
1383c66ac9dbSNicholas Bellinger 		" Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1384c66ac9dbSNicholas Bellinger 		tfo->tpg_get_wwn(se_tpg));
1385c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
138635d1efe8SMasanari Iida 		" Identifier Tag: %hu %s Portal Group Tag: %hu"
1387f2d30680SHannes Reinecke 		" %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
1388c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
138979dc9c9eSNicholas Bellinger 		tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
1390c66ac9dbSNicholas Bellinger 
1391d977f437SChristoph Hellwig out_unlock:
1392d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1393c66ac9dbSNicholas Bellinger 	return len;
1394c66ac9dbSNicholas Bellinger }
1395c66ac9dbSNicholas Bellinger 
1396c66ac9dbSNicholas Bellinger 
13972eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
13982eafd729SChristoph Hellwig 		char *page)
1399c66ac9dbSNicholas Bellinger {
14002eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
14019ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1402c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1403c66ac9dbSNicholas Bellinger 	unsigned char buf[384];
1404c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1405c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1406d2843c17SAndy Grover 	int reg_count = 0;
1407c66ac9dbSNicholas Bellinger 
1408c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1409c66ac9dbSNicholas Bellinger 
14100fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
14110fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1412c66ac9dbSNicholas Bellinger 			pr_reg_list) {
1413c66ac9dbSNicholas Bellinger 
1414c66ac9dbSNicholas Bellinger 		memset(buf, 0, 384);
1415c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1416c66ac9dbSNicholas Bellinger 		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1417d2843c17SAndy Grover 		core_pr_dump_initiator_port(pr_reg, i_buf,
1418c66ac9dbSNicholas Bellinger 					PR_REG_ISID_ID_LEN);
1419c66ac9dbSNicholas Bellinger 		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1420c66ac9dbSNicholas Bellinger 			tfo->get_fabric_name(),
1421d2843c17SAndy Grover 			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1422c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_generation);
1423c66ac9dbSNicholas Bellinger 
14246708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1425c66ac9dbSNicholas Bellinger 			break;
1426c66ac9dbSNicholas Bellinger 
1427c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1428c66ac9dbSNicholas Bellinger 		reg_count++;
1429c66ac9dbSNicholas Bellinger 	}
14300fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
1431c66ac9dbSNicholas Bellinger 
14326708bb27SAndy Grover 	if (!reg_count)
1433c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "None\n");
1434c66ac9dbSNicholas Bellinger 
1435c66ac9dbSNicholas Bellinger 	return len;
1436c66ac9dbSNicholas Bellinger }
1437c66ac9dbSNicholas Bellinger 
14382eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1439c66ac9dbSNicholas Bellinger {
14402eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1441c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1442c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1443c66ac9dbSNicholas Bellinger 
1444c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1445c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1446d977f437SChristoph Hellwig 	if (pr_reg) {
1447c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1448c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1449d977f437SChristoph Hellwig 	} else {
1450d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1451d977f437SChristoph Hellwig 	}
1452c66ac9dbSNicholas Bellinger 
1453d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1454c66ac9dbSNicholas Bellinger 	return len;
1455c66ac9dbSNicholas Bellinger }
1456c66ac9dbSNicholas Bellinger 
14572eafd729SChristoph Hellwig static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
1458c66ac9dbSNicholas Bellinger {
14592eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
14602eafd729SChristoph Hellwig 
1461a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1462d977f437SChristoph Hellwig 		return sprintf(page, "SPC_PASSTHROUGH\n");
1463d977f437SChristoph Hellwig 	else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1464d977f437SChristoph Hellwig 		return sprintf(page, "SPC2_RESERVATIONS\n");
1465d977f437SChristoph Hellwig 	else
1466d977f437SChristoph Hellwig 		return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1467c66ac9dbSNicholas Bellinger }
1468c66ac9dbSNicholas Bellinger 
14692eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
14702eafd729SChristoph Hellwig 		char *page)
1471c66ac9dbSNicholas Bellinger {
14722eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
14732eafd729SChristoph Hellwig 
1474a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1475c66ac9dbSNicholas Bellinger 		return 0;
1476c66ac9dbSNicholas Bellinger 
1477c66ac9dbSNicholas Bellinger 	return sprintf(page, "APTPL Bit Status: %s\n",
14780fd97ccfSChristoph Hellwig 		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1479c66ac9dbSNicholas Bellinger }
1480c66ac9dbSNicholas Bellinger 
14812eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
14822eafd729SChristoph Hellwig 		char *page)
1483c66ac9dbSNicholas Bellinger {
14842eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
14852eafd729SChristoph Hellwig 
1486a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1487c66ac9dbSNicholas Bellinger 		return 0;
1488c66ac9dbSNicholas Bellinger 
1489c66ac9dbSNicholas Bellinger 	return sprintf(page, "Ready to process PR APTPL metadata..\n");
1490c66ac9dbSNicholas Bellinger }
1491c66ac9dbSNicholas Bellinger 
1492c66ac9dbSNicholas Bellinger enum {
1493c66ac9dbSNicholas Bellinger 	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1494c66ac9dbSNicholas Bellinger 	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1495c66ac9dbSNicholas Bellinger 	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1496c66ac9dbSNicholas Bellinger 	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1497c66ac9dbSNicholas Bellinger };
1498c66ac9dbSNicholas Bellinger 
1499c66ac9dbSNicholas Bellinger static match_table_t tokens = {
1500c66ac9dbSNicholas Bellinger 	{Opt_initiator_fabric, "initiator_fabric=%s"},
1501c66ac9dbSNicholas Bellinger 	{Opt_initiator_node, "initiator_node=%s"},
1502c66ac9dbSNicholas Bellinger 	{Opt_initiator_sid, "initiator_sid=%s"},
1503c66ac9dbSNicholas Bellinger 	{Opt_sa_res_key, "sa_res_key=%s"},
1504c66ac9dbSNicholas Bellinger 	{Opt_res_holder, "res_holder=%d"},
1505c66ac9dbSNicholas Bellinger 	{Opt_res_type, "res_type=%d"},
1506c66ac9dbSNicholas Bellinger 	{Opt_res_scope, "res_scope=%d"},
1507c66ac9dbSNicholas Bellinger 	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1508f2d30680SHannes Reinecke 	{Opt_mapped_lun, "mapped_lun=%lld"},
1509c66ac9dbSNicholas Bellinger 	{Opt_target_fabric, "target_fabric=%s"},
1510c66ac9dbSNicholas Bellinger 	{Opt_target_node, "target_node=%s"},
1511c66ac9dbSNicholas Bellinger 	{Opt_tpgt, "tpgt=%d"},
1512c66ac9dbSNicholas Bellinger 	{Opt_port_rtpi, "port_rtpi=%d"},
1513f2d30680SHannes Reinecke 	{Opt_target_lun, "target_lun=%lld"},
1514c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
1515c66ac9dbSNicholas Bellinger };
1516c66ac9dbSNicholas Bellinger 
15172eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
15182eafd729SChristoph Hellwig 		const char *page, size_t count)
1519c66ac9dbSNicholas Bellinger {
15202eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
15216d180253SJesper Juhl 	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
15226d180253SJesper Juhl 	unsigned char *t_fabric = NULL, *t_port = NULL;
15238d213559SJoern Engel 	char *orig, *ptr, *opts;
1524c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
1525c66ac9dbSNicholas Bellinger 	unsigned long long tmp_ll;
1526c66ac9dbSNicholas Bellinger 	u64 sa_res_key = 0;
1527f2d30680SHannes Reinecke 	u64 mapped_lun = 0, target_lun = 0;
1528c66ac9dbSNicholas Bellinger 	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
152945fb94c2SBart Van Assche 	u16 tpgt = 0;
153045fb94c2SBart Van Assche 	u8 type = 0;
1531c66ac9dbSNicholas Bellinger 
1532a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
15339105bfc0SAndy Grover 		return count;
1534d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
15359105bfc0SAndy Grover 		return count;
1536c66ac9dbSNicholas Bellinger 
15370fd97ccfSChristoph Hellwig 	if (dev->export_count) {
15386708bb27SAndy Grover 		pr_debug("Unable to process APTPL metadata while"
1539c66ac9dbSNicholas Bellinger 			" active fabric exports exist\n");
1540c66ac9dbSNicholas Bellinger 		return -EINVAL;
1541c66ac9dbSNicholas Bellinger 	}
1542c66ac9dbSNicholas Bellinger 
1543c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
1544c66ac9dbSNicholas Bellinger 	if (!opts)
1545c66ac9dbSNicholas Bellinger 		return -ENOMEM;
1546c66ac9dbSNicholas Bellinger 
1547c66ac9dbSNicholas Bellinger 	orig = opts;
154890c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
1549c66ac9dbSNicholas Bellinger 		if (!*ptr)
1550c66ac9dbSNicholas Bellinger 			continue;
1551c66ac9dbSNicholas Bellinger 
1552c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
1553c66ac9dbSNicholas Bellinger 		switch (token) {
1554c66ac9dbSNicholas Bellinger 		case Opt_initiator_fabric:
15558d213559SJoern Engel 			i_fabric = match_strdup(args);
15566d180253SJesper Juhl 			if (!i_fabric) {
15576d180253SJesper Juhl 				ret = -ENOMEM;
15586d180253SJesper Juhl 				goto out;
15596d180253SJesper Juhl 			}
1560c66ac9dbSNicholas Bellinger 			break;
1561c66ac9dbSNicholas Bellinger 		case Opt_initiator_node:
15628d213559SJoern Engel 			i_port = match_strdup(args);
15636d180253SJesper Juhl 			if (!i_port) {
15646d180253SJesper Juhl 				ret = -ENOMEM;
15656d180253SJesper Juhl 				goto out;
15666d180253SJesper Juhl 			}
156760d645a4SDan Carpenter 			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
15686708bb27SAndy Grover 				pr_err("APTPL metadata initiator_node="
1569c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1570c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_IPORT_LEN);
1571c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1572c66ac9dbSNicholas Bellinger 				break;
1573c66ac9dbSNicholas Bellinger 			}
1574c66ac9dbSNicholas Bellinger 			break;
1575c66ac9dbSNicholas Bellinger 		case Opt_initiator_sid:
15768d213559SJoern Engel 			isid = match_strdup(args);
15776d180253SJesper Juhl 			if (!isid) {
15786d180253SJesper Juhl 				ret = -ENOMEM;
15796d180253SJesper Juhl 				goto out;
15806d180253SJesper Juhl 			}
158160d645a4SDan Carpenter 			if (strlen(isid) >= PR_REG_ISID_LEN) {
15826708bb27SAndy Grover 				pr_err("APTPL metadata initiator_isid"
1583c66ac9dbSNicholas Bellinger 					"= exceeds PR_REG_ISID_LEN: %d\n",
1584c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
1585c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1586c66ac9dbSNicholas Bellinger 				break;
1587c66ac9dbSNicholas Bellinger 			}
1588c66ac9dbSNicholas Bellinger 			break;
1589c66ac9dbSNicholas Bellinger 		case Opt_sa_res_key:
15908d213559SJoern Engel 			ret = kstrtoull(args->from, 0, &tmp_ll);
1591c66ac9dbSNicholas Bellinger 			if (ret < 0) {
15928d213559SJoern Engel 				pr_err("kstrtoull() failed for sa_res_key=\n");
1593c66ac9dbSNicholas Bellinger 				goto out;
1594c66ac9dbSNicholas Bellinger 			}
1595c66ac9dbSNicholas Bellinger 			sa_res_key = (u64)tmp_ll;
1596c66ac9dbSNicholas Bellinger 			break;
1597c66ac9dbSNicholas Bellinger 		/*
1598c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Reservation
1599c66ac9dbSNicholas Bellinger 		 */
1600c66ac9dbSNicholas Bellinger 		case Opt_res_holder:
1601c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1602c2091026SDavid Disseldorp 			if (ret)
1603c2091026SDavid Disseldorp 				goto out;
1604c66ac9dbSNicholas Bellinger 			res_holder = arg;
1605c66ac9dbSNicholas Bellinger 			break;
1606c66ac9dbSNicholas Bellinger 		case Opt_res_type:
1607c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1608c2091026SDavid Disseldorp 			if (ret)
1609c2091026SDavid Disseldorp 				goto out;
1610c66ac9dbSNicholas Bellinger 			type = (u8)arg;
1611c66ac9dbSNicholas Bellinger 			break;
1612c66ac9dbSNicholas Bellinger 		case Opt_res_scope:
1613c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1614c2091026SDavid Disseldorp 			if (ret)
1615c2091026SDavid Disseldorp 				goto out;
1616c66ac9dbSNicholas Bellinger 			break;
1617c66ac9dbSNicholas Bellinger 		case Opt_res_all_tg_pt:
1618c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1619c2091026SDavid Disseldorp 			if (ret)
1620c2091026SDavid Disseldorp 				goto out;
1621c66ac9dbSNicholas Bellinger 			all_tg_pt = (int)arg;
1622c66ac9dbSNicholas Bellinger 			break;
1623c66ac9dbSNicholas Bellinger 		case Opt_mapped_lun:
1624c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1625c2091026SDavid Disseldorp 			if (ret)
1626c2091026SDavid Disseldorp 				goto out;
1627f2d30680SHannes Reinecke 			mapped_lun = (u64)arg;
1628c66ac9dbSNicholas Bellinger 			break;
1629c66ac9dbSNicholas Bellinger 		/*
1630c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Target Port
1631c66ac9dbSNicholas Bellinger 		 */
1632c66ac9dbSNicholas Bellinger 		case Opt_target_fabric:
16338d213559SJoern Engel 			t_fabric = match_strdup(args);
16346d180253SJesper Juhl 			if (!t_fabric) {
16356d180253SJesper Juhl 				ret = -ENOMEM;
16366d180253SJesper Juhl 				goto out;
16376d180253SJesper Juhl 			}
1638c66ac9dbSNicholas Bellinger 			break;
1639c66ac9dbSNicholas Bellinger 		case Opt_target_node:
16408d213559SJoern Engel 			t_port = match_strdup(args);
16416d180253SJesper Juhl 			if (!t_port) {
16426d180253SJesper Juhl 				ret = -ENOMEM;
16436d180253SJesper Juhl 				goto out;
16446d180253SJesper Juhl 			}
164560d645a4SDan Carpenter 			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
16466708bb27SAndy Grover 				pr_err("APTPL metadata target_node="
1647c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1648c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_TPORT_LEN);
1649c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1650c66ac9dbSNicholas Bellinger 				break;
1651c66ac9dbSNicholas Bellinger 			}
1652c66ac9dbSNicholas Bellinger 			break;
1653c66ac9dbSNicholas Bellinger 		case Opt_tpgt:
1654c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1655c2091026SDavid Disseldorp 			if (ret)
1656c2091026SDavid Disseldorp 				goto out;
1657c66ac9dbSNicholas Bellinger 			tpgt = (u16)arg;
1658c66ac9dbSNicholas Bellinger 			break;
1659c66ac9dbSNicholas Bellinger 		case Opt_port_rtpi:
1660c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1661c2091026SDavid Disseldorp 			if (ret)
1662c2091026SDavid Disseldorp 				goto out;
1663c66ac9dbSNicholas Bellinger 			break;
1664c66ac9dbSNicholas Bellinger 		case Opt_target_lun:
1665c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1666c2091026SDavid Disseldorp 			if (ret)
1667c2091026SDavid Disseldorp 				goto out;
1668f2d30680SHannes Reinecke 			target_lun = (u64)arg;
1669c66ac9dbSNicholas Bellinger 			break;
1670c66ac9dbSNicholas Bellinger 		default:
1671c66ac9dbSNicholas Bellinger 			break;
1672c66ac9dbSNicholas Bellinger 		}
1673c66ac9dbSNicholas Bellinger 	}
1674c66ac9dbSNicholas Bellinger 
16756708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
16766708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
1677c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1678c66ac9dbSNicholas Bellinger 		goto out;
1679c66ac9dbSNicholas Bellinger 	}
1680c66ac9dbSNicholas Bellinger 
1681c66ac9dbSNicholas Bellinger 	if (res_holder && !(type)) {
16826708bb27SAndy Grover 		pr_err("Illegal PR type: 0x%02x for reservation"
1683c66ac9dbSNicholas Bellinger 				" holder\n", type);
1684c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1685c66ac9dbSNicholas Bellinger 		goto out;
1686c66ac9dbSNicholas Bellinger 	}
1687c66ac9dbSNicholas Bellinger 
16880fd97ccfSChristoph Hellwig 	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1689c66ac9dbSNicholas Bellinger 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1690c66ac9dbSNicholas Bellinger 			res_holder, all_tg_pt, type);
1691c66ac9dbSNicholas Bellinger out:
16926d180253SJesper Juhl 	kfree(i_fabric);
16936d180253SJesper Juhl 	kfree(i_port);
16946d180253SJesper Juhl 	kfree(isid);
16956d180253SJesper Juhl 	kfree(t_fabric);
16966d180253SJesper Juhl 	kfree(t_port);
1697c66ac9dbSNicholas Bellinger 	kfree(orig);
1698c66ac9dbSNicholas Bellinger 	return (ret == 0) ? count : ret;
1699c66ac9dbSNicholas Bellinger }
1700c66ac9dbSNicholas Bellinger 
1701c66ac9dbSNicholas Bellinger 
17022eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_holder);
17032eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
17042eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
17052eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
17062eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
17072eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
17082eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_type);
17092eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
17102eafd729SChristoph Hellwig CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
1711c66ac9dbSNicholas Bellinger 
1712c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_pr_attrs[] = {
17132eafd729SChristoph Hellwig 	&target_pr_attr_res_holder,
17142eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_all_tgt_pts,
17152eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_generation,
17162eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_holder_tg_port,
17172eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_registered_i_pts,
17182eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_type,
17192eafd729SChristoph Hellwig 	&target_pr_attr_res_type,
17202eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_active,
17212eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_metadata,
1722c66ac9dbSNicholas Bellinger 	NULL,
1723c66ac9dbSNicholas Bellinger };
1724c66ac9dbSNicholas Bellinger 
17252eafd729SChristoph Hellwig TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
1726c66ac9dbSNicholas Bellinger 
172791e2e39bSNicholas Bellinger /*  End functions for struct config_item_type tb_dev_pr_cit */
1728c66ac9dbSNicholas Bellinger 
172973112edcSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_cit */
1730c66ac9dbSNicholas Bellinger 
17312eafd729SChristoph Hellwig static inline struct se_device *to_device(struct config_item *item)
1732c66ac9dbSNicholas Bellinger {
17332eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device, dev_group);
17342eafd729SChristoph Hellwig }
17352eafd729SChristoph Hellwig 
17362eafd729SChristoph Hellwig static ssize_t target_dev_info_show(struct config_item *item, char *page)
17372eafd729SChristoph Hellwig {
17382eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1739c66ac9dbSNicholas Bellinger 	int bl = 0;
1740c66ac9dbSNicholas Bellinger 	ssize_t read_bytes = 0;
1741c66ac9dbSNicholas Bellinger 
17420fd97ccfSChristoph Hellwig 	transport_dump_dev_state(dev, page, &bl);
1743c66ac9dbSNicholas Bellinger 	read_bytes += bl;
17440a06d430SChristoph Hellwig 	read_bytes += dev->transport->show_configfs_dev_params(dev,
17450a06d430SChristoph Hellwig 			page+read_bytes);
1746c66ac9dbSNicholas Bellinger 	return read_bytes;
1747c66ac9dbSNicholas Bellinger }
1748c66ac9dbSNicholas Bellinger 
17492eafd729SChristoph Hellwig static ssize_t target_dev_control_store(struct config_item *item,
17502eafd729SChristoph Hellwig 		const char *page, size_t count)
1751c66ac9dbSNicholas Bellinger {
17522eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1753c66ac9dbSNicholas Bellinger 
17540a06d430SChristoph Hellwig 	return dev->transport->set_configfs_dev_params(dev, page, count);
1755c66ac9dbSNicholas Bellinger }
1756c66ac9dbSNicholas Bellinger 
17572eafd729SChristoph Hellwig static ssize_t target_dev_alias_show(struct config_item *item, char *page)
1758c66ac9dbSNicholas Bellinger {
17592eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1760c66ac9dbSNicholas Bellinger 
17610fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_ALIAS))
1762c66ac9dbSNicholas Bellinger 		return 0;
1763c66ac9dbSNicholas Bellinger 
17640fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1765c66ac9dbSNicholas Bellinger }
1766c66ac9dbSNicholas Bellinger 
17672eafd729SChristoph Hellwig static ssize_t target_dev_alias_store(struct config_item *item,
17682eafd729SChristoph Hellwig 		const char *page, size_t count)
1769c66ac9dbSNicholas Bellinger {
17702eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
17710fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1772c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1773c66ac9dbSNicholas Bellinger 
1774c66ac9dbSNicholas Bellinger 	if (count > (SE_DEV_ALIAS_LEN-1)) {
17756708bb27SAndy Grover 		pr_err("alias count: %d exceeds"
1776c66ac9dbSNicholas Bellinger 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1777c66ac9dbSNicholas Bellinger 			SE_DEV_ALIAS_LEN-1);
1778c66ac9dbSNicholas Bellinger 		return -EINVAL;
1779c66ac9dbSNicholas Bellinger 	}
1780c66ac9dbSNicholas Bellinger 
17810fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
17823011684cSDan Carpenter 	if (!read_bytes)
17833011684cSDan Carpenter 		return -EINVAL;
17840fd97ccfSChristoph Hellwig 	if (dev->dev_alias[read_bytes - 1] == '\n')
17850fd97ccfSChristoph Hellwig 		dev->dev_alias[read_bytes - 1] = '\0';
17860877eafdSSebastian Andrzej Siewior 
17870fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_ALIAS;
17883011684cSDan Carpenter 
17896708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1790c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
17910fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
17920fd97ccfSChristoph Hellwig 		dev->dev_alias);
1793c66ac9dbSNicholas Bellinger 
1794c66ac9dbSNicholas Bellinger 	return read_bytes;
1795c66ac9dbSNicholas Bellinger }
1796c66ac9dbSNicholas Bellinger 
17972eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
1798c66ac9dbSNicholas Bellinger {
17992eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1800c66ac9dbSNicholas Bellinger 
18010fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1802c66ac9dbSNicholas Bellinger 		return 0;
1803c66ac9dbSNicholas Bellinger 
18040fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1805c66ac9dbSNicholas Bellinger }
1806c66ac9dbSNicholas Bellinger 
18072eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_store(struct config_item *item,
18082eafd729SChristoph Hellwig 		const char *page, size_t count)
1809c66ac9dbSNicholas Bellinger {
18102eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
18110fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1812c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1813c66ac9dbSNicholas Bellinger 
1814c66ac9dbSNicholas Bellinger 	if (count > (SE_UDEV_PATH_LEN-1)) {
18156708bb27SAndy Grover 		pr_err("udev_path count: %d exceeds"
1816c66ac9dbSNicholas Bellinger 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1817c66ac9dbSNicholas Bellinger 			SE_UDEV_PATH_LEN-1);
1818c66ac9dbSNicholas Bellinger 		return -EINVAL;
1819c66ac9dbSNicholas Bellinger 	}
1820c66ac9dbSNicholas Bellinger 
18210fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1822c66ac9dbSNicholas Bellinger 			"%s", page);
18233011684cSDan Carpenter 	if (!read_bytes)
18243011684cSDan Carpenter 		return -EINVAL;
18250fd97ccfSChristoph Hellwig 	if (dev->udev_path[read_bytes - 1] == '\n')
18260fd97ccfSChristoph Hellwig 		dev->udev_path[read_bytes - 1] = '\0';
18270877eafdSSebastian Andrzej Siewior 
18280fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_UDEV_PATH;
18293011684cSDan Carpenter 
18306708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1831c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
18320fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
18330fd97ccfSChristoph Hellwig 		dev->udev_path);
1834c66ac9dbSNicholas Bellinger 
1835c66ac9dbSNicholas Bellinger 	return read_bytes;
1836c66ac9dbSNicholas Bellinger }
1837c66ac9dbSNicholas Bellinger 
18382eafd729SChristoph Hellwig static ssize_t target_dev_enable_show(struct config_item *item, char *page)
183964146db7SAndy Grover {
18402eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
184164146db7SAndy Grover 
184264146db7SAndy Grover 	return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
184364146db7SAndy Grover }
184464146db7SAndy Grover 
18452eafd729SChristoph Hellwig static ssize_t target_dev_enable_store(struct config_item *item,
18462eafd729SChristoph Hellwig 		const char *page, size_t count)
1847c66ac9dbSNicholas Bellinger {
18482eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1849c66ac9dbSNicholas Bellinger 	char *ptr;
18500fd97ccfSChristoph Hellwig 	int ret;
1851c66ac9dbSNicholas Bellinger 
1852c66ac9dbSNicholas Bellinger 	ptr = strstr(page, "1");
18536708bb27SAndy Grover 	if (!ptr) {
18546708bb27SAndy Grover 		pr_err("For dev_enable ops, only valid value"
1855c66ac9dbSNicholas Bellinger 				" is \"1\"\n");
1856c66ac9dbSNicholas Bellinger 		return -EINVAL;
1857c66ac9dbSNicholas Bellinger 	}
1858c66ac9dbSNicholas Bellinger 
18590fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
18600fd97ccfSChristoph Hellwig 	if (ret)
18610fd97ccfSChristoph Hellwig 		return ret;
1862c66ac9dbSNicholas Bellinger 	return count;
1863c66ac9dbSNicholas Bellinger }
1864c66ac9dbSNicholas Bellinger 
18652eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
1866c66ac9dbSNicholas Bellinger {
18672eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1868c66ac9dbSNicholas Bellinger 	struct config_item *lu_ci;
1869c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
1870c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1871c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1872c66ac9dbSNicholas Bellinger 
1873c66ac9dbSNicholas Bellinger 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1874c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
1875c87fbd56SChristoph Hellwig 		return 0;
1876c66ac9dbSNicholas Bellinger 
1877c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1878c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
18796708bb27SAndy Grover 	if (lu_gp) {
1880c66ac9dbSNicholas Bellinger 		lu_ci = &lu_gp->lu_gp_group.cg_item;
1881c66ac9dbSNicholas Bellinger 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1882c66ac9dbSNicholas Bellinger 			config_item_name(lu_ci), lu_gp->lu_gp_id);
1883c66ac9dbSNicholas Bellinger 	}
1884c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1885c66ac9dbSNicholas Bellinger 
1886c66ac9dbSNicholas Bellinger 	return len;
1887c66ac9dbSNicholas Bellinger }
1888c66ac9dbSNicholas Bellinger 
18892eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_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 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1895c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1896c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
1897c66ac9dbSNicholas Bellinger 	int move = 0;
1898c66ac9dbSNicholas Bellinger 
1899c87fbd56SChristoph Hellwig 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1900c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
19019105bfc0SAndy Grover 		return count;
1902c87fbd56SChristoph Hellwig 
1903c66ac9dbSNicholas Bellinger 	if (count > LU_GROUP_NAME_BUF) {
19046708bb27SAndy Grover 		pr_err("ALUA LU Group Alias too large!\n");
1905c66ac9dbSNicholas Bellinger 		return -EINVAL;
1906c66ac9dbSNicholas Bellinger 	}
1907c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
1908c66ac9dbSNicholas Bellinger 	memcpy(buf, page, count);
1909c66ac9dbSNicholas Bellinger 	/*
1910c66ac9dbSNicholas Bellinger 	 * Any ALUA logical unit alias besides "NULL" means we will be
1911c66ac9dbSNicholas Bellinger 	 * making a new group association.
1912c66ac9dbSNicholas Bellinger 	 */
1913c66ac9dbSNicholas Bellinger 	if (strcmp(strstrip(buf), "NULL")) {
1914c66ac9dbSNicholas Bellinger 		/*
1915c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name() will increment reference to
1916c66ac9dbSNicholas Bellinger 		 * struct t10_alua_lu_gp.  This reference is released with
1917c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name below().
1918c66ac9dbSNicholas Bellinger 		 */
1919c66ac9dbSNicholas Bellinger 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
19206708bb27SAndy Grover 		if (!lu_gp_new)
1921c66ac9dbSNicholas Bellinger 			return -ENODEV;
1922c66ac9dbSNicholas Bellinger 	}
1923c66ac9dbSNicholas Bellinger 
1924c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1925c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
19266708bb27SAndy Grover 	if (lu_gp) {
1927c66ac9dbSNicholas Bellinger 		/*
1928c66ac9dbSNicholas Bellinger 		 * Clearing an existing lu_gp association, and replacing
1929c66ac9dbSNicholas Bellinger 		 * with NULL
1930c66ac9dbSNicholas Bellinger 		 */
19316708bb27SAndy Grover 		if (!lu_gp_new) {
19326708bb27SAndy Grover 			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1933c66ac9dbSNicholas Bellinger 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1934c66ac9dbSNicholas Bellinger 				" %hu\n",
1935c66ac9dbSNicholas Bellinger 				config_item_name(&hba->hba_group.cg_item),
19360fd97ccfSChristoph Hellwig 				config_item_name(&dev->dev_group.cg_item),
1937c66ac9dbSNicholas Bellinger 				config_item_name(&lu_gp->lu_gp_group.cg_item),
1938c66ac9dbSNicholas Bellinger 				lu_gp->lu_gp_id);
1939c66ac9dbSNicholas Bellinger 
1940c66ac9dbSNicholas Bellinger 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1941c66ac9dbSNicholas Bellinger 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1942c66ac9dbSNicholas Bellinger 
1943c66ac9dbSNicholas Bellinger 			return count;
1944c66ac9dbSNicholas Bellinger 		}
1945c66ac9dbSNicholas Bellinger 		/*
1946c66ac9dbSNicholas Bellinger 		 * Removing existing association of lu_gp_mem with lu_gp
1947c66ac9dbSNicholas Bellinger 		 */
1948c66ac9dbSNicholas Bellinger 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1949c66ac9dbSNicholas Bellinger 		move = 1;
1950c66ac9dbSNicholas Bellinger 	}
1951c66ac9dbSNicholas Bellinger 	/*
1952c66ac9dbSNicholas Bellinger 	 * Associate lu_gp_mem with lu_gp_new.
1953c66ac9dbSNicholas Bellinger 	 */
1954c66ac9dbSNicholas Bellinger 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1955c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1956c66ac9dbSNicholas Bellinger 
19576708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1958c66ac9dbSNicholas Bellinger 		" core/alua/lu_gps/%s, ID: %hu\n",
1959c66ac9dbSNicholas Bellinger 		(move) ? "Moving" : "Adding",
1960c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
19610fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
1962c66ac9dbSNicholas Bellinger 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1963c66ac9dbSNicholas Bellinger 		lu_gp_new->lu_gp_id);
1964c66ac9dbSNicholas Bellinger 
1965c66ac9dbSNicholas Bellinger 	core_alua_put_lu_gp_from_name(lu_gp_new);
1966c66ac9dbSNicholas Bellinger 	return count;
1967c66ac9dbSNicholas Bellinger }
1968c66ac9dbSNicholas Bellinger 
19692eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
1970229d4f11SHannes Reinecke {
19712eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1972229d4f11SHannes Reinecke 	struct t10_alua_lba_map *map;
1973229d4f11SHannes Reinecke 	struct t10_alua_lba_map_member *mem;
1974229d4f11SHannes Reinecke 	char *b = page;
1975229d4f11SHannes Reinecke 	int bl = 0;
1976229d4f11SHannes Reinecke 	char state;
1977229d4f11SHannes Reinecke 
1978229d4f11SHannes Reinecke 	spin_lock(&dev->t10_alua.lba_map_lock);
1979229d4f11SHannes Reinecke 	if (!list_empty(&dev->t10_alua.lba_map_list))
1980229d4f11SHannes Reinecke 	    bl += sprintf(b + bl, "%u %u\n",
1981229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_size,
1982229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_multiplier);
1983229d4f11SHannes Reinecke 	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1984229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "%llu %llu",
1985229d4f11SHannes Reinecke 			      map->lba_map_first_lba, map->lba_map_last_lba);
1986229d4f11SHannes Reinecke 		list_for_each_entry(mem, &map->lba_map_mem_list,
1987229d4f11SHannes Reinecke 				    lba_map_mem_list) {
1988229d4f11SHannes Reinecke 			switch (mem->lba_map_mem_alua_state) {
1989229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1990229d4f11SHannes Reinecke 				state = 'O';
1991229d4f11SHannes Reinecke 				break;
1992229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1993229d4f11SHannes Reinecke 				state = 'A';
1994229d4f11SHannes Reinecke 				break;
1995229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_STANDBY:
1996229d4f11SHannes Reinecke 				state = 'S';
1997229d4f11SHannes Reinecke 				break;
1998229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_UNAVAILABLE:
1999229d4f11SHannes Reinecke 				state = 'U';
2000229d4f11SHannes Reinecke 				break;
2001229d4f11SHannes Reinecke 			default:
2002229d4f11SHannes Reinecke 				state = '.';
2003229d4f11SHannes Reinecke 				break;
2004229d4f11SHannes Reinecke 			}
2005229d4f11SHannes Reinecke 			bl += sprintf(b + bl, " %d:%c",
2006229d4f11SHannes Reinecke 				      mem->lba_map_mem_alua_pg_id, state);
2007229d4f11SHannes Reinecke 		}
2008229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "\n");
2009229d4f11SHannes Reinecke 	}
2010229d4f11SHannes Reinecke 	spin_unlock(&dev->t10_alua.lba_map_lock);
2011229d4f11SHannes Reinecke 	return bl;
2012229d4f11SHannes Reinecke }
2013229d4f11SHannes Reinecke 
20142eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_store(struct config_item *item,
20152eafd729SChristoph Hellwig 		const char *page, size_t count)
2016229d4f11SHannes Reinecke {
20172eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2018229d4f11SHannes Reinecke 	struct t10_alua_lba_map *lba_map = NULL;
2019229d4f11SHannes Reinecke 	struct list_head lba_list;
2020229d4f11SHannes Reinecke 	char *map_entries, *ptr;
2021229d4f11SHannes Reinecke 	char state;
2022229d4f11SHannes Reinecke 	int pg_num = -1, pg;
2023229d4f11SHannes Reinecke 	int ret = 0, num = 0, pg_id, alua_state;
2024229d4f11SHannes Reinecke 	unsigned long start_lba = -1, end_lba = -1;
2025229d4f11SHannes Reinecke 	unsigned long segment_size = -1, segment_mult = -1;
2026229d4f11SHannes Reinecke 
2027229d4f11SHannes Reinecke 	map_entries = kstrdup(page, GFP_KERNEL);
2028229d4f11SHannes Reinecke 	if (!map_entries)
2029229d4f11SHannes Reinecke 		return -ENOMEM;
2030229d4f11SHannes Reinecke 
2031229d4f11SHannes Reinecke 	INIT_LIST_HEAD(&lba_list);
2032229d4f11SHannes Reinecke 	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2033229d4f11SHannes Reinecke 		if (!*ptr)
2034229d4f11SHannes Reinecke 			continue;
2035229d4f11SHannes Reinecke 
2036229d4f11SHannes Reinecke 		if (num == 0) {
2037229d4f11SHannes Reinecke 			if (sscanf(ptr, "%lu %lu\n",
2038229d4f11SHannes Reinecke 				   &segment_size, &segment_mult) != 2) {
2039229d4f11SHannes Reinecke 				pr_err("Invalid line %d\n", num);
2040229d4f11SHannes Reinecke 				ret = -EINVAL;
2041229d4f11SHannes Reinecke 				break;
2042229d4f11SHannes Reinecke 			}
2043229d4f11SHannes Reinecke 			num++;
2044229d4f11SHannes Reinecke 			continue;
2045229d4f11SHannes Reinecke 		}
2046229d4f11SHannes Reinecke 		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2047229d4f11SHannes Reinecke 			pr_err("Invalid line %d\n", num);
2048229d4f11SHannes Reinecke 			ret = -EINVAL;
2049229d4f11SHannes Reinecke 			break;
2050229d4f11SHannes Reinecke 		}
2051229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2052229d4f11SHannes Reinecke 		if (!ptr) {
2053229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing end lba\n", num);
2054229d4f11SHannes Reinecke 			ret = -EINVAL;
2055229d4f11SHannes Reinecke 			break;
2056229d4f11SHannes Reinecke 		}
2057229d4f11SHannes Reinecke 		ptr++;
2058229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2059229d4f11SHannes Reinecke 		if (!ptr) {
2060229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing state definitions\n",
2061229d4f11SHannes Reinecke 			       num);
2062229d4f11SHannes Reinecke 			ret = -EINVAL;
2063229d4f11SHannes Reinecke 			break;
2064229d4f11SHannes Reinecke 		}
2065229d4f11SHannes Reinecke 		ptr++;
2066229d4f11SHannes Reinecke 		lba_map = core_alua_allocate_lba_map(&lba_list,
2067229d4f11SHannes Reinecke 						     start_lba, end_lba);
2068229d4f11SHannes Reinecke 		if (IS_ERR(lba_map)) {
2069229d4f11SHannes Reinecke 			ret = PTR_ERR(lba_map);
2070229d4f11SHannes Reinecke 			break;
2071229d4f11SHannes Reinecke 		}
2072229d4f11SHannes Reinecke 		pg = 0;
2073229d4f11SHannes Reinecke 		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2074229d4f11SHannes Reinecke 			switch (state) {
2075229d4f11SHannes Reinecke 			case 'O':
2076229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2077229d4f11SHannes Reinecke 				break;
2078229d4f11SHannes Reinecke 			case 'A':
2079229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2080229d4f11SHannes Reinecke 				break;
2081229d4f11SHannes Reinecke 			case 'S':
2082229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_STANDBY;
2083229d4f11SHannes Reinecke 				break;
2084229d4f11SHannes Reinecke 			case 'U':
2085229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2086229d4f11SHannes Reinecke 				break;
2087229d4f11SHannes Reinecke 			default:
2088229d4f11SHannes Reinecke 				pr_err("Invalid ALUA state '%c'\n", state);
2089229d4f11SHannes Reinecke 				ret = -EINVAL;
2090229d4f11SHannes Reinecke 				goto out;
2091229d4f11SHannes Reinecke 			}
2092229d4f11SHannes Reinecke 
2093229d4f11SHannes Reinecke 			ret = core_alua_allocate_lba_map_mem(lba_map,
2094229d4f11SHannes Reinecke 							     pg_id, alua_state);
2095229d4f11SHannes Reinecke 			if (ret) {
2096229d4f11SHannes Reinecke 				pr_err("Invalid target descriptor %d:%c "
2097229d4f11SHannes Reinecke 				       "at line %d\n",
2098229d4f11SHannes Reinecke 				       pg_id, state, num);
2099229d4f11SHannes Reinecke 				break;
2100229d4f11SHannes Reinecke 			}
2101229d4f11SHannes Reinecke 			pg++;
2102229d4f11SHannes Reinecke 			ptr = strchr(ptr, ' ');
2103229d4f11SHannes Reinecke 			if (ptr)
2104229d4f11SHannes Reinecke 				ptr++;
2105229d4f11SHannes Reinecke 			else
2106229d4f11SHannes Reinecke 				break;
2107229d4f11SHannes Reinecke 		}
2108229d4f11SHannes Reinecke 		if (pg_num == -1)
2109229d4f11SHannes Reinecke 		    pg_num = pg;
2110229d4f11SHannes Reinecke 		else if (pg != pg_num) {
2111229d4f11SHannes Reinecke 			pr_err("Only %d from %d port groups definitions "
2112229d4f11SHannes Reinecke 			       "at line %d\n", pg, pg_num, num);
2113229d4f11SHannes Reinecke 			ret = -EINVAL;
2114229d4f11SHannes Reinecke 			break;
2115229d4f11SHannes Reinecke 		}
2116229d4f11SHannes Reinecke 		num++;
2117229d4f11SHannes Reinecke 	}
2118229d4f11SHannes Reinecke out:
2119229d4f11SHannes Reinecke 	if (ret) {
2120229d4f11SHannes Reinecke 		core_alua_free_lba_map(&lba_list);
2121229d4f11SHannes Reinecke 		count = ret;
2122229d4f11SHannes Reinecke 	} else
2123229d4f11SHannes Reinecke 		core_alua_set_lba_map(dev, &lba_list,
2124229d4f11SHannes Reinecke 				      segment_size, segment_mult);
2125229d4f11SHannes Reinecke 	kfree(map_entries);
2126229d4f11SHannes Reinecke 	return count;
2127229d4f11SHannes Reinecke }
2128229d4f11SHannes Reinecke 
21292eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_dev_, info);
21302eafd729SChristoph Hellwig CONFIGFS_ATTR_WO(target_dev_, control);
21312eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alias);
21322eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, udev_path);
21332eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, enable);
21342eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alua_lu_gp);
21352eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, lba_map);
2136229d4f11SHannes Reinecke 
213773112edcSNicholas Bellinger static struct configfs_attribute *target_core_dev_attrs[] = {
21382eafd729SChristoph Hellwig 	&target_dev_attr_info,
21392eafd729SChristoph Hellwig 	&target_dev_attr_control,
21402eafd729SChristoph Hellwig 	&target_dev_attr_alias,
21412eafd729SChristoph Hellwig 	&target_dev_attr_udev_path,
21422eafd729SChristoph Hellwig 	&target_dev_attr_enable,
21432eafd729SChristoph Hellwig 	&target_dev_attr_alua_lu_gp,
21442eafd729SChristoph Hellwig 	&target_dev_attr_lba_map,
2145c66ac9dbSNicholas Bellinger 	NULL,
2146c66ac9dbSNicholas Bellinger };
2147c66ac9dbSNicholas Bellinger 
2148c66ac9dbSNicholas Bellinger static void target_core_dev_release(struct config_item *item)
2149c66ac9dbSNicholas Bellinger {
21500fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
21510fd97ccfSChristoph Hellwig 	struct se_device *dev =
21520fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2153c66ac9dbSNicholas Bellinger 
2154c66ac9dbSNicholas Bellinger 	kfree(dev_cg->default_groups);
21550fd97ccfSChristoph Hellwig 	target_free_device(dev);
2156c66ac9dbSNicholas Bellinger }
2157c66ac9dbSNicholas Bellinger 
2158c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_item_ops = {
2159c66ac9dbSNicholas Bellinger 	.release		= target_core_dev_release,
2160c66ac9dbSNicholas Bellinger };
2161c66ac9dbSNicholas Bellinger 
216273112edcSNicholas Bellinger TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2163c66ac9dbSNicholas Bellinger 
216473112edcSNicholas Bellinger /* End functions for struct config_item_type tb_dev_cit */
2165c66ac9dbSNicholas Bellinger 
2166c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2167c66ac9dbSNicholas Bellinger 
21682eafd729SChristoph Hellwig static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2169c66ac9dbSNicholas Bellinger {
21702eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_lu_gp,
21712eafd729SChristoph Hellwig 			lu_gp_group);
21722eafd729SChristoph Hellwig }
21732eafd729SChristoph Hellwig 
21742eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
21752eafd729SChristoph Hellwig {
21762eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
21772eafd729SChristoph Hellwig 
21786708bb27SAndy Grover 	if (!lu_gp->lu_gp_valid_id)
2179c66ac9dbSNicholas Bellinger 		return 0;
2180c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2181c66ac9dbSNicholas Bellinger }
2182c66ac9dbSNicholas Bellinger 
21832eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
21842eafd729SChristoph Hellwig 		const char *page, size_t count)
2185c66ac9dbSNicholas Bellinger {
21862eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2187c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2188c66ac9dbSNicholas Bellinger 	unsigned long lu_gp_id;
2189c66ac9dbSNicholas Bellinger 	int ret;
2190c66ac9dbSNicholas Bellinger 
219157103d7fSJingoo Han 	ret = kstrtoul(page, 0, &lu_gp_id);
2192c66ac9dbSNicholas Bellinger 	if (ret < 0) {
219357103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2194c66ac9dbSNicholas Bellinger 			" lu_gp_id\n", ret);
219557103d7fSJingoo Han 		return ret;
2196c66ac9dbSNicholas Bellinger 	}
2197c66ac9dbSNicholas Bellinger 	if (lu_gp_id > 0x0000ffff) {
21986708bb27SAndy Grover 		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2199c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", lu_gp_id);
2200c66ac9dbSNicholas Bellinger 		return -EINVAL;
2201c66ac9dbSNicholas Bellinger 	}
2202c66ac9dbSNicholas Bellinger 
2203c66ac9dbSNicholas Bellinger 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2204c66ac9dbSNicholas Bellinger 	if (ret < 0)
2205c66ac9dbSNicholas Bellinger 		return -EINVAL;
2206c66ac9dbSNicholas Bellinger 
22076708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2208c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
2209c66ac9dbSNicholas Bellinger 		config_item_name(&alua_lu_gp_cg->cg_item),
2210c66ac9dbSNicholas Bellinger 		lu_gp->lu_gp_id);
2211c66ac9dbSNicholas Bellinger 
2212c66ac9dbSNicholas Bellinger 	return count;
2213c66ac9dbSNicholas Bellinger }
2214c66ac9dbSNicholas Bellinger 
22152eafd729SChristoph Hellwig static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2216c66ac9dbSNicholas Bellinger {
22172eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2218c66ac9dbSNicholas Bellinger 	struct se_device *dev;
2219c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2220c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2221c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2222c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
2223c66ac9dbSNicholas Bellinger 
2224c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
2225c66ac9dbSNicholas Bellinger 
2226c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp->lu_gp_lock);
2227c66ac9dbSNicholas Bellinger 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2228c66ac9dbSNicholas Bellinger 		dev = lu_gp_mem->lu_gp_mem_dev;
22290fd97ccfSChristoph Hellwig 		hba = dev->se_hba;
2230c66ac9dbSNicholas Bellinger 
2231c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2232c66ac9dbSNicholas Bellinger 			config_item_name(&hba->hba_group.cg_item),
22330fd97ccfSChristoph Hellwig 			config_item_name(&dev->dev_group.cg_item));
2234c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2235c66ac9dbSNicholas Bellinger 
2236c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
22376708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2238c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2239c66ac9dbSNicholas Bellinger 			break;
2240c66ac9dbSNicholas Bellinger 		}
2241c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2242c66ac9dbSNicholas Bellinger 		len += cur_len;
2243c66ac9dbSNicholas Bellinger 	}
2244c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp->lu_gp_lock);
2245c66ac9dbSNicholas Bellinger 
2246c66ac9dbSNicholas Bellinger 	return len;
2247c66ac9dbSNicholas Bellinger }
2248c66ac9dbSNicholas Bellinger 
22492eafd729SChristoph Hellwig CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
22502eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_lu_gp_, members);
2251c66ac9dbSNicholas Bellinger 
2252c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
22532eafd729SChristoph Hellwig 	&target_lu_gp_attr_lu_gp_id,
22542eafd729SChristoph Hellwig 	&target_lu_gp_attr_members,
2255c66ac9dbSNicholas Bellinger 	NULL,
2256c66ac9dbSNicholas Bellinger };
2257c66ac9dbSNicholas Bellinger 
22581f6fe7cbSNicholas Bellinger static void target_core_alua_lu_gp_release(struct config_item *item)
22591f6fe7cbSNicholas Bellinger {
22601f6fe7cbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
22611f6fe7cbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
22621f6fe7cbSNicholas Bellinger 
22631f6fe7cbSNicholas Bellinger 	core_alua_free_lu_gp(lu_gp);
22641f6fe7cbSNicholas Bellinger }
22651f6fe7cbSNicholas Bellinger 
2266c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_lu_gp_ops = {
22671f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_lu_gp_release,
2268c66ac9dbSNicholas Bellinger };
2269c66ac9dbSNicholas Bellinger 
2270c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gp_cit = {
2271c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
2272c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_lu_gp_attrs,
2273c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2274c66ac9dbSNicholas Bellinger };
2275c66ac9dbSNicholas Bellinger 
2276c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2277c66ac9dbSNicholas Bellinger 
2278c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2279c66ac9dbSNicholas Bellinger 
2280c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_lu_gp(
2281c66ac9dbSNicholas Bellinger 	struct config_group *group,
2282c66ac9dbSNicholas Bellinger 	const char *name)
2283c66ac9dbSNicholas Bellinger {
2284c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2285c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = NULL;
2286c66ac9dbSNicholas Bellinger 	struct config_item *alua_lu_gp_ci = NULL;
2287c66ac9dbSNicholas Bellinger 
2288c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp(name, 0);
2289c66ac9dbSNicholas Bellinger 	if (IS_ERR(lu_gp))
2290c66ac9dbSNicholas Bellinger 		return NULL;
2291c66ac9dbSNicholas Bellinger 
2292c66ac9dbSNicholas Bellinger 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
2293c66ac9dbSNicholas Bellinger 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2294c66ac9dbSNicholas Bellinger 
2295c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_lu_gp_cg, name,
2296c66ac9dbSNicholas Bellinger 			&target_core_alua_lu_gp_cit);
2297c66ac9dbSNicholas Bellinger 
22986708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2299c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s\n",
2300c66ac9dbSNicholas Bellinger 		config_item_name(alua_lu_gp_ci));
2301c66ac9dbSNicholas Bellinger 
2302c66ac9dbSNicholas Bellinger 	return alua_lu_gp_cg;
2303c66ac9dbSNicholas Bellinger 
2304c66ac9dbSNicholas Bellinger }
2305c66ac9dbSNicholas Bellinger 
2306c66ac9dbSNicholas Bellinger static void target_core_alua_drop_lu_gp(
2307c66ac9dbSNicholas Bellinger 	struct config_group *group,
2308c66ac9dbSNicholas Bellinger 	struct config_item *item)
2309c66ac9dbSNicholas Bellinger {
2310c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2311c66ac9dbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
2312c66ac9dbSNicholas Bellinger 
23136708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2314c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
2315c66ac9dbSNicholas Bellinger 		config_item_name(item), lu_gp->lu_gp_id);
23161f6fe7cbSNicholas Bellinger 	/*
23171f6fe7cbSNicholas Bellinger 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
23181f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_lu_gp_release()
23191f6fe7cbSNicholas Bellinger 	 */
2320c66ac9dbSNicholas Bellinger 	config_item_put(item);
2321c66ac9dbSNicholas Bellinger }
2322c66ac9dbSNicholas Bellinger 
2323c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2324c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_lu_gp,
2325c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_lu_gp,
2326c66ac9dbSNicholas Bellinger };
2327c66ac9dbSNicholas Bellinger 
2328c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gps_cit = {
2329c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2330c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
2331c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2332c66ac9dbSNicholas Bellinger };
2333c66ac9dbSNicholas Bellinger 
2334c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2335c66ac9dbSNicholas Bellinger 
2336c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2337c66ac9dbSNicholas Bellinger 
23382eafd729SChristoph Hellwig static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
23392eafd729SChristoph Hellwig {
23402eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
23412eafd729SChristoph Hellwig 			tg_pt_gp_group);
23422eafd729SChristoph Hellwig }
2343c66ac9dbSNicholas Bellinger 
23442eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2345c66ac9dbSNicholas Bellinger 		char *page)
2346c66ac9dbSNicholas Bellinger {
2347c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n",
23482eafd729SChristoph Hellwig 		atomic_read(&to_tg_pt_gp(item)->tg_pt_gp_alua_access_state));
2349c66ac9dbSNicholas Bellinger }
2350c66ac9dbSNicholas Bellinger 
23512eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
23522eafd729SChristoph Hellwig 		const char *page, size_t count)
2353c66ac9dbSNicholas Bellinger {
23542eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
23550fd97ccfSChristoph Hellwig 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2356c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2357c66ac9dbSNicholas Bellinger 	int new_state, ret;
2358c66ac9dbSNicholas Bellinger 
23596708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2360125d0119SHannes Reinecke 		pr_err("Unable to do implicit ALUA on non valid"
2361c66ac9dbSNicholas Bellinger 			" tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2362c66ac9dbSNicholas Bellinger 		return -EINVAL;
2363c66ac9dbSNicholas Bellinger 	}
2364f1453773SNicholas Bellinger 	if (!(dev->dev_flags & DF_CONFIGURED)) {
2365f1453773SNicholas Bellinger 		pr_err("Unable to set alua_access_state while device is"
2366f1453773SNicholas Bellinger 		       " not configured\n");
2367f1453773SNicholas Bellinger 		return -ENODEV;
2368f1453773SNicholas Bellinger 	}
2369c66ac9dbSNicholas Bellinger 
237057103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2371c66ac9dbSNicholas Bellinger 	if (ret < 0) {
23726708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access state from"
2373c66ac9dbSNicholas Bellinger 				" %s\n", page);
237457103d7fSJingoo Han 		return ret;
2375c66ac9dbSNicholas Bellinger 	}
2376c66ac9dbSNicholas Bellinger 	new_state = (int)tmp;
2377c66ac9dbSNicholas Bellinger 
2378125d0119SHannes Reinecke 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2379125d0119SHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA"
2380125d0119SHannes Reinecke 			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
2381c66ac9dbSNicholas Bellinger 		return -EINVAL;
2382c66ac9dbSNicholas Bellinger 	}
2383c66094bfSHannes Reinecke 	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2384c66094bfSHannes Reinecke 	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2385c66094bfSHannes Reinecke 		/* LBA DEPENDENT is only allowed with implicit ALUA */
2386c66094bfSHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA transition"
2387c66094bfSHannes Reinecke 		       " while explicit ALUA management is enabled\n");
2388c66094bfSHannes Reinecke 		return -EINVAL;
2389c66094bfSHannes Reinecke 	}
2390c66ac9dbSNicholas Bellinger 
23910fd97ccfSChristoph Hellwig 	ret = core_alua_do_port_transition(tg_pt_gp, dev,
2392c66ac9dbSNicholas Bellinger 					NULL, NULL, new_state, 0);
2393c66ac9dbSNicholas Bellinger 	return (!ret) ? count : -EINVAL;
2394c66ac9dbSNicholas Bellinger }
2395c66ac9dbSNicholas Bellinger 
23962eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2397c66ac9dbSNicholas Bellinger 		char *page)
2398c66ac9dbSNicholas Bellinger {
23992eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2400c66ac9dbSNicholas Bellinger 	return sprintf(page, "%s\n",
2401c66ac9dbSNicholas Bellinger 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2402c66ac9dbSNicholas Bellinger }
2403c66ac9dbSNicholas Bellinger 
24042eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_store(
24052eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2406c66ac9dbSNicholas Bellinger {
24072eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2408c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2409c66ac9dbSNicholas Bellinger 	int new_status, ret;
2410c66ac9dbSNicholas Bellinger 
24116708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
24126708bb27SAndy Grover 		pr_err("Unable to do set ALUA access status on non"
2413c66ac9dbSNicholas Bellinger 			" valid tg_pt_gp ID: %hu\n",
2414c66ac9dbSNicholas Bellinger 			tg_pt_gp->tg_pt_gp_valid_id);
2415c66ac9dbSNicholas Bellinger 		return -EINVAL;
2416c66ac9dbSNicholas Bellinger 	}
2417c66ac9dbSNicholas Bellinger 
241857103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2419c66ac9dbSNicholas Bellinger 	if (ret < 0) {
24206708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access status"
2421c66ac9dbSNicholas Bellinger 				" from %s\n", page);
242257103d7fSJingoo Han 		return ret;
2423c66ac9dbSNicholas Bellinger 	}
2424c66ac9dbSNicholas Bellinger 	new_status = (int)tmp;
2425c66ac9dbSNicholas Bellinger 
2426c66ac9dbSNicholas Bellinger 	if ((new_status != ALUA_STATUS_NONE) &&
2427125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2428125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
24296708bb27SAndy Grover 		pr_err("Illegal ALUA access status: 0x%02x\n",
2430c66ac9dbSNicholas Bellinger 				new_status);
2431c66ac9dbSNicholas Bellinger 		return -EINVAL;
2432c66ac9dbSNicholas Bellinger 	}
2433c66ac9dbSNicholas Bellinger 
2434c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2435c66ac9dbSNicholas Bellinger 	return count;
2436c66ac9dbSNicholas Bellinger }
2437c66ac9dbSNicholas Bellinger 
24382eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2439c66ac9dbSNicholas Bellinger 		char *page)
2440c66ac9dbSNicholas Bellinger {
24412eafd729SChristoph Hellwig 	return core_alua_show_access_type(to_tg_pt_gp(item), page);
2442c66ac9dbSNicholas Bellinger }
2443c66ac9dbSNicholas Bellinger 
24442eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
24452eafd729SChristoph Hellwig 		const char *page, size_t count)
2446c66ac9dbSNicholas Bellinger {
24472eafd729SChristoph Hellwig 	return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2448c66ac9dbSNicholas Bellinger }
2449c66ac9dbSNicholas Bellinger 
24502eafd729SChristoph Hellwig #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit)				\
24512eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_show(		\
24522eafd729SChristoph Hellwig 		struct config_item *item, char *p)			\
2453b0a382c5SHannes Reinecke {									\
24542eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
24552eafd729SChristoph Hellwig 	return sprintf(p, "%d\n",					\
24562eafd729SChristoph Hellwig 		!!(t->tg_pt_gp_alua_supported_states & _bit));		\
24572eafd729SChristoph Hellwig }									\
24582eafd729SChristoph Hellwig 									\
24592eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_store(		\
24602eafd729SChristoph Hellwig 		struct config_item *item, const char *p, size_t c)	\
2461b0a382c5SHannes Reinecke {									\
24622eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
2463b0a382c5SHannes Reinecke 	unsigned long tmp;						\
2464b0a382c5SHannes Reinecke 	int ret;							\
2465b0a382c5SHannes Reinecke 									\
2466b0a382c5SHannes Reinecke 	if (!t->tg_pt_gp_valid_id) {					\
2467b0a382c5SHannes Reinecke 		pr_err("Unable to do set ##_name ALUA state on non"	\
2468b0a382c5SHannes Reinecke 		       " valid tg_pt_gp ID: %hu\n",			\
2469b0a382c5SHannes Reinecke 		       t->tg_pt_gp_valid_id);				\
2470b0a382c5SHannes Reinecke 		return -EINVAL;						\
2471b0a382c5SHannes Reinecke 	}								\
2472b0a382c5SHannes Reinecke 									\
2473b0a382c5SHannes Reinecke 	ret = kstrtoul(p, 0, &tmp);					\
2474b0a382c5SHannes Reinecke 	if (ret < 0) {							\
2475b0a382c5SHannes Reinecke 		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
2476b0a382c5SHannes Reinecke 		return -EINVAL;						\
2477b0a382c5SHannes Reinecke 	}								\
2478b0a382c5SHannes Reinecke 	if (tmp > 1) {							\
2479b0a382c5SHannes Reinecke 		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2480b0a382c5SHannes Reinecke 		return -EINVAL;						\
2481b0a382c5SHannes Reinecke 	}								\
24821f0b030cSSebastian Herbszt 	if (tmp)							\
24832eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states |= _bit;		\
2484b0a382c5SHannes Reinecke 	else								\
24852eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states &= ~_bit;		\
2486b0a382c5SHannes Reinecke 									\
2487b0a382c5SHannes Reinecke 	return c;							\
24886be526c4SHannes Reinecke }
24896be526c4SHannes Reinecke 
24902eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
24912eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
24922eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
24932eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
24942eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
24952eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
24962eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
24976be526c4SHannes Reinecke 
24982eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_show(
24992eafd729SChristoph Hellwig 		struct config_item *item, char *page)
2500c66ac9dbSNicholas Bellinger {
25012eafd729SChristoph Hellwig 	return sprintf(page, "%d\n",
25022eafd729SChristoph Hellwig 		to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
2503c66ac9dbSNicholas Bellinger }
2504c66ac9dbSNicholas Bellinger 
25052eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_store(
25062eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2507c66ac9dbSNicholas Bellinger {
25082eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2509c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2510c66ac9dbSNicholas Bellinger 	int ret;
2511c66ac9dbSNicholas Bellinger 
251257103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2513c66ac9dbSNicholas Bellinger 	if (ret < 0) {
25146708bb27SAndy Grover 		pr_err("Unable to extract alua_write_metadata\n");
251557103d7fSJingoo Han 		return ret;
2516c66ac9dbSNicholas Bellinger 	}
2517c66ac9dbSNicholas Bellinger 
2518c66ac9dbSNicholas Bellinger 	if ((tmp != 0) && (tmp != 1)) {
25196708bb27SAndy Grover 		pr_err("Illegal value for alua_write_metadata:"
2520c66ac9dbSNicholas Bellinger 			" %lu\n", tmp);
2521c66ac9dbSNicholas Bellinger 		return -EINVAL;
2522c66ac9dbSNicholas Bellinger 	}
2523c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2524c66ac9dbSNicholas Bellinger 
2525c66ac9dbSNicholas Bellinger 	return count;
2526c66ac9dbSNicholas Bellinger }
2527c66ac9dbSNicholas Bellinger 
25282eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2529c66ac9dbSNicholas Bellinger 		char *page)
2530c66ac9dbSNicholas Bellinger {
25312eafd729SChristoph Hellwig 	return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
2532c66ac9dbSNicholas Bellinger }
2533c66ac9dbSNicholas Bellinger 
25342eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
25352eafd729SChristoph Hellwig 		const char *page, size_t count)
2536c66ac9dbSNicholas Bellinger {
25372eafd729SChristoph Hellwig 	return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
25382eafd729SChristoph Hellwig 			count);
2539c66ac9dbSNicholas Bellinger }
2540c66ac9dbSNicholas Bellinger 
25412eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2542c66ac9dbSNicholas Bellinger 		char *page)
2543c66ac9dbSNicholas Bellinger {
25442eafd729SChristoph Hellwig 	return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
2545c66ac9dbSNicholas Bellinger }
2546c66ac9dbSNicholas Bellinger 
25472eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
25482eafd729SChristoph Hellwig 		const char *page, size_t count)
2549c66ac9dbSNicholas Bellinger {
25502eafd729SChristoph Hellwig 	return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
25512eafd729SChristoph Hellwig 			count);
2552c66ac9dbSNicholas Bellinger }
2553c66ac9dbSNicholas Bellinger 
25542eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
25552eafd729SChristoph Hellwig 		struct config_item *item, char *page)
25562eafd729SChristoph Hellwig {
25572eafd729SChristoph Hellwig 	return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
25582eafd729SChristoph Hellwig }
2559c66ac9dbSNicholas Bellinger 
25602eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
25612eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
25622eafd729SChristoph Hellwig {
25632eafd729SChristoph Hellwig 	return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
25642eafd729SChristoph Hellwig 			count);
25652eafd729SChristoph Hellwig }
25662eafd729SChristoph Hellwig 
25672eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
25685b9a4d72SNicholas Bellinger 		char *page)
25695b9a4d72SNicholas Bellinger {
25702eafd729SChristoph Hellwig 	return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
25715b9a4d72SNicholas Bellinger }
25725b9a4d72SNicholas Bellinger 
25732eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
25742eafd729SChristoph Hellwig 		const char *page, size_t count)
25755b9a4d72SNicholas Bellinger {
25762eafd729SChristoph Hellwig 	return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
25775b9a4d72SNicholas Bellinger }
25785b9a4d72SNicholas Bellinger 
25792eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2580c66ac9dbSNicholas Bellinger 		char *page)
2581c66ac9dbSNicholas Bellinger {
25822eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2583c66ac9dbSNicholas Bellinger 
25846708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id)
2585c66ac9dbSNicholas Bellinger 		return 0;
2586c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2587c66ac9dbSNicholas Bellinger }
2588c66ac9dbSNicholas Bellinger 
25892eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
25902eafd729SChristoph Hellwig 		const char *page, size_t count)
2591c66ac9dbSNicholas Bellinger {
25922eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2593c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2594c66ac9dbSNicholas Bellinger 	unsigned long tg_pt_gp_id;
2595c66ac9dbSNicholas Bellinger 	int ret;
2596c66ac9dbSNicholas Bellinger 
259757103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tg_pt_gp_id);
2598c66ac9dbSNicholas Bellinger 	if (ret < 0) {
259957103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2600c66ac9dbSNicholas Bellinger 			" tg_pt_gp_id\n", ret);
260157103d7fSJingoo Han 		return ret;
2602c66ac9dbSNicholas Bellinger 	}
2603c66ac9dbSNicholas Bellinger 	if (tg_pt_gp_id > 0x0000ffff) {
26046708bb27SAndy Grover 		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2605c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", tg_pt_gp_id);
2606c66ac9dbSNicholas Bellinger 		return -EINVAL;
2607c66ac9dbSNicholas Bellinger 	}
2608c66ac9dbSNicholas Bellinger 
2609c66ac9dbSNicholas Bellinger 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2610c66ac9dbSNicholas Bellinger 	if (ret < 0)
2611c66ac9dbSNicholas Bellinger 		return -EINVAL;
2612c66ac9dbSNicholas Bellinger 
26136708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2614c66ac9dbSNicholas Bellinger 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
2615c66ac9dbSNicholas Bellinger 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
2616c66ac9dbSNicholas Bellinger 		tg_pt_gp->tg_pt_gp_id);
2617c66ac9dbSNicholas Bellinger 
2618c66ac9dbSNicholas Bellinger 	return count;
2619c66ac9dbSNicholas Bellinger }
2620c66ac9dbSNicholas Bellinger 
26212eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2622c66ac9dbSNicholas Bellinger 		char *page)
2623c66ac9dbSNicholas Bellinger {
26242eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2625c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
2626c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2627c66ac9dbSNicholas Bellinger 	unsigned char buf[TG_PT_GROUP_NAME_BUF];
2628c66ac9dbSNicholas Bellinger 
2629c66ac9dbSNicholas Bellinger 	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2630c66ac9dbSNicholas Bellinger 
2631c66ac9dbSNicholas Bellinger 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2632adf653f9SChristoph Hellwig 	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2633adf653f9SChristoph Hellwig 			lun_tg_pt_gp_link) {
2634adf653f9SChristoph Hellwig 		struct se_portal_group *tpg = lun->lun_tpg;
2635c66ac9dbSNicholas Bellinger 
2636c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2637e3d6f909SAndy Grover 			"/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2638e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2639e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
2640c66ac9dbSNicholas Bellinger 			config_item_name(&lun->lun_group.cg_item));
2641c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2642c66ac9dbSNicholas Bellinger 
2643c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
26446708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2645c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2646c66ac9dbSNicholas Bellinger 			break;
2647c66ac9dbSNicholas Bellinger 		}
2648c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2649c66ac9dbSNicholas Bellinger 		len += cur_len;
2650c66ac9dbSNicholas Bellinger 	}
2651c66ac9dbSNicholas Bellinger 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2652c66ac9dbSNicholas Bellinger 
2653c66ac9dbSNicholas Bellinger 	return len;
2654c66ac9dbSNicholas Bellinger }
2655c66ac9dbSNicholas Bellinger 
26562eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
26572eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
26582eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
26592eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
26602eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
26612eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
26622eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
26632eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
26642eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
26652eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
26662eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
26672eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
26682eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
26692eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
26702eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
26712eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
26722eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
2673c66ac9dbSNicholas Bellinger 
2674c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
26752eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_state,
26762eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_status,
26772eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_type,
26782eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_transitioning,
26792eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_offline,
26802eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_lba_dependent,
26812eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_unavailable,
26822eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_standby,
26832eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_nonoptimized,
26842eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_optimized,
26852eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_write_metadata,
26862eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_nonop_delay_msecs,
26872eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_trans_delay_msecs,
26882eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_implicit_trans_secs,
26892eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_preferred,
26902eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_tg_pt_gp_id,
26912eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_members,
2692c66ac9dbSNicholas Bellinger 	NULL,
2693c66ac9dbSNicholas Bellinger };
2694c66ac9dbSNicholas Bellinger 
26951f6fe7cbSNicholas Bellinger static void target_core_alua_tg_pt_gp_release(struct config_item *item)
26961f6fe7cbSNicholas Bellinger {
26971f6fe7cbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
26981f6fe7cbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
26991f6fe7cbSNicholas Bellinger 
27001f6fe7cbSNicholas Bellinger 	core_alua_free_tg_pt_gp(tg_pt_gp);
27011f6fe7cbSNicholas Bellinger }
27021f6fe7cbSNicholas Bellinger 
2703c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
27041f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_tg_pt_gp_release,
2705c66ac9dbSNicholas Bellinger };
2706c66ac9dbSNicholas Bellinger 
2707c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2708c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
2709c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
2710c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2711c66ac9dbSNicholas Bellinger };
2712c66ac9dbSNicholas Bellinger 
2713c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2714c66ac9dbSNicholas Bellinger 
271572aca57bSNicholas Bellinger /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2716c66ac9dbSNicholas Bellinger 
2717c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_tg_pt_gp(
2718c66ac9dbSNicholas Bellinger 	struct config_group *group,
2719c66ac9dbSNicholas Bellinger 	const char *name)
2720c66ac9dbSNicholas Bellinger {
2721c66ac9dbSNicholas Bellinger 	struct t10_alua *alua = container_of(group, struct t10_alua,
2722c66ac9dbSNicholas Bellinger 					alua_tg_pt_gps_group);
2723c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2724c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = NULL;
2725c66ac9dbSNicholas Bellinger 	struct config_item *alua_tg_pt_gp_ci = NULL;
2726c66ac9dbSNicholas Bellinger 
27270fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
27286708bb27SAndy Grover 	if (!tg_pt_gp)
2729c66ac9dbSNicholas Bellinger 		return NULL;
2730c66ac9dbSNicholas Bellinger 
2731c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2732c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2733c66ac9dbSNicholas Bellinger 
2734c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
2735c66ac9dbSNicholas Bellinger 			&target_core_alua_tg_pt_gp_cit);
2736c66ac9dbSNicholas Bellinger 
27376708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2738c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s\n",
2739c66ac9dbSNicholas Bellinger 		config_item_name(alua_tg_pt_gp_ci));
2740c66ac9dbSNicholas Bellinger 
2741c66ac9dbSNicholas Bellinger 	return alua_tg_pt_gp_cg;
2742c66ac9dbSNicholas Bellinger }
2743c66ac9dbSNicholas Bellinger 
2744c66ac9dbSNicholas Bellinger static void target_core_alua_drop_tg_pt_gp(
2745c66ac9dbSNicholas Bellinger 	struct config_group *group,
2746c66ac9dbSNicholas Bellinger 	struct config_item *item)
2747c66ac9dbSNicholas Bellinger {
2748c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2749c66ac9dbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2750c66ac9dbSNicholas Bellinger 
27516708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2752c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
2753c66ac9dbSNicholas Bellinger 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
27541f6fe7cbSNicholas Bellinger 	/*
27551f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
27561f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_tg_pt_gp_release().
27571f6fe7cbSNicholas Bellinger 	 */
2758c66ac9dbSNicholas Bellinger 	config_item_put(item);
2759c66ac9dbSNicholas Bellinger }
2760c66ac9dbSNicholas Bellinger 
2761c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2762c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_tg_pt_gp,
2763c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
2764c66ac9dbSNicholas Bellinger };
2765c66ac9dbSNicholas Bellinger 
276672aca57bSNicholas Bellinger TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
2767c66ac9dbSNicholas Bellinger 
276872aca57bSNicholas Bellinger /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2769c66ac9dbSNicholas Bellinger 
2770c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_cit */
2771c66ac9dbSNicholas Bellinger 
2772c66ac9dbSNicholas Bellinger /*
2773c66ac9dbSNicholas Bellinger  * target_core_alua_cit is a ConfigFS group that lives under
2774c66ac9dbSNicholas Bellinger  * /sys/kernel/config/target/core/alua.  There are default groups
2775c66ac9dbSNicholas Bellinger  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2776c66ac9dbSNicholas Bellinger  * target_core_alua_cit in target_core_init_configfs() below.
2777c66ac9dbSNicholas Bellinger  */
2778c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_cit = {
2779c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2780c66ac9dbSNicholas Bellinger 	.ct_attrs		= NULL,
2781c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2782c66ac9dbSNicholas Bellinger };
2783c66ac9dbSNicholas Bellinger 
2784c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_cit */
2785c66ac9dbSNicholas Bellinger 
2786d23ab570SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_stat_cit */
278712d23384SNicholas Bellinger 
278812d23384SNicholas Bellinger static struct config_group *target_core_stat_mkdir(
278912d23384SNicholas Bellinger 	struct config_group *group,
279012d23384SNicholas Bellinger 	const char *name)
279112d23384SNicholas Bellinger {
279212d23384SNicholas Bellinger 	return ERR_PTR(-ENOSYS);
279312d23384SNicholas Bellinger }
279412d23384SNicholas Bellinger 
279512d23384SNicholas Bellinger static void target_core_stat_rmdir(
279612d23384SNicholas Bellinger 	struct config_group *group,
279712d23384SNicholas Bellinger 	struct config_item *item)
279812d23384SNicholas Bellinger {
279912d23384SNicholas Bellinger 	return;
280012d23384SNicholas Bellinger }
280112d23384SNicholas Bellinger 
280212d23384SNicholas Bellinger static struct configfs_group_operations target_core_stat_group_ops = {
280312d23384SNicholas Bellinger 	.make_group		= &target_core_stat_mkdir,
280412d23384SNicholas Bellinger 	.drop_item		= &target_core_stat_rmdir,
280512d23384SNicholas Bellinger };
280612d23384SNicholas Bellinger 
2807d23ab570SNicholas Bellinger TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
280812d23384SNicholas Bellinger 
2809d23ab570SNicholas Bellinger /* End functions for struct config_item_type tb_dev_stat_cit */
281012d23384SNicholas Bellinger 
2811c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_hba_cit */
2812c66ac9dbSNicholas Bellinger 
2813c66ac9dbSNicholas Bellinger static struct config_group *target_core_make_subdev(
2814c66ac9dbSNicholas Bellinger 	struct config_group *group,
2815c66ac9dbSNicholas Bellinger 	const char *name)
2816c66ac9dbSNicholas Bellinger {
2817c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2818c66ac9dbSNicholas Bellinger 	struct config_item *hba_ci = &group->cg_item;
2819c66ac9dbSNicholas Bellinger 	struct se_hba *hba = item_to_hba(hba_ci);
28200a06d430SChristoph Hellwig 	struct target_backend *tb = hba->backend;
28210fd97ccfSChristoph Hellwig 	struct se_device *dev;
2822c66ac9dbSNicholas Bellinger 	struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
282312d23384SNicholas Bellinger 	struct config_group *dev_stat_grp = NULL;
282412d23384SNicholas Bellinger 	int errno = -ENOMEM, ret;
2825c66ac9dbSNicholas Bellinger 
282612d23384SNicholas Bellinger 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
282712d23384SNicholas Bellinger 	if (ret)
282812d23384SNicholas Bellinger 		return ERR_PTR(ret);
2829c66ac9dbSNicholas Bellinger 
28300fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, name);
28310fd97ccfSChristoph Hellwig 	if (!dev)
28320fd97ccfSChristoph Hellwig 		goto out_unlock;
2833c66ac9dbSNicholas Bellinger 
28340fd97ccfSChristoph Hellwig 	dev_cg = &dev->dev_group;
2835c66ac9dbSNicholas Bellinger 
283613f6a914SSebastian Andrzej Siewior 	dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
2837c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
28386708bb27SAndy Grover 	if (!dev_cg->default_groups)
28390fd97ccfSChristoph Hellwig 		goto out_free_device;
2840c66ac9dbSNicholas Bellinger 
28410a06d430SChristoph Hellwig 	config_group_init_type_name(dev_cg, name, &tb->tb_dev_cit);
28420fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
28430a06d430SChristoph Hellwig 			&tb->tb_dev_attrib_cit);
28440fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_pr_group, "pr",
28450a06d430SChristoph Hellwig 			&tb->tb_dev_pr_cit);
28460fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
28470a06d430SChristoph Hellwig 			&tb->tb_dev_wwn_cit);
28480fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
28490a06d430SChristoph Hellwig 			"alua", &tb->tb_dev_alua_tg_pt_gps_cit);
28500fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
28510a06d430SChristoph Hellwig 			"statistics", &tb->tb_dev_stat_cit);
285212d23384SNicholas Bellinger 
28530fd97ccfSChristoph Hellwig 	dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
28540fd97ccfSChristoph Hellwig 	dev_cg->default_groups[1] = &dev->dev_pr_group;
28550fd97ccfSChristoph Hellwig 	dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
28560fd97ccfSChristoph Hellwig 	dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
28570fd97ccfSChristoph Hellwig 	dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
285812d23384SNicholas Bellinger 	dev_cg->default_groups[5] = NULL;
2859c66ac9dbSNicholas Bellinger 	/*
286012d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2861c66ac9dbSNicholas Bellinger 	 */
28620fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
28636708bb27SAndy Grover 	if (!tg_pt_gp)
28640fd97ccfSChristoph Hellwig 		goto out_free_dev_cg_default_groups;
28650fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2866c66ac9dbSNicholas Bellinger 
28670fd97ccfSChristoph Hellwig 	tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
286813f6a914SSebastian Andrzej Siewior 	tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2869c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
28706708bb27SAndy Grover 	if (!tg_pt_gp_cg->default_groups) {
28716708bb27SAndy Grover 		pr_err("Unable to allocate tg_pt_gp_cg->"
2872c66ac9dbSNicholas Bellinger 				"default_groups\n");
28730fd97ccfSChristoph Hellwig 		goto out_free_tg_pt_gp;
2874c66ac9dbSNicholas Bellinger 	}
2875c66ac9dbSNicholas Bellinger 
2876c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2877c66ac9dbSNicholas Bellinger 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2878c66ac9dbSNicholas Bellinger 	tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2879c66ac9dbSNicholas Bellinger 	tg_pt_gp_cg->default_groups[1] = NULL;
288012d23384SNicholas Bellinger 	/*
288112d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/statistics/ default groups
288212d23384SNicholas Bellinger 	 */
28830fd97ccfSChristoph Hellwig 	dev_stat_grp = &dev->dev_stat_grps.stat_group;
288413f6a914SSebastian Andrzej Siewior 	dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
288512d23384SNicholas Bellinger 				GFP_KERNEL);
288612d23384SNicholas Bellinger 	if (!dev_stat_grp->default_groups) {
28876708bb27SAndy Grover 		pr_err("Unable to allocate dev_stat_grp->default_groups\n");
28880fd97ccfSChristoph Hellwig 		goto out_free_tg_pt_gp_cg_default_groups;
288912d23384SNicholas Bellinger 	}
28900fd97ccfSChristoph Hellwig 	target_stat_setup_dev_default_groups(dev);
2891c66ac9dbSNicholas Bellinger 
2892c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
28930fd97ccfSChristoph Hellwig 	return dev_cg;
28940fd97ccfSChristoph Hellwig 
28950fd97ccfSChristoph Hellwig out_free_tg_pt_gp_cg_default_groups:
2896c66ac9dbSNicholas Bellinger 	kfree(tg_pt_gp_cg->default_groups);
28970fd97ccfSChristoph Hellwig out_free_tg_pt_gp:
28980fd97ccfSChristoph Hellwig 	core_alua_free_tg_pt_gp(tg_pt_gp);
28990fd97ccfSChristoph Hellwig out_free_dev_cg_default_groups:
2900c66ac9dbSNicholas Bellinger 	kfree(dev_cg->default_groups);
29010fd97ccfSChristoph Hellwig out_free_device:
29020fd97ccfSChristoph Hellwig 	target_free_device(dev);
29030fd97ccfSChristoph Hellwig out_unlock:
2904c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
290512d23384SNicholas Bellinger 	return ERR_PTR(errno);
2906c66ac9dbSNicholas Bellinger }
2907c66ac9dbSNicholas Bellinger 
2908c66ac9dbSNicholas Bellinger static void target_core_drop_subdev(
2909c66ac9dbSNicholas Bellinger 	struct config_group *group,
2910c66ac9dbSNicholas Bellinger 	struct config_item *item)
2911c66ac9dbSNicholas Bellinger {
29120fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
29130fd97ccfSChristoph Hellwig 	struct se_device *dev =
29140fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2915c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2916c66ac9dbSNicholas Bellinger 	struct config_item *df_item;
29170fd97ccfSChristoph Hellwig 	struct config_group *tg_pt_gp_cg, *dev_stat_grp;
29181f6fe7cbSNicholas Bellinger 	int i;
2919c66ac9dbSNicholas Bellinger 
29200fd97ccfSChristoph Hellwig 	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
2921c66ac9dbSNicholas Bellinger 
29221f6fe7cbSNicholas Bellinger 	mutex_lock(&hba->hba_access_mutex);
2923c66ac9dbSNicholas Bellinger 
29240fd97ccfSChristoph Hellwig 	dev_stat_grp = &dev->dev_stat_grps.stat_group;
292512d23384SNicholas Bellinger 	for (i = 0; dev_stat_grp->default_groups[i]; i++) {
292612d23384SNicholas Bellinger 		df_item = &dev_stat_grp->default_groups[i]->cg_item;
292712d23384SNicholas Bellinger 		dev_stat_grp->default_groups[i] = NULL;
292812d23384SNicholas Bellinger 		config_item_put(df_item);
292912d23384SNicholas Bellinger 	}
293012d23384SNicholas Bellinger 	kfree(dev_stat_grp->default_groups);
293112d23384SNicholas Bellinger 
29320fd97ccfSChristoph Hellwig 	tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2933c66ac9dbSNicholas Bellinger 	for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2934c66ac9dbSNicholas Bellinger 		df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2935c66ac9dbSNicholas Bellinger 		tg_pt_gp_cg->default_groups[i] = NULL;
2936c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
2937c66ac9dbSNicholas Bellinger 	}
2938c66ac9dbSNicholas Bellinger 	kfree(tg_pt_gp_cg->default_groups);
29391f6fe7cbSNicholas Bellinger 	/*
29401f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
29411f6fe7cbSNicholas Bellinger 	 * directly from target_core_alua_tg_pt_gp_release().
29421f6fe7cbSNicholas Bellinger 	 */
29430fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = NULL;
2944c66ac9dbSNicholas Bellinger 
2945c66ac9dbSNicholas Bellinger 	for (i = 0; dev_cg->default_groups[i]; i++) {
2946c66ac9dbSNicholas Bellinger 		df_item = &dev_cg->default_groups[i]->cg_item;
2947c66ac9dbSNicholas Bellinger 		dev_cg->default_groups[i] = NULL;
2948c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
2949c66ac9dbSNicholas Bellinger 	}
29501f6fe7cbSNicholas Bellinger 	/*
29510fd97ccfSChristoph Hellwig 	 * se_dev is released from target_core_dev_item_ops->release()
29521f6fe7cbSNicholas Bellinger 	 */
2953c66ac9dbSNicholas Bellinger 	config_item_put(item);
2954c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
2955c66ac9dbSNicholas Bellinger }
2956c66ac9dbSNicholas Bellinger 
2957c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_hba_group_ops = {
2958c66ac9dbSNicholas Bellinger 	.make_group		= target_core_make_subdev,
2959c66ac9dbSNicholas Bellinger 	.drop_item		= target_core_drop_subdev,
2960c66ac9dbSNicholas Bellinger };
2961c66ac9dbSNicholas Bellinger 
2962c66ac9dbSNicholas Bellinger 
29632eafd729SChristoph Hellwig static inline struct se_hba *to_hba(struct config_item *item)
2964c66ac9dbSNicholas Bellinger {
29652eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_hba, hba_group);
29662eafd729SChristoph Hellwig }
29672eafd729SChristoph Hellwig 
29682eafd729SChristoph Hellwig static ssize_t target_hba_info_show(struct config_item *item, char *page)
29692eafd729SChristoph Hellwig {
29702eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
29712eafd729SChristoph Hellwig 
2972c66ac9dbSNicholas Bellinger 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
29730a06d430SChristoph Hellwig 			hba->hba_id, hba->backend->ops->name,
2974ce8dd25dSChristoph Hellwig 			TARGET_CORE_VERSION);
2975c66ac9dbSNicholas Bellinger }
2976c66ac9dbSNicholas Bellinger 
29772eafd729SChristoph Hellwig static ssize_t target_hba_mode_show(struct config_item *item, char *page)
2978c66ac9dbSNicholas Bellinger {
29792eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
2980c66ac9dbSNicholas Bellinger 	int hba_mode = 0;
2981c66ac9dbSNicholas Bellinger 
2982c66ac9dbSNicholas Bellinger 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2983c66ac9dbSNicholas Bellinger 		hba_mode = 1;
2984c66ac9dbSNicholas Bellinger 
2985c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", hba_mode);
2986c66ac9dbSNicholas Bellinger }
2987c66ac9dbSNicholas Bellinger 
29882eafd729SChristoph Hellwig static ssize_t target_hba_mode_store(struct config_item *item,
2989c66ac9dbSNicholas Bellinger 		const char *page, size_t count)
2990c66ac9dbSNicholas Bellinger {
29912eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
2992c66ac9dbSNicholas Bellinger 	unsigned long mode_flag;
2993c66ac9dbSNicholas Bellinger 	int ret;
2994c66ac9dbSNicholas Bellinger 
29950a06d430SChristoph Hellwig 	if (hba->backend->ops->pmode_enable_hba == NULL)
2996c66ac9dbSNicholas Bellinger 		return -EINVAL;
2997c66ac9dbSNicholas Bellinger 
299857103d7fSJingoo Han 	ret = kstrtoul(page, 0, &mode_flag);
2999c66ac9dbSNicholas Bellinger 	if (ret < 0) {
30006708bb27SAndy Grover 		pr_err("Unable to extract hba mode flag: %d\n", ret);
300157103d7fSJingoo Han 		return ret;
3002c66ac9dbSNicholas Bellinger 	}
3003c66ac9dbSNicholas Bellinger 
30040fd97ccfSChristoph Hellwig 	if (hba->dev_count) {
30056708bb27SAndy Grover 		pr_err("Unable to set hba_mode with active devices\n");
3006c66ac9dbSNicholas Bellinger 		return -EINVAL;
3007c66ac9dbSNicholas Bellinger 	}
3008c66ac9dbSNicholas Bellinger 
30090a06d430SChristoph Hellwig 	ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3010c66ac9dbSNicholas Bellinger 	if (ret < 0)
3011c66ac9dbSNicholas Bellinger 		return -EINVAL;
3012c66ac9dbSNicholas Bellinger 	if (ret > 0)
3013c66ac9dbSNicholas Bellinger 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3014c66ac9dbSNicholas Bellinger 	else if (ret == 0)
3015c66ac9dbSNicholas Bellinger 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3016c66ac9dbSNicholas Bellinger 
3017c66ac9dbSNicholas Bellinger 	return count;
3018c66ac9dbSNicholas Bellinger }
3019c66ac9dbSNicholas Bellinger 
30202eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_, hba_info);
30212eafd729SChristoph Hellwig CONFIGFS_ATTR(target_, hba_mode);
3022c66ac9dbSNicholas Bellinger 
30231f6fe7cbSNicholas Bellinger static void target_core_hba_release(struct config_item *item)
30241f6fe7cbSNicholas Bellinger {
30251f6fe7cbSNicholas Bellinger 	struct se_hba *hba = container_of(to_config_group(item),
30261f6fe7cbSNicholas Bellinger 				struct se_hba, hba_group);
30271f6fe7cbSNicholas Bellinger 	core_delete_hba(hba);
30281f6fe7cbSNicholas Bellinger }
30291f6fe7cbSNicholas Bellinger 
3030c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_hba_attrs[] = {
30312eafd729SChristoph Hellwig 	&target_attr_hba_info,
30322eafd729SChristoph Hellwig 	&target_attr_hba_mode,
3033c66ac9dbSNicholas Bellinger 	NULL,
3034c66ac9dbSNicholas Bellinger };
3035c66ac9dbSNicholas Bellinger 
3036c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_hba_item_ops = {
30371f6fe7cbSNicholas Bellinger 	.release		= target_core_hba_release,
3038c66ac9dbSNicholas Bellinger };
3039c66ac9dbSNicholas Bellinger 
3040c66ac9dbSNicholas Bellinger static struct config_item_type target_core_hba_cit = {
3041c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_hba_item_ops,
3042c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_hba_group_ops,
3043c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_hba_attrs,
3044c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3045c66ac9dbSNicholas Bellinger };
3046c66ac9dbSNicholas Bellinger 
3047c66ac9dbSNicholas Bellinger static struct config_group *target_core_call_addhbatotarget(
3048c66ac9dbSNicholas Bellinger 	struct config_group *group,
3049c66ac9dbSNicholas Bellinger 	const char *name)
3050c66ac9dbSNicholas Bellinger {
3051c66ac9dbSNicholas Bellinger 	char *se_plugin_str, *str, *str2;
3052c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
3053c66ac9dbSNicholas Bellinger 	char buf[TARGET_CORE_NAME_MAX_LEN];
3054c66ac9dbSNicholas Bellinger 	unsigned long plugin_dep_id = 0;
3055c66ac9dbSNicholas Bellinger 	int ret;
3056c66ac9dbSNicholas Bellinger 
3057c66ac9dbSNicholas Bellinger 	memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
305860d645a4SDan Carpenter 	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
30596708bb27SAndy Grover 		pr_err("Passed *name strlen(): %d exceeds"
3060c66ac9dbSNicholas Bellinger 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3061c66ac9dbSNicholas Bellinger 			TARGET_CORE_NAME_MAX_LEN);
3062c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENAMETOOLONG);
3063c66ac9dbSNicholas Bellinger 	}
3064c66ac9dbSNicholas Bellinger 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3065c66ac9dbSNicholas Bellinger 
3066c66ac9dbSNicholas Bellinger 	str = strstr(buf, "_");
30676708bb27SAndy Grover 	if (!str) {
30686708bb27SAndy Grover 		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3069c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
3070c66ac9dbSNicholas Bellinger 	}
3071c66ac9dbSNicholas Bellinger 	se_plugin_str = buf;
3072c66ac9dbSNicholas Bellinger 	/*
3073c66ac9dbSNicholas Bellinger 	 * Special case for subsystem plugins that have "_" in their names.
3074c66ac9dbSNicholas Bellinger 	 * Namely rd_direct and rd_mcp..
3075c66ac9dbSNicholas Bellinger 	 */
3076c66ac9dbSNicholas Bellinger 	str2 = strstr(str+1, "_");
30776708bb27SAndy Grover 	if (str2) {
3078c66ac9dbSNicholas Bellinger 		*str2 = '\0'; /* Terminate for *se_plugin_str */
3079c66ac9dbSNicholas Bellinger 		str2++; /* Skip to start of plugin dependent ID */
3080c66ac9dbSNicholas Bellinger 		str = str2;
3081c66ac9dbSNicholas Bellinger 	} else {
3082c66ac9dbSNicholas Bellinger 		*str = '\0'; /* Terminate for *se_plugin_str */
3083c66ac9dbSNicholas Bellinger 		str++; /* Skip to start of plugin dependent ID */
3084c66ac9dbSNicholas Bellinger 	}
3085c66ac9dbSNicholas Bellinger 
308657103d7fSJingoo Han 	ret = kstrtoul(str, 0, &plugin_dep_id);
3087c66ac9dbSNicholas Bellinger 	if (ret < 0) {
308857103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
3089c66ac9dbSNicholas Bellinger 				" plugin_dep_id\n", ret);
309057103d7fSJingoo Han 		return ERR_PTR(ret);
3091c66ac9dbSNicholas Bellinger 	}
3092c66ac9dbSNicholas Bellinger 	/*
3093c66ac9dbSNicholas Bellinger 	 * Load up TCM subsystem plugins if they have not already been loaded.
3094c66ac9dbSNicholas Bellinger 	 */
3095dbc5623eSNicholas Bellinger 	transport_subsystem_check_init();
3096c66ac9dbSNicholas Bellinger 
3097c66ac9dbSNicholas Bellinger 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3098c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
3099c66ac9dbSNicholas Bellinger 		return ERR_CAST(hba);
3100c66ac9dbSNicholas Bellinger 
3101c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&hba->hba_group, name,
3102c66ac9dbSNicholas Bellinger 			&target_core_hba_cit);
3103c66ac9dbSNicholas Bellinger 
3104c66ac9dbSNicholas Bellinger 	return &hba->hba_group;
3105c66ac9dbSNicholas Bellinger }
3106c66ac9dbSNicholas Bellinger 
3107c66ac9dbSNicholas Bellinger static void target_core_call_delhbafromtarget(
3108c66ac9dbSNicholas Bellinger 	struct config_group *group,
3109c66ac9dbSNicholas Bellinger 	struct config_item *item)
3110c66ac9dbSNicholas Bellinger {
31111f6fe7cbSNicholas Bellinger 	/*
31121f6fe7cbSNicholas Bellinger 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
31131f6fe7cbSNicholas Bellinger 	 * -> target_core_hba_release()
31141f6fe7cbSNicholas Bellinger 	 */
3115c66ac9dbSNicholas Bellinger 	config_item_put(item);
3116c66ac9dbSNicholas Bellinger }
3117c66ac9dbSNicholas Bellinger 
3118c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_group_ops = {
3119c66ac9dbSNicholas Bellinger 	.make_group	= target_core_call_addhbatotarget,
3120c66ac9dbSNicholas Bellinger 	.drop_item	= target_core_call_delhbafromtarget,
3121c66ac9dbSNicholas Bellinger };
3122c66ac9dbSNicholas Bellinger 
3123c66ac9dbSNicholas Bellinger static struct config_item_type target_core_cit = {
3124c66ac9dbSNicholas Bellinger 	.ct_item_ops	= NULL,
3125c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_group_ops,
3126c66ac9dbSNicholas Bellinger 	.ct_attrs	= NULL,
3127c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
3128c66ac9dbSNicholas Bellinger };
3129c66ac9dbSNicholas Bellinger 
3130c66ac9dbSNicholas Bellinger /* Stop functions for struct config_item_type target_core_hba_cit */
3131c66ac9dbSNicholas Bellinger 
31320a06d430SChristoph Hellwig void target_setup_backend_cits(struct target_backend *tb)
313373112edcSNicholas Bellinger {
31340a06d430SChristoph Hellwig 	target_core_setup_dev_cit(tb);
31350a06d430SChristoph Hellwig 	target_core_setup_dev_attrib_cit(tb);
31360a06d430SChristoph Hellwig 	target_core_setup_dev_pr_cit(tb);
31370a06d430SChristoph Hellwig 	target_core_setup_dev_wwn_cit(tb);
31380a06d430SChristoph Hellwig 	target_core_setup_dev_alua_tg_pt_gps_cit(tb);
31390a06d430SChristoph Hellwig 	target_core_setup_dev_stat_cit(tb);
314073112edcSNicholas Bellinger }
314173112edcSNicholas Bellinger 
314254550fabSAxel Lin static int __init target_core_init_configfs(void)
3143c66ac9dbSNicholas Bellinger {
3144c66ac9dbSNicholas Bellinger 	struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3145c66ac9dbSNicholas Bellinger 	struct config_group *lu_gp_cg = NULL;
3146d588cf8fSChristoph Hellwig 	struct configfs_subsystem *subsys = &target_core_fabrics;
3147c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
3148c66ac9dbSNicholas Bellinger 	int ret;
3149c66ac9dbSNicholas Bellinger 
31506708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3151c66ac9dbSNicholas Bellinger 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
3152c66ac9dbSNicholas Bellinger 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3153c66ac9dbSNicholas Bellinger 
3154c66ac9dbSNicholas Bellinger 	config_group_init(&subsys->su_group);
3155c66ac9dbSNicholas Bellinger 	mutex_init(&subsys->su_mutex);
3156c66ac9dbSNicholas Bellinger 
3157e3d6f909SAndy Grover 	ret = init_se_kmem_caches();
3158c66ac9dbSNicholas Bellinger 	if (ret < 0)
3159e3d6f909SAndy Grover 		return ret;
3160c66ac9dbSNicholas Bellinger 	/*
3161c66ac9dbSNicholas Bellinger 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3162c66ac9dbSNicholas Bellinger 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3163c66ac9dbSNicholas Bellinger 	 */
3164c66ac9dbSNicholas Bellinger 	target_cg = &subsys->su_group;
3165ab6dae82SAndy Grover 	target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3166c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
31676708bb27SAndy Grover 	if (!target_cg->default_groups) {
31686708bb27SAndy Grover 		pr_err("Unable to allocate target_cg->default_groups\n");
316937bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3170c66ac9dbSNicholas Bellinger 		goto out_global;
3171c66ac9dbSNicholas Bellinger 	}
3172c66ac9dbSNicholas Bellinger 
3173e3d6f909SAndy Grover 	config_group_init_type_name(&target_core_hbagroup,
3174c66ac9dbSNicholas Bellinger 			"core", &target_core_cit);
3175e3d6f909SAndy Grover 	target_cg->default_groups[0] = &target_core_hbagroup;
3176c66ac9dbSNicholas Bellinger 	target_cg->default_groups[1] = NULL;
3177c66ac9dbSNicholas Bellinger 	/*
3178c66ac9dbSNicholas Bellinger 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3179c66ac9dbSNicholas Bellinger 	 */
3180e3d6f909SAndy Grover 	hba_cg = &target_core_hbagroup;
318113f6a914SSebastian Andrzej Siewior 	hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3182c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
31836708bb27SAndy Grover 	if (!hba_cg->default_groups) {
31846708bb27SAndy Grover 		pr_err("Unable to allocate hba_cg->default_groups\n");
318537bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3186c66ac9dbSNicholas Bellinger 		goto out_global;
3187c66ac9dbSNicholas Bellinger 	}
3188e3d6f909SAndy Grover 	config_group_init_type_name(&alua_group,
3189c66ac9dbSNicholas Bellinger 			"alua", &target_core_alua_cit);
3190e3d6f909SAndy Grover 	hba_cg->default_groups[0] = &alua_group;
3191c66ac9dbSNicholas Bellinger 	hba_cg->default_groups[1] = NULL;
3192c66ac9dbSNicholas Bellinger 	/*
3193c66ac9dbSNicholas Bellinger 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3194c66ac9dbSNicholas Bellinger 	 * groups under /sys/kernel/config/target/core/alua/
3195c66ac9dbSNicholas Bellinger 	 */
3196e3d6f909SAndy Grover 	alua_cg = &alua_group;
319713f6a914SSebastian Andrzej Siewior 	alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3198c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
31996708bb27SAndy Grover 	if (!alua_cg->default_groups) {
32006708bb27SAndy Grover 		pr_err("Unable to allocate alua_cg->default_groups\n");
320137bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3202c66ac9dbSNicholas Bellinger 		goto out_global;
3203c66ac9dbSNicholas Bellinger 	}
3204c66ac9dbSNicholas Bellinger 
3205e3d6f909SAndy Grover 	config_group_init_type_name(&alua_lu_gps_group,
3206c66ac9dbSNicholas Bellinger 			"lu_gps", &target_core_alua_lu_gps_cit);
3207e3d6f909SAndy Grover 	alua_cg->default_groups[0] = &alua_lu_gps_group;
3208c66ac9dbSNicholas Bellinger 	alua_cg->default_groups[1] = NULL;
3209c66ac9dbSNicholas Bellinger 	/*
3210c66ac9dbSNicholas Bellinger 	 * Add core/alua/lu_gps/default_lu_gp
3211c66ac9dbSNicholas Bellinger 	 */
3212c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
321337bb7899SPeter Senna Tschudin 	if (IS_ERR(lu_gp)) {
321437bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3215c66ac9dbSNicholas Bellinger 		goto out_global;
321637bb7899SPeter Senna Tschudin 	}
3217c66ac9dbSNicholas Bellinger 
3218e3d6f909SAndy Grover 	lu_gp_cg = &alua_lu_gps_group;
321913f6a914SSebastian Andrzej Siewior 	lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3220c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
32216708bb27SAndy Grover 	if (!lu_gp_cg->default_groups) {
32226708bb27SAndy Grover 		pr_err("Unable to allocate lu_gp_cg->default_groups\n");
322337bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3224c66ac9dbSNicholas Bellinger 		goto out_global;
3225c66ac9dbSNicholas Bellinger 	}
3226c66ac9dbSNicholas Bellinger 
3227c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3228c66ac9dbSNicholas Bellinger 				&target_core_alua_lu_gp_cit);
3229c66ac9dbSNicholas Bellinger 	lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3230c66ac9dbSNicholas Bellinger 	lu_gp_cg->default_groups[1] = NULL;
3231e3d6f909SAndy Grover 	default_lu_gp = lu_gp;
3232c66ac9dbSNicholas Bellinger 	/*
3233c66ac9dbSNicholas Bellinger 	 * Register the target_core_mod subsystem with configfs.
3234c66ac9dbSNicholas Bellinger 	 */
3235c66ac9dbSNicholas Bellinger 	ret = configfs_register_subsystem(subsys);
3236c66ac9dbSNicholas Bellinger 	if (ret < 0) {
32376708bb27SAndy Grover 		pr_err("Error %d while registering subsystem %s\n",
3238c66ac9dbSNicholas Bellinger 			ret, subsys->su_group.cg_item.ci_namebuf);
3239c66ac9dbSNicholas Bellinger 		goto out_global;
3240c66ac9dbSNicholas Bellinger 	}
32416708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3242ce8dd25dSChristoph Hellwig 		" Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3243c66ac9dbSNicholas Bellinger 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3244c66ac9dbSNicholas Bellinger 	/*
3245c66ac9dbSNicholas Bellinger 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3246c66ac9dbSNicholas Bellinger 	 */
3247c66ac9dbSNicholas Bellinger 	ret = rd_module_init();
3248c66ac9dbSNicholas Bellinger 	if (ret < 0)
3249c66ac9dbSNicholas Bellinger 		goto out;
3250c66ac9dbSNicholas Bellinger 
32510d0f9dfbSRoland Dreier 	ret = core_dev_setup_virtual_lun0();
32520d0f9dfbSRoland Dreier 	if (ret < 0)
3253c66ac9dbSNicholas Bellinger 		goto out;
3254c66ac9dbSNicholas Bellinger 
3255f99715acSNicholas Bellinger 	ret = target_xcopy_setup_pt();
3256f99715acSNicholas Bellinger 	if (ret < 0)
3257f99715acSNicholas Bellinger 		goto out;
3258f99715acSNicholas Bellinger 
3259c66ac9dbSNicholas Bellinger 	return 0;
3260c66ac9dbSNicholas Bellinger 
3261c66ac9dbSNicholas Bellinger out:
3262c66ac9dbSNicholas Bellinger 	configfs_unregister_subsystem(subsys);
3263c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3264c66ac9dbSNicholas Bellinger 	rd_module_exit();
3265c66ac9dbSNicholas Bellinger out_global:
3266e3d6f909SAndy Grover 	if (default_lu_gp) {
3267e3d6f909SAndy Grover 		core_alua_free_lu_gp(default_lu_gp);
3268e3d6f909SAndy Grover 		default_lu_gp = NULL;
3269c66ac9dbSNicholas Bellinger 	}
3270c66ac9dbSNicholas Bellinger 	if (lu_gp_cg)
3271c66ac9dbSNicholas Bellinger 		kfree(lu_gp_cg->default_groups);
3272c66ac9dbSNicholas Bellinger 	if (alua_cg)
3273c66ac9dbSNicholas Bellinger 		kfree(alua_cg->default_groups);
3274c66ac9dbSNicholas Bellinger 	if (hba_cg)
3275c66ac9dbSNicholas Bellinger 		kfree(hba_cg->default_groups);
3276c66ac9dbSNicholas Bellinger 	kfree(target_cg->default_groups);
3277e3d6f909SAndy Grover 	release_se_kmem_caches();
3278e3d6f909SAndy Grover 	return ret;
3279c66ac9dbSNicholas Bellinger }
3280c66ac9dbSNicholas Bellinger 
328154550fabSAxel Lin static void __exit target_core_exit_configfs(void)
3282c66ac9dbSNicholas Bellinger {
3283c66ac9dbSNicholas Bellinger 	struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3284c66ac9dbSNicholas Bellinger 	struct config_item *item;
3285c66ac9dbSNicholas Bellinger 	int i;
3286c66ac9dbSNicholas Bellinger 
3287e3d6f909SAndy Grover 	lu_gp_cg = &alua_lu_gps_group;
3288c66ac9dbSNicholas Bellinger 	for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3289c66ac9dbSNicholas Bellinger 		item = &lu_gp_cg->default_groups[i]->cg_item;
3290c66ac9dbSNicholas Bellinger 		lu_gp_cg->default_groups[i] = NULL;
3291c66ac9dbSNicholas Bellinger 		config_item_put(item);
3292c66ac9dbSNicholas Bellinger 	}
3293c66ac9dbSNicholas Bellinger 	kfree(lu_gp_cg->default_groups);
32947c2bf6e9SNicholas Bellinger 	lu_gp_cg->default_groups = NULL;
3295c66ac9dbSNicholas Bellinger 
3296e3d6f909SAndy Grover 	alua_cg = &alua_group;
3297c66ac9dbSNicholas Bellinger 	for (i = 0; alua_cg->default_groups[i]; i++) {
3298c66ac9dbSNicholas Bellinger 		item = &alua_cg->default_groups[i]->cg_item;
3299c66ac9dbSNicholas Bellinger 		alua_cg->default_groups[i] = NULL;
3300c66ac9dbSNicholas Bellinger 		config_item_put(item);
3301c66ac9dbSNicholas Bellinger 	}
3302c66ac9dbSNicholas Bellinger 	kfree(alua_cg->default_groups);
33037c2bf6e9SNicholas Bellinger 	alua_cg->default_groups = NULL;
3304c66ac9dbSNicholas Bellinger 
3305e3d6f909SAndy Grover 	hba_cg = &target_core_hbagroup;
3306c66ac9dbSNicholas Bellinger 	for (i = 0; hba_cg->default_groups[i]; i++) {
3307c66ac9dbSNicholas Bellinger 		item = &hba_cg->default_groups[i]->cg_item;
3308c66ac9dbSNicholas Bellinger 		hba_cg->default_groups[i] = NULL;
3309c66ac9dbSNicholas Bellinger 		config_item_put(item);
3310c66ac9dbSNicholas Bellinger 	}
3311c66ac9dbSNicholas Bellinger 	kfree(hba_cg->default_groups);
33127c2bf6e9SNicholas Bellinger 	hba_cg->default_groups = NULL;
33137c2bf6e9SNicholas Bellinger 	/*
33147c2bf6e9SNicholas Bellinger 	 * We expect subsys->su_group.default_groups to be released
33157c2bf6e9SNicholas Bellinger 	 * by configfs subsystem provider logic..
33167c2bf6e9SNicholas Bellinger 	 */
3317d588cf8fSChristoph Hellwig 	configfs_unregister_subsystem(&target_core_fabrics);
3318d588cf8fSChristoph Hellwig 	kfree(target_core_fabrics.su_group.default_groups);
3319c66ac9dbSNicholas Bellinger 
3320e3d6f909SAndy Grover 	core_alua_free_lu_gp(default_lu_gp);
3321e3d6f909SAndy Grover 	default_lu_gp = NULL;
33227c2bf6e9SNicholas Bellinger 
33236708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3324c66ac9dbSNicholas Bellinger 			" Infrastructure\n");
3325c66ac9dbSNicholas Bellinger 
3326c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3327c66ac9dbSNicholas Bellinger 	rd_module_exit();
3328f99715acSNicholas Bellinger 	target_xcopy_release_pt();
3329e3d6f909SAndy Grover 	release_se_kmem_caches();
3330c66ac9dbSNicholas Bellinger }
3331c66ac9dbSNicholas Bellinger 
3332c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3333c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3334c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
3335c66ac9dbSNicholas Bellinger 
3336c66ac9dbSNicholas Bellinger module_init(target_core_init_configfs);
3337c66ac9dbSNicholas Bellinger module_exit(target_core_exit_configfs);
3338