tcm_mod_builder.py 42 KB

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