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