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 } 1409ba2a5cbSSagi Grimberg 1419ba2a5cbSSagi Grimberg if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1) 1429ba2a5cbSSagi Grimberg return -EINVAL; 1439ba2a5cbSSagi Grimberg return count; 144a07b4970SChristoph Hellwig } 145a07b4970SChristoph Hellwig 146a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_traddr); 147a07b4970SChristoph Hellwig 148a07b4970SChristoph Hellwig static ssize_t nvmet_addr_treq_show(struct config_item *item, 149a07b4970SChristoph Hellwig char *page) 150a07b4970SChristoph Hellwig { 151a07b4970SChristoph Hellwig switch (to_nvmet_port(item)->disc_addr.treq) { 152a07b4970SChristoph Hellwig case NVMF_TREQ_NOT_SPECIFIED: 153a07b4970SChristoph Hellwig return sprintf(page, "not specified\n"); 154a07b4970SChristoph Hellwig case NVMF_TREQ_REQUIRED: 155a07b4970SChristoph Hellwig return sprintf(page, "required\n"); 156a07b4970SChristoph Hellwig case NVMF_TREQ_NOT_REQUIRED: 157a07b4970SChristoph Hellwig return sprintf(page, "not required\n"); 158a07b4970SChristoph Hellwig default: 159a07b4970SChristoph Hellwig return sprintf(page, "\n"); 160a07b4970SChristoph Hellwig } 161a07b4970SChristoph Hellwig } 162a07b4970SChristoph Hellwig 163a07b4970SChristoph Hellwig static ssize_t nvmet_addr_treq_store(struct config_item *item, 164a07b4970SChristoph Hellwig const char *page, size_t count) 165a07b4970SChristoph Hellwig { 166a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 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")) { 175a07b4970SChristoph Hellwig port->disc_addr.treq = NVMF_TREQ_NOT_SPECIFIED; 176a07b4970SChristoph Hellwig } else if (sysfs_streq(page, "required")) { 177a07b4970SChristoph Hellwig port->disc_addr.treq = NVMF_TREQ_REQUIRED; 178a07b4970SChristoph Hellwig } else if (sysfs_streq(page, "not required")) { 179a07b4970SChristoph Hellwig port->disc_addr.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 } 184a07b4970SChristoph Hellwig 185a07b4970SChristoph Hellwig return count; 186a07b4970SChristoph Hellwig } 187a07b4970SChristoph Hellwig 188a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_treq); 189a07b4970SChristoph Hellwig 190a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_show(struct config_item *item, 191a07b4970SChristoph Hellwig char *page) 192a07b4970SChristoph Hellwig { 193a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 194a07b4970SChristoph Hellwig 195a07b4970SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%s\n", 196a07b4970SChristoph Hellwig port->disc_addr.trsvcid); 197a07b4970SChristoph Hellwig } 198a07b4970SChristoph Hellwig 199a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_store(struct config_item *item, 200a07b4970SChristoph Hellwig const char *page, size_t count) 201a07b4970SChristoph Hellwig { 202a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 203a07b4970SChristoph Hellwig 204a07b4970SChristoph Hellwig if (count > NVMF_TRSVCID_SIZE) { 205a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for trsvcid\n", page); 206a07b4970SChristoph Hellwig return -EINVAL; 207a07b4970SChristoph Hellwig } 208a07b4970SChristoph Hellwig if (port->enabled) { 209a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 210a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 211a07b4970SChristoph Hellwig return -EACCES; 212a07b4970SChristoph Hellwig } 2139ba2a5cbSSagi Grimberg 2149ba2a5cbSSagi Grimberg if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1) 2159ba2a5cbSSagi Grimberg return -EINVAL; 2169ba2a5cbSSagi Grimberg return count; 217a07b4970SChristoph Hellwig } 218a07b4970SChristoph Hellwig 219a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trsvcid); 220a07b4970SChristoph Hellwig 221a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_show(struct config_item *item, 222a07b4970SChristoph Hellwig char *page) 223a07b4970SChristoph Hellwig { 224a5d18612SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 225a5d18612SChristoph Hellwig int i; 226a5d18612SChristoph Hellwig 227a5d18612SChristoph Hellwig for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) { 228a5d18612SChristoph Hellwig if (port->disc_addr.trtype != nvmet_transport_names[i].type) 229a5d18612SChristoph Hellwig continue; 230a5d18612SChristoph Hellwig return sprintf(page, "%s\n", nvmet_transport_names[i].name); 231a07b4970SChristoph Hellwig } 232a5d18612SChristoph Hellwig 233a5d18612SChristoph Hellwig return sprintf(page, "\n"); 234a07b4970SChristoph Hellwig } 235a07b4970SChristoph Hellwig 236a07b4970SChristoph Hellwig static void nvmet_port_init_tsas_rdma(struct nvmet_port *port) 237a07b4970SChristoph Hellwig { 238a07b4970SChristoph Hellwig port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED; 239a07b4970SChristoph Hellwig port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED; 240a07b4970SChristoph Hellwig port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM; 241a07b4970SChristoph Hellwig } 242a07b4970SChristoph Hellwig 243a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_store(struct config_item *item, 244a07b4970SChristoph Hellwig const char *page, size_t count) 245a07b4970SChristoph Hellwig { 246a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 247a5d18612SChristoph Hellwig int i; 248a07b4970SChristoph Hellwig 249a07b4970SChristoph Hellwig if (port->enabled) { 250a07b4970SChristoph Hellwig pr_err("Cannot modify address while enabled\n"); 251a07b4970SChristoph Hellwig pr_err("Disable the address before modifying\n"); 252a07b4970SChristoph Hellwig return -EACCES; 253a07b4970SChristoph Hellwig } 254a07b4970SChristoph Hellwig 255a5d18612SChristoph Hellwig for (i = 0; i < ARRAY_SIZE(nvmet_transport_names); i++) { 256a5d18612SChristoph Hellwig if (sysfs_streq(page, nvmet_transport_names[i].name)) 257a5d18612SChristoph Hellwig goto found; 258a07b4970SChristoph Hellwig } 259a07b4970SChristoph Hellwig 260a5d18612SChristoph Hellwig pr_err("Invalid value '%s' for trtype\n", page); 261a5d18612SChristoph Hellwig return -EINVAL; 262a5d18612SChristoph Hellwig found: 263a5d18612SChristoph Hellwig memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE); 264a5d18612SChristoph Hellwig port->disc_addr.trtype = nvmet_transport_names[i].type; 265a5d18612SChristoph Hellwig if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA) 266a5d18612SChristoph Hellwig nvmet_port_init_tsas_rdma(port); 267a07b4970SChristoph Hellwig return count; 268a07b4970SChristoph Hellwig } 269a07b4970SChristoph Hellwig 270a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trtype); 271a07b4970SChristoph Hellwig 272a07b4970SChristoph Hellwig /* 273a07b4970SChristoph Hellwig * Namespace structures & file operation functions below 274a07b4970SChristoph Hellwig */ 275a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page) 276a07b4970SChristoph Hellwig { 277a07b4970SChristoph Hellwig return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path); 278a07b4970SChristoph Hellwig } 279a07b4970SChristoph Hellwig 280a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_store(struct config_item *item, 281a07b4970SChristoph Hellwig const char *page, size_t count) 282a07b4970SChristoph Hellwig { 283a07b4970SChristoph Hellwig struct nvmet_ns *ns = to_nvmet_ns(item); 284a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = ns->subsys; 285a07b4970SChristoph Hellwig int ret; 286a07b4970SChristoph Hellwig 287a07b4970SChristoph Hellwig mutex_lock(&subsys->lock); 288a07b4970SChristoph Hellwig ret = -EBUSY; 289e4fcf07cSSolganik Alexander if (ns->enabled) 290a07b4970SChristoph Hellwig goto out_unlock; 291a07b4970SChristoph Hellwig 292a07b4970SChristoph Hellwig kfree(ns->device_path); 293a07b4970SChristoph Hellwig 294a07b4970SChristoph Hellwig ret = -ENOMEM; 2959ba2a5cbSSagi Grimberg ns->device_path = kstrndup(page, strcspn(page, "\n"), GFP_KERNEL); 296a07b4970SChristoph Hellwig if (!ns->device_path) 297a07b4970SChristoph Hellwig goto out_unlock; 298a07b4970SChristoph Hellwig 299a07b4970SChristoph Hellwig mutex_unlock(&subsys->lock); 300a07b4970SChristoph Hellwig return count; 301a07b4970SChristoph Hellwig 302a07b4970SChristoph Hellwig out_unlock: 303a07b4970SChristoph Hellwig mutex_unlock(&subsys->lock); 304a07b4970SChristoph Hellwig return ret; 305a07b4970SChristoph Hellwig } 306a07b4970SChristoph Hellwig 307a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_path); 308a07b4970SChristoph Hellwig 309430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page) 310430c7befSJohannes Thumshirn { 311430c7befSJohannes Thumshirn return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid); 312430c7befSJohannes Thumshirn } 313430c7befSJohannes Thumshirn 314430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_store(struct config_item *item, 315430c7befSJohannes Thumshirn const char *page, size_t count) 316430c7befSJohannes Thumshirn { 317430c7befSJohannes Thumshirn struct nvmet_ns *ns = to_nvmet_ns(item); 318430c7befSJohannes Thumshirn struct nvmet_subsys *subsys = ns->subsys; 319430c7befSJohannes Thumshirn int ret = 0; 320430c7befSJohannes Thumshirn 321430c7befSJohannes Thumshirn 322430c7befSJohannes Thumshirn mutex_lock(&subsys->lock); 323430c7befSJohannes Thumshirn if (ns->enabled) { 324430c7befSJohannes Thumshirn ret = -EBUSY; 325430c7befSJohannes Thumshirn goto out_unlock; 326430c7befSJohannes Thumshirn } 327430c7befSJohannes Thumshirn 328430c7befSJohannes Thumshirn 329430c7befSJohannes Thumshirn if (uuid_parse(page, &ns->uuid)) 330430c7befSJohannes Thumshirn ret = -EINVAL; 331430c7befSJohannes Thumshirn 332430c7befSJohannes Thumshirn out_unlock: 333430c7befSJohannes Thumshirn mutex_unlock(&subsys->lock); 334430c7befSJohannes Thumshirn return ret ? ret : count; 335430c7befSJohannes Thumshirn } 336430c7befSJohannes Thumshirn 337f871749aSMax Gurtovoy CONFIGFS_ATTR(nvmet_ns_, device_uuid); 338f871749aSMax Gurtovoy 339a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page) 340a07b4970SChristoph Hellwig { 341a07b4970SChristoph Hellwig return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid); 342a07b4970SChristoph Hellwig } 343a07b4970SChristoph Hellwig 344a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_store(struct config_item *item, 345a07b4970SChristoph Hellwig const char *page, size_t count) 346a07b4970SChristoph Hellwig { 347a07b4970SChristoph Hellwig struct nvmet_ns *ns = to_nvmet_ns(item); 348a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = ns->subsys; 349a07b4970SChristoph Hellwig u8 nguid[16]; 350a07b4970SChristoph Hellwig const char *p = page; 351a07b4970SChristoph Hellwig int i; 352a07b4970SChristoph Hellwig int ret = 0; 353a07b4970SChristoph Hellwig 354a07b4970SChristoph Hellwig mutex_lock(&subsys->lock); 355e4fcf07cSSolganik Alexander if (ns->enabled) { 356a07b4970SChristoph Hellwig ret = -EBUSY; 357a07b4970SChristoph Hellwig goto out_unlock; 358a07b4970SChristoph Hellwig } 359a07b4970SChristoph Hellwig 360a07b4970SChristoph Hellwig for (i = 0; i < 16; i++) { 361a07b4970SChristoph Hellwig if (p + 2 > page + count) { 362a07b4970SChristoph Hellwig ret = -EINVAL; 363a07b4970SChristoph Hellwig goto out_unlock; 364a07b4970SChristoph Hellwig } 365a07b4970SChristoph Hellwig if (!isxdigit(p[0]) || !isxdigit(p[1])) { 366a07b4970SChristoph Hellwig ret = -EINVAL; 367a07b4970SChristoph Hellwig goto out_unlock; 368a07b4970SChristoph Hellwig } 369a07b4970SChristoph Hellwig 370a07b4970SChristoph Hellwig nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]); 371a07b4970SChristoph Hellwig p += 2; 372a07b4970SChristoph Hellwig 373a07b4970SChristoph Hellwig if (*p == '-' || *p == ':') 374a07b4970SChristoph Hellwig p++; 375a07b4970SChristoph Hellwig } 376a07b4970SChristoph Hellwig 377a07b4970SChristoph Hellwig memcpy(&ns->nguid, nguid, sizeof(nguid)); 378a07b4970SChristoph Hellwig out_unlock: 379a07b4970SChristoph Hellwig mutex_unlock(&subsys->lock); 380a07b4970SChristoph Hellwig return ret ? ret : count; 381a07b4970SChristoph Hellwig } 382a07b4970SChristoph Hellwig 383a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_nguid); 384a07b4970SChristoph Hellwig 385a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page) 386a07b4970SChristoph Hellwig { 387e4fcf07cSSolganik Alexander return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled); 388a07b4970SChristoph Hellwig } 389a07b4970SChristoph Hellwig 390a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_store(struct config_item *item, 391a07b4970SChristoph Hellwig const char *page, size_t count) 392a07b4970SChristoph Hellwig { 393a07b4970SChristoph Hellwig struct nvmet_ns *ns = to_nvmet_ns(item); 394a07b4970SChristoph Hellwig bool enable; 395a07b4970SChristoph Hellwig int ret = 0; 396a07b4970SChristoph Hellwig 397a07b4970SChristoph Hellwig if (strtobool(page, &enable)) 398a07b4970SChristoph Hellwig return -EINVAL; 399a07b4970SChristoph Hellwig 400a07b4970SChristoph Hellwig if (enable) 401a07b4970SChristoph Hellwig ret = nvmet_ns_enable(ns); 402a07b4970SChristoph Hellwig else 403a07b4970SChristoph Hellwig nvmet_ns_disable(ns); 404a07b4970SChristoph Hellwig 405a07b4970SChristoph Hellwig return ret ? ret : count; 406a07b4970SChristoph Hellwig } 407a07b4970SChristoph Hellwig 408a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, enable); 409a07b4970SChristoph Hellwig 41055eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page) 41155eb942eSChaitanya Kulkarni { 41255eb942eSChaitanya Kulkarni return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io); 41355eb942eSChaitanya Kulkarni } 41455eb942eSChaitanya Kulkarni 41555eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_store(struct config_item *item, 41655eb942eSChaitanya Kulkarni const char *page, size_t count) 41755eb942eSChaitanya Kulkarni { 41855eb942eSChaitanya Kulkarni struct nvmet_ns *ns = to_nvmet_ns(item); 41955eb942eSChaitanya Kulkarni bool val; 42055eb942eSChaitanya Kulkarni 42155eb942eSChaitanya Kulkarni if (strtobool(page, &val)) 42255eb942eSChaitanya Kulkarni return -EINVAL; 42355eb942eSChaitanya Kulkarni 42455eb942eSChaitanya Kulkarni mutex_lock(&ns->subsys->lock); 42555eb942eSChaitanya Kulkarni if (ns->enabled) { 42655eb942eSChaitanya Kulkarni pr_err("disable ns before setting buffered_io value.\n"); 42755eb942eSChaitanya Kulkarni mutex_unlock(&ns->subsys->lock); 42855eb942eSChaitanya Kulkarni return -EINVAL; 42955eb942eSChaitanya Kulkarni } 43055eb942eSChaitanya Kulkarni 43155eb942eSChaitanya Kulkarni ns->buffered_io = val; 43255eb942eSChaitanya Kulkarni mutex_unlock(&ns->subsys->lock); 43355eb942eSChaitanya Kulkarni return count; 43455eb942eSChaitanya Kulkarni } 43555eb942eSChaitanya Kulkarni 43655eb942eSChaitanya Kulkarni CONFIGFS_ATTR(nvmet_ns_, buffered_io); 43755eb942eSChaitanya Kulkarni 438a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_ns_attrs[] = { 439a07b4970SChristoph Hellwig &nvmet_ns_attr_device_path, 440a07b4970SChristoph Hellwig &nvmet_ns_attr_device_nguid, 441430c7befSJohannes Thumshirn &nvmet_ns_attr_device_uuid, 442a07b4970SChristoph Hellwig &nvmet_ns_attr_enable, 44355eb942eSChaitanya Kulkarni &nvmet_ns_attr_buffered_io, 444a07b4970SChristoph Hellwig NULL, 445a07b4970SChristoph Hellwig }; 446a07b4970SChristoph Hellwig 447a07b4970SChristoph Hellwig static void nvmet_ns_release(struct config_item *item) 448a07b4970SChristoph Hellwig { 449a07b4970SChristoph Hellwig struct nvmet_ns *ns = to_nvmet_ns(item); 450a07b4970SChristoph Hellwig 451a07b4970SChristoph Hellwig nvmet_ns_free(ns); 452a07b4970SChristoph Hellwig } 453a07b4970SChristoph Hellwig 454a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_ns_item_ops = { 455a07b4970SChristoph Hellwig .release = nvmet_ns_release, 456a07b4970SChristoph Hellwig }; 457a07b4970SChristoph Hellwig 45866603a31SBhumika Goyal static const struct config_item_type nvmet_ns_type = { 459a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_ns_item_ops, 460a07b4970SChristoph Hellwig .ct_attrs = nvmet_ns_attrs, 461a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 462a07b4970SChristoph Hellwig }; 463a07b4970SChristoph Hellwig 464a07b4970SChristoph Hellwig static struct config_group *nvmet_ns_make(struct config_group *group, 465a07b4970SChristoph Hellwig const char *name) 466a07b4970SChristoph Hellwig { 467a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item); 468a07b4970SChristoph Hellwig struct nvmet_ns *ns; 469a07b4970SChristoph Hellwig int ret; 470a07b4970SChristoph Hellwig u32 nsid; 471a07b4970SChristoph Hellwig 472a07b4970SChristoph Hellwig ret = kstrtou32(name, 0, &nsid); 473a07b4970SChristoph Hellwig if (ret) 474a07b4970SChristoph Hellwig goto out; 475a07b4970SChristoph Hellwig 476a07b4970SChristoph Hellwig ret = -EINVAL; 4771645d503SChristoph Hellwig if (nsid == 0 || nsid == NVME_NSID_ALL) 478a07b4970SChristoph Hellwig goto out; 479a07b4970SChristoph Hellwig 480a07b4970SChristoph Hellwig ret = -ENOMEM; 481a07b4970SChristoph Hellwig ns = nvmet_ns_alloc(subsys, nsid); 482a07b4970SChristoph Hellwig if (!ns) 483a07b4970SChristoph Hellwig goto out; 484a07b4970SChristoph Hellwig config_group_init_type_name(&ns->group, name, &nvmet_ns_type); 485a07b4970SChristoph Hellwig 486a07b4970SChristoph Hellwig pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn); 487a07b4970SChristoph Hellwig 488a07b4970SChristoph Hellwig return &ns->group; 489a07b4970SChristoph Hellwig out: 490a07b4970SChristoph Hellwig return ERR_PTR(ret); 491a07b4970SChristoph Hellwig } 492a07b4970SChristoph Hellwig 493a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_namespaces_group_ops = { 494a07b4970SChristoph Hellwig .make_group = nvmet_ns_make, 495a07b4970SChristoph Hellwig }; 496a07b4970SChristoph Hellwig 49766603a31SBhumika Goyal static const struct config_item_type nvmet_namespaces_type = { 498a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_namespaces_group_ops, 499a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 500a07b4970SChristoph Hellwig }; 501a07b4970SChristoph Hellwig 502a07b4970SChristoph Hellwig static int nvmet_port_subsys_allow_link(struct config_item *parent, 503a07b4970SChristoph Hellwig struct config_item *target) 504a07b4970SChristoph Hellwig { 505a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(parent->ci_parent); 506a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 507a07b4970SChristoph Hellwig struct nvmet_subsys_link *link, *p; 508a07b4970SChristoph Hellwig int ret; 509a07b4970SChristoph Hellwig 510a07b4970SChristoph Hellwig if (target->ci_type != &nvmet_subsys_type) { 511a07b4970SChristoph Hellwig pr_err("can only link subsystems into the subsystems dir.!\n"); 512a07b4970SChristoph Hellwig return -EINVAL; 513a07b4970SChristoph Hellwig } 514a07b4970SChristoph Hellwig subsys = to_subsys(target); 515a07b4970SChristoph Hellwig link = kmalloc(sizeof(*link), GFP_KERNEL); 516a07b4970SChristoph Hellwig if (!link) 517a07b4970SChristoph Hellwig return -ENOMEM; 518a07b4970SChristoph Hellwig link->subsys = subsys; 519a07b4970SChristoph Hellwig 520a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 521a07b4970SChristoph Hellwig ret = -EEXIST; 522a07b4970SChristoph Hellwig list_for_each_entry(p, &port->subsystems, entry) { 523a07b4970SChristoph Hellwig if (p->subsys == subsys) 524a07b4970SChristoph Hellwig goto out_free_link; 525a07b4970SChristoph Hellwig } 526a07b4970SChristoph Hellwig 527a07b4970SChristoph Hellwig if (list_empty(&port->subsystems)) { 528a07b4970SChristoph Hellwig ret = nvmet_enable_port(port); 529a07b4970SChristoph Hellwig if (ret) 530a07b4970SChristoph Hellwig goto out_free_link; 531a07b4970SChristoph Hellwig } 532a07b4970SChristoph Hellwig 533a07b4970SChristoph Hellwig list_add_tail(&link->entry, &port->subsystems); 534a07b4970SChristoph Hellwig nvmet_genctr++; 535a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 536a07b4970SChristoph Hellwig return 0; 537a07b4970SChristoph Hellwig 538a07b4970SChristoph Hellwig out_free_link: 539a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 540a07b4970SChristoph Hellwig kfree(link); 541a07b4970SChristoph Hellwig return ret; 542a07b4970SChristoph Hellwig } 543a07b4970SChristoph Hellwig 544e16769d4SAndrzej Pietrasiewicz static void nvmet_port_subsys_drop_link(struct config_item *parent, 545a07b4970SChristoph Hellwig struct config_item *target) 546a07b4970SChristoph Hellwig { 547a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(parent->ci_parent); 548a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(target); 549a07b4970SChristoph Hellwig struct nvmet_subsys_link *p; 550a07b4970SChristoph Hellwig 551a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 552a07b4970SChristoph Hellwig list_for_each_entry(p, &port->subsystems, entry) { 553a07b4970SChristoph Hellwig if (p->subsys == subsys) 554a07b4970SChristoph Hellwig goto found; 555a07b4970SChristoph Hellwig } 556a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 557e16769d4SAndrzej Pietrasiewicz return; 558a07b4970SChristoph Hellwig 559a07b4970SChristoph Hellwig found: 560a07b4970SChristoph Hellwig list_del(&p->entry); 561a07b4970SChristoph Hellwig nvmet_genctr++; 562a07b4970SChristoph Hellwig if (list_empty(&port->subsystems)) 563a07b4970SChristoph Hellwig nvmet_disable_port(port); 564a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 565a07b4970SChristoph Hellwig kfree(p); 566a07b4970SChristoph Hellwig } 567a07b4970SChristoph Hellwig 568a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_subsys_item_ops = { 569a07b4970SChristoph Hellwig .allow_link = nvmet_port_subsys_allow_link, 570a07b4970SChristoph Hellwig .drop_link = nvmet_port_subsys_drop_link, 571a07b4970SChristoph Hellwig }; 572a07b4970SChristoph Hellwig 57366603a31SBhumika Goyal static const struct config_item_type nvmet_port_subsys_type = { 574a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_port_subsys_item_ops, 575a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 576a07b4970SChristoph Hellwig }; 577a07b4970SChristoph Hellwig 578a07b4970SChristoph Hellwig static int nvmet_allowed_hosts_allow_link(struct config_item *parent, 579a07b4970SChristoph Hellwig struct config_item *target) 580a07b4970SChristoph Hellwig { 581a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(parent->ci_parent); 582a07b4970SChristoph Hellwig struct nvmet_host *host; 583a07b4970SChristoph Hellwig struct nvmet_host_link *link, *p; 584a07b4970SChristoph Hellwig int ret; 585a07b4970SChristoph Hellwig 586a07b4970SChristoph Hellwig if (target->ci_type != &nvmet_host_type) { 587a07b4970SChristoph Hellwig pr_err("can only link hosts into the allowed_hosts directory!\n"); 588a07b4970SChristoph Hellwig return -EINVAL; 589a07b4970SChristoph Hellwig } 590a07b4970SChristoph Hellwig 591a07b4970SChristoph Hellwig host = to_host(target); 592a07b4970SChristoph Hellwig link = kmalloc(sizeof(*link), GFP_KERNEL); 593a07b4970SChristoph Hellwig if (!link) 594a07b4970SChristoph Hellwig return -ENOMEM; 595a07b4970SChristoph Hellwig link->host = host; 596a07b4970SChristoph Hellwig 597a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 598a07b4970SChristoph Hellwig ret = -EINVAL; 599a07b4970SChristoph Hellwig if (subsys->allow_any_host) { 600a07b4970SChristoph Hellwig pr_err("can't add hosts when allow_any_host is set!\n"); 601a07b4970SChristoph Hellwig goto out_free_link; 602a07b4970SChristoph Hellwig } 603a07b4970SChristoph Hellwig 604a07b4970SChristoph Hellwig ret = -EEXIST; 605a07b4970SChristoph Hellwig list_for_each_entry(p, &subsys->hosts, entry) { 606a07b4970SChristoph Hellwig if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host))) 607a07b4970SChristoph Hellwig goto out_free_link; 608a07b4970SChristoph Hellwig } 609a07b4970SChristoph Hellwig list_add_tail(&link->entry, &subsys->hosts); 610a07b4970SChristoph Hellwig nvmet_genctr++; 611a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 612a07b4970SChristoph Hellwig return 0; 613a07b4970SChristoph Hellwig out_free_link: 614a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 615a07b4970SChristoph Hellwig kfree(link); 616a07b4970SChristoph Hellwig return ret; 617a07b4970SChristoph Hellwig } 618a07b4970SChristoph Hellwig 619e16769d4SAndrzej Pietrasiewicz static void nvmet_allowed_hosts_drop_link(struct config_item *parent, 620a07b4970SChristoph Hellwig struct config_item *target) 621a07b4970SChristoph Hellwig { 622a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(parent->ci_parent); 623a07b4970SChristoph Hellwig struct nvmet_host *host = to_host(target); 624a07b4970SChristoph Hellwig struct nvmet_host_link *p; 625a07b4970SChristoph Hellwig 626a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 627a07b4970SChristoph Hellwig list_for_each_entry(p, &subsys->hosts, entry) { 628a07b4970SChristoph Hellwig if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host))) 629a07b4970SChristoph Hellwig goto found; 630a07b4970SChristoph Hellwig } 631a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 632e16769d4SAndrzej Pietrasiewicz return; 633a07b4970SChristoph Hellwig 634a07b4970SChristoph Hellwig found: 635a07b4970SChristoph Hellwig list_del(&p->entry); 636a07b4970SChristoph Hellwig nvmet_genctr++; 637a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 638a07b4970SChristoph Hellwig kfree(p); 639a07b4970SChristoph Hellwig } 640a07b4970SChristoph Hellwig 641a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_allowed_hosts_item_ops = { 642a07b4970SChristoph Hellwig .allow_link = nvmet_allowed_hosts_allow_link, 643a07b4970SChristoph Hellwig .drop_link = nvmet_allowed_hosts_drop_link, 644a07b4970SChristoph Hellwig }; 645a07b4970SChristoph Hellwig 64666603a31SBhumika Goyal static const struct config_item_type nvmet_allowed_hosts_type = { 647a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_allowed_hosts_item_ops, 648a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 649a07b4970SChristoph Hellwig }; 650a07b4970SChristoph Hellwig 651a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item, 652a07b4970SChristoph Hellwig char *page) 653a07b4970SChristoph Hellwig { 654a07b4970SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%d\n", 655a07b4970SChristoph Hellwig to_subsys(item)->allow_any_host); 656a07b4970SChristoph Hellwig } 657a07b4970SChristoph Hellwig 658a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item, 659a07b4970SChristoph Hellwig const char *page, size_t count) 660a07b4970SChristoph Hellwig { 661a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(item); 662a07b4970SChristoph Hellwig bool allow_any_host; 663a07b4970SChristoph Hellwig int ret = 0; 664a07b4970SChristoph Hellwig 665a07b4970SChristoph Hellwig if (strtobool(page, &allow_any_host)) 666a07b4970SChristoph Hellwig return -EINVAL; 667a07b4970SChristoph Hellwig 668a07b4970SChristoph Hellwig down_write(&nvmet_config_sem); 669a07b4970SChristoph Hellwig if (allow_any_host && !list_empty(&subsys->hosts)) { 670a07b4970SChristoph Hellwig pr_err("Can't set allow_any_host when explicit hosts are set!\n"); 671a07b4970SChristoph Hellwig ret = -EINVAL; 672a07b4970SChristoph Hellwig goto out_unlock; 673a07b4970SChristoph Hellwig } 674a07b4970SChristoph Hellwig 675a07b4970SChristoph Hellwig subsys->allow_any_host = allow_any_host; 676a07b4970SChristoph Hellwig out_unlock: 677a07b4970SChristoph Hellwig up_write(&nvmet_config_sem); 678a07b4970SChristoph Hellwig return ret ? ret : count; 679a07b4970SChristoph Hellwig } 680a07b4970SChristoph Hellwig 681a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host); 682a07b4970SChristoph Hellwig 68341528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_show(struct config_item *item, 684c61d788bSJohannes Thumshirn char *page) 685c61d788bSJohannes Thumshirn { 686c61d788bSJohannes Thumshirn struct nvmet_subsys *subsys = to_subsys(item); 687c61d788bSJohannes Thumshirn 688c61d788bSJohannes Thumshirn if (NVME_TERTIARY(subsys->ver)) 689c61d788bSJohannes Thumshirn return snprintf(page, PAGE_SIZE, "%d.%d.%d\n", 690c61d788bSJohannes Thumshirn (int)NVME_MAJOR(subsys->ver), 691c61d788bSJohannes Thumshirn (int)NVME_MINOR(subsys->ver), 692c61d788bSJohannes Thumshirn (int)NVME_TERTIARY(subsys->ver)); 693c61d788bSJohannes Thumshirn else 694c61d788bSJohannes Thumshirn return snprintf(page, PAGE_SIZE, "%d.%d\n", 695c61d788bSJohannes Thumshirn (int)NVME_MAJOR(subsys->ver), 696c61d788bSJohannes Thumshirn (int)NVME_MINOR(subsys->ver)); 697c61d788bSJohannes Thumshirn } 698c61d788bSJohannes Thumshirn 69941528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, 700c61d788bSJohannes Thumshirn const char *page, size_t count) 701c61d788bSJohannes Thumshirn { 702c61d788bSJohannes Thumshirn struct nvmet_subsys *subsys = to_subsys(item); 703c61d788bSJohannes Thumshirn int major, minor, tertiary = 0; 704c61d788bSJohannes Thumshirn int ret; 705c61d788bSJohannes Thumshirn 706c61d788bSJohannes Thumshirn 707c61d788bSJohannes Thumshirn ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary); 708c61d788bSJohannes Thumshirn if (ret != 2 && ret != 3) 709c61d788bSJohannes Thumshirn return -EINVAL; 710c61d788bSJohannes Thumshirn 711c61d788bSJohannes Thumshirn down_write(&nvmet_config_sem); 712c61d788bSJohannes Thumshirn subsys->ver = NVME_VS(major, minor, tertiary); 713c61d788bSJohannes Thumshirn up_write(&nvmet_config_sem); 714c61d788bSJohannes Thumshirn 715c61d788bSJohannes Thumshirn return count; 716c61d788bSJohannes Thumshirn } 71741528f80SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_version); 718c61d788bSJohannes Thumshirn 719fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item, 720fcbc5459SJohannes Thumshirn char *page) 721fcbc5459SJohannes Thumshirn { 722fcbc5459SJohannes Thumshirn struct nvmet_subsys *subsys = to_subsys(item); 723fcbc5459SJohannes Thumshirn 724fcbc5459SJohannes Thumshirn return snprintf(page, PAGE_SIZE, "%llx\n", subsys->serial); 725fcbc5459SJohannes Thumshirn } 726fcbc5459SJohannes Thumshirn 727fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item, 728fcbc5459SJohannes Thumshirn const char *page, size_t count) 729fcbc5459SJohannes Thumshirn { 730fcbc5459SJohannes Thumshirn struct nvmet_subsys *subsys = to_subsys(item); 731fcbc5459SJohannes Thumshirn 732fcbc5459SJohannes Thumshirn down_write(&nvmet_config_sem); 733fcbc5459SJohannes Thumshirn sscanf(page, "%llx\n", &subsys->serial); 734fcbc5459SJohannes Thumshirn up_write(&nvmet_config_sem); 735fcbc5459SJohannes Thumshirn 736fcbc5459SJohannes Thumshirn return count; 737fcbc5459SJohannes Thumshirn } 738fcbc5459SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_serial); 739fcbc5459SJohannes Thumshirn 740a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_subsys_attrs[] = { 741a07b4970SChristoph Hellwig &nvmet_subsys_attr_attr_allow_any_host, 74241528f80SJohannes Thumshirn &nvmet_subsys_attr_attr_version, 743fcbc5459SJohannes Thumshirn &nvmet_subsys_attr_attr_serial, 744a07b4970SChristoph Hellwig NULL, 745a07b4970SChristoph Hellwig }; 746a07b4970SChristoph Hellwig 747a07b4970SChristoph Hellwig /* 748a07b4970SChristoph Hellwig * Subsystem structures & folder operation functions below 749a07b4970SChristoph Hellwig */ 750a07b4970SChristoph Hellwig static void nvmet_subsys_release(struct config_item *item) 751a07b4970SChristoph Hellwig { 752a07b4970SChristoph Hellwig struct nvmet_subsys *subsys = to_subsys(item); 753a07b4970SChristoph Hellwig 754344770b0SSagi Grimberg nvmet_subsys_del_ctrls(subsys); 755a07b4970SChristoph Hellwig nvmet_subsys_put(subsys); 756a07b4970SChristoph Hellwig } 757a07b4970SChristoph Hellwig 758a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_subsys_item_ops = { 759a07b4970SChristoph Hellwig .release = nvmet_subsys_release, 760a07b4970SChristoph Hellwig }; 761a07b4970SChristoph Hellwig 76266603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type = { 763a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_subsys_item_ops, 764a07b4970SChristoph Hellwig .ct_attrs = nvmet_subsys_attrs, 765a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 766a07b4970SChristoph Hellwig }; 767a07b4970SChristoph Hellwig 768a07b4970SChristoph Hellwig static struct config_group *nvmet_subsys_make(struct config_group *group, 769a07b4970SChristoph Hellwig const char *name) 770a07b4970SChristoph Hellwig { 771a07b4970SChristoph Hellwig struct nvmet_subsys *subsys; 772a07b4970SChristoph Hellwig 773a07b4970SChristoph Hellwig if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) { 774a07b4970SChristoph Hellwig pr_err("can't create discovery subsystem through configfs\n"); 775a07b4970SChristoph Hellwig return ERR_PTR(-EINVAL); 776a07b4970SChristoph Hellwig } 777a07b4970SChristoph Hellwig 778a07b4970SChristoph Hellwig subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME); 779a07b4970SChristoph Hellwig if (!subsys) 780a07b4970SChristoph Hellwig return ERR_PTR(-ENOMEM); 781a07b4970SChristoph Hellwig 782a07b4970SChristoph Hellwig config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type); 783a07b4970SChristoph Hellwig 784a07b4970SChristoph Hellwig config_group_init_type_name(&subsys->namespaces_group, 785a07b4970SChristoph Hellwig "namespaces", &nvmet_namespaces_type); 786a07b4970SChristoph Hellwig configfs_add_default_group(&subsys->namespaces_group, &subsys->group); 787a07b4970SChristoph Hellwig 788a07b4970SChristoph Hellwig config_group_init_type_name(&subsys->allowed_hosts_group, 789a07b4970SChristoph Hellwig "allowed_hosts", &nvmet_allowed_hosts_type); 790a07b4970SChristoph Hellwig configfs_add_default_group(&subsys->allowed_hosts_group, 791a07b4970SChristoph Hellwig &subsys->group); 792a07b4970SChristoph Hellwig 793a07b4970SChristoph Hellwig return &subsys->group; 794a07b4970SChristoph Hellwig } 795a07b4970SChristoph Hellwig 796a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_subsystems_group_ops = { 797a07b4970SChristoph Hellwig .make_group = nvmet_subsys_make, 798a07b4970SChristoph Hellwig }; 799a07b4970SChristoph Hellwig 80066603a31SBhumika Goyal static const struct config_item_type nvmet_subsystems_type = { 801a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_subsystems_group_ops, 802a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 803a07b4970SChristoph Hellwig }; 804a07b4970SChristoph Hellwig 805a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_show(struct config_item *item, 806a07b4970SChristoph Hellwig char *page) 807a07b4970SChristoph Hellwig { 808a07b4970SChristoph Hellwig return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled); 809a07b4970SChristoph Hellwig } 810a07b4970SChristoph Hellwig 811a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_store(struct config_item *item, 812a07b4970SChristoph Hellwig const char *page, size_t count) 813a07b4970SChristoph Hellwig { 814a07b4970SChristoph Hellwig struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent); 815a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 816a07b4970SChristoph Hellwig bool enable; 817a07b4970SChristoph Hellwig 818a07b4970SChristoph Hellwig if (strtobool(page, &enable)) 819a07b4970SChristoph Hellwig goto inval; 820a07b4970SChristoph Hellwig 821a07b4970SChristoph Hellwig if (enable) 822a07b4970SChristoph Hellwig nvmet_referral_enable(parent, port); 823a07b4970SChristoph Hellwig else 824a07b4970SChristoph Hellwig nvmet_referral_disable(port); 825a07b4970SChristoph Hellwig 826a07b4970SChristoph Hellwig return count; 827a07b4970SChristoph Hellwig inval: 828a07b4970SChristoph Hellwig pr_err("Invalid value '%s' for enable\n", page); 829a07b4970SChristoph Hellwig return -EINVAL; 830a07b4970SChristoph Hellwig } 831a07b4970SChristoph Hellwig 832a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_referral_, enable); 833a07b4970SChristoph Hellwig 834a07b4970SChristoph Hellwig /* 835a07b4970SChristoph Hellwig * Discovery Service subsystem definitions 836a07b4970SChristoph Hellwig */ 837a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_referral_attrs[] = { 838a07b4970SChristoph Hellwig &nvmet_attr_addr_adrfam, 839a07b4970SChristoph Hellwig &nvmet_attr_addr_portid, 840a07b4970SChristoph Hellwig &nvmet_attr_addr_treq, 841a07b4970SChristoph Hellwig &nvmet_attr_addr_traddr, 842a07b4970SChristoph Hellwig &nvmet_attr_addr_trsvcid, 843a07b4970SChristoph Hellwig &nvmet_attr_addr_trtype, 844a07b4970SChristoph Hellwig &nvmet_referral_attr_enable, 845a07b4970SChristoph Hellwig NULL, 846a07b4970SChristoph Hellwig }; 847a07b4970SChristoph Hellwig 848a07b4970SChristoph Hellwig static void nvmet_referral_release(struct config_item *item) 849a07b4970SChristoph Hellwig { 850a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 851a07b4970SChristoph Hellwig 852a07b4970SChristoph Hellwig nvmet_referral_disable(port); 853a07b4970SChristoph Hellwig kfree(port); 854a07b4970SChristoph Hellwig } 855a07b4970SChristoph Hellwig 856a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_referral_item_ops = { 857a07b4970SChristoph Hellwig .release = nvmet_referral_release, 858a07b4970SChristoph Hellwig }; 859a07b4970SChristoph Hellwig 86066603a31SBhumika Goyal static const struct config_item_type nvmet_referral_type = { 861a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 862a07b4970SChristoph Hellwig .ct_attrs = nvmet_referral_attrs, 863a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_referral_item_ops, 864a07b4970SChristoph Hellwig }; 865a07b4970SChristoph Hellwig 866a07b4970SChristoph Hellwig static struct config_group *nvmet_referral_make( 867a07b4970SChristoph Hellwig struct config_group *group, const char *name) 868a07b4970SChristoph Hellwig { 869a07b4970SChristoph Hellwig struct nvmet_port *port; 870a07b4970SChristoph Hellwig 871a07b4970SChristoph Hellwig port = kzalloc(sizeof(*port), GFP_KERNEL); 872a07b4970SChristoph Hellwig if (!port) 873f98d9ca1SDan Carpenter return ERR_PTR(-ENOMEM); 874a07b4970SChristoph Hellwig 875a07b4970SChristoph Hellwig INIT_LIST_HEAD(&port->entry); 876a07b4970SChristoph Hellwig config_group_init_type_name(&port->group, name, &nvmet_referral_type); 877a07b4970SChristoph Hellwig 878a07b4970SChristoph Hellwig return &port->group; 879a07b4970SChristoph Hellwig } 880a07b4970SChristoph Hellwig 881a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_referral_group_ops = { 882a07b4970SChristoph Hellwig .make_group = nvmet_referral_make, 883a07b4970SChristoph Hellwig }; 884a07b4970SChristoph Hellwig 88566603a31SBhumika Goyal static const struct config_item_type nvmet_referrals_type = { 886a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 887a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_referral_group_ops, 888a07b4970SChristoph Hellwig }; 889a07b4970SChristoph Hellwig 890a07b4970SChristoph Hellwig /* 891a07b4970SChristoph Hellwig * Ports definitions. 892a07b4970SChristoph Hellwig */ 893a07b4970SChristoph Hellwig static void nvmet_port_release(struct config_item *item) 894a07b4970SChristoph Hellwig { 895a07b4970SChristoph Hellwig struct nvmet_port *port = to_nvmet_port(item); 896a07b4970SChristoph Hellwig 897a07b4970SChristoph Hellwig kfree(port); 898a07b4970SChristoph Hellwig } 899a07b4970SChristoph Hellwig 900a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_port_attrs[] = { 901a07b4970SChristoph Hellwig &nvmet_attr_addr_adrfam, 902a07b4970SChristoph Hellwig &nvmet_attr_addr_treq, 903a07b4970SChristoph Hellwig &nvmet_attr_addr_traddr, 904a07b4970SChristoph Hellwig &nvmet_attr_addr_trsvcid, 905a07b4970SChristoph Hellwig &nvmet_attr_addr_trtype, 906a07b4970SChristoph Hellwig NULL, 907a07b4970SChristoph Hellwig }; 908a07b4970SChristoph Hellwig 909a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_item_ops = { 910a07b4970SChristoph Hellwig .release = nvmet_port_release, 911a07b4970SChristoph Hellwig }; 912a07b4970SChristoph Hellwig 91366603a31SBhumika Goyal static const struct config_item_type nvmet_port_type = { 914a07b4970SChristoph Hellwig .ct_attrs = nvmet_port_attrs, 915a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_port_item_ops, 916a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 917a07b4970SChristoph Hellwig }; 918a07b4970SChristoph Hellwig 919a07b4970SChristoph Hellwig static struct config_group *nvmet_ports_make(struct config_group *group, 920a07b4970SChristoph Hellwig const char *name) 921a07b4970SChristoph Hellwig { 922a07b4970SChristoph Hellwig struct nvmet_port *port; 923a07b4970SChristoph Hellwig u16 portid; 924a07b4970SChristoph Hellwig 925a07b4970SChristoph Hellwig if (kstrtou16(name, 0, &portid)) 926a07b4970SChristoph Hellwig return ERR_PTR(-EINVAL); 927a07b4970SChristoph Hellwig 928a07b4970SChristoph Hellwig port = kzalloc(sizeof(*port), GFP_KERNEL); 929a07b4970SChristoph Hellwig if (!port) 930f98d9ca1SDan Carpenter return ERR_PTR(-ENOMEM); 931a07b4970SChristoph Hellwig 932a07b4970SChristoph Hellwig INIT_LIST_HEAD(&port->entry); 933a07b4970SChristoph Hellwig INIT_LIST_HEAD(&port->subsystems); 934a07b4970SChristoph Hellwig INIT_LIST_HEAD(&port->referrals); 935a07b4970SChristoph Hellwig 936a07b4970SChristoph Hellwig port->disc_addr.portid = cpu_to_le16(portid); 937a07b4970SChristoph Hellwig config_group_init_type_name(&port->group, name, &nvmet_port_type); 938a07b4970SChristoph Hellwig 939a07b4970SChristoph Hellwig config_group_init_type_name(&port->subsys_group, 940a07b4970SChristoph Hellwig "subsystems", &nvmet_port_subsys_type); 941a07b4970SChristoph Hellwig configfs_add_default_group(&port->subsys_group, &port->group); 942a07b4970SChristoph Hellwig 943a07b4970SChristoph Hellwig config_group_init_type_name(&port->referrals_group, 944a07b4970SChristoph Hellwig "referrals", &nvmet_referrals_type); 945a07b4970SChristoph Hellwig configfs_add_default_group(&port->referrals_group, &port->group); 946a07b4970SChristoph Hellwig 947a07b4970SChristoph Hellwig return &port->group; 948a07b4970SChristoph Hellwig } 949a07b4970SChristoph Hellwig 950a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_ports_group_ops = { 951a07b4970SChristoph Hellwig .make_group = nvmet_ports_make, 952a07b4970SChristoph Hellwig }; 953a07b4970SChristoph Hellwig 95466603a31SBhumika Goyal static const struct config_item_type nvmet_ports_type = { 955a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_ports_group_ops, 956a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 957a07b4970SChristoph Hellwig }; 958a07b4970SChristoph Hellwig 959a07b4970SChristoph Hellwig static struct config_group nvmet_subsystems_group; 960a07b4970SChristoph Hellwig static struct config_group nvmet_ports_group; 961a07b4970SChristoph Hellwig 962a07b4970SChristoph Hellwig static void nvmet_host_release(struct config_item *item) 963a07b4970SChristoph Hellwig { 964a07b4970SChristoph Hellwig struct nvmet_host *host = to_host(item); 965a07b4970SChristoph Hellwig 966a07b4970SChristoph Hellwig kfree(host); 967a07b4970SChristoph Hellwig } 968a07b4970SChristoph Hellwig 969a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_host_item_ops = { 970a07b4970SChristoph Hellwig .release = nvmet_host_release, 971a07b4970SChristoph Hellwig }; 972a07b4970SChristoph Hellwig 97366603a31SBhumika Goyal static const struct config_item_type nvmet_host_type = { 974a07b4970SChristoph Hellwig .ct_item_ops = &nvmet_host_item_ops, 975a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 976a07b4970SChristoph Hellwig }; 977a07b4970SChristoph Hellwig 978a07b4970SChristoph Hellwig static struct config_group *nvmet_hosts_make_group(struct config_group *group, 979a07b4970SChristoph Hellwig const char *name) 980a07b4970SChristoph Hellwig { 981a07b4970SChristoph Hellwig struct nvmet_host *host; 982a07b4970SChristoph Hellwig 983a07b4970SChristoph Hellwig host = kzalloc(sizeof(*host), GFP_KERNEL); 984a07b4970SChristoph Hellwig if (!host) 985a07b4970SChristoph Hellwig return ERR_PTR(-ENOMEM); 986a07b4970SChristoph Hellwig 987a07b4970SChristoph Hellwig config_group_init_type_name(&host->group, name, &nvmet_host_type); 988a07b4970SChristoph Hellwig 989a07b4970SChristoph Hellwig return &host->group; 990a07b4970SChristoph Hellwig } 991a07b4970SChristoph Hellwig 992a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_hosts_group_ops = { 993a07b4970SChristoph Hellwig .make_group = nvmet_hosts_make_group, 994a07b4970SChristoph Hellwig }; 995a07b4970SChristoph Hellwig 99666603a31SBhumika Goyal static const struct config_item_type nvmet_hosts_type = { 997a07b4970SChristoph Hellwig .ct_group_ops = &nvmet_hosts_group_ops, 998a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 999a07b4970SChristoph Hellwig }; 1000a07b4970SChristoph Hellwig 1001a07b4970SChristoph Hellwig static struct config_group nvmet_hosts_group; 1002a07b4970SChristoph Hellwig 100366603a31SBhumika Goyal static const struct config_item_type nvmet_root_type = { 1004a07b4970SChristoph Hellwig .ct_owner = THIS_MODULE, 1005a07b4970SChristoph Hellwig }; 1006a07b4970SChristoph Hellwig 1007a07b4970SChristoph Hellwig static struct configfs_subsystem nvmet_configfs_subsystem = { 1008a07b4970SChristoph Hellwig .su_group = { 1009a07b4970SChristoph Hellwig .cg_item = { 1010a07b4970SChristoph Hellwig .ci_namebuf = "nvmet", 1011a07b4970SChristoph Hellwig .ci_type = &nvmet_root_type, 1012a07b4970SChristoph Hellwig }, 1013a07b4970SChristoph Hellwig }, 1014a07b4970SChristoph Hellwig }; 1015a07b4970SChristoph Hellwig 1016a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void) 1017a07b4970SChristoph Hellwig { 1018a07b4970SChristoph Hellwig int ret; 1019a07b4970SChristoph Hellwig 1020a07b4970SChristoph Hellwig config_group_init(&nvmet_configfs_subsystem.su_group); 1021a07b4970SChristoph Hellwig mutex_init(&nvmet_configfs_subsystem.su_mutex); 1022a07b4970SChristoph Hellwig 1023a07b4970SChristoph Hellwig config_group_init_type_name(&nvmet_subsystems_group, 1024a07b4970SChristoph Hellwig "subsystems", &nvmet_subsystems_type); 1025a07b4970SChristoph Hellwig configfs_add_default_group(&nvmet_subsystems_group, 1026a07b4970SChristoph Hellwig &nvmet_configfs_subsystem.su_group); 1027a07b4970SChristoph Hellwig 1028a07b4970SChristoph Hellwig config_group_init_type_name(&nvmet_ports_group, 1029a07b4970SChristoph Hellwig "ports", &nvmet_ports_type); 1030a07b4970SChristoph Hellwig configfs_add_default_group(&nvmet_ports_group, 1031a07b4970SChristoph Hellwig &nvmet_configfs_subsystem.su_group); 1032a07b4970SChristoph Hellwig 1033a07b4970SChristoph Hellwig config_group_init_type_name(&nvmet_hosts_group, 1034a07b4970SChristoph Hellwig "hosts", &nvmet_hosts_type); 1035a07b4970SChristoph Hellwig configfs_add_default_group(&nvmet_hosts_group, 1036a07b4970SChristoph Hellwig &nvmet_configfs_subsystem.su_group); 1037a07b4970SChristoph Hellwig 1038a07b4970SChristoph Hellwig ret = configfs_register_subsystem(&nvmet_configfs_subsystem); 1039a07b4970SChristoph Hellwig if (ret) { 1040a07b4970SChristoph Hellwig pr_err("configfs_register_subsystem: %d\n", ret); 1041a07b4970SChristoph Hellwig return ret; 1042a07b4970SChristoph Hellwig } 1043a07b4970SChristoph Hellwig 1044a07b4970SChristoph Hellwig return 0; 1045a07b4970SChristoph Hellwig } 1046a07b4970SChristoph Hellwig 1047a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void) 1048a07b4970SChristoph Hellwig { 1049a07b4970SChristoph Hellwig configfs_unregister_subsystem(&nvmet_configfs_subsystem); 1050a07b4970SChristoph Hellwig } 1051