1c66ac9dbSNicholas Bellinger /*******************************************************************************
2c66ac9dbSNicholas Bellinger  * Filename:  target_core_configfs.c
3c66ac9dbSNicholas Bellinger  *
4c66ac9dbSNicholas Bellinger  * This file contains ConfigFS logic for the Generic Target Engine project.
5c66ac9dbSNicholas Bellinger  *
64c76251eSNicholas Bellinger  * (c) Copyright 2008-2013 Datera, Inc.
7c66ac9dbSNicholas Bellinger  *
8c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
9c66ac9dbSNicholas Bellinger  *
10c66ac9dbSNicholas Bellinger  * based on configfs Copyright (C) 2005 Oracle.  All rights reserved.
11c66ac9dbSNicholas Bellinger  *
12c66ac9dbSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
13c66ac9dbSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
14c66ac9dbSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
15c66ac9dbSNicholas Bellinger  * (at your option) any later version.
16c66ac9dbSNicholas Bellinger  *
17c66ac9dbSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
18c66ac9dbSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19c66ac9dbSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c66ac9dbSNicholas Bellinger  * GNU General Public License for more details.
21c66ac9dbSNicholas Bellinger  ****************************************************************************/
22c66ac9dbSNicholas Bellinger 
23c66ac9dbSNicholas Bellinger #include <linux/module.h>
24c66ac9dbSNicholas Bellinger #include <linux/moduleparam.h>
25c66ac9dbSNicholas Bellinger #include <generated/utsrelease.h>
26c66ac9dbSNicholas Bellinger #include <linux/utsname.h>
27c66ac9dbSNicholas Bellinger #include <linux/init.h>
28c66ac9dbSNicholas Bellinger #include <linux/fs.h>
29c66ac9dbSNicholas Bellinger #include <linux/namei.h>
30c66ac9dbSNicholas Bellinger #include <linux/slab.h>
31c66ac9dbSNicholas Bellinger #include <linux/types.h>
32c66ac9dbSNicholas Bellinger #include <linux/delay.h>
33c66ac9dbSNicholas Bellinger #include <linux/unistd.h>
34c66ac9dbSNicholas Bellinger #include <linux/string.h>
35c66ac9dbSNicholas Bellinger #include <linux/parser.h>
36c66ac9dbSNicholas Bellinger #include <linux/syscalls.h>
37c66ac9dbSNicholas Bellinger #include <linux/configfs.h>
38e3d6f909SAndy Grover #include <linux/spinlock.h>
39c66ac9dbSNicholas Bellinger 
40c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
41c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
42c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
43c66ac9dbSNicholas Bellinger #include <target/target_core_fabric_configfs.h>
44c66ac9dbSNicholas Bellinger #include <target/target_core_configfs.h>
45c66ac9dbSNicholas Bellinger #include <target/configfs_macros.h>
46c66ac9dbSNicholas Bellinger 
47e26d99aeSChristoph Hellwig #include "target_core_internal.h"
48c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
49c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
50c66ac9dbSNicholas Bellinger #include "target_core_rd.h"
51f99715acSNicholas Bellinger #include "target_core_xcopy.h"
52c66ac9dbSNicholas Bellinger 
5373112edcSNicholas Bellinger #define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs)		\
5473112edcSNicholas Bellinger static void target_core_setup_##_name##_cit(struct se_subsystem_api *sa) \
5573112edcSNicholas Bellinger {									\
5673112edcSNicholas Bellinger 	struct target_backend_cits *tbc = &sa->tb_cits;			\
5773112edcSNicholas Bellinger 	struct config_item_type *cit = &tbc->tb_##_name##_cit;		\
5873112edcSNicholas Bellinger 									\
5973112edcSNicholas Bellinger 	cit->ct_item_ops = _item_ops;					\
6073112edcSNicholas Bellinger 	cit->ct_group_ops = _group_ops;					\
6173112edcSNicholas Bellinger 	cit->ct_attrs = _attrs;						\
6273112edcSNicholas Bellinger 	cit->ct_owner = sa->owner;					\
6373112edcSNicholas Bellinger 	pr_debug("Setup generic %s\n", __stringify(_name));		\
6473112edcSNicholas Bellinger }
6573112edcSNicholas Bellinger 
66e3d6f909SAndy Grover extern struct t10_alua_lu_gp *default_lu_gp;
67e3d6f909SAndy Grover 
68d0f474e5SRoland Dreier static LIST_HEAD(g_tf_list);
69d0f474e5SRoland Dreier static DEFINE_MUTEX(g_tf_lock);
70c66ac9dbSNicholas Bellinger 
71c66ac9dbSNicholas Bellinger struct target_core_configfs_attribute {
72c66ac9dbSNicholas Bellinger 	struct configfs_attribute attr;
73c66ac9dbSNicholas Bellinger 	ssize_t (*show)(void *, char *);
74c66ac9dbSNicholas Bellinger 	ssize_t (*store)(void *, const char *, size_t);
75c66ac9dbSNicholas Bellinger };
76c66ac9dbSNicholas Bellinger 
77e3d6f909SAndy Grover static struct config_group target_core_hbagroup;
78e3d6f909SAndy Grover static struct config_group alua_group;
79e3d6f909SAndy Grover static struct config_group alua_lu_gps_group;
80e3d6f909SAndy Grover 
81c66ac9dbSNicholas Bellinger static inline struct se_hba *
82c66ac9dbSNicholas Bellinger item_to_hba(struct config_item *item)
83c66ac9dbSNicholas Bellinger {
84c66ac9dbSNicholas Bellinger 	return container_of(to_config_group(item), struct se_hba, hba_group);
85c66ac9dbSNicholas Bellinger }
86c66ac9dbSNicholas Bellinger 
87c66ac9dbSNicholas Bellinger /*
88c66ac9dbSNicholas Bellinger  * Attributes for /sys/kernel/config/target/
89c66ac9dbSNicholas Bellinger  */
90c66ac9dbSNicholas Bellinger static ssize_t target_core_attr_show(struct config_item *item,
91c66ac9dbSNicholas Bellinger 				      struct configfs_attribute *attr,
92c66ac9dbSNicholas Bellinger 				      char *page)
93c66ac9dbSNicholas Bellinger {
94c66ac9dbSNicholas Bellinger 	return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
95c66ac9dbSNicholas Bellinger 		" on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
96c66ac9dbSNicholas Bellinger 		utsname()->sysname, utsname()->machine);
97c66ac9dbSNicholas Bellinger }
98c66ac9dbSNicholas Bellinger 
99c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_fabric_item_ops = {
100c66ac9dbSNicholas Bellinger 	.show_attribute = target_core_attr_show,
101c66ac9dbSNicholas Bellinger };
102c66ac9dbSNicholas Bellinger 
103c66ac9dbSNicholas Bellinger static struct configfs_attribute target_core_item_attr_version = {
104c66ac9dbSNicholas Bellinger 	.ca_owner	= THIS_MODULE,
105c66ac9dbSNicholas Bellinger 	.ca_name	= "version",
106c66ac9dbSNicholas Bellinger 	.ca_mode	= S_IRUGO,
107c66ac9dbSNicholas Bellinger };
108c66ac9dbSNicholas Bellinger 
109c66ac9dbSNicholas Bellinger static struct target_fabric_configfs *target_core_get_fabric(
110c66ac9dbSNicholas Bellinger 	const char *name)
111c66ac9dbSNicholas Bellinger {
112c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
113c66ac9dbSNicholas Bellinger 
1146708bb27SAndy Grover 	if (!name)
115c66ac9dbSNicholas Bellinger 		return NULL;
116c66ac9dbSNicholas Bellinger 
117c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
118c66ac9dbSNicholas Bellinger 	list_for_each_entry(tf, &g_tf_list, tf_list) {
1196708bb27SAndy Grover 		if (!strcmp(tf->tf_name, name)) {
120c66ac9dbSNicholas Bellinger 			atomic_inc(&tf->tf_access_cnt);
121c66ac9dbSNicholas Bellinger 			mutex_unlock(&g_tf_lock);
122c66ac9dbSNicholas Bellinger 			return tf;
123c66ac9dbSNicholas Bellinger 		}
124c66ac9dbSNicholas Bellinger 	}
125c66ac9dbSNicholas Bellinger 	mutex_unlock(&g_tf_lock);
126c66ac9dbSNicholas Bellinger 
127c66ac9dbSNicholas Bellinger 	return NULL;
128c66ac9dbSNicholas Bellinger }
129c66ac9dbSNicholas Bellinger 
130c66ac9dbSNicholas Bellinger /*
131c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->make_group()
132c66ac9dbSNicholas Bellinger  */
133c66ac9dbSNicholas Bellinger static struct config_group *target_core_register_fabric(
134c66ac9dbSNicholas Bellinger 	struct config_group *group,
135c66ac9dbSNicholas Bellinger 	const char *name)
136c66ac9dbSNicholas Bellinger {
137c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
138c66ac9dbSNicholas Bellinger 	int ret;
139c66ac9dbSNicholas Bellinger 
1406708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
141c66ac9dbSNicholas Bellinger 			" %s\n", group, name);
142e7b7af6eSRoland Dreier 
143e7b7af6eSRoland Dreier 	tf = target_core_get_fabric(name);
144e7b7af6eSRoland Dreier 	if (!tf) {
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:"
196c66ac9dbSNicholas Bellinger 			" %s\n", tf->tf_name);
197c66ac9dbSNicholas Bellinger 	/*
198c66ac9dbSNicholas Bellinger 	 * On a successful target_core_get_fabric() look, the returned
199c66ac9dbSNicholas Bellinger 	 * struct target_fabric_configfs *tf will contain a usage reference.
200c66ac9dbSNicholas Bellinger 	 */
2016708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
202d80e224dSAndy Grover 			&tf->tf_cit_tmpl.tfc_wwn_cit);
203c66ac9dbSNicholas Bellinger 
204c66ac9dbSNicholas Bellinger 	tf->tf_group.default_groups = tf->tf_default_groups;
205c66ac9dbSNicholas Bellinger 	tf->tf_group.default_groups[0] = &tf->tf_disc_group;
206c66ac9dbSNicholas Bellinger 	tf->tf_group.default_groups[1] = NULL;
207c66ac9dbSNicholas Bellinger 
208c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tf->tf_group, name,
209d80e224dSAndy Grover 			&tf->tf_cit_tmpl.tfc_wwn_cit);
210c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
211d80e224dSAndy Grover 			&tf->tf_cit_tmpl.tfc_discovery_cit);
212c66ac9dbSNicholas Bellinger 
2136708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
214c66ac9dbSNicholas Bellinger 			" %s\n", tf->tf_group.cg_item.ci_name);
215c66ac9dbSNicholas Bellinger 	tf->tf_fabric = &tf->tf_group.cg_item;
2166708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
217c66ac9dbSNicholas Bellinger 			" for %s\n", name);
218c66ac9dbSNicholas Bellinger 
219c66ac9dbSNicholas Bellinger 	return &tf->tf_group;
220c66ac9dbSNicholas Bellinger }
221c66ac9dbSNicholas Bellinger 
222c66ac9dbSNicholas Bellinger /*
223c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->drop_item()
224c66ac9dbSNicholas Bellinger  */
225c66ac9dbSNicholas Bellinger static void target_core_deregister_fabric(
226c66ac9dbSNicholas Bellinger 	struct config_group *group,
227c66ac9dbSNicholas Bellinger 	struct config_item *item)
228c66ac9dbSNicholas Bellinger {
229c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf = container_of(
230c66ac9dbSNicholas Bellinger 		to_config_group(item), struct target_fabric_configfs, tf_group);
231c66ac9dbSNicholas Bellinger 	struct config_group *tf_group;
232c66ac9dbSNicholas Bellinger 	struct config_item *df_item;
233c66ac9dbSNicholas Bellinger 	int i;
234c66ac9dbSNicholas Bellinger 
2356708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
236c66ac9dbSNicholas Bellinger 		" tf list\n", config_item_name(item));
237c66ac9dbSNicholas Bellinger 
2386708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
239c66ac9dbSNicholas Bellinger 			" %s\n", tf->tf_name);
240c66ac9dbSNicholas Bellinger 	atomic_dec(&tf->tf_access_cnt);
241c66ac9dbSNicholas Bellinger 
2426708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
243c66ac9dbSNicholas Bellinger 			" tf->tf_fabric for %s\n", tf->tf_name);
244c66ac9dbSNicholas Bellinger 	tf->tf_fabric = NULL;
245c66ac9dbSNicholas Bellinger 
2466708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
247c66ac9dbSNicholas Bellinger 			" %s\n", config_item_name(item));
248c66ac9dbSNicholas Bellinger 
249c66ac9dbSNicholas Bellinger 	tf_group = &tf->tf_group;
250c66ac9dbSNicholas Bellinger 	for (i = 0; tf_group->default_groups[i]; i++) {
251c66ac9dbSNicholas Bellinger 		df_item = &tf_group->default_groups[i]->cg_item;
252c66ac9dbSNicholas Bellinger 		tf_group->default_groups[i] = NULL;
253c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
254c66ac9dbSNicholas Bellinger 	}
255c66ac9dbSNicholas Bellinger 	config_item_put(item);
256c66ac9dbSNicholas Bellinger }
257c66ac9dbSNicholas Bellinger 
258c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_fabric_group_ops = {
259c66ac9dbSNicholas Bellinger 	.make_group	= &target_core_register_fabric,
260c66ac9dbSNicholas Bellinger 	.drop_item	= &target_core_deregister_fabric,
261c66ac9dbSNicholas Bellinger };
262c66ac9dbSNicholas Bellinger 
263c66ac9dbSNicholas Bellinger /*
264c66ac9dbSNicholas Bellinger  * All item attributes appearing in /sys/kernel/target/ appear here.
265c66ac9dbSNicholas Bellinger  */
266c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_fabric_item_attrs[] = {
267c66ac9dbSNicholas Bellinger 	&target_core_item_attr_version,
268c66ac9dbSNicholas Bellinger 	NULL,
269c66ac9dbSNicholas Bellinger };
270c66ac9dbSNicholas Bellinger 
271c66ac9dbSNicholas Bellinger /*
272c66ac9dbSNicholas Bellinger  * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
273c66ac9dbSNicholas Bellinger  */
274c66ac9dbSNicholas Bellinger static struct config_item_type target_core_fabrics_item = {
275c66ac9dbSNicholas Bellinger 	.ct_item_ops	= &target_core_fabric_item_ops,
276c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_fabric_group_ops,
277c66ac9dbSNicholas Bellinger 	.ct_attrs	= target_core_fabric_item_attrs,
278c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
279c66ac9dbSNicholas Bellinger };
280c66ac9dbSNicholas Bellinger 
281c66ac9dbSNicholas Bellinger static struct configfs_subsystem target_core_fabrics = {
282c66ac9dbSNicholas Bellinger 	.su_group = {
283c66ac9dbSNicholas Bellinger 		.cg_item = {
284c66ac9dbSNicholas Bellinger 			.ci_namebuf = "target",
285c66ac9dbSNicholas Bellinger 			.ci_type = &target_core_fabrics_item,
286c66ac9dbSNicholas Bellinger 		},
287c66ac9dbSNicholas Bellinger 	},
288c66ac9dbSNicholas Bellinger };
289c66ac9dbSNicholas Bellinger 
290d588cf8fSChristoph Hellwig int target_depend_item(struct config_item *item)
291d588cf8fSChristoph Hellwig {
292d588cf8fSChristoph Hellwig 	return configfs_depend_item(&target_core_fabrics, item);
293d588cf8fSChristoph Hellwig }
294d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_depend_item);
295d588cf8fSChristoph Hellwig 
296d588cf8fSChristoph Hellwig void target_undepend_item(struct config_item *item)
297d588cf8fSChristoph Hellwig {
298d588cf8fSChristoph Hellwig 	return configfs_undepend_item(&target_core_fabrics, item);
299d588cf8fSChristoph Hellwig }
300d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_undepend_item);
301c66ac9dbSNicholas Bellinger 
302c66ac9dbSNicholas Bellinger /*##############################################################################
303c66ac9dbSNicholas Bellinger // Start functions called by external Target Fabrics Modules
304c66ac9dbSNicholas Bellinger //############################################################################*/
305c66ac9dbSNicholas Bellinger 
3069ac8928eSChristoph Hellwig static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
307c66ac9dbSNicholas Bellinger {
3089ac8928eSChristoph Hellwig 	if (!tfo->name) {
3099ac8928eSChristoph Hellwig 		pr_err("Missing tfo->name\n");
3109ac8928eSChristoph Hellwig 		return -EINVAL;
311c66ac9dbSNicholas Bellinger 	}
3129ac8928eSChristoph Hellwig 	if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
3136708bb27SAndy Grover 		pr_err("Passed name: %s exceeds TARGET_FABRIC"
3149ac8928eSChristoph Hellwig 			"_NAME_SIZE\n", tfo->name);
3159ac8928eSChristoph Hellwig 		return -EINVAL;
316c66ac9dbSNicholas Bellinger 	}
3176708bb27SAndy Grover 	if (!tfo->get_fabric_name) {
3186708bb27SAndy Grover 		pr_err("Missing tfo->get_fabric_name()\n");
319c66ac9dbSNicholas Bellinger 		return -EINVAL;
320c66ac9dbSNicholas Bellinger 	}
3216708bb27SAndy Grover 	if (!tfo->tpg_get_wwn) {
3226708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_wwn()\n");
323c66ac9dbSNicholas Bellinger 		return -EINVAL;
324c66ac9dbSNicholas Bellinger 	}
3256708bb27SAndy Grover 	if (!tfo->tpg_get_tag) {
3266708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_tag()\n");
327c66ac9dbSNicholas Bellinger 		return -EINVAL;
328c66ac9dbSNicholas Bellinger 	}
3296708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode) {
3306708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode()\n");
331c66ac9dbSNicholas Bellinger 		return -EINVAL;
332c66ac9dbSNicholas Bellinger 	}
3336708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_cache) {
3346708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
335c66ac9dbSNicholas Bellinger 		return -EINVAL;
336c66ac9dbSNicholas Bellinger 	}
3376708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_write_protect) {
3386708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
339c66ac9dbSNicholas Bellinger 		return -EINVAL;
340c66ac9dbSNicholas Bellinger 	}
3416708bb27SAndy Grover 	if (!tfo->tpg_check_prod_mode_write_protect) {
3426708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
343c66ac9dbSNicholas Bellinger 		return -EINVAL;
344c66ac9dbSNicholas Bellinger 	}
3456708bb27SAndy Grover 	if (!tfo->tpg_get_inst_index) {
3466708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_inst_index()\n");
347c66ac9dbSNicholas Bellinger 		return -EINVAL;
348c66ac9dbSNicholas Bellinger 	}
34935462975SChristoph Hellwig 	if (!tfo->release_cmd) {
3506708bb27SAndy Grover 		pr_err("Missing tfo->release_cmd()\n");
351c66ac9dbSNicholas Bellinger 		return -EINVAL;
352c66ac9dbSNicholas Bellinger 	}
3536708bb27SAndy Grover 	if (!tfo->shutdown_session) {
3546708bb27SAndy Grover 		pr_err("Missing tfo->shutdown_session()\n");
355c66ac9dbSNicholas Bellinger 		return -EINVAL;
356c66ac9dbSNicholas Bellinger 	}
3576708bb27SAndy Grover 	if (!tfo->close_session) {
3586708bb27SAndy Grover 		pr_err("Missing tfo->close_session()\n");
359c66ac9dbSNicholas Bellinger 		return -EINVAL;
360c66ac9dbSNicholas Bellinger 	}
3616708bb27SAndy Grover 	if (!tfo->sess_get_index) {
3626708bb27SAndy Grover 		pr_err("Missing tfo->sess_get_index()\n");
363c66ac9dbSNicholas Bellinger 		return -EINVAL;
364c66ac9dbSNicholas Bellinger 	}
3656708bb27SAndy Grover 	if (!tfo->write_pending) {
3666708bb27SAndy Grover 		pr_err("Missing tfo->write_pending()\n");
367c66ac9dbSNicholas Bellinger 		return -EINVAL;
368c66ac9dbSNicholas Bellinger 	}
3696708bb27SAndy Grover 	if (!tfo->write_pending_status) {
3706708bb27SAndy Grover 		pr_err("Missing tfo->write_pending_status()\n");
371c66ac9dbSNicholas Bellinger 		return -EINVAL;
372c66ac9dbSNicholas Bellinger 	}
3736708bb27SAndy Grover 	if (!tfo->set_default_node_attributes) {
3746708bb27SAndy Grover 		pr_err("Missing tfo->set_default_node_attributes()\n");
375c66ac9dbSNicholas Bellinger 		return -EINVAL;
376c66ac9dbSNicholas Bellinger 	}
3776708bb27SAndy Grover 	if (!tfo->get_cmd_state) {
3786708bb27SAndy Grover 		pr_err("Missing tfo->get_cmd_state()\n");
379c66ac9dbSNicholas Bellinger 		return -EINVAL;
380c66ac9dbSNicholas Bellinger 	}
3816708bb27SAndy Grover 	if (!tfo->queue_data_in) {
3826708bb27SAndy Grover 		pr_err("Missing tfo->queue_data_in()\n");
383c66ac9dbSNicholas Bellinger 		return -EINVAL;
384c66ac9dbSNicholas Bellinger 	}
3856708bb27SAndy Grover 	if (!tfo->queue_status) {
3866708bb27SAndy Grover 		pr_err("Missing tfo->queue_status()\n");
387c66ac9dbSNicholas Bellinger 		return -EINVAL;
388c66ac9dbSNicholas Bellinger 	}
3896708bb27SAndy Grover 	if (!tfo->queue_tm_rsp) {
3906708bb27SAndy Grover 		pr_err("Missing tfo->queue_tm_rsp()\n");
391c66ac9dbSNicholas Bellinger 		return -EINVAL;
392c66ac9dbSNicholas Bellinger 	}
393131e6abcSNicholas Bellinger 	if (!tfo->aborted_task) {
394131e6abcSNicholas Bellinger 		pr_err("Missing tfo->aborted_task()\n");
395131e6abcSNicholas Bellinger 		return -EINVAL;
396131e6abcSNicholas Bellinger 	}
397c66ac9dbSNicholas Bellinger 	/*
398c66ac9dbSNicholas Bellinger 	 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
399c66ac9dbSNicholas Bellinger 	 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
400c66ac9dbSNicholas Bellinger 	 * target_core_fabric_configfs.c WWN+TPG group context code.
401c66ac9dbSNicholas Bellinger 	 */
4026708bb27SAndy Grover 	if (!tfo->fabric_make_wwn) {
4036708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_wwn()\n");
404c66ac9dbSNicholas Bellinger 		return -EINVAL;
405c66ac9dbSNicholas Bellinger 	}
4066708bb27SAndy Grover 	if (!tfo->fabric_drop_wwn) {
4076708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_wwn()\n");
408c66ac9dbSNicholas Bellinger 		return -EINVAL;
409c66ac9dbSNicholas Bellinger 	}
4106708bb27SAndy Grover 	if (!tfo->fabric_make_tpg) {
4116708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_tpg()\n");
412c66ac9dbSNicholas Bellinger 		return -EINVAL;
413c66ac9dbSNicholas Bellinger 	}
4146708bb27SAndy Grover 	if (!tfo->fabric_drop_tpg) {
4156708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_tpg()\n");
416c66ac9dbSNicholas Bellinger 		return -EINVAL;
417c66ac9dbSNicholas Bellinger 	}
418c66ac9dbSNicholas Bellinger 
419c66ac9dbSNicholas Bellinger 	return 0;
420c66ac9dbSNicholas Bellinger }
421c66ac9dbSNicholas Bellinger 
4229ac8928eSChristoph Hellwig int target_register_template(const struct target_core_fabric_ops *fo)
423c66ac9dbSNicholas Bellinger {
4249ac8928eSChristoph Hellwig 	struct target_fabric_configfs *tf;
425c66ac9dbSNicholas Bellinger 	int ret;
426c66ac9dbSNicholas Bellinger 
4279ac8928eSChristoph Hellwig 	ret = target_fabric_tf_ops_check(fo);
4289ac8928eSChristoph Hellwig 	if (ret)
429c66ac9dbSNicholas Bellinger 		return ret;
430c66ac9dbSNicholas Bellinger 
4319ac8928eSChristoph Hellwig 	tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
4329ac8928eSChristoph Hellwig 	if (!tf) {
4339ac8928eSChristoph Hellwig 		pr_err("%s: could not allocate memory!\n", __func__);
4349ac8928eSChristoph Hellwig 		return -ENOMEM;
4359ac8928eSChristoph Hellwig 	}
4369ac8928eSChristoph Hellwig 
4379ac8928eSChristoph Hellwig 	INIT_LIST_HEAD(&tf->tf_list);
4389ac8928eSChristoph Hellwig 	atomic_set(&tf->tf_access_cnt, 0);
4399ac8928eSChristoph Hellwig 
4409ac8928eSChristoph Hellwig 	/*
4419ac8928eSChristoph Hellwig 	 * Setup the default generic struct config_item_type's (cits) in
4429ac8928eSChristoph Hellwig 	 * struct target_fabric_configfs->tf_cit_tmpl
4439ac8928eSChristoph Hellwig 	 */
4449ac8928eSChristoph Hellwig 	tf->tf_module = fo->module;
4459ac8928eSChristoph Hellwig 	snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", fo->name);
4469ac8928eSChristoph Hellwig 
4479ac8928eSChristoph Hellwig 	tf->tf_ops = *fo;
4489ac8928eSChristoph Hellwig 	target_fabric_setup_cits(tf);
4499ac8928eSChristoph Hellwig 
4509ac8928eSChristoph Hellwig 	mutex_lock(&g_tf_lock);
4519ac8928eSChristoph Hellwig 	list_add_tail(&tf->tf_list, &g_tf_list);
4529ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4539ac8928eSChristoph Hellwig 
454c66ac9dbSNicholas Bellinger 	return 0;
455c66ac9dbSNicholas Bellinger }
4569ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_register_template);
457c66ac9dbSNicholas Bellinger 
4589ac8928eSChristoph Hellwig void target_unregister_template(const struct target_core_fabric_ops *fo)
459c66ac9dbSNicholas Bellinger {
4609ac8928eSChristoph Hellwig 	struct target_fabric_configfs *t;
461c66ac9dbSNicholas Bellinger 
462c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
4639ac8928eSChristoph Hellwig 	list_for_each_entry(t, &g_tf_list, tf_list) {
4649ac8928eSChristoph Hellwig 		if (!strcmp(t->tf_name, fo->name)) {
4659ac8928eSChristoph Hellwig 			BUG_ON(atomic_read(&t->tf_access_cnt));
4669ac8928eSChristoph Hellwig 			list_del(&t->tf_list);
4679ac8928eSChristoph Hellwig 			kfree(t);
4689ac8928eSChristoph Hellwig 			break;
469c66ac9dbSNicholas Bellinger 		}
470c66ac9dbSNicholas Bellinger 	}
4719ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4729ac8928eSChristoph Hellwig }
4739ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_unregister_template);
474c66ac9dbSNicholas Bellinger 
475c66ac9dbSNicholas Bellinger /*##############################################################################
476c66ac9dbSNicholas Bellinger // Stop functions called by external Target Fabrics Modules
477c66ac9dbSNicholas Bellinger //############################################################################*/
478c66ac9dbSNicholas Bellinger 
479f79a897eSNicholas Bellinger /* Start functions for struct config_item_type tb_dev_attrib_cit */
480c66ac9dbSNicholas Bellinger 
481c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
482c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
483c66ac9dbSNicholas Bellinger 
484c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_attrib_ops = {
485c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_dev_attrib_attr_show,
486c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_dev_attrib_attr_store,
487c66ac9dbSNicholas Bellinger };
488c66ac9dbSNicholas Bellinger 
48943cf208cSNicholas Bellinger TB_CIT_SETUP(dev_attrib, &target_core_dev_attrib_ops, NULL, NULL);
490c66ac9dbSNicholas Bellinger 
491f79a897eSNicholas Bellinger /* End functions for struct config_item_type tb_dev_attrib_cit */
492c66ac9dbSNicholas Bellinger 
493f8d389c6SNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_wwn_cit */
494c66ac9dbSNicholas Bellinger 
495c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
496c66ac9dbSNicholas Bellinger #define SE_DEV_WWN_ATTR(_name, _mode)					\
497c66ac9dbSNicholas Bellinger static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
498c66ac9dbSNicholas Bellinger 		__CONFIGFS_EATTR(_name, _mode,				\
499c66ac9dbSNicholas Bellinger 		target_core_dev_wwn_show_attr_##_name,			\
500c66ac9dbSNicholas Bellinger 		target_core_dev_wwn_store_attr_##_name);
501c66ac9dbSNicholas Bellinger 
502c66ac9dbSNicholas Bellinger #define SE_DEV_WWN_ATTR_RO(_name);					\
503c66ac9dbSNicholas Bellinger do {									\
504c66ac9dbSNicholas Bellinger 	static struct target_core_dev_wwn_attribute			\
505c66ac9dbSNicholas Bellinger 			target_core_dev_wwn_##_name =			\
506c66ac9dbSNicholas Bellinger 		__CONFIGFS_EATTR_RO(_name,				\
507c66ac9dbSNicholas Bellinger 		target_core_dev_wwn_show_attr_##_name);			\
508c66ac9dbSNicholas Bellinger } while (0);
509c66ac9dbSNicholas Bellinger 
510c66ac9dbSNicholas Bellinger /*
511c66ac9dbSNicholas Bellinger  * VPD page 0x80 Unit serial
512c66ac9dbSNicholas Bellinger  */
513c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
514c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
515c66ac9dbSNicholas Bellinger 	char *page)
516c66ac9dbSNicholas Bellinger {
517c66ac9dbSNicholas Bellinger 	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
518c66ac9dbSNicholas Bellinger 		&t10_wwn->unit_serial[0]);
519c66ac9dbSNicholas Bellinger }
520c66ac9dbSNicholas Bellinger 
521c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
522c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
523c66ac9dbSNicholas Bellinger 	const char *page,
524c66ac9dbSNicholas Bellinger 	size_t count)
525c66ac9dbSNicholas Bellinger {
5260fd97ccfSChristoph Hellwig 	struct se_device *dev = t10_wwn->t10_dev;
527c66ac9dbSNicholas Bellinger 	unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
528c66ac9dbSNicholas Bellinger 
529c66ac9dbSNicholas Bellinger 	/*
530c66ac9dbSNicholas Bellinger 	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
531c66ac9dbSNicholas Bellinger 	 * from the struct scsi_device level firmware, do not allow
532c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial to be emulated.
533c66ac9dbSNicholas Bellinger 	 *
534c66ac9dbSNicholas Bellinger 	 * Note this struct scsi_device could also be emulating VPD
535c66ac9dbSNicholas Bellinger 	 * information from its drivers/scsi LLD.  But for now we assume
536c66ac9dbSNicholas Bellinger 	 * it is doing 'the right thing' wrt a world wide unique
537c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial Number that OS dependent multipath can depend on.
538c66ac9dbSNicholas Bellinger 	 */
5390fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
5406708bb27SAndy Grover 		pr_err("Underlying SCSI device firmware provided VPD"
541c66ac9dbSNicholas Bellinger 			" Unit Serial, ignoring request\n");
542c66ac9dbSNicholas Bellinger 		return -EOPNOTSUPP;
543c66ac9dbSNicholas Bellinger 	}
544c66ac9dbSNicholas Bellinger 
54560d645a4SDan Carpenter 	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
5466708bb27SAndy Grover 		pr_err("Emulated VPD Unit Serial exceeds"
547c66ac9dbSNicholas Bellinger 		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
548c66ac9dbSNicholas Bellinger 		return -EOVERFLOW;
549c66ac9dbSNicholas Bellinger 	}
550c66ac9dbSNicholas Bellinger 	/*
551c66ac9dbSNicholas Bellinger 	 * Check to see if any active $FABRIC_MOD exports exist.  If they
552c66ac9dbSNicholas Bellinger 	 * do exist, fail here as changing this information on the fly
553c66ac9dbSNicholas Bellinger 	 * (underneath the initiator side OS dependent multipath code)
554c66ac9dbSNicholas Bellinger 	 * could cause negative effects.
555c66ac9dbSNicholas Bellinger 	 */
5560fd97ccfSChristoph Hellwig 	if (dev->export_count) {
5576708bb27SAndy Grover 		pr_err("Unable to set VPD Unit Serial while"
558c66ac9dbSNicholas Bellinger 			" active %d $FABRIC_MOD exports exist\n",
5590fd97ccfSChristoph Hellwig 			dev->export_count);
560c66ac9dbSNicholas Bellinger 		return -EINVAL;
561c66ac9dbSNicholas Bellinger 	}
5620fd97ccfSChristoph Hellwig 
563c66ac9dbSNicholas Bellinger 	/*
564c66ac9dbSNicholas Bellinger 	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
565c66ac9dbSNicholas Bellinger 	 *
566c66ac9dbSNicholas Bellinger 	 * Also, strip any newline added from the userspace
567c66ac9dbSNicholas Bellinger 	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
568c66ac9dbSNicholas Bellinger 	 */
569c66ac9dbSNicholas Bellinger 	memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
570c66ac9dbSNicholas Bellinger 	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
5710fd97ccfSChristoph Hellwig 	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
572c66ac9dbSNicholas Bellinger 			"%s", strstrip(buf));
5730fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
574c66ac9dbSNicholas Bellinger 
5756708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
5760fd97ccfSChristoph Hellwig 			" %s\n", dev->t10_wwn.unit_serial);
577c66ac9dbSNicholas Bellinger 
578c66ac9dbSNicholas Bellinger 	return count;
579c66ac9dbSNicholas Bellinger }
580c66ac9dbSNicholas Bellinger 
581c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
582c66ac9dbSNicholas Bellinger 
583c66ac9dbSNicholas Bellinger /*
584c66ac9dbSNicholas Bellinger  * VPD page 0x83 Protocol Identifier
585c66ac9dbSNicholas Bellinger  */
586c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
587c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
588c66ac9dbSNicholas Bellinger 	char *page)
589c66ac9dbSNicholas Bellinger {
590c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;
591c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];
592c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
593c66ac9dbSNicholas Bellinger 
594c66ac9dbSNicholas Bellinger 	memset(buf, 0, VPD_TMP_BUF_SIZE);
595c66ac9dbSNicholas Bellinger 
596c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);
597c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
5986708bb27SAndy Grover 		if (!vpd->protocol_identifier_set)
599c66ac9dbSNicholas Bellinger 			continue;
600c66ac9dbSNicholas Bellinger 
601c66ac9dbSNicholas Bellinger 		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
602c66ac9dbSNicholas Bellinger 
6036708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
604c66ac9dbSNicholas Bellinger 			break;
605c66ac9dbSNicholas Bellinger 
606c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
607c66ac9dbSNicholas Bellinger 	}
608c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);
609c66ac9dbSNicholas Bellinger 
610c66ac9dbSNicholas Bellinger 	return len;
611c66ac9dbSNicholas Bellinger }
612c66ac9dbSNicholas Bellinger 
613c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
614c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
615c66ac9dbSNicholas Bellinger 	const char *page,
616c66ac9dbSNicholas Bellinger 	size_t count)
617c66ac9dbSNicholas Bellinger {
618c66ac9dbSNicholas Bellinger 	return -ENOSYS;
619c66ac9dbSNicholas Bellinger }
620c66ac9dbSNicholas Bellinger 
621c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
622c66ac9dbSNicholas Bellinger 
623c66ac9dbSNicholas Bellinger /*
624c66ac9dbSNicholas Bellinger  * Generic wrapper for dumping VPD identifiers by association.
625c66ac9dbSNicholas Bellinger  */
626c66ac9dbSNicholas Bellinger #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
627c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_show_attr_##_name(			\
628c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,					\
629c66ac9dbSNicholas Bellinger 	char *page)							\
630c66ac9dbSNicholas Bellinger {									\
631c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;							\
632c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];				\
633c66ac9dbSNicholas Bellinger 	ssize_t len = 0;						\
634c66ac9dbSNicholas Bellinger 									\
635c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);				\
636c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
637c66ac9dbSNicholas Bellinger 		if (vpd->association != _assoc)				\
638c66ac9dbSNicholas Bellinger 			continue;					\
639c66ac9dbSNicholas Bellinger 									\
640c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
641c66ac9dbSNicholas Bellinger 		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
6426708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
643c66ac9dbSNicholas Bellinger 			break;						\
644c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
645c66ac9dbSNicholas Bellinger 									\
646c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
647c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
6486708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
649c66ac9dbSNicholas Bellinger 			break;						\
650c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
651c66ac9dbSNicholas Bellinger 									\
652c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
653c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
6546708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
655c66ac9dbSNicholas Bellinger 			break;						\
656c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
657c66ac9dbSNicholas Bellinger 	}								\
658c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);				\
659c66ac9dbSNicholas Bellinger 									\
660c66ac9dbSNicholas Bellinger 	return len;							\
661c66ac9dbSNicholas Bellinger }
662c66ac9dbSNicholas Bellinger 
663c66ac9dbSNicholas Bellinger /*
664163cd5faSAndy Shevchenko  * VPD page 0x83 Association: Logical Unit
665c66ac9dbSNicholas Bellinger  */
666c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
667c66ac9dbSNicholas Bellinger 
668c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
669c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
670c66ac9dbSNicholas Bellinger 	const char *page,
671c66ac9dbSNicholas Bellinger 	size_t count)
672c66ac9dbSNicholas Bellinger {
673c66ac9dbSNicholas Bellinger 	return -ENOSYS;
674c66ac9dbSNicholas Bellinger }
675c66ac9dbSNicholas Bellinger 
676c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
677c66ac9dbSNicholas Bellinger 
678c66ac9dbSNicholas Bellinger /*
679c66ac9dbSNicholas Bellinger  * VPD page 0x83 Association: Target Port
680c66ac9dbSNicholas Bellinger  */
681c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
682c66ac9dbSNicholas Bellinger 
683c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
684c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
685c66ac9dbSNicholas Bellinger 	const char *page,
686c66ac9dbSNicholas Bellinger 	size_t count)
687c66ac9dbSNicholas Bellinger {
688c66ac9dbSNicholas Bellinger 	return -ENOSYS;
689c66ac9dbSNicholas Bellinger }
690c66ac9dbSNicholas Bellinger 
691c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
692c66ac9dbSNicholas Bellinger 
693c66ac9dbSNicholas Bellinger /*
694c66ac9dbSNicholas Bellinger  * VPD page 0x83 Association: SCSI Target Device
695c66ac9dbSNicholas Bellinger  */
696c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
697c66ac9dbSNicholas Bellinger 
698c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
699c66ac9dbSNicholas Bellinger 	struct t10_wwn *t10_wwn,
700c66ac9dbSNicholas Bellinger 	const char *page,
701c66ac9dbSNicholas Bellinger 	size_t count)
702c66ac9dbSNicholas Bellinger {
703c66ac9dbSNicholas Bellinger 	return -ENOSYS;
704c66ac9dbSNicholas Bellinger }
705c66ac9dbSNicholas Bellinger 
706c66ac9dbSNicholas Bellinger SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
707c66ac9dbSNicholas Bellinger 
708c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
709c66ac9dbSNicholas Bellinger 
710c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
711c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_unit_serial.attr,
712c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_protocol_identifier.attr,
713c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_assoc_logical_unit.attr,
714c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_assoc_target_port.attr,
715c66ac9dbSNicholas Bellinger 	&target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
716c66ac9dbSNicholas Bellinger 	NULL,
717c66ac9dbSNicholas Bellinger };
718c66ac9dbSNicholas Bellinger 
719c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_wwn_ops = {
720c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_dev_wwn_attr_show,
721c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_dev_wwn_attr_store,
722c66ac9dbSNicholas Bellinger };
723c66ac9dbSNicholas Bellinger 
724f8d389c6SNicholas Bellinger TB_CIT_SETUP(dev_wwn, &target_core_dev_wwn_ops, NULL, target_core_dev_wwn_attrs);
725c66ac9dbSNicholas Bellinger 
726f8d389c6SNicholas Bellinger /*  End functions for struct config_item_type tb_dev_wwn_cit */
727c66ac9dbSNicholas Bellinger 
72891e2e39bSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_pr_cit */
729c66ac9dbSNicholas Bellinger 
7300fd97ccfSChristoph Hellwig CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
731c66ac9dbSNicholas Bellinger #define SE_DEV_PR_ATTR(_name, _mode)					\
732c66ac9dbSNicholas Bellinger static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
733c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR(_name, _mode,					\
734c66ac9dbSNicholas Bellinger 	target_core_dev_pr_show_attr_##_name,				\
735c66ac9dbSNicholas Bellinger 	target_core_dev_pr_store_attr_##_name);
736c66ac9dbSNicholas Bellinger 
737c66ac9dbSNicholas Bellinger #define SE_DEV_PR_ATTR_RO(_name);					\
738c66ac9dbSNicholas Bellinger static struct target_core_dev_pr_attribute target_core_dev_pr_##_name =	\
739c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR_RO(_name,					\
740c66ac9dbSNicholas Bellinger 	target_core_dev_pr_show_attr_##_name);
741c66ac9dbSNicholas Bellinger 
742d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
743d977f437SChristoph Hellwig 		char *page)
744c66ac9dbSNicholas Bellinger {
745c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
746c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
747c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
748c66ac9dbSNicholas Bellinger 
749c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
750c66ac9dbSNicholas Bellinger 
751c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
752d977f437SChristoph Hellwig 	if (!pr_reg)
753d977f437SChristoph Hellwig 		return sprintf(page, "No SPC-3 Reservation holder\n");
754d977f437SChristoph Hellwig 
755c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
756d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
757c66ac9dbSNicholas Bellinger 
758d977f437SChristoph Hellwig 	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
759e3d6f909SAndy Grover 		se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
760d2843c17SAndy Grover 		se_nacl->initiatorname, i_buf);
761c66ac9dbSNicholas Bellinger }
762c66ac9dbSNicholas Bellinger 
763d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
764d977f437SChristoph Hellwig 		char *page)
765c66ac9dbSNicholas Bellinger {
766c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
767d977f437SChristoph Hellwig 	ssize_t len;
768c66ac9dbSNicholas Bellinger 
769c66ac9dbSNicholas Bellinger 	se_nacl = dev->dev_reserved_node_acl;
770d977f437SChristoph Hellwig 	if (se_nacl) {
771d977f437SChristoph Hellwig 		len = sprintf(page,
772d977f437SChristoph Hellwig 			      "SPC-2 Reservation: %s Initiator: %s\n",
773e3d6f909SAndy Grover 			      se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
774c66ac9dbSNicholas Bellinger 			      se_nacl->initiatorname);
775d977f437SChristoph Hellwig 	} else {
776d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-2 Reservation holder\n");
777d977f437SChristoph Hellwig 	}
778d977f437SChristoph Hellwig 	return len;
779c66ac9dbSNicholas Bellinger }
780c66ac9dbSNicholas Bellinger 
7810fd97ccfSChristoph Hellwig static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
782c66ac9dbSNicholas Bellinger 		char *page)
783c66ac9dbSNicholas Bellinger {
784d977f437SChristoph Hellwig 	int ret;
785c66ac9dbSNicholas Bellinger 
786a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
787d977f437SChristoph Hellwig 		return sprintf(page, "Passthrough\n");
788c66ac9dbSNicholas Bellinger 
789d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
790d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
791d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc2_res(dev, page);
792d977f437SChristoph Hellwig 	else
793d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc3_res(dev, page);
794d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
795d977f437SChristoph Hellwig 	return ret;
796c66ac9dbSNicholas Bellinger }
797c66ac9dbSNicholas Bellinger 
798c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_holder);
799c66ac9dbSNicholas Bellinger 
800c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
8010fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
802c66ac9dbSNicholas Bellinger {
803c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
804c66ac9dbSNicholas Bellinger 
805c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
806d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder) {
807c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
808d977f437SChristoph Hellwig 	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
809c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: All Target"
810c66ac9dbSNicholas Bellinger 			" Ports registration\n");
811d977f437SChristoph Hellwig 	} else {
812c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: Single"
813c66ac9dbSNicholas Bellinger 			" Target Port registration\n");
814d977f437SChristoph Hellwig 	}
815c66ac9dbSNicholas Bellinger 
816d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
817c66ac9dbSNicholas Bellinger 	return len;
818c66ac9dbSNicholas Bellinger }
819c66ac9dbSNicholas Bellinger 
820c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
821c66ac9dbSNicholas Bellinger 
822c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
8230fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
824c66ac9dbSNicholas Bellinger {
8250fd97ccfSChristoph Hellwig 	return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
826c66ac9dbSNicholas Bellinger }
827c66ac9dbSNicholas Bellinger 
828c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_generation);
829c66ac9dbSNicholas Bellinger 
830c66ac9dbSNicholas Bellinger /*
831c66ac9dbSNicholas Bellinger  * res_pr_holder_tg_port
832c66ac9dbSNicholas Bellinger  */
833c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
8340fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
835c66ac9dbSNicholas Bellinger {
836c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
837c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
838c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
839c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
8409ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
841c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
842c66ac9dbSNicholas Bellinger 
843c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
844c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
8456708bb27SAndy Grover 	if (!pr_reg) {
846c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
847d977f437SChristoph Hellwig 		goto out_unlock;
848c66ac9dbSNicholas Bellinger 	}
849d977f437SChristoph Hellwig 
850c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
851c66ac9dbSNicholas Bellinger 	se_tpg = se_nacl->se_tpg;
852c66ac9dbSNicholas Bellinger 	lun = pr_reg->pr_reg_tg_pt_lun;
853e3d6f909SAndy Grover 	tfo = se_tpg->se_tpg_tfo;
854c66ac9dbSNicholas Bellinger 
855c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: %s"
856c66ac9dbSNicholas Bellinger 		" Target Node Endpoint: %s\n", tfo->get_fabric_name(),
857c66ac9dbSNicholas Bellinger 		tfo->tpg_get_wwn(se_tpg));
858c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
85935d1efe8SMasanari Iida 		" Identifier Tag: %hu %s Portal Group Tag: %hu"
860c66ac9dbSNicholas Bellinger 		" %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
861c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
862c66ac9dbSNicholas Bellinger 		tfo->get_fabric_name(), lun->unpacked_lun);
863c66ac9dbSNicholas Bellinger 
864d977f437SChristoph Hellwig out_unlock:
865d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
866c66ac9dbSNicholas Bellinger 	return len;
867c66ac9dbSNicholas Bellinger }
868c66ac9dbSNicholas Bellinger 
869c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
870c66ac9dbSNicholas Bellinger 
871c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
8720fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
873c66ac9dbSNicholas Bellinger {
8749ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
875c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
876c66ac9dbSNicholas Bellinger 	unsigned char buf[384];
877c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
878c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
879d2843c17SAndy Grover 	int reg_count = 0;
880c66ac9dbSNicholas Bellinger 
881c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 PR Registrations:\n");
882c66ac9dbSNicholas Bellinger 
8830fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
8840fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
885c66ac9dbSNicholas Bellinger 			pr_reg_list) {
886c66ac9dbSNicholas Bellinger 
887c66ac9dbSNicholas Bellinger 		memset(buf, 0, 384);
888c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
889c66ac9dbSNicholas Bellinger 		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
890d2843c17SAndy Grover 		core_pr_dump_initiator_port(pr_reg, i_buf,
891c66ac9dbSNicholas Bellinger 					PR_REG_ISID_ID_LEN);
892c66ac9dbSNicholas Bellinger 		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
893c66ac9dbSNicholas Bellinger 			tfo->get_fabric_name(),
894d2843c17SAndy Grover 			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
895c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_generation);
896c66ac9dbSNicholas Bellinger 
8976708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
898c66ac9dbSNicholas Bellinger 			break;
899c66ac9dbSNicholas Bellinger 
900c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
901c66ac9dbSNicholas Bellinger 		reg_count++;
902c66ac9dbSNicholas Bellinger 	}
9030fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
904c66ac9dbSNicholas Bellinger 
9056708bb27SAndy Grover 	if (!reg_count)
906c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "None\n");
907c66ac9dbSNicholas Bellinger 
908c66ac9dbSNicholas Bellinger 	return len;
909c66ac9dbSNicholas Bellinger }
910c66ac9dbSNicholas Bellinger 
911c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
912c66ac9dbSNicholas Bellinger 
913c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_pr_type(
9140fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
915c66ac9dbSNicholas Bellinger {
916c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
917c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
918c66ac9dbSNicholas Bellinger 
919c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
920c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
921d977f437SChristoph Hellwig 	if (pr_reg) {
922c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
923c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
924d977f437SChristoph Hellwig 	} else {
925d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-3 Reservation holder\n");
926d977f437SChristoph Hellwig 	}
927c66ac9dbSNicholas Bellinger 
928d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
929c66ac9dbSNicholas Bellinger 	return len;
930c66ac9dbSNicholas Bellinger }
931c66ac9dbSNicholas Bellinger 
932c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_pr_type);
933c66ac9dbSNicholas Bellinger 
934c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_type(
9350fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
936c66ac9dbSNicholas Bellinger {
937a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
938d977f437SChristoph Hellwig 		return sprintf(page, "SPC_PASSTHROUGH\n");
939d977f437SChristoph Hellwig 	else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
940d977f437SChristoph Hellwig 		return sprintf(page, "SPC2_RESERVATIONS\n");
941d977f437SChristoph Hellwig 	else
942d977f437SChristoph Hellwig 		return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
943c66ac9dbSNicholas Bellinger }
944c66ac9dbSNicholas Bellinger 
945c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_type);
946c66ac9dbSNicholas Bellinger 
947c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
9480fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
949c66ac9dbSNicholas Bellinger {
950a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
951c66ac9dbSNicholas Bellinger 		return 0;
952c66ac9dbSNicholas Bellinger 
953c66ac9dbSNicholas Bellinger 	return sprintf(page, "APTPL Bit Status: %s\n",
9540fd97ccfSChristoph Hellwig 		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
955c66ac9dbSNicholas Bellinger }
956c66ac9dbSNicholas Bellinger 
957c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR_RO(res_aptpl_active);
958c66ac9dbSNicholas Bellinger 
959c66ac9dbSNicholas Bellinger /*
960c66ac9dbSNicholas Bellinger  * res_aptpl_metadata
961c66ac9dbSNicholas Bellinger  */
962c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
9630fd97ccfSChristoph Hellwig 		struct se_device *dev, char *page)
964c66ac9dbSNicholas Bellinger {
965a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
966c66ac9dbSNicholas Bellinger 		return 0;
967c66ac9dbSNicholas Bellinger 
968c66ac9dbSNicholas Bellinger 	return sprintf(page, "Ready to process PR APTPL metadata..\n");
969c66ac9dbSNicholas Bellinger }
970c66ac9dbSNicholas Bellinger 
971c66ac9dbSNicholas Bellinger enum {
972c66ac9dbSNicholas Bellinger 	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
973c66ac9dbSNicholas Bellinger 	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
974c66ac9dbSNicholas Bellinger 	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
975c66ac9dbSNicholas Bellinger 	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
976c66ac9dbSNicholas Bellinger };
977c66ac9dbSNicholas Bellinger 
978c66ac9dbSNicholas Bellinger static match_table_t tokens = {
979c66ac9dbSNicholas Bellinger 	{Opt_initiator_fabric, "initiator_fabric=%s"},
980c66ac9dbSNicholas Bellinger 	{Opt_initiator_node, "initiator_node=%s"},
981c66ac9dbSNicholas Bellinger 	{Opt_initiator_sid, "initiator_sid=%s"},
982c66ac9dbSNicholas Bellinger 	{Opt_sa_res_key, "sa_res_key=%s"},
983c66ac9dbSNicholas Bellinger 	{Opt_res_holder, "res_holder=%d"},
984c66ac9dbSNicholas Bellinger 	{Opt_res_type, "res_type=%d"},
985c66ac9dbSNicholas Bellinger 	{Opt_res_scope, "res_scope=%d"},
986c66ac9dbSNicholas Bellinger 	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
987c66ac9dbSNicholas Bellinger 	{Opt_mapped_lun, "mapped_lun=%d"},
988c66ac9dbSNicholas Bellinger 	{Opt_target_fabric, "target_fabric=%s"},
989c66ac9dbSNicholas Bellinger 	{Opt_target_node, "target_node=%s"},
990c66ac9dbSNicholas Bellinger 	{Opt_tpgt, "tpgt=%d"},
991c66ac9dbSNicholas Bellinger 	{Opt_port_rtpi, "port_rtpi=%d"},
992c66ac9dbSNicholas Bellinger 	{Opt_target_lun, "target_lun=%d"},
993c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
994c66ac9dbSNicholas Bellinger };
995c66ac9dbSNicholas Bellinger 
996c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
9970fd97ccfSChristoph Hellwig 	struct se_device *dev,
998c66ac9dbSNicholas Bellinger 	const char *page,
999c66ac9dbSNicholas Bellinger 	size_t count)
1000c66ac9dbSNicholas Bellinger {
10016d180253SJesper Juhl 	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
10026d180253SJesper Juhl 	unsigned char *t_fabric = NULL, *t_port = NULL;
10038d213559SJoern Engel 	char *orig, *ptr, *opts;
1004c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
1005c66ac9dbSNicholas Bellinger 	unsigned long long tmp_ll;
1006c66ac9dbSNicholas Bellinger 	u64 sa_res_key = 0;
1007c66ac9dbSNicholas Bellinger 	u32 mapped_lun = 0, target_lun = 0;
1008c66ac9dbSNicholas Bellinger 	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
100945fb94c2SBart Van Assche 	u16 tpgt = 0;
101045fb94c2SBart Van Assche 	u8 type = 0;
1011c66ac9dbSNicholas Bellinger 
1012a3541703SAndy Grover 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1013d977f437SChristoph Hellwig 		return 0;
1014d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1015c66ac9dbSNicholas Bellinger 		return 0;
1016c66ac9dbSNicholas Bellinger 
10170fd97ccfSChristoph Hellwig 	if (dev->export_count) {
10186708bb27SAndy Grover 		pr_debug("Unable to process APTPL metadata while"
1019c66ac9dbSNicholas Bellinger 			" active fabric exports exist\n");
1020c66ac9dbSNicholas Bellinger 		return -EINVAL;
1021c66ac9dbSNicholas Bellinger 	}
1022c66ac9dbSNicholas Bellinger 
1023c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
1024c66ac9dbSNicholas Bellinger 	if (!opts)
1025c66ac9dbSNicholas Bellinger 		return -ENOMEM;
1026c66ac9dbSNicholas Bellinger 
1027c66ac9dbSNicholas Bellinger 	orig = opts;
102890c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
1029c66ac9dbSNicholas Bellinger 		if (!*ptr)
1030c66ac9dbSNicholas Bellinger 			continue;
1031c66ac9dbSNicholas Bellinger 
1032c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
1033c66ac9dbSNicholas Bellinger 		switch (token) {
1034c66ac9dbSNicholas Bellinger 		case Opt_initiator_fabric:
10358d213559SJoern Engel 			i_fabric = match_strdup(args);
10366d180253SJesper Juhl 			if (!i_fabric) {
10376d180253SJesper Juhl 				ret = -ENOMEM;
10386d180253SJesper Juhl 				goto out;
10396d180253SJesper Juhl 			}
1040c66ac9dbSNicholas Bellinger 			break;
1041c66ac9dbSNicholas Bellinger 		case Opt_initiator_node:
10428d213559SJoern Engel 			i_port = match_strdup(args);
10436d180253SJesper Juhl 			if (!i_port) {
10446d180253SJesper Juhl 				ret = -ENOMEM;
10456d180253SJesper Juhl 				goto out;
10466d180253SJesper Juhl 			}
104760d645a4SDan Carpenter 			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
10486708bb27SAndy Grover 				pr_err("APTPL metadata initiator_node="
1049c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1050c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_IPORT_LEN);
1051c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1052c66ac9dbSNicholas Bellinger 				break;
1053c66ac9dbSNicholas Bellinger 			}
1054c66ac9dbSNicholas Bellinger 			break;
1055c66ac9dbSNicholas Bellinger 		case Opt_initiator_sid:
10568d213559SJoern Engel 			isid = match_strdup(args);
10576d180253SJesper Juhl 			if (!isid) {
10586d180253SJesper Juhl 				ret = -ENOMEM;
10596d180253SJesper Juhl 				goto out;
10606d180253SJesper Juhl 			}
106160d645a4SDan Carpenter 			if (strlen(isid) >= PR_REG_ISID_LEN) {
10626708bb27SAndy Grover 				pr_err("APTPL metadata initiator_isid"
1063c66ac9dbSNicholas Bellinger 					"= exceeds PR_REG_ISID_LEN: %d\n",
1064c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
1065c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1066c66ac9dbSNicholas Bellinger 				break;
1067c66ac9dbSNicholas Bellinger 			}
1068c66ac9dbSNicholas Bellinger 			break;
1069c66ac9dbSNicholas Bellinger 		case Opt_sa_res_key:
10708d213559SJoern Engel 			ret = kstrtoull(args->from, 0, &tmp_ll);
1071c66ac9dbSNicholas Bellinger 			if (ret < 0) {
10728d213559SJoern Engel 				pr_err("kstrtoull() failed for sa_res_key=\n");
1073c66ac9dbSNicholas Bellinger 				goto out;
1074c66ac9dbSNicholas Bellinger 			}
1075c66ac9dbSNicholas Bellinger 			sa_res_key = (u64)tmp_ll;
1076c66ac9dbSNicholas Bellinger 			break;
1077c66ac9dbSNicholas Bellinger 		/*
1078c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Reservation
1079c66ac9dbSNicholas Bellinger 		 */
1080c66ac9dbSNicholas Bellinger 		case Opt_res_holder:
1081c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1082c66ac9dbSNicholas Bellinger 			res_holder = arg;
1083c66ac9dbSNicholas Bellinger 			break;
1084c66ac9dbSNicholas Bellinger 		case Opt_res_type:
1085c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1086c66ac9dbSNicholas Bellinger 			type = (u8)arg;
1087c66ac9dbSNicholas Bellinger 			break;
1088c66ac9dbSNicholas Bellinger 		case Opt_res_scope:
1089c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1090c66ac9dbSNicholas Bellinger 			break;
1091c66ac9dbSNicholas Bellinger 		case Opt_res_all_tg_pt:
1092c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1093c66ac9dbSNicholas Bellinger 			all_tg_pt = (int)arg;
1094c66ac9dbSNicholas Bellinger 			break;
1095c66ac9dbSNicholas Bellinger 		case Opt_mapped_lun:
1096c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1097c66ac9dbSNicholas Bellinger 			mapped_lun = (u32)arg;
1098c66ac9dbSNicholas Bellinger 			break;
1099c66ac9dbSNicholas Bellinger 		/*
1100c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Target Port
1101c66ac9dbSNicholas Bellinger 		 */
1102c66ac9dbSNicholas Bellinger 		case Opt_target_fabric:
11038d213559SJoern Engel 			t_fabric = match_strdup(args);
11046d180253SJesper Juhl 			if (!t_fabric) {
11056d180253SJesper Juhl 				ret = -ENOMEM;
11066d180253SJesper Juhl 				goto out;
11076d180253SJesper Juhl 			}
1108c66ac9dbSNicholas Bellinger 			break;
1109c66ac9dbSNicholas Bellinger 		case Opt_target_node:
11108d213559SJoern Engel 			t_port = match_strdup(args);
11116d180253SJesper Juhl 			if (!t_port) {
11126d180253SJesper Juhl 				ret = -ENOMEM;
11136d180253SJesper Juhl 				goto out;
11146d180253SJesper Juhl 			}
111560d645a4SDan Carpenter 			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
11166708bb27SAndy Grover 				pr_err("APTPL metadata target_node="
1117c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1118c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_TPORT_LEN);
1119c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1120c66ac9dbSNicholas Bellinger 				break;
1121c66ac9dbSNicholas Bellinger 			}
1122c66ac9dbSNicholas Bellinger 			break;
1123c66ac9dbSNicholas Bellinger 		case Opt_tpgt:
1124c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1125c66ac9dbSNicholas Bellinger 			tpgt = (u16)arg;
1126c66ac9dbSNicholas Bellinger 			break;
1127c66ac9dbSNicholas Bellinger 		case Opt_port_rtpi:
1128c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1129c66ac9dbSNicholas Bellinger 			break;
1130c66ac9dbSNicholas Bellinger 		case Opt_target_lun:
1131c66ac9dbSNicholas Bellinger 			match_int(args, &arg);
1132c66ac9dbSNicholas Bellinger 			target_lun = (u32)arg;
1133c66ac9dbSNicholas Bellinger 			break;
1134c66ac9dbSNicholas Bellinger 		default:
1135c66ac9dbSNicholas Bellinger 			break;
1136c66ac9dbSNicholas Bellinger 		}
1137c66ac9dbSNicholas Bellinger 	}
1138c66ac9dbSNicholas Bellinger 
11396708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
11406708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
1141c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1142c66ac9dbSNicholas Bellinger 		goto out;
1143c66ac9dbSNicholas Bellinger 	}
1144c66ac9dbSNicholas Bellinger 
1145c66ac9dbSNicholas Bellinger 	if (res_holder && !(type)) {
11466708bb27SAndy Grover 		pr_err("Illegal PR type: 0x%02x for reservation"
1147c66ac9dbSNicholas Bellinger 				" holder\n", type);
1148c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1149c66ac9dbSNicholas Bellinger 		goto out;
1150c66ac9dbSNicholas Bellinger 	}
1151c66ac9dbSNicholas Bellinger 
11520fd97ccfSChristoph Hellwig 	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1153c66ac9dbSNicholas Bellinger 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1154c66ac9dbSNicholas Bellinger 			res_holder, all_tg_pt, type);
1155c66ac9dbSNicholas Bellinger out:
11566d180253SJesper Juhl 	kfree(i_fabric);
11576d180253SJesper Juhl 	kfree(i_port);
11586d180253SJesper Juhl 	kfree(isid);
11596d180253SJesper Juhl 	kfree(t_fabric);
11606d180253SJesper Juhl 	kfree(t_port);
1161c66ac9dbSNicholas Bellinger 	kfree(orig);
1162c66ac9dbSNicholas Bellinger 	return (ret == 0) ? count : ret;
1163c66ac9dbSNicholas Bellinger }
1164c66ac9dbSNicholas Bellinger 
1165c66ac9dbSNicholas Bellinger SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1166c66ac9dbSNicholas Bellinger 
11670fd97ccfSChristoph Hellwig CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
1168c66ac9dbSNicholas Bellinger 
1169c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1170c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_holder.attr,
1171c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_all_tgt_pts.attr,
1172c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_generation.attr,
1173c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_holder_tg_port.attr,
1174c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_registered_i_pts.attr,
1175c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_pr_type.attr,
1176c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_type.attr,
1177c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_aptpl_active.attr,
1178c66ac9dbSNicholas Bellinger 	&target_core_dev_pr_res_aptpl_metadata.attr,
1179c66ac9dbSNicholas Bellinger 	NULL,
1180c66ac9dbSNicholas Bellinger };
1181c66ac9dbSNicholas Bellinger 
1182c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_pr_ops = {
1183c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_dev_pr_attr_show,
1184c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_dev_pr_attr_store,
1185c66ac9dbSNicholas Bellinger };
1186c66ac9dbSNicholas Bellinger 
118791e2e39bSNicholas Bellinger TB_CIT_SETUP(dev_pr, &target_core_dev_pr_ops, NULL, target_core_dev_pr_attrs);
1188c66ac9dbSNicholas Bellinger 
118991e2e39bSNicholas Bellinger /*  End functions for struct config_item_type tb_dev_pr_cit */
1190c66ac9dbSNicholas Bellinger 
119173112edcSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_cit */
1192c66ac9dbSNicholas Bellinger 
1193c66ac9dbSNicholas Bellinger static ssize_t target_core_show_dev_info(void *p, char *page)
1194c66ac9dbSNicholas Bellinger {
11950fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
11960fd97ccfSChristoph Hellwig 	struct se_subsystem_api *t = dev->transport;
1197c66ac9dbSNicholas Bellinger 	int bl = 0;
1198c66ac9dbSNicholas Bellinger 	ssize_t read_bytes = 0;
1199c66ac9dbSNicholas Bellinger 
12000fd97ccfSChristoph Hellwig 	transport_dump_dev_state(dev, page, &bl);
1201c66ac9dbSNicholas Bellinger 	read_bytes += bl;
12020fd97ccfSChristoph Hellwig 	read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
1203c66ac9dbSNicholas Bellinger 	return read_bytes;
1204c66ac9dbSNicholas Bellinger }
1205c66ac9dbSNicholas Bellinger 
1206c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_info = {
1207c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1208c66ac9dbSNicholas Bellinger 		    .ca_name = "info",
1209c66ac9dbSNicholas Bellinger 		    .ca_mode = S_IRUGO },
1210c66ac9dbSNicholas Bellinger 	.show	= target_core_show_dev_info,
1211c66ac9dbSNicholas Bellinger 	.store	= NULL,
1212c66ac9dbSNicholas Bellinger };
1213c66ac9dbSNicholas Bellinger 
1214c66ac9dbSNicholas Bellinger static ssize_t target_core_store_dev_control(
1215c66ac9dbSNicholas Bellinger 	void *p,
1216c66ac9dbSNicholas Bellinger 	const char *page,
1217c66ac9dbSNicholas Bellinger 	size_t count)
1218c66ac9dbSNicholas Bellinger {
12190fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
12200fd97ccfSChristoph Hellwig 	struct se_subsystem_api *t = dev->transport;
1221c66ac9dbSNicholas Bellinger 
12220fd97ccfSChristoph Hellwig 	return t->set_configfs_dev_params(dev, page, count);
1223c66ac9dbSNicholas Bellinger }
1224c66ac9dbSNicholas Bellinger 
1225c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_control = {
1226c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1227c66ac9dbSNicholas Bellinger 		    .ca_name = "control",
1228c66ac9dbSNicholas Bellinger 		    .ca_mode = S_IWUSR },
1229c66ac9dbSNicholas Bellinger 	.show	= NULL,
1230c66ac9dbSNicholas Bellinger 	.store	= target_core_store_dev_control,
1231c66ac9dbSNicholas Bellinger };
1232c66ac9dbSNicholas Bellinger 
1233c66ac9dbSNicholas Bellinger static ssize_t target_core_show_dev_alias(void *p, char *page)
1234c66ac9dbSNicholas Bellinger {
12350fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
1236c66ac9dbSNicholas Bellinger 
12370fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_ALIAS))
1238c66ac9dbSNicholas Bellinger 		return 0;
1239c66ac9dbSNicholas Bellinger 
12400fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1241c66ac9dbSNicholas Bellinger }
1242c66ac9dbSNicholas Bellinger 
1243c66ac9dbSNicholas Bellinger static ssize_t target_core_store_dev_alias(
1244c66ac9dbSNicholas Bellinger 	void *p,
1245c66ac9dbSNicholas Bellinger 	const char *page,
1246c66ac9dbSNicholas Bellinger 	size_t count)
1247c66ac9dbSNicholas Bellinger {
12480fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
12490fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1250c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1251c66ac9dbSNicholas Bellinger 
1252c66ac9dbSNicholas Bellinger 	if (count > (SE_DEV_ALIAS_LEN-1)) {
12536708bb27SAndy Grover 		pr_err("alias count: %d exceeds"
1254c66ac9dbSNicholas Bellinger 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1255c66ac9dbSNicholas Bellinger 			SE_DEV_ALIAS_LEN-1);
1256c66ac9dbSNicholas Bellinger 		return -EINVAL;
1257c66ac9dbSNicholas Bellinger 	}
1258c66ac9dbSNicholas Bellinger 
12590fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
12603011684cSDan Carpenter 	if (!read_bytes)
12613011684cSDan Carpenter 		return -EINVAL;
12620fd97ccfSChristoph Hellwig 	if (dev->dev_alias[read_bytes - 1] == '\n')
12630fd97ccfSChristoph Hellwig 		dev->dev_alias[read_bytes - 1] = '\0';
12640877eafdSSebastian Andrzej Siewior 
12650fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_ALIAS;
12663011684cSDan Carpenter 
12676708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1268c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
12690fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
12700fd97ccfSChristoph Hellwig 		dev->dev_alias);
1271c66ac9dbSNicholas Bellinger 
1272c66ac9dbSNicholas Bellinger 	return read_bytes;
1273c66ac9dbSNicholas Bellinger }
1274c66ac9dbSNicholas Bellinger 
1275c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1276c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1277c66ac9dbSNicholas Bellinger 		    .ca_name = "alias",
1278c66ac9dbSNicholas Bellinger 		    .ca_mode =  S_IRUGO | S_IWUSR },
1279c66ac9dbSNicholas Bellinger 	.show	= target_core_show_dev_alias,
1280c66ac9dbSNicholas Bellinger 	.store	= target_core_store_dev_alias,
1281c66ac9dbSNicholas Bellinger };
1282c66ac9dbSNicholas Bellinger 
1283c66ac9dbSNicholas Bellinger static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1284c66ac9dbSNicholas Bellinger {
12850fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
1286c66ac9dbSNicholas Bellinger 
12870fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1288c66ac9dbSNicholas Bellinger 		return 0;
1289c66ac9dbSNicholas Bellinger 
12900fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1291c66ac9dbSNicholas Bellinger }
1292c66ac9dbSNicholas Bellinger 
1293c66ac9dbSNicholas Bellinger static ssize_t target_core_store_dev_udev_path(
1294c66ac9dbSNicholas Bellinger 	void *p,
1295c66ac9dbSNicholas Bellinger 	const char *page,
1296c66ac9dbSNicholas Bellinger 	size_t count)
1297c66ac9dbSNicholas Bellinger {
12980fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
12990fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1300c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1301c66ac9dbSNicholas Bellinger 
1302c66ac9dbSNicholas Bellinger 	if (count > (SE_UDEV_PATH_LEN-1)) {
13036708bb27SAndy Grover 		pr_err("udev_path count: %d exceeds"
1304c66ac9dbSNicholas Bellinger 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1305c66ac9dbSNicholas Bellinger 			SE_UDEV_PATH_LEN-1);
1306c66ac9dbSNicholas Bellinger 		return -EINVAL;
1307c66ac9dbSNicholas Bellinger 	}
1308c66ac9dbSNicholas Bellinger 
13090fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1310c66ac9dbSNicholas Bellinger 			"%s", page);
13113011684cSDan Carpenter 	if (!read_bytes)
13123011684cSDan Carpenter 		return -EINVAL;
13130fd97ccfSChristoph Hellwig 	if (dev->udev_path[read_bytes - 1] == '\n')
13140fd97ccfSChristoph Hellwig 		dev->udev_path[read_bytes - 1] = '\0';
13150877eafdSSebastian Andrzej Siewior 
13160fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_UDEV_PATH;
13173011684cSDan Carpenter 
13186708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1319c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
13200fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
13210fd97ccfSChristoph Hellwig 		dev->udev_path);
1322c66ac9dbSNicholas Bellinger 
1323c66ac9dbSNicholas Bellinger 	return read_bytes;
1324c66ac9dbSNicholas Bellinger }
1325c66ac9dbSNicholas Bellinger 
1326c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1327c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1328c66ac9dbSNicholas Bellinger 		    .ca_name = "udev_path",
1329c66ac9dbSNicholas Bellinger 		    .ca_mode =  S_IRUGO | S_IWUSR },
1330c66ac9dbSNicholas Bellinger 	.show	= target_core_show_dev_udev_path,
1331c66ac9dbSNicholas Bellinger 	.store	= target_core_store_dev_udev_path,
1332c66ac9dbSNicholas Bellinger };
1333c66ac9dbSNicholas Bellinger 
133464146db7SAndy Grover static ssize_t target_core_show_dev_enable(void *p, char *page)
133564146db7SAndy Grover {
133664146db7SAndy Grover 	struct se_device *dev = p;
133764146db7SAndy Grover 
133864146db7SAndy Grover 	return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
133964146db7SAndy Grover }
134064146db7SAndy Grover 
1341c66ac9dbSNicholas Bellinger static ssize_t target_core_store_dev_enable(
1342c66ac9dbSNicholas Bellinger 	void *p,
1343c66ac9dbSNicholas Bellinger 	const char *page,
1344c66ac9dbSNicholas Bellinger 	size_t count)
1345c66ac9dbSNicholas Bellinger {
13460fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
1347c66ac9dbSNicholas Bellinger 	char *ptr;
13480fd97ccfSChristoph Hellwig 	int ret;
1349c66ac9dbSNicholas Bellinger 
1350c66ac9dbSNicholas Bellinger 	ptr = strstr(page, "1");
13516708bb27SAndy Grover 	if (!ptr) {
13526708bb27SAndy Grover 		pr_err("For dev_enable ops, only valid value"
1353c66ac9dbSNicholas Bellinger 				" is \"1\"\n");
1354c66ac9dbSNicholas Bellinger 		return -EINVAL;
1355c66ac9dbSNicholas Bellinger 	}
1356c66ac9dbSNicholas Bellinger 
13570fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
13580fd97ccfSChristoph Hellwig 	if (ret)
13590fd97ccfSChristoph Hellwig 		return ret;
1360c66ac9dbSNicholas Bellinger 	return count;
1361c66ac9dbSNicholas Bellinger }
1362c66ac9dbSNicholas Bellinger 
1363c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1364c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1365c66ac9dbSNicholas Bellinger 		    .ca_name = "enable",
136664146db7SAndy Grover 		    .ca_mode =  S_IRUGO | S_IWUSR },
136764146db7SAndy Grover 	.show	= target_core_show_dev_enable,
1368c66ac9dbSNicholas Bellinger 	.store	= target_core_store_dev_enable,
1369c66ac9dbSNicholas Bellinger };
1370c66ac9dbSNicholas Bellinger 
1371c66ac9dbSNicholas Bellinger static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1372c66ac9dbSNicholas Bellinger {
13730fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
1374c66ac9dbSNicholas Bellinger 	struct config_item *lu_ci;
1375c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
1376c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1377c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1378c66ac9dbSNicholas Bellinger 
1379c66ac9dbSNicholas Bellinger 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1380c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
1381c87fbd56SChristoph Hellwig 		return 0;
1382c66ac9dbSNicholas Bellinger 
1383c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1384c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
13856708bb27SAndy Grover 	if (lu_gp) {
1386c66ac9dbSNicholas Bellinger 		lu_ci = &lu_gp->lu_gp_group.cg_item;
1387c66ac9dbSNicholas Bellinger 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1388c66ac9dbSNicholas Bellinger 			config_item_name(lu_ci), lu_gp->lu_gp_id);
1389c66ac9dbSNicholas Bellinger 	}
1390c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1391c66ac9dbSNicholas Bellinger 
1392c66ac9dbSNicholas Bellinger 	return len;
1393c66ac9dbSNicholas Bellinger }
1394c66ac9dbSNicholas Bellinger 
1395c66ac9dbSNicholas Bellinger static ssize_t target_core_store_alua_lu_gp(
1396c66ac9dbSNicholas Bellinger 	void *p,
1397c66ac9dbSNicholas Bellinger 	const char *page,
1398c66ac9dbSNicholas Bellinger 	size_t count)
1399c66ac9dbSNicholas Bellinger {
14000fd97ccfSChristoph Hellwig 	struct se_device *dev = p;
14010fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1402c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1403c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1404c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
1405c66ac9dbSNicholas Bellinger 	int move = 0;
1406c66ac9dbSNicholas Bellinger 
1407c87fbd56SChristoph Hellwig 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1408c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
1409c87fbd56SChristoph Hellwig 		return 0;
1410c87fbd56SChristoph Hellwig 
1411c66ac9dbSNicholas Bellinger 	if (count > LU_GROUP_NAME_BUF) {
14126708bb27SAndy Grover 		pr_err("ALUA LU Group Alias too large!\n");
1413c66ac9dbSNicholas Bellinger 		return -EINVAL;
1414c66ac9dbSNicholas Bellinger 	}
1415c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
1416c66ac9dbSNicholas Bellinger 	memcpy(buf, page, count);
1417c66ac9dbSNicholas Bellinger 	/*
1418c66ac9dbSNicholas Bellinger 	 * Any ALUA logical unit alias besides "NULL" means we will be
1419c66ac9dbSNicholas Bellinger 	 * making a new group association.
1420c66ac9dbSNicholas Bellinger 	 */
1421c66ac9dbSNicholas Bellinger 	if (strcmp(strstrip(buf), "NULL")) {
1422c66ac9dbSNicholas Bellinger 		/*
1423c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name() will increment reference to
1424c66ac9dbSNicholas Bellinger 		 * struct t10_alua_lu_gp.  This reference is released with
1425c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name below().
1426c66ac9dbSNicholas Bellinger 		 */
1427c66ac9dbSNicholas Bellinger 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
14286708bb27SAndy Grover 		if (!lu_gp_new)
1429c66ac9dbSNicholas Bellinger 			return -ENODEV;
1430c66ac9dbSNicholas Bellinger 	}
1431c66ac9dbSNicholas Bellinger 
1432c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1433c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
14346708bb27SAndy Grover 	if (lu_gp) {
1435c66ac9dbSNicholas Bellinger 		/*
1436c66ac9dbSNicholas Bellinger 		 * Clearing an existing lu_gp association, and replacing
1437c66ac9dbSNicholas Bellinger 		 * with NULL
1438c66ac9dbSNicholas Bellinger 		 */
14396708bb27SAndy Grover 		if (!lu_gp_new) {
14406708bb27SAndy Grover 			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1441c66ac9dbSNicholas Bellinger 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1442c66ac9dbSNicholas Bellinger 				" %hu\n",
1443c66ac9dbSNicholas Bellinger 				config_item_name(&hba->hba_group.cg_item),
14440fd97ccfSChristoph Hellwig 				config_item_name(&dev->dev_group.cg_item),
1445c66ac9dbSNicholas Bellinger 				config_item_name(&lu_gp->lu_gp_group.cg_item),
1446c66ac9dbSNicholas Bellinger 				lu_gp->lu_gp_id);
1447c66ac9dbSNicholas Bellinger 
1448c66ac9dbSNicholas Bellinger 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1449c66ac9dbSNicholas Bellinger 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1450c66ac9dbSNicholas Bellinger 
1451c66ac9dbSNicholas Bellinger 			return count;
1452c66ac9dbSNicholas Bellinger 		}
1453c66ac9dbSNicholas Bellinger 		/*
1454c66ac9dbSNicholas Bellinger 		 * Removing existing association of lu_gp_mem with lu_gp
1455c66ac9dbSNicholas Bellinger 		 */
1456c66ac9dbSNicholas Bellinger 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1457c66ac9dbSNicholas Bellinger 		move = 1;
1458c66ac9dbSNicholas Bellinger 	}
1459c66ac9dbSNicholas Bellinger 	/*
1460c66ac9dbSNicholas Bellinger 	 * Associate lu_gp_mem with lu_gp_new.
1461c66ac9dbSNicholas Bellinger 	 */
1462c66ac9dbSNicholas Bellinger 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1463c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1464c66ac9dbSNicholas Bellinger 
14656708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1466c66ac9dbSNicholas Bellinger 		" core/alua/lu_gps/%s, ID: %hu\n",
1467c66ac9dbSNicholas Bellinger 		(move) ? "Moving" : "Adding",
1468c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
14690fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
1470c66ac9dbSNicholas Bellinger 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1471c66ac9dbSNicholas Bellinger 		lu_gp_new->lu_gp_id);
1472c66ac9dbSNicholas Bellinger 
1473c66ac9dbSNicholas Bellinger 	core_alua_put_lu_gp_from_name(lu_gp_new);
1474c66ac9dbSNicholas Bellinger 	return count;
1475c66ac9dbSNicholas Bellinger }
1476c66ac9dbSNicholas Bellinger 
1477c66ac9dbSNicholas Bellinger static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1478c66ac9dbSNicholas Bellinger 	.attr	= { .ca_owner = THIS_MODULE,
1479c66ac9dbSNicholas Bellinger 		    .ca_name = "alua_lu_gp",
1480c66ac9dbSNicholas Bellinger 		    .ca_mode = S_IRUGO | S_IWUSR },
1481c66ac9dbSNicholas Bellinger 	.show	= target_core_show_alua_lu_gp,
1482c66ac9dbSNicholas Bellinger 	.store	= target_core_store_alua_lu_gp,
1483c66ac9dbSNicholas Bellinger };
1484c66ac9dbSNicholas Bellinger 
1485229d4f11SHannes Reinecke static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1486229d4f11SHannes Reinecke {
1487229d4f11SHannes Reinecke 	struct se_device *dev = p;
1488229d4f11SHannes Reinecke 	struct t10_alua_lba_map *map;
1489229d4f11SHannes Reinecke 	struct t10_alua_lba_map_member *mem;
1490229d4f11SHannes Reinecke 	char *b = page;
1491229d4f11SHannes Reinecke 	int bl = 0;
1492229d4f11SHannes Reinecke 	char state;
1493229d4f11SHannes Reinecke 
1494229d4f11SHannes Reinecke 	spin_lock(&dev->t10_alua.lba_map_lock);
1495229d4f11SHannes Reinecke 	if (!list_empty(&dev->t10_alua.lba_map_list))
1496229d4f11SHannes Reinecke 	    bl += sprintf(b + bl, "%u %u\n",
1497229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_size,
1498229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_multiplier);
1499229d4f11SHannes Reinecke 	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1500229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "%llu %llu",
1501229d4f11SHannes Reinecke 			      map->lba_map_first_lba, map->lba_map_last_lba);
1502229d4f11SHannes Reinecke 		list_for_each_entry(mem, &map->lba_map_mem_list,
1503229d4f11SHannes Reinecke 				    lba_map_mem_list) {
1504229d4f11SHannes Reinecke 			switch (mem->lba_map_mem_alua_state) {
1505229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1506229d4f11SHannes Reinecke 				state = 'O';
1507229d4f11SHannes Reinecke 				break;
1508229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1509229d4f11SHannes Reinecke 				state = 'A';
1510229d4f11SHannes Reinecke 				break;
1511229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_STANDBY:
1512229d4f11SHannes Reinecke 				state = 'S';
1513229d4f11SHannes Reinecke 				break;
1514229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_UNAVAILABLE:
1515229d4f11SHannes Reinecke 				state = 'U';
1516229d4f11SHannes Reinecke 				break;
1517229d4f11SHannes Reinecke 			default:
1518229d4f11SHannes Reinecke 				state = '.';
1519229d4f11SHannes Reinecke 				break;
1520229d4f11SHannes Reinecke 			}
1521229d4f11SHannes Reinecke 			bl += sprintf(b + bl, " %d:%c",
1522229d4f11SHannes Reinecke 				      mem->lba_map_mem_alua_pg_id, state);
1523229d4f11SHannes Reinecke 		}
1524229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "\n");
1525229d4f11SHannes Reinecke 	}
1526229d4f11SHannes Reinecke 	spin_unlock(&dev->t10_alua.lba_map_lock);
1527229d4f11SHannes Reinecke 	return bl;
1528229d4f11SHannes Reinecke }
1529229d4f11SHannes Reinecke 
1530229d4f11SHannes Reinecke static ssize_t target_core_store_dev_lba_map(
1531229d4f11SHannes Reinecke 	void *p,
1532229d4f11SHannes Reinecke 	const char *page,
1533229d4f11SHannes Reinecke 	size_t count)
1534229d4f11SHannes Reinecke {
1535229d4f11SHannes Reinecke 	struct se_device *dev = p;
1536229d4f11SHannes Reinecke 	struct t10_alua_lba_map *lba_map = NULL;
1537229d4f11SHannes Reinecke 	struct list_head lba_list;
1538229d4f11SHannes Reinecke 	char *map_entries, *ptr;
1539229d4f11SHannes Reinecke 	char state;
1540229d4f11SHannes Reinecke 	int pg_num = -1, pg;
1541229d4f11SHannes Reinecke 	int ret = 0, num = 0, pg_id, alua_state;
1542229d4f11SHannes Reinecke 	unsigned long start_lba = -1, end_lba = -1;
1543229d4f11SHannes Reinecke 	unsigned long segment_size = -1, segment_mult = -1;
1544229d4f11SHannes Reinecke 
1545229d4f11SHannes Reinecke 	map_entries = kstrdup(page, GFP_KERNEL);
1546229d4f11SHannes Reinecke 	if (!map_entries)
1547229d4f11SHannes Reinecke 		return -ENOMEM;
1548229d4f11SHannes Reinecke 
1549229d4f11SHannes Reinecke 	INIT_LIST_HEAD(&lba_list);
1550229d4f11SHannes Reinecke 	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1551229d4f11SHannes Reinecke 		if (!*ptr)
1552229d4f11SHannes Reinecke 			continue;
1553229d4f11SHannes Reinecke 
1554229d4f11SHannes Reinecke 		if (num == 0) {
1555229d4f11SHannes Reinecke 			if (sscanf(ptr, "%lu %lu\n",
1556229d4f11SHannes Reinecke 				   &segment_size, &segment_mult) != 2) {
1557229d4f11SHannes Reinecke 				pr_err("Invalid line %d\n", num);
1558229d4f11SHannes Reinecke 				ret = -EINVAL;
1559229d4f11SHannes Reinecke 				break;
1560229d4f11SHannes Reinecke 			}
1561229d4f11SHannes Reinecke 			num++;
1562229d4f11SHannes Reinecke 			continue;
1563229d4f11SHannes Reinecke 		}
1564229d4f11SHannes Reinecke 		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1565229d4f11SHannes Reinecke 			pr_err("Invalid line %d\n", num);
1566229d4f11SHannes Reinecke 			ret = -EINVAL;
1567229d4f11SHannes Reinecke 			break;
1568229d4f11SHannes Reinecke 		}
1569229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
1570229d4f11SHannes Reinecke 		if (!ptr) {
1571229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing end lba\n", num);
1572229d4f11SHannes Reinecke 			ret = -EINVAL;
1573229d4f11SHannes Reinecke 			break;
1574229d4f11SHannes Reinecke 		}
1575229d4f11SHannes Reinecke 		ptr++;
1576229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
1577229d4f11SHannes Reinecke 		if (!ptr) {
1578229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing state definitions\n",
1579229d4f11SHannes Reinecke 			       num);
1580229d4f11SHannes Reinecke 			ret = -EINVAL;
1581229d4f11SHannes Reinecke 			break;
1582229d4f11SHannes Reinecke 		}
1583229d4f11SHannes Reinecke 		ptr++;
1584229d4f11SHannes Reinecke 		lba_map = core_alua_allocate_lba_map(&lba_list,
1585229d4f11SHannes Reinecke 						     start_lba, end_lba);
1586229d4f11SHannes Reinecke 		if (IS_ERR(lba_map)) {
1587229d4f11SHannes Reinecke 			ret = PTR_ERR(lba_map);
1588229d4f11SHannes Reinecke 			break;
1589229d4f11SHannes Reinecke 		}
1590229d4f11SHannes Reinecke 		pg = 0;
1591229d4f11SHannes Reinecke 		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1592229d4f11SHannes Reinecke 			switch (state) {
1593229d4f11SHannes Reinecke 			case 'O':
1594229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1595229d4f11SHannes Reinecke 				break;
1596229d4f11SHannes Reinecke 			case 'A':
1597229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1598229d4f11SHannes Reinecke 				break;
1599229d4f11SHannes Reinecke 			case 'S':
1600229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_STANDBY;
1601229d4f11SHannes Reinecke 				break;
1602229d4f11SHannes Reinecke 			case 'U':
1603229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1604229d4f11SHannes Reinecke 				break;
1605229d4f11SHannes Reinecke 			default:
1606229d4f11SHannes Reinecke 				pr_err("Invalid ALUA state '%c'\n", state);
1607229d4f11SHannes Reinecke 				ret = -EINVAL;
1608229d4f11SHannes Reinecke 				goto out;
1609229d4f11SHannes Reinecke 			}
1610229d4f11SHannes Reinecke 
1611229d4f11SHannes Reinecke 			ret = core_alua_allocate_lba_map_mem(lba_map,
1612229d4f11SHannes Reinecke 							     pg_id, alua_state);
1613229d4f11SHannes Reinecke 			if (ret) {
1614229d4f11SHannes Reinecke 				pr_err("Invalid target descriptor %d:%c "
1615229d4f11SHannes Reinecke 				       "at line %d\n",
1616229d4f11SHannes Reinecke 				       pg_id, state, num);
1617229d4f11SHannes Reinecke 				break;
1618229d4f11SHannes Reinecke 			}
1619229d4f11SHannes Reinecke 			pg++;
1620229d4f11SHannes Reinecke 			ptr = strchr(ptr, ' ');
1621229d4f11SHannes Reinecke 			if (ptr)
1622229d4f11SHannes Reinecke 				ptr++;
1623229d4f11SHannes Reinecke 			else
1624229d4f11SHannes Reinecke 				break;
1625229d4f11SHannes Reinecke 		}
1626229d4f11SHannes Reinecke 		if (pg_num == -1)
1627229d4f11SHannes Reinecke 		    pg_num = pg;
1628229d4f11SHannes Reinecke 		else if (pg != pg_num) {
1629229d4f11SHannes Reinecke 			pr_err("Only %d from %d port groups definitions "
1630229d4f11SHannes Reinecke 			       "at line %d\n", pg, pg_num, num);
1631229d4f11SHannes Reinecke 			ret = -EINVAL;
1632229d4f11SHannes Reinecke 			break;
1633229d4f11SHannes Reinecke 		}
1634229d4f11SHannes Reinecke 		num++;
1635229d4f11SHannes Reinecke 	}
1636229d4f11SHannes Reinecke out:
1637229d4f11SHannes Reinecke 	if (ret) {
1638229d4f11SHannes Reinecke 		core_alua_free_lba_map(&lba_list);
1639229d4f11SHannes Reinecke 		count = ret;
1640229d4f11SHannes Reinecke 	} else
1641229d4f11SHannes Reinecke 		core_alua_set_lba_map(dev, &lba_list,
1642229d4f11SHannes Reinecke 				      segment_size, segment_mult);
1643229d4f11SHannes Reinecke 	kfree(map_entries);
1644229d4f11SHannes Reinecke 	return count;
1645229d4f11SHannes Reinecke }
1646229d4f11SHannes Reinecke 
1647229d4f11SHannes Reinecke static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1648229d4f11SHannes Reinecke 	.attr	= { .ca_owner = THIS_MODULE,
1649229d4f11SHannes Reinecke 		    .ca_name = "lba_map",
1650229d4f11SHannes Reinecke 		    .ca_mode = S_IRUGO | S_IWUSR },
1651229d4f11SHannes Reinecke 	.show	= target_core_show_dev_lba_map,
1652229d4f11SHannes Reinecke 	.store	= target_core_store_dev_lba_map,
1653229d4f11SHannes Reinecke };
1654229d4f11SHannes Reinecke 
165573112edcSNicholas Bellinger static struct configfs_attribute *target_core_dev_attrs[] = {
1656c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_info.attr,
1657c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_control.attr,
1658c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_alias.attr,
1659c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_udev_path.attr,
1660c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_enable.attr,
1661c66ac9dbSNicholas Bellinger 	&target_core_attr_dev_alua_lu_gp.attr,
1662229d4f11SHannes Reinecke 	&target_core_attr_dev_lba_map.attr,
1663c66ac9dbSNicholas Bellinger 	NULL,
1664c66ac9dbSNicholas Bellinger };
1665c66ac9dbSNicholas Bellinger 
1666c66ac9dbSNicholas Bellinger static void target_core_dev_release(struct config_item *item)
1667c66ac9dbSNicholas Bellinger {
16680fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
16690fd97ccfSChristoph Hellwig 	struct se_device *dev =
16700fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
1671c66ac9dbSNicholas Bellinger 
1672c66ac9dbSNicholas Bellinger 	kfree(dev_cg->default_groups);
16730fd97ccfSChristoph Hellwig 	target_free_device(dev);
1674c66ac9dbSNicholas Bellinger }
1675c66ac9dbSNicholas Bellinger 
1676c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_show(struct config_item *item,
1677c66ac9dbSNicholas Bellinger 				     struct configfs_attribute *attr,
1678c66ac9dbSNicholas Bellinger 				     char *page)
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->show)
1687c66ac9dbSNicholas Bellinger 		return -EINVAL;
1688c66ac9dbSNicholas Bellinger 
16890fd97ccfSChristoph Hellwig 	return tc_attr->show(dev, page);
1690c66ac9dbSNicholas Bellinger }
1691c66ac9dbSNicholas Bellinger 
1692c66ac9dbSNicholas Bellinger static ssize_t target_core_dev_store(struct config_item *item,
1693c66ac9dbSNicholas Bellinger 				      struct configfs_attribute *attr,
1694c66ac9dbSNicholas Bellinger 				      const char *page, size_t count)
1695c66ac9dbSNicholas Bellinger {
16960fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
16970fd97ccfSChristoph Hellwig 	struct se_device *dev =
16980fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
1699c66ac9dbSNicholas Bellinger 	struct target_core_configfs_attribute *tc_attr = container_of(
1700c66ac9dbSNicholas Bellinger 			attr, struct target_core_configfs_attribute, attr);
1701c66ac9dbSNicholas Bellinger 
17026708bb27SAndy Grover 	if (!tc_attr->store)
1703c66ac9dbSNicholas Bellinger 		return -EINVAL;
1704c66ac9dbSNicholas Bellinger 
17050fd97ccfSChristoph Hellwig 	return tc_attr->store(dev, page, count);
1706c66ac9dbSNicholas Bellinger }
1707c66ac9dbSNicholas Bellinger 
1708c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_dev_item_ops = {
1709c66ac9dbSNicholas Bellinger 	.release		= target_core_dev_release,
1710c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_dev_show,
1711c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_dev_store,
1712c66ac9dbSNicholas Bellinger };
1713c66ac9dbSNicholas Bellinger 
171473112edcSNicholas Bellinger TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
1715c66ac9dbSNicholas Bellinger 
171673112edcSNicholas Bellinger /* End functions for struct config_item_type tb_dev_cit */
1717c66ac9dbSNicholas Bellinger 
1718c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1719c66ac9dbSNicholas Bellinger 
1720c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1721c66ac9dbSNicholas Bellinger #define SE_DEV_ALUA_LU_ATTR(_name, _mode)				\
1722c66ac9dbSNicholas Bellinger static struct target_core_alua_lu_gp_attribute				\
1723c66ac9dbSNicholas Bellinger 			target_core_alua_lu_gp_##_name =		\
1724c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR(_name, _mode,					\
1725c66ac9dbSNicholas Bellinger 	target_core_alua_lu_gp_show_attr_##_name,			\
1726c66ac9dbSNicholas Bellinger 	target_core_alua_lu_gp_store_attr_##_name);
1727c66ac9dbSNicholas Bellinger 
1728c66ac9dbSNicholas Bellinger #define SE_DEV_ALUA_LU_ATTR_RO(_name)					\
1729c66ac9dbSNicholas Bellinger static struct target_core_alua_lu_gp_attribute				\
1730c66ac9dbSNicholas Bellinger 			target_core_alua_lu_gp_##_name =		\
1731c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR_RO(_name,					\
1732c66ac9dbSNicholas Bellinger 	target_core_alua_lu_gp_show_attr_##_name);
1733c66ac9dbSNicholas Bellinger 
1734c66ac9dbSNicholas Bellinger /*
1735c66ac9dbSNicholas Bellinger  * lu_gp_id
1736c66ac9dbSNicholas Bellinger  */
1737c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
1738c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp,
1739c66ac9dbSNicholas Bellinger 	char *page)
1740c66ac9dbSNicholas Bellinger {
17416708bb27SAndy Grover 	if (!lu_gp->lu_gp_valid_id)
1742c66ac9dbSNicholas Bellinger 		return 0;
1743c66ac9dbSNicholas Bellinger 
1744c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
1745c66ac9dbSNicholas Bellinger }
1746c66ac9dbSNicholas Bellinger 
1747c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
1748c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp,
1749c66ac9dbSNicholas Bellinger 	const char *page,
1750c66ac9dbSNicholas Bellinger 	size_t count)
1751c66ac9dbSNicholas Bellinger {
1752c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
1753c66ac9dbSNicholas Bellinger 	unsigned long lu_gp_id;
1754c66ac9dbSNicholas Bellinger 	int ret;
1755c66ac9dbSNicholas Bellinger 
175657103d7fSJingoo Han 	ret = kstrtoul(page, 0, &lu_gp_id);
1757c66ac9dbSNicholas Bellinger 	if (ret < 0) {
175857103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
1759c66ac9dbSNicholas Bellinger 			" lu_gp_id\n", ret);
176057103d7fSJingoo Han 		return ret;
1761c66ac9dbSNicholas Bellinger 	}
1762c66ac9dbSNicholas Bellinger 	if (lu_gp_id > 0x0000ffff) {
17636708bb27SAndy Grover 		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
1764c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", lu_gp_id);
1765c66ac9dbSNicholas Bellinger 		return -EINVAL;
1766c66ac9dbSNicholas Bellinger 	}
1767c66ac9dbSNicholas Bellinger 
1768c66ac9dbSNicholas Bellinger 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
1769c66ac9dbSNicholas Bellinger 	if (ret < 0)
1770c66ac9dbSNicholas Bellinger 		return -EINVAL;
1771c66ac9dbSNicholas Bellinger 
17726708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
1773c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
1774c66ac9dbSNicholas Bellinger 		config_item_name(&alua_lu_gp_cg->cg_item),
1775c66ac9dbSNicholas Bellinger 		lu_gp->lu_gp_id);
1776c66ac9dbSNicholas Bellinger 
1777c66ac9dbSNicholas Bellinger 	return count;
1778c66ac9dbSNicholas Bellinger }
1779c66ac9dbSNicholas Bellinger 
1780c66ac9dbSNicholas Bellinger SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
1781c66ac9dbSNicholas Bellinger 
1782c66ac9dbSNicholas Bellinger /*
1783c66ac9dbSNicholas Bellinger  * members
1784c66ac9dbSNicholas Bellinger  */
1785c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_lu_gp_show_attr_members(
1786c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp,
1787c66ac9dbSNicholas Bellinger 	char *page)
1788c66ac9dbSNicholas Bellinger {
1789c66ac9dbSNicholas Bellinger 	struct se_device *dev;
1790c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
1791c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1792c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
1793c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
1794c66ac9dbSNicholas Bellinger 
1795c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
1796c66ac9dbSNicholas Bellinger 
1797c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp->lu_gp_lock);
1798c66ac9dbSNicholas Bellinger 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1799c66ac9dbSNicholas Bellinger 		dev = lu_gp_mem->lu_gp_mem_dev;
18000fd97ccfSChristoph Hellwig 		hba = dev->se_hba;
1801c66ac9dbSNicholas Bellinger 
1802c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
1803c66ac9dbSNicholas Bellinger 			config_item_name(&hba->hba_group.cg_item),
18040fd97ccfSChristoph Hellwig 			config_item_name(&dev->dev_group.cg_item));
1805c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
1806c66ac9dbSNicholas Bellinger 
1807c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
18086708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
1809c66ac9dbSNicholas Bellinger 				"_members buffer\n");
1810c66ac9dbSNicholas Bellinger 			break;
1811c66ac9dbSNicholas Bellinger 		}
1812c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
1813c66ac9dbSNicholas Bellinger 		len += cur_len;
1814c66ac9dbSNicholas Bellinger 	}
1815c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp->lu_gp_lock);
1816c66ac9dbSNicholas Bellinger 
1817c66ac9dbSNicholas Bellinger 	return len;
1818c66ac9dbSNicholas Bellinger }
1819c66ac9dbSNicholas Bellinger 
1820c66ac9dbSNicholas Bellinger SE_DEV_ALUA_LU_ATTR_RO(members);
1821c66ac9dbSNicholas Bellinger 
1822c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
1823c66ac9dbSNicholas Bellinger 
1824c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
1825c66ac9dbSNicholas Bellinger 	&target_core_alua_lu_gp_lu_gp_id.attr,
1826c66ac9dbSNicholas Bellinger 	&target_core_alua_lu_gp_members.attr,
1827c66ac9dbSNicholas Bellinger 	NULL,
1828c66ac9dbSNicholas Bellinger };
1829c66ac9dbSNicholas Bellinger 
18301f6fe7cbSNicholas Bellinger static void target_core_alua_lu_gp_release(struct config_item *item)
18311f6fe7cbSNicholas Bellinger {
18321f6fe7cbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
18331f6fe7cbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
18341f6fe7cbSNicholas Bellinger 
18351f6fe7cbSNicholas Bellinger 	core_alua_free_lu_gp(lu_gp);
18361f6fe7cbSNicholas Bellinger }
18371f6fe7cbSNicholas Bellinger 
1838c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_lu_gp_ops = {
18391f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_lu_gp_release,
1840c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_alua_lu_gp_attr_show,
1841c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_alua_lu_gp_attr_store,
1842c66ac9dbSNicholas Bellinger };
1843c66ac9dbSNicholas Bellinger 
1844c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gp_cit = {
1845c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
1846c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_lu_gp_attrs,
1847c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
1848c66ac9dbSNicholas Bellinger };
1849c66ac9dbSNicholas Bellinger 
1850c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
1851c66ac9dbSNicholas Bellinger 
1852c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
1853c66ac9dbSNicholas Bellinger 
1854c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_lu_gp(
1855c66ac9dbSNicholas Bellinger 	struct config_group *group,
1856c66ac9dbSNicholas Bellinger 	const char *name)
1857c66ac9dbSNicholas Bellinger {
1858c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
1859c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = NULL;
1860c66ac9dbSNicholas Bellinger 	struct config_item *alua_lu_gp_ci = NULL;
1861c66ac9dbSNicholas Bellinger 
1862c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp(name, 0);
1863c66ac9dbSNicholas Bellinger 	if (IS_ERR(lu_gp))
1864c66ac9dbSNicholas Bellinger 		return NULL;
1865c66ac9dbSNicholas Bellinger 
1866c66ac9dbSNicholas Bellinger 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
1867c66ac9dbSNicholas Bellinger 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
1868c66ac9dbSNicholas Bellinger 
1869c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_lu_gp_cg, name,
1870c66ac9dbSNicholas Bellinger 			&target_core_alua_lu_gp_cit);
1871c66ac9dbSNicholas Bellinger 
18726708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
1873c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s\n",
1874c66ac9dbSNicholas Bellinger 		config_item_name(alua_lu_gp_ci));
1875c66ac9dbSNicholas Bellinger 
1876c66ac9dbSNicholas Bellinger 	return alua_lu_gp_cg;
1877c66ac9dbSNicholas Bellinger 
1878c66ac9dbSNicholas Bellinger }
1879c66ac9dbSNicholas Bellinger 
1880c66ac9dbSNicholas Bellinger static void target_core_alua_drop_lu_gp(
1881c66ac9dbSNicholas Bellinger 	struct config_group *group,
1882c66ac9dbSNicholas Bellinger 	struct config_item *item)
1883c66ac9dbSNicholas Bellinger {
1884c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1885c66ac9dbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
1886c66ac9dbSNicholas Bellinger 
18876708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
1888c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
1889c66ac9dbSNicholas Bellinger 		config_item_name(item), lu_gp->lu_gp_id);
18901f6fe7cbSNicholas Bellinger 	/*
18911f6fe7cbSNicholas Bellinger 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
18921f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_lu_gp_release()
18931f6fe7cbSNicholas Bellinger 	 */
1894c66ac9dbSNicholas Bellinger 	config_item_put(item);
1895c66ac9dbSNicholas Bellinger }
1896c66ac9dbSNicholas Bellinger 
1897c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
1898c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_lu_gp,
1899c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_lu_gp,
1900c66ac9dbSNicholas Bellinger };
1901c66ac9dbSNicholas Bellinger 
1902c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_lu_gps_cit = {
1903c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
1904c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
1905c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
1906c66ac9dbSNicholas Bellinger };
1907c66ac9dbSNicholas Bellinger 
1908c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
1909c66ac9dbSNicholas Bellinger 
1910c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
1911c66ac9dbSNicholas Bellinger 
1912c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
1913c66ac9dbSNicholas Bellinger #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode)				\
1914c66ac9dbSNicholas Bellinger static struct target_core_alua_tg_pt_gp_attribute			\
1915c66ac9dbSNicholas Bellinger 			target_core_alua_tg_pt_gp_##_name =		\
1916c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR(_name, _mode,					\
1917c66ac9dbSNicholas Bellinger 	target_core_alua_tg_pt_gp_show_attr_##_name,			\
1918c66ac9dbSNicholas Bellinger 	target_core_alua_tg_pt_gp_store_attr_##_name);
1919c66ac9dbSNicholas Bellinger 
1920c66ac9dbSNicholas Bellinger #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name)				\
1921c66ac9dbSNicholas Bellinger static struct target_core_alua_tg_pt_gp_attribute			\
1922c66ac9dbSNicholas Bellinger 			target_core_alua_tg_pt_gp_##_name =		\
1923c66ac9dbSNicholas Bellinger 	__CONFIGFS_EATTR_RO(_name,					\
1924c66ac9dbSNicholas Bellinger 	target_core_alua_tg_pt_gp_show_attr_##_name);
1925c66ac9dbSNicholas Bellinger 
1926c66ac9dbSNicholas Bellinger /*
1927c66ac9dbSNicholas Bellinger  * alua_access_state
1928c66ac9dbSNicholas Bellinger  */
1929c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
1930c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
1931c66ac9dbSNicholas Bellinger 	char *page)
1932c66ac9dbSNicholas Bellinger {
1933c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n",
1934c66ac9dbSNicholas Bellinger 		atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
1935c66ac9dbSNicholas Bellinger }
1936c66ac9dbSNicholas Bellinger 
1937c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
1938c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
1939c66ac9dbSNicholas Bellinger 	const char *page,
1940c66ac9dbSNicholas Bellinger 	size_t count)
1941c66ac9dbSNicholas Bellinger {
19420fd97ccfSChristoph Hellwig 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1943c66ac9dbSNicholas Bellinger 	unsigned long tmp;
1944c66ac9dbSNicholas Bellinger 	int new_state, ret;
1945c66ac9dbSNicholas Bellinger 
19466708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
1947125d0119SHannes Reinecke 		pr_err("Unable to do implicit ALUA on non valid"
1948c66ac9dbSNicholas Bellinger 			" tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
1949c66ac9dbSNicholas Bellinger 		return -EINVAL;
1950c66ac9dbSNicholas Bellinger 	}
1951f1453773SNicholas Bellinger 	if (!(dev->dev_flags & DF_CONFIGURED)) {
1952f1453773SNicholas Bellinger 		pr_err("Unable to set alua_access_state while device is"
1953f1453773SNicholas Bellinger 		       " not configured\n");
1954f1453773SNicholas Bellinger 		return -ENODEV;
1955f1453773SNicholas Bellinger 	}
1956c66ac9dbSNicholas Bellinger 
195757103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
1958c66ac9dbSNicholas Bellinger 	if (ret < 0) {
19596708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access state from"
1960c66ac9dbSNicholas Bellinger 				" %s\n", page);
196157103d7fSJingoo Han 		return ret;
1962c66ac9dbSNicholas Bellinger 	}
1963c66ac9dbSNicholas Bellinger 	new_state = (int)tmp;
1964c66ac9dbSNicholas Bellinger 
1965125d0119SHannes Reinecke 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
1966125d0119SHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA"
1967125d0119SHannes Reinecke 			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
1968c66ac9dbSNicholas Bellinger 		return -EINVAL;
1969c66ac9dbSNicholas Bellinger 	}
1970c66094bfSHannes Reinecke 	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
1971c66094bfSHannes Reinecke 	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
1972c66094bfSHannes Reinecke 		/* LBA DEPENDENT is only allowed with implicit ALUA */
1973c66094bfSHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA transition"
1974c66094bfSHannes Reinecke 		       " while explicit ALUA management is enabled\n");
1975c66094bfSHannes Reinecke 		return -EINVAL;
1976c66094bfSHannes Reinecke 	}
1977c66ac9dbSNicholas Bellinger 
19780fd97ccfSChristoph Hellwig 	ret = core_alua_do_port_transition(tg_pt_gp, dev,
1979c66ac9dbSNicholas Bellinger 					NULL, NULL, new_state, 0);
1980c66ac9dbSNicholas Bellinger 	return (!ret) ? count : -EINVAL;
1981c66ac9dbSNicholas Bellinger }
1982c66ac9dbSNicholas Bellinger 
1983c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
1984c66ac9dbSNicholas Bellinger 
1985c66ac9dbSNicholas Bellinger /*
1986c66ac9dbSNicholas Bellinger  * alua_access_status
1987c66ac9dbSNicholas Bellinger  */
1988c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
1989c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
1990c66ac9dbSNicholas Bellinger 	char *page)
1991c66ac9dbSNicholas Bellinger {
1992c66ac9dbSNicholas Bellinger 	return sprintf(page, "%s\n",
1993c66ac9dbSNicholas Bellinger 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
1994c66ac9dbSNicholas Bellinger }
1995c66ac9dbSNicholas Bellinger 
1996c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
1997c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
1998c66ac9dbSNicholas Bellinger 	const char *page,
1999c66ac9dbSNicholas Bellinger 	size_t count)
2000c66ac9dbSNicholas Bellinger {
2001c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2002c66ac9dbSNicholas Bellinger 	int new_status, ret;
2003c66ac9dbSNicholas Bellinger 
20046708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
20056708bb27SAndy Grover 		pr_err("Unable to do set ALUA access status on non"
2006c66ac9dbSNicholas Bellinger 			" valid tg_pt_gp ID: %hu\n",
2007c66ac9dbSNicholas Bellinger 			tg_pt_gp->tg_pt_gp_valid_id);
2008c66ac9dbSNicholas Bellinger 		return -EINVAL;
2009c66ac9dbSNicholas Bellinger 	}
2010c66ac9dbSNicholas Bellinger 
201157103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2012c66ac9dbSNicholas Bellinger 	if (ret < 0) {
20136708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access status"
2014c66ac9dbSNicholas Bellinger 				" from %s\n", page);
201557103d7fSJingoo Han 		return ret;
2016c66ac9dbSNicholas Bellinger 	}
2017c66ac9dbSNicholas Bellinger 	new_status = (int)tmp;
2018c66ac9dbSNicholas Bellinger 
2019c66ac9dbSNicholas Bellinger 	if ((new_status != ALUA_STATUS_NONE) &&
2020125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2021125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
20226708bb27SAndy Grover 		pr_err("Illegal ALUA access status: 0x%02x\n",
2023c66ac9dbSNicholas Bellinger 				new_status);
2024c66ac9dbSNicholas Bellinger 		return -EINVAL;
2025c66ac9dbSNicholas Bellinger 	}
2026c66ac9dbSNicholas Bellinger 
2027c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2028c66ac9dbSNicholas Bellinger 	return count;
2029c66ac9dbSNicholas Bellinger }
2030c66ac9dbSNicholas Bellinger 
2031c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2032c66ac9dbSNicholas Bellinger 
2033c66ac9dbSNicholas Bellinger /*
2034c66ac9dbSNicholas Bellinger  * alua_access_type
2035c66ac9dbSNicholas Bellinger  */
2036c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2037c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2038c66ac9dbSNicholas Bellinger 	char *page)
2039c66ac9dbSNicholas Bellinger {
2040c66ac9dbSNicholas Bellinger 	return core_alua_show_access_type(tg_pt_gp, page);
2041c66ac9dbSNicholas Bellinger }
2042c66ac9dbSNicholas Bellinger 
2043c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2044c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2045c66ac9dbSNicholas Bellinger 	const char *page,
2046c66ac9dbSNicholas Bellinger 	size_t count)
2047c66ac9dbSNicholas Bellinger {
2048c66ac9dbSNicholas Bellinger 	return core_alua_store_access_type(tg_pt_gp, page, count);
2049c66ac9dbSNicholas Bellinger }
2050c66ac9dbSNicholas Bellinger 
2051c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2052c66ac9dbSNicholas Bellinger 
2053c66ac9dbSNicholas Bellinger /*
20546be526c4SHannes Reinecke  * alua_supported_states
20556be526c4SHannes Reinecke  */
2056b0a382c5SHannes Reinecke 
2057b0a382c5SHannes Reinecke #define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit)		\
2058b0a382c5SHannes Reinecke static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2059b0a382c5SHannes Reinecke 	struct t10_alua_tg_pt_gp *t, char *p)				\
2060b0a382c5SHannes Reinecke {									\
2061b0a382c5SHannes Reinecke 	return sprintf(p, "%d\n", !!(t->_var & _bit));			\
20626be526c4SHannes Reinecke }
20636be526c4SHannes Reinecke 
2064b0a382c5SHannes Reinecke #define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit)		\
2065b0a382c5SHannes Reinecke static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2066b0a382c5SHannes Reinecke 	struct t10_alua_tg_pt_gp *t, const char *p, size_t c)		\
2067b0a382c5SHannes Reinecke {									\
2068b0a382c5SHannes Reinecke 	unsigned long tmp;						\
2069b0a382c5SHannes Reinecke 	int ret;							\
2070b0a382c5SHannes Reinecke 									\
2071b0a382c5SHannes Reinecke 	if (!t->tg_pt_gp_valid_id) {					\
2072b0a382c5SHannes Reinecke 		pr_err("Unable to do set ##_name ALUA state on non"	\
2073b0a382c5SHannes Reinecke 		       " valid tg_pt_gp ID: %hu\n",			\
2074b0a382c5SHannes Reinecke 		       t->tg_pt_gp_valid_id);				\
2075b0a382c5SHannes Reinecke 		return -EINVAL;						\
2076b0a382c5SHannes Reinecke 	}								\
2077b0a382c5SHannes Reinecke 									\
2078b0a382c5SHannes Reinecke 	ret = kstrtoul(p, 0, &tmp);					\
2079b0a382c5SHannes Reinecke 	if (ret < 0) {							\
2080b0a382c5SHannes Reinecke 		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
2081b0a382c5SHannes Reinecke 		return -EINVAL;						\
2082b0a382c5SHannes Reinecke 	}								\
2083b0a382c5SHannes Reinecke 	if (tmp > 1) {							\
2084b0a382c5SHannes Reinecke 		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2085b0a382c5SHannes Reinecke 		return -EINVAL;						\
2086b0a382c5SHannes Reinecke 	}								\
20871f0b030cSSebastian Herbszt 	if (tmp)							\
2088b0a382c5SHannes Reinecke 		t->_var |= _bit;					\
2089b0a382c5SHannes Reinecke 	else								\
2090b0a382c5SHannes Reinecke 		t->_var &= ~_bit;					\
2091b0a382c5SHannes Reinecke 									\
2092b0a382c5SHannes Reinecke 	return c;							\
20936be526c4SHannes Reinecke }
20946be526c4SHannes Reinecke 
2095b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2096b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2097b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2098b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2099b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
21006be526c4SHannes Reinecke 
2101b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2102b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2103b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2104b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2105b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
21066be526c4SHannes Reinecke 
2107b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2108b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2109b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2110b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2111c66094bfSHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
21126be526c4SHannes Reinecke 
2113b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2114b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2115b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2116b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2117b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
21186be526c4SHannes Reinecke 
2119b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2120b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2121b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2122b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2123b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2124b0a382c5SHannes Reinecke 
2125b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2126b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2127b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2128b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2129b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2130b0a382c5SHannes Reinecke 
2131b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2132b0a382c5SHannes Reinecke 			       tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2133b0a382c5SHannes Reinecke SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2134b0a382c5SHannes Reinecke 				tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2135b0a382c5SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
21366be526c4SHannes Reinecke 
21376be526c4SHannes Reinecke /*
2138c66ac9dbSNicholas Bellinger  * alua_write_metadata
2139c66ac9dbSNicholas Bellinger  */
2140c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2141c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2142c66ac9dbSNicholas Bellinger 	char *page)
2143c66ac9dbSNicholas Bellinger {
2144c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2145c66ac9dbSNicholas Bellinger }
2146c66ac9dbSNicholas Bellinger 
2147c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2148c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2149c66ac9dbSNicholas Bellinger 	const char *page,
2150c66ac9dbSNicholas Bellinger 	size_t count)
2151c66ac9dbSNicholas Bellinger {
2152c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2153c66ac9dbSNicholas Bellinger 	int ret;
2154c66ac9dbSNicholas Bellinger 
215557103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2156c66ac9dbSNicholas Bellinger 	if (ret < 0) {
21576708bb27SAndy Grover 		pr_err("Unable to extract alua_write_metadata\n");
215857103d7fSJingoo Han 		return ret;
2159c66ac9dbSNicholas Bellinger 	}
2160c66ac9dbSNicholas Bellinger 
2161c66ac9dbSNicholas Bellinger 	if ((tmp != 0) && (tmp != 1)) {
21626708bb27SAndy Grover 		pr_err("Illegal value for alua_write_metadata:"
2163c66ac9dbSNicholas Bellinger 			" %lu\n", tmp);
2164c66ac9dbSNicholas Bellinger 		return -EINVAL;
2165c66ac9dbSNicholas Bellinger 	}
2166c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2167c66ac9dbSNicholas Bellinger 
2168c66ac9dbSNicholas Bellinger 	return count;
2169c66ac9dbSNicholas Bellinger }
2170c66ac9dbSNicholas Bellinger 
2171c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2172c66ac9dbSNicholas Bellinger 
2173c66ac9dbSNicholas Bellinger 
2174c66ac9dbSNicholas Bellinger 
2175c66ac9dbSNicholas Bellinger /*
2176c66ac9dbSNicholas Bellinger  * nonop_delay_msecs
2177c66ac9dbSNicholas Bellinger  */
2178c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2179c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2180c66ac9dbSNicholas Bellinger 	char *page)
2181c66ac9dbSNicholas Bellinger {
2182c66ac9dbSNicholas Bellinger 	return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2183c66ac9dbSNicholas Bellinger 
2184c66ac9dbSNicholas Bellinger }
2185c66ac9dbSNicholas Bellinger 
2186c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2187c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2188c66ac9dbSNicholas Bellinger 	const char *page,
2189c66ac9dbSNicholas Bellinger 	size_t count)
2190c66ac9dbSNicholas Bellinger {
2191c66ac9dbSNicholas Bellinger 	return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2192c66ac9dbSNicholas Bellinger }
2193c66ac9dbSNicholas Bellinger 
2194c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2195c66ac9dbSNicholas Bellinger 
2196c66ac9dbSNicholas Bellinger /*
2197c66ac9dbSNicholas Bellinger  * trans_delay_msecs
2198c66ac9dbSNicholas Bellinger  */
2199c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2200c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2201c66ac9dbSNicholas Bellinger 	char *page)
2202c66ac9dbSNicholas Bellinger {
2203c66ac9dbSNicholas Bellinger 	return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2204c66ac9dbSNicholas Bellinger }
2205c66ac9dbSNicholas Bellinger 
2206c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2207c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2208c66ac9dbSNicholas Bellinger 	const char *page,
2209c66ac9dbSNicholas Bellinger 	size_t count)
2210c66ac9dbSNicholas Bellinger {
2211c66ac9dbSNicholas Bellinger 	return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2212c66ac9dbSNicholas Bellinger }
2213c66ac9dbSNicholas Bellinger 
2214c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2215c66ac9dbSNicholas Bellinger 
2216c66ac9dbSNicholas Bellinger /*
2217125d0119SHannes Reinecke  * implicit_trans_secs
22185b9a4d72SNicholas Bellinger  */
2219125d0119SHannes Reinecke static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
22205b9a4d72SNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
22215b9a4d72SNicholas Bellinger 	char *page)
22225b9a4d72SNicholas Bellinger {
2223125d0119SHannes Reinecke 	return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
22245b9a4d72SNicholas Bellinger }
22255b9a4d72SNicholas Bellinger 
2226125d0119SHannes Reinecke static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
22275b9a4d72SNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
22285b9a4d72SNicholas Bellinger 	const char *page,
22295b9a4d72SNicholas Bellinger 	size_t count)
22305b9a4d72SNicholas Bellinger {
2231125d0119SHannes Reinecke 	return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
22325b9a4d72SNicholas Bellinger }
22335b9a4d72SNicholas Bellinger 
2234125d0119SHannes Reinecke SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
22355b9a4d72SNicholas Bellinger 
22365b9a4d72SNicholas Bellinger /*
2237c66ac9dbSNicholas Bellinger  * preferred
2238c66ac9dbSNicholas Bellinger  */
2239c66ac9dbSNicholas Bellinger 
2240c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2241c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2242c66ac9dbSNicholas Bellinger 	char *page)
2243c66ac9dbSNicholas Bellinger {
2244c66ac9dbSNicholas Bellinger 	return core_alua_show_preferred_bit(tg_pt_gp, page);
2245c66ac9dbSNicholas Bellinger }
2246c66ac9dbSNicholas Bellinger 
2247c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2248c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2249c66ac9dbSNicholas Bellinger 	const char *page,
2250c66ac9dbSNicholas Bellinger 	size_t count)
2251c66ac9dbSNicholas Bellinger {
2252c66ac9dbSNicholas Bellinger 	return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2253c66ac9dbSNicholas Bellinger }
2254c66ac9dbSNicholas Bellinger 
2255c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2256c66ac9dbSNicholas Bellinger 
2257c66ac9dbSNicholas Bellinger /*
2258c66ac9dbSNicholas Bellinger  * tg_pt_gp_id
2259c66ac9dbSNicholas Bellinger  */
2260c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2261c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2262c66ac9dbSNicholas Bellinger 	char *page)
2263c66ac9dbSNicholas Bellinger {
22646708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id)
2265c66ac9dbSNicholas Bellinger 		return 0;
2266c66ac9dbSNicholas Bellinger 
2267c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2268c66ac9dbSNicholas Bellinger }
2269c66ac9dbSNicholas Bellinger 
2270c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2271c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2272c66ac9dbSNicholas Bellinger 	const char *page,
2273c66ac9dbSNicholas Bellinger 	size_t count)
2274c66ac9dbSNicholas Bellinger {
2275c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2276c66ac9dbSNicholas Bellinger 	unsigned long tg_pt_gp_id;
2277c66ac9dbSNicholas Bellinger 	int ret;
2278c66ac9dbSNicholas Bellinger 
227957103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tg_pt_gp_id);
2280c66ac9dbSNicholas Bellinger 	if (ret < 0) {
228157103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2282c66ac9dbSNicholas Bellinger 			" tg_pt_gp_id\n", ret);
228357103d7fSJingoo Han 		return ret;
2284c66ac9dbSNicholas Bellinger 	}
2285c66ac9dbSNicholas Bellinger 	if (tg_pt_gp_id > 0x0000ffff) {
22866708bb27SAndy Grover 		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2287c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", tg_pt_gp_id);
2288c66ac9dbSNicholas Bellinger 		return -EINVAL;
2289c66ac9dbSNicholas Bellinger 	}
2290c66ac9dbSNicholas Bellinger 
2291c66ac9dbSNicholas Bellinger 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2292c66ac9dbSNicholas Bellinger 	if (ret < 0)
2293c66ac9dbSNicholas Bellinger 		return -EINVAL;
2294c66ac9dbSNicholas Bellinger 
22956708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2296c66ac9dbSNicholas Bellinger 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
2297c66ac9dbSNicholas Bellinger 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
2298c66ac9dbSNicholas Bellinger 		tg_pt_gp->tg_pt_gp_id);
2299c66ac9dbSNicholas Bellinger 
2300c66ac9dbSNicholas Bellinger 	return count;
2301c66ac9dbSNicholas Bellinger }
2302c66ac9dbSNicholas Bellinger 
2303c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2304c66ac9dbSNicholas Bellinger 
2305c66ac9dbSNicholas Bellinger /*
2306c66ac9dbSNicholas Bellinger  * members
2307c66ac9dbSNicholas Bellinger  */
2308c66ac9dbSNicholas Bellinger static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2309c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2310c66ac9dbSNicholas Bellinger 	char *page)
2311c66ac9dbSNicholas Bellinger {
2312c66ac9dbSNicholas Bellinger 	struct se_port *port;
2313c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg;
2314c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
2315c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2316c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2317c66ac9dbSNicholas Bellinger 	unsigned char buf[TG_PT_GROUP_NAME_BUF];
2318c66ac9dbSNicholas Bellinger 
2319c66ac9dbSNicholas Bellinger 	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2320c66ac9dbSNicholas Bellinger 
2321c66ac9dbSNicholas Bellinger 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2322c66ac9dbSNicholas Bellinger 	list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2323c66ac9dbSNicholas Bellinger 			tg_pt_gp_mem_list) {
2324c66ac9dbSNicholas Bellinger 		port = tg_pt_gp_mem->tg_pt;
2325c66ac9dbSNicholas Bellinger 		tpg = port->sep_tpg;
2326c66ac9dbSNicholas Bellinger 		lun = port->sep_lun;
2327c66ac9dbSNicholas Bellinger 
2328c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2329e3d6f909SAndy Grover 			"/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2330e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2331e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
2332c66ac9dbSNicholas Bellinger 			config_item_name(&lun->lun_group.cg_item));
2333c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2334c66ac9dbSNicholas Bellinger 
2335c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
23366708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2337c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2338c66ac9dbSNicholas Bellinger 			break;
2339c66ac9dbSNicholas Bellinger 		}
2340c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2341c66ac9dbSNicholas Bellinger 		len += cur_len;
2342c66ac9dbSNicholas Bellinger 	}
2343c66ac9dbSNicholas Bellinger 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2344c66ac9dbSNicholas Bellinger 
2345c66ac9dbSNicholas Bellinger 	return len;
2346c66ac9dbSNicholas Bellinger }
2347c66ac9dbSNicholas Bellinger 
2348c66ac9dbSNicholas Bellinger SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2349c66ac9dbSNicholas Bellinger 
2350c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2351c66ac9dbSNicholas Bellinger 			tg_pt_gp_group);
2352c66ac9dbSNicholas Bellinger 
2353c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2354c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_alua_access_state.attr,
2355c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_alua_access_status.attr,
2356c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_alua_access_type.attr,
2357b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2358b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_offline.attr,
2359b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2360b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2361b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_standby.attr,
2362b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2363b0a382c5SHannes Reinecke 	&target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
2364c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2365c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2366c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2367125d0119SHannes Reinecke 	&target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
2368c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_preferred.attr,
2369c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2370c66ac9dbSNicholas Bellinger 	&target_core_alua_tg_pt_gp_members.attr,
2371c66ac9dbSNicholas Bellinger 	NULL,
2372c66ac9dbSNicholas Bellinger };
2373c66ac9dbSNicholas Bellinger 
23741f6fe7cbSNicholas Bellinger static void target_core_alua_tg_pt_gp_release(struct config_item *item)
23751f6fe7cbSNicholas Bellinger {
23761f6fe7cbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
23771f6fe7cbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
23781f6fe7cbSNicholas Bellinger 
23791f6fe7cbSNicholas Bellinger 	core_alua_free_tg_pt_gp(tg_pt_gp);
23801f6fe7cbSNicholas Bellinger }
23811f6fe7cbSNicholas Bellinger 
2382c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
23831f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_tg_pt_gp_release,
2384c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_alua_tg_pt_gp_attr_show,
2385c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_alua_tg_pt_gp_attr_store,
2386c66ac9dbSNicholas Bellinger };
2387c66ac9dbSNicholas Bellinger 
2388c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2389c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
2390c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
2391c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2392c66ac9dbSNicholas Bellinger };
2393c66ac9dbSNicholas Bellinger 
2394c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2395c66ac9dbSNicholas Bellinger 
239672aca57bSNicholas Bellinger /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2397c66ac9dbSNicholas Bellinger 
2398c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_tg_pt_gp(
2399c66ac9dbSNicholas Bellinger 	struct config_group *group,
2400c66ac9dbSNicholas Bellinger 	const char *name)
2401c66ac9dbSNicholas Bellinger {
2402c66ac9dbSNicholas Bellinger 	struct t10_alua *alua = container_of(group, struct t10_alua,
2403c66ac9dbSNicholas Bellinger 					alua_tg_pt_gps_group);
2404c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2405c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = NULL;
2406c66ac9dbSNicholas Bellinger 	struct config_item *alua_tg_pt_gp_ci = NULL;
2407c66ac9dbSNicholas Bellinger 
24080fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
24096708bb27SAndy Grover 	if (!tg_pt_gp)
2410c66ac9dbSNicholas Bellinger 		return NULL;
2411c66ac9dbSNicholas Bellinger 
2412c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2413c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2414c66ac9dbSNicholas Bellinger 
2415c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
2416c66ac9dbSNicholas Bellinger 			&target_core_alua_tg_pt_gp_cit);
2417c66ac9dbSNicholas Bellinger 
24186708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2419c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s\n",
2420c66ac9dbSNicholas Bellinger 		config_item_name(alua_tg_pt_gp_ci));
2421c66ac9dbSNicholas Bellinger 
2422c66ac9dbSNicholas Bellinger 	return alua_tg_pt_gp_cg;
2423c66ac9dbSNicholas Bellinger }
2424c66ac9dbSNicholas Bellinger 
2425c66ac9dbSNicholas Bellinger static void target_core_alua_drop_tg_pt_gp(
2426c66ac9dbSNicholas Bellinger 	struct config_group *group,
2427c66ac9dbSNicholas Bellinger 	struct config_item *item)
2428c66ac9dbSNicholas Bellinger {
2429c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2430c66ac9dbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2431c66ac9dbSNicholas Bellinger 
24326708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2433c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
2434c66ac9dbSNicholas Bellinger 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
24351f6fe7cbSNicholas Bellinger 	/*
24361f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
24371f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_tg_pt_gp_release().
24381f6fe7cbSNicholas Bellinger 	 */
2439c66ac9dbSNicholas Bellinger 	config_item_put(item);
2440c66ac9dbSNicholas Bellinger }
2441c66ac9dbSNicholas Bellinger 
2442c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2443c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_tg_pt_gp,
2444c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
2445c66ac9dbSNicholas Bellinger };
2446c66ac9dbSNicholas Bellinger 
244772aca57bSNicholas Bellinger TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
2448c66ac9dbSNicholas Bellinger 
244972aca57bSNicholas Bellinger /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2450c66ac9dbSNicholas Bellinger 
2451c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_cit */
2452c66ac9dbSNicholas Bellinger 
2453c66ac9dbSNicholas Bellinger /*
2454c66ac9dbSNicholas Bellinger  * target_core_alua_cit is a ConfigFS group that lives under
2455c66ac9dbSNicholas Bellinger  * /sys/kernel/config/target/core/alua.  There are default groups
2456c66ac9dbSNicholas Bellinger  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2457c66ac9dbSNicholas Bellinger  * target_core_alua_cit in target_core_init_configfs() below.
2458c66ac9dbSNicholas Bellinger  */
2459c66ac9dbSNicholas Bellinger static struct config_item_type target_core_alua_cit = {
2460c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2461c66ac9dbSNicholas Bellinger 	.ct_attrs		= NULL,
2462c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2463c66ac9dbSNicholas Bellinger };
2464c66ac9dbSNicholas Bellinger 
2465c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_cit */
2466c66ac9dbSNicholas Bellinger 
2467d23ab570SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_stat_cit */
246812d23384SNicholas Bellinger 
246912d23384SNicholas Bellinger static struct config_group *target_core_stat_mkdir(
247012d23384SNicholas Bellinger 	struct config_group *group,
247112d23384SNicholas Bellinger 	const char *name)
247212d23384SNicholas Bellinger {
247312d23384SNicholas Bellinger 	return ERR_PTR(-ENOSYS);
247412d23384SNicholas Bellinger }
247512d23384SNicholas Bellinger 
247612d23384SNicholas Bellinger static void target_core_stat_rmdir(
247712d23384SNicholas Bellinger 	struct config_group *group,
247812d23384SNicholas Bellinger 	struct config_item *item)
247912d23384SNicholas Bellinger {
248012d23384SNicholas Bellinger 	return;
248112d23384SNicholas Bellinger }
248212d23384SNicholas Bellinger 
248312d23384SNicholas Bellinger static struct configfs_group_operations target_core_stat_group_ops = {
248412d23384SNicholas Bellinger 	.make_group		= &target_core_stat_mkdir,
248512d23384SNicholas Bellinger 	.drop_item		= &target_core_stat_rmdir,
248612d23384SNicholas Bellinger };
248712d23384SNicholas Bellinger 
2488d23ab570SNicholas Bellinger TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
248912d23384SNicholas Bellinger 
2490d23ab570SNicholas Bellinger /* End functions for struct config_item_type tb_dev_stat_cit */
249112d23384SNicholas Bellinger 
2492c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_hba_cit */
2493c66ac9dbSNicholas Bellinger 
2494c66ac9dbSNicholas Bellinger static struct config_group *target_core_make_subdev(
2495c66ac9dbSNicholas Bellinger 	struct config_group *group,
2496c66ac9dbSNicholas Bellinger 	const char *name)
2497c66ac9dbSNicholas Bellinger {
2498c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2499c66ac9dbSNicholas Bellinger 	struct se_subsystem_api *t;
2500c66ac9dbSNicholas Bellinger 	struct config_item *hba_ci = &group->cg_item;
2501c66ac9dbSNicholas Bellinger 	struct se_hba *hba = item_to_hba(hba_ci);
25020fd97ccfSChristoph Hellwig 	struct se_device *dev;
2503c66ac9dbSNicholas Bellinger 	struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
250412d23384SNicholas Bellinger 	struct config_group *dev_stat_grp = NULL;
250512d23384SNicholas Bellinger 	int errno = -ENOMEM, ret;
2506c66ac9dbSNicholas Bellinger 
250712d23384SNicholas Bellinger 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
250812d23384SNicholas Bellinger 	if (ret)
250912d23384SNicholas Bellinger 		return ERR_PTR(ret);
2510c66ac9dbSNicholas Bellinger 	/*
2511c66ac9dbSNicholas Bellinger 	 * Locate the struct se_subsystem_api from parent's struct se_hba.
2512c66ac9dbSNicholas Bellinger 	 */
2513c66ac9dbSNicholas Bellinger 	t = hba->transport;
2514c66ac9dbSNicholas Bellinger 
25150fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, name);
25160fd97ccfSChristoph Hellwig 	if (!dev)
25170fd97ccfSChristoph Hellwig 		goto out_unlock;
2518c66ac9dbSNicholas Bellinger 
25190fd97ccfSChristoph Hellwig 	dev_cg = &dev->dev_group;
2520c66ac9dbSNicholas Bellinger 
252113f6a914SSebastian Andrzej Siewior 	dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
2522c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
25236708bb27SAndy Grover 	if (!dev_cg->default_groups)
25240fd97ccfSChristoph Hellwig 		goto out_free_device;
2525c66ac9dbSNicholas Bellinger 
252673112edcSNicholas Bellinger 	config_group_init_type_name(dev_cg, name, &t->tb_cits.tb_dev_cit);
25270fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
2528f79a897eSNicholas Bellinger 			&t->tb_cits.tb_dev_attrib_cit);
25290fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_pr_group, "pr",
253091e2e39bSNicholas Bellinger 			&t->tb_cits.tb_dev_pr_cit);
25310fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
2532f8d389c6SNicholas Bellinger 			&t->tb_cits.tb_dev_wwn_cit);
25330fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
253472aca57bSNicholas Bellinger 			"alua", &t->tb_cits.tb_dev_alua_tg_pt_gps_cit);
25350fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
2536d23ab570SNicholas Bellinger 			"statistics", &t->tb_cits.tb_dev_stat_cit);
253712d23384SNicholas Bellinger 
25380fd97ccfSChristoph Hellwig 	dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
25390fd97ccfSChristoph Hellwig 	dev_cg->default_groups[1] = &dev->dev_pr_group;
25400fd97ccfSChristoph Hellwig 	dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
25410fd97ccfSChristoph Hellwig 	dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
25420fd97ccfSChristoph Hellwig 	dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
254312d23384SNicholas Bellinger 	dev_cg->default_groups[5] = NULL;
2544c66ac9dbSNicholas Bellinger 	/*
254512d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2546c66ac9dbSNicholas Bellinger 	 */
25470fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
25486708bb27SAndy Grover 	if (!tg_pt_gp)
25490fd97ccfSChristoph Hellwig 		goto out_free_dev_cg_default_groups;
25500fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2551c66ac9dbSNicholas Bellinger 
25520fd97ccfSChristoph Hellwig 	tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
255313f6a914SSebastian Andrzej Siewior 	tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2554c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
25556708bb27SAndy Grover 	if (!tg_pt_gp_cg->default_groups) {
25566708bb27SAndy Grover 		pr_err("Unable to allocate tg_pt_gp_cg->"
2557c66ac9dbSNicholas Bellinger 				"default_groups\n");
25580fd97ccfSChristoph Hellwig 		goto out_free_tg_pt_gp;
2559c66ac9dbSNicholas Bellinger 	}
2560c66ac9dbSNicholas Bellinger 
2561c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2562c66ac9dbSNicholas Bellinger 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2563c66ac9dbSNicholas Bellinger 	tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2564c66ac9dbSNicholas Bellinger 	tg_pt_gp_cg->default_groups[1] = NULL;
256512d23384SNicholas Bellinger 	/*
256612d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/statistics/ default groups
256712d23384SNicholas Bellinger 	 */
25680fd97ccfSChristoph Hellwig 	dev_stat_grp = &dev->dev_stat_grps.stat_group;
256913f6a914SSebastian Andrzej Siewior 	dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
257012d23384SNicholas Bellinger 				GFP_KERNEL);
257112d23384SNicholas Bellinger 	if (!dev_stat_grp->default_groups) {
25726708bb27SAndy Grover 		pr_err("Unable to allocate dev_stat_grp->default_groups\n");
25730fd97ccfSChristoph Hellwig 		goto out_free_tg_pt_gp_cg_default_groups;
257412d23384SNicholas Bellinger 	}
25750fd97ccfSChristoph Hellwig 	target_stat_setup_dev_default_groups(dev);
2576c66ac9dbSNicholas Bellinger 
2577c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
25780fd97ccfSChristoph Hellwig 	return dev_cg;
25790fd97ccfSChristoph Hellwig 
25800fd97ccfSChristoph Hellwig out_free_tg_pt_gp_cg_default_groups:
2581c66ac9dbSNicholas Bellinger 	kfree(tg_pt_gp_cg->default_groups);
25820fd97ccfSChristoph Hellwig out_free_tg_pt_gp:
25830fd97ccfSChristoph Hellwig 	core_alua_free_tg_pt_gp(tg_pt_gp);
25840fd97ccfSChristoph Hellwig out_free_dev_cg_default_groups:
2585c66ac9dbSNicholas Bellinger 	kfree(dev_cg->default_groups);
25860fd97ccfSChristoph Hellwig out_free_device:
25870fd97ccfSChristoph Hellwig 	target_free_device(dev);
25880fd97ccfSChristoph Hellwig out_unlock:
2589c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
259012d23384SNicholas Bellinger 	return ERR_PTR(errno);
2591c66ac9dbSNicholas Bellinger }
2592c66ac9dbSNicholas Bellinger 
2593c66ac9dbSNicholas Bellinger static void target_core_drop_subdev(
2594c66ac9dbSNicholas Bellinger 	struct config_group *group,
2595c66ac9dbSNicholas Bellinger 	struct config_item *item)
2596c66ac9dbSNicholas Bellinger {
25970fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
25980fd97ccfSChristoph Hellwig 	struct se_device *dev =
25990fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2600c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2601c66ac9dbSNicholas Bellinger 	struct config_item *df_item;
26020fd97ccfSChristoph Hellwig 	struct config_group *tg_pt_gp_cg, *dev_stat_grp;
26031f6fe7cbSNicholas Bellinger 	int i;
2604c66ac9dbSNicholas Bellinger 
26050fd97ccfSChristoph Hellwig 	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
2606c66ac9dbSNicholas Bellinger 
26071f6fe7cbSNicholas Bellinger 	mutex_lock(&hba->hba_access_mutex);
2608c66ac9dbSNicholas Bellinger 
26090fd97ccfSChristoph Hellwig 	dev_stat_grp = &dev->dev_stat_grps.stat_group;
261012d23384SNicholas Bellinger 	for (i = 0; dev_stat_grp->default_groups[i]; i++) {
261112d23384SNicholas Bellinger 		df_item = &dev_stat_grp->default_groups[i]->cg_item;
261212d23384SNicholas Bellinger 		dev_stat_grp->default_groups[i] = NULL;
261312d23384SNicholas Bellinger 		config_item_put(df_item);
261412d23384SNicholas Bellinger 	}
261512d23384SNicholas Bellinger 	kfree(dev_stat_grp->default_groups);
261612d23384SNicholas Bellinger 
26170fd97ccfSChristoph Hellwig 	tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2618c66ac9dbSNicholas Bellinger 	for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2619c66ac9dbSNicholas Bellinger 		df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2620c66ac9dbSNicholas Bellinger 		tg_pt_gp_cg->default_groups[i] = NULL;
2621c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
2622c66ac9dbSNicholas Bellinger 	}
2623c66ac9dbSNicholas Bellinger 	kfree(tg_pt_gp_cg->default_groups);
26241f6fe7cbSNicholas Bellinger 	/*
26251f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
26261f6fe7cbSNicholas Bellinger 	 * directly from target_core_alua_tg_pt_gp_release().
26271f6fe7cbSNicholas Bellinger 	 */
26280fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = NULL;
2629c66ac9dbSNicholas Bellinger 
2630c66ac9dbSNicholas Bellinger 	for (i = 0; dev_cg->default_groups[i]; i++) {
2631c66ac9dbSNicholas Bellinger 		df_item = &dev_cg->default_groups[i]->cg_item;
2632c66ac9dbSNicholas Bellinger 		dev_cg->default_groups[i] = NULL;
2633c66ac9dbSNicholas Bellinger 		config_item_put(df_item);
2634c66ac9dbSNicholas Bellinger 	}
26351f6fe7cbSNicholas Bellinger 	/*
26360fd97ccfSChristoph Hellwig 	 * se_dev is released from target_core_dev_item_ops->release()
26371f6fe7cbSNicholas Bellinger 	 */
2638c66ac9dbSNicholas Bellinger 	config_item_put(item);
2639c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
2640c66ac9dbSNicholas Bellinger }
2641c66ac9dbSNicholas Bellinger 
2642c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_hba_group_ops = {
2643c66ac9dbSNicholas Bellinger 	.make_group		= target_core_make_subdev,
2644c66ac9dbSNicholas Bellinger 	.drop_item		= target_core_drop_subdev,
2645c66ac9dbSNicholas Bellinger };
2646c66ac9dbSNicholas Bellinger 
2647c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2648c66ac9dbSNicholas Bellinger #define SE_HBA_ATTR(_name, _mode)				\
2649c66ac9dbSNicholas Bellinger static struct target_core_hba_attribute				\
2650c66ac9dbSNicholas Bellinger 		target_core_hba_##_name =			\
2651c66ac9dbSNicholas Bellinger 		__CONFIGFS_EATTR(_name, _mode,			\
2652c66ac9dbSNicholas Bellinger 		target_core_hba_show_attr_##_name,		\
2653c66ac9dbSNicholas Bellinger 		target_core_hba_store_attr_##_name);
2654c66ac9dbSNicholas Bellinger 
2655c66ac9dbSNicholas Bellinger #define SE_HBA_ATTR_RO(_name)					\
2656c66ac9dbSNicholas Bellinger static struct target_core_hba_attribute				\
2657c66ac9dbSNicholas Bellinger 		target_core_hba_##_name =			\
2658c66ac9dbSNicholas Bellinger 		__CONFIGFS_EATTR_RO(_name,			\
2659c66ac9dbSNicholas Bellinger 		target_core_hba_show_attr_##_name);
2660c66ac9dbSNicholas Bellinger 
2661c66ac9dbSNicholas Bellinger static ssize_t target_core_hba_show_attr_hba_info(
2662c66ac9dbSNicholas Bellinger 	struct se_hba *hba,
2663c66ac9dbSNicholas Bellinger 	char *page)
2664c66ac9dbSNicholas Bellinger {
2665c66ac9dbSNicholas Bellinger 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2666c66ac9dbSNicholas Bellinger 			hba->hba_id, hba->transport->name,
2667c66ac9dbSNicholas Bellinger 			TARGET_CORE_CONFIGFS_VERSION);
2668c66ac9dbSNicholas Bellinger }
2669c66ac9dbSNicholas Bellinger 
2670c66ac9dbSNicholas Bellinger SE_HBA_ATTR_RO(hba_info);
2671c66ac9dbSNicholas Bellinger 
2672c66ac9dbSNicholas Bellinger static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2673c66ac9dbSNicholas Bellinger 				char *page)
2674c66ac9dbSNicholas Bellinger {
2675c66ac9dbSNicholas Bellinger 	int hba_mode = 0;
2676c66ac9dbSNicholas Bellinger 
2677c66ac9dbSNicholas Bellinger 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2678c66ac9dbSNicholas Bellinger 		hba_mode = 1;
2679c66ac9dbSNicholas Bellinger 
2680c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", hba_mode);
2681c66ac9dbSNicholas Bellinger }
2682c66ac9dbSNicholas Bellinger 
2683c66ac9dbSNicholas Bellinger static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2684c66ac9dbSNicholas Bellinger 				const char *page, size_t count)
2685c66ac9dbSNicholas Bellinger {
2686c66ac9dbSNicholas Bellinger 	struct se_subsystem_api *transport = hba->transport;
2687c66ac9dbSNicholas Bellinger 	unsigned long mode_flag;
2688c66ac9dbSNicholas Bellinger 	int ret;
2689c66ac9dbSNicholas Bellinger 
2690c66ac9dbSNicholas Bellinger 	if (transport->pmode_enable_hba == NULL)
2691c66ac9dbSNicholas Bellinger 		return -EINVAL;
2692c66ac9dbSNicholas Bellinger 
269357103d7fSJingoo Han 	ret = kstrtoul(page, 0, &mode_flag);
2694c66ac9dbSNicholas Bellinger 	if (ret < 0) {
26956708bb27SAndy Grover 		pr_err("Unable to extract hba mode flag: %d\n", ret);
269657103d7fSJingoo Han 		return ret;
2697c66ac9dbSNicholas Bellinger 	}
2698c66ac9dbSNicholas Bellinger 
26990fd97ccfSChristoph Hellwig 	if (hba->dev_count) {
27006708bb27SAndy Grover 		pr_err("Unable to set hba_mode with active devices\n");
2701c66ac9dbSNicholas Bellinger 		return -EINVAL;
2702c66ac9dbSNicholas Bellinger 	}
2703c66ac9dbSNicholas Bellinger 
2704c66ac9dbSNicholas Bellinger 	ret = transport->pmode_enable_hba(hba, mode_flag);
2705c66ac9dbSNicholas Bellinger 	if (ret < 0)
2706c66ac9dbSNicholas Bellinger 		return -EINVAL;
2707c66ac9dbSNicholas Bellinger 	if (ret > 0)
2708c66ac9dbSNicholas Bellinger 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2709c66ac9dbSNicholas Bellinger 	else if (ret == 0)
2710c66ac9dbSNicholas Bellinger 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2711c66ac9dbSNicholas Bellinger 
2712c66ac9dbSNicholas Bellinger 	return count;
2713c66ac9dbSNicholas Bellinger }
2714c66ac9dbSNicholas Bellinger 
2715c66ac9dbSNicholas Bellinger SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2716c66ac9dbSNicholas Bellinger 
2717c66ac9dbSNicholas Bellinger CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2718c66ac9dbSNicholas Bellinger 
27191f6fe7cbSNicholas Bellinger static void target_core_hba_release(struct config_item *item)
27201f6fe7cbSNicholas Bellinger {
27211f6fe7cbSNicholas Bellinger 	struct se_hba *hba = container_of(to_config_group(item),
27221f6fe7cbSNicholas Bellinger 				struct se_hba, hba_group);
27231f6fe7cbSNicholas Bellinger 	core_delete_hba(hba);
27241f6fe7cbSNicholas Bellinger }
27251f6fe7cbSNicholas Bellinger 
2726c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_hba_attrs[] = {
2727c66ac9dbSNicholas Bellinger 	&target_core_hba_hba_info.attr,
2728c66ac9dbSNicholas Bellinger 	&target_core_hba_hba_mode.attr,
2729c66ac9dbSNicholas Bellinger 	NULL,
2730c66ac9dbSNicholas Bellinger };
2731c66ac9dbSNicholas Bellinger 
2732c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_hba_item_ops = {
27331f6fe7cbSNicholas Bellinger 	.release		= target_core_hba_release,
2734c66ac9dbSNicholas Bellinger 	.show_attribute		= target_core_hba_attr_show,
2735c66ac9dbSNicholas Bellinger 	.store_attribute	= target_core_hba_attr_store,
2736c66ac9dbSNicholas Bellinger };
2737c66ac9dbSNicholas Bellinger 
2738c66ac9dbSNicholas Bellinger static struct config_item_type target_core_hba_cit = {
2739c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_hba_item_ops,
2740c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_hba_group_ops,
2741c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_hba_attrs,
2742c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2743c66ac9dbSNicholas Bellinger };
2744c66ac9dbSNicholas Bellinger 
2745c66ac9dbSNicholas Bellinger static struct config_group *target_core_call_addhbatotarget(
2746c66ac9dbSNicholas Bellinger 	struct config_group *group,
2747c66ac9dbSNicholas Bellinger 	const char *name)
2748c66ac9dbSNicholas Bellinger {
2749c66ac9dbSNicholas Bellinger 	char *se_plugin_str, *str, *str2;
2750c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2751c66ac9dbSNicholas Bellinger 	char buf[TARGET_CORE_NAME_MAX_LEN];
2752c66ac9dbSNicholas Bellinger 	unsigned long plugin_dep_id = 0;
2753c66ac9dbSNicholas Bellinger 	int ret;
2754c66ac9dbSNicholas Bellinger 
2755c66ac9dbSNicholas Bellinger 	memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
275660d645a4SDan Carpenter 	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
27576708bb27SAndy Grover 		pr_err("Passed *name strlen(): %d exceeds"
2758c66ac9dbSNicholas Bellinger 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
2759c66ac9dbSNicholas Bellinger 			TARGET_CORE_NAME_MAX_LEN);
2760c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENAMETOOLONG);
2761c66ac9dbSNicholas Bellinger 	}
2762c66ac9dbSNicholas Bellinger 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
2763c66ac9dbSNicholas Bellinger 
2764c66ac9dbSNicholas Bellinger 	str = strstr(buf, "_");
27656708bb27SAndy Grover 	if (!str) {
27666708bb27SAndy Grover 		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
2767c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
2768c66ac9dbSNicholas Bellinger 	}
2769c66ac9dbSNicholas Bellinger 	se_plugin_str = buf;
2770c66ac9dbSNicholas Bellinger 	/*
2771c66ac9dbSNicholas Bellinger 	 * Special case for subsystem plugins that have "_" in their names.
2772c66ac9dbSNicholas Bellinger 	 * Namely rd_direct and rd_mcp..
2773c66ac9dbSNicholas Bellinger 	 */
2774c66ac9dbSNicholas Bellinger 	str2 = strstr(str+1, "_");
27756708bb27SAndy Grover 	if (str2) {
2776c66ac9dbSNicholas Bellinger 		*str2 = '\0'; /* Terminate for *se_plugin_str */
2777c66ac9dbSNicholas Bellinger 		str2++; /* Skip to start of plugin dependent ID */
2778c66ac9dbSNicholas Bellinger 		str = str2;
2779c66ac9dbSNicholas Bellinger 	} else {
2780c66ac9dbSNicholas Bellinger 		*str = '\0'; /* Terminate for *se_plugin_str */
2781c66ac9dbSNicholas Bellinger 		str++; /* Skip to start of plugin dependent ID */
2782c66ac9dbSNicholas Bellinger 	}
2783c66ac9dbSNicholas Bellinger 
278457103d7fSJingoo Han 	ret = kstrtoul(str, 0, &plugin_dep_id);
2785c66ac9dbSNicholas Bellinger 	if (ret < 0) {
278657103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2787c66ac9dbSNicholas Bellinger 				" plugin_dep_id\n", ret);
278857103d7fSJingoo Han 		return ERR_PTR(ret);
2789c66ac9dbSNicholas Bellinger 	}
2790c66ac9dbSNicholas Bellinger 	/*
2791c66ac9dbSNicholas Bellinger 	 * Load up TCM subsystem plugins if they have not already been loaded.
2792c66ac9dbSNicholas Bellinger 	 */
2793dbc5623eSNicholas Bellinger 	transport_subsystem_check_init();
2794c66ac9dbSNicholas Bellinger 
2795c66ac9dbSNicholas Bellinger 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
2796c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
2797c66ac9dbSNicholas Bellinger 		return ERR_CAST(hba);
2798c66ac9dbSNicholas Bellinger 
2799c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&hba->hba_group, name,
2800c66ac9dbSNicholas Bellinger 			&target_core_hba_cit);
2801c66ac9dbSNicholas Bellinger 
2802c66ac9dbSNicholas Bellinger 	return &hba->hba_group;
2803c66ac9dbSNicholas Bellinger }
2804c66ac9dbSNicholas Bellinger 
2805c66ac9dbSNicholas Bellinger static void target_core_call_delhbafromtarget(
2806c66ac9dbSNicholas Bellinger 	struct config_group *group,
2807c66ac9dbSNicholas Bellinger 	struct config_item *item)
2808c66ac9dbSNicholas Bellinger {
28091f6fe7cbSNicholas Bellinger 	/*
28101f6fe7cbSNicholas Bellinger 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
28111f6fe7cbSNicholas Bellinger 	 * -> target_core_hba_release()
28121f6fe7cbSNicholas Bellinger 	 */
2813c66ac9dbSNicholas Bellinger 	config_item_put(item);
2814c66ac9dbSNicholas Bellinger }
2815c66ac9dbSNicholas Bellinger 
2816c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_group_ops = {
2817c66ac9dbSNicholas Bellinger 	.make_group	= target_core_call_addhbatotarget,
2818c66ac9dbSNicholas Bellinger 	.drop_item	= target_core_call_delhbafromtarget,
2819c66ac9dbSNicholas Bellinger };
2820c66ac9dbSNicholas Bellinger 
2821c66ac9dbSNicholas Bellinger static struct config_item_type target_core_cit = {
2822c66ac9dbSNicholas Bellinger 	.ct_item_ops	= NULL,
2823c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_group_ops,
2824c66ac9dbSNicholas Bellinger 	.ct_attrs	= NULL,
2825c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
2826c66ac9dbSNicholas Bellinger };
2827c66ac9dbSNicholas Bellinger 
2828c66ac9dbSNicholas Bellinger /* Stop functions for struct config_item_type target_core_hba_cit */
2829c66ac9dbSNicholas Bellinger 
283073112edcSNicholas Bellinger void target_core_setup_sub_cits(struct se_subsystem_api *sa)
283173112edcSNicholas Bellinger {
283273112edcSNicholas Bellinger 	target_core_setup_dev_cit(sa);
2833f79a897eSNicholas Bellinger 	target_core_setup_dev_attrib_cit(sa);
283491e2e39bSNicholas Bellinger 	target_core_setup_dev_pr_cit(sa);
2835f8d389c6SNicholas Bellinger 	target_core_setup_dev_wwn_cit(sa);
283672aca57bSNicholas Bellinger 	target_core_setup_dev_alua_tg_pt_gps_cit(sa);
2837d23ab570SNicholas Bellinger 	target_core_setup_dev_stat_cit(sa);
283873112edcSNicholas Bellinger }
283973112edcSNicholas Bellinger EXPORT_SYMBOL(target_core_setup_sub_cits);
284073112edcSNicholas Bellinger 
284154550fabSAxel Lin static int __init target_core_init_configfs(void)
2842c66ac9dbSNicholas Bellinger {
2843c66ac9dbSNicholas Bellinger 	struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
2844c66ac9dbSNicholas Bellinger 	struct config_group *lu_gp_cg = NULL;
2845d588cf8fSChristoph Hellwig 	struct configfs_subsystem *subsys = &target_core_fabrics;
2846c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2847c66ac9dbSNicholas Bellinger 	int ret;
2848c66ac9dbSNicholas Bellinger 
28496708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
2850c66ac9dbSNicholas Bellinger 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
2851c66ac9dbSNicholas Bellinger 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
2852c66ac9dbSNicholas Bellinger 
2853c66ac9dbSNicholas Bellinger 	config_group_init(&subsys->su_group);
2854c66ac9dbSNicholas Bellinger 	mutex_init(&subsys->su_mutex);
2855c66ac9dbSNicholas Bellinger 
2856e3d6f909SAndy Grover 	ret = init_se_kmem_caches();
2857c66ac9dbSNicholas Bellinger 	if (ret < 0)
2858e3d6f909SAndy Grover 		return ret;
2859c66ac9dbSNicholas Bellinger 	/*
2860c66ac9dbSNicholas Bellinger 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
2861c66ac9dbSNicholas Bellinger 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
2862c66ac9dbSNicholas Bellinger 	 */
2863c66ac9dbSNicholas Bellinger 	target_cg = &subsys->su_group;
2864ab6dae82SAndy Grover 	target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2865c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
28666708bb27SAndy Grover 	if (!target_cg->default_groups) {
28676708bb27SAndy Grover 		pr_err("Unable to allocate target_cg->default_groups\n");
286837bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
2869c66ac9dbSNicholas Bellinger 		goto out_global;
2870c66ac9dbSNicholas Bellinger 	}
2871c66ac9dbSNicholas Bellinger 
2872e3d6f909SAndy Grover 	config_group_init_type_name(&target_core_hbagroup,
2873c66ac9dbSNicholas Bellinger 			"core", &target_core_cit);
2874e3d6f909SAndy Grover 	target_cg->default_groups[0] = &target_core_hbagroup;
2875c66ac9dbSNicholas Bellinger 	target_cg->default_groups[1] = NULL;
2876c66ac9dbSNicholas Bellinger 	/*
2877c66ac9dbSNicholas Bellinger 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
2878c66ac9dbSNicholas Bellinger 	 */
2879e3d6f909SAndy Grover 	hba_cg = &target_core_hbagroup;
288013f6a914SSebastian Andrzej Siewior 	hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2881c66ac9dbSNicholas Bellinger 				GFP_KERNEL);
28826708bb27SAndy Grover 	if (!hba_cg->default_groups) {
28836708bb27SAndy Grover 		pr_err("Unable to allocate hba_cg->default_groups\n");
288437bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
2885c66ac9dbSNicholas Bellinger 		goto out_global;
2886c66ac9dbSNicholas Bellinger 	}
2887e3d6f909SAndy Grover 	config_group_init_type_name(&alua_group,
2888c66ac9dbSNicholas Bellinger 			"alua", &target_core_alua_cit);
2889e3d6f909SAndy Grover 	hba_cg->default_groups[0] = &alua_group;
2890c66ac9dbSNicholas Bellinger 	hba_cg->default_groups[1] = NULL;
2891c66ac9dbSNicholas Bellinger 	/*
2892c66ac9dbSNicholas Bellinger 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
2893c66ac9dbSNicholas Bellinger 	 * groups under /sys/kernel/config/target/core/alua/
2894c66ac9dbSNicholas Bellinger 	 */
2895e3d6f909SAndy Grover 	alua_cg = &alua_group;
289613f6a914SSebastian Andrzej Siewior 	alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2897c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
28986708bb27SAndy Grover 	if (!alua_cg->default_groups) {
28996708bb27SAndy Grover 		pr_err("Unable to allocate alua_cg->default_groups\n");
290037bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
2901c66ac9dbSNicholas Bellinger 		goto out_global;
2902c66ac9dbSNicholas Bellinger 	}
2903c66ac9dbSNicholas Bellinger 
2904e3d6f909SAndy Grover 	config_group_init_type_name(&alua_lu_gps_group,
2905c66ac9dbSNicholas Bellinger 			"lu_gps", &target_core_alua_lu_gps_cit);
2906e3d6f909SAndy Grover 	alua_cg->default_groups[0] = &alua_lu_gps_group;
2907c66ac9dbSNicholas Bellinger 	alua_cg->default_groups[1] = NULL;
2908c66ac9dbSNicholas Bellinger 	/*
2909c66ac9dbSNicholas Bellinger 	 * Add core/alua/lu_gps/default_lu_gp
2910c66ac9dbSNicholas Bellinger 	 */
2911c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
291237bb7899SPeter Senna Tschudin 	if (IS_ERR(lu_gp)) {
291337bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
2914c66ac9dbSNicholas Bellinger 		goto out_global;
291537bb7899SPeter Senna Tschudin 	}
2916c66ac9dbSNicholas Bellinger 
2917e3d6f909SAndy Grover 	lu_gp_cg = &alua_lu_gps_group;
291813f6a914SSebastian Andrzej Siewior 	lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2919c66ac9dbSNicholas Bellinger 			GFP_KERNEL);
29206708bb27SAndy Grover 	if (!lu_gp_cg->default_groups) {
29216708bb27SAndy Grover 		pr_err("Unable to allocate lu_gp_cg->default_groups\n");
292237bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
2923c66ac9dbSNicholas Bellinger 		goto out_global;
2924c66ac9dbSNicholas Bellinger 	}
2925c66ac9dbSNicholas Bellinger 
2926c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
2927c66ac9dbSNicholas Bellinger 				&target_core_alua_lu_gp_cit);
2928c66ac9dbSNicholas Bellinger 	lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
2929c66ac9dbSNicholas Bellinger 	lu_gp_cg->default_groups[1] = NULL;
2930e3d6f909SAndy Grover 	default_lu_gp = lu_gp;
2931c66ac9dbSNicholas Bellinger 	/*
2932c66ac9dbSNicholas Bellinger 	 * Register the target_core_mod subsystem with configfs.
2933c66ac9dbSNicholas Bellinger 	 */
2934c66ac9dbSNicholas Bellinger 	ret = configfs_register_subsystem(subsys);
2935c66ac9dbSNicholas Bellinger 	if (ret < 0) {
29366708bb27SAndy Grover 		pr_err("Error %d while registering subsystem %s\n",
2937c66ac9dbSNicholas Bellinger 			ret, subsys->su_group.cg_item.ci_namebuf);
2938c66ac9dbSNicholas Bellinger 		goto out_global;
2939c66ac9dbSNicholas Bellinger 	}
29406708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
2941c66ac9dbSNicholas Bellinger 		" Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
2942c66ac9dbSNicholas Bellinger 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
2943c66ac9dbSNicholas Bellinger 	/*
2944c66ac9dbSNicholas Bellinger 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
2945c66ac9dbSNicholas Bellinger 	 */
2946c66ac9dbSNicholas Bellinger 	ret = rd_module_init();
2947c66ac9dbSNicholas Bellinger 	if (ret < 0)
2948c66ac9dbSNicholas Bellinger 		goto out;
2949c66ac9dbSNicholas Bellinger 
29500d0f9dfbSRoland Dreier 	ret = core_dev_setup_virtual_lun0();
29510d0f9dfbSRoland Dreier 	if (ret < 0)
2952c66ac9dbSNicholas Bellinger 		goto out;
2953c66ac9dbSNicholas Bellinger 
2954f99715acSNicholas Bellinger 	ret = target_xcopy_setup_pt();
2955f99715acSNicholas Bellinger 	if (ret < 0)
2956f99715acSNicholas Bellinger 		goto out;
2957f99715acSNicholas Bellinger 
2958c66ac9dbSNicholas Bellinger 	return 0;
2959c66ac9dbSNicholas Bellinger 
2960c66ac9dbSNicholas Bellinger out:
2961c66ac9dbSNicholas Bellinger 	configfs_unregister_subsystem(subsys);
2962c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
2963c66ac9dbSNicholas Bellinger 	rd_module_exit();
2964c66ac9dbSNicholas Bellinger out_global:
2965e3d6f909SAndy Grover 	if (default_lu_gp) {
2966e3d6f909SAndy Grover 		core_alua_free_lu_gp(default_lu_gp);
2967e3d6f909SAndy Grover 		default_lu_gp = NULL;
2968c66ac9dbSNicholas Bellinger 	}
2969c66ac9dbSNicholas Bellinger 	if (lu_gp_cg)
2970c66ac9dbSNicholas Bellinger 		kfree(lu_gp_cg->default_groups);
2971c66ac9dbSNicholas Bellinger 	if (alua_cg)
2972c66ac9dbSNicholas Bellinger 		kfree(alua_cg->default_groups);
2973c66ac9dbSNicholas Bellinger 	if (hba_cg)
2974c66ac9dbSNicholas Bellinger 		kfree(hba_cg->default_groups);
2975c66ac9dbSNicholas Bellinger 	kfree(target_cg->default_groups);
2976e3d6f909SAndy Grover 	release_se_kmem_caches();
2977e3d6f909SAndy Grover 	return ret;
2978c66ac9dbSNicholas Bellinger }
2979c66ac9dbSNicholas Bellinger 
298054550fabSAxel Lin static void __exit target_core_exit_configfs(void)
2981c66ac9dbSNicholas Bellinger {
2982c66ac9dbSNicholas Bellinger 	struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
2983c66ac9dbSNicholas Bellinger 	struct config_item *item;
2984c66ac9dbSNicholas Bellinger 	int i;
2985c66ac9dbSNicholas Bellinger 
2986e3d6f909SAndy Grover 	lu_gp_cg = &alua_lu_gps_group;
2987c66ac9dbSNicholas Bellinger 	for (i = 0; lu_gp_cg->default_groups[i]; i++) {
2988c66ac9dbSNicholas Bellinger 		item = &lu_gp_cg->default_groups[i]->cg_item;
2989c66ac9dbSNicholas Bellinger 		lu_gp_cg->default_groups[i] = NULL;
2990c66ac9dbSNicholas Bellinger 		config_item_put(item);
2991c66ac9dbSNicholas Bellinger 	}
2992c66ac9dbSNicholas Bellinger 	kfree(lu_gp_cg->default_groups);
29937c2bf6e9SNicholas Bellinger 	lu_gp_cg->default_groups = NULL;
2994c66ac9dbSNicholas Bellinger 
2995e3d6f909SAndy Grover 	alua_cg = &alua_group;
2996c66ac9dbSNicholas Bellinger 	for (i = 0; alua_cg->default_groups[i]; i++) {
2997c66ac9dbSNicholas Bellinger 		item = &alua_cg->default_groups[i]->cg_item;
2998c66ac9dbSNicholas Bellinger 		alua_cg->default_groups[i] = NULL;
2999c66ac9dbSNicholas Bellinger 		config_item_put(item);
3000c66ac9dbSNicholas Bellinger 	}
3001c66ac9dbSNicholas Bellinger 	kfree(alua_cg->default_groups);
30027c2bf6e9SNicholas Bellinger 	alua_cg->default_groups = NULL;
3003c66ac9dbSNicholas Bellinger 
3004e3d6f909SAndy Grover 	hba_cg = &target_core_hbagroup;
3005c66ac9dbSNicholas Bellinger 	for (i = 0; hba_cg->default_groups[i]; i++) {
3006c66ac9dbSNicholas Bellinger 		item = &hba_cg->default_groups[i]->cg_item;
3007c66ac9dbSNicholas Bellinger 		hba_cg->default_groups[i] = NULL;
3008c66ac9dbSNicholas Bellinger 		config_item_put(item);
3009c66ac9dbSNicholas Bellinger 	}
3010c66ac9dbSNicholas Bellinger 	kfree(hba_cg->default_groups);
30117c2bf6e9SNicholas Bellinger 	hba_cg->default_groups = NULL;
30127c2bf6e9SNicholas Bellinger 	/*
30137c2bf6e9SNicholas Bellinger 	 * We expect subsys->su_group.default_groups to be released
30147c2bf6e9SNicholas Bellinger 	 * by configfs subsystem provider logic..
30157c2bf6e9SNicholas Bellinger 	 */
3016d588cf8fSChristoph Hellwig 	configfs_unregister_subsystem(&target_core_fabrics);
3017d588cf8fSChristoph Hellwig 	kfree(target_core_fabrics.su_group.default_groups);
3018c66ac9dbSNicholas Bellinger 
3019e3d6f909SAndy Grover 	core_alua_free_lu_gp(default_lu_gp);
3020e3d6f909SAndy Grover 	default_lu_gp = NULL;
30217c2bf6e9SNicholas Bellinger 
30226708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3023c66ac9dbSNicholas Bellinger 			" Infrastructure\n");
3024c66ac9dbSNicholas Bellinger 
3025c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3026c66ac9dbSNicholas Bellinger 	rd_module_exit();
3027f99715acSNicholas Bellinger 	target_xcopy_release_pt();
3028e3d6f909SAndy Grover 	release_se_kmem_caches();
3029c66ac9dbSNicholas Bellinger }
3030c66ac9dbSNicholas Bellinger 
3031c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3032c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3033c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
3034c66ac9dbSNicholas Bellinger 
3035c66ac9dbSNicholas Bellinger module_init(target_core_init_configfs);
3036c66ac9dbSNicholas Bellinger module_exit(target_core_exit_configfs);
3037