tcm_mod_builder.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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 += " .sess_get_index = " + fabric_mod_name + "_sess_get_index,\n"
  250. buf += " .sess_get_initiator_sid = NULL,\n"
  251. buf += " .write_pending = " + fabric_mod_name + "_write_pending,\n"
  252. buf += " .write_pending_status = " + fabric_mod_name + "_write_pending_status,\n"
  253. buf += " .set_default_node_attributes = " + fabric_mod_name + "_set_default_node_attrs,\n"
  254. buf += " .get_cmd_state = " + fabric_mod_name + "_get_cmd_state,\n"
  255. buf += " .queue_data_in = " + fabric_mod_name + "_queue_data_in,\n"
  256. buf += " .queue_status = " + fabric_mod_name + "_queue_status,\n"
  257. buf += " .queue_tm_rsp = " + fabric_mod_name + "_queue_tm_rsp,\n"
  258. buf += " .aborted_task = " + fabric_mod_name + "_aborted_task,\n"
  259. buf += " /*\n"
  260. buf += " * Setup function pointers for generic logic in target_core_fabric_configfs.c\n"
  261. buf += " */\n"
  262. buf += " .fabric_make_wwn = " + fabric_mod_name + "_make_" + fabric_mod_port + ",\n"
  263. buf += " .fabric_drop_wwn = " + fabric_mod_name + "_drop_" + fabric_mod_port + ",\n"
  264. buf += " .fabric_make_tpg = " + fabric_mod_name + "_make_tpg,\n"
  265. buf += " .fabric_drop_tpg = " + fabric_mod_name + "_drop_tpg,\n"
  266. buf += "};\n\n"
  267. buf += "static int __init " + fabric_mod_name + "_init(void)\n"
  268. buf += "{\n"
  269. buf += " return target_register_template(&" + fabric_mod_name + "_ops);\n"
  270. buf += "};\n\n"
  271. buf += "static void __exit " + fabric_mod_name + "_exit(void)\n"
  272. buf += "{\n"
  273. buf += " target_unregister_template(&" + fabric_mod_name + "_ops);\n"
  274. buf += "};\n\n"
  275. buf += "MODULE_DESCRIPTION(\"" + fabric_mod_name.upper() + " series fabric driver\");\n"
  276. buf += "MODULE_LICENSE(\"GPL\");\n"
  277. buf += "module_init(" + fabric_mod_name + "_init);\n"
  278. buf += "module_exit(" + fabric_mod_name + "_exit);\n"
  279. ret = p.write(buf)
  280. if ret:
  281. tcm_mod_err("Unable to write f: " + f)
  282. p.close()
  283. return
  284. def tcm_mod_scan_fabric_ops(tcm_dir):
  285. fabric_ops_api = tcm_dir + "include/target/target_core_fabric.h"
  286. print "Using tcm_mod_scan_fabric_ops: " + fabric_ops_api
  287. process_fo = 0;
  288. p = open(fabric_ops_api, 'r')
  289. line = p.readline()
  290. while line:
  291. if process_fo == 0 and re.search('struct target_core_fabric_ops {', line):
  292. line = p.readline()
  293. continue
  294. if process_fo == 0:
  295. process_fo = 1;
  296. line = p.readline()
  297. # Search for function pointer
  298. if not re.search('\(\*', line):
  299. continue
  300. fabric_ops.append(line.rstrip())
  301. continue
  302. line = p.readline()
  303. # Search for function pointer
  304. if not re.search('\(\*', line):
  305. continue
  306. fabric_ops.append(line.rstrip())
  307. p.close()
  308. return
  309. def tcm_mod_dump_fabric_ops(proto_ident, fabric_mod_dir_var, fabric_mod_name):
  310. buf = ""
  311. bufi = ""
  312. f = fabric_mod_dir_var + "/" + fabric_mod_name + "_fabric.c"
  313. print "Writing file: " + f
  314. p = open(f, 'w')
  315. if not p:
  316. tcm_mod_err("Unable to open file: " + f)
  317. fi = fabric_mod_dir_var + "/" + fabric_mod_name + "_fabric.h"
  318. print "Writing file: " + fi
  319. pi = open(fi, 'w')
  320. if not pi:
  321. tcm_mod_err("Unable to open file: " + fi)
  322. buf = "#include <linux/slab.h>\n"
  323. buf += "#include <linux/kthread.h>\n"
  324. buf += "#include <linux/types.h>\n"
  325. buf += "#include <linux/list.h>\n"
  326. buf += "#include <linux/types.h>\n"
  327. buf += "#include <linux/string.h>\n"
  328. buf += "#include <linux/ctype.h>\n"
  329. buf += "#include <asm/unaligned.h>\n"
  330. buf += "#include <scsi/scsi_common.h>\n"
  331. buf += "#include <scsi/scsi_proto.h>\n"
  332. buf += "#include <target/target_core_base.h>\n"
  333. buf += "#include <target/target_core_fabric.h>\n"
  334. buf += "#include \"" + fabric_mod_name + "_base.h\"\n"
  335. buf += "#include \"" + fabric_mod_name + "_fabric.h\"\n\n"
  336. buf += "int " + fabric_mod_name + "_check_true(struct se_portal_group *se_tpg)\n"
  337. buf += "{\n"
  338. buf += " return 1;\n"
  339. buf += "}\n\n"
  340. bufi += "int " + fabric_mod_name + "_check_true(struct se_portal_group *);\n"
  341. buf += "int " + fabric_mod_name + "_check_false(struct se_portal_group *se_tpg)\n"
  342. buf += "{\n"
  343. buf += " return 0;\n"
  344. buf += "}\n\n"
  345. bufi += "int " + fabric_mod_name + "_check_false(struct se_portal_group *);\n"
  346. total_fabric_ops = len(fabric_ops)
  347. i = 0
  348. while i < total_fabric_ops:
  349. fo = fabric_ops[i]
  350. i += 1
  351. # print "fabric_ops: " + fo
  352. if re.search('get_fabric_name', fo):
  353. buf += "char *" + fabric_mod_name + "_get_fabric_name(void)\n"
  354. buf += "{\n"
  355. buf += " return \"" + fabric_mod_name + "\";\n"
  356. buf += "}\n\n"
  357. bufi += "char *" + fabric_mod_name + "_get_fabric_name(void);\n"
  358. continue
  359. if re.search('get_wwn', fo):
  360. buf += "char *" + fabric_mod_name + "_get_fabric_wwn(struct se_portal_group *se_tpg)\n"
  361. buf += "{\n"
  362. buf += " struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
  363. buf += " struct " + fabric_mod_name + "_tpg, se_tpg);\n"
  364. buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n\n"
  365. buf += " return &" + fabric_mod_port + "->" + fabric_mod_port + "_name[0];\n"
  366. buf += "}\n\n"
  367. bufi += "char *" + fabric_mod_name + "_get_fabric_wwn(struct se_portal_group *);\n"
  368. if re.search('get_tag', fo):
  369. buf += "u16 " + fabric_mod_name + "_get_tag(struct se_portal_group *se_tpg)\n"
  370. buf += "{\n"
  371. buf += " struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
  372. buf += " struct " + fabric_mod_name + "_tpg, se_tpg);\n"
  373. buf += " return tpg->" + fabric_mod_port + "_tpgt;\n"
  374. buf += "}\n\n"
  375. bufi += "u16 " + fabric_mod_name + "_get_tag(struct se_portal_group *);\n"
  376. if re.search('tpg_get_inst_index\)\(', fo):
  377. buf += "u32 " + fabric_mod_name + "_tpg_get_inst_index(struct se_portal_group *se_tpg)\n"
  378. buf += "{\n"
  379. buf += " return 1;\n"
  380. buf += "}\n\n"
  381. bufi += "u32 " + fabric_mod_name + "_tpg_get_inst_index(struct se_portal_group *);\n"
  382. if re.search('\*release_cmd\)\(', fo):
  383. buf += "void " + fabric_mod_name + "_release_cmd(struct se_cmd *se_cmd)\n"
  384. buf += "{\n"
  385. buf += " return;\n"
  386. buf += "}\n\n"
  387. bufi += "void " + fabric_mod_name + "_release_cmd(struct se_cmd *);\n"
  388. if re.search('sess_get_index\)\(', fo):
  389. buf += "u32 " + fabric_mod_name + "_sess_get_index(struct se_session *se_sess)\n"
  390. buf += "{\n"
  391. buf += " return 0;\n"
  392. buf += "}\n\n"
  393. bufi += "u32 " + fabric_mod_name + "_sess_get_index(struct se_session *);\n"
  394. if re.search('write_pending\)\(', fo):
  395. buf += "int " + fabric_mod_name + "_write_pending(struct se_cmd *se_cmd)\n"
  396. buf += "{\n"
  397. buf += " return 0;\n"
  398. buf += "}\n\n"
  399. bufi += "int " + fabric_mod_name + "_write_pending(struct se_cmd *);\n"
  400. if re.search('write_pending_status\)\(', fo):
  401. buf += "int " + fabric_mod_name + "_write_pending_status(struct se_cmd *se_cmd)\n"
  402. buf += "{\n"
  403. buf += " return 0;\n"
  404. buf += "}\n\n"
  405. bufi += "int " + fabric_mod_name + "_write_pending_status(struct se_cmd *);\n"
  406. if re.search('set_default_node_attributes\)\(', fo):
  407. buf += "void " + fabric_mod_name + "_set_default_node_attrs(struct se_node_acl *nacl)\n"
  408. buf += "{\n"
  409. buf += " return;\n"
  410. buf += "}\n\n"
  411. bufi += "void " + fabric_mod_name + "_set_default_node_attrs(struct se_node_acl *);\n"
  412. if re.search('get_cmd_state\)\(', fo):
  413. buf += "int " + fabric_mod_name + "_get_cmd_state(struct se_cmd *se_cmd)\n"
  414. buf += "{\n"
  415. buf += " return 0;\n"
  416. buf += "}\n\n"
  417. bufi += "int " + fabric_mod_name + "_get_cmd_state(struct se_cmd *);\n"
  418. if re.search('queue_data_in\)\(', fo):
  419. buf += "int " + fabric_mod_name + "_queue_data_in(struct se_cmd *se_cmd)\n"
  420. buf += "{\n"
  421. buf += " return 0;\n"
  422. buf += "}\n\n"
  423. bufi += "int " + fabric_mod_name + "_queue_data_in(struct se_cmd *);\n"
  424. if re.search('queue_status\)\(', fo):
  425. buf += "int " + fabric_mod_name + "_queue_status(struct se_cmd *se_cmd)\n"
  426. buf += "{\n"
  427. buf += " return 0;\n"
  428. buf += "}\n\n"
  429. bufi += "int " + fabric_mod_name + "_queue_status(struct se_cmd *);\n"
  430. if re.search('queue_tm_rsp\)\(', fo):
  431. buf += "void " + fabric_mod_name + "_queue_tm_rsp(struct se_cmd *se_cmd)\n"
  432. buf += "{\n"
  433. buf += " return;\n"
  434. buf += "}\n\n"
  435. bufi += "void " + fabric_mod_name + "_queue_tm_rsp(struct se_cmd *);\n"
  436. if re.search('aborted_task\)\(', fo):
  437. buf += "void " + fabric_mod_name + "_aborted_task(struct se_cmd *se_cmd)\n"
  438. buf += "{\n"
  439. buf += " return;\n"
  440. buf += "}\n\n"
  441. bufi += "void " + fabric_mod_name + "_aborted_task(struct se_cmd *);\n"
  442. ret = p.write(buf)
  443. if ret:
  444. tcm_mod_err("Unable to write f: " + f)
  445. p.close()
  446. ret = pi.write(bufi)
  447. if ret:
  448. tcm_mod_err("Unable to write fi: " + fi)
  449. pi.close()
  450. return
  451. def tcm_mod_build_kbuild(fabric_mod_dir_var, fabric_mod_name):
  452. buf = ""
  453. f = fabric_mod_dir_var + "/Makefile"
  454. print "Writing file: " + f
  455. p = open(f, 'w')
  456. if not p:
  457. tcm_mod_err("Unable to open file: " + f)
  458. buf += fabric_mod_name + "-objs := " + fabric_mod_name + "_fabric.o \\\n"
  459. buf += " " + fabric_mod_name + "_configfs.o\n"
  460. buf += "obj-$(CONFIG_" + fabric_mod_name.upper() + ") += " + fabric_mod_name + ".o\n"
  461. ret = p.write(buf)
  462. if ret:
  463. tcm_mod_err("Unable to write f: " + f)
  464. p.close()
  465. return
  466. def tcm_mod_build_kconfig(fabric_mod_dir_var, fabric_mod_name):
  467. buf = ""
  468. f = fabric_mod_dir_var + "/Kconfig"
  469. print "Writing file: " + f
  470. p = open(f, 'w')
  471. if not p:
  472. tcm_mod_err("Unable to open file: " + f)
  473. buf = "config " + fabric_mod_name.upper() + "\n"
  474. buf += " tristate \"" + fabric_mod_name.upper() + " fabric module\"\n"
  475. buf += " depends on TARGET_CORE && CONFIGFS_FS\n"
  476. buf += " default n\n"
  477. buf += " ---help---\n"
  478. buf += " Say Y here to enable the " + fabric_mod_name.upper() + " fabric module\n"
  479. ret = p.write(buf)
  480. if ret:
  481. tcm_mod_err("Unable to write f: " + f)
  482. p.close()
  483. return
  484. def tcm_mod_add_kbuild(tcm_dir, fabric_mod_name):
  485. buf = "obj-$(CONFIG_" + fabric_mod_name.upper() + ") += " + fabric_mod_name.lower() + "/\n"
  486. kbuild = tcm_dir + "/drivers/target/Makefile"
  487. f = open(kbuild, 'a')
  488. f.write(buf)
  489. f.close()
  490. return
  491. def tcm_mod_add_kconfig(tcm_dir, fabric_mod_name):
  492. buf = "source \"drivers/target/" + fabric_mod_name.lower() + "/Kconfig\"\n"
  493. kconfig = tcm_dir + "/drivers/target/Kconfig"
  494. f = open(kconfig, 'a')
  495. f.write(buf)
  496. f.close()
  497. return
  498. def main(modname, proto_ident):
  499. # proto_ident = "FC"
  500. # proto_ident = "SAS"
  501. # proto_ident = "iSCSI"
  502. tcm_dir = os.getcwd();
  503. tcm_dir += "/../../"
  504. print "tcm_dir: " + tcm_dir
  505. fabric_mod_name = modname
  506. fabric_mod_dir = tcm_dir + "drivers/target/" + fabric_mod_name
  507. print "Set fabric_mod_name: " + fabric_mod_name
  508. print "Set fabric_mod_dir: " + fabric_mod_dir
  509. print "Using proto_ident: " + proto_ident
  510. if proto_ident != "FC" and proto_ident != "SAS" and proto_ident != "iSCSI":
  511. print "Unsupported proto_ident: " + proto_ident
  512. sys.exit(1)
  513. ret = tcm_mod_create_module_subdir(fabric_mod_dir)
  514. if ret:
  515. print "tcm_mod_create_module_subdir() failed because module already exists!"
  516. sys.exit(1)
  517. tcm_mod_build_base_includes(proto_ident, fabric_mod_dir, fabric_mod_name)
  518. tcm_mod_scan_fabric_ops(tcm_dir)
  519. tcm_mod_dump_fabric_ops(proto_ident, fabric_mod_dir, fabric_mod_name)
  520. tcm_mod_build_configfs(proto_ident, fabric_mod_dir, fabric_mod_name)
  521. tcm_mod_build_kbuild(fabric_mod_dir, fabric_mod_name)
  522. tcm_mod_build_kconfig(fabric_mod_dir, fabric_mod_name)
  523. input = raw_input("Would you like to add " + fabric_mod_name + " to drivers/target/Makefile..? [yes,no]: ")
  524. if input == "yes" or input == "y":
  525. tcm_mod_add_kbuild(tcm_dir, fabric_mod_name)
  526. input = raw_input("Would you like to add " + fabric_mod_name + " to drivers/target/Kconfig..? [yes,no]: ")
  527. if input == "yes" or input == "y":
  528. tcm_mod_add_kconfig(tcm_dir, fabric_mod_name)
  529. return
  530. parser = optparse.OptionParser()
  531. parser.add_option('-m', '--modulename', help='Module name', dest='modname',
  532. action='store', nargs=1, type='string')
  533. parser.add_option('-p', '--protoident', help='Protocol Ident', dest='protoident',
  534. action='store', nargs=1, type='string')
  535. (opts, args) = parser.parse_args()
  536. mandatories = ['modname', 'protoident']
  537. for m in mandatories:
  538. if not opts.__dict__[m]:
  539. print "mandatory option is missing\n"
  540. parser.print_help()
  541. exit(-1)
  542. if __name__ == "__main__":
  543. main(str(opts.modname), opts.protoident)