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 #include <target/target_core_fabric_configfs.h>
44c66ac9dbSNicholas Bellinger #include <target/target_core_configfs.h>
45c66ac9dbSNicholas Bellinger #include <target/configfs_macros.h>
46c66ac9dbSNicholas Bellinger 
47e26d99aeSChristoph Hellwig #include "target_core_internal.h"
48c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
49c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
50c66ac9dbSNicholas Bellinger #include "target_core_rd.h"
51f99715acSNicholas Bellinger #include "target_core_xcopy.h"
52c66ac9dbSNicholas Bellinger 
5373112edcSNicholas Bellinger #define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs)		\
5473112edcSNicholas Bellinger static void target_core_setup_##_name##_cit(struct se_subsystem_api *sa) \
5573112edcSNicholas Bellinger {									\
5673112edcSNicholas Bellinger 	struct target_backend_cits *tbc = &sa->tb_cits;			\
5773112edcSNicholas Bellinger 	struct config_item_type *cit = &tbc->tb_##_name##_cit;		\
5873112edcSNicholas Bellinger 									\
5973112edcSNicholas Bellinger 	cit->ct_item_ops = _item_ops;					\
6073112edcSNicholas Bellinger 	cit->ct_group_ops = _group_ops;					\
6173112edcSNicholas Bellinger 	cit->ct_attrs = _attrs;						\
6273112edcSNicholas Bellinger 	cit->ct_owner = sa->owner;					\
6373112edcSNicholas Bellinger 	pr_debug("Setup generic %s\n", __stringify(_name));		\
6473112edcSNicholas Bellinger }
6573112edcSNicholas Bellinger 
66e3d6f909SAndy Grover extern struct t10_alua_lu_gp *default_lu_gp;
67e3d6f909SAndy Grover 
68d0f474e5SRoland Dreier static LIST_HEAD(g_tf_list);
69d0f474e5SRoland Dreier static DEFINE_MUTEX(g_tf_lock);
70c66ac9dbSNicholas Bellinger 
71c66ac9dbSNicholas Bellinger struct target_core_configfs_attribute {
72c66ac9dbSNicholas Bellinger 	struct configfs_attribute attr;
73c66ac9dbSNicholas Bellinger 	ssize_t (*show)(void *, char *);
74c66ac9dbSNicholas Bellinger 	ssize_t (*store)(void *, const char *, size_t);
75c66ac9dbSNicholas Bellinger };
76c66ac9dbSNicholas Bellinger 
77e3d6f909SAndy Grover static struct config_group target_core_hbagroup;
78e3d6f909SAndy Grover static struct config_group alua_group;
79e3d6f909SAndy Grover static struct config_group alua_lu_gps_group;
80e3d6f909SAndy Grover 
81c66ac9dbSNicholas Bellinger static inline struct se_hba *
82c66ac9dbSNicholas Bellinger item_to_hba(struct config_item *item)
83c66ac9dbSNicholas Bellinger {
84c66ac9dbSNicholas Bellinger 	return container_of(to_config_group(item), struct se_hba, hba_group);
85c66ac9dbSNicholas Bellinger }
86c66ac9dbSNicholas Bellinger 
87c66ac9dbSNicholas Bellinger /*
88c66ac9dbSNicholas Bellinger  * Attributes for /sys/kernel/config/target/
89c66ac9dbSNicholas Bellinger  */
90c66ac9dbSNicholas Bellinger static ssize_t target_core_attr_show(struct config_item *item,
91c66ac9dbSNicholas Bellinger 				      struct configfs_attribute *attr,
92c66ac9dbSNicholas Bellinger 				      char *page)
93c66ac9dbSNicholas Bellinger {
94c66ac9dbSNicholas Bellinger 	return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
95c66ac9dbSNicholas Bellinger 		" on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
96c66ac9dbSNicholas Bellinger 		utsname()->sysname, utsname()->machine);
97c66ac9dbSNicholas Bellinger }
98c66ac9dbSNicholas Bellinger 
99c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_fabric_item_ops = {
100c66ac9dbSNicholas Bellinger 	.show_attribute = target_core_attr_show,
101c66ac9dbSNicholas Bellinger };
102c66ac9dbSNicholas Bellinger 
103c66ac9dbSNicholas Bellinger static struct configfs_attribute target_core_item_attr_version = {
104c66ac9dbSNicholas Bellinger 	.ca_owner	= THIS_MODULE,
105c66ac9dbSNicholas Bellinger 	.ca_name	= "version",
106c66ac9dbSNicholas Bellinger 	.ca_mode	= S_IRUGO,
107c66ac9dbSNicholas Bellinger };
108c66ac9dbSNicholas Bellinger 
109c66ac9dbSNicholas Bellinger static struct target_fabric_configfs *target_core_get_fabric(
110c66ac9dbSNicholas Bellinger 	const char *name)
111c66ac9dbSNicholas Bellinger {
112c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
113c66ac9dbSNicholas Bellinger 
1146708bb27SAndy Grover 	if (!name)
115c66ac9dbSNicholas Bellinger 		return NULL;
116c66ac9dbSNicholas Bellinger 
117c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
118c66ac9dbSNicholas Bellinger 	list_for_each_entry(tf, &g_tf_list, tf_list) {
1196708bb27SAndy Grover 		if (!strcmp(tf->tf_name, name)) {
120c66ac9dbSNicholas Bellinger 			atomic_inc(&tf->tf_access_cnt);
121c66ac9dbSNicholas Bellinger 			mutex_unlock(&g_tf_lock);
122c66ac9dbSNicholas Bellinger 			return tf;
123c66ac9dbSNicholas Bellinger 		}
124c66ac9dbSNicholas Bellinger 	}
125c66ac9dbSNicholas Bellinger 	mutex_unlock(&g_tf_lock);
126c66ac9dbSNicholas Bellinger 
127c66ac9dbSNicholas Bellinger 	return NULL;
128c66ac9dbSNicholas Bellinger }
129c66ac9dbSNicholas Bellinger 
130c66ac9dbSNicholas Bellinger /*
131c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->make_group()
132c66ac9dbSNicholas Bellinger  */
133c66ac9dbSNicholas Bellinger static struct config_group *target_core_register_fabric(
134c66ac9dbSNicholas Bellinger 	struct config_group *group,
135c66ac9dbSNicholas Bellinger 	const char *name)
136c66ac9dbSNicholas Bellinger {
137c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
138c66ac9dbSNicholas Bellinger 	int ret;
139c66ac9dbSNicholas Bellinger 
1406708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
141c66ac9dbSNicholas Bellinger 			" %s\n", group, name);
142e7b7af6eSRoland Dreier 
143e7b7af6eSRoland Dreier 	tf = target_core_get_fabric(name);
144e7b7af6eSRoland Dreier 	if (!tf) {
145e7b7af6eSRoland Dreier 		pr_err("target_core_register_fabric() trying autoload for %s\n",
146e7b7af6eSRoland Dreier 			name);
147e7b7af6eSRoland Dreier 
148c66ac9dbSNicholas Bellinger 		/*
149c66ac9dbSNicholas Bellinger 		 * Below are some hardcoded request_module() calls to automatically
150c66ac9dbSNicholas Bellinger 		 * local fabric modules when the following is called:
151c66ac9dbSNicholas Bellinger 		 *
152c66ac9dbSNicholas Bellinger 		 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
153c66ac9dbSNicholas Bellinger 		 *
154c66ac9dbSNicholas Bellinger 		 * Note that this does not limit which TCM fabric module can be
155c66ac9dbSNicholas Bellinger 		 * registered, but simply provids auto loading logic for modules with
156c66ac9dbSNicholas Bellinger 		 * mkdir(2) system calls with known TCM fabric modules.
157c66ac9dbSNicholas Bellinger 		 */
158e7b7af6eSRoland Dreier 
1596708bb27SAndy Grover 		if (!strncmp(name, "iscsi", 5)) {
160c66ac9dbSNicholas Bellinger 			/*
161c66ac9dbSNicholas Bellinger 			 * Automatically load the LIO Target fabric module when the
162c66ac9dbSNicholas Bellinger 			 * following is called:
163c66ac9dbSNicholas Bellinger 			 *
164c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/iscsi
165c66ac9dbSNicholas Bellinger 			 */
166c66ac9dbSNicholas Bellinger 			ret = request_module("iscsi_target_mod");
167c66ac9dbSNicholas Bellinger 			if (ret < 0) {
1686708bb27SAndy Grover 				pr_err("request_module() failed for"
169c66ac9dbSNicholas Bellinger 				       " iscsi_target_mod.ko: %d\n", ret);
170c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
171c66ac9dbSNicholas Bellinger 			}
1726708bb27SAndy Grover 		} else if (!strncmp(name, "loopback", 8)) {
173c66ac9dbSNicholas Bellinger 			/*
174c66ac9dbSNicholas Bellinger 			 * Automatically load the tcm_loop fabric module when the
175c66ac9dbSNicholas Bellinger 			 * following is called:
176c66ac9dbSNicholas Bellinger 			 *
177c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/loopback
178c66ac9dbSNicholas Bellinger 			 */
179c66ac9dbSNicholas Bellinger 			ret = request_module("tcm_loop");
180c66ac9dbSNicholas Bellinger 			if (ret < 0) {
1816708bb27SAndy Grover 				pr_err("request_module() failed for"
182c66ac9dbSNicholas Bellinger 				       " tcm_loop.ko: %d\n", ret);
183c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
184c66ac9dbSNicholas Bellinger 			}
185c66ac9dbSNicholas Bellinger 		}
186c66ac9dbSNicholas Bellinger 
187c66ac9dbSNicholas Bellinger 		tf = target_core_get_fabric(name);
188e7b7af6eSRoland Dreier 	}
189e7b7af6eSRoland Dreier 
1906708bb27SAndy Grover 	if (!tf) {
1916708bb27SAndy Grover 		pr_err("target_core_get_fabric() failed for %s\n",
192c66ac9dbSNicholas Bellinger 		       name);
193c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
194c66ac9dbSNicholas Bellinger 	}
1956708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
196c66ac9dbSNicholas Bellinger 			" %s\n", tf->tf_name);
197c66ac9dbSNicholas Bellinger 	/*
198c66ac9dbSNicholas Bellinger 	 * On a successful target_core_get_fabric() look, the returned
199c66ac9dbSNicholas Bellinger 	 * struct target_fabric_configfs *tf will contain a usage reference.
200c66ac9dbSNicholas Bellinger 	 */
2016708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
202d80e224dSAndy Grover 			&tf->tf_cit_tmpl.tfc_wwn_cit);
203c66ac9dbSNicholas Bellinger 
204c66ac9dbSNicholas Bellinger 	tf->tf_group.default_groups = tf->tf_default_groups;
205c66ac9dbSNicholas Bellinger 	tf->tf_group.default_groups[0] = &tf->tf_disc_group;
206c66ac9dbSNicholas Bellinger 	tf->tf_group.default_groups[1] = NULL;
207c66ac9dbSNicholas Bellinger 
208c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tf->tf_group, name,
209d80e224dSAndy Grover 			&tf->tf_cit_tmpl.tfc_wwn_cit);
210c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
211d80e224dSAndy Grover 			&tf->tf_cit_tmpl.tfc_discovery_cit);
212c66ac9dbSNicholas Bellinger 
2136708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
214c66ac9dbSNicholas Bellinger 			" %s\n", tf->tf_group.cg_item.ci_name);
215c66ac9dbSNicholas Bellinger 	/*
216c66ac9dbSNicholas Bellinger 	 * Setup tf_ops.tf_subsys pointer for usage with configfs_depend_item()
217c66ac9dbSNicholas Bellinger 	 */
218c66ac9dbSNicholas Bellinger 	tf->tf_ops.tf_subsys = tf->tf_subsys;
219c66ac9dbSNicholas Bellinger 	tf->tf_fabric = &tf->tf_group.cg_item;
2206708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
221c66ac9dbSNicholas Bellinger 			" for %s\n", name);
222c66ac9dbSNicholas Bellinger 
223c66ac9dbSNicholas Bellinger 	return &tf->tf_group;
224c66ac9dbSNicholas Bellinger }
225c66ac9dbSNicholas Bellinger 
226c66ac9dbSNicholas Bellinger /*
227c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->drop_item()
228c66ac9dbSNicholas Bellinger  */
229c66ac9dbSNicholas Bellinger static void target_core_deregister_fabric(
230c66ac9dbSNicholas Bellinger 	struct config_group *group,
231c66ac9dbSNicholas Bellinger 	struct config_item *item)
232c66ac9dbSNicholas Bellinger {
233c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf = container_of(
234c66ac9dbSNicholas Bellinger 		to_config_group(item), struct target_fabric_configfs, tf_group);
235c66ac9dbSNicholas Bellinger 	struct config_group *tf_group;
236c66ac9dbSNicholas Bellinger 	struct config_item *df_item;
237c66ac9dbSNicholas Bellinger 	int i;
238c66ac9dbSNicholas Bellinger 
2396708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
240c66ac9dbSNicholas Bellinger 		" tf list\n", config_item_name(item));
241c66ac9dbSNicholas Bellinger 
2426708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
243c66ac9dbSNicholas Bellinger 			" %s\n", tf->tf_name);
244c66ac9dbSNicholas Bellinger 	atomic_dec(&tf->tf_access_cnt);
245c66ac9dbSNicholas Bellinger 
2466708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
247c66ac9dbSNicholas Bellinger 			" tf->tf_fabric for %s\n", tf->tf_name);
248c66ac9dbSNicholas Bellinger 	tf->tf_fabric = NULL;
249c66ac9dbSNicholas Bellinger 
2506708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
251c66ac9dbSNicholas Bellinger 			" %s\n", config_item_name(item));
252c66ac9dbSNicholas Bellinger 
253c66ac9dbSNicholas Bellinger 	tf_group = &tf->tf_group;
254c66ac9dbSNicholas Bellinger 	for (i = 0; tf_group->default_groups[i]; i++) {
255c66ac9dbSNicholas Bellinger 		df_item = &tf_group->default_groups[i]->cg_item;
256c66ac9dbSNicholas Bellinger 		tf_group->default_groups[i] = NULL;
257c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
258c66ac9dbSNicholas Bellinger 	}
259c66ac9dbSNicholas Bellinger 	config_item_put(item);
260c66ac9dbSNicholas Bellinger }
261c66ac9dbSNicholas Bellinger 
262c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_fabric_group_ops = {
263c66ac9dbSNicholas Bellinger 	.make_group	= &target_core_register_fabric,
264c66ac9dbSNicholas Bellinger 	.drop_item	= &target_core_deregister_fabric,
265c66ac9dbSNicholas Bellinger };
266c66ac9dbSNicholas Bellinger 
267c66ac9dbSNicholas Bellinger /*
268c66ac9dbSNicholas Bellinger  * All item attributes appearing in /sys/kernel/target/ appear here.
269c66ac9dbSNicholas Bellinger  */
270c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_fabric_item_attrs[] = {
271c66ac9dbSNicholas Bellinger 	&target_core_item_attr_version,
272c66ac9dbSNicholas Bellinger 	NULL,
273c66ac9dbSNicholas Bellinger };
274c66ac9dbSNicholas Bellinger 
275c66ac9dbSNicholas Bellinger /*
276c66ac9dbSNicholas Bellinger  * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
277c66ac9dbSNicholas Bellinger  */
278c66ac9dbSNicholas Bellinger static struct config_item_type target_core_fabrics_item = {
279c66ac9dbSNicholas Bellinger 	.ct_item_ops	= &target_core_fabric_item_ops,
280c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_fabric_group_ops,
281c66ac9dbSNicholas Bellinger 	.ct_attrs	= target_core_fabric_item_attrs,
282c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
283c66ac9dbSNicholas Bellinger };
284c66ac9dbSNicholas Bellinger 
285c66ac9dbSNicholas Bellinger static struct configfs_subsystem target_core_fabrics = {
286c66ac9dbSNicholas Bellinger 	.su_group = {
287c66ac9dbSNicholas Bellinger 		.cg_item = {
288c66ac9dbSNicholas Bellinger 			.ci_namebuf = "target",
289c66ac9dbSNicholas Bellinger 			.ci_type = &target_core_fabrics_item,
290c66ac9dbSNicholas Bellinger 		},
291c66ac9dbSNicholas Bellinger 	},
292c66ac9dbSNicholas Bellinger };
293c66ac9dbSNicholas Bellinger 
29456d128faSNicholas Bellinger struct configfs_subsystem *target_core_subsystem[] = {
295c66ac9dbSNicholas Bellinger 	&target_core_fabrics,
296c66ac9dbSNicholas Bellinger 	NULL,
297c66ac9dbSNicholas Bellinger };
298c66ac9dbSNicholas Bellinger 
299c66ac9dbSNicholas Bellinger /*##############################################################################
300c66ac9dbSNicholas Bellinger // Start functions called by external Target Fabrics Modules
301c66ac9dbSNicholas Bellinger //############################################################################*/
302c66ac9dbSNicholas Bellinger 
303c66ac9dbSNicholas Bellinger /*
304c66ac9dbSNicholas Bellinger  * First function called by fabric modules to:
305c66ac9dbSNicholas Bellinger  *
306c66ac9dbSNicholas Bellinger  * 1) Allocate a struct target_fabric_configfs and save the *fabric_cit pointer.
307c66ac9dbSNicholas Bellinger  * 2) Add struct target_fabric_configfs to g_tf_list
308c66ac9dbSNicholas Bellinger  * 3) Return struct target_fabric_configfs to fabric module to be passed
309c66ac9dbSNicholas Bellinger  *    into target_fabric_configfs_register().
310c66ac9dbSNicholas Bellinger  */
311c66ac9dbSNicholas Bellinger struct target_fabric_configfs *target_fabric_configfs_init(
312c66ac9dbSNicholas Bellinger 	struct module *fabric_mod,
313c66ac9dbSNicholas Bellinger 	const char *name)
314c66ac9dbSNicholas Bellinger {
315c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
316c66ac9dbSNicholas Bellinger 
317c66ac9dbSNicholas Bellinger 	if (!(name)) {
3186708bb27SAndy Grover 		pr_err("Unable to locate passed fabric name\n");
319e3d6f909SAndy Grover 		return ERR_PTR(-EINVAL);
320c66ac9dbSNicholas Bellinger 	}
32160d645a4SDan Carpenter 	if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
3226708bb27SAndy Grover 		pr_err("Passed name: %s exceeds TARGET_FABRIC"
323c66ac9dbSNicholas Bellinger 			"_NAME_SIZE\n", name);
324e3d6f909SAndy Grover 		return ERR_PTR(-EINVAL);
325c66ac9dbSNicholas Bellinger 	}
326c66ac9dbSNicholas Bellinger 
327c66ac9dbSNicholas Bellinger 	tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
3286708bb27SAndy Grover 	if (!tf)
329e3d6f909SAndy Grover 		return ERR_PTR(-ENOMEM);
330c66ac9dbSNicholas Bellinger 
331c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&tf->tf_list);
332c66ac9dbSNicholas Bellinger 	atomic_set(&tf->tf_access_cnt, 0);
333c66ac9dbSNicholas Bellinger 	/*
334c66ac9dbSNicholas Bellinger 	 * Setup the default generic struct config_item_type's (cits) in
335c66ac9dbSNicholas Bellinger 	 * struct target_fabric_configfs->tf_cit_tmpl
336c66ac9dbSNicholas Bellinger 	 */
337c66ac9dbSNicholas Bellinger 	tf->tf_module = fabric_mod;
338c66ac9dbSNicholas Bellinger 	target_fabric_setup_cits(tf);
339c66ac9dbSNicholas Bellinger 
340c66ac9dbSNicholas Bellinger 	tf->tf_subsys = target_core_subsystem[0];
341c66ac9dbSNicholas Bellinger 	snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
342c66ac9dbSNicholas Bellinger 
343c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
344c66ac9dbSNicholas Bellinger 	list_add_tail(&tf->tf_list, &g_tf_list);
345c66ac9dbSNicholas Bellinger 	mutex_unlock(&g_tf_lock);
346c66ac9dbSNicholas Bellinger 
3476708bb27SAndy Grover 	pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
348c66ac9dbSNicholas Bellinger 			">>>>>>>>>>>>>>\n");
3496708bb27SAndy Grover 	pr_debug("Initialized struct target_fabric_configfs: %p for"
350c66ac9dbSNicholas Bellinger 			" %s\n", tf, tf->tf_name);
351c66ac9dbSNicholas Bellinger 	return tf;
352c66ac9dbSNicholas Bellinger }
353c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(target_fabric_configfs_init);
354c66ac9dbSNicholas Bellinger 
355c66ac9dbSNicholas Bellinger /*
356c66ac9dbSNicholas Bellinger  * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
357c66ac9dbSNicholas Bellinger  */
358c66ac9dbSNicholas Bellinger void target_fabric_configfs_free(
359c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf)
360c66ac9dbSNicholas Bellinger {
361c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
362c66ac9dbSNicholas Bellinger 	list_del(&tf->tf_list);
363c66ac9dbSNicholas Bellinger 	mutex_unlock(&g_tf_lock);
364c66ac9dbSNicholas Bellinger 
365c66ac9dbSNicholas Bellinger 	kfree(tf);
366c66ac9dbSNicholas Bellinger }
367c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(target_fabric_configfs_free);
368c66ac9dbSNicholas Bellinger 
369c66ac9dbSNicholas Bellinger /*
370c66ac9dbSNicholas Bellinger  * Perform a sanity check of the passed tf->tf_ops before completing
371c66ac9dbSNicholas Bellinger  * TCM fabric module registration.
372c66ac9dbSNicholas Bellinger  */
373c66ac9dbSNicholas Bellinger static int target_fabric_tf_ops_check(
374c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf)
375c66ac9dbSNicholas Bellinger {
376c66ac9dbSNicholas Bellinger 	struct target_core_fabric_ops *tfo = &tf->tf_ops;
377c66ac9dbSNicholas Bellinger 
3786708bb27SAndy Grover 	if (!tfo->get_fabric_name) {
3796708bb27SAndy Grover 		pr_err("Missing tfo->get_fabric_name()\n");
380c66ac9dbSNicholas Bellinger 		return -EINVAL;
381c66ac9dbSNicholas Bellinger 	}
3826708bb27SAndy Grover 	if (!tfo->get_fabric_proto_ident) {
3836708bb27SAndy Grover 		pr_err("Missing tfo->get_fabric_proto_ident()\n");
384c66ac9dbSNicholas Bellinger 		return -EINVAL;
385c66ac9dbSNicholas Bellinger 	}
3866708bb27SAndy Grover 	if (!tfo->tpg_get_wwn) {
3876708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_wwn()\n");
388c66ac9dbSNicholas Bellinger 		return -EINVAL;
389c66ac9dbSNicholas Bellinger 	}
3906708bb27SAndy Grover 	if (!tfo->tpg_get_tag) {
3916708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_tag()\n");
392c66ac9dbSNicholas Bellinger 		return -EINVAL;
393c66ac9dbSNicholas Bellinger 	}
3946708bb27SAndy Grover 	if (!tfo->tpg_get_default_depth) {
3956708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_default_depth()\n");
396c66ac9dbSNicholas Bellinger 		return -EINVAL;
397c66ac9dbSNicholas Bellinger 	}
3986708bb27SAndy Grover 	if (!tfo->tpg_get_pr_transport_id) {
3996708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
400c66ac9dbSNicholas Bellinger 		return -EINVAL;
401c66ac9dbSNicholas Bellinger 	}
4026708bb27SAndy Grover 	if (!tfo->tpg_get_pr_transport_id_len) {
4036708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
404c66ac9dbSNicholas Bellinger 		return -EINVAL;
405c66ac9dbSNicholas Bellinger 	}
4066708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode) {
4076708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode()\n");
408c66ac9dbSNicholas Bellinger 		return -EINVAL;
409c66ac9dbSNicholas Bellinger 	}
4106708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_cache) {
4116708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
412c66ac9dbSNicholas Bellinger 		return -EINVAL;
413c66ac9dbSNicholas Bellinger 	}
4146708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_write_protect) {
4156708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
416c66ac9dbSNicholas Bellinger 		return -EINVAL;
417c66ac9dbSNicholas Bellinger 	}
4186708bb27SAndy Grover 	if (!tfo->tpg_check_prod_mode_write_protect) {
4196708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
420c66ac9dbSNicholas Bellinger 		return -EINVAL;
421c66ac9dbSNicholas Bellinger 	}
4226708bb27SAndy Grover 	if (!tfo->tpg_alloc_fabric_acl) {
4236708bb27SAndy Grover 		pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
424c66ac9dbSNicholas Bellinger 		return -EINVAL;
425c66ac9dbSNicholas Bellinger 	}
4266708bb27SAndy Grover 	if (!tfo->tpg_release_fabric_acl) {
4276708bb27SAndy Grover 		pr_err("Missing tfo->tpg_release_fabric_acl()\n");
428c66ac9dbSNicholas Bellinger 		return -EINVAL;
429c66ac9dbSNicholas Bellinger 	}
4306708bb27SAndy Grover 	if (!tfo->tpg_get_inst_index) {
4316708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_inst_index()\n");
432c66ac9dbSNicholas Bellinger 		return -EINVAL;
433c66ac9dbSNicholas Bellinger 	}
43435462975SChristoph Hellwig 	if (!tfo->release_cmd) {
4356708bb27SAndy Grover 		pr_err("Missing tfo->release_cmd()\n");
436c66ac9dbSNicholas Bellinger 		return -EINVAL;
437c66ac9dbSNicholas Bellinger 	}
4386708bb27SAndy Grover 	if (!tfo->shutdown_session) {
4396708bb27SAndy Grover 		pr_err("Missing tfo->shutdown_session()\n");
440c66ac9dbSNicholas Bellinger 		return -EINVAL;
441c66ac9dbSNicholas Bellinger 	}
4426708bb27SAndy Grover 	if (!tfo->close_session) {
4436708bb27SAndy Grover 		pr_err("Missing tfo->close_session()\n");
444c66ac9dbSNicholas Bellinger 		return -EINVAL;
445c66ac9dbSNicholas Bellinger 	}
4466708bb27SAndy Grover 	if (!tfo->sess_get_index) {
4476708bb27SAndy Grover 		pr_err("Missing tfo->sess_get_index()\n");
448c66ac9dbSNicholas Bellinger 		return -EINVAL;
449c66ac9dbSNicholas Bellinger 	}
4506708bb27SAndy Grover 	if (!tfo->write_pending) {
4516708bb27SAndy Grover 		pr_err("Missing tfo->write_pending()\n");
452c66ac9dbSNicholas Bellinger 		return -EINVAL;
453c66ac9dbSNicholas Bellinger 	}
4546708bb27SAndy Grover 	if (!tfo->write_pending_status) {
4556708bb27SAndy Grover 		pr_err("Missing tfo->write_pending_status()\n");
456c66ac9dbSNicholas Bellinger 		return -EINVAL;
457c66ac9dbSNicholas Bellinger 	}
4586708bb27SAndy Grover 	if (!tfo->set_default_node_attributes) {
4596708bb27SAndy Grover 		pr_err("Missing tfo->set_default_node_attributes()\n");
460c66ac9dbSNicholas Bellinger 		return -EINVAL;
461c66ac9dbSNicholas Bellinger 	}
4626708bb27SAndy Grover 	if (!tfo->get_task_tag) {
4636708bb27SAndy Grover 		pr_err("Missing tfo->get_task_tag()\n");
464c66ac9dbSNicholas Bellinger 		return -EINVAL;
465c66ac9dbSNicholas Bellinger 	}
4666708bb27SAndy Grover 	if (!tfo->get_cmd_state) {
4676708bb27SAndy Grover 		pr_err("Missing tfo->get_cmd_state()\n");
468c66ac9dbSNicholas Bellinger 		return -EINVAL;
469c66ac9dbSNicholas Bellinger 	}
4706708bb27SAndy Grover 	if (!tfo->queue_data_in) {
4716708bb27SAndy Grover 		pr_err("Missing tfo->queue_data_in()\n");
472c66ac9dbSNicholas Bellinger 		return -EINVAL;
473c66ac9dbSNicholas Bellinger 	}
4746708bb27SAndy Grover 	if (!tfo->queue_status) {
4756708bb27SAndy Grover 		pr_err("Missing tfo->queue_status()\n");
476c66ac9dbSNicholas Bellinger 		return -EINVAL;
477c66ac9dbSNicholas Bellinger 	}
4786708bb27SAndy Grover 	if (!tfo->queue_tm_rsp) {
4796708bb27SAndy Grover 		pr_err("Missing tfo->queue_tm_rsp()\n");
480c66ac9dbSNicholas Bellinger 		return -EINVAL;
481c66ac9dbSNicholas Bellinger 	}
482131e6abcSNicholas Bellinger 	if (!tfo->aborted_task) {
483131e6abcSNicholas Bellinger 		pr_err("Missing tfo->aborted_task()\n");
484131e6abcSNicholas Bellinger 		return -EINVAL;
485131e6abcSNicholas Bellinger 	}
486c66ac9dbSNicholas Bellinger 	/*
487c66ac9dbSNicholas Bellinger 	 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
488c66ac9dbSNicholas Bellinger 	 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
489c66ac9dbSNicholas Bellinger 	 * target_core_fabric_configfs.c WWN+TPG group context code.
490c66ac9dbSNicholas Bellinger 	 */
4916708bb27SAndy Grover 	if (!tfo->fabric_make_wwn) {
4926708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_wwn()\n");
493c66ac9dbSNicholas Bellinger 		return -EINVAL;
494c66ac9dbSNicholas Bellinger 	}
4956708bb27SAndy Grover 	if (!tfo->fabric_drop_wwn) {
4966708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_wwn()\n");
497c66ac9dbSNicholas Bellinger 		return -EINVAL;
498c66ac9dbSNicholas Bellinger 	}
4996708bb27SAndy Grover 	if (!tfo->fabric_make_tpg) {
5006708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_tpg()\n");
501c66ac9dbSNicholas Bellinger 		return -EINVAL;
502c66ac9dbSNicholas Bellinger 	}
5036708bb27SAndy Grover 	if (!tfo->fabric_drop_tpg) {
5046708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_tpg()\n");
505c66ac9dbSNicholas Bellinger 		return -EINVAL;
506c66ac9dbSNicholas Bellinger 	}
507c66ac9dbSNicholas Bellinger 
508c66ac9dbSNicholas Bellinger 	return 0;
509c66ac9dbSNicholas Bellinger }
510c66ac9dbSNicholas Bellinger 
511c66ac9dbSNicholas Bellinger /*
512c66ac9dbSNicholas Bellinger  * Called 2nd from fabric module with returned parameter of
513c66ac9dbSNicholas Bellinger  * struct target_fabric_configfs * from target_fabric_configfs_init().
514c66ac9dbSNicholas Bellinger  *
515c66ac9dbSNicholas Bellinger  * Upon a successful registration, the new fabric's struct config_item is
516c66ac9dbSNicholas Bellinger  * return.  Also, a pointer to this struct is set in the passed
517c66ac9dbSNicholas Bellinger  * struct target_fabric_configfs.
518c66ac9dbSNicholas Bellinger  */
519c66ac9dbSNicholas Bellinger int target_fabric_configfs_register(
520c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf)
521c66ac9dbSNicholas Bellinger {
522c66ac9dbSNicholas Bellinger 	int ret;
523c66ac9dbSNicholas Bellinger 
5246708bb27SAndy Grover 	if (!tf) {
5256708bb27SAndy Grover 		pr_err("Unable to locate target_fabric_configfs"
526c66ac9dbSNicholas Bellinger 			" pointer\n");
527c66ac9dbSNicholas Bellinger 		return -EINVAL;
528c66ac9dbSNicholas Bellinger 	}
5296708bb27SAndy Grover 	if (!tf->tf_subsys) {
5306708bb27SAndy Grover 		pr_err("Unable to target struct config_subsystem"
531c66ac9dbSNicholas Bellinger 			" pointer\n");
532c66ac9dbSNicholas Bellinger 		return -EINVAL;
533c66ac9dbSNicholas Bellinger 	}
534c66ac9dbSNicholas Bellinger 	ret = target_fabric_tf_ops_check(tf);
535c66ac9dbSNicholas Bellinger 	if (ret < 0)
536c66ac9dbSNicholas Bellinger 		return ret;
537c66ac9dbSNicholas Bellinger 
5386708bb27SAndy Grover 	pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
539c66ac9dbSNicholas Bellinger 		">>>>>>>>>>\n");
540c66ac9dbSNicholas Bellinger 	return 0;
541c66ac9dbSNicholas Bellinger }
542c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(target_fabric_configfs_register);
543c66ac9dbSNicholas Bellinger 
544c66ac9dbSNicholas Bellinger void target_fabric_configfs_deregister(
545c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf)
546c66ac9dbSNicholas Bellinger {
547c66ac9dbSNicholas Bellinger 	struct configfs_subsystem *su;
548c66ac9dbSNicholas Bellinger 
5496708bb27SAndy Grover 	if (!tf) {
5506708bb27SAndy Grover 		pr_err("Unable to locate passed target_fabric_"
551c66ac9dbSNicholas Bellinger 			"configfs\n");
552c66ac9dbSNicholas Bellinger 		return;
553c66ac9dbSNicholas Bellinger 	}
554c66ac9dbSNicholas Bellinger 	su = tf->tf_subsys;
5556708bb27SAndy Grover 	if (!su) {
5566708bb27SAndy Grover 		pr_err("Unable to locate passed tf->tf_subsys"
557c66ac9dbSNicholas Bellinger 			" pointer\n");
558c66ac9dbSNicholas Bellinger 		return;
559c66ac9dbSNicholas Bellinger 	}
5606708bb27SAndy Grover 	pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
561c66ac9dbSNicholas Bellinger 			">>>>>>>>>>>>\n");
562c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
563c66ac9dbSNicholas Bellinger 	if (atomic_read(&tf->tf_access_cnt)) {
564c66ac9dbSNicholas Bellinger 		mutex_unlock(&g_tf_lock);
5656708bb27SAndy Grover 		pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
566c66ac9dbSNicholas Bellinger 			tf->tf_name);
567c66ac9dbSNicholas Bellinger 		BUG();
568c66ac9dbSNicholas Bellinger 	}
569c66ac9dbSNicholas Bellinger 	list_del(&tf->tf_list);
570c66ac9dbSNicholas Bellinger 	mutex_unlock(&g_tf_lock);
571c66ac9dbSNicholas Bellinger 
5726708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
573c66ac9dbSNicholas Bellinger 			" %s\n", tf->tf_name);
574c66ac9dbSNicholas Bellinger 	tf->tf_module = NULL;
575c66ac9dbSNicholas Bellinger 	tf->tf_subsys = NULL;
576c66ac9dbSNicholas Bellinger 	kfree(tf);
577c66ac9dbSNicholas Bellinger 
5786708bb27SAndy Grover 	pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
579c66ac9dbSNicholas Bellinger 			">>>>>\n");
580c66ac9dbSNicholas Bellinger }
581c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(target_fabric_configfs_deregister);
582c66ac9dbSNicholas Bellinger 
583c66ac9dbSNicholas Bellinger /*##############################################################################
584c66ac9dbSNicholas Bellinger // Stop functions called by external Target Fabrics Modules
585c66ac9dbSNicholas Bellinger //############################################################################*/
586c66ac9dbSNicholas Bellinger 
587f79a897eSNicholas Bellinger /* Start functions for struct config_item_type tb_dev_attrib_cit */
588c66ac9dbSNicholas Bellinger 
589c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
590c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
591c66ac9dbSNicholas Bellinger 
592c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_attrib_ops = {
593c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_dev_attrib_attr_show,
594c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_dev_attrib_attr_store,
595c66ac9dbSNicholas Bellinger };
596c66ac9dbSNicholas Bellinger 
59743cf208cSNicholas Bellinger TB_CIT_SETUP(dev_attrib, &target_core_dev_attrib_ops, NULL, NULL);
598c66ac9dbSNicholas Bellinger 
599f79a897eSNicholas Bellinger /* End functions for struct config_item_type tb_dev_attrib_cit */
600c66ac9dbSNicholas Bellinger 
601f8d389c6SNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_wwn_cit */
602c66ac9dbSNicholas Bellinger 
603c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
604c66ac9dbSNicholas Bellinger #define SE_DEV_WWN_ATTR(_name, _mode)					\
605c66ac9dbSNicholas Bellinger static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
606c66ac9dbSNicholas Bellinger 		__CONFIGFS_EATTR(_name, _mode,				\
607c66ac9dbSNicholas Bellinger 		target_core_dev_wwn_show_attr_##_name,			\
608c66ac9dbSNicholas Bellinger 		target_core_dev_wwn_store_attr_##_name);
609c66ac9dbSNicholas Bellinger 
610c66ac9dbSNicholas Bellinger #define SE_DEV_WWN_ATTR_RO(_name);					\
611c66ac9dbSNicholas Bellinger do {									\
612c66ac9dbSNicholas Bellinger 	static struct target_core_dev_wwn_attribute			\
613c66ac9dbSNicholas Bellinger 			target_core_dev_wwn_##_name =			\
614c66ac9dbSNicholas Bellinger 		__CONFIGFS_EATTR_RO(_name,				\
615c66ac9dbSNicholas Bellinger 		target_core_dev_wwn_show_attr_##_name);			\
616c66ac9dbSNicholas Bellinger } while (0);
617c66ac9dbSNicholas Bellinger 
618c66ac9dbSNicholas Bellinger /*
619c66ac9dbSNicholas Bellinger  * VPD page 0x80 Unit serial
620c66ac9dbSNicholas Bellinger  */
621c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
622c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
623c66ac9dbSNicholas Bellinger 	char *page)
624c66ac9dbSNicholas Bellinger {
625c66ac9dbSNicholas Bellinger 	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
626c66ac9dbSNicholas Bellinger 		&t10_wwn->unit_serial[0]);
627c66ac9dbSNicholas Bellinger }
628c66ac9dbSNicholas Bellinger 
629c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
630c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
631c66ac9dbSNicholas Bellinger 	const char *page,
632c66ac9dbSNicholas Bellinger 	size_t count)
633c66ac9dbSNicholas Bellinger {
6340fd97ccfSChristoph Hellwig 	struct se_device *dev = t10_wwn->t10_dev;
635c66ac9dbSNicholas Bellinger 	unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
636c66ac9dbSNicholas Bellinger 
637c66ac9dbSNicholas Bellinger 	/*
638c66ac9dbSNicholas Bellinger 	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
639c66ac9dbSNicholas Bellinger 	 * from the struct scsi_device level firmware, do not allow
640c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial to be emulated.
641c66ac9dbSNicholas Bellinger 	 *
642c66ac9dbSNicholas Bellinger 	 * Note this struct scsi_device could also be emulating VPD
643c66ac9dbSNicholas Bellinger 	 * information from its drivers/scsi LLD.  But for now we assume
644c66ac9dbSNicholas Bellinger 	 * it is doing 'the right thing' wrt a world wide unique
645c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial Number that OS dependent multipath can depend on.
646c66ac9dbSNicholas Bellinger 	 */
6470fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
6486708bb27SAndy Grover 		pr_err("Underlying SCSI device firmware provided VPD"
649c66ac9dbSNicholas Bellinger 			" Unit Serial, ignoring request\n");
650c66ac9dbSNicholas Bellinger 		return -EOPNOTSUPP;
651c66ac9dbSNicholas Bellinger 	}
652c66ac9dbSNicholas Bellinger 
65360d645a4SDan Carpenter 	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
6546708bb27SAndy Grover 		pr_err("Emulated VPD Unit Serial exceeds"
655c66ac9dbSNicholas Bellinger 		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
656c66ac9dbSNicholas Bellinger 		return -EOVERFLOW;
657c66ac9dbSNicholas Bellinger 	}
658c66ac9dbSNicholas Bellinger 	/*
659c66ac9dbSNicholas Bellinger 	 * Check to see if any active $FABRIC_MOD exports exist.  If they
660c66ac9dbSNicholas Bellinger 	 * do exist, fail here as changing this information on the fly
661c66ac9dbSNicholas Bellinger 	 * (underneath the initiator side OS dependent multipath code)
662c66ac9dbSNicholas Bellinger 	 * could cause negative effects.
663c66ac9dbSNicholas Bellinger 	 */
6640fd97ccfSChristoph Hellwig 	if (dev->export_count) {
6656708bb27SAndy Grover 		pr_err("Unable to set VPD Unit Serial while"
666c66ac9dbSNicholas Bellinger 			" active %d $FABRIC_MOD exports exist\n",
6670fd97ccfSChristoph Hellwig 			dev->export_count);
668c66ac9dbSNicholas Bellinger 		return -EINVAL;
669c66ac9dbSNicholas Bellinger 	}
6700fd97ccfSChristoph Hellwig 
671c66ac9dbSNicholas Bellinger 	/*
672c66ac9dbSNicholas Bellinger 	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
673c66ac9dbSNicholas Bellinger 	 *
674c66ac9dbSNicholas Bellinger 	 * Also, strip any newline added from the userspace
675c66ac9dbSNicholas Bellinger 	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
676c66ac9dbSNicholas Bellinger 	 */
677c66ac9dbSNicholas Bellinger 	memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
678c66ac9dbSNicholas Bellinger 	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
6790fd97ccfSChristoph Hellwig 	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
680c66ac9dbSNicholas Bellinger 			"%s", strstrip(buf));
6810fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
682c66ac9dbSNicholas Bellinger 
6836708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
6840fd97ccfSChristoph Hellwig 			" %s\n", dev->t10_wwn.unit_serial);
685c66ac9dbSNicholas Bellinger 
686c66ac9dbSNicholas Bellinger 	return count;
687c66ac9dbSNicholas Bellinger }
688c66ac9dbSNicholas Bellinger 
689c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
690c66ac9dbSNicholas Bellinger 
691c66ac9dbSNicholas Bellinger /*
692c66ac9dbSNicholas Bellinger  * VPD page 0x83 Protocol Identifier
693c66ac9dbSNicholas Bellinger  */
694c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
695c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
696c66ac9dbSNicholas Bellinger 	char *page)
697c66ac9dbSNicholas Bellinger {
698c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;
699c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];
700c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
701c66ac9dbSNicholas Bellinger 
702c66ac9dbSNicholas Bellinger 	memset(buf, 0, VPD_TMP_BUF_SIZE);
703c66ac9dbSNicholas Bellinger 
704c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);
705c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
7066708bb27SAndy Grover 		if (!vpd->protocol_identifier_set)
707c66ac9dbSNicholas Bellinger 			continue;
708c66ac9dbSNicholas Bellinger 
709c66ac9dbSNicholas Bellinger 		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
710c66ac9dbSNicholas Bellinger 
7116708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
712c66ac9dbSNicholas Bellinger 			break;
713c66ac9dbSNicholas Bellinger 
714c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
715c66ac9dbSNicholas Bellinger 	}
716c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);
717c66ac9dbSNicholas Bellinger 
718c66ac9dbSNicholas Bellinger 	return len;
719c66ac9dbSNicholas Bellinger }
720c66ac9dbSNicholas Bellinger 
721c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
722c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
723c66ac9dbSNicholas Bellinger 	const char *page,
724c66ac9dbSNicholas Bellinger 	size_t count)
725c66ac9dbSNicholas Bellinger {
726c66ac9dbSNicholas Bellinger 	return -ENOSYS;
727c66ac9dbSNicholas Bellinger }
728c66ac9dbSNicholas Bellinger 
729c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
730c66ac9dbSNicholas Bellinger 
731c66ac9dbSNicholas Bellinger /*
732c66ac9dbSNicholas Bellinger  * Generic wrapper for dumping VPD identifiers by association.
733c66ac9dbSNicholas Bellinger  */
734c66ac9dbSNicholas Bellinger #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
735c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_show_attr_##_name(			\
736c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,					\
737c66ac9dbSNicholas Bellinger 	char *page)							\
738c66ac9dbSNicholas Bellinger {									\
739c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;							\
740c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];				\
741c66ac9dbSNicholas Bellinger 	ssize_t len = 0;						\
742c66ac9dbSNicholas Bellinger 									\
743c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);				\
744c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
745c66ac9dbSNicholas Bellinger 		if (vpd->association != _assoc)				\
746c66ac9dbSNicholas Bellinger 			continue;					\
747c66ac9dbSNicholas Bellinger 									\
748c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
749c66ac9dbSNicholas Bellinger 		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
7506708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
751c66ac9dbSNicholas Bellinger 			break;						\
752c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
753c66ac9dbSNicholas Bellinger 									\
754c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
755c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
7566708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
757c66ac9dbSNicholas Bellinger 			break;						\
758c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
759c66ac9dbSNicholas Bellinger 									\
760c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
761c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
7626708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
763c66ac9dbSNicholas Bellinger 			break;						\
764c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
765c66ac9dbSNicholas Bellinger 	}								\
766c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);				\
767c66ac9dbSNicholas Bellinger 									\
768c66ac9dbSNicholas Bellinger 	return len;							\
769c66ac9dbSNicholas Bellinger }
770c66ac9dbSNicholas Bellinger 
771c66ac9dbSNicholas Bellinger /*
772163cd5faSAndy Shevchenko  * VPD page 0x83 Association: Logical Unit
773c66ac9dbSNicholas Bellinger  */
774c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
775c66ac9dbSNicholas Bellinger 
776c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
777c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
778c66ac9dbSNicholas Bellinger 	const char *page,
779c66ac9dbSNicholas Bellinger 	size_t count)
780c66ac9dbSNicholas Bellinger {
781c66ac9dbSNicholas Bellinger 	return -ENOSYS;
782c66ac9dbSNicholas Bellinger }
783c66ac9dbSNicholas Bellinger 
784c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
785c66ac9dbSNicholas Bellinger 
786c66ac9dbSNicholas Bellinger /*
787c66ac9dbSNicholas Bellinger  * VPD page 0x83 Association: Target Port
788c66ac9dbSNicholas Bellinger  */
789c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
790c66ac9dbSNicholas Bellinger 
791c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
792c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
793c66ac9dbSNicholas Bellinger 	const char *page,
794c66ac9dbSNicholas Bellinger 	size_t count)
795c66ac9dbSNicholas Bellinger {
796c66ac9dbSNicholas Bellinger 	return -ENOSYS;
797c66ac9dbSNicholas Bellinger }
798c66ac9dbSNicholas Bellinger 
799c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
800c66ac9dbSNicholas Bellinger 
801c66ac9dbSNicholas Bellinger /*
802c66ac9dbSNicholas Bellinger  * VPD page 0x83 Association: SCSI Target Device
803c66ac9dbSNicholas Bellinger  */
804c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
805c66ac9dbSNicholas Bellinger 
806c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
807c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
808c66ac9dbSNicholas Bellinger 	const char *page,
809c66ac9dbSNicholas Bellinger 	size_t count)
810c66ac9dbSNicholas Bellinger {
811c66ac9dbSNicholas Bellinger 	return -ENOSYS;
812c66ac9dbSNicholas Bellinger }
813c66ac9dbSNicholas Bellinger 
814c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
815c66ac9dbSNicholas Bellinger 
816c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
817c66ac9dbSNicholas Bellinger 
818c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
819c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_unit_serial.attr,
820c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_protocol_identifier.attr,
821c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_assoc_logical_unit.attr,
822c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_assoc_target_port.attr,
823c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
824c66ac9dbSNicholas Bellinger 	NULL,
825c66ac9dbSNicholas Bellinger };
826c66ac9dbSNicholas Bellinger 
827c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_wwn_ops = {
828c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_dev_wwn_attr_show,
829c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_dev_wwn_attr_store,
830c66ac9dbSNicholas Bellinger };
831c66ac9dbSNicholas Bellinger 
832f8d389c6SNicholas Bellinger TB_CIT_SETUP(dev_wwn, &target_core_dev_wwn_ops, NULL, target_core_dev_wwn_attrs);
833c66ac9dbSNicholas Bellinger 
834f8d389c6SNicholas Bellinger /*  End functions for struct config_item_type tb_dev_wwn_cit */
835c66ac9dbSNicholas Bellinger 
83691e2e39bSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_pr_cit */
837c66ac9dbSNicholas Bellinger 
8380fd97ccfSChristoph Hellwig CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
839c66ac9dbSNicholas Bellinger #define SE_DEV_PR_ATTR(_name, _mode)					\
840c66ac9dbSNicholas Bellinger static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
841c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR(_name, _mode,					\
842c66ac9dbSNicholas Bellinger 	target_core_dev_pr_show_attr_##_name,				\
843c66ac9dbSNicholas Bellinger 	target_core_dev_pr_store_attr_##_name);
844c66ac9dbSNicholas Bellinger 
845c66ac9dbSNicholas Bellinger #define SE_DEV_PR_ATTR_RO(_name);					\
846c66ac9dbSNicholas Bellinger static struct target_core_dev_pr_attribute target_core_dev_pr_##_name =	\
847c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR_RO(_name,					\
848c66ac9dbSNicholas Bellinger 	target_core_dev_pr_show_attr_##_name);
849c66ac9dbSNicholas Bellinger 
850d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
851d977f437SChristoph Hellwig 		char *page)
852c66ac9dbSNicholas Bellinger {
853c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
854c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
855c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
856c66ac9dbSNicholas Bellinger 
857c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
858c66ac9dbSNicholas Bellinger 
859c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
860d977f437SChristoph Hellwig 	if (!pr_reg)
861d977f437SChristoph Hellwig 		return sprintf(page, "No SPC-3 Reservation holder\n");
862d977f437SChristoph Hellwig 
863c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
864d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
865c66ac9dbSNicholas Bellinger 
866d977f437SChristoph Hellwig 	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
867e3d6f909SAndy Grover 		se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
868d2843c17SAndy Grover 		se_nacl->initiatorname, i_buf);
869c66ac9dbSNicholas Bellinger }
870c66ac9dbSNicholas Bellinger 
871d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
872d977f437SChristoph Hellwig 		char *page)
873c66ac9dbSNicholas Bellinger {
874c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
875d977f437SChristoph Hellwig 	ssize_t len;
876c66ac9dbSNicholas Bellinger 
877c66ac9dbSNicholas Bellinger 	se_nacl = dev->dev_reserved_node_acl;
878d977f437SChristoph Hellwig 	if (se_nacl) {
879d977f437SChristoph Hellwig 		len = sprintf(page,
880d977f437SChristoph Hellwig 			      "SPC-2 Reservation: %s Initiator: %s\n",
881e3d6f909SAndy Grover 			      se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
882c66ac9dbSNicholas Bellinger 			      se_nacl->initiatorname);
883d977f437SChristoph Hellwig 	} else {
884d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-2 Reservation holder\n");
885d977f437SChristoph Hellwig 	}
886d977f437SChristoph Hellwig 	return len;
887c66ac9dbSNicholas Bellinger }
888c66ac9dbSNicholas Bellinger 
8890fd97ccfSChristoph Hellwig static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
890c66ac9dbSNicholas Bellinger 		char *page)
891c66ac9dbSNicholas Bellinger {
892d977f437SChristoph Hellwig 	int ret;
893c66ac9dbSNicholas Bellinger 
894d977f437SChristoph Hellwig 	if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
895d977f437SChristoph Hellwig 		return sprintf(page, "Passthrough\n");
896c66ac9dbSNicholas Bellinger 
897d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
898d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
899d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc2_res(dev, page);
900d977f437SChristoph Hellwig 	else
901d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc3_res(dev, page);
902d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
903d977f437SChristoph Hellwig 	return ret;
904c66ac9dbSNicholas Bellinger }
905c66ac9dbSNicholas Bellinger 
906c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_holder);
907c66ac9dbSNicholas Bellinger 
908c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
9090fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
910c66ac9dbSNicholas Bellinger {
911c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
912c66ac9dbSNicholas Bellinger 
913c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
914d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder) {
915c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
916d977f437SChristoph Hellwig 	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
917c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: All Target"
918c66ac9dbSNicholas Bellinger 			" Ports registration\n");
919d977f437SChristoph Hellwig 	} else {
920c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: Single"
921c66ac9dbSNicholas Bellinger 			" Target Port registration\n");
922d977f437SChristoph Hellwig 	}
923c66ac9dbSNicholas Bellinger 
924d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
925c66ac9dbSNicholas Bellinger 	return len;
926c66ac9dbSNicholas Bellinger }
927c66ac9dbSNicholas Bellinger 
928c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
929c66ac9dbSNicholas Bellinger 
930c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
9310fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
932c66ac9dbSNicholas Bellinger {
9330fd97ccfSChristoph Hellwig 	return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
934c66ac9dbSNicholas Bellinger }
935c66ac9dbSNicholas Bellinger 
936c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_generation);
937c66ac9dbSNicholas Bellinger 
938c66ac9dbSNicholas Bellinger /*
939c66ac9dbSNicholas Bellinger  * res_pr_holder_tg_port
940c66ac9dbSNicholas Bellinger  */
941c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
9420fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
943c66ac9dbSNicholas Bellinger {
944c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
945c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
946c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
947c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
948c66ac9dbSNicholas Bellinger 	struct target_core_fabric_ops *tfo;
949c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
950c66ac9dbSNicholas Bellinger 
951c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
952c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
9536708bb27SAndy Grover 	if (!pr_reg) {
954c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
955d977f437SChristoph Hellwig 		goto out_unlock;
956c66ac9dbSNicholas Bellinger 	}
957d977f437SChristoph Hellwig 
958c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
959c66ac9dbSNicholas Bellinger 	se_tpg = se_nacl->se_tpg;
960c66ac9dbSNicholas Bellinger 	lun = pr_reg->pr_reg_tg_pt_lun;
961e3d6f909SAndy Grover 	tfo = se_tpg->se_tpg_tfo;
962c66ac9dbSNicholas Bellinger 
963c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: %s"
964c66ac9dbSNicholas Bellinger 		" Target Node Endpoint: %s\n", tfo->get_fabric_name(),
965c66ac9dbSNicholas Bellinger 		tfo->tpg_get_wwn(se_tpg));
966c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
96735d1efe8SMasanari Iida 		" Identifier Tag: %hu %s Portal Group Tag: %hu"
968c66ac9dbSNicholas Bellinger 		" %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
969c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
970c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), lun->unpacked_lun);
971c66ac9dbSNicholas Bellinger 
972d977f437SChristoph Hellwig out_unlock:
973d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
974c66ac9dbSNicholas Bellinger 	return len;
975c66ac9dbSNicholas Bellinger }
976c66ac9dbSNicholas Bellinger 
977c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
978c66ac9dbSNicholas Bellinger 
979c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
9800fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
981c66ac9dbSNicholas Bellinger {
982c66ac9dbSNicholas Bellinger 	struct target_core_fabric_ops *tfo;
983c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
984c66ac9dbSNicholas Bellinger 	unsigned char buf[384];
985c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
986c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
987d2843c17SAndy Grover 	int reg_count = 0;
988c66ac9dbSNicholas Bellinger 
989c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 PR Registrations:\n");
990c66ac9dbSNicholas Bellinger 
9910fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
9920fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
993c66ac9dbSNicholas Bellinger 			pr_reg_list) {
994c66ac9dbSNicholas Bellinger 
995c66ac9dbSNicholas Bellinger 		memset(buf, 0, 384);
996c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
997c66ac9dbSNicholas Bellinger 		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
998d2843c17SAndy Grover 		core_pr_dump_initiator_port(pr_reg, i_buf,
999c66ac9dbSNicholas Bellinger 					PR_REG_ISID_ID_LEN);
1000c66ac9dbSNicholas Bellinger 		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1001c66ac9dbSNicholas Bellinger 			tfo->get_fabric_name(),
1002d2843c17SAndy Grover 			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1003c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_generation);
1004c66ac9dbSNicholas Bellinger 
10056708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1006c66ac9dbSNicholas Bellinger 			break;
1007c66ac9dbSNicholas Bellinger 
1008c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1009c66ac9dbSNicholas Bellinger 		reg_count++;
1010c66ac9dbSNicholas Bellinger 	}
10110fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
1012c66ac9dbSNicholas Bellinger 
10136708bb27SAndy Grover 	if (!reg_count)
1014c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "None\n");
1015c66ac9dbSNicholas Bellinger 
1016c66ac9dbSNicholas Bellinger 	return len;
1017c66ac9dbSNicholas Bellinger }
1018c66ac9dbSNicholas Bellinger 
1019c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1020c66ac9dbSNicholas Bellinger 
1021c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_type(
10220fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
1023c66ac9dbSNicholas Bellinger {
1024c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1025c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1026c66ac9dbSNicholas Bellinger 
1027c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1028c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1029d977f437SChristoph Hellwig 	if (pr_reg) {
1030c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1031c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1032d977f437SChristoph Hellwig 	} else {
1033d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1034d977f437SChristoph Hellwig 	}
1035c66ac9dbSNicholas Bellinger 
1036d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1037c66ac9dbSNicholas Bellinger 	return len;
1038c66ac9dbSNicholas Bellinger }
1039c66ac9dbSNicholas Bellinger 
1040c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_type);
1041c66ac9dbSNicholas Bellinger 
1042c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_type(
10430fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
1044c66ac9dbSNicholas Bellinger {
1045d977f437SChristoph Hellwig 	if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1046d977f437SChristoph Hellwig 		return sprintf(page, "SPC_PASSTHROUGH\n");
1047d977f437SChristoph Hellwig 	else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1048d977f437SChristoph Hellwig 		return sprintf(page, "SPC2_RESERVATIONS\n");
1049d977f437SChristoph Hellwig 	else
1050d977f437SChristoph Hellwig 		return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1051c66ac9dbSNicholas Bellinger }
1052c66ac9dbSNicholas Bellinger 
1053c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_type);
1054c66ac9dbSNicholas Bellinger 
1055c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
10560fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
1057c66ac9dbSNicholas Bellinger {
1058d977f437SChristoph Hellwig 	if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1059c66ac9dbSNicholas Bellinger 		return 0;
1060c66ac9dbSNicholas Bellinger 
1061c66ac9dbSNicholas Bellinger 	return sprintf(page, "APTPL Bit Status: %s\n",
10620fd97ccfSChristoph Hellwig 		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1063c66ac9dbSNicholas Bellinger }
1064c66ac9dbSNicholas Bellinger 
1065c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_aptpl_active);
1066c66ac9dbSNicholas Bellinger 
1067c66ac9dbSNicholas Bellinger /*
1068c66ac9dbSNicholas Bellinger  * res_aptpl_metadata
1069c66ac9dbSNicholas Bellinger  */
1070c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
10710fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
1072c66ac9dbSNicholas Bellinger {
1073d977f437SChristoph Hellwig 	if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1074c66ac9dbSNicholas Bellinger 		return 0;
1075c66ac9dbSNicholas Bellinger 
1076c66ac9dbSNicholas Bellinger 	return sprintf(page, "Ready to process PR APTPL metadata..\n");
1077c66ac9dbSNicholas Bellinger }
1078c66ac9dbSNicholas Bellinger 
1079c66ac9dbSNicholas Bellinger enum {
1080c66ac9dbSNicholas Bellinger 	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1081c66ac9dbSNicholas Bellinger 	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1082c66ac9dbSNicholas Bellinger 	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1083c66ac9dbSNicholas Bellinger 	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1084c66ac9dbSNicholas Bellinger };
1085c66ac9dbSNicholas Bellinger 
1086c66ac9dbSNicholas Bellinger static match_table_t tokens = {
1087c66ac9dbSNicholas Bellinger 	{Opt_initiator_fabric, "initiator_fabric=%s"},
1088c66ac9dbSNicholas Bellinger 	{Opt_initiator_node, "initiator_node=%s"},
1089c66ac9dbSNicholas Bellinger 	{Opt_initiator_sid, "initiator_sid=%s"},
1090c66ac9dbSNicholas Bellinger 	{Opt_sa_res_key, "sa_res_key=%s"},
1091c66ac9dbSNicholas Bellinger 	{Opt_res_holder, "res_holder=%d"},
1092c66ac9dbSNicholas Bellinger 	{Opt_res_type, "res_type=%d"},
1093c66ac9dbSNicholas Bellinger 	{Opt_res_scope, "res_scope=%d"},
1094c66ac9dbSNicholas Bellinger 	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1095c66ac9dbSNicholas Bellinger 	{Opt_mapped_lun, "mapped_lun=%d"},
1096c66ac9dbSNicholas Bellinger 	{Opt_target_fabric, "target_fabric=%s"},
1097c66ac9dbSNicholas Bellinger 	{Opt_target_node, "target_node=%s"},
1098c66ac9dbSNicholas Bellinger 	{Opt_tpgt, "tpgt=%d"},
1099c66ac9dbSNicholas Bellinger 	{Opt_port_rtpi, "port_rtpi=%d"},
1100c66ac9dbSNicholas Bellinger 	{Opt_target_lun, "target_lun=%d"},
1101c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
1102c66ac9dbSNicholas Bellinger };
1103c66ac9dbSNicholas Bellinger 
1104c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
11050fd97ccfSChristoph Hellwig 	struct se_device *dev,
1106c66ac9dbSNicholas Bellinger 	const char *page,
1107c66ac9dbSNicholas Bellinger 	size_t count)
1108c66ac9dbSNicholas Bellinger {
11096d180253SJesper Juhl 	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
11106d180253SJesper Juhl 	unsigned char *t_fabric = NULL, *t_port = NULL;
11118d213559SJoern Engel 	char *orig, *ptr, *opts;
1112c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
1113c66ac9dbSNicholas Bellinger 	unsigned long long tmp_ll;
1114c66ac9dbSNicholas Bellinger 	u64 sa_res_key = 0;
1115c66ac9dbSNicholas Bellinger 	u32 mapped_lun = 0, target_lun = 0;
1116c66ac9dbSNicholas Bellinger 	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1117c66ac9dbSNicholas Bellinger 	u16 port_rpti = 0, tpgt = 0;
1118c66ac9dbSNicholas Bellinger 	u8 type = 0, scope;
1119c66ac9dbSNicholas Bellinger 
1120d977f437SChristoph Hellwig 	if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1121d977f437SChristoph Hellwig 		return 0;
1122d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1123c66ac9dbSNicholas Bellinger 		return 0;
1124c66ac9dbSNicholas Bellinger 
11250fd97ccfSChristoph Hellwig 	if (dev->export_count) {
11266708bb27SAndy Grover 		pr_debug("Unable to process APTPL metadata while"
1127c66ac9dbSNicholas Bellinger 			" active fabric exports exist\n");
1128c66ac9dbSNicholas Bellinger 		return -EINVAL;
1129c66ac9dbSNicholas Bellinger 	}
1130c66ac9dbSNicholas Bellinger 
1131c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
1132c66ac9dbSNicholas Bellinger 	if (!opts)
1133c66ac9dbSNicholas Bellinger 		return -ENOMEM;
1134c66ac9dbSNicholas Bellinger 
1135c66ac9dbSNicholas Bellinger 	orig = opts;
113690c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
1137c66ac9dbSNicholas Bellinger 		if (!*ptr)
1138c66ac9dbSNicholas Bellinger 			continue;
1139c66ac9dbSNicholas Bellinger 
1140c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
1141c66ac9dbSNicholas Bellinger 		switch (token) {
1142c66ac9dbSNicholas Bellinger 		case Opt_initiator_fabric:
11438d213559SJoern Engel 			i_fabric = match_strdup(args);
11446d180253SJesper Juhl 			if (!i_fabric) {
11456d180253SJesper Juhl 				ret = -ENOMEM;
11466d180253SJesper Juhl 				goto out;
11476d180253SJesper Juhl 			}
1148c66ac9dbSNicholas Bellinger 			break;
1149c66ac9dbSNicholas Bellinger 		case Opt_initiator_node:
11508d213559SJoern Engel 			i_port = match_strdup(args);
11516d180253SJesper Juhl 			if (!i_port) {
11526d180253SJesper Juhl 				ret = -ENOMEM;
11536d180253SJesper Juhl 				goto out;
11546d180253SJesper Juhl 			}
115560d645a4SDan Carpenter 			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
11566708bb27SAndy Grover 				pr_err("APTPL metadata initiator_node="
1157c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1158c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_IPORT_LEN);
1159c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1160c66ac9dbSNicholas Bellinger 				break;
1161c66ac9dbSNicholas Bellinger 			}
1162c66ac9dbSNicholas Bellinger 			break;
1163c66ac9dbSNicholas Bellinger 		case Opt_initiator_sid:
11648d213559SJoern Engel 			isid = match_strdup(args);
11656d180253SJesper Juhl 			if (!isid) {
11666d180253SJesper Juhl 				ret = -ENOMEM;
11676d180253SJesper Juhl 				goto out;
11686d180253SJesper Juhl 			}
116960d645a4SDan Carpenter 			if (strlen(isid) >= PR_REG_ISID_LEN) {
11706708bb27SAndy Grover 				pr_err("APTPL metadata initiator_isid"
1171c66ac9dbSNicholas Bellinger 					"= exceeds PR_REG_ISID_LEN: %d\n",
1172c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
1173c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1174c66ac9dbSNicholas Bellinger 				break;
1175c66ac9dbSNicholas Bellinger 			}
1176c66ac9dbSNicholas Bellinger 			break;
1177c66ac9dbSNicholas Bellinger 		case Opt_sa_res_key:
11788d213559SJoern Engel 			ret = kstrtoull(args->from, 0, &tmp_ll);
1179c66ac9dbSNicholas Bellinger 			if (ret < 0) {
11808d213559SJoern Engel 				pr_err("kstrtoull() failed for sa_res_key=\n");
1181c66ac9dbSNicholas Bellinger 				goto out;
1182c66ac9dbSNicholas Bellinger 			}
1183c66ac9dbSNicholas Bellinger 			sa_res_key = (u64)tmp_ll;
1184c66ac9dbSNicholas Bellinger 			break;
1185c66ac9dbSNicholas Bellinger 		/*
1186c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Reservation
1187c66ac9dbSNicholas Bellinger 		 */
1188c66ac9dbSNicholas Bellinger 		case Opt_res_holder:
1189c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1190c66ac9dbSNicholas Bellinger 			res_holder = arg;
1191c66ac9dbSNicholas Bellinger 			break;
1192c66ac9dbSNicholas Bellinger 		case Opt_res_type:
1193c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1194c66ac9dbSNicholas Bellinger 			type = (u8)arg;
1195c66ac9dbSNicholas Bellinger 			break;
1196c66ac9dbSNicholas Bellinger 		case Opt_res_scope:
1197c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1198c66ac9dbSNicholas Bellinger 			scope = (u8)arg;
1199c66ac9dbSNicholas Bellinger 			break;
1200c66ac9dbSNicholas Bellinger 		case Opt_res_all_tg_pt:
1201c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1202c66ac9dbSNicholas Bellinger 			all_tg_pt = (int)arg;
1203c66ac9dbSNicholas Bellinger 			break;
1204c66ac9dbSNicholas Bellinger 		case Opt_mapped_lun:
1205c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1206c66ac9dbSNicholas Bellinger 			mapped_lun = (u32)arg;
1207c66ac9dbSNicholas Bellinger 			break;
1208c66ac9dbSNicholas Bellinger 		/*
1209c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Target Port
1210c66ac9dbSNicholas Bellinger 		 */
1211c66ac9dbSNicholas Bellinger 		case Opt_target_fabric:
12128d213559SJoern Engel 			t_fabric = match_strdup(args);
12136d180253SJesper Juhl 			if (!t_fabric) {
12146d180253SJesper Juhl 				ret = -ENOMEM;
12156d180253SJesper Juhl 				goto out;
12166d180253SJesper Juhl 			}
1217c66ac9dbSNicholas Bellinger 			break;
1218c66ac9dbSNicholas Bellinger 		case Opt_target_node:
12198d213559SJoern Engel 			t_port = match_strdup(args);
12206d180253SJesper Juhl 			if (!t_port) {
12216d180253SJesper Juhl 				ret = -ENOMEM;
12226d180253SJesper Juhl 				goto out;
12236d180253SJesper Juhl 			}
122460d645a4SDan Carpenter 			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
12256708bb27SAndy Grover 				pr_err("APTPL metadata target_node="
1226c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1227c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_TPORT_LEN);
1228c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1229c66ac9dbSNicholas Bellinger 				break;
1230c66ac9dbSNicholas Bellinger 			}
1231c66ac9dbSNicholas Bellinger 			break;
1232c66ac9dbSNicholas Bellinger 		case Opt_tpgt:
1233c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1234c66ac9dbSNicholas Bellinger 			tpgt = (u16)arg;
1235c66ac9dbSNicholas Bellinger 			break;
1236c66ac9dbSNicholas Bellinger 		case Opt_port_rtpi:
1237c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1238c66ac9dbSNicholas Bellinger 			port_rpti = (u16)arg;
1239c66ac9dbSNicholas Bellinger 			break;
1240c66ac9dbSNicholas Bellinger 		case Opt_target_lun:
1241c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1242c66ac9dbSNicholas Bellinger 			target_lun = (u32)arg;
1243c66ac9dbSNicholas Bellinger 			break;
1244c66ac9dbSNicholas Bellinger 		default:
1245c66ac9dbSNicholas Bellinger 			break;
1246c66ac9dbSNicholas Bellinger 		}
1247c66ac9dbSNicholas Bellinger 	}
1248c66ac9dbSNicholas Bellinger 
12496708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
12506708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
1251c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1252c66ac9dbSNicholas Bellinger 		goto out;
1253c66ac9dbSNicholas Bellinger 	}
1254c66ac9dbSNicholas Bellinger 
1255c66ac9dbSNicholas Bellinger 	if (res_holder && !(type)) {
12566708bb27SAndy Grover 		pr_err("Illegal PR type: 0x%02x for reservation"
1257c66ac9dbSNicholas Bellinger 				" holder\n", type);
1258c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1259c66ac9dbSNicholas Bellinger 		goto out;
1260c66ac9dbSNicholas Bellinger 	}
1261c66ac9dbSNicholas Bellinger 
12620fd97ccfSChristoph Hellwig 	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1263c66ac9dbSNicholas Bellinger 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1264c66ac9dbSNicholas Bellinger 			res_holder, all_tg_pt, type);
1265c66ac9dbSNicholas Bellinger out:
12666d180253SJesper Juhl 	kfree(i_fabric);
12676d180253SJesper Juhl 	kfree(i_port);
12686d180253SJesper Juhl 	kfree(isid);
12696d180253SJesper Juhl 	kfree(t_fabric);
12706d180253SJesper Juhl 	kfree(t_port);
1271c66ac9dbSNicholas Bellinger 	kfree(orig);
1272c66ac9dbSNicholas Bellinger 	return (ret == 0) ? count : ret;
1273c66ac9dbSNicholas Bellinger }
1274c66ac9dbSNicholas Bellinger 
1275c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1276c66ac9dbSNicholas Bellinger 
12770fd97ccfSChristoph Hellwig CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
1278c66ac9dbSNicholas Bellinger 
1279c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1280c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_holder.attr,
1281c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_all_tgt_pts.attr,
1282c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_generation.attr,
1283c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_holder_tg_port.attr,
1284c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_registered_i_pts.attr,
1285c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_type.attr,
1286c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_type.attr,
1287c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_aptpl_active.attr,
1288c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_aptpl_metadata.attr,
1289c66ac9dbSNicholas Bellinger 	NULL,
1290c66ac9dbSNicholas Bellinger };
1291c66ac9dbSNicholas Bellinger 
1292c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_pr_ops = {
1293c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_dev_pr_attr_show,
1294c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_dev_pr_attr_store,
1295c66ac9dbSNicholas Bellinger };
1296c66ac9dbSNicholas Bellinger 
129791e2e39bSNicholas Bellinger TB_CIT_SETUP(dev_pr, &target_core_dev_pr_ops, NULL, target_core_dev_pr_attrs);
1298c66ac9dbSNicholas Bellinger 
129991e2e39bSNicholas Bellinger /*  End functions for struct config_item_type tb_dev_pr_cit */
1300c66ac9dbSNicholas Bellinger 
130173112edcSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_cit */
1302c66ac9dbSNicholas Bellinger 
1303c66ac9dbSNicholas Bellinger static ssize_t target_core_show_dev_info(void *p, char *page)
1304c66ac9dbSNicholas Bellinger {
13050fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
13060fd97ccfSChristoph Hellwig 	struct se_subsystem_api *t = dev->transport;
1307c66ac9dbSNicholas Bellinger 	int bl = 0;
1308c66ac9dbSNicholas Bellinger 	ssize_t read_bytes = 0;
1309c66ac9dbSNicholas Bellinger 
13100fd97ccfSChristoph Hellwig 	transport_dump_dev_state(dev, page, &bl);
1311c66ac9dbSNicholas Bellinger 	read_bytes += bl;
13120fd97ccfSChristoph Hellwig 	read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
1313c66ac9dbSNicholas Bellinger 	return read_bytes;
1314c66ac9dbSNicholas Bellinger }
1315c66ac9dbSNicholas Bellinger 
1316c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_info = {
1317c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1318c66ac9dbSNicholas Bellinger 		    .ca_name = "info",
1319c66ac9dbSNicholas Bellinger 		    .ca_mode = S_IRUGO },
1320c66ac9dbSNicholas Bellinger 	.show	= target_core_show_dev_info,
1321c66ac9dbSNicholas Bellinger 	.store	= NULL,
1322c66ac9dbSNicholas Bellinger };
1323c66ac9dbSNicholas Bellinger 
1324c66ac9dbSNicholas Bellinger static ssize_t target_core_store_dev_control(
1325c66ac9dbSNicholas Bellinger 	void *p,
1326c66ac9dbSNicholas Bellinger 	const char *page,
1327c66ac9dbSNicholas Bellinger 	size_t count)
1328c66ac9dbSNicholas Bellinger {
13290fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
13300fd97ccfSChristoph Hellwig 	struct se_subsystem_api *t = dev->transport;
1331c66ac9dbSNicholas Bellinger 
13320fd97ccfSChristoph Hellwig 	return t->set_configfs_dev_params(dev, page, count);
1333c66ac9dbSNicholas Bellinger }
1334c66ac9dbSNicholas Bellinger 
1335c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_control = {
1336c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1337c66ac9dbSNicholas Bellinger 		    .ca_name = "control",
1338c66ac9dbSNicholas Bellinger 		    .ca_mode = S_IWUSR },
1339c66ac9dbSNicholas Bellinger 	.show	= NULL,
1340c66ac9dbSNicholas Bellinger 	.store	= target_core_store_dev_control,
1341c66ac9dbSNicholas Bellinger };
1342c66ac9dbSNicholas Bellinger 
1343c66ac9dbSNicholas Bellinger static ssize_t target_core_show_dev_alias(void *p, char *page)
1344c66ac9dbSNicholas Bellinger {
13450fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
1346c66ac9dbSNicholas Bellinger 
13470fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_ALIAS))
1348c66ac9dbSNicholas Bellinger 		return 0;
1349c66ac9dbSNicholas Bellinger 
13500fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1351c66ac9dbSNicholas Bellinger }
1352c66ac9dbSNicholas Bellinger 
1353c66ac9dbSNicholas Bellinger static ssize_t target_core_store_dev_alias(
1354c66ac9dbSNicholas Bellinger 	void *p,
1355c66ac9dbSNicholas Bellinger 	const char *page,
1356c66ac9dbSNicholas Bellinger 	size_t count)
1357c66ac9dbSNicholas Bellinger {
13580fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
13590fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1360c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1361c66ac9dbSNicholas Bellinger 
1362c66ac9dbSNicholas Bellinger 	if (count > (SE_DEV_ALIAS_LEN-1)) {
13636708bb27SAndy Grover 		pr_err("alias count: %d exceeds"
1364c66ac9dbSNicholas Bellinger 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1365c66ac9dbSNicholas Bellinger 			SE_DEV_ALIAS_LEN-1);
1366c66ac9dbSNicholas Bellinger 		return -EINVAL;
1367c66ac9dbSNicholas Bellinger 	}
1368c66ac9dbSNicholas Bellinger 
13690fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
13703011684cSDan Carpenter 	if (!read_bytes)
13713011684cSDan Carpenter 		return -EINVAL;
13720fd97ccfSChristoph Hellwig 	if (dev->dev_alias[read_bytes - 1] == '\n')
13730fd97ccfSChristoph Hellwig 		dev->dev_alias[read_bytes - 1] = '\0';
13740877eafdSSebastian Andrzej Siewior 
13750fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_ALIAS;
13763011684cSDan Carpenter 
13776708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1378c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
13790fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
13800fd97ccfSChristoph Hellwig 		dev->dev_alias);
1381c66ac9dbSNicholas Bellinger 
1382c66ac9dbSNicholas Bellinger 	return read_bytes;
1383c66ac9dbSNicholas Bellinger }
1384c66ac9dbSNicholas Bellinger 
1385c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1386c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1387c66ac9dbSNicholas Bellinger 		    .ca_name = "alias",
1388c66ac9dbSNicholas Bellinger 		    .ca_mode =  S_IRUGO | S_IWUSR },
1389c66ac9dbSNicholas Bellinger 	.show	= target_core_show_dev_alias,
1390c66ac9dbSNicholas Bellinger 	.store	= target_core_store_dev_alias,
1391c66ac9dbSNicholas Bellinger };
1392c66ac9dbSNicholas Bellinger 
1393c66ac9dbSNicholas Bellinger static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1394c66ac9dbSNicholas Bellinger {
13950fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
1396c66ac9dbSNicholas Bellinger 
13970fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1398c66ac9dbSNicholas Bellinger 		return 0;
1399c66ac9dbSNicholas Bellinger 
14000fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1401c66ac9dbSNicholas Bellinger }
1402c66ac9dbSNicholas Bellinger 
1403c66ac9dbSNicholas Bellinger static ssize_t target_core_store_dev_udev_path(
1404c66ac9dbSNicholas Bellinger 	void *p,
1405c66ac9dbSNicholas Bellinger 	const char *page,
1406c66ac9dbSNicholas Bellinger 	size_t count)
1407c66ac9dbSNicholas Bellinger {
14080fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
14090fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1410c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1411c66ac9dbSNicholas Bellinger 
1412c66ac9dbSNicholas Bellinger 	if (count > (SE_UDEV_PATH_LEN-1)) {
14136708bb27SAndy Grover 		pr_err("udev_path count: %d exceeds"
1414c66ac9dbSNicholas Bellinger 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1415c66ac9dbSNicholas Bellinger 			SE_UDEV_PATH_LEN-1);
1416c66ac9dbSNicholas Bellinger 		return -EINVAL;
1417c66ac9dbSNicholas Bellinger 	}
1418c66ac9dbSNicholas Bellinger 
14190fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1420c66ac9dbSNicholas Bellinger 			"%s", page);
14213011684cSDan Carpenter 	if (!read_bytes)
14223011684cSDan Carpenter 		return -EINVAL;
14230fd97ccfSChristoph Hellwig 	if (dev->udev_path[read_bytes - 1] == '\n')
14240fd97ccfSChristoph Hellwig 		dev->udev_path[read_bytes - 1] = '\0';
14250877eafdSSebastian Andrzej Siewior 
14260fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_UDEV_PATH;
14273011684cSDan Carpenter 
14286708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1429c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
14300fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
14310fd97ccfSChristoph Hellwig 		dev->udev_path);
1432c66ac9dbSNicholas Bellinger 
1433c66ac9dbSNicholas Bellinger 	return read_bytes;
1434c66ac9dbSNicholas Bellinger }
1435c66ac9dbSNicholas Bellinger 
1436c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1437c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1438c66ac9dbSNicholas Bellinger 		    .ca_name = "udev_path",
1439c66ac9dbSNicholas Bellinger 		    .ca_mode =  S_IRUGO | S_IWUSR },
1440c66ac9dbSNicholas Bellinger 	.show	= target_core_show_dev_udev_path,
1441c66ac9dbSNicholas Bellinger 	.store	= target_core_store_dev_udev_path,
1442c66ac9dbSNicholas Bellinger };
1443c66ac9dbSNicholas Bellinger 
144464146db7SAndy Grover static ssize_t target_core_show_dev_enable(void *p, char *page)
144564146db7SAndy Grover {
144664146db7SAndy Grover 	struct se_device *dev = p;
144764146db7SAndy Grover 
144864146db7SAndy Grover 	return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
144964146db7SAndy Grover }
145064146db7SAndy Grover 
1451c66ac9dbSNicholas Bellinger static ssize_t target_core_store_dev_enable(
1452c66ac9dbSNicholas Bellinger 	void *p,
1453c66ac9dbSNicholas Bellinger 	const char *page,
1454c66ac9dbSNicholas Bellinger 	size_t count)
1455c66ac9dbSNicholas Bellinger {
14560fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
1457c66ac9dbSNicholas Bellinger 	char *ptr;
14580fd97ccfSChristoph Hellwig 	int ret;
1459c66ac9dbSNicholas Bellinger 
1460c66ac9dbSNicholas Bellinger 	ptr = strstr(page, "1");
14616708bb27SAndy Grover 	if (!ptr) {
14626708bb27SAndy Grover 		pr_err("For dev_enable ops, only valid value"
1463c66ac9dbSNicholas Bellinger 				" is \"1\"\n");
1464c66ac9dbSNicholas Bellinger 		return -EINVAL;
1465c66ac9dbSNicholas Bellinger 	}
1466c66ac9dbSNicholas Bellinger 
14670fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
14680fd97ccfSChristoph Hellwig 	if (ret)
14690fd97ccfSChristoph Hellwig 		return ret;
1470c66ac9dbSNicholas Bellinger 	return count;
1471c66ac9dbSNicholas Bellinger }
1472c66ac9dbSNicholas Bellinger 
1473c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1474c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1475c66ac9dbSNicholas Bellinger 		    .ca_name = "enable",
147664146db7SAndy Grover 		    .ca_mode =  S_IRUGO | S_IWUSR },
147764146db7SAndy Grover 	.show	= target_core_show_dev_enable,
1478c66ac9dbSNicholas Bellinger 	.store	= target_core_store_dev_enable,
1479c66ac9dbSNicholas Bellinger };
1480c66ac9dbSNicholas Bellinger 
1481c66ac9dbSNicholas Bellinger static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1482c66ac9dbSNicholas Bellinger {
14830fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
1484c66ac9dbSNicholas Bellinger 	struct config_item *lu_ci;
1485c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
1486c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1487c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1488c66ac9dbSNicholas Bellinger 
1489c66ac9dbSNicholas Bellinger 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1490c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
1491c87fbd56SChristoph Hellwig 		return 0;
1492c66ac9dbSNicholas Bellinger 
1493c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1494c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
14956708bb27SAndy Grover 	if (lu_gp) {
1496c66ac9dbSNicholas Bellinger 		lu_ci = &lu_gp->lu_gp_group.cg_item;
1497c66ac9dbSNicholas Bellinger 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1498c66ac9dbSNicholas Bellinger 			config_item_name(lu_ci), lu_gp->lu_gp_id);
1499c66ac9dbSNicholas Bellinger 	}
1500c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1501c66ac9dbSNicholas Bellinger 
1502c66ac9dbSNicholas Bellinger 	return len;
1503c66ac9dbSNicholas Bellinger }
1504c66ac9dbSNicholas Bellinger 
1505c66ac9dbSNicholas Bellinger static ssize_t target_core_store_alua_lu_gp(
1506c66ac9dbSNicholas Bellinger 	void *p,
1507c66ac9dbSNicholas Bellinger 	const char *page,
1508c66ac9dbSNicholas Bellinger 	size_t count)
1509c66ac9dbSNicholas Bellinger {
15100fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
15110fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1512c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1513c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1514c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
1515c66ac9dbSNicholas Bellinger 	int move = 0;
1516c66ac9dbSNicholas Bellinger 
1517c87fbd56SChristoph Hellwig 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1518c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
1519c87fbd56SChristoph Hellwig 		return 0;
1520c87fbd56SChristoph Hellwig 
1521c66ac9dbSNicholas Bellinger 	if (count > LU_GROUP_NAME_BUF) {
15226708bb27SAndy Grover 		pr_err("ALUA LU Group Alias too large!\n");
1523c66ac9dbSNicholas Bellinger 		return -EINVAL;
1524c66ac9dbSNicholas Bellinger 	}
1525c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
1526c66ac9dbSNicholas Bellinger 	memcpy(buf, page, count);
1527c66ac9dbSNicholas Bellinger 	/*
1528c66ac9dbSNicholas Bellinger 	 * Any ALUA logical unit alias besides "NULL" means we will be
1529c66ac9dbSNicholas Bellinger 	 * making a new group association.
1530c66ac9dbSNicholas Bellinger 	 */
1531c66ac9dbSNicholas Bellinger 	if (strcmp(strstrip(buf), "NULL")) {
1532c66ac9dbSNicholas Bellinger 		/*
1533c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name() will increment reference to
1534c66ac9dbSNicholas Bellinger 		 * struct t10_alua_lu_gp.  This reference is released with
1535c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name below().
1536c66ac9dbSNicholas Bellinger 		 */
1537c66ac9dbSNicholas Bellinger 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
15386708bb27SAndy Grover 		if (!lu_gp_new)
1539c66ac9dbSNicholas Bellinger 			return -ENODEV;
1540c66ac9dbSNicholas Bellinger 	}
1541c66ac9dbSNicholas Bellinger 
1542c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1543c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
15446708bb27SAndy Grover 	if (lu_gp) {
1545c66ac9dbSNicholas Bellinger 		/*
1546c66ac9dbSNicholas Bellinger 		 * Clearing an existing lu_gp association, and replacing
1547c66ac9dbSNicholas Bellinger 		 * with NULL
1548c66ac9dbSNicholas Bellinger 		 */
15496708bb27SAndy Grover 		if (!lu_gp_new) {
15506708bb27SAndy Grover 			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1551c66ac9dbSNicholas Bellinger 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1552c66ac9dbSNicholas Bellinger 				" %hu\n",
1553c66ac9dbSNicholas Bellinger 				config_item_name(&hba->hba_group.cg_item),
15540fd97ccfSChristoph Hellwig 				config_item_name(&dev->dev_group.cg_item),
1555c66ac9dbSNicholas Bellinger 				config_item_name(&lu_gp->lu_gp_group.cg_item),
1556c66ac9dbSNicholas Bellinger 				lu_gp->lu_gp_id);
1557c66ac9dbSNicholas Bellinger 
1558c66ac9dbSNicholas Bellinger 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1559c66ac9dbSNicholas Bellinger 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1560c66ac9dbSNicholas Bellinger 
1561c66ac9dbSNicholas Bellinger 			return count;
1562c66ac9dbSNicholas Bellinger 		}
1563c66ac9dbSNicholas Bellinger 		/*
1564c66ac9dbSNicholas Bellinger 		 * Removing existing association of lu_gp_mem with lu_gp
1565c66ac9dbSNicholas Bellinger 		 */
1566c66ac9dbSNicholas Bellinger 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1567c66ac9dbSNicholas Bellinger 		move = 1;
1568c66ac9dbSNicholas Bellinger 	}
1569c66ac9dbSNicholas Bellinger 	/*
1570c66ac9dbSNicholas Bellinger 	 * Associate lu_gp_mem with lu_gp_new.
1571c66ac9dbSNicholas Bellinger 	 */
1572c66ac9dbSNicholas Bellinger 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1573c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1574c66ac9dbSNicholas Bellinger 
15756708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1576c66ac9dbSNicholas Bellinger 		" core/alua/lu_gps/%s, ID: %hu\n",
1577c66ac9dbSNicholas Bellinger 		(move) ? "Moving" : "Adding",
1578c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
15790fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
1580c66ac9dbSNicholas Bellinger 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1581c66ac9dbSNicholas Bellinger 		lu_gp_new->lu_gp_id);
1582c66ac9dbSNicholas Bellinger 
1583c66ac9dbSNicholas Bellinger 	core_alua_put_lu_gp_from_name(lu_gp_new);
1584c66ac9dbSNicholas Bellinger 	return count;
1585c66ac9dbSNicholas Bellinger }
1586c66ac9dbSNicholas Bellinger 
1587c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1588c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1589c66ac9dbSNicholas Bellinger 		    .ca_name = "alua_lu_gp",
1590c66ac9dbSNicholas Bellinger 		    .ca_mode = S_IRUGO | S_IWUSR },
1591c66ac9dbSNicholas Bellinger 	.show	= target_core_show_alua_lu_gp,
1592c66ac9dbSNicholas Bellinger 	.store	= target_core_store_alua_lu_gp,
1593c66ac9dbSNicholas Bellinger };
1594c66ac9dbSNicholas Bellinger 
1595229d4f11SHannes Reinecke static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1596229d4f11SHannes Reinecke {
1597229d4f11SHannes Reinecke 	struct se_device *dev = p;
1598229d4f11SHannes Reinecke 	struct t10_alua_lba_map *map;
1599229d4f11SHannes Reinecke 	struct t10_alua_lba_map_member *mem;
1600229d4f11SHannes Reinecke 	char *b = page;
1601229d4f11SHannes Reinecke 	int bl = 0;
1602229d4f11SHannes Reinecke 	char state;
1603229d4f11SHannes Reinecke 
1604229d4f11SHannes Reinecke 	spin_lock(&dev->t10_alua.lba_map_lock);
1605229d4f11SHannes Reinecke 	if (!list_empty(&dev->t10_alua.lba_map_list))
1606229d4f11SHannes Reinecke 	    bl += sprintf(b + bl, "%u %u\n",
1607229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_size,
1608229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_multiplier);
1609229d4f11SHannes Reinecke 	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1610229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "%llu %llu",
1611229d4f11SHannes Reinecke 			      map->lba_map_first_lba, map->lba_map_last_lba);
1612229d4f11SHannes Reinecke 		list_for_each_entry(mem, &map->lba_map_mem_list,
1613229d4f11SHannes Reinecke 				    lba_map_mem_list) {
1614229d4f11SHannes Reinecke 			switch (mem->lba_map_mem_alua_state) {
1615229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1616229d4f11SHannes Reinecke 				state = 'O';
1617229d4f11SHannes Reinecke 				break;
1618229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1619229d4f11SHannes Reinecke 				state = 'A';
1620229d4f11SHannes Reinecke 				break;
1621229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_STANDBY:
1622229d4f11SHannes Reinecke 				state = 'S';
1623229d4f11SHannes Reinecke 				break;
1624229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_UNAVAILABLE:
1625229d4f11SHannes Reinecke 				state = 'U';
1626229d4f11SHannes Reinecke 				break;
1627229d4f11SHannes Reinecke 			default:
1628229d4f11SHannes Reinecke 				state = '.';
1629229d4f11SHannes Reinecke 				break;
1630229d4f11SHannes Reinecke 			}
1631229d4f11SHannes Reinecke 			bl += sprintf(b + bl, " %d:%c",
1632229d4f11SHannes Reinecke 				      mem->lba_map_mem_alua_pg_id, state);
1633229d4f11SHannes Reinecke 		}
1634229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "\n");
1635229d4f11SHannes Reinecke 	}
1636229d4f11SHannes Reinecke 	spin_unlock(&dev->t10_alua.lba_map_lock);
1637229d4f11SHannes Reinecke 	return bl;
1638229d4f11SHannes Reinecke }
1639229d4f11SHannes Reinecke 
1640229d4f11SHannes Reinecke static ssize_t target_core_store_dev_lba_map(
1641229d4f11SHannes Reinecke 	void *p,
1642229d4f11SHannes Reinecke 	const char *page,
1643229d4f11SHannes Reinecke 	size_t count)
1644229d4f11SHannes Reinecke {
1645229d4f11SHannes Reinecke 	struct se_device *dev = p;
1646229d4f11SHannes Reinecke 	struct t10_alua_lba_map *lba_map = NULL;
1647229d4f11SHannes Reinecke 	struct list_head lba_list;
1648229d4f11SHannes Reinecke 	char *map_entries, *ptr;
1649229d4f11SHannes Reinecke 	char state;
1650229d4f11SHannes Reinecke 	int pg_num = -1, pg;
1651229d4f11SHannes Reinecke 	int ret = 0, num = 0, pg_id, alua_state;
1652229d4f11SHannes Reinecke 	unsigned long start_lba = -1, end_lba = -1;
1653229d4f11SHannes Reinecke 	unsigned long segment_size = -1, segment_mult = -1;
1654229d4f11SHannes Reinecke 
1655229d4f11SHannes Reinecke 	map_entries = kstrdup(page, GFP_KERNEL);
1656229d4f11SHannes Reinecke 	if (!map_entries)
1657229d4f11SHannes Reinecke 		return -ENOMEM;
1658229d4f11SHannes Reinecke 
1659229d4f11SHannes Reinecke 	INIT_LIST_HEAD(&lba_list);
1660229d4f11SHannes Reinecke 	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1661229d4f11SHannes Reinecke 		if (!*ptr)
1662229d4f11SHannes Reinecke 			continue;
1663229d4f11SHannes Reinecke 
1664229d4f11SHannes Reinecke 		if (num == 0) {
1665229d4f11SHannes Reinecke 			if (sscanf(ptr, "%lu %lu\n",
1666229d4f11SHannes Reinecke 				   &segment_size, &segment_mult) != 2) {
1667229d4f11SHannes Reinecke 				pr_err("Invalid line %d\n", num);
1668229d4f11SHannes Reinecke 				ret = -EINVAL;
1669229d4f11SHannes Reinecke 				break;
1670229d4f11SHannes Reinecke 			}
1671229d4f11SHannes Reinecke 			num++;
1672229d4f11SHannes Reinecke 			continue;
1673229d4f11SHannes Reinecke 		}
1674229d4f11SHannes Reinecke 		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1675229d4f11SHannes Reinecke 			pr_err("Invalid line %d\n", num);
1676229d4f11SHannes Reinecke 			ret = -EINVAL;
1677229d4f11SHannes Reinecke 			break;
1678229d4f11SHannes Reinecke 		}
1679229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
1680229d4f11SHannes Reinecke 		if (!ptr) {
1681229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing end lba\n", num);
1682229d4f11SHannes Reinecke 			ret = -EINVAL;
1683229d4f11SHannes Reinecke 			break;
1684229d4f11SHannes Reinecke 		}
1685229d4f11SHannes Reinecke 		ptr++;
1686229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
1687229d4f11SHannes Reinecke 		if (!ptr) {
1688229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing state definitions\n",
1689229d4f11SHannes Reinecke 			       num);
1690229d4f11SHannes Reinecke 			ret = -EINVAL;
1691229d4f11SHannes Reinecke 			break;
1692229d4f11SHannes Reinecke 		}
1693229d4f11SHannes Reinecke 		ptr++;
1694229d4f11SHannes Reinecke 		lba_map = core_alua_allocate_lba_map(&lba_list,
1695229d4f11SHannes Reinecke 						     start_lba, end_lba);
1696229d4f11SHannes Reinecke 		if (IS_ERR(lba_map)) {
1697229d4f11SHannes Reinecke 			ret = PTR_ERR(lba_map);
1698229d4f11SHannes Reinecke 			break;
1699229d4f11SHannes Reinecke 		}
1700229d4f11SHannes Reinecke 		pg = 0;
1701229d4f11SHannes Reinecke 		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1702229d4f11SHannes Reinecke 			switch (state) {
1703229d4f11SHannes Reinecke 			case 'O':
1704229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1705229d4f11SHannes Reinecke 				break;
1706229d4f11SHannes Reinecke 			case 'A':
1707229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1708229d4f11SHannes Reinecke 				break;
1709229d4f11SHannes Reinecke 			case 'S':
1710229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_STANDBY;
1711229d4f11SHannes Reinecke 				break;
1712229d4f11SHannes Reinecke 			case 'U':
1713229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1714229d4f11SHannes Reinecke 				break;
1715229d4f11SHannes Reinecke 			default:
1716229d4f11SHannes Reinecke 				pr_err("Invalid ALUA state '%c'\n", state);
1717229d4f11SHannes Reinecke 				ret = -EINVAL;
1718229d4f11SHannes Reinecke 				goto out;
1719229d4f11SHannes Reinecke 			}
1720229d4f11SHannes Reinecke 
1721229d4f11SHannes Reinecke 			ret = core_alua_allocate_lba_map_mem(lba_map,
1722229d4f11SHannes Reinecke 							     pg_id, alua_state);
1723229d4f11SHannes Reinecke 			if (ret) {
1724229d4f11SHannes Reinecke 				pr_err("Invalid target descriptor %d:%c "
1725229d4f11SHannes Reinecke 				       "at line %d\n",
1726229d4f11SHannes Reinecke 				       pg_id, state, num);
1727229d4f11SHannes Reinecke 				break;
1728229d4f11SHannes Reinecke 			}
1729229d4f11SHannes Reinecke 			pg++;
1730229d4f11SHannes Reinecke 			ptr = strchr(ptr, ' ');
1731229d4f11SHannes Reinecke 			if (ptr)
1732229d4f11SHannes Reinecke 				ptr++;
1733229d4f11SHannes Reinecke 			else
1734229d4f11SHannes Reinecke 				break;
1735229d4f11SHannes Reinecke 		}
1736229d4f11SHannes Reinecke 		if (pg_num == -1)
1737229d4f11SHannes Reinecke 		    pg_num = pg;
1738229d4f11SHannes Reinecke 		else if (pg != pg_num) {
1739229d4f11SHannes Reinecke 			pr_err("Only %d from %d port groups definitions "
1740229d4f11SHannes Reinecke 			       "at line %d\n", pg, pg_num, num);
1741229d4f11SHannes Reinecke 			ret = -EINVAL;
1742229d4f11SHannes Reinecke 			break;
1743229d4f11SHannes Reinecke 		}
1744229d4f11SHannes Reinecke 		num++;
1745229d4f11SHannes Reinecke 	}
1746229d4f11SHannes Reinecke out:
1747229d4f11SHannes Reinecke 	if (ret) {
1748229d4f11SHannes Reinecke 		core_alua_free_lba_map(&lba_list);
1749229d4f11SHannes Reinecke 		count = ret;
1750229d4f11SHannes Reinecke 	} else
1751229d4f11SHannes Reinecke 		core_alua_set_lba_map(dev, &lba_list,
1752229d4f11SHannes Reinecke 				      segment_size, segment_mult);
1753229d4f11SHannes Reinecke 	kfree(map_entries);
1754229d4f11SHannes Reinecke 	return count;
1755229d4f11SHannes Reinecke }
1756229d4f11SHannes Reinecke 
1757229d4f11SHannes Reinecke static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1758229d4f11SHannes Reinecke 	.attr	= { .ca_owner = THIS_MODULE,
1759229d4f11SHannes Reinecke 		    .ca_name = "lba_map",
1760229d4f11SHannes Reinecke 		    .ca_mode = S_IRUGO | S_IWUSR },
1761229d4f11SHannes Reinecke 	.show	= target_core_show_dev_lba_map,
1762229d4f11SHannes Reinecke 	.store	= target_core_store_dev_lba_map,
1763229d4f11SHannes Reinecke };
1764229d4f11SHannes Reinecke 
176573112edcSNicholas Bellinger static struct configfs_attribute *target_core_dev_attrs[] = {
1766c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_info.attr,
1767c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_control.attr,
1768c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_alias.attr,
1769c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_udev_path.attr,
1770c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_enable.attr,
1771c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_alua_lu_gp.attr,
1772229d4f11SHannes Reinecke 	&target_core_attr_dev_lba_map.attr,
1773c66ac9dbSNicholas Bellinger 	NULL,
1774c66ac9dbSNicholas Bellinger };
1775c66ac9dbSNicholas Bellinger 
1776c66ac9dbSNicholas Bellinger static void target_core_dev_release(struct config_item *item)
1777c66ac9dbSNicholas Bellinger {
17780fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
17790fd97ccfSChristoph Hellwig 	struct se_device *dev =
17800fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
1781c66ac9dbSNicholas Bellinger 
1782c66ac9dbSNicholas Bellinger 	kfree(dev_cg->default_groups);
17830fd97ccfSChristoph Hellwig 	target_free_device(dev);
1784c66ac9dbSNicholas Bellinger }
1785c66ac9dbSNicholas Bellinger 
1786c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_show(struct config_item *item,
1787c66ac9dbSNicholas Bellinger 				     struct configfs_attribute *attr,
1788c66ac9dbSNicholas Bellinger 				     char *page)
1789c66ac9dbSNicholas Bellinger {
17900fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
17910fd97ccfSChristoph Hellwig 	struct se_device *dev =
17920fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
1793c66ac9dbSNicholas Bellinger 	struct target_core_configfs_attribute *tc_attr = container_of(
1794c66ac9dbSNicholas Bellinger 			attr, struct target_core_configfs_attribute, attr);
1795c66ac9dbSNicholas Bellinger 
17966708bb27SAndy Grover 	if (!tc_attr->show)
1797c66ac9dbSNicholas Bellinger 		return -EINVAL;
1798c66ac9dbSNicholas Bellinger 
17990fd97ccfSChristoph Hellwig 	return tc_attr->show(dev, page);
1800c66ac9dbSNicholas Bellinger }
1801c66ac9dbSNicholas Bellinger 
1802c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_store(struct config_item *item,
1803c66ac9dbSNicholas Bellinger 				      struct configfs_attribute *attr,
1804c66ac9dbSNicholas Bellinger 				      const char *page, size_t count)
1805c66ac9dbSNicholas Bellinger {
18060fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
18070fd97ccfSChristoph Hellwig 	struct se_device *dev =
18080fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
1809c66ac9dbSNicholas Bellinger 	struct target_core_configfs_attribute *tc_attr = container_of(
1810c66ac9dbSNicholas Bellinger 			attr, struct target_core_configfs_attribute, attr);
1811c66ac9dbSNicholas Bellinger 
18126708bb27SAndy Grover 	if (!tc_attr->store)
1813c66ac9dbSNicholas Bellinger 		return -EINVAL;
1814c66ac9dbSNicholas Bellinger 
18150fd97ccfSChristoph Hellwig 	return tc_attr->store(dev, page, count);
1816c66ac9dbSNicholas Bellinger }
1817c66ac9dbSNicholas Bellinger 
1818c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_item_ops = {
1819c66ac9dbSNicholas Bellinger 	.release		= target_core_dev_release,
1820c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_dev_show,
1821c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_dev_store,
1822c66ac9dbSNicholas Bellinger };
1823c66ac9dbSNicholas Bellinger 
182473112edcSNicholas Bellinger TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
1825c66ac9dbSNicholas Bellinger 
182673112edcSNicholas Bellinger /* End functions for struct config_item_type tb_dev_cit */
1827c66ac9dbSNicholas Bellinger 
1828c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1829c66ac9dbSNicholas Bellinger 
1830c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1831c66ac9dbSNicholas Bellinger #define SE_DEV_ALUA_LU_ATTR(_name, _mode)				\
1832c66ac9dbSNicholas Bellinger static struct target_core_alua_lu_gp_attribute				\
1833c66ac9dbSNicholas Bellinger 			target_core_alua_lu_gp_##_name =		\
1834c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR(_name, _mode,					\
1835c66ac9dbSNicholas Bellinger 	target_core_alua_lu_gp_show_attr_##_name,			\
1836c66ac9dbSNicholas Bellinger 	target_core_alua_lu_gp_store_attr_##_name);
1837c66ac9dbSNicholas Bellinger 
1838c66ac9dbSNicholas Bellinger #define SE_DEV_ALUA_LU_ATTR_RO(_name)					\
1839c66ac9dbSNicholas Bellinger static struct target_core_alua_lu_gp_attribute				\
1840c66ac9dbSNicholas Bellinger 			target_core_alua_lu_gp_##_name =		\
1841c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR_RO(_name,					\
1842c66ac9dbSNicholas Bellinger 	target_core_alua_lu_gp_show_attr_##_name);
1843c66ac9dbSNicholas Bellinger 
1844c66ac9dbSNicholas Bellinger /*
1845c66ac9dbSNicholas Bellinger  * lu_gp_id
1846c66ac9dbSNicholas Bellinger  */
1847c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
1848c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp,
1849c66ac9dbSNicholas Bellinger 	char *page)
1850c66ac9dbSNicholas Bellinger {
18516708bb27SAndy Grover 	if (!lu_gp->lu_gp_valid_id)
1852c66ac9dbSNicholas Bellinger 		return 0;
1853c66ac9dbSNicholas Bellinger 
1854c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
1855c66ac9dbSNicholas Bellinger }
1856c66ac9dbSNicholas Bellinger 
1857c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
1858c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp,
1859c66ac9dbSNicholas Bellinger 	const char *page,
1860c66ac9dbSNicholas Bellinger 	size_t count)
1861c66ac9dbSNicholas Bellinger {
1862c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
1863c66ac9dbSNicholas Bellinger 	unsigned long lu_gp_id;
1864c66ac9dbSNicholas Bellinger 	int ret;
1865c66ac9dbSNicholas Bellinger 
186657103d7fSJingoo Han 	ret = kstrtoul(page, 0, &lu_gp_id);
1867c66ac9dbSNicholas Bellinger 	if (ret < 0) {
186857103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
1869c66ac9dbSNicholas Bellinger 			" lu_gp_id\n", ret);
187057103d7fSJingoo Han 		return ret;
1871c66ac9dbSNicholas Bellinger 	}
1872c66ac9dbSNicholas Bellinger 	if (lu_gp_id > 0x0000ffff) {
18736708bb27SAndy Grover 		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
1874c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", lu_gp_id);
1875c66ac9dbSNicholas Bellinger 		return -EINVAL;
1876c66ac9dbSNicholas Bellinger 	}
1877c66ac9dbSNicholas Bellinger 
1878c66ac9dbSNicholas Bellinger 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
1879c66ac9dbSNicholas Bellinger 	if (ret < 0)
1880c66ac9dbSNicholas Bellinger 		return -EINVAL;
1881c66ac9dbSNicholas Bellinger 
18826708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
1883c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
1884c66ac9dbSNicholas Bellinger 		config_item_name(&alua_lu_gp_cg->cg_item),
1885c66ac9dbSNicholas Bellinger 		lu_gp->lu_gp_id);
1886c66ac9dbSNicholas Bellinger 
1887c66ac9dbSNicholas Bellinger 	return count;
1888c66ac9dbSNicholas Bellinger }
1889c66ac9dbSNicholas Bellinger 
1890c66ac9dbSNicholas Bellinger SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
1891c66ac9dbSNicholas Bellinger 
1892c66ac9dbSNicholas Bellinger /*
1893c66ac9dbSNicholas Bellinger  * members
1894c66ac9dbSNicholas Bellinger  */
1895c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_lu_gp_show_attr_members(
1896c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp,
1897c66ac9dbSNicholas Bellinger 	char *page)
1898c66ac9dbSNicholas Bellinger {
1899c66ac9dbSNicholas Bellinger 	struct se_device *dev;
1900c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
1901c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1902c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
1903c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
1904c66ac9dbSNicholas Bellinger 
1905c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
1906c66ac9dbSNicholas Bellinger 
1907c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp->lu_gp_lock);
1908c66ac9dbSNicholas Bellinger 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1909c66ac9dbSNicholas Bellinger 		dev = lu_gp_mem->lu_gp_mem_dev;
19100fd97ccfSChristoph Hellwig 		hba = dev->se_hba;
1911c66ac9dbSNicholas Bellinger 
1912c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
1913c66ac9dbSNicholas Bellinger 			config_item_name(&hba->hba_group.cg_item),
19140fd97ccfSChristoph Hellwig 			config_item_name(&dev->dev_group.cg_item));
1915c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
1916c66ac9dbSNicholas Bellinger 
1917c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
19186708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
1919c66ac9dbSNicholas Bellinger 				"_members buffer\n");
1920c66ac9dbSNicholas Bellinger 			break;
1921c66ac9dbSNicholas Bellinger 		}
1922c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
1923c66ac9dbSNicholas Bellinger 		len += cur_len;
1924c66ac9dbSNicholas Bellinger 	}
1925c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp->lu_gp_lock);
1926c66ac9dbSNicholas Bellinger 
1927c66ac9dbSNicholas Bellinger 	return len;
1928c66ac9dbSNicholas Bellinger }
1929c66ac9dbSNicholas Bellinger 
1930c66ac9dbSNicholas Bellinger SE_DEV_ALUA_LU_ATTR_RO(members);
1931c66ac9dbSNicholas Bellinger 
1932c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
1933c66ac9dbSNicholas Bellinger 
1934c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
1935c66ac9dbSNicholas Bellinger 	&target_core_alua_lu_gp_lu_gp_id.attr,
1936c66ac9dbSNicholas Bellinger 	&target_core_alua_lu_gp_members.attr,
1937c66ac9dbSNicholas Bellinger 	NULL,
1938c66ac9dbSNicholas Bellinger };
1939c66ac9dbSNicholas Bellinger 
19401f6fe7cbSNicholas Bellinger static void target_core_alua_lu_gp_release(struct config_item *item)
19411f6fe7cbSNicholas Bellinger {
19421f6fe7cbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
19431f6fe7cbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
19441f6fe7cbSNicholas Bellinger 
19451f6fe7cbSNicholas Bellinger 	core_alua_free_lu_gp(lu_gp);
19461f6fe7cbSNicholas Bellinger }
19471f6fe7cbSNicholas Bellinger 
1948c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_lu_gp_ops = {
19491f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_lu_gp_release,
1950c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_alua_lu_gp_attr_show,
1951c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_alua_lu_gp_attr_store,
1952c66ac9dbSNicholas Bellinger };
1953c66ac9dbSNicholas Bellinger 
1954c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gp_cit = {
1955c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
1956c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_lu_gp_attrs,
1957c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
1958c66ac9dbSNicholas Bellinger };
1959c66ac9dbSNicholas Bellinger 
1960c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
1961c66ac9dbSNicholas Bellinger 
1962c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
1963c66ac9dbSNicholas Bellinger 
1964c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_lu_gp(
1965c66ac9dbSNicholas Bellinger 	struct config_group *group,
1966c66ac9dbSNicholas Bellinger 	const char *name)
1967c66ac9dbSNicholas Bellinger {
1968c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
1969c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = NULL;
1970c66ac9dbSNicholas Bellinger 	struct config_item *alua_lu_gp_ci = NULL;
1971c66ac9dbSNicholas Bellinger 
1972c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp(name, 0);
1973c66ac9dbSNicholas Bellinger 	if (IS_ERR(lu_gp))
1974c66ac9dbSNicholas Bellinger 		return NULL;
1975c66ac9dbSNicholas Bellinger 
1976c66ac9dbSNicholas Bellinger 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
1977c66ac9dbSNicholas Bellinger 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
1978c66ac9dbSNicholas Bellinger 
1979c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_lu_gp_cg, name,
1980c66ac9dbSNicholas Bellinger 			&target_core_alua_lu_gp_cit);
1981c66ac9dbSNicholas Bellinger 
19826708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
1983c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s\n",
1984c66ac9dbSNicholas Bellinger 		config_item_name(alua_lu_gp_ci));
1985c66ac9dbSNicholas Bellinger 
1986c66ac9dbSNicholas Bellinger 	return alua_lu_gp_cg;
1987c66ac9dbSNicholas Bellinger 
1988c66ac9dbSNicholas Bellinger }
1989c66ac9dbSNicholas Bellinger 
1990c66ac9dbSNicholas Bellinger static void target_core_alua_drop_lu_gp(
1991c66ac9dbSNicholas Bellinger 	struct config_group *group,
1992c66ac9dbSNicholas Bellinger 	struct config_item *item)
1993c66ac9dbSNicholas Bellinger {
1994c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1995c66ac9dbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
1996c66ac9dbSNicholas Bellinger 
19976708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
1998c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
1999c66ac9dbSNicholas Bellinger 		config_item_name(item), lu_gp->lu_gp_id);
20001f6fe7cbSNicholas Bellinger 	/*
20011f6fe7cbSNicholas Bellinger 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
20021f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_lu_gp_release()
20031f6fe7cbSNicholas Bellinger 	 */
2004c66ac9dbSNicholas Bellinger 	config_item_put(item);
2005c66ac9dbSNicholas Bellinger }
2006c66ac9dbSNicholas Bellinger 
2007c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2008c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_lu_gp,
2009c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_lu_gp,
2010c66ac9dbSNicholas Bellinger };
2011c66ac9dbSNicholas Bellinger 
2012c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gps_cit = {
2013c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2014c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
2015c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2016c66ac9dbSNicholas Bellinger };
2017c66ac9dbSNicholas Bellinger 
2018c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2019c66ac9dbSNicholas Bellinger 
2020c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2021c66ac9dbSNicholas Bellinger 
2022c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2023c66ac9dbSNicholas Bellinger #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode)				\
2024c66ac9dbSNicholas Bellinger static struct target_core_alua_tg_pt_gp_attribute			\
2025c66ac9dbSNicholas Bellinger 			target_core_alua_tg_pt_gp_##_name =		\
2026c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR(_name, _mode,					\
2027c66ac9dbSNicholas Bellinger 	target_core_alua_tg_pt_gp_show_attr_##_name,			\
2028c66ac9dbSNicholas Bellinger 	target_core_alua_tg_pt_gp_store_attr_##_name);
2029c66ac9dbSNicholas Bellinger 
2030c66ac9dbSNicholas Bellinger #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name)				\
2031c66ac9dbSNicholas Bellinger static struct target_core_alua_tg_pt_gp_attribute			\
2032c66ac9dbSNicholas Bellinger 			target_core_alua_tg_pt_gp_##_name =		\
2033c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR_RO(_name,					\
2034c66ac9dbSNicholas Bellinger 	target_core_alua_tg_pt_gp_show_attr_##_name);
2035c66ac9dbSNicholas Bellinger 
2036c66ac9dbSNicholas Bellinger /*
2037c66ac9dbSNicholas Bellinger  * alua_access_state
2038c66ac9dbSNicholas Bellinger  */
2039c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2040c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2041c66ac9dbSNicholas Bellinger 	char *page)
2042c66ac9dbSNicholas Bellinger {
2043c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n",
2044c66ac9dbSNicholas Bellinger 		atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2045c66ac9dbSNicholas Bellinger }
2046c66ac9dbSNicholas Bellinger 
2047c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2048c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2049c66ac9dbSNicholas Bellinger 	const char *page,
2050c66ac9dbSNicholas Bellinger 	size_t count)
2051c66ac9dbSNicholas Bellinger {
20520fd97ccfSChristoph Hellwig 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2053c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2054c66ac9dbSNicholas Bellinger 	int new_state, ret;
2055c66ac9dbSNicholas Bellinger 
20566708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2057125d0119SHannes Reinecke 		pr_err("Unable to do implicit ALUA on non valid"
2058c66ac9dbSNicholas Bellinger 			" tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2059c66ac9dbSNicholas Bellinger 		return -EINVAL;
2060c66ac9dbSNicholas Bellinger 	}
2061f1453773SNicholas Bellinger 	if (!(dev->dev_flags & DF_CONFIGURED)) {
2062f1453773SNicholas Bellinger 		pr_err("Unable to set alua_access_state while device is"
2063f1453773SNicholas Bellinger 		       " not configured\n");
2064f1453773SNicholas Bellinger 		return -ENODEV;
2065f1453773SNicholas Bellinger 	}
2066c66ac9dbSNicholas Bellinger 
206757103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2068c66ac9dbSNicholas Bellinger 	if (ret < 0) {
20696708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access state from"
2070c66ac9dbSNicholas Bellinger 				" %s\n", page);
207157103d7fSJingoo Han 		return ret;
2072c66ac9dbSNicholas Bellinger 	}
2073c66ac9dbSNicholas Bellinger 	new_state = (int)tmp;
2074c66ac9dbSNicholas Bellinger 
2075125d0119SHannes Reinecke 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2076125d0119SHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA"
2077125d0119SHannes Reinecke 			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
2078c66ac9dbSNicholas Bellinger 		return -EINVAL;
2079c66ac9dbSNicholas Bellinger 	}
2080c66094bfSHannes Reinecke 	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2081c66094bfSHannes Reinecke 	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2082c66094bfSHannes Reinecke 		/* LBA DEPENDENT is only allowed with implicit ALUA */
2083c66094bfSHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA transition"
2084c66094bfSHannes Reinecke 		       " while explicit ALUA management is enabled\n");
2085c66094bfSHannes Reinecke 		return -EINVAL;
2086c66094bfSHannes Reinecke 	}
2087c66ac9dbSNicholas Bellinger 
20880fd97ccfSChristoph Hellwig 	ret = core_alua_do_port_transition(tg_pt_gp, dev,
2089c66ac9dbSNicholas Bellinger 					NULL, NULL, new_state, 0);
2090c66ac9dbSNicholas Bellinger 	return (!ret) ? count : -EINVAL;
2091c66ac9dbSNicholas Bellinger }
2092c66ac9dbSNicholas Bellinger 
2093c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2094c66ac9dbSNicholas Bellinger 
2095c66ac9dbSNicholas Bellinger /*
2096c66ac9dbSNicholas Bellinger  * alua_access_status
2097c66ac9dbSNicholas Bellinger  */
2098c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2099c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2100c66ac9dbSNicholas Bellinger 	char *page)
2101c66ac9dbSNicholas Bellinger {
2102c66ac9dbSNicholas Bellinger 	return sprintf(page, "%s\n",
2103c66ac9dbSNicholas Bellinger 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2104c66ac9dbSNicholas Bellinger }
2105c66ac9dbSNicholas Bellinger 
2106c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2107c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2108c66ac9dbSNicholas Bellinger 	const char *page,
2109c66ac9dbSNicholas Bellinger 	size_t count)
2110c66ac9dbSNicholas Bellinger {
2111c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2112c66ac9dbSNicholas Bellinger 	int new_status, ret;
2113c66ac9dbSNicholas Bellinger 
21146708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
21156708bb27SAndy Grover 		pr_err("Unable to do set ALUA access status on non"
2116c66ac9dbSNicholas Bellinger 			" valid tg_pt_gp ID: %hu\n",
2117c66ac9dbSNicholas Bellinger 			tg_pt_gp->tg_pt_gp_valid_id);
2118c66ac9dbSNicholas Bellinger 		return -EINVAL;
2119c66ac9dbSNicholas Bellinger 	}
2120c66ac9dbSNicholas Bellinger 
212157103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2122c66ac9dbSNicholas Bellinger 	if (ret < 0) {
21236708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access status"
2124c66ac9dbSNicholas Bellinger 				" from %s\n", page);
212557103d7fSJingoo Han 		return ret;
2126c66ac9dbSNicholas Bellinger 	}
2127c66ac9dbSNicholas Bellinger 	new_status = (int)tmp;
2128c66ac9dbSNicholas Bellinger 
2129c66ac9dbSNicholas Bellinger 	if ((new_status != ALUA_STATUS_NONE) &&
2130125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2131125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
21326708bb27SAndy Grover 		pr_err("Illegal ALUA access status: 0x%02x\n",
2133c66ac9dbSNicholas Bellinger 				new_status);
2134c66ac9dbSNicholas Bellinger 		return -EINVAL;
2135c66ac9dbSNicholas Bellinger 	}
2136c66ac9dbSNicholas Bellinger 
2137c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2138c66ac9dbSNicholas Bellinger 	return count;
2139c66ac9dbSNicholas Bellinger }
2140c66ac9dbSNicholas Bellinger 
2141c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2142c66ac9dbSNicholas Bellinger 
2143c66ac9dbSNicholas Bellinger /*
2144c66ac9dbSNicholas Bellinger  * alua_access_type
2145c66ac9dbSNicholas Bellinger  */
2146c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2147c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2148c66ac9dbSNicholas Bellinger 	char *page)
2149c66ac9dbSNicholas Bellinger {
2150c66ac9dbSNicholas Bellinger 	return core_alua_show_access_type(tg_pt_gp, page);
2151c66ac9dbSNicholas Bellinger }
2152c66ac9dbSNicholas Bellinger 
2153c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2154c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2155c66ac9dbSNicholas Bellinger 	const char *page,
2156c66ac9dbSNicholas Bellinger 	size_t count)
2157c66ac9dbSNicholas Bellinger {
2158c66ac9dbSNicholas Bellinger 	return core_alua_store_access_type(tg_pt_gp, page, count);
2159c66ac9dbSNicholas Bellinger }
2160c66ac9dbSNicholas Bellinger 
2161c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2162c66ac9dbSNicholas Bellinger 
2163c66ac9dbSNicholas Bellinger /*
21646be526c4SHannes Reinecke  * alua_supported_states
21656be526c4SHannes Reinecke  */
2166b0a382c5SHannes Reinecke 
2167b0a382c5SHannes Reinecke #define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit)		\
2168b0a382c5SHannes Reinecke static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2169b0a382c5SHannes Reinecke 	struct t10_alua_tg_pt_gp *t, char *p)				\
2170b0a382c5SHannes Reinecke {									\
2171b0a382c5SHannes Reinecke 	return sprintf(p, "%d\n", !!(t->_var & _bit));			\
21726be526c4SHannes Reinecke }
21736be526c4SHannes Reinecke 
2174b0a382c5SHannes Reinecke #define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit)		\
2175b0a382c5SHannes Reinecke static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2176b0a382c5SHannes Reinecke 	struct t10_alua_tg_pt_gp *t, const char *p, size_t c)		\
2177b0a382c5SHannes Reinecke {									\
2178b0a382c5SHannes Reinecke 	unsigned long tmp;						\
2179b0a382c5SHannes Reinecke 	int ret;							\
2180b0a382c5SHannes Reinecke 									\
2181b0a382c5SHannes Reinecke 	if (!t->tg_pt_gp_valid_id) {					\
2182b0a382c5SHannes Reinecke 		pr_err("Unable to do set ##_name ALUA state on non"	\
2183b0a382c5SHannes Reinecke 		       " valid tg_pt_gp ID: %hu\n",			\
2184b0a382c5SHannes Reinecke 		       t->tg_pt_gp_valid_id);				\
2185b0a382c5SHannes Reinecke 		return -EINVAL;						\
2186b0a382c5SHannes Reinecke 	}								\
2187b0a382c5SHannes Reinecke 									\
2188b0a382c5SHannes Reinecke 	ret = kstrtoul(p, 0, &tmp);					\
2189b0a382c5SHannes Reinecke 	if (ret < 0) {							\
2190b0a382c5SHannes Reinecke 		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
2191b0a382c5SHannes Reinecke 		return -EINVAL;						\
2192b0a382c5SHannes Reinecke 	}								\
2193b0a382c5SHannes Reinecke 	if (tmp > 1) {							\
2194b0a382c5SHannes Reinecke 		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2195b0a382c5SHannes Reinecke 		return -EINVAL;						\
2196b0a382c5SHannes Reinecke 	}								\
21971f0b030cSSebastian Herbszt 	if (tmp)							\
2198b0a382c5SHannes Reinecke 		t->_var |= _bit;					\
2199b0a382c5SHannes Reinecke 	else								\
2200b0a382c5SHannes Reinecke 		t->_var &= ~_bit;					\
2201b0a382c5SHannes Reinecke 									\
2202b0a382c5SHannes Reinecke 	return c;							\
22036be526c4SHannes Reinecke }
22046be526c4SHannes Reinecke 
2205b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2206b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2207b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2208b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2209b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
22106be526c4SHannes Reinecke 
2211b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2212b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2213b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2214b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2215b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
22166be526c4SHannes Reinecke 
2217b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2218b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2219b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2220b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2221c66094bfSHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
22226be526c4SHannes Reinecke 
2223b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2224b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2225b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2226b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2227b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
22286be526c4SHannes Reinecke 
2229b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2230b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2231b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2232b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2233b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2234b0a382c5SHannes Reinecke 
2235b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2236b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2237b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2238b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2239b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2240b0a382c5SHannes Reinecke 
2241b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2242b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2243b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2244b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2245b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
22466be526c4SHannes Reinecke 
22476be526c4SHannes Reinecke /*
2248c66ac9dbSNicholas Bellinger  * alua_write_metadata
2249c66ac9dbSNicholas Bellinger  */
2250c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2251c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2252c66ac9dbSNicholas Bellinger 	char *page)
2253c66ac9dbSNicholas Bellinger {
2254c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2255c66ac9dbSNicholas Bellinger }
2256c66ac9dbSNicholas Bellinger 
2257c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2258c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2259c66ac9dbSNicholas Bellinger 	const char *page,
2260c66ac9dbSNicholas Bellinger 	size_t count)
2261c66ac9dbSNicholas Bellinger {
2262c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2263c66ac9dbSNicholas Bellinger 	int ret;
2264c66ac9dbSNicholas Bellinger 
226557103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2266c66ac9dbSNicholas Bellinger 	if (ret < 0) {
22676708bb27SAndy Grover 		pr_err("Unable to extract alua_write_metadata\n");
226857103d7fSJingoo Han 		return ret;
2269c66ac9dbSNicholas Bellinger 	}
2270c66ac9dbSNicholas Bellinger 
2271c66ac9dbSNicholas Bellinger 	if ((tmp != 0) && (tmp != 1)) {
22726708bb27SAndy Grover 		pr_err("Illegal value for alua_write_metadata:"
2273c66ac9dbSNicholas Bellinger 			" %lu\n", tmp);
2274c66ac9dbSNicholas Bellinger 		return -EINVAL;
2275c66ac9dbSNicholas Bellinger 	}
2276c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2277c66ac9dbSNicholas Bellinger 
2278c66ac9dbSNicholas Bellinger 	return count;
2279c66ac9dbSNicholas Bellinger }
2280c66ac9dbSNicholas Bellinger 
2281c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2282c66ac9dbSNicholas Bellinger 
2283c66ac9dbSNicholas Bellinger 
2284c66ac9dbSNicholas Bellinger 
2285c66ac9dbSNicholas Bellinger /*
2286c66ac9dbSNicholas Bellinger  * nonop_delay_msecs
2287c66ac9dbSNicholas Bellinger  */
2288c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2289c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2290c66ac9dbSNicholas Bellinger 	char *page)
2291c66ac9dbSNicholas Bellinger {
2292c66ac9dbSNicholas Bellinger 	return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2293c66ac9dbSNicholas Bellinger 
2294c66ac9dbSNicholas Bellinger }
2295c66ac9dbSNicholas Bellinger 
2296c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2297c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2298c66ac9dbSNicholas Bellinger 	const char *page,
2299c66ac9dbSNicholas Bellinger 	size_t count)
2300c66ac9dbSNicholas Bellinger {
2301c66ac9dbSNicholas Bellinger 	return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2302c66ac9dbSNicholas Bellinger }
2303c66ac9dbSNicholas Bellinger 
2304c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2305c66ac9dbSNicholas Bellinger 
2306c66ac9dbSNicholas Bellinger /*
2307c66ac9dbSNicholas Bellinger  * trans_delay_msecs
2308c66ac9dbSNicholas Bellinger  */
2309c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2310c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2311c66ac9dbSNicholas Bellinger 	char *page)
2312c66ac9dbSNicholas Bellinger {
2313c66ac9dbSNicholas Bellinger 	return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2314c66ac9dbSNicholas Bellinger }
2315c66ac9dbSNicholas Bellinger 
2316c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2317c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2318c66ac9dbSNicholas Bellinger 	const char *page,
2319c66ac9dbSNicholas Bellinger 	size_t count)
2320c66ac9dbSNicholas Bellinger {
2321c66ac9dbSNicholas Bellinger 	return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2322c66ac9dbSNicholas Bellinger }
2323c66ac9dbSNicholas Bellinger 
2324c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2325c66ac9dbSNicholas Bellinger 
2326c66ac9dbSNicholas Bellinger /*
2327125d0119SHannes Reinecke  * implicit_trans_secs
23285b9a4d72SNicholas Bellinger  */
2329125d0119SHannes Reinecke static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
23305b9a4d72SNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
23315b9a4d72SNicholas Bellinger 	char *page)
23325b9a4d72SNicholas Bellinger {
2333125d0119SHannes Reinecke 	return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
23345b9a4d72SNicholas Bellinger }
23355b9a4d72SNicholas Bellinger 
2336125d0119SHannes Reinecke static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
23375b9a4d72SNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
23385b9a4d72SNicholas Bellinger 	const char *page,
23395b9a4d72SNicholas Bellinger 	size_t count)
23405b9a4d72SNicholas Bellinger {
2341125d0119SHannes Reinecke 	return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
23425b9a4d72SNicholas Bellinger }
23435b9a4d72SNicholas Bellinger 
2344125d0119SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
23455b9a4d72SNicholas Bellinger 
23465b9a4d72SNicholas Bellinger /*
2347c66ac9dbSNicholas Bellinger  * preferred
2348c66ac9dbSNicholas Bellinger  */
2349c66ac9dbSNicholas Bellinger 
2350c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2351c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2352c66ac9dbSNicholas Bellinger 	char *page)
2353c66ac9dbSNicholas Bellinger {
2354c66ac9dbSNicholas Bellinger 	return core_alua_show_preferred_bit(tg_pt_gp, page);
2355c66ac9dbSNicholas Bellinger }
2356c66ac9dbSNicholas Bellinger 
2357c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2358c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2359c66ac9dbSNicholas Bellinger 	const char *page,
2360c66ac9dbSNicholas Bellinger 	size_t count)
2361c66ac9dbSNicholas Bellinger {
2362c66ac9dbSNicholas Bellinger 	return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2363c66ac9dbSNicholas Bellinger }
2364c66ac9dbSNicholas Bellinger 
2365c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2366c66ac9dbSNicholas Bellinger 
2367c66ac9dbSNicholas Bellinger /*
2368c66ac9dbSNicholas Bellinger  * tg_pt_gp_id
2369c66ac9dbSNicholas Bellinger  */
2370c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2371c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2372c66ac9dbSNicholas Bellinger 	char *page)
2373c66ac9dbSNicholas Bellinger {
23746708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id)
2375c66ac9dbSNicholas Bellinger 		return 0;
2376c66ac9dbSNicholas Bellinger 
2377c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2378c66ac9dbSNicholas Bellinger }
2379c66ac9dbSNicholas Bellinger 
2380c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2381c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2382c66ac9dbSNicholas Bellinger 	const char *page,
2383c66ac9dbSNicholas Bellinger 	size_t count)
2384c66ac9dbSNicholas Bellinger {
2385c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2386c66ac9dbSNicholas Bellinger 	unsigned long tg_pt_gp_id;
2387c66ac9dbSNicholas Bellinger 	int ret;
2388c66ac9dbSNicholas Bellinger 
238957103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tg_pt_gp_id);
2390c66ac9dbSNicholas Bellinger 	if (ret < 0) {
239157103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2392c66ac9dbSNicholas Bellinger 			" tg_pt_gp_id\n", ret);
239357103d7fSJingoo Han 		return ret;
2394c66ac9dbSNicholas Bellinger 	}
2395c66ac9dbSNicholas Bellinger 	if (tg_pt_gp_id > 0x0000ffff) {
23966708bb27SAndy Grover 		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2397c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", tg_pt_gp_id);
2398c66ac9dbSNicholas Bellinger 		return -EINVAL;
2399c66ac9dbSNicholas Bellinger 	}
2400c66ac9dbSNicholas Bellinger 
2401c66ac9dbSNicholas Bellinger 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2402c66ac9dbSNicholas Bellinger 	if (ret < 0)
2403c66ac9dbSNicholas Bellinger 		return -EINVAL;
2404c66ac9dbSNicholas Bellinger 
24056708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2406c66ac9dbSNicholas Bellinger 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
2407c66ac9dbSNicholas Bellinger 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
2408c66ac9dbSNicholas Bellinger 		tg_pt_gp->tg_pt_gp_id);
2409c66ac9dbSNicholas Bellinger 
2410c66ac9dbSNicholas Bellinger 	return count;
2411c66ac9dbSNicholas Bellinger }
2412c66ac9dbSNicholas Bellinger 
2413c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2414c66ac9dbSNicholas Bellinger 
2415c66ac9dbSNicholas Bellinger /*
2416c66ac9dbSNicholas Bellinger  * members
2417c66ac9dbSNicholas Bellinger  */
2418c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2419c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2420c66ac9dbSNicholas Bellinger 	char *page)
2421c66ac9dbSNicholas Bellinger {
2422c66ac9dbSNicholas Bellinger 	struct se_port *port;
2423c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg;
2424c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
2425c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2426c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2427c66ac9dbSNicholas Bellinger 	unsigned char buf[TG_PT_GROUP_NAME_BUF];
2428c66ac9dbSNicholas Bellinger 
2429c66ac9dbSNicholas Bellinger 	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2430c66ac9dbSNicholas Bellinger 
2431c66ac9dbSNicholas Bellinger 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2432c66ac9dbSNicholas Bellinger 	list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2433c66ac9dbSNicholas Bellinger 			tg_pt_gp_mem_list) {
2434c66ac9dbSNicholas Bellinger 		port = tg_pt_gp_mem->tg_pt;
2435c66ac9dbSNicholas Bellinger 		tpg = port->sep_tpg;
2436c66ac9dbSNicholas Bellinger 		lun = port->sep_lun;
2437c66ac9dbSNicholas Bellinger 
2438c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2439e3d6f909SAndy Grover 			"/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2440e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2441e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
2442c66ac9dbSNicholas Bellinger 			config_item_name(&lun->lun_group.cg_item));
2443c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2444c66ac9dbSNicholas Bellinger 
2445c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
24466708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2447c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2448c66ac9dbSNicholas Bellinger 			break;
2449c66ac9dbSNicholas Bellinger 		}
2450c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2451c66ac9dbSNicholas Bellinger 		len += cur_len;
2452c66ac9dbSNicholas Bellinger 	}
2453c66ac9dbSNicholas Bellinger 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2454c66ac9dbSNicholas Bellinger 
2455c66ac9dbSNicholas Bellinger 	return len;
2456c66ac9dbSNicholas Bellinger }
2457c66ac9dbSNicholas Bellinger 
2458c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2459c66ac9dbSNicholas Bellinger 
2460c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2461c66ac9dbSNicholas Bellinger 			tg_pt_gp_group);
2462c66ac9dbSNicholas Bellinger 
2463c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2464c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_alua_access_state.attr,
2465c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_alua_access_status.attr,
2466c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_alua_access_type.attr,
2467b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2468b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_offline.attr,
2469b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2470b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2471b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_standby.attr,
2472b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2473b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
2474c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2475c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2476c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2477125d0119SHannes Reinecke 	&target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
2478c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_preferred.attr,
2479c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2480c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_members.attr,
2481c66ac9dbSNicholas Bellinger 	NULL,
2482c66ac9dbSNicholas Bellinger };
2483c66ac9dbSNicholas Bellinger 
24841f6fe7cbSNicholas Bellinger static void target_core_alua_tg_pt_gp_release(struct config_item *item)
24851f6fe7cbSNicholas Bellinger {
24861f6fe7cbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
24871f6fe7cbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
24881f6fe7cbSNicholas Bellinger 
24891f6fe7cbSNicholas Bellinger 	core_alua_free_tg_pt_gp(tg_pt_gp);
24901f6fe7cbSNicholas Bellinger }
24911f6fe7cbSNicholas Bellinger 
2492c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
24931f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_tg_pt_gp_release,
2494c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_alua_tg_pt_gp_attr_show,
2495c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_alua_tg_pt_gp_attr_store,
2496c66ac9dbSNicholas Bellinger };
2497c66ac9dbSNicholas Bellinger 
2498c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2499c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
2500c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
2501c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2502c66ac9dbSNicholas Bellinger };
2503c66ac9dbSNicholas Bellinger 
2504c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2505c66ac9dbSNicholas Bellinger 
250672aca57bSNicholas Bellinger /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2507c66ac9dbSNicholas Bellinger 
2508c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_tg_pt_gp(
2509c66ac9dbSNicholas Bellinger 	struct config_group *group,
2510c66ac9dbSNicholas Bellinger 	const char *name)
2511c66ac9dbSNicholas Bellinger {
2512c66ac9dbSNicholas Bellinger 	struct t10_alua *alua = container_of(group, struct t10_alua,
2513c66ac9dbSNicholas Bellinger 					alua_tg_pt_gps_group);
2514c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2515c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = NULL;
2516c66ac9dbSNicholas Bellinger 	struct config_item *alua_tg_pt_gp_ci = NULL;
2517c66ac9dbSNicholas Bellinger 
25180fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
25196708bb27SAndy Grover 	if (!tg_pt_gp)
2520c66ac9dbSNicholas Bellinger 		return NULL;
2521c66ac9dbSNicholas Bellinger 
2522c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2523c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2524c66ac9dbSNicholas Bellinger 
2525c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
2526c66ac9dbSNicholas Bellinger 			&target_core_alua_tg_pt_gp_cit);
2527c66ac9dbSNicholas Bellinger 
25286708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2529c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s\n",
2530c66ac9dbSNicholas Bellinger 		config_item_name(alua_tg_pt_gp_ci));
2531c66ac9dbSNicholas Bellinger 
2532c66ac9dbSNicholas Bellinger 	return alua_tg_pt_gp_cg;
2533c66ac9dbSNicholas Bellinger }
2534c66ac9dbSNicholas Bellinger 
2535c66ac9dbSNicholas Bellinger static void target_core_alua_drop_tg_pt_gp(
2536c66ac9dbSNicholas Bellinger 	struct config_group *group,
2537c66ac9dbSNicholas Bellinger 	struct config_item *item)
2538c66ac9dbSNicholas Bellinger {
2539c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2540c66ac9dbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2541c66ac9dbSNicholas Bellinger 
25426708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2543c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
2544c66ac9dbSNicholas Bellinger 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
25451f6fe7cbSNicholas Bellinger 	/*
25461f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
25471f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_tg_pt_gp_release().
25481f6fe7cbSNicholas Bellinger 	 */
2549c66ac9dbSNicholas Bellinger 	config_item_put(item);
2550c66ac9dbSNicholas Bellinger }
2551c66ac9dbSNicholas Bellinger 
2552c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2553c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_tg_pt_gp,
2554c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
2555c66ac9dbSNicholas Bellinger };
2556c66ac9dbSNicholas Bellinger 
255772aca57bSNicholas Bellinger TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
2558c66ac9dbSNicholas Bellinger 
255972aca57bSNicholas Bellinger /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2560c66ac9dbSNicholas Bellinger 
2561c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_cit */
2562c66ac9dbSNicholas Bellinger 
2563c66ac9dbSNicholas Bellinger /*
2564c66ac9dbSNicholas Bellinger  * target_core_alua_cit is a ConfigFS group that lives under
2565c66ac9dbSNicholas Bellinger  * /sys/kernel/config/target/core/alua.  There are default groups
2566c66ac9dbSNicholas Bellinger  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2567c66ac9dbSNicholas Bellinger  * target_core_alua_cit in target_core_init_configfs() below.
2568c66ac9dbSNicholas Bellinger  */
2569c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_cit = {
2570c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2571c66ac9dbSNicholas Bellinger 	.ct_attrs		= NULL,
2572c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2573c66ac9dbSNicholas Bellinger };
2574c66ac9dbSNicholas Bellinger 
2575c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_cit */
2576c66ac9dbSNicholas Bellinger 
2577d23ab570SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_stat_cit */
257812d23384SNicholas Bellinger 
257912d23384SNicholas Bellinger static struct config_group *target_core_stat_mkdir(
258012d23384SNicholas Bellinger 	struct config_group *group,
258112d23384SNicholas Bellinger 	const char *name)
258212d23384SNicholas Bellinger {
258312d23384SNicholas Bellinger 	return ERR_PTR(-ENOSYS);
258412d23384SNicholas Bellinger }
258512d23384SNicholas Bellinger 
258612d23384SNicholas Bellinger static void target_core_stat_rmdir(
258712d23384SNicholas Bellinger 	struct config_group *group,
258812d23384SNicholas Bellinger 	struct config_item *item)
258912d23384SNicholas Bellinger {
259012d23384SNicholas Bellinger 	return;
259112d23384SNicholas Bellinger }
259212d23384SNicholas Bellinger 
259312d23384SNicholas Bellinger static struct configfs_group_operations target_core_stat_group_ops = {
259412d23384SNicholas Bellinger 	.make_group		= &target_core_stat_mkdir,
259512d23384SNicholas Bellinger 	.drop_item		= &target_core_stat_rmdir,
259612d23384SNicholas Bellinger };
259712d23384SNicholas Bellinger 
2598d23ab570SNicholas Bellinger TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
259912d23384SNicholas Bellinger 
2600d23ab570SNicholas Bellinger /* End functions for struct config_item_type tb_dev_stat_cit */
260112d23384SNicholas Bellinger 
2602c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_hba_cit */
2603c66ac9dbSNicholas Bellinger 
2604c66ac9dbSNicholas Bellinger static struct config_group *target_core_make_subdev(
2605c66ac9dbSNicholas Bellinger 	struct config_group *group,
2606c66ac9dbSNicholas Bellinger 	const char *name)
2607c66ac9dbSNicholas Bellinger {
2608c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2609c66ac9dbSNicholas Bellinger 	struct se_subsystem_api *t;
2610c66ac9dbSNicholas Bellinger 	struct config_item *hba_ci = &group->cg_item;
2611c66ac9dbSNicholas Bellinger 	struct se_hba *hba = item_to_hba(hba_ci);
26120fd97ccfSChristoph Hellwig 	struct se_device *dev;
2613c66ac9dbSNicholas Bellinger 	struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
261412d23384SNicholas Bellinger 	struct config_group *dev_stat_grp = NULL;
261512d23384SNicholas Bellinger 	int errno = -ENOMEM, ret;
2616c66ac9dbSNicholas Bellinger 
261712d23384SNicholas Bellinger 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
261812d23384SNicholas Bellinger 	if (ret)
261912d23384SNicholas Bellinger 		return ERR_PTR(ret);
2620c66ac9dbSNicholas Bellinger 	/*
2621c66ac9dbSNicholas Bellinger 	 * Locate the struct se_subsystem_api from parent's struct se_hba.
2622c66ac9dbSNicholas Bellinger 	 */
2623c66ac9dbSNicholas Bellinger 	t = hba->transport;
2624c66ac9dbSNicholas Bellinger 
26250fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, name);
26260fd97ccfSChristoph Hellwig 	if (!dev)
26270fd97ccfSChristoph Hellwig 		goto out_unlock;
2628c66ac9dbSNicholas Bellinger 
26290fd97ccfSChristoph Hellwig 	dev_cg = &dev->dev_group;
2630c66ac9dbSNicholas Bellinger 
263113f6a914SSebastian Andrzej Siewior 	dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
2632c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
26336708bb27SAndy Grover 	if (!dev_cg->default_groups)
26340fd97ccfSChristoph Hellwig 		goto out_free_device;
2635c66ac9dbSNicholas Bellinger 
263673112edcSNicholas Bellinger 	config_group_init_type_name(dev_cg, name, &t->tb_cits.tb_dev_cit);
26370fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
2638f79a897eSNicholas Bellinger 			&t->tb_cits.tb_dev_attrib_cit);
26390fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_pr_group, "pr",
264091e2e39bSNicholas Bellinger 			&t->tb_cits.tb_dev_pr_cit);
26410fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
2642f8d389c6SNicholas Bellinger 			&t->tb_cits.tb_dev_wwn_cit);
26430fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
264472aca57bSNicholas Bellinger 			"alua", &t->tb_cits.tb_dev_alua_tg_pt_gps_cit);
26450fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
2646d23ab570SNicholas Bellinger 			"statistics", &t->tb_cits.tb_dev_stat_cit);
264712d23384SNicholas Bellinger 
26480fd97ccfSChristoph Hellwig 	dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
26490fd97ccfSChristoph Hellwig 	dev_cg->default_groups[1] = &dev->dev_pr_group;
26500fd97ccfSChristoph Hellwig 	dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
26510fd97ccfSChristoph Hellwig 	dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
26520fd97ccfSChristoph Hellwig 	dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
265312d23384SNicholas Bellinger 	dev_cg->default_groups[5] = NULL;
2654c66ac9dbSNicholas Bellinger 	/*
265512d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2656c66ac9dbSNicholas Bellinger 	 */
26570fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
26586708bb27SAndy Grover 	if (!tg_pt_gp)
26590fd97ccfSChristoph Hellwig 		goto out_free_dev_cg_default_groups;
26600fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2661c66ac9dbSNicholas Bellinger 
26620fd97ccfSChristoph Hellwig 	tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
266313f6a914SSebastian Andrzej Siewior 	tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2664c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
26656708bb27SAndy Grover 	if (!tg_pt_gp_cg->default_groups) {
26666708bb27SAndy Grover 		pr_err("Unable to allocate tg_pt_gp_cg->"
2667c66ac9dbSNicholas Bellinger 				"default_groups\n");
26680fd97ccfSChristoph Hellwig 		goto out_free_tg_pt_gp;
2669c66ac9dbSNicholas Bellinger 	}
2670c66ac9dbSNicholas Bellinger 
2671c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2672c66ac9dbSNicholas Bellinger 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2673c66ac9dbSNicholas Bellinger 	tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2674c66ac9dbSNicholas Bellinger 	tg_pt_gp_cg->default_groups[1] = NULL;
267512d23384SNicholas Bellinger 	/*
267612d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/statistics/ default groups
267712d23384SNicholas Bellinger 	 */
26780fd97ccfSChristoph Hellwig 	dev_stat_grp = &dev->dev_stat_grps.stat_group;
267913f6a914SSebastian Andrzej Siewior 	dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
268012d23384SNicholas Bellinger 				GFP_KERNEL);
268112d23384SNicholas Bellinger 	if (!dev_stat_grp->default_groups) {
26826708bb27SAndy Grover 		pr_err("Unable to allocate dev_stat_grp->default_groups\n");
26830fd97ccfSChristoph Hellwig 		goto out_free_tg_pt_gp_cg_default_groups;
268412d23384SNicholas Bellinger 	}
26850fd97ccfSChristoph Hellwig 	target_stat_setup_dev_default_groups(dev);
2686c66ac9dbSNicholas Bellinger 
2687c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
26880fd97ccfSChristoph Hellwig 	return dev_cg;
26890fd97ccfSChristoph Hellwig 
26900fd97ccfSChristoph Hellwig out_free_tg_pt_gp_cg_default_groups:
2691c66ac9dbSNicholas Bellinger 	kfree(tg_pt_gp_cg->default_groups);
26920fd97ccfSChristoph Hellwig out_free_tg_pt_gp:
26930fd97ccfSChristoph Hellwig 	core_alua_free_tg_pt_gp(tg_pt_gp);
26940fd97ccfSChristoph Hellwig out_free_dev_cg_default_groups:
2695c66ac9dbSNicholas Bellinger 	kfree(dev_cg->default_groups);
26960fd97ccfSChristoph Hellwig out_free_device:
26970fd97ccfSChristoph Hellwig 	target_free_device(dev);
26980fd97ccfSChristoph Hellwig out_unlock:
2699c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
270012d23384SNicholas Bellinger 	return ERR_PTR(errno);
2701c66ac9dbSNicholas Bellinger }
2702c66ac9dbSNicholas Bellinger 
2703c66ac9dbSNicholas Bellinger static void target_core_drop_subdev(
2704c66ac9dbSNicholas Bellinger 	struct config_group *group,
2705c66ac9dbSNicholas Bellinger 	struct config_item *item)
2706c66ac9dbSNicholas Bellinger {
27070fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
27080fd97ccfSChristoph Hellwig 	struct se_device *dev =
27090fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2710c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2711c66ac9dbSNicholas Bellinger 	struct config_item *df_item;
27120fd97ccfSChristoph Hellwig 	struct config_group *tg_pt_gp_cg, *dev_stat_grp;
27131f6fe7cbSNicholas Bellinger 	int i;
2714c66ac9dbSNicholas Bellinger 
27150fd97ccfSChristoph Hellwig 	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
2716c66ac9dbSNicholas Bellinger 
27171f6fe7cbSNicholas Bellinger 	mutex_lock(&hba->hba_access_mutex);
2718c66ac9dbSNicholas Bellinger 
27190fd97ccfSChristoph Hellwig 	dev_stat_grp = &dev->dev_stat_grps.stat_group;
272012d23384SNicholas Bellinger 	for (i = 0; dev_stat_grp->default_groups[i]; i++) {
272112d23384SNicholas Bellinger 		df_item = &dev_stat_grp->default_groups[i]->cg_item;
272212d23384SNicholas Bellinger 		dev_stat_grp->default_groups[i] = NULL;
272312d23384SNicholas Bellinger 		config_item_put(df_item);
272412d23384SNicholas Bellinger 	}
272512d23384SNicholas Bellinger 	kfree(dev_stat_grp->default_groups);
272612d23384SNicholas Bellinger 
27270fd97ccfSChristoph Hellwig 	tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2728c66ac9dbSNicholas Bellinger 	for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2729c66ac9dbSNicholas Bellinger 		df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2730c66ac9dbSNicholas Bellinger 		tg_pt_gp_cg->default_groups[i] = NULL;
2731c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
2732c66ac9dbSNicholas Bellinger 	}
2733c66ac9dbSNicholas Bellinger 	kfree(tg_pt_gp_cg->default_groups);
27341f6fe7cbSNicholas Bellinger 	/*
27351f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
27361f6fe7cbSNicholas Bellinger 	 * directly from target_core_alua_tg_pt_gp_release().
27371f6fe7cbSNicholas Bellinger 	 */
27380fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = NULL;
2739c66ac9dbSNicholas Bellinger 
2740c66ac9dbSNicholas Bellinger 	for (i = 0; dev_cg->default_groups[i]; i++) {
2741c66ac9dbSNicholas Bellinger 		df_item = &dev_cg->default_groups[i]->cg_item;
2742c66ac9dbSNicholas Bellinger 		dev_cg->default_groups[i] = NULL;
2743c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
2744c66ac9dbSNicholas Bellinger 	}
27451f6fe7cbSNicholas Bellinger 	/*
27460fd97ccfSChristoph Hellwig 	 * se_dev is released from target_core_dev_item_ops->release()
27471f6fe7cbSNicholas Bellinger 	 */
2748c66ac9dbSNicholas Bellinger 	config_item_put(item);
2749c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
2750c66ac9dbSNicholas Bellinger }
2751c66ac9dbSNicholas Bellinger 
2752c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_hba_group_ops = {
2753c66ac9dbSNicholas Bellinger 	.make_group		= target_core_make_subdev,
2754c66ac9dbSNicholas Bellinger 	.drop_item		= target_core_drop_subdev,
2755c66ac9dbSNicholas Bellinger };
2756c66ac9dbSNicholas Bellinger 
2757c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2758c66ac9dbSNicholas Bellinger #define SE_HBA_ATTR(_name, _mode)				\
2759c66ac9dbSNicholas Bellinger static struct target_core_hba_attribute				\
2760c66ac9dbSNicholas Bellinger 		target_core_hba_##_name =			\
2761c66ac9dbSNicholas Bellinger 		__CONFIGFS_EATTR(_name, _mode,			\
2762c66ac9dbSNicholas Bellinger 		target_core_hba_show_attr_##_name,		\
2763c66ac9dbSNicholas Bellinger 		target_core_hba_store_attr_##_name);
2764c66ac9dbSNicholas Bellinger 
2765c66ac9dbSNicholas Bellinger #define SE_HBA_ATTR_RO(_name)					\
2766c66ac9dbSNicholas Bellinger static struct target_core_hba_attribute				\
2767c66ac9dbSNicholas Bellinger 		target_core_hba_##_name =			\
2768c66ac9dbSNicholas Bellinger 		__CONFIGFS_EATTR_RO(_name,			\
2769c66ac9dbSNicholas Bellinger 		target_core_hba_show_attr_##_name);
2770c66ac9dbSNicholas Bellinger 
2771c66ac9dbSNicholas Bellinger static ssize_t target_core_hba_show_attr_hba_info(
2772c66ac9dbSNicholas Bellinger 	struct se_hba *hba,
2773c66ac9dbSNicholas Bellinger 	char *page)
2774c66ac9dbSNicholas Bellinger {
2775c66ac9dbSNicholas Bellinger 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2776c66ac9dbSNicholas Bellinger 			hba->hba_id, hba->transport->name,
2777c66ac9dbSNicholas Bellinger 			TARGET_CORE_CONFIGFS_VERSION);
2778c66ac9dbSNicholas Bellinger }
2779c66ac9dbSNicholas Bellinger 
2780c66ac9dbSNicholas Bellinger SE_HBA_ATTR_RO(hba_info);
2781c66ac9dbSNicholas Bellinger 
2782c66ac9dbSNicholas Bellinger static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2783c66ac9dbSNicholas Bellinger 				char *page)
2784c66ac9dbSNicholas Bellinger {
2785c66ac9dbSNicholas Bellinger 	int hba_mode = 0;
2786c66ac9dbSNicholas Bellinger 
2787c66ac9dbSNicholas Bellinger 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2788c66ac9dbSNicholas Bellinger 		hba_mode = 1;
2789c66ac9dbSNicholas Bellinger 
2790c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", hba_mode);
2791c66ac9dbSNicholas Bellinger }
2792c66ac9dbSNicholas Bellinger 
2793c66ac9dbSNicholas Bellinger static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2794c66ac9dbSNicholas Bellinger 				const char *page, size_t count)
2795c66ac9dbSNicholas Bellinger {
2796c66ac9dbSNicholas Bellinger 	struct se_subsystem_api *transport = hba->transport;
2797c66ac9dbSNicholas Bellinger 	unsigned long mode_flag;
2798c66ac9dbSNicholas Bellinger 	int ret;
2799c66ac9dbSNicholas Bellinger 
2800c66ac9dbSNicholas Bellinger 	if (transport->pmode_enable_hba == NULL)
2801c66ac9dbSNicholas Bellinger 		return -EINVAL;
2802c66ac9dbSNicholas Bellinger 
280357103d7fSJingoo Han 	ret = kstrtoul(page, 0, &mode_flag);
2804c66ac9dbSNicholas Bellinger 	if (ret < 0) {
28056708bb27SAndy Grover 		pr_err("Unable to extract hba mode flag: %d\n", ret);
280657103d7fSJingoo Han 		return ret;
2807c66ac9dbSNicholas Bellinger 	}
2808c66ac9dbSNicholas Bellinger 
28090fd97ccfSChristoph Hellwig 	if (hba->dev_count) {
28106708bb27SAndy Grover 		pr_err("Unable to set hba_mode with active devices\n");
2811c66ac9dbSNicholas Bellinger 		return -EINVAL;
2812c66ac9dbSNicholas Bellinger 	}
2813c66ac9dbSNicholas Bellinger 
2814c66ac9dbSNicholas Bellinger 	ret = transport->pmode_enable_hba(hba, mode_flag);
2815c66ac9dbSNicholas Bellinger 	if (ret < 0)
2816c66ac9dbSNicholas Bellinger 		return -EINVAL;
2817c66ac9dbSNicholas Bellinger 	if (ret > 0)
2818c66ac9dbSNicholas Bellinger 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2819c66ac9dbSNicholas Bellinger 	else if (ret == 0)
2820c66ac9dbSNicholas Bellinger 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2821c66ac9dbSNicholas Bellinger 
2822c66ac9dbSNicholas Bellinger 	return count;
2823c66ac9dbSNicholas Bellinger }
2824c66ac9dbSNicholas Bellinger 
2825c66ac9dbSNicholas Bellinger SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2826c66ac9dbSNicholas Bellinger 
2827c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2828c66ac9dbSNicholas Bellinger 
28291f6fe7cbSNicholas Bellinger static void target_core_hba_release(struct config_item *item)
28301f6fe7cbSNicholas Bellinger {
28311f6fe7cbSNicholas Bellinger 	struct se_hba *hba = container_of(to_config_group(item),
28321f6fe7cbSNicholas Bellinger 				struct se_hba, hba_group);
28331f6fe7cbSNicholas Bellinger 	core_delete_hba(hba);
28341f6fe7cbSNicholas Bellinger }
28351f6fe7cbSNicholas Bellinger 
2836c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_hba_attrs[] = {
2837c66ac9dbSNicholas Bellinger 	&target_core_hba_hba_info.attr,
2838c66ac9dbSNicholas Bellinger 	&target_core_hba_hba_mode.attr,
2839c66ac9dbSNicholas Bellinger 	NULL,
2840c66ac9dbSNicholas Bellinger };
2841c66ac9dbSNicholas Bellinger 
2842c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_hba_item_ops = {
28431f6fe7cbSNicholas Bellinger 	.release		= target_core_hba_release,
2844c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_hba_attr_show,
2845c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_hba_attr_store,
2846c66ac9dbSNicholas Bellinger };
2847c66ac9dbSNicholas Bellinger 
2848c66ac9dbSNicholas Bellinger static struct config_item_type target_core_hba_cit = {
2849c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_hba_item_ops,
2850c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_hba_group_ops,
2851c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_hba_attrs,
2852c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2853c66ac9dbSNicholas Bellinger };
2854c66ac9dbSNicholas Bellinger 
2855c66ac9dbSNicholas Bellinger static struct config_group *target_core_call_addhbatotarget(
2856c66ac9dbSNicholas Bellinger 	struct config_group *group,
2857c66ac9dbSNicholas Bellinger 	const char *name)
2858c66ac9dbSNicholas Bellinger {
2859c66ac9dbSNicholas Bellinger 	char *se_plugin_str, *str, *str2;
2860c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2861c66ac9dbSNicholas Bellinger 	char buf[TARGET_CORE_NAME_MAX_LEN];
2862c66ac9dbSNicholas Bellinger 	unsigned long plugin_dep_id = 0;
2863c66ac9dbSNicholas Bellinger 	int ret;
2864c66ac9dbSNicholas Bellinger 
2865c66ac9dbSNicholas Bellinger 	memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
286660d645a4SDan Carpenter 	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
28676708bb27SAndy Grover 		pr_err("Passed *name strlen(): %d exceeds"
2868c66ac9dbSNicholas Bellinger 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
2869c66ac9dbSNicholas Bellinger 			TARGET_CORE_NAME_MAX_LEN);
2870c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENAMETOOLONG);
2871c66ac9dbSNicholas Bellinger 	}
2872c66ac9dbSNicholas Bellinger 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
2873c66ac9dbSNicholas Bellinger 
2874c66ac9dbSNicholas Bellinger 	str = strstr(buf, "_");
28756708bb27SAndy Grover 	if (!str) {
28766708bb27SAndy Grover 		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
2877c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
2878c66ac9dbSNicholas Bellinger 	}
2879c66ac9dbSNicholas Bellinger 	se_plugin_str = buf;
2880c66ac9dbSNicholas Bellinger 	/*
2881c66ac9dbSNicholas Bellinger 	 * Special case for subsystem plugins that have "_" in their names.
2882c66ac9dbSNicholas Bellinger 	 * Namely rd_direct and rd_mcp..
2883c66ac9dbSNicholas Bellinger 	 */
2884c66ac9dbSNicholas Bellinger 	str2 = strstr(str+1, "_");
28856708bb27SAndy Grover 	if (str2) {
2886c66ac9dbSNicholas Bellinger 		*str2 = '\0'; /* Terminate for *se_plugin_str */
2887c66ac9dbSNicholas Bellinger 		str2++; /* Skip to start of plugin dependent ID */
2888c66ac9dbSNicholas Bellinger 		str = str2;
2889c66ac9dbSNicholas Bellinger 	} else {
2890c66ac9dbSNicholas Bellinger 		*str = '\0'; /* Terminate for *se_plugin_str */
2891c66ac9dbSNicholas Bellinger 		str++; /* Skip to start of plugin dependent ID */
2892c66ac9dbSNicholas Bellinger 	}
2893c66ac9dbSNicholas Bellinger 
289457103d7fSJingoo Han 	ret = kstrtoul(str, 0, &plugin_dep_id);
2895c66ac9dbSNicholas Bellinger 	if (ret < 0) {
289657103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2897c66ac9dbSNicholas Bellinger 				" plugin_dep_id\n", ret);
289857103d7fSJingoo Han 		return ERR_PTR(ret);
2899c66ac9dbSNicholas Bellinger 	}
2900c66ac9dbSNicholas Bellinger 	/*
2901c66ac9dbSNicholas Bellinger 	 * Load up TCM subsystem plugins if they have not already been loaded.
2902c66ac9dbSNicholas Bellinger 	 */
2903dbc5623eSNicholas Bellinger 	transport_subsystem_check_init();
2904c66ac9dbSNicholas Bellinger 
2905c66ac9dbSNicholas Bellinger 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
2906c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
2907c66ac9dbSNicholas Bellinger 		return ERR_CAST(hba);
2908c66ac9dbSNicholas Bellinger 
2909c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&hba->hba_group, name,
2910c66ac9dbSNicholas Bellinger 			&target_core_hba_cit);
2911c66ac9dbSNicholas Bellinger 
2912c66ac9dbSNicholas Bellinger 	return &hba->hba_group;
2913c66ac9dbSNicholas Bellinger }
2914c66ac9dbSNicholas Bellinger 
2915c66ac9dbSNicholas Bellinger static void target_core_call_delhbafromtarget(
2916c66ac9dbSNicholas Bellinger 	struct config_group *group,
2917c66ac9dbSNicholas Bellinger 	struct config_item *item)
2918c66ac9dbSNicholas Bellinger {
29191f6fe7cbSNicholas Bellinger 	/*
29201f6fe7cbSNicholas Bellinger 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
29211f6fe7cbSNicholas Bellinger 	 * -> target_core_hba_release()
29221f6fe7cbSNicholas Bellinger 	 */
2923c66ac9dbSNicholas Bellinger 	config_item_put(item);
2924c66ac9dbSNicholas Bellinger }
2925c66ac9dbSNicholas Bellinger 
2926c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_group_ops = {
2927c66ac9dbSNicholas Bellinger 	.make_group	= target_core_call_addhbatotarget,
2928c66ac9dbSNicholas Bellinger 	.drop_item	= target_core_call_delhbafromtarget,
2929c66ac9dbSNicholas Bellinger };
2930c66ac9dbSNicholas Bellinger 
2931c66ac9dbSNicholas Bellinger static struct config_item_type target_core_cit = {
2932c66ac9dbSNicholas Bellinger 	.ct_item_ops	= NULL,
2933c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_group_ops,
2934c66ac9dbSNicholas Bellinger 	.ct_attrs	= NULL,
2935c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
2936c66ac9dbSNicholas Bellinger };
2937c66ac9dbSNicholas Bellinger 
2938c66ac9dbSNicholas Bellinger /* Stop functions for struct config_item_type target_core_hba_cit */
2939c66ac9dbSNicholas Bellinger 
294073112edcSNicholas Bellinger void target_core_setup_sub_cits(struct se_subsystem_api *sa)
294173112edcSNicholas Bellinger {
294273112edcSNicholas Bellinger 	target_core_setup_dev_cit(sa);
2943f79a897eSNicholas Bellinger 	target_core_setup_dev_attrib_cit(sa);
294491e2e39bSNicholas Bellinger 	target_core_setup_dev_pr_cit(sa);
2945f8d389c6SNicholas Bellinger 	target_core_setup_dev_wwn_cit(sa);
294672aca57bSNicholas Bellinger 	target_core_setup_dev_alua_tg_pt_gps_cit(sa);
2947d23ab570SNicholas Bellinger 	target_core_setup_dev_stat_cit(sa);
294873112edcSNicholas Bellinger }
294973112edcSNicholas Bellinger EXPORT_SYMBOL(target_core_setup_sub_cits);
295073112edcSNicholas Bellinger 
295154550fabSAxel Lin static int __init target_core_init_configfs(void)
2952c66ac9dbSNicholas Bellinger {
2953c66ac9dbSNicholas Bellinger 	struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
2954c66ac9dbSNicholas Bellinger 	struct config_group *lu_gp_cg = NULL;
2955c66ac9dbSNicholas Bellinger 	struct configfs_subsystem *subsys;
2956c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2957c66ac9dbSNicholas Bellinger 	int ret;
2958c66ac9dbSNicholas Bellinger 
29596708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
2960c66ac9dbSNicholas Bellinger 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
2961c66ac9dbSNicholas Bellinger 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
2962c66ac9dbSNicholas Bellinger 
2963c66ac9dbSNicholas Bellinger 	subsys = target_core_subsystem[0];
2964c66ac9dbSNicholas Bellinger 	config_group_init(&subsys->su_group);
2965c66ac9dbSNicholas Bellinger 	mutex_init(&subsys->su_mutex);
2966c66ac9dbSNicholas Bellinger 
2967e3d6f909SAndy Grover 	ret = init_se_kmem_caches();
2968c66ac9dbSNicholas Bellinger 	if (ret < 0)
2969e3d6f909SAndy Grover 		return ret;
2970c66ac9dbSNicholas Bellinger 	/*
2971c66ac9dbSNicholas Bellinger 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
2972c66ac9dbSNicholas Bellinger 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
2973c66ac9dbSNicholas Bellinger 	 */
2974c66ac9dbSNicholas Bellinger 	target_cg = &subsys->su_group;
2975ab6dae82SAndy Grover 	target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2976c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
29776708bb27SAndy Grover 	if (!target_cg->default_groups) {
29786708bb27SAndy Grover 		pr_err("Unable to allocate target_cg->default_groups\n");
297937bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
2980c66ac9dbSNicholas Bellinger 		goto out_global;
2981c66ac9dbSNicholas Bellinger 	}
2982c66ac9dbSNicholas Bellinger 
2983e3d6f909SAndy Grover 	config_group_init_type_name(&target_core_hbagroup,
2984c66ac9dbSNicholas Bellinger 			"core", &target_core_cit);
2985e3d6f909SAndy Grover 	target_cg->default_groups[0] = &target_core_hbagroup;
2986c66ac9dbSNicholas Bellinger 	target_cg->default_groups[1] = NULL;
2987c66ac9dbSNicholas Bellinger 	/*
2988c66ac9dbSNicholas Bellinger 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
2989c66ac9dbSNicholas Bellinger 	 */
2990e3d6f909SAndy Grover 	hba_cg = &target_core_hbagroup;
299113f6a914SSebastian Andrzej Siewior 	hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2992c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
29936708bb27SAndy Grover 	if (!hba_cg->default_groups) {
29946708bb27SAndy Grover 		pr_err("Unable to allocate hba_cg->default_groups\n");
299537bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
2996c66ac9dbSNicholas Bellinger 		goto out_global;
2997c66ac9dbSNicholas Bellinger 	}
2998e3d6f909SAndy Grover 	config_group_init_type_name(&alua_group,
2999c66ac9dbSNicholas Bellinger 			"alua", &target_core_alua_cit);
3000e3d6f909SAndy Grover 	hba_cg->default_groups[0] = &alua_group;
3001c66ac9dbSNicholas Bellinger 	hba_cg->default_groups[1] = NULL;
3002c66ac9dbSNicholas Bellinger 	/*
3003c66ac9dbSNicholas Bellinger 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3004c66ac9dbSNicholas Bellinger 	 * groups under /sys/kernel/config/target/core/alua/
3005c66ac9dbSNicholas Bellinger 	 */
3006e3d6f909SAndy Grover 	alua_cg = &alua_group;
300713f6a914SSebastian Andrzej Siewior 	alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3008c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
30096708bb27SAndy Grover 	if (!alua_cg->default_groups) {
30106708bb27SAndy Grover 		pr_err("Unable to allocate alua_cg->default_groups\n");
301137bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3012c66ac9dbSNicholas Bellinger 		goto out_global;
3013c66ac9dbSNicholas Bellinger 	}
3014c66ac9dbSNicholas Bellinger 
3015e3d6f909SAndy Grover 	config_group_init_type_name(&alua_lu_gps_group,
3016c66ac9dbSNicholas Bellinger 			"lu_gps", &target_core_alua_lu_gps_cit);
3017e3d6f909SAndy Grover 	alua_cg->default_groups[0] = &alua_lu_gps_group;
3018c66ac9dbSNicholas Bellinger 	alua_cg->default_groups[1] = NULL;
3019c66ac9dbSNicholas Bellinger 	/*
3020c66ac9dbSNicholas Bellinger 	 * Add core/alua/lu_gps/default_lu_gp
3021c66ac9dbSNicholas Bellinger 	 */
3022c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
302337bb7899SPeter Senna Tschudin 	if (IS_ERR(lu_gp)) {
302437bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3025c66ac9dbSNicholas Bellinger 		goto out_global;
302637bb7899SPeter Senna Tschudin 	}
3027c66ac9dbSNicholas Bellinger 
3028e3d6f909SAndy Grover 	lu_gp_cg = &alua_lu_gps_group;
302913f6a914SSebastian Andrzej Siewior 	lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3030c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
30316708bb27SAndy Grover 	if (!lu_gp_cg->default_groups) {
30326708bb27SAndy Grover 		pr_err("Unable to allocate lu_gp_cg->default_groups\n");
303337bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3034c66ac9dbSNicholas Bellinger 		goto out_global;
3035c66ac9dbSNicholas Bellinger 	}
3036c66ac9dbSNicholas Bellinger 
3037c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3038c66ac9dbSNicholas Bellinger 				&target_core_alua_lu_gp_cit);
3039c66ac9dbSNicholas Bellinger 	lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3040c66ac9dbSNicholas Bellinger 	lu_gp_cg->default_groups[1] = NULL;
3041e3d6f909SAndy Grover 	default_lu_gp = lu_gp;
3042c66ac9dbSNicholas Bellinger 	/*
3043c66ac9dbSNicholas Bellinger 	 * Register the target_core_mod subsystem with configfs.
3044c66ac9dbSNicholas Bellinger 	 */
3045c66ac9dbSNicholas Bellinger 	ret = configfs_register_subsystem(subsys);
3046c66ac9dbSNicholas Bellinger 	if (ret < 0) {
30476708bb27SAndy Grover 		pr_err("Error %d while registering subsystem %s\n",
3048c66ac9dbSNicholas Bellinger 			ret, subsys->su_group.cg_item.ci_namebuf);
3049c66ac9dbSNicholas Bellinger 		goto out_global;
3050c66ac9dbSNicholas Bellinger 	}
30516708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3052c66ac9dbSNicholas Bellinger 		" Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3053c66ac9dbSNicholas Bellinger 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3054c66ac9dbSNicholas Bellinger 	/*
3055c66ac9dbSNicholas Bellinger 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3056c66ac9dbSNicholas Bellinger 	 */
3057c66ac9dbSNicholas Bellinger 	ret = rd_module_init();
3058c66ac9dbSNicholas Bellinger 	if (ret < 0)
3059c66ac9dbSNicholas Bellinger 		goto out;
3060c66ac9dbSNicholas Bellinger 
30610d0f9dfbSRoland Dreier 	ret = core_dev_setup_virtual_lun0();
30620d0f9dfbSRoland Dreier 	if (ret < 0)
3063c66ac9dbSNicholas Bellinger 		goto out;
3064c66ac9dbSNicholas Bellinger 
3065f99715acSNicholas Bellinger 	ret = target_xcopy_setup_pt();
3066f99715acSNicholas Bellinger 	if (ret < 0)
3067f99715acSNicholas Bellinger 		goto out;
3068f99715acSNicholas Bellinger 
3069c66ac9dbSNicholas Bellinger 	return 0;
3070c66ac9dbSNicholas Bellinger 
3071c66ac9dbSNicholas Bellinger out:
3072c66ac9dbSNicholas Bellinger 	configfs_unregister_subsystem(subsys);
3073c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3074c66ac9dbSNicholas Bellinger 	rd_module_exit();
3075c66ac9dbSNicholas Bellinger out_global:
3076e3d6f909SAndy Grover 	if (default_lu_gp) {
3077e3d6f909SAndy Grover 		core_alua_free_lu_gp(default_lu_gp);
3078e3d6f909SAndy Grover 		default_lu_gp = NULL;
3079c66ac9dbSNicholas Bellinger 	}
3080c66ac9dbSNicholas Bellinger 	if (lu_gp_cg)
3081c66ac9dbSNicholas Bellinger 		kfree(lu_gp_cg->default_groups);
3082c66ac9dbSNicholas Bellinger 	if (alua_cg)
3083c66ac9dbSNicholas Bellinger 		kfree(alua_cg->default_groups);
3084c66ac9dbSNicholas Bellinger 	if (hba_cg)
3085c66ac9dbSNicholas Bellinger 		kfree(hba_cg->default_groups);
3086c66ac9dbSNicholas Bellinger 	kfree(target_cg->default_groups);
3087e3d6f909SAndy Grover 	release_se_kmem_caches();
3088e3d6f909SAndy Grover 	return ret;
3089c66ac9dbSNicholas Bellinger }
3090c66ac9dbSNicholas Bellinger 
309154550fabSAxel Lin static void __exit target_core_exit_configfs(void)
3092c66ac9dbSNicholas Bellinger {
3093c66ac9dbSNicholas Bellinger 	struct configfs_subsystem *subsys;
3094c66ac9dbSNicholas Bellinger 	struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3095c66ac9dbSNicholas Bellinger 	struct config_item *item;
3096c66ac9dbSNicholas Bellinger 	int i;
3097c66ac9dbSNicholas Bellinger 
3098c66ac9dbSNicholas Bellinger 	subsys = target_core_subsystem[0];
3099c66ac9dbSNicholas Bellinger 
3100e3d6f909SAndy Grover 	lu_gp_cg = &alua_lu_gps_group;
3101c66ac9dbSNicholas Bellinger 	for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3102c66ac9dbSNicholas Bellinger 		item = &lu_gp_cg->default_groups[i]->cg_item;
3103c66ac9dbSNicholas Bellinger 		lu_gp_cg->default_groups[i] = NULL;
3104c66ac9dbSNicholas Bellinger 		config_item_put(item);
3105c66ac9dbSNicholas Bellinger 	}
3106c66ac9dbSNicholas Bellinger 	kfree(lu_gp_cg->default_groups);
31077c2bf6e9SNicholas Bellinger 	lu_gp_cg->default_groups = NULL;
3108c66ac9dbSNicholas Bellinger 
3109e3d6f909SAndy Grover 	alua_cg = &alua_group;
3110c66ac9dbSNicholas Bellinger 	for (i = 0; alua_cg->default_groups[i]; i++) {
3111c66ac9dbSNicholas Bellinger 		item = &alua_cg->default_groups[i]->cg_item;
3112c66ac9dbSNicholas Bellinger 		alua_cg->default_groups[i] = NULL;
3113c66ac9dbSNicholas Bellinger 		config_item_put(item);
3114c66ac9dbSNicholas Bellinger 	}
3115c66ac9dbSNicholas Bellinger 	kfree(alua_cg->default_groups);
31167c2bf6e9SNicholas Bellinger 	alua_cg->default_groups = NULL;
3117c66ac9dbSNicholas Bellinger 
3118e3d6f909SAndy Grover 	hba_cg = &target_core_hbagroup;
3119c66ac9dbSNicholas Bellinger 	for (i = 0; hba_cg->default_groups[i]; i++) {
3120c66ac9dbSNicholas Bellinger 		item = &hba_cg->default_groups[i]->cg_item;
3121c66ac9dbSNicholas Bellinger 		hba_cg->default_groups[i] = NULL;
3122c66ac9dbSNicholas Bellinger 		config_item_put(item);
3123c66ac9dbSNicholas Bellinger 	}
3124c66ac9dbSNicholas Bellinger 	kfree(hba_cg->default_groups);
31257c2bf6e9SNicholas Bellinger 	hba_cg->default_groups = NULL;
31267c2bf6e9SNicholas Bellinger 	/*
31277c2bf6e9SNicholas Bellinger 	 * We expect subsys->su_group.default_groups to be released
31287c2bf6e9SNicholas Bellinger 	 * by configfs subsystem provider logic..
31297c2bf6e9SNicholas Bellinger 	 */
31307c2bf6e9SNicholas Bellinger 	configfs_unregister_subsystem(subsys);
3131c66ac9dbSNicholas Bellinger 	kfree(subsys->su_group.default_groups);
3132c66ac9dbSNicholas Bellinger 
3133e3d6f909SAndy Grover 	core_alua_free_lu_gp(default_lu_gp);
3134e3d6f909SAndy Grover 	default_lu_gp = NULL;
31357c2bf6e9SNicholas Bellinger 
31366708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3137c66ac9dbSNicholas Bellinger 			" Infrastructure\n");
3138c66ac9dbSNicholas Bellinger 
3139c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3140c66ac9dbSNicholas Bellinger 	rd_module_exit();
3141f99715acSNicholas Bellinger 	target_xcopy_release_pt();
3142e3d6f909SAndy Grover 	release_se_kmem_caches();
3143c66ac9dbSNicholas Bellinger }
3144c66ac9dbSNicholas Bellinger 
3145c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3146c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3147c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
3148c66ac9dbSNicholas Bellinger 
3149c66ac9dbSNicholas Bellinger module_init(target_core_init_configfs);
3150c66ac9dbSNicholas Bellinger module_exit(target_core_exit_configfs);
3151