tfc_conf.c 12 KB

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