Merge tag 'ras_for_3.20' of git://git.kernel.org/pub/scm/linux/kernel/git/bp/bp into...
[cascardo/linux.git] / Documentation / target / tcm_mod_builder.py
1 #!/usr/bin/python
2 # The TCM v4 multi-protocol fabric module generation script for drivers/target/$NEW_MOD
3 #
4 # Copyright (c) 2010 Rising Tide Systems
5 # Copyright (c) 2010 Linux-iSCSI.org
6 #
7 # Author: nab@kernel.org
8 #
9 import os, sys
10 import subprocess as sub
11 import string
12 import re
13 import optparse
14
15 tcm_dir = ""
16
17 fabric_ops = []
18 fabric_mod_dir = ""
19 fabric_mod_port = ""
20 fabric_mod_init_port = ""
21
22 def tcm_mod_err(msg):
23         print msg
24         sys.exit(1)
25
26 def tcm_mod_create_module_subdir(fabric_mod_dir_var):
27
28         if os.path.isdir(fabric_mod_dir_var) == True:
29                 return 1
30
31         print "Creating fabric_mod_dir: " + fabric_mod_dir_var
32         ret = os.mkdir(fabric_mod_dir_var)
33         if ret:
34                 tcm_mod_err("Unable to mkdir " + fabric_mod_dir_var)
35
36         return
37
38 def tcm_mod_build_FC_include(fabric_mod_dir_var, fabric_mod_name):
39         global fabric_mod_port
40         global fabric_mod_init_port
41         buf = ""
42
43         f = fabric_mod_dir_var + "/" + fabric_mod_name + "_base.h"
44         print "Writing file: " + f
45
46         p = open(f, 'w');
47         if not p:
48                 tcm_mod_err("Unable to open file: " + f)
49
50         buf = "#define " + fabric_mod_name.upper() + "_VERSION  \"v0.1\"\n"
51         buf += "#define " + fabric_mod_name.upper() + "_NAMELEN 32\n"
52         buf += "\n"
53         buf += "struct " + fabric_mod_name + "_nacl {\n"
54         buf += "        /* Binary World Wide unique Port Name for FC Initiator Nport */\n"
55         buf += "        u64 nport_wwpn;\n"
56         buf += "        /* ASCII formatted WWPN for FC Initiator Nport */\n"
57         buf += "        char nport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
58         buf += "        /* Returned by " + fabric_mod_name + "_make_nodeacl() */\n"
59         buf += "        struct se_node_acl se_node_acl;\n"
60         buf += "};\n"
61         buf += "\n"
62         buf += "struct " + fabric_mod_name + "_tpg {\n"
63         buf += "        /* FC lport target portal group tag for TCM */\n"
64         buf += "        u16 lport_tpgt;\n"
65         buf += "        /* Pointer back to " + fabric_mod_name + "_lport */\n"
66         buf += "        struct " + fabric_mod_name + "_lport *lport;\n"
67         buf += "        /* Returned by " + fabric_mod_name + "_make_tpg() */\n"
68         buf += "        struct se_portal_group se_tpg;\n"
69         buf += "};\n"
70         buf += "\n"
71         buf += "struct " + fabric_mod_name + "_lport {\n"
72         buf += "        /* SCSI protocol the lport is providing */\n"
73         buf += "        u8 lport_proto_id;\n"
74         buf += "        /* Binary World Wide unique Port Name for FC Target Lport */\n"
75         buf += "        u64 lport_wwpn;\n"
76         buf += "        /* ASCII formatted WWPN for FC Target Lport */\n"
77         buf += "        char lport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
78         buf += "        /* Returned by " + fabric_mod_name + "_make_lport() */\n"
79         buf += "        struct se_wwn lport_wwn;\n"
80         buf += "};\n"
81
82         ret = p.write(buf)
83         if ret:
84                 tcm_mod_err("Unable to write f: " + f)
85
86         p.close()
87
88         fabric_mod_port = "lport"
89         fabric_mod_init_port = "nport"
90
91         return
92
93 def tcm_mod_build_SAS_include(fabric_mod_dir_var, fabric_mod_name):
94         global fabric_mod_port
95         global fabric_mod_init_port
96         buf = ""
97
98         f = fabric_mod_dir_var + "/" + fabric_mod_name + "_base.h"
99         print "Writing file: " + f
100
101         p = open(f, 'w');
102         if not p:
103                 tcm_mod_err("Unable to open file: " + f)
104
105         buf = "#define " + fabric_mod_name.upper() + "_VERSION  \"v0.1\"\n"
106         buf += "#define " + fabric_mod_name.upper() + "_NAMELEN 32\n"
107         buf += "\n"
108         buf += "struct " + fabric_mod_name + "_nacl {\n"
109         buf += "        /* Binary World Wide unique Port Name for SAS Initiator port */\n"
110         buf += "        u64 iport_wwpn;\n"
111         buf += "        /* ASCII formatted WWPN for Sas Initiator port */\n"
112         buf += "        char iport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
113         buf += "        /* Returned by " + fabric_mod_name + "_make_nodeacl() */\n"
114         buf += "        struct se_node_acl se_node_acl;\n"
115         buf += "};\n\n"
116         buf += "struct " + fabric_mod_name + "_tpg {\n"
117         buf += "        /* SAS port target portal group tag for TCM */\n"
118         buf += "        u16 tport_tpgt;\n"
119         buf += "        /* Pointer back to " + fabric_mod_name + "_tport */\n"
120         buf += "        struct " + fabric_mod_name + "_tport *tport;\n"
121         buf += "        /* Returned by " + fabric_mod_name + "_make_tpg() */\n"
122         buf += "        struct se_portal_group se_tpg;\n"
123         buf += "};\n\n"
124         buf += "struct " + fabric_mod_name + "_tport {\n"
125         buf += "        /* SCSI protocol the tport is providing */\n"
126         buf += "        u8 tport_proto_id;\n"
127         buf += "        /* Binary World Wide unique Port Name for SAS Target port */\n"
128         buf += "        u64 tport_wwpn;\n"
129         buf += "        /* ASCII formatted WWPN for SAS Target port */\n"
130         buf += "        char tport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
131         buf += "        /* Returned by " + fabric_mod_name + "_make_tport() */\n"
132         buf += "        struct se_wwn tport_wwn;\n"
133         buf += "};\n"
134
135         ret = p.write(buf)
136         if ret:
137                 tcm_mod_err("Unable to write f: " + f)
138
139         p.close()
140
141         fabric_mod_port = "tport"
142         fabric_mod_init_port = "iport"
143
144         return
145
146 def tcm_mod_build_iSCSI_include(fabric_mod_dir_var, fabric_mod_name):
147         global fabric_mod_port
148         global fabric_mod_init_port
149         buf = ""
150
151         f = fabric_mod_dir_var + "/" + fabric_mod_name + "_base.h"
152         print "Writing file: " + f
153
154         p = open(f, 'w');
155         if not p:
156                 tcm_mod_err("Unable to open file: " + f)
157
158         buf = "#define " + fabric_mod_name.upper() + "_VERSION  \"v0.1\"\n"
159         buf += "#define " + fabric_mod_name.upper() + "_NAMELEN 32\n"
160         buf += "\n"
161         buf += "struct " + fabric_mod_name + "_nacl {\n"
162         buf += "        /* ASCII formatted InitiatorName */\n"
163         buf += "        char iport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
164         buf += "        /* Returned by " + fabric_mod_name + "_make_nodeacl() */\n"
165         buf += "        struct se_node_acl se_node_acl;\n"
166         buf += "};\n\n"
167         buf += "struct " + fabric_mod_name + "_tpg {\n"
168         buf += "        /* iSCSI target portal group tag for TCM */\n"
169         buf += "        u16 tport_tpgt;\n"
170         buf += "        /* Pointer back to " + fabric_mod_name + "_tport */\n"
171         buf += "        struct " + fabric_mod_name + "_tport *tport;\n"
172         buf += "        /* Returned by " + fabric_mod_name + "_make_tpg() */\n"
173         buf += "        struct se_portal_group se_tpg;\n"
174         buf += "};\n\n"
175         buf += "struct " + fabric_mod_name + "_tport {\n"
176         buf += "        /* SCSI protocol the tport is providing */\n"
177         buf += "        u8 tport_proto_id;\n"
178         buf += "        /* ASCII formatted TargetName for IQN */\n"
179         buf += "        char tport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
180         buf += "        /* Returned by " + fabric_mod_name + "_make_tport() */\n"
181         buf += "        struct se_wwn tport_wwn;\n"
182         buf += "};\n"
183
184         ret = p.write(buf)
185         if ret:
186                 tcm_mod_err("Unable to write f: " + f)
187
188         p.close()
189
190         fabric_mod_port = "tport"
191         fabric_mod_init_port = "iport"
192
193         return
194
195 def tcm_mod_build_base_includes(proto_ident, fabric_mod_dir_val, fabric_mod_name):
196
197         if proto_ident == "FC":
198                 tcm_mod_build_FC_include(fabric_mod_dir_val, fabric_mod_name)
199         elif proto_ident == "SAS":
200                 tcm_mod_build_SAS_include(fabric_mod_dir_val, fabric_mod_name)
201         elif proto_ident == "iSCSI":
202                 tcm_mod_build_iSCSI_include(fabric_mod_dir_val, fabric_mod_name)
203         else:
204                 print "Unsupported proto_ident: " + proto_ident
205                 sys.exit(1)
206
207         return
208
209 def tcm_mod_build_configfs(proto_ident, fabric_mod_dir_var, fabric_mod_name):
210         buf = ""
211
212         f = fabric_mod_dir_var + "/" + fabric_mod_name + "_configfs.c"
213         print "Writing file: " + f
214
215         p = open(f, 'w');
216         if not p:
217                 tcm_mod_err("Unable to open file: " + f)
218
219         buf = "#include <linux/module.h>\n"
220         buf += "#include <linux/moduleparam.h>\n"
221         buf += "#include <linux/version.h>\n"
222         buf += "#include <generated/utsrelease.h>\n"
223         buf += "#include <linux/utsname.h>\n"
224         buf += "#include <linux/init.h>\n"
225         buf += "#include <linux/slab.h>\n"
226         buf += "#include <linux/kthread.h>\n"
227         buf += "#include <linux/types.h>\n"
228         buf += "#include <linux/string.h>\n"
229         buf += "#include <linux/configfs.h>\n"
230         buf += "#include <linux/ctype.h>\n"
231         buf += "#include <asm/unaligned.h>\n\n"
232         buf += "#include <target/target_core_base.h>\n"
233         buf += "#include <target/target_core_fabric.h>\n"
234         buf += "#include <target/target_core_fabric_configfs.h>\n"
235         buf += "#include <target/target_core_configfs.h>\n"
236         buf += "#include <target/configfs_macros.h>\n\n"
237         buf += "#include \"" + fabric_mod_name + "_base.h\"\n"
238         buf += "#include \"" + fabric_mod_name + "_fabric.h\"\n\n"
239
240         buf += "/* Local pointer to allocated TCM configfs fabric module */\n"
241         buf += "struct target_fabric_configfs *" + fabric_mod_name + "_fabric_configfs;\n\n"
242
243         buf += "static struct se_node_acl *" + fabric_mod_name + "_make_nodeacl(\n"
244         buf += "        struct se_portal_group *se_tpg,\n"
245         buf += "        struct config_group *group,\n"
246         buf += "        const char *name)\n"
247         buf += "{\n"
248         buf += "        struct se_node_acl *se_nacl, *se_nacl_new;\n"
249         buf += "        struct " + fabric_mod_name + "_nacl *nacl;\n"
250
251         if proto_ident == "FC" or proto_ident == "SAS":
252                 buf += "        u64 wwpn = 0;\n"
253
254         buf += "        u32 nexus_depth;\n\n"
255         buf += "        /* " + fabric_mod_name + "_parse_wwn(name, &wwpn, 1) < 0)\n"
256         buf += "                return ERR_PTR(-EINVAL); */\n"
257         buf += "        se_nacl_new = " + fabric_mod_name + "_alloc_fabric_acl(se_tpg);\n"
258         buf += "        if (!se_nacl_new)\n"
259         buf += "                return ERR_PTR(-ENOMEM);\n"
260         buf += "//#warning FIXME: Hardcoded nexus depth in " + fabric_mod_name + "_make_nodeacl()\n"
261         buf += "        nexus_depth = 1;\n"
262         buf += "        /*\n"
263         buf += "         * se_nacl_new may be released by core_tpg_add_initiator_node_acl()\n"
264         buf += "         * when converting a NodeACL from demo mode -> explict\n"
265         buf += "         */\n"
266         buf += "        se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,\n"
267         buf += "                                name, nexus_depth);\n"
268         buf += "        if (IS_ERR(se_nacl)) {\n"
269         buf += "                " + fabric_mod_name + "_release_fabric_acl(se_tpg, se_nacl_new);\n"
270         buf += "                return se_nacl;\n"
271         buf += "        }\n"
272         buf += "        /*\n"
273         buf += "         * Locate our struct " + fabric_mod_name + "_nacl and set the FC Nport WWPN\n"
274         buf += "         */\n"
275         buf += "        nacl = container_of(se_nacl, struct " + fabric_mod_name + "_nacl, se_node_acl);\n"
276
277         if proto_ident == "FC" or proto_ident == "SAS":
278                 buf += "        nacl->" + fabric_mod_init_port + "_wwpn = wwpn;\n"
279
280         buf += "        /* " + fabric_mod_name + "_format_wwn(&nacl->" + fabric_mod_init_port + "_name[0], " + fabric_mod_name.upper() + "_NAMELEN, wwpn); */\n\n"
281         buf += "        return se_nacl;\n"
282         buf += "}\n\n"
283         buf += "static void " + fabric_mod_name + "_drop_nodeacl(struct se_node_acl *se_acl)\n"
284         buf += "{\n"
285         buf += "        struct " + fabric_mod_name + "_nacl *nacl = container_of(se_acl,\n"
286         buf += "                                struct " + fabric_mod_name + "_nacl, se_node_acl);\n"
287         buf += "        core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);\n"
288         buf += "        kfree(nacl);\n"
289         buf += "}\n\n"
290
291         buf += "static struct se_portal_group *" + fabric_mod_name + "_make_tpg(\n"
292         buf += "        struct se_wwn *wwn,\n"
293         buf += "        struct config_group *group,\n"
294         buf += "        const char *name)\n"
295         buf += "{\n"
296         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + "*" + fabric_mod_port + " = container_of(wwn,\n"
297         buf += "                        struct " + fabric_mod_name + "_" + fabric_mod_port + ", " + fabric_mod_port + "_wwn);\n\n"
298         buf += "        struct " + fabric_mod_name + "_tpg *tpg;\n"
299         buf += "        unsigned long tpgt;\n"
300         buf += "        int ret;\n\n"
301         buf += "        if (strstr(name, \"tpgt_\") != name)\n"
302         buf += "                return ERR_PTR(-EINVAL);\n"
303         buf += "        if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)\n"
304         buf += "                return ERR_PTR(-EINVAL);\n\n"
305         buf += "        tpg = kzalloc(sizeof(struct " + fabric_mod_name + "_tpg), GFP_KERNEL);\n"
306         buf += "        if (!tpg) {\n"
307         buf += "                printk(KERN_ERR \"Unable to allocate struct " + fabric_mod_name + "_tpg\");\n"
308         buf += "                return ERR_PTR(-ENOMEM);\n"
309         buf += "        }\n"
310         buf += "        tpg->" + fabric_mod_port + " = " + fabric_mod_port + ";\n"
311         buf += "        tpg->" + fabric_mod_port + "_tpgt = tpgt;\n\n"
312         buf += "        ret = core_tpg_register(&" + fabric_mod_name + "_fabric_configfs->tf_ops, wwn,\n"
313         buf += "                                &tpg->se_tpg, (void *)tpg,\n"
314         buf += "                                TRANSPORT_TPG_TYPE_NORMAL);\n"
315         buf += "        if (ret < 0) {\n"
316         buf += "                kfree(tpg);\n"
317         buf += "                return NULL;\n"
318         buf += "        }\n"
319         buf += "        return &tpg->se_tpg;\n"
320         buf += "}\n\n"
321         buf += "static void " + fabric_mod_name + "_drop_tpg(struct se_portal_group *se_tpg)\n"
322         buf += "{\n"
323         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
324         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n\n"
325         buf += "        core_tpg_deregister(se_tpg);\n"
326         buf += "        kfree(tpg);\n"
327         buf += "}\n\n"
328
329         buf += "static struct se_wwn *" + fabric_mod_name + "_make_" + fabric_mod_port + "(\n"
330         buf += "        struct target_fabric_configfs *tf,\n"
331         buf += "        struct config_group *group,\n"
332         buf += "        const char *name)\n"
333         buf += "{\n"
334         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + ";\n"
335
336         if proto_ident == "FC" or proto_ident == "SAS":
337                 buf += "        u64 wwpn = 0;\n\n"
338
339         buf += "        /* if (" + fabric_mod_name + "_parse_wwn(name, &wwpn, 1) < 0)\n"
340         buf += "                return ERR_PTR(-EINVAL); */\n\n"
341         buf += "        " + fabric_mod_port + " = kzalloc(sizeof(struct " + fabric_mod_name + "_" + fabric_mod_port + "), GFP_KERNEL);\n"
342         buf += "        if (!" + fabric_mod_port + ") {\n"
343         buf += "                printk(KERN_ERR \"Unable to allocate struct " + fabric_mod_name + "_" + fabric_mod_port + "\");\n"
344         buf += "                return ERR_PTR(-ENOMEM);\n"
345         buf += "        }\n"
346
347         if proto_ident == "FC" or proto_ident == "SAS":
348                 buf += "        " + fabric_mod_port + "->" + fabric_mod_port + "_wwpn = wwpn;\n"
349
350         buf += "        /* " + fabric_mod_name + "_format_wwn(&" + fabric_mod_port + "->" + fabric_mod_port + "_name[0], " + fabric_mod_name.upper() + "_NAMELEN, wwpn); */\n\n"
351         buf += "        return &" + fabric_mod_port + "->" + fabric_mod_port + "_wwn;\n"
352         buf += "}\n\n"
353         buf += "static void " + fabric_mod_name + "_drop_" + fabric_mod_port + "(struct se_wwn *wwn)\n"
354         buf += "{\n"
355         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = container_of(wwn,\n"
356         buf += "                                struct " + fabric_mod_name + "_" + fabric_mod_port + ", " + fabric_mod_port + "_wwn);\n"
357         buf += "        kfree(" + fabric_mod_port + ");\n"
358         buf += "}\n\n"
359         buf += "static ssize_t " + fabric_mod_name + "_wwn_show_attr_version(\n"
360         buf += "        struct target_fabric_configfs *tf,\n"
361         buf += "        char *page)\n"
362         buf += "{\n"
363         buf += "        return sprintf(page, \"" + fabric_mod_name.upper() + " fabric module %s on %s/%s\"\n"
364         buf += "                \"on \"UTS_RELEASE\"\\n\", " + fabric_mod_name.upper() + "_VERSION, utsname()->sysname,\n"
365         buf += "                utsname()->machine);\n"
366         buf += "}\n\n"
367         buf += "TF_WWN_ATTR_RO(" + fabric_mod_name + ", version);\n\n"
368         buf += "static struct configfs_attribute *" + fabric_mod_name + "_wwn_attrs[] = {\n"
369         buf += "        &" + fabric_mod_name + "_wwn_version.attr,\n"
370         buf += "        NULL,\n"
371         buf += "};\n\n"
372
373         buf += "static struct target_core_fabric_ops " + fabric_mod_name + "_ops = {\n"
374         buf += "        .get_fabric_name                = " + fabric_mod_name + "_get_fabric_name,\n"
375         buf += "        .get_fabric_proto_ident         = " + fabric_mod_name + "_get_fabric_proto_ident,\n"
376         buf += "        .tpg_get_wwn                    = " + fabric_mod_name + "_get_fabric_wwn,\n"
377         buf += "        .tpg_get_tag                    = " + fabric_mod_name + "_get_tag,\n"
378         buf += "        .tpg_get_default_depth          = " + fabric_mod_name + "_get_default_depth,\n"
379         buf += "        .tpg_get_pr_transport_id        = " + fabric_mod_name + "_get_pr_transport_id,\n"
380         buf += "        .tpg_get_pr_transport_id_len    = " + fabric_mod_name + "_get_pr_transport_id_len,\n"
381         buf += "        .tpg_parse_pr_out_transport_id  = " + fabric_mod_name + "_parse_pr_out_transport_id,\n"
382         buf += "        .tpg_check_demo_mode            = " + fabric_mod_name + "_check_false,\n"
383         buf += "        .tpg_check_demo_mode_cache      = " + fabric_mod_name + "_check_true,\n"
384         buf += "        .tpg_check_demo_mode_write_protect = " + fabric_mod_name + "_check_true,\n"
385         buf += "        .tpg_check_prod_mode_write_protect = " + fabric_mod_name + "_check_false,\n"
386         buf += "        .tpg_alloc_fabric_acl           = " + fabric_mod_name + "_alloc_fabric_acl,\n"
387         buf += "        .tpg_release_fabric_acl         = " + fabric_mod_name + "_release_fabric_acl,\n"
388         buf += "        .tpg_get_inst_index             = " + fabric_mod_name + "_tpg_get_inst_index,\n"
389         buf += "        .release_cmd                    = " + fabric_mod_name + "_release_cmd,\n"
390         buf += "        .shutdown_session               = " + fabric_mod_name + "_shutdown_session,\n"
391         buf += "        .close_session                  = " + fabric_mod_name + "_close_session,\n"
392         buf += "        .sess_get_index                 = " + fabric_mod_name + "_sess_get_index,\n"
393         buf += "        .sess_get_initiator_sid         = NULL,\n"
394         buf += "        .write_pending                  = " + fabric_mod_name + "_write_pending,\n"
395         buf += "        .write_pending_status           = " + fabric_mod_name + "_write_pending_status,\n"
396         buf += "        .set_default_node_attributes    = " + fabric_mod_name + "_set_default_node_attrs,\n"
397         buf += "        .get_task_tag                   = " + fabric_mod_name + "_get_task_tag,\n"
398         buf += "        .get_cmd_state                  = " + fabric_mod_name + "_get_cmd_state,\n"
399         buf += "        .queue_data_in                  = " + fabric_mod_name + "_queue_data_in,\n"
400         buf += "        .queue_status                   = " + fabric_mod_name + "_queue_status,\n"
401         buf += "        .queue_tm_rsp                   = " + fabric_mod_name + "_queue_tm_rsp,\n"
402         buf += "        .aborted_task                   = " + fabric_mod_name + "_aborted_task,\n"
403         buf += "        /*\n"
404         buf += "         * Setup function pointers for generic logic in target_core_fabric_configfs.c\n"
405         buf += "         */\n"
406         buf += "        .fabric_make_wwn                = " + fabric_mod_name + "_make_" + fabric_mod_port + ",\n"
407         buf += "        .fabric_drop_wwn                = " + fabric_mod_name + "_drop_" + fabric_mod_port + ",\n"
408         buf += "        .fabric_make_tpg                = " + fabric_mod_name + "_make_tpg,\n"
409         buf += "        .fabric_drop_tpg                = " + fabric_mod_name + "_drop_tpg,\n"
410         buf += "        .fabric_post_link               = NULL,\n"
411         buf += "        .fabric_pre_unlink              = NULL,\n"
412         buf += "        .fabric_make_np                 = NULL,\n"
413         buf += "        .fabric_drop_np                 = NULL,\n"
414         buf += "        .fabric_make_nodeacl            = " + fabric_mod_name + "_make_nodeacl,\n"
415         buf += "        .fabric_drop_nodeacl            = " + fabric_mod_name + "_drop_nodeacl,\n"
416         buf += "};\n\n"
417
418         buf += "static int " + fabric_mod_name + "_register_configfs(void)\n"
419         buf += "{\n"
420         buf += "        struct target_fabric_configfs *fabric;\n"
421         buf += "        int ret;\n\n"
422         buf += "        printk(KERN_INFO \"" + fabric_mod_name.upper() + " fabric module %s on %s/%s\"\n"
423         buf += "                \" on \"UTS_RELEASE\"\\n\"," + fabric_mod_name.upper() + "_VERSION, utsname()->sysname,\n"
424         buf += "                utsname()->machine);\n"
425         buf += "        /*\n"
426         buf += "         * Register the top level struct config_item_type with TCM core\n"
427         buf += "         */\n"
428         buf += "        fabric = target_fabric_configfs_init(THIS_MODULE, \"" + fabric_mod_name + "\");\n"
429         buf += "        if (IS_ERR(fabric)) {\n"
430         buf += "                printk(KERN_ERR \"target_fabric_configfs_init() failed\\n\");\n"
431         buf += "                return PTR_ERR(fabric);\n"
432         buf += "        }\n"
433         buf += "        /*\n"
434         buf += "         * Setup fabric->tf_ops from our local " + fabric_mod_name + "_ops\n"
435         buf += "         */\n"
436         buf += "        fabric->tf_ops = " + fabric_mod_name + "_ops;\n"
437         buf += "        /*\n"
438         buf += "         * Setup default attribute lists for various fabric->tf_cit_tmpl\n"
439         buf += "         */\n"
440         buf += "        fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = " + fabric_mod_name + "_wwn_attrs;\n"
441         buf += "        fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = NULL;\n"
442         buf += "        fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;\n"
443         buf += "        fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;\n"
444         buf += "        fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;\n"
445         buf += "        fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;\n"
446         buf += "        fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;\n"
447         buf += "        fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;\n"
448         buf += "        fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;\n"
449         buf += "        /*\n"
450         buf += "         * Register the fabric for use within TCM\n"
451         buf += "         */\n"
452         buf += "        ret = target_fabric_configfs_register(fabric);\n"
453         buf += "        if (ret < 0) {\n"
454         buf += "                printk(KERN_ERR \"target_fabric_configfs_register() failed\"\n"
455         buf += "                                \" for " + fabric_mod_name.upper() + "\\n\");\n"
456         buf += "                return ret;\n"
457         buf += "        }\n"
458         buf += "        /*\n"
459         buf += "         * Setup our local pointer to *fabric\n"
460         buf += "         */\n"
461         buf += "        " + fabric_mod_name + "_fabric_configfs = fabric;\n"
462         buf += "        printk(KERN_INFO \"" +  fabric_mod_name.upper() + "[0] - Set fabric -> " + fabric_mod_name + "_fabric_configfs\\n\");\n"
463         buf += "        return 0;\n"
464         buf += "};\n\n"
465         buf += "static void __exit " + fabric_mod_name + "_deregister_configfs(void)\n"
466         buf += "{\n"
467         buf += "        if (!" + fabric_mod_name + "_fabric_configfs)\n"
468         buf += "                return;\n\n"
469         buf += "        target_fabric_configfs_deregister(" + fabric_mod_name + "_fabric_configfs);\n"
470         buf += "        " + fabric_mod_name + "_fabric_configfs = NULL;\n"
471         buf += "        printk(KERN_INFO \"" +  fabric_mod_name.upper() + "[0] - Cleared " + fabric_mod_name + "_fabric_configfs\\n\");\n"
472         buf += "};\n\n"
473
474         buf += "static int __init " + fabric_mod_name + "_init(void)\n"
475         buf += "{\n"
476         buf += "        int ret;\n\n"
477         buf += "        ret = " + fabric_mod_name + "_register_configfs();\n"
478         buf += "        if (ret < 0)\n"
479         buf += "                return ret;\n\n"
480         buf += "        return 0;\n"
481         buf += "};\n\n"
482         buf += "static void __exit " + fabric_mod_name + "_exit(void)\n"
483         buf += "{\n"
484         buf += "        " + fabric_mod_name + "_deregister_configfs();\n"
485         buf += "};\n\n"
486
487         buf += "MODULE_DESCRIPTION(\"" + fabric_mod_name.upper() + " series fabric driver\");\n"
488         buf += "MODULE_LICENSE(\"GPL\");\n"
489         buf += "module_init(" + fabric_mod_name + "_init);\n"
490         buf += "module_exit(" + fabric_mod_name + "_exit);\n"
491
492         ret = p.write(buf)
493         if ret:
494                 tcm_mod_err("Unable to write f: " + f)
495
496         p.close()
497
498         return
499
500 def tcm_mod_scan_fabric_ops(tcm_dir):
501
502         fabric_ops_api = tcm_dir + "include/target/target_core_fabric.h"
503
504         print "Using tcm_mod_scan_fabric_ops: " + fabric_ops_api
505         process_fo = 0;
506
507         p = open(fabric_ops_api, 'r')
508
509         line = p.readline()
510         while line:
511                 if process_fo == 0 and re.search('struct target_core_fabric_ops {', line):
512                         line = p.readline()
513                         continue
514
515                 if process_fo == 0:
516                         process_fo = 1;
517                         line = p.readline()
518                         # Search for function pointer
519                         if not re.search('\(\*', line):
520                                 continue
521
522                         fabric_ops.append(line.rstrip())
523                         continue
524
525                 line = p.readline()
526                 # Search for function pointer
527                 if not re.search('\(\*', line):
528                         continue
529
530                 fabric_ops.append(line.rstrip())
531
532         p.close()
533         return
534
535 def tcm_mod_dump_fabric_ops(proto_ident, fabric_mod_dir_var, fabric_mod_name):
536         buf = ""
537         bufi = ""
538
539         f = fabric_mod_dir_var + "/" + fabric_mod_name + "_fabric.c"
540         print "Writing file: " + f
541
542         p = open(f, 'w')
543         if not p:
544                 tcm_mod_err("Unable to open file: " + f)
545
546         fi = fabric_mod_dir_var + "/" + fabric_mod_name + "_fabric.h"
547         print "Writing file: " + fi
548
549         pi = open(fi, 'w')
550         if not pi:
551                 tcm_mod_err("Unable to open file: " + fi)
552
553         buf = "#include <linux/slab.h>\n"
554         buf += "#include <linux/kthread.h>\n"
555         buf += "#include <linux/types.h>\n"
556         buf += "#include <linux/list.h>\n"
557         buf += "#include <linux/types.h>\n"
558         buf += "#include <linux/string.h>\n"
559         buf += "#include <linux/ctype.h>\n"
560         buf += "#include <asm/unaligned.h>\n"
561         buf += "#include <scsi/scsi.h>\n"
562         buf += "#include <scsi/scsi_host.h>\n"
563         buf += "#include <scsi/scsi_device.h>\n"
564         buf += "#include <scsi/scsi_cmnd.h>\n"
565         buf += "#include <scsi/libfc.h>\n\n"
566         buf += "#include <target/target_core_base.h>\n"
567         buf += "#include <target/target_core_fabric.h>\n"
568         buf += "#include <target/target_core_configfs.h>\n\n"
569         buf += "#include \"" + fabric_mod_name + "_base.h\"\n"
570         buf += "#include \"" + fabric_mod_name + "_fabric.h\"\n\n"
571
572         buf += "int " + fabric_mod_name + "_check_true(struct se_portal_group *se_tpg)\n"
573         buf += "{\n"
574         buf += "        return 1;\n"
575         buf += "}\n\n"
576         bufi += "int " + fabric_mod_name + "_check_true(struct se_portal_group *);\n"
577
578         buf += "int " + fabric_mod_name + "_check_false(struct se_portal_group *se_tpg)\n"
579         buf += "{\n"
580         buf += "        return 0;\n"
581         buf += "}\n\n"
582         bufi += "int " + fabric_mod_name + "_check_false(struct se_portal_group *);\n"
583
584         total_fabric_ops = len(fabric_ops)
585         i = 0
586
587         while i < total_fabric_ops:
588                 fo = fabric_ops[i]
589                 i += 1
590 #               print "fabric_ops: " + fo
591
592                 if re.search('get_fabric_name', fo):
593                         buf += "char *" + fabric_mod_name + "_get_fabric_name(void)\n"
594                         buf += "{\n"
595                         buf += "        return \"" + fabric_mod_name + "\";\n"
596                         buf += "}\n\n"
597                         bufi += "char *" + fabric_mod_name + "_get_fabric_name(void);\n"
598                         continue
599
600                 if re.search('get_fabric_proto_ident', fo):
601                         buf += "u8 " + fabric_mod_name + "_get_fabric_proto_ident(struct se_portal_group *se_tpg)\n"
602                         buf += "{\n"
603                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
604                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
605                         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n"
606                         buf += "        u8 proto_id;\n\n"
607                         buf += "        switch (" + fabric_mod_port + "->" + fabric_mod_port + "_proto_id) {\n"
608                         if proto_ident == "FC":
609                                 buf += "        case SCSI_PROTOCOL_FCP:\n"
610                                 buf += "        default:\n"
611                                 buf += "                proto_id = fc_get_fabric_proto_ident(se_tpg);\n"
612                                 buf += "                break;\n"
613                         elif proto_ident == "SAS":
614                                 buf += "        case SCSI_PROTOCOL_SAS:\n"
615                                 buf += "        default:\n"
616                                 buf += "                proto_id = sas_get_fabric_proto_ident(se_tpg);\n"
617                                 buf += "                break;\n"
618                         elif proto_ident == "iSCSI":
619                                 buf += "        case SCSI_PROTOCOL_ISCSI:\n"
620                                 buf += "        default:\n"
621                                 buf += "                proto_id = iscsi_get_fabric_proto_ident(se_tpg);\n"
622                                 buf += "                break;\n"
623
624                         buf += "        }\n\n"
625                         buf += "        return proto_id;\n"
626                         buf += "}\n\n"
627                         bufi += "u8 " + fabric_mod_name + "_get_fabric_proto_ident(struct se_portal_group *);\n"
628
629                 if re.search('get_wwn', fo):
630                         buf += "char *" + fabric_mod_name + "_get_fabric_wwn(struct se_portal_group *se_tpg)\n"
631                         buf += "{\n"
632                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
633                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
634                         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n\n"
635                         buf += "        return &" + fabric_mod_port + "->" + fabric_mod_port + "_name[0];\n"
636                         buf += "}\n\n"
637                         bufi += "char *" + fabric_mod_name + "_get_fabric_wwn(struct se_portal_group *);\n"
638
639                 if re.search('get_tag', fo):
640                         buf += "u16 " + fabric_mod_name + "_get_tag(struct se_portal_group *se_tpg)\n"
641                         buf += "{\n"
642                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
643                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
644                         buf += "        return tpg->" + fabric_mod_port + "_tpgt;\n"
645                         buf += "}\n\n"
646                         bufi += "u16 " + fabric_mod_name + "_get_tag(struct se_portal_group *);\n"
647
648                 if re.search('get_default_depth', fo):
649                         buf += "u32 " + fabric_mod_name + "_get_default_depth(struct se_portal_group *se_tpg)\n"
650                         buf += "{\n"
651                         buf += "        return 1;\n"
652                         buf += "}\n\n"
653                         bufi += "u32 " + fabric_mod_name + "_get_default_depth(struct se_portal_group *);\n"
654
655                 if re.search('get_pr_transport_id\)\(', fo):
656                         buf += "u32 " + fabric_mod_name + "_get_pr_transport_id(\n"
657                         buf += "        struct se_portal_group *se_tpg,\n"
658                         buf += "        struct se_node_acl *se_nacl,\n"
659                         buf += "        struct t10_pr_registration *pr_reg,\n"
660                         buf += "        int *format_code,\n"
661                         buf += "        unsigned char *buf)\n"
662                         buf += "{\n"
663                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
664                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
665                         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n"
666                         buf += "        int ret = 0;\n\n"
667                         buf += "        switch (" + fabric_mod_port + "->" + fabric_mod_port + "_proto_id) {\n"
668                         if proto_ident == "FC":
669                                 buf += "        case SCSI_PROTOCOL_FCP:\n"
670                                 buf += "        default:\n"
671                                 buf += "                ret = fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,\n"
672                                 buf += "                                        format_code, buf);\n"
673                                 buf += "                break;\n"
674                         elif proto_ident == "SAS":
675                                 buf += "        case SCSI_PROTOCOL_SAS:\n"
676                                 buf += "        default:\n"
677                                 buf += "                ret = sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,\n"
678                                 buf += "                                        format_code, buf);\n"
679                                 buf += "                break;\n"
680                         elif proto_ident == "iSCSI":
681                                 buf += "        case SCSI_PROTOCOL_ISCSI:\n"
682                                 buf += "        default:\n"
683                                 buf += "                ret = iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,\n"
684                                 buf += "                                        format_code, buf);\n"
685                                 buf += "                break;\n"
686
687                         buf += "        }\n\n"
688                         buf += "        return ret;\n"
689                         buf += "}\n\n"
690                         bufi += "u32 " + fabric_mod_name + "_get_pr_transport_id(struct se_portal_group *,\n"
691                         bufi += "                       struct se_node_acl *, struct t10_pr_registration *,\n"
692                         bufi += "                       int *, unsigned char *);\n"
693
694                 if re.search('get_pr_transport_id_len\)\(', fo):
695                         buf += "u32 " + fabric_mod_name + "_get_pr_transport_id_len(\n"
696                         buf += "        struct se_portal_group *se_tpg,\n"
697                         buf += "        struct se_node_acl *se_nacl,\n"
698                         buf += "        struct t10_pr_registration *pr_reg,\n"
699                         buf += "        int *format_code)\n"
700                         buf += "{\n"
701                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
702                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
703                         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n"
704                         buf += "        int ret = 0;\n\n"
705                         buf += "        switch (" + fabric_mod_port + "->" + fabric_mod_port + "_proto_id) {\n"
706                         if proto_ident == "FC":
707                                 buf += "        case SCSI_PROTOCOL_FCP:\n"
708                                 buf += "        default:\n"
709                                 buf += "                ret = fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,\n"
710                                 buf += "                                        format_code);\n"
711                                 buf += "                break;\n"
712                         elif proto_ident == "SAS":
713                                 buf += "        case SCSI_PROTOCOL_SAS:\n"
714                                 buf += "        default:\n"
715                                 buf += "                ret = sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,\n"
716                                 buf += "                                        format_code);\n"
717                                 buf += "                break;\n"
718                         elif proto_ident == "iSCSI":
719                                 buf += "        case SCSI_PROTOCOL_ISCSI:\n"
720                                 buf += "        default:\n"
721                                 buf += "                ret = iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,\n"
722                                 buf += "                                        format_code);\n"
723                                 buf += "                break;\n"
724
725
726                         buf += "        }\n\n"
727                         buf += "        return ret;\n"
728                         buf += "}\n\n"
729                         bufi += "u32 " + fabric_mod_name + "_get_pr_transport_id_len(struct se_portal_group *,\n"
730                         bufi += "                       struct se_node_acl *, struct t10_pr_registration *,\n"
731                         bufi += "                       int *);\n"
732
733                 if re.search('parse_pr_out_transport_id\)\(', fo):
734                         buf += "char *" + fabric_mod_name + "_parse_pr_out_transport_id(\n"
735                         buf += "        struct se_portal_group *se_tpg,\n"
736                         buf += "        const char *buf,\n"
737                         buf += "        u32 *out_tid_len,\n"
738                         buf += "        char **port_nexus_ptr)\n"
739                         buf += "{\n"
740                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
741                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
742                         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n"
743                         buf += "        char *tid = NULL;\n\n"
744                         buf += "        switch (" + fabric_mod_port + "->" + fabric_mod_port + "_proto_id) {\n"
745                         if proto_ident == "FC":
746                                 buf += "        case SCSI_PROTOCOL_FCP:\n"
747                                 buf += "        default:\n"
748                                 buf += "                tid = fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,\n"
749                                 buf += "                                        port_nexus_ptr);\n"
750                         elif proto_ident == "SAS":
751                                 buf += "        case SCSI_PROTOCOL_SAS:\n"
752                                 buf += "        default:\n"
753                                 buf += "                tid = sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,\n"
754                                 buf += "                                        port_nexus_ptr);\n"
755                         elif proto_ident == "iSCSI":
756                                 buf += "        case SCSI_PROTOCOL_ISCSI:\n"
757                                 buf += "        default:\n"
758                                 buf += "                tid = iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,\n"
759                                 buf += "                                        port_nexus_ptr);\n"
760
761                         buf += "        }\n\n"
762                         buf += "        return tid;\n"
763                         buf += "}\n\n"
764                         bufi += "char *" + fabric_mod_name + "_parse_pr_out_transport_id(struct se_portal_group *,\n"
765                         bufi += "                       const char *, u32 *, char **);\n"
766
767                 if re.search('alloc_fabric_acl\)\(', fo):
768                         buf += "struct se_node_acl *" + fabric_mod_name + "_alloc_fabric_acl(struct se_portal_group *se_tpg)\n"
769                         buf += "{\n"
770                         buf += "        struct " + fabric_mod_name + "_nacl *nacl;\n\n"
771                         buf += "        nacl = kzalloc(sizeof(struct " + fabric_mod_name + "_nacl), GFP_KERNEL);\n"
772                         buf += "        if (!nacl) {\n"
773                         buf += "                printk(KERN_ERR \"Unable to allocate struct " + fabric_mod_name + "_nacl\\n\");\n"
774                         buf += "                return NULL;\n"
775                         buf += "        }\n\n"
776                         buf += "        return &nacl->se_node_acl;\n"
777                         buf += "}\n\n"
778                         bufi += "struct se_node_acl *" + fabric_mod_name + "_alloc_fabric_acl(struct se_portal_group *);\n"
779
780                 if re.search('release_fabric_acl\)\(', fo):
781                         buf += "void " + fabric_mod_name + "_release_fabric_acl(\n"
782                         buf += "        struct se_portal_group *se_tpg,\n"
783                         buf += "        struct se_node_acl *se_nacl)\n"
784                         buf += "{\n"
785                         buf += "        struct " + fabric_mod_name + "_nacl *nacl = container_of(se_nacl,\n"
786                         buf += "                        struct " + fabric_mod_name + "_nacl, se_node_acl);\n"
787                         buf += "        kfree(nacl);\n"
788                         buf += "}\n\n"
789                         bufi += "void " + fabric_mod_name + "_release_fabric_acl(struct se_portal_group *,\n"
790                         bufi += "                       struct se_node_acl *);\n"
791
792                 if re.search('tpg_get_inst_index\)\(', fo):
793                         buf += "u32 " + fabric_mod_name + "_tpg_get_inst_index(struct se_portal_group *se_tpg)\n"
794                         buf += "{\n"
795                         buf += "        return 1;\n"
796                         buf += "}\n\n"
797                         bufi += "u32 " + fabric_mod_name + "_tpg_get_inst_index(struct se_portal_group *);\n"
798
799                 if re.search('\*release_cmd\)\(', fo):
800                         buf += "void " + fabric_mod_name + "_release_cmd(struct se_cmd *se_cmd)\n"
801                         buf += "{\n"
802                         buf += "        return;\n"
803                         buf += "}\n\n"
804                         bufi += "void " + fabric_mod_name + "_release_cmd(struct se_cmd *);\n"
805
806                 if re.search('shutdown_session\)\(', fo):
807                         buf += "int " + fabric_mod_name + "_shutdown_session(struct se_session *se_sess)\n"
808                         buf += "{\n"
809                         buf += "        return 0;\n"
810                         buf += "}\n\n"
811                         bufi += "int " + fabric_mod_name + "_shutdown_session(struct se_session *);\n"
812
813                 if re.search('close_session\)\(', fo):
814                         buf += "void " + fabric_mod_name + "_close_session(struct se_session *se_sess)\n"
815                         buf += "{\n"
816                         buf += "        return;\n"
817                         buf += "}\n\n"
818                         bufi += "void " + fabric_mod_name + "_close_session(struct se_session *);\n"
819
820                 if re.search('sess_get_index\)\(', fo):
821                         buf += "u32 " + fabric_mod_name + "_sess_get_index(struct se_session *se_sess)\n"
822                         buf += "{\n"
823                         buf += "        return 0;\n"
824                         buf += "}\n\n"
825                         bufi += "u32 " + fabric_mod_name + "_sess_get_index(struct se_session *);\n"
826
827                 if re.search('write_pending\)\(', fo):
828                         buf += "int " + fabric_mod_name + "_write_pending(struct se_cmd *se_cmd)\n"
829                         buf += "{\n"
830                         buf += "        return 0;\n"
831                         buf += "}\n\n"
832                         bufi += "int " + fabric_mod_name + "_write_pending(struct se_cmd *);\n"
833
834                 if re.search('write_pending_status\)\(', fo):
835                         buf += "int " + fabric_mod_name + "_write_pending_status(struct se_cmd *se_cmd)\n"
836                         buf += "{\n"
837                         buf += "        return 0;\n"
838                         buf += "}\n\n"
839                         bufi += "int " + fabric_mod_name + "_write_pending_status(struct se_cmd *);\n"
840
841                 if re.search('set_default_node_attributes\)\(', fo):
842                         buf += "void " + fabric_mod_name + "_set_default_node_attrs(struct se_node_acl *nacl)\n"
843                         buf += "{\n"
844                         buf += "        return;\n"
845                         buf += "}\n\n"
846                         bufi += "void " + fabric_mod_name + "_set_default_node_attrs(struct se_node_acl *);\n"
847
848                 if re.search('get_task_tag\)\(', fo):
849                         buf += "u32 " + fabric_mod_name + "_get_task_tag(struct se_cmd *se_cmd)\n"
850                         buf += "{\n"
851                         buf += "        return 0;\n"
852                         buf += "}\n\n"
853                         bufi += "u32 " + fabric_mod_name + "_get_task_tag(struct se_cmd *);\n"
854
855                 if re.search('get_cmd_state\)\(', fo):
856                         buf += "int " + fabric_mod_name + "_get_cmd_state(struct se_cmd *se_cmd)\n"
857                         buf += "{\n"
858                         buf += "        return 0;\n"
859                         buf += "}\n\n"
860                         bufi += "int " + fabric_mod_name + "_get_cmd_state(struct se_cmd *);\n"
861
862                 if re.search('queue_data_in\)\(', fo):
863                         buf += "int " + fabric_mod_name + "_queue_data_in(struct se_cmd *se_cmd)\n"
864                         buf += "{\n"
865                         buf += "        return 0;\n"
866                         buf += "}\n\n"
867                         bufi += "int " + fabric_mod_name + "_queue_data_in(struct se_cmd *);\n"
868
869                 if re.search('queue_status\)\(', fo):
870                         buf += "int " + fabric_mod_name + "_queue_status(struct se_cmd *se_cmd)\n"
871                         buf += "{\n"
872                         buf += "        return 0;\n"
873                         buf += "}\n\n"
874                         bufi += "int " + fabric_mod_name + "_queue_status(struct se_cmd *);\n"
875
876                 if re.search('queue_tm_rsp\)\(', fo):
877                         buf += "void " + fabric_mod_name + "_queue_tm_rsp(struct se_cmd *se_cmd)\n"
878                         buf += "{\n"
879                         buf += "        return;\n"
880                         buf += "}\n\n"
881                         bufi += "void " + fabric_mod_name + "_queue_tm_rsp(struct se_cmd *);\n"
882
883                 if re.search('aborted_task\)\(', fo):
884                         buf += "void " + fabric_mod_name + "_aborted_task(struct se_cmd *se_cmd)\n"
885                         buf += "{\n"
886                         buf += "        return;\n"
887                         buf += "}\n\n"
888                         bufi += "void " + fabric_mod_name + "_aborted_task(struct se_cmd *);\n"
889
890         ret = p.write(buf)
891         if ret:
892                 tcm_mod_err("Unable to write f: " + f)
893
894         p.close()
895
896         ret = pi.write(bufi)
897         if ret:
898                 tcm_mod_err("Unable to write fi: " + fi)
899
900         pi.close()
901         return
902
903 def tcm_mod_build_kbuild(fabric_mod_dir_var, fabric_mod_name):
904
905         buf = ""
906         f = fabric_mod_dir_var + "/Makefile"
907         print "Writing file: " + f
908
909         p = open(f, 'w')
910         if not p:
911                 tcm_mod_err("Unable to open file: " + f)
912
913         buf += fabric_mod_name + "-objs                 := " + fabric_mod_name + "_fabric.o \\\n"
914         buf += "                                           " + fabric_mod_name + "_configfs.o\n"
915         buf += "obj-$(CONFIG_" + fabric_mod_name.upper() + ")           += " + fabric_mod_name + ".o\n"
916
917         ret = p.write(buf)
918         if ret:
919                 tcm_mod_err("Unable to write f: " + f)
920
921         p.close()
922         return
923
924 def tcm_mod_build_kconfig(fabric_mod_dir_var, fabric_mod_name):
925
926         buf = ""
927         f = fabric_mod_dir_var + "/Kconfig"
928         print "Writing file: " + f
929
930         p = open(f, 'w')
931         if not p:
932                 tcm_mod_err("Unable to open file: " + f)
933
934         buf = "config " + fabric_mod_name.upper() + "\n"
935         buf += "        tristate \"" + fabric_mod_name.upper() + " fabric module\"\n"
936         buf += "        depends on TARGET_CORE && CONFIGFS_FS\n"
937         buf += "        default n\n"
938         buf += "        ---help---\n"
939         buf += "        Say Y here to enable the " + fabric_mod_name.upper() + " fabric module\n"
940
941         ret = p.write(buf)
942         if ret:
943                 tcm_mod_err("Unable to write f: " + f)
944
945         p.close()
946         return
947
948 def tcm_mod_add_kbuild(tcm_dir, fabric_mod_name):
949         buf = "obj-$(CONFIG_" + fabric_mod_name.upper() + ")    += " + fabric_mod_name.lower() + "/\n"
950         kbuild = tcm_dir + "/drivers/target/Makefile"
951
952         f = open(kbuild, 'a')
953         f.write(buf)
954         f.close()
955         return
956
957 def tcm_mod_add_kconfig(tcm_dir, fabric_mod_name):
958         buf = "source \"drivers/target/" + fabric_mod_name.lower() + "/Kconfig\"\n"
959         kconfig = tcm_dir + "/drivers/target/Kconfig"
960
961         f = open(kconfig, 'a')
962         f.write(buf)
963         f.close()
964         return
965
966 def main(modname, proto_ident):
967 #       proto_ident = "FC"
968 #       proto_ident = "SAS"
969 #       proto_ident = "iSCSI"
970
971         tcm_dir = os.getcwd();
972         tcm_dir += "/../../"
973         print "tcm_dir: " + tcm_dir
974         fabric_mod_name = modname
975         fabric_mod_dir = tcm_dir + "drivers/target/" + fabric_mod_name
976         print "Set fabric_mod_name: " + fabric_mod_name
977         print "Set fabric_mod_dir: " + fabric_mod_dir
978         print "Using proto_ident: " + proto_ident
979
980         if proto_ident != "FC" and proto_ident != "SAS" and proto_ident != "iSCSI":
981                 print "Unsupported proto_ident: " + proto_ident
982                 sys.exit(1)
983
984         ret = tcm_mod_create_module_subdir(fabric_mod_dir)
985         if ret:
986                 print "tcm_mod_create_module_subdir() failed because module already exists!"
987                 sys.exit(1)
988
989         tcm_mod_build_base_includes(proto_ident, fabric_mod_dir, fabric_mod_name)
990         tcm_mod_scan_fabric_ops(tcm_dir)
991         tcm_mod_dump_fabric_ops(proto_ident, fabric_mod_dir, fabric_mod_name)
992         tcm_mod_build_configfs(proto_ident, fabric_mod_dir, fabric_mod_name)
993         tcm_mod_build_kbuild(fabric_mod_dir, fabric_mod_name)
994         tcm_mod_build_kconfig(fabric_mod_dir, fabric_mod_name)
995
996         input = raw_input("Would you like to add " + fabric_mod_name + " to drivers/target/Makefile..? [yes,no]: ")
997         if input == "yes" or input == "y":
998                 tcm_mod_add_kbuild(tcm_dir, fabric_mod_name)
999
1000         input = raw_input("Would you like to add " + fabric_mod_name + " to drivers/target/Kconfig..? [yes,no]: ")
1001         if input == "yes" or input == "y":
1002                 tcm_mod_add_kconfig(tcm_dir, fabric_mod_name)
1003
1004         return
1005
1006 parser = optparse.OptionParser()
1007 parser.add_option('-m', '--modulename', help='Module name', dest='modname',
1008                 action='store', nargs=1, type='string')
1009 parser.add_option('-p', '--protoident', help='Protocol Ident', dest='protoident',
1010                 action='store', nargs=1, type='string')
1011
1012 (opts, args) = parser.parse_args()
1013
1014 mandatories = ['modname', 'protoident']
1015 for m in mandatories:
1016         if not opts.__dict__[m]:
1017                 print "mandatory option is missing\n"
1018                 parser.print_help()
1019                 exit(-1)
1020
1021 if __name__ == "__main__":
1022
1023         main(str(opts.modname), opts.protoident)