blob: 41498b6966413ba5771d13b2c71d79853d8dcf75 [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);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800129
130 tf = target_core_get_fabric(name);
Andy Grover6708bb22011-06-08 10:36:43 -0700131 if (!tf) {
Roland Dreiere7b7af62014-11-14 12:54:36 -0800132 pr_err("target_core_register_fabric() trying autoload for %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800133 name);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800134
135 /*
136 * Below are some hardcoded request_module() calls to automatically
137 * local fabric modules when the following is called:
138 *
139 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
140 *
141 * Note that this does not limit which TCM fabric module can be
142 * registered, but simply provids auto loading logic for modules with
143 * mkdir(2) system calls with known TCM fabric modules.
144 */
145
146 if (!strncmp(name, "iscsi", 5)) {
147 /*
148 * Automatically load the LIO Target fabric module when the
149 * following is called:
150 *
151 * mkdir -p $CONFIGFS/target/iscsi
152 */
153 ret = request_module("iscsi_target_mod");
154 if (ret < 0) {
155 pr_err("request_module() failed for"
156 " iscsi_target_mod.ko: %d\n", ret);
157 return ERR_PTR(-EINVAL);
158 }
159 } else if (!strncmp(name, "loopback", 8)) {
160 /*
161 * Automatically load the tcm_loop fabric module when the
162 * following is called:
163 *
164 * mkdir -p $CONFIGFS/target/loopback
165 */
166 ret = request_module("tcm_loop");
167 if (ret < 0) {
168 pr_err("request_module() failed for"
169 " tcm_loop.ko: %d\n", ret);
170 return ERR_PTR(-EINVAL);
171 }
172 }
173
174 tf = target_core_get_fabric(name);
175 }
176
177 if (!tf) {
178 pr_err("target_core_get_fabric() failed for %s\n",
179 name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800180 return ERR_PTR(-EINVAL);
181 }
Andy Grover6708bb22011-06-08 10:36:43 -0700182 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800183 " %s\n", tf->tf_name);
184 /*
185 * On a successful target_core_get_fabric() look, the returned
186 * struct target_fabric_configfs *tf will contain a usage reference.
187 */
Andy Grover6708bb22011-06-08 10:36:43 -0700188 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
Andy Groverd80e224d2013-10-09 11:05:56 -0700189 &tf->tf_cit_tmpl.tfc_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800190
191 tf->tf_group.default_groups = tf->tf_default_groups;
192 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
193 tf->tf_group.default_groups[1] = NULL;
194
195 config_group_init_type_name(&tf->tf_group, name,
Andy Groverd80e224d2013-10-09 11:05:56 -0700196 &tf->tf_cit_tmpl.tfc_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800197 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
Andy Groverd80e224d2013-10-09 11:05:56 -0700198 &tf->tf_cit_tmpl.tfc_discovery_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800199
Andy Grover6708bb22011-06-08 10:36:43 -0700200 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800201 " %s\n", tf->tf_group.cg_item.ci_name);
202 /*
203 * Setup tf_ops.tf_subsys pointer for usage with configfs_depend_item()
204 */
205 tf->tf_ops.tf_subsys = tf->tf_subsys;
206 tf->tf_fabric = &tf->tf_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -0700207 pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800208 " for %s\n", name);
209
210 return &tf->tf_group;
211}
212
213/*
214 * Called from struct target_core_group_ops->drop_item()
215 */
216static void target_core_deregister_fabric(
217 struct config_group *group,
218 struct config_item *item)
219{
220 struct target_fabric_configfs *tf = container_of(
221 to_config_group(item), struct target_fabric_configfs, tf_group);
222 struct config_group *tf_group;
223 struct config_item *df_item;
224 int i;
225
Andy Grover6708bb22011-06-08 10:36:43 -0700226 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800227 " tf list\n", config_item_name(item));
228
Andy Grover6708bb22011-06-08 10:36:43 -0700229 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800230 " %s\n", tf->tf_name);
231 atomic_dec(&tf->tf_access_cnt);
232
Andy Grover6708bb22011-06-08 10:36:43 -0700233 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800234 " tf->tf_fabric for %s\n", tf->tf_name);
235 tf->tf_fabric = NULL;
236
Andy Grover6708bb22011-06-08 10:36:43 -0700237 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800238 " %s\n", config_item_name(item));
239
240 tf_group = &tf->tf_group;
241 for (i = 0; tf_group->default_groups[i]; i++) {
242 df_item = &tf_group->default_groups[i]->cg_item;
243 tf_group->default_groups[i] = NULL;
244 config_item_put(df_item);
245 }
246 config_item_put(item);
247}
248
249static struct configfs_group_operations target_core_fabric_group_ops = {
250 .make_group = &target_core_register_fabric,
251 .drop_item = &target_core_deregister_fabric,
252};
253
254/*
255 * All item attributes appearing in /sys/kernel/target/ appear here.
256 */
257static struct configfs_attribute *target_core_fabric_item_attrs[] = {
258 &target_core_item_attr_version,
259 NULL,
260};
261
262/*
263 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
264 */
265static struct config_item_type target_core_fabrics_item = {
266 .ct_item_ops = &target_core_fabric_item_ops,
267 .ct_group_ops = &target_core_fabric_group_ops,
268 .ct_attrs = target_core_fabric_item_attrs,
269 .ct_owner = THIS_MODULE,
270};
271
272static struct configfs_subsystem target_core_fabrics = {
273 .su_group = {
274 .cg_item = {
275 .ci_namebuf = "target",
276 .ci_type = &target_core_fabrics_item,
277 },
278 },
279};
280
Nicholas Bellinger56d128f2013-08-22 11:32:31 -0700281struct configfs_subsystem *target_core_subsystem[] = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800282 &target_core_fabrics,
283 NULL,
284};
285
286/*##############################################################################
287// Start functions called by external Target Fabrics Modules
288//############################################################################*/
289
290/*
291 * First function called by fabric modules to:
292 *
293 * 1) Allocate a struct target_fabric_configfs and save the *fabric_cit pointer.
294 * 2) Add struct target_fabric_configfs to g_tf_list
295 * 3) Return struct target_fabric_configfs to fabric module to be passed
296 * into target_fabric_configfs_register().
297 */
298struct target_fabric_configfs *target_fabric_configfs_init(
299 struct module *fabric_mod,
300 const char *name)
301{
302 struct target_fabric_configfs *tf;
303
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800304 if (!(name)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700305 pr_err("Unable to locate passed fabric name\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000306 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800307 }
Dan Carpenter60d645a2011-06-15 10:03:05 -0700308 if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700309 pr_err("Passed name: %s exceeds TARGET_FABRIC"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800310 "_NAME_SIZE\n", name);
Andy Grovere3d6f902011-07-19 08:55:10 +0000311 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800312 }
313
314 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700315 if (!tf)
Andy Grovere3d6f902011-07-19 08:55:10 +0000316 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800317
318 INIT_LIST_HEAD(&tf->tf_list);
319 atomic_set(&tf->tf_access_cnt, 0);
320 /*
321 * Setup the default generic struct config_item_type's (cits) in
322 * struct target_fabric_configfs->tf_cit_tmpl
323 */
324 tf->tf_module = fabric_mod;
325 target_fabric_setup_cits(tf);
326
327 tf->tf_subsys = target_core_subsystem[0];
328 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
329
330 mutex_lock(&g_tf_lock);
331 list_add_tail(&tf->tf_list, &g_tf_list);
332 mutex_unlock(&g_tf_lock);
333
Andy Grover6708bb22011-06-08 10:36:43 -0700334 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800335 ">>>>>>>>>>>>>>\n");
Andy Grover6708bb22011-06-08 10:36:43 -0700336 pr_debug("Initialized struct target_fabric_configfs: %p for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800337 " %s\n", tf, tf->tf_name);
338 return tf;
339}
340EXPORT_SYMBOL(target_fabric_configfs_init);
341
342/*
343 * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
344 */
345void target_fabric_configfs_free(
346 struct target_fabric_configfs *tf)
347{
348 mutex_lock(&g_tf_lock);
349 list_del(&tf->tf_list);
350 mutex_unlock(&g_tf_lock);
351
352 kfree(tf);
353}
354EXPORT_SYMBOL(target_fabric_configfs_free);
355
356/*
357 * Perform a sanity check of the passed tf->tf_ops before completing
358 * TCM fabric module registration.
359 */
360static int target_fabric_tf_ops_check(
361 struct target_fabric_configfs *tf)
362{
363 struct target_core_fabric_ops *tfo = &tf->tf_ops;
364
Andy Grover6708bb22011-06-08 10:36:43 -0700365 if (!tfo->get_fabric_name) {
366 pr_err("Missing tfo->get_fabric_name()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800367 return -EINVAL;
368 }
Andy Grover6708bb22011-06-08 10:36:43 -0700369 if (!tfo->get_fabric_proto_ident) {
370 pr_err("Missing tfo->get_fabric_proto_ident()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800371 return -EINVAL;
372 }
Andy Grover6708bb22011-06-08 10:36:43 -0700373 if (!tfo->tpg_get_wwn) {
374 pr_err("Missing tfo->tpg_get_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800375 return -EINVAL;
376 }
Andy Grover6708bb22011-06-08 10:36:43 -0700377 if (!tfo->tpg_get_tag) {
378 pr_err("Missing tfo->tpg_get_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800379 return -EINVAL;
380 }
Andy Grover6708bb22011-06-08 10:36:43 -0700381 if (!tfo->tpg_get_default_depth) {
382 pr_err("Missing tfo->tpg_get_default_depth()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800383 return -EINVAL;
384 }
Andy Grover6708bb22011-06-08 10:36:43 -0700385 if (!tfo->tpg_get_pr_transport_id) {
386 pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800387 return -EINVAL;
388 }
Andy Grover6708bb22011-06-08 10:36:43 -0700389 if (!tfo->tpg_get_pr_transport_id_len) {
390 pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800391 return -EINVAL;
392 }
Andy Grover6708bb22011-06-08 10:36:43 -0700393 if (!tfo->tpg_check_demo_mode) {
394 pr_err("Missing tfo->tpg_check_demo_mode()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800395 return -EINVAL;
396 }
Andy Grover6708bb22011-06-08 10:36:43 -0700397 if (!tfo->tpg_check_demo_mode_cache) {
398 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800399 return -EINVAL;
400 }
Andy Grover6708bb22011-06-08 10:36:43 -0700401 if (!tfo->tpg_check_demo_mode_write_protect) {
402 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800403 return -EINVAL;
404 }
Andy Grover6708bb22011-06-08 10:36:43 -0700405 if (!tfo->tpg_check_prod_mode_write_protect) {
406 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800407 return -EINVAL;
408 }
Andy Grover6708bb22011-06-08 10:36:43 -0700409 if (!tfo->tpg_alloc_fabric_acl) {
410 pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800411 return -EINVAL;
412 }
Andy Grover6708bb22011-06-08 10:36:43 -0700413 if (!tfo->tpg_release_fabric_acl) {
414 pr_err("Missing tfo->tpg_release_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800415 return -EINVAL;
416 }
Andy Grover6708bb22011-06-08 10:36:43 -0700417 if (!tfo->tpg_get_inst_index) {
418 pr_err("Missing tfo->tpg_get_inst_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800419 return -EINVAL;
420 }
Christoph Hellwig35462972011-05-31 23:56:57 -0400421 if (!tfo->release_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700422 pr_err("Missing tfo->release_cmd()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800423 return -EINVAL;
424 }
Andy Grover6708bb22011-06-08 10:36:43 -0700425 if (!tfo->shutdown_session) {
426 pr_err("Missing tfo->shutdown_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800427 return -EINVAL;
428 }
Andy Grover6708bb22011-06-08 10:36:43 -0700429 if (!tfo->close_session) {
430 pr_err("Missing tfo->close_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800431 return -EINVAL;
432 }
Andy Grover6708bb22011-06-08 10:36:43 -0700433 if (!tfo->sess_get_index) {
434 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800435 return -EINVAL;
436 }
Andy Grover6708bb22011-06-08 10:36:43 -0700437 if (!tfo->write_pending) {
438 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800439 return -EINVAL;
440 }
Andy Grover6708bb22011-06-08 10:36:43 -0700441 if (!tfo->write_pending_status) {
442 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800443 return -EINVAL;
444 }
Andy Grover6708bb22011-06-08 10:36:43 -0700445 if (!tfo->set_default_node_attributes) {
446 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800447 return -EINVAL;
448 }
Andy Grover6708bb22011-06-08 10:36:43 -0700449 if (!tfo->get_task_tag) {
450 pr_err("Missing tfo->get_task_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800451 return -EINVAL;
452 }
Andy Grover6708bb22011-06-08 10:36:43 -0700453 if (!tfo->get_cmd_state) {
454 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800455 return -EINVAL;
456 }
Andy Grover6708bb22011-06-08 10:36:43 -0700457 if (!tfo->queue_data_in) {
458 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800459 return -EINVAL;
460 }
Andy Grover6708bb22011-06-08 10:36:43 -0700461 if (!tfo->queue_status) {
462 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800463 return -EINVAL;
464 }
Andy Grover6708bb22011-06-08 10:36:43 -0700465 if (!tfo->queue_tm_rsp) {
466 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800467 return -EINVAL;
468 }
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700469 if (!tfo->aborted_task) {
470 pr_err("Missing tfo->aborted_task()\n");
471 return -EINVAL;
472 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800473 /*
474 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
475 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
476 * target_core_fabric_configfs.c WWN+TPG group context code.
477 */
Andy Grover6708bb22011-06-08 10:36:43 -0700478 if (!tfo->fabric_make_wwn) {
479 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800480 return -EINVAL;
481 }
Andy Grover6708bb22011-06-08 10:36:43 -0700482 if (!tfo->fabric_drop_wwn) {
483 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800484 return -EINVAL;
485 }
Andy Grover6708bb22011-06-08 10:36:43 -0700486 if (!tfo->fabric_make_tpg) {
487 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800488 return -EINVAL;
489 }
Andy Grover6708bb22011-06-08 10:36:43 -0700490 if (!tfo->fabric_drop_tpg) {
491 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800492 return -EINVAL;
493 }
494
495 return 0;
496}
497
498/*
499 * Called 2nd from fabric module with returned parameter of
500 * struct target_fabric_configfs * from target_fabric_configfs_init().
501 *
502 * Upon a successful registration, the new fabric's struct config_item is
503 * return. Also, a pointer to this struct is set in the passed
504 * struct target_fabric_configfs.
505 */
506int target_fabric_configfs_register(
507 struct target_fabric_configfs *tf)
508{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800509 int ret;
510
Andy Grover6708bb22011-06-08 10:36:43 -0700511 if (!tf) {
512 pr_err("Unable to locate target_fabric_configfs"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800513 " pointer\n");
514 return -EINVAL;
515 }
Andy Grover6708bb22011-06-08 10:36:43 -0700516 if (!tf->tf_subsys) {
517 pr_err("Unable to target struct config_subsystem"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800518 " pointer\n");
519 return -EINVAL;
520 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800521 ret = target_fabric_tf_ops_check(tf);
522 if (ret < 0)
523 return ret;
524
Andy Grover6708bb22011-06-08 10:36:43 -0700525 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800526 ">>>>>>>>>>\n");
527 return 0;
528}
529EXPORT_SYMBOL(target_fabric_configfs_register);
530
531void target_fabric_configfs_deregister(
532 struct target_fabric_configfs *tf)
533{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800534 struct configfs_subsystem *su;
535
Andy Grover6708bb22011-06-08 10:36:43 -0700536 if (!tf) {
537 pr_err("Unable to locate passed target_fabric_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800538 "configfs\n");
539 return;
540 }
541 su = tf->tf_subsys;
Andy Grover6708bb22011-06-08 10:36:43 -0700542 if (!su) {
543 pr_err("Unable to locate passed tf->tf_subsys"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800544 " pointer\n");
545 return;
546 }
Andy Grover6708bb22011-06-08 10:36:43 -0700547 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800548 ">>>>>>>>>>>>\n");
549 mutex_lock(&g_tf_lock);
550 if (atomic_read(&tf->tf_access_cnt)) {
551 mutex_unlock(&g_tf_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700552 pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800553 tf->tf_name);
554 BUG();
555 }
556 list_del(&tf->tf_list);
557 mutex_unlock(&g_tf_lock);
558
Andy Grover6708bb22011-06-08 10:36:43 -0700559 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800560 " %s\n", tf->tf_name);
561 tf->tf_module = NULL;
562 tf->tf_subsys = NULL;
563 kfree(tf);
564
Andy Grover6708bb22011-06-08 10:36:43 -0700565 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800566 ">>>>>\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800567}
568EXPORT_SYMBOL(target_fabric_configfs_deregister);
569
570/*##############################################################################
571// Stop functions called by external Target Fabrics Modules
572//############################################################################*/
573
574/* Start functions for struct config_item_type target_core_dev_attrib_cit */
575
576#define DEF_DEV_ATTRIB_SHOW(_name) \
577static ssize_t target_core_dev_show_attr_##_name( \
578 struct se_dev_attrib *da, \
579 char *page) \
580{ \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400581 return snprintf(page, PAGE_SIZE, "%u\n", \
582 (u32)da->da_dev->dev_attrib._name); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800583}
584
585#define DEF_DEV_ATTRIB_STORE(_name) \
586static ssize_t target_core_dev_store_attr_##_name( \
587 struct se_dev_attrib *da, \
588 const char *page, \
589 size_t count) \
590{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800591 unsigned long val; \
592 int ret; \
593 \
Jingoo Han57103d72013-07-19 16:22:19 +0900594 ret = kstrtoul(page, 0, &val); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800595 if (ret < 0) { \
Jingoo Han57103d72013-07-19 16:22:19 +0900596 pr_err("kstrtoul() failed with" \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800597 " ret: %d\n", ret); \
598 return -EINVAL; \
599 } \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400600 ret = se_dev_set_##_name(da->da_dev, (u32)val); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800601 \
602 return (!ret) ? count : -EINVAL; \
603}
604
605#define DEF_DEV_ATTRIB(_name) \
606DEF_DEV_ATTRIB_SHOW(_name); \
607DEF_DEV_ATTRIB_STORE(_name);
608
609#define DEF_DEV_ATTRIB_RO(_name) \
610DEF_DEV_ATTRIB_SHOW(_name);
611
612CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
613#define SE_DEV_ATTR(_name, _mode) \
614static struct target_core_dev_attrib_attribute \
615 target_core_dev_attrib_##_name = \
616 __CONFIGFS_EATTR(_name, _mode, \
617 target_core_dev_show_attr_##_name, \
618 target_core_dev_store_attr_##_name);
619
620#define SE_DEV_ATTR_RO(_name); \
621static struct target_core_dev_attrib_attribute \
622 target_core_dev_attrib_##_name = \
623 __CONFIGFS_EATTR_RO(_name, \
624 target_core_dev_show_attr_##_name);
625
Tregaron Baylyadfa9572013-01-31 15:30:24 -0700626DEF_DEV_ATTRIB(emulate_model_alias);
627SE_DEV_ATTR(emulate_model_alias, S_IRUGO | S_IWUSR);
628
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800629DEF_DEV_ATTRIB(emulate_dpo);
630SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
631
632DEF_DEV_ATTRIB(emulate_fua_write);
633SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
634
635DEF_DEV_ATTRIB(emulate_fua_read);
636SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
637
638DEF_DEV_ATTRIB(emulate_write_cache);
639SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
640
641DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
642SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
643
644DEF_DEV_ATTRIB(emulate_tas);
645SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
646
647DEF_DEV_ATTRIB(emulate_tpu);
648SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
649
650DEF_DEV_ATTRIB(emulate_tpws);
651SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
652
Nicholas Bellinger0123a9e2013-08-20 14:24:09 -0700653DEF_DEV_ATTRIB(emulate_caw);
654SE_DEV_ATTR(emulate_caw, S_IRUGO | S_IWUSR);
655
Nicholas Bellingerd397a442013-08-22 14:17:20 -0700656DEF_DEV_ATTRIB(emulate_3pc);
657SE_DEV_ATTR(emulate_3pc, S_IRUGO | S_IWUSR);
658
Nicholas Bellinger2ed22c92014-01-08 18:19:31 +0000659DEF_DEV_ATTRIB(pi_prot_type);
660SE_DEV_ATTR(pi_prot_type, S_IRUGO | S_IWUSR);
661
662DEF_DEV_ATTRIB_RO(hw_pi_prot_type);
663SE_DEV_ATTR_RO(hw_pi_prot_type);
664
665DEF_DEV_ATTRIB(pi_prot_format);
666SE_DEV_ATTR(pi_prot_format, S_IRUGO | S_IWUSR);
667
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800668DEF_DEV_ATTRIB(enforce_pr_isids);
669SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
670
Roland Dreiere22a7f02011-07-05 13:34:52 -0700671DEF_DEV_ATTRIB(is_nonrot);
672SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
673
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700674DEF_DEV_ATTRIB(emulate_rest_reord);
675SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
676
Nicholas Bellinger92404e62014-10-04 01:06:08 +0000677DEF_DEV_ATTRIB(force_pr_aptpl);
678SE_DEV_ATTR(force_pr_aptpl, S_IRUGO | S_IWUSR);
679
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800680DEF_DEV_ATTRIB_RO(hw_block_size);
681SE_DEV_ATTR_RO(hw_block_size);
682
683DEF_DEV_ATTRIB(block_size);
684SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
685
686DEF_DEV_ATTRIB_RO(hw_max_sectors);
687SE_DEV_ATTR_RO(hw_max_sectors);
688
Roland Dreier015487b2012-02-13 16:18:17 -0800689DEF_DEV_ATTRIB(fabric_max_sectors);
690SE_DEV_ATTR(fabric_max_sectors, S_IRUGO | S_IWUSR);
691
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800692DEF_DEV_ATTRIB(optimal_sectors);
693SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
694
695DEF_DEV_ATTRIB_RO(hw_queue_depth);
696SE_DEV_ATTR_RO(hw_queue_depth);
697
698DEF_DEV_ATTRIB(queue_depth);
699SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
700
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800701DEF_DEV_ATTRIB(max_unmap_lba_count);
702SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
703
704DEF_DEV_ATTRIB(max_unmap_block_desc_count);
705SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
706
707DEF_DEV_ATTRIB(unmap_granularity);
708SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
709
710DEF_DEV_ATTRIB(unmap_granularity_alignment);
711SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
712
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800713DEF_DEV_ATTRIB(max_write_same_len);
714SE_DEV_ATTR(max_write_same_len, S_IRUGO | S_IWUSR);
715
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800716CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
717
718static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
Tregaron Baylyadfa9572013-01-31 15:30:24 -0700719 &target_core_dev_attrib_emulate_model_alias.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800720 &target_core_dev_attrib_emulate_dpo.attr,
721 &target_core_dev_attrib_emulate_fua_write.attr,
722 &target_core_dev_attrib_emulate_fua_read.attr,
723 &target_core_dev_attrib_emulate_write_cache.attr,
724 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
725 &target_core_dev_attrib_emulate_tas.attr,
726 &target_core_dev_attrib_emulate_tpu.attr,
727 &target_core_dev_attrib_emulate_tpws.attr,
Nicholas Bellinger0123a9e2013-08-20 14:24:09 -0700728 &target_core_dev_attrib_emulate_caw.attr,
Nicholas Bellingerd397a442013-08-22 14:17:20 -0700729 &target_core_dev_attrib_emulate_3pc.attr,
Nicholas Bellinger2ed22c92014-01-08 18:19:31 +0000730 &target_core_dev_attrib_pi_prot_type.attr,
731 &target_core_dev_attrib_hw_pi_prot_type.attr,
732 &target_core_dev_attrib_pi_prot_format.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800733 &target_core_dev_attrib_enforce_pr_isids.attr,
Nicholas Bellinger92404e62014-10-04 01:06:08 +0000734 &target_core_dev_attrib_force_pr_aptpl.attr,
Roland Dreiere22a7f02011-07-05 13:34:52 -0700735 &target_core_dev_attrib_is_nonrot.attr,
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700736 &target_core_dev_attrib_emulate_rest_reord.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800737 &target_core_dev_attrib_hw_block_size.attr,
738 &target_core_dev_attrib_block_size.attr,
739 &target_core_dev_attrib_hw_max_sectors.attr,
Roland Dreier015487b2012-02-13 16:18:17 -0800740 &target_core_dev_attrib_fabric_max_sectors.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800741 &target_core_dev_attrib_optimal_sectors.attr,
742 &target_core_dev_attrib_hw_queue_depth.attr,
743 &target_core_dev_attrib_queue_depth.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800744 &target_core_dev_attrib_max_unmap_lba_count.attr,
745 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
746 &target_core_dev_attrib_unmap_granularity.attr,
747 &target_core_dev_attrib_unmap_granularity_alignment.attr,
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800748 &target_core_dev_attrib_max_write_same_len.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800749 NULL,
750};
751
752static struct configfs_item_operations target_core_dev_attrib_ops = {
753 .show_attribute = target_core_dev_attrib_attr_show,
754 .store_attribute = target_core_dev_attrib_attr_store,
755};
756
757static struct config_item_type target_core_dev_attrib_cit = {
758 .ct_item_ops = &target_core_dev_attrib_ops,
759 .ct_attrs = target_core_dev_attrib_attrs,
760 .ct_owner = THIS_MODULE,
761};
762
763/* End functions for struct config_item_type target_core_dev_attrib_cit */
764
765/* Start functions for struct config_item_type target_core_dev_wwn_cit */
766
767CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
768#define SE_DEV_WWN_ATTR(_name, _mode) \
769static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
770 __CONFIGFS_EATTR(_name, _mode, \
771 target_core_dev_wwn_show_attr_##_name, \
772 target_core_dev_wwn_store_attr_##_name);
773
774#define SE_DEV_WWN_ATTR_RO(_name); \
775do { \
776 static struct target_core_dev_wwn_attribute \
777 target_core_dev_wwn_##_name = \
778 __CONFIGFS_EATTR_RO(_name, \
779 target_core_dev_wwn_show_attr_##_name); \
780} while (0);
781
782/*
783 * VPD page 0x80 Unit serial
784 */
785static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
786 struct t10_wwn *t10_wwn,
787 char *page)
788{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800789 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
790 &t10_wwn->unit_serial[0]);
791}
792
793static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
794 struct t10_wwn *t10_wwn,
795 const char *page,
796 size_t count)
797{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400798 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800799 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
800
801 /*
802 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
803 * from the struct scsi_device level firmware, do not allow
804 * VPD Unit Serial to be emulated.
805 *
806 * Note this struct scsi_device could also be emulating VPD
807 * information from its drivers/scsi LLD. But for now we assume
808 * it is doing 'the right thing' wrt a world wide unique
809 * VPD Unit Serial Number that OS dependent multipath can depend on.
810 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400811 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700812 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800813 " Unit Serial, ignoring request\n");
814 return -EOPNOTSUPP;
815 }
816
Dan Carpenter60d645a2011-06-15 10:03:05 -0700817 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -0700818 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800819 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
820 return -EOVERFLOW;
821 }
822 /*
823 * Check to see if any active $FABRIC_MOD exports exist. If they
824 * do exist, fail here as changing this information on the fly
825 * (underneath the initiator side OS dependent multipath code)
826 * could cause negative effects.
827 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400828 if (dev->export_count) {
829 pr_err("Unable to set VPD Unit Serial while"
830 " active %d $FABRIC_MOD exports exist\n",
831 dev->export_count);
832 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800833 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400834
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800835 /*
836 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
837 *
838 * Also, strip any newline added from the userspace
839 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
840 */
841 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
842 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400843 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800844 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400845 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800846
Andy Grover6708bb22011-06-08 10:36:43 -0700847 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400848 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800849
850 return count;
851}
852
853SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
854
855/*
856 * VPD page 0x83 Protocol Identifier
857 */
858static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
859 struct t10_wwn *t10_wwn,
860 char *page)
861{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800862 struct t10_vpd *vpd;
863 unsigned char buf[VPD_TMP_BUF_SIZE];
864 ssize_t len = 0;
865
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800866 memset(buf, 0, VPD_TMP_BUF_SIZE);
867
868 spin_lock(&t10_wwn->t10_vpd_lock);
869 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700870 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800871 continue;
872
873 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
874
Andy Grover6708bb22011-06-08 10:36:43 -0700875 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800876 break;
877
878 len += sprintf(page+len, "%s", buf);
879 }
880 spin_unlock(&t10_wwn->t10_vpd_lock);
881
882 return len;
883}
884
885static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
886 struct t10_wwn *t10_wwn,
887 const char *page,
888 size_t count)
889{
890 return -ENOSYS;
891}
892
893SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
894
895/*
896 * Generic wrapper for dumping VPD identifiers by association.
897 */
898#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
899static ssize_t target_core_dev_wwn_show_attr_##_name( \
900 struct t10_wwn *t10_wwn, \
901 char *page) \
902{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800903 struct t10_vpd *vpd; \
904 unsigned char buf[VPD_TMP_BUF_SIZE]; \
905 ssize_t len = 0; \
906 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800907 spin_lock(&t10_wwn->t10_vpd_lock); \
908 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
909 if (vpd->association != _assoc) \
910 continue; \
911 \
912 memset(buf, 0, VPD_TMP_BUF_SIZE); \
913 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700914 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800915 break; \
916 len += sprintf(page+len, "%s", buf); \
917 \
918 memset(buf, 0, VPD_TMP_BUF_SIZE); \
919 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700920 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800921 break; \
922 len += sprintf(page+len, "%s", buf); \
923 \
924 memset(buf, 0, VPD_TMP_BUF_SIZE); \
925 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700926 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800927 break; \
928 len += sprintf(page+len, "%s", buf); \
929 } \
930 spin_unlock(&t10_wwn->t10_vpd_lock); \
931 \
932 return len; \
933}
934
935/*
Andy Shevchenko163cd5f2011-07-18 22:17:43 -0700936 * VPD page 0x83 Association: Logical Unit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800937 */
938DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
939
940static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
941 struct t10_wwn *t10_wwn,
942 const char *page,
943 size_t count)
944{
945 return -ENOSYS;
946}
947
948SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
949
950/*
951 * VPD page 0x83 Association: Target Port
952 */
953DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
954
955static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
956 struct t10_wwn *t10_wwn,
957 const char *page,
958 size_t count)
959{
960 return -ENOSYS;
961}
962
963SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
964
965/*
966 * VPD page 0x83 Association: SCSI Target Device
967 */
968DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
969
970static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
971 struct t10_wwn *t10_wwn,
972 const char *page,
973 size_t count)
974{
975 return -ENOSYS;
976}
977
978SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
979
980CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
981
982static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
983 &target_core_dev_wwn_vpd_unit_serial.attr,
984 &target_core_dev_wwn_vpd_protocol_identifier.attr,
985 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
986 &target_core_dev_wwn_vpd_assoc_target_port.attr,
987 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
988 NULL,
989};
990
991static struct configfs_item_operations target_core_dev_wwn_ops = {
992 .show_attribute = target_core_dev_wwn_attr_show,
993 .store_attribute = target_core_dev_wwn_attr_store,
994};
995
996static struct config_item_type target_core_dev_wwn_cit = {
997 .ct_item_ops = &target_core_dev_wwn_ops,
998 .ct_attrs = target_core_dev_wwn_attrs,
999 .ct_owner = THIS_MODULE,
1000};
1001
1002/* End functions for struct config_item_type target_core_dev_wwn_cit */
1003
1004/* Start functions for struct config_item_type target_core_dev_pr_cit */
1005
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001006CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001007#define SE_DEV_PR_ATTR(_name, _mode) \
1008static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1009 __CONFIGFS_EATTR(_name, _mode, \
1010 target_core_dev_pr_show_attr_##_name, \
1011 target_core_dev_pr_store_attr_##_name);
1012
1013#define SE_DEV_PR_ATTR_RO(_name); \
1014static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1015 __CONFIGFS_EATTR_RO(_name, \
1016 target_core_dev_pr_show_attr_##_name);
1017
Christoph Hellwigd977f432012-10-10 17:37:15 -04001018static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1019 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001020{
1021 struct se_node_acl *se_nacl;
1022 struct t10_pr_registration *pr_reg;
1023 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001024
1025 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1026
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001027 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001028 if (!pr_reg)
1029 return sprintf(page, "No SPC-3 Reservation holder\n");
1030
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001031 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -07001032 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001033
Christoph Hellwigd977f432012-10-10 17:37:15 -04001034 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001035 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001036 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001037}
1038
Christoph Hellwigd977f432012-10-10 17:37:15 -04001039static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1040 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001041{
1042 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001043 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001044
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001045 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001046 if (se_nacl) {
1047 len = sprintf(page,
1048 "SPC-2 Reservation: %s Initiator: %s\n",
1049 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1050 se_nacl->initiatorname);
1051 } else {
1052 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001053 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001054 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001055}
1056
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001057static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
1058 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001060 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001061
Christoph Hellwigd977f432012-10-10 17:37:15 -04001062 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1063 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001064
Christoph Hellwigd977f432012-10-10 17:37:15 -04001065 spin_lock(&dev->dev_reservation_lock);
1066 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1067 ret = target_core_dev_pr_show_spc2_res(dev, page);
1068 else
1069 ret = target_core_dev_pr_show_spc3_res(dev, page);
1070 spin_unlock(&dev->dev_reservation_lock);
1071 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001072}
1073
1074SE_DEV_PR_ATTR_RO(res_holder);
1075
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001076static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001077 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001078{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001079 ssize_t len = 0;
1080
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001081 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001082 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001083 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001084 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001085 len = sprintf(page, "SPC-3 Reservation: All Target"
1086 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001087 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001088 len = sprintf(page, "SPC-3 Reservation: Single"
1089 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001090 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001091
Christoph Hellwigd977f432012-10-10 17:37:15 -04001092 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001093 return len;
1094}
1095
1096SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1097
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001098static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001099 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001100{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001101 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001102}
1103
1104SE_DEV_PR_ATTR_RO(res_pr_generation);
1105
1106/*
1107 * res_pr_holder_tg_port
1108 */
1109static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001110 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001111{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001112 struct se_node_acl *se_nacl;
1113 struct se_lun *lun;
1114 struct se_portal_group *se_tpg;
1115 struct t10_pr_registration *pr_reg;
1116 struct target_core_fabric_ops *tfo;
1117 ssize_t len = 0;
1118
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001119 spin_lock(&dev->dev_reservation_lock);
1120 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001121 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001122 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001123 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001124 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001125
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001126 se_nacl = pr_reg->pr_reg_nacl;
1127 se_tpg = se_nacl->se_tpg;
1128 lun = pr_reg->pr_reg_tg_pt_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +00001129 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001130
1131 len += sprintf(page+len, "SPC-3 Reservation: %s"
1132 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1133 tfo->tpg_get_wwn(se_tpg));
1134 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001135 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001136 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1137 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1138 tfo->get_fabric_name(), lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001139
Christoph Hellwigd977f432012-10-10 17:37:15 -04001140out_unlock:
1141 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001142 return len;
1143}
1144
1145SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1146
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001147static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001148 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001149{
1150 struct target_core_fabric_ops *tfo;
1151 struct t10_pr_registration *pr_reg;
1152 unsigned char buf[384];
1153 char i_buf[PR_REG_ISID_ID_LEN];
1154 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001155 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001156
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001157 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1158
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001159 spin_lock(&dev->t10_pr.registration_lock);
1160 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001161 pr_reg_list) {
1162
1163 memset(buf, 0, 384);
1164 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1165 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -07001166 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001167 PR_REG_ISID_ID_LEN);
1168 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1169 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001170 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001171 pr_reg->pr_res_generation);
1172
Andy Grover6708bb22011-06-08 10:36:43 -07001173 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001174 break;
1175
1176 len += sprintf(page+len, "%s", buf);
1177 reg_count++;
1178 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001179 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001180
Andy Grover6708bb22011-06-08 10:36:43 -07001181 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001182 len += sprintf(page+len, "None\n");
1183
1184 return len;
1185}
1186
1187SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1188
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001189static ssize_t target_core_dev_pr_show_attr_res_pr_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001190 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001191{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001192 struct t10_pr_registration *pr_reg;
1193 ssize_t len = 0;
1194
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001195 spin_lock(&dev->dev_reservation_lock);
1196 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001197 if (pr_reg) {
1198 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1199 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1200 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001201 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001202 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001203
Christoph Hellwigd977f432012-10-10 17:37:15 -04001204 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001205 return len;
1206}
1207
1208SE_DEV_PR_ATTR_RO(res_pr_type);
1209
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001210static ssize_t target_core_dev_pr_show_attr_res_type(
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)
1214 return sprintf(page, "SPC_PASSTHROUGH\n");
1215 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1216 return sprintf(page, "SPC2_RESERVATIONS\n");
1217 else
1218 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001219}
1220
1221SE_DEV_PR_ATTR_RO(res_type);
1222
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001223static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001224 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001225{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001226 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227 return 0;
1228
1229 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001230 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001231}
1232
1233SE_DEV_PR_ATTR_RO(res_aptpl_active);
1234
1235/*
1236 * res_aptpl_metadata
1237 */
1238static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001239 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001240{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001241 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001242 return 0;
1243
1244 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1245}
1246
1247enum {
1248 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1249 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1250 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1251 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1252};
1253
1254static match_table_t tokens = {
1255 {Opt_initiator_fabric, "initiator_fabric=%s"},
1256 {Opt_initiator_node, "initiator_node=%s"},
1257 {Opt_initiator_sid, "initiator_sid=%s"},
1258 {Opt_sa_res_key, "sa_res_key=%s"},
1259 {Opt_res_holder, "res_holder=%d"},
1260 {Opt_res_type, "res_type=%d"},
1261 {Opt_res_scope, "res_scope=%d"},
1262 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1263 {Opt_mapped_lun, "mapped_lun=%d"},
1264 {Opt_target_fabric, "target_fabric=%s"},
1265 {Opt_target_node, "target_node=%s"},
1266 {Opt_tpgt, "tpgt=%d"},
1267 {Opt_port_rtpi, "port_rtpi=%d"},
1268 {Opt_target_lun, "target_lun=%d"},
1269 {Opt_err, NULL}
1270};
1271
1272static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001273 struct se_device *dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001274 const char *page,
1275 size_t count)
1276{
Jesper Juhl6d180252011-03-14 04:05:56 -07001277 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1278 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -04001279 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001280 substring_t args[MAX_OPT_ARGS];
1281 unsigned long long tmp_ll;
1282 u64 sa_res_key = 0;
1283 u32 mapped_lun = 0, target_lun = 0;
1284 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1285 u16 port_rpti = 0, tpgt = 0;
1286 u8 type = 0, scope;
1287
Christoph Hellwigd977f432012-10-10 17:37:15 -04001288 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1289 return 0;
1290 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001291 return 0;
1292
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001293 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001294 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001295 " active fabric exports exist\n");
1296 return -EINVAL;
1297 }
1298
1299 opts = kstrdup(page, GFP_KERNEL);
1300 if (!opts)
1301 return -ENOMEM;
1302
1303 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001304 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001305 if (!*ptr)
1306 continue;
1307
1308 token = match_token(ptr, tokens, args);
1309 switch (token) {
1310 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001311 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001312 if (!i_fabric) {
1313 ret = -ENOMEM;
1314 goto out;
1315 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001316 break;
1317 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001318 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001319 if (!i_port) {
1320 ret = -ENOMEM;
1321 goto out;
1322 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001323 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001324 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001325 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1326 PR_APTPL_MAX_IPORT_LEN);
1327 ret = -EINVAL;
1328 break;
1329 }
1330 break;
1331 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001332 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001333 if (!isid) {
1334 ret = -ENOMEM;
1335 goto out;
1336 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001337 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001338 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339 "= exceeds PR_REG_ISID_LEN: %d\n",
1340 PR_REG_ISID_LEN);
1341 ret = -EINVAL;
1342 break;
1343 }
1344 break;
1345 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001346 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001347 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001348 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001349 goto out;
1350 }
1351 sa_res_key = (u64)tmp_ll;
1352 break;
1353 /*
1354 * PR APTPL Metadata for Reservation
1355 */
1356 case Opt_res_holder:
1357 match_int(args, &arg);
1358 res_holder = arg;
1359 break;
1360 case Opt_res_type:
1361 match_int(args, &arg);
1362 type = (u8)arg;
1363 break;
1364 case Opt_res_scope:
1365 match_int(args, &arg);
1366 scope = (u8)arg;
1367 break;
1368 case Opt_res_all_tg_pt:
1369 match_int(args, &arg);
1370 all_tg_pt = (int)arg;
1371 break;
1372 case Opt_mapped_lun:
1373 match_int(args, &arg);
1374 mapped_lun = (u32)arg;
1375 break;
1376 /*
1377 * PR APTPL Metadata for Target Port
1378 */
1379 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001380 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001381 if (!t_fabric) {
1382 ret = -ENOMEM;
1383 goto out;
1384 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001385 break;
1386 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001387 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001388 if (!t_port) {
1389 ret = -ENOMEM;
1390 goto out;
1391 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001392 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001393 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001394 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1395 PR_APTPL_MAX_TPORT_LEN);
1396 ret = -EINVAL;
1397 break;
1398 }
1399 break;
1400 case Opt_tpgt:
1401 match_int(args, &arg);
1402 tpgt = (u16)arg;
1403 break;
1404 case Opt_port_rtpi:
1405 match_int(args, &arg);
1406 port_rpti = (u16)arg;
1407 break;
1408 case Opt_target_lun:
1409 match_int(args, &arg);
1410 target_lun = (u32)arg;
1411 break;
1412 default:
1413 break;
1414 }
1415 }
1416
Andy Grover6708bb22011-06-08 10:36:43 -07001417 if (!i_port || !t_port || !sa_res_key) {
1418 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001419 ret = -EINVAL;
1420 goto out;
1421 }
1422
1423 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001424 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001425 " holder\n", type);
1426 ret = -EINVAL;
1427 goto out;
1428 }
1429
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001430 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001431 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1432 res_holder, all_tg_pt, type);
1433out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001434 kfree(i_fabric);
1435 kfree(i_port);
1436 kfree(isid);
1437 kfree(t_fabric);
1438 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001439 kfree(orig);
1440 return (ret == 0) ? count : ret;
1441}
1442
1443SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1444
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001445CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001446
1447static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1448 &target_core_dev_pr_res_holder.attr,
1449 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1450 &target_core_dev_pr_res_pr_generation.attr,
1451 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1452 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1453 &target_core_dev_pr_res_pr_type.attr,
1454 &target_core_dev_pr_res_type.attr,
1455 &target_core_dev_pr_res_aptpl_active.attr,
1456 &target_core_dev_pr_res_aptpl_metadata.attr,
1457 NULL,
1458};
1459
1460static struct configfs_item_operations target_core_dev_pr_ops = {
1461 .show_attribute = target_core_dev_pr_attr_show,
1462 .store_attribute = target_core_dev_pr_attr_store,
1463};
1464
1465static struct config_item_type target_core_dev_pr_cit = {
1466 .ct_item_ops = &target_core_dev_pr_ops,
1467 .ct_attrs = target_core_dev_pr_attrs,
1468 .ct_owner = THIS_MODULE,
1469};
1470
1471/* End functions for struct config_item_type target_core_dev_pr_cit */
1472
1473/* Start functions for struct config_item_type target_core_dev_cit */
1474
1475static ssize_t target_core_show_dev_info(void *p, char *page)
1476{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001477 struct se_device *dev = p;
1478 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001479 int bl = 0;
1480 ssize_t read_bytes = 0;
1481
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001482 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001483 read_bytes += bl;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001484 read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485 return read_bytes;
1486}
1487
1488static struct target_core_configfs_attribute target_core_attr_dev_info = {
1489 .attr = { .ca_owner = THIS_MODULE,
1490 .ca_name = "info",
1491 .ca_mode = S_IRUGO },
1492 .show = target_core_show_dev_info,
1493 .store = NULL,
1494};
1495
1496static ssize_t target_core_store_dev_control(
1497 void *p,
1498 const char *page,
1499 size_t count)
1500{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001501 struct se_device *dev = p;
1502 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001503
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001504 return t->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001505}
1506
1507static struct target_core_configfs_attribute target_core_attr_dev_control = {
1508 .attr = { .ca_owner = THIS_MODULE,
1509 .ca_name = "control",
1510 .ca_mode = S_IWUSR },
1511 .show = NULL,
1512 .store = target_core_store_dev_control,
1513};
1514
1515static ssize_t target_core_show_dev_alias(void *p, char *page)
1516{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001517 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001518
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001519 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001520 return 0;
1521
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001522 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001523}
1524
1525static ssize_t target_core_store_dev_alias(
1526 void *p,
1527 const char *page,
1528 size_t count)
1529{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001530 struct se_device *dev = p;
1531 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001532 ssize_t read_bytes;
1533
1534 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001535 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001536 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1537 SE_DEV_ALIAS_LEN-1);
1538 return -EINVAL;
1539 }
1540
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001541 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001542 if (!read_bytes)
1543 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001544 if (dev->dev_alias[read_bytes - 1] == '\n')
1545 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001546
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001547 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001548
Andy Grover6708bb22011-06-08 10:36:43 -07001549 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001550 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001551 config_item_name(&dev->dev_group.cg_item),
1552 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001553
1554 return read_bytes;
1555}
1556
1557static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1558 .attr = { .ca_owner = THIS_MODULE,
1559 .ca_name = "alias",
1560 .ca_mode = S_IRUGO | S_IWUSR },
1561 .show = target_core_show_dev_alias,
1562 .store = target_core_store_dev_alias,
1563};
1564
1565static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1566{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001567 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001568
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001569 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001570 return 0;
1571
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001572 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001573}
1574
1575static ssize_t target_core_store_dev_udev_path(
1576 void *p,
1577 const char *page,
1578 size_t count)
1579{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001580 struct se_device *dev = p;
1581 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001582 ssize_t read_bytes;
1583
1584 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001585 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001586 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1587 SE_UDEV_PATH_LEN-1);
1588 return -EINVAL;
1589 }
1590
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001591 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001592 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001593 if (!read_bytes)
1594 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001595 if (dev->udev_path[read_bytes - 1] == '\n')
1596 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001597
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001598 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001599
Andy Grover6708bb22011-06-08 10:36:43 -07001600 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001601 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001602 config_item_name(&dev->dev_group.cg_item),
1603 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001604
1605 return read_bytes;
1606}
1607
1608static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1609 .attr = { .ca_owner = THIS_MODULE,
1610 .ca_name = "udev_path",
1611 .ca_mode = S_IRUGO | S_IWUSR },
1612 .show = target_core_show_dev_udev_path,
1613 .store = target_core_store_dev_udev_path,
1614};
1615
Andy Grover64146db2013-04-30 11:59:15 -07001616static ssize_t target_core_show_dev_enable(void *p, char *page)
1617{
1618 struct se_device *dev = p;
1619
1620 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1621}
1622
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001623static ssize_t target_core_store_dev_enable(
1624 void *p,
1625 const char *page,
1626 size_t count)
1627{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001628 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001629 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001630 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001631
1632 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001633 if (!ptr) {
1634 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001635 " is \"1\"\n");
1636 return -EINVAL;
1637 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001638
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001639 ret = target_configure_device(dev);
1640 if (ret)
1641 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001642 return count;
1643}
1644
1645static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1646 .attr = { .ca_owner = THIS_MODULE,
1647 .ca_name = "enable",
Andy Grover64146db2013-04-30 11:59:15 -07001648 .ca_mode = S_IRUGO | S_IWUSR },
1649 .show = target_core_show_dev_enable,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001650 .store = target_core_store_dev_enable,
1651};
1652
1653static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1654{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001655 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001656 struct config_item *lu_ci;
1657 struct t10_alua_lu_gp *lu_gp;
1658 struct t10_alua_lu_gp_member *lu_gp_mem;
1659 ssize_t len = 0;
1660
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001661 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001662 if (!lu_gp_mem)
1663 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001664
1665 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1666 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001667 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001668 lu_ci = &lu_gp->lu_gp_group.cg_item;
1669 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1670 config_item_name(lu_ci), lu_gp->lu_gp_id);
1671 }
1672 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1673
1674 return len;
1675}
1676
1677static ssize_t target_core_store_alua_lu_gp(
1678 void *p,
1679 const char *page,
1680 size_t count)
1681{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001682 struct se_device *dev = p;
1683 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001684 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1685 struct t10_alua_lu_gp_member *lu_gp_mem;
1686 unsigned char buf[LU_GROUP_NAME_BUF];
1687 int move = 0;
1688
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001689 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1690 if (!lu_gp_mem)
1691 return 0;
1692
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001693 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001694 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001695 return -EINVAL;
1696 }
1697 memset(buf, 0, LU_GROUP_NAME_BUF);
1698 memcpy(buf, page, count);
1699 /*
1700 * Any ALUA logical unit alias besides "NULL" means we will be
1701 * making a new group association.
1702 */
1703 if (strcmp(strstrip(buf), "NULL")) {
1704 /*
1705 * core_alua_get_lu_gp_by_name() will increment reference to
1706 * struct t10_alua_lu_gp. This reference is released with
1707 * core_alua_get_lu_gp_by_name below().
1708 */
1709 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001710 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001711 return -ENODEV;
1712 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001713
1714 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1715 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001716 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001717 /*
1718 * Clearing an existing lu_gp association, and replacing
1719 * with NULL
1720 */
Andy Grover6708bb22011-06-08 10:36:43 -07001721 if (!lu_gp_new) {
1722 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001723 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1724 " %hu\n",
1725 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001726 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001727 config_item_name(&lu_gp->lu_gp_group.cg_item),
1728 lu_gp->lu_gp_id);
1729
1730 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1731 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1732
1733 return count;
1734 }
1735 /*
1736 * Removing existing association of lu_gp_mem with lu_gp
1737 */
1738 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1739 move = 1;
1740 }
1741 /*
1742 * Associate lu_gp_mem with lu_gp_new.
1743 */
1744 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1745 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1746
Andy Grover6708bb22011-06-08 10:36:43 -07001747 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001748 " core/alua/lu_gps/%s, ID: %hu\n",
1749 (move) ? "Moving" : "Adding",
1750 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001751 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001752 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1753 lu_gp_new->lu_gp_id);
1754
1755 core_alua_put_lu_gp_from_name(lu_gp_new);
1756 return count;
1757}
1758
1759static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1760 .attr = { .ca_owner = THIS_MODULE,
1761 .ca_name = "alua_lu_gp",
1762 .ca_mode = S_IRUGO | S_IWUSR },
1763 .show = target_core_show_alua_lu_gp,
1764 .store = target_core_store_alua_lu_gp,
1765};
1766
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001767static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1768{
1769 struct se_device *dev = p;
1770 struct t10_alua_lba_map *map;
1771 struct t10_alua_lba_map_member *mem;
1772 char *b = page;
1773 int bl = 0;
1774 char state;
1775
1776 spin_lock(&dev->t10_alua.lba_map_lock);
1777 if (!list_empty(&dev->t10_alua.lba_map_list))
1778 bl += sprintf(b + bl, "%u %u\n",
1779 dev->t10_alua.lba_map_segment_size,
1780 dev->t10_alua.lba_map_segment_multiplier);
1781 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1782 bl += sprintf(b + bl, "%llu %llu",
1783 map->lba_map_first_lba, map->lba_map_last_lba);
1784 list_for_each_entry(mem, &map->lba_map_mem_list,
1785 lba_map_mem_list) {
1786 switch (mem->lba_map_mem_alua_state) {
1787 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1788 state = 'O';
1789 break;
1790 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1791 state = 'A';
1792 break;
1793 case ALUA_ACCESS_STATE_STANDBY:
1794 state = 'S';
1795 break;
1796 case ALUA_ACCESS_STATE_UNAVAILABLE:
1797 state = 'U';
1798 break;
1799 default:
1800 state = '.';
1801 break;
1802 }
1803 bl += sprintf(b + bl, " %d:%c",
1804 mem->lba_map_mem_alua_pg_id, state);
1805 }
1806 bl += sprintf(b + bl, "\n");
1807 }
1808 spin_unlock(&dev->t10_alua.lba_map_lock);
1809 return bl;
1810}
1811
1812static ssize_t target_core_store_dev_lba_map(
1813 void *p,
1814 const char *page,
1815 size_t count)
1816{
1817 struct se_device *dev = p;
1818 struct t10_alua_lba_map *lba_map = NULL;
1819 struct list_head lba_list;
1820 char *map_entries, *ptr;
1821 char state;
1822 int pg_num = -1, pg;
1823 int ret = 0, num = 0, pg_id, alua_state;
1824 unsigned long start_lba = -1, end_lba = -1;
1825 unsigned long segment_size = -1, segment_mult = -1;
1826
1827 map_entries = kstrdup(page, GFP_KERNEL);
1828 if (!map_entries)
1829 return -ENOMEM;
1830
1831 INIT_LIST_HEAD(&lba_list);
1832 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1833 if (!*ptr)
1834 continue;
1835
1836 if (num == 0) {
1837 if (sscanf(ptr, "%lu %lu\n",
1838 &segment_size, &segment_mult) != 2) {
1839 pr_err("Invalid line %d\n", num);
1840 ret = -EINVAL;
1841 break;
1842 }
1843 num++;
1844 continue;
1845 }
1846 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1847 pr_err("Invalid line %d\n", num);
1848 ret = -EINVAL;
1849 break;
1850 }
1851 ptr = strchr(ptr, ' ');
1852 if (!ptr) {
1853 pr_err("Invalid line %d, missing end lba\n", num);
1854 ret = -EINVAL;
1855 break;
1856 }
1857 ptr++;
1858 ptr = strchr(ptr, ' ');
1859 if (!ptr) {
1860 pr_err("Invalid line %d, missing state definitions\n",
1861 num);
1862 ret = -EINVAL;
1863 break;
1864 }
1865 ptr++;
1866 lba_map = core_alua_allocate_lba_map(&lba_list,
1867 start_lba, end_lba);
1868 if (IS_ERR(lba_map)) {
1869 ret = PTR_ERR(lba_map);
1870 break;
1871 }
1872 pg = 0;
1873 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1874 switch (state) {
1875 case 'O':
1876 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1877 break;
1878 case 'A':
1879 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1880 break;
1881 case 'S':
1882 alua_state = ALUA_ACCESS_STATE_STANDBY;
1883 break;
1884 case 'U':
1885 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1886 break;
1887 default:
1888 pr_err("Invalid ALUA state '%c'\n", state);
1889 ret = -EINVAL;
1890 goto out;
1891 }
1892
1893 ret = core_alua_allocate_lba_map_mem(lba_map,
1894 pg_id, alua_state);
1895 if (ret) {
1896 pr_err("Invalid target descriptor %d:%c "
1897 "at line %d\n",
1898 pg_id, state, num);
1899 break;
1900 }
1901 pg++;
1902 ptr = strchr(ptr, ' ');
1903 if (ptr)
1904 ptr++;
1905 else
1906 break;
1907 }
1908 if (pg_num == -1)
1909 pg_num = pg;
1910 else if (pg != pg_num) {
1911 pr_err("Only %d from %d port groups definitions "
1912 "at line %d\n", pg, pg_num, num);
1913 ret = -EINVAL;
1914 break;
1915 }
1916 num++;
1917 }
1918out:
1919 if (ret) {
1920 core_alua_free_lba_map(&lba_list);
1921 count = ret;
1922 } else
1923 core_alua_set_lba_map(dev, &lba_list,
1924 segment_size, segment_mult);
1925 kfree(map_entries);
1926 return count;
1927}
1928
1929static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1930 .attr = { .ca_owner = THIS_MODULE,
1931 .ca_name = "lba_map",
1932 .ca_mode = S_IRUGO | S_IWUSR },
1933 .show = target_core_show_dev_lba_map,
1934 .store = target_core_store_dev_lba_map,
1935};
1936
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001937static struct configfs_attribute *lio_core_dev_attrs[] = {
1938 &target_core_attr_dev_info.attr,
1939 &target_core_attr_dev_control.attr,
1940 &target_core_attr_dev_alias.attr,
1941 &target_core_attr_dev_udev_path.attr,
1942 &target_core_attr_dev_enable.attr,
1943 &target_core_attr_dev_alua_lu_gp.attr,
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001944 &target_core_attr_dev_lba_map.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001945 NULL,
1946};
1947
1948static void target_core_dev_release(struct config_item *item)
1949{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001950 struct config_group *dev_cg = to_config_group(item);
1951 struct se_device *dev =
1952 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001953
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001954 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001955 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001956}
1957
1958static ssize_t target_core_dev_show(struct config_item *item,
1959 struct configfs_attribute *attr,
1960 char *page)
1961{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001962 struct config_group *dev_cg = to_config_group(item);
1963 struct se_device *dev =
1964 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001965 struct target_core_configfs_attribute *tc_attr = container_of(
1966 attr, struct target_core_configfs_attribute, attr);
1967
Andy Grover6708bb22011-06-08 10:36:43 -07001968 if (!tc_attr->show)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001969 return -EINVAL;
1970
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001971 return tc_attr->show(dev, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001972}
1973
1974static ssize_t target_core_dev_store(struct config_item *item,
1975 struct configfs_attribute *attr,
1976 const char *page, size_t count)
1977{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001978 struct config_group *dev_cg = to_config_group(item);
1979 struct se_device *dev =
1980 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001981 struct target_core_configfs_attribute *tc_attr = container_of(
1982 attr, struct target_core_configfs_attribute, attr);
1983
Andy Grover6708bb22011-06-08 10:36:43 -07001984 if (!tc_attr->store)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001985 return -EINVAL;
1986
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001987 return tc_attr->store(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001988}
1989
1990static struct configfs_item_operations target_core_dev_item_ops = {
1991 .release = target_core_dev_release,
1992 .show_attribute = target_core_dev_show,
1993 .store_attribute = target_core_dev_store,
1994};
1995
1996static struct config_item_type target_core_dev_cit = {
1997 .ct_item_ops = &target_core_dev_item_ops,
1998 .ct_attrs = lio_core_dev_attrs,
1999 .ct_owner = THIS_MODULE,
2000};
2001
2002/* End functions for struct config_item_type target_core_dev_cit */
2003
2004/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2005
2006CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
2007#define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
2008static struct target_core_alua_lu_gp_attribute \
2009 target_core_alua_lu_gp_##_name = \
2010 __CONFIGFS_EATTR(_name, _mode, \
2011 target_core_alua_lu_gp_show_attr_##_name, \
2012 target_core_alua_lu_gp_store_attr_##_name);
2013
2014#define SE_DEV_ALUA_LU_ATTR_RO(_name) \
2015static struct target_core_alua_lu_gp_attribute \
2016 target_core_alua_lu_gp_##_name = \
2017 __CONFIGFS_EATTR_RO(_name, \
2018 target_core_alua_lu_gp_show_attr_##_name);
2019
2020/*
2021 * lu_gp_id
2022 */
2023static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2024 struct t10_alua_lu_gp *lu_gp,
2025 char *page)
2026{
Andy Grover6708bb22011-06-08 10:36:43 -07002027 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002028 return 0;
2029
2030 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2031}
2032
2033static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2034 struct t10_alua_lu_gp *lu_gp,
2035 const char *page,
2036 size_t count)
2037{
2038 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2039 unsigned long lu_gp_id;
2040 int ret;
2041
Jingoo Han57103d72013-07-19 16:22:19 +09002042 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002043 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002044 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002045 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002046 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002047 }
2048 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002049 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002050 " 0x0000ffff\n", lu_gp_id);
2051 return -EINVAL;
2052 }
2053
2054 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2055 if (ret < 0)
2056 return -EINVAL;
2057
Andy Grover6708bb22011-06-08 10:36:43 -07002058 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002059 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2060 config_item_name(&alua_lu_gp_cg->cg_item),
2061 lu_gp->lu_gp_id);
2062
2063 return count;
2064}
2065
2066SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2067
2068/*
2069 * members
2070 */
2071static ssize_t target_core_alua_lu_gp_show_attr_members(
2072 struct t10_alua_lu_gp *lu_gp,
2073 char *page)
2074{
2075 struct se_device *dev;
2076 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002077 struct t10_alua_lu_gp_member *lu_gp_mem;
2078 ssize_t len = 0, cur_len;
2079 unsigned char buf[LU_GROUP_NAME_BUF];
2080
2081 memset(buf, 0, LU_GROUP_NAME_BUF);
2082
2083 spin_lock(&lu_gp->lu_gp_lock);
2084 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2085 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002086 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002087
2088 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2089 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002090 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002091 cur_len++; /* Extra byte for NULL terminator */
2092
2093 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002094 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002095 "_members buffer\n");
2096 break;
2097 }
2098 memcpy(page+len, buf, cur_len);
2099 len += cur_len;
2100 }
2101 spin_unlock(&lu_gp->lu_gp_lock);
2102
2103 return len;
2104}
2105
2106SE_DEV_ALUA_LU_ATTR_RO(members);
2107
2108CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2109
2110static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2111 &target_core_alua_lu_gp_lu_gp_id.attr,
2112 &target_core_alua_lu_gp_members.attr,
2113 NULL,
2114};
2115
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002116static void target_core_alua_lu_gp_release(struct config_item *item)
2117{
2118 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2119 struct t10_alua_lu_gp, lu_gp_group);
2120
2121 core_alua_free_lu_gp(lu_gp);
2122}
2123
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002124static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002125 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002126 .show_attribute = target_core_alua_lu_gp_attr_show,
2127 .store_attribute = target_core_alua_lu_gp_attr_store,
2128};
2129
2130static struct config_item_type target_core_alua_lu_gp_cit = {
2131 .ct_item_ops = &target_core_alua_lu_gp_ops,
2132 .ct_attrs = target_core_alua_lu_gp_attrs,
2133 .ct_owner = THIS_MODULE,
2134};
2135
2136/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2137
2138/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2139
2140static struct config_group *target_core_alua_create_lu_gp(
2141 struct config_group *group,
2142 const char *name)
2143{
2144 struct t10_alua_lu_gp *lu_gp;
2145 struct config_group *alua_lu_gp_cg = NULL;
2146 struct config_item *alua_lu_gp_ci = NULL;
2147
2148 lu_gp = core_alua_allocate_lu_gp(name, 0);
2149 if (IS_ERR(lu_gp))
2150 return NULL;
2151
2152 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2153 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2154
2155 config_group_init_type_name(alua_lu_gp_cg, name,
2156 &target_core_alua_lu_gp_cit);
2157
Andy Grover6708bb22011-06-08 10:36:43 -07002158 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002159 " Group: core/alua/lu_gps/%s\n",
2160 config_item_name(alua_lu_gp_ci));
2161
2162 return alua_lu_gp_cg;
2163
2164}
2165
2166static void target_core_alua_drop_lu_gp(
2167 struct config_group *group,
2168 struct config_item *item)
2169{
2170 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2171 struct t10_alua_lu_gp, lu_gp_group);
2172
Andy Grover6708bb22011-06-08 10:36:43 -07002173 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002174 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2175 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002176 /*
2177 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2178 * -> target_core_alua_lu_gp_release()
2179 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002180 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002181}
2182
2183static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2184 .make_group = &target_core_alua_create_lu_gp,
2185 .drop_item = &target_core_alua_drop_lu_gp,
2186};
2187
2188static struct config_item_type target_core_alua_lu_gps_cit = {
2189 .ct_item_ops = NULL,
2190 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2191 .ct_owner = THIS_MODULE,
2192};
2193
2194/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2195
2196/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2197
2198CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2199#define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
2200static struct target_core_alua_tg_pt_gp_attribute \
2201 target_core_alua_tg_pt_gp_##_name = \
2202 __CONFIGFS_EATTR(_name, _mode, \
2203 target_core_alua_tg_pt_gp_show_attr_##_name, \
2204 target_core_alua_tg_pt_gp_store_attr_##_name);
2205
2206#define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
2207static struct target_core_alua_tg_pt_gp_attribute \
2208 target_core_alua_tg_pt_gp_##_name = \
2209 __CONFIGFS_EATTR_RO(_name, \
2210 target_core_alua_tg_pt_gp_show_attr_##_name);
2211
2212/*
2213 * alua_access_state
2214 */
2215static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2216 struct t10_alua_tg_pt_gp *tg_pt_gp,
2217 char *page)
2218{
2219 return sprintf(page, "%d\n",
2220 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2221}
2222
2223static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2224 struct t10_alua_tg_pt_gp *tg_pt_gp,
2225 const char *page,
2226 size_t count)
2227{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002228 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002229 unsigned long tmp;
2230 int new_state, ret;
2231
Andy Grover6708bb22011-06-08 10:36:43 -07002232 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002233 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002234 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2235 return -EINVAL;
2236 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07002237 if (!(dev->dev_flags & DF_CONFIGURED)) {
2238 pr_err("Unable to set alua_access_state while device is"
2239 " not configured\n");
2240 return -ENODEV;
2241 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002242
Jingoo Han57103d72013-07-19 16:22:19 +09002243 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002244 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002245 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002246 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002247 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002248 }
2249 new_state = (int)tmp;
2250
Hannes Reinecke125d0112013-11-19 09:07:46 +01002251 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2252 pr_err("Unable to process implicit configfs ALUA"
2253 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002254 return -EINVAL;
2255 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002256 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2257 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2258 /* LBA DEPENDENT is only allowed with implicit ALUA */
2259 pr_err("Unable to process implicit configfs ALUA transition"
2260 " while explicit ALUA management is enabled\n");
2261 return -EINVAL;
2262 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002263
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002264 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002265 NULL, NULL, new_state, 0);
2266 return (!ret) ? count : -EINVAL;
2267}
2268
2269SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2270
2271/*
2272 * alua_access_status
2273 */
2274static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2275 struct t10_alua_tg_pt_gp *tg_pt_gp,
2276 char *page)
2277{
2278 return sprintf(page, "%s\n",
2279 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2280}
2281
2282static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2283 struct t10_alua_tg_pt_gp *tg_pt_gp,
2284 const char *page,
2285 size_t count)
2286{
2287 unsigned long tmp;
2288 int new_status, ret;
2289
Andy Grover6708bb22011-06-08 10:36:43 -07002290 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2291 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002292 " valid tg_pt_gp ID: %hu\n",
2293 tg_pt_gp->tg_pt_gp_valid_id);
2294 return -EINVAL;
2295 }
2296
Jingoo Han57103d72013-07-19 16:22:19 +09002297 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002298 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002299 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002300 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002301 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002302 }
2303 new_status = (int)tmp;
2304
2305 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002306 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2307 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002308 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002309 new_status);
2310 return -EINVAL;
2311 }
2312
2313 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2314 return count;
2315}
2316
2317SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2318
2319/*
2320 * alua_access_type
2321 */
2322static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2323 struct t10_alua_tg_pt_gp *tg_pt_gp,
2324 char *page)
2325{
2326 return core_alua_show_access_type(tg_pt_gp, page);
2327}
2328
2329static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2330 struct t10_alua_tg_pt_gp *tg_pt_gp,
2331 const char *page,
2332 size_t count)
2333{
2334 return core_alua_store_access_type(tg_pt_gp, page, count);
2335}
2336
2337SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2338
2339/*
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002340 * alua_supported_states
2341 */
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002342
2343#define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit) \
2344static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2345 struct t10_alua_tg_pt_gp *t, char *p) \
2346{ \
2347 return sprintf(p, "%d\n", !!(t->_var & _bit)); \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002348}
2349
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002350#define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit) \
2351static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2352 struct t10_alua_tg_pt_gp *t, const char *p, size_t c) \
2353{ \
2354 unsigned long tmp; \
2355 int ret; \
2356 \
2357 if (!t->tg_pt_gp_valid_id) { \
2358 pr_err("Unable to do set ##_name ALUA state on non" \
2359 " valid tg_pt_gp ID: %hu\n", \
2360 t->tg_pt_gp_valid_id); \
2361 return -EINVAL; \
2362 } \
2363 \
2364 ret = kstrtoul(p, 0, &tmp); \
2365 if (ret < 0) { \
2366 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2367 return -EINVAL; \
2368 } \
2369 if (tmp > 1) { \
2370 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2371 return -EINVAL; \
2372 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002373 if (tmp) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002374 t->_var |= _bit; \
2375 else \
2376 t->_var &= ~_bit; \
2377 \
2378 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002379}
2380
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002381SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2382 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2383SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2384 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2385SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
2386
2387SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2388 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2389SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2390 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2391SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
2392
2393SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2394 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2395SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2396 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
Hannes Reineckec66094b2013-12-17 09:18:49 +01002397SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002398
2399SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2400 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2401SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2402 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2403SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
2404
2405SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2406 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2407SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2408 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2409SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2410
2411SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2412 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2413SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2414 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2415SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2416
2417SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2418 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2419SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2420 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2421SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002422
2423/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002424 * alua_write_metadata
2425 */
2426static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2427 struct t10_alua_tg_pt_gp *tg_pt_gp,
2428 char *page)
2429{
2430 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2431}
2432
2433static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2434 struct t10_alua_tg_pt_gp *tg_pt_gp,
2435 const char *page,
2436 size_t count)
2437{
2438 unsigned long tmp;
2439 int ret;
2440
Jingoo Han57103d72013-07-19 16:22:19 +09002441 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002442 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002443 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002444 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002445 }
2446
2447 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002448 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002449 " %lu\n", tmp);
2450 return -EINVAL;
2451 }
2452 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2453
2454 return count;
2455}
2456
2457SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2458
2459
2460
2461/*
2462 * nonop_delay_msecs
2463 */
2464static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2465 struct t10_alua_tg_pt_gp *tg_pt_gp,
2466 char *page)
2467{
2468 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2469
2470}
2471
2472static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2473 struct t10_alua_tg_pt_gp *tg_pt_gp,
2474 const char *page,
2475 size_t count)
2476{
2477 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2478}
2479
2480SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2481
2482/*
2483 * trans_delay_msecs
2484 */
2485static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2486 struct t10_alua_tg_pt_gp *tg_pt_gp,
2487 char *page)
2488{
2489 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2490}
2491
2492static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2493 struct t10_alua_tg_pt_gp *tg_pt_gp,
2494 const char *page,
2495 size_t count)
2496{
2497 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2498}
2499
2500SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2501
2502/*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002503 * implicit_trans_secs
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002504 */
Hannes Reinecke125d0112013-11-19 09:07:46 +01002505static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002506 struct t10_alua_tg_pt_gp *tg_pt_gp,
2507 char *page)
2508{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002509 return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002510}
2511
Hannes Reinecke125d0112013-11-19 09:07:46 +01002512static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002513 struct t10_alua_tg_pt_gp *tg_pt_gp,
2514 const char *page,
2515 size_t count)
2516{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002517 return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002518}
2519
Hannes Reinecke125d0112013-11-19 09:07:46 +01002520SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002521
2522/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002523 * preferred
2524 */
2525
2526static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2527 struct t10_alua_tg_pt_gp *tg_pt_gp,
2528 char *page)
2529{
2530 return core_alua_show_preferred_bit(tg_pt_gp, page);
2531}
2532
2533static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2534 struct t10_alua_tg_pt_gp *tg_pt_gp,
2535 const char *page,
2536 size_t count)
2537{
2538 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2539}
2540
2541SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2542
2543/*
2544 * tg_pt_gp_id
2545 */
2546static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2547 struct t10_alua_tg_pt_gp *tg_pt_gp,
2548 char *page)
2549{
Andy Grover6708bb22011-06-08 10:36:43 -07002550 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002551 return 0;
2552
2553 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2554}
2555
2556static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2557 struct t10_alua_tg_pt_gp *tg_pt_gp,
2558 const char *page,
2559 size_t count)
2560{
2561 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2562 unsigned long tg_pt_gp_id;
2563 int ret;
2564
Jingoo Han57103d72013-07-19 16:22:19 +09002565 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002566 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002567 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002568 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002569 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002570 }
2571 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002572 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002573 " 0x0000ffff\n", tg_pt_gp_id);
2574 return -EINVAL;
2575 }
2576
2577 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2578 if (ret < 0)
2579 return -EINVAL;
2580
Andy Grover6708bb22011-06-08 10:36:43 -07002581 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002582 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2583 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2584 tg_pt_gp->tg_pt_gp_id);
2585
2586 return count;
2587}
2588
2589SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2590
2591/*
2592 * members
2593 */
2594static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2595 struct t10_alua_tg_pt_gp *tg_pt_gp,
2596 char *page)
2597{
2598 struct se_port *port;
2599 struct se_portal_group *tpg;
2600 struct se_lun *lun;
2601 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2602 ssize_t len = 0, cur_len;
2603 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2604
2605 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2606
2607 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2608 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2609 tg_pt_gp_mem_list) {
2610 port = tg_pt_gp_mem->tg_pt;
2611 tpg = port->sep_tpg;
2612 lun = port->sep_lun;
2613
2614 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002615 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2616 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2617 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002618 config_item_name(&lun->lun_group.cg_item));
2619 cur_len++; /* Extra byte for NULL terminator */
2620
2621 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002622 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002623 "_members buffer\n");
2624 break;
2625 }
2626 memcpy(page+len, buf, cur_len);
2627 len += cur_len;
2628 }
2629 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2630
2631 return len;
2632}
2633
2634SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2635
2636CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2637 tg_pt_gp_group);
2638
2639static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2640 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2641 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2642 &target_core_alua_tg_pt_gp_alua_access_type.attr,
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002643 &target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2644 &target_core_alua_tg_pt_gp_alua_support_offline.attr,
2645 &target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2646 &target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2647 &target_core_alua_tg_pt_gp_alua_support_standby.attr,
2648 &target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2649 &target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002650 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2651 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2652 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
Hannes Reinecke125d0112013-11-19 09:07:46 +01002653 &target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002654 &target_core_alua_tg_pt_gp_preferred.attr,
2655 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2656 &target_core_alua_tg_pt_gp_members.attr,
2657 NULL,
2658};
2659
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002660static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2661{
2662 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2663 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2664
2665 core_alua_free_tg_pt_gp(tg_pt_gp);
2666}
2667
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002668static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002669 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002670 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2671 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2672};
2673
2674static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2675 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2676 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2677 .ct_owner = THIS_MODULE,
2678};
2679
2680/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2681
2682/* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2683
2684static struct config_group *target_core_alua_create_tg_pt_gp(
2685 struct config_group *group,
2686 const char *name)
2687{
2688 struct t10_alua *alua = container_of(group, struct t10_alua,
2689 alua_tg_pt_gps_group);
2690 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002691 struct config_group *alua_tg_pt_gp_cg = NULL;
2692 struct config_item *alua_tg_pt_gp_ci = NULL;
2693
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002694 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002695 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002696 return NULL;
2697
2698 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2699 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2700
2701 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2702 &target_core_alua_tg_pt_gp_cit);
2703
Andy Grover6708bb22011-06-08 10:36:43 -07002704 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002705 " Group: alua/tg_pt_gps/%s\n",
2706 config_item_name(alua_tg_pt_gp_ci));
2707
2708 return alua_tg_pt_gp_cg;
2709}
2710
2711static void target_core_alua_drop_tg_pt_gp(
2712 struct config_group *group,
2713 struct config_item *item)
2714{
2715 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2716 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2717
Andy Grover6708bb22011-06-08 10:36:43 -07002718 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002719 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2720 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002721 /*
2722 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2723 * -> target_core_alua_tg_pt_gp_release().
2724 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002725 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002726}
2727
2728static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2729 .make_group = &target_core_alua_create_tg_pt_gp,
2730 .drop_item = &target_core_alua_drop_tg_pt_gp,
2731};
2732
2733static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2734 .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
2735 .ct_owner = THIS_MODULE,
2736};
2737
2738/* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2739
2740/* Start functions for struct config_item_type target_core_alua_cit */
2741
2742/*
2743 * target_core_alua_cit is a ConfigFS group that lives under
2744 * /sys/kernel/config/target/core/alua. There are default groups
2745 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2746 * target_core_alua_cit in target_core_init_configfs() below.
2747 */
2748static struct config_item_type target_core_alua_cit = {
2749 .ct_item_ops = NULL,
2750 .ct_attrs = NULL,
2751 .ct_owner = THIS_MODULE,
2752};
2753
2754/* End functions for struct config_item_type target_core_alua_cit */
2755
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002756/* Start functions for struct config_item_type target_core_stat_cit */
2757
2758static struct config_group *target_core_stat_mkdir(
2759 struct config_group *group,
2760 const char *name)
2761{
2762 return ERR_PTR(-ENOSYS);
2763}
2764
2765static void target_core_stat_rmdir(
2766 struct config_group *group,
2767 struct config_item *item)
2768{
2769 return;
2770}
2771
2772static struct configfs_group_operations target_core_stat_group_ops = {
2773 .make_group = &target_core_stat_mkdir,
2774 .drop_item = &target_core_stat_rmdir,
2775};
2776
2777static struct config_item_type target_core_stat_cit = {
2778 .ct_group_ops = &target_core_stat_group_ops,
2779 .ct_owner = THIS_MODULE,
2780};
2781
2782/* End functions for struct config_item_type target_core_stat_cit */
2783
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002784/* Start functions for struct config_item_type target_core_hba_cit */
2785
2786static struct config_group *target_core_make_subdev(
2787 struct config_group *group,
2788 const char *name)
2789{
2790 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002791 struct se_subsystem_api *t;
2792 struct config_item *hba_ci = &group->cg_item;
2793 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002794 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002795 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002796 struct config_group *dev_stat_grp = NULL;
2797 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002798
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002799 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2800 if (ret)
2801 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002802 /*
2803 * Locate the struct se_subsystem_api from parent's struct se_hba.
2804 */
2805 t = hba->transport;
2806
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002807 dev = target_alloc_device(hba, name);
2808 if (!dev)
2809 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002810
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002811 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002812
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002813 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002814 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002815 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002816 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002817
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002818 config_group_init_type_name(dev_cg, name, &target_core_dev_cit);
2819 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002820 &target_core_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002821 config_group_init_type_name(&dev->dev_pr_group, "pr",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002822 &target_core_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002823 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002824 &target_core_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002825 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002826 "alua", &target_core_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002827 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002828 "statistics", &target_core_stat_cit);
2829
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002830 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2831 dev_cg->default_groups[1] = &dev->dev_pr_group;
2832 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2833 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2834 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002835 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002836 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002837 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002838 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002839 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002840 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002841 goto out_free_dev_cg_default_groups;
2842 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002843
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002844 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002845 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002846 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002847 if (!tg_pt_gp_cg->default_groups) {
2848 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002849 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002850 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002851 }
2852
2853 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2854 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2855 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2856 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002857 /*
2858 * Add core/$HBA/$DEV/statistics/ default groups
2859 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002860 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002861 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002862 GFP_KERNEL);
2863 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002864 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002865 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002866 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002867 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002868
2869 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002870 return dev_cg;
2871
2872out_free_tg_pt_gp_cg_default_groups:
2873 kfree(tg_pt_gp_cg->default_groups);
2874out_free_tg_pt_gp:
2875 core_alua_free_tg_pt_gp(tg_pt_gp);
2876out_free_dev_cg_default_groups:
2877 kfree(dev_cg->default_groups);
2878out_free_device:
2879 target_free_device(dev);
2880out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002881 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002882 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002883}
2884
2885static void target_core_drop_subdev(
2886 struct config_group *group,
2887 struct config_item *item)
2888{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002889 struct config_group *dev_cg = to_config_group(item);
2890 struct se_device *dev =
2891 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002892 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002893 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002894 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002895 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002896
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002897 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002898
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002899 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002900
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002901 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002902 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2903 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2904 dev_stat_grp->default_groups[i] = NULL;
2905 config_item_put(df_item);
2906 }
2907 kfree(dev_stat_grp->default_groups);
2908
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002909 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002910 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2911 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2912 tg_pt_gp_cg->default_groups[i] = NULL;
2913 config_item_put(df_item);
2914 }
2915 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002916 /*
2917 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2918 * directly from target_core_alua_tg_pt_gp_release().
2919 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002920 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002921
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002922 for (i = 0; dev_cg->default_groups[i]; i++) {
2923 df_item = &dev_cg->default_groups[i]->cg_item;
2924 dev_cg->default_groups[i] = NULL;
2925 config_item_put(df_item);
2926 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002927 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002928 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002929 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002930 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002931 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002932}
2933
2934static struct configfs_group_operations target_core_hba_group_ops = {
2935 .make_group = target_core_make_subdev,
2936 .drop_item = target_core_drop_subdev,
2937};
2938
2939CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2940#define SE_HBA_ATTR(_name, _mode) \
2941static struct target_core_hba_attribute \
2942 target_core_hba_##_name = \
2943 __CONFIGFS_EATTR(_name, _mode, \
2944 target_core_hba_show_attr_##_name, \
2945 target_core_hba_store_attr_##_name);
2946
2947#define SE_HBA_ATTR_RO(_name) \
2948static struct target_core_hba_attribute \
2949 target_core_hba_##_name = \
2950 __CONFIGFS_EATTR_RO(_name, \
2951 target_core_hba_show_attr_##_name);
2952
2953static ssize_t target_core_hba_show_attr_hba_info(
2954 struct se_hba *hba,
2955 char *page)
2956{
2957 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2958 hba->hba_id, hba->transport->name,
2959 TARGET_CORE_CONFIGFS_VERSION);
2960}
2961
2962SE_HBA_ATTR_RO(hba_info);
2963
2964static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2965 char *page)
2966{
2967 int hba_mode = 0;
2968
2969 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2970 hba_mode = 1;
2971
2972 return sprintf(page, "%d\n", hba_mode);
2973}
2974
2975static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2976 const char *page, size_t count)
2977{
2978 struct se_subsystem_api *transport = hba->transport;
2979 unsigned long mode_flag;
2980 int ret;
2981
2982 if (transport->pmode_enable_hba == NULL)
2983 return -EINVAL;
2984
Jingoo Han57103d72013-07-19 16:22:19 +09002985 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002986 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002987 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002988 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002989 }
2990
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002991 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07002992 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002993 return -EINVAL;
2994 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002995
2996 ret = transport->pmode_enable_hba(hba, mode_flag);
2997 if (ret < 0)
2998 return -EINVAL;
2999 if (ret > 0)
3000 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3001 else if (ret == 0)
3002 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3003
3004 return count;
3005}
3006
3007SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
3008
3009CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
3010
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003011static void target_core_hba_release(struct config_item *item)
3012{
3013 struct se_hba *hba = container_of(to_config_group(item),
3014 struct se_hba, hba_group);
3015 core_delete_hba(hba);
3016}
3017
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003018static struct configfs_attribute *target_core_hba_attrs[] = {
3019 &target_core_hba_hba_info.attr,
3020 &target_core_hba_hba_mode.attr,
3021 NULL,
3022};
3023
3024static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003025 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003026 .show_attribute = target_core_hba_attr_show,
3027 .store_attribute = target_core_hba_attr_store,
3028};
3029
3030static struct config_item_type target_core_hba_cit = {
3031 .ct_item_ops = &target_core_hba_item_ops,
3032 .ct_group_ops = &target_core_hba_group_ops,
3033 .ct_attrs = target_core_hba_attrs,
3034 .ct_owner = THIS_MODULE,
3035};
3036
3037static struct config_group *target_core_call_addhbatotarget(
3038 struct config_group *group,
3039 const char *name)
3040{
3041 char *se_plugin_str, *str, *str2;
3042 struct se_hba *hba;
3043 char buf[TARGET_CORE_NAME_MAX_LEN];
3044 unsigned long plugin_dep_id = 0;
3045 int ret;
3046
3047 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07003048 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07003049 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003050 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3051 TARGET_CORE_NAME_MAX_LEN);
3052 return ERR_PTR(-ENAMETOOLONG);
3053 }
3054 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3055
3056 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003057 if (!str) {
3058 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003059 return ERR_PTR(-EINVAL);
3060 }
3061 se_plugin_str = buf;
3062 /*
3063 * Special case for subsystem plugins that have "_" in their names.
3064 * Namely rd_direct and rd_mcp..
3065 */
3066 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003067 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003068 *str2 = '\0'; /* Terminate for *se_plugin_str */
3069 str2++; /* Skip to start of plugin dependent ID */
3070 str = str2;
3071 } else {
3072 *str = '\0'; /* Terminate for *se_plugin_str */
3073 str++; /* Skip to start of plugin dependent ID */
3074 }
3075
Jingoo Han57103d72013-07-19 16:22:19 +09003076 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003077 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09003078 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003079 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003080 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003081 }
3082 /*
3083 * Load up TCM subsystem plugins if they have not already been loaded.
3084 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07003085 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003086
3087 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3088 if (IS_ERR(hba))
3089 return ERR_CAST(hba);
3090
3091 config_group_init_type_name(&hba->hba_group, name,
3092 &target_core_hba_cit);
3093
3094 return &hba->hba_group;
3095}
3096
3097static void target_core_call_delhbafromtarget(
3098 struct config_group *group,
3099 struct config_item *item)
3100{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003101 /*
3102 * core_delete_hba() is called from target_core_hba_item_ops->release()
3103 * -> target_core_hba_release()
3104 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003105 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003106}
3107
3108static struct configfs_group_operations target_core_group_ops = {
3109 .make_group = target_core_call_addhbatotarget,
3110 .drop_item = target_core_call_delhbafromtarget,
3111};
3112
3113static struct config_item_type target_core_cit = {
3114 .ct_item_ops = NULL,
3115 .ct_group_ops = &target_core_group_ops,
3116 .ct_attrs = NULL,
3117 .ct_owner = THIS_MODULE,
3118};
3119
3120/* Stop functions for struct config_item_type target_core_hba_cit */
3121
Axel Lin54550fa2011-03-14 04:06:09 -07003122static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003123{
3124 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3125 struct config_group *lu_gp_cg = NULL;
3126 struct configfs_subsystem *subsys;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003127 struct t10_alua_lu_gp *lu_gp;
3128 int ret;
3129
Andy Grover6708bb22011-06-08 10:36:43 -07003130 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003131 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3132 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3133
3134 subsys = target_core_subsystem[0];
3135 config_group_init(&subsys->su_group);
3136 mutex_init(&subsys->su_mutex);
3137
Andy Grovere3d6f902011-07-19 08:55:10 +00003138 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003139 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003140 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003141 /*
3142 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3143 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3144 */
3145 target_cg = &subsys->su_group;
Andy Groverab6dae82013-12-09 14:27:36 -08003146 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003147 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003148 if (!target_cg->default_groups) {
3149 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003150 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003151 goto out_global;
3152 }
3153
Andy Grovere3d6f902011-07-19 08:55:10 +00003154 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003155 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003156 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003157 target_cg->default_groups[1] = NULL;
3158 /*
3159 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3160 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003161 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003162 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003163 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003164 if (!hba_cg->default_groups) {
3165 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003166 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003167 goto out_global;
3168 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003169 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003170 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003171 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003172 hba_cg->default_groups[1] = NULL;
3173 /*
3174 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3175 * groups under /sys/kernel/config/target/core/alua/
3176 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003177 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003178 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003179 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003180 if (!alua_cg->default_groups) {
3181 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003182 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003183 goto out_global;
3184 }
3185
Andy Grovere3d6f902011-07-19 08:55:10 +00003186 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003187 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003188 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003189 alua_cg->default_groups[1] = NULL;
3190 /*
3191 * Add core/alua/lu_gps/default_lu_gp
3192 */
3193 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003194 if (IS_ERR(lu_gp)) {
3195 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003196 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003197 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003198
Andy Grovere3d6f902011-07-19 08:55:10 +00003199 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003200 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003201 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003202 if (!lu_gp_cg->default_groups) {
3203 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003204 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003205 goto out_global;
3206 }
3207
3208 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3209 &target_core_alua_lu_gp_cit);
3210 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3211 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003212 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003213 /*
3214 * Register the target_core_mod subsystem with configfs.
3215 */
3216 ret = configfs_register_subsystem(subsys);
3217 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003218 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003219 ret, subsys->su_group.cg_item.ci_namebuf);
3220 goto out_global;
3221 }
Andy Grover6708bb22011-06-08 10:36:43 -07003222 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003223 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3224 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3225 /*
3226 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3227 */
3228 ret = rd_module_init();
3229 if (ret < 0)
3230 goto out;
3231
Roland Dreier0d0f9df2012-10-31 09:16:44 -07003232 ret = core_dev_setup_virtual_lun0();
3233 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003234 goto out;
3235
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003236 ret = target_xcopy_setup_pt();
3237 if (ret < 0)
3238 goto out;
3239
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003240 return 0;
3241
3242out:
3243 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003244 core_dev_release_virtual_lun0();
3245 rd_module_exit();
3246out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00003247 if (default_lu_gp) {
3248 core_alua_free_lu_gp(default_lu_gp);
3249 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003250 }
3251 if (lu_gp_cg)
3252 kfree(lu_gp_cg->default_groups);
3253 if (alua_cg)
3254 kfree(alua_cg->default_groups);
3255 if (hba_cg)
3256 kfree(hba_cg->default_groups);
3257 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00003258 release_se_kmem_caches();
3259 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003260}
3261
Axel Lin54550fa2011-03-14 04:06:09 -07003262static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003263{
3264 struct configfs_subsystem *subsys;
3265 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3266 struct config_item *item;
3267 int i;
3268
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003269 subsys = target_core_subsystem[0];
3270
Andy Grovere3d6f902011-07-19 08:55:10 +00003271 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003272 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3273 item = &lu_gp_cg->default_groups[i]->cg_item;
3274 lu_gp_cg->default_groups[i] = NULL;
3275 config_item_put(item);
3276 }
3277 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003278 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003279
Andy Grovere3d6f902011-07-19 08:55:10 +00003280 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003281 for (i = 0; alua_cg->default_groups[i]; i++) {
3282 item = &alua_cg->default_groups[i]->cg_item;
3283 alua_cg->default_groups[i] = NULL;
3284 config_item_put(item);
3285 }
3286 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003287 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003288
Andy Grovere3d6f902011-07-19 08:55:10 +00003289 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003290 for (i = 0; hba_cg->default_groups[i]; i++) {
3291 item = &hba_cg->default_groups[i]->cg_item;
3292 hba_cg->default_groups[i] = NULL;
3293 config_item_put(item);
3294 }
3295 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003296 hba_cg->default_groups = NULL;
3297 /*
3298 * We expect subsys->su_group.default_groups to be released
3299 * by configfs subsystem provider logic..
3300 */
3301 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003302 kfree(subsys->su_group.default_groups);
3303
Andy Grovere3d6f902011-07-19 08:55:10 +00003304 core_alua_free_lu_gp(default_lu_gp);
3305 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003306
Andy Grover6708bb22011-06-08 10:36:43 -07003307 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003308 " Infrastructure\n");
3309
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003310 core_dev_release_virtual_lun0();
3311 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003312 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003313 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003314}
3315
3316MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3317MODULE_AUTHOR("nab@Linux-iSCSI.org");
3318MODULE_LICENSE("GPL");
3319
3320module_init(target_core_init_configfs);
3321module_exit(target_core_exit_configfs);