Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 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 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/moduleparam.h> |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 26 | #include <generated/utsrelease.h> |
| 27 | #include <linux/utsname.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/kthread.h> |
| 31 | #include <linux/types.h> |
| 32 | #include <linux/string.h> |
| 33 | #include <linux/configfs.h> |
Andy Shevchenko | 635a2b3 | 2011-09-30 14:45:40 +0300 | [diff] [blame] | 34 | #include <linux/kernel.h> |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 35 | #include <linux/ctype.h> |
| 36 | #include <asm/unaligned.h> |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 37 | #include <scsi/libfc.h> |
| 38 | |
| 39 | #include <target/target_core_base.h> |
Christoph Hellwig | c4795fb | 2011-11-16 09:46:48 -0500 | [diff] [blame] | 40 | #include <target/target_core_fabric.h> |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 41 | |
| 42 | #include "tcm_fc.h" |
| 43 | |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 44 | static LIST_HEAD(ft_wwn_list); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 45 | DEFINE_MUTEX(ft_lport_lock); |
| 46 | |
| 47 | unsigned int ft_debug_logging; |
| 48 | module_param_named(debug_logging, ft_debug_logging, int, S_IRUGO|S_IWUSR); |
| 49 | MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels"); |
| 50 | |
| 51 | /* |
| 52 | * Parse WWN. |
| 53 | * If strict, we require lower-case hex and colon separators to be sure |
| 54 | * the name is the same as what would be generated by ft_format_wwn() |
| 55 | * so the name and wwn are mapped one-to-one. |
| 56 | */ |
| 57 | static ssize_t ft_parse_wwn(const char *name, u64 *wwn, int strict) |
| 58 | { |
| 59 | const char *cp; |
| 60 | char c; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 61 | u32 byte = 0; |
| 62 | u32 pos = 0; |
| 63 | u32 err; |
Andy Shevchenko | 635a2b3 | 2011-09-30 14:45:40 +0300 | [diff] [blame] | 64 | int val; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 65 | |
| 66 | *wwn = 0; |
| 67 | for (cp = name; cp < &name[FT_NAMELEN - 1]; cp++) { |
| 68 | c = *cp; |
| 69 | if (c == '\n' && cp[1] == '\0') |
| 70 | continue; |
| 71 | if (strict && pos++ == 2 && byte++ < 7) { |
| 72 | pos = 0; |
| 73 | if (c == ':') |
| 74 | continue; |
| 75 | err = 1; |
| 76 | goto fail; |
| 77 | } |
| 78 | if (c == '\0') { |
| 79 | err = 2; |
| 80 | if (strict && byte != 8) |
| 81 | goto fail; |
| 82 | return cp - name; |
| 83 | } |
| 84 | err = 3; |
Andy Shevchenko | 635a2b3 | 2011-09-30 14:45:40 +0300 | [diff] [blame] | 85 | val = hex_to_bin(c); |
| 86 | if (val < 0 || (strict && isupper(c))) |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 87 | goto fail; |
Andy Shevchenko | 635a2b3 | 2011-09-30 14:45:40 +0300 | [diff] [blame] | 88 | *wwn = (*wwn << 4) | val; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 89 | } |
| 90 | err = 4; |
| 91 | fail: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 92 | pr_debug("err %u len %zu pos %u byte %u\n", |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 93 | err, cp - name, pos, byte); |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | ssize_t ft_format_wwn(char *buf, size_t len, u64 wwn) |
| 98 | { |
| 99 | u8 b[8]; |
| 100 | |
| 101 | put_unaligned_be64(wwn, b); |
| 102 | return snprintf(buf, len, |
| 103 | "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x", |
| 104 | b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]); |
| 105 | } |
| 106 | |
| 107 | static ssize_t ft_wwn_show(void *arg, char *buf) |
| 108 | { |
| 109 | u64 *wwn = arg; |
| 110 | ssize_t len; |
| 111 | |
| 112 | len = ft_format_wwn(buf, PAGE_SIZE - 2, *wwn); |
| 113 | buf[len++] = '\n'; |
| 114 | return len; |
| 115 | } |
| 116 | |
| 117 | static ssize_t ft_wwn_store(void *arg, const char *buf, size_t len) |
| 118 | { |
| 119 | ssize_t ret; |
| 120 | u64 wwn; |
| 121 | |
| 122 | ret = ft_parse_wwn(buf, &wwn, 0); |
| 123 | if (ret > 0) |
| 124 | *(u64 *)arg = wwn; |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * ACL auth ops. |
| 130 | */ |
| 131 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 132 | static ssize_t ft_nacl_port_name_show(struct config_item *item, char *page) |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 133 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 134 | struct se_node_acl *se_nacl = acl_to_nacl(item); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 135 | struct ft_node_acl *acl = container_of(se_nacl, |
| 136 | struct ft_node_acl, se_node_acl); |
| 137 | |
| 138 | return ft_wwn_show(&acl->node_auth.port_name, page); |
| 139 | } |
| 140 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 141 | static ssize_t ft_nacl_port_name_store(struct config_item *item, |
| 142 | const char *page, size_t count) |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 143 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 144 | struct se_node_acl *se_nacl = acl_to_nacl(item); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 145 | struct ft_node_acl *acl = container_of(se_nacl, |
| 146 | struct ft_node_acl, se_node_acl); |
| 147 | |
| 148 | return ft_wwn_store(&acl->node_auth.port_name, page, count); |
| 149 | } |
| 150 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 151 | static ssize_t ft_nacl_node_name_show(struct config_item *item, |
| 152 | char *page) |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 153 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 154 | struct se_node_acl *se_nacl = acl_to_nacl(item); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 155 | struct ft_node_acl *acl = container_of(se_nacl, |
| 156 | struct ft_node_acl, se_node_acl); |
| 157 | |
| 158 | return ft_wwn_show(&acl->node_auth.node_name, page); |
| 159 | } |
| 160 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 161 | static ssize_t ft_nacl_node_name_store(struct config_item *item, |
| 162 | const char *page, size_t count) |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 163 | { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 164 | struct se_node_acl *se_nacl = acl_to_nacl(item); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 165 | struct ft_node_acl *acl = container_of(se_nacl, |
| 166 | struct ft_node_acl, se_node_acl); |
| 167 | |
| 168 | return ft_wwn_store(&acl->node_auth.node_name, page, count); |
| 169 | } |
| 170 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 171 | CONFIGFS_ATTR(ft_nacl_, node_name); |
| 172 | CONFIGFS_ATTR(ft_nacl_, port_name); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 173 | |
Andy Grover | 091b706 | 2015-12-21 18:48:05 -0800 | [diff] [blame] | 174 | static ssize_t ft_nacl_tag_show(struct config_item *item, |
| 175 | char *page) |
| 176 | { |
| 177 | return snprintf(page, PAGE_SIZE, "%s", acl_to_nacl(item)->acl_tag); |
| 178 | } |
| 179 | |
| 180 | static ssize_t ft_nacl_tag_store(struct config_item *item, |
| 181 | const char *page, size_t count) |
| 182 | { |
| 183 | struct se_node_acl *se_nacl = acl_to_nacl(item); |
| 184 | int ret; |
| 185 | |
| 186 | ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page); |
| 187 | |
| 188 | if (ret < 0) |
| 189 | return ret; |
| 190 | return count; |
| 191 | } |
| 192 | |
| 193 | CONFIGFS_ATTR(ft_nacl_, tag); |
| 194 | |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 195 | static struct configfs_attribute *ft_nacl_base_attrs[] = { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 196 | &ft_nacl_attr_port_name, |
| 197 | &ft_nacl_attr_node_name, |
Andy Grover | 091b706 | 2015-12-21 18:48:05 -0800 | [diff] [blame] | 198 | &ft_nacl_attr_tag, |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 199 | NULL, |
| 200 | }; |
| 201 | |
| 202 | /* |
| 203 | * ACL ops. |
| 204 | */ |
| 205 | |
| 206 | /* |
| 207 | * Add ACL for an initiator. The ACL is named arbitrarily. |
| 208 | * The port_name and/or node_name are attributes. |
| 209 | */ |
Christoph Hellwig | c7d6a80 | 2015-04-13 19:51:14 +0200 | [diff] [blame] | 210 | static int ft_init_nodeacl(struct se_node_acl *nacl, const char *name) |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 211 | { |
Christoph Hellwig | c7d6a80 | 2015-04-13 19:51:14 +0200 | [diff] [blame] | 212 | struct ft_node_acl *acl = |
| 213 | container_of(nacl, struct ft_node_acl, se_node_acl); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 214 | u64 wwpn; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 215 | |
| 216 | if (ft_parse_wwn(name, &wwpn, 1) < 0) |
Christoph Hellwig | c7d6a80 | 2015-04-13 19:51:14 +0200 | [diff] [blame] | 217 | return -EINVAL; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 218 | |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 219 | acl->node_auth.port_name = wwpn; |
Christoph Hellwig | c7d6a80 | 2015-04-13 19:51:14 +0200 | [diff] [blame] | 220 | return 0; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 223 | /* |
| 224 | * local_port port_group (tpg) ops. |
| 225 | */ |
| 226 | static struct se_portal_group *ft_add_tpg( |
| 227 | struct se_wwn *wwn, |
| 228 | struct config_group *group, |
| 229 | const char *name) |
| 230 | { |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 231 | struct ft_lport_wwn *ft_wwn; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 232 | struct ft_tpg *tpg; |
Mark Rustad | 06383f1 | 2012-04-03 10:24:52 -0700 | [diff] [blame] | 233 | struct workqueue_struct *wq; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 234 | unsigned long index; |
| 235 | int ret; |
| 236 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 237 | pr_debug("tcm_fc: add tpg %s\n", name); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 238 | |
| 239 | /* |
| 240 | * Name must be "tpgt_" followed by the index. |
| 241 | */ |
| 242 | if (strstr(name, "tpgt_") != name) |
| 243 | return NULL; |
Jingoo Han | 57103d7 | 2013-07-19 16:22:19 +0900 | [diff] [blame] | 244 | |
| 245 | ret = kstrtoul(name + 5, 10, &index); |
| 246 | if (ret) |
| 247 | return NULL; |
| 248 | if (index > UINT_MAX) |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 249 | return NULL; |
| 250 | |
Andy Grover | d242c1d | 2014-04-04 16:54:12 -0700 | [diff] [blame] | 251 | if ((index != 1)) { |
| 252 | pr_err("Error, a single TPG=1 is used for HW port mappings\n"); |
| 253 | return ERR_PTR(-ENOSYS); |
| 254 | } |
| 255 | |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 256 | ft_wwn = container_of(wwn, struct ft_lport_wwn, se_wwn); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 257 | tpg = kzalloc(sizeof(*tpg), GFP_KERNEL); |
| 258 | if (!tpg) |
| 259 | return NULL; |
| 260 | tpg->index = index; |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 261 | tpg->lport_wwn = ft_wwn; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 262 | INIT_LIST_HEAD(&tpg->lun_list); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 263 | |
Mark Rustad | 06383f1 | 2012-04-03 10:24:52 -0700 | [diff] [blame] | 264 | wq = alloc_workqueue("tcm_fc", 0, 1); |
| 265 | if (!wq) { |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 266 | kfree(tpg); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
Nicholas Bellinger | bc0c94b | 2015-05-20 21:48:03 -0700 | [diff] [blame] | 270 | ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP); |
Mark Rustad | 06383f1 | 2012-04-03 10:24:52 -0700 | [diff] [blame] | 271 | if (ret < 0) { |
| 272 | destroy_workqueue(wq); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 273 | kfree(tpg); |
| 274 | return NULL; |
| 275 | } |
Mark Rustad | 06383f1 | 2012-04-03 10:24:52 -0700 | [diff] [blame] | 276 | tpg->workqueue = wq; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 277 | |
| 278 | mutex_lock(&ft_lport_lock); |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 279 | ft_wwn->tpg = tpg; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 280 | mutex_unlock(&ft_lport_lock); |
| 281 | |
| 282 | return &tpg->se_tpg; |
| 283 | } |
| 284 | |
| 285 | static void ft_del_tpg(struct se_portal_group *se_tpg) |
| 286 | { |
| 287 | struct ft_tpg *tpg = container_of(se_tpg, struct ft_tpg, se_tpg); |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 288 | struct ft_lport_wwn *ft_wwn = tpg->lport_wwn; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 289 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 290 | pr_debug("del tpg %s\n", |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 291 | config_item_name(&tpg->se_tpg.tpg_group.cg_item)); |
| 292 | |
Christoph Hellwig | 58fc73d | 2011-08-26 09:25:38 -0700 | [diff] [blame] | 293 | destroy_workqueue(tpg->workqueue); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 294 | |
| 295 | /* Wait for sessions to be freed thru RCU, for BUG_ON below */ |
| 296 | synchronize_rcu(); |
| 297 | |
| 298 | mutex_lock(&ft_lport_lock); |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 299 | ft_wwn->tpg = NULL; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 300 | if (tpg->tport) { |
| 301 | tpg->tport->tpg = NULL; |
| 302 | tpg->tport = NULL; |
| 303 | } |
| 304 | mutex_unlock(&ft_lport_lock); |
| 305 | |
| 306 | core_tpg_deregister(se_tpg); |
| 307 | kfree(tpg); |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Verify that an lport is configured to use the tcm_fc module, and return |
| 312 | * the target port group that should be used. |
| 313 | * |
| 314 | * The caller holds ft_lport_lock. |
| 315 | */ |
| 316 | struct ft_tpg *ft_lport_find_tpg(struct fc_lport *lport) |
| 317 | { |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 318 | struct ft_lport_wwn *ft_wwn; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 319 | |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 320 | list_for_each_entry(ft_wwn, &ft_wwn_list, ft_wwn_node) { |
| 321 | if (ft_wwn->wwpn == lport->wwpn) |
| 322 | return ft_wwn->tpg; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 323 | } |
| 324 | return NULL; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * target config instance ops. |
| 329 | */ |
| 330 | |
| 331 | /* |
| 332 | * Add lport to allowed config. |
| 333 | * The name is the WWPN in lower-case ASCII, colon-separated bytes. |
| 334 | */ |
Andy Grover | 0d7cb93 | 2014-04-04 16:54:14 -0700 | [diff] [blame] | 335 | static struct se_wwn *ft_add_wwn( |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 336 | struct target_fabric_configfs *tf, |
| 337 | struct config_group *group, |
| 338 | const char *name) |
| 339 | { |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 340 | struct ft_lport_wwn *ft_wwn; |
| 341 | struct ft_lport_wwn *old_ft_wwn; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 342 | u64 wwpn; |
| 343 | |
Andy Grover | 0d7cb93 | 2014-04-04 16:54:14 -0700 | [diff] [blame] | 344 | pr_debug("add wwn %s\n", name); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 345 | if (ft_parse_wwn(name, &wwpn, 1) < 0) |
| 346 | return NULL; |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 347 | ft_wwn = kzalloc(sizeof(*ft_wwn), GFP_KERNEL); |
| 348 | if (!ft_wwn) |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 349 | return NULL; |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 350 | ft_wwn->wwpn = wwpn; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 351 | |
| 352 | mutex_lock(&ft_lport_lock); |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 353 | list_for_each_entry(old_ft_wwn, &ft_wwn_list, ft_wwn_node) { |
| 354 | if (old_ft_wwn->wwpn == wwpn) { |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 355 | mutex_unlock(&ft_lport_lock); |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 356 | kfree(ft_wwn); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 357 | return NULL; |
| 358 | } |
| 359 | } |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 360 | list_add_tail(&ft_wwn->ft_wwn_node, &ft_wwn_list); |
| 361 | ft_format_wwn(ft_wwn->name, sizeof(ft_wwn->name), wwpn); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 362 | mutex_unlock(&ft_lport_lock); |
| 363 | |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 364 | return &ft_wwn->se_wwn; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Andy Grover | 0d7cb93 | 2014-04-04 16:54:14 -0700 | [diff] [blame] | 367 | static void ft_del_wwn(struct se_wwn *wwn) |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 368 | { |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 369 | struct ft_lport_wwn *ft_wwn = container_of(wwn, |
| 370 | struct ft_lport_wwn, se_wwn); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 371 | |
Andy Grover | 0d7cb93 | 2014-04-04 16:54:14 -0700 | [diff] [blame] | 372 | pr_debug("del wwn %s\n", ft_wwn->name); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 373 | mutex_lock(&ft_lport_lock); |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 374 | list_del(&ft_wwn->ft_wwn_node); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 375 | mutex_unlock(&ft_lport_lock); |
| 376 | |
Andy Grover | 705665d | 2014-04-04 16:54:13 -0700 | [diff] [blame] | 377 | kfree(ft_wwn); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 380 | static ssize_t ft_wwn_version_show(struct config_item *item, char *page) |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 381 | { |
| 382 | return sprintf(page, "TCM FC " FT_VERSION " on %s/%s on " |
| 383 | ""UTS_RELEASE"\n", utsname()->sysname, utsname()->machine); |
| 384 | } |
| 385 | |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 386 | CONFIGFS_ATTR_RO(ft_wwn_, version); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 387 | |
| 388 | static struct configfs_attribute *ft_wwn_attrs[] = { |
Christoph Hellwig | 2eafd72 | 2015-10-03 15:32:55 +0200 | [diff] [blame] | 389 | &ft_wwn_attr_version, |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 390 | NULL, |
| 391 | }; |
| 392 | |
Christoph Hellwig | 3868e43 | 2015-05-01 17:47:55 +0200 | [diff] [blame] | 393 | static inline struct ft_tpg *ft_tpg(struct se_portal_group *se_tpg) |
| 394 | { |
| 395 | return container_of(se_tpg, struct ft_tpg, se_tpg); |
| 396 | } |
| 397 | |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 398 | static char *ft_get_fabric_name(void) |
| 399 | { |
| 400 | return "fc"; |
| 401 | } |
| 402 | |
| 403 | static char *ft_get_fabric_wwn(struct se_portal_group *se_tpg) |
| 404 | { |
Christoph Hellwig | 3868e43 | 2015-05-01 17:47:55 +0200 | [diff] [blame] | 405 | return ft_tpg(se_tpg)->lport_wwn->name; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static u16 ft_get_tag(struct se_portal_group *se_tpg) |
| 409 | { |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 410 | /* |
| 411 | * This tag is used when forming SCSI Name identifier in EVPD=1 0x83 |
| 412 | * to represent the SCSI Target Port. |
| 413 | */ |
Christoph Hellwig | 3868e43 | 2015-05-01 17:47:55 +0200 | [diff] [blame] | 414 | return ft_tpg(se_tpg)->index; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static int ft_check_false(struct se_portal_group *se_tpg) |
| 418 | { |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | static void ft_set_default_node_attr(struct se_node_acl *se_nacl) |
| 423 | { |
| 424 | } |
| 425 | |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 426 | static u32 ft_tpg_get_inst_index(struct se_portal_group *se_tpg) |
| 427 | { |
Christoph Hellwig | 3868e43 | 2015-05-01 17:47:55 +0200 | [diff] [blame] | 428 | return ft_tpg(se_tpg)->index; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 431 | static const struct target_core_fabric_ops ft_fabric_ops = { |
| 432 | .module = THIS_MODULE, |
| 433 | .name = "fc", |
Christoph Hellwig | 144bc4c | 2015-04-13 19:51:16 +0200 | [diff] [blame] | 434 | .node_acl_size = sizeof(struct ft_node_acl), |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 435 | .get_fabric_name = ft_get_fabric_name, |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 436 | .tpg_get_wwn = ft_get_fabric_wwn, |
| 437 | .tpg_get_tag = ft_get_tag, |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 438 | .tpg_check_demo_mode = ft_check_false, |
| 439 | .tpg_check_demo_mode_cache = ft_check_false, |
| 440 | .tpg_check_demo_mode_write_protect = ft_check_false, |
| 441 | .tpg_check_prod_mode_write_protect = ft_check_false, |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 442 | .tpg_get_inst_index = ft_tpg_get_inst_index, |
| 443 | .check_stop_free = ft_check_stop_free, |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 444 | .release_cmd = ft_release_cmd, |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 445 | .close_session = ft_sess_close, |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 446 | .sess_get_index = ft_sess_get_index, |
| 447 | .sess_get_initiator_sid = NULL, |
| 448 | .write_pending = ft_write_pending, |
| 449 | .write_pending_status = ft_write_pending_status, |
| 450 | .set_default_node_attributes = ft_set_default_node_attr, |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 451 | .get_cmd_state = ft_get_cmd_state, |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 452 | .queue_data_in = ft_queue_data_in, |
| 453 | .queue_status = ft_queue_status, |
| 454 | .queue_tm_rsp = ft_queue_tm_resp, |
Nicholas Bellinger | 131e6ab | 2014-03-22 14:55:56 -0700 | [diff] [blame] | 455 | .aborted_task = ft_aborted_task, |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 456 | /* |
| 457 | * Setup function pointers for generic logic in |
| 458 | * target_core_fabric_configfs.c |
| 459 | */ |
Andy Grover | 0d7cb93 | 2014-04-04 16:54:14 -0700 | [diff] [blame] | 460 | .fabric_make_wwn = &ft_add_wwn, |
| 461 | .fabric_drop_wwn = &ft_del_wwn, |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 462 | .fabric_make_tpg = &ft_add_tpg, |
| 463 | .fabric_drop_tpg = &ft_del_tpg, |
Christoph Hellwig | c7d6a80 | 2015-04-13 19:51:14 +0200 | [diff] [blame] | 464 | .fabric_init_nodeacl = &ft_init_nodeacl, |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 465 | |
| 466 | .tfc_wwn_attrs = ft_wwn_attrs, |
| 467 | .tfc_tpg_nacl_base_attrs = ft_nacl_base_attrs, |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 468 | }; |
| 469 | |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 470 | static struct notifier_block ft_notifier = { |
| 471 | .notifier_call = ft_lport_notify |
| 472 | }; |
| 473 | |
| 474 | static int __init ft_init(void) |
| 475 | { |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 476 | int ret; |
| 477 | |
| 478 | ret = target_register_template(&ft_fabric_ops); |
| 479 | if (ret) |
| 480 | goto out; |
| 481 | |
| 482 | ret = fc_fc4_register_provider(FC_TYPE_FCP, &ft_prov); |
| 483 | if (ret) |
| 484 | goto out_unregister_template; |
| 485 | |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 486 | blocking_notifier_chain_register(&fc_lport_notifier_head, &ft_notifier); |
| 487 | fc_lport_iterate(ft_lport_add, NULL); |
| 488 | return 0; |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 489 | |
| 490 | out_unregister_template: |
| 491 | target_unregister_template(&ft_fabric_ops); |
| 492 | out: |
| 493 | return ret; |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static void __exit ft_exit(void) |
| 497 | { |
| 498 | blocking_notifier_chain_unregister(&fc_lport_notifier_head, |
| 499 | &ft_notifier); |
| 500 | fc_fc4_deregister_provider(FC_TYPE_FCP, &ft_prov); |
| 501 | fc_lport_iterate(ft_lport_del, NULL); |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 502 | target_unregister_template(&ft_fabric_ops); |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 503 | synchronize_rcu(); |
| 504 | } |
| 505 | |
Kiran Patil | 3699d92 | 2011-04-18 16:24:14 -0700 | [diff] [blame] | 506 | MODULE_DESCRIPTION("FC TCM fabric driver " FT_VERSION); |
| 507 | MODULE_LICENSE("GPL"); |
| 508 | module_init(ft_init); |
| 509 | module_exit(ft_exit); |