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