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