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					\
5523effdb90SChristoph Hellwig 		"ignoring deprecated ##_name## attribute\n");	\
5533effdb90SChristoph Hellwig 	return count;							\
5543effdb90SChristoph Hellwig }
5553effdb90SChristoph Hellwig 
5562eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
5572eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
5583effdb90SChristoph Hellwig 
5593effdb90SChristoph Hellwig static void dev_set_t10_wwn_model_alias(struct se_device *dev)
5603effdb90SChristoph Hellwig {
5613effdb90SChristoph Hellwig 	const char *configname;
5623effdb90SChristoph Hellwig 
5633effdb90SChristoph Hellwig 	configname = config_item_name(&dev->dev_group.cg_item);
5643effdb90SChristoph Hellwig 	if (strlen(configname) >= 16) {
5653effdb90SChristoph Hellwig 		pr_warn("dev[%p]: Backstore name '%s' is too long for "
5663effdb90SChristoph Hellwig 			"INQUIRY_MODEL, truncating to 16 bytes\n", dev,
5673effdb90SChristoph Hellwig 			configname);
5683effdb90SChristoph Hellwig 	}
5693effdb90SChristoph Hellwig 	snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
5703effdb90SChristoph Hellwig }
5713effdb90SChristoph Hellwig 
5722eafd729SChristoph Hellwig static ssize_t emulate_model_alias_store(struct config_item *item,
5733effdb90SChristoph Hellwig 		const char *page, size_t count)
5743effdb90SChristoph Hellwig {
5752eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
5763effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
5773effdb90SChristoph Hellwig 	bool flag;
5783effdb90SChristoph Hellwig 	int ret;
5793effdb90SChristoph Hellwig 
5803effdb90SChristoph Hellwig 	if (dev->export_count) {
5813effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change model alias"
5823effdb90SChristoph Hellwig 			" while export_count is %d\n",
5833effdb90SChristoph Hellwig 			dev, dev->export_count);
5843effdb90SChristoph Hellwig 		return -EINVAL;
5853effdb90SChristoph Hellwig 	}
5863effdb90SChristoph Hellwig 
5873effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
5883effdb90SChristoph Hellwig 	if (ret < 0)
5893effdb90SChristoph Hellwig 		return ret;
5903effdb90SChristoph Hellwig 
5913effdb90SChristoph Hellwig 	if (flag) {
5923effdb90SChristoph Hellwig 		dev_set_t10_wwn_model_alias(dev);
5933effdb90SChristoph Hellwig 	} else {
5943effdb90SChristoph Hellwig 		strncpy(&dev->t10_wwn.model[0],
5953effdb90SChristoph Hellwig 			dev->transport->inquiry_prod, 16);
5963effdb90SChristoph Hellwig 	}
5973effdb90SChristoph Hellwig 	da->emulate_model_alias = flag;
5983effdb90SChristoph Hellwig 	return count;
5993effdb90SChristoph Hellwig }
6003effdb90SChristoph Hellwig 
6012eafd729SChristoph Hellwig static ssize_t emulate_write_cache_store(struct config_item *item,
6023effdb90SChristoph Hellwig 		const char *page, size_t count)
6033effdb90SChristoph Hellwig {
6042eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6053effdb90SChristoph Hellwig 	bool flag;
6063effdb90SChristoph Hellwig 	int ret;
6073effdb90SChristoph Hellwig 
6083effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6093effdb90SChristoph Hellwig 	if (ret < 0)
6103effdb90SChristoph Hellwig 		return ret;
6113effdb90SChristoph Hellwig 
6123effdb90SChristoph Hellwig 	if (flag && da->da_dev->transport->get_write_cache) {
6133effdb90SChristoph Hellwig 		pr_err("emulate_write_cache not supported for this device\n");
6143effdb90SChristoph Hellwig 		return -EINVAL;
6153effdb90SChristoph Hellwig 	}
6163effdb90SChristoph Hellwig 
6173effdb90SChristoph Hellwig 	da->emulate_write_cache = flag;
6183effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
6193effdb90SChristoph Hellwig 			da->da_dev, flag);
6203effdb90SChristoph Hellwig 	return count;
6213effdb90SChristoph Hellwig }
6223effdb90SChristoph Hellwig 
6232eafd729SChristoph Hellwig static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
6243effdb90SChristoph Hellwig 		const char *page, size_t count)
6253effdb90SChristoph Hellwig {
6262eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6273effdb90SChristoph Hellwig 	u32 val;
6283effdb90SChristoph Hellwig 	int ret;
6293effdb90SChristoph Hellwig 
6303effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
6313effdb90SChristoph Hellwig 	if (ret < 0)
6323effdb90SChristoph Hellwig 		return ret;
6333effdb90SChristoph Hellwig 
6343effdb90SChristoph Hellwig 	if (val != 0 && val != 1 && val != 2) {
6353effdb90SChristoph Hellwig 		pr_err("Illegal value %d\n", val);
6363effdb90SChristoph Hellwig 		return -EINVAL;
6373effdb90SChristoph Hellwig 	}
6383effdb90SChristoph Hellwig 
6393effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
6403effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
6413effdb90SChristoph Hellwig 			" UA_INTRLCK_CTRL while export_count is %d\n",
6423effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
6433effdb90SChristoph Hellwig 		return -EINVAL;
6443effdb90SChristoph Hellwig 	}
6453effdb90SChristoph Hellwig 	da->emulate_ua_intlck_ctrl = val;
6463effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
6473effdb90SChristoph Hellwig 		da->da_dev, val);
6483effdb90SChristoph Hellwig 	return count;
6493effdb90SChristoph Hellwig }
6503effdb90SChristoph Hellwig 
6512eafd729SChristoph Hellwig static ssize_t emulate_tas_store(struct config_item *item,
6523effdb90SChristoph Hellwig 		const char *page, size_t count)
6533effdb90SChristoph Hellwig {
6542eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6553effdb90SChristoph Hellwig 	bool flag;
6563effdb90SChristoph Hellwig 	int ret;
6573effdb90SChristoph Hellwig 
6583effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6593effdb90SChristoph Hellwig 	if (ret < 0)
6603effdb90SChristoph Hellwig 		return ret;
6613effdb90SChristoph Hellwig 
6623effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
6633effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TAS while"
6643effdb90SChristoph Hellwig 			" export_count is %d\n",
6653effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
6663effdb90SChristoph Hellwig 		return -EINVAL;
6673effdb90SChristoph Hellwig 	}
6683effdb90SChristoph Hellwig 	da->emulate_tas = flag;
6693effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
6703effdb90SChristoph Hellwig 		da->da_dev, flag ? "Enabled" : "Disabled");
6713effdb90SChristoph Hellwig 
6723effdb90SChristoph Hellwig 	return count;
6733effdb90SChristoph Hellwig }
6743effdb90SChristoph Hellwig 
6752eafd729SChristoph Hellwig static ssize_t emulate_tpu_store(struct config_item *item,
6763effdb90SChristoph Hellwig 		const char *page, size_t count)
6773effdb90SChristoph Hellwig {
6782eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6793effdb90SChristoph Hellwig 	bool flag;
6803effdb90SChristoph Hellwig 	int ret;
6813effdb90SChristoph Hellwig 
6823effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6833effdb90SChristoph Hellwig 	if (ret < 0)
6843effdb90SChristoph Hellwig 		return ret;
6853effdb90SChristoph Hellwig 
6863effdb90SChristoph Hellwig 	/*
6873effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
6883effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
6893effdb90SChristoph Hellwig 	 */
6903effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
6913effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
6923effdb90SChristoph Hellwig 		return -ENOSYS;
6933effdb90SChristoph Hellwig 	}
6943effdb90SChristoph Hellwig 
6953effdb90SChristoph Hellwig 	da->emulate_tpu = flag;
6963effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
6973effdb90SChristoph Hellwig 		da->da_dev, flag);
6983effdb90SChristoph Hellwig 	return count;
6993effdb90SChristoph Hellwig }
7003effdb90SChristoph Hellwig 
7012eafd729SChristoph Hellwig static ssize_t emulate_tpws_store(struct config_item *item,
7023effdb90SChristoph Hellwig 		const char *page, size_t count)
7033effdb90SChristoph Hellwig {
7042eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7053effdb90SChristoph Hellwig 	bool flag;
7063effdb90SChristoph Hellwig 	int ret;
7073effdb90SChristoph Hellwig 
7083effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7093effdb90SChristoph Hellwig 	if (ret < 0)
7103effdb90SChristoph Hellwig 		return ret;
7113effdb90SChristoph Hellwig 
7123effdb90SChristoph Hellwig 	/*
7133effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
7143effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
7153effdb90SChristoph Hellwig 	 */
7163effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
7173effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
7183effdb90SChristoph Hellwig 		return -ENOSYS;
7193effdb90SChristoph Hellwig 	}
7203effdb90SChristoph Hellwig 
7213effdb90SChristoph Hellwig 	da->emulate_tpws = flag;
7223effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
7233effdb90SChristoph Hellwig 				da->da_dev, flag);
7243effdb90SChristoph Hellwig 	return count;
7253effdb90SChristoph Hellwig }
7263effdb90SChristoph Hellwig 
7272eafd729SChristoph Hellwig static ssize_t pi_prot_type_store(struct config_item *item,
7283effdb90SChristoph Hellwig 		const char *page, size_t count)
7293effdb90SChristoph Hellwig {
7302eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7313effdb90SChristoph Hellwig 	int old_prot = da->pi_prot_type, ret;
7323effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
7333effdb90SChristoph Hellwig 	u32 flag;
7343effdb90SChristoph Hellwig 
7353effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &flag);
7363effdb90SChristoph Hellwig 	if (ret < 0)
7373effdb90SChristoph Hellwig 		return ret;
7383effdb90SChristoph Hellwig 
7393effdb90SChristoph Hellwig 	if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
7403effdb90SChristoph Hellwig 		pr_err("Illegal value %d for pi_prot_type\n", flag);
7413effdb90SChristoph Hellwig 		return -EINVAL;
7423effdb90SChristoph Hellwig 	}
7433effdb90SChristoph Hellwig 	if (flag == 2) {
7443effdb90SChristoph Hellwig 		pr_err("DIF TYPE2 protection currently not supported\n");
7453effdb90SChristoph Hellwig 		return -ENOSYS;
7463effdb90SChristoph Hellwig 	}
7473effdb90SChristoph Hellwig 	if (da->hw_pi_prot_type) {
7483effdb90SChristoph Hellwig 		pr_warn("DIF protection enabled on underlying hardware,"
7493effdb90SChristoph Hellwig 			" ignoring\n");
7503effdb90SChristoph Hellwig 		return count;
7513effdb90SChristoph Hellwig 	}
7523effdb90SChristoph Hellwig 	if (!dev->transport->init_prot || !dev->transport->free_prot) {
7533effdb90SChristoph Hellwig 		/* 0 is only allowed value for non-supporting backends */
7543effdb90SChristoph Hellwig 		if (flag == 0)
755bc1a7d6aSAndy Grover 			return count;
7563effdb90SChristoph Hellwig 
7573effdb90SChristoph Hellwig 		pr_err("DIF protection not supported by backend: %s\n",
7583effdb90SChristoph Hellwig 		       dev->transport->name);
7593effdb90SChristoph Hellwig 		return -ENOSYS;
7603effdb90SChristoph Hellwig 	}
7613effdb90SChristoph Hellwig 	if (!(dev->dev_flags & DF_CONFIGURED)) {
7623effdb90SChristoph Hellwig 		pr_err("DIF protection requires device to be configured\n");
7633effdb90SChristoph Hellwig 		return -ENODEV;
7643effdb90SChristoph Hellwig 	}
7653effdb90SChristoph Hellwig 	if (dev->export_count) {
7663effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device PROT type while"
7673effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
7683effdb90SChristoph Hellwig 		return -EINVAL;
7693effdb90SChristoph Hellwig 	}
7703effdb90SChristoph Hellwig 
7713effdb90SChristoph Hellwig 	da->pi_prot_type = flag;
7723effdb90SChristoph Hellwig 
7733effdb90SChristoph Hellwig 	if (flag && !old_prot) {
7743effdb90SChristoph Hellwig 		ret = dev->transport->init_prot(dev);
7753effdb90SChristoph Hellwig 		if (ret) {
7763effdb90SChristoph Hellwig 			da->pi_prot_type = old_prot;
7773effdb90SChristoph Hellwig 			return ret;
7783effdb90SChristoph Hellwig 		}
7793effdb90SChristoph Hellwig 
7803effdb90SChristoph Hellwig 	} else if (!flag && old_prot) {
7813effdb90SChristoph Hellwig 		dev->transport->free_prot(dev);
7823effdb90SChristoph Hellwig 	}
7833effdb90SChristoph Hellwig 
7843effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
7853effdb90SChristoph Hellwig 	return count;
7863effdb90SChristoph Hellwig }
7873effdb90SChristoph Hellwig 
7882eafd729SChristoph Hellwig static ssize_t pi_prot_format_store(struct config_item *item,
7893effdb90SChristoph Hellwig 		const char *page, size_t count)
7903effdb90SChristoph Hellwig {
7912eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7923effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
7933effdb90SChristoph Hellwig 	bool flag;
7943effdb90SChristoph Hellwig 	int ret;
7953effdb90SChristoph Hellwig 
7963effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7973effdb90SChristoph Hellwig 	if (ret < 0)
7983effdb90SChristoph Hellwig 		return ret;
7993effdb90SChristoph Hellwig 
8003effdb90SChristoph Hellwig 	if (!flag)
8013effdb90SChristoph Hellwig 		return count;
8023effdb90SChristoph Hellwig 
8033effdb90SChristoph Hellwig 	if (!dev->transport->format_prot) {
8043effdb90SChristoph Hellwig 		pr_err("DIF protection format not supported by backend %s\n",
8053effdb90SChristoph Hellwig 		       dev->transport->name);
8063effdb90SChristoph Hellwig 		return -ENOSYS;
8073effdb90SChristoph Hellwig 	}
8083effdb90SChristoph Hellwig 	if (!(dev->dev_flags & DF_CONFIGURED)) {
8093effdb90SChristoph Hellwig 		pr_err("DIF protection format requires device to be configured\n");
8103effdb90SChristoph Hellwig 		return -ENODEV;
8113effdb90SChristoph Hellwig 	}
8123effdb90SChristoph Hellwig 	if (dev->export_count) {
8133effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to format SE Device PROT type while"
8143effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
8153effdb90SChristoph Hellwig 		return -EINVAL;
8163effdb90SChristoph Hellwig 	}
8173effdb90SChristoph Hellwig 
8183effdb90SChristoph Hellwig 	ret = dev->transport->format_prot(dev);
8193effdb90SChristoph Hellwig 	if (ret)
8203effdb90SChristoph Hellwig 		return ret;
8213effdb90SChristoph Hellwig 
8223effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
8233effdb90SChristoph Hellwig 	return count;
8243effdb90SChristoph Hellwig }
8253effdb90SChristoph Hellwig 
8262eafd729SChristoph Hellwig static ssize_t force_pr_aptpl_store(struct config_item *item,
8273effdb90SChristoph Hellwig 		const char *page, size_t count)
8283effdb90SChristoph Hellwig {
8292eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
8303effdb90SChristoph Hellwig 	bool flag;
8313effdb90SChristoph Hellwig 	int ret;
8323effdb90SChristoph Hellwig 
8333effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
8343effdb90SChristoph Hellwig 	if (ret < 0)
8353effdb90SChristoph Hellwig 		return ret;
8363effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
8373effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to set force_pr_aptpl while"
8383effdb90SChristoph Hellwig 		       " export_count is %d\n",
8393effdb90SChristoph Hellwig 		       da->da_dev, da->da_dev->export_count);
8403effdb90SChristoph Hellwig 		return -EINVAL;
8413effdb90SChristoph Hellwig 	}
8423effdb90SChristoph Hellwig 
8433effdb90SChristoph Hellwig 	da->force_pr_aptpl = flag;
8443effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
8453effdb90SChristoph Hellwig 	return count;
8463effdb90SChristoph Hellwig }
8473effdb90SChristoph Hellwig 
8482eafd729SChristoph Hellwig static ssize_t emulate_rest_reord_store(struct config_item *item,
8493effdb90SChristoph Hellwig 		const char *page, size_t count)
8503effdb90SChristoph Hellwig {
8512eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
8523effdb90SChristoph Hellwig 	bool flag;
8533effdb90SChristoph Hellwig 	int ret;
8543effdb90SChristoph Hellwig 
8553effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
8563effdb90SChristoph Hellwig 	if (ret < 0)
8573effdb90SChristoph Hellwig 		return ret;
8583effdb90SChristoph Hellwig 
8593effdb90SChristoph Hellwig 	if (flag != 0) {
8603effdb90SChristoph Hellwig 		printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
8613effdb90SChristoph Hellwig 			" reordering not implemented\n", da->da_dev);
8623effdb90SChristoph Hellwig 		return -ENOSYS;
8633effdb90SChristoph Hellwig 	}
8643effdb90SChristoph Hellwig 	da->emulate_rest_reord = flag;
8653effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
8663effdb90SChristoph Hellwig 		da->da_dev, flag);
8673effdb90SChristoph Hellwig 	return count;
8683effdb90SChristoph Hellwig }
8693effdb90SChristoph Hellwig 
870e6f41633SJamie Pocas static ssize_t unmap_zeroes_data_store(struct config_item *item,
871e6f41633SJamie Pocas 		const char *page, size_t count)
872e6f41633SJamie Pocas {
873e6f41633SJamie Pocas 	struct se_dev_attrib *da = to_attrib(item);
874e6f41633SJamie Pocas 	bool flag;
875e6f41633SJamie Pocas 	int ret;
876e6f41633SJamie Pocas 
877e6f41633SJamie Pocas 	ret = strtobool(page, &flag);
878e6f41633SJamie Pocas 	if (ret < 0)
879e6f41633SJamie Pocas 		return ret;
880e6f41633SJamie Pocas 
881e6f41633SJamie Pocas 	if (da->da_dev->export_count) {
882e6f41633SJamie Pocas 		pr_err("dev[%p]: Unable to change SE Device"
883e6f41633SJamie Pocas 		       " unmap_zeroes_data while export_count is %d\n",
884e6f41633SJamie Pocas 		       da->da_dev, da->da_dev->export_count);
885e6f41633SJamie Pocas 		return -EINVAL;
886e6f41633SJamie Pocas 	}
887e6f41633SJamie Pocas 	/*
888e6f41633SJamie Pocas 	 * We expect this value to be non-zero when generic Block Layer
889e6f41633SJamie Pocas 	 * Discard supported is detected iblock_configure_device().
890e6f41633SJamie Pocas 	 */
891e6f41633SJamie Pocas 	if (flag && !da->max_unmap_block_desc_count) {
892e6f41633SJamie Pocas 		pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
893e6f41633SJamie Pocas 		       " because max_unmap_block_desc_count is zero\n",
894e6f41633SJamie Pocas 		       da->da_dev);
895e6f41633SJamie Pocas 	       return -ENOSYS;
896e6f41633SJamie Pocas 	}
897e6f41633SJamie Pocas 	da->unmap_zeroes_data = flag;
898e6f41633SJamie Pocas 	pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
899e6f41633SJamie Pocas 		 da->da_dev, flag);
900e6f41633SJamie Pocas 	return 0;
901e6f41633SJamie Pocas }
902e6f41633SJamie Pocas 
9033effdb90SChristoph Hellwig /*
9043effdb90SChristoph Hellwig  * Note, this can only be called on unexported SE Device Object.
9053effdb90SChristoph Hellwig  */
9062eafd729SChristoph Hellwig static ssize_t queue_depth_store(struct config_item *item,
9073effdb90SChristoph Hellwig 		const char *page, size_t count)
9083effdb90SChristoph Hellwig {
9092eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9103effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
9113effdb90SChristoph Hellwig 	u32 val;
9123effdb90SChristoph Hellwig 	int ret;
9133effdb90SChristoph Hellwig 
9143effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
9153effdb90SChristoph Hellwig 	if (ret < 0)
9163effdb90SChristoph Hellwig 		return ret;
9173effdb90SChristoph Hellwig 
9183effdb90SChristoph Hellwig 	if (dev->export_count) {
9193effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TCQ while"
9203effdb90SChristoph Hellwig 			" export_count is %d\n",
9213effdb90SChristoph Hellwig 			dev, dev->export_count);
9223effdb90SChristoph Hellwig 		return -EINVAL;
9233effdb90SChristoph Hellwig 	}
9243effdb90SChristoph Hellwig 	if (!val) {
9253effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
9263effdb90SChristoph Hellwig 		return -EINVAL;
9273effdb90SChristoph Hellwig 	}
9283effdb90SChristoph Hellwig 
9293effdb90SChristoph Hellwig 	if (val > dev->dev_attrib.queue_depth) {
9303effdb90SChristoph Hellwig 		if (val > dev->dev_attrib.hw_queue_depth) {
9313effdb90SChristoph Hellwig 			pr_err("dev[%p]: Passed queue_depth:"
9323effdb90SChristoph Hellwig 				" %u exceeds TCM/SE_Device MAX"
9333effdb90SChristoph Hellwig 				" TCQ: %u\n", dev, val,
9343effdb90SChristoph Hellwig 				dev->dev_attrib.hw_queue_depth);
9353effdb90SChristoph Hellwig 			return -EINVAL;
9363effdb90SChristoph Hellwig 		}
9373effdb90SChristoph Hellwig 	}
9383effdb90SChristoph Hellwig 	da->queue_depth = dev->queue_depth = val;
9393effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
9403effdb90SChristoph Hellwig 	return count;
9413effdb90SChristoph Hellwig }
9423effdb90SChristoph Hellwig 
9432eafd729SChristoph Hellwig static ssize_t optimal_sectors_store(struct config_item *item,
9443effdb90SChristoph Hellwig 		const char *page, size_t count)
9453effdb90SChristoph Hellwig {
9462eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9473effdb90SChristoph Hellwig 	u32 val;
9483effdb90SChristoph Hellwig 	int ret;
9493effdb90SChristoph Hellwig 
9503effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
9513effdb90SChristoph Hellwig 	if (ret < 0)
9523effdb90SChristoph Hellwig 		return ret;
9533effdb90SChristoph Hellwig 
9543effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
9553effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
9563effdb90SChristoph Hellwig 			" optimal_sectors while export_count is %d\n",
9573effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
9583effdb90SChristoph Hellwig 		return -EINVAL;
9593effdb90SChristoph Hellwig 	}
9603effdb90SChristoph Hellwig 	if (val > da->hw_max_sectors) {
9613effdb90SChristoph Hellwig 		pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
9623effdb90SChristoph Hellwig 			" greater than hw_max_sectors: %u\n",
9633effdb90SChristoph Hellwig 			da->da_dev, val, da->hw_max_sectors);
9643effdb90SChristoph Hellwig 		return -EINVAL;
9653effdb90SChristoph Hellwig 	}
9663effdb90SChristoph Hellwig 
9673effdb90SChristoph Hellwig 	da->optimal_sectors = val;
9683effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
9693effdb90SChristoph Hellwig 			da->da_dev, val);
9703effdb90SChristoph Hellwig 	return count;
9713effdb90SChristoph Hellwig }
9723effdb90SChristoph Hellwig 
9732eafd729SChristoph Hellwig static ssize_t block_size_store(struct config_item *item,
9743effdb90SChristoph Hellwig 		const char *page, size_t count)
9753effdb90SChristoph Hellwig {
9762eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9773effdb90SChristoph Hellwig 	u32 val;
9783effdb90SChristoph Hellwig 	int ret;
9793effdb90SChristoph Hellwig 
9803effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
9813effdb90SChristoph Hellwig 	if (ret < 0)
9823effdb90SChristoph Hellwig 		return ret;
9833effdb90SChristoph Hellwig 
9843effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
9853effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device block_size"
9863effdb90SChristoph Hellwig 			" while export_count is %d\n",
9873effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
9883effdb90SChristoph Hellwig 		return -EINVAL;
9893effdb90SChristoph Hellwig 	}
9903effdb90SChristoph Hellwig 
9913effdb90SChristoph Hellwig 	if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
9923effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal value for block_device: %u"
9933effdb90SChristoph Hellwig 			" for SE device, must be 512, 1024, 2048 or 4096\n",
9943effdb90SChristoph Hellwig 			da->da_dev, val);
9953effdb90SChristoph Hellwig 		return -EINVAL;
9963effdb90SChristoph Hellwig 	}
9973effdb90SChristoph Hellwig 
9983effdb90SChristoph Hellwig 	da->block_size = val;
9993effdb90SChristoph Hellwig 	if (da->max_bytes_per_io)
10003effdb90SChristoph Hellwig 		da->hw_max_sectors = da->max_bytes_per_io / val;
10013effdb90SChristoph Hellwig 
10023effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device block_size changed to %u\n",
10033effdb90SChristoph Hellwig 			da->da_dev, val);
10043effdb90SChristoph Hellwig 	return count;
10053effdb90SChristoph Hellwig }
10065873c4d1SChristoph Hellwig 
10072eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_model_alias);
10082eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_dpo);
10092eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_write);
10102eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_read);
10112eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_write_cache);
10122eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
10132eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tas);
10142eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpu);
10152eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpws);
10162eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_caw);
10172eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_3pc);
10182eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_type);
10192eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_pi_prot_type);
10202eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_format);
10212eafd729SChristoph Hellwig CONFIGFS_ATTR(, enforce_pr_isids);
10222eafd729SChristoph Hellwig CONFIGFS_ATTR(, is_nonrot);
10232eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_rest_reord);
10242eafd729SChristoph Hellwig CONFIGFS_ATTR(, force_pr_aptpl);
10252eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_block_size);
10262eafd729SChristoph Hellwig CONFIGFS_ATTR(, block_size);
10272eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_max_sectors);
10282eafd729SChristoph Hellwig CONFIGFS_ATTR(, optimal_sectors);
10292eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_queue_depth);
10302eafd729SChristoph Hellwig CONFIGFS_ATTR(, queue_depth);
10312eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_lba_count);
10322eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_block_desc_count);
10332eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity);
10342eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity_alignment);
1035e6f41633SJamie Pocas CONFIGFS_ATTR(, unmap_zeroes_data);
10362eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_write_same_len);
1037c66ac9dbSNicholas Bellinger 
10385873c4d1SChristoph Hellwig /*
10395873c4d1SChristoph Hellwig  * dev_attrib attributes for devices using the target core SBC/SPC
10405873c4d1SChristoph Hellwig  * interpreter.  Any backend using spc_parse_cdb should be using
10415873c4d1SChristoph Hellwig  * these.
10425873c4d1SChristoph Hellwig  */
10435873c4d1SChristoph Hellwig struct configfs_attribute *sbc_attrib_attrs[] = {
10442eafd729SChristoph Hellwig 	&attr_emulate_model_alias,
10452eafd729SChristoph Hellwig 	&attr_emulate_dpo,
10462eafd729SChristoph Hellwig 	&attr_emulate_fua_write,
10472eafd729SChristoph Hellwig 	&attr_emulate_fua_read,
10482eafd729SChristoph Hellwig 	&attr_emulate_write_cache,
10492eafd729SChristoph Hellwig 	&attr_emulate_ua_intlck_ctrl,
10502eafd729SChristoph Hellwig 	&attr_emulate_tas,
10512eafd729SChristoph Hellwig 	&attr_emulate_tpu,
10522eafd729SChristoph Hellwig 	&attr_emulate_tpws,
10532eafd729SChristoph Hellwig 	&attr_emulate_caw,
10542eafd729SChristoph Hellwig 	&attr_emulate_3pc,
10552eafd729SChristoph Hellwig 	&attr_pi_prot_type,
10562eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
10572eafd729SChristoph Hellwig 	&attr_pi_prot_format,
10582eafd729SChristoph Hellwig 	&attr_enforce_pr_isids,
10592eafd729SChristoph Hellwig 	&attr_is_nonrot,
10602eafd729SChristoph Hellwig 	&attr_emulate_rest_reord,
10612eafd729SChristoph Hellwig 	&attr_force_pr_aptpl,
10622eafd729SChristoph Hellwig 	&attr_hw_block_size,
10632eafd729SChristoph Hellwig 	&attr_block_size,
10642eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
10652eafd729SChristoph Hellwig 	&attr_optimal_sectors,
10662eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
10672eafd729SChristoph Hellwig 	&attr_queue_depth,
10682eafd729SChristoph Hellwig 	&attr_max_unmap_lba_count,
10692eafd729SChristoph Hellwig 	&attr_max_unmap_block_desc_count,
10702eafd729SChristoph Hellwig 	&attr_unmap_granularity,
10712eafd729SChristoph Hellwig 	&attr_unmap_granularity_alignment,
1072e6f41633SJamie Pocas 	&attr_unmap_zeroes_data,
10732eafd729SChristoph Hellwig 	&attr_max_write_same_len,
10745873c4d1SChristoph Hellwig 	NULL,
10755873c4d1SChristoph Hellwig };
10765873c4d1SChristoph Hellwig EXPORT_SYMBOL(sbc_attrib_attrs);
10775873c4d1SChristoph Hellwig 
10785873c4d1SChristoph Hellwig /*
10795873c4d1SChristoph Hellwig  * Minimal dev_attrib attributes for devices passing through CDBs.
10805873c4d1SChristoph Hellwig  * In this case we only provide a few read-only attributes for
10815873c4d1SChristoph Hellwig  * backwards compatibility.
10825873c4d1SChristoph Hellwig  */
10835873c4d1SChristoph Hellwig struct configfs_attribute *passthrough_attrib_attrs[] = {
10842eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
10852eafd729SChristoph Hellwig 	&attr_hw_block_size,
10862eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
10872eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
10885873c4d1SChristoph Hellwig 	NULL,
10895873c4d1SChristoph Hellwig };
10905873c4d1SChristoph Hellwig EXPORT_SYMBOL(passthrough_attrib_attrs);
10915873c4d1SChristoph Hellwig 
10922eafd729SChristoph Hellwig TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
1093c66ac9dbSNicholas Bellinger 
1094f79a897eSNicholas Bellinger /* End functions for struct config_item_type tb_dev_attrib_cit */
1095c66ac9dbSNicholas Bellinger 
1096f8d389c6SNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_wwn_cit */
1097c66ac9dbSNicholas Bellinger 
10982eafd729SChristoph Hellwig static struct t10_wwn *to_t10_wwn(struct config_item *item)
10992eafd729SChristoph Hellwig {
11002eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
11012eafd729SChristoph Hellwig }
1102c66ac9dbSNicholas Bellinger 
1103c66ac9dbSNicholas Bellinger /*
1104c66ac9dbSNicholas Bellinger  * VPD page 0x80 Unit serial
1105c66ac9dbSNicholas Bellinger  */
11062eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1107c66ac9dbSNicholas Bellinger 		char *page)
1108c66ac9dbSNicholas Bellinger {
1109c66ac9dbSNicholas Bellinger 	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
11102eafd729SChristoph Hellwig 		&to_t10_wwn(item)->unit_serial[0]);
1111c66ac9dbSNicholas Bellinger }
1112c66ac9dbSNicholas Bellinger 
11132eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
11142eafd729SChristoph Hellwig 		const char *page, size_t count)
1115c66ac9dbSNicholas Bellinger {
11162eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
11170fd97ccfSChristoph Hellwig 	struct se_device *dev = t10_wwn->t10_dev;
1118c66ac9dbSNicholas Bellinger 	unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1119c66ac9dbSNicholas Bellinger 
1120c66ac9dbSNicholas Bellinger 	/*
1121c66ac9dbSNicholas Bellinger 	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1122c66ac9dbSNicholas Bellinger 	 * from the struct scsi_device level firmware, do not allow
1123c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial to be emulated.
1124c66ac9dbSNicholas Bellinger 	 *
1125c66ac9dbSNicholas Bellinger 	 * Note this struct scsi_device could also be emulating VPD
1126c66ac9dbSNicholas Bellinger 	 * information from its drivers/scsi LLD.  But for now we assume
1127c66ac9dbSNicholas Bellinger 	 * it is doing 'the right thing' wrt a world wide unique
1128c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial Number that OS dependent multipath can depend on.
1129c66ac9dbSNicholas Bellinger 	 */
11300fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
11316708bb27SAndy Grover 		pr_err("Underlying SCSI device firmware provided VPD"
1132c66ac9dbSNicholas Bellinger 			" Unit Serial, ignoring request\n");
1133c66ac9dbSNicholas Bellinger 		return -EOPNOTSUPP;
1134c66ac9dbSNicholas Bellinger 	}
1135c66ac9dbSNicholas Bellinger 
113660d645a4SDan Carpenter 	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
11376708bb27SAndy Grover 		pr_err("Emulated VPD Unit Serial exceeds"
1138c66ac9dbSNicholas Bellinger 		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1139c66ac9dbSNicholas Bellinger 		return -EOVERFLOW;
1140c66ac9dbSNicholas Bellinger 	}
1141c66ac9dbSNicholas Bellinger 	/*
1142c66ac9dbSNicholas Bellinger 	 * Check to see if any active $FABRIC_MOD exports exist.  If they
1143c66ac9dbSNicholas Bellinger 	 * do exist, fail here as changing this information on the fly
1144c66ac9dbSNicholas Bellinger 	 * (underneath the initiator side OS dependent multipath code)
1145c66ac9dbSNicholas Bellinger 	 * could cause negative effects.
1146c66ac9dbSNicholas Bellinger 	 */
11470fd97ccfSChristoph Hellwig 	if (dev->export_count) {
11486708bb27SAndy Grover 		pr_err("Unable to set VPD Unit Serial while"
1149c66ac9dbSNicholas Bellinger 			" active %d $FABRIC_MOD exports exist\n",
11500fd97ccfSChristoph Hellwig 			dev->export_count);
1151c66ac9dbSNicholas Bellinger 		return -EINVAL;
1152c66ac9dbSNicholas Bellinger 	}
11530fd97ccfSChristoph Hellwig 
1154c66ac9dbSNicholas Bellinger 	/*
1155c66ac9dbSNicholas Bellinger 	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1156c66ac9dbSNicholas Bellinger 	 *
1157c66ac9dbSNicholas Bellinger 	 * Also, strip any newline added from the userspace
1158c66ac9dbSNicholas Bellinger 	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1159c66ac9dbSNicholas Bellinger 	 */
1160c66ac9dbSNicholas Bellinger 	memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1161c66ac9dbSNicholas Bellinger 	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
11620fd97ccfSChristoph Hellwig 	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1163c66ac9dbSNicholas Bellinger 			"%s", strstrip(buf));
11640fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1165c66ac9dbSNicholas Bellinger 
11666708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
11670fd97ccfSChristoph Hellwig 			" %s\n", dev->t10_wwn.unit_serial);
1168c66ac9dbSNicholas Bellinger 
1169c66ac9dbSNicholas Bellinger 	return count;
1170c66ac9dbSNicholas Bellinger }
1171c66ac9dbSNicholas Bellinger 
1172c66ac9dbSNicholas Bellinger /*
1173c66ac9dbSNicholas Bellinger  * VPD page 0x83 Protocol Identifier
1174c66ac9dbSNicholas Bellinger  */
11752eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1176c66ac9dbSNicholas Bellinger 		char *page)
1177c66ac9dbSNicholas Bellinger {
11782eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1179c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;
1180c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];
1181c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1182c66ac9dbSNicholas Bellinger 
1183c66ac9dbSNicholas Bellinger 	memset(buf, 0, VPD_TMP_BUF_SIZE);
1184c66ac9dbSNicholas Bellinger 
1185c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);
1186c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
11876708bb27SAndy Grover 		if (!vpd->protocol_identifier_set)
1188c66ac9dbSNicholas Bellinger 			continue;
1189c66ac9dbSNicholas Bellinger 
1190c66ac9dbSNicholas Bellinger 		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1191c66ac9dbSNicholas Bellinger 
11926708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1193c66ac9dbSNicholas Bellinger 			break;
1194c66ac9dbSNicholas Bellinger 
1195c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1196c66ac9dbSNicholas Bellinger 	}
1197c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);
1198c66ac9dbSNicholas Bellinger 
1199c66ac9dbSNicholas Bellinger 	return len;
1200c66ac9dbSNicholas Bellinger }
1201c66ac9dbSNicholas Bellinger 
1202c66ac9dbSNicholas Bellinger /*
1203c66ac9dbSNicholas Bellinger  * Generic wrapper for dumping VPD identifiers by association.
1204c66ac9dbSNicholas Bellinger  */
1205c66ac9dbSNicholas Bellinger #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
12062eafd729SChristoph Hellwig static ssize_t target_wwn_##_name##_show(struct config_item *item,	\
1207c66ac9dbSNicholas Bellinger 		char *page)						\
1208c66ac9dbSNicholas Bellinger {									\
12092eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);			\
1210c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;						\
1211c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];				\
1212c66ac9dbSNicholas Bellinger 	ssize_t len = 0;						\
1213c66ac9dbSNicholas Bellinger 									\
1214c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);				\
1215c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
1216c66ac9dbSNicholas Bellinger 		if (vpd->association != _assoc)				\
1217c66ac9dbSNicholas Bellinger 			continue;					\
1218c66ac9dbSNicholas Bellinger 									\
1219c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1220c66ac9dbSNicholas Bellinger 		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
12216708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1222c66ac9dbSNicholas Bellinger 			break;						\
1223c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1224c66ac9dbSNicholas Bellinger 									\
1225c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1226c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
12276708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1228c66ac9dbSNicholas Bellinger 			break;						\
1229c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1230c66ac9dbSNicholas Bellinger 									\
1231c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1232c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
12336708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1234c66ac9dbSNicholas Bellinger 			break;						\
1235c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1236c66ac9dbSNicholas Bellinger 	}								\
1237c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);				\
1238c66ac9dbSNicholas Bellinger 									\
1239c66ac9dbSNicholas Bellinger 	return len;							\
1240c66ac9dbSNicholas Bellinger }
1241c66ac9dbSNicholas Bellinger 
12422eafd729SChristoph Hellwig /* VPD page 0x83 Association: Logical Unit */
1243c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
12442eafd729SChristoph Hellwig /* VPD page 0x83 Association: Target Port */
1245c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
12462eafd729SChristoph Hellwig /* VPD page 0x83 Association: SCSI Target Device */
1247c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1248c66ac9dbSNicholas Bellinger 
12492eafd729SChristoph Hellwig CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
12502eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
12512eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
12522eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
12532eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1254c66ac9dbSNicholas Bellinger 
1255c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
12562eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_unit_serial,
12572eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_protocol_identifier,
12582eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_logical_unit,
12592eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_target_port,
12602eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_scsi_target_device,
1261c66ac9dbSNicholas Bellinger 	NULL,
1262c66ac9dbSNicholas Bellinger };
1263c66ac9dbSNicholas Bellinger 
12642eafd729SChristoph Hellwig TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1265c66ac9dbSNicholas Bellinger 
1266f8d389c6SNicholas Bellinger /*  End functions for struct config_item_type tb_dev_wwn_cit */
1267c66ac9dbSNicholas Bellinger 
126891e2e39bSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_pr_cit */
1269c66ac9dbSNicholas Bellinger 
12702eafd729SChristoph Hellwig static struct se_device *pr_to_dev(struct config_item *item)
12712eafd729SChristoph Hellwig {
12722eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device,
12732eafd729SChristoph Hellwig 			dev_pr_group);
12742eafd729SChristoph Hellwig }
1275c66ac9dbSNicholas Bellinger 
1276d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1277d977f437SChristoph Hellwig 		char *page)
1278c66ac9dbSNicholas Bellinger {
1279c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1280c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1281c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1282c66ac9dbSNicholas Bellinger 
1283c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1284c66ac9dbSNicholas Bellinger 
1285c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1286d977f437SChristoph Hellwig 	if (!pr_reg)
1287d977f437SChristoph Hellwig 		return sprintf(page, "No SPC-3 Reservation holder\n");
1288d977f437SChristoph Hellwig 
1289c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1290d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1291c66ac9dbSNicholas Bellinger 
1292d977f437SChristoph Hellwig 	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
1293e3d6f909SAndy Grover 		se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1294d2843c17SAndy Grover 		se_nacl->initiatorname, i_buf);
1295c66ac9dbSNicholas Bellinger }
1296c66ac9dbSNicholas Bellinger 
1297d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1298d977f437SChristoph Hellwig 		char *page)
1299c66ac9dbSNicholas Bellinger {
1300c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1301d977f437SChristoph Hellwig 	ssize_t len;
1302c66ac9dbSNicholas Bellinger 
1303c66ac9dbSNicholas Bellinger 	se_nacl = dev->dev_reserved_node_acl;
1304d977f437SChristoph Hellwig 	if (se_nacl) {
1305d977f437SChristoph Hellwig 		len = sprintf(page,
1306d977f437SChristoph Hellwig 			      "SPC-2 Reservation: %s Initiator: %s\n",
1307e3d6f909SAndy Grover 			      se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1308c66ac9dbSNicholas Bellinger 			      se_nacl->initiatorname);
1309d977f437SChristoph Hellwig 	} else {
1310d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-2 Reservation holder\n");
1311d977f437SChristoph Hellwig 	}
1312d977f437SChristoph Hellwig 	return len;
1313c66ac9dbSNicholas Bellinger }
1314c66ac9dbSNicholas Bellinger 
13152eafd729SChristoph Hellwig static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1316c66ac9dbSNicholas Bellinger {
13172eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1318d977f437SChristoph Hellwig 	int ret;
1319c66ac9dbSNicholas Bellinger 
1320a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1321d977f437SChristoph Hellwig 		return sprintf(page, "Passthrough\n");
1322c66ac9dbSNicholas Bellinger 
1323d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
1324d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1325d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc2_res(dev, page);
1326d977f437SChristoph Hellwig 	else
1327d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc3_res(dev, page);
1328d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1329d977f437SChristoph Hellwig 	return ret;
1330c66ac9dbSNicholas Bellinger }
1331c66ac9dbSNicholas Bellinger 
13322eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
13332eafd729SChristoph Hellwig 		char *page)
1334c66ac9dbSNicholas Bellinger {
13352eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1336c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1337c66ac9dbSNicholas Bellinger 
1338c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1339d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder) {
1340c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1341d977f437SChristoph Hellwig 	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1342c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: All Target"
1343c66ac9dbSNicholas Bellinger 			" Ports registration\n");
1344d977f437SChristoph Hellwig 	} else {
1345c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: Single"
1346c66ac9dbSNicholas Bellinger 			" Target Port registration\n");
1347d977f437SChristoph Hellwig 	}
1348c66ac9dbSNicholas Bellinger 
1349d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1350c66ac9dbSNicholas Bellinger 	return len;
1351c66ac9dbSNicholas Bellinger }
1352c66ac9dbSNicholas Bellinger 
13532eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
13542eafd729SChristoph Hellwig 		char *page)
1355c66ac9dbSNicholas Bellinger {
13562eafd729SChristoph Hellwig 	return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1357c66ac9dbSNicholas Bellinger }
1358c66ac9dbSNicholas Bellinger 
1359c66ac9dbSNicholas Bellinger 
13602eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
13612eafd729SChristoph Hellwig 		char *page)
1362c66ac9dbSNicholas Bellinger {
13632eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1364c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1365c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
1366c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
13679ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1368c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1369c66ac9dbSNicholas Bellinger 
1370c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1371c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
13726708bb27SAndy Grover 	if (!pr_reg) {
1373c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1374d977f437SChristoph Hellwig 		goto out_unlock;
1375c66ac9dbSNicholas Bellinger 	}
1376d977f437SChristoph Hellwig 
1377c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1378c66ac9dbSNicholas Bellinger 	se_tpg = se_nacl->se_tpg;
1379e3d6f909SAndy Grover 	tfo = se_tpg->se_tpg_tfo;
1380c66ac9dbSNicholas Bellinger 
1381c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: %s"
1382c66ac9dbSNicholas Bellinger 		" Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1383c66ac9dbSNicholas Bellinger 		tfo->tpg_get_wwn(se_tpg));
1384c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
138535d1efe8SMasanari Iida 		" Identifier Tag: %hu %s Portal Group Tag: %hu"
1386f2d30680SHannes Reinecke 		" %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
1387c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
138879dc9c9eSNicholas Bellinger 		tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
1389c66ac9dbSNicholas Bellinger 
1390d977f437SChristoph Hellwig out_unlock:
1391d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1392c66ac9dbSNicholas Bellinger 	return len;
1393c66ac9dbSNicholas Bellinger }
1394c66ac9dbSNicholas Bellinger 
1395c66ac9dbSNicholas Bellinger 
13962eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
13972eafd729SChristoph Hellwig 		char *page)
1398c66ac9dbSNicholas Bellinger {
13992eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
14009ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1401c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1402c66ac9dbSNicholas Bellinger 	unsigned char buf[384];
1403c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1404c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1405d2843c17SAndy Grover 	int reg_count = 0;
1406c66ac9dbSNicholas Bellinger 
1407c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1408c66ac9dbSNicholas Bellinger 
14090fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
14100fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1411c66ac9dbSNicholas Bellinger 			pr_reg_list) {
1412c66ac9dbSNicholas Bellinger 
1413c66ac9dbSNicholas Bellinger 		memset(buf, 0, 384);
1414c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1415c66ac9dbSNicholas Bellinger 		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1416d2843c17SAndy Grover 		core_pr_dump_initiator_port(pr_reg, i_buf,
1417c66ac9dbSNicholas Bellinger 					PR_REG_ISID_ID_LEN);
1418c66ac9dbSNicholas Bellinger 		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1419c66ac9dbSNicholas Bellinger 			tfo->get_fabric_name(),
1420d2843c17SAndy Grover 			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1421c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_generation);
1422c66ac9dbSNicholas Bellinger 
14236708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1424c66ac9dbSNicholas Bellinger 			break;
1425c66ac9dbSNicholas Bellinger 
1426c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1427c66ac9dbSNicholas Bellinger 		reg_count++;
1428c66ac9dbSNicholas Bellinger 	}
14290fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
1430c66ac9dbSNicholas Bellinger 
14316708bb27SAndy Grover 	if (!reg_count)
1432c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "None\n");
1433c66ac9dbSNicholas Bellinger 
1434c66ac9dbSNicholas Bellinger 	return len;
1435c66ac9dbSNicholas Bellinger }
1436c66ac9dbSNicholas Bellinger 
14372eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1438c66ac9dbSNicholas Bellinger {
14392eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1440c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1441c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1442c66ac9dbSNicholas Bellinger 
1443c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1444c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1445d977f437SChristoph Hellwig 	if (pr_reg) {
1446c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1447c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1448d977f437SChristoph Hellwig 	} else {
1449d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1450d977f437SChristoph Hellwig 	}
1451c66ac9dbSNicholas Bellinger 
1452d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1453c66ac9dbSNicholas Bellinger 	return len;
1454c66ac9dbSNicholas Bellinger }
1455c66ac9dbSNicholas Bellinger 
14562eafd729SChristoph Hellwig static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
1457c66ac9dbSNicholas Bellinger {
14582eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
14592eafd729SChristoph Hellwig 
1460a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1461d977f437SChristoph Hellwig 		return sprintf(page, "SPC_PASSTHROUGH\n");
1462d977f437SChristoph Hellwig 	else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1463d977f437SChristoph Hellwig 		return sprintf(page, "SPC2_RESERVATIONS\n");
1464d977f437SChristoph Hellwig 	else
1465d977f437SChristoph Hellwig 		return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1466c66ac9dbSNicholas Bellinger }
1467c66ac9dbSNicholas Bellinger 
14682eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
14692eafd729SChristoph Hellwig 		char *page)
1470c66ac9dbSNicholas Bellinger {
14712eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
14722eafd729SChristoph Hellwig 
1473a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1474c66ac9dbSNicholas Bellinger 		return 0;
1475c66ac9dbSNicholas Bellinger 
1476c66ac9dbSNicholas Bellinger 	return sprintf(page, "APTPL Bit Status: %s\n",
14770fd97ccfSChristoph Hellwig 		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1478c66ac9dbSNicholas Bellinger }
1479c66ac9dbSNicholas Bellinger 
14802eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
14812eafd729SChristoph Hellwig 		char *page)
1482c66ac9dbSNicholas Bellinger {
14832eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
14842eafd729SChristoph Hellwig 
1485a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1486c66ac9dbSNicholas Bellinger 		return 0;
1487c66ac9dbSNicholas Bellinger 
1488c66ac9dbSNicholas Bellinger 	return sprintf(page, "Ready to process PR APTPL metadata..\n");
1489c66ac9dbSNicholas Bellinger }
1490c66ac9dbSNicholas Bellinger 
1491c66ac9dbSNicholas Bellinger enum {
1492c66ac9dbSNicholas Bellinger 	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1493c66ac9dbSNicholas Bellinger 	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1494c66ac9dbSNicholas Bellinger 	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1495c66ac9dbSNicholas Bellinger 	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1496c66ac9dbSNicholas Bellinger };
1497c66ac9dbSNicholas Bellinger 
1498c66ac9dbSNicholas Bellinger static match_table_t tokens = {
1499c66ac9dbSNicholas Bellinger 	{Opt_initiator_fabric, "initiator_fabric=%s"},
1500c66ac9dbSNicholas Bellinger 	{Opt_initiator_node, "initiator_node=%s"},
1501c66ac9dbSNicholas Bellinger 	{Opt_initiator_sid, "initiator_sid=%s"},
1502c66ac9dbSNicholas Bellinger 	{Opt_sa_res_key, "sa_res_key=%s"},
1503c66ac9dbSNicholas Bellinger 	{Opt_res_holder, "res_holder=%d"},
1504c66ac9dbSNicholas Bellinger 	{Opt_res_type, "res_type=%d"},
1505c66ac9dbSNicholas Bellinger 	{Opt_res_scope, "res_scope=%d"},
1506c66ac9dbSNicholas Bellinger 	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1507f2d30680SHannes Reinecke 	{Opt_mapped_lun, "mapped_lun=%lld"},
1508c66ac9dbSNicholas Bellinger 	{Opt_target_fabric, "target_fabric=%s"},
1509c66ac9dbSNicholas Bellinger 	{Opt_target_node, "target_node=%s"},
1510c66ac9dbSNicholas Bellinger 	{Opt_tpgt, "tpgt=%d"},
1511c66ac9dbSNicholas Bellinger 	{Opt_port_rtpi, "port_rtpi=%d"},
1512f2d30680SHannes Reinecke 	{Opt_target_lun, "target_lun=%lld"},
1513c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
1514c66ac9dbSNicholas Bellinger };
1515c66ac9dbSNicholas Bellinger 
15162eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
15172eafd729SChristoph Hellwig 		const char *page, size_t count)
1518c66ac9dbSNicholas Bellinger {
15192eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
15206d180253SJesper Juhl 	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
15216d180253SJesper Juhl 	unsigned char *t_fabric = NULL, *t_port = NULL;
15228d213559SJoern Engel 	char *orig, *ptr, *opts;
1523c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
1524c66ac9dbSNicholas Bellinger 	unsigned long long tmp_ll;
1525c66ac9dbSNicholas Bellinger 	u64 sa_res_key = 0;
1526f2d30680SHannes Reinecke 	u64 mapped_lun = 0, target_lun = 0;
1527c66ac9dbSNicholas Bellinger 	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
152845fb94c2SBart Van Assche 	u16 tpgt = 0;
152945fb94c2SBart Van Assche 	u8 type = 0;
1530c66ac9dbSNicholas Bellinger 
1531a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
15329105bfc0SAndy Grover 		return count;
1533d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
15349105bfc0SAndy Grover 		return count;
1535c66ac9dbSNicholas Bellinger 
15360fd97ccfSChristoph Hellwig 	if (dev->export_count) {
15376708bb27SAndy Grover 		pr_debug("Unable to process APTPL metadata while"
1538c66ac9dbSNicholas Bellinger 			" active fabric exports exist\n");
1539c66ac9dbSNicholas Bellinger 		return -EINVAL;
1540c66ac9dbSNicholas Bellinger 	}
1541c66ac9dbSNicholas Bellinger 
1542c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
1543c66ac9dbSNicholas Bellinger 	if (!opts)
1544c66ac9dbSNicholas Bellinger 		return -ENOMEM;
1545c66ac9dbSNicholas Bellinger 
1546c66ac9dbSNicholas Bellinger 	orig = opts;
154790c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
1548c66ac9dbSNicholas Bellinger 		if (!*ptr)
1549c66ac9dbSNicholas Bellinger 			continue;
1550c66ac9dbSNicholas Bellinger 
1551c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
1552c66ac9dbSNicholas Bellinger 		switch (token) {
1553c66ac9dbSNicholas Bellinger 		case Opt_initiator_fabric:
15548d213559SJoern Engel 			i_fabric = match_strdup(args);
15556d180253SJesper Juhl 			if (!i_fabric) {
15566d180253SJesper Juhl 				ret = -ENOMEM;
15576d180253SJesper Juhl 				goto out;
15586d180253SJesper Juhl 			}
1559c66ac9dbSNicholas Bellinger 			break;
1560c66ac9dbSNicholas Bellinger 		case Opt_initiator_node:
15618d213559SJoern Engel 			i_port = match_strdup(args);
15626d180253SJesper Juhl 			if (!i_port) {
15636d180253SJesper Juhl 				ret = -ENOMEM;
15646d180253SJesper Juhl 				goto out;
15656d180253SJesper Juhl 			}
156660d645a4SDan Carpenter 			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
15676708bb27SAndy Grover 				pr_err("APTPL metadata initiator_node="
1568c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1569c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_IPORT_LEN);
1570c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1571c66ac9dbSNicholas Bellinger 				break;
1572c66ac9dbSNicholas Bellinger 			}
1573c66ac9dbSNicholas Bellinger 			break;
1574c66ac9dbSNicholas Bellinger 		case Opt_initiator_sid:
15758d213559SJoern Engel 			isid = match_strdup(args);
15766d180253SJesper Juhl 			if (!isid) {
15776d180253SJesper Juhl 				ret = -ENOMEM;
15786d180253SJesper Juhl 				goto out;
15796d180253SJesper Juhl 			}
158060d645a4SDan Carpenter 			if (strlen(isid) >= PR_REG_ISID_LEN) {
15816708bb27SAndy Grover 				pr_err("APTPL metadata initiator_isid"
1582c66ac9dbSNicholas Bellinger 					"= exceeds PR_REG_ISID_LEN: %d\n",
1583c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
1584c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1585c66ac9dbSNicholas Bellinger 				break;
1586c66ac9dbSNicholas Bellinger 			}
1587c66ac9dbSNicholas Bellinger 			break;
1588c66ac9dbSNicholas Bellinger 		case Opt_sa_res_key:
15898d213559SJoern Engel 			ret = kstrtoull(args->from, 0, &tmp_ll);
1590c66ac9dbSNicholas Bellinger 			if (ret < 0) {
15918d213559SJoern Engel 				pr_err("kstrtoull() failed for sa_res_key=\n");
1592c66ac9dbSNicholas Bellinger 				goto out;
1593c66ac9dbSNicholas Bellinger 			}
1594c66ac9dbSNicholas Bellinger 			sa_res_key = (u64)tmp_ll;
1595c66ac9dbSNicholas Bellinger 			break;
1596c66ac9dbSNicholas Bellinger 		/*
1597c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Reservation
1598c66ac9dbSNicholas Bellinger 		 */
1599c66ac9dbSNicholas Bellinger 		case Opt_res_holder:
1600c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1601c2091026SDavid Disseldorp 			if (ret)
1602c2091026SDavid Disseldorp 				goto out;
1603c66ac9dbSNicholas Bellinger 			res_holder = arg;
1604c66ac9dbSNicholas Bellinger 			break;
1605c66ac9dbSNicholas Bellinger 		case Opt_res_type:
1606c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1607c2091026SDavid Disseldorp 			if (ret)
1608c2091026SDavid Disseldorp 				goto out;
1609c66ac9dbSNicholas Bellinger 			type = (u8)arg;
1610c66ac9dbSNicholas Bellinger 			break;
1611c66ac9dbSNicholas Bellinger 		case Opt_res_scope:
1612c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1613c2091026SDavid Disseldorp 			if (ret)
1614c2091026SDavid Disseldorp 				goto out;
1615c66ac9dbSNicholas Bellinger 			break;
1616c66ac9dbSNicholas Bellinger 		case Opt_res_all_tg_pt:
1617c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1618c2091026SDavid Disseldorp 			if (ret)
1619c2091026SDavid Disseldorp 				goto out;
1620c66ac9dbSNicholas Bellinger 			all_tg_pt = (int)arg;
1621c66ac9dbSNicholas Bellinger 			break;
1622c66ac9dbSNicholas Bellinger 		case Opt_mapped_lun:
1623c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1624c2091026SDavid Disseldorp 			if (ret)
1625c2091026SDavid Disseldorp 				goto out;
1626f2d30680SHannes Reinecke 			mapped_lun = (u64)arg;
1627c66ac9dbSNicholas Bellinger 			break;
1628c66ac9dbSNicholas Bellinger 		/*
1629c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Target Port
1630c66ac9dbSNicholas Bellinger 		 */
1631c66ac9dbSNicholas Bellinger 		case Opt_target_fabric:
16328d213559SJoern Engel 			t_fabric = match_strdup(args);
16336d180253SJesper Juhl 			if (!t_fabric) {
16346d180253SJesper Juhl 				ret = -ENOMEM;
16356d180253SJesper Juhl 				goto out;
16366d180253SJesper Juhl 			}
1637c66ac9dbSNicholas Bellinger 			break;
1638c66ac9dbSNicholas Bellinger 		case Opt_target_node:
16398d213559SJoern Engel 			t_port = match_strdup(args);
16406d180253SJesper Juhl 			if (!t_port) {
16416d180253SJesper Juhl 				ret = -ENOMEM;
16426d180253SJesper Juhl 				goto out;
16436d180253SJesper Juhl 			}
164460d645a4SDan Carpenter 			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
16456708bb27SAndy Grover 				pr_err("APTPL metadata target_node="
1646c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1647c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_TPORT_LEN);
1648c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1649c66ac9dbSNicholas Bellinger 				break;
1650c66ac9dbSNicholas Bellinger 			}
1651c66ac9dbSNicholas Bellinger 			break;
1652c66ac9dbSNicholas Bellinger 		case Opt_tpgt:
1653c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1654c2091026SDavid Disseldorp 			if (ret)
1655c2091026SDavid Disseldorp 				goto out;
1656c66ac9dbSNicholas Bellinger 			tpgt = (u16)arg;
1657c66ac9dbSNicholas Bellinger 			break;
1658c66ac9dbSNicholas Bellinger 		case Opt_port_rtpi:
1659c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1660c2091026SDavid Disseldorp 			if (ret)
1661c2091026SDavid Disseldorp 				goto out;
1662c66ac9dbSNicholas Bellinger 			break;
1663c66ac9dbSNicholas Bellinger 		case Opt_target_lun:
1664c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1665c2091026SDavid Disseldorp 			if (ret)
1666c2091026SDavid Disseldorp 				goto out;
1667f2d30680SHannes Reinecke 			target_lun = (u64)arg;
1668c66ac9dbSNicholas Bellinger 			break;
1669c66ac9dbSNicholas Bellinger 		default:
1670c66ac9dbSNicholas Bellinger 			break;
1671c66ac9dbSNicholas Bellinger 		}
1672c66ac9dbSNicholas Bellinger 	}
1673c66ac9dbSNicholas Bellinger 
16746708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
16756708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
1676c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1677c66ac9dbSNicholas Bellinger 		goto out;
1678c66ac9dbSNicholas Bellinger 	}
1679c66ac9dbSNicholas Bellinger 
1680c66ac9dbSNicholas Bellinger 	if (res_holder && !(type)) {
16816708bb27SAndy Grover 		pr_err("Illegal PR type: 0x%02x for reservation"
1682c66ac9dbSNicholas Bellinger 				" holder\n", type);
1683c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1684c66ac9dbSNicholas Bellinger 		goto out;
1685c66ac9dbSNicholas Bellinger 	}
1686c66ac9dbSNicholas Bellinger 
16870fd97ccfSChristoph Hellwig 	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1688c66ac9dbSNicholas Bellinger 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1689c66ac9dbSNicholas Bellinger 			res_holder, all_tg_pt, type);
1690c66ac9dbSNicholas Bellinger out:
16916d180253SJesper Juhl 	kfree(i_fabric);
16926d180253SJesper Juhl 	kfree(i_port);
16936d180253SJesper Juhl 	kfree(isid);
16946d180253SJesper Juhl 	kfree(t_fabric);
16956d180253SJesper Juhl 	kfree(t_port);
1696c66ac9dbSNicholas Bellinger 	kfree(orig);
1697c66ac9dbSNicholas Bellinger 	return (ret == 0) ? count : ret;
1698c66ac9dbSNicholas Bellinger }
1699c66ac9dbSNicholas Bellinger 
1700c66ac9dbSNicholas Bellinger 
17012eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_holder);
17022eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
17032eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
17042eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
17052eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
17062eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
17072eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_type);
17082eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
17092eafd729SChristoph Hellwig CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
1710c66ac9dbSNicholas Bellinger 
1711c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_pr_attrs[] = {
17122eafd729SChristoph Hellwig 	&target_pr_attr_res_holder,
17132eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_all_tgt_pts,
17142eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_generation,
17152eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_holder_tg_port,
17162eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_registered_i_pts,
17172eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_type,
17182eafd729SChristoph Hellwig 	&target_pr_attr_res_type,
17192eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_active,
17202eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_metadata,
1721c66ac9dbSNicholas Bellinger 	NULL,
1722c66ac9dbSNicholas Bellinger };
1723c66ac9dbSNicholas Bellinger 
17242eafd729SChristoph Hellwig TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
1725c66ac9dbSNicholas Bellinger 
172691e2e39bSNicholas Bellinger /*  End functions for struct config_item_type tb_dev_pr_cit */
1727c66ac9dbSNicholas Bellinger 
172873112edcSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_cit */
1729c66ac9dbSNicholas Bellinger 
17302eafd729SChristoph Hellwig static inline struct se_device *to_device(struct config_item *item)
1731c66ac9dbSNicholas Bellinger {
17322eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device, dev_group);
17332eafd729SChristoph Hellwig }
17342eafd729SChristoph Hellwig 
17352eafd729SChristoph Hellwig static ssize_t target_dev_info_show(struct config_item *item, char *page)
17362eafd729SChristoph Hellwig {
17372eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1738c66ac9dbSNicholas Bellinger 	int bl = 0;
1739c66ac9dbSNicholas Bellinger 	ssize_t read_bytes = 0;
1740c66ac9dbSNicholas Bellinger 
17410fd97ccfSChristoph Hellwig 	transport_dump_dev_state(dev, page, &bl);
1742c66ac9dbSNicholas Bellinger 	read_bytes += bl;
17430a06d430SChristoph Hellwig 	read_bytes += dev->transport->show_configfs_dev_params(dev,
17440a06d430SChristoph Hellwig 			page+read_bytes);
1745c66ac9dbSNicholas Bellinger 	return read_bytes;
1746c66ac9dbSNicholas Bellinger }
1747c66ac9dbSNicholas Bellinger 
17482eafd729SChristoph Hellwig static ssize_t target_dev_control_store(struct config_item *item,
17492eafd729SChristoph Hellwig 		const char *page, size_t count)
1750c66ac9dbSNicholas Bellinger {
17512eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1752c66ac9dbSNicholas Bellinger 
17530a06d430SChristoph Hellwig 	return dev->transport->set_configfs_dev_params(dev, page, count);
1754c66ac9dbSNicholas Bellinger }
1755c66ac9dbSNicholas Bellinger 
17562eafd729SChristoph Hellwig static ssize_t target_dev_alias_show(struct config_item *item, char *page)
1757c66ac9dbSNicholas Bellinger {
17582eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1759c66ac9dbSNicholas Bellinger 
17600fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_ALIAS))
1761c66ac9dbSNicholas Bellinger 		return 0;
1762c66ac9dbSNicholas Bellinger 
17630fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1764c66ac9dbSNicholas Bellinger }
1765c66ac9dbSNicholas Bellinger 
17662eafd729SChristoph Hellwig static ssize_t target_dev_alias_store(struct config_item *item,
17672eafd729SChristoph Hellwig 		const char *page, size_t count)
1768c66ac9dbSNicholas Bellinger {
17692eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
17700fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1771c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1772c66ac9dbSNicholas Bellinger 
1773c66ac9dbSNicholas Bellinger 	if (count > (SE_DEV_ALIAS_LEN-1)) {
17746708bb27SAndy Grover 		pr_err("alias count: %d exceeds"
1775c66ac9dbSNicholas Bellinger 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1776c66ac9dbSNicholas Bellinger 			SE_DEV_ALIAS_LEN-1);
1777c66ac9dbSNicholas Bellinger 		return -EINVAL;
1778c66ac9dbSNicholas Bellinger 	}
1779c66ac9dbSNicholas Bellinger 
17800fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
17813011684cSDan Carpenter 	if (!read_bytes)
17823011684cSDan Carpenter 		return -EINVAL;
17830fd97ccfSChristoph Hellwig 	if (dev->dev_alias[read_bytes - 1] == '\n')
17840fd97ccfSChristoph Hellwig 		dev->dev_alias[read_bytes - 1] = '\0';
17850877eafdSSebastian Andrzej Siewior 
17860fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_ALIAS;
17873011684cSDan Carpenter 
17886708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1789c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
17900fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
17910fd97ccfSChristoph Hellwig 		dev->dev_alias);
1792c66ac9dbSNicholas Bellinger 
1793c66ac9dbSNicholas Bellinger 	return read_bytes;
1794c66ac9dbSNicholas Bellinger }
1795c66ac9dbSNicholas Bellinger 
17962eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
1797c66ac9dbSNicholas Bellinger {
17982eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1799c66ac9dbSNicholas Bellinger 
18000fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1801c66ac9dbSNicholas Bellinger 		return 0;
1802c66ac9dbSNicholas Bellinger 
18030fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1804c66ac9dbSNicholas Bellinger }
1805c66ac9dbSNicholas Bellinger 
18062eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_store(struct config_item *item,
18072eafd729SChristoph Hellwig 		const char *page, size_t count)
1808c66ac9dbSNicholas Bellinger {
18092eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
18100fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1811c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1812c66ac9dbSNicholas Bellinger 
1813c66ac9dbSNicholas Bellinger 	if (count > (SE_UDEV_PATH_LEN-1)) {
18146708bb27SAndy Grover 		pr_err("udev_path count: %d exceeds"
1815c66ac9dbSNicholas Bellinger 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1816c66ac9dbSNicholas Bellinger 			SE_UDEV_PATH_LEN-1);
1817c66ac9dbSNicholas Bellinger 		return -EINVAL;
1818c66ac9dbSNicholas Bellinger 	}
1819c66ac9dbSNicholas Bellinger 
18200fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1821c66ac9dbSNicholas Bellinger 			"%s", page);
18223011684cSDan Carpenter 	if (!read_bytes)
18233011684cSDan Carpenter 		return -EINVAL;
18240fd97ccfSChristoph Hellwig 	if (dev->udev_path[read_bytes - 1] == '\n')
18250fd97ccfSChristoph Hellwig 		dev->udev_path[read_bytes - 1] = '\0';
18260877eafdSSebastian Andrzej Siewior 
18270fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_UDEV_PATH;
18283011684cSDan Carpenter 
18296708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1830c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
18310fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
18320fd97ccfSChristoph Hellwig 		dev->udev_path);
1833c66ac9dbSNicholas Bellinger 
1834c66ac9dbSNicholas Bellinger 	return read_bytes;
1835c66ac9dbSNicholas Bellinger }
1836c66ac9dbSNicholas Bellinger 
18372eafd729SChristoph Hellwig static ssize_t target_dev_enable_show(struct config_item *item, char *page)
183864146db7SAndy Grover {
18392eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
184064146db7SAndy Grover 
184164146db7SAndy Grover 	return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
184264146db7SAndy Grover }
184364146db7SAndy Grover 
18442eafd729SChristoph Hellwig static ssize_t target_dev_enable_store(struct config_item *item,
18452eafd729SChristoph Hellwig 		const char *page, size_t count)
1846c66ac9dbSNicholas Bellinger {
18472eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1848c66ac9dbSNicholas Bellinger 	char *ptr;
18490fd97ccfSChristoph Hellwig 	int ret;
1850c66ac9dbSNicholas Bellinger 
1851c66ac9dbSNicholas Bellinger 	ptr = strstr(page, "1");
18526708bb27SAndy Grover 	if (!ptr) {
18536708bb27SAndy Grover 		pr_err("For dev_enable ops, only valid value"
1854c66ac9dbSNicholas Bellinger 				" is \"1\"\n");
1855c66ac9dbSNicholas Bellinger 		return -EINVAL;
1856c66ac9dbSNicholas Bellinger 	}
1857c66ac9dbSNicholas Bellinger 
18580fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
18590fd97ccfSChristoph Hellwig 	if (ret)
18600fd97ccfSChristoph Hellwig 		return ret;
1861c66ac9dbSNicholas Bellinger 	return count;
1862c66ac9dbSNicholas Bellinger }
1863c66ac9dbSNicholas Bellinger 
18642eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
1865c66ac9dbSNicholas Bellinger {
18662eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1867c66ac9dbSNicholas Bellinger 	struct config_item *lu_ci;
1868c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
1869c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1870c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1871c66ac9dbSNicholas Bellinger 
1872c66ac9dbSNicholas Bellinger 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1873c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
1874c87fbd56SChristoph Hellwig 		return 0;
1875c66ac9dbSNicholas Bellinger 
1876c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1877c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
18786708bb27SAndy Grover 	if (lu_gp) {
1879c66ac9dbSNicholas Bellinger 		lu_ci = &lu_gp->lu_gp_group.cg_item;
1880c66ac9dbSNicholas Bellinger 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1881c66ac9dbSNicholas Bellinger 			config_item_name(lu_ci), lu_gp->lu_gp_id);
1882c66ac9dbSNicholas Bellinger 	}
1883c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1884c66ac9dbSNicholas Bellinger 
1885c66ac9dbSNicholas Bellinger 	return len;
1886c66ac9dbSNicholas Bellinger }
1887c66ac9dbSNicholas Bellinger 
18882eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
18892eafd729SChristoph Hellwig 		const char *page, size_t count)
1890c66ac9dbSNicholas Bellinger {
18912eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
18920fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1893c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1894c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1895c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
1896c66ac9dbSNicholas Bellinger 	int move = 0;
1897c66ac9dbSNicholas Bellinger 
1898c87fbd56SChristoph Hellwig 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1899c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
19009105bfc0SAndy Grover 		return count;
1901c87fbd56SChristoph Hellwig 
1902c66ac9dbSNicholas Bellinger 	if (count > LU_GROUP_NAME_BUF) {
19036708bb27SAndy Grover 		pr_err("ALUA LU Group Alias too large!\n");
1904c66ac9dbSNicholas Bellinger 		return -EINVAL;
1905c66ac9dbSNicholas Bellinger 	}
1906c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
1907c66ac9dbSNicholas Bellinger 	memcpy(buf, page, count);
1908c66ac9dbSNicholas Bellinger 	/*
1909c66ac9dbSNicholas Bellinger 	 * Any ALUA logical unit alias besides "NULL" means we will be
1910c66ac9dbSNicholas Bellinger 	 * making a new group association.
1911c66ac9dbSNicholas Bellinger 	 */
1912c66ac9dbSNicholas Bellinger 	if (strcmp(strstrip(buf), "NULL")) {
1913c66ac9dbSNicholas Bellinger 		/*
1914c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name() will increment reference to
1915c66ac9dbSNicholas Bellinger 		 * struct t10_alua_lu_gp.  This reference is released with
1916c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name below().
1917c66ac9dbSNicholas Bellinger 		 */
1918c66ac9dbSNicholas Bellinger 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
19196708bb27SAndy Grover 		if (!lu_gp_new)
1920c66ac9dbSNicholas Bellinger 			return -ENODEV;
1921c66ac9dbSNicholas Bellinger 	}
1922c66ac9dbSNicholas Bellinger 
1923c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1924c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
19256708bb27SAndy Grover 	if (lu_gp) {
1926c66ac9dbSNicholas Bellinger 		/*
1927c66ac9dbSNicholas Bellinger 		 * Clearing an existing lu_gp association, and replacing
1928c66ac9dbSNicholas Bellinger 		 * with NULL
1929c66ac9dbSNicholas Bellinger 		 */
19306708bb27SAndy Grover 		if (!lu_gp_new) {
19316708bb27SAndy Grover 			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1932c66ac9dbSNicholas Bellinger 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1933c66ac9dbSNicholas Bellinger 				" %hu\n",
1934c66ac9dbSNicholas Bellinger 				config_item_name(&hba->hba_group.cg_item),
19350fd97ccfSChristoph Hellwig 				config_item_name(&dev->dev_group.cg_item),
1936c66ac9dbSNicholas Bellinger 				config_item_name(&lu_gp->lu_gp_group.cg_item),
1937c66ac9dbSNicholas Bellinger 				lu_gp->lu_gp_id);
1938c66ac9dbSNicholas Bellinger 
1939c66ac9dbSNicholas Bellinger 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1940c66ac9dbSNicholas Bellinger 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1941c66ac9dbSNicholas Bellinger 
1942c66ac9dbSNicholas Bellinger 			return count;
1943c66ac9dbSNicholas Bellinger 		}
1944c66ac9dbSNicholas Bellinger 		/*
1945c66ac9dbSNicholas Bellinger 		 * Removing existing association of lu_gp_mem with lu_gp
1946c66ac9dbSNicholas Bellinger 		 */
1947c66ac9dbSNicholas Bellinger 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1948c66ac9dbSNicholas Bellinger 		move = 1;
1949c66ac9dbSNicholas Bellinger 	}
1950c66ac9dbSNicholas Bellinger 	/*
1951c66ac9dbSNicholas Bellinger 	 * Associate lu_gp_mem with lu_gp_new.
1952c66ac9dbSNicholas Bellinger 	 */
1953c66ac9dbSNicholas Bellinger 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1954c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1955c66ac9dbSNicholas Bellinger 
19566708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1957c66ac9dbSNicholas Bellinger 		" core/alua/lu_gps/%s, ID: %hu\n",
1958c66ac9dbSNicholas Bellinger 		(move) ? "Moving" : "Adding",
1959c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
19600fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
1961c66ac9dbSNicholas Bellinger 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1962c66ac9dbSNicholas Bellinger 		lu_gp_new->lu_gp_id);
1963c66ac9dbSNicholas Bellinger 
1964c66ac9dbSNicholas Bellinger 	core_alua_put_lu_gp_from_name(lu_gp_new);
1965c66ac9dbSNicholas Bellinger 	return count;
1966c66ac9dbSNicholas Bellinger }
1967c66ac9dbSNicholas Bellinger 
19682eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
1969229d4f11SHannes Reinecke {
19702eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1971229d4f11SHannes Reinecke 	struct t10_alua_lba_map *map;
1972229d4f11SHannes Reinecke 	struct t10_alua_lba_map_member *mem;
1973229d4f11SHannes Reinecke 	char *b = page;
1974229d4f11SHannes Reinecke 	int bl = 0;
1975229d4f11SHannes Reinecke 	char state;
1976229d4f11SHannes Reinecke 
1977229d4f11SHannes Reinecke 	spin_lock(&dev->t10_alua.lba_map_lock);
1978229d4f11SHannes Reinecke 	if (!list_empty(&dev->t10_alua.lba_map_list))
1979229d4f11SHannes Reinecke 	    bl += sprintf(b + bl, "%u %u\n",
1980229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_size,
1981229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_multiplier);
1982229d4f11SHannes Reinecke 	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1983229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "%llu %llu",
1984229d4f11SHannes Reinecke 			      map->lba_map_first_lba, map->lba_map_last_lba);
1985229d4f11SHannes Reinecke 		list_for_each_entry(mem, &map->lba_map_mem_list,
1986229d4f11SHannes Reinecke 				    lba_map_mem_list) {
1987229d4f11SHannes Reinecke 			switch (mem->lba_map_mem_alua_state) {
1988229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1989229d4f11SHannes Reinecke 				state = 'O';
1990229d4f11SHannes Reinecke 				break;
1991229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1992229d4f11SHannes Reinecke 				state = 'A';
1993229d4f11SHannes Reinecke 				break;
1994229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_STANDBY:
1995229d4f11SHannes Reinecke 				state = 'S';
1996229d4f11SHannes Reinecke 				break;
1997229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_UNAVAILABLE:
1998229d4f11SHannes Reinecke 				state = 'U';
1999229d4f11SHannes Reinecke 				break;
2000229d4f11SHannes Reinecke 			default:
2001229d4f11SHannes Reinecke 				state = '.';
2002229d4f11SHannes Reinecke 				break;
2003229d4f11SHannes Reinecke 			}
2004229d4f11SHannes Reinecke 			bl += sprintf(b + bl, " %d:%c",
2005229d4f11SHannes Reinecke 				      mem->lba_map_mem_alua_pg_id, state);
2006229d4f11SHannes Reinecke 		}
2007229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "\n");
2008229d4f11SHannes Reinecke 	}
2009229d4f11SHannes Reinecke 	spin_unlock(&dev->t10_alua.lba_map_lock);
2010229d4f11SHannes Reinecke 	return bl;
2011229d4f11SHannes Reinecke }
2012229d4f11SHannes Reinecke 
20132eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_store(struct config_item *item,
20142eafd729SChristoph Hellwig 		const char *page, size_t count)
2015229d4f11SHannes Reinecke {
20162eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2017229d4f11SHannes Reinecke 	struct t10_alua_lba_map *lba_map = NULL;
2018229d4f11SHannes Reinecke 	struct list_head lba_list;
2019229d4f11SHannes Reinecke 	char *map_entries, *ptr;
2020229d4f11SHannes Reinecke 	char state;
2021229d4f11SHannes Reinecke 	int pg_num = -1, pg;
2022229d4f11SHannes Reinecke 	int ret = 0, num = 0, pg_id, alua_state;
2023229d4f11SHannes Reinecke 	unsigned long start_lba = -1, end_lba = -1;
2024229d4f11SHannes Reinecke 	unsigned long segment_size = -1, segment_mult = -1;
2025229d4f11SHannes Reinecke 
2026229d4f11SHannes Reinecke 	map_entries = kstrdup(page, GFP_KERNEL);
2027229d4f11SHannes Reinecke 	if (!map_entries)
2028229d4f11SHannes Reinecke 		return -ENOMEM;
2029229d4f11SHannes Reinecke 
2030229d4f11SHannes Reinecke 	INIT_LIST_HEAD(&lba_list);
2031229d4f11SHannes Reinecke 	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2032229d4f11SHannes Reinecke 		if (!*ptr)
2033229d4f11SHannes Reinecke 			continue;
2034229d4f11SHannes Reinecke 
2035229d4f11SHannes Reinecke 		if (num == 0) {
2036229d4f11SHannes Reinecke 			if (sscanf(ptr, "%lu %lu\n",
2037229d4f11SHannes Reinecke 				   &segment_size, &segment_mult) != 2) {
2038229d4f11SHannes Reinecke 				pr_err("Invalid line %d\n", num);
2039229d4f11SHannes Reinecke 				ret = -EINVAL;
2040229d4f11SHannes Reinecke 				break;
2041229d4f11SHannes Reinecke 			}
2042229d4f11SHannes Reinecke 			num++;
2043229d4f11SHannes Reinecke 			continue;
2044229d4f11SHannes Reinecke 		}
2045229d4f11SHannes Reinecke 		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2046229d4f11SHannes Reinecke 			pr_err("Invalid line %d\n", num);
2047229d4f11SHannes Reinecke 			ret = -EINVAL;
2048229d4f11SHannes Reinecke 			break;
2049229d4f11SHannes Reinecke 		}
2050229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2051229d4f11SHannes Reinecke 		if (!ptr) {
2052229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing end lba\n", num);
2053229d4f11SHannes Reinecke 			ret = -EINVAL;
2054229d4f11SHannes Reinecke 			break;
2055229d4f11SHannes Reinecke 		}
2056229d4f11SHannes Reinecke 		ptr++;
2057229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2058229d4f11SHannes Reinecke 		if (!ptr) {
2059229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing state definitions\n",
2060229d4f11SHannes Reinecke 			       num);
2061229d4f11SHannes Reinecke 			ret = -EINVAL;
2062229d4f11SHannes Reinecke 			break;
2063229d4f11SHannes Reinecke 		}
2064229d4f11SHannes Reinecke 		ptr++;
2065229d4f11SHannes Reinecke 		lba_map = core_alua_allocate_lba_map(&lba_list,
2066229d4f11SHannes Reinecke 						     start_lba, end_lba);
2067229d4f11SHannes Reinecke 		if (IS_ERR(lba_map)) {
2068229d4f11SHannes Reinecke 			ret = PTR_ERR(lba_map);
2069229d4f11SHannes Reinecke 			break;
2070229d4f11SHannes Reinecke 		}
2071229d4f11SHannes Reinecke 		pg = 0;
2072229d4f11SHannes Reinecke 		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2073229d4f11SHannes Reinecke 			switch (state) {
2074229d4f11SHannes Reinecke 			case 'O':
2075229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2076229d4f11SHannes Reinecke 				break;
2077229d4f11SHannes Reinecke 			case 'A':
2078229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2079229d4f11SHannes Reinecke 				break;
2080229d4f11SHannes Reinecke 			case 'S':
2081229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_STANDBY;
2082229d4f11SHannes Reinecke 				break;
2083229d4f11SHannes Reinecke 			case 'U':
2084229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2085229d4f11SHannes Reinecke 				break;
2086229d4f11SHannes Reinecke 			default:
2087229d4f11SHannes Reinecke 				pr_err("Invalid ALUA state '%c'\n", state);
2088229d4f11SHannes Reinecke 				ret = -EINVAL;
2089229d4f11SHannes Reinecke 				goto out;
2090229d4f11SHannes Reinecke 			}
2091229d4f11SHannes Reinecke 
2092229d4f11SHannes Reinecke 			ret = core_alua_allocate_lba_map_mem(lba_map,
2093229d4f11SHannes Reinecke 							     pg_id, alua_state);
2094229d4f11SHannes Reinecke 			if (ret) {
2095229d4f11SHannes Reinecke 				pr_err("Invalid target descriptor %d:%c "
2096229d4f11SHannes Reinecke 				       "at line %d\n",
2097229d4f11SHannes Reinecke 				       pg_id, state, num);
2098229d4f11SHannes Reinecke 				break;
2099229d4f11SHannes Reinecke 			}
2100229d4f11SHannes Reinecke 			pg++;
2101229d4f11SHannes Reinecke 			ptr = strchr(ptr, ' ');
2102229d4f11SHannes Reinecke 			if (ptr)
2103229d4f11SHannes Reinecke 				ptr++;
2104229d4f11SHannes Reinecke 			else
2105229d4f11SHannes Reinecke 				break;
2106229d4f11SHannes Reinecke 		}
2107229d4f11SHannes Reinecke 		if (pg_num == -1)
2108229d4f11SHannes Reinecke 		    pg_num = pg;
2109229d4f11SHannes Reinecke 		else if (pg != pg_num) {
2110229d4f11SHannes Reinecke 			pr_err("Only %d from %d port groups definitions "
2111229d4f11SHannes Reinecke 			       "at line %d\n", pg, pg_num, num);
2112229d4f11SHannes Reinecke 			ret = -EINVAL;
2113229d4f11SHannes Reinecke 			break;
2114229d4f11SHannes Reinecke 		}
2115229d4f11SHannes Reinecke 		num++;
2116229d4f11SHannes Reinecke 	}
2117229d4f11SHannes Reinecke out:
2118229d4f11SHannes Reinecke 	if (ret) {
2119229d4f11SHannes Reinecke 		core_alua_free_lba_map(&lba_list);
2120229d4f11SHannes Reinecke 		count = ret;
2121229d4f11SHannes Reinecke 	} else
2122229d4f11SHannes Reinecke 		core_alua_set_lba_map(dev, &lba_list,
2123229d4f11SHannes Reinecke 				      segment_size, segment_mult);
2124229d4f11SHannes Reinecke 	kfree(map_entries);
2125229d4f11SHannes Reinecke 	return count;
2126229d4f11SHannes Reinecke }
2127229d4f11SHannes Reinecke 
21282eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_dev_, info);
21292eafd729SChristoph Hellwig CONFIGFS_ATTR_WO(target_dev_, control);
21302eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alias);
21312eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, udev_path);
21322eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, enable);
21332eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alua_lu_gp);
21342eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, lba_map);
2135229d4f11SHannes Reinecke 
213673112edcSNicholas Bellinger static struct configfs_attribute *target_core_dev_attrs[] = {
21372eafd729SChristoph Hellwig 	&target_dev_attr_info,
21382eafd729SChristoph Hellwig 	&target_dev_attr_control,
21392eafd729SChristoph Hellwig 	&target_dev_attr_alias,
21402eafd729SChristoph Hellwig 	&target_dev_attr_udev_path,
21412eafd729SChristoph Hellwig 	&target_dev_attr_enable,
21422eafd729SChristoph Hellwig 	&target_dev_attr_alua_lu_gp,
21432eafd729SChristoph Hellwig 	&target_dev_attr_lba_map,
2144c66ac9dbSNicholas Bellinger 	NULL,
2145c66ac9dbSNicholas Bellinger };
2146c66ac9dbSNicholas Bellinger 
2147c66ac9dbSNicholas Bellinger static void target_core_dev_release(struct config_item *item)
2148c66ac9dbSNicholas Bellinger {
21490fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
21500fd97ccfSChristoph Hellwig 	struct se_device *dev =
21510fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2152c66ac9dbSNicholas Bellinger 
2153c66ac9dbSNicholas Bellinger 	kfree(dev_cg->default_groups);
21540fd97ccfSChristoph Hellwig 	target_free_device(dev);
2155c66ac9dbSNicholas Bellinger }
2156c66ac9dbSNicholas Bellinger 
2157c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_item_ops = {
2158c66ac9dbSNicholas Bellinger 	.release		= target_core_dev_release,
2159c66ac9dbSNicholas Bellinger };
2160c66ac9dbSNicholas Bellinger 
216173112edcSNicholas Bellinger TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2162c66ac9dbSNicholas Bellinger 
216373112edcSNicholas Bellinger /* End functions for struct config_item_type tb_dev_cit */
2164c66ac9dbSNicholas Bellinger 
2165c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2166c66ac9dbSNicholas Bellinger 
21672eafd729SChristoph Hellwig static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2168c66ac9dbSNicholas Bellinger {
21692eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_lu_gp,
21702eafd729SChristoph Hellwig 			lu_gp_group);
21712eafd729SChristoph Hellwig }
21722eafd729SChristoph Hellwig 
21732eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
21742eafd729SChristoph Hellwig {
21752eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
21762eafd729SChristoph Hellwig 
21776708bb27SAndy Grover 	if (!lu_gp->lu_gp_valid_id)
2178c66ac9dbSNicholas Bellinger 		return 0;
2179c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2180c66ac9dbSNicholas Bellinger }
2181c66ac9dbSNicholas Bellinger 
21822eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
21832eafd729SChristoph Hellwig 		const char *page, size_t count)
2184c66ac9dbSNicholas Bellinger {
21852eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2186c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2187c66ac9dbSNicholas Bellinger 	unsigned long lu_gp_id;
2188c66ac9dbSNicholas Bellinger 	int ret;
2189c66ac9dbSNicholas Bellinger 
219057103d7fSJingoo Han 	ret = kstrtoul(page, 0, &lu_gp_id);
2191c66ac9dbSNicholas Bellinger 	if (ret < 0) {
219257103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2193c66ac9dbSNicholas Bellinger 			" lu_gp_id\n", ret);
219457103d7fSJingoo Han 		return ret;
2195c66ac9dbSNicholas Bellinger 	}
2196c66ac9dbSNicholas Bellinger 	if (lu_gp_id > 0x0000ffff) {
21976708bb27SAndy Grover 		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2198c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", lu_gp_id);
2199c66ac9dbSNicholas Bellinger 		return -EINVAL;
2200c66ac9dbSNicholas Bellinger 	}
2201c66ac9dbSNicholas Bellinger 
2202c66ac9dbSNicholas Bellinger 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2203c66ac9dbSNicholas Bellinger 	if (ret < 0)
2204c66ac9dbSNicholas Bellinger 		return -EINVAL;
2205c66ac9dbSNicholas Bellinger 
22066708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2207c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
2208c66ac9dbSNicholas Bellinger 		config_item_name(&alua_lu_gp_cg->cg_item),
2209c66ac9dbSNicholas Bellinger 		lu_gp->lu_gp_id);
2210c66ac9dbSNicholas Bellinger 
2211c66ac9dbSNicholas Bellinger 	return count;
2212c66ac9dbSNicholas Bellinger }
2213c66ac9dbSNicholas Bellinger 
22142eafd729SChristoph Hellwig static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2215c66ac9dbSNicholas Bellinger {
22162eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2217c66ac9dbSNicholas Bellinger 	struct se_device *dev;
2218c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2219c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2220c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2221c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
2222c66ac9dbSNicholas Bellinger 
2223c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
2224c66ac9dbSNicholas Bellinger 
2225c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp->lu_gp_lock);
2226c66ac9dbSNicholas Bellinger 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2227c66ac9dbSNicholas Bellinger 		dev = lu_gp_mem->lu_gp_mem_dev;
22280fd97ccfSChristoph Hellwig 		hba = dev->se_hba;
2229c66ac9dbSNicholas Bellinger 
2230c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2231c66ac9dbSNicholas Bellinger 			config_item_name(&hba->hba_group.cg_item),
22320fd97ccfSChristoph Hellwig 			config_item_name(&dev->dev_group.cg_item));
2233c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2234c66ac9dbSNicholas Bellinger 
2235c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
22366708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2237c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2238c66ac9dbSNicholas Bellinger 			break;
2239c66ac9dbSNicholas Bellinger 		}
2240c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2241c66ac9dbSNicholas Bellinger 		len += cur_len;
2242c66ac9dbSNicholas Bellinger 	}
2243c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp->lu_gp_lock);
2244c66ac9dbSNicholas Bellinger 
2245c66ac9dbSNicholas Bellinger 	return len;
2246c66ac9dbSNicholas Bellinger }
2247c66ac9dbSNicholas Bellinger 
22482eafd729SChristoph Hellwig CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
22492eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_lu_gp_, members);
2250c66ac9dbSNicholas Bellinger 
2251c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
22522eafd729SChristoph Hellwig 	&target_lu_gp_attr_lu_gp_id,
22532eafd729SChristoph Hellwig 	&target_lu_gp_attr_members,
2254c66ac9dbSNicholas Bellinger 	NULL,
2255c66ac9dbSNicholas Bellinger };
2256c66ac9dbSNicholas Bellinger 
22571f6fe7cbSNicholas Bellinger static void target_core_alua_lu_gp_release(struct config_item *item)
22581f6fe7cbSNicholas Bellinger {
22591f6fe7cbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
22601f6fe7cbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
22611f6fe7cbSNicholas Bellinger 
22621f6fe7cbSNicholas Bellinger 	core_alua_free_lu_gp(lu_gp);
22631f6fe7cbSNicholas Bellinger }
22641f6fe7cbSNicholas Bellinger 
2265c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_lu_gp_ops = {
22661f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_lu_gp_release,
2267c66ac9dbSNicholas Bellinger };
2268c66ac9dbSNicholas Bellinger 
2269c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gp_cit = {
2270c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
2271c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_lu_gp_attrs,
2272c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2273c66ac9dbSNicholas Bellinger };
2274c66ac9dbSNicholas Bellinger 
2275c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2276c66ac9dbSNicholas Bellinger 
2277c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2278c66ac9dbSNicholas Bellinger 
2279c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_lu_gp(
2280c66ac9dbSNicholas Bellinger 	struct config_group *group,
2281c66ac9dbSNicholas Bellinger 	const char *name)
2282c66ac9dbSNicholas Bellinger {
2283c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2284c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = NULL;
2285c66ac9dbSNicholas Bellinger 	struct config_item *alua_lu_gp_ci = NULL;
2286c66ac9dbSNicholas Bellinger 
2287c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp(name, 0);
2288c66ac9dbSNicholas Bellinger 	if (IS_ERR(lu_gp))
2289c66ac9dbSNicholas Bellinger 		return NULL;
2290c66ac9dbSNicholas Bellinger 
2291c66ac9dbSNicholas Bellinger 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
2292c66ac9dbSNicholas Bellinger 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2293c66ac9dbSNicholas Bellinger 
2294c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_lu_gp_cg, name,
2295c66ac9dbSNicholas Bellinger 			&target_core_alua_lu_gp_cit);
2296c66ac9dbSNicholas Bellinger 
22976708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2298c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s\n",
2299c66ac9dbSNicholas Bellinger 		config_item_name(alua_lu_gp_ci));
2300c66ac9dbSNicholas Bellinger 
2301c66ac9dbSNicholas Bellinger 	return alua_lu_gp_cg;
2302c66ac9dbSNicholas Bellinger 
2303c66ac9dbSNicholas Bellinger }
2304c66ac9dbSNicholas Bellinger 
2305c66ac9dbSNicholas Bellinger static void target_core_alua_drop_lu_gp(
2306c66ac9dbSNicholas Bellinger 	struct config_group *group,
2307c66ac9dbSNicholas Bellinger 	struct config_item *item)
2308c66ac9dbSNicholas Bellinger {
2309c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2310c66ac9dbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
2311c66ac9dbSNicholas Bellinger 
23126708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2313c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
2314c66ac9dbSNicholas Bellinger 		config_item_name(item), lu_gp->lu_gp_id);
23151f6fe7cbSNicholas Bellinger 	/*
23161f6fe7cbSNicholas Bellinger 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
23171f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_lu_gp_release()
23181f6fe7cbSNicholas Bellinger 	 */
2319c66ac9dbSNicholas Bellinger 	config_item_put(item);
2320c66ac9dbSNicholas Bellinger }
2321c66ac9dbSNicholas Bellinger 
2322c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2323c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_lu_gp,
2324c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_lu_gp,
2325c66ac9dbSNicholas Bellinger };
2326c66ac9dbSNicholas Bellinger 
2327c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gps_cit = {
2328c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2329c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
2330c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2331c66ac9dbSNicholas Bellinger };
2332c66ac9dbSNicholas Bellinger 
2333c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2334c66ac9dbSNicholas Bellinger 
2335c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2336c66ac9dbSNicholas Bellinger 
23372eafd729SChristoph Hellwig static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
23382eafd729SChristoph Hellwig {
23392eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
23402eafd729SChristoph Hellwig 			tg_pt_gp_group);
23412eafd729SChristoph Hellwig }
2342c66ac9dbSNicholas Bellinger 
23432eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2344c66ac9dbSNicholas Bellinger 		char *page)
2345c66ac9dbSNicholas Bellinger {
2346c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n",
23472eafd729SChristoph Hellwig 		atomic_read(&to_tg_pt_gp(item)->tg_pt_gp_alua_access_state));
2348c66ac9dbSNicholas Bellinger }
2349c66ac9dbSNicholas Bellinger 
23502eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
23512eafd729SChristoph Hellwig 		const char *page, size_t count)
2352c66ac9dbSNicholas Bellinger {
23532eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
23540fd97ccfSChristoph Hellwig 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2355c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2356c66ac9dbSNicholas Bellinger 	int new_state, ret;
2357c66ac9dbSNicholas Bellinger 
23586708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2359125d0119SHannes Reinecke 		pr_err("Unable to do implicit ALUA on non valid"
2360c66ac9dbSNicholas Bellinger 			" tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2361c66ac9dbSNicholas Bellinger 		return -EINVAL;
2362c66ac9dbSNicholas Bellinger 	}
2363f1453773SNicholas Bellinger 	if (!(dev->dev_flags & DF_CONFIGURED)) {
2364f1453773SNicholas Bellinger 		pr_err("Unable to set alua_access_state while device is"
2365f1453773SNicholas Bellinger 		       " not configured\n");
2366f1453773SNicholas Bellinger 		return -ENODEV;
2367f1453773SNicholas Bellinger 	}
2368c66ac9dbSNicholas Bellinger 
236957103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2370c66ac9dbSNicholas Bellinger 	if (ret < 0) {
23716708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access state from"
2372c66ac9dbSNicholas Bellinger 				" %s\n", page);
237357103d7fSJingoo Han 		return ret;
2374c66ac9dbSNicholas Bellinger 	}
2375c66ac9dbSNicholas Bellinger 	new_state = (int)tmp;
2376c66ac9dbSNicholas Bellinger 
2377125d0119SHannes Reinecke 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2378125d0119SHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA"
2379125d0119SHannes Reinecke 			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
2380c66ac9dbSNicholas Bellinger 		return -EINVAL;
2381c66ac9dbSNicholas Bellinger 	}
2382c66094bfSHannes Reinecke 	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2383c66094bfSHannes Reinecke 	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2384c66094bfSHannes Reinecke 		/* LBA DEPENDENT is only allowed with implicit ALUA */
2385c66094bfSHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA transition"
2386c66094bfSHannes Reinecke 		       " while explicit ALUA management is enabled\n");
2387c66094bfSHannes Reinecke 		return -EINVAL;
2388c66094bfSHannes Reinecke 	}
2389c66ac9dbSNicholas Bellinger 
23900fd97ccfSChristoph Hellwig 	ret = core_alua_do_port_transition(tg_pt_gp, dev,
2391c66ac9dbSNicholas Bellinger 					NULL, NULL, new_state, 0);
2392c66ac9dbSNicholas Bellinger 	return (!ret) ? count : -EINVAL;
2393c66ac9dbSNicholas Bellinger }
2394c66ac9dbSNicholas Bellinger 
23952eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2396c66ac9dbSNicholas Bellinger 		char *page)
2397c66ac9dbSNicholas Bellinger {
23982eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2399c66ac9dbSNicholas Bellinger 	return sprintf(page, "%s\n",
2400c66ac9dbSNicholas Bellinger 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2401c66ac9dbSNicholas Bellinger }
2402c66ac9dbSNicholas Bellinger 
24032eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_store(
24042eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2405c66ac9dbSNicholas Bellinger {
24062eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2407c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2408c66ac9dbSNicholas Bellinger 	int new_status, ret;
2409c66ac9dbSNicholas Bellinger 
24106708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
24116708bb27SAndy Grover 		pr_err("Unable to do set ALUA access status on non"
2412c66ac9dbSNicholas Bellinger 			" valid tg_pt_gp ID: %hu\n",
2413c66ac9dbSNicholas Bellinger 			tg_pt_gp->tg_pt_gp_valid_id);
2414c66ac9dbSNicholas Bellinger 		return -EINVAL;
2415c66ac9dbSNicholas Bellinger 	}
2416c66ac9dbSNicholas Bellinger 
241757103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2418c66ac9dbSNicholas Bellinger 	if (ret < 0) {
24196708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access status"
2420c66ac9dbSNicholas Bellinger 				" from %s\n", page);
242157103d7fSJingoo Han 		return ret;
2422c66ac9dbSNicholas Bellinger 	}
2423c66ac9dbSNicholas Bellinger 	new_status = (int)tmp;
2424c66ac9dbSNicholas Bellinger 
2425c66ac9dbSNicholas Bellinger 	if ((new_status != ALUA_STATUS_NONE) &&
2426125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2427125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
24286708bb27SAndy Grover 		pr_err("Illegal ALUA access status: 0x%02x\n",
2429c66ac9dbSNicholas Bellinger 				new_status);
2430c66ac9dbSNicholas Bellinger 		return -EINVAL;
2431c66ac9dbSNicholas Bellinger 	}
2432c66ac9dbSNicholas Bellinger 
2433c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2434c66ac9dbSNicholas Bellinger 	return count;
2435c66ac9dbSNicholas Bellinger }
2436c66ac9dbSNicholas Bellinger 
24372eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2438c66ac9dbSNicholas Bellinger 		char *page)
2439c66ac9dbSNicholas Bellinger {
24402eafd729SChristoph Hellwig 	return core_alua_show_access_type(to_tg_pt_gp(item), page);
2441c66ac9dbSNicholas Bellinger }
2442c66ac9dbSNicholas Bellinger 
24432eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
24442eafd729SChristoph Hellwig 		const char *page, size_t count)
2445c66ac9dbSNicholas Bellinger {
24462eafd729SChristoph Hellwig 	return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2447c66ac9dbSNicholas Bellinger }
2448c66ac9dbSNicholas Bellinger 
24492eafd729SChristoph Hellwig #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit)				\
24502eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_show(		\
24512eafd729SChristoph Hellwig 		struct config_item *item, char *p)			\
2452b0a382c5SHannes Reinecke {									\
24532eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
24542eafd729SChristoph Hellwig 	return sprintf(p, "%d\n",					\
24552eafd729SChristoph Hellwig 		!!(t->tg_pt_gp_alua_supported_states & _bit));		\
24562eafd729SChristoph Hellwig }									\
24572eafd729SChristoph Hellwig 									\
24582eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_store(		\
24592eafd729SChristoph Hellwig 		struct config_item *item, const char *p, size_t c)	\
2460b0a382c5SHannes Reinecke {									\
24612eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
2462b0a382c5SHannes Reinecke 	unsigned long tmp;						\
2463b0a382c5SHannes Reinecke 	int ret;							\
2464b0a382c5SHannes Reinecke 									\
2465b0a382c5SHannes Reinecke 	if (!t->tg_pt_gp_valid_id) {					\
2466b0a382c5SHannes Reinecke 		pr_err("Unable to do set ##_name ALUA state on non"	\
2467b0a382c5SHannes Reinecke 		       " valid tg_pt_gp ID: %hu\n",			\
2468b0a382c5SHannes Reinecke 		       t->tg_pt_gp_valid_id);				\
2469b0a382c5SHannes Reinecke 		return -EINVAL;						\
2470b0a382c5SHannes Reinecke 	}								\
2471b0a382c5SHannes Reinecke 									\
2472b0a382c5SHannes Reinecke 	ret = kstrtoul(p, 0, &tmp);					\
2473b0a382c5SHannes Reinecke 	if (ret < 0) {							\
2474b0a382c5SHannes Reinecke 		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
2475b0a382c5SHannes Reinecke 		return -EINVAL;						\
2476b0a382c5SHannes Reinecke 	}								\
2477b0a382c5SHannes Reinecke 	if (tmp > 1) {							\
2478b0a382c5SHannes Reinecke 		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2479b0a382c5SHannes Reinecke 		return -EINVAL;						\
2480b0a382c5SHannes Reinecke 	}								\
24811f0b030cSSebastian Herbszt 	if (tmp)							\
24822eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states |= _bit;		\
2483b0a382c5SHannes Reinecke 	else								\
24842eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states &= ~_bit;		\
2485b0a382c5SHannes Reinecke 									\
2486b0a382c5SHannes Reinecke 	return c;							\
24876be526c4SHannes Reinecke }
24886be526c4SHannes Reinecke 
24892eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
24902eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
24912eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
24922eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
24932eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
24942eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
24952eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
24966be526c4SHannes Reinecke 
24972eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_show(
24982eafd729SChristoph Hellwig 		struct config_item *item, char *page)
2499c66ac9dbSNicholas Bellinger {
25002eafd729SChristoph Hellwig 	return sprintf(page, "%d\n",
25012eafd729SChristoph Hellwig 		to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
2502c66ac9dbSNicholas Bellinger }
2503c66ac9dbSNicholas Bellinger 
25042eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_store(
25052eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2506c66ac9dbSNicholas Bellinger {
25072eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2508c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2509c66ac9dbSNicholas Bellinger 	int ret;
2510c66ac9dbSNicholas Bellinger 
251157103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2512c66ac9dbSNicholas Bellinger 	if (ret < 0) {
25136708bb27SAndy Grover 		pr_err("Unable to extract alua_write_metadata\n");
251457103d7fSJingoo Han 		return ret;
2515c66ac9dbSNicholas Bellinger 	}
2516c66ac9dbSNicholas Bellinger 
2517c66ac9dbSNicholas Bellinger 	if ((tmp != 0) && (tmp != 1)) {
25186708bb27SAndy Grover 		pr_err("Illegal value for alua_write_metadata:"
2519c66ac9dbSNicholas Bellinger 			" %lu\n", tmp);
2520c66ac9dbSNicholas Bellinger 		return -EINVAL;
2521c66ac9dbSNicholas Bellinger 	}
2522c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2523c66ac9dbSNicholas Bellinger 
2524c66ac9dbSNicholas Bellinger 	return count;
2525c66ac9dbSNicholas Bellinger }
2526c66ac9dbSNicholas Bellinger 
25272eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2528c66ac9dbSNicholas Bellinger 		char *page)
2529c66ac9dbSNicholas Bellinger {
25302eafd729SChristoph Hellwig 	return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
2531c66ac9dbSNicholas Bellinger }
2532c66ac9dbSNicholas Bellinger 
25332eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
25342eafd729SChristoph Hellwig 		const char *page, size_t count)
2535c66ac9dbSNicholas Bellinger {
25362eafd729SChristoph Hellwig 	return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
25372eafd729SChristoph Hellwig 			count);
2538c66ac9dbSNicholas Bellinger }
2539c66ac9dbSNicholas Bellinger 
25402eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2541c66ac9dbSNicholas Bellinger 		char *page)
2542c66ac9dbSNicholas Bellinger {
25432eafd729SChristoph Hellwig 	return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
2544c66ac9dbSNicholas Bellinger }
2545c66ac9dbSNicholas Bellinger 
25462eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
25472eafd729SChristoph Hellwig 		const char *page, size_t count)
2548c66ac9dbSNicholas Bellinger {
25492eafd729SChristoph Hellwig 	return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
25502eafd729SChristoph Hellwig 			count);
2551c66ac9dbSNicholas Bellinger }
2552c66ac9dbSNicholas Bellinger 
25532eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
25542eafd729SChristoph Hellwig 		struct config_item *item, char *page)
25552eafd729SChristoph Hellwig {
25562eafd729SChristoph Hellwig 	return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
25572eafd729SChristoph Hellwig }
2558c66ac9dbSNicholas Bellinger 
25592eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
25602eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
25612eafd729SChristoph Hellwig {
25622eafd729SChristoph Hellwig 	return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
25632eafd729SChristoph Hellwig 			count);
25642eafd729SChristoph Hellwig }
25652eafd729SChristoph Hellwig 
25662eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
25675b9a4d72SNicholas Bellinger 		char *page)
25685b9a4d72SNicholas Bellinger {
25692eafd729SChristoph Hellwig 	return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
25705b9a4d72SNicholas Bellinger }
25715b9a4d72SNicholas Bellinger 
25722eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
25732eafd729SChristoph Hellwig 		const char *page, size_t count)
25745b9a4d72SNicholas Bellinger {
25752eafd729SChristoph Hellwig 	return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
25765b9a4d72SNicholas Bellinger }
25775b9a4d72SNicholas Bellinger 
25782eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2579c66ac9dbSNicholas Bellinger 		char *page)
2580c66ac9dbSNicholas Bellinger {
25812eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2582c66ac9dbSNicholas Bellinger 
25836708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id)
2584c66ac9dbSNicholas Bellinger 		return 0;
2585c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2586c66ac9dbSNicholas Bellinger }
2587c66ac9dbSNicholas Bellinger 
25882eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
25892eafd729SChristoph Hellwig 		const char *page, size_t count)
2590c66ac9dbSNicholas Bellinger {
25912eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2592c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2593c66ac9dbSNicholas Bellinger 	unsigned long tg_pt_gp_id;
2594c66ac9dbSNicholas Bellinger 	int ret;
2595c66ac9dbSNicholas Bellinger 
259657103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tg_pt_gp_id);
2597c66ac9dbSNicholas Bellinger 	if (ret < 0) {
259857103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2599c66ac9dbSNicholas Bellinger 			" tg_pt_gp_id\n", ret);
260057103d7fSJingoo Han 		return ret;
2601c66ac9dbSNicholas Bellinger 	}
2602c66ac9dbSNicholas Bellinger 	if (tg_pt_gp_id > 0x0000ffff) {
26036708bb27SAndy Grover 		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2604c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", tg_pt_gp_id);
2605c66ac9dbSNicholas Bellinger 		return -EINVAL;
2606c66ac9dbSNicholas Bellinger 	}
2607c66ac9dbSNicholas Bellinger 
2608c66ac9dbSNicholas Bellinger 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2609c66ac9dbSNicholas Bellinger 	if (ret < 0)
2610c66ac9dbSNicholas Bellinger 		return -EINVAL;
2611c66ac9dbSNicholas Bellinger 
26126708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2613c66ac9dbSNicholas Bellinger 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
2614c66ac9dbSNicholas Bellinger 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
2615c66ac9dbSNicholas Bellinger 		tg_pt_gp->tg_pt_gp_id);
2616c66ac9dbSNicholas Bellinger 
2617c66ac9dbSNicholas Bellinger 	return count;
2618c66ac9dbSNicholas Bellinger }
2619c66ac9dbSNicholas Bellinger 
26202eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2621c66ac9dbSNicholas Bellinger 		char *page)
2622c66ac9dbSNicholas Bellinger {
26232eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2624c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
2625c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2626c66ac9dbSNicholas Bellinger 	unsigned char buf[TG_PT_GROUP_NAME_BUF];
2627c66ac9dbSNicholas Bellinger 
2628c66ac9dbSNicholas Bellinger 	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2629c66ac9dbSNicholas Bellinger 
2630c66ac9dbSNicholas Bellinger 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2631adf653f9SChristoph Hellwig 	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2632adf653f9SChristoph Hellwig 			lun_tg_pt_gp_link) {
2633adf653f9SChristoph Hellwig 		struct se_portal_group *tpg = lun->lun_tpg;
2634c66ac9dbSNicholas Bellinger 
2635c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2636e3d6f909SAndy Grover 			"/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2637e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2638e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
2639c66ac9dbSNicholas Bellinger 			config_item_name(&lun->lun_group.cg_item));
2640c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2641c66ac9dbSNicholas Bellinger 
2642c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
26436708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2644c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2645c66ac9dbSNicholas Bellinger 			break;
2646c66ac9dbSNicholas Bellinger 		}
2647c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2648c66ac9dbSNicholas Bellinger 		len += cur_len;
2649c66ac9dbSNicholas Bellinger 	}
2650c66ac9dbSNicholas Bellinger 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2651c66ac9dbSNicholas Bellinger 
2652c66ac9dbSNicholas Bellinger 	return len;
2653c66ac9dbSNicholas Bellinger }
2654c66ac9dbSNicholas Bellinger 
26552eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
26562eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
26572eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
26582eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
26592eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
26602eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
26612eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
26622eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
26632eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
26642eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
26652eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
26662eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
26672eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
26682eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
26692eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
26702eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
26712eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
2672c66ac9dbSNicholas Bellinger 
2673c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
26742eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_state,
26752eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_status,
26762eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_type,
26772eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_transitioning,
26782eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_offline,
26792eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_lba_dependent,
26802eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_unavailable,
26812eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_standby,
26822eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_nonoptimized,
26832eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_optimized,
26842eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_write_metadata,
26852eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_nonop_delay_msecs,
26862eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_trans_delay_msecs,
26872eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_implicit_trans_secs,
26882eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_preferred,
26892eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_tg_pt_gp_id,
26902eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_members,
2691c66ac9dbSNicholas Bellinger 	NULL,
2692c66ac9dbSNicholas Bellinger };
2693c66ac9dbSNicholas Bellinger 
26941f6fe7cbSNicholas Bellinger static void target_core_alua_tg_pt_gp_release(struct config_item *item)
26951f6fe7cbSNicholas Bellinger {
26961f6fe7cbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
26971f6fe7cbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
26981f6fe7cbSNicholas Bellinger 
26991f6fe7cbSNicholas Bellinger 	core_alua_free_tg_pt_gp(tg_pt_gp);
27001f6fe7cbSNicholas Bellinger }
27011f6fe7cbSNicholas Bellinger 
2702c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
27031f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_tg_pt_gp_release,
2704c66ac9dbSNicholas Bellinger };
2705c66ac9dbSNicholas Bellinger 
2706c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2707c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
2708c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
2709c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2710c66ac9dbSNicholas Bellinger };
2711c66ac9dbSNicholas Bellinger 
2712c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2713c66ac9dbSNicholas Bellinger 
271472aca57bSNicholas Bellinger /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2715c66ac9dbSNicholas Bellinger 
2716c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_tg_pt_gp(
2717c66ac9dbSNicholas Bellinger 	struct config_group *group,
2718c66ac9dbSNicholas Bellinger 	const char *name)
2719c66ac9dbSNicholas Bellinger {
2720c66ac9dbSNicholas Bellinger 	struct t10_alua *alua = container_of(group, struct t10_alua,
2721c66ac9dbSNicholas Bellinger 					alua_tg_pt_gps_group);
2722c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2723c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = NULL;
2724c66ac9dbSNicholas Bellinger 	struct config_item *alua_tg_pt_gp_ci = NULL;
2725c66ac9dbSNicholas Bellinger 
27260fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
27276708bb27SAndy Grover 	if (!tg_pt_gp)
2728c66ac9dbSNicholas Bellinger 		return NULL;
2729c66ac9dbSNicholas Bellinger 
2730c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2731c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2732c66ac9dbSNicholas Bellinger 
2733c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
2734c66ac9dbSNicholas Bellinger 			&target_core_alua_tg_pt_gp_cit);
2735c66ac9dbSNicholas Bellinger 
27366708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2737c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s\n",
2738c66ac9dbSNicholas Bellinger 		config_item_name(alua_tg_pt_gp_ci));
2739c66ac9dbSNicholas Bellinger 
2740c66ac9dbSNicholas Bellinger 	return alua_tg_pt_gp_cg;
2741c66ac9dbSNicholas Bellinger }
2742c66ac9dbSNicholas Bellinger 
2743c66ac9dbSNicholas Bellinger static void target_core_alua_drop_tg_pt_gp(
2744c66ac9dbSNicholas Bellinger 	struct config_group *group,
2745c66ac9dbSNicholas Bellinger 	struct config_item *item)
2746c66ac9dbSNicholas Bellinger {
2747c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2748c66ac9dbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2749c66ac9dbSNicholas Bellinger 
27506708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2751c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
2752c66ac9dbSNicholas Bellinger 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
27531f6fe7cbSNicholas Bellinger 	/*
27541f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
27551f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_tg_pt_gp_release().
27561f6fe7cbSNicholas Bellinger 	 */
2757c66ac9dbSNicholas Bellinger 	config_item_put(item);
2758c66ac9dbSNicholas Bellinger }
2759c66ac9dbSNicholas Bellinger 
2760c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2761c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_tg_pt_gp,
2762c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
2763c66ac9dbSNicholas Bellinger };
2764c66ac9dbSNicholas Bellinger 
276572aca57bSNicholas Bellinger TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
2766c66ac9dbSNicholas Bellinger 
276772aca57bSNicholas Bellinger /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2768c66ac9dbSNicholas Bellinger 
2769c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_cit */
2770c66ac9dbSNicholas Bellinger 
2771c66ac9dbSNicholas Bellinger /*
2772c66ac9dbSNicholas Bellinger  * target_core_alua_cit is a ConfigFS group that lives under
2773c66ac9dbSNicholas Bellinger  * /sys/kernel/config/target/core/alua.  There are default groups
2774c66ac9dbSNicholas Bellinger  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2775c66ac9dbSNicholas Bellinger  * target_core_alua_cit in target_core_init_configfs() below.
2776c66ac9dbSNicholas Bellinger  */
2777c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_cit = {
2778c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2779c66ac9dbSNicholas Bellinger 	.ct_attrs		= NULL,
2780c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2781c66ac9dbSNicholas Bellinger };
2782c66ac9dbSNicholas Bellinger 
2783c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_cit */
2784c66ac9dbSNicholas Bellinger 
2785d23ab570SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_stat_cit */
278612d23384SNicholas Bellinger 
278712d23384SNicholas Bellinger static struct config_group *target_core_stat_mkdir(
278812d23384SNicholas Bellinger 	struct config_group *group,
278912d23384SNicholas Bellinger 	const char *name)
279012d23384SNicholas Bellinger {
279112d23384SNicholas Bellinger 	return ERR_PTR(-ENOSYS);
279212d23384SNicholas Bellinger }
279312d23384SNicholas Bellinger 
279412d23384SNicholas Bellinger static void target_core_stat_rmdir(
279512d23384SNicholas Bellinger 	struct config_group *group,
279612d23384SNicholas Bellinger 	struct config_item *item)
279712d23384SNicholas Bellinger {
279812d23384SNicholas Bellinger 	return;
279912d23384SNicholas Bellinger }
280012d23384SNicholas Bellinger 
280112d23384SNicholas Bellinger static struct configfs_group_operations target_core_stat_group_ops = {
280212d23384SNicholas Bellinger 	.make_group		= &target_core_stat_mkdir,
280312d23384SNicholas Bellinger 	.drop_item		= &target_core_stat_rmdir,
280412d23384SNicholas Bellinger };
280512d23384SNicholas Bellinger 
2806d23ab570SNicholas Bellinger TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
280712d23384SNicholas Bellinger 
2808d23ab570SNicholas Bellinger /* End functions for struct config_item_type tb_dev_stat_cit */
280912d23384SNicholas Bellinger 
2810c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_hba_cit */
2811c66ac9dbSNicholas Bellinger 
2812c66ac9dbSNicholas Bellinger static struct config_group *target_core_make_subdev(
2813c66ac9dbSNicholas Bellinger 	struct config_group *group,
2814c66ac9dbSNicholas Bellinger 	const char *name)
2815c66ac9dbSNicholas Bellinger {
2816c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2817c66ac9dbSNicholas Bellinger 	struct config_item *hba_ci = &group->cg_item;
2818c66ac9dbSNicholas Bellinger 	struct se_hba *hba = item_to_hba(hba_ci);
28190a06d430SChristoph Hellwig 	struct target_backend *tb = hba->backend;
28200fd97ccfSChristoph Hellwig 	struct se_device *dev;
2821c66ac9dbSNicholas Bellinger 	struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
282212d23384SNicholas Bellinger 	struct config_group *dev_stat_grp = NULL;
282312d23384SNicholas Bellinger 	int errno = -ENOMEM, ret;
2824c66ac9dbSNicholas Bellinger 
282512d23384SNicholas Bellinger 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
282612d23384SNicholas Bellinger 	if (ret)
282712d23384SNicholas Bellinger 		return ERR_PTR(ret);
2828c66ac9dbSNicholas Bellinger 
28290fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, name);
28300fd97ccfSChristoph Hellwig 	if (!dev)
28310fd97ccfSChristoph Hellwig 		goto out_unlock;
2832c66ac9dbSNicholas Bellinger 
28330fd97ccfSChristoph Hellwig 	dev_cg = &dev->dev_group;
2834c66ac9dbSNicholas Bellinger 
283513f6a914SSebastian Andrzej Siewior 	dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
2836c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
28376708bb27SAndy Grover 	if (!dev_cg->default_groups)
28380fd97ccfSChristoph Hellwig 		goto out_free_device;
2839c66ac9dbSNicholas Bellinger 
28400a06d430SChristoph Hellwig 	config_group_init_type_name(dev_cg, name, &tb->tb_dev_cit);
28410fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
28420a06d430SChristoph Hellwig 			&tb->tb_dev_attrib_cit);
28430fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_pr_group, "pr",
28440a06d430SChristoph Hellwig 			&tb->tb_dev_pr_cit);
28450fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
28460a06d430SChristoph Hellwig 			&tb->tb_dev_wwn_cit);
28470fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
28480a06d430SChristoph Hellwig 			"alua", &tb->tb_dev_alua_tg_pt_gps_cit);
28490fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
28500a06d430SChristoph Hellwig 			"statistics", &tb->tb_dev_stat_cit);
285112d23384SNicholas Bellinger 
28520fd97ccfSChristoph Hellwig 	dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
28530fd97ccfSChristoph Hellwig 	dev_cg->default_groups[1] = &dev->dev_pr_group;
28540fd97ccfSChristoph Hellwig 	dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
28550fd97ccfSChristoph Hellwig 	dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
28560fd97ccfSChristoph Hellwig 	dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
285712d23384SNicholas Bellinger 	dev_cg->default_groups[5] = NULL;
2858c66ac9dbSNicholas Bellinger 	/*
285912d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2860c66ac9dbSNicholas Bellinger 	 */
28610fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
28626708bb27SAndy Grover 	if (!tg_pt_gp)
28630fd97ccfSChristoph Hellwig 		goto out_free_dev_cg_default_groups;
28640fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2865c66ac9dbSNicholas Bellinger 
28660fd97ccfSChristoph Hellwig 	tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
286713f6a914SSebastian Andrzej Siewior 	tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2868c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
28696708bb27SAndy Grover 	if (!tg_pt_gp_cg->default_groups) {
28706708bb27SAndy Grover 		pr_err("Unable to allocate tg_pt_gp_cg->"
2871c66ac9dbSNicholas Bellinger 				"default_groups\n");
28720fd97ccfSChristoph Hellwig 		goto out_free_tg_pt_gp;
2873c66ac9dbSNicholas Bellinger 	}
2874c66ac9dbSNicholas Bellinger 
2875c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2876c66ac9dbSNicholas Bellinger 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2877c66ac9dbSNicholas Bellinger 	tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2878c66ac9dbSNicholas Bellinger 	tg_pt_gp_cg->default_groups[1] = NULL;
287912d23384SNicholas Bellinger 	/*
288012d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/statistics/ default groups
288112d23384SNicholas Bellinger 	 */
28820fd97ccfSChristoph Hellwig 	dev_stat_grp = &dev->dev_stat_grps.stat_group;
288313f6a914SSebastian Andrzej Siewior 	dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
288412d23384SNicholas Bellinger 				GFP_KERNEL);
288512d23384SNicholas Bellinger 	if (!dev_stat_grp->default_groups) {
28866708bb27SAndy Grover 		pr_err("Unable to allocate dev_stat_grp->default_groups\n");
28870fd97ccfSChristoph Hellwig 		goto out_free_tg_pt_gp_cg_default_groups;
288812d23384SNicholas Bellinger 	}
28890fd97ccfSChristoph Hellwig 	target_stat_setup_dev_default_groups(dev);
2890c66ac9dbSNicholas Bellinger 
2891c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
28920fd97ccfSChristoph Hellwig 	return dev_cg;
28930fd97ccfSChristoph Hellwig 
28940fd97ccfSChristoph Hellwig out_free_tg_pt_gp_cg_default_groups:
2895c66ac9dbSNicholas Bellinger 	kfree(tg_pt_gp_cg->default_groups);
28960fd97ccfSChristoph Hellwig out_free_tg_pt_gp:
28970fd97ccfSChristoph Hellwig 	core_alua_free_tg_pt_gp(tg_pt_gp);
28980fd97ccfSChristoph Hellwig out_free_dev_cg_default_groups:
2899c66ac9dbSNicholas Bellinger 	kfree(dev_cg->default_groups);
29000fd97ccfSChristoph Hellwig out_free_device:
29010fd97ccfSChristoph Hellwig 	target_free_device(dev);
29020fd97ccfSChristoph Hellwig out_unlock:
2903c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
290412d23384SNicholas Bellinger 	return ERR_PTR(errno);
2905c66ac9dbSNicholas Bellinger }
2906c66ac9dbSNicholas Bellinger 
2907c66ac9dbSNicholas Bellinger static void target_core_drop_subdev(
2908c66ac9dbSNicholas Bellinger 	struct config_group *group,
2909c66ac9dbSNicholas Bellinger 	struct config_item *item)
2910c66ac9dbSNicholas Bellinger {
29110fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
29120fd97ccfSChristoph Hellwig 	struct se_device *dev =
29130fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2914c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2915c66ac9dbSNicholas Bellinger 	struct config_item *df_item;
29160fd97ccfSChristoph Hellwig 	struct config_group *tg_pt_gp_cg, *dev_stat_grp;
29171f6fe7cbSNicholas Bellinger 	int i;
2918c66ac9dbSNicholas Bellinger 
29190fd97ccfSChristoph Hellwig 	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
2920c66ac9dbSNicholas Bellinger 
29211f6fe7cbSNicholas Bellinger 	mutex_lock(&hba->hba_access_mutex);
2922c66ac9dbSNicholas Bellinger 
29230fd97ccfSChristoph Hellwig 	dev_stat_grp = &dev->dev_stat_grps.stat_group;
292412d23384SNicholas Bellinger 	for (i = 0; dev_stat_grp->default_groups[i]; i++) {
292512d23384SNicholas Bellinger 		df_item = &dev_stat_grp->default_groups[i]->cg_item;
292612d23384SNicholas Bellinger 		dev_stat_grp->default_groups[i] = NULL;
292712d23384SNicholas Bellinger 		config_item_put(df_item);
292812d23384SNicholas Bellinger 	}
292912d23384SNicholas Bellinger 	kfree(dev_stat_grp->default_groups);
293012d23384SNicholas Bellinger 
29310fd97ccfSChristoph Hellwig 	tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2932c66ac9dbSNicholas Bellinger 	for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2933c66ac9dbSNicholas Bellinger 		df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2934c66ac9dbSNicholas Bellinger 		tg_pt_gp_cg->default_groups[i] = NULL;
2935c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
2936c66ac9dbSNicholas Bellinger 	}
2937c66ac9dbSNicholas Bellinger 	kfree(tg_pt_gp_cg->default_groups);
29381f6fe7cbSNicholas Bellinger 	/*
29391f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
29401f6fe7cbSNicholas Bellinger 	 * directly from target_core_alua_tg_pt_gp_release().
29411f6fe7cbSNicholas Bellinger 	 */
29420fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = NULL;
2943c66ac9dbSNicholas Bellinger 
2944c66ac9dbSNicholas Bellinger 	for (i = 0; dev_cg->default_groups[i]; i++) {
2945c66ac9dbSNicholas Bellinger 		df_item = &dev_cg->default_groups[i]->cg_item;
2946c66ac9dbSNicholas Bellinger 		dev_cg->default_groups[i] = NULL;
2947c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
2948c66ac9dbSNicholas Bellinger 	}
29491f6fe7cbSNicholas Bellinger 	/*
29500fd97ccfSChristoph Hellwig 	 * se_dev is released from target_core_dev_item_ops->release()
29511f6fe7cbSNicholas Bellinger 	 */
2952c66ac9dbSNicholas Bellinger 	config_item_put(item);
2953c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
2954c66ac9dbSNicholas Bellinger }
2955c66ac9dbSNicholas Bellinger 
2956c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_hba_group_ops = {
2957c66ac9dbSNicholas Bellinger 	.make_group		= target_core_make_subdev,
2958c66ac9dbSNicholas Bellinger 	.drop_item		= target_core_drop_subdev,
2959c66ac9dbSNicholas Bellinger };
2960c66ac9dbSNicholas Bellinger 
2961c66ac9dbSNicholas Bellinger 
29622eafd729SChristoph Hellwig static inline struct se_hba *to_hba(struct config_item *item)
2963c66ac9dbSNicholas Bellinger {
29642eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_hba, hba_group);
29652eafd729SChristoph Hellwig }
29662eafd729SChristoph Hellwig 
29672eafd729SChristoph Hellwig static ssize_t target_hba_info_show(struct config_item *item, char *page)
29682eafd729SChristoph Hellwig {
29692eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
29702eafd729SChristoph Hellwig 
2971c66ac9dbSNicholas Bellinger 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
29720a06d430SChristoph Hellwig 			hba->hba_id, hba->backend->ops->name,
2973ce8dd25dSChristoph Hellwig 			TARGET_CORE_VERSION);
2974c66ac9dbSNicholas Bellinger }
2975c66ac9dbSNicholas Bellinger 
29762eafd729SChristoph Hellwig static ssize_t target_hba_mode_show(struct config_item *item, char *page)
2977c66ac9dbSNicholas Bellinger {
29782eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
2979c66ac9dbSNicholas Bellinger 	int hba_mode = 0;
2980c66ac9dbSNicholas Bellinger 
2981c66ac9dbSNicholas Bellinger 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2982c66ac9dbSNicholas Bellinger 		hba_mode = 1;
2983c66ac9dbSNicholas Bellinger 
2984c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", hba_mode);
2985c66ac9dbSNicholas Bellinger }
2986c66ac9dbSNicholas Bellinger 
29872eafd729SChristoph Hellwig static ssize_t target_hba_mode_store(struct config_item *item,
2988c66ac9dbSNicholas Bellinger 		const char *page, size_t count)
2989c66ac9dbSNicholas Bellinger {
29902eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
2991c66ac9dbSNicholas Bellinger 	unsigned long mode_flag;
2992c66ac9dbSNicholas Bellinger 	int ret;
2993c66ac9dbSNicholas Bellinger 
29940a06d430SChristoph Hellwig 	if (hba->backend->ops->pmode_enable_hba == NULL)
2995c66ac9dbSNicholas Bellinger 		return -EINVAL;
2996c66ac9dbSNicholas Bellinger 
299757103d7fSJingoo Han 	ret = kstrtoul(page, 0, &mode_flag);
2998c66ac9dbSNicholas Bellinger 	if (ret < 0) {
29996708bb27SAndy Grover 		pr_err("Unable to extract hba mode flag: %d\n", ret);
300057103d7fSJingoo Han 		return ret;
3001c66ac9dbSNicholas Bellinger 	}
3002c66ac9dbSNicholas Bellinger 
30030fd97ccfSChristoph Hellwig 	if (hba->dev_count) {
30046708bb27SAndy Grover 		pr_err("Unable to set hba_mode with active devices\n");
3005c66ac9dbSNicholas Bellinger 		return -EINVAL;
3006c66ac9dbSNicholas Bellinger 	}
3007c66ac9dbSNicholas Bellinger 
30080a06d430SChristoph Hellwig 	ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3009c66ac9dbSNicholas Bellinger 	if (ret < 0)
3010c66ac9dbSNicholas Bellinger 		return -EINVAL;
3011c66ac9dbSNicholas Bellinger 	if (ret > 0)
3012c66ac9dbSNicholas Bellinger 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3013c66ac9dbSNicholas Bellinger 	else if (ret == 0)
3014c66ac9dbSNicholas Bellinger 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3015c66ac9dbSNicholas Bellinger 
3016c66ac9dbSNicholas Bellinger 	return count;
3017c66ac9dbSNicholas Bellinger }
3018c66ac9dbSNicholas Bellinger 
30192eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_, hba_info);
30202eafd729SChristoph Hellwig CONFIGFS_ATTR(target_, hba_mode);
3021c66ac9dbSNicholas Bellinger 
30221f6fe7cbSNicholas Bellinger static void target_core_hba_release(struct config_item *item)
30231f6fe7cbSNicholas Bellinger {
30241f6fe7cbSNicholas Bellinger 	struct se_hba *hba = container_of(to_config_group(item),
30251f6fe7cbSNicholas Bellinger 				struct se_hba, hba_group);
30261f6fe7cbSNicholas Bellinger 	core_delete_hba(hba);
30271f6fe7cbSNicholas Bellinger }
30281f6fe7cbSNicholas Bellinger 
3029c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_hba_attrs[] = {
30302eafd729SChristoph Hellwig 	&target_attr_hba_info,
30312eafd729SChristoph Hellwig 	&target_attr_hba_mode,
3032c66ac9dbSNicholas Bellinger 	NULL,
3033c66ac9dbSNicholas Bellinger };
3034c66ac9dbSNicholas Bellinger 
3035c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_hba_item_ops = {
30361f6fe7cbSNicholas Bellinger 	.release		= target_core_hba_release,
3037c66ac9dbSNicholas Bellinger };
3038c66ac9dbSNicholas Bellinger 
3039c66ac9dbSNicholas Bellinger static struct config_item_type target_core_hba_cit = {
3040c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_hba_item_ops,
3041c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_hba_group_ops,
3042c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_hba_attrs,
3043c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3044c66ac9dbSNicholas Bellinger };
3045c66ac9dbSNicholas Bellinger 
3046c66ac9dbSNicholas Bellinger static struct config_group *target_core_call_addhbatotarget(
3047c66ac9dbSNicholas Bellinger 	struct config_group *group,
3048c66ac9dbSNicholas Bellinger 	const char *name)
3049c66ac9dbSNicholas Bellinger {
3050c66ac9dbSNicholas Bellinger 	char *se_plugin_str, *str, *str2;
3051c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
3052c66ac9dbSNicholas Bellinger 	char buf[TARGET_CORE_NAME_MAX_LEN];
3053c66ac9dbSNicholas Bellinger 	unsigned long plugin_dep_id = 0;
3054c66ac9dbSNicholas Bellinger 	int ret;
3055c66ac9dbSNicholas Bellinger 
3056c66ac9dbSNicholas Bellinger 	memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
305760d645a4SDan Carpenter 	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
30586708bb27SAndy Grover 		pr_err("Passed *name strlen(): %d exceeds"
3059c66ac9dbSNicholas Bellinger 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3060c66ac9dbSNicholas Bellinger 			TARGET_CORE_NAME_MAX_LEN);
3061c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENAMETOOLONG);
3062c66ac9dbSNicholas Bellinger 	}
3063c66ac9dbSNicholas Bellinger 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3064c66ac9dbSNicholas Bellinger 
3065c66ac9dbSNicholas Bellinger 	str = strstr(buf, "_");
30666708bb27SAndy Grover 	if (!str) {
30676708bb27SAndy Grover 		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3068c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
3069c66ac9dbSNicholas Bellinger 	}
3070c66ac9dbSNicholas Bellinger 	se_plugin_str = buf;
3071c66ac9dbSNicholas Bellinger 	/*
3072c66ac9dbSNicholas Bellinger 	 * Special case for subsystem plugins that have "_" in their names.
3073c66ac9dbSNicholas Bellinger 	 * Namely rd_direct and rd_mcp..
3074c66ac9dbSNicholas Bellinger 	 */
3075c66ac9dbSNicholas Bellinger 	str2 = strstr(str+1, "_");
30766708bb27SAndy Grover 	if (str2) {
3077c66ac9dbSNicholas Bellinger 		*str2 = '\0'; /* Terminate for *se_plugin_str */
3078c66ac9dbSNicholas Bellinger 		str2++; /* Skip to start of plugin dependent ID */
3079c66ac9dbSNicholas Bellinger 		str = str2;
3080c66ac9dbSNicholas Bellinger 	} else {
3081c66ac9dbSNicholas Bellinger 		*str = '\0'; /* Terminate for *se_plugin_str */
3082c66ac9dbSNicholas Bellinger 		str++; /* Skip to start of plugin dependent ID */
3083c66ac9dbSNicholas Bellinger 	}
3084c66ac9dbSNicholas Bellinger 
308557103d7fSJingoo Han 	ret = kstrtoul(str, 0, &plugin_dep_id);
3086c66ac9dbSNicholas Bellinger 	if (ret < 0) {
308757103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
3088c66ac9dbSNicholas Bellinger 				" plugin_dep_id\n", ret);
308957103d7fSJingoo Han 		return ERR_PTR(ret);
3090c66ac9dbSNicholas Bellinger 	}
3091c66ac9dbSNicholas Bellinger 	/*
3092c66ac9dbSNicholas Bellinger 	 * Load up TCM subsystem plugins if they have not already been loaded.
3093c66ac9dbSNicholas Bellinger 	 */
3094dbc5623eSNicholas Bellinger 	transport_subsystem_check_init();
3095c66ac9dbSNicholas Bellinger 
3096c66ac9dbSNicholas Bellinger 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3097c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
3098c66ac9dbSNicholas Bellinger 		return ERR_CAST(hba);
3099c66ac9dbSNicholas Bellinger 
3100c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&hba->hba_group, name,
3101c66ac9dbSNicholas Bellinger 			&target_core_hba_cit);
3102c66ac9dbSNicholas Bellinger 
3103c66ac9dbSNicholas Bellinger 	return &hba->hba_group;
3104c66ac9dbSNicholas Bellinger }
3105c66ac9dbSNicholas Bellinger 
3106c66ac9dbSNicholas Bellinger static void target_core_call_delhbafromtarget(
3107c66ac9dbSNicholas Bellinger 	struct config_group *group,
3108c66ac9dbSNicholas Bellinger 	struct config_item *item)
3109c66ac9dbSNicholas Bellinger {
31101f6fe7cbSNicholas Bellinger 	/*
31111f6fe7cbSNicholas Bellinger 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
31121f6fe7cbSNicholas Bellinger 	 * -> target_core_hba_release()
31131f6fe7cbSNicholas Bellinger 	 */
3114c66ac9dbSNicholas Bellinger 	config_item_put(item);
3115c66ac9dbSNicholas Bellinger }
3116c66ac9dbSNicholas Bellinger 
3117c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_group_ops = {
3118c66ac9dbSNicholas Bellinger 	.make_group	= target_core_call_addhbatotarget,
3119c66ac9dbSNicholas Bellinger 	.drop_item	= target_core_call_delhbafromtarget,
3120c66ac9dbSNicholas Bellinger };
3121c66ac9dbSNicholas Bellinger 
3122c66ac9dbSNicholas Bellinger static struct config_item_type target_core_cit = {
3123c66ac9dbSNicholas Bellinger 	.ct_item_ops	= NULL,
3124c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_group_ops,
3125c66ac9dbSNicholas Bellinger 	.ct_attrs	= NULL,
3126c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
3127c66ac9dbSNicholas Bellinger };
3128c66ac9dbSNicholas Bellinger 
3129c66ac9dbSNicholas Bellinger /* Stop functions for struct config_item_type target_core_hba_cit */
3130c66ac9dbSNicholas Bellinger 
31310a06d430SChristoph Hellwig void target_setup_backend_cits(struct target_backend *tb)
313273112edcSNicholas Bellinger {
31330a06d430SChristoph Hellwig 	target_core_setup_dev_cit(tb);
31340a06d430SChristoph Hellwig 	target_core_setup_dev_attrib_cit(tb);
31350a06d430SChristoph Hellwig 	target_core_setup_dev_pr_cit(tb);
31360a06d430SChristoph Hellwig 	target_core_setup_dev_wwn_cit(tb);
31370a06d430SChristoph Hellwig 	target_core_setup_dev_alua_tg_pt_gps_cit(tb);
31380a06d430SChristoph Hellwig 	target_core_setup_dev_stat_cit(tb);
313973112edcSNicholas Bellinger }
314073112edcSNicholas Bellinger 
314154550fabSAxel Lin static int __init target_core_init_configfs(void)
3142c66ac9dbSNicholas Bellinger {
3143c66ac9dbSNicholas Bellinger 	struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3144c66ac9dbSNicholas Bellinger 	struct config_group *lu_gp_cg = NULL;
3145d588cf8fSChristoph Hellwig 	struct configfs_subsystem *subsys = &target_core_fabrics;
3146c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
3147c66ac9dbSNicholas Bellinger 	int ret;
3148c66ac9dbSNicholas Bellinger 
31496708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3150c66ac9dbSNicholas Bellinger 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
3151c66ac9dbSNicholas Bellinger 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3152c66ac9dbSNicholas Bellinger 
3153c66ac9dbSNicholas Bellinger 	config_group_init(&subsys->su_group);
3154c66ac9dbSNicholas Bellinger 	mutex_init(&subsys->su_mutex);
3155c66ac9dbSNicholas Bellinger 
3156e3d6f909SAndy Grover 	ret = init_se_kmem_caches();
3157c66ac9dbSNicholas Bellinger 	if (ret < 0)
3158e3d6f909SAndy Grover 		return ret;
3159c66ac9dbSNicholas Bellinger 	/*
3160c66ac9dbSNicholas Bellinger 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3161c66ac9dbSNicholas Bellinger 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3162c66ac9dbSNicholas Bellinger 	 */
3163c66ac9dbSNicholas Bellinger 	target_cg = &subsys->su_group;
3164ab6dae82SAndy Grover 	target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3165c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
31666708bb27SAndy Grover 	if (!target_cg->default_groups) {
31676708bb27SAndy Grover 		pr_err("Unable to allocate target_cg->default_groups\n");
316837bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3169c66ac9dbSNicholas Bellinger 		goto out_global;
3170c66ac9dbSNicholas Bellinger 	}
3171c66ac9dbSNicholas Bellinger 
3172e3d6f909SAndy Grover 	config_group_init_type_name(&target_core_hbagroup,
3173c66ac9dbSNicholas Bellinger 			"core", &target_core_cit);
3174e3d6f909SAndy Grover 	target_cg->default_groups[0] = &target_core_hbagroup;
3175c66ac9dbSNicholas Bellinger 	target_cg->default_groups[1] = NULL;
3176c66ac9dbSNicholas Bellinger 	/*
3177c66ac9dbSNicholas Bellinger 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3178c66ac9dbSNicholas Bellinger 	 */
3179e3d6f909SAndy Grover 	hba_cg = &target_core_hbagroup;
318013f6a914SSebastian Andrzej Siewior 	hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3181c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
31826708bb27SAndy Grover 	if (!hba_cg->default_groups) {
31836708bb27SAndy Grover 		pr_err("Unable to allocate hba_cg->default_groups\n");
318437bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3185c66ac9dbSNicholas Bellinger 		goto out_global;
3186c66ac9dbSNicholas Bellinger 	}
3187e3d6f909SAndy Grover 	config_group_init_type_name(&alua_group,
3188c66ac9dbSNicholas Bellinger 			"alua", &target_core_alua_cit);
3189e3d6f909SAndy Grover 	hba_cg->default_groups[0] = &alua_group;
3190c66ac9dbSNicholas Bellinger 	hba_cg->default_groups[1] = NULL;
3191c66ac9dbSNicholas Bellinger 	/*
3192c66ac9dbSNicholas Bellinger 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3193c66ac9dbSNicholas Bellinger 	 * groups under /sys/kernel/config/target/core/alua/
3194c66ac9dbSNicholas Bellinger 	 */
3195e3d6f909SAndy Grover 	alua_cg = &alua_group;
319613f6a914SSebastian Andrzej Siewior 	alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3197c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
31986708bb27SAndy Grover 	if (!alua_cg->default_groups) {
31996708bb27SAndy Grover 		pr_err("Unable to allocate alua_cg->default_groups\n");
320037bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3201c66ac9dbSNicholas Bellinger 		goto out_global;
3202c66ac9dbSNicholas Bellinger 	}
3203c66ac9dbSNicholas Bellinger 
3204e3d6f909SAndy Grover 	config_group_init_type_name(&alua_lu_gps_group,
3205c66ac9dbSNicholas Bellinger 			"lu_gps", &target_core_alua_lu_gps_cit);
3206e3d6f909SAndy Grover 	alua_cg->default_groups[0] = &alua_lu_gps_group;
3207c66ac9dbSNicholas Bellinger 	alua_cg->default_groups[1] = NULL;
3208c66ac9dbSNicholas Bellinger 	/*
3209c66ac9dbSNicholas Bellinger 	 * Add core/alua/lu_gps/default_lu_gp
3210c66ac9dbSNicholas Bellinger 	 */
3211c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
321237bb7899SPeter Senna Tschudin 	if (IS_ERR(lu_gp)) {
321337bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3214c66ac9dbSNicholas Bellinger 		goto out_global;
321537bb7899SPeter Senna Tschudin 	}
3216c66ac9dbSNicholas Bellinger 
3217e3d6f909SAndy Grover 	lu_gp_cg = &alua_lu_gps_group;
321813f6a914SSebastian Andrzej Siewior 	lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3219c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
32206708bb27SAndy Grover 	if (!lu_gp_cg->default_groups) {
32216708bb27SAndy Grover 		pr_err("Unable to allocate lu_gp_cg->default_groups\n");
322237bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3223c66ac9dbSNicholas Bellinger 		goto out_global;
3224c66ac9dbSNicholas Bellinger 	}
3225c66ac9dbSNicholas Bellinger 
3226c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3227c66ac9dbSNicholas Bellinger 				&target_core_alua_lu_gp_cit);
3228c66ac9dbSNicholas Bellinger 	lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3229c66ac9dbSNicholas Bellinger 	lu_gp_cg->default_groups[1] = NULL;
3230e3d6f909SAndy Grover 	default_lu_gp = lu_gp;
3231c66ac9dbSNicholas Bellinger 	/*
3232c66ac9dbSNicholas Bellinger 	 * Register the target_core_mod subsystem with configfs.
3233c66ac9dbSNicholas Bellinger 	 */
3234c66ac9dbSNicholas Bellinger 	ret = configfs_register_subsystem(subsys);
3235c66ac9dbSNicholas Bellinger 	if (ret < 0) {
32366708bb27SAndy Grover 		pr_err("Error %d while registering subsystem %s\n",
3237c66ac9dbSNicholas Bellinger 			ret, subsys->su_group.cg_item.ci_namebuf);
3238c66ac9dbSNicholas Bellinger 		goto out_global;
3239c66ac9dbSNicholas Bellinger 	}
32406708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3241ce8dd25dSChristoph Hellwig 		" Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3242c66ac9dbSNicholas Bellinger 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3243c66ac9dbSNicholas Bellinger 	/*
3244c66ac9dbSNicholas Bellinger 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3245c66ac9dbSNicholas Bellinger 	 */
3246c66ac9dbSNicholas Bellinger 	ret = rd_module_init();
3247c66ac9dbSNicholas Bellinger 	if (ret < 0)
3248c66ac9dbSNicholas Bellinger 		goto out;
3249c66ac9dbSNicholas Bellinger 
32500d0f9dfbSRoland Dreier 	ret = core_dev_setup_virtual_lun0();
32510d0f9dfbSRoland Dreier 	if (ret < 0)
3252c66ac9dbSNicholas Bellinger 		goto out;
3253c66ac9dbSNicholas Bellinger 
3254f99715acSNicholas Bellinger 	ret = target_xcopy_setup_pt();
3255f99715acSNicholas Bellinger 	if (ret < 0)
3256f99715acSNicholas Bellinger 		goto out;
3257f99715acSNicholas Bellinger 
3258c66ac9dbSNicholas Bellinger 	return 0;
3259c66ac9dbSNicholas Bellinger 
3260c66ac9dbSNicholas Bellinger out:
3261c66ac9dbSNicholas Bellinger 	configfs_unregister_subsystem(subsys);
3262c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3263c66ac9dbSNicholas Bellinger 	rd_module_exit();
3264c66ac9dbSNicholas Bellinger out_global:
3265e3d6f909SAndy Grover 	if (default_lu_gp) {
3266e3d6f909SAndy Grover 		core_alua_free_lu_gp(default_lu_gp);
3267e3d6f909SAndy Grover 		default_lu_gp = NULL;
3268c66ac9dbSNicholas Bellinger 	}
3269c66ac9dbSNicholas Bellinger 	if (lu_gp_cg)
3270c66ac9dbSNicholas Bellinger 		kfree(lu_gp_cg->default_groups);
3271c66ac9dbSNicholas Bellinger 	if (alua_cg)
3272c66ac9dbSNicholas Bellinger 		kfree(alua_cg->default_groups);
3273c66ac9dbSNicholas Bellinger 	if (hba_cg)
3274c66ac9dbSNicholas Bellinger 		kfree(hba_cg->default_groups);
3275c66ac9dbSNicholas Bellinger 	kfree(target_cg->default_groups);
3276e3d6f909SAndy Grover 	release_se_kmem_caches();
3277e3d6f909SAndy Grover 	return ret;
3278c66ac9dbSNicholas Bellinger }
3279c66ac9dbSNicholas Bellinger 
328054550fabSAxel Lin static void __exit target_core_exit_configfs(void)
3281c66ac9dbSNicholas Bellinger {
3282c66ac9dbSNicholas Bellinger 	struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3283c66ac9dbSNicholas Bellinger 	struct config_item *item;
3284c66ac9dbSNicholas Bellinger 	int i;
3285c66ac9dbSNicholas Bellinger 
3286e3d6f909SAndy Grover 	lu_gp_cg = &alua_lu_gps_group;
3287c66ac9dbSNicholas Bellinger 	for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3288c66ac9dbSNicholas Bellinger 		item = &lu_gp_cg->default_groups[i]->cg_item;
3289c66ac9dbSNicholas Bellinger 		lu_gp_cg->default_groups[i] = NULL;
3290c66ac9dbSNicholas Bellinger 		config_item_put(item);
3291c66ac9dbSNicholas Bellinger 	}
3292c66ac9dbSNicholas Bellinger 	kfree(lu_gp_cg->default_groups);
32937c2bf6e9SNicholas Bellinger 	lu_gp_cg->default_groups = NULL;
3294c66ac9dbSNicholas Bellinger 
3295e3d6f909SAndy Grover 	alua_cg = &alua_group;
3296c66ac9dbSNicholas Bellinger 	for (i = 0; alua_cg->default_groups[i]; i++) {
3297c66ac9dbSNicholas Bellinger 		item = &alua_cg->default_groups[i]->cg_item;
3298c66ac9dbSNicholas Bellinger 		alua_cg->default_groups[i] = NULL;
3299c66ac9dbSNicholas Bellinger 		config_item_put(item);
3300c66ac9dbSNicholas Bellinger 	}
3301c66ac9dbSNicholas Bellinger 	kfree(alua_cg->default_groups);
33027c2bf6e9SNicholas Bellinger 	alua_cg->default_groups = NULL;
3303c66ac9dbSNicholas Bellinger 
3304e3d6f909SAndy Grover 	hba_cg = &target_core_hbagroup;
3305c66ac9dbSNicholas Bellinger 	for (i = 0; hba_cg->default_groups[i]; i++) {
3306c66ac9dbSNicholas Bellinger 		item = &hba_cg->default_groups[i]->cg_item;
3307c66ac9dbSNicholas Bellinger 		hba_cg->default_groups[i] = NULL;
3308c66ac9dbSNicholas Bellinger 		config_item_put(item);
3309c66ac9dbSNicholas Bellinger 	}
3310c66ac9dbSNicholas Bellinger 	kfree(hba_cg->default_groups);
33117c2bf6e9SNicholas Bellinger 	hba_cg->default_groups = NULL;
33127c2bf6e9SNicholas Bellinger 	/*
33137c2bf6e9SNicholas Bellinger 	 * We expect subsys->su_group.default_groups to be released
33147c2bf6e9SNicholas Bellinger 	 * by configfs subsystem provider logic..
33157c2bf6e9SNicholas Bellinger 	 */
3316d588cf8fSChristoph Hellwig 	configfs_unregister_subsystem(&target_core_fabrics);
3317d588cf8fSChristoph Hellwig 	kfree(target_core_fabrics.su_group.default_groups);
3318c66ac9dbSNicholas Bellinger 
3319e3d6f909SAndy Grover 	core_alua_free_lu_gp(default_lu_gp);
3320e3d6f909SAndy Grover 	default_lu_gp = NULL;
33217c2bf6e9SNicholas Bellinger 
33226708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3323c66ac9dbSNicholas Bellinger 			" Infrastructure\n");
3324c66ac9dbSNicholas Bellinger 
3325c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3326c66ac9dbSNicholas Bellinger 	rd_module_exit();
3327f99715acSNicholas Bellinger 	target_xcopy_release_pt();
3328e3d6f909SAndy Grover 	release_se_kmem_caches();
3329c66ac9dbSNicholas Bellinger }
3330c66ac9dbSNicholas Bellinger 
3331c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3332c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3333c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
3334c66ac9dbSNicholas Bellinger 
3335c66ac9dbSNicholas Bellinger module_init(target_core_init_configfs);
3336c66ac9dbSNicholas Bellinger module_exit(target_core_exit_configfs);
3337