1a07b4970SChristoph Hellwig /* 2a07b4970SChristoph Hellwig * Configfs interface for the NVMe target. 3a07b4970SChristoph Hellwig * Copyright (c) 2015-2016 HGST, a Western Digital Company. 4a07b4970SChristoph Hellwig * 5a07b4970SChristoph Hellwig * This program is free software; you can redistribute it and/or modify it 6a07b4970SChristoph Hellwig * under the terms and conditions of the GNU General Public License, 7a07b4970SChristoph Hellwig * version 2, as published by the Free Software Foundation. 8a07b4970SChristoph Hellwig * 9a07b4970SChristoph Hellwig * This program is distributed in the hope it will be useful, but WITHOUT 10a07b4970SChristoph Hellwig * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11a07b4970SChristoph Hellwig * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12a07b4970SChristoph Hellwig * more details. 13a07b4970SChristoph Hellwig */ 14a07b4970SChristoph Hellwig #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15a07b4970SChristoph Hellwig #include <linux/kernel.h> 16a07b4970SChristoph Hellwig #include <linux/module.h> 17a07b4970SChristoph Hellwig #include <linux/slab.h> 18a07b4970SChristoph Hellwig #include <linux/stat.h> 19a07b4970SChristoph Hellwig #include <linux/ctype.h> 20c6925093SLogan Gunthorpe #include <linux/pci.h> 21c6925093SLogan Gunthorpe #include <linux/pci-p2pdma.h> 22a07b4970SChristoph Hellwig 23a07b4970SChristoph Hellwig #include "nvmet.h" 24a07b4970SChristoph Hellwig 2566603a31SBhumika Goyal static const struct config_item_type nvmet_host_type; 2666603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type; 27a07b4970SChristoph Hellwig 28b662a078SJay Sternberg static LIST_HEAD(nvmet_ports_list); 29b662a078SJay Sternberg struct list_head *nvmet_ports = &nvmet_ports_list; 30b662a078SJay Sternberg 31a5d18612SChristoph Hellwig static const struct nvmet_transport_name { 32a5d18612SChristoph Hellwig u8 type; 33a5d18612SChristoph Hellwig const char *name; 34a5d18612SChristoph Hellwig } nvmet_transport_names[] = { 35a5d18612SChristoph Hellwig { NVMF_TRTYPE_RDMA, "rdma" }, 36a5d18612SChristoph Hellwig { NVMF_TRTYPE_FC, "fc" }, 37a5d18612SChristoph Hellwig { NVMF_TRTYPE_LOOP, "loop" }, 38a5d18612SChristoph Hellwig }; 39a5d18612SChristoph Hellwig 40a07b4970SChristoph Hellwig /* 41a07b4970SChristoph Hellwig * nvmet_port Generic ConfigFS definitions. 42a07b4970SChristoph Hellwig * Used in any place in the ConfigFS tree that refers to an address. 43a07b4970SChristoph Hellwig */ 44a07b4970SChristoph Hellwig static ssize_t nvmet_addr_adrfam_show(struct config_item *item, 45a07b4970SChristoph Hellwig char *page) 46a07b4970SChristoph Hellwig { 47a07b4970SChristoph Hellwig switch (to_nvmet_port(item)->disc_addr.adrfam) { 48a07b4970SChristoph Hellwig case NVMF_ADDR_FAMILY_IP4: 49a07b4970SChristoph Hellwig return sprintf(page, "ipv4\n"); 50a07b4970SChristoph Hellwig case NVMF_ADDR_FAMILY_IP6: 51a07b4970SChristoph Hellwig return sprintf(page, "ipv6\n"); 52a07b4970SChristoph Hellwig case NVMF_ADDR_FAMILY_IB: 53a07b4970SChristoph Hellwig return sprintf(page, "ib\n"); 54885aa401SJames Smart case NVMF_ADDR_FAMILY_FC: 55885aa401SJames Smart return sprintf(page, "fc\n"); 56a07b4970SChristoph Hellwig default: 57a07b4970SChristoph Hellwig return sprintf(page, "\n"); 58a07b4970SChristoph Hellwig } 59a07b4970SChristoph Hellwig } 60a07b4970SChristoph Hellwig 61a07b4970SChristoph Hellwig static ssize_t nvmet_addr_adrfam_store(struct config_item *item, 62a07b4970SChristoph Hellwig const char *page, size_t count) 63a07b4970SChristoph Hellwig { 64a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 65a07b4970SChristoph Hellwig 66a07b4970SChristoph Hellwig if (port->enabled) { 67a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 68a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 69a07b4970SChristoph Hellwig return -EACCES; 70a07b4970SChristoph Hellwig } 71a07b4970SChristoph Hellwig 72a07b4970SChristoph Hellwig if (sysfs_streq(page, "ipv4")) { 73a07b4970SChristoph Hellwig port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IP4; 74a07b4970SChristoph Hellwig } else if (sysfs_streq(page, "ipv6")) { 75a07b4970SChristoph Hellwig port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IP6; 76a07b4970SChristoph Hellwig } else if (sysfs_streq(page, "ib")) { 77a07b4970SChristoph Hellwig port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IB; 78885aa401SJames Smart } else if (sysfs_streq(page, "fc")) { 79885aa401SJames Smart port->disc_addr.adrfam = NVMF_ADDR_FAMILY_FC; 80a07b4970SChristoph Hellwig } else { 81a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for adrfam\n", page); 82a07b4970SChristoph Hellwig return -EINVAL; 83a07b4970SChristoph Hellwig } 84a07b4970SChristoph Hellwig 85a07b4970SChristoph Hellwig return count; 86a07b4970SChristoph Hellwig } 87a07b4970SChristoph Hellwig 88a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_adrfam); 89a07b4970SChristoph Hellwig 90a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_show(struct config_item *item, 91a07b4970SChristoph Hellwig char *page) 92a07b4970SChristoph Hellwig { 93a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 94a07b4970SChristoph Hellwig 95a07b4970SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%d\n", 96a07b4970SChristoph Hellwig le16_to_cpu(port->disc_addr.portid)); 97a07b4970SChristoph Hellwig } 98a07b4970SChristoph Hellwig 99a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_store(struct config_item *item, 100a07b4970SChristoph Hellwig const char *page, size_t count) 101a07b4970SChristoph Hellwig { 102a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 103a07b4970SChristoph Hellwig u16 portid = 0; 104a07b4970SChristoph Hellwig 105a07b4970SChristoph Hellwig if (kstrtou16(page, 0, &portid)) { 106a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for portid\n", page); 107a07b4970SChristoph Hellwig return -EINVAL; 108a07b4970SChristoph Hellwig } 109a07b4970SChristoph Hellwig 110a07b4970SChristoph Hellwig if (port->enabled) { 111a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 112a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 113a07b4970SChristoph Hellwig return -EACCES; 114a07b4970SChristoph Hellwig } 115a07b4970SChristoph Hellwig port->disc_addr.portid = cpu_to_le16(portid); 116a07b4970SChristoph Hellwig return count; 117a07b4970SChristoph Hellwig } 118a07b4970SChristoph Hellwig 119a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_portid); 120a07b4970SChristoph Hellwig 121a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_show(struct config_item *item, 122a07b4970SChristoph Hellwig char *page) 123a07b4970SChristoph Hellwig { 124a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 125a07b4970SChristoph Hellwig 126a07b4970SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%s\n", 127a07b4970SChristoph Hellwig port->disc_addr.traddr); 128a07b4970SChristoph Hellwig } 129a07b4970SChristoph Hellwig 130a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_store(struct config_item *item, 131a07b4970SChristoph Hellwig const char *page, size_t count) 132a07b4970SChristoph Hellwig { 133a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 134a07b4970SChristoph Hellwig 135a07b4970SChristoph Hellwig if (count > NVMF_TRADDR_SIZE) { 136a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for traddr\n", page); 137a07b4970SChristoph Hellwig return -EINVAL; 138a07b4970SChristoph Hellwig } 139a07b4970SChristoph Hellwig 140a07b4970SChristoph Hellwig if (port->enabled) { 141a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 142a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 143a07b4970SChristoph Hellwig return -EACCES; 144a07b4970SChristoph Hellwig } 1459ba2a5cbSSagi Grimberg 1469ba2a5cbSSagi Grimberg if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1) 1479ba2a5cbSSagi Grimberg return -EINVAL; 1489ba2a5cbSSagi Grimberg return count; 149a07b4970SChristoph Hellwig } 150a07b4970SChristoph Hellwig 151a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_traddr); 152a07b4970SChristoph Hellwig 153a07b4970SChristoph Hellwig static ssize_t nvmet_addr_treq_show(struct config_item *item, 154a07b4970SChristoph Hellwig char *page) 155a07b4970SChristoph Hellwig { 156a07b4970SChristoph Hellwig switch (to_nvmet_port(item)->disc_addr.treq) { 157a07b4970SChristoph Hellwig case NVMF_TREQ_NOT_SPECIFIED: 158a07b4970SChristoph Hellwig return sprintf(page, "not specified\n"); 159a07b4970SChristoph Hellwig case NVMF_TREQ_REQUIRED: 160a07b4970SChristoph Hellwig return sprintf(page, "required\n"); 161a07b4970SChristoph Hellwig case NVMF_TREQ_NOT_REQUIRED: 162a07b4970SChristoph Hellwig return sprintf(page, "not required\n"); 163a07b4970SChristoph Hellwig default: 164a07b4970SChristoph Hellwig return sprintf(page, "\n"); 165a07b4970SChristoph Hellwig } 166a07b4970SChristoph Hellwig } 167a07b4970SChristoph Hellwig 168a07b4970SChristoph Hellwig static ssize_t nvmet_addr_treq_store(struct config_item *item, 169a07b4970SChristoph Hellwig const char *page, size_t count) 170a07b4970SChristoph Hellwig { 171a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 172a07b4970SChristoph Hellwig 173a07b4970SChristoph Hellwig if (port->enabled) { 174a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 175a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 176a07b4970SChristoph Hellwig return -EACCES; 177a07b4970SChristoph Hellwig } 178a07b4970SChristoph Hellwig 179a07b4970SChristoph Hellwig if (sysfs_streq(page, "not specified")) { 180a07b4970SChristoph Hellwig port->disc_addr.treq = NVMF_TREQ_NOT_SPECIFIED; 181a07b4970SChristoph Hellwig } else if (sysfs_streq(page, "required")) { 182a07b4970SChristoph Hellwig port->disc_addr.treq = NVMF_TREQ_REQUIRED; 183a07b4970SChristoph Hellwig } else if (sysfs_streq(page, "not required")) { 184a07b4970SChristoph Hellwig port->disc_addr.treq = NVMF_TREQ_NOT_REQUIRED; 185a07b4970SChristoph Hellwig } else { 186a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for treq\n", page); 187a07b4970SChristoph Hellwig return -EINVAL; 188a07b4970SChristoph Hellwig } 189a07b4970SChristoph Hellwig 190a07b4970SChristoph Hellwig return count; 191a07b4970SChristoph Hellwig } 192a07b4970SChristoph Hellwig 193a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_treq); 194a07b4970SChristoph Hellwig 195a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_show(struct config_item *item, 196a07b4970SChristoph Hellwig char *page) 197a07b4970SChristoph Hellwig { 198a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 199a07b4970SChristoph Hellwig 200a07b4970SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%s\n", 201a07b4970SChristoph Hellwig port->disc_addr.trsvcid); 202a07b4970SChristoph Hellwig } 203a07b4970SChristoph Hellwig 204a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_store(struct config_item *item, 205a07b4970SChristoph Hellwig const char *page, size_t count) 206a07b4970SChristoph Hellwig { 207a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 208a07b4970SChristoph Hellwig 209a07b4970SChristoph Hellwig if (count > NVMF_TRSVCID_SIZE) { 210a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for trsvcid\n", page); 211a07b4970SChristoph Hellwig return -EINVAL; 212a07b4970SChristoph Hellwig } 213a07b4970SChristoph Hellwig if (port->enabled) { 214a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 215a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 216a07b4970SChristoph Hellwig return -EACCES; 217a07b4970SChristoph Hellwig } 2189ba2a5cbSSagi Grimberg 2199ba2a5cbSSagi Grimberg if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1) 2209ba2a5cbSSagi Grimberg return -EINVAL; 2219ba2a5cbSSagi Grimberg return count; 222a07b4970SChristoph Hellwig } 223a07b4970SChristoph Hellwig 224a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trsvcid); 225a07b4970SChristoph Hellwig 2260d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_show(struct config_item *item, 2270d5ee2b2SSteve Wise char *page) 2280d5ee2b2SSteve Wise { 2290d5ee2b2SSteve Wise struct nvmet_port *port = to_nvmet_port(item); 2300d5ee2b2SSteve Wise 2310d5ee2b2SSteve Wise return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size); 2320d5ee2b2SSteve Wise } 2330d5ee2b2SSteve Wise 2340d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_store(struct config_item *item, 2350d5ee2b2SSteve Wise const char *page, size_t count) 2360d5ee2b2SSteve Wise { 2370d5ee2b2SSteve Wise struct nvmet_port *port = to_nvmet_port(item); 2380d5ee2b2SSteve Wise int ret; 2390d5ee2b2SSteve Wise 2400d5ee2b2SSteve Wise if (port->enabled) { 2410d5ee2b2SSteve Wise pr_err("Cannot modify inline_data_size while port enabled\n"); 2420d5ee2b2SSteve Wise pr_err("Disable the port before modifying\n"); 2430d5ee2b2SSteve Wise return -EACCES; 2440d5ee2b2SSteve Wise } 2450d5ee2b2SSteve Wise ret = kstrtoint(page, 0, &port->inline_data_size); 2460d5ee2b2SSteve Wise if (ret) { 2470d5ee2b2SSteve Wise pr_err("Invalid value '%s' for inline_data_size\n", page); 2480d5ee2b2SSteve Wise return -EINVAL; 2490d5ee2b2SSteve Wise } 2500d5ee2b2SSteve Wise return count; 2510d5ee2b2SSteve Wise } 2520d5ee2b2SSteve Wise 2530d5ee2b2SSteve Wise CONFIGFS_ATTR(nvmet_, param_inline_data_size); 2540d5ee2b2SSteve Wise 255a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_show(struct config_item *item, 256a07b4970SChristoph Hellwig char *page) 257a07b4970SChristoph Hellwig { 258a5d18612SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 259a5d18612SChristoph Hellwig int i; 260a5d18612SChristoph Hellwig 261a5d18612SChristoph Hellwig for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) { 262a5d18612SChristoph Hellwig if (port->disc_addr.trtype != nvmet_transport_names[i].type) 263a5d18612SChristoph Hellwig continue; 264a5d18612SChristoph Hellwig return sprintf(page, "%s\n", nvmet_transport_names[i].name); 265a07b4970SChristoph Hellwig } 266a5d18612SChristoph Hellwig 267a5d18612SChristoph Hellwig return sprintf(page, "\n"); 268a07b4970SChristoph Hellwig } 269a07b4970SChristoph Hellwig 270a07b4970SChristoph Hellwig static void nvmet_port_init_tsas_rdma(struct nvmet_port *port) 271a07b4970SChristoph Hellwig { 272a07b4970SChristoph Hellwig port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED; 273a07b4970SChristoph Hellwig port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED; 274a07b4970SChristoph Hellwig port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM; 275a07b4970SChristoph Hellwig } 276a07b4970SChristoph Hellwig 277a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_store(struct config_item *item, 278a07b4970SChristoph Hellwig const char *page, size_t count) 279a07b4970SChristoph Hellwig { 280a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 281a5d18612SChristoph Hellwig int i; 282a07b4970SChristoph Hellwig 283a07b4970SChristoph Hellwig if (port->enabled) { 284a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 285a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 286a07b4970SChristoph Hellwig return -EACCES; 287a07b4970SChristoph Hellwig } 288a07b4970SChristoph Hellwig 289a5d18612SChristoph Hellwig for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) { 290a5d18612SChristoph Hellwig if (sysfs_streq(page, nvmet_transport_names[i].name)) 291a5d18612SChristoph Hellwig goto found; 292a07b4970SChristoph Hellwig } 293a07b4970SChristoph Hellwig 294a5d18612SChristoph Hellwig pr_err("Invalid value '%s' for trtype\n", page); 295a5d18612SChristoph Hellwig return -EINVAL; 296a5d18612SChristoph Hellwig found: 297a5d18612SChristoph Hellwig memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE); 298a5d18612SChristoph Hellwig port->disc_addr.trtype = nvmet_transport_names[i].type; 299a5d18612SChristoph Hellwig if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA) 300a5d18612SChristoph Hellwig nvmet_port_init_tsas_rdma(port); 301a07b4970SChristoph Hellwig return count; 302a07b4970SChristoph Hellwig } 303a07b4970SChristoph Hellwig 304a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trtype); 305a07b4970SChristoph Hellwig 306a07b4970SChristoph Hellwig /* 307a07b4970SChristoph Hellwig * Namespace structures & file operation functions below 308a07b4970SChristoph Hellwig */ 309a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page) 310a07b4970SChristoph Hellwig { 311a07b4970SChristoph Hellwig return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path); 312a07b4970SChristoph Hellwig } 313a07b4970SChristoph Hellwig 314a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_store(struct config_item *item, 315a07b4970SChristoph Hellwig const char *page, size_t count) 316a07b4970SChristoph Hellwig { 317a07b4970SChristoph Hellwig struct nvmet_ns *ns = to_nvmet_ns(item); 318a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = ns->subsys; 3195613d312SHannes Reinecke size_t len; 320a07b4970SChristoph Hellwig int ret; 321a07b4970SChristoph Hellwig 322a07b4970SChristoph Hellwig mutex_lock(&subsys->lock); 323a07b4970SChristoph Hellwig ret = -EBUSY; 324e4fcf07cSSolganik Alexander if (ns->enabled) 325a07b4970SChristoph Hellwig goto out_unlock; 326a07b4970SChristoph Hellwig 3275613d312SHannes Reinecke ret = -EINVAL; 3285613d312SHannes Reinecke len = strcspn(page, "\n"); 3295613d312SHannes Reinecke if (!len) 3305613d312SHannes Reinecke goto out_unlock; 331a07b4970SChristoph Hellwig 3325613d312SHannes Reinecke kfree(ns->device_path); 333a07b4970SChristoph Hellwig ret = -ENOMEM; 3345613d312SHannes Reinecke ns->device_path = kstrndup(page, len, GFP_KERNEL); 335a07b4970SChristoph Hellwig if (!ns->device_path) 336a07b4970SChristoph Hellwig goto out_unlock; 337a07b4970SChristoph Hellwig 338a07b4970SChristoph Hellwig mutex_unlock(&subsys->lock); 339a07b4970SChristoph Hellwig return count; 340a07b4970SChristoph Hellwig 341a07b4970SChristoph Hellwig out_unlock: 342a07b4970SChristoph Hellwig mutex_unlock(&subsys->lock); 343a07b4970SChristoph Hellwig return ret; 344a07b4970SChristoph Hellwig } 345a07b4970SChristoph Hellwig 346a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_path); 347a07b4970SChristoph Hellwig 348c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA 349c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page) 350c6925093SLogan Gunthorpe { 351c6925093SLogan Gunthorpe struct nvmet_ns *ns = to_nvmet_ns(item); 352c6925093SLogan Gunthorpe 353c6925093SLogan Gunthorpe return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem); 354c6925093SLogan Gunthorpe } 355c6925093SLogan Gunthorpe 356c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_store(struct config_item *item, 357c6925093SLogan Gunthorpe const char *page, size_t count) 358c6925093SLogan Gunthorpe { 359c6925093SLogan Gunthorpe struct nvmet_ns *ns = to_nvmet_ns(item); 360c6925093SLogan Gunthorpe struct pci_dev *p2p_dev = NULL; 361c6925093SLogan Gunthorpe bool use_p2pmem; 362c6925093SLogan Gunthorpe int ret = count; 363c6925093SLogan Gunthorpe int error; 364c6925093SLogan Gunthorpe 365c6925093SLogan Gunthorpe mutex_lock(&ns->subsys->lock); 366c6925093SLogan Gunthorpe if (ns->enabled) { 367c6925093SLogan Gunthorpe ret = -EBUSY; 368c6925093SLogan Gunthorpe goto out_unlock; 369c6925093SLogan Gunthorpe } 370c6925093SLogan Gunthorpe 371c6925093SLogan Gunthorpe error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem); 372c6925093SLogan Gunthorpe if (error) { 373c6925093SLogan Gunthorpe ret = error; 374c6925093SLogan Gunthorpe goto out_unlock; 375c6925093SLogan Gunthorpe } 376c6925093SLogan Gunthorpe 377c6925093SLogan Gunthorpe ns->use_p2pmem = use_p2pmem; 378c6925093SLogan Gunthorpe pci_dev_put(ns->p2p_dev); 379c6925093SLogan Gunthorpe ns->p2p_dev = p2p_dev; 380c6925093SLogan Gunthorpe 381c6925093SLogan Gunthorpe out_unlock: 382c6925093SLogan Gunthorpe mutex_unlock(&ns->subsys->lock); 383c6925093SLogan Gunthorpe 384c6925093SLogan Gunthorpe return ret; 385c6925093SLogan Gunthorpe } 386c6925093SLogan Gunthorpe 387c6925093SLogan Gunthorpe CONFIGFS_ATTR(nvmet_ns_, p2pmem); 388c6925093SLogan Gunthorpe #endif /* CONFIG_PCI_P2PDMA */ 389c6925093SLogan Gunthorpe 390430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page) 391430c7befSJohannes Thumshirn { 392430c7befSJohannes Thumshirn return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid); 393430c7befSJohannes Thumshirn } 394430c7befSJohannes Thumshirn 395430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_store(struct config_item *item, 396430c7befSJohannes Thumshirn const char *page, size_t count) 397430c7befSJohannes Thumshirn { 398430c7befSJohannes Thumshirn struct nvmet_ns *ns = to_nvmet_ns(item); 399430c7befSJohannes Thumshirn struct nvmet_subsys *subsys = ns->subsys; 400430c7befSJohannes Thumshirn int ret = 0; 401430c7befSJohannes Thumshirn 402430c7befSJohannes Thumshirn 403430c7befSJohannes Thumshirn mutex_lock(&subsys->lock); 404430c7befSJohannes Thumshirn if (ns->enabled) { 405430c7befSJohannes Thumshirn ret = -EBUSY; 406430c7befSJohannes Thumshirn goto out_unlock; 407430c7befSJohannes Thumshirn } 408430c7befSJohannes Thumshirn 409430c7befSJohannes Thumshirn 410430c7befSJohannes Thumshirn if (uuid_parse(page, &ns->uuid)) 411430c7befSJohannes Thumshirn ret = -EINVAL; 412430c7befSJohannes Thumshirn 413430c7befSJohannes Thumshirn out_unlock: 414430c7befSJohannes Thumshirn mutex_unlock(&subsys->lock); 415430c7befSJohannes Thumshirn return ret ? ret : count; 416430c7befSJohannes Thumshirn } 417430c7befSJohannes Thumshirn 418f871749aSMax Gurtovoy CONFIGFS_ATTR(nvmet_ns_, device_uuid); 419f871749aSMax Gurtovoy 420a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page) 421a07b4970SChristoph Hellwig { 422a07b4970SChristoph Hellwig return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid); 423a07b4970SChristoph Hellwig } 424a07b4970SChristoph Hellwig 425a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_store(struct config_item *item, 426a07b4970SChristoph Hellwig const char *page, size_t count) 427a07b4970SChristoph Hellwig { 428a07b4970SChristoph Hellwig struct nvmet_ns *ns = to_nvmet_ns(item); 429a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = ns->subsys; 430a07b4970SChristoph Hellwig u8 nguid[16]; 431a07b4970SChristoph Hellwig const char *p = page; 432a07b4970SChristoph Hellwig int i; 433a07b4970SChristoph Hellwig int ret = 0; 434a07b4970SChristoph Hellwig 435a07b4970SChristoph Hellwig mutex_lock(&subsys->lock); 436e4fcf07cSSolganik Alexander if (ns->enabled) { 437a07b4970SChristoph Hellwig ret = -EBUSY; 438a07b4970SChristoph Hellwig goto out_unlock; 439a07b4970SChristoph Hellwig } 440a07b4970SChristoph Hellwig 441a07b4970SChristoph Hellwig for (i = 0; i < 16; i++) { 442a07b4970SChristoph Hellwig if (p + 2 > page + count) { 443a07b4970SChristoph Hellwig ret = -EINVAL; 444a07b4970SChristoph Hellwig goto out_unlock; 445a07b4970SChristoph Hellwig } 446a07b4970SChristoph Hellwig if (!isxdigit(p[0]) || !isxdigit(p[1])) { 447a07b4970SChristoph Hellwig ret = -EINVAL; 448a07b4970SChristoph Hellwig goto out_unlock; 449a07b4970SChristoph Hellwig } 450a07b4970SChristoph Hellwig 451a07b4970SChristoph Hellwig nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]); 452a07b4970SChristoph Hellwig p += 2; 453a07b4970SChristoph Hellwig 454a07b4970SChristoph Hellwig if (*p == '-' || *p == ':') 455a07b4970SChristoph Hellwig p++; 456a07b4970SChristoph Hellwig } 457a07b4970SChristoph Hellwig 458a07b4970SChristoph Hellwig memcpy(&ns->nguid, nguid, sizeof(nguid)); 459a07b4970SChristoph Hellwig out_unlock: 460a07b4970SChristoph Hellwig mutex_unlock(&subsys->lock); 461a07b4970SChristoph Hellwig return ret ? ret : count; 462a07b4970SChristoph Hellwig } 463a07b4970SChristoph Hellwig 464a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_nguid); 465a07b4970SChristoph Hellwig 46662ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page) 46762ac0d32SChristoph Hellwig { 46862ac0d32SChristoph Hellwig return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid); 46962ac0d32SChristoph Hellwig } 47062ac0d32SChristoph Hellwig 47162ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item, 47262ac0d32SChristoph Hellwig const char *page, size_t count) 47362ac0d32SChristoph Hellwig { 47462ac0d32SChristoph Hellwig struct nvmet_ns *ns = to_nvmet_ns(item); 47562ac0d32SChristoph Hellwig u32 oldgrpid, newgrpid; 47662ac0d32SChristoph Hellwig int ret; 47762ac0d32SChristoph Hellwig 47862ac0d32SChristoph Hellwig ret = kstrtou32(page, 0, &newgrpid); 47962ac0d32SChristoph Hellwig if (ret) 48062ac0d32SChristoph Hellwig return ret; 48162ac0d32SChristoph Hellwig 48262ac0d32SChristoph Hellwig if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS) 48362ac0d32SChristoph Hellwig return -EINVAL; 48462ac0d32SChristoph Hellwig 48562ac0d32SChristoph Hellwig down_write(&nvmet_ana_sem); 48662ac0d32SChristoph Hellwig oldgrpid = ns->anagrpid; 48762ac0d32SChristoph Hellwig nvmet_ana_group_enabled[newgrpid]++; 48862ac0d32SChristoph Hellwig ns->anagrpid = newgrpid; 48962ac0d32SChristoph Hellwig nvmet_ana_group_enabled[oldgrpid]--; 49062ac0d32SChristoph Hellwig nvmet_ana_chgcnt++; 49162ac0d32SChristoph Hellwig up_write(&nvmet_ana_sem); 49262ac0d32SChristoph Hellwig 49362ac0d32SChristoph Hellwig nvmet_send_ana_event(ns->subsys, NULL); 49462ac0d32SChristoph Hellwig return count; 49562ac0d32SChristoph Hellwig } 49662ac0d32SChristoph Hellwig 49762ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, ana_grpid); 49862ac0d32SChristoph Hellwig 499a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page) 500a07b4970SChristoph Hellwig { 501e4fcf07cSSolganik Alexander return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled); 502a07b4970SChristoph Hellwig } 503a07b4970SChristoph Hellwig 504a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_store(struct config_item *item, 505a07b4970SChristoph Hellwig const char *page, size_t count) 506a07b4970SChristoph Hellwig { 507a07b4970SChristoph Hellwig struct nvmet_ns *ns = to_nvmet_ns(item); 508a07b4970SChristoph Hellwig bool enable; 509a07b4970SChristoph Hellwig int ret = 0; 510a07b4970SChristoph Hellwig 511a07b4970SChristoph Hellwig if (strtobool(page, &enable)) 512a07b4970SChristoph Hellwig return -EINVAL; 513a07b4970SChristoph Hellwig 514a07b4970SChristoph Hellwig if (enable) 515a07b4970SChristoph Hellwig ret = nvmet_ns_enable(ns); 516a07b4970SChristoph Hellwig else 517a07b4970SChristoph Hellwig nvmet_ns_disable(ns); 518a07b4970SChristoph Hellwig 519a07b4970SChristoph Hellwig return ret ? ret : count; 520a07b4970SChristoph Hellwig } 521a07b4970SChristoph Hellwig 522a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, enable); 523a07b4970SChristoph Hellwig 52455eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page) 52555eb942eSChaitanya Kulkarni { 52655eb942eSChaitanya Kulkarni return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io); 52755eb942eSChaitanya Kulkarni } 52855eb942eSChaitanya Kulkarni 52955eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_store(struct config_item *item, 53055eb942eSChaitanya Kulkarni const char *page, size_t count) 53155eb942eSChaitanya Kulkarni { 53255eb942eSChaitanya Kulkarni struct nvmet_ns *ns = to_nvmet_ns(item); 53355eb942eSChaitanya Kulkarni bool val; 53455eb942eSChaitanya Kulkarni 53555eb942eSChaitanya Kulkarni if (strtobool(page, &val)) 53655eb942eSChaitanya Kulkarni return -EINVAL; 53755eb942eSChaitanya Kulkarni 53855eb942eSChaitanya Kulkarni mutex_lock(&ns->subsys->lock); 53955eb942eSChaitanya Kulkarni if (ns->enabled) { 54055eb942eSChaitanya Kulkarni pr_err("disable ns before setting buffered_io value.\n"); 54155eb942eSChaitanya Kulkarni mutex_unlock(&ns->subsys->lock); 54255eb942eSChaitanya Kulkarni return -EINVAL; 54355eb942eSChaitanya Kulkarni } 54455eb942eSChaitanya Kulkarni 54555eb942eSChaitanya Kulkarni ns->buffered_io = val; 54655eb942eSChaitanya Kulkarni mutex_unlock(&ns->subsys->lock); 54755eb942eSChaitanya Kulkarni return count; 54855eb942eSChaitanya Kulkarni } 54955eb942eSChaitanya Kulkarni 55055eb942eSChaitanya Kulkarni CONFIGFS_ATTR(nvmet_ns_, buffered_io); 55155eb942eSChaitanya Kulkarni 552a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_ns_attrs[] = { 553a07b4970SChristoph Hellwig &nvmet_ns_attr_device_path, 554a07b4970SChristoph Hellwig &nvmet_ns_attr_device_nguid, 555430c7befSJohannes Thumshirn &nvmet_ns_attr_device_uuid, 55662ac0d32SChristoph Hellwig &nvmet_ns_attr_ana_grpid, 557a07b4970SChristoph Hellwig &nvmet_ns_attr_enable, 55855eb942eSChaitanya Kulkarni &nvmet_ns_attr_buffered_io, 559c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA 560c6925093SLogan Gunthorpe &nvmet_ns_attr_p2pmem, 561c6925093SLogan Gunthorpe #endif 562a07b4970SChristoph Hellwig NULL, 563a07b4970SChristoph Hellwig }; 564a07b4970SChristoph Hellwig 565a07b4970SChristoph Hellwig static void nvmet_ns_release(struct config_item *item) 566a07b4970SChristoph Hellwig { 567a07b4970SChristoph Hellwig struct nvmet_ns *ns = to_nvmet_ns(item); 568a07b4970SChristoph Hellwig 569a07b4970SChristoph Hellwig nvmet_ns_free(ns); 570a07b4970SChristoph Hellwig } 571a07b4970SChristoph Hellwig 572a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_ns_item_ops = { 573a07b4970SChristoph Hellwig .release = nvmet_ns_release, 574a07b4970SChristoph Hellwig }; 575a07b4970SChristoph Hellwig 57666603a31SBhumika Goyal static const struct config_item_type nvmet_ns_type = { 577a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_ns_item_ops, 578a07b4970SChristoph Hellwig .ct_attrs = nvmet_ns_attrs, 579a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 580a07b4970SChristoph Hellwig }; 581a07b4970SChristoph Hellwig 582a07b4970SChristoph Hellwig static struct config_group *nvmet_ns_make(struct config_group *group, 583a07b4970SChristoph Hellwig const char *name) 584a07b4970SChristoph Hellwig { 585a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item); 586a07b4970SChristoph Hellwig struct nvmet_ns *ns; 587a07b4970SChristoph Hellwig int ret; 588a07b4970SChristoph Hellwig u32 nsid; 589a07b4970SChristoph Hellwig 590a07b4970SChristoph Hellwig ret = kstrtou32(name, 0, &nsid); 591a07b4970SChristoph Hellwig if (ret) 592a07b4970SChristoph Hellwig goto out; 593a07b4970SChristoph Hellwig 594a07b4970SChristoph Hellwig ret = -EINVAL; 5951645d503SChristoph Hellwig if (nsid == 0 || nsid == NVME_NSID_ALL) 596a07b4970SChristoph Hellwig goto out; 597a07b4970SChristoph Hellwig 598a07b4970SChristoph Hellwig ret = -ENOMEM; 599a07b4970SChristoph Hellwig ns = nvmet_ns_alloc(subsys, nsid); 600a07b4970SChristoph Hellwig if (!ns) 601a07b4970SChristoph Hellwig goto out; 602a07b4970SChristoph Hellwig config_group_init_type_name(&ns->group, name, &nvmet_ns_type); 603a07b4970SChristoph Hellwig 604a07b4970SChristoph Hellwig pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn); 605a07b4970SChristoph Hellwig 606a07b4970SChristoph Hellwig return &ns->group; 607a07b4970SChristoph Hellwig out: 608a07b4970SChristoph Hellwig return ERR_PTR(ret); 609a07b4970SChristoph Hellwig } 610a07b4970SChristoph Hellwig 611a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_namespaces_group_ops = { 612a07b4970SChristoph Hellwig .make_group = nvmet_ns_make, 613a07b4970SChristoph Hellwig }; 614a07b4970SChristoph Hellwig 61566603a31SBhumika Goyal static const struct config_item_type nvmet_namespaces_type = { 616a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_namespaces_group_ops, 617a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 618a07b4970SChristoph Hellwig }; 619a07b4970SChristoph Hellwig 620a07b4970SChristoph Hellwig static int nvmet_port_subsys_allow_link(struct config_item *parent, 621a07b4970SChristoph Hellwig struct config_item *target) 622a07b4970SChristoph Hellwig { 623a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(parent->ci_parent); 624a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 625a07b4970SChristoph Hellwig struct nvmet_subsys_link *link, *p; 626a07b4970SChristoph Hellwig int ret; 627a07b4970SChristoph Hellwig 628a07b4970SChristoph Hellwig if (target->ci_type != &nvmet_subsys_type) { 629a07b4970SChristoph Hellwig pr_err("can only link subsystems into the subsystems dir.!\n"); 630a07b4970SChristoph Hellwig return -EINVAL; 631a07b4970SChristoph Hellwig } 632a07b4970SChristoph Hellwig subsys = to_subsys(target); 633a07b4970SChristoph Hellwig link = kmalloc(sizeof(*link), GFP_KERNEL); 634a07b4970SChristoph Hellwig if (!link) 635a07b4970SChristoph Hellwig return -ENOMEM; 636a07b4970SChristoph Hellwig link->subsys = subsys; 637a07b4970SChristoph Hellwig 638a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 639a07b4970SChristoph Hellwig ret = -EEXIST; 640a07b4970SChristoph Hellwig list_for_each_entry(p, &port->subsystems, entry) { 641a07b4970SChristoph Hellwig if (p->subsys == subsys) 642a07b4970SChristoph Hellwig goto out_free_link; 643a07b4970SChristoph Hellwig } 644a07b4970SChristoph Hellwig 645a07b4970SChristoph Hellwig if (list_empty(&port->subsystems)) { 646a07b4970SChristoph Hellwig ret = nvmet_enable_port(port); 647a07b4970SChristoph Hellwig if (ret) 648a07b4970SChristoph Hellwig goto out_free_link; 649a07b4970SChristoph Hellwig } 650a07b4970SChristoph Hellwig 651a07b4970SChristoph Hellwig list_add_tail(&link->entry, &port->subsystems); 652b662a078SJay Sternberg nvmet_port_disc_changed(port, subsys); 653b662a078SJay Sternberg 654a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 655a07b4970SChristoph Hellwig return 0; 656a07b4970SChristoph Hellwig 657a07b4970SChristoph Hellwig out_free_link: 658a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 659a07b4970SChristoph Hellwig kfree(link); 660a07b4970SChristoph Hellwig return ret; 661a07b4970SChristoph Hellwig } 662a07b4970SChristoph Hellwig 663e16769d4SAndrzej Pietrasiewicz static void nvmet_port_subsys_drop_link(struct config_item *parent, 664a07b4970SChristoph Hellwig struct config_item *target) 665a07b4970SChristoph Hellwig { 666a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(parent->ci_parent); 667a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(target); 668a07b4970SChristoph Hellwig struct nvmet_subsys_link *p; 669a07b4970SChristoph Hellwig 670a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 671a07b4970SChristoph Hellwig list_for_each_entry(p, &port->subsystems, entry) { 672a07b4970SChristoph Hellwig if (p->subsys == subsys) 673a07b4970SChristoph Hellwig goto found; 674a07b4970SChristoph Hellwig } 675a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 676e16769d4SAndrzej Pietrasiewicz return; 677a07b4970SChristoph Hellwig 678a07b4970SChristoph Hellwig found: 679a07b4970SChristoph Hellwig list_del(&p->entry); 680b662a078SJay Sternberg nvmet_port_disc_changed(port, subsys); 681b662a078SJay Sternberg 682a07b4970SChristoph Hellwig if (list_empty(&port->subsystems)) 683a07b4970SChristoph Hellwig nvmet_disable_port(port); 684a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 685a07b4970SChristoph Hellwig kfree(p); 686a07b4970SChristoph Hellwig } 687a07b4970SChristoph Hellwig 688a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_subsys_item_ops = { 689a07b4970SChristoph Hellwig .allow_link = nvmet_port_subsys_allow_link, 690a07b4970SChristoph Hellwig .drop_link = nvmet_port_subsys_drop_link, 691a07b4970SChristoph Hellwig }; 692a07b4970SChristoph Hellwig 69366603a31SBhumika Goyal static const struct config_item_type nvmet_port_subsys_type = { 694a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_port_subsys_item_ops, 695a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 696a07b4970SChristoph Hellwig }; 697a07b4970SChristoph Hellwig 698a07b4970SChristoph Hellwig static int nvmet_allowed_hosts_allow_link(struct config_item *parent, 699a07b4970SChristoph Hellwig struct config_item *target) 700a07b4970SChristoph Hellwig { 701a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(parent->ci_parent); 702a07b4970SChristoph Hellwig struct nvmet_host *host; 703a07b4970SChristoph Hellwig struct nvmet_host_link *link, *p; 704a07b4970SChristoph Hellwig int ret; 705a07b4970SChristoph Hellwig 706a07b4970SChristoph Hellwig if (target->ci_type != &nvmet_host_type) { 707a07b4970SChristoph Hellwig pr_err("can only link hosts into the allowed_hosts directory!\n"); 708a07b4970SChristoph Hellwig return -EINVAL; 709a07b4970SChristoph Hellwig } 710a07b4970SChristoph Hellwig 711a07b4970SChristoph Hellwig host = to_host(target); 712a07b4970SChristoph Hellwig link = kmalloc(sizeof(*link), GFP_KERNEL); 713a07b4970SChristoph Hellwig if (!link) 714a07b4970SChristoph Hellwig return -ENOMEM; 715a07b4970SChristoph Hellwig link->host = host; 716a07b4970SChristoph Hellwig 717a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 718a07b4970SChristoph Hellwig ret = -EINVAL; 719a07b4970SChristoph Hellwig if (subsys->allow_any_host) { 720a07b4970SChristoph Hellwig pr_err("can't add hosts when allow_any_host is set!\n"); 721a07b4970SChristoph Hellwig goto out_free_link; 722a07b4970SChristoph Hellwig } 723a07b4970SChristoph Hellwig 724a07b4970SChristoph Hellwig ret = -EEXIST; 725a07b4970SChristoph Hellwig list_for_each_entry(p, &subsys->hosts, entry) { 726a07b4970SChristoph Hellwig if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host))) 727a07b4970SChristoph Hellwig goto out_free_link; 728a07b4970SChristoph Hellwig } 729a07b4970SChristoph Hellwig list_add_tail(&link->entry, &subsys->hosts); 730b662a078SJay Sternberg nvmet_subsys_disc_changed(subsys, host); 731b662a078SJay Sternberg 732a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 733a07b4970SChristoph Hellwig return 0; 734a07b4970SChristoph Hellwig out_free_link: 735a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 736a07b4970SChristoph Hellwig kfree(link); 737a07b4970SChristoph Hellwig return ret; 738a07b4970SChristoph Hellwig } 739a07b4970SChristoph Hellwig 740e16769d4SAndrzej Pietrasiewicz static void nvmet_allowed_hosts_drop_link(struct config_item *parent, 741a07b4970SChristoph Hellwig struct config_item *target) 742a07b4970SChristoph Hellwig { 743a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(parent->ci_parent); 744a07b4970SChristoph Hellwig struct nvmet_host *host = to_host(target); 745a07b4970SChristoph Hellwig struct nvmet_host_link *p; 746a07b4970SChristoph Hellwig 747a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 748a07b4970SChristoph Hellwig list_for_each_entry(p, &subsys->hosts, entry) { 749a07b4970SChristoph Hellwig if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host))) 750a07b4970SChristoph Hellwig goto found; 751a07b4970SChristoph Hellwig } 752a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 753e16769d4SAndrzej Pietrasiewicz return; 754a07b4970SChristoph Hellwig 755a07b4970SChristoph Hellwig found: 756a07b4970SChristoph Hellwig list_del(&p->entry); 757b662a078SJay Sternberg nvmet_subsys_disc_changed(subsys, host); 758b662a078SJay Sternberg 759a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 760a07b4970SChristoph Hellwig kfree(p); 761a07b4970SChristoph Hellwig } 762a07b4970SChristoph Hellwig 763a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_allowed_hosts_item_ops = { 764a07b4970SChristoph Hellwig .allow_link = nvmet_allowed_hosts_allow_link, 765a07b4970SChristoph Hellwig .drop_link = nvmet_allowed_hosts_drop_link, 766a07b4970SChristoph Hellwig }; 767a07b4970SChristoph Hellwig 76866603a31SBhumika Goyal static const struct config_item_type nvmet_allowed_hosts_type = { 769a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_allowed_hosts_item_ops, 770a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 771a07b4970SChristoph Hellwig }; 772a07b4970SChristoph Hellwig 773a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item, 774a07b4970SChristoph Hellwig char *page) 775a07b4970SChristoph Hellwig { 776a07b4970SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%d\n", 777a07b4970SChristoph Hellwig to_subsys(item)->allow_any_host); 778a07b4970SChristoph Hellwig } 779a07b4970SChristoph Hellwig 780a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item, 781a07b4970SChristoph Hellwig const char *page, size_t count) 782a07b4970SChristoph Hellwig { 783a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(item); 784a07b4970SChristoph Hellwig bool allow_any_host; 785a07b4970SChristoph Hellwig int ret = 0; 786a07b4970SChristoph Hellwig 787a07b4970SChristoph Hellwig if (strtobool(page, &allow_any_host)) 788a07b4970SChristoph Hellwig return -EINVAL; 789a07b4970SChristoph Hellwig 790a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 791a07b4970SChristoph Hellwig if (allow_any_host && !list_empty(&subsys->hosts)) { 792a07b4970SChristoph Hellwig pr_err("Can't set allow_any_host when explicit hosts are set!\n"); 793a07b4970SChristoph Hellwig ret = -EINVAL; 794a07b4970SChristoph Hellwig goto out_unlock; 795a07b4970SChristoph Hellwig } 796a07b4970SChristoph Hellwig 797b662a078SJay Sternberg if (subsys->allow_any_host != allow_any_host) { 798a07b4970SChristoph Hellwig subsys->allow_any_host = allow_any_host; 799b662a078SJay Sternberg nvmet_subsys_disc_changed(subsys, NULL); 800b662a078SJay Sternberg } 801b662a078SJay Sternberg 802a07b4970SChristoph Hellwig out_unlock: 803a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 804a07b4970SChristoph Hellwig return ret ? ret : count; 805a07b4970SChristoph Hellwig } 806a07b4970SChristoph Hellwig 807a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host); 808a07b4970SChristoph Hellwig 80941528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_show(struct config_item *item, 810c61d788bSJohannes Thumshirn char *page) 811c61d788bSJohannes Thumshirn { 812c61d788bSJohannes Thumshirn struct nvmet_subsys *subsys = to_subsys(item); 813c61d788bSJohannes Thumshirn 814c61d788bSJohannes Thumshirn if (NVME_TERTIARY(subsys->ver)) 815c61d788bSJohannes Thumshirn return snprintf(page, PAGE_SIZE, "%d.%d.%d\n", 816c61d788bSJohannes Thumshirn (int)NVME_MAJOR(subsys->ver), 817c61d788bSJohannes Thumshirn (int)NVME_MINOR(subsys->ver), 818c61d788bSJohannes Thumshirn (int)NVME_TERTIARY(subsys->ver)); 819c61d788bSJohannes Thumshirn else 820c61d788bSJohannes Thumshirn return snprintf(page, PAGE_SIZE, "%d.%d\n", 821c61d788bSJohannes Thumshirn (int)NVME_MAJOR(subsys->ver), 822c61d788bSJohannes Thumshirn (int)NVME_MINOR(subsys->ver)); 823c61d788bSJohannes Thumshirn } 824c61d788bSJohannes Thumshirn 82541528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, 826c61d788bSJohannes Thumshirn const char *page, size_t count) 827c61d788bSJohannes Thumshirn { 828c61d788bSJohannes Thumshirn struct nvmet_subsys *subsys = to_subsys(item); 829c61d788bSJohannes Thumshirn int major, minor, tertiary = 0; 830c61d788bSJohannes Thumshirn int ret; 831c61d788bSJohannes Thumshirn 832c61d788bSJohannes Thumshirn 833c61d788bSJohannes Thumshirn ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary); 834c61d788bSJohannes Thumshirn if (ret != 2 && ret != 3) 835c61d788bSJohannes Thumshirn return -EINVAL; 836c61d788bSJohannes Thumshirn 837c61d788bSJohannes Thumshirn down_write(&nvmet_config_sem); 838c61d788bSJohannes Thumshirn subsys->ver = NVME_VS(major, minor, tertiary); 839c61d788bSJohannes Thumshirn up_write(&nvmet_config_sem); 840c61d788bSJohannes Thumshirn 841c61d788bSJohannes Thumshirn return count; 842c61d788bSJohannes Thumshirn } 84341528f80SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_version); 844c61d788bSJohannes Thumshirn 845fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item, 846fcbc5459SJohannes Thumshirn char *page) 847fcbc5459SJohannes Thumshirn { 848fcbc5459SJohannes Thumshirn struct nvmet_subsys *subsys = to_subsys(item); 849fcbc5459SJohannes Thumshirn 850fcbc5459SJohannes Thumshirn return snprintf(page, PAGE_SIZE, "%llx\n", subsys->serial); 851fcbc5459SJohannes Thumshirn } 852fcbc5459SJohannes Thumshirn 853fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item, 854fcbc5459SJohannes Thumshirn const char *page, size_t count) 855fcbc5459SJohannes Thumshirn { 856fcbc5459SJohannes Thumshirn struct nvmet_subsys *subsys = to_subsys(item); 857fcbc5459SJohannes Thumshirn 858fcbc5459SJohannes Thumshirn down_write(&nvmet_config_sem); 859fcbc5459SJohannes Thumshirn sscanf(page, "%llx\n", &subsys->serial); 860fcbc5459SJohannes Thumshirn up_write(&nvmet_config_sem); 861fcbc5459SJohannes Thumshirn 862fcbc5459SJohannes Thumshirn return count; 863fcbc5459SJohannes Thumshirn } 864fcbc5459SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_serial); 865fcbc5459SJohannes Thumshirn 866a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_subsys_attrs[] = { 867a07b4970SChristoph Hellwig &nvmet_subsys_attr_attr_allow_any_host, 86841528f80SJohannes Thumshirn &nvmet_subsys_attr_attr_version, 869fcbc5459SJohannes Thumshirn &nvmet_subsys_attr_attr_serial, 870a07b4970SChristoph Hellwig NULL, 871a07b4970SChristoph Hellwig }; 872a07b4970SChristoph Hellwig 873a07b4970SChristoph Hellwig /* 874a07b4970SChristoph Hellwig * Subsystem structures & folder operation functions below 875a07b4970SChristoph Hellwig */ 876a07b4970SChristoph Hellwig static void nvmet_subsys_release(struct config_item *item) 877a07b4970SChristoph Hellwig { 878a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(item); 879a07b4970SChristoph Hellwig 880344770b0SSagi Grimberg nvmet_subsys_del_ctrls(subsys); 881a07b4970SChristoph Hellwig nvmet_subsys_put(subsys); 882a07b4970SChristoph Hellwig } 883a07b4970SChristoph Hellwig 884a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_subsys_item_ops = { 885a07b4970SChristoph Hellwig .release = nvmet_subsys_release, 886a07b4970SChristoph Hellwig }; 887a07b4970SChristoph Hellwig 88866603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type = { 889a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_subsys_item_ops, 890a07b4970SChristoph Hellwig .ct_attrs = nvmet_subsys_attrs, 891a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 892a07b4970SChristoph Hellwig }; 893a07b4970SChristoph Hellwig 894a07b4970SChristoph Hellwig static struct config_group *nvmet_subsys_make(struct config_group *group, 895a07b4970SChristoph Hellwig const char *name) 896a07b4970SChristoph Hellwig { 897a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 898a07b4970SChristoph Hellwig 899a07b4970SChristoph Hellwig if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) { 900a07b4970SChristoph Hellwig pr_err("can't create discovery subsystem through configfs\n"); 901a07b4970SChristoph Hellwig return ERR_PTR(-EINVAL); 902a07b4970SChristoph Hellwig } 903a07b4970SChristoph Hellwig 904a07b4970SChristoph Hellwig subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME); 905a07b4970SChristoph Hellwig if (!subsys) 906a07b4970SChristoph Hellwig return ERR_PTR(-ENOMEM); 907a07b4970SChristoph Hellwig 908a07b4970SChristoph Hellwig config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type); 909a07b4970SChristoph Hellwig 910a07b4970SChristoph Hellwig config_group_init_type_name(&subsys->namespaces_group, 911a07b4970SChristoph Hellwig "namespaces", &nvmet_namespaces_type); 912a07b4970SChristoph Hellwig configfs_add_default_group(&subsys->namespaces_group, &subsys->group); 913a07b4970SChristoph Hellwig 914a07b4970SChristoph Hellwig config_group_init_type_name(&subsys->allowed_hosts_group, 915a07b4970SChristoph Hellwig "allowed_hosts", &nvmet_allowed_hosts_type); 916a07b4970SChristoph Hellwig configfs_add_default_group(&subsys->allowed_hosts_group, 917a07b4970SChristoph Hellwig &subsys->group); 918a07b4970SChristoph Hellwig 919a07b4970SChristoph Hellwig return &subsys->group; 920a07b4970SChristoph Hellwig } 921a07b4970SChristoph Hellwig 922a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_subsystems_group_ops = { 923a07b4970SChristoph Hellwig .make_group = nvmet_subsys_make, 924a07b4970SChristoph Hellwig }; 925a07b4970SChristoph Hellwig 92666603a31SBhumika Goyal static const struct config_item_type nvmet_subsystems_type = { 927a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_subsystems_group_ops, 928a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 929a07b4970SChristoph Hellwig }; 930a07b4970SChristoph Hellwig 931a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_show(struct config_item *item, 932a07b4970SChristoph Hellwig char *page) 933a07b4970SChristoph Hellwig { 934a07b4970SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled); 935a07b4970SChristoph Hellwig } 936a07b4970SChristoph Hellwig 937a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_store(struct config_item *item, 938a07b4970SChristoph Hellwig const char *page, size_t count) 939a07b4970SChristoph Hellwig { 940a07b4970SChristoph Hellwig struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent); 941a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 942a07b4970SChristoph Hellwig bool enable; 943a07b4970SChristoph Hellwig 944a07b4970SChristoph Hellwig if (strtobool(page, &enable)) 945a07b4970SChristoph Hellwig goto inval; 946a07b4970SChristoph Hellwig 947a07b4970SChristoph Hellwig if (enable) 948a07b4970SChristoph Hellwig nvmet_referral_enable(parent, port); 949a07b4970SChristoph Hellwig else 950b662a078SJay Sternberg nvmet_referral_disable(parent, port); 951a07b4970SChristoph Hellwig 952a07b4970SChristoph Hellwig return count; 953a07b4970SChristoph Hellwig inval: 954a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for enable\n", page); 955a07b4970SChristoph Hellwig return -EINVAL; 956a07b4970SChristoph Hellwig } 957a07b4970SChristoph Hellwig 958a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_referral_, enable); 959a07b4970SChristoph Hellwig 960a07b4970SChristoph Hellwig /* 961a07b4970SChristoph Hellwig * Discovery Service subsystem definitions 962a07b4970SChristoph Hellwig */ 963a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_referral_attrs[] = { 964a07b4970SChristoph Hellwig &nvmet_attr_addr_adrfam, 965a07b4970SChristoph Hellwig &nvmet_attr_addr_portid, 966a07b4970SChristoph Hellwig &nvmet_attr_addr_treq, 967a07b4970SChristoph Hellwig &nvmet_attr_addr_traddr, 968a07b4970SChristoph Hellwig &nvmet_attr_addr_trsvcid, 969a07b4970SChristoph Hellwig &nvmet_attr_addr_trtype, 970a07b4970SChristoph Hellwig &nvmet_referral_attr_enable, 971a07b4970SChristoph Hellwig NULL, 972a07b4970SChristoph Hellwig }; 973a07b4970SChristoph Hellwig 974a07b4970SChristoph Hellwig static void nvmet_referral_release(struct config_item *item) 975a07b4970SChristoph Hellwig { 976b662a078SJay Sternberg struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent); 977a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 978a07b4970SChristoph Hellwig 979b662a078SJay Sternberg nvmet_referral_disable(parent, port); 980a07b4970SChristoph Hellwig kfree(port); 981a07b4970SChristoph Hellwig } 982a07b4970SChristoph Hellwig 983a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_referral_item_ops = { 984a07b4970SChristoph Hellwig .release = nvmet_referral_release, 985a07b4970SChristoph Hellwig }; 986a07b4970SChristoph Hellwig 98766603a31SBhumika Goyal static const struct config_item_type nvmet_referral_type = { 988a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 989a07b4970SChristoph Hellwig .ct_attrs = nvmet_referral_attrs, 990a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_referral_item_ops, 991a07b4970SChristoph Hellwig }; 992a07b4970SChristoph Hellwig 993a07b4970SChristoph Hellwig static struct config_group *nvmet_referral_make( 994a07b4970SChristoph Hellwig struct config_group *group, const char *name) 995a07b4970SChristoph Hellwig { 996a07b4970SChristoph Hellwig struct nvmet_port *port; 997a07b4970SChristoph Hellwig 998a07b4970SChristoph Hellwig port = kzalloc(sizeof(*port), GFP_KERNEL); 999a07b4970SChristoph Hellwig if (!port) 1000f98d9ca1SDan Carpenter return ERR_PTR(-ENOMEM); 1001a07b4970SChristoph Hellwig 1002a07b4970SChristoph Hellwig INIT_LIST_HEAD(&port->entry); 1003a07b4970SChristoph Hellwig config_group_init_type_name(&port->group, name, &nvmet_referral_type); 1004a07b4970SChristoph Hellwig 1005a07b4970SChristoph Hellwig return &port->group; 1006a07b4970SChristoph Hellwig } 1007a07b4970SChristoph Hellwig 1008a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_referral_group_ops = { 1009a07b4970SChristoph Hellwig .make_group = nvmet_referral_make, 1010a07b4970SChristoph Hellwig }; 1011a07b4970SChristoph Hellwig 101266603a31SBhumika Goyal static const struct config_item_type nvmet_referrals_type = { 1013a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 1014a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_referral_group_ops, 1015a07b4970SChristoph Hellwig }; 1016a07b4970SChristoph Hellwig 101762ac0d32SChristoph Hellwig static struct { 101862ac0d32SChristoph Hellwig enum nvme_ana_state state; 101962ac0d32SChristoph Hellwig const char *name; 102062ac0d32SChristoph Hellwig } nvmet_ana_state_names[] = { 102162ac0d32SChristoph Hellwig { NVME_ANA_OPTIMIZED, "optimized" }, 102262ac0d32SChristoph Hellwig { NVME_ANA_NONOPTIMIZED, "non-optimized" }, 102362ac0d32SChristoph Hellwig { NVME_ANA_INACCESSIBLE, "inaccessible" }, 102462ac0d32SChristoph Hellwig { NVME_ANA_PERSISTENT_LOSS, "persistent-loss" }, 102562ac0d32SChristoph Hellwig { NVME_ANA_CHANGE, "change" }, 102662ac0d32SChristoph Hellwig }; 102762ac0d32SChristoph Hellwig 102862ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item, 102962ac0d32SChristoph Hellwig char *page) 103062ac0d32SChristoph Hellwig { 103162ac0d32SChristoph Hellwig struct nvmet_ana_group *grp = to_ana_group(item); 103262ac0d32SChristoph Hellwig enum nvme_ana_state state = grp->port->ana_state[grp->grpid]; 103362ac0d32SChristoph Hellwig int i; 103462ac0d32SChristoph Hellwig 103562ac0d32SChristoph Hellwig for (i = 0; i < ARRAY_SIZE(nvmet_ana_state_names); i++) { 103662ac0d32SChristoph Hellwig if (state != nvmet_ana_state_names[i].state) 103762ac0d32SChristoph Hellwig continue; 103862ac0d32SChristoph Hellwig return sprintf(page, "%s\n", nvmet_ana_state_names[i].name); 103962ac0d32SChristoph Hellwig } 104062ac0d32SChristoph Hellwig 104162ac0d32SChristoph Hellwig return sprintf(page, "\n"); 104262ac0d32SChristoph Hellwig } 104362ac0d32SChristoph Hellwig 104462ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item, 104562ac0d32SChristoph Hellwig const char *page, size_t count) 104662ac0d32SChristoph Hellwig { 104762ac0d32SChristoph Hellwig struct nvmet_ana_group *grp = to_ana_group(item); 104862ac0d32SChristoph Hellwig int i; 104962ac0d32SChristoph Hellwig 105062ac0d32SChristoph Hellwig for (i = 0; i < ARRAY_SIZE(nvmet_ana_state_names); i++) { 105162ac0d32SChristoph Hellwig if (sysfs_streq(page, nvmet_ana_state_names[i].name)) 105262ac0d32SChristoph Hellwig goto found; 105362ac0d32SChristoph Hellwig } 105462ac0d32SChristoph Hellwig 105562ac0d32SChristoph Hellwig pr_err("Invalid value '%s' for ana_state\n", page); 105662ac0d32SChristoph Hellwig return -EINVAL; 105762ac0d32SChristoph Hellwig 105862ac0d32SChristoph Hellwig found: 105962ac0d32SChristoph Hellwig down_write(&nvmet_ana_sem); 106062ac0d32SChristoph Hellwig grp->port->ana_state[grp->grpid] = nvmet_ana_state_names[i].state; 106162ac0d32SChristoph Hellwig nvmet_ana_chgcnt++; 106262ac0d32SChristoph Hellwig up_write(&nvmet_ana_sem); 106362ac0d32SChristoph Hellwig 106462ac0d32SChristoph Hellwig nvmet_port_send_ana_event(grp->port); 106562ac0d32SChristoph Hellwig return count; 106662ac0d32SChristoph Hellwig } 106762ac0d32SChristoph Hellwig 106862ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ana_group_, ana_state); 106962ac0d32SChristoph Hellwig 107062ac0d32SChristoph Hellwig static struct configfs_attribute *nvmet_ana_group_attrs[] = { 107162ac0d32SChristoph Hellwig &nvmet_ana_group_attr_ana_state, 107262ac0d32SChristoph Hellwig NULL, 107362ac0d32SChristoph Hellwig }; 107462ac0d32SChristoph Hellwig 107562ac0d32SChristoph Hellwig static void nvmet_ana_group_release(struct config_item *item) 107662ac0d32SChristoph Hellwig { 107762ac0d32SChristoph Hellwig struct nvmet_ana_group *grp = to_ana_group(item); 107862ac0d32SChristoph Hellwig 107962ac0d32SChristoph Hellwig if (grp == &grp->port->ana_default_group) 108062ac0d32SChristoph Hellwig return; 108162ac0d32SChristoph Hellwig 108262ac0d32SChristoph Hellwig down_write(&nvmet_ana_sem); 108362ac0d32SChristoph Hellwig grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE; 108462ac0d32SChristoph Hellwig nvmet_ana_group_enabled[grp->grpid]--; 108562ac0d32SChristoph Hellwig up_write(&nvmet_ana_sem); 108662ac0d32SChristoph Hellwig 108762ac0d32SChristoph Hellwig nvmet_port_send_ana_event(grp->port); 108862ac0d32SChristoph Hellwig kfree(grp); 108962ac0d32SChristoph Hellwig } 109062ac0d32SChristoph Hellwig 109162ac0d32SChristoph Hellwig static struct configfs_item_operations nvmet_ana_group_item_ops = { 109262ac0d32SChristoph Hellwig .release = nvmet_ana_group_release, 109362ac0d32SChristoph Hellwig }; 109462ac0d32SChristoph Hellwig 109562ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_group_type = { 109662ac0d32SChristoph Hellwig .ct_item_ops = &nvmet_ana_group_item_ops, 109762ac0d32SChristoph Hellwig .ct_attrs = nvmet_ana_group_attrs, 109862ac0d32SChristoph Hellwig .ct_owner = THIS_MODULE, 109962ac0d32SChristoph Hellwig }; 110062ac0d32SChristoph Hellwig 110162ac0d32SChristoph Hellwig static struct config_group *nvmet_ana_groups_make_group( 110262ac0d32SChristoph Hellwig struct config_group *group, const char *name) 110362ac0d32SChristoph Hellwig { 110462ac0d32SChristoph Hellwig struct nvmet_port *port = ana_groups_to_port(&group->cg_item); 110562ac0d32SChristoph Hellwig struct nvmet_ana_group *grp; 110662ac0d32SChristoph Hellwig u32 grpid; 110762ac0d32SChristoph Hellwig int ret; 110862ac0d32SChristoph Hellwig 110962ac0d32SChristoph Hellwig ret = kstrtou32(name, 0, &grpid); 111062ac0d32SChristoph Hellwig if (ret) 111162ac0d32SChristoph Hellwig goto out; 111262ac0d32SChristoph Hellwig 111362ac0d32SChristoph Hellwig ret = -EINVAL; 111462ac0d32SChristoph Hellwig if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS) 111562ac0d32SChristoph Hellwig goto out; 111662ac0d32SChristoph Hellwig 111762ac0d32SChristoph Hellwig ret = -ENOMEM; 111862ac0d32SChristoph Hellwig grp = kzalloc(sizeof(*grp), GFP_KERNEL); 111962ac0d32SChristoph Hellwig if (!grp) 112062ac0d32SChristoph Hellwig goto out; 112162ac0d32SChristoph Hellwig grp->port = port; 112262ac0d32SChristoph Hellwig grp->grpid = grpid; 112362ac0d32SChristoph Hellwig 112462ac0d32SChristoph Hellwig down_write(&nvmet_ana_sem); 112562ac0d32SChristoph Hellwig nvmet_ana_group_enabled[grpid]++; 112662ac0d32SChristoph Hellwig up_write(&nvmet_ana_sem); 112762ac0d32SChristoph Hellwig 112862ac0d32SChristoph Hellwig nvmet_port_send_ana_event(grp->port); 112962ac0d32SChristoph Hellwig 113062ac0d32SChristoph Hellwig config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type); 113162ac0d32SChristoph Hellwig return &grp->group; 113262ac0d32SChristoph Hellwig out: 113362ac0d32SChristoph Hellwig return ERR_PTR(ret); 113462ac0d32SChristoph Hellwig } 113562ac0d32SChristoph Hellwig 113662ac0d32SChristoph Hellwig static struct configfs_group_operations nvmet_ana_groups_group_ops = { 113762ac0d32SChristoph Hellwig .make_group = nvmet_ana_groups_make_group, 113862ac0d32SChristoph Hellwig }; 113962ac0d32SChristoph Hellwig 114062ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_groups_type = { 114162ac0d32SChristoph Hellwig .ct_group_ops = &nvmet_ana_groups_group_ops, 114262ac0d32SChristoph Hellwig .ct_owner = THIS_MODULE, 114362ac0d32SChristoph Hellwig }; 114462ac0d32SChristoph Hellwig 1145a07b4970SChristoph Hellwig /* 1146a07b4970SChristoph Hellwig * Ports definitions. 1147a07b4970SChristoph Hellwig */ 1148a07b4970SChristoph Hellwig static void nvmet_port_release(struct config_item *item) 1149a07b4970SChristoph Hellwig { 1150a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 1151a07b4970SChristoph Hellwig 1152b662a078SJay Sternberg list_del(&port->global_entry); 1153b662a078SJay Sternberg 115472efd25dSChristoph Hellwig kfree(port->ana_state); 1155a07b4970SChristoph Hellwig kfree(port); 1156a07b4970SChristoph Hellwig } 1157a07b4970SChristoph Hellwig 1158a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_port_attrs[] = { 1159a07b4970SChristoph Hellwig &nvmet_attr_addr_adrfam, 1160a07b4970SChristoph Hellwig &nvmet_attr_addr_treq, 1161a07b4970SChristoph Hellwig &nvmet_attr_addr_traddr, 1162a07b4970SChristoph Hellwig &nvmet_attr_addr_trsvcid, 1163a07b4970SChristoph Hellwig &nvmet_attr_addr_trtype, 11640d5ee2b2SSteve Wise &nvmet_attr_param_inline_data_size, 1165a07b4970SChristoph Hellwig NULL, 1166a07b4970SChristoph Hellwig }; 1167a07b4970SChristoph Hellwig 1168a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_item_ops = { 1169a07b4970SChristoph Hellwig .release = nvmet_port_release, 1170a07b4970SChristoph Hellwig }; 1171a07b4970SChristoph Hellwig 117266603a31SBhumika Goyal static const struct config_item_type nvmet_port_type = { 1173a07b4970SChristoph Hellwig .ct_attrs = nvmet_port_attrs, 1174a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_port_item_ops, 1175a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 1176a07b4970SChristoph Hellwig }; 1177a07b4970SChristoph Hellwig 1178a07b4970SChristoph Hellwig static struct config_group *nvmet_ports_make(struct config_group *group, 1179a07b4970SChristoph Hellwig const char *name) 1180a07b4970SChristoph Hellwig { 1181a07b4970SChristoph Hellwig struct nvmet_port *port; 1182a07b4970SChristoph Hellwig u16 portid; 118362ac0d32SChristoph Hellwig u32 i; 1184a07b4970SChristoph Hellwig 1185a07b4970SChristoph Hellwig if (kstrtou16(name, 0, &portid)) 1186a07b4970SChristoph Hellwig return ERR_PTR(-EINVAL); 1187a07b4970SChristoph Hellwig 1188a07b4970SChristoph Hellwig port = kzalloc(sizeof(*port), GFP_KERNEL); 1189a07b4970SChristoph Hellwig if (!port) 1190f98d9ca1SDan Carpenter return ERR_PTR(-ENOMEM); 1191a07b4970SChristoph Hellwig 119272efd25dSChristoph Hellwig port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1, 119372efd25dSChristoph Hellwig sizeof(*port->ana_state), GFP_KERNEL); 119472efd25dSChristoph Hellwig if (!port->ana_state) { 119572efd25dSChristoph Hellwig kfree(port); 119672efd25dSChristoph Hellwig return ERR_PTR(-ENOMEM); 119772efd25dSChristoph Hellwig } 119872efd25dSChristoph Hellwig 119962ac0d32SChristoph Hellwig for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) { 120062ac0d32SChristoph Hellwig if (i == NVMET_DEFAULT_ANA_GRPID) 120162ac0d32SChristoph Hellwig port->ana_state[1] = NVME_ANA_OPTIMIZED; 120262ac0d32SChristoph Hellwig else 120362ac0d32SChristoph Hellwig port->ana_state[i] = NVME_ANA_INACCESSIBLE; 120462ac0d32SChristoph Hellwig } 120572efd25dSChristoph Hellwig 1206b662a078SJay Sternberg list_add(&port->global_entry, &nvmet_ports_list); 1207b662a078SJay Sternberg 1208a07b4970SChristoph Hellwig INIT_LIST_HEAD(&port->entry); 1209a07b4970SChristoph Hellwig INIT_LIST_HEAD(&port->subsystems); 1210a07b4970SChristoph Hellwig INIT_LIST_HEAD(&port->referrals); 12110d5ee2b2SSteve Wise port->inline_data_size = -1; /* < 0 == let the transport choose */ 1212a07b4970SChristoph Hellwig 1213a07b4970SChristoph Hellwig port->disc_addr.portid = cpu_to_le16(portid); 1214a07b4970SChristoph Hellwig config_group_init_type_name(&port->group, name, &nvmet_port_type); 1215a07b4970SChristoph Hellwig 1216a07b4970SChristoph Hellwig config_group_init_type_name(&port->subsys_group, 1217a07b4970SChristoph Hellwig "subsystems", &nvmet_port_subsys_type); 1218a07b4970SChristoph Hellwig configfs_add_default_group(&port->subsys_group, &port->group); 1219a07b4970SChristoph Hellwig 1220a07b4970SChristoph Hellwig config_group_init_type_name(&port->referrals_group, 1221a07b4970SChristoph Hellwig "referrals", &nvmet_referrals_type); 1222a07b4970SChristoph Hellwig configfs_add_default_group(&port->referrals_group, &port->group); 1223a07b4970SChristoph Hellwig 122462ac0d32SChristoph Hellwig config_group_init_type_name(&port->ana_groups_group, 122562ac0d32SChristoph Hellwig "ana_groups", &nvmet_ana_groups_type); 122662ac0d32SChristoph Hellwig configfs_add_default_group(&port->ana_groups_group, &port->group); 122762ac0d32SChristoph Hellwig 122862ac0d32SChristoph Hellwig port->ana_default_group.port = port; 122962ac0d32SChristoph Hellwig port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID; 123062ac0d32SChristoph Hellwig config_group_init_type_name(&port->ana_default_group.group, 123162ac0d32SChristoph Hellwig __stringify(NVMET_DEFAULT_ANA_GRPID), 123262ac0d32SChristoph Hellwig &nvmet_ana_group_type); 123362ac0d32SChristoph Hellwig configfs_add_default_group(&port->ana_default_group.group, 123462ac0d32SChristoph Hellwig &port->ana_groups_group); 123562ac0d32SChristoph Hellwig 1236a07b4970SChristoph Hellwig return &port->group; 1237a07b4970SChristoph Hellwig } 1238a07b4970SChristoph Hellwig 1239a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_ports_group_ops = { 1240a07b4970SChristoph Hellwig .make_group = nvmet_ports_make, 1241a07b4970SChristoph Hellwig }; 1242a07b4970SChristoph Hellwig 124366603a31SBhumika Goyal static const struct config_item_type nvmet_ports_type = { 1244a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_ports_group_ops, 1245a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 1246a07b4970SChristoph Hellwig }; 1247a07b4970SChristoph Hellwig 1248a07b4970SChristoph Hellwig static struct config_group nvmet_subsystems_group; 1249a07b4970SChristoph Hellwig static struct config_group nvmet_ports_group; 1250a07b4970SChristoph Hellwig 1251a07b4970SChristoph Hellwig static void nvmet_host_release(struct config_item *item) 1252a07b4970SChristoph Hellwig { 1253a07b4970SChristoph Hellwig struct nvmet_host *host = to_host(item); 1254a07b4970SChristoph Hellwig 1255a07b4970SChristoph Hellwig kfree(host); 1256a07b4970SChristoph Hellwig } 1257a07b4970SChristoph Hellwig 1258a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_host_item_ops = { 1259a07b4970SChristoph Hellwig .release = nvmet_host_release, 1260a07b4970SChristoph Hellwig }; 1261a07b4970SChristoph Hellwig 126266603a31SBhumika Goyal static const struct config_item_type nvmet_host_type = { 1263a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_host_item_ops, 1264a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 1265a07b4970SChristoph Hellwig }; 1266a07b4970SChristoph Hellwig 1267a07b4970SChristoph Hellwig static struct config_group *nvmet_hosts_make_group(struct config_group *group, 1268a07b4970SChristoph Hellwig const char *name) 1269a07b4970SChristoph Hellwig { 1270a07b4970SChristoph Hellwig struct nvmet_host *host; 1271a07b4970SChristoph Hellwig 1272a07b4970SChristoph Hellwig host = kzalloc(sizeof(*host), GFP_KERNEL); 1273a07b4970SChristoph Hellwig if (!host) 1274a07b4970SChristoph Hellwig return ERR_PTR(-ENOMEM); 1275a07b4970SChristoph Hellwig 1276a07b4970SChristoph Hellwig config_group_init_type_name(&host->group, name, &nvmet_host_type); 1277a07b4970SChristoph Hellwig 1278a07b4970SChristoph Hellwig return &host->group; 1279a07b4970SChristoph Hellwig } 1280a07b4970SChristoph Hellwig 1281a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_hosts_group_ops = { 1282a07b4970SChristoph Hellwig .make_group = nvmet_hosts_make_group, 1283a07b4970SChristoph Hellwig }; 1284a07b4970SChristoph Hellwig 128566603a31SBhumika Goyal static const struct config_item_type nvmet_hosts_type = { 1286a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_hosts_group_ops, 1287a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 1288a07b4970SChristoph Hellwig }; 1289a07b4970SChristoph Hellwig 1290a07b4970SChristoph Hellwig static struct config_group nvmet_hosts_group; 1291a07b4970SChristoph Hellwig 129266603a31SBhumika Goyal static const struct config_item_type nvmet_root_type = { 1293a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 1294a07b4970SChristoph Hellwig }; 1295a07b4970SChristoph Hellwig 1296a07b4970SChristoph Hellwig static struct configfs_subsystem nvmet_configfs_subsystem = { 1297a07b4970SChristoph Hellwig .su_group = { 1298a07b4970SChristoph Hellwig .cg_item = { 1299a07b4970SChristoph Hellwig .ci_namebuf = "nvmet", 1300a07b4970SChristoph Hellwig .ci_type = &nvmet_root_type, 1301a07b4970SChristoph Hellwig }, 1302a07b4970SChristoph Hellwig }, 1303a07b4970SChristoph Hellwig }; 1304a07b4970SChristoph Hellwig 1305a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void) 1306a07b4970SChristoph Hellwig { 1307a07b4970SChristoph Hellwig int ret; 1308a07b4970SChristoph Hellwig 1309a07b4970SChristoph Hellwig config_group_init(&nvmet_configfs_subsystem.su_group); 1310a07b4970SChristoph Hellwig mutex_init(&nvmet_configfs_subsystem.su_mutex); 1311a07b4970SChristoph Hellwig 1312a07b4970SChristoph Hellwig config_group_init_type_name(&nvmet_subsystems_group, 1313a07b4970SChristoph Hellwig "subsystems", &nvmet_subsystems_type); 1314a07b4970SChristoph Hellwig configfs_add_default_group(&nvmet_subsystems_group, 1315a07b4970SChristoph Hellwig &nvmet_configfs_subsystem.su_group); 1316a07b4970SChristoph Hellwig 1317a07b4970SChristoph Hellwig config_group_init_type_name(&nvmet_ports_group, 1318a07b4970SChristoph Hellwig "ports", &nvmet_ports_type); 1319a07b4970SChristoph Hellwig configfs_add_default_group(&nvmet_ports_group, 1320a07b4970SChristoph Hellwig &nvmet_configfs_subsystem.su_group); 1321a07b4970SChristoph Hellwig 1322a07b4970SChristoph Hellwig config_group_init_type_name(&nvmet_hosts_group, 1323a07b4970SChristoph Hellwig "hosts", &nvmet_hosts_type); 1324a07b4970SChristoph Hellwig configfs_add_default_group(&nvmet_hosts_group, 1325a07b4970SChristoph Hellwig &nvmet_configfs_subsystem.su_group); 1326a07b4970SChristoph Hellwig 1327a07b4970SChristoph Hellwig ret = configfs_register_subsystem(&nvmet_configfs_subsystem); 1328a07b4970SChristoph Hellwig if (ret) { 1329a07b4970SChristoph Hellwig pr_err("configfs_register_subsystem: %d\n", ret); 1330a07b4970SChristoph Hellwig return ret; 1331a07b4970SChristoph Hellwig } 1332a07b4970SChristoph Hellwig 1333a07b4970SChristoph Hellwig return 0; 1334a07b4970SChristoph Hellwig } 1335a07b4970SChristoph Hellwig 1336a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void) 1337a07b4970SChristoph Hellwig { 1338a07b4970SChristoph Hellwig configfs_unregister_subsystem(&nvmet_configfs_subsystem); 1339a07b4970SChristoph Hellwig } 1340