1c66ac9dbSNicholas Bellinger /*******************************************************************************
2c66ac9dbSNicholas Bellinger  * Filename:  target_core_configfs.c
3c66ac9dbSNicholas Bellinger  *
4c66ac9dbSNicholas Bellinger  * This file contains ConfigFS logic for the Generic Target Engine project.
5c66ac9dbSNicholas Bellinger  *
64c76251eSNicholas Bellinger  * (c) Copyright 2008-2013 Datera, Inc.
7c66ac9dbSNicholas Bellinger  *
8c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
9c66ac9dbSNicholas Bellinger  *
10c66ac9dbSNicholas Bellinger  * based on configfs Copyright (C) 2005 Oracle.  All rights reserved.
11c66ac9dbSNicholas Bellinger  *
12c66ac9dbSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
13c66ac9dbSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
14c66ac9dbSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
15c66ac9dbSNicholas Bellinger  * (at your option) any later version.
16c66ac9dbSNicholas Bellinger  *
17c66ac9dbSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
18c66ac9dbSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19c66ac9dbSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c66ac9dbSNicholas Bellinger  * GNU General Public License for more details.
21c66ac9dbSNicholas Bellinger  ****************************************************************************/
22c66ac9dbSNicholas Bellinger 
23c66ac9dbSNicholas Bellinger #include <linux/module.h>
24c66ac9dbSNicholas Bellinger #include <linux/moduleparam.h>
25c66ac9dbSNicholas Bellinger #include <generated/utsrelease.h>
26c66ac9dbSNicholas Bellinger #include <linux/utsname.h>
27c66ac9dbSNicholas Bellinger #include <linux/init.h>
28c66ac9dbSNicholas Bellinger #include <linux/fs.h>
29c66ac9dbSNicholas Bellinger #include <linux/namei.h>
30c66ac9dbSNicholas Bellinger #include <linux/slab.h>
31c66ac9dbSNicholas Bellinger #include <linux/types.h>
32c66ac9dbSNicholas Bellinger #include <linux/delay.h>
33c66ac9dbSNicholas Bellinger #include <linux/unistd.h>
34c66ac9dbSNicholas Bellinger #include <linux/string.h>
35c66ac9dbSNicholas Bellinger #include <linux/parser.h>
36c66ac9dbSNicholas Bellinger #include <linux/syscalls.h>
37c66ac9dbSNicholas Bellinger #include <linux/configfs.h>
38e3d6f909SAndy Grover #include <linux/spinlock.h>
39c66ac9dbSNicholas Bellinger 
40c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
41c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
42c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
43c66ac9dbSNicholas Bellinger 
44e26d99aeSChristoph Hellwig #include "target_core_internal.h"
45c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
46c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
47c66ac9dbSNicholas Bellinger #include "target_core_rd.h"
48f99715acSNicholas Bellinger #include "target_core_xcopy.h"
49c66ac9dbSNicholas Bellinger 
5073112edcSNicholas Bellinger #define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs)		\
510a06d430SChristoph Hellwig static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
5273112edcSNicholas Bellinger {									\
530a06d430SChristoph Hellwig 	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
5473112edcSNicholas Bellinger 									\
5573112edcSNicholas Bellinger 	cit->ct_item_ops = _item_ops;					\
5673112edcSNicholas Bellinger 	cit->ct_group_ops = _group_ops;					\
5773112edcSNicholas Bellinger 	cit->ct_attrs = _attrs;						\
580a06d430SChristoph Hellwig 	cit->ct_owner = tb->ops->owner;					\
590a06d430SChristoph Hellwig 	pr_debug("Setup generic %s\n", __stringify(_name));		\
600a06d430SChristoph Hellwig }
610a06d430SChristoph Hellwig 
620a06d430SChristoph Hellwig #define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops)			\
630a06d430SChristoph Hellwig static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
640a06d430SChristoph Hellwig {									\
650a06d430SChristoph Hellwig 	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
660a06d430SChristoph Hellwig 									\
670a06d430SChristoph Hellwig 	cit->ct_item_ops = _item_ops;					\
680a06d430SChristoph Hellwig 	cit->ct_group_ops = _group_ops;					\
690a06d430SChristoph Hellwig 	cit->ct_attrs = tb->ops->tb_##_name##_attrs;			\
700a06d430SChristoph Hellwig 	cit->ct_owner = tb->ops->owner;					\
7173112edcSNicholas Bellinger 	pr_debug("Setup generic %s\n", __stringify(_name));		\
7273112edcSNicholas Bellinger }
7373112edcSNicholas Bellinger 
74e3d6f909SAndy Grover extern struct t10_alua_lu_gp *default_lu_gp;
75e3d6f909SAndy Grover 
76d0f474e5SRoland Dreier static LIST_HEAD(g_tf_list);
77d0f474e5SRoland Dreier static DEFINE_MUTEX(g_tf_lock);
78c66ac9dbSNicholas Bellinger 
79e3d6f909SAndy Grover static struct config_group target_core_hbagroup;
80e3d6f909SAndy Grover static struct config_group alua_group;
81e3d6f909SAndy Grover static struct config_group alua_lu_gps_group;
82e3d6f909SAndy Grover 
83c66ac9dbSNicholas Bellinger static inline struct se_hba *
84c66ac9dbSNicholas Bellinger item_to_hba(struct config_item *item)
85c66ac9dbSNicholas Bellinger {
86c66ac9dbSNicholas Bellinger 	return container_of(to_config_group(item), struct se_hba, hba_group);
87c66ac9dbSNicholas Bellinger }
88c66ac9dbSNicholas Bellinger 
89c66ac9dbSNicholas Bellinger /*
90c66ac9dbSNicholas Bellinger  * Attributes for /sys/kernel/config/target/
91c66ac9dbSNicholas Bellinger  */
922eafd729SChristoph Hellwig static ssize_t target_core_item_version_show(struct config_item *item,
93c66ac9dbSNicholas Bellinger 		char *page)
94c66ac9dbSNicholas Bellinger {
95c66ac9dbSNicholas Bellinger 	return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
96ce8dd25dSChristoph Hellwig 		" on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
97c66ac9dbSNicholas Bellinger 		utsname()->sysname, utsname()->machine);
98c66ac9dbSNicholas Bellinger }
99c66ac9dbSNicholas Bellinger 
1002eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_core_item_, version);
101c66ac9dbSNicholas Bellinger 
102a96e9783SLee Duncan char db_root[DB_ROOT_LEN] = DB_ROOT_DEFAULT;
103a96e9783SLee Duncan static char db_root_stage[DB_ROOT_LEN];
104a96e9783SLee Duncan 
105a96e9783SLee Duncan static ssize_t target_core_item_dbroot_show(struct config_item *item,
106a96e9783SLee Duncan 					    char *page)
107a96e9783SLee Duncan {
108a96e9783SLee Duncan 	return sprintf(page, "%s\n", db_root);
109a96e9783SLee Duncan }
110a96e9783SLee Duncan 
111a96e9783SLee Duncan static ssize_t target_core_item_dbroot_store(struct config_item *item,
112a96e9783SLee Duncan 					const char *page, size_t count)
113a96e9783SLee Duncan {
114a96e9783SLee Duncan 	ssize_t read_bytes;
115a96e9783SLee Duncan 	struct file *fp;
116a96e9783SLee Duncan 
117a96e9783SLee Duncan 	mutex_lock(&g_tf_lock);
118a96e9783SLee Duncan 	if (!list_empty(&g_tf_list)) {
119a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
120a96e9783SLee Duncan 		pr_err("db_root: cannot be changed: target drivers registered");
121a96e9783SLee Duncan 		return -EINVAL;
122a96e9783SLee Duncan 	}
123a96e9783SLee Duncan 
124a96e9783SLee Duncan 	if (count > (DB_ROOT_LEN - 1)) {
125a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
126a96e9783SLee Duncan 		pr_err("db_root: count %d exceeds DB_ROOT_LEN-1: %u\n",
127a96e9783SLee Duncan 		       (int)count, DB_ROOT_LEN - 1);
128a96e9783SLee Duncan 		return -EINVAL;
129a96e9783SLee Duncan 	}
130a96e9783SLee Duncan 
131a96e9783SLee Duncan 	read_bytes = snprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
132a96e9783SLee Duncan 	if (!read_bytes) {
133a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
134a96e9783SLee Duncan 		return -EINVAL;
135a96e9783SLee Duncan 	}
136a96e9783SLee Duncan 	if (db_root_stage[read_bytes - 1] == '\n')
137a96e9783SLee Duncan 		db_root_stage[read_bytes - 1] = '\0';
138a96e9783SLee Duncan 
139a96e9783SLee Duncan 	/* validate new db root before accepting it */
140a96e9783SLee Duncan 	fp = filp_open(db_root_stage, O_RDONLY, 0);
141a96e9783SLee Duncan 	if (IS_ERR(fp)) {
142a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
143a96e9783SLee Duncan 		pr_err("db_root: cannot open: %s\n", db_root_stage);
144a96e9783SLee Duncan 		return -EINVAL;
145a96e9783SLee Duncan 	}
14645063097SAl Viro 	if (!S_ISDIR(file_inode(fp)->i_mode)) {
1478cc3bb07SBart Van Assche 		filp_close(fp, NULL);
148a96e9783SLee Duncan 		mutex_unlock(&g_tf_lock);
149a96e9783SLee Duncan 		pr_err("db_root: not a directory: %s\n", db_root_stage);
150a96e9783SLee Duncan 		return -EINVAL;
151a96e9783SLee Duncan 	}
1528cc3bb07SBart Van Assche 	filp_close(fp, NULL);
153a96e9783SLee Duncan 
154a96e9783SLee Duncan 	strncpy(db_root, db_root_stage, read_bytes);
155a96e9783SLee Duncan 
156a96e9783SLee Duncan 	mutex_unlock(&g_tf_lock);
157a96e9783SLee Duncan 
15878a6295cSLee Duncan 	pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
15978a6295cSLee Duncan 
160a96e9783SLee Duncan 	return read_bytes;
161a96e9783SLee Duncan }
162a96e9783SLee Duncan 
163a96e9783SLee Duncan CONFIGFS_ATTR(target_core_item_, dbroot);
164a96e9783SLee Duncan 
165c66ac9dbSNicholas Bellinger static struct target_fabric_configfs *target_core_get_fabric(
166c66ac9dbSNicholas Bellinger 	const char *name)
167c66ac9dbSNicholas Bellinger {
168c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
169c66ac9dbSNicholas Bellinger 
1706708bb27SAndy Grover 	if (!name)
171c66ac9dbSNicholas Bellinger 		return NULL;
172c66ac9dbSNicholas Bellinger 
173c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
174c66ac9dbSNicholas Bellinger 	list_for_each_entry(tf, &g_tf_list, tf_list) {
1750dc2e8d1SChristoph Hellwig 		if (!strcmp(tf->tf_ops->name, name)) {
176c66ac9dbSNicholas Bellinger 			atomic_inc(&tf->tf_access_cnt);
177c66ac9dbSNicholas Bellinger 			mutex_unlock(&g_tf_lock);
178c66ac9dbSNicholas Bellinger 			return tf;
179c66ac9dbSNicholas Bellinger 		}
180c66ac9dbSNicholas Bellinger 	}
181c66ac9dbSNicholas Bellinger 	mutex_unlock(&g_tf_lock);
182c66ac9dbSNicholas Bellinger 
183c66ac9dbSNicholas Bellinger 	return NULL;
184c66ac9dbSNicholas Bellinger }
185c66ac9dbSNicholas Bellinger 
186c66ac9dbSNicholas Bellinger /*
187c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->make_group()
188c66ac9dbSNicholas Bellinger  */
189c66ac9dbSNicholas Bellinger static struct config_group *target_core_register_fabric(
190c66ac9dbSNicholas Bellinger 	struct config_group *group,
191c66ac9dbSNicholas Bellinger 	const char *name)
192c66ac9dbSNicholas Bellinger {
193c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf;
194c66ac9dbSNicholas Bellinger 	int ret;
195c66ac9dbSNicholas Bellinger 
1966708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
197c66ac9dbSNicholas Bellinger 			" %s\n", group, name);
198e7b7af6eSRoland Dreier 
199e7b7af6eSRoland Dreier 	tf = target_core_get_fabric(name);
200e7b7af6eSRoland Dreier 	if (!tf) {
20162554910SNicholas Bellinger 		pr_debug("target_core_register_fabric() trying autoload for %s\n",
202e7b7af6eSRoland Dreier 			 name);
203e7b7af6eSRoland Dreier 
204c66ac9dbSNicholas Bellinger 		/*
205c66ac9dbSNicholas Bellinger 		 * Below are some hardcoded request_module() calls to automatically
206c66ac9dbSNicholas Bellinger 		 * local fabric modules when the following is called:
207c66ac9dbSNicholas Bellinger 		 *
208c66ac9dbSNicholas Bellinger 		 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
209c66ac9dbSNicholas Bellinger 		 *
210c66ac9dbSNicholas Bellinger 		 * Note that this does not limit which TCM fabric module can be
211c66ac9dbSNicholas Bellinger 		 * registered, but simply provids auto loading logic for modules with
212c66ac9dbSNicholas Bellinger 		 * mkdir(2) system calls with known TCM fabric modules.
213c66ac9dbSNicholas Bellinger 		 */
214e7b7af6eSRoland Dreier 
2156708bb27SAndy Grover 		if (!strncmp(name, "iscsi", 5)) {
216c66ac9dbSNicholas Bellinger 			/*
217c66ac9dbSNicholas Bellinger 			 * Automatically load the LIO Target fabric module when the
218c66ac9dbSNicholas Bellinger 			 * following is called:
219c66ac9dbSNicholas Bellinger 			 *
220c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/iscsi
221c66ac9dbSNicholas Bellinger 			 */
222c66ac9dbSNicholas Bellinger 			ret = request_module("iscsi_target_mod");
223c66ac9dbSNicholas Bellinger 			if (ret < 0) {
22462554910SNicholas Bellinger 				pr_debug("request_module() failed for"
225c66ac9dbSNicholas Bellinger 				         " iscsi_target_mod.ko: %d\n", ret);
226c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
227c66ac9dbSNicholas Bellinger 			}
2286708bb27SAndy Grover 		} else if (!strncmp(name, "loopback", 8)) {
229c66ac9dbSNicholas Bellinger 			/*
230c66ac9dbSNicholas Bellinger 			 * Automatically load the tcm_loop fabric module when the
231c66ac9dbSNicholas Bellinger 			 * following is called:
232c66ac9dbSNicholas Bellinger 			 *
233c66ac9dbSNicholas Bellinger 			 * mkdir -p $CONFIGFS/target/loopback
234c66ac9dbSNicholas Bellinger 			 */
235c66ac9dbSNicholas Bellinger 			ret = request_module("tcm_loop");
236c66ac9dbSNicholas Bellinger 			if (ret < 0) {
23762554910SNicholas Bellinger 				pr_debug("request_module() failed for"
238c66ac9dbSNicholas Bellinger 				         " tcm_loop.ko: %d\n", ret);
239c66ac9dbSNicholas Bellinger 				return ERR_PTR(-EINVAL);
240c66ac9dbSNicholas Bellinger 			}
241c66ac9dbSNicholas Bellinger 		}
242c66ac9dbSNicholas Bellinger 
243c66ac9dbSNicholas Bellinger 		tf = target_core_get_fabric(name);
244e7b7af6eSRoland Dreier 	}
245e7b7af6eSRoland Dreier 
2466708bb27SAndy Grover 	if (!tf) {
24762554910SNicholas Bellinger 		pr_debug("target_core_get_fabric() failed for %s\n",
248c66ac9dbSNicholas Bellinger 		         name);
249c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
250c66ac9dbSNicholas Bellinger 	}
2516708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
2520dc2e8d1SChristoph Hellwig 			" %s\n", tf->tf_ops->name);
253c66ac9dbSNicholas Bellinger 	/*
254c66ac9dbSNicholas Bellinger 	 * On a successful target_core_get_fabric() look, the returned
255c66ac9dbSNicholas Bellinger 	 * struct target_fabric_configfs *tf will contain a usage reference.
256c66ac9dbSNicholas Bellinger 	 */
2576708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
258968ebe75SChristoph Hellwig 			&tf->tf_wwn_cit);
259c66ac9dbSNicholas Bellinger 
260968ebe75SChristoph Hellwig 	config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
2611ae1602dSChristoph Hellwig 
262c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
263968ebe75SChristoph Hellwig 			&tf->tf_discovery_cit);
2641ae1602dSChristoph Hellwig 	configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
265c66ac9dbSNicholas Bellinger 
2666f3bf5a2SBart Van Assche 	pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric: %s\n",
2676f3bf5a2SBart Van Assche 		 config_item_name(&tf->tf_group.cg_item));
268c66ac9dbSNicholas Bellinger 	return &tf->tf_group;
269c66ac9dbSNicholas Bellinger }
270c66ac9dbSNicholas Bellinger 
271c66ac9dbSNicholas Bellinger /*
272c66ac9dbSNicholas Bellinger  * Called from struct target_core_group_ops->drop_item()
273c66ac9dbSNicholas Bellinger  */
274c66ac9dbSNicholas Bellinger static void target_core_deregister_fabric(
275c66ac9dbSNicholas Bellinger 	struct config_group *group,
276c66ac9dbSNicholas Bellinger 	struct config_item *item)
277c66ac9dbSNicholas Bellinger {
278c66ac9dbSNicholas Bellinger 	struct target_fabric_configfs *tf = container_of(
279c66ac9dbSNicholas Bellinger 		to_config_group(item), struct target_fabric_configfs, tf_group);
280c66ac9dbSNicholas Bellinger 
2816708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
282c66ac9dbSNicholas Bellinger 		" tf list\n", config_item_name(item));
283c66ac9dbSNicholas Bellinger 
2846708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
2850dc2e8d1SChristoph Hellwig 			" %s\n", tf->tf_ops->name);
286c66ac9dbSNicholas Bellinger 	atomic_dec(&tf->tf_access_cnt);
287c66ac9dbSNicholas Bellinger 
2886708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
289c66ac9dbSNicholas Bellinger 			" %s\n", config_item_name(item));
290c66ac9dbSNicholas Bellinger 
2911ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&tf->tf_group);
292c66ac9dbSNicholas Bellinger 	config_item_put(item);
293c66ac9dbSNicholas Bellinger }
294c66ac9dbSNicholas Bellinger 
295c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_fabric_group_ops = {
296c66ac9dbSNicholas Bellinger 	.make_group	= &target_core_register_fabric,
297c66ac9dbSNicholas Bellinger 	.drop_item	= &target_core_deregister_fabric,
298c66ac9dbSNicholas Bellinger };
299c66ac9dbSNicholas Bellinger 
300c66ac9dbSNicholas Bellinger /*
301c66ac9dbSNicholas Bellinger  * All item attributes appearing in /sys/kernel/target/ appear here.
302c66ac9dbSNicholas Bellinger  */
303c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_fabric_item_attrs[] = {
304c66ac9dbSNicholas Bellinger 	&target_core_item_attr_version,
305a96e9783SLee Duncan 	&target_core_item_attr_dbroot,
306c66ac9dbSNicholas Bellinger 	NULL,
307c66ac9dbSNicholas Bellinger };
308c66ac9dbSNicholas Bellinger 
309c66ac9dbSNicholas Bellinger /*
310c66ac9dbSNicholas Bellinger  * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
311c66ac9dbSNicholas Bellinger  */
312ece550b5SBhumika Goyal static const struct config_item_type target_core_fabrics_item = {
313c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_fabric_group_ops,
314c66ac9dbSNicholas Bellinger 	.ct_attrs	= target_core_fabric_item_attrs,
315c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
316c66ac9dbSNicholas Bellinger };
317c66ac9dbSNicholas Bellinger 
318c66ac9dbSNicholas Bellinger static struct configfs_subsystem target_core_fabrics = {
319c66ac9dbSNicholas Bellinger 	.su_group = {
320c66ac9dbSNicholas Bellinger 		.cg_item = {
321c66ac9dbSNicholas Bellinger 			.ci_namebuf = "target",
322c66ac9dbSNicholas Bellinger 			.ci_type = &target_core_fabrics_item,
323c66ac9dbSNicholas Bellinger 		},
324c66ac9dbSNicholas Bellinger 	},
325c66ac9dbSNicholas Bellinger };
326c66ac9dbSNicholas Bellinger 
327d588cf8fSChristoph Hellwig int target_depend_item(struct config_item *item)
328d588cf8fSChristoph Hellwig {
329d588cf8fSChristoph Hellwig 	return configfs_depend_item(&target_core_fabrics, item);
330d588cf8fSChristoph Hellwig }
331d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_depend_item);
332d588cf8fSChristoph Hellwig 
333d588cf8fSChristoph Hellwig void target_undepend_item(struct config_item *item)
334d588cf8fSChristoph Hellwig {
3359a9e3415SKrzysztof Opasiak 	return configfs_undepend_item(item);
336d588cf8fSChristoph Hellwig }
337d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_undepend_item);
338c66ac9dbSNicholas Bellinger 
339c66ac9dbSNicholas Bellinger /*##############################################################################
340c66ac9dbSNicholas Bellinger // Start functions called by external Target Fabrics Modules
341c66ac9dbSNicholas Bellinger //############################################################################*/
342c66ac9dbSNicholas Bellinger 
3439ac8928eSChristoph Hellwig static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
344c66ac9dbSNicholas Bellinger {
3459ac8928eSChristoph Hellwig 	if (!tfo->name) {
3469ac8928eSChristoph Hellwig 		pr_err("Missing tfo->name\n");
3479ac8928eSChristoph Hellwig 		return -EINVAL;
348c66ac9dbSNicholas Bellinger 	}
3499ac8928eSChristoph Hellwig 	if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
3506708bb27SAndy Grover 		pr_err("Passed name: %s exceeds TARGET_FABRIC"
3519ac8928eSChristoph Hellwig 			"_NAME_SIZE\n", tfo->name);
3529ac8928eSChristoph Hellwig 		return -EINVAL;
353c66ac9dbSNicholas Bellinger 	}
35430c7ca93SDavid Disseldorp 	if (!tfo->fabric_name) {
35530c7ca93SDavid Disseldorp 		pr_err("Missing tfo->fabric_name\n");
356c66ac9dbSNicholas Bellinger 		return -EINVAL;
357c66ac9dbSNicholas Bellinger 	}
3586708bb27SAndy Grover 	if (!tfo->tpg_get_wwn) {
3596708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_wwn()\n");
360c66ac9dbSNicholas Bellinger 		return -EINVAL;
361c66ac9dbSNicholas Bellinger 	}
3626708bb27SAndy Grover 	if (!tfo->tpg_get_tag) {
3636708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_tag()\n");
364c66ac9dbSNicholas Bellinger 		return -EINVAL;
365c66ac9dbSNicholas Bellinger 	}
3666708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode) {
3676708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode()\n");
368c66ac9dbSNicholas Bellinger 		return -EINVAL;
369c66ac9dbSNicholas Bellinger 	}
3706708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_cache) {
3716708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
372c66ac9dbSNicholas Bellinger 		return -EINVAL;
373c66ac9dbSNicholas Bellinger 	}
3746708bb27SAndy Grover 	if (!tfo->tpg_check_demo_mode_write_protect) {
3756708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
376c66ac9dbSNicholas Bellinger 		return -EINVAL;
377c66ac9dbSNicholas Bellinger 	}
3786708bb27SAndy Grover 	if (!tfo->tpg_check_prod_mode_write_protect) {
3796708bb27SAndy Grover 		pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
380c66ac9dbSNicholas Bellinger 		return -EINVAL;
381c66ac9dbSNicholas Bellinger 	}
3826708bb27SAndy Grover 	if (!tfo->tpg_get_inst_index) {
3836708bb27SAndy Grover 		pr_err("Missing tfo->tpg_get_inst_index()\n");
384c66ac9dbSNicholas Bellinger 		return -EINVAL;
385c66ac9dbSNicholas Bellinger 	}
38635462975SChristoph Hellwig 	if (!tfo->release_cmd) {
3876708bb27SAndy Grover 		pr_err("Missing tfo->release_cmd()\n");
388c66ac9dbSNicholas Bellinger 		return -EINVAL;
389c66ac9dbSNicholas Bellinger 	}
3906708bb27SAndy Grover 	if (!tfo->sess_get_index) {
3916708bb27SAndy Grover 		pr_err("Missing tfo->sess_get_index()\n");
392c66ac9dbSNicholas Bellinger 		return -EINVAL;
393c66ac9dbSNicholas Bellinger 	}
3946708bb27SAndy Grover 	if (!tfo->write_pending) {
3956708bb27SAndy Grover 		pr_err("Missing tfo->write_pending()\n");
396c66ac9dbSNicholas Bellinger 		return -EINVAL;
397c66ac9dbSNicholas Bellinger 	}
3986708bb27SAndy Grover 	if (!tfo->write_pending_status) {
3996708bb27SAndy Grover 		pr_err("Missing tfo->write_pending_status()\n");
400c66ac9dbSNicholas Bellinger 		return -EINVAL;
401c66ac9dbSNicholas Bellinger 	}
4026708bb27SAndy Grover 	if (!tfo->set_default_node_attributes) {
4036708bb27SAndy Grover 		pr_err("Missing tfo->set_default_node_attributes()\n");
404c66ac9dbSNicholas Bellinger 		return -EINVAL;
405c66ac9dbSNicholas Bellinger 	}
4066708bb27SAndy Grover 	if (!tfo->get_cmd_state) {
4076708bb27SAndy Grover 		pr_err("Missing tfo->get_cmd_state()\n");
408c66ac9dbSNicholas Bellinger 		return -EINVAL;
409c66ac9dbSNicholas Bellinger 	}
4106708bb27SAndy Grover 	if (!tfo->queue_data_in) {
4116708bb27SAndy Grover 		pr_err("Missing tfo->queue_data_in()\n");
412c66ac9dbSNicholas Bellinger 		return -EINVAL;
413c66ac9dbSNicholas Bellinger 	}
4146708bb27SAndy Grover 	if (!tfo->queue_status) {
4156708bb27SAndy Grover 		pr_err("Missing tfo->queue_status()\n");
416c66ac9dbSNicholas Bellinger 		return -EINVAL;
417c66ac9dbSNicholas Bellinger 	}
4186708bb27SAndy Grover 	if (!tfo->queue_tm_rsp) {
4196708bb27SAndy Grover 		pr_err("Missing tfo->queue_tm_rsp()\n");
420c66ac9dbSNicholas Bellinger 		return -EINVAL;
421c66ac9dbSNicholas Bellinger 	}
422131e6abcSNicholas Bellinger 	if (!tfo->aborted_task) {
423131e6abcSNicholas Bellinger 		pr_err("Missing tfo->aborted_task()\n");
424131e6abcSNicholas Bellinger 		return -EINVAL;
425131e6abcSNicholas Bellinger 	}
4269c28ca4fSNicholas Bellinger 	if (!tfo->check_stop_free) {
4279c28ca4fSNicholas Bellinger 		pr_err("Missing tfo->check_stop_free()\n");
4289c28ca4fSNicholas Bellinger 		return -EINVAL;
4299c28ca4fSNicholas Bellinger 	}
430c66ac9dbSNicholas Bellinger 	/*
431c66ac9dbSNicholas Bellinger 	 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
432c66ac9dbSNicholas Bellinger 	 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
433c66ac9dbSNicholas Bellinger 	 * target_core_fabric_configfs.c WWN+TPG group context code.
434c66ac9dbSNicholas Bellinger 	 */
4356708bb27SAndy Grover 	if (!tfo->fabric_make_wwn) {
4366708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_wwn()\n");
437c66ac9dbSNicholas Bellinger 		return -EINVAL;
438c66ac9dbSNicholas Bellinger 	}
4396708bb27SAndy Grover 	if (!tfo->fabric_drop_wwn) {
4406708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_wwn()\n");
441c66ac9dbSNicholas Bellinger 		return -EINVAL;
442c66ac9dbSNicholas Bellinger 	}
4436708bb27SAndy Grover 	if (!tfo->fabric_make_tpg) {
4446708bb27SAndy Grover 		pr_err("Missing tfo->fabric_make_tpg()\n");
445c66ac9dbSNicholas Bellinger 		return -EINVAL;
446c66ac9dbSNicholas Bellinger 	}
4476708bb27SAndy Grover 	if (!tfo->fabric_drop_tpg) {
4486708bb27SAndy Grover 		pr_err("Missing tfo->fabric_drop_tpg()\n");
449c66ac9dbSNicholas Bellinger 		return -EINVAL;
450c66ac9dbSNicholas Bellinger 	}
451c66ac9dbSNicholas Bellinger 
452c66ac9dbSNicholas Bellinger 	return 0;
453c66ac9dbSNicholas Bellinger }
454c66ac9dbSNicholas Bellinger 
4559ac8928eSChristoph Hellwig int target_register_template(const struct target_core_fabric_ops *fo)
456c66ac9dbSNicholas Bellinger {
4579ac8928eSChristoph Hellwig 	struct target_fabric_configfs *tf;
458c66ac9dbSNicholas Bellinger 	int ret;
459c66ac9dbSNicholas Bellinger 
4609ac8928eSChristoph Hellwig 	ret = target_fabric_tf_ops_check(fo);
4619ac8928eSChristoph Hellwig 	if (ret)
462c66ac9dbSNicholas Bellinger 		return ret;
463c66ac9dbSNicholas Bellinger 
4649ac8928eSChristoph Hellwig 	tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
4659ac8928eSChristoph Hellwig 	if (!tf) {
4669ac8928eSChristoph Hellwig 		pr_err("%s: could not allocate memory!\n", __func__);
4679ac8928eSChristoph Hellwig 		return -ENOMEM;
4689ac8928eSChristoph Hellwig 	}
4699ac8928eSChristoph Hellwig 
4709ac8928eSChristoph Hellwig 	INIT_LIST_HEAD(&tf->tf_list);
4719ac8928eSChristoph Hellwig 	atomic_set(&tf->tf_access_cnt, 0);
472ef0caf8dSChristoph Hellwig 	tf->tf_ops = fo;
4739ac8928eSChristoph Hellwig 	target_fabric_setup_cits(tf);
4749ac8928eSChristoph Hellwig 
4759ac8928eSChristoph Hellwig 	mutex_lock(&g_tf_lock);
4769ac8928eSChristoph Hellwig 	list_add_tail(&tf->tf_list, &g_tf_list);
4779ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
4789ac8928eSChristoph Hellwig 
479c66ac9dbSNicholas Bellinger 	return 0;
480c66ac9dbSNicholas Bellinger }
4819ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_register_template);
482c66ac9dbSNicholas Bellinger 
4839ac8928eSChristoph Hellwig void target_unregister_template(const struct target_core_fabric_ops *fo)
484c66ac9dbSNicholas Bellinger {
4859ac8928eSChristoph Hellwig 	struct target_fabric_configfs *t;
486c66ac9dbSNicholas Bellinger 
487c66ac9dbSNicholas Bellinger 	mutex_lock(&g_tf_lock);
4889ac8928eSChristoph Hellwig 	list_for_each_entry(t, &g_tf_list, tf_list) {
4890dc2e8d1SChristoph Hellwig 		if (!strcmp(t->tf_ops->name, fo->name)) {
4909ac8928eSChristoph Hellwig 			BUG_ON(atomic_read(&t->tf_access_cnt));
4919ac8928eSChristoph Hellwig 			list_del(&t->tf_list);
49294509182SNicholas Bellinger 			mutex_unlock(&g_tf_lock);
49394509182SNicholas Bellinger 			/*
49494509182SNicholas Bellinger 			 * Wait for any outstanding fabric se_deve_entry->rcu_head
49594509182SNicholas Bellinger 			 * callbacks to complete post kfree_rcu(), before allowing
49694509182SNicholas Bellinger 			 * fabric driver unload of TFO->module to proceed.
49794509182SNicholas Bellinger 			 */
49894509182SNicholas Bellinger 			rcu_barrier();
4999ac8928eSChristoph Hellwig 			kfree(t);
50094509182SNicholas Bellinger 			return;
501c66ac9dbSNicholas Bellinger 		}
502c66ac9dbSNicholas Bellinger 	}
5039ac8928eSChristoph Hellwig 	mutex_unlock(&g_tf_lock);
5049ac8928eSChristoph Hellwig }
5059ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_unregister_template);
506c66ac9dbSNicholas Bellinger 
507c66ac9dbSNicholas Bellinger /*##############################################################################
508c66ac9dbSNicholas Bellinger // Stop functions called by external Target Fabrics Modules
509c66ac9dbSNicholas Bellinger //############################################################################*/
510c66ac9dbSNicholas Bellinger 
5112eafd729SChristoph Hellwig static inline struct se_dev_attrib *to_attrib(struct config_item *item)
5122eafd729SChristoph Hellwig {
5132eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_dev_attrib,
5142eafd729SChristoph Hellwig 			da_group);
5155873c4d1SChristoph Hellwig }
5165873c4d1SChristoph Hellwig 
5172eafd729SChristoph Hellwig /* Start functions for struct config_item_type tb_dev_attrib_cit */
5182eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_SHOW(_name)					\
5192eafd729SChristoph Hellwig static ssize_t _name##_show(struct config_item *item, char *page)	\
5202eafd729SChristoph Hellwig {									\
5212eafd729SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
5222eafd729SChristoph Hellwig }
5233effdb90SChristoph Hellwig 
5242eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
5252eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
5262eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
5272eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
5282eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
5292eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
5302eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
5312eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
5322eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
5332eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
5342eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
535b49d6f78SDavid Disseldorp DEF_CONFIGFS_ATTRIB_SHOW(emulate_pr);
5362eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
5372eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
538056e8924SDmitry Monakhov DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_verify);
5392eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
5402eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
5412eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
5422eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
5432eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
5442eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(block_size);
5452eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
5462eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
5472eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
5482eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
5492eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
5502eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
5512eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
5522eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
553e6f41633SJamie Pocas DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
5542eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
5552eafd729SChristoph Hellwig 
5562eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_U32(_name)				\
5572eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
5585873c4d1SChristoph Hellwig 		size_t count)						\
5595873c4d1SChristoph Hellwig {									\
5602eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5613effdb90SChristoph Hellwig 	u32 val;							\
5625873c4d1SChristoph Hellwig 	int ret;							\
5635873c4d1SChristoph Hellwig 									\
5643effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);					\
5653effdb90SChristoph Hellwig 	if (ret < 0)							\
5663effdb90SChristoph Hellwig 		return ret;						\
5673effdb90SChristoph Hellwig 	da->_name = val;						\
5683effdb90SChristoph Hellwig 	return count;							\
5695873c4d1SChristoph Hellwig }
5705873c4d1SChristoph Hellwig 
5712eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
5722eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
5732eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
5742eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
5752eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
5765873c4d1SChristoph Hellwig 
5772eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name)				\
5782eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,	\
5793effdb90SChristoph Hellwig 		size_t count)						\
5803effdb90SChristoph Hellwig {									\
5812eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);			\
5823effdb90SChristoph Hellwig 	bool flag;							\
5833effdb90SChristoph Hellwig 	int ret;							\
5843effdb90SChristoph Hellwig 									\
5853effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);					\
5863effdb90SChristoph Hellwig 	if (ret < 0)							\
5873effdb90SChristoph Hellwig 		return ret;						\
5883effdb90SChristoph Hellwig 	da->_name = flag;						\
5893effdb90SChristoph Hellwig 	return count;							\
5903effdb90SChristoph Hellwig }
5913effdb90SChristoph Hellwig 
5922eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
5932eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
5942eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
595b49d6f78SDavid Disseldorp DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_pr);
5962eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
5972eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
5983effdb90SChristoph Hellwig 
5992eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name)				\
6002eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
6013effdb90SChristoph Hellwig 		size_t count)						\
6023effdb90SChristoph Hellwig {									\
6033effdb90SChristoph Hellwig 	printk_once(KERN_WARNING					\
604234bdbc4SChristophe Vu-Brugier 		"ignoring deprecated %s attribute\n",			\
605234bdbc4SChristophe Vu-Brugier 		__stringify(_name));					\
6063effdb90SChristoph Hellwig 	return count;							\
6073effdb90SChristoph Hellwig }
6083effdb90SChristoph Hellwig 
6092eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
6102eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
6113effdb90SChristoph Hellwig 
6123effdb90SChristoph Hellwig static void dev_set_t10_wwn_model_alias(struct se_device *dev)
6133effdb90SChristoph Hellwig {
6143effdb90SChristoph Hellwig 	const char *configname;
6153effdb90SChristoph Hellwig 
6163effdb90SChristoph Hellwig 	configname = config_item_name(&dev->dev_group.cg_item);
6173effdb90SChristoph Hellwig 	if (strlen(configname) >= 16) {
6183effdb90SChristoph Hellwig 		pr_warn("dev[%p]: Backstore name '%s' is too long for "
6193effdb90SChristoph Hellwig 			"INQUIRY_MODEL, truncating to 16 bytes\n", dev,
6203effdb90SChristoph Hellwig 			configname);
6213effdb90SChristoph Hellwig 	}
6223effdb90SChristoph Hellwig 	snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
6233effdb90SChristoph Hellwig }
6243effdb90SChristoph Hellwig 
6252eafd729SChristoph Hellwig static ssize_t emulate_model_alias_store(struct config_item *item,
6263effdb90SChristoph Hellwig 		const char *page, size_t count)
6273effdb90SChristoph Hellwig {
6282eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6293effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
6303effdb90SChristoph Hellwig 	bool flag;
6313effdb90SChristoph Hellwig 	int ret;
6323effdb90SChristoph Hellwig 
6333effdb90SChristoph Hellwig 	if (dev->export_count) {
6343effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change model alias"
6353effdb90SChristoph Hellwig 			" while export_count is %d\n",
6363effdb90SChristoph Hellwig 			dev, dev->export_count);
6373effdb90SChristoph Hellwig 		return -EINVAL;
6383effdb90SChristoph Hellwig 	}
6393effdb90SChristoph Hellwig 
6403effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6413effdb90SChristoph Hellwig 	if (ret < 0)
6423effdb90SChristoph Hellwig 		return ret;
6433effdb90SChristoph Hellwig 
6443effdb90SChristoph Hellwig 	if (flag) {
6453effdb90SChristoph Hellwig 		dev_set_t10_wwn_model_alias(dev);
6463effdb90SChristoph Hellwig 	} else {
6473effdb90SChristoph Hellwig 		strncpy(&dev->t10_wwn.model[0],
6483effdb90SChristoph Hellwig 			dev->transport->inquiry_prod, 16);
6493effdb90SChristoph Hellwig 	}
6503effdb90SChristoph Hellwig 	da->emulate_model_alias = flag;
6513effdb90SChristoph Hellwig 	return count;
6523effdb90SChristoph Hellwig }
6533effdb90SChristoph Hellwig 
6542eafd729SChristoph Hellwig static ssize_t emulate_write_cache_store(struct config_item *item,
6553effdb90SChristoph Hellwig 		const char *page, size_t count)
6563effdb90SChristoph Hellwig {
6572eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6583effdb90SChristoph Hellwig 	bool flag;
6593effdb90SChristoph Hellwig 	int ret;
6603effdb90SChristoph Hellwig 
6613effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
6623effdb90SChristoph Hellwig 	if (ret < 0)
6633effdb90SChristoph Hellwig 		return ret;
6643effdb90SChristoph Hellwig 
6653effdb90SChristoph Hellwig 	if (flag && da->da_dev->transport->get_write_cache) {
6663effdb90SChristoph Hellwig 		pr_err("emulate_write_cache not supported for this device\n");
6673effdb90SChristoph Hellwig 		return -EINVAL;
6683effdb90SChristoph Hellwig 	}
6693effdb90SChristoph Hellwig 
6703effdb90SChristoph Hellwig 	da->emulate_write_cache = flag;
6713effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
6723effdb90SChristoph Hellwig 			da->da_dev, flag);
6733effdb90SChristoph Hellwig 	return count;
6743effdb90SChristoph Hellwig }
6753effdb90SChristoph Hellwig 
6762eafd729SChristoph Hellwig static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
6773effdb90SChristoph Hellwig 		const char *page, size_t count)
6783effdb90SChristoph Hellwig {
6792eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
6803effdb90SChristoph Hellwig 	u32 val;
6813effdb90SChristoph Hellwig 	int ret;
6823effdb90SChristoph Hellwig 
6833effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
6843effdb90SChristoph Hellwig 	if (ret < 0)
6853effdb90SChristoph Hellwig 		return ret;
6863effdb90SChristoph Hellwig 
6873effdb90SChristoph Hellwig 	if (val != 0 && val != 1 && val != 2) {
6883effdb90SChristoph Hellwig 		pr_err("Illegal value %d\n", val);
6893effdb90SChristoph Hellwig 		return -EINVAL;
6903effdb90SChristoph Hellwig 	}
6913effdb90SChristoph Hellwig 
6923effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
6933effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
6943effdb90SChristoph Hellwig 			" UA_INTRLCK_CTRL while export_count is %d\n",
6953effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
6963effdb90SChristoph Hellwig 		return -EINVAL;
6973effdb90SChristoph Hellwig 	}
6983effdb90SChristoph Hellwig 	da->emulate_ua_intlck_ctrl = val;
6993effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
7003effdb90SChristoph Hellwig 		da->da_dev, val);
7013effdb90SChristoph Hellwig 	return count;
7023effdb90SChristoph Hellwig }
7033effdb90SChristoph Hellwig 
7042eafd729SChristoph Hellwig static ssize_t emulate_tas_store(struct config_item *item,
7053effdb90SChristoph Hellwig 		const char *page, size_t count)
7063effdb90SChristoph Hellwig {
7072eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7083effdb90SChristoph Hellwig 	bool flag;
7093effdb90SChristoph Hellwig 	int ret;
7103effdb90SChristoph Hellwig 
7113effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7123effdb90SChristoph Hellwig 	if (ret < 0)
7133effdb90SChristoph Hellwig 		return ret;
7143effdb90SChristoph Hellwig 
7153effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
7163effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TAS while"
7173effdb90SChristoph Hellwig 			" export_count is %d\n",
7183effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
7193effdb90SChristoph Hellwig 		return -EINVAL;
7203effdb90SChristoph Hellwig 	}
7213effdb90SChristoph Hellwig 	da->emulate_tas = flag;
7223effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
7233effdb90SChristoph Hellwig 		da->da_dev, flag ? "Enabled" : "Disabled");
7243effdb90SChristoph Hellwig 
7253effdb90SChristoph Hellwig 	return count;
7263effdb90SChristoph Hellwig }
7273effdb90SChristoph Hellwig 
7282eafd729SChristoph Hellwig static ssize_t emulate_tpu_store(struct config_item *item,
7293effdb90SChristoph Hellwig 		const char *page, size_t count)
7303effdb90SChristoph Hellwig {
7312eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7323effdb90SChristoph Hellwig 	bool flag;
7333effdb90SChristoph Hellwig 	int ret;
7343effdb90SChristoph Hellwig 
7353effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7363effdb90SChristoph Hellwig 	if (ret < 0)
7373effdb90SChristoph Hellwig 		return ret;
7383effdb90SChristoph Hellwig 
7393effdb90SChristoph Hellwig 	/*
7403effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
7413effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
7423effdb90SChristoph Hellwig 	 */
7433effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
7443effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
7453effdb90SChristoph Hellwig 		return -ENOSYS;
7463effdb90SChristoph Hellwig 	}
7473effdb90SChristoph Hellwig 
7483effdb90SChristoph Hellwig 	da->emulate_tpu = flag;
7493effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
7503effdb90SChristoph Hellwig 		da->da_dev, flag);
7513effdb90SChristoph Hellwig 	return count;
7523effdb90SChristoph Hellwig }
7533effdb90SChristoph Hellwig 
7542eafd729SChristoph Hellwig static ssize_t emulate_tpws_store(struct config_item *item,
7553effdb90SChristoph Hellwig 		const char *page, size_t count)
7563effdb90SChristoph Hellwig {
7572eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7583effdb90SChristoph Hellwig 	bool flag;
7593effdb90SChristoph Hellwig 	int ret;
7603effdb90SChristoph Hellwig 
7613effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
7623effdb90SChristoph Hellwig 	if (ret < 0)
7633effdb90SChristoph Hellwig 		return ret;
7643effdb90SChristoph Hellwig 
7653effdb90SChristoph Hellwig 	/*
7663effdb90SChristoph Hellwig 	 * We expect this value to be non-zero when generic Block Layer
7673effdb90SChristoph Hellwig 	 * Discard supported is detected iblock_create_virtdevice().
7683effdb90SChristoph Hellwig 	 */
7693effdb90SChristoph Hellwig 	if (flag && !da->max_unmap_block_desc_count) {
7703effdb90SChristoph Hellwig 		pr_err("Generic Block Discard not supported\n");
7713effdb90SChristoph Hellwig 		return -ENOSYS;
7723effdb90SChristoph Hellwig 	}
7733effdb90SChristoph Hellwig 
7743effdb90SChristoph Hellwig 	da->emulate_tpws = flag;
7753effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
7763effdb90SChristoph Hellwig 				da->da_dev, flag);
7773effdb90SChristoph Hellwig 	return count;
7783effdb90SChristoph Hellwig }
7793effdb90SChristoph Hellwig 
7802eafd729SChristoph Hellwig static ssize_t pi_prot_type_store(struct config_item *item,
7813effdb90SChristoph Hellwig 		const char *page, size_t count)
7823effdb90SChristoph Hellwig {
7832eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
7843effdb90SChristoph Hellwig 	int old_prot = da->pi_prot_type, ret;
7853effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
7863effdb90SChristoph Hellwig 	u32 flag;
7873effdb90SChristoph Hellwig 
7883effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &flag);
7893effdb90SChristoph Hellwig 	if (ret < 0)
7903effdb90SChristoph Hellwig 		return ret;
7913effdb90SChristoph Hellwig 
7923effdb90SChristoph Hellwig 	if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
7933effdb90SChristoph Hellwig 		pr_err("Illegal value %d for pi_prot_type\n", flag);
7943effdb90SChristoph Hellwig 		return -EINVAL;
7953effdb90SChristoph Hellwig 	}
7963effdb90SChristoph Hellwig 	if (flag == 2) {
7973effdb90SChristoph Hellwig 		pr_err("DIF TYPE2 protection currently not supported\n");
7983effdb90SChristoph Hellwig 		return -ENOSYS;
7993effdb90SChristoph Hellwig 	}
8003effdb90SChristoph Hellwig 	if (da->hw_pi_prot_type) {
8013effdb90SChristoph Hellwig 		pr_warn("DIF protection enabled on underlying hardware,"
8023effdb90SChristoph Hellwig 			" ignoring\n");
8033effdb90SChristoph Hellwig 		return count;
8043effdb90SChristoph Hellwig 	}
8053effdb90SChristoph Hellwig 	if (!dev->transport->init_prot || !dev->transport->free_prot) {
8063effdb90SChristoph Hellwig 		/* 0 is only allowed value for non-supporting backends */
8073effdb90SChristoph Hellwig 		if (flag == 0)
808bc1a7d6aSAndy Grover 			return count;
8093effdb90SChristoph Hellwig 
8103effdb90SChristoph Hellwig 		pr_err("DIF protection not supported by backend: %s\n",
8113effdb90SChristoph Hellwig 		       dev->transport->name);
8123effdb90SChristoph Hellwig 		return -ENOSYS;
8133effdb90SChristoph Hellwig 	}
814cb0f32e1SMike Christie 	if (!target_dev_configured(dev)) {
8153effdb90SChristoph Hellwig 		pr_err("DIF protection requires device to be configured\n");
8163effdb90SChristoph Hellwig 		return -ENODEV;
8173effdb90SChristoph Hellwig 	}
8183effdb90SChristoph Hellwig 	if (dev->export_count) {
8193effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device PROT type while"
8203effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
8213effdb90SChristoph Hellwig 		return -EINVAL;
8223effdb90SChristoph Hellwig 	}
8233effdb90SChristoph Hellwig 
8243effdb90SChristoph Hellwig 	da->pi_prot_type = flag;
8253effdb90SChristoph Hellwig 
8263effdb90SChristoph Hellwig 	if (flag && !old_prot) {
8273effdb90SChristoph Hellwig 		ret = dev->transport->init_prot(dev);
8283effdb90SChristoph Hellwig 		if (ret) {
8293effdb90SChristoph Hellwig 			da->pi_prot_type = old_prot;
830056e8924SDmitry Monakhov 			da->pi_prot_verify = (bool) da->pi_prot_type;
8313effdb90SChristoph Hellwig 			return ret;
8323effdb90SChristoph Hellwig 		}
8333effdb90SChristoph Hellwig 
8343effdb90SChristoph Hellwig 	} else if (!flag && old_prot) {
8353effdb90SChristoph Hellwig 		dev->transport->free_prot(dev);
8363effdb90SChristoph Hellwig 	}
8373effdb90SChristoph Hellwig 
838056e8924SDmitry Monakhov 	da->pi_prot_verify = (bool) da->pi_prot_type;
8393effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
8403effdb90SChristoph Hellwig 	return count;
8413effdb90SChristoph Hellwig }
8423effdb90SChristoph Hellwig 
8432eafd729SChristoph Hellwig static ssize_t pi_prot_format_store(struct config_item *item,
8443effdb90SChristoph Hellwig 		const char *page, size_t count)
8453effdb90SChristoph Hellwig {
8462eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
8473effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
8483effdb90SChristoph Hellwig 	bool flag;
8493effdb90SChristoph Hellwig 	int ret;
8503effdb90SChristoph Hellwig 
8513effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
8523effdb90SChristoph Hellwig 	if (ret < 0)
8533effdb90SChristoph Hellwig 		return ret;
8543effdb90SChristoph Hellwig 
8553effdb90SChristoph Hellwig 	if (!flag)
8563effdb90SChristoph Hellwig 		return count;
8573effdb90SChristoph Hellwig 
8583effdb90SChristoph Hellwig 	if (!dev->transport->format_prot) {
8593effdb90SChristoph Hellwig 		pr_err("DIF protection format not supported by backend %s\n",
8603effdb90SChristoph Hellwig 		       dev->transport->name);
8613effdb90SChristoph Hellwig 		return -ENOSYS;
8623effdb90SChristoph Hellwig 	}
863cb0f32e1SMike Christie 	if (!target_dev_configured(dev)) {
8643effdb90SChristoph Hellwig 		pr_err("DIF protection format requires device to be configured\n");
8653effdb90SChristoph Hellwig 		return -ENODEV;
8663effdb90SChristoph Hellwig 	}
8673effdb90SChristoph Hellwig 	if (dev->export_count) {
8683effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to format SE Device PROT type while"
8693effdb90SChristoph Hellwig 		       " export_count is %d\n", dev, dev->export_count);
8703effdb90SChristoph Hellwig 		return -EINVAL;
8713effdb90SChristoph Hellwig 	}
8723effdb90SChristoph Hellwig 
8733effdb90SChristoph Hellwig 	ret = dev->transport->format_prot(dev);
8743effdb90SChristoph Hellwig 	if (ret)
8753effdb90SChristoph Hellwig 		return ret;
8763effdb90SChristoph Hellwig 
8773effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
8783effdb90SChristoph Hellwig 	return count;
8793effdb90SChristoph Hellwig }
8803effdb90SChristoph Hellwig 
881056e8924SDmitry Monakhov static ssize_t pi_prot_verify_store(struct config_item *item,
882056e8924SDmitry Monakhov 		const char *page, size_t count)
883056e8924SDmitry Monakhov {
884056e8924SDmitry Monakhov 	struct se_dev_attrib *da = to_attrib(item);
885056e8924SDmitry Monakhov 	bool flag;
886056e8924SDmitry Monakhov 	int ret;
887056e8924SDmitry Monakhov 
888056e8924SDmitry Monakhov 	ret = strtobool(page, &flag);
889056e8924SDmitry Monakhov 	if (ret < 0)
890056e8924SDmitry Monakhov 		return ret;
891056e8924SDmitry Monakhov 
892056e8924SDmitry Monakhov 	if (!flag) {
893056e8924SDmitry Monakhov 		da->pi_prot_verify = flag;
894056e8924SDmitry Monakhov 		return count;
895056e8924SDmitry Monakhov 	}
896056e8924SDmitry Monakhov 	if (da->hw_pi_prot_type) {
897056e8924SDmitry Monakhov 		pr_warn("DIF protection enabled on underlying hardware,"
898056e8924SDmitry Monakhov 			" ignoring\n");
899056e8924SDmitry Monakhov 		return count;
900056e8924SDmitry Monakhov 	}
901056e8924SDmitry Monakhov 	if (!da->pi_prot_type) {
902056e8924SDmitry Monakhov 		pr_warn("DIF protection not supported by backend, ignoring\n");
903056e8924SDmitry Monakhov 		return count;
904056e8924SDmitry Monakhov 	}
905056e8924SDmitry Monakhov 	da->pi_prot_verify = flag;
906056e8924SDmitry Monakhov 
907056e8924SDmitry Monakhov 	return count;
908056e8924SDmitry Monakhov }
909056e8924SDmitry Monakhov 
9102eafd729SChristoph Hellwig static ssize_t force_pr_aptpl_store(struct config_item *item,
9113effdb90SChristoph Hellwig 		const char *page, size_t count)
9123effdb90SChristoph Hellwig {
9132eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9143effdb90SChristoph Hellwig 	bool flag;
9153effdb90SChristoph Hellwig 	int ret;
9163effdb90SChristoph Hellwig 
9173effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
9183effdb90SChristoph Hellwig 	if (ret < 0)
9193effdb90SChristoph Hellwig 		return ret;
9203effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
9213effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to set force_pr_aptpl while"
9223effdb90SChristoph Hellwig 		       " export_count is %d\n",
9233effdb90SChristoph Hellwig 		       da->da_dev, da->da_dev->export_count);
9243effdb90SChristoph Hellwig 		return -EINVAL;
9253effdb90SChristoph Hellwig 	}
9263effdb90SChristoph Hellwig 
9273effdb90SChristoph Hellwig 	da->force_pr_aptpl = flag;
9283effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
9293effdb90SChristoph Hellwig 	return count;
9303effdb90SChristoph Hellwig }
9313effdb90SChristoph Hellwig 
9322eafd729SChristoph Hellwig static ssize_t emulate_rest_reord_store(struct config_item *item,
9333effdb90SChristoph Hellwig 		const char *page, size_t count)
9343effdb90SChristoph Hellwig {
9352eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9363effdb90SChristoph Hellwig 	bool flag;
9373effdb90SChristoph Hellwig 	int ret;
9383effdb90SChristoph Hellwig 
9393effdb90SChristoph Hellwig 	ret = strtobool(page, &flag);
9403effdb90SChristoph Hellwig 	if (ret < 0)
9413effdb90SChristoph Hellwig 		return ret;
9423effdb90SChristoph Hellwig 
9433effdb90SChristoph Hellwig 	if (flag != 0) {
9443effdb90SChristoph Hellwig 		printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
9453effdb90SChristoph Hellwig 			" reordering not implemented\n", da->da_dev);
9463effdb90SChristoph Hellwig 		return -ENOSYS;
9473effdb90SChristoph Hellwig 	}
9483effdb90SChristoph Hellwig 	da->emulate_rest_reord = flag;
9493effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
9503effdb90SChristoph Hellwig 		da->da_dev, flag);
9513effdb90SChristoph Hellwig 	return count;
9523effdb90SChristoph Hellwig }
9533effdb90SChristoph Hellwig 
954e6f41633SJamie Pocas static ssize_t unmap_zeroes_data_store(struct config_item *item,
955e6f41633SJamie Pocas 		const char *page, size_t count)
956e6f41633SJamie Pocas {
957e6f41633SJamie Pocas 	struct se_dev_attrib *da = to_attrib(item);
958e6f41633SJamie Pocas 	bool flag;
959e6f41633SJamie Pocas 	int ret;
960e6f41633SJamie Pocas 
961e6f41633SJamie Pocas 	ret = strtobool(page, &flag);
962e6f41633SJamie Pocas 	if (ret < 0)
963e6f41633SJamie Pocas 		return ret;
964e6f41633SJamie Pocas 
965e6f41633SJamie Pocas 	if (da->da_dev->export_count) {
966e6f41633SJamie Pocas 		pr_err("dev[%p]: Unable to change SE Device"
967e6f41633SJamie Pocas 		       " unmap_zeroes_data while export_count is %d\n",
968e6f41633SJamie Pocas 		       da->da_dev, da->da_dev->export_count);
969e6f41633SJamie Pocas 		return -EINVAL;
970e6f41633SJamie Pocas 	}
971e6f41633SJamie Pocas 	/*
972e6f41633SJamie Pocas 	 * We expect this value to be non-zero when generic Block Layer
973e6f41633SJamie Pocas 	 * Discard supported is detected iblock_configure_device().
974e6f41633SJamie Pocas 	 */
975e6f41633SJamie Pocas 	if (flag && !da->max_unmap_block_desc_count) {
976e6f41633SJamie Pocas 		pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
977e6f41633SJamie Pocas 		       " because max_unmap_block_desc_count is zero\n",
978e6f41633SJamie Pocas 		       da->da_dev);
979e6f41633SJamie Pocas 		return -ENOSYS;
980e6f41633SJamie Pocas 	}
981e6f41633SJamie Pocas 	da->unmap_zeroes_data = flag;
982e6f41633SJamie Pocas 	pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
983e6f41633SJamie Pocas 		 da->da_dev, flag);
9842e498f25SNicholas Bellinger 	return count;
985e6f41633SJamie Pocas }
986e6f41633SJamie Pocas 
9873effdb90SChristoph Hellwig /*
9883effdb90SChristoph Hellwig  * Note, this can only be called on unexported SE Device Object.
9893effdb90SChristoph Hellwig  */
9902eafd729SChristoph Hellwig static ssize_t queue_depth_store(struct config_item *item,
9913effdb90SChristoph Hellwig 		const char *page, size_t count)
9923effdb90SChristoph Hellwig {
9932eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
9943effdb90SChristoph Hellwig 	struct se_device *dev = da->da_dev;
9953effdb90SChristoph Hellwig 	u32 val;
9963effdb90SChristoph Hellwig 	int ret;
9973effdb90SChristoph Hellwig 
9983effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
9993effdb90SChristoph Hellwig 	if (ret < 0)
10003effdb90SChristoph Hellwig 		return ret;
10013effdb90SChristoph Hellwig 
10023effdb90SChristoph Hellwig 	if (dev->export_count) {
10033effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device TCQ while"
10043effdb90SChristoph Hellwig 			" export_count is %d\n",
10053effdb90SChristoph Hellwig 			dev, dev->export_count);
10063effdb90SChristoph Hellwig 		return -EINVAL;
10073effdb90SChristoph Hellwig 	}
10083effdb90SChristoph Hellwig 	if (!val) {
10093effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
10103effdb90SChristoph Hellwig 		return -EINVAL;
10113effdb90SChristoph Hellwig 	}
10123effdb90SChristoph Hellwig 
10133effdb90SChristoph Hellwig 	if (val > dev->dev_attrib.queue_depth) {
10143effdb90SChristoph Hellwig 		if (val > dev->dev_attrib.hw_queue_depth) {
10153effdb90SChristoph Hellwig 			pr_err("dev[%p]: Passed queue_depth:"
10163effdb90SChristoph Hellwig 				" %u exceeds TCM/SE_Device MAX"
10173effdb90SChristoph Hellwig 				" TCQ: %u\n", dev, val,
10183effdb90SChristoph Hellwig 				dev->dev_attrib.hw_queue_depth);
10193effdb90SChristoph Hellwig 			return -EINVAL;
10203effdb90SChristoph Hellwig 		}
10213effdb90SChristoph Hellwig 	}
10223effdb90SChristoph Hellwig 	da->queue_depth = dev->queue_depth = val;
10233effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
10243effdb90SChristoph Hellwig 	return count;
10253effdb90SChristoph Hellwig }
10263effdb90SChristoph Hellwig 
10272eafd729SChristoph Hellwig static ssize_t optimal_sectors_store(struct config_item *item,
10283effdb90SChristoph Hellwig 		const char *page, size_t count)
10293effdb90SChristoph Hellwig {
10302eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
10313effdb90SChristoph Hellwig 	u32 val;
10323effdb90SChristoph Hellwig 	int ret;
10333effdb90SChristoph Hellwig 
10343effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
10353effdb90SChristoph Hellwig 	if (ret < 0)
10363effdb90SChristoph Hellwig 		return ret;
10373effdb90SChristoph Hellwig 
10383effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
10393effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device"
10403effdb90SChristoph Hellwig 			" optimal_sectors while export_count is %d\n",
10413effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
10423effdb90SChristoph Hellwig 		return -EINVAL;
10433effdb90SChristoph Hellwig 	}
10443effdb90SChristoph Hellwig 	if (val > da->hw_max_sectors) {
10453effdb90SChristoph Hellwig 		pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
10463effdb90SChristoph Hellwig 			" greater than hw_max_sectors: %u\n",
10473effdb90SChristoph Hellwig 			da->da_dev, val, da->hw_max_sectors);
10483effdb90SChristoph Hellwig 		return -EINVAL;
10493effdb90SChristoph Hellwig 	}
10503effdb90SChristoph Hellwig 
10513effdb90SChristoph Hellwig 	da->optimal_sectors = val;
10523effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
10533effdb90SChristoph Hellwig 			da->da_dev, val);
10543effdb90SChristoph Hellwig 	return count;
10553effdb90SChristoph Hellwig }
10563effdb90SChristoph Hellwig 
10572eafd729SChristoph Hellwig static ssize_t block_size_store(struct config_item *item,
10583effdb90SChristoph Hellwig 		const char *page, size_t count)
10593effdb90SChristoph Hellwig {
10602eafd729SChristoph Hellwig 	struct se_dev_attrib *da = to_attrib(item);
10613effdb90SChristoph Hellwig 	u32 val;
10623effdb90SChristoph Hellwig 	int ret;
10633effdb90SChristoph Hellwig 
10643effdb90SChristoph Hellwig 	ret = kstrtou32(page, 0, &val);
10653effdb90SChristoph Hellwig 	if (ret < 0)
10663effdb90SChristoph Hellwig 		return ret;
10673effdb90SChristoph Hellwig 
10683effdb90SChristoph Hellwig 	if (da->da_dev->export_count) {
10693effdb90SChristoph Hellwig 		pr_err("dev[%p]: Unable to change SE Device block_size"
10703effdb90SChristoph Hellwig 			" while export_count is %d\n",
10713effdb90SChristoph Hellwig 			da->da_dev, da->da_dev->export_count);
10723effdb90SChristoph Hellwig 		return -EINVAL;
10733effdb90SChristoph Hellwig 	}
10743effdb90SChristoph Hellwig 
10753effdb90SChristoph Hellwig 	if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
10763effdb90SChristoph Hellwig 		pr_err("dev[%p]: Illegal value for block_device: %u"
10773effdb90SChristoph Hellwig 			" for SE device, must be 512, 1024, 2048 or 4096\n",
10783effdb90SChristoph Hellwig 			da->da_dev, val);
10793effdb90SChristoph Hellwig 		return -EINVAL;
10803effdb90SChristoph Hellwig 	}
10813effdb90SChristoph Hellwig 
10823effdb90SChristoph Hellwig 	da->block_size = val;
10833effdb90SChristoph Hellwig 	if (da->max_bytes_per_io)
10843effdb90SChristoph Hellwig 		da->hw_max_sectors = da->max_bytes_per_io / val;
10853effdb90SChristoph Hellwig 
10863effdb90SChristoph Hellwig 	pr_debug("dev[%p]: SE Device block_size changed to %u\n",
10873effdb90SChristoph Hellwig 			da->da_dev, val);
10883effdb90SChristoph Hellwig 	return count;
10893effdb90SChristoph Hellwig }
10905873c4d1SChristoph Hellwig 
1091c17d5d5fSMike Christie static ssize_t alua_support_show(struct config_item *item, char *page)
1092c17d5d5fSMike Christie {
1093c17d5d5fSMike Christie 	struct se_dev_attrib *da = to_attrib(item);
1094c17d5d5fSMike Christie 	u8 flags = da->da_dev->transport->transport_flags;
1095c17d5d5fSMike Christie 
1096c17d5d5fSMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n",
1097c17d5d5fSMike Christie 			flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ? 0 : 1);
1098c17d5d5fSMike Christie }
1099c17d5d5fSMike Christie 
1100c17d5d5fSMike Christie static ssize_t pgr_support_show(struct config_item *item, char *page)
1101c17d5d5fSMike Christie {
1102c17d5d5fSMike Christie 	struct se_dev_attrib *da = to_attrib(item);
1103c17d5d5fSMike Christie 	u8 flags = da->da_dev->transport->transport_flags;
1104c17d5d5fSMike Christie 
1105c17d5d5fSMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n",
1106c17d5d5fSMike Christie 			flags & TRANSPORT_FLAG_PASSTHROUGH_PGR ? 0 : 1);
1107c17d5d5fSMike Christie }
1108c17d5d5fSMike Christie 
11092eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_model_alias);
11102eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_dpo);
11112eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_write);
11122eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_read);
11132eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_write_cache);
11142eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
11152eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tas);
11162eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpu);
11172eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpws);
11182eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_caw);
11192eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_3pc);
1120b49d6f78SDavid Disseldorp CONFIGFS_ATTR(, emulate_pr);
11212eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_type);
11222eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_pi_prot_type);
11236baca760SDavid Disseldorp CONFIGFS_ATTR_WO(, pi_prot_format);
1124056e8924SDmitry Monakhov CONFIGFS_ATTR(, pi_prot_verify);
11252eafd729SChristoph Hellwig CONFIGFS_ATTR(, enforce_pr_isids);
11262eafd729SChristoph Hellwig CONFIGFS_ATTR(, is_nonrot);
11272eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_rest_reord);
11282eafd729SChristoph Hellwig CONFIGFS_ATTR(, force_pr_aptpl);
11292eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_block_size);
11302eafd729SChristoph Hellwig CONFIGFS_ATTR(, block_size);
11312eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_max_sectors);
11322eafd729SChristoph Hellwig CONFIGFS_ATTR(, optimal_sectors);
11332eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_queue_depth);
11342eafd729SChristoph Hellwig CONFIGFS_ATTR(, queue_depth);
11352eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_lba_count);
11362eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_block_desc_count);
11372eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity);
11382eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity_alignment);
1139e6f41633SJamie Pocas CONFIGFS_ATTR(, unmap_zeroes_data);
11402eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_write_same_len);
1141c17d5d5fSMike Christie CONFIGFS_ATTR_RO(, alua_support);
1142c17d5d5fSMike Christie CONFIGFS_ATTR_RO(, pgr_support);
1143c66ac9dbSNicholas Bellinger 
11445873c4d1SChristoph Hellwig /*
11455873c4d1SChristoph Hellwig  * dev_attrib attributes for devices using the target core SBC/SPC
11465873c4d1SChristoph Hellwig  * interpreter.  Any backend using spc_parse_cdb should be using
11475873c4d1SChristoph Hellwig  * these.
11485873c4d1SChristoph Hellwig  */
11495873c4d1SChristoph Hellwig struct configfs_attribute *sbc_attrib_attrs[] = {
11502eafd729SChristoph Hellwig 	&attr_emulate_model_alias,
11512eafd729SChristoph Hellwig 	&attr_emulate_dpo,
11522eafd729SChristoph Hellwig 	&attr_emulate_fua_write,
11532eafd729SChristoph Hellwig 	&attr_emulate_fua_read,
11542eafd729SChristoph Hellwig 	&attr_emulate_write_cache,
11552eafd729SChristoph Hellwig 	&attr_emulate_ua_intlck_ctrl,
11562eafd729SChristoph Hellwig 	&attr_emulate_tas,
11572eafd729SChristoph Hellwig 	&attr_emulate_tpu,
11582eafd729SChristoph Hellwig 	&attr_emulate_tpws,
11592eafd729SChristoph Hellwig 	&attr_emulate_caw,
11602eafd729SChristoph Hellwig 	&attr_emulate_3pc,
1161b49d6f78SDavid Disseldorp 	&attr_emulate_pr,
11622eafd729SChristoph Hellwig 	&attr_pi_prot_type,
11632eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
11642eafd729SChristoph Hellwig 	&attr_pi_prot_format,
1165056e8924SDmitry Monakhov 	&attr_pi_prot_verify,
11662eafd729SChristoph Hellwig 	&attr_enforce_pr_isids,
11672eafd729SChristoph Hellwig 	&attr_is_nonrot,
11682eafd729SChristoph Hellwig 	&attr_emulate_rest_reord,
11692eafd729SChristoph Hellwig 	&attr_force_pr_aptpl,
11702eafd729SChristoph Hellwig 	&attr_hw_block_size,
11712eafd729SChristoph Hellwig 	&attr_block_size,
11722eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
11732eafd729SChristoph Hellwig 	&attr_optimal_sectors,
11742eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
11752eafd729SChristoph Hellwig 	&attr_queue_depth,
11762eafd729SChristoph Hellwig 	&attr_max_unmap_lba_count,
11772eafd729SChristoph Hellwig 	&attr_max_unmap_block_desc_count,
11782eafd729SChristoph Hellwig 	&attr_unmap_granularity,
11792eafd729SChristoph Hellwig 	&attr_unmap_granularity_alignment,
1180e6f41633SJamie Pocas 	&attr_unmap_zeroes_data,
11812eafd729SChristoph Hellwig 	&attr_max_write_same_len,
1182c17d5d5fSMike Christie 	&attr_alua_support,
1183c17d5d5fSMike Christie 	&attr_pgr_support,
11845873c4d1SChristoph Hellwig 	NULL,
11855873c4d1SChristoph Hellwig };
11865873c4d1SChristoph Hellwig EXPORT_SYMBOL(sbc_attrib_attrs);
11875873c4d1SChristoph Hellwig 
11885873c4d1SChristoph Hellwig /*
11895873c4d1SChristoph Hellwig  * Minimal dev_attrib attributes for devices passing through CDBs.
11905873c4d1SChristoph Hellwig  * In this case we only provide a few read-only attributes for
11915873c4d1SChristoph Hellwig  * backwards compatibility.
11925873c4d1SChristoph Hellwig  */
11935873c4d1SChristoph Hellwig struct configfs_attribute *passthrough_attrib_attrs[] = {
11942eafd729SChristoph Hellwig 	&attr_hw_pi_prot_type,
11952eafd729SChristoph Hellwig 	&attr_hw_block_size,
11962eafd729SChristoph Hellwig 	&attr_hw_max_sectors,
11972eafd729SChristoph Hellwig 	&attr_hw_queue_depth,
1198c17d5d5fSMike Christie 	&attr_alua_support,
1199c17d5d5fSMike Christie 	&attr_pgr_support,
12005873c4d1SChristoph Hellwig 	NULL,
12015873c4d1SChristoph Hellwig };
12025873c4d1SChristoph Hellwig EXPORT_SYMBOL(passthrough_attrib_attrs);
12035873c4d1SChristoph Hellwig 
12042eafd729SChristoph Hellwig TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
12058dc31ff9SMike Christie TB_CIT_SETUP_DRV(dev_action, NULL, NULL);
1206c66ac9dbSNicholas Bellinger 
1207f79a897eSNicholas Bellinger /* End functions for struct config_item_type tb_dev_attrib_cit */
1208c66ac9dbSNicholas Bellinger 
1209f8d389c6SNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_wwn_cit */
1210c66ac9dbSNicholas Bellinger 
12112eafd729SChristoph Hellwig static struct t10_wwn *to_t10_wwn(struct config_item *item)
12122eafd729SChristoph Hellwig {
12132eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
12142eafd729SChristoph Hellwig }
1215c66ac9dbSNicholas Bellinger 
1216c66ac9dbSNicholas Bellinger /*
1217c66ac9dbSNicholas Bellinger  * VPD page 0x80 Unit serial
1218c66ac9dbSNicholas Bellinger  */
12192eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1220c66ac9dbSNicholas Bellinger 		char *page)
1221c66ac9dbSNicholas Bellinger {
1222c66ac9dbSNicholas Bellinger 	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
12232eafd729SChristoph Hellwig 		&to_t10_wwn(item)->unit_serial[0]);
1224c66ac9dbSNicholas Bellinger }
1225c66ac9dbSNicholas Bellinger 
12262eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
12272eafd729SChristoph Hellwig 		const char *page, size_t count)
1228c66ac9dbSNicholas Bellinger {
12292eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
12300fd97ccfSChristoph Hellwig 	struct se_device *dev = t10_wwn->t10_dev;
1231c66ac9dbSNicholas Bellinger 	unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1232c66ac9dbSNicholas Bellinger 
1233c66ac9dbSNicholas Bellinger 	/*
1234c66ac9dbSNicholas Bellinger 	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1235c66ac9dbSNicholas Bellinger 	 * from the struct scsi_device level firmware, do not allow
1236c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial to be emulated.
1237c66ac9dbSNicholas Bellinger 	 *
1238c66ac9dbSNicholas Bellinger 	 * Note this struct scsi_device could also be emulating VPD
1239c66ac9dbSNicholas Bellinger 	 * information from its drivers/scsi LLD.  But for now we assume
1240c66ac9dbSNicholas Bellinger 	 * it is doing 'the right thing' wrt a world wide unique
1241c66ac9dbSNicholas Bellinger 	 * VPD Unit Serial Number that OS dependent multipath can depend on.
1242c66ac9dbSNicholas Bellinger 	 */
12430fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
12446708bb27SAndy Grover 		pr_err("Underlying SCSI device firmware provided VPD"
1245c66ac9dbSNicholas Bellinger 			" Unit Serial, ignoring request\n");
1246c66ac9dbSNicholas Bellinger 		return -EOPNOTSUPP;
1247c66ac9dbSNicholas Bellinger 	}
1248c66ac9dbSNicholas Bellinger 
124960d645a4SDan Carpenter 	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
12506708bb27SAndy Grover 		pr_err("Emulated VPD Unit Serial exceeds"
1251c66ac9dbSNicholas Bellinger 		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1252c66ac9dbSNicholas Bellinger 		return -EOVERFLOW;
1253c66ac9dbSNicholas Bellinger 	}
1254c66ac9dbSNicholas Bellinger 	/*
1255c66ac9dbSNicholas Bellinger 	 * Check to see if any active $FABRIC_MOD exports exist.  If they
1256c66ac9dbSNicholas Bellinger 	 * do exist, fail here as changing this information on the fly
1257c66ac9dbSNicholas Bellinger 	 * (underneath the initiator side OS dependent multipath code)
1258c66ac9dbSNicholas Bellinger 	 * could cause negative effects.
1259c66ac9dbSNicholas Bellinger 	 */
12600fd97ccfSChristoph Hellwig 	if (dev->export_count) {
12616708bb27SAndy Grover 		pr_err("Unable to set VPD Unit Serial while"
1262c66ac9dbSNicholas Bellinger 			" active %d $FABRIC_MOD exports exist\n",
12630fd97ccfSChristoph Hellwig 			dev->export_count);
1264c66ac9dbSNicholas Bellinger 		return -EINVAL;
1265c66ac9dbSNicholas Bellinger 	}
12660fd97ccfSChristoph Hellwig 
1267c66ac9dbSNicholas Bellinger 	/*
1268c66ac9dbSNicholas Bellinger 	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1269c66ac9dbSNicholas Bellinger 	 *
1270c66ac9dbSNicholas Bellinger 	 * Also, strip any newline added from the userspace
1271c66ac9dbSNicholas Bellinger 	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1272c66ac9dbSNicholas Bellinger 	 */
1273c66ac9dbSNicholas Bellinger 	memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1274c66ac9dbSNicholas Bellinger 	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
12750fd97ccfSChristoph Hellwig 	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1276c66ac9dbSNicholas Bellinger 			"%s", strstrip(buf));
12770fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1278c66ac9dbSNicholas Bellinger 
12796708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
12800fd97ccfSChristoph Hellwig 			" %s\n", dev->t10_wwn.unit_serial);
1281c66ac9dbSNicholas Bellinger 
1282c66ac9dbSNicholas Bellinger 	return count;
1283c66ac9dbSNicholas Bellinger }
1284c66ac9dbSNicholas Bellinger 
1285c66ac9dbSNicholas Bellinger /*
1286c66ac9dbSNicholas Bellinger  * VPD page 0x83 Protocol Identifier
1287c66ac9dbSNicholas Bellinger  */
12882eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1289c66ac9dbSNicholas Bellinger 		char *page)
1290c66ac9dbSNicholas Bellinger {
12912eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1292c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;
1293c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];
1294c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1295c66ac9dbSNicholas Bellinger 
1296c66ac9dbSNicholas Bellinger 	memset(buf, 0, VPD_TMP_BUF_SIZE);
1297c66ac9dbSNicholas Bellinger 
1298c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);
1299c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
13006708bb27SAndy Grover 		if (!vpd->protocol_identifier_set)
1301c66ac9dbSNicholas Bellinger 			continue;
1302c66ac9dbSNicholas Bellinger 
1303c66ac9dbSNicholas Bellinger 		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1304c66ac9dbSNicholas Bellinger 
13056708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1306c66ac9dbSNicholas Bellinger 			break;
1307c66ac9dbSNicholas Bellinger 
1308c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1309c66ac9dbSNicholas Bellinger 	}
1310c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);
1311c66ac9dbSNicholas Bellinger 
1312c66ac9dbSNicholas Bellinger 	return len;
1313c66ac9dbSNicholas Bellinger }
1314c66ac9dbSNicholas Bellinger 
1315c66ac9dbSNicholas Bellinger /*
1316c66ac9dbSNicholas Bellinger  * Generic wrapper for dumping VPD identifiers by association.
1317c66ac9dbSNicholas Bellinger  */
1318c66ac9dbSNicholas Bellinger #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
13192eafd729SChristoph Hellwig static ssize_t target_wwn_##_name##_show(struct config_item *item,	\
1320c66ac9dbSNicholas Bellinger 		char *page)						\
1321c66ac9dbSNicholas Bellinger {									\
13222eafd729SChristoph Hellwig 	struct t10_wwn *t10_wwn = to_t10_wwn(item);			\
1323c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd;						\
1324c66ac9dbSNicholas Bellinger 	unsigned char buf[VPD_TMP_BUF_SIZE];				\
1325c66ac9dbSNicholas Bellinger 	ssize_t len = 0;						\
1326c66ac9dbSNicholas Bellinger 									\
1327c66ac9dbSNicholas Bellinger 	spin_lock(&t10_wwn->t10_vpd_lock);				\
1328c66ac9dbSNicholas Bellinger 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
1329c66ac9dbSNicholas Bellinger 		if (vpd->association != _assoc)				\
1330c66ac9dbSNicholas Bellinger 			continue;					\
1331c66ac9dbSNicholas Bellinger 									\
1332c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1333c66ac9dbSNicholas Bellinger 		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
13346708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1335c66ac9dbSNicholas Bellinger 			break;						\
1336c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1337c66ac9dbSNicholas Bellinger 									\
1338c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1339c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
13406708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1341c66ac9dbSNicholas Bellinger 			break;						\
1342c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1343c66ac9dbSNicholas Bellinger 									\
1344c66ac9dbSNicholas Bellinger 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1345c66ac9dbSNicholas Bellinger 		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
13466708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)			\
1347c66ac9dbSNicholas Bellinger 			break;						\
1348c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);			\
1349c66ac9dbSNicholas Bellinger 	}								\
1350c66ac9dbSNicholas Bellinger 	spin_unlock(&t10_wwn->t10_vpd_lock);				\
1351c66ac9dbSNicholas Bellinger 									\
1352c66ac9dbSNicholas Bellinger 	return len;							\
1353c66ac9dbSNicholas Bellinger }
1354c66ac9dbSNicholas Bellinger 
13552eafd729SChristoph Hellwig /* VPD page 0x83 Association: Logical Unit */
1356c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
13572eafd729SChristoph Hellwig /* VPD page 0x83 Association: Target Port */
1358c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
13592eafd729SChristoph Hellwig /* VPD page 0x83 Association: SCSI Target Device */
1360c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1361c66ac9dbSNicholas Bellinger 
13622eafd729SChristoph Hellwig CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
13632eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
13642eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
13652eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
13662eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1367c66ac9dbSNicholas Bellinger 
1368c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
13692eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_unit_serial,
13702eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_protocol_identifier,
13712eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_logical_unit,
13722eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_target_port,
13732eafd729SChristoph Hellwig 	&target_wwn_attr_vpd_assoc_scsi_target_device,
1374c66ac9dbSNicholas Bellinger 	NULL,
1375c66ac9dbSNicholas Bellinger };
1376c66ac9dbSNicholas Bellinger 
13772eafd729SChristoph Hellwig TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1378c66ac9dbSNicholas Bellinger 
1379f8d389c6SNicholas Bellinger /*  End functions for struct config_item_type tb_dev_wwn_cit */
1380c66ac9dbSNicholas Bellinger 
138191e2e39bSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_pr_cit */
1382c66ac9dbSNicholas Bellinger 
13832eafd729SChristoph Hellwig static struct se_device *pr_to_dev(struct config_item *item)
13842eafd729SChristoph Hellwig {
13852eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device,
13862eafd729SChristoph Hellwig 			dev_pr_group);
13872eafd729SChristoph Hellwig }
1388c66ac9dbSNicholas Bellinger 
1389d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1390d977f437SChristoph Hellwig 		char *page)
1391c66ac9dbSNicholas Bellinger {
1392c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1393c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1394c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1395c66ac9dbSNicholas Bellinger 
1396c66ac9dbSNicholas Bellinger 	memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1397c66ac9dbSNicholas Bellinger 
1398c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1399d977f437SChristoph Hellwig 	if (!pr_reg)
1400d977f437SChristoph Hellwig 		return sprintf(page, "No SPC-3 Reservation holder\n");
1401d977f437SChristoph Hellwig 
1402c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1403d2843c17SAndy Grover 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1404c66ac9dbSNicholas Bellinger 
1405d977f437SChristoph Hellwig 	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
140630c7ca93SDavid Disseldorp 		se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1407d2843c17SAndy Grover 		se_nacl->initiatorname, i_buf);
1408c66ac9dbSNicholas Bellinger }
1409c66ac9dbSNicholas Bellinger 
1410d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1411d977f437SChristoph Hellwig 		char *page)
1412c66ac9dbSNicholas Bellinger {
1413c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1414d977f437SChristoph Hellwig 	ssize_t len;
1415c66ac9dbSNicholas Bellinger 
1416c66ac9dbSNicholas Bellinger 	se_nacl = dev->dev_reserved_node_acl;
1417d977f437SChristoph Hellwig 	if (se_nacl) {
1418d977f437SChristoph Hellwig 		len = sprintf(page,
1419d977f437SChristoph Hellwig 			      "SPC-2 Reservation: %s Initiator: %s\n",
142030c7ca93SDavid Disseldorp 			      se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1421c66ac9dbSNicholas Bellinger 			      se_nacl->initiatorname);
1422d977f437SChristoph Hellwig 	} else {
1423d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-2 Reservation holder\n");
1424d977f437SChristoph Hellwig 	}
1425d977f437SChristoph Hellwig 	return len;
1426c66ac9dbSNicholas Bellinger }
1427c66ac9dbSNicholas Bellinger 
14282eafd729SChristoph Hellwig static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1429c66ac9dbSNicholas Bellinger {
14302eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1431d977f437SChristoph Hellwig 	int ret;
1432c66ac9dbSNicholas Bellinger 
1433b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr)
1434b49d6f78SDavid Disseldorp 		return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1435b49d6f78SDavid Disseldorp 
14364ec5bf0eSBryant G. Ly 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1437d977f437SChristoph Hellwig 		return sprintf(page, "Passthrough\n");
1438c66ac9dbSNicholas Bellinger 
1439d977f437SChristoph Hellwig 	spin_lock(&dev->dev_reservation_lock);
1440d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1441d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc2_res(dev, page);
1442d977f437SChristoph Hellwig 	else
1443d977f437SChristoph Hellwig 		ret = target_core_dev_pr_show_spc3_res(dev, page);
1444d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1445d977f437SChristoph Hellwig 	return ret;
1446c66ac9dbSNicholas Bellinger }
1447c66ac9dbSNicholas Bellinger 
14482eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
14492eafd729SChristoph Hellwig 		char *page)
1450c66ac9dbSNicholas Bellinger {
14512eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1452c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1453c66ac9dbSNicholas Bellinger 
1454c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1455d977f437SChristoph Hellwig 	if (!dev->dev_pr_res_holder) {
1456c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1457d977f437SChristoph Hellwig 	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1458c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: All Target"
1459c66ac9dbSNicholas Bellinger 			" Ports registration\n");
1460d977f437SChristoph Hellwig 	} else {
1461c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation: Single"
1462c66ac9dbSNicholas Bellinger 			" Target Port registration\n");
1463d977f437SChristoph Hellwig 	}
1464c66ac9dbSNicholas Bellinger 
1465d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1466c66ac9dbSNicholas Bellinger 	return len;
1467c66ac9dbSNicholas Bellinger }
1468c66ac9dbSNicholas Bellinger 
14692eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
14702eafd729SChristoph Hellwig 		char *page)
1471c66ac9dbSNicholas Bellinger {
14722eafd729SChristoph Hellwig 	return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1473c66ac9dbSNicholas Bellinger }
1474c66ac9dbSNicholas Bellinger 
1475c66ac9dbSNicholas Bellinger 
14762eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
14772eafd729SChristoph Hellwig 		char *page)
1478c66ac9dbSNicholas Bellinger {
14792eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1480c66ac9dbSNicholas Bellinger 	struct se_node_acl *se_nacl;
1481c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg;
1482c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
14839ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1484c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1485c66ac9dbSNicholas Bellinger 
1486c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1487c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
14886708bb27SAndy Grover 	if (!pr_reg) {
1489c66ac9dbSNicholas Bellinger 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1490d977f437SChristoph Hellwig 		goto out_unlock;
1491c66ac9dbSNicholas Bellinger 	}
1492d977f437SChristoph Hellwig 
1493c66ac9dbSNicholas Bellinger 	se_nacl = pr_reg->pr_reg_nacl;
1494c66ac9dbSNicholas Bellinger 	se_tpg = se_nacl->se_tpg;
1495e3d6f909SAndy Grover 	tfo = se_tpg->se_tpg_tfo;
1496c66ac9dbSNicholas Bellinger 
1497c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: %s"
149830c7ca93SDavid Disseldorp 		" Target Node Endpoint: %s\n", tfo->fabric_name,
1499c66ac9dbSNicholas Bellinger 		tfo->tpg_get_wwn(se_tpg));
1500c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
150135d1efe8SMasanari Iida 		" Identifier Tag: %hu %s Portal Group Tag: %hu"
1502f2d30680SHannes Reinecke 		" %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
150330c7ca93SDavid Disseldorp 		tfo->fabric_name, tfo->tpg_get_tag(se_tpg),
150430c7ca93SDavid Disseldorp 		tfo->fabric_name, pr_reg->pr_aptpl_target_lun);
1505c66ac9dbSNicholas Bellinger 
1506d977f437SChristoph Hellwig out_unlock:
1507d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1508c66ac9dbSNicholas Bellinger 	return len;
1509c66ac9dbSNicholas Bellinger }
1510c66ac9dbSNicholas Bellinger 
1511c66ac9dbSNicholas Bellinger 
15122eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
15132eafd729SChristoph Hellwig 		char *page)
1514c66ac9dbSNicholas Bellinger {
15152eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
15169ac8928eSChristoph Hellwig 	const struct target_core_fabric_ops *tfo;
1517c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1518c66ac9dbSNicholas Bellinger 	unsigned char buf[384];
1519c66ac9dbSNicholas Bellinger 	char i_buf[PR_REG_ISID_ID_LEN];
1520c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1521d2843c17SAndy Grover 	int reg_count = 0;
1522c66ac9dbSNicholas Bellinger 
1523c66ac9dbSNicholas Bellinger 	len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1524c66ac9dbSNicholas Bellinger 
15250fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_pr.registration_lock);
15260fd97ccfSChristoph Hellwig 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1527c66ac9dbSNicholas Bellinger 			pr_reg_list) {
1528c66ac9dbSNicholas Bellinger 
1529c66ac9dbSNicholas Bellinger 		memset(buf, 0, 384);
1530c66ac9dbSNicholas Bellinger 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1531c66ac9dbSNicholas Bellinger 		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1532d2843c17SAndy Grover 		core_pr_dump_initiator_port(pr_reg, i_buf,
1533c66ac9dbSNicholas Bellinger 					PR_REG_ISID_ID_LEN);
1534c66ac9dbSNicholas Bellinger 		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
153530c7ca93SDavid Disseldorp 			tfo->fabric_name,
1536d2843c17SAndy Grover 			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1537c66ac9dbSNicholas Bellinger 			pr_reg->pr_res_generation);
1538c66ac9dbSNicholas Bellinger 
15396708bb27SAndy Grover 		if (len + strlen(buf) >= PAGE_SIZE)
1540c66ac9dbSNicholas Bellinger 			break;
1541c66ac9dbSNicholas Bellinger 
1542c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "%s", buf);
1543c66ac9dbSNicholas Bellinger 		reg_count++;
1544c66ac9dbSNicholas Bellinger 	}
15450fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_pr.registration_lock);
1546c66ac9dbSNicholas Bellinger 
15476708bb27SAndy Grover 	if (!reg_count)
1548c66ac9dbSNicholas Bellinger 		len += sprintf(page+len, "None\n");
1549c66ac9dbSNicholas Bellinger 
1550c66ac9dbSNicholas Bellinger 	return len;
1551c66ac9dbSNicholas Bellinger }
1552c66ac9dbSNicholas Bellinger 
15532eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1554c66ac9dbSNicholas Bellinger {
15552eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
1556c66ac9dbSNicholas Bellinger 	struct t10_pr_registration *pr_reg;
1557c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1558c66ac9dbSNicholas Bellinger 
1559c66ac9dbSNicholas Bellinger 	spin_lock(&dev->dev_reservation_lock);
1560c66ac9dbSNicholas Bellinger 	pr_reg = dev->dev_pr_res_holder;
1561d977f437SChristoph Hellwig 	if (pr_reg) {
1562c66ac9dbSNicholas Bellinger 		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1563c66ac9dbSNicholas Bellinger 			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1564d977f437SChristoph Hellwig 	} else {
1565d977f437SChristoph Hellwig 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1566d977f437SChristoph Hellwig 	}
1567c66ac9dbSNicholas Bellinger 
1568d977f437SChristoph Hellwig 	spin_unlock(&dev->dev_reservation_lock);
1569c66ac9dbSNicholas Bellinger 	return len;
1570c66ac9dbSNicholas Bellinger }
1571c66ac9dbSNicholas Bellinger 
15722eafd729SChristoph Hellwig static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
1573c66ac9dbSNicholas Bellinger {
15742eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
15752eafd729SChristoph Hellwig 
1576b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr)
1577b49d6f78SDavid Disseldorp 		return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
15784ec5bf0eSBryant G. Ly 	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1579d977f437SChristoph Hellwig 		return sprintf(page, "SPC_PASSTHROUGH\n");
1580b49d6f78SDavid Disseldorp 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1581d977f437SChristoph Hellwig 		return sprintf(page, "SPC2_RESERVATIONS\n");
1582b49d6f78SDavid Disseldorp 
1583d977f437SChristoph Hellwig 	return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1584c66ac9dbSNicholas Bellinger }
1585c66ac9dbSNicholas Bellinger 
15862eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
15872eafd729SChristoph Hellwig 		char *page)
1588c66ac9dbSNicholas Bellinger {
15892eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
15902eafd729SChristoph Hellwig 
1591b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
1592b49d6f78SDavid Disseldorp 	    (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1593c66ac9dbSNicholas Bellinger 		return 0;
1594c66ac9dbSNicholas Bellinger 
1595c66ac9dbSNicholas Bellinger 	return sprintf(page, "APTPL Bit Status: %s\n",
15960fd97ccfSChristoph Hellwig 		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1597c66ac9dbSNicholas Bellinger }
1598c66ac9dbSNicholas Bellinger 
15992eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
16002eafd729SChristoph Hellwig 		char *page)
1601c66ac9dbSNicholas Bellinger {
16022eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
16032eafd729SChristoph Hellwig 
1604b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
1605b49d6f78SDavid Disseldorp 	    (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1606c66ac9dbSNicholas Bellinger 		return 0;
1607c66ac9dbSNicholas Bellinger 
1608c66ac9dbSNicholas Bellinger 	return sprintf(page, "Ready to process PR APTPL metadata..\n");
1609c66ac9dbSNicholas Bellinger }
1610c66ac9dbSNicholas Bellinger 
1611c66ac9dbSNicholas Bellinger enum {
1612c66ac9dbSNicholas Bellinger 	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1613c66ac9dbSNicholas Bellinger 	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1614c66ac9dbSNicholas Bellinger 	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1615c66ac9dbSNicholas Bellinger 	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1616c66ac9dbSNicholas Bellinger };
1617c66ac9dbSNicholas Bellinger 
1618c66ac9dbSNicholas Bellinger static match_table_t tokens = {
1619c66ac9dbSNicholas Bellinger 	{Opt_initiator_fabric, "initiator_fabric=%s"},
1620c66ac9dbSNicholas Bellinger 	{Opt_initiator_node, "initiator_node=%s"},
1621c66ac9dbSNicholas Bellinger 	{Opt_initiator_sid, "initiator_sid=%s"},
1622c66ac9dbSNicholas Bellinger 	{Opt_sa_res_key, "sa_res_key=%s"},
1623c66ac9dbSNicholas Bellinger 	{Opt_res_holder, "res_holder=%d"},
1624c66ac9dbSNicholas Bellinger 	{Opt_res_type, "res_type=%d"},
1625c66ac9dbSNicholas Bellinger 	{Opt_res_scope, "res_scope=%d"},
1626c66ac9dbSNicholas Bellinger 	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1627a2db857bStangwenji 	{Opt_mapped_lun, "mapped_lun=%u"},
1628c66ac9dbSNicholas Bellinger 	{Opt_target_fabric, "target_fabric=%s"},
1629c66ac9dbSNicholas Bellinger 	{Opt_target_node, "target_node=%s"},
1630c66ac9dbSNicholas Bellinger 	{Opt_tpgt, "tpgt=%d"},
1631c66ac9dbSNicholas Bellinger 	{Opt_port_rtpi, "port_rtpi=%d"},
1632a2db857bStangwenji 	{Opt_target_lun, "target_lun=%u"},
1633c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
1634c66ac9dbSNicholas Bellinger };
1635c66ac9dbSNicholas Bellinger 
16362eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
16372eafd729SChristoph Hellwig 		const char *page, size_t count)
1638c66ac9dbSNicholas Bellinger {
16392eafd729SChristoph Hellwig 	struct se_device *dev = pr_to_dev(item);
16406d180253SJesper Juhl 	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
16416d180253SJesper Juhl 	unsigned char *t_fabric = NULL, *t_port = NULL;
16428d213559SJoern Engel 	char *orig, *ptr, *opts;
1643c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
1644c66ac9dbSNicholas Bellinger 	unsigned long long tmp_ll;
1645c66ac9dbSNicholas Bellinger 	u64 sa_res_key = 0;
1646f2d30680SHannes Reinecke 	u64 mapped_lun = 0, target_lun = 0;
1647c66ac9dbSNicholas Bellinger 	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
164845fb94c2SBart Van Assche 	u16 tpgt = 0;
164945fb94c2SBart Van Assche 	u8 type = 0;
1650c66ac9dbSNicholas Bellinger 
1651b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr ||
1652b49d6f78SDavid Disseldorp 	    (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
16539105bfc0SAndy Grover 		return count;
1654d977f437SChristoph Hellwig 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
16559105bfc0SAndy Grover 		return count;
1656c66ac9dbSNicholas Bellinger 
16570fd97ccfSChristoph Hellwig 	if (dev->export_count) {
16586708bb27SAndy Grover 		pr_debug("Unable to process APTPL metadata while"
1659c66ac9dbSNicholas Bellinger 			" active fabric exports exist\n");
1660c66ac9dbSNicholas Bellinger 		return -EINVAL;
1661c66ac9dbSNicholas Bellinger 	}
1662c66ac9dbSNicholas Bellinger 
1663c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
1664c66ac9dbSNicholas Bellinger 	if (!opts)
1665c66ac9dbSNicholas Bellinger 		return -ENOMEM;
1666c66ac9dbSNicholas Bellinger 
1667c66ac9dbSNicholas Bellinger 	orig = opts;
166890c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
1669c66ac9dbSNicholas Bellinger 		if (!*ptr)
1670c66ac9dbSNicholas Bellinger 			continue;
1671c66ac9dbSNicholas Bellinger 
1672c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
1673c66ac9dbSNicholas Bellinger 		switch (token) {
1674c66ac9dbSNicholas Bellinger 		case Opt_initiator_fabric:
16758d213559SJoern Engel 			i_fabric = match_strdup(args);
16766d180253SJesper Juhl 			if (!i_fabric) {
16776d180253SJesper Juhl 				ret = -ENOMEM;
16786d180253SJesper Juhl 				goto out;
16796d180253SJesper Juhl 			}
1680c66ac9dbSNicholas Bellinger 			break;
1681c66ac9dbSNicholas Bellinger 		case Opt_initiator_node:
16828d213559SJoern Engel 			i_port = match_strdup(args);
16836d180253SJesper Juhl 			if (!i_port) {
16846d180253SJesper Juhl 				ret = -ENOMEM;
16856d180253SJesper Juhl 				goto out;
16866d180253SJesper Juhl 			}
168760d645a4SDan Carpenter 			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
16886708bb27SAndy Grover 				pr_err("APTPL metadata initiator_node="
1689c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1690c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_IPORT_LEN);
1691c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1692c66ac9dbSNicholas Bellinger 				break;
1693c66ac9dbSNicholas Bellinger 			}
1694c66ac9dbSNicholas Bellinger 			break;
1695c66ac9dbSNicholas Bellinger 		case Opt_initiator_sid:
16968d213559SJoern Engel 			isid = match_strdup(args);
16976d180253SJesper Juhl 			if (!isid) {
16986d180253SJesper Juhl 				ret = -ENOMEM;
16996d180253SJesper Juhl 				goto out;
17006d180253SJesper Juhl 			}
170160d645a4SDan Carpenter 			if (strlen(isid) >= PR_REG_ISID_LEN) {
17026708bb27SAndy Grover 				pr_err("APTPL metadata initiator_isid"
1703c66ac9dbSNicholas Bellinger 					"= exceeds PR_REG_ISID_LEN: %d\n",
1704c66ac9dbSNicholas Bellinger 					PR_REG_ISID_LEN);
1705c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1706c66ac9dbSNicholas Bellinger 				break;
1707c66ac9dbSNicholas Bellinger 			}
1708c66ac9dbSNicholas Bellinger 			break;
1709c66ac9dbSNicholas Bellinger 		case Opt_sa_res_key:
1710a2db857bStangwenji 			ret = match_u64(args,  &tmp_ll);
1711c66ac9dbSNicholas Bellinger 			if (ret < 0) {
17128d213559SJoern Engel 				pr_err("kstrtoull() failed for sa_res_key=\n");
1713c66ac9dbSNicholas Bellinger 				goto out;
1714c66ac9dbSNicholas Bellinger 			}
1715c66ac9dbSNicholas Bellinger 			sa_res_key = (u64)tmp_ll;
1716c66ac9dbSNicholas Bellinger 			break;
1717c66ac9dbSNicholas Bellinger 		/*
1718c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Reservation
1719c66ac9dbSNicholas Bellinger 		 */
1720c66ac9dbSNicholas Bellinger 		case Opt_res_holder:
1721c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1722c2091026SDavid Disseldorp 			if (ret)
1723c2091026SDavid Disseldorp 				goto out;
1724c66ac9dbSNicholas Bellinger 			res_holder = arg;
1725c66ac9dbSNicholas Bellinger 			break;
1726c66ac9dbSNicholas Bellinger 		case Opt_res_type:
1727c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1728c2091026SDavid Disseldorp 			if (ret)
1729c2091026SDavid Disseldorp 				goto out;
1730c66ac9dbSNicholas Bellinger 			type = (u8)arg;
1731c66ac9dbSNicholas Bellinger 			break;
1732c66ac9dbSNicholas Bellinger 		case Opt_res_scope:
1733c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1734c2091026SDavid Disseldorp 			if (ret)
1735c2091026SDavid Disseldorp 				goto out;
1736c66ac9dbSNicholas Bellinger 			break;
1737c66ac9dbSNicholas Bellinger 		case Opt_res_all_tg_pt:
1738c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1739c2091026SDavid Disseldorp 			if (ret)
1740c2091026SDavid Disseldorp 				goto out;
1741c66ac9dbSNicholas Bellinger 			all_tg_pt = (int)arg;
1742c66ac9dbSNicholas Bellinger 			break;
1743c66ac9dbSNicholas Bellinger 		case Opt_mapped_lun:
1744a2db857bStangwenji 			ret = match_u64(args, &tmp_ll);
1745c2091026SDavid Disseldorp 			if (ret)
1746c2091026SDavid Disseldorp 				goto out;
1747a2db857bStangwenji 			mapped_lun = (u64)tmp_ll;
1748c66ac9dbSNicholas Bellinger 			break;
1749c66ac9dbSNicholas Bellinger 		/*
1750c66ac9dbSNicholas Bellinger 		 * PR APTPL Metadata for Target Port
1751c66ac9dbSNicholas Bellinger 		 */
1752c66ac9dbSNicholas Bellinger 		case Opt_target_fabric:
17538d213559SJoern Engel 			t_fabric = match_strdup(args);
17546d180253SJesper Juhl 			if (!t_fabric) {
17556d180253SJesper Juhl 				ret = -ENOMEM;
17566d180253SJesper Juhl 				goto out;
17576d180253SJesper Juhl 			}
1758c66ac9dbSNicholas Bellinger 			break;
1759c66ac9dbSNicholas Bellinger 		case Opt_target_node:
17608d213559SJoern Engel 			t_port = match_strdup(args);
17616d180253SJesper Juhl 			if (!t_port) {
17626d180253SJesper Juhl 				ret = -ENOMEM;
17636d180253SJesper Juhl 				goto out;
17646d180253SJesper Juhl 			}
176560d645a4SDan Carpenter 			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
17666708bb27SAndy Grover 				pr_err("APTPL metadata target_node="
1767c66ac9dbSNicholas Bellinger 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1768c66ac9dbSNicholas Bellinger 					PR_APTPL_MAX_TPORT_LEN);
1769c66ac9dbSNicholas Bellinger 				ret = -EINVAL;
1770c66ac9dbSNicholas Bellinger 				break;
1771c66ac9dbSNicholas Bellinger 			}
1772c66ac9dbSNicholas Bellinger 			break;
1773c66ac9dbSNicholas Bellinger 		case Opt_tpgt:
1774c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1775c2091026SDavid Disseldorp 			if (ret)
1776c2091026SDavid Disseldorp 				goto out;
1777c66ac9dbSNicholas Bellinger 			tpgt = (u16)arg;
1778c66ac9dbSNicholas Bellinger 			break;
1779c66ac9dbSNicholas Bellinger 		case Opt_port_rtpi:
1780c2091026SDavid Disseldorp 			ret = match_int(args, &arg);
1781c2091026SDavid Disseldorp 			if (ret)
1782c2091026SDavid Disseldorp 				goto out;
1783c66ac9dbSNicholas Bellinger 			break;
1784c66ac9dbSNicholas Bellinger 		case Opt_target_lun:
1785a2db857bStangwenji 			ret = match_u64(args, &tmp_ll);
1786c2091026SDavid Disseldorp 			if (ret)
1787c2091026SDavid Disseldorp 				goto out;
1788a2db857bStangwenji 			target_lun = (u64)tmp_ll;
1789c66ac9dbSNicholas Bellinger 			break;
1790c66ac9dbSNicholas Bellinger 		default:
1791c66ac9dbSNicholas Bellinger 			break;
1792c66ac9dbSNicholas Bellinger 		}
1793c66ac9dbSNicholas Bellinger 	}
1794c66ac9dbSNicholas Bellinger 
17956708bb27SAndy Grover 	if (!i_port || !t_port || !sa_res_key) {
17966708bb27SAndy Grover 		pr_err("Illegal parameters for APTPL registration\n");
1797c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1798c66ac9dbSNicholas Bellinger 		goto out;
1799c66ac9dbSNicholas Bellinger 	}
1800c66ac9dbSNicholas Bellinger 
1801c66ac9dbSNicholas Bellinger 	if (res_holder && !(type)) {
18026708bb27SAndy Grover 		pr_err("Illegal PR type: 0x%02x for reservation"
1803c66ac9dbSNicholas Bellinger 				" holder\n", type);
1804c66ac9dbSNicholas Bellinger 		ret = -EINVAL;
1805c66ac9dbSNicholas Bellinger 		goto out;
1806c66ac9dbSNicholas Bellinger 	}
1807c66ac9dbSNicholas Bellinger 
18080fd97ccfSChristoph Hellwig 	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1809c66ac9dbSNicholas Bellinger 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1810c66ac9dbSNicholas Bellinger 			res_holder, all_tg_pt, type);
1811c66ac9dbSNicholas Bellinger out:
18126d180253SJesper Juhl 	kfree(i_fabric);
18136d180253SJesper Juhl 	kfree(i_port);
18146d180253SJesper Juhl 	kfree(isid);
18156d180253SJesper Juhl 	kfree(t_fabric);
18166d180253SJesper Juhl 	kfree(t_port);
1817c66ac9dbSNicholas Bellinger 	kfree(orig);
1818c66ac9dbSNicholas Bellinger 	return (ret == 0) ? count : ret;
1819c66ac9dbSNicholas Bellinger }
1820c66ac9dbSNicholas Bellinger 
1821c66ac9dbSNicholas Bellinger 
18222eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_holder);
18232eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
18242eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
18252eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
18262eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
18272eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
18282eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_type);
18292eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
18302eafd729SChristoph Hellwig CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
1831c66ac9dbSNicholas Bellinger 
1832c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_pr_attrs[] = {
18332eafd729SChristoph Hellwig 	&target_pr_attr_res_holder,
18342eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_all_tgt_pts,
18352eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_generation,
18362eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_holder_tg_port,
18372eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_registered_i_pts,
18382eafd729SChristoph Hellwig 	&target_pr_attr_res_pr_type,
18392eafd729SChristoph Hellwig 	&target_pr_attr_res_type,
18402eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_active,
18412eafd729SChristoph Hellwig 	&target_pr_attr_res_aptpl_metadata,
1842c66ac9dbSNicholas Bellinger 	NULL,
1843c66ac9dbSNicholas Bellinger };
1844c66ac9dbSNicholas Bellinger 
18452eafd729SChristoph Hellwig TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
1846c66ac9dbSNicholas Bellinger 
184791e2e39bSNicholas Bellinger /*  End functions for struct config_item_type tb_dev_pr_cit */
1848c66ac9dbSNicholas Bellinger 
184973112edcSNicholas Bellinger /*  Start functions for struct config_item_type tb_dev_cit */
1850c66ac9dbSNicholas Bellinger 
18512eafd729SChristoph Hellwig static inline struct se_device *to_device(struct config_item *item)
1852c66ac9dbSNicholas Bellinger {
18532eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_device, dev_group);
18542eafd729SChristoph Hellwig }
18552eafd729SChristoph Hellwig 
18562eafd729SChristoph Hellwig static ssize_t target_dev_info_show(struct config_item *item, char *page)
18572eafd729SChristoph Hellwig {
18582eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1859c66ac9dbSNicholas Bellinger 	int bl = 0;
1860c66ac9dbSNicholas Bellinger 	ssize_t read_bytes = 0;
1861c66ac9dbSNicholas Bellinger 
18620fd97ccfSChristoph Hellwig 	transport_dump_dev_state(dev, page, &bl);
1863c66ac9dbSNicholas Bellinger 	read_bytes += bl;
18640a06d430SChristoph Hellwig 	read_bytes += dev->transport->show_configfs_dev_params(dev,
18650a06d430SChristoph Hellwig 			page+read_bytes);
1866c66ac9dbSNicholas Bellinger 	return read_bytes;
1867c66ac9dbSNicholas Bellinger }
1868c66ac9dbSNicholas Bellinger 
18692eafd729SChristoph Hellwig static ssize_t target_dev_control_store(struct config_item *item,
18702eafd729SChristoph Hellwig 		const char *page, size_t count)
1871c66ac9dbSNicholas Bellinger {
18722eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1873c66ac9dbSNicholas Bellinger 
18740a06d430SChristoph Hellwig 	return dev->transport->set_configfs_dev_params(dev, page, count);
1875c66ac9dbSNicholas Bellinger }
1876c66ac9dbSNicholas Bellinger 
18772eafd729SChristoph Hellwig static ssize_t target_dev_alias_show(struct config_item *item, char *page)
1878c66ac9dbSNicholas Bellinger {
18792eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1880c66ac9dbSNicholas Bellinger 
18810fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_ALIAS))
1882c66ac9dbSNicholas Bellinger 		return 0;
1883c66ac9dbSNicholas Bellinger 
18840fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1885c66ac9dbSNicholas Bellinger }
1886c66ac9dbSNicholas Bellinger 
18872eafd729SChristoph Hellwig static ssize_t target_dev_alias_store(struct config_item *item,
18882eafd729SChristoph Hellwig 		const char *page, size_t count)
1889c66ac9dbSNicholas Bellinger {
18902eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
18910fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1892c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1893c66ac9dbSNicholas Bellinger 
1894c66ac9dbSNicholas Bellinger 	if (count > (SE_DEV_ALIAS_LEN-1)) {
18956708bb27SAndy Grover 		pr_err("alias count: %d exceeds"
1896c66ac9dbSNicholas Bellinger 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1897c66ac9dbSNicholas Bellinger 			SE_DEV_ALIAS_LEN-1);
1898c66ac9dbSNicholas Bellinger 		return -EINVAL;
1899c66ac9dbSNicholas Bellinger 	}
1900c66ac9dbSNicholas Bellinger 
19010fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
19023011684cSDan Carpenter 	if (!read_bytes)
19033011684cSDan Carpenter 		return -EINVAL;
19040fd97ccfSChristoph Hellwig 	if (dev->dev_alias[read_bytes - 1] == '\n')
19050fd97ccfSChristoph Hellwig 		dev->dev_alias[read_bytes - 1] = '\0';
19060877eafdSSebastian Andrzej Siewior 
19070fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_ALIAS;
19083011684cSDan Carpenter 
19096708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1910c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
19110fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
19120fd97ccfSChristoph Hellwig 		dev->dev_alias);
1913c66ac9dbSNicholas Bellinger 
1914c66ac9dbSNicholas Bellinger 	return read_bytes;
1915c66ac9dbSNicholas Bellinger }
1916c66ac9dbSNicholas Bellinger 
19172eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
1918c66ac9dbSNicholas Bellinger {
19192eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1920c66ac9dbSNicholas Bellinger 
19210fd97ccfSChristoph Hellwig 	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1922c66ac9dbSNicholas Bellinger 		return 0;
1923c66ac9dbSNicholas Bellinger 
19240fd97ccfSChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1925c66ac9dbSNicholas Bellinger }
1926c66ac9dbSNicholas Bellinger 
19272eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_store(struct config_item *item,
19282eafd729SChristoph Hellwig 		const char *page, size_t count)
1929c66ac9dbSNicholas Bellinger {
19302eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
19310fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
1932c66ac9dbSNicholas Bellinger 	ssize_t read_bytes;
1933c66ac9dbSNicholas Bellinger 
1934c66ac9dbSNicholas Bellinger 	if (count > (SE_UDEV_PATH_LEN-1)) {
19356708bb27SAndy Grover 		pr_err("udev_path count: %d exceeds"
1936c66ac9dbSNicholas Bellinger 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1937c66ac9dbSNicholas Bellinger 			SE_UDEV_PATH_LEN-1);
1938c66ac9dbSNicholas Bellinger 		return -EINVAL;
1939c66ac9dbSNicholas Bellinger 	}
1940c66ac9dbSNicholas Bellinger 
19410fd97ccfSChristoph Hellwig 	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1942c66ac9dbSNicholas Bellinger 			"%s", page);
19433011684cSDan Carpenter 	if (!read_bytes)
19443011684cSDan Carpenter 		return -EINVAL;
19450fd97ccfSChristoph Hellwig 	if (dev->udev_path[read_bytes - 1] == '\n')
19460fd97ccfSChristoph Hellwig 		dev->udev_path[read_bytes - 1] = '\0';
19470877eafdSSebastian Andrzej Siewior 
19480fd97ccfSChristoph Hellwig 	dev->dev_flags |= DF_USING_UDEV_PATH;
19493011684cSDan Carpenter 
19506708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1951c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
19520fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
19530fd97ccfSChristoph Hellwig 		dev->udev_path);
1954c66ac9dbSNicholas Bellinger 
1955c66ac9dbSNicholas Bellinger 	return read_bytes;
1956c66ac9dbSNicholas Bellinger }
1957c66ac9dbSNicholas Bellinger 
19582eafd729SChristoph Hellwig static ssize_t target_dev_enable_show(struct config_item *item, char *page)
195964146db7SAndy Grover {
19602eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
196164146db7SAndy Grover 
1962cb0f32e1SMike Christie 	return snprintf(page, PAGE_SIZE, "%d\n", target_dev_configured(dev));
196364146db7SAndy Grover }
196464146db7SAndy Grover 
19652eafd729SChristoph Hellwig static ssize_t target_dev_enable_store(struct config_item *item,
19662eafd729SChristoph Hellwig 		const char *page, size_t count)
1967c66ac9dbSNicholas Bellinger {
19682eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1969c66ac9dbSNicholas Bellinger 	char *ptr;
19700fd97ccfSChristoph Hellwig 	int ret;
1971c66ac9dbSNicholas Bellinger 
1972c66ac9dbSNicholas Bellinger 	ptr = strstr(page, "1");
19736708bb27SAndy Grover 	if (!ptr) {
19746708bb27SAndy Grover 		pr_err("For dev_enable ops, only valid value"
1975c66ac9dbSNicholas Bellinger 				" is \"1\"\n");
1976c66ac9dbSNicholas Bellinger 		return -EINVAL;
1977c66ac9dbSNicholas Bellinger 	}
1978c66ac9dbSNicholas Bellinger 
19790fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
19800fd97ccfSChristoph Hellwig 	if (ret)
19810fd97ccfSChristoph Hellwig 		return ret;
1982c66ac9dbSNicholas Bellinger 	return count;
1983c66ac9dbSNicholas Bellinger }
1984c66ac9dbSNicholas Bellinger 
19852eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
1986c66ac9dbSNicholas Bellinger {
19872eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
1988c66ac9dbSNicholas Bellinger 	struct config_item *lu_ci;
1989c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
1990c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
1991c66ac9dbSNicholas Bellinger 	ssize_t len = 0;
1992c66ac9dbSNicholas Bellinger 
1993c66ac9dbSNicholas Bellinger 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1994c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
1995c87fbd56SChristoph Hellwig 		return 0;
1996c66ac9dbSNicholas Bellinger 
1997c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1998c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
19996708bb27SAndy Grover 	if (lu_gp) {
2000c66ac9dbSNicholas Bellinger 		lu_ci = &lu_gp->lu_gp_group.cg_item;
2001c66ac9dbSNicholas Bellinger 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
2002c66ac9dbSNicholas Bellinger 			config_item_name(lu_ci), lu_gp->lu_gp_id);
2003c66ac9dbSNicholas Bellinger 	}
2004c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2005c66ac9dbSNicholas Bellinger 
2006c66ac9dbSNicholas Bellinger 	return len;
2007c66ac9dbSNicholas Bellinger }
2008c66ac9dbSNicholas Bellinger 
20092eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
20102eafd729SChristoph Hellwig 		const char *page, size_t count)
2011c66ac9dbSNicholas Bellinger {
20122eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
20130fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
2014c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
2015c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2016c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
2017c66ac9dbSNicholas Bellinger 	int move = 0;
2018c66ac9dbSNicholas Bellinger 
2019c87fbd56SChristoph Hellwig 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
2020c87fbd56SChristoph Hellwig 	if (!lu_gp_mem)
20219105bfc0SAndy Grover 		return count;
2022c87fbd56SChristoph Hellwig 
2023c66ac9dbSNicholas Bellinger 	if (count > LU_GROUP_NAME_BUF) {
20246708bb27SAndy Grover 		pr_err("ALUA LU Group Alias too large!\n");
2025c66ac9dbSNicholas Bellinger 		return -EINVAL;
2026c66ac9dbSNicholas Bellinger 	}
2027c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
2028c66ac9dbSNicholas Bellinger 	memcpy(buf, page, count);
2029c66ac9dbSNicholas Bellinger 	/*
2030c66ac9dbSNicholas Bellinger 	 * Any ALUA logical unit alias besides "NULL" means we will be
2031c66ac9dbSNicholas Bellinger 	 * making a new group association.
2032c66ac9dbSNicholas Bellinger 	 */
2033c66ac9dbSNicholas Bellinger 	if (strcmp(strstrip(buf), "NULL")) {
2034c66ac9dbSNicholas Bellinger 		/*
2035c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name() will increment reference to
2036c66ac9dbSNicholas Bellinger 		 * struct t10_alua_lu_gp.  This reference is released with
2037c66ac9dbSNicholas Bellinger 		 * core_alua_get_lu_gp_by_name below().
2038c66ac9dbSNicholas Bellinger 		 */
2039c66ac9dbSNicholas Bellinger 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
20406708bb27SAndy Grover 		if (!lu_gp_new)
2041c66ac9dbSNicholas Bellinger 			return -ENODEV;
2042c66ac9dbSNicholas Bellinger 	}
2043c66ac9dbSNicholas Bellinger 
2044c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2045c66ac9dbSNicholas Bellinger 	lu_gp = lu_gp_mem->lu_gp;
20466708bb27SAndy Grover 	if (lu_gp) {
2047c66ac9dbSNicholas Bellinger 		/*
2048c66ac9dbSNicholas Bellinger 		 * Clearing an existing lu_gp association, and replacing
2049c66ac9dbSNicholas Bellinger 		 * with NULL
2050c66ac9dbSNicholas Bellinger 		 */
20516708bb27SAndy Grover 		if (!lu_gp_new) {
20526708bb27SAndy Grover 			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
2053c66ac9dbSNicholas Bellinger 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2054c66ac9dbSNicholas Bellinger 				" %hu\n",
2055c66ac9dbSNicholas Bellinger 				config_item_name(&hba->hba_group.cg_item),
20560fd97ccfSChristoph Hellwig 				config_item_name(&dev->dev_group.cg_item),
2057c66ac9dbSNicholas Bellinger 				config_item_name(&lu_gp->lu_gp_group.cg_item),
2058c66ac9dbSNicholas Bellinger 				lu_gp->lu_gp_id);
2059c66ac9dbSNicholas Bellinger 
2060c66ac9dbSNicholas Bellinger 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2061c66ac9dbSNicholas Bellinger 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2062c66ac9dbSNicholas Bellinger 
2063c66ac9dbSNicholas Bellinger 			return count;
2064c66ac9dbSNicholas Bellinger 		}
2065c66ac9dbSNicholas Bellinger 		/*
2066c66ac9dbSNicholas Bellinger 		 * Removing existing association of lu_gp_mem with lu_gp
2067c66ac9dbSNicholas Bellinger 		 */
2068c66ac9dbSNicholas Bellinger 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2069c66ac9dbSNicholas Bellinger 		move = 1;
2070c66ac9dbSNicholas Bellinger 	}
2071c66ac9dbSNicholas Bellinger 	/*
2072c66ac9dbSNicholas Bellinger 	 * Associate lu_gp_mem with lu_gp_new.
2073c66ac9dbSNicholas Bellinger 	 */
2074c66ac9dbSNicholas Bellinger 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2075c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2076c66ac9dbSNicholas Bellinger 
20776708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
2078c66ac9dbSNicholas Bellinger 		" core/alua/lu_gps/%s, ID: %hu\n",
2079c66ac9dbSNicholas Bellinger 		(move) ? "Moving" : "Adding",
2080c66ac9dbSNicholas Bellinger 		config_item_name(&hba->hba_group.cg_item),
20810fd97ccfSChristoph Hellwig 		config_item_name(&dev->dev_group.cg_item),
2082c66ac9dbSNicholas Bellinger 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2083c66ac9dbSNicholas Bellinger 		lu_gp_new->lu_gp_id);
2084c66ac9dbSNicholas Bellinger 
2085c66ac9dbSNicholas Bellinger 	core_alua_put_lu_gp_from_name(lu_gp_new);
2086c66ac9dbSNicholas Bellinger 	return count;
2087c66ac9dbSNicholas Bellinger }
2088c66ac9dbSNicholas Bellinger 
20892eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
2090229d4f11SHannes Reinecke {
20912eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2092229d4f11SHannes Reinecke 	struct t10_alua_lba_map *map;
2093229d4f11SHannes Reinecke 	struct t10_alua_lba_map_member *mem;
2094229d4f11SHannes Reinecke 	char *b = page;
2095229d4f11SHannes Reinecke 	int bl = 0;
2096229d4f11SHannes Reinecke 	char state;
2097229d4f11SHannes Reinecke 
2098229d4f11SHannes Reinecke 	spin_lock(&dev->t10_alua.lba_map_lock);
2099229d4f11SHannes Reinecke 	if (!list_empty(&dev->t10_alua.lba_map_list))
2100229d4f11SHannes Reinecke 	    bl += sprintf(b + bl, "%u %u\n",
2101229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_size,
2102229d4f11SHannes Reinecke 			  dev->t10_alua.lba_map_segment_multiplier);
2103229d4f11SHannes Reinecke 	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2104229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "%llu %llu",
2105229d4f11SHannes Reinecke 			      map->lba_map_first_lba, map->lba_map_last_lba);
2106229d4f11SHannes Reinecke 		list_for_each_entry(mem, &map->lba_map_mem_list,
2107229d4f11SHannes Reinecke 				    lba_map_mem_list) {
2108229d4f11SHannes Reinecke 			switch (mem->lba_map_mem_alua_state) {
2109229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2110229d4f11SHannes Reinecke 				state = 'O';
2111229d4f11SHannes Reinecke 				break;
2112229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2113229d4f11SHannes Reinecke 				state = 'A';
2114229d4f11SHannes Reinecke 				break;
2115229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_STANDBY:
2116229d4f11SHannes Reinecke 				state = 'S';
2117229d4f11SHannes Reinecke 				break;
2118229d4f11SHannes Reinecke 			case ALUA_ACCESS_STATE_UNAVAILABLE:
2119229d4f11SHannes Reinecke 				state = 'U';
2120229d4f11SHannes Reinecke 				break;
2121229d4f11SHannes Reinecke 			default:
2122229d4f11SHannes Reinecke 				state = '.';
2123229d4f11SHannes Reinecke 				break;
2124229d4f11SHannes Reinecke 			}
2125229d4f11SHannes Reinecke 			bl += sprintf(b + bl, " %d:%c",
2126229d4f11SHannes Reinecke 				      mem->lba_map_mem_alua_pg_id, state);
2127229d4f11SHannes Reinecke 		}
2128229d4f11SHannes Reinecke 		bl += sprintf(b + bl, "\n");
2129229d4f11SHannes Reinecke 	}
2130229d4f11SHannes Reinecke 	spin_unlock(&dev->t10_alua.lba_map_lock);
2131229d4f11SHannes Reinecke 	return bl;
2132229d4f11SHannes Reinecke }
2133229d4f11SHannes Reinecke 
21342eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_store(struct config_item *item,
21352eafd729SChristoph Hellwig 		const char *page, size_t count)
2136229d4f11SHannes Reinecke {
21372eafd729SChristoph Hellwig 	struct se_device *dev = to_device(item);
2138229d4f11SHannes Reinecke 	struct t10_alua_lba_map *lba_map = NULL;
2139229d4f11SHannes Reinecke 	struct list_head lba_list;
2140f0a8afecSBart Van Assche 	char *map_entries, *orig, *ptr;
2141229d4f11SHannes Reinecke 	char state;
2142229d4f11SHannes Reinecke 	int pg_num = -1, pg;
2143229d4f11SHannes Reinecke 	int ret = 0, num = 0, pg_id, alua_state;
2144229d4f11SHannes Reinecke 	unsigned long start_lba = -1, end_lba = -1;
2145229d4f11SHannes Reinecke 	unsigned long segment_size = -1, segment_mult = -1;
2146229d4f11SHannes Reinecke 
2147f0a8afecSBart Van Assche 	orig = map_entries = kstrdup(page, GFP_KERNEL);
2148229d4f11SHannes Reinecke 	if (!map_entries)
2149229d4f11SHannes Reinecke 		return -ENOMEM;
2150229d4f11SHannes Reinecke 
2151229d4f11SHannes Reinecke 	INIT_LIST_HEAD(&lba_list);
2152229d4f11SHannes Reinecke 	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2153229d4f11SHannes Reinecke 		if (!*ptr)
2154229d4f11SHannes Reinecke 			continue;
2155229d4f11SHannes Reinecke 
2156229d4f11SHannes Reinecke 		if (num == 0) {
2157229d4f11SHannes Reinecke 			if (sscanf(ptr, "%lu %lu\n",
2158229d4f11SHannes Reinecke 				   &segment_size, &segment_mult) != 2) {
2159229d4f11SHannes Reinecke 				pr_err("Invalid line %d\n", num);
2160229d4f11SHannes Reinecke 				ret = -EINVAL;
2161229d4f11SHannes Reinecke 				break;
2162229d4f11SHannes Reinecke 			}
2163229d4f11SHannes Reinecke 			num++;
2164229d4f11SHannes Reinecke 			continue;
2165229d4f11SHannes Reinecke 		}
2166229d4f11SHannes Reinecke 		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2167229d4f11SHannes Reinecke 			pr_err("Invalid line %d\n", num);
2168229d4f11SHannes Reinecke 			ret = -EINVAL;
2169229d4f11SHannes Reinecke 			break;
2170229d4f11SHannes Reinecke 		}
2171229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2172229d4f11SHannes Reinecke 		if (!ptr) {
2173229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing end lba\n", num);
2174229d4f11SHannes Reinecke 			ret = -EINVAL;
2175229d4f11SHannes Reinecke 			break;
2176229d4f11SHannes Reinecke 		}
2177229d4f11SHannes Reinecke 		ptr++;
2178229d4f11SHannes Reinecke 		ptr = strchr(ptr, ' ');
2179229d4f11SHannes Reinecke 		if (!ptr) {
2180229d4f11SHannes Reinecke 			pr_err("Invalid line %d, missing state definitions\n",
2181229d4f11SHannes Reinecke 			       num);
2182229d4f11SHannes Reinecke 			ret = -EINVAL;
2183229d4f11SHannes Reinecke 			break;
2184229d4f11SHannes Reinecke 		}
2185229d4f11SHannes Reinecke 		ptr++;
2186229d4f11SHannes Reinecke 		lba_map = core_alua_allocate_lba_map(&lba_list,
2187229d4f11SHannes Reinecke 						     start_lba, end_lba);
2188229d4f11SHannes Reinecke 		if (IS_ERR(lba_map)) {
2189229d4f11SHannes Reinecke 			ret = PTR_ERR(lba_map);
2190229d4f11SHannes Reinecke 			break;
2191229d4f11SHannes Reinecke 		}
2192229d4f11SHannes Reinecke 		pg = 0;
2193229d4f11SHannes Reinecke 		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2194229d4f11SHannes Reinecke 			switch (state) {
2195229d4f11SHannes Reinecke 			case 'O':
2196229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2197229d4f11SHannes Reinecke 				break;
2198229d4f11SHannes Reinecke 			case 'A':
2199229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2200229d4f11SHannes Reinecke 				break;
2201229d4f11SHannes Reinecke 			case 'S':
2202229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_STANDBY;
2203229d4f11SHannes Reinecke 				break;
2204229d4f11SHannes Reinecke 			case 'U':
2205229d4f11SHannes Reinecke 				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2206229d4f11SHannes Reinecke 				break;
2207229d4f11SHannes Reinecke 			default:
2208229d4f11SHannes Reinecke 				pr_err("Invalid ALUA state '%c'\n", state);
2209229d4f11SHannes Reinecke 				ret = -EINVAL;
2210229d4f11SHannes Reinecke 				goto out;
2211229d4f11SHannes Reinecke 			}
2212229d4f11SHannes Reinecke 
2213229d4f11SHannes Reinecke 			ret = core_alua_allocate_lba_map_mem(lba_map,
2214229d4f11SHannes Reinecke 							     pg_id, alua_state);
2215229d4f11SHannes Reinecke 			if (ret) {
2216229d4f11SHannes Reinecke 				pr_err("Invalid target descriptor %d:%c "
2217229d4f11SHannes Reinecke 				       "at line %d\n",
2218229d4f11SHannes Reinecke 				       pg_id, state, num);
2219229d4f11SHannes Reinecke 				break;
2220229d4f11SHannes Reinecke 			}
2221229d4f11SHannes Reinecke 			pg++;
2222229d4f11SHannes Reinecke 			ptr = strchr(ptr, ' ');
2223229d4f11SHannes Reinecke 			if (ptr)
2224229d4f11SHannes Reinecke 				ptr++;
2225229d4f11SHannes Reinecke 			else
2226229d4f11SHannes Reinecke 				break;
2227229d4f11SHannes Reinecke 		}
2228229d4f11SHannes Reinecke 		if (pg_num == -1)
2229229d4f11SHannes Reinecke 		    pg_num = pg;
2230229d4f11SHannes Reinecke 		else if (pg != pg_num) {
2231229d4f11SHannes Reinecke 			pr_err("Only %d from %d port groups definitions "
2232229d4f11SHannes Reinecke 			       "at line %d\n", pg, pg_num, num);
2233229d4f11SHannes Reinecke 			ret = -EINVAL;
2234229d4f11SHannes Reinecke 			break;
2235229d4f11SHannes Reinecke 		}
2236229d4f11SHannes Reinecke 		num++;
2237229d4f11SHannes Reinecke 	}
2238229d4f11SHannes Reinecke out:
2239229d4f11SHannes Reinecke 	if (ret) {
2240229d4f11SHannes Reinecke 		core_alua_free_lba_map(&lba_list);
2241229d4f11SHannes Reinecke 		count = ret;
2242229d4f11SHannes Reinecke 	} else
2243229d4f11SHannes Reinecke 		core_alua_set_lba_map(dev, &lba_list,
2244229d4f11SHannes Reinecke 				      segment_size, segment_mult);
2245f0a8afecSBart Van Assche 	kfree(orig);
2246229d4f11SHannes Reinecke 	return count;
2247229d4f11SHannes Reinecke }
2248229d4f11SHannes Reinecke 
22492eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_dev_, info);
22502eafd729SChristoph Hellwig CONFIGFS_ATTR_WO(target_dev_, control);
22512eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alias);
22522eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, udev_path);
22532eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, enable);
22542eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alua_lu_gp);
22552eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, lba_map);
2256229d4f11SHannes Reinecke 
225773112edcSNicholas Bellinger static struct configfs_attribute *target_core_dev_attrs[] = {
22582eafd729SChristoph Hellwig 	&target_dev_attr_info,
22592eafd729SChristoph Hellwig 	&target_dev_attr_control,
22602eafd729SChristoph Hellwig 	&target_dev_attr_alias,
22612eafd729SChristoph Hellwig 	&target_dev_attr_udev_path,
22622eafd729SChristoph Hellwig 	&target_dev_attr_enable,
22632eafd729SChristoph Hellwig 	&target_dev_attr_alua_lu_gp,
22642eafd729SChristoph Hellwig 	&target_dev_attr_lba_map,
2265c66ac9dbSNicholas Bellinger 	NULL,
2266c66ac9dbSNicholas Bellinger };
2267c66ac9dbSNicholas Bellinger 
2268c66ac9dbSNicholas Bellinger static void target_core_dev_release(struct config_item *item)
2269c66ac9dbSNicholas Bellinger {
22700fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
22710fd97ccfSChristoph Hellwig 	struct se_device *dev =
22720fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
2273c66ac9dbSNicholas Bellinger 
22740fd97ccfSChristoph Hellwig 	target_free_device(dev);
2275c66ac9dbSNicholas Bellinger }
2276c66ac9dbSNicholas Bellinger 
2277c17cd249SNicholas Bellinger /*
2278c17cd249SNicholas Bellinger  * Used in target_core_fabric_configfs.c to verify valid se_device symlink
2279c17cd249SNicholas Bellinger  * within target_fabric_port_link()
2280c17cd249SNicholas Bellinger  */
2281c17cd249SNicholas Bellinger struct configfs_item_operations target_core_dev_item_ops = {
2282c66ac9dbSNicholas Bellinger 	.release		= target_core_dev_release,
2283c66ac9dbSNicholas Bellinger };
2284c66ac9dbSNicholas Bellinger 
228573112edcSNicholas Bellinger TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2286c66ac9dbSNicholas Bellinger 
228773112edcSNicholas Bellinger /* End functions for struct config_item_type tb_dev_cit */
2288c66ac9dbSNicholas Bellinger 
2289c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2290c66ac9dbSNicholas Bellinger 
22912eafd729SChristoph Hellwig static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2292c66ac9dbSNicholas Bellinger {
22932eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_lu_gp,
22942eafd729SChristoph Hellwig 			lu_gp_group);
22952eafd729SChristoph Hellwig }
22962eafd729SChristoph Hellwig 
22972eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
22982eafd729SChristoph Hellwig {
22992eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
23002eafd729SChristoph Hellwig 
23016708bb27SAndy Grover 	if (!lu_gp->lu_gp_valid_id)
2302c66ac9dbSNicholas Bellinger 		return 0;
2303c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2304c66ac9dbSNicholas Bellinger }
2305c66ac9dbSNicholas Bellinger 
23062eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
23072eafd729SChristoph Hellwig 		const char *page, size_t count)
2308c66ac9dbSNicholas Bellinger {
23092eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2310c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2311c66ac9dbSNicholas Bellinger 	unsigned long lu_gp_id;
2312c66ac9dbSNicholas Bellinger 	int ret;
2313c66ac9dbSNicholas Bellinger 
231457103d7fSJingoo Han 	ret = kstrtoul(page, 0, &lu_gp_id);
2315c66ac9dbSNicholas Bellinger 	if (ret < 0) {
231657103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
2317c66ac9dbSNicholas Bellinger 			" lu_gp_id\n", ret);
231857103d7fSJingoo Han 		return ret;
2319c66ac9dbSNicholas Bellinger 	}
2320c66ac9dbSNicholas Bellinger 	if (lu_gp_id > 0x0000ffff) {
23216708bb27SAndy Grover 		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2322c66ac9dbSNicholas Bellinger 			" 0x0000ffff\n", lu_gp_id);
2323c66ac9dbSNicholas Bellinger 		return -EINVAL;
2324c66ac9dbSNicholas Bellinger 	}
2325c66ac9dbSNicholas Bellinger 
2326c66ac9dbSNicholas Bellinger 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2327c66ac9dbSNicholas Bellinger 	if (ret < 0)
2328c66ac9dbSNicholas Bellinger 		return -EINVAL;
2329c66ac9dbSNicholas Bellinger 
23306708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2331c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
2332c66ac9dbSNicholas Bellinger 		config_item_name(&alua_lu_gp_cg->cg_item),
2333c66ac9dbSNicholas Bellinger 		lu_gp->lu_gp_id);
2334c66ac9dbSNicholas Bellinger 
2335c66ac9dbSNicholas Bellinger 	return count;
2336c66ac9dbSNicholas Bellinger }
2337c66ac9dbSNicholas Bellinger 
23382eafd729SChristoph Hellwig static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2339c66ac9dbSNicholas Bellinger {
23402eafd729SChristoph Hellwig 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2341c66ac9dbSNicholas Bellinger 	struct se_device *dev;
2342c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
2343c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp_member *lu_gp_mem;
2344c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2345c66ac9dbSNicholas Bellinger 	unsigned char buf[LU_GROUP_NAME_BUF];
2346c66ac9dbSNicholas Bellinger 
2347c66ac9dbSNicholas Bellinger 	memset(buf, 0, LU_GROUP_NAME_BUF);
2348c66ac9dbSNicholas Bellinger 
2349c66ac9dbSNicholas Bellinger 	spin_lock(&lu_gp->lu_gp_lock);
2350c66ac9dbSNicholas Bellinger 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2351c66ac9dbSNicholas Bellinger 		dev = lu_gp_mem->lu_gp_mem_dev;
23520fd97ccfSChristoph Hellwig 		hba = dev->se_hba;
2353c66ac9dbSNicholas Bellinger 
2354c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2355c66ac9dbSNicholas Bellinger 			config_item_name(&hba->hba_group.cg_item),
23560fd97ccfSChristoph Hellwig 			config_item_name(&dev->dev_group.cg_item));
2357c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2358c66ac9dbSNicholas Bellinger 
2359c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
23606708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2361c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2362c66ac9dbSNicholas Bellinger 			break;
2363c66ac9dbSNicholas Bellinger 		}
2364c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2365c66ac9dbSNicholas Bellinger 		len += cur_len;
2366c66ac9dbSNicholas Bellinger 	}
2367c66ac9dbSNicholas Bellinger 	spin_unlock(&lu_gp->lu_gp_lock);
2368c66ac9dbSNicholas Bellinger 
2369c66ac9dbSNicholas Bellinger 	return len;
2370c66ac9dbSNicholas Bellinger }
2371c66ac9dbSNicholas Bellinger 
23722eafd729SChristoph Hellwig CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
23732eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_lu_gp_, members);
2374c66ac9dbSNicholas Bellinger 
2375c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
23762eafd729SChristoph Hellwig 	&target_lu_gp_attr_lu_gp_id,
23772eafd729SChristoph Hellwig 	&target_lu_gp_attr_members,
2378c66ac9dbSNicholas Bellinger 	NULL,
2379c66ac9dbSNicholas Bellinger };
2380c66ac9dbSNicholas Bellinger 
23811f6fe7cbSNicholas Bellinger static void target_core_alua_lu_gp_release(struct config_item *item)
23821f6fe7cbSNicholas Bellinger {
23831f6fe7cbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
23841f6fe7cbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
23851f6fe7cbSNicholas Bellinger 
23861f6fe7cbSNicholas Bellinger 	core_alua_free_lu_gp(lu_gp);
23871f6fe7cbSNicholas Bellinger }
23881f6fe7cbSNicholas Bellinger 
2389c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_lu_gp_ops = {
23901f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_lu_gp_release,
2391c66ac9dbSNicholas Bellinger };
2392c66ac9dbSNicholas Bellinger 
2393ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_lu_gp_cit = {
2394c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
2395c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_lu_gp_attrs,
2396c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2397c66ac9dbSNicholas Bellinger };
2398c66ac9dbSNicholas Bellinger 
2399c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2400c66ac9dbSNicholas Bellinger 
2401c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2402c66ac9dbSNicholas Bellinger 
2403c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_lu_gp(
2404c66ac9dbSNicholas Bellinger 	struct config_group *group,
2405c66ac9dbSNicholas Bellinger 	const char *name)
2406c66ac9dbSNicholas Bellinger {
2407c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
2408c66ac9dbSNicholas Bellinger 	struct config_group *alua_lu_gp_cg = NULL;
2409c66ac9dbSNicholas Bellinger 	struct config_item *alua_lu_gp_ci = NULL;
2410c66ac9dbSNicholas Bellinger 
2411c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp(name, 0);
2412c66ac9dbSNicholas Bellinger 	if (IS_ERR(lu_gp))
2413c66ac9dbSNicholas Bellinger 		return NULL;
2414c66ac9dbSNicholas Bellinger 
2415c66ac9dbSNicholas Bellinger 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
2416c66ac9dbSNicholas Bellinger 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2417c66ac9dbSNicholas Bellinger 
2418c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_lu_gp_cg, name,
2419c66ac9dbSNicholas Bellinger 			&target_core_alua_lu_gp_cit);
2420c66ac9dbSNicholas Bellinger 
24216708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2422c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s\n",
2423c66ac9dbSNicholas Bellinger 		config_item_name(alua_lu_gp_ci));
2424c66ac9dbSNicholas Bellinger 
2425c66ac9dbSNicholas Bellinger 	return alua_lu_gp_cg;
2426c66ac9dbSNicholas Bellinger 
2427c66ac9dbSNicholas Bellinger }
2428c66ac9dbSNicholas Bellinger 
2429c66ac9dbSNicholas Bellinger static void target_core_alua_drop_lu_gp(
2430c66ac9dbSNicholas Bellinger 	struct config_group *group,
2431c66ac9dbSNicholas Bellinger 	struct config_item *item)
2432c66ac9dbSNicholas Bellinger {
2433c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2434c66ac9dbSNicholas Bellinger 			struct t10_alua_lu_gp, lu_gp_group);
2435c66ac9dbSNicholas Bellinger 
24366708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2437c66ac9dbSNicholas Bellinger 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
2438c66ac9dbSNicholas Bellinger 		config_item_name(item), lu_gp->lu_gp_id);
24391f6fe7cbSNicholas Bellinger 	/*
24401f6fe7cbSNicholas Bellinger 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
24411f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_lu_gp_release()
24421f6fe7cbSNicholas Bellinger 	 */
2443c66ac9dbSNicholas Bellinger 	config_item_put(item);
2444c66ac9dbSNicholas Bellinger }
2445c66ac9dbSNicholas Bellinger 
2446c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2447c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_lu_gp,
2448c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_lu_gp,
2449c66ac9dbSNicholas Bellinger };
2450c66ac9dbSNicholas Bellinger 
2451ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_lu_gps_cit = {
2452c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2453c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
2454c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2455c66ac9dbSNicholas Bellinger };
2456c66ac9dbSNicholas Bellinger 
2457c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2458c66ac9dbSNicholas Bellinger 
2459c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2460c66ac9dbSNicholas Bellinger 
24612eafd729SChristoph Hellwig static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
24622eafd729SChristoph Hellwig {
24632eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
24642eafd729SChristoph Hellwig 			tg_pt_gp_group);
24652eafd729SChristoph Hellwig }
2466c66ac9dbSNicholas Bellinger 
24672eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2468c66ac9dbSNicholas Bellinger 		char *page)
2469c66ac9dbSNicholas Bellinger {
2470c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n",
2471d19c4643SMike Christie 		       to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
2472c66ac9dbSNicholas Bellinger }
2473c66ac9dbSNicholas Bellinger 
24742eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
24752eafd729SChristoph Hellwig 		const char *page, size_t count)
2476c66ac9dbSNicholas Bellinger {
24772eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
24780fd97ccfSChristoph Hellwig 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2479c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2480c66ac9dbSNicholas Bellinger 	int new_state, ret;
2481c66ac9dbSNicholas Bellinger 
24826708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2483125d0119SHannes Reinecke 		pr_err("Unable to do implicit ALUA on non valid"
2484c66ac9dbSNicholas Bellinger 			" tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2485c66ac9dbSNicholas Bellinger 		return -EINVAL;
2486c66ac9dbSNicholas Bellinger 	}
2487cb0f32e1SMike Christie 	if (!target_dev_configured(dev)) {
2488f1453773SNicholas Bellinger 		pr_err("Unable to set alua_access_state while device is"
2489f1453773SNicholas Bellinger 		       " not configured\n");
2490f1453773SNicholas Bellinger 		return -ENODEV;
2491f1453773SNicholas Bellinger 	}
2492c66ac9dbSNicholas Bellinger 
249357103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2494c66ac9dbSNicholas Bellinger 	if (ret < 0) {
24956708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access state from"
2496c66ac9dbSNicholas Bellinger 				" %s\n", page);
249757103d7fSJingoo Han 		return ret;
2498c66ac9dbSNicholas Bellinger 	}
2499c66ac9dbSNicholas Bellinger 	new_state = (int)tmp;
2500c66ac9dbSNicholas Bellinger 
2501125d0119SHannes Reinecke 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2502125d0119SHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA"
2503125d0119SHannes Reinecke 			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
2504c66ac9dbSNicholas Bellinger 		return -EINVAL;
2505c66ac9dbSNicholas Bellinger 	}
2506c66094bfSHannes Reinecke 	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2507c66094bfSHannes Reinecke 	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2508c66094bfSHannes Reinecke 		/* LBA DEPENDENT is only allowed with implicit ALUA */
2509c66094bfSHannes Reinecke 		pr_err("Unable to process implicit configfs ALUA transition"
2510c66094bfSHannes Reinecke 		       " while explicit ALUA management is enabled\n");
2511c66094bfSHannes Reinecke 		return -EINVAL;
2512c66094bfSHannes Reinecke 	}
2513c66ac9dbSNicholas Bellinger 
25140fd97ccfSChristoph Hellwig 	ret = core_alua_do_port_transition(tg_pt_gp, dev,
2515c66ac9dbSNicholas Bellinger 					NULL, NULL, new_state, 0);
2516c66ac9dbSNicholas Bellinger 	return (!ret) ? count : -EINVAL;
2517c66ac9dbSNicholas Bellinger }
2518c66ac9dbSNicholas Bellinger 
25192eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2520c66ac9dbSNicholas Bellinger 		char *page)
2521c66ac9dbSNicholas Bellinger {
25222eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2523c66ac9dbSNicholas Bellinger 	return sprintf(page, "%s\n",
2524c66ac9dbSNicholas Bellinger 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2525c66ac9dbSNicholas Bellinger }
2526c66ac9dbSNicholas Bellinger 
25272eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_store(
25282eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2529c66ac9dbSNicholas Bellinger {
25302eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2531c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2532c66ac9dbSNicholas Bellinger 	int new_status, ret;
2533c66ac9dbSNicholas Bellinger 
25346708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
25356708bb27SAndy Grover 		pr_err("Unable to do set ALUA access status on non"
2536c66ac9dbSNicholas Bellinger 			" valid tg_pt_gp ID: %hu\n",
2537c66ac9dbSNicholas Bellinger 			tg_pt_gp->tg_pt_gp_valid_id);
2538c66ac9dbSNicholas Bellinger 		return -EINVAL;
2539c66ac9dbSNicholas Bellinger 	}
2540c66ac9dbSNicholas Bellinger 
254157103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2542c66ac9dbSNicholas Bellinger 	if (ret < 0) {
25436708bb27SAndy Grover 		pr_err("Unable to extract new ALUA access status"
2544c66ac9dbSNicholas Bellinger 				" from %s\n", page);
254557103d7fSJingoo Han 		return ret;
2546c66ac9dbSNicholas Bellinger 	}
2547c66ac9dbSNicholas Bellinger 	new_status = (int)tmp;
2548c66ac9dbSNicholas Bellinger 
2549c66ac9dbSNicholas Bellinger 	if ((new_status != ALUA_STATUS_NONE) &&
2550125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2551125d0119SHannes Reinecke 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
25526708bb27SAndy Grover 		pr_err("Illegal ALUA access status: 0x%02x\n",
2553c66ac9dbSNicholas Bellinger 				new_status);
2554c66ac9dbSNicholas Bellinger 		return -EINVAL;
2555c66ac9dbSNicholas Bellinger 	}
2556c66ac9dbSNicholas Bellinger 
2557c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2558c66ac9dbSNicholas Bellinger 	return count;
2559c66ac9dbSNicholas Bellinger }
2560c66ac9dbSNicholas Bellinger 
25612eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2562c66ac9dbSNicholas Bellinger 		char *page)
2563c66ac9dbSNicholas Bellinger {
25642eafd729SChristoph Hellwig 	return core_alua_show_access_type(to_tg_pt_gp(item), page);
2565c66ac9dbSNicholas Bellinger }
2566c66ac9dbSNicholas Bellinger 
25672eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
25682eafd729SChristoph Hellwig 		const char *page, size_t count)
2569c66ac9dbSNicholas Bellinger {
25702eafd729SChristoph Hellwig 	return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2571c66ac9dbSNicholas Bellinger }
2572c66ac9dbSNicholas Bellinger 
25732eafd729SChristoph Hellwig #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit)				\
25742eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_show(		\
25752eafd729SChristoph Hellwig 		struct config_item *item, char *p)			\
2576b0a382c5SHannes Reinecke {									\
25772eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
25782eafd729SChristoph Hellwig 	return sprintf(p, "%d\n",					\
25792eafd729SChristoph Hellwig 		!!(t->tg_pt_gp_alua_supported_states & _bit));		\
25802eafd729SChristoph Hellwig }									\
25812eafd729SChristoph Hellwig 									\
25822eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_store(		\
25832eafd729SChristoph Hellwig 		struct config_item *item, const char *p, size_t c)	\
2584b0a382c5SHannes Reinecke {									\
25852eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
2586b0a382c5SHannes Reinecke 	unsigned long tmp;						\
2587b0a382c5SHannes Reinecke 	int ret;							\
2588b0a382c5SHannes Reinecke 									\
2589b0a382c5SHannes Reinecke 	if (!t->tg_pt_gp_valid_id) {					\
2590c0dcafd8SHannes Reinecke 		pr_err("Unable to do set " #_name " ALUA state on non"	\
2591b0a382c5SHannes Reinecke 		       " valid tg_pt_gp ID: %hu\n",			\
2592b0a382c5SHannes Reinecke 		       t->tg_pt_gp_valid_id);				\
2593b0a382c5SHannes Reinecke 		return -EINVAL;						\
2594b0a382c5SHannes Reinecke 	}								\
2595b0a382c5SHannes Reinecke 									\
2596b0a382c5SHannes Reinecke 	ret = kstrtoul(p, 0, &tmp);					\
2597b0a382c5SHannes Reinecke 	if (ret < 0) {							\
2598b0a382c5SHannes Reinecke 		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
2599b0a382c5SHannes Reinecke 		return -EINVAL;						\
2600b0a382c5SHannes Reinecke 	}								\
2601b0a382c5SHannes Reinecke 	if (tmp > 1) {							\
2602b0a382c5SHannes Reinecke 		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2603b0a382c5SHannes Reinecke 		return -EINVAL;						\
2604b0a382c5SHannes Reinecke 	}								\
26051f0b030cSSebastian Herbszt 	if (tmp)							\
26062eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states |= _bit;		\
2607b0a382c5SHannes Reinecke 	else								\
26082eafd729SChristoph Hellwig 		t->tg_pt_gp_alua_supported_states &= ~_bit;		\
2609b0a382c5SHannes Reinecke 									\
2610b0a382c5SHannes Reinecke 	return c;							\
26116be526c4SHannes Reinecke }
26126be526c4SHannes Reinecke 
26132eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
26142eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
26152eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
26162eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
26172eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
26182eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
26192eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
26206be526c4SHannes Reinecke 
26212eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_show(
26222eafd729SChristoph Hellwig 		struct config_item *item, char *page)
2623c66ac9dbSNicholas Bellinger {
26242eafd729SChristoph Hellwig 	return sprintf(page, "%d\n",
26252eafd729SChristoph Hellwig 		to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
2626c66ac9dbSNicholas Bellinger }
2627c66ac9dbSNicholas Bellinger 
26282eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_store(
26292eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
2630c66ac9dbSNicholas Bellinger {
26312eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2632c66ac9dbSNicholas Bellinger 	unsigned long tmp;
2633c66ac9dbSNicholas Bellinger 	int ret;
2634c66ac9dbSNicholas Bellinger 
263557103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tmp);
2636c66ac9dbSNicholas Bellinger 	if (ret < 0) {
26376708bb27SAndy Grover 		pr_err("Unable to extract alua_write_metadata\n");
263857103d7fSJingoo Han 		return ret;
2639c66ac9dbSNicholas Bellinger 	}
2640c66ac9dbSNicholas Bellinger 
2641c66ac9dbSNicholas Bellinger 	if ((tmp != 0) && (tmp != 1)) {
26426708bb27SAndy Grover 		pr_err("Illegal value for alua_write_metadata:"
2643c66ac9dbSNicholas Bellinger 			" %lu\n", tmp);
2644c66ac9dbSNicholas Bellinger 		return -EINVAL;
2645c66ac9dbSNicholas Bellinger 	}
2646c66ac9dbSNicholas Bellinger 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2647c66ac9dbSNicholas Bellinger 
2648c66ac9dbSNicholas Bellinger 	return count;
2649c66ac9dbSNicholas Bellinger }
2650c66ac9dbSNicholas Bellinger 
26512eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2652c66ac9dbSNicholas Bellinger 		char *page)
2653c66ac9dbSNicholas Bellinger {
26542eafd729SChristoph Hellwig 	return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
2655c66ac9dbSNicholas Bellinger }
2656c66ac9dbSNicholas Bellinger 
26572eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
26582eafd729SChristoph Hellwig 		const char *page, size_t count)
2659c66ac9dbSNicholas Bellinger {
26602eafd729SChristoph Hellwig 	return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
26612eafd729SChristoph Hellwig 			count);
2662c66ac9dbSNicholas Bellinger }
2663c66ac9dbSNicholas Bellinger 
26642eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2665c66ac9dbSNicholas Bellinger 		char *page)
2666c66ac9dbSNicholas Bellinger {
26672eafd729SChristoph Hellwig 	return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
2668c66ac9dbSNicholas Bellinger }
2669c66ac9dbSNicholas Bellinger 
26702eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
26712eafd729SChristoph Hellwig 		const char *page, size_t count)
2672c66ac9dbSNicholas Bellinger {
26732eafd729SChristoph Hellwig 	return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
26742eafd729SChristoph Hellwig 			count);
2675c66ac9dbSNicholas Bellinger }
2676c66ac9dbSNicholas Bellinger 
26772eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
26782eafd729SChristoph Hellwig 		struct config_item *item, char *page)
26792eafd729SChristoph Hellwig {
26802eafd729SChristoph Hellwig 	return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
26812eafd729SChristoph Hellwig }
2682c66ac9dbSNicholas Bellinger 
26832eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
26842eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count)
26852eafd729SChristoph Hellwig {
26862eafd729SChristoph Hellwig 	return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
26872eafd729SChristoph Hellwig 			count);
26882eafd729SChristoph Hellwig }
26892eafd729SChristoph Hellwig 
26902eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
26915b9a4d72SNicholas Bellinger 		char *page)
26925b9a4d72SNicholas Bellinger {
26932eafd729SChristoph Hellwig 	return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
26945b9a4d72SNicholas Bellinger }
26955b9a4d72SNicholas Bellinger 
26962eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
26972eafd729SChristoph Hellwig 		const char *page, size_t count)
26985b9a4d72SNicholas Bellinger {
26992eafd729SChristoph Hellwig 	return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
27005b9a4d72SNicholas Bellinger }
27015b9a4d72SNicholas Bellinger 
27022eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2703c66ac9dbSNicholas Bellinger 		char *page)
2704c66ac9dbSNicholas Bellinger {
27052eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2706c66ac9dbSNicholas Bellinger 
27076708bb27SAndy Grover 	if (!tg_pt_gp->tg_pt_gp_valid_id)
2708c66ac9dbSNicholas Bellinger 		return 0;
2709c66ac9dbSNicholas Bellinger 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2710c66ac9dbSNicholas Bellinger }
2711c66ac9dbSNicholas Bellinger 
27122eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
27132eafd729SChristoph Hellwig 		const char *page, size_t count)
2714c66ac9dbSNicholas Bellinger {
27152eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2716c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2717c66ac9dbSNicholas Bellinger 	unsigned long tg_pt_gp_id;
2718c66ac9dbSNicholas Bellinger 	int ret;
2719c66ac9dbSNicholas Bellinger 
272057103d7fSJingoo Han 	ret = kstrtoul(page, 0, &tg_pt_gp_id);
2721c66ac9dbSNicholas Bellinger 	if (ret < 0) {
27223d035237SHannes Reinecke 		pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
27233d035237SHannes Reinecke 		       page);
272457103d7fSJingoo Han 		return ret;
2725c66ac9dbSNicholas Bellinger 	}
2726c66ac9dbSNicholas Bellinger 	if (tg_pt_gp_id > 0x0000ffff) {
27273d035237SHannes Reinecke 		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
27283d035237SHannes Reinecke 		       tg_pt_gp_id);
2729c66ac9dbSNicholas Bellinger 		return -EINVAL;
2730c66ac9dbSNicholas Bellinger 	}
2731c66ac9dbSNicholas Bellinger 
2732c66ac9dbSNicholas Bellinger 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2733c66ac9dbSNicholas Bellinger 	if (ret < 0)
2734c66ac9dbSNicholas Bellinger 		return -EINVAL;
2735c66ac9dbSNicholas Bellinger 
27366708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2737c66ac9dbSNicholas Bellinger 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
2738c66ac9dbSNicholas Bellinger 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
2739c66ac9dbSNicholas Bellinger 		tg_pt_gp->tg_pt_gp_id);
2740c66ac9dbSNicholas Bellinger 
2741c66ac9dbSNicholas Bellinger 	return count;
2742c66ac9dbSNicholas Bellinger }
2743c66ac9dbSNicholas Bellinger 
27442eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2745c66ac9dbSNicholas Bellinger 		char *page)
2746c66ac9dbSNicholas Bellinger {
27472eafd729SChristoph Hellwig 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2748c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
2749c66ac9dbSNicholas Bellinger 	ssize_t len = 0, cur_len;
2750c66ac9dbSNicholas Bellinger 	unsigned char buf[TG_PT_GROUP_NAME_BUF];
2751c66ac9dbSNicholas Bellinger 
2752c66ac9dbSNicholas Bellinger 	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2753c66ac9dbSNicholas Bellinger 
2754c66ac9dbSNicholas Bellinger 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2755adf653f9SChristoph Hellwig 	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2756adf653f9SChristoph Hellwig 			lun_tg_pt_gp_link) {
2757adf653f9SChristoph Hellwig 		struct se_portal_group *tpg = lun->lun_tpg;
2758c66ac9dbSNicholas Bellinger 
2759c66ac9dbSNicholas Bellinger 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
276030c7ca93SDavid Disseldorp 			"/%s\n", tpg->se_tpg_tfo->fabric_name,
2761e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2762e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
2763c66ac9dbSNicholas Bellinger 			config_item_name(&lun->lun_group.cg_item));
2764c66ac9dbSNicholas Bellinger 		cur_len++; /* Extra byte for NULL terminator */
2765c66ac9dbSNicholas Bellinger 
2766c66ac9dbSNicholas Bellinger 		if ((cur_len + len) > PAGE_SIZE) {
27676708bb27SAndy Grover 			pr_warn("Ran out of lu_gp_show_attr"
2768c66ac9dbSNicholas Bellinger 				"_members buffer\n");
2769c66ac9dbSNicholas Bellinger 			break;
2770c66ac9dbSNicholas Bellinger 		}
2771c66ac9dbSNicholas Bellinger 		memcpy(page+len, buf, cur_len);
2772c66ac9dbSNicholas Bellinger 		len += cur_len;
2773c66ac9dbSNicholas Bellinger 	}
2774c66ac9dbSNicholas Bellinger 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2775c66ac9dbSNicholas Bellinger 
2776c66ac9dbSNicholas Bellinger 	return len;
2777c66ac9dbSNicholas Bellinger }
2778c66ac9dbSNicholas Bellinger 
27792eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
27802eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
27812eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
27822eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
27832eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
27842eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
27852eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
27862eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
27872eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
27882eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
27892eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
27902eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
27912eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
27922eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
27932eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
27942eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
27952eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
2796c66ac9dbSNicholas Bellinger 
2797c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
27982eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_state,
27992eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_status,
28002eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_access_type,
28012eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_transitioning,
28022eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_offline,
28032eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_lba_dependent,
28042eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_unavailable,
28052eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_standby,
28062eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_nonoptimized,
28072eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_support_active_optimized,
28082eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_alua_write_metadata,
28092eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_nonop_delay_msecs,
28102eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_trans_delay_msecs,
28112eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_implicit_trans_secs,
28122eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_preferred,
28132eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_tg_pt_gp_id,
28142eafd729SChristoph Hellwig 	&target_tg_pt_gp_attr_members,
2815c66ac9dbSNicholas Bellinger 	NULL,
2816c66ac9dbSNicholas Bellinger };
2817c66ac9dbSNicholas Bellinger 
28181f6fe7cbSNicholas Bellinger static void target_core_alua_tg_pt_gp_release(struct config_item *item)
28191f6fe7cbSNicholas Bellinger {
28201f6fe7cbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
28211f6fe7cbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
28221f6fe7cbSNicholas Bellinger 
28231f6fe7cbSNicholas Bellinger 	core_alua_free_tg_pt_gp(tg_pt_gp);
28241f6fe7cbSNicholas Bellinger }
28251f6fe7cbSNicholas Bellinger 
2826c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
28271f6fe7cbSNicholas Bellinger 	.release		= target_core_alua_tg_pt_gp_release,
2828c66ac9dbSNicholas Bellinger };
2829c66ac9dbSNicholas Bellinger 
2830ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_tg_pt_gp_cit = {
2831c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
2832c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
2833c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2834c66ac9dbSNicholas Bellinger };
2835c66ac9dbSNicholas Bellinger 
2836c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2837c66ac9dbSNicholas Bellinger 
283872aca57bSNicholas Bellinger /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2839c66ac9dbSNicholas Bellinger 
2840c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_tg_pt_gp(
2841c66ac9dbSNicholas Bellinger 	struct config_group *group,
2842c66ac9dbSNicholas Bellinger 	const char *name)
2843c66ac9dbSNicholas Bellinger {
2844c66ac9dbSNicholas Bellinger 	struct t10_alua *alua = container_of(group, struct t10_alua,
2845c66ac9dbSNicholas Bellinger 					alua_tg_pt_gps_group);
2846c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2847c66ac9dbSNicholas Bellinger 	struct config_group *alua_tg_pt_gp_cg = NULL;
2848c66ac9dbSNicholas Bellinger 	struct config_item *alua_tg_pt_gp_ci = NULL;
2849c66ac9dbSNicholas Bellinger 
28500fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
28516708bb27SAndy Grover 	if (!tg_pt_gp)
2852c66ac9dbSNicholas Bellinger 		return NULL;
2853c66ac9dbSNicholas Bellinger 
2854c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2855c66ac9dbSNicholas Bellinger 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2856c66ac9dbSNicholas Bellinger 
2857c66ac9dbSNicholas Bellinger 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
2858c66ac9dbSNicholas Bellinger 			&target_core_alua_tg_pt_gp_cit);
2859c66ac9dbSNicholas Bellinger 
28606708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2861c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s\n",
2862c66ac9dbSNicholas Bellinger 		config_item_name(alua_tg_pt_gp_ci));
2863c66ac9dbSNicholas Bellinger 
2864c66ac9dbSNicholas Bellinger 	return alua_tg_pt_gp_cg;
2865c66ac9dbSNicholas Bellinger }
2866c66ac9dbSNicholas Bellinger 
2867c66ac9dbSNicholas Bellinger static void target_core_alua_drop_tg_pt_gp(
2868c66ac9dbSNicholas Bellinger 	struct config_group *group,
2869c66ac9dbSNicholas Bellinger 	struct config_item *item)
2870c66ac9dbSNicholas Bellinger {
2871c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2872c66ac9dbSNicholas Bellinger 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2873c66ac9dbSNicholas Bellinger 
28746708bb27SAndy Grover 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2875c66ac9dbSNicholas Bellinger 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
2876c66ac9dbSNicholas Bellinger 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
28771f6fe7cbSNicholas Bellinger 	/*
28781f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
28791f6fe7cbSNicholas Bellinger 	 * -> target_core_alua_tg_pt_gp_release().
28801f6fe7cbSNicholas Bellinger 	 */
2881c66ac9dbSNicholas Bellinger 	config_item_put(item);
2882c66ac9dbSNicholas Bellinger }
2883c66ac9dbSNicholas Bellinger 
2884c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2885c66ac9dbSNicholas Bellinger 	.make_group		= &target_core_alua_create_tg_pt_gp,
2886c66ac9dbSNicholas Bellinger 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
2887c66ac9dbSNicholas Bellinger };
2888c66ac9dbSNicholas Bellinger 
288972aca57bSNicholas Bellinger TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
2890c66ac9dbSNicholas Bellinger 
289172aca57bSNicholas Bellinger /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2892c66ac9dbSNicholas Bellinger 
2893c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_cit */
2894c66ac9dbSNicholas Bellinger 
2895c66ac9dbSNicholas Bellinger /*
2896c66ac9dbSNicholas Bellinger  * target_core_alua_cit is a ConfigFS group that lives under
2897c66ac9dbSNicholas Bellinger  * /sys/kernel/config/target/core/alua.  There are default groups
2898c66ac9dbSNicholas Bellinger  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2899c66ac9dbSNicholas Bellinger  * target_core_alua_cit in target_core_init_configfs() below.
2900c66ac9dbSNicholas Bellinger  */
2901ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_cit = {
2902c66ac9dbSNicholas Bellinger 	.ct_item_ops		= NULL,
2903c66ac9dbSNicholas Bellinger 	.ct_attrs		= NULL,
2904c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
2905c66ac9dbSNicholas Bellinger };
2906c66ac9dbSNicholas Bellinger 
2907c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_cit */
2908c66ac9dbSNicholas Bellinger 
2909d23ab570SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_stat_cit */
291012d23384SNicholas Bellinger 
291112d23384SNicholas Bellinger static struct config_group *target_core_stat_mkdir(
291212d23384SNicholas Bellinger 	struct config_group *group,
291312d23384SNicholas Bellinger 	const char *name)
291412d23384SNicholas Bellinger {
291512d23384SNicholas Bellinger 	return ERR_PTR(-ENOSYS);
291612d23384SNicholas Bellinger }
291712d23384SNicholas Bellinger 
291812d23384SNicholas Bellinger static void target_core_stat_rmdir(
291912d23384SNicholas Bellinger 	struct config_group *group,
292012d23384SNicholas Bellinger 	struct config_item *item)
292112d23384SNicholas Bellinger {
292212d23384SNicholas Bellinger 	return;
292312d23384SNicholas Bellinger }
292412d23384SNicholas Bellinger 
292512d23384SNicholas Bellinger static struct configfs_group_operations target_core_stat_group_ops = {
292612d23384SNicholas Bellinger 	.make_group		= &target_core_stat_mkdir,
292712d23384SNicholas Bellinger 	.drop_item		= &target_core_stat_rmdir,
292812d23384SNicholas Bellinger };
292912d23384SNicholas Bellinger 
2930d23ab570SNicholas Bellinger TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
293112d23384SNicholas Bellinger 
2932d23ab570SNicholas Bellinger /* End functions for struct config_item_type tb_dev_stat_cit */
293312d23384SNicholas Bellinger 
2934c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_hba_cit */
2935c66ac9dbSNicholas Bellinger 
2936c66ac9dbSNicholas Bellinger static struct config_group *target_core_make_subdev(
2937c66ac9dbSNicholas Bellinger 	struct config_group *group,
2938c66ac9dbSNicholas Bellinger 	const char *name)
2939c66ac9dbSNicholas Bellinger {
2940c66ac9dbSNicholas Bellinger 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2941c66ac9dbSNicholas Bellinger 	struct config_item *hba_ci = &group->cg_item;
2942c66ac9dbSNicholas Bellinger 	struct se_hba *hba = item_to_hba(hba_ci);
29430a06d430SChristoph Hellwig 	struct target_backend *tb = hba->backend;
29440fd97ccfSChristoph Hellwig 	struct se_device *dev;
294512d23384SNicholas Bellinger 	int errno = -ENOMEM, ret;
2946c66ac9dbSNicholas Bellinger 
294712d23384SNicholas Bellinger 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
294812d23384SNicholas Bellinger 	if (ret)
294912d23384SNicholas Bellinger 		return ERR_PTR(ret);
2950c66ac9dbSNicholas Bellinger 
29510fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, name);
29520fd97ccfSChristoph Hellwig 	if (!dev)
29530fd97ccfSChristoph Hellwig 		goto out_unlock;
2954c66ac9dbSNicholas Bellinger 
29551ae1602dSChristoph Hellwig 	config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
2956c66ac9dbSNicholas Bellinger 
29578dc31ff9SMike Christie 	config_group_init_type_name(&dev->dev_action_group, "action",
29588dc31ff9SMike Christie 			&tb->tb_dev_action_cit);
29598dc31ff9SMike Christie 	configfs_add_default_group(&dev->dev_action_group, &dev->dev_group);
29608dc31ff9SMike Christie 
29610fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
29620a06d430SChristoph Hellwig 			&tb->tb_dev_attrib_cit);
29631ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
29641ae1602dSChristoph Hellwig 
29650fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_pr_group, "pr",
29660a06d430SChristoph Hellwig 			&tb->tb_dev_pr_cit);
29671ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
29681ae1602dSChristoph Hellwig 
29690fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
29700a06d430SChristoph Hellwig 			&tb->tb_dev_wwn_cit);
29711ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
29721ae1602dSChristoph Hellwig 			&dev->dev_group);
29731ae1602dSChristoph Hellwig 
29740fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
29750a06d430SChristoph Hellwig 			"alua", &tb->tb_dev_alua_tg_pt_gps_cit);
29761ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
29771ae1602dSChristoph Hellwig 			&dev->dev_group);
29781ae1602dSChristoph Hellwig 
29790fd97ccfSChristoph Hellwig 	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
29800a06d430SChristoph Hellwig 			"statistics", &tb->tb_dev_stat_cit);
29811ae1602dSChristoph Hellwig 	configfs_add_default_group(&dev->dev_stat_grps.stat_group,
29821ae1602dSChristoph Hellwig 			&dev->dev_group);
298312d23384SNicholas Bellinger 
2984c66ac9dbSNicholas Bellinger 	/*
298512d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2986c66ac9dbSNicholas Bellinger 	 */
29870fd97ccfSChristoph Hellwig 	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
29886708bb27SAndy Grover 	if (!tg_pt_gp)
29891ae1602dSChristoph Hellwig 		goto out_free_device;
29900fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2991c66ac9dbSNicholas Bellinger 
2992c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2993c66ac9dbSNicholas Bellinger 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
29941ae1602dSChristoph Hellwig 	configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
29951ae1602dSChristoph Hellwig 			&dev->t10_alua.alua_tg_pt_gps_group);
29961ae1602dSChristoph Hellwig 
299712d23384SNicholas Bellinger 	/*
299812d23384SNicholas Bellinger 	 * Add core/$HBA/$DEV/statistics/ default groups
299912d23384SNicholas Bellinger 	 */
30000fd97ccfSChristoph Hellwig 	target_stat_setup_dev_default_groups(dev);
3001c66ac9dbSNicholas Bellinger 
3002c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
30031ae1602dSChristoph Hellwig 	return &dev->dev_group;
30040fd97ccfSChristoph Hellwig 
30050fd97ccfSChristoph Hellwig out_free_device:
30060fd97ccfSChristoph Hellwig 	target_free_device(dev);
30070fd97ccfSChristoph Hellwig out_unlock:
3008c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
300912d23384SNicholas Bellinger 	return ERR_PTR(errno);
3010c66ac9dbSNicholas Bellinger }
3011c66ac9dbSNicholas Bellinger 
3012c66ac9dbSNicholas Bellinger static void target_core_drop_subdev(
3013c66ac9dbSNicholas Bellinger 	struct config_group *group,
3014c66ac9dbSNicholas Bellinger 	struct config_item *item)
3015c66ac9dbSNicholas Bellinger {
30160fd97ccfSChristoph Hellwig 	struct config_group *dev_cg = to_config_group(item);
30170fd97ccfSChristoph Hellwig 	struct se_device *dev =
30180fd97ccfSChristoph Hellwig 		container_of(dev_cg, struct se_device, dev_group);
3019c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
3020c66ac9dbSNicholas Bellinger 
30210fd97ccfSChristoph Hellwig 	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
3022c66ac9dbSNicholas Bellinger 
30231f6fe7cbSNicholas Bellinger 	mutex_lock(&hba->hba_access_mutex);
3024c66ac9dbSNicholas Bellinger 
30251ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
30261ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
302712d23384SNicholas Bellinger 
30281f6fe7cbSNicholas Bellinger 	/*
30291f6fe7cbSNicholas Bellinger 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
30301f6fe7cbSNicholas Bellinger 	 * directly from target_core_alua_tg_pt_gp_release().
30311f6fe7cbSNicholas Bellinger 	 */
30320fd97ccfSChristoph Hellwig 	dev->t10_alua.default_tg_pt_gp = NULL;
3033c66ac9dbSNicholas Bellinger 
30341ae1602dSChristoph Hellwig 	configfs_remove_default_groups(dev_cg);
30351ae1602dSChristoph Hellwig 
30361f6fe7cbSNicholas Bellinger 	/*
30370fd97ccfSChristoph Hellwig 	 * se_dev is released from target_core_dev_item_ops->release()
30381f6fe7cbSNicholas Bellinger 	 */
3039c66ac9dbSNicholas Bellinger 	config_item_put(item);
3040c66ac9dbSNicholas Bellinger 	mutex_unlock(&hba->hba_access_mutex);
3041c66ac9dbSNicholas Bellinger }
3042c66ac9dbSNicholas Bellinger 
3043c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_hba_group_ops = {
3044c66ac9dbSNicholas Bellinger 	.make_group		= target_core_make_subdev,
3045c66ac9dbSNicholas Bellinger 	.drop_item		= target_core_drop_subdev,
3046c66ac9dbSNicholas Bellinger };
3047c66ac9dbSNicholas Bellinger 
3048c66ac9dbSNicholas Bellinger 
30492eafd729SChristoph Hellwig static inline struct se_hba *to_hba(struct config_item *item)
3050c66ac9dbSNicholas Bellinger {
30512eafd729SChristoph Hellwig 	return container_of(to_config_group(item), struct se_hba, hba_group);
30522eafd729SChristoph Hellwig }
30532eafd729SChristoph Hellwig 
30542eafd729SChristoph Hellwig static ssize_t target_hba_info_show(struct config_item *item, char *page)
30552eafd729SChristoph Hellwig {
30562eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
30572eafd729SChristoph Hellwig 
3058c66ac9dbSNicholas Bellinger 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
30590a06d430SChristoph Hellwig 			hba->hba_id, hba->backend->ops->name,
3060ce8dd25dSChristoph Hellwig 			TARGET_CORE_VERSION);
3061c66ac9dbSNicholas Bellinger }
3062c66ac9dbSNicholas Bellinger 
30632eafd729SChristoph Hellwig static ssize_t target_hba_mode_show(struct config_item *item, char *page)
3064c66ac9dbSNicholas Bellinger {
30652eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
3066c66ac9dbSNicholas Bellinger 	int hba_mode = 0;
3067c66ac9dbSNicholas Bellinger 
3068c66ac9dbSNicholas Bellinger 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3069c66ac9dbSNicholas Bellinger 		hba_mode = 1;
3070c66ac9dbSNicholas Bellinger 
3071c66ac9dbSNicholas Bellinger 	return sprintf(page, "%d\n", hba_mode);
3072c66ac9dbSNicholas Bellinger }
3073c66ac9dbSNicholas Bellinger 
30742eafd729SChristoph Hellwig static ssize_t target_hba_mode_store(struct config_item *item,
3075c66ac9dbSNicholas Bellinger 		const char *page, size_t count)
3076c66ac9dbSNicholas Bellinger {
30772eafd729SChristoph Hellwig 	struct se_hba *hba = to_hba(item);
3078c66ac9dbSNicholas Bellinger 	unsigned long mode_flag;
3079c66ac9dbSNicholas Bellinger 	int ret;
3080c66ac9dbSNicholas Bellinger 
30810a06d430SChristoph Hellwig 	if (hba->backend->ops->pmode_enable_hba == NULL)
3082c66ac9dbSNicholas Bellinger 		return -EINVAL;
3083c66ac9dbSNicholas Bellinger 
308457103d7fSJingoo Han 	ret = kstrtoul(page, 0, &mode_flag);
3085c66ac9dbSNicholas Bellinger 	if (ret < 0) {
30866708bb27SAndy Grover 		pr_err("Unable to extract hba mode flag: %d\n", ret);
308757103d7fSJingoo Han 		return ret;
3088c66ac9dbSNicholas Bellinger 	}
3089c66ac9dbSNicholas Bellinger 
30900fd97ccfSChristoph Hellwig 	if (hba->dev_count) {
30916708bb27SAndy Grover 		pr_err("Unable to set hba_mode with active devices\n");
3092c66ac9dbSNicholas Bellinger 		return -EINVAL;
3093c66ac9dbSNicholas Bellinger 	}
3094c66ac9dbSNicholas Bellinger 
30950a06d430SChristoph Hellwig 	ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3096c66ac9dbSNicholas Bellinger 	if (ret < 0)
3097c66ac9dbSNicholas Bellinger 		return -EINVAL;
3098c66ac9dbSNicholas Bellinger 	if (ret > 0)
3099c66ac9dbSNicholas Bellinger 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3100c66ac9dbSNicholas Bellinger 	else if (ret == 0)
3101c66ac9dbSNicholas Bellinger 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3102c66ac9dbSNicholas Bellinger 
3103c66ac9dbSNicholas Bellinger 	return count;
3104c66ac9dbSNicholas Bellinger }
3105c66ac9dbSNicholas Bellinger 
31062eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_, hba_info);
31072eafd729SChristoph Hellwig CONFIGFS_ATTR(target_, hba_mode);
3108c66ac9dbSNicholas Bellinger 
31091f6fe7cbSNicholas Bellinger static void target_core_hba_release(struct config_item *item)
31101f6fe7cbSNicholas Bellinger {
31111f6fe7cbSNicholas Bellinger 	struct se_hba *hba = container_of(to_config_group(item),
31121f6fe7cbSNicholas Bellinger 				struct se_hba, hba_group);
31131f6fe7cbSNicholas Bellinger 	core_delete_hba(hba);
31141f6fe7cbSNicholas Bellinger }
31151f6fe7cbSNicholas Bellinger 
3116c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_hba_attrs[] = {
31172eafd729SChristoph Hellwig 	&target_attr_hba_info,
31182eafd729SChristoph Hellwig 	&target_attr_hba_mode,
3119c66ac9dbSNicholas Bellinger 	NULL,
3120c66ac9dbSNicholas Bellinger };
3121c66ac9dbSNicholas Bellinger 
3122c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_hba_item_ops = {
31231f6fe7cbSNicholas Bellinger 	.release		= target_core_hba_release,
3124c66ac9dbSNicholas Bellinger };
3125c66ac9dbSNicholas Bellinger 
3126ece550b5SBhumika Goyal static const struct config_item_type target_core_hba_cit = {
3127c66ac9dbSNicholas Bellinger 	.ct_item_ops		= &target_core_hba_item_ops,
3128c66ac9dbSNicholas Bellinger 	.ct_group_ops		= &target_core_hba_group_ops,
3129c66ac9dbSNicholas Bellinger 	.ct_attrs		= target_core_hba_attrs,
3130c66ac9dbSNicholas Bellinger 	.ct_owner		= THIS_MODULE,
3131c66ac9dbSNicholas Bellinger };
3132c66ac9dbSNicholas Bellinger 
3133c66ac9dbSNicholas Bellinger static struct config_group *target_core_call_addhbatotarget(
3134c66ac9dbSNicholas Bellinger 	struct config_group *group,
3135c66ac9dbSNicholas Bellinger 	const char *name)
3136c66ac9dbSNicholas Bellinger {
3137c66ac9dbSNicholas Bellinger 	char *se_plugin_str, *str, *str2;
3138c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
3139c66ac9dbSNicholas Bellinger 	char buf[TARGET_CORE_NAME_MAX_LEN];
3140c66ac9dbSNicholas Bellinger 	unsigned long plugin_dep_id = 0;
3141c66ac9dbSNicholas Bellinger 	int ret;
3142c66ac9dbSNicholas Bellinger 
3143c66ac9dbSNicholas Bellinger 	memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
314460d645a4SDan Carpenter 	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
31456708bb27SAndy Grover 		pr_err("Passed *name strlen(): %d exceeds"
3146c66ac9dbSNicholas Bellinger 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3147c66ac9dbSNicholas Bellinger 			TARGET_CORE_NAME_MAX_LEN);
3148c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENAMETOOLONG);
3149c66ac9dbSNicholas Bellinger 	}
3150c66ac9dbSNicholas Bellinger 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3151c66ac9dbSNicholas Bellinger 
3152c66ac9dbSNicholas Bellinger 	str = strstr(buf, "_");
31536708bb27SAndy Grover 	if (!str) {
31546708bb27SAndy Grover 		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3155c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EINVAL);
3156c66ac9dbSNicholas Bellinger 	}
3157c66ac9dbSNicholas Bellinger 	se_plugin_str = buf;
3158c66ac9dbSNicholas Bellinger 	/*
3159c66ac9dbSNicholas Bellinger 	 * Special case for subsystem plugins that have "_" in their names.
3160c66ac9dbSNicholas Bellinger 	 * Namely rd_direct and rd_mcp..
3161c66ac9dbSNicholas Bellinger 	 */
3162c66ac9dbSNicholas Bellinger 	str2 = strstr(str+1, "_");
31636708bb27SAndy Grover 	if (str2) {
3164c66ac9dbSNicholas Bellinger 		*str2 = '\0'; /* Terminate for *se_plugin_str */
3165c66ac9dbSNicholas Bellinger 		str2++; /* Skip to start of plugin dependent ID */
3166c66ac9dbSNicholas Bellinger 		str = str2;
3167c66ac9dbSNicholas Bellinger 	} else {
3168c66ac9dbSNicholas Bellinger 		*str = '\0'; /* Terminate for *se_plugin_str */
3169c66ac9dbSNicholas Bellinger 		str++; /* Skip to start of plugin dependent ID */
3170c66ac9dbSNicholas Bellinger 	}
3171c66ac9dbSNicholas Bellinger 
317257103d7fSJingoo Han 	ret = kstrtoul(str, 0, &plugin_dep_id);
3173c66ac9dbSNicholas Bellinger 	if (ret < 0) {
317457103d7fSJingoo Han 		pr_err("kstrtoul() returned %d for"
3175c66ac9dbSNicholas Bellinger 				" plugin_dep_id\n", ret);
317657103d7fSJingoo Han 		return ERR_PTR(ret);
3177c66ac9dbSNicholas Bellinger 	}
3178c66ac9dbSNicholas Bellinger 	/*
3179c66ac9dbSNicholas Bellinger 	 * Load up TCM subsystem plugins if they have not already been loaded.
3180c66ac9dbSNicholas Bellinger 	 */
3181dbc5623eSNicholas Bellinger 	transport_subsystem_check_init();
3182c66ac9dbSNicholas Bellinger 
3183c66ac9dbSNicholas Bellinger 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3184c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
3185c66ac9dbSNicholas Bellinger 		return ERR_CAST(hba);
3186c66ac9dbSNicholas Bellinger 
3187c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&hba->hba_group, name,
3188c66ac9dbSNicholas Bellinger 			&target_core_hba_cit);
3189c66ac9dbSNicholas Bellinger 
3190c66ac9dbSNicholas Bellinger 	return &hba->hba_group;
3191c66ac9dbSNicholas Bellinger }
3192c66ac9dbSNicholas Bellinger 
3193c66ac9dbSNicholas Bellinger static void target_core_call_delhbafromtarget(
3194c66ac9dbSNicholas Bellinger 	struct config_group *group,
3195c66ac9dbSNicholas Bellinger 	struct config_item *item)
3196c66ac9dbSNicholas Bellinger {
31971f6fe7cbSNicholas Bellinger 	/*
31981f6fe7cbSNicholas Bellinger 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
31991f6fe7cbSNicholas Bellinger 	 * -> target_core_hba_release()
32001f6fe7cbSNicholas Bellinger 	 */
3201c66ac9dbSNicholas Bellinger 	config_item_put(item);
3202c66ac9dbSNicholas Bellinger }
3203c66ac9dbSNicholas Bellinger 
3204c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_group_ops = {
3205c66ac9dbSNicholas Bellinger 	.make_group	= target_core_call_addhbatotarget,
3206c66ac9dbSNicholas Bellinger 	.drop_item	= target_core_call_delhbafromtarget,
3207c66ac9dbSNicholas Bellinger };
3208c66ac9dbSNicholas Bellinger 
3209ece550b5SBhumika Goyal static const struct config_item_type target_core_cit = {
3210c66ac9dbSNicholas Bellinger 	.ct_item_ops	= NULL,
3211c66ac9dbSNicholas Bellinger 	.ct_group_ops	= &target_core_group_ops,
3212c66ac9dbSNicholas Bellinger 	.ct_attrs	= NULL,
3213c66ac9dbSNicholas Bellinger 	.ct_owner	= THIS_MODULE,
3214c66ac9dbSNicholas Bellinger };
3215c66ac9dbSNicholas Bellinger 
3216c66ac9dbSNicholas Bellinger /* Stop functions for struct config_item_type target_core_hba_cit */
3217c66ac9dbSNicholas Bellinger 
32180a06d430SChristoph Hellwig void target_setup_backend_cits(struct target_backend *tb)
321973112edcSNicholas Bellinger {
32200a06d430SChristoph Hellwig 	target_core_setup_dev_cit(tb);
32218dc31ff9SMike Christie 	target_core_setup_dev_action_cit(tb);
32220a06d430SChristoph Hellwig 	target_core_setup_dev_attrib_cit(tb);
32230a06d430SChristoph Hellwig 	target_core_setup_dev_pr_cit(tb);
32240a06d430SChristoph Hellwig 	target_core_setup_dev_wwn_cit(tb);
32250a06d430SChristoph Hellwig 	target_core_setup_dev_alua_tg_pt_gps_cit(tb);
32260a06d430SChristoph Hellwig 	target_core_setup_dev_stat_cit(tb);
322773112edcSNicholas Bellinger }
322873112edcSNicholas Bellinger 
322978a6295cSLee Duncan static void target_init_dbroot(void)
323078a6295cSLee Duncan {
323178a6295cSLee Duncan 	struct file *fp;
323278a6295cSLee Duncan 
323378a6295cSLee Duncan 	snprintf(db_root_stage, DB_ROOT_LEN, DB_ROOT_PREFERRED);
323478a6295cSLee Duncan 	fp = filp_open(db_root_stage, O_RDONLY, 0);
323578a6295cSLee Duncan 	if (IS_ERR(fp)) {
323678a6295cSLee Duncan 		pr_err("db_root: cannot open: %s\n", db_root_stage);
323778a6295cSLee Duncan 		return;
323878a6295cSLee Duncan 	}
323978a6295cSLee Duncan 	if (!S_ISDIR(file_inode(fp)->i_mode)) {
324078a6295cSLee Duncan 		filp_close(fp, NULL);
324178a6295cSLee Duncan 		pr_err("db_root: not a valid directory: %s\n", db_root_stage);
324278a6295cSLee Duncan 		return;
324378a6295cSLee Duncan 	}
324478a6295cSLee Duncan 	filp_close(fp, NULL);
324578a6295cSLee Duncan 
324678a6295cSLee Duncan 	strncpy(db_root, db_root_stage, DB_ROOT_LEN);
324778a6295cSLee Duncan 	pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
324878a6295cSLee Duncan }
324978a6295cSLee Duncan 
325054550fabSAxel Lin static int __init target_core_init_configfs(void)
3251c66ac9dbSNicholas Bellinger {
3252d588cf8fSChristoph Hellwig 	struct configfs_subsystem *subsys = &target_core_fabrics;
3253c66ac9dbSNicholas Bellinger 	struct t10_alua_lu_gp *lu_gp;
3254c66ac9dbSNicholas Bellinger 	int ret;
3255c66ac9dbSNicholas Bellinger 
32566708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3257c66ac9dbSNicholas Bellinger 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
3258c66ac9dbSNicholas Bellinger 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3259c66ac9dbSNicholas Bellinger 
3260c66ac9dbSNicholas Bellinger 	config_group_init(&subsys->su_group);
3261c66ac9dbSNicholas Bellinger 	mutex_init(&subsys->su_mutex);
3262c66ac9dbSNicholas Bellinger 
3263e3d6f909SAndy Grover 	ret = init_se_kmem_caches();
3264c66ac9dbSNicholas Bellinger 	if (ret < 0)
3265e3d6f909SAndy Grover 		return ret;
3266c66ac9dbSNicholas Bellinger 	/*
3267c66ac9dbSNicholas Bellinger 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3268c66ac9dbSNicholas Bellinger 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3269c66ac9dbSNicholas Bellinger 	 */
32701ae1602dSChristoph Hellwig 	config_group_init_type_name(&target_core_hbagroup, "core",
32711ae1602dSChristoph Hellwig 			&target_core_cit);
32721ae1602dSChristoph Hellwig 	configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
3273c66ac9dbSNicholas Bellinger 
3274c66ac9dbSNicholas Bellinger 	/*
3275c66ac9dbSNicholas Bellinger 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3276c66ac9dbSNicholas Bellinger 	 */
32771ae1602dSChristoph Hellwig 	config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
32781ae1602dSChristoph Hellwig 	configfs_add_default_group(&alua_group, &target_core_hbagroup);
32791ae1602dSChristoph Hellwig 
3280c66ac9dbSNicholas Bellinger 	/*
3281c66ac9dbSNicholas Bellinger 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3282c66ac9dbSNicholas Bellinger 	 * groups under /sys/kernel/config/target/core/alua/
3283c66ac9dbSNicholas Bellinger 	 */
32841ae1602dSChristoph Hellwig 	config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
32851ae1602dSChristoph Hellwig 			&target_core_alua_lu_gps_cit);
32861ae1602dSChristoph Hellwig 	configfs_add_default_group(&alua_lu_gps_group, &alua_group);
3287c66ac9dbSNicholas Bellinger 
3288c66ac9dbSNicholas Bellinger 	/*
3289c66ac9dbSNicholas Bellinger 	 * Add core/alua/lu_gps/default_lu_gp
3290c66ac9dbSNicholas Bellinger 	 */
3291c66ac9dbSNicholas Bellinger 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
329237bb7899SPeter Senna Tschudin 	if (IS_ERR(lu_gp)) {
329337bb7899SPeter Senna Tschudin 		ret = -ENOMEM;
3294c66ac9dbSNicholas Bellinger 		goto out_global;
329537bb7899SPeter Senna Tschudin 	}
3296c66ac9dbSNicholas Bellinger 
3297c66ac9dbSNicholas Bellinger 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3298c66ac9dbSNicholas Bellinger 				&target_core_alua_lu_gp_cit);
32991ae1602dSChristoph Hellwig 	configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
33001ae1602dSChristoph Hellwig 
3301e3d6f909SAndy Grover 	default_lu_gp = lu_gp;
33021ae1602dSChristoph Hellwig 
3303c66ac9dbSNicholas Bellinger 	/*
3304c66ac9dbSNicholas Bellinger 	 * Register the target_core_mod subsystem with configfs.
3305c66ac9dbSNicholas Bellinger 	 */
3306c66ac9dbSNicholas Bellinger 	ret = configfs_register_subsystem(subsys);
3307c66ac9dbSNicholas Bellinger 	if (ret < 0) {
33086708bb27SAndy Grover 		pr_err("Error %d while registering subsystem %s\n",
3309c66ac9dbSNicholas Bellinger 			ret, subsys->su_group.cg_item.ci_namebuf);
3310c66ac9dbSNicholas Bellinger 		goto out_global;
3311c66ac9dbSNicholas Bellinger 	}
33126708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3313ce8dd25dSChristoph Hellwig 		" Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3314c66ac9dbSNicholas Bellinger 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3315c66ac9dbSNicholas Bellinger 	/*
3316c66ac9dbSNicholas Bellinger 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3317c66ac9dbSNicholas Bellinger 	 */
3318c66ac9dbSNicholas Bellinger 	ret = rd_module_init();
3319c66ac9dbSNicholas Bellinger 	if (ret < 0)
3320c66ac9dbSNicholas Bellinger 		goto out;
3321c66ac9dbSNicholas Bellinger 
33220d0f9dfbSRoland Dreier 	ret = core_dev_setup_virtual_lun0();
33230d0f9dfbSRoland Dreier 	if (ret < 0)
3324c66ac9dbSNicholas Bellinger 		goto out;
3325c66ac9dbSNicholas Bellinger 
3326f99715acSNicholas Bellinger 	ret = target_xcopy_setup_pt();
3327f99715acSNicholas Bellinger 	if (ret < 0)
3328f99715acSNicholas Bellinger 		goto out;
3329f99715acSNicholas Bellinger 
333078a6295cSLee Duncan 	target_init_dbroot();
333178a6295cSLee Duncan 
3332c66ac9dbSNicholas Bellinger 	return 0;
3333c66ac9dbSNicholas Bellinger 
3334c66ac9dbSNicholas Bellinger out:
3335c66ac9dbSNicholas Bellinger 	configfs_unregister_subsystem(subsys);
3336c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3337c66ac9dbSNicholas Bellinger 	rd_module_exit();
3338c66ac9dbSNicholas Bellinger out_global:
3339e3d6f909SAndy Grover 	if (default_lu_gp) {
3340e3d6f909SAndy Grover 		core_alua_free_lu_gp(default_lu_gp);
3341e3d6f909SAndy Grover 		default_lu_gp = NULL;
3342c66ac9dbSNicholas Bellinger 	}
3343e3d6f909SAndy Grover 	release_se_kmem_caches();
3344e3d6f909SAndy Grover 	return ret;
3345c66ac9dbSNicholas Bellinger }
3346c66ac9dbSNicholas Bellinger 
334754550fabSAxel Lin static void __exit target_core_exit_configfs(void)
3348c66ac9dbSNicholas Bellinger {
33491ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&alua_lu_gps_group);
33501ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&alua_group);
33511ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&target_core_hbagroup);
3352c66ac9dbSNicholas Bellinger 
33537c2bf6e9SNicholas Bellinger 	/*
33547c2bf6e9SNicholas Bellinger 	 * We expect subsys->su_group.default_groups to be released
33557c2bf6e9SNicholas Bellinger 	 * by configfs subsystem provider logic..
33567c2bf6e9SNicholas Bellinger 	 */
3357d588cf8fSChristoph Hellwig 	configfs_unregister_subsystem(&target_core_fabrics);
3358c66ac9dbSNicholas Bellinger 
3359e3d6f909SAndy Grover 	core_alua_free_lu_gp(default_lu_gp);
3360e3d6f909SAndy Grover 	default_lu_gp = NULL;
33617c2bf6e9SNicholas Bellinger 
33626708bb27SAndy Grover 	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3363c66ac9dbSNicholas Bellinger 			" Infrastructure\n");
3364c66ac9dbSNicholas Bellinger 
3365c66ac9dbSNicholas Bellinger 	core_dev_release_virtual_lun0();
3366c66ac9dbSNicholas Bellinger 	rd_module_exit();
3367f99715acSNicholas Bellinger 	target_xcopy_release_pt();
3368e3d6f909SAndy Grover 	release_se_kmem_caches();
3369c66ac9dbSNicholas Bellinger }
3370c66ac9dbSNicholas Bellinger 
3371c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3372c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3373c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
3374c66ac9dbSNicholas Bellinger 
3375c66ac9dbSNicholas Bellinger module_init(target_core_init_configfs);
3376c66ac9dbSNicholas Bellinger module_exit(target_core_exit_configfs);
3377