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