target: Add force_pr_aptpl device attribute
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / drivers / target / target_core_configfs.c
1 /*******************************************************************************
2 * Filename: target_core_configfs.c
3 *
4 * This file contains ConfigFS logic for the Generic Target Engine project.
5 *
6 * (c) Copyright 2008-2013 Datera, Inc.
7 *
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>
25 #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>
38 #include <linux/spinlock.h>
39
40 #include <target/target_core_base.h>
41 #include <target/target_core_backend.h>
42 #include <target/target_core_fabric.h>
43 #include <target/target_core_fabric_configfs.h>
44 #include <target/target_core_configfs.h>
45 #include <target/configfs_macros.h>
46
47 #include "target_core_internal.h"
48 #include "target_core_alua.h"
49 #include "target_core_pr.h"
50 #include "target_core_rd.h"
51 #include "target_core_xcopy.h"
52
53 extern struct t10_alua_lu_gp *default_lu_gp;
54
55 static LIST_HEAD(g_tf_list);
56 static DEFINE_MUTEX(g_tf_lock);
57
58 struct target_core_configfs_attribute {
59 struct configfs_attribute attr;
60 ssize_t (*show)(void *, char *);
61 ssize_t (*store)(void *, const char *, size_t);
62 };
63
64 static struct config_group target_core_hbagroup;
65 static struct config_group alua_group;
66 static struct config_group alua_lu_gps_group;
67
68 static inline struct se_hba *
69 item_to_hba(struct config_item *item)
70 {
71 return container_of(to_config_group(item), struct se_hba, hba_group);
72 }
73
74 /*
75 * Attributes for /sys/kernel/config/target/
76 */
77 static ssize_t target_core_attr_show(struct config_item *item,
78 struct configfs_attribute *attr,
79 char *page)
80 {
81 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
82 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
83 utsname()->sysname, utsname()->machine);
84 }
85
86 static struct configfs_item_operations target_core_fabric_item_ops = {
87 .show_attribute = target_core_attr_show,
88 };
89
90 static struct configfs_attribute target_core_item_attr_version = {
91 .ca_owner = THIS_MODULE,
92 .ca_name = "version",
93 .ca_mode = S_IRUGO,
94 };
95
96 static struct target_fabric_configfs *target_core_get_fabric(
97 const char *name)
98 {
99 struct target_fabric_configfs *tf;
100
101 if (!name)
102 return NULL;
103
104 mutex_lock(&g_tf_lock);
105 list_for_each_entry(tf, &g_tf_list, tf_list) {
106 if (!strcmp(tf->tf_name, name)) {
107 atomic_inc(&tf->tf_access_cnt);
108 mutex_unlock(&g_tf_lock);
109 return tf;
110 }
111 }
112 mutex_unlock(&g_tf_lock);
113
114 return NULL;
115 }
116
117 /*
118 * Called from struct target_core_group_ops->make_group()
119 */
120 static struct config_group *target_core_register_fabric(
121 struct config_group *group,
122 const char *name)
123 {
124 struct target_fabric_configfs *tf;
125 int ret;
126
127 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
128 " %s\n", group, name);
129 /*
130 * Below are some hardcoded request_module() calls to automatically
131 * local fabric modules when the following is called:
132 *
133 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
134 *
135 * Note that this does not limit which TCM fabric module can be
136 * registered, but simply provids auto loading logic for modules with
137 * mkdir(2) system calls with known TCM fabric modules.
138 */
139 if (!strncmp(name, "iscsi", 5)) {
140 /*
141 * Automatically load the LIO Target fabric module when the
142 * following is called:
143 *
144 * mkdir -p $CONFIGFS/target/iscsi
145 */
146 ret = request_module("iscsi_target_mod");
147 if (ret < 0) {
148 pr_err("request_module() failed for"
149 " iscsi_target_mod.ko: %d\n", ret);
150 return ERR_PTR(-EINVAL);
151 }
152 } else if (!strncmp(name, "loopback", 8)) {
153 /*
154 * Automatically load the tcm_loop fabric module when the
155 * following is called:
156 *
157 * mkdir -p $CONFIGFS/target/loopback
158 */
159 ret = request_module("tcm_loop");
160 if (ret < 0) {
161 pr_err("request_module() failed for"
162 " tcm_loop.ko: %d\n", ret);
163 return ERR_PTR(-EINVAL);
164 }
165 }
166
167 tf = target_core_get_fabric(name);
168 if (!tf) {
169 pr_err("target_core_get_fabric() failed for %s\n",
170 name);
171 return ERR_PTR(-EINVAL);
172 }
173 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
174 " %s\n", tf->tf_name);
175 /*
176 * On a successful target_core_get_fabric() look, the returned
177 * struct target_fabric_configfs *tf will contain a usage reference.
178 */
179 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
180 &tf->tf_cit_tmpl.tfc_wwn_cit);
181
182 tf->tf_group.default_groups = tf->tf_default_groups;
183 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
184 tf->tf_group.default_groups[1] = NULL;
185
186 config_group_init_type_name(&tf->tf_group, name,
187 &tf->tf_cit_tmpl.tfc_wwn_cit);
188 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
189 &tf->tf_cit_tmpl.tfc_discovery_cit);
190
191 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
192 " %s\n", tf->tf_group.cg_item.ci_name);
193 /*
194 * Setup tf_ops.tf_subsys pointer for usage with configfs_depend_item()
195 */
196 tf->tf_ops.tf_subsys = tf->tf_subsys;
197 tf->tf_fabric = &tf->tf_group.cg_item;
198 pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
199 " for %s\n", name);
200
201 return &tf->tf_group;
202 }
203
204 /*
205 * Called from struct target_core_group_ops->drop_item()
206 */
207 static void target_core_deregister_fabric(
208 struct config_group *group,
209 struct config_item *item)
210 {
211 struct target_fabric_configfs *tf = container_of(
212 to_config_group(item), struct target_fabric_configfs, tf_group);
213 struct config_group *tf_group;
214 struct config_item *df_item;
215 int i;
216
217 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
218 " tf list\n", config_item_name(item));
219
220 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
221 " %s\n", tf->tf_name);
222 atomic_dec(&tf->tf_access_cnt);
223
224 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
225 " tf->tf_fabric for %s\n", tf->tf_name);
226 tf->tf_fabric = NULL;
227
228 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
229 " %s\n", config_item_name(item));
230
231 tf_group = &tf->tf_group;
232 for (i = 0; tf_group->default_groups[i]; i++) {
233 df_item = &tf_group->default_groups[i]->cg_item;
234 tf_group->default_groups[i] = NULL;
235 config_item_put(df_item);
236 }
237 config_item_put(item);
238 }
239
240 static struct configfs_group_operations target_core_fabric_group_ops = {
241 .make_group = &target_core_register_fabric,
242 .drop_item = &target_core_deregister_fabric,
243 };
244
245 /*
246 * All item attributes appearing in /sys/kernel/target/ appear here.
247 */
248 static struct configfs_attribute *target_core_fabric_item_attrs[] = {
249 &target_core_item_attr_version,
250 NULL,
251 };
252
253 /*
254 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
255 */
256 static struct config_item_type target_core_fabrics_item = {
257 .ct_item_ops = &target_core_fabric_item_ops,
258 .ct_group_ops = &target_core_fabric_group_ops,
259 .ct_attrs = target_core_fabric_item_attrs,
260 .ct_owner = THIS_MODULE,
261 };
262
263 static struct configfs_subsystem target_core_fabrics = {
264 .su_group = {
265 .cg_item = {
266 .ci_namebuf = "target",
267 .ci_type = &target_core_fabrics_item,
268 },
269 },
270 };
271
272 struct configfs_subsystem *target_core_subsystem[] = {
273 &target_core_fabrics,
274 NULL,
275 };
276
277 /*##############################################################################
278 // Start functions called by external Target Fabrics Modules
279 //############################################################################*/
280
281 /*
282 * First function called by fabric modules to:
283 *
284 * 1) Allocate a struct target_fabric_configfs and save the *fabric_cit pointer.
285 * 2) Add struct target_fabric_configfs to g_tf_list
286 * 3) Return struct target_fabric_configfs to fabric module to be passed
287 * into target_fabric_configfs_register().
288 */
289 struct target_fabric_configfs *target_fabric_configfs_init(
290 struct module *fabric_mod,
291 const char *name)
292 {
293 struct target_fabric_configfs *tf;
294
295 if (!(name)) {
296 pr_err("Unable to locate passed fabric name\n");
297 return ERR_PTR(-EINVAL);
298 }
299 if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
300 pr_err("Passed name: %s exceeds TARGET_FABRIC"
301 "_NAME_SIZE\n", name);
302 return ERR_PTR(-EINVAL);
303 }
304
305 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
306 if (!tf)
307 return ERR_PTR(-ENOMEM);
308
309 INIT_LIST_HEAD(&tf->tf_list);
310 atomic_set(&tf->tf_access_cnt, 0);
311 /*
312 * Setup the default generic struct config_item_type's (cits) in
313 * struct target_fabric_configfs->tf_cit_tmpl
314 */
315 tf->tf_module = fabric_mod;
316 target_fabric_setup_cits(tf);
317
318 tf->tf_subsys = target_core_subsystem[0];
319 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
320
321 mutex_lock(&g_tf_lock);
322 list_add_tail(&tf->tf_list, &g_tf_list);
323 mutex_unlock(&g_tf_lock);
324
325 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
326 ">>>>>>>>>>>>>>\n");
327 pr_debug("Initialized struct target_fabric_configfs: %p for"
328 " %s\n", tf, tf->tf_name);
329 return tf;
330 }
331 EXPORT_SYMBOL(target_fabric_configfs_init);
332
333 /*
334 * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
335 */
336 void target_fabric_configfs_free(
337 struct target_fabric_configfs *tf)
338 {
339 mutex_lock(&g_tf_lock);
340 list_del(&tf->tf_list);
341 mutex_unlock(&g_tf_lock);
342
343 kfree(tf);
344 }
345 EXPORT_SYMBOL(target_fabric_configfs_free);
346
347 /*
348 * Perform a sanity check of the passed tf->tf_ops before completing
349 * TCM fabric module registration.
350 */
351 static int target_fabric_tf_ops_check(
352 struct target_fabric_configfs *tf)
353 {
354 struct target_core_fabric_ops *tfo = &tf->tf_ops;
355
356 if (!tfo->get_fabric_name) {
357 pr_err("Missing tfo->get_fabric_name()\n");
358 return -EINVAL;
359 }
360 if (!tfo->get_fabric_proto_ident) {
361 pr_err("Missing tfo->get_fabric_proto_ident()\n");
362 return -EINVAL;
363 }
364 if (!tfo->tpg_get_wwn) {
365 pr_err("Missing tfo->tpg_get_wwn()\n");
366 return -EINVAL;
367 }
368 if (!tfo->tpg_get_tag) {
369 pr_err("Missing tfo->tpg_get_tag()\n");
370 return -EINVAL;
371 }
372 if (!tfo->tpg_get_default_depth) {
373 pr_err("Missing tfo->tpg_get_default_depth()\n");
374 return -EINVAL;
375 }
376 if (!tfo->tpg_get_pr_transport_id) {
377 pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
378 return -EINVAL;
379 }
380 if (!tfo->tpg_get_pr_transport_id_len) {
381 pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
382 return -EINVAL;
383 }
384 if (!tfo->tpg_check_demo_mode) {
385 pr_err("Missing tfo->tpg_check_demo_mode()\n");
386 return -EINVAL;
387 }
388 if (!tfo->tpg_check_demo_mode_cache) {
389 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
390 return -EINVAL;
391 }
392 if (!tfo->tpg_check_demo_mode_write_protect) {
393 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
394 return -EINVAL;
395 }
396 if (!tfo->tpg_check_prod_mode_write_protect) {
397 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
398 return -EINVAL;
399 }
400 if (!tfo->tpg_alloc_fabric_acl) {
401 pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
402 return -EINVAL;
403 }
404 if (!tfo->tpg_release_fabric_acl) {
405 pr_err("Missing tfo->tpg_release_fabric_acl()\n");
406 return -EINVAL;
407 }
408 if (!tfo->tpg_get_inst_index) {
409 pr_err("Missing tfo->tpg_get_inst_index()\n");
410 return -EINVAL;
411 }
412 if (!tfo->release_cmd) {
413 pr_err("Missing tfo->release_cmd()\n");
414 return -EINVAL;
415 }
416 if (!tfo->shutdown_session) {
417 pr_err("Missing tfo->shutdown_session()\n");
418 return -EINVAL;
419 }
420 if (!tfo->close_session) {
421 pr_err("Missing tfo->close_session()\n");
422 return -EINVAL;
423 }
424 if (!tfo->sess_get_index) {
425 pr_err("Missing tfo->sess_get_index()\n");
426 return -EINVAL;
427 }
428 if (!tfo->write_pending) {
429 pr_err("Missing tfo->write_pending()\n");
430 return -EINVAL;
431 }
432 if (!tfo->write_pending_status) {
433 pr_err("Missing tfo->write_pending_status()\n");
434 return -EINVAL;
435 }
436 if (!tfo->set_default_node_attributes) {
437 pr_err("Missing tfo->set_default_node_attributes()\n");
438 return -EINVAL;
439 }
440 if (!tfo->get_task_tag) {
441 pr_err("Missing tfo->get_task_tag()\n");
442 return -EINVAL;
443 }
444 if (!tfo->get_cmd_state) {
445 pr_err("Missing tfo->get_cmd_state()\n");
446 return -EINVAL;
447 }
448 if (!tfo->queue_data_in) {
449 pr_err("Missing tfo->queue_data_in()\n");
450 return -EINVAL;
451 }
452 if (!tfo->queue_status) {
453 pr_err("Missing tfo->queue_status()\n");
454 return -EINVAL;
455 }
456 if (!tfo->queue_tm_rsp) {
457 pr_err("Missing tfo->queue_tm_rsp()\n");
458 return -EINVAL;
459 }
460 if (!tfo->aborted_task) {
461 pr_err("Missing tfo->aborted_task()\n");
462 return -EINVAL;
463 }
464 /*
465 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
466 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
467 * target_core_fabric_configfs.c WWN+TPG group context code.
468 */
469 if (!tfo->fabric_make_wwn) {
470 pr_err("Missing tfo->fabric_make_wwn()\n");
471 return -EINVAL;
472 }
473 if (!tfo->fabric_drop_wwn) {
474 pr_err("Missing tfo->fabric_drop_wwn()\n");
475 return -EINVAL;
476 }
477 if (!tfo->fabric_make_tpg) {
478 pr_err("Missing tfo->fabric_make_tpg()\n");
479 return -EINVAL;
480 }
481 if (!tfo->fabric_drop_tpg) {
482 pr_err("Missing tfo->fabric_drop_tpg()\n");
483 return -EINVAL;
484 }
485
486 return 0;
487 }
488
489 /*
490 * Called 2nd from fabric module with returned parameter of
491 * struct target_fabric_configfs * from target_fabric_configfs_init().
492 *
493 * Upon a successful registration, the new fabric's struct config_item is
494 * return. Also, a pointer to this struct is set in the passed
495 * struct target_fabric_configfs.
496 */
497 int target_fabric_configfs_register(
498 struct target_fabric_configfs *tf)
499 {
500 int ret;
501
502 if (!tf) {
503 pr_err("Unable to locate target_fabric_configfs"
504 " pointer\n");
505 return -EINVAL;
506 }
507 if (!tf->tf_subsys) {
508 pr_err("Unable to target struct config_subsystem"
509 " pointer\n");
510 return -EINVAL;
511 }
512 ret = target_fabric_tf_ops_check(tf);
513 if (ret < 0)
514 return ret;
515
516 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
517 ">>>>>>>>>>\n");
518 return 0;
519 }
520 EXPORT_SYMBOL(target_fabric_configfs_register);
521
522 void target_fabric_configfs_deregister(
523 struct target_fabric_configfs *tf)
524 {
525 struct configfs_subsystem *su;
526
527 if (!tf) {
528 pr_err("Unable to locate passed target_fabric_"
529 "configfs\n");
530 return;
531 }
532 su = tf->tf_subsys;
533 if (!su) {
534 pr_err("Unable to locate passed tf->tf_subsys"
535 " pointer\n");
536 return;
537 }
538 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
539 ">>>>>>>>>>>>\n");
540 mutex_lock(&g_tf_lock);
541 if (atomic_read(&tf->tf_access_cnt)) {
542 mutex_unlock(&g_tf_lock);
543 pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
544 tf->tf_name);
545 BUG();
546 }
547 list_del(&tf->tf_list);
548 mutex_unlock(&g_tf_lock);
549
550 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
551 " %s\n", tf->tf_name);
552 tf->tf_module = NULL;
553 tf->tf_subsys = NULL;
554 kfree(tf);
555
556 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
557 ">>>>>\n");
558 }
559 EXPORT_SYMBOL(target_fabric_configfs_deregister);
560
561 /*##############################################################################
562 // Stop functions called by external Target Fabrics Modules
563 //############################################################################*/
564
565 /* Start functions for struct config_item_type target_core_dev_attrib_cit */
566
567 #define DEF_DEV_ATTRIB_SHOW(_name) \
568 static ssize_t target_core_dev_show_attr_##_name( \
569 struct se_dev_attrib *da, \
570 char *page) \
571 { \
572 return snprintf(page, PAGE_SIZE, "%u\n", \
573 (u32)da->da_dev->dev_attrib._name); \
574 }
575
576 #define DEF_DEV_ATTRIB_STORE(_name) \
577 static ssize_t target_core_dev_store_attr_##_name( \
578 struct se_dev_attrib *da, \
579 const char *page, \
580 size_t count) \
581 { \
582 unsigned long val; \
583 int ret; \
584 \
585 ret = kstrtoul(page, 0, &val); \
586 if (ret < 0) { \
587 pr_err("kstrtoul() failed with" \
588 " ret: %d\n", ret); \
589 return -EINVAL; \
590 } \
591 ret = se_dev_set_##_name(da->da_dev, (u32)val); \
592 \
593 return (!ret) ? count : -EINVAL; \
594 }
595
596 #define DEF_DEV_ATTRIB(_name) \
597 DEF_DEV_ATTRIB_SHOW(_name); \
598 DEF_DEV_ATTRIB_STORE(_name);
599
600 #define DEF_DEV_ATTRIB_RO(_name) \
601 DEF_DEV_ATTRIB_SHOW(_name);
602
603 CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
604 #define SE_DEV_ATTR(_name, _mode) \
605 static struct target_core_dev_attrib_attribute \
606 target_core_dev_attrib_##_name = \
607 __CONFIGFS_EATTR(_name, _mode, \
608 target_core_dev_show_attr_##_name, \
609 target_core_dev_store_attr_##_name);
610
611 #define SE_DEV_ATTR_RO(_name); \
612 static struct target_core_dev_attrib_attribute \
613 target_core_dev_attrib_##_name = \
614 __CONFIGFS_EATTR_RO(_name, \
615 target_core_dev_show_attr_##_name);
616
617 DEF_DEV_ATTRIB(emulate_model_alias);
618 SE_DEV_ATTR(emulate_model_alias, S_IRUGO | S_IWUSR);
619
620 DEF_DEV_ATTRIB(emulate_dpo);
621 SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
622
623 DEF_DEV_ATTRIB(emulate_fua_write);
624 SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
625
626 DEF_DEV_ATTRIB(emulate_fua_read);
627 SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
628
629 DEF_DEV_ATTRIB(emulate_write_cache);
630 SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
631
632 DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
633 SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
634
635 DEF_DEV_ATTRIB(emulate_tas);
636 SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
637
638 DEF_DEV_ATTRIB(emulate_tpu);
639 SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
640
641 DEF_DEV_ATTRIB(emulate_tpws);
642 SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
643
644 DEF_DEV_ATTRIB(emulate_caw);
645 SE_DEV_ATTR(emulate_caw, S_IRUGO | S_IWUSR);
646
647 DEF_DEV_ATTRIB(emulate_3pc);
648 SE_DEV_ATTR(emulate_3pc, S_IRUGO | S_IWUSR);
649
650 DEF_DEV_ATTRIB(pi_prot_type);
651 SE_DEV_ATTR(pi_prot_type, S_IRUGO | S_IWUSR);
652
653 DEF_DEV_ATTRIB_RO(hw_pi_prot_type);
654 SE_DEV_ATTR_RO(hw_pi_prot_type);
655
656 DEF_DEV_ATTRIB(pi_prot_format);
657 SE_DEV_ATTR(pi_prot_format, S_IRUGO | S_IWUSR);
658
659 DEF_DEV_ATTRIB(enforce_pr_isids);
660 SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
661
662 DEF_DEV_ATTRIB(is_nonrot);
663 SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
664
665 DEF_DEV_ATTRIB(emulate_rest_reord);
666 SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
667
668 DEF_DEV_ATTRIB(force_pr_aptpl);
669 SE_DEV_ATTR(force_pr_aptpl, S_IRUGO | S_IWUSR);
670
671 DEF_DEV_ATTRIB_RO(hw_block_size);
672 SE_DEV_ATTR_RO(hw_block_size);
673
674 DEF_DEV_ATTRIB(block_size);
675 SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
676
677 DEF_DEV_ATTRIB_RO(hw_max_sectors);
678 SE_DEV_ATTR_RO(hw_max_sectors);
679
680 DEF_DEV_ATTRIB(fabric_max_sectors);
681 SE_DEV_ATTR(fabric_max_sectors, S_IRUGO | S_IWUSR);
682
683 DEF_DEV_ATTRIB(optimal_sectors);
684 SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
685
686 DEF_DEV_ATTRIB_RO(hw_queue_depth);
687 SE_DEV_ATTR_RO(hw_queue_depth);
688
689 DEF_DEV_ATTRIB(queue_depth);
690 SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
691
692 DEF_DEV_ATTRIB(max_unmap_lba_count);
693 SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
694
695 DEF_DEV_ATTRIB(max_unmap_block_desc_count);
696 SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
697
698 DEF_DEV_ATTRIB(unmap_granularity);
699 SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
700
701 DEF_DEV_ATTRIB(unmap_granularity_alignment);
702 SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
703
704 DEF_DEV_ATTRIB(max_write_same_len);
705 SE_DEV_ATTR(max_write_same_len, S_IRUGO | S_IWUSR);
706
707 CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
708
709 static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
710 &target_core_dev_attrib_emulate_model_alias.attr,
711 &target_core_dev_attrib_emulate_dpo.attr,
712 &target_core_dev_attrib_emulate_fua_write.attr,
713 &target_core_dev_attrib_emulate_fua_read.attr,
714 &target_core_dev_attrib_emulate_write_cache.attr,
715 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
716 &target_core_dev_attrib_emulate_tas.attr,
717 &target_core_dev_attrib_emulate_tpu.attr,
718 &target_core_dev_attrib_emulate_tpws.attr,
719 &target_core_dev_attrib_emulate_caw.attr,
720 &target_core_dev_attrib_emulate_3pc.attr,
721 &target_core_dev_attrib_pi_prot_type.attr,
722 &target_core_dev_attrib_hw_pi_prot_type.attr,
723 &target_core_dev_attrib_pi_prot_format.attr,
724 &target_core_dev_attrib_enforce_pr_isids.attr,
725 &target_core_dev_attrib_force_pr_aptpl.attr,
726 &target_core_dev_attrib_is_nonrot.attr,
727 &target_core_dev_attrib_emulate_rest_reord.attr,
728 &target_core_dev_attrib_hw_block_size.attr,
729 &target_core_dev_attrib_block_size.attr,
730 &target_core_dev_attrib_hw_max_sectors.attr,
731 &target_core_dev_attrib_fabric_max_sectors.attr,
732 &target_core_dev_attrib_optimal_sectors.attr,
733 &target_core_dev_attrib_hw_queue_depth.attr,
734 &target_core_dev_attrib_queue_depth.attr,
735 &target_core_dev_attrib_max_unmap_lba_count.attr,
736 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
737 &target_core_dev_attrib_unmap_granularity.attr,
738 &target_core_dev_attrib_unmap_granularity_alignment.attr,
739 &target_core_dev_attrib_max_write_same_len.attr,
740 NULL,
741 };
742
743 static struct configfs_item_operations target_core_dev_attrib_ops = {
744 .show_attribute = target_core_dev_attrib_attr_show,
745 .store_attribute = target_core_dev_attrib_attr_store,
746 };
747
748 static struct config_item_type target_core_dev_attrib_cit = {
749 .ct_item_ops = &target_core_dev_attrib_ops,
750 .ct_attrs = target_core_dev_attrib_attrs,
751 .ct_owner = THIS_MODULE,
752 };
753
754 /* End functions for struct config_item_type target_core_dev_attrib_cit */
755
756 /* Start functions for struct config_item_type target_core_dev_wwn_cit */
757
758 CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
759 #define SE_DEV_WWN_ATTR(_name, _mode) \
760 static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
761 __CONFIGFS_EATTR(_name, _mode, \
762 target_core_dev_wwn_show_attr_##_name, \
763 target_core_dev_wwn_store_attr_##_name);
764
765 #define SE_DEV_WWN_ATTR_RO(_name); \
766 do { \
767 static struct target_core_dev_wwn_attribute \
768 target_core_dev_wwn_##_name = \
769 __CONFIGFS_EATTR_RO(_name, \
770 target_core_dev_wwn_show_attr_##_name); \
771 } while (0);
772
773 /*
774 * VPD page 0x80 Unit serial
775 */
776 static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
777 struct t10_wwn *t10_wwn,
778 char *page)
779 {
780 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
781 &t10_wwn->unit_serial[0]);
782 }
783
784 static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
785 struct t10_wwn *t10_wwn,
786 const char *page,
787 size_t count)
788 {
789 struct se_device *dev = t10_wwn->t10_dev;
790 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
791
792 /*
793 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
794 * from the struct scsi_device level firmware, do not allow
795 * VPD Unit Serial to be emulated.
796 *
797 * Note this struct scsi_device could also be emulating VPD
798 * information from its drivers/scsi LLD. But for now we assume
799 * it is doing 'the right thing' wrt a world wide unique
800 * VPD Unit Serial Number that OS dependent multipath can depend on.
801 */
802 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
803 pr_err("Underlying SCSI device firmware provided VPD"
804 " Unit Serial, ignoring request\n");
805 return -EOPNOTSUPP;
806 }
807
808 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
809 pr_err("Emulated VPD Unit Serial exceeds"
810 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
811 return -EOVERFLOW;
812 }
813 /*
814 * Check to see if any active $FABRIC_MOD exports exist. If they
815 * do exist, fail here as changing this information on the fly
816 * (underneath the initiator side OS dependent multipath code)
817 * could cause negative effects.
818 */
819 if (dev->export_count) {
820 pr_err("Unable to set VPD Unit Serial while"
821 " active %d $FABRIC_MOD exports exist\n",
822 dev->export_count);
823 return -EINVAL;
824 }
825
826 /*
827 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
828 *
829 * Also, strip any newline added from the userspace
830 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
831 */
832 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
833 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
834 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
835 "%s", strstrip(buf));
836 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
837
838 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
839 " %s\n", dev->t10_wwn.unit_serial);
840
841 return count;
842 }
843
844 SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
845
846 /*
847 * VPD page 0x83 Protocol Identifier
848 */
849 static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
850 struct t10_wwn *t10_wwn,
851 char *page)
852 {
853 struct t10_vpd *vpd;
854 unsigned char buf[VPD_TMP_BUF_SIZE];
855 ssize_t len = 0;
856
857 memset(buf, 0, VPD_TMP_BUF_SIZE);
858
859 spin_lock(&t10_wwn->t10_vpd_lock);
860 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
861 if (!vpd->protocol_identifier_set)
862 continue;
863
864 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
865
866 if (len + strlen(buf) >= PAGE_SIZE)
867 break;
868
869 len += sprintf(page+len, "%s", buf);
870 }
871 spin_unlock(&t10_wwn->t10_vpd_lock);
872
873 return len;
874 }
875
876 static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
877 struct t10_wwn *t10_wwn,
878 const char *page,
879 size_t count)
880 {
881 return -ENOSYS;
882 }
883
884 SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
885
886 /*
887 * Generic wrapper for dumping VPD identifiers by association.
888 */
889 #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
890 static ssize_t target_core_dev_wwn_show_attr_##_name( \
891 struct t10_wwn *t10_wwn, \
892 char *page) \
893 { \
894 struct t10_vpd *vpd; \
895 unsigned char buf[VPD_TMP_BUF_SIZE]; \
896 ssize_t len = 0; \
897 \
898 spin_lock(&t10_wwn->t10_vpd_lock); \
899 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
900 if (vpd->association != _assoc) \
901 continue; \
902 \
903 memset(buf, 0, VPD_TMP_BUF_SIZE); \
904 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
905 if (len + strlen(buf) >= PAGE_SIZE) \
906 break; \
907 len += sprintf(page+len, "%s", buf); \
908 \
909 memset(buf, 0, VPD_TMP_BUF_SIZE); \
910 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
911 if (len + strlen(buf) >= PAGE_SIZE) \
912 break; \
913 len += sprintf(page+len, "%s", buf); \
914 \
915 memset(buf, 0, VPD_TMP_BUF_SIZE); \
916 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
917 if (len + strlen(buf) >= PAGE_SIZE) \
918 break; \
919 len += sprintf(page+len, "%s", buf); \
920 } \
921 spin_unlock(&t10_wwn->t10_vpd_lock); \
922 \
923 return len; \
924 }
925
926 /*
927 * VPD page 0x83 Association: Logical Unit
928 */
929 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
930
931 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
932 struct t10_wwn *t10_wwn,
933 const char *page,
934 size_t count)
935 {
936 return -ENOSYS;
937 }
938
939 SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
940
941 /*
942 * VPD page 0x83 Association: Target Port
943 */
944 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
945
946 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
947 struct t10_wwn *t10_wwn,
948 const char *page,
949 size_t count)
950 {
951 return -ENOSYS;
952 }
953
954 SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
955
956 /*
957 * VPD page 0x83 Association: SCSI Target Device
958 */
959 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
960
961 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
962 struct t10_wwn *t10_wwn,
963 const char *page,
964 size_t count)
965 {
966 return -ENOSYS;
967 }
968
969 SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
970
971 CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
972
973 static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
974 &target_core_dev_wwn_vpd_unit_serial.attr,
975 &target_core_dev_wwn_vpd_protocol_identifier.attr,
976 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
977 &target_core_dev_wwn_vpd_assoc_target_port.attr,
978 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
979 NULL,
980 };
981
982 static struct configfs_item_operations target_core_dev_wwn_ops = {
983 .show_attribute = target_core_dev_wwn_attr_show,
984 .store_attribute = target_core_dev_wwn_attr_store,
985 };
986
987 static struct config_item_type target_core_dev_wwn_cit = {
988 .ct_item_ops = &target_core_dev_wwn_ops,
989 .ct_attrs = target_core_dev_wwn_attrs,
990 .ct_owner = THIS_MODULE,
991 };
992
993 /* End functions for struct config_item_type target_core_dev_wwn_cit */
994
995 /* Start functions for struct config_item_type target_core_dev_pr_cit */
996
997 CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
998 #define SE_DEV_PR_ATTR(_name, _mode) \
999 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1000 __CONFIGFS_EATTR(_name, _mode, \
1001 target_core_dev_pr_show_attr_##_name, \
1002 target_core_dev_pr_store_attr_##_name);
1003
1004 #define SE_DEV_PR_ATTR_RO(_name); \
1005 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1006 __CONFIGFS_EATTR_RO(_name, \
1007 target_core_dev_pr_show_attr_##_name);
1008
1009 static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1010 char *page)
1011 {
1012 struct se_node_acl *se_nacl;
1013 struct t10_pr_registration *pr_reg;
1014 char i_buf[PR_REG_ISID_ID_LEN];
1015
1016 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1017
1018 pr_reg = dev->dev_pr_res_holder;
1019 if (!pr_reg)
1020 return sprintf(page, "No SPC-3 Reservation holder\n");
1021
1022 se_nacl = pr_reg->pr_reg_nacl;
1023 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1024
1025 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
1026 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1027 se_nacl->initiatorname, i_buf);
1028 }
1029
1030 static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1031 char *page)
1032 {
1033 struct se_node_acl *se_nacl;
1034 ssize_t len;
1035
1036 se_nacl = dev->dev_reserved_node_acl;
1037 if (se_nacl) {
1038 len = sprintf(page,
1039 "SPC-2 Reservation: %s Initiator: %s\n",
1040 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1041 se_nacl->initiatorname);
1042 } else {
1043 len = sprintf(page, "No SPC-2 Reservation holder\n");
1044 }
1045 return len;
1046 }
1047
1048 static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
1049 char *page)
1050 {
1051 int ret;
1052
1053 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1054 return sprintf(page, "Passthrough\n");
1055
1056 spin_lock(&dev->dev_reservation_lock);
1057 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1058 ret = target_core_dev_pr_show_spc2_res(dev, page);
1059 else
1060 ret = target_core_dev_pr_show_spc3_res(dev, page);
1061 spin_unlock(&dev->dev_reservation_lock);
1062 return ret;
1063 }
1064
1065 SE_DEV_PR_ATTR_RO(res_holder);
1066
1067 static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
1068 struct se_device *dev, char *page)
1069 {
1070 ssize_t len = 0;
1071
1072 spin_lock(&dev->dev_reservation_lock);
1073 if (!dev->dev_pr_res_holder) {
1074 len = sprintf(page, "No SPC-3 Reservation holder\n");
1075 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1076 len = sprintf(page, "SPC-3 Reservation: All Target"
1077 " Ports registration\n");
1078 } else {
1079 len = sprintf(page, "SPC-3 Reservation: Single"
1080 " Target Port registration\n");
1081 }
1082
1083 spin_unlock(&dev->dev_reservation_lock);
1084 return len;
1085 }
1086
1087 SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1088
1089 static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
1090 struct se_device *dev, char *page)
1091 {
1092 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
1093 }
1094
1095 SE_DEV_PR_ATTR_RO(res_pr_generation);
1096
1097 /*
1098 * res_pr_holder_tg_port
1099 */
1100 static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
1101 struct se_device *dev, char *page)
1102 {
1103 struct se_node_acl *se_nacl;
1104 struct se_lun *lun;
1105 struct se_portal_group *se_tpg;
1106 struct t10_pr_registration *pr_reg;
1107 struct target_core_fabric_ops *tfo;
1108 ssize_t len = 0;
1109
1110 spin_lock(&dev->dev_reservation_lock);
1111 pr_reg = dev->dev_pr_res_holder;
1112 if (!pr_reg) {
1113 len = sprintf(page, "No SPC-3 Reservation holder\n");
1114 goto out_unlock;
1115 }
1116
1117 se_nacl = pr_reg->pr_reg_nacl;
1118 se_tpg = se_nacl->se_tpg;
1119 lun = pr_reg->pr_reg_tg_pt_lun;
1120 tfo = se_tpg->se_tpg_tfo;
1121
1122 len += sprintf(page+len, "SPC-3 Reservation: %s"
1123 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1124 tfo->tpg_get_wwn(se_tpg));
1125 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
1126 " Identifier Tag: %hu %s Portal Group Tag: %hu"
1127 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1128 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1129 tfo->get_fabric_name(), lun->unpacked_lun);
1130
1131 out_unlock:
1132 spin_unlock(&dev->dev_reservation_lock);
1133 return len;
1134 }
1135
1136 SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1137
1138 static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
1139 struct se_device *dev, char *page)
1140 {
1141 struct target_core_fabric_ops *tfo;
1142 struct t10_pr_registration *pr_reg;
1143 unsigned char buf[384];
1144 char i_buf[PR_REG_ISID_ID_LEN];
1145 ssize_t len = 0;
1146 int reg_count = 0;
1147
1148 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1149
1150 spin_lock(&dev->t10_pr.registration_lock);
1151 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1152 pr_reg_list) {
1153
1154 memset(buf, 0, 384);
1155 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1156 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1157 core_pr_dump_initiator_port(pr_reg, i_buf,
1158 PR_REG_ISID_ID_LEN);
1159 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1160 tfo->get_fabric_name(),
1161 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1162 pr_reg->pr_res_generation);
1163
1164 if (len + strlen(buf) >= PAGE_SIZE)
1165 break;
1166
1167 len += sprintf(page+len, "%s", buf);
1168 reg_count++;
1169 }
1170 spin_unlock(&dev->t10_pr.registration_lock);
1171
1172 if (!reg_count)
1173 len += sprintf(page+len, "None\n");
1174
1175 return len;
1176 }
1177
1178 SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1179
1180 static ssize_t target_core_dev_pr_show_attr_res_pr_type(
1181 struct se_device *dev, char *page)
1182 {
1183 struct t10_pr_registration *pr_reg;
1184 ssize_t len = 0;
1185
1186 spin_lock(&dev->dev_reservation_lock);
1187 pr_reg = dev->dev_pr_res_holder;
1188 if (pr_reg) {
1189 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1190 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1191 } else {
1192 len = sprintf(page, "No SPC-3 Reservation holder\n");
1193 }
1194
1195 spin_unlock(&dev->dev_reservation_lock);
1196 return len;
1197 }
1198
1199 SE_DEV_PR_ATTR_RO(res_pr_type);
1200
1201 static ssize_t target_core_dev_pr_show_attr_res_type(
1202 struct se_device *dev, char *page)
1203 {
1204 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1205 return sprintf(page, "SPC_PASSTHROUGH\n");
1206 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1207 return sprintf(page, "SPC2_RESERVATIONS\n");
1208 else
1209 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1210 }
1211
1212 SE_DEV_PR_ATTR_RO(res_type);
1213
1214 static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
1215 struct se_device *dev, char *page)
1216 {
1217 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1218 return 0;
1219
1220 return sprintf(page, "APTPL Bit Status: %s\n",
1221 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1222 }
1223
1224 SE_DEV_PR_ATTR_RO(res_aptpl_active);
1225
1226 /*
1227 * res_aptpl_metadata
1228 */
1229 static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
1230 struct se_device *dev, char *page)
1231 {
1232 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1233 return 0;
1234
1235 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1236 }
1237
1238 enum {
1239 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1240 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1241 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1242 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1243 };
1244
1245 static match_table_t tokens = {
1246 {Opt_initiator_fabric, "initiator_fabric=%s"},
1247 {Opt_initiator_node, "initiator_node=%s"},
1248 {Opt_initiator_sid, "initiator_sid=%s"},
1249 {Opt_sa_res_key, "sa_res_key=%s"},
1250 {Opt_res_holder, "res_holder=%d"},
1251 {Opt_res_type, "res_type=%d"},
1252 {Opt_res_scope, "res_scope=%d"},
1253 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1254 {Opt_mapped_lun, "mapped_lun=%d"},
1255 {Opt_target_fabric, "target_fabric=%s"},
1256 {Opt_target_node, "target_node=%s"},
1257 {Opt_tpgt, "tpgt=%d"},
1258 {Opt_port_rtpi, "port_rtpi=%d"},
1259 {Opt_target_lun, "target_lun=%d"},
1260 {Opt_err, NULL}
1261 };
1262
1263 static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
1264 struct se_device *dev,
1265 const char *page,
1266 size_t count)
1267 {
1268 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1269 unsigned char *t_fabric = NULL, *t_port = NULL;
1270 char *orig, *ptr, *opts;
1271 substring_t args[MAX_OPT_ARGS];
1272 unsigned long long tmp_ll;
1273 u64 sa_res_key = 0;
1274 u32 mapped_lun = 0, target_lun = 0;
1275 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1276 u16 port_rpti = 0, tpgt = 0;
1277 u8 type = 0, scope;
1278
1279 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1280 return 0;
1281 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1282 return 0;
1283
1284 if (dev->export_count) {
1285 pr_debug("Unable to process APTPL metadata while"
1286 " active fabric exports exist\n");
1287 return -EINVAL;
1288 }
1289
1290 opts = kstrdup(page, GFP_KERNEL);
1291 if (!opts)
1292 return -ENOMEM;
1293
1294 orig = opts;
1295 while ((ptr = strsep(&opts, ",\n")) != NULL) {
1296 if (!*ptr)
1297 continue;
1298
1299 token = match_token(ptr, tokens, args);
1300 switch (token) {
1301 case Opt_initiator_fabric:
1302 i_fabric = match_strdup(args);
1303 if (!i_fabric) {
1304 ret = -ENOMEM;
1305 goto out;
1306 }
1307 break;
1308 case Opt_initiator_node:
1309 i_port = match_strdup(args);
1310 if (!i_port) {
1311 ret = -ENOMEM;
1312 goto out;
1313 }
1314 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
1315 pr_err("APTPL metadata initiator_node="
1316 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1317 PR_APTPL_MAX_IPORT_LEN);
1318 ret = -EINVAL;
1319 break;
1320 }
1321 break;
1322 case Opt_initiator_sid:
1323 isid = match_strdup(args);
1324 if (!isid) {
1325 ret = -ENOMEM;
1326 goto out;
1327 }
1328 if (strlen(isid) >= PR_REG_ISID_LEN) {
1329 pr_err("APTPL metadata initiator_isid"
1330 "= exceeds PR_REG_ISID_LEN: %d\n",
1331 PR_REG_ISID_LEN);
1332 ret = -EINVAL;
1333 break;
1334 }
1335 break;
1336 case Opt_sa_res_key:
1337 ret = kstrtoull(args->from, 0, &tmp_ll);
1338 if (ret < 0) {
1339 pr_err("kstrtoull() failed for sa_res_key=\n");
1340 goto out;
1341 }
1342 sa_res_key = (u64)tmp_ll;
1343 break;
1344 /*
1345 * PR APTPL Metadata for Reservation
1346 */
1347 case Opt_res_holder:
1348 match_int(args, &arg);
1349 res_holder = arg;
1350 break;
1351 case Opt_res_type:
1352 match_int(args, &arg);
1353 type = (u8)arg;
1354 break;
1355 case Opt_res_scope:
1356 match_int(args, &arg);
1357 scope = (u8)arg;
1358 break;
1359 case Opt_res_all_tg_pt:
1360 match_int(args, &arg);
1361 all_tg_pt = (int)arg;
1362 break;
1363 case Opt_mapped_lun:
1364 match_int(args, &arg);
1365 mapped_lun = (u32)arg;
1366 break;
1367 /*
1368 * PR APTPL Metadata for Target Port
1369 */
1370 case Opt_target_fabric:
1371 t_fabric = match_strdup(args);
1372 if (!t_fabric) {
1373 ret = -ENOMEM;
1374 goto out;
1375 }
1376 break;
1377 case Opt_target_node:
1378 t_port = match_strdup(args);
1379 if (!t_port) {
1380 ret = -ENOMEM;
1381 goto out;
1382 }
1383 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
1384 pr_err("APTPL metadata target_node="
1385 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1386 PR_APTPL_MAX_TPORT_LEN);
1387 ret = -EINVAL;
1388 break;
1389 }
1390 break;
1391 case Opt_tpgt:
1392 match_int(args, &arg);
1393 tpgt = (u16)arg;
1394 break;
1395 case Opt_port_rtpi:
1396 match_int(args, &arg);
1397 port_rpti = (u16)arg;
1398 break;
1399 case Opt_target_lun:
1400 match_int(args, &arg);
1401 target_lun = (u32)arg;
1402 break;
1403 default:
1404 break;
1405 }
1406 }
1407
1408 if (!i_port || !t_port || !sa_res_key) {
1409 pr_err("Illegal parameters for APTPL registration\n");
1410 ret = -EINVAL;
1411 goto out;
1412 }
1413
1414 if (res_holder && !(type)) {
1415 pr_err("Illegal PR type: 0x%02x for reservation"
1416 " holder\n", type);
1417 ret = -EINVAL;
1418 goto out;
1419 }
1420
1421 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1422 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1423 res_holder, all_tg_pt, type);
1424 out:
1425 kfree(i_fabric);
1426 kfree(i_port);
1427 kfree(isid);
1428 kfree(t_fabric);
1429 kfree(t_port);
1430 kfree(orig);
1431 return (ret == 0) ? count : ret;
1432 }
1433
1434 SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1435
1436 CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
1437
1438 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1439 &target_core_dev_pr_res_holder.attr,
1440 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1441 &target_core_dev_pr_res_pr_generation.attr,
1442 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1443 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1444 &target_core_dev_pr_res_pr_type.attr,
1445 &target_core_dev_pr_res_type.attr,
1446 &target_core_dev_pr_res_aptpl_active.attr,
1447 &target_core_dev_pr_res_aptpl_metadata.attr,
1448 NULL,
1449 };
1450
1451 static struct configfs_item_operations target_core_dev_pr_ops = {
1452 .show_attribute = target_core_dev_pr_attr_show,
1453 .store_attribute = target_core_dev_pr_attr_store,
1454 };
1455
1456 static struct config_item_type target_core_dev_pr_cit = {
1457 .ct_item_ops = &target_core_dev_pr_ops,
1458 .ct_attrs = target_core_dev_pr_attrs,
1459 .ct_owner = THIS_MODULE,
1460 };
1461
1462 /* End functions for struct config_item_type target_core_dev_pr_cit */
1463
1464 /* Start functions for struct config_item_type target_core_dev_cit */
1465
1466 static ssize_t target_core_show_dev_info(void *p, char *page)
1467 {
1468 struct se_device *dev = p;
1469 struct se_subsystem_api *t = dev->transport;
1470 int bl = 0;
1471 ssize_t read_bytes = 0;
1472
1473 transport_dump_dev_state(dev, page, &bl);
1474 read_bytes += bl;
1475 read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
1476 return read_bytes;
1477 }
1478
1479 static struct target_core_configfs_attribute target_core_attr_dev_info = {
1480 .attr = { .ca_owner = THIS_MODULE,
1481 .ca_name = "info",
1482 .ca_mode = S_IRUGO },
1483 .show = target_core_show_dev_info,
1484 .store = NULL,
1485 };
1486
1487 static ssize_t target_core_store_dev_control(
1488 void *p,
1489 const char *page,
1490 size_t count)
1491 {
1492 struct se_device *dev = p;
1493 struct se_subsystem_api *t = dev->transport;
1494
1495 return t->set_configfs_dev_params(dev, page, count);
1496 }
1497
1498 static struct target_core_configfs_attribute target_core_attr_dev_control = {
1499 .attr = { .ca_owner = THIS_MODULE,
1500 .ca_name = "control",
1501 .ca_mode = S_IWUSR },
1502 .show = NULL,
1503 .store = target_core_store_dev_control,
1504 };
1505
1506 static ssize_t target_core_show_dev_alias(void *p, char *page)
1507 {
1508 struct se_device *dev = p;
1509
1510 if (!(dev->dev_flags & DF_USING_ALIAS))
1511 return 0;
1512
1513 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1514 }
1515
1516 static ssize_t target_core_store_dev_alias(
1517 void *p,
1518 const char *page,
1519 size_t count)
1520 {
1521 struct se_device *dev = p;
1522 struct se_hba *hba = dev->se_hba;
1523 ssize_t read_bytes;
1524
1525 if (count > (SE_DEV_ALIAS_LEN-1)) {
1526 pr_err("alias count: %d exceeds"
1527 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1528 SE_DEV_ALIAS_LEN-1);
1529 return -EINVAL;
1530 }
1531
1532 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
1533 if (!read_bytes)
1534 return -EINVAL;
1535 if (dev->dev_alias[read_bytes - 1] == '\n')
1536 dev->dev_alias[read_bytes - 1] = '\0';
1537
1538 dev->dev_flags |= DF_USING_ALIAS;
1539
1540 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1541 config_item_name(&hba->hba_group.cg_item),
1542 config_item_name(&dev->dev_group.cg_item),
1543 dev->dev_alias);
1544
1545 return read_bytes;
1546 }
1547
1548 static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1549 .attr = { .ca_owner = THIS_MODULE,
1550 .ca_name = "alias",
1551 .ca_mode = S_IRUGO | S_IWUSR },
1552 .show = target_core_show_dev_alias,
1553 .store = target_core_store_dev_alias,
1554 };
1555
1556 static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1557 {
1558 struct se_device *dev = p;
1559
1560 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1561 return 0;
1562
1563 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1564 }
1565
1566 static ssize_t target_core_store_dev_udev_path(
1567 void *p,
1568 const char *page,
1569 size_t count)
1570 {
1571 struct se_device *dev = p;
1572 struct se_hba *hba = dev->se_hba;
1573 ssize_t read_bytes;
1574
1575 if (count > (SE_UDEV_PATH_LEN-1)) {
1576 pr_err("udev_path count: %d exceeds"
1577 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1578 SE_UDEV_PATH_LEN-1);
1579 return -EINVAL;
1580 }
1581
1582 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1583 "%s", page);
1584 if (!read_bytes)
1585 return -EINVAL;
1586 if (dev->udev_path[read_bytes - 1] == '\n')
1587 dev->udev_path[read_bytes - 1] = '\0';
1588
1589 dev->dev_flags |= DF_USING_UDEV_PATH;
1590
1591 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1592 config_item_name(&hba->hba_group.cg_item),
1593 config_item_name(&dev->dev_group.cg_item),
1594 dev->udev_path);
1595
1596 return read_bytes;
1597 }
1598
1599 static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1600 .attr = { .ca_owner = THIS_MODULE,
1601 .ca_name = "udev_path",
1602 .ca_mode = S_IRUGO | S_IWUSR },
1603 .show = target_core_show_dev_udev_path,
1604 .store = target_core_store_dev_udev_path,
1605 };
1606
1607 static ssize_t target_core_show_dev_enable(void *p, char *page)
1608 {
1609 struct se_device *dev = p;
1610
1611 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1612 }
1613
1614 static ssize_t target_core_store_dev_enable(
1615 void *p,
1616 const char *page,
1617 size_t count)
1618 {
1619 struct se_device *dev = p;
1620 char *ptr;
1621 int ret;
1622
1623 ptr = strstr(page, "1");
1624 if (!ptr) {
1625 pr_err("For dev_enable ops, only valid value"
1626 " is \"1\"\n");
1627 return -EINVAL;
1628 }
1629
1630 ret = target_configure_device(dev);
1631 if (ret)
1632 return ret;
1633 return count;
1634 }
1635
1636 static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1637 .attr = { .ca_owner = THIS_MODULE,
1638 .ca_name = "enable",
1639 .ca_mode = S_IRUGO | S_IWUSR },
1640 .show = target_core_show_dev_enable,
1641 .store = target_core_store_dev_enable,
1642 };
1643
1644 static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1645 {
1646 struct se_device *dev = p;
1647 struct config_item *lu_ci;
1648 struct t10_alua_lu_gp *lu_gp;
1649 struct t10_alua_lu_gp_member *lu_gp_mem;
1650 ssize_t len = 0;
1651
1652 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1653 if (!lu_gp_mem)
1654 return 0;
1655
1656 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1657 lu_gp = lu_gp_mem->lu_gp;
1658 if (lu_gp) {
1659 lu_ci = &lu_gp->lu_gp_group.cg_item;
1660 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1661 config_item_name(lu_ci), lu_gp->lu_gp_id);
1662 }
1663 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1664
1665 return len;
1666 }
1667
1668 static ssize_t target_core_store_alua_lu_gp(
1669 void *p,
1670 const char *page,
1671 size_t count)
1672 {
1673 struct se_device *dev = p;
1674 struct se_hba *hba = dev->se_hba;
1675 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1676 struct t10_alua_lu_gp_member *lu_gp_mem;
1677 unsigned char buf[LU_GROUP_NAME_BUF];
1678 int move = 0;
1679
1680 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1681 if (!lu_gp_mem)
1682 return 0;
1683
1684 if (count > LU_GROUP_NAME_BUF) {
1685 pr_err("ALUA LU Group Alias too large!\n");
1686 return -EINVAL;
1687 }
1688 memset(buf, 0, LU_GROUP_NAME_BUF);
1689 memcpy(buf, page, count);
1690 /*
1691 * Any ALUA logical unit alias besides "NULL" means we will be
1692 * making a new group association.
1693 */
1694 if (strcmp(strstrip(buf), "NULL")) {
1695 /*
1696 * core_alua_get_lu_gp_by_name() will increment reference to
1697 * struct t10_alua_lu_gp. This reference is released with
1698 * core_alua_get_lu_gp_by_name below().
1699 */
1700 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
1701 if (!lu_gp_new)
1702 return -ENODEV;
1703 }
1704
1705 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1706 lu_gp = lu_gp_mem->lu_gp;
1707 if (lu_gp) {
1708 /*
1709 * Clearing an existing lu_gp association, and replacing
1710 * with NULL
1711 */
1712 if (!lu_gp_new) {
1713 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1714 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1715 " %hu\n",
1716 config_item_name(&hba->hba_group.cg_item),
1717 config_item_name(&dev->dev_group.cg_item),
1718 config_item_name(&lu_gp->lu_gp_group.cg_item),
1719 lu_gp->lu_gp_id);
1720
1721 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1722 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1723
1724 return count;
1725 }
1726 /*
1727 * Removing existing association of lu_gp_mem with lu_gp
1728 */
1729 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1730 move = 1;
1731 }
1732 /*
1733 * Associate lu_gp_mem with lu_gp_new.
1734 */
1735 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1736 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1737
1738 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1739 " core/alua/lu_gps/%s, ID: %hu\n",
1740 (move) ? "Moving" : "Adding",
1741 config_item_name(&hba->hba_group.cg_item),
1742 config_item_name(&dev->dev_group.cg_item),
1743 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1744 lu_gp_new->lu_gp_id);
1745
1746 core_alua_put_lu_gp_from_name(lu_gp_new);
1747 return count;
1748 }
1749
1750 static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1751 .attr = { .ca_owner = THIS_MODULE,
1752 .ca_name = "alua_lu_gp",
1753 .ca_mode = S_IRUGO | S_IWUSR },
1754 .show = target_core_show_alua_lu_gp,
1755 .store = target_core_store_alua_lu_gp,
1756 };
1757
1758 static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1759 {
1760 struct se_device *dev = p;
1761 struct t10_alua_lba_map *map;
1762 struct t10_alua_lba_map_member *mem;
1763 char *b = page;
1764 int bl = 0;
1765 char state;
1766
1767 spin_lock(&dev->t10_alua.lba_map_lock);
1768 if (!list_empty(&dev->t10_alua.lba_map_list))
1769 bl += sprintf(b + bl, "%u %u\n",
1770 dev->t10_alua.lba_map_segment_size,
1771 dev->t10_alua.lba_map_segment_multiplier);
1772 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1773 bl += sprintf(b + bl, "%llu %llu",
1774 map->lba_map_first_lba, map->lba_map_last_lba);
1775 list_for_each_entry(mem, &map->lba_map_mem_list,
1776 lba_map_mem_list) {
1777 switch (mem->lba_map_mem_alua_state) {
1778 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1779 state = 'O';
1780 break;
1781 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1782 state = 'A';
1783 break;
1784 case ALUA_ACCESS_STATE_STANDBY:
1785 state = 'S';
1786 break;
1787 case ALUA_ACCESS_STATE_UNAVAILABLE:
1788 state = 'U';
1789 break;
1790 default:
1791 state = '.';
1792 break;
1793 }
1794 bl += sprintf(b + bl, " %d:%c",
1795 mem->lba_map_mem_alua_pg_id, state);
1796 }
1797 bl += sprintf(b + bl, "\n");
1798 }
1799 spin_unlock(&dev->t10_alua.lba_map_lock);
1800 return bl;
1801 }
1802
1803 static ssize_t target_core_store_dev_lba_map(
1804 void *p,
1805 const char *page,
1806 size_t count)
1807 {
1808 struct se_device *dev = p;
1809 struct t10_alua_lba_map *lba_map = NULL;
1810 struct list_head lba_list;
1811 char *map_entries, *ptr;
1812 char state;
1813 int pg_num = -1, pg;
1814 int ret = 0, num = 0, pg_id, alua_state;
1815 unsigned long start_lba = -1, end_lba = -1;
1816 unsigned long segment_size = -1, segment_mult = -1;
1817
1818 map_entries = kstrdup(page, GFP_KERNEL);
1819 if (!map_entries)
1820 return -ENOMEM;
1821
1822 INIT_LIST_HEAD(&lba_list);
1823 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1824 if (!*ptr)
1825 continue;
1826
1827 if (num == 0) {
1828 if (sscanf(ptr, "%lu %lu\n",
1829 &segment_size, &segment_mult) != 2) {
1830 pr_err("Invalid line %d\n", num);
1831 ret = -EINVAL;
1832 break;
1833 }
1834 num++;
1835 continue;
1836 }
1837 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1838 pr_err("Invalid line %d\n", num);
1839 ret = -EINVAL;
1840 break;
1841 }
1842 ptr = strchr(ptr, ' ');
1843 if (!ptr) {
1844 pr_err("Invalid line %d, missing end lba\n", num);
1845 ret = -EINVAL;
1846 break;
1847 }
1848 ptr++;
1849 ptr = strchr(ptr, ' ');
1850 if (!ptr) {
1851 pr_err("Invalid line %d, missing state definitions\n",
1852 num);
1853 ret = -EINVAL;
1854 break;
1855 }
1856 ptr++;
1857 lba_map = core_alua_allocate_lba_map(&lba_list,
1858 start_lba, end_lba);
1859 if (IS_ERR(lba_map)) {
1860 ret = PTR_ERR(lba_map);
1861 break;
1862 }
1863 pg = 0;
1864 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1865 switch (state) {
1866 case 'O':
1867 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1868 break;
1869 case 'A':
1870 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1871 break;
1872 case 'S':
1873 alua_state = ALUA_ACCESS_STATE_STANDBY;
1874 break;
1875 case 'U':
1876 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1877 break;
1878 default:
1879 pr_err("Invalid ALUA state '%c'\n", state);
1880 ret = -EINVAL;
1881 goto out;
1882 }
1883
1884 ret = core_alua_allocate_lba_map_mem(lba_map,
1885 pg_id, alua_state);
1886 if (ret) {
1887 pr_err("Invalid target descriptor %d:%c "
1888 "at line %d\n",
1889 pg_id, state, num);
1890 break;
1891 }
1892 pg++;
1893 ptr = strchr(ptr, ' ');
1894 if (ptr)
1895 ptr++;
1896 else
1897 break;
1898 }
1899 if (pg_num == -1)
1900 pg_num = pg;
1901 else if (pg != pg_num) {
1902 pr_err("Only %d from %d port groups definitions "
1903 "at line %d\n", pg, pg_num, num);
1904 ret = -EINVAL;
1905 break;
1906 }
1907 num++;
1908 }
1909 out:
1910 if (ret) {
1911 core_alua_free_lba_map(&lba_list);
1912 count = ret;
1913 } else
1914 core_alua_set_lba_map(dev, &lba_list,
1915 segment_size, segment_mult);
1916 kfree(map_entries);
1917 return count;
1918 }
1919
1920 static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1921 .attr = { .ca_owner = THIS_MODULE,
1922 .ca_name = "lba_map",
1923 .ca_mode = S_IRUGO | S_IWUSR },
1924 .show = target_core_show_dev_lba_map,
1925 .store = target_core_store_dev_lba_map,
1926 };
1927
1928 static struct configfs_attribute *lio_core_dev_attrs[] = {
1929 &target_core_attr_dev_info.attr,
1930 &target_core_attr_dev_control.attr,
1931 &target_core_attr_dev_alias.attr,
1932 &target_core_attr_dev_udev_path.attr,
1933 &target_core_attr_dev_enable.attr,
1934 &target_core_attr_dev_alua_lu_gp.attr,
1935 &target_core_attr_dev_lba_map.attr,
1936 NULL,
1937 };
1938
1939 static void target_core_dev_release(struct config_item *item)
1940 {
1941 struct config_group *dev_cg = to_config_group(item);
1942 struct se_device *dev =
1943 container_of(dev_cg, struct se_device, dev_group);
1944
1945 kfree(dev_cg->default_groups);
1946 target_free_device(dev);
1947 }
1948
1949 static ssize_t target_core_dev_show(struct config_item *item,
1950 struct configfs_attribute *attr,
1951 char *page)
1952 {
1953 struct config_group *dev_cg = to_config_group(item);
1954 struct se_device *dev =
1955 container_of(dev_cg, struct se_device, dev_group);
1956 struct target_core_configfs_attribute *tc_attr = container_of(
1957 attr, struct target_core_configfs_attribute, attr);
1958
1959 if (!tc_attr->show)
1960 return -EINVAL;
1961
1962 return tc_attr->show(dev, page);
1963 }
1964
1965 static ssize_t target_core_dev_store(struct config_item *item,
1966 struct configfs_attribute *attr,
1967 const char *page, size_t count)
1968 {
1969 struct config_group *dev_cg = to_config_group(item);
1970 struct se_device *dev =
1971 container_of(dev_cg, struct se_device, dev_group);
1972 struct target_core_configfs_attribute *tc_attr = container_of(
1973 attr, struct target_core_configfs_attribute, attr);
1974
1975 if (!tc_attr->store)
1976 return -EINVAL;
1977
1978 return tc_attr->store(dev, page, count);
1979 }
1980
1981 static struct configfs_item_operations target_core_dev_item_ops = {
1982 .release = target_core_dev_release,
1983 .show_attribute = target_core_dev_show,
1984 .store_attribute = target_core_dev_store,
1985 };
1986
1987 static struct config_item_type target_core_dev_cit = {
1988 .ct_item_ops = &target_core_dev_item_ops,
1989 .ct_attrs = lio_core_dev_attrs,
1990 .ct_owner = THIS_MODULE,
1991 };
1992
1993 /* End functions for struct config_item_type target_core_dev_cit */
1994
1995 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1996
1997 CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1998 #define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
1999 static struct target_core_alua_lu_gp_attribute \
2000 target_core_alua_lu_gp_##_name = \
2001 __CONFIGFS_EATTR(_name, _mode, \
2002 target_core_alua_lu_gp_show_attr_##_name, \
2003 target_core_alua_lu_gp_store_attr_##_name);
2004
2005 #define SE_DEV_ALUA_LU_ATTR_RO(_name) \
2006 static struct target_core_alua_lu_gp_attribute \
2007 target_core_alua_lu_gp_##_name = \
2008 __CONFIGFS_EATTR_RO(_name, \
2009 target_core_alua_lu_gp_show_attr_##_name);
2010
2011 /*
2012 * lu_gp_id
2013 */
2014 static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2015 struct t10_alua_lu_gp *lu_gp,
2016 char *page)
2017 {
2018 if (!lu_gp->lu_gp_valid_id)
2019 return 0;
2020
2021 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2022 }
2023
2024 static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2025 struct t10_alua_lu_gp *lu_gp,
2026 const char *page,
2027 size_t count)
2028 {
2029 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2030 unsigned long lu_gp_id;
2031 int ret;
2032
2033 ret = kstrtoul(page, 0, &lu_gp_id);
2034 if (ret < 0) {
2035 pr_err("kstrtoul() returned %d for"
2036 " lu_gp_id\n", ret);
2037 return ret;
2038 }
2039 if (lu_gp_id > 0x0000ffff) {
2040 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2041 " 0x0000ffff\n", lu_gp_id);
2042 return -EINVAL;
2043 }
2044
2045 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2046 if (ret < 0)
2047 return -EINVAL;
2048
2049 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2050 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2051 config_item_name(&alua_lu_gp_cg->cg_item),
2052 lu_gp->lu_gp_id);
2053
2054 return count;
2055 }
2056
2057 SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2058
2059 /*
2060 * members
2061 */
2062 static ssize_t target_core_alua_lu_gp_show_attr_members(
2063 struct t10_alua_lu_gp *lu_gp,
2064 char *page)
2065 {
2066 struct se_device *dev;
2067 struct se_hba *hba;
2068 struct t10_alua_lu_gp_member *lu_gp_mem;
2069 ssize_t len = 0, cur_len;
2070 unsigned char buf[LU_GROUP_NAME_BUF];
2071
2072 memset(buf, 0, LU_GROUP_NAME_BUF);
2073
2074 spin_lock(&lu_gp->lu_gp_lock);
2075 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2076 dev = lu_gp_mem->lu_gp_mem_dev;
2077 hba = dev->se_hba;
2078
2079 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2080 config_item_name(&hba->hba_group.cg_item),
2081 config_item_name(&dev->dev_group.cg_item));
2082 cur_len++; /* Extra byte for NULL terminator */
2083
2084 if ((cur_len + len) > PAGE_SIZE) {
2085 pr_warn("Ran out of lu_gp_show_attr"
2086 "_members buffer\n");
2087 break;
2088 }
2089 memcpy(page+len, buf, cur_len);
2090 len += cur_len;
2091 }
2092 spin_unlock(&lu_gp->lu_gp_lock);
2093
2094 return len;
2095 }
2096
2097 SE_DEV_ALUA_LU_ATTR_RO(members);
2098
2099 CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2100
2101 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2102 &target_core_alua_lu_gp_lu_gp_id.attr,
2103 &target_core_alua_lu_gp_members.attr,
2104 NULL,
2105 };
2106
2107 static void target_core_alua_lu_gp_release(struct config_item *item)
2108 {
2109 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2110 struct t10_alua_lu_gp, lu_gp_group);
2111
2112 core_alua_free_lu_gp(lu_gp);
2113 }
2114
2115 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
2116 .release = target_core_alua_lu_gp_release,
2117 .show_attribute = target_core_alua_lu_gp_attr_show,
2118 .store_attribute = target_core_alua_lu_gp_attr_store,
2119 };
2120
2121 static struct config_item_type target_core_alua_lu_gp_cit = {
2122 .ct_item_ops = &target_core_alua_lu_gp_ops,
2123 .ct_attrs = target_core_alua_lu_gp_attrs,
2124 .ct_owner = THIS_MODULE,
2125 };
2126
2127 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2128
2129 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2130
2131 static struct config_group *target_core_alua_create_lu_gp(
2132 struct config_group *group,
2133 const char *name)
2134 {
2135 struct t10_alua_lu_gp *lu_gp;
2136 struct config_group *alua_lu_gp_cg = NULL;
2137 struct config_item *alua_lu_gp_ci = NULL;
2138
2139 lu_gp = core_alua_allocate_lu_gp(name, 0);
2140 if (IS_ERR(lu_gp))
2141 return NULL;
2142
2143 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2144 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2145
2146 config_group_init_type_name(alua_lu_gp_cg, name,
2147 &target_core_alua_lu_gp_cit);
2148
2149 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2150 " Group: core/alua/lu_gps/%s\n",
2151 config_item_name(alua_lu_gp_ci));
2152
2153 return alua_lu_gp_cg;
2154
2155 }
2156
2157 static void target_core_alua_drop_lu_gp(
2158 struct config_group *group,
2159 struct config_item *item)
2160 {
2161 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2162 struct t10_alua_lu_gp, lu_gp_group);
2163
2164 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2165 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2166 config_item_name(item), lu_gp->lu_gp_id);
2167 /*
2168 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2169 * -> target_core_alua_lu_gp_release()
2170 */
2171 config_item_put(item);
2172 }
2173
2174 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2175 .make_group = &target_core_alua_create_lu_gp,
2176 .drop_item = &target_core_alua_drop_lu_gp,
2177 };
2178
2179 static struct config_item_type target_core_alua_lu_gps_cit = {
2180 .ct_item_ops = NULL,
2181 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2182 .ct_owner = THIS_MODULE,
2183 };
2184
2185 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2186
2187 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2188
2189 CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2190 #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
2191 static struct target_core_alua_tg_pt_gp_attribute \
2192 target_core_alua_tg_pt_gp_##_name = \
2193 __CONFIGFS_EATTR(_name, _mode, \
2194 target_core_alua_tg_pt_gp_show_attr_##_name, \
2195 target_core_alua_tg_pt_gp_store_attr_##_name);
2196
2197 #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
2198 static struct target_core_alua_tg_pt_gp_attribute \
2199 target_core_alua_tg_pt_gp_##_name = \
2200 __CONFIGFS_EATTR_RO(_name, \
2201 target_core_alua_tg_pt_gp_show_attr_##_name);
2202
2203 /*
2204 * alua_access_state
2205 */
2206 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2207 struct t10_alua_tg_pt_gp *tg_pt_gp,
2208 char *page)
2209 {
2210 return sprintf(page, "%d\n",
2211 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2212 }
2213
2214 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2215 struct t10_alua_tg_pt_gp *tg_pt_gp,
2216 const char *page,
2217 size_t count)
2218 {
2219 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2220 unsigned long tmp;
2221 int new_state, ret;
2222
2223 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2224 pr_err("Unable to do implicit ALUA on non valid"
2225 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2226 return -EINVAL;
2227 }
2228 if (!(dev->dev_flags & DF_CONFIGURED)) {
2229 pr_err("Unable to set alua_access_state while device is"
2230 " not configured\n");
2231 return -ENODEV;
2232 }
2233
2234 ret = kstrtoul(page, 0, &tmp);
2235 if (ret < 0) {
2236 pr_err("Unable to extract new ALUA access state from"
2237 " %s\n", page);
2238 return ret;
2239 }
2240 new_state = (int)tmp;
2241
2242 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2243 pr_err("Unable to process implicit configfs ALUA"
2244 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
2245 return -EINVAL;
2246 }
2247 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2248 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2249 /* LBA DEPENDENT is only allowed with implicit ALUA */
2250 pr_err("Unable to process implicit configfs ALUA transition"
2251 " while explicit ALUA management is enabled\n");
2252 return -EINVAL;
2253 }
2254
2255 ret = core_alua_do_port_transition(tg_pt_gp, dev,
2256 NULL, NULL, new_state, 0);
2257 return (!ret) ? count : -EINVAL;
2258 }
2259
2260 SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2261
2262 /*
2263 * alua_access_status
2264 */
2265 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2266 struct t10_alua_tg_pt_gp *tg_pt_gp,
2267 char *page)
2268 {
2269 return sprintf(page, "%s\n",
2270 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2271 }
2272
2273 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2274 struct t10_alua_tg_pt_gp *tg_pt_gp,
2275 const char *page,
2276 size_t count)
2277 {
2278 unsigned long tmp;
2279 int new_status, ret;
2280
2281 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2282 pr_err("Unable to do set ALUA access status on non"
2283 " valid tg_pt_gp ID: %hu\n",
2284 tg_pt_gp->tg_pt_gp_valid_id);
2285 return -EINVAL;
2286 }
2287
2288 ret = kstrtoul(page, 0, &tmp);
2289 if (ret < 0) {
2290 pr_err("Unable to extract new ALUA access status"
2291 " from %s\n", page);
2292 return ret;
2293 }
2294 new_status = (int)tmp;
2295
2296 if ((new_status != ALUA_STATUS_NONE) &&
2297 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2298 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
2299 pr_err("Illegal ALUA access status: 0x%02x\n",
2300 new_status);
2301 return -EINVAL;
2302 }
2303
2304 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2305 return count;
2306 }
2307
2308 SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2309
2310 /*
2311 * alua_access_type
2312 */
2313 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2314 struct t10_alua_tg_pt_gp *tg_pt_gp,
2315 char *page)
2316 {
2317 return core_alua_show_access_type(tg_pt_gp, page);
2318 }
2319
2320 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2321 struct t10_alua_tg_pt_gp *tg_pt_gp,
2322 const char *page,
2323 size_t count)
2324 {
2325 return core_alua_store_access_type(tg_pt_gp, page, count);
2326 }
2327
2328 SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2329
2330 /*
2331 * alua_supported_states
2332 */
2333
2334 #define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit) \
2335 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2336 struct t10_alua_tg_pt_gp *t, char *p) \
2337 { \
2338 return sprintf(p, "%d\n", !!(t->_var & _bit)); \
2339 }
2340
2341 #define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit) \
2342 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2343 struct t10_alua_tg_pt_gp *t, const char *p, size_t c) \
2344 { \
2345 unsigned long tmp; \
2346 int ret; \
2347 \
2348 if (!t->tg_pt_gp_valid_id) { \
2349 pr_err("Unable to do set ##_name ALUA state on non" \
2350 " valid tg_pt_gp ID: %hu\n", \
2351 t->tg_pt_gp_valid_id); \
2352 return -EINVAL; \
2353 } \
2354 \
2355 ret = kstrtoul(p, 0, &tmp); \
2356 if (ret < 0) { \
2357 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2358 return -EINVAL; \
2359 } \
2360 if (tmp > 1) { \
2361 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2362 return -EINVAL; \
2363 } \
2364 if (!tmp) \
2365 t->_var |= _bit; \
2366 else \
2367 t->_var &= ~_bit; \
2368 \
2369 return c; \
2370 }
2371
2372 SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2373 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2374 SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2375 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2376 SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
2377
2378 SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2379 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2380 SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2381 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2382 SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
2383
2384 SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2385 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2386 SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2387 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2388 SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
2389
2390 SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2391 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2392 SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2393 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2394 SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
2395
2396 SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2397 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2398 SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2399 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2400 SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2401
2402 SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2403 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2404 SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2405 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2406 SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2407
2408 SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2409 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2410 SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2411 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2412 SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
2413
2414 /*
2415 * alua_write_metadata
2416 */
2417 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2418 struct t10_alua_tg_pt_gp *tg_pt_gp,
2419 char *page)
2420 {
2421 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2422 }
2423
2424 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2425 struct t10_alua_tg_pt_gp *tg_pt_gp,
2426 const char *page,
2427 size_t count)
2428 {
2429 unsigned long tmp;
2430 int ret;
2431
2432 ret = kstrtoul(page, 0, &tmp);
2433 if (ret < 0) {
2434 pr_err("Unable to extract alua_write_metadata\n");
2435 return ret;
2436 }
2437
2438 if ((tmp != 0) && (tmp != 1)) {
2439 pr_err("Illegal value for alua_write_metadata:"
2440 " %lu\n", tmp);
2441 return -EINVAL;
2442 }
2443 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2444
2445 return count;
2446 }
2447
2448 SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2449
2450
2451
2452 /*
2453 * nonop_delay_msecs
2454 */
2455 static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2456 struct t10_alua_tg_pt_gp *tg_pt_gp,
2457 char *page)
2458 {
2459 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2460
2461 }
2462
2463 static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2464 struct t10_alua_tg_pt_gp *tg_pt_gp,
2465 const char *page,
2466 size_t count)
2467 {
2468 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2469 }
2470
2471 SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2472
2473 /*
2474 * trans_delay_msecs
2475 */
2476 static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2477 struct t10_alua_tg_pt_gp *tg_pt_gp,
2478 char *page)
2479 {
2480 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2481 }
2482
2483 static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2484 struct t10_alua_tg_pt_gp *tg_pt_gp,
2485 const char *page,
2486 size_t count)
2487 {
2488 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2489 }
2490
2491 SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2492
2493 /*
2494 * implicit_trans_secs
2495 */
2496 static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
2497 struct t10_alua_tg_pt_gp *tg_pt_gp,
2498 char *page)
2499 {
2500 return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
2501 }
2502
2503 static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
2504 struct t10_alua_tg_pt_gp *tg_pt_gp,
2505 const char *page,
2506 size_t count)
2507 {
2508 return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
2509 }
2510
2511 SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
2512
2513 /*
2514 * preferred
2515 */
2516
2517 static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2518 struct t10_alua_tg_pt_gp *tg_pt_gp,
2519 char *page)
2520 {
2521 return core_alua_show_preferred_bit(tg_pt_gp, page);
2522 }
2523
2524 static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2525 struct t10_alua_tg_pt_gp *tg_pt_gp,
2526 const char *page,
2527 size_t count)
2528 {
2529 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2530 }
2531
2532 SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2533
2534 /*
2535 * tg_pt_gp_id
2536 */
2537 static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2538 struct t10_alua_tg_pt_gp *tg_pt_gp,
2539 char *page)
2540 {
2541 if (!tg_pt_gp->tg_pt_gp_valid_id)
2542 return 0;
2543
2544 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2545 }
2546
2547 static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2548 struct t10_alua_tg_pt_gp *tg_pt_gp,
2549 const char *page,
2550 size_t count)
2551 {
2552 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2553 unsigned long tg_pt_gp_id;
2554 int ret;
2555
2556 ret = kstrtoul(page, 0, &tg_pt_gp_id);
2557 if (ret < 0) {
2558 pr_err("kstrtoul() returned %d for"
2559 " tg_pt_gp_id\n", ret);
2560 return ret;
2561 }
2562 if (tg_pt_gp_id > 0x0000ffff) {
2563 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2564 " 0x0000ffff\n", tg_pt_gp_id);
2565 return -EINVAL;
2566 }
2567
2568 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2569 if (ret < 0)
2570 return -EINVAL;
2571
2572 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2573 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2574 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2575 tg_pt_gp->tg_pt_gp_id);
2576
2577 return count;
2578 }
2579
2580 SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2581
2582 /*
2583 * members
2584 */
2585 static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2586 struct t10_alua_tg_pt_gp *tg_pt_gp,
2587 char *page)
2588 {
2589 struct se_port *port;
2590 struct se_portal_group *tpg;
2591 struct se_lun *lun;
2592 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2593 ssize_t len = 0, cur_len;
2594 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2595
2596 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2597
2598 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2599 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2600 tg_pt_gp_mem_list) {
2601 port = tg_pt_gp_mem->tg_pt;
2602 tpg = port->sep_tpg;
2603 lun = port->sep_lun;
2604
2605 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2606 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2607 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2608 tpg->se_tpg_tfo->tpg_get_tag(tpg),
2609 config_item_name(&lun->lun_group.cg_item));
2610 cur_len++; /* Extra byte for NULL terminator */
2611
2612 if ((cur_len + len) > PAGE_SIZE) {
2613 pr_warn("Ran out of lu_gp_show_attr"
2614 "_members buffer\n");
2615 break;
2616 }
2617 memcpy(page+len, buf, cur_len);
2618 len += cur_len;
2619 }
2620 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2621
2622 return len;
2623 }
2624
2625 SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2626
2627 CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2628 tg_pt_gp_group);
2629
2630 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2631 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2632 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2633 &target_core_alua_tg_pt_gp_alua_access_type.attr,
2634 &target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2635 &target_core_alua_tg_pt_gp_alua_support_offline.attr,
2636 &target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2637 &target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2638 &target_core_alua_tg_pt_gp_alua_support_standby.attr,
2639 &target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2640 &target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
2641 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2642 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2643 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2644 &target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
2645 &target_core_alua_tg_pt_gp_preferred.attr,
2646 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2647 &target_core_alua_tg_pt_gp_members.attr,
2648 NULL,
2649 };
2650
2651 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2652 {
2653 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2654 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2655
2656 core_alua_free_tg_pt_gp(tg_pt_gp);
2657 }
2658
2659 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
2660 .release = target_core_alua_tg_pt_gp_release,
2661 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2662 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2663 };
2664
2665 static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2666 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2667 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2668 .ct_owner = THIS_MODULE,
2669 };
2670
2671 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2672
2673 /* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2674
2675 static struct config_group *target_core_alua_create_tg_pt_gp(
2676 struct config_group *group,
2677 const char *name)
2678 {
2679 struct t10_alua *alua = container_of(group, struct t10_alua,
2680 alua_tg_pt_gps_group);
2681 struct t10_alua_tg_pt_gp *tg_pt_gp;
2682 struct config_group *alua_tg_pt_gp_cg = NULL;
2683 struct config_item *alua_tg_pt_gp_ci = NULL;
2684
2685 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
2686 if (!tg_pt_gp)
2687 return NULL;
2688
2689 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2690 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2691
2692 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2693 &target_core_alua_tg_pt_gp_cit);
2694
2695 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2696 " Group: alua/tg_pt_gps/%s\n",
2697 config_item_name(alua_tg_pt_gp_ci));
2698
2699 return alua_tg_pt_gp_cg;
2700 }
2701
2702 static void target_core_alua_drop_tg_pt_gp(
2703 struct config_group *group,
2704 struct config_item *item)
2705 {
2706 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2707 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2708
2709 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2710 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2711 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
2712 /*
2713 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2714 * -> target_core_alua_tg_pt_gp_release().
2715 */
2716 config_item_put(item);
2717 }
2718
2719 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2720 .make_group = &target_core_alua_create_tg_pt_gp,
2721 .drop_item = &target_core_alua_drop_tg_pt_gp,
2722 };
2723
2724 static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2725 .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
2726 .ct_owner = THIS_MODULE,
2727 };
2728
2729 /* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2730
2731 /* Start functions for struct config_item_type target_core_alua_cit */
2732
2733 /*
2734 * target_core_alua_cit is a ConfigFS group that lives under
2735 * /sys/kernel/config/target/core/alua. There are default groups
2736 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2737 * target_core_alua_cit in target_core_init_configfs() below.
2738 */
2739 static struct config_item_type target_core_alua_cit = {
2740 .ct_item_ops = NULL,
2741 .ct_attrs = NULL,
2742 .ct_owner = THIS_MODULE,
2743 };
2744
2745 /* End functions for struct config_item_type target_core_alua_cit */
2746
2747 /* Start functions for struct config_item_type target_core_stat_cit */
2748
2749 static struct config_group *target_core_stat_mkdir(
2750 struct config_group *group,
2751 const char *name)
2752 {
2753 return ERR_PTR(-ENOSYS);
2754 }
2755
2756 static void target_core_stat_rmdir(
2757 struct config_group *group,
2758 struct config_item *item)
2759 {
2760 return;
2761 }
2762
2763 static struct configfs_group_operations target_core_stat_group_ops = {
2764 .make_group = &target_core_stat_mkdir,
2765 .drop_item = &target_core_stat_rmdir,
2766 };
2767
2768 static struct config_item_type target_core_stat_cit = {
2769 .ct_group_ops = &target_core_stat_group_ops,
2770 .ct_owner = THIS_MODULE,
2771 };
2772
2773 /* End functions for struct config_item_type target_core_stat_cit */
2774
2775 /* Start functions for struct config_item_type target_core_hba_cit */
2776
2777 static struct config_group *target_core_make_subdev(
2778 struct config_group *group,
2779 const char *name)
2780 {
2781 struct t10_alua_tg_pt_gp *tg_pt_gp;
2782 struct se_subsystem_api *t;
2783 struct config_item *hba_ci = &group->cg_item;
2784 struct se_hba *hba = item_to_hba(hba_ci);
2785 struct se_device *dev;
2786 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
2787 struct config_group *dev_stat_grp = NULL;
2788 int errno = -ENOMEM, ret;
2789
2790 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2791 if (ret)
2792 return ERR_PTR(ret);
2793 /*
2794 * Locate the struct se_subsystem_api from parent's struct se_hba.
2795 */
2796 t = hba->transport;
2797
2798 dev = target_alloc_device(hba, name);
2799 if (!dev)
2800 goto out_unlock;
2801
2802 dev_cg = &dev->dev_group;
2803
2804 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
2805 GFP_KERNEL);
2806 if (!dev_cg->default_groups)
2807 goto out_free_device;
2808
2809 config_group_init_type_name(dev_cg, name, &target_core_dev_cit);
2810 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
2811 &target_core_dev_attrib_cit);
2812 config_group_init_type_name(&dev->dev_pr_group, "pr",
2813 &target_core_dev_pr_cit);
2814 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
2815 &target_core_dev_wwn_cit);
2816 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
2817 "alua", &target_core_alua_tg_pt_gps_cit);
2818 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
2819 "statistics", &target_core_stat_cit);
2820
2821 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2822 dev_cg->default_groups[1] = &dev->dev_pr_group;
2823 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2824 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2825 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
2826 dev_cg->default_groups[5] = NULL;
2827 /*
2828 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2829 */
2830 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
2831 if (!tg_pt_gp)
2832 goto out_free_dev_cg_default_groups;
2833 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2834
2835 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2836 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2837 GFP_KERNEL);
2838 if (!tg_pt_gp_cg->default_groups) {
2839 pr_err("Unable to allocate tg_pt_gp_cg->"
2840 "default_groups\n");
2841 goto out_free_tg_pt_gp;
2842 }
2843
2844 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2845 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2846 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2847 tg_pt_gp_cg->default_groups[1] = NULL;
2848 /*
2849 * Add core/$HBA/$DEV/statistics/ default groups
2850 */
2851 dev_stat_grp = &dev->dev_stat_grps.stat_group;
2852 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
2853 GFP_KERNEL);
2854 if (!dev_stat_grp->default_groups) {
2855 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
2856 goto out_free_tg_pt_gp_cg_default_groups;
2857 }
2858 target_stat_setup_dev_default_groups(dev);
2859
2860 mutex_unlock(&hba->hba_access_mutex);
2861 return dev_cg;
2862
2863 out_free_tg_pt_gp_cg_default_groups:
2864 kfree(tg_pt_gp_cg->default_groups);
2865 out_free_tg_pt_gp:
2866 core_alua_free_tg_pt_gp(tg_pt_gp);
2867 out_free_dev_cg_default_groups:
2868 kfree(dev_cg->default_groups);
2869 out_free_device:
2870 target_free_device(dev);
2871 out_unlock:
2872 mutex_unlock(&hba->hba_access_mutex);
2873 return ERR_PTR(errno);
2874 }
2875
2876 static void target_core_drop_subdev(
2877 struct config_group *group,
2878 struct config_item *item)
2879 {
2880 struct config_group *dev_cg = to_config_group(item);
2881 struct se_device *dev =
2882 container_of(dev_cg, struct se_device, dev_group);
2883 struct se_hba *hba;
2884 struct config_item *df_item;
2885 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
2886 int i;
2887
2888 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
2889
2890 mutex_lock(&hba->hba_access_mutex);
2891
2892 dev_stat_grp = &dev->dev_stat_grps.stat_group;
2893 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2894 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2895 dev_stat_grp->default_groups[i] = NULL;
2896 config_item_put(df_item);
2897 }
2898 kfree(dev_stat_grp->default_groups);
2899
2900 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2901 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2902 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2903 tg_pt_gp_cg->default_groups[i] = NULL;
2904 config_item_put(df_item);
2905 }
2906 kfree(tg_pt_gp_cg->default_groups);
2907 /*
2908 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2909 * directly from target_core_alua_tg_pt_gp_release().
2910 */
2911 dev->t10_alua.default_tg_pt_gp = NULL;
2912
2913 for (i = 0; dev_cg->default_groups[i]; i++) {
2914 df_item = &dev_cg->default_groups[i]->cg_item;
2915 dev_cg->default_groups[i] = NULL;
2916 config_item_put(df_item);
2917 }
2918 /*
2919 * se_dev is released from target_core_dev_item_ops->release()
2920 */
2921 config_item_put(item);
2922 mutex_unlock(&hba->hba_access_mutex);
2923 }
2924
2925 static struct configfs_group_operations target_core_hba_group_ops = {
2926 .make_group = target_core_make_subdev,
2927 .drop_item = target_core_drop_subdev,
2928 };
2929
2930 CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2931 #define SE_HBA_ATTR(_name, _mode) \
2932 static struct target_core_hba_attribute \
2933 target_core_hba_##_name = \
2934 __CONFIGFS_EATTR(_name, _mode, \
2935 target_core_hba_show_attr_##_name, \
2936 target_core_hba_store_attr_##_name);
2937
2938 #define SE_HBA_ATTR_RO(_name) \
2939 static struct target_core_hba_attribute \
2940 target_core_hba_##_name = \
2941 __CONFIGFS_EATTR_RO(_name, \
2942 target_core_hba_show_attr_##_name);
2943
2944 static ssize_t target_core_hba_show_attr_hba_info(
2945 struct se_hba *hba,
2946 char *page)
2947 {
2948 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2949 hba->hba_id, hba->transport->name,
2950 TARGET_CORE_CONFIGFS_VERSION);
2951 }
2952
2953 SE_HBA_ATTR_RO(hba_info);
2954
2955 static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2956 char *page)
2957 {
2958 int hba_mode = 0;
2959
2960 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2961 hba_mode = 1;
2962
2963 return sprintf(page, "%d\n", hba_mode);
2964 }
2965
2966 static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2967 const char *page, size_t count)
2968 {
2969 struct se_subsystem_api *transport = hba->transport;
2970 unsigned long mode_flag;
2971 int ret;
2972
2973 if (transport->pmode_enable_hba == NULL)
2974 return -EINVAL;
2975
2976 ret = kstrtoul(page, 0, &mode_flag);
2977 if (ret < 0) {
2978 pr_err("Unable to extract hba mode flag: %d\n", ret);
2979 return ret;
2980 }
2981
2982 if (hba->dev_count) {
2983 pr_err("Unable to set hba_mode with active devices\n");
2984 return -EINVAL;
2985 }
2986
2987 ret = transport->pmode_enable_hba(hba, mode_flag);
2988 if (ret < 0)
2989 return -EINVAL;
2990 if (ret > 0)
2991 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2992 else if (ret == 0)
2993 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2994
2995 return count;
2996 }
2997
2998 SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2999
3000 CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
3001
3002 static void target_core_hba_release(struct config_item *item)
3003 {
3004 struct se_hba *hba = container_of(to_config_group(item),
3005 struct se_hba, hba_group);
3006 core_delete_hba(hba);
3007 }
3008
3009 static struct configfs_attribute *target_core_hba_attrs[] = {
3010 &target_core_hba_hba_info.attr,
3011 &target_core_hba_hba_mode.attr,
3012 NULL,
3013 };
3014
3015 static struct configfs_item_operations target_core_hba_item_ops = {
3016 .release = target_core_hba_release,
3017 .show_attribute = target_core_hba_attr_show,
3018 .store_attribute = target_core_hba_attr_store,
3019 };
3020
3021 static struct config_item_type target_core_hba_cit = {
3022 .ct_item_ops = &target_core_hba_item_ops,
3023 .ct_group_ops = &target_core_hba_group_ops,
3024 .ct_attrs = target_core_hba_attrs,
3025 .ct_owner = THIS_MODULE,
3026 };
3027
3028 static struct config_group *target_core_call_addhbatotarget(
3029 struct config_group *group,
3030 const char *name)
3031 {
3032 char *se_plugin_str, *str, *str2;
3033 struct se_hba *hba;
3034 char buf[TARGET_CORE_NAME_MAX_LEN];
3035 unsigned long plugin_dep_id = 0;
3036 int ret;
3037
3038 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
3039 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
3040 pr_err("Passed *name strlen(): %d exceeds"
3041 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3042 TARGET_CORE_NAME_MAX_LEN);
3043 return ERR_PTR(-ENAMETOOLONG);
3044 }
3045 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3046
3047 str = strstr(buf, "_");
3048 if (!str) {
3049 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3050 return ERR_PTR(-EINVAL);
3051 }
3052 se_plugin_str = buf;
3053 /*
3054 * Special case for subsystem plugins that have "_" in their names.
3055 * Namely rd_direct and rd_mcp..
3056 */
3057 str2 = strstr(str+1, "_");
3058 if (str2) {
3059 *str2 = '\0'; /* Terminate for *se_plugin_str */
3060 str2++; /* Skip to start of plugin dependent ID */
3061 str = str2;
3062 } else {
3063 *str = '\0'; /* Terminate for *se_plugin_str */
3064 str++; /* Skip to start of plugin dependent ID */
3065 }
3066
3067 ret = kstrtoul(str, 0, &plugin_dep_id);
3068 if (ret < 0) {
3069 pr_err("kstrtoul() returned %d for"
3070 " plugin_dep_id\n", ret);
3071 return ERR_PTR(ret);
3072 }
3073 /*
3074 * Load up TCM subsystem plugins if they have not already been loaded.
3075 */
3076 transport_subsystem_check_init();
3077
3078 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3079 if (IS_ERR(hba))
3080 return ERR_CAST(hba);
3081
3082 config_group_init_type_name(&hba->hba_group, name,
3083 &target_core_hba_cit);
3084
3085 return &hba->hba_group;
3086 }
3087
3088 static void target_core_call_delhbafromtarget(
3089 struct config_group *group,
3090 struct config_item *item)
3091 {
3092 /*
3093 * core_delete_hba() is called from target_core_hba_item_ops->release()
3094 * -> target_core_hba_release()
3095 */
3096 config_item_put(item);
3097 }
3098
3099 static struct configfs_group_operations target_core_group_ops = {
3100 .make_group = target_core_call_addhbatotarget,
3101 .drop_item = target_core_call_delhbafromtarget,
3102 };
3103
3104 static struct config_item_type target_core_cit = {
3105 .ct_item_ops = NULL,
3106 .ct_group_ops = &target_core_group_ops,
3107 .ct_attrs = NULL,
3108 .ct_owner = THIS_MODULE,
3109 };
3110
3111 /* Stop functions for struct config_item_type target_core_hba_cit */
3112
3113 static int __init target_core_init_configfs(void)
3114 {
3115 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3116 struct config_group *lu_gp_cg = NULL;
3117 struct configfs_subsystem *subsys;
3118 struct t10_alua_lu_gp *lu_gp;
3119 int ret;
3120
3121 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3122 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3123 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3124
3125 subsys = target_core_subsystem[0];
3126 config_group_init(&subsys->su_group);
3127 mutex_init(&subsys->su_mutex);
3128
3129 ret = init_se_kmem_caches();
3130 if (ret < 0)
3131 return ret;
3132 /*
3133 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3134 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3135 */
3136 target_cg = &subsys->su_group;
3137 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3138 GFP_KERNEL);
3139 if (!target_cg->default_groups) {
3140 pr_err("Unable to allocate target_cg->default_groups\n");
3141 ret = -ENOMEM;
3142 goto out_global;
3143 }
3144
3145 config_group_init_type_name(&target_core_hbagroup,
3146 "core", &target_core_cit);
3147 target_cg->default_groups[0] = &target_core_hbagroup;
3148 target_cg->default_groups[1] = NULL;
3149 /*
3150 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3151 */
3152 hba_cg = &target_core_hbagroup;
3153 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3154 GFP_KERNEL);
3155 if (!hba_cg->default_groups) {
3156 pr_err("Unable to allocate hba_cg->default_groups\n");
3157 ret = -ENOMEM;
3158 goto out_global;
3159 }
3160 config_group_init_type_name(&alua_group,
3161 "alua", &target_core_alua_cit);
3162 hba_cg->default_groups[0] = &alua_group;
3163 hba_cg->default_groups[1] = NULL;
3164 /*
3165 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3166 * groups under /sys/kernel/config/target/core/alua/
3167 */
3168 alua_cg = &alua_group;
3169 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3170 GFP_KERNEL);
3171 if (!alua_cg->default_groups) {
3172 pr_err("Unable to allocate alua_cg->default_groups\n");
3173 ret = -ENOMEM;
3174 goto out_global;
3175 }
3176
3177 config_group_init_type_name(&alua_lu_gps_group,
3178 "lu_gps", &target_core_alua_lu_gps_cit);
3179 alua_cg->default_groups[0] = &alua_lu_gps_group;
3180 alua_cg->default_groups[1] = NULL;
3181 /*
3182 * Add core/alua/lu_gps/default_lu_gp
3183 */
3184 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
3185 if (IS_ERR(lu_gp)) {
3186 ret = -ENOMEM;
3187 goto out_global;
3188 }
3189
3190 lu_gp_cg = &alua_lu_gps_group;
3191 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
3192 GFP_KERNEL);
3193 if (!lu_gp_cg->default_groups) {
3194 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
3195 ret = -ENOMEM;
3196 goto out_global;
3197 }
3198
3199 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3200 &target_core_alua_lu_gp_cit);
3201 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3202 lu_gp_cg->default_groups[1] = NULL;
3203 default_lu_gp = lu_gp;
3204 /*
3205 * Register the target_core_mod subsystem with configfs.
3206 */
3207 ret = configfs_register_subsystem(subsys);
3208 if (ret < 0) {
3209 pr_err("Error %d while registering subsystem %s\n",
3210 ret, subsys->su_group.cg_item.ci_namebuf);
3211 goto out_global;
3212 }
3213 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3214 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3215 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3216 /*
3217 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3218 */
3219 ret = rd_module_init();
3220 if (ret < 0)
3221 goto out;
3222
3223 ret = core_dev_setup_virtual_lun0();
3224 if (ret < 0)
3225 goto out;
3226
3227 ret = target_xcopy_setup_pt();
3228 if (ret < 0)
3229 goto out;
3230
3231 return 0;
3232
3233 out:
3234 configfs_unregister_subsystem(subsys);
3235 core_dev_release_virtual_lun0();
3236 rd_module_exit();
3237 out_global:
3238 if (default_lu_gp) {
3239 core_alua_free_lu_gp(default_lu_gp);
3240 default_lu_gp = NULL;
3241 }
3242 if (lu_gp_cg)
3243 kfree(lu_gp_cg->default_groups);
3244 if (alua_cg)
3245 kfree(alua_cg->default_groups);
3246 if (hba_cg)
3247 kfree(hba_cg->default_groups);
3248 kfree(target_cg->default_groups);
3249 release_se_kmem_caches();
3250 return ret;
3251 }
3252
3253 static void __exit target_core_exit_configfs(void)
3254 {
3255 struct configfs_subsystem *subsys;
3256 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3257 struct config_item *item;
3258 int i;
3259
3260 subsys = target_core_subsystem[0];
3261
3262 lu_gp_cg = &alua_lu_gps_group;
3263 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3264 item = &lu_gp_cg->default_groups[i]->cg_item;
3265 lu_gp_cg->default_groups[i] = NULL;
3266 config_item_put(item);
3267 }
3268 kfree(lu_gp_cg->default_groups);
3269 lu_gp_cg->default_groups = NULL;
3270
3271 alua_cg = &alua_group;
3272 for (i = 0; alua_cg->default_groups[i]; i++) {
3273 item = &alua_cg->default_groups[i]->cg_item;
3274 alua_cg->default_groups[i] = NULL;
3275 config_item_put(item);
3276 }
3277 kfree(alua_cg->default_groups);
3278 alua_cg->default_groups = NULL;
3279
3280 hba_cg = &target_core_hbagroup;
3281 for (i = 0; hba_cg->default_groups[i]; i++) {
3282 item = &hba_cg->default_groups[i]->cg_item;
3283 hba_cg->default_groups[i] = NULL;
3284 config_item_put(item);
3285 }
3286 kfree(hba_cg->default_groups);
3287 hba_cg->default_groups = NULL;
3288 /*
3289 * We expect subsys->su_group.default_groups to be released
3290 * by configfs subsystem provider logic..
3291 */
3292 configfs_unregister_subsystem(subsys);
3293 kfree(subsys->su_group.default_groups);
3294
3295 core_alua_free_lu_gp(default_lu_gp);
3296 default_lu_gp = NULL;
3297
3298 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3299 " Infrastructure\n");
3300
3301 core_dev_release_virtual_lun0();
3302 rd_module_exit();
3303 target_xcopy_release_pt();
3304 release_se_kmem_caches();
3305 }
3306
3307 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3308 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3309 MODULE_LICENSE("GPL");
3310
3311 module_init(target_core_init_configfs);
3312 module_exit(target_core_exit_configfs);