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) {
1190dc2e8d1SChristoph Hellwig 		if (!strcmp(tf->tf_ops->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) {
14562554910SNicholas Bellinger 		pr_debug("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) {
16862554910SNicholas Bellinger 				pr_debug("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) {
18162554910SNicholas Bellinger 				pr_debug("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) {
19162554910SNicholas Bellinger 		pr_debug("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:"
1960dc2e8d1SChristoph Hellwig 			" %s\n", tf->tf_ops->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 	return &tf->tf_group;
216c66ac9dbSNicholas Bellinger }
217c66ac9dbSNicholas Bellinger 
218c66ac9dbSNicholas Bellinger /*
219c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->drop_item()
220c66ac9dbSNicholas Bellinger  */
221c66ac9dbSNicholas Bellinger static void target_core_deregister_fabric(
222c66ac9dbSNicholas Bellinger 	struct config_group *group,
223c66ac9dbSNicholas Bellinger 	struct config_item *item)
224c66ac9dbSNicholas Bellinger {
225c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf = container_of(
226c66ac9dbSNicholas Bellinger 		to_config_group(item), struct target_fabric_configfs, tf_group);
227c66ac9dbSNicholas Bellinger 	struct config_group *tf_group;
228c66ac9dbSNicholas Bellinger 	struct config_item *df_item;
229c66ac9dbSNicholas Bellinger 	int i;
230c66ac9dbSNicholas Bellinger 
2316708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
232c66ac9dbSNicholas Bellinger 		" tf list\n", config_item_name(item));
233c66ac9dbSNicholas Bellinger 
2346708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
2350dc2e8d1SChristoph Hellwig 			" %s\n", tf->tf_ops->name);
236c66ac9dbSNicholas Bellinger 	atomic_dec(&tf->tf_access_cnt);
237c66ac9dbSNicholas Bellinger 
2386708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
239c66ac9dbSNicholas Bellinger 			" %s\n", config_item_name(item));
240c66ac9dbSNicholas Bellinger 
241c66ac9dbSNicholas Bellinger 	tf_group = &tf->tf_group;
242c66ac9dbSNicholas Bellinger 	for (i = 0; tf_group->default_groups[i]; i++) {
243c66ac9dbSNicholas Bellinger 		df_item = &tf_group->default_groups[i]->cg_item;
244c66ac9dbSNicholas Bellinger 		tf_group->default_groups[i] = NULL;
245c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
246c66ac9dbSNicholas Bellinger 	}
247c66ac9dbSNicholas Bellinger 	config_item_put(item);
248c66ac9dbSNicholas Bellinger }
249c66ac9dbSNicholas Bellinger 
250c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_fabric_group_ops = {
251c66ac9dbSNicholas Bellinger 	.make_group	= &target_core_register_fabric,
252c66ac9dbSNicholas Bellinger 	.drop_item	= &target_core_deregister_fabric,
253c66ac9dbSNicholas Bellinger };
254c66ac9dbSNicholas Bellinger 
255c66ac9dbSNicholas Bellinger /*
256c66ac9dbSNicholas Bellinger  * All item attributes appearing in /sys/kernel/target/ appear here.
257c66ac9dbSNicholas Bellinger  */
258c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_fabric_item_attrs[] = {
259c66ac9dbSNicholas Bellinger 	&target_core_item_attr_version,
260c66ac9dbSNicholas Bellinger 	NULL,
261c66ac9dbSNicholas Bellinger };
262c66ac9dbSNicholas Bellinger 
263c66ac9dbSNicholas Bellinger /*
264c66ac9dbSNicholas Bellinger  * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
265c66ac9dbSNicholas Bellinger  */
266c66ac9dbSNicholas Bellinger static struct config_item_type target_core_fabrics_item = {
267c66ac9dbSNicholas Bellinger 	.ct_item_ops	= &target_core_fabric_item_ops,
268c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_fabric_group_ops,
269c66ac9dbSNicholas Bellinger 	.ct_attrs	= target_core_fabric_item_attrs,
270c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
271c66ac9dbSNicholas Bellinger };
272c66ac9dbSNicholas Bellinger 
273c66ac9dbSNicholas Bellinger static struct configfs_subsystem target_core_fabrics = {
274c66ac9dbSNicholas Bellinger 	.su_group = {
275c66ac9dbSNicholas Bellinger 		.cg_item = {
276c66ac9dbSNicholas Bellinger 			.ci_namebuf = "target",
277c66ac9dbSNicholas Bellinger 			.ci_type = &target_core_fabrics_item,
278c66ac9dbSNicholas Bellinger 		},
279c66ac9dbSNicholas Bellinger 	},
280c66ac9dbSNicholas Bellinger };
281c66ac9dbSNicholas Bellinger 
282d588cf8fSChristoph Hellwig int target_depend_item(struct config_item *item)
283d588cf8fSChristoph Hellwig {
284d588cf8fSChristoph Hellwig 	return configfs_depend_item(&target_core_fabrics, item);
285d588cf8fSChristoph Hellwig }
286d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_depend_item);
287d588cf8fSChristoph Hellwig 
288d588cf8fSChristoph Hellwig void target_undepend_item(struct config_item *item)
289d588cf8fSChristoph Hellwig {
290d588cf8fSChristoph Hellwig 	return configfs_undepend_item(&target_core_fabrics, item);
291d588cf8fSChristoph Hellwig }
292d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_undepend_item);
293c66ac9dbSNicholas Bellinger 
294c66ac9dbSNicholas Bellinger /*##############################################################################
295c66ac9dbSNicholas Bellinger // Start functions called by external Target Fabrics Modules
296c66ac9dbSNicholas Bellinger //############################################################################*/
297c66ac9dbSNicholas Bellinger 
2989ac8928eSChristoph Hellwig static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
299c66ac9dbSNicholas Bellinger {
3009ac8928eSChristoph Hellwig 	if (!tfo->name) {
3019ac8928eSChristoph Hellwig 		pr_err("Missing tfo->name\n");
3029ac8928eSChristoph Hellwig 		return -EINVAL;
303c66ac9dbSNicholas Bellinger 	}
3049ac8928eSChristoph Hellwig 	if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
3056708bb27SAndy Grover 		pr_err("Passed name: %s exceeds TARGET_FABRIC"
3069ac8928eSChristoph Hellwig 			"_NAME_SIZE\n", tfo->name);
3079ac8928eSChristoph Hellwig 		return -EINVAL;
308c66ac9dbSNicholas Bellinger 	}
3096708bb27SAndy Grover 	if (!tfo->get_fabric_name) {
3106708bb27SAndy Grover 		pr_err("Missing tfo->get_fabric_name()\n");
311c66ac9dbSNicholas Bellinger 		return -EINVAL;
312c66ac9dbSNicholas Bellinger 	}
3136708bb27SAndy Grover 	if (!tfo->tpg_get_wwn) {
3146708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_wwn()\n");
315c66ac9dbSNicholas Bellinger 		return -EINVAL;
316c66ac9dbSNicholas Bellinger 	}
3176708bb27SAndy Grover 	if (!tfo->tpg_get_tag) {
3186708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_tag()\n");
319c66ac9dbSNicholas Bellinger 		return -EINVAL;
320c66ac9dbSNicholas Bellinger 	}
3216708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode) {
3226708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode()\n");
323c66ac9dbSNicholas Bellinger 		return -EINVAL;
324c66ac9dbSNicholas Bellinger 	}
3256708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_cache) {
3266708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
327c66ac9dbSNicholas Bellinger 		return -EINVAL;
328c66ac9dbSNicholas Bellinger 	}
3296708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_write_protect) {
3306708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
331c66ac9dbSNicholas Bellinger 		return -EINVAL;
332c66ac9dbSNicholas Bellinger 	}
3336708bb27SAndy Grover 	if (!tfo->tpg_check_prod_mode_write_protect) {
3346708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
335c66ac9dbSNicholas Bellinger 		return -EINVAL;
336c66ac9dbSNicholas Bellinger 	}
3376708bb27SAndy Grover 	if (!tfo->tpg_get_inst_index) {
3386708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_inst_index()\n");
339c66ac9dbSNicholas Bellinger 		return -EINVAL;
340c66ac9dbSNicholas Bellinger 	}
34135462975SChristoph Hellwig 	if (!tfo->release_cmd) {
3426708bb27SAndy Grover 		pr_err("Missing tfo->release_cmd()\n");
343c66ac9dbSNicholas Bellinger 		return -EINVAL;
344c66ac9dbSNicholas Bellinger 	}
3456708bb27SAndy Grover 	if (!tfo->shutdown_session) {
3466708bb27SAndy Grover 		pr_err("Missing tfo->shutdown_session()\n");
347c66ac9dbSNicholas Bellinger 		return -EINVAL;
348c66ac9dbSNicholas Bellinger 	}
3496708bb27SAndy Grover 	if (!tfo->close_session) {
3506708bb27SAndy Grover 		pr_err("Missing tfo->close_session()\n");
351c66ac9dbSNicholas Bellinger 		return -EINVAL;
352c66ac9dbSNicholas Bellinger 	}
3536708bb27SAndy Grover 	if (!tfo->sess_get_index) {
3546708bb27SAndy Grover 		pr_err("Missing tfo->sess_get_index()\n");
355c66ac9dbSNicholas Bellinger 		return -EINVAL;
356c66ac9dbSNicholas Bellinger 	}
3576708bb27SAndy Grover 	if (!tfo->write_pending) {
3586708bb27SAndy Grover 		pr_err("Missing tfo->write_pending()\n");
359c66ac9dbSNicholas Bellinger 		return -EINVAL;
360c66ac9dbSNicholas Bellinger 	}
3616708bb27SAndy Grover 	if (!tfo->write_pending_status) {
3626708bb27SAndy Grover 		pr_err("Missing tfo->write_pending_status()\n");
363c66ac9dbSNicholas Bellinger 		return -EINVAL;
364c66ac9dbSNicholas Bellinger 	}
3656708bb27SAndy Grover 	if (!tfo->set_default_node_attributes) {
3666708bb27SAndy Grover 		pr_err("Missing tfo->set_default_node_attributes()\n");
367c66ac9dbSNicholas Bellinger 		return -EINVAL;
368c66ac9dbSNicholas Bellinger 	}
3696708bb27SAndy Grover 	if (!tfo->get_cmd_state) {
3706708bb27SAndy Grover 		pr_err("Missing tfo->get_cmd_state()\n");
371c66ac9dbSNicholas Bellinger 		return -EINVAL;
372c66ac9dbSNicholas Bellinger 	}
3736708bb27SAndy Grover 	if (!tfo->queue_data_in) {
3746708bb27SAndy Grover 		pr_err("Missing tfo->queue_data_in()\n");
375c66ac9dbSNicholas Bellinger 		return -EINVAL;
376c66ac9dbSNicholas Bellinger 	}
3776708bb27SAndy Grover 	if (!tfo->queue_status) {
3786708bb27SAndy Grover 		pr_err("Missing tfo->queue_status()\n");
379c66ac9dbSNicholas Bellinger 		return -EINVAL;
380c66ac9dbSNicholas Bellinger 	}
3816708bb27SAndy Grover 	if (!tfo->queue_tm_rsp) {
3826708bb27SAndy Grover 		pr_err("Missing tfo->queue_tm_rsp()\n");
383c66ac9dbSNicholas Bellinger 		return -EINVAL;
384c66ac9dbSNicholas Bellinger 	}
385131e6abcSNicholas Bellinger 	if (!tfo->aborted_task) {
386131e6abcSNicholas Bellinger 		pr_err("Missing tfo->aborted_task()\n");
387131e6abcSNicholas Bellinger 		return -EINVAL;
388131e6abcSNicholas Bellinger 	}
389c66ac9dbSNicholas Bellinger 	/*
390c66ac9dbSNicholas Bellinger 	 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
391c66ac9dbSNicholas Bellinger 	 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
392c66ac9dbSNicholas Bellinger 	 * target_core_fabric_configfs.c WWN+TPG group context code.
393c66ac9dbSNicholas Bellinger 	 */
3946708bb27SAndy Grover 	if (!tfo->fabric_make_wwn) {
3956708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_wwn()\n");
396c66ac9dbSNicholas Bellinger 		return -EINVAL;
397c66ac9dbSNicholas Bellinger 	}
3986708bb27SAndy Grover 	if (!tfo->fabric_drop_wwn) {
3996708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_wwn()\n");
400c66ac9dbSNicholas Bellinger 		return -EINVAL;
401c66ac9dbSNicholas Bellinger 	}
4026708bb27SAndy Grover 	if (!tfo->fabric_make_tpg) {
4036708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_tpg()\n");
404c66ac9dbSNicholas Bellinger 		return -EINVAL;
405c66ac9dbSNicholas Bellinger 	}
4066708bb27SAndy Grover 	if (!tfo->fabric_drop_tpg) {
4076708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_tpg()\n");
408c66ac9dbSNicholas Bellinger 		return -EINVAL;
409c66ac9dbSNicholas Bellinger 	}
410c66ac9dbSNicholas Bellinger 
411c66ac9dbSNicholas Bellinger 	return 0;
412c66ac9dbSNicholas Bellinger }
413c66ac9dbSNicholas Bellinger 
4149ac8928eSChristoph Hellwig int target_register_template(const struct target_core_fabric_ops *fo)
415c66ac9dbSNicholas Bellinger {
4169ac8928eSChristoph Hellwig 	struct target_fabric_configfs *tf;
417c66ac9dbSNicholas Bellinger 	int ret;
418c66ac9dbSNicholas Bellinger 
4199ac8928eSChristoph Hellwig 	ret = target_fabric_tf_ops_check(fo);
4209ac8928eSChristoph Hellwig 	if (ret)
421c66ac9dbSNicholas Bellinger 		return ret;
422c66ac9dbSNicholas Bellinger 
4239ac8928eSChristoph Hellwig 	tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
4249ac8928eSChristoph Hellwig 	if (!tf) {
4259ac8928eSChristoph Hellwig 		pr_err("%s: could not allocate memory!\n", __func__);
4269ac8928eSChristoph Hellwig 		return -ENOMEM;
4279ac8928eSChristoph Hellwig 	}
4289ac8928eSChristoph Hellwig 
4299ac8928eSChristoph Hellwig 	INIT_LIST_HEAD(&tf->tf_list);
4309ac8928eSChristoph Hellwig 	atomic_set(&tf->tf_access_cnt, 0);
431ef0caf8dSChristoph Hellwig 	tf->tf_ops = fo;
4329ac8928eSChristoph Hellwig 	target_fabric_setup_cits(tf);
4339ac8928eSChristoph Hellwig 
4349ac8928eSChristoph Hellwig 	mutex_lock(&g_tf_lock);
4359ac8928eSChristoph Hellwig 	list_add_tail(&tf->tf_list, &g_tf_list);
4369ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4379ac8928eSChristoph Hellwig 
438c66ac9dbSNicholas Bellinger 	return 0;
439c66ac9dbSNicholas Bellinger }
4409ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_register_template);
441c66ac9dbSNicholas Bellinger 
4429ac8928eSChristoph Hellwig void target_unregister_template(const struct target_core_fabric_ops *fo)
443c66ac9dbSNicholas Bellinger {
4449ac8928eSChristoph Hellwig 	struct target_fabric_configfs *t;
445c66ac9dbSNicholas Bellinger 
446c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
4479ac8928eSChristoph Hellwig 	list_for_each_entry(t, &g_tf_list, tf_list) {
4480dc2e8d1SChristoph Hellwig 		if (!strcmp(t->tf_ops->name, fo->name)) {
4499ac8928eSChristoph Hellwig 			BUG_ON(atomic_read(&t->tf_access_cnt));
4509ac8928eSChristoph Hellwig 			list_del(&t->tf_list);
4519ac8928eSChristoph Hellwig 			kfree(t);
4529ac8928eSChristoph Hellwig 			break;
453c66ac9dbSNicholas Bellinger 		}
454c66ac9dbSNicholas Bellinger 	}
4559ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4569ac8928eSChristoph Hellwig }
4579ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_unregister_template);
458c66ac9dbSNicholas Bellinger 
459c66ac9dbSNicholas Bellinger /*##############################################################################
460c66ac9dbSNicholas Bellinger // Stop functions called by external Target Fabrics Modules
461c66ac9dbSNicholas Bellinger //############################################################################*/
462c66ac9dbSNicholas Bellinger 
463f79a897eSNicholas Bellinger /* Start functions for struct config_item_type tb_dev_attrib_cit */
464c66ac9dbSNicholas Bellinger 
465c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
466c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
467c66ac9dbSNicholas Bellinger 
468c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_attrib_ops = {
469c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_dev_attrib_attr_show,
470c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_dev_attrib_attr_store,
471c66ac9dbSNicholas Bellinger };
472c66ac9dbSNicholas Bellinger 
47343cf208cSNicholas Bellinger TB_CIT_SETUP(dev_attrib, &target_core_dev_attrib_ops, NULL, NULL);
474c66ac9dbSNicholas Bellinger 
475f79a897eSNicholas Bellinger /* End functions for struct config_item_type tb_dev_attrib_cit */
476c66ac9dbSNicholas Bellinger 
477f8d389c6SNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_wwn_cit */
478c66ac9dbSNicholas Bellinger 
479c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
480c66ac9dbSNicholas Bellinger #define SE_DEV_WWN_ATTR(_name, _mode)					\
481c66ac9dbSNicholas Bellinger static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
482c66ac9dbSNicholas Bellinger 		__CONFIGFS_EATTR(_name, _mode,				\
483c66ac9dbSNicholas Bellinger 		target_core_dev_wwn_show_attr_##_name,			\
484c66ac9dbSNicholas Bellinger 		target_core_dev_wwn_store_attr_##_name);
485c66ac9dbSNicholas Bellinger 
486c66ac9dbSNicholas Bellinger #define SE_DEV_WWN_ATTR_RO(_name);					\
487c66ac9dbSNicholas Bellinger do {									\
488c66ac9dbSNicholas Bellinger 	static struct target_core_dev_wwn_attribute			\
489c66ac9dbSNicholas Bellinger 			target_core_dev_wwn_##_name =			\
490c66ac9dbSNicholas Bellinger 		__CONFIGFS_EATTR_RO(_name,				\
491c66ac9dbSNicholas Bellinger 		target_core_dev_wwn_show_attr_##_name);			\
492c66ac9dbSNicholas Bellinger } while (0);
493c66ac9dbSNicholas Bellinger 
494c66ac9dbSNicholas Bellinger /*
495c66ac9dbSNicholas Bellinger  * VPD page 0x80 Unit serial
496c66ac9dbSNicholas Bellinger  */
497c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
498c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
499c66ac9dbSNicholas Bellinger 	char *page)
500c66ac9dbSNicholas Bellinger {
501c66ac9dbSNicholas Bellinger 	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
502c66ac9dbSNicholas Bellinger 		&t10_wwn->unit_serial[0]);
503c66ac9dbSNicholas Bellinger }
504c66ac9dbSNicholas Bellinger 
505c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
506c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
507c66ac9dbSNicholas Bellinger 	const char *page,
508c66ac9dbSNicholas Bellinger 	size_t count)
509c66ac9dbSNicholas Bellinger {
5100fd97ccfSChristoph Hellwig 	struct se_device *dev = t10_wwn->t10_dev;
511c66ac9dbSNicholas Bellinger 	unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
512c66ac9dbSNicholas Bellinger 
513c66ac9dbSNicholas Bellinger 	/*
514c66ac9dbSNicholas Bellinger 	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
515c66ac9dbSNicholas Bellinger 	 * from the struct scsi_device level firmware, do not allow
516c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial to be emulated.
517c66ac9dbSNicholas Bellinger 	 *
518c66ac9dbSNicholas Bellinger 	 * Note this struct scsi_device could also be emulating VPD
519c66ac9dbSNicholas Bellinger 	 * information from its drivers/scsi LLD.  But for now we assume
520c66ac9dbSNicholas Bellinger 	 * it is doing 'the right thing' wrt a world wide unique
521c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial Number that OS dependent multipath can depend on.
522c66ac9dbSNicholas Bellinger 	 */
5230fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
5246708bb27SAndy Grover 		pr_err("Underlying SCSI device firmware provided VPD"
525c66ac9dbSNicholas Bellinger 			" Unit Serial, ignoring request\n");
526c66ac9dbSNicholas Bellinger 		return -EOPNOTSUPP;
527c66ac9dbSNicholas Bellinger 	}
528c66ac9dbSNicholas Bellinger 
52960d645a4SDan Carpenter 	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
5306708bb27SAndy Grover 		pr_err("Emulated VPD Unit Serial exceeds"
531c66ac9dbSNicholas Bellinger 		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
532c66ac9dbSNicholas Bellinger 		return -EOVERFLOW;
533c66ac9dbSNicholas Bellinger 	}
534c66ac9dbSNicholas Bellinger 	/*
535c66ac9dbSNicholas Bellinger 	 * Check to see if any active $FABRIC_MOD exports exist.  If they
536c66ac9dbSNicholas Bellinger 	 * do exist, fail here as changing this information on the fly
537c66ac9dbSNicholas Bellinger 	 * (underneath the initiator side OS dependent multipath code)
538c66ac9dbSNicholas Bellinger 	 * could cause negative effects.
539c66ac9dbSNicholas Bellinger 	 */
5400fd97ccfSChristoph Hellwig 	if (dev->export_count) {
5416708bb27SAndy Grover 		pr_err("Unable to set VPD Unit Serial while"
542c66ac9dbSNicholas Bellinger 			" active %d $FABRIC_MOD exports exist\n",
5430fd97ccfSChristoph Hellwig 			dev->export_count);
544c66ac9dbSNicholas Bellinger 		return -EINVAL;
545c66ac9dbSNicholas Bellinger 	}
5460fd97ccfSChristoph Hellwig 
547c66ac9dbSNicholas Bellinger 	/*
548c66ac9dbSNicholas Bellinger 	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
549c66ac9dbSNicholas Bellinger 	 *
550c66ac9dbSNicholas Bellinger 	 * Also, strip any newline added from the userspace
551c66ac9dbSNicholas Bellinger 	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
552c66ac9dbSNicholas Bellinger 	 */
553c66ac9dbSNicholas Bellinger 	memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
554c66ac9dbSNicholas Bellinger 	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
5550fd97ccfSChristoph Hellwig 	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
556c66ac9dbSNicholas Bellinger 			"%s", strstrip(buf));
5570fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
558c66ac9dbSNicholas Bellinger 
5596708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
5600fd97ccfSChristoph Hellwig 			" %s\n", dev->t10_wwn.unit_serial);
561c66ac9dbSNicholas Bellinger 
562c66ac9dbSNicholas Bellinger 	return count;
563c66ac9dbSNicholas Bellinger }
564c66ac9dbSNicholas Bellinger 
565c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
566c66ac9dbSNicholas Bellinger 
567c66ac9dbSNicholas Bellinger /*
568c66ac9dbSNicholas Bellinger  * VPD page 0x83 Protocol Identifier
569c66ac9dbSNicholas Bellinger  */
570c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
571c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
572c66ac9dbSNicholas Bellinger 	char *page)
573c66ac9dbSNicholas Bellinger {
574c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;
575c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];
576c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
577c66ac9dbSNicholas Bellinger 
578c66ac9dbSNicholas Bellinger 	memset(buf, 0, VPD_TMP_BUF_SIZE);
579c66ac9dbSNicholas Bellinger 
580c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);
581c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
5826708bb27SAndy Grover 		if (!vpd->protocol_identifier_set)
583c66ac9dbSNicholas Bellinger 			continue;
584c66ac9dbSNicholas Bellinger 
585c66ac9dbSNicholas Bellinger 		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
586c66ac9dbSNicholas Bellinger 
5876708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
588c66ac9dbSNicholas Bellinger 			break;
589c66ac9dbSNicholas Bellinger 
590c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
591c66ac9dbSNicholas Bellinger 	}
592c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);
593c66ac9dbSNicholas Bellinger 
594c66ac9dbSNicholas Bellinger 	return len;
595c66ac9dbSNicholas Bellinger }
596c66ac9dbSNicholas Bellinger 
597c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
598c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
599c66ac9dbSNicholas Bellinger 	const char *page,
600c66ac9dbSNicholas Bellinger 	size_t count)
601c66ac9dbSNicholas Bellinger {
602c66ac9dbSNicholas Bellinger 	return -ENOSYS;
603c66ac9dbSNicholas Bellinger }
604c66ac9dbSNicholas Bellinger 
605c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
606c66ac9dbSNicholas Bellinger 
607c66ac9dbSNicholas Bellinger /*
608c66ac9dbSNicholas Bellinger  * Generic wrapper for dumping VPD identifiers by association.
609c66ac9dbSNicholas Bellinger  */
610c66ac9dbSNicholas Bellinger #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
611c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_show_attr_##_name(			\
612c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,					\
613c66ac9dbSNicholas Bellinger 	char *page)							\
614c66ac9dbSNicholas Bellinger {									\
615c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;							\
616c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];				\
617c66ac9dbSNicholas Bellinger 	ssize_t len = 0;						\
618c66ac9dbSNicholas Bellinger 									\
619c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);				\
620c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
621c66ac9dbSNicholas Bellinger 		if (vpd->association != _assoc)				\
622c66ac9dbSNicholas Bellinger 			continue;					\
623c66ac9dbSNicholas Bellinger 									\
624c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
625c66ac9dbSNicholas Bellinger 		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
6266708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
627c66ac9dbSNicholas Bellinger 			break;						\
628c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
629c66ac9dbSNicholas Bellinger 									\
630c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
631c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
6326708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
633c66ac9dbSNicholas Bellinger 			break;						\
634c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
635c66ac9dbSNicholas Bellinger 									\
636c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
637c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
6386708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
639c66ac9dbSNicholas Bellinger 			break;						\
640c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
641c66ac9dbSNicholas Bellinger 	}								\
642c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);				\
643c66ac9dbSNicholas Bellinger 									\
644c66ac9dbSNicholas Bellinger 	return len;							\
645c66ac9dbSNicholas Bellinger }
646c66ac9dbSNicholas Bellinger 
647c66ac9dbSNicholas Bellinger /*
648163cd5faSAndy Shevchenko  * VPD page 0x83 Association: Logical Unit
649c66ac9dbSNicholas Bellinger  */
650c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
651c66ac9dbSNicholas Bellinger 
652c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
653c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
654c66ac9dbSNicholas Bellinger 	const char *page,
655c66ac9dbSNicholas Bellinger 	size_t count)
656c66ac9dbSNicholas Bellinger {
657c66ac9dbSNicholas Bellinger 	return -ENOSYS;
658c66ac9dbSNicholas Bellinger }
659c66ac9dbSNicholas Bellinger 
660c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
661c66ac9dbSNicholas Bellinger 
662c66ac9dbSNicholas Bellinger /*
663c66ac9dbSNicholas Bellinger  * VPD page 0x83 Association: Target Port
664c66ac9dbSNicholas Bellinger  */
665c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
666c66ac9dbSNicholas Bellinger 
667c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
668c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
669c66ac9dbSNicholas Bellinger 	const char *page,
670c66ac9dbSNicholas Bellinger 	size_t count)
671c66ac9dbSNicholas Bellinger {
672c66ac9dbSNicholas Bellinger 	return -ENOSYS;
673c66ac9dbSNicholas Bellinger }
674c66ac9dbSNicholas Bellinger 
675c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
676c66ac9dbSNicholas Bellinger 
677c66ac9dbSNicholas Bellinger /*
678c66ac9dbSNicholas Bellinger  * VPD page 0x83 Association: SCSI Target Device
679c66ac9dbSNicholas Bellinger  */
680c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
681c66ac9dbSNicholas Bellinger 
682c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
683c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
684c66ac9dbSNicholas Bellinger 	const char *page,
685c66ac9dbSNicholas Bellinger 	size_t count)
686c66ac9dbSNicholas Bellinger {
687c66ac9dbSNicholas Bellinger 	return -ENOSYS;
688c66ac9dbSNicholas Bellinger }
689c66ac9dbSNicholas Bellinger 
690c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
691c66ac9dbSNicholas Bellinger 
692c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
693c66ac9dbSNicholas Bellinger 
694c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
695c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_unit_serial.attr,
696c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_protocol_identifier.attr,
697c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_assoc_logical_unit.attr,
698c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_assoc_target_port.attr,
699c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
700c66ac9dbSNicholas Bellinger 	NULL,
701c66ac9dbSNicholas Bellinger };
702c66ac9dbSNicholas Bellinger 
703c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_wwn_ops = {
704c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_dev_wwn_attr_show,
705c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_dev_wwn_attr_store,
706c66ac9dbSNicholas Bellinger };
707c66ac9dbSNicholas Bellinger 
708f8d389c6SNicholas Bellinger TB_CIT_SETUP(dev_wwn, &target_core_dev_wwn_ops, NULL, target_core_dev_wwn_attrs);
709c66ac9dbSNicholas Bellinger 
710f8d389c6SNicholas Bellinger /*  End functions for struct config_item_type tb_dev_wwn_cit */
711c66ac9dbSNicholas Bellinger 
71291e2e39bSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_pr_cit */
713c66ac9dbSNicholas Bellinger 
7140fd97ccfSChristoph Hellwig CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
715c66ac9dbSNicholas Bellinger #define SE_DEV_PR_ATTR(_name, _mode)					\
716c66ac9dbSNicholas Bellinger static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
717c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR(_name, _mode,					\
718c66ac9dbSNicholas Bellinger 	target_core_dev_pr_show_attr_##_name,				\
719c66ac9dbSNicholas Bellinger 	target_core_dev_pr_store_attr_##_name);
720c66ac9dbSNicholas Bellinger 
721c66ac9dbSNicholas Bellinger #define SE_DEV_PR_ATTR_RO(_name);					\
722c66ac9dbSNicholas Bellinger static struct target_core_dev_pr_attribute target_core_dev_pr_##_name =	\
723c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR_RO(_name,					\
724c66ac9dbSNicholas Bellinger 	target_core_dev_pr_show_attr_##_name);
725c66ac9dbSNicholas Bellinger 
726d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
727d977f437SChristoph Hellwig 		char *page)
728c66ac9dbSNicholas Bellinger {
729c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
730c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
731c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
732c66ac9dbSNicholas Bellinger 
733c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
734c66ac9dbSNicholas Bellinger 
735c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
736d977f437SChristoph Hellwig 	if (!pr_reg)
737d977f437SChristoph Hellwig 		return sprintf(page, "No SPC-3 Reservation holder\n");
738d977f437SChristoph Hellwig 
739c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
740d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
741c66ac9dbSNicholas Bellinger 
742d977f437SChristoph Hellwig 	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
743e3d6f909SAndy Grover 		se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
744d2843c17SAndy Grover 		se_nacl->initiatorname, i_buf);
745c66ac9dbSNicholas Bellinger }
746c66ac9dbSNicholas Bellinger 
747d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
748d977f437SChristoph Hellwig 		char *page)
749c66ac9dbSNicholas Bellinger {
750c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
751d977f437SChristoph Hellwig 	ssize_t len;
752c66ac9dbSNicholas Bellinger 
753c66ac9dbSNicholas Bellinger 	se_nacl = dev->dev_reserved_node_acl;
754d977f437SChristoph Hellwig 	if (se_nacl) {
755d977f437SChristoph Hellwig 		len = sprintf(page,
756d977f437SChristoph Hellwig 			      "SPC-2 Reservation: %s Initiator: %s\n",
757e3d6f909SAndy Grover 			      se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
758c66ac9dbSNicholas Bellinger 			      se_nacl->initiatorname);
759d977f437SChristoph Hellwig 	} else {
760d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-2 Reservation holder\n");
761d977f437SChristoph Hellwig 	}
762d977f437SChristoph Hellwig 	return len;
763c66ac9dbSNicholas Bellinger }
764c66ac9dbSNicholas Bellinger 
7650fd97ccfSChristoph Hellwig static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
766c66ac9dbSNicholas Bellinger 		char *page)
767c66ac9dbSNicholas Bellinger {
768d977f437SChristoph Hellwig 	int ret;
769c66ac9dbSNicholas Bellinger 
770a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
771d977f437SChristoph Hellwig 		return sprintf(page, "Passthrough\n");
772c66ac9dbSNicholas Bellinger 
773d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
774d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
775d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc2_res(dev, page);
776d977f437SChristoph Hellwig 	else
777d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc3_res(dev, page);
778d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
779d977f437SChristoph Hellwig 	return ret;
780c66ac9dbSNicholas Bellinger }
781c66ac9dbSNicholas Bellinger 
782c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_holder);
783c66ac9dbSNicholas Bellinger 
784c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
7850fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
786c66ac9dbSNicholas Bellinger {
787c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
788c66ac9dbSNicholas Bellinger 
789c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
790d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder) {
791c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
792d977f437SChristoph Hellwig 	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
793c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: All Target"
794c66ac9dbSNicholas Bellinger 			" Ports registration\n");
795d977f437SChristoph Hellwig 	} else {
796c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: Single"
797c66ac9dbSNicholas Bellinger 			" Target Port registration\n");
798d977f437SChristoph Hellwig 	}
799c66ac9dbSNicholas Bellinger 
800d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
801c66ac9dbSNicholas Bellinger 	return len;
802c66ac9dbSNicholas Bellinger }
803c66ac9dbSNicholas Bellinger 
804c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
805c66ac9dbSNicholas Bellinger 
806c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
8070fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
808c66ac9dbSNicholas Bellinger {
8090fd97ccfSChristoph Hellwig 	return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
810c66ac9dbSNicholas Bellinger }
811c66ac9dbSNicholas Bellinger 
812c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_generation);
813c66ac9dbSNicholas Bellinger 
814c66ac9dbSNicholas Bellinger /*
815c66ac9dbSNicholas Bellinger  * res_pr_holder_tg_port
816c66ac9dbSNicholas Bellinger  */
817c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
8180fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
819c66ac9dbSNicholas Bellinger {
820c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
821c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
822c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
823c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
8249ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
825c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
826c66ac9dbSNicholas Bellinger 
827c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
828c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
8296708bb27SAndy Grover 	if (!pr_reg) {
830c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
831d977f437SChristoph Hellwig 		goto out_unlock;
832c66ac9dbSNicholas Bellinger 	}
833d977f437SChristoph Hellwig 
834c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
835c66ac9dbSNicholas Bellinger 	se_tpg = se_nacl->se_tpg;
836c66ac9dbSNicholas Bellinger 	lun = pr_reg->pr_reg_tg_pt_lun;
837e3d6f909SAndy Grover 	tfo = se_tpg->se_tpg_tfo;
838c66ac9dbSNicholas Bellinger 
839c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: %s"
840c66ac9dbSNicholas Bellinger 		" Target Node Endpoint: %s\n", tfo->get_fabric_name(),
841c66ac9dbSNicholas Bellinger 		tfo->tpg_get_wwn(se_tpg));
842c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
84335d1efe8SMasanari Iida 		" Identifier Tag: %hu %s Portal Group Tag: %hu"
844c66ac9dbSNicholas Bellinger 		" %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
845c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
846c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), lun->unpacked_lun);
847c66ac9dbSNicholas Bellinger 
848d977f437SChristoph Hellwig out_unlock:
849d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
850c66ac9dbSNicholas Bellinger 	return len;
851c66ac9dbSNicholas Bellinger }
852c66ac9dbSNicholas Bellinger 
853c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
854c66ac9dbSNicholas Bellinger 
855c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
8560fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
857c66ac9dbSNicholas Bellinger {
8589ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
859c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
860c66ac9dbSNicholas Bellinger 	unsigned char buf[384];
861c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
862c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
863d2843c17SAndy Grover 	int reg_count = 0;
864c66ac9dbSNicholas Bellinger 
865c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 PR Registrations:\n");
866c66ac9dbSNicholas Bellinger 
8670fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
8680fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
869c66ac9dbSNicholas Bellinger 			pr_reg_list) {
870c66ac9dbSNicholas Bellinger 
871c66ac9dbSNicholas Bellinger 		memset(buf, 0, 384);
872c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
873c66ac9dbSNicholas Bellinger 		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
874d2843c17SAndy Grover 		core_pr_dump_initiator_port(pr_reg, i_buf,
875c66ac9dbSNicholas Bellinger 					PR_REG_ISID_ID_LEN);
876c66ac9dbSNicholas Bellinger 		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
877c66ac9dbSNicholas Bellinger 			tfo->get_fabric_name(),
878d2843c17SAndy Grover 			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
879c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_generation);
880c66ac9dbSNicholas Bellinger 
8816708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
882c66ac9dbSNicholas Bellinger 			break;
883c66ac9dbSNicholas Bellinger 
884c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
885c66ac9dbSNicholas Bellinger 		reg_count++;
886c66ac9dbSNicholas Bellinger 	}
8870fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
888c66ac9dbSNicholas Bellinger 
8896708bb27SAndy Grover 	if (!reg_count)
890c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "None\n");
891c66ac9dbSNicholas Bellinger 
892c66ac9dbSNicholas Bellinger 	return len;
893c66ac9dbSNicholas Bellinger }
894c66ac9dbSNicholas Bellinger 
895c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
896c66ac9dbSNicholas Bellinger 
897c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_type(
8980fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
899c66ac9dbSNicholas Bellinger {
900c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
901c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
902c66ac9dbSNicholas Bellinger 
903c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
904c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
905d977f437SChristoph Hellwig 	if (pr_reg) {
906c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
907c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
908d977f437SChristoph Hellwig 	} else {
909d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-3 Reservation holder\n");
910d977f437SChristoph Hellwig 	}
911c66ac9dbSNicholas Bellinger 
912d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
913c66ac9dbSNicholas Bellinger 	return len;
914c66ac9dbSNicholas Bellinger }
915c66ac9dbSNicholas Bellinger 
916c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_type);
917c66ac9dbSNicholas Bellinger 
918c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_type(
9190fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
920c66ac9dbSNicholas Bellinger {
921a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
922d977f437SChristoph Hellwig 		return sprintf(page, "SPC_PASSTHROUGH\n");
923d977f437SChristoph Hellwig 	else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
924d977f437SChristoph Hellwig 		return sprintf(page, "SPC2_RESERVATIONS\n");
925d977f437SChristoph Hellwig 	else
926d977f437SChristoph Hellwig 		return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
927c66ac9dbSNicholas Bellinger }
928c66ac9dbSNicholas Bellinger 
929c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_type);
930c66ac9dbSNicholas Bellinger 
931c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
9320fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
933c66ac9dbSNicholas Bellinger {
934a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
935c66ac9dbSNicholas Bellinger 		return 0;
936c66ac9dbSNicholas Bellinger 
937c66ac9dbSNicholas Bellinger 	return sprintf(page, "APTPL Bit Status: %s\n",
9380fd97ccfSChristoph Hellwig 		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
939c66ac9dbSNicholas Bellinger }
940c66ac9dbSNicholas Bellinger 
941c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_aptpl_active);
942c66ac9dbSNicholas Bellinger 
943c66ac9dbSNicholas Bellinger /*
944c66ac9dbSNicholas Bellinger  * res_aptpl_metadata
945c66ac9dbSNicholas Bellinger  */
946c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
9470fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
948c66ac9dbSNicholas Bellinger {
949a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
950c66ac9dbSNicholas Bellinger 		return 0;
951c66ac9dbSNicholas Bellinger 
952c66ac9dbSNicholas Bellinger 	return sprintf(page, "Ready to process PR APTPL metadata..\n");
953c66ac9dbSNicholas Bellinger }
954c66ac9dbSNicholas Bellinger 
955c66ac9dbSNicholas Bellinger enum {
956c66ac9dbSNicholas Bellinger 	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
957c66ac9dbSNicholas Bellinger 	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
958c66ac9dbSNicholas Bellinger 	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
959c66ac9dbSNicholas Bellinger 	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
960c66ac9dbSNicholas Bellinger };
961c66ac9dbSNicholas Bellinger 
962c66ac9dbSNicholas Bellinger static match_table_t tokens = {
963c66ac9dbSNicholas Bellinger 	{Opt_initiator_fabric, "initiator_fabric=%s"},
964c66ac9dbSNicholas Bellinger 	{Opt_initiator_node, "initiator_node=%s"},
965c66ac9dbSNicholas Bellinger 	{Opt_initiator_sid, "initiator_sid=%s"},
966c66ac9dbSNicholas Bellinger 	{Opt_sa_res_key, "sa_res_key=%s"},
967c66ac9dbSNicholas Bellinger 	{Opt_res_holder, "res_holder=%d"},
968c66ac9dbSNicholas Bellinger 	{Opt_res_type, "res_type=%d"},
969c66ac9dbSNicholas Bellinger 	{Opt_res_scope, "res_scope=%d"},
970c66ac9dbSNicholas Bellinger 	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
971c66ac9dbSNicholas Bellinger 	{Opt_mapped_lun, "mapped_lun=%d"},
972c66ac9dbSNicholas Bellinger 	{Opt_target_fabric, "target_fabric=%s"},
973c66ac9dbSNicholas Bellinger 	{Opt_target_node, "target_node=%s"},
974c66ac9dbSNicholas Bellinger 	{Opt_tpgt, "tpgt=%d"},
975c66ac9dbSNicholas Bellinger 	{Opt_port_rtpi, "port_rtpi=%d"},
976c66ac9dbSNicholas Bellinger 	{Opt_target_lun, "target_lun=%d"},
977c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
978c66ac9dbSNicholas Bellinger };
979c66ac9dbSNicholas Bellinger 
980c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
9810fd97ccfSChristoph Hellwig 	struct se_device *dev,
982c66ac9dbSNicholas Bellinger 	const char *page,
983c66ac9dbSNicholas Bellinger 	size_t count)
984c66ac9dbSNicholas Bellinger {
9856d180253SJesper Juhl 	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
9866d180253SJesper Juhl 	unsigned char *t_fabric = NULL, *t_port = NULL;
9878d213559SJoern Engel 	char *orig, *ptr, *opts;
988c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
989c66ac9dbSNicholas Bellinger 	unsigned long long tmp_ll;
990c66ac9dbSNicholas Bellinger 	u64 sa_res_key = 0;
991c66ac9dbSNicholas Bellinger 	u32 mapped_lun = 0, target_lun = 0;
992c66ac9dbSNicholas Bellinger 	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
99345fb94c2SBart Van Assche 	u16 tpgt = 0;
99445fb94c2SBart Van Assche 	u8 type = 0;
995c66ac9dbSNicholas Bellinger 
996a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
997d977f437SChristoph Hellwig 		return 0;
998d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
999c66ac9dbSNicholas Bellinger 		return 0;
1000c66ac9dbSNicholas Bellinger 
10010fd97ccfSChristoph Hellwig 	if (dev->export_count) {
10026708bb27SAndy Grover 		pr_debug("Unable to process APTPL metadata while"
1003c66ac9dbSNicholas Bellinger 			" active fabric exports exist\n");
1004c66ac9dbSNicholas Bellinger 		return -EINVAL;
1005c66ac9dbSNicholas Bellinger 	}
1006c66ac9dbSNicholas Bellinger 
1007c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
1008c66ac9dbSNicholas Bellinger 	if (!opts)
1009c66ac9dbSNicholas Bellinger 		return -ENOMEM;
1010c66ac9dbSNicholas Bellinger 
1011c66ac9dbSNicholas Bellinger 	orig = opts;
101290c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
1013c66ac9dbSNicholas Bellinger 		if (!*ptr)
1014c66ac9dbSNicholas Bellinger 			continue;
1015c66ac9dbSNicholas Bellinger 
1016c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
1017c66ac9dbSNicholas Bellinger 		switch (token) {
1018c66ac9dbSNicholas Bellinger 		case Opt_initiator_fabric:
10198d213559SJoern Engel 			i_fabric = match_strdup(args);
10206d180253SJesper Juhl 			if (!i_fabric) {
10216d180253SJesper Juhl 				ret = -ENOMEM;
10226d180253SJesper Juhl 				goto out;
10236d180253SJesper Juhl 			}
1024c66ac9dbSNicholas Bellinger 			break;
1025c66ac9dbSNicholas Bellinger 		case Opt_initiator_node:
10268d213559SJoern Engel 			i_port = match_strdup(args);
10276d180253SJesper Juhl 			if (!i_port) {
10286d180253SJesper Juhl 				ret = -ENOMEM;
10296d180253SJesper Juhl 				goto out;
10306d180253SJesper Juhl 			}
103160d645a4SDan Carpenter 			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
10326708bb27SAndy Grover 				pr_err("APTPL metadata initiator_node="
1033c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1034c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_IPORT_LEN);
1035c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1036c66ac9dbSNicholas Bellinger 				break;
1037c66ac9dbSNicholas Bellinger 			}
1038c66ac9dbSNicholas Bellinger 			break;
1039c66ac9dbSNicholas Bellinger 		case Opt_initiator_sid:
10408d213559SJoern Engel 			isid = match_strdup(args);
10416d180253SJesper Juhl 			if (!isid) {
10426d180253SJesper Juhl 				ret = -ENOMEM;
10436d180253SJesper Juhl 				goto out;
10446d180253SJesper Juhl 			}
104560d645a4SDan Carpenter 			if (strlen(isid) >= PR_REG_ISID_LEN) {
10466708bb27SAndy Grover 				pr_err("APTPL metadata initiator_isid"
1047c66ac9dbSNicholas Bellinger 					"= exceeds PR_REG_ISID_LEN: %d\n",
1048c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
1049c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1050c66ac9dbSNicholas Bellinger 				break;
1051c66ac9dbSNicholas Bellinger 			}
1052c66ac9dbSNicholas Bellinger 			break;
1053c66ac9dbSNicholas Bellinger 		case Opt_sa_res_key:
10548d213559SJoern Engel 			ret = kstrtoull(args->from, 0, &tmp_ll);
1055c66ac9dbSNicholas Bellinger 			if (ret < 0) {
10568d213559SJoern Engel 				pr_err("kstrtoull() failed for sa_res_key=\n");
1057c66ac9dbSNicholas Bellinger 				goto out;
1058c66ac9dbSNicholas Bellinger 			}
1059c66ac9dbSNicholas Bellinger 			sa_res_key = (u64)tmp_ll;
1060c66ac9dbSNicholas Bellinger 			break;
1061c66ac9dbSNicholas Bellinger 		/*
1062c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Reservation
1063c66ac9dbSNicholas Bellinger 		 */
1064c66ac9dbSNicholas Bellinger 		case Opt_res_holder:
1065c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1066c66ac9dbSNicholas Bellinger 			res_holder = arg;
1067c66ac9dbSNicholas Bellinger 			break;
1068c66ac9dbSNicholas Bellinger 		case Opt_res_type:
1069c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1070c66ac9dbSNicholas Bellinger 			type = (u8)arg;
1071c66ac9dbSNicholas Bellinger 			break;
1072c66ac9dbSNicholas Bellinger 		case Opt_res_scope:
1073c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1074c66ac9dbSNicholas Bellinger 			break;
1075c66ac9dbSNicholas Bellinger 		case Opt_res_all_tg_pt:
1076c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1077c66ac9dbSNicholas Bellinger 			all_tg_pt = (int)arg;
1078c66ac9dbSNicholas Bellinger 			break;
1079c66ac9dbSNicholas Bellinger 		case Opt_mapped_lun:
1080c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1081c66ac9dbSNicholas Bellinger 			mapped_lun = (u32)arg;
1082c66ac9dbSNicholas Bellinger 			break;
1083c66ac9dbSNicholas Bellinger 		/*
1084c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Target Port
1085c66ac9dbSNicholas Bellinger 		 */
1086c66ac9dbSNicholas Bellinger 		case Opt_target_fabric:
10878d213559SJoern Engel 			t_fabric = match_strdup(args);
10886d180253SJesper Juhl 			if (!t_fabric) {
10896d180253SJesper Juhl 				ret = -ENOMEM;
10906d180253SJesper Juhl 				goto out;
10916d180253SJesper Juhl 			}
1092c66ac9dbSNicholas Bellinger 			break;
1093c66ac9dbSNicholas Bellinger 		case Opt_target_node:
10948d213559SJoern Engel 			t_port = match_strdup(args);
10956d180253SJesper Juhl 			if (!t_port) {
10966d180253SJesper Juhl 				ret = -ENOMEM;
10976d180253SJesper Juhl 				goto out;
10986d180253SJesper Juhl 			}
109960d645a4SDan Carpenter 			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
11006708bb27SAndy Grover 				pr_err("APTPL metadata target_node="
1101c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1102c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_TPORT_LEN);
1103c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1104c66ac9dbSNicholas Bellinger 				break;
1105c66ac9dbSNicholas Bellinger 			}
1106c66ac9dbSNicholas Bellinger 			break;
1107c66ac9dbSNicholas Bellinger 		case Opt_tpgt:
1108c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1109c66ac9dbSNicholas Bellinger 			tpgt = (u16)arg;
1110c66ac9dbSNicholas Bellinger 			break;
1111c66ac9dbSNicholas Bellinger 		case Opt_port_rtpi:
1112c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1113c66ac9dbSNicholas Bellinger 			break;
1114c66ac9dbSNicholas Bellinger 		case Opt_target_lun:
1115c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1116c66ac9dbSNicholas Bellinger 			target_lun = (u32)arg;
1117c66ac9dbSNicholas Bellinger 			break;
1118c66ac9dbSNicholas Bellinger 		default:
1119c66ac9dbSNicholas Bellinger 			break;
1120c66ac9dbSNicholas Bellinger 		}
1121c66ac9dbSNicholas Bellinger 	}
1122c66ac9dbSNicholas Bellinger 
11236708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
11246708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
1125c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1126c66ac9dbSNicholas Bellinger 		goto out;
1127c66ac9dbSNicholas Bellinger 	}
1128c66ac9dbSNicholas Bellinger 
1129c66ac9dbSNicholas Bellinger 	if (res_holder && !(type)) {
11306708bb27SAndy Grover 		pr_err("Illegal PR type: 0x%02x for reservation"
1131c66ac9dbSNicholas Bellinger 				" holder\n", type);
1132c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1133c66ac9dbSNicholas Bellinger 		goto out;
1134c66ac9dbSNicholas Bellinger 	}
1135c66ac9dbSNicholas Bellinger 
11360fd97ccfSChristoph Hellwig 	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1137c66ac9dbSNicholas Bellinger 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1138c66ac9dbSNicholas Bellinger 			res_holder, all_tg_pt, type);
1139c66ac9dbSNicholas Bellinger out:
11406d180253SJesper Juhl 	kfree(i_fabric);
11416d180253SJesper Juhl 	kfree(i_port);
11426d180253SJesper Juhl 	kfree(isid);
11436d180253SJesper Juhl 	kfree(t_fabric);
11446d180253SJesper Juhl 	kfree(t_port);
1145c66ac9dbSNicholas Bellinger 	kfree(orig);
1146c66ac9dbSNicholas Bellinger 	return (ret == 0) ? count : ret;
1147c66ac9dbSNicholas Bellinger }
1148c66ac9dbSNicholas Bellinger 
1149c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1150c66ac9dbSNicholas Bellinger 
11510fd97ccfSChristoph Hellwig CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
1152c66ac9dbSNicholas Bellinger 
1153c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1154c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_holder.attr,
1155c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_all_tgt_pts.attr,
1156c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_generation.attr,
1157c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_holder_tg_port.attr,
1158c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_registered_i_pts.attr,
1159c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_type.attr,
1160c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_type.attr,
1161c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_aptpl_active.attr,
1162c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_aptpl_metadata.attr,
1163c66ac9dbSNicholas Bellinger 	NULL,
1164c66ac9dbSNicholas Bellinger };
1165c66ac9dbSNicholas Bellinger 
1166c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_pr_ops = {
1167c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_dev_pr_attr_show,
1168c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_dev_pr_attr_store,
1169c66ac9dbSNicholas Bellinger };
1170c66ac9dbSNicholas Bellinger 
117191e2e39bSNicholas Bellinger TB_CIT_SETUP(dev_pr, &target_core_dev_pr_ops, NULL, target_core_dev_pr_attrs);
1172c66ac9dbSNicholas Bellinger 
117391e2e39bSNicholas Bellinger /*  End functions for struct config_item_type tb_dev_pr_cit */
1174c66ac9dbSNicholas Bellinger 
117573112edcSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_cit */
1176c66ac9dbSNicholas Bellinger 
1177c66ac9dbSNicholas Bellinger static ssize_t target_core_show_dev_info(void *p, char *page)
1178c66ac9dbSNicholas Bellinger {
11790fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
11800fd97ccfSChristoph Hellwig 	struct se_subsystem_api *t = dev->transport;
1181c66ac9dbSNicholas Bellinger 	int bl = 0;
1182c66ac9dbSNicholas Bellinger 	ssize_t read_bytes = 0;
1183c66ac9dbSNicholas Bellinger 
11840fd97ccfSChristoph Hellwig 	transport_dump_dev_state(dev, page, &bl);
1185c66ac9dbSNicholas Bellinger 	read_bytes += bl;
11860fd97ccfSChristoph Hellwig 	read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
1187c66ac9dbSNicholas Bellinger 	return read_bytes;
1188c66ac9dbSNicholas Bellinger }
1189c66ac9dbSNicholas Bellinger 
1190c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_info = {
1191c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1192c66ac9dbSNicholas Bellinger 		    .ca_name = "info",
1193c66ac9dbSNicholas Bellinger 		    .ca_mode = S_IRUGO },
1194c66ac9dbSNicholas Bellinger 	.show	= target_core_show_dev_info,
1195c66ac9dbSNicholas Bellinger 	.store	= NULL,
1196c66ac9dbSNicholas Bellinger };
1197c66ac9dbSNicholas Bellinger 
1198c66ac9dbSNicholas Bellinger static ssize_t target_core_store_dev_control(
1199c66ac9dbSNicholas Bellinger 	void *p,
1200c66ac9dbSNicholas Bellinger 	const char *page,
1201c66ac9dbSNicholas Bellinger 	size_t count)
1202c66ac9dbSNicholas Bellinger {
12030fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
12040fd97ccfSChristoph Hellwig 	struct se_subsystem_api *t = dev->transport;
1205c66ac9dbSNicholas Bellinger 
12060fd97ccfSChristoph Hellwig 	return t->set_configfs_dev_params(dev, page, count);
1207c66ac9dbSNicholas Bellinger }
1208c66ac9dbSNicholas Bellinger 
1209c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_control = {
1210c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1211c66ac9dbSNicholas Bellinger 		    .ca_name = "control",
1212c66ac9dbSNicholas Bellinger 		    .ca_mode = S_IWUSR },
1213c66ac9dbSNicholas Bellinger 	.show	= NULL,
1214c66ac9dbSNicholas Bellinger 	.store	= target_core_store_dev_control,
1215c66ac9dbSNicholas Bellinger };
1216c66ac9dbSNicholas Bellinger 
1217c66ac9dbSNicholas Bellinger static ssize_t target_core_show_dev_alias(void *p, char *page)
1218c66ac9dbSNicholas Bellinger {
12190fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
1220c66ac9dbSNicholas Bellinger 
12210fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_ALIAS))
1222c66ac9dbSNicholas Bellinger 		return 0;
1223c66ac9dbSNicholas Bellinger 
12240fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1225c66ac9dbSNicholas Bellinger }
1226c66ac9dbSNicholas Bellinger 
1227c66ac9dbSNicholas Bellinger static ssize_t target_core_store_dev_alias(
1228c66ac9dbSNicholas Bellinger 	void *p,
1229c66ac9dbSNicholas Bellinger 	const char *page,
1230c66ac9dbSNicholas Bellinger 	size_t count)
1231c66ac9dbSNicholas Bellinger {
12320fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
12330fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1234c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1235c66ac9dbSNicholas Bellinger 
1236c66ac9dbSNicholas Bellinger 	if (count > (SE_DEV_ALIAS_LEN-1)) {
12376708bb27SAndy Grover 		pr_err("alias count: %d exceeds"
1238c66ac9dbSNicholas Bellinger 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1239c66ac9dbSNicholas Bellinger 			SE_DEV_ALIAS_LEN-1);
1240c66ac9dbSNicholas Bellinger 		return -EINVAL;
1241c66ac9dbSNicholas Bellinger 	}
1242c66ac9dbSNicholas Bellinger 
12430fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
12443011684cSDan Carpenter 	if (!read_bytes)
12453011684cSDan Carpenter 		return -EINVAL;
12460fd97ccfSChristoph Hellwig 	if (dev->dev_alias[read_bytes - 1] == '\n')
12470fd97ccfSChristoph Hellwig 		dev->dev_alias[read_bytes - 1] = '\0';
12480877eafdSSebastian Andrzej Siewior 
12490fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_ALIAS;
12503011684cSDan Carpenter 
12516708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1252c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
12530fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
12540fd97ccfSChristoph Hellwig 		dev->dev_alias);
1255c66ac9dbSNicholas Bellinger 
1256c66ac9dbSNicholas Bellinger 	return read_bytes;
1257c66ac9dbSNicholas Bellinger }
1258c66ac9dbSNicholas Bellinger 
1259c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1260c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1261c66ac9dbSNicholas Bellinger 		    .ca_name = "alias",
1262c66ac9dbSNicholas Bellinger 		    .ca_mode =  S_IRUGO | S_IWUSR },
1263c66ac9dbSNicholas Bellinger 	.show	= target_core_show_dev_alias,
1264c66ac9dbSNicholas Bellinger 	.store	= target_core_store_dev_alias,
1265c66ac9dbSNicholas Bellinger };
1266c66ac9dbSNicholas Bellinger 
1267c66ac9dbSNicholas Bellinger static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1268c66ac9dbSNicholas Bellinger {
12690fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
1270c66ac9dbSNicholas Bellinger 
12710fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1272c66ac9dbSNicholas Bellinger 		return 0;
1273c66ac9dbSNicholas Bellinger 
12740fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1275c66ac9dbSNicholas Bellinger }
1276c66ac9dbSNicholas Bellinger 
1277c66ac9dbSNicholas Bellinger static ssize_t target_core_store_dev_udev_path(
1278c66ac9dbSNicholas Bellinger 	void *p,
1279c66ac9dbSNicholas Bellinger 	const char *page,
1280c66ac9dbSNicholas Bellinger 	size_t count)
1281c66ac9dbSNicholas Bellinger {
12820fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
12830fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1284c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1285c66ac9dbSNicholas Bellinger 
1286c66ac9dbSNicholas Bellinger 	if (count > (SE_UDEV_PATH_LEN-1)) {
12876708bb27SAndy Grover 		pr_err("udev_path count: %d exceeds"
1288c66ac9dbSNicholas Bellinger 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1289c66ac9dbSNicholas Bellinger 			SE_UDEV_PATH_LEN-1);
1290c66ac9dbSNicholas Bellinger 		return -EINVAL;
1291c66ac9dbSNicholas Bellinger 	}
1292c66ac9dbSNicholas Bellinger 
12930fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1294c66ac9dbSNicholas Bellinger 			"%s", page);
12953011684cSDan Carpenter 	if (!read_bytes)
12963011684cSDan Carpenter 		return -EINVAL;
12970fd97ccfSChristoph Hellwig 	if (dev->udev_path[read_bytes - 1] == '\n')
12980fd97ccfSChristoph Hellwig 		dev->udev_path[read_bytes - 1] = '\0';
12990877eafdSSebastian Andrzej Siewior 
13000fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_UDEV_PATH;
13013011684cSDan Carpenter 
13026708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1303c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
13040fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
13050fd97ccfSChristoph Hellwig 		dev->udev_path);
1306c66ac9dbSNicholas Bellinger 
1307c66ac9dbSNicholas Bellinger 	return read_bytes;
1308c66ac9dbSNicholas Bellinger }
1309c66ac9dbSNicholas Bellinger 
1310c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1311c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1312c66ac9dbSNicholas Bellinger 		    .ca_name = "udev_path",
1313c66ac9dbSNicholas Bellinger 		    .ca_mode =  S_IRUGO | S_IWUSR },
1314c66ac9dbSNicholas Bellinger 	.show	= target_core_show_dev_udev_path,
1315c66ac9dbSNicholas Bellinger 	.store	= target_core_store_dev_udev_path,
1316c66ac9dbSNicholas Bellinger };
1317c66ac9dbSNicholas Bellinger 
131864146db7SAndy Grover static ssize_t target_core_show_dev_enable(void *p, char *page)
131964146db7SAndy Grover {
132064146db7SAndy Grover 	struct se_device *dev = p;
132164146db7SAndy Grover 
132264146db7SAndy Grover 	return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
132364146db7SAndy Grover }
132464146db7SAndy Grover 
1325c66ac9dbSNicholas Bellinger static ssize_t target_core_store_dev_enable(
1326c66ac9dbSNicholas Bellinger 	void *p,
1327c66ac9dbSNicholas Bellinger 	const char *page,
1328c66ac9dbSNicholas Bellinger 	size_t count)
1329c66ac9dbSNicholas Bellinger {
13300fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
1331c66ac9dbSNicholas Bellinger 	char *ptr;
13320fd97ccfSChristoph Hellwig 	int ret;
1333c66ac9dbSNicholas Bellinger 
1334c66ac9dbSNicholas Bellinger 	ptr = strstr(page, "1");
13356708bb27SAndy Grover 	if (!ptr) {
13366708bb27SAndy Grover 		pr_err("For dev_enable ops, only valid value"
1337c66ac9dbSNicholas Bellinger 				" is \"1\"\n");
1338c66ac9dbSNicholas Bellinger 		return -EINVAL;
1339c66ac9dbSNicholas Bellinger 	}
1340c66ac9dbSNicholas Bellinger 
13410fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
13420fd97ccfSChristoph Hellwig 	if (ret)
13430fd97ccfSChristoph Hellwig 		return ret;
1344c66ac9dbSNicholas Bellinger 	return count;
1345c66ac9dbSNicholas Bellinger }
1346c66ac9dbSNicholas Bellinger 
1347c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1348c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1349c66ac9dbSNicholas Bellinger 		    .ca_name = "enable",
135064146db7SAndy Grover 		    .ca_mode =  S_IRUGO | S_IWUSR },
135164146db7SAndy Grover 	.show	= target_core_show_dev_enable,
1352c66ac9dbSNicholas Bellinger 	.store	= target_core_store_dev_enable,
1353c66ac9dbSNicholas Bellinger };
1354c66ac9dbSNicholas Bellinger 
1355c66ac9dbSNicholas Bellinger static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1356c66ac9dbSNicholas Bellinger {
13570fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
1358c66ac9dbSNicholas Bellinger 	struct config_item *lu_ci;
1359c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
1360c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1361c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1362c66ac9dbSNicholas Bellinger 
1363c66ac9dbSNicholas Bellinger 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1364c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
1365c87fbd56SChristoph Hellwig 		return 0;
1366c66ac9dbSNicholas Bellinger 
1367c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1368c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
13696708bb27SAndy Grover 	if (lu_gp) {
1370c66ac9dbSNicholas Bellinger 		lu_ci = &lu_gp->lu_gp_group.cg_item;
1371c66ac9dbSNicholas Bellinger 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1372c66ac9dbSNicholas Bellinger 			config_item_name(lu_ci), lu_gp->lu_gp_id);
1373c66ac9dbSNicholas Bellinger 	}
1374c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1375c66ac9dbSNicholas Bellinger 
1376c66ac9dbSNicholas Bellinger 	return len;
1377c66ac9dbSNicholas Bellinger }
1378c66ac9dbSNicholas Bellinger 
1379c66ac9dbSNicholas Bellinger static ssize_t target_core_store_alua_lu_gp(
1380c66ac9dbSNicholas Bellinger 	void *p,
1381c66ac9dbSNicholas Bellinger 	const char *page,
1382c66ac9dbSNicholas Bellinger 	size_t count)
1383c66ac9dbSNicholas Bellinger {
13840fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
13850fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1386c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1387c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1388c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
1389c66ac9dbSNicholas Bellinger 	int move = 0;
1390c66ac9dbSNicholas Bellinger 
1391c87fbd56SChristoph Hellwig 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1392c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
1393c87fbd56SChristoph Hellwig 		return 0;
1394c87fbd56SChristoph Hellwig 
1395c66ac9dbSNicholas Bellinger 	if (count > LU_GROUP_NAME_BUF) {
13966708bb27SAndy Grover 		pr_err("ALUA LU Group Alias too large!\n");
1397c66ac9dbSNicholas Bellinger 		return -EINVAL;
1398c66ac9dbSNicholas Bellinger 	}
1399c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
1400c66ac9dbSNicholas Bellinger 	memcpy(buf, page, count);
1401c66ac9dbSNicholas Bellinger 	/*
1402c66ac9dbSNicholas Bellinger 	 * Any ALUA logical unit alias besides "NULL" means we will be
1403c66ac9dbSNicholas Bellinger 	 * making a new group association.
1404c66ac9dbSNicholas Bellinger 	 */
1405c66ac9dbSNicholas Bellinger 	if (strcmp(strstrip(buf), "NULL")) {
1406c66ac9dbSNicholas Bellinger 		/*
1407c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name() will increment reference to
1408c66ac9dbSNicholas Bellinger 		 * struct t10_alua_lu_gp.  This reference is released with
1409c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name below().
1410c66ac9dbSNicholas Bellinger 		 */
1411c66ac9dbSNicholas Bellinger 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
14126708bb27SAndy Grover 		if (!lu_gp_new)
1413c66ac9dbSNicholas Bellinger 			return -ENODEV;
1414c66ac9dbSNicholas Bellinger 	}
1415c66ac9dbSNicholas Bellinger 
1416c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1417c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
14186708bb27SAndy Grover 	if (lu_gp) {
1419c66ac9dbSNicholas Bellinger 		/*
1420c66ac9dbSNicholas Bellinger 		 * Clearing an existing lu_gp association, and replacing
1421c66ac9dbSNicholas Bellinger 		 * with NULL
1422c66ac9dbSNicholas Bellinger 		 */
14236708bb27SAndy Grover 		if (!lu_gp_new) {
14246708bb27SAndy Grover 			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1425c66ac9dbSNicholas Bellinger 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1426c66ac9dbSNicholas Bellinger 				" %hu\n",
1427c66ac9dbSNicholas Bellinger 				config_item_name(&hba->hba_group.cg_item),
14280fd97ccfSChristoph Hellwig 				config_item_name(&dev->dev_group.cg_item),
1429c66ac9dbSNicholas Bellinger 				config_item_name(&lu_gp->lu_gp_group.cg_item),
1430c66ac9dbSNicholas Bellinger 				lu_gp->lu_gp_id);
1431c66ac9dbSNicholas Bellinger 
1432c66ac9dbSNicholas Bellinger 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1433c66ac9dbSNicholas Bellinger 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1434c66ac9dbSNicholas Bellinger 
1435c66ac9dbSNicholas Bellinger 			return count;
1436c66ac9dbSNicholas Bellinger 		}
1437c66ac9dbSNicholas Bellinger 		/*
1438c66ac9dbSNicholas Bellinger 		 * Removing existing association of lu_gp_mem with lu_gp
1439c66ac9dbSNicholas Bellinger 		 */
1440c66ac9dbSNicholas Bellinger 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1441c66ac9dbSNicholas Bellinger 		move = 1;
1442c66ac9dbSNicholas Bellinger 	}
1443c66ac9dbSNicholas Bellinger 	/*
1444c66ac9dbSNicholas Bellinger 	 * Associate lu_gp_mem with lu_gp_new.
1445c66ac9dbSNicholas Bellinger 	 */
1446c66ac9dbSNicholas Bellinger 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1447c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1448c66ac9dbSNicholas Bellinger 
14496708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1450c66ac9dbSNicholas Bellinger 		" core/alua/lu_gps/%s, ID: %hu\n",
1451c66ac9dbSNicholas Bellinger 		(move) ? "Moving" : "Adding",
1452c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
14530fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
1454c66ac9dbSNicholas Bellinger 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1455c66ac9dbSNicholas Bellinger 		lu_gp_new->lu_gp_id);
1456c66ac9dbSNicholas Bellinger 
1457c66ac9dbSNicholas Bellinger 	core_alua_put_lu_gp_from_name(lu_gp_new);
1458c66ac9dbSNicholas Bellinger 	return count;
1459c66ac9dbSNicholas Bellinger }
1460c66ac9dbSNicholas Bellinger 
1461c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1462c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1463c66ac9dbSNicholas Bellinger 		    .ca_name = "alua_lu_gp",
1464c66ac9dbSNicholas Bellinger 		    .ca_mode = S_IRUGO | S_IWUSR },
1465c66ac9dbSNicholas Bellinger 	.show	= target_core_show_alua_lu_gp,
1466c66ac9dbSNicholas Bellinger 	.store	= target_core_store_alua_lu_gp,
1467c66ac9dbSNicholas Bellinger };
1468c66ac9dbSNicholas Bellinger 
1469229d4f11SHannes Reinecke static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1470229d4f11SHannes Reinecke {
1471229d4f11SHannes Reinecke 	struct se_device *dev = p;
1472229d4f11SHannes Reinecke 	struct t10_alua_lba_map *map;
1473229d4f11SHannes Reinecke 	struct t10_alua_lba_map_member *mem;
1474229d4f11SHannes Reinecke 	char *b = page;
1475229d4f11SHannes Reinecke 	int bl = 0;
1476229d4f11SHannes Reinecke 	char state;
1477229d4f11SHannes Reinecke 
1478229d4f11SHannes Reinecke 	spin_lock(&dev->t10_alua.lba_map_lock);
1479229d4f11SHannes Reinecke 	if (!list_empty(&dev->t10_alua.lba_map_list))
1480229d4f11SHannes Reinecke 	    bl += sprintf(b + bl, "%u %u\n",
1481229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_size,
1482229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_multiplier);
1483229d4f11SHannes Reinecke 	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1484229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "%llu %llu",
1485229d4f11SHannes Reinecke 			      map->lba_map_first_lba, map->lba_map_last_lba);
1486229d4f11SHannes Reinecke 		list_for_each_entry(mem, &map->lba_map_mem_list,
1487229d4f11SHannes Reinecke 				    lba_map_mem_list) {
1488229d4f11SHannes Reinecke 			switch (mem->lba_map_mem_alua_state) {
1489229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1490229d4f11SHannes Reinecke 				state = 'O';
1491229d4f11SHannes Reinecke 				break;
1492229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1493229d4f11SHannes Reinecke 				state = 'A';
1494229d4f11SHannes Reinecke 				break;
1495229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_STANDBY:
1496229d4f11SHannes Reinecke 				state = 'S';
1497229d4f11SHannes Reinecke 				break;
1498229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_UNAVAILABLE:
1499229d4f11SHannes Reinecke 				state = 'U';
1500229d4f11SHannes Reinecke 				break;
1501229d4f11SHannes Reinecke 			default:
1502229d4f11SHannes Reinecke 				state = '.';
1503229d4f11SHannes Reinecke 				break;
1504229d4f11SHannes Reinecke 			}
1505229d4f11SHannes Reinecke 			bl += sprintf(b + bl, " %d:%c",
1506229d4f11SHannes Reinecke 				      mem->lba_map_mem_alua_pg_id, state);
1507229d4f11SHannes Reinecke 		}
1508229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "\n");
1509229d4f11SHannes Reinecke 	}
1510229d4f11SHannes Reinecke 	spin_unlock(&dev->t10_alua.lba_map_lock);
1511229d4f11SHannes Reinecke 	return bl;
1512229d4f11SHannes Reinecke }
1513229d4f11SHannes Reinecke 
1514229d4f11SHannes Reinecke static ssize_t target_core_store_dev_lba_map(
1515229d4f11SHannes Reinecke 	void *p,
1516229d4f11SHannes Reinecke 	const char *page,
1517229d4f11SHannes Reinecke 	size_t count)
1518229d4f11SHannes Reinecke {
1519229d4f11SHannes Reinecke 	struct se_device *dev = p;
1520229d4f11SHannes Reinecke 	struct t10_alua_lba_map *lba_map = NULL;
1521229d4f11SHannes Reinecke 	struct list_head lba_list;
1522229d4f11SHannes Reinecke 	char *map_entries, *ptr;
1523229d4f11SHannes Reinecke 	char state;
1524229d4f11SHannes Reinecke 	int pg_num = -1, pg;
1525229d4f11SHannes Reinecke 	int ret = 0, num = 0, pg_id, alua_state;
1526229d4f11SHannes Reinecke 	unsigned long start_lba = -1, end_lba = -1;
1527229d4f11SHannes Reinecke 	unsigned long segment_size = -1, segment_mult = -1;
1528229d4f11SHannes Reinecke 
1529229d4f11SHannes Reinecke 	map_entries = kstrdup(page, GFP_KERNEL);
1530229d4f11SHannes Reinecke 	if (!map_entries)
1531229d4f11SHannes Reinecke 		return -ENOMEM;
1532229d4f11SHannes Reinecke 
1533229d4f11SHannes Reinecke 	INIT_LIST_HEAD(&lba_list);
1534229d4f11SHannes Reinecke 	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1535229d4f11SHannes Reinecke 		if (!*ptr)
1536229d4f11SHannes Reinecke 			continue;
1537229d4f11SHannes Reinecke 
1538229d4f11SHannes Reinecke 		if (num == 0) {
1539229d4f11SHannes Reinecke 			if (sscanf(ptr, "%lu %lu\n",
1540229d4f11SHannes Reinecke 				   &segment_size, &segment_mult) != 2) {
1541229d4f11SHannes Reinecke 				pr_err("Invalid line %d\n", num);
1542229d4f11SHannes Reinecke 				ret = -EINVAL;
1543229d4f11SHannes Reinecke 				break;
1544229d4f11SHannes Reinecke 			}
1545229d4f11SHannes Reinecke 			num++;
1546229d4f11SHannes Reinecke 			continue;
1547229d4f11SHannes Reinecke 		}
1548229d4f11SHannes Reinecke 		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1549229d4f11SHannes Reinecke 			pr_err("Invalid line %d\n", num);
1550229d4f11SHannes Reinecke 			ret = -EINVAL;
1551229d4f11SHannes Reinecke 			break;
1552229d4f11SHannes Reinecke 		}
1553229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
1554229d4f11SHannes Reinecke 		if (!ptr) {
1555229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing end lba\n", num);
1556229d4f11SHannes Reinecke 			ret = -EINVAL;
1557229d4f11SHannes Reinecke 			break;
1558229d4f11SHannes Reinecke 		}
1559229d4f11SHannes Reinecke 		ptr++;
1560229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
1561229d4f11SHannes Reinecke 		if (!ptr) {
1562229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing state definitions\n",
1563229d4f11SHannes Reinecke 			       num);
1564229d4f11SHannes Reinecke 			ret = -EINVAL;
1565229d4f11SHannes Reinecke 			break;
1566229d4f11SHannes Reinecke 		}
1567229d4f11SHannes Reinecke 		ptr++;
1568229d4f11SHannes Reinecke 		lba_map = core_alua_allocate_lba_map(&lba_list,
1569229d4f11SHannes Reinecke 						     start_lba, end_lba);
1570229d4f11SHannes Reinecke 		if (IS_ERR(lba_map)) {
1571229d4f11SHannes Reinecke 			ret = PTR_ERR(lba_map);
1572229d4f11SHannes Reinecke 			break;
1573229d4f11SHannes Reinecke 		}
1574229d4f11SHannes Reinecke 		pg = 0;
1575229d4f11SHannes Reinecke 		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1576229d4f11SHannes Reinecke 			switch (state) {
1577229d4f11SHannes Reinecke 			case 'O':
1578229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1579229d4f11SHannes Reinecke 				break;
1580229d4f11SHannes Reinecke 			case 'A':
1581229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1582229d4f11SHannes Reinecke 				break;
1583229d4f11SHannes Reinecke 			case 'S':
1584229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_STANDBY;
1585229d4f11SHannes Reinecke 				break;
1586229d4f11SHannes Reinecke 			case 'U':
1587229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1588229d4f11SHannes Reinecke 				break;
1589229d4f11SHannes Reinecke 			default:
1590229d4f11SHannes Reinecke 				pr_err("Invalid ALUA state '%c'\n", state);
1591229d4f11SHannes Reinecke 				ret = -EINVAL;
1592229d4f11SHannes Reinecke 				goto out;
1593229d4f11SHannes Reinecke 			}
1594229d4f11SHannes Reinecke 
1595229d4f11SHannes Reinecke 			ret = core_alua_allocate_lba_map_mem(lba_map,
1596229d4f11SHannes Reinecke 							     pg_id, alua_state);
1597229d4f11SHannes Reinecke 			if (ret) {
1598229d4f11SHannes Reinecke 				pr_err("Invalid target descriptor %d:%c "
1599229d4f11SHannes Reinecke 				       "at line %d\n",
1600229d4f11SHannes Reinecke 				       pg_id, state, num);
1601229d4f11SHannes Reinecke 				break;
1602229d4f11SHannes Reinecke 			}
1603229d4f11SHannes Reinecke 			pg++;
1604229d4f11SHannes Reinecke 			ptr = strchr(ptr, ' ');
1605229d4f11SHannes Reinecke 			if (ptr)
1606229d4f11SHannes Reinecke 				ptr++;
1607229d4f11SHannes Reinecke 			else
1608229d4f11SHannes Reinecke 				break;
1609229d4f11SHannes Reinecke 		}
1610229d4f11SHannes Reinecke 		if (pg_num == -1)
1611229d4f11SHannes Reinecke 		    pg_num = pg;
1612229d4f11SHannes Reinecke 		else if (pg != pg_num) {
1613229d4f11SHannes Reinecke 			pr_err("Only %d from %d port groups definitions "
1614229d4f11SHannes Reinecke 			       "at line %d\n", pg, pg_num, num);
1615229d4f11SHannes Reinecke 			ret = -EINVAL;
1616229d4f11SHannes Reinecke 			break;
1617229d4f11SHannes Reinecke 		}
1618229d4f11SHannes Reinecke 		num++;
1619229d4f11SHannes Reinecke 	}
1620229d4f11SHannes Reinecke out:
1621229d4f11SHannes Reinecke 	if (ret) {
1622229d4f11SHannes Reinecke 		core_alua_free_lba_map(&lba_list);
1623229d4f11SHannes Reinecke 		count = ret;
1624229d4f11SHannes Reinecke 	} else
1625229d4f11SHannes Reinecke 		core_alua_set_lba_map(dev, &lba_list,
1626229d4f11SHannes Reinecke 				      segment_size, segment_mult);
1627229d4f11SHannes Reinecke 	kfree(map_entries);
1628229d4f11SHannes Reinecke 	return count;
1629229d4f11SHannes Reinecke }
1630229d4f11SHannes Reinecke 
1631229d4f11SHannes Reinecke static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1632229d4f11SHannes Reinecke 	.attr	= { .ca_owner = THIS_MODULE,
1633229d4f11SHannes Reinecke 		    .ca_name = "lba_map",
1634229d4f11SHannes Reinecke 		    .ca_mode = S_IRUGO | S_IWUSR },
1635229d4f11SHannes Reinecke 	.show	= target_core_show_dev_lba_map,
1636229d4f11SHannes Reinecke 	.store	= target_core_store_dev_lba_map,
1637229d4f11SHannes Reinecke };
1638229d4f11SHannes Reinecke 
163973112edcSNicholas Bellinger static struct configfs_attribute *target_core_dev_attrs[] = {
1640c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_info.attr,
1641c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_control.attr,
1642c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_alias.attr,
1643c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_udev_path.attr,
1644c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_enable.attr,
1645c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_alua_lu_gp.attr,
1646229d4f11SHannes Reinecke 	&target_core_attr_dev_lba_map.attr,
1647c66ac9dbSNicholas Bellinger 	NULL,
1648c66ac9dbSNicholas Bellinger };
1649c66ac9dbSNicholas Bellinger 
1650c66ac9dbSNicholas Bellinger static void target_core_dev_release(struct config_item *item)
1651c66ac9dbSNicholas Bellinger {
16520fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
16530fd97ccfSChristoph Hellwig 	struct se_device *dev =
16540fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
1655c66ac9dbSNicholas Bellinger 
1656c66ac9dbSNicholas Bellinger 	kfree(dev_cg->default_groups);
16570fd97ccfSChristoph Hellwig 	target_free_device(dev);
1658c66ac9dbSNicholas Bellinger }
1659c66ac9dbSNicholas Bellinger 
1660c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_show(struct config_item *item,
1661c66ac9dbSNicholas Bellinger 				     struct configfs_attribute *attr,
1662c66ac9dbSNicholas Bellinger 				     char *page)
1663c66ac9dbSNicholas Bellinger {
16640fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
16650fd97ccfSChristoph Hellwig 	struct se_device *dev =
16660fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
1667c66ac9dbSNicholas Bellinger 	struct target_core_configfs_attribute *tc_attr = container_of(
1668c66ac9dbSNicholas Bellinger 			attr, struct target_core_configfs_attribute, attr);
1669c66ac9dbSNicholas Bellinger 
16706708bb27SAndy Grover 	if (!tc_attr->show)
1671c66ac9dbSNicholas Bellinger 		return -EINVAL;
1672c66ac9dbSNicholas Bellinger 
16730fd97ccfSChristoph Hellwig 	return tc_attr->show(dev, page);
1674c66ac9dbSNicholas Bellinger }
1675c66ac9dbSNicholas Bellinger 
1676c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_store(struct config_item *item,
1677c66ac9dbSNicholas Bellinger 				      struct configfs_attribute *attr,
1678c66ac9dbSNicholas Bellinger 				      const char *page, size_t count)
1679c66ac9dbSNicholas Bellinger {
16800fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
16810fd97ccfSChristoph Hellwig 	struct se_device *dev =
16820fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
1683c66ac9dbSNicholas Bellinger 	struct target_core_configfs_attribute *tc_attr = container_of(
1684c66ac9dbSNicholas Bellinger 			attr, struct target_core_configfs_attribute, attr);
1685c66ac9dbSNicholas Bellinger 
16866708bb27SAndy Grover 	if (!tc_attr->store)
1687c66ac9dbSNicholas Bellinger 		return -EINVAL;
1688c66ac9dbSNicholas Bellinger 
16890fd97ccfSChristoph Hellwig 	return tc_attr->store(dev, page, count);
1690c66ac9dbSNicholas Bellinger }
1691c66ac9dbSNicholas Bellinger 
1692c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_item_ops = {
1693c66ac9dbSNicholas Bellinger 	.release		= target_core_dev_release,
1694c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_dev_show,
1695c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_dev_store,
1696c66ac9dbSNicholas Bellinger };
1697c66ac9dbSNicholas Bellinger 
169873112edcSNicholas Bellinger TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
1699c66ac9dbSNicholas Bellinger 
170073112edcSNicholas Bellinger /* End functions for struct config_item_type tb_dev_cit */
1701c66ac9dbSNicholas Bellinger 
1702c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1703c66ac9dbSNicholas Bellinger 
1704c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1705c66ac9dbSNicholas Bellinger #define SE_DEV_ALUA_LU_ATTR(_name, _mode)				\
1706c66ac9dbSNicholas Bellinger static struct target_core_alua_lu_gp_attribute				\
1707c66ac9dbSNicholas Bellinger 			target_core_alua_lu_gp_##_name =		\
1708c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR(_name, _mode,					\
1709c66ac9dbSNicholas Bellinger 	target_core_alua_lu_gp_show_attr_##_name,			\
1710c66ac9dbSNicholas Bellinger 	target_core_alua_lu_gp_store_attr_##_name);
1711c66ac9dbSNicholas Bellinger 
1712c66ac9dbSNicholas Bellinger #define SE_DEV_ALUA_LU_ATTR_RO(_name)					\
1713c66ac9dbSNicholas Bellinger static struct target_core_alua_lu_gp_attribute				\
1714c66ac9dbSNicholas Bellinger 			target_core_alua_lu_gp_##_name =		\
1715c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR_RO(_name,					\
1716c66ac9dbSNicholas Bellinger 	target_core_alua_lu_gp_show_attr_##_name);
1717c66ac9dbSNicholas Bellinger 
1718c66ac9dbSNicholas Bellinger /*
1719c66ac9dbSNicholas Bellinger  * lu_gp_id
1720c66ac9dbSNicholas Bellinger  */
1721c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
1722c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp,
1723c66ac9dbSNicholas Bellinger 	char *page)
1724c66ac9dbSNicholas Bellinger {
17256708bb27SAndy Grover 	if (!lu_gp->lu_gp_valid_id)
1726c66ac9dbSNicholas Bellinger 		return 0;
1727c66ac9dbSNicholas Bellinger 
1728c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
1729c66ac9dbSNicholas Bellinger }
1730c66ac9dbSNicholas Bellinger 
1731c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
1732c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp,
1733c66ac9dbSNicholas Bellinger 	const char *page,
1734c66ac9dbSNicholas Bellinger 	size_t count)
1735c66ac9dbSNicholas Bellinger {
1736c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
1737c66ac9dbSNicholas Bellinger 	unsigned long lu_gp_id;
1738c66ac9dbSNicholas Bellinger 	int ret;
1739c66ac9dbSNicholas Bellinger 
174057103d7fSJingoo Han 	ret = kstrtoul(page, 0, &lu_gp_id);
1741c66ac9dbSNicholas Bellinger 	if (ret < 0) {
174257103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
1743c66ac9dbSNicholas Bellinger 			" lu_gp_id\n", ret);
174457103d7fSJingoo Han 		return ret;
1745c66ac9dbSNicholas Bellinger 	}
1746c66ac9dbSNicholas Bellinger 	if (lu_gp_id > 0x0000ffff) {
17476708bb27SAndy Grover 		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
1748c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", lu_gp_id);
1749c66ac9dbSNicholas Bellinger 		return -EINVAL;
1750c66ac9dbSNicholas Bellinger 	}
1751c66ac9dbSNicholas Bellinger 
1752c66ac9dbSNicholas Bellinger 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
1753c66ac9dbSNicholas Bellinger 	if (ret < 0)
1754c66ac9dbSNicholas Bellinger 		return -EINVAL;
1755c66ac9dbSNicholas Bellinger 
17566708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
1757c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
1758c66ac9dbSNicholas Bellinger 		config_item_name(&alua_lu_gp_cg->cg_item),
1759c66ac9dbSNicholas Bellinger 		lu_gp->lu_gp_id);
1760c66ac9dbSNicholas Bellinger 
1761c66ac9dbSNicholas Bellinger 	return count;
1762c66ac9dbSNicholas Bellinger }
1763c66ac9dbSNicholas Bellinger 
1764c66ac9dbSNicholas Bellinger SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
1765c66ac9dbSNicholas Bellinger 
1766c66ac9dbSNicholas Bellinger /*
1767c66ac9dbSNicholas Bellinger  * members
1768c66ac9dbSNicholas Bellinger  */
1769c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_lu_gp_show_attr_members(
1770c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp,
1771c66ac9dbSNicholas Bellinger 	char *page)
1772c66ac9dbSNicholas Bellinger {
1773c66ac9dbSNicholas Bellinger 	struct se_device *dev;
1774c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
1775c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1776c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
1777c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
1778c66ac9dbSNicholas Bellinger 
1779c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
1780c66ac9dbSNicholas Bellinger 
1781c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp->lu_gp_lock);
1782c66ac9dbSNicholas Bellinger 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1783c66ac9dbSNicholas Bellinger 		dev = lu_gp_mem->lu_gp_mem_dev;
17840fd97ccfSChristoph Hellwig 		hba = dev->se_hba;
1785c66ac9dbSNicholas Bellinger 
1786c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
1787c66ac9dbSNicholas Bellinger 			config_item_name(&hba->hba_group.cg_item),
17880fd97ccfSChristoph Hellwig 			config_item_name(&dev->dev_group.cg_item));
1789c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
1790c66ac9dbSNicholas Bellinger 
1791c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
17926708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
1793c66ac9dbSNicholas Bellinger 				"_members buffer\n");
1794c66ac9dbSNicholas Bellinger 			break;
1795c66ac9dbSNicholas Bellinger 		}
1796c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
1797c66ac9dbSNicholas Bellinger 		len += cur_len;
1798c66ac9dbSNicholas Bellinger 	}
1799c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp->lu_gp_lock);
1800c66ac9dbSNicholas Bellinger 
1801c66ac9dbSNicholas Bellinger 	return len;
1802c66ac9dbSNicholas Bellinger }
1803c66ac9dbSNicholas Bellinger 
1804c66ac9dbSNicholas Bellinger SE_DEV_ALUA_LU_ATTR_RO(members);
1805c66ac9dbSNicholas Bellinger 
1806c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
1807c66ac9dbSNicholas Bellinger 
1808c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
1809c66ac9dbSNicholas Bellinger 	&target_core_alua_lu_gp_lu_gp_id.attr,
1810c66ac9dbSNicholas Bellinger 	&target_core_alua_lu_gp_members.attr,
1811c66ac9dbSNicholas Bellinger 	NULL,
1812c66ac9dbSNicholas Bellinger };
1813c66ac9dbSNicholas Bellinger 
18141f6fe7cbSNicholas Bellinger static void target_core_alua_lu_gp_release(struct config_item *item)
18151f6fe7cbSNicholas Bellinger {
18161f6fe7cbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
18171f6fe7cbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
18181f6fe7cbSNicholas Bellinger 
18191f6fe7cbSNicholas Bellinger 	core_alua_free_lu_gp(lu_gp);
18201f6fe7cbSNicholas Bellinger }
18211f6fe7cbSNicholas Bellinger 
1822c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_lu_gp_ops = {
18231f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_lu_gp_release,
1824c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_alua_lu_gp_attr_show,
1825c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_alua_lu_gp_attr_store,
1826c66ac9dbSNicholas Bellinger };
1827c66ac9dbSNicholas Bellinger 
1828c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gp_cit = {
1829c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
1830c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_lu_gp_attrs,
1831c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
1832c66ac9dbSNicholas Bellinger };
1833c66ac9dbSNicholas Bellinger 
1834c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
1835c66ac9dbSNicholas Bellinger 
1836c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
1837c66ac9dbSNicholas Bellinger 
1838c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_lu_gp(
1839c66ac9dbSNicholas Bellinger 	struct config_group *group,
1840c66ac9dbSNicholas Bellinger 	const char *name)
1841c66ac9dbSNicholas Bellinger {
1842c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
1843c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = NULL;
1844c66ac9dbSNicholas Bellinger 	struct config_item *alua_lu_gp_ci = NULL;
1845c66ac9dbSNicholas Bellinger 
1846c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp(name, 0);
1847c66ac9dbSNicholas Bellinger 	if (IS_ERR(lu_gp))
1848c66ac9dbSNicholas Bellinger 		return NULL;
1849c66ac9dbSNicholas Bellinger 
1850c66ac9dbSNicholas Bellinger 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
1851c66ac9dbSNicholas Bellinger 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
1852c66ac9dbSNicholas Bellinger 
1853c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_lu_gp_cg, name,
1854c66ac9dbSNicholas Bellinger 			&target_core_alua_lu_gp_cit);
1855c66ac9dbSNicholas Bellinger 
18566708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
1857c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s\n",
1858c66ac9dbSNicholas Bellinger 		config_item_name(alua_lu_gp_ci));
1859c66ac9dbSNicholas Bellinger 
1860c66ac9dbSNicholas Bellinger 	return alua_lu_gp_cg;
1861c66ac9dbSNicholas Bellinger 
1862c66ac9dbSNicholas Bellinger }
1863c66ac9dbSNicholas Bellinger 
1864c66ac9dbSNicholas Bellinger static void target_core_alua_drop_lu_gp(
1865c66ac9dbSNicholas Bellinger 	struct config_group *group,
1866c66ac9dbSNicholas Bellinger 	struct config_item *item)
1867c66ac9dbSNicholas Bellinger {
1868c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1869c66ac9dbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
1870c66ac9dbSNicholas Bellinger 
18716708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
1872c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
1873c66ac9dbSNicholas Bellinger 		config_item_name(item), lu_gp->lu_gp_id);
18741f6fe7cbSNicholas Bellinger 	/*
18751f6fe7cbSNicholas Bellinger 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
18761f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_lu_gp_release()
18771f6fe7cbSNicholas Bellinger 	 */
1878c66ac9dbSNicholas Bellinger 	config_item_put(item);
1879c66ac9dbSNicholas Bellinger }
1880c66ac9dbSNicholas Bellinger 
1881c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
1882c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_lu_gp,
1883c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_lu_gp,
1884c66ac9dbSNicholas Bellinger };
1885c66ac9dbSNicholas Bellinger 
1886c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gps_cit = {
1887c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
1888c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
1889c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
1890c66ac9dbSNicholas Bellinger };
1891c66ac9dbSNicholas Bellinger 
1892c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
1893c66ac9dbSNicholas Bellinger 
1894c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
1895c66ac9dbSNicholas Bellinger 
1896c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
1897c66ac9dbSNicholas Bellinger #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode)				\
1898c66ac9dbSNicholas Bellinger static struct target_core_alua_tg_pt_gp_attribute			\
1899c66ac9dbSNicholas Bellinger 			target_core_alua_tg_pt_gp_##_name =		\
1900c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR(_name, _mode,					\
1901c66ac9dbSNicholas Bellinger 	target_core_alua_tg_pt_gp_show_attr_##_name,			\
1902c66ac9dbSNicholas Bellinger 	target_core_alua_tg_pt_gp_store_attr_##_name);
1903c66ac9dbSNicholas Bellinger 
1904c66ac9dbSNicholas Bellinger #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name)				\
1905c66ac9dbSNicholas Bellinger static struct target_core_alua_tg_pt_gp_attribute			\
1906c66ac9dbSNicholas Bellinger 			target_core_alua_tg_pt_gp_##_name =		\
1907c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR_RO(_name,					\
1908c66ac9dbSNicholas Bellinger 	target_core_alua_tg_pt_gp_show_attr_##_name);
1909c66ac9dbSNicholas Bellinger 
1910c66ac9dbSNicholas Bellinger /*
1911c66ac9dbSNicholas Bellinger  * alua_access_state
1912c66ac9dbSNicholas Bellinger  */
1913c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
1914c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
1915c66ac9dbSNicholas Bellinger 	char *page)
1916c66ac9dbSNicholas Bellinger {
1917c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n",
1918c66ac9dbSNicholas Bellinger 		atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
1919c66ac9dbSNicholas Bellinger }
1920c66ac9dbSNicholas Bellinger 
1921c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
1922c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
1923c66ac9dbSNicholas Bellinger 	const char *page,
1924c66ac9dbSNicholas Bellinger 	size_t count)
1925c66ac9dbSNicholas Bellinger {
19260fd97ccfSChristoph Hellwig 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1927c66ac9dbSNicholas Bellinger 	unsigned long tmp;
1928c66ac9dbSNicholas Bellinger 	int new_state, ret;
1929c66ac9dbSNicholas Bellinger 
19306708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
1931125d0119SHannes Reinecke 		pr_err("Unable to do implicit ALUA on non valid"
1932c66ac9dbSNicholas Bellinger 			" tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
1933c66ac9dbSNicholas Bellinger 		return -EINVAL;
1934c66ac9dbSNicholas Bellinger 	}
1935f1453773SNicholas Bellinger 	if (!(dev->dev_flags & DF_CONFIGURED)) {
1936f1453773SNicholas Bellinger 		pr_err("Unable to set alua_access_state while device is"
1937f1453773SNicholas Bellinger 		       " not configured\n");
1938f1453773SNicholas Bellinger 		return -ENODEV;
1939f1453773SNicholas Bellinger 	}
1940c66ac9dbSNicholas Bellinger 
194157103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
1942c66ac9dbSNicholas Bellinger 	if (ret < 0) {
19436708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access state from"
1944c66ac9dbSNicholas Bellinger 				" %s\n", page);
194557103d7fSJingoo Han 		return ret;
1946c66ac9dbSNicholas Bellinger 	}
1947c66ac9dbSNicholas Bellinger 	new_state = (int)tmp;
1948c66ac9dbSNicholas Bellinger 
1949125d0119SHannes Reinecke 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
1950125d0119SHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA"
1951125d0119SHannes Reinecke 			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
1952c66ac9dbSNicholas Bellinger 		return -EINVAL;
1953c66ac9dbSNicholas Bellinger 	}
1954c66094bfSHannes Reinecke 	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
1955c66094bfSHannes Reinecke 	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
1956c66094bfSHannes Reinecke 		/* LBA DEPENDENT is only allowed with implicit ALUA */
1957c66094bfSHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA transition"
1958c66094bfSHannes Reinecke 		       " while explicit ALUA management is enabled\n");
1959c66094bfSHannes Reinecke 		return -EINVAL;
1960c66094bfSHannes Reinecke 	}
1961c66ac9dbSNicholas Bellinger 
19620fd97ccfSChristoph Hellwig 	ret = core_alua_do_port_transition(tg_pt_gp, dev,
1963c66ac9dbSNicholas Bellinger 					NULL, NULL, new_state, 0);
1964c66ac9dbSNicholas Bellinger 	return (!ret) ? count : -EINVAL;
1965c66ac9dbSNicholas Bellinger }
1966c66ac9dbSNicholas Bellinger 
1967c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
1968c66ac9dbSNicholas Bellinger 
1969c66ac9dbSNicholas Bellinger /*
1970c66ac9dbSNicholas Bellinger  * alua_access_status
1971c66ac9dbSNicholas Bellinger  */
1972c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
1973c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
1974c66ac9dbSNicholas Bellinger 	char *page)
1975c66ac9dbSNicholas Bellinger {
1976c66ac9dbSNicholas Bellinger 	return sprintf(page, "%s\n",
1977c66ac9dbSNicholas Bellinger 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
1978c66ac9dbSNicholas Bellinger }
1979c66ac9dbSNicholas Bellinger 
1980c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
1981c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
1982c66ac9dbSNicholas Bellinger 	const char *page,
1983c66ac9dbSNicholas Bellinger 	size_t count)
1984c66ac9dbSNicholas Bellinger {
1985c66ac9dbSNicholas Bellinger 	unsigned long tmp;
1986c66ac9dbSNicholas Bellinger 	int new_status, ret;
1987c66ac9dbSNicholas Bellinger 
19886708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
19896708bb27SAndy Grover 		pr_err("Unable to do set ALUA access status on non"
1990c66ac9dbSNicholas Bellinger 			" valid tg_pt_gp ID: %hu\n",
1991c66ac9dbSNicholas Bellinger 			tg_pt_gp->tg_pt_gp_valid_id);
1992c66ac9dbSNicholas Bellinger 		return -EINVAL;
1993c66ac9dbSNicholas Bellinger 	}
1994c66ac9dbSNicholas Bellinger 
199557103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
1996c66ac9dbSNicholas Bellinger 	if (ret < 0) {
19976708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access status"
1998c66ac9dbSNicholas Bellinger 				" from %s\n", page);
199957103d7fSJingoo Han 		return ret;
2000c66ac9dbSNicholas Bellinger 	}
2001c66ac9dbSNicholas Bellinger 	new_status = (int)tmp;
2002c66ac9dbSNicholas Bellinger 
2003c66ac9dbSNicholas Bellinger 	if ((new_status != ALUA_STATUS_NONE) &&
2004125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2005125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
20066708bb27SAndy Grover 		pr_err("Illegal ALUA access status: 0x%02x\n",
2007c66ac9dbSNicholas Bellinger 				new_status);
2008c66ac9dbSNicholas Bellinger 		return -EINVAL;
2009c66ac9dbSNicholas Bellinger 	}
2010c66ac9dbSNicholas Bellinger 
2011c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2012c66ac9dbSNicholas Bellinger 	return count;
2013c66ac9dbSNicholas Bellinger }
2014c66ac9dbSNicholas Bellinger 
2015c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2016c66ac9dbSNicholas Bellinger 
2017c66ac9dbSNicholas Bellinger /*
2018c66ac9dbSNicholas Bellinger  * alua_access_type
2019c66ac9dbSNicholas Bellinger  */
2020c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2021c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2022c66ac9dbSNicholas Bellinger 	char *page)
2023c66ac9dbSNicholas Bellinger {
2024c66ac9dbSNicholas Bellinger 	return core_alua_show_access_type(tg_pt_gp, page);
2025c66ac9dbSNicholas Bellinger }
2026c66ac9dbSNicholas Bellinger 
2027c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2028c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2029c66ac9dbSNicholas Bellinger 	const char *page,
2030c66ac9dbSNicholas Bellinger 	size_t count)
2031c66ac9dbSNicholas Bellinger {
2032c66ac9dbSNicholas Bellinger 	return core_alua_store_access_type(tg_pt_gp, page, count);
2033c66ac9dbSNicholas Bellinger }
2034c66ac9dbSNicholas Bellinger 
2035c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2036c66ac9dbSNicholas Bellinger 
2037c66ac9dbSNicholas Bellinger /*
20386be526c4SHannes Reinecke  * alua_supported_states
20396be526c4SHannes Reinecke  */
2040b0a382c5SHannes Reinecke 
2041b0a382c5SHannes Reinecke #define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit)		\
2042b0a382c5SHannes Reinecke static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2043b0a382c5SHannes Reinecke 	struct t10_alua_tg_pt_gp *t, char *p)				\
2044b0a382c5SHannes Reinecke {									\
2045b0a382c5SHannes Reinecke 	return sprintf(p, "%d\n", !!(t->_var & _bit));			\
20466be526c4SHannes Reinecke }
20476be526c4SHannes Reinecke 
2048b0a382c5SHannes Reinecke #define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit)		\
2049b0a382c5SHannes Reinecke static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2050b0a382c5SHannes Reinecke 	struct t10_alua_tg_pt_gp *t, const char *p, size_t c)		\
2051b0a382c5SHannes Reinecke {									\
2052b0a382c5SHannes Reinecke 	unsigned long tmp;						\
2053b0a382c5SHannes Reinecke 	int ret;							\
2054b0a382c5SHannes Reinecke 									\
2055b0a382c5SHannes Reinecke 	if (!t->tg_pt_gp_valid_id) {					\
2056b0a382c5SHannes Reinecke 		pr_err("Unable to do set ##_name ALUA state on non"	\
2057b0a382c5SHannes Reinecke 		       " valid tg_pt_gp ID: %hu\n",			\
2058b0a382c5SHannes Reinecke 		       t->tg_pt_gp_valid_id);				\
2059b0a382c5SHannes Reinecke 		return -EINVAL;						\
2060b0a382c5SHannes Reinecke 	}								\
2061b0a382c5SHannes Reinecke 									\
2062b0a382c5SHannes Reinecke 	ret = kstrtoul(p, 0, &tmp);					\
2063b0a382c5SHannes Reinecke 	if (ret < 0) {							\
2064b0a382c5SHannes Reinecke 		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
2065b0a382c5SHannes Reinecke 		return -EINVAL;						\
2066b0a382c5SHannes Reinecke 	}								\
2067b0a382c5SHannes Reinecke 	if (tmp > 1) {							\
2068b0a382c5SHannes Reinecke 		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2069b0a382c5SHannes Reinecke 		return -EINVAL;						\
2070b0a382c5SHannes Reinecke 	}								\
20711f0b030cSSebastian Herbszt 	if (tmp)							\
2072b0a382c5SHannes Reinecke 		t->_var |= _bit;					\
2073b0a382c5SHannes Reinecke 	else								\
2074b0a382c5SHannes Reinecke 		t->_var &= ~_bit;					\
2075b0a382c5SHannes Reinecke 									\
2076b0a382c5SHannes Reinecke 	return c;							\
20776be526c4SHannes Reinecke }
20786be526c4SHannes Reinecke 
2079b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2080b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2081b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2082b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2083b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
20846be526c4SHannes Reinecke 
2085b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2086b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2087b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2088b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2089b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
20906be526c4SHannes Reinecke 
2091b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2092b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2093b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2094b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2095c66094bfSHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
20966be526c4SHannes Reinecke 
2097b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2098b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2099b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2100b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2101b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
21026be526c4SHannes Reinecke 
2103b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2104b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2105b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2106b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2107b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2108b0a382c5SHannes Reinecke 
2109b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2110b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2111b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2112b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2113b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2114b0a382c5SHannes Reinecke 
2115b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2116b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2117b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2118b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2119b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
21206be526c4SHannes Reinecke 
21216be526c4SHannes Reinecke /*
2122c66ac9dbSNicholas Bellinger  * alua_write_metadata
2123c66ac9dbSNicholas Bellinger  */
2124c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2125c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2126c66ac9dbSNicholas Bellinger 	char *page)
2127c66ac9dbSNicholas Bellinger {
2128c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2129c66ac9dbSNicholas Bellinger }
2130c66ac9dbSNicholas Bellinger 
2131c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2132c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2133c66ac9dbSNicholas Bellinger 	const char *page,
2134c66ac9dbSNicholas Bellinger 	size_t count)
2135c66ac9dbSNicholas Bellinger {
2136c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2137c66ac9dbSNicholas Bellinger 	int ret;
2138c66ac9dbSNicholas Bellinger 
213957103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2140c66ac9dbSNicholas Bellinger 	if (ret < 0) {
21416708bb27SAndy Grover 		pr_err("Unable to extract alua_write_metadata\n");
214257103d7fSJingoo Han 		return ret;
2143c66ac9dbSNicholas Bellinger 	}
2144c66ac9dbSNicholas Bellinger 
2145c66ac9dbSNicholas Bellinger 	if ((tmp != 0) && (tmp != 1)) {
21466708bb27SAndy Grover 		pr_err("Illegal value for alua_write_metadata:"
2147c66ac9dbSNicholas Bellinger 			" %lu\n", tmp);
2148c66ac9dbSNicholas Bellinger 		return -EINVAL;
2149c66ac9dbSNicholas Bellinger 	}
2150c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2151c66ac9dbSNicholas Bellinger 
2152c66ac9dbSNicholas Bellinger 	return count;
2153c66ac9dbSNicholas Bellinger }
2154c66ac9dbSNicholas Bellinger 
2155c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2156c66ac9dbSNicholas Bellinger 
2157c66ac9dbSNicholas Bellinger 
2158c66ac9dbSNicholas Bellinger 
2159c66ac9dbSNicholas Bellinger /*
2160c66ac9dbSNicholas Bellinger  * nonop_delay_msecs
2161c66ac9dbSNicholas Bellinger  */
2162c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2163c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2164c66ac9dbSNicholas Bellinger 	char *page)
2165c66ac9dbSNicholas Bellinger {
2166c66ac9dbSNicholas Bellinger 	return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2167c66ac9dbSNicholas Bellinger 
2168c66ac9dbSNicholas Bellinger }
2169c66ac9dbSNicholas Bellinger 
2170c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2171c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2172c66ac9dbSNicholas Bellinger 	const char *page,
2173c66ac9dbSNicholas Bellinger 	size_t count)
2174c66ac9dbSNicholas Bellinger {
2175c66ac9dbSNicholas Bellinger 	return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2176c66ac9dbSNicholas Bellinger }
2177c66ac9dbSNicholas Bellinger 
2178c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2179c66ac9dbSNicholas Bellinger 
2180c66ac9dbSNicholas Bellinger /*
2181c66ac9dbSNicholas Bellinger  * trans_delay_msecs
2182c66ac9dbSNicholas Bellinger  */
2183c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2184c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2185c66ac9dbSNicholas Bellinger 	char *page)
2186c66ac9dbSNicholas Bellinger {
2187c66ac9dbSNicholas Bellinger 	return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2188c66ac9dbSNicholas Bellinger }
2189c66ac9dbSNicholas Bellinger 
2190c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2191c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2192c66ac9dbSNicholas Bellinger 	const char *page,
2193c66ac9dbSNicholas Bellinger 	size_t count)
2194c66ac9dbSNicholas Bellinger {
2195c66ac9dbSNicholas Bellinger 	return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2196c66ac9dbSNicholas Bellinger }
2197c66ac9dbSNicholas Bellinger 
2198c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2199c66ac9dbSNicholas Bellinger 
2200c66ac9dbSNicholas Bellinger /*
2201125d0119SHannes Reinecke  * implicit_trans_secs
22025b9a4d72SNicholas Bellinger  */
2203125d0119SHannes Reinecke static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
22045b9a4d72SNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
22055b9a4d72SNicholas Bellinger 	char *page)
22065b9a4d72SNicholas Bellinger {
2207125d0119SHannes Reinecke 	return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
22085b9a4d72SNicholas Bellinger }
22095b9a4d72SNicholas Bellinger 
2210125d0119SHannes Reinecke static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
22115b9a4d72SNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
22125b9a4d72SNicholas Bellinger 	const char *page,
22135b9a4d72SNicholas Bellinger 	size_t count)
22145b9a4d72SNicholas Bellinger {
2215125d0119SHannes Reinecke 	return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
22165b9a4d72SNicholas Bellinger }
22175b9a4d72SNicholas Bellinger 
2218125d0119SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
22195b9a4d72SNicholas Bellinger 
22205b9a4d72SNicholas Bellinger /*
2221c66ac9dbSNicholas Bellinger  * preferred
2222c66ac9dbSNicholas Bellinger  */
2223c66ac9dbSNicholas Bellinger 
2224c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2225c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2226c66ac9dbSNicholas Bellinger 	char *page)
2227c66ac9dbSNicholas Bellinger {
2228c66ac9dbSNicholas Bellinger 	return core_alua_show_preferred_bit(tg_pt_gp, page);
2229c66ac9dbSNicholas Bellinger }
2230c66ac9dbSNicholas Bellinger 
2231c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2232c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2233c66ac9dbSNicholas Bellinger 	const char *page,
2234c66ac9dbSNicholas Bellinger 	size_t count)
2235c66ac9dbSNicholas Bellinger {
2236c66ac9dbSNicholas Bellinger 	return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2237c66ac9dbSNicholas Bellinger }
2238c66ac9dbSNicholas Bellinger 
2239c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2240c66ac9dbSNicholas Bellinger 
2241c66ac9dbSNicholas Bellinger /*
2242c66ac9dbSNicholas Bellinger  * tg_pt_gp_id
2243c66ac9dbSNicholas Bellinger  */
2244c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2245c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2246c66ac9dbSNicholas Bellinger 	char *page)
2247c66ac9dbSNicholas Bellinger {
22486708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id)
2249c66ac9dbSNicholas Bellinger 		return 0;
2250c66ac9dbSNicholas Bellinger 
2251c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2252c66ac9dbSNicholas Bellinger }
2253c66ac9dbSNicholas Bellinger 
2254c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2255c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2256c66ac9dbSNicholas Bellinger 	const char *page,
2257c66ac9dbSNicholas Bellinger 	size_t count)
2258c66ac9dbSNicholas Bellinger {
2259c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2260c66ac9dbSNicholas Bellinger 	unsigned long tg_pt_gp_id;
2261c66ac9dbSNicholas Bellinger 	int ret;
2262c66ac9dbSNicholas Bellinger 
226357103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tg_pt_gp_id);
2264c66ac9dbSNicholas Bellinger 	if (ret < 0) {
226557103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2266c66ac9dbSNicholas Bellinger 			" tg_pt_gp_id\n", ret);
226757103d7fSJingoo Han 		return ret;
2268c66ac9dbSNicholas Bellinger 	}
2269c66ac9dbSNicholas Bellinger 	if (tg_pt_gp_id > 0x0000ffff) {
22706708bb27SAndy Grover 		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2271c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", tg_pt_gp_id);
2272c66ac9dbSNicholas Bellinger 		return -EINVAL;
2273c66ac9dbSNicholas Bellinger 	}
2274c66ac9dbSNicholas Bellinger 
2275c66ac9dbSNicholas Bellinger 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2276c66ac9dbSNicholas Bellinger 	if (ret < 0)
2277c66ac9dbSNicholas Bellinger 		return -EINVAL;
2278c66ac9dbSNicholas Bellinger 
22796708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2280c66ac9dbSNicholas Bellinger 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
2281c66ac9dbSNicholas Bellinger 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
2282c66ac9dbSNicholas Bellinger 		tg_pt_gp->tg_pt_gp_id);
2283c66ac9dbSNicholas Bellinger 
2284c66ac9dbSNicholas Bellinger 	return count;
2285c66ac9dbSNicholas Bellinger }
2286c66ac9dbSNicholas Bellinger 
2287c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2288c66ac9dbSNicholas Bellinger 
2289c66ac9dbSNicholas Bellinger /*
2290c66ac9dbSNicholas Bellinger  * members
2291c66ac9dbSNicholas Bellinger  */
2292c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2293c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2294c66ac9dbSNicholas Bellinger 	char *page)
2295c66ac9dbSNicholas Bellinger {
2296c66ac9dbSNicholas Bellinger 	struct se_port *port;
2297c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg;
2298c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
2299c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2300c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2301c66ac9dbSNicholas Bellinger 	unsigned char buf[TG_PT_GROUP_NAME_BUF];
2302c66ac9dbSNicholas Bellinger 
2303c66ac9dbSNicholas Bellinger 	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2304c66ac9dbSNicholas Bellinger 
2305c66ac9dbSNicholas Bellinger 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2306c66ac9dbSNicholas Bellinger 	list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2307c66ac9dbSNicholas Bellinger 			tg_pt_gp_mem_list) {
2308c66ac9dbSNicholas Bellinger 		port = tg_pt_gp_mem->tg_pt;
2309c66ac9dbSNicholas Bellinger 		tpg = port->sep_tpg;
2310c66ac9dbSNicholas Bellinger 		lun = port->sep_lun;
2311c66ac9dbSNicholas Bellinger 
2312c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2313e3d6f909SAndy Grover 			"/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2314e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2315e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
2316c66ac9dbSNicholas Bellinger 			config_item_name(&lun->lun_group.cg_item));
2317c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2318c66ac9dbSNicholas Bellinger 
2319c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
23206708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2321c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2322c66ac9dbSNicholas Bellinger 			break;
2323c66ac9dbSNicholas Bellinger 		}
2324c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2325c66ac9dbSNicholas Bellinger 		len += cur_len;
2326c66ac9dbSNicholas Bellinger 	}
2327c66ac9dbSNicholas Bellinger 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2328c66ac9dbSNicholas Bellinger 
2329c66ac9dbSNicholas Bellinger 	return len;
2330c66ac9dbSNicholas Bellinger }
2331c66ac9dbSNicholas Bellinger 
2332c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2333c66ac9dbSNicholas Bellinger 
2334c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2335c66ac9dbSNicholas Bellinger 			tg_pt_gp_group);
2336c66ac9dbSNicholas Bellinger 
2337c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2338c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_alua_access_state.attr,
2339c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_alua_access_status.attr,
2340c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_alua_access_type.attr,
2341b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2342b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_offline.attr,
2343b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2344b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2345b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_standby.attr,
2346b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2347b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
2348c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2349c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2350c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2351125d0119SHannes Reinecke 	&target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
2352c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_preferred.attr,
2353c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2354c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_members.attr,
2355c66ac9dbSNicholas Bellinger 	NULL,
2356c66ac9dbSNicholas Bellinger };
2357c66ac9dbSNicholas Bellinger 
23581f6fe7cbSNicholas Bellinger static void target_core_alua_tg_pt_gp_release(struct config_item *item)
23591f6fe7cbSNicholas Bellinger {
23601f6fe7cbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
23611f6fe7cbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
23621f6fe7cbSNicholas Bellinger 
23631f6fe7cbSNicholas Bellinger 	core_alua_free_tg_pt_gp(tg_pt_gp);
23641f6fe7cbSNicholas Bellinger }
23651f6fe7cbSNicholas Bellinger 
2366c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
23671f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_tg_pt_gp_release,
2368c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_alua_tg_pt_gp_attr_show,
2369c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_alua_tg_pt_gp_attr_store,
2370c66ac9dbSNicholas Bellinger };
2371c66ac9dbSNicholas Bellinger 
2372c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2373c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
2374c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
2375c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2376c66ac9dbSNicholas Bellinger };
2377c66ac9dbSNicholas Bellinger 
2378c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2379c66ac9dbSNicholas Bellinger 
238072aca57bSNicholas Bellinger /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2381c66ac9dbSNicholas Bellinger 
2382c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_tg_pt_gp(
2383c66ac9dbSNicholas Bellinger 	struct config_group *group,
2384c66ac9dbSNicholas Bellinger 	const char *name)
2385c66ac9dbSNicholas Bellinger {
2386c66ac9dbSNicholas Bellinger 	struct t10_alua *alua = container_of(group, struct t10_alua,
2387c66ac9dbSNicholas Bellinger 					alua_tg_pt_gps_group);
2388c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2389c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = NULL;
2390c66ac9dbSNicholas Bellinger 	struct config_item *alua_tg_pt_gp_ci = NULL;
2391c66ac9dbSNicholas Bellinger 
23920fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
23936708bb27SAndy Grover 	if (!tg_pt_gp)
2394c66ac9dbSNicholas Bellinger 		return NULL;
2395c66ac9dbSNicholas Bellinger 
2396c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2397c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2398c66ac9dbSNicholas Bellinger 
2399c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
2400c66ac9dbSNicholas Bellinger 			&target_core_alua_tg_pt_gp_cit);
2401c66ac9dbSNicholas Bellinger 
24026708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2403c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s\n",
2404c66ac9dbSNicholas Bellinger 		config_item_name(alua_tg_pt_gp_ci));
2405c66ac9dbSNicholas Bellinger 
2406c66ac9dbSNicholas Bellinger 	return alua_tg_pt_gp_cg;
2407c66ac9dbSNicholas Bellinger }
2408c66ac9dbSNicholas Bellinger 
2409c66ac9dbSNicholas Bellinger static void target_core_alua_drop_tg_pt_gp(
2410c66ac9dbSNicholas Bellinger 	struct config_group *group,
2411c66ac9dbSNicholas Bellinger 	struct config_item *item)
2412c66ac9dbSNicholas Bellinger {
2413c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2414c66ac9dbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2415c66ac9dbSNicholas Bellinger 
24166708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2417c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
2418c66ac9dbSNicholas Bellinger 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
24191f6fe7cbSNicholas Bellinger 	/*
24201f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
24211f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_tg_pt_gp_release().
24221f6fe7cbSNicholas Bellinger 	 */
2423c66ac9dbSNicholas Bellinger 	config_item_put(item);
2424c66ac9dbSNicholas Bellinger }
2425c66ac9dbSNicholas Bellinger 
2426c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2427c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_tg_pt_gp,
2428c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
2429c66ac9dbSNicholas Bellinger };
2430c66ac9dbSNicholas Bellinger 
243172aca57bSNicholas Bellinger TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
2432c66ac9dbSNicholas Bellinger 
243372aca57bSNicholas Bellinger /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2434c66ac9dbSNicholas Bellinger 
2435c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_cit */
2436c66ac9dbSNicholas Bellinger 
2437c66ac9dbSNicholas Bellinger /*
2438c66ac9dbSNicholas Bellinger  * target_core_alua_cit is a ConfigFS group that lives under
2439c66ac9dbSNicholas Bellinger  * /sys/kernel/config/target/core/alua.  There are default groups
2440c66ac9dbSNicholas Bellinger  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2441c66ac9dbSNicholas Bellinger  * target_core_alua_cit in target_core_init_configfs() below.
2442c66ac9dbSNicholas Bellinger  */
2443c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_cit = {
2444c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2445c66ac9dbSNicholas Bellinger 	.ct_attrs		= NULL,
2446c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2447c66ac9dbSNicholas Bellinger };
2448c66ac9dbSNicholas Bellinger 
2449c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_cit */
2450c66ac9dbSNicholas Bellinger 
2451d23ab570SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_stat_cit */
245212d23384SNicholas Bellinger 
245312d23384SNicholas Bellinger static struct config_group *target_core_stat_mkdir(
245412d23384SNicholas Bellinger 	struct config_group *group,
245512d23384SNicholas Bellinger 	const char *name)
245612d23384SNicholas Bellinger {
245712d23384SNicholas Bellinger 	return ERR_PTR(-ENOSYS);
245812d23384SNicholas Bellinger }
245912d23384SNicholas Bellinger 
246012d23384SNicholas Bellinger static void target_core_stat_rmdir(
246112d23384SNicholas Bellinger 	struct config_group *group,
246212d23384SNicholas Bellinger 	struct config_item *item)
246312d23384SNicholas Bellinger {
246412d23384SNicholas Bellinger 	return;
246512d23384SNicholas Bellinger }
246612d23384SNicholas Bellinger 
246712d23384SNicholas Bellinger static struct configfs_group_operations target_core_stat_group_ops = {
246812d23384SNicholas Bellinger 	.make_group		= &target_core_stat_mkdir,
246912d23384SNicholas Bellinger 	.drop_item		= &target_core_stat_rmdir,
247012d23384SNicholas Bellinger };
247112d23384SNicholas Bellinger 
2472d23ab570SNicholas Bellinger TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
247312d23384SNicholas Bellinger 
2474d23ab570SNicholas Bellinger /* End functions for struct config_item_type tb_dev_stat_cit */
247512d23384SNicholas Bellinger 
2476c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_hba_cit */
2477c66ac9dbSNicholas Bellinger 
2478c66ac9dbSNicholas Bellinger static struct config_group *target_core_make_subdev(
2479c66ac9dbSNicholas Bellinger 	struct config_group *group,
2480c66ac9dbSNicholas Bellinger 	const char *name)
2481c66ac9dbSNicholas Bellinger {
2482c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2483c66ac9dbSNicholas Bellinger 	struct se_subsystem_api *t;
2484c66ac9dbSNicholas Bellinger 	struct config_item *hba_ci = &group->cg_item;
2485c66ac9dbSNicholas Bellinger 	struct se_hba *hba = item_to_hba(hba_ci);
24860fd97ccfSChristoph Hellwig 	struct se_device *dev;
2487c66ac9dbSNicholas Bellinger 	struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
248812d23384SNicholas Bellinger 	struct config_group *dev_stat_grp = NULL;
248912d23384SNicholas Bellinger 	int errno = -ENOMEM, ret;
2490c66ac9dbSNicholas Bellinger 
249112d23384SNicholas Bellinger 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
249212d23384SNicholas Bellinger 	if (ret)
249312d23384SNicholas Bellinger 		return ERR_PTR(ret);
2494c66ac9dbSNicholas Bellinger 	/*
2495c66ac9dbSNicholas Bellinger 	 * Locate the struct se_subsystem_api from parent's struct se_hba.
2496c66ac9dbSNicholas Bellinger 	 */
2497c66ac9dbSNicholas Bellinger 	t = hba->transport;
2498c66ac9dbSNicholas Bellinger 
24990fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, name);
25000fd97ccfSChristoph Hellwig 	if (!dev)
25010fd97ccfSChristoph Hellwig 		goto out_unlock;
2502c66ac9dbSNicholas Bellinger 
25030fd97ccfSChristoph Hellwig 	dev_cg = &dev->dev_group;
2504c66ac9dbSNicholas Bellinger 
250513f6a914SSebastian Andrzej Siewior 	dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
2506c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
25076708bb27SAndy Grover 	if (!dev_cg->default_groups)
25080fd97ccfSChristoph Hellwig 		goto out_free_device;
2509c66ac9dbSNicholas Bellinger 
251073112edcSNicholas Bellinger 	config_group_init_type_name(dev_cg, name, &t->tb_cits.tb_dev_cit);
25110fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
2512f79a897eSNicholas Bellinger 			&t->tb_cits.tb_dev_attrib_cit);
25130fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_pr_group, "pr",
251491e2e39bSNicholas Bellinger 			&t->tb_cits.tb_dev_pr_cit);
25150fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
2516f8d389c6SNicholas Bellinger 			&t->tb_cits.tb_dev_wwn_cit);
25170fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
251872aca57bSNicholas Bellinger 			"alua", &t->tb_cits.tb_dev_alua_tg_pt_gps_cit);
25190fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
2520d23ab570SNicholas Bellinger 			"statistics", &t->tb_cits.tb_dev_stat_cit);
252112d23384SNicholas Bellinger 
25220fd97ccfSChristoph Hellwig 	dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
25230fd97ccfSChristoph Hellwig 	dev_cg->default_groups[1] = &dev->dev_pr_group;
25240fd97ccfSChristoph Hellwig 	dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
25250fd97ccfSChristoph Hellwig 	dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
25260fd97ccfSChristoph Hellwig 	dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
252712d23384SNicholas Bellinger 	dev_cg->default_groups[5] = NULL;
2528c66ac9dbSNicholas Bellinger 	/*
252912d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2530c66ac9dbSNicholas Bellinger 	 */
25310fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
25326708bb27SAndy Grover 	if (!tg_pt_gp)
25330fd97ccfSChristoph Hellwig 		goto out_free_dev_cg_default_groups;
25340fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2535c66ac9dbSNicholas Bellinger 
25360fd97ccfSChristoph Hellwig 	tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
253713f6a914SSebastian Andrzej Siewior 	tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2538c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
25396708bb27SAndy Grover 	if (!tg_pt_gp_cg->default_groups) {
25406708bb27SAndy Grover 		pr_err("Unable to allocate tg_pt_gp_cg->"
2541c66ac9dbSNicholas Bellinger 				"default_groups\n");
25420fd97ccfSChristoph Hellwig 		goto out_free_tg_pt_gp;
2543c66ac9dbSNicholas Bellinger 	}
2544c66ac9dbSNicholas Bellinger 
2545c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2546c66ac9dbSNicholas Bellinger 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2547c66ac9dbSNicholas Bellinger 	tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2548c66ac9dbSNicholas Bellinger 	tg_pt_gp_cg->default_groups[1] = NULL;
254912d23384SNicholas Bellinger 	/*
255012d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/statistics/ default groups
255112d23384SNicholas Bellinger 	 */
25520fd97ccfSChristoph Hellwig 	dev_stat_grp = &dev->dev_stat_grps.stat_group;
255313f6a914SSebastian Andrzej Siewior 	dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
255412d23384SNicholas Bellinger 				GFP_KERNEL);
255512d23384SNicholas Bellinger 	if (!dev_stat_grp->default_groups) {
25566708bb27SAndy Grover 		pr_err("Unable to allocate dev_stat_grp->default_groups\n");
25570fd97ccfSChristoph Hellwig 		goto out_free_tg_pt_gp_cg_default_groups;
255812d23384SNicholas Bellinger 	}
25590fd97ccfSChristoph Hellwig 	target_stat_setup_dev_default_groups(dev);
2560c66ac9dbSNicholas Bellinger 
2561c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
25620fd97ccfSChristoph Hellwig 	return dev_cg;
25630fd97ccfSChristoph Hellwig 
25640fd97ccfSChristoph Hellwig out_free_tg_pt_gp_cg_default_groups:
2565c66ac9dbSNicholas Bellinger 	kfree(tg_pt_gp_cg->default_groups);
25660fd97ccfSChristoph Hellwig out_free_tg_pt_gp:
25670fd97ccfSChristoph Hellwig 	core_alua_free_tg_pt_gp(tg_pt_gp);
25680fd97ccfSChristoph Hellwig out_free_dev_cg_default_groups:
2569c66ac9dbSNicholas Bellinger 	kfree(dev_cg->default_groups);
25700fd97ccfSChristoph Hellwig out_free_device:
25710fd97ccfSChristoph Hellwig 	target_free_device(dev);
25720fd97ccfSChristoph Hellwig out_unlock:
2573c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
257412d23384SNicholas Bellinger 	return ERR_PTR(errno);
2575c66ac9dbSNicholas Bellinger }
2576c66ac9dbSNicholas Bellinger 
2577c66ac9dbSNicholas Bellinger static void target_core_drop_subdev(
2578c66ac9dbSNicholas Bellinger 	struct config_group *group,
2579c66ac9dbSNicholas Bellinger 	struct config_item *item)
2580c66ac9dbSNicholas Bellinger {
25810fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
25820fd97ccfSChristoph Hellwig 	struct se_device *dev =
25830fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2584c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2585c66ac9dbSNicholas Bellinger 	struct config_item *df_item;
25860fd97ccfSChristoph Hellwig 	struct config_group *tg_pt_gp_cg, *dev_stat_grp;
25871f6fe7cbSNicholas Bellinger 	int i;
2588c66ac9dbSNicholas Bellinger 
25890fd97ccfSChristoph Hellwig 	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
2590c66ac9dbSNicholas Bellinger 
25911f6fe7cbSNicholas Bellinger 	mutex_lock(&hba->hba_access_mutex);
2592c66ac9dbSNicholas Bellinger 
25930fd97ccfSChristoph Hellwig 	dev_stat_grp = &dev->dev_stat_grps.stat_group;
259412d23384SNicholas Bellinger 	for (i = 0; dev_stat_grp->default_groups[i]; i++) {
259512d23384SNicholas Bellinger 		df_item = &dev_stat_grp->default_groups[i]->cg_item;
259612d23384SNicholas Bellinger 		dev_stat_grp->default_groups[i] = NULL;
259712d23384SNicholas Bellinger 		config_item_put(df_item);
259812d23384SNicholas Bellinger 	}
259912d23384SNicholas Bellinger 	kfree(dev_stat_grp->default_groups);
260012d23384SNicholas Bellinger 
26010fd97ccfSChristoph Hellwig 	tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2602c66ac9dbSNicholas Bellinger 	for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2603c66ac9dbSNicholas Bellinger 		df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2604c66ac9dbSNicholas Bellinger 		tg_pt_gp_cg->default_groups[i] = NULL;
2605c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
2606c66ac9dbSNicholas Bellinger 	}
2607c66ac9dbSNicholas Bellinger 	kfree(tg_pt_gp_cg->default_groups);
26081f6fe7cbSNicholas Bellinger 	/*
26091f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
26101f6fe7cbSNicholas Bellinger 	 * directly from target_core_alua_tg_pt_gp_release().
26111f6fe7cbSNicholas Bellinger 	 */
26120fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = NULL;
2613c66ac9dbSNicholas Bellinger 
2614c66ac9dbSNicholas Bellinger 	for (i = 0; dev_cg->default_groups[i]; i++) {
2615c66ac9dbSNicholas Bellinger 		df_item = &dev_cg->default_groups[i]->cg_item;
2616c66ac9dbSNicholas Bellinger 		dev_cg->default_groups[i] = NULL;
2617c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
2618c66ac9dbSNicholas Bellinger 	}
26191f6fe7cbSNicholas Bellinger 	/*
26200fd97ccfSChristoph Hellwig 	 * se_dev is released from target_core_dev_item_ops->release()
26211f6fe7cbSNicholas Bellinger 	 */
2622c66ac9dbSNicholas Bellinger 	config_item_put(item);
2623c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
2624c66ac9dbSNicholas Bellinger }
2625c66ac9dbSNicholas Bellinger 
2626c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_hba_group_ops = {
2627c66ac9dbSNicholas Bellinger 	.make_group		= target_core_make_subdev,
2628c66ac9dbSNicholas Bellinger 	.drop_item		= target_core_drop_subdev,
2629c66ac9dbSNicholas Bellinger };
2630c66ac9dbSNicholas Bellinger 
2631c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2632c66ac9dbSNicholas Bellinger #define SE_HBA_ATTR(_name, _mode)				\
2633c66ac9dbSNicholas Bellinger static struct target_core_hba_attribute				\
2634c66ac9dbSNicholas Bellinger 		target_core_hba_##_name =			\
2635c66ac9dbSNicholas Bellinger 		__CONFIGFS_EATTR(_name, _mode,			\
2636c66ac9dbSNicholas Bellinger 		target_core_hba_show_attr_##_name,		\
2637c66ac9dbSNicholas Bellinger 		target_core_hba_store_attr_##_name);
2638c66ac9dbSNicholas Bellinger 
2639c66ac9dbSNicholas Bellinger #define SE_HBA_ATTR_RO(_name)					\
2640c66ac9dbSNicholas Bellinger static struct target_core_hba_attribute				\
2641c66ac9dbSNicholas Bellinger 		target_core_hba_##_name =			\
2642c66ac9dbSNicholas Bellinger 		__CONFIGFS_EATTR_RO(_name,			\
2643c66ac9dbSNicholas Bellinger 		target_core_hba_show_attr_##_name);
2644c66ac9dbSNicholas Bellinger 
2645c66ac9dbSNicholas Bellinger static ssize_t target_core_hba_show_attr_hba_info(
2646c66ac9dbSNicholas Bellinger 	struct se_hba *hba,
2647c66ac9dbSNicholas Bellinger 	char *page)
2648c66ac9dbSNicholas Bellinger {
2649c66ac9dbSNicholas Bellinger 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2650c66ac9dbSNicholas Bellinger 			hba->hba_id, hba->transport->name,
2651c66ac9dbSNicholas Bellinger 			TARGET_CORE_CONFIGFS_VERSION);
2652c66ac9dbSNicholas Bellinger }
2653c66ac9dbSNicholas Bellinger 
2654c66ac9dbSNicholas Bellinger SE_HBA_ATTR_RO(hba_info);
2655c66ac9dbSNicholas Bellinger 
2656c66ac9dbSNicholas Bellinger static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2657c66ac9dbSNicholas Bellinger 				char *page)
2658c66ac9dbSNicholas Bellinger {
2659c66ac9dbSNicholas Bellinger 	int hba_mode = 0;
2660c66ac9dbSNicholas Bellinger 
2661c66ac9dbSNicholas Bellinger 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2662c66ac9dbSNicholas Bellinger 		hba_mode = 1;
2663c66ac9dbSNicholas Bellinger 
2664c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", hba_mode);
2665c66ac9dbSNicholas Bellinger }
2666c66ac9dbSNicholas Bellinger 
2667c66ac9dbSNicholas Bellinger static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2668c66ac9dbSNicholas Bellinger 				const char *page, size_t count)
2669c66ac9dbSNicholas Bellinger {
2670c66ac9dbSNicholas Bellinger 	struct se_subsystem_api *transport = hba->transport;
2671c66ac9dbSNicholas Bellinger 	unsigned long mode_flag;
2672c66ac9dbSNicholas Bellinger 	int ret;
2673c66ac9dbSNicholas Bellinger 
2674c66ac9dbSNicholas Bellinger 	if (transport->pmode_enable_hba == NULL)
2675c66ac9dbSNicholas Bellinger 		return -EINVAL;
2676c66ac9dbSNicholas Bellinger 
267757103d7fSJingoo Han 	ret = kstrtoul(page, 0, &mode_flag);
2678c66ac9dbSNicholas Bellinger 	if (ret < 0) {
26796708bb27SAndy Grover 		pr_err("Unable to extract hba mode flag: %d\n", ret);
268057103d7fSJingoo Han 		return ret;
2681c66ac9dbSNicholas Bellinger 	}
2682c66ac9dbSNicholas Bellinger 
26830fd97ccfSChristoph Hellwig 	if (hba->dev_count) {
26846708bb27SAndy Grover 		pr_err("Unable to set hba_mode with active devices\n");
2685c66ac9dbSNicholas Bellinger 		return -EINVAL;
2686c66ac9dbSNicholas Bellinger 	}
2687c66ac9dbSNicholas Bellinger 
2688c66ac9dbSNicholas Bellinger 	ret = transport->pmode_enable_hba(hba, mode_flag);
2689c66ac9dbSNicholas Bellinger 	if (ret < 0)
2690c66ac9dbSNicholas Bellinger 		return -EINVAL;
2691c66ac9dbSNicholas Bellinger 	if (ret > 0)
2692c66ac9dbSNicholas Bellinger 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2693c66ac9dbSNicholas Bellinger 	else if (ret == 0)
2694c66ac9dbSNicholas Bellinger 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2695c66ac9dbSNicholas Bellinger 
2696c66ac9dbSNicholas Bellinger 	return count;
2697c66ac9dbSNicholas Bellinger }
2698c66ac9dbSNicholas Bellinger 
2699c66ac9dbSNicholas Bellinger SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2700c66ac9dbSNicholas Bellinger 
2701c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2702c66ac9dbSNicholas Bellinger 
27031f6fe7cbSNicholas Bellinger static void target_core_hba_release(struct config_item *item)
27041f6fe7cbSNicholas Bellinger {
27051f6fe7cbSNicholas Bellinger 	struct se_hba *hba = container_of(to_config_group(item),
27061f6fe7cbSNicholas Bellinger 				struct se_hba, hba_group);
27071f6fe7cbSNicholas Bellinger 	core_delete_hba(hba);
27081f6fe7cbSNicholas Bellinger }
27091f6fe7cbSNicholas Bellinger 
2710c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_hba_attrs[] = {
2711c66ac9dbSNicholas Bellinger 	&target_core_hba_hba_info.attr,
2712c66ac9dbSNicholas Bellinger 	&target_core_hba_hba_mode.attr,
2713c66ac9dbSNicholas Bellinger 	NULL,
2714c66ac9dbSNicholas Bellinger };
2715c66ac9dbSNicholas Bellinger 
2716c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_hba_item_ops = {
27171f6fe7cbSNicholas Bellinger 	.release		= target_core_hba_release,
2718c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_hba_attr_show,
2719c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_hba_attr_store,
2720c66ac9dbSNicholas Bellinger };
2721c66ac9dbSNicholas Bellinger 
2722c66ac9dbSNicholas Bellinger static struct config_item_type target_core_hba_cit = {
2723c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_hba_item_ops,
2724c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_hba_group_ops,
2725c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_hba_attrs,
2726c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2727c66ac9dbSNicholas Bellinger };
2728c66ac9dbSNicholas Bellinger 
2729c66ac9dbSNicholas Bellinger static struct config_group *target_core_call_addhbatotarget(
2730c66ac9dbSNicholas Bellinger 	struct config_group *group,
2731c66ac9dbSNicholas Bellinger 	const char *name)
2732c66ac9dbSNicholas Bellinger {
2733c66ac9dbSNicholas Bellinger 	char *se_plugin_str, *str, *str2;
2734c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2735c66ac9dbSNicholas Bellinger 	char buf[TARGET_CORE_NAME_MAX_LEN];
2736c66ac9dbSNicholas Bellinger 	unsigned long plugin_dep_id = 0;
2737c66ac9dbSNicholas Bellinger 	int ret;
2738c66ac9dbSNicholas Bellinger 
2739c66ac9dbSNicholas Bellinger 	memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
274060d645a4SDan Carpenter 	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
27416708bb27SAndy Grover 		pr_err("Passed *name strlen(): %d exceeds"
2742c66ac9dbSNicholas Bellinger 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
2743c66ac9dbSNicholas Bellinger 			TARGET_CORE_NAME_MAX_LEN);
2744c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENAMETOOLONG);
2745c66ac9dbSNicholas Bellinger 	}
2746c66ac9dbSNicholas Bellinger 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
2747c66ac9dbSNicholas Bellinger 
2748c66ac9dbSNicholas Bellinger 	str = strstr(buf, "_");
27496708bb27SAndy Grover 	if (!str) {
27506708bb27SAndy Grover 		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
2751c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
2752c66ac9dbSNicholas Bellinger 	}
2753c66ac9dbSNicholas Bellinger 	se_plugin_str = buf;
2754c66ac9dbSNicholas Bellinger 	/*
2755c66ac9dbSNicholas Bellinger 	 * Special case for subsystem plugins that have "_" in their names.
2756c66ac9dbSNicholas Bellinger 	 * Namely rd_direct and rd_mcp..
2757c66ac9dbSNicholas Bellinger 	 */
2758c66ac9dbSNicholas Bellinger 	str2 = strstr(str+1, "_");
27596708bb27SAndy Grover 	if (str2) {
2760c66ac9dbSNicholas Bellinger 		*str2 = '\0'; /* Terminate for *se_plugin_str */
2761c66ac9dbSNicholas Bellinger 		str2++; /* Skip to start of plugin dependent ID */
2762c66ac9dbSNicholas Bellinger 		str = str2;
2763c66ac9dbSNicholas Bellinger 	} else {
2764c66ac9dbSNicholas Bellinger 		*str = '\0'; /* Terminate for *se_plugin_str */
2765c66ac9dbSNicholas Bellinger 		str++; /* Skip to start of plugin dependent ID */
2766c66ac9dbSNicholas Bellinger 	}
2767c66ac9dbSNicholas Bellinger 
276857103d7fSJingoo Han 	ret = kstrtoul(str, 0, &plugin_dep_id);
2769c66ac9dbSNicholas Bellinger 	if (ret < 0) {
277057103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2771c66ac9dbSNicholas Bellinger 				" plugin_dep_id\n", ret);
277257103d7fSJingoo Han 		return ERR_PTR(ret);
2773c66ac9dbSNicholas Bellinger 	}
2774c66ac9dbSNicholas Bellinger 	/*
2775c66ac9dbSNicholas Bellinger 	 * Load up TCM subsystem plugins if they have not already been loaded.
2776c66ac9dbSNicholas Bellinger 	 */
2777dbc5623eSNicholas Bellinger 	transport_subsystem_check_init();
2778c66ac9dbSNicholas Bellinger 
2779c66ac9dbSNicholas Bellinger 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
2780c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
2781c66ac9dbSNicholas Bellinger 		return ERR_CAST(hba);
2782c66ac9dbSNicholas Bellinger 
2783c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&hba->hba_group, name,
2784c66ac9dbSNicholas Bellinger 			&target_core_hba_cit);
2785c66ac9dbSNicholas Bellinger 
2786c66ac9dbSNicholas Bellinger 	return &hba->hba_group;
2787c66ac9dbSNicholas Bellinger }
2788c66ac9dbSNicholas Bellinger 
2789c66ac9dbSNicholas Bellinger static void target_core_call_delhbafromtarget(
2790c66ac9dbSNicholas Bellinger 	struct config_group *group,
2791c66ac9dbSNicholas Bellinger 	struct config_item *item)
2792c66ac9dbSNicholas Bellinger {
27931f6fe7cbSNicholas Bellinger 	/*
27941f6fe7cbSNicholas Bellinger 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
27951f6fe7cbSNicholas Bellinger 	 * -> target_core_hba_release()
27961f6fe7cbSNicholas Bellinger 	 */
2797c66ac9dbSNicholas Bellinger 	config_item_put(item);
2798c66ac9dbSNicholas Bellinger }
2799c66ac9dbSNicholas Bellinger 
2800c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_group_ops = {
2801c66ac9dbSNicholas Bellinger 	.make_group	= target_core_call_addhbatotarget,
2802c66ac9dbSNicholas Bellinger 	.drop_item	= target_core_call_delhbafromtarget,
2803c66ac9dbSNicholas Bellinger };
2804c66ac9dbSNicholas Bellinger 
2805c66ac9dbSNicholas Bellinger static struct config_item_type target_core_cit = {
2806c66ac9dbSNicholas Bellinger 	.ct_item_ops	= NULL,
2807c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_group_ops,
2808c66ac9dbSNicholas Bellinger 	.ct_attrs	= NULL,
2809c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
2810c66ac9dbSNicholas Bellinger };
2811c66ac9dbSNicholas Bellinger 
2812c66ac9dbSNicholas Bellinger /* Stop functions for struct config_item_type target_core_hba_cit */
2813c66ac9dbSNicholas Bellinger 
281473112edcSNicholas Bellinger void target_core_setup_sub_cits(struct se_subsystem_api *sa)
281573112edcSNicholas Bellinger {
281673112edcSNicholas Bellinger 	target_core_setup_dev_cit(sa);
2817f79a897eSNicholas Bellinger 	target_core_setup_dev_attrib_cit(sa);
281891e2e39bSNicholas Bellinger 	target_core_setup_dev_pr_cit(sa);
2819f8d389c6SNicholas Bellinger 	target_core_setup_dev_wwn_cit(sa);
282072aca57bSNicholas Bellinger 	target_core_setup_dev_alua_tg_pt_gps_cit(sa);
2821d23ab570SNicholas Bellinger 	target_core_setup_dev_stat_cit(sa);
282273112edcSNicholas Bellinger }
282373112edcSNicholas Bellinger EXPORT_SYMBOL(target_core_setup_sub_cits);
282473112edcSNicholas Bellinger 
282554550fabSAxel Lin static int __init target_core_init_configfs(void)
2826c66ac9dbSNicholas Bellinger {
2827c66ac9dbSNicholas Bellinger 	struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
2828c66ac9dbSNicholas Bellinger 	struct config_group *lu_gp_cg = NULL;
2829d588cf8fSChristoph Hellwig 	struct configfs_subsystem *subsys = &target_core_fabrics;
2830c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2831c66ac9dbSNicholas Bellinger 	int ret;
2832c66ac9dbSNicholas Bellinger 
28336708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
2834c66ac9dbSNicholas Bellinger 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
2835c66ac9dbSNicholas Bellinger 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
2836c66ac9dbSNicholas Bellinger 
2837c66ac9dbSNicholas Bellinger 	config_group_init(&subsys->su_group);
2838c66ac9dbSNicholas Bellinger 	mutex_init(&subsys->su_mutex);
2839c66ac9dbSNicholas Bellinger 
2840e3d6f909SAndy Grover 	ret = init_se_kmem_caches();
2841c66ac9dbSNicholas Bellinger 	if (ret < 0)
2842e3d6f909SAndy Grover 		return ret;
2843c66ac9dbSNicholas Bellinger 	/*
2844c66ac9dbSNicholas Bellinger 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
2845c66ac9dbSNicholas Bellinger 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
2846c66ac9dbSNicholas Bellinger 	 */
2847c66ac9dbSNicholas Bellinger 	target_cg = &subsys->su_group;
2848ab6dae82SAndy Grover 	target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2849c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
28506708bb27SAndy Grover 	if (!target_cg->default_groups) {
28516708bb27SAndy Grover 		pr_err("Unable to allocate target_cg->default_groups\n");
285237bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
2853c66ac9dbSNicholas Bellinger 		goto out_global;
2854c66ac9dbSNicholas Bellinger 	}
2855c66ac9dbSNicholas Bellinger 
2856e3d6f909SAndy Grover 	config_group_init_type_name(&target_core_hbagroup,
2857c66ac9dbSNicholas Bellinger 			"core", &target_core_cit);
2858e3d6f909SAndy Grover 	target_cg->default_groups[0] = &target_core_hbagroup;
2859c66ac9dbSNicholas Bellinger 	target_cg->default_groups[1] = NULL;
2860c66ac9dbSNicholas Bellinger 	/*
2861c66ac9dbSNicholas Bellinger 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
2862c66ac9dbSNicholas Bellinger 	 */
2863e3d6f909SAndy Grover 	hba_cg = &target_core_hbagroup;
286413f6a914SSebastian Andrzej Siewior 	hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2865c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
28666708bb27SAndy Grover 	if (!hba_cg->default_groups) {
28676708bb27SAndy Grover 		pr_err("Unable to allocate hba_cg->default_groups\n");
286837bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
2869c66ac9dbSNicholas Bellinger 		goto out_global;
2870c66ac9dbSNicholas Bellinger 	}
2871e3d6f909SAndy Grover 	config_group_init_type_name(&alua_group,
2872c66ac9dbSNicholas Bellinger 			"alua", &target_core_alua_cit);
2873e3d6f909SAndy Grover 	hba_cg->default_groups[0] = &alua_group;
2874c66ac9dbSNicholas Bellinger 	hba_cg->default_groups[1] = NULL;
2875c66ac9dbSNicholas Bellinger 	/*
2876c66ac9dbSNicholas Bellinger 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
2877c66ac9dbSNicholas Bellinger 	 * groups under /sys/kernel/config/target/core/alua/
2878c66ac9dbSNicholas Bellinger 	 */
2879e3d6f909SAndy Grover 	alua_cg = &alua_group;
288013f6a914SSebastian Andrzej Siewior 	alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2881c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
28826708bb27SAndy Grover 	if (!alua_cg->default_groups) {
28836708bb27SAndy Grover 		pr_err("Unable to allocate alua_cg->default_groups\n");
288437bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
2885c66ac9dbSNicholas Bellinger 		goto out_global;
2886c66ac9dbSNicholas Bellinger 	}
2887c66ac9dbSNicholas Bellinger 
2888e3d6f909SAndy Grover 	config_group_init_type_name(&alua_lu_gps_group,
2889c66ac9dbSNicholas Bellinger 			"lu_gps", &target_core_alua_lu_gps_cit);
2890e3d6f909SAndy Grover 	alua_cg->default_groups[0] = &alua_lu_gps_group;
2891c66ac9dbSNicholas Bellinger 	alua_cg->default_groups[1] = NULL;
2892c66ac9dbSNicholas Bellinger 	/*
2893c66ac9dbSNicholas Bellinger 	 * Add core/alua/lu_gps/default_lu_gp
2894c66ac9dbSNicholas Bellinger 	 */
2895c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
289637bb7899SPeter Senna Tschudin 	if (IS_ERR(lu_gp)) {
289737bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
2898c66ac9dbSNicholas Bellinger 		goto out_global;
289937bb7899SPeter Senna Tschudin 	}
2900c66ac9dbSNicholas Bellinger 
2901e3d6f909SAndy Grover 	lu_gp_cg = &alua_lu_gps_group;
290213f6a914SSebastian Andrzej Siewior 	lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2903c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
29046708bb27SAndy Grover 	if (!lu_gp_cg->default_groups) {
29056708bb27SAndy Grover 		pr_err("Unable to allocate lu_gp_cg->default_groups\n");
290637bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
2907c66ac9dbSNicholas Bellinger 		goto out_global;
2908c66ac9dbSNicholas Bellinger 	}
2909c66ac9dbSNicholas Bellinger 
2910c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
2911c66ac9dbSNicholas Bellinger 				&target_core_alua_lu_gp_cit);
2912c66ac9dbSNicholas Bellinger 	lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
2913c66ac9dbSNicholas Bellinger 	lu_gp_cg->default_groups[1] = NULL;
2914e3d6f909SAndy Grover 	default_lu_gp = lu_gp;
2915c66ac9dbSNicholas Bellinger 	/*
2916c66ac9dbSNicholas Bellinger 	 * Register the target_core_mod subsystem with configfs.
2917c66ac9dbSNicholas Bellinger 	 */
2918c66ac9dbSNicholas Bellinger 	ret = configfs_register_subsystem(subsys);
2919c66ac9dbSNicholas Bellinger 	if (ret < 0) {
29206708bb27SAndy Grover 		pr_err("Error %d while registering subsystem %s\n",
2921c66ac9dbSNicholas Bellinger 			ret, subsys->su_group.cg_item.ci_namebuf);
2922c66ac9dbSNicholas Bellinger 		goto out_global;
2923c66ac9dbSNicholas Bellinger 	}
29246708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
2925c66ac9dbSNicholas Bellinger 		" Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
2926c66ac9dbSNicholas Bellinger 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
2927c66ac9dbSNicholas Bellinger 	/*
2928c66ac9dbSNicholas Bellinger 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
2929c66ac9dbSNicholas Bellinger 	 */
2930c66ac9dbSNicholas Bellinger 	ret = rd_module_init();
2931c66ac9dbSNicholas Bellinger 	if (ret < 0)
2932c66ac9dbSNicholas Bellinger 		goto out;
2933c66ac9dbSNicholas Bellinger 
29340d0f9dfbSRoland Dreier 	ret = core_dev_setup_virtual_lun0();
29350d0f9dfbSRoland Dreier 	if (ret < 0)
2936c66ac9dbSNicholas Bellinger 		goto out;
2937c66ac9dbSNicholas Bellinger 
2938f99715acSNicholas Bellinger 	ret = target_xcopy_setup_pt();
2939f99715acSNicholas Bellinger 	if (ret < 0)
2940f99715acSNicholas Bellinger 		goto out;
2941f99715acSNicholas Bellinger 
2942c66ac9dbSNicholas Bellinger 	return 0;
2943c66ac9dbSNicholas Bellinger 
2944c66ac9dbSNicholas Bellinger out:
2945c66ac9dbSNicholas Bellinger 	configfs_unregister_subsystem(subsys);
2946c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
2947c66ac9dbSNicholas Bellinger 	rd_module_exit();
2948c66ac9dbSNicholas Bellinger out_global:
2949e3d6f909SAndy Grover 	if (default_lu_gp) {
2950e3d6f909SAndy Grover 		core_alua_free_lu_gp(default_lu_gp);
2951e3d6f909SAndy Grover 		default_lu_gp = NULL;
2952c66ac9dbSNicholas Bellinger 	}
2953c66ac9dbSNicholas Bellinger 	if (lu_gp_cg)
2954c66ac9dbSNicholas Bellinger 		kfree(lu_gp_cg->default_groups);
2955c66ac9dbSNicholas Bellinger 	if (alua_cg)
2956c66ac9dbSNicholas Bellinger 		kfree(alua_cg->default_groups);
2957c66ac9dbSNicholas Bellinger 	if (hba_cg)
2958c66ac9dbSNicholas Bellinger 		kfree(hba_cg->default_groups);
2959c66ac9dbSNicholas Bellinger 	kfree(target_cg->default_groups);
2960e3d6f909SAndy Grover 	release_se_kmem_caches();
2961e3d6f909SAndy Grover 	return ret;
2962c66ac9dbSNicholas Bellinger }
2963c66ac9dbSNicholas Bellinger 
296454550fabSAxel Lin static void __exit target_core_exit_configfs(void)
2965c66ac9dbSNicholas Bellinger {
2966c66ac9dbSNicholas Bellinger 	struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
2967c66ac9dbSNicholas Bellinger 	struct config_item *item;
2968c66ac9dbSNicholas Bellinger 	int i;
2969c66ac9dbSNicholas Bellinger 
2970e3d6f909SAndy Grover 	lu_gp_cg = &alua_lu_gps_group;
2971c66ac9dbSNicholas Bellinger 	for (i = 0; lu_gp_cg->default_groups[i]; i++) {
2972c66ac9dbSNicholas Bellinger 		item = &lu_gp_cg->default_groups[i]->cg_item;
2973c66ac9dbSNicholas Bellinger 		lu_gp_cg->default_groups[i] = NULL;
2974c66ac9dbSNicholas Bellinger 		config_item_put(item);
2975c66ac9dbSNicholas Bellinger 	}
2976c66ac9dbSNicholas Bellinger 	kfree(lu_gp_cg->default_groups);
29777c2bf6e9SNicholas Bellinger 	lu_gp_cg->default_groups = NULL;
2978c66ac9dbSNicholas Bellinger 
2979e3d6f909SAndy Grover 	alua_cg = &alua_group;
2980c66ac9dbSNicholas Bellinger 	for (i = 0; alua_cg->default_groups[i]; i++) {
2981c66ac9dbSNicholas Bellinger 		item = &alua_cg->default_groups[i]->cg_item;
2982c66ac9dbSNicholas Bellinger 		alua_cg->default_groups[i] = NULL;
2983c66ac9dbSNicholas Bellinger 		config_item_put(item);
2984c66ac9dbSNicholas Bellinger 	}
2985c66ac9dbSNicholas Bellinger 	kfree(alua_cg->default_groups);
29867c2bf6e9SNicholas Bellinger 	alua_cg->default_groups = NULL;
2987c66ac9dbSNicholas Bellinger 
2988e3d6f909SAndy Grover 	hba_cg = &target_core_hbagroup;
2989c66ac9dbSNicholas Bellinger 	for (i = 0; hba_cg->default_groups[i]; i++) {
2990c66ac9dbSNicholas Bellinger 		item = &hba_cg->default_groups[i]->cg_item;
2991c66ac9dbSNicholas Bellinger 		hba_cg->default_groups[i] = NULL;
2992c66ac9dbSNicholas Bellinger 		config_item_put(item);
2993c66ac9dbSNicholas Bellinger 	}
2994c66ac9dbSNicholas Bellinger 	kfree(hba_cg->default_groups);
29957c2bf6e9SNicholas Bellinger 	hba_cg->default_groups = NULL;
29967c2bf6e9SNicholas Bellinger 	/*
29977c2bf6e9SNicholas Bellinger 	 * We expect subsys->su_group.default_groups to be released
29987c2bf6e9SNicholas Bellinger 	 * by configfs subsystem provider logic..
29997c2bf6e9SNicholas Bellinger 	 */
3000d588cf8fSChristoph Hellwig 	configfs_unregister_subsystem(&target_core_fabrics);
3001d588cf8fSChristoph Hellwig 	kfree(target_core_fabrics.su_group.default_groups);
3002c66ac9dbSNicholas Bellinger 
3003e3d6f909SAndy Grover 	core_alua_free_lu_gp(default_lu_gp);
3004e3d6f909SAndy Grover 	default_lu_gp = NULL;
30057c2bf6e9SNicholas Bellinger 
30066708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3007c66ac9dbSNicholas Bellinger 			" Infrastructure\n");
3008c66ac9dbSNicholas Bellinger 
3009c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3010c66ac9dbSNicholas Bellinger 	rd_module_exit();
3011f99715acSNicholas Bellinger 	target_xcopy_release_pt();
3012e3d6f909SAndy Grover 	release_se_kmem_caches();
3013c66ac9dbSNicholas Bellinger }
3014c66ac9dbSNicholas Bellinger 
3015c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3016c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3017c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
3018c66ac9dbSNicholas Bellinger 
3019c66ac9dbSNicholas Bellinger module_init(target_core_init_configfs);
3020c66ac9dbSNicholas Bellinger module_exit(target_core_exit_configfs);
3021