blob: cda56df9b8a7ce591f3eb254cad1d423c0a856c5 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001#!/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#
9import os, sys
10import subprocess as sub
11import string
12import re
13import optparse
14
15tcm_dir = ""
16
17fabric_ops = []
18fabric_mod_dir = ""
19fabric_mod_port = ""
20fabric_mod_init_port = ""
21
22def tcm_mod_err(msg):
23 print msg
24 sys.exit(1)
25
26def 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
38def 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"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080053 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"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063 buf += " /* Binary World Wide unique Port Name for FC Target Lport */\n"
64 buf += " u64 lport_wwpn;\n"
65 buf += " /* ASCII formatted WWPN for FC Target Lport */\n"
66 buf += " char lport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
67 buf += " /* Returned by " + fabric_mod_name + "_make_lport() */\n"
68 buf += " struct se_wwn lport_wwn;\n"
69 buf += "};\n"
70
71 ret = p.write(buf)
72 if ret:
73 tcm_mod_err("Unable to write f: " + f)
74
75 p.close()
76
77 fabric_mod_port = "lport"
78 fabric_mod_init_port = "nport"
79
80 return
81
82def tcm_mod_build_SAS_include(fabric_mod_dir_var, fabric_mod_name):
83 global fabric_mod_port
84 global fabric_mod_init_port
85 buf = ""
86
87 f = fabric_mod_dir_var + "/" + fabric_mod_name + "_base.h"
88 print "Writing file: " + f
89
90 p = open(f, 'w');
91 if not p:
92 tcm_mod_err("Unable to open file: " + f)
93
94 buf = "#define " + fabric_mod_name.upper() + "_VERSION \"v0.1\"\n"
95 buf += "#define " + fabric_mod_name.upper() + "_NAMELEN 32\n"
96 buf += "\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080097 buf += "struct " + fabric_mod_name + "_tpg {\n"
98 buf += " /* SAS port target portal group tag for TCM */\n"
99 buf += " u16 tport_tpgt;\n"
100 buf += " /* Pointer back to " + fabric_mod_name + "_tport */\n"
101 buf += " struct " + fabric_mod_name + "_tport *tport;\n"
102 buf += " /* Returned by " + fabric_mod_name + "_make_tpg() */\n"
103 buf += " struct se_portal_group se_tpg;\n"
104 buf += "};\n\n"
105 buf += "struct " + fabric_mod_name + "_tport {\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800106 buf += " /* Binary World Wide unique Port Name for SAS Target port */\n"
107 buf += " u64 tport_wwpn;\n"
108 buf += " /* ASCII formatted WWPN for SAS Target port */\n"
109 buf += " char tport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
110 buf += " /* Returned by " + fabric_mod_name + "_make_tport() */\n"
111 buf += " struct se_wwn tport_wwn;\n"
112 buf += "};\n"
113
114 ret = p.write(buf)
115 if ret:
116 tcm_mod_err("Unable to write f: " + f)
117
118 p.close()
119
120 fabric_mod_port = "tport"
121 fabric_mod_init_port = "iport"
122
123 return
124
125def tcm_mod_build_iSCSI_include(fabric_mod_dir_var, fabric_mod_name):
126 global fabric_mod_port
127 global fabric_mod_init_port
128 buf = ""
129
130 f = fabric_mod_dir_var + "/" + fabric_mod_name + "_base.h"
131 print "Writing file: " + f
132
133 p = open(f, 'w');
134 if not p:
135 tcm_mod_err("Unable to open file: " + f)
136
137 buf = "#define " + fabric_mod_name.upper() + "_VERSION \"v0.1\"\n"
138 buf += "#define " + fabric_mod_name.upper() + "_NAMELEN 32\n"
139 buf += "\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800140 buf += "struct " + fabric_mod_name + "_tpg {\n"
141 buf += " /* iSCSI target portal group tag for TCM */\n"
142 buf += " u16 tport_tpgt;\n"
143 buf += " /* Pointer back to " + fabric_mod_name + "_tport */\n"
144 buf += " struct " + fabric_mod_name + "_tport *tport;\n"
145 buf += " /* Returned by " + fabric_mod_name + "_make_tpg() */\n"
146 buf += " struct se_portal_group se_tpg;\n"
147 buf += "};\n\n"
148 buf += "struct " + fabric_mod_name + "_tport {\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800149 buf += " /* ASCII formatted TargetName for IQN */\n"
150 buf += " char tport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
151 buf += " /* Returned by " + fabric_mod_name + "_make_tport() */\n"
152 buf += " struct se_wwn tport_wwn;\n"
153 buf += "};\n"
154
155 ret = p.write(buf)
156 if ret:
157 tcm_mod_err("Unable to write f: " + f)
158
159 p.close()
160
161 fabric_mod_port = "tport"
162 fabric_mod_init_port = "iport"
163
164 return
165
166def tcm_mod_build_base_includes(proto_ident, fabric_mod_dir_val, fabric_mod_name):
167
168 if proto_ident == "FC":
169 tcm_mod_build_FC_include(fabric_mod_dir_val, fabric_mod_name)
170 elif proto_ident == "SAS":
171 tcm_mod_build_SAS_include(fabric_mod_dir_val, fabric_mod_name)
172 elif proto_ident == "iSCSI":
173 tcm_mod_build_iSCSI_include(fabric_mod_dir_val, fabric_mod_name)
174 else:
175 print "Unsupported proto_ident: " + proto_ident
176 sys.exit(1)
177
178 return
179
180def tcm_mod_build_configfs(proto_ident, fabric_mod_dir_var, fabric_mod_name):
181 buf = ""
182
183 f = fabric_mod_dir_var + "/" + fabric_mod_name + "_configfs.c"
184 print "Writing file: " + f
185
186 p = open(f, 'w');
187 if not p:
188 tcm_mod_err("Unable to open file: " + f)
189
190 buf = "#include <linux/module.h>\n"
191 buf += "#include <linux/moduleparam.h>\n"
192 buf += "#include <linux/version.h>\n"
193 buf += "#include <generated/utsrelease.h>\n"
194 buf += "#include <linux/utsname.h>\n"
195 buf += "#include <linux/init.h>\n"
196 buf += "#include <linux/slab.h>\n"
197 buf += "#include <linux/kthread.h>\n"
198 buf += "#include <linux/types.h>\n"
199 buf += "#include <linux/string.h>\n"
200 buf += "#include <linux/configfs.h>\n"
201 buf += "#include <linux/ctype.h>\n"
Sebastian Herbsztc4cfdd82015-07-20 00:16:01 +0200202 buf += "#include <asm/unaligned.h>\n"
203 buf += "#include <scsi/scsi_proto.h>\n\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204 buf += "#include <target/target_core_base.h>\n"
Christoph Hellwigc4795fb2011-11-16 09:46:48 -0500205 buf += "#include <target/target_core_fabric.h>\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800206 buf += "#include <target/target_core_fabric_configfs.h>\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800207 buf += "#include <target/configfs_macros.h>\n\n"
Nicholas Bellinger10635c82011-03-14 04:05:57 -0700208 buf += "#include \"" + fabric_mod_name + "_base.h\"\n"
209 buf += "#include \"" + fabric_mod_name + "_fabric.h\"\n\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800210
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200211 buf += "static const struct target_core_fabric_ops " + fabric_mod_name + "_ops;\n\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800212
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800213 buf += "static struct se_portal_group *" + fabric_mod_name + "_make_tpg(\n"
214 buf += " struct se_wwn *wwn,\n"
215 buf += " struct config_group *group,\n"
216 buf += " const char *name)\n"
217 buf += "{\n"
218 buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + "*" + fabric_mod_port + " = container_of(wwn,\n"
219 buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + ", " + fabric_mod_port + "_wwn);\n\n"
220 buf += " struct " + fabric_mod_name + "_tpg *tpg;\n"
221 buf += " unsigned long tpgt;\n"
222 buf += " int ret;\n\n"
223 buf += " if (strstr(name, \"tpgt_\") != name)\n"
224 buf += " return ERR_PTR(-EINVAL);\n"
Jingoo Han0ab943b2013-07-31 14:59:16 -0700225 buf += " if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800226 buf += " return ERR_PTR(-EINVAL);\n\n"
227 buf += " tpg = kzalloc(sizeof(struct " + fabric_mod_name + "_tpg), GFP_KERNEL);\n"
Sebastian Andrzej Siewiorced38c82011-12-06 11:20:31 +0100228 buf += " if (!tpg) {\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229 buf += " printk(KERN_ERR \"Unable to allocate struct " + fabric_mod_name + "_tpg\");\n"
230 buf += " return ERR_PTR(-ENOMEM);\n"
231 buf += " }\n"
232 buf += " tpg->" + fabric_mod_port + " = " + fabric_mod_port + ";\n"
233 buf += " tpg->" + fabric_mod_port + "_tpgt = tpgt;\n\n"
Sebastian Herbsztc4cfdd82015-07-20 00:16:01 +0200234
235 if proto_ident == "FC":
236 buf += " ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);\n"
237 elif proto_ident == "SAS":
238 buf += " ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_SAS);\n"
239 elif proto_ident == "iSCSI":
240 buf += " ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_ISCSI);\n"
241
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800242 buf += " if (ret < 0) {\n"
243 buf += " kfree(tpg);\n"
244 buf += " return NULL;\n"
245 buf += " }\n"
246 buf += " return &tpg->se_tpg;\n"
247 buf += "}\n\n"
248 buf += "static void " + fabric_mod_name + "_drop_tpg(struct se_portal_group *se_tpg)\n"
249 buf += "{\n"
250 buf += " struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
251 buf += " struct " + fabric_mod_name + "_tpg, se_tpg);\n\n"
252 buf += " core_tpg_deregister(se_tpg);\n"
253 buf += " kfree(tpg);\n"
254 buf += "}\n\n"
255
256 buf += "static struct se_wwn *" + fabric_mod_name + "_make_" + fabric_mod_port + "(\n"
257 buf += " struct target_fabric_configfs *tf,\n"
258 buf += " struct config_group *group,\n"
259 buf += " const char *name)\n"
260 buf += "{\n"
261 buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + ";\n"
262
263 if proto_ident == "FC" or proto_ident == "SAS":
264 buf += " u64 wwpn = 0;\n\n"
265
266 buf += " /* if (" + fabric_mod_name + "_parse_wwn(name, &wwpn, 1) < 0)\n"
267 buf += " return ERR_PTR(-EINVAL); */\n\n"
268 buf += " " + fabric_mod_port + " = kzalloc(sizeof(struct " + fabric_mod_name + "_" + fabric_mod_port + "), GFP_KERNEL);\n"
Sebastian Andrzej Siewiorced38c82011-12-06 11:20:31 +0100269 buf += " if (!" + fabric_mod_port + ") {\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800270 buf += " printk(KERN_ERR \"Unable to allocate struct " + fabric_mod_name + "_" + fabric_mod_port + "\");\n"
271 buf += " return ERR_PTR(-ENOMEM);\n"
272 buf += " }\n"
273
274 if proto_ident == "FC" or proto_ident == "SAS":
275 buf += " " + fabric_mod_port + "->" + fabric_mod_port + "_wwpn = wwpn;\n"
276
Sebastian Andrzej Siewiorced38c82011-12-06 11:20:31 +0100277 buf += " /* " + fabric_mod_name + "_format_wwn(&" + fabric_mod_port + "->" + fabric_mod_port + "_name[0], " + fabric_mod_name.upper() + "_NAMELEN, wwpn); */\n\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800278 buf += " return &" + fabric_mod_port + "->" + fabric_mod_port + "_wwn;\n"
279 buf += "}\n\n"
280 buf += "static void " + fabric_mod_name + "_drop_" + fabric_mod_port + "(struct se_wwn *wwn)\n"
281 buf += "{\n"
282 buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = container_of(wwn,\n"
283 buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + ", " + fabric_mod_port + "_wwn);\n"
284 buf += " kfree(" + fabric_mod_port + ");\n"
285 buf += "}\n\n"
286 buf += "static ssize_t " + fabric_mod_name + "_wwn_show_attr_version(\n"
287 buf += " struct target_fabric_configfs *tf,\n"
288 buf += " char *page)\n"
289 buf += "{\n"
290 buf += " return sprintf(page, \"" + fabric_mod_name.upper() + " fabric module %s on %s/%s\"\n"
291 buf += " \"on \"UTS_RELEASE\"\\n\", " + fabric_mod_name.upper() + "_VERSION, utsname()->sysname,\n"
292 buf += " utsname()->machine);\n"
293 buf += "}\n\n"
294 buf += "TF_WWN_ATTR_RO(" + fabric_mod_name + ", version);\n\n"
295 buf += "static struct configfs_attribute *" + fabric_mod_name + "_wwn_attrs[] = {\n"
296 buf += " &" + fabric_mod_name + "_wwn_version.attr,\n"
297 buf += " NULL,\n"
298 buf += "};\n\n"
299
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200300 buf += "static const struct target_core_fabric_ops " + fabric_mod_name + "_ops = {\n"
Christoph Hellwig65204c82015-04-15 09:04:41 +0200301 buf += " .module = THIS_MODULE,\n"
Sebastian Herbsztc4cfdd82015-07-20 00:16:01 +0200302 buf += " .name = \"" + fabric_mod_name + "\",\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303 buf += " .get_fabric_name = " + fabric_mod_name + "_get_fabric_name,\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800304 buf += " .tpg_get_wwn = " + fabric_mod_name + "_get_fabric_wwn,\n"
305 buf += " .tpg_get_tag = " + fabric_mod_name + "_get_tag,\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800306 buf += " .tpg_check_demo_mode = " + fabric_mod_name + "_check_false,\n"
307 buf += " .tpg_check_demo_mode_cache = " + fabric_mod_name + "_check_true,\n"
308 buf += " .tpg_check_demo_mode_write_protect = " + fabric_mod_name + "_check_true,\n"
309 buf += " .tpg_check_prod_mode_write_protect = " + fabric_mod_name + "_check_false,\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800310 buf += " .tpg_get_inst_index = " + fabric_mod_name + "_tpg_get_inst_index,\n"
Nicholas Bellinger77271de2011-11-28 17:54:28 -0800311 buf += " .release_cmd = " + fabric_mod_name + "_release_cmd,\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800312 buf += " .shutdown_session = " + fabric_mod_name + "_shutdown_session,\n"
313 buf += " .close_session = " + fabric_mod_name + "_close_session,\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800314 buf += " .sess_get_index = " + fabric_mod_name + "_sess_get_index,\n"
315 buf += " .sess_get_initiator_sid = NULL,\n"
316 buf += " .write_pending = " + fabric_mod_name + "_write_pending,\n"
317 buf += " .write_pending_status = " + fabric_mod_name + "_write_pending_status,\n"
318 buf += " .set_default_node_attributes = " + fabric_mod_name + "_set_default_node_attrs,\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800319 buf += " .get_cmd_state = " + fabric_mod_name + "_get_cmd_state,\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800320 buf += " .queue_data_in = " + fabric_mod_name + "_queue_data_in,\n"
321 buf += " .queue_status = " + fabric_mod_name + "_queue_status,\n"
322 buf += " .queue_tm_rsp = " + fabric_mod_name + "_queue_tm_rsp,\n"
Nicholas Bellinger67e51da2014-12-23 00:32:21 -0800323 buf += " .aborted_task = " + fabric_mod_name + "_aborted_task,\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800324 buf += " /*\n"
325 buf += " * Setup function pointers for generic logic in target_core_fabric_configfs.c\n"
326 buf += " */\n"
327 buf += " .fabric_make_wwn = " + fabric_mod_name + "_make_" + fabric_mod_port + ",\n"
328 buf += " .fabric_drop_wwn = " + fabric_mod_name + "_drop_" + fabric_mod_port + ",\n"
329 buf += " .fabric_make_tpg = " + fabric_mod_name + "_make_tpg,\n"
330 buf += " .fabric_drop_tpg = " + fabric_mod_name + "_drop_tpg,\n"
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200331 buf += "\n"
Sebastian Herbsztc4cfdd82015-07-20 00:16:01 +0200332 buf += " .tfc_wwn_attrs = " + fabric_mod_name + "_wwn_attrs,\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800333 buf += "};\n\n"
334
335 buf += "static int __init " + fabric_mod_name + "_init(void)\n"
336 buf += "{\n"
Sebastian Herbsztc4cfdd82015-07-20 00:16:01 +0200337 buf += " return target_register_template(&" + fabric_mod_name + "_ops);\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800338 buf += "};\n\n"
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200339
Sebastian Andrzej Siewiorced38c82011-12-06 11:20:31 +0100340 buf += "static void __exit " + fabric_mod_name + "_exit(void)\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800341 buf += "{\n"
Sebastian Herbsztc4cfdd82015-07-20 00:16:01 +0200342 buf += " target_unregister_template(&" + fabric_mod_name + "_ops);\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800343 buf += "};\n\n"
344
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800345 buf += "MODULE_DESCRIPTION(\"" + fabric_mod_name.upper() + " series fabric driver\");\n"
346 buf += "MODULE_LICENSE(\"GPL\");\n"
347 buf += "module_init(" + fabric_mod_name + "_init);\n"
348 buf += "module_exit(" + fabric_mod_name + "_exit);\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800349
350 ret = p.write(buf)
351 if ret:
352 tcm_mod_err("Unable to write f: " + f)
353
354 p.close()
355
356 return
357
358def tcm_mod_scan_fabric_ops(tcm_dir):
359
Christoph Hellwigc4795fb2011-11-16 09:46:48 -0500360 fabric_ops_api = tcm_dir + "include/target/target_core_fabric.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800361
362 print "Using tcm_mod_scan_fabric_ops: " + fabric_ops_api
363 process_fo = 0;
364
365 p = open(fabric_ops_api, 'r')
366
367 line = p.readline()
368 while line:
369 if process_fo == 0 and re.search('struct target_core_fabric_ops {', line):
370 line = p.readline()
371 continue
372
373 if process_fo == 0:
374 process_fo = 1;
375 line = p.readline()
376 # Search for function pointer
377 if not re.search('\(\*', line):
378 continue
379
380 fabric_ops.append(line.rstrip())
381 continue
382
383 line = p.readline()
384 # Search for function pointer
385 if not re.search('\(\*', line):
386 continue
387
388 fabric_ops.append(line.rstrip())
389
390 p.close()
391 return
392
393def tcm_mod_dump_fabric_ops(proto_ident, fabric_mod_dir_var, fabric_mod_name):
394 buf = ""
395 bufi = ""
396
397 f = fabric_mod_dir_var + "/" + fabric_mod_name + "_fabric.c"
398 print "Writing file: " + f
399
400 p = open(f, 'w')
401 if not p:
402 tcm_mod_err("Unable to open file: " + f)
403
404 fi = fabric_mod_dir_var + "/" + fabric_mod_name + "_fabric.h"
405 print "Writing file: " + fi
406
407 pi = open(fi, 'w')
408 if not pi:
409 tcm_mod_err("Unable to open file: " + fi)
410
411 buf = "#include <linux/slab.h>\n"
412 buf += "#include <linux/kthread.h>\n"
413 buf += "#include <linux/types.h>\n"
414 buf += "#include <linux/list.h>\n"
415 buf += "#include <linux/types.h>\n"
416 buf += "#include <linux/string.h>\n"
417 buf += "#include <linux/ctype.h>\n"
418 buf += "#include <asm/unaligned.h>\n"
Bart Van Asscheba929992015-05-08 10:11:12 +0200419 buf += "#include <scsi/scsi_common.h>\n"
420 buf += "#include <scsi/scsi_proto.h>\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800421 buf += "#include <target/target_core_base.h>\n"
Christoph Hellwigc4795fb2011-11-16 09:46:48 -0500422 buf += "#include <target/target_core_fabric.h>\n"
Nicholas Bellinger10635c82011-03-14 04:05:57 -0700423 buf += "#include \"" + fabric_mod_name + "_base.h\"\n"
424 buf += "#include \"" + fabric_mod_name + "_fabric.h\"\n\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800425
426 buf += "int " + fabric_mod_name + "_check_true(struct se_portal_group *se_tpg)\n"
427 buf += "{\n"
428 buf += " return 1;\n"
429 buf += "}\n\n"
430 bufi += "int " + fabric_mod_name + "_check_true(struct se_portal_group *);\n"
431
432 buf += "int " + fabric_mod_name + "_check_false(struct se_portal_group *se_tpg)\n"
433 buf += "{\n"
434 buf += " return 0;\n"
435 buf += "}\n\n"
436 bufi += "int " + fabric_mod_name + "_check_false(struct se_portal_group *);\n"
437
438 total_fabric_ops = len(fabric_ops)
439 i = 0
440
441 while i < total_fabric_ops:
442 fo = fabric_ops[i]
443 i += 1
444# print "fabric_ops: " + fo
445
446 if re.search('get_fabric_name', fo):
447 buf += "char *" + fabric_mod_name + "_get_fabric_name(void)\n"
448 buf += "{\n"
Nicholas Bellinger67e51da2014-12-23 00:32:21 -0800449 buf += " return \"" + fabric_mod_name + "\";\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800450 buf += "}\n\n"
451 bufi += "char *" + fabric_mod_name + "_get_fabric_name(void);\n"
452 continue
453
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800454 if re.search('get_wwn', fo):
455 buf += "char *" + fabric_mod_name + "_get_fabric_wwn(struct se_portal_group *se_tpg)\n"
456 buf += "{\n"
457 buf += " struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
458 buf += " struct " + fabric_mod_name + "_tpg, se_tpg);\n"
459 buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n\n"
460 buf += " return &" + fabric_mod_port + "->" + fabric_mod_port + "_name[0];\n"
461 buf += "}\n\n"
462 bufi += "char *" + fabric_mod_name + "_get_fabric_wwn(struct se_portal_group *);\n"
463
464 if re.search('get_tag', fo):
465 buf += "u16 " + fabric_mod_name + "_get_tag(struct se_portal_group *se_tpg)\n"
466 buf += "{\n"
467 buf += " struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
468 buf += " struct " + fabric_mod_name + "_tpg, se_tpg);\n"
469 buf += " return tpg->" + fabric_mod_port + "_tpgt;\n"
470 buf += "}\n\n"
471 bufi += "u16 " + fabric_mod_name + "_get_tag(struct se_portal_group *);\n"
472
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800473 if re.search('tpg_get_inst_index\)\(', fo):
474 buf += "u32 " + fabric_mod_name + "_tpg_get_inst_index(struct se_portal_group *se_tpg)\n"
475 buf += "{\n"
476 buf += " return 1;\n"
477 buf += "}\n\n"
478 bufi += "u32 " + fabric_mod_name + "_tpg_get_inst_index(struct se_portal_group *);\n"
479
Nicholas Bellinger77271de2011-11-28 17:54:28 -0800480 if re.search('\*release_cmd\)\(', fo):
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800481 buf += "void " + fabric_mod_name + "_release_cmd(struct se_cmd *se_cmd)\n"
482 buf += "{\n"
483 buf += " return;\n"
484 buf += "}\n\n"
485 bufi += "void " + fabric_mod_name + "_release_cmd(struct se_cmd *);\n"
486
487 if re.search('shutdown_session\)\(', fo):
488 buf += "int " + fabric_mod_name + "_shutdown_session(struct se_session *se_sess)\n"
489 buf += "{\n"
490 buf += " return 0;\n"
491 buf += "}\n\n"
492 bufi += "int " + fabric_mod_name + "_shutdown_session(struct se_session *);\n"
493
494 if re.search('close_session\)\(', fo):
495 buf += "void " + fabric_mod_name + "_close_session(struct se_session *se_sess)\n"
496 buf += "{\n"
497 buf += " return;\n"
498 buf += "}\n\n"
499 bufi += "void " + fabric_mod_name + "_close_session(struct se_session *);\n"
500
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800501 if re.search('sess_get_index\)\(', fo):
502 buf += "u32 " + fabric_mod_name + "_sess_get_index(struct se_session *se_sess)\n"
503 buf += "{\n"
504 buf += " return 0;\n"
505 buf += "}\n\n"
506 bufi += "u32 " + fabric_mod_name + "_sess_get_index(struct se_session *);\n"
507
508 if re.search('write_pending\)\(', fo):
509 buf += "int " + fabric_mod_name + "_write_pending(struct se_cmd *se_cmd)\n"
510 buf += "{\n"
511 buf += " return 0;\n"
512 buf += "}\n\n"
513 bufi += "int " + fabric_mod_name + "_write_pending(struct se_cmd *);\n"
514
515 if re.search('write_pending_status\)\(', fo):
516 buf += "int " + fabric_mod_name + "_write_pending_status(struct se_cmd *se_cmd)\n"
517 buf += "{\n"
518 buf += " return 0;\n"
519 buf += "}\n\n"
520 bufi += "int " + fabric_mod_name + "_write_pending_status(struct se_cmd *);\n"
521
522 if re.search('set_default_node_attributes\)\(', fo):
523 buf += "void " + fabric_mod_name + "_set_default_node_attrs(struct se_node_acl *nacl)\n"
524 buf += "{\n"
525 buf += " return;\n"
526 buf += "}\n\n"
527 bufi += "void " + fabric_mod_name + "_set_default_node_attrs(struct se_node_acl *);\n"
528
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800529 if re.search('get_cmd_state\)\(', fo):
530 buf += "int " + fabric_mod_name + "_get_cmd_state(struct se_cmd *se_cmd)\n"
531 buf += "{\n"
532 buf += " return 0;\n"
533 buf += "}\n\n"
534 bufi += "int " + fabric_mod_name + "_get_cmd_state(struct se_cmd *);\n"
535
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800536 if re.search('queue_data_in\)\(', fo):
537 buf += "int " + fabric_mod_name + "_queue_data_in(struct se_cmd *se_cmd)\n"
538 buf += "{\n"
539 buf += " return 0;\n"
540 buf += "}\n\n"
541 bufi += "int " + fabric_mod_name + "_queue_data_in(struct se_cmd *);\n"
542
543 if re.search('queue_status\)\(', fo):
544 buf += "int " + fabric_mod_name + "_queue_status(struct se_cmd *se_cmd)\n"
545 buf += "{\n"
546 buf += " return 0;\n"
547 buf += "}\n\n"
548 bufi += "int " + fabric_mod_name + "_queue_status(struct se_cmd *);\n"
549
550 if re.search('queue_tm_rsp\)\(', fo):
Nicholas Bellinger67e51da2014-12-23 00:32:21 -0800551 buf += "void " + fabric_mod_name + "_queue_tm_rsp(struct se_cmd *se_cmd)\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800552 buf += "{\n"
Nicholas Bellinger67e51da2014-12-23 00:32:21 -0800553 buf += " return;\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800554 buf += "}\n\n"
Nicholas Bellinger67e51da2014-12-23 00:32:21 -0800555 bufi += "void " + fabric_mod_name + "_queue_tm_rsp(struct se_cmd *);\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800556
Nicholas Bellinger67e51da2014-12-23 00:32:21 -0800557 if re.search('aborted_task\)\(', fo):
558 buf += "void " + fabric_mod_name + "_aborted_task(struct se_cmd *se_cmd)\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800559 buf += "{\n"
Nicholas Bellinger67e51da2014-12-23 00:32:21 -0800560 buf += " return;\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800561 buf += "}\n\n"
Nicholas Bellinger67e51da2014-12-23 00:32:21 -0800562 bufi += "void " + fabric_mod_name + "_aborted_task(struct se_cmd *);\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800563
564 ret = p.write(buf)
565 if ret:
566 tcm_mod_err("Unable to write f: " + f)
567
568 p.close()
569
570 ret = pi.write(bufi)
571 if ret:
572 tcm_mod_err("Unable to write fi: " + fi)
573
574 pi.close()
575 return
576
577def tcm_mod_build_kbuild(fabric_mod_dir_var, fabric_mod_name):
578
579 buf = ""
Nicholas Bellinger10635c82011-03-14 04:05:57 -0700580 f = fabric_mod_dir_var + "/Makefile"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800581 print "Writing file: " + f
582
583 p = open(f, 'w')
584 if not p:
585 tcm_mod_err("Unable to open file: " + f)
586
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800587 buf += fabric_mod_name + "-objs := " + fabric_mod_name + "_fabric.o \\\n"
588 buf += " " + fabric_mod_name + "_configfs.o\n"
589 buf += "obj-$(CONFIG_" + fabric_mod_name.upper() + ") += " + fabric_mod_name + ".o\n"
590
591 ret = p.write(buf)
592 if ret:
593 tcm_mod_err("Unable to write f: " + f)
594
595 p.close()
596 return
597
598def tcm_mod_build_kconfig(fabric_mod_dir_var, fabric_mod_name):
599
600 buf = ""
601 f = fabric_mod_dir_var + "/Kconfig"
602 print "Writing file: " + f
603
604 p = open(f, 'w')
605 if not p:
606 tcm_mod_err("Unable to open file: " + f)
607
608 buf = "config " + fabric_mod_name.upper() + "\n"
609 buf += " tristate \"" + fabric_mod_name.upper() + " fabric module\"\n"
610 buf += " depends on TARGET_CORE && CONFIGFS_FS\n"
611 buf += " default n\n"
612 buf += " ---help---\n"
613 buf += " Say Y here to enable the " + fabric_mod_name.upper() + " fabric module\n"
614
615 ret = p.write(buf)
616 if ret:
617 tcm_mod_err("Unable to write f: " + f)
618
619 p.close()
620 return
621
622def tcm_mod_add_kbuild(tcm_dir, fabric_mod_name):
623 buf = "obj-$(CONFIG_" + fabric_mod_name.upper() + ") += " + fabric_mod_name.lower() + "/\n"
Nicholas Bellinger10635c82011-03-14 04:05:57 -0700624 kbuild = tcm_dir + "/drivers/target/Makefile"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800625
626 f = open(kbuild, 'a')
627 f.write(buf)
628 f.close()
629 return
630
631def tcm_mod_add_kconfig(tcm_dir, fabric_mod_name):
632 buf = "source \"drivers/target/" + fabric_mod_name.lower() + "/Kconfig\"\n"
633 kconfig = tcm_dir + "/drivers/target/Kconfig"
634
635 f = open(kconfig, 'a')
636 f.write(buf)
637 f.close()
638 return
639
640def main(modname, proto_ident):
641# proto_ident = "FC"
642# proto_ident = "SAS"
643# proto_ident = "iSCSI"
644
645 tcm_dir = os.getcwd();
646 tcm_dir += "/../../"
647 print "tcm_dir: " + tcm_dir
648 fabric_mod_name = modname
649 fabric_mod_dir = tcm_dir + "drivers/target/" + fabric_mod_name
650 print "Set fabric_mod_name: " + fabric_mod_name
651 print "Set fabric_mod_dir: " + fabric_mod_dir
652 print "Using proto_ident: " + proto_ident
653
654 if proto_ident != "FC" and proto_ident != "SAS" and proto_ident != "iSCSI":
655 print "Unsupported proto_ident: " + proto_ident
656 sys.exit(1)
657
658 ret = tcm_mod_create_module_subdir(fabric_mod_dir)
659 if ret:
660 print "tcm_mod_create_module_subdir() failed because module already exists!"
661 sys.exit(1)
662
663 tcm_mod_build_base_includes(proto_ident, fabric_mod_dir, fabric_mod_name)
664 tcm_mod_scan_fabric_ops(tcm_dir)
665 tcm_mod_dump_fabric_ops(proto_ident, fabric_mod_dir, fabric_mod_name)
666 tcm_mod_build_configfs(proto_ident, fabric_mod_dir, fabric_mod_name)
667 tcm_mod_build_kbuild(fabric_mod_dir, fabric_mod_name)
668 tcm_mod_build_kconfig(fabric_mod_dir, fabric_mod_name)
669
Nicholas Bellinger67e51da2014-12-23 00:32:21 -0800670 input = raw_input("Would you like to add " + fabric_mod_name + " to drivers/target/Makefile..? [yes,no]: ")
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800671 if input == "yes" or input == "y":
672 tcm_mod_add_kbuild(tcm_dir, fabric_mod_name)
673
Nicholas Bellinger67e51da2014-12-23 00:32:21 -0800674 input = raw_input("Would you like to add " + fabric_mod_name + " to drivers/target/Kconfig..? [yes,no]: ")
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800675 if input == "yes" or input == "y":
676 tcm_mod_add_kconfig(tcm_dir, fabric_mod_name)
677
678 return
679
680parser = optparse.OptionParser()
681parser.add_option('-m', '--modulename', help='Module name', dest='modname',
682 action='store', nargs=1, type='string')
683parser.add_option('-p', '--protoident', help='Protocol Ident', dest='protoident',
684 action='store', nargs=1, type='string')
685
686(opts, args) = parser.parse_args()
687
688mandatories = ['modname', 'protoident']
689for m in mandatories:
690 if not opts.__dict__[m]:
691 print "mandatory option is missing\n"
692 parser.print_help()
693 exit(-1)
694
695if __name__ == "__main__":
696
697 main(str(opts.modname), opts.protoident)