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