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