tcm_mod_builder.py 22 KB

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