tfc_conf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 <target/target_core_fabric_configfs.h>
  40. #include <target/configfs_macros.h>
  41. #include "tcm_fc.h"
  42. static LIST_HEAD(ft_wwn_list);
  43. DEFINE_MUTEX(ft_lport_lock);
  44. unsigned int ft_debug_logging;
  45. module_param_named(debug_logging, ft_debug_logging, int, S_IRUGO|S_IWUSR);
  46. MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
  47. /*
  48. * Parse WWN.
  49. * If strict, we require lower-case hex and colon separators to be sure
  50. * the name is the same as what would be generated by ft_format_wwn()
  51. * so the name and wwn are mapped one-to-one.
  52. */
  53. static ssize_t ft_parse_wwn(const char *name, u64 *wwn, int strict)
  54. {
  55. const char *cp;
  56. char c;
  57. u32 byte = 0;
  58. u32 pos = 0;
  59. u32 err;
  60. int val;
  61. *wwn = 0;
  62. for (cp = name; cp < &name[FT_NAMELEN - 1]; cp++) {
  63. c = *cp;
  64. if (c == '\n' && cp[1] == '\0')
  65. continue;
  66. if (strict && pos++ == 2 && byte++ < 7) {
  67. pos = 0;
  68. if (c == ':')
  69. continue;
  70. err = 1;
  71. goto fail;
  72. }
  73. if (c == '\0') {
  74. err = 2;
  75. if (strict && byte != 8)
  76. goto fail;
  77. return cp - name;
  78. }
  79. err = 3;
  80. val = hex_to_bin(c);
  81. if (val < 0 || (strict && isupper(c)))
  82. goto fail;
  83. *wwn = (*wwn << 4) | val;
  84. }
  85. err = 4;
  86. fail:
  87. pr_debug("err %u len %zu pos %u byte %u\n",
  88. err, cp - name, pos, byte);
  89. return -1;
  90. }
  91. ssize_t ft_format_wwn(char *buf, size_t len, u64 wwn)
  92. {
  93. u8 b[8];
  94. put_unaligned_be64(wwn, b);
  95. return snprintf(buf, len,
  96. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
  97. b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
  98. }
  99. static ssize_t ft_wwn_show(void *arg, char *buf)
  100. {
  101. u64 *wwn = arg;
  102. ssize_t len;
  103. len = ft_format_wwn(buf, PAGE_SIZE - 2, *wwn);
  104. buf[len++] = '\n';
  105. return len;
  106. }
  107. static ssize_t ft_wwn_store(void *arg, const char *buf, size_t len)
  108. {
  109. ssize_t ret;
  110. u64 wwn;
  111. ret = ft_parse_wwn(buf, &wwn, 0);
  112. if (ret > 0)
  113. *(u64 *)arg = wwn;
  114. return ret;
  115. }
  116. /*
  117. * ACL auth ops.
  118. */
  119. static ssize_t ft_nacl_show_port_name(
  120. struct se_node_acl *se_nacl,
  121. char *page)
  122. {
  123. struct ft_node_acl *acl = container_of(se_nacl,
  124. struct ft_node_acl, se_node_acl);
  125. return ft_wwn_show(&acl->node_auth.port_name, page);
  126. }
  127. static ssize_t ft_nacl_store_port_name(
  128. struct se_node_acl *se_nacl,
  129. const char *page,
  130. size_t count)
  131. {
  132. struct ft_node_acl *acl = container_of(se_nacl,
  133. struct ft_node_acl, se_node_acl);
  134. return ft_wwn_store(&acl->node_auth.port_name, page, count);
  135. }
  136. TF_NACL_BASE_ATTR(ft, port_name, S_IRUGO | S_IWUSR);
  137. static ssize_t ft_nacl_show_node_name(
  138. struct se_node_acl *se_nacl,
  139. char *page)
  140. {
  141. struct ft_node_acl *acl = container_of(se_nacl,
  142. struct ft_node_acl, se_node_acl);
  143. return ft_wwn_show(&acl->node_auth.node_name, page);
  144. }
  145. static ssize_t ft_nacl_store_node_name(
  146. struct se_node_acl *se_nacl,
  147. const char *page,
  148. size_t count)
  149. {
  150. struct ft_node_acl *acl = container_of(se_nacl,
  151. struct ft_node_acl, se_node_acl);
  152. return ft_wwn_store(&acl->node_auth.node_name, page, count);
  153. }
  154. TF_NACL_BASE_ATTR(ft, node_name, S_IRUGO | S_IWUSR);
  155. static struct configfs_attribute *ft_nacl_base_attrs[] = {
  156. &ft_nacl_port_name.attr,
  157. &ft_nacl_node_name.attr,
  158. NULL,
  159. };
  160. /*
  161. * ACL ops.
  162. */
  163. /*
  164. * Add ACL for an initiator. The ACL is named arbitrarily.
  165. * The port_name and/or node_name are attributes.
  166. */
  167. static int ft_init_nodeacl(struct se_node_acl *nacl, const char *name)
  168. {
  169. struct ft_node_acl *acl =
  170. container_of(nacl, struct ft_node_acl, se_node_acl);
  171. u64 wwpn;
  172. if (ft_parse_wwn(name, &wwpn, 1) < 0)
  173. return -EINVAL;
  174. acl->node_auth.port_name = wwpn;
  175. return 0;
  176. }
  177. struct ft_node_acl *ft_acl_get(struct ft_tpg *tpg, struct fc_rport_priv *rdata)
  178. {
  179. struct ft_node_acl *found = NULL;
  180. struct ft_node_acl *acl;
  181. struct se_portal_group *se_tpg = &tpg->se_tpg;
  182. struct se_node_acl *se_acl;
  183. mutex_lock(&se_tpg->acl_node_mutex);
  184. list_for_each_entry(se_acl, &se_tpg->acl_node_list, acl_list) {
  185. acl = container_of(se_acl, struct ft_node_acl, se_node_acl);
  186. pr_debug("acl %p port_name %llx\n",
  187. acl, (unsigned long long)acl->node_auth.port_name);
  188. if (acl->node_auth.port_name == rdata->ids.port_name ||
  189. acl->node_auth.node_name == rdata->ids.node_name) {
  190. pr_debug("acl %p port_name %llx matched\n", acl,
  191. (unsigned long long)rdata->ids.port_name);
  192. found = acl;
  193. /* XXX need to hold onto ACL */
  194. break;
  195. }
  196. }
  197. mutex_unlock(&se_tpg->acl_node_mutex);
  198. return found;
  199. }
  200. /*
  201. * local_port port_group (tpg) ops.
  202. */
  203. static struct se_portal_group *ft_add_tpg(
  204. struct se_wwn *wwn,
  205. struct config_group *group,
  206. const char *name)
  207. {
  208. struct ft_lport_wwn *ft_wwn;
  209. struct ft_tpg *tpg;
  210. struct workqueue_struct *wq;
  211. unsigned long index;
  212. int ret;
  213. pr_debug("tcm_fc: add tpg %s\n", name);
  214. /*
  215. * Name must be "tpgt_" followed by the index.
  216. */
  217. if (strstr(name, "tpgt_") != name)
  218. return NULL;
  219. ret = kstrtoul(name + 5, 10, &index);
  220. if (ret)
  221. return NULL;
  222. if (index > UINT_MAX)
  223. return NULL;
  224. if ((index != 1)) {
  225. pr_err("Error, a single TPG=1 is used for HW port mappings\n");
  226. return ERR_PTR(-ENOSYS);
  227. }
  228. ft_wwn = container_of(wwn, struct ft_lport_wwn, se_wwn);
  229. tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
  230. if (!tpg)
  231. return NULL;
  232. tpg->index = index;
  233. tpg->lport_wwn = ft_wwn;
  234. INIT_LIST_HEAD(&tpg->lun_list);
  235. wq = alloc_workqueue("tcm_fc", 0, 1);
  236. if (!wq) {
  237. kfree(tpg);
  238. return NULL;
  239. }
  240. ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
  241. if (ret < 0) {
  242. destroy_workqueue(wq);
  243. kfree(tpg);
  244. return NULL;
  245. }
  246. tpg->workqueue = wq;
  247. mutex_lock(&ft_lport_lock);
  248. ft_wwn->tpg = tpg;
  249. mutex_unlock(&ft_lport_lock);
  250. return &tpg->se_tpg;
  251. }
  252. static void ft_del_tpg(struct se_portal_group *se_tpg)
  253. {
  254. struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg);
  255. struct ft_lport_wwn *ft_wwn = tpg->lport_wwn;
  256. pr_debug("del tpg %s\n",
  257. config_item_name(&tpg->se_tpg.tpg_group.cg_item));
  258. destroy_workqueue(tpg->workqueue);
  259. /* Wait for sessions to be freed thru RCU, for BUG_ON below */
  260. synchronize_rcu();
  261. mutex_lock(&ft_lport_lock);
  262. ft_wwn->tpg = NULL;
  263. if (tpg->tport) {
  264. tpg->tport->tpg = NULL;
  265. tpg->tport = NULL;
  266. }
  267. mutex_unlock(&ft_lport_lock);
  268. core_tpg_deregister(se_tpg);
  269. kfree(tpg);
  270. }
  271. /*
  272. * Verify that an lport is configured to use the tcm_fc module, and return
  273. * the target port group that should be used.
  274. *
  275. * The caller holds ft_lport_lock.
  276. */
  277. struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport)
  278. {
  279. struct ft_lport_wwn *ft_wwn;
  280. list_for_each_entry(ft_wwn, &ft_wwn_list, ft_wwn_node) {
  281. if (ft_wwn->wwpn == lport->wwpn)
  282. return ft_wwn->tpg;
  283. }
  284. return NULL;
  285. }
  286. /*
  287. * target config instance ops.
  288. */
  289. /*
  290. * Add lport to allowed config.
  291. * The name is the WWPN in lower-case ASCII, colon-separated bytes.
  292. */
  293. static struct se_wwn *ft_add_wwn(
  294. struct target_fabric_configfs *tf,
  295. struct config_group *group,
  296. const char *name)
  297. {
  298. struct ft_lport_wwn *ft_wwn;
  299. struct ft_lport_wwn *old_ft_wwn;
  300. u64 wwpn;
  301. pr_debug("add wwn %s\n", name);
  302. if (ft_parse_wwn(name, &wwpn, 1) < 0)
  303. return NULL;
  304. ft_wwn = kzalloc(sizeof(*ft_wwn), GFP_KERNEL);
  305. if (!ft_wwn)
  306. return NULL;
  307. ft_wwn->wwpn = wwpn;
  308. mutex_lock(&ft_lport_lock);
  309. list_for_each_entry(old_ft_wwn, &ft_wwn_list, ft_wwn_node) {
  310. if (old_ft_wwn->wwpn == wwpn) {
  311. mutex_unlock(&ft_lport_lock);
  312. kfree(ft_wwn);
  313. return NULL;
  314. }
  315. }
  316. list_add_tail(&ft_wwn->ft_wwn_node, &ft_wwn_list);
  317. ft_format_wwn(ft_wwn->name, sizeof(ft_wwn->name), wwpn);
  318. mutex_unlock(&ft_lport_lock);
  319. return &ft_wwn->se_wwn;
  320. }
  321. static void ft_del_wwn(struct se_wwn *wwn)
  322. {
  323. struct ft_lport_wwn *ft_wwn = container_of(wwn,
  324. struct ft_lport_wwn, se_wwn);
  325. pr_debug("del wwn %s\n", ft_wwn->name);
  326. mutex_lock(&ft_lport_lock);
  327. list_del(&ft_wwn->ft_wwn_node);
  328. mutex_unlock(&ft_lport_lock);
  329. kfree(ft_wwn);
  330. }
  331. static ssize_t ft_wwn_show_attr_version(
  332. struct target_fabric_configfs *tf,
  333. char *page)
  334. {
  335. return sprintf(page, "TCM FC " FT_VERSION " on %s/%s on "
  336. ""UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
  337. }
  338. TF_WWN_ATTR_RO(ft, version);
  339. static struct configfs_attribute *ft_wwn_attrs[] = {
  340. &ft_wwn_version.attr,
  341. NULL,
  342. };
  343. static inline struct ft_tpg *ft_tpg(struct se_portal_group *se_tpg)
  344. {
  345. return container_of(se_tpg, struct ft_tpg, se_tpg);
  346. }
  347. static char *ft_get_fabric_name(void)
  348. {
  349. return "fc";
  350. }
  351. static char *ft_get_fabric_wwn(struct se_portal_group *se_tpg)
  352. {
  353. return ft_tpg(se_tpg)->lport_wwn->name;
  354. }
  355. static u16 ft_get_tag(struct se_portal_group *se_tpg)
  356. {
  357. /*
  358. * This tag is used when forming SCSI Name identifier in EVPD=1 0x83
  359. * to represent the SCSI Target Port.
  360. */
  361. return ft_tpg(se_tpg)->index;
  362. }
  363. static int ft_check_false(struct se_portal_group *se_tpg)
  364. {
  365. return 0;
  366. }
  367. static void ft_set_default_node_attr(struct se_node_acl *se_nacl)
  368. {
  369. }
  370. static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg)
  371. {
  372. return ft_tpg(se_tpg)->index;
  373. }
  374. static const struct target_core_fabric_ops ft_fabric_ops = {
  375. .module = THIS_MODULE,
  376. .name = "fc",
  377. .node_acl_size = sizeof(struct ft_node_acl),
  378. .get_fabric_name = ft_get_fabric_name,
  379. .tpg_get_wwn = ft_get_fabric_wwn,
  380. .tpg_get_tag = ft_get_tag,
  381. .tpg_check_demo_mode = ft_check_false,
  382. .tpg_check_demo_mode_cache = ft_check_false,
  383. .tpg_check_demo_mode_write_protect = ft_check_false,
  384. .tpg_check_prod_mode_write_protect = ft_check_false,
  385. .tpg_get_inst_index = ft_tpg_get_inst_index,
  386. .check_stop_free = ft_check_stop_free,
  387. .release_cmd = ft_release_cmd,
  388. .shutdown_session = ft_sess_shutdown,
  389. .close_session = ft_sess_close,
  390. .sess_get_index = ft_sess_get_index,
  391. .sess_get_initiator_sid = NULL,
  392. .write_pending = ft_write_pending,
  393. .write_pending_status = ft_write_pending_status,
  394. .set_default_node_attributes = ft_set_default_node_attr,
  395. .get_cmd_state = ft_get_cmd_state,
  396. .queue_data_in = ft_queue_data_in,
  397. .queue_status = ft_queue_status,
  398. .queue_tm_rsp = ft_queue_tm_resp,
  399. .aborted_task = ft_aborted_task,
  400. /*
  401. * Setup function pointers for generic logic in
  402. * target_core_fabric_configfs.c
  403. */
  404. .fabric_make_wwn = &ft_add_wwn,
  405. .fabric_drop_wwn = &ft_del_wwn,
  406. .fabric_make_tpg = &ft_add_tpg,
  407. .fabric_drop_tpg = &ft_del_tpg,
  408. .fabric_init_nodeacl = &ft_init_nodeacl,
  409. .tfc_wwn_attrs = ft_wwn_attrs,
  410. .tfc_tpg_nacl_base_attrs = ft_nacl_base_attrs,
  411. };
  412. static struct notifier_block ft_notifier = {
  413. .notifier_call = ft_lport_notify
  414. };
  415. static int __init ft_init(void)
  416. {
  417. int ret;
  418. ret = target_register_template(&ft_fabric_ops);
  419. if (ret)
  420. goto out;
  421. ret = fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov);
  422. if (ret)
  423. goto out_unregister_template;
  424. blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier);
  425. fc_lport_iterate(ft_lport_add, NULL);
  426. return 0;
  427. out_unregister_template:
  428. target_unregister_template(&ft_fabric_ops);
  429. out:
  430. return ret;
  431. }
  432. static void __exit ft_exit(void)
  433. {
  434. blocking_notifier_chain_unregister(&fc_lport_notifier_head,
  435. &ft_notifier);
  436. fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov);
  437. fc_lport_iterate(ft_lport_del, NULL);
  438. target_unregister_template(&ft_fabric_ops);
  439. synchronize_rcu();
  440. }
  441. MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION);
  442. MODULE_LICENSE("GPL");
  443. module_init(ft_init);
  444. module_exit(ft_exit);