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