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