tfc_conf.c 12 KB

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