blob: 4efb61b8d00195b7d2f6018f0578f4e02273cda1 [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 Bellingerfd9a11d2012-11-09 14:51:48 -08006 * (c) Copyright 2008-2012 RisingTide Systems LLC.
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"
51
Andy Grovere3d6f902011-07-19 08:55:10 +000052extern struct t10_alua_lu_gp *default_lu_gp;
53
Roland Dreierd0f474e2012-01-12 10:41:18 -080054static LIST_HEAD(g_tf_list);
55static DEFINE_MUTEX(g_tf_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080056
57struct target_core_configfs_attribute {
58 struct configfs_attribute attr;
59 ssize_t (*show)(void *, char *);
60 ssize_t (*store)(void *, const char *, size_t);
61};
62
Andy Grovere3d6f902011-07-19 08:55:10 +000063static struct config_group target_core_hbagroup;
64static struct config_group alua_group;
65static struct config_group alua_lu_gps_group;
66
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080067static inline struct se_hba *
68item_to_hba(struct config_item *item)
69{
70 return container_of(to_config_group(item), struct se_hba, hba_group);
71}
72
73/*
74 * Attributes for /sys/kernel/config/target/
75 */
76static ssize_t target_core_attr_show(struct config_item *item,
77 struct configfs_attribute *attr,
78 char *page)
79{
80 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
81 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
82 utsname()->sysname, utsname()->machine);
83}
84
85static struct configfs_item_operations target_core_fabric_item_ops = {
86 .show_attribute = target_core_attr_show,
87};
88
89static struct configfs_attribute target_core_item_attr_version = {
90 .ca_owner = THIS_MODULE,
91 .ca_name = "version",
92 .ca_mode = S_IRUGO,
93};
94
95static struct target_fabric_configfs *target_core_get_fabric(
96 const char *name)
97{
98 struct target_fabric_configfs *tf;
99
Andy Grover6708bb22011-06-08 10:36:43 -0700100 if (!name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800101 return NULL;
102
103 mutex_lock(&g_tf_lock);
104 list_for_each_entry(tf, &g_tf_list, tf_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700105 if (!strcmp(tf->tf_name, name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800106 atomic_inc(&tf->tf_access_cnt);
107 mutex_unlock(&g_tf_lock);
108 return tf;
109 }
110 }
111 mutex_unlock(&g_tf_lock);
112
113 return NULL;
114}
115
116/*
117 * Called from struct target_core_group_ops->make_group()
118 */
119static struct config_group *target_core_register_fabric(
120 struct config_group *group,
121 const char *name)
122{
123 struct target_fabric_configfs *tf;
124 int ret;
125
Andy Grover6708bb22011-06-08 10:36:43 -0700126 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800127 " %s\n", group, name);
128 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800129 * Below are some hardcoded request_module() calls to automatically
130 * local fabric modules when the following is called:
131 *
132 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
133 *
134 * Note that this does not limit which TCM fabric module can be
135 * registered, but simply provids auto loading logic for modules with
136 * mkdir(2) system calls with known TCM fabric modules.
137 */
Andy Grover6708bb22011-06-08 10:36:43 -0700138 if (!strncmp(name, "iscsi", 5)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800139 /*
140 * Automatically load the LIO Target fabric module when the
141 * following is called:
142 *
143 * mkdir -p $CONFIGFS/target/iscsi
144 */
145 ret = request_module("iscsi_target_mod");
146 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700147 pr_err("request_module() failed for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800148 " iscsi_target_mod.ko: %d\n", ret);
149 return ERR_PTR(-EINVAL);
150 }
Andy Grover6708bb22011-06-08 10:36:43 -0700151 } else if (!strncmp(name, "loopback", 8)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800152 /*
153 * Automatically load the tcm_loop fabric module when the
154 * following is called:
155 *
156 * mkdir -p $CONFIGFS/target/loopback
157 */
158 ret = request_module("tcm_loop");
159 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700160 pr_err("request_module() failed for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800161 " tcm_loop.ko: %d\n", ret);
162 return ERR_PTR(-EINVAL);
163 }
164 }
165
166 tf = target_core_get_fabric(name);
Andy Grover6708bb22011-06-08 10:36:43 -0700167 if (!tf) {
168 pr_err("target_core_get_fabric() failed for %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800169 name);
170 return ERR_PTR(-EINVAL);
171 }
Andy Grover6708bb22011-06-08 10:36:43 -0700172 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800173 " %s\n", tf->tf_name);
174 /*
175 * On a successful target_core_get_fabric() look, the returned
176 * struct target_fabric_configfs *tf will contain a usage reference.
177 */
Andy Grover6708bb22011-06-08 10:36:43 -0700178 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800179 &TF_CIT_TMPL(tf)->tfc_wwn_cit);
180
181 tf->tf_group.default_groups = tf->tf_default_groups;
182 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
183 tf->tf_group.default_groups[1] = NULL;
184
185 config_group_init_type_name(&tf->tf_group, name,
186 &TF_CIT_TMPL(tf)->tfc_wwn_cit);
187 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
188 &TF_CIT_TMPL(tf)->tfc_discovery_cit);
189
Andy Grover6708bb22011-06-08 10:36:43 -0700190 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800191 " %s\n", tf->tf_group.cg_item.ci_name);
192 /*
193 * Setup tf_ops.tf_subsys pointer for usage with configfs_depend_item()
194 */
195 tf->tf_ops.tf_subsys = tf->tf_subsys;
196 tf->tf_fabric = &tf->tf_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -0700197 pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800198 " for %s\n", name);
199
200 return &tf->tf_group;
201}
202
203/*
204 * Called from struct target_core_group_ops->drop_item()
205 */
206static void target_core_deregister_fabric(
207 struct config_group *group,
208 struct config_item *item)
209{
210 struct target_fabric_configfs *tf = container_of(
211 to_config_group(item), struct target_fabric_configfs, tf_group);
212 struct config_group *tf_group;
213 struct config_item *df_item;
214 int i;
215
Andy Grover6708bb22011-06-08 10:36:43 -0700216 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800217 " tf list\n", config_item_name(item));
218
Andy Grover6708bb22011-06-08 10:36:43 -0700219 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800220 " %s\n", tf->tf_name);
221 atomic_dec(&tf->tf_access_cnt);
222
Andy Grover6708bb22011-06-08 10:36:43 -0700223 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800224 " tf->tf_fabric for %s\n", tf->tf_name);
225 tf->tf_fabric = NULL;
226
Andy Grover6708bb22011-06-08 10:36:43 -0700227 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800228 " %s\n", config_item_name(item));
229
230 tf_group = &tf->tf_group;
231 for (i = 0; tf_group->default_groups[i]; i++) {
232 df_item = &tf_group->default_groups[i]->cg_item;
233 tf_group->default_groups[i] = NULL;
234 config_item_put(df_item);
235 }
236 config_item_put(item);
237}
238
239static struct configfs_group_operations target_core_fabric_group_ops = {
240 .make_group = &target_core_register_fabric,
241 .drop_item = &target_core_deregister_fabric,
242};
243
244/*
245 * All item attributes appearing in /sys/kernel/target/ appear here.
246 */
247static struct configfs_attribute *target_core_fabric_item_attrs[] = {
248 &target_core_item_attr_version,
249 NULL,
250};
251
252/*
253 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
254 */
255static struct config_item_type target_core_fabrics_item = {
256 .ct_item_ops = &target_core_fabric_item_ops,
257 .ct_group_ops = &target_core_fabric_group_ops,
258 .ct_attrs = target_core_fabric_item_attrs,
259 .ct_owner = THIS_MODULE,
260};
261
262static struct configfs_subsystem target_core_fabrics = {
263 .su_group = {
264 .cg_item = {
265 .ci_namebuf = "target",
266 .ci_type = &target_core_fabrics_item,
267 },
268 },
269};
270
271static struct configfs_subsystem *target_core_subsystem[] = {
272 &target_core_fabrics,
273 NULL,
274};
275
276/*##############################################################################
277// Start functions called by external Target Fabrics Modules
278//############################################################################*/
279
280/*
281 * First function called by fabric modules to:
282 *
283 * 1) Allocate a struct target_fabric_configfs and save the *fabric_cit pointer.
284 * 2) Add struct target_fabric_configfs to g_tf_list
285 * 3) Return struct target_fabric_configfs to fabric module to be passed
286 * into target_fabric_configfs_register().
287 */
288struct target_fabric_configfs *target_fabric_configfs_init(
289 struct module *fabric_mod,
290 const char *name)
291{
292 struct target_fabric_configfs *tf;
293
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800294 if (!(name)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700295 pr_err("Unable to locate passed fabric name\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000296 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800297 }
Dan Carpenter60d645a2011-06-15 10:03:05 -0700298 if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700299 pr_err("Passed name: %s exceeds TARGET_FABRIC"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800300 "_NAME_SIZE\n", name);
Andy Grovere3d6f902011-07-19 08:55:10 +0000301 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800302 }
303
304 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700305 if (!tf)
Andy Grovere3d6f902011-07-19 08:55:10 +0000306 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800307
308 INIT_LIST_HEAD(&tf->tf_list);
309 atomic_set(&tf->tf_access_cnt, 0);
310 /*
311 * Setup the default generic struct config_item_type's (cits) in
312 * struct target_fabric_configfs->tf_cit_tmpl
313 */
314 tf->tf_module = fabric_mod;
315 target_fabric_setup_cits(tf);
316
317 tf->tf_subsys = target_core_subsystem[0];
318 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
319
320 mutex_lock(&g_tf_lock);
321 list_add_tail(&tf->tf_list, &g_tf_list);
322 mutex_unlock(&g_tf_lock);
323
Andy Grover6708bb22011-06-08 10:36:43 -0700324 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800325 ">>>>>>>>>>>>>>\n");
Andy Grover6708bb22011-06-08 10:36:43 -0700326 pr_debug("Initialized struct target_fabric_configfs: %p for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800327 " %s\n", tf, tf->tf_name);
328 return tf;
329}
330EXPORT_SYMBOL(target_fabric_configfs_init);
331
332/*
333 * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
334 */
335void target_fabric_configfs_free(
336 struct target_fabric_configfs *tf)
337{
338 mutex_lock(&g_tf_lock);
339 list_del(&tf->tf_list);
340 mutex_unlock(&g_tf_lock);
341
342 kfree(tf);
343}
344EXPORT_SYMBOL(target_fabric_configfs_free);
345
346/*
347 * Perform a sanity check of the passed tf->tf_ops before completing
348 * TCM fabric module registration.
349 */
350static int target_fabric_tf_ops_check(
351 struct target_fabric_configfs *tf)
352{
353 struct target_core_fabric_ops *tfo = &tf->tf_ops;
354
Andy Grover6708bb22011-06-08 10:36:43 -0700355 if (!tfo->get_fabric_name) {
356 pr_err("Missing tfo->get_fabric_name()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800357 return -EINVAL;
358 }
Andy Grover6708bb22011-06-08 10:36:43 -0700359 if (!tfo->get_fabric_proto_ident) {
360 pr_err("Missing tfo->get_fabric_proto_ident()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800361 return -EINVAL;
362 }
Andy Grover6708bb22011-06-08 10:36:43 -0700363 if (!tfo->tpg_get_wwn) {
364 pr_err("Missing tfo->tpg_get_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800365 return -EINVAL;
366 }
Andy Grover6708bb22011-06-08 10:36:43 -0700367 if (!tfo->tpg_get_tag) {
368 pr_err("Missing tfo->tpg_get_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800369 return -EINVAL;
370 }
Andy Grover6708bb22011-06-08 10:36:43 -0700371 if (!tfo->tpg_get_default_depth) {
372 pr_err("Missing tfo->tpg_get_default_depth()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800373 return -EINVAL;
374 }
Andy Grover6708bb22011-06-08 10:36:43 -0700375 if (!tfo->tpg_get_pr_transport_id) {
376 pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800377 return -EINVAL;
378 }
Andy Grover6708bb22011-06-08 10:36:43 -0700379 if (!tfo->tpg_get_pr_transport_id_len) {
380 pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800381 return -EINVAL;
382 }
Andy Grover6708bb22011-06-08 10:36:43 -0700383 if (!tfo->tpg_check_demo_mode) {
384 pr_err("Missing tfo->tpg_check_demo_mode()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800385 return -EINVAL;
386 }
Andy Grover6708bb22011-06-08 10:36:43 -0700387 if (!tfo->tpg_check_demo_mode_cache) {
388 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800389 return -EINVAL;
390 }
Andy Grover6708bb22011-06-08 10:36:43 -0700391 if (!tfo->tpg_check_demo_mode_write_protect) {
392 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800393 return -EINVAL;
394 }
Andy Grover6708bb22011-06-08 10:36:43 -0700395 if (!tfo->tpg_check_prod_mode_write_protect) {
396 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800397 return -EINVAL;
398 }
Andy Grover6708bb22011-06-08 10:36:43 -0700399 if (!tfo->tpg_alloc_fabric_acl) {
400 pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800401 return -EINVAL;
402 }
Andy Grover6708bb22011-06-08 10:36:43 -0700403 if (!tfo->tpg_release_fabric_acl) {
404 pr_err("Missing tfo->tpg_release_fabric_acl()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800405 return -EINVAL;
406 }
Andy Grover6708bb22011-06-08 10:36:43 -0700407 if (!tfo->tpg_get_inst_index) {
408 pr_err("Missing tfo->tpg_get_inst_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800409 return -EINVAL;
410 }
Christoph Hellwig35462972011-05-31 23:56:57 -0400411 if (!tfo->release_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700412 pr_err("Missing tfo->release_cmd()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800413 return -EINVAL;
414 }
Andy Grover6708bb22011-06-08 10:36:43 -0700415 if (!tfo->shutdown_session) {
416 pr_err("Missing tfo->shutdown_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800417 return -EINVAL;
418 }
Andy Grover6708bb22011-06-08 10:36:43 -0700419 if (!tfo->close_session) {
420 pr_err("Missing tfo->close_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800421 return -EINVAL;
422 }
Andy Grover6708bb22011-06-08 10:36:43 -0700423 if (!tfo->sess_get_index) {
424 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800425 return -EINVAL;
426 }
Andy Grover6708bb22011-06-08 10:36:43 -0700427 if (!tfo->write_pending) {
428 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800429 return -EINVAL;
430 }
Andy Grover6708bb22011-06-08 10:36:43 -0700431 if (!tfo->write_pending_status) {
432 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800433 return -EINVAL;
434 }
Andy Grover6708bb22011-06-08 10:36:43 -0700435 if (!tfo->set_default_node_attributes) {
436 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800437 return -EINVAL;
438 }
Andy Grover6708bb22011-06-08 10:36:43 -0700439 if (!tfo->get_task_tag) {
440 pr_err("Missing tfo->get_task_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800441 return -EINVAL;
442 }
Andy Grover6708bb22011-06-08 10:36:43 -0700443 if (!tfo->get_cmd_state) {
444 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800445 return -EINVAL;
446 }
Andy Grover6708bb22011-06-08 10:36:43 -0700447 if (!tfo->queue_data_in) {
448 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800449 return -EINVAL;
450 }
Andy Grover6708bb22011-06-08 10:36:43 -0700451 if (!tfo->queue_status) {
452 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800453 return -EINVAL;
454 }
Andy Grover6708bb22011-06-08 10:36:43 -0700455 if (!tfo->queue_tm_rsp) {
456 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800457 return -EINVAL;
458 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800459 /*
460 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
461 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
462 * target_core_fabric_configfs.c WWN+TPG group context code.
463 */
Andy Grover6708bb22011-06-08 10:36:43 -0700464 if (!tfo->fabric_make_wwn) {
465 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800466 return -EINVAL;
467 }
Andy Grover6708bb22011-06-08 10:36:43 -0700468 if (!tfo->fabric_drop_wwn) {
469 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800470 return -EINVAL;
471 }
Andy Grover6708bb22011-06-08 10:36:43 -0700472 if (!tfo->fabric_make_tpg) {
473 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800474 return -EINVAL;
475 }
Andy Grover6708bb22011-06-08 10:36:43 -0700476 if (!tfo->fabric_drop_tpg) {
477 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800478 return -EINVAL;
479 }
480
481 return 0;
482}
483
484/*
485 * Called 2nd from fabric module with returned parameter of
486 * struct target_fabric_configfs * from target_fabric_configfs_init().
487 *
488 * Upon a successful registration, the new fabric's struct config_item is
489 * return. Also, a pointer to this struct is set in the passed
490 * struct target_fabric_configfs.
491 */
492int target_fabric_configfs_register(
493 struct target_fabric_configfs *tf)
494{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800495 int ret;
496
Andy Grover6708bb22011-06-08 10:36:43 -0700497 if (!tf) {
498 pr_err("Unable to locate target_fabric_configfs"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800499 " pointer\n");
500 return -EINVAL;
501 }
Andy Grover6708bb22011-06-08 10:36:43 -0700502 if (!tf->tf_subsys) {
503 pr_err("Unable to target struct config_subsystem"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800504 " pointer\n");
505 return -EINVAL;
506 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800507 ret = target_fabric_tf_ops_check(tf);
508 if (ret < 0)
509 return ret;
510
Andy Grover6708bb22011-06-08 10:36:43 -0700511 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800512 ">>>>>>>>>>\n");
513 return 0;
514}
515EXPORT_SYMBOL(target_fabric_configfs_register);
516
517void target_fabric_configfs_deregister(
518 struct target_fabric_configfs *tf)
519{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800520 struct configfs_subsystem *su;
521
Andy Grover6708bb22011-06-08 10:36:43 -0700522 if (!tf) {
523 pr_err("Unable to locate passed target_fabric_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800524 "configfs\n");
525 return;
526 }
527 su = tf->tf_subsys;
Andy Grover6708bb22011-06-08 10:36:43 -0700528 if (!su) {
529 pr_err("Unable to locate passed tf->tf_subsys"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800530 " pointer\n");
531 return;
532 }
Andy Grover6708bb22011-06-08 10:36:43 -0700533 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800534 ">>>>>>>>>>>>\n");
535 mutex_lock(&g_tf_lock);
536 if (atomic_read(&tf->tf_access_cnt)) {
537 mutex_unlock(&g_tf_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700538 pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800539 tf->tf_name);
540 BUG();
541 }
542 list_del(&tf->tf_list);
543 mutex_unlock(&g_tf_lock);
544
Andy Grover6708bb22011-06-08 10:36:43 -0700545 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800546 " %s\n", tf->tf_name);
547 tf->tf_module = NULL;
548 tf->tf_subsys = NULL;
549 kfree(tf);
550
Andy Grover6708bb22011-06-08 10:36:43 -0700551 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800552 ">>>>>\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800553}
554EXPORT_SYMBOL(target_fabric_configfs_deregister);
555
556/*##############################################################################
557// Stop functions called by external Target Fabrics Modules
558//############################################################################*/
559
560/* Start functions for struct config_item_type target_core_dev_attrib_cit */
561
562#define DEF_DEV_ATTRIB_SHOW(_name) \
563static ssize_t target_core_dev_show_attr_##_name( \
564 struct se_dev_attrib *da, \
565 char *page) \
566{ \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400567 return snprintf(page, PAGE_SIZE, "%u\n", \
568 (u32)da->da_dev->dev_attrib._name); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800569}
570
571#define DEF_DEV_ATTRIB_STORE(_name) \
572static ssize_t target_core_dev_store_attr_##_name( \
573 struct se_dev_attrib *da, \
574 const char *page, \
575 size_t count) \
576{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800577 unsigned long val; \
578 int ret; \
579 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800580 ret = strict_strtoul(page, 0, &val); \
581 if (ret < 0) { \
Andy Grover6708bb22011-06-08 10:36:43 -0700582 pr_err("strict_strtoul() failed with" \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800583 " ret: %d\n", ret); \
584 return -EINVAL; \
585 } \
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400586 ret = se_dev_set_##_name(da->da_dev, (u32)val); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800587 \
588 return (!ret) ? count : -EINVAL; \
589}
590
591#define DEF_DEV_ATTRIB(_name) \
592DEF_DEV_ATTRIB_SHOW(_name); \
593DEF_DEV_ATTRIB_STORE(_name);
594
595#define DEF_DEV_ATTRIB_RO(_name) \
596DEF_DEV_ATTRIB_SHOW(_name);
597
598CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
599#define SE_DEV_ATTR(_name, _mode) \
600static struct target_core_dev_attrib_attribute \
601 target_core_dev_attrib_##_name = \
602 __CONFIGFS_EATTR(_name, _mode, \
603 target_core_dev_show_attr_##_name, \
604 target_core_dev_store_attr_##_name);
605
606#define SE_DEV_ATTR_RO(_name); \
607static struct target_core_dev_attrib_attribute \
608 target_core_dev_attrib_##_name = \
609 __CONFIGFS_EATTR_RO(_name, \
610 target_core_dev_show_attr_##_name);
611
612DEF_DEV_ATTRIB(emulate_dpo);
613SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
614
615DEF_DEV_ATTRIB(emulate_fua_write);
616SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
617
618DEF_DEV_ATTRIB(emulate_fua_read);
619SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
620
621DEF_DEV_ATTRIB(emulate_write_cache);
622SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
623
624DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
625SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
626
627DEF_DEV_ATTRIB(emulate_tas);
628SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
629
630DEF_DEV_ATTRIB(emulate_tpu);
631SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
632
633DEF_DEV_ATTRIB(emulate_tpws);
634SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
635
636DEF_DEV_ATTRIB(enforce_pr_isids);
637SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
638
Roland Dreiere22a7f02011-07-05 13:34:52 -0700639DEF_DEV_ATTRIB(is_nonrot);
640SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
641
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700642DEF_DEV_ATTRIB(emulate_rest_reord);
643SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
644
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800645DEF_DEV_ATTRIB_RO(hw_block_size);
646SE_DEV_ATTR_RO(hw_block_size);
647
648DEF_DEV_ATTRIB(block_size);
649SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
650
651DEF_DEV_ATTRIB_RO(hw_max_sectors);
652SE_DEV_ATTR_RO(hw_max_sectors);
653
Roland Dreier015487b2012-02-13 16:18:17 -0800654DEF_DEV_ATTRIB(fabric_max_sectors);
655SE_DEV_ATTR(fabric_max_sectors, S_IRUGO | S_IWUSR);
656
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800657DEF_DEV_ATTRIB(optimal_sectors);
658SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
659
660DEF_DEV_ATTRIB_RO(hw_queue_depth);
661SE_DEV_ATTR_RO(hw_queue_depth);
662
663DEF_DEV_ATTRIB(queue_depth);
664SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
665
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800666DEF_DEV_ATTRIB(max_unmap_lba_count);
667SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
668
669DEF_DEV_ATTRIB(max_unmap_block_desc_count);
670SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
671
672DEF_DEV_ATTRIB(unmap_granularity);
673SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
674
675DEF_DEV_ATTRIB(unmap_granularity_alignment);
676SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
677
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800678DEF_DEV_ATTRIB(max_write_same_len);
679SE_DEV_ATTR(max_write_same_len, S_IRUGO | S_IWUSR);
680
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800681CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
682
683static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
684 &target_core_dev_attrib_emulate_dpo.attr,
685 &target_core_dev_attrib_emulate_fua_write.attr,
686 &target_core_dev_attrib_emulate_fua_read.attr,
687 &target_core_dev_attrib_emulate_write_cache.attr,
688 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
689 &target_core_dev_attrib_emulate_tas.attr,
690 &target_core_dev_attrib_emulate_tpu.attr,
691 &target_core_dev_attrib_emulate_tpws.attr,
692 &target_core_dev_attrib_enforce_pr_isids.attr,
Roland Dreiere22a7f02011-07-05 13:34:52 -0700693 &target_core_dev_attrib_is_nonrot.attr,
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700694 &target_core_dev_attrib_emulate_rest_reord.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800695 &target_core_dev_attrib_hw_block_size.attr,
696 &target_core_dev_attrib_block_size.attr,
697 &target_core_dev_attrib_hw_max_sectors.attr,
Roland Dreier015487b2012-02-13 16:18:17 -0800698 &target_core_dev_attrib_fabric_max_sectors.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800699 &target_core_dev_attrib_optimal_sectors.attr,
700 &target_core_dev_attrib_hw_queue_depth.attr,
701 &target_core_dev_attrib_queue_depth.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800702 &target_core_dev_attrib_max_unmap_lba_count.attr,
703 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
704 &target_core_dev_attrib_unmap_granularity.attr,
705 &target_core_dev_attrib_unmap_granularity_alignment.attr,
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800706 &target_core_dev_attrib_max_write_same_len.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800707 NULL,
708};
709
710static struct configfs_item_operations target_core_dev_attrib_ops = {
711 .show_attribute = target_core_dev_attrib_attr_show,
712 .store_attribute = target_core_dev_attrib_attr_store,
713};
714
715static struct config_item_type target_core_dev_attrib_cit = {
716 .ct_item_ops = &target_core_dev_attrib_ops,
717 .ct_attrs = target_core_dev_attrib_attrs,
718 .ct_owner = THIS_MODULE,
719};
720
721/* End functions for struct config_item_type target_core_dev_attrib_cit */
722
723/* Start functions for struct config_item_type target_core_dev_wwn_cit */
724
725CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
726#define SE_DEV_WWN_ATTR(_name, _mode) \
727static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
728 __CONFIGFS_EATTR(_name, _mode, \
729 target_core_dev_wwn_show_attr_##_name, \
730 target_core_dev_wwn_store_attr_##_name);
731
732#define SE_DEV_WWN_ATTR_RO(_name); \
733do { \
734 static struct target_core_dev_wwn_attribute \
735 target_core_dev_wwn_##_name = \
736 __CONFIGFS_EATTR_RO(_name, \
737 target_core_dev_wwn_show_attr_##_name); \
738} while (0);
739
740/*
741 * VPD page 0x80 Unit serial
742 */
743static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
744 struct t10_wwn *t10_wwn,
745 char *page)
746{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800747 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
748 &t10_wwn->unit_serial[0]);
749}
750
751static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
752 struct t10_wwn *t10_wwn,
753 const char *page,
754 size_t count)
755{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400756 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800757 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
758
759 /*
760 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
761 * from the struct scsi_device level firmware, do not allow
762 * VPD Unit Serial to be emulated.
763 *
764 * Note this struct scsi_device could also be emulating VPD
765 * information from its drivers/scsi LLD. But for now we assume
766 * it is doing 'the right thing' wrt a world wide unique
767 * VPD Unit Serial Number that OS dependent multipath can depend on.
768 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400769 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700770 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800771 " Unit Serial, ignoring request\n");
772 return -EOPNOTSUPP;
773 }
774
Dan Carpenter60d645a2011-06-15 10:03:05 -0700775 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -0700776 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800777 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
778 return -EOVERFLOW;
779 }
780 /*
781 * Check to see if any active $FABRIC_MOD exports exist. If they
782 * do exist, fail here as changing this information on the fly
783 * (underneath the initiator side OS dependent multipath code)
784 * could cause negative effects.
785 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400786 if (dev->export_count) {
787 pr_err("Unable to set VPD Unit Serial while"
788 " active %d $FABRIC_MOD exports exist\n",
789 dev->export_count);
790 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800791 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400792
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800793 /*
794 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
795 *
796 * Also, strip any newline added from the userspace
797 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
798 */
799 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
800 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400801 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800802 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400803 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800804
Andy Grover6708bb22011-06-08 10:36:43 -0700805 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400806 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800807
808 return count;
809}
810
811SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
812
813/*
814 * VPD page 0x83 Protocol Identifier
815 */
816static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
817 struct t10_wwn *t10_wwn,
818 char *page)
819{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800820 struct t10_vpd *vpd;
821 unsigned char buf[VPD_TMP_BUF_SIZE];
822 ssize_t len = 0;
823
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800824 memset(buf, 0, VPD_TMP_BUF_SIZE);
825
826 spin_lock(&t10_wwn->t10_vpd_lock);
827 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700828 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800829 continue;
830
831 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
832
Andy Grover6708bb22011-06-08 10:36:43 -0700833 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800834 break;
835
836 len += sprintf(page+len, "%s", buf);
837 }
838 spin_unlock(&t10_wwn->t10_vpd_lock);
839
840 return len;
841}
842
843static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
844 struct t10_wwn *t10_wwn,
845 const char *page,
846 size_t count)
847{
848 return -ENOSYS;
849}
850
851SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
852
853/*
854 * Generic wrapper for dumping VPD identifiers by association.
855 */
856#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
857static ssize_t target_core_dev_wwn_show_attr_##_name( \
858 struct t10_wwn *t10_wwn, \
859 char *page) \
860{ \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800861 struct t10_vpd *vpd; \
862 unsigned char buf[VPD_TMP_BUF_SIZE]; \
863 ssize_t len = 0; \
864 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800865 spin_lock(&t10_wwn->t10_vpd_lock); \
866 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
867 if (vpd->association != _assoc) \
868 continue; \
869 \
870 memset(buf, 0, VPD_TMP_BUF_SIZE); \
871 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700872 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800873 break; \
874 len += sprintf(page+len, "%s", buf); \
875 \
876 memset(buf, 0, VPD_TMP_BUF_SIZE); \
877 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700878 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800879 break; \
880 len += sprintf(page+len, "%s", buf); \
881 \
882 memset(buf, 0, VPD_TMP_BUF_SIZE); \
883 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -0700884 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800885 break; \
886 len += sprintf(page+len, "%s", buf); \
887 } \
888 spin_unlock(&t10_wwn->t10_vpd_lock); \
889 \
890 return len; \
891}
892
893/*
Andy Shevchenko163cd5f2011-07-18 22:17:43 -0700894 * VPD page 0x83 Association: Logical Unit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800895 */
896DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
897
898static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
899 struct t10_wwn *t10_wwn,
900 const char *page,
901 size_t count)
902{
903 return -ENOSYS;
904}
905
906SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
907
908/*
909 * VPD page 0x83 Association: Target Port
910 */
911DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
912
913static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
914 struct t10_wwn *t10_wwn,
915 const char *page,
916 size_t count)
917{
918 return -ENOSYS;
919}
920
921SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
922
923/*
924 * VPD page 0x83 Association: SCSI Target Device
925 */
926DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
927
928static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
929 struct t10_wwn *t10_wwn,
930 const char *page,
931 size_t count)
932{
933 return -ENOSYS;
934}
935
936SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
937
938CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
939
940static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
941 &target_core_dev_wwn_vpd_unit_serial.attr,
942 &target_core_dev_wwn_vpd_protocol_identifier.attr,
943 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
944 &target_core_dev_wwn_vpd_assoc_target_port.attr,
945 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
946 NULL,
947};
948
949static struct configfs_item_operations target_core_dev_wwn_ops = {
950 .show_attribute = target_core_dev_wwn_attr_show,
951 .store_attribute = target_core_dev_wwn_attr_store,
952};
953
954static struct config_item_type target_core_dev_wwn_cit = {
955 .ct_item_ops = &target_core_dev_wwn_ops,
956 .ct_attrs = target_core_dev_wwn_attrs,
957 .ct_owner = THIS_MODULE,
958};
959
960/* End functions for struct config_item_type target_core_dev_wwn_cit */
961
962/* Start functions for struct config_item_type target_core_dev_pr_cit */
963
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400964CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800965#define SE_DEV_PR_ATTR(_name, _mode) \
966static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
967 __CONFIGFS_EATTR(_name, _mode, \
968 target_core_dev_pr_show_attr_##_name, \
969 target_core_dev_pr_store_attr_##_name);
970
971#define SE_DEV_PR_ATTR_RO(_name); \
972static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
973 __CONFIGFS_EATTR_RO(_name, \
974 target_core_dev_pr_show_attr_##_name);
975
Christoph Hellwigd977f432012-10-10 17:37:15 -0400976static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
977 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800978{
979 struct se_node_acl *se_nacl;
980 struct t10_pr_registration *pr_reg;
981 char i_buf[PR_REG_ISID_ID_LEN];
982 int prf_isid;
983
984 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
985
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800986 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400987 if (!pr_reg)
988 return sprintf(page, "No SPC-3 Reservation holder\n");
989
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800990 se_nacl = pr_reg->pr_reg_nacl;
991 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
992 PR_REG_ISID_ID_LEN);
993
Christoph Hellwigd977f432012-10-10 17:37:15 -0400994 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000995 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800996 se_nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800997}
998
Christoph Hellwigd977f432012-10-10 17:37:15 -0400999static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1000 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001001{
1002 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001003 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001004
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001005 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001006 if (se_nacl) {
1007 len = sprintf(page,
1008 "SPC-2 Reservation: %s Initiator: %s\n",
1009 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1010 se_nacl->initiatorname);
1011 } else {
1012 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001013 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001014 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001015}
1016
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001017static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
1018 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001019{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001020 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001021
Christoph Hellwigd977f432012-10-10 17:37:15 -04001022 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1023 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001024
Christoph Hellwigd977f432012-10-10 17:37:15 -04001025 spin_lock(&dev->dev_reservation_lock);
1026 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1027 ret = target_core_dev_pr_show_spc2_res(dev, page);
1028 else
1029 ret = target_core_dev_pr_show_spc3_res(dev, page);
1030 spin_unlock(&dev->dev_reservation_lock);
1031 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001032}
1033
1034SE_DEV_PR_ATTR_RO(res_holder);
1035
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001036static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001037 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001038{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001039 ssize_t len = 0;
1040
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001041 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001042 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001043 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001044 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001045 len = sprintf(page, "SPC-3 Reservation: All Target"
1046 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001047 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001048 len = sprintf(page, "SPC-3 Reservation: Single"
1049 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001050 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001051
Christoph Hellwigd977f432012-10-10 17:37:15 -04001052 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001053 return len;
1054}
1055
1056SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1057
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001058static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001059 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001060{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001061 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001062}
1063
1064SE_DEV_PR_ATTR_RO(res_pr_generation);
1065
1066/*
1067 * res_pr_holder_tg_port
1068 */
1069static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001070 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001071{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001072 struct se_node_acl *se_nacl;
1073 struct se_lun *lun;
1074 struct se_portal_group *se_tpg;
1075 struct t10_pr_registration *pr_reg;
1076 struct target_core_fabric_ops *tfo;
1077 ssize_t len = 0;
1078
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001079 spin_lock(&dev->dev_reservation_lock);
1080 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001081 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001082 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001083 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001084 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001085
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001086 se_nacl = pr_reg->pr_reg_nacl;
1087 se_tpg = se_nacl->se_tpg;
1088 lun = pr_reg->pr_reg_tg_pt_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +00001089 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001090
1091 len += sprintf(page+len, "SPC-3 Reservation: %s"
1092 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1093 tfo->tpg_get_wwn(se_tpg));
1094 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001095 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001096 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1097 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1098 tfo->get_fabric_name(), lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001099
Christoph Hellwigd977f432012-10-10 17:37:15 -04001100out_unlock:
1101 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001102 return len;
1103}
1104
1105SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1106
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001107static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001108 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001109{
1110 struct target_core_fabric_ops *tfo;
1111 struct t10_pr_registration *pr_reg;
1112 unsigned char buf[384];
1113 char i_buf[PR_REG_ISID_ID_LEN];
1114 ssize_t len = 0;
1115 int reg_count = 0, prf_isid;
1116
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001117 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1118
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001119 spin_lock(&dev->t10_pr.registration_lock);
1120 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001121 pr_reg_list) {
1122
1123 memset(buf, 0, 384);
1124 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1125 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1126 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1127 PR_REG_ISID_ID_LEN);
1128 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1129 tfo->get_fabric_name(),
1130 pr_reg->pr_reg_nacl->initiatorname, (prf_isid) ?
1131 &i_buf[0] : "", pr_reg->pr_res_key,
1132 pr_reg->pr_res_generation);
1133
Andy Grover6708bb22011-06-08 10:36:43 -07001134 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001135 break;
1136
1137 len += sprintf(page+len, "%s", buf);
1138 reg_count++;
1139 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001140 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001141
Andy Grover6708bb22011-06-08 10:36:43 -07001142 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001143 len += sprintf(page+len, "None\n");
1144
1145 return len;
1146}
1147
1148SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1149
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001150static ssize_t target_core_dev_pr_show_attr_res_pr_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001151 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001152{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001153 struct t10_pr_registration *pr_reg;
1154 ssize_t len = 0;
1155
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001156 spin_lock(&dev->dev_reservation_lock);
1157 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001158 if (pr_reg) {
1159 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1160 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1161 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001162 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001163 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001164
Christoph Hellwigd977f432012-10-10 17:37:15 -04001165 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001166 return len;
1167}
1168
1169SE_DEV_PR_ATTR_RO(res_pr_type);
1170
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001171static ssize_t target_core_dev_pr_show_attr_res_type(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001172 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001173{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001174 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1175 return sprintf(page, "SPC_PASSTHROUGH\n");
1176 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1177 return sprintf(page, "SPC2_RESERVATIONS\n");
1178 else
1179 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001180}
1181
1182SE_DEV_PR_ATTR_RO(res_type);
1183
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001184static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001185 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001186{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001187 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001188 return 0;
1189
1190 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001191 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001192}
1193
1194SE_DEV_PR_ATTR_RO(res_aptpl_active);
1195
1196/*
1197 * res_aptpl_metadata
1198 */
1199static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001200 struct se_device *dev, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001201{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001202 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001203 return 0;
1204
1205 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1206}
1207
1208enum {
1209 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1210 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1211 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1212 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1213};
1214
1215static match_table_t tokens = {
1216 {Opt_initiator_fabric, "initiator_fabric=%s"},
1217 {Opt_initiator_node, "initiator_node=%s"},
1218 {Opt_initiator_sid, "initiator_sid=%s"},
1219 {Opt_sa_res_key, "sa_res_key=%s"},
1220 {Opt_res_holder, "res_holder=%d"},
1221 {Opt_res_type, "res_type=%d"},
1222 {Opt_res_scope, "res_scope=%d"},
1223 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1224 {Opt_mapped_lun, "mapped_lun=%d"},
1225 {Opt_target_fabric, "target_fabric=%s"},
1226 {Opt_target_node, "target_node=%s"},
1227 {Opt_tpgt, "tpgt=%d"},
1228 {Opt_port_rtpi, "port_rtpi=%d"},
1229 {Opt_target_lun, "target_lun=%d"},
1230 {Opt_err, NULL}
1231};
1232
1233static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001234 struct se_device *dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001235 const char *page,
1236 size_t count)
1237{
Jesper Juhl6d180252011-03-14 04:05:56 -07001238 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1239 unsigned char *t_fabric = NULL, *t_port = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001240 char *orig, *ptr, *arg_p, *opts;
1241 substring_t args[MAX_OPT_ARGS];
1242 unsigned long long tmp_ll;
1243 u64 sa_res_key = 0;
1244 u32 mapped_lun = 0, target_lun = 0;
1245 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1246 u16 port_rpti = 0, tpgt = 0;
1247 u8 type = 0, scope;
1248
Christoph Hellwigd977f432012-10-10 17:37:15 -04001249 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1250 return 0;
1251 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001252 return 0;
1253
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001254 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001255 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001256 " active fabric exports exist\n");
1257 return -EINVAL;
1258 }
1259
1260 opts = kstrdup(page, GFP_KERNEL);
1261 if (!opts)
1262 return -ENOMEM;
1263
1264 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001265 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001266 if (!*ptr)
1267 continue;
1268
1269 token = match_token(ptr, tokens, args);
1270 switch (token) {
1271 case Opt_initiator_fabric:
1272 i_fabric = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001273 if (!i_fabric) {
1274 ret = -ENOMEM;
1275 goto out;
1276 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001277 break;
1278 case Opt_initiator_node:
1279 i_port = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001280 if (!i_port) {
1281 ret = -ENOMEM;
1282 goto out;
1283 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001284 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001285 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001286 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1287 PR_APTPL_MAX_IPORT_LEN);
1288 ret = -EINVAL;
1289 break;
1290 }
1291 break;
1292 case Opt_initiator_sid:
1293 isid = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001294 if (!isid) {
1295 ret = -ENOMEM;
1296 goto out;
1297 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001298 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001299 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001300 "= exceeds PR_REG_ISID_LEN: %d\n",
1301 PR_REG_ISID_LEN);
1302 ret = -EINVAL;
1303 break;
1304 }
1305 break;
1306 case Opt_sa_res_key:
1307 arg_p = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001308 if (!arg_p) {
1309 ret = -ENOMEM;
1310 goto out;
1311 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312 ret = strict_strtoull(arg_p, 0, &tmp_ll);
1313 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001314 pr_err("strict_strtoull() failed for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001315 " sa_res_key=\n");
1316 goto out;
1317 }
1318 sa_res_key = (u64)tmp_ll;
1319 break;
1320 /*
1321 * PR APTPL Metadata for Reservation
1322 */
1323 case Opt_res_holder:
1324 match_int(args, &arg);
1325 res_holder = arg;
1326 break;
1327 case Opt_res_type:
1328 match_int(args, &arg);
1329 type = (u8)arg;
1330 break;
1331 case Opt_res_scope:
1332 match_int(args, &arg);
1333 scope = (u8)arg;
1334 break;
1335 case Opt_res_all_tg_pt:
1336 match_int(args, &arg);
1337 all_tg_pt = (int)arg;
1338 break;
1339 case Opt_mapped_lun:
1340 match_int(args, &arg);
1341 mapped_lun = (u32)arg;
1342 break;
1343 /*
1344 * PR APTPL Metadata for Target Port
1345 */
1346 case Opt_target_fabric:
1347 t_fabric = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001348 if (!t_fabric) {
1349 ret = -ENOMEM;
1350 goto out;
1351 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001352 break;
1353 case Opt_target_node:
1354 t_port = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -07001355 if (!t_port) {
1356 ret = -ENOMEM;
1357 goto out;
1358 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001359 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001360 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001361 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1362 PR_APTPL_MAX_TPORT_LEN);
1363 ret = -EINVAL;
1364 break;
1365 }
1366 break;
1367 case Opt_tpgt:
1368 match_int(args, &arg);
1369 tpgt = (u16)arg;
1370 break;
1371 case Opt_port_rtpi:
1372 match_int(args, &arg);
1373 port_rpti = (u16)arg;
1374 break;
1375 case Opt_target_lun:
1376 match_int(args, &arg);
1377 target_lun = (u32)arg;
1378 break;
1379 default:
1380 break;
1381 }
1382 }
1383
Andy Grover6708bb22011-06-08 10:36:43 -07001384 if (!i_port || !t_port || !sa_res_key) {
1385 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001386 ret = -EINVAL;
1387 goto out;
1388 }
1389
1390 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001391 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001392 " holder\n", type);
1393 ret = -EINVAL;
1394 goto out;
1395 }
1396
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001397 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001398 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1399 res_holder, all_tg_pt, type);
1400out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001401 kfree(i_fabric);
1402 kfree(i_port);
1403 kfree(isid);
1404 kfree(t_fabric);
1405 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001406 kfree(orig);
1407 return (ret == 0) ? count : ret;
1408}
1409
1410SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1411
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001412CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001413
1414static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1415 &target_core_dev_pr_res_holder.attr,
1416 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1417 &target_core_dev_pr_res_pr_generation.attr,
1418 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1419 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1420 &target_core_dev_pr_res_pr_type.attr,
1421 &target_core_dev_pr_res_type.attr,
1422 &target_core_dev_pr_res_aptpl_active.attr,
1423 &target_core_dev_pr_res_aptpl_metadata.attr,
1424 NULL,
1425};
1426
1427static struct configfs_item_operations target_core_dev_pr_ops = {
1428 .show_attribute = target_core_dev_pr_attr_show,
1429 .store_attribute = target_core_dev_pr_attr_store,
1430};
1431
1432static struct config_item_type target_core_dev_pr_cit = {
1433 .ct_item_ops = &target_core_dev_pr_ops,
1434 .ct_attrs = target_core_dev_pr_attrs,
1435 .ct_owner = THIS_MODULE,
1436};
1437
1438/* End functions for struct config_item_type target_core_dev_pr_cit */
1439
1440/* Start functions for struct config_item_type target_core_dev_cit */
1441
1442static ssize_t target_core_show_dev_info(void *p, char *page)
1443{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001444 struct se_device *dev = p;
1445 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001446 int bl = 0;
1447 ssize_t read_bytes = 0;
1448
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001449 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001450 read_bytes += bl;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001451 read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001452 return read_bytes;
1453}
1454
1455static struct target_core_configfs_attribute target_core_attr_dev_info = {
1456 .attr = { .ca_owner = THIS_MODULE,
1457 .ca_name = "info",
1458 .ca_mode = S_IRUGO },
1459 .show = target_core_show_dev_info,
1460 .store = NULL,
1461};
1462
1463static ssize_t target_core_store_dev_control(
1464 void *p,
1465 const char *page,
1466 size_t count)
1467{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001468 struct se_device *dev = p;
1469 struct se_subsystem_api *t = dev->transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001470
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001471 return t->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001472}
1473
1474static struct target_core_configfs_attribute target_core_attr_dev_control = {
1475 .attr = { .ca_owner = THIS_MODULE,
1476 .ca_name = "control",
1477 .ca_mode = S_IWUSR },
1478 .show = NULL,
1479 .store = target_core_store_dev_control,
1480};
1481
1482static ssize_t target_core_show_dev_alias(void *p, char *page)
1483{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001484 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001486 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001487 return 0;
1488
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001489 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001490}
1491
1492static ssize_t target_core_store_dev_alias(
1493 void *p,
1494 const char *page,
1495 size_t count)
1496{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001497 struct se_device *dev = p;
1498 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001499 ssize_t read_bytes;
1500
1501 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001502 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001503 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1504 SE_DEV_ALIAS_LEN-1);
1505 return -EINVAL;
1506 }
1507
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001508 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001509 if (!read_bytes)
1510 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001511 if (dev->dev_alias[read_bytes - 1] == '\n')
1512 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001513
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001514 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001515
Andy Grover6708bb22011-06-08 10:36:43 -07001516 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001517 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001518 config_item_name(&dev->dev_group.cg_item),
1519 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001520
1521 return read_bytes;
1522}
1523
1524static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1525 .attr = { .ca_owner = THIS_MODULE,
1526 .ca_name = "alias",
1527 .ca_mode = S_IRUGO | S_IWUSR },
1528 .show = target_core_show_dev_alias,
1529 .store = target_core_store_dev_alias,
1530};
1531
1532static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1533{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001534 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001535
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001536 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001537 return 0;
1538
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001539 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001540}
1541
1542static ssize_t target_core_store_dev_udev_path(
1543 void *p,
1544 const char *page,
1545 size_t count)
1546{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001547 struct se_device *dev = p;
1548 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001549 ssize_t read_bytes;
1550
1551 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001552 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001553 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1554 SE_UDEV_PATH_LEN-1);
1555 return -EINVAL;
1556 }
1557
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001558 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001559 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001560 if (!read_bytes)
1561 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001562 if (dev->udev_path[read_bytes - 1] == '\n')
1563 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001564
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001565 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001566
Andy Grover6708bb22011-06-08 10:36:43 -07001567 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001568 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001569 config_item_name(&dev->dev_group.cg_item),
1570 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001571
1572 return read_bytes;
1573}
1574
1575static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1576 .attr = { .ca_owner = THIS_MODULE,
1577 .ca_name = "udev_path",
1578 .ca_mode = S_IRUGO | S_IWUSR },
1579 .show = target_core_show_dev_udev_path,
1580 .store = target_core_store_dev_udev_path,
1581};
1582
1583static ssize_t target_core_store_dev_enable(
1584 void *p,
1585 const char *page,
1586 size_t count)
1587{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001588 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001589 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001590 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001591
1592 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001593 if (!ptr) {
1594 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001595 " is \"1\"\n");
1596 return -EINVAL;
1597 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001598
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001599 ret = target_configure_device(dev);
1600 if (ret)
1601 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001602 return count;
1603}
1604
1605static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1606 .attr = { .ca_owner = THIS_MODULE,
1607 .ca_name = "enable",
1608 .ca_mode = S_IWUSR },
1609 .show = NULL,
1610 .store = target_core_store_dev_enable,
1611};
1612
1613static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1614{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001615 struct se_device *dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001616 struct config_item *lu_ci;
1617 struct t10_alua_lu_gp *lu_gp;
1618 struct t10_alua_lu_gp_member *lu_gp_mem;
1619 ssize_t len = 0;
1620
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001621 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001622 if (!lu_gp_mem)
1623 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001624
1625 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1626 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001627 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001628 lu_ci = &lu_gp->lu_gp_group.cg_item;
1629 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1630 config_item_name(lu_ci), lu_gp->lu_gp_id);
1631 }
1632 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1633
1634 return len;
1635}
1636
1637static ssize_t target_core_store_alua_lu_gp(
1638 void *p,
1639 const char *page,
1640 size_t count)
1641{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001642 struct se_device *dev = p;
1643 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001644 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1645 struct t10_alua_lu_gp_member *lu_gp_mem;
1646 unsigned char buf[LU_GROUP_NAME_BUF];
1647 int move = 0;
1648
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001649 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1650 if (!lu_gp_mem)
1651 return 0;
1652
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001653 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001654 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001655 return -EINVAL;
1656 }
1657 memset(buf, 0, LU_GROUP_NAME_BUF);
1658 memcpy(buf, page, count);
1659 /*
1660 * Any ALUA logical unit alias besides "NULL" means we will be
1661 * making a new group association.
1662 */
1663 if (strcmp(strstrip(buf), "NULL")) {
1664 /*
1665 * core_alua_get_lu_gp_by_name() will increment reference to
1666 * struct t10_alua_lu_gp. This reference is released with
1667 * core_alua_get_lu_gp_by_name below().
1668 */
1669 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001670 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001671 return -ENODEV;
1672 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001673
1674 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1675 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001676 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001677 /*
1678 * Clearing an existing lu_gp association, and replacing
1679 * with NULL
1680 */
Andy Grover6708bb22011-06-08 10:36:43 -07001681 if (!lu_gp_new) {
1682 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001683 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1684 " %hu\n",
1685 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001686 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001687 config_item_name(&lu_gp->lu_gp_group.cg_item),
1688 lu_gp->lu_gp_id);
1689
1690 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1691 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1692
1693 return count;
1694 }
1695 /*
1696 * Removing existing association of lu_gp_mem with lu_gp
1697 */
1698 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1699 move = 1;
1700 }
1701 /*
1702 * Associate lu_gp_mem with lu_gp_new.
1703 */
1704 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1705 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1706
Andy Grover6708bb22011-06-08 10:36:43 -07001707 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001708 " core/alua/lu_gps/%s, ID: %hu\n",
1709 (move) ? "Moving" : "Adding",
1710 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001711 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001712 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1713 lu_gp_new->lu_gp_id);
1714
1715 core_alua_put_lu_gp_from_name(lu_gp_new);
1716 return count;
1717}
1718
1719static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1720 .attr = { .ca_owner = THIS_MODULE,
1721 .ca_name = "alua_lu_gp",
1722 .ca_mode = S_IRUGO | S_IWUSR },
1723 .show = target_core_show_alua_lu_gp,
1724 .store = target_core_store_alua_lu_gp,
1725};
1726
1727static struct configfs_attribute *lio_core_dev_attrs[] = {
1728 &target_core_attr_dev_info.attr,
1729 &target_core_attr_dev_control.attr,
1730 &target_core_attr_dev_alias.attr,
1731 &target_core_attr_dev_udev_path.attr,
1732 &target_core_attr_dev_enable.attr,
1733 &target_core_attr_dev_alua_lu_gp.attr,
1734 NULL,
1735};
1736
1737static void target_core_dev_release(struct config_item *item)
1738{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001739 struct config_group *dev_cg = to_config_group(item);
1740 struct se_device *dev =
1741 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001742
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001743 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001744 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001745}
1746
1747static ssize_t target_core_dev_show(struct config_item *item,
1748 struct configfs_attribute *attr,
1749 char *page)
1750{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001751 struct config_group *dev_cg = to_config_group(item);
1752 struct se_device *dev =
1753 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001754 struct target_core_configfs_attribute *tc_attr = container_of(
1755 attr, struct target_core_configfs_attribute, attr);
1756
Andy Grover6708bb22011-06-08 10:36:43 -07001757 if (!tc_attr->show)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001758 return -EINVAL;
1759
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001760 return tc_attr->show(dev, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001761}
1762
1763static ssize_t target_core_dev_store(struct config_item *item,
1764 struct configfs_attribute *attr,
1765 const char *page, size_t count)
1766{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001767 struct config_group *dev_cg = to_config_group(item);
1768 struct se_device *dev =
1769 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001770 struct target_core_configfs_attribute *tc_attr = container_of(
1771 attr, struct target_core_configfs_attribute, attr);
1772
Andy Grover6708bb22011-06-08 10:36:43 -07001773 if (!tc_attr->store)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001774 return -EINVAL;
1775
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001776 return tc_attr->store(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001777}
1778
1779static struct configfs_item_operations target_core_dev_item_ops = {
1780 .release = target_core_dev_release,
1781 .show_attribute = target_core_dev_show,
1782 .store_attribute = target_core_dev_store,
1783};
1784
1785static struct config_item_type target_core_dev_cit = {
1786 .ct_item_ops = &target_core_dev_item_ops,
1787 .ct_attrs = lio_core_dev_attrs,
1788 .ct_owner = THIS_MODULE,
1789};
1790
1791/* End functions for struct config_item_type target_core_dev_cit */
1792
1793/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1794
1795CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1796#define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
1797static struct target_core_alua_lu_gp_attribute \
1798 target_core_alua_lu_gp_##_name = \
1799 __CONFIGFS_EATTR(_name, _mode, \
1800 target_core_alua_lu_gp_show_attr_##_name, \
1801 target_core_alua_lu_gp_store_attr_##_name);
1802
1803#define SE_DEV_ALUA_LU_ATTR_RO(_name) \
1804static struct target_core_alua_lu_gp_attribute \
1805 target_core_alua_lu_gp_##_name = \
1806 __CONFIGFS_EATTR_RO(_name, \
1807 target_core_alua_lu_gp_show_attr_##_name);
1808
1809/*
1810 * lu_gp_id
1811 */
1812static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
1813 struct t10_alua_lu_gp *lu_gp,
1814 char *page)
1815{
Andy Grover6708bb22011-06-08 10:36:43 -07001816 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001817 return 0;
1818
1819 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
1820}
1821
1822static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
1823 struct t10_alua_lu_gp *lu_gp,
1824 const char *page,
1825 size_t count)
1826{
1827 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
1828 unsigned long lu_gp_id;
1829 int ret;
1830
1831 ret = strict_strtoul(page, 0, &lu_gp_id);
1832 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001833 pr_err("strict_strtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001834 " lu_gp_id\n", ret);
1835 return -EINVAL;
1836 }
1837 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001838 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001839 " 0x0000ffff\n", lu_gp_id);
1840 return -EINVAL;
1841 }
1842
1843 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
1844 if (ret < 0)
1845 return -EINVAL;
1846
Andy Grover6708bb22011-06-08 10:36:43 -07001847 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001848 " Group: core/alua/lu_gps/%s to ID: %hu\n",
1849 config_item_name(&alua_lu_gp_cg->cg_item),
1850 lu_gp->lu_gp_id);
1851
1852 return count;
1853}
1854
1855SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
1856
1857/*
1858 * members
1859 */
1860static ssize_t target_core_alua_lu_gp_show_attr_members(
1861 struct t10_alua_lu_gp *lu_gp,
1862 char *page)
1863{
1864 struct se_device *dev;
1865 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001866 struct t10_alua_lu_gp_member *lu_gp_mem;
1867 ssize_t len = 0, cur_len;
1868 unsigned char buf[LU_GROUP_NAME_BUF];
1869
1870 memset(buf, 0, LU_GROUP_NAME_BUF);
1871
1872 spin_lock(&lu_gp->lu_gp_lock);
1873 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1874 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001875 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001876
1877 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
1878 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001879 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001880 cur_len++; /* Extra byte for NULL terminator */
1881
1882 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001883 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001884 "_members buffer\n");
1885 break;
1886 }
1887 memcpy(page+len, buf, cur_len);
1888 len += cur_len;
1889 }
1890 spin_unlock(&lu_gp->lu_gp_lock);
1891
1892 return len;
1893}
1894
1895SE_DEV_ALUA_LU_ATTR_RO(members);
1896
1897CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
1898
1899static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
1900 &target_core_alua_lu_gp_lu_gp_id.attr,
1901 &target_core_alua_lu_gp_members.attr,
1902 NULL,
1903};
1904
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001905static void target_core_alua_lu_gp_release(struct config_item *item)
1906{
1907 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1908 struct t10_alua_lu_gp, lu_gp_group);
1909
1910 core_alua_free_lu_gp(lu_gp);
1911}
1912
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001913static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001914 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001915 .show_attribute = target_core_alua_lu_gp_attr_show,
1916 .store_attribute = target_core_alua_lu_gp_attr_store,
1917};
1918
1919static struct config_item_type target_core_alua_lu_gp_cit = {
1920 .ct_item_ops = &target_core_alua_lu_gp_ops,
1921 .ct_attrs = target_core_alua_lu_gp_attrs,
1922 .ct_owner = THIS_MODULE,
1923};
1924
1925/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
1926
1927/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
1928
1929static struct config_group *target_core_alua_create_lu_gp(
1930 struct config_group *group,
1931 const char *name)
1932{
1933 struct t10_alua_lu_gp *lu_gp;
1934 struct config_group *alua_lu_gp_cg = NULL;
1935 struct config_item *alua_lu_gp_ci = NULL;
1936
1937 lu_gp = core_alua_allocate_lu_gp(name, 0);
1938 if (IS_ERR(lu_gp))
1939 return NULL;
1940
1941 alua_lu_gp_cg = &lu_gp->lu_gp_group;
1942 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
1943
1944 config_group_init_type_name(alua_lu_gp_cg, name,
1945 &target_core_alua_lu_gp_cit);
1946
Andy Grover6708bb22011-06-08 10:36:43 -07001947 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001948 " Group: core/alua/lu_gps/%s\n",
1949 config_item_name(alua_lu_gp_ci));
1950
1951 return alua_lu_gp_cg;
1952
1953}
1954
1955static void target_core_alua_drop_lu_gp(
1956 struct config_group *group,
1957 struct config_item *item)
1958{
1959 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1960 struct t10_alua_lu_gp, lu_gp_group);
1961
Andy Grover6708bb22011-06-08 10:36:43 -07001962 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001963 " Group: core/alua/lu_gps/%s, ID: %hu\n",
1964 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001965 /*
1966 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
1967 * -> target_core_alua_lu_gp_release()
1968 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001969 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001970}
1971
1972static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
1973 .make_group = &target_core_alua_create_lu_gp,
1974 .drop_item = &target_core_alua_drop_lu_gp,
1975};
1976
1977static struct config_item_type target_core_alua_lu_gps_cit = {
1978 .ct_item_ops = NULL,
1979 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
1980 .ct_owner = THIS_MODULE,
1981};
1982
1983/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
1984
1985/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
1986
1987CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
1988#define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
1989static struct target_core_alua_tg_pt_gp_attribute \
1990 target_core_alua_tg_pt_gp_##_name = \
1991 __CONFIGFS_EATTR(_name, _mode, \
1992 target_core_alua_tg_pt_gp_show_attr_##_name, \
1993 target_core_alua_tg_pt_gp_store_attr_##_name);
1994
1995#define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
1996static struct target_core_alua_tg_pt_gp_attribute \
1997 target_core_alua_tg_pt_gp_##_name = \
1998 __CONFIGFS_EATTR_RO(_name, \
1999 target_core_alua_tg_pt_gp_show_attr_##_name);
2000
2001/*
2002 * alua_access_state
2003 */
2004static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2005 struct t10_alua_tg_pt_gp *tg_pt_gp,
2006 char *page)
2007{
2008 return sprintf(page, "%d\n",
2009 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2010}
2011
2012static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2013 struct t10_alua_tg_pt_gp *tg_pt_gp,
2014 const char *page,
2015 size_t count)
2016{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002017 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002018 unsigned long tmp;
2019 int new_state, ret;
2020
Andy Grover6708bb22011-06-08 10:36:43 -07002021 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2022 pr_err("Unable to do implict ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002023 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2024 return -EINVAL;
2025 }
2026
2027 ret = strict_strtoul(page, 0, &tmp);
2028 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002029 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002030 " %s\n", page);
2031 return -EINVAL;
2032 }
2033 new_state = (int)tmp;
2034
2035 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002036 pr_err("Unable to process implict configfs ALUA"
Masanari Iida5e58b022012-02-27 23:18:15 +09002037 " transition while TPGS_IMPLICT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002038 return -EINVAL;
2039 }
2040
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002041 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002042 NULL, NULL, new_state, 0);
2043 return (!ret) ? count : -EINVAL;
2044}
2045
2046SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2047
2048/*
2049 * alua_access_status
2050 */
2051static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2052 struct t10_alua_tg_pt_gp *tg_pt_gp,
2053 char *page)
2054{
2055 return sprintf(page, "%s\n",
2056 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2057}
2058
2059static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2060 struct t10_alua_tg_pt_gp *tg_pt_gp,
2061 const char *page,
2062 size_t count)
2063{
2064 unsigned long tmp;
2065 int new_status, ret;
2066
Andy Grover6708bb22011-06-08 10:36:43 -07002067 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2068 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002069 " valid tg_pt_gp ID: %hu\n",
2070 tg_pt_gp->tg_pt_gp_valid_id);
2071 return -EINVAL;
2072 }
2073
2074 ret = strict_strtoul(page, 0, &tmp);
2075 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002076 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002077 " from %s\n", page);
2078 return -EINVAL;
2079 }
2080 new_status = (int)tmp;
2081
2082 if ((new_status != ALUA_STATUS_NONE) &&
2083 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
2084 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002085 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002086 new_status);
2087 return -EINVAL;
2088 }
2089
2090 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2091 return count;
2092}
2093
2094SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2095
2096/*
2097 * alua_access_type
2098 */
2099static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2100 struct t10_alua_tg_pt_gp *tg_pt_gp,
2101 char *page)
2102{
2103 return core_alua_show_access_type(tg_pt_gp, page);
2104}
2105
2106static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2107 struct t10_alua_tg_pt_gp *tg_pt_gp,
2108 const char *page,
2109 size_t count)
2110{
2111 return core_alua_store_access_type(tg_pt_gp, page, count);
2112}
2113
2114SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2115
2116/*
2117 * alua_write_metadata
2118 */
2119static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2120 struct t10_alua_tg_pt_gp *tg_pt_gp,
2121 char *page)
2122{
2123 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2124}
2125
2126static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2127 struct t10_alua_tg_pt_gp *tg_pt_gp,
2128 const char *page,
2129 size_t count)
2130{
2131 unsigned long tmp;
2132 int ret;
2133
2134 ret = strict_strtoul(page, 0, &tmp);
2135 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002136 pr_err("Unable to extract alua_write_metadata\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002137 return -EINVAL;
2138 }
2139
2140 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002141 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002142 " %lu\n", tmp);
2143 return -EINVAL;
2144 }
2145 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2146
2147 return count;
2148}
2149
2150SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2151
2152
2153
2154/*
2155 * nonop_delay_msecs
2156 */
2157static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2158 struct t10_alua_tg_pt_gp *tg_pt_gp,
2159 char *page)
2160{
2161 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2162
2163}
2164
2165static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2166 struct t10_alua_tg_pt_gp *tg_pt_gp,
2167 const char *page,
2168 size_t count)
2169{
2170 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2171}
2172
2173SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2174
2175/*
2176 * trans_delay_msecs
2177 */
2178static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2179 struct t10_alua_tg_pt_gp *tg_pt_gp,
2180 char *page)
2181{
2182 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2183}
2184
2185static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2186 struct t10_alua_tg_pt_gp *tg_pt_gp,
2187 const char *page,
2188 size_t count)
2189{
2190 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2191}
2192
2193SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2194
2195/*
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002196 * implict_trans_secs
2197 */
2198static ssize_t target_core_alua_tg_pt_gp_show_attr_implict_trans_secs(
2199 struct t10_alua_tg_pt_gp *tg_pt_gp,
2200 char *page)
2201{
2202 return core_alua_show_implict_trans_secs(tg_pt_gp, page);
2203}
2204
2205static ssize_t target_core_alua_tg_pt_gp_store_attr_implict_trans_secs(
2206 struct t10_alua_tg_pt_gp *tg_pt_gp,
2207 const char *page,
2208 size_t count)
2209{
2210 return core_alua_store_implict_trans_secs(tg_pt_gp, page, count);
2211}
2212
2213SE_DEV_ALUA_TG_PT_ATTR(implict_trans_secs, S_IRUGO | S_IWUSR);
2214
2215/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002216 * preferred
2217 */
2218
2219static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2220 struct t10_alua_tg_pt_gp *tg_pt_gp,
2221 char *page)
2222{
2223 return core_alua_show_preferred_bit(tg_pt_gp, page);
2224}
2225
2226static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2227 struct t10_alua_tg_pt_gp *tg_pt_gp,
2228 const char *page,
2229 size_t count)
2230{
2231 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2232}
2233
2234SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2235
2236/*
2237 * tg_pt_gp_id
2238 */
2239static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2240 struct t10_alua_tg_pt_gp *tg_pt_gp,
2241 char *page)
2242{
Andy Grover6708bb22011-06-08 10:36:43 -07002243 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002244 return 0;
2245
2246 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2247}
2248
2249static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2250 struct t10_alua_tg_pt_gp *tg_pt_gp,
2251 const char *page,
2252 size_t count)
2253{
2254 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2255 unsigned long tg_pt_gp_id;
2256 int ret;
2257
2258 ret = strict_strtoul(page, 0, &tg_pt_gp_id);
2259 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002260 pr_err("strict_strtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002261 " tg_pt_gp_id\n", ret);
2262 return -EINVAL;
2263 }
2264 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002265 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002266 " 0x0000ffff\n", tg_pt_gp_id);
2267 return -EINVAL;
2268 }
2269
2270 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2271 if (ret < 0)
2272 return -EINVAL;
2273
Andy Grover6708bb22011-06-08 10:36:43 -07002274 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002275 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2276 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2277 tg_pt_gp->tg_pt_gp_id);
2278
2279 return count;
2280}
2281
2282SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2283
2284/*
2285 * members
2286 */
2287static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2288 struct t10_alua_tg_pt_gp *tg_pt_gp,
2289 char *page)
2290{
2291 struct se_port *port;
2292 struct se_portal_group *tpg;
2293 struct se_lun *lun;
2294 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2295 ssize_t len = 0, cur_len;
2296 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2297
2298 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2299
2300 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2301 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2302 tg_pt_gp_mem_list) {
2303 port = tg_pt_gp_mem->tg_pt;
2304 tpg = port->sep_tpg;
2305 lun = port->sep_lun;
2306
2307 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002308 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2309 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2310 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002311 config_item_name(&lun->lun_group.cg_item));
2312 cur_len++; /* Extra byte for NULL terminator */
2313
2314 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002315 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002316 "_members buffer\n");
2317 break;
2318 }
2319 memcpy(page+len, buf, cur_len);
2320 len += cur_len;
2321 }
2322 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2323
2324 return len;
2325}
2326
2327SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2328
2329CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2330 tg_pt_gp_group);
2331
2332static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2333 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2334 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2335 &target_core_alua_tg_pt_gp_alua_access_type.attr,
2336 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2337 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2338 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002339 &target_core_alua_tg_pt_gp_implict_trans_secs.attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002340 &target_core_alua_tg_pt_gp_preferred.attr,
2341 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2342 &target_core_alua_tg_pt_gp_members.attr,
2343 NULL,
2344};
2345
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002346static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2347{
2348 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2349 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2350
2351 core_alua_free_tg_pt_gp(tg_pt_gp);
2352}
2353
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002354static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002355 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002356 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2357 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2358};
2359
2360static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2361 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2362 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2363 .ct_owner = THIS_MODULE,
2364};
2365
2366/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2367
2368/* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2369
2370static struct config_group *target_core_alua_create_tg_pt_gp(
2371 struct config_group *group,
2372 const char *name)
2373{
2374 struct t10_alua *alua = container_of(group, struct t10_alua,
2375 alua_tg_pt_gps_group);
2376 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002377 struct config_group *alua_tg_pt_gp_cg = NULL;
2378 struct config_item *alua_tg_pt_gp_ci = NULL;
2379
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002380 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002381 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002382 return NULL;
2383
2384 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2385 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2386
2387 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2388 &target_core_alua_tg_pt_gp_cit);
2389
Andy Grover6708bb22011-06-08 10:36:43 -07002390 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002391 " Group: alua/tg_pt_gps/%s\n",
2392 config_item_name(alua_tg_pt_gp_ci));
2393
2394 return alua_tg_pt_gp_cg;
2395}
2396
2397static void target_core_alua_drop_tg_pt_gp(
2398 struct config_group *group,
2399 struct config_item *item)
2400{
2401 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2402 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2403
Andy Grover6708bb22011-06-08 10:36:43 -07002404 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002405 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2406 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002407 /*
2408 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2409 * -> target_core_alua_tg_pt_gp_release().
2410 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002411 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002412}
2413
2414static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2415 .make_group = &target_core_alua_create_tg_pt_gp,
2416 .drop_item = &target_core_alua_drop_tg_pt_gp,
2417};
2418
2419static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2420 .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
2421 .ct_owner = THIS_MODULE,
2422};
2423
2424/* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2425
2426/* Start functions for struct config_item_type target_core_alua_cit */
2427
2428/*
2429 * target_core_alua_cit is a ConfigFS group that lives under
2430 * /sys/kernel/config/target/core/alua. There are default groups
2431 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2432 * target_core_alua_cit in target_core_init_configfs() below.
2433 */
2434static struct config_item_type target_core_alua_cit = {
2435 .ct_item_ops = NULL,
2436 .ct_attrs = NULL,
2437 .ct_owner = THIS_MODULE,
2438};
2439
2440/* End functions for struct config_item_type target_core_alua_cit */
2441
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002442/* Start functions for struct config_item_type target_core_stat_cit */
2443
2444static struct config_group *target_core_stat_mkdir(
2445 struct config_group *group,
2446 const char *name)
2447{
2448 return ERR_PTR(-ENOSYS);
2449}
2450
2451static void target_core_stat_rmdir(
2452 struct config_group *group,
2453 struct config_item *item)
2454{
2455 return;
2456}
2457
2458static struct configfs_group_operations target_core_stat_group_ops = {
2459 .make_group = &target_core_stat_mkdir,
2460 .drop_item = &target_core_stat_rmdir,
2461};
2462
2463static struct config_item_type target_core_stat_cit = {
2464 .ct_group_ops = &target_core_stat_group_ops,
2465 .ct_owner = THIS_MODULE,
2466};
2467
2468/* End functions for struct config_item_type target_core_stat_cit */
2469
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002470/* Start functions for struct config_item_type target_core_hba_cit */
2471
2472static struct config_group *target_core_make_subdev(
2473 struct config_group *group,
2474 const char *name)
2475{
2476 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002477 struct se_subsystem_api *t;
2478 struct config_item *hba_ci = &group->cg_item;
2479 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002480 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002481 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002482 struct config_group *dev_stat_grp = NULL;
2483 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002484
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002485 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2486 if (ret)
2487 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002488 /*
2489 * Locate the struct se_subsystem_api from parent's struct se_hba.
2490 */
2491 t = hba->transport;
2492
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002493 dev = target_alloc_device(hba, name);
2494 if (!dev)
2495 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002496
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002497 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002498
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002499 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002500 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002501 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002502 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002503
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002504 config_group_init_type_name(dev_cg, name, &target_core_dev_cit);
2505 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002506 &target_core_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002507 config_group_init_type_name(&dev->dev_pr_group, "pr",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002508 &target_core_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002509 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002510 &target_core_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002511 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002512 "alua", &target_core_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002513 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002514 "statistics", &target_core_stat_cit);
2515
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002516 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2517 dev_cg->default_groups[1] = &dev->dev_pr_group;
2518 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2519 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2520 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002521 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002522 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002523 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002524 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002525 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002526 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002527 goto out_free_dev_cg_default_groups;
2528 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002529
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002530 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002531 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002532 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002533 if (!tg_pt_gp_cg->default_groups) {
2534 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002535 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002536 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002537 }
2538
2539 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2540 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2541 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2542 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002543 /*
2544 * Add core/$HBA/$DEV/statistics/ default groups
2545 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002546 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002547 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002548 GFP_KERNEL);
2549 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002550 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002551 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002552 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002553 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002554
2555 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002556 return dev_cg;
2557
2558out_free_tg_pt_gp_cg_default_groups:
2559 kfree(tg_pt_gp_cg->default_groups);
2560out_free_tg_pt_gp:
2561 core_alua_free_tg_pt_gp(tg_pt_gp);
2562out_free_dev_cg_default_groups:
2563 kfree(dev_cg->default_groups);
2564out_free_device:
2565 target_free_device(dev);
2566out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002567 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002568 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002569}
2570
2571static void target_core_drop_subdev(
2572 struct config_group *group,
2573 struct config_item *item)
2574{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002575 struct config_group *dev_cg = to_config_group(item);
2576 struct se_device *dev =
2577 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002578 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002579 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002580 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002581 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002582
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002583 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002584
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002585 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002586
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002587 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002588 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2589 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2590 dev_stat_grp->default_groups[i] = NULL;
2591 config_item_put(df_item);
2592 }
2593 kfree(dev_stat_grp->default_groups);
2594
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002595 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002596 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2597 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2598 tg_pt_gp_cg->default_groups[i] = NULL;
2599 config_item_put(df_item);
2600 }
2601 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002602 /*
2603 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2604 * directly from target_core_alua_tg_pt_gp_release().
2605 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002606 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002607
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002608 for (i = 0; dev_cg->default_groups[i]; i++) {
2609 df_item = &dev_cg->default_groups[i]->cg_item;
2610 dev_cg->default_groups[i] = NULL;
2611 config_item_put(df_item);
2612 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002613 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002614 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002615 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002616 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002617 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002618}
2619
2620static struct configfs_group_operations target_core_hba_group_ops = {
2621 .make_group = target_core_make_subdev,
2622 .drop_item = target_core_drop_subdev,
2623};
2624
2625CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2626#define SE_HBA_ATTR(_name, _mode) \
2627static struct target_core_hba_attribute \
2628 target_core_hba_##_name = \
2629 __CONFIGFS_EATTR(_name, _mode, \
2630 target_core_hba_show_attr_##_name, \
2631 target_core_hba_store_attr_##_name);
2632
2633#define SE_HBA_ATTR_RO(_name) \
2634static struct target_core_hba_attribute \
2635 target_core_hba_##_name = \
2636 __CONFIGFS_EATTR_RO(_name, \
2637 target_core_hba_show_attr_##_name);
2638
2639static ssize_t target_core_hba_show_attr_hba_info(
2640 struct se_hba *hba,
2641 char *page)
2642{
2643 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2644 hba->hba_id, hba->transport->name,
2645 TARGET_CORE_CONFIGFS_VERSION);
2646}
2647
2648SE_HBA_ATTR_RO(hba_info);
2649
2650static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2651 char *page)
2652{
2653 int hba_mode = 0;
2654
2655 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2656 hba_mode = 1;
2657
2658 return sprintf(page, "%d\n", hba_mode);
2659}
2660
2661static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2662 const char *page, size_t count)
2663{
2664 struct se_subsystem_api *transport = hba->transport;
2665 unsigned long mode_flag;
2666 int ret;
2667
2668 if (transport->pmode_enable_hba == NULL)
2669 return -EINVAL;
2670
2671 ret = strict_strtoul(page, 0, &mode_flag);
2672 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002673 pr_err("Unable to extract hba mode flag: %d\n", ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002674 return -EINVAL;
2675 }
2676
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002677 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07002678 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002679 return -EINVAL;
2680 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002681
2682 ret = transport->pmode_enable_hba(hba, mode_flag);
2683 if (ret < 0)
2684 return -EINVAL;
2685 if (ret > 0)
2686 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2687 else if (ret == 0)
2688 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2689
2690 return count;
2691}
2692
2693SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2694
2695CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2696
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002697static void target_core_hba_release(struct config_item *item)
2698{
2699 struct se_hba *hba = container_of(to_config_group(item),
2700 struct se_hba, hba_group);
2701 core_delete_hba(hba);
2702}
2703
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002704static struct configfs_attribute *target_core_hba_attrs[] = {
2705 &target_core_hba_hba_info.attr,
2706 &target_core_hba_hba_mode.attr,
2707 NULL,
2708};
2709
2710static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002711 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002712 .show_attribute = target_core_hba_attr_show,
2713 .store_attribute = target_core_hba_attr_store,
2714};
2715
2716static struct config_item_type target_core_hba_cit = {
2717 .ct_item_ops = &target_core_hba_item_ops,
2718 .ct_group_ops = &target_core_hba_group_ops,
2719 .ct_attrs = target_core_hba_attrs,
2720 .ct_owner = THIS_MODULE,
2721};
2722
2723static struct config_group *target_core_call_addhbatotarget(
2724 struct config_group *group,
2725 const char *name)
2726{
2727 char *se_plugin_str, *str, *str2;
2728 struct se_hba *hba;
2729 char buf[TARGET_CORE_NAME_MAX_LEN];
2730 unsigned long plugin_dep_id = 0;
2731 int ret;
2732
2733 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07002734 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07002735 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002736 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
2737 TARGET_CORE_NAME_MAX_LEN);
2738 return ERR_PTR(-ENAMETOOLONG);
2739 }
2740 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
2741
2742 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07002743 if (!str) {
2744 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002745 return ERR_PTR(-EINVAL);
2746 }
2747 se_plugin_str = buf;
2748 /*
2749 * Special case for subsystem plugins that have "_" in their names.
2750 * Namely rd_direct and rd_mcp..
2751 */
2752 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07002753 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002754 *str2 = '\0'; /* Terminate for *se_plugin_str */
2755 str2++; /* Skip to start of plugin dependent ID */
2756 str = str2;
2757 } else {
2758 *str = '\0'; /* Terminate for *se_plugin_str */
2759 str++; /* Skip to start of plugin dependent ID */
2760 }
2761
2762 ret = strict_strtoul(str, 0, &plugin_dep_id);
2763 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002764 pr_err("strict_strtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002765 " plugin_dep_id\n", ret);
2766 return ERR_PTR(-EINVAL);
2767 }
2768 /*
2769 * Load up TCM subsystem plugins if they have not already been loaded.
2770 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07002771 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002772
2773 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
2774 if (IS_ERR(hba))
2775 return ERR_CAST(hba);
2776
2777 config_group_init_type_name(&hba->hba_group, name,
2778 &target_core_hba_cit);
2779
2780 return &hba->hba_group;
2781}
2782
2783static void target_core_call_delhbafromtarget(
2784 struct config_group *group,
2785 struct config_item *item)
2786{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002787 /*
2788 * core_delete_hba() is called from target_core_hba_item_ops->release()
2789 * -> target_core_hba_release()
2790 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002791 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002792}
2793
2794static struct configfs_group_operations target_core_group_ops = {
2795 .make_group = target_core_call_addhbatotarget,
2796 .drop_item = target_core_call_delhbafromtarget,
2797};
2798
2799static struct config_item_type target_core_cit = {
2800 .ct_item_ops = NULL,
2801 .ct_group_ops = &target_core_group_ops,
2802 .ct_attrs = NULL,
2803 .ct_owner = THIS_MODULE,
2804};
2805
2806/* Stop functions for struct config_item_type target_core_hba_cit */
2807
Axel Lin54550fa2011-03-14 04:06:09 -07002808static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002809{
2810 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
2811 struct config_group *lu_gp_cg = NULL;
2812 struct configfs_subsystem *subsys;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002813 struct t10_alua_lu_gp *lu_gp;
2814 int ret;
2815
Andy Grover6708bb22011-06-08 10:36:43 -07002816 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002817 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
2818 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
2819
2820 subsys = target_core_subsystem[0];
2821 config_group_init(&subsys->su_group);
2822 mutex_init(&subsys->su_mutex);
2823
Andy Grovere3d6f902011-07-19 08:55:10 +00002824 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002825 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00002826 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002827 /*
2828 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
2829 * and ALUA Logical Unit Group and Target Port Group infrastructure.
2830 */
2831 target_cg = &subsys->su_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002832 target_cg->default_groups = kmalloc(sizeof(struct config_group) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002833 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002834 if (!target_cg->default_groups) {
2835 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002836 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002837 goto out_global;
2838 }
2839
Andy Grovere3d6f902011-07-19 08:55:10 +00002840 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002841 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002842 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002843 target_cg->default_groups[1] = NULL;
2844 /*
2845 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
2846 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002847 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002848 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002849 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002850 if (!hba_cg->default_groups) {
2851 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002852 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002853 goto out_global;
2854 }
Andy Grovere3d6f902011-07-19 08:55:10 +00002855 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002856 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002857 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002858 hba_cg->default_groups[1] = NULL;
2859 /*
2860 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
2861 * groups under /sys/kernel/config/target/core/alua/
2862 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002863 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002864 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002865 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002866 if (!alua_cg->default_groups) {
2867 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002868 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002869 goto out_global;
2870 }
2871
Andy Grovere3d6f902011-07-19 08:55:10 +00002872 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002873 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00002874 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002875 alua_cg->default_groups[1] = NULL;
2876 /*
2877 * Add core/alua/lu_gps/default_lu_gp
2878 */
2879 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002880 if (IS_ERR(lu_gp)) {
2881 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002882 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002883 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002884
Andy Grovere3d6f902011-07-19 08:55:10 +00002885 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002886 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002887 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002888 if (!lu_gp_cg->default_groups) {
2889 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02002890 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002891 goto out_global;
2892 }
2893
2894 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
2895 &target_core_alua_lu_gp_cit);
2896 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
2897 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00002898 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002899 /*
2900 * Register the target_core_mod subsystem with configfs.
2901 */
2902 ret = configfs_register_subsystem(subsys);
2903 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002904 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002905 ret, subsys->su_group.cg_item.ci_namebuf);
2906 goto out_global;
2907 }
Andy Grover6708bb22011-06-08 10:36:43 -07002908 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002909 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
2910 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
2911 /*
2912 * Register built-in RAMDISK subsystem logic for virtual LUN 0
2913 */
2914 ret = rd_module_init();
2915 if (ret < 0)
2916 goto out;
2917
Roland Dreier0d0f9df2012-10-31 09:16:44 -07002918 ret = core_dev_setup_virtual_lun0();
2919 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002920 goto out;
2921
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002922 return 0;
2923
2924out:
2925 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002926 core_dev_release_virtual_lun0();
2927 rd_module_exit();
2928out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00002929 if (default_lu_gp) {
2930 core_alua_free_lu_gp(default_lu_gp);
2931 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002932 }
2933 if (lu_gp_cg)
2934 kfree(lu_gp_cg->default_groups);
2935 if (alua_cg)
2936 kfree(alua_cg->default_groups);
2937 if (hba_cg)
2938 kfree(hba_cg->default_groups);
2939 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00002940 release_se_kmem_caches();
2941 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002942}
2943
Axel Lin54550fa2011-03-14 04:06:09 -07002944static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002945{
2946 struct configfs_subsystem *subsys;
2947 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
2948 struct config_item *item;
2949 int i;
2950
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002951 subsys = target_core_subsystem[0];
2952
Andy Grovere3d6f902011-07-19 08:55:10 +00002953 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002954 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
2955 item = &lu_gp_cg->default_groups[i]->cg_item;
2956 lu_gp_cg->default_groups[i] = NULL;
2957 config_item_put(item);
2958 }
2959 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08002960 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002961
Andy Grovere3d6f902011-07-19 08:55:10 +00002962 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002963 for (i = 0; alua_cg->default_groups[i]; i++) {
2964 item = &alua_cg->default_groups[i]->cg_item;
2965 alua_cg->default_groups[i] = NULL;
2966 config_item_put(item);
2967 }
2968 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08002969 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002970
Andy Grovere3d6f902011-07-19 08:55:10 +00002971 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002972 for (i = 0; hba_cg->default_groups[i]; i++) {
2973 item = &hba_cg->default_groups[i]->cg_item;
2974 hba_cg->default_groups[i] = NULL;
2975 config_item_put(item);
2976 }
2977 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08002978 hba_cg->default_groups = NULL;
2979 /*
2980 * We expect subsys->su_group.default_groups to be released
2981 * by configfs subsystem provider logic..
2982 */
2983 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002984 kfree(subsys->su_group.default_groups);
2985
Andy Grovere3d6f902011-07-19 08:55:10 +00002986 core_alua_free_lu_gp(default_lu_gp);
2987 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08002988
Andy Grover6708bb22011-06-08 10:36:43 -07002989 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002990 " Infrastructure\n");
2991
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002992 core_dev_release_virtual_lun0();
2993 rd_module_exit();
Andy Grovere3d6f902011-07-19 08:55:10 +00002994 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002995}
2996
2997MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
2998MODULE_AUTHOR("nab@Linux-iSCSI.org");
2999MODULE_LICENSE("GPL");
3000
3001module_init(target_core_init_configfs);
3002module_exit(target_core_exit_configfs);