blob: affe4c393ebcbbdbd15d049208548470d7e71a1f [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_configfs.c
3 *
4 * This file contains ConfigFS logic for the Generic Target Engine project.
5 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07006 * (c) Copyright 2008-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08007 *
8 * Nicholas A. Bellinger <nab@kernel.org>
9 *
10 * based on configfs Copyright (C) 2005 Oracle. All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 ****************************************************************************/
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080025#include <generated/utsrelease.h>
26#include <linux/utsname.h>
27#include <linux/init.h>
28#include <linux/fs.h>
29#include <linux/namei.h>
30#include <linux/slab.h>
31#include <linux/types.h>
32#include <linux/delay.h>
33#include <linux/unistd.h>
34#include <linux/string.h>
35#include <linux/parser.h>
36#include <linux/syscalls.h>
37#include <linux/configfs.h>
Andy Grovere3d6f902011-07-19 08:55:10 +000038#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080039
40#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050041#include <target/target_core_backend.h>
42#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080043
Christoph Hellwige26d99a2011-11-14 12:30:30 -050044#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080045#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080046#include "target_core_pr.h"
47#include "target_core_rd.h"
Nicholas Bellingerf99715a2013-08-22 12:48:53 -070048#include "target_core_xcopy.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080049
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080050#define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
Christoph Hellwig0a06d432015-05-10 18:14:56 +020051static void target_core_setup_##_name##_cit(struct target_backend *tb) \
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080052{ \
Christoph Hellwig0a06d432015-05-10 18:14:56 +020053 struct config_item_type *cit = &tb->tb_##_name##_cit; \
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080054 \
55 cit->ct_item_ops = _item_ops; \
56 cit->ct_group_ops = _group_ops; \
57 cit->ct_attrs = _attrs; \
Christoph Hellwig0a06d432015-05-10 18:14:56 +020058 cit->ct_owner = tb->ops->owner; \
59 pr_debug("Setup generic %s\n", __stringify(_name)); \
60}
61
62#define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
63static void target_core_setup_##_name##_cit(struct target_backend *tb) \
64{ \
65 struct config_item_type *cit = &tb->tb_##_name##_cit; \
66 \
67 cit->ct_item_ops = _item_ops; \
68 cit->ct_group_ops = _group_ops; \
69 cit->ct_attrs = tb->ops->tb_##_name##_attrs; \
70 cit->ct_owner = tb->ops->owner; \
Nicholas Bellinger73112ed2014-11-27 13:59:20 -080071 pr_debug("Setup generic %s\n", __stringify(_name)); \
72}
73
Andy Grovere3d6f902011-07-19 08:55:10 +000074extern struct t10_alua_lu_gp *default_lu_gp;
75
Roland Dreierd0f474e2012-01-12 10:41:18 -080076static LIST_HEAD(g_tf_list);
77static DEFINE_MUTEX(g_tf_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078
Andy Grovere3d6f902011-07-19 08:55:10 +000079static struct config_group target_core_hbagroup;
80static struct config_group alua_group;
81static struct config_group alua_lu_gps_group;
82
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083static inline struct se_hba *
84item_to_hba(struct config_item *item)
85{
86 return container_of(to_config_group(item), struct se_hba, hba_group);
87}
88
89/*
90 * Attributes for /sys/kernel/config/target/
91 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +020092static ssize_t target_core_item_version_show(struct config_item *item,
93 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080094{
95 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
Christoph Hellwigce8dd252015-06-19 15:14:39 +020096 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080097 utsname()->sysname, utsname()->machine);
98}
99
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200100CONFIGFS_ATTR_RO(target_core_item_, version);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800101
102static struct target_fabric_configfs *target_core_get_fabric(
103 const char *name)
104{
105 struct target_fabric_configfs *tf;
106
Andy Grover6708bb22011-06-08 10:36:43 -0700107 if (!name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800108 return NULL;
109
110 mutex_lock(&g_tf_lock);
111 list_for_each_entry(tf, &g_tf_list, tf_list) {
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200112 if (!strcmp(tf->tf_ops->name, name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800113 atomic_inc(&tf->tf_access_cnt);
114 mutex_unlock(&g_tf_lock);
115 return tf;
116 }
117 }
118 mutex_unlock(&g_tf_lock);
119
120 return NULL;
121}
122
123/*
124 * Called from struct target_core_group_ops->make_group()
125 */
126static struct config_group *target_core_register_fabric(
127 struct config_group *group,
128 const char *name)
129{
130 struct target_fabric_configfs *tf;
131 int ret;
132
Andy Grover6708bb22011-06-08 10:36:43 -0700133 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800134 " %s\n", group, name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800135
136 tf = target_core_get_fabric(name);
Andy Grover6708bb22011-06-08 10:36:43 -0700137 if (!tf) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700138 pr_debug("target_core_register_fabric() trying autoload for %s\n",
139 name);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800140
141 /*
142 * Below are some hardcoded request_module() calls to automatically
143 * local fabric modules when the following is called:
144 *
145 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
146 *
147 * Note that this does not limit which TCM fabric module can be
148 * registered, but simply provids auto loading logic for modules with
149 * mkdir(2) system calls with known TCM fabric modules.
150 */
151
152 if (!strncmp(name, "iscsi", 5)) {
153 /*
154 * Automatically load the LIO Target fabric module when the
155 * following is called:
156 *
157 * mkdir -p $CONFIGFS/target/iscsi
158 */
159 ret = request_module("iscsi_target_mod");
160 if (ret < 0) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700161 pr_debug("request_module() failed for"
162 " iscsi_target_mod.ko: %d\n", ret);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800163 return ERR_PTR(-EINVAL);
164 }
165 } else if (!strncmp(name, "loopback", 8)) {
166 /*
167 * Automatically load the tcm_loop fabric module when the
168 * following is called:
169 *
170 * mkdir -p $CONFIGFS/target/loopback
171 */
172 ret = request_module("tcm_loop");
173 if (ret < 0) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700174 pr_debug("request_module() failed for"
175 " tcm_loop.ko: %d\n", ret);
Roland Dreiere7b7af62014-11-14 12:54:36 -0800176 return ERR_PTR(-EINVAL);
177 }
178 }
179
180 tf = target_core_get_fabric(name);
181 }
182
183 if (!tf) {
Nicholas Bellinger62554912015-03-20 11:36:50 -0700184 pr_debug("target_core_get_fabric() failed for %s\n",
185 name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800186 return ERR_PTR(-EINVAL);
187 }
Andy Grover6708bb22011-06-08 10:36:43 -0700188 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200189 " %s\n", tf->tf_ops->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800190 /*
191 * On a successful target_core_get_fabric() look, the returned
192 * struct target_fabric_configfs *tf will contain a usage reference.
193 */
Andy Grover6708bb22011-06-08 10:36:43 -0700194 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200195 &tf->tf_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800196
197 tf->tf_group.default_groups = tf->tf_default_groups;
198 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
199 tf->tf_group.default_groups[1] = NULL;
200
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200201 config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800202 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200203 &tf->tf_discovery_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204
Andy Grover6708bb22011-06-08 10:36:43 -0700205 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800206 " %s\n", tf->tf_group.cg_item.ci_name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800207 return &tf->tf_group;
208}
209
210/*
211 * Called from struct target_core_group_ops->drop_item()
212 */
213static void target_core_deregister_fabric(
214 struct config_group *group,
215 struct config_item *item)
216{
217 struct target_fabric_configfs *tf = container_of(
218 to_config_group(item), struct target_fabric_configfs, tf_group);
219 struct config_group *tf_group;
220 struct config_item *df_item;
221 int i;
222
Andy Grover6708bb22011-06-08 10:36:43 -0700223 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800224 " tf list\n", config_item_name(item));
225
Andy Grover6708bb22011-06-08 10:36:43 -0700226 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200227 " %s\n", tf->tf_ops->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800228 atomic_dec(&tf->tf_access_cnt);
229
Andy Grover6708bb22011-06-08 10:36:43 -0700230 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800231 " %s\n", config_item_name(item));
232
233 tf_group = &tf->tf_group;
234 for (i = 0; tf_group->default_groups[i]; i++) {
235 df_item = &tf_group->default_groups[i]->cg_item;
236 tf_group->default_groups[i] = NULL;
237 config_item_put(df_item);
238 }
239 config_item_put(item);
240}
241
242static struct configfs_group_operations target_core_fabric_group_ops = {
243 .make_group = &target_core_register_fabric,
244 .drop_item = &target_core_deregister_fabric,
245};
246
247/*
248 * All item attributes appearing in /sys/kernel/target/ appear here.
249 */
250static struct configfs_attribute *target_core_fabric_item_attrs[] = {
251 &target_core_item_attr_version,
252 NULL,
253};
254
255/*
256 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
257 */
258static struct config_item_type target_core_fabrics_item = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800259 .ct_group_ops = &target_core_fabric_group_ops,
260 .ct_attrs = target_core_fabric_item_attrs,
261 .ct_owner = THIS_MODULE,
262};
263
264static struct configfs_subsystem target_core_fabrics = {
265 .su_group = {
266 .cg_item = {
267 .ci_namebuf = "target",
268 .ci_type = &target_core_fabrics_item,
269 },
270 },
271};
272
Christoph Hellwigd588cf82015-05-03 08:50:52 +0200273int target_depend_item(struct config_item *item)
274{
275 return configfs_depend_item(&target_core_fabrics, item);
276}
277EXPORT_SYMBOL(target_depend_item);
278
279void target_undepend_item(struct config_item *item)
280{
Krzysztof Opasiak9a9e3412015-12-11 16:06:09 +0100281 return configfs_undepend_item(item);
Christoph Hellwigd588cf82015-05-03 08:50:52 +0200282}
283EXPORT_SYMBOL(target_undepend_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800284
285/*##############################################################################
286// Start functions called by external Target Fabrics Modules
287//############################################################################*/
288
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200289static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800290{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200291 if (!tfo->name) {
292 pr_err("Missing tfo->name\n");
293 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800294 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200295 if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700296 pr_err("Passed name: %s exceeds TARGET_FABRIC"
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200297 "_NAME_SIZE\n", tfo->name);
298 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800299 }
Andy Grover6708bb22011-06-08 10:36:43 -0700300 if (!tfo->get_fabric_name) {
301 pr_err("Missing tfo->get_fabric_name()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800302 return -EINVAL;
303 }
Andy Grover6708bb22011-06-08 10:36:43 -0700304 if (!tfo->tpg_get_wwn) {
305 pr_err("Missing tfo->tpg_get_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800306 return -EINVAL;
307 }
Andy Grover6708bb22011-06-08 10:36:43 -0700308 if (!tfo->tpg_get_tag) {
309 pr_err("Missing tfo->tpg_get_tag()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800310 return -EINVAL;
311 }
Andy Grover6708bb22011-06-08 10:36:43 -0700312 if (!tfo->tpg_check_demo_mode) {
313 pr_err("Missing tfo->tpg_check_demo_mode()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800314 return -EINVAL;
315 }
Andy Grover6708bb22011-06-08 10:36:43 -0700316 if (!tfo->tpg_check_demo_mode_cache) {
317 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800318 return -EINVAL;
319 }
Andy Grover6708bb22011-06-08 10:36:43 -0700320 if (!tfo->tpg_check_demo_mode_write_protect) {
321 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800322 return -EINVAL;
323 }
Andy Grover6708bb22011-06-08 10:36:43 -0700324 if (!tfo->tpg_check_prod_mode_write_protect) {
325 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800326 return -EINVAL;
327 }
Andy Grover6708bb22011-06-08 10:36:43 -0700328 if (!tfo->tpg_get_inst_index) {
329 pr_err("Missing tfo->tpg_get_inst_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800330 return -EINVAL;
331 }
Christoph Hellwig35462972011-05-31 23:56:57 -0400332 if (!tfo->release_cmd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700333 pr_err("Missing tfo->release_cmd()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800334 return -EINVAL;
335 }
Andy Grover6708bb22011-06-08 10:36:43 -0700336 if (!tfo->shutdown_session) {
337 pr_err("Missing tfo->shutdown_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800338 return -EINVAL;
339 }
Andy Grover6708bb22011-06-08 10:36:43 -0700340 if (!tfo->close_session) {
341 pr_err("Missing tfo->close_session()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800342 return -EINVAL;
343 }
Andy Grover6708bb22011-06-08 10:36:43 -0700344 if (!tfo->sess_get_index) {
345 pr_err("Missing tfo->sess_get_index()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800346 return -EINVAL;
347 }
Andy Grover6708bb22011-06-08 10:36:43 -0700348 if (!tfo->write_pending) {
349 pr_err("Missing tfo->write_pending()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800350 return -EINVAL;
351 }
Andy Grover6708bb22011-06-08 10:36:43 -0700352 if (!tfo->write_pending_status) {
353 pr_err("Missing tfo->write_pending_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800354 return -EINVAL;
355 }
Andy Grover6708bb22011-06-08 10:36:43 -0700356 if (!tfo->set_default_node_attributes) {
357 pr_err("Missing tfo->set_default_node_attributes()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800358 return -EINVAL;
359 }
Andy Grover6708bb22011-06-08 10:36:43 -0700360 if (!tfo->get_cmd_state) {
361 pr_err("Missing tfo->get_cmd_state()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800362 return -EINVAL;
363 }
Andy Grover6708bb22011-06-08 10:36:43 -0700364 if (!tfo->queue_data_in) {
365 pr_err("Missing tfo->queue_data_in()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800366 return -EINVAL;
367 }
Andy Grover6708bb22011-06-08 10:36:43 -0700368 if (!tfo->queue_status) {
369 pr_err("Missing tfo->queue_status()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800370 return -EINVAL;
371 }
Andy Grover6708bb22011-06-08 10:36:43 -0700372 if (!tfo->queue_tm_rsp) {
373 pr_err("Missing tfo->queue_tm_rsp()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800374 return -EINVAL;
375 }
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700376 if (!tfo->aborted_task) {
377 pr_err("Missing tfo->aborted_task()\n");
378 return -EINVAL;
379 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800380 /*
381 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
382 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
383 * target_core_fabric_configfs.c WWN+TPG group context code.
384 */
Andy Grover6708bb22011-06-08 10:36:43 -0700385 if (!tfo->fabric_make_wwn) {
386 pr_err("Missing tfo->fabric_make_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800387 return -EINVAL;
388 }
Andy Grover6708bb22011-06-08 10:36:43 -0700389 if (!tfo->fabric_drop_wwn) {
390 pr_err("Missing tfo->fabric_drop_wwn()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800391 return -EINVAL;
392 }
Andy Grover6708bb22011-06-08 10:36:43 -0700393 if (!tfo->fabric_make_tpg) {
394 pr_err("Missing tfo->fabric_make_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800395 return -EINVAL;
396 }
Andy Grover6708bb22011-06-08 10:36:43 -0700397 if (!tfo->fabric_drop_tpg) {
398 pr_err("Missing tfo->fabric_drop_tpg()\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800399 return -EINVAL;
400 }
401
402 return 0;
403}
404
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200405int target_register_template(const struct target_core_fabric_ops *fo)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406{
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200407 struct target_fabric_configfs *tf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800408 int ret;
409
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200410 ret = target_fabric_tf_ops_check(fo);
411 if (ret)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800412 return ret;
413
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200414 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700415 if (!tf) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200416 pr_err("%s: could not allocate memory!\n", __func__);
417 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800418 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200419
420 INIT_LIST_HEAD(&tf->tf_list);
421 atomic_set(&tf->tf_access_cnt, 0);
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200422 tf->tf_ops = fo;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200423 target_fabric_setup_cits(tf);
424
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800425 mutex_lock(&g_tf_lock);
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200426 list_add_tail(&tf->tf_list, &g_tf_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800427 mutex_unlock(&g_tf_lock);
428
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200429 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430}
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200431EXPORT_SYMBOL(target_register_template);
432
433void target_unregister_template(const struct target_core_fabric_ops *fo)
434{
435 struct target_fabric_configfs *t;
436
437 mutex_lock(&g_tf_lock);
438 list_for_each_entry(t, &g_tf_list, tf_list) {
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +0200439 if (!strcmp(t->tf_ops->name, fo->name)) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200440 BUG_ON(atomic_read(&t->tf_access_cnt));
441 list_del(&t->tf_list);
Nicholas Bellinger94509182015-07-29 22:27:13 -0700442 mutex_unlock(&g_tf_lock);
443 /*
444 * Wait for any outstanding fabric se_deve_entry->rcu_head
445 * callbacks to complete post kfree_rcu(), before allowing
446 * fabric driver unload of TFO->module to proceed.
447 */
448 rcu_barrier();
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200449 kfree(t);
Nicholas Bellinger94509182015-07-29 22:27:13 -0700450 return;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200451 }
452 }
453 mutex_unlock(&g_tf_lock);
454}
455EXPORT_SYMBOL(target_unregister_template);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800456
457/*##############################################################################
458// Stop functions called by external Target Fabrics Modules
459//############################################################################*/
460
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200461static inline struct se_dev_attrib *to_attrib(struct config_item *item)
462{
463 return container_of(to_config_group(item), struct se_dev_attrib,
464 da_group);
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200465}
466
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200467/* Start functions for struct config_item_type tb_dev_attrib_cit */
468#define DEF_CONFIGFS_ATTRIB_SHOW(_name) \
469static ssize_t _name##_show(struct config_item *item, char *page) \
470{ \
471 return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
472}
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200473
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200474DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
475DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
476DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
477DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
478DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
479DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
480DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
481DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
482DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
483DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
484DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
485DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
486DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
487DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_format);
488DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
489DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
490DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
491DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
492DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
493DEF_CONFIGFS_ATTRIB_SHOW(block_size);
494DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
495DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
496DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
497DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
498DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
499DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
500DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
501DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
Jamie Pocase6f416332015-11-29 14:44:57 -0800502DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200503DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
504
505#define DEF_CONFIGFS_ATTRIB_STORE_U32(_name) \
506static ssize_t _name##_store(struct config_item *item, const char *page,\
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200507 size_t count) \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200508{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200509 struct se_dev_attrib *da = to_attrib(item); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200510 u32 val; \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200511 int ret; \
512 \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200513 ret = kstrtou32(page, 0, &val); \
514 if (ret < 0) \
515 return ret; \
516 da->_name = val; \
517 return count; \
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200518}
519
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200520DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
521DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
522DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
523DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
524DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200525
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200526#define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name) \
527static ssize_t _name##_store(struct config_item *item, const char *page, \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200528 size_t count) \
529{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200530 struct se_dev_attrib *da = to_attrib(item); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200531 bool flag; \
532 int ret; \
533 \
534 ret = strtobool(page, &flag); \
535 if (ret < 0) \
536 return ret; \
537 da->_name = flag; \
538 return count; \
539}
540
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200541DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
542DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
543DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
544DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
545DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200546
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200547#define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name) \
548static ssize_t _name##_store(struct config_item *item, const char *page,\
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200549 size_t count) \
550{ \
551 printk_once(KERN_WARNING \
552 "ignoring deprecated ##_name## attribute\n"); \
553 return count; \
554}
555
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200556DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
557DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200558
559static void dev_set_t10_wwn_model_alias(struct se_device *dev)
560{
561 const char *configname;
562
563 configname = config_item_name(&dev->dev_group.cg_item);
564 if (strlen(configname) >= 16) {
565 pr_warn("dev[%p]: Backstore name '%s' is too long for "
566 "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
567 configname);
568 }
569 snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
570}
571
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200572static ssize_t emulate_model_alias_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200573 const char *page, size_t count)
574{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200575 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200576 struct se_device *dev = da->da_dev;
577 bool flag;
578 int ret;
579
580 if (dev->export_count) {
581 pr_err("dev[%p]: Unable to change model alias"
582 " while export_count is %d\n",
583 dev, dev->export_count);
584 return -EINVAL;
585 }
586
587 ret = strtobool(page, &flag);
588 if (ret < 0)
589 return ret;
590
591 if (flag) {
592 dev_set_t10_wwn_model_alias(dev);
593 } else {
594 strncpy(&dev->t10_wwn.model[0],
595 dev->transport->inquiry_prod, 16);
596 }
597 da->emulate_model_alias = flag;
598 return count;
599}
600
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200601static ssize_t emulate_write_cache_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200602 const char *page, size_t count)
603{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200604 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200605 bool flag;
606 int ret;
607
608 ret = strtobool(page, &flag);
609 if (ret < 0)
610 return ret;
611
612 if (flag && da->da_dev->transport->get_write_cache) {
613 pr_err("emulate_write_cache not supported for this device\n");
614 return -EINVAL;
615 }
616
617 da->emulate_write_cache = flag;
618 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
619 da->da_dev, flag);
620 return count;
621}
622
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200623static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200624 const char *page, size_t count)
625{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200626 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200627 u32 val;
628 int ret;
629
630 ret = kstrtou32(page, 0, &val);
631 if (ret < 0)
632 return ret;
633
634 if (val != 0 && val != 1 && val != 2) {
635 pr_err("Illegal value %d\n", val);
636 return -EINVAL;
637 }
638
639 if (da->da_dev->export_count) {
640 pr_err("dev[%p]: Unable to change SE Device"
641 " UA_INTRLCK_CTRL while export_count is %d\n",
642 da->da_dev, da->da_dev->export_count);
643 return -EINVAL;
644 }
645 da->emulate_ua_intlck_ctrl = val;
646 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
647 da->da_dev, val);
648 return count;
649}
650
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200651static ssize_t emulate_tas_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200652 const char *page, size_t count)
653{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200654 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200655 bool flag;
656 int ret;
657
658 ret = strtobool(page, &flag);
659 if (ret < 0)
660 return ret;
661
662 if (da->da_dev->export_count) {
663 pr_err("dev[%p]: Unable to change SE Device TAS while"
664 " export_count is %d\n",
665 da->da_dev, da->da_dev->export_count);
666 return -EINVAL;
667 }
668 da->emulate_tas = flag;
669 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
670 da->da_dev, flag ? "Enabled" : "Disabled");
671
672 return count;
673}
674
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200675static ssize_t emulate_tpu_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200676 const char *page, size_t count)
677{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200678 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200679 bool flag;
680 int ret;
681
682 ret = strtobool(page, &flag);
683 if (ret < 0)
684 return ret;
685
686 /*
687 * We expect this value to be non-zero when generic Block Layer
688 * Discard supported is detected iblock_create_virtdevice().
689 */
690 if (flag && !da->max_unmap_block_desc_count) {
691 pr_err("Generic Block Discard not supported\n");
692 return -ENOSYS;
693 }
694
695 da->emulate_tpu = flag;
696 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
697 da->da_dev, flag);
698 return count;
699}
700
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200701static ssize_t emulate_tpws_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200702 const char *page, size_t count)
703{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200704 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200705 bool flag;
706 int ret;
707
708 ret = strtobool(page, &flag);
709 if (ret < 0)
710 return ret;
711
712 /*
713 * We expect this value to be non-zero when generic Block Layer
714 * Discard supported is detected iblock_create_virtdevice().
715 */
716 if (flag && !da->max_unmap_block_desc_count) {
717 pr_err("Generic Block Discard not supported\n");
718 return -ENOSYS;
719 }
720
721 da->emulate_tpws = flag;
722 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
723 da->da_dev, flag);
724 return count;
725}
726
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200727static ssize_t pi_prot_type_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200728 const char *page, size_t count)
729{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200730 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200731 int old_prot = da->pi_prot_type, ret;
732 struct se_device *dev = da->da_dev;
733 u32 flag;
734
735 ret = kstrtou32(page, 0, &flag);
736 if (ret < 0)
737 return ret;
738
739 if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
740 pr_err("Illegal value %d for pi_prot_type\n", flag);
741 return -EINVAL;
742 }
743 if (flag == 2) {
744 pr_err("DIF TYPE2 protection currently not supported\n");
745 return -ENOSYS;
746 }
747 if (da->hw_pi_prot_type) {
748 pr_warn("DIF protection enabled on underlying hardware,"
749 " ignoring\n");
750 return count;
751 }
752 if (!dev->transport->init_prot || !dev->transport->free_prot) {
753 /* 0 is only allowed value for non-supporting backends */
754 if (flag == 0)
Andy Groverbc1a7d62015-07-09 09:56:47 -0700755 return count;
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200756
757 pr_err("DIF protection not supported by backend: %s\n",
758 dev->transport->name);
759 return -ENOSYS;
760 }
761 if (!(dev->dev_flags & DF_CONFIGURED)) {
762 pr_err("DIF protection requires device to be configured\n");
763 return -ENODEV;
764 }
765 if (dev->export_count) {
766 pr_err("dev[%p]: Unable to change SE Device PROT type while"
767 " export_count is %d\n", dev, dev->export_count);
768 return -EINVAL;
769 }
770
771 da->pi_prot_type = flag;
772
773 if (flag && !old_prot) {
774 ret = dev->transport->init_prot(dev);
775 if (ret) {
776 da->pi_prot_type = old_prot;
777 return ret;
778 }
779
780 } else if (!flag && old_prot) {
781 dev->transport->free_prot(dev);
782 }
783
784 pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
785 return count;
786}
787
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200788static ssize_t pi_prot_format_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200789 const char *page, size_t count)
790{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200791 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200792 struct se_device *dev = da->da_dev;
793 bool flag;
794 int ret;
795
796 ret = strtobool(page, &flag);
797 if (ret < 0)
798 return ret;
799
800 if (!flag)
801 return count;
802
803 if (!dev->transport->format_prot) {
804 pr_err("DIF protection format not supported by backend %s\n",
805 dev->transport->name);
806 return -ENOSYS;
807 }
808 if (!(dev->dev_flags & DF_CONFIGURED)) {
809 pr_err("DIF protection format requires device to be configured\n");
810 return -ENODEV;
811 }
812 if (dev->export_count) {
813 pr_err("dev[%p]: Unable to format SE Device PROT type while"
814 " export_count is %d\n", dev, dev->export_count);
815 return -EINVAL;
816 }
817
818 ret = dev->transport->format_prot(dev);
819 if (ret)
820 return ret;
821
822 pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
823 return count;
824}
825
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200826static ssize_t force_pr_aptpl_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200827 const char *page, size_t count)
828{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200829 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200830 bool flag;
831 int ret;
832
833 ret = strtobool(page, &flag);
834 if (ret < 0)
835 return ret;
836 if (da->da_dev->export_count) {
837 pr_err("dev[%p]: Unable to set force_pr_aptpl while"
838 " export_count is %d\n",
839 da->da_dev, da->da_dev->export_count);
840 return -EINVAL;
841 }
842
843 da->force_pr_aptpl = flag;
844 pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
845 return count;
846}
847
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200848static ssize_t emulate_rest_reord_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200849 const char *page, size_t count)
850{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200851 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200852 bool flag;
853 int ret;
854
855 ret = strtobool(page, &flag);
856 if (ret < 0)
857 return ret;
858
859 if (flag != 0) {
860 printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
861 " reordering not implemented\n", da->da_dev);
862 return -ENOSYS;
863 }
864 da->emulate_rest_reord = flag;
865 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
866 da->da_dev, flag);
867 return count;
868}
869
Jamie Pocase6f416332015-11-29 14:44:57 -0800870static ssize_t unmap_zeroes_data_store(struct config_item *item,
871 const char *page, size_t count)
872{
873 struct se_dev_attrib *da = to_attrib(item);
874 bool flag;
875 int ret;
876
877 ret = strtobool(page, &flag);
878 if (ret < 0)
879 return ret;
880
881 if (da->da_dev->export_count) {
882 pr_err("dev[%p]: Unable to change SE Device"
883 " unmap_zeroes_data while export_count is %d\n",
884 da->da_dev, da->da_dev->export_count);
885 return -EINVAL;
886 }
887 /*
888 * We expect this value to be non-zero when generic Block Layer
889 * Discard supported is detected iblock_configure_device().
890 */
891 if (flag && !da->max_unmap_block_desc_count) {
892 pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
893 " because max_unmap_block_desc_count is zero\n",
894 da->da_dev);
895 return -ENOSYS;
896 }
897 da->unmap_zeroes_data = flag;
898 pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
899 da->da_dev, flag);
900 return 0;
901}
902
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200903/*
904 * Note, this can only be called on unexported SE Device Object.
905 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200906static ssize_t queue_depth_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200907 const char *page, size_t count)
908{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200909 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200910 struct se_device *dev = da->da_dev;
911 u32 val;
912 int ret;
913
914 ret = kstrtou32(page, 0, &val);
915 if (ret < 0)
916 return ret;
917
918 if (dev->export_count) {
919 pr_err("dev[%p]: Unable to change SE Device TCQ while"
920 " export_count is %d\n",
921 dev, dev->export_count);
922 return -EINVAL;
923 }
924 if (!val) {
925 pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
926 return -EINVAL;
927 }
928
929 if (val > dev->dev_attrib.queue_depth) {
930 if (val > dev->dev_attrib.hw_queue_depth) {
931 pr_err("dev[%p]: Passed queue_depth:"
932 " %u exceeds TCM/SE_Device MAX"
933 " TCQ: %u\n", dev, val,
934 dev->dev_attrib.hw_queue_depth);
935 return -EINVAL;
936 }
937 }
938 da->queue_depth = dev->queue_depth = val;
939 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
940 return count;
941}
942
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200943static ssize_t optimal_sectors_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200944 const char *page, size_t count)
945{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200946 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200947 u32 val;
948 int ret;
949
950 ret = kstrtou32(page, 0, &val);
951 if (ret < 0)
952 return ret;
953
954 if (da->da_dev->export_count) {
955 pr_err("dev[%p]: Unable to change SE Device"
956 " optimal_sectors while export_count is %d\n",
957 da->da_dev, da->da_dev->export_count);
958 return -EINVAL;
959 }
960 if (val > da->hw_max_sectors) {
961 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
962 " greater than hw_max_sectors: %u\n",
963 da->da_dev, val, da->hw_max_sectors);
964 return -EINVAL;
965 }
966
967 da->optimal_sectors = val;
968 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
969 da->da_dev, val);
970 return count;
971}
972
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200973static ssize_t block_size_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200974 const char *page, size_t count)
975{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200976 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200977 u32 val;
978 int ret;
979
980 ret = kstrtou32(page, 0, &val);
981 if (ret < 0)
982 return ret;
983
984 if (da->da_dev->export_count) {
985 pr_err("dev[%p]: Unable to change SE Device block_size"
986 " while export_count is %d\n",
987 da->da_dev, da->da_dev->export_count);
988 return -EINVAL;
989 }
990
991 if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
992 pr_err("dev[%p]: Illegal value for block_device: %u"
993 " for SE device, must be 512, 1024, 2048 or 4096\n",
994 da->da_dev, val);
995 return -EINVAL;
996 }
997
998 da->block_size = val;
999 if (da->max_bytes_per_io)
1000 da->hw_max_sectors = da->max_bytes_per_io / val;
1001
1002 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
1003 da->da_dev, val);
1004 return count;
1005}
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001006
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001007CONFIGFS_ATTR(, emulate_model_alias);
1008CONFIGFS_ATTR(, emulate_dpo);
1009CONFIGFS_ATTR(, emulate_fua_write);
1010CONFIGFS_ATTR(, emulate_fua_read);
1011CONFIGFS_ATTR(, emulate_write_cache);
1012CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
1013CONFIGFS_ATTR(, emulate_tas);
1014CONFIGFS_ATTR(, emulate_tpu);
1015CONFIGFS_ATTR(, emulate_tpws);
1016CONFIGFS_ATTR(, emulate_caw);
1017CONFIGFS_ATTR(, emulate_3pc);
1018CONFIGFS_ATTR(, pi_prot_type);
1019CONFIGFS_ATTR_RO(, hw_pi_prot_type);
1020CONFIGFS_ATTR(, pi_prot_format);
1021CONFIGFS_ATTR(, enforce_pr_isids);
1022CONFIGFS_ATTR(, is_nonrot);
1023CONFIGFS_ATTR(, emulate_rest_reord);
1024CONFIGFS_ATTR(, force_pr_aptpl);
1025CONFIGFS_ATTR_RO(, hw_block_size);
1026CONFIGFS_ATTR(, block_size);
1027CONFIGFS_ATTR_RO(, hw_max_sectors);
1028CONFIGFS_ATTR(, optimal_sectors);
1029CONFIGFS_ATTR_RO(, hw_queue_depth);
1030CONFIGFS_ATTR(, queue_depth);
1031CONFIGFS_ATTR(, max_unmap_lba_count);
1032CONFIGFS_ATTR(, max_unmap_block_desc_count);
1033CONFIGFS_ATTR(, unmap_granularity);
1034CONFIGFS_ATTR(, unmap_granularity_alignment);
Jamie Pocase6f416332015-11-29 14:44:57 -08001035CONFIGFS_ATTR(, unmap_zeroes_data);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001036CONFIGFS_ATTR(, max_write_same_len);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001037
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001038/*
1039 * dev_attrib attributes for devices using the target core SBC/SPC
1040 * interpreter. Any backend using spc_parse_cdb should be using
1041 * these.
1042 */
1043struct configfs_attribute *sbc_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001044 &attr_emulate_model_alias,
1045 &attr_emulate_dpo,
1046 &attr_emulate_fua_write,
1047 &attr_emulate_fua_read,
1048 &attr_emulate_write_cache,
1049 &attr_emulate_ua_intlck_ctrl,
1050 &attr_emulate_tas,
1051 &attr_emulate_tpu,
1052 &attr_emulate_tpws,
1053 &attr_emulate_caw,
1054 &attr_emulate_3pc,
1055 &attr_pi_prot_type,
1056 &attr_hw_pi_prot_type,
1057 &attr_pi_prot_format,
1058 &attr_enforce_pr_isids,
1059 &attr_is_nonrot,
1060 &attr_emulate_rest_reord,
1061 &attr_force_pr_aptpl,
1062 &attr_hw_block_size,
1063 &attr_block_size,
1064 &attr_hw_max_sectors,
1065 &attr_optimal_sectors,
1066 &attr_hw_queue_depth,
1067 &attr_queue_depth,
1068 &attr_max_unmap_lba_count,
1069 &attr_max_unmap_block_desc_count,
1070 &attr_unmap_granularity,
1071 &attr_unmap_granularity_alignment,
Jamie Pocase6f416332015-11-29 14:44:57 -08001072 &attr_unmap_zeroes_data,
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001073 &attr_max_write_same_len,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001074 NULL,
1075};
1076EXPORT_SYMBOL(sbc_attrib_attrs);
1077
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001078/*
1079 * Minimal dev_attrib attributes for devices passing through CDBs.
1080 * In this case we only provide a few read-only attributes for
1081 * backwards compatibility.
1082 */
1083struct configfs_attribute *passthrough_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001084 &attr_hw_pi_prot_type,
1085 &attr_hw_block_size,
1086 &attr_hw_max_sectors,
1087 &attr_hw_queue_depth,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001088 NULL,
1089};
1090EXPORT_SYMBOL(passthrough_attrib_attrs);
1091
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001092TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001093
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08001094/* End functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001095
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001096/* Start functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001097
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001098static struct t10_wwn *to_t10_wwn(struct config_item *item)
1099{
1100 return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
1101}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001102
1103/*
1104 * VPD page 0x80 Unit serial
1105 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001106static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1107 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001108{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001109 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001110 &to_t10_wwn(item)->unit_serial[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001111}
1112
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001113static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
1114 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001115{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001116 struct t10_wwn *t10_wwn = to_t10_wwn(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001117 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001118 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1119
1120 /*
1121 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1122 * from the struct scsi_device level firmware, do not allow
1123 * VPD Unit Serial to be emulated.
1124 *
1125 * Note this struct scsi_device could also be emulating VPD
1126 * information from its drivers/scsi LLD. But for now we assume
1127 * it is doing 'the right thing' wrt a world wide unique
1128 * VPD Unit Serial Number that OS dependent multipath can depend on.
1129 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001130 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -07001131 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001132 " Unit Serial, ignoring request\n");
1133 return -EOPNOTSUPP;
1134 }
1135
Dan Carpenter60d645a2011-06-15 10:03:05 -07001136 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001137 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001138 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1139 return -EOVERFLOW;
1140 }
1141 /*
1142 * Check to see if any active $FABRIC_MOD exports exist. If they
1143 * do exist, fail here as changing this information on the fly
1144 * (underneath the initiator side OS dependent multipath code)
1145 * could cause negative effects.
1146 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001147 if (dev->export_count) {
1148 pr_err("Unable to set VPD Unit Serial while"
1149 " active %d $FABRIC_MOD exports exist\n",
1150 dev->export_count);
1151 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001152 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001153
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001154 /*
1155 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1156 *
1157 * Also, strip any newline added from the userspace
1158 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1159 */
1160 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1161 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001162 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001163 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001164 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001165
Andy Grover6708bb22011-06-08 10:36:43 -07001166 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001167 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001168
1169 return count;
1170}
1171
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001172/*
1173 * VPD page 0x83 Protocol Identifier
1174 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001175static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1176 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001177{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001178 struct t10_wwn *t10_wwn = to_t10_wwn(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001179 struct t10_vpd *vpd;
1180 unsigned char buf[VPD_TMP_BUF_SIZE];
1181 ssize_t len = 0;
1182
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001183 memset(buf, 0, VPD_TMP_BUF_SIZE);
1184
1185 spin_lock(&t10_wwn->t10_vpd_lock);
1186 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -07001187 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001188 continue;
1189
1190 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1191
Andy Grover6708bb22011-06-08 10:36:43 -07001192 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001193 break;
1194
1195 len += sprintf(page+len, "%s", buf);
1196 }
1197 spin_unlock(&t10_wwn->t10_vpd_lock);
1198
1199 return len;
1200}
1201
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001202/*
1203 * Generic wrapper for dumping VPD identifiers by association.
1204 */
1205#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001206static ssize_t target_wwn_##_name##_show(struct config_item *item, \
1207 char *page) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001208{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001209 struct t10_wwn *t10_wwn = to_t10_wwn(item); \
1210 struct t10_vpd *vpd; \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001211 unsigned char buf[VPD_TMP_BUF_SIZE]; \
1212 ssize_t len = 0; \
1213 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001214 spin_lock(&t10_wwn->t10_vpd_lock); \
1215 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1216 if (vpd->association != _assoc) \
1217 continue; \
1218 \
1219 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1220 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001221 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001222 break; \
1223 len += sprintf(page+len, "%s", buf); \
1224 \
1225 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1226 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001227 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001228 break; \
1229 len += sprintf(page+len, "%s", buf); \
1230 \
1231 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1232 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001233 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001234 break; \
1235 len += sprintf(page+len, "%s", buf); \
1236 } \
1237 spin_unlock(&t10_wwn->t10_vpd_lock); \
1238 \
1239 return len; \
1240}
1241
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001242/* VPD page 0x83 Association: Logical Unit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001243DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001244/* VPD page 0x83 Association: Target Port */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001245DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001246/* VPD page 0x83 Association: SCSI Target Device */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001247DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1248
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001249CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
1250CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
1251CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
1252CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
1253CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001254
1255static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001256 &target_wwn_attr_vpd_unit_serial,
1257 &target_wwn_attr_vpd_protocol_identifier,
1258 &target_wwn_attr_vpd_assoc_logical_unit,
1259 &target_wwn_attr_vpd_assoc_target_port,
1260 &target_wwn_attr_vpd_assoc_scsi_target_device,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001261 NULL,
1262};
1263
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001264TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001265
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001266/* End functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001267
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001268/* Start functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001269
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001270static struct se_device *pr_to_dev(struct config_item *item)
1271{
1272 return container_of(to_config_group(item), struct se_device,
1273 dev_pr_group);
1274}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001275
Christoph Hellwigd977f432012-10-10 17:37:15 -04001276static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1277 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001278{
1279 struct se_node_acl *se_nacl;
1280 struct t10_pr_registration *pr_reg;
1281 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001282
1283 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1284
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001285 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001286 if (!pr_reg)
1287 return sprintf(page, "No SPC-3 Reservation holder\n");
1288
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001289 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -07001290 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001291
Christoph Hellwigd977f432012-10-10 17:37:15 -04001292 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001293 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001294 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001295}
1296
Christoph Hellwigd977f432012-10-10 17:37:15 -04001297static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1298 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001299{
1300 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001301 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001302
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001303 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001304 if (se_nacl) {
1305 len = sprintf(page,
1306 "SPC-2 Reservation: %s Initiator: %s\n",
1307 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1308 se_nacl->initiatorname);
1309 } else {
1310 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001311 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001312 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001313}
1314
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001315static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001316{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001317 struct se_device *dev = pr_to_dev(item);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001318 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001319
Andy Grovera3541702015-05-19 14:44:41 -07001320 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001321 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001322
Christoph Hellwigd977f432012-10-10 17:37:15 -04001323 spin_lock(&dev->dev_reservation_lock);
1324 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1325 ret = target_core_dev_pr_show_spc2_res(dev, page);
1326 else
1327 ret = target_core_dev_pr_show_spc3_res(dev, page);
1328 spin_unlock(&dev->dev_reservation_lock);
1329 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001330}
1331
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001332static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
1333 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001334{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001335 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001336 ssize_t len = 0;
1337
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001338 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001339 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001340 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001341 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001342 len = sprintf(page, "SPC-3 Reservation: All Target"
1343 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001344 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001345 len = sprintf(page, "SPC-3 Reservation: Single"
1346 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001347 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001348
Christoph Hellwigd977f432012-10-10 17:37:15 -04001349 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001350 return len;
1351}
1352
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001353static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
1354 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001355{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001356 return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001357}
1358
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001359
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001360static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
1361 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001362{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001363 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001364 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001365 struct se_portal_group *se_tpg;
1366 struct t10_pr_registration *pr_reg;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001367 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001368 ssize_t len = 0;
1369
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001370 spin_lock(&dev->dev_reservation_lock);
1371 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001372 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001373 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001374 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001375 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001376
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001377 se_nacl = pr_reg->pr_reg_nacl;
1378 se_tpg = se_nacl->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001379 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001380
1381 len += sprintf(page+len, "SPC-3 Reservation: %s"
1382 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1383 tfo->tpg_get_wwn(se_tpg));
1384 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001385 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001386 " %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001387 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001388 tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001389
Christoph Hellwigd977f432012-10-10 17:37:15 -04001390out_unlock:
1391 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001392 return len;
1393}
1394
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001395
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001396static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
1397 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001398{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001399 struct se_device *dev = pr_to_dev(item);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001400 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001401 struct t10_pr_registration *pr_reg;
1402 unsigned char buf[384];
1403 char i_buf[PR_REG_ISID_ID_LEN];
1404 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001405 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001406
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001407 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1408
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001409 spin_lock(&dev->t10_pr.registration_lock);
1410 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001411 pr_reg_list) {
1412
1413 memset(buf, 0, 384);
1414 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1415 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -07001416 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001417 PR_REG_ISID_ID_LEN);
1418 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1419 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001420 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001421 pr_reg->pr_res_generation);
1422
Andy Grover6708bb22011-06-08 10:36:43 -07001423 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001424 break;
1425
1426 len += sprintf(page+len, "%s", buf);
1427 reg_count++;
1428 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001429 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001430
Andy Grover6708bb22011-06-08 10:36:43 -07001431 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001432 len += sprintf(page+len, "None\n");
1433
1434 return len;
1435}
1436
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001437static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001438{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001439 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001440 struct t10_pr_registration *pr_reg;
1441 ssize_t len = 0;
1442
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001443 spin_lock(&dev->dev_reservation_lock);
1444 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001445 if (pr_reg) {
1446 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1447 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1448 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001449 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001450 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001451
Christoph Hellwigd977f432012-10-10 17:37:15 -04001452 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001453 return len;
1454}
1455
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001456static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001457{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001458 struct se_device *dev = pr_to_dev(item);
1459
Andy Grovera3541702015-05-19 14:44:41 -07001460 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001461 return sprintf(page, "SPC_PASSTHROUGH\n");
1462 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1463 return sprintf(page, "SPC2_RESERVATIONS\n");
1464 else
1465 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001466}
1467
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001468static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
1469 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001470{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001471 struct se_device *dev = pr_to_dev(item);
1472
Andy Grovera3541702015-05-19 14:44:41 -07001473 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001474 return 0;
1475
1476 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001477 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001478}
1479
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001480static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
1481 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001482{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001483 struct se_device *dev = pr_to_dev(item);
1484
Andy Grovera3541702015-05-19 14:44:41 -07001485 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001486 return 0;
1487
1488 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1489}
1490
1491enum {
1492 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1493 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1494 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1495 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1496};
1497
1498static match_table_t tokens = {
1499 {Opt_initiator_fabric, "initiator_fabric=%s"},
1500 {Opt_initiator_node, "initiator_node=%s"},
1501 {Opt_initiator_sid, "initiator_sid=%s"},
1502 {Opt_sa_res_key, "sa_res_key=%s"},
1503 {Opt_res_holder, "res_holder=%d"},
1504 {Opt_res_type, "res_type=%d"},
1505 {Opt_res_scope, "res_scope=%d"},
1506 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
Hannes Reineckef2d30682015-06-10 08:41:22 +02001507 {Opt_mapped_lun, "mapped_lun=%lld"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001508 {Opt_target_fabric, "target_fabric=%s"},
1509 {Opt_target_node, "target_node=%s"},
1510 {Opt_tpgt, "tpgt=%d"},
1511 {Opt_port_rtpi, "port_rtpi=%d"},
Hannes Reineckef2d30682015-06-10 08:41:22 +02001512 {Opt_target_lun, "target_lun=%lld"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001513 {Opt_err, NULL}
1514};
1515
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001516static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
1517 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001518{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001519 struct se_device *dev = pr_to_dev(item);
Jesper Juhl6d180252011-03-14 04:05:56 -07001520 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1521 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -04001522 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001523 substring_t args[MAX_OPT_ARGS];
1524 unsigned long long tmp_ll;
1525 u64 sa_res_key = 0;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001526 u64 mapped_lun = 0, target_lun = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001527 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
Bart Van Assche45fb94c2015-04-14 13:00:58 +02001528 u16 tpgt = 0;
1529 u8 type = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001530
Andy Grovera3541702015-05-19 14:44:41 -07001531 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Andy Grover9105bfc2015-07-09 09:56:48 -07001532 return count;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001533 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Andy Grover9105bfc2015-07-09 09:56:48 -07001534 return count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001535
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001536 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001537 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001538 " active fabric exports exist\n");
1539 return -EINVAL;
1540 }
1541
1542 opts = kstrdup(page, GFP_KERNEL);
1543 if (!opts)
1544 return -ENOMEM;
1545
1546 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001547 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001548 if (!*ptr)
1549 continue;
1550
1551 token = match_token(ptr, tokens, args);
1552 switch (token) {
1553 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001554 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001555 if (!i_fabric) {
1556 ret = -ENOMEM;
1557 goto out;
1558 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001559 break;
1560 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001561 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001562 if (!i_port) {
1563 ret = -ENOMEM;
1564 goto out;
1565 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001566 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001567 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001568 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1569 PR_APTPL_MAX_IPORT_LEN);
1570 ret = -EINVAL;
1571 break;
1572 }
1573 break;
1574 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001575 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001576 if (!isid) {
1577 ret = -ENOMEM;
1578 goto out;
1579 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001580 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001581 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001582 "= exceeds PR_REG_ISID_LEN: %d\n",
1583 PR_REG_ISID_LEN);
1584 ret = -EINVAL;
1585 break;
1586 }
1587 break;
1588 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001589 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001590 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001591 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001592 goto out;
1593 }
1594 sa_res_key = (u64)tmp_ll;
1595 break;
1596 /*
1597 * PR APTPL Metadata for Reservation
1598 */
1599 case Opt_res_holder:
David Disseldorpc2091022015-07-12 18:49:18 +02001600 ret = match_int(args, &arg);
1601 if (ret)
1602 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001603 res_holder = arg;
1604 break;
1605 case Opt_res_type:
David Disseldorpc2091022015-07-12 18:49:18 +02001606 ret = match_int(args, &arg);
1607 if (ret)
1608 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001609 type = (u8)arg;
1610 break;
1611 case Opt_res_scope:
David Disseldorpc2091022015-07-12 18:49:18 +02001612 ret = match_int(args, &arg);
1613 if (ret)
1614 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001615 break;
1616 case Opt_res_all_tg_pt:
David Disseldorpc2091022015-07-12 18:49:18 +02001617 ret = match_int(args, &arg);
1618 if (ret)
1619 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001620 all_tg_pt = (int)arg;
1621 break;
1622 case Opt_mapped_lun:
David Disseldorpc2091022015-07-12 18:49:18 +02001623 ret = match_int(args, &arg);
1624 if (ret)
1625 goto out;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001626 mapped_lun = (u64)arg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001627 break;
1628 /*
1629 * PR APTPL Metadata for Target Port
1630 */
1631 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001632 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001633 if (!t_fabric) {
1634 ret = -ENOMEM;
1635 goto out;
1636 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001637 break;
1638 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001639 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001640 if (!t_port) {
1641 ret = -ENOMEM;
1642 goto out;
1643 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001644 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001645 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001646 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1647 PR_APTPL_MAX_TPORT_LEN);
1648 ret = -EINVAL;
1649 break;
1650 }
1651 break;
1652 case Opt_tpgt:
David Disseldorpc2091022015-07-12 18:49:18 +02001653 ret = match_int(args, &arg);
1654 if (ret)
1655 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001656 tpgt = (u16)arg;
1657 break;
1658 case Opt_port_rtpi:
David Disseldorpc2091022015-07-12 18:49:18 +02001659 ret = match_int(args, &arg);
1660 if (ret)
1661 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001662 break;
1663 case Opt_target_lun:
David Disseldorpc2091022015-07-12 18:49:18 +02001664 ret = match_int(args, &arg);
1665 if (ret)
1666 goto out;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001667 target_lun = (u64)arg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001668 break;
1669 default:
1670 break;
1671 }
1672 }
1673
Andy Grover6708bb22011-06-08 10:36:43 -07001674 if (!i_port || !t_port || !sa_res_key) {
1675 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001676 ret = -EINVAL;
1677 goto out;
1678 }
1679
1680 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001681 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001682 " holder\n", type);
1683 ret = -EINVAL;
1684 goto out;
1685 }
1686
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001687 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001688 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1689 res_holder, all_tg_pt, type);
1690out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001691 kfree(i_fabric);
1692 kfree(i_port);
1693 kfree(isid);
1694 kfree(t_fabric);
1695 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001696 kfree(orig);
1697 return (ret == 0) ? count : ret;
1698}
1699
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001700
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001701CONFIGFS_ATTR_RO(target_pr_, res_holder);
1702CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
1703CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
1704CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
1705CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
1706CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
1707CONFIGFS_ATTR_RO(target_pr_, res_type);
1708CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
1709CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001710
1711static struct configfs_attribute *target_core_dev_pr_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001712 &target_pr_attr_res_holder,
1713 &target_pr_attr_res_pr_all_tgt_pts,
1714 &target_pr_attr_res_pr_generation,
1715 &target_pr_attr_res_pr_holder_tg_port,
1716 &target_pr_attr_res_pr_registered_i_pts,
1717 &target_pr_attr_res_pr_type,
1718 &target_pr_attr_res_type,
1719 &target_pr_attr_res_aptpl_active,
1720 &target_pr_attr_res_aptpl_metadata,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001721 NULL,
1722};
1723
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001724TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001725
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001726/* End functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001727
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001728/* Start functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001729
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001730static inline struct se_device *to_device(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001731{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001732 return container_of(to_config_group(item), struct se_device, dev_group);
1733}
1734
1735static ssize_t target_dev_info_show(struct config_item *item, char *page)
1736{
1737 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001738 int bl = 0;
1739 ssize_t read_bytes = 0;
1740
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001741 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001742 read_bytes += bl;
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001743 read_bytes += dev->transport->show_configfs_dev_params(dev,
1744 page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001745 return read_bytes;
1746}
1747
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001748static ssize_t target_dev_control_store(struct config_item *item,
1749 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001750{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001751 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001752
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001753 return dev->transport->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001754}
1755
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001756static ssize_t target_dev_alias_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001757{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001758 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001759
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001760 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001761 return 0;
1762
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001763 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001764}
1765
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001766static ssize_t target_dev_alias_store(struct config_item *item,
1767 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001768{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001769 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001770 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001771 ssize_t read_bytes;
1772
1773 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001774 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001775 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1776 SE_DEV_ALIAS_LEN-1);
1777 return -EINVAL;
1778 }
1779
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001780 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001781 if (!read_bytes)
1782 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001783 if (dev->dev_alias[read_bytes - 1] == '\n')
1784 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001785
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001786 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001787
Andy Grover6708bb22011-06-08 10:36:43 -07001788 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001789 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001790 config_item_name(&dev->dev_group.cg_item),
1791 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001792
1793 return read_bytes;
1794}
1795
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001796static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001797{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001798 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001799
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001800 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001801 return 0;
1802
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001803 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001804}
1805
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001806static ssize_t target_dev_udev_path_store(struct config_item *item,
1807 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001808{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001809 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001810 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001811 ssize_t read_bytes;
1812
1813 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001814 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001815 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1816 SE_UDEV_PATH_LEN-1);
1817 return -EINVAL;
1818 }
1819
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001820 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001821 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001822 if (!read_bytes)
1823 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001824 if (dev->udev_path[read_bytes - 1] == '\n')
1825 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001826
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001827 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001828
Andy Grover6708bb22011-06-08 10:36:43 -07001829 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001830 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001831 config_item_name(&dev->dev_group.cg_item),
1832 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001833
1834 return read_bytes;
1835}
1836
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001837static ssize_t target_dev_enable_show(struct config_item *item, char *page)
Andy Grover64146db2013-04-30 11:59:15 -07001838{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001839 struct se_device *dev = to_device(item);
Andy Grover64146db2013-04-30 11:59:15 -07001840
1841 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1842}
1843
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001844static ssize_t target_dev_enable_store(struct config_item *item,
1845 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001846{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001847 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001848 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001849 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001850
1851 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001852 if (!ptr) {
1853 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001854 " is \"1\"\n");
1855 return -EINVAL;
1856 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001857
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001858 ret = target_configure_device(dev);
1859 if (ret)
1860 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001861 return count;
1862}
1863
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001864static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001865{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001866 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001867 struct config_item *lu_ci;
1868 struct t10_alua_lu_gp *lu_gp;
1869 struct t10_alua_lu_gp_member *lu_gp_mem;
1870 ssize_t len = 0;
1871
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001872 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001873 if (!lu_gp_mem)
1874 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001875
1876 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1877 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001878 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001879 lu_ci = &lu_gp->lu_gp_group.cg_item;
1880 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1881 config_item_name(lu_ci), lu_gp->lu_gp_id);
1882 }
1883 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1884
1885 return len;
1886}
1887
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001888static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
1889 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001890{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001891 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001892 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001893 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1894 struct t10_alua_lu_gp_member *lu_gp_mem;
1895 unsigned char buf[LU_GROUP_NAME_BUF];
1896 int move = 0;
1897
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001898 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1899 if (!lu_gp_mem)
Andy Grover9105bfc2015-07-09 09:56:48 -07001900 return count;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001901
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001902 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001903 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001904 return -EINVAL;
1905 }
1906 memset(buf, 0, LU_GROUP_NAME_BUF);
1907 memcpy(buf, page, count);
1908 /*
1909 * Any ALUA logical unit alias besides "NULL" means we will be
1910 * making a new group association.
1911 */
1912 if (strcmp(strstrip(buf), "NULL")) {
1913 /*
1914 * core_alua_get_lu_gp_by_name() will increment reference to
1915 * struct t10_alua_lu_gp. This reference is released with
1916 * core_alua_get_lu_gp_by_name below().
1917 */
1918 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001919 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001920 return -ENODEV;
1921 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001922
1923 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1924 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001925 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001926 /*
1927 * Clearing an existing lu_gp association, and replacing
1928 * with NULL
1929 */
Andy Grover6708bb22011-06-08 10:36:43 -07001930 if (!lu_gp_new) {
1931 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001932 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1933 " %hu\n",
1934 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001935 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001936 config_item_name(&lu_gp->lu_gp_group.cg_item),
1937 lu_gp->lu_gp_id);
1938
1939 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1940 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1941
1942 return count;
1943 }
1944 /*
1945 * Removing existing association of lu_gp_mem with lu_gp
1946 */
1947 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1948 move = 1;
1949 }
1950 /*
1951 * Associate lu_gp_mem with lu_gp_new.
1952 */
1953 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1954 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1955
Andy Grover6708bb22011-06-08 10:36:43 -07001956 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001957 " core/alua/lu_gps/%s, ID: %hu\n",
1958 (move) ? "Moving" : "Adding",
1959 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001960 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001961 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1962 lu_gp_new->lu_gp_id);
1963
1964 core_alua_put_lu_gp_from_name(lu_gp_new);
1965 return count;
1966}
1967
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001968static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001969{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001970 struct se_device *dev = to_device(item);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001971 struct t10_alua_lba_map *map;
1972 struct t10_alua_lba_map_member *mem;
1973 char *b = page;
1974 int bl = 0;
1975 char state;
1976
1977 spin_lock(&dev->t10_alua.lba_map_lock);
1978 if (!list_empty(&dev->t10_alua.lba_map_list))
1979 bl += sprintf(b + bl, "%u %u\n",
1980 dev->t10_alua.lba_map_segment_size,
1981 dev->t10_alua.lba_map_segment_multiplier);
1982 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1983 bl += sprintf(b + bl, "%llu %llu",
1984 map->lba_map_first_lba, map->lba_map_last_lba);
1985 list_for_each_entry(mem, &map->lba_map_mem_list,
1986 lba_map_mem_list) {
1987 switch (mem->lba_map_mem_alua_state) {
1988 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1989 state = 'O';
1990 break;
1991 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1992 state = 'A';
1993 break;
1994 case ALUA_ACCESS_STATE_STANDBY:
1995 state = 'S';
1996 break;
1997 case ALUA_ACCESS_STATE_UNAVAILABLE:
1998 state = 'U';
1999 break;
2000 default:
2001 state = '.';
2002 break;
2003 }
2004 bl += sprintf(b + bl, " %d:%c",
2005 mem->lba_map_mem_alua_pg_id, state);
2006 }
2007 bl += sprintf(b + bl, "\n");
2008 }
2009 spin_unlock(&dev->t10_alua.lba_map_lock);
2010 return bl;
2011}
2012
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002013static ssize_t target_dev_lba_map_store(struct config_item *item,
2014 const char *page, size_t count)
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002015{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002016 struct se_device *dev = to_device(item);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002017 struct t10_alua_lba_map *lba_map = NULL;
2018 struct list_head lba_list;
2019 char *map_entries, *ptr;
2020 char state;
2021 int pg_num = -1, pg;
2022 int ret = 0, num = 0, pg_id, alua_state;
2023 unsigned long start_lba = -1, end_lba = -1;
2024 unsigned long segment_size = -1, segment_mult = -1;
2025
2026 map_entries = kstrdup(page, GFP_KERNEL);
2027 if (!map_entries)
2028 return -ENOMEM;
2029
2030 INIT_LIST_HEAD(&lba_list);
2031 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2032 if (!*ptr)
2033 continue;
2034
2035 if (num == 0) {
2036 if (sscanf(ptr, "%lu %lu\n",
2037 &segment_size, &segment_mult) != 2) {
2038 pr_err("Invalid line %d\n", num);
2039 ret = -EINVAL;
2040 break;
2041 }
2042 num++;
2043 continue;
2044 }
2045 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2046 pr_err("Invalid line %d\n", num);
2047 ret = -EINVAL;
2048 break;
2049 }
2050 ptr = strchr(ptr, ' ');
2051 if (!ptr) {
2052 pr_err("Invalid line %d, missing end lba\n", num);
2053 ret = -EINVAL;
2054 break;
2055 }
2056 ptr++;
2057 ptr = strchr(ptr, ' ');
2058 if (!ptr) {
2059 pr_err("Invalid line %d, missing state definitions\n",
2060 num);
2061 ret = -EINVAL;
2062 break;
2063 }
2064 ptr++;
2065 lba_map = core_alua_allocate_lba_map(&lba_list,
2066 start_lba, end_lba);
2067 if (IS_ERR(lba_map)) {
2068 ret = PTR_ERR(lba_map);
2069 break;
2070 }
2071 pg = 0;
2072 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2073 switch (state) {
2074 case 'O':
2075 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2076 break;
2077 case 'A':
2078 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2079 break;
2080 case 'S':
2081 alua_state = ALUA_ACCESS_STATE_STANDBY;
2082 break;
2083 case 'U':
2084 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2085 break;
2086 default:
2087 pr_err("Invalid ALUA state '%c'\n", state);
2088 ret = -EINVAL;
2089 goto out;
2090 }
2091
2092 ret = core_alua_allocate_lba_map_mem(lba_map,
2093 pg_id, alua_state);
2094 if (ret) {
2095 pr_err("Invalid target descriptor %d:%c "
2096 "at line %d\n",
2097 pg_id, state, num);
2098 break;
2099 }
2100 pg++;
2101 ptr = strchr(ptr, ' ');
2102 if (ptr)
2103 ptr++;
2104 else
2105 break;
2106 }
2107 if (pg_num == -1)
2108 pg_num = pg;
2109 else if (pg != pg_num) {
2110 pr_err("Only %d from %d port groups definitions "
2111 "at line %d\n", pg, pg_num, num);
2112 ret = -EINVAL;
2113 break;
2114 }
2115 num++;
2116 }
2117out:
2118 if (ret) {
2119 core_alua_free_lba_map(&lba_list);
2120 count = ret;
2121 } else
2122 core_alua_set_lba_map(dev, &lba_list,
2123 segment_size, segment_mult);
2124 kfree(map_entries);
2125 return count;
2126}
2127
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002128CONFIGFS_ATTR_RO(target_dev_, info);
2129CONFIGFS_ATTR_WO(target_dev_, control);
2130CONFIGFS_ATTR(target_dev_, alias);
2131CONFIGFS_ATTR(target_dev_, udev_path);
2132CONFIGFS_ATTR(target_dev_, enable);
2133CONFIGFS_ATTR(target_dev_, alua_lu_gp);
2134CONFIGFS_ATTR(target_dev_, lba_map);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002135
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002136static struct configfs_attribute *target_core_dev_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002137 &target_dev_attr_info,
2138 &target_dev_attr_control,
2139 &target_dev_attr_alias,
2140 &target_dev_attr_udev_path,
2141 &target_dev_attr_enable,
2142 &target_dev_attr_alua_lu_gp,
2143 &target_dev_attr_lba_map,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002144 NULL,
2145};
2146
2147static void target_core_dev_release(struct config_item *item)
2148{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002149 struct config_group *dev_cg = to_config_group(item);
2150 struct se_device *dev =
2151 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002152
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002153 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002154 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002155}
2156
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002157static struct configfs_item_operations target_core_dev_item_ops = {
2158 .release = target_core_dev_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002159};
2160
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002161TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002162
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002163/* End functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002164
2165/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2166
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002167static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002168{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002169 return container_of(to_config_group(item), struct t10_alua_lu_gp,
2170 lu_gp_group);
2171}
2172
2173static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
2174{
2175 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2176
Andy Grover6708bb22011-06-08 10:36:43 -07002177 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002178 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002179 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2180}
2181
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002182static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
2183 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002184{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002185 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002186 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2187 unsigned long lu_gp_id;
2188 int ret;
2189
Jingoo Han57103d72013-07-19 16:22:19 +09002190 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002191 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002192 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002193 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002194 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002195 }
2196 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002197 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002198 " 0x0000ffff\n", lu_gp_id);
2199 return -EINVAL;
2200 }
2201
2202 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2203 if (ret < 0)
2204 return -EINVAL;
2205
Andy Grover6708bb22011-06-08 10:36:43 -07002206 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002207 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2208 config_item_name(&alua_lu_gp_cg->cg_item),
2209 lu_gp->lu_gp_id);
2210
2211 return count;
2212}
2213
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002214static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002215{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002216 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002217 struct se_device *dev;
2218 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002219 struct t10_alua_lu_gp_member *lu_gp_mem;
2220 ssize_t len = 0, cur_len;
2221 unsigned char buf[LU_GROUP_NAME_BUF];
2222
2223 memset(buf, 0, LU_GROUP_NAME_BUF);
2224
2225 spin_lock(&lu_gp->lu_gp_lock);
2226 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2227 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002228 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002229
2230 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2231 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002232 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002233 cur_len++; /* Extra byte for NULL terminator */
2234
2235 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002236 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002237 "_members buffer\n");
2238 break;
2239 }
2240 memcpy(page+len, buf, cur_len);
2241 len += cur_len;
2242 }
2243 spin_unlock(&lu_gp->lu_gp_lock);
2244
2245 return len;
2246}
2247
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002248CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
2249CONFIGFS_ATTR_RO(target_lu_gp_, members);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002250
2251static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002252 &target_lu_gp_attr_lu_gp_id,
2253 &target_lu_gp_attr_members,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002254 NULL,
2255};
2256
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002257static void target_core_alua_lu_gp_release(struct config_item *item)
2258{
2259 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2260 struct t10_alua_lu_gp, lu_gp_group);
2261
2262 core_alua_free_lu_gp(lu_gp);
2263}
2264
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002265static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002266 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002267};
2268
2269static struct config_item_type target_core_alua_lu_gp_cit = {
2270 .ct_item_ops = &target_core_alua_lu_gp_ops,
2271 .ct_attrs = target_core_alua_lu_gp_attrs,
2272 .ct_owner = THIS_MODULE,
2273};
2274
2275/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2276
2277/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2278
2279static struct config_group *target_core_alua_create_lu_gp(
2280 struct config_group *group,
2281 const char *name)
2282{
2283 struct t10_alua_lu_gp *lu_gp;
2284 struct config_group *alua_lu_gp_cg = NULL;
2285 struct config_item *alua_lu_gp_ci = NULL;
2286
2287 lu_gp = core_alua_allocate_lu_gp(name, 0);
2288 if (IS_ERR(lu_gp))
2289 return NULL;
2290
2291 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2292 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2293
2294 config_group_init_type_name(alua_lu_gp_cg, name,
2295 &target_core_alua_lu_gp_cit);
2296
Andy Grover6708bb22011-06-08 10:36:43 -07002297 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002298 " Group: core/alua/lu_gps/%s\n",
2299 config_item_name(alua_lu_gp_ci));
2300
2301 return alua_lu_gp_cg;
2302
2303}
2304
2305static void target_core_alua_drop_lu_gp(
2306 struct config_group *group,
2307 struct config_item *item)
2308{
2309 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2310 struct t10_alua_lu_gp, lu_gp_group);
2311
Andy Grover6708bb22011-06-08 10:36:43 -07002312 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002313 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2314 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002315 /*
2316 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2317 * -> target_core_alua_lu_gp_release()
2318 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002319 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002320}
2321
2322static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2323 .make_group = &target_core_alua_create_lu_gp,
2324 .drop_item = &target_core_alua_drop_lu_gp,
2325};
2326
2327static struct config_item_type target_core_alua_lu_gps_cit = {
2328 .ct_item_ops = NULL,
2329 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2330 .ct_owner = THIS_MODULE,
2331};
2332
2333/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2334
2335/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2336
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002337static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002338{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002339 return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
2340 tg_pt_gp_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002341}
2342
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002343static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2344 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002345{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002346 return sprintf(page, "%d\n",
2347 atomic_read(&to_tg_pt_gp(item)->tg_pt_gp_alua_access_state));
2348}
2349
2350static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
2351 const char *page, size_t count)
2352{
2353 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002354 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002355 unsigned long tmp;
2356 int new_state, ret;
2357
Andy Grover6708bb22011-06-08 10:36:43 -07002358 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002359 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002360 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2361 return -EINVAL;
2362 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07002363 if (!(dev->dev_flags & DF_CONFIGURED)) {
2364 pr_err("Unable to set alua_access_state while device is"
2365 " not configured\n");
2366 return -ENODEV;
2367 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002368
Jingoo Han57103d72013-07-19 16:22:19 +09002369 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002370 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002371 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002372 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002373 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002374 }
2375 new_state = (int)tmp;
2376
Hannes Reinecke125d0112013-11-19 09:07:46 +01002377 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2378 pr_err("Unable to process implicit configfs ALUA"
2379 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002380 return -EINVAL;
2381 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002382 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2383 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2384 /* LBA DEPENDENT is only allowed with implicit ALUA */
2385 pr_err("Unable to process implicit configfs ALUA transition"
2386 " while explicit ALUA management is enabled\n");
2387 return -EINVAL;
2388 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002389
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002390 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002391 NULL, NULL, new_state, 0);
2392 return (!ret) ? count : -EINVAL;
2393}
2394
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002395static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2396 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002397{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002398 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002399 return sprintf(page, "%s\n",
2400 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2401}
2402
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002403static ssize_t target_tg_pt_gp_alua_access_status_store(
2404 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002405{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002406 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002407 unsigned long tmp;
2408 int new_status, ret;
2409
Andy Grover6708bb22011-06-08 10:36:43 -07002410 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2411 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002412 " valid tg_pt_gp ID: %hu\n",
2413 tg_pt_gp->tg_pt_gp_valid_id);
2414 return -EINVAL;
2415 }
2416
Jingoo Han57103d72013-07-19 16:22:19 +09002417 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002418 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002419 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002420 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002421 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002422 }
2423 new_status = (int)tmp;
2424
2425 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002426 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2427 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002428 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002429 new_status);
2430 return -EINVAL;
2431 }
2432
2433 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2434 return count;
2435}
2436
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002437static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2438 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002439{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002440 return core_alua_show_access_type(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002441}
2442
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002443static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
2444 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002445{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002446 return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002447}
2448
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002449#define ALUA_SUPPORTED_STATE_ATTR(_name, _bit) \
2450static ssize_t target_tg_pt_gp_alua_support_##_name##_show( \
2451 struct config_item *item, char *p) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002452{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002453 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
2454 return sprintf(p, "%d\n", \
2455 !!(t->tg_pt_gp_alua_supported_states & _bit)); \
2456} \
2457 \
2458static ssize_t target_tg_pt_gp_alua_support_##_name##_store( \
2459 struct config_item *item, const char *p, size_t c) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002460{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002461 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002462 unsigned long tmp; \
2463 int ret; \
2464 \
2465 if (!t->tg_pt_gp_valid_id) { \
2466 pr_err("Unable to do set ##_name ALUA state on non" \
2467 " valid tg_pt_gp ID: %hu\n", \
2468 t->tg_pt_gp_valid_id); \
2469 return -EINVAL; \
2470 } \
2471 \
2472 ret = kstrtoul(p, 0, &tmp); \
2473 if (ret < 0) { \
2474 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2475 return -EINVAL; \
2476 } \
2477 if (tmp > 1) { \
2478 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2479 return -EINVAL; \
2480 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002481 if (tmp) \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002482 t->tg_pt_gp_alua_supported_states |= _bit; \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002483 else \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002484 t->tg_pt_gp_alua_supported_states &= ~_bit; \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002485 \
2486 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002487}
2488
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002489ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
2490ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
2491ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
2492ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
2493ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
2494ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
2495ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002496
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002497static ssize_t target_tg_pt_gp_alua_write_metadata_show(
2498 struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002499{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002500 return sprintf(page, "%d\n",
2501 to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002502}
2503
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002504static ssize_t target_tg_pt_gp_alua_write_metadata_store(
2505 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002506{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002507 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002508 unsigned long tmp;
2509 int ret;
2510
Jingoo Han57103d72013-07-19 16:22:19 +09002511 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002512 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002513 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002514 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002515 }
2516
2517 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002518 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002519 " %lu\n", tmp);
2520 return -EINVAL;
2521 }
2522 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2523
2524 return count;
2525}
2526
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002527static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2528 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002529{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002530 return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002531}
2532
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002533static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
2534 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002535{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002536 return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
2537 count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538}
2539
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002540static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2541 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002542{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002543 return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002544}
2545
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002546static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
2547 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002548{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002549 return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
2550 count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002551}
2552
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002553static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
2554 struct config_item *item, char *page)
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002555{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002556 return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002557}
2558
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002559static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
2560 struct config_item *item, const char *page, size_t count)
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002561{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002562 return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
2563 count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002564}
2565
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002566static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
2567 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002568{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002569 return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002570}
2571
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002572static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
2573 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002574{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002575 return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002576}
2577
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002578static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2579 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002580{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002581 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2582
Andy Grover6708bb22011-06-08 10:36:43 -07002583 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002584 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002585 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2586}
2587
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002588static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
2589 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002590{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002591 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002592 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2593 unsigned long tg_pt_gp_id;
2594 int ret;
2595
Jingoo Han57103d72013-07-19 16:22:19 +09002596 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002597 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002598 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002599 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002600 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002601 }
2602 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002603 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002604 " 0x0000ffff\n", tg_pt_gp_id);
2605 return -EINVAL;
2606 }
2607
2608 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2609 if (ret < 0)
2610 return -EINVAL;
2611
Andy Grover6708bb22011-06-08 10:36:43 -07002612 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002613 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2614 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2615 tg_pt_gp->tg_pt_gp_id);
2616
2617 return count;
2618}
2619
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002620static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2621 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002622{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002623 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002624 struct se_lun *lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002625 ssize_t len = 0, cur_len;
2626 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2627
2628 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2629
2630 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002631 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2632 lun_tg_pt_gp_link) {
2633 struct se_portal_group *tpg = lun->lun_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002634
2635 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002636 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2637 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2638 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002639 config_item_name(&lun->lun_group.cg_item));
2640 cur_len++; /* Extra byte for NULL terminator */
2641
2642 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002643 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002644 "_members buffer\n");
2645 break;
2646 }
2647 memcpy(page+len, buf, cur_len);
2648 len += cur_len;
2649 }
2650 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2651
2652 return len;
2653}
2654
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002655CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
2656CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
2657CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
2658CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
2659CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
2660CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
2661CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
2662CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
2663CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
2664CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
2665CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
2666CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
2667CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
2668CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
2669CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
2670CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
2671CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002672
2673static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002674 &target_tg_pt_gp_attr_alua_access_state,
2675 &target_tg_pt_gp_attr_alua_access_status,
2676 &target_tg_pt_gp_attr_alua_access_type,
2677 &target_tg_pt_gp_attr_alua_support_transitioning,
2678 &target_tg_pt_gp_attr_alua_support_offline,
2679 &target_tg_pt_gp_attr_alua_support_lba_dependent,
2680 &target_tg_pt_gp_attr_alua_support_unavailable,
2681 &target_tg_pt_gp_attr_alua_support_standby,
2682 &target_tg_pt_gp_attr_alua_support_active_nonoptimized,
2683 &target_tg_pt_gp_attr_alua_support_active_optimized,
2684 &target_tg_pt_gp_attr_alua_write_metadata,
2685 &target_tg_pt_gp_attr_nonop_delay_msecs,
2686 &target_tg_pt_gp_attr_trans_delay_msecs,
2687 &target_tg_pt_gp_attr_implicit_trans_secs,
2688 &target_tg_pt_gp_attr_preferred,
2689 &target_tg_pt_gp_attr_tg_pt_gp_id,
2690 &target_tg_pt_gp_attr_members,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002691 NULL,
2692};
2693
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002694static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2695{
2696 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2697 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2698
2699 core_alua_free_tg_pt_gp(tg_pt_gp);
2700}
2701
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002702static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002703 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002704};
2705
2706static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2707 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2708 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2709 .ct_owner = THIS_MODULE,
2710};
2711
2712/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2713
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002714/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002715
2716static struct config_group *target_core_alua_create_tg_pt_gp(
2717 struct config_group *group,
2718 const char *name)
2719{
2720 struct t10_alua *alua = container_of(group, struct t10_alua,
2721 alua_tg_pt_gps_group);
2722 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002723 struct config_group *alua_tg_pt_gp_cg = NULL;
2724 struct config_item *alua_tg_pt_gp_ci = NULL;
2725
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002726 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002727 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002728 return NULL;
2729
2730 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2731 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2732
2733 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2734 &target_core_alua_tg_pt_gp_cit);
2735
Andy Grover6708bb22011-06-08 10:36:43 -07002736 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002737 " Group: alua/tg_pt_gps/%s\n",
2738 config_item_name(alua_tg_pt_gp_ci));
2739
2740 return alua_tg_pt_gp_cg;
2741}
2742
2743static void target_core_alua_drop_tg_pt_gp(
2744 struct config_group *group,
2745 struct config_item *item)
2746{
2747 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2748 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2749
Andy Grover6708bb22011-06-08 10:36:43 -07002750 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002751 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2752 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002753 /*
2754 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2755 * -> target_core_alua_tg_pt_gp_release().
2756 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002757 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002758}
2759
2760static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2761 .make_group = &target_core_alua_create_tg_pt_gp,
2762 .drop_item = &target_core_alua_drop_tg_pt_gp,
2763};
2764
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002765TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002766
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002767/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002768
2769/* Start functions for struct config_item_type target_core_alua_cit */
2770
2771/*
2772 * target_core_alua_cit is a ConfigFS group that lives under
2773 * /sys/kernel/config/target/core/alua. There are default groups
2774 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2775 * target_core_alua_cit in target_core_init_configfs() below.
2776 */
2777static struct config_item_type target_core_alua_cit = {
2778 .ct_item_ops = NULL,
2779 .ct_attrs = NULL,
2780 .ct_owner = THIS_MODULE,
2781};
2782
2783/* End functions for struct config_item_type target_core_alua_cit */
2784
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002785/* Start functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002786
2787static struct config_group *target_core_stat_mkdir(
2788 struct config_group *group,
2789 const char *name)
2790{
2791 return ERR_PTR(-ENOSYS);
2792}
2793
2794static void target_core_stat_rmdir(
2795 struct config_group *group,
2796 struct config_item *item)
2797{
2798 return;
2799}
2800
2801static struct configfs_group_operations target_core_stat_group_ops = {
2802 .make_group = &target_core_stat_mkdir,
2803 .drop_item = &target_core_stat_rmdir,
2804};
2805
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002806TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002807
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002808/* End functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002809
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002810/* Start functions for struct config_item_type target_core_hba_cit */
2811
2812static struct config_group *target_core_make_subdev(
2813 struct config_group *group,
2814 const char *name)
2815{
2816 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002817 struct config_item *hba_ci = &group->cg_item;
2818 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002819 struct target_backend *tb = hba->backend;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002820 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002821 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002822 struct config_group *dev_stat_grp = NULL;
2823 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002824
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002825 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2826 if (ret)
2827 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002828
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002829 dev = target_alloc_device(hba, name);
2830 if (!dev)
2831 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002832
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002833 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002834
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002835 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002836 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002837 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002838 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002839
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002840 config_group_init_type_name(dev_cg, name, &tb->tb_dev_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002841 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002842 &tb->tb_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002843 config_group_init_type_name(&dev->dev_pr_group, "pr",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002844 &tb->tb_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002845 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002846 &tb->tb_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002847 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002848 "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002849 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002850 "statistics", &tb->tb_dev_stat_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002851
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002852 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2853 dev_cg->default_groups[1] = &dev->dev_pr_group;
2854 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2855 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2856 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002857 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002858 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002859 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002860 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002861 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002862 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002863 goto out_free_dev_cg_default_groups;
2864 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002865
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002866 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002867 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002868 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002869 if (!tg_pt_gp_cg->default_groups) {
2870 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002871 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002872 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002873 }
2874
2875 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2876 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2877 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2878 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002879 /*
2880 * Add core/$HBA/$DEV/statistics/ default groups
2881 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002882 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002883 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002884 GFP_KERNEL);
2885 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002886 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002887 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002888 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002889 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002890
2891 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002892 return dev_cg;
2893
2894out_free_tg_pt_gp_cg_default_groups:
2895 kfree(tg_pt_gp_cg->default_groups);
2896out_free_tg_pt_gp:
2897 core_alua_free_tg_pt_gp(tg_pt_gp);
2898out_free_dev_cg_default_groups:
2899 kfree(dev_cg->default_groups);
2900out_free_device:
2901 target_free_device(dev);
2902out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002903 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002904 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002905}
2906
2907static void target_core_drop_subdev(
2908 struct config_group *group,
2909 struct config_item *item)
2910{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002911 struct config_group *dev_cg = to_config_group(item);
2912 struct se_device *dev =
2913 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002914 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002915 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002916 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002917 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002918
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002919 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002920
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002921 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002922
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002923 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002924 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2925 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2926 dev_stat_grp->default_groups[i] = NULL;
2927 config_item_put(df_item);
2928 }
2929 kfree(dev_stat_grp->default_groups);
2930
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002931 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002932 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2933 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2934 tg_pt_gp_cg->default_groups[i] = NULL;
2935 config_item_put(df_item);
2936 }
2937 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002938 /*
2939 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2940 * directly from target_core_alua_tg_pt_gp_release().
2941 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002942 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002943
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002944 for (i = 0; dev_cg->default_groups[i]; i++) {
2945 df_item = &dev_cg->default_groups[i]->cg_item;
2946 dev_cg->default_groups[i] = NULL;
2947 config_item_put(df_item);
2948 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002949 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002950 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002951 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002952 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002953 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002954}
2955
2956static struct configfs_group_operations target_core_hba_group_ops = {
2957 .make_group = target_core_make_subdev,
2958 .drop_item = target_core_drop_subdev,
2959};
2960
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002961
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002962static inline struct se_hba *to_hba(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002963{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002964 return container_of(to_config_group(item), struct se_hba, hba_group);
2965}
2966
2967static ssize_t target_hba_info_show(struct config_item *item, char *page)
2968{
2969 struct se_hba *hba = to_hba(item);
2970
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002971 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002972 hba->hba_id, hba->backend->ops->name,
Christoph Hellwigce8dd252015-06-19 15:14:39 +02002973 TARGET_CORE_VERSION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002974}
2975
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002976static ssize_t target_hba_mode_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002977{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002978 struct se_hba *hba = to_hba(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002979 int hba_mode = 0;
2980
2981 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2982 hba_mode = 1;
2983
2984 return sprintf(page, "%d\n", hba_mode);
2985}
2986
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002987static ssize_t target_hba_mode_store(struct config_item *item,
2988 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002989{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002990 struct se_hba *hba = to_hba(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002991 unsigned long mode_flag;
2992 int ret;
2993
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002994 if (hba->backend->ops->pmode_enable_hba == NULL)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002995 return -EINVAL;
2996
Jingoo Han57103d72013-07-19 16:22:19 +09002997 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002998 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002999 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003000 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003001 }
3002
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003003 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07003004 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003005 return -EINVAL;
3006 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003007
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003008 ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003009 if (ret < 0)
3010 return -EINVAL;
3011 if (ret > 0)
3012 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3013 else if (ret == 0)
3014 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3015
3016 return count;
3017}
3018
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003019CONFIGFS_ATTR_RO(target_, hba_info);
3020CONFIGFS_ATTR(target_, hba_mode);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003021
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003022static void target_core_hba_release(struct config_item *item)
3023{
3024 struct se_hba *hba = container_of(to_config_group(item),
3025 struct se_hba, hba_group);
3026 core_delete_hba(hba);
3027}
3028
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003029static struct configfs_attribute *target_core_hba_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003030 &target_attr_hba_info,
3031 &target_attr_hba_mode,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003032 NULL,
3033};
3034
3035static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003036 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003037};
3038
3039static struct config_item_type target_core_hba_cit = {
3040 .ct_item_ops = &target_core_hba_item_ops,
3041 .ct_group_ops = &target_core_hba_group_ops,
3042 .ct_attrs = target_core_hba_attrs,
3043 .ct_owner = THIS_MODULE,
3044};
3045
3046static struct config_group *target_core_call_addhbatotarget(
3047 struct config_group *group,
3048 const char *name)
3049{
3050 char *se_plugin_str, *str, *str2;
3051 struct se_hba *hba;
3052 char buf[TARGET_CORE_NAME_MAX_LEN];
3053 unsigned long plugin_dep_id = 0;
3054 int ret;
3055
3056 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07003057 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07003058 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003059 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3060 TARGET_CORE_NAME_MAX_LEN);
3061 return ERR_PTR(-ENAMETOOLONG);
3062 }
3063 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3064
3065 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003066 if (!str) {
3067 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003068 return ERR_PTR(-EINVAL);
3069 }
3070 se_plugin_str = buf;
3071 /*
3072 * Special case for subsystem plugins that have "_" in their names.
3073 * Namely rd_direct and rd_mcp..
3074 */
3075 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003076 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003077 *str2 = '\0'; /* Terminate for *se_plugin_str */
3078 str2++; /* Skip to start of plugin dependent ID */
3079 str = str2;
3080 } else {
3081 *str = '\0'; /* Terminate for *se_plugin_str */
3082 str++; /* Skip to start of plugin dependent ID */
3083 }
3084
Jingoo Han57103d72013-07-19 16:22:19 +09003085 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003086 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09003087 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003088 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003089 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003090 }
3091 /*
3092 * Load up TCM subsystem plugins if they have not already been loaded.
3093 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07003094 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003095
3096 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3097 if (IS_ERR(hba))
3098 return ERR_CAST(hba);
3099
3100 config_group_init_type_name(&hba->hba_group, name,
3101 &target_core_hba_cit);
3102
3103 return &hba->hba_group;
3104}
3105
3106static void target_core_call_delhbafromtarget(
3107 struct config_group *group,
3108 struct config_item *item)
3109{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003110 /*
3111 * core_delete_hba() is called from target_core_hba_item_ops->release()
3112 * -> target_core_hba_release()
3113 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003114 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003115}
3116
3117static struct configfs_group_operations target_core_group_ops = {
3118 .make_group = target_core_call_addhbatotarget,
3119 .drop_item = target_core_call_delhbafromtarget,
3120};
3121
3122static struct config_item_type target_core_cit = {
3123 .ct_item_ops = NULL,
3124 .ct_group_ops = &target_core_group_ops,
3125 .ct_attrs = NULL,
3126 .ct_owner = THIS_MODULE,
3127};
3128
3129/* Stop functions for struct config_item_type target_core_hba_cit */
3130
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003131void target_setup_backend_cits(struct target_backend *tb)
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003132{
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003133 target_core_setup_dev_cit(tb);
3134 target_core_setup_dev_attrib_cit(tb);
3135 target_core_setup_dev_pr_cit(tb);
3136 target_core_setup_dev_wwn_cit(tb);
3137 target_core_setup_dev_alua_tg_pt_gps_cit(tb);
3138 target_core_setup_dev_stat_cit(tb);
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003139}
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003140
Axel Lin54550fa2011-03-14 04:06:09 -07003141static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003142{
3143 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3144 struct config_group *lu_gp_cg = NULL;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003145 struct configfs_subsystem *subsys = &target_core_fabrics;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003146 struct t10_alua_lu_gp *lu_gp;
3147 int ret;
3148
Andy Grover6708bb22011-06-08 10:36:43 -07003149 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003150 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3151 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3152
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003153 config_group_init(&subsys->su_group);
3154 mutex_init(&subsys->su_mutex);
3155
Andy Grovere3d6f902011-07-19 08:55:10 +00003156 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003157 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003158 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003159 /*
3160 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3161 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3162 */
3163 target_cg = &subsys->su_group;
Andy Groverab6dae82013-12-09 14:27:36 -08003164 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003165 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003166 if (!target_cg->default_groups) {
3167 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003168 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003169 goto out_global;
3170 }
3171
Andy Grovere3d6f902011-07-19 08:55:10 +00003172 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003173 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003174 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003175 target_cg->default_groups[1] = NULL;
3176 /*
3177 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3178 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003179 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003180 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003181 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003182 if (!hba_cg->default_groups) {
3183 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003184 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003185 goto out_global;
3186 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003187 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003188 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003189 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003190 hba_cg->default_groups[1] = NULL;
3191 /*
3192 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3193 * groups under /sys/kernel/config/target/core/alua/
3194 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003195 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003196 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003197 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003198 if (!alua_cg->default_groups) {
3199 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003200 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003201 goto out_global;
3202 }
3203
Andy Grovere3d6f902011-07-19 08:55:10 +00003204 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003205 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003206 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003207 alua_cg->default_groups[1] = NULL;
3208 /*
3209 * Add core/alua/lu_gps/default_lu_gp
3210 */
3211 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003212 if (IS_ERR(lu_gp)) {
3213 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003214 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003215 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003216
Andy Grovere3d6f902011-07-19 08:55:10 +00003217 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003218 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003219 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003220 if (!lu_gp_cg->default_groups) {
3221 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003222 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003223 goto out_global;
3224 }
3225
3226 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3227 &target_core_alua_lu_gp_cit);
3228 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3229 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003230 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003231 /*
3232 * Register the target_core_mod subsystem with configfs.
3233 */
3234 ret = configfs_register_subsystem(subsys);
3235 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003236 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003237 ret, subsys->su_group.cg_item.ci_namebuf);
3238 goto out_global;
3239 }
Andy Grover6708bb22011-06-08 10:36:43 -07003240 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Christoph Hellwigce8dd252015-06-19 15:14:39 +02003241 " Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003242 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3243 /*
3244 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3245 */
3246 ret = rd_module_init();
3247 if (ret < 0)
3248 goto out;
3249
Roland Dreier0d0f9df2012-10-31 09:16:44 -07003250 ret = core_dev_setup_virtual_lun0();
3251 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003252 goto out;
3253
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003254 ret = target_xcopy_setup_pt();
3255 if (ret < 0)
3256 goto out;
3257
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003258 return 0;
3259
3260out:
3261 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003262 core_dev_release_virtual_lun0();
3263 rd_module_exit();
3264out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00003265 if (default_lu_gp) {
3266 core_alua_free_lu_gp(default_lu_gp);
3267 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003268 }
3269 if (lu_gp_cg)
3270 kfree(lu_gp_cg->default_groups);
3271 if (alua_cg)
3272 kfree(alua_cg->default_groups);
3273 if (hba_cg)
3274 kfree(hba_cg->default_groups);
3275 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00003276 release_se_kmem_caches();
3277 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003278}
3279
Axel Lin54550fa2011-03-14 04:06:09 -07003280static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003281{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003282 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3283 struct config_item *item;
3284 int i;
3285
Andy Grovere3d6f902011-07-19 08:55:10 +00003286 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003287 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3288 item = &lu_gp_cg->default_groups[i]->cg_item;
3289 lu_gp_cg->default_groups[i] = NULL;
3290 config_item_put(item);
3291 }
3292 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003293 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003294
Andy Grovere3d6f902011-07-19 08:55:10 +00003295 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003296 for (i = 0; alua_cg->default_groups[i]; i++) {
3297 item = &alua_cg->default_groups[i]->cg_item;
3298 alua_cg->default_groups[i] = NULL;
3299 config_item_put(item);
3300 }
3301 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003302 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003303
Andy Grovere3d6f902011-07-19 08:55:10 +00003304 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003305 for (i = 0; hba_cg->default_groups[i]; i++) {
3306 item = &hba_cg->default_groups[i]->cg_item;
3307 hba_cg->default_groups[i] = NULL;
3308 config_item_put(item);
3309 }
3310 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003311 hba_cg->default_groups = NULL;
3312 /*
3313 * We expect subsys->su_group.default_groups to be released
3314 * by configfs subsystem provider logic..
3315 */
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003316 configfs_unregister_subsystem(&target_core_fabrics);
3317 kfree(target_core_fabrics.su_group.default_groups);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003318
Andy Grovere3d6f902011-07-19 08:55:10 +00003319 core_alua_free_lu_gp(default_lu_gp);
3320 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e92011-02-09 15:34:53 -08003321
Andy Grover6708bb22011-06-08 10:36:43 -07003322 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003323 " Infrastructure\n");
3324
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003325 core_dev_release_virtual_lun0();
3326 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003327 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003328 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003329}
3330
3331MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3332MODULE_AUTHOR("nab@Linux-iSCSI.org");
3333MODULE_LICENSE("GPL");
3334
3335module_init(target_core_init_configfs);
3336module_exit(target_core_exit_configfs);