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