blob: 291dc711fbc3f6457a8a216ef2b91f7551858dae [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
Andy Grovere3d6f902011-07-19 08:55:10 +000053extern struct t10_alua_lu_gp *default_lu_gp;
54
Roland Dreierd0f474e2012-01-12 10:41:18 -080055static LIST_HEAD(g_tf_list);
56static DEFINE_MUTEX(g_tf_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080057
58struct target_core_configfs_attribute {
59 struct configfs_attribute attr;
60 ssize_t (*show)(void *, char *);
61 ssize_t (*store)(void *, const char *, size_t);
62};
63
Andy Grovere3d6f902011-07-19 08:55:10 +000064static struct config_group target_core_hbagroup;
65static struct config_group alua_group;
66static struct config_group alua_lu_gps_group;
67
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080068static inline struct se_hba *
69item_to_hba(struct config_item *item)
70{
71 return container_of(to_config_group(item), struct se_hba, hba_group);
72}
73
74/*
75 * Attributes for /sys/kernel/config/target/
76 */
77static ssize_t target_core_attr_show(struct config_item *item,
78 struct configfs_attribute *attr,
79 char *page)
80{
81 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
82 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
83 utsname()->sysname, utsname()->machine);
84}
85
86static struct configfs_item_operations target_core_fabric_item_ops = {
87 .show_attribute = target_core_attr_show,
88};
89
90static struct configfs_attribute target_core_item_attr_version = {
91 .ca_owner = THIS_MODULE,
92 .ca_name = "version",
93 .ca_mode = S_IRUGO,
94};
95
96static struct target_fabric_configfs *target_core_get_fabric(
97 const char *name)
98{
99 struct target_fabric_configfs *tf;
100
Andy Grover6708bb22011-06-08 10:36:43 -0700101 if (!name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800102 return NULL;
103
104 mutex_lock(&g_tf_lock);
105 list_for_each_entry(tf, &g_tf_list, tf_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700106 if (!strcmp(tf->tf_name, name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800107 atomic_inc(&tf->tf_access_cnt);
108 mutex_unlock(&g_tf_lock);
109 return tf;
110 }
111 }
112 mutex_unlock(&g_tf_lock);
113
114 return NULL;
115}
116
117/*
118 * Called from struct target_core_group_ops->make_group()
119 */
120static struct config_group *target_core_register_fabric(
121 struct config_group *group,
122 const char *name)
123{
124 struct target_fabric_configfs *tf;
125 int ret;
126
Andy Grover6708bb22011-06-08 10:36:43 -0700127 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800128 " %s\n", group, name);
129 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800130 * Below are some hardcoded request_module() calls to automatically
131 * local fabric modules when the following is called:
132 *
133 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
134 *
135 * Note that this does not limit which TCM fabric module can be
136 * registered, but simply provids auto loading logic for modules with
137 * mkdir(2) system calls with known TCM fabric modules.
138 */
Andy Grover6708bb22011-06-08 10:36:43 -0700139 if (!strncmp(name, "iscsi", 5)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800140 /*
141 * Automatically load the LIO Target fabric module when the
142 * following is called:
143 *
144 * mkdir -p $CONFIGFS/target/iscsi
145 */
146 ret = request_module("iscsi_target_mod");
147 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700148 pr_err("request_module() failed for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800149 " iscsi_target_mod.ko: %d\n", ret);
150 return ERR_PTR(-EINVAL);
151 }
Andy Grover6708bb22011-06-08 10:36:43 -0700152 } else if (!strncmp(name, "loopback", 8)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800153 /*
154 * Automatically load the tcm_loop fabric module when the
155 * following is called:
156 *
157 * mkdir -p $CONFIGFS/target/loopback
158 */
159 ret = request_module("tcm_loop");
160 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700161 pr_err("request_module() failed for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800162 " tcm_loop.ko: %d\n", ret);
163 return ERR_PTR(-EINVAL);
164 }
165 }
166
167 tf = target_core_get_fabric(name);
Andy Grover6708bb22011-06-08 10:36:43 -0700168 if (!tf) {
169 pr_err("target_core_get_fabric() failed for %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800170 name);
171 return ERR_PTR(-EINVAL);
172 }
Andy Grover6708bb22011-06-08 10:36:43 -0700173 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800174 " %s\n", tf->tf_name);
175 /*
176 * On a successful target_core_get_fabric() look, the returned
177 * struct target_fabric_configfs *tf will contain a usage reference.
178 */
Andy Grover6708bb22011-06-08 10:36:43 -0700179 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
Andy Groverd80e224d2013-10-09 11:05:56 -0700180 &tf->tf_cit_tmpl.tfc_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800181
182 tf->tf_group.default_groups = tf->tf_default_groups;
183 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
184 tf->tf_group.default_groups[1] = NULL;
185
186 config_group_init_type_name(&tf->tf_group, name,
Andy Groverd80e224d2013-10-09 11:05:56 -0700187 &tf->tf_cit_tmpl.tfc_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800188 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
Andy Groverd80e224d2013-10-09 11:05:56 -0700189 &tf->tf_cit_tmpl.tfc_discovery_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800190
Andy Grover6708bb22011-06-08 10:36:43 -0700191 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800192 " %s\n", tf->tf_group.cg_item.ci_name);
193 /*
194 * Setup tf_ops.tf_subsys pointer for usage with configfs_depend_item()
195 */
196 tf->tf_ops.tf_subsys = tf->tf_subsys;
197 tf->tf_fabric = &tf->tf_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -0700198 pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800199 " for %s\n", name);
200
201 return &tf->tf_group;
202}
203
204/*
205 * Called from struct target_core_group_ops->drop_item()
206 */
207static void target_core_deregister_fabric(
208 struct config_group *group,
209 struct config_item *item)
210{
211 struct target_fabric_configfs *tf = container_of(
212 to_config_group(item), struct target_fabric_configfs, tf_group);
213 struct config_group *tf_group;
214 struct config_item *df_item;
215 int i;
216
Andy Grover6708bb22011-06-08 10:36:43 -0700217 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800218 " tf list\n", config_item_name(item));
219
Andy Grover6708bb22011-06-08 10:36:43 -0700220 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800221 " %s\n", tf->tf_name);
222 atomic_dec(&tf->tf_access_cnt);
223
Andy Grover6708bb22011-06-08 10:36:43 -0700224 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800225 " tf->tf_fabric for %s\n", tf->tf_name);
226 tf->tf_fabric = NULL;
227
Andy Grover6708bb22011-06-08 10:36:43 -0700228 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229 " %s\n", config_item_name(item));
230
231 tf_group = &tf->tf_group;
232 for (i = 0; tf_group->default_groups[i]; i++) {
233 df_item = &tf_group->default_groups[i]->cg_item;
234 tf_group->default_groups[i] = NULL;
235 config_item_put(df_item);
236 }
237 config_item_put(item);
238}
239
240static struct configfs_group_operations target_core_fabric_group_ops = {
241 .make_group = &target_core_register_fabric,
242 .drop_item = &target_core_deregister_fabric,
243};
244
245/*
246 * All item attributes appearing in /sys/kernel/target/ appear here.
247 */
248static struct configfs_attribute *target_core_fabric_item_attrs[] = {
249 &target_core_item_attr_version,
250 NULL,
251};
252
253/*
254 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
255 */
256static struct config_item_type target_core_fabrics_item = {
257 .ct_item_ops = &target_core_fabric_item_ops,
258 .ct_group_ops = &target_core_fabric_group_ops,
259 .ct_attrs = target_core_fabric_item_attrs,
260 .ct_owner = THIS_MODULE,
261};
262
263static struct configfs_subsystem target_core_fabrics = {
264 .su_group = {
265 .cg_item = {
266 .ci_namebuf = "target",
267 .ci_type = &target_core_fabrics_item,
268 },
269 },
270};
271
Nicholas Bellinger56d128f2013-08-22 11:32:31 -0700272struct configfs_subsystem *target_core_subsystem[] = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800273 &target_core_fabrics,
274 NULL,
275};
276
277/*##############################################################################
278// Start functions called by external Target Fabrics Modules
279//############################################################################*/
280
281/*
282 * First function called by fabric modules to:
283 *
284 * 1) Allocate a struct target_fabric_configfs and save the *fabric_cit pointer.
285 * 2) Add struct target_fabric_configfs to g_tf_list
286 * 3) Return struct target_fabric_configfs to fabric module to be passed
287 * into target_fabric_configfs_register().
288 */
289struct target_fabric_configfs *target_fabric_configfs_init(
290 struct module *fabric_mod,
291 const char *name)
292{
293 struct target_fabric_configfs *tf;
294
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800295 if (!(name)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700296 pr_err("Unable to locate passed fabric name\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000297 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800298 }
Dan Carpenter60d645a2011-06-15 10:03:05 -0700299 if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700300 pr_err("Passed name: %s exceeds TARGET_FABRIC"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800301 "_NAME_SIZE\n", name);
Andy Grovere3d6f902011-07-19 08:55:10 +0000302 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303 }
304
305 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700306 if (!tf)
Andy Grovere3d6f902011-07-19 08:55:10 +0000307 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800308
309 INIT_LIST_HEAD(&tf->tf_list);
310 atomic_set(&tf->tf_access_cnt, 0);
311 /*
312 * Setup the default generic struct config_item_type's (cits) in
313 * struct target_fabric_configfs->tf_cit_tmpl
314 */
315 tf->tf_module = fabric_mod;
316 target_fabric_setup_cits(tf);
317
318 tf->tf_subsys = target_core_subsystem[0];
319 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
320
321 mutex_lock(&g_tf_lock);
322 list_add_tail(&tf->tf_list, &g_tf_list);
323 mutex_unlock(&g_tf_lock);
324
Andy Grover6708bb22011-06-08 10:36:43 -0700325 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800326 ">>>>>>>>>>>>>>\n");
Andy Grover6708bb22011-06-08 10:36:43 -0700327 pr_debug("Initialized struct target_fabric_configfs: %p for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800328 " %s\n", tf, tf->tf_name);
329 return tf;
330}
331EXPORT_SYMBOL(target_fabric_configfs_init);
332
333/*
334 * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
335 */
336void target_fabric_configfs_free(
337 struct target_fabric_configfs *tf)
338{
339 mutex_lock(&g_tf_lock);
340 list_del(&tf->tf_list);
341 mutex_unlock(&g_tf_lock);
342
343 kfree(tf);
344}
345EXPORT_SYMBOL(target_fabric_configfs_free);
346
347/*
348 * Perform a sanity check of the passed tf->tf_ops before completing
349 * TCM fabric module registration.
350 */
351static int target_fabric_tf_ops_check(
352 struct target_fabric_configfs *tf)
353{
354 struct target_core_fabric_ops *tfo = &tf->tf_ops;
355
Andy Grover6708bb22011-06-08 10:36:43 -0700356 if (!tfo->get_fabric_name) {
357 pr_err("Missing tfo->get_fabric_name()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800358 return -EINVAL;
359 }
Andy Grover6708bb22011-06-08 10:36:43 -0700360 if (!tfo->get_fabric_proto_ident) {
361 pr_err("Missing tfo->get_fabric_proto_ident()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800362 return -EINVAL;
363 }
Andy Grover6708bb22011-06-08 10:36:43 -0700364 if (!tfo->tpg_get_wwn) {
365 pr_err("Missing tfo->tpg_get_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800366 return -EINVAL;
367 }
Andy Grover6708bb22011-06-08 10:36:43 -0700368 if (!tfo->tpg_get_tag) {
369 pr_err("Missing tfo->tpg_get_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800370 return -EINVAL;
371 }
Andy Grover6708bb22011-06-08 10:36:43 -0700372 if (!tfo->tpg_get_default_depth) {
373 pr_err("Missing tfo->tpg_get_default_depth()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800374 return -EINVAL;
375 }
Andy Grover6708bb22011-06-08 10:36:43 -0700376 if (!tfo->tpg_get_pr_transport_id) {
377 pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800378 return -EINVAL;
379 }
Andy Grover6708bb22011-06-08 10:36:43 -0700380 if (!tfo->tpg_get_pr_transport_id_len) {
381 pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800382 return -EINVAL;
383 }
Andy Grover6708bb22011-06-08 10:36:43 -0700384 if (!tfo->tpg_check_demo_mode) {
385 pr_err("Missing tfo->tpg_check_demo_mode()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800386 return -EINVAL;
387 }
Andy Grover6708bb22011-06-08 10:36:43 -0700388 if (!tfo->tpg_check_demo_mode_cache) {
389 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800390 return -EINVAL;
391 }
Andy Grover6708bb22011-06-08 10:36:43 -0700392 if (!tfo->tpg_check_demo_mode_write_protect) {
393 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800394 return -EINVAL;
395 }
Andy Grover6708bb22011-06-08 10:36:43 -0700396 if (!tfo->tpg_check_prod_mode_write_protect) {
397 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800398 return -EINVAL;
399 }
Andy Grover6708bb22011-06-08 10:36:43 -0700400 if (!tfo->tpg_alloc_fabric_acl) {
401 pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 return -EINVAL;
403 }
Andy Grover6708bb22011-06-08 10:36:43 -0700404 if (!tfo->tpg_release_fabric_acl) {
405 pr_err("Missing tfo->tpg_release_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406 return -EINVAL;
407 }
Andy Grover6708bb22011-06-08 10:36:43 -0700408 if (!tfo->tpg_get_inst_index) {
409 pr_err("Missing tfo->tpg_get_inst_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800410 return -EINVAL;
411 }
Christoph Hellwig35462972011-05-31 23:56:57 -0400412 if (!tfo->release_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700413 pr_err("Missing tfo->release_cmd()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800414 return -EINVAL;
415 }
Andy Grover6708bb22011-06-08 10:36:43 -0700416 if (!tfo->shutdown_session) {
417 pr_err("Missing tfo->shutdown_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800418 return -EINVAL;
419 }
Andy Grover6708bb22011-06-08 10:36:43 -0700420 if (!tfo->close_session) {
421 pr_err("Missing tfo->close_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800422 return -EINVAL;
423 }
Andy Grover6708bb22011-06-08 10:36:43 -0700424 if (!tfo->sess_get_index) {
425 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800426 return -EINVAL;
427 }
Andy Grover6708bb22011-06-08 10:36:43 -0700428 if (!tfo->write_pending) {
429 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430 return -EINVAL;
431 }
Andy Grover6708bb22011-06-08 10:36:43 -0700432 if (!tfo->write_pending_status) {
433 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800434 return -EINVAL;
435 }
Andy Grover6708bb22011-06-08 10:36:43 -0700436 if (!tfo->set_default_node_attributes) {
437 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800438 return -EINVAL;
439 }
Andy Grover6708bb22011-06-08 10:36:43 -0700440 if (!tfo->get_task_tag) {
441 pr_err("Missing tfo->get_task_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800442 return -EINVAL;
443 }
Andy Grover6708bb22011-06-08 10:36:43 -0700444 if (!tfo->get_cmd_state) {
445 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800446 return -EINVAL;
447 }
Andy Grover6708bb22011-06-08 10:36:43 -0700448 if (!tfo->queue_data_in) {
449 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800450 return -EINVAL;
451 }
Andy Grover6708bb22011-06-08 10:36:43 -0700452 if (!tfo->queue_status) {
453 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800454 return -EINVAL;
455 }
Andy Grover6708bb22011-06-08 10:36:43 -0700456 if (!tfo->queue_tm_rsp) {
457 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800458 return -EINVAL;
459 }
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700460 if (!tfo->aborted_task) {
461 pr_err("Missing tfo->aborted_task()\n");
462 return -EINVAL;
463 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800464 /*
465 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
466 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
467 * target_core_fabric_configfs.c WWN+TPG group context code.
468 */
Andy Grover6708bb22011-06-08 10:36:43 -0700469 if (!tfo->fabric_make_wwn) {
470 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800471 return -EINVAL;
472 }
Andy Grover6708bb22011-06-08 10:36:43 -0700473 if (!tfo->fabric_drop_wwn) {
474 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800475 return -EINVAL;
476 }
Andy Grover6708bb22011-06-08 10:36:43 -0700477 if (!tfo->fabric_make_tpg) {
478 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800479 return -EINVAL;
480 }
Andy Grover6708bb22011-06-08 10:36:43 -0700481 if (!tfo->fabric_drop_tpg) {
482 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800483 return -EINVAL;
484 }
485
486 return 0;
487}
488
489/*
490 * Called 2nd from fabric module with returned parameter of
491 * struct target_fabric_configfs * from target_fabric_configfs_init().
492 *
493 * Upon a successful registration, the new fabric's struct config_item is
494 * return. Also, a pointer to this struct is set in the passed
495 * struct target_fabric_configfs.
496 */
497int target_fabric_configfs_register(
498 struct target_fabric_configfs *tf)
499{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500 int ret;
501
Andy Grover6708bb22011-06-08 10:36:43 -0700502 if (!tf) {
503 pr_err("Unable to locate target_fabric_configfs"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800504 " pointer\n");
505 return -EINVAL;
506 }
Andy Grover6708bb22011-06-08 10:36:43 -0700507 if (!tf->tf_subsys) {
508 pr_err("Unable to target struct config_subsystem"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800509 " pointer\n");
510 return -EINVAL;
511 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800512 ret = target_fabric_tf_ops_check(tf);
513 if (ret < 0)
514 return ret;
515
Andy Grover6708bb22011-06-08 10:36:43 -0700516 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800517 ">>>>>>>>>>\n");
518 return 0;
519}
520EXPORT_SYMBOL(target_fabric_configfs_register);
521
522void target_fabric_configfs_deregister(
523 struct target_fabric_configfs *tf)
524{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800525 struct configfs_subsystem *su;
526
Andy Grover6708bb22011-06-08 10:36:43 -0700527 if (!tf) {
528 pr_err("Unable to locate passed target_fabric_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800529 "configfs\n");
530 return;
531 }
532 su = tf->tf_subsys;
Andy Grover6708bb22011-06-08 10:36:43 -0700533 if (!su) {
534 pr_err("Unable to locate passed tf->tf_subsys"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535 " pointer\n");
536 return;
537 }
Andy Grover6708bb22011-06-08 10:36:43 -0700538 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800539 ">>>>>>>>>>>>\n");
540 mutex_lock(&g_tf_lock);
541 if (atomic_read(&tf->tf_access_cnt)) {
542 mutex_unlock(&g_tf_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700543 pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800544 tf->tf_name);
545 BUG();
546 }
547 list_del(&tf->tf_list);
548 mutex_unlock(&g_tf_lock);
549
Andy Grover6708bb22011-06-08 10:36:43 -0700550 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800551 " %s\n", tf->tf_name);
552 tf->tf_module = NULL;
553 tf->tf_subsys = NULL;
554 kfree(tf);
555
Andy Grover6708bb22011-06-08 10:36:43 -0700556 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800557 ">>>>>\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800558}
559EXPORT_SYMBOL(target_fabric_configfs_deregister);
560
561/*##############################################################################
562// Stop functions called by external Target Fabrics Modules
563//############################################################################*/
564
565/* Start functions for struct config_item_type target_core_dev_attrib_cit */
566
567#define DEF_DEV_ATTRIB_SHOW(_name) \
568static ssize_t target_core_dev_show_attr_##_name( \
569 struct se_dev_attrib *da, \
570 char *page) \
571{ \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400572 return snprintf(page, PAGE_SIZE, "%u\n", \
573 (u32)da->da_dev->dev_attrib._name); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800574}
575
576#define DEF_DEV_ATTRIB_STORE(_name) \
577static ssize_t target_core_dev_store_attr_##_name( \
578 struct se_dev_attrib *da, \
579 const char *page, \
580 size_t count) \
581{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800582 unsigned long val; \
583 int ret; \
584 \
Jingoo Han57103d72013-07-19 16:22:19 +0900585 ret = kstrtoul(page, 0, &val); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800586 if (ret < 0) { \
Jingoo Han57103d72013-07-19 16:22:19 +0900587 pr_err("kstrtoul() failed with" \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588 " ret: %d\n", ret); \
589 return -EINVAL; \
590 } \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400591 ret = se_dev_set_##_name(da->da_dev, (u32)val); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800592 \
593 return (!ret) ? count : -EINVAL; \
594}
595
596#define DEF_DEV_ATTRIB(_name) \
597DEF_DEV_ATTRIB_SHOW(_name); \
598DEF_DEV_ATTRIB_STORE(_name);
599
600#define DEF_DEV_ATTRIB_RO(_name) \
601DEF_DEV_ATTRIB_SHOW(_name);
602
603CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
604#define SE_DEV_ATTR(_name, _mode) \
605static struct target_core_dev_attrib_attribute \
606 target_core_dev_attrib_##_name = \
607 __CONFIGFS_EATTR(_name, _mode, \
608 target_core_dev_show_attr_##_name, \
609 target_core_dev_store_attr_##_name);
610
611#define SE_DEV_ATTR_RO(_name); \
612static struct target_core_dev_attrib_attribute \
613 target_core_dev_attrib_##_name = \
614 __CONFIGFS_EATTR_RO(_name, \
615 target_core_dev_show_attr_##_name);
616
Tregaron Baylyadfa9572013-01-31 15:30:24 -0700617DEF_DEV_ATTRIB(emulate_model_alias);
618SE_DEV_ATTR(emulate_model_alias, S_IRUGO | S_IWUSR);
619
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800620DEF_DEV_ATTRIB(emulate_dpo);
621SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
622
623DEF_DEV_ATTRIB(emulate_fua_write);
624SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
625
626DEF_DEV_ATTRIB(emulate_fua_read);
627SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
628
629DEF_DEV_ATTRIB(emulate_write_cache);
630SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
631
632DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
633SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
634
635DEF_DEV_ATTRIB(emulate_tas);
636SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
637
638DEF_DEV_ATTRIB(emulate_tpu);
639SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
640
641DEF_DEV_ATTRIB(emulate_tpws);
642SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
643
Nicholas Bellinger0123a9e2013-08-20 14:24:09 -0700644DEF_DEV_ATTRIB(emulate_caw);
645SE_DEV_ATTR(emulate_caw, S_IRUGO | S_IWUSR);
646
Nicholas Bellingerd397a442013-08-22 14:17:20 -0700647DEF_DEV_ATTRIB(emulate_3pc);
648SE_DEV_ATTR(emulate_3pc, S_IRUGO | S_IWUSR);
649
Nicholas Bellinger2ed22c92014-01-08 18:19:31 +0000650DEF_DEV_ATTRIB(pi_prot_type);
651SE_DEV_ATTR(pi_prot_type, S_IRUGO | S_IWUSR);
652
653DEF_DEV_ATTRIB_RO(hw_pi_prot_type);
654SE_DEV_ATTR_RO(hw_pi_prot_type);
655
656DEF_DEV_ATTRIB(pi_prot_format);
657SE_DEV_ATTR(pi_prot_format, S_IRUGO | S_IWUSR);
658
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800659DEF_DEV_ATTRIB(enforce_pr_isids);
660SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
661
Roland Dreiere22a7f02011-07-05 13:34:52 -0700662DEF_DEV_ATTRIB(is_nonrot);
663SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
664
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700665DEF_DEV_ATTRIB(emulate_rest_reord);
666SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
667
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800668DEF_DEV_ATTRIB_RO(hw_block_size);
669SE_DEV_ATTR_RO(hw_block_size);
670
671DEF_DEV_ATTRIB(block_size);
672SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
673
674DEF_DEV_ATTRIB_RO(hw_max_sectors);
675SE_DEV_ATTR_RO(hw_max_sectors);
676
Roland Dreier015487b2012-02-13 16:18:17 -0800677DEF_DEV_ATTRIB(fabric_max_sectors);
678SE_DEV_ATTR(fabric_max_sectors, S_IRUGO | S_IWUSR);
679
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800680DEF_DEV_ATTRIB(optimal_sectors);
681SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
682
683DEF_DEV_ATTRIB_RO(hw_queue_depth);
684SE_DEV_ATTR_RO(hw_queue_depth);
685
686DEF_DEV_ATTRIB(queue_depth);
687SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
688
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800689DEF_DEV_ATTRIB(max_unmap_lba_count);
690SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
691
692DEF_DEV_ATTRIB(max_unmap_block_desc_count);
693SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
694
695DEF_DEV_ATTRIB(unmap_granularity);
696SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
697
698DEF_DEV_ATTRIB(unmap_granularity_alignment);
699SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
700
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800701DEF_DEV_ATTRIB(max_write_same_len);
702SE_DEV_ATTR(max_write_same_len, S_IRUGO | S_IWUSR);
703
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800704CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
705
706static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
Tregaron Baylyadfa9572013-01-31 15:30:24 -0700707 &target_core_dev_attrib_emulate_model_alias.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800708 &target_core_dev_attrib_emulate_dpo.attr,
709 &target_core_dev_attrib_emulate_fua_write.attr,
710 &target_core_dev_attrib_emulate_fua_read.attr,
711 &target_core_dev_attrib_emulate_write_cache.attr,
712 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
713 &target_core_dev_attrib_emulate_tas.attr,
714 &target_core_dev_attrib_emulate_tpu.attr,
715 &target_core_dev_attrib_emulate_tpws.attr,
Nicholas Bellinger0123a9e2013-08-20 14:24:09 -0700716 &target_core_dev_attrib_emulate_caw.attr,
Nicholas Bellingerd397a442013-08-22 14:17:20 -0700717 &target_core_dev_attrib_emulate_3pc.attr,
Nicholas Bellinger2ed22c92014-01-08 18:19:31 +0000718 &target_core_dev_attrib_pi_prot_type.attr,
719 &target_core_dev_attrib_hw_pi_prot_type.attr,
720 &target_core_dev_attrib_pi_prot_format.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800721 &target_core_dev_attrib_enforce_pr_isids.attr,
Roland Dreiere22a7f02011-07-05 13:34:52 -0700722 &target_core_dev_attrib_is_nonrot.attr,
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700723 &target_core_dev_attrib_emulate_rest_reord.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800724 &target_core_dev_attrib_hw_block_size.attr,
725 &target_core_dev_attrib_block_size.attr,
726 &target_core_dev_attrib_hw_max_sectors.attr,
Roland Dreier015487b2012-02-13 16:18:17 -0800727 &target_core_dev_attrib_fabric_max_sectors.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800728 &target_core_dev_attrib_optimal_sectors.attr,
729 &target_core_dev_attrib_hw_queue_depth.attr,
730 &target_core_dev_attrib_queue_depth.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800731 &target_core_dev_attrib_max_unmap_lba_count.attr,
732 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
733 &target_core_dev_attrib_unmap_granularity.attr,
734 &target_core_dev_attrib_unmap_granularity_alignment.attr,
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800735 &target_core_dev_attrib_max_write_same_len.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800736 NULL,
737};
738
739static struct configfs_item_operations target_core_dev_attrib_ops = {
740 .show_attribute = target_core_dev_attrib_attr_show,
741 .store_attribute = target_core_dev_attrib_attr_store,
742};
743
744static struct config_item_type target_core_dev_attrib_cit = {
745 .ct_item_ops = &target_core_dev_attrib_ops,
746 .ct_attrs = target_core_dev_attrib_attrs,
747 .ct_owner = THIS_MODULE,
748};
749
750/* End functions for struct config_item_type target_core_dev_attrib_cit */
751
752/* Start functions for struct config_item_type target_core_dev_wwn_cit */
753
754CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
755#define SE_DEV_WWN_ATTR(_name, _mode) \
756static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
757 __CONFIGFS_EATTR(_name, _mode, \
758 target_core_dev_wwn_show_attr_##_name, \
759 target_core_dev_wwn_store_attr_##_name);
760
761#define SE_DEV_WWN_ATTR_RO(_name); \
762do { \
763 static struct target_core_dev_wwn_attribute \
764 target_core_dev_wwn_##_name = \
765 __CONFIGFS_EATTR_RO(_name, \
766 target_core_dev_wwn_show_attr_##_name); \
767} while (0);
768
769/*
770 * VPD page 0x80 Unit serial
771 */
772static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
773 struct t10_wwn *t10_wwn,
774 char *page)
775{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800776 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
777 &t10_wwn->unit_serial[0]);
778}
779
780static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
781 struct t10_wwn *t10_wwn,
782 const char *page,
783 size_t count)
784{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400785 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800786 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
787
788 /*
789 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
790 * from the struct scsi_device level firmware, do not allow
791 * VPD Unit Serial to be emulated.
792 *
793 * Note this struct scsi_device could also be emulating VPD
794 * information from its drivers/scsi LLD. But for now we assume
795 * it is doing 'the right thing' wrt a world wide unique
796 * VPD Unit Serial Number that OS dependent multipath can depend on.
797 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400798 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700799 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800800 " Unit Serial, ignoring request\n");
801 return -EOPNOTSUPP;
802 }
803
Dan Carpenter60d645a2011-06-15 10:03:05 -0700804 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -0700805 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800806 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
807 return -EOVERFLOW;
808 }
809 /*
810 * Check to see if any active $FABRIC_MOD exports exist. If they
811 * do exist, fail here as changing this information on the fly
812 * (underneath the initiator side OS dependent multipath code)
813 * could cause negative effects.
814 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400815 if (dev->export_count) {
816 pr_err("Unable to set VPD Unit Serial while"
817 " active %d $FABRIC_MOD exports exist\n",
818 dev->export_count);
819 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800820 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400821
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800822 /*
823 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
824 *
825 * Also, strip any newline added from the userspace
826 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
827 */
828 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
829 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400830 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800831 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400832 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800833
Andy Grover6708bb22011-06-08 10:36:43 -0700834 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400835 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800836
837 return count;
838}
839
840SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
841
842/*
843 * VPD page 0x83 Protocol Identifier
844 */
845static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
846 struct t10_wwn *t10_wwn,
847 char *page)
848{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800849 struct t10_vpd *vpd;
850 unsigned char buf[VPD_TMP_BUF_SIZE];
851 ssize_t len = 0;
852
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800853 memset(buf, 0, VPD_TMP_BUF_SIZE);
854
855 spin_lock(&t10_wwn->t10_vpd_lock);
856 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700857 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800858 continue;
859
860 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
861
Andy Grover6708bb22011-06-08 10:36:43 -0700862 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800863 break;
864
865 len += sprintf(page+len, "%s", buf);
866 }
867 spin_unlock(&t10_wwn->t10_vpd_lock);
868
869 return len;
870}
871
872static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
873 struct t10_wwn *t10_wwn,
874 const char *page,
875 size_t count)
876{
877 return -ENOSYS;
878}
879
880SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
881
882/*
883 * Generic wrapper for dumping VPD identifiers by association.
884 */
885#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
886static ssize_t target_core_dev_wwn_show_attr_##_name( \
887 struct t10_wwn *t10_wwn, \
888 char *page) \
889{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800890 struct t10_vpd *vpd; \
891 unsigned char buf[VPD_TMP_BUF_SIZE]; \
892 ssize_t len = 0; \
893 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800894 spin_lock(&t10_wwn->t10_vpd_lock); \
895 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
896 if (vpd->association != _assoc) \
897 continue; \
898 \
899 memset(buf, 0, VPD_TMP_BUF_SIZE); \
900 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700901 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800902 break; \
903 len += sprintf(page+len, "%s", buf); \
904 \
905 memset(buf, 0, VPD_TMP_BUF_SIZE); \
906 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700907 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800908 break; \
909 len += sprintf(page+len, "%s", buf); \
910 \
911 memset(buf, 0, VPD_TMP_BUF_SIZE); \
912 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700913 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800914 break; \
915 len += sprintf(page+len, "%s", buf); \
916 } \
917 spin_unlock(&t10_wwn->t10_vpd_lock); \
918 \
919 return len; \
920}
921
922/*
Andy Shevchenko163cd5f2011-07-18 22:17:43 -0700923 * VPD page 0x83 Association: Logical Unit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800924 */
925DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
926
927static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
928 struct t10_wwn *t10_wwn,
929 const char *page,
930 size_t count)
931{
932 return -ENOSYS;
933}
934
935SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
936
937/*
938 * VPD page 0x83 Association: Target Port
939 */
940DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
941
942static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
943 struct t10_wwn *t10_wwn,
944 const char *page,
945 size_t count)
946{
947 return -ENOSYS;
948}
949
950SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
951
952/*
953 * VPD page 0x83 Association: SCSI Target Device
954 */
955DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
956
957static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
958 struct t10_wwn *t10_wwn,
959 const char *page,
960 size_t count)
961{
962 return -ENOSYS;
963}
964
965SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
966
967CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
968
969static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
970 &target_core_dev_wwn_vpd_unit_serial.attr,
971 &target_core_dev_wwn_vpd_protocol_identifier.attr,
972 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
973 &target_core_dev_wwn_vpd_assoc_target_port.attr,
974 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
975 NULL,
976};
977
978static struct configfs_item_operations target_core_dev_wwn_ops = {
979 .show_attribute = target_core_dev_wwn_attr_show,
980 .store_attribute = target_core_dev_wwn_attr_store,
981};
982
983static struct config_item_type target_core_dev_wwn_cit = {
984 .ct_item_ops = &target_core_dev_wwn_ops,
985 .ct_attrs = target_core_dev_wwn_attrs,
986 .ct_owner = THIS_MODULE,
987};
988
989/* End functions for struct config_item_type target_core_dev_wwn_cit */
990
991/* Start functions for struct config_item_type target_core_dev_pr_cit */
992
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400993CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800994#define SE_DEV_PR_ATTR(_name, _mode) \
995static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
996 __CONFIGFS_EATTR(_name, _mode, \
997 target_core_dev_pr_show_attr_##_name, \
998 target_core_dev_pr_store_attr_##_name);
999
1000#define SE_DEV_PR_ATTR_RO(_name); \
1001static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1002 __CONFIGFS_EATTR_RO(_name, \
1003 target_core_dev_pr_show_attr_##_name);
1004
Christoph Hellwigd977f432012-10-10 17:37:15 -04001005static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1006 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001007{
1008 struct se_node_acl *se_nacl;
1009 struct t10_pr_registration *pr_reg;
1010 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001011
1012 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1013
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001014 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001015 if (!pr_reg)
1016 return sprintf(page, "No SPC-3 Reservation holder\n");
1017
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001018 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -07001019 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001020
Christoph Hellwigd977f432012-10-10 17:37:15 -04001021 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001022 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001023 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001024}
1025
Christoph Hellwigd977f432012-10-10 17:37:15 -04001026static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1027 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001028{
1029 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001030 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001031
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001032 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001033 if (se_nacl) {
1034 len = sprintf(page,
1035 "SPC-2 Reservation: %s Initiator: %s\n",
1036 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1037 se_nacl->initiatorname);
1038 } else {
1039 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001040 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001041 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001042}
1043
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001044static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
1045 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001046{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001047 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001048
Christoph Hellwigd977f432012-10-10 17:37:15 -04001049 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1050 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001051
Christoph Hellwigd977f432012-10-10 17:37:15 -04001052 spin_lock(&dev->dev_reservation_lock);
1053 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1054 ret = target_core_dev_pr_show_spc2_res(dev, page);
1055 else
1056 ret = target_core_dev_pr_show_spc3_res(dev, page);
1057 spin_unlock(&dev->dev_reservation_lock);
1058 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059}
1060
1061SE_DEV_PR_ATTR_RO(res_holder);
1062
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001063static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001064 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001065{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001066 ssize_t len = 0;
1067
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001068 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001069 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001070 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001071 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001072 len = sprintf(page, "SPC-3 Reservation: All Target"
1073 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001074 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001075 len = sprintf(page, "SPC-3 Reservation: Single"
1076 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001077 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001078
Christoph Hellwigd977f432012-10-10 17:37:15 -04001079 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001080 return len;
1081}
1082
1083SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1084
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001085static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001086 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001087{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001088 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001089}
1090
1091SE_DEV_PR_ATTR_RO(res_pr_generation);
1092
1093/*
1094 * res_pr_holder_tg_port
1095 */
1096static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001097 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001098{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001099 struct se_node_acl *se_nacl;
1100 struct se_lun *lun;
1101 struct se_portal_group *se_tpg;
1102 struct t10_pr_registration *pr_reg;
1103 struct target_core_fabric_ops *tfo;
1104 ssize_t len = 0;
1105
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001106 spin_lock(&dev->dev_reservation_lock);
1107 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001108 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001109 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001110 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001111 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001112
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001113 se_nacl = pr_reg->pr_reg_nacl;
1114 se_tpg = se_nacl->se_tpg;
1115 lun = pr_reg->pr_reg_tg_pt_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +00001116 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001117
1118 len += sprintf(page+len, "SPC-3 Reservation: %s"
1119 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1120 tfo->tpg_get_wwn(se_tpg));
1121 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001122 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001123 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1124 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1125 tfo->get_fabric_name(), lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001126
Christoph Hellwigd977f432012-10-10 17:37:15 -04001127out_unlock:
1128 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001129 return len;
1130}
1131
1132SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1133
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001134static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001135 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001136{
1137 struct target_core_fabric_ops *tfo;
1138 struct t10_pr_registration *pr_reg;
1139 unsigned char buf[384];
1140 char i_buf[PR_REG_ISID_ID_LEN];
1141 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001142 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001143
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001144 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1145
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001146 spin_lock(&dev->t10_pr.registration_lock);
1147 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001148 pr_reg_list) {
1149
1150 memset(buf, 0, 384);
1151 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1152 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -07001153 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001154 PR_REG_ISID_ID_LEN);
1155 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1156 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001157 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001158 pr_reg->pr_res_generation);
1159
Andy Grover6708bb22011-06-08 10:36:43 -07001160 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001161 break;
1162
1163 len += sprintf(page+len, "%s", buf);
1164 reg_count++;
1165 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001166 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001167
Andy Grover6708bb22011-06-08 10:36:43 -07001168 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001169 len += sprintf(page+len, "None\n");
1170
1171 return len;
1172}
1173
1174SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1175
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001176static ssize_t target_core_dev_pr_show_attr_res_pr_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001177 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001178{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001179 struct t10_pr_registration *pr_reg;
1180 ssize_t len = 0;
1181
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001182 spin_lock(&dev->dev_reservation_lock);
1183 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001184 if (pr_reg) {
1185 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1186 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1187 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001188 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001189 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001190
Christoph Hellwigd977f432012-10-10 17:37:15 -04001191 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001192 return len;
1193}
1194
1195SE_DEV_PR_ATTR_RO(res_pr_type);
1196
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001197static ssize_t target_core_dev_pr_show_attr_res_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001198 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001199{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001200 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1201 return sprintf(page, "SPC_PASSTHROUGH\n");
1202 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1203 return sprintf(page, "SPC2_RESERVATIONS\n");
1204 else
1205 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001206}
1207
1208SE_DEV_PR_ATTR_RO(res_type);
1209
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001210static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001211 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001212{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001213 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001214 return 0;
1215
1216 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001217 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001218}
1219
1220SE_DEV_PR_ATTR_RO(res_aptpl_active);
1221
1222/*
1223 * res_aptpl_metadata
1224 */
1225static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001226 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001228 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001229 return 0;
1230
1231 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1232}
1233
1234enum {
1235 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1236 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1237 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1238 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1239};
1240
1241static match_table_t tokens = {
1242 {Opt_initiator_fabric, "initiator_fabric=%s"},
1243 {Opt_initiator_node, "initiator_node=%s"},
1244 {Opt_initiator_sid, "initiator_sid=%s"},
1245 {Opt_sa_res_key, "sa_res_key=%s"},
1246 {Opt_res_holder, "res_holder=%d"},
1247 {Opt_res_type, "res_type=%d"},
1248 {Opt_res_scope, "res_scope=%d"},
1249 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1250 {Opt_mapped_lun, "mapped_lun=%d"},
1251 {Opt_target_fabric, "target_fabric=%s"},
1252 {Opt_target_node, "target_node=%s"},
1253 {Opt_tpgt, "tpgt=%d"},
1254 {Opt_port_rtpi, "port_rtpi=%d"},
1255 {Opt_target_lun, "target_lun=%d"},
1256 {Opt_err, NULL}
1257};
1258
1259static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001260 struct se_device *dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001261 const char *page,
1262 size_t count)
1263{
Jesper Juhl6d180252011-03-14 04:05:56 -07001264 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1265 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -04001266 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001267 substring_t args[MAX_OPT_ARGS];
1268 unsigned long long tmp_ll;
1269 u64 sa_res_key = 0;
1270 u32 mapped_lun = 0, target_lun = 0;
1271 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1272 u16 port_rpti = 0, tpgt = 0;
1273 u8 type = 0, scope;
1274
Christoph Hellwigd977f432012-10-10 17:37:15 -04001275 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1276 return 0;
1277 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001278 return 0;
1279
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001280 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001281 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001282 " active fabric exports exist\n");
1283 return -EINVAL;
1284 }
1285
1286 opts = kstrdup(page, GFP_KERNEL);
1287 if (!opts)
1288 return -ENOMEM;
1289
1290 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001291 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001292 if (!*ptr)
1293 continue;
1294
1295 token = match_token(ptr, tokens, args);
1296 switch (token) {
1297 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001298 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001299 if (!i_fabric) {
1300 ret = -ENOMEM;
1301 goto out;
1302 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001303 break;
1304 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001305 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001306 if (!i_port) {
1307 ret = -ENOMEM;
1308 goto out;
1309 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001310 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001311 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1313 PR_APTPL_MAX_IPORT_LEN);
1314 ret = -EINVAL;
1315 break;
1316 }
1317 break;
1318 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001319 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001320 if (!isid) {
1321 ret = -ENOMEM;
1322 goto out;
1323 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001324 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001325 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001326 "= exceeds PR_REG_ISID_LEN: %d\n",
1327 PR_REG_ISID_LEN);
1328 ret = -EINVAL;
1329 break;
1330 }
1331 break;
1332 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001333 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001334 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001335 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001336 goto out;
1337 }
1338 sa_res_key = (u64)tmp_ll;
1339 break;
1340 /*
1341 * PR APTPL Metadata for Reservation
1342 */
1343 case Opt_res_holder:
1344 match_int(args, &arg);
1345 res_holder = arg;
1346 break;
1347 case Opt_res_type:
1348 match_int(args, &arg);
1349 type = (u8)arg;
1350 break;
1351 case Opt_res_scope:
1352 match_int(args, &arg);
1353 scope = (u8)arg;
1354 break;
1355 case Opt_res_all_tg_pt:
1356 match_int(args, &arg);
1357 all_tg_pt = (int)arg;
1358 break;
1359 case Opt_mapped_lun:
1360 match_int(args, &arg);
1361 mapped_lun = (u32)arg;
1362 break;
1363 /*
1364 * PR APTPL Metadata for Target Port
1365 */
1366 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001367 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001368 if (!t_fabric) {
1369 ret = -ENOMEM;
1370 goto out;
1371 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001372 break;
1373 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001374 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001375 if (!t_port) {
1376 ret = -ENOMEM;
1377 goto out;
1378 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001379 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001380 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001381 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1382 PR_APTPL_MAX_TPORT_LEN);
1383 ret = -EINVAL;
1384 break;
1385 }
1386 break;
1387 case Opt_tpgt:
1388 match_int(args, &arg);
1389 tpgt = (u16)arg;
1390 break;
1391 case Opt_port_rtpi:
1392 match_int(args, &arg);
1393 port_rpti = (u16)arg;
1394 break;
1395 case Opt_target_lun:
1396 match_int(args, &arg);
1397 target_lun = (u32)arg;
1398 break;
1399 default:
1400 break;
1401 }
1402 }
1403
Andy Grover6708bb22011-06-08 10:36:43 -07001404 if (!i_port || !t_port || !sa_res_key) {
1405 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001406 ret = -EINVAL;
1407 goto out;
1408 }
1409
1410 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001411 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001412 " holder\n", type);
1413 ret = -EINVAL;
1414 goto out;
1415 }
1416
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001417 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001418 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1419 res_holder, all_tg_pt, type);
1420out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001421 kfree(i_fabric);
1422 kfree(i_port);
1423 kfree(isid);
1424 kfree(t_fabric);
1425 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001426 kfree(orig);
1427 return (ret == 0) ? count : ret;
1428}
1429
1430SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1431
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001432CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001433
1434static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1435 &target_core_dev_pr_res_holder.attr,
1436 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1437 &target_core_dev_pr_res_pr_generation.attr,
1438 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1439 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1440 &target_core_dev_pr_res_pr_type.attr,
1441 &target_core_dev_pr_res_type.attr,
1442 &target_core_dev_pr_res_aptpl_active.attr,
1443 &target_core_dev_pr_res_aptpl_metadata.attr,
1444 NULL,
1445};
1446
1447static struct configfs_item_operations target_core_dev_pr_ops = {
1448 .show_attribute = target_core_dev_pr_attr_show,
1449 .store_attribute = target_core_dev_pr_attr_store,
1450};
1451
1452static struct config_item_type target_core_dev_pr_cit = {
1453 .ct_item_ops = &target_core_dev_pr_ops,
1454 .ct_attrs = target_core_dev_pr_attrs,
1455 .ct_owner = THIS_MODULE,
1456};
1457
1458/* End functions for struct config_item_type target_core_dev_pr_cit */
1459
1460/* Start functions for struct config_item_type target_core_dev_cit */
1461
1462static ssize_t target_core_show_dev_info(void *p, char *page)
1463{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001464 struct se_device *dev = p;
1465 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001466 int bl = 0;
1467 ssize_t read_bytes = 0;
1468
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001469 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001470 read_bytes += bl;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001471 read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001472 return read_bytes;
1473}
1474
1475static struct target_core_configfs_attribute target_core_attr_dev_info = {
1476 .attr = { .ca_owner = THIS_MODULE,
1477 .ca_name = "info",
1478 .ca_mode = S_IRUGO },
1479 .show = target_core_show_dev_info,
1480 .store = NULL,
1481};
1482
1483static ssize_t target_core_store_dev_control(
1484 void *p,
1485 const char *page,
1486 size_t count)
1487{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001488 struct se_device *dev = p;
1489 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001490
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001491 return t->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001492}
1493
1494static struct target_core_configfs_attribute target_core_attr_dev_control = {
1495 .attr = { .ca_owner = THIS_MODULE,
1496 .ca_name = "control",
1497 .ca_mode = S_IWUSR },
1498 .show = NULL,
1499 .store = target_core_store_dev_control,
1500};
1501
1502static ssize_t target_core_show_dev_alias(void *p, char *page)
1503{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001504 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001505
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001506 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001507 return 0;
1508
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001509 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001510}
1511
1512static ssize_t target_core_store_dev_alias(
1513 void *p,
1514 const char *page,
1515 size_t count)
1516{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001517 struct se_device *dev = p;
1518 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001519 ssize_t read_bytes;
1520
1521 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001522 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001523 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1524 SE_DEV_ALIAS_LEN-1);
1525 return -EINVAL;
1526 }
1527
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001528 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001529 if (!read_bytes)
1530 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001531 if (dev->dev_alias[read_bytes - 1] == '\n')
1532 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001533
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001534 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001535
Andy Grover6708bb22011-06-08 10:36:43 -07001536 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001537 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001538 config_item_name(&dev->dev_group.cg_item),
1539 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001540
1541 return read_bytes;
1542}
1543
1544static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1545 .attr = { .ca_owner = THIS_MODULE,
1546 .ca_name = "alias",
1547 .ca_mode = S_IRUGO | S_IWUSR },
1548 .show = target_core_show_dev_alias,
1549 .store = target_core_store_dev_alias,
1550};
1551
1552static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1553{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001554 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001555
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001556 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001557 return 0;
1558
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001559 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001560}
1561
1562static ssize_t target_core_store_dev_udev_path(
1563 void *p,
1564 const char *page,
1565 size_t count)
1566{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001567 struct se_device *dev = p;
1568 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001569 ssize_t read_bytes;
1570
1571 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001572 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001573 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1574 SE_UDEV_PATH_LEN-1);
1575 return -EINVAL;
1576 }
1577
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001578 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001579 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001580 if (!read_bytes)
1581 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001582 if (dev->udev_path[read_bytes - 1] == '\n')
1583 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001584
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001585 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001586
Andy Grover6708bb22011-06-08 10:36:43 -07001587 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001588 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001589 config_item_name(&dev->dev_group.cg_item),
1590 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001591
1592 return read_bytes;
1593}
1594
1595static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1596 .attr = { .ca_owner = THIS_MODULE,
1597 .ca_name = "udev_path",
1598 .ca_mode = S_IRUGO | S_IWUSR },
1599 .show = target_core_show_dev_udev_path,
1600 .store = target_core_store_dev_udev_path,
1601};
1602
Andy Grover64146db2013-04-30 11:59:15 -07001603static ssize_t target_core_show_dev_enable(void *p, char *page)
1604{
1605 struct se_device *dev = p;
1606
1607 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1608}
1609
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001610static ssize_t target_core_store_dev_enable(
1611 void *p,
1612 const char *page,
1613 size_t count)
1614{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001615 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001616 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001617 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001618
1619 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001620 if (!ptr) {
1621 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001622 " is \"1\"\n");
1623 return -EINVAL;
1624 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001625
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001626 ret = target_configure_device(dev);
1627 if (ret)
1628 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001629 return count;
1630}
1631
1632static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1633 .attr = { .ca_owner = THIS_MODULE,
1634 .ca_name = "enable",
Andy Grover64146db2013-04-30 11:59:15 -07001635 .ca_mode = S_IRUGO | S_IWUSR },
1636 .show = target_core_show_dev_enable,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001637 .store = target_core_store_dev_enable,
1638};
1639
1640static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1641{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001642 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001643 struct config_item *lu_ci;
1644 struct t10_alua_lu_gp *lu_gp;
1645 struct t10_alua_lu_gp_member *lu_gp_mem;
1646 ssize_t len = 0;
1647
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001648 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001649 if (!lu_gp_mem)
1650 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001651
1652 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1653 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001654 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001655 lu_ci = &lu_gp->lu_gp_group.cg_item;
1656 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1657 config_item_name(lu_ci), lu_gp->lu_gp_id);
1658 }
1659 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1660
1661 return len;
1662}
1663
1664static ssize_t target_core_store_alua_lu_gp(
1665 void *p,
1666 const char *page,
1667 size_t count)
1668{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001669 struct se_device *dev = p;
1670 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001671 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1672 struct t10_alua_lu_gp_member *lu_gp_mem;
1673 unsigned char buf[LU_GROUP_NAME_BUF];
1674 int move = 0;
1675
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001676 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1677 if (!lu_gp_mem)
1678 return 0;
1679
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001680 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001681 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001682 return -EINVAL;
1683 }
1684 memset(buf, 0, LU_GROUP_NAME_BUF);
1685 memcpy(buf, page, count);
1686 /*
1687 * Any ALUA logical unit alias besides "NULL" means we will be
1688 * making a new group association.
1689 */
1690 if (strcmp(strstrip(buf), "NULL")) {
1691 /*
1692 * core_alua_get_lu_gp_by_name() will increment reference to
1693 * struct t10_alua_lu_gp. This reference is released with
1694 * core_alua_get_lu_gp_by_name below().
1695 */
1696 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001697 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001698 return -ENODEV;
1699 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001700
1701 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1702 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001703 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001704 /*
1705 * Clearing an existing lu_gp association, and replacing
1706 * with NULL
1707 */
Andy Grover6708bb22011-06-08 10:36:43 -07001708 if (!lu_gp_new) {
1709 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001710 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1711 " %hu\n",
1712 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001713 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001714 config_item_name(&lu_gp->lu_gp_group.cg_item),
1715 lu_gp->lu_gp_id);
1716
1717 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1718 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1719
1720 return count;
1721 }
1722 /*
1723 * Removing existing association of lu_gp_mem with lu_gp
1724 */
1725 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1726 move = 1;
1727 }
1728 /*
1729 * Associate lu_gp_mem with lu_gp_new.
1730 */
1731 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1732 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1733
Andy Grover6708bb22011-06-08 10:36:43 -07001734 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001735 " core/alua/lu_gps/%s, ID: %hu\n",
1736 (move) ? "Moving" : "Adding",
1737 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001738 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001739 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1740 lu_gp_new->lu_gp_id);
1741
1742 core_alua_put_lu_gp_from_name(lu_gp_new);
1743 return count;
1744}
1745
1746static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1747 .attr = { .ca_owner = THIS_MODULE,
1748 .ca_name = "alua_lu_gp",
1749 .ca_mode = S_IRUGO | S_IWUSR },
1750 .show = target_core_show_alua_lu_gp,
1751 .store = target_core_store_alua_lu_gp,
1752};
1753
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001754static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1755{
1756 struct se_device *dev = p;
1757 struct t10_alua_lba_map *map;
1758 struct t10_alua_lba_map_member *mem;
1759 char *b = page;
1760 int bl = 0;
1761 char state;
1762
1763 spin_lock(&dev->t10_alua.lba_map_lock);
1764 if (!list_empty(&dev->t10_alua.lba_map_list))
1765 bl += sprintf(b + bl, "%u %u\n",
1766 dev->t10_alua.lba_map_segment_size,
1767 dev->t10_alua.lba_map_segment_multiplier);
1768 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1769 bl += sprintf(b + bl, "%llu %llu",
1770 map->lba_map_first_lba, map->lba_map_last_lba);
1771 list_for_each_entry(mem, &map->lba_map_mem_list,
1772 lba_map_mem_list) {
1773 switch (mem->lba_map_mem_alua_state) {
1774 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1775 state = 'O';
1776 break;
1777 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1778 state = 'A';
1779 break;
1780 case ALUA_ACCESS_STATE_STANDBY:
1781 state = 'S';
1782 break;
1783 case ALUA_ACCESS_STATE_UNAVAILABLE:
1784 state = 'U';
1785 break;
1786 default:
1787 state = '.';
1788 break;
1789 }
1790 bl += sprintf(b + bl, " %d:%c",
1791 mem->lba_map_mem_alua_pg_id, state);
1792 }
1793 bl += sprintf(b + bl, "\n");
1794 }
1795 spin_unlock(&dev->t10_alua.lba_map_lock);
1796 return bl;
1797}
1798
1799static ssize_t target_core_store_dev_lba_map(
1800 void *p,
1801 const char *page,
1802 size_t count)
1803{
1804 struct se_device *dev = p;
1805 struct t10_alua_lba_map *lba_map = NULL;
1806 struct list_head lba_list;
1807 char *map_entries, *ptr;
1808 char state;
1809 int pg_num = -1, pg;
1810 int ret = 0, num = 0, pg_id, alua_state;
1811 unsigned long start_lba = -1, end_lba = -1;
1812 unsigned long segment_size = -1, segment_mult = -1;
1813
1814 map_entries = kstrdup(page, GFP_KERNEL);
1815 if (!map_entries)
1816 return -ENOMEM;
1817
1818 INIT_LIST_HEAD(&lba_list);
1819 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1820 if (!*ptr)
1821 continue;
1822
1823 if (num == 0) {
1824 if (sscanf(ptr, "%lu %lu\n",
1825 &segment_size, &segment_mult) != 2) {
1826 pr_err("Invalid line %d\n", num);
1827 ret = -EINVAL;
1828 break;
1829 }
1830 num++;
1831 continue;
1832 }
1833 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1834 pr_err("Invalid line %d\n", num);
1835 ret = -EINVAL;
1836 break;
1837 }
1838 ptr = strchr(ptr, ' ');
1839 if (!ptr) {
1840 pr_err("Invalid line %d, missing end lba\n", num);
1841 ret = -EINVAL;
1842 break;
1843 }
1844 ptr++;
1845 ptr = strchr(ptr, ' ');
1846 if (!ptr) {
1847 pr_err("Invalid line %d, missing state definitions\n",
1848 num);
1849 ret = -EINVAL;
1850 break;
1851 }
1852 ptr++;
1853 lba_map = core_alua_allocate_lba_map(&lba_list,
1854 start_lba, end_lba);
1855 if (IS_ERR(lba_map)) {
1856 ret = PTR_ERR(lba_map);
1857 break;
1858 }
1859 pg = 0;
1860 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1861 switch (state) {
1862 case 'O':
1863 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1864 break;
1865 case 'A':
1866 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1867 break;
1868 case 'S':
1869 alua_state = ALUA_ACCESS_STATE_STANDBY;
1870 break;
1871 case 'U':
1872 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1873 break;
1874 default:
1875 pr_err("Invalid ALUA state '%c'\n", state);
1876 ret = -EINVAL;
1877 goto out;
1878 }
1879
1880 ret = core_alua_allocate_lba_map_mem(lba_map,
1881 pg_id, alua_state);
1882 if (ret) {
1883 pr_err("Invalid target descriptor %d:%c "
1884 "at line %d\n",
1885 pg_id, state, num);
1886 break;
1887 }
1888 pg++;
1889 ptr = strchr(ptr, ' ');
1890 if (ptr)
1891 ptr++;
1892 else
1893 break;
1894 }
1895 if (pg_num == -1)
1896 pg_num = pg;
1897 else if (pg != pg_num) {
1898 pr_err("Only %d from %d port groups definitions "
1899 "at line %d\n", pg, pg_num, num);
1900 ret = -EINVAL;
1901 break;
1902 }
1903 num++;
1904 }
1905out:
1906 if (ret) {
1907 core_alua_free_lba_map(&lba_list);
1908 count = ret;
1909 } else
1910 core_alua_set_lba_map(dev, &lba_list,
1911 segment_size, segment_mult);
1912 kfree(map_entries);
1913 return count;
1914}
1915
1916static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1917 .attr = { .ca_owner = THIS_MODULE,
1918 .ca_name = "lba_map",
1919 .ca_mode = S_IRUGO | S_IWUSR },
1920 .show = target_core_show_dev_lba_map,
1921 .store = target_core_store_dev_lba_map,
1922};
1923
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001924static struct configfs_attribute *lio_core_dev_attrs[] = {
1925 &target_core_attr_dev_info.attr,
1926 &target_core_attr_dev_control.attr,
1927 &target_core_attr_dev_alias.attr,
1928 &target_core_attr_dev_udev_path.attr,
1929 &target_core_attr_dev_enable.attr,
1930 &target_core_attr_dev_alua_lu_gp.attr,
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001931 &target_core_attr_dev_lba_map.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001932 NULL,
1933};
1934
1935static void target_core_dev_release(struct config_item *item)
1936{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001937 struct config_group *dev_cg = to_config_group(item);
1938 struct se_device *dev =
1939 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001940
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001941 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001942 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001943}
1944
1945static ssize_t target_core_dev_show(struct config_item *item,
1946 struct configfs_attribute *attr,
1947 char *page)
1948{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001949 struct config_group *dev_cg = to_config_group(item);
1950 struct se_device *dev =
1951 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001952 struct target_core_configfs_attribute *tc_attr = container_of(
1953 attr, struct target_core_configfs_attribute, attr);
1954
Andy Grover6708bb22011-06-08 10:36:43 -07001955 if (!tc_attr->show)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001956 return -EINVAL;
1957
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001958 return tc_attr->show(dev, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001959}
1960
1961static ssize_t target_core_dev_store(struct config_item *item,
1962 struct configfs_attribute *attr,
1963 const char *page, size_t count)
1964{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001965 struct config_group *dev_cg = to_config_group(item);
1966 struct se_device *dev =
1967 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001968 struct target_core_configfs_attribute *tc_attr = container_of(
1969 attr, struct target_core_configfs_attribute, attr);
1970
Andy Grover6708bb22011-06-08 10:36:43 -07001971 if (!tc_attr->store)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001972 return -EINVAL;
1973
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001974 return tc_attr->store(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001975}
1976
1977static struct configfs_item_operations target_core_dev_item_ops = {
1978 .release = target_core_dev_release,
1979 .show_attribute = target_core_dev_show,
1980 .store_attribute = target_core_dev_store,
1981};
1982
1983static struct config_item_type target_core_dev_cit = {
1984 .ct_item_ops = &target_core_dev_item_ops,
1985 .ct_attrs = lio_core_dev_attrs,
1986 .ct_owner = THIS_MODULE,
1987};
1988
1989/* End functions for struct config_item_type target_core_dev_cit */
1990
1991/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1992
1993CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1994#define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
1995static struct target_core_alua_lu_gp_attribute \
1996 target_core_alua_lu_gp_##_name = \
1997 __CONFIGFS_EATTR(_name, _mode, \
1998 target_core_alua_lu_gp_show_attr_##_name, \
1999 target_core_alua_lu_gp_store_attr_##_name);
2000
2001#define SE_DEV_ALUA_LU_ATTR_RO(_name) \
2002static struct target_core_alua_lu_gp_attribute \
2003 target_core_alua_lu_gp_##_name = \
2004 __CONFIGFS_EATTR_RO(_name, \
2005 target_core_alua_lu_gp_show_attr_##_name);
2006
2007/*
2008 * lu_gp_id
2009 */
2010static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2011 struct t10_alua_lu_gp *lu_gp,
2012 char *page)
2013{
Andy Grover6708bb22011-06-08 10:36:43 -07002014 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002015 return 0;
2016
2017 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2018}
2019
2020static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2021 struct t10_alua_lu_gp *lu_gp,
2022 const char *page,
2023 size_t count)
2024{
2025 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2026 unsigned long lu_gp_id;
2027 int ret;
2028
Jingoo Han57103d72013-07-19 16:22:19 +09002029 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002030 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002031 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002032 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002033 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002034 }
2035 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002036 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002037 " 0x0000ffff\n", lu_gp_id);
2038 return -EINVAL;
2039 }
2040
2041 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2042 if (ret < 0)
2043 return -EINVAL;
2044
Andy Grover6708bb22011-06-08 10:36:43 -07002045 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002046 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2047 config_item_name(&alua_lu_gp_cg->cg_item),
2048 lu_gp->lu_gp_id);
2049
2050 return count;
2051}
2052
2053SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2054
2055/*
2056 * members
2057 */
2058static ssize_t target_core_alua_lu_gp_show_attr_members(
2059 struct t10_alua_lu_gp *lu_gp,
2060 char *page)
2061{
2062 struct se_device *dev;
2063 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002064 struct t10_alua_lu_gp_member *lu_gp_mem;
2065 ssize_t len = 0, cur_len;
2066 unsigned char buf[LU_GROUP_NAME_BUF];
2067
2068 memset(buf, 0, LU_GROUP_NAME_BUF);
2069
2070 spin_lock(&lu_gp->lu_gp_lock);
2071 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2072 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002073 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002074
2075 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2076 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002077 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002078 cur_len++; /* Extra byte for NULL terminator */
2079
2080 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002081 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002082 "_members buffer\n");
2083 break;
2084 }
2085 memcpy(page+len, buf, cur_len);
2086 len += cur_len;
2087 }
2088 spin_unlock(&lu_gp->lu_gp_lock);
2089
2090 return len;
2091}
2092
2093SE_DEV_ALUA_LU_ATTR_RO(members);
2094
2095CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2096
2097static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2098 &target_core_alua_lu_gp_lu_gp_id.attr,
2099 &target_core_alua_lu_gp_members.attr,
2100 NULL,
2101};
2102
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002103static void target_core_alua_lu_gp_release(struct config_item *item)
2104{
2105 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2106 struct t10_alua_lu_gp, lu_gp_group);
2107
2108 core_alua_free_lu_gp(lu_gp);
2109}
2110
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002111static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002112 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002113 .show_attribute = target_core_alua_lu_gp_attr_show,
2114 .store_attribute = target_core_alua_lu_gp_attr_store,
2115};
2116
2117static struct config_item_type target_core_alua_lu_gp_cit = {
2118 .ct_item_ops = &target_core_alua_lu_gp_ops,
2119 .ct_attrs = target_core_alua_lu_gp_attrs,
2120 .ct_owner = THIS_MODULE,
2121};
2122
2123/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2124
2125/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2126
2127static struct config_group *target_core_alua_create_lu_gp(
2128 struct config_group *group,
2129 const char *name)
2130{
2131 struct t10_alua_lu_gp *lu_gp;
2132 struct config_group *alua_lu_gp_cg = NULL;
2133 struct config_item *alua_lu_gp_ci = NULL;
2134
2135 lu_gp = core_alua_allocate_lu_gp(name, 0);
2136 if (IS_ERR(lu_gp))
2137 return NULL;
2138
2139 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2140 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2141
2142 config_group_init_type_name(alua_lu_gp_cg, name,
2143 &target_core_alua_lu_gp_cit);
2144
Andy Grover6708bb22011-06-08 10:36:43 -07002145 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002146 " Group: core/alua/lu_gps/%s\n",
2147 config_item_name(alua_lu_gp_ci));
2148
2149 return alua_lu_gp_cg;
2150
2151}
2152
2153static void target_core_alua_drop_lu_gp(
2154 struct config_group *group,
2155 struct config_item *item)
2156{
2157 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2158 struct t10_alua_lu_gp, lu_gp_group);
2159
Andy Grover6708bb22011-06-08 10:36:43 -07002160 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002161 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2162 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002163 /*
2164 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2165 * -> target_core_alua_lu_gp_release()
2166 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002167 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002168}
2169
2170static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2171 .make_group = &target_core_alua_create_lu_gp,
2172 .drop_item = &target_core_alua_drop_lu_gp,
2173};
2174
2175static struct config_item_type target_core_alua_lu_gps_cit = {
2176 .ct_item_ops = NULL,
2177 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2178 .ct_owner = THIS_MODULE,
2179};
2180
2181/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2182
2183/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2184
2185CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2186#define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
2187static struct target_core_alua_tg_pt_gp_attribute \
2188 target_core_alua_tg_pt_gp_##_name = \
2189 __CONFIGFS_EATTR(_name, _mode, \
2190 target_core_alua_tg_pt_gp_show_attr_##_name, \
2191 target_core_alua_tg_pt_gp_store_attr_##_name);
2192
2193#define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
2194static struct target_core_alua_tg_pt_gp_attribute \
2195 target_core_alua_tg_pt_gp_##_name = \
2196 __CONFIGFS_EATTR_RO(_name, \
2197 target_core_alua_tg_pt_gp_show_attr_##_name);
2198
2199/*
2200 * alua_access_state
2201 */
2202static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2203 struct t10_alua_tg_pt_gp *tg_pt_gp,
2204 char *page)
2205{
2206 return sprintf(page, "%d\n",
2207 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2208}
2209
2210static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2211 struct t10_alua_tg_pt_gp *tg_pt_gp,
2212 const char *page,
2213 size_t count)
2214{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002215 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002216 unsigned long tmp;
2217 int new_state, ret;
2218
Andy Grover6708bb22011-06-08 10:36:43 -07002219 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002220 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002221 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2222 return -EINVAL;
2223 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07002224 if (!(dev->dev_flags & DF_CONFIGURED)) {
2225 pr_err("Unable to set alua_access_state while device is"
2226 " not configured\n");
2227 return -ENODEV;
2228 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002229
Jingoo Han57103d72013-07-19 16:22:19 +09002230 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002231 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002232 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002233 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002234 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002235 }
2236 new_state = (int)tmp;
2237
Hannes Reinecke125d0112013-11-19 09:07:46 +01002238 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2239 pr_err("Unable to process implicit configfs ALUA"
2240 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002241 return -EINVAL;
2242 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002243 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2244 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2245 /* LBA DEPENDENT is only allowed with implicit ALUA */
2246 pr_err("Unable to process implicit configfs ALUA transition"
2247 " while explicit ALUA management is enabled\n");
2248 return -EINVAL;
2249 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002250
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002251 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002252 NULL, NULL, new_state, 0);
2253 return (!ret) ? count : -EINVAL;
2254}
2255
2256SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2257
2258/*
2259 * alua_access_status
2260 */
2261static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2262 struct t10_alua_tg_pt_gp *tg_pt_gp,
2263 char *page)
2264{
2265 return sprintf(page, "%s\n",
2266 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2267}
2268
2269static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2270 struct t10_alua_tg_pt_gp *tg_pt_gp,
2271 const char *page,
2272 size_t count)
2273{
2274 unsigned long tmp;
2275 int new_status, ret;
2276
Andy Grover6708bb22011-06-08 10:36:43 -07002277 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2278 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002279 " valid tg_pt_gp ID: %hu\n",
2280 tg_pt_gp->tg_pt_gp_valid_id);
2281 return -EINVAL;
2282 }
2283
Jingoo Han57103d72013-07-19 16:22:19 +09002284 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002285 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002286 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002287 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002288 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002289 }
2290 new_status = (int)tmp;
2291
2292 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002293 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2294 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002295 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002296 new_status);
2297 return -EINVAL;
2298 }
2299
2300 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2301 return count;
2302}
2303
2304SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2305
2306/*
2307 * alua_access_type
2308 */
2309static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2310 struct t10_alua_tg_pt_gp *tg_pt_gp,
2311 char *page)
2312{
2313 return core_alua_show_access_type(tg_pt_gp, page);
2314}
2315
2316static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2317 struct t10_alua_tg_pt_gp *tg_pt_gp,
2318 const char *page,
2319 size_t count)
2320{
2321 return core_alua_store_access_type(tg_pt_gp, page, count);
2322}
2323
2324SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2325
2326/*
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002327 * alua_supported_states
2328 */
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002329
2330#define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit) \
2331static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2332 struct t10_alua_tg_pt_gp *t, char *p) \
2333{ \
2334 return sprintf(p, "%d\n", !!(t->_var & _bit)); \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002335}
2336
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002337#define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit) \
2338static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2339 struct t10_alua_tg_pt_gp *t, const char *p, size_t c) \
2340{ \
2341 unsigned long tmp; \
2342 int ret; \
2343 \
2344 if (!t->tg_pt_gp_valid_id) { \
2345 pr_err("Unable to do set ##_name ALUA state on non" \
2346 " valid tg_pt_gp ID: %hu\n", \
2347 t->tg_pt_gp_valid_id); \
2348 return -EINVAL; \
2349 } \
2350 \
2351 ret = kstrtoul(p, 0, &tmp); \
2352 if (ret < 0) { \
2353 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2354 return -EINVAL; \
2355 } \
2356 if (tmp > 1) { \
2357 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2358 return -EINVAL; \
2359 } \
2360 if (!tmp) \
2361 t->_var |= _bit; \
2362 else \
2363 t->_var &= ~_bit; \
2364 \
2365 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002366}
2367
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002368SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2369 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2370SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2371 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2372SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
2373
2374SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2375 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2376SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2377 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2378SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
2379
2380SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2381 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2382SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2383 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
Hannes Reineckec66094b2013-12-17 09:18:49 +01002384SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002385
2386SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2387 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2388SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2389 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2390SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
2391
2392SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2393 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2394SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2395 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2396SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2397
2398SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2399 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2400SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2401 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2402SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2403
2404SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2405 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2406SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2407 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2408SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002409
2410/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002411 * alua_write_metadata
2412 */
2413static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2414 struct t10_alua_tg_pt_gp *tg_pt_gp,
2415 char *page)
2416{
2417 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2418}
2419
2420static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2421 struct t10_alua_tg_pt_gp *tg_pt_gp,
2422 const char *page,
2423 size_t count)
2424{
2425 unsigned long tmp;
2426 int ret;
2427
Jingoo Han57103d72013-07-19 16:22:19 +09002428 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002429 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002430 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002431 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002432 }
2433
2434 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002435 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002436 " %lu\n", tmp);
2437 return -EINVAL;
2438 }
2439 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2440
2441 return count;
2442}
2443
2444SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2445
2446
2447
2448/*
2449 * nonop_delay_msecs
2450 */
2451static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2452 struct t10_alua_tg_pt_gp *tg_pt_gp,
2453 char *page)
2454{
2455 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2456
2457}
2458
2459static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2460 struct t10_alua_tg_pt_gp *tg_pt_gp,
2461 const char *page,
2462 size_t count)
2463{
2464 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2465}
2466
2467SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2468
2469/*
2470 * trans_delay_msecs
2471 */
2472static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2473 struct t10_alua_tg_pt_gp *tg_pt_gp,
2474 char *page)
2475{
2476 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2477}
2478
2479static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2480 struct t10_alua_tg_pt_gp *tg_pt_gp,
2481 const char *page,
2482 size_t count)
2483{
2484 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2485}
2486
2487SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2488
2489/*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002490 * implicit_trans_secs
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002491 */
Hannes Reinecke125d0112013-11-19 09:07:46 +01002492static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002493 struct t10_alua_tg_pt_gp *tg_pt_gp,
2494 char *page)
2495{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002496 return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002497}
2498
Hannes Reinecke125d0112013-11-19 09:07:46 +01002499static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002500 struct t10_alua_tg_pt_gp *tg_pt_gp,
2501 const char *page,
2502 size_t count)
2503{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002504 return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002505}
2506
Hannes Reinecke125d0112013-11-19 09:07:46 +01002507SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002508
2509/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002510 * preferred
2511 */
2512
2513static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2514 struct t10_alua_tg_pt_gp *tg_pt_gp,
2515 char *page)
2516{
2517 return core_alua_show_preferred_bit(tg_pt_gp, page);
2518}
2519
2520static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2521 struct t10_alua_tg_pt_gp *tg_pt_gp,
2522 const char *page,
2523 size_t count)
2524{
2525 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2526}
2527
2528SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2529
2530/*
2531 * tg_pt_gp_id
2532 */
2533static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2534 struct t10_alua_tg_pt_gp *tg_pt_gp,
2535 char *page)
2536{
Andy Grover6708bb22011-06-08 10:36:43 -07002537 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538 return 0;
2539
2540 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2541}
2542
2543static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2544 struct t10_alua_tg_pt_gp *tg_pt_gp,
2545 const char *page,
2546 size_t count)
2547{
2548 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2549 unsigned long tg_pt_gp_id;
2550 int ret;
2551
Jingoo Han57103d72013-07-19 16:22:19 +09002552 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002553 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002554 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002555 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002556 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002557 }
2558 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002559 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002560 " 0x0000ffff\n", tg_pt_gp_id);
2561 return -EINVAL;
2562 }
2563
2564 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2565 if (ret < 0)
2566 return -EINVAL;
2567
Andy Grover6708bb22011-06-08 10:36:43 -07002568 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002569 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2570 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2571 tg_pt_gp->tg_pt_gp_id);
2572
2573 return count;
2574}
2575
2576SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2577
2578/*
2579 * members
2580 */
2581static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2582 struct t10_alua_tg_pt_gp *tg_pt_gp,
2583 char *page)
2584{
2585 struct se_port *port;
2586 struct se_portal_group *tpg;
2587 struct se_lun *lun;
2588 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2589 ssize_t len = 0, cur_len;
2590 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2591
2592 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2593
2594 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2595 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2596 tg_pt_gp_mem_list) {
2597 port = tg_pt_gp_mem->tg_pt;
2598 tpg = port->sep_tpg;
2599 lun = port->sep_lun;
2600
2601 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002602 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2603 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2604 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002605 config_item_name(&lun->lun_group.cg_item));
2606 cur_len++; /* Extra byte for NULL terminator */
2607
2608 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002609 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002610 "_members buffer\n");
2611 break;
2612 }
2613 memcpy(page+len, buf, cur_len);
2614 len += cur_len;
2615 }
2616 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2617
2618 return len;
2619}
2620
2621SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2622
2623CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2624 tg_pt_gp_group);
2625
2626static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2627 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2628 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2629 &target_core_alua_tg_pt_gp_alua_access_type.attr,
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002630 &target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2631 &target_core_alua_tg_pt_gp_alua_support_offline.attr,
2632 &target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2633 &target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2634 &target_core_alua_tg_pt_gp_alua_support_standby.attr,
2635 &target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2636 &target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002637 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2638 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2639 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
Hannes Reinecke125d0112013-11-19 09:07:46 +01002640 &target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002641 &target_core_alua_tg_pt_gp_preferred.attr,
2642 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2643 &target_core_alua_tg_pt_gp_members.attr,
2644 NULL,
2645};
2646
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002647static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2648{
2649 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2650 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2651
2652 core_alua_free_tg_pt_gp(tg_pt_gp);
2653}
2654
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002655static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002656 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002657 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2658 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2659};
2660
2661static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2662 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2663 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2664 .ct_owner = THIS_MODULE,
2665};
2666
2667/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2668
2669/* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2670
2671static struct config_group *target_core_alua_create_tg_pt_gp(
2672 struct config_group *group,
2673 const char *name)
2674{
2675 struct t10_alua *alua = container_of(group, struct t10_alua,
2676 alua_tg_pt_gps_group);
2677 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002678 struct config_group *alua_tg_pt_gp_cg = NULL;
2679 struct config_item *alua_tg_pt_gp_ci = NULL;
2680
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002681 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002682 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002683 return NULL;
2684
2685 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2686 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2687
2688 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2689 &target_core_alua_tg_pt_gp_cit);
2690
Andy Grover6708bb22011-06-08 10:36:43 -07002691 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002692 " Group: alua/tg_pt_gps/%s\n",
2693 config_item_name(alua_tg_pt_gp_ci));
2694
2695 return alua_tg_pt_gp_cg;
2696}
2697
2698static void target_core_alua_drop_tg_pt_gp(
2699 struct config_group *group,
2700 struct config_item *item)
2701{
2702 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2703 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2704
Andy Grover6708bb22011-06-08 10:36:43 -07002705 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002706 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2707 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002708 /*
2709 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2710 * -> target_core_alua_tg_pt_gp_release().
2711 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002712 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002713}
2714
2715static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2716 .make_group = &target_core_alua_create_tg_pt_gp,
2717 .drop_item = &target_core_alua_drop_tg_pt_gp,
2718};
2719
2720static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2721 .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
2722 .ct_owner = THIS_MODULE,
2723};
2724
2725/* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2726
2727/* Start functions for struct config_item_type target_core_alua_cit */
2728
2729/*
2730 * target_core_alua_cit is a ConfigFS group that lives under
2731 * /sys/kernel/config/target/core/alua. There are default groups
2732 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2733 * target_core_alua_cit in target_core_init_configfs() below.
2734 */
2735static struct config_item_type target_core_alua_cit = {
2736 .ct_item_ops = NULL,
2737 .ct_attrs = NULL,
2738 .ct_owner = THIS_MODULE,
2739};
2740
2741/* End functions for struct config_item_type target_core_alua_cit */
2742
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002743/* Start functions for struct config_item_type target_core_stat_cit */
2744
2745static struct config_group *target_core_stat_mkdir(
2746 struct config_group *group,
2747 const char *name)
2748{
2749 return ERR_PTR(-ENOSYS);
2750}
2751
2752static void target_core_stat_rmdir(
2753 struct config_group *group,
2754 struct config_item *item)
2755{
2756 return;
2757}
2758
2759static struct configfs_group_operations target_core_stat_group_ops = {
2760 .make_group = &target_core_stat_mkdir,
2761 .drop_item = &target_core_stat_rmdir,
2762};
2763
2764static struct config_item_type target_core_stat_cit = {
2765 .ct_group_ops = &target_core_stat_group_ops,
2766 .ct_owner = THIS_MODULE,
2767};
2768
2769/* End functions for struct config_item_type target_core_stat_cit */
2770
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002771/* Start functions for struct config_item_type target_core_hba_cit */
2772
2773static struct config_group *target_core_make_subdev(
2774 struct config_group *group,
2775 const char *name)
2776{
2777 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002778 struct se_subsystem_api *t;
2779 struct config_item *hba_ci = &group->cg_item;
2780 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002781 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002782 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002783 struct config_group *dev_stat_grp = NULL;
2784 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002785
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002786 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2787 if (ret)
2788 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002789 /*
2790 * Locate the struct se_subsystem_api from parent's struct se_hba.
2791 */
2792 t = hba->transport;
2793
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002794 dev = target_alloc_device(hba, name);
2795 if (!dev)
2796 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002797
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002798 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002799
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002800 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002801 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002802 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002803 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002804
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002805 config_group_init_type_name(dev_cg, name, &target_core_dev_cit);
2806 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002807 &target_core_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002808 config_group_init_type_name(&dev->dev_pr_group, "pr",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002809 &target_core_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002810 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002811 &target_core_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002812 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002813 "alua", &target_core_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002814 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002815 "statistics", &target_core_stat_cit);
2816
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002817 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2818 dev_cg->default_groups[1] = &dev->dev_pr_group;
2819 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2820 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2821 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002822 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002823 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002824 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002825 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002826 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002827 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002828 goto out_free_dev_cg_default_groups;
2829 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002830
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002831 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002832 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002833 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002834 if (!tg_pt_gp_cg->default_groups) {
2835 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002836 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002837 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002838 }
2839
2840 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2841 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2842 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2843 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002844 /*
2845 * Add core/$HBA/$DEV/statistics/ default groups
2846 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002847 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002848 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002849 GFP_KERNEL);
2850 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002851 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002852 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002853 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002854 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002855
2856 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002857 return dev_cg;
2858
2859out_free_tg_pt_gp_cg_default_groups:
2860 kfree(tg_pt_gp_cg->default_groups);
2861out_free_tg_pt_gp:
2862 core_alua_free_tg_pt_gp(tg_pt_gp);
2863out_free_dev_cg_default_groups:
2864 kfree(dev_cg->default_groups);
2865out_free_device:
2866 target_free_device(dev);
2867out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002868 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002869 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002870}
2871
2872static void target_core_drop_subdev(
2873 struct config_group *group,
2874 struct config_item *item)
2875{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002876 struct config_group *dev_cg = to_config_group(item);
2877 struct se_device *dev =
2878 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002879 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002880 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002881 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002882 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002883
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002884 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002885
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002886 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002887
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002888 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002889 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2890 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2891 dev_stat_grp->default_groups[i] = NULL;
2892 config_item_put(df_item);
2893 }
2894 kfree(dev_stat_grp->default_groups);
2895
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002896 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002897 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2898 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2899 tg_pt_gp_cg->default_groups[i] = NULL;
2900 config_item_put(df_item);
2901 }
2902 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002903 /*
2904 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2905 * directly from target_core_alua_tg_pt_gp_release().
2906 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002907 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002908
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002909 for (i = 0; dev_cg->default_groups[i]; i++) {
2910 df_item = &dev_cg->default_groups[i]->cg_item;
2911 dev_cg->default_groups[i] = NULL;
2912 config_item_put(df_item);
2913 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002914 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002915 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002916 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002917 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002918 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002919}
2920
2921static struct configfs_group_operations target_core_hba_group_ops = {
2922 .make_group = target_core_make_subdev,
2923 .drop_item = target_core_drop_subdev,
2924};
2925
2926CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2927#define SE_HBA_ATTR(_name, _mode) \
2928static struct target_core_hba_attribute \
2929 target_core_hba_##_name = \
2930 __CONFIGFS_EATTR(_name, _mode, \
2931 target_core_hba_show_attr_##_name, \
2932 target_core_hba_store_attr_##_name);
2933
2934#define SE_HBA_ATTR_RO(_name) \
2935static struct target_core_hba_attribute \
2936 target_core_hba_##_name = \
2937 __CONFIGFS_EATTR_RO(_name, \
2938 target_core_hba_show_attr_##_name);
2939
2940static ssize_t target_core_hba_show_attr_hba_info(
2941 struct se_hba *hba,
2942 char *page)
2943{
2944 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2945 hba->hba_id, hba->transport->name,
2946 TARGET_CORE_CONFIGFS_VERSION);
2947}
2948
2949SE_HBA_ATTR_RO(hba_info);
2950
2951static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2952 char *page)
2953{
2954 int hba_mode = 0;
2955
2956 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2957 hba_mode = 1;
2958
2959 return sprintf(page, "%d\n", hba_mode);
2960}
2961
2962static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2963 const char *page, size_t count)
2964{
2965 struct se_subsystem_api *transport = hba->transport;
2966 unsigned long mode_flag;
2967 int ret;
2968
2969 if (transport->pmode_enable_hba == NULL)
2970 return -EINVAL;
2971
Jingoo Han57103d72013-07-19 16:22:19 +09002972 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002973 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002974 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002975 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002976 }
2977
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002978 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07002979 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002980 return -EINVAL;
2981 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002982
2983 ret = transport->pmode_enable_hba(hba, mode_flag);
2984 if (ret < 0)
2985 return -EINVAL;
2986 if (ret > 0)
2987 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2988 else if (ret == 0)
2989 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2990
2991 return count;
2992}
2993
2994SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2995
2996CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2997
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002998static void target_core_hba_release(struct config_item *item)
2999{
3000 struct se_hba *hba = container_of(to_config_group(item),
3001 struct se_hba, hba_group);
3002 core_delete_hba(hba);
3003}
3004
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003005static struct configfs_attribute *target_core_hba_attrs[] = {
3006 &target_core_hba_hba_info.attr,
3007 &target_core_hba_hba_mode.attr,
3008 NULL,
3009};
3010
3011static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003012 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003013 .show_attribute = target_core_hba_attr_show,
3014 .store_attribute = target_core_hba_attr_store,
3015};
3016
3017static struct config_item_type target_core_hba_cit = {
3018 .ct_item_ops = &target_core_hba_item_ops,
3019 .ct_group_ops = &target_core_hba_group_ops,
3020 .ct_attrs = target_core_hba_attrs,
3021 .ct_owner = THIS_MODULE,
3022};
3023
3024static struct config_group *target_core_call_addhbatotarget(
3025 struct config_group *group,
3026 const char *name)
3027{
3028 char *se_plugin_str, *str, *str2;
3029 struct se_hba *hba;
3030 char buf[TARGET_CORE_NAME_MAX_LEN];
3031 unsigned long plugin_dep_id = 0;
3032 int ret;
3033
3034 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07003035 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07003036 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003037 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3038 TARGET_CORE_NAME_MAX_LEN);
3039 return ERR_PTR(-ENAMETOOLONG);
3040 }
3041 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3042
3043 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003044 if (!str) {
3045 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003046 return ERR_PTR(-EINVAL);
3047 }
3048 se_plugin_str = buf;
3049 /*
3050 * Special case for subsystem plugins that have "_" in their names.
3051 * Namely rd_direct and rd_mcp..
3052 */
3053 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003054 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003055 *str2 = '\0'; /* Terminate for *se_plugin_str */
3056 str2++; /* Skip to start of plugin dependent ID */
3057 str = str2;
3058 } else {
3059 *str = '\0'; /* Terminate for *se_plugin_str */
3060 str++; /* Skip to start of plugin dependent ID */
3061 }
3062
Jingoo Han57103d72013-07-19 16:22:19 +09003063 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003064 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09003065 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003066 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003067 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003068 }
3069 /*
3070 * Load up TCM subsystem plugins if they have not already been loaded.
3071 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07003072 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003073
3074 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3075 if (IS_ERR(hba))
3076 return ERR_CAST(hba);
3077
3078 config_group_init_type_name(&hba->hba_group, name,
3079 &target_core_hba_cit);
3080
3081 return &hba->hba_group;
3082}
3083
3084static void target_core_call_delhbafromtarget(
3085 struct config_group *group,
3086 struct config_item *item)
3087{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003088 /*
3089 * core_delete_hba() is called from target_core_hba_item_ops->release()
3090 * -> target_core_hba_release()
3091 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003092 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003093}
3094
3095static struct configfs_group_operations target_core_group_ops = {
3096 .make_group = target_core_call_addhbatotarget,
3097 .drop_item = target_core_call_delhbafromtarget,
3098};
3099
3100static struct config_item_type target_core_cit = {
3101 .ct_item_ops = NULL,
3102 .ct_group_ops = &target_core_group_ops,
3103 .ct_attrs = NULL,
3104 .ct_owner = THIS_MODULE,
3105};
3106
3107/* Stop functions for struct config_item_type target_core_hba_cit */
3108
Axel Lin54550fa2011-03-14 04:06:09 -07003109static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003110{
3111 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3112 struct config_group *lu_gp_cg = NULL;
3113 struct configfs_subsystem *subsys;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003114 struct t10_alua_lu_gp *lu_gp;
3115 int ret;
3116
Andy Grover6708bb22011-06-08 10:36:43 -07003117 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003118 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3119 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3120
3121 subsys = target_core_subsystem[0];
3122 config_group_init(&subsys->su_group);
3123 mutex_init(&subsys->su_mutex);
3124
Andy Grovere3d6f902011-07-19 08:55:10 +00003125 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003126 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003127 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003128 /*
3129 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3130 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3131 */
3132 target_cg = &subsys->su_group;
Andy Groverab6dae82013-12-09 14:27:36 -08003133 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003134 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003135 if (!target_cg->default_groups) {
3136 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003137 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003138 goto out_global;
3139 }
3140
Andy Grovere3d6f902011-07-19 08:55:10 +00003141 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003142 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003143 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003144 target_cg->default_groups[1] = NULL;
3145 /*
3146 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3147 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003148 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003149 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003150 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003151 if (!hba_cg->default_groups) {
3152 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003153 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003154 goto out_global;
3155 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003156 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003157 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003158 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003159 hba_cg->default_groups[1] = NULL;
3160 /*
3161 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3162 * groups under /sys/kernel/config/target/core/alua/
3163 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003164 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003165 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003166 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003167 if (!alua_cg->default_groups) {
3168 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003169 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003170 goto out_global;
3171 }
3172
Andy Grovere3d6f902011-07-19 08:55:10 +00003173 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003174 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003175 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003176 alua_cg->default_groups[1] = NULL;
3177 /*
3178 * Add core/alua/lu_gps/default_lu_gp
3179 */
3180 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003181 if (IS_ERR(lu_gp)) {
3182 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003183 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003184 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003185
Andy Grovere3d6f902011-07-19 08:55:10 +00003186 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003187 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003188 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003189 if (!lu_gp_cg->default_groups) {
3190 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003191 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003192 goto out_global;
3193 }
3194
3195 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3196 &target_core_alua_lu_gp_cit);
3197 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3198 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003199 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003200 /*
3201 * Register the target_core_mod subsystem with configfs.
3202 */
3203 ret = configfs_register_subsystem(subsys);
3204 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003205 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003206 ret, subsys->su_group.cg_item.ci_namebuf);
3207 goto out_global;
3208 }
Andy Grover6708bb22011-06-08 10:36:43 -07003209 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003210 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3211 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3212 /*
3213 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3214 */
3215 ret = rd_module_init();
3216 if (ret < 0)
3217 goto out;
3218
Roland Dreier0d0f9df2012-10-31 09:16:44 -07003219 ret = core_dev_setup_virtual_lun0();
3220 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003221 goto out;
3222
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003223 ret = target_xcopy_setup_pt();
3224 if (ret < 0)
3225 goto out;
3226
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003227 return 0;
3228
3229out:
3230 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003231 core_dev_release_virtual_lun0();
3232 rd_module_exit();
3233out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00003234 if (default_lu_gp) {
3235 core_alua_free_lu_gp(default_lu_gp);
3236 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003237 }
3238 if (lu_gp_cg)
3239 kfree(lu_gp_cg->default_groups);
3240 if (alua_cg)
3241 kfree(alua_cg->default_groups);
3242 if (hba_cg)
3243 kfree(hba_cg->default_groups);
3244 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00003245 release_se_kmem_caches();
3246 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003247}
3248
Axel Lin54550fa2011-03-14 04:06:09 -07003249static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003250{
3251 struct configfs_subsystem *subsys;
3252 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3253 struct config_item *item;
3254 int i;
3255
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003256 subsys = target_core_subsystem[0];
3257
Andy Grovere3d6f902011-07-19 08:55:10 +00003258 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003259 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3260 item = &lu_gp_cg->default_groups[i]->cg_item;
3261 lu_gp_cg->default_groups[i] = NULL;
3262 config_item_put(item);
3263 }
3264 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003265 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003266
Andy Grovere3d6f902011-07-19 08:55:10 +00003267 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003268 for (i = 0; alua_cg->default_groups[i]; i++) {
3269 item = &alua_cg->default_groups[i]->cg_item;
3270 alua_cg->default_groups[i] = NULL;
3271 config_item_put(item);
3272 }
3273 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003274 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003275
Andy Grovere3d6f902011-07-19 08:55:10 +00003276 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003277 for (i = 0; hba_cg->default_groups[i]; i++) {
3278 item = &hba_cg->default_groups[i]->cg_item;
3279 hba_cg->default_groups[i] = NULL;
3280 config_item_put(item);
3281 }
3282 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003283 hba_cg->default_groups = NULL;
3284 /*
3285 * We expect subsys->su_group.default_groups to be released
3286 * by configfs subsystem provider logic..
3287 */
3288 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003289 kfree(subsys->su_group.default_groups);
3290
Andy Grovere3d6f902011-07-19 08:55:10 +00003291 core_alua_free_lu_gp(default_lu_gp);
3292 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003293
Andy Grover6708bb22011-06-08 10:36:43 -07003294 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003295 " Infrastructure\n");
3296
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003297 core_dev_release_virtual_lun0();
3298 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003299 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003300 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003301}
3302
3303MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3304MODULE_AUTHOR("nab@Linux-iSCSI.org");
3305MODULE_LICENSE("GPL");
3306
3307module_init(target_core_init_configfs);
3308module_exit(target_core_exit_configfs);