tcm_mod_builder.py 23 KB

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