]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/target/target_core_configfs.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157
[thirdparty/kernel/stable.git] / drivers / target / target_core_configfs.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
c66ac9db
NB
2/*******************************************************************************
3 * Filename: target_core_configfs.c
4 *
5 * This file contains ConfigFS logic for the Generic Target Engine project.
6 *
4c76251e 7 * (c) Copyright 2008-2013 Datera, Inc.
c66ac9db
NB
8 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 * based on configfs Copyright (C) 2005 Oracle. All rights reserved.
12 *
c66ac9db
NB
13 ****************************************************************************/
14
15#include <linux/module.h>
16#include <linux/moduleparam.h>
c66ac9db
NB
17#include <generated/utsrelease.h>
18#include <linux/utsname.h>
19#include <linux/init.h>
20#include <linux/fs.h>
21#include <linux/namei.h>
22#include <linux/slab.h>
23#include <linux/types.h>
24#include <linux/delay.h>
25#include <linux/unistd.h>
26#include <linux/string.h>
27#include <linux/parser.h>
28#include <linux/syscalls.h>
29#include <linux/configfs.h>
e3d6f909 30#include <linux/spinlock.h>
c66ac9db
NB
31
32#include <target/target_core_base.h>
c4795fb2
CH
33#include <target/target_core_backend.h>
34#include <target/target_core_fabric.h>
c66ac9db 35
e26d99ae 36#include "target_core_internal.h"
c66ac9db 37#include "target_core_alua.h"
c66ac9db
NB
38#include "target_core_pr.h"
39#include "target_core_rd.h"
f99715ac 40#include "target_core_xcopy.h"
c66ac9db 41
73112edc 42#define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
0a06d430 43static void target_core_setup_##_name##_cit(struct target_backend *tb) \
73112edc 44{ \
0a06d430 45 struct config_item_type *cit = &tb->tb_##_name##_cit; \
73112edc
NB
46 \
47 cit->ct_item_ops = _item_ops; \
48 cit->ct_group_ops = _group_ops; \
49 cit->ct_attrs = _attrs; \
0a06d430
CH
50 cit->ct_owner = tb->ops->owner; \
51 pr_debug("Setup generic %s\n", __stringify(_name)); \
52}
53
54#define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
55static void target_core_setup_##_name##_cit(struct target_backend *tb) \
56{ \
57 struct config_item_type *cit = &tb->tb_##_name##_cit; \
58 \
59 cit->ct_item_ops = _item_ops; \
60 cit->ct_group_ops = _group_ops; \
61 cit->ct_attrs = tb->ops->tb_##_name##_attrs; \
62 cit->ct_owner = tb->ops->owner; \
73112edc
NB
63 pr_debug("Setup generic %s\n", __stringify(_name)); \
64}
65
e3d6f909
AG
66extern struct t10_alua_lu_gp *default_lu_gp;
67
d0f474e5
RD
68static LIST_HEAD(g_tf_list);
69static DEFINE_MUTEX(g_tf_lock);
c66ac9db 70
e3d6f909
AG
71static struct config_group target_core_hbagroup;
72static struct config_group alua_group;
73static struct config_group alua_lu_gps_group;
74
c66ac9db
NB
75static inline struct se_hba *
76item_to_hba(struct config_item *item)
77{
78 return container_of(to_config_group(item), struct se_hba, hba_group);
79}
80
81/*
82 * Attributes for /sys/kernel/config/target/
83 */
2eafd729
CH
84static ssize_t target_core_item_version_show(struct config_item *item,
85 char *page)
c66ac9db
NB
86{
87 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
ce8dd25d 88 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
c66ac9db
NB
89 utsname()->sysname, utsname()->machine);
90}
91
2eafd729 92CONFIGFS_ATTR_RO(target_core_item_, version);
c66ac9db 93
a96e9783
LD
94char db_root[DB_ROOT_LEN] = DB_ROOT_DEFAULT;
95static char db_root_stage[DB_ROOT_LEN];
96
97static ssize_t target_core_item_dbroot_show(struct config_item *item,
98 char *page)
99{
100 return sprintf(page, "%s\n", db_root);
101}
102
103static ssize_t target_core_item_dbroot_store(struct config_item *item,
104 const char *page, size_t count)
105{
106 ssize_t read_bytes;
107 struct file *fp;
108
109 mutex_lock(&g_tf_lock);
110 if (!list_empty(&g_tf_list)) {
111 mutex_unlock(&g_tf_lock);
112 pr_err("db_root: cannot be changed: target drivers registered");
113 return -EINVAL;
114 }
115
116 if (count > (DB_ROOT_LEN - 1)) {
117 mutex_unlock(&g_tf_lock);
118 pr_err("db_root: count %d exceeds DB_ROOT_LEN-1: %u\n",
119 (int)count, DB_ROOT_LEN - 1);
120 return -EINVAL;
121 }
122
123 read_bytes = snprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
124 if (!read_bytes) {
125 mutex_unlock(&g_tf_lock);
126 return -EINVAL;
127 }
128 if (db_root_stage[read_bytes - 1] == '\n')
129 db_root_stage[read_bytes - 1] = '\0';
130
131 /* validate new db root before accepting it */
132 fp = filp_open(db_root_stage, O_RDONLY, 0);
133 if (IS_ERR(fp)) {
134 mutex_unlock(&g_tf_lock);
135 pr_err("db_root: cannot open: %s\n", db_root_stage);
136 return -EINVAL;
137 }
45063097 138 if (!S_ISDIR(file_inode(fp)->i_mode)) {
8cc3bb07 139 filp_close(fp, NULL);
a96e9783
LD
140 mutex_unlock(&g_tf_lock);
141 pr_err("db_root: not a directory: %s\n", db_root_stage);
142 return -EINVAL;
143 }
8cc3bb07 144 filp_close(fp, NULL);
a96e9783
LD
145
146 strncpy(db_root, db_root_stage, read_bytes);
147
148 mutex_unlock(&g_tf_lock);
149
78a6295c
LD
150 pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
151
a96e9783
LD
152 return read_bytes;
153}
154
155CONFIGFS_ATTR(target_core_item_, dbroot);
156
c66ac9db
NB
157static struct target_fabric_configfs *target_core_get_fabric(
158 const char *name)
159{
160 struct target_fabric_configfs *tf;
161
6708bb27 162 if (!name)
c66ac9db
NB
163 return NULL;
164
165 mutex_lock(&g_tf_lock);
166 list_for_each_entry(tf, &g_tf_list, tf_list) {
59a206b4
DD
167 const char *cmp_name = tf->tf_ops->fabric_alias;
168 if (!cmp_name)
169 cmp_name = tf->tf_ops->fabric_name;
170 if (!strcmp(cmp_name, name)) {
c66ac9db
NB
171 atomic_inc(&tf->tf_access_cnt);
172 mutex_unlock(&g_tf_lock);
173 return tf;
174 }
175 }
176 mutex_unlock(&g_tf_lock);
177
178 return NULL;
179}
180
181/*
182 * Called from struct target_core_group_ops->make_group()
183 */
184static struct config_group *target_core_register_fabric(
185 struct config_group *group,
186 const char *name)
187{
188 struct target_fabric_configfs *tf;
189 int ret;
190
6708bb27 191 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
c66ac9db 192 " %s\n", group, name);
e7b7af6e
RD
193
194 tf = target_core_get_fabric(name);
195 if (!tf) {
62554910
NB
196 pr_debug("target_core_register_fabric() trying autoload for %s\n",
197 name);
e7b7af6e 198
c66ac9db 199 /*
e7b7af6e
RD
200 * Below are some hardcoded request_module() calls to automatically
201 * local fabric modules when the following is called:
c66ac9db 202 *
e7b7af6e 203 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
c66ac9db 204 *
e7b7af6e
RD
205 * Note that this does not limit which TCM fabric module can be
206 * registered, but simply provids auto loading logic for modules with
207 * mkdir(2) system calls with known TCM fabric modules.
c66ac9db 208 */
e7b7af6e
RD
209
210 if (!strncmp(name, "iscsi", 5)) {
211 /*
212 * Automatically load the LIO Target fabric module when the
213 * following is called:
214 *
215 * mkdir -p $CONFIGFS/target/iscsi
216 */
217 ret = request_module("iscsi_target_mod");
218 if (ret < 0) {
62554910
NB
219 pr_debug("request_module() failed for"
220 " iscsi_target_mod.ko: %d\n", ret);
e7b7af6e
RD
221 return ERR_PTR(-EINVAL);
222 }
223 } else if (!strncmp(name, "loopback", 8)) {
224 /*
225 * Automatically load the tcm_loop fabric module when the
226 * following is called:
227 *
228 * mkdir -p $CONFIGFS/target/loopback
229 */
230 ret = request_module("tcm_loop");
231 if (ret < 0) {
62554910
NB
232 pr_debug("request_module() failed for"
233 " tcm_loop.ko: %d\n", ret);
e7b7af6e
RD
234 return ERR_PTR(-EINVAL);
235 }
c66ac9db 236 }
e7b7af6e
RD
237
238 tf = target_core_get_fabric(name);
c66ac9db
NB
239 }
240
6708bb27 241 if (!tf) {
62554910
NB
242 pr_debug("target_core_get_fabric() failed for %s\n",
243 name);
c66ac9db
NB
244 return ERR_PTR(-EINVAL);
245 }
6708bb27 246 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
59a206b4 247 " %s\n", tf->tf_ops->fabric_name);
c66ac9db
NB
248 /*
249 * On a successful target_core_get_fabric() look, the returned
250 * struct target_fabric_configfs *tf will contain a usage reference.
251 */
6708bb27 252 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
968ebe75 253 &tf->tf_wwn_cit);
c66ac9db 254
968ebe75 255 config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
1ae1602d 256
c66ac9db 257 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
968ebe75 258 &tf->tf_discovery_cit);
1ae1602d 259 configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
c66ac9db 260
6f3bf5a2
BVA
261 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric: %s\n",
262 config_item_name(&tf->tf_group.cg_item));
c66ac9db
NB
263 return &tf->tf_group;
264}
265
266/*
267 * Called from struct target_core_group_ops->drop_item()
268 */
269static void target_core_deregister_fabric(
270 struct config_group *group,
271 struct config_item *item)
272{
273 struct target_fabric_configfs *tf = container_of(
274 to_config_group(item), struct target_fabric_configfs, tf_group);
c66ac9db 275
6708bb27 276 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
c66ac9db
NB
277 " tf list\n", config_item_name(item));
278
6708bb27 279 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
59a206b4 280 " %s\n", tf->tf_ops->fabric_name);
c66ac9db
NB
281 atomic_dec(&tf->tf_access_cnt);
282
6708bb27 283 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
c66ac9db
NB
284 " %s\n", config_item_name(item));
285
1ae1602d 286 configfs_remove_default_groups(&tf->tf_group);
c66ac9db
NB
287 config_item_put(item);
288}
289
290static struct configfs_group_operations target_core_fabric_group_ops = {
291 .make_group = &target_core_register_fabric,
292 .drop_item = &target_core_deregister_fabric,
293};
294
295/*
296 * All item attributes appearing in /sys/kernel/target/ appear here.
297 */
298static struct configfs_attribute *target_core_fabric_item_attrs[] = {
299 &target_core_item_attr_version,
a96e9783 300 &target_core_item_attr_dbroot,
c66ac9db
NB
301 NULL,
302};
303
304/*
305 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
306 */
ece550b5 307static const struct config_item_type target_core_fabrics_item = {
c66ac9db
NB
308 .ct_group_ops = &target_core_fabric_group_ops,
309 .ct_attrs = target_core_fabric_item_attrs,
310 .ct_owner = THIS_MODULE,
311};
312
313static struct configfs_subsystem target_core_fabrics = {
314 .su_group = {
315 .cg_item = {
316 .ci_namebuf = "target",
317 .ci_type = &target_core_fabrics_item,
318 },
319 },
320};
321
d588cf8f
CH
322int target_depend_item(struct config_item *item)
323{
324 return configfs_depend_item(&target_core_fabrics, item);
325}
326EXPORT_SYMBOL(target_depend_item);
327
328void target_undepend_item(struct config_item *item)
329{
9a9e3415 330 return configfs_undepend_item(item);
d588cf8f
CH
331}
332EXPORT_SYMBOL(target_undepend_item);
c66ac9db
NB
333
334/*##############################################################################
335// Start functions called by external Target Fabrics Modules
336//############################################################################*/
337
9ac8928e 338static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
c66ac9db 339{
59a206b4
DD
340 if (tfo->fabric_alias) {
341 if (strlen(tfo->fabric_alias) >= TARGET_FABRIC_NAME_SIZE) {
342 pr_err("Passed alias: %s exceeds "
343 "TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_alias);
344 return -EINVAL;
345 }
c66ac9db 346 }
30c7ca93
DD
347 if (!tfo->fabric_name) {
348 pr_err("Missing tfo->fabric_name\n");
c66ac9db
NB
349 return -EINVAL;
350 }
59a206b4
DD
351 if (strlen(tfo->fabric_name) >= TARGET_FABRIC_NAME_SIZE) {
352 pr_err("Passed name: %s exceeds "
353 "TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_name);
354 return -EINVAL;
355 }
6708bb27
AG
356 if (!tfo->tpg_get_wwn) {
357 pr_err("Missing tfo->tpg_get_wwn()\n");
c66ac9db
NB
358 return -EINVAL;
359 }
6708bb27
AG
360 if (!tfo->tpg_get_tag) {
361 pr_err("Missing tfo->tpg_get_tag()\n");
c66ac9db
NB
362 return -EINVAL;
363 }
6708bb27
AG
364 if (!tfo->tpg_check_demo_mode) {
365 pr_err("Missing tfo->tpg_check_demo_mode()\n");
c66ac9db
NB
366 return -EINVAL;
367 }
6708bb27
AG
368 if (!tfo->tpg_check_demo_mode_cache) {
369 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
c66ac9db
NB
370 return -EINVAL;
371 }
6708bb27
AG
372 if (!tfo->tpg_check_demo_mode_write_protect) {
373 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
c66ac9db
NB
374 return -EINVAL;
375 }
6708bb27
AG
376 if (!tfo->tpg_check_prod_mode_write_protect) {
377 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
c66ac9db
NB
378 return -EINVAL;
379 }
6708bb27
AG
380 if (!tfo->tpg_get_inst_index) {
381 pr_err("Missing tfo->tpg_get_inst_index()\n");
c66ac9db
NB
382 return -EINVAL;
383 }
35462975 384 if (!tfo->release_cmd) {
6708bb27 385 pr_err("Missing tfo->release_cmd()\n");
c66ac9db
NB
386 return -EINVAL;
387 }
6708bb27
AG
388 if (!tfo->sess_get_index) {
389 pr_err("Missing tfo->sess_get_index()\n");
c66ac9db
NB
390 return -EINVAL;
391 }
6708bb27
AG
392 if (!tfo->write_pending) {
393 pr_err("Missing tfo->write_pending()\n");
c66ac9db
NB
394 return -EINVAL;
395 }
6708bb27
AG
396 if (!tfo->set_default_node_attributes) {
397 pr_err("Missing tfo->set_default_node_attributes()\n");
c66ac9db
NB
398 return -EINVAL;
399 }
6708bb27
AG
400 if (!tfo->get_cmd_state) {
401 pr_err("Missing tfo->get_cmd_state()\n");
c66ac9db
NB
402 return -EINVAL;
403 }
6708bb27
AG
404 if (!tfo->queue_data_in) {
405 pr_err("Missing tfo->queue_data_in()\n");
c66ac9db
NB
406 return -EINVAL;
407 }
6708bb27
AG
408 if (!tfo->queue_status) {
409 pr_err("Missing tfo->queue_status()\n");
c66ac9db
NB
410 return -EINVAL;
411 }
6708bb27
AG
412 if (!tfo->queue_tm_rsp) {
413 pr_err("Missing tfo->queue_tm_rsp()\n");
c66ac9db
NB
414 return -EINVAL;
415 }
131e6abc
NB
416 if (!tfo->aborted_task) {
417 pr_err("Missing tfo->aborted_task()\n");
418 return -EINVAL;
419 }
9c28ca4f
NB
420 if (!tfo->check_stop_free) {
421 pr_err("Missing tfo->check_stop_free()\n");
422 return -EINVAL;
423 }
c66ac9db
NB
424 /*
425 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
426 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
427 * target_core_fabric_configfs.c WWN+TPG group context code.
428 */
6708bb27
AG
429 if (!tfo->fabric_make_wwn) {
430 pr_err("Missing tfo->fabric_make_wwn()\n");
c66ac9db
NB
431 return -EINVAL;
432 }
6708bb27
AG
433 if (!tfo->fabric_drop_wwn) {
434 pr_err("Missing tfo->fabric_drop_wwn()\n");
c66ac9db
NB
435 return -EINVAL;
436 }
6708bb27
AG
437 if (!tfo->fabric_make_tpg) {
438 pr_err("Missing tfo->fabric_make_tpg()\n");
c66ac9db
NB
439 return -EINVAL;
440 }
6708bb27
AG
441 if (!tfo->fabric_drop_tpg) {
442 pr_err("Missing tfo->fabric_drop_tpg()\n");
c66ac9db
NB
443 return -EINVAL;
444 }
445
446 return 0;
447}
448
9ac8928e 449int target_register_template(const struct target_core_fabric_ops *fo)
c66ac9db 450{
9ac8928e 451 struct target_fabric_configfs *tf;
c66ac9db
NB
452 int ret;
453
9ac8928e
CH
454 ret = target_fabric_tf_ops_check(fo);
455 if (ret)
456 return ret;
457
458 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
6708bb27 459 if (!tf) {
9ac8928e
CH
460 pr_err("%s: could not allocate memory!\n", __func__);
461 return -ENOMEM;
c66ac9db 462 }
c66ac9db 463
9ac8928e
CH
464 INIT_LIST_HEAD(&tf->tf_list);
465 atomic_set(&tf->tf_access_cnt, 0);
ef0caf8d 466 tf->tf_ops = fo;
9ac8928e
CH
467 target_fabric_setup_cits(tf);
468
469 mutex_lock(&g_tf_lock);
470 list_add_tail(&tf->tf_list, &g_tf_list);
471 mutex_unlock(&g_tf_lock);
472
c66ac9db
NB
473 return 0;
474}
9ac8928e 475EXPORT_SYMBOL(target_register_template);
c66ac9db 476
9ac8928e 477void target_unregister_template(const struct target_core_fabric_ops *fo)
c66ac9db 478{
9ac8928e 479 struct target_fabric_configfs *t;
c66ac9db 480
c66ac9db 481 mutex_lock(&g_tf_lock);
9ac8928e 482 list_for_each_entry(t, &g_tf_list, tf_list) {
59a206b4 483 if (!strcmp(t->tf_ops->fabric_name, fo->fabric_name)) {
9ac8928e
CH
484 BUG_ON(atomic_read(&t->tf_access_cnt));
485 list_del(&t->tf_list);
94509182
NB
486 mutex_unlock(&g_tf_lock);
487 /*
488 * Wait for any outstanding fabric se_deve_entry->rcu_head
489 * callbacks to complete post kfree_rcu(), before allowing
490 * fabric driver unload of TFO->module to proceed.
491 */
492 rcu_barrier();
9ac8928e 493 kfree(t);
94509182 494 return;
9ac8928e 495 }
c66ac9db 496 }
c66ac9db 497 mutex_unlock(&g_tf_lock);
c66ac9db 498}
9ac8928e 499EXPORT_SYMBOL(target_unregister_template);
c66ac9db
NB
500
501/*##############################################################################
502// Stop functions called by external Target Fabrics Modules
503//############################################################################*/
504
2eafd729
CH
505static inline struct se_dev_attrib *to_attrib(struct config_item *item)
506{
507 return container_of(to_config_group(item), struct se_dev_attrib,
508 da_group);
509}
510
f79a897e 511/* Start functions for struct config_item_type tb_dev_attrib_cit */
2eafd729
CH
512#define DEF_CONFIGFS_ATTRIB_SHOW(_name) \
513static ssize_t _name##_show(struct config_item *item, char *page) \
5873c4d1 514{ \
2eafd729
CH
515 return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
516}
517
518DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
519DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
520DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
521DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
522DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
523DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
524DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
525DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
526DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
527DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
528DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
b49d6f78 529DEF_CONFIGFS_ATTRIB_SHOW(emulate_pr);
2eafd729
CH
530DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
531DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
056e8924 532DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_verify);
2eafd729
CH
533DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
534DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
535DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
536DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
537DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
538DEF_CONFIGFS_ATTRIB_SHOW(block_size);
539DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
540DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
541DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
542DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
543DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
544DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
545DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
546DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
e6f41633 547DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
2eafd729
CH
548DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
549
550#define DEF_CONFIGFS_ATTRIB_STORE_U32(_name) \
551static ssize_t _name##_store(struct config_item *item, const char *page,\
3effdb90 552 size_t count) \
5873c4d1 553{ \
2eafd729 554 struct se_dev_attrib *da = to_attrib(item); \
3effdb90 555 u32 val; \
5873c4d1
CH
556 int ret; \
557 \
3effdb90
CH
558 ret = kstrtou32(page, 0, &val); \
559 if (ret < 0) \
560 return ret; \
561 da->_name = val; \
562 return count; \
563}
564
2eafd729
CH
565DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
566DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
567DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
568DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
569DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
3effdb90 570
2eafd729
CH
571#define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name) \
572static ssize_t _name##_store(struct config_item *item, const char *page, \
3effdb90
CH
573 size_t count) \
574{ \
2eafd729 575 struct se_dev_attrib *da = to_attrib(item); \
3effdb90
CH
576 bool flag; \
577 int ret; \
5873c4d1 578 \
3effdb90
CH
579 ret = strtobool(page, &flag); \
580 if (ret < 0) \
581 return ret; \
582 da->_name = flag; \
583 return count; \
584}
585
2eafd729
CH
586DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
587DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
588DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
b49d6f78 589DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_pr);
2eafd729
CH
590DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
591DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
3effdb90 592
2eafd729
CH
593#define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name) \
594static ssize_t _name##_store(struct config_item *item, const char *page,\
3effdb90
CH
595 size_t count) \
596{ \
597 printk_once(KERN_WARNING \
234bdbc4
CVB
598 "ignoring deprecated %s attribute\n", \
599 __stringify(_name)); \
3effdb90
CH
600 return count; \
601}
602
2eafd729
CH
603DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
604DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
3effdb90
CH
605
606static void dev_set_t10_wwn_model_alias(struct se_device *dev)
607{
608 const char *configname;
609
610 configname = config_item_name(&dev->dev_group.cg_item);
b2da4abf 611 if (strlen(configname) >= INQUIRY_MODEL_LEN) {
3effdb90 612 pr_warn("dev[%p]: Backstore name '%s' is too long for "
b2da4abf 613 "INQUIRY_MODEL, truncating to 15 characters\n", dev,
3effdb90
CH
614 configname);
615 }
b2da4abf
DD
616 /*
617 * XXX We can't use sizeof(dev->t10_wwn.model) (INQUIRY_MODEL_LEN + 1)
618 * here without potentially breaking existing setups, so continue to
619 * truncate one byte shorter than what can be carried in INQUIRY.
620 */
621 strlcpy(dev->t10_wwn.model, configname, INQUIRY_MODEL_LEN);
3effdb90
CH
622}
623
2eafd729 624static ssize_t emulate_model_alias_store(struct config_item *item,
3effdb90
CH
625 const char *page, size_t count)
626{
2eafd729 627 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
628 struct se_device *dev = da->da_dev;
629 bool flag;
630 int ret;
631
632 if (dev->export_count) {
633 pr_err("dev[%p]: Unable to change model alias"
634 " while export_count is %d\n",
635 dev, dev->export_count);
636 return -EINVAL;
637 }
638
639 ret = strtobool(page, &flag);
640 if (ret < 0)
641 return ret;
642
b2da4abf 643 BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
3effdb90
CH
644 if (flag) {
645 dev_set_t10_wwn_model_alias(dev);
646 } else {
b2da4abf
DD
647 strlcpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
648 sizeof(dev->t10_wwn.model));
3effdb90
CH
649 }
650 da->emulate_model_alias = flag;
651 return count;
652}
653
2eafd729 654static ssize_t emulate_write_cache_store(struct config_item *item,
3effdb90
CH
655 const char *page, size_t count)
656{
2eafd729 657 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
658 bool flag;
659 int ret;
660
661 ret = strtobool(page, &flag);
662 if (ret < 0)
663 return ret;
664
665 if (flag && da->da_dev->transport->get_write_cache) {
666 pr_err("emulate_write_cache not supported for this device\n");
667 return -EINVAL;
668 }
669
670 da->emulate_write_cache = flag;
671 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
672 da->da_dev, flag);
673 return count;
674}
675
2eafd729 676static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
3effdb90
CH
677 const char *page, size_t count)
678{
2eafd729 679 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
680 u32 val;
681 int ret;
682
683 ret = kstrtou32(page, 0, &val);
684 if (ret < 0)
685 return ret;
686
687 if (val != 0 && val != 1 && val != 2) {
688 pr_err("Illegal value %d\n", val);
689 return -EINVAL;
690 }
691
692 if (da->da_dev->export_count) {
693 pr_err("dev[%p]: Unable to change SE Device"
694 " UA_INTRLCK_CTRL while export_count is %d\n",
695 da->da_dev, da->da_dev->export_count);
696 return -EINVAL;
697 }
698 da->emulate_ua_intlck_ctrl = val;
699 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
700 da->da_dev, val);
701 return count;
702}
703
2eafd729 704static ssize_t emulate_tas_store(struct config_item *item,
3effdb90
CH
705 const char *page, size_t count)
706{
2eafd729 707 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
708 bool flag;
709 int ret;
710
711 ret = strtobool(page, &flag);
712 if (ret < 0)
713 return ret;
714
715 if (da->da_dev->export_count) {
716 pr_err("dev[%p]: Unable to change SE Device TAS while"
717 " export_count is %d\n",
718 da->da_dev, da->da_dev->export_count);
719 return -EINVAL;
720 }
721 da->emulate_tas = flag;
722 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
723 da->da_dev, flag ? "Enabled" : "Disabled");
724
725 return count;
726}
727
2eafd729 728static ssize_t emulate_tpu_store(struct config_item *item,
3effdb90
CH
729 const char *page, size_t count)
730{
2eafd729 731 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
732 bool flag;
733 int ret;
734
735 ret = strtobool(page, &flag);
736 if (ret < 0)
737 return ret;
738
739 /*
740 * We expect this value to be non-zero when generic Block Layer
741 * Discard supported is detected iblock_create_virtdevice().
742 */
743 if (flag && !da->max_unmap_block_desc_count) {
744 pr_err("Generic Block Discard not supported\n");
745 return -ENOSYS;
746 }
747
748 da->emulate_tpu = flag;
749 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
750 da->da_dev, flag);
751 return count;
752}
753
2eafd729 754static ssize_t emulate_tpws_store(struct config_item *item,
3effdb90
CH
755 const char *page, size_t count)
756{
2eafd729 757 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
758 bool flag;
759 int ret;
760
761 ret = strtobool(page, &flag);
762 if (ret < 0)
763 return ret;
764
765 /*
766 * We expect this value to be non-zero when generic Block Layer
767 * Discard supported is detected iblock_create_virtdevice().
768 */
769 if (flag && !da->max_unmap_block_desc_count) {
770 pr_err("Generic Block Discard not supported\n");
771 return -ENOSYS;
772 }
773
774 da->emulate_tpws = flag;
775 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
776 da->da_dev, flag);
777 return count;
778}
779
2eafd729 780static ssize_t pi_prot_type_store(struct config_item *item,
3effdb90
CH
781 const char *page, size_t count)
782{
2eafd729 783 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
784 int old_prot = da->pi_prot_type, ret;
785 struct se_device *dev = da->da_dev;
786 u32 flag;
787
788 ret = kstrtou32(page, 0, &flag);
789 if (ret < 0)
790 return ret;
791
792 if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
793 pr_err("Illegal value %d for pi_prot_type\n", flag);
794 return -EINVAL;
795 }
796 if (flag == 2) {
797 pr_err("DIF TYPE2 protection currently not supported\n");
798 return -ENOSYS;
799 }
800 if (da->hw_pi_prot_type) {
801 pr_warn("DIF protection enabled on underlying hardware,"
802 " ignoring\n");
803 return count;
804 }
805 if (!dev->transport->init_prot || !dev->transport->free_prot) {
806 /* 0 is only allowed value for non-supporting backends */
807 if (flag == 0)
bc1a7d6a 808 return count;
3effdb90
CH
809
810 pr_err("DIF protection not supported by backend: %s\n",
811 dev->transport->name);
812 return -ENOSYS;
813 }
cb0f32e1 814 if (!target_dev_configured(dev)) {
3effdb90
CH
815 pr_err("DIF protection requires device to be configured\n");
816 return -ENODEV;
817 }
818 if (dev->export_count) {
819 pr_err("dev[%p]: Unable to change SE Device PROT type while"
820 " export_count is %d\n", dev, dev->export_count);
821 return -EINVAL;
822 }
823
824 da->pi_prot_type = flag;
825
826 if (flag && !old_prot) {
827 ret = dev->transport->init_prot(dev);
828 if (ret) {
829 da->pi_prot_type = old_prot;
056e8924 830 da->pi_prot_verify = (bool) da->pi_prot_type;
3effdb90
CH
831 return ret;
832 }
833
834 } else if (!flag && old_prot) {
835 dev->transport->free_prot(dev);
836 }
837
056e8924 838 da->pi_prot_verify = (bool) da->pi_prot_type;
3effdb90
CH
839 pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
840 return count;
841}
842
b6cd7f34
DD
843/* always zero, but attr needs to remain RW to avoid userspace breakage */
844static ssize_t pi_prot_format_show(struct config_item *item, char *page)
845{
846 return snprintf(page, PAGE_SIZE, "0\n");
847}
848
2eafd729 849static ssize_t pi_prot_format_store(struct config_item *item,
3effdb90
CH
850 const char *page, size_t count)
851{
2eafd729 852 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
853 struct se_device *dev = da->da_dev;
854 bool flag;
855 int ret;
856
857 ret = strtobool(page, &flag);
858 if (ret < 0)
859 return ret;
860
861 if (!flag)
862 return count;
863
864 if (!dev->transport->format_prot) {
865 pr_err("DIF protection format not supported by backend %s\n",
866 dev->transport->name);
867 return -ENOSYS;
868 }
cb0f32e1 869 if (!target_dev_configured(dev)) {
3effdb90
CH
870 pr_err("DIF protection format requires device to be configured\n");
871 return -ENODEV;
872 }
873 if (dev->export_count) {
874 pr_err("dev[%p]: Unable to format SE Device PROT type while"
875 " export_count is %d\n", dev, dev->export_count);
876 return -EINVAL;
877 }
878
879 ret = dev->transport->format_prot(dev);
880 if (ret)
881 return ret;
882
883 pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
884 return count;
885}
886
056e8924
DM
887static ssize_t pi_prot_verify_store(struct config_item *item,
888 const char *page, size_t count)
889{
890 struct se_dev_attrib *da = to_attrib(item);
891 bool flag;
892 int ret;
893
894 ret = strtobool(page, &flag);
895 if (ret < 0)
896 return ret;
897
898 if (!flag) {
899 da->pi_prot_verify = flag;
900 return count;
901 }
902 if (da->hw_pi_prot_type) {
903 pr_warn("DIF protection enabled on underlying hardware,"
904 " ignoring\n");
905 return count;
906 }
907 if (!da->pi_prot_type) {
908 pr_warn("DIF protection not supported by backend, ignoring\n");
909 return count;
910 }
911 da->pi_prot_verify = flag;
912
913 return count;
914}
915
2eafd729 916static ssize_t force_pr_aptpl_store(struct config_item *item,
3effdb90
CH
917 const char *page, size_t count)
918{
2eafd729 919 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
920 bool flag;
921 int ret;
922
923 ret = strtobool(page, &flag);
924 if (ret < 0)
925 return ret;
926 if (da->da_dev->export_count) {
927 pr_err("dev[%p]: Unable to set force_pr_aptpl while"
928 " export_count is %d\n",
929 da->da_dev, da->da_dev->export_count);
930 return -EINVAL;
931 }
932
933 da->force_pr_aptpl = flag;
934 pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
935 return count;
936}
937
2eafd729 938static ssize_t emulate_rest_reord_store(struct config_item *item,
3effdb90
CH
939 const char *page, size_t count)
940{
2eafd729 941 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
942 bool flag;
943 int ret;
944
945 ret = strtobool(page, &flag);
946 if (ret < 0)
947 return ret;
948
949 if (flag != 0) {
950 printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
951 " reordering not implemented\n", da->da_dev);
952 return -ENOSYS;
953 }
954 da->emulate_rest_reord = flag;
955 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
956 da->da_dev, flag);
957 return count;
958}
959
e6f41633
JP
960static ssize_t unmap_zeroes_data_store(struct config_item *item,
961 const char *page, size_t count)
962{
963 struct se_dev_attrib *da = to_attrib(item);
964 bool flag;
965 int ret;
966
967 ret = strtobool(page, &flag);
968 if (ret < 0)
969 return ret;
970
971 if (da->da_dev->export_count) {
972 pr_err("dev[%p]: Unable to change SE Device"
973 " unmap_zeroes_data while export_count is %d\n",
974 da->da_dev, da->da_dev->export_count);
975 return -EINVAL;
976 }
977 /*
978 * We expect this value to be non-zero when generic Block Layer
979 * Discard supported is detected iblock_configure_device().
980 */
981 if (flag && !da->max_unmap_block_desc_count) {
982 pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
983 " because max_unmap_block_desc_count is zero\n",
984 da->da_dev);
9b3118ce 985 return -ENOSYS;
e6f41633
JP
986 }
987 da->unmap_zeroes_data = flag;
988 pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
989 da->da_dev, flag);
2e498f25 990 return count;
e6f41633
JP
991}
992
3effdb90
CH
993/*
994 * Note, this can only be called on unexported SE Device Object.
995 */
2eafd729 996static ssize_t queue_depth_store(struct config_item *item,
3effdb90
CH
997 const char *page, size_t count)
998{
2eafd729 999 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
1000 struct se_device *dev = da->da_dev;
1001 u32 val;
1002 int ret;
1003
1004 ret = kstrtou32(page, 0, &val);
1005 if (ret < 0)
1006 return ret;
1007
1008 if (dev->export_count) {
1009 pr_err("dev[%p]: Unable to change SE Device TCQ while"
1010 " export_count is %d\n",
1011 dev, dev->export_count);
1012 return -EINVAL;
1013 }
1014 if (!val) {
1015 pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
1016 return -EINVAL;
1017 }
1018
1019 if (val > dev->dev_attrib.queue_depth) {
1020 if (val > dev->dev_attrib.hw_queue_depth) {
1021 pr_err("dev[%p]: Passed queue_depth:"
1022 " %u exceeds TCM/SE_Device MAX"
1023 " TCQ: %u\n", dev, val,
1024 dev->dev_attrib.hw_queue_depth);
1025 return -EINVAL;
1026 }
1027 }
1028 da->queue_depth = dev->queue_depth = val;
1029 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
1030 return count;
1031}
1032
2eafd729 1033static ssize_t optimal_sectors_store(struct config_item *item,
3effdb90
CH
1034 const char *page, size_t count)
1035{
2eafd729 1036 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
1037 u32 val;
1038 int ret;
1039
1040 ret = kstrtou32(page, 0, &val);
1041 if (ret < 0)
1042 return ret;
1043
1044 if (da->da_dev->export_count) {
1045 pr_err("dev[%p]: Unable to change SE Device"
1046 " optimal_sectors while export_count is %d\n",
1047 da->da_dev, da->da_dev->export_count);
1048 return -EINVAL;
1049 }
1050 if (val > da->hw_max_sectors) {
1051 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
1052 " greater than hw_max_sectors: %u\n",
1053 da->da_dev, val, da->hw_max_sectors);
1054 return -EINVAL;
1055 }
1056
1057 da->optimal_sectors = val;
1058 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
1059 da->da_dev, val);
1060 return count;
5873c4d1
CH
1061}
1062
2eafd729 1063static ssize_t block_size_store(struct config_item *item,
3effdb90
CH
1064 const char *page, size_t count)
1065{
2eafd729 1066 struct se_dev_attrib *da = to_attrib(item);
3effdb90
CH
1067 u32 val;
1068 int ret;
1069
1070 ret = kstrtou32(page, 0, &val);
1071 if (ret < 0)
1072 return ret;
5873c4d1 1073
3effdb90
CH
1074 if (da->da_dev->export_count) {
1075 pr_err("dev[%p]: Unable to change SE Device block_size"
1076 " while export_count is %d\n",
1077 da->da_dev, da->da_dev->export_count);
1078 return -EINVAL;
1079 }
1080
1081 if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
1082 pr_err("dev[%p]: Illegal value for block_device: %u"
1083 " for SE device, must be 512, 1024, 2048 or 4096\n",
1084 da->da_dev, val);
1085 return -EINVAL;
1086 }
1087
1088 da->block_size = val;
1089 if (da->max_bytes_per_io)
1090 da->hw_max_sectors = da->max_bytes_per_io / val;
1091
1092 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
1093 da->da_dev, val);
1094 return count;
1095}
5873c4d1 1096
c17d5d5f
MC
1097static ssize_t alua_support_show(struct config_item *item, char *page)
1098{
1099 struct se_dev_attrib *da = to_attrib(item);
1100 u8 flags = da->da_dev->transport->transport_flags;
1101
1102 return snprintf(page, PAGE_SIZE, "%d\n",
1103 flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ? 0 : 1);
1104}
1105
1106static ssize_t pgr_support_show(struct config_item *item, char *page)
1107{
1108 struct se_dev_attrib *da = to_attrib(item);
1109 u8 flags = da->da_dev->transport->transport_flags;
1110
1111 return snprintf(page, PAGE_SIZE, "%d\n",
1112 flags & TRANSPORT_FLAG_PASSTHROUGH_PGR ? 0 : 1);
1113}
1114
2eafd729
CH
1115CONFIGFS_ATTR(, emulate_model_alias);
1116CONFIGFS_ATTR(, emulate_dpo);
1117CONFIGFS_ATTR(, emulate_fua_write);
1118CONFIGFS_ATTR(, emulate_fua_read);
1119CONFIGFS_ATTR(, emulate_write_cache);
1120CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
1121CONFIGFS_ATTR(, emulate_tas);
1122CONFIGFS_ATTR(, emulate_tpu);
1123CONFIGFS_ATTR(, emulate_tpws);
1124CONFIGFS_ATTR(, emulate_caw);
1125CONFIGFS_ATTR(, emulate_3pc);
b49d6f78 1126CONFIGFS_ATTR(, emulate_pr);
2eafd729
CH
1127CONFIGFS_ATTR(, pi_prot_type);
1128CONFIGFS_ATTR_RO(, hw_pi_prot_type);
b6cd7f34 1129CONFIGFS_ATTR(, pi_prot_format);
056e8924 1130CONFIGFS_ATTR(, pi_prot_verify);
2eafd729
CH
1131CONFIGFS_ATTR(, enforce_pr_isids);
1132CONFIGFS_ATTR(, is_nonrot);
1133CONFIGFS_ATTR(, emulate_rest_reord);
1134CONFIGFS_ATTR(, force_pr_aptpl);
1135CONFIGFS_ATTR_RO(, hw_block_size);
1136CONFIGFS_ATTR(, block_size);
1137CONFIGFS_ATTR_RO(, hw_max_sectors);
1138CONFIGFS_ATTR(, optimal_sectors);
1139CONFIGFS_ATTR_RO(, hw_queue_depth);
1140CONFIGFS_ATTR(, queue_depth);
1141CONFIGFS_ATTR(, max_unmap_lba_count);
1142CONFIGFS_ATTR(, max_unmap_block_desc_count);
1143CONFIGFS_ATTR(, unmap_granularity);
1144CONFIGFS_ATTR(, unmap_granularity_alignment);
e6f41633 1145CONFIGFS_ATTR(, unmap_zeroes_data);
2eafd729 1146CONFIGFS_ATTR(, max_write_same_len);
c17d5d5f
MC
1147CONFIGFS_ATTR_RO(, alua_support);
1148CONFIGFS_ATTR_RO(, pgr_support);
c66ac9db 1149
5873c4d1
CH
1150/*
1151 * dev_attrib attributes for devices using the target core SBC/SPC
1152 * interpreter. Any backend using spc_parse_cdb should be using
1153 * these.
1154 */
1155struct configfs_attribute *sbc_attrib_attrs[] = {
2eafd729
CH
1156 &attr_emulate_model_alias,
1157 &attr_emulate_dpo,
1158 &attr_emulate_fua_write,
1159 &attr_emulate_fua_read,
1160 &attr_emulate_write_cache,
1161 &attr_emulate_ua_intlck_ctrl,
1162 &attr_emulate_tas,
1163 &attr_emulate_tpu,
1164 &attr_emulate_tpws,
1165 &attr_emulate_caw,
1166 &attr_emulate_3pc,
b49d6f78 1167 &attr_emulate_pr,
2eafd729
CH
1168 &attr_pi_prot_type,
1169 &attr_hw_pi_prot_type,
1170 &attr_pi_prot_format,
056e8924 1171 &attr_pi_prot_verify,
2eafd729
CH
1172 &attr_enforce_pr_isids,
1173 &attr_is_nonrot,
1174 &attr_emulate_rest_reord,
1175 &attr_force_pr_aptpl,
1176 &attr_hw_block_size,
1177 &attr_block_size,
1178 &attr_hw_max_sectors,
1179 &attr_optimal_sectors,
1180 &attr_hw_queue_depth,
1181 &attr_queue_depth,
1182 &attr_max_unmap_lba_count,
1183 &attr_max_unmap_block_desc_count,
1184 &attr_unmap_granularity,
1185 &attr_unmap_granularity_alignment,
e6f41633 1186 &attr_unmap_zeroes_data,
2eafd729 1187 &attr_max_write_same_len,
c17d5d5f
MC
1188 &attr_alua_support,
1189 &attr_pgr_support,
5873c4d1
CH
1190 NULL,
1191};
1192EXPORT_SYMBOL(sbc_attrib_attrs);
1193
5873c4d1
CH
1194/*
1195 * Minimal dev_attrib attributes for devices passing through CDBs.
1196 * In this case we only provide a few read-only attributes for
1197 * backwards compatibility.
1198 */
1199struct configfs_attribute *passthrough_attrib_attrs[] = {
2eafd729
CH
1200 &attr_hw_pi_prot_type,
1201 &attr_hw_block_size,
1202 &attr_hw_max_sectors,
1203 &attr_hw_queue_depth,
c17d5d5f
MC
1204 &attr_alua_support,
1205 &attr_pgr_support,
5873c4d1
CH
1206 NULL,
1207};
1208EXPORT_SYMBOL(passthrough_attrib_attrs);
1209
2eafd729 1210TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
8dc31ff9 1211TB_CIT_SETUP_DRV(dev_action, NULL, NULL);
c66ac9db 1212
f79a897e 1213/* End functions for struct config_item_type tb_dev_attrib_cit */
c66ac9db 1214
f8d389c6 1215/* Start functions for struct config_item_type tb_dev_wwn_cit */
c66ac9db 1216
2eafd729
CH
1217static struct t10_wwn *to_t10_wwn(struct config_item *item)
1218{
1219 return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
1220}
c66ac9db 1221
0322913c
AA
1222static ssize_t target_check_inquiry_data(char *buf)
1223{
1224 size_t len;
1225 int i;
1226
1227 len = strlen(buf);
1228
1229 /*
1230 * SPC 4.3.1:
1231 * ASCII data fields shall contain only ASCII printable characters
1232 * (i.e., code values 20h to 7Eh) and may be terminated with one or
1233 * more ASCII null (00h) characters.
1234 */
1235 for (i = 0; i < len; i++) {
1236 if (buf[i] < 0x20 || buf[i] > 0x7E) {
1237 pr_err("Emulated T10 Inquiry Data contains non-ASCII-printable characters\n");
1238 return -EINVAL;
1239 }
1240 }
1241
1242 return len;
1243}
1244
54a6f3f6
DD
1245/*
1246 * STANDARD and VPD page 0x83 T10 Vendor Identification
1247 */
1248static ssize_t target_wwn_vendor_id_show(struct config_item *item,
1249 char *page)
1250{
1251 return sprintf(page, "%s\n", &to_t10_wwn(item)->vendor[0]);
1252}
1253
1254static ssize_t target_wwn_vendor_id_store(struct config_item *item,
1255 const char *page, size_t count)
1256{
1257 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1258 struct se_device *dev = t10_wwn->t10_dev;
1259 /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1260 unsigned char buf[INQUIRY_VENDOR_LEN + 2];
1261 char *stripped = NULL;
ee26724a
CIK
1262 size_t len;
1263 ssize_t ret;
54a6f3f6
DD
1264
1265 len = strlcpy(buf, page, sizeof(buf));
1266 if (len < sizeof(buf)) {
1267 /* Strip any newline added from userspace. */
1268 stripped = strstrip(buf);
1269 len = strlen(stripped);
1270 }
1271 if (len > INQUIRY_VENDOR_LEN) {
1272 pr_err("Emulated T10 Vendor Identification exceeds"
1273 " INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)
1274 "\n");
1275 return -EOVERFLOW;
1276 }
1277
0322913c
AA
1278 ret = target_check_inquiry_data(stripped);
1279
1280 if (ret < 0)
1281 return ret;
54a6f3f6
DD
1282
1283 /*
1284 * Check to see if any active exports exist. If they do exist, fail
1285 * here as changing this information on the fly (underneath the
1286 * initiator side OS dependent multipath code) could cause negative
1287 * effects.
1288 */
1289 if (dev->export_count) {
1290 pr_err("Unable to set T10 Vendor Identification while"
1291 " active %d exports exist\n", dev->export_count);
1292 return -EINVAL;
1293 }
1294
1295 BUILD_BUG_ON(sizeof(dev->t10_wwn.vendor) != INQUIRY_VENDOR_LEN + 1);
1296 strlcpy(dev->t10_wwn.vendor, stripped, sizeof(dev->t10_wwn.vendor));
1297
1298 pr_debug("Target_Core_ConfigFS: Set emulated T10 Vendor Identification:"
1299 " %s\n", dev->t10_wwn.vendor);
1300
1301 return count;
1302}
1303
0322913c
AA
1304static ssize_t target_wwn_product_id_show(struct config_item *item,
1305 char *page)
1306{
1307 return sprintf(page, "%s\n", &to_t10_wwn(item)->model[0]);
1308}
1309
1310static ssize_t target_wwn_product_id_store(struct config_item *item,
1311 const char *page, size_t count)
1312{
1313 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1314 struct se_device *dev = t10_wwn->t10_dev;
1315 /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1316 unsigned char buf[INQUIRY_MODEL_LEN + 2];
1317 char *stripped = NULL;
ee26724a
CIK
1318 size_t len;
1319 ssize_t ret;
0322913c
AA
1320
1321 len = strlcpy(buf, page, sizeof(buf));
1322 if (len < sizeof(buf)) {
1323 /* Strip any newline added from userspace. */
1324 stripped = strstrip(buf);
1325 len = strlen(stripped);
1326 }
1327 if (len > INQUIRY_MODEL_LEN) {
1328 pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: "
1329 __stringify(INQUIRY_MODEL_LEN)
1330 "\n");
1331 return -EOVERFLOW;
1332 }
1333
1334 ret = target_check_inquiry_data(stripped);
1335
1336 if (ret < 0)
1337 return ret;
1338
1339 /*
1340 * Check to see if any active exports exist. If they do exist, fail
1341 * here as changing this information on the fly (underneath the
1342 * initiator side OS dependent multipath code) could cause negative
1343 * effects.
1344 */
1345 if (dev->export_count) {
1346 pr_err("Unable to set T10 Model while active %d exports exist\n",
1347 dev->export_count);
1348 return -EINVAL;
1349 }
1350
1351 BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
1352 strlcpy(dev->t10_wwn.model, stripped, sizeof(dev->t10_wwn.model));
1353
1354 pr_debug("Target_Core_ConfigFS: Set emulated T10 Model Identification: %s\n",
1355 dev->t10_wwn.model);
1356
1357 return count;
1358}
1359
1360static ssize_t target_wwn_revision_show(struct config_item *item,
1361 char *page)
1362{
1363 return sprintf(page, "%s\n", &to_t10_wwn(item)->revision[0]);
1364}
1365
1366static ssize_t target_wwn_revision_store(struct config_item *item,
1367 const char *page, size_t count)
1368{
1369 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1370 struct se_device *dev = t10_wwn->t10_dev;
1371 /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1372 unsigned char buf[INQUIRY_REVISION_LEN + 2];
1373 char *stripped = NULL;
ee26724a
CIK
1374 size_t len;
1375 ssize_t ret;
0322913c
AA
1376
1377 len = strlcpy(buf, page, sizeof(buf));
1378 if (len < sizeof(buf)) {
1379 /* Strip any newline added from userspace. */
1380 stripped = strstrip(buf);
1381 len = strlen(stripped);
1382 }
1383 if (len > INQUIRY_REVISION_LEN) {
1384 pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: "
1385 __stringify(INQUIRY_REVISION_LEN)
1386 "\n");
1387 return -EOVERFLOW;
1388 }
1389
1390 ret = target_check_inquiry_data(stripped);
1391
1392 if (ret < 0)
1393 return ret;
1394
1395 /*
1396 * Check to see if any active exports exist. If they do exist, fail
1397 * here as changing this information on the fly (underneath the
1398 * initiator side OS dependent multipath code) could cause negative
1399 * effects.
1400 */
1401 if (dev->export_count) {
1402 pr_err("Unable to set T10 Revision while active %d exports exist\n",
1403 dev->export_count);
1404 return -EINVAL;
1405 }
1406
1407 BUILD_BUG_ON(sizeof(dev->t10_wwn.revision) != INQUIRY_REVISION_LEN + 1);
1408 strlcpy(dev->t10_wwn.revision, stripped, sizeof(dev->t10_wwn.revision));
1409
1410 pr_debug("Target_Core_ConfigFS: Set emulated T10 Revision: %s\n",
1411 dev->t10_wwn.revision);
1412
1413 return count;
1414}
1415
c66ac9db
NB
1416/*
1417 * VPD page 0x80 Unit serial
1418 */
2eafd729
CH
1419static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1420 char *page)
c66ac9db 1421{
c66ac9db 1422 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
2eafd729 1423 &to_t10_wwn(item)->unit_serial[0]);
c66ac9db
NB
1424}
1425
2eafd729
CH
1426static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
1427 const char *page, size_t count)
c66ac9db 1428{
2eafd729 1429 struct t10_wwn *t10_wwn = to_t10_wwn(item);
0fd97ccf 1430 struct se_device *dev = t10_wwn->t10_dev;
c66ac9db
NB
1431 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1432
1433 /*
1434 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1435 * from the struct scsi_device level firmware, do not allow
1436 * VPD Unit Serial to be emulated.
1437 *
1438 * Note this struct scsi_device could also be emulating VPD
1439 * information from its drivers/scsi LLD. But for now we assume
1440 * it is doing 'the right thing' wrt a world wide unique
1441 * VPD Unit Serial Number that OS dependent multipath can depend on.
1442 */
0fd97ccf 1443 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
6708bb27 1444 pr_err("Underlying SCSI device firmware provided VPD"
c66ac9db
NB
1445 " Unit Serial, ignoring request\n");
1446 return -EOPNOTSUPP;
1447 }
1448
60d645a4 1449 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
6708bb27 1450 pr_err("Emulated VPD Unit Serial exceeds"
c66ac9db
NB
1451 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1452 return -EOVERFLOW;
1453 }
1454 /*
1455 * Check to see if any active $FABRIC_MOD exports exist. If they
1456 * do exist, fail here as changing this information on the fly
1457 * (underneath the initiator side OS dependent multipath code)
1458 * could cause negative effects.
1459 */
0fd97ccf
CH
1460 if (dev->export_count) {
1461 pr_err("Unable to set VPD Unit Serial while"
1462 " active %d $FABRIC_MOD exports exist\n",
1463 dev->export_count);
1464 return -EINVAL;
c66ac9db 1465 }
0fd97ccf 1466
c66ac9db
NB
1467 /*
1468 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1469 *
1470 * Also, strip any newline added from the userspace
1471 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1472 */
1473 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1474 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
0fd97ccf 1475 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
c66ac9db 1476 "%s", strstrip(buf));
0fd97ccf 1477 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
c66ac9db 1478
6708bb27 1479 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
0fd97ccf 1480 " %s\n", dev->t10_wwn.unit_serial);
c66ac9db
NB
1481
1482 return count;
1483}
1484
c66ac9db
NB
1485/*
1486 * VPD page 0x83 Protocol Identifier
1487 */
2eafd729
CH
1488static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1489 char *page)
c66ac9db 1490{
2eafd729 1491 struct t10_wwn *t10_wwn = to_t10_wwn(item);
c66ac9db
NB
1492 struct t10_vpd *vpd;
1493 unsigned char buf[VPD_TMP_BUF_SIZE];
1494 ssize_t len = 0;
1495
c66ac9db
NB
1496 memset(buf, 0, VPD_TMP_BUF_SIZE);
1497
1498 spin_lock(&t10_wwn->t10_vpd_lock);
1499 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
6708bb27 1500 if (!vpd->protocol_identifier_set)
c66ac9db
NB
1501 continue;
1502
1503 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1504
6708bb27 1505 if (len + strlen(buf) >= PAGE_SIZE)
c66ac9db
NB
1506 break;
1507
1508 len += sprintf(page+len, "%s", buf);
1509 }
1510 spin_unlock(&t10_wwn->t10_vpd_lock);
1511
1512 return len;
1513}
1514
c66ac9db
NB
1515/*
1516 * Generic wrapper for dumping VPD identifiers by association.
1517 */
1518#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
2eafd729
CH
1519static ssize_t target_wwn_##_name##_show(struct config_item *item, \
1520 char *page) \
c66ac9db 1521{ \
2eafd729
CH
1522 struct t10_wwn *t10_wwn = to_t10_wwn(item); \
1523 struct t10_vpd *vpd; \
c66ac9db
NB
1524 unsigned char buf[VPD_TMP_BUF_SIZE]; \
1525 ssize_t len = 0; \
1526 \
c66ac9db
NB
1527 spin_lock(&t10_wwn->t10_vpd_lock); \
1528 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1529 if (vpd->association != _assoc) \
1530 continue; \
1531 \
1532 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1533 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
6708bb27 1534 if (len + strlen(buf) >= PAGE_SIZE) \
c66ac9db
NB
1535 break; \
1536 len += sprintf(page+len, "%s", buf); \
1537 \
1538 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1539 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
6708bb27 1540 if (len + strlen(buf) >= PAGE_SIZE) \
c66ac9db
NB
1541 break; \
1542 len += sprintf(page+len, "%s", buf); \
1543 \
1544 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1545 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
6708bb27 1546 if (len + strlen(buf) >= PAGE_SIZE) \
c66ac9db
NB
1547 break; \
1548 len += sprintf(page+len, "%s", buf); \
1549 } \
1550 spin_unlock(&t10_wwn->t10_vpd_lock); \
1551 \
1552 return len; \
1553}
1554
2eafd729 1555/* VPD page 0x83 Association: Logical Unit */
c66ac9db 1556DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
2eafd729 1557/* VPD page 0x83 Association: Target Port */
c66ac9db 1558DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
2eafd729 1559/* VPD page 0x83 Association: SCSI Target Device */
c66ac9db
NB
1560DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1561
54a6f3f6 1562CONFIGFS_ATTR(target_wwn_, vendor_id);
0322913c
AA
1563CONFIGFS_ATTR(target_wwn_, product_id);
1564CONFIGFS_ATTR(target_wwn_, revision);
2eafd729
CH
1565CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
1566CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
1567CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
1568CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
1569CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
c66ac9db
NB
1570
1571static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
54a6f3f6 1572 &target_wwn_attr_vendor_id,
0322913c
AA
1573 &target_wwn_attr_product_id,
1574 &target_wwn_attr_revision,
2eafd729
CH
1575 &target_wwn_attr_vpd_unit_serial,
1576 &target_wwn_attr_vpd_protocol_identifier,
1577 &target_wwn_attr_vpd_assoc_logical_unit,
1578 &target_wwn_attr_vpd_assoc_target_port,
1579 &target_wwn_attr_vpd_assoc_scsi_target_device,
c66ac9db
NB
1580 NULL,
1581};
1582
2eafd729 1583TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
c66ac9db 1584
f8d389c6 1585/* End functions for struct config_item_type tb_dev_wwn_cit */
c66ac9db 1586
91e2e39b 1587/* Start functions for struct config_item_type tb_dev_pr_cit */
c66ac9db 1588
2eafd729
CH
1589static struct se_device *pr_to_dev(struct config_item *item)
1590{
1591 return container_of(to_config_group(item), struct se_device,
1592 dev_pr_group);
1593}
c66ac9db 1594
d977f437
CH
1595static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1596 char *page)
c66ac9db
NB
1597{
1598 struct se_node_acl *se_nacl;
1599 struct t10_pr_registration *pr_reg;
1600 char i_buf[PR_REG_ISID_ID_LEN];
c66ac9db
NB
1601
1602 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1603
c66ac9db 1604 pr_reg = dev->dev_pr_res_holder;
d977f437
CH
1605 if (!pr_reg)
1606 return sprintf(page, "No SPC-3 Reservation holder\n");
1607
c66ac9db 1608 se_nacl = pr_reg->pr_reg_nacl;
d2843c17 1609 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
c66ac9db 1610
d977f437 1611 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
30c7ca93 1612 se_nacl->se_tpg->se_tpg_tfo->fabric_name,
d2843c17 1613 se_nacl->initiatorname, i_buf);
c66ac9db
NB
1614}
1615
d977f437
CH
1616static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1617 char *page)
c66ac9db 1618{
fae43461 1619 struct se_session *sess = dev->reservation_holder;
c66ac9db 1620 struct se_node_acl *se_nacl;
d977f437 1621 ssize_t len;
c66ac9db 1622
fae43461
BVA
1623 if (sess) {
1624 se_nacl = sess->se_node_acl;
d977f437
CH
1625 len = sprintf(page,
1626 "SPC-2 Reservation: %s Initiator: %s\n",
30c7ca93 1627 se_nacl->se_tpg->se_tpg_tfo->fabric_name,
d977f437
CH
1628 se_nacl->initiatorname);
1629 } else {
1630 len = sprintf(page, "No SPC-2 Reservation holder\n");
c66ac9db 1631 }
d977f437 1632 return len;
c66ac9db
NB
1633}
1634
2eafd729 1635static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
c66ac9db 1636{
2eafd729 1637 struct se_device *dev = pr_to_dev(item);
d977f437 1638 int ret;
c66ac9db 1639
b49d6f78
DD
1640 if (!dev->dev_attrib.emulate_pr)
1641 return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1642
4ec5bf0e 1643 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
d977f437 1644 return sprintf(page, "Passthrough\n");
c66ac9db 1645
d977f437
CH
1646 spin_lock(&dev->dev_reservation_lock);
1647 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1648 ret = target_core_dev_pr_show_spc2_res(dev, page);
1649 else
1650 ret = target_core_dev_pr_show_spc3_res(dev, page);
1651 spin_unlock(&dev->dev_reservation_lock);
1652 return ret;
c66ac9db
NB
1653}
1654
2eafd729
CH
1655static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
1656 char *page)
c66ac9db 1657{
2eafd729 1658 struct se_device *dev = pr_to_dev(item);
c66ac9db
NB
1659 ssize_t len = 0;
1660
c66ac9db 1661 spin_lock(&dev->dev_reservation_lock);
d977f437 1662 if (!dev->dev_pr_res_holder) {
c66ac9db 1663 len = sprintf(page, "No SPC-3 Reservation holder\n");
d977f437 1664 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
c66ac9db
NB
1665 len = sprintf(page, "SPC-3 Reservation: All Target"
1666 " Ports registration\n");
d977f437 1667 } else {
c66ac9db
NB
1668 len = sprintf(page, "SPC-3 Reservation: Single"
1669 " Target Port registration\n");
d977f437 1670 }
c66ac9db 1671
d977f437 1672 spin_unlock(&dev->dev_reservation_lock);
c66ac9db
NB
1673 return len;
1674}
1675
2eafd729
CH
1676static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
1677 char *page)
c66ac9db 1678{
2eafd729 1679 return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
c66ac9db
NB
1680}
1681
c66ac9db 1682
2eafd729
CH
1683static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
1684 char *page)
c66ac9db 1685{
2eafd729 1686 struct se_device *dev = pr_to_dev(item);
c66ac9db 1687 struct se_node_acl *se_nacl;
c66ac9db
NB
1688 struct se_portal_group *se_tpg;
1689 struct t10_pr_registration *pr_reg;
9ac8928e 1690 const struct target_core_fabric_ops *tfo;
c66ac9db
NB
1691 ssize_t len = 0;
1692
c66ac9db
NB
1693 spin_lock(&dev->dev_reservation_lock);
1694 pr_reg = dev->dev_pr_res_holder;
6708bb27 1695 if (!pr_reg) {
c66ac9db 1696 len = sprintf(page, "No SPC-3 Reservation holder\n");
d977f437 1697 goto out_unlock;
c66ac9db 1698 }
d977f437 1699
c66ac9db
NB
1700 se_nacl = pr_reg->pr_reg_nacl;
1701 se_tpg = se_nacl->se_tpg;
e3d6f909 1702 tfo = se_tpg->se_tpg_tfo;
c66ac9db
NB
1703
1704 len += sprintf(page+len, "SPC-3 Reservation: %s"
30c7ca93 1705 " Target Node Endpoint: %s\n", tfo->fabric_name,
c66ac9db
NB
1706 tfo->tpg_get_wwn(se_tpg));
1707 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
35d1efe8 1708 " Identifier Tag: %hu %s Portal Group Tag: %hu"
f2d30680 1709 " %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
30c7ca93
DD
1710 tfo->fabric_name, tfo->tpg_get_tag(se_tpg),
1711 tfo->fabric_name, pr_reg->pr_aptpl_target_lun);
c66ac9db 1712
d977f437
CH
1713out_unlock:
1714 spin_unlock(&dev->dev_reservation_lock);
c66ac9db
NB
1715 return len;
1716}
1717
c66ac9db 1718
2eafd729
CH
1719static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
1720 char *page)
c66ac9db 1721{
2eafd729 1722 struct se_device *dev = pr_to_dev(item);
9ac8928e 1723 const struct target_core_fabric_ops *tfo;
c66ac9db
NB
1724 struct t10_pr_registration *pr_reg;
1725 unsigned char buf[384];
1726 char i_buf[PR_REG_ISID_ID_LEN];
1727 ssize_t len = 0;
d2843c17 1728 int reg_count = 0;
c66ac9db 1729
c66ac9db
NB
1730 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1731
0fd97ccf
CH
1732 spin_lock(&dev->t10_pr.registration_lock);
1733 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
c66ac9db
NB
1734 pr_reg_list) {
1735
1736 memset(buf, 0, 384);
1737 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1738 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
d2843c17 1739 core_pr_dump_initiator_port(pr_reg, i_buf,
c66ac9db
NB
1740 PR_REG_ISID_ID_LEN);
1741 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
30c7ca93 1742 tfo->fabric_name,
d2843c17 1743 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
c66ac9db
NB
1744 pr_reg->pr_res_generation);
1745
6708bb27 1746 if (len + strlen(buf) >= PAGE_SIZE)
c66ac9db
NB
1747 break;
1748
1749 len += sprintf(page+len, "%s", buf);
1750 reg_count++;
1751 }
0fd97ccf 1752 spin_unlock(&dev->t10_pr.registration_lock);
c66ac9db 1753
6708bb27 1754 if (!reg_count)
c66ac9db
NB
1755 len += sprintf(page+len, "None\n");
1756
1757 return len;
1758}
1759
2eafd729 1760static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
c66ac9db 1761{
2eafd729 1762 struct se_device *dev = pr_to_dev(item);
c66ac9db
NB
1763 struct t10_pr_registration *pr_reg;
1764 ssize_t len = 0;
1765
c66ac9db
NB
1766 spin_lock(&dev->dev_reservation_lock);
1767 pr_reg = dev->dev_pr_res_holder;
d977f437
CH
1768 if (pr_reg) {
1769 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1770 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1771 } else {
c66ac9db 1772 len = sprintf(page, "No SPC-3 Reservation holder\n");
c66ac9db 1773 }
c66ac9db 1774
d977f437 1775 spin_unlock(&dev->dev_reservation_lock);
c66ac9db
NB
1776 return len;
1777}
1778
2eafd729 1779static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
c66ac9db 1780{
2eafd729
CH
1781 struct se_device *dev = pr_to_dev(item);
1782
b49d6f78
DD
1783 if (!dev->dev_attrib.emulate_pr)
1784 return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
4ec5bf0e 1785 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
d977f437 1786 return sprintf(page, "SPC_PASSTHROUGH\n");
b49d6f78 1787 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
d977f437 1788 return sprintf(page, "SPC2_RESERVATIONS\n");
b49d6f78
DD
1789
1790 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
c66ac9db
NB
1791}
1792
2eafd729
CH
1793static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
1794 char *page)
c66ac9db 1795{
2eafd729
CH
1796 struct se_device *dev = pr_to_dev(item);
1797
b49d6f78
DD
1798 if (!dev->dev_attrib.emulate_pr ||
1799 (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
c66ac9db
NB
1800 return 0;
1801
1802 return sprintf(page, "APTPL Bit Status: %s\n",
0fd97ccf 1803 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
c66ac9db
NB
1804}
1805
2eafd729
CH
1806static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
1807 char *page)
c66ac9db 1808{
2eafd729
CH
1809 struct se_device *dev = pr_to_dev(item);
1810
b49d6f78
DD
1811 if (!dev->dev_attrib.emulate_pr ||
1812 (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
c66ac9db
NB
1813 return 0;
1814
1815 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1816}
1817
1818enum {
1819 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1820 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1821 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1822 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1823};
1824
1825static match_table_t tokens = {
1826 {Opt_initiator_fabric, "initiator_fabric=%s"},
1827 {Opt_initiator_node, "initiator_node=%s"},
1828 {Opt_initiator_sid, "initiator_sid=%s"},
1829 {Opt_sa_res_key, "sa_res_key=%s"},
1830 {Opt_res_holder, "res_holder=%d"},
1831 {Opt_res_type, "res_type=%d"},
1832 {Opt_res_scope, "res_scope=%d"},
1833 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
a2db857b 1834 {Opt_mapped_lun, "mapped_lun=%u"},
c66ac9db
NB
1835 {Opt_target_fabric, "target_fabric=%s"},
1836 {Opt_target_node, "target_node=%s"},
1837 {Opt_tpgt, "tpgt=%d"},
1838 {Opt_port_rtpi, "port_rtpi=%d"},
a2db857b 1839 {Opt_target_lun, "target_lun=%u"},
c66ac9db
NB
1840 {Opt_err, NULL}
1841};
1842
2eafd729
CH
1843static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
1844 const char *page, size_t count)
c66ac9db 1845{
2eafd729 1846 struct se_device *dev = pr_to_dev(item);
6d180253
JJ
1847 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1848 unsigned char *t_fabric = NULL, *t_port = NULL;
8d213559 1849 char *orig, *ptr, *opts;
c66ac9db
NB
1850 substring_t args[MAX_OPT_ARGS];
1851 unsigned long long tmp_ll;
1852 u64 sa_res_key = 0;
f2d30680 1853 u64 mapped_lun = 0, target_lun = 0;
c66ac9db 1854 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
45fb94c2
BVA
1855 u16 tpgt = 0;
1856 u8 type = 0;
c66ac9db 1857
b49d6f78
DD
1858 if (!dev->dev_attrib.emulate_pr ||
1859 (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
9105bfc0 1860 return count;
d977f437 1861 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
9105bfc0 1862 return count;
c66ac9db 1863
0fd97ccf 1864 if (dev->export_count) {
6708bb27 1865 pr_debug("Unable to process APTPL metadata while"
c66ac9db
NB
1866 " active fabric exports exist\n");
1867 return -EINVAL;
1868 }
1869
1870 opts = kstrdup(page, GFP_KERNEL);
1871 if (!opts)
1872 return -ENOMEM;
1873
1874 orig = opts;
90c161b6 1875 while ((ptr = strsep(&opts, ",\n")) != NULL) {
c66ac9db
NB
1876 if (!*ptr)
1877 continue;
1878
1879 token = match_token(ptr, tokens, args);
1880 switch (token) {
1881 case Opt_initiator_fabric:
8d213559 1882 i_fabric = match_strdup(args);
6d180253
JJ
1883 if (!i_fabric) {
1884 ret = -ENOMEM;
1885 goto out;
1886 }
c66ac9db
NB
1887 break;
1888 case Opt_initiator_node:
8d213559 1889 i_port = match_strdup(args);
6d180253
JJ
1890 if (!i_port) {
1891 ret = -ENOMEM;
1892 goto out;
1893 }
60d645a4 1894 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
6708bb27 1895 pr_err("APTPL metadata initiator_node="
c66ac9db
NB
1896 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1897 PR_APTPL_MAX_IPORT_LEN);
1898 ret = -EINVAL;
1899 break;
1900 }
1901 break;
1902 case Opt_initiator_sid:
8d213559 1903 isid = match_strdup(args);
6d180253
JJ
1904 if (!isid) {
1905 ret = -ENOMEM;
1906 goto out;
1907 }
60d645a4 1908 if (strlen(isid) >= PR_REG_ISID_LEN) {
6708bb27 1909 pr_err("APTPL metadata initiator_isid"
c66ac9db
NB
1910 "= exceeds PR_REG_ISID_LEN: %d\n",
1911 PR_REG_ISID_LEN);
1912 ret = -EINVAL;
1913 break;
1914 }
1915 break;
1916 case Opt_sa_res_key:
a2db857b 1917 ret = match_u64(args, &tmp_ll);
c66ac9db 1918 if (ret < 0) {
8d213559 1919 pr_err("kstrtoull() failed for sa_res_key=\n");
c66ac9db
NB
1920 goto out;
1921 }
1922 sa_res_key = (u64)tmp_ll;
1923 break;
1924 /*
1925 * PR APTPL Metadata for Reservation
1926 */
1927 case Opt_res_holder:
c2091026
DD
1928 ret = match_int(args, &arg);
1929 if (ret)
1930 goto out;
c66ac9db
NB
1931 res_holder = arg;
1932 break;
1933 case Opt_res_type:
c2091026
DD
1934 ret = match_int(args, &arg);
1935 if (ret)
1936 goto out;
c66ac9db
NB
1937 type = (u8)arg;
1938 break;
1939 case Opt_res_scope:
c2091026
DD
1940 ret = match_int(args, &arg);
1941 if (ret)
1942 goto out;
c66ac9db
NB
1943 break;
1944 case Opt_res_all_tg_pt:
c2091026
DD
1945 ret = match_int(args, &arg);
1946 if (ret)
1947 goto out;
c66ac9db
NB
1948 all_tg_pt = (int)arg;
1949 break;
1950 case Opt_mapped_lun:
a2db857b 1951 ret = match_u64(args, &tmp_ll);
c2091026
DD
1952 if (ret)
1953 goto out;
a2db857b 1954 mapped_lun = (u64)tmp_ll;
c66ac9db
NB
1955 break;
1956 /*
1957 * PR APTPL Metadata for Target Port
1958 */
1959 case Opt_target_fabric:
8d213559 1960 t_fabric = match_strdup(args);
6d180253
JJ
1961 if (!t_fabric) {
1962 ret = -ENOMEM;
1963 goto out;
1964 }
c66ac9db
NB
1965 break;
1966 case Opt_target_node:
8d213559 1967 t_port = match_strdup(args);
6d180253
JJ
1968 if (!t_port) {
1969 ret = -ENOMEM;
1970 goto out;
1971 }
60d645a4 1972 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
6708bb27 1973 pr_err("APTPL metadata target_node="
c66ac9db
NB
1974 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1975 PR_APTPL_MAX_TPORT_LEN);
1976 ret = -EINVAL;
1977 break;
1978 }
1979 break;
1980 case Opt_tpgt:
c2091026
DD
1981 ret = match_int(args, &arg);
1982 if (ret)
1983 goto out;
c66ac9db
NB
1984 tpgt = (u16)arg;
1985 break;
1986 case Opt_port_rtpi:
c2091026
DD
1987 ret = match_int(args, &arg);
1988 if (ret)
1989 goto out;
c66ac9db
NB
1990 break;
1991 case Opt_target_lun:
a2db857b 1992 ret = match_u64(args, &tmp_ll);
c2091026
DD
1993 if (ret)
1994 goto out;
a2db857b 1995 target_lun = (u64)tmp_ll;
c66ac9db
NB
1996 break;
1997 default:
1998 break;
1999 }
2000 }
2001
6708bb27
AG
2002 if (!i_port || !t_port || !sa_res_key) {
2003 pr_err("Illegal parameters for APTPL registration\n");
c66ac9db
NB
2004 ret = -EINVAL;
2005 goto out;
2006 }
2007
2008 if (res_holder && !(type)) {
6708bb27 2009 pr_err("Illegal PR type: 0x%02x for reservation"
c66ac9db
NB
2010 " holder\n", type);
2011 ret = -EINVAL;
2012 goto out;
2013 }
2014
0fd97ccf 2015 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
c66ac9db
NB
2016 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
2017 res_holder, all_tg_pt, type);
2018out:
6d180253
JJ
2019 kfree(i_fabric);
2020 kfree(i_port);
2021 kfree(isid);
2022 kfree(t_fabric);
2023 kfree(t_port);
c66ac9db
NB
2024 kfree(orig);
2025 return (ret == 0) ? count : ret;
2026}
2027
c66ac9db 2028
2eafd729
CH
2029CONFIGFS_ATTR_RO(target_pr_, res_holder);
2030CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
2031CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
2032CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
2033CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
2034CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
2035CONFIGFS_ATTR_RO(target_pr_, res_type);
2036CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
2037CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
c66ac9db
NB
2038
2039static struct configfs_attribute *target_core_dev_pr_attrs[] = {
2eafd729
CH
2040 &target_pr_attr_res_holder,
2041 &target_pr_attr_res_pr_all_tgt_pts,
2042 &target_pr_attr_res_pr_generation,
2043 &target_pr_attr_res_pr_holder_tg_port,
2044 &target_pr_attr_res_pr_registered_i_pts,
2045 &target_pr_attr_res_pr_type,
2046 &target_pr_attr_res_type,
2047 &target_pr_attr_res_aptpl_active,
2048 &target_pr_attr_res_aptpl_metadata,
c66ac9db
NB
2049 NULL,
2050};
2051
2eafd729 2052TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
c66ac9db 2053
91e2e39b 2054/* End functions for struct config_item_type tb_dev_pr_cit */
c66ac9db 2055
73112edc 2056/* Start functions for struct config_item_type tb_dev_cit */
c66ac9db 2057
2eafd729
CH
2058static inline struct se_device *to_device(struct config_item *item)
2059{
2060 return container_of(to_config_group(item), struct se_device, dev_group);
2061}
2062
2063static ssize_t target_dev_info_show(struct config_item *item, char *page)
c66ac9db 2064{
2eafd729 2065 struct se_device *dev = to_device(item);
c66ac9db
NB
2066 int bl = 0;
2067 ssize_t read_bytes = 0;
2068
0fd97ccf 2069 transport_dump_dev_state(dev, page, &bl);
c66ac9db 2070 read_bytes += bl;
0a06d430
CH
2071 read_bytes += dev->transport->show_configfs_dev_params(dev,
2072 page+read_bytes);
c66ac9db
NB
2073 return read_bytes;
2074}
2075
2eafd729
CH
2076static ssize_t target_dev_control_store(struct config_item *item,
2077 const char *page, size_t count)
c66ac9db 2078{
2eafd729 2079 struct se_device *dev = to_device(item);
c66ac9db 2080
0a06d430 2081 return dev->transport->set_configfs_dev_params(dev, page, count);
c66ac9db
NB
2082}
2083
2eafd729 2084static ssize_t target_dev_alias_show(struct config_item *item, char *page)
c66ac9db 2085{
2eafd729 2086 struct se_device *dev = to_device(item);
c66ac9db 2087
0fd97ccf 2088 if (!(dev->dev_flags & DF_USING_ALIAS))
c66ac9db
NB
2089 return 0;
2090
0fd97ccf 2091 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
c66ac9db
NB
2092}
2093
2eafd729
CH
2094static ssize_t target_dev_alias_store(struct config_item *item,
2095 const char *page, size_t count)
c66ac9db 2096{
2eafd729 2097 struct se_device *dev = to_device(item);
0fd97ccf 2098 struct se_hba *hba = dev->se_hba;
c66ac9db
NB
2099 ssize_t read_bytes;
2100
2101 if (count > (SE_DEV_ALIAS_LEN-1)) {
6708bb27 2102 pr_err("alias count: %d exceeds"
c66ac9db
NB
2103 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
2104 SE_DEV_ALIAS_LEN-1);
2105 return -EINVAL;
2106 }
2107
0fd97ccf 2108 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
3011684c
DC
2109 if (!read_bytes)
2110 return -EINVAL;
0fd97ccf
CH
2111 if (dev->dev_alias[read_bytes - 1] == '\n')
2112 dev->dev_alias[read_bytes - 1] = '\0';
0877eafd 2113
0fd97ccf 2114 dev->dev_flags |= DF_USING_ALIAS;
3011684c 2115
6708bb27 2116 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
c66ac9db 2117 config_item_name(&hba->hba_group.cg_item),
0fd97ccf
CH
2118 config_item_name(&dev->dev_group.cg_item),
2119 dev->dev_alias);
c66ac9db
NB
2120
2121 return read_bytes;
2122}
2123
2eafd729 2124static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
c66ac9db 2125{
2eafd729 2126 struct se_device *dev = to_device(item);
c66ac9db 2127
0fd97ccf 2128 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
c66ac9db
NB
2129 return 0;
2130
0fd97ccf 2131 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
c66ac9db
NB
2132}
2133
2eafd729
CH
2134static ssize_t target_dev_udev_path_store(struct config_item *item,
2135 const char *page, size_t count)
c66ac9db 2136{
2eafd729 2137 struct se_device *dev = to_device(item);
0fd97ccf 2138 struct se_hba *hba = dev->se_hba;
c66ac9db
NB
2139 ssize_t read_bytes;
2140
2141 if (count > (SE_UDEV_PATH_LEN-1)) {
6708bb27 2142 pr_err("udev_path count: %d exceeds"
c66ac9db
NB
2143 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
2144 SE_UDEV_PATH_LEN-1);
2145 return -EINVAL;
2146 }
2147
0fd97ccf 2148 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
c66ac9db 2149 "%s", page);
3011684c
DC
2150 if (!read_bytes)
2151 return -EINVAL;
0fd97ccf
CH
2152 if (dev->udev_path[read_bytes - 1] == '\n')
2153 dev->udev_path[read_bytes - 1] = '\0';
0877eafd 2154
0fd97ccf 2155 dev->dev_flags |= DF_USING_UDEV_PATH;
3011684c 2156
6708bb27 2157 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
c66ac9db 2158 config_item_name(&hba->hba_group.cg_item),
0fd97ccf
CH
2159 config_item_name(&dev->dev_group.cg_item),
2160 dev->udev_path);
c66ac9db
NB
2161
2162 return read_bytes;
2163}
2164
2eafd729 2165static ssize_t target_dev_enable_show(struct config_item *item, char *page)
64146db7 2166{
2eafd729 2167 struct se_device *dev = to_device(item);
64146db7 2168
cb0f32e1 2169 return snprintf(page, PAGE_SIZE, "%d\n", target_dev_configured(dev));
64146db7
AG
2170}
2171
2eafd729
CH
2172static ssize_t target_dev_enable_store(struct config_item *item,
2173 const char *page, size_t count)
c66ac9db 2174{
2eafd729 2175 struct se_device *dev = to_device(item);
c66ac9db 2176 char *ptr;
0fd97ccf 2177 int ret;
c66ac9db
NB
2178
2179 ptr = strstr(page, "1");
6708bb27
AG
2180 if (!ptr) {
2181 pr_err("For dev_enable ops, only valid value"
c66ac9db
NB
2182 " is \"1\"\n");
2183 return -EINVAL;
2184 }
c66ac9db 2185
0fd97ccf
CH
2186 ret = target_configure_device(dev);
2187 if (ret)
2188 return ret;
c66ac9db
NB
2189 return count;
2190}
2191
2eafd729 2192static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
c66ac9db 2193{
2eafd729 2194 struct se_device *dev = to_device(item);
c66ac9db
NB
2195 struct config_item *lu_ci;
2196 struct t10_alua_lu_gp *lu_gp;
2197 struct t10_alua_lu_gp_member *lu_gp_mem;
2198 ssize_t len = 0;
2199
c66ac9db 2200 lu_gp_mem = dev->dev_alua_lu_gp_mem;
c87fbd56
CH
2201 if (!lu_gp_mem)
2202 return 0;
c66ac9db
NB
2203
2204 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2205 lu_gp = lu_gp_mem->lu_gp;
6708bb27 2206 if (lu_gp) {
c66ac9db
NB
2207 lu_ci = &lu_gp->lu_gp_group.cg_item;
2208 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
2209 config_item_name(lu_ci), lu_gp->lu_gp_id);
2210 }
2211 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2212
2213 return len;
2214}
2215
2eafd729
CH
2216static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
2217 const char *page, size_t count)
c66ac9db 2218{
2eafd729 2219 struct se_device *dev = to_device(item);
0fd97ccf 2220 struct se_hba *hba = dev->se_hba;
c66ac9db
NB
2221 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
2222 struct t10_alua_lu_gp_member *lu_gp_mem;
2223 unsigned char buf[LU_GROUP_NAME_BUF];
2224 int move = 0;
2225
c87fbd56
CH
2226 lu_gp_mem = dev->dev_alua_lu_gp_mem;
2227 if (!lu_gp_mem)
9105bfc0 2228 return count;
c87fbd56 2229
c66ac9db 2230 if (count > LU_GROUP_NAME_BUF) {
6708bb27 2231 pr_err("ALUA LU Group Alias too large!\n");
c66ac9db
NB
2232 return -EINVAL;
2233 }
2234 memset(buf, 0, LU_GROUP_NAME_BUF);
2235 memcpy(buf, page, count);
2236 /*
2237 * Any ALUA logical unit alias besides "NULL" means we will be
2238 * making a new group association.
2239 */
2240 if (strcmp(strstrip(buf), "NULL")) {
2241 /*
2242 * core_alua_get_lu_gp_by_name() will increment reference to
2243 * struct t10_alua_lu_gp. This reference is released with
2244 * core_alua_get_lu_gp_by_name below().
2245 */
2246 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
6708bb27 2247 if (!lu_gp_new)
c66ac9db
NB
2248 return -ENODEV;
2249 }
c66ac9db
NB
2250
2251 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2252 lu_gp = lu_gp_mem->lu_gp;
6708bb27 2253 if (lu_gp) {
c66ac9db
NB
2254 /*
2255 * Clearing an existing lu_gp association, and replacing
2256 * with NULL
2257 */
6708bb27
AG
2258 if (!lu_gp_new) {
2259 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
c66ac9db
NB
2260 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2261 " %hu\n",
2262 config_item_name(&hba->hba_group.cg_item),
0fd97ccf 2263 config_item_name(&dev->dev_group.cg_item),
c66ac9db
NB
2264 config_item_name(&lu_gp->lu_gp_group.cg_item),
2265 lu_gp->lu_gp_id);
2266
2267 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2268 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2269
2270 return count;
2271 }
2272 /*
2273 * Removing existing association of lu_gp_mem with lu_gp
2274 */
2275 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2276 move = 1;
2277 }
2278 /*
2279 * Associate lu_gp_mem with lu_gp_new.
2280 */
2281 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2282 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2283
6708bb27 2284 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
c66ac9db
NB
2285 " core/alua/lu_gps/%s, ID: %hu\n",
2286 (move) ? "Moving" : "Adding",
2287 config_item_name(&hba->hba_group.cg_item),
0fd97ccf 2288 config_item_name(&dev->dev_group.cg_item),
c66ac9db
NB
2289 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2290 lu_gp_new->lu_gp_id);
2291
2292 core_alua_put_lu_gp_from_name(lu_gp_new);
2293 return count;
2294}
2295
2eafd729 2296static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
229d4f11 2297{
2eafd729 2298 struct se_device *dev = to_device(item);
229d4f11
HR
2299 struct t10_alua_lba_map *map;
2300 struct t10_alua_lba_map_member *mem;
2301 char *b = page;
2302 int bl = 0;
2303 char state;
2304
2305 spin_lock(&dev->t10_alua.lba_map_lock);
2306 if (!list_empty(&dev->t10_alua.lba_map_list))
2307 bl += sprintf(b + bl, "%u %u\n",
2308 dev->t10_alua.lba_map_segment_size,
2309 dev->t10_alua.lba_map_segment_multiplier);
2310 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2311 bl += sprintf(b + bl, "%llu %llu",
2312 map->lba_map_first_lba, map->lba_map_last_lba);
2313 list_for_each_entry(mem, &map->lba_map_mem_list,
2314 lba_map_mem_list) {
2315 switch (mem->lba_map_mem_alua_state) {
2316 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2317 state = 'O';
2318 break;
2319 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2320 state = 'A';
2321 break;
2322 case ALUA_ACCESS_STATE_STANDBY:
2323 state = 'S';
2324 break;
2325 case ALUA_ACCESS_STATE_UNAVAILABLE:
2326 state = 'U';
2327 break;
2328 default:
2329 state = '.';
2330 break;
2331 }
2332 bl += sprintf(b + bl, " %d:%c",
2333 mem->lba_map_mem_alua_pg_id, state);
2334 }
2335 bl += sprintf(b + bl, "\n");
2336 }
2337 spin_unlock(&dev->t10_alua.lba_map_lock);
2338 return bl;
2339}
2340
2eafd729
CH
2341static ssize_t target_dev_lba_map_store(struct config_item *item,
2342 const char *page, size_t count)
229d4f11 2343{
2eafd729 2344 struct se_device *dev = to_device(item);
229d4f11
HR
2345 struct t10_alua_lba_map *lba_map = NULL;
2346 struct list_head lba_list;
f0a8afec 2347 char *map_entries, *orig, *ptr;
229d4f11
HR
2348 char state;
2349 int pg_num = -1, pg;
2350 int ret = 0, num = 0, pg_id, alua_state;
2351 unsigned long start_lba = -1, end_lba = -1;
2352 unsigned long segment_size = -1, segment_mult = -1;
2353
f0a8afec 2354 orig = map_entries = kstrdup(page, GFP_KERNEL);
229d4f11
HR
2355 if (!map_entries)
2356 return -ENOMEM;
2357
2358 INIT_LIST_HEAD(&lba_list);
2359 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2360 if (!*ptr)
2361 continue;
2362
2363 if (num == 0) {
2364 if (sscanf(ptr, "%lu %lu\n",
2365 &segment_size, &segment_mult) != 2) {
2366 pr_err("Invalid line %d\n", num);
2367 ret = -EINVAL;
2368 break;
2369 }
2370 num++;
2371 continue;
2372 }
2373 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2374 pr_err("Invalid line %d\n", num);
2375 ret = -EINVAL;
2376 break;
2377 }
2378 ptr = strchr(ptr, ' ');
2379 if (!ptr) {
2380 pr_err("Invalid line %d, missing end lba\n", num);
2381 ret = -EINVAL;
2382 break;
2383 }
2384 ptr++;
2385 ptr = strchr(ptr, ' ');
2386 if (!ptr) {
2387 pr_err("Invalid line %d, missing state definitions\n",
2388 num);
2389 ret = -EINVAL;
2390 break;
2391 }
2392 ptr++;
2393 lba_map = core_alua_allocate_lba_map(&lba_list,
2394 start_lba, end_lba);
2395 if (IS_ERR(lba_map)) {
2396 ret = PTR_ERR(lba_map);
2397 break;
2398 }
2399 pg = 0;
2400 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2401 switch (state) {
2402 case 'O':
2403 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2404 break;
2405 case 'A':
2406 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2407 break;
2408 case 'S':
2409 alua_state = ALUA_ACCESS_STATE_STANDBY;
2410 break;
2411 case 'U':
2412 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2413 break;
2414 default:
2415 pr_err("Invalid ALUA state '%c'\n", state);
2416 ret = -EINVAL;
2417 goto out;
2418 }
2419
2420 ret = core_alua_allocate_lba_map_mem(lba_map,
2421 pg_id, alua_state);
2422 if (ret) {
2423 pr_err("Invalid target descriptor %d:%c "
2424 "at line %d\n",
2425 pg_id, state, num);
2426 break;
2427 }
2428 pg++;
2429 ptr = strchr(ptr, ' ');
2430 if (ptr)
2431 ptr++;
2432 else
2433 break;
2434 }
2435 if (pg_num == -1)
2436 pg_num = pg;
2437 else if (pg != pg_num) {
2438 pr_err("Only %d from %d port groups definitions "
2439 "at line %d\n", pg, pg_num, num);
2440 ret = -EINVAL;
2441 break;
2442 }
2443 num++;
2444 }
2445out:
2446 if (ret) {
2447 core_alua_free_lba_map(&lba_list);
2448 count = ret;
2449 } else
2450 core_alua_set_lba_map(dev, &lba_list,
2451 segment_size, segment_mult);
f0a8afec 2452 kfree(orig);
229d4f11
HR
2453 return count;
2454}
2455
2eafd729
CH
2456CONFIGFS_ATTR_RO(target_dev_, info);
2457CONFIGFS_ATTR_WO(target_dev_, control);
2458CONFIGFS_ATTR(target_dev_, alias);
2459CONFIGFS_ATTR(target_dev_, udev_path);
2460CONFIGFS_ATTR(target_dev_, enable);
2461CONFIGFS_ATTR(target_dev_, alua_lu_gp);
2462CONFIGFS_ATTR(target_dev_, lba_map);
229d4f11 2463
73112edc 2464static struct configfs_attribute *target_core_dev_attrs[] = {
2eafd729
CH
2465 &target_dev_attr_info,
2466 &target_dev_attr_control,
2467 &target_dev_attr_alias,
2468 &target_dev_attr_udev_path,
2469 &target_dev_attr_enable,
2470 &target_dev_attr_alua_lu_gp,
2471 &target_dev_attr_lba_map,
c66ac9db
NB
2472 NULL,
2473};
2474
2475static void target_core_dev_release(struct config_item *item)
2476{
0fd97ccf
CH
2477 struct config_group *dev_cg = to_config_group(item);
2478 struct se_device *dev =
2479 container_of(dev_cg, struct se_device, dev_group);
c66ac9db 2480
0fd97ccf 2481 target_free_device(dev);
c66ac9db
NB
2482}
2483
c17cd249
NB
2484/*
2485 * Used in target_core_fabric_configfs.c to verify valid se_device symlink
2486 * within target_fabric_port_link()
2487 */
2488struct configfs_item_operations target_core_dev_item_ops = {
c66ac9db 2489 .release = target_core_dev_release,
c66ac9db
NB
2490};
2491
73112edc 2492TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
c66ac9db 2493
73112edc 2494/* End functions for struct config_item_type tb_dev_cit */
c66ac9db
NB
2495
2496/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2497
2eafd729
CH
2498static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2499{
2500 return container_of(to_config_group(item), struct t10_alua_lu_gp,
2501 lu_gp_group);
2502}
c66ac9db 2503
2eafd729 2504static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
c66ac9db 2505{
2eafd729
CH
2506 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2507
6708bb27 2508 if (!lu_gp->lu_gp_valid_id)
c66ac9db 2509 return 0;
c66ac9db
NB
2510 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2511}
2512
2eafd729
CH
2513static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
2514 const char *page, size_t count)
c66ac9db 2515{
2eafd729 2516 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
c66ac9db
NB
2517 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2518 unsigned long lu_gp_id;
2519 int ret;
2520
57103d7f 2521 ret = kstrtoul(page, 0, &lu_gp_id);
c66ac9db 2522 if (ret < 0) {
57103d7f 2523 pr_err("kstrtoul() returned %d for"
c66ac9db 2524 " lu_gp_id\n", ret);
57103d7f 2525 return ret;
c66ac9db
NB
2526 }
2527 if (lu_gp_id > 0x0000ffff) {
6708bb27 2528 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
c66ac9db
NB
2529 " 0x0000ffff\n", lu_gp_id);
2530 return -EINVAL;
2531 }
2532
2533 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2534 if (ret < 0)
2535 return -EINVAL;
2536
6708bb27 2537 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
c66ac9db
NB
2538 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2539 config_item_name(&alua_lu_gp_cg->cg_item),
2540 lu_gp->lu_gp_id);
2541
2542 return count;
2543}
2544
2eafd729 2545static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
c66ac9db 2546{
2eafd729 2547 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
c66ac9db
NB
2548 struct se_device *dev;
2549 struct se_hba *hba;
c66ac9db
NB
2550 struct t10_alua_lu_gp_member *lu_gp_mem;
2551 ssize_t len = 0, cur_len;
2552 unsigned char buf[LU_GROUP_NAME_BUF];
2553
2554 memset(buf, 0, LU_GROUP_NAME_BUF);
2555
2556 spin_lock(&lu_gp->lu_gp_lock);
2557 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2558 dev = lu_gp_mem->lu_gp_mem_dev;
0fd97ccf 2559 hba = dev->se_hba;
c66ac9db
NB
2560
2561 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2562 config_item_name(&hba->hba_group.cg_item),
0fd97ccf 2563 config_item_name(&dev->dev_group.cg_item));
c66ac9db
NB
2564 cur_len++; /* Extra byte for NULL terminator */
2565
2566 if ((cur_len + len) > PAGE_SIZE) {
6708bb27 2567 pr_warn("Ran out of lu_gp_show_attr"
c66ac9db
NB
2568 "_members buffer\n");
2569 break;
2570 }
2571 memcpy(page+len, buf, cur_len);
2572 len += cur_len;
2573 }
2574 spin_unlock(&lu_gp->lu_gp_lock);
2575
2576 return len;
2577}
2578
2eafd729
CH
2579CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
2580CONFIGFS_ATTR_RO(target_lu_gp_, members);
c66ac9db
NB
2581
2582static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2eafd729
CH
2583 &target_lu_gp_attr_lu_gp_id,
2584 &target_lu_gp_attr_members,
c66ac9db
NB
2585 NULL,
2586};
2587
1f6fe7cb
NB
2588static void target_core_alua_lu_gp_release(struct config_item *item)
2589{
2590 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2591 struct t10_alua_lu_gp, lu_gp_group);
2592
2593 core_alua_free_lu_gp(lu_gp);
2594}
2595
c66ac9db 2596static struct configfs_item_operations target_core_alua_lu_gp_ops = {
1f6fe7cb 2597 .release = target_core_alua_lu_gp_release,
c66ac9db
NB
2598};
2599
ece550b5 2600static const struct config_item_type target_core_alua_lu_gp_cit = {
c66ac9db
NB
2601 .ct_item_ops = &target_core_alua_lu_gp_ops,
2602 .ct_attrs = target_core_alua_lu_gp_attrs,
2603 .ct_owner = THIS_MODULE,
2604};
2605
2606/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2607
2608/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2609
2610static struct config_group *target_core_alua_create_lu_gp(
2611 struct config_group *group,
2612 const char *name)
2613{
2614 struct t10_alua_lu_gp *lu_gp;
2615 struct config_group *alua_lu_gp_cg = NULL;
2616 struct config_item *alua_lu_gp_ci = NULL;
2617
2618 lu_gp = core_alua_allocate_lu_gp(name, 0);
2619 if (IS_ERR(lu_gp))
2620 return NULL;
2621
2622 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2623 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2624
2625 config_group_init_type_name(alua_lu_gp_cg, name,
2626 &target_core_alua_lu_gp_cit);
2627
6708bb27 2628 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
c66ac9db
NB
2629 " Group: core/alua/lu_gps/%s\n",
2630 config_item_name(alua_lu_gp_ci));
2631
2632 return alua_lu_gp_cg;
2633
2634}
2635
2636static void target_core_alua_drop_lu_gp(
2637 struct config_group *group,
2638 struct config_item *item)
2639{
2640 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2641 struct t10_alua_lu_gp, lu_gp_group);
2642
6708bb27 2643 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
c66ac9db
NB
2644 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2645 config_item_name(item), lu_gp->lu_gp_id);
1f6fe7cb
NB
2646 /*
2647 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2648 * -> target_core_alua_lu_gp_release()
2649 */
c66ac9db 2650 config_item_put(item);
c66ac9db
NB
2651}
2652
2653static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2654 .make_group = &target_core_alua_create_lu_gp,
2655 .drop_item = &target_core_alua_drop_lu_gp,
2656};
2657
ece550b5 2658static const struct config_item_type target_core_alua_lu_gps_cit = {
c66ac9db
NB
2659 .ct_item_ops = NULL,
2660 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2661 .ct_owner = THIS_MODULE,
2662};
2663
2664/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2665
2666/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2667
2eafd729
CH
2668static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
2669{
2670 return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
2671 tg_pt_gp_group);
2672}
c66ac9db 2673
2eafd729
CH
2674static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2675 char *page)
c66ac9db
NB
2676{
2677 return sprintf(page, "%d\n",
d19c4643 2678 to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
c66ac9db
NB
2679}
2680
2eafd729
CH
2681static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
2682 const char *page, size_t count)
c66ac9db 2683{
2eafd729 2684 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
0fd97ccf 2685 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
c66ac9db
NB
2686 unsigned long tmp;
2687 int new_state, ret;
2688
6708bb27 2689 if (!tg_pt_gp->tg_pt_gp_valid_id) {
125d0119 2690 pr_err("Unable to do implicit ALUA on non valid"
c66ac9db
NB
2691 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2692 return -EINVAL;
2693 }
cb0f32e1 2694 if (!target_dev_configured(dev)) {
f1453773
NB
2695 pr_err("Unable to set alua_access_state while device is"
2696 " not configured\n");
2697 return -ENODEV;
2698 }
c66ac9db 2699
57103d7f 2700 ret = kstrtoul(page, 0, &tmp);
c66ac9db 2701 if (ret < 0) {
6708bb27 2702 pr_err("Unable to extract new ALUA access state from"
c66ac9db 2703 " %s\n", page);
57103d7f 2704 return ret;
c66ac9db
NB
2705 }
2706 new_state = (int)tmp;
2707
125d0119
HR
2708 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2709 pr_err("Unable to process implicit configfs ALUA"
2710 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
c66ac9db
NB
2711 return -EINVAL;
2712 }
c66094bf
HR
2713 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2714 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2715 /* LBA DEPENDENT is only allowed with implicit ALUA */
2716 pr_err("Unable to process implicit configfs ALUA transition"
2717 " while explicit ALUA management is enabled\n");
2718 return -EINVAL;
2719 }
c66ac9db 2720
0fd97ccf 2721 ret = core_alua_do_port_transition(tg_pt_gp, dev,
c66ac9db
NB
2722 NULL, NULL, new_state, 0);
2723 return (!ret) ? count : -EINVAL;
2724}
2725
2eafd729
CH
2726static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2727 char *page)
c66ac9db 2728{
2eafd729 2729 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
c66ac9db
NB
2730 return sprintf(page, "%s\n",
2731 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2732}
2733
2eafd729
CH
2734static ssize_t target_tg_pt_gp_alua_access_status_store(
2735 struct config_item *item, const char *page, size_t count)
c66ac9db 2736{
2eafd729 2737 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
c66ac9db
NB
2738 unsigned long tmp;
2739 int new_status, ret;
2740
6708bb27
AG
2741 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2742 pr_err("Unable to do set ALUA access status on non"
c66ac9db
NB
2743 " valid tg_pt_gp ID: %hu\n",
2744 tg_pt_gp->tg_pt_gp_valid_id);
2745 return -EINVAL;
2746 }
2747
57103d7f 2748 ret = kstrtoul(page, 0, &tmp);
c66ac9db 2749 if (ret < 0) {
6708bb27 2750 pr_err("Unable to extract new ALUA access status"
c66ac9db 2751 " from %s\n", page);
57103d7f 2752 return ret;
c66ac9db
NB
2753 }
2754 new_status = (int)tmp;
2755
2756 if ((new_status != ALUA_STATUS_NONE) &&
125d0119
HR
2757 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2758 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
6708bb27 2759 pr_err("Illegal ALUA access status: 0x%02x\n",
c66ac9db
NB
2760 new_status);
2761 return -EINVAL;
2762 }
2763
2764 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2765 return count;
2766}
2767
2eafd729
CH
2768static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2769 char *page)
c66ac9db 2770{
2eafd729 2771 return core_alua_show_access_type(to_tg_pt_gp(item), page);
c66ac9db
NB
2772}
2773
2eafd729
CH
2774static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
2775 const char *page, size_t count)
c66ac9db 2776{
2eafd729 2777 return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
c66ac9db
NB
2778}
2779
2eafd729
CH
2780#define ALUA_SUPPORTED_STATE_ATTR(_name, _bit) \
2781static ssize_t target_tg_pt_gp_alua_support_##_name##_show( \
2782 struct config_item *item, char *p) \
b0a382c5 2783{ \
2eafd729
CH
2784 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
2785 return sprintf(p, "%d\n", \
2786 !!(t->tg_pt_gp_alua_supported_states & _bit)); \
2787} \
2788 \
2789static ssize_t target_tg_pt_gp_alua_support_##_name##_store( \
2790 struct config_item *item, const char *p, size_t c) \
b0a382c5 2791{ \
2eafd729 2792 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
b0a382c5
HR
2793 unsigned long tmp; \
2794 int ret; \
2795 \
2796 if (!t->tg_pt_gp_valid_id) { \
c0dcafd8 2797 pr_err("Unable to do set " #_name " ALUA state on non" \
b0a382c5
HR
2798 " valid tg_pt_gp ID: %hu\n", \
2799 t->tg_pt_gp_valid_id); \
2800 return -EINVAL; \
2801 } \
2802 \
2803 ret = kstrtoul(p, 0, &tmp); \
2804 if (ret < 0) { \
2805 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2806 return -EINVAL; \
2807 } \
2808 if (tmp > 1) { \
2809 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2810 return -EINVAL; \
2811 } \
1f0b030c 2812 if (tmp) \
2eafd729 2813 t->tg_pt_gp_alua_supported_states |= _bit; \
b0a382c5 2814 else \
2eafd729 2815 t->tg_pt_gp_alua_supported_states &= ~_bit; \
b0a382c5
HR
2816 \
2817 return c; \
2818}
2819
2eafd729
CH
2820ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
2821ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
2822ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
2823ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
2824ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
2825ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
2826ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
6be526c4 2827
2eafd729
CH
2828static ssize_t target_tg_pt_gp_alua_write_metadata_show(
2829 struct config_item *item, char *page)
c66ac9db 2830{
2eafd729
CH
2831 return sprintf(page, "%d\n",
2832 to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
c66ac9db
NB
2833}
2834
2eafd729
CH
2835static ssize_t target_tg_pt_gp_alua_write_metadata_store(
2836 struct config_item *item, const char *page, size_t count)
c66ac9db 2837{
2eafd729 2838 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
c66ac9db
NB
2839 unsigned long tmp;
2840 int ret;
2841
57103d7f 2842 ret = kstrtoul(page, 0, &tmp);
c66ac9db 2843 if (ret < 0) {
6708bb27 2844 pr_err("Unable to extract alua_write_metadata\n");
57103d7f 2845 return ret;
c66ac9db
NB
2846 }
2847
2848 if ((tmp != 0) && (tmp != 1)) {
6708bb27 2849 pr_err("Illegal value for alua_write_metadata:"
c66ac9db
NB
2850 " %lu\n", tmp);
2851 return -EINVAL;
2852 }
2853 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2854
2855 return count;
2856}
2857
2eafd729
CH
2858static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2859 char *page)
c66ac9db 2860{
2eafd729 2861 return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
c66ac9db
NB
2862}
2863
2eafd729
CH
2864static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
2865 const char *page, size_t count)
c66ac9db 2866{
2eafd729
CH
2867 return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
2868 count);
c66ac9db
NB
2869}
2870
2eafd729
CH
2871static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2872 char *page)
c66ac9db 2873{
2eafd729 2874 return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
c66ac9db
NB
2875}
2876
2eafd729
CH
2877static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
2878 const char *page, size_t count)
c66ac9db 2879{
2eafd729
CH
2880 return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
2881 count);
c66ac9db
NB
2882}
2883
2eafd729
CH
2884static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
2885 struct config_item *item, char *page)
5b9a4d72 2886{
2eafd729 2887 return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
5b9a4d72
NB
2888}
2889
2eafd729
CH
2890static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
2891 struct config_item *item, const char *page, size_t count)
5b9a4d72 2892{
2eafd729
CH
2893 return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
2894 count);
5b9a4d72
NB
2895}
2896
2eafd729
CH
2897static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
2898 char *page)
c66ac9db 2899{
2eafd729 2900 return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
c66ac9db
NB
2901}
2902
2eafd729
CH
2903static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
2904 const char *page, size_t count)
c66ac9db 2905{
2eafd729 2906 return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
c66ac9db
NB
2907}
2908
2eafd729
CH
2909static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2910 char *page)
c66ac9db 2911{
2eafd729
CH
2912 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2913
6708bb27 2914 if (!tg_pt_gp->tg_pt_gp_valid_id)
c66ac9db 2915 return 0;
c66ac9db
NB
2916 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2917}
2918
2eafd729
CH
2919static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
2920 const char *page, size_t count)
c66ac9db 2921{
2eafd729 2922 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
c66ac9db
NB
2923 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2924 unsigned long tg_pt_gp_id;
2925 int ret;
2926
57103d7f 2927 ret = kstrtoul(page, 0, &tg_pt_gp_id);
c66ac9db 2928 if (ret < 0) {
3d035237
HR
2929 pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
2930 page);
57103d7f 2931 return ret;
c66ac9db
NB
2932 }
2933 if (tg_pt_gp_id > 0x0000ffff) {
3d035237
HR
2934 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
2935 tg_pt_gp_id);
c66ac9db
NB
2936 return -EINVAL;
2937 }
2938
2939 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2940 if (ret < 0)
2941 return -EINVAL;
2942
6708bb27 2943 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
c66ac9db
NB
2944 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2945 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2946 tg_pt_gp->tg_pt_gp_id);
2947
2948 return count;
2949}
2950
2eafd729
CH
2951static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2952 char *page)
c66ac9db 2953{
2eafd729 2954 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
c66ac9db 2955 struct se_lun *lun;
c66ac9db
NB
2956 ssize_t len = 0, cur_len;
2957 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2958
2959 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2960
2961 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
adf653f9
CH
2962 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2963 lun_tg_pt_gp_link) {
2964 struct se_portal_group *tpg = lun->lun_tpg;
c66ac9db
NB
2965
2966 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
30c7ca93 2967 "/%s\n", tpg->se_tpg_tfo->fabric_name,
e3d6f909
AG
2968 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2969 tpg->se_tpg_tfo->tpg_get_tag(tpg),
c66ac9db
NB
2970 config_item_name(&lun->lun_group.cg_item));
2971 cur_len++; /* Extra byte for NULL terminator */
2972
2973 if ((cur_len + len) > PAGE_SIZE) {
6708bb27 2974 pr_warn("Ran out of lu_gp_show_attr"
c66ac9db
NB
2975 "_members buffer\n");
2976 break;
2977 }
2978 memcpy(page+len, buf, cur_len);
2979 len += cur_len;
2980 }
2981 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2982
2983 return len;
2984}
2985
2eafd729
CH
2986CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
2987CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
2988CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
2989CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
2990CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
2991CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
2992CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
2993CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
2994CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
2995CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
2996CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
2997CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
2998CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
2999CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
3000CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
3001CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
3002CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
c66ac9db
NB
3003
3004static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2eafd729
CH
3005 &target_tg_pt_gp_attr_alua_access_state,
3006 &target_tg_pt_gp_attr_alua_access_status,
3007 &target_tg_pt_gp_attr_alua_access_type,
3008 &target_tg_pt_gp_attr_alua_support_transitioning,
3009 &target_tg_pt_gp_attr_alua_support_offline,
3010 &target_tg_pt_gp_attr_alua_support_lba_dependent,
3011 &target_tg_pt_gp_attr_alua_support_unavailable,
3012 &target_tg_pt_gp_attr_alua_support_standby,
3013 &target_tg_pt_gp_attr_alua_support_active_nonoptimized,
3014 &target_tg_pt_gp_attr_alua_support_active_optimized,
3015 &target_tg_pt_gp_attr_alua_write_metadata,
3016 &target_tg_pt_gp_attr_nonop_delay_msecs,
3017 &target_tg_pt_gp_attr_trans_delay_msecs,
3018 &target_tg_pt_gp_attr_implicit_trans_secs,
3019 &target_tg_pt_gp_attr_preferred,
3020 &target_tg_pt_gp_attr_tg_pt_gp_id,
3021 &target_tg_pt_gp_attr_members,
c66ac9db
NB
3022 NULL,
3023};
3024
1f6fe7cb
NB
3025static void target_core_alua_tg_pt_gp_release(struct config_item *item)
3026{
3027 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3028 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3029
3030 core_alua_free_tg_pt_gp(tg_pt_gp);
3031}
3032
c66ac9db 3033static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
1f6fe7cb 3034 .release = target_core_alua_tg_pt_gp_release,
c66ac9db
NB
3035};
3036
ece550b5 3037static const struct config_item_type target_core_alua_tg_pt_gp_cit = {
c66ac9db
NB
3038 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
3039 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
3040 .ct_owner = THIS_MODULE,
3041};
3042
3043/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
3044
72aca57b 3045/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
c66ac9db
NB
3046
3047static struct config_group *target_core_alua_create_tg_pt_gp(
3048 struct config_group *group,
3049 const char *name)
3050{
3051 struct t10_alua *alua = container_of(group, struct t10_alua,
3052 alua_tg_pt_gps_group);
3053 struct t10_alua_tg_pt_gp *tg_pt_gp;
c66ac9db
NB
3054 struct config_group *alua_tg_pt_gp_cg = NULL;
3055 struct config_item *alua_tg_pt_gp_ci = NULL;
3056
0fd97ccf 3057 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
6708bb27 3058 if (!tg_pt_gp)
c66ac9db
NB
3059 return NULL;
3060
3061 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3062 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
3063
3064 config_group_init_type_name(alua_tg_pt_gp_cg, name,
3065 &target_core_alua_tg_pt_gp_cit);
3066
6708bb27 3067 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
c66ac9db
NB
3068 " Group: alua/tg_pt_gps/%s\n",
3069 config_item_name(alua_tg_pt_gp_ci));
3070
3071 return alua_tg_pt_gp_cg;
3072}
3073
3074static void target_core_alua_drop_tg_pt_gp(
3075 struct config_group *group,
3076 struct config_item *item)
3077{
3078 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3079 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3080
6708bb27 3081 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
c66ac9db
NB
3082 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
3083 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
1f6fe7cb
NB
3084 /*
3085 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
3086 * -> target_core_alua_tg_pt_gp_release().
3087 */
c66ac9db 3088 config_item_put(item);
c66ac9db
NB
3089}
3090
3091static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
3092 .make_group = &target_core_alua_create_tg_pt_gp,
3093 .drop_item = &target_core_alua_drop_tg_pt_gp,
3094};
3095
72aca57b 3096TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
c66ac9db 3097
72aca57b 3098/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
c66ac9db
NB
3099
3100/* Start functions for struct config_item_type target_core_alua_cit */
3101
3102/*
3103 * target_core_alua_cit is a ConfigFS group that lives under
3104 * /sys/kernel/config/target/core/alua. There are default groups
3105 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
3106 * target_core_alua_cit in target_core_init_configfs() below.
3107 */
ece550b5 3108static const struct config_item_type target_core_alua_cit = {
c66ac9db
NB
3109 .ct_item_ops = NULL,
3110 .ct_attrs = NULL,
3111 .ct_owner = THIS_MODULE,
3112};
3113
3114/* End functions for struct config_item_type target_core_alua_cit */
3115
d23ab570 3116/* Start functions for struct config_item_type tb_dev_stat_cit */
12d23384
NB
3117
3118static struct config_group *target_core_stat_mkdir(
3119 struct config_group *group,
3120 const char *name)
3121{
3122 return ERR_PTR(-ENOSYS);
3123}
3124
3125static void target_core_stat_rmdir(
3126 struct config_group *group,
3127 struct config_item *item)
3128{
3129 return;
3130}
3131
3132static struct configfs_group_operations target_core_stat_group_ops = {
3133 .make_group = &target_core_stat_mkdir,
3134 .drop_item = &target_core_stat_rmdir,
3135};
3136
d23ab570 3137TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
12d23384 3138
d23ab570 3139/* End functions for struct config_item_type tb_dev_stat_cit */
12d23384 3140
c66ac9db
NB
3141/* Start functions for struct config_item_type target_core_hba_cit */
3142
3143static struct config_group *target_core_make_subdev(
3144 struct config_group *group,
3145 const char *name)
3146{
3147 struct t10_alua_tg_pt_gp *tg_pt_gp;
c66ac9db
NB
3148 struct config_item *hba_ci = &group->cg_item;
3149 struct se_hba *hba = item_to_hba(hba_ci);
0a06d430 3150 struct target_backend *tb = hba->backend;
0fd97ccf 3151 struct se_device *dev;
12d23384 3152 int errno = -ENOMEM, ret;
c66ac9db 3153
12d23384
NB
3154 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
3155 if (ret)
3156 return ERR_PTR(ret);
c66ac9db 3157
0fd97ccf
CH
3158 dev = target_alloc_device(hba, name);
3159 if (!dev)
3160 goto out_unlock;
3161
1ae1602d 3162 config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
c66ac9db 3163
8dc31ff9
MC
3164 config_group_init_type_name(&dev->dev_action_group, "action",
3165 &tb->tb_dev_action_cit);
3166 configfs_add_default_group(&dev->dev_action_group, &dev->dev_group);
3167
0fd97ccf 3168 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
0a06d430 3169 &tb->tb_dev_attrib_cit);
1ae1602d
CH
3170 configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
3171
0fd97ccf 3172 config_group_init_type_name(&dev->dev_pr_group, "pr",
0a06d430 3173 &tb->tb_dev_pr_cit);
1ae1602d
CH
3174 configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
3175
0fd97ccf 3176 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
0a06d430 3177 &tb->tb_dev_wwn_cit);
1ae1602d
CH
3178 configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
3179 &dev->dev_group);
3180
0fd97ccf 3181 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
0a06d430 3182 "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
1ae1602d
CH
3183 configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
3184 &dev->dev_group);
3185
0fd97ccf 3186 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
0a06d430 3187 "statistics", &tb->tb_dev_stat_cit);
1ae1602d
CH
3188 configfs_add_default_group(&dev->dev_stat_grps.stat_group,
3189 &dev->dev_group);
12d23384 3190
c66ac9db 3191 /*
12d23384 3192 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
c66ac9db 3193 */
0fd97ccf 3194 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
6708bb27 3195 if (!tg_pt_gp)
1ae1602d 3196 goto out_free_device;
0fd97ccf 3197 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
c66ac9db 3198
c66ac9db
NB
3199 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
3200 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
1ae1602d
CH
3201 configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
3202 &dev->t10_alua.alua_tg_pt_gps_group);
3203
12d23384
NB
3204 /*
3205 * Add core/$HBA/$DEV/statistics/ default groups
3206 */
0fd97ccf 3207 target_stat_setup_dev_default_groups(dev);
c66ac9db
NB
3208
3209 mutex_unlock(&hba->hba_access_mutex);
1ae1602d 3210 return &dev->dev_group;
0fd97ccf 3211
0fd97ccf
CH
3212out_free_device:
3213 target_free_device(dev);
3214out_unlock:
c66ac9db 3215 mutex_unlock(&hba->hba_access_mutex);
12d23384 3216 return ERR_PTR(errno);
c66ac9db
NB
3217}
3218
3219static void target_core_drop_subdev(
3220 struct config_group *group,
3221 struct config_item *item)
3222{
0fd97ccf
CH
3223 struct config_group *dev_cg = to_config_group(item);
3224 struct se_device *dev =
3225 container_of(dev_cg, struct se_device, dev_group);
c66ac9db 3226 struct se_hba *hba;
c66ac9db 3227
0fd97ccf 3228 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
c66ac9db 3229
1f6fe7cb 3230 mutex_lock(&hba->hba_access_mutex);
c66ac9db 3231
1ae1602d
CH
3232 configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
3233 configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
12d23384 3234
1f6fe7cb
NB
3235 /*
3236 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
3237 * directly from target_core_alua_tg_pt_gp_release().
3238 */
0fd97ccf 3239 dev->t10_alua.default_tg_pt_gp = NULL;
c66ac9db 3240
1ae1602d
CH
3241 configfs_remove_default_groups(dev_cg);
3242
c66ac9db 3243 /*
0fd97ccf 3244 * se_dev is released from target_core_dev_item_ops->release()
c66ac9db 3245 */
1f6fe7cb 3246 config_item_put(item);
c66ac9db 3247 mutex_unlock(&hba->hba_access_mutex);
c66ac9db
NB
3248}
3249
3250static struct configfs_group_operations target_core_hba_group_ops = {
3251 .make_group = target_core_make_subdev,
3252 .drop_item = target_core_drop_subdev,
3253};
3254
c66ac9db 3255
2eafd729
CH
3256static inline struct se_hba *to_hba(struct config_item *item)
3257{
3258 return container_of(to_config_group(item), struct se_hba, hba_group);
3259}
c66ac9db 3260
2eafd729 3261static ssize_t target_hba_info_show(struct config_item *item, char *page)
c66ac9db 3262{
2eafd729
CH
3263 struct se_hba *hba = to_hba(item);
3264
c66ac9db 3265 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
0a06d430 3266 hba->hba_id, hba->backend->ops->name,
ce8dd25d 3267 TARGET_CORE_VERSION);
c66ac9db
NB
3268}
3269
2eafd729 3270static ssize_t target_hba_mode_show(struct config_item *item, char *page)
c66ac9db 3271{
2eafd729 3272 struct se_hba *hba = to_hba(item);
c66ac9db
NB
3273 int hba_mode = 0;
3274
3275 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3276 hba_mode = 1;
3277
3278 return sprintf(page, "%d\n", hba_mode);
3279}
3280
2eafd729
CH
3281static ssize_t target_hba_mode_store(struct config_item *item,
3282 const char *page, size_t count)
c66ac9db 3283{
2eafd729 3284 struct se_hba *hba = to_hba(item);
c66ac9db
NB
3285 unsigned long mode_flag;
3286 int ret;
3287
0a06d430 3288 if (hba->backend->ops->pmode_enable_hba == NULL)
c66ac9db
NB
3289 return -EINVAL;
3290
57103d7f 3291 ret = kstrtoul(page, 0, &mode_flag);
c66ac9db 3292 if (ret < 0) {
6708bb27 3293 pr_err("Unable to extract hba mode flag: %d\n", ret);
57103d7f 3294 return ret;
c66ac9db
NB
3295 }
3296
0fd97ccf 3297 if (hba->dev_count) {
6708bb27 3298 pr_err("Unable to set hba_mode with active devices\n");
c66ac9db
NB
3299 return -EINVAL;
3300 }
c66ac9db 3301
0a06d430 3302 ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
c66ac9db
NB
3303 if (ret < 0)
3304 return -EINVAL;
3305 if (ret > 0)
3306 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3307 else if (ret == 0)
3308 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3309
3310 return count;
3311}
3312
2eafd729
CH
3313CONFIGFS_ATTR_RO(target_, hba_info);
3314CONFIGFS_ATTR(target_, hba_mode);
c66ac9db 3315
1f6fe7cb
NB
3316static void target_core_hba_release(struct config_item *item)
3317{
3318 struct se_hba *hba = container_of(to_config_group(item),
3319 struct se_hba, hba_group);
3320 core_delete_hba(hba);
3321}
3322
c66ac9db 3323static struct configfs_attribute *target_core_hba_attrs[] = {
2eafd729
CH
3324 &target_attr_hba_info,
3325 &target_attr_hba_mode,
c66ac9db
NB
3326 NULL,
3327};
3328
3329static struct configfs_item_operations target_core_hba_item_ops = {
1f6fe7cb 3330 .release = target_core_hba_release,
c66ac9db
NB
3331};
3332
ece550b5 3333static const struct config_item_type target_core_hba_cit = {
c66ac9db
NB
3334 .ct_item_ops = &target_core_hba_item_ops,
3335 .ct_group_ops = &target_core_hba_group_ops,
3336 .ct_attrs = target_core_hba_attrs,
3337 .ct_owner = THIS_MODULE,
3338};
3339
3340static struct config_group *target_core_call_addhbatotarget(
3341 struct config_group *group,
3342 const char *name)
3343{
3344 char *se_plugin_str, *str, *str2;
3345 struct se_hba *hba;
3346 char buf[TARGET_CORE_NAME_MAX_LEN];
3347 unsigned long plugin_dep_id = 0;
3348 int ret;
3349
3350 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
60d645a4 3351 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
6708bb27 3352 pr_err("Passed *name strlen(): %d exceeds"
c66ac9db
NB
3353 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3354 TARGET_CORE_NAME_MAX_LEN);
3355 return ERR_PTR(-ENAMETOOLONG);
3356 }
3357 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3358
3359 str = strstr(buf, "_");
6708bb27
AG
3360 if (!str) {
3361 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
c66ac9db
NB
3362 return ERR_PTR(-EINVAL);
3363 }
3364 se_plugin_str = buf;
3365 /*
3366 * Special case for subsystem plugins that have "_" in their names.
3367 * Namely rd_direct and rd_mcp..
3368 */
3369 str2 = strstr(str+1, "_");
6708bb27 3370 if (str2) {
c66ac9db
NB
3371 *str2 = '\0'; /* Terminate for *se_plugin_str */
3372 str2++; /* Skip to start of plugin dependent ID */
3373 str = str2;
3374 } else {
3375 *str = '\0'; /* Terminate for *se_plugin_str */
3376 str++; /* Skip to start of plugin dependent ID */
3377 }
3378
57103d7f 3379 ret = kstrtoul(str, 0, &plugin_dep_id);
c66ac9db 3380 if (ret < 0) {
57103d7f 3381 pr_err("kstrtoul() returned %d for"
c66ac9db 3382 " plugin_dep_id\n", ret);
57103d7f 3383 return ERR_PTR(ret);
c66ac9db
NB
3384 }
3385 /*
3386 * Load up TCM subsystem plugins if they have not already been loaded.
3387 */
dbc5623e 3388 transport_subsystem_check_init();
c66ac9db
NB
3389
3390 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3391 if (IS_ERR(hba))
3392 return ERR_CAST(hba);
3393
3394 config_group_init_type_name(&hba->hba_group, name,
3395 &target_core_hba_cit);
3396
3397 return &hba->hba_group;
3398}
3399
3400static void target_core_call_delhbafromtarget(
3401 struct config_group *group,
3402 struct config_item *item)
3403{
1f6fe7cb
NB
3404 /*
3405 * core_delete_hba() is called from target_core_hba_item_ops->release()
3406 * -> target_core_hba_release()
3407 */
c66ac9db 3408 config_item_put(item);
c66ac9db
NB
3409}
3410
3411static struct configfs_group_operations target_core_group_ops = {
3412 .make_group = target_core_call_addhbatotarget,
3413 .drop_item = target_core_call_delhbafromtarget,
3414};
3415
ece550b5 3416static const struct config_item_type target_core_cit = {
c66ac9db
NB
3417 .ct_item_ops = NULL,
3418 .ct_group_ops = &target_core_group_ops,
3419 .ct_attrs = NULL,
3420 .ct_owner = THIS_MODULE,
3421};
3422
3423/* Stop functions for struct config_item_type target_core_hba_cit */
3424
0a06d430 3425void target_setup_backend_cits(struct target_backend *tb)
73112edc 3426{
0a06d430 3427 target_core_setup_dev_cit(tb);
8dc31ff9 3428 target_core_setup_dev_action_cit(tb);
0a06d430
CH
3429 target_core_setup_dev_attrib_cit(tb);
3430 target_core_setup_dev_pr_cit(tb);
3431 target_core_setup_dev_wwn_cit(tb);
3432 target_core_setup_dev_alua_tg_pt_gps_cit(tb);
3433 target_core_setup_dev_stat_cit(tb);
73112edc 3434}
73112edc 3435
78a6295c
LD
3436static void target_init_dbroot(void)
3437{
3438 struct file *fp;
3439
3440 snprintf(db_root_stage, DB_ROOT_LEN, DB_ROOT_PREFERRED);
3441 fp = filp_open(db_root_stage, O_RDONLY, 0);
3442 if (IS_ERR(fp)) {
3443 pr_err("db_root: cannot open: %s\n", db_root_stage);
3444 return;
3445 }
3446 if (!S_ISDIR(file_inode(fp)->i_mode)) {
3447 filp_close(fp, NULL);
3448 pr_err("db_root: not a valid directory: %s\n", db_root_stage);
3449 return;
3450 }
3451 filp_close(fp, NULL);
3452
3453 strncpy(db_root, db_root_stage, DB_ROOT_LEN);
3454 pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
3455}
3456
54550fab 3457static int __init target_core_init_configfs(void)
c66ac9db 3458{
d588cf8f 3459 struct configfs_subsystem *subsys = &target_core_fabrics;
c66ac9db
NB
3460 struct t10_alua_lu_gp *lu_gp;
3461 int ret;
3462
6708bb27 3463 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
c66ac9db
NB
3464 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3465 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3466
c66ac9db
NB
3467 config_group_init(&subsys->su_group);
3468 mutex_init(&subsys->su_mutex);
3469
e3d6f909 3470 ret = init_se_kmem_caches();
c66ac9db 3471 if (ret < 0)
e3d6f909 3472 return ret;
c66ac9db
NB
3473 /*
3474 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3475 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3476 */
1ae1602d
CH
3477 config_group_init_type_name(&target_core_hbagroup, "core",
3478 &target_core_cit);
3479 configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
c66ac9db 3480
c66ac9db
NB
3481 /*
3482 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3483 */
1ae1602d
CH
3484 config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
3485 configfs_add_default_group(&alua_group, &target_core_hbagroup);
3486
c66ac9db
NB
3487 /*
3488 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3489 * groups under /sys/kernel/config/target/core/alua/
3490 */
1ae1602d
CH
3491 config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
3492 &target_core_alua_lu_gps_cit);
3493 configfs_add_default_group(&alua_lu_gps_group, &alua_group);
c66ac9db 3494
c66ac9db
NB
3495 /*
3496 * Add core/alua/lu_gps/default_lu_gp
3497 */
3498 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
37bb7899
PST
3499 if (IS_ERR(lu_gp)) {
3500 ret = -ENOMEM;
c66ac9db 3501 goto out_global;
37bb7899 3502 }
c66ac9db 3503
c66ac9db
NB
3504 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3505 &target_core_alua_lu_gp_cit);
1ae1602d
CH
3506 configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
3507
e3d6f909 3508 default_lu_gp = lu_gp;
1ae1602d 3509
c66ac9db
NB
3510 /*
3511 * Register the target_core_mod subsystem with configfs.
3512 */
3513 ret = configfs_register_subsystem(subsys);
3514 if (ret < 0) {
6708bb27 3515 pr_err("Error %d while registering subsystem %s\n",
c66ac9db
NB
3516 ret, subsys->su_group.cg_item.ci_namebuf);
3517 goto out_global;
3518 }
6708bb27 3519 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
ce8dd25d 3520 " Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
c66ac9db
NB
3521 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3522 /*
3523 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3524 */
3525 ret = rd_module_init();
3526 if (ret < 0)
3527 goto out;
3528
0d0f9dfb
RD
3529 ret = core_dev_setup_virtual_lun0();
3530 if (ret < 0)
c66ac9db
NB
3531 goto out;
3532
f99715ac
NB
3533 ret = target_xcopy_setup_pt();
3534 if (ret < 0)
3535 goto out;
3536
78a6295c
LD
3537 target_init_dbroot();
3538
c66ac9db
NB
3539 return 0;
3540
3541out:
3542 configfs_unregister_subsystem(subsys);
c66ac9db
NB
3543 core_dev_release_virtual_lun0();
3544 rd_module_exit();
3545out_global:
e3d6f909
AG
3546 if (default_lu_gp) {
3547 core_alua_free_lu_gp(default_lu_gp);
3548 default_lu_gp = NULL;
c66ac9db 3549 }
e3d6f909
AG
3550 release_se_kmem_caches();
3551 return ret;
c66ac9db
NB
3552}
3553
54550fab 3554static void __exit target_core_exit_configfs(void)
c66ac9db 3555{
1ae1602d
CH
3556 configfs_remove_default_groups(&alua_lu_gps_group);
3557 configfs_remove_default_groups(&alua_group);
3558 configfs_remove_default_groups(&target_core_hbagroup);
c66ac9db 3559
7c2bf6e9
NB
3560 /*
3561 * We expect subsys->su_group.default_groups to be released
3562 * by configfs subsystem provider logic..
3563 */
d588cf8f 3564 configfs_unregister_subsystem(&target_core_fabrics);
c66ac9db 3565
e3d6f909
AG
3566 core_alua_free_lu_gp(default_lu_gp);
3567 default_lu_gp = NULL;
7c2bf6e9 3568
6708bb27 3569 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
c66ac9db
NB
3570 " Infrastructure\n");
3571
c66ac9db
NB
3572 core_dev_release_virtual_lun0();
3573 rd_module_exit();
f99715ac 3574 target_xcopy_release_pt();
e3d6f909 3575 release_se_kmem_caches();
c66ac9db
NB
3576}
3577
3578MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3579MODULE_AUTHOR("nab@Linux-iSCSI.org");
3580MODULE_LICENSE("GPL");
3581
3582module_init(target_core_init_configfs);
3583module_exit(target_core_exit_configfs);