]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - Documentation/target/tcm_mod_builder.py
target: refactor init/drop_nodeacl methods
[karo-tx-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 + "_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_alloc_fabric_acl           = " + fabric_mod_name + "_alloc_fabric_acl,\n"
317         buf += "        .tpg_release_fabric_acl         = " + fabric_mod_name + "_release_fabric_acl,\n"
318         buf += "        .tpg_get_inst_index             = " + fabric_mod_name + "_tpg_get_inst_index,\n"
319         buf += "        .release_cmd                    = " + fabric_mod_name + "_release_cmd,\n"
320         buf += "        .shutdown_session               = " + fabric_mod_name + "_shutdown_session,\n"
321         buf += "        .close_session                  = " + fabric_mod_name + "_close_session,\n"
322         buf += "        .sess_get_index                 = " + fabric_mod_name + "_sess_get_index,\n"
323         buf += "        .sess_get_initiator_sid         = NULL,\n"
324         buf += "        .write_pending                  = " + fabric_mod_name + "_write_pending,\n"
325         buf += "        .write_pending_status           = " + fabric_mod_name + "_write_pending_status,\n"
326         buf += "        .set_default_node_attributes    = " + fabric_mod_name + "_set_default_node_attrs,\n"
327         buf += "        .get_task_tag                   = " + fabric_mod_name + "_get_task_tag,\n"
328         buf += "        .get_cmd_state                  = " + fabric_mod_name + "_get_cmd_state,\n"
329         buf += "        .queue_data_in                  = " + fabric_mod_name + "_queue_data_in,\n"
330         buf += "        .queue_status                   = " + fabric_mod_name + "_queue_status,\n"
331         buf += "        .queue_tm_rsp                   = " + fabric_mod_name + "_queue_tm_rsp,\n"
332         buf += "        .aborted_task                   = " + fabric_mod_name + "_aborted_task,\n"
333         buf += "        /*\n"
334         buf += "         * Setup function pointers for generic logic in target_core_fabric_configfs.c\n"
335         buf += "         */\n"
336         buf += "        .fabric_make_wwn                = " + fabric_mod_name + "_make_" + fabric_mod_port + ",\n"
337         buf += "        .fabric_drop_wwn                = " + fabric_mod_name + "_drop_" + fabric_mod_port + ",\n"
338         buf += "        .fabric_make_tpg                = " + fabric_mod_name + "_make_tpg,\n"
339         buf += "        .fabric_drop_tpg                = " + fabric_mod_name + "_drop_tpg,\n"
340         buf += "\n"
341         buf += "        .tfc_wwn_attrs                  = " + fabric_mod_name + "_wwn_attrs;\n"
342         buf += "};\n\n"
343
344         buf += "static int __init " + fabric_mod_name + "_init(void)\n"
345         buf += "{\n"
346         buf += "        return target_register_template(" + fabric_mod_name + "_ops);\n"
347         buf += "};\n\n"
348
349         buf += "static void __exit " + fabric_mod_name + "_exit(void)\n"
350         buf += "{\n"
351         buf += "        target_unregister_template(" + fabric_mod_name + "_ops);\n"
352         buf += "};\n\n"
353
354         buf += "MODULE_DESCRIPTION(\"" + fabric_mod_name.upper() + " series fabric driver\");\n"
355         buf += "MODULE_LICENSE(\"GPL\");\n"
356         buf += "module_init(" + fabric_mod_name + "_init);\n"
357         buf += "module_exit(" + fabric_mod_name + "_exit);\n"
358
359         ret = p.write(buf)
360         if ret:
361                 tcm_mod_err("Unable to write f: " + f)
362
363         p.close()
364
365         return
366
367 def tcm_mod_scan_fabric_ops(tcm_dir):
368
369         fabric_ops_api = tcm_dir + "include/target/target_core_fabric.h"
370
371         print "Using tcm_mod_scan_fabric_ops: " + fabric_ops_api
372         process_fo = 0;
373
374         p = open(fabric_ops_api, 'r')
375
376         line = p.readline()
377         while line:
378                 if process_fo == 0 and re.search('struct target_core_fabric_ops {', line):
379                         line = p.readline()
380                         continue
381
382                 if process_fo == 0:
383                         process_fo = 1;
384                         line = p.readline()
385                         # Search for function pointer
386                         if not re.search('\(\*', line):
387                                 continue
388
389                         fabric_ops.append(line.rstrip())
390                         continue
391
392                 line = p.readline()
393                 # Search for function pointer
394                 if not re.search('\(\*', line):
395                         continue
396
397                 fabric_ops.append(line.rstrip())
398
399         p.close()
400         return
401
402 def tcm_mod_dump_fabric_ops(proto_ident, fabric_mod_dir_var, fabric_mod_name):
403         buf = ""
404         bufi = ""
405
406         f = fabric_mod_dir_var + "/" + fabric_mod_name + "_fabric.c"
407         print "Writing file: " + f
408
409         p = open(f, 'w')
410         if not p:
411                 tcm_mod_err("Unable to open file: " + f)
412
413         fi = fabric_mod_dir_var + "/" + fabric_mod_name + "_fabric.h"
414         print "Writing file: " + fi
415
416         pi = open(fi, 'w')
417         if not pi:
418                 tcm_mod_err("Unable to open file: " + fi)
419
420         buf = "#include <linux/slab.h>\n"
421         buf += "#include <linux/kthread.h>\n"
422         buf += "#include <linux/types.h>\n"
423         buf += "#include <linux/list.h>\n"
424         buf += "#include <linux/types.h>\n"
425         buf += "#include <linux/string.h>\n"
426         buf += "#include <linux/ctype.h>\n"
427         buf += "#include <asm/unaligned.h>\n"
428         buf += "#include <scsi/scsi.h>\n"
429         buf += "#include <scsi/scsi_host.h>\n"
430         buf += "#include <scsi/scsi_device.h>\n"
431         buf += "#include <scsi/scsi_cmnd.h>\n"
432         buf += "#include <scsi/libfc.h>\n\n"
433         buf += "#include <target/target_core_base.h>\n"
434         buf += "#include <target/target_core_fabric.h>\n"
435         buf += "#include <target/target_core_configfs.h>\n\n"
436         buf += "#include \"" + fabric_mod_name + "_base.h\"\n"
437         buf += "#include \"" + fabric_mod_name + "_fabric.h\"\n\n"
438
439         buf += "int " + fabric_mod_name + "_check_true(struct se_portal_group *se_tpg)\n"
440         buf += "{\n"
441         buf += "        return 1;\n"
442         buf += "}\n\n"
443         bufi += "int " + fabric_mod_name + "_check_true(struct se_portal_group *);\n"
444
445         buf += "int " + fabric_mod_name + "_check_false(struct se_portal_group *se_tpg)\n"
446         buf += "{\n"
447         buf += "        return 0;\n"
448         buf += "}\n\n"
449         bufi += "int " + fabric_mod_name + "_check_false(struct se_portal_group *);\n"
450
451         total_fabric_ops = len(fabric_ops)
452         i = 0
453
454         while i < total_fabric_ops:
455                 fo = fabric_ops[i]
456                 i += 1
457 #               print "fabric_ops: " + fo
458
459                 if re.search('get_fabric_name', fo):
460                         buf += "char *" + fabric_mod_name + "_get_fabric_name(void)\n"
461                         buf += "{\n"
462                         buf += "        return \"" + fabric_mod_name + "\";\n"
463                         buf += "}\n\n"
464                         bufi += "char *" + fabric_mod_name + "_get_fabric_name(void);\n"
465                         continue
466
467                 if re.search('get_fabric_proto_ident', fo):
468                         buf += "u8 " + fabric_mod_name + "_get_fabric_proto_ident(struct se_portal_group *se_tpg)\n"
469                         buf += "{\n"
470                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
471                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
472                         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n"
473                         buf += "        u8 proto_id;\n\n"
474                         buf += "        switch (" + fabric_mod_port + "->" + fabric_mod_port + "_proto_id) {\n"
475                         if proto_ident == "FC":
476                                 buf += "        case SCSI_PROTOCOL_FCP:\n"
477                                 buf += "        default:\n"
478                                 buf += "                proto_id = fc_get_fabric_proto_ident(se_tpg);\n"
479                                 buf += "                break;\n"
480                         elif proto_ident == "SAS":
481                                 buf += "        case SCSI_PROTOCOL_SAS:\n"
482                                 buf += "        default:\n"
483                                 buf += "                proto_id = sas_get_fabric_proto_ident(se_tpg);\n"
484                                 buf += "                break;\n"
485                         elif proto_ident == "iSCSI":
486                                 buf += "        case SCSI_PROTOCOL_ISCSI:\n"
487                                 buf += "        default:\n"
488                                 buf += "                proto_id = iscsi_get_fabric_proto_ident(se_tpg);\n"
489                                 buf += "                break;\n"
490
491                         buf += "        }\n\n"
492                         buf += "        return proto_id;\n"
493                         buf += "}\n\n"
494                         bufi += "u8 " + fabric_mod_name + "_get_fabric_proto_ident(struct se_portal_group *);\n"
495
496                 if re.search('get_wwn', fo):
497                         buf += "char *" + fabric_mod_name + "_get_fabric_wwn(struct se_portal_group *se_tpg)\n"
498                         buf += "{\n"
499                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
500                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
501                         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n\n"
502                         buf += "        return &" + fabric_mod_port + "->" + fabric_mod_port + "_name[0];\n"
503                         buf += "}\n\n"
504                         bufi += "char *" + fabric_mod_name + "_get_fabric_wwn(struct se_portal_group *);\n"
505
506                 if re.search('get_tag', fo):
507                         buf += "u16 " + fabric_mod_name + "_get_tag(struct se_portal_group *se_tpg)\n"
508                         buf += "{\n"
509                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
510                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
511                         buf += "        return tpg->" + fabric_mod_port + "_tpgt;\n"
512                         buf += "}\n\n"
513                         bufi += "u16 " + fabric_mod_name + "_get_tag(struct se_portal_group *);\n"
514
515                 if re.search('get_pr_transport_id\)\(', fo):
516                         buf += "u32 " + fabric_mod_name + "_get_pr_transport_id(\n"
517                         buf += "        struct se_portal_group *se_tpg,\n"
518                         buf += "        struct se_node_acl *se_nacl,\n"
519                         buf += "        struct t10_pr_registration *pr_reg,\n"
520                         buf += "        int *format_code,\n"
521                         buf += "        unsigned char *buf)\n"
522                         buf += "{\n"
523                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
524                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
525                         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n"
526                         buf += "        int ret = 0;\n\n"
527                         buf += "        switch (" + fabric_mod_port + "->" + fabric_mod_port + "_proto_id) {\n"
528                         if proto_ident == "FC":
529                                 buf += "        case SCSI_PROTOCOL_FCP:\n"
530                                 buf += "        default:\n"
531                                 buf += "                ret = fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,\n"
532                                 buf += "                                        format_code, buf);\n"
533                                 buf += "                break;\n"
534                         elif proto_ident == "SAS":
535                                 buf += "        case SCSI_PROTOCOL_SAS:\n"
536                                 buf += "        default:\n"
537                                 buf += "                ret = sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,\n"
538                                 buf += "                                        format_code, buf);\n"
539                                 buf += "                break;\n"
540                         elif proto_ident == "iSCSI":
541                                 buf += "        case SCSI_PROTOCOL_ISCSI:\n"
542                                 buf += "        default:\n"
543                                 buf += "                ret = iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,\n"
544                                 buf += "                                        format_code, buf);\n"
545                                 buf += "                break;\n"
546
547                         buf += "        }\n\n"
548                         buf += "        return ret;\n"
549                         buf += "}\n\n"
550                         bufi += "u32 " + fabric_mod_name + "_get_pr_transport_id(struct se_portal_group *,\n"
551                         bufi += "                       struct se_node_acl *, struct t10_pr_registration *,\n"
552                         bufi += "                       int *, unsigned char *);\n"
553
554                 if re.search('get_pr_transport_id_len\)\(', fo):
555                         buf += "u32 " + fabric_mod_name + "_get_pr_transport_id_len(\n"
556                         buf += "        struct se_portal_group *se_tpg,\n"
557                         buf += "        struct se_node_acl *se_nacl,\n"
558                         buf += "        struct t10_pr_registration *pr_reg,\n"
559                         buf += "        int *format_code)\n"
560                         buf += "{\n"
561                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
562                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
563                         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n"
564                         buf += "        int ret = 0;\n\n"
565                         buf += "        switch (" + fabric_mod_port + "->" + fabric_mod_port + "_proto_id) {\n"
566                         if proto_ident == "FC":
567                                 buf += "        case SCSI_PROTOCOL_FCP:\n"
568                                 buf += "        default:\n"
569                                 buf += "                ret = fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,\n"
570                                 buf += "                                        format_code);\n"
571                                 buf += "                break;\n"
572                         elif proto_ident == "SAS":
573                                 buf += "        case SCSI_PROTOCOL_SAS:\n"
574                                 buf += "        default:\n"
575                                 buf += "                ret = sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,\n"
576                                 buf += "                                        format_code);\n"
577                                 buf += "                break;\n"
578                         elif proto_ident == "iSCSI":
579                                 buf += "        case SCSI_PROTOCOL_ISCSI:\n"
580                                 buf += "        default:\n"
581                                 buf += "                ret = iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,\n"
582                                 buf += "                                        format_code);\n"
583                                 buf += "                break;\n"
584
585
586                         buf += "        }\n\n"
587                         buf += "        return ret;\n"
588                         buf += "}\n\n"
589                         bufi += "u32 " + fabric_mod_name + "_get_pr_transport_id_len(struct se_portal_group *,\n"
590                         bufi += "                       struct se_node_acl *, struct t10_pr_registration *,\n"
591                         bufi += "                       int *);\n"
592
593                 if re.search('parse_pr_out_transport_id\)\(', fo):
594                         buf += "char *" + fabric_mod_name + "_parse_pr_out_transport_id(\n"
595                         buf += "        struct se_portal_group *se_tpg,\n"
596                         buf += "        const char *buf,\n"
597                         buf += "        u32 *out_tid_len,\n"
598                         buf += "        char **port_nexus_ptr)\n"
599                         buf += "{\n"
600                         buf += "        struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
601                         buf += "                                struct " + fabric_mod_name + "_tpg, se_tpg);\n"
602                         buf += "        struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n"
603                         buf += "        char *tid = NULL;\n\n"
604                         buf += "        switch (" + fabric_mod_port + "->" + fabric_mod_port + "_proto_id) {\n"
605                         if proto_ident == "FC":
606                                 buf += "        case SCSI_PROTOCOL_FCP:\n"
607                                 buf += "        default:\n"
608                                 buf += "                tid = fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,\n"
609                                 buf += "                                        port_nexus_ptr);\n"
610                         elif proto_ident == "SAS":
611                                 buf += "        case SCSI_PROTOCOL_SAS:\n"
612                                 buf += "        default:\n"
613                                 buf += "                tid = sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,\n"
614                                 buf += "                                        port_nexus_ptr);\n"
615                         elif proto_ident == "iSCSI":
616                                 buf += "        case SCSI_PROTOCOL_ISCSI:\n"
617                                 buf += "        default:\n"
618                                 buf += "                tid = iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,\n"
619                                 buf += "                                        port_nexus_ptr);\n"
620
621                         buf += "        }\n\n"
622                         buf += "        return tid;\n"
623                         buf += "}\n\n"
624                         bufi += "char *" + fabric_mod_name + "_parse_pr_out_transport_id(struct se_portal_group *,\n"
625                         bufi += "                       const char *, u32 *, char **);\n"
626
627                 if re.search('alloc_fabric_acl\)\(', fo):
628                         buf += "struct se_node_acl *" + fabric_mod_name + "_alloc_fabric_acl(struct se_portal_group *se_tpg)\n"
629                         buf += "{\n"
630                         buf += "        struct " + fabric_mod_name + "_nacl *nacl;\n\n"
631                         buf += "        nacl = kzalloc(sizeof(struct " + fabric_mod_name + "_nacl), GFP_KERNEL);\n"
632                         buf += "        if (!nacl) {\n"
633                         buf += "                printk(KERN_ERR \"Unable to allocate struct " + fabric_mod_name + "_nacl\\n\");\n"
634                         buf += "                return NULL;\n"
635                         buf += "        }\n\n"
636                         buf += "        return &nacl->se_node_acl;\n"
637                         buf += "}\n\n"
638                         bufi += "struct se_node_acl *" + fabric_mod_name + "_alloc_fabric_acl(struct se_portal_group *);\n"
639
640                 if re.search('release_fabric_acl\)\(', fo):
641                         buf += "void " + fabric_mod_name + "_release_fabric_acl(\n"
642                         buf += "        struct se_portal_group *se_tpg,\n"
643                         buf += "        struct se_node_acl *se_nacl)\n"
644                         buf += "{\n"
645                         buf += "        struct " + fabric_mod_name + "_nacl *nacl = container_of(se_nacl,\n"
646                         buf += "                        struct " + fabric_mod_name + "_nacl, se_node_acl);\n"
647                         buf += "        kfree(nacl);\n"
648                         buf += "}\n\n"
649                         bufi += "void " + fabric_mod_name + "_release_fabric_acl(struct se_portal_group *,\n"
650                         bufi += "                       struct se_node_acl *);\n"
651
652                 if re.search('tpg_get_inst_index\)\(', fo):
653                         buf += "u32 " + fabric_mod_name + "_tpg_get_inst_index(struct se_portal_group *se_tpg)\n"
654                         buf += "{\n"
655                         buf += "        return 1;\n"
656                         buf += "}\n\n"
657                         bufi += "u32 " + fabric_mod_name + "_tpg_get_inst_index(struct se_portal_group *);\n"
658
659                 if re.search('\*release_cmd\)\(', fo):
660                         buf += "void " + fabric_mod_name + "_release_cmd(struct se_cmd *se_cmd)\n"
661                         buf += "{\n"
662                         buf += "        return;\n"
663                         buf += "}\n\n"
664                         bufi += "void " + fabric_mod_name + "_release_cmd(struct se_cmd *);\n"
665
666                 if re.search('shutdown_session\)\(', fo):
667                         buf += "int " + fabric_mod_name + "_shutdown_session(struct se_session *se_sess)\n"
668                         buf += "{\n"
669                         buf += "        return 0;\n"
670                         buf += "}\n\n"
671                         bufi += "int " + fabric_mod_name + "_shutdown_session(struct se_session *);\n"
672
673                 if re.search('close_session\)\(', fo):
674                         buf += "void " + fabric_mod_name + "_close_session(struct se_session *se_sess)\n"
675                         buf += "{\n"
676                         buf += "        return;\n"
677                         buf += "}\n\n"
678                         bufi += "void " + fabric_mod_name + "_close_session(struct se_session *);\n"
679
680                 if re.search('sess_get_index\)\(', fo):
681                         buf += "u32 " + fabric_mod_name + "_sess_get_index(struct se_session *se_sess)\n"
682                         buf += "{\n"
683                         buf += "        return 0;\n"
684                         buf += "}\n\n"
685                         bufi += "u32 " + fabric_mod_name + "_sess_get_index(struct se_session *);\n"
686
687                 if re.search('write_pending\)\(', fo):
688                         buf += "int " + fabric_mod_name + "_write_pending(struct se_cmd *se_cmd)\n"
689                         buf += "{\n"
690                         buf += "        return 0;\n"
691                         buf += "}\n\n"
692                         bufi += "int " + fabric_mod_name + "_write_pending(struct se_cmd *);\n"
693
694                 if re.search('write_pending_status\)\(', fo):
695                         buf += "int " + fabric_mod_name + "_write_pending_status(struct se_cmd *se_cmd)\n"
696                         buf += "{\n"
697                         buf += "        return 0;\n"
698                         buf += "}\n\n"
699                         bufi += "int " + fabric_mod_name + "_write_pending_status(struct se_cmd *);\n"
700
701                 if re.search('set_default_node_attributes\)\(', fo):
702                         buf += "void " + fabric_mod_name + "_set_default_node_attrs(struct se_node_acl *nacl)\n"
703                         buf += "{\n"
704                         buf += "        return;\n"
705                         buf += "}\n\n"
706                         bufi += "void " + fabric_mod_name + "_set_default_node_attrs(struct se_node_acl *);\n"
707
708                 if re.search('get_task_tag\)\(', fo):
709                         buf += "u32 " + fabric_mod_name + "_get_task_tag(struct se_cmd *se_cmd)\n"
710                         buf += "{\n"
711                         buf += "        return 0;\n"
712                         buf += "}\n\n"
713                         bufi += "u32 " + fabric_mod_name + "_get_task_tag(struct se_cmd *);\n"
714
715                 if re.search('get_cmd_state\)\(', fo):
716                         buf += "int " + fabric_mod_name + "_get_cmd_state(struct se_cmd *se_cmd)\n"
717                         buf += "{\n"
718                         buf += "        return 0;\n"
719                         buf += "}\n\n"
720                         bufi += "int " + fabric_mod_name + "_get_cmd_state(struct se_cmd *);\n"
721
722                 if re.search('queue_data_in\)\(', fo):
723                         buf += "int " + fabric_mod_name + "_queue_data_in(struct se_cmd *se_cmd)\n"
724                         buf += "{\n"
725                         buf += "        return 0;\n"
726                         buf += "}\n\n"
727                         bufi += "int " + fabric_mod_name + "_queue_data_in(struct se_cmd *);\n"
728
729                 if re.search('queue_status\)\(', fo):
730                         buf += "int " + fabric_mod_name + "_queue_status(struct se_cmd *se_cmd)\n"
731                         buf += "{\n"
732                         buf += "        return 0;\n"
733                         buf += "}\n\n"
734                         bufi += "int " + fabric_mod_name + "_queue_status(struct se_cmd *);\n"
735
736                 if re.search('queue_tm_rsp\)\(', fo):
737                         buf += "void " + fabric_mod_name + "_queue_tm_rsp(struct se_cmd *se_cmd)\n"
738                         buf += "{\n"
739                         buf += "        return;\n"
740                         buf += "}\n\n"
741                         bufi += "void " + fabric_mod_name + "_queue_tm_rsp(struct se_cmd *);\n"
742
743                 if re.search('aborted_task\)\(', fo):
744                         buf += "void " + fabric_mod_name + "_aborted_task(struct se_cmd *se_cmd)\n"
745                         buf += "{\n"
746                         buf += "        return;\n"
747                         buf += "}\n\n"
748                         bufi += "void " + fabric_mod_name + "_aborted_task(struct se_cmd *);\n"
749
750         ret = p.write(buf)
751         if ret:
752                 tcm_mod_err("Unable to write f: " + f)
753
754         p.close()
755
756         ret = pi.write(bufi)
757         if ret:
758                 tcm_mod_err("Unable to write fi: " + fi)
759
760         pi.close()
761         return
762
763 def tcm_mod_build_kbuild(fabric_mod_dir_var, fabric_mod_name):
764
765         buf = ""
766         f = fabric_mod_dir_var + "/Makefile"
767         print "Writing file: " + f
768
769         p = open(f, 'w')
770         if not p:
771                 tcm_mod_err("Unable to open file: " + f)
772
773         buf += fabric_mod_name + "-objs                 := " + fabric_mod_name + "_fabric.o \\\n"
774         buf += "                                           " + fabric_mod_name + "_configfs.o\n"
775         buf += "obj-$(CONFIG_" + fabric_mod_name.upper() + ")           += " + fabric_mod_name + ".o\n"
776
777         ret = p.write(buf)
778         if ret:
779                 tcm_mod_err("Unable to write f: " + f)
780
781         p.close()
782         return
783
784 def tcm_mod_build_kconfig(fabric_mod_dir_var, fabric_mod_name):
785
786         buf = ""
787         f = fabric_mod_dir_var + "/Kconfig"
788         print "Writing file: " + f
789
790         p = open(f, 'w')
791         if not p:
792                 tcm_mod_err("Unable to open file: " + f)
793
794         buf = "config " + fabric_mod_name.upper() + "\n"
795         buf += "        tristate \"" + fabric_mod_name.upper() + " fabric module\"\n"
796         buf += "        depends on TARGET_CORE && CONFIGFS_FS\n"
797         buf += "        default n\n"
798         buf += "        ---help---\n"
799         buf += "        Say Y here to enable the " + fabric_mod_name.upper() + " fabric module\n"
800
801         ret = p.write(buf)
802         if ret:
803                 tcm_mod_err("Unable to write f: " + f)
804
805         p.close()
806         return
807
808 def tcm_mod_add_kbuild(tcm_dir, fabric_mod_name):
809         buf = "obj-$(CONFIG_" + fabric_mod_name.upper() + ")    += " + fabric_mod_name.lower() + "/\n"
810         kbuild = tcm_dir + "/drivers/target/Makefile"
811
812         f = open(kbuild, 'a')
813         f.write(buf)
814         f.close()
815         return
816
817 def tcm_mod_add_kconfig(tcm_dir, fabric_mod_name):
818         buf = "source \"drivers/target/" + fabric_mod_name.lower() + "/Kconfig\"\n"
819         kconfig = tcm_dir + "/drivers/target/Kconfig"
820
821         f = open(kconfig, 'a')
822         f.write(buf)
823         f.close()
824         return
825
826 def main(modname, proto_ident):
827 #       proto_ident = "FC"
828 #       proto_ident = "SAS"
829 #       proto_ident = "iSCSI"
830
831         tcm_dir = os.getcwd();
832         tcm_dir += "/../../"
833         print "tcm_dir: " + tcm_dir
834         fabric_mod_name = modname
835         fabric_mod_dir = tcm_dir + "drivers/target/" + fabric_mod_name
836         print "Set fabric_mod_name: " + fabric_mod_name
837         print "Set fabric_mod_dir: " + fabric_mod_dir
838         print "Using proto_ident: " + proto_ident
839
840         if proto_ident != "FC" and proto_ident != "SAS" and proto_ident != "iSCSI":
841                 print "Unsupported proto_ident: " + proto_ident
842                 sys.exit(1)
843
844         ret = tcm_mod_create_module_subdir(fabric_mod_dir)
845         if ret:
846                 print "tcm_mod_create_module_subdir() failed because module already exists!"
847                 sys.exit(1)
848
849         tcm_mod_build_base_includes(proto_ident, fabric_mod_dir, fabric_mod_name)
850         tcm_mod_scan_fabric_ops(tcm_dir)
851         tcm_mod_dump_fabric_ops(proto_ident, fabric_mod_dir, fabric_mod_name)
852         tcm_mod_build_configfs(proto_ident, fabric_mod_dir, fabric_mod_name)
853         tcm_mod_build_kbuild(fabric_mod_dir, fabric_mod_name)
854         tcm_mod_build_kconfig(fabric_mod_dir, fabric_mod_name)
855
856         input = raw_input("Would you like to add " + fabric_mod_name + " to drivers/target/Makefile..? [yes,no]: ")
857         if input == "yes" or input == "y":
858                 tcm_mod_add_kbuild(tcm_dir, fabric_mod_name)
859
860         input = raw_input("Would you like to add " + fabric_mod_name + " to drivers/target/Kconfig..? [yes,no]: ")
861         if input == "yes" or input == "y":
862                 tcm_mod_add_kconfig(tcm_dir, fabric_mod_name)
863
864         return
865
866 parser = optparse.OptionParser()
867 parser.add_option('-m', '--modulename', help='Module name', dest='modname',
868                 action='store', nargs=1, type='string')
869 parser.add_option('-p', '--protoident', help='Protocol Ident', dest='protoident',
870                 action='store', nargs=1, type='string')
871
872 (opts, args) = parser.parse_args()
873
874 mandatories = ['modname', 'protoident']
875 for m in mandatories:
876         if not opts.__dict__[m]:
877                 print "mandatory option is missing\n"
878                 parser.print_help()
879                 exit(-1)
880
881 if __name__ == "__main__":
882
883         main(str(opts.modname), opts.protoident)