tcm_mod_builder.py 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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 + "_nacl {\n"
  43. buf += " /* Binary World Wide unique Port Name for FC Initiator Nport */\n"
  44. buf += " u64 nport_wwpn;\n"
  45. buf += " /* ASCII formatted WWPN for FC Initiator Nport */\n"
  46. buf += " char nport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
  47. buf += " /* Returned by " + fabric_mod_name + "_make_nodeacl() */\n"
  48. buf += " struct se_node_acl se_node_acl;\n"
  49. buf += "};\n"
  50. buf += "\n"
  51. buf += "struct " + fabric_mod_name + "_tpg {\n"
  52. buf += " /* FC lport target portal group tag for TCM */\n"
  53. buf += " u16 lport_tpgt;\n"
  54. buf += " /* Pointer back to " + fabric_mod_name + "_lport */\n"
  55. buf += " struct " + fabric_mod_name + "_lport *lport;\n"
  56. buf += " /* Returned by " + fabric_mod_name + "_make_tpg() */\n"
  57. buf += " struct se_portal_group se_tpg;\n"
  58. buf += "};\n"
  59. buf += "\n"
  60. buf += "struct " + fabric_mod_name + "_lport {\n"
  61. buf += " /* SCSI protocol the lport is providing */\n"
  62. buf += " u8 lport_proto_id;\n"
  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. ret = p.write(buf)
  71. if ret:
  72. tcm_mod_err("Unable to write f: " + f)
  73. p.close()
  74. fabric_mod_port = "lport"
  75. fabric_mod_init_port = "nport"
  76. return
  77. def tcm_mod_build_SAS_include(fabric_mod_dir_var, fabric_mod_name):
  78. global fabric_mod_port
  79. global fabric_mod_init_port
  80. buf = ""
  81. f = fabric_mod_dir_var + "/" + fabric_mod_name + "_base.h"
  82. print "Writing file: " + f
  83. p = open(f, 'w');
  84. if not p:
  85. tcm_mod_err("Unable to open file: " + f)
  86. buf = "#define " + fabric_mod_name.upper() + "_VERSION \"v0.1\"\n"
  87. buf += "#define " + fabric_mod_name.upper() + "_NAMELEN 32\n"
  88. buf += "\n"
  89. buf += "struct " + fabric_mod_name + "_nacl {\n"
  90. buf += " /* Binary World Wide unique Port Name for SAS Initiator port */\n"
  91. buf += " u64 iport_wwpn;\n"
  92. buf += " /* ASCII formatted WWPN for Sas Initiator port */\n"
  93. buf += " char iport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
  94. buf += " /* Returned by " + fabric_mod_name + "_make_nodeacl() */\n"
  95. buf += " struct se_node_acl se_node_acl;\n"
  96. buf += "};\n\n"
  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"
  106. buf += " /* SCSI protocol the tport is providing */\n"
  107. buf += " u8 tport_proto_id;\n"
  108. buf += " /* Binary World Wide unique Port Name for SAS Target port */\n"
  109. buf += " u64 tport_wwpn;\n"
  110. buf += " /* ASCII formatted WWPN for SAS Target port */\n"
  111. buf += " char tport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
  112. buf += " /* Returned by " + fabric_mod_name + "_make_tport() */\n"
  113. buf += " struct se_wwn tport_wwn;\n"
  114. buf += "};\n"
  115. ret = p.write(buf)
  116. if ret:
  117. tcm_mod_err("Unable to write f: " + f)
  118. p.close()
  119. fabric_mod_port = "tport"
  120. fabric_mod_init_port = "iport"
  121. return
  122. def tcm_mod_build_iSCSI_include(fabric_mod_dir_var, fabric_mod_name):
  123. global fabric_mod_port
  124. global fabric_mod_init_port
  125. buf = ""
  126. f = fabric_mod_dir_var + "/" + fabric_mod_name + "_base.h"
  127. print "Writing file: " + f
  128. p = open(f, 'w');
  129. if not p:
  130. tcm_mod_err("Unable to open file: " + f)
  131. buf = "#define " + fabric_mod_name.upper() + "_VERSION \"v0.1\"\n"
  132. buf += "#define " + fabric_mod_name.upper() + "_NAMELEN 32\n"
  133. buf += "\n"
  134. buf += "struct " + fabric_mod_name + "_nacl {\n"
  135. buf += " /* ASCII formatted InitiatorName */\n"
  136. buf += " char iport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
  137. buf += " /* Returned by " + fabric_mod_name + "_make_nodeacl() */\n"
  138. buf += " struct se_node_acl se_node_acl;\n"
  139. buf += "};\n\n"
  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"
  149. buf += " /* SCSI protocol the tport is providing */\n"
  150. buf += " u8 tport_proto_id;\n"
  151. buf += " /* ASCII formatted TargetName for IQN */\n"
  152. buf += " char tport_name[" + fabric_mod_name.upper() + "_NAMELEN];\n"
  153. buf += " /* Returned by " + fabric_mod_name + "_make_tport() */\n"
  154. buf += " struct se_wwn tport_wwn;\n"
  155. buf += "};\n"
  156. ret = p.write(buf)
  157. if ret:
  158. tcm_mod_err("Unable to write f: " + f)
  159. p.close()
  160. fabric_mod_port = "tport"
  161. fabric_mod_init_port = "iport"
  162. return
  163. def tcm_mod_build_base_includes(proto_ident, fabric_mod_dir_val, fabric_mod_name):
  164. if proto_ident == "FC":
  165. tcm_mod_build_FC_include(fabric_mod_dir_val, fabric_mod_name)
  166. elif proto_ident == "SAS":
  167. tcm_mod_build_SAS_include(fabric_mod_dir_val, fabric_mod_name)
  168. elif proto_ident == "iSCSI":
  169. tcm_mod_build_iSCSI_include(fabric_mod_dir_val, fabric_mod_name)
  170. else:
  171. print "Unsupported proto_ident: " + proto_ident
  172. sys.exit(1)
  173. return
  174. def tcm_mod_build_configfs(proto_ident, fabric_mod_dir_var, fabric_mod_name):
  175. buf = ""
  176. f = fabric_mod_dir_var + "/" + fabric_mod_name + "_configfs.c"
  177. print "Writing file: " + f
  178. p = open(f, 'w');
  179. if not p:
  180. tcm_mod_err("Unable to open file: " + f)
  181. buf = "#include <linux/module.h>\n"
  182. buf += "#include <linux/moduleparam.h>\n"
  183. buf += "#include <linux/version.h>\n"
  184. buf += "#include <generated/utsrelease.h>\n"
  185. buf += "#include <linux/utsname.h>\n"
  186. buf += "#include <linux/init.h>\n"
  187. buf += "#include <linux/slab.h>\n"
  188. buf += "#include <linux/kthread.h>\n"
  189. buf += "#include <linux/types.h>\n"
  190. buf += "#include <linux/string.h>\n"
  191. buf += "#include <linux/configfs.h>\n"
  192. buf += "#include <linux/ctype.h>\n"
  193. buf += "#include <asm/unaligned.h>\n\n"
  194. buf += "#include <target/target_core_base.h>\n"
  195. buf += "#include <target/target_core_fabric.h>\n"
  196. buf += "#include <target/target_core_fabric_configfs.h>\n"
  197. buf += "#include <target/target_core_configfs.h>\n"
  198. buf += "#include <target/configfs_macros.h>\n\n"
  199. buf += "#include \"" + fabric_mod_name + "_base.h\"\n"
  200. buf += "#include \"" + fabric_mod_name + "_fabric.h\"\n\n"
  201. buf += "/* Local pointer to allocated TCM configfs fabric module */\n"
  202. buf += "struct target_fabric_configfs *" + fabric_mod_name + "_fabric_configfs;\n\n"
  203. buf += "static struct se_node_acl *" + fabric_mod_name + "_make_nodeacl(\n"
  204. buf += " struct se_portal_group *se_tpg,\n"
  205. buf += " struct config_group *group,\n"
  206. buf += " const char *name)\n"
  207. buf += "{\n"
  208. buf += " struct se_node_acl *se_nacl, *se_nacl_new;\n"
  209. buf += " struct " + fabric_mod_name + "_nacl *nacl;\n"
  210. if proto_ident == "FC" or proto_ident == "SAS":
  211. buf += " u64 wwpn = 0;\n"
  212. buf += " u32 nexus_depth;\n\n"
  213. buf += " /* " + fabric_mod_name + "_parse_wwn(name, &wwpn, 1) < 0)\n"
  214. buf += " return ERR_PTR(-EINVAL); */\n"
  215. buf += " se_nacl_new = " + fabric_mod_name + "_alloc_fabric_acl(se_tpg);\n"
  216. buf += " if (!(se_nacl_new))\n"
  217. buf += " return ERR_PTR(-ENOMEM);\n"
  218. buf += "//#warning FIXME: Hardcoded nexus depth in " + fabric_mod_name + "_make_nodeacl()\n"
  219. buf += " nexus_depth = 1;\n"
  220. buf += " /*\n"
  221. buf += " * se_nacl_new may be released by core_tpg_add_initiator_node_acl()\n"
  222. buf += " * when converting a NodeACL from demo mode -> explict\n"
  223. buf += " */\n"
  224. buf += " se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,\n"
  225. buf += " name, nexus_depth);\n"
  226. buf += " if (IS_ERR(se_nacl)) {\n"
  227. buf += " " + fabric_mod_name + "_release_fabric_acl(se_tpg, se_nacl_new);\n"
  228. buf += " return se_nacl;\n"
  229. buf += " }\n"
  230. buf += " /*\n"
  231. buf += " * Locate our struct " + fabric_mod_name + "_nacl and set the FC Nport WWPN\n"
  232. buf += " */\n"
  233. buf += " nacl = container_of(se_nacl, struct " + fabric_mod_name + "_nacl, se_node_acl);\n"
  234. if proto_ident == "FC" or proto_ident == "SAS":
  235. buf += " nacl->" + fabric_mod_init_port + "_wwpn = wwpn;\n"
  236. buf += " /* " + fabric_mod_name + "_format_wwn(&nacl->" + fabric_mod_init_port + "_name[0], " + fabric_mod_name.upper() + "_NAMELEN, wwpn); */\n\n"
  237. buf += " return se_nacl;\n"
  238. buf += "}\n\n"
  239. buf += "static void " + fabric_mod_name + "_drop_nodeacl(struct se_node_acl *se_acl)\n"
  240. buf += "{\n"
  241. buf += " struct " + fabric_mod_name + "_nacl *nacl = container_of(se_acl,\n"
  242. buf += " struct " + fabric_mod_name + "_nacl, se_node_acl);\n"
  243. buf += " core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);\n"
  244. buf += " kfree(nacl);\n"
  245. buf += "}\n\n"
  246. buf += "static struct se_portal_group *" + fabric_mod_name + "_make_tpg(\n"
  247. buf += " struct se_wwn *wwn,\n"
  248. buf += " struct config_group *group,\n"
  249. buf += " const char *name)\n"
  250. buf += "{\n"
  251. buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + "*" + fabric_mod_port + " = container_of(wwn,\n"
  252. buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + ", " + fabric_mod_port + "_wwn);\n\n"
  253. buf += " struct " + fabric_mod_name + "_tpg *tpg;\n"
  254. buf += " unsigned long tpgt;\n"
  255. buf += " int ret;\n\n"
  256. buf += " if (strstr(name, \"tpgt_\") != name)\n"
  257. buf += " return ERR_PTR(-EINVAL);\n"
  258. buf += " if (strict_strtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)\n"
  259. buf += " return ERR_PTR(-EINVAL);\n\n"
  260. buf += " tpg = kzalloc(sizeof(struct " + fabric_mod_name + "_tpg), GFP_KERNEL);\n"
  261. buf += " if (!(tpg)) {\n"
  262. buf += " printk(KERN_ERR \"Unable to allocate struct " + fabric_mod_name + "_tpg\");\n"
  263. buf += " return ERR_PTR(-ENOMEM);\n"
  264. buf += " }\n"
  265. buf += " tpg->" + fabric_mod_port + " = " + fabric_mod_port + ";\n"
  266. buf += " tpg->" + fabric_mod_port + "_tpgt = tpgt;\n\n"
  267. buf += " ret = core_tpg_register(&" + fabric_mod_name + "_fabric_configfs->tf_ops, wwn,\n"
  268. buf += " &tpg->se_tpg, (void *)tpg,\n"
  269. buf += " TRANSPORT_TPG_TYPE_NORMAL);\n"
  270. buf += " if (ret < 0) {\n"
  271. buf += " kfree(tpg);\n"
  272. buf += " return NULL;\n"
  273. buf += " }\n"
  274. buf += " return &tpg->se_tpg;\n"
  275. buf += "}\n\n"
  276. buf += "static void " + fabric_mod_name + "_drop_tpg(struct se_portal_group *se_tpg)\n"
  277. buf += "{\n"
  278. buf += " struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
  279. buf += " struct " + fabric_mod_name + "_tpg, se_tpg);\n\n"
  280. buf += " core_tpg_deregister(se_tpg);\n"
  281. buf += " kfree(tpg);\n"
  282. buf += "}\n\n"
  283. buf += "static struct se_wwn *" + fabric_mod_name + "_make_" + fabric_mod_port + "(\n"
  284. buf += " struct target_fabric_configfs *tf,\n"
  285. buf += " struct config_group *group,\n"
  286. buf += " const char *name)\n"
  287. buf += "{\n"
  288. buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + ";\n"
  289. if proto_ident == "FC" or proto_ident == "SAS":
  290. buf += " u64 wwpn = 0;\n\n"
  291. buf += " /* if (" + fabric_mod_name + "_parse_wwn(name, &wwpn, 1) < 0)\n"
  292. buf += " return ERR_PTR(-EINVAL); */\n\n"
  293. buf += " " + fabric_mod_port + " = kzalloc(sizeof(struct " + fabric_mod_name + "_" + fabric_mod_port + "), GFP_KERNEL);\n"
  294. buf += " if (!(" + fabric_mod_port + ")) {\n"
  295. buf += " printk(KERN_ERR \"Unable to allocate struct " + fabric_mod_name + "_" + fabric_mod_port + "\");\n"
  296. buf += " return ERR_PTR(-ENOMEM);\n"
  297. buf += " }\n"
  298. if proto_ident == "FC" or proto_ident == "SAS":
  299. buf += " " + fabric_mod_port + "->" + fabric_mod_port + "_wwpn = wwpn;\n"
  300. buf += " /* " + fabric_mod_name + "_format_wwn(&" + fabric_mod_port + "->" + fabric_mod_port + "_name[0], " + fabric_mod_name.upper() + "__NAMELEN, wwpn); */\n\n"
  301. buf += " return &" + fabric_mod_port + "->" + fabric_mod_port + "_wwn;\n"
  302. buf += "}\n\n"
  303. buf += "static void " + fabric_mod_name + "_drop_" + fabric_mod_port + "(struct se_wwn *wwn)\n"
  304. buf += "{\n"
  305. buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = container_of(wwn,\n"
  306. buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + ", " + fabric_mod_port + "_wwn);\n"
  307. buf += " kfree(" + fabric_mod_port + ");\n"
  308. buf += "}\n\n"
  309. buf += "static ssize_t " + fabric_mod_name + "_wwn_show_attr_version(\n"
  310. buf += " struct target_fabric_configfs *tf,\n"
  311. buf += " char *page)\n"
  312. buf += "{\n"
  313. buf += " return sprintf(page, \"" + fabric_mod_name.upper() + " fabric module %s on %s/%s\"\n"
  314. buf += " \"on \"UTS_RELEASE\"\\n\", " + fabric_mod_name.upper() + "_VERSION, utsname()->sysname,\n"
  315. buf += " utsname()->machine);\n"
  316. buf += "}\n\n"
  317. buf += "TF_WWN_ATTR_RO(" + fabric_mod_name + ", version);\n\n"
  318. buf += "static struct configfs_attribute *" + fabric_mod_name + "_wwn_attrs[] = {\n"
  319. buf += " &" + fabric_mod_name + "_wwn_version.attr,\n"
  320. buf += " NULL,\n"
  321. buf += "};\n\n"
  322. buf += "static struct target_core_fabric_ops " + fabric_mod_name + "_ops = {\n"
  323. buf += " .get_fabric_name = " + fabric_mod_name + "_get_fabric_name,\n"
  324. buf += " .get_fabric_proto_ident = " + fabric_mod_name + "_get_fabric_proto_ident,\n"
  325. buf += " .tpg_get_wwn = " + fabric_mod_name + "_get_fabric_wwn,\n"
  326. buf += " .tpg_get_tag = " + fabric_mod_name + "_get_tag,\n"
  327. buf += " .tpg_get_default_depth = " + fabric_mod_name + "_get_default_depth,\n"
  328. buf += " .tpg_get_pr_transport_id = " + fabric_mod_name + "_get_pr_transport_id,\n"
  329. buf += " .tpg_get_pr_transport_id_len = " + fabric_mod_name + "_get_pr_transport_id_len,\n"
  330. buf += " .tpg_parse_pr_out_transport_id = " + fabric_mod_name + "_parse_pr_out_transport_id,\n"
  331. buf += " .tpg_check_demo_mode = " + fabric_mod_name + "_check_false,\n"
  332. buf += " .tpg_check_demo_mode_cache = " + fabric_mod_name + "_check_true,\n"
  333. buf += " .tpg_check_demo_mode_write_protect = " + fabric_mod_name + "_check_true,\n"
  334. buf += " .tpg_check_prod_mode_write_protect = " + fabric_mod_name + "_check_false,\n"
  335. buf += " .tpg_alloc_fabric_acl = " + fabric_mod_name + "_alloc_fabric_acl,\n"
  336. buf += " .tpg_release_fabric_acl = " + fabric_mod_name + "_release_fabric_acl,\n"
  337. buf += " .tpg_get_inst_index = " + fabric_mod_name + "_tpg_get_inst_index,\n"
  338. buf += " .release_cmd = " + fabric_mod_name + "_release_cmd,\n"
  339. buf += " .shutdown_session = " + fabric_mod_name + "_shutdown_session,\n"
  340. buf += " .close_session = " + fabric_mod_name + "_close_session,\n"
  341. buf += " .stop_session = " + fabric_mod_name + "_stop_session,\n"
  342. buf += " .fall_back_to_erl0 = " + fabric_mod_name + "_reset_nexus,\n"
  343. buf += " .sess_logged_in = " + fabric_mod_name + "_sess_logged_in,\n"
  344. buf += " .sess_get_index = " + fabric_mod_name + "_sess_get_index,\n"
  345. buf += " .sess_get_initiator_sid = NULL,\n"
  346. buf += " .write_pending = " + fabric_mod_name + "_write_pending,\n"
  347. buf += " .write_pending_status = " + fabric_mod_name + "_write_pending_status,\n"
  348. buf += " .set_default_node_attributes = " + fabric_mod_name + "_set_default_node_attrs,\n"
  349. buf += " .get_task_tag = " + fabric_mod_name + "_get_task_tag,\n"
  350. buf += " .get_cmd_state = " + fabric_mod_name + "_get_cmd_state,\n"
  351. buf += " .queue_data_in = " + fabric_mod_name + "_queue_data_in,\n"
  352. buf += " .queue_status = " + fabric_mod_name + "_queue_status,\n"
  353. buf += " .queue_tm_rsp = " + fabric_mod_name + "_queue_tm_rsp,\n"
  354. buf += " .get_fabric_sense_len = " + fabric_mod_name + "_get_fabric_sense_len,\n"
  355. buf += " .set_fabric_sense_len = " + fabric_mod_name + "_set_fabric_sense_len,\n"
  356. buf += " .is_state_remove = " + fabric_mod_name + "_is_state_remove,\n"
  357. buf += " /*\n"
  358. buf += " * Setup function pointers for generic logic in target_core_fabric_configfs.c\n"
  359. buf += " */\n"
  360. buf += " .fabric_make_wwn = " + fabric_mod_name + "_make_" + fabric_mod_port + ",\n"
  361. buf += " .fabric_drop_wwn = " + fabric_mod_name + "_drop_" + fabric_mod_port + ",\n"
  362. buf += " .fabric_make_tpg = " + fabric_mod_name + "_make_tpg,\n"
  363. buf += " .fabric_drop_tpg = " + fabric_mod_name + "_drop_tpg,\n"
  364. buf += " .fabric_post_link = NULL,\n"
  365. buf += " .fabric_pre_unlink = NULL,\n"
  366. buf += " .fabric_make_np = NULL,\n"
  367. buf += " .fabric_drop_np = NULL,\n"
  368. buf += " .fabric_make_nodeacl = " + fabric_mod_name + "_make_nodeacl,\n"
  369. buf += " .fabric_drop_nodeacl = " + fabric_mod_name + "_drop_nodeacl,\n"
  370. buf += "};\n\n"
  371. buf += "static int " + fabric_mod_name + "_register_configfs(void)\n"
  372. buf += "{\n"
  373. buf += " struct target_fabric_configfs *fabric;\n"
  374. buf += " int ret;\n\n"
  375. buf += " printk(KERN_INFO \"" + fabric_mod_name.upper() + " fabric module %s on %s/%s\"\n"
  376. buf += " \" on \"UTS_RELEASE\"\\n\"," + fabric_mod_name.upper() + "_VERSION, utsname()->sysname,\n"
  377. buf += " utsname()->machine);\n"
  378. buf += " /*\n"
  379. buf += " * Register the top level struct config_item_type with TCM core\n"
  380. buf += " */\n"
  381. buf += " fabric = target_fabric_configfs_init(THIS_MODULE, \"" + fabric_mod_name[4:] + "\");\n"
  382. buf += " if (!(fabric)) {\n"
  383. buf += " printk(KERN_ERR \"target_fabric_configfs_init() failed\\n\");\n"
  384. buf += " return -ENOMEM;\n"
  385. buf += " }\n"
  386. buf += " /*\n"
  387. buf += " * Setup fabric->tf_ops from our local " + fabric_mod_name + "_ops\n"
  388. buf += " */\n"
  389. buf += " fabric->tf_ops = " + fabric_mod_name + "_ops;\n"
  390. buf += " /*\n"
  391. buf += " * Setup default attribute lists for various fabric->tf_cit_tmpl\n"
  392. buf += " */\n"
  393. buf += " TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = " + fabric_mod_name + "_wwn_attrs;\n"
  394. buf += " TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = NULL;\n"
  395. buf += " TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;\n"
  396. buf += " TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;\n"
  397. buf += " TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;\n"
  398. buf += " TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;\n"
  399. buf += " TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;\n"
  400. buf += " TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;\n"
  401. buf += " TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;\n"
  402. buf += " /*\n"
  403. buf += " * Register the fabric for use within TCM\n"
  404. buf += " */\n"
  405. buf += " ret = target_fabric_configfs_register(fabric);\n"
  406. buf += " if (ret < 0) {\n"
  407. buf += " printk(KERN_ERR \"target_fabric_configfs_register() failed\"\n"
  408. buf += " \" for " + fabric_mod_name.upper() + "\\n\");\n"
  409. buf += " return ret;\n"
  410. buf += " }\n"
  411. buf += " /*\n"
  412. buf += " * Setup our local pointer to *fabric\n"
  413. buf += " */\n"
  414. buf += " " + fabric_mod_name + "_fabric_configfs = fabric;\n"
  415. buf += " printk(KERN_INFO \"" + fabric_mod_name.upper() + "[0] - Set fabric -> " + fabric_mod_name + "_fabric_configfs\\n\");\n"
  416. buf += " return 0;\n"
  417. buf += "};\n\n"
  418. buf += "static void " + fabric_mod_name + "_deregister_configfs(void)\n"
  419. buf += "{\n"
  420. buf += " if (!(" + fabric_mod_name + "_fabric_configfs))\n"
  421. buf += " return;\n\n"
  422. buf += " target_fabric_configfs_deregister(" + fabric_mod_name + "_fabric_configfs);\n"
  423. buf += " " + fabric_mod_name + "_fabric_configfs = NULL;\n"
  424. buf += " printk(KERN_INFO \"" + fabric_mod_name.upper() + "[0] - Cleared " + fabric_mod_name + "_fabric_configfs\\n\");\n"
  425. buf += "};\n\n"
  426. buf += "static int __init " + fabric_mod_name + "_init(void)\n"
  427. buf += "{\n"
  428. buf += " int ret;\n\n"
  429. buf += " ret = " + fabric_mod_name + "_register_configfs();\n"
  430. buf += " if (ret < 0)\n"
  431. buf += " return ret;\n\n"
  432. buf += " return 0;\n"
  433. buf += "};\n\n"
  434. buf += "static void " + fabric_mod_name + "_exit(void)\n"
  435. buf += "{\n"
  436. buf += " " + fabric_mod_name + "_deregister_configfs();\n"
  437. buf += "};\n\n"
  438. buf += "#ifdef MODULE\n"
  439. buf += "MODULE_DESCRIPTION(\"" + fabric_mod_name.upper() + " series fabric driver\");\n"
  440. buf += "MODULE_LICENSE(\"GPL\");\n"
  441. buf += "module_init(" + fabric_mod_name + "_init);\n"
  442. buf += "module_exit(" + fabric_mod_name + "_exit);\n"
  443. buf += "#endif\n"
  444. ret = p.write(buf)
  445. if ret:
  446. tcm_mod_err("Unable to write f: " + f)
  447. p.close()
  448. return
  449. def tcm_mod_scan_fabric_ops(tcm_dir):
  450. fabric_ops_api = tcm_dir + "include/target/target_core_fabric.h"
  451. print "Using tcm_mod_scan_fabric_ops: " + fabric_ops_api
  452. process_fo = 0;
  453. p = open(fabric_ops_api, 'r')
  454. line = p.readline()
  455. while line:
  456. if process_fo == 0 and re.search('struct target_core_fabric_ops {', line):
  457. line = p.readline()
  458. continue
  459. if process_fo == 0:
  460. process_fo = 1;
  461. line = p.readline()
  462. # Search for function pointer
  463. if not re.search('\(\*', line):
  464. continue
  465. fabric_ops.append(line.rstrip())
  466. continue
  467. line = p.readline()
  468. # Search for function pointer
  469. if not re.search('\(\*', line):
  470. continue
  471. fabric_ops.append(line.rstrip())
  472. p.close()
  473. return
  474. def tcm_mod_dump_fabric_ops(proto_ident, fabric_mod_dir_var, fabric_mod_name):
  475. buf = ""
  476. bufi = ""
  477. f = fabric_mod_dir_var + "/" + fabric_mod_name + "_fabric.c"
  478. print "Writing file: " + f
  479. p = open(f, 'w')
  480. if not p:
  481. tcm_mod_err("Unable to open file: " + f)
  482. fi = fabric_mod_dir_var + "/" + fabric_mod_name + "_fabric.h"
  483. print "Writing file: " + fi
  484. pi = open(fi, 'w')
  485. if not pi:
  486. tcm_mod_err("Unable to open file: " + fi)
  487. buf = "#include <linux/slab.h>\n"
  488. buf += "#include <linux/kthread.h>\n"
  489. buf += "#include <linux/types.h>\n"
  490. buf += "#include <linux/list.h>\n"
  491. buf += "#include <linux/types.h>\n"
  492. buf += "#include <linux/string.h>\n"
  493. buf += "#include <linux/ctype.h>\n"
  494. buf += "#include <asm/unaligned.h>\n"
  495. buf += "#include <scsi/scsi.h>\n"
  496. buf += "#include <scsi/scsi_host.h>\n"
  497. buf += "#include <scsi/scsi_device.h>\n"
  498. buf += "#include <scsi/scsi_cmnd.h>\n"
  499. buf += "#include <scsi/libfc.h>\n\n"
  500. buf += "#include <target/target_core_base.h>\n"
  501. buf += "#include <target/target_core_fabric.h>\n"
  502. buf += "#include <target/target_core_configfs.h>\n\n"
  503. buf += "#include \"" + fabric_mod_name + "_base.h\"\n"
  504. buf += "#include \"" + fabric_mod_name + "_fabric.h\"\n\n"
  505. buf += "int " + fabric_mod_name + "_check_true(struct se_portal_group *se_tpg)\n"
  506. buf += "{\n"
  507. buf += " return 1;\n"
  508. buf += "}\n\n"
  509. bufi += "int " + fabric_mod_name + "_check_true(struct se_portal_group *);\n"
  510. buf += "int " + fabric_mod_name + "_check_false(struct se_portal_group *se_tpg)\n"
  511. buf += "{\n"
  512. buf += " return 0;\n"
  513. buf += "}\n\n"
  514. bufi += "int " + fabric_mod_name + "_check_false(struct se_portal_group *);\n"
  515. total_fabric_ops = len(fabric_ops)
  516. i = 0
  517. while i < total_fabric_ops:
  518. fo = fabric_ops[i]
  519. i += 1
  520. # print "fabric_ops: " + fo
  521. if re.search('get_fabric_name', fo):
  522. buf += "char *" + fabric_mod_name + "_get_fabric_name(void)\n"
  523. buf += "{\n"
  524. buf += " return \"" + fabric_mod_name[4:] + "\";\n"
  525. buf += "}\n\n"
  526. bufi += "char *" + fabric_mod_name + "_get_fabric_name(void);\n"
  527. continue
  528. if re.search('get_fabric_proto_ident', fo):
  529. buf += "u8 " + fabric_mod_name + "_get_fabric_proto_ident(struct se_portal_group *se_tpg)\n"
  530. buf += "{\n"
  531. buf += " struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
  532. buf += " struct " + fabric_mod_name + "_tpg, se_tpg);\n"
  533. buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n"
  534. buf += " u8 proto_id;\n\n"
  535. buf += " switch (" + fabric_mod_port + "->" + fabric_mod_port + "_proto_id) {\n"
  536. if proto_ident == "FC":
  537. buf += " case SCSI_PROTOCOL_FCP:\n"
  538. buf += " default:\n"
  539. buf += " proto_id = fc_get_fabric_proto_ident(se_tpg);\n"
  540. buf += " break;\n"
  541. elif proto_ident == "SAS":
  542. buf += " case SCSI_PROTOCOL_SAS:\n"
  543. buf += " default:\n"
  544. buf += " proto_id = sas_get_fabric_proto_ident(se_tpg);\n"
  545. buf += " break;\n"
  546. elif proto_ident == "iSCSI":
  547. buf += " case SCSI_PROTOCOL_ISCSI:\n"
  548. buf += " default:\n"
  549. buf += " proto_id = iscsi_get_fabric_proto_ident(se_tpg);\n"
  550. buf += " break;\n"
  551. buf += " }\n\n"
  552. buf += " return proto_id;\n"
  553. buf += "}\n\n"
  554. bufi += "u8 " + fabric_mod_name + "_get_fabric_proto_ident(struct se_portal_group *);\n"
  555. if re.search('get_wwn', fo):
  556. buf += "char *" + fabric_mod_name + "_get_fabric_wwn(struct se_portal_group *se_tpg)\n"
  557. buf += "{\n"
  558. buf += " struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
  559. buf += " struct " + fabric_mod_name + "_tpg, se_tpg);\n"
  560. buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n\n"
  561. buf += " return &" + fabric_mod_port + "->" + fabric_mod_port + "_name[0];\n"
  562. buf += "}\n\n"
  563. bufi += "char *" + fabric_mod_name + "_get_fabric_wwn(struct se_portal_group *);\n"
  564. if re.search('get_tag', fo):
  565. buf += "u16 " + fabric_mod_name + "_get_tag(struct se_portal_group *se_tpg)\n"
  566. buf += "{\n"
  567. buf += " struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
  568. buf += " struct " + fabric_mod_name + "_tpg, se_tpg);\n"
  569. buf += " return tpg->" + fabric_mod_port + "_tpgt;\n"
  570. buf += "}\n\n"
  571. bufi += "u16 " + fabric_mod_name + "_get_tag(struct se_portal_group *);\n"
  572. if re.search('get_default_depth', fo):
  573. buf += "u32 " + fabric_mod_name + "_get_default_depth(struct se_portal_group *se_tpg)\n"
  574. buf += "{\n"
  575. buf += " return 1;\n"
  576. buf += "}\n\n"
  577. bufi += "u32 " + fabric_mod_name + "_get_default_depth(struct se_portal_group *);\n"
  578. if re.search('get_pr_transport_id\)\(', fo):
  579. buf += "u32 " + fabric_mod_name + "_get_pr_transport_id(\n"
  580. buf += " struct se_portal_group *se_tpg,\n"
  581. buf += " struct se_node_acl *se_nacl,\n"
  582. buf += " struct t10_pr_registration *pr_reg,\n"
  583. buf += " int *format_code,\n"
  584. buf += " unsigned char *buf)\n"
  585. buf += "{\n"
  586. buf += " struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
  587. buf += " struct " + fabric_mod_name + "_tpg, se_tpg);\n"
  588. buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n"
  589. buf += " int ret = 0;\n\n"
  590. buf += " switch (" + fabric_mod_port + "->" + fabric_mod_port + "_proto_id) {\n"
  591. if proto_ident == "FC":
  592. buf += " case SCSI_PROTOCOL_FCP:\n"
  593. buf += " default:\n"
  594. buf += " ret = fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,\n"
  595. buf += " format_code, buf);\n"
  596. buf += " break;\n"
  597. elif proto_ident == "SAS":
  598. buf += " case SCSI_PROTOCOL_SAS:\n"
  599. buf += " default:\n"
  600. buf += " ret = sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,\n"
  601. buf += " format_code, buf);\n"
  602. buf += " break;\n"
  603. elif proto_ident == "iSCSI":
  604. buf += " case SCSI_PROTOCOL_ISCSI:\n"
  605. buf += " default:\n"
  606. buf += " ret = iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,\n"
  607. buf += " format_code, buf);\n"
  608. buf += " break;\n"
  609. buf += " }\n\n"
  610. buf += " return ret;\n"
  611. buf += "}\n\n"
  612. bufi += "u32 " + fabric_mod_name + "_get_pr_transport_id(struct se_portal_group *,\n"
  613. bufi += " struct se_node_acl *, struct t10_pr_registration *,\n"
  614. bufi += " int *, unsigned char *);\n"
  615. if re.search('get_pr_transport_id_len\)\(', fo):
  616. buf += "u32 " + fabric_mod_name + "_get_pr_transport_id_len(\n"
  617. buf += " struct se_portal_group *se_tpg,\n"
  618. buf += " struct se_node_acl *se_nacl,\n"
  619. buf += " struct t10_pr_registration *pr_reg,\n"
  620. buf += " int *format_code)\n"
  621. buf += "{\n"
  622. buf += " struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
  623. buf += " struct " + fabric_mod_name + "_tpg, se_tpg);\n"
  624. buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n"
  625. buf += " int ret = 0;\n\n"
  626. buf += " switch (" + fabric_mod_port + "->" + fabric_mod_port + "_proto_id) {\n"
  627. if proto_ident == "FC":
  628. buf += " case SCSI_PROTOCOL_FCP:\n"
  629. buf += " default:\n"
  630. buf += " ret = fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,\n"
  631. buf += " format_code);\n"
  632. buf += " break;\n"
  633. elif proto_ident == "SAS":
  634. buf += " case SCSI_PROTOCOL_SAS:\n"
  635. buf += " default:\n"
  636. buf += " ret = sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,\n"
  637. buf += " format_code);\n"
  638. buf += " break;\n"
  639. elif proto_ident == "iSCSI":
  640. buf += " case SCSI_PROTOCOL_ISCSI:\n"
  641. buf += " default:\n"
  642. buf += " ret = iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,\n"
  643. buf += " format_code);\n"
  644. buf += " break;\n"
  645. buf += " }\n\n"
  646. buf += " return ret;\n"
  647. buf += "}\n\n"
  648. bufi += "u32 " + fabric_mod_name + "_get_pr_transport_id_len(struct se_portal_group *,\n"
  649. bufi += " struct se_node_acl *, struct t10_pr_registration *,\n"
  650. bufi += " int *);\n"
  651. if re.search('parse_pr_out_transport_id\)\(', fo):
  652. buf += "char *" + fabric_mod_name + "_parse_pr_out_transport_id(\n"
  653. buf += " struct se_portal_group *se_tpg,\n"
  654. buf += " const char *buf,\n"
  655. buf += " u32 *out_tid_len,\n"
  656. buf += " char **port_nexus_ptr)\n"
  657. buf += "{\n"
  658. buf += " struct " + fabric_mod_name + "_tpg *tpg = container_of(se_tpg,\n"
  659. buf += " struct " + fabric_mod_name + "_tpg, se_tpg);\n"
  660. buf += " struct " + fabric_mod_name + "_" + fabric_mod_port + " *" + fabric_mod_port + " = tpg->" + fabric_mod_port + ";\n"
  661. buf += " char *tid = NULL;\n\n"
  662. buf += " switch (" + fabric_mod_port + "->" + fabric_mod_port + "_proto_id) {\n"
  663. if proto_ident == "FC":
  664. buf += " case SCSI_PROTOCOL_FCP:\n"
  665. buf += " default:\n"
  666. buf += " tid = fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,\n"
  667. buf += " port_nexus_ptr);\n"
  668. elif proto_ident == "SAS":
  669. buf += " case SCSI_PROTOCOL_SAS:\n"
  670. buf += " default:\n"
  671. buf += " tid = sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,\n"
  672. buf += " port_nexus_ptr);\n"
  673. elif proto_ident == "iSCSI":
  674. buf += " case SCSI_PROTOCOL_ISCSI:\n"
  675. buf += " default:\n"
  676. buf += " tid = iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,\n"
  677. buf += " port_nexus_ptr);\n"
  678. buf += " }\n\n"
  679. buf += " return tid;\n"
  680. buf += "}\n\n"
  681. bufi += "char *" + fabric_mod_name + "_parse_pr_out_transport_id(struct se_portal_group *,\n"
  682. bufi += " const char *, u32 *, char **);\n"
  683. if re.search('alloc_fabric_acl\)\(', fo):
  684. buf += "struct se_node_acl *" + fabric_mod_name + "_alloc_fabric_acl(struct se_portal_group *se_tpg)\n"
  685. buf += "{\n"
  686. buf += " struct " + fabric_mod_name + "_nacl *nacl;\n\n"
  687. buf += " nacl = kzalloc(sizeof(struct " + fabric_mod_name + "_nacl), GFP_KERNEL);\n"
  688. buf += " if (!(nacl)) {\n"
  689. buf += " printk(KERN_ERR \"Unable to alocate struct " + fabric_mod_name + "_nacl\\n\");\n"
  690. buf += " return NULL;\n"
  691. buf += " }\n\n"
  692. buf += " return &nacl->se_node_acl;\n"
  693. buf += "}\n\n"
  694. bufi += "struct se_node_acl *" + fabric_mod_name + "_alloc_fabric_acl(struct se_portal_group *);\n"
  695. if re.search('release_fabric_acl\)\(', fo):
  696. buf += "void " + fabric_mod_name + "_release_fabric_acl(\n"
  697. buf += " struct se_portal_group *se_tpg,\n"
  698. buf += " struct se_node_acl *se_nacl)\n"
  699. buf += "{\n"
  700. buf += " struct " + fabric_mod_name + "_nacl *nacl = container_of(se_nacl,\n"
  701. buf += " struct " + fabric_mod_name + "_nacl, se_node_acl);\n"
  702. buf += " kfree(nacl);\n"
  703. buf += "}\n\n"
  704. bufi += "void " + fabric_mod_name + "_release_fabric_acl(struct se_portal_group *,\n"
  705. bufi += " struct se_node_acl *);\n"
  706. if re.search('tpg_get_inst_index\)\(', fo):
  707. buf += "u32 " + fabric_mod_name + "_tpg_get_inst_index(struct se_portal_group *se_tpg)\n"
  708. buf += "{\n"
  709. buf += " return 1;\n"
  710. buf += "}\n\n"
  711. bufi += "u32 " + fabric_mod_name + "_tpg_get_inst_index(struct se_portal_group *);\n"
  712. if re.search('\*release_cmd\)\(', fo):
  713. buf += "void " + fabric_mod_name + "_release_cmd(struct se_cmd *se_cmd)\n"
  714. buf += "{\n"
  715. buf += " return;\n"
  716. buf += "}\n\n"
  717. bufi += "void " + fabric_mod_name + "_release_cmd(struct se_cmd *);\n"
  718. if re.search('shutdown_session\)\(', fo):
  719. buf += "int " + fabric_mod_name + "_shutdown_session(struct se_session *se_sess)\n"
  720. buf += "{\n"
  721. buf += " return 0;\n"
  722. buf += "}\n\n"
  723. bufi += "int " + fabric_mod_name + "_shutdown_session(struct se_session *);\n"
  724. if re.search('close_session\)\(', fo):
  725. buf += "void " + fabric_mod_name + "_close_session(struct se_session *se_sess)\n"
  726. buf += "{\n"
  727. buf += " return;\n"
  728. buf += "}\n\n"
  729. bufi += "void " + fabric_mod_name + "_close_session(struct se_session *);\n"
  730. if re.search('stop_session\)\(', fo):
  731. buf += "void " + fabric_mod_name + "_stop_session(struct se_session *se_sess, int sess_sleep , int conn_sleep)\n"
  732. buf += "{\n"
  733. buf += " return;\n"
  734. buf += "}\n\n"
  735. bufi += "void " + fabric_mod_name + "_stop_session(struct se_session *, int, int);\n"
  736. if re.search('fall_back_to_erl0\)\(', fo):
  737. buf += "void " + fabric_mod_name + "_reset_nexus(struct se_session *se_sess)\n"
  738. buf += "{\n"
  739. buf += " return;\n"
  740. buf += "}\n\n"
  741. bufi += "void " + fabric_mod_name + "_reset_nexus(struct se_session *);\n"
  742. if re.search('sess_logged_in\)\(', fo):
  743. buf += "int " + fabric_mod_name + "_sess_logged_in(struct se_session *se_sess)\n"
  744. buf += "{\n"
  745. buf += " return 0;\n"
  746. buf += "}\n\n"
  747. bufi += "int " + fabric_mod_name + "_sess_logged_in(struct se_session *);\n"
  748. if re.search('sess_get_index\)\(', fo):
  749. buf += "u32 " + fabric_mod_name + "_sess_get_index(struct se_session *se_sess)\n"
  750. buf += "{\n"
  751. buf += " return 0;\n"
  752. buf += "}\n\n"
  753. bufi += "u32 " + fabric_mod_name + "_sess_get_index(struct se_session *);\n"
  754. if re.search('write_pending\)\(', fo):
  755. buf += "int " + fabric_mod_name + "_write_pending(struct se_cmd *se_cmd)\n"
  756. buf += "{\n"
  757. buf += " return 0;\n"
  758. buf += "}\n\n"
  759. bufi += "int " + fabric_mod_name + "_write_pending(struct se_cmd *);\n"
  760. if re.search('write_pending_status\)\(', fo):
  761. buf += "int " + fabric_mod_name + "_write_pending_status(struct se_cmd *se_cmd)\n"
  762. buf += "{\n"
  763. buf += " return 0;\n"
  764. buf += "}\n\n"
  765. bufi += "int " + fabric_mod_name + "_write_pending_status(struct se_cmd *);\n"
  766. if re.search('set_default_node_attributes\)\(', fo):
  767. buf += "void " + fabric_mod_name + "_set_default_node_attrs(struct se_node_acl *nacl)\n"
  768. buf += "{\n"
  769. buf += " return;\n"
  770. buf += "}\n\n"
  771. bufi += "void " + fabric_mod_name + "_set_default_node_attrs(struct se_node_acl *);\n"
  772. if re.search('get_task_tag\)\(', fo):
  773. buf += "u32 " + fabric_mod_name + "_get_task_tag(struct se_cmd *se_cmd)\n"
  774. buf += "{\n"
  775. buf += " return 0;\n"
  776. buf += "}\n\n"
  777. bufi += "u32 " + fabric_mod_name + "_get_task_tag(struct se_cmd *);\n"
  778. if re.search('get_cmd_state\)\(', fo):
  779. buf += "int " + fabric_mod_name + "_get_cmd_state(struct se_cmd *se_cmd)\n"
  780. buf += "{\n"
  781. buf += " return 0;\n"
  782. buf += "}\n\n"
  783. bufi += "int " + fabric_mod_name + "_get_cmd_state(struct se_cmd *);\n"
  784. if re.search('queue_data_in\)\(', fo):
  785. buf += "int " + fabric_mod_name + "_queue_data_in(struct se_cmd *se_cmd)\n"
  786. buf += "{\n"
  787. buf += " return 0;\n"
  788. buf += "}\n\n"
  789. bufi += "int " + fabric_mod_name + "_queue_data_in(struct se_cmd *);\n"
  790. if re.search('queue_status\)\(', fo):
  791. buf += "int " + fabric_mod_name + "_queue_status(struct se_cmd *se_cmd)\n"
  792. buf += "{\n"
  793. buf += " return 0;\n"
  794. buf += "}\n\n"
  795. bufi += "int " + fabric_mod_name + "_queue_status(struct se_cmd *);\n"
  796. if re.search('queue_tm_rsp\)\(', fo):
  797. buf += "int " + fabric_mod_name + "_queue_tm_rsp(struct se_cmd *se_cmd)\n"
  798. buf += "{\n"
  799. buf += " return 0;\n"
  800. buf += "}\n\n"
  801. bufi += "int " + fabric_mod_name + "_queue_tm_rsp(struct se_cmd *);\n"
  802. if re.search('get_fabric_sense_len\)\(', fo):
  803. buf += "u16 " + fabric_mod_name + "_get_fabric_sense_len(void)\n"
  804. buf += "{\n"
  805. buf += " return 0;\n"
  806. buf += "}\n\n"
  807. bufi += "u16 " + fabric_mod_name + "_get_fabric_sense_len(void);\n"
  808. if re.search('set_fabric_sense_len\)\(', fo):
  809. buf += "u16 " + fabric_mod_name + "_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)\n"
  810. buf += "{\n"
  811. buf += " return 0;\n"
  812. buf += "}\n\n"
  813. bufi += "u16 " + fabric_mod_name + "_set_fabric_sense_len(struct se_cmd *, u32);\n"
  814. if re.search('is_state_remove\)\(', fo):
  815. buf += "int " + fabric_mod_name + "_is_state_remove(struct se_cmd *se_cmd)\n"
  816. buf += "{\n"
  817. buf += " return 0;\n"
  818. buf += "}\n\n"
  819. bufi += "int " + fabric_mod_name + "_is_state_remove(struct se_cmd *);\n"
  820. ret = p.write(buf)
  821. if ret:
  822. tcm_mod_err("Unable to write f: " + f)
  823. p.close()
  824. ret = pi.write(bufi)
  825. if ret:
  826. tcm_mod_err("Unable to write fi: " + fi)
  827. pi.close()
  828. return
  829. def tcm_mod_build_kbuild(fabric_mod_dir_var, fabric_mod_name):
  830. buf = ""
  831. f = fabric_mod_dir_var + "/Makefile"
  832. print "Writing file: " + f
  833. p = open(f, 'w')
  834. if not p:
  835. tcm_mod_err("Unable to open file: " + f)
  836. buf += fabric_mod_name + "-objs := " + fabric_mod_name + "_fabric.o \\\n"
  837. buf += " " + fabric_mod_name + "_configfs.o\n"
  838. buf += "obj-$(CONFIG_" + fabric_mod_name.upper() + ") += " + fabric_mod_name + ".o\n"
  839. ret = p.write(buf)
  840. if ret:
  841. tcm_mod_err("Unable to write f: " + f)
  842. p.close()
  843. return
  844. def tcm_mod_build_kconfig(fabric_mod_dir_var, fabric_mod_name):
  845. buf = ""
  846. f = fabric_mod_dir_var + "/Kconfig"
  847. print "Writing file: " + f
  848. p = open(f, 'w')
  849. if not p:
  850. tcm_mod_err("Unable to open file: " + f)
  851. buf = "config " + fabric_mod_name.upper() + "\n"
  852. buf += " tristate \"" + fabric_mod_name.upper() + " fabric module\"\n"
  853. buf += " depends on TARGET_CORE && CONFIGFS_FS\n"
  854. buf += " default n\n"
  855. buf += " ---help---\n"
  856. buf += " Say Y here to enable the " + fabric_mod_name.upper() + " fabric module\n"
  857. ret = p.write(buf)
  858. if ret:
  859. tcm_mod_err("Unable to write f: " + f)
  860. p.close()
  861. return
  862. def tcm_mod_add_kbuild(tcm_dir, fabric_mod_name):
  863. buf = "obj-$(CONFIG_" + fabric_mod_name.upper() + ") += " + fabric_mod_name.lower() + "/\n"
  864. kbuild = tcm_dir + "/drivers/target/Makefile"
  865. f = open(kbuild, 'a')
  866. f.write(buf)
  867. f.close()
  868. return
  869. def tcm_mod_add_kconfig(tcm_dir, fabric_mod_name):
  870. buf = "source \"drivers/target/" + fabric_mod_name.lower() + "/Kconfig\"\n"
  871. kconfig = tcm_dir + "/drivers/target/Kconfig"
  872. f = open(kconfig, 'a')
  873. f.write(buf)
  874. f.close()
  875. return
  876. def main(modname, proto_ident):
  877. # proto_ident = "FC"
  878. # proto_ident = "SAS"
  879. # proto_ident = "iSCSI"
  880. tcm_dir = os.getcwd();
  881. tcm_dir += "/../../"
  882. print "tcm_dir: " + tcm_dir
  883. fabric_mod_name = modname
  884. fabric_mod_dir = tcm_dir + "drivers/target/" + fabric_mod_name
  885. print "Set fabric_mod_name: " + fabric_mod_name
  886. print "Set fabric_mod_dir: " + fabric_mod_dir
  887. print "Using proto_ident: " + proto_ident
  888. if proto_ident != "FC" and proto_ident != "SAS" and proto_ident != "iSCSI":
  889. print "Unsupported proto_ident: " + proto_ident
  890. sys.exit(1)
  891. ret = tcm_mod_create_module_subdir(fabric_mod_dir)
  892. if ret:
  893. print "tcm_mod_create_module_subdir() failed because module already exists!"
  894. sys.exit(1)
  895. tcm_mod_build_base_includes(proto_ident, fabric_mod_dir, fabric_mod_name)
  896. tcm_mod_scan_fabric_ops(tcm_dir)
  897. tcm_mod_dump_fabric_ops(proto_ident, fabric_mod_dir, fabric_mod_name)
  898. tcm_mod_build_configfs(proto_ident, fabric_mod_dir, fabric_mod_name)
  899. tcm_mod_build_kbuild(fabric_mod_dir, fabric_mod_name)
  900. tcm_mod_build_kconfig(fabric_mod_dir, fabric_mod_name)
  901. input = raw_input("Would you like to add " + fabric_mod_name + "to drivers/target/Makefile..? [yes,no]: ")
  902. if input == "yes" or input == "y":
  903. tcm_mod_add_kbuild(tcm_dir, fabric_mod_name)
  904. input = raw_input("Would you like to add " + fabric_mod_name + "to drivers/target/Kconfig..? [yes,no]: ")
  905. if input == "yes" or input == "y":
  906. tcm_mod_add_kconfig(tcm_dir, fabric_mod_name)
  907. return
  908. parser = optparse.OptionParser()
  909. parser.add_option('-m', '--modulename', help='Module name', dest='modname',
  910. action='store', nargs=1, type='string')
  911. parser.add_option('-p', '--protoident', help='Protocol Ident', dest='protoident',
  912. action='store', nargs=1, type='string')
  913. (opts, args) = parser.parse_args()
  914. mandatories = ['modname', 'protoident']
  915. for m in mandatories:
  916. if not opts.__dict__[m]:
  917. print "mandatory option is missing\n"
  918. parser.print_help()
  919. exit(-1)
  920. if __name__ == "__main__":
  921. main(str(opts.modname), opts.protoident)