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