blob: 0cf41ad54bbf7a43c1a1f2e918020c6fbf026fd4 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_configfs.c
3 *
4 * This file contains ConfigFS logic for the Generic Target Engine project.
5 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07006 * (c) Copyright 2008-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08007 *
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>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080025#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>
Andy Grovere3d6f902011-07-19 08:55:10 +000038#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080039
40#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050041#include <target/target_core_backend.h>
42#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080043#include <target/target_core_fabric_configfs.h>
44#include <target/target_core_configfs.h>
45#include <target/configfs_macros.h>
46
Christoph Hellwige26d99a2011-11-14 12:30:30 -050047#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080048#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080049#include "target_core_pr.h"
50#include "target_core_rd.h"
Nicholas Bellingerf99715a2013-08-22 12:48:53 -070051#include "target_core_xcopy.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080052
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080053#define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
54static void target_core_setup_##_name##_cit(struct se_subsystem_api *sa) \
55{ \
56 struct target_backend_cits *tbc = &sa->tb_cits; \
57 struct config_item_type *cit = &tbc->tb_##_name##_cit; \
58 \
59 cit->ct_item_ops = _item_ops; \
60 cit->ct_group_ops = _group_ops; \
61 cit->ct_attrs = _attrs; \
62 cit->ct_owner = sa->owner; \
63 pr_debug("Setup generic %s\n", __stringify(_name)); \
64}
65
Andy Grovere3d6f902011-07-19 08:55:10 +000066extern struct t10_alua_lu_gp *default_lu_gp;
67
Roland Dreierd0f474e2012-01-12 10:41:18 -080068static LIST_HEAD(g_tf_list);
69static DEFINE_MUTEX(g_tf_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080070
71struct target_core_configfs_attribute {
72 struct configfs_attribute attr;
73 ssize_t (*show)(void *, char *);
74 ssize_t (*store)(void *, const char *, size_t);
75};
76
Andy Grovere3d6f902011-07-19 08:55:10 +000077static struct config_group target_core_hbagroup;
78static struct config_group alua_group;
79static struct config_group alua_lu_gps_group;
80
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080081static inline struct se_hba *
82item_to_hba(struct config_item *item)
83{
84 return container_of(to_config_group(item), struct se_hba, hba_group);
85}
86
87/*
88 * Attributes for /sys/kernel/config/target/
89 */
90static ssize_t target_core_attr_show(struct config_item *item,
91 struct configfs_attribute *attr,
92 char *page)
93{
94 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
95 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
96 utsname()->sysname, utsname()->machine);
97}
98
99static struct configfs_item_operations target_core_fabric_item_ops = {
100 .show_attribute = target_core_attr_show,
101};
102
103static struct configfs_attribute target_core_item_attr_version = {
104 .ca_owner = THIS_MODULE,
105 .ca_name = "version",
106 .ca_mode = S_IRUGO,
107};
108
109static struct target_fabric_configfs *target_core_get_fabric(
110 const char *name)
111{
112 struct target_fabric_configfs *tf;
113
Andy Grover6708bb22011-06-08 10:36:43 -0700114 if (!name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800115 return NULL;
116
117 mutex_lock(&g_tf_lock);
118 list_for_each_entry(tf, &g_tf_list, tf_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700119 if (!strcmp(tf->tf_name, name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800120 atomic_inc(&tf->tf_access_cnt);
121 mutex_unlock(&g_tf_lock);
122 return tf;
123 }
124 }
125 mutex_unlock(&g_tf_lock);
126
127 return NULL;
128}
129
130/*
131 * Called from struct target_core_group_ops->make_group()
132 */
133static struct config_group *target_core_register_fabric(
134 struct config_group *group,
135 const char *name)
136{
137 struct target_fabric_configfs *tf;
138 int ret;
139
Andy Grover6708bb22011-06-08 10:36:43 -0700140 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800141 " %s\n", group, name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800142
143 tf = target_core_get_fabric(name);
Andy Grover6708bb22011-06-08 10:36:43 -0700144 if (!tf) {
Roland Dreiere7b7af62014-11-14 12:54:36 -0800145 pr_err("target_core_register_fabric() trying autoload for %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800146 name);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800147
148 /*
149 * Below are some hardcoded request_module() calls to automatically
150 * local fabric modules when the following is called:
151 *
152 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
153 *
154 * Note that this does not limit which TCM fabric module can be
155 * registered, but simply provids auto loading logic for modules with
156 * mkdir(2) system calls with known TCM fabric modules.
157 */
158
159 if (!strncmp(name, "iscsi", 5)) {
160 /*
161 * Automatically load the LIO Target fabric module when the
162 * following is called:
163 *
164 * mkdir -p $CONFIGFS/target/iscsi
165 */
166 ret = request_module("iscsi_target_mod");
167 if (ret < 0) {
168 pr_err("request_module() failed for"
169 " iscsi_target_mod.ko: %d\n", ret);
170 return ERR_PTR(-EINVAL);
171 }
172 } else if (!strncmp(name, "loopback", 8)) {
173 /*
174 * Automatically load the tcm_loop fabric module when the
175 * following is called:
176 *
177 * mkdir -p $CONFIGFS/target/loopback
178 */
179 ret = request_module("tcm_loop");
180 if (ret < 0) {
181 pr_err("request_module() failed for"
182 " tcm_loop.ko: %d\n", ret);
183 return ERR_PTR(-EINVAL);
184 }
185 }
186
187 tf = target_core_get_fabric(name);
188 }
189
190 if (!tf) {
191 pr_err("target_core_get_fabric() failed for %s\n",
192 name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800193 return ERR_PTR(-EINVAL);
194 }
Andy Grover6708bb22011-06-08 10:36:43 -0700195 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800196 " %s\n", tf->tf_name);
197 /*
198 * On a successful target_core_get_fabric() look, the returned
199 * struct target_fabric_configfs *tf will contain a usage reference.
200 */
Andy Grover6708bb22011-06-08 10:36:43 -0700201 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
Andy Groverd80e224d2013-10-09 11:05:56 -0700202 &tf->tf_cit_tmpl.tfc_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800203
204 tf->tf_group.default_groups = tf->tf_default_groups;
205 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
206 tf->tf_group.default_groups[1] = NULL;
207
208 config_group_init_type_name(&tf->tf_group, name,
Andy Groverd80e224d2013-10-09 11:05:56 -0700209 &tf->tf_cit_tmpl.tfc_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800210 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
Andy Groverd80e224d2013-10-09 11:05:56 -0700211 &tf->tf_cit_tmpl.tfc_discovery_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800212
Andy Grover6708bb22011-06-08 10:36:43 -0700213 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214 " %s\n", tf->tf_group.cg_item.ci_name);
215 /*
216 * Setup tf_ops.tf_subsys pointer for usage with configfs_depend_item()
217 */
218 tf->tf_ops.tf_subsys = tf->tf_subsys;
219 tf->tf_fabric = &tf->tf_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -0700220 pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800221 " for %s\n", name);
222
223 return &tf->tf_group;
224}
225
226/*
227 * Called from struct target_core_group_ops->drop_item()
228 */
229static void target_core_deregister_fabric(
230 struct config_group *group,
231 struct config_item *item)
232{
233 struct target_fabric_configfs *tf = container_of(
234 to_config_group(item), struct target_fabric_configfs, tf_group);
235 struct config_group *tf_group;
236 struct config_item *df_item;
237 int i;
238
Andy Grover6708bb22011-06-08 10:36:43 -0700239 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800240 " tf list\n", config_item_name(item));
241
Andy Grover6708bb22011-06-08 10:36:43 -0700242 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800243 " %s\n", tf->tf_name);
244 atomic_dec(&tf->tf_access_cnt);
245
Andy Grover6708bb22011-06-08 10:36:43 -0700246 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800247 " tf->tf_fabric for %s\n", tf->tf_name);
248 tf->tf_fabric = NULL;
249
Andy Grover6708bb22011-06-08 10:36:43 -0700250 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800251 " %s\n", config_item_name(item));
252
253 tf_group = &tf->tf_group;
254 for (i = 0; tf_group->default_groups[i]; i++) {
255 df_item = &tf_group->default_groups[i]->cg_item;
256 tf_group->default_groups[i] = NULL;
257 config_item_put(df_item);
258 }
259 config_item_put(item);
260}
261
262static struct configfs_group_operations target_core_fabric_group_ops = {
263 .make_group = &target_core_register_fabric,
264 .drop_item = &target_core_deregister_fabric,
265};
266
267/*
268 * All item attributes appearing in /sys/kernel/target/ appear here.
269 */
270static struct configfs_attribute *target_core_fabric_item_attrs[] = {
271 &target_core_item_attr_version,
272 NULL,
273};
274
275/*
276 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
277 */
278static struct config_item_type target_core_fabrics_item = {
279 .ct_item_ops = &target_core_fabric_item_ops,
280 .ct_group_ops = &target_core_fabric_group_ops,
281 .ct_attrs = target_core_fabric_item_attrs,
282 .ct_owner = THIS_MODULE,
283};
284
285static struct configfs_subsystem target_core_fabrics = {
286 .su_group = {
287 .cg_item = {
288 .ci_namebuf = "target",
289 .ci_type = &target_core_fabrics_item,
290 },
291 },
292};
293
Nicholas Bellinger56d128f2013-08-22 11:32:31 -0700294struct configfs_subsystem *target_core_subsystem[] = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800295 &target_core_fabrics,
296 NULL,
297};
298
299/*##############################################################################
300// Start functions called by external Target Fabrics Modules
301//############################################################################*/
302
303/*
304 * First function called by fabric modules to:
305 *
306 * 1) Allocate a struct target_fabric_configfs and save the *fabric_cit pointer.
307 * 2) Add struct target_fabric_configfs to g_tf_list
308 * 3) Return struct target_fabric_configfs to fabric module to be passed
309 * into target_fabric_configfs_register().
310 */
311struct target_fabric_configfs *target_fabric_configfs_init(
312 struct module *fabric_mod,
313 const char *name)
314{
315 struct target_fabric_configfs *tf;
316
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800317 if (!(name)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700318 pr_err("Unable to locate passed fabric name\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000319 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800320 }
Dan Carpenter60d645a2011-06-15 10:03:05 -0700321 if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700322 pr_err("Passed name: %s exceeds TARGET_FABRIC"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800323 "_NAME_SIZE\n", name);
Andy Grovere3d6f902011-07-19 08:55:10 +0000324 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800325 }
326
327 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700328 if (!tf)
Andy Grovere3d6f902011-07-19 08:55:10 +0000329 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800330
331 INIT_LIST_HEAD(&tf->tf_list);
332 atomic_set(&tf->tf_access_cnt, 0);
333 /*
334 * Setup the default generic struct config_item_type's (cits) in
335 * struct target_fabric_configfs->tf_cit_tmpl
336 */
337 tf->tf_module = fabric_mod;
338 target_fabric_setup_cits(tf);
339
340 tf->tf_subsys = target_core_subsystem[0];
341 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
342
343 mutex_lock(&g_tf_lock);
344 list_add_tail(&tf->tf_list, &g_tf_list);
345 mutex_unlock(&g_tf_lock);
346
Andy Grover6708bb22011-06-08 10:36:43 -0700347 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800348 ">>>>>>>>>>>>>>\n");
Andy Grover6708bb22011-06-08 10:36:43 -0700349 pr_debug("Initialized struct target_fabric_configfs: %p for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800350 " %s\n", tf, tf->tf_name);
351 return tf;
352}
353EXPORT_SYMBOL(target_fabric_configfs_init);
354
355/*
356 * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
357 */
358void target_fabric_configfs_free(
359 struct target_fabric_configfs *tf)
360{
361 mutex_lock(&g_tf_lock);
362 list_del(&tf->tf_list);
363 mutex_unlock(&g_tf_lock);
364
365 kfree(tf);
366}
367EXPORT_SYMBOL(target_fabric_configfs_free);
368
369/*
370 * Perform a sanity check of the passed tf->tf_ops before completing
371 * TCM fabric module registration.
372 */
373static int target_fabric_tf_ops_check(
374 struct target_fabric_configfs *tf)
375{
376 struct target_core_fabric_ops *tfo = &tf->tf_ops;
377
Andy Grover6708bb22011-06-08 10:36:43 -0700378 if (!tfo->get_fabric_name) {
379 pr_err("Missing tfo->get_fabric_name()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800380 return -EINVAL;
381 }
Andy Grover6708bb22011-06-08 10:36:43 -0700382 if (!tfo->get_fabric_proto_ident) {
383 pr_err("Missing tfo->get_fabric_proto_ident()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800384 return -EINVAL;
385 }
Andy Grover6708bb22011-06-08 10:36:43 -0700386 if (!tfo->tpg_get_wwn) {
387 pr_err("Missing tfo->tpg_get_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800388 return -EINVAL;
389 }
Andy Grover6708bb22011-06-08 10:36:43 -0700390 if (!tfo->tpg_get_tag) {
391 pr_err("Missing tfo->tpg_get_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800392 return -EINVAL;
393 }
Andy Grover6708bb22011-06-08 10:36:43 -0700394 if (!tfo->tpg_get_default_depth) {
395 pr_err("Missing tfo->tpg_get_default_depth()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800396 return -EINVAL;
397 }
Andy Grover6708bb22011-06-08 10:36:43 -0700398 if (!tfo->tpg_get_pr_transport_id) {
399 pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800400 return -EINVAL;
401 }
Andy Grover6708bb22011-06-08 10:36:43 -0700402 if (!tfo->tpg_get_pr_transport_id_len) {
403 pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800404 return -EINVAL;
405 }
Andy Grover6708bb22011-06-08 10:36:43 -0700406 if (!tfo->tpg_check_demo_mode) {
407 pr_err("Missing tfo->tpg_check_demo_mode()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800408 return -EINVAL;
409 }
Andy Grover6708bb22011-06-08 10:36:43 -0700410 if (!tfo->tpg_check_demo_mode_cache) {
411 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800412 return -EINVAL;
413 }
Andy Grover6708bb22011-06-08 10:36:43 -0700414 if (!tfo->tpg_check_demo_mode_write_protect) {
415 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800416 return -EINVAL;
417 }
Andy Grover6708bb22011-06-08 10:36:43 -0700418 if (!tfo->tpg_check_prod_mode_write_protect) {
419 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800420 return -EINVAL;
421 }
Andy Grover6708bb22011-06-08 10:36:43 -0700422 if (!tfo->tpg_alloc_fabric_acl) {
423 pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800424 return -EINVAL;
425 }
Andy Grover6708bb22011-06-08 10:36:43 -0700426 if (!tfo->tpg_release_fabric_acl) {
427 pr_err("Missing tfo->tpg_release_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800428 return -EINVAL;
429 }
Andy Grover6708bb22011-06-08 10:36:43 -0700430 if (!tfo->tpg_get_inst_index) {
431 pr_err("Missing tfo->tpg_get_inst_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800432 return -EINVAL;
433 }
Christoph Hellwig35462972011-05-31 23:56:57 -0400434 if (!tfo->release_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700435 pr_err("Missing tfo->release_cmd()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800436 return -EINVAL;
437 }
Andy Grover6708bb22011-06-08 10:36:43 -0700438 if (!tfo->shutdown_session) {
439 pr_err("Missing tfo->shutdown_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800440 return -EINVAL;
441 }
Andy Grover6708bb22011-06-08 10:36:43 -0700442 if (!tfo->close_session) {
443 pr_err("Missing tfo->close_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800444 return -EINVAL;
445 }
Andy Grover6708bb22011-06-08 10:36:43 -0700446 if (!tfo->sess_get_index) {
447 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800448 return -EINVAL;
449 }
Andy Grover6708bb22011-06-08 10:36:43 -0700450 if (!tfo->write_pending) {
451 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800452 return -EINVAL;
453 }
Andy Grover6708bb22011-06-08 10:36:43 -0700454 if (!tfo->write_pending_status) {
455 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800456 return -EINVAL;
457 }
Andy Grover6708bb22011-06-08 10:36:43 -0700458 if (!tfo->set_default_node_attributes) {
459 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460 return -EINVAL;
461 }
Andy Grover6708bb22011-06-08 10:36:43 -0700462 if (!tfo->get_task_tag) {
463 pr_err("Missing tfo->get_task_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800464 return -EINVAL;
465 }
Andy Grover6708bb22011-06-08 10:36:43 -0700466 if (!tfo->get_cmd_state) {
467 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800468 return -EINVAL;
469 }
Andy Grover6708bb22011-06-08 10:36:43 -0700470 if (!tfo->queue_data_in) {
471 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800472 return -EINVAL;
473 }
Andy Grover6708bb22011-06-08 10:36:43 -0700474 if (!tfo->queue_status) {
475 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800476 return -EINVAL;
477 }
Andy Grover6708bb22011-06-08 10:36:43 -0700478 if (!tfo->queue_tm_rsp) {
479 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800480 return -EINVAL;
481 }
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700482 if (!tfo->aborted_task) {
483 pr_err("Missing tfo->aborted_task()\n");
484 return -EINVAL;
485 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800486 /*
487 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
488 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
489 * target_core_fabric_configfs.c WWN+TPG group context code.
490 */
Andy Grover6708bb22011-06-08 10:36:43 -0700491 if (!tfo->fabric_make_wwn) {
492 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800493 return -EINVAL;
494 }
Andy Grover6708bb22011-06-08 10:36:43 -0700495 if (!tfo->fabric_drop_wwn) {
496 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800497 return -EINVAL;
498 }
Andy Grover6708bb22011-06-08 10:36:43 -0700499 if (!tfo->fabric_make_tpg) {
500 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800501 return -EINVAL;
502 }
Andy Grover6708bb22011-06-08 10:36:43 -0700503 if (!tfo->fabric_drop_tpg) {
504 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800505 return -EINVAL;
506 }
507
508 return 0;
509}
510
511/*
512 * Called 2nd from fabric module with returned parameter of
513 * struct target_fabric_configfs * from target_fabric_configfs_init().
514 *
515 * Upon a successful registration, the new fabric's struct config_item is
516 * return. Also, a pointer to this struct is set in the passed
517 * struct target_fabric_configfs.
518 */
519int target_fabric_configfs_register(
520 struct target_fabric_configfs *tf)
521{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800522 int ret;
523
Andy Grover6708bb22011-06-08 10:36:43 -0700524 if (!tf) {
525 pr_err("Unable to locate target_fabric_configfs"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800526 " pointer\n");
527 return -EINVAL;
528 }
Andy Grover6708bb22011-06-08 10:36:43 -0700529 if (!tf->tf_subsys) {
530 pr_err("Unable to target struct config_subsystem"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800531 " pointer\n");
532 return -EINVAL;
533 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800534 ret = target_fabric_tf_ops_check(tf);
535 if (ret < 0)
536 return ret;
537
Andy Grover6708bb22011-06-08 10:36:43 -0700538 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800539 ">>>>>>>>>>\n");
540 return 0;
541}
542EXPORT_SYMBOL(target_fabric_configfs_register);
543
544void target_fabric_configfs_deregister(
545 struct target_fabric_configfs *tf)
546{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800547 struct configfs_subsystem *su;
548
Andy Grover6708bb22011-06-08 10:36:43 -0700549 if (!tf) {
550 pr_err("Unable to locate passed target_fabric_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800551 "configfs\n");
552 return;
553 }
554 su = tf->tf_subsys;
Andy Grover6708bb22011-06-08 10:36:43 -0700555 if (!su) {
556 pr_err("Unable to locate passed tf->tf_subsys"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800557 " pointer\n");
558 return;
559 }
Andy Grover6708bb22011-06-08 10:36:43 -0700560 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800561 ">>>>>>>>>>>>\n");
562 mutex_lock(&g_tf_lock);
563 if (atomic_read(&tf->tf_access_cnt)) {
564 mutex_unlock(&g_tf_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700565 pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800566 tf->tf_name);
567 BUG();
568 }
569 list_del(&tf->tf_list);
570 mutex_unlock(&g_tf_lock);
571
Andy Grover6708bb22011-06-08 10:36:43 -0700572 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800573 " %s\n", tf->tf_name);
574 tf->tf_module = NULL;
575 tf->tf_subsys = NULL;
576 kfree(tf);
577
Andy Grover6708bb22011-06-08 10:36:43 -0700578 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800579 ">>>>>\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800580}
581EXPORT_SYMBOL(target_fabric_configfs_deregister);
582
583/*##############################################################################
584// Stop functions called by external Target Fabrics Modules
585//############################################################################*/
586
Nicholas Bellingerf79a8972014-11-27 14:51:14 -0800587/* Start functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588
589#define DEF_DEV_ATTRIB_SHOW(_name) \
590static ssize_t target_core_dev_show_attr_##_name( \
591 struct se_dev_attrib *da, \
592 char *page) \
593{ \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400594 return snprintf(page, PAGE_SIZE, "%u\n", \
595 (u32)da->da_dev->dev_attrib._name); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800596}
597
598#define DEF_DEV_ATTRIB_STORE(_name) \
599static ssize_t target_core_dev_store_attr_##_name( \
600 struct se_dev_attrib *da, \
601 const char *page, \
602 size_t count) \
603{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800604 unsigned long val; \
605 int ret; \
606 \
Jingoo Han57103d72013-07-19 16:22:19 +0900607 ret = kstrtoul(page, 0, &val); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800608 if (ret < 0) { \
Jingoo Han57103d72013-07-19 16:22:19 +0900609 pr_err("kstrtoul() failed with" \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800610 " ret: %d\n", ret); \
611 return -EINVAL; \
612 } \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400613 ret = se_dev_set_##_name(da->da_dev, (u32)val); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800614 \
615 return (!ret) ? count : -EINVAL; \
616}
617
618#define DEF_DEV_ATTRIB(_name) \
619DEF_DEV_ATTRIB_SHOW(_name); \
620DEF_DEV_ATTRIB_STORE(_name);
621
622#define DEF_DEV_ATTRIB_RO(_name) \
623DEF_DEV_ATTRIB_SHOW(_name);
624
625CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
626#define SE_DEV_ATTR(_name, _mode) \
627static struct target_core_dev_attrib_attribute \
628 target_core_dev_attrib_##_name = \
629 __CONFIGFS_EATTR(_name, _mode, \
630 target_core_dev_show_attr_##_name, \
631 target_core_dev_store_attr_##_name);
632
633#define SE_DEV_ATTR_RO(_name); \
634static struct target_core_dev_attrib_attribute \
635 target_core_dev_attrib_##_name = \
636 __CONFIGFS_EATTR_RO(_name, \
637 target_core_dev_show_attr_##_name);
638
Tregaron Baylyadfa9572013-01-31 15:30:24 -0700639DEF_DEV_ATTRIB(emulate_model_alias);
640SE_DEV_ATTR(emulate_model_alias, S_IRUGO | S_IWUSR);
641
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800642DEF_DEV_ATTRIB(emulate_dpo);
643SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
644
645DEF_DEV_ATTRIB(emulate_fua_write);
646SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
647
648DEF_DEV_ATTRIB(emulate_fua_read);
649SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
650
651DEF_DEV_ATTRIB(emulate_write_cache);
652SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
653
654DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
655SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
656
657DEF_DEV_ATTRIB(emulate_tas);
658SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
659
660DEF_DEV_ATTRIB(emulate_tpu);
661SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
662
663DEF_DEV_ATTRIB(emulate_tpws);
664SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
665
Nicholas Bellinger0123a9e2013-08-20 14:24:09 -0700666DEF_DEV_ATTRIB(emulate_caw);
667SE_DEV_ATTR(emulate_caw, S_IRUGO | S_IWUSR);
668
Nicholas Bellingerd397a442013-08-22 14:17:20 -0700669DEF_DEV_ATTRIB(emulate_3pc);
670SE_DEV_ATTR(emulate_3pc, S_IRUGO | S_IWUSR);
671
Nicholas Bellinger2ed22c92014-01-08 18:19:31 +0000672DEF_DEV_ATTRIB(pi_prot_type);
673SE_DEV_ATTR(pi_prot_type, S_IRUGO | S_IWUSR);
674
675DEF_DEV_ATTRIB_RO(hw_pi_prot_type);
676SE_DEV_ATTR_RO(hw_pi_prot_type);
677
678DEF_DEV_ATTRIB(pi_prot_format);
679SE_DEV_ATTR(pi_prot_format, S_IRUGO | S_IWUSR);
680
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800681DEF_DEV_ATTRIB(enforce_pr_isids);
682SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
683
Roland Dreiere22a7f072011-07-05 13:34:52 -0700684DEF_DEV_ATTRIB(is_nonrot);
685SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
686
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700687DEF_DEV_ATTRIB(emulate_rest_reord);
688SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
689
Nicholas Bellinger92404e62014-10-04 01:06:08 +0000690DEF_DEV_ATTRIB(force_pr_aptpl);
691SE_DEV_ATTR(force_pr_aptpl, S_IRUGO | S_IWUSR);
692
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800693DEF_DEV_ATTRIB_RO(hw_block_size);
694SE_DEV_ATTR_RO(hw_block_size);
695
696DEF_DEV_ATTRIB(block_size);
697SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
698
699DEF_DEV_ATTRIB_RO(hw_max_sectors);
700SE_DEV_ATTR_RO(hw_max_sectors);
701
Roland Dreier015487b2012-02-13 16:18:17 -0800702DEF_DEV_ATTRIB(fabric_max_sectors);
703SE_DEV_ATTR(fabric_max_sectors, S_IRUGO | S_IWUSR);
704
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800705DEF_DEV_ATTRIB(optimal_sectors);
706SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
707
708DEF_DEV_ATTRIB_RO(hw_queue_depth);
709SE_DEV_ATTR_RO(hw_queue_depth);
710
711DEF_DEV_ATTRIB(queue_depth);
712SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
713
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800714DEF_DEV_ATTRIB(max_unmap_lba_count);
715SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
716
717DEF_DEV_ATTRIB(max_unmap_block_desc_count);
718SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
719
720DEF_DEV_ATTRIB(unmap_granularity);
721SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
722
723DEF_DEV_ATTRIB(unmap_granularity_alignment);
724SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
725
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800726DEF_DEV_ATTRIB(max_write_same_len);
727SE_DEV_ATTR(max_write_same_len, S_IRUGO | S_IWUSR);
728
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800729CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
730
731static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
Tregaron Baylyadfa9572013-01-31 15:30:24 -0700732 &target_core_dev_attrib_emulate_model_alias.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800733 &target_core_dev_attrib_emulate_dpo.attr,
734 &target_core_dev_attrib_emulate_fua_write.attr,
735 &target_core_dev_attrib_emulate_fua_read.attr,
736 &target_core_dev_attrib_emulate_write_cache.attr,
737 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
738 &target_core_dev_attrib_emulate_tas.attr,
739 &target_core_dev_attrib_emulate_tpu.attr,
740 &target_core_dev_attrib_emulate_tpws.attr,
Nicholas Bellinger0123a9e2013-08-20 14:24:09 -0700741 &target_core_dev_attrib_emulate_caw.attr,
Nicholas Bellingerd397a442013-08-22 14:17:20 -0700742 &target_core_dev_attrib_emulate_3pc.attr,
Nicholas Bellinger2ed22c92014-01-08 18:19:31 +0000743 &target_core_dev_attrib_pi_prot_type.attr,
744 &target_core_dev_attrib_hw_pi_prot_type.attr,
745 &target_core_dev_attrib_pi_prot_format.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800746 &target_core_dev_attrib_enforce_pr_isids.attr,
Nicholas Bellinger92404e62014-10-04 01:06:08 +0000747 &target_core_dev_attrib_force_pr_aptpl.attr,
Roland Dreiere22a7f072011-07-05 13:34:52 -0700748 &target_core_dev_attrib_is_nonrot.attr,
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700749 &target_core_dev_attrib_emulate_rest_reord.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800750 &target_core_dev_attrib_hw_block_size.attr,
751 &target_core_dev_attrib_block_size.attr,
752 &target_core_dev_attrib_hw_max_sectors.attr,
Roland Dreier015487b2012-02-13 16:18:17 -0800753 &target_core_dev_attrib_fabric_max_sectors.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800754 &target_core_dev_attrib_optimal_sectors.attr,
755 &target_core_dev_attrib_hw_queue_depth.attr,
756 &target_core_dev_attrib_queue_depth.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800757 &target_core_dev_attrib_max_unmap_lba_count.attr,
758 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
759 &target_core_dev_attrib_unmap_granularity.attr,
760 &target_core_dev_attrib_unmap_granularity_alignment.attr,
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800761 &target_core_dev_attrib_max_write_same_len.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800762 NULL,
763};
764
765static struct configfs_item_operations target_core_dev_attrib_ops = {
766 .show_attribute = target_core_dev_attrib_attr_show,
767 .store_attribute = target_core_dev_attrib_attr_store,
768};
769
Nicholas Bellingerf79a8972014-11-27 14:51:14 -0800770TB_CIT_SETUP(dev_attrib, &target_core_dev_attrib_ops, NULL,
771 target_core_dev_attrib_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800772
Nicholas Bellingerf79a8972014-11-27 14:51:14 -0800773/* End functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800774
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -0800775/* Start functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800776
777CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
778#define SE_DEV_WWN_ATTR(_name, _mode) \
779static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
780 __CONFIGFS_EATTR(_name, _mode, \
781 target_core_dev_wwn_show_attr_##_name, \
782 target_core_dev_wwn_store_attr_##_name);
783
784#define SE_DEV_WWN_ATTR_RO(_name); \
785do { \
786 static struct target_core_dev_wwn_attribute \
787 target_core_dev_wwn_##_name = \
788 __CONFIGFS_EATTR_RO(_name, \
789 target_core_dev_wwn_show_attr_##_name); \
790} while (0);
791
792/*
793 * VPD page 0x80 Unit serial
794 */
795static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
796 struct t10_wwn *t10_wwn,
797 char *page)
798{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800799 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
800 &t10_wwn->unit_serial[0]);
801}
802
803static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
804 struct t10_wwn *t10_wwn,
805 const char *page,
806 size_t count)
807{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400808 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800809 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
810
811 /*
812 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
813 * from the struct scsi_device level firmware, do not allow
814 * VPD Unit Serial to be emulated.
815 *
816 * Note this struct scsi_device could also be emulating VPD
817 * information from its drivers/scsi LLD. But for now we assume
818 * it is doing 'the right thing' wrt a world wide unique
819 * VPD Unit Serial Number that OS dependent multipath can depend on.
820 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400821 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700822 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800823 " Unit Serial, ignoring request\n");
824 return -EOPNOTSUPP;
825 }
826
Dan Carpenter60d645a2011-06-15 10:03:05 -0700827 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -0700828 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800829 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
830 return -EOVERFLOW;
831 }
832 /*
833 * Check to see if any active $FABRIC_MOD exports exist. If they
834 * do exist, fail here as changing this information on the fly
835 * (underneath the initiator side OS dependent multipath code)
836 * could cause negative effects.
837 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400838 if (dev->export_count) {
839 pr_err("Unable to set VPD Unit Serial while"
840 " active %d $FABRIC_MOD exports exist\n",
841 dev->export_count);
842 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800843 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400844
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800845 /*
846 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
847 *
848 * Also, strip any newline added from the userspace
849 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
850 */
851 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
852 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400853 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800854 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400855 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800856
Andy Grover6708bb22011-06-08 10:36:43 -0700857 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400858 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800859
860 return count;
861}
862
863SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
864
865/*
866 * VPD page 0x83 Protocol Identifier
867 */
868static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
869 struct t10_wwn *t10_wwn,
870 char *page)
871{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800872 struct t10_vpd *vpd;
873 unsigned char buf[VPD_TMP_BUF_SIZE];
874 ssize_t len = 0;
875
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800876 memset(buf, 0, VPD_TMP_BUF_SIZE);
877
878 spin_lock(&t10_wwn->t10_vpd_lock);
879 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700880 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800881 continue;
882
883 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
884
Andy Grover6708bb22011-06-08 10:36:43 -0700885 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800886 break;
887
888 len += sprintf(page+len, "%s", buf);
889 }
890 spin_unlock(&t10_wwn->t10_vpd_lock);
891
892 return len;
893}
894
895static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
896 struct t10_wwn *t10_wwn,
897 const char *page,
898 size_t count)
899{
900 return -ENOSYS;
901}
902
903SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
904
905/*
906 * Generic wrapper for dumping VPD identifiers by association.
907 */
908#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
909static ssize_t target_core_dev_wwn_show_attr_##_name( \
910 struct t10_wwn *t10_wwn, \
911 char *page) \
912{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800913 struct t10_vpd *vpd; \
914 unsigned char buf[VPD_TMP_BUF_SIZE]; \
915 ssize_t len = 0; \
916 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800917 spin_lock(&t10_wwn->t10_vpd_lock); \
918 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
919 if (vpd->association != _assoc) \
920 continue; \
921 \
922 memset(buf, 0, VPD_TMP_BUF_SIZE); \
923 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700924 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800925 break; \
926 len += sprintf(page+len, "%s", buf); \
927 \
928 memset(buf, 0, VPD_TMP_BUF_SIZE); \
929 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700930 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800931 break; \
932 len += sprintf(page+len, "%s", buf); \
933 \
934 memset(buf, 0, VPD_TMP_BUF_SIZE); \
935 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700936 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800937 break; \
938 len += sprintf(page+len, "%s", buf); \
939 } \
940 spin_unlock(&t10_wwn->t10_vpd_lock); \
941 \
942 return len; \
943}
944
945/*
Andy Shevchenko163cd5f2011-07-18 22:17:43 -0700946 * VPD page 0x83 Association: Logical Unit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800947 */
948DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
949
950static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
951 struct t10_wwn *t10_wwn,
952 const char *page,
953 size_t count)
954{
955 return -ENOSYS;
956}
957
958SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
959
960/*
961 * VPD page 0x83 Association: Target Port
962 */
963DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
964
965static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
966 struct t10_wwn *t10_wwn,
967 const char *page,
968 size_t count)
969{
970 return -ENOSYS;
971}
972
973SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
974
975/*
976 * VPD page 0x83 Association: SCSI Target Device
977 */
978DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
979
980static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
981 struct t10_wwn *t10_wwn,
982 const char *page,
983 size_t count)
984{
985 return -ENOSYS;
986}
987
988SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
989
990CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
991
992static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
993 &target_core_dev_wwn_vpd_unit_serial.attr,
994 &target_core_dev_wwn_vpd_protocol_identifier.attr,
995 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
996 &target_core_dev_wwn_vpd_assoc_target_port.attr,
997 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
998 NULL,
999};
1000
1001static struct configfs_item_operations target_core_dev_wwn_ops = {
1002 .show_attribute = target_core_dev_wwn_attr_show,
1003 .store_attribute = target_core_dev_wwn_attr_store,
1004};
1005
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001006TB_CIT_SETUP(dev_wwn, &target_core_dev_wwn_ops, NULL, target_core_dev_wwn_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001007
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001008/* End functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001009
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001010/* Start functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001011
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001012CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001013#define SE_DEV_PR_ATTR(_name, _mode) \
1014static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1015 __CONFIGFS_EATTR(_name, _mode, \
1016 target_core_dev_pr_show_attr_##_name, \
1017 target_core_dev_pr_store_attr_##_name);
1018
1019#define SE_DEV_PR_ATTR_RO(_name); \
1020static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1021 __CONFIGFS_EATTR_RO(_name, \
1022 target_core_dev_pr_show_attr_##_name);
1023
Christoph Hellwigd977f432012-10-10 17:37:15 -04001024static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1025 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001026{
1027 struct se_node_acl *se_nacl;
1028 struct t10_pr_registration *pr_reg;
1029 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001030
1031 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1032
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001033 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001034 if (!pr_reg)
1035 return sprintf(page, "No SPC-3 Reservation holder\n");
1036
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001037 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -07001038 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001039
Christoph Hellwigd977f432012-10-10 17:37:15 -04001040 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001041 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001042 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001043}
1044
Christoph Hellwigd977f432012-10-10 17:37:15 -04001045static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1046 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001047{
1048 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001049 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001050
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001051 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001052 if (se_nacl) {
1053 len = sprintf(page,
1054 "SPC-2 Reservation: %s Initiator: %s\n",
1055 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1056 se_nacl->initiatorname);
1057 } else {
1058 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001060 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001061}
1062
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001063static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
1064 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001065{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001066 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001067
Christoph Hellwigd977f432012-10-10 17:37:15 -04001068 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1069 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001070
Christoph Hellwigd977f432012-10-10 17:37:15 -04001071 spin_lock(&dev->dev_reservation_lock);
1072 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1073 ret = target_core_dev_pr_show_spc2_res(dev, page);
1074 else
1075 ret = target_core_dev_pr_show_spc3_res(dev, page);
1076 spin_unlock(&dev->dev_reservation_lock);
1077 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001078}
1079
1080SE_DEV_PR_ATTR_RO(res_holder);
1081
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001082static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001083 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001084{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001085 ssize_t len = 0;
1086
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001087 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001088 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001089 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001090 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001091 len = sprintf(page, "SPC-3 Reservation: All Target"
1092 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001093 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001094 len = sprintf(page, "SPC-3 Reservation: Single"
1095 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001096 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001097
Christoph Hellwigd977f432012-10-10 17:37:15 -04001098 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001099 return len;
1100}
1101
1102SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1103
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001104static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001105 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001106{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001107 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001108}
1109
1110SE_DEV_PR_ATTR_RO(res_pr_generation);
1111
1112/*
1113 * res_pr_holder_tg_port
1114 */
1115static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001116 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001117{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001118 struct se_node_acl *se_nacl;
1119 struct se_lun *lun;
1120 struct se_portal_group *se_tpg;
1121 struct t10_pr_registration *pr_reg;
1122 struct target_core_fabric_ops *tfo;
1123 ssize_t len = 0;
1124
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001125 spin_lock(&dev->dev_reservation_lock);
1126 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001127 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001128 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001129 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001130 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001131
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001132 se_nacl = pr_reg->pr_reg_nacl;
1133 se_tpg = se_nacl->se_tpg;
1134 lun = pr_reg->pr_reg_tg_pt_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +00001135 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001136
1137 len += sprintf(page+len, "SPC-3 Reservation: %s"
1138 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1139 tfo->tpg_get_wwn(se_tpg));
1140 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001141 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001142 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1143 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1144 tfo->get_fabric_name(), lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001145
Christoph Hellwigd977f432012-10-10 17:37:15 -04001146out_unlock:
1147 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001148 return len;
1149}
1150
1151SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1152
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001153static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001154 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001155{
1156 struct target_core_fabric_ops *tfo;
1157 struct t10_pr_registration *pr_reg;
1158 unsigned char buf[384];
1159 char i_buf[PR_REG_ISID_ID_LEN];
1160 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001161 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001162
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001163 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1164
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001165 spin_lock(&dev->t10_pr.registration_lock);
1166 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001167 pr_reg_list) {
1168
1169 memset(buf, 0, 384);
1170 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1171 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -07001172 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001173 PR_REG_ISID_ID_LEN);
1174 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1175 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001176 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001177 pr_reg->pr_res_generation);
1178
Andy Grover6708bb22011-06-08 10:36:43 -07001179 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001180 break;
1181
1182 len += sprintf(page+len, "%s", buf);
1183 reg_count++;
1184 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001185 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001186
Andy Grover6708bb22011-06-08 10:36:43 -07001187 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001188 len += sprintf(page+len, "None\n");
1189
1190 return len;
1191}
1192
1193SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1194
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001195static ssize_t target_core_dev_pr_show_attr_res_pr_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001196 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001197{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001198 struct t10_pr_registration *pr_reg;
1199 ssize_t len = 0;
1200
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001201 spin_lock(&dev->dev_reservation_lock);
1202 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001203 if (pr_reg) {
1204 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1205 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1206 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001207 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001208 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001209
Christoph Hellwigd977f432012-10-10 17:37:15 -04001210 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001211 return len;
1212}
1213
1214SE_DEV_PR_ATTR_RO(res_pr_type);
1215
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001216static ssize_t target_core_dev_pr_show_attr_res_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001217 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001218{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001219 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1220 return sprintf(page, "SPC_PASSTHROUGH\n");
1221 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1222 return sprintf(page, "SPC2_RESERVATIONS\n");
1223 else
1224 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001225}
1226
1227SE_DEV_PR_ATTR_RO(res_type);
1228
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001229static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001230 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001231{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001232 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001233 return 0;
1234
1235 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001236 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001237}
1238
1239SE_DEV_PR_ATTR_RO(res_aptpl_active);
1240
1241/*
1242 * res_aptpl_metadata
1243 */
1244static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001245 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001246{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001247 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001248 return 0;
1249
1250 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1251}
1252
1253enum {
1254 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1255 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1256 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1257 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1258};
1259
1260static match_table_t tokens = {
1261 {Opt_initiator_fabric, "initiator_fabric=%s"},
1262 {Opt_initiator_node, "initiator_node=%s"},
1263 {Opt_initiator_sid, "initiator_sid=%s"},
1264 {Opt_sa_res_key, "sa_res_key=%s"},
1265 {Opt_res_holder, "res_holder=%d"},
1266 {Opt_res_type, "res_type=%d"},
1267 {Opt_res_scope, "res_scope=%d"},
1268 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1269 {Opt_mapped_lun, "mapped_lun=%d"},
1270 {Opt_target_fabric, "target_fabric=%s"},
1271 {Opt_target_node, "target_node=%s"},
1272 {Opt_tpgt, "tpgt=%d"},
1273 {Opt_port_rtpi, "port_rtpi=%d"},
1274 {Opt_target_lun, "target_lun=%d"},
1275 {Opt_err, NULL}
1276};
1277
1278static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001279 struct se_device *dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001280 const char *page,
1281 size_t count)
1282{
Jesper Juhl6d180252011-03-14 04:05:56 -07001283 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1284 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -04001285 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001286 substring_t args[MAX_OPT_ARGS];
1287 unsigned long long tmp_ll;
1288 u64 sa_res_key = 0;
1289 u32 mapped_lun = 0, target_lun = 0;
1290 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1291 u16 port_rpti = 0, tpgt = 0;
1292 u8 type = 0, scope;
1293
Christoph Hellwigd977f432012-10-10 17:37:15 -04001294 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1295 return 0;
1296 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001297 return 0;
1298
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001299 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001300 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001301 " active fabric exports exist\n");
1302 return -EINVAL;
1303 }
1304
1305 opts = kstrdup(page, GFP_KERNEL);
1306 if (!opts)
1307 return -ENOMEM;
1308
1309 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001310 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001311 if (!*ptr)
1312 continue;
1313
1314 token = match_token(ptr, tokens, args);
1315 switch (token) {
1316 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001317 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001318 if (!i_fabric) {
1319 ret = -ENOMEM;
1320 goto out;
1321 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001322 break;
1323 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001324 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001325 if (!i_port) {
1326 ret = -ENOMEM;
1327 goto out;
1328 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001329 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001330 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001331 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1332 PR_APTPL_MAX_IPORT_LEN);
1333 ret = -EINVAL;
1334 break;
1335 }
1336 break;
1337 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001338 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001339 if (!isid) {
1340 ret = -ENOMEM;
1341 goto out;
1342 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001343 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001344 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001345 "= exceeds PR_REG_ISID_LEN: %d\n",
1346 PR_REG_ISID_LEN);
1347 ret = -EINVAL;
1348 break;
1349 }
1350 break;
1351 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001352 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001353 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001354 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001355 goto out;
1356 }
1357 sa_res_key = (u64)tmp_ll;
1358 break;
1359 /*
1360 * PR APTPL Metadata for Reservation
1361 */
1362 case Opt_res_holder:
1363 match_int(args, &arg);
1364 res_holder = arg;
1365 break;
1366 case Opt_res_type:
1367 match_int(args, &arg);
1368 type = (u8)arg;
1369 break;
1370 case Opt_res_scope:
1371 match_int(args, &arg);
1372 scope = (u8)arg;
1373 break;
1374 case Opt_res_all_tg_pt:
1375 match_int(args, &arg);
1376 all_tg_pt = (int)arg;
1377 break;
1378 case Opt_mapped_lun:
1379 match_int(args, &arg);
1380 mapped_lun = (u32)arg;
1381 break;
1382 /*
1383 * PR APTPL Metadata for Target Port
1384 */
1385 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001386 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001387 if (!t_fabric) {
1388 ret = -ENOMEM;
1389 goto out;
1390 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001391 break;
1392 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001393 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001394 if (!t_port) {
1395 ret = -ENOMEM;
1396 goto out;
1397 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001398 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001399 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001400 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1401 PR_APTPL_MAX_TPORT_LEN);
1402 ret = -EINVAL;
1403 break;
1404 }
1405 break;
1406 case Opt_tpgt:
1407 match_int(args, &arg);
1408 tpgt = (u16)arg;
1409 break;
1410 case Opt_port_rtpi:
1411 match_int(args, &arg);
1412 port_rpti = (u16)arg;
1413 break;
1414 case Opt_target_lun:
1415 match_int(args, &arg);
1416 target_lun = (u32)arg;
1417 break;
1418 default:
1419 break;
1420 }
1421 }
1422
Andy Grover6708bb22011-06-08 10:36:43 -07001423 if (!i_port || !t_port || !sa_res_key) {
1424 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001425 ret = -EINVAL;
1426 goto out;
1427 }
1428
1429 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001430 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001431 " holder\n", type);
1432 ret = -EINVAL;
1433 goto out;
1434 }
1435
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001436 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001437 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1438 res_holder, all_tg_pt, type);
1439out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001440 kfree(i_fabric);
1441 kfree(i_port);
1442 kfree(isid);
1443 kfree(t_fabric);
1444 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001445 kfree(orig);
1446 return (ret == 0) ? count : ret;
1447}
1448
1449SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1450
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001451CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001452
1453static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1454 &target_core_dev_pr_res_holder.attr,
1455 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1456 &target_core_dev_pr_res_pr_generation.attr,
1457 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1458 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1459 &target_core_dev_pr_res_pr_type.attr,
1460 &target_core_dev_pr_res_type.attr,
1461 &target_core_dev_pr_res_aptpl_active.attr,
1462 &target_core_dev_pr_res_aptpl_metadata.attr,
1463 NULL,
1464};
1465
1466static struct configfs_item_operations target_core_dev_pr_ops = {
1467 .show_attribute = target_core_dev_pr_attr_show,
1468 .store_attribute = target_core_dev_pr_attr_store,
1469};
1470
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001471TB_CIT_SETUP(dev_pr, &target_core_dev_pr_ops, NULL, target_core_dev_pr_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001472
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001473/* End functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001474
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001475/* Start functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001476
1477static ssize_t target_core_show_dev_info(void *p, char *page)
1478{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001479 struct se_device *dev = p;
1480 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001481 int bl = 0;
1482 ssize_t read_bytes = 0;
1483
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001484 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485 read_bytes += bl;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001486 read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001487 return read_bytes;
1488}
1489
1490static struct target_core_configfs_attribute target_core_attr_dev_info = {
1491 .attr = { .ca_owner = THIS_MODULE,
1492 .ca_name = "info",
1493 .ca_mode = S_IRUGO },
1494 .show = target_core_show_dev_info,
1495 .store = NULL,
1496};
1497
1498static ssize_t target_core_store_dev_control(
1499 void *p,
1500 const char *page,
1501 size_t count)
1502{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001503 struct se_device *dev = p;
1504 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001505
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001506 return t->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001507}
1508
1509static struct target_core_configfs_attribute target_core_attr_dev_control = {
1510 .attr = { .ca_owner = THIS_MODULE,
1511 .ca_name = "control",
1512 .ca_mode = S_IWUSR },
1513 .show = NULL,
1514 .store = target_core_store_dev_control,
1515};
1516
1517static ssize_t target_core_show_dev_alias(void *p, char *page)
1518{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001519 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001520
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001521 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001522 return 0;
1523
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001524 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001525}
1526
1527static ssize_t target_core_store_dev_alias(
1528 void *p,
1529 const char *page,
1530 size_t count)
1531{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001532 struct se_device *dev = p;
1533 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001534 ssize_t read_bytes;
1535
1536 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001537 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001538 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1539 SE_DEV_ALIAS_LEN-1);
1540 return -EINVAL;
1541 }
1542
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001543 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001544 if (!read_bytes)
1545 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001546 if (dev->dev_alias[read_bytes - 1] == '\n')
1547 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001548
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001549 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001550
Andy Grover6708bb22011-06-08 10:36:43 -07001551 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001552 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001553 config_item_name(&dev->dev_group.cg_item),
1554 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001555
1556 return read_bytes;
1557}
1558
1559static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1560 .attr = { .ca_owner = THIS_MODULE,
1561 .ca_name = "alias",
1562 .ca_mode = S_IRUGO | S_IWUSR },
1563 .show = target_core_show_dev_alias,
1564 .store = target_core_store_dev_alias,
1565};
1566
1567static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1568{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001569 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001570
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001571 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001572 return 0;
1573
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001574 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001575}
1576
1577static ssize_t target_core_store_dev_udev_path(
1578 void *p,
1579 const char *page,
1580 size_t count)
1581{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001582 struct se_device *dev = p;
1583 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001584 ssize_t read_bytes;
1585
1586 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001587 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001588 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1589 SE_UDEV_PATH_LEN-1);
1590 return -EINVAL;
1591 }
1592
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001593 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001594 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001595 if (!read_bytes)
1596 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001597 if (dev->udev_path[read_bytes - 1] == '\n')
1598 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001599
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001600 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001601
Andy Grover6708bb22011-06-08 10:36:43 -07001602 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001603 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001604 config_item_name(&dev->dev_group.cg_item),
1605 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001606
1607 return read_bytes;
1608}
1609
1610static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1611 .attr = { .ca_owner = THIS_MODULE,
1612 .ca_name = "udev_path",
1613 .ca_mode = S_IRUGO | S_IWUSR },
1614 .show = target_core_show_dev_udev_path,
1615 .store = target_core_store_dev_udev_path,
1616};
1617
Andy Grover64146db2013-04-30 11:59:15 -07001618static ssize_t target_core_show_dev_enable(void *p, char *page)
1619{
1620 struct se_device *dev = p;
1621
1622 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1623}
1624
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001625static ssize_t target_core_store_dev_enable(
1626 void *p,
1627 const char *page,
1628 size_t count)
1629{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001630 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001631 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001632 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001633
1634 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001635 if (!ptr) {
1636 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001637 " is \"1\"\n");
1638 return -EINVAL;
1639 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001640
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001641 ret = target_configure_device(dev);
1642 if (ret)
1643 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001644 return count;
1645}
1646
1647static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1648 .attr = { .ca_owner = THIS_MODULE,
1649 .ca_name = "enable",
Andy Grover64146db2013-04-30 11:59:15 -07001650 .ca_mode = S_IRUGO | S_IWUSR },
1651 .show = target_core_show_dev_enable,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001652 .store = target_core_store_dev_enable,
1653};
1654
1655static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1656{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001657 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001658 struct config_item *lu_ci;
1659 struct t10_alua_lu_gp *lu_gp;
1660 struct t10_alua_lu_gp_member *lu_gp_mem;
1661 ssize_t len = 0;
1662
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001663 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001664 if (!lu_gp_mem)
1665 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001666
1667 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1668 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001669 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001670 lu_ci = &lu_gp->lu_gp_group.cg_item;
1671 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1672 config_item_name(lu_ci), lu_gp->lu_gp_id);
1673 }
1674 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1675
1676 return len;
1677}
1678
1679static ssize_t target_core_store_alua_lu_gp(
1680 void *p,
1681 const char *page,
1682 size_t count)
1683{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001684 struct se_device *dev = p;
1685 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001686 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1687 struct t10_alua_lu_gp_member *lu_gp_mem;
1688 unsigned char buf[LU_GROUP_NAME_BUF];
1689 int move = 0;
1690
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001691 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1692 if (!lu_gp_mem)
1693 return 0;
1694
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001695 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001696 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001697 return -EINVAL;
1698 }
1699 memset(buf, 0, LU_GROUP_NAME_BUF);
1700 memcpy(buf, page, count);
1701 /*
1702 * Any ALUA logical unit alias besides "NULL" means we will be
1703 * making a new group association.
1704 */
1705 if (strcmp(strstrip(buf), "NULL")) {
1706 /*
1707 * core_alua_get_lu_gp_by_name() will increment reference to
1708 * struct t10_alua_lu_gp. This reference is released with
1709 * core_alua_get_lu_gp_by_name below().
1710 */
1711 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001712 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001713 return -ENODEV;
1714 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001715
1716 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1717 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001718 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001719 /*
1720 * Clearing an existing lu_gp association, and replacing
1721 * with NULL
1722 */
Andy Grover6708bb22011-06-08 10:36:43 -07001723 if (!lu_gp_new) {
1724 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001725 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1726 " %hu\n",
1727 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001728 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001729 config_item_name(&lu_gp->lu_gp_group.cg_item),
1730 lu_gp->lu_gp_id);
1731
1732 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1733 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1734
1735 return count;
1736 }
1737 /*
1738 * Removing existing association of lu_gp_mem with lu_gp
1739 */
1740 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1741 move = 1;
1742 }
1743 /*
1744 * Associate lu_gp_mem with lu_gp_new.
1745 */
1746 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1747 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1748
Andy Grover6708bb22011-06-08 10:36:43 -07001749 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001750 " core/alua/lu_gps/%s, ID: %hu\n",
1751 (move) ? "Moving" : "Adding",
1752 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001753 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001754 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1755 lu_gp_new->lu_gp_id);
1756
1757 core_alua_put_lu_gp_from_name(lu_gp_new);
1758 return count;
1759}
1760
1761static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1762 .attr = { .ca_owner = THIS_MODULE,
1763 .ca_name = "alua_lu_gp",
1764 .ca_mode = S_IRUGO | S_IWUSR },
1765 .show = target_core_show_alua_lu_gp,
1766 .store = target_core_store_alua_lu_gp,
1767};
1768
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001769static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1770{
1771 struct se_device *dev = p;
1772 struct t10_alua_lba_map *map;
1773 struct t10_alua_lba_map_member *mem;
1774 char *b = page;
1775 int bl = 0;
1776 char state;
1777
1778 spin_lock(&dev->t10_alua.lba_map_lock);
1779 if (!list_empty(&dev->t10_alua.lba_map_list))
1780 bl += sprintf(b + bl, "%u %u\n",
1781 dev->t10_alua.lba_map_segment_size,
1782 dev->t10_alua.lba_map_segment_multiplier);
1783 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1784 bl += sprintf(b + bl, "%llu %llu",
1785 map->lba_map_first_lba, map->lba_map_last_lba);
1786 list_for_each_entry(mem, &map->lba_map_mem_list,
1787 lba_map_mem_list) {
1788 switch (mem->lba_map_mem_alua_state) {
1789 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1790 state = 'O';
1791 break;
1792 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1793 state = 'A';
1794 break;
1795 case ALUA_ACCESS_STATE_STANDBY:
1796 state = 'S';
1797 break;
1798 case ALUA_ACCESS_STATE_UNAVAILABLE:
1799 state = 'U';
1800 break;
1801 default:
1802 state = '.';
1803 break;
1804 }
1805 bl += sprintf(b + bl, " %d:%c",
1806 mem->lba_map_mem_alua_pg_id, state);
1807 }
1808 bl += sprintf(b + bl, "\n");
1809 }
1810 spin_unlock(&dev->t10_alua.lba_map_lock);
1811 return bl;
1812}
1813
1814static ssize_t target_core_store_dev_lba_map(
1815 void *p,
1816 const char *page,
1817 size_t count)
1818{
1819 struct se_device *dev = p;
1820 struct t10_alua_lba_map *lba_map = NULL;
1821 struct list_head lba_list;
1822 char *map_entries, *ptr;
1823 char state;
1824 int pg_num = -1, pg;
1825 int ret = 0, num = 0, pg_id, alua_state;
1826 unsigned long start_lba = -1, end_lba = -1;
1827 unsigned long segment_size = -1, segment_mult = -1;
1828
1829 map_entries = kstrdup(page, GFP_KERNEL);
1830 if (!map_entries)
1831 return -ENOMEM;
1832
1833 INIT_LIST_HEAD(&lba_list);
1834 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1835 if (!*ptr)
1836 continue;
1837
1838 if (num == 0) {
1839 if (sscanf(ptr, "%lu %lu\n",
1840 &segment_size, &segment_mult) != 2) {
1841 pr_err("Invalid line %d\n", num);
1842 ret = -EINVAL;
1843 break;
1844 }
1845 num++;
1846 continue;
1847 }
1848 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1849 pr_err("Invalid line %d\n", num);
1850 ret = -EINVAL;
1851 break;
1852 }
1853 ptr = strchr(ptr, ' ');
1854 if (!ptr) {
1855 pr_err("Invalid line %d, missing end lba\n", num);
1856 ret = -EINVAL;
1857 break;
1858 }
1859 ptr++;
1860 ptr = strchr(ptr, ' ');
1861 if (!ptr) {
1862 pr_err("Invalid line %d, missing state definitions\n",
1863 num);
1864 ret = -EINVAL;
1865 break;
1866 }
1867 ptr++;
1868 lba_map = core_alua_allocate_lba_map(&lba_list,
1869 start_lba, end_lba);
1870 if (IS_ERR(lba_map)) {
1871 ret = PTR_ERR(lba_map);
1872 break;
1873 }
1874 pg = 0;
1875 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1876 switch (state) {
1877 case 'O':
1878 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1879 break;
1880 case 'A':
1881 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1882 break;
1883 case 'S':
1884 alua_state = ALUA_ACCESS_STATE_STANDBY;
1885 break;
1886 case 'U':
1887 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1888 break;
1889 default:
1890 pr_err("Invalid ALUA state '%c'\n", state);
1891 ret = -EINVAL;
1892 goto out;
1893 }
1894
1895 ret = core_alua_allocate_lba_map_mem(lba_map,
1896 pg_id, alua_state);
1897 if (ret) {
1898 pr_err("Invalid target descriptor %d:%c "
1899 "at line %d\n",
1900 pg_id, state, num);
1901 break;
1902 }
1903 pg++;
1904 ptr = strchr(ptr, ' ');
1905 if (ptr)
1906 ptr++;
1907 else
1908 break;
1909 }
1910 if (pg_num == -1)
1911 pg_num = pg;
1912 else if (pg != pg_num) {
1913 pr_err("Only %d from %d port groups definitions "
1914 "at line %d\n", pg, pg_num, num);
1915 ret = -EINVAL;
1916 break;
1917 }
1918 num++;
1919 }
1920out:
1921 if (ret) {
1922 core_alua_free_lba_map(&lba_list);
1923 count = ret;
1924 } else
1925 core_alua_set_lba_map(dev, &lba_list,
1926 segment_size, segment_mult);
1927 kfree(map_entries);
1928 return count;
1929}
1930
1931static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1932 .attr = { .ca_owner = THIS_MODULE,
1933 .ca_name = "lba_map",
1934 .ca_mode = S_IRUGO | S_IWUSR },
1935 .show = target_core_show_dev_lba_map,
1936 .store = target_core_store_dev_lba_map,
1937};
1938
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001939static struct configfs_attribute *target_core_dev_attrs[] = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001940 &target_core_attr_dev_info.attr,
1941 &target_core_attr_dev_control.attr,
1942 &target_core_attr_dev_alias.attr,
1943 &target_core_attr_dev_udev_path.attr,
1944 &target_core_attr_dev_enable.attr,
1945 &target_core_attr_dev_alua_lu_gp.attr,
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001946 &target_core_attr_dev_lba_map.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001947 NULL,
1948};
1949
1950static void target_core_dev_release(struct config_item *item)
1951{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001952 struct config_group *dev_cg = to_config_group(item);
1953 struct se_device *dev =
1954 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001955
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001956 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001957 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001958}
1959
1960static ssize_t target_core_dev_show(struct config_item *item,
1961 struct configfs_attribute *attr,
1962 char *page)
1963{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001964 struct config_group *dev_cg = to_config_group(item);
1965 struct se_device *dev =
1966 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001967 struct target_core_configfs_attribute *tc_attr = container_of(
1968 attr, struct target_core_configfs_attribute, attr);
1969
Andy Grover6708bb22011-06-08 10:36:43 -07001970 if (!tc_attr->show)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001971 return -EINVAL;
1972
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001973 return tc_attr->show(dev, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001974}
1975
1976static ssize_t target_core_dev_store(struct config_item *item,
1977 struct configfs_attribute *attr,
1978 const char *page, size_t count)
1979{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001980 struct config_group *dev_cg = to_config_group(item);
1981 struct se_device *dev =
1982 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001983 struct target_core_configfs_attribute *tc_attr = container_of(
1984 attr, struct target_core_configfs_attribute, attr);
1985
Andy Grover6708bb22011-06-08 10:36:43 -07001986 if (!tc_attr->store)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001987 return -EINVAL;
1988
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001989 return tc_attr->store(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001990}
1991
1992static struct configfs_item_operations target_core_dev_item_ops = {
1993 .release = target_core_dev_release,
1994 .show_attribute = target_core_dev_show,
1995 .store_attribute = target_core_dev_store,
1996};
1997
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001998TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001999
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002000/* End functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002001
2002/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2003
2004CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
2005#define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
2006static struct target_core_alua_lu_gp_attribute \
2007 target_core_alua_lu_gp_##_name = \
2008 __CONFIGFS_EATTR(_name, _mode, \
2009 target_core_alua_lu_gp_show_attr_##_name, \
2010 target_core_alua_lu_gp_store_attr_##_name);
2011
2012#define SE_DEV_ALUA_LU_ATTR_RO(_name) \
2013static struct target_core_alua_lu_gp_attribute \
2014 target_core_alua_lu_gp_##_name = \
2015 __CONFIGFS_EATTR_RO(_name, \
2016 target_core_alua_lu_gp_show_attr_##_name);
2017
2018/*
2019 * lu_gp_id
2020 */
2021static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2022 struct t10_alua_lu_gp *lu_gp,
2023 char *page)
2024{
Andy Grover6708bb22011-06-08 10:36:43 -07002025 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002026 return 0;
2027
2028 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2029}
2030
2031static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2032 struct t10_alua_lu_gp *lu_gp,
2033 const char *page,
2034 size_t count)
2035{
2036 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2037 unsigned long lu_gp_id;
2038 int ret;
2039
Jingoo Han57103d72013-07-19 16:22:19 +09002040 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002041 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002042 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002043 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002044 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002045 }
2046 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002047 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002048 " 0x0000ffff\n", lu_gp_id);
2049 return -EINVAL;
2050 }
2051
2052 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2053 if (ret < 0)
2054 return -EINVAL;
2055
Andy Grover6708bb22011-06-08 10:36:43 -07002056 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002057 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2058 config_item_name(&alua_lu_gp_cg->cg_item),
2059 lu_gp->lu_gp_id);
2060
2061 return count;
2062}
2063
2064SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2065
2066/*
2067 * members
2068 */
2069static ssize_t target_core_alua_lu_gp_show_attr_members(
2070 struct t10_alua_lu_gp *lu_gp,
2071 char *page)
2072{
2073 struct se_device *dev;
2074 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002075 struct t10_alua_lu_gp_member *lu_gp_mem;
2076 ssize_t len = 0, cur_len;
2077 unsigned char buf[LU_GROUP_NAME_BUF];
2078
2079 memset(buf, 0, LU_GROUP_NAME_BUF);
2080
2081 spin_lock(&lu_gp->lu_gp_lock);
2082 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2083 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002084 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002085
2086 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2087 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002088 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002089 cur_len++; /* Extra byte for NULL terminator */
2090
2091 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002092 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002093 "_members buffer\n");
2094 break;
2095 }
2096 memcpy(page+len, buf, cur_len);
2097 len += cur_len;
2098 }
2099 spin_unlock(&lu_gp->lu_gp_lock);
2100
2101 return len;
2102}
2103
2104SE_DEV_ALUA_LU_ATTR_RO(members);
2105
2106CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2107
2108static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2109 &target_core_alua_lu_gp_lu_gp_id.attr,
2110 &target_core_alua_lu_gp_members.attr,
2111 NULL,
2112};
2113
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002114static void target_core_alua_lu_gp_release(struct config_item *item)
2115{
2116 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2117 struct t10_alua_lu_gp, lu_gp_group);
2118
2119 core_alua_free_lu_gp(lu_gp);
2120}
2121
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002122static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002123 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002124 .show_attribute = target_core_alua_lu_gp_attr_show,
2125 .store_attribute = target_core_alua_lu_gp_attr_store,
2126};
2127
2128static struct config_item_type target_core_alua_lu_gp_cit = {
2129 .ct_item_ops = &target_core_alua_lu_gp_ops,
2130 .ct_attrs = target_core_alua_lu_gp_attrs,
2131 .ct_owner = THIS_MODULE,
2132};
2133
2134/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2135
2136/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2137
2138static struct config_group *target_core_alua_create_lu_gp(
2139 struct config_group *group,
2140 const char *name)
2141{
2142 struct t10_alua_lu_gp *lu_gp;
2143 struct config_group *alua_lu_gp_cg = NULL;
2144 struct config_item *alua_lu_gp_ci = NULL;
2145
2146 lu_gp = core_alua_allocate_lu_gp(name, 0);
2147 if (IS_ERR(lu_gp))
2148 return NULL;
2149
2150 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2151 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2152
2153 config_group_init_type_name(alua_lu_gp_cg, name,
2154 &target_core_alua_lu_gp_cit);
2155
Andy Grover6708bb22011-06-08 10:36:43 -07002156 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002157 " Group: core/alua/lu_gps/%s\n",
2158 config_item_name(alua_lu_gp_ci));
2159
2160 return alua_lu_gp_cg;
2161
2162}
2163
2164static void target_core_alua_drop_lu_gp(
2165 struct config_group *group,
2166 struct config_item *item)
2167{
2168 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2169 struct t10_alua_lu_gp, lu_gp_group);
2170
Andy Grover6708bb22011-06-08 10:36:43 -07002171 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002172 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2173 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002174 /*
2175 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2176 * -> target_core_alua_lu_gp_release()
2177 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002178 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002179}
2180
2181static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2182 .make_group = &target_core_alua_create_lu_gp,
2183 .drop_item = &target_core_alua_drop_lu_gp,
2184};
2185
2186static struct config_item_type target_core_alua_lu_gps_cit = {
2187 .ct_item_ops = NULL,
2188 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2189 .ct_owner = THIS_MODULE,
2190};
2191
2192/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2193
2194/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2195
2196CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2197#define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
2198static struct target_core_alua_tg_pt_gp_attribute \
2199 target_core_alua_tg_pt_gp_##_name = \
2200 __CONFIGFS_EATTR(_name, _mode, \
2201 target_core_alua_tg_pt_gp_show_attr_##_name, \
2202 target_core_alua_tg_pt_gp_store_attr_##_name);
2203
2204#define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
2205static struct target_core_alua_tg_pt_gp_attribute \
2206 target_core_alua_tg_pt_gp_##_name = \
2207 __CONFIGFS_EATTR_RO(_name, \
2208 target_core_alua_tg_pt_gp_show_attr_##_name);
2209
2210/*
2211 * alua_access_state
2212 */
2213static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2214 struct t10_alua_tg_pt_gp *tg_pt_gp,
2215 char *page)
2216{
2217 return sprintf(page, "%d\n",
2218 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2219}
2220
2221static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2222 struct t10_alua_tg_pt_gp *tg_pt_gp,
2223 const char *page,
2224 size_t count)
2225{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002226 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002227 unsigned long tmp;
2228 int new_state, ret;
2229
Andy Grover6708bb22011-06-08 10:36:43 -07002230 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002231 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002232 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2233 return -EINVAL;
2234 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07002235 if (!(dev->dev_flags & DF_CONFIGURED)) {
2236 pr_err("Unable to set alua_access_state while device is"
2237 " not configured\n");
2238 return -ENODEV;
2239 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002240
Jingoo Han57103d72013-07-19 16:22:19 +09002241 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002242 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002243 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002244 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002245 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002246 }
2247 new_state = (int)tmp;
2248
Hannes Reinecke125d0112013-11-19 09:07:46 +01002249 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2250 pr_err("Unable to process implicit configfs ALUA"
2251 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002252 return -EINVAL;
2253 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002254 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2255 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2256 /* LBA DEPENDENT is only allowed with implicit ALUA */
2257 pr_err("Unable to process implicit configfs ALUA transition"
2258 " while explicit ALUA management is enabled\n");
2259 return -EINVAL;
2260 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002261
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002262 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002263 NULL, NULL, new_state, 0);
2264 return (!ret) ? count : -EINVAL;
2265}
2266
2267SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2268
2269/*
2270 * alua_access_status
2271 */
2272static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2273 struct t10_alua_tg_pt_gp *tg_pt_gp,
2274 char *page)
2275{
2276 return sprintf(page, "%s\n",
2277 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2278}
2279
2280static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2281 struct t10_alua_tg_pt_gp *tg_pt_gp,
2282 const char *page,
2283 size_t count)
2284{
2285 unsigned long tmp;
2286 int new_status, ret;
2287
Andy Grover6708bb22011-06-08 10:36:43 -07002288 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2289 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002290 " valid tg_pt_gp ID: %hu\n",
2291 tg_pt_gp->tg_pt_gp_valid_id);
2292 return -EINVAL;
2293 }
2294
Jingoo Han57103d72013-07-19 16:22:19 +09002295 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002296 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002297 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002298 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002299 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002300 }
2301 new_status = (int)tmp;
2302
2303 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002304 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2305 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002306 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002307 new_status);
2308 return -EINVAL;
2309 }
2310
2311 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2312 return count;
2313}
2314
2315SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2316
2317/*
2318 * alua_access_type
2319 */
2320static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2321 struct t10_alua_tg_pt_gp *tg_pt_gp,
2322 char *page)
2323{
2324 return core_alua_show_access_type(tg_pt_gp, page);
2325}
2326
2327static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2328 struct t10_alua_tg_pt_gp *tg_pt_gp,
2329 const char *page,
2330 size_t count)
2331{
2332 return core_alua_store_access_type(tg_pt_gp, page, count);
2333}
2334
2335SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2336
2337/*
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002338 * alua_supported_states
2339 */
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002340
2341#define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit) \
2342static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2343 struct t10_alua_tg_pt_gp *t, char *p) \
2344{ \
2345 return sprintf(p, "%d\n", !!(t->_var & _bit)); \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002346}
2347
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002348#define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit) \
2349static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2350 struct t10_alua_tg_pt_gp *t, const char *p, size_t c) \
2351{ \
2352 unsigned long tmp; \
2353 int ret; \
2354 \
2355 if (!t->tg_pt_gp_valid_id) { \
2356 pr_err("Unable to do set ##_name ALUA state on non" \
2357 " valid tg_pt_gp ID: %hu\n", \
2358 t->tg_pt_gp_valid_id); \
2359 return -EINVAL; \
2360 } \
2361 \
2362 ret = kstrtoul(p, 0, &tmp); \
2363 if (ret < 0) { \
2364 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2365 return -EINVAL; \
2366 } \
2367 if (tmp > 1) { \
2368 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2369 return -EINVAL; \
2370 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002371 if (tmp) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002372 t->_var |= _bit; \
2373 else \
2374 t->_var &= ~_bit; \
2375 \
2376 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002377}
2378
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002379SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2380 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2381SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2382 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2383SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
2384
2385SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2386 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2387SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2388 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2389SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
2390
2391SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2392 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2393SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2394 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
Hannes Reineckec66094b2013-12-17 09:18:49 +01002395SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002396
2397SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2398 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2399SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2400 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2401SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
2402
2403SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2404 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2405SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2406 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2407SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2408
2409SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2410 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2411SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2412 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2413SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2414
2415SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2416 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2417SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2418 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2419SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002420
2421/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002422 * alua_write_metadata
2423 */
2424static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2425 struct t10_alua_tg_pt_gp *tg_pt_gp,
2426 char *page)
2427{
2428 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2429}
2430
2431static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2432 struct t10_alua_tg_pt_gp *tg_pt_gp,
2433 const char *page,
2434 size_t count)
2435{
2436 unsigned long tmp;
2437 int ret;
2438
Jingoo Han57103d72013-07-19 16:22:19 +09002439 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002440 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002441 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002442 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002443 }
2444
2445 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002446 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002447 " %lu\n", tmp);
2448 return -EINVAL;
2449 }
2450 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2451
2452 return count;
2453}
2454
2455SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2456
2457
2458
2459/*
2460 * nonop_delay_msecs
2461 */
2462static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2463 struct t10_alua_tg_pt_gp *tg_pt_gp,
2464 char *page)
2465{
2466 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2467
2468}
2469
2470static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2471 struct t10_alua_tg_pt_gp *tg_pt_gp,
2472 const char *page,
2473 size_t count)
2474{
2475 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2476}
2477
2478SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2479
2480/*
2481 * trans_delay_msecs
2482 */
2483static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2484 struct t10_alua_tg_pt_gp *tg_pt_gp,
2485 char *page)
2486{
2487 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2488}
2489
2490static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2491 struct t10_alua_tg_pt_gp *tg_pt_gp,
2492 const char *page,
2493 size_t count)
2494{
2495 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2496}
2497
2498SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2499
2500/*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002501 * implicit_trans_secs
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002502 */
Hannes Reinecke125d0112013-11-19 09:07:46 +01002503static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002504 struct t10_alua_tg_pt_gp *tg_pt_gp,
2505 char *page)
2506{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002507 return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002508}
2509
Hannes Reinecke125d0112013-11-19 09:07:46 +01002510static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002511 struct t10_alua_tg_pt_gp *tg_pt_gp,
2512 const char *page,
2513 size_t count)
2514{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002515 return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002516}
2517
Hannes Reinecke125d0112013-11-19 09:07:46 +01002518SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002519
2520/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002521 * preferred
2522 */
2523
2524static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2525 struct t10_alua_tg_pt_gp *tg_pt_gp,
2526 char *page)
2527{
2528 return core_alua_show_preferred_bit(tg_pt_gp, page);
2529}
2530
2531static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2532 struct t10_alua_tg_pt_gp *tg_pt_gp,
2533 const char *page,
2534 size_t count)
2535{
2536 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2537}
2538
2539SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2540
2541/*
2542 * tg_pt_gp_id
2543 */
2544static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2545 struct t10_alua_tg_pt_gp *tg_pt_gp,
2546 char *page)
2547{
Andy Grover6708bb22011-06-08 10:36:43 -07002548 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002549 return 0;
2550
2551 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2552}
2553
2554static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2555 struct t10_alua_tg_pt_gp *tg_pt_gp,
2556 const char *page,
2557 size_t count)
2558{
2559 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2560 unsigned long tg_pt_gp_id;
2561 int ret;
2562
Jingoo Han57103d72013-07-19 16:22:19 +09002563 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002564 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002565 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002566 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002567 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002568 }
2569 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002570 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002571 " 0x0000ffff\n", tg_pt_gp_id);
2572 return -EINVAL;
2573 }
2574
2575 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2576 if (ret < 0)
2577 return -EINVAL;
2578
Andy Grover6708bb22011-06-08 10:36:43 -07002579 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002580 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2581 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2582 tg_pt_gp->tg_pt_gp_id);
2583
2584 return count;
2585}
2586
2587SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2588
2589/*
2590 * members
2591 */
2592static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2593 struct t10_alua_tg_pt_gp *tg_pt_gp,
2594 char *page)
2595{
2596 struct se_port *port;
2597 struct se_portal_group *tpg;
2598 struct se_lun *lun;
2599 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2600 ssize_t len = 0, cur_len;
2601 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2602
2603 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2604
2605 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2606 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2607 tg_pt_gp_mem_list) {
2608 port = tg_pt_gp_mem->tg_pt;
2609 tpg = port->sep_tpg;
2610 lun = port->sep_lun;
2611
2612 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002613 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2614 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2615 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002616 config_item_name(&lun->lun_group.cg_item));
2617 cur_len++; /* Extra byte for NULL terminator */
2618
2619 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002620 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002621 "_members buffer\n");
2622 break;
2623 }
2624 memcpy(page+len, buf, cur_len);
2625 len += cur_len;
2626 }
2627 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2628
2629 return len;
2630}
2631
2632SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2633
2634CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2635 tg_pt_gp_group);
2636
2637static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2638 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2639 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2640 &target_core_alua_tg_pt_gp_alua_access_type.attr,
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002641 &target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2642 &target_core_alua_tg_pt_gp_alua_support_offline.attr,
2643 &target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2644 &target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2645 &target_core_alua_tg_pt_gp_alua_support_standby.attr,
2646 &target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2647 &target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002648 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2649 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2650 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
Hannes Reinecke125d0112013-11-19 09:07:46 +01002651 &target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002652 &target_core_alua_tg_pt_gp_preferred.attr,
2653 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2654 &target_core_alua_tg_pt_gp_members.attr,
2655 NULL,
2656};
2657
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002658static 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
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002666static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002667 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002668 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2669 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2670};
2671
2672static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2673 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2674 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2675 .ct_owner = THIS_MODULE,
2676};
2677
2678/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2679
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002680/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002681
2682static struct config_group *target_core_alua_create_tg_pt_gp(
2683 struct config_group *group,
2684 const char *name)
2685{
2686 struct t10_alua *alua = container_of(group, struct t10_alua,
2687 alua_tg_pt_gps_group);
2688 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002689 struct config_group *alua_tg_pt_gp_cg = NULL;
2690 struct config_item *alua_tg_pt_gp_ci = NULL;
2691
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002692 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002693 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002694 return NULL;
2695
2696 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2697 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2698
2699 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2700 &target_core_alua_tg_pt_gp_cit);
2701
Andy Grover6708bb22011-06-08 10:36:43 -07002702 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002703 " Group: alua/tg_pt_gps/%s\n",
2704 config_item_name(alua_tg_pt_gp_ci));
2705
2706 return alua_tg_pt_gp_cg;
2707}
2708
2709static void target_core_alua_drop_tg_pt_gp(
2710 struct config_group *group,
2711 struct config_item *item)
2712{
2713 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2714 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2715
Andy Grover6708bb22011-06-08 10:36:43 -07002716 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002717 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2718 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002719 /*
2720 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2721 * -> target_core_alua_tg_pt_gp_release().
2722 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002723 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002724}
2725
2726static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2727 .make_group = &target_core_alua_create_tg_pt_gp,
2728 .drop_item = &target_core_alua_drop_tg_pt_gp,
2729};
2730
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002731TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002732
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002733/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002734
2735/* Start functions for struct config_item_type target_core_alua_cit */
2736
2737/*
2738 * target_core_alua_cit is a ConfigFS group that lives under
2739 * /sys/kernel/config/target/core/alua. There are default groups
2740 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2741 * target_core_alua_cit in target_core_init_configfs() below.
2742 */
2743static struct config_item_type target_core_alua_cit = {
2744 .ct_item_ops = NULL,
2745 .ct_attrs = NULL,
2746 .ct_owner = THIS_MODULE,
2747};
2748
2749/* End functions for struct config_item_type target_core_alua_cit */
2750
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002751/* Start functions for struct config_item_type target_core_stat_cit */
2752
2753static struct config_group *target_core_stat_mkdir(
2754 struct config_group *group,
2755 const char *name)
2756{
2757 return ERR_PTR(-ENOSYS);
2758}
2759
2760static void target_core_stat_rmdir(
2761 struct config_group *group,
2762 struct config_item *item)
2763{
2764 return;
2765}
2766
2767static struct configfs_group_operations target_core_stat_group_ops = {
2768 .make_group = &target_core_stat_mkdir,
2769 .drop_item = &target_core_stat_rmdir,
2770};
2771
2772static struct config_item_type target_core_stat_cit = {
2773 .ct_group_ops = &target_core_stat_group_ops,
2774 .ct_owner = THIS_MODULE,
2775};
2776
2777/* End functions for struct config_item_type target_core_stat_cit */
2778
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002779/* Start functions for struct config_item_type target_core_hba_cit */
2780
2781static struct config_group *target_core_make_subdev(
2782 struct config_group *group,
2783 const char *name)
2784{
2785 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002786 struct se_subsystem_api *t;
2787 struct config_item *hba_ci = &group->cg_item;
2788 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002789 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002790 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002791 struct config_group *dev_stat_grp = NULL;
2792 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002793
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002794 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2795 if (ret)
2796 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002797 /*
2798 * Locate the struct se_subsystem_api from parent's struct se_hba.
2799 */
2800 t = hba->transport;
2801
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002802 dev = target_alloc_device(hba, name);
2803 if (!dev)
2804 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002805
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002806 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002807
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002808 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002809 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002810 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002811 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002812
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002813 config_group_init_type_name(dev_cg, name, &t->tb_cits.tb_dev_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002814 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08002815 &t->tb_cits.tb_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002816 config_group_init_type_name(&dev->dev_pr_group, "pr",
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08002817 &t->tb_cits.tb_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002818 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08002819 &t->tb_cits.tb_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002820 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002821 "alua", &t->tb_cits.tb_dev_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002822 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002823 "statistics", &target_core_stat_cit);
2824
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002825 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2826 dev_cg->default_groups[1] = &dev->dev_pr_group;
2827 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2828 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2829 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002830 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002831 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002832 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002833 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002834 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002835 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002836 goto out_free_dev_cg_default_groups;
2837 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002838
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002839 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002840 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002841 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002842 if (!tg_pt_gp_cg->default_groups) {
2843 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002844 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002845 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002846 }
2847
2848 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2849 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2850 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2851 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002852 /*
2853 * Add core/$HBA/$DEV/statistics/ default groups
2854 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002855 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002856 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002857 GFP_KERNEL);
2858 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002859 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002860 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002861 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002862 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002863
2864 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002865 return dev_cg;
2866
2867out_free_tg_pt_gp_cg_default_groups:
2868 kfree(tg_pt_gp_cg->default_groups);
2869out_free_tg_pt_gp:
2870 core_alua_free_tg_pt_gp(tg_pt_gp);
2871out_free_dev_cg_default_groups:
2872 kfree(dev_cg->default_groups);
2873out_free_device:
2874 target_free_device(dev);
2875out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002876 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002877 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002878}
2879
2880static void target_core_drop_subdev(
2881 struct config_group *group,
2882 struct config_item *item)
2883{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002884 struct config_group *dev_cg = to_config_group(item);
2885 struct se_device *dev =
2886 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002887 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002888 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002889 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002890 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002891
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002892 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002893
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002894 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002895
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002896 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002897 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2898 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2899 dev_stat_grp->default_groups[i] = NULL;
2900 config_item_put(df_item);
2901 }
2902 kfree(dev_stat_grp->default_groups);
2903
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002904 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002905 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2906 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2907 tg_pt_gp_cg->default_groups[i] = NULL;
2908 config_item_put(df_item);
2909 }
2910 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002911 /*
2912 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2913 * directly from target_core_alua_tg_pt_gp_release().
2914 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002915 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002916
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002917 for (i = 0; dev_cg->default_groups[i]; i++) {
2918 df_item = &dev_cg->default_groups[i]->cg_item;
2919 dev_cg->default_groups[i] = NULL;
2920 config_item_put(df_item);
2921 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002922 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002923 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002924 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002925 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002926 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002927}
2928
2929static struct configfs_group_operations target_core_hba_group_ops = {
2930 .make_group = target_core_make_subdev,
2931 .drop_item = target_core_drop_subdev,
2932};
2933
2934CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2935#define SE_HBA_ATTR(_name, _mode) \
2936static struct target_core_hba_attribute \
2937 target_core_hba_##_name = \
2938 __CONFIGFS_EATTR(_name, _mode, \
2939 target_core_hba_show_attr_##_name, \
2940 target_core_hba_store_attr_##_name);
2941
2942#define SE_HBA_ATTR_RO(_name) \
2943static struct target_core_hba_attribute \
2944 target_core_hba_##_name = \
2945 __CONFIGFS_EATTR_RO(_name, \
2946 target_core_hba_show_attr_##_name);
2947
2948static ssize_t target_core_hba_show_attr_hba_info(
2949 struct se_hba *hba,
2950 char *page)
2951{
2952 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2953 hba->hba_id, hba->transport->name,
2954 TARGET_CORE_CONFIGFS_VERSION);
2955}
2956
2957SE_HBA_ATTR_RO(hba_info);
2958
2959static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2960 char *page)
2961{
2962 int hba_mode = 0;
2963
2964 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2965 hba_mode = 1;
2966
2967 return sprintf(page, "%d\n", hba_mode);
2968}
2969
2970static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2971 const char *page, size_t count)
2972{
2973 struct se_subsystem_api *transport = hba->transport;
2974 unsigned long mode_flag;
2975 int ret;
2976
2977 if (transport->pmode_enable_hba == NULL)
2978 return -EINVAL;
2979
Jingoo Han57103d72013-07-19 16:22:19 +09002980 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002981 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002982 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002983 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002984 }
2985
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002986 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07002987 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002988 return -EINVAL;
2989 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002990
2991 ret = transport->pmode_enable_hba(hba, mode_flag);
2992 if (ret < 0)
2993 return -EINVAL;
2994 if (ret > 0)
2995 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2996 else if (ret == 0)
2997 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2998
2999 return count;
3000}
3001
3002SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
3003
3004CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
3005
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003006static void target_core_hba_release(struct config_item *item)
3007{
3008 struct se_hba *hba = container_of(to_config_group(item),
3009 struct se_hba, hba_group);
3010 core_delete_hba(hba);
3011}
3012
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003013static struct configfs_attribute *target_core_hba_attrs[] = {
3014 &target_core_hba_hba_info.attr,
3015 &target_core_hba_hba_mode.attr,
3016 NULL,
3017};
3018
3019static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003020 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003021 .show_attribute = target_core_hba_attr_show,
3022 .store_attribute = target_core_hba_attr_store,
3023};
3024
3025static struct config_item_type target_core_hba_cit = {
3026 .ct_item_ops = &target_core_hba_item_ops,
3027 .ct_group_ops = &target_core_hba_group_ops,
3028 .ct_attrs = target_core_hba_attrs,
3029 .ct_owner = THIS_MODULE,
3030};
3031
3032static struct config_group *target_core_call_addhbatotarget(
3033 struct config_group *group,
3034 const char *name)
3035{
3036 char *se_plugin_str, *str, *str2;
3037 struct se_hba *hba;
3038 char buf[TARGET_CORE_NAME_MAX_LEN];
3039 unsigned long plugin_dep_id = 0;
3040 int ret;
3041
3042 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07003043 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07003044 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003045 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3046 TARGET_CORE_NAME_MAX_LEN);
3047 return ERR_PTR(-ENAMETOOLONG);
3048 }
3049 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3050
3051 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003052 if (!str) {
3053 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003054 return ERR_PTR(-EINVAL);
3055 }
3056 se_plugin_str = buf;
3057 /*
3058 * Special case for subsystem plugins that have "_" in their names.
3059 * Namely rd_direct and rd_mcp..
3060 */
3061 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003062 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003063 *str2 = '\0'; /* Terminate for *se_plugin_str */
3064 str2++; /* Skip to start of plugin dependent ID */
3065 str = str2;
3066 } else {
3067 *str = '\0'; /* Terminate for *se_plugin_str */
3068 str++; /* Skip to start of plugin dependent ID */
3069 }
3070
Jingoo Han57103d72013-07-19 16:22:19 +09003071 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003072 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09003073 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003074 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003075 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003076 }
3077 /*
3078 * Load up TCM subsystem plugins if they have not already been loaded.
3079 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07003080 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003081
3082 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3083 if (IS_ERR(hba))
3084 return ERR_CAST(hba);
3085
3086 config_group_init_type_name(&hba->hba_group, name,
3087 &target_core_hba_cit);
3088
3089 return &hba->hba_group;
3090}
3091
3092static void target_core_call_delhbafromtarget(
3093 struct config_group *group,
3094 struct config_item *item)
3095{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003096 /*
3097 * core_delete_hba() is called from target_core_hba_item_ops->release()
3098 * -> target_core_hba_release()
3099 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003100 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003101}
3102
3103static struct configfs_group_operations target_core_group_ops = {
3104 .make_group = target_core_call_addhbatotarget,
3105 .drop_item = target_core_call_delhbafromtarget,
3106};
3107
3108static struct config_item_type target_core_cit = {
3109 .ct_item_ops = NULL,
3110 .ct_group_ops = &target_core_group_ops,
3111 .ct_attrs = NULL,
3112 .ct_owner = THIS_MODULE,
3113};
3114
3115/* Stop functions for struct config_item_type target_core_hba_cit */
3116
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003117void target_core_setup_sub_cits(struct se_subsystem_api *sa)
3118{
3119 target_core_setup_dev_cit(sa);
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08003120 target_core_setup_dev_attrib_cit(sa);
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08003121 target_core_setup_dev_pr_cit(sa);
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08003122 target_core_setup_dev_wwn_cit(sa);
Nicholas Bellinger72aca572014-11-27 15:06:23 -08003123 target_core_setup_dev_alua_tg_pt_gps_cit(sa);
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003124}
3125EXPORT_SYMBOL(target_core_setup_sub_cits);
3126
Axel Lin54550fa2011-03-14 04:06:09 -07003127static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003128{
3129 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3130 struct config_group *lu_gp_cg = NULL;
3131 struct configfs_subsystem *subsys;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003132 struct t10_alua_lu_gp *lu_gp;
3133 int ret;
3134
Andy Grover6708bb22011-06-08 10:36:43 -07003135 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003136 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3137 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3138
3139 subsys = target_core_subsystem[0];
3140 config_group_init(&subsys->su_group);
3141 mutex_init(&subsys->su_mutex);
3142
Andy Grovere3d6f902011-07-19 08:55:10 +00003143 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003144 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003145 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003146 /*
3147 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3148 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3149 */
3150 target_cg = &subsys->su_group;
Andy Groverab6dae82013-12-09 14:27:36 -08003151 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003152 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003153 if (!target_cg->default_groups) {
3154 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003155 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003156 goto out_global;
3157 }
3158
Andy Grovere3d6f902011-07-19 08:55:10 +00003159 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003160 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003161 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003162 target_cg->default_groups[1] = NULL;
3163 /*
3164 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3165 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003166 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003167 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003168 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003169 if (!hba_cg->default_groups) {
3170 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003171 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003172 goto out_global;
3173 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003174 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003175 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003176 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003177 hba_cg->default_groups[1] = NULL;
3178 /*
3179 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3180 * groups under /sys/kernel/config/target/core/alua/
3181 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003182 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003183 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003184 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003185 if (!alua_cg->default_groups) {
3186 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003187 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003188 goto out_global;
3189 }
3190
Andy Grovere3d6f902011-07-19 08:55:10 +00003191 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003192 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003193 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003194 alua_cg->default_groups[1] = NULL;
3195 /*
3196 * Add core/alua/lu_gps/default_lu_gp
3197 */
3198 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003199 if (IS_ERR(lu_gp)) {
3200 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003201 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003202 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003203
Andy Grovere3d6f902011-07-19 08:55:10 +00003204 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003205 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003206 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003207 if (!lu_gp_cg->default_groups) {
3208 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003209 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003210 goto out_global;
3211 }
3212
3213 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3214 &target_core_alua_lu_gp_cit);
3215 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3216 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003217 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003218 /*
3219 * Register the target_core_mod subsystem with configfs.
3220 */
3221 ret = configfs_register_subsystem(subsys);
3222 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003223 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003224 ret, subsys->su_group.cg_item.ci_namebuf);
3225 goto out_global;
3226 }
Andy Grover6708bb22011-06-08 10:36:43 -07003227 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003228 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3229 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3230 /*
3231 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3232 */
3233 ret = rd_module_init();
3234 if (ret < 0)
3235 goto out;
3236
Roland Dreier0d0f9df2012-10-31 09:16:44 -07003237 ret = core_dev_setup_virtual_lun0();
3238 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003239 goto out;
3240
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003241 ret = target_xcopy_setup_pt();
3242 if (ret < 0)
3243 goto out;
3244
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003245 return 0;
3246
3247out:
3248 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003249 core_dev_release_virtual_lun0();
3250 rd_module_exit();
3251out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00003252 if (default_lu_gp) {
3253 core_alua_free_lu_gp(default_lu_gp);
3254 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003255 }
3256 if (lu_gp_cg)
3257 kfree(lu_gp_cg->default_groups);
3258 if (alua_cg)
3259 kfree(alua_cg->default_groups);
3260 if (hba_cg)
3261 kfree(hba_cg->default_groups);
3262 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00003263 release_se_kmem_caches();
3264 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003265}
3266
Axel Lin54550fa2011-03-14 04:06:09 -07003267static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003268{
3269 struct configfs_subsystem *subsys;
3270 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3271 struct config_item *item;
3272 int i;
3273
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003274 subsys = target_core_subsystem[0];
3275
Andy Grovere3d6f902011-07-19 08:55:10 +00003276 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003277 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3278 item = &lu_gp_cg->default_groups[i]->cg_item;
3279 lu_gp_cg->default_groups[i] = NULL;
3280 config_item_put(item);
3281 }
3282 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003283 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003284
Andy Grovere3d6f902011-07-19 08:55:10 +00003285 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003286 for (i = 0; alua_cg->default_groups[i]; i++) {
3287 item = &alua_cg->default_groups[i]->cg_item;
3288 alua_cg->default_groups[i] = NULL;
3289 config_item_put(item);
3290 }
3291 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003292 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003293
Andy Grovere3d6f902011-07-19 08:55:10 +00003294 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003295 for (i = 0; hba_cg->default_groups[i]; i++) {
3296 item = &hba_cg->default_groups[i]->cg_item;
3297 hba_cg->default_groups[i] = NULL;
3298 config_item_put(item);
3299 }
3300 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003301 hba_cg->default_groups = NULL;
3302 /*
3303 * We expect subsys->su_group.default_groups to be released
3304 * by configfs subsystem provider logic..
3305 */
3306 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003307 kfree(subsys->su_group.default_groups);
3308
Andy Grovere3d6f902011-07-19 08:55:10 +00003309 core_alua_free_lu_gp(default_lu_gp);
3310 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003311
Andy Grover6708bb22011-06-08 10:36:43 -07003312 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003313 " Infrastructure\n");
3314
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003315 core_dev_release_virtual_lun0();
3316 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003317 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003318 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003319}
3320
3321MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3322MODULE_AUTHOR("nab@Linux-iSCSI.org");
3323MODULE_LICENSE("GPL");
3324
3325module_init(target_core_init_configfs);
3326module_exit(target_core_exit_configfs);