blob: 78ed857cc7b94d04023dd1da224c78f1df09d56a [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
775/* Start functions for struct config_item_type target_core_dev_wwn_cit */
776
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
1006static struct config_item_type target_core_dev_wwn_cit = {
1007 .ct_item_ops = &target_core_dev_wwn_ops,
1008 .ct_attrs = target_core_dev_wwn_attrs,
1009 .ct_owner = THIS_MODULE,
1010};
1011
1012/* End functions for struct config_item_type target_core_dev_wwn_cit */
1013
1014/* Start functions for struct config_item_type target_core_dev_pr_cit */
1015
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001016CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001017#define SE_DEV_PR_ATTR(_name, _mode) \
1018static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1019 __CONFIGFS_EATTR(_name, _mode, \
1020 target_core_dev_pr_show_attr_##_name, \
1021 target_core_dev_pr_store_attr_##_name);
1022
1023#define SE_DEV_PR_ATTR_RO(_name); \
1024static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1025 __CONFIGFS_EATTR_RO(_name, \
1026 target_core_dev_pr_show_attr_##_name);
1027
Christoph Hellwigd977f432012-10-10 17:37:15 -04001028static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1029 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001030{
1031 struct se_node_acl *se_nacl;
1032 struct t10_pr_registration *pr_reg;
1033 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001034
1035 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1036
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001037 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001038 if (!pr_reg)
1039 return sprintf(page, "No SPC-3 Reservation holder\n");
1040
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001041 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -07001042 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001043
Christoph Hellwigd977f432012-10-10 17:37:15 -04001044 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001045 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001046 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001047}
1048
Christoph Hellwigd977f432012-10-10 17:37:15 -04001049static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1050 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001051{
1052 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001053 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001054
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001055 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001056 if (se_nacl) {
1057 len = sprintf(page,
1058 "SPC-2 Reservation: %s Initiator: %s\n",
1059 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1060 se_nacl->initiatorname);
1061 } else {
1062 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001063 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001064 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001065}
1066
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001067static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
1068 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001069{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001070 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001071
Christoph Hellwigd977f432012-10-10 17:37:15 -04001072 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1073 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001074
Christoph Hellwigd977f432012-10-10 17:37:15 -04001075 spin_lock(&dev->dev_reservation_lock);
1076 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1077 ret = target_core_dev_pr_show_spc2_res(dev, page);
1078 else
1079 ret = target_core_dev_pr_show_spc3_res(dev, page);
1080 spin_unlock(&dev->dev_reservation_lock);
1081 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001082}
1083
1084SE_DEV_PR_ATTR_RO(res_holder);
1085
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001086static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001087 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001088{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001089 ssize_t len = 0;
1090
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001091 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001092 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001093 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001094 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001095 len = sprintf(page, "SPC-3 Reservation: All Target"
1096 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001097 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001098 len = sprintf(page, "SPC-3 Reservation: Single"
1099 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001100 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001101
Christoph Hellwigd977f432012-10-10 17:37:15 -04001102 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001103 return len;
1104}
1105
1106SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1107
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001108static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001109 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001110{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001111 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001112}
1113
1114SE_DEV_PR_ATTR_RO(res_pr_generation);
1115
1116/*
1117 * res_pr_holder_tg_port
1118 */
1119static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001120 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001121{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001122 struct se_node_acl *se_nacl;
1123 struct se_lun *lun;
1124 struct se_portal_group *se_tpg;
1125 struct t10_pr_registration *pr_reg;
1126 struct target_core_fabric_ops *tfo;
1127 ssize_t len = 0;
1128
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001129 spin_lock(&dev->dev_reservation_lock);
1130 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001131 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001132 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001133 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001134 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001135
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001136 se_nacl = pr_reg->pr_reg_nacl;
1137 se_tpg = se_nacl->se_tpg;
1138 lun = pr_reg->pr_reg_tg_pt_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +00001139 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001140
1141 len += sprintf(page+len, "SPC-3 Reservation: %s"
1142 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1143 tfo->tpg_get_wwn(se_tpg));
1144 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001145 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001146 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1147 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1148 tfo->get_fabric_name(), lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001149
Christoph Hellwigd977f432012-10-10 17:37:15 -04001150out_unlock:
1151 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001152 return len;
1153}
1154
1155SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1156
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001157static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001158 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001159{
1160 struct target_core_fabric_ops *tfo;
1161 struct t10_pr_registration *pr_reg;
1162 unsigned char buf[384];
1163 char i_buf[PR_REG_ISID_ID_LEN];
1164 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001165 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001166
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001167 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1168
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001169 spin_lock(&dev->t10_pr.registration_lock);
1170 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001171 pr_reg_list) {
1172
1173 memset(buf, 0, 384);
1174 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1175 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -07001176 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001177 PR_REG_ISID_ID_LEN);
1178 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1179 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001180 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001181 pr_reg->pr_res_generation);
1182
Andy Grover6708bb22011-06-08 10:36:43 -07001183 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001184 break;
1185
1186 len += sprintf(page+len, "%s", buf);
1187 reg_count++;
1188 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001189 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001190
Andy Grover6708bb22011-06-08 10:36:43 -07001191 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001192 len += sprintf(page+len, "None\n");
1193
1194 return len;
1195}
1196
1197SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1198
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001199static ssize_t target_core_dev_pr_show_attr_res_pr_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001200 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001201{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001202 struct t10_pr_registration *pr_reg;
1203 ssize_t len = 0;
1204
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001205 spin_lock(&dev->dev_reservation_lock);
1206 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001207 if (pr_reg) {
1208 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1209 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1210 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001211 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001212 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001213
Christoph Hellwigd977f432012-10-10 17:37:15 -04001214 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001215 return len;
1216}
1217
1218SE_DEV_PR_ATTR_RO(res_pr_type);
1219
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001220static ssize_t target_core_dev_pr_show_attr_res_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001221 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001222{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001223 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1224 return sprintf(page, "SPC_PASSTHROUGH\n");
1225 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1226 return sprintf(page, "SPC2_RESERVATIONS\n");
1227 else
1228 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001229}
1230
1231SE_DEV_PR_ATTR_RO(res_type);
1232
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001233static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001234 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001235{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001236 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001237 return 0;
1238
1239 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001240 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001241}
1242
1243SE_DEV_PR_ATTR_RO(res_aptpl_active);
1244
1245/*
1246 * res_aptpl_metadata
1247 */
1248static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001249 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001250{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001251 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001252 return 0;
1253
1254 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1255}
1256
1257enum {
1258 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1259 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1260 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1261 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1262};
1263
1264static match_table_t tokens = {
1265 {Opt_initiator_fabric, "initiator_fabric=%s"},
1266 {Opt_initiator_node, "initiator_node=%s"},
1267 {Opt_initiator_sid, "initiator_sid=%s"},
1268 {Opt_sa_res_key, "sa_res_key=%s"},
1269 {Opt_res_holder, "res_holder=%d"},
1270 {Opt_res_type, "res_type=%d"},
1271 {Opt_res_scope, "res_scope=%d"},
1272 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1273 {Opt_mapped_lun, "mapped_lun=%d"},
1274 {Opt_target_fabric, "target_fabric=%s"},
1275 {Opt_target_node, "target_node=%s"},
1276 {Opt_tpgt, "tpgt=%d"},
1277 {Opt_port_rtpi, "port_rtpi=%d"},
1278 {Opt_target_lun, "target_lun=%d"},
1279 {Opt_err, NULL}
1280};
1281
1282static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001283 struct se_device *dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001284 const char *page,
1285 size_t count)
1286{
Jesper Juhl6d180252011-03-14 04:05:56 -07001287 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1288 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -04001289 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001290 substring_t args[MAX_OPT_ARGS];
1291 unsigned long long tmp_ll;
1292 u64 sa_res_key = 0;
1293 u32 mapped_lun = 0, target_lun = 0;
1294 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1295 u16 port_rpti = 0, tpgt = 0;
1296 u8 type = 0, scope;
1297
Christoph Hellwigd977f432012-10-10 17:37:15 -04001298 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1299 return 0;
1300 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001301 return 0;
1302
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001303 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001304 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001305 " active fabric exports exist\n");
1306 return -EINVAL;
1307 }
1308
1309 opts = kstrdup(page, GFP_KERNEL);
1310 if (!opts)
1311 return -ENOMEM;
1312
1313 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001314 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001315 if (!*ptr)
1316 continue;
1317
1318 token = match_token(ptr, tokens, args);
1319 switch (token) {
1320 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001321 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001322 if (!i_fabric) {
1323 ret = -ENOMEM;
1324 goto out;
1325 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001326 break;
1327 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001328 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001329 if (!i_port) {
1330 ret = -ENOMEM;
1331 goto out;
1332 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001333 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001334 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001335 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1336 PR_APTPL_MAX_IPORT_LEN);
1337 ret = -EINVAL;
1338 break;
1339 }
1340 break;
1341 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001342 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001343 if (!isid) {
1344 ret = -ENOMEM;
1345 goto out;
1346 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001347 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001348 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001349 "= exceeds PR_REG_ISID_LEN: %d\n",
1350 PR_REG_ISID_LEN);
1351 ret = -EINVAL;
1352 break;
1353 }
1354 break;
1355 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001356 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001357 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001358 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001359 goto out;
1360 }
1361 sa_res_key = (u64)tmp_ll;
1362 break;
1363 /*
1364 * PR APTPL Metadata for Reservation
1365 */
1366 case Opt_res_holder:
1367 match_int(args, &arg);
1368 res_holder = arg;
1369 break;
1370 case Opt_res_type:
1371 match_int(args, &arg);
1372 type = (u8)arg;
1373 break;
1374 case Opt_res_scope:
1375 match_int(args, &arg);
1376 scope = (u8)arg;
1377 break;
1378 case Opt_res_all_tg_pt:
1379 match_int(args, &arg);
1380 all_tg_pt = (int)arg;
1381 break;
1382 case Opt_mapped_lun:
1383 match_int(args, &arg);
1384 mapped_lun = (u32)arg;
1385 break;
1386 /*
1387 * PR APTPL Metadata for Target Port
1388 */
1389 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001390 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001391 if (!t_fabric) {
1392 ret = -ENOMEM;
1393 goto out;
1394 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001395 break;
1396 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001397 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001398 if (!t_port) {
1399 ret = -ENOMEM;
1400 goto out;
1401 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001402 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001403 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001404 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1405 PR_APTPL_MAX_TPORT_LEN);
1406 ret = -EINVAL;
1407 break;
1408 }
1409 break;
1410 case Opt_tpgt:
1411 match_int(args, &arg);
1412 tpgt = (u16)arg;
1413 break;
1414 case Opt_port_rtpi:
1415 match_int(args, &arg);
1416 port_rpti = (u16)arg;
1417 break;
1418 case Opt_target_lun:
1419 match_int(args, &arg);
1420 target_lun = (u32)arg;
1421 break;
1422 default:
1423 break;
1424 }
1425 }
1426
Andy Grover6708bb22011-06-08 10:36:43 -07001427 if (!i_port || !t_port || !sa_res_key) {
1428 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001429 ret = -EINVAL;
1430 goto out;
1431 }
1432
1433 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001434 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001435 " holder\n", type);
1436 ret = -EINVAL;
1437 goto out;
1438 }
1439
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001440 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001441 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1442 res_holder, all_tg_pt, type);
1443out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001444 kfree(i_fabric);
1445 kfree(i_port);
1446 kfree(isid);
1447 kfree(t_fabric);
1448 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001449 kfree(orig);
1450 return (ret == 0) ? count : ret;
1451}
1452
1453SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1454
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001455CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001456
1457static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1458 &target_core_dev_pr_res_holder.attr,
1459 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1460 &target_core_dev_pr_res_pr_generation.attr,
1461 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1462 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1463 &target_core_dev_pr_res_pr_type.attr,
1464 &target_core_dev_pr_res_type.attr,
1465 &target_core_dev_pr_res_aptpl_active.attr,
1466 &target_core_dev_pr_res_aptpl_metadata.attr,
1467 NULL,
1468};
1469
1470static struct configfs_item_operations target_core_dev_pr_ops = {
1471 .show_attribute = target_core_dev_pr_attr_show,
1472 .store_attribute = target_core_dev_pr_attr_store,
1473};
1474
1475static struct config_item_type target_core_dev_pr_cit = {
1476 .ct_item_ops = &target_core_dev_pr_ops,
1477 .ct_attrs = target_core_dev_pr_attrs,
1478 .ct_owner = THIS_MODULE,
1479};
1480
1481/* End functions for struct config_item_type target_core_dev_pr_cit */
1482
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001483/* Start functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001484
1485static ssize_t target_core_show_dev_info(void *p, char *page)
1486{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001487 struct se_device *dev = p;
1488 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001489 int bl = 0;
1490 ssize_t read_bytes = 0;
1491
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001492 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001493 read_bytes += bl;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001494 read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001495 return read_bytes;
1496}
1497
1498static struct target_core_configfs_attribute target_core_attr_dev_info = {
1499 .attr = { .ca_owner = THIS_MODULE,
1500 .ca_name = "info",
1501 .ca_mode = S_IRUGO },
1502 .show = target_core_show_dev_info,
1503 .store = NULL,
1504};
1505
1506static ssize_t target_core_store_dev_control(
1507 void *p,
1508 const char *page,
1509 size_t count)
1510{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001511 struct se_device *dev = p;
1512 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001513
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001514 return t->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001515}
1516
1517static struct target_core_configfs_attribute target_core_attr_dev_control = {
1518 .attr = { .ca_owner = THIS_MODULE,
1519 .ca_name = "control",
1520 .ca_mode = S_IWUSR },
1521 .show = NULL,
1522 .store = target_core_store_dev_control,
1523};
1524
1525static ssize_t target_core_show_dev_alias(void *p, char *page)
1526{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001527 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001528
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001529 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001530 return 0;
1531
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001532 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001533}
1534
1535static ssize_t target_core_store_dev_alias(
1536 void *p,
1537 const char *page,
1538 size_t count)
1539{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001540 struct se_device *dev = p;
1541 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001542 ssize_t read_bytes;
1543
1544 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001545 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001546 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1547 SE_DEV_ALIAS_LEN-1);
1548 return -EINVAL;
1549 }
1550
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001551 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001552 if (!read_bytes)
1553 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001554 if (dev->dev_alias[read_bytes - 1] == '\n')
1555 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001556
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001557 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001558
Andy Grover6708bb22011-06-08 10:36:43 -07001559 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001560 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001561 config_item_name(&dev->dev_group.cg_item),
1562 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001563
1564 return read_bytes;
1565}
1566
1567static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1568 .attr = { .ca_owner = THIS_MODULE,
1569 .ca_name = "alias",
1570 .ca_mode = S_IRUGO | S_IWUSR },
1571 .show = target_core_show_dev_alias,
1572 .store = target_core_store_dev_alias,
1573};
1574
1575static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1576{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001577 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001578
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001579 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001580 return 0;
1581
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001582 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001583}
1584
1585static ssize_t target_core_store_dev_udev_path(
1586 void *p,
1587 const char *page,
1588 size_t count)
1589{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001590 struct se_device *dev = p;
1591 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001592 ssize_t read_bytes;
1593
1594 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001595 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001596 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1597 SE_UDEV_PATH_LEN-1);
1598 return -EINVAL;
1599 }
1600
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001601 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001602 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001603 if (!read_bytes)
1604 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001605 if (dev->udev_path[read_bytes - 1] == '\n')
1606 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001607
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001608 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001609
Andy Grover6708bb22011-06-08 10:36:43 -07001610 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001611 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001612 config_item_name(&dev->dev_group.cg_item),
1613 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001614
1615 return read_bytes;
1616}
1617
1618static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1619 .attr = { .ca_owner = THIS_MODULE,
1620 .ca_name = "udev_path",
1621 .ca_mode = S_IRUGO | S_IWUSR },
1622 .show = target_core_show_dev_udev_path,
1623 .store = target_core_store_dev_udev_path,
1624};
1625
Andy Grover64146db2013-04-30 11:59:15 -07001626static ssize_t target_core_show_dev_enable(void *p, char *page)
1627{
1628 struct se_device *dev = p;
1629
1630 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1631}
1632
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001633static ssize_t target_core_store_dev_enable(
1634 void *p,
1635 const char *page,
1636 size_t count)
1637{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001638 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001639 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001640 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001641
1642 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001643 if (!ptr) {
1644 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001645 " is \"1\"\n");
1646 return -EINVAL;
1647 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001648
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001649 ret = target_configure_device(dev);
1650 if (ret)
1651 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001652 return count;
1653}
1654
1655static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1656 .attr = { .ca_owner = THIS_MODULE,
1657 .ca_name = "enable",
Andy Grover64146db2013-04-30 11:59:15 -07001658 .ca_mode = S_IRUGO | S_IWUSR },
1659 .show = target_core_show_dev_enable,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001660 .store = target_core_store_dev_enable,
1661};
1662
1663static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1664{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001665 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001666 struct config_item *lu_ci;
1667 struct t10_alua_lu_gp *lu_gp;
1668 struct t10_alua_lu_gp_member *lu_gp_mem;
1669 ssize_t len = 0;
1670
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001671 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001672 if (!lu_gp_mem)
1673 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001674
1675 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1676 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001677 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001678 lu_ci = &lu_gp->lu_gp_group.cg_item;
1679 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1680 config_item_name(lu_ci), lu_gp->lu_gp_id);
1681 }
1682 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1683
1684 return len;
1685}
1686
1687static ssize_t target_core_store_alua_lu_gp(
1688 void *p,
1689 const char *page,
1690 size_t count)
1691{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001692 struct se_device *dev = p;
1693 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001694 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1695 struct t10_alua_lu_gp_member *lu_gp_mem;
1696 unsigned char buf[LU_GROUP_NAME_BUF];
1697 int move = 0;
1698
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001699 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1700 if (!lu_gp_mem)
1701 return 0;
1702
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001703 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001704 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001705 return -EINVAL;
1706 }
1707 memset(buf, 0, LU_GROUP_NAME_BUF);
1708 memcpy(buf, page, count);
1709 /*
1710 * Any ALUA logical unit alias besides "NULL" means we will be
1711 * making a new group association.
1712 */
1713 if (strcmp(strstrip(buf), "NULL")) {
1714 /*
1715 * core_alua_get_lu_gp_by_name() will increment reference to
1716 * struct t10_alua_lu_gp. This reference is released with
1717 * core_alua_get_lu_gp_by_name below().
1718 */
1719 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001720 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001721 return -ENODEV;
1722 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001723
1724 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1725 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001726 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001727 /*
1728 * Clearing an existing lu_gp association, and replacing
1729 * with NULL
1730 */
Andy Grover6708bb22011-06-08 10:36:43 -07001731 if (!lu_gp_new) {
1732 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001733 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1734 " %hu\n",
1735 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001736 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001737 config_item_name(&lu_gp->lu_gp_group.cg_item),
1738 lu_gp->lu_gp_id);
1739
1740 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1741 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1742
1743 return count;
1744 }
1745 /*
1746 * Removing existing association of lu_gp_mem with lu_gp
1747 */
1748 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1749 move = 1;
1750 }
1751 /*
1752 * Associate lu_gp_mem with lu_gp_new.
1753 */
1754 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1755 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1756
Andy Grover6708bb22011-06-08 10:36:43 -07001757 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001758 " core/alua/lu_gps/%s, ID: %hu\n",
1759 (move) ? "Moving" : "Adding",
1760 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001761 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001762 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1763 lu_gp_new->lu_gp_id);
1764
1765 core_alua_put_lu_gp_from_name(lu_gp_new);
1766 return count;
1767}
1768
1769static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1770 .attr = { .ca_owner = THIS_MODULE,
1771 .ca_name = "alua_lu_gp",
1772 .ca_mode = S_IRUGO | S_IWUSR },
1773 .show = target_core_show_alua_lu_gp,
1774 .store = target_core_store_alua_lu_gp,
1775};
1776
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001777static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1778{
1779 struct se_device *dev = p;
1780 struct t10_alua_lba_map *map;
1781 struct t10_alua_lba_map_member *mem;
1782 char *b = page;
1783 int bl = 0;
1784 char state;
1785
1786 spin_lock(&dev->t10_alua.lba_map_lock);
1787 if (!list_empty(&dev->t10_alua.lba_map_list))
1788 bl += sprintf(b + bl, "%u %u\n",
1789 dev->t10_alua.lba_map_segment_size,
1790 dev->t10_alua.lba_map_segment_multiplier);
1791 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1792 bl += sprintf(b + bl, "%llu %llu",
1793 map->lba_map_first_lba, map->lba_map_last_lba);
1794 list_for_each_entry(mem, &map->lba_map_mem_list,
1795 lba_map_mem_list) {
1796 switch (mem->lba_map_mem_alua_state) {
1797 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1798 state = 'O';
1799 break;
1800 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1801 state = 'A';
1802 break;
1803 case ALUA_ACCESS_STATE_STANDBY:
1804 state = 'S';
1805 break;
1806 case ALUA_ACCESS_STATE_UNAVAILABLE:
1807 state = 'U';
1808 break;
1809 default:
1810 state = '.';
1811 break;
1812 }
1813 bl += sprintf(b + bl, " %d:%c",
1814 mem->lba_map_mem_alua_pg_id, state);
1815 }
1816 bl += sprintf(b + bl, "\n");
1817 }
1818 spin_unlock(&dev->t10_alua.lba_map_lock);
1819 return bl;
1820}
1821
1822static ssize_t target_core_store_dev_lba_map(
1823 void *p,
1824 const char *page,
1825 size_t count)
1826{
1827 struct se_device *dev = p;
1828 struct t10_alua_lba_map *lba_map = NULL;
1829 struct list_head lba_list;
1830 char *map_entries, *ptr;
1831 char state;
1832 int pg_num = -1, pg;
1833 int ret = 0, num = 0, pg_id, alua_state;
1834 unsigned long start_lba = -1, end_lba = -1;
1835 unsigned long segment_size = -1, segment_mult = -1;
1836
1837 map_entries = kstrdup(page, GFP_KERNEL);
1838 if (!map_entries)
1839 return -ENOMEM;
1840
1841 INIT_LIST_HEAD(&lba_list);
1842 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1843 if (!*ptr)
1844 continue;
1845
1846 if (num == 0) {
1847 if (sscanf(ptr, "%lu %lu\n",
1848 &segment_size, &segment_mult) != 2) {
1849 pr_err("Invalid line %d\n", num);
1850 ret = -EINVAL;
1851 break;
1852 }
1853 num++;
1854 continue;
1855 }
1856 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1857 pr_err("Invalid line %d\n", num);
1858 ret = -EINVAL;
1859 break;
1860 }
1861 ptr = strchr(ptr, ' ');
1862 if (!ptr) {
1863 pr_err("Invalid line %d, missing end lba\n", num);
1864 ret = -EINVAL;
1865 break;
1866 }
1867 ptr++;
1868 ptr = strchr(ptr, ' ');
1869 if (!ptr) {
1870 pr_err("Invalid line %d, missing state definitions\n",
1871 num);
1872 ret = -EINVAL;
1873 break;
1874 }
1875 ptr++;
1876 lba_map = core_alua_allocate_lba_map(&lba_list,
1877 start_lba, end_lba);
1878 if (IS_ERR(lba_map)) {
1879 ret = PTR_ERR(lba_map);
1880 break;
1881 }
1882 pg = 0;
1883 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1884 switch (state) {
1885 case 'O':
1886 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1887 break;
1888 case 'A':
1889 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1890 break;
1891 case 'S':
1892 alua_state = ALUA_ACCESS_STATE_STANDBY;
1893 break;
1894 case 'U':
1895 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1896 break;
1897 default:
1898 pr_err("Invalid ALUA state '%c'\n", state);
1899 ret = -EINVAL;
1900 goto out;
1901 }
1902
1903 ret = core_alua_allocate_lba_map_mem(lba_map,
1904 pg_id, alua_state);
1905 if (ret) {
1906 pr_err("Invalid target descriptor %d:%c "
1907 "at line %d\n",
1908 pg_id, state, num);
1909 break;
1910 }
1911 pg++;
1912 ptr = strchr(ptr, ' ');
1913 if (ptr)
1914 ptr++;
1915 else
1916 break;
1917 }
1918 if (pg_num == -1)
1919 pg_num = pg;
1920 else if (pg != pg_num) {
1921 pr_err("Only %d from %d port groups definitions "
1922 "at line %d\n", pg, pg_num, num);
1923 ret = -EINVAL;
1924 break;
1925 }
1926 num++;
1927 }
1928out:
1929 if (ret) {
1930 core_alua_free_lba_map(&lba_list);
1931 count = ret;
1932 } else
1933 core_alua_set_lba_map(dev, &lba_list,
1934 segment_size, segment_mult);
1935 kfree(map_entries);
1936 return count;
1937}
1938
1939static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1940 .attr = { .ca_owner = THIS_MODULE,
1941 .ca_name = "lba_map",
1942 .ca_mode = S_IRUGO | S_IWUSR },
1943 .show = target_core_show_dev_lba_map,
1944 .store = target_core_store_dev_lba_map,
1945};
1946
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001947static struct configfs_attribute *target_core_dev_attrs[] = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001948 &target_core_attr_dev_info.attr,
1949 &target_core_attr_dev_control.attr,
1950 &target_core_attr_dev_alias.attr,
1951 &target_core_attr_dev_udev_path.attr,
1952 &target_core_attr_dev_enable.attr,
1953 &target_core_attr_dev_alua_lu_gp.attr,
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001954 &target_core_attr_dev_lba_map.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001955 NULL,
1956};
1957
1958static void target_core_dev_release(struct config_item *item)
1959{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001960 struct config_group *dev_cg = to_config_group(item);
1961 struct se_device *dev =
1962 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001963
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001964 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001965 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001966}
1967
1968static ssize_t target_core_dev_show(struct config_item *item,
1969 struct configfs_attribute *attr,
1970 char *page)
1971{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001972 struct config_group *dev_cg = to_config_group(item);
1973 struct se_device *dev =
1974 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001975 struct target_core_configfs_attribute *tc_attr = container_of(
1976 attr, struct target_core_configfs_attribute, attr);
1977
Andy Grover6708bb22011-06-08 10:36:43 -07001978 if (!tc_attr->show)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001979 return -EINVAL;
1980
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001981 return tc_attr->show(dev, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001982}
1983
1984static ssize_t target_core_dev_store(struct config_item *item,
1985 struct configfs_attribute *attr,
1986 const char *page, size_t count)
1987{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001988 struct config_group *dev_cg = to_config_group(item);
1989 struct se_device *dev =
1990 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001991 struct target_core_configfs_attribute *tc_attr = container_of(
1992 attr, struct target_core_configfs_attribute, attr);
1993
Andy Grover6708bb22011-06-08 10:36:43 -07001994 if (!tc_attr->store)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001995 return -EINVAL;
1996
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001997 return tc_attr->store(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001998}
1999
2000static struct configfs_item_operations target_core_dev_item_ops = {
2001 .release = target_core_dev_release,
2002 .show_attribute = target_core_dev_show,
2003 .store_attribute = target_core_dev_store,
2004};
2005
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002006TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002007
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002008/* End functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002009
2010/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2011
2012CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
2013#define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
2014static struct target_core_alua_lu_gp_attribute \
2015 target_core_alua_lu_gp_##_name = \
2016 __CONFIGFS_EATTR(_name, _mode, \
2017 target_core_alua_lu_gp_show_attr_##_name, \
2018 target_core_alua_lu_gp_store_attr_##_name);
2019
2020#define SE_DEV_ALUA_LU_ATTR_RO(_name) \
2021static struct target_core_alua_lu_gp_attribute \
2022 target_core_alua_lu_gp_##_name = \
2023 __CONFIGFS_EATTR_RO(_name, \
2024 target_core_alua_lu_gp_show_attr_##_name);
2025
2026/*
2027 * lu_gp_id
2028 */
2029static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2030 struct t10_alua_lu_gp *lu_gp,
2031 char *page)
2032{
Andy Grover6708bb22011-06-08 10:36:43 -07002033 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002034 return 0;
2035
2036 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2037}
2038
2039static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2040 struct t10_alua_lu_gp *lu_gp,
2041 const char *page,
2042 size_t count)
2043{
2044 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2045 unsigned long lu_gp_id;
2046 int ret;
2047
Jingoo Han57103d72013-07-19 16:22:19 +09002048 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002049 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002050 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002051 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002052 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002053 }
2054 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002055 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002056 " 0x0000ffff\n", lu_gp_id);
2057 return -EINVAL;
2058 }
2059
2060 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2061 if (ret < 0)
2062 return -EINVAL;
2063
Andy Grover6708bb22011-06-08 10:36:43 -07002064 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002065 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2066 config_item_name(&alua_lu_gp_cg->cg_item),
2067 lu_gp->lu_gp_id);
2068
2069 return count;
2070}
2071
2072SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2073
2074/*
2075 * members
2076 */
2077static ssize_t target_core_alua_lu_gp_show_attr_members(
2078 struct t10_alua_lu_gp *lu_gp,
2079 char *page)
2080{
2081 struct se_device *dev;
2082 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002083 struct t10_alua_lu_gp_member *lu_gp_mem;
2084 ssize_t len = 0, cur_len;
2085 unsigned char buf[LU_GROUP_NAME_BUF];
2086
2087 memset(buf, 0, LU_GROUP_NAME_BUF);
2088
2089 spin_lock(&lu_gp->lu_gp_lock);
2090 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2091 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002092 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002093
2094 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2095 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002096 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002097 cur_len++; /* Extra byte for NULL terminator */
2098
2099 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002100 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002101 "_members buffer\n");
2102 break;
2103 }
2104 memcpy(page+len, buf, cur_len);
2105 len += cur_len;
2106 }
2107 spin_unlock(&lu_gp->lu_gp_lock);
2108
2109 return len;
2110}
2111
2112SE_DEV_ALUA_LU_ATTR_RO(members);
2113
2114CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2115
2116static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2117 &target_core_alua_lu_gp_lu_gp_id.attr,
2118 &target_core_alua_lu_gp_members.attr,
2119 NULL,
2120};
2121
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002122static void target_core_alua_lu_gp_release(struct config_item *item)
2123{
2124 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2125 struct t10_alua_lu_gp, lu_gp_group);
2126
2127 core_alua_free_lu_gp(lu_gp);
2128}
2129
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002130static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002131 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002132 .show_attribute = target_core_alua_lu_gp_attr_show,
2133 .store_attribute = target_core_alua_lu_gp_attr_store,
2134};
2135
2136static struct config_item_type target_core_alua_lu_gp_cit = {
2137 .ct_item_ops = &target_core_alua_lu_gp_ops,
2138 .ct_attrs = target_core_alua_lu_gp_attrs,
2139 .ct_owner = THIS_MODULE,
2140};
2141
2142/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2143
2144/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2145
2146static struct config_group *target_core_alua_create_lu_gp(
2147 struct config_group *group,
2148 const char *name)
2149{
2150 struct t10_alua_lu_gp *lu_gp;
2151 struct config_group *alua_lu_gp_cg = NULL;
2152 struct config_item *alua_lu_gp_ci = NULL;
2153
2154 lu_gp = core_alua_allocate_lu_gp(name, 0);
2155 if (IS_ERR(lu_gp))
2156 return NULL;
2157
2158 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2159 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2160
2161 config_group_init_type_name(alua_lu_gp_cg, name,
2162 &target_core_alua_lu_gp_cit);
2163
Andy Grover6708bb22011-06-08 10:36:43 -07002164 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002165 " Group: core/alua/lu_gps/%s\n",
2166 config_item_name(alua_lu_gp_ci));
2167
2168 return alua_lu_gp_cg;
2169
2170}
2171
2172static void target_core_alua_drop_lu_gp(
2173 struct config_group *group,
2174 struct config_item *item)
2175{
2176 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2177 struct t10_alua_lu_gp, lu_gp_group);
2178
Andy Grover6708bb22011-06-08 10:36:43 -07002179 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002180 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2181 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002182 /*
2183 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2184 * -> target_core_alua_lu_gp_release()
2185 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002186 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002187}
2188
2189static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2190 .make_group = &target_core_alua_create_lu_gp,
2191 .drop_item = &target_core_alua_drop_lu_gp,
2192};
2193
2194static struct config_item_type target_core_alua_lu_gps_cit = {
2195 .ct_item_ops = NULL,
2196 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2197 .ct_owner = THIS_MODULE,
2198};
2199
2200/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2201
2202/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2203
2204CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2205#define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
2206static struct target_core_alua_tg_pt_gp_attribute \
2207 target_core_alua_tg_pt_gp_##_name = \
2208 __CONFIGFS_EATTR(_name, _mode, \
2209 target_core_alua_tg_pt_gp_show_attr_##_name, \
2210 target_core_alua_tg_pt_gp_store_attr_##_name);
2211
2212#define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
2213static struct target_core_alua_tg_pt_gp_attribute \
2214 target_core_alua_tg_pt_gp_##_name = \
2215 __CONFIGFS_EATTR_RO(_name, \
2216 target_core_alua_tg_pt_gp_show_attr_##_name);
2217
2218/*
2219 * alua_access_state
2220 */
2221static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2222 struct t10_alua_tg_pt_gp *tg_pt_gp,
2223 char *page)
2224{
2225 return sprintf(page, "%d\n",
2226 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2227}
2228
2229static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2230 struct t10_alua_tg_pt_gp *tg_pt_gp,
2231 const char *page,
2232 size_t count)
2233{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002234 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002235 unsigned long tmp;
2236 int new_state, ret;
2237
Andy Grover6708bb22011-06-08 10:36:43 -07002238 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002239 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002240 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2241 return -EINVAL;
2242 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07002243 if (!(dev->dev_flags & DF_CONFIGURED)) {
2244 pr_err("Unable to set alua_access_state while device is"
2245 " not configured\n");
2246 return -ENODEV;
2247 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002248
Jingoo Han57103d72013-07-19 16:22:19 +09002249 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002250 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002251 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002252 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002253 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002254 }
2255 new_state = (int)tmp;
2256
Hannes Reinecke125d0112013-11-19 09:07:46 +01002257 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2258 pr_err("Unable to process implicit configfs ALUA"
2259 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002260 return -EINVAL;
2261 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002262 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2263 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2264 /* LBA DEPENDENT is only allowed with implicit ALUA */
2265 pr_err("Unable to process implicit configfs ALUA transition"
2266 " while explicit ALUA management is enabled\n");
2267 return -EINVAL;
2268 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002269
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002270 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002271 NULL, NULL, new_state, 0);
2272 return (!ret) ? count : -EINVAL;
2273}
2274
2275SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2276
2277/*
2278 * alua_access_status
2279 */
2280static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2281 struct t10_alua_tg_pt_gp *tg_pt_gp,
2282 char *page)
2283{
2284 return sprintf(page, "%s\n",
2285 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2286}
2287
2288static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2289 struct t10_alua_tg_pt_gp *tg_pt_gp,
2290 const char *page,
2291 size_t count)
2292{
2293 unsigned long tmp;
2294 int new_status, ret;
2295
Andy Grover6708bb22011-06-08 10:36:43 -07002296 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2297 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002298 " valid tg_pt_gp ID: %hu\n",
2299 tg_pt_gp->tg_pt_gp_valid_id);
2300 return -EINVAL;
2301 }
2302
Jingoo Han57103d72013-07-19 16:22:19 +09002303 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002304 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002305 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002306 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002307 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002308 }
2309 new_status = (int)tmp;
2310
2311 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002312 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2313 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002314 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002315 new_status);
2316 return -EINVAL;
2317 }
2318
2319 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2320 return count;
2321}
2322
2323SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2324
2325/*
2326 * alua_access_type
2327 */
2328static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2329 struct t10_alua_tg_pt_gp *tg_pt_gp,
2330 char *page)
2331{
2332 return core_alua_show_access_type(tg_pt_gp, page);
2333}
2334
2335static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2336 struct t10_alua_tg_pt_gp *tg_pt_gp,
2337 const char *page,
2338 size_t count)
2339{
2340 return core_alua_store_access_type(tg_pt_gp, page, count);
2341}
2342
2343SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2344
2345/*
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002346 * alua_supported_states
2347 */
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002348
2349#define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit) \
2350static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2351 struct t10_alua_tg_pt_gp *t, char *p) \
2352{ \
2353 return sprintf(p, "%d\n", !!(t->_var & _bit)); \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002354}
2355
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002356#define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit) \
2357static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2358 struct t10_alua_tg_pt_gp *t, const char *p, size_t c) \
2359{ \
2360 unsigned long tmp; \
2361 int ret; \
2362 \
2363 if (!t->tg_pt_gp_valid_id) { \
2364 pr_err("Unable to do set ##_name ALUA state on non" \
2365 " valid tg_pt_gp ID: %hu\n", \
2366 t->tg_pt_gp_valid_id); \
2367 return -EINVAL; \
2368 } \
2369 \
2370 ret = kstrtoul(p, 0, &tmp); \
2371 if (ret < 0) { \
2372 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2373 return -EINVAL; \
2374 } \
2375 if (tmp > 1) { \
2376 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2377 return -EINVAL; \
2378 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002379 if (tmp) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002380 t->_var |= _bit; \
2381 else \
2382 t->_var &= ~_bit; \
2383 \
2384 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002385}
2386
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002387SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2388 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2389SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2390 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2391SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
2392
2393SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2394 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2395SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2396 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2397SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
2398
2399SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2400 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2401SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2402 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
Hannes Reineckec66094b2013-12-17 09:18:49 +01002403SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002404
2405SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2406 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2407SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2408 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2409SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
2410
2411SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2412 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2413SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2414 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2415SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2416
2417SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2418 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2419SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2420 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2421SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2422
2423SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2424 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2425SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2426 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2427SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002428
2429/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002430 * alua_write_metadata
2431 */
2432static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2433 struct t10_alua_tg_pt_gp *tg_pt_gp,
2434 char *page)
2435{
2436 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2437}
2438
2439static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2440 struct t10_alua_tg_pt_gp *tg_pt_gp,
2441 const char *page,
2442 size_t count)
2443{
2444 unsigned long tmp;
2445 int ret;
2446
Jingoo Han57103d72013-07-19 16:22:19 +09002447 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002448 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002449 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002450 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002451 }
2452
2453 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002454 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002455 " %lu\n", tmp);
2456 return -EINVAL;
2457 }
2458 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2459
2460 return count;
2461}
2462
2463SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2464
2465
2466
2467/*
2468 * nonop_delay_msecs
2469 */
2470static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2471 struct t10_alua_tg_pt_gp *tg_pt_gp,
2472 char *page)
2473{
2474 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2475
2476}
2477
2478static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2479 struct t10_alua_tg_pt_gp *tg_pt_gp,
2480 const char *page,
2481 size_t count)
2482{
2483 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2484}
2485
2486SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2487
2488/*
2489 * trans_delay_msecs
2490 */
2491static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2492 struct t10_alua_tg_pt_gp *tg_pt_gp,
2493 char *page)
2494{
2495 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2496}
2497
2498static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2499 struct t10_alua_tg_pt_gp *tg_pt_gp,
2500 const char *page,
2501 size_t count)
2502{
2503 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2504}
2505
2506SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2507
2508/*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002509 * implicit_trans_secs
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002510 */
Hannes Reinecke125d0112013-11-19 09:07:46 +01002511static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002512 struct t10_alua_tg_pt_gp *tg_pt_gp,
2513 char *page)
2514{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002515 return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002516}
2517
Hannes Reinecke125d0112013-11-19 09:07:46 +01002518static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002519 struct t10_alua_tg_pt_gp *tg_pt_gp,
2520 const char *page,
2521 size_t count)
2522{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002523 return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002524}
2525
Hannes Reinecke125d0112013-11-19 09:07:46 +01002526SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002527
2528/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002529 * preferred
2530 */
2531
2532static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2533 struct t10_alua_tg_pt_gp *tg_pt_gp,
2534 char *page)
2535{
2536 return core_alua_show_preferred_bit(tg_pt_gp, page);
2537}
2538
2539static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2540 struct t10_alua_tg_pt_gp *tg_pt_gp,
2541 const char *page,
2542 size_t count)
2543{
2544 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2545}
2546
2547SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2548
2549/*
2550 * tg_pt_gp_id
2551 */
2552static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2553 struct t10_alua_tg_pt_gp *tg_pt_gp,
2554 char *page)
2555{
Andy Grover6708bb22011-06-08 10:36:43 -07002556 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002557 return 0;
2558
2559 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2560}
2561
2562static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2563 struct t10_alua_tg_pt_gp *tg_pt_gp,
2564 const char *page,
2565 size_t count)
2566{
2567 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2568 unsigned long tg_pt_gp_id;
2569 int ret;
2570
Jingoo Han57103d72013-07-19 16:22:19 +09002571 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002572 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002573 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002574 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002575 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002576 }
2577 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002578 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002579 " 0x0000ffff\n", tg_pt_gp_id);
2580 return -EINVAL;
2581 }
2582
2583 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2584 if (ret < 0)
2585 return -EINVAL;
2586
Andy Grover6708bb22011-06-08 10:36:43 -07002587 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002588 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2589 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2590 tg_pt_gp->tg_pt_gp_id);
2591
2592 return count;
2593}
2594
2595SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2596
2597/*
2598 * members
2599 */
2600static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2601 struct t10_alua_tg_pt_gp *tg_pt_gp,
2602 char *page)
2603{
2604 struct se_port *port;
2605 struct se_portal_group *tpg;
2606 struct se_lun *lun;
2607 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2608 ssize_t len = 0, cur_len;
2609 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2610
2611 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2612
2613 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2614 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2615 tg_pt_gp_mem_list) {
2616 port = tg_pt_gp_mem->tg_pt;
2617 tpg = port->sep_tpg;
2618 lun = port->sep_lun;
2619
2620 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002621 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2622 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2623 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002624 config_item_name(&lun->lun_group.cg_item));
2625 cur_len++; /* Extra byte for NULL terminator */
2626
2627 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002628 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002629 "_members buffer\n");
2630 break;
2631 }
2632 memcpy(page+len, buf, cur_len);
2633 len += cur_len;
2634 }
2635 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2636
2637 return len;
2638}
2639
2640SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2641
2642CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2643 tg_pt_gp_group);
2644
2645static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2646 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2647 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2648 &target_core_alua_tg_pt_gp_alua_access_type.attr,
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002649 &target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2650 &target_core_alua_tg_pt_gp_alua_support_offline.attr,
2651 &target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2652 &target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2653 &target_core_alua_tg_pt_gp_alua_support_standby.attr,
2654 &target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2655 &target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002656 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2657 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2658 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
Hannes Reinecke125d0112013-11-19 09:07:46 +01002659 &target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002660 &target_core_alua_tg_pt_gp_preferred.attr,
2661 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2662 &target_core_alua_tg_pt_gp_members.attr,
2663 NULL,
2664};
2665
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002666static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2667{
2668 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2669 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2670
2671 core_alua_free_tg_pt_gp(tg_pt_gp);
2672}
2673
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002674static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002675 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002676 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2677 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2678};
2679
2680static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2681 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2682 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2683 .ct_owner = THIS_MODULE,
2684};
2685
2686/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2687
2688/* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2689
2690static struct config_group *target_core_alua_create_tg_pt_gp(
2691 struct config_group *group,
2692 const char *name)
2693{
2694 struct t10_alua *alua = container_of(group, struct t10_alua,
2695 alua_tg_pt_gps_group);
2696 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002697 struct config_group *alua_tg_pt_gp_cg = NULL;
2698 struct config_item *alua_tg_pt_gp_ci = NULL;
2699
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002700 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002701 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002702 return NULL;
2703
2704 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2705 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2706
2707 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2708 &target_core_alua_tg_pt_gp_cit);
2709
Andy Grover6708bb22011-06-08 10:36:43 -07002710 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002711 " Group: alua/tg_pt_gps/%s\n",
2712 config_item_name(alua_tg_pt_gp_ci));
2713
2714 return alua_tg_pt_gp_cg;
2715}
2716
2717static void target_core_alua_drop_tg_pt_gp(
2718 struct config_group *group,
2719 struct config_item *item)
2720{
2721 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2722 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2723
Andy Grover6708bb22011-06-08 10:36:43 -07002724 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002725 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2726 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002727 /*
2728 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2729 * -> target_core_alua_tg_pt_gp_release().
2730 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002731 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002732}
2733
2734static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2735 .make_group = &target_core_alua_create_tg_pt_gp,
2736 .drop_item = &target_core_alua_drop_tg_pt_gp,
2737};
2738
2739static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2740 .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
2741 .ct_owner = THIS_MODULE,
2742};
2743
2744/* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2745
2746/* Start functions for struct config_item_type target_core_alua_cit */
2747
2748/*
2749 * target_core_alua_cit is a ConfigFS group that lives under
2750 * /sys/kernel/config/target/core/alua. There are default groups
2751 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2752 * target_core_alua_cit in target_core_init_configfs() below.
2753 */
2754static struct config_item_type target_core_alua_cit = {
2755 .ct_item_ops = NULL,
2756 .ct_attrs = NULL,
2757 .ct_owner = THIS_MODULE,
2758};
2759
2760/* End functions for struct config_item_type target_core_alua_cit */
2761
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002762/* Start functions for struct config_item_type target_core_stat_cit */
2763
2764static struct config_group *target_core_stat_mkdir(
2765 struct config_group *group,
2766 const char *name)
2767{
2768 return ERR_PTR(-ENOSYS);
2769}
2770
2771static void target_core_stat_rmdir(
2772 struct config_group *group,
2773 struct config_item *item)
2774{
2775 return;
2776}
2777
2778static struct configfs_group_operations target_core_stat_group_ops = {
2779 .make_group = &target_core_stat_mkdir,
2780 .drop_item = &target_core_stat_rmdir,
2781};
2782
2783static struct config_item_type target_core_stat_cit = {
2784 .ct_group_ops = &target_core_stat_group_ops,
2785 .ct_owner = THIS_MODULE,
2786};
2787
2788/* End functions for struct config_item_type target_core_stat_cit */
2789
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002790/* Start functions for struct config_item_type target_core_hba_cit */
2791
2792static struct config_group *target_core_make_subdev(
2793 struct config_group *group,
2794 const char *name)
2795{
2796 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002797 struct se_subsystem_api *t;
2798 struct config_item *hba_ci = &group->cg_item;
2799 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002800 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002801 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002802 struct config_group *dev_stat_grp = NULL;
2803 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002804
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002805 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2806 if (ret)
2807 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002808 /*
2809 * Locate the struct se_subsystem_api from parent's struct se_hba.
2810 */
2811 t = hba->transport;
2812
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002813 dev = target_alloc_device(hba, name);
2814 if (!dev)
2815 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002816
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002817 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002818
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002819 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002820 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002821 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002822 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002823
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002824 config_group_init_type_name(dev_cg, name, &t->tb_cits.tb_dev_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002825 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08002826 &t->tb_cits.tb_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002827 config_group_init_type_name(&dev->dev_pr_group, "pr",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002828 &target_core_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002829 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002830 &target_core_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002831 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002832 "alua", &target_core_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002833 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002834 "statistics", &target_core_stat_cit);
2835
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002836 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2837 dev_cg->default_groups[1] = &dev->dev_pr_group;
2838 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2839 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2840 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002841 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002842 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002843 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002844 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002845 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002846 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002847 goto out_free_dev_cg_default_groups;
2848 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002849
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002850 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002851 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002852 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002853 if (!tg_pt_gp_cg->default_groups) {
2854 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002855 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002856 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002857 }
2858
2859 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2860 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2861 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2862 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002863 /*
2864 * Add core/$HBA/$DEV/statistics/ default groups
2865 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002866 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002867 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002868 GFP_KERNEL);
2869 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002870 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002871 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002872 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002873 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002874
2875 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002876 return dev_cg;
2877
2878out_free_tg_pt_gp_cg_default_groups:
2879 kfree(tg_pt_gp_cg->default_groups);
2880out_free_tg_pt_gp:
2881 core_alua_free_tg_pt_gp(tg_pt_gp);
2882out_free_dev_cg_default_groups:
2883 kfree(dev_cg->default_groups);
2884out_free_device:
2885 target_free_device(dev);
2886out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002887 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002888 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002889}
2890
2891static void target_core_drop_subdev(
2892 struct config_group *group,
2893 struct config_item *item)
2894{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002895 struct config_group *dev_cg = to_config_group(item);
2896 struct se_device *dev =
2897 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002898 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002899 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002900 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002901 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002902
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002903 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002904
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002905 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002906
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002907 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002908 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2909 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2910 dev_stat_grp->default_groups[i] = NULL;
2911 config_item_put(df_item);
2912 }
2913 kfree(dev_stat_grp->default_groups);
2914
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002915 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002916 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2917 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2918 tg_pt_gp_cg->default_groups[i] = NULL;
2919 config_item_put(df_item);
2920 }
2921 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002922 /*
2923 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2924 * directly from target_core_alua_tg_pt_gp_release().
2925 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002926 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002927
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002928 for (i = 0; dev_cg->default_groups[i]; i++) {
2929 df_item = &dev_cg->default_groups[i]->cg_item;
2930 dev_cg->default_groups[i] = NULL;
2931 config_item_put(df_item);
2932 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002933 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002934 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002935 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002936 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002937 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002938}
2939
2940static struct configfs_group_operations target_core_hba_group_ops = {
2941 .make_group = target_core_make_subdev,
2942 .drop_item = target_core_drop_subdev,
2943};
2944
2945CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2946#define SE_HBA_ATTR(_name, _mode) \
2947static struct target_core_hba_attribute \
2948 target_core_hba_##_name = \
2949 __CONFIGFS_EATTR(_name, _mode, \
2950 target_core_hba_show_attr_##_name, \
2951 target_core_hba_store_attr_##_name);
2952
2953#define SE_HBA_ATTR_RO(_name) \
2954static struct target_core_hba_attribute \
2955 target_core_hba_##_name = \
2956 __CONFIGFS_EATTR_RO(_name, \
2957 target_core_hba_show_attr_##_name);
2958
2959static ssize_t target_core_hba_show_attr_hba_info(
2960 struct se_hba *hba,
2961 char *page)
2962{
2963 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2964 hba->hba_id, hba->transport->name,
2965 TARGET_CORE_CONFIGFS_VERSION);
2966}
2967
2968SE_HBA_ATTR_RO(hba_info);
2969
2970static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2971 char *page)
2972{
2973 int hba_mode = 0;
2974
2975 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2976 hba_mode = 1;
2977
2978 return sprintf(page, "%d\n", hba_mode);
2979}
2980
2981static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2982 const char *page, size_t count)
2983{
2984 struct se_subsystem_api *transport = hba->transport;
2985 unsigned long mode_flag;
2986 int ret;
2987
2988 if (transport->pmode_enable_hba == NULL)
2989 return -EINVAL;
2990
Jingoo Han57103d72013-07-19 16:22:19 +09002991 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002992 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002993 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002994 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002995 }
2996
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002997 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07002998 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002999 return -EINVAL;
3000 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003001
3002 ret = transport->pmode_enable_hba(hba, mode_flag);
3003 if (ret < 0)
3004 return -EINVAL;
3005 if (ret > 0)
3006 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3007 else if (ret == 0)
3008 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3009
3010 return count;
3011}
3012
3013SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
3014
3015CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
3016
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003017static void target_core_hba_release(struct config_item *item)
3018{
3019 struct se_hba *hba = container_of(to_config_group(item),
3020 struct se_hba, hba_group);
3021 core_delete_hba(hba);
3022}
3023
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003024static struct configfs_attribute *target_core_hba_attrs[] = {
3025 &target_core_hba_hba_info.attr,
3026 &target_core_hba_hba_mode.attr,
3027 NULL,
3028};
3029
3030static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003031 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003032 .show_attribute = target_core_hba_attr_show,
3033 .store_attribute = target_core_hba_attr_store,
3034};
3035
3036static struct config_item_type target_core_hba_cit = {
3037 .ct_item_ops = &target_core_hba_item_ops,
3038 .ct_group_ops = &target_core_hba_group_ops,
3039 .ct_attrs = target_core_hba_attrs,
3040 .ct_owner = THIS_MODULE,
3041};
3042
3043static struct config_group *target_core_call_addhbatotarget(
3044 struct config_group *group,
3045 const char *name)
3046{
3047 char *se_plugin_str, *str, *str2;
3048 struct se_hba *hba;
3049 char buf[TARGET_CORE_NAME_MAX_LEN];
3050 unsigned long plugin_dep_id = 0;
3051 int ret;
3052
3053 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07003054 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07003055 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003056 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3057 TARGET_CORE_NAME_MAX_LEN);
3058 return ERR_PTR(-ENAMETOOLONG);
3059 }
3060 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3061
3062 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003063 if (!str) {
3064 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003065 return ERR_PTR(-EINVAL);
3066 }
3067 se_plugin_str = buf;
3068 /*
3069 * Special case for subsystem plugins that have "_" in their names.
3070 * Namely rd_direct and rd_mcp..
3071 */
3072 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003073 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003074 *str2 = '\0'; /* Terminate for *se_plugin_str */
3075 str2++; /* Skip to start of plugin dependent ID */
3076 str = str2;
3077 } else {
3078 *str = '\0'; /* Terminate for *se_plugin_str */
3079 str++; /* Skip to start of plugin dependent ID */
3080 }
3081
Jingoo Han57103d72013-07-19 16:22:19 +09003082 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003083 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09003084 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003085 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003086 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003087 }
3088 /*
3089 * Load up TCM subsystem plugins if they have not already been loaded.
3090 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07003091 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003092
3093 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3094 if (IS_ERR(hba))
3095 return ERR_CAST(hba);
3096
3097 config_group_init_type_name(&hba->hba_group, name,
3098 &target_core_hba_cit);
3099
3100 return &hba->hba_group;
3101}
3102
3103static void target_core_call_delhbafromtarget(
3104 struct config_group *group,
3105 struct config_item *item)
3106{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003107 /*
3108 * core_delete_hba() is called from target_core_hba_item_ops->release()
3109 * -> target_core_hba_release()
3110 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003111 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003112}
3113
3114static struct configfs_group_operations target_core_group_ops = {
3115 .make_group = target_core_call_addhbatotarget,
3116 .drop_item = target_core_call_delhbafromtarget,
3117};
3118
3119static struct config_item_type target_core_cit = {
3120 .ct_item_ops = NULL,
3121 .ct_group_ops = &target_core_group_ops,
3122 .ct_attrs = NULL,
3123 .ct_owner = THIS_MODULE,
3124};
3125
3126/* Stop functions for struct config_item_type target_core_hba_cit */
3127
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003128void target_core_setup_sub_cits(struct se_subsystem_api *sa)
3129{
3130 target_core_setup_dev_cit(sa);
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08003131 target_core_setup_dev_attrib_cit(sa);
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003132}
3133EXPORT_SYMBOL(target_core_setup_sub_cits);
3134
Axel Lin54550fa2011-03-14 04:06:09 -07003135static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003136{
3137 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3138 struct config_group *lu_gp_cg = NULL;
3139 struct configfs_subsystem *subsys;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003140 struct t10_alua_lu_gp *lu_gp;
3141 int ret;
3142
Andy Grover6708bb22011-06-08 10:36:43 -07003143 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003144 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3145 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3146
3147 subsys = target_core_subsystem[0];
3148 config_group_init(&subsys->su_group);
3149 mutex_init(&subsys->su_mutex);
3150
Andy Grovere3d6f902011-07-19 08:55:10 +00003151 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003152 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003153 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003154 /*
3155 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3156 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3157 */
3158 target_cg = &subsys->su_group;
Andy Groverab6dae82013-12-09 14:27:36 -08003159 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003160 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003161 if (!target_cg->default_groups) {
3162 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003163 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003164 goto out_global;
3165 }
3166
Andy Grovere3d6f902011-07-19 08:55:10 +00003167 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003168 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003169 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003170 target_cg->default_groups[1] = NULL;
3171 /*
3172 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3173 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003174 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003175 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003176 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003177 if (!hba_cg->default_groups) {
3178 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003179 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003180 goto out_global;
3181 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003182 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003183 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003184 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003185 hba_cg->default_groups[1] = NULL;
3186 /*
3187 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3188 * groups under /sys/kernel/config/target/core/alua/
3189 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003190 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003191 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003192 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003193 if (!alua_cg->default_groups) {
3194 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003195 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003196 goto out_global;
3197 }
3198
Andy Grovere3d6f902011-07-19 08:55:10 +00003199 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003200 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003201 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003202 alua_cg->default_groups[1] = NULL;
3203 /*
3204 * Add core/alua/lu_gps/default_lu_gp
3205 */
3206 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003207 if (IS_ERR(lu_gp)) {
3208 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003209 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003210 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003211
Andy Grovere3d6f902011-07-19 08:55:10 +00003212 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003213 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003214 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003215 if (!lu_gp_cg->default_groups) {
3216 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003217 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003218 goto out_global;
3219 }
3220
3221 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3222 &target_core_alua_lu_gp_cit);
3223 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3224 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003225 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003226 /*
3227 * Register the target_core_mod subsystem with configfs.
3228 */
3229 ret = configfs_register_subsystem(subsys);
3230 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003231 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003232 ret, subsys->su_group.cg_item.ci_namebuf);
3233 goto out_global;
3234 }
Andy Grover6708bb22011-06-08 10:36:43 -07003235 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003236 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3237 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3238 /*
3239 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3240 */
3241 ret = rd_module_init();
3242 if (ret < 0)
3243 goto out;
3244
Roland Dreier0d0f9df2012-10-31 09:16:44 -07003245 ret = core_dev_setup_virtual_lun0();
3246 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003247 goto out;
3248
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003249 ret = target_xcopy_setup_pt();
3250 if (ret < 0)
3251 goto out;
3252
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003253 return 0;
3254
3255out:
3256 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003257 core_dev_release_virtual_lun0();
3258 rd_module_exit();
3259out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00003260 if (default_lu_gp) {
3261 core_alua_free_lu_gp(default_lu_gp);
3262 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003263 }
3264 if (lu_gp_cg)
3265 kfree(lu_gp_cg->default_groups);
3266 if (alua_cg)
3267 kfree(alua_cg->default_groups);
3268 if (hba_cg)
3269 kfree(hba_cg->default_groups);
3270 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00003271 release_se_kmem_caches();
3272 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003273}
3274
Axel Lin54550fa2011-03-14 04:06:09 -07003275static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003276{
3277 struct configfs_subsystem *subsys;
3278 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3279 struct config_item *item;
3280 int i;
3281
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003282 subsys = target_core_subsystem[0];
3283
Andy Grovere3d6f902011-07-19 08:55:10 +00003284 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003285 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3286 item = &lu_gp_cg->default_groups[i]->cg_item;
3287 lu_gp_cg->default_groups[i] = NULL;
3288 config_item_put(item);
3289 }
3290 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003291 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003292
Andy Grovere3d6f902011-07-19 08:55:10 +00003293 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003294 for (i = 0; alua_cg->default_groups[i]; i++) {
3295 item = &alua_cg->default_groups[i]->cg_item;
3296 alua_cg->default_groups[i] = NULL;
3297 config_item_put(item);
3298 }
3299 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003300 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003301
Andy Grovere3d6f902011-07-19 08:55:10 +00003302 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003303 for (i = 0; hba_cg->default_groups[i]; i++) {
3304 item = &hba_cg->default_groups[i]->cg_item;
3305 hba_cg->default_groups[i] = NULL;
3306 config_item_put(item);
3307 }
3308 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003309 hba_cg->default_groups = NULL;
3310 /*
3311 * We expect subsys->su_group.default_groups to be released
3312 * by configfs subsystem provider logic..
3313 */
3314 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003315 kfree(subsys->su_group.default_groups);
3316
Andy Grovere3d6f902011-07-19 08:55:10 +00003317 core_alua_free_lu_gp(default_lu_gp);
3318 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003319
Andy Grover6708bb22011-06-08 10:36:43 -07003320 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003321 " Infrastructure\n");
3322
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003323 core_dev_release_virtual_lun0();
3324 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003325 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003326 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003327}
3328
3329MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3330MODULE_AUTHOR("nab@Linux-iSCSI.org");
3331MODULE_LICENSE("GPL");
3332
3333module_init(target_core_init_configfs);
3334module_exit(target_core_exit_configfs);