tfc_conf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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(
  193. struct se_wwn *wwn,
  194. struct config_group *group,
  195. const char *name)
  196. {
  197. struct ft_lport_wwn *ft_wwn;
  198. struct ft_tpg *tpg;
  199. struct workqueue_struct *wq;
  200. unsigned long index;
  201. int ret;
  202. pr_debug("tcm_fc: add tpg %s\n", name);
  203. /*
  204. * Name must be "tpgt_" followed by the index.
  205. */
  206. if (strstr(name, "tpgt_") != name)
  207. return NULL;
  208. ret = kstrtoul(name + 5, 10, &index);
  209. if (ret)
  210. return NULL;
  211. if (index > UINT_MAX)
  212. return NULL;
  213. if ((index != 1)) {
  214. pr_err("Error, a single TPG=1 is used for HW port mappings\n");
  215. return ERR_PTR(-ENOSYS);
  216. }
  217. ft_wwn = container_of(wwn, struct ft_lport_wwn, se_wwn);
  218. tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
  219. if (!tpg)
  220. return NULL;
  221. tpg->index = index;
  222. tpg->lport_wwn = ft_wwn;
  223. INIT_LIST_HEAD(&tpg->lun_list);
  224. wq = alloc_workqueue("tcm_fc", 0, 1);
  225. if (!wq) {
  226. kfree(tpg);
  227. return NULL;
  228. }
  229. ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
  230. if (ret < 0) {
  231. destroy_workqueue(wq);
  232. kfree(tpg);
  233. return NULL;
  234. }
  235. tpg->workqueue = wq;
  236. mutex_lock(&ft_lport_lock);
  237. ft_wwn->tpg = tpg;
  238. mutex_unlock(&ft_lport_lock);
  239. return &tpg->se_tpg;
  240. }
  241. static void ft_del_tpg(struct se_portal_group *se_tpg)
  242. {
  243. struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
  244. struct ft_lport_wwn *ft_wwn = tpg->lport_wwn;
  245. pr_debug("del tpg %s\n",
  246. config_item_name(&tpg->se_tpg.tpg_group.cg_item));
  247. destroy_workqueue(tpg->workqueue);
  248. /* Wait for sessions to be freed thru RCU, for BUG_ON below */
  249. synchronize_rcu();
  250. mutex_lock(&ft_lport_lock);
  251. ft_wwn->tpg = NULL;
  252. if (tpg->tport) {
  253. tpg->tport->tpg = NULL;
  254. tpg->tport = NULL;
  255. }
  256. mutex_unlock(&ft_lport_lock);
  257. core_tpg_deregister(se_tpg);
  258. kfree(tpg);
  259. }
  260. /*
  261. * Verify that an lport is configured to use the tcm_fc module, and return
  262. * the target port group that should be used.
  263. *
  264. * The caller holds ft_lport_lock.
  265. */
  266. struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport)
  267. {
  268. struct ft_lport_wwn *ft_wwn;
  269. list_for_each_entry(ft_wwn, &ft_wwn_list, ft_wwn_node) {
  270. if (ft_wwn->wwpn == lport->wwpn)
  271. return ft_wwn->tpg;
  272. }
  273. return NULL;
  274. }
  275. /*
  276. * target config instance ops.
  277. */
  278. /*
  279. * Add lport to allowed config.
  280. * The name is the WWPN in lower-case ASCII, colon-separated bytes.
  281. */
  282. static struct se_wwn *ft_add_wwn(
  283. struct target_fabric_configfs *tf,
  284. struct config_group *group,
  285. const char *name)
  286. {
  287. struct ft_lport_wwn *ft_wwn;
  288. struct ft_lport_wwn *old_ft_wwn;
  289. u64 wwpn;
  290. pr_debug("add wwn %s\n", name);
  291. if (ft_parse_wwn(name, &wwpn, 1) < 0)
  292. return NULL;
  293. ft_wwn = kzalloc(sizeof(*ft_wwn), GFP_KERNEL);
  294. if (!ft_wwn)
  295. return NULL;
  296. ft_wwn->wwpn = wwpn;
  297. mutex_lock(&ft_lport_lock);
  298. list_for_each_entry(old_ft_wwn, &ft_wwn_list, ft_wwn_node) {
  299. if (old_ft_wwn->wwpn == wwpn) {
  300. mutex_unlock(&ft_lport_lock);
  301. kfree(ft_wwn);
  302. return NULL;
  303. }
  304. }
  305. list_add_tail(&ft_wwn->ft_wwn_node, &ft_wwn_list);
  306. ft_format_wwn(ft_wwn->name, sizeof(ft_wwn->name), wwpn);
  307. mutex_unlock(&ft_lport_lock);
  308. return &ft_wwn->se_wwn;
  309. }
  310. static void ft_del_wwn(struct se_wwn *wwn)
  311. {
  312. struct ft_lport_wwn *ft_wwn = container_of(wwn,
  313. struct ft_lport_wwn, se_wwn);
  314. pr_debug("del wwn %s\n", ft_wwn->name);
  315. mutex_lock(&ft_lport_lock);
  316. list_del(&ft_wwn->ft_wwn_node);
  317. mutex_unlock(&ft_lport_lock);
  318. kfree(ft_wwn);
  319. }
  320. static ssize_t ft_wwn_version_show(struct config_item *item, char *page)
  321. {
  322. return sprintf(page, "TCM FC " FT_VERSION " on %s/%s on "
  323. ""UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
  324. }
  325. CONFIGFS_ATTR_RO(ft_wwn_, version);
  326. static struct configfs_attribute *ft_wwn_attrs[] = {
  327. &ft_wwn_attr_version,
  328. NULL,
  329. };
  330. static inline struct ft_tpg *ft_tpg(struct se_portal_group *se_tpg)
  331. {
  332. return container_of(se_tpg, struct ft_tpg, se_tpg);
  333. }
  334. static char *ft_get_fabric_name(void)
  335. {
  336. return "fc";
  337. }
  338. static char *ft_get_fabric_wwn(struct se_portal_group *se_tpg)
  339. {
  340. return ft_tpg(se_tpg)->lport_wwn->name;
  341. }
  342. static u16 ft_get_tag(struct se_portal_group *se_tpg)
  343. {
  344. /*
  345. * This tag is used when forming SCSI Name identifier in EVPD=1 0x83
  346. * to represent the SCSI Target Port.
  347. */
  348. return ft_tpg(se_tpg)->index;
  349. }
  350. static int ft_check_false(struct se_portal_group *se_tpg)
  351. {
  352. return 0;
  353. }
  354. static void ft_set_default_node_attr(struct se_node_acl *se_nacl)
  355. {
  356. }
  357. static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg)
  358. {
  359. return ft_tpg(se_tpg)->index;
  360. }
  361. static const struct target_core_fabric_ops ft_fabric_ops = {
  362. .module = THIS_MODULE,
  363. .name = "fc",
  364. .node_acl_size = sizeof(struct ft_node_acl),
  365. .get_fabric_name = ft_get_fabric_name,
  366. .tpg_get_wwn = ft_get_fabric_wwn,
  367. .tpg_get_tag = ft_get_tag,
  368. .tpg_check_demo_mode = ft_check_false,
  369. .tpg_check_demo_mode_cache = ft_check_false,
  370. .tpg_check_demo_mode_write_protect = ft_check_false,
  371. .tpg_check_prod_mode_write_protect = ft_check_false,
  372. .tpg_get_inst_index = ft_tpg_get_inst_index,
  373. .check_stop_free = ft_check_stop_free,
  374. .release_cmd = ft_release_cmd,
  375. .close_session = ft_sess_close,
  376. .sess_get_index = ft_sess_get_index,
  377. .sess_get_initiator_sid = NULL,
  378. .write_pending = ft_write_pending,
  379. .write_pending_status = ft_write_pending_status,
  380. .set_default_node_attributes = ft_set_default_node_attr,
  381. .get_cmd_state = ft_get_cmd_state,
  382. .queue_data_in = ft_queue_data_in,
  383. .queue_status = ft_queue_status,
  384. .queue_tm_rsp = ft_queue_tm_resp,
  385. .aborted_task = ft_aborted_task,
  386. /*
  387. * Setup function pointers for generic logic in
  388. * target_core_fabric_configfs.c
  389. */
  390. .fabric_make_wwn = &ft_add_wwn,
  391. .fabric_drop_wwn = &ft_del_wwn,
  392. .fabric_make_tpg = &ft_add_tpg,
  393. .fabric_drop_tpg = &ft_del_tpg,
  394. .fabric_init_nodeacl = &ft_init_nodeacl,
  395. .tfc_wwn_attrs = ft_wwn_attrs,
  396. .tfc_tpg_nacl_base_attrs = ft_nacl_base_attrs,
  397. };
  398. static struct notifier_block ft_notifier = {
  399. .notifier_call = ft_lport_notify
  400. };
  401. static int __init ft_init(void)
  402. {
  403. int ret;
  404. ret = target_register_template(&ft_fabric_ops);
  405. if (ret)
  406. goto out;
  407. ret = fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov);
  408. if (ret)
  409. goto out_unregister_template;
  410. blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier);
  411. fc_lport_iterate(ft_lport_add, NULL);
  412. return 0;
  413. out_unregister_template:
  414. target_unregister_template(&ft_fabric_ops);
  415. out:
  416. return ret;
  417. }
  418. static void __exit ft_exit(void)
  419. {
  420. blocking_notifier_chain_unregister(&fc_lport_notifier_head,
  421. &ft_notifier);
  422. fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov);
  423. fc_lport_iterate(ft_lport_del, NULL);
  424. target_unregister_template(&ft_fabric_ops);
  425. synchronize_rcu();
  426. }
  427. MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION);
  428. MODULE_LICENSE("GPL");
  429. module_init(ft_init);
  430. module_exit(ft_exit);