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> 20a07b4970SChristoph Hellwig 21a07b4970SChristoph Hellwig #include "nvmet.h" 22a07b4970SChristoph Hellwig 2366603a31SBhumika Goyal static const struct config_item_type nvmet_host_type; 2466603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type; 25a07b4970SChristoph Hellwig 26a5d18612SChristoph Hellwig static const struct nvmet_transport_name { 27a5d18612SChristoph Hellwig u8 type; 28a5d18612SChristoph Hellwig const char *name; 29a5d18612SChristoph Hellwig } nvmet_transport_names[] = { 30a5d18612SChristoph Hellwig { NVMF_TRTYPE_RDMA, "rdma" }, 31a5d18612SChristoph Hellwig { NVMF_TRTYPE_FC, "fc" }, 32a5d18612SChristoph Hellwig { NVMF_TRTYPE_LOOP, "loop" }, 33a5d18612SChristoph Hellwig }; 34a5d18612SChristoph Hellwig 35a07b4970SChristoph Hellwig /* 36a07b4970SChristoph Hellwig * nvmet_port Generic ConfigFS definitions. 37a07b4970SChristoph Hellwig * Used in any place in the ConfigFS tree that refers to an address. 38a07b4970SChristoph Hellwig */ 39a07b4970SChristoph Hellwig static ssize_t nvmet_addr_adrfam_show(struct config_item *item, 40a07b4970SChristoph Hellwig char *page) 41a07b4970SChristoph Hellwig { 42a07b4970SChristoph Hellwig switch (to_nvmet_port(item)->disc_addr.adrfam) { 43a07b4970SChristoph Hellwig case NVMF_ADDR_FAMILY_IP4: 44a07b4970SChristoph Hellwig return sprintf(page, "ipv4\n"); 45a07b4970SChristoph Hellwig case NVMF_ADDR_FAMILY_IP6: 46a07b4970SChristoph Hellwig return sprintf(page, "ipv6\n"); 47a07b4970SChristoph Hellwig case NVMF_ADDR_FAMILY_IB: 48a07b4970SChristoph Hellwig return sprintf(page, "ib\n"); 49885aa401SJames Smart case NVMF_ADDR_FAMILY_FC: 50885aa401SJames Smart return sprintf(page, "fc\n"); 51a07b4970SChristoph Hellwig default: 52a07b4970SChristoph Hellwig return sprintf(page, "\n"); 53a07b4970SChristoph Hellwig } 54a07b4970SChristoph Hellwig } 55a07b4970SChristoph Hellwig 56a07b4970SChristoph Hellwig static ssize_t nvmet_addr_adrfam_store(struct config_item *item, 57a07b4970SChristoph Hellwig const char *page, size_t count) 58a07b4970SChristoph Hellwig { 59a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 60a07b4970SChristoph Hellwig 61a07b4970SChristoph Hellwig if (port->enabled) { 62a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 63a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 64a07b4970SChristoph Hellwig return -EACCES; 65a07b4970SChristoph Hellwig } 66a07b4970SChristoph Hellwig 67a07b4970SChristoph Hellwig if (sysfs_streq(page, "ipv4")) { 68a07b4970SChristoph Hellwig port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IP4; 69a07b4970SChristoph Hellwig } else if (sysfs_streq(page, "ipv6")) { 70a07b4970SChristoph Hellwig port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IP6; 71a07b4970SChristoph Hellwig } else if (sysfs_streq(page, "ib")) { 72a07b4970SChristoph Hellwig port->disc_addr.adrfam = NVMF_ADDR_FAMILY_IB; 73885aa401SJames Smart } else if (sysfs_streq(page, "fc")) { 74885aa401SJames Smart port->disc_addr.adrfam = NVMF_ADDR_FAMILY_FC; 75a07b4970SChristoph Hellwig } else { 76a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for adrfam\n", page); 77a07b4970SChristoph Hellwig return -EINVAL; 78a07b4970SChristoph Hellwig } 79a07b4970SChristoph Hellwig 80a07b4970SChristoph Hellwig return count; 81a07b4970SChristoph Hellwig } 82a07b4970SChristoph Hellwig 83a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_adrfam); 84a07b4970SChristoph Hellwig 85a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_show(struct config_item *item, 86a07b4970SChristoph Hellwig char *page) 87a07b4970SChristoph Hellwig { 88a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 89a07b4970SChristoph Hellwig 90a07b4970SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%d\n", 91a07b4970SChristoph Hellwig le16_to_cpu(port->disc_addr.portid)); 92a07b4970SChristoph Hellwig } 93a07b4970SChristoph Hellwig 94a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_store(struct config_item *item, 95a07b4970SChristoph Hellwig const char *page, size_t count) 96a07b4970SChristoph Hellwig { 97a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 98a07b4970SChristoph Hellwig u16 portid = 0; 99a07b4970SChristoph Hellwig 100a07b4970SChristoph Hellwig if (kstrtou16(page, 0, &portid)) { 101a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for portid\n", page); 102a07b4970SChristoph Hellwig return -EINVAL; 103a07b4970SChristoph Hellwig } 104a07b4970SChristoph Hellwig 105a07b4970SChristoph Hellwig if (port->enabled) { 106a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 107a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 108a07b4970SChristoph Hellwig return -EACCES; 109a07b4970SChristoph Hellwig } 110a07b4970SChristoph Hellwig port->disc_addr.portid = cpu_to_le16(portid); 111a07b4970SChristoph Hellwig return count; 112a07b4970SChristoph Hellwig } 113a07b4970SChristoph Hellwig 114a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_portid); 115a07b4970SChristoph Hellwig 116a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_show(struct config_item *item, 117a07b4970SChristoph Hellwig char *page) 118a07b4970SChristoph Hellwig { 119a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 120a07b4970SChristoph Hellwig 121a07b4970SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%s\n", 122a07b4970SChristoph Hellwig port->disc_addr.traddr); 123a07b4970SChristoph Hellwig } 124a07b4970SChristoph Hellwig 125a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_store(struct config_item *item, 126a07b4970SChristoph Hellwig const char *page, size_t count) 127a07b4970SChristoph Hellwig { 128a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 129a07b4970SChristoph Hellwig 130a07b4970SChristoph Hellwig if (count > NVMF_TRADDR_SIZE) { 131a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for traddr\n", page); 132a07b4970SChristoph Hellwig return -EINVAL; 133a07b4970SChristoph Hellwig } 134a07b4970SChristoph Hellwig 135a07b4970SChristoph Hellwig if (port->enabled) { 136a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 137a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 138a07b4970SChristoph Hellwig return -EACCES; 139a07b4970SChristoph Hellwig } 140a07b4970SChristoph Hellwig return snprintf(port->disc_addr.traddr, 141a07b4970SChristoph Hellwig sizeof(port->disc_addr.traddr), "%s", page); 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 { 149a07b4970SChristoph Hellwig switch (to_nvmet_port(item)->disc_addr.treq) { 150a07b4970SChristoph Hellwig case NVMF_TREQ_NOT_SPECIFIED: 151a07b4970SChristoph Hellwig return sprintf(page, "not specified\n"); 152a07b4970SChristoph Hellwig case NVMF_TREQ_REQUIRED: 153a07b4970SChristoph Hellwig return sprintf(page, "required\n"); 154a07b4970SChristoph Hellwig case NVMF_TREQ_NOT_REQUIRED: 155a07b4970SChristoph Hellwig return sprintf(page, "not required\n"); 156a07b4970SChristoph Hellwig default: 157a07b4970SChristoph Hellwig return sprintf(page, "\n"); 158a07b4970SChristoph Hellwig } 159a07b4970SChristoph Hellwig } 160a07b4970SChristoph Hellwig 161a07b4970SChristoph Hellwig static ssize_t nvmet_addr_treq_store(struct config_item *item, 162a07b4970SChristoph Hellwig const char *page, size_t count) 163a07b4970SChristoph Hellwig { 164a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 165a07b4970SChristoph Hellwig 166a07b4970SChristoph Hellwig if (port->enabled) { 167a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 168a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 169a07b4970SChristoph Hellwig return -EACCES; 170a07b4970SChristoph Hellwig } 171a07b4970SChristoph Hellwig 172a07b4970SChristoph Hellwig if (sysfs_streq(page, "not specified")) { 173a07b4970SChristoph Hellwig port->disc_addr.treq = NVMF_TREQ_NOT_SPECIFIED; 174a07b4970SChristoph Hellwig } else if (sysfs_streq(page, "required")) { 175a07b4970SChristoph Hellwig port->disc_addr.treq = NVMF_TREQ_REQUIRED; 176a07b4970SChristoph Hellwig } else if (sysfs_streq(page, "not required")) { 177a07b4970SChristoph Hellwig port->disc_addr.treq = NVMF_TREQ_NOT_REQUIRED; 178a07b4970SChristoph Hellwig } else { 179a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for treq\n", page); 180a07b4970SChristoph Hellwig return -EINVAL; 181a07b4970SChristoph Hellwig } 182a07b4970SChristoph Hellwig 183a07b4970SChristoph Hellwig return count; 184a07b4970SChristoph Hellwig } 185a07b4970SChristoph Hellwig 186a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_treq); 187a07b4970SChristoph Hellwig 188a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_show(struct config_item *item, 189a07b4970SChristoph Hellwig char *page) 190a07b4970SChristoph Hellwig { 191a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 192a07b4970SChristoph Hellwig 193a07b4970SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%s\n", 194a07b4970SChristoph Hellwig port->disc_addr.trsvcid); 195a07b4970SChristoph Hellwig } 196a07b4970SChristoph Hellwig 197a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_store(struct config_item *item, 198a07b4970SChristoph Hellwig const char *page, size_t count) 199a07b4970SChristoph Hellwig { 200a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 201a07b4970SChristoph Hellwig 202a07b4970SChristoph Hellwig if (count > NVMF_TRSVCID_SIZE) { 203a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for trsvcid\n", page); 204a07b4970SChristoph Hellwig return -EINVAL; 205a07b4970SChristoph Hellwig } 206a07b4970SChristoph Hellwig if (port->enabled) { 207a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 208a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 209a07b4970SChristoph Hellwig return -EACCES; 210a07b4970SChristoph Hellwig } 211a07b4970SChristoph Hellwig return snprintf(port->disc_addr.trsvcid, 212a07b4970SChristoph Hellwig sizeof(port->disc_addr.trsvcid), "%s", page); 213a07b4970SChristoph Hellwig } 214a07b4970SChristoph Hellwig 215a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trsvcid); 216a07b4970SChristoph Hellwig 217a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_show(struct config_item *item, 218a07b4970SChristoph Hellwig char *page) 219a07b4970SChristoph Hellwig { 220a5d18612SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 221a5d18612SChristoph Hellwig int i; 222a5d18612SChristoph Hellwig 223a5d18612SChristoph Hellwig for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) { 224a5d18612SChristoph Hellwig if (port->disc_addr.trtype != nvmet_transport_names[i].type) 225a5d18612SChristoph Hellwig continue; 226a5d18612SChristoph Hellwig return sprintf(page, "%s\n", nvmet_transport_names[i].name); 227a07b4970SChristoph Hellwig } 228a5d18612SChristoph Hellwig 229a5d18612SChristoph Hellwig return sprintf(page, "\n"); 230a07b4970SChristoph Hellwig } 231a07b4970SChristoph Hellwig 232a07b4970SChristoph Hellwig static void nvmet_port_init_tsas_rdma(struct nvmet_port *port) 233a07b4970SChristoph Hellwig { 234a07b4970SChristoph Hellwig port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED; 235a07b4970SChristoph Hellwig port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED; 236a07b4970SChristoph Hellwig port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM; 237a07b4970SChristoph Hellwig } 238a07b4970SChristoph Hellwig 239a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_store(struct config_item *item, 240a07b4970SChristoph Hellwig const char *page, size_t count) 241a07b4970SChristoph Hellwig { 242a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 243a5d18612SChristoph Hellwig int i; 244a07b4970SChristoph Hellwig 245a07b4970SChristoph Hellwig if (port->enabled) { 246a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 247a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 248a07b4970SChristoph Hellwig return -EACCES; 249a07b4970SChristoph Hellwig } 250a07b4970SChristoph Hellwig 251a5d18612SChristoph Hellwig for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) { 252a5d18612SChristoph Hellwig if (sysfs_streq(page, nvmet_transport_names[i].name)) 253a5d18612SChristoph Hellwig goto found; 254a07b4970SChristoph Hellwig } 255a07b4970SChristoph Hellwig 256a5d18612SChristoph Hellwig pr_err("Invalid value '%s' for trtype\n", page); 257a5d18612SChristoph Hellwig return -EINVAL; 258a5d18612SChristoph Hellwig found: 259a5d18612SChristoph Hellwig memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE); 260a5d18612SChristoph Hellwig port->disc_addr.trtype = nvmet_transport_names[i].type; 261a5d18612SChristoph Hellwig if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA) 262a5d18612SChristoph Hellwig nvmet_port_init_tsas_rdma(port); 263a07b4970SChristoph Hellwig return count; 264a07b4970SChristoph Hellwig } 265a07b4970SChristoph Hellwig 266a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trtype); 267a07b4970SChristoph Hellwig 268a07b4970SChristoph Hellwig /* 269a07b4970SChristoph Hellwig * Namespace structures & file operation functions below 270a07b4970SChristoph Hellwig */ 271a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page) 272a07b4970SChristoph Hellwig { 273a07b4970SChristoph Hellwig return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path); 274a07b4970SChristoph Hellwig } 275a07b4970SChristoph Hellwig 276a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_store(struct config_item *item, 277a07b4970SChristoph Hellwig const char *page, size_t count) 278a07b4970SChristoph Hellwig { 279a07b4970SChristoph Hellwig struct nvmet_ns *ns = to_nvmet_ns(item); 280a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = ns->subsys; 281a07b4970SChristoph Hellwig int ret; 282a07b4970SChristoph Hellwig 283a07b4970SChristoph Hellwig mutex_lock(&subsys->lock); 284a07b4970SChristoph Hellwig ret = -EBUSY; 285e4fcf07cSSolganik Alexander if (ns->enabled) 286a07b4970SChristoph Hellwig goto out_unlock; 287a07b4970SChristoph Hellwig 288a07b4970SChristoph Hellwig kfree(ns->device_path); 289a07b4970SChristoph Hellwig 290a07b4970SChristoph Hellwig ret = -ENOMEM; 291a07b4970SChristoph Hellwig ns->device_path = kstrdup(page, GFP_KERNEL); 292a07b4970SChristoph Hellwig if (!ns->device_path) 293a07b4970SChristoph Hellwig goto out_unlock; 294a07b4970SChristoph Hellwig 295a07b4970SChristoph Hellwig mutex_unlock(&subsys->lock); 296a07b4970SChristoph Hellwig return count; 297a07b4970SChristoph Hellwig 298a07b4970SChristoph Hellwig out_unlock: 299a07b4970SChristoph Hellwig mutex_unlock(&subsys->lock); 300a07b4970SChristoph Hellwig return ret; 301a07b4970SChristoph Hellwig } 302a07b4970SChristoph Hellwig 303a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_path); 304a07b4970SChristoph Hellwig 305430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page) 306430c7befSJohannes Thumshirn { 307430c7befSJohannes Thumshirn return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid); 308430c7befSJohannes Thumshirn } 309430c7befSJohannes Thumshirn 310430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_store(struct config_item *item, 311430c7befSJohannes Thumshirn const char *page, size_t count) 312430c7befSJohannes Thumshirn { 313430c7befSJohannes Thumshirn struct nvmet_ns *ns = to_nvmet_ns(item); 314430c7befSJohannes Thumshirn struct nvmet_subsys *subsys = ns->subsys; 315430c7befSJohannes Thumshirn int ret = 0; 316430c7befSJohannes Thumshirn 317430c7befSJohannes Thumshirn 318430c7befSJohannes Thumshirn mutex_lock(&subsys->lock); 319430c7befSJohannes Thumshirn if (ns->enabled) { 320430c7befSJohannes Thumshirn ret = -EBUSY; 321430c7befSJohannes Thumshirn goto out_unlock; 322430c7befSJohannes Thumshirn } 323430c7befSJohannes Thumshirn 324430c7befSJohannes Thumshirn 325430c7befSJohannes Thumshirn if (uuid_parse(page, &ns->uuid)) 326430c7befSJohannes Thumshirn ret = -EINVAL; 327430c7befSJohannes Thumshirn 328430c7befSJohannes Thumshirn out_unlock: 329430c7befSJohannes Thumshirn mutex_unlock(&subsys->lock); 330430c7befSJohannes Thumshirn return ret ? ret : count; 331430c7befSJohannes Thumshirn } 332430c7befSJohannes Thumshirn 333f871749aSMax Gurtovoy CONFIGFS_ATTR(nvmet_ns_, device_uuid); 334f871749aSMax Gurtovoy 335a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page) 336a07b4970SChristoph Hellwig { 337a07b4970SChristoph Hellwig return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid); 338a07b4970SChristoph Hellwig } 339a07b4970SChristoph Hellwig 340a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_store(struct config_item *item, 341a07b4970SChristoph Hellwig const char *page, size_t count) 342a07b4970SChristoph Hellwig { 343a07b4970SChristoph Hellwig struct nvmet_ns *ns = to_nvmet_ns(item); 344a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = ns->subsys; 345a07b4970SChristoph Hellwig u8 nguid[16]; 346a07b4970SChristoph Hellwig const char *p = page; 347a07b4970SChristoph Hellwig int i; 348a07b4970SChristoph Hellwig int ret = 0; 349a07b4970SChristoph Hellwig 350a07b4970SChristoph Hellwig mutex_lock(&subsys->lock); 351e4fcf07cSSolganik Alexander if (ns->enabled) { 352a07b4970SChristoph Hellwig ret = -EBUSY; 353a07b4970SChristoph Hellwig goto out_unlock; 354a07b4970SChristoph Hellwig } 355a07b4970SChristoph Hellwig 356a07b4970SChristoph Hellwig for (i = 0; i < 16; i++) { 357a07b4970SChristoph Hellwig if (p + 2 > page + count) { 358a07b4970SChristoph Hellwig ret = -EINVAL; 359a07b4970SChristoph Hellwig goto out_unlock; 360a07b4970SChristoph Hellwig } 361a07b4970SChristoph Hellwig if (!isxdigit(p[0]) || !isxdigit(p[1])) { 362a07b4970SChristoph Hellwig ret = -EINVAL; 363a07b4970SChristoph Hellwig goto out_unlock; 364a07b4970SChristoph Hellwig } 365a07b4970SChristoph Hellwig 366a07b4970SChristoph Hellwig nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]); 367a07b4970SChristoph Hellwig p += 2; 368a07b4970SChristoph Hellwig 369a07b4970SChristoph Hellwig if (*p == '-' || *p == ':') 370a07b4970SChristoph Hellwig p++; 371a07b4970SChristoph Hellwig } 372a07b4970SChristoph Hellwig 373a07b4970SChristoph Hellwig memcpy(&ns->nguid, nguid, sizeof(nguid)); 374a07b4970SChristoph Hellwig out_unlock: 375a07b4970SChristoph Hellwig mutex_unlock(&subsys->lock); 376a07b4970SChristoph Hellwig return ret ? ret : count; 377a07b4970SChristoph Hellwig } 378a07b4970SChristoph Hellwig 379a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_nguid); 380a07b4970SChristoph Hellwig 381a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page) 382a07b4970SChristoph Hellwig { 383e4fcf07cSSolganik Alexander return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled); 384a07b4970SChristoph Hellwig } 385a07b4970SChristoph Hellwig 386a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_store(struct config_item *item, 387a07b4970SChristoph Hellwig const char *page, size_t count) 388a07b4970SChristoph Hellwig { 389a07b4970SChristoph Hellwig struct nvmet_ns *ns = to_nvmet_ns(item); 390a07b4970SChristoph Hellwig bool enable; 391a07b4970SChristoph Hellwig int ret = 0; 392a07b4970SChristoph Hellwig 393a07b4970SChristoph Hellwig if (strtobool(page, &enable)) 394a07b4970SChristoph Hellwig return -EINVAL; 395a07b4970SChristoph Hellwig 396a07b4970SChristoph Hellwig if (enable) 397a07b4970SChristoph Hellwig ret = nvmet_ns_enable(ns); 398a07b4970SChristoph Hellwig else 399a07b4970SChristoph Hellwig nvmet_ns_disable(ns); 400a07b4970SChristoph Hellwig 401a07b4970SChristoph Hellwig return ret ? ret : count; 402a07b4970SChristoph Hellwig } 403a07b4970SChristoph Hellwig 404a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, enable); 405a07b4970SChristoph Hellwig 406a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_ns_attrs[] = { 407a07b4970SChristoph Hellwig &nvmet_ns_attr_device_path, 408a07b4970SChristoph Hellwig &nvmet_ns_attr_device_nguid, 409430c7befSJohannes Thumshirn &nvmet_ns_attr_device_uuid, 410a07b4970SChristoph Hellwig &nvmet_ns_attr_enable, 411a07b4970SChristoph Hellwig NULL, 412a07b4970SChristoph Hellwig }; 413a07b4970SChristoph Hellwig 414a07b4970SChristoph Hellwig static void nvmet_ns_release(struct config_item *item) 415a07b4970SChristoph Hellwig { 416a07b4970SChristoph Hellwig struct nvmet_ns *ns = to_nvmet_ns(item); 417a07b4970SChristoph Hellwig 418a07b4970SChristoph Hellwig nvmet_ns_free(ns); 419a07b4970SChristoph Hellwig } 420a07b4970SChristoph Hellwig 421a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_ns_item_ops = { 422a07b4970SChristoph Hellwig .release = nvmet_ns_release, 423a07b4970SChristoph Hellwig }; 424a07b4970SChristoph Hellwig 42566603a31SBhumika Goyal static const struct config_item_type nvmet_ns_type = { 426a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_ns_item_ops, 427a07b4970SChristoph Hellwig .ct_attrs = nvmet_ns_attrs, 428a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 429a07b4970SChristoph Hellwig }; 430a07b4970SChristoph Hellwig 431a07b4970SChristoph Hellwig static struct config_group *nvmet_ns_make(struct config_group *group, 432a07b4970SChristoph Hellwig const char *name) 433a07b4970SChristoph Hellwig { 434a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item); 435a07b4970SChristoph Hellwig struct nvmet_ns *ns; 436a07b4970SChristoph Hellwig int ret; 437a07b4970SChristoph Hellwig u32 nsid; 438a07b4970SChristoph Hellwig 439a07b4970SChristoph Hellwig ret = kstrtou32(name, 0, &nsid); 440a07b4970SChristoph Hellwig if (ret) 441a07b4970SChristoph Hellwig goto out; 442a07b4970SChristoph Hellwig 443a07b4970SChristoph Hellwig ret = -EINVAL; 4441645d503SChristoph Hellwig if (nsid == 0 || nsid == NVME_NSID_ALL) 445a07b4970SChristoph Hellwig goto out; 446a07b4970SChristoph Hellwig 447a07b4970SChristoph Hellwig ret = -ENOMEM; 448a07b4970SChristoph Hellwig ns = nvmet_ns_alloc(subsys, nsid); 449a07b4970SChristoph Hellwig if (!ns) 450a07b4970SChristoph Hellwig goto out; 451a07b4970SChristoph Hellwig config_group_init_type_name(&ns->group, name, &nvmet_ns_type); 452a07b4970SChristoph Hellwig 453a07b4970SChristoph Hellwig pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn); 454a07b4970SChristoph Hellwig 455a07b4970SChristoph Hellwig return &ns->group; 456a07b4970SChristoph Hellwig out: 457a07b4970SChristoph Hellwig return ERR_PTR(ret); 458a07b4970SChristoph Hellwig } 459a07b4970SChristoph Hellwig 460a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_namespaces_group_ops = { 461a07b4970SChristoph Hellwig .make_group = nvmet_ns_make, 462a07b4970SChristoph Hellwig }; 463a07b4970SChristoph Hellwig 46466603a31SBhumika Goyal static const struct config_item_type nvmet_namespaces_type = { 465a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_namespaces_group_ops, 466a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 467a07b4970SChristoph Hellwig }; 468a07b4970SChristoph Hellwig 469a07b4970SChristoph Hellwig static int nvmet_port_subsys_allow_link(struct config_item *parent, 470a07b4970SChristoph Hellwig struct config_item *target) 471a07b4970SChristoph Hellwig { 472a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(parent->ci_parent); 473a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 474a07b4970SChristoph Hellwig struct nvmet_subsys_link *link, *p; 475a07b4970SChristoph Hellwig int ret; 476a07b4970SChristoph Hellwig 477a07b4970SChristoph Hellwig if (target->ci_type != &nvmet_subsys_type) { 478a07b4970SChristoph Hellwig pr_err("can only link subsystems into the subsystems dir.!\n"); 479a07b4970SChristoph Hellwig return -EINVAL; 480a07b4970SChristoph Hellwig } 481a07b4970SChristoph Hellwig subsys = to_subsys(target); 482a07b4970SChristoph Hellwig link = kmalloc(sizeof(*link), GFP_KERNEL); 483a07b4970SChristoph Hellwig if (!link) 484a07b4970SChristoph Hellwig return -ENOMEM; 485a07b4970SChristoph Hellwig link->subsys = subsys; 486a07b4970SChristoph Hellwig 487a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 488a07b4970SChristoph Hellwig ret = -EEXIST; 489a07b4970SChristoph Hellwig list_for_each_entry(p, &port->subsystems, entry) { 490a07b4970SChristoph Hellwig if (p->subsys == subsys) 491a07b4970SChristoph Hellwig goto out_free_link; 492a07b4970SChristoph Hellwig } 493a07b4970SChristoph Hellwig 494a07b4970SChristoph Hellwig if (list_empty(&port->subsystems)) { 495a07b4970SChristoph Hellwig ret = nvmet_enable_port(port); 496a07b4970SChristoph Hellwig if (ret) 497a07b4970SChristoph Hellwig goto out_free_link; 498a07b4970SChristoph Hellwig } 499a07b4970SChristoph Hellwig 500a07b4970SChristoph Hellwig list_add_tail(&link->entry, &port->subsystems); 501a07b4970SChristoph Hellwig nvmet_genctr++; 502a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 503a07b4970SChristoph Hellwig return 0; 504a07b4970SChristoph Hellwig 505a07b4970SChristoph Hellwig out_free_link: 506a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 507a07b4970SChristoph Hellwig kfree(link); 508a07b4970SChristoph Hellwig return ret; 509a07b4970SChristoph Hellwig } 510a07b4970SChristoph Hellwig 511e16769d4SAndrzej Pietrasiewicz static void nvmet_port_subsys_drop_link(struct config_item *parent, 512a07b4970SChristoph Hellwig struct config_item *target) 513a07b4970SChristoph Hellwig { 514a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(parent->ci_parent); 515a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(target); 516a07b4970SChristoph Hellwig struct nvmet_subsys_link *p; 517a07b4970SChristoph Hellwig 518a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 519a07b4970SChristoph Hellwig list_for_each_entry(p, &port->subsystems, entry) { 520a07b4970SChristoph Hellwig if (p->subsys == subsys) 521a07b4970SChristoph Hellwig goto found; 522a07b4970SChristoph Hellwig } 523a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 524e16769d4SAndrzej Pietrasiewicz return; 525a07b4970SChristoph Hellwig 526a07b4970SChristoph Hellwig found: 527a07b4970SChristoph Hellwig list_del(&p->entry); 528a07b4970SChristoph Hellwig nvmet_genctr++; 529a07b4970SChristoph Hellwig if (list_empty(&port->subsystems)) 530a07b4970SChristoph Hellwig nvmet_disable_port(port); 531a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 532a07b4970SChristoph Hellwig kfree(p); 533a07b4970SChristoph Hellwig } 534a07b4970SChristoph Hellwig 535a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_subsys_item_ops = { 536a07b4970SChristoph Hellwig .allow_link = nvmet_port_subsys_allow_link, 537a07b4970SChristoph Hellwig .drop_link = nvmet_port_subsys_drop_link, 538a07b4970SChristoph Hellwig }; 539a07b4970SChristoph Hellwig 54066603a31SBhumika Goyal static const struct config_item_type nvmet_port_subsys_type = { 541a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_port_subsys_item_ops, 542a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 543a07b4970SChristoph Hellwig }; 544a07b4970SChristoph Hellwig 545a07b4970SChristoph Hellwig static int nvmet_allowed_hosts_allow_link(struct config_item *parent, 546a07b4970SChristoph Hellwig struct config_item *target) 547a07b4970SChristoph Hellwig { 548a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(parent->ci_parent); 549a07b4970SChristoph Hellwig struct nvmet_host *host; 550a07b4970SChristoph Hellwig struct nvmet_host_link *link, *p; 551a07b4970SChristoph Hellwig int ret; 552a07b4970SChristoph Hellwig 553a07b4970SChristoph Hellwig if (target->ci_type != &nvmet_host_type) { 554a07b4970SChristoph Hellwig pr_err("can only link hosts into the allowed_hosts directory!\n"); 555a07b4970SChristoph Hellwig return -EINVAL; 556a07b4970SChristoph Hellwig } 557a07b4970SChristoph Hellwig 558a07b4970SChristoph Hellwig host = to_host(target); 559a07b4970SChristoph Hellwig link = kmalloc(sizeof(*link), GFP_KERNEL); 560a07b4970SChristoph Hellwig if (!link) 561a07b4970SChristoph Hellwig return -ENOMEM; 562a07b4970SChristoph Hellwig link->host = host; 563a07b4970SChristoph Hellwig 564a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 565a07b4970SChristoph Hellwig ret = -EINVAL; 566a07b4970SChristoph Hellwig if (subsys->allow_any_host) { 567a07b4970SChristoph Hellwig pr_err("can't add hosts when allow_any_host is set!\n"); 568a07b4970SChristoph Hellwig goto out_free_link; 569a07b4970SChristoph Hellwig } 570a07b4970SChristoph Hellwig 571a07b4970SChristoph Hellwig ret = -EEXIST; 572a07b4970SChristoph Hellwig list_for_each_entry(p, &subsys->hosts, entry) { 573a07b4970SChristoph Hellwig if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host))) 574a07b4970SChristoph Hellwig goto out_free_link; 575a07b4970SChristoph Hellwig } 576a07b4970SChristoph Hellwig list_add_tail(&link->entry, &subsys->hosts); 577a07b4970SChristoph Hellwig nvmet_genctr++; 578a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 579a07b4970SChristoph Hellwig return 0; 580a07b4970SChristoph Hellwig out_free_link: 581a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 582a07b4970SChristoph Hellwig kfree(link); 583a07b4970SChristoph Hellwig return ret; 584a07b4970SChristoph Hellwig } 585a07b4970SChristoph Hellwig 586e16769d4SAndrzej Pietrasiewicz static void nvmet_allowed_hosts_drop_link(struct config_item *parent, 587a07b4970SChristoph Hellwig struct config_item *target) 588a07b4970SChristoph Hellwig { 589a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(parent->ci_parent); 590a07b4970SChristoph Hellwig struct nvmet_host *host = to_host(target); 591a07b4970SChristoph Hellwig struct nvmet_host_link *p; 592a07b4970SChristoph Hellwig 593a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 594a07b4970SChristoph Hellwig list_for_each_entry(p, &subsys->hosts, entry) { 595a07b4970SChristoph Hellwig if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host))) 596a07b4970SChristoph Hellwig goto found; 597a07b4970SChristoph Hellwig } 598a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 599e16769d4SAndrzej Pietrasiewicz return; 600a07b4970SChristoph Hellwig 601a07b4970SChristoph Hellwig found: 602a07b4970SChristoph Hellwig list_del(&p->entry); 603a07b4970SChristoph Hellwig nvmet_genctr++; 604a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 605a07b4970SChristoph Hellwig kfree(p); 606a07b4970SChristoph Hellwig } 607a07b4970SChristoph Hellwig 608a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_allowed_hosts_item_ops = { 609a07b4970SChristoph Hellwig .allow_link = nvmet_allowed_hosts_allow_link, 610a07b4970SChristoph Hellwig .drop_link = nvmet_allowed_hosts_drop_link, 611a07b4970SChristoph Hellwig }; 612a07b4970SChristoph Hellwig 61366603a31SBhumika Goyal static const struct config_item_type nvmet_allowed_hosts_type = { 614a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_allowed_hosts_item_ops, 615a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 616a07b4970SChristoph Hellwig }; 617a07b4970SChristoph Hellwig 618a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item, 619a07b4970SChristoph Hellwig char *page) 620a07b4970SChristoph Hellwig { 621a07b4970SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%d\n", 622a07b4970SChristoph Hellwig to_subsys(item)->allow_any_host); 623a07b4970SChristoph Hellwig } 624a07b4970SChristoph Hellwig 625a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item, 626a07b4970SChristoph Hellwig const char *page, size_t count) 627a07b4970SChristoph Hellwig { 628a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(item); 629a07b4970SChristoph Hellwig bool allow_any_host; 630a07b4970SChristoph Hellwig int ret = 0; 631a07b4970SChristoph Hellwig 632a07b4970SChristoph Hellwig if (strtobool(page, &allow_any_host)) 633a07b4970SChristoph Hellwig return -EINVAL; 634a07b4970SChristoph Hellwig 635a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 636a07b4970SChristoph Hellwig if (allow_any_host && !list_empty(&subsys->hosts)) { 637a07b4970SChristoph Hellwig pr_err("Can't set allow_any_host when explicit hosts are set!\n"); 638a07b4970SChristoph Hellwig ret = -EINVAL; 639a07b4970SChristoph Hellwig goto out_unlock; 640a07b4970SChristoph Hellwig } 641a07b4970SChristoph Hellwig 642a07b4970SChristoph Hellwig subsys->allow_any_host = allow_any_host; 643a07b4970SChristoph Hellwig out_unlock: 644a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 645a07b4970SChristoph Hellwig return ret ? ret : count; 646a07b4970SChristoph Hellwig } 647a07b4970SChristoph Hellwig 648a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host); 649a07b4970SChristoph Hellwig 65041528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_show(struct config_item *item, 651c61d788bSJohannes Thumshirn char *page) 652c61d788bSJohannes Thumshirn { 653c61d788bSJohannes Thumshirn struct nvmet_subsys *subsys = to_subsys(item); 654c61d788bSJohannes Thumshirn 655c61d788bSJohannes Thumshirn if (NVME_TERTIARY(subsys->ver)) 656c61d788bSJohannes Thumshirn return snprintf(page, PAGE_SIZE, "%d.%d.%d\n", 657c61d788bSJohannes Thumshirn (int)NVME_MAJOR(subsys->ver), 658c61d788bSJohannes Thumshirn (int)NVME_MINOR(subsys->ver), 659c61d788bSJohannes Thumshirn (int)NVME_TERTIARY(subsys->ver)); 660c61d788bSJohannes Thumshirn else 661c61d788bSJohannes Thumshirn return snprintf(page, PAGE_SIZE, "%d.%d\n", 662c61d788bSJohannes Thumshirn (int)NVME_MAJOR(subsys->ver), 663c61d788bSJohannes Thumshirn (int)NVME_MINOR(subsys->ver)); 664c61d788bSJohannes Thumshirn } 665c61d788bSJohannes Thumshirn 66641528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, 667c61d788bSJohannes Thumshirn const char *page, size_t count) 668c61d788bSJohannes Thumshirn { 669c61d788bSJohannes Thumshirn struct nvmet_subsys *subsys = to_subsys(item); 670c61d788bSJohannes Thumshirn int major, minor, tertiary = 0; 671c61d788bSJohannes Thumshirn int ret; 672c61d788bSJohannes Thumshirn 673c61d788bSJohannes Thumshirn 674c61d788bSJohannes Thumshirn ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary); 675c61d788bSJohannes Thumshirn if (ret != 2 && ret != 3) 676c61d788bSJohannes Thumshirn return -EINVAL; 677c61d788bSJohannes Thumshirn 678c61d788bSJohannes Thumshirn down_write(&nvmet_config_sem); 679c61d788bSJohannes Thumshirn subsys->ver = NVME_VS(major, minor, tertiary); 680c61d788bSJohannes Thumshirn up_write(&nvmet_config_sem); 681c61d788bSJohannes Thumshirn 682c61d788bSJohannes Thumshirn return count; 683c61d788bSJohannes Thumshirn } 68441528f80SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_version); 685c61d788bSJohannes Thumshirn 686fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item, 687fcbc5459SJohannes Thumshirn char *page) 688fcbc5459SJohannes Thumshirn { 689fcbc5459SJohannes Thumshirn struct nvmet_subsys *subsys = to_subsys(item); 690fcbc5459SJohannes Thumshirn 691fcbc5459SJohannes Thumshirn return snprintf(page, PAGE_SIZE, "%llx\n", subsys->serial); 692fcbc5459SJohannes Thumshirn } 693fcbc5459SJohannes Thumshirn 694fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item, 695fcbc5459SJohannes Thumshirn const char *page, size_t count) 696fcbc5459SJohannes Thumshirn { 697fcbc5459SJohannes Thumshirn struct nvmet_subsys *subsys = to_subsys(item); 698fcbc5459SJohannes Thumshirn 699fcbc5459SJohannes Thumshirn down_write(&nvmet_config_sem); 700fcbc5459SJohannes Thumshirn sscanf(page, "%llx\n", &subsys->serial); 701fcbc5459SJohannes Thumshirn up_write(&nvmet_config_sem); 702fcbc5459SJohannes Thumshirn 703fcbc5459SJohannes Thumshirn return count; 704fcbc5459SJohannes Thumshirn } 705fcbc5459SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_serial); 706fcbc5459SJohannes Thumshirn 707a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_subsys_attrs[] = { 708a07b4970SChristoph Hellwig &nvmet_subsys_attr_attr_allow_any_host, 70941528f80SJohannes Thumshirn &nvmet_subsys_attr_attr_version, 710fcbc5459SJohannes Thumshirn &nvmet_subsys_attr_attr_serial, 711a07b4970SChristoph Hellwig NULL, 712a07b4970SChristoph Hellwig }; 713a07b4970SChristoph Hellwig 714a07b4970SChristoph Hellwig /* 715a07b4970SChristoph Hellwig * Subsystem structures & folder operation functions below 716a07b4970SChristoph Hellwig */ 717a07b4970SChristoph Hellwig static void nvmet_subsys_release(struct config_item *item) 718a07b4970SChristoph Hellwig { 719a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(item); 720a07b4970SChristoph Hellwig 721344770b0SSagi Grimberg nvmet_subsys_del_ctrls(subsys); 722a07b4970SChristoph Hellwig nvmet_subsys_put(subsys); 723a07b4970SChristoph Hellwig } 724a07b4970SChristoph Hellwig 725a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_subsys_item_ops = { 726a07b4970SChristoph Hellwig .release = nvmet_subsys_release, 727a07b4970SChristoph Hellwig }; 728a07b4970SChristoph Hellwig 72966603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type = { 730a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_subsys_item_ops, 731a07b4970SChristoph Hellwig .ct_attrs = nvmet_subsys_attrs, 732a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 733a07b4970SChristoph Hellwig }; 734a07b4970SChristoph Hellwig 735a07b4970SChristoph Hellwig static struct config_group *nvmet_subsys_make(struct config_group *group, 736a07b4970SChristoph Hellwig const char *name) 737a07b4970SChristoph Hellwig { 738a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 739a07b4970SChristoph Hellwig 740a07b4970SChristoph Hellwig if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) { 741a07b4970SChristoph Hellwig pr_err("can't create discovery subsystem through configfs\n"); 742a07b4970SChristoph Hellwig return ERR_PTR(-EINVAL); 743a07b4970SChristoph Hellwig } 744a07b4970SChristoph Hellwig 745a07b4970SChristoph Hellwig subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME); 746a07b4970SChristoph Hellwig if (!subsys) 747a07b4970SChristoph Hellwig return ERR_PTR(-ENOMEM); 748a07b4970SChristoph Hellwig 749a07b4970SChristoph Hellwig config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type); 750a07b4970SChristoph Hellwig 751a07b4970SChristoph Hellwig config_group_init_type_name(&subsys->namespaces_group, 752a07b4970SChristoph Hellwig "namespaces", &nvmet_namespaces_type); 753a07b4970SChristoph Hellwig configfs_add_default_group(&subsys->namespaces_group, &subsys->group); 754a07b4970SChristoph Hellwig 755a07b4970SChristoph Hellwig config_group_init_type_name(&subsys->allowed_hosts_group, 756a07b4970SChristoph Hellwig "allowed_hosts", &nvmet_allowed_hosts_type); 757a07b4970SChristoph Hellwig configfs_add_default_group(&subsys->allowed_hosts_group, 758a07b4970SChristoph Hellwig &subsys->group); 759a07b4970SChristoph Hellwig 760a07b4970SChristoph Hellwig return &subsys->group; 761a07b4970SChristoph Hellwig } 762a07b4970SChristoph Hellwig 763a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_subsystems_group_ops = { 764a07b4970SChristoph Hellwig .make_group = nvmet_subsys_make, 765a07b4970SChristoph Hellwig }; 766a07b4970SChristoph Hellwig 76766603a31SBhumika Goyal static const struct config_item_type nvmet_subsystems_type = { 768a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_subsystems_group_ops, 769a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 770a07b4970SChristoph Hellwig }; 771a07b4970SChristoph Hellwig 772a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_show(struct config_item *item, 773a07b4970SChristoph Hellwig char *page) 774a07b4970SChristoph Hellwig { 775a07b4970SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled); 776a07b4970SChristoph Hellwig } 777a07b4970SChristoph Hellwig 778a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_store(struct config_item *item, 779a07b4970SChristoph Hellwig const char *page, size_t count) 780a07b4970SChristoph Hellwig { 781a07b4970SChristoph Hellwig struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent); 782a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 783a07b4970SChristoph Hellwig bool enable; 784a07b4970SChristoph Hellwig 785a07b4970SChristoph Hellwig if (strtobool(page, &enable)) 786a07b4970SChristoph Hellwig goto inval; 787a07b4970SChristoph Hellwig 788a07b4970SChristoph Hellwig if (enable) 789a07b4970SChristoph Hellwig nvmet_referral_enable(parent, port); 790a07b4970SChristoph Hellwig else 791a07b4970SChristoph Hellwig nvmet_referral_disable(port); 792a07b4970SChristoph Hellwig 793a07b4970SChristoph Hellwig return count; 794a07b4970SChristoph Hellwig inval: 795a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for enable\n", page); 796a07b4970SChristoph Hellwig return -EINVAL; 797a07b4970SChristoph Hellwig } 798a07b4970SChristoph Hellwig 799a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_referral_, enable); 800a07b4970SChristoph Hellwig 801a07b4970SChristoph Hellwig /* 802a07b4970SChristoph Hellwig * Discovery Service subsystem definitions 803a07b4970SChristoph Hellwig */ 804a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_referral_attrs[] = { 805a07b4970SChristoph Hellwig &nvmet_attr_addr_adrfam, 806a07b4970SChristoph Hellwig &nvmet_attr_addr_portid, 807a07b4970SChristoph Hellwig &nvmet_attr_addr_treq, 808a07b4970SChristoph Hellwig &nvmet_attr_addr_traddr, 809a07b4970SChristoph Hellwig &nvmet_attr_addr_trsvcid, 810a07b4970SChristoph Hellwig &nvmet_attr_addr_trtype, 811a07b4970SChristoph Hellwig &nvmet_referral_attr_enable, 812a07b4970SChristoph Hellwig NULL, 813a07b4970SChristoph Hellwig }; 814a07b4970SChristoph Hellwig 815a07b4970SChristoph Hellwig static void nvmet_referral_release(struct config_item *item) 816a07b4970SChristoph Hellwig { 817a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 818a07b4970SChristoph Hellwig 819a07b4970SChristoph Hellwig nvmet_referral_disable(port); 820a07b4970SChristoph Hellwig kfree(port); 821a07b4970SChristoph Hellwig } 822a07b4970SChristoph Hellwig 823a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_referral_item_ops = { 824a07b4970SChristoph Hellwig .release = nvmet_referral_release, 825a07b4970SChristoph Hellwig }; 826a07b4970SChristoph Hellwig 82766603a31SBhumika Goyal static const struct config_item_type nvmet_referral_type = { 828a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 829a07b4970SChristoph Hellwig .ct_attrs = nvmet_referral_attrs, 830a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_referral_item_ops, 831a07b4970SChristoph Hellwig }; 832a07b4970SChristoph Hellwig 833a07b4970SChristoph Hellwig static struct config_group *nvmet_referral_make( 834a07b4970SChristoph Hellwig struct config_group *group, const char *name) 835a07b4970SChristoph Hellwig { 836a07b4970SChristoph Hellwig struct nvmet_port *port; 837a07b4970SChristoph Hellwig 838a07b4970SChristoph Hellwig port = kzalloc(sizeof(*port), GFP_KERNEL); 839a07b4970SChristoph Hellwig if (!port) 840f98d9ca1SDan Carpenter return ERR_PTR(-ENOMEM); 841a07b4970SChristoph Hellwig 842a07b4970SChristoph Hellwig INIT_LIST_HEAD(&port->entry); 843a07b4970SChristoph Hellwig config_group_init_type_name(&port->group, name, &nvmet_referral_type); 844a07b4970SChristoph Hellwig 845a07b4970SChristoph Hellwig return &port->group; 846a07b4970SChristoph Hellwig } 847a07b4970SChristoph Hellwig 848a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_referral_group_ops = { 849a07b4970SChristoph Hellwig .make_group = nvmet_referral_make, 850a07b4970SChristoph Hellwig }; 851a07b4970SChristoph Hellwig 85266603a31SBhumika Goyal static const struct config_item_type nvmet_referrals_type = { 853a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 854a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_referral_group_ops, 855a07b4970SChristoph Hellwig }; 856a07b4970SChristoph Hellwig 857a07b4970SChristoph Hellwig /* 858a07b4970SChristoph Hellwig * Ports definitions. 859a07b4970SChristoph Hellwig */ 860a07b4970SChristoph Hellwig static void nvmet_port_release(struct config_item *item) 861a07b4970SChristoph Hellwig { 862a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 863a07b4970SChristoph Hellwig 864a07b4970SChristoph Hellwig kfree(port); 865a07b4970SChristoph Hellwig } 866a07b4970SChristoph Hellwig 867a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_port_attrs[] = { 868a07b4970SChristoph Hellwig &nvmet_attr_addr_adrfam, 869a07b4970SChristoph Hellwig &nvmet_attr_addr_treq, 870a07b4970SChristoph Hellwig &nvmet_attr_addr_traddr, 871a07b4970SChristoph Hellwig &nvmet_attr_addr_trsvcid, 872a07b4970SChristoph Hellwig &nvmet_attr_addr_trtype, 873a07b4970SChristoph Hellwig NULL, 874a07b4970SChristoph Hellwig }; 875a07b4970SChristoph Hellwig 876a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_item_ops = { 877a07b4970SChristoph Hellwig .release = nvmet_port_release, 878a07b4970SChristoph Hellwig }; 879a07b4970SChristoph Hellwig 88066603a31SBhumika Goyal static const struct config_item_type nvmet_port_type = { 881a07b4970SChristoph Hellwig .ct_attrs = nvmet_port_attrs, 882a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_port_item_ops, 883a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 884a07b4970SChristoph Hellwig }; 885a07b4970SChristoph Hellwig 886a07b4970SChristoph Hellwig static struct config_group *nvmet_ports_make(struct config_group *group, 887a07b4970SChristoph Hellwig const char *name) 888a07b4970SChristoph Hellwig { 889a07b4970SChristoph Hellwig struct nvmet_port *port; 890a07b4970SChristoph Hellwig u16 portid; 891a07b4970SChristoph Hellwig 892a07b4970SChristoph Hellwig if (kstrtou16(name, 0, &portid)) 893a07b4970SChristoph Hellwig return ERR_PTR(-EINVAL); 894a07b4970SChristoph Hellwig 895a07b4970SChristoph Hellwig port = kzalloc(sizeof(*port), GFP_KERNEL); 896a07b4970SChristoph Hellwig if (!port) 897f98d9ca1SDan Carpenter return ERR_PTR(-ENOMEM); 898a07b4970SChristoph Hellwig 899a07b4970SChristoph Hellwig INIT_LIST_HEAD(&port->entry); 900a07b4970SChristoph Hellwig INIT_LIST_HEAD(&port->subsystems); 901a07b4970SChristoph Hellwig INIT_LIST_HEAD(&port->referrals); 902a07b4970SChristoph Hellwig 903a07b4970SChristoph Hellwig port->disc_addr.portid = cpu_to_le16(portid); 904a07b4970SChristoph Hellwig config_group_init_type_name(&port->group, name, &nvmet_port_type); 905a07b4970SChristoph Hellwig 906a07b4970SChristoph Hellwig config_group_init_type_name(&port->subsys_group, 907a07b4970SChristoph Hellwig "subsystems", &nvmet_port_subsys_type); 908a07b4970SChristoph Hellwig configfs_add_default_group(&port->subsys_group, &port->group); 909a07b4970SChristoph Hellwig 910a07b4970SChristoph Hellwig config_group_init_type_name(&port->referrals_group, 911a07b4970SChristoph Hellwig "referrals", &nvmet_referrals_type); 912a07b4970SChristoph Hellwig configfs_add_default_group(&port->referrals_group, &port->group); 913a07b4970SChristoph Hellwig 914a07b4970SChristoph Hellwig return &port->group; 915a07b4970SChristoph Hellwig } 916a07b4970SChristoph Hellwig 917a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_ports_group_ops = { 918a07b4970SChristoph Hellwig .make_group = nvmet_ports_make, 919a07b4970SChristoph Hellwig }; 920a07b4970SChristoph Hellwig 92166603a31SBhumika Goyal static const struct config_item_type nvmet_ports_type = { 922a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_ports_group_ops, 923a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 924a07b4970SChristoph Hellwig }; 925a07b4970SChristoph Hellwig 926a07b4970SChristoph Hellwig static struct config_group nvmet_subsystems_group; 927a07b4970SChristoph Hellwig static struct config_group nvmet_ports_group; 928a07b4970SChristoph Hellwig 929a07b4970SChristoph Hellwig static void nvmet_host_release(struct config_item *item) 930a07b4970SChristoph Hellwig { 931a07b4970SChristoph Hellwig struct nvmet_host *host = to_host(item); 932a07b4970SChristoph Hellwig 933a07b4970SChristoph Hellwig kfree(host); 934a07b4970SChristoph Hellwig } 935a07b4970SChristoph Hellwig 936a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_host_item_ops = { 937a07b4970SChristoph Hellwig .release = nvmet_host_release, 938a07b4970SChristoph Hellwig }; 939a07b4970SChristoph Hellwig 94066603a31SBhumika Goyal static const struct config_item_type nvmet_host_type = { 941a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_host_item_ops, 942a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 943a07b4970SChristoph Hellwig }; 944a07b4970SChristoph Hellwig 945a07b4970SChristoph Hellwig static struct config_group *nvmet_hosts_make_group(struct config_group *group, 946a07b4970SChristoph Hellwig const char *name) 947a07b4970SChristoph Hellwig { 948a07b4970SChristoph Hellwig struct nvmet_host *host; 949a07b4970SChristoph Hellwig 950a07b4970SChristoph Hellwig host = kzalloc(sizeof(*host), GFP_KERNEL); 951a07b4970SChristoph Hellwig if (!host) 952a07b4970SChristoph Hellwig return ERR_PTR(-ENOMEM); 953a07b4970SChristoph Hellwig 954a07b4970SChristoph Hellwig config_group_init_type_name(&host->group, name, &nvmet_host_type); 955a07b4970SChristoph Hellwig 956a07b4970SChristoph Hellwig return &host->group; 957a07b4970SChristoph Hellwig } 958a07b4970SChristoph Hellwig 959a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_hosts_group_ops = { 960a07b4970SChristoph Hellwig .make_group = nvmet_hosts_make_group, 961a07b4970SChristoph Hellwig }; 962a07b4970SChristoph Hellwig 96366603a31SBhumika Goyal static const struct config_item_type nvmet_hosts_type = { 964a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_hosts_group_ops, 965a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 966a07b4970SChristoph Hellwig }; 967a07b4970SChristoph Hellwig 968a07b4970SChristoph Hellwig static struct config_group nvmet_hosts_group; 969a07b4970SChristoph Hellwig 97066603a31SBhumika Goyal static const struct config_item_type nvmet_root_type = { 971a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 972a07b4970SChristoph Hellwig }; 973a07b4970SChristoph Hellwig 974a07b4970SChristoph Hellwig static struct configfs_subsystem nvmet_configfs_subsystem = { 975a07b4970SChristoph Hellwig .su_group = { 976a07b4970SChristoph Hellwig .cg_item = { 977a07b4970SChristoph Hellwig .ci_namebuf = "nvmet", 978a07b4970SChristoph Hellwig .ci_type = &nvmet_root_type, 979a07b4970SChristoph Hellwig }, 980a07b4970SChristoph Hellwig }, 981a07b4970SChristoph Hellwig }; 982a07b4970SChristoph Hellwig 983a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void) 984a07b4970SChristoph Hellwig { 985a07b4970SChristoph Hellwig int ret; 986a07b4970SChristoph Hellwig 987a07b4970SChristoph Hellwig config_group_init(&nvmet_configfs_subsystem.su_group); 988a07b4970SChristoph Hellwig mutex_init(&nvmet_configfs_subsystem.su_mutex); 989a07b4970SChristoph Hellwig 990a07b4970SChristoph Hellwig config_group_init_type_name(&nvmet_subsystems_group, 991a07b4970SChristoph Hellwig "subsystems", &nvmet_subsystems_type); 992a07b4970SChristoph Hellwig configfs_add_default_group(&nvmet_subsystems_group, 993a07b4970SChristoph Hellwig &nvmet_configfs_subsystem.su_group); 994a07b4970SChristoph Hellwig 995a07b4970SChristoph Hellwig config_group_init_type_name(&nvmet_ports_group, 996a07b4970SChristoph Hellwig "ports", &nvmet_ports_type); 997a07b4970SChristoph Hellwig configfs_add_default_group(&nvmet_ports_group, 998a07b4970SChristoph Hellwig &nvmet_configfs_subsystem.su_group); 999a07b4970SChristoph Hellwig 1000a07b4970SChristoph Hellwig config_group_init_type_name(&nvmet_hosts_group, 1001a07b4970SChristoph Hellwig "hosts", &nvmet_hosts_type); 1002a07b4970SChristoph Hellwig configfs_add_default_group(&nvmet_hosts_group, 1003a07b4970SChristoph Hellwig &nvmet_configfs_subsystem.su_group); 1004a07b4970SChristoph Hellwig 1005a07b4970SChristoph Hellwig ret = configfs_register_subsystem(&nvmet_configfs_subsystem); 1006a07b4970SChristoph Hellwig if (ret) { 1007a07b4970SChristoph Hellwig pr_err("configfs_register_subsystem: %d\n", ret); 1008a07b4970SChristoph Hellwig return ret; 1009a07b4970SChristoph Hellwig } 1010a07b4970SChristoph Hellwig 1011a07b4970SChristoph Hellwig return 0; 1012a07b4970SChristoph Hellwig } 1013a07b4970SChristoph Hellwig 1014a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void) 1015a07b4970SChristoph Hellwig { 1016a07b4970SChristoph Hellwig configfs_unregister_subsystem(&nvmet_configfs_subsystem); 1017a07b4970SChristoph Hellwig } 1018