1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c66ac9dbSNicholas Bellinger /*******************************************************************************
3c66ac9dbSNicholas Bellinger * Filename: target_core_configfs.c
4c66ac9dbSNicholas Bellinger *
5c66ac9dbSNicholas Bellinger * This file contains ConfigFS logic for the Generic Target Engine project.
6c66ac9dbSNicholas Bellinger *
74c76251eSNicholas Bellinger * (c) Copyright 2008-2013 Datera, Inc.
8c66ac9dbSNicholas Bellinger *
9c66ac9dbSNicholas Bellinger * Nicholas A. Bellinger <nab@kernel.org>
10c66ac9dbSNicholas Bellinger *
11c66ac9dbSNicholas Bellinger * based on configfs Copyright (C) 2005 Oracle. All rights reserved.
12c66ac9dbSNicholas Bellinger *
13c66ac9dbSNicholas Bellinger ****************************************************************************/
14c66ac9dbSNicholas Bellinger
15e56ca6bcSChristophe JAILLET #include <linux/kstrtox.h>
16c66ac9dbSNicholas Bellinger #include <linux/module.h>
17c66ac9dbSNicholas Bellinger #include <linux/moduleparam.h>
18c66ac9dbSNicholas Bellinger #include <generated/utsrelease.h>
19c66ac9dbSNicholas Bellinger #include <linux/utsname.h>
20c66ac9dbSNicholas Bellinger #include <linux/init.h>
21c66ac9dbSNicholas Bellinger #include <linux/fs.h>
22c66ac9dbSNicholas Bellinger #include <linux/namei.h>
23c66ac9dbSNicholas Bellinger #include <linux/slab.h>
24c66ac9dbSNicholas Bellinger #include <linux/types.h>
25c66ac9dbSNicholas Bellinger #include <linux/delay.h>
26c66ac9dbSNicholas Bellinger #include <linux/unistd.h>
27c66ac9dbSNicholas Bellinger #include <linux/string.h>
28c66ac9dbSNicholas Bellinger #include <linux/parser.h>
29c66ac9dbSNicholas Bellinger #include <linux/syscalls.h>
30c66ac9dbSNicholas Bellinger #include <linux/configfs.h>
31e3d6f909SAndy Grover #include <linux/spinlock.h>
32c66ac9dbSNicholas Bellinger
33c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
34c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
35c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
36c66ac9dbSNicholas Bellinger
37e26d99aeSChristoph Hellwig #include "target_core_internal.h"
38c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
39c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
40c66ac9dbSNicholas Bellinger #include "target_core_rd.h"
41f99715acSNicholas Bellinger #include "target_core_xcopy.h"
42c66ac9dbSNicholas Bellinger
4373112edcSNicholas Bellinger #define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
440a06d430SChristoph Hellwig static void target_core_setup_##_name##_cit(struct target_backend *tb) \
4573112edcSNicholas Bellinger { \
460a06d430SChristoph Hellwig struct config_item_type *cit = &tb->tb_##_name##_cit; \
4773112edcSNicholas Bellinger \
4873112edcSNicholas Bellinger cit->ct_item_ops = _item_ops; \
4973112edcSNicholas Bellinger cit->ct_group_ops = _group_ops; \
5073112edcSNicholas Bellinger cit->ct_attrs = _attrs; \
510a06d430SChristoph Hellwig cit->ct_owner = tb->ops->owner; \
520a06d430SChristoph Hellwig pr_debug("Setup generic %s\n", __stringify(_name)); \
530a06d430SChristoph Hellwig }
540a06d430SChristoph Hellwig
550a06d430SChristoph Hellwig #define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
560a06d430SChristoph Hellwig static void target_core_setup_##_name##_cit(struct target_backend *tb) \
570a06d430SChristoph Hellwig { \
580a06d430SChristoph Hellwig struct config_item_type *cit = &tb->tb_##_name##_cit; \
590a06d430SChristoph Hellwig \
600a06d430SChristoph Hellwig cit->ct_item_ops = _item_ops; \
610a06d430SChristoph Hellwig cit->ct_group_ops = _group_ops; \
620a06d430SChristoph Hellwig cit->ct_attrs = tb->ops->tb_##_name##_attrs; \
630a06d430SChristoph Hellwig cit->ct_owner = tb->ops->owner; \
6473112edcSNicholas Bellinger pr_debug("Setup generic %s\n", __stringify(_name)); \
6573112edcSNicholas Bellinger }
6673112edcSNicholas Bellinger
67e3d6f909SAndy Grover extern struct t10_alua_lu_gp *default_lu_gp;
68e3d6f909SAndy Grover
69d0f474e5SRoland Dreier static LIST_HEAD(g_tf_list);
70d0f474e5SRoland Dreier static DEFINE_MUTEX(g_tf_lock);
71c66ac9dbSNicholas Bellinger
72e3d6f909SAndy Grover static struct config_group target_core_hbagroup;
73e3d6f909SAndy Grover static struct config_group alua_group;
74e3d6f909SAndy Grover static struct config_group alua_lu_gps_group;
75e3d6f909SAndy Grover
7680890c5eSMaurizio Lombardi static unsigned int target_devices;
7780890c5eSMaurizio Lombardi static DEFINE_MUTEX(target_devices_lock);
7880890c5eSMaurizio Lombardi
79c66ac9dbSNicholas Bellinger static inline struct se_hba *
item_to_hba(struct config_item * item)80c66ac9dbSNicholas Bellinger item_to_hba(struct config_item *item)
81c66ac9dbSNicholas Bellinger {
82c66ac9dbSNicholas Bellinger return container_of(to_config_group(item), struct se_hba, hba_group);
83c66ac9dbSNicholas Bellinger }
84c66ac9dbSNicholas Bellinger
85c66ac9dbSNicholas Bellinger /*
86c66ac9dbSNicholas Bellinger * Attributes for /sys/kernel/config/target/
87c66ac9dbSNicholas Bellinger */
target_core_item_version_show(struct config_item * item,char * page)882eafd729SChristoph Hellwig static ssize_t target_core_item_version_show(struct config_item *item,
89c66ac9dbSNicholas Bellinger char *page)
90c66ac9dbSNicholas Bellinger {
91c66ac9dbSNicholas Bellinger return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
92ce8dd25dSChristoph Hellwig " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
93c66ac9dbSNicholas Bellinger utsname()->sysname, utsname()->machine);
94c66ac9dbSNicholas Bellinger }
95c66ac9dbSNicholas Bellinger
962eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_core_item_, version);
97c66ac9dbSNicholas Bellinger
98a96e9783SLee Duncan char db_root[DB_ROOT_LEN] = DB_ROOT_DEFAULT;
99a96e9783SLee Duncan static char db_root_stage[DB_ROOT_LEN];
100a96e9783SLee Duncan
target_core_item_dbroot_show(struct config_item * item,char * page)101a96e9783SLee Duncan static ssize_t target_core_item_dbroot_show(struct config_item *item,
102a96e9783SLee Duncan char *page)
103a96e9783SLee Duncan {
104a96e9783SLee Duncan return sprintf(page, "%s\n", db_root);
105a96e9783SLee Duncan }
106a96e9783SLee Duncan
target_core_item_dbroot_store(struct config_item * item,const char * page,size_t count)107a96e9783SLee Duncan static ssize_t target_core_item_dbroot_store(struct config_item *item,
108a96e9783SLee Duncan const char *page, size_t count)
109a96e9783SLee Duncan {
110a96e9783SLee Duncan ssize_t read_bytes;
111a96e9783SLee Duncan struct file *fp;
11280890c5eSMaurizio Lombardi ssize_t r = -EINVAL;
113a96e9783SLee Duncan
11480890c5eSMaurizio Lombardi mutex_lock(&target_devices_lock);
11580890c5eSMaurizio Lombardi if (target_devices) {
11680890c5eSMaurizio Lombardi pr_err("db_root: cannot be changed because it's in use\n");
11780890c5eSMaurizio Lombardi goto unlock;
118a96e9783SLee Duncan }
119a96e9783SLee Duncan
120a96e9783SLee Duncan if (count > (DB_ROOT_LEN - 1)) {
121a96e9783SLee Duncan pr_err("db_root: count %d exceeds DB_ROOT_LEN-1: %u\n",
122a96e9783SLee Duncan (int)count, DB_ROOT_LEN - 1);
12380890c5eSMaurizio Lombardi goto unlock;
124a96e9783SLee Duncan }
125a96e9783SLee Duncan
126a96e9783SLee Duncan read_bytes = snprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
12780890c5eSMaurizio Lombardi if (!read_bytes)
12880890c5eSMaurizio Lombardi goto unlock;
12980890c5eSMaurizio Lombardi
130a96e9783SLee Duncan if (db_root_stage[read_bytes - 1] == '\n')
131a96e9783SLee Duncan db_root_stage[read_bytes - 1] = '\0';
132a96e9783SLee Duncan
133a96e9783SLee Duncan /* validate new db root before accepting it */
134a96e9783SLee Duncan fp = filp_open(db_root_stage, O_RDONLY, 0);
135a96e9783SLee Duncan if (IS_ERR(fp)) {
136a96e9783SLee Duncan pr_err("db_root: cannot open: %s\n", db_root_stage);
13780890c5eSMaurizio Lombardi goto unlock;
138a96e9783SLee Duncan }
13945063097SAl Viro if (!S_ISDIR(file_inode(fp)->i_mode)) {
1408cc3bb07SBart Van Assche filp_close(fp, NULL);
141a96e9783SLee Duncan pr_err("db_root: not a directory: %s\n", db_root_stage);
14280890c5eSMaurizio Lombardi goto unlock;
143a96e9783SLee Duncan }
1448cc3bb07SBart Van Assche filp_close(fp, NULL);
145a96e9783SLee Duncan
146a96e9783SLee Duncan strncpy(db_root, db_root_stage, read_bytes);
14778a6295cSLee Duncan pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
14878a6295cSLee Duncan
14980890c5eSMaurizio Lombardi r = read_bytes;
15080890c5eSMaurizio Lombardi
15180890c5eSMaurizio Lombardi unlock:
15280890c5eSMaurizio Lombardi mutex_unlock(&target_devices_lock);
15380890c5eSMaurizio Lombardi return r;
154a96e9783SLee Duncan }
155a96e9783SLee Duncan
156a96e9783SLee Duncan CONFIGFS_ATTR(target_core_item_, dbroot);
157a96e9783SLee Duncan
target_core_get_fabric(const char * name)158c66ac9dbSNicholas Bellinger static struct target_fabric_configfs *target_core_get_fabric(
159c66ac9dbSNicholas Bellinger const char *name)
160c66ac9dbSNicholas Bellinger {
161c66ac9dbSNicholas Bellinger struct target_fabric_configfs *tf;
162c66ac9dbSNicholas Bellinger
1636708bb27SAndy Grover if (!name)
164c66ac9dbSNicholas Bellinger return NULL;
165c66ac9dbSNicholas Bellinger
166c66ac9dbSNicholas Bellinger mutex_lock(&g_tf_lock);
167c66ac9dbSNicholas Bellinger list_for_each_entry(tf, &g_tf_list, tf_list) {
16859a206b4SDavid Disseldorp const char *cmp_name = tf->tf_ops->fabric_alias;
16959a206b4SDavid Disseldorp if (!cmp_name)
17059a206b4SDavid Disseldorp cmp_name = tf->tf_ops->fabric_name;
17159a206b4SDavid Disseldorp if (!strcmp(cmp_name, name)) {
172c66ac9dbSNicholas Bellinger atomic_inc(&tf->tf_access_cnt);
173c66ac9dbSNicholas Bellinger mutex_unlock(&g_tf_lock);
174c66ac9dbSNicholas Bellinger return tf;
175c66ac9dbSNicholas Bellinger }
176c66ac9dbSNicholas Bellinger }
177c66ac9dbSNicholas Bellinger mutex_unlock(&g_tf_lock);
178c66ac9dbSNicholas Bellinger
179c66ac9dbSNicholas Bellinger return NULL;
180c66ac9dbSNicholas Bellinger }
181c66ac9dbSNicholas Bellinger
182c66ac9dbSNicholas Bellinger /*
183c66ac9dbSNicholas Bellinger * Called from struct target_core_group_ops->make_group()
184c66ac9dbSNicholas Bellinger */
target_core_register_fabric(struct config_group * group,const char * name)185c66ac9dbSNicholas Bellinger static struct config_group *target_core_register_fabric(
186c66ac9dbSNicholas Bellinger struct config_group *group,
187c66ac9dbSNicholas Bellinger const char *name)
188c66ac9dbSNicholas Bellinger {
189c66ac9dbSNicholas Bellinger struct target_fabric_configfs *tf;
190c66ac9dbSNicholas Bellinger int ret;
191c66ac9dbSNicholas Bellinger
1926708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
193c66ac9dbSNicholas Bellinger " %s\n", group, name);
194e7b7af6eSRoland Dreier
195e7b7af6eSRoland Dreier tf = target_core_get_fabric(name);
196e7b7af6eSRoland Dreier if (!tf) {
19762554910SNicholas Bellinger pr_debug("target_core_register_fabric() trying autoload for %s\n",
198e7b7af6eSRoland Dreier name);
199e7b7af6eSRoland Dreier
200c66ac9dbSNicholas Bellinger /*
201c66ac9dbSNicholas Bellinger * Below are some hardcoded request_module() calls to automatically
202c66ac9dbSNicholas Bellinger * local fabric modules when the following is called:
203c66ac9dbSNicholas Bellinger *
204c66ac9dbSNicholas Bellinger * mkdir -p /sys/kernel/config/target/$MODULE_NAME
205c66ac9dbSNicholas Bellinger *
206c66ac9dbSNicholas Bellinger * Note that this does not limit which TCM fabric module can be
207c66ac9dbSNicholas Bellinger * registered, but simply provids auto loading logic for modules with
208c66ac9dbSNicholas Bellinger * mkdir(2) system calls with known TCM fabric modules.
209c66ac9dbSNicholas Bellinger */
210e7b7af6eSRoland Dreier
2116708bb27SAndy Grover if (!strncmp(name, "iscsi", 5)) {
212c66ac9dbSNicholas Bellinger /*
213c66ac9dbSNicholas Bellinger * Automatically load the LIO Target fabric module when the
214c66ac9dbSNicholas Bellinger * following is called:
215c66ac9dbSNicholas Bellinger *
216c66ac9dbSNicholas Bellinger * mkdir -p $CONFIGFS/target/iscsi
217c66ac9dbSNicholas Bellinger */
218c66ac9dbSNicholas Bellinger ret = request_module("iscsi_target_mod");
219c66ac9dbSNicholas Bellinger if (ret < 0) {
22062554910SNicholas Bellinger pr_debug("request_module() failed for"
221c66ac9dbSNicholas Bellinger " iscsi_target_mod.ko: %d\n", ret);
222c66ac9dbSNicholas Bellinger return ERR_PTR(-EINVAL);
223c66ac9dbSNicholas Bellinger }
2246708bb27SAndy Grover } else if (!strncmp(name, "loopback", 8)) {
225c66ac9dbSNicholas Bellinger /*
226c66ac9dbSNicholas Bellinger * Automatically load the tcm_loop fabric module when the
227c66ac9dbSNicholas Bellinger * following is called:
228c66ac9dbSNicholas Bellinger *
229c66ac9dbSNicholas Bellinger * mkdir -p $CONFIGFS/target/loopback
230c66ac9dbSNicholas Bellinger */
231c66ac9dbSNicholas Bellinger ret = request_module("tcm_loop");
232c66ac9dbSNicholas Bellinger if (ret < 0) {
23362554910SNicholas Bellinger pr_debug("request_module() failed for"
234c66ac9dbSNicholas Bellinger " tcm_loop.ko: %d\n", ret);
235c66ac9dbSNicholas Bellinger return ERR_PTR(-EINVAL);
236c66ac9dbSNicholas Bellinger }
237c66ac9dbSNicholas Bellinger }
238c66ac9dbSNicholas Bellinger
239c66ac9dbSNicholas Bellinger tf = target_core_get_fabric(name);
240e7b7af6eSRoland Dreier }
241e7b7af6eSRoland Dreier
2426708bb27SAndy Grover if (!tf) {
24362554910SNicholas Bellinger pr_debug("target_core_get_fabric() failed for %s\n",
244c66ac9dbSNicholas Bellinger name);
245c66ac9dbSNicholas Bellinger return ERR_PTR(-EINVAL);
246c66ac9dbSNicholas Bellinger }
2476708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
24859a206b4SDavid Disseldorp " %s\n", tf->tf_ops->fabric_name);
249c66ac9dbSNicholas Bellinger /*
250c66ac9dbSNicholas Bellinger * On a successful target_core_get_fabric() look, the returned
251c66ac9dbSNicholas Bellinger * struct target_fabric_configfs *tf will contain a usage reference.
252c66ac9dbSNicholas Bellinger */
2536708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
254968ebe75SChristoph Hellwig &tf->tf_wwn_cit);
255c66ac9dbSNicholas Bellinger
256968ebe75SChristoph Hellwig config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
2571ae1602dSChristoph Hellwig
258c66ac9dbSNicholas Bellinger config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
259968ebe75SChristoph Hellwig &tf->tf_discovery_cit);
2601ae1602dSChristoph Hellwig configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
261c66ac9dbSNicholas Bellinger
2626f3bf5a2SBart Van Assche pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric: %s\n",
2636f3bf5a2SBart Van Assche config_item_name(&tf->tf_group.cg_item));
264c66ac9dbSNicholas Bellinger return &tf->tf_group;
265c66ac9dbSNicholas Bellinger }
266c66ac9dbSNicholas Bellinger
267c66ac9dbSNicholas Bellinger /*
268c66ac9dbSNicholas Bellinger * Called from struct target_core_group_ops->drop_item()
269c66ac9dbSNicholas Bellinger */
target_core_deregister_fabric(struct config_group * group,struct config_item * item)270c66ac9dbSNicholas Bellinger static void target_core_deregister_fabric(
271c66ac9dbSNicholas Bellinger struct config_group *group,
272c66ac9dbSNicholas Bellinger struct config_item *item)
273c66ac9dbSNicholas Bellinger {
274c66ac9dbSNicholas Bellinger struct target_fabric_configfs *tf = container_of(
275c66ac9dbSNicholas Bellinger to_config_group(item), struct target_fabric_configfs, tf_group);
276c66ac9dbSNicholas Bellinger
2776708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
278c66ac9dbSNicholas Bellinger " tf list\n", config_item_name(item));
279c66ac9dbSNicholas Bellinger
2806708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
28159a206b4SDavid Disseldorp " %s\n", tf->tf_ops->fabric_name);
282c66ac9dbSNicholas Bellinger atomic_dec(&tf->tf_access_cnt);
283c66ac9dbSNicholas Bellinger
2846708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
285c66ac9dbSNicholas Bellinger " %s\n", config_item_name(item));
286c66ac9dbSNicholas Bellinger
2871ae1602dSChristoph Hellwig configfs_remove_default_groups(&tf->tf_group);
288c66ac9dbSNicholas Bellinger config_item_put(item);
289c66ac9dbSNicholas Bellinger }
290c66ac9dbSNicholas Bellinger
291c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_fabric_group_ops = {
292c66ac9dbSNicholas Bellinger .make_group = &target_core_register_fabric,
293c66ac9dbSNicholas Bellinger .drop_item = &target_core_deregister_fabric,
294c66ac9dbSNicholas Bellinger };
295c66ac9dbSNicholas Bellinger
296c66ac9dbSNicholas Bellinger /*
297c66ac9dbSNicholas Bellinger * All item attributes appearing in /sys/kernel/target/ appear here.
298c66ac9dbSNicholas Bellinger */
299c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_fabric_item_attrs[] = {
300c66ac9dbSNicholas Bellinger &target_core_item_attr_version,
301a96e9783SLee Duncan &target_core_item_attr_dbroot,
302c66ac9dbSNicholas Bellinger NULL,
303c66ac9dbSNicholas Bellinger };
304c66ac9dbSNicholas Bellinger
305c66ac9dbSNicholas Bellinger /*
306c66ac9dbSNicholas Bellinger * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
307c66ac9dbSNicholas Bellinger */
308ece550b5SBhumika Goyal static const struct config_item_type target_core_fabrics_item = {
309c66ac9dbSNicholas Bellinger .ct_group_ops = &target_core_fabric_group_ops,
310c66ac9dbSNicholas Bellinger .ct_attrs = target_core_fabric_item_attrs,
311c66ac9dbSNicholas Bellinger .ct_owner = THIS_MODULE,
312c66ac9dbSNicholas Bellinger };
313c66ac9dbSNicholas Bellinger
314c66ac9dbSNicholas Bellinger static struct configfs_subsystem target_core_fabrics = {
315c66ac9dbSNicholas Bellinger .su_group = {
316c66ac9dbSNicholas Bellinger .cg_item = {
317c66ac9dbSNicholas Bellinger .ci_namebuf = "target",
318c66ac9dbSNicholas Bellinger .ci_type = &target_core_fabrics_item,
319c66ac9dbSNicholas Bellinger },
320c66ac9dbSNicholas Bellinger },
321c66ac9dbSNicholas Bellinger };
322c66ac9dbSNicholas Bellinger
target_depend_item(struct config_item * item)323d588cf8fSChristoph Hellwig int target_depend_item(struct config_item *item)
324d588cf8fSChristoph Hellwig {
325d588cf8fSChristoph Hellwig return configfs_depend_item(&target_core_fabrics, item);
326d588cf8fSChristoph Hellwig }
327d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_depend_item);
328d588cf8fSChristoph Hellwig
target_undepend_item(struct config_item * item)329d588cf8fSChristoph Hellwig void target_undepend_item(struct config_item *item)
330d588cf8fSChristoph Hellwig {
3319a9e3415SKrzysztof Opasiak return configfs_undepend_item(item);
332d588cf8fSChristoph Hellwig }
333d588cf8fSChristoph Hellwig EXPORT_SYMBOL(target_undepend_item);
334c66ac9dbSNicholas Bellinger
335c66ac9dbSNicholas Bellinger /*##############################################################################
336c66ac9dbSNicholas Bellinger // Start functions called by external Target Fabrics Modules
337c66ac9dbSNicholas Bellinger //############################################################################*/
target_disable_feature(struct se_portal_group * se_tpg)338e5dc6e44SDmitry Bogdanov static int target_disable_feature(struct se_portal_group *se_tpg)
339e5dc6e44SDmitry Bogdanov {
340e5dc6e44SDmitry Bogdanov return 0;
341e5dc6e44SDmitry Bogdanov }
342e5dc6e44SDmitry Bogdanov
target_default_get_inst_index(struct se_portal_group * se_tpg)343e5dc6e44SDmitry Bogdanov static u32 target_default_get_inst_index(struct se_portal_group *se_tpg)
344e5dc6e44SDmitry Bogdanov {
345e5dc6e44SDmitry Bogdanov return 1;
346e5dc6e44SDmitry Bogdanov }
347e5dc6e44SDmitry Bogdanov
target_default_sess_get_index(struct se_session * se_sess)348e5dc6e44SDmitry Bogdanov static u32 target_default_sess_get_index(struct se_session *se_sess)
349e5dc6e44SDmitry Bogdanov {
350e5dc6e44SDmitry Bogdanov return 0;
351e5dc6e44SDmitry Bogdanov }
352e5dc6e44SDmitry Bogdanov
target_set_default_node_attributes(struct se_node_acl * se_acl)353e5dc6e44SDmitry Bogdanov static void target_set_default_node_attributes(struct se_node_acl *se_acl)
354e5dc6e44SDmitry Bogdanov {
355e5dc6e44SDmitry Bogdanov }
356e5dc6e44SDmitry Bogdanov
target_default_get_cmd_state(struct se_cmd * se_cmd)357e5dc6e44SDmitry Bogdanov static int target_default_get_cmd_state(struct se_cmd *se_cmd)
358e5dc6e44SDmitry Bogdanov {
359e5dc6e44SDmitry Bogdanov return 0;
360e5dc6e44SDmitry Bogdanov }
361c66ac9dbSNicholas Bellinger
target_fabric_tf_ops_check(const struct target_core_fabric_ops * tfo)3629ac8928eSChristoph Hellwig static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
363c66ac9dbSNicholas Bellinger {
36459a206b4SDavid Disseldorp if (tfo->fabric_alias) {
36559a206b4SDavid Disseldorp if (strlen(tfo->fabric_alias) >= TARGET_FABRIC_NAME_SIZE) {
36659a206b4SDavid Disseldorp pr_err("Passed alias: %s exceeds "
36759a206b4SDavid Disseldorp "TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_alias);
3689ac8928eSChristoph Hellwig return -EINVAL;
369c66ac9dbSNicholas Bellinger }
370c66ac9dbSNicholas Bellinger }
37130c7ca93SDavid Disseldorp if (!tfo->fabric_name) {
37230c7ca93SDavid Disseldorp pr_err("Missing tfo->fabric_name\n");
373c66ac9dbSNicholas Bellinger return -EINVAL;
374c66ac9dbSNicholas Bellinger }
37559a206b4SDavid Disseldorp if (strlen(tfo->fabric_name) >= TARGET_FABRIC_NAME_SIZE) {
37659a206b4SDavid Disseldorp pr_err("Passed name: %s exceeds "
37759a206b4SDavid Disseldorp "TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_name);
37859a206b4SDavid Disseldorp return -EINVAL;
37959a206b4SDavid Disseldorp }
3806708bb27SAndy Grover if (!tfo->tpg_get_wwn) {
3816708bb27SAndy Grover pr_err("Missing tfo->tpg_get_wwn()\n");
382c66ac9dbSNicholas Bellinger return -EINVAL;
383c66ac9dbSNicholas Bellinger }
3846708bb27SAndy Grover if (!tfo->tpg_get_tag) {
3856708bb27SAndy Grover pr_err("Missing tfo->tpg_get_tag()\n");
386c66ac9dbSNicholas Bellinger return -EINVAL;
387c66ac9dbSNicholas Bellinger }
38835462975SChristoph Hellwig if (!tfo->release_cmd) {
3896708bb27SAndy Grover pr_err("Missing tfo->release_cmd()\n");
390c66ac9dbSNicholas Bellinger return -EINVAL;
391c66ac9dbSNicholas Bellinger }
3926708bb27SAndy Grover if (!tfo->write_pending) {
3936708bb27SAndy Grover pr_err("Missing tfo->write_pending()\n");
394c66ac9dbSNicholas Bellinger return -EINVAL;
395c66ac9dbSNicholas Bellinger }
3966708bb27SAndy Grover if (!tfo->queue_data_in) {
3976708bb27SAndy Grover pr_err("Missing tfo->queue_data_in()\n");
398c66ac9dbSNicholas Bellinger return -EINVAL;
399c66ac9dbSNicholas Bellinger }
4006708bb27SAndy Grover if (!tfo->queue_status) {
4016708bb27SAndy Grover pr_err("Missing tfo->queue_status()\n");
402c66ac9dbSNicholas Bellinger return -EINVAL;
403c66ac9dbSNicholas Bellinger }
4046708bb27SAndy Grover if (!tfo->queue_tm_rsp) {
4056708bb27SAndy Grover pr_err("Missing tfo->queue_tm_rsp()\n");
406c66ac9dbSNicholas Bellinger return -EINVAL;
407c66ac9dbSNicholas Bellinger }
408131e6abcSNicholas Bellinger if (!tfo->aborted_task) {
409131e6abcSNicholas Bellinger pr_err("Missing tfo->aborted_task()\n");
410131e6abcSNicholas Bellinger return -EINVAL;
411131e6abcSNicholas Bellinger }
4129c28ca4fSNicholas Bellinger if (!tfo->check_stop_free) {
4139c28ca4fSNicholas Bellinger pr_err("Missing tfo->check_stop_free()\n");
4149c28ca4fSNicholas Bellinger return -EINVAL;
4159c28ca4fSNicholas Bellinger }
416c66ac9dbSNicholas Bellinger /*
417c66ac9dbSNicholas Bellinger * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
418c66ac9dbSNicholas Bellinger * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
419c66ac9dbSNicholas Bellinger * target_core_fabric_configfs.c WWN+TPG group context code.
420c66ac9dbSNicholas Bellinger */
4216708bb27SAndy Grover if (!tfo->fabric_make_wwn) {
4226708bb27SAndy Grover pr_err("Missing tfo->fabric_make_wwn()\n");
423c66ac9dbSNicholas Bellinger return -EINVAL;
424c66ac9dbSNicholas Bellinger }
4256708bb27SAndy Grover if (!tfo->fabric_drop_wwn) {
4266708bb27SAndy Grover pr_err("Missing tfo->fabric_drop_wwn()\n");
427c66ac9dbSNicholas Bellinger return -EINVAL;
428c66ac9dbSNicholas Bellinger }
4296708bb27SAndy Grover if (!tfo->fabric_make_tpg) {
4306708bb27SAndy Grover pr_err("Missing tfo->fabric_make_tpg()\n");
431c66ac9dbSNicholas Bellinger return -EINVAL;
432c66ac9dbSNicholas Bellinger }
4336708bb27SAndy Grover if (!tfo->fabric_drop_tpg) {
4346708bb27SAndy Grover pr_err("Missing tfo->fabric_drop_tpg()\n");
435c66ac9dbSNicholas Bellinger return -EINVAL;
436c66ac9dbSNicholas Bellinger }
437c66ac9dbSNicholas Bellinger
438c66ac9dbSNicholas Bellinger return 0;
439c66ac9dbSNicholas Bellinger }
440c66ac9dbSNicholas Bellinger
target_set_default_ops(struct target_core_fabric_ops * tfo)441e5dc6e44SDmitry Bogdanov static void target_set_default_ops(struct target_core_fabric_ops *tfo)
442e5dc6e44SDmitry Bogdanov {
443e5dc6e44SDmitry Bogdanov if (!tfo->tpg_check_demo_mode)
444e5dc6e44SDmitry Bogdanov tfo->tpg_check_demo_mode = target_disable_feature;
445e5dc6e44SDmitry Bogdanov
446e5dc6e44SDmitry Bogdanov if (!tfo->tpg_check_demo_mode_cache)
447e5dc6e44SDmitry Bogdanov tfo->tpg_check_demo_mode_cache = target_disable_feature;
448e5dc6e44SDmitry Bogdanov
449e5dc6e44SDmitry Bogdanov if (!tfo->tpg_check_demo_mode_write_protect)
450e5dc6e44SDmitry Bogdanov tfo->tpg_check_demo_mode_write_protect = target_disable_feature;
451e5dc6e44SDmitry Bogdanov
452e5dc6e44SDmitry Bogdanov if (!tfo->tpg_check_prod_mode_write_protect)
453e5dc6e44SDmitry Bogdanov tfo->tpg_check_prod_mode_write_protect = target_disable_feature;
454e5dc6e44SDmitry Bogdanov
455e5dc6e44SDmitry Bogdanov if (!tfo->tpg_get_inst_index)
456e5dc6e44SDmitry Bogdanov tfo->tpg_get_inst_index = target_default_get_inst_index;
457e5dc6e44SDmitry Bogdanov
458e5dc6e44SDmitry Bogdanov if (!tfo->sess_get_index)
459e5dc6e44SDmitry Bogdanov tfo->sess_get_index = target_default_sess_get_index;
460e5dc6e44SDmitry Bogdanov
461e5dc6e44SDmitry Bogdanov if (!tfo->set_default_node_attributes)
462e5dc6e44SDmitry Bogdanov tfo->set_default_node_attributes = target_set_default_node_attributes;
463e5dc6e44SDmitry Bogdanov
464e5dc6e44SDmitry Bogdanov if (!tfo->get_cmd_state)
465e5dc6e44SDmitry Bogdanov tfo->get_cmd_state = target_default_get_cmd_state;
466e5dc6e44SDmitry Bogdanov }
467e5dc6e44SDmitry Bogdanov
target_register_template(const struct target_core_fabric_ops * fo)4689ac8928eSChristoph Hellwig int target_register_template(const struct target_core_fabric_ops *fo)
469c66ac9dbSNicholas Bellinger {
470e5dc6e44SDmitry Bogdanov struct target_core_fabric_ops *tfo;
4719ac8928eSChristoph Hellwig struct target_fabric_configfs *tf;
472c66ac9dbSNicholas Bellinger int ret;
473c66ac9dbSNicholas Bellinger
4749ac8928eSChristoph Hellwig ret = target_fabric_tf_ops_check(fo);
4759ac8928eSChristoph Hellwig if (ret)
476c66ac9dbSNicholas Bellinger return ret;
477c66ac9dbSNicholas Bellinger
4789ac8928eSChristoph Hellwig tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
4799ac8928eSChristoph Hellwig if (!tf) {
4809ac8928eSChristoph Hellwig pr_err("%s: could not allocate memory!\n", __func__);
4819ac8928eSChristoph Hellwig return -ENOMEM;
4829ac8928eSChristoph Hellwig }
483e5dc6e44SDmitry Bogdanov tfo = kzalloc(sizeof(struct target_core_fabric_ops), GFP_KERNEL);
484e5dc6e44SDmitry Bogdanov if (!tfo) {
485e5dc6e44SDmitry Bogdanov kfree(tf);
486e5dc6e44SDmitry Bogdanov pr_err("%s: could not allocate memory!\n", __func__);
487e5dc6e44SDmitry Bogdanov return -ENOMEM;
488e5dc6e44SDmitry Bogdanov }
489e5dc6e44SDmitry Bogdanov memcpy(tfo, fo, sizeof(*tfo));
490e5dc6e44SDmitry Bogdanov target_set_default_ops(tfo);
4919ac8928eSChristoph Hellwig
4929ac8928eSChristoph Hellwig INIT_LIST_HEAD(&tf->tf_list);
4939ac8928eSChristoph Hellwig atomic_set(&tf->tf_access_cnt, 0);
494e5dc6e44SDmitry Bogdanov tf->tf_ops = tfo;
4959ac8928eSChristoph Hellwig target_fabric_setup_cits(tf);
4969ac8928eSChristoph Hellwig
4979ac8928eSChristoph Hellwig mutex_lock(&g_tf_lock);
4989ac8928eSChristoph Hellwig list_add_tail(&tf->tf_list, &g_tf_list);
4999ac8928eSChristoph Hellwig mutex_unlock(&g_tf_lock);
5009ac8928eSChristoph Hellwig
501c66ac9dbSNicholas Bellinger return 0;
502c66ac9dbSNicholas Bellinger }
5039ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_register_template);
504c66ac9dbSNicholas Bellinger
target_unregister_template(const struct target_core_fabric_ops * fo)5059ac8928eSChristoph Hellwig void target_unregister_template(const struct target_core_fabric_ops *fo)
506c66ac9dbSNicholas Bellinger {
5079ac8928eSChristoph Hellwig struct target_fabric_configfs *t;
508c66ac9dbSNicholas Bellinger
509c66ac9dbSNicholas Bellinger mutex_lock(&g_tf_lock);
5109ac8928eSChristoph Hellwig list_for_each_entry(t, &g_tf_list, tf_list) {
51159a206b4SDavid Disseldorp if (!strcmp(t->tf_ops->fabric_name, fo->fabric_name)) {
5129ac8928eSChristoph Hellwig BUG_ON(atomic_read(&t->tf_access_cnt));
5139ac8928eSChristoph Hellwig list_del(&t->tf_list);
51494509182SNicholas Bellinger mutex_unlock(&g_tf_lock);
51594509182SNicholas Bellinger /*
51694509182SNicholas Bellinger * Wait for any outstanding fabric se_deve_entry->rcu_head
51794509182SNicholas Bellinger * callbacks to complete post kfree_rcu(), before allowing
51894509182SNicholas Bellinger * fabric driver unload of TFO->module to proceed.
51994509182SNicholas Bellinger */
52094509182SNicholas Bellinger rcu_barrier();
52180ed33c8SDmitry Bogdanov kfree(t->tf_tpg_base_cit.ct_attrs);
522e5dc6e44SDmitry Bogdanov kfree(t->tf_ops);
5239ac8928eSChristoph Hellwig kfree(t);
52494509182SNicholas Bellinger return;
525c66ac9dbSNicholas Bellinger }
526c66ac9dbSNicholas Bellinger }
5279ac8928eSChristoph Hellwig mutex_unlock(&g_tf_lock);
5289ac8928eSChristoph Hellwig }
5299ac8928eSChristoph Hellwig EXPORT_SYMBOL(target_unregister_template);
530c66ac9dbSNicholas Bellinger
531c66ac9dbSNicholas Bellinger /*##############################################################################
532c66ac9dbSNicholas Bellinger // Stop functions called by external Target Fabrics Modules
533c66ac9dbSNicholas Bellinger //############################################################################*/
534c66ac9dbSNicholas Bellinger
to_attrib(struct config_item * item)5352eafd729SChristoph Hellwig static inline struct se_dev_attrib *to_attrib(struct config_item *item)
5362eafd729SChristoph Hellwig {
5372eafd729SChristoph Hellwig return container_of(to_config_group(item), struct se_dev_attrib,
5382eafd729SChristoph Hellwig da_group);
5395873c4d1SChristoph Hellwig }
5405873c4d1SChristoph Hellwig
5412eafd729SChristoph Hellwig /* Start functions for struct config_item_type tb_dev_attrib_cit */
5422eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_SHOW(_name) \
5432eafd729SChristoph Hellwig static ssize_t _name##_show(struct config_item *item, char *page) \
5442eafd729SChristoph Hellwig { \
5452eafd729SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
5462eafd729SChristoph Hellwig }
5473effdb90SChristoph Hellwig
5482eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
5492eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
5502eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
5512eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
5522eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
5532eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
5542eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
5552eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
5562eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
5572eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
5582eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
559b49d6f78SDavid Disseldorp DEF_CONFIGFS_ATTRIB_SHOW(emulate_pr);
5602eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
5612eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
562056e8924SDmitry Monakhov DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_verify);
5632eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
5642eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
5652eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
5662eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
5672eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
5682eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(block_size);
5692eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
5702eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
5712eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
5722eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
5732eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
5742eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
5752eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
5762eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
577e6f41633SJamie Pocas DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
5782eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
579bd217b8cSDmitry Bogdanov DEF_CONFIGFS_ATTRIB_SHOW(emulate_rsoc);
5802eafd729SChristoph Hellwig
5812eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_U32(_name) \
5822eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
5835873c4d1SChristoph Hellwig size_t count) \
5845873c4d1SChristoph Hellwig { \
5852eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item); \
5863effdb90SChristoph Hellwig u32 val; \
5875873c4d1SChristoph Hellwig int ret; \
5885873c4d1SChristoph Hellwig \
5893effdb90SChristoph Hellwig ret = kstrtou32(page, 0, &val); \
5903effdb90SChristoph Hellwig if (ret < 0) \
5913effdb90SChristoph Hellwig return ret; \
5923effdb90SChristoph Hellwig da->_name = val; \
5933effdb90SChristoph Hellwig return count; \
5945873c4d1SChristoph Hellwig }
5955873c4d1SChristoph Hellwig
5962eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
5972eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
5982eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
5992eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
6002eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
6015873c4d1SChristoph Hellwig
6022eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name) \
6032eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page, \
6043effdb90SChristoph Hellwig size_t count) \
6053effdb90SChristoph Hellwig { \
6062eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item); \
6073effdb90SChristoph Hellwig bool flag; \
6083effdb90SChristoph Hellwig int ret; \
6093effdb90SChristoph Hellwig \
610e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag); \
6113effdb90SChristoph Hellwig if (ret < 0) \
6123effdb90SChristoph Hellwig return ret; \
6133effdb90SChristoph Hellwig da->_name = flag; \
6143effdb90SChristoph Hellwig return count; \
6153effdb90SChristoph Hellwig }
6163effdb90SChristoph Hellwig
6172eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
6182eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
6192eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
620b49d6f78SDavid Disseldorp DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_pr);
6212eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
6222eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
6233effdb90SChristoph Hellwig
6242eafd729SChristoph Hellwig #define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name) \
6252eafd729SChristoph Hellwig static ssize_t _name##_store(struct config_item *item, const char *page,\
6263effdb90SChristoph Hellwig size_t count) \
6273effdb90SChristoph Hellwig { \
6283effdb90SChristoph Hellwig printk_once(KERN_WARNING \
629234bdbc4SChristophe Vu-Brugier "ignoring deprecated %s attribute\n", \
630234bdbc4SChristophe Vu-Brugier __stringify(_name)); \
6313effdb90SChristoph Hellwig return count; \
6323effdb90SChristoph Hellwig }
6333effdb90SChristoph Hellwig
6342eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
6352eafd729SChristoph Hellwig DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
6363effdb90SChristoph Hellwig
dev_set_t10_wwn_model_alias(struct se_device * dev)6373effdb90SChristoph Hellwig static void dev_set_t10_wwn_model_alias(struct se_device *dev)
6383effdb90SChristoph Hellwig {
6393effdb90SChristoph Hellwig const char *configname;
6403effdb90SChristoph Hellwig
6413effdb90SChristoph Hellwig configname = config_item_name(&dev->dev_group.cg_item);
642b2da4abfSDavid Disseldorp if (strlen(configname) >= INQUIRY_MODEL_LEN) {
6433effdb90SChristoph Hellwig pr_warn("dev[%p]: Backstore name '%s' is too long for "
644b2da4abfSDavid Disseldorp "INQUIRY_MODEL, truncating to 15 characters\n", dev,
6453effdb90SChristoph Hellwig configname);
6463effdb90SChristoph Hellwig }
647b2da4abfSDavid Disseldorp /*
648b2da4abfSDavid Disseldorp * XXX We can't use sizeof(dev->t10_wwn.model) (INQUIRY_MODEL_LEN + 1)
649b2da4abfSDavid Disseldorp * here without potentially breaking existing setups, so continue to
650b2da4abfSDavid Disseldorp * truncate one byte shorter than what can be carried in INQUIRY.
651b2da4abfSDavid Disseldorp */
6520871237aSAzeem Shaikh strscpy(dev->t10_wwn.model, configname, INQUIRY_MODEL_LEN);
6533effdb90SChristoph Hellwig }
6543effdb90SChristoph Hellwig
emulate_model_alias_store(struct config_item * item,const char * page,size_t count)6552eafd729SChristoph Hellwig static ssize_t emulate_model_alias_store(struct config_item *item,
6563effdb90SChristoph Hellwig const char *page, size_t count)
6573effdb90SChristoph Hellwig {
6582eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item);
6593effdb90SChristoph Hellwig struct se_device *dev = da->da_dev;
6603effdb90SChristoph Hellwig bool flag;
6613effdb90SChristoph Hellwig int ret;
6623effdb90SChristoph Hellwig
6633effdb90SChristoph Hellwig if (dev->export_count) {
6643effdb90SChristoph Hellwig pr_err("dev[%p]: Unable to change model alias"
6653effdb90SChristoph Hellwig " while export_count is %d\n",
6663effdb90SChristoph Hellwig dev, dev->export_count);
6673effdb90SChristoph Hellwig return -EINVAL;
6683effdb90SChristoph Hellwig }
6693effdb90SChristoph Hellwig
670e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag);
6713effdb90SChristoph Hellwig if (ret < 0)
6723effdb90SChristoph Hellwig return ret;
6733effdb90SChristoph Hellwig
674b2da4abfSDavid Disseldorp BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
6753effdb90SChristoph Hellwig if (flag) {
6763effdb90SChristoph Hellwig dev_set_t10_wwn_model_alias(dev);
6773effdb90SChristoph Hellwig } else {
6780871237aSAzeem Shaikh strscpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
679b2da4abfSDavid Disseldorp sizeof(dev->t10_wwn.model));
6803effdb90SChristoph Hellwig }
6813effdb90SChristoph Hellwig da->emulate_model_alias = flag;
6823effdb90SChristoph Hellwig return count;
6833effdb90SChristoph Hellwig }
6843effdb90SChristoph Hellwig
emulate_write_cache_store(struct config_item * item,const char * page,size_t count)6852eafd729SChristoph Hellwig static ssize_t emulate_write_cache_store(struct config_item *item,
6863effdb90SChristoph Hellwig const char *page, size_t count)
6873effdb90SChristoph Hellwig {
6882eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item);
6893effdb90SChristoph Hellwig bool flag;
6903effdb90SChristoph Hellwig int ret;
6913effdb90SChristoph Hellwig
692e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag);
6933effdb90SChristoph Hellwig if (ret < 0)
6943effdb90SChristoph Hellwig return ret;
6953effdb90SChristoph Hellwig
6963effdb90SChristoph Hellwig if (flag && da->da_dev->transport->get_write_cache) {
6973effdb90SChristoph Hellwig pr_err("emulate_write_cache not supported for this device\n");
6983effdb90SChristoph Hellwig return -EINVAL;
6993effdb90SChristoph Hellwig }
7003effdb90SChristoph Hellwig
7013effdb90SChristoph Hellwig da->emulate_write_cache = flag;
7023effdb90SChristoph Hellwig pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
7033effdb90SChristoph Hellwig da->da_dev, flag);
7043effdb90SChristoph Hellwig return count;
7053effdb90SChristoph Hellwig }
7063effdb90SChristoph Hellwig
emulate_ua_intlck_ctrl_store(struct config_item * item,const char * page,size_t count)7072eafd729SChristoph Hellwig static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
7083effdb90SChristoph Hellwig const char *page, size_t count)
7093effdb90SChristoph Hellwig {
7102eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item);
7113effdb90SChristoph Hellwig u32 val;
7123effdb90SChristoph Hellwig int ret;
7133effdb90SChristoph Hellwig
7143effdb90SChristoph Hellwig ret = kstrtou32(page, 0, &val);
7153effdb90SChristoph Hellwig if (ret < 0)
7163effdb90SChristoph Hellwig return ret;
7173effdb90SChristoph Hellwig
7181bf630fdSDavid Disseldorp if (val != TARGET_UA_INTLCK_CTRL_CLEAR
7191bf630fdSDavid Disseldorp && val != TARGET_UA_INTLCK_CTRL_NO_CLEAR
7201bf630fdSDavid Disseldorp && val != TARGET_UA_INTLCK_CTRL_ESTABLISH_UA) {
7213effdb90SChristoph Hellwig pr_err("Illegal value %d\n", val);
7223effdb90SChristoph Hellwig return -EINVAL;
7233effdb90SChristoph Hellwig }
7243effdb90SChristoph Hellwig
7253effdb90SChristoph Hellwig if (da->da_dev->export_count) {
7263effdb90SChristoph Hellwig pr_err("dev[%p]: Unable to change SE Device"
7273effdb90SChristoph Hellwig " UA_INTRLCK_CTRL while export_count is %d\n",
7283effdb90SChristoph Hellwig da->da_dev, da->da_dev->export_count);
7293effdb90SChristoph Hellwig return -EINVAL;
7303effdb90SChristoph Hellwig }
7313effdb90SChristoph Hellwig da->emulate_ua_intlck_ctrl = val;
7323effdb90SChristoph Hellwig pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
7333effdb90SChristoph Hellwig da->da_dev, val);
7343effdb90SChristoph Hellwig return count;
7353effdb90SChristoph Hellwig }
7363effdb90SChristoph Hellwig
emulate_tas_store(struct config_item * item,const char * page,size_t count)7372eafd729SChristoph Hellwig static ssize_t emulate_tas_store(struct config_item *item,
7383effdb90SChristoph Hellwig const char *page, size_t count)
7393effdb90SChristoph Hellwig {
7402eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item);
7413effdb90SChristoph Hellwig bool flag;
7423effdb90SChristoph Hellwig int ret;
7433effdb90SChristoph Hellwig
744e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag);
7453effdb90SChristoph Hellwig if (ret < 0)
7463effdb90SChristoph Hellwig return ret;
7473effdb90SChristoph Hellwig
7483effdb90SChristoph Hellwig if (da->da_dev->export_count) {
7493effdb90SChristoph Hellwig pr_err("dev[%p]: Unable to change SE Device TAS while"
7503effdb90SChristoph Hellwig " export_count is %d\n",
7513effdb90SChristoph Hellwig da->da_dev, da->da_dev->export_count);
7523effdb90SChristoph Hellwig return -EINVAL;
7533effdb90SChristoph Hellwig }
7543effdb90SChristoph Hellwig da->emulate_tas = flag;
7553effdb90SChristoph Hellwig pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
7563effdb90SChristoph Hellwig da->da_dev, flag ? "Enabled" : "Disabled");
7573effdb90SChristoph Hellwig
7583effdb90SChristoph Hellwig return count;
7593effdb90SChristoph Hellwig }
7603effdb90SChristoph Hellwig
emulate_tpu_store(struct config_item * item,const char * page,size_t count)7612eafd729SChristoph Hellwig static ssize_t emulate_tpu_store(struct config_item *item,
7623effdb90SChristoph Hellwig const char *page, size_t count)
7633effdb90SChristoph Hellwig {
7642eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item);
76534bd1dcaSMike Christie struct se_device *dev = da->da_dev;
7663effdb90SChristoph Hellwig bool flag;
7673effdb90SChristoph Hellwig int ret;
7683effdb90SChristoph Hellwig
769e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag);
7703effdb90SChristoph Hellwig if (ret < 0)
7713effdb90SChristoph Hellwig return ret;
7723effdb90SChristoph Hellwig
7733effdb90SChristoph Hellwig /*
7743effdb90SChristoph Hellwig * We expect this value to be non-zero when generic Block Layer
7753effdb90SChristoph Hellwig * Discard supported is detected iblock_create_virtdevice().
7763effdb90SChristoph Hellwig */
7773effdb90SChristoph Hellwig if (flag && !da->max_unmap_block_desc_count) {
77834bd1dcaSMike Christie if (!dev->transport->configure_unmap ||
77934bd1dcaSMike Christie !dev->transport->configure_unmap(dev)) {
7803effdb90SChristoph Hellwig pr_err("Generic Block Discard not supported\n");
7813effdb90SChristoph Hellwig return -ENOSYS;
7823effdb90SChristoph Hellwig }
78334bd1dcaSMike Christie }
7843effdb90SChristoph Hellwig
7853effdb90SChristoph Hellwig da->emulate_tpu = flag;
7863effdb90SChristoph Hellwig pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
7873effdb90SChristoph Hellwig da->da_dev, flag);
7883effdb90SChristoph Hellwig return count;
7893effdb90SChristoph Hellwig }
7903effdb90SChristoph Hellwig
emulate_tpws_store(struct config_item * item,const char * page,size_t count)7912eafd729SChristoph Hellwig static ssize_t emulate_tpws_store(struct config_item *item,
7923effdb90SChristoph Hellwig const char *page, size_t count)
7933effdb90SChristoph Hellwig {
7942eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item);
79534bd1dcaSMike Christie struct se_device *dev = da->da_dev;
7963effdb90SChristoph Hellwig bool flag;
7973effdb90SChristoph Hellwig int ret;
7983effdb90SChristoph Hellwig
799e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag);
8003effdb90SChristoph Hellwig if (ret < 0)
8013effdb90SChristoph Hellwig return ret;
8023effdb90SChristoph Hellwig
8033effdb90SChristoph Hellwig /*
8043effdb90SChristoph Hellwig * We expect this value to be non-zero when generic Block Layer
8053effdb90SChristoph Hellwig * Discard supported is detected iblock_create_virtdevice().
8063effdb90SChristoph Hellwig */
8073effdb90SChristoph Hellwig if (flag && !da->max_unmap_block_desc_count) {
80834bd1dcaSMike Christie if (!dev->transport->configure_unmap ||
80934bd1dcaSMike Christie !dev->transport->configure_unmap(dev)) {
8103effdb90SChristoph Hellwig pr_err("Generic Block Discard not supported\n");
8113effdb90SChristoph Hellwig return -ENOSYS;
8123effdb90SChristoph Hellwig }
81334bd1dcaSMike Christie }
8143effdb90SChristoph Hellwig
8153effdb90SChristoph Hellwig da->emulate_tpws = flag;
8163effdb90SChristoph Hellwig pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
8173effdb90SChristoph Hellwig da->da_dev, flag);
8183effdb90SChristoph Hellwig return count;
8193effdb90SChristoph Hellwig }
8203effdb90SChristoph Hellwig
pi_prot_type_store(struct config_item * item,const char * page,size_t count)8212eafd729SChristoph Hellwig static ssize_t pi_prot_type_store(struct config_item *item,
8223effdb90SChristoph Hellwig const char *page, size_t count)
8233effdb90SChristoph Hellwig {
8242eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item);
8253effdb90SChristoph Hellwig int old_prot = da->pi_prot_type, ret;
8263effdb90SChristoph Hellwig struct se_device *dev = da->da_dev;
8273effdb90SChristoph Hellwig u32 flag;
8283effdb90SChristoph Hellwig
8293effdb90SChristoph Hellwig ret = kstrtou32(page, 0, &flag);
8303effdb90SChristoph Hellwig if (ret < 0)
8313effdb90SChristoph Hellwig return ret;
8323effdb90SChristoph Hellwig
8333effdb90SChristoph Hellwig if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
8343effdb90SChristoph Hellwig pr_err("Illegal value %d for pi_prot_type\n", flag);
8353effdb90SChristoph Hellwig return -EINVAL;
8363effdb90SChristoph Hellwig }
8373effdb90SChristoph Hellwig if (flag == 2) {
8383effdb90SChristoph Hellwig pr_err("DIF TYPE2 protection currently not supported\n");
8393effdb90SChristoph Hellwig return -ENOSYS;
8403effdb90SChristoph Hellwig }
8413effdb90SChristoph Hellwig if (da->hw_pi_prot_type) {
8423effdb90SChristoph Hellwig pr_warn("DIF protection enabled on underlying hardware,"
8433effdb90SChristoph Hellwig " ignoring\n");
8443effdb90SChristoph Hellwig return count;
8453effdb90SChristoph Hellwig }
8463effdb90SChristoph Hellwig if (!dev->transport->init_prot || !dev->transport->free_prot) {
8473effdb90SChristoph Hellwig /* 0 is only allowed value for non-supporting backends */
8483effdb90SChristoph Hellwig if (flag == 0)
849bc1a7d6aSAndy Grover return count;
8503effdb90SChristoph Hellwig
8513effdb90SChristoph Hellwig pr_err("DIF protection not supported by backend: %s\n",
8523effdb90SChristoph Hellwig dev->transport->name);
8533effdb90SChristoph Hellwig return -ENOSYS;
8543effdb90SChristoph Hellwig }
855cb0f32e1SMike Christie if (!target_dev_configured(dev)) {
8563effdb90SChristoph Hellwig pr_err("DIF protection requires device to be configured\n");
8573effdb90SChristoph Hellwig return -ENODEV;
8583effdb90SChristoph Hellwig }
8593effdb90SChristoph Hellwig if (dev->export_count) {
8603effdb90SChristoph Hellwig pr_err("dev[%p]: Unable to change SE Device PROT type while"
8613effdb90SChristoph Hellwig " export_count is %d\n", dev, dev->export_count);
8623effdb90SChristoph Hellwig return -EINVAL;
8633effdb90SChristoph Hellwig }
8643effdb90SChristoph Hellwig
8653effdb90SChristoph Hellwig da->pi_prot_type = flag;
8663effdb90SChristoph Hellwig
8673effdb90SChristoph Hellwig if (flag && !old_prot) {
8683effdb90SChristoph Hellwig ret = dev->transport->init_prot(dev);
8693effdb90SChristoph Hellwig if (ret) {
8703effdb90SChristoph Hellwig da->pi_prot_type = old_prot;
871056e8924SDmitry Monakhov da->pi_prot_verify = (bool) da->pi_prot_type;
8723effdb90SChristoph Hellwig return ret;
8733effdb90SChristoph Hellwig }
8743effdb90SChristoph Hellwig
8753effdb90SChristoph Hellwig } else if (!flag && old_prot) {
8763effdb90SChristoph Hellwig dev->transport->free_prot(dev);
8773effdb90SChristoph Hellwig }
8783effdb90SChristoph Hellwig
879056e8924SDmitry Monakhov da->pi_prot_verify = (bool) da->pi_prot_type;
8803effdb90SChristoph Hellwig pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
8813effdb90SChristoph Hellwig return count;
8823effdb90SChristoph Hellwig }
8833effdb90SChristoph Hellwig
884b6cd7f34SDavid Disseldorp /* always zero, but attr needs to remain RW to avoid userspace breakage */
pi_prot_format_show(struct config_item * item,char * page)885b6cd7f34SDavid Disseldorp static ssize_t pi_prot_format_show(struct config_item *item, char *page)
886b6cd7f34SDavid Disseldorp {
887b6cd7f34SDavid Disseldorp return snprintf(page, PAGE_SIZE, "0\n");
888b6cd7f34SDavid Disseldorp }
889b6cd7f34SDavid Disseldorp
pi_prot_format_store(struct config_item * item,const char * page,size_t count)8902eafd729SChristoph Hellwig static ssize_t pi_prot_format_store(struct config_item *item,
8913effdb90SChristoph Hellwig const char *page, size_t count)
8923effdb90SChristoph Hellwig {
8932eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item);
8943effdb90SChristoph Hellwig struct se_device *dev = da->da_dev;
8953effdb90SChristoph Hellwig bool flag;
8963effdb90SChristoph Hellwig int ret;
8973effdb90SChristoph Hellwig
898e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag);
8993effdb90SChristoph Hellwig if (ret < 0)
9003effdb90SChristoph Hellwig return ret;
9013effdb90SChristoph Hellwig
9023effdb90SChristoph Hellwig if (!flag)
9033effdb90SChristoph Hellwig return count;
9043effdb90SChristoph Hellwig
9053effdb90SChristoph Hellwig if (!dev->transport->format_prot) {
9063effdb90SChristoph Hellwig pr_err("DIF protection format not supported by backend %s\n",
9073effdb90SChristoph Hellwig dev->transport->name);
9083effdb90SChristoph Hellwig return -ENOSYS;
9093effdb90SChristoph Hellwig }
910cb0f32e1SMike Christie if (!target_dev_configured(dev)) {
9113effdb90SChristoph Hellwig pr_err("DIF protection format requires device to be configured\n");
9123effdb90SChristoph Hellwig return -ENODEV;
9133effdb90SChristoph Hellwig }
9143effdb90SChristoph Hellwig if (dev->export_count) {
9153effdb90SChristoph Hellwig pr_err("dev[%p]: Unable to format SE Device PROT type while"
9163effdb90SChristoph Hellwig " export_count is %d\n", dev, dev->export_count);
9173effdb90SChristoph Hellwig return -EINVAL;
9183effdb90SChristoph Hellwig }
9193effdb90SChristoph Hellwig
9203effdb90SChristoph Hellwig ret = dev->transport->format_prot(dev);
9213effdb90SChristoph Hellwig if (ret)
9223effdb90SChristoph Hellwig return ret;
9233effdb90SChristoph Hellwig
9243effdb90SChristoph Hellwig pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
9253effdb90SChristoph Hellwig return count;
9263effdb90SChristoph Hellwig }
9273effdb90SChristoph Hellwig
pi_prot_verify_store(struct config_item * item,const char * page,size_t count)928056e8924SDmitry Monakhov static ssize_t pi_prot_verify_store(struct config_item *item,
929056e8924SDmitry Monakhov const char *page, size_t count)
930056e8924SDmitry Monakhov {
931056e8924SDmitry Monakhov struct se_dev_attrib *da = to_attrib(item);
932056e8924SDmitry Monakhov bool flag;
933056e8924SDmitry Monakhov int ret;
934056e8924SDmitry Monakhov
935e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag);
936056e8924SDmitry Monakhov if (ret < 0)
937056e8924SDmitry Monakhov return ret;
938056e8924SDmitry Monakhov
939056e8924SDmitry Monakhov if (!flag) {
940056e8924SDmitry Monakhov da->pi_prot_verify = flag;
941056e8924SDmitry Monakhov return count;
942056e8924SDmitry Monakhov }
943056e8924SDmitry Monakhov if (da->hw_pi_prot_type) {
944056e8924SDmitry Monakhov pr_warn("DIF protection enabled on underlying hardware,"
945056e8924SDmitry Monakhov " ignoring\n");
946056e8924SDmitry Monakhov return count;
947056e8924SDmitry Monakhov }
948056e8924SDmitry Monakhov if (!da->pi_prot_type) {
949056e8924SDmitry Monakhov pr_warn("DIF protection not supported by backend, ignoring\n");
950056e8924SDmitry Monakhov return count;
951056e8924SDmitry Monakhov }
952056e8924SDmitry Monakhov da->pi_prot_verify = flag;
953056e8924SDmitry Monakhov
954056e8924SDmitry Monakhov return count;
955056e8924SDmitry Monakhov }
956056e8924SDmitry Monakhov
force_pr_aptpl_store(struct config_item * item,const char * page,size_t count)9572eafd729SChristoph Hellwig static ssize_t force_pr_aptpl_store(struct config_item *item,
9583effdb90SChristoph Hellwig const char *page, size_t count)
9593effdb90SChristoph Hellwig {
9602eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item);
9613effdb90SChristoph Hellwig bool flag;
9623effdb90SChristoph Hellwig int ret;
9633effdb90SChristoph Hellwig
964e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag);
9653effdb90SChristoph Hellwig if (ret < 0)
9663effdb90SChristoph Hellwig return ret;
9673effdb90SChristoph Hellwig if (da->da_dev->export_count) {
9683effdb90SChristoph Hellwig pr_err("dev[%p]: Unable to set force_pr_aptpl while"
9693effdb90SChristoph Hellwig " export_count is %d\n",
9703effdb90SChristoph Hellwig da->da_dev, da->da_dev->export_count);
9713effdb90SChristoph Hellwig return -EINVAL;
9723effdb90SChristoph Hellwig }
9733effdb90SChristoph Hellwig
9743effdb90SChristoph Hellwig da->force_pr_aptpl = flag;
9753effdb90SChristoph Hellwig pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
9763effdb90SChristoph Hellwig return count;
9773effdb90SChristoph Hellwig }
9783effdb90SChristoph Hellwig
emulate_rest_reord_store(struct config_item * item,const char * page,size_t count)9792eafd729SChristoph Hellwig static ssize_t emulate_rest_reord_store(struct config_item *item,
9803effdb90SChristoph Hellwig const char *page, size_t count)
9813effdb90SChristoph Hellwig {
9822eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item);
9833effdb90SChristoph Hellwig bool flag;
9843effdb90SChristoph Hellwig int ret;
9853effdb90SChristoph Hellwig
986e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag);
9873effdb90SChristoph Hellwig if (ret < 0)
9883effdb90SChristoph Hellwig return ret;
9893effdb90SChristoph Hellwig
9903effdb90SChristoph Hellwig if (flag != 0) {
9913effdb90SChristoph Hellwig printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
9923effdb90SChristoph Hellwig " reordering not implemented\n", da->da_dev);
9933effdb90SChristoph Hellwig return -ENOSYS;
9943effdb90SChristoph Hellwig }
9953effdb90SChristoph Hellwig da->emulate_rest_reord = flag;
9963effdb90SChristoph Hellwig pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
9973effdb90SChristoph Hellwig da->da_dev, flag);
9983effdb90SChristoph Hellwig return count;
9993effdb90SChristoph Hellwig }
10003effdb90SChristoph Hellwig
unmap_zeroes_data_store(struct config_item * item,const char * page,size_t count)1001e6f41633SJamie Pocas static ssize_t unmap_zeroes_data_store(struct config_item *item,
1002e6f41633SJamie Pocas const char *page, size_t count)
1003e6f41633SJamie Pocas {
1004e6f41633SJamie Pocas struct se_dev_attrib *da = to_attrib(item);
100534bd1dcaSMike Christie struct se_device *dev = da->da_dev;
1006e6f41633SJamie Pocas bool flag;
1007e6f41633SJamie Pocas int ret;
1008e6f41633SJamie Pocas
1009e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag);
1010e6f41633SJamie Pocas if (ret < 0)
1011e6f41633SJamie Pocas return ret;
1012e6f41633SJamie Pocas
1013e6f41633SJamie Pocas if (da->da_dev->export_count) {
1014e6f41633SJamie Pocas pr_err("dev[%p]: Unable to change SE Device"
1015e6f41633SJamie Pocas " unmap_zeroes_data while export_count is %d\n",
1016e6f41633SJamie Pocas da->da_dev, da->da_dev->export_count);
1017e6f41633SJamie Pocas return -EINVAL;
1018e6f41633SJamie Pocas }
1019e6f41633SJamie Pocas /*
1020e6f41633SJamie Pocas * We expect this value to be non-zero when generic Block Layer
1021e6f41633SJamie Pocas * Discard supported is detected iblock_configure_device().
1022e6f41633SJamie Pocas */
1023e6f41633SJamie Pocas if (flag && !da->max_unmap_block_desc_count) {
102434bd1dcaSMike Christie if (!dev->transport->configure_unmap ||
102534bd1dcaSMike Christie !dev->transport->configure_unmap(dev)) {
102634bd1dcaSMike Christie pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set because max_unmap_block_desc_count is zero\n",
1027e6f41633SJamie Pocas da->da_dev);
1028e6f41633SJamie Pocas return -ENOSYS;
1029e6f41633SJamie Pocas }
103034bd1dcaSMike Christie }
1031e6f41633SJamie Pocas da->unmap_zeroes_data = flag;
1032e6f41633SJamie Pocas pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
1033e6f41633SJamie Pocas da->da_dev, flag);
10342e498f25SNicholas Bellinger return count;
1035e6f41633SJamie Pocas }
1036e6f41633SJamie Pocas
10373effdb90SChristoph Hellwig /*
10383effdb90SChristoph Hellwig * Note, this can only be called on unexported SE Device Object.
10393effdb90SChristoph Hellwig */
queue_depth_store(struct config_item * item,const char * page,size_t count)10402eafd729SChristoph Hellwig static ssize_t queue_depth_store(struct config_item *item,
10413effdb90SChristoph Hellwig const char *page, size_t count)
10423effdb90SChristoph Hellwig {
10432eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item);
10443effdb90SChristoph Hellwig struct se_device *dev = da->da_dev;
10453effdb90SChristoph Hellwig u32 val;
10463effdb90SChristoph Hellwig int ret;
10473effdb90SChristoph Hellwig
10483effdb90SChristoph Hellwig ret = kstrtou32(page, 0, &val);
10493effdb90SChristoph Hellwig if (ret < 0)
10503effdb90SChristoph Hellwig return ret;
10513effdb90SChristoph Hellwig
10523effdb90SChristoph Hellwig if (dev->export_count) {
10533effdb90SChristoph Hellwig pr_err("dev[%p]: Unable to change SE Device TCQ while"
10543effdb90SChristoph Hellwig " export_count is %d\n",
10553effdb90SChristoph Hellwig dev, dev->export_count);
10563effdb90SChristoph Hellwig return -EINVAL;
10573effdb90SChristoph Hellwig }
10583effdb90SChristoph Hellwig if (!val) {
10593effdb90SChristoph Hellwig pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
10603effdb90SChristoph Hellwig return -EINVAL;
10613effdb90SChristoph Hellwig }
10623effdb90SChristoph Hellwig
10633effdb90SChristoph Hellwig if (val > dev->dev_attrib.queue_depth) {
10643effdb90SChristoph Hellwig if (val > dev->dev_attrib.hw_queue_depth) {
10653effdb90SChristoph Hellwig pr_err("dev[%p]: Passed queue_depth:"
10663effdb90SChristoph Hellwig " %u exceeds TCM/SE_Device MAX"
10673effdb90SChristoph Hellwig " TCQ: %u\n", dev, val,
10683effdb90SChristoph Hellwig dev->dev_attrib.hw_queue_depth);
10693effdb90SChristoph Hellwig return -EINVAL;
10703effdb90SChristoph Hellwig }
10713effdb90SChristoph Hellwig }
10723effdb90SChristoph Hellwig da->queue_depth = dev->queue_depth = val;
10733effdb90SChristoph Hellwig pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
10743effdb90SChristoph Hellwig return count;
10753effdb90SChristoph Hellwig }
10763effdb90SChristoph Hellwig
optimal_sectors_store(struct config_item * item,const char * page,size_t count)10772eafd729SChristoph Hellwig static ssize_t optimal_sectors_store(struct config_item *item,
10783effdb90SChristoph Hellwig const char *page, size_t count)
10793effdb90SChristoph Hellwig {
10802eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item);
10813effdb90SChristoph Hellwig u32 val;
10823effdb90SChristoph Hellwig int ret;
10833effdb90SChristoph Hellwig
10843effdb90SChristoph Hellwig ret = kstrtou32(page, 0, &val);
10853effdb90SChristoph Hellwig if (ret < 0)
10863effdb90SChristoph Hellwig return ret;
10873effdb90SChristoph Hellwig
10883effdb90SChristoph Hellwig if (da->da_dev->export_count) {
10893effdb90SChristoph Hellwig pr_err("dev[%p]: Unable to change SE Device"
10903effdb90SChristoph Hellwig " optimal_sectors while export_count is %d\n",
10913effdb90SChristoph Hellwig da->da_dev, da->da_dev->export_count);
10923effdb90SChristoph Hellwig return -EINVAL;
10933effdb90SChristoph Hellwig }
10943effdb90SChristoph Hellwig if (val > da->hw_max_sectors) {
10953effdb90SChristoph Hellwig pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
10963effdb90SChristoph Hellwig " greater than hw_max_sectors: %u\n",
10973effdb90SChristoph Hellwig da->da_dev, val, da->hw_max_sectors);
10983effdb90SChristoph Hellwig return -EINVAL;
10993effdb90SChristoph Hellwig }
11003effdb90SChristoph Hellwig
11013effdb90SChristoph Hellwig da->optimal_sectors = val;
11023effdb90SChristoph Hellwig pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
11033effdb90SChristoph Hellwig da->da_dev, val);
11043effdb90SChristoph Hellwig return count;
11053effdb90SChristoph Hellwig }
11063effdb90SChristoph Hellwig
block_size_store(struct config_item * item,const char * page,size_t count)11072eafd729SChristoph Hellwig static ssize_t block_size_store(struct config_item *item,
11083effdb90SChristoph Hellwig const char *page, size_t count)
11093effdb90SChristoph Hellwig {
11102eafd729SChristoph Hellwig struct se_dev_attrib *da = to_attrib(item);
11113effdb90SChristoph Hellwig u32 val;
11123effdb90SChristoph Hellwig int ret;
11133effdb90SChristoph Hellwig
11143effdb90SChristoph Hellwig ret = kstrtou32(page, 0, &val);
11153effdb90SChristoph Hellwig if (ret < 0)
11163effdb90SChristoph Hellwig return ret;
11173effdb90SChristoph Hellwig
11183effdb90SChristoph Hellwig if (da->da_dev->export_count) {
11193effdb90SChristoph Hellwig pr_err("dev[%p]: Unable to change SE Device block_size"
11203effdb90SChristoph Hellwig " while export_count is %d\n",
11213effdb90SChristoph Hellwig da->da_dev, da->da_dev->export_count);
11223effdb90SChristoph Hellwig return -EINVAL;
11233effdb90SChristoph Hellwig }
11243effdb90SChristoph Hellwig
11253effdb90SChristoph Hellwig if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
11263effdb90SChristoph Hellwig pr_err("dev[%p]: Illegal value for block_device: %u"
11273effdb90SChristoph Hellwig " for SE device, must be 512, 1024, 2048 or 4096\n",
11283effdb90SChristoph Hellwig da->da_dev, val);
11293effdb90SChristoph Hellwig return -EINVAL;
11303effdb90SChristoph Hellwig }
11313effdb90SChristoph Hellwig
11323effdb90SChristoph Hellwig da->block_size = val;
11333effdb90SChristoph Hellwig
11343effdb90SChristoph Hellwig pr_debug("dev[%p]: SE Device block_size changed to %u\n",
11353effdb90SChristoph Hellwig da->da_dev, val);
11363effdb90SChristoph Hellwig return count;
11373effdb90SChristoph Hellwig }
11385873c4d1SChristoph Hellwig
alua_support_show(struct config_item * item,char * page)1139c17d5d5fSMike Christie static ssize_t alua_support_show(struct config_item *item, char *page)
1140c17d5d5fSMike Christie {
1141c17d5d5fSMike Christie struct se_dev_attrib *da = to_attrib(item);
114269088a04SBodo Stroesser u8 flags = da->da_dev->transport_flags;
1143c17d5d5fSMike Christie
1144c17d5d5fSMike Christie return snprintf(page, PAGE_SIZE, "%d\n",
1145c17d5d5fSMike Christie flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ? 0 : 1);
1146c17d5d5fSMike Christie }
1147c17d5d5fSMike Christie
alua_support_store(struct config_item * item,const char * page,size_t count)1148356ba2a8SBodo Stroesser static ssize_t alua_support_store(struct config_item *item,
1149356ba2a8SBodo Stroesser const char *page, size_t count)
1150356ba2a8SBodo Stroesser {
1151356ba2a8SBodo Stroesser struct se_dev_attrib *da = to_attrib(item);
1152356ba2a8SBodo Stroesser struct se_device *dev = da->da_dev;
1153ef7ae7f7SMaurizio Lombardi bool flag, oldflag;
1154356ba2a8SBodo Stroesser int ret;
1155356ba2a8SBodo Stroesser
1156e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag);
1157ef7ae7f7SMaurizio Lombardi if (ret < 0)
1158ef7ae7f7SMaurizio Lombardi return ret;
1159ef7ae7f7SMaurizio Lombardi
1160ef7ae7f7SMaurizio Lombardi oldflag = !(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA);
1161ef7ae7f7SMaurizio Lombardi if (flag == oldflag)
1162ef7ae7f7SMaurizio Lombardi return count;
1163ef7ae7f7SMaurizio Lombardi
1164356ba2a8SBodo Stroesser if (!(dev->transport->transport_flags_changeable &
1165356ba2a8SBodo Stroesser TRANSPORT_FLAG_PASSTHROUGH_ALUA)) {
1166356ba2a8SBodo Stroesser pr_err("dev[%p]: Unable to change SE Device alua_support:"
1167356ba2a8SBodo Stroesser " alua_support has fixed value\n", dev);
1168ef7ae7f7SMaurizio Lombardi return -ENOSYS;
1169356ba2a8SBodo Stroesser }
1170356ba2a8SBodo Stroesser
1171356ba2a8SBodo Stroesser if (flag)
1172356ba2a8SBodo Stroesser dev->transport_flags &= ~TRANSPORT_FLAG_PASSTHROUGH_ALUA;
1173356ba2a8SBodo Stroesser else
1174356ba2a8SBodo Stroesser dev->transport_flags |= TRANSPORT_FLAG_PASSTHROUGH_ALUA;
1175356ba2a8SBodo Stroesser return count;
1176356ba2a8SBodo Stroesser }
1177356ba2a8SBodo Stroesser
pgr_support_show(struct config_item * item,char * page)1178c17d5d5fSMike Christie static ssize_t pgr_support_show(struct config_item *item, char *page)
1179c17d5d5fSMike Christie {
1180c17d5d5fSMike Christie struct se_dev_attrib *da = to_attrib(item);
118169088a04SBodo Stroesser u8 flags = da->da_dev->transport_flags;
1182c17d5d5fSMike Christie
1183c17d5d5fSMike Christie return snprintf(page, PAGE_SIZE, "%d\n",
1184c17d5d5fSMike Christie flags & TRANSPORT_FLAG_PASSTHROUGH_PGR ? 0 : 1);
1185c17d5d5fSMike Christie }
1186c17d5d5fSMike Christie
pgr_support_store(struct config_item * item,const char * page,size_t count)1187356ba2a8SBodo Stroesser static ssize_t pgr_support_store(struct config_item *item,
1188356ba2a8SBodo Stroesser const char *page, size_t count)
1189356ba2a8SBodo Stroesser {
1190356ba2a8SBodo Stroesser struct se_dev_attrib *da = to_attrib(item);
1191356ba2a8SBodo Stroesser struct se_device *dev = da->da_dev;
1192ef7ae7f7SMaurizio Lombardi bool flag, oldflag;
1193356ba2a8SBodo Stroesser int ret;
1194356ba2a8SBodo Stroesser
1195e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag);
1196ef7ae7f7SMaurizio Lombardi if (ret < 0)
1197ef7ae7f7SMaurizio Lombardi return ret;
1198ef7ae7f7SMaurizio Lombardi
1199ef7ae7f7SMaurizio Lombardi oldflag = !(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR);
1200ef7ae7f7SMaurizio Lombardi if (flag == oldflag)
1201ef7ae7f7SMaurizio Lombardi return count;
1202ef7ae7f7SMaurizio Lombardi
1203356ba2a8SBodo Stroesser if (!(dev->transport->transport_flags_changeable &
1204356ba2a8SBodo Stroesser TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
1205356ba2a8SBodo Stroesser pr_err("dev[%p]: Unable to change SE Device pgr_support:"
1206356ba2a8SBodo Stroesser " pgr_support has fixed value\n", dev);
1207ef7ae7f7SMaurizio Lombardi return -ENOSYS;
1208356ba2a8SBodo Stroesser }
1209356ba2a8SBodo Stroesser
1210356ba2a8SBodo Stroesser if (flag)
1211356ba2a8SBodo Stroesser dev->transport_flags &= ~TRANSPORT_FLAG_PASSTHROUGH_PGR;
1212356ba2a8SBodo Stroesser else
1213356ba2a8SBodo Stroesser dev->transport_flags |= TRANSPORT_FLAG_PASSTHROUGH_PGR;
1214356ba2a8SBodo Stroesser return count;
1215356ba2a8SBodo Stroesser }
1216356ba2a8SBodo Stroesser
emulate_rsoc_store(struct config_item * item,const char * page,size_t count)1217bd217b8cSDmitry Bogdanov static ssize_t emulate_rsoc_store(struct config_item *item,
1218bd217b8cSDmitry Bogdanov const char *page, size_t count)
1219bd217b8cSDmitry Bogdanov {
1220bd217b8cSDmitry Bogdanov struct se_dev_attrib *da = to_attrib(item);
1221bd217b8cSDmitry Bogdanov bool flag;
1222bd217b8cSDmitry Bogdanov int ret;
1223bd217b8cSDmitry Bogdanov
1224e56ca6bcSChristophe JAILLET ret = kstrtobool(page, &flag);
1225bd217b8cSDmitry Bogdanov if (ret < 0)
1226bd217b8cSDmitry Bogdanov return ret;
1227bd217b8cSDmitry Bogdanov
1228bd217b8cSDmitry Bogdanov da->emulate_rsoc = flag;
1229bd217b8cSDmitry Bogdanov pr_debug("dev[%p]: SE Device REPORT_SUPPORTED_OPERATION_CODES_EMULATION flag: %d\n",
1230bd217b8cSDmitry Bogdanov da->da_dev, flag);
1231bd217b8cSDmitry Bogdanov return count;
1232bd217b8cSDmitry Bogdanov }
1233bd217b8cSDmitry Bogdanov
12342eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_model_alias);
12352eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_dpo);
12362eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_write);
12372eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_fua_read);
12382eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_write_cache);
12392eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
12402eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tas);
12412eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpu);
12422eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_tpws);
12432eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_caw);
12442eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_3pc);
1245b49d6f78SDavid Disseldorp CONFIGFS_ATTR(, emulate_pr);
1246bd217b8cSDmitry Bogdanov CONFIGFS_ATTR(, emulate_rsoc);
12472eafd729SChristoph Hellwig CONFIGFS_ATTR(, pi_prot_type);
12482eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_pi_prot_type);
1249b6cd7f34SDavid Disseldorp CONFIGFS_ATTR(, pi_prot_format);
1250056e8924SDmitry Monakhov CONFIGFS_ATTR(, pi_prot_verify);
12512eafd729SChristoph Hellwig CONFIGFS_ATTR(, enforce_pr_isids);
12522eafd729SChristoph Hellwig CONFIGFS_ATTR(, is_nonrot);
12532eafd729SChristoph Hellwig CONFIGFS_ATTR(, emulate_rest_reord);
12542eafd729SChristoph Hellwig CONFIGFS_ATTR(, force_pr_aptpl);
12552eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_block_size);
12562eafd729SChristoph Hellwig CONFIGFS_ATTR(, block_size);
12572eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_max_sectors);
12582eafd729SChristoph Hellwig CONFIGFS_ATTR(, optimal_sectors);
12592eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(, hw_queue_depth);
12602eafd729SChristoph Hellwig CONFIGFS_ATTR(, queue_depth);
12612eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_lba_count);
12622eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_unmap_block_desc_count);
12632eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity);
12642eafd729SChristoph Hellwig CONFIGFS_ATTR(, unmap_granularity_alignment);
1265e6f41633SJamie Pocas CONFIGFS_ATTR(, unmap_zeroes_data);
12662eafd729SChristoph Hellwig CONFIGFS_ATTR(, max_write_same_len);
1267356ba2a8SBodo Stroesser CONFIGFS_ATTR(, alua_support);
1268356ba2a8SBodo Stroesser CONFIGFS_ATTR(, pgr_support);
1269c66ac9dbSNicholas Bellinger
12705873c4d1SChristoph Hellwig /*
12715873c4d1SChristoph Hellwig * dev_attrib attributes for devices using the target core SBC/SPC
12725873c4d1SChristoph Hellwig * interpreter. Any backend using spc_parse_cdb should be using
12735873c4d1SChristoph Hellwig * these.
12745873c4d1SChristoph Hellwig */
12755873c4d1SChristoph Hellwig struct configfs_attribute *sbc_attrib_attrs[] = {
12762eafd729SChristoph Hellwig &attr_emulate_model_alias,
12772eafd729SChristoph Hellwig &attr_emulate_dpo,
12782eafd729SChristoph Hellwig &attr_emulate_fua_write,
12792eafd729SChristoph Hellwig &attr_emulate_fua_read,
12802eafd729SChristoph Hellwig &attr_emulate_write_cache,
12812eafd729SChristoph Hellwig &attr_emulate_ua_intlck_ctrl,
12822eafd729SChristoph Hellwig &attr_emulate_tas,
12832eafd729SChristoph Hellwig &attr_emulate_tpu,
12842eafd729SChristoph Hellwig &attr_emulate_tpws,
12852eafd729SChristoph Hellwig &attr_emulate_caw,
12862eafd729SChristoph Hellwig &attr_emulate_3pc,
1287b49d6f78SDavid Disseldorp &attr_emulate_pr,
12882eafd729SChristoph Hellwig &attr_pi_prot_type,
12892eafd729SChristoph Hellwig &attr_hw_pi_prot_type,
12902eafd729SChristoph Hellwig &attr_pi_prot_format,
1291056e8924SDmitry Monakhov &attr_pi_prot_verify,
12922eafd729SChristoph Hellwig &attr_enforce_pr_isids,
12932eafd729SChristoph Hellwig &attr_is_nonrot,
12942eafd729SChristoph Hellwig &attr_emulate_rest_reord,
12952eafd729SChristoph Hellwig &attr_force_pr_aptpl,
12962eafd729SChristoph Hellwig &attr_hw_block_size,
12972eafd729SChristoph Hellwig &attr_block_size,
12982eafd729SChristoph Hellwig &attr_hw_max_sectors,
12992eafd729SChristoph Hellwig &attr_optimal_sectors,
13002eafd729SChristoph Hellwig &attr_hw_queue_depth,
13012eafd729SChristoph Hellwig &attr_queue_depth,
13022eafd729SChristoph Hellwig &attr_max_unmap_lba_count,
13032eafd729SChristoph Hellwig &attr_max_unmap_block_desc_count,
13042eafd729SChristoph Hellwig &attr_unmap_granularity,
13052eafd729SChristoph Hellwig &attr_unmap_granularity_alignment,
1306e6f41633SJamie Pocas &attr_unmap_zeroes_data,
13072eafd729SChristoph Hellwig &attr_max_write_same_len,
1308c17d5d5fSMike Christie &attr_alua_support,
1309c17d5d5fSMike Christie &attr_pgr_support,
1310bd217b8cSDmitry Bogdanov &attr_emulate_rsoc,
13115873c4d1SChristoph Hellwig NULL,
13125873c4d1SChristoph Hellwig };
13135873c4d1SChristoph Hellwig EXPORT_SYMBOL(sbc_attrib_attrs);
13145873c4d1SChristoph Hellwig
13155873c4d1SChristoph Hellwig /*
13165873c4d1SChristoph Hellwig * Minimal dev_attrib attributes for devices passing through CDBs.
13175873c4d1SChristoph Hellwig * In this case we only provide a few read-only attributes for
13185873c4d1SChristoph Hellwig * backwards compatibility.
13195873c4d1SChristoph Hellwig */
13205873c4d1SChristoph Hellwig struct configfs_attribute *passthrough_attrib_attrs[] = {
13212eafd729SChristoph Hellwig &attr_hw_pi_prot_type,
13222eafd729SChristoph Hellwig &attr_hw_block_size,
13232eafd729SChristoph Hellwig &attr_hw_max_sectors,
13242eafd729SChristoph Hellwig &attr_hw_queue_depth,
132592999417SBodo Stroesser &attr_emulate_pr,
1326c17d5d5fSMike Christie &attr_alua_support,
1327c17d5d5fSMike Christie &attr_pgr_support,
13285873c4d1SChristoph Hellwig NULL,
13295873c4d1SChristoph Hellwig };
13305873c4d1SChristoph Hellwig EXPORT_SYMBOL(passthrough_attrib_attrs);
13315873c4d1SChristoph Hellwig
13324703b625SBodo Stroesser /*
13334703b625SBodo Stroesser * pr related dev_attrib attributes for devices passing through CDBs,
13344703b625SBodo Stroesser * but allowing in core pr emulation.
13354703b625SBodo Stroesser */
13364703b625SBodo Stroesser struct configfs_attribute *passthrough_pr_attrib_attrs[] = {
13374703b625SBodo Stroesser &attr_enforce_pr_isids,
13384703b625SBodo Stroesser &attr_force_pr_aptpl,
13394703b625SBodo Stroesser NULL,
13404703b625SBodo Stroesser };
13414703b625SBodo Stroesser EXPORT_SYMBOL(passthrough_pr_attrib_attrs);
13424703b625SBodo Stroesser
13432eafd729SChristoph Hellwig TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
13448dc31ff9SMike Christie TB_CIT_SETUP_DRV(dev_action, NULL, NULL);
1345c66ac9dbSNicholas Bellinger
1346f79a897eSNicholas Bellinger /* End functions for struct config_item_type tb_dev_attrib_cit */
1347c66ac9dbSNicholas Bellinger
1348f8d389c6SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_wwn_cit */
1349c66ac9dbSNicholas Bellinger
to_t10_wwn(struct config_item * item)13502eafd729SChristoph Hellwig static struct t10_wwn *to_t10_wwn(struct config_item *item)
13512eafd729SChristoph Hellwig {
13522eafd729SChristoph Hellwig return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
13532eafd729SChristoph Hellwig }
1354c66ac9dbSNicholas Bellinger
target_check_inquiry_data(char * buf)13550322913cSAlan Adamson static ssize_t target_check_inquiry_data(char *buf)
13560322913cSAlan Adamson {
13570322913cSAlan Adamson size_t len;
13580322913cSAlan Adamson int i;
13590322913cSAlan Adamson
13600322913cSAlan Adamson len = strlen(buf);
13610322913cSAlan Adamson
13620322913cSAlan Adamson /*
13630322913cSAlan Adamson * SPC 4.3.1:
13640322913cSAlan Adamson * ASCII data fields shall contain only ASCII printable characters
13650322913cSAlan Adamson * (i.e., code values 20h to 7Eh) and may be terminated with one or
13660322913cSAlan Adamson * more ASCII null (00h) characters.
13670322913cSAlan Adamson */
13680322913cSAlan Adamson for (i = 0; i < len; i++) {
13690322913cSAlan Adamson if (buf[i] < 0x20 || buf[i] > 0x7E) {
13700322913cSAlan Adamson pr_err("Emulated T10 Inquiry Data contains non-ASCII-printable characters\n");
13710322913cSAlan Adamson return -EINVAL;
13720322913cSAlan Adamson }
13730322913cSAlan Adamson }
13740322913cSAlan Adamson
13750322913cSAlan Adamson return len;
13760322913cSAlan Adamson }
13770322913cSAlan Adamson
1378c66ac9dbSNicholas Bellinger /*
137954a6f3f6SDavid Disseldorp * STANDARD and VPD page 0x83 T10 Vendor Identification
138054a6f3f6SDavid Disseldorp */
target_wwn_vendor_id_show(struct config_item * item,char * page)138154a6f3f6SDavid Disseldorp static ssize_t target_wwn_vendor_id_show(struct config_item *item,
138254a6f3f6SDavid Disseldorp char *page)
138354a6f3f6SDavid Disseldorp {
138454a6f3f6SDavid Disseldorp return sprintf(page, "%s\n", &to_t10_wwn(item)->vendor[0]);
138554a6f3f6SDavid Disseldorp }
138654a6f3f6SDavid Disseldorp
target_wwn_vendor_id_store(struct config_item * item,const char * page,size_t count)138754a6f3f6SDavid Disseldorp static ssize_t target_wwn_vendor_id_store(struct config_item *item,
138854a6f3f6SDavid Disseldorp const char *page, size_t count)
138954a6f3f6SDavid Disseldorp {
139054a6f3f6SDavid Disseldorp struct t10_wwn *t10_wwn = to_t10_wwn(item);
139154a6f3f6SDavid Disseldorp struct se_device *dev = t10_wwn->t10_dev;
139254a6f3f6SDavid Disseldorp /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
139354a6f3f6SDavid Disseldorp unsigned char buf[INQUIRY_VENDOR_LEN + 2];
139454a6f3f6SDavid Disseldorp char *stripped = NULL;
13955c584fe6SAzeem Shaikh ssize_t len;
1396ee26724aSColin Ian King ssize_t ret;
139754a6f3f6SDavid Disseldorp
13985c584fe6SAzeem Shaikh len = strscpy(buf, page, sizeof(buf));
13995c584fe6SAzeem Shaikh if (len > 0) {
140054a6f3f6SDavid Disseldorp /* Strip any newline added from userspace. */
140154a6f3f6SDavid Disseldorp stripped = strstrip(buf);
140254a6f3f6SDavid Disseldorp len = strlen(stripped);
140354a6f3f6SDavid Disseldorp }
14045c584fe6SAzeem Shaikh if (len < 0 || len > INQUIRY_VENDOR_LEN) {
140554a6f3f6SDavid Disseldorp pr_err("Emulated T10 Vendor Identification exceeds"
140654a6f3f6SDavid Disseldorp " INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)
140754a6f3f6SDavid Disseldorp "\n");
140854a6f3f6SDavid Disseldorp return -EOVERFLOW;
140954a6f3f6SDavid Disseldorp }
141054a6f3f6SDavid Disseldorp
14110322913cSAlan Adamson ret = target_check_inquiry_data(stripped);
14120322913cSAlan Adamson
14130322913cSAlan Adamson if (ret < 0)
14140322913cSAlan Adamson return ret;
141554a6f3f6SDavid Disseldorp
141654a6f3f6SDavid Disseldorp /*
141754a6f3f6SDavid Disseldorp * Check to see if any active exports exist. If they do exist, fail
141854a6f3f6SDavid Disseldorp * here as changing this information on the fly (underneath the
141954a6f3f6SDavid Disseldorp * initiator side OS dependent multipath code) could cause negative
142054a6f3f6SDavid Disseldorp * effects.
142154a6f3f6SDavid Disseldorp */
142254a6f3f6SDavid Disseldorp if (dev->export_count) {
142354a6f3f6SDavid Disseldorp pr_err("Unable to set T10 Vendor Identification while"
142454a6f3f6SDavid Disseldorp " active %d exports exist\n", dev->export_count);
142554a6f3f6SDavid Disseldorp return -EINVAL;
142654a6f3f6SDavid Disseldorp }
142754a6f3f6SDavid Disseldorp
142854a6f3f6SDavid Disseldorp BUILD_BUG_ON(sizeof(dev->t10_wwn.vendor) != INQUIRY_VENDOR_LEN + 1);
14290871237aSAzeem Shaikh strscpy(dev->t10_wwn.vendor, stripped, sizeof(dev->t10_wwn.vendor));
143054a6f3f6SDavid Disseldorp
143154a6f3f6SDavid Disseldorp pr_debug("Target_Core_ConfigFS: Set emulated T10 Vendor Identification:"
143254a6f3f6SDavid Disseldorp " %s\n", dev->t10_wwn.vendor);
143354a6f3f6SDavid Disseldorp
143454a6f3f6SDavid Disseldorp return count;
143554a6f3f6SDavid Disseldorp }
143654a6f3f6SDavid Disseldorp
target_wwn_product_id_show(struct config_item * item,char * page)14370322913cSAlan Adamson static ssize_t target_wwn_product_id_show(struct config_item *item,
14380322913cSAlan Adamson char *page)
14390322913cSAlan Adamson {
14400322913cSAlan Adamson return sprintf(page, "%s\n", &to_t10_wwn(item)->model[0]);
14410322913cSAlan Adamson }
14420322913cSAlan Adamson
target_wwn_product_id_store(struct config_item * item,const char * page,size_t count)14430322913cSAlan Adamson static ssize_t target_wwn_product_id_store(struct config_item *item,
14440322913cSAlan Adamson const char *page, size_t count)
14450322913cSAlan Adamson {
14460322913cSAlan Adamson struct t10_wwn *t10_wwn = to_t10_wwn(item);
14470322913cSAlan Adamson struct se_device *dev = t10_wwn->t10_dev;
14480322913cSAlan Adamson /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
14490322913cSAlan Adamson unsigned char buf[INQUIRY_MODEL_LEN + 2];
14500322913cSAlan Adamson char *stripped = NULL;
14515c584fe6SAzeem Shaikh ssize_t len;
1452ee26724aSColin Ian King ssize_t ret;
14530322913cSAlan Adamson
14545c584fe6SAzeem Shaikh len = strscpy(buf, page, sizeof(buf));
14555c584fe6SAzeem Shaikh if (len > 0) {
14560322913cSAlan Adamson /* Strip any newline added from userspace. */
14570322913cSAlan Adamson stripped = strstrip(buf);
14580322913cSAlan Adamson len = strlen(stripped);
14590322913cSAlan Adamson }
14605c584fe6SAzeem Shaikh if (len < 0 || len > INQUIRY_MODEL_LEN) {
14610322913cSAlan Adamson pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: "
14620322913cSAlan Adamson __stringify(INQUIRY_MODEL_LEN)
14630322913cSAlan Adamson "\n");
14640322913cSAlan Adamson return -EOVERFLOW;
14650322913cSAlan Adamson }
14660322913cSAlan Adamson
14670322913cSAlan Adamson ret = target_check_inquiry_data(stripped);
14680322913cSAlan Adamson
14690322913cSAlan Adamson if (ret < 0)
14700322913cSAlan Adamson return ret;
14710322913cSAlan Adamson
14720322913cSAlan Adamson /*
14730322913cSAlan Adamson * Check to see if any active exports exist. If they do exist, fail
14740322913cSAlan Adamson * here as changing this information on the fly (underneath the
14750322913cSAlan Adamson * initiator side OS dependent multipath code) could cause negative
14760322913cSAlan Adamson * effects.
14770322913cSAlan Adamson */
14780322913cSAlan Adamson if (dev->export_count) {
14790322913cSAlan Adamson pr_err("Unable to set T10 Model while active %d exports exist\n",
14800322913cSAlan Adamson dev->export_count);
14810322913cSAlan Adamson return -EINVAL;
14820322913cSAlan Adamson }
14830322913cSAlan Adamson
14840322913cSAlan Adamson BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
14850871237aSAzeem Shaikh strscpy(dev->t10_wwn.model, stripped, sizeof(dev->t10_wwn.model));
14860322913cSAlan Adamson
14870322913cSAlan Adamson pr_debug("Target_Core_ConfigFS: Set emulated T10 Model Identification: %s\n",
14880322913cSAlan Adamson dev->t10_wwn.model);
14890322913cSAlan Adamson
14900322913cSAlan Adamson return count;
14910322913cSAlan Adamson }
14920322913cSAlan Adamson
target_wwn_revision_show(struct config_item * item,char * page)14930322913cSAlan Adamson static ssize_t target_wwn_revision_show(struct config_item *item,
14940322913cSAlan Adamson char *page)
14950322913cSAlan Adamson {
14960322913cSAlan Adamson return sprintf(page, "%s\n", &to_t10_wwn(item)->revision[0]);
14970322913cSAlan Adamson }
14980322913cSAlan Adamson
target_wwn_revision_store(struct config_item * item,const char * page,size_t count)14990322913cSAlan Adamson static ssize_t target_wwn_revision_store(struct config_item *item,
15000322913cSAlan Adamson const char *page, size_t count)
15010322913cSAlan Adamson {
15020322913cSAlan Adamson struct t10_wwn *t10_wwn = to_t10_wwn(item);
15030322913cSAlan Adamson struct se_device *dev = t10_wwn->t10_dev;
15040322913cSAlan Adamson /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
15050322913cSAlan Adamson unsigned char buf[INQUIRY_REVISION_LEN + 2];
15060322913cSAlan Adamson char *stripped = NULL;
15075c584fe6SAzeem Shaikh ssize_t len;
1508ee26724aSColin Ian King ssize_t ret;
15090322913cSAlan Adamson
15105c584fe6SAzeem Shaikh len = strscpy(buf, page, sizeof(buf));
15115c584fe6SAzeem Shaikh if (len > 0) {
15120322913cSAlan Adamson /* Strip any newline added from userspace. */
15130322913cSAlan Adamson stripped = strstrip(buf);
15140322913cSAlan Adamson len = strlen(stripped);
15150322913cSAlan Adamson }
15165c584fe6SAzeem Shaikh if (len < 0 || len > INQUIRY_REVISION_LEN) {
15170322913cSAlan Adamson pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: "
15180322913cSAlan Adamson __stringify(INQUIRY_REVISION_LEN)
15190322913cSAlan Adamson "\n");
15200322913cSAlan Adamson return -EOVERFLOW;
15210322913cSAlan Adamson }
15220322913cSAlan Adamson
15230322913cSAlan Adamson ret = target_check_inquiry_data(stripped);
15240322913cSAlan Adamson
15250322913cSAlan Adamson if (ret < 0)
15260322913cSAlan Adamson return ret;
15270322913cSAlan Adamson
15280322913cSAlan Adamson /*
15290322913cSAlan Adamson * Check to see if any active exports exist. If they do exist, fail
15300322913cSAlan Adamson * here as changing this information on the fly (underneath the
15310322913cSAlan Adamson * initiator side OS dependent multipath code) could cause negative
15320322913cSAlan Adamson * effects.
15330322913cSAlan Adamson */
15340322913cSAlan Adamson if (dev->export_count) {
15350322913cSAlan Adamson pr_err("Unable to set T10 Revision while active %d exports exist\n",
15360322913cSAlan Adamson dev->export_count);
15370322913cSAlan Adamson return -EINVAL;
15380322913cSAlan Adamson }
15390322913cSAlan Adamson
15400322913cSAlan Adamson BUILD_BUG_ON(sizeof(dev->t10_wwn.revision) != INQUIRY_REVISION_LEN + 1);
15410871237aSAzeem Shaikh strscpy(dev->t10_wwn.revision, stripped, sizeof(dev->t10_wwn.revision));
15420322913cSAlan Adamson
15430322913cSAlan Adamson pr_debug("Target_Core_ConfigFS: Set emulated T10 Revision: %s\n",
15440322913cSAlan Adamson dev->t10_wwn.revision);
15450322913cSAlan Adamson
15460322913cSAlan Adamson return count;
15470322913cSAlan Adamson }
15480322913cSAlan Adamson
15492469f1e0SSergey Samoylenko static ssize_t
target_wwn_company_id_show(struct config_item * item,char * page)15502469f1e0SSergey Samoylenko target_wwn_company_id_show(struct config_item *item,
15512469f1e0SSergey Samoylenko char *page)
15522469f1e0SSergey Samoylenko {
15532469f1e0SSergey Samoylenko return snprintf(page, PAGE_SIZE, "%#08x\n",
15542469f1e0SSergey Samoylenko to_t10_wwn(item)->company_id);
15552469f1e0SSergey Samoylenko }
15562469f1e0SSergey Samoylenko
15572469f1e0SSergey Samoylenko static ssize_t
target_wwn_company_id_store(struct config_item * item,const char * page,size_t count)15582469f1e0SSergey Samoylenko target_wwn_company_id_store(struct config_item *item,
15592469f1e0SSergey Samoylenko const char *page, size_t count)
15602469f1e0SSergey Samoylenko {
15612469f1e0SSergey Samoylenko struct t10_wwn *t10_wwn = to_t10_wwn(item);
15622469f1e0SSergey Samoylenko struct se_device *dev = t10_wwn->t10_dev;
15632469f1e0SSergey Samoylenko u32 val;
15642469f1e0SSergey Samoylenko int ret;
15652469f1e0SSergey Samoylenko
15662469f1e0SSergey Samoylenko /*
15672469f1e0SSergey Samoylenko * The IEEE COMPANY_ID field should contain a 24-bit canonical
15682469f1e0SSergey Samoylenko * form OUI assigned by the IEEE.
15692469f1e0SSergey Samoylenko */
15702469f1e0SSergey Samoylenko ret = kstrtou32(page, 0, &val);
15712469f1e0SSergey Samoylenko if (ret < 0)
15722469f1e0SSergey Samoylenko return ret;
15732469f1e0SSergey Samoylenko
15742469f1e0SSergey Samoylenko if (val >= 0x1000000)
15752469f1e0SSergey Samoylenko return -EOVERFLOW;
15762469f1e0SSergey Samoylenko
15772469f1e0SSergey Samoylenko /*
15782469f1e0SSergey Samoylenko * Check to see if any active exports exist. If they do exist, fail
15792469f1e0SSergey Samoylenko * here as changing this information on the fly (underneath the
15802469f1e0SSergey Samoylenko * initiator side OS dependent multipath code) could cause negative
15812469f1e0SSergey Samoylenko * effects.
15822469f1e0SSergey Samoylenko */
15832469f1e0SSergey Samoylenko if (dev->export_count) {
15842469f1e0SSergey Samoylenko pr_err("Unable to set Company ID while %u exports exist\n",
15852469f1e0SSergey Samoylenko dev->export_count);
15862469f1e0SSergey Samoylenko return -EINVAL;
15872469f1e0SSergey Samoylenko }
15882469f1e0SSergey Samoylenko
15892469f1e0SSergey Samoylenko t10_wwn->company_id = val;
15902469f1e0SSergey Samoylenko
15912469f1e0SSergey Samoylenko pr_debug("Target_Core_ConfigFS: Set IEEE Company ID: %#08x\n",
15922469f1e0SSergey Samoylenko t10_wwn->company_id);
15932469f1e0SSergey Samoylenko
15942469f1e0SSergey Samoylenko return count;
15952469f1e0SSergey Samoylenko }
15962469f1e0SSergey Samoylenko
159754a6f3f6SDavid Disseldorp /*
1598c66ac9dbSNicholas Bellinger * VPD page 0x80 Unit serial
1599c66ac9dbSNicholas Bellinger */
target_wwn_vpd_unit_serial_show(struct config_item * item,char * page)16002eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1601c66ac9dbSNicholas Bellinger char *page)
1602c66ac9dbSNicholas Bellinger {
1603c66ac9dbSNicholas Bellinger return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
16042eafd729SChristoph Hellwig &to_t10_wwn(item)->unit_serial[0]);
1605c66ac9dbSNicholas Bellinger }
1606c66ac9dbSNicholas Bellinger
target_wwn_vpd_unit_serial_store(struct config_item * item,const char * page,size_t count)16072eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
16082eafd729SChristoph Hellwig const char *page, size_t count)
1609c66ac9dbSNicholas Bellinger {
16102eafd729SChristoph Hellwig struct t10_wwn *t10_wwn = to_t10_wwn(item);
16110fd97ccfSChristoph Hellwig struct se_device *dev = t10_wwn->t10_dev;
16122d4e2dafSChaitanya Kulkarni unsigned char buf[INQUIRY_VPD_SERIAL_LEN] = { };
1613c66ac9dbSNicholas Bellinger
1614c66ac9dbSNicholas Bellinger /*
1615c66ac9dbSNicholas Bellinger * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1616c66ac9dbSNicholas Bellinger * from the struct scsi_device level firmware, do not allow
1617c66ac9dbSNicholas Bellinger * VPD Unit Serial to be emulated.
1618c66ac9dbSNicholas Bellinger *
1619c66ac9dbSNicholas Bellinger * Note this struct scsi_device could also be emulating VPD
1620c66ac9dbSNicholas Bellinger * information from its drivers/scsi LLD. But for now we assume
1621c66ac9dbSNicholas Bellinger * it is doing 'the right thing' wrt a world wide unique
1622c66ac9dbSNicholas Bellinger * VPD Unit Serial Number that OS dependent multipath can depend on.
1623c66ac9dbSNicholas Bellinger */
16240fd97ccfSChristoph Hellwig if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
16256708bb27SAndy Grover pr_err("Underlying SCSI device firmware provided VPD"
1626c66ac9dbSNicholas Bellinger " Unit Serial, ignoring request\n");
1627c66ac9dbSNicholas Bellinger return -EOPNOTSUPP;
1628c66ac9dbSNicholas Bellinger }
1629c66ac9dbSNicholas Bellinger
163060d645a4SDan Carpenter if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
16316708bb27SAndy Grover pr_err("Emulated VPD Unit Serial exceeds"
1632c66ac9dbSNicholas Bellinger " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1633c66ac9dbSNicholas Bellinger return -EOVERFLOW;
1634c66ac9dbSNicholas Bellinger }
1635c66ac9dbSNicholas Bellinger /*
1636c66ac9dbSNicholas Bellinger * Check to see if any active $FABRIC_MOD exports exist. If they
1637c66ac9dbSNicholas Bellinger * do exist, fail here as changing this information on the fly
1638c66ac9dbSNicholas Bellinger * (underneath the initiator side OS dependent multipath code)
1639c66ac9dbSNicholas Bellinger * could cause negative effects.
1640c66ac9dbSNicholas Bellinger */
16410fd97ccfSChristoph Hellwig if (dev->export_count) {
16426708bb27SAndy Grover pr_err("Unable to set VPD Unit Serial while"
1643c66ac9dbSNicholas Bellinger " active %d $FABRIC_MOD exports exist\n",
16440fd97ccfSChristoph Hellwig dev->export_count);
1645c66ac9dbSNicholas Bellinger return -EINVAL;
1646c66ac9dbSNicholas Bellinger }
16470fd97ccfSChristoph Hellwig
1648c66ac9dbSNicholas Bellinger /*
1649c66ac9dbSNicholas Bellinger * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1650c66ac9dbSNicholas Bellinger *
1651c66ac9dbSNicholas Bellinger * Also, strip any newline added from the userspace
1652c66ac9dbSNicholas Bellinger * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1653c66ac9dbSNicholas Bellinger */
1654c66ac9dbSNicholas Bellinger snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
16550fd97ccfSChristoph Hellwig snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1656c66ac9dbSNicholas Bellinger "%s", strstrip(buf));
16570fd97ccfSChristoph Hellwig dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1658c66ac9dbSNicholas Bellinger
16596708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
16600fd97ccfSChristoph Hellwig " %s\n", dev->t10_wwn.unit_serial);
1661c66ac9dbSNicholas Bellinger
1662c66ac9dbSNicholas Bellinger return count;
1663c66ac9dbSNicholas Bellinger }
1664c66ac9dbSNicholas Bellinger
1665c66ac9dbSNicholas Bellinger /*
1666c66ac9dbSNicholas Bellinger * VPD page 0x83 Protocol Identifier
1667c66ac9dbSNicholas Bellinger */
target_wwn_vpd_protocol_identifier_show(struct config_item * item,char * page)16682eafd729SChristoph Hellwig static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1669c66ac9dbSNicholas Bellinger char *page)
1670c66ac9dbSNicholas Bellinger {
16712eafd729SChristoph Hellwig struct t10_wwn *t10_wwn = to_t10_wwn(item);
1672c66ac9dbSNicholas Bellinger struct t10_vpd *vpd;
16732d4e2dafSChaitanya Kulkarni unsigned char buf[VPD_TMP_BUF_SIZE] = { };
1674c66ac9dbSNicholas Bellinger ssize_t len = 0;
1675c66ac9dbSNicholas Bellinger
1676c66ac9dbSNicholas Bellinger spin_lock(&t10_wwn->t10_vpd_lock);
1677c66ac9dbSNicholas Bellinger list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
16786708bb27SAndy Grover if (!vpd->protocol_identifier_set)
1679c66ac9dbSNicholas Bellinger continue;
1680c66ac9dbSNicholas Bellinger
1681c66ac9dbSNicholas Bellinger transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1682c66ac9dbSNicholas Bellinger
16836708bb27SAndy Grover if (len + strlen(buf) >= PAGE_SIZE)
1684c66ac9dbSNicholas Bellinger break;
1685c66ac9dbSNicholas Bellinger
1686c66ac9dbSNicholas Bellinger len += sprintf(page+len, "%s", buf);
1687c66ac9dbSNicholas Bellinger }
1688c66ac9dbSNicholas Bellinger spin_unlock(&t10_wwn->t10_vpd_lock);
1689c66ac9dbSNicholas Bellinger
1690c66ac9dbSNicholas Bellinger return len;
1691c66ac9dbSNicholas Bellinger }
1692c66ac9dbSNicholas Bellinger
1693c66ac9dbSNicholas Bellinger /*
1694c66ac9dbSNicholas Bellinger * Generic wrapper for dumping VPD identifiers by association.
1695c66ac9dbSNicholas Bellinger */
1696c66ac9dbSNicholas Bellinger #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
16972eafd729SChristoph Hellwig static ssize_t target_wwn_##_name##_show(struct config_item *item, \
1698c66ac9dbSNicholas Bellinger char *page) \
1699c66ac9dbSNicholas Bellinger { \
17002eafd729SChristoph Hellwig struct t10_wwn *t10_wwn = to_t10_wwn(item); \
1701c66ac9dbSNicholas Bellinger struct t10_vpd *vpd; \
1702c66ac9dbSNicholas Bellinger unsigned char buf[VPD_TMP_BUF_SIZE]; \
1703c66ac9dbSNicholas Bellinger ssize_t len = 0; \
1704c66ac9dbSNicholas Bellinger \
1705c66ac9dbSNicholas Bellinger spin_lock(&t10_wwn->t10_vpd_lock); \
1706c66ac9dbSNicholas Bellinger list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1707c66ac9dbSNicholas Bellinger if (vpd->association != _assoc) \
1708c66ac9dbSNicholas Bellinger continue; \
1709c66ac9dbSNicholas Bellinger \
1710c66ac9dbSNicholas Bellinger memset(buf, 0, VPD_TMP_BUF_SIZE); \
1711c66ac9dbSNicholas Bellinger transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
17126708bb27SAndy Grover if (len + strlen(buf) >= PAGE_SIZE) \
1713c66ac9dbSNicholas Bellinger break; \
1714c66ac9dbSNicholas Bellinger len += sprintf(page+len, "%s", buf); \
1715c66ac9dbSNicholas Bellinger \
1716c66ac9dbSNicholas Bellinger memset(buf, 0, VPD_TMP_BUF_SIZE); \
1717c66ac9dbSNicholas Bellinger transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
17186708bb27SAndy Grover if (len + strlen(buf) >= PAGE_SIZE) \
1719c66ac9dbSNicholas Bellinger break; \
1720c66ac9dbSNicholas Bellinger len += sprintf(page+len, "%s", buf); \
1721c66ac9dbSNicholas Bellinger \
1722c66ac9dbSNicholas Bellinger memset(buf, 0, VPD_TMP_BUF_SIZE); \
1723c66ac9dbSNicholas Bellinger transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
17246708bb27SAndy Grover if (len + strlen(buf) >= PAGE_SIZE) \
1725c66ac9dbSNicholas Bellinger break; \
1726c66ac9dbSNicholas Bellinger len += sprintf(page+len, "%s", buf); \
1727c66ac9dbSNicholas Bellinger } \
1728c66ac9dbSNicholas Bellinger spin_unlock(&t10_wwn->t10_vpd_lock); \
1729c66ac9dbSNicholas Bellinger \
1730c66ac9dbSNicholas Bellinger return len; \
1731c66ac9dbSNicholas Bellinger }
1732c66ac9dbSNicholas Bellinger
17332eafd729SChristoph Hellwig /* VPD page 0x83 Association: Logical Unit */
1734c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
17352eafd729SChristoph Hellwig /* VPD page 0x83 Association: Target Port */
1736c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
17372eafd729SChristoph Hellwig /* VPD page 0x83 Association: SCSI Target Device */
1738c66ac9dbSNicholas Bellinger DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1739c66ac9dbSNicholas Bellinger
174054a6f3f6SDavid Disseldorp CONFIGFS_ATTR(target_wwn_, vendor_id);
17410322913cSAlan Adamson CONFIGFS_ATTR(target_wwn_, product_id);
17420322913cSAlan Adamson CONFIGFS_ATTR(target_wwn_, revision);
17432469f1e0SSergey Samoylenko CONFIGFS_ATTR(target_wwn_, company_id);
17442eafd729SChristoph Hellwig CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
17452eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
17462eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
17472eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
17482eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1749c66ac9dbSNicholas Bellinger
1750c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
175154a6f3f6SDavid Disseldorp &target_wwn_attr_vendor_id,
17520322913cSAlan Adamson &target_wwn_attr_product_id,
17530322913cSAlan Adamson &target_wwn_attr_revision,
17542469f1e0SSergey Samoylenko &target_wwn_attr_company_id,
17552eafd729SChristoph Hellwig &target_wwn_attr_vpd_unit_serial,
17562eafd729SChristoph Hellwig &target_wwn_attr_vpd_protocol_identifier,
17572eafd729SChristoph Hellwig &target_wwn_attr_vpd_assoc_logical_unit,
17582eafd729SChristoph Hellwig &target_wwn_attr_vpd_assoc_target_port,
17592eafd729SChristoph Hellwig &target_wwn_attr_vpd_assoc_scsi_target_device,
1760c66ac9dbSNicholas Bellinger NULL,
1761c66ac9dbSNicholas Bellinger };
1762c66ac9dbSNicholas Bellinger
17632eafd729SChristoph Hellwig TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1764c66ac9dbSNicholas Bellinger
1765f8d389c6SNicholas Bellinger /* End functions for struct config_item_type tb_dev_wwn_cit */
1766c66ac9dbSNicholas Bellinger
176791e2e39bSNicholas Bellinger /* Start functions for struct config_item_type tb_dev_pr_cit */
1768c66ac9dbSNicholas Bellinger
pr_to_dev(struct config_item * item)17692eafd729SChristoph Hellwig static struct se_device *pr_to_dev(struct config_item *item)
17702eafd729SChristoph Hellwig {
17712eafd729SChristoph Hellwig return container_of(to_config_group(item), struct se_device,
17722eafd729SChristoph Hellwig dev_pr_group);
17732eafd729SChristoph Hellwig }
1774c66ac9dbSNicholas Bellinger
target_core_dev_pr_show_spc3_res(struct se_device * dev,char * page)1775d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1776d977f437SChristoph Hellwig char *page)
1777c66ac9dbSNicholas Bellinger {
1778c66ac9dbSNicholas Bellinger struct se_node_acl *se_nacl;
1779c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg;
17802d4e2dafSChaitanya Kulkarni char i_buf[PR_REG_ISID_ID_LEN] = { };
1781c66ac9dbSNicholas Bellinger
1782c66ac9dbSNicholas Bellinger pr_reg = dev->dev_pr_res_holder;
1783d977f437SChristoph Hellwig if (!pr_reg)
1784d977f437SChristoph Hellwig return sprintf(page, "No SPC-3 Reservation holder\n");
1785d977f437SChristoph Hellwig
1786c66ac9dbSNicholas Bellinger se_nacl = pr_reg->pr_reg_nacl;
1787d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1788c66ac9dbSNicholas Bellinger
1789d977f437SChristoph Hellwig return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
179030c7ca93SDavid Disseldorp se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1791d2843c17SAndy Grover se_nacl->initiatorname, i_buf);
1792c66ac9dbSNicholas Bellinger }
1793c66ac9dbSNicholas Bellinger
target_core_dev_pr_show_spc2_res(struct se_device * dev,char * page)1794d977f437SChristoph Hellwig static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1795d977f437SChristoph Hellwig char *page)
1796c66ac9dbSNicholas Bellinger {
1797fae43461SBart Van Assche struct se_session *sess = dev->reservation_holder;
1798c66ac9dbSNicholas Bellinger struct se_node_acl *se_nacl;
1799d977f437SChristoph Hellwig ssize_t len;
1800c66ac9dbSNicholas Bellinger
1801fae43461SBart Van Assche if (sess) {
1802fae43461SBart Van Assche se_nacl = sess->se_node_acl;
1803d977f437SChristoph Hellwig len = sprintf(page,
1804d977f437SChristoph Hellwig "SPC-2 Reservation: %s Initiator: %s\n",
180530c7ca93SDavid Disseldorp se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1806c66ac9dbSNicholas Bellinger se_nacl->initiatorname);
1807d977f437SChristoph Hellwig } else {
1808d977f437SChristoph Hellwig len = sprintf(page, "No SPC-2 Reservation holder\n");
1809d977f437SChristoph Hellwig }
1810d977f437SChristoph Hellwig return len;
1811c66ac9dbSNicholas Bellinger }
1812c66ac9dbSNicholas Bellinger
target_pr_res_holder_show(struct config_item * item,char * page)18132eafd729SChristoph Hellwig static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1814c66ac9dbSNicholas Bellinger {
18152eafd729SChristoph Hellwig struct se_device *dev = pr_to_dev(item);
1816d977f437SChristoph Hellwig int ret;
1817c66ac9dbSNicholas Bellinger
1818b49d6f78SDavid Disseldorp if (!dev->dev_attrib.emulate_pr)
1819b49d6f78SDavid Disseldorp return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1820b49d6f78SDavid Disseldorp
182169088a04SBodo Stroesser if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1822d977f437SChristoph Hellwig return sprintf(page, "Passthrough\n");
1823c66ac9dbSNicholas Bellinger
1824d977f437SChristoph Hellwig spin_lock(&dev->dev_reservation_lock);
1825d977f437SChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1826d977f437SChristoph Hellwig ret = target_core_dev_pr_show_spc2_res(dev, page);
1827d977f437SChristoph Hellwig else
1828d977f437SChristoph Hellwig ret = target_core_dev_pr_show_spc3_res(dev, page);
1829d977f437SChristoph Hellwig spin_unlock(&dev->dev_reservation_lock);
1830d977f437SChristoph Hellwig return ret;
1831c66ac9dbSNicholas Bellinger }
1832c66ac9dbSNicholas Bellinger
target_pr_res_pr_all_tgt_pts_show(struct config_item * item,char * page)18332eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
18342eafd729SChristoph Hellwig char *page)
1835c66ac9dbSNicholas Bellinger {
18362eafd729SChristoph Hellwig struct se_device *dev = pr_to_dev(item);
1837c66ac9dbSNicholas Bellinger ssize_t len = 0;
1838c66ac9dbSNicholas Bellinger
1839c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock);
1840d977f437SChristoph Hellwig if (!dev->dev_pr_res_holder) {
1841c66ac9dbSNicholas Bellinger len = sprintf(page, "No SPC-3 Reservation holder\n");
1842d977f437SChristoph Hellwig } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1843c66ac9dbSNicholas Bellinger len = sprintf(page, "SPC-3 Reservation: All Target"
1844c66ac9dbSNicholas Bellinger " Ports registration\n");
1845d977f437SChristoph Hellwig } else {
1846c66ac9dbSNicholas Bellinger len = sprintf(page, "SPC-3 Reservation: Single"
1847c66ac9dbSNicholas Bellinger " Target Port registration\n");
1848d977f437SChristoph Hellwig }
1849c66ac9dbSNicholas Bellinger
1850d977f437SChristoph Hellwig spin_unlock(&dev->dev_reservation_lock);
1851c66ac9dbSNicholas Bellinger return len;
1852c66ac9dbSNicholas Bellinger }
1853c66ac9dbSNicholas Bellinger
target_pr_res_pr_generation_show(struct config_item * item,char * page)18542eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
18552eafd729SChristoph Hellwig char *page)
1856c66ac9dbSNicholas Bellinger {
18572eafd729SChristoph Hellwig return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1858c66ac9dbSNicholas Bellinger }
1859c66ac9dbSNicholas Bellinger
1860c66ac9dbSNicholas Bellinger
target_pr_res_pr_holder_tg_port_show(struct config_item * item,char * page)18612eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
18622eafd729SChristoph Hellwig char *page)
1863c66ac9dbSNicholas Bellinger {
18642eafd729SChristoph Hellwig struct se_device *dev = pr_to_dev(item);
1865c66ac9dbSNicholas Bellinger struct se_node_acl *se_nacl;
1866c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg;
1867c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg;
18689ac8928eSChristoph Hellwig const struct target_core_fabric_ops *tfo;
1869c66ac9dbSNicholas Bellinger ssize_t len = 0;
1870c66ac9dbSNicholas Bellinger
1871c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock);
1872c66ac9dbSNicholas Bellinger pr_reg = dev->dev_pr_res_holder;
18736708bb27SAndy Grover if (!pr_reg) {
1874c66ac9dbSNicholas Bellinger len = sprintf(page, "No SPC-3 Reservation holder\n");
1875d977f437SChristoph Hellwig goto out_unlock;
1876c66ac9dbSNicholas Bellinger }
1877d977f437SChristoph Hellwig
1878c66ac9dbSNicholas Bellinger se_nacl = pr_reg->pr_reg_nacl;
1879c66ac9dbSNicholas Bellinger se_tpg = se_nacl->se_tpg;
1880e3d6f909SAndy Grover tfo = se_tpg->se_tpg_tfo;
1881c66ac9dbSNicholas Bellinger
1882c66ac9dbSNicholas Bellinger len += sprintf(page+len, "SPC-3 Reservation: %s"
188330c7ca93SDavid Disseldorp " Target Node Endpoint: %s\n", tfo->fabric_name,
1884c66ac9dbSNicholas Bellinger tfo->tpg_get_wwn(se_tpg));
1885c66ac9dbSNicholas Bellinger len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
188635d1efe8SMasanari Iida " Identifier Tag: %hu %s Portal Group Tag: %hu"
1887f2d30680SHannes Reinecke " %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
188830c7ca93SDavid Disseldorp tfo->fabric_name, tfo->tpg_get_tag(se_tpg),
188930c7ca93SDavid Disseldorp tfo->fabric_name, pr_reg->pr_aptpl_target_lun);
1890c66ac9dbSNicholas Bellinger
1891d977f437SChristoph Hellwig out_unlock:
1892d977f437SChristoph Hellwig spin_unlock(&dev->dev_reservation_lock);
1893c66ac9dbSNicholas Bellinger return len;
1894c66ac9dbSNicholas Bellinger }
1895c66ac9dbSNicholas Bellinger
1896c66ac9dbSNicholas Bellinger
target_pr_res_pr_registered_i_pts_show(struct config_item * item,char * page)18972eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
18982eafd729SChristoph Hellwig char *page)
1899c66ac9dbSNicholas Bellinger {
19002eafd729SChristoph Hellwig struct se_device *dev = pr_to_dev(item);
19019ac8928eSChristoph Hellwig const struct target_core_fabric_ops *tfo;
1902c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg;
1903c66ac9dbSNicholas Bellinger unsigned char buf[384];
1904c66ac9dbSNicholas Bellinger char i_buf[PR_REG_ISID_ID_LEN];
1905c66ac9dbSNicholas Bellinger ssize_t len = 0;
1906d2843c17SAndy Grover int reg_count = 0;
1907c66ac9dbSNicholas Bellinger
1908c66ac9dbSNicholas Bellinger len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1909c66ac9dbSNicholas Bellinger
19100fd97ccfSChristoph Hellwig spin_lock(&dev->t10_pr.registration_lock);
19110fd97ccfSChristoph Hellwig list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1912c66ac9dbSNicholas Bellinger pr_reg_list) {
1913c66ac9dbSNicholas Bellinger
1914c66ac9dbSNicholas Bellinger memset(buf, 0, 384);
1915c66ac9dbSNicholas Bellinger memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1916c66ac9dbSNicholas Bellinger tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1917d2843c17SAndy Grover core_pr_dump_initiator_port(pr_reg, i_buf,
1918c66ac9dbSNicholas Bellinger PR_REG_ISID_ID_LEN);
1919c66ac9dbSNicholas Bellinger sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
192030c7ca93SDavid Disseldorp tfo->fabric_name,
1921d2843c17SAndy Grover pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1922c66ac9dbSNicholas Bellinger pr_reg->pr_res_generation);
1923c66ac9dbSNicholas Bellinger
19246708bb27SAndy Grover if (len + strlen(buf) >= PAGE_SIZE)
1925c66ac9dbSNicholas Bellinger break;
1926c66ac9dbSNicholas Bellinger
1927c66ac9dbSNicholas Bellinger len += sprintf(page+len, "%s", buf);
1928c66ac9dbSNicholas Bellinger reg_count++;
1929c66ac9dbSNicholas Bellinger }
19300fd97ccfSChristoph Hellwig spin_unlock(&dev->t10_pr.registration_lock);
1931c66ac9dbSNicholas Bellinger
19326708bb27SAndy Grover if (!reg_count)
1933c66ac9dbSNicholas Bellinger len += sprintf(page+len, "None\n");
1934c66ac9dbSNicholas Bellinger
1935c66ac9dbSNicholas Bellinger return len;
1936c66ac9dbSNicholas Bellinger }
1937c66ac9dbSNicholas Bellinger
target_pr_res_pr_type_show(struct config_item * item,char * page)19382eafd729SChristoph Hellwig static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1939c66ac9dbSNicholas Bellinger {
19402eafd729SChristoph Hellwig struct se_device *dev = pr_to_dev(item);
1941c66ac9dbSNicholas Bellinger struct t10_pr_registration *pr_reg;
1942c66ac9dbSNicholas Bellinger ssize_t len = 0;
1943c66ac9dbSNicholas Bellinger
1944c66ac9dbSNicholas Bellinger spin_lock(&dev->dev_reservation_lock);
1945c66ac9dbSNicholas Bellinger pr_reg = dev->dev_pr_res_holder;
1946d977f437SChristoph Hellwig if (pr_reg) {
1947c66ac9dbSNicholas Bellinger len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1948c66ac9dbSNicholas Bellinger core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1949d977f437SChristoph Hellwig } else {
1950d977f437SChristoph Hellwig len = sprintf(page, "No SPC-3 Reservation holder\n");
1951d977f437SChristoph Hellwig }
1952c66ac9dbSNicholas Bellinger
1953d977f437SChristoph Hellwig spin_unlock(&dev->dev_reservation_lock);
1954c66ac9dbSNicholas Bellinger return len;
1955c66ac9dbSNicholas Bellinger }
1956c66ac9dbSNicholas Bellinger
target_pr_res_type_show(struct config_item * item,char * page)19572eafd729SChristoph Hellwig static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
1958c66ac9dbSNicholas Bellinger {
19592eafd729SChristoph Hellwig struct se_device *dev = pr_to_dev(item);
19602eafd729SChristoph Hellwig
1961b49d6f78SDavid Disseldorp if (!dev->dev_attrib.emulate_pr)
1962b49d6f78SDavid Disseldorp return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
196369088a04SBodo Stroesser if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1964d977f437SChristoph Hellwig return sprintf(page, "SPC_PASSTHROUGH\n");
1965b49d6f78SDavid Disseldorp if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1966d977f437SChristoph Hellwig return sprintf(page, "SPC2_RESERVATIONS\n");
1967b49d6f78SDavid Disseldorp
1968d977f437SChristoph Hellwig return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1969c66ac9dbSNicholas Bellinger }
1970c66ac9dbSNicholas Bellinger
target_pr_res_aptpl_active_show(struct config_item * item,char * page)19712eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
19722eafd729SChristoph Hellwig char *page)
1973c66ac9dbSNicholas Bellinger {
19742eafd729SChristoph Hellwig struct se_device *dev = pr_to_dev(item);
19752eafd729SChristoph Hellwig
1976b49d6f78SDavid Disseldorp if (!dev->dev_attrib.emulate_pr ||
197769088a04SBodo Stroesser (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1978c66ac9dbSNicholas Bellinger return 0;
1979c66ac9dbSNicholas Bellinger
1980c66ac9dbSNicholas Bellinger return sprintf(page, "APTPL Bit Status: %s\n",
19810fd97ccfSChristoph Hellwig (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1982c66ac9dbSNicholas Bellinger }
1983c66ac9dbSNicholas Bellinger
target_pr_res_aptpl_metadata_show(struct config_item * item,char * page)19842eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
19852eafd729SChristoph Hellwig char *page)
1986c66ac9dbSNicholas Bellinger {
19872eafd729SChristoph Hellwig struct se_device *dev = pr_to_dev(item);
19882eafd729SChristoph Hellwig
1989b49d6f78SDavid Disseldorp if (!dev->dev_attrib.emulate_pr ||
199069088a04SBodo Stroesser (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1991c66ac9dbSNicholas Bellinger return 0;
1992c66ac9dbSNicholas Bellinger
1993c66ac9dbSNicholas Bellinger return sprintf(page, "Ready to process PR APTPL metadata..\n");
1994c66ac9dbSNicholas Bellinger }
1995c66ac9dbSNicholas Bellinger
1996c66ac9dbSNicholas Bellinger enum {
1997c66ac9dbSNicholas Bellinger Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1998c66ac9dbSNicholas Bellinger Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1999c66ac9dbSNicholas Bellinger Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
2000c66ac9dbSNicholas Bellinger Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
2001c66ac9dbSNicholas Bellinger };
2002c66ac9dbSNicholas Bellinger
2003c66ac9dbSNicholas Bellinger static match_table_t tokens = {
2004c66ac9dbSNicholas Bellinger {Opt_initiator_fabric, "initiator_fabric=%s"},
2005c66ac9dbSNicholas Bellinger {Opt_initiator_node, "initiator_node=%s"},
2006c66ac9dbSNicholas Bellinger {Opt_initiator_sid, "initiator_sid=%s"},
2007c66ac9dbSNicholas Bellinger {Opt_sa_res_key, "sa_res_key=%s"},
2008c66ac9dbSNicholas Bellinger {Opt_res_holder, "res_holder=%d"},
2009c66ac9dbSNicholas Bellinger {Opt_res_type, "res_type=%d"},
2010c66ac9dbSNicholas Bellinger {Opt_res_scope, "res_scope=%d"},
2011c66ac9dbSNicholas Bellinger {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
2012a2db857bStangwenji {Opt_mapped_lun, "mapped_lun=%u"},
2013c66ac9dbSNicholas Bellinger {Opt_target_fabric, "target_fabric=%s"},
2014c66ac9dbSNicholas Bellinger {Opt_target_node, "target_node=%s"},
2015c66ac9dbSNicholas Bellinger {Opt_tpgt, "tpgt=%d"},
2016c66ac9dbSNicholas Bellinger {Opt_port_rtpi, "port_rtpi=%d"},
2017a2db857bStangwenji {Opt_target_lun, "target_lun=%u"},
2018c66ac9dbSNicholas Bellinger {Opt_err, NULL}
2019c66ac9dbSNicholas Bellinger };
2020c66ac9dbSNicholas Bellinger
target_pr_res_aptpl_metadata_store(struct config_item * item,const char * page,size_t count)20212eafd729SChristoph Hellwig static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
20222eafd729SChristoph Hellwig const char *page, size_t count)
2023c66ac9dbSNicholas Bellinger {
20242eafd729SChristoph Hellwig struct se_device *dev = pr_to_dev(item);
20256d180253SJesper Juhl unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
20266d180253SJesper Juhl unsigned char *t_fabric = NULL, *t_port = NULL;
20278d213559SJoern Engel char *orig, *ptr, *opts;
2028c66ac9dbSNicholas Bellinger substring_t args[MAX_OPT_ARGS];
2029c66ac9dbSNicholas Bellinger unsigned long long tmp_ll;
2030c66ac9dbSNicholas Bellinger u64 sa_res_key = 0;
2031f2d30680SHannes Reinecke u64 mapped_lun = 0, target_lun = 0;
2032c66ac9dbSNicholas Bellinger int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
203345fb94c2SBart Van Assche u16 tpgt = 0;
203445fb94c2SBart Van Assche u8 type = 0;
2035c66ac9dbSNicholas Bellinger
2036b49d6f78SDavid Disseldorp if (!dev->dev_attrib.emulate_pr ||
203769088a04SBodo Stroesser (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
20389105bfc0SAndy Grover return count;
2039d977f437SChristoph Hellwig if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
20409105bfc0SAndy Grover return count;
2041c66ac9dbSNicholas Bellinger
20420fd97ccfSChristoph Hellwig if (dev->export_count) {
20436708bb27SAndy Grover pr_debug("Unable to process APTPL metadata while"
2044c66ac9dbSNicholas Bellinger " active fabric exports exist\n");
2045c66ac9dbSNicholas Bellinger return -EINVAL;
2046c66ac9dbSNicholas Bellinger }
2047c66ac9dbSNicholas Bellinger
2048c66ac9dbSNicholas Bellinger opts = kstrdup(page, GFP_KERNEL);
2049c66ac9dbSNicholas Bellinger if (!opts)
2050c66ac9dbSNicholas Bellinger return -ENOMEM;
2051c66ac9dbSNicholas Bellinger
2052c66ac9dbSNicholas Bellinger orig = opts;
205390c161b6SSebastian Andrzej Siewior while ((ptr = strsep(&opts, ",\n")) != NULL) {
2054c66ac9dbSNicholas Bellinger if (!*ptr)
2055c66ac9dbSNicholas Bellinger continue;
2056c66ac9dbSNicholas Bellinger
2057c66ac9dbSNicholas Bellinger token = match_token(ptr, tokens, args);
2058c66ac9dbSNicholas Bellinger switch (token) {
2059c66ac9dbSNicholas Bellinger case Opt_initiator_fabric:
20608d213559SJoern Engel i_fabric = match_strdup(args);
20616d180253SJesper Juhl if (!i_fabric) {
20626d180253SJesper Juhl ret = -ENOMEM;
20636d180253SJesper Juhl goto out;
20646d180253SJesper Juhl }
2065c66ac9dbSNicholas Bellinger break;
2066c66ac9dbSNicholas Bellinger case Opt_initiator_node:
20678d213559SJoern Engel i_port = match_strdup(args);
20686d180253SJesper Juhl if (!i_port) {
20696d180253SJesper Juhl ret = -ENOMEM;
20706d180253SJesper Juhl goto out;
20716d180253SJesper Juhl }
207260d645a4SDan Carpenter if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
20736708bb27SAndy Grover pr_err("APTPL metadata initiator_node="
2074c66ac9dbSNicholas Bellinger " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
2075c66ac9dbSNicholas Bellinger PR_APTPL_MAX_IPORT_LEN);
2076c66ac9dbSNicholas Bellinger ret = -EINVAL;
2077c66ac9dbSNicholas Bellinger break;
2078c66ac9dbSNicholas Bellinger }
2079c66ac9dbSNicholas Bellinger break;
2080c66ac9dbSNicholas Bellinger case Opt_initiator_sid:
20818d213559SJoern Engel isid = match_strdup(args);
20826d180253SJesper Juhl if (!isid) {
20836d180253SJesper Juhl ret = -ENOMEM;
20846d180253SJesper Juhl goto out;
20856d180253SJesper Juhl }
208660d645a4SDan Carpenter if (strlen(isid) >= PR_REG_ISID_LEN) {
20876708bb27SAndy Grover pr_err("APTPL metadata initiator_isid"
2088c66ac9dbSNicholas Bellinger "= exceeds PR_REG_ISID_LEN: %d\n",
2089c66ac9dbSNicholas Bellinger PR_REG_ISID_LEN);
2090c66ac9dbSNicholas Bellinger ret = -EINVAL;
2091c66ac9dbSNicholas Bellinger break;
2092c66ac9dbSNicholas Bellinger }
2093c66ac9dbSNicholas Bellinger break;
2094c66ac9dbSNicholas Bellinger case Opt_sa_res_key:
2095a2db857bStangwenji ret = match_u64(args, &tmp_ll);
2096c66ac9dbSNicholas Bellinger if (ret < 0) {
20978d213559SJoern Engel pr_err("kstrtoull() failed for sa_res_key=\n");
2098c66ac9dbSNicholas Bellinger goto out;
2099c66ac9dbSNicholas Bellinger }
2100c66ac9dbSNicholas Bellinger sa_res_key = (u64)tmp_ll;
2101c66ac9dbSNicholas Bellinger break;
2102c66ac9dbSNicholas Bellinger /*
2103c66ac9dbSNicholas Bellinger * PR APTPL Metadata for Reservation
2104c66ac9dbSNicholas Bellinger */
2105c66ac9dbSNicholas Bellinger case Opt_res_holder:
2106c2091026SDavid Disseldorp ret = match_int(args, &arg);
2107c2091026SDavid Disseldorp if (ret)
2108c2091026SDavid Disseldorp goto out;
2109c66ac9dbSNicholas Bellinger res_holder = arg;
2110c66ac9dbSNicholas Bellinger break;
2111c66ac9dbSNicholas Bellinger case Opt_res_type:
2112c2091026SDavid Disseldorp ret = match_int(args, &arg);
2113c2091026SDavid Disseldorp if (ret)
2114c2091026SDavid Disseldorp goto out;
2115c66ac9dbSNicholas Bellinger type = (u8)arg;
2116c66ac9dbSNicholas Bellinger break;
2117c66ac9dbSNicholas Bellinger case Opt_res_scope:
2118c2091026SDavid Disseldorp ret = match_int(args, &arg);
2119c2091026SDavid Disseldorp if (ret)
2120c2091026SDavid Disseldorp goto out;
2121c66ac9dbSNicholas Bellinger break;
2122c66ac9dbSNicholas Bellinger case Opt_res_all_tg_pt:
2123c2091026SDavid Disseldorp ret = match_int(args, &arg);
2124c2091026SDavid Disseldorp if (ret)
2125c2091026SDavid Disseldorp goto out;
2126c66ac9dbSNicholas Bellinger all_tg_pt = (int)arg;
2127c66ac9dbSNicholas Bellinger break;
2128c66ac9dbSNicholas Bellinger case Opt_mapped_lun:
2129a2db857bStangwenji ret = match_u64(args, &tmp_ll);
2130c2091026SDavid Disseldorp if (ret)
2131c2091026SDavid Disseldorp goto out;
2132a2db857bStangwenji mapped_lun = (u64)tmp_ll;
2133c66ac9dbSNicholas Bellinger break;
2134c66ac9dbSNicholas Bellinger /*
2135c66ac9dbSNicholas Bellinger * PR APTPL Metadata for Target Port
2136c66ac9dbSNicholas Bellinger */
2137c66ac9dbSNicholas Bellinger case Opt_target_fabric:
21388d213559SJoern Engel t_fabric = match_strdup(args);
21396d180253SJesper Juhl if (!t_fabric) {
21406d180253SJesper Juhl ret = -ENOMEM;
21416d180253SJesper Juhl goto out;
21426d180253SJesper Juhl }
2143c66ac9dbSNicholas Bellinger break;
2144c66ac9dbSNicholas Bellinger case Opt_target_node:
21458d213559SJoern Engel t_port = match_strdup(args);
21466d180253SJesper Juhl if (!t_port) {
21476d180253SJesper Juhl ret = -ENOMEM;
21486d180253SJesper Juhl goto out;
21496d180253SJesper Juhl }
215060d645a4SDan Carpenter if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
21516708bb27SAndy Grover pr_err("APTPL metadata target_node="
2152c66ac9dbSNicholas Bellinger " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
2153c66ac9dbSNicholas Bellinger PR_APTPL_MAX_TPORT_LEN);
2154c66ac9dbSNicholas Bellinger ret = -EINVAL;
2155c66ac9dbSNicholas Bellinger break;
2156c66ac9dbSNicholas Bellinger }
2157c66ac9dbSNicholas Bellinger break;
2158c66ac9dbSNicholas Bellinger case Opt_tpgt:
2159c2091026SDavid Disseldorp ret = match_int(args, &arg);
2160c2091026SDavid Disseldorp if (ret)
2161c2091026SDavid Disseldorp goto out;
2162c66ac9dbSNicholas Bellinger tpgt = (u16)arg;
2163c66ac9dbSNicholas Bellinger break;
2164c66ac9dbSNicholas Bellinger case Opt_port_rtpi:
2165c2091026SDavid Disseldorp ret = match_int(args, &arg);
2166c2091026SDavid Disseldorp if (ret)
2167c2091026SDavid Disseldorp goto out;
2168c66ac9dbSNicholas Bellinger break;
2169c66ac9dbSNicholas Bellinger case Opt_target_lun:
2170a2db857bStangwenji ret = match_u64(args, &tmp_ll);
2171c2091026SDavid Disseldorp if (ret)
2172c2091026SDavid Disseldorp goto out;
2173a2db857bStangwenji target_lun = (u64)tmp_ll;
2174c66ac9dbSNicholas Bellinger break;
2175c66ac9dbSNicholas Bellinger default:
2176c66ac9dbSNicholas Bellinger break;
2177c66ac9dbSNicholas Bellinger }
2178c66ac9dbSNicholas Bellinger }
2179c66ac9dbSNicholas Bellinger
21806708bb27SAndy Grover if (!i_port || !t_port || !sa_res_key) {
21816708bb27SAndy Grover pr_err("Illegal parameters for APTPL registration\n");
2182c66ac9dbSNicholas Bellinger ret = -EINVAL;
2183c66ac9dbSNicholas Bellinger goto out;
2184c66ac9dbSNicholas Bellinger }
2185c66ac9dbSNicholas Bellinger
2186c66ac9dbSNicholas Bellinger if (res_holder && !(type)) {
21876708bb27SAndy Grover pr_err("Illegal PR type: 0x%02x for reservation"
2188c66ac9dbSNicholas Bellinger " holder\n", type);
2189c66ac9dbSNicholas Bellinger ret = -EINVAL;
2190c66ac9dbSNicholas Bellinger goto out;
2191c66ac9dbSNicholas Bellinger }
2192c66ac9dbSNicholas Bellinger
21930fd97ccfSChristoph Hellwig ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
2194c66ac9dbSNicholas Bellinger i_port, isid, mapped_lun, t_port, tpgt, target_lun,
2195c66ac9dbSNicholas Bellinger res_holder, all_tg_pt, type);
2196c66ac9dbSNicholas Bellinger out:
21976d180253SJesper Juhl kfree(i_fabric);
21986d180253SJesper Juhl kfree(i_port);
21996d180253SJesper Juhl kfree(isid);
22006d180253SJesper Juhl kfree(t_fabric);
22016d180253SJesper Juhl kfree(t_port);
2202c66ac9dbSNicholas Bellinger kfree(orig);
2203c66ac9dbSNicholas Bellinger return (ret == 0) ? count : ret;
2204c66ac9dbSNicholas Bellinger }
2205c66ac9dbSNicholas Bellinger
2206c66ac9dbSNicholas Bellinger
22072eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_holder);
22082eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
22092eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
22102eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
22112eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
22122eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
22132eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_type);
22142eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
22152eafd729SChristoph Hellwig CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
2216c66ac9dbSNicholas Bellinger
2217c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_dev_pr_attrs[] = {
22182eafd729SChristoph Hellwig &target_pr_attr_res_holder,
22192eafd729SChristoph Hellwig &target_pr_attr_res_pr_all_tgt_pts,
22202eafd729SChristoph Hellwig &target_pr_attr_res_pr_generation,
22212eafd729SChristoph Hellwig &target_pr_attr_res_pr_holder_tg_port,
22222eafd729SChristoph Hellwig &target_pr_attr_res_pr_registered_i_pts,
22232eafd729SChristoph Hellwig &target_pr_attr_res_pr_type,
22242eafd729SChristoph Hellwig &target_pr_attr_res_type,
22252eafd729SChristoph Hellwig &target_pr_attr_res_aptpl_active,
22262eafd729SChristoph Hellwig &target_pr_attr_res_aptpl_metadata,
2227c66ac9dbSNicholas Bellinger NULL,
2228c66ac9dbSNicholas Bellinger };
2229c66ac9dbSNicholas Bellinger
22302eafd729SChristoph Hellwig TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
2231c66ac9dbSNicholas Bellinger
223291e2e39bSNicholas Bellinger /* End functions for struct config_item_type tb_dev_pr_cit */
2233c66ac9dbSNicholas Bellinger
223473112edcSNicholas Bellinger /* Start functions for struct config_item_type tb_dev_cit */
2235c66ac9dbSNicholas Bellinger
to_device(struct config_item * item)22362eafd729SChristoph Hellwig static inline struct se_device *to_device(struct config_item *item)
2237c66ac9dbSNicholas Bellinger {
22382eafd729SChristoph Hellwig return container_of(to_config_group(item), struct se_device, dev_group);
22392eafd729SChristoph Hellwig }
22402eafd729SChristoph Hellwig
target_dev_info_show(struct config_item * item,char * page)22412eafd729SChristoph Hellwig static ssize_t target_dev_info_show(struct config_item *item, char *page)
22422eafd729SChristoph Hellwig {
22432eafd729SChristoph Hellwig struct se_device *dev = to_device(item);
2244c66ac9dbSNicholas Bellinger int bl = 0;
2245c66ac9dbSNicholas Bellinger ssize_t read_bytes = 0;
2246c66ac9dbSNicholas Bellinger
22470fd97ccfSChristoph Hellwig transport_dump_dev_state(dev, page, &bl);
2248c66ac9dbSNicholas Bellinger read_bytes += bl;
22490a06d430SChristoph Hellwig read_bytes += dev->transport->show_configfs_dev_params(dev,
22500a06d430SChristoph Hellwig page+read_bytes);
2251c66ac9dbSNicholas Bellinger return read_bytes;
2252c66ac9dbSNicholas Bellinger }
2253c66ac9dbSNicholas Bellinger
target_dev_control_store(struct config_item * item,const char * page,size_t count)22542eafd729SChristoph Hellwig static ssize_t target_dev_control_store(struct config_item *item,
22552eafd729SChristoph Hellwig const char *page, size_t count)
2256c66ac9dbSNicholas Bellinger {
22572eafd729SChristoph Hellwig struct se_device *dev = to_device(item);
2258c66ac9dbSNicholas Bellinger
22590a06d430SChristoph Hellwig return dev->transport->set_configfs_dev_params(dev, page, count);
2260c66ac9dbSNicholas Bellinger }
2261c66ac9dbSNicholas Bellinger
target_dev_alias_show(struct config_item * item,char * page)22622eafd729SChristoph Hellwig static ssize_t target_dev_alias_show(struct config_item *item, char *page)
2263c66ac9dbSNicholas Bellinger {
22642eafd729SChristoph Hellwig struct se_device *dev = to_device(item);
2265c66ac9dbSNicholas Bellinger
22660fd97ccfSChristoph Hellwig if (!(dev->dev_flags & DF_USING_ALIAS))
2267c66ac9dbSNicholas Bellinger return 0;
2268c66ac9dbSNicholas Bellinger
22690fd97ccfSChristoph Hellwig return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
2270c66ac9dbSNicholas Bellinger }
2271c66ac9dbSNicholas Bellinger
target_dev_alias_store(struct config_item * item,const char * page,size_t count)22722eafd729SChristoph Hellwig static ssize_t target_dev_alias_store(struct config_item *item,
22732eafd729SChristoph Hellwig const char *page, size_t count)
2274c66ac9dbSNicholas Bellinger {
22752eafd729SChristoph Hellwig struct se_device *dev = to_device(item);
22760fd97ccfSChristoph Hellwig struct se_hba *hba = dev->se_hba;
2277c66ac9dbSNicholas Bellinger ssize_t read_bytes;
2278c66ac9dbSNicholas Bellinger
2279c66ac9dbSNicholas Bellinger if (count > (SE_DEV_ALIAS_LEN-1)) {
22806708bb27SAndy Grover pr_err("alias count: %d exceeds"
2281c66ac9dbSNicholas Bellinger " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
2282c66ac9dbSNicholas Bellinger SE_DEV_ALIAS_LEN-1);
2283c66ac9dbSNicholas Bellinger return -EINVAL;
2284c66ac9dbSNicholas Bellinger }
2285c66ac9dbSNicholas Bellinger
22860fd97ccfSChristoph Hellwig read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
22873011684cSDan Carpenter if (!read_bytes)
22883011684cSDan Carpenter return -EINVAL;
22890fd97ccfSChristoph Hellwig if (dev->dev_alias[read_bytes - 1] == '\n')
22900fd97ccfSChristoph Hellwig dev->dev_alias[read_bytes - 1] = '\0';
22910877eafdSSebastian Andrzej Siewior
22920fd97ccfSChristoph Hellwig dev->dev_flags |= DF_USING_ALIAS;
22933011684cSDan Carpenter
22946708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
2295c66ac9dbSNicholas Bellinger config_item_name(&hba->hba_group.cg_item),
22960fd97ccfSChristoph Hellwig config_item_name(&dev->dev_group.cg_item),
22970fd97ccfSChristoph Hellwig dev->dev_alias);
2298c66ac9dbSNicholas Bellinger
2299c66ac9dbSNicholas Bellinger return read_bytes;
2300c66ac9dbSNicholas Bellinger }
2301c66ac9dbSNicholas Bellinger
target_dev_udev_path_show(struct config_item * item,char * page)23022eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
2303c66ac9dbSNicholas Bellinger {
23042eafd729SChristoph Hellwig struct se_device *dev = to_device(item);
2305c66ac9dbSNicholas Bellinger
23060fd97ccfSChristoph Hellwig if (!(dev->dev_flags & DF_USING_UDEV_PATH))
2307c66ac9dbSNicholas Bellinger return 0;
2308c66ac9dbSNicholas Bellinger
23090fd97ccfSChristoph Hellwig return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
2310c66ac9dbSNicholas Bellinger }
2311c66ac9dbSNicholas Bellinger
target_dev_udev_path_store(struct config_item * item,const char * page,size_t count)23122eafd729SChristoph Hellwig static ssize_t target_dev_udev_path_store(struct config_item *item,
23132eafd729SChristoph Hellwig const char *page, size_t count)
2314c66ac9dbSNicholas Bellinger {
23152eafd729SChristoph Hellwig struct se_device *dev = to_device(item);
23160fd97ccfSChristoph Hellwig struct se_hba *hba = dev->se_hba;
2317c66ac9dbSNicholas Bellinger ssize_t read_bytes;
2318c66ac9dbSNicholas Bellinger
2319c66ac9dbSNicholas Bellinger if (count > (SE_UDEV_PATH_LEN-1)) {
23206708bb27SAndy Grover pr_err("udev_path count: %d exceeds"
2321c66ac9dbSNicholas Bellinger " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
2322c66ac9dbSNicholas Bellinger SE_UDEV_PATH_LEN-1);
2323c66ac9dbSNicholas Bellinger return -EINVAL;
2324c66ac9dbSNicholas Bellinger }
2325c66ac9dbSNicholas Bellinger
23260fd97ccfSChristoph Hellwig read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
2327c66ac9dbSNicholas Bellinger "%s", page);
23283011684cSDan Carpenter if (!read_bytes)
23293011684cSDan Carpenter return -EINVAL;
23300fd97ccfSChristoph Hellwig if (dev->udev_path[read_bytes - 1] == '\n')
23310fd97ccfSChristoph Hellwig dev->udev_path[read_bytes - 1] = '\0';
23320877eafdSSebastian Andrzej Siewior
23330fd97ccfSChristoph Hellwig dev->dev_flags |= DF_USING_UDEV_PATH;
23343011684cSDan Carpenter
23356708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
2336c66ac9dbSNicholas Bellinger config_item_name(&hba->hba_group.cg_item),
23370fd97ccfSChristoph Hellwig config_item_name(&dev->dev_group.cg_item),
23380fd97ccfSChristoph Hellwig dev->udev_path);
2339c66ac9dbSNicholas Bellinger
2340c66ac9dbSNicholas Bellinger return read_bytes;
2341c66ac9dbSNicholas Bellinger }
2342c66ac9dbSNicholas Bellinger
target_dev_enable_show(struct config_item * item,char * page)23432eafd729SChristoph Hellwig static ssize_t target_dev_enable_show(struct config_item *item, char *page)
234464146db7SAndy Grover {
23452eafd729SChristoph Hellwig struct se_device *dev = to_device(item);
234664146db7SAndy Grover
2347cb0f32e1SMike Christie return snprintf(page, PAGE_SIZE, "%d\n", target_dev_configured(dev));
234864146db7SAndy Grover }
234964146db7SAndy Grover
target_dev_enable_store(struct config_item * item,const char * page,size_t count)23502eafd729SChristoph Hellwig static ssize_t target_dev_enable_store(struct config_item *item,
23512eafd729SChristoph Hellwig const char *page, size_t count)
2352c66ac9dbSNicholas Bellinger {
23532eafd729SChristoph Hellwig struct se_device *dev = to_device(item);
2354c66ac9dbSNicholas Bellinger char *ptr;
23550fd97ccfSChristoph Hellwig int ret;
2356c66ac9dbSNicholas Bellinger
2357c66ac9dbSNicholas Bellinger ptr = strstr(page, "1");
23586708bb27SAndy Grover if (!ptr) {
23596708bb27SAndy Grover pr_err("For dev_enable ops, only valid value"
2360c66ac9dbSNicholas Bellinger " is \"1\"\n");
2361c66ac9dbSNicholas Bellinger return -EINVAL;
2362c66ac9dbSNicholas Bellinger }
2363c66ac9dbSNicholas Bellinger
23640fd97ccfSChristoph Hellwig ret = target_configure_device(dev);
23650fd97ccfSChristoph Hellwig if (ret)
23660fd97ccfSChristoph Hellwig return ret;
2367c66ac9dbSNicholas Bellinger return count;
2368c66ac9dbSNicholas Bellinger }
2369c66ac9dbSNicholas Bellinger
target_dev_alua_lu_gp_show(struct config_item * item,char * page)23702eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
2371c66ac9dbSNicholas Bellinger {
23722eafd729SChristoph Hellwig struct se_device *dev = to_device(item);
2373c66ac9dbSNicholas Bellinger struct config_item *lu_ci;
2374c66ac9dbSNicholas Bellinger struct t10_alua_lu_gp *lu_gp;
2375c66ac9dbSNicholas Bellinger struct t10_alua_lu_gp_member *lu_gp_mem;
2376c66ac9dbSNicholas Bellinger ssize_t len = 0;
2377c66ac9dbSNicholas Bellinger
2378c66ac9dbSNicholas Bellinger lu_gp_mem = dev->dev_alua_lu_gp_mem;
2379c87fbd56SChristoph Hellwig if (!lu_gp_mem)
2380c87fbd56SChristoph Hellwig return 0;
2381c66ac9dbSNicholas Bellinger
2382c66ac9dbSNicholas Bellinger spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2383c66ac9dbSNicholas Bellinger lu_gp = lu_gp_mem->lu_gp;
23846708bb27SAndy Grover if (lu_gp) {
2385c66ac9dbSNicholas Bellinger lu_ci = &lu_gp->lu_gp_group.cg_item;
2386c66ac9dbSNicholas Bellinger len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
2387c66ac9dbSNicholas Bellinger config_item_name(lu_ci), lu_gp->lu_gp_id);
2388c66ac9dbSNicholas Bellinger }
2389c66ac9dbSNicholas Bellinger spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2390c66ac9dbSNicholas Bellinger
2391c66ac9dbSNicholas Bellinger return len;
2392c66ac9dbSNicholas Bellinger }
2393c66ac9dbSNicholas Bellinger
target_dev_alua_lu_gp_store(struct config_item * item,const char * page,size_t count)23942eafd729SChristoph Hellwig static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
23952eafd729SChristoph Hellwig const char *page, size_t count)
2396c66ac9dbSNicholas Bellinger {
23972eafd729SChristoph Hellwig struct se_device *dev = to_device(item);
23980fd97ccfSChristoph Hellwig struct se_hba *hba = dev->se_hba;
2399c66ac9dbSNicholas Bellinger struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
2400c66ac9dbSNicholas Bellinger struct t10_alua_lu_gp_member *lu_gp_mem;
24012d4e2dafSChaitanya Kulkarni unsigned char buf[LU_GROUP_NAME_BUF] = { };
2402c66ac9dbSNicholas Bellinger int move = 0;
2403c66ac9dbSNicholas Bellinger
2404c87fbd56SChristoph Hellwig lu_gp_mem = dev->dev_alua_lu_gp_mem;
2405c87fbd56SChristoph Hellwig if (!lu_gp_mem)
24069105bfc0SAndy Grover return count;
2407c87fbd56SChristoph Hellwig
2408c66ac9dbSNicholas Bellinger if (count > LU_GROUP_NAME_BUF) {
24096708bb27SAndy Grover pr_err("ALUA LU Group Alias too large!\n");
2410c66ac9dbSNicholas Bellinger return -EINVAL;
2411c66ac9dbSNicholas Bellinger }
2412c66ac9dbSNicholas Bellinger memcpy(buf, page, count);
2413c66ac9dbSNicholas Bellinger /*
2414c66ac9dbSNicholas Bellinger * Any ALUA logical unit alias besides "NULL" means we will be
2415c66ac9dbSNicholas Bellinger * making a new group association.
2416c66ac9dbSNicholas Bellinger */
2417c66ac9dbSNicholas Bellinger if (strcmp(strstrip(buf), "NULL")) {
2418c66ac9dbSNicholas Bellinger /*
2419c66ac9dbSNicholas Bellinger * core_alua_get_lu_gp_by_name() will increment reference to
2420c66ac9dbSNicholas Bellinger * struct t10_alua_lu_gp. This reference is released with
2421c66ac9dbSNicholas Bellinger * core_alua_get_lu_gp_by_name below().
2422c66ac9dbSNicholas Bellinger */
2423c66ac9dbSNicholas Bellinger lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
24246708bb27SAndy Grover if (!lu_gp_new)
2425c66ac9dbSNicholas Bellinger return -ENODEV;
2426c66ac9dbSNicholas Bellinger }
2427c66ac9dbSNicholas Bellinger
2428c66ac9dbSNicholas Bellinger spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2429c66ac9dbSNicholas Bellinger lu_gp = lu_gp_mem->lu_gp;
24306708bb27SAndy Grover if (lu_gp) {
2431c66ac9dbSNicholas Bellinger /*
2432c66ac9dbSNicholas Bellinger * Clearing an existing lu_gp association, and replacing
2433c66ac9dbSNicholas Bellinger * with NULL
2434c66ac9dbSNicholas Bellinger */
24356708bb27SAndy Grover if (!lu_gp_new) {
24366708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
2437c66ac9dbSNicholas Bellinger " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2438c66ac9dbSNicholas Bellinger " %hu\n",
2439c66ac9dbSNicholas Bellinger config_item_name(&hba->hba_group.cg_item),
24400fd97ccfSChristoph Hellwig config_item_name(&dev->dev_group.cg_item),
2441c66ac9dbSNicholas Bellinger config_item_name(&lu_gp->lu_gp_group.cg_item),
2442c66ac9dbSNicholas Bellinger lu_gp->lu_gp_id);
2443c66ac9dbSNicholas Bellinger
2444c66ac9dbSNicholas Bellinger __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2445c66ac9dbSNicholas Bellinger spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2446c66ac9dbSNicholas Bellinger
2447c66ac9dbSNicholas Bellinger return count;
2448c66ac9dbSNicholas Bellinger }
2449c66ac9dbSNicholas Bellinger /*
2450c66ac9dbSNicholas Bellinger * Removing existing association of lu_gp_mem with lu_gp
2451c66ac9dbSNicholas Bellinger */
2452c66ac9dbSNicholas Bellinger __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2453c66ac9dbSNicholas Bellinger move = 1;
2454c66ac9dbSNicholas Bellinger }
2455c66ac9dbSNicholas Bellinger /*
2456c66ac9dbSNicholas Bellinger * Associate lu_gp_mem with lu_gp_new.
2457c66ac9dbSNicholas Bellinger */
2458c66ac9dbSNicholas Bellinger __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2459c66ac9dbSNicholas Bellinger spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2460c66ac9dbSNicholas Bellinger
24616708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
2462c66ac9dbSNicholas Bellinger " core/alua/lu_gps/%s, ID: %hu\n",
2463c66ac9dbSNicholas Bellinger (move) ? "Moving" : "Adding",
2464c66ac9dbSNicholas Bellinger config_item_name(&hba->hba_group.cg_item),
24650fd97ccfSChristoph Hellwig config_item_name(&dev->dev_group.cg_item),
2466c66ac9dbSNicholas Bellinger config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2467c66ac9dbSNicholas Bellinger lu_gp_new->lu_gp_id);
2468c66ac9dbSNicholas Bellinger
2469c66ac9dbSNicholas Bellinger core_alua_put_lu_gp_from_name(lu_gp_new);
2470c66ac9dbSNicholas Bellinger return count;
2471c66ac9dbSNicholas Bellinger }
2472c66ac9dbSNicholas Bellinger
target_dev_lba_map_show(struct config_item * item,char * page)24732eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
2474229d4f11SHannes Reinecke {
24752eafd729SChristoph Hellwig struct se_device *dev = to_device(item);
2476229d4f11SHannes Reinecke struct t10_alua_lba_map *map;
2477229d4f11SHannes Reinecke struct t10_alua_lba_map_member *mem;
2478229d4f11SHannes Reinecke char *b = page;
2479229d4f11SHannes Reinecke int bl = 0;
2480229d4f11SHannes Reinecke char state;
2481229d4f11SHannes Reinecke
2482229d4f11SHannes Reinecke spin_lock(&dev->t10_alua.lba_map_lock);
2483229d4f11SHannes Reinecke if (!list_empty(&dev->t10_alua.lba_map_list))
2484229d4f11SHannes Reinecke bl += sprintf(b + bl, "%u %u\n",
2485229d4f11SHannes Reinecke dev->t10_alua.lba_map_segment_size,
2486229d4f11SHannes Reinecke dev->t10_alua.lba_map_segment_multiplier);
2487229d4f11SHannes Reinecke list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2488229d4f11SHannes Reinecke bl += sprintf(b + bl, "%llu %llu",
2489229d4f11SHannes Reinecke map->lba_map_first_lba, map->lba_map_last_lba);
2490229d4f11SHannes Reinecke list_for_each_entry(mem, &map->lba_map_mem_list,
2491229d4f11SHannes Reinecke lba_map_mem_list) {
2492229d4f11SHannes Reinecke switch (mem->lba_map_mem_alua_state) {
2493229d4f11SHannes Reinecke case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2494229d4f11SHannes Reinecke state = 'O';
2495229d4f11SHannes Reinecke break;
2496229d4f11SHannes Reinecke case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2497229d4f11SHannes Reinecke state = 'A';
2498229d4f11SHannes Reinecke break;
2499229d4f11SHannes Reinecke case ALUA_ACCESS_STATE_STANDBY:
2500229d4f11SHannes Reinecke state = 'S';
2501229d4f11SHannes Reinecke break;
2502229d4f11SHannes Reinecke case ALUA_ACCESS_STATE_UNAVAILABLE:
2503229d4f11SHannes Reinecke state = 'U';
2504229d4f11SHannes Reinecke break;
2505229d4f11SHannes Reinecke default:
2506229d4f11SHannes Reinecke state = '.';
2507229d4f11SHannes Reinecke break;
2508229d4f11SHannes Reinecke }
2509229d4f11SHannes Reinecke bl += sprintf(b + bl, " %d:%c",
2510229d4f11SHannes Reinecke mem->lba_map_mem_alua_pg_id, state);
2511229d4f11SHannes Reinecke }
2512229d4f11SHannes Reinecke bl += sprintf(b + bl, "\n");
2513229d4f11SHannes Reinecke }
2514229d4f11SHannes Reinecke spin_unlock(&dev->t10_alua.lba_map_lock);
2515229d4f11SHannes Reinecke return bl;
2516229d4f11SHannes Reinecke }
2517229d4f11SHannes Reinecke
target_dev_lba_map_store(struct config_item * item,const char * page,size_t count)25182eafd729SChristoph Hellwig static ssize_t target_dev_lba_map_store(struct config_item *item,
25192eafd729SChristoph Hellwig const char *page, size_t count)
2520229d4f11SHannes Reinecke {
25212eafd729SChristoph Hellwig struct se_device *dev = to_device(item);
2522229d4f11SHannes Reinecke struct t10_alua_lba_map *lba_map = NULL;
2523229d4f11SHannes Reinecke struct list_head lba_list;
2524f0a8afecSBart Van Assche char *map_entries, *orig, *ptr;
2525229d4f11SHannes Reinecke char state;
2526229d4f11SHannes Reinecke int pg_num = -1, pg;
2527229d4f11SHannes Reinecke int ret = 0, num = 0, pg_id, alua_state;
2528229d4f11SHannes Reinecke unsigned long start_lba = -1, end_lba = -1;
2529229d4f11SHannes Reinecke unsigned long segment_size = -1, segment_mult = -1;
2530229d4f11SHannes Reinecke
2531f0a8afecSBart Van Assche orig = map_entries = kstrdup(page, GFP_KERNEL);
2532229d4f11SHannes Reinecke if (!map_entries)
2533229d4f11SHannes Reinecke return -ENOMEM;
2534229d4f11SHannes Reinecke
2535229d4f11SHannes Reinecke INIT_LIST_HEAD(&lba_list);
2536229d4f11SHannes Reinecke while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2537229d4f11SHannes Reinecke if (!*ptr)
2538229d4f11SHannes Reinecke continue;
2539229d4f11SHannes Reinecke
2540229d4f11SHannes Reinecke if (num == 0) {
2541229d4f11SHannes Reinecke if (sscanf(ptr, "%lu %lu\n",
2542229d4f11SHannes Reinecke &segment_size, &segment_mult) != 2) {
2543229d4f11SHannes Reinecke pr_err("Invalid line %d\n", num);
2544229d4f11SHannes Reinecke ret = -EINVAL;
2545229d4f11SHannes Reinecke break;
2546229d4f11SHannes Reinecke }
2547229d4f11SHannes Reinecke num++;
2548229d4f11SHannes Reinecke continue;
2549229d4f11SHannes Reinecke }
2550229d4f11SHannes Reinecke if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2551229d4f11SHannes Reinecke pr_err("Invalid line %d\n", num);
2552229d4f11SHannes Reinecke ret = -EINVAL;
2553229d4f11SHannes Reinecke break;
2554229d4f11SHannes Reinecke }
2555229d4f11SHannes Reinecke ptr = strchr(ptr, ' ');
2556229d4f11SHannes Reinecke if (!ptr) {
2557229d4f11SHannes Reinecke pr_err("Invalid line %d, missing end lba\n", num);
2558229d4f11SHannes Reinecke ret = -EINVAL;
2559229d4f11SHannes Reinecke break;
2560229d4f11SHannes Reinecke }
2561229d4f11SHannes Reinecke ptr++;
2562229d4f11SHannes Reinecke ptr = strchr(ptr, ' ');
2563229d4f11SHannes Reinecke if (!ptr) {
2564229d4f11SHannes Reinecke pr_err("Invalid line %d, missing state definitions\n",
2565229d4f11SHannes Reinecke num);
2566229d4f11SHannes Reinecke ret = -EINVAL;
2567229d4f11SHannes Reinecke break;
2568229d4f11SHannes Reinecke }
2569229d4f11SHannes Reinecke ptr++;
2570229d4f11SHannes Reinecke lba_map = core_alua_allocate_lba_map(&lba_list,
2571229d4f11SHannes Reinecke start_lba, end_lba);
2572229d4f11SHannes Reinecke if (IS_ERR(lba_map)) {
2573229d4f11SHannes Reinecke ret = PTR_ERR(lba_map);
2574229d4f11SHannes Reinecke break;
2575229d4f11SHannes Reinecke }
2576229d4f11SHannes Reinecke pg = 0;
2577229d4f11SHannes Reinecke while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2578229d4f11SHannes Reinecke switch (state) {
2579229d4f11SHannes Reinecke case 'O':
2580229d4f11SHannes Reinecke alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2581229d4f11SHannes Reinecke break;
2582229d4f11SHannes Reinecke case 'A':
2583229d4f11SHannes Reinecke alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2584229d4f11SHannes Reinecke break;
2585229d4f11SHannes Reinecke case 'S':
2586229d4f11SHannes Reinecke alua_state = ALUA_ACCESS_STATE_STANDBY;
2587229d4f11SHannes Reinecke break;
2588229d4f11SHannes Reinecke case 'U':
2589229d4f11SHannes Reinecke alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2590229d4f11SHannes Reinecke break;
2591229d4f11SHannes Reinecke default:
2592229d4f11SHannes Reinecke pr_err("Invalid ALUA state '%c'\n", state);
2593229d4f11SHannes Reinecke ret = -EINVAL;
2594229d4f11SHannes Reinecke goto out;
2595229d4f11SHannes Reinecke }
2596229d4f11SHannes Reinecke
2597229d4f11SHannes Reinecke ret = core_alua_allocate_lba_map_mem(lba_map,
2598229d4f11SHannes Reinecke pg_id, alua_state);
2599229d4f11SHannes Reinecke if (ret) {
2600229d4f11SHannes Reinecke pr_err("Invalid target descriptor %d:%c "
2601229d4f11SHannes Reinecke "at line %d\n",
2602229d4f11SHannes Reinecke pg_id, state, num);
2603229d4f11SHannes Reinecke break;
2604229d4f11SHannes Reinecke }
2605229d4f11SHannes Reinecke pg++;
2606229d4f11SHannes Reinecke ptr = strchr(ptr, ' ');
2607229d4f11SHannes Reinecke if (ptr)
2608229d4f11SHannes Reinecke ptr++;
2609229d4f11SHannes Reinecke else
2610229d4f11SHannes Reinecke break;
2611229d4f11SHannes Reinecke }
2612229d4f11SHannes Reinecke if (pg_num == -1)
2613229d4f11SHannes Reinecke pg_num = pg;
2614229d4f11SHannes Reinecke else if (pg != pg_num) {
2615229d4f11SHannes Reinecke pr_err("Only %d from %d port groups definitions "
2616229d4f11SHannes Reinecke "at line %d\n", pg, pg_num, num);
2617229d4f11SHannes Reinecke ret = -EINVAL;
2618229d4f11SHannes Reinecke break;
2619229d4f11SHannes Reinecke }
2620229d4f11SHannes Reinecke num++;
2621229d4f11SHannes Reinecke }
2622229d4f11SHannes Reinecke out:
2623229d4f11SHannes Reinecke if (ret) {
2624229d4f11SHannes Reinecke core_alua_free_lba_map(&lba_list);
2625229d4f11SHannes Reinecke count = ret;
2626229d4f11SHannes Reinecke } else
2627229d4f11SHannes Reinecke core_alua_set_lba_map(dev, &lba_list,
2628229d4f11SHannes Reinecke segment_size, segment_mult);
2629f0a8afecSBart Van Assche kfree(orig);
2630229d4f11SHannes Reinecke return count;
2631229d4f11SHannes Reinecke }
2632229d4f11SHannes Reinecke
26332eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_dev_, info);
26342eafd729SChristoph Hellwig CONFIGFS_ATTR_WO(target_dev_, control);
26352eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alias);
26362eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, udev_path);
26372eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, enable);
26382eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, alua_lu_gp);
26392eafd729SChristoph Hellwig CONFIGFS_ATTR(target_dev_, lba_map);
2640229d4f11SHannes Reinecke
264173112edcSNicholas Bellinger static struct configfs_attribute *target_core_dev_attrs[] = {
26422eafd729SChristoph Hellwig &target_dev_attr_info,
26432eafd729SChristoph Hellwig &target_dev_attr_control,
26442eafd729SChristoph Hellwig &target_dev_attr_alias,
26452eafd729SChristoph Hellwig &target_dev_attr_udev_path,
26462eafd729SChristoph Hellwig &target_dev_attr_enable,
26472eafd729SChristoph Hellwig &target_dev_attr_alua_lu_gp,
26482eafd729SChristoph Hellwig &target_dev_attr_lba_map,
2649c66ac9dbSNicholas Bellinger NULL,
2650c66ac9dbSNicholas Bellinger };
2651c66ac9dbSNicholas Bellinger
target_core_dev_release(struct config_item * item)2652c66ac9dbSNicholas Bellinger static void target_core_dev_release(struct config_item *item)
2653c66ac9dbSNicholas Bellinger {
26540fd97ccfSChristoph Hellwig struct config_group *dev_cg = to_config_group(item);
26550fd97ccfSChristoph Hellwig struct se_device *dev =
26560fd97ccfSChristoph Hellwig container_of(dev_cg, struct se_device, dev_group);
2657c66ac9dbSNicholas Bellinger
26580fd97ccfSChristoph Hellwig target_free_device(dev);
2659c66ac9dbSNicholas Bellinger }
2660c66ac9dbSNicholas Bellinger
2661c17cd249SNicholas Bellinger /*
2662c17cd249SNicholas Bellinger * Used in target_core_fabric_configfs.c to verify valid se_device symlink
2663c17cd249SNicholas Bellinger * within target_fabric_port_link()
2664c17cd249SNicholas Bellinger */
2665c17cd249SNicholas Bellinger struct configfs_item_operations target_core_dev_item_ops = {
2666c66ac9dbSNicholas Bellinger .release = target_core_dev_release,
2667c66ac9dbSNicholas Bellinger };
2668c66ac9dbSNicholas Bellinger
266973112edcSNicholas Bellinger TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2670c66ac9dbSNicholas Bellinger
267173112edcSNicholas Bellinger /* End functions for struct config_item_type tb_dev_cit */
2672c66ac9dbSNicholas Bellinger
2673c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2674c66ac9dbSNicholas Bellinger
to_lu_gp(struct config_item * item)26752eafd729SChristoph Hellwig static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2676c66ac9dbSNicholas Bellinger {
26772eafd729SChristoph Hellwig return container_of(to_config_group(item), struct t10_alua_lu_gp,
26782eafd729SChristoph Hellwig lu_gp_group);
26792eafd729SChristoph Hellwig }
26802eafd729SChristoph Hellwig
target_lu_gp_lu_gp_id_show(struct config_item * item,char * page)26812eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
26822eafd729SChristoph Hellwig {
26832eafd729SChristoph Hellwig struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
26842eafd729SChristoph Hellwig
26856708bb27SAndy Grover if (!lu_gp->lu_gp_valid_id)
2686c66ac9dbSNicholas Bellinger return 0;
2687c66ac9dbSNicholas Bellinger return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2688c66ac9dbSNicholas Bellinger }
2689c66ac9dbSNicholas Bellinger
target_lu_gp_lu_gp_id_store(struct config_item * item,const char * page,size_t count)26902eafd729SChristoph Hellwig static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
26912eafd729SChristoph Hellwig const char *page, size_t count)
2692c66ac9dbSNicholas Bellinger {
26932eafd729SChristoph Hellwig struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2694c66ac9dbSNicholas Bellinger struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2695c66ac9dbSNicholas Bellinger unsigned long lu_gp_id;
2696c66ac9dbSNicholas Bellinger int ret;
2697c66ac9dbSNicholas Bellinger
269857103d7fSJingoo Han ret = kstrtoul(page, 0, &lu_gp_id);
2699c66ac9dbSNicholas Bellinger if (ret < 0) {
270057103d7fSJingoo Han pr_err("kstrtoul() returned %d for"
2701c66ac9dbSNicholas Bellinger " lu_gp_id\n", ret);
270257103d7fSJingoo Han return ret;
2703c66ac9dbSNicholas Bellinger }
2704c66ac9dbSNicholas Bellinger if (lu_gp_id > 0x0000ffff) {
27056708bb27SAndy Grover pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2706c66ac9dbSNicholas Bellinger " 0x0000ffff\n", lu_gp_id);
2707c66ac9dbSNicholas Bellinger return -EINVAL;
2708c66ac9dbSNicholas Bellinger }
2709c66ac9dbSNicholas Bellinger
2710c66ac9dbSNicholas Bellinger ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2711c66ac9dbSNicholas Bellinger if (ret < 0)
2712c66ac9dbSNicholas Bellinger return -EINVAL;
2713c66ac9dbSNicholas Bellinger
27146708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2715c66ac9dbSNicholas Bellinger " Group: core/alua/lu_gps/%s to ID: %hu\n",
2716c66ac9dbSNicholas Bellinger config_item_name(&alua_lu_gp_cg->cg_item),
2717c66ac9dbSNicholas Bellinger lu_gp->lu_gp_id);
2718c66ac9dbSNicholas Bellinger
2719c66ac9dbSNicholas Bellinger return count;
2720c66ac9dbSNicholas Bellinger }
2721c66ac9dbSNicholas Bellinger
target_lu_gp_members_show(struct config_item * item,char * page)27222eafd729SChristoph Hellwig static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2723c66ac9dbSNicholas Bellinger {
27242eafd729SChristoph Hellwig struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2725c66ac9dbSNicholas Bellinger struct se_device *dev;
2726c66ac9dbSNicholas Bellinger struct se_hba *hba;
2727c66ac9dbSNicholas Bellinger struct t10_alua_lu_gp_member *lu_gp_mem;
2728c66ac9dbSNicholas Bellinger ssize_t len = 0, cur_len;
27292d4e2dafSChaitanya Kulkarni unsigned char buf[LU_GROUP_NAME_BUF] = { };
2730c66ac9dbSNicholas Bellinger
2731c66ac9dbSNicholas Bellinger spin_lock(&lu_gp->lu_gp_lock);
2732c66ac9dbSNicholas Bellinger list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2733c66ac9dbSNicholas Bellinger dev = lu_gp_mem->lu_gp_mem_dev;
27340fd97ccfSChristoph Hellwig hba = dev->se_hba;
2735c66ac9dbSNicholas Bellinger
2736c66ac9dbSNicholas Bellinger cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2737c66ac9dbSNicholas Bellinger config_item_name(&hba->hba_group.cg_item),
27380fd97ccfSChristoph Hellwig config_item_name(&dev->dev_group.cg_item));
2739c66ac9dbSNicholas Bellinger cur_len++; /* Extra byte for NULL terminator */
2740c66ac9dbSNicholas Bellinger
2741c66ac9dbSNicholas Bellinger if ((cur_len + len) > PAGE_SIZE) {
27426708bb27SAndy Grover pr_warn("Ran out of lu_gp_show_attr"
2743c66ac9dbSNicholas Bellinger "_members buffer\n");
2744c66ac9dbSNicholas Bellinger break;
2745c66ac9dbSNicholas Bellinger }
2746c66ac9dbSNicholas Bellinger memcpy(page+len, buf, cur_len);
2747c66ac9dbSNicholas Bellinger len += cur_len;
2748c66ac9dbSNicholas Bellinger }
2749c66ac9dbSNicholas Bellinger spin_unlock(&lu_gp->lu_gp_lock);
2750c66ac9dbSNicholas Bellinger
2751c66ac9dbSNicholas Bellinger return len;
2752c66ac9dbSNicholas Bellinger }
2753c66ac9dbSNicholas Bellinger
27542eafd729SChristoph Hellwig CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
27552eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_lu_gp_, members);
2756c66ac9dbSNicholas Bellinger
2757c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
27582eafd729SChristoph Hellwig &target_lu_gp_attr_lu_gp_id,
27592eafd729SChristoph Hellwig &target_lu_gp_attr_members,
2760c66ac9dbSNicholas Bellinger NULL,
2761c66ac9dbSNicholas Bellinger };
2762c66ac9dbSNicholas Bellinger
target_core_alua_lu_gp_release(struct config_item * item)27631f6fe7cbSNicholas Bellinger static void target_core_alua_lu_gp_release(struct config_item *item)
27641f6fe7cbSNicholas Bellinger {
27651f6fe7cbSNicholas Bellinger struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
27661f6fe7cbSNicholas Bellinger struct t10_alua_lu_gp, lu_gp_group);
27671f6fe7cbSNicholas Bellinger
27681f6fe7cbSNicholas Bellinger core_alua_free_lu_gp(lu_gp);
27691f6fe7cbSNicholas Bellinger }
27701f6fe7cbSNicholas Bellinger
2771c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_lu_gp_ops = {
27721f6fe7cbSNicholas Bellinger .release = target_core_alua_lu_gp_release,
2773c66ac9dbSNicholas Bellinger };
2774c66ac9dbSNicholas Bellinger
2775ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_lu_gp_cit = {
2776c66ac9dbSNicholas Bellinger .ct_item_ops = &target_core_alua_lu_gp_ops,
2777c66ac9dbSNicholas Bellinger .ct_attrs = target_core_alua_lu_gp_attrs,
2778c66ac9dbSNicholas Bellinger .ct_owner = THIS_MODULE,
2779c66ac9dbSNicholas Bellinger };
2780c66ac9dbSNicholas Bellinger
2781c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2782c66ac9dbSNicholas Bellinger
2783c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2784c66ac9dbSNicholas Bellinger
target_core_alua_create_lu_gp(struct config_group * group,const char * name)2785c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_lu_gp(
2786c66ac9dbSNicholas Bellinger struct config_group *group,
2787c66ac9dbSNicholas Bellinger const char *name)
2788c66ac9dbSNicholas Bellinger {
2789c66ac9dbSNicholas Bellinger struct t10_alua_lu_gp *lu_gp;
2790c66ac9dbSNicholas Bellinger struct config_group *alua_lu_gp_cg = NULL;
2791c66ac9dbSNicholas Bellinger struct config_item *alua_lu_gp_ci = NULL;
2792c66ac9dbSNicholas Bellinger
2793c66ac9dbSNicholas Bellinger lu_gp = core_alua_allocate_lu_gp(name, 0);
2794c66ac9dbSNicholas Bellinger if (IS_ERR(lu_gp))
2795c66ac9dbSNicholas Bellinger return NULL;
2796c66ac9dbSNicholas Bellinger
2797c66ac9dbSNicholas Bellinger alua_lu_gp_cg = &lu_gp->lu_gp_group;
2798c66ac9dbSNicholas Bellinger alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2799c66ac9dbSNicholas Bellinger
2800c66ac9dbSNicholas Bellinger config_group_init_type_name(alua_lu_gp_cg, name,
2801c66ac9dbSNicholas Bellinger &target_core_alua_lu_gp_cit);
2802c66ac9dbSNicholas Bellinger
28036708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2804c66ac9dbSNicholas Bellinger " Group: core/alua/lu_gps/%s\n",
2805c66ac9dbSNicholas Bellinger config_item_name(alua_lu_gp_ci));
2806c66ac9dbSNicholas Bellinger
2807c66ac9dbSNicholas Bellinger return alua_lu_gp_cg;
2808c66ac9dbSNicholas Bellinger
2809c66ac9dbSNicholas Bellinger }
2810c66ac9dbSNicholas Bellinger
target_core_alua_drop_lu_gp(struct config_group * group,struct config_item * item)2811c66ac9dbSNicholas Bellinger static void target_core_alua_drop_lu_gp(
2812c66ac9dbSNicholas Bellinger struct config_group *group,
2813c66ac9dbSNicholas Bellinger struct config_item *item)
2814c66ac9dbSNicholas Bellinger {
2815c66ac9dbSNicholas Bellinger struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2816c66ac9dbSNicholas Bellinger struct t10_alua_lu_gp, lu_gp_group);
2817c66ac9dbSNicholas Bellinger
28186708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2819c66ac9dbSNicholas Bellinger " Group: core/alua/lu_gps/%s, ID: %hu\n",
2820c66ac9dbSNicholas Bellinger config_item_name(item), lu_gp->lu_gp_id);
28211f6fe7cbSNicholas Bellinger /*
28221f6fe7cbSNicholas Bellinger * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
28231f6fe7cbSNicholas Bellinger * -> target_core_alua_lu_gp_release()
28241f6fe7cbSNicholas Bellinger */
2825c66ac9dbSNicholas Bellinger config_item_put(item);
2826c66ac9dbSNicholas Bellinger }
2827c66ac9dbSNicholas Bellinger
2828c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2829c66ac9dbSNicholas Bellinger .make_group = &target_core_alua_create_lu_gp,
2830c66ac9dbSNicholas Bellinger .drop_item = &target_core_alua_drop_lu_gp,
2831c66ac9dbSNicholas Bellinger };
2832c66ac9dbSNicholas Bellinger
2833ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_lu_gps_cit = {
2834c66ac9dbSNicholas Bellinger .ct_item_ops = NULL,
2835c66ac9dbSNicholas Bellinger .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2836c66ac9dbSNicholas Bellinger .ct_owner = THIS_MODULE,
2837c66ac9dbSNicholas Bellinger };
2838c66ac9dbSNicholas Bellinger
2839c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2840c66ac9dbSNicholas Bellinger
2841c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2842c66ac9dbSNicholas Bellinger
to_tg_pt_gp(struct config_item * item)28432eafd729SChristoph Hellwig static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
28442eafd729SChristoph Hellwig {
28452eafd729SChristoph Hellwig return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
28462eafd729SChristoph Hellwig tg_pt_gp_group);
28472eafd729SChristoph Hellwig }
2848c66ac9dbSNicholas Bellinger
target_tg_pt_gp_alua_access_state_show(struct config_item * item,char * page)28492eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2850c66ac9dbSNicholas Bellinger char *page)
2851c66ac9dbSNicholas Bellinger {
2852c66ac9dbSNicholas Bellinger return sprintf(page, "%d\n",
2853d19c4643SMike Christie to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
2854c66ac9dbSNicholas Bellinger }
2855c66ac9dbSNicholas Bellinger
target_tg_pt_gp_alua_access_state_store(struct config_item * item,const char * page,size_t count)28562eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
28572eafd729SChristoph Hellwig const char *page, size_t count)
2858c66ac9dbSNicholas Bellinger {
28592eafd729SChristoph Hellwig struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
28600fd97ccfSChristoph Hellwig struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2861c66ac9dbSNicholas Bellinger unsigned long tmp;
2862c66ac9dbSNicholas Bellinger int new_state, ret;
2863c66ac9dbSNicholas Bellinger
28646708bb27SAndy Grover if (!tg_pt_gp->tg_pt_gp_valid_id) {
2865baa75afdSBart Van Assche pr_err("Unable to do implicit ALUA on invalid tg_pt_gp ID\n");
2866c66ac9dbSNicholas Bellinger return -EINVAL;
2867c66ac9dbSNicholas Bellinger }
2868cb0f32e1SMike Christie if (!target_dev_configured(dev)) {
2869f1453773SNicholas Bellinger pr_err("Unable to set alua_access_state while device is"
2870f1453773SNicholas Bellinger " not configured\n");
2871f1453773SNicholas Bellinger return -ENODEV;
2872f1453773SNicholas Bellinger }
2873c66ac9dbSNicholas Bellinger
287457103d7fSJingoo Han ret = kstrtoul(page, 0, &tmp);
2875c66ac9dbSNicholas Bellinger if (ret < 0) {
28766708bb27SAndy Grover pr_err("Unable to extract new ALUA access state from"
2877c66ac9dbSNicholas Bellinger " %s\n", page);
287857103d7fSJingoo Han return ret;
2879c66ac9dbSNicholas Bellinger }
2880c66ac9dbSNicholas Bellinger new_state = (int)tmp;
2881c66ac9dbSNicholas Bellinger
2882125d0119SHannes Reinecke if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2883125d0119SHannes Reinecke pr_err("Unable to process implicit configfs ALUA"
2884125d0119SHannes Reinecke " transition while TPGS_IMPLICIT_ALUA is disabled\n");
2885c66ac9dbSNicholas Bellinger return -EINVAL;
2886c66ac9dbSNicholas Bellinger }
2887c66094bfSHannes Reinecke if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2888c66094bfSHannes Reinecke new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2889c66094bfSHannes Reinecke /* LBA DEPENDENT is only allowed with implicit ALUA */
2890c66094bfSHannes Reinecke pr_err("Unable to process implicit configfs ALUA transition"
2891c66094bfSHannes Reinecke " while explicit ALUA management is enabled\n");
2892c66094bfSHannes Reinecke return -EINVAL;
2893c66094bfSHannes Reinecke }
2894c66ac9dbSNicholas Bellinger
28950fd97ccfSChristoph Hellwig ret = core_alua_do_port_transition(tg_pt_gp, dev,
2896c66ac9dbSNicholas Bellinger NULL, NULL, new_state, 0);
2897c66ac9dbSNicholas Bellinger return (!ret) ? count : -EINVAL;
2898c66ac9dbSNicholas Bellinger }
2899c66ac9dbSNicholas Bellinger
target_tg_pt_gp_alua_access_status_show(struct config_item * item,char * page)29002eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2901c66ac9dbSNicholas Bellinger char *page)
2902c66ac9dbSNicholas Bellinger {
29032eafd729SChristoph Hellwig struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2904c66ac9dbSNicholas Bellinger return sprintf(page, "%s\n",
2905c66ac9dbSNicholas Bellinger core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2906c66ac9dbSNicholas Bellinger }
2907c66ac9dbSNicholas Bellinger
target_tg_pt_gp_alua_access_status_store(struct config_item * item,const char * page,size_t count)29082eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_status_store(
29092eafd729SChristoph Hellwig struct config_item *item, const char *page, size_t count)
2910c66ac9dbSNicholas Bellinger {
29112eafd729SChristoph Hellwig struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2912c66ac9dbSNicholas Bellinger unsigned long tmp;
2913c66ac9dbSNicholas Bellinger int new_status, ret;
2914c66ac9dbSNicholas Bellinger
29156708bb27SAndy Grover if (!tg_pt_gp->tg_pt_gp_valid_id) {
2916baa75afdSBart Van Assche pr_err("Unable to set ALUA access status on invalid tg_pt_gp ID\n");
2917c66ac9dbSNicholas Bellinger return -EINVAL;
2918c66ac9dbSNicholas Bellinger }
2919c66ac9dbSNicholas Bellinger
292057103d7fSJingoo Han ret = kstrtoul(page, 0, &tmp);
2921c66ac9dbSNicholas Bellinger if (ret < 0) {
29226708bb27SAndy Grover pr_err("Unable to extract new ALUA access status"
2923c66ac9dbSNicholas Bellinger " from %s\n", page);
292457103d7fSJingoo Han return ret;
2925c66ac9dbSNicholas Bellinger }
2926c66ac9dbSNicholas Bellinger new_status = (int)tmp;
2927c66ac9dbSNicholas Bellinger
2928c66ac9dbSNicholas Bellinger if ((new_status != ALUA_STATUS_NONE) &&
2929125d0119SHannes Reinecke (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2930125d0119SHannes Reinecke (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
29316708bb27SAndy Grover pr_err("Illegal ALUA access status: 0x%02x\n",
2932c66ac9dbSNicholas Bellinger new_status);
2933c66ac9dbSNicholas Bellinger return -EINVAL;
2934c66ac9dbSNicholas Bellinger }
2935c66ac9dbSNicholas Bellinger
2936c66ac9dbSNicholas Bellinger tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2937c66ac9dbSNicholas Bellinger return count;
2938c66ac9dbSNicholas Bellinger }
2939c66ac9dbSNicholas Bellinger
target_tg_pt_gp_alua_access_type_show(struct config_item * item,char * page)29402eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2941c66ac9dbSNicholas Bellinger char *page)
2942c66ac9dbSNicholas Bellinger {
29432eafd729SChristoph Hellwig return core_alua_show_access_type(to_tg_pt_gp(item), page);
2944c66ac9dbSNicholas Bellinger }
2945c66ac9dbSNicholas Bellinger
target_tg_pt_gp_alua_access_type_store(struct config_item * item,const char * page,size_t count)29462eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
29472eafd729SChristoph Hellwig const char *page, size_t count)
2948c66ac9dbSNicholas Bellinger {
29492eafd729SChristoph Hellwig return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2950c66ac9dbSNicholas Bellinger }
2951c66ac9dbSNicholas Bellinger
29522eafd729SChristoph Hellwig #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit) \
29532eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_show( \
29542eafd729SChristoph Hellwig struct config_item *item, char *p) \
2955b0a382c5SHannes Reinecke { \
29562eafd729SChristoph Hellwig struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
29572eafd729SChristoph Hellwig return sprintf(p, "%d\n", \
29582eafd729SChristoph Hellwig !!(t->tg_pt_gp_alua_supported_states & _bit)); \
29592eafd729SChristoph Hellwig } \
29602eafd729SChristoph Hellwig \
29612eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_support_##_name##_store( \
29622eafd729SChristoph Hellwig struct config_item *item, const char *p, size_t c) \
2963b0a382c5SHannes Reinecke { \
29642eafd729SChristoph Hellwig struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
2965b0a382c5SHannes Reinecke unsigned long tmp; \
2966b0a382c5SHannes Reinecke int ret; \
2967b0a382c5SHannes Reinecke \
2968b0a382c5SHannes Reinecke if (!t->tg_pt_gp_valid_id) { \
2969baa75afdSBart Van Assche pr_err("Unable to set " #_name " ALUA state on invalid tg_pt_gp ID\n"); \
2970b0a382c5SHannes Reinecke return -EINVAL; \
2971b0a382c5SHannes Reinecke } \
2972b0a382c5SHannes Reinecke \
2973b0a382c5SHannes Reinecke ret = kstrtoul(p, 0, &tmp); \
2974b0a382c5SHannes Reinecke if (ret < 0) { \
2975b0a382c5SHannes Reinecke pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2976b0a382c5SHannes Reinecke return -EINVAL; \
2977b0a382c5SHannes Reinecke } \
2978b0a382c5SHannes Reinecke if (tmp > 1) { \
2979b0a382c5SHannes Reinecke pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2980b0a382c5SHannes Reinecke return -EINVAL; \
2981b0a382c5SHannes Reinecke } \
29821f0b030cSSebastian Herbszt if (tmp) \
29832eafd729SChristoph Hellwig t->tg_pt_gp_alua_supported_states |= _bit; \
2984b0a382c5SHannes Reinecke else \
29852eafd729SChristoph Hellwig t->tg_pt_gp_alua_supported_states &= ~_bit; \
2986b0a382c5SHannes Reinecke \
2987b0a382c5SHannes Reinecke return c; \
29886be526c4SHannes Reinecke }
29896be526c4SHannes Reinecke
29902eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
29912eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
29922eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
29932eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
29942eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
29952eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
29962eafd729SChristoph Hellwig ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
29976be526c4SHannes Reinecke
target_tg_pt_gp_alua_write_metadata_show(struct config_item * item,char * page)29982eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_show(
29992eafd729SChristoph Hellwig struct config_item *item, char *page)
3000c66ac9dbSNicholas Bellinger {
30012eafd729SChristoph Hellwig return sprintf(page, "%d\n",
30022eafd729SChristoph Hellwig to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
3003c66ac9dbSNicholas Bellinger }
3004c66ac9dbSNicholas Bellinger
target_tg_pt_gp_alua_write_metadata_store(struct config_item * item,const char * page,size_t count)30052eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_alua_write_metadata_store(
30062eafd729SChristoph Hellwig struct config_item *item, const char *page, size_t count)
3007c66ac9dbSNicholas Bellinger {
30082eafd729SChristoph Hellwig struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3009c66ac9dbSNicholas Bellinger unsigned long tmp;
3010c66ac9dbSNicholas Bellinger int ret;
3011c66ac9dbSNicholas Bellinger
301257103d7fSJingoo Han ret = kstrtoul(page, 0, &tmp);
3013c66ac9dbSNicholas Bellinger if (ret < 0) {
30146708bb27SAndy Grover pr_err("Unable to extract alua_write_metadata\n");
301557103d7fSJingoo Han return ret;
3016c66ac9dbSNicholas Bellinger }
3017c66ac9dbSNicholas Bellinger
3018c66ac9dbSNicholas Bellinger if ((tmp != 0) && (tmp != 1)) {
30196708bb27SAndy Grover pr_err("Illegal value for alua_write_metadata:"
3020c66ac9dbSNicholas Bellinger " %lu\n", tmp);
3021c66ac9dbSNicholas Bellinger return -EINVAL;
3022c66ac9dbSNicholas Bellinger }
3023c66ac9dbSNicholas Bellinger tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
3024c66ac9dbSNicholas Bellinger
3025c66ac9dbSNicholas Bellinger return count;
3026c66ac9dbSNicholas Bellinger }
3027c66ac9dbSNicholas Bellinger
target_tg_pt_gp_nonop_delay_msecs_show(struct config_item * item,char * page)30282eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
3029c66ac9dbSNicholas Bellinger char *page)
3030c66ac9dbSNicholas Bellinger {
30312eafd729SChristoph Hellwig return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
3032c66ac9dbSNicholas Bellinger }
3033c66ac9dbSNicholas Bellinger
target_tg_pt_gp_nonop_delay_msecs_store(struct config_item * item,const char * page,size_t count)30342eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
30352eafd729SChristoph Hellwig const char *page, size_t count)
3036c66ac9dbSNicholas Bellinger {
30372eafd729SChristoph Hellwig return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
30382eafd729SChristoph Hellwig count);
3039c66ac9dbSNicholas Bellinger }
3040c66ac9dbSNicholas Bellinger
target_tg_pt_gp_trans_delay_msecs_show(struct config_item * item,char * page)30412eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
3042c66ac9dbSNicholas Bellinger char *page)
3043c66ac9dbSNicholas Bellinger {
30442eafd729SChristoph Hellwig return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
3045c66ac9dbSNicholas Bellinger }
3046c66ac9dbSNicholas Bellinger
target_tg_pt_gp_trans_delay_msecs_store(struct config_item * item,const char * page,size_t count)30472eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
30482eafd729SChristoph Hellwig const char *page, size_t count)
3049c66ac9dbSNicholas Bellinger {
30502eafd729SChristoph Hellwig return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
30512eafd729SChristoph Hellwig count);
3052c66ac9dbSNicholas Bellinger }
3053c66ac9dbSNicholas Bellinger
target_tg_pt_gp_implicit_trans_secs_show(struct config_item * item,char * page)30542eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
30552eafd729SChristoph Hellwig struct config_item *item, char *page)
30562eafd729SChristoph Hellwig {
30572eafd729SChristoph Hellwig return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
30582eafd729SChristoph Hellwig }
3059c66ac9dbSNicholas Bellinger
target_tg_pt_gp_implicit_trans_secs_store(struct config_item * item,const char * page,size_t count)30602eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
30612eafd729SChristoph Hellwig struct config_item *item, const char *page, size_t count)
30622eafd729SChristoph Hellwig {
30632eafd729SChristoph Hellwig return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
30642eafd729SChristoph Hellwig count);
30652eafd729SChristoph Hellwig }
30662eafd729SChristoph Hellwig
target_tg_pt_gp_preferred_show(struct config_item * item,char * page)30672eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
30685b9a4d72SNicholas Bellinger char *page)
30695b9a4d72SNicholas Bellinger {
30702eafd729SChristoph Hellwig return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
30715b9a4d72SNicholas Bellinger }
30725b9a4d72SNicholas Bellinger
target_tg_pt_gp_preferred_store(struct config_item * item,const char * page,size_t count)30732eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
30742eafd729SChristoph Hellwig const char *page, size_t count)
30755b9a4d72SNicholas Bellinger {
30762eafd729SChristoph Hellwig return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
30775b9a4d72SNicholas Bellinger }
30785b9a4d72SNicholas Bellinger
target_tg_pt_gp_tg_pt_gp_id_show(struct config_item * item,char * page)30792eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
3080c66ac9dbSNicholas Bellinger char *page)
3081c66ac9dbSNicholas Bellinger {
30822eafd729SChristoph Hellwig struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3083c66ac9dbSNicholas Bellinger
30846708bb27SAndy Grover if (!tg_pt_gp->tg_pt_gp_valid_id)
3085c66ac9dbSNicholas Bellinger return 0;
3086c66ac9dbSNicholas Bellinger return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
3087c66ac9dbSNicholas Bellinger }
3088c66ac9dbSNicholas Bellinger
target_tg_pt_gp_tg_pt_gp_id_store(struct config_item * item,const char * page,size_t count)30892eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
30902eafd729SChristoph Hellwig const char *page, size_t count)
3091c66ac9dbSNicholas Bellinger {
30922eafd729SChristoph Hellwig struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3093c66ac9dbSNicholas Bellinger struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3094c66ac9dbSNicholas Bellinger unsigned long tg_pt_gp_id;
3095c66ac9dbSNicholas Bellinger int ret;
3096c66ac9dbSNicholas Bellinger
309757103d7fSJingoo Han ret = kstrtoul(page, 0, &tg_pt_gp_id);
3098c66ac9dbSNicholas Bellinger if (ret < 0) {
30993d035237SHannes Reinecke pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
31003d035237SHannes Reinecke page);
310157103d7fSJingoo Han return ret;
3102c66ac9dbSNicholas Bellinger }
3103c66ac9dbSNicholas Bellinger if (tg_pt_gp_id > 0x0000ffff) {
31043d035237SHannes Reinecke pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
31053d035237SHannes Reinecke tg_pt_gp_id);
3106c66ac9dbSNicholas Bellinger return -EINVAL;
3107c66ac9dbSNicholas Bellinger }
3108c66ac9dbSNicholas Bellinger
3109c66ac9dbSNicholas Bellinger ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
3110c66ac9dbSNicholas Bellinger if (ret < 0)
3111c66ac9dbSNicholas Bellinger return -EINVAL;
3112c66ac9dbSNicholas Bellinger
31136708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
3114c66ac9dbSNicholas Bellinger "core/alua/tg_pt_gps/%s to ID: %hu\n",
3115c66ac9dbSNicholas Bellinger config_item_name(&alua_tg_pt_gp_cg->cg_item),
3116c66ac9dbSNicholas Bellinger tg_pt_gp->tg_pt_gp_id);
3117c66ac9dbSNicholas Bellinger
3118c66ac9dbSNicholas Bellinger return count;
3119c66ac9dbSNicholas Bellinger }
3120c66ac9dbSNicholas Bellinger
target_tg_pt_gp_members_show(struct config_item * item,char * page)31212eafd729SChristoph Hellwig static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
3122c66ac9dbSNicholas Bellinger char *page)
3123c66ac9dbSNicholas Bellinger {
31242eafd729SChristoph Hellwig struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3125c66ac9dbSNicholas Bellinger struct se_lun *lun;
3126c66ac9dbSNicholas Bellinger ssize_t len = 0, cur_len;
31272d4e2dafSChaitanya Kulkarni unsigned char buf[TG_PT_GROUP_NAME_BUF] = { };
3128c66ac9dbSNicholas Bellinger
3129c66ac9dbSNicholas Bellinger spin_lock(&tg_pt_gp->tg_pt_gp_lock);
3130adf653f9SChristoph Hellwig list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
3131adf653f9SChristoph Hellwig lun_tg_pt_gp_link) {
3132adf653f9SChristoph Hellwig struct se_portal_group *tpg = lun->lun_tpg;
3133c66ac9dbSNicholas Bellinger
3134c66ac9dbSNicholas Bellinger cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
313530c7ca93SDavid Disseldorp "/%s\n", tpg->se_tpg_tfo->fabric_name,
3136e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_wwn(tpg),
3137e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_tag(tpg),
3138c66ac9dbSNicholas Bellinger config_item_name(&lun->lun_group.cg_item));
3139c66ac9dbSNicholas Bellinger cur_len++; /* Extra byte for NULL terminator */
3140c66ac9dbSNicholas Bellinger
3141c66ac9dbSNicholas Bellinger if ((cur_len + len) > PAGE_SIZE) {
31426708bb27SAndy Grover pr_warn("Ran out of lu_gp_show_attr"
3143c66ac9dbSNicholas Bellinger "_members buffer\n");
3144c66ac9dbSNicholas Bellinger break;
3145c66ac9dbSNicholas Bellinger }
3146c66ac9dbSNicholas Bellinger memcpy(page+len, buf, cur_len);
3147c66ac9dbSNicholas Bellinger len += cur_len;
3148c66ac9dbSNicholas Bellinger }
3149c66ac9dbSNicholas Bellinger spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
3150c66ac9dbSNicholas Bellinger
3151c66ac9dbSNicholas Bellinger return len;
3152c66ac9dbSNicholas Bellinger }
3153c66ac9dbSNicholas Bellinger
31542eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
31552eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
31562eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
31572eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
31582eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
31592eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
31602eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
31612eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
31622eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
31632eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
31642eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
31652eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
31662eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
31672eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
31682eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
31692eafd729SChristoph Hellwig CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
31702eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
3171c66ac9dbSNicholas Bellinger
3172c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
31732eafd729SChristoph Hellwig &target_tg_pt_gp_attr_alua_access_state,
31742eafd729SChristoph Hellwig &target_tg_pt_gp_attr_alua_access_status,
31752eafd729SChristoph Hellwig &target_tg_pt_gp_attr_alua_access_type,
31762eafd729SChristoph Hellwig &target_tg_pt_gp_attr_alua_support_transitioning,
31772eafd729SChristoph Hellwig &target_tg_pt_gp_attr_alua_support_offline,
31782eafd729SChristoph Hellwig &target_tg_pt_gp_attr_alua_support_lba_dependent,
31792eafd729SChristoph Hellwig &target_tg_pt_gp_attr_alua_support_unavailable,
31802eafd729SChristoph Hellwig &target_tg_pt_gp_attr_alua_support_standby,
31812eafd729SChristoph Hellwig &target_tg_pt_gp_attr_alua_support_active_nonoptimized,
31822eafd729SChristoph Hellwig &target_tg_pt_gp_attr_alua_support_active_optimized,
31832eafd729SChristoph Hellwig &target_tg_pt_gp_attr_alua_write_metadata,
31842eafd729SChristoph Hellwig &target_tg_pt_gp_attr_nonop_delay_msecs,
31852eafd729SChristoph Hellwig &target_tg_pt_gp_attr_trans_delay_msecs,
31862eafd729SChristoph Hellwig &target_tg_pt_gp_attr_implicit_trans_secs,
31872eafd729SChristoph Hellwig &target_tg_pt_gp_attr_preferred,
31882eafd729SChristoph Hellwig &target_tg_pt_gp_attr_tg_pt_gp_id,
31892eafd729SChristoph Hellwig &target_tg_pt_gp_attr_members,
3190c66ac9dbSNicholas Bellinger NULL,
3191c66ac9dbSNicholas Bellinger };
3192c66ac9dbSNicholas Bellinger
target_core_alua_tg_pt_gp_release(struct config_item * item)31931f6fe7cbSNicholas Bellinger static void target_core_alua_tg_pt_gp_release(struct config_item *item)
31941f6fe7cbSNicholas Bellinger {
31951f6fe7cbSNicholas Bellinger struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
31961f6fe7cbSNicholas Bellinger struct t10_alua_tg_pt_gp, tg_pt_gp_group);
31971f6fe7cbSNicholas Bellinger
31981f6fe7cbSNicholas Bellinger core_alua_free_tg_pt_gp(tg_pt_gp);
31991f6fe7cbSNicholas Bellinger }
32001f6fe7cbSNicholas Bellinger
3201c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
32021f6fe7cbSNicholas Bellinger .release = target_core_alua_tg_pt_gp_release,
3203c66ac9dbSNicholas Bellinger };
3204c66ac9dbSNicholas Bellinger
3205ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_tg_pt_gp_cit = {
3206c66ac9dbSNicholas Bellinger .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
3207c66ac9dbSNicholas Bellinger .ct_attrs = target_core_alua_tg_pt_gp_attrs,
3208c66ac9dbSNicholas Bellinger .ct_owner = THIS_MODULE,
3209c66ac9dbSNicholas Bellinger };
3210c66ac9dbSNicholas Bellinger
3211c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
3212c66ac9dbSNicholas Bellinger
321372aca57bSNicholas Bellinger /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3214c66ac9dbSNicholas Bellinger
target_core_alua_create_tg_pt_gp(struct config_group * group,const char * name)3215c66ac9dbSNicholas Bellinger static struct config_group *target_core_alua_create_tg_pt_gp(
3216c66ac9dbSNicholas Bellinger struct config_group *group,
3217c66ac9dbSNicholas Bellinger const char *name)
3218c66ac9dbSNicholas Bellinger {
3219c66ac9dbSNicholas Bellinger struct t10_alua *alua = container_of(group, struct t10_alua,
3220c66ac9dbSNicholas Bellinger alua_tg_pt_gps_group);
3221c66ac9dbSNicholas Bellinger struct t10_alua_tg_pt_gp *tg_pt_gp;
3222c66ac9dbSNicholas Bellinger struct config_group *alua_tg_pt_gp_cg = NULL;
3223c66ac9dbSNicholas Bellinger struct config_item *alua_tg_pt_gp_ci = NULL;
3224c66ac9dbSNicholas Bellinger
32250fd97ccfSChristoph Hellwig tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
32266708bb27SAndy Grover if (!tg_pt_gp)
3227c66ac9dbSNicholas Bellinger return NULL;
3228c66ac9dbSNicholas Bellinger
3229c66ac9dbSNicholas Bellinger alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3230c66ac9dbSNicholas Bellinger alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
3231c66ac9dbSNicholas Bellinger
3232c66ac9dbSNicholas Bellinger config_group_init_type_name(alua_tg_pt_gp_cg, name,
3233c66ac9dbSNicholas Bellinger &target_core_alua_tg_pt_gp_cit);
3234c66ac9dbSNicholas Bellinger
32356708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
3236c66ac9dbSNicholas Bellinger " Group: alua/tg_pt_gps/%s\n",
3237c66ac9dbSNicholas Bellinger config_item_name(alua_tg_pt_gp_ci));
3238c66ac9dbSNicholas Bellinger
3239c66ac9dbSNicholas Bellinger return alua_tg_pt_gp_cg;
3240c66ac9dbSNicholas Bellinger }
3241c66ac9dbSNicholas Bellinger
target_core_alua_drop_tg_pt_gp(struct config_group * group,struct config_item * item)3242c66ac9dbSNicholas Bellinger static void target_core_alua_drop_tg_pt_gp(
3243c66ac9dbSNicholas Bellinger struct config_group *group,
3244c66ac9dbSNicholas Bellinger struct config_item *item)
3245c66ac9dbSNicholas Bellinger {
3246c66ac9dbSNicholas Bellinger struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3247c66ac9dbSNicholas Bellinger struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3248c66ac9dbSNicholas Bellinger
32496708bb27SAndy Grover pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
3250c66ac9dbSNicholas Bellinger " Group: alua/tg_pt_gps/%s, ID: %hu\n",
3251c66ac9dbSNicholas Bellinger config_item_name(item), tg_pt_gp->tg_pt_gp_id);
32521f6fe7cbSNicholas Bellinger /*
32531f6fe7cbSNicholas Bellinger * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
32541f6fe7cbSNicholas Bellinger * -> target_core_alua_tg_pt_gp_release().
32551f6fe7cbSNicholas Bellinger */
3256c66ac9dbSNicholas Bellinger config_item_put(item);
3257c66ac9dbSNicholas Bellinger }
3258c66ac9dbSNicholas Bellinger
3259c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
3260c66ac9dbSNicholas Bellinger .make_group = &target_core_alua_create_tg_pt_gp,
3261c66ac9dbSNicholas Bellinger .drop_item = &target_core_alua_drop_tg_pt_gp,
3262c66ac9dbSNicholas Bellinger };
3263c66ac9dbSNicholas Bellinger
326472aca57bSNicholas Bellinger TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
3265c66ac9dbSNicholas Bellinger
326672aca57bSNicholas Bellinger /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3267c66ac9dbSNicholas Bellinger
3268c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_alua_cit */
3269c66ac9dbSNicholas Bellinger
3270c66ac9dbSNicholas Bellinger /*
3271c66ac9dbSNicholas Bellinger * target_core_alua_cit is a ConfigFS group that lives under
3272c66ac9dbSNicholas Bellinger * /sys/kernel/config/target/core/alua. There are default groups
3273c66ac9dbSNicholas Bellinger * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
3274c66ac9dbSNicholas Bellinger * target_core_alua_cit in target_core_init_configfs() below.
3275c66ac9dbSNicholas Bellinger */
3276ece550b5SBhumika Goyal static const struct config_item_type target_core_alua_cit = {
3277c66ac9dbSNicholas Bellinger .ct_item_ops = NULL,
3278c66ac9dbSNicholas Bellinger .ct_attrs = NULL,
3279c66ac9dbSNicholas Bellinger .ct_owner = THIS_MODULE,
3280c66ac9dbSNicholas Bellinger };
3281c66ac9dbSNicholas Bellinger
3282c66ac9dbSNicholas Bellinger /* End functions for struct config_item_type target_core_alua_cit */
3283c66ac9dbSNicholas Bellinger
3284d23ab570SNicholas Bellinger /* Start functions for struct config_item_type tb_dev_stat_cit */
328512d23384SNicholas Bellinger
target_core_stat_mkdir(struct config_group * group,const char * name)328612d23384SNicholas Bellinger static struct config_group *target_core_stat_mkdir(
328712d23384SNicholas Bellinger struct config_group *group,
328812d23384SNicholas Bellinger const char *name)
328912d23384SNicholas Bellinger {
329012d23384SNicholas Bellinger return ERR_PTR(-ENOSYS);
329112d23384SNicholas Bellinger }
329212d23384SNicholas Bellinger
target_core_stat_rmdir(struct config_group * group,struct config_item * item)329312d23384SNicholas Bellinger static void target_core_stat_rmdir(
329412d23384SNicholas Bellinger struct config_group *group,
329512d23384SNicholas Bellinger struct config_item *item)
329612d23384SNicholas Bellinger {
329712d23384SNicholas Bellinger return;
329812d23384SNicholas Bellinger }
329912d23384SNicholas Bellinger
330012d23384SNicholas Bellinger static struct configfs_group_operations target_core_stat_group_ops = {
330112d23384SNicholas Bellinger .make_group = &target_core_stat_mkdir,
330212d23384SNicholas Bellinger .drop_item = &target_core_stat_rmdir,
330312d23384SNicholas Bellinger };
330412d23384SNicholas Bellinger
3305d23ab570SNicholas Bellinger TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
330612d23384SNicholas Bellinger
3307d23ab570SNicholas Bellinger /* End functions for struct config_item_type tb_dev_stat_cit */
330812d23384SNicholas Bellinger
3309c66ac9dbSNicholas Bellinger /* Start functions for struct config_item_type target_core_hba_cit */
3310c66ac9dbSNicholas Bellinger
target_core_make_subdev(struct config_group * group,const char * name)3311c66ac9dbSNicholas Bellinger static struct config_group *target_core_make_subdev(
3312c66ac9dbSNicholas Bellinger struct config_group *group,
3313c66ac9dbSNicholas Bellinger const char *name)
3314c66ac9dbSNicholas Bellinger {
3315c66ac9dbSNicholas Bellinger struct t10_alua_tg_pt_gp *tg_pt_gp;
3316c66ac9dbSNicholas Bellinger struct config_item *hba_ci = &group->cg_item;
3317c66ac9dbSNicholas Bellinger struct se_hba *hba = item_to_hba(hba_ci);
33180a06d430SChristoph Hellwig struct target_backend *tb = hba->backend;
33190fd97ccfSChristoph Hellwig struct se_device *dev;
332012d23384SNicholas Bellinger int errno = -ENOMEM, ret;
3321c66ac9dbSNicholas Bellinger
332212d23384SNicholas Bellinger ret = mutex_lock_interruptible(&hba->hba_access_mutex);
332312d23384SNicholas Bellinger if (ret)
332412d23384SNicholas Bellinger return ERR_PTR(ret);
3325c66ac9dbSNicholas Bellinger
33260fd97ccfSChristoph Hellwig dev = target_alloc_device(hba, name);
33270fd97ccfSChristoph Hellwig if (!dev)
33280fd97ccfSChristoph Hellwig goto out_unlock;
3329c66ac9dbSNicholas Bellinger
33301ae1602dSChristoph Hellwig config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
3331c66ac9dbSNicholas Bellinger
33328dc31ff9SMike Christie config_group_init_type_name(&dev->dev_action_group, "action",
33338dc31ff9SMike Christie &tb->tb_dev_action_cit);
33348dc31ff9SMike Christie configfs_add_default_group(&dev->dev_action_group, &dev->dev_group);
33358dc31ff9SMike Christie
33360fd97ccfSChristoph Hellwig config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
33370a06d430SChristoph Hellwig &tb->tb_dev_attrib_cit);
33381ae1602dSChristoph Hellwig configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
33391ae1602dSChristoph Hellwig
33400fd97ccfSChristoph Hellwig config_group_init_type_name(&dev->dev_pr_group, "pr",
33410a06d430SChristoph Hellwig &tb->tb_dev_pr_cit);
33421ae1602dSChristoph Hellwig configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
33431ae1602dSChristoph Hellwig
33440fd97ccfSChristoph Hellwig config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
33450a06d430SChristoph Hellwig &tb->tb_dev_wwn_cit);
33461ae1602dSChristoph Hellwig configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
33471ae1602dSChristoph Hellwig &dev->dev_group);
33481ae1602dSChristoph Hellwig
33490fd97ccfSChristoph Hellwig config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
33500a06d430SChristoph Hellwig "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
33511ae1602dSChristoph Hellwig configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
33521ae1602dSChristoph Hellwig &dev->dev_group);
33531ae1602dSChristoph Hellwig
33540fd97ccfSChristoph Hellwig config_group_init_type_name(&dev->dev_stat_grps.stat_group,
33550a06d430SChristoph Hellwig "statistics", &tb->tb_dev_stat_cit);
33561ae1602dSChristoph Hellwig configfs_add_default_group(&dev->dev_stat_grps.stat_group,
33571ae1602dSChristoph Hellwig &dev->dev_group);
335812d23384SNicholas Bellinger
3359c66ac9dbSNicholas Bellinger /*
336012d23384SNicholas Bellinger * Add core/$HBA/$DEV/alua/default_tg_pt_gp
3361c66ac9dbSNicholas Bellinger */
33620fd97ccfSChristoph Hellwig tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
33636708bb27SAndy Grover if (!tg_pt_gp)
33641ae1602dSChristoph Hellwig goto out_free_device;
33650fd97ccfSChristoph Hellwig dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
3366c66ac9dbSNicholas Bellinger
3367c66ac9dbSNicholas Bellinger config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
3368c66ac9dbSNicholas Bellinger "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
33691ae1602dSChristoph Hellwig configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
33701ae1602dSChristoph Hellwig &dev->t10_alua.alua_tg_pt_gps_group);
33711ae1602dSChristoph Hellwig
337212d23384SNicholas Bellinger /*
337312d23384SNicholas Bellinger * Add core/$HBA/$DEV/statistics/ default groups
337412d23384SNicholas Bellinger */
33750fd97ccfSChristoph Hellwig target_stat_setup_dev_default_groups(dev);
3376c66ac9dbSNicholas Bellinger
337780890c5eSMaurizio Lombardi mutex_lock(&target_devices_lock);
337880890c5eSMaurizio Lombardi target_devices++;
337980890c5eSMaurizio Lombardi mutex_unlock(&target_devices_lock);
338080890c5eSMaurizio Lombardi
3381c66ac9dbSNicholas Bellinger mutex_unlock(&hba->hba_access_mutex);
33821ae1602dSChristoph Hellwig return &dev->dev_group;
33830fd97ccfSChristoph Hellwig
33840fd97ccfSChristoph Hellwig out_free_device:
33850fd97ccfSChristoph Hellwig target_free_device(dev);
33860fd97ccfSChristoph Hellwig out_unlock:
3387c66ac9dbSNicholas Bellinger mutex_unlock(&hba->hba_access_mutex);
338812d23384SNicholas Bellinger return ERR_PTR(errno);
3389c66ac9dbSNicholas Bellinger }
3390c66ac9dbSNicholas Bellinger
target_core_drop_subdev(struct config_group * group,struct config_item * item)3391c66ac9dbSNicholas Bellinger static void target_core_drop_subdev(
3392c66ac9dbSNicholas Bellinger struct config_group *group,
3393c66ac9dbSNicholas Bellinger struct config_item *item)
3394c66ac9dbSNicholas Bellinger {
33950fd97ccfSChristoph Hellwig struct config_group *dev_cg = to_config_group(item);
33960fd97ccfSChristoph Hellwig struct se_device *dev =
33970fd97ccfSChristoph Hellwig container_of(dev_cg, struct se_device, dev_group);
3398c66ac9dbSNicholas Bellinger struct se_hba *hba;
3399c66ac9dbSNicholas Bellinger
34000fd97ccfSChristoph Hellwig hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
3401c66ac9dbSNicholas Bellinger
34021f6fe7cbSNicholas Bellinger mutex_lock(&hba->hba_access_mutex);
3403c66ac9dbSNicholas Bellinger
34041ae1602dSChristoph Hellwig configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
34051ae1602dSChristoph Hellwig configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
340612d23384SNicholas Bellinger
34071f6fe7cbSNicholas Bellinger /*
34081f6fe7cbSNicholas Bellinger * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
34091f6fe7cbSNicholas Bellinger * directly from target_core_alua_tg_pt_gp_release().
34101f6fe7cbSNicholas Bellinger */
34110fd97ccfSChristoph Hellwig dev->t10_alua.default_tg_pt_gp = NULL;
3412c66ac9dbSNicholas Bellinger
34131ae1602dSChristoph Hellwig configfs_remove_default_groups(dev_cg);
34141ae1602dSChristoph Hellwig
34151f6fe7cbSNicholas Bellinger /*
34160fd97ccfSChristoph Hellwig * se_dev is released from target_core_dev_item_ops->release()
34171f6fe7cbSNicholas Bellinger */
3418c66ac9dbSNicholas Bellinger config_item_put(item);
341980890c5eSMaurizio Lombardi
342080890c5eSMaurizio Lombardi mutex_lock(&target_devices_lock);
342180890c5eSMaurizio Lombardi target_devices--;
342280890c5eSMaurizio Lombardi mutex_unlock(&target_devices_lock);
342380890c5eSMaurizio Lombardi
3424c66ac9dbSNicholas Bellinger mutex_unlock(&hba->hba_access_mutex);
3425c66ac9dbSNicholas Bellinger }
3426c66ac9dbSNicholas Bellinger
3427c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_hba_group_ops = {
3428c66ac9dbSNicholas Bellinger .make_group = target_core_make_subdev,
3429c66ac9dbSNicholas Bellinger .drop_item = target_core_drop_subdev,
3430c66ac9dbSNicholas Bellinger };
3431c66ac9dbSNicholas Bellinger
3432c66ac9dbSNicholas Bellinger
to_hba(struct config_item * item)34332eafd729SChristoph Hellwig static inline struct se_hba *to_hba(struct config_item *item)
3434c66ac9dbSNicholas Bellinger {
34352eafd729SChristoph Hellwig return container_of(to_config_group(item), struct se_hba, hba_group);
34362eafd729SChristoph Hellwig }
34372eafd729SChristoph Hellwig
target_hba_info_show(struct config_item * item,char * page)34382eafd729SChristoph Hellwig static ssize_t target_hba_info_show(struct config_item *item, char *page)
34392eafd729SChristoph Hellwig {
34402eafd729SChristoph Hellwig struct se_hba *hba = to_hba(item);
34412eafd729SChristoph Hellwig
3442c66ac9dbSNicholas Bellinger return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
34430a06d430SChristoph Hellwig hba->hba_id, hba->backend->ops->name,
3444ce8dd25dSChristoph Hellwig TARGET_CORE_VERSION);
3445c66ac9dbSNicholas Bellinger }
3446c66ac9dbSNicholas Bellinger
target_hba_mode_show(struct config_item * item,char * page)34472eafd729SChristoph Hellwig static ssize_t target_hba_mode_show(struct config_item *item, char *page)
3448c66ac9dbSNicholas Bellinger {
34492eafd729SChristoph Hellwig struct se_hba *hba = to_hba(item);
3450c66ac9dbSNicholas Bellinger int hba_mode = 0;
3451c66ac9dbSNicholas Bellinger
3452c66ac9dbSNicholas Bellinger if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3453c66ac9dbSNicholas Bellinger hba_mode = 1;
3454c66ac9dbSNicholas Bellinger
3455c66ac9dbSNicholas Bellinger return sprintf(page, "%d\n", hba_mode);
3456c66ac9dbSNicholas Bellinger }
3457c66ac9dbSNicholas Bellinger
target_hba_mode_store(struct config_item * item,const char * page,size_t count)34582eafd729SChristoph Hellwig static ssize_t target_hba_mode_store(struct config_item *item,
3459c66ac9dbSNicholas Bellinger const char *page, size_t count)
3460c66ac9dbSNicholas Bellinger {
34612eafd729SChristoph Hellwig struct se_hba *hba = to_hba(item);
3462c66ac9dbSNicholas Bellinger unsigned long mode_flag;
3463c66ac9dbSNicholas Bellinger int ret;
3464c66ac9dbSNicholas Bellinger
34650a06d430SChristoph Hellwig if (hba->backend->ops->pmode_enable_hba == NULL)
3466c66ac9dbSNicholas Bellinger return -EINVAL;
3467c66ac9dbSNicholas Bellinger
346857103d7fSJingoo Han ret = kstrtoul(page, 0, &mode_flag);
3469c66ac9dbSNicholas Bellinger if (ret < 0) {
34706708bb27SAndy Grover pr_err("Unable to extract hba mode flag: %d\n", ret);
347157103d7fSJingoo Han return ret;
3472c66ac9dbSNicholas Bellinger }
3473c66ac9dbSNicholas Bellinger
34740fd97ccfSChristoph Hellwig if (hba->dev_count) {
34756708bb27SAndy Grover pr_err("Unable to set hba_mode with active devices\n");
3476c66ac9dbSNicholas Bellinger return -EINVAL;
3477c66ac9dbSNicholas Bellinger }
3478c66ac9dbSNicholas Bellinger
34790a06d430SChristoph Hellwig ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3480c66ac9dbSNicholas Bellinger if (ret < 0)
3481c66ac9dbSNicholas Bellinger return -EINVAL;
3482c66ac9dbSNicholas Bellinger if (ret > 0)
3483c66ac9dbSNicholas Bellinger hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3484c66ac9dbSNicholas Bellinger else if (ret == 0)
3485c66ac9dbSNicholas Bellinger hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3486c66ac9dbSNicholas Bellinger
3487c66ac9dbSNicholas Bellinger return count;
3488c66ac9dbSNicholas Bellinger }
3489c66ac9dbSNicholas Bellinger
34902eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(target_, hba_info);
34912eafd729SChristoph Hellwig CONFIGFS_ATTR(target_, hba_mode);
3492c66ac9dbSNicholas Bellinger
target_core_hba_release(struct config_item * item)34931f6fe7cbSNicholas Bellinger static void target_core_hba_release(struct config_item *item)
34941f6fe7cbSNicholas Bellinger {
34951f6fe7cbSNicholas Bellinger struct se_hba *hba = container_of(to_config_group(item),
34961f6fe7cbSNicholas Bellinger struct se_hba, hba_group);
34971f6fe7cbSNicholas Bellinger core_delete_hba(hba);
34981f6fe7cbSNicholas Bellinger }
34991f6fe7cbSNicholas Bellinger
3500c66ac9dbSNicholas Bellinger static struct configfs_attribute *target_core_hba_attrs[] = {
35012eafd729SChristoph Hellwig &target_attr_hba_info,
35022eafd729SChristoph Hellwig &target_attr_hba_mode,
3503c66ac9dbSNicholas Bellinger NULL,
3504c66ac9dbSNicholas Bellinger };
3505c66ac9dbSNicholas Bellinger
3506c66ac9dbSNicholas Bellinger static struct configfs_item_operations target_core_hba_item_ops = {
35071f6fe7cbSNicholas Bellinger .release = target_core_hba_release,
3508c66ac9dbSNicholas Bellinger };
3509c66ac9dbSNicholas Bellinger
3510ece550b5SBhumika Goyal static const struct config_item_type target_core_hba_cit = {
3511c66ac9dbSNicholas Bellinger .ct_item_ops = &target_core_hba_item_ops,
3512c66ac9dbSNicholas Bellinger .ct_group_ops = &target_core_hba_group_ops,
3513c66ac9dbSNicholas Bellinger .ct_attrs = target_core_hba_attrs,
3514c66ac9dbSNicholas Bellinger .ct_owner = THIS_MODULE,
3515c66ac9dbSNicholas Bellinger };
3516c66ac9dbSNicholas Bellinger
target_core_call_addhbatotarget(struct config_group * group,const char * name)3517c66ac9dbSNicholas Bellinger static struct config_group *target_core_call_addhbatotarget(
3518c66ac9dbSNicholas Bellinger struct config_group *group,
3519c66ac9dbSNicholas Bellinger const char *name)
3520c66ac9dbSNicholas Bellinger {
3521c66ac9dbSNicholas Bellinger char *se_plugin_str, *str, *str2;
3522c66ac9dbSNicholas Bellinger struct se_hba *hba;
35232d4e2dafSChaitanya Kulkarni char buf[TARGET_CORE_NAME_MAX_LEN] = { };
3524c66ac9dbSNicholas Bellinger unsigned long plugin_dep_id = 0;
3525c66ac9dbSNicholas Bellinger int ret;
3526c66ac9dbSNicholas Bellinger
352760d645a4SDan Carpenter if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
35286708bb27SAndy Grover pr_err("Passed *name strlen(): %d exceeds"
3529c66ac9dbSNicholas Bellinger " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3530c66ac9dbSNicholas Bellinger TARGET_CORE_NAME_MAX_LEN);
3531c66ac9dbSNicholas Bellinger return ERR_PTR(-ENAMETOOLONG);
3532c66ac9dbSNicholas Bellinger }
3533c66ac9dbSNicholas Bellinger snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3534c66ac9dbSNicholas Bellinger
3535c66ac9dbSNicholas Bellinger str = strstr(buf, "_");
35366708bb27SAndy Grover if (!str) {
35376708bb27SAndy Grover pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3538c66ac9dbSNicholas Bellinger return ERR_PTR(-EINVAL);
3539c66ac9dbSNicholas Bellinger }
3540c66ac9dbSNicholas Bellinger se_plugin_str = buf;
3541c66ac9dbSNicholas Bellinger /*
3542c66ac9dbSNicholas Bellinger * Special case for subsystem plugins that have "_" in their names.
3543c66ac9dbSNicholas Bellinger * Namely rd_direct and rd_mcp..
3544c66ac9dbSNicholas Bellinger */
3545c66ac9dbSNicholas Bellinger str2 = strstr(str+1, "_");
35466708bb27SAndy Grover if (str2) {
3547c66ac9dbSNicholas Bellinger *str2 = '\0'; /* Terminate for *se_plugin_str */
3548c66ac9dbSNicholas Bellinger str2++; /* Skip to start of plugin dependent ID */
3549c66ac9dbSNicholas Bellinger str = str2;
3550c66ac9dbSNicholas Bellinger } else {
3551c66ac9dbSNicholas Bellinger *str = '\0'; /* Terminate for *se_plugin_str */
3552c66ac9dbSNicholas Bellinger str++; /* Skip to start of plugin dependent ID */
3553c66ac9dbSNicholas Bellinger }
3554c66ac9dbSNicholas Bellinger
355557103d7fSJingoo Han ret = kstrtoul(str, 0, &plugin_dep_id);
3556c66ac9dbSNicholas Bellinger if (ret < 0) {
355757103d7fSJingoo Han pr_err("kstrtoul() returned %d for"
3558c66ac9dbSNicholas Bellinger " plugin_dep_id\n", ret);
355957103d7fSJingoo Han return ERR_PTR(ret);
3560c66ac9dbSNicholas Bellinger }
3561c66ac9dbSNicholas Bellinger /*
3562c66ac9dbSNicholas Bellinger * Load up TCM subsystem plugins if they have not already been loaded.
3563c66ac9dbSNicholas Bellinger */
3564dbc5623eSNicholas Bellinger transport_subsystem_check_init();
3565c66ac9dbSNicholas Bellinger
3566c66ac9dbSNicholas Bellinger hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3567c66ac9dbSNicholas Bellinger if (IS_ERR(hba))
3568c66ac9dbSNicholas Bellinger return ERR_CAST(hba);
3569c66ac9dbSNicholas Bellinger
3570c66ac9dbSNicholas Bellinger config_group_init_type_name(&hba->hba_group, name,
3571c66ac9dbSNicholas Bellinger &target_core_hba_cit);
3572c66ac9dbSNicholas Bellinger
3573c66ac9dbSNicholas Bellinger return &hba->hba_group;
3574c66ac9dbSNicholas Bellinger }
3575c66ac9dbSNicholas Bellinger
target_core_call_delhbafromtarget(struct config_group * group,struct config_item * item)3576c66ac9dbSNicholas Bellinger static void target_core_call_delhbafromtarget(
3577c66ac9dbSNicholas Bellinger struct config_group *group,
3578c66ac9dbSNicholas Bellinger struct config_item *item)
3579c66ac9dbSNicholas Bellinger {
35801f6fe7cbSNicholas Bellinger /*
35811f6fe7cbSNicholas Bellinger * core_delete_hba() is called from target_core_hba_item_ops->release()
35821f6fe7cbSNicholas Bellinger * -> target_core_hba_release()
35831f6fe7cbSNicholas Bellinger */
3584c66ac9dbSNicholas Bellinger config_item_put(item);
3585c66ac9dbSNicholas Bellinger }
3586c66ac9dbSNicholas Bellinger
3587c66ac9dbSNicholas Bellinger static struct configfs_group_operations target_core_group_ops = {
3588c66ac9dbSNicholas Bellinger .make_group = target_core_call_addhbatotarget,
3589c66ac9dbSNicholas Bellinger .drop_item = target_core_call_delhbafromtarget,
3590c66ac9dbSNicholas Bellinger };
3591c66ac9dbSNicholas Bellinger
3592ece550b5SBhumika Goyal static const struct config_item_type target_core_cit = {
3593c66ac9dbSNicholas Bellinger .ct_item_ops = NULL,
3594c66ac9dbSNicholas Bellinger .ct_group_ops = &target_core_group_ops,
3595c66ac9dbSNicholas Bellinger .ct_attrs = NULL,
3596c66ac9dbSNicholas Bellinger .ct_owner = THIS_MODULE,
3597c66ac9dbSNicholas Bellinger };
3598c66ac9dbSNicholas Bellinger
3599c66ac9dbSNicholas Bellinger /* Stop functions for struct config_item_type target_core_hba_cit */
3600c66ac9dbSNicholas Bellinger
target_setup_backend_cits(struct target_backend * tb)36010a06d430SChristoph Hellwig void target_setup_backend_cits(struct target_backend *tb)
360273112edcSNicholas Bellinger {
36030a06d430SChristoph Hellwig target_core_setup_dev_cit(tb);
36048dc31ff9SMike Christie target_core_setup_dev_action_cit(tb);
36050a06d430SChristoph Hellwig target_core_setup_dev_attrib_cit(tb);
36060a06d430SChristoph Hellwig target_core_setup_dev_pr_cit(tb);
36070a06d430SChristoph Hellwig target_core_setup_dev_wwn_cit(tb);
36080a06d430SChristoph Hellwig target_core_setup_dev_alua_tg_pt_gps_cit(tb);
36090a06d430SChristoph Hellwig target_core_setup_dev_stat_cit(tb);
361073112edcSNicholas Bellinger }
361173112edcSNicholas Bellinger
target_init_dbroot(void)361278a6295cSLee Duncan static void target_init_dbroot(void)
361378a6295cSLee Duncan {
361478a6295cSLee Duncan struct file *fp;
361578a6295cSLee Duncan
361678a6295cSLee Duncan snprintf(db_root_stage, DB_ROOT_LEN, DB_ROOT_PREFERRED);
361778a6295cSLee Duncan fp = filp_open(db_root_stage, O_RDONLY, 0);
361878a6295cSLee Duncan if (IS_ERR(fp)) {
361978a6295cSLee Duncan pr_err("db_root: cannot open: %s\n", db_root_stage);
362078a6295cSLee Duncan return;
362178a6295cSLee Duncan }
362278a6295cSLee Duncan if (!S_ISDIR(file_inode(fp)->i_mode)) {
362378a6295cSLee Duncan filp_close(fp, NULL);
362478a6295cSLee Duncan pr_err("db_root: not a valid directory: %s\n", db_root_stage);
362578a6295cSLee Duncan return;
362678a6295cSLee Duncan }
362778a6295cSLee Duncan filp_close(fp, NULL);
362878a6295cSLee Duncan
362978a6295cSLee Duncan strncpy(db_root, db_root_stage, DB_ROOT_LEN);
363078a6295cSLee Duncan pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
363178a6295cSLee Duncan }
363278a6295cSLee Duncan
target_core_init_configfs(void)363354550fabSAxel Lin static int __init target_core_init_configfs(void)
3634c66ac9dbSNicholas Bellinger {
3635d588cf8fSChristoph Hellwig struct configfs_subsystem *subsys = &target_core_fabrics;
3636c66ac9dbSNicholas Bellinger struct t10_alua_lu_gp *lu_gp;
36372f35afacSMaurizio Lombardi struct cred *kern_cred;
36382f35afacSMaurizio Lombardi const struct cred *old_cred;
3639c66ac9dbSNicholas Bellinger int ret;
3640c66ac9dbSNicholas Bellinger
36416708bb27SAndy Grover pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3642c66ac9dbSNicholas Bellinger " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3643c66ac9dbSNicholas Bellinger TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3644c66ac9dbSNicholas Bellinger
3645c66ac9dbSNicholas Bellinger config_group_init(&subsys->su_group);
3646c66ac9dbSNicholas Bellinger mutex_init(&subsys->su_mutex);
3647c66ac9dbSNicholas Bellinger
3648e3d6f909SAndy Grover ret = init_se_kmem_caches();
3649c66ac9dbSNicholas Bellinger if (ret < 0)
3650e3d6f909SAndy Grover return ret;
3651c66ac9dbSNicholas Bellinger /*
3652c66ac9dbSNicholas Bellinger * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3653c66ac9dbSNicholas Bellinger * and ALUA Logical Unit Group and Target Port Group infrastructure.
3654c66ac9dbSNicholas Bellinger */
36551ae1602dSChristoph Hellwig config_group_init_type_name(&target_core_hbagroup, "core",
36561ae1602dSChristoph Hellwig &target_core_cit);
36571ae1602dSChristoph Hellwig configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
3658c66ac9dbSNicholas Bellinger
3659c66ac9dbSNicholas Bellinger /*
3660c66ac9dbSNicholas Bellinger * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3661c66ac9dbSNicholas Bellinger */
36621ae1602dSChristoph Hellwig config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
36631ae1602dSChristoph Hellwig configfs_add_default_group(&alua_group, &target_core_hbagroup);
36641ae1602dSChristoph Hellwig
3665c66ac9dbSNicholas Bellinger /*
3666c66ac9dbSNicholas Bellinger * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3667c66ac9dbSNicholas Bellinger * groups under /sys/kernel/config/target/core/alua/
3668c66ac9dbSNicholas Bellinger */
36691ae1602dSChristoph Hellwig config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
36701ae1602dSChristoph Hellwig &target_core_alua_lu_gps_cit);
36711ae1602dSChristoph Hellwig configfs_add_default_group(&alua_lu_gps_group, &alua_group);
3672c66ac9dbSNicholas Bellinger
3673c66ac9dbSNicholas Bellinger /*
3674c66ac9dbSNicholas Bellinger * Add core/alua/lu_gps/default_lu_gp
3675c66ac9dbSNicholas Bellinger */
3676c66ac9dbSNicholas Bellinger lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
367737bb7899SPeter Senna Tschudin if (IS_ERR(lu_gp)) {
367837bb7899SPeter Senna Tschudin ret = -ENOMEM;
3679c66ac9dbSNicholas Bellinger goto out_global;
368037bb7899SPeter Senna Tschudin }
3681c66ac9dbSNicholas Bellinger
3682c66ac9dbSNicholas Bellinger config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3683c66ac9dbSNicholas Bellinger &target_core_alua_lu_gp_cit);
36841ae1602dSChristoph Hellwig configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
36851ae1602dSChristoph Hellwig
3686e3d6f909SAndy Grover default_lu_gp = lu_gp;
36871ae1602dSChristoph Hellwig
3688c66ac9dbSNicholas Bellinger /*
3689c66ac9dbSNicholas Bellinger * Register the target_core_mod subsystem with configfs.
3690c66ac9dbSNicholas Bellinger */
3691c66ac9dbSNicholas Bellinger ret = configfs_register_subsystem(subsys);
3692c66ac9dbSNicholas Bellinger if (ret < 0) {
36936708bb27SAndy Grover pr_err("Error %d while registering subsystem %s\n",
3694c66ac9dbSNicholas Bellinger ret, subsys->su_group.cg_item.ci_namebuf);
3695c66ac9dbSNicholas Bellinger goto out_global;
3696c66ac9dbSNicholas Bellinger }
36976708bb27SAndy Grover pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3698ce8dd25dSChristoph Hellwig " Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3699c66ac9dbSNicholas Bellinger " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3700c66ac9dbSNicholas Bellinger /*
3701c66ac9dbSNicholas Bellinger * Register built-in RAMDISK subsystem logic for virtual LUN 0
3702c66ac9dbSNicholas Bellinger */
3703c66ac9dbSNicholas Bellinger ret = rd_module_init();
3704c66ac9dbSNicholas Bellinger if (ret < 0)
3705c66ac9dbSNicholas Bellinger goto out;
3706c66ac9dbSNicholas Bellinger
37070d0f9dfbSRoland Dreier ret = core_dev_setup_virtual_lun0();
37080d0f9dfbSRoland Dreier if (ret < 0)
3709c66ac9dbSNicholas Bellinger goto out;
3710c66ac9dbSNicholas Bellinger
3711f99715acSNicholas Bellinger ret = target_xcopy_setup_pt();
3712f99715acSNicholas Bellinger if (ret < 0)
3713f99715acSNicholas Bellinger goto out;
3714f99715acSNicholas Bellinger
37152f35afacSMaurizio Lombardi /* We use the kernel credentials to access the target directory */
37162f35afacSMaurizio Lombardi kern_cred = prepare_kernel_cred(&init_task);
37172f35afacSMaurizio Lombardi if (!kern_cred) {
37182f35afacSMaurizio Lombardi ret = -ENOMEM;
37192f35afacSMaurizio Lombardi goto out;
37202f35afacSMaurizio Lombardi }
37212f35afacSMaurizio Lombardi old_cred = override_creds(kern_cred);
372278a6295cSLee Duncan target_init_dbroot();
37232f35afacSMaurizio Lombardi revert_creds(old_cred);
37242f35afacSMaurizio Lombardi put_cred(kern_cred);
372578a6295cSLee Duncan
3726c66ac9dbSNicholas Bellinger return 0;
3727c66ac9dbSNicholas Bellinger
3728c66ac9dbSNicholas Bellinger out:
37292f35afacSMaurizio Lombardi target_xcopy_release_pt();
3730c66ac9dbSNicholas Bellinger configfs_unregister_subsystem(subsys);
3731c66ac9dbSNicholas Bellinger core_dev_release_virtual_lun0();
3732c66ac9dbSNicholas Bellinger rd_module_exit();
3733c66ac9dbSNicholas Bellinger out_global:
3734e3d6f909SAndy Grover if (default_lu_gp) {
3735e3d6f909SAndy Grover core_alua_free_lu_gp(default_lu_gp);
3736e3d6f909SAndy Grover default_lu_gp = NULL;
3737c66ac9dbSNicholas Bellinger }
3738e3d6f909SAndy Grover release_se_kmem_caches();
3739e3d6f909SAndy Grover return ret;
3740c66ac9dbSNicholas Bellinger }
3741c66ac9dbSNicholas Bellinger
target_core_exit_configfs(void)374254550fabSAxel Lin static void __exit target_core_exit_configfs(void)
3743c66ac9dbSNicholas Bellinger {
37441ae1602dSChristoph Hellwig configfs_remove_default_groups(&alua_lu_gps_group);
37451ae1602dSChristoph Hellwig configfs_remove_default_groups(&alua_group);
37461ae1602dSChristoph Hellwig configfs_remove_default_groups(&target_core_hbagroup);
3747c66ac9dbSNicholas Bellinger
37487c2bf6e9SNicholas Bellinger /*
37497c2bf6e9SNicholas Bellinger * We expect subsys->su_group.default_groups to be released
37507c2bf6e9SNicholas Bellinger * by configfs subsystem provider logic..
37517c2bf6e9SNicholas Bellinger */
3752d588cf8fSChristoph Hellwig configfs_unregister_subsystem(&target_core_fabrics);
3753c66ac9dbSNicholas Bellinger
3754e3d6f909SAndy Grover core_alua_free_lu_gp(default_lu_gp);
3755e3d6f909SAndy Grover default_lu_gp = NULL;
37567c2bf6e9SNicholas Bellinger
37576708bb27SAndy Grover pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3758c66ac9dbSNicholas Bellinger " Infrastructure\n");
3759c66ac9dbSNicholas Bellinger
3760c66ac9dbSNicholas Bellinger core_dev_release_virtual_lun0();
3761c66ac9dbSNicholas Bellinger rd_module_exit();
3762f99715acSNicholas Bellinger target_xcopy_release_pt();
3763e3d6f909SAndy Grover release_se_kmem_caches();
3764c66ac9dbSNicholas Bellinger }
3765c66ac9dbSNicholas Bellinger
3766c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3767c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
3768c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
3769c66ac9dbSNicholas Bellinger
3770c66ac9dbSNicholas Bellinger module_init(target_core_init_configfs);
3771c66ac9dbSNicholas Bellinger module_exit(target_core_exit_configfs);
3772