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