tfc_conf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. * Filename: tcm_fc.c
  4. *
  5. * This file contains the configfs implementation for TCM_fc fabric node.
  6. * Based on tcm_loop_configfs.c
  7. *
  8. * Copyright (c) 2010 Cisco Systems, Inc.
  9. * Copyright (c) 2009,2010 Rising Tide, Inc.
  10. * Copyright (c) 2009,2010 Linux-iSCSI.org
  11. *
  12. * Copyright (c) 2009,2010 Nicholas A. Bellinger <nab@linux-iscsi.org>
  13. *
  14. ****************************************************************************/
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <generated/utsrelease.h>
  18. #include <linux/utsname.h>
  19. #include <linux/hex.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/kthread.h>
  23. #include <linux/types.h>
  24. #include <linux/string.h>
  25. #include <linux/configfs.h>
  26. #include <linux/kernel.h>
  27. #include <linux/ctype.h>
  28. #include <linux/unaligned.h>
  29. #include <scsi/libfc.h>
  30. #include <target/target_core_base.h>
  31. #include <target/target_core_fabric.h>
  32. #include "tcm_fc.h"
  33. static LIST_HEAD(ft_wwn_list);
  34. DEFINE_MUTEX(ft_lport_lock);
  35. unsigned int ft_debug_logging;
  36. module_param_named(debug_logging, ft_debug_logging, int, S_IRUGO|S_IWUSR);
  37. MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
  38. /*
  39. * Parse WWN.
  40. * If strict, we require lower-case hex and colon separators to be sure
  41. * the name is the same as what would be generated by ft_format_wwn()
  42. * so the name and wwn are mapped one-to-one.
  43. */
  44. static ssize_t ft_parse_wwn(const char *name, u64 *wwn, int strict)
  45. {
  46. const char *cp;
  47. char c;
  48. u32 byte = 0;
  49. u32 pos = 0;
  50. u32 err;
  51. int val;
  52. *wwn = 0;
  53. for (cp = name; cp < &name[FT_NAMELEN - 1]; cp++) {
  54. c = *cp;
  55. if (c == '\n' && cp[1] == '\0')
  56. continue;
  57. if (strict && pos++ == 2 && byte++ < 7) {
  58. pos = 0;
  59. if (c == ':')
  60. continue;
  61. err = 1;
  62. goto fail;
  63. }
  64. if (c == '\0') {
  65. err = 2;
  66. if (strict && byte != 8)
  67. goto fail;
  68. return cp - name;
  69. }
  70. err = 3;
  71. val = hex_to_bin(c);
  72. if (val < 0 || (strict && isupper(c)))
  73. goto fail;
  74. *wwn = (*wwn << 4) | val;
  75. }
  76. err = 4;
  77. fail:
  78. pr_debug("err %u len %zu pos %u byte %u\n",
  79. err, cp - name, pos, byte);
  80. return -1;
  81. }
  82. ssize_t ft_format_wwn(char *buf, size_t len, u64 wwn)
  83. {
  84. u8 b[8];
  85. put_unaligned_be64(wwn, b);
  86. return snprintf(buf, len,
  87. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
  88. b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
  89. }
  90. static ssize_t ft_wwn_show(void *arg, char *buf)
  91. {
  92. u64 *wwn = arg;
  93. ssize_t len;
  94. len = ft_format_wwn(buf, PAGE_SIZE - 2, *wwn);
  95. buf[len++] = '\n';
  96. return len;
  97. }
  98. static ssize_t ft_wwn_store(void *arg, const char *buf, size_t len)
  99. {
  100. ssize_t ret;
  101. u64 wwn;
  102. ret = ft_parse_wwn(buf, &wwn, 0);
  103. if (ret > 0)
  104. *(u64 *)arg = wwn;
  105. return ret;
  106. }
  107. /*
  108. * ACL auth ops.
  109. */
  110. static ssize_t ft_nacl_port_name_show(struct config_item *item, char *page)
  111. {
  112. struct se_node_acl *se_nacl = acl_to_nacl(item);
  113. struct ft_node_acl *acl = container_of(se_nacl,
  114. struct ft_node_acl, se_node_acl);
  115. return ft_wwn_show(&acl->node_auth.port_name, page);
  116. }
  117. static ssize_t ft_nacl_port_name_store(struct config_item *item,
  118. const char *page, size_t count)
  119. {
  120. struct se_node_acl *se_nacl = acl_to_nacl(item);
  121. struct ft_node_acl *acl = container_of(se_nacl,
  122. struct ft_node_acl, se_node_acl);
  123. return ft_wwn_store(&acl->node_auth.port_name, page, count);
  124. }
  125. static ssize_t ft_nacl_node_name_show(struct config_item *item,
  126. char *page)
  127. {
  128. struct se_node_acl *se_nacl = acl_to_nacl(item);
  129. struct ft_node_acl *acl = container_of(se_nacl,
  130. struct ft_node_acl, se_node_acl);
  131. return ft_wwn_show(&acl->node_auth.node_name, page);
  132. }
  133. static ssize_t ft_nacl_node_name_store(struct config_item *item,
  134. const char *page, size_t count)
  135. {
  136. struct se_node_acl *se_nacl = acl_to_nacl(item);
  137. struct ft_node_acl *acl = container_of(se_nacl,
  138. struct ft_node_acl, se_node_acl);
  139. return ft_wwn_store(&acl->node_auth.node_name, page, count);
  140. }
  141. CONFIGFS_ATTR(ft_nacl_, node_name);
  142. CONFIGFS_ATTR(ft_nacl_, port_name);
  143. static ssize_t ft_nacl_tag_show(struct config_item *item,
  144. char *page)
  145. {
  146. return snprintf(page, PAGE_SIZE, "%s", acl_to_nacl(item)->acl_tag);
  147. }
  148. static ssize_t ft_nacl_tag_store(struct config_item *item,
  149. const char *page, size_t count)
  150. {
  151. struct se_node_acl *se_nacl = acl_to_nacl(item);
  152. int ret;
  153. ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
  154. if (ret < 0)
  155. return ret;
  156. return count;
  157. }
  158. CONFIGFS_ATTR(ft_nacl_, tag);
  159. static struct configfs_attribute *ft_nacl_base_attrs[] = {
  160. &ft_nacl_attr_port_name,
  161. &ft_nacl_attr_node_name,
  162. &ft_nacl_attr_tag,
  163. NULL,
  164. };
  165. /*
  166. * ACL ops.
  167. */
  168. /*
  169. * Add ACL for an initiator. The ACL is named arbitrarily.
  170. * The port_name and/or node_name are attributes.
  171. */
  172. static int ft_init_nodeacl(struct se_node_acl *nacl, const char *name)
  173. {
  174. struct ft_node_acl *acl =
  175. container_of(nacl, struct ft_node_acl, se_node_acl);
  176. u64 wwpn;
  177. if (ft_parse_wwn(name, &wwpn, 1) < 0)
  178. return -EINVAL;
  179. acl->node_auth.port_name = wwpn;
  180. return 0;
  181. }
  182. /*
  183. * local_port port_group (tpg) ops.
  184. */
  185. static struct se_portal_group *ft_add_tpg(struct se_wwn *wwn, const char *name)
  186. {
  187. struct ft_lport_wwn *ft_wwn;
  188. struct ft_tpg *tpg;
  189. struct workqueue_struct *wq;
  190. unsigned long index;
  191. int ret;
  192. pr_debug("tcm_fc: add tpg %s\n", name);
  193. /*
  194. * Name must be "tpgt_" followed by the index.
  195. */
  196. if (strstr(name, "tpgt_") != name)
  197. return NULL;
  198. ret = kstrtoul(name + 5, 10, &index);
  199. if (ret)
  200. return NULL;
  201. if (index > UINT_MAX)
  202. return NULL;
  203. if ((index != 1)) {
  204. pr_err("Error, a single TPG=1 is used for HW port mappings\n");
  205. return ERR_PTR(-ENOSYS);
  206. }
  207. ft_wwn = container_of(wwn, struct ft_lport_wwn, se_wwn);
  208. tpg = kzalloc_obj(*tpg);
  209. if (!tpg)
  210. return NULL;
  211. tpg->index = index;
  212. tpg->lport_wwn = ft_wwn;
  213. INIT_LIST_HEAD(&tpg->lun_list);
  214. wq = alloc_workqueue("tcm_fc", WQ_PERCPU, 1);
  215. if (!wq) {
  216. kfree(tpg);
  217. return NULL;
  218. }
  219. ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
  220. if (ret < 0) {
  221. destroy_workqueue(wq);
  222. kfree(tpg);
  223. return NULL;
  224. }
  225. tpg->workqueue = wq;
  226. mutex_lock(&ft_lport_lock);
  227. ft_wwn->tpg = tpg;
  228. mutex_unlock(&ft_lport_lock);
  229. return &tpg->se_tpg;
  230. }
  231. static void ft_del_tpg(struct se_portal_group *se_tpg)
  232. {
  233. struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
  234. struct ft_lport_wwn *ft_wwn = tpg->lport_wwn;
  235. pr_debug("del tpg %s\n",
  236. config_item_name(&tpg->se_tpg.tpg_group.cg_item));
  237. destroy_workqueue(tpg->workqueue);
  238. /* Wait for sessions to be freed thru RCU, for BUG_ON below */
  239. synchronize_rcu();
  240. mutex_lock(&ft_lport_lock);
  241. ft_wwn->tpg = NULL;
  242. if (tpg->tport) {
  243. tpg->tport->tpg = NULL;
  244. tpg->tport = NULL;
  245. }
  246. mutex_unlock(&ft_lport_lock);
  247. core_tpg_deregister(se_tpg);
  248. kfree(tpg);
  249. }
  250. /*
  251. * Verify that an lport is configured to use the tcm_fc module, and return
  252. * the target port group that should be used.
  253. *
  254. * The caller holds ft_lport_lock.
  255. */
  256. struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport)
  257. {
  258. struct ft_lport_wwn *ft_wwn;
  259. list_for_each_entry(ft_wwn, &ft_wwn_list, ft_wwn_node) {
  260. if (ft_wwn->wwpn == lport->wwpn)
  261. return ft_wwn->tpg;
  262. }
  263. return NULL;
  264. }
  265. /*
  266. * target config instance ops.
  267. */
  268. /*
  269. * Add lport to allowed config.
  270. * The name is the WWPN in lower-case ASCII, colon-separated bytes.
  271. */
  272. static struct se_wwn *ft_add_wwn(
  273. struct target_fabric_configfs *tf,
  274. struct config_group *group,
  275. const char *name)
  276. {
  277. struct ft_lport_wwn *ft_wwn;
  278. struct ft_lport_wwn *old_ft_wwn;
  279. u64 wwpn;
  280. pr_debug("add wwn %s\n", name);
  281. if (ft_parse_wwn(name, &wwpn, 1) < 0)
  282. return NULL;
  283. ft_wwn = kzalloc_obj(*ft_wwn);
  284. if (!ft_wwn)
  285. return NULL;
  286. ft_wwn->wwpn = wwpn;
  287. mutex_lock(&ft_lport_lock);
  288. list_for_each_entry(old_ft_wwn, &ft_wwn_list, ft_wwn_node) {
  289. if (old_ft_wwn->wwpn == wwpn) {
  290. mutex_unlock(&ft_lport_lock);
  291. kfree(ft_wwn);
  292. return NULL;
  293. }
  294. }
  295. list_add_tail(&ft_wwn->ft_wwn_node, &ft_wwn_list);
  296. ft_format_wwn(ft_wwn->name, sizeof(ft_wwn->name), wwpn);
  297. mutex_unlock(&ft_lport_lock);
  298. return &ft_wwn->se_wwn;
  299. }
  300. static void ft_del_wwn(struct se_wwn *wwn)
  301. {
  302. struct ft_lport_wwn *ft_wwn = container_of(wwn,
  303. struct ft_lport_wwn, se_wwn);
  304. pr_debug("del wwn %s\n", ft_wwn->name);
  305. mutex_lock(&ft_lport_lock);
  306. list_del(&ft_wwn->ft_wwn_node);
  307. mutex_unlock(&ft_lport_lock);
  308. kfree(ft_wwn);
  309. }
  310. static ssize_t ft_wwn_version_show(struct config_item *item, char *page)
  311. {
  312. return sprintf(page, "TCM FC " FT_VERSION " on %s/%s on "
  313. ""UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
  314. }
  315. CONFIGFS_ATTR_RO(ft_wwn_, version);
  316. static struct configfs_attribute *ft_wwn_attrs[] = {
  317. &ft_wwn_attr_version,
  318. NULL,
  319. };
  320. static inline struct ft_tpg *ft_tpg(struct se_portal_group *se_tpg)
  321. {
  322. return container_of(se_tpg, struct ft_tpg, se_tpg);
  323. }
  324. static char *ft_get_fabric_wwn(struct se_portal_group *se_tpg)
  325. {
  326. return ft_tpg(se_tpg)->lport_wwn->name;
  327. }
  328. static u16 ft_get_tag(struct se_portal_group *se_tpg)
  329. {
  330. /*
  331. * This tag is used when forming SCSI Name identifier in EVPD=1 0x83
  332. * to represent the SCSI Target Port.
  333. */
  334. return ft_tpg(se_tpg)->index;
  335. }
  336. static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg)
  337. {
  338. return ft_tpg(se_tpg)->index;
  339. }
  340. static const struct target_core_fabric_ops ft_fabric_ops = {
  341. .module = THIS_MODULE,
  342. .fabric_name = "fc",
  343. .node_acl_size = sizeof(struct ft_node_acl),
  344. .tpg_get_wwn = ft_get_fabric_wwn,
  345. .tpg_get_tag = ft_get_tag,
  346. .tpg_get_inst_index = ft_tpg_get_inst_index,
  347. .check_stop_free = ft_check_stop_free,
  348. .release_cmd = ft_release_cmd,
  349. .close_session = ft_sess_close,
  350. .sess_get_index = ft_sess_get_index,
  351. .sess_get_initiator_sid = NULL,
  352. .write_pending = ft_write_pending,
  353. .queue_data_in = ft_queue_data_in,
  354. .queue_status = ft_queue_status,
  355. .queue_tm_rsp = ft_queue_tm_resp,
  356. .aborted_task = ft_aborted_task,
  357. /*
  358. * Setup function pointers for generic logic in
  359. * target_core_fabric_configfs.c
  360. */
  361. .fabric_make_wwn = &ft_add_wwn,
  362. .fabric_drop_wwn = &ft_del_wwn,
  363. .fabric_make_tpg = &ft_add_tpg,
  364. .fabric_drop_tpg = &ft_del_tpg,
  365. .fabric_init_nodeacl = &ft_init_nodeacl,
  366. .tfc_wwn_attrs = ft_wwn_attrs,
  367. .tfc_tpg_nacl_base_attrs = ft_nacl_base_attrs,
  368. .default_submit_type = TARGET_DIRECT_SUBMIT,
  369. .direct_submit_supp = 1,
  370. };
  371. static struct notifier_block ft_notifier = {
  372. .notifier_call = ft_lport_notify
  373. };
  374. static int __init ft_init(void)
  375. {
  376. int ret;
  377. ret = target_register_template(&ft_fabric_ops);
  378. if (ret)
  379. goto out;
  380. ret = fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov);
  381. if (ret)
  382. goto out_unregister_template;
  383. blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier);
  384. fc_lport_iterate(ft_lport_add, NULL);
  385. return 0;
  386. out_unregister_template:
  387. target_unregister_template(&ft_fabric_ops);
  388. out:
  389. return ret;
  390. }
  391. static void __exit ft_exit(void)
  392. {
  393. blocking_notifier_chain_unregister(&fc_lport_notifier_head,
  394. &ft_notifier);
  395. fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov);
  396. fc_lport_iterate(ft_lport_del, NULL);
  397. target_unregister_template(&ft_fabric_ops);
  398. synchronize_rcu();
  399. }
  400. MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION);
  401. MODULE_LICENSE("GPL");
  402. module_init(ft_init);
  403. module_exit(ft_exit);