target: Bump core version to v5.0
[pandora-kernel.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 + "_tpg {\n"
54         buf += "        /* FC lport target portal group tag for TCM */\n"
55         buf += "        u16 lport_tpgt;\n"
56         buf += "        /* Pointer back to " + fabric_mod_name + "_lport */\n"
57         buf += "        struct " + fabric_mod_name + "_lport *lport;\n"
58         buf += "        /* Returned by " + fabric_mod_name + "_make_tpg() */\n"
59         buf += "        struct se_portal_group se_tpg;\n"
60         buf += "};\n"
61         buf += "\n"
62         buf += "struct " + fabric_mod_name + "_lport {\n"
63         buf += "        /* Binary World Wide unique Port Name for FC Target Lport */\n"
64         buf += "        u64 lport_wwpn;\n"
65         buf += "        /* ASCII formatted WWPN for FC Target Lport */\n"
66         buf += "        char lport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
67         buf += "        /* Returned by " + fabric_mod_name + "_make_lport() */\n"
68         buf += "        struct se_wwn lport_wwn;\n"
69         buf += "};\n"
70
71         ret = p.write(buf)
72         if ret:
73                 tcm_mod_err("Unable to write f: " + f)
74
75         p.close()
76
77         fabric_mod_port = "lport"
78         fabric_mod_init_port = "nport"
79
80         return
81
82 def tcm_mod_build_SAS_include(fabric_mod_dir_var, fabric_mod_name):
83         global fabric_mod_port
84         global fabric_mod_init_port
85         buf = ""
86
87         f = fabric_mod_dir_var + "/" + fabric_mod_name + "_base.h"
88         print "Writing file: " + f
89
90         p = open(f, 'w');
91         if not p:
92                 tcm_mod_err("Unable to open file: " + f)
93
94         buf = "#define " + fabric_mod_name.upper() + "_VERSION  \"v0.1\"\n"
95         buf += "#define " + fabric_mod_name.upper() + "_NAMELEN 32\n"
96         buf += "\n"
97         buf += "struct " + fabric_mod_name + "_tpg {\n"
98         buf += "        /* SAS port target portal group tag for TCM */\n"
99         buf += "        u16 tport_tpgt;\n"
100         buf += "        /* Pointer back to " + fabric_mod_name + "_tport */\n"
101         buf += "        struct " + fabric_mod_name + "_tport *tport;\n"
102         buf += "        /* Returned by " + fabric_mod_name + "_make_tpg() */\n"
103         buf += "        struct se_portal_group se_tpg;\n"
104         buf += "};\n\n"
105         buf += "struct " + fabric_mod_name + "_tport {\n"
106         buf += "        /* Binary World Wide unique Port Name for SAS Target port */\n"
107         buf += "        u64 tport_wwpn;\n"
108         buf += "        /* ASCII formatted WWPN for SAS Target port */\n"
109         buf += "        char tport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
110         buf += "        /* Returned by " + fabric_mod_name + "_make_tport() */\n"
111         buf += "        struct se_wwn tport_wwn;\n"
112         buf += "};\n"
113
114         ret = p.write(buf)
115         if ret:
116                 tcm_mod_err("Unable to write f: " + f)
117
118         p.close()
119
120         fabric_mod_port = "tport"
121         fabric_mod_init_port = "iport"
122
123         return
124
125 def tcm_mod_build_iSCSI_include(fabric_mod_dir_var, fabric_mod_name):
126         global fabric_mod_port
127         global fabric_mod_init_port
128         buf = ""
129
130         f = fabric_mod_dir_var + "/" + fabric_mod_name + "_base.h"
131         print "Writing file: " + f
132
133         p = open(f, 'w');
134         if not p:
135                 tcm_mod_err("Unable to open file: " + f)
136
137         buf = "#define " + fabric_mod_name.upper() + "_VERSION  \"v0.1\"\n"
138         buf += "#define " + fabric_mod_name.upper() + "_NAMELEN 32\n"
139         buf += "\n"
140         buf += "struct " + fabric_mod_name + "_tpg {\n"
141         buf += "        /* iSCSI target portal group tag for TCM */\n"
142         buf += "        u16 tport_tpgt;\n"
143         buf += "        /* Pointer back to " + fabric_mod_name + "_tport */\n"
144         buf += "        struct " + fabric_mod_name + "_tport *tport;\n"
145         buf += "        /* Returned by " + fabric_mod_name + "_make_tpg() */\n"
146         buf += "        struct se_portal_group se_tpg;\n"
147         buf += "};\n\n"
148         buf += "struct " + fabric_mod_name + "_tport {\n"
149         buf += "        /* ASCII formatted TargetName for IQN */\n"
150         buf += "        char tport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
151         buf += "        /* Returned by " + fabric_mod_name + "_make_tport() */\n"
152         buf += "        struct se_wwn tport_wwn;\n"
153         buf += "};\n"
154
155         ret = p.write(buf)
156         if ret:
157                 tcm_mod_err("Unable to write f: " + f)
158
159         p.close()
160
161         fabric_mod_port = "tport"
162         fabric_mod_init_port = "iport"
163
164         return
165
166 def tcm_mod_build_base_includes(proto_ident, fabric_mod_dir_val, fabric_mod_name):
167
168         if proto_ident == "FC":
169                 tcm_mod_build_FC_include(fabric_mod_dir_val, fabric_mod_name)
170         elif proto_ident == "SAS":
171                 tcm_mod_build_SAS_include(fabric_mod_dir_val, fabric_mod_name)
172         elif proto_ident == "iSCSI":
173                 tcm_mod_build_iSCSI_include(fabric_mod_dir_val, fabric_mod_name)
174         else:
175                 print "Unsupported proto_ident: " + proto_ident
176                 sys.exit(1)
177
178         return
179
180 def tcm_mod_build_configfs(proto_ident, fabric_mod_dir_var, fabric_mod_name):
181         buf = ""
182
183         f = fabric_mod_dir_var + "/" + fabric_mod_name + "_configfs.c"
184         print "Writing file: " + f
185
186         p = open(f, 'w');
187         if not p:
188                 tcm_mod_err("Unable to open file: " + f)
189
190         buf = "#include <linux/module.h>\n"
191         buf += "#include <linux/moduleparam.h>\n"
192         buf += "#include <linux/version.h>\n"
193         buf += "#include <generated/utsrelease.h>\n"
194         buf += "#include <linux/utsname.h>\n"
195         buf += "#include <linux/init.h>\n"
196         buf += "#include <linux/slab.h>\n"
197         buf += "#include <linux/kthread.h>\n"
198         buf += "#include <linux/types.h>\n"
199         buf += "#include <linux/string.h>\n"
200         buf += "#include <linux/configfs.h>\n"
201         buf += "#include <linux/ctype.h>\n"
202         buf += "#include <asm/unaligned.h>\n\n"
203         buf += "#include <target/target_core_base.h>\n"
204         buf += "#include <target/target_core_fabric.h>\n"
205         buf += "#include <target/target_core_fabric_configfs.h>\n"
206         buf += "#include <target/configfs_macros.h>\n\n"
207         buf += "#include \"" + fabric_mod_name + "_base.h\"\n"
208         buf += "#include \"" + fabric_mod_name + "_fabric.h\"\n\n"
209
210         buf += "static const struct target_core_fabric_ops " + fabric_mod_name + "_ops;\n\n"
211
212         buf += "static struct se_portal_group *" + fabric_mod_name + "_make_tpg(\n"
213         buf += "        struct se_wwn *wwn,\n"
214         buf += "        struct config_group *group,\n"
215         buf += "        const char *name)\n"
216         buf += "{\n"
217         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + "*" + fabric_mod_port + " = container_of(wwn,\n"
218         buf += "                        struct " + fabric_mod_name + "_" + fabric_mod_port + ", " + fabric_mod_port + "_wwn);\n\n"
219         buf += "        struct " + fabric_mod_name + "_tpg *tpg;\n"
220         buf += "        unsigned long tpgt;\n"
221         buf += "        int ret;\n\n"
222         buf += "        if (strstr(name, \"tpgt_\") != name)\n"
223         buf += "                return ERR_PTR(-EINVAL);\n"
224         buf += "        if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)\n"
225         buf += "                return ERR_PTR(-EINVAL);\n\n"
226         buf += "        tpg = kzalloc(sizeof(struct " + fabric_mod_name + "_tpg), GFP_KERNEL);\n"
227         buf += "        if (!tpg) {\n"
228         buf += "                printk(KERN_ERR \"Unable to allocate struct " + fabric_mod_name + "_tpg\");\n"
229         buf += "                return ERR_PTR(-ENOMEM);\n"
230         buf += "        }\n"
231         buf += "        tpg->" + fabric_mod_port + " = " + fabric_mod_port + ";\n"
232         buf += "        tpg->" + fabric_mod_port + "_tpgt = tpgt;\n\n"
233         buf += "        ret = core_tpg_register(&" + fabric_mod_name + "_ops, wwn,\n"
234         buf += "                                &tpg->se_tpg, SCSI_PROTOCOL_SAS);\n"
235         buf += "        if (ret < 0) {\n"
236         buf += "                kfree(tpg);\n"
237         buf += "                return NULL;\n"
238         buf += "        }\n"
239         buf += "        return &tpg->se_tpg;\n"
240         buf += "}\n\n"
241         buf += "static void " + fabric_mod_name + "_drop_tpg(struct se_portal_group *se_tpg)\n"
242         buf += "{\n"
243         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
244         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n\n"
245         buf += "        core_tpg_deregister(se_tpg);\n"
246         buf += "        kfree(tpg);\n"
247         buf += "}\n\n"
248
249         buf += "static struct se_wwn *" + fabric_mod_name + "_make_" + fabric_mod_port + "(\n"
250         buf += "        struct target_fabric_configfs *tf,\n"
251         buf += "        struct config_group *group,\n"
252         buf += "        const char *name)\n"
253         buf += "{\n"
254         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + ";\n"
255
256         if proto_ident == "FC" or proto_ident == "SAS":
257                 buf += "        u64 wwpn = 0;\n\n"
258
259         buf += "        /* if (" + fabric_mod_name + "_parse_wwn(name, &wwpn, 1) < 0)\n"
260         buf += "                return ERR_PTR(-EINVAL); */\n\n"
261         buf += "        " + fabric_mod_port + " = kzalloc(sizeof(struct " + fabric_mod_name + "_" + fabric_mod_port + "), GFP_KERNEL);\n"
262         buf += "        if (!" + fabric_mod_port + ") {\n"
263         buf += "                printk(KERN_ERR \"Unable to allocate struct " + fabric_mod_name + "_" + fabric_mod_port + "\");\n"
264         buf += "                return ERR_PTR(-ENOMEM);\n"
265         buf += "        }\n"
266
267         if proto_ident == "FC" or proto_ident == "SAS":
268                 buf += "        " + fabric_mod_port + "->" + fabric_mod_port + "_wwpn = wwpn;\n"
269
270         buf += "        /* " + fabric_mod_name + "_format_wwn(&" + fabric_mod_port + "->" + fabric_mod_port + "_name[0], " + fabric_mod_name.upper() + "_NAMELEN, wwpn); */\n\n"
271         buf += "        return &" + fabric_mod_port + "->" + fabric_mod_port + "_wwn;\n"
272         buf += "}\n\n"
273         buf += "static void " + fabric_mod_name + "_drop_" + fabric_mod_port + "(struct se_wwn *wwn)\n"
274         buf += "{\n"
275         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = container_of(wwn,\n"
276         buf += "                                struct " + fabric_mod_name + "_" + fabric_mod_port + ", " + fabric_mod_port + "_wwn);\n"
277         buf += "        kfree(" + fabric_mod_port + ");\n"
278         buf += "}\n\n"
279         buf += "static ssize_t " + fabric_mod_name + "_wwn_show_attr_version(\n"
280         buf += "        struct target_fabric_configfs *tf,\n"
281         buf += "        char *page)\n"
282         buf += "{\n"
283         buf += "        return sprintf(page, \"" + fabric_mod_name.upper() + " fabric module %s on %s/%s\"\n"
284         buf += "                \"on \"UTS_RELEASE\"\\n\", " + fabric_mod_name.upper() + "_VERSION, utsname()->sysname,\n"
285         buf += "                utsname()->machine);\n"
286         buf += "}\n\n"
287         buf += "TF_WWN_ATTR_RO(" + fabric_mod_name + ", version);\n\n"
288         buf += "static struct configfs_attribute *" + fabric_mod_name + "_wwn_attrs[] = {\n"
289         buf += "        &" + fabric_mod_name + "_wwn_version.attr,\n"
290         buf += "        NULL,\n"
291         buf += "};\n\n"
292
293         buf += "static const struct target_core_fabric_ops " + fabric_mod_name + "_ops = {\n"
294         buf += "        .module                         = THIS_MODULE,\n"
295         buf += "        .name                           = " + fabric_mod_name + ",\n"
296         buf += "        .get_fabric_name                = " + fabric_mod_name + "_get_fabric_name,\n"
297         buf += "        .tpg_get_wwn                    = " + fabric_mod_name + "_get_fabric_wwn,\n"
298         buf += "        .tpg_get_tag                    = " + fabric_mod_name + "_get_tag,\n"
299         buf += "        .tpg_check_demo_mode            = " + fabric_mod_name + "_check_false,\n"
300         buf += "        .tpg_check_demo_mode_cache      = " + fabric_mod_name + "_check_true,\n"
301         buf += "        .tpg_check_demo_mode_write_protect = " + fabric_mod_name + "_check_true,\n"
302         buf += "        .tpg_check_prod_mode_write_protect = " + fabric_mod_name + "_check_false,\n"
303         buf += "        .tpg_get_inst_index             = " + fabric_mod_name + "_tpg_get_inst_index,\n"
304         buf += "        .release_cmd                    = " + fabric_mod_name + "_release_cmd,\n"
305         buf += "        .shutdown_session               = " + fabric_mod_name + "_shutdown_session,\n"
306         buf += "        .close_session                  = " + fabric_mod_name + "_close_session,\n"
307         buf += "        .sess_get_index                 = " + fabric_mod_name + "_sess_get_index,\n"
308         buf += "        .sess_get_initiator_sid         = NULL,\n"
309         buf += "        .write_pending                  = " + fabric_mod_name + "_write_pending,\n"
310         buf += "        .write_pending_status           = " + fabric_mod_name + "_write_pending_status,\n"
311         buf += "        .set_default_node_attributes    = " + fabric_mod_name + "_set_default_node_attrs,\n"
312         buf += "        .get_cmd_state                  = " + fabric_mod_name + "_get_cmd_state,\n"
313         buf += "        .queue_data_in                  = " + fabric_mod_name + "_queue_data_in,\n"
314         buf += "        .queue_status                   = " + fabric_mod_name + "_queue_status,\n"
315         buf += "        .queue_tm_rsp                   = " + fabric_mod_name + "_queue_tm_rsp,\n"
316         buf += "        .aborted_task                   = " + fabric_mod_name + "_aborted_task,\n"
317         buf += "        /*\n"
318         buf += "         * Setup function pointers for generic logic in target_core_fabric_configfs.c\n"
319         buf += "         */\n"
320         buf += "        .fabric_make_wwn                = " + fabric_mod_name + "_make_" + fabric_mod_port + ",\n"
321         buf += "        .fabric_drop_wwn                = " + fabric_mod_name + "_drop_" + fabric_mod_port + ",\n"
322         buf += "        .fabric_make_tpg                = " + fabric_mod_name + "_make_tpg,\n"
323         buf += "        .fabric_drop_tpg                = " + fabric_mod_name + "_drop_tpg,\n"
324         buf += "\n"
325         buf += "        .tfc_wwn_attrs                  = " + fabric_mod_name + "_wwn_attrs;\n"
326         buf += "};\n\n"
327
328         buf += "static int __init " + fabric_mod_name + "_init(void)\n"
329         buf += "{\n"
330         buf += "        return target_register_template(" + fabric_mod_name + "_ops);\n"
331         buf += "};\n\n"
332
333         buf += "static void __exit " + fabric_mod_name + "_exit(void)\n"
334         buf += "{\n"
335         buf += "        target_unregister_template(" + fabric_mod_name + "_ops);\n"
336         buf += "};\n\n"
337
338         buf += "MODULE_DESCRIPTION(\"" + fabric_mod_name.upper() + " series fabric driver\");\n"
339         buf += "MODULE_LICENSE(\"GPL\");\n"
340         buf += "module_init(" + fabric_mod_name + "_init);\n"
341         buf += "module_exit(" + fabric_mod_name + "_exit);\n"
342
343         ret = p.write(buf)
344         if ret:
345                 tcm_mod_err("Unable to write f: " + f)
346
347         p.close()
348
349         return
350
351 def tcm_mod_scan_fabric_ops(tcm_dir):
352
353         fabric_ops_api = tcm_dir + "include/target/target_core_fabric.h"
354
355         print "Using tcm_mod_scan_fabric_ops: " + fabric_ops_api
356         process_fo = 0;
357
358         p = open(fabric_ops_api, 'r')
359
360         line = p.readline()
361         while line:
362                 if process_fo == 0 and re.search('struct target_core_fabric_ops {', line):
363                         line = p.readline()
364                         continue
365
366                 if process_fo == 0:
367                         process_fo = 1;
368                         line = p.readline()
369                         # Search for function pointer
370                         if not re.search('\(\*', line):
371                                 continue
372
373                         fabric_ops.append(line.rstrip())
374                         continue
375
376                 line = p.readline()
377                 # Search for function pointer
378                 if not re.search('\(\*', line):
379                         continue
380
381                 fabric_ops.append(line.rstrip())
382
383         p.close()
384         return
385
386 def tcm_mod_dump_fabric_ops(proto_ident, fabric_mod_dir_var, fabric_mod_name):
387         buf = ""
388         bufi = ""
389
390         f = fabric_mod_dir_var + "/" + fabric_mod_name + "_fabric.c"
391         print "Writing file: " + f
392
393         p = open(f, 'w')
394         if not p:
395                 tcm_mod_err("Unable to open file: " + f)
396
397         fi = fabric_mod_dir_var + "/" + fabric_mod_name + "_fabric.h"
398         print "Writing file: " + fi
399
400         pi = open(fi, 'w')
401         if not pi:
402                 tcm_mod_err("Unable to open file: " + fi)
403
404         buf = "#include <linux/slab.h>\n"
405         buf += "#include <linux/kthread.h>\n"
406         buf += "#include <linux/types.h>\n"
407         buf += "#include <linux/list.h>\n"
408         buf += "#include <linux/types.h>\n"
409         buf += "#include <linux/string.h>\n"
410         buf += "#include <linux/ctype.h>\n"
411         buf += "#include <asm/unaligned.h>\n"
412         buf += "#include <scsi/scsi.h>\n"
413         buf += "#include <scsi/scsi_host.h>\n"
414         buf += "#include <scsi/scsi_device.h>\n"
415         buf += "#include <scsi/scsi_cmnd.h>\n"
416         buf += "#include <scsi/libfc.h>\n\n"
417         buf += "#include <target/target_core_base.h>\n"
418         buf += "#include <target/target_core_fabric.h>\n"
419         buf += "#include \"" + fabric_mod_name + "_base.h\"\n"
420         buf += "#include \"" + fabric_mod_name + "_fabric.h\"\n\n"
421
422         buf += "int " + fabric_mod_name + "_check_true(struct se_portal_group *se_tpg)\n"
423         buf += "{\n"
424         buf += "        return 1;\n"
425         buf += "}\n\n"
426         bufi += "int " + fabric_mod_name + "_check_true(struct se_portal_group *);\n"
427
428         buf += "int " + fabric_mod_name + "_check_false(struct se_portal_group *se_tpg)\n"
429         buf += "{\n"
430         buf += "        return 0;\n"
431         buf += "}\n\n"
432         bufi += "int " + fabric_mod_name + "_check_false(struct se_portal_group *);\n"
433
434         total_fabric_ops = len(fabric_ops)
435         i = 0
436
437         while i < total_fabric_ops:
438                 fo = fabric_ops[i]
439                 i += 1
440 #               print "fabric_ops: " + fo
441
442                 if re.search('get_fabric_name', fo):
443                         buf += "char *" + fabric_mod_name + "_get_fabric_name(void)\n"
444                         buf += "{\n"
445                         buf += "        return \"" + fabric_mod_name + "\";\n"
446                         buf += "}\n\n"
447                         bufi += "char *" + fabric_mod_name + "_get_fabric_name(void);\n"
448                         continue
449
450                 if re.search('get_wwn', fo):
451                         buf += "char *" + fabric_mod_name + "_get_fabric_wwn(struct se_portal_group *se_tpg)\n"
452                         buf += "{\n"
453                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
454                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
455                         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n\n"
456                         buf += "        return &" + fabric_mod_port + "->" + fabric_mod_port + "_name[0];\n"
457                         buf += "}\n\n"
458                         bufi += "char *" + fabric_mod_name + "_get_fabric_wwn(struct se_portal_group *);\n"
459
460                 if re.search('get_tag', fo):
461                         buf += "u16 " + fabric_mod_name + "_get_tag(struct se_portal_group *se_tpg)\n"
462                         buf += "{\n"
463                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
464                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
465                         buf += "        return tpg->" + fabric_mod_port + "_tpgt;\n"
466                         buf += "}\n\n"
467                         bufi += "u16 " + fabric_mod_name + "_get_tag(struct se_portal_group *);\n"
468
469                 if re.search('tpg_get_inst_index\)\(', fo):
470                         buf += "u32 " + fabric_mod_name + "_tpg_get_inst_index(struct se_portal_group *se_tpg)\n"
471                         buf += "{\n"
472                         buf += "        return 1;\n"
473                         buf += "}\n\n"
474                         bufi += "u32 " + fabric_mod_name + "_tpg_get_inst_index(struct se_portal_group *);\n"
475
476                 if re.search('\*release_cmd\)\(', fo):
477                         buf += "void " + fabric_mod_name + "_release_cmd(struct se_cmd *se_cmd)\n"
478                         buf += "{\n"
479                         buf += "        return;\n"
480                         buf += "}\n\n"
481                         bufi += "void " + fabric_mod_name + "_release_cmd(struct se_cmd *);\n"
482
483                 if re.search('shutdown_session\)\(', fo):
484                         buf += "int " + fabric_mod_name + "_shutdown_session(struct se_session *se_sess)\n"
485                         buf += "{\n"
486                         buf += "        return 0;\n"
487                         buf += "}\n\n"
488                         bufi += "int " + fabric_mod_name + "_shutdown_session(struct se_session *);\n"
489
490                 if re.search('close_session\)\(', fo):
491                         buf += "void " + fabric_mod_name + "_close_session(struct se_session *se_sess)\n"
492                         buf += "{\n"
493                         buf += "        return;\n"
494                         buf += "}\n\n"
495                         bufi += "void " + fabric_mod_name + "_close_session(struct se_session *);\n"
496
497                 if re.search('sess_get_index\)\(', fo):
498                         buf += "u32 " + fabric_mod_name + "_sess_get_index(struct se_session *se_sess)\n"
499                         buf += "{\n"
500                         buf += "        return 0;\n"
501                         buf += "}\n\n"
502                         bufi += "u32 " + fabric_mod_name + "_sess_get_index(struct se_session *);\n"
503
504                 if re.search('write_pending\)\(', fo):
505                         buf += "int " + fabric_mod_name + "_write_pending(struct se_cmd *se_cmd)\n"
506                         buf += "{\n"
507                         buf += "        return 0;\n"
508                         buf += "}\n\n"
509                         bufi += "int " + fabric_mod_name + "_write_pending(struct se_cmd *);\n"
510
511                 if re.search('write_pending_status\)\(', fo):
512                         buf += "int " + fabric_mod_name + "_write_pending_status(struct se_cmd *se_cmd)\n"
513                         buf += "{\n"
514                         buf += "        return 0;\n"
515                         buf += "}\n\n"
516                         bufi += "int " + fabric_mod_name + "_write_pending_status(struct se_cmd *);\n"
517
518                 if re.search('set_default_node_attributes\)\(', fo):
519                         buf += "void " + fabric_mod_name + "_set_default_node_attrs(struct se_node_acl *nacl)\n"
520                         buf += "{\n"
521                         buf += "        return;\n"
522                         buf += "}\n\n"
523                         bufi += "void " + fabric_mod_name + "_set_default_node_attrs(struct se_node_acl *);\n"
524
525                 if re.search('get_cmd_state\)\(', fo):
526                         buf += "int " + fabric_mod_name + "_get_cmd_state(struct se_cmd *se_cmd)\n"
527                         buf += "{\n"
528                         buf += "        return 0;\n"
529                         buf += "}\n\n"
530                         bufi += "int " + fabric_mod_name + "_get_cmd_state(struct se_cmd *);\n"
531
532                 if re.search('queue_data_in\)\(', fo):
533                         buf += "int " + fabric_mod_name + "_queue_data_in(struct se_cmd *se_cmd)\n"
534                         buf += "{\n"
535                         buf += "        return 0;\n"
536                         buf += "}\n\n"
537                         bufi += "int " + fabric_mod_name + "_queue_data_in(struct se_cmd *);\n"
538
539                 if re.search('queue_status\)\(', fo):
540                         buf += "int " + fabric_mod_name + "_queue_status(struct se_cmd *se_cmd)\n"
541                         buf += "{\n"
542                         buf += "        return 0;\n"
543                         buf += "}\n\n"
544                         bufi += "int " + fabric_mod_name + "_queue_status(struct se_cmd *);\n"
545
546                 if re.search('queue_tm_rsp\)\(', fo):
547                         buf += "void " + fabric_mod_name + "_queue_tm_rsp(struct se_cmd *se_cmd)\n"
548                         buf += "{\n"
549                         buf += "        return;\n"
550                         buf += "}\n\n"
551                         bufi += "void " + fabric_mod_name + "_queue_tm_rsp(struct se_cmd *);\n"
552
553                 if re.search('aborted_task\)\(', fo):
554                         buf += "void " + fabric_mod_name + "_aborted_task(struct se_cmd *se_cmd)\n"
555                         buf += "{\n"
556                         buf += "        return;\n"
557                         buf += "}\n\n"
558                         bufi += "void " + fabric_mod_name + "_aborted_task(struct se_cmd *);\n"
559
560         ret = p.write(buf)
561         if ret:
562                 tcm_mod_err("Unable to write f: " + f)
563
564         p.close()
565
566         ret = pi.write(bufi)
567         if ret:
568                 tcm_mod_err("Unable to write fi: " + fi)
569
570         pi.close()
571         return
572
573 def tcm_mod_build_kbuild(fabric_mod_dir_var, fabric_mod_name):
574
575         buf = ""
576         f = fabric_mod_dir_var + "/Makefile"
577         print "Writing file: " + f
578
579         p = open(f, 'w')
580         if not p:
581                 tcm_mod_err("Unable to open file: " + f)
582
583         buf += fabric_mod_name + "-objs                 := " + fabric_mod_name + "_fabric.o \\\n"
584         buf += "                                           " + fabric_mod_name + "_configfs.o\n"
585         buf += "obj-$(CONFIG_" + fabric_mod_name.upper() + ")           += " + fabric_mod_name + ".o\n"
586
587         ret = p.write(buf)
588         if ret:
589                 tcm_mod_err("Unable to write f: " + f)
590
591         p.close()
592         return
593
594 def tcm_mod_build_kconfig(fabric_mod_dir_var, fabric_mod_name):
595
596         buf = ""
597         f = fabric_mod_dir_var + "/Kconfig"
598         print "Writing file: " + f
599
600         p = open(f, 'w')
601         if not p:
602                 tcm_mod_err("Unable to open file: " + f)
603
604         buf = "config " + fabric_mod_name.upper() + "\n"
605         buf += "        tristate \"" + fabric_mod_name.upper() + " fabric module\"\n"
606         buf += "        depends on TARGET_CORE && CONFIGFS_FS\n"
607         buf += "        default n\n"
608         buf += "        ---help---\n"
609         buf += "        Say Y here to enable the " + fabric_mod_name.upper() + " fabric module\n"
610
611         ret = p.write(buf)
612         if ret:
613                 tcm_mod_err("Unable to write f: " + f)
614
615         p.close()
616         return
617
618 def tcm_mod_add_kbuild(tcm_dir, fabric_mod_name):
619         buf = "obj-$(CONFIG_" + fabric_mod_name.upper() + ")    += " + fabric_mod_name.lower() + "/\n"
620         kbuild = tcm_dir + "/drivers/target/Makefile"
621
622         f = open(kbuild, 'a')
623         f.write(buf)
624         f.close()
625         return
626
627 def tcm_mod_add_kconfig(tcm_dir, fabric_mod_name):
628         buf = "source \"drivers/target/" + fabric_mod_name.lower() + "/Kconfig\"\n"
629         kconfig = tcm_dir + "/drivers/target/Kconfig"
630
631         f = open(kconfig, 'a')
632         f.write(buf)
633         f.close()
634         return
635
636 def main(modname, proto_ident):
637 #       proto_ident = "FC"
638 #       proto_ident = "SAS"
639 #       proto_ident = "iSCSI"
640
641         tcm_dir = os.getcwd();
642         tcm_dir += "/../../"
643         print "tcm_dir: " + tcm_dir
644         fabric_mod_name = modname
645         fabric_mod_dir = tcm_dir + "drivers/target/" + fabric_mod_name
646         print "Set fabric_mod_name: " + fabric_mod_name
647         print "Set fabric_mod_dir: " + fabric_mod_dir
648         print "Using proto_ident: " + proto_ident
649
650         if proto_ident != "FC" and proto_ident != "SAS" and proto_ident != "iSCSI":
651                 print "Unsupported proto_ident: " + proto_ident
652                 sys.exit(1)
653
654         ret = tcm_mod_create_module_subdir(fabric_mod_dir)
655         if ret:
656                 print "tcm_mod_create_module_subdir() failed because module already exists!"
657                 sys.exit(1)
658
659         tcm_mod_build_base_includes(proto_ident, fabric_mod_dir, fabric_mod_name)
660         tcm_mod_scan_fabric_ops(tcm_dir)
661         tcm_mod_dump_fabric_ops(proto_ident, fabric_mod_dir, fabric_mod_name)
662         tcm_mod_build_configfs(proto_ident, fabric_mod_dir, fabric_mod_name)
663         tcm_mod_build_kbuild(fabric_mod_dir, fabric_mod_name)
664         tcm_mod_build_kconfig(fabric_mod_dir, fabric_mod_name)
665
666         input = raw_input("Would you like to add " + fabric_mod_name + " to drivers/target/Makefile..? [yes,no]: ")
667         if input == "yes" or input == "y":
668                 tcm_mod_add_kbuild(tcm_dir, fabric_mod_name)
669
670         input = raw_input("Would you like to add " + fabric_mod_name + " to drivers/target/Kconfig..? [yes,no]: ")
671         if input == "yes" or input == "y":
672                 tcm_mod_add_kconfig(tcm_dir, fabric_mod_name)
673
674         return
675
676 parser = optparse.OptionParser()
677 parser.add_option('-m', '--modulename', help='Module name', dest='modname',
678                 action='store', nargs=1, type='string')
679 parser.add_option('-p', '--protoident', help='Protocol Ident', dest='protoident',
680                 action='store', nargs=1, type='string')
681
682 (opts, args) = parser.parse_args()
683
684 mandatories = ['modname', 'protoident']
685 for m in mandatories:
686         if not opts.__dict__[m]:
687                 print "mandatory option is missing\n"
688                 parser.print_help()
689                 exit(-1)
690
691 if __name__ == "__main__":
692
693         main(str(opts.modname), opts.protoident)