blob: e656b1cef4dae2ca1c9da3e118e2a89afff6bfc7 [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 Pocase6f41632015-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 \
Christophe Vu-Brugier234bdbc2015-11-18 09:22:58 +0100552 "ignoring deprecated %s attribute\n", \
553 __stringify(_name)); \
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200554 return count; \
555}
556
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200557DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
558DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200559
560static void dev_set_t10_wwn_model_alias(struct se_device *dev)
561{
562 const char *configname;
563
564 configname = config_item_name(&dev->dev_group.cg_item);
565 if (strlen(configname) >= 16) {
566 pr_warn("dev[%p]: Backstore name '%s' is too long for "
567 "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
568 configname);
569 }
570 snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
571}
572
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200573static ssize_t emulate_model_alias_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200574 const char *page, size_t count)
575{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200576 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200577 struct se_device *dev = da->da_dev;
578 bool flag;
579 int ret;
580
581 if (dev->export_count) {
582 pr_err("dev[%p]: Unable to change model alias"
583 " while export_count is %d\n",
584 dev, dev->export_count);
585 return -EINVAL;
586 }
587
588 ret = strtobool(page, &flag);
589 if (ret < 0)
590 return ret;
591
592 if (flag) {
593 dev_set_t10_wwn_model_alias(dev);
594 } else {
595 strncpy(&dev->t10_wwn.model[0],
596 dev->transport->inquiry_prod, 16);
597 }
598 da->emulate_model_alias = flag;
599 return count;
600}
601
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200602static ssize_t emulate_write_cache_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200603 const char *page, size_t count)
604{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200605 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200606 bool flag;
607 int ret;
608
609 ret = strtobool(page, &flag);
610 if (ret < 0)
611 return ret;
612
613 if (flag && da->da_dev->transport->get_write_cache) {
614 pr_err("emulate_write_cache not supported for this device\n");
615 return -EINVAL;
616 }
617
618 da->emulate_write_cache = flag;
619 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
620 da->da_dev, flag);
621 return count;
622}
623
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200624static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200625 const char *page, size_t count)
626{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200627 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200628 u32 val;
629 int ret;
630
631 ret = kstrtou32(page, 0, &val);
632 if (ret < 0)
633 return ret;
634
635 if (val != 0 && val != 1 && val != 2) {
636 pr_err("Illegal value %d\n", val);
637 return -EINVAL;
638 }
639
640 if (da->da_dev->export_count) {
641 pr_err("dev[%p]: Unable to change SE Device"
642 " UA_INTRLCK_CTRL while export_count is %d\n",
643 da->da_dev, da->da_dev->export_count);
644 return -EINVAL;
645 }
646 da->emulate_ua_intlck_ctrl = val;
647 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
648 da->da_dev, val);
649 return count;
650}
651
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200652static ssize_t emulate_tas_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200653 const char *page, size_t count)
654{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200655 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200656 bool flag;
657 int ret;
658
659 ret = strtobool(page, &flag);
660 if (ret < 0)
661 return ret;
662
663 if (da->da_dev->export_count) {
664 pr_err("dev[%p]: Unable to change SE Device TAS while"
665 " export_count is %d\n",
666 da->da_dev, da->da_dev->export_count);
667 return -EINVAL;
668 }
669 da->emulate_tas = flag;
670 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
671 da->da_dev, flag ? "Enabled" : "Disabled");
672
673 return count;
674}
675
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200676static ssize_t emulate_tpu_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200677 const char *page, size_t count)
678{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200679 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200680 bool flag;
681 int ret;
682
683 ret = strtobool(page, &flag);
684 if (ret < 0)
685 return ret;
686
687 /*
688 * We expect this value to be non-zero when generic Block Layer
689 * Discard supported is detected iblock_create_virtdevice().
690 */
691 if (flag && !da->max_unmap_block_desc_count) {
692 pr_err("Generic Block Discard not supported\n");
693 return -ENOSYS;
694 }
695
696 da->emulate_tpu = flag;
697 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
698 da->da_dev, flag);
699 return count;
700}
701
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200702static ssize_t emulate_tpws_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200703 const char *page, size_t count)
704{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200705 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200706 bool flag;
707 int ret;
708
709 ret = strtobool(page, &flag);
710 if (ret < 0)
711 return ret;
712
713 /*
714 * We expect this value to be non-zero when generic Block Layer
715 * Discard supported is detected iblock_create_virtdevice().
716 */
717 if (flag && !da->max_unmap_block_desc_count) {
718 pr_err("Generic Block Discard not supported\n");
719 return -ENOSYS;
720 }
721
722 da->emulate_tpws = flag;
723 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
724 da->da_dev, flag);
725 return count;
726}
727
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200728static ssize_t pi_prot_type_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200729 const char *page, size_t count)
730{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200731 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200732 int old_prot = da->pi_prot_type, ret;
733 struct se_device *dev = da->da_dev;
734 u32 flag;
735
736 ret = kstrtou32(page, 0, &flag);
737 if (ret < 0)
738 return ret;
739
740 if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
741 pr_err("Illegal value %d for pi_prot_type\n", flag);
742 return -EINVAL;
743 }
744 if (flag == 2) {
745 pr_err("DIF TYPE2 protection currently not supported\n");
746 return -ENOSYS;
747 }
748 if (da->hw_pi_prot_type) {
749 pr_warn("DIF protection enabled on underlying hardware,"
750 " ignoring\n");
751 return count;
752 }
753 if (!dev->transport->init_prot || !dev->transport->free_prot) {
754 /* 0 is only allowed value for non-supporting backends */
755 if (flag == 0)
Andy Groverbc1a7d62015-07-09 09:56:47 -0700756 return count;
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200757
758 pr_err("DIF protection not supported by backend: %s\n",
759 dev->transport->name);
760 return -ENOSYS;
761 }
762 if (!(dev->dev_flags & DF_CONFIGURED)) {
763 pr_err("DIF protection requires device to be configured\n");
764 return -ENODEV;
765 }
766 if (dev->export_count) {
767 pr_err("dev[%p]: Unable to change SE Device PROT type while"
768 " export_count is %d\n", dev, dev->export_count);
769 return -EINVAL;
770 }
771
772 da->pi_prot_type = flag;
773
774 if (flag && !old_prot) {
775 ret = dev->transport->init_prot(dev);
776 if (ret) {
777 da->pi_prot_type = old_prot;
778 return ret;
779 }
780
781 } else if (!flag && old_prot) {
782 dev->transport->free_prot(dev);
783 }
784
785 pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
786 return count;
787}
788
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200789static ssize_t pi_prot_format_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200790 const char *page, size_t count)
791{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200792 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200793 struct se_device *dev = da->da_dev;
794 bool flag;
795 int ret;
796
797 ret = strtobool(page, &flag);
798 if (ret < 0)
799 return ret;
800
801 if (!flag)
802 return count;
803
804 if (!dev->transport->format_prot) {
805 pr_err("DIF protection format not supported by backend %s\n",
806 dev->transport->name);
807 return -ENOSYS;
808 }
809 if (!(dev->dev_flags & DF_CONFIGURED)) {
810 pr_err("DIF protection format requires device to be configured\n");
811 return -ENODEV;
812 }
813 if (dev->export_count) {
814 pr_err("dev[%p]: Unable to format SE Device PROT type while"
815 " export_count is %d\n", dev, dev->export_count);
816 return -EINVAL;
817 }
818
819 ret = dev->transport->format_prot(dev);
820 if (ret)
821 return ret;
822
823 pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
824 return count;
825}
826
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200827static ssize_t force_pr_aptpl_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200828 const char *page, size_t count)
829{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200830 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200831 bool flag;
832 int ret;
833
834 ret = strtobool(page, &flag);
835 if (ret < 0)
836 return ret;
837 if (da->da_dev->export_count) {
838 pr_err("dev[%p]: Unable to set force_pr_aptpl while"
839 " export_count is %d\n",
840 da->da_dev, da->da_dev->export_count);
841 return -EINVAL;
842 }
843
844 da->force_pr_aptpl = flag;
845 pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
846 return count;
847}
848
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200849static ssize_t emulate_rest_reord_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200850 const char *page, size_t count)
851{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200852 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200853 bool flag;
854 int ret;
855
856 ret = strtobool(page, &flag);
857 if (ret < 0)
858 return ret;
859
860 if (flag != 0) {
861 printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
862 " reordering not implemented\n", da->da_dev);
863 return -ENOSYS;
864 }
865 da->emulate_rest_reord = flag;
866 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
867 da->da_dev, flag);
868 return count;
869}
870
Jamie Pocase6f41632015-11-29 14:44:57 -0800871static ssize_t unmap_zeroes_data_store(struct config_item *item,
872 const char *page, size_t count)
873{
874 struct se_dev_attrib *da = to_attrib(item);
875 bool flag;
876 int ret;
877
878 ret = strtobool(page, &flag);
879 if (ret < 0)
880 return ret;
881
882 if (da->da_dev->export_count) {
883 pr_err("dev[%p]: Unable to change SE Device"
884 " unmap_zeroes_data while export_count is %d\n",
885 da->da_dev, da->da_dev->export_count);
886 return -EINVAL;
887 }
888 /*
889 * We expect this value to be non-zero when generic Block Layer
890 * Discard supported is detected iblock_configure_device().
891 */
892 if (flag && !da->max_unmap_block_desc_count) {
893 pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
894 " because max_unmap_block_desc_count is zero\n",
895 da->da_dev);
896 return -ENOSYS;
897 }
898 da->unmap_zeroes_data = flag;
899 pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
900 da->da_dev, flag);
901 return 0;
902}
903
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200904/*
905 * Note, this can only be called on unexported SE Device Object.
906 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200907static ssize_t queue_depth_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200908 const char *page, size_t count)
909{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200910 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200911 struct se_device *dev = da->da_dev;
912 u32 val;
913 int ret;
914
915 ret = kstrtou32(page, 0, &val);
916 if (ret < 0)
917 return ret;
918
919 if (dev->export_count) {
920 pr_err("dev[%p]: Unable to change SE Device TCQ while"
921 " export_count is %d\n",
922 dev, dev->export_count);
923 return -EINVAL;
924 }
925 if (!val) {
926 pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
927 return -EINVAL;
928 }
929
930 if (val > dev->dev_attrib.queue_depth) {
931 if (val > dev->dev_attrib.hw_queue_depth) {
932 pr_err("dev[%p]: Passed queue_depth:"
933 " %u exceeds TCM/SE_Device MAX"
934 " TCQ: %u\n", dev, val,
935 dev->dev_attrib.hw_queue_depth);
936 return -EINVAL;
937 }
938 }
939 da->queue_depth = dev->queue_depth = val;
940 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
941 return count;
942}
943
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200944static ssize_t optimal_sectors_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200945 const char *page, size_t count)
946{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200947 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200948 u32 val;
949 int ret;
950
951 ret = kstrtou32(page, 0, &val);
952 if (ret < 0)
953 return ret;
954
955 if (da->da_dev->export_count) {
956 pr_err("dev[%p]: Unable to change SE Device"
957 " optimal_sectors while export_count is %d\n",
958 da->da_dev, da->da_dev->export_count);
959 return -EINVAL;
960 }
961 if (val > da->hw_max_sectors) {
962 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
963 " greater than hw_max_sectors: %u\n",
964 da->da_dev, val, da->hw_max_sectors);
965 return -EINVAL;
966 }
967
968 da->optimal_sectors = val;
969 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
970 da->da_dev, val);
971 return count;
972}
973
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200974static ssize_t block_size_store(struct config_item *item,
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200975 const char *page, size_t count)
976{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200977 struct se_dev_attrib *da = to_attrib(item);
Christoph Hellwig3effdb92015-05-10 18:14:58 +0200978 u32 val;
979 int ret;
980
981 ret = kstrtou32(page, 0, &val);
982 if (ret < 0)
983 return ret;
984
985 if (da->da_dev->export_count) {
986 pr_err("dev[%p]: Unable to change SE Device block_size"
987 " while export_count is %d\n",
988 da->da_dev, da->da_dev->export_count);
989 return -EINVAL;
990 }
991
992 if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
993 pr_err("dev[%p]: Illegal value for block_device: %u"
994 " for SE device, must be 512, 1024, 2048 or 4096\n",
995 da->da_dev, val);
996 return -EINVAL;
997 }
998
999 da->block_size = val;
1000 if (da->max_bytes_per_io)
1001 da->hw_max_sectors = da->max_bytes_per_io / val;
1002
1003 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
1004 da->da_dev, val);
1005 return count;
1006}
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001007
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001008CONFIGFS_ATTR(, emulate_model_alias);
1009CONFIGFS_ATTR(, emulate_dpo);
1010CONFIGFS_ATTR(, emulate_fua_write);
1011CONFIGFS_ATTR(, emulate_fua_read);
1012CONFIGFS_ATTR(, emulate_write_cache);
1013CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
1014CONFIGFS_ATTR(, emulate_tas);
1015CONFIGFS_ATTR(, emulate_tpu);
1016CONFIGFS_ATTR(, emulate_tpws);
1017CONFIGFS_ATTR(, emulate_caw);
1018CONFIGFS_ATTR(, emulate_3pc);
1019CONFIGFS_ATTR(, pi_prot_type);
1020CONFIGFS_ATTR_RO(, hw_pi_prot_type);
1021CONFIGFS_ATTR(, pi_prot_format);
1022CONFIGFS_ATTR(, enforce_pr_isids);
1023CONFIGFS_ATTR(, is_nonrot);
1024CONFIGFS_ATTR(, emulate_rest_reord);
1025CONFIGFS_ATTR(, force_pr_aptpl);
1026CONFIGFS_ATTR_RO(, hw_block_size);
1027CONFIGFS_ATTR(, block_size);
1028CONFIGFS_ATTR_RO(, hw_max_sectors);
1029CONFIGFS_ATTR(, optimal_sectors);
1030CONFIGFS_ATTR_RO(, hw_queue_depth);
1031CONFIGFS_ATTR(, queue_depth);
1032CONFIGFS_ATTR(, max_unmap_lba_count);
1033CONFIGFS_ATTR(, max_unmap_block_desc_count);
1034CONFIGFS_ATTR(, unmap_granularity);
1035CONFIGFS_ATTR(, unmap_granularity_alignment);
Jamie Pocase6f41632015-11-29 14:44:57 -08001036CONFIGFS_ATTR(, unmap_zeroes_data);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001037CONFIGFS_ATTR(, max_write_same_len);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001038
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001039/*
1040 * dev_attrib attributes for devices using the target core SBC/SPC
1041 * interpreter. Any backend using spc_parse_cdb should be using
1042 * these.
1043 */
1044struct configfs_attribute *sbc_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001045 &attr_emulate_model_alias,
1046 &attr_emulate_dpo,
1047 &attr_emulate_fua_write,
1048 &attr_emulate_fua_read,
1049 &attr_emulate_write_cache,
1050 &attr_emulate_ua_intlck_ctrl,
1051 &attr_emulate_tas,
1052 &attr_emulate_tpu,
1053 &attr_emulate_tpws,
1054 &attr_emulate_caw,
1055 &attr_emulate_3pc,
1056 &attr_pi_prot_type,
1057 &attr_hw_pi_prot_type,
1058 &attr_pi_prot_format,
1059 &attr_enforce_pr_isids,
1060 &attr_is_nonrot,
1061 &attr_emulate_rest_reord,
1062 &attr_force_pr_aptpl,
1063 &attr_hw_block_size,
1064 &attr_block_size,
1065 &attr_hw_max_sectors,
1066 &attr_optimal_sectors,
1067 &attr_hw_queue_depth,
1068 &attr_queue_depth,
1069 &attr_max_unmap_lba_count,
1070 &attr_max_unmap_block_desc_count,
1071 &attr_unmap_granularity,
1072 &attr_unmap_granularity_alignment,
Jamie Pocase6f41632015-11-29 14:44:57 -08001073 &attr_unmap_zeroes_data,
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001074 &attr_max_write_same_len,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001075 NULL,
1076};
1077EXPORT_SYMBOL(sbc_attrib_attrs);
1078
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001079/*
1080 * Minimal dev_attrib attributes for devices passing through CDBs.
1081 * In this case we only provide a few read-only attributes for
1082 * backwards compatibility.
1083 */
1084struct configfs_attribute *passthrough_attrib_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001085 &attr_hw_pi_prot_type,
1086 &attr_hw_block_size,
1087 &attr_hw_max_sectors,
1088 &attr_hw_queue_depth,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001089 NULL,
1090};
1091EXPORT_SYMBOL(passthrough_attrib_attrs);
1092
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001093TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001094
Nicholas Bellingerf79a8972014-11-27 14:51:14 -08001095/* End functions for struct config_item_type tb_dev_attrib_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001096
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001097/* Start functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001098
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001099static struct t10_wwn *to_t10_wwn(struct config_item *item)
1100{
1101 return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
1102}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001103
1104/*
1105 * VPD page 0x80 Unit serial
1106 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001107static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1108 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001109{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001110 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001111 &to_t10_wwn(item)->unit_serial[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001112}
1113
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001114static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
1115 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001116{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001117 struct t10_wwn *t10_wwn = to_t10_wwn(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001118 struct se_device *dev = t10_wwn->t10_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001119 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1120
1121 /*
1122 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1123 * from the struct scsi_device level firmware, do not allow
1124 * VPD Unit Serial to be emulated.
1125 *
1126 * Note this struct scsi_device could also be emulating VPD
1127 * information from its drivers/scsi LLD. But for now we assume
1128 * it is doing 'the right thing' wrt a world wide unique
1129 * VPD Unit Serial Number that OS dependent multipath can depend on.
1130 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001131 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
Andy Grover6708bb22011-06-08 10:36:43 -07001132 pr_err("Underlying SCSI device firmware provided VPD"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001133 " Unit Serial, ignoring request\n");
1134 return -EOPNOTSUPP;
1135 }
1136
Dan Carpenter60d645a2011-06-15 10:03:05 -07001137 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001138 pr_err("Emulated VPD Unit Serial exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001139 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1140 return -EOVERFLOW;
1141 }
1142 /*
1143 * Check to see if any active $FABRIC_MOD exports exist. If they
1144 * do exist, fail here as changing this information on the fly
1145 * (underneath the initiator side OS dependent multipath code)
1146 * could cause negative effects.
1147 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001148 if (dev->export_count) {
1149 pr_err("Unable to set VPD Unit Serial while"
1150 " active %d $FABRIC_MOD exports exist\n",
1151 dev->export_count);
1152 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001153 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001154
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001155 /*
1156 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1157 *
1158 * Also, strip any newline added from the userspace
1159 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1160 */
1161 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1162 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001163 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001164 "%s", strstrip(buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001165 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001166
Andy Grover6708bb22011-06-08 10:36:43 -07001167 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001168 " %s\n", dev->t10_wwn.unit_serial);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001169
1170 return count;
1171}
1172
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001173/*
1174 * VPD page 0x83 Protocol Identifier
1175 */
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001176static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1177 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001178{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001179 struct t10_wwn *t10_wwn = to_t10_wwn(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001180 struct t10_vpd *vpd;
1181 unsigned char buf[VPD_TMP_BUF_SIZE];
1182 ssize_t len = 0;
1183
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001184 memset(buf, 0, VPD_TMP_BUF_SIZE);
1185
1186 spin_lock(&t10_wwn->t10_vpd_lock);
1187 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
Andy Grover6708bb22011-06-08 10:36:43 -07001188 if (!vpd->protocol_identifier_set)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001189 continue;
1190
1191 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1192
Andy Grover6708bb22011-06-08 10:36:43 -07001193 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001194 break;
1195
1196 len += sprintf(page+len, "%s", buf);
1197 }
1198 spin_unlock(&t10_wwn->t10_vpd_lock);
1199
1200 return len;
1201}
1202
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001203/*
1204 * Generic wrapper for dumping VPD identifiers by association.
1205 */
1206#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001207static ssize_t target_wwn_##_name##_show(struct config_item *item, \
1208 char *page) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001209{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001210 struct t10_wwn *t10_wwn = to_t10_wwn(item); \
1211 struct t10_vpd *vpd; \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001212 unsigned char buf[VPD_TMP_BUF_SIZE]; \
1213 ssize_t len = 0; \
1214 \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001215 spin_lock(&t10_wwn->t10_vpd_lock); \
1216 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1217 if (vpd->association != _assoc) \
1218 continue; \
1219 \
1220 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1221 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001222 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001223 break; \
1224 len += sprintf(page+len, "%s", buf); \
1225 \
1226 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1227 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001228 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001229 break; \
1230 len += sprintf(page+len, "%s", buf); \
1231 \
1232 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1233 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
Andy Grover6708bb22011-06-08 10:36:43 -07001234 if (len + strlen(buf) >= PAGE_SIZE) \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001235 break; \
1236 len += sprintf(page+len, "%s", buf); \
1237 } \
1238 spin_unlock(&t10_wwn->t10_vpd_lock); \
1239 \
1240 return len; \
1241}
1242
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001243/* VPD page 0x83 Association: Logical Unit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001244DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001245/* VPD page 0x83 Association: Target Port */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001246DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001247/* VPD page 0x83 Association: SCSI Target Device */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001248DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1249
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001250CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
1251CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
1252CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
1253CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
1254CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001255
1256static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001257 &target_wwn_attr_vpd_unit_serial,
1258 &target_wwn_attr_vpd_protocol_identifier,
1259 &target_wwn_attr_vpd_assoc_logical_unit,
1260 &target_wwn_attr_vpd_assoc_target_port,
1261 &target_wwn_attr_vpd_assoc_scsi_target_device,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001262 NULL,
1263};
1264
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001265TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001266
Nicholas Bellingerf8d389c2014-11-27 15:01:12 -08001267/* End functions for struct config_item_type tb_dev_wwn_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001268
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001269/* Start functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001270
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001271static struct se_device *pr_to_dev(struct config_item *item)
1272{
1273 return container_of(to_config_group(item), struct se_device,
1274 dev_pr_group);
1275}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001276
Christoph Hellwigd977f432012-10-10 17:37:15 -04001277static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1278 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001279{
1280 struct se_node_acl *se_nacl;
1281 struct t10_pr_registration *pr_reg;
1282 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001283
1284 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1285
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001286 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001287 if (!pr_reg)
1288 return sprintf(page, "No SPC-3 Reservation holder\n");
1289
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001290 se_nacl = pr_reg->pr_reg_nacl;
Andy Groverd2843c12013-05-16 10:40:55 -07001291 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001292
Christoph Hellwigd977f432012-10-10 17:37:15 -04001293 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001294 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001295 se_nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001296}
1297
Christoph Hellwigd977f432012-10-10 17:37:15 -04001298static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1299 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001300{
1301 struct se_node_acl *se_nacl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001302 ssize_t len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001303
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001304 se_nacl = dev->dev_reserved_node_acl;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001305 if (se_nacl) {
1306 len = sprintf(page,
1307 "SPC-2 Reservation: %s Initiator: %s\n",
1308 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1309 se_nacl->initiatorname);
1310 } else {
1311 len = sprintf(page, "No SPC-2 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001313 return len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001314}
1315
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001316static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001317{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001318 struct se_device *dev = pr_to_dev(item);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001319 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001320
Andy Grovera3541702015-05-19 14:44:41 -07001321 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001322 return sprintf(page, "Passthrough\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001323
Christoph Hellwigd977f432012-10-10 17:37:15 -04001324 spin_lock(&dev->dev_reservation_lock);
1325 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1326 ret = target_core_dev_pr_show_spc2_res(dev, page);
1327 else
1328 ret = target_core_dev_pr_show_spc3_res(dev, page);
1329 spin_unlock(&dev->dev_reservation_lock);
1330 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001331}
1332
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001333static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
1334 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001335{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001336 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001337 ssize_t len = 0;
1338
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd977f432012-10-10 17:37:15 -04001340 if (!dev->dev_pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001341 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001342 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001343 len = sprintf(page, "SPC-3 Reservation: All Target"
1344 " Ports registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001345 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001346 len = sprintf(page, "SPC-3 Reservation: Single"
1347 " Target Port registration\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001348 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001349
Christoph Hellwigd977f432012-10-10 17:37:15 -04001350 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001351 return len;
1352}
1353
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001354static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
1355 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001356{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001357 return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001358}
1359
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001360
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001361static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
1362 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001363{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001364 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001365 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001366 struct se_portal_group *se_tpg;
1367 struct t10_pr_registration *pr_reg;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001368 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001369 ssize_t len = 0;
1370
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001371 spin_lock(&dev->dev_reservation_lock);
1372 pr_reg = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001373 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001374 len = sprintf(page, "No SPC-3 Reservation holder\n");
Christoph Hellwigd977f432012-10-10 17:37:15 -04001375 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001376 }
Christoph Hellwigd977f432012-10-10 17:37:15 -04001377
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001378 se_nacl = pr_reg->pr_reg_nacl;
1379 se_tpg = se_nacl->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001380 tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001381
1382 len += sprintf(page+len, "SPC-3 Reservation: %s"
1383 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1384 tfo->tpg_get_wwn(se_tpg));
1385 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
Masanari Iida35d1efe2012-08-16 22:43:13 +09001386 " Identifier Tag: %hu %s Portal Group Tag: %hu"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001387 " %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001388 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001389 tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001390
Christoph Hellwigd977f432012-10-10 17:37:15 -04001391out_unlock:
1392 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001393 return len;
1394}
1395
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001396
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001397static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
1398 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001399{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001400 struct se_device *dev = pr_to_dev(item);
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001401 const struct target_core_fabric_ops *tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001402 struct t10_pr_registration *pr_reg;
1403 unsigned char buf[384];
1404 char i_buf[PR_REG_ISID_ID_LEN];
1405 ssize_t len = 0;
Andy Groverd2843c12013-05-16 10:40:55 -07001406 int reg_count = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001407
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001408 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1409
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001410 spin_lock(&dev->t10_pr.registration_lock);
1411 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001412 pr_reg_list) {
1413
1414 memset(buf, 0, 384);
1415 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1416 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Andy Groverd2843c12013-05-16 10:40:55 -07001417 core_pr_dump_initiator_port(pr_reg, i_buf,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001418 PR_REG_ISID_ID_LEN);
1419 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1420 tfo->get_fabric_name(),
Andy Groverd2843c12013-05-16 10:40:55 -07001421 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001422 pr_reg->pr_res_generation);
1423
Andy Grover6708bb22011-06-08 10:36:43 -07001424 if (len + strlen(buf) >= PAGE_SIZE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001425 break;
1426
1427 len += sprintf(page+len, "%s", buf);
1428 reg_count++;
1429 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001430 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001431
Andy Grover6708bb22011-06-08 10:36:43 -07001432 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001433 len += sprintf(page+len, "None\n");
1434
1435 return len;
1436}
1437
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001438static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001439{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001440 struct se_device *dev = pr_to_dev(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001441 struct t10_pr_registration *pr_reg;
1442 ssize_t len = 0;
1443
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001444 spin_lock(&dev->dev_reservation_lock);
1445 pr_reg = dev->dev_pr_res_holder;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001446 if (pr_reg) {
1447 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1448 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1449 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001450 len = sprintf(page, "No SPC-3 Reservation holder\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001451 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001452
Christoph Hellwigd977f432012-10-10 17:37:15 -04001453 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001454 return len;
1455}
1456
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001457static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001458{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001459 struct se_device *dev = pr_to_dev(item);
1460
Andy Grovera3541702015-05-19 14:44:41 -07001461 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04001462 return sprintf(page, "SPC_PASSTHROUGH\n");
1463 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1464 return sprintf(page, "SPC2_RESERVATIONS\n");
1465 else
1466 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001467}
1468
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001469static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
1470 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001471{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001472 struct se_device *dev = pr_to_dev(item);
1473
Andy Grovera3541702015-05-19 14:44:41 -07001474 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001475 return 0;
1476
1477 return sprintf(page, "APTPL Bit Status: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001478 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001479}
1480
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001481static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
1482 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001483{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001484 struct se_device *dev = pr_to_dev(item);
1485
Andy Grovera3541702015-05-19 14:44:41 -07001486 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001487 return 0;
1488
1489 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1490}
1491
1492enum {
1493 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1494 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1495 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1496 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1497};
1498
1499static match_table_t tokens = {
1500 {Opt_initiator_fabric, "initiator_fabric=%s"},
1501 {Opt_initiator_node, "initiator_node=%s"},
1502 {Opt_initiator_sid, "initiator_sid=%s"},
1503 {Opt_sa_res_key, "sa_res_key=%s"},
1504 {Opt_res_holder, "res_holder=%d"},
1505 {Opt_res_type, "res_type=%d"},
1506 {Opt_res_scope, "res_scope=%d"},
1507 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
Hannes Reineckef2d30682015-06-10 08:41:22 +02001508 {Opt_mapped_lun, "mapped_lun=%lld"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001509 {Opt_target_fabric, "target_fabric=%s"},
1510 {Opt_target_node, "target_node=%s"},
1511 {Opt_tpgt, "tpgt=%d"},
1512 {Opt_port_rtpi, "port_rtpi=%d"},
Hannes Reineckef2d30682015-06-10 08:41:22 +02001513 {Opt_target_lun, "target_lun=%lld"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001514 {Opt_err, NULL}
1515};
1516
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001517static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
1518 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001519{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001520 struct se_device *dev = pr_to_dev(item);
Jesper Juhl6d180252011-03-14 04:05:56 -07001521 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1522 unsigned char *t_fabric = NULL, *t_port = NULL;
Joern Engel8d213552014-09-02 17:49:56 -04001523 char *orig, *ptr, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001524 substring_t args[MAX_OPT_ARGS];
1525 unsigned long long tmp_ll;
1526 u64 sa_res_key = 0;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001527 u64 mapped_lun = 0, target_lun = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001528 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
Bart Van Assche45fb94c2015-04-14 13:00:58 +02001529 u16 tpgt = 0;
1530 u8 type = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001531
Andy Grovera3541702015-05-19 14:44:41 -07001532 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Andy Grover9105bfc2015-07-09 09:56:48 -07001533 return count;
Christoph Hellwigd977f432012-10-10 17:37:15 -04001534 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Andy Grover9105bfc2015-07-09 09:56:48 -07001535 return count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001536
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001537 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001538 pr_debug("Unable to process APTPL metadata while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001539 " active fabric exports exist\n");
1540 return -EINVAL;
1541 }
1542
1543 opts = kstrdup(page, GFP_KERNEL);
1544 if (!opts)
1545 return -ENOMEM;
1546
1547 orig = opts;
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +01001548 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001549 if (!*ptr)
1550 continue;
1551
1552 token = match_token(ptr, tokens, args);
1553 switch (token) {
1554 case Opt_initiator_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001555 i_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001556 if (!i_fabric) {
1557 ret = -ENOMEM;
1558 goto out;
1559 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001560 break;
1561 case Opt_initiator_node:
Joern Engel8d213552014-09-02 17:49:56 -04001562 i_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001563 if (!i_port) {
1564 ret = -ENOMEM;
1565 goto out;
1566 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001567 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001568 pr_err("APTPL metadata initiator_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001569 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1570 PR_APTPL_MAX_IPORT_LEN);
1571 ret = -EINVAL;
1572 break;
1573 }
1574 break;
1575 case Opt_initiator_sid:
Joern Engel8d213552014-09-02 17:49:56 -04001576 isid = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001577 if (!isid) {
1578 ret = -ENOMEM;
1579 goto out;
1580 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001581 if (strlen(isid) >= PR_REG_ISID_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001582 pr_err("APTPL metadata initiator_isid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001583 "= exceeds PR_REG_ISID_LEN: %d\n",
1584 PR_REG_ISID_LEN);
1585 ret = -EINVAL;
1586 break;
1587 }
1588 break;
1589 case Opt_sa_res_key:
Joern Engel8d213552014-09-02 17:49:56 -04001590 ret = kstrtoull(args->from, 0, &tmp_ll);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001591 if (ret < 0) {
Joern Engel8d213552014-09-02 17:49:56 -04001592 pr_err("kstrtoull() failed for sa_res_key=\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001593 goto out;
1594 }
1595 sa_res_key = (u64)tmp_ll;
1596 break;
1597 /*
1598 * PR APTPL Metadata for Reservation
1599 */
1600 case Opt_res_holder:
David Disseldorpc2091022015-07-12 18:49:18 +02001601 ret = match_int(args, &arg);
1602 if (ret)
1603 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001604 res_holder = arg;
1605 break;
1606 case Opt_res_type:
David Disseldorpc2091022015-07-12 18:49:18 +02001607 ret = match_int(args, &arg);
1608 if (ret)
1609 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001610 type = (u8)arg;
1611 break;
1612 case Opt_res_scope:
David Disseldorpc2091022015-07-12 18:49:18 +02001613 ret = match_int(args, &arg);
1614 if (ret)
1615 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001616 break;
1617 case Opt_res_all_tg_pt:
David Disseldorpc2091022015-07-12 18:49:18 +02001618 ret = match_int(args, &arg);
1619 if (ret)
1620 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001621 all_tg_pt = (int)arg;
1622 break;
1623 case Opt_mapped_lun:
David Disseldorpc2091022015-07-12 18:49:18 +02001624 ret = match_int(args, &arg);
1625 if (ret)
1626 goto out;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001627 mapped_lun = (u64)arg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001628 break;
1629 /*
1630 * PR APTPL Metadata for Target Port
1631 */
1632 case Opt_target_fabric:
Joern Engel8d213552014-09-02 17:49:56 -04001633 t_fabric = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001634 if (!t_fabric) {
1635 ret = -ENOMEM;
1636 goto out;
1637 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001638 break;
1639 case Opt_target_node:
Joern Engel8d213552014-09-02 17:49:56 -04001640 t_port = match_strdup(args);
Jesper Juhl6d180252011-03-14 04:05:56 -07001641 if (!t_port) {
1642 ret = -ENOMEM;
1643 goto out;
1644 }
Dan Carpenter60d645a2011-06-15 10:03:05 -07001645 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001646 pr_err("APTPL metadata target_node="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001647 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1648 PR_APTPL_MAX_TPORT_LEN);
1649 ret = -EINVAL;
1650 break;
1651 }
1652 break;
1653 case Opt_tpgt:
David Disseldorpc2091022015-07-12 18:49:18 +02001654 ret = match_int(args, &arg);
1655 if (ret)
1656 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001657 tpgt = (u16)arg;
1658 break;
1659 case Opt_port_rtpi:
David Disseldorpc2091022015-07-12 18:49:18 +02001660 ret = match_int(args, &arg);
1661 if (ret)
1662 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001663 break;
1664 case Opt_target_lun:
David Disseldorpc2091022015-07-12 18:49:18 +02001665 ret = match_int(args, &arg);
1666 if (ret)
1667 goto out;
Hannes Reineckef2d30682015-06-10 08:41:22 +02001668 target_lun = (u64)arg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001669 break;
1670 default:
1671 break;
1672 }
1673 }
1674
Andy Grover6708bb22011-06-08 10:36:43 -07001675 if (!i_port || !t_port || !sa_res_key) {
1676 pr_err("Illegal parameters for APTPL registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001677 ret = -EINVAL;
1678 goto out;
1679 }
1680
1681 if (res_holder && !(type)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001682 pr_err("Illegal PR type: 0x%02x for reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001683 " holder\n", type);
1684 ret = -EINVAL;
1685 goto out;
1686 }
1687
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001688 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001689 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1690 res_holder, all_tg_pt, type);
1691out:
Jesper Juhl6d180252011-03-14 04:05:56 -07001692 kfree(i_fabric);
1693 kfree(i_port);
1694 kfree(isid);
1695 kfree(t_fabric);
1696 kfree(t_port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001697 kfree(orig);
1698 return (ret == 0) ? count : ret;
1699}
1700
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001701
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001702CONFIGFS_ATTR_RO(target_pr_, res_holder);
1703CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
1704CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
1705CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
1706CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
1707CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
1708CONFIGFS_ATTR_RO(target_pr_, res_type);
1709CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
1710CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001711
1712static struct configfs_attribute *target_core_dev_pr_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001713 &target_pr_attr_res_holder,
1714 &target_pr_attr_res_pr_all_tgt_pts,
1715 &target_pr_attr_res_pr_generation,
1716 &target_pr_attr_res_pr_holder_tg_port,
1717 &target_pr_attr_res_pr_registered_i_pts,
1718 &target_pr_attr_res_pr_type,
1719 &target_pr_attr_res_type,
1720 &target_pr_attr_res_aptpl_active,
1721 &target_pr_attr_res_aptpl_metadata,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001722 NULL,
1723};
1724
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001725TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001726
Nicholas Bellinger91e2e392014-11-27 14:57:01 -08001727/* End functions for struct config_item_type tb_dev_pr_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001728
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08001729/* Start functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001730
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001731static inline struct se_device *to_device(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001732{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001733 return container_of(to_config_group(item), struct se_device, dev_group);
1734}
1735
1736static ssize_t target_dev_info_show(struct config_item *item, char *page)
1737{
1738 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001739 int bl = 0;
1740 ssize_t read_bytes = 0;
1741
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001742 transport_dump_dev_state(dev, page, &bl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001743 read_bytes += bl;
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001744 read_bytes += dev->transport->show_configfs_dev_params(dev,
1745 page+read_bytes);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001746 return read_bytes;
1747}
1748
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001749static ssize_t target_dev_control_store(struct config_item *item,
1750 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001751{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001752 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001753
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001754 return dev->transport->set_configfs_dev_params(dev, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001755}
1756
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001757static ssize_t target_dev_alias_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001758{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001759 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001760
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001761 if (!(dev->dev_flags & DF_USING_ALIAS))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001762 return 0;
1763
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001764 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001765}
1766
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001767static ssize_t target_dev_alias_store(struct config_item *item,
1768 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001769{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001770 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001771 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001772 ssize_t read_bytes;
1773
1774 if (count > (SE_DEV_ALIAS_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001775 pr_err("alias count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001776 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1777 SE_DEV_ALIAS_LEN-1);
1778 return -EINVAL;
1779 }
1780
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001781 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001782 if (!read_bytes)
1783 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001784 if (dev->dev_alias[read_bytes - 1] == '\n')
1785 dev->dev_alias[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001786
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001787 dev->dev_flags |= DF_USING_ALIAS;
Dan Carpenter30116842012-01-27 15:50:55 +03001788
Andy Grover6708bb22011-06-08 10:36:43 -07001789 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001790 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001791 config_item_name(&dev->dev_group.cg_item),
1792 dev->dev_alias);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001793
1794 return read_bytes;
1795}
1796
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001797static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001798{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001799 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001800
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001801 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001802 return 0;
1803
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001804 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001805}
1806
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001807static ssize_t target_dev_udev_path_store(struct config_item *item,
1808 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001809{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001810 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001811 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001812 ssize_t read_bytes;
1813
1814 if (count > (SE_UDEV_PATH_LEN-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001815 pr_err("udev_path count: %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001816 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1817 SE_UDEV_PATH_LEN-1);
1818 return -EINVAL;
1819 }
1820
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001821 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001822 "%s", page);
Dan Carpenter30116842012-01-27 15:50:55 +03001823 if (!read_bytes)
1824 return -EINVAL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001825 if (dev->udev_path[read_bytes - 1] == '\n')
1826 dev->udev_path[read_bytes - 1] = '\0';
Sebastian Andrzej Siewior0877eafd2011-11-28 13:57:25 +01001827
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001828 dev->dev_flags |= DF_USING_UDEV_PATH;
Dan Carpenter30116842012-01-27 15:50:55 +03001829
Andy Grover6708bb22011-06-08 10:36:43 -07001830 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001831 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001832 config_item_name(&dev->dev_group.cg_item),
1833 dev->udev_path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001834
1835 return read_bytes;
1836}
1837
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001838static ssize_t target_dev_enable_show(struct config_item *item, char *page)
Andy Grover64146db2013-04-30 11:59:15 -07001839{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001840 struct se_device *dev = to_device(item);
Andy Grover64146db2013-04-30 11:59:15 -07001841
1842 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1843}
1844
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001845static ssize_t target_dev_enable_store(struct config_item *item,
1846 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001847{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001848 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001849 char *ptr;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001850 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001851
1852 ptr = strstr(page, "1");
Andy Grover6708bb22011-06-08 10:36:43 -07001853 if (!ptr) {
1854 pr_err("For dev_enable ops, only valid value"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001855 " is \"1\"\n");
1856 return -EINVAL;
1857 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001858
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001859 ret = target_configure_device(dev);
1860 if (ret)
1861 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001862 return count;
1863}
1864
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001865static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001866{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001867 struct se_device *dev = to_device(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001868 struct config_item *lu_ci;
1869 struct t10_alua_lu_gp *lu_gp;
1870 struct t10_alua_lu_gp_member *lu_gp_mem;
1871 ssize_t len = 0;
1872
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001873 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001874 if (!lu_gp_mem)
1875 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001876
1877 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1878 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001879 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001880 lu_ci = &lu_gp->lu_gp_group.cg_item;
1881 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1882 config_item_name(lu_ci), lu_gp->lu_gp_id);
1883 }
1884 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1885
1886 return len;
1887}
1888
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001889static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
1890 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001891{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001892 struct se_device *dev = to_device(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001893 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001894 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1895 struct t10_alua_lu_gp_member *lu_gp_mem;
1896 unsigned char buf[LU_GROUP_NAME_BUF];
1897 int move = 0;
1898
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001899 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1900 if (!lu_gp_mem)
Andy Grover9105bfc2015-07-09 09:56:48 -07001901 return count;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001902
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001903 if (count > LU_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001904 pr_err("ALUA LU Group Alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001905 return -EINVAL;
1906 }
1907 memset(buf, 0, LU_GROUP_NAME_BUF);
1908 memcpy(buf, page, count);
1909 /*
1910 * Any ALUA logical unit alias besides "NULL" means we will be
1911 * making a new group association.
1912 */
1913 if (strcmp(strstrip(buf), "NULL")) {
1914 /*
1915 * core_alua_get_lu_gp_by_name() will increment reference to
1916 * struct t10_alua_lu_gp. This reference is released with
1917 * core_alua_get_lu_gp_by_name below().
1918 */
1919 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001920 if (!lu_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001921 return -ENODEV;
1922 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001923
1924 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1925 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001926 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001927 /*
1928 * Clearing an existing lu_gp association, and replacing
1929 * with NULL
1930 */
Andy Grover6708bb22011-06-08 10:36:43 -07001931 if (!lu_gp_new) {
1932 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001933 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1934 " %hu\n",
1935 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001936 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001937 config_item_name(&lu_gp->lu_gp_group.cg_item),
1938 lu_gp->lu_gp_id);
1939
1940 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1941 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1942
1943 return count;
1944 }
1945 /*
1946 * Removing existing association of lu_gp_mem with lu_gp
1947 */
1948 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1949 move = 1;
1950 }
1951 /*
1952 * Associate lu_gp_mem with lu_gp_new.
1953 */
1954 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1955 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1956
Andy Grover6708bb22011-06-08 10:36:43 -07001957 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001958 " core/alua/lu_gps/%s, ID: %hu\n",
1959 (move) ? "Moving" : "Adding",
1960 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001961 config_item_name(&dev->dev_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001962 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1963 lu_gp_new->lu_gp_id);
1964
1965 core_alua_put_lu_gp_from_name(lu_gp_new);
1966 return count;
1967}
1968
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001969static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001970{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001971 struct se_device *dev = to_device(item);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001972 struct t10_alua_lba_map *map;
1973 struct t10_alua_lba_map_member *mem;
1974 char *b = page;
1975 int bl = 0;
1976 char state;
1977
1978 spin_lock(&dev->t10_alua.lba_map_lock);
1979 if (!list_empty(&dev->t10_alua.lba_map_list))
1980 bl += sprintf(b + bl, "%u %u\n",
1981 dev->t10_alua.lba_map_segment_size,
1982 dev->t10_alua.lba_map_segment_multiplier);
1983 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1984 bl += sprintf(b + bl, "%llu %llu",
1985 map->lba_map_first_lba, map->lba_map_last_lba);
1986 list_for_each_entry(mem, &map->lba_map_mem_list,
1987 lba_map_mem_list) {
1988 switch (mem->lba_map_mem_alua_state) {
1989 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1990 state = 'O';
1991 break;
1992 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1993 state = 'A';
1994 break;
1995 case ALUA_ACCESS_STATE_STANDBY:
1996 state = 'S';
1997 break;
1998 case ALUA_ACCESS_STATE_UNAVAILABLE:
1999 state = 'U';
2000 break;
2001 default:
2002 state = '.';
2003 break;
2004 }
2005 bl += sprintf(b + bl, " %d:%c",
2006 mem->lba_map_mem_alua_pg_id, state);
2007 }
2008 bl += sprintf(b + bl, "\n");
2009 }
2010 spin_unlock(&dev->t10_alua.lba_map_lock);
2011 return bl;
2012}
2013
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002014static ssize_t target_dev_lba_map_store(struct config_item *item,
2015 const char *page, size_t count)
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002016{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002017 struct se_device *dev = to_device(item);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002018 struct t10_alua_lba_map *lba_map = NULL;
2019 struct list_head lba_list;
2020 char *map_entries, *ptr;
2021 char state;
2022 int pg_num = -1, pg;
2023 int ret = 0, num = 0, pg_id, alua_state;
2024 unsigned long start_lba = -1, end_lba = -1;
2025 unsigned long segment_size = -1, segment_mult = -1;
2026
2027 map_entries = kstrdup(page, GFP_KERNEL);
2028 if (!map_entries)
2029 return -ENOMEM;
2030
2031 INIT_LIST_HEAD(&lba_list);
2032 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2033 if (!*ptr)
2034 continue;
2035
2036 if (num == 0) {
2037 if (sscanf(ptr, "%lu %lu\n",
2038 &segment_size, &segment_mult) != 2) {
2039 pr_err("Invalid line %d\n", num);
2040 ret = -EINVAL;
2041 break;
2042 }
2043 num++;
2044 continue;
2045 }
2046 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2047 pr_err("Invalid line %d\n", num);
2048 ret = -EINVAL;
2049 break;
2050 }
2051 ptr = strchr(ptr, ' ');
2052 if (!ptr) {
2053 pr_err("Invalid line %d, missing end lba\n", num);
2054 ret = -EINVAL;
2055 break;
2056 }
2057 ptr++;
2058 ptr = strchr(ptr, ' ');
2059 if (!ptr) {
2060 pr_err("Invalid line %d, missing state definitions\n",
2061 num);
2062 ret = -EINVAL;
2063 break;
2064 }
2065 ptr++;
2066 lba_map = core_alua_allocate_lba_map(&lba_list,
2067 start_lba, end_lba);
2068 if (IS_ERR(lba_map)) {
2069 ret = PTR_ERR(lba_map);
2070 break;
2071 }
2072 pg = 0;
2073 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2074 switch (state) {
2075 case 'O':
2076 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2077 break;
2078 case 'A':
2079 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2080 break;
2081 case 'S':
2082 alua_state = ALUA_ACCESS_STATE_STANDBY;
2083 break;
2084 case 'U':
2085 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2086 break;
2087 default:
2088 pr_err("Invalid ALUA state '%c'\n", state);
2089 ret = -EINVAL;
2090 goto out;
2091 }
2092
2093 ret = core_alua_allocate_lba_map_mem(lba_map,
2094 pg_id, alua_state);
2095 if (ret) {
2096 pr_err("Invalid target descriptor %d:%c "
2097 "at line %d\n",
2098 pg_id, state, num);
2099 break;
2100 }
2101 pg++;
2102 ptr = strchr(ptr, ' ');
2103 if (ptr)
2104 ptr++;
2105 else
2106 break;
2107 }
2108 if (pg_num == -1)
2109 pg_num = pg;
2110 else if (pg != pg_num) {
2111 pr_err("Only %d from %d port groups definitions "
2112 "at line %d\n", pg, pg_num, num);
2113 ret = -EINVAL;
2114 break;
2115 }
2116 num++;
2117 }
2118out:
2119 if (ret) {
2120 core_alua_free_lba_map(&lba_list);
2121 count = ret;
2122 } else
2123 core_alua_set_lba_map(dev, &lba_list,
2124 segment_size, segment_mult);
2125 kfree(map_entries);
2126 return count;
2127}
2128
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002129CONFIGFS_ATTR_RO(target_dev_, info);
2130CONFIGFS_ATTR_WO(target_dev_, control);
2131CONFIGFS_ATTR(target_dev_, alias);
2132CONFIGFS_ATTR(target_dev_, udev_path);
2133CONFIGFS_ATTR(target_dev_, enable);
2134CONFIGFS_ATTR(target_dev_, alua_lu_gp);
2135CONFIGFS_ATTR(target_dev_, lba_map);
Hannes Reinecke229d4f12013-12-17 09:18:50 +01002136
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002137static struct configfs_attribute *target_core_dev_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002138 &target_dev_attr_info,
2139 &target_dev_attr_control,
2140 &target_dev_attr_alias,
2141 &target_dev_attr_udev_path,
2142 &target_dev_attr_enable,
2143 &target_dev_attr_alua_lu_gp,
2144 &target_dev_attr_lba_map,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002145 NULL,
2146};
2147
2148static void target_core_dev_release(struct config_item *item)
2149{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002150 struct config_group *dev_cg = to_config_group(item);
2151 struct se_device *dev =
2152 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002153
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002154 kfree(dev_cg->default_groups);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002155 target_free_device(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002156}
2157
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002158static struct configfs_item_operations target_core_dev_item_ops = {
2159 .release = target_core_dev_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002160};
2161
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002162TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002163
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08002164/* End functions for struct config_item_type tb_dev_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002165
2166/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2167
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002168static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002169{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002170 return container_of(to_config_group(item), struct t10_alua_lu_gp,
2171 lu_gp_group);
2172}
2173
2174static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
2175{
2176 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2177
Andy Grover6708bb22011-06-08 10:36:43 -07002178 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002179 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002180 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2181}
2182
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002183static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
2184 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002185{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002186 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002187 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2188 unsigned long lu_gp_id;
2189 int ret;
2190
Jingoo Han57103d72013-07-19 16:22:19 +09002191 ret = kstrtoul(page, 0, &lu_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002192 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002193 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002194 " lu_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002195 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002196 }
2197 if (lu_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002198 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002199 " 0x0000ffff\n", lu_gp_id);
2200 return -EINVAL;
2201 }
2202
2203 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2204 if (ret < 0)
2205 return -EINVAL;
2206
Andy Grover6708bb22011-06-08 10:36:43 -07002207 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002208 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2209 config_item_name(&alua_lu_gp_cg->cg_item),
2210 lu_gp->lu_gp_id);
2211
2212 return count;
2213}
2214
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002215static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002216{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002217 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002218 struct se_device *dev;
2219 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002220 struct t10_alua_lu_gp_member *lu_gp_mem;
2221 ssize_t len = 0, cur_len;
2222 unsigned char buf[LU_GROUP_NAME_BUF];
2223
2224 memset(buf, 0, LU_GROUP_NAME_BUF);
2225
2226 spin_lock(&lu_gp->lu_gp_lock);
2227 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2228 dev = lu_gp_mem->lu_gp_mem_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002229 hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002230
2231 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2232 config_item_name(&hba->hba_group.cg_item),
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002233 config_item_name(&dev->dev_group.cg_item));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002234 cur_len++; /* Extra byte for NULL terminator */
2235
2236 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002237 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002238 "_members buffer\n");
2239 break;
2240 }
2241 memcpy(page+len, buf, cur_len);
2242 len += cur_len;
2243 }
2244 spin_unlock(&lu_gp->lu_gp_lock);
2245
2246 return len;
2247}
2248
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002249CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
2250CONFIGFS_ATTR_RO(target_lu_gp_, members);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002251
2252static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002253 &target_lu_gp_attr_lu_gp_id,
2254 &target_lu_gp_attr_members,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002255 NULL,
2256};
2257
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002258static void target_core_alua_lu_gp_release(struct config_item *item)
2259{
2260 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2261 struct t10_alua_lu_gp, lu_gp_group);
2262
2263 core_alua_free_lu_gp(lu_gp);
2264}
2265
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002266static struct configfs_item_operations target_core_alua_lu_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002267 .release = target_core_alua_lu_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002268};
2269
2270static struct config_item_type target_core_alua_lu_gp_cit = {
2271 .ct_item_ops = &target_core_alua_lu_gp_ops,
2272 .ct_attrs = target_core_alua_lu_gp_attrs,
2273 .ct_owner = THIS_MODULE,
2274};
2275
2276/* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2277
2278/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2279
2280static struct config_group *target_core_alua_create_lu_gp(
2281 struct config_group *group,
2282 const char *name)
2283{
2284 struct t10_alua_lu_gp *lu_gp;
2285 struct config_group *alua_lu_gp_cg = NULL;
2286 struct config_item *alua_lu_gp_ci = NULL;
2287
2288 lu_gp = core_alua_allocate_lu_gp(name, 0);
2289 if (IS_ERR(lu_gp))
2290 return NULL;
2291
2292 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2293 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2294
2295 config_group_init_type_name(alua_lu_gp_cg, name,
2296 &target_core_alua_lu_gp_cit);
2297
Andy Grover6708bb22011-06-08 10:36:43 -07002298 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002299 " Group: core/alua/lu_gps/%s\n",
2300 config_item_name(alua_lu_gp_ci));
2301
2302 return alua_lu_gp_cg;
2303
2304}
2305
2306static void target_core_alua_drop_lu_gp(
2307 struct config_group *group,
2308 struct config_item *item)
2309{
2310 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2311 struct t10_alua_lu_gp, lu_gp_group);
2312
Andy Grover6708bb22011-06-08 10:36:43 -07002313 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002314 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2315 config_item_name(item), lu_gp->lu_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002316 /*
2317 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2318 * -> target_core_alua_lu_gp_release()
2319 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002320 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002321}
2322
2323static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2324 .make_group = &target_core_alua_create_lu_gp,
2325 .drop_item = &target_core_alua_drop_lu_gp,
2326};
2327
2328static struct config_item_type target_core_alua_lu_gps_cit = {
2329 .ct_item_ops = NULL,
2330 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2331 .ct_owner = THIS_MODULE,
2332};
2333
2334/* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2335
2336/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2337
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002338static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002339{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002340 return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
2341 tg_pt_gp_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002342}
2343
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002344static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2345 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002346{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002347 return sprintf(page, "%d\n",
2348 atomic_read(&to_tg_pt_gp(item)->tg_pt_gp_alua_access_state));
2349}
2350
2351static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
2352 const char *page, size_t count)
2353{
2354 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002355 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002356 unsigned long tmp;
2357 int new_state, ret;
2358
Andy Grover6708bb22011-06-08 10:36:43 -07002359 if (!tg_pt_gp->tg_pt_gp_valid_id) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002360 pr_err("Unable to do implicit ALUA on non valid"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002361 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2362 return -EINVAL;
2363 }
Nicholas Bellingerf1453772014-06-06 00:52:57 -07002364 if (!(dev->dev_flags & DF_CONFIGURED)) {
2365 pr_err("Unable to set alua_access_state while device is"
2366 " not configured\n");
2367 return -ENODEV;
2368 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002369
Jingoo Han57103d72013-07-19 16:22:19 +09002370 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002371 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002372 pr_err("Unable to extract new ALUA access state from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002373 " %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002374 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002375 }
2376 new_state = (int)tmp;
2377
Hannes Reinecke125d0112013-11-19 09:07:46 +01002378 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2379 pr_err("Unable to process implicit configfs ALUA"
2380 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002381 return -EINVAL;
2382 }
Hannes Reineckec66094b2013-12-17 09:18:49 +01002383 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2384 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2385 /* LBA DEPENDENT is only allowed with implicit ALUA */
2386 pr_err("Unable to process implicit configfs ALUA transition"
2387 " while explicit ALUA management is enabled\n");
2388 return -EINVAL;
2389 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002390
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002391 ret = core_alua_do_port_transition(tg_pt_gp, dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002392 NULL, NULL, new_state, 0);
2393 return (!ret) ? count : -EINVAL;
2394}
2395
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002396static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2397 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002398{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002399 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002400 return sprintf(page, "%s\n",
2401 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2402}
2403
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002404static ssize_t target_tg_pt_gp_alua_access_status_store(
2405 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002406{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002407 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002408 unsigned long tmp;
2409 int new_status, ret;
2410
Andy Grover6708bb22011-06-08 10:36:43 -07002411 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2412 pr_err("Unable to do set ALUA access status on non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002413 " valid tg_pt_gp ID: %hu\n",
2414 tg_pt_gp->tg_pt_gp_valid_id);
2415 return -EINVAL;
2416 }
2417
Jingoo Han57103d72013-07-19 16:22:19 +09002418 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002419 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002420 pr_err("Unable to extract new ALUA access status"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002421 " from %s\n", page);
Jingoo Han57103d72013-07-19 16:22:19 +09002422 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002423 }
2424 new_status = (int)tmp;
2425
2426 if ((new_status != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002427 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2428 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002429 pr_err("Illegal ALUA access status: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002430 new_status);
2431 return -EINVAL;
2432 }
2433
2434 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2435 return count;
2436}
2437
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002438static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2439 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002440{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002441 return core_alua_show_access_type(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002442}
2443
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002444static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
2445 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002446{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002447 return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002448}
2449
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002450#define ALUA_SUPPORTED_STATE_ATTR(_name, _bit) \
2451static ssize_t target_tg_pt_gp_alua_support_##_name##_show( \
2452 struct config_item *item, char *p) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002453{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002454 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
2455 return sprintf(p, "%d\n", \
2456 !!(t->tg_pt_gp_alua_supported_states & _bit)); \
2457} \
2458 \
2459static ssize_t target_tg_pt_gp_alua_support_##_name##_store( \
2460 struct config_item *item, const char *p, size_t c) \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002461{ \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002462 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002463 unsigned long tmp; \
2464 int ret; \
2465 \
2466 if (!t->tg_pt_gp_valid_id) { \
2467 pr_err("Unable to do set ##_name ALUA state on non" \
2468 " valid tg_pt_gp ID: %hu\n", \
2469 t->tg_pt_gp_valid_id); \
2470 return -EINVAL; \
2471 } \
2472 \
2473 ret = kstrtoul(p, 0, &tmp); \
2474 if (ret < 0) { \
2475 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2476 return -EINVAL; \
2477 } \
2478 if (tmp > 1) { \
2479 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2480 return -EINVAL; \
2481 } \
Sebastian Herbszt1f0b0302014-09-01 00:17:53 +02002482 if (tmp) \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002483 t->tg_pt_gp_alua_supported_states |= _bit; \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002484 else \
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002485 t->tg_pt_gp_alua_supported_states &= ~_bit; \
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002486 \
2487 return c; \
Hannes Reinecke6be526c2013-11-19 09:07:50 +01002488}
2489
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002490ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
2491ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
2492ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
2493ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
2494ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
2495ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
2496ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
Hannes Reineckeb0a382c2013-11-19 09:07:51 +01002497
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002498static ssize_t target_tg_pt_gp_alua_write_metadata_show(
2499 struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002500{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002501 return sprintf(page, "%d\n",
2502 to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002503}
2504
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002505static ssize_t target_tg_pt_gp_alua_write_metadata_store(
2506 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002507{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002508 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002509 unsigned long tmp;
2510 int ret;
2511
Jingoo Han57103d72013-07-19 16:22:19 +09002512 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002513 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002514 pr_err("Unable to extract alua_write_metadata\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002515 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002516 }
2517
2518 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002519 pr_err("Illegal value for alua_write_metadata:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002520 " %lu\n", tmp);
2521 return -EINVAL;
2522 }
2523 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2524
2525 return count;
2526}
2527
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002528static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2529 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002530{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002531 return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002532}
2533
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002534static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
2535 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002536{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002537 return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
2538 count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002539}
2540
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002541static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2542 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002543{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002544 return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002545}
2546
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002547static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
2548 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002549{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002550 return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
2551 count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002552}
2553
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002554static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
2555 struct config_item *item, char *page)
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002556{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002557 return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002558}
2559
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002560static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
2561 struct config_item *item, const char *page, size_t count)
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002562{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002563 return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
2564 count);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002565}
2566
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002567static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
2568 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002569{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002570 return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002571}
2572
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002573static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
2574 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002575{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002576 return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002577}
2578
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002579static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2580 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002581{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002582 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2583
Andy Grover6708bb22011-06-08 10:36:43 -07002584 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002585 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002586 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2587}
2588
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002589static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
2590 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002591{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002592 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002593 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2594 unsigned long tg_pt_gp_id;
2595 int ret;
2596
Jingoo Han57103d72013-07-19 16:22:19 +09002597 ret = kstrtoul(page, 0, &tg_pt_gp_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002598 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09002599 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002600 " tg_pt_gp_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09002601 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002602 }
2603 if (tg_pt_gp_id > 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07002604 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002605 " 0x0000ffff\n", tg_pt_gp_id);
2606 return -EINVAL;
2607 }
2608
2609 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2610 if (ret < 0)
2611 return -EINVAL;
2612
Andy Grover6708bb22011-06-08 10:36:43 -07002613 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002614 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2615 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2616 tg_pt_gp->tg_pt_gp_id);
2617
2618 return count;
2619}
2620
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002621static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2622 char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002623{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002624 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002625 struct se_lun *lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002626 ssize_t len = 0, cur_len;
2627 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2628
2629 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2630
2631 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002632 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2633 lun_tg_pt_gp_link) {
2634 struct se_portal_group *tpg = lun->lun_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002635
2636 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
Andy Grovere3d6f902011-07-19 08:55:10 +00002637 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2638 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2639 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002640 config_item_name(&lun->lun_group.cg_item));
2641 cur_len++; /* Extra byte for NULL terminator */
2642
2643 if ((cur_len + len) > PAGE_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002644 pr_warn("Ran out of lu_gp_show_attr"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002645 "_members buffer\n");
2646 break;
2647 }
2648 memcpy(page+len, buf, cur_len);
2649 len += cur_len;
2650 }
2651 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2652
2653 return len;
2654}
2655
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002656CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
2657CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
2658CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
2659CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
2660CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
2661CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
2662CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
2663CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
2664CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
2665CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
2666CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
2667CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
2668CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
2669CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
2670CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
2671CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
2672CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002673
2674static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002675 &target_tg_pt_gp_attr_alua_access_state,
2676 &target_tg_pt_gp_attr_alua_access_status,
2677 &target_tg_pt_gp_attr_alua_access_type,
2678 &target_tg_pt_gp_attr_alua_support_transitioning,
2679 &target_tg_pt_gp_attr_alua_support_offline,
2680 &target_tg_pt_gp_attr_alua_support_lba_dependent,
2681 &target_tg_pt_gp_attr_alua_support_unavailable,
2682 &target_tg_pt_gp_attr_alua_support_standby,
2683 &target_tg_pt_gp_attr_alua_support_active_nonoptimized,
2684 &target_tg_pt_gp_attr_alua_support_active_optimized,
2685 &target_tg_pt_gp_attr_alua_write_metadata,
2686 &target_tg_pt_gp_attr_nonop_delay_msecs,
2687 &target_tg_pt_gp_attr_trans_delay_msecs,
2688 &target_tg_pt_gp_attr_implicit_trans_secs,
2689 &target_tg_pt_gp_attr_preferred,
2690 &target_tg_pt_gp_attr_tg_pt_gp_id,
2691 &target_tg_pt_gp_attr_members,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002692 NULL,
2693};
2694
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002695static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2696{
2697 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2698 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2699
2700 core_alua_free_tg_pt_gp(tg_pt_gp);
2701}
2702
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002703static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002704 .release = target_core_alua_tg_pt_gp_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002705};
2706
2707static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2708 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2709 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2710 .ct_owner = THIS_MODULE,
2711};
2712
2713/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2714
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002715/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002716
2717static struct config_group *target_core_alua_create_tg_pt_gp(
2718 struct config_group *group,
2719 const char *name)
2720{
2721 struct t10_alua *alua = container_of(group, struct t10_alua,
2722 alua_tg_pt_gps_group);
2723 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002724 struct config_group *alua_tg_pt_gp_cg = NULL;
2725 struct config_item *alua_tg_pt_gp_ci = NULL;
2726
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002727 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002728 if (!tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002729 return NULL;
2730
2731 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2732 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2733
2734 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2735 &target_core_alua_tg_pt_gp_cit);
2736
Andy Grover6708bb22011-06-08 10:36:43 -07002737 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002738 " Group: alua/tg_pt_gps/%s\n",
2739 config_item_name(alua_tg_pt_gp_ci));
2740
2741 return alua_tg_pt_gp_cg;
2742}
2743
2744static void target_core_alua_drop_tg_pt_gp(
2745 struct config_group *group,
2746 struct config_item *item)
2747{
2748 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2749 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2750
Andy Grover6708bb22011-06-08 10:36:43 -07002751 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002752 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2753 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002754 /*
2755 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2756 * -> target_core_alua_tg_pt_gp_release().
2757 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002758 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002759}
2760
2761static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2762 .make_group = &target_core_alua_create_tg_pt_gp,
2763 .drop_item = &target_core_alua_drop_tg_pt_gp,
2764};
2765
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002766TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002767
Nicholas Bellinger72aca572014-11-27 15:06:23 -08002768/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002769
2770/* Start functions for struct config_item_type target_core_alua_cit */
2771
2772/*
2773 * target_core_alua_cit is a ConfigFS group that lives under
2774 * /sys/kernel/config/target/core/alua. There are default groups
2775 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2776 * target_core_alua_cit in target_core_init_configfs() below.
2777 */
2778static struct config_item_type target_core_alua_cit = {
2779 .ct_item_ops = NULL,
2780 .ct_attrs = NULL,
2781 .ct_owner = THIS_MODULE,
2782};
2783
2784/* End functions for struct config_item_type target_core_alua_cit */
2785
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002786/* Start functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002787
2788static struct config_group *target_core_stat_mkdir(
2789 struct config_group *group,
2790 const char *name)
2791{
2792 return ERR_PTR(-ENOSYS);
2793}
2794
2795static void target_core_stat_rmdir(
2796 struct config_group *group,
2797 struct config_item *item)
2798{
2799 return;
2800}
2801
2802static struct configfs_group_operations target_core_stat_group_ops = {
2803 .make_group = &target_core_stat_mkdir,
2804 .drop_item = &target_core_stat_rmdir,
2805};
2806
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002807TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002808
Nicholas Bellingerd23ab572014-11-27 15:09:32 -08002809/* End functions for struct config_item_type tb_dev_stat_cit */
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002810
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002811/* Start functions for struct config_item_type target_core_hba_cit */
2812
2813static struct config_group *target_core_make_subdev(
2814 struct config_group *group,
2815 const char *name)
2816{
2817 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002818 struct config_item *hba_ci = &group->cg_item;
2819 struct se_hba *hba = item_to_hba(hba_ci);
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002820 struct target_backend *tb = hba->backend;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002821 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002822 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002823 struct config_group *dev_stat_grp = NULL;
2824 int errno = -ENOMEM, ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002825
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002826 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2827 if (ret)
2828 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002829
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002830 dev = target_alloc_device(hba, name);
2831 if (!dev)
2832 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002833
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002834 dev_cg = &dev->dev_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002835
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002836 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002837 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002838 if (!dev_cg->default_groups)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002839 goto out_free_device;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002840
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002841 config_group_init_type_name(dev_cg, name, &tb->tb_dev_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002842 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002843 &tb->tb_dev_attrib_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002844 config_group_init_type_name(&dev->dev_pr_group, "pr",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002845 &tb->tb_dev_pr_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002846 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002847 &tb->tb_dev_wwn_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002848 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002849 "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002850 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002851 "statistics", &tb->tb_dev_stat_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002852
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002853 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2854 dev_cg->default_groups[1] = &dev->dev_pr_group;
2855 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2856 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2857 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002858 dev_cg->default_groups[5] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002859 /*
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002860 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002861 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002862 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
Andy Grover6708bb22011-06-08 10:36:43 -07002863 if (!tg_pt_gp)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002864 goto out_free_dev_cg_default_groups;
2865 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002866
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002867 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002868 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002869 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002870 if (!tg_pt_gp_cg->default_groups) {
2871 pr_err("Unable to allocate tg_pt_gp_cg->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002872 "default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002873 goto out_free_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002874 }
2875
2876 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2877 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2878 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2879 tg_pt_gp_cg->default_groups[1] = NULL;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002880 /*
2881 * Add core/$HBA/$DEV/statistics/ default groups
2882 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002883 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01002884 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002885 GFP_KERNEL);
2886 if (!dev_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -07002887 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002888 goto out_free_tg_pt_gp_cg_default_groups;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002889 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002890 target_stat_setup_dev_default_groups(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002891
2892 mutex_unlock(&hba->hba_access_mutex);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002893 return dev_cg;
2894
2895out_free_tg_pt_gp_cg_default_groups:
2896 kfree(tg_pt_gp_cg->default_groups);
2897out_free_tg_pt_gp:
2898 core_alua_free_tg_pt_gp(tg_pt_gp);
2899out_free_dev_cg_default_groups:
2900 kfree(dev_cg->default_groups);
2901out_free_device:
2902 target_free_device(dev);
2903out_unlock:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002904 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002905 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002906}
2907
2908static void target_core_drop_subdev(
2909 struct config_group *group,
2910 struct config_item *item)
2911{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002912 struct config_group *dev_cg = to_config_group(item);
2913 struct se_device *dev =
2914 container_of(dev_cg, struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002915 struct se_hba *hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002916 struct config_item *df_item;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002917 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002918 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002919
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002920 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002921
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002922 mutex_lock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002923
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002924 dev_stat_grp = &dev->dev_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07002925 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2926 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2927 dev_stat_grp->default_groups[i] = NULL;
2928 config_item_put(df_item);
2929 }
2930 kfree(dev_stat_grp->default_groups);
2931
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002932 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002933 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2934 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2935 tg_pt_gp_cg->default_groups[i] = NULL;
2936 config_item_put(df_item);
2937 }
2938 kfree(tg_pt_gp_cg->default_groups);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002939 /*
2940 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2941 * directly from target_core_alua_tg_pt_gp_release().
2942 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002943 dev->t10_alua.default_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002944
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002945 for (i = 0; dev_cg->default_groups[i]; i++) {
2946 df_item = &dev_cg->default_groups[i]->cg_item;
2947 dev_cg->default_groups[i] = NULL;
2948 config_item_put(df_item);
2949 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002950 /*
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002951 * se_dev is released from target_core_dev_item_ops->release()
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002952 */
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08002953 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002954 mutex_unlock(&hba->hba_access_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002955}
2956
2957static struct configfs_group_operations target_core_hba_group_ops = {
2958 .make_group = target_core_make_subdev,
2959 .drop_item = target_core_drop_subdev,
2960};
2961
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002962
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002963static inline struct se_hba *to_hba(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002964{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002965 return container_of(to_config_group(item), struct se_hba, hba_group);
2966}
2967
2968static ssize_t target_hba_info_show(struct config_item *item, char *page)
2969{
2970 struct se_hba *hba = to_hba(item);
2971
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002972 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002973 hba->hba_id, hba->backend->ops->name,
Christoph Hellwigce8dd252015-06-19 15:14:39 +02002974 TARGET_CORE_VERSION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002975}
2976
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002977static ssize_t target_hba_mode_show(struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002978{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002979 struct se_hba *hba = to_hba(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002980 int hba_mode = 0;
2981
2982 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2983 hba_mode = 1;
2984
2985 return sprintf(page, "%d\n", hba_mode);
2986}
2987
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002988static ssize_t target_hba_mode_store(struct config_item *item,
2989 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002990{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02002991 struct se_hba *hba = to_hba(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002992 unsigned long mode_flag;
2993 int ret;
2994
Christoph Hellwig0a06d432015-05-10 18:14:56 +02002995 if (hba->backend->ops->pmode_enable_hba == NULL)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002996 return -EINVAL;
2997
Jingoo Han57103d72013-07-19 16:22:19 +09002998 ret = kstrtoul(page, 0, &mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002999 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003000 pr_err("Unable to extract hba mode flag: %d\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003001 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003002 }
3003
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003004 if (hba->dev_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07003005 pr_err("Unable to set hba_mode with active devices\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003006 return -EINVAL;
3007 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003008
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003009 ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003010 if (ret < 0)
3011 return -EINVAL;
3012 if (ret > 0)
3013 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3014 else if (ret == 0)
3015 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3016
3017 return count;
3018}
3019
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003020CONFIGFS_ATTR_RO(target_, hba_info);
3021CONFIGFS_ATTR(target_, hba_mode);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003022
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003023static void target_core_hba_release(struct config_item *item)
3024{
3025 struct se_hba *hba = container_of(to_config_group(item),
3026 struct se_hba, hba_group);
3027 core_delete_hba(hba);
3028}
3029
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003030static struct configfs_attribute *target_core_hba_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02003031 &target_attr_hba_info,
3032 &target_attr_hba_mode,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003033 NULL,
3034};
3035
3036static struct configfs_item_operations target_core_hba_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003037 .release = target_core_hba_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003038};
3039
3040static struct config_item_type target_core_hba_cit = {
3041 .ct_item_ops = &target_core_hba_item_ops,
3042 .ct_group_ops = &target_core_hba_group_ops,
3043 .ct_attrs = target_core_hba_attrs,
3044 .ct_owner = THIS_MODULE,
3045};
3046
3047static struct config_group *target_core_call_addhbatotarget(
3048 struct config_group *group,
3049 const char *name)
3050{
3051 char *se_plugin_str, *str, *str2;
3052 struct se_hba *hba;
3053 char buf[TARGET_CORE_NAME_MAX_LEN];
3054 unsigned long plugin_dep_id = 0;
3055 int ret;
3056
3057 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
Dan Carpenter60d645a2011-06-15 10:03:05 -07003058 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07003059 pr_err("Passed *name strlen(): %d exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003060 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3061 TARGET_CORE_NAME_MAX_LEN);
3062 return ERR_PTR(-ENAMETOOLONG);
3063 }
3064 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3065
3066 str = strstr(buf, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003067 if (!str) {
3068 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003069 return ERR_PTR(-EINVAL);
3070 }
3071 se_plugin_str = buf;
3072 /*
3073 * Special case for subsystem plugins that have "_" in their names.
3074 * Namely rd_direct and rd_mcp..
3075 */
3076 str2 = strstr(str+1, "_");
Andy Grover6708bb22011-06-08 10:36:43 -07003077 if (str2) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003078 *str2 = '\0'; /* Terminate for *se_plugin_str */
3079 str2++; /* Skip to start of plugin dependent ID */
3080 str = str2;
3081 } else {
3082 *str = '\0'; /* Terminate for *se_plugin_str */
3083 str++; /* Skip to start of plugin dependent ID */
3084 }
3085
Jingoo Han57103d72013-07-19 16:22:19 +09003086 ret = kstrtoul(str, 0, &plugin_dep_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003087 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +09003088 pr_err("kstrtoul() returned %d for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003089 " plugin_dep_id\n", ret);
Jingoo Han57103d72013-07-19 16:22:19 +09003090 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003091 }
3092 /*
3093 * Load up TCM subsystem plugins if they have not already been loaded.
3094 */
Nicholas Bellingerdbc56232011-10-22 01:03:54 -07003095 transport_subsystem_check_init();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003096
3097 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3098 if (IS_ERR(hba))
3099 return ERR_CAST(hba);
3100
3101 config_group_init_type_name(&hba->hba_group, name,
3102 &target_core_hba_cit);
3103
3104 return &hba->hba_group;
3105}
3106
3107static void target_core_call_delhbafromtarget(
3108 struct config_group *group,
3109 struct config_item *item)
3110{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08003111 /*
3112 * core_delete_hba() is called from target_core_hba_item_ops->release()
3113 * -> target_core_hba_release()
3114 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003115 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003116}
3117
3118static struct configfs_group_operations target_core_group_ops = {
3119 .make_group = target_core_call_addhbatotarget,
3120 .drop_item = target_core_call_delhbafromtarget,
3121};
3122
3123static struct config_item_type target_core_cit = {
3124 .ct_item_ops = NULL,
3125 .ct_group_ops = &target_core_group_ops,
3126 .ct_attrs = NULL,
3127 .ct_owner = THIS_MODULE,
3128};
3129
3130/* Stop functions for struct config_item_type target_core_hba_cit */
3131
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003132void target_setup_backend_cits(struct target_backend *tb)
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003133{
Christoph Hellwig0a06d432015-05-10 18:14:56 +02003134 target_core_setup_dev_cit(tb);
3135 target_core_setup_dev_attrib_cit(tb);
3136 target_core_setup_dev_pr_cit(tb);
3137 target_core_setup_dev_wwn_cit(tb);
3138 target_core_setup_dev_alua_tg_pt_gps_cit(tb);
3139 target_core_setup_dev_stat_cit(tb);
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003140}
Nicholas Bellinger73112ed2014-11-27 13:59:20 -08003141
Axel Lin54550fa2011-03-14 04:06:09 -07003142static int __init target_core_init_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003143{
3144 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3145 struct config_group *lu_gp_cg = NULL;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003146 struct configfs_subsystem *subsys = &target_core_fabrics;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003147 struct t10_alua_lu_gp *lu_gp;
3148 int ret;
3149
Andy Grover6708bb22011-06-08 10:36:43 -07003150 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003151 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3152 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3153
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003154 config_group_init(&subsys->su_group);
3155 mutex_init(&subsys->su_mutex);
3156
Andy Grovere3d6f902011-07-19 08:55:10 +00003157 ret = init_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003158 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003159 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003160 /*
3161 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3162 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3163 */
3164 target_cg = &subsys->su_group;
Andy Groverab6dae82013-12-09 14:27:36 -08003165 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003166 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003167 if (!target_cg->default_groups) {
3168 pr_err("Unable to allocate target_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003169 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003170 goto out_global;
3171 }
3172
Andy Grovere3d6f902011-07-19 08:55:10 +00003173 config_group_init_type_name(&target_core_hbagroup,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003174 "core", &target_core_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003175 target_cg->default_groups[0] = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003176 target_cg->default_groups[1] = NULL;
3177 /*
3178 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3179 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003180 hba_cg = &target_core_hbagroup;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003181 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003182 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003183 if (!hba_cg->default_groups) {
3184 pr_err("Unable to allocate hba_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003185 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003186 goto out_global;
3187 }
Andy Grovere3d6f902011-07-19 08:55:10 +00003188 config_group_init_type_name(&alua_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003189 "alua", &target_core_alua_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003190 hba_cg->default_groups[0] = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003191 hba_cg->default_groups[1] = NULL;
3192 /*
3193 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3194 * groups under /sys/kernel/config/target/core/alua/
3195 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003196 alua_cg = &alua_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003197 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003198 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003199 if (!alua_cg->default_groups) {
3200 pr_err("Unable to allocate alua_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003201 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003202 goto out_global;
3203 }
3204
Andy Grovere3d6f902011-07-19 08:55:10 +00003205 config_group_init_type_name(&alua_lu_gps_group,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003206 "lu_gps", &target_core_alua_lu_gps_cit);
Andy Grovere3d6f902011-07-19 08:55:10 +00003207 alua_cg->default_groups[0] = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003208 alua_cg->default_groups[1] = NULL;
3209 /*
3210 * Add core/alua/lu_gps/default_lu_gp
3211 */
3212 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003213 if (IS_ERR(lu_gp)) {
3214 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003215 goto out_global;
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003216 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003217
Andy Grovere3d6f902011-07-19 08:55:10 +00003218 lu_gp_cg = &alua_lu_gps_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +01003219 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003220 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07003221 if (!lu_gp_cg->default_groups) {
3222 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
Peter Senna Tschudin37bb7892012-09-17 20:05:33 +02003223 ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003224 goto out_global;
3225 }
3226
3227 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3228 &target_core_alua_lu_gp_cit);
3229 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3230 lu_gp_cg->default_groups[1] = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00003231 default_lu_gp = lu_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003232 /*
3233 * Register the target_core_mod subsystem with configfs.
3234 */
3235 ret = configfs_register_subsystem(subsys);
3236 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07003237 pr_err("Error %d while registering subsystem %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003238 ret, subsys->su_group.cg_item.ci_namebuf);
3239 goto out_global;
3240 }
Andy Grover6708bb22011-06-08 10:36:43 -07003241 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
Christoph Hellwigce8dd252015-06-19 15:14:39 +02003242 " Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003243 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3244 /*
3245 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3246 */
3247 ret = rd_module_init();
3248 if (ret < 0)
3249 goto out;
3250
Roland Dreier0d0f9df2012-10-31 09:16:44 -07003251 ret = core_dev_setup_virtual_lun0();
3252 if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003253 goto out;
3254
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003255 ret = target_xcopy_setup_pt();
3256 if (ret < 0)
3257 goto out;
3258
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003259 return 0;
3260
3261out:
3262 configfs_unregister_subsystem(subsys);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003263 core_dev_release_virtual_lun0();
3264 rd_module_exit();
3265out_global:
Andy Grovere3d6f902011-07-19 08:55:10 +00003266 if (default_lu_gp) {
3267 core_alua_free_lu_gp(default_lu_gp);
3268 default_lu_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003269 }
3270 if (lu_gp_cg)
3271 kfree(lu_gp_cg->default_groups);
3272 if (alua_cg)
3273 kfree(alua_cg->default_groups);
3274 if (hba_cg)
3275 kfree(hba_cg->default_groups);
3276 kfree(target_cg->default_groups);
Andy Grovere3d6f902011-07-19 08:55:10 +00003277 release_se_kmem_caches();
3278 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003279}
3280
Axel Lin54550fa2011-03-14 04:06:09 -07003281static void __exit target_core_exit_configfs(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003282{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003283 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3284 struct config_item *item;
3285 int i;
3286
Andy Grovere3d6f902011-07-19 08:55:10 +00003287 lu_gp_cg = &alua_lu_gps_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003288 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3289 item = &lu_gp_cg->default_groups[i]->cg_item;
3290 lu_gp_cg->default_groups[i] = NULL;
3291 config_item_put(item);
3292 }
3293 kfree(lu_gp_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003294 lu_gp_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003295
Andy Grovere3d6f902011-07-19 08:55:10 +00003296 alua_cg = &alua_group;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003297 for (i = 0; alua_cg->default_groups[i]; i++) {
3298 item = &alua_cg->default_groups[i]->cg_item;
3299 alua_cg->default_groups[i] = NULL;
3300 config_item_put(item);
3301 }
3302 kfree(alua_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003303 alua_cg->default_groups = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003304
Andy Grovere3d6f902011-07-19 08:55:10 +00003305 hba_cg = &target_core_hbagroup;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003306 for (i = 0; hba_cg->default_groups[i]; i++) {
3307 item = &hba_cg->default_groups[i]->cg_item;
3308 hba_cg->default_groups[i] = NULL;
3309 config_item_put(item);
3310 }
3311 kfree(hba_cg->default_groups);
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003312 hba_cg->default_groups = NULL;
3313 /*
3314 * We expect subsys->su_group.default_groups to be released
3315 * by configfs subsystem provider logic..
3316 */
Christoph Hellwigd588cf82015-05-03 08:50:52 +02003317 configfs_unregister_subsystem(&target_core_fabrics);
3318 kfree(target_core_fabrics.su_group.default_groups);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003319
Andy Grovere3d6f902011-07-19 08:55:10 +00003320 core_alua_free_lu_gp(default_lu_gp);
3321 default_lu_gp = NULL;
Nicholas Bellinger7c2bf6e2011-02-09 15:34:53 -08003322
Andy Grover6708bb22011-06-08 10:36:43 -07003323 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003324 " Infrastructure\n");
3325
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003326 core_dev_release_virtual_lun0();
3327 rd_module_exit();
Nicholas Bellingerf99715a2013-08-22 12:48:53 -07003328 target_xcopy_release_pt();
Andy Grovere3d6f902011-07-19 08:55:10 +00003329 release_se_kmem_caches();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003330}
3331
3332MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3333MODULE_AUTHOR("nab@Linux-iSCSI.org");
3334MODULE_LICENSE("GPL");
3335
3336module_init(target_core_init_configfs);
3337module_exit(target_core_exit_configfs);