1 // SPDX-License-Identifier: GPL-2.0
3 * Configfs interface for the NVMe target.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/kstrtox.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/stat.h>
12 #include <linux/ctype.h>
13 #include <linux/pci.h>
14 #include <linux/pci-p2pdma.h>
15 #ifdef CONFIG_NVME_TARGET_AUTH
16 #include <linux/nvme-auth.h>
18 #include <linux/nvme-keyring.h>
19 #include <crypto/hash.h>
20 #include <crypto/kpp.h>
21 #include <linux/nospec.h>
25 static const struct config_item_type nvmet_host_type;
26 static const struct config_item_type nvmet_subsys_type;
28 static LIST_HEAD(nvmet_ports_list);
29 struct list_head *nvmet_ports = &nvmet_ports_list;
31 struct nvmet_type_name_map {
36 static struct nvmet_type_name_map nvmet_transport[] = {
37 { NVMF_TRTYPE_RDMA, "rdma" },
38 { NVMF_TRTYPE_FC, "fc" },
39 { NVMF_TRTYPE_TCP, "tcp" },
40 { NVMF_TRTYPE_LOOP, "loop" },
43 static const struct nvmet_type_name_map nvmet_addr_family[] = {
44 { NVMF_ADDR_FAMILY_PCI, "pcie" },
45 { NVMF_ADDR_FAMILY_IP4, "ipv4" },
46 { NVMF_ADDR_FAMILY_IP6, "ipv6" },
47 { NVMF_ADDR_FAMILY_IB, "ib" },
48 { NVMF_ADDR_FAMILY_FC, "fc" },
49 { NVMF_ADDR_FAMILY_LOOP, "loop" },
52 static bool nvmet_is_port_enabled(struct nvmet_port *p, const char *caller)
55 pr_err("Disable port '%u' before changing attribute in %s\n",
56 le16_to_cpu(p->disc_addr.portid), caller);
61 * nvmet_port Generic ConfigFS definitions.
62 * Used in any place in the ConfigFS tree that refers to an address.
64 static ssize_t nvmet_addr_adrfam_show(struct config_item *item, char *page)
66 u8 adrfam = to_nvmet_port(item)->disc_addr.adrfam;
69 for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
70 if (nvmet_addr_family[i].type == adrfam)
71 return snprintf(page, PAGE_SIZE, "%s\n",
72 nvmet_addr_family[i].name);
75 return snprintf(page, PAGE_SIZE, "\n");
78 static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
79 const char *page, size_t count)
81 struct nvmet_port *port = to_nvmet_port(item);
84 if (nvmet_is_port_enabled(port, __func__))
87 for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
88 if (sysfs_streq(page, nvmet_addr_family[i].name))
92 pr_err("Invalid value '%s' for adrfam\n", page);
96 port->disc_addr.adrfam = nvmet_addr_family[i].type;
100 CONFIGFS_ATTR(nvmet_, addr_adrfam);
102 static ssize_t nvmet_addr_portid_show(struct config_item *item,
105 __le16 portid = to_nvmet_port(item)->disc_addr.portid;
107 return snprintf(page, PAGE_SIZE, "%d\n", le16_to_cpu(portid));
110 static ssize_t nvmet_addr_portid_store(struct config_item *item,
111 const char *page, size_t count)
113 struct nvmet_port *port = to_nvmet_port(item);
116 if (kstrtou16(page, 0, &portid)) {
117 pr_err("Invalid value '%s' for portid\n", page);
121 if (nvmet_is_port_enabled(port, __func__))
124 port->disc_addr.portid = cpu_to_le16(portid);
128 CONFIGFS_ATTR(nvmet_, addr_portid);
130 static ssize_t nvmet_addr_traddr_show(struct config_item *item,
133 struct nvmet_port *port = to_nvmet_port(item);
135 return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.traddr);
138 static ssize_t nvmet_addr_traddr_store(struct config_item *item,
139 const char *page, size_t count)
141 struct nvmet_port *port = to_nvmet_port(item);
143 if (count > NVMF_TRADDR_SIZE) {
144 pr_err("Invalid value '%s' for traddr\n", page);
148 if (nvmet_is_port_enabled(port, __func__))
151 if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
156 CONFIGFS_ATTR(nvmet_, addr_traddr);
158 static const struct nvmet_type_name_map nvmet_addr_treq[] = {
159 { NVMF_TREQ_NOT_SPECIFIED, "not specified" },
160 { NVMF_TREQ_REQUIRED, "required" },
161 { NVMF_TREQ_NOT_REQUIRED, "not required" },
164 static inline u8 nvmet_port_disc_addr_treq_mask(struct nvmet_port *port)
166 return (port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK);
169 static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page)
171 u8 treq = nvmet_port_disc_addr_treq_secure_channel(to_nvmet_port(item));
174 for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
175 if (treq == nvmet_addr_treq[i].type)
176 return snprintf(page, PAGE_SIZE, "%s\n",
177 nvmet_addr_treq[i].name);
180 return snprintf(page, PAGE_SIZE, "\n");
183 static ssize_t nvmet_addr_treq_store(struct config_item *item,
184 const char *page, size_t count)
186 struct nvmet_port *port = to_nvmet_port(item);
187 u8 treq = nvmet_port_disc_addr_treq_mask(port);
190 if (nvmet_is_port_enabled(port, __func__))
193 for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
194 if (sysfs_streq(page, nvmet_addr_treq[i].name))
198 pr_err("Invalid value '%s' for treq\n", page);
202 if (port->disc_addr.trtype == NVMF_TRTYPE_TCP &&
203 port->disc_addr.tsas.tcp.sectype == NVMF_TCP_SECTYPE_TLS13) {
204 switch (nvmet_addr_treq[i].type) {
205 case NVMF_TREQ_NOT_SPECIFIED:
206 pr_debug("treq '%s' not allowed for TLS1.3\n",
207 nvmet_addr_treq[i].name);
209 case NVMF_TREQ_NOT_REQUIRED:
210 pr_warn("Allow non-TLS connections while TLS1.3 is enabled\n");
216 treq |= nvmet_addr_treq[i].type;
217 port->disc_addr.treq = treq;
221 CONFIGFS_ATTR(nvmet_, addr_treq);
223 static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
226 struct nvmet_port *port = to_nvmet_port(item);
228 return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.trsvcid);
231 static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
232 const char *page, size_t count)
234 struct nvmet_port *port = to_nvmet_port(item);
236 if (count > NVMF_TRSVCID_SIZE) {
237 pr_err("Invalid value '%s' for trsvcid\n", page);
240 if (nvmet_is_port_enabled(port, __func__))
243 if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
248 CONFIGFS_ATTR(nvmet_, addr_trsvcid);
250 static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
253 struct nvmet_port *port = to_nvmet_port(item);
255 return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
258 static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
259 const char *page, size_t count)
261 struct nvmet_port *port = to_nvmet_port(item);
264 if (nvmet_is_port_enabled(port, __func__))
266 ret = kstrtoint(page, 0, &port->inline_data_size);
268 pr_err("Invalid value '%s' for inline_data_size\n", page);
274 CONFIGFS_ATTR(nvmet_, param_inline_data_size);
276 #ifdef CONFIG_BLK_DEV_INTEGRITY
277 static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
280 struct nvmet_port *port = to_nvmet_port(item);
282 return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
285 static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
286 const char *page, size_t count)
288 struct nvmet_port *port = to_nvmet_port(item);
291 if (kstrtobool(page, &val))
294 if (nvmet_is_port_enabled(port, __func__))
297 port->pi_enable = val;
301 CONFIGFS_ATTR(nvmet_, param_pi_enable);
304 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
307 struct nvmet_port *port = to_nvmet_port(item);
310 for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
311 if (port->disc_addr.trtype == nvmet_transport[i].type)
312 return snprintf(page, PAGE_SIZE,
313 "%s\n", nvmet_transport[i].name);
316 return sprintf(page, "\n");
319 static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
321 port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
322 port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
323 port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
326 static void nvmet_port_init_tsas_tcp(struct nvmet_port *port, int sectype)
328 port->disc_addr.tsas.tcp.sectype = sectype;
331 static ssize_t nvmet_addr_trtype_store(struct config_item *item,
332 const char *page, size_t count)
334 struct nvmet_port *port = to_nvmet_port(item);
337 if (nvmet_is_port_enabled(port, __func__))
340 for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
341 if (sysfs_streq(page, nvmet_transport[i].name))
345 pr_err("Invalid value '%s' for trtype\n", page);
349 memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
350 port->disc_addr.trtype = nvmet_transport[i].type;
351 if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
352 nvmet_port_init_tsas_rdma(port);
353 else if (port->disc_addr.trtype == NVMF_TRTYPE_TCP)
354 nvmet_port_init_tsas_tcp(port, NVMF_TCP_SECTYPE_NONE);
358 CONFIGFS_ATTR(nvmet_, addr_trtype);
360 static const struct nvmet_type_name_map nvmet_addr_tsas_tcp[] = {
361 { NVMF_TCP_SECTYPE_NONE, "none" },
362 { NVMF_TCP_SECTYPE_TLS13, "tls1.3" },
365 static const struct nvmet_type_name_map nvmet_addr_tsas_rdma[] = {
366 { NVMF_RDMA_QPTYPE_CONNECTED, "connected" },
367 { NVMF_RDMA_QPTYPE_DATAGRAM, "datagram" },
370 static ssize_t nvmet_addr_tsas_show(struct config_item *item,
373 struct nvmet_port *port = to_nvmet_port(item);
376 if (port->disc_addr.trtype == NVMF_TRTYPE_TCP) {
377 for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_tcp); i++) {
378 if (port->disc_addr.tsas.tcp.sectype == nvmet_addr_tsas_tcp[i].type)
379 return sprintf(page, "%s\n", nvmet_addr_tsas_tcp[i].name);
381 } else if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA) {
382 for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_rdma); i++) {
383 if (port->disc_addr.tsas.rdma.qptype == nvmet_addr_tsas_rdma[i].type)
384 return sprintf(page, "%s\n", nvmet_addr_tsas_rdma[i].name);
387 return sprintf(page, "reserved\n");
390 static ssize_t nvmet_addr_tsas_store(struct config_item *item,
391 const char *page, size_t count)
393 struct nvmet_port *port = to_nvmet_port(item);
394 u8 treq = nvmet_port_disc_addr_treq_mask(port);
398 if (nvmet_is_port_enabled(port, __func__))
401 if (port->disc_addr.trtype != NVMF_TRTYPE_TCP)
404 for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_tcp); i++) {
405 if (sysfs_streq(page, nvmet_addr_tsas_tcp[i].name)) {
406 sectype = nvmet_addr_tsas_tcp[i].type;
411 pr_err("Invalid value '%s' for tsas\n", page);
415 if (sectype == NVMF_TCP_SECTYPE_TLS13) {
416 if (!IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS)) {
417 pr_err("TLS is not supported\n");
420 if (!port->keyring) {
421 pr_err("TLS keyring not configured\n");
426 nvmet_port_init_tsas_tcp(port, sectype);
428 * If TLS is enabled TREQ should be set to 'required' per default
430 if (sectype == NVMF_TCP_SECTYPE_TLS13) {
431 u8 sc = nvmet_port_disc_addr_treq_secure_channel(port);
433 if (sc == NVMF_TREQ_NOT_SPECIFIED)
434 treq |= NVMF_TREQ_REQUIRED;
438 treq |= NVMF_TREQ_NOT_SPECIFIED;
440 port->disc_addr.treq = treq;
444 CONFIGFS_ATTR(nvmet_, addr_tsas);
447 * Namespace structures & file operation functions below
449 static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
451 return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
454 static ssize_t nvmet_ns_device_path_store(struct config_item *item,
455 const char *page, size_t count)
457 struct nvmet_ns *ns = to_nvmet_ns(item);
458 struct nvmet_subsys *subsys = ns->subsys;
462 mutex_lock(&subsys->lock);
468 len = strcspn(page, "\n");
472 kfree(ns->device_path);
474 ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
475 if (!ns->device_path)
478 mutex_unlock(&subsys->lock);
482 mutex_unlock(&subsys->lock);
486 CONFIGFS_ATTR(nvmet_ns_, device_path);
488 #ifdef CONFIG_PCI_P2PDMA
489 static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
491 struct nvmet_ns *ns = to_nvmet_ns(item);
493 return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
496 static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
497 const char *page, size_t count)
499 struct nvmet_ns *ns = to_nvmet_ns(item);
500 struct pci_dev *p2p_dev = NULL;
505 mutex_lock(&ns->subsys->lock);
511 error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
517 ns->use_p2pmem = use_p2pmem;
518 pci_dev_put(ns->p2p_dev);
519 ns->p2p_dev = p2p_dev;
522 mutex_unlock(&ns->subsys->lock);
527 CONFIGFS_ATTR(nvmet_ns_, p2pmem);
528 #endif /* CONFIG_PCI_P2PDMA */
530 static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
532 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
535 static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
536 const char *page, size_t count)
538 struct nvmet_ns *ns = to_nvmet_ns(item);
539 struct nvmet_subsys *subsys = ns->subsys;
542 mutex_lock(&subsys->lock);
548 if (uuid_parse(page, &ns->uuid))
552 mutex_unlock(&subsys->lock);
553 return ret ? ret : count;
556 CONFIGFS_ATTR(nvmet_ns_, device_uuid);
558 static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
560 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
563 static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
564 const char *page, size_t count)
566 struct nvmet_ns *ns = to_nvmet_ns(item);
567 struct nvmet_subsys *subsys = ns->subsys;
569 const char *p = page;
573 mutex_lock(&subsys->lock);
579 for (i = 0; i < 16; i++) {
580 if (p + 2 > page + count) {
584 if (!isxdigit(p[0]) || !isxdigit(p[1])) {
589 nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
592 if (*p == '-' || *p == ':')
596 memcpy(&ns->nguid, nguid, sizeof(nguid));
598 mutex_unlock(&subsys->lock);
599 return ret ? ret : count;
602 CONFIGFS_ATTR(nvmet_ns_, device_nguid);
604 static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
606 return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
609 static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
610 const char *page, size_t count)
612 struct nvmet_ns *ns = to_nvmet_ns(item);
613 u32 oldgrpid, newgrpid;
616 ret = kstrtou32(page, 0, &newgrpid);
620 if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
623 down_write(&nvmet_ana_sem);
624 oldgrpid = ns->anagrpid;
625 newgrpid = array_index_nospec(newgrpid, NVMET_MAX_ANAGRPS);
626 nvmet_ana_group_enabled[newgrpid]++;
627 ns->anagrpid = newgrpid;
628 nvmet_ana_group_enabled[oldgrpid]--;
630 up_write(&nvmet_ana_sem);
632 nvmet_send_ana_event(ns->subsys, NULL);
636 CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
638 static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
640 return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
643 static ssize_t nvmet_ns_enable_store(struct config_item *item,
644 const char *page, size_t count)
646 struct nvmet_ns *ns = to_nvmet_ns(item);
650 if (kstrtobool(page, &enable))
654 ret = nvmet_ns_enable(ns);
656 nvmet_ns_disable(ns);
658 return ret ? ret : count;
661 CONFIGFS_ATTR(nvmet_ns_, enable);
663 static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
665 return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
668 static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
669 const char *page, size_t count)
671 struct nvmet_ns *ns = to_nvmet_ns(item);
674 if (kstrtobool(page, &val))
677 mutex_lock(&ns->subsys->lock);
679 pr_err("disable ns before setting buffered_io value.\n");
680 mutex_unlock(&ns->subsys->lock);
684 ns->buffered_io = val;
685 mutex_unlock(&ns->subsys->lock);
689 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
691 static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
692 const char *page, size_t count)
694 struct nvmet_ns *ns = to_nvmet_ns(item);
697 if (kstrtobool(page, &val))
703 mutex_lock(&ns->subsys->lock);
705 pr_err("enable ns before revalidate.\n");
706 mutex_unlock(&ns->subsys->lock);
709 if (nvmet_ns_revalidate(ns))
710 nvmet_ns_changed(ns->subsys, ns->nsid);
711 mutex_unlock(&ns->subsys->lock);
715 CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
717 static struct configfs_attribute *nvmet_ns_attrs[] = {
718 &nvmet_ns_attr_device_path,
719 &nvmet_ns_attr_device_nguid,
720 &nvmet_ns_attr_device_uuid,
721 &nvmet_ns_attr_ana_grpid,
722 &nvmet_ns_attr_enable,
723 &nvmet_ns_attr_buffered_io,
724 &nvmet_ns_attr_revalidate_size,
725 #ifdef CONFIG_PCI_P2PDMA
726 &nvmet_ns_attr_p2pmem,
731 static void nvmet_ns_release(struct config_item *item)
733 struct nvmet_ns *ns = to_nvmet_ns(item);
738 static struct configfs_item_operations nvmet_ns_item_ops = {
739 .release = nvmet_ns_release,
742 static const struct config_item_type nvmet_ns_type = {
743 .ct_item_ops = &nvmet_ns_item_ops,
744 .ct_attrs = nvmet_ns_attrs,
745 .ct_owner = THIS_MODULE,
748 static struct config_group *nvmet_ns_make(struct config_group *group,
751 struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
756 ret = kstrtou32(name, 0, &nsid);
761 if (nsid == 0 || nsid == NVME_NSID_ALL) {
762 pr_err("invalid nsid %#x", nsid);
767 ns = nvmet_ns_alloc(subsys, nsid);
770 config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
772 pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
779 static struct configfs_group_operations nvmet_namespaces_group_ops = {
780 .make_group = nvmet_ns_make,
783 static const struct config_item_type nvmet_namespaces_type = {
784 .ct_group_ops = &nvmet_namespaces_group_ops,
785 .ct_owner = THIS_MODULE,
788 #ifdef CONFIG_NVME_TARGET_PASSTHRU
790 static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
793 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
795 return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
798 static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
799 const char *page, size_t count)
801 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
805 mutex_lock(&subsys->lock);
808 if (subsys->passthru_ctrl)
812 len = strcspn(page, "\n");
816 kfree(subsys->passthru_ctrl_path);
818 subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
819 if (!subsys->passthru_ctrl_path)
822 mutex_unlock(&subsys->lock);
826 mutex_unlock(&subsys->lock);
829 CONFIGFS_ATTR(nvmet_passthru_, device_path);
831 static ssize_t nvmet_passthru_enable_show(struct config_item *item,
834 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
836 return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
839 static ssize_t nvmet_passthru_enable_store(struct config_item *item,
840 const char *page, size_t count)
842 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
846 if (kstrtobool(page, &enable))
850 ret = nvmet_passthru_ctrl_enable(subsys);
852 nvmet_passthru_ctrl_disable(subsys);
854 return ret ? ret : count;
856 CONFIGFS_ATTR(nvmet_passthru_, enable);
858 static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
861 return sprintf(page, "%u\n", to_subsys(item->ci_parent)->admin_timeout);
864 static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
865 const char *page, size_t count)
867 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
868 unsigned int timeout;
870 if (kstrtouint(page, 0, &timeout))
872 subsys->admin_timeout = timeout;
875 CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
877 static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
880 return sprintf(page, "%u\n", to_subsys(item->ci_parent)->io_timeout);
883 static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
884 const char *page, size_t count)
886 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
887 unsigned int timeout;
889 if (kstrtouint(page, 0, &timeout))
891 subsys->io_timeout = timeout;
894 CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
896 static ssize_t nvmet_passthru_clear_ids_show(struct config_item *item,
899 return sprintf(page, "%u\n", to_subsys(item->ci_parent)->clear_ids);
902 static ssize_t nvmet_passthru_clear_ids_store(struct config_item *item,
903 const char *page, size_t count)
905 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
906 unsigned int clear_ids;
908 if (kstrtouint(page, 0, &clear_ids))
910 subsys->clear_ids = clear_ids;
913 CONFIGFS_ATTR(nvmet_passthru_, clear_ids);
915 static struct configfs_attribute *nvmet_passthru_attrs[] = {
916 &nvmet_passthru_attr_device_path,
917 &nvmet_passthru_attr_enable,
918 &nvmet_passthru_attr_admin_timeout,
919 &nvmet_passthru_attr_io_timeout,
920 &nvmet_passthru_attr_clear_ids,
924 static const struct config_item_type nvmet_passthru_type = {
925 .ct_attrs = nvmet_passthru_attrs,
926 .ct_owner = THIS_MODULE,
929 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
931 config_group_init_type_name(&subsys->passthru_group,
932 "passthru", &nvmet_passthru_type);
933 configfs_add_default_group(&subsys->passthru_group,
937 #else /* CONFIG_NVME_TARGET_PASSTHRU */
939 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
943 #endif /* CONFIG_NVME_TARGET_PASSTHRU */
945 static int nvmet_port_subsys_allow_link(struct config_item *parent,
946 struct config_item *target)
948 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
949 struct nvmet_subsys *subsys;
950 struct nvmet_subsys_link *link, *p;
953 if (target->ci_type != &nvmet_subsys_type) {
954 pr_err("can only link subsystems into the subsystems dir.!\n");
957 subsys = to_subsys(target);
958 link = kmalloc(sizeof(*link), GFP_KERNEL);
961 link->subsys = subsys;
963 down_write(&nvmet_config_sem);
965 list_for_each_entry(p, &port->subsystems, entry) {
966 if (p->subsys == subsys)
970 if (list_empty(&port->subsystems)) {
971 ret = nvmet_enable_port(port);
976 list_add_tail(&link->entry, &port->subsystems);
977 nvmet_port_disc_changed(port, subsys);
979 up_write(&nvmet_config_sem);
983 up_write(&nvmet_config_sem);
988 static void nvmet_port_subsys_drop_link(struct config_item *parent,
989 struct config_item *target)
991 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
992 struct nvmet_subsys *subsys = to_subsys(target);
993 struct nvmet_subsys_link *p;
995 down_write(&nvmet_config_sem);
996 list_for_each_entry(p, &port->subsystems, entry) {
997 if (p->subsys == subsys)
1000 up_write(&nvmet_config_sem);
1004 list_del(&p->entry);
1005 nvmet_port_del_ctrls(port, subsys);
1006 nvmet_port_disc_changed(port, subsys);
1008 if (list_empty(&port->subsystems))
1009 nvmet_disable_port(port);
1010 up_write(&nvmet_config_sem);
1014 static struct configfs_item_operations nvmet_port_subsys_item_ops = {
1015 .allow_link = nvmet_port_subsys_allow_link,
1016 .drop_link = nvmet_port_subsys_drop_link,
1019 static const struct config_item_type nvmet_port_subsys_type = {
1020 .ct_item_ops = &nvmet_port_subsys_item_ops,
1021 .ct_owner = THIS_MODULE,
1024 static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
1025 struct config_item *target)
1027 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
1028 struct nvmet_host *host;
1029 struct nvmet_host_link *link, *p;
1032 if (target->ci_type != &nvmet_host_type) {
1033 pr_err("can only link hosts into the allowed_hosts directory!\n");
1037 host = to_host(target);
1038 link = kmalloc(sizeof(*link), GFP_KERNEL);
1043 down_write(&nvmet_config_sem);
1045 if (subsys->allow_any_host) {
1046 pr_err("can't add hosts when allow_any_host is set!\n");
1051 list_for_each_entry(p, &subsys->hosts, entry) {
1052 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
1055 list_add_tail(&link->entry, &subsys->hosts);
1056 nvmet_subsys_disc_changed(subsys, host);
1058 up_write(&nvmet_config_sem);
1061 up_write(&nvmet_config_sem);
1066 static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
1067 struct config_item *target)
1069 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
1070 struct nvmet_host *host = to_host(target);
1071 struct nvmet_host_link *p;
1073 down_write(&nvmet_config_sem);
1074 list_for_each_entry(p, &subsys->hosts, entry) {
1075 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
1078 up_write(&nvmet_config_sem);
1082 list_del(&p->entry);
1083 nvmet_subsys_disc_changed(subsys, host);
1085 up_write(&nvmet_config_sem);
1089 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
1090 .allow_link = nvmet_allowed_hosts_allow_link,
1091 .drop_link = nvmet_allowed_hosts_drop_link,
1094 static const struct config_item_type nvmet_allowed_hosts_type = {
1095 .ct_item_ops = &nvmet_allowed_hosts_item_ops,
1096 .ct_owner = THIS_MODULE,
1099 static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
1102 return snprintf(page, PAGE_SIZE, "%d\n",
1103 to_subsys(item)->allow_any_host);
1106 static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
1107 const char *page, size_t count)
1109 struct nvmet_subsys *subsys = to_subsys(item);
1110 bool allow_any_host;
1113 if (kstrtobool(page, &allow_any_host))
1116 down_write(&nvmet_config_sem);
1117 if (allow_any_host && !list_empty(&subsys->hosts)) {
1118 pr_err("Can't set allow_any_host when explicit hosts are set!\n");
1123 if (subsys->allow_any_host != allow_any_host) {
1124 subsys->allow_any_host = allow_any_host;
1125 nvmet_subsys_disc_changed(subsys, NULL);
1129 up_write(&nvmet_config_sem);
1130 return ret ? ret : count;
1133 CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
1135 static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
1138 struct nvmet_subsys *subsys = to_subsys(item);
1140 if (NVME_TERTIARY(subsys->ver))
1141 return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
1142 NVME_MAJOR(subsys->ver),
1143 NVME_MINOR(subsys->ver),
1144 NVME_TERTIARY(subsys->ver));
1146 return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
1147 NVME_MAJOR(subsys->ver),
1148 NVME_MINOR(subsys->ver));
1152 nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,
1153 const char *page, size_t count)
1155 int major, minor, tertiary = 0;
1158 if (subsys->subsys_discovered) {
1159 if (NVME_TERTIARY(subsys->ver))
1160 pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
1161 NVME_MAJOR(subsys->ver),
1162 NVME_MINOR(subsys->ver),
1163 NVME_TERTIARY(subsys->ver));
1165 pr_err("Can't set version number. %llu.%llu is already assigned\n",
1166 NVME_MAJOR(subsys->ver),
1167 NVME_MINOR(subsys->ver));
1171 /* passthru subsystems use the underlying controller's version */
1172 if (nvmet_is_passthru_subsys(subsys))
1175 ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
1176 if (ret != 2 && ret != 3)
1179 subsys->ver = NVME_VS(major, minor, tertiary);
1184 static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
1185 const char *page, size_t count)
1187 struct nvmet_subsys *subsys = to_subsys(item);
1190 down_write(&nvmet_config_sem);
1191 mutex_lock(&subsys->lock);
1192 ret = nvmet_subsys_attr_version_store_locked(subsys, page, count);
1193 mutex_unlock(&subsys->lock);
1194 up_write(&nvmet_config_sem);
1198 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
1200 /* See Section 1.5 of NVMe 1.4 */
1201 static bool nvmet_is_ascii(const char c)
1203 return c >= 0x20 && c <= 0x7e;
1206 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
1209 struct nvmet_subsys *subsys = to_subsys(item);
1211 return snprintf(page, PAGE_SIZE, "%.*s\n",
1212 NVMET_SN_MAX_SIZE, subsys->serial);
1216 nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
1217 const char *page, size_t count)
1219 int pos, len = strcspn(page, "\n");
1221 if (subsys->subsys_discovered) {
1222 pr_err("Can't set serial number. %s is already assigned\n",
1227 if (!len || len > NVMET_SN_MAX_SIZE) {
1228 pr_err("Serial Number can not be empty or exceed %d Bytes\n",
1233 for (pos = 0; pos < len; pos++) {
1234 if (!nvmet_is_ascii(page[pos])) {
1235 pr_err("Serial Number must contain only ASCII strings\n");
1240 memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
1245 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
1246 const char *page, size_t count)
1248 struct nvmet_subsys *subsys = to_subsys(item);
1251 down_write(&nvmet_config_sem);
1252 mutex_lock(&subsys->lock);
1253 ret = nvmet_subsys_attr_serial_store_locked(subsys, page, count);
1254 mutex_unlock(&subsys->lock);
1255 up_write(&nvmet_config_sem);
1259 CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
1261 static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
1264 return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
1267 static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
1268 const char *page, size_t cnt)
1272 if (sscanf(page, "%hu\n", &cntlid_min) != 1)
1275 if (cntlid_min == 0)
1278 down_write(&nvmet_config_sem);
1279 if (cntlid_min > to_subsys(item)->cntlid_max)
1281 to_subsys(item)->cntlid_min = cntlid_min;
1282 up_write(&nvmet_config_sem);
1286 up_write(&nvmet_config_sem);
1289 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
1291 static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
1294 return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
1297 static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
1298 const char *page, size_t cnt)
1302 if (sscanf(page, "%hu\n", &cntlid_max) != 1)
1305 if (cntlid_max == 0)
1308 down_write(&nvmet_config_sem);
1309 if (cntlid_max < to_subsys(item)->cntlid_min)
1311 to_subsys(item)->cntlid_max = cntlid_max;
1312 up_write(&nvmet_config_sem);
1316 up_write(&nvmet_config_sem);
1319 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
1321 static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
1324 struct nvmet_subsys *subsys = to_subsys(item);
1326 return snprintf(page, PAGE_SIZE, "%s\n", subsys->model_number);
1329 static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
1330 const char *page, size_t count)
1335 if (subsys->subsys_discovered) {
1336 pr_err("Can't set model number. %s is already assigned\n",
1337 subsys->model_number);
1341 len = strcspn(page, "\n");
1345 if (len > NVMET_MN_MAX_SIZE) {
1346 pr_err("Model number size can not exceed %d Bytes\n",
1351 for (pos = 0; pos < len; pos++) {
1352 if (!nvmet_is_ascii(page[pos]))
1356 val = kmemdup_nul(page, len, GFP_KERNEL);
1359 kfree(subsys->model_number);
1360 subsys->model_number = val;
1364 static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
1365 const char *page, size_t count)
1367 struct nvmet_subsys *subsys = to_subsys(item);
1370 down_write(&nvmet_config_sem);
1371 mutex_lock(&subsys->lock);
1372 ret = nvmet_subsys_attr_model_store_locked(subsys, page, count);
1373 mutex_unlock(&subsys->lock);
1374 up_write(&nvmet_config_sem);
1378 CONFIGFS_ATTR(nvmet_subsys_, attr_model);
1380 static ssize_t nvmet_subsys_attr_ieee_oui_show(struct config_item *item,
1383 struct nvmet_subsys *subsys = to_subsys(item);
1385 return sysfs_emit(page, "0x%06x\n", subsys->ieee_oui);
1388 static ssize_t nvmet_subsys_attr_ieee_oui_store_locked(struct nvmet_subsys *subsys,
1389 const char *page, size_t count)
1394 if (subsys->subsys_discovered) {
1395 pr_err("Can't set IEEE OUI. 0x%06x is already assigned\n",
1400 ret = kstrtou32(page, 0, &val);
1404 if (val >= 0x1000000)
1407 subsys->ieee_oui = val;
1412 static ssize_t nvmet_subsys_attr_ieee_oui_store(struct config_item *item,
1413 const char *page, size_t count)
1415 struct nvmet_subsys *subsys = to_subsys(item);
1418 down_write(&nvmet_config_sem);
1419 mutex_lock(&subsys->lock);
1420 ret = nvmet_subsys_attr_ieee_oui_store_locked(subsys, page, count);
1421 mutex_unlock(&subsys->lock);
1422 up_write(&nvmet_config_sem);
1426 CONFIGFS_ATTR(nvmet_subsys_, attr_ieee_oui);
1428 static ssize_t nvmet_subsys_attr_firmware_show(struct config_item *item,
1431 struct nvmet_subsys *subsys = to_subsys(item);
1433 return sysfs_emit(page, "%s\n", subsys->firmware_rev);
1436 static ssize_t nvmet_subsys_attr_firmware_store_locked(struct nvmet_subsys *subsys,
1437 const char *page, size_t count)
1442 if (subsys->subsys_discovered) {
1443 pr_err("Can't set firmware revision. %s is already assigned\n",
1444 subsys->firmware_rev);
1448 len = strcspn(page, "\n");
1452 if (len > NVMET_FR_MAX_SIZE) {
1453 pr_err("Firmware revision size can not exceed %d Bytes\n",
1458 for (pos = 0; pos < len; pos++) {
1459 if (!nvmet_is_ascii(page[pos]))
1463 val = kmemdup_nul(page, len, GFP_KERNEL);
1467 kfree(subsys->firmware_rev);
1469 subsys->firmware_rev = val;
1474 static ssize_t nvmet_subsys_attr_firmware_store(struct config_item *item,
1475 const char *page, size_t count)
1477 struct nvmet_subsys *subsys = to_subsys(item);
1480 down_write(&nvmet_config_sem);
1481 mutex_lock(&subsys->lock);
1482 ret = nvmet_subsys_attr_firmware_store_locked(subsys, page, count);
1483 mutex_unlock(&subsys->lock);
1484 up_write(&nvmet_config_sem);
1488 CONFIGFS_ATTR(nvmet_subsys_, attr_firmware);
1490 #ifdef CONFIG_BLK_DEV_INTEGRITY
1491 static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
1494 return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
1497 static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
1498 const char *page, size_t count)
1500 struct nvmet_subsys *subsys = to_subsys(item);
1503 if (kstrtobool(page, &pi_enable))
1506 subsys->pi_support = pi_enable;
1509 CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
1512 static ssize_t nvmet_subsys_attr_qid_max_show(struct config_item *item,
1515 return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->max_qid);
1518 static ssize_t nvmet_subsys_attr_qid_max_store(struct config_item *item,
1519 const char *page, size_t cnt)
1521 struct nvmet_subsys *subsys = to_subsys(item);
1522 struct nvmet_ctrl *ctrl;
1525 if (sscanf(page, "%hu\n", &qid_max) != 1)
1528 if (qid_max < 1 || qid_max > NVMET_NR_QUEUES)
1531 down_write(&nvmet_config_sem);
1532 subsys->max_qid = qid_max;
1534 /* Force reconnect */
1535 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1536 ctrl->ops->delete_ctrl(ctrl);
1537 up_write(&nvmet_config_sem);
1541 CONFIGFS_ATTR(nvmet_subsys_, attr_qid_max);
1543 static struct configfs_attribute *nvmet_subsys_attrs[] = {
1544 &nvmet_subsys_attr_attr_allow_any_host,
1545 &nvmet_subsys_attr_attr_version,
1546 &nvmet_subsys_attr_attr_serial,
1547 &nvmet_subsys_attr_attr_cntlid_min,
1548 &nvmet_subsys_attr_attr_cntlid_max,
1549 &nvmet_subsys_attr_attr_model,
1550 &nvmet_subsys_attr_attr_qid_max,
1551 &nvmet_subsys_attr_attr_ieee_oui,
1552 &nvmet_subsys_attr_attr_firmware,
1553 #ifdef CONFIG_BLK_DEV_INTEGRITY
1554 &nvmet_subsys_attr_attr_pi_enable,
1560 * Subsystem structures & folder operation functions below
1562 static void nvmet_subsys_release(struct config_item *item)
1564 struct nvmet_subsys *subsys = to_subsys(item);
1566 nvmet_subsys_del_ctrls(subsys);
1567 nvmet_subsys_put(subsys);
1570 static struct configfs_item_operations nvmet_subsys_item_ops = {
1571 .release = nvmet_subsys_release,
1574 static const struct config_item_type nvmet_subsys_type = {
1575 .ct_item_ops = &nvmet_subsys_item_ops,
1576 .ct_attrs = nvmet_subsys_attrs,
1577 .ct_owner = THIS_MODULE,
1580 static struct config_group *nvmet_subsys_make(struct config_group *group,
1583 struct nvmet_subsys *subsys;
1585 if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1586 pr_err("can't create discovery subsystem through configfs\n");
1587 return ERR_PTR(-EINVAL);
1590 subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
1592 return ERR_CAST(subsys);
1594 config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1596 config_group_init_type_name(&subsys->namespaces_group,
1597 "namespaces", &nvmet_namespaces_type);
1598 configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1600 config_group_init_type_name(&subsys->allowed_hosts_group,
1601 "allowed_hosts", &nvmet_allowed_hosts_type);
1602 configfs_add_default_group(&subsys->allowed_hosts_group,
1605 nvmet_add_passthru_group(subsys);
1607 return &subsys->group;
1610 static struct configfs_group_operations nvmet_subsystems_group_ops = {
1611 .make_group = nvmet_subsys_make,
1614 static const struct config_item_type nvmet_subsystems_type = {
1615 .ct_group_ops = &nvmet_subsystems_group_ops,
1616 .ct_owner = THIS_MODULE,
1619 static ssize_t nvmet_referral_enable_show(struct config_item *item,
1622 return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1625 static ssize_t nvmet_referral_enable_store(struct config_item *item,
1626 const char *page, size_t count)
1628 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1629 struct nvmet_port *port = to_nvmet_port(item);
1632 if (kstrtobool(page, &enable))
1636 nvmet_referral_enable(parent, port);
1638 nvmet_referral_disable(parent, port);
1642 pr_err("Invalid value '%s' for enable\n", page);
1646 CONFIGFS_ATTR(nvmet_referral_, enable);
1649 * Discovery Service subsystem definitions
1651 static struct configfs_attribute *nvmet_referral_attrs[] = {
1652 &nvmet_attr_addr_adrfam,
1653 &nvmet_attr_addr_portid,
1654 &nvmet_attr_addr_treq,
1655 &nvmet_attr_addr_traddr,
1656 &nvmet_attr_addr_trsvcid,
1657 &nvmet_attr_addr_trtype,
1658 &nvmet_referral_attr_enable,
1662 static void nvmet_referral_notify(struct config_group *group,
1663 struct config_item *item)
1665 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1666 struct nvmet_port *port = to_nvmet_port(item);
1668 nvmet_referral_disable(parent, port);
1671 static void nvmet_referral_release(struct config_item *item)
1673 struct nvmet_port *port = to_nvmet_port(item);
1678 static struct configfs_item_operations nvmet_referral_item_ops = {
1679 .release = nvmet_referral_release,
1682 static const struct config_item_type nvmet_referral_type = {
1683 .ct_owner = THIS_MODULE,
1684 .ct_attrs = nvmet_referral_attrs,
1685 .ct_item_ops = &nvmet_referral_item_ops,
1688 static struct config_group *nvmet_referral_make(
1689 struct config_group *group, const char *name)
1691 struct nvmet_port *port;
1693 port = kzalloc(sizeof(*port), GFP_KERNEL);
1695 return ERR_PTR(-ENOMEM);
1697 INIT_LIST_HEAD(&port->entry);
1698 config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1700 return &port->group;
1703 static struct configfs_group_operations nvmet_referral_group_ops = {
1704 .make_group = nvmet_referral_make,
1705 .disconnect_notify = nvmet_referral_notify,
1708 static const struct config_item_type nvmet_referrals_type = {
1709 .ct_owner = THIS_MODULE,
1710 .ct_group_ops = &nvmet_referral_group_ops,
1713 static struct nvmet_type_name_map nvmet_ana_state[] = {
1714 { NVME_ANA_OPTIMIZED, "optimized" },
1715 { NVME_ANA_NONOPTIMIZED, "non-optimized" },
1716 { NVME_ANA_INACCESSIBLE, "inaccessible" },
1717 { NVME_ANA_PERSISTENT_LOSS, "persistent-loss" },
1718 { NVME_ANA_CHANGE, "change" },
1721 static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
1724 struct nvmet_ana_group *grp = to_ana_group(item);
1725 enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
1728 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1729 if (state == nvmet_ana_state[i].type)
1730 return sprintf(page, "%s\n", nvmet_ana_state[i].name);
1733 return sprintf(page, "\n");
1736 static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
1737 const char *page, size_t count)
1739 struct nvmet_ana_group *grp = to_ana_group(item);
1740 enum nvme_ana_state *ana_state = grp->port->ana_state;
1743 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1744 if (sysfs_streq(page, nvmet_ana_state[i].name))
1748 pr_err("Invalid value '%s' for ana_state\n", page);
1752 down_write(&nvmet_ana_sem);
1753 ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
1755 up_write(&nvmet_ana_sem);
1756 nvmet_port_send_ana_event(grp->port);
1760 CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
1762 static struct configfs_attribute *nvmet_ana_group_attrs[] = {
1763 &nvmet_ana_group_attr_ana_state,
1767 static void nvmet_ana_group_release(struct config_item *item)
1769 struct nvmet_ana_group *grp = to_ana_group(item);
1771 if (grp == &grp->port->ana_default_group)
1774 down_write(&nvmet_ana_sem);
1775 grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
1776 nvmet_ana_group_enabled[grp->grpid]--;
1777 up_write(&nvmet_ana_sem);
1779 nvmet_port_send_ana_event(grp->port);
1783 static struct configfs_item_operations nvmet_ana_group_item_ops = {
1784 .release = nvmet_ana_group_release,
1787 static const struct config_item_type nvmet_ana_group_type = {
1788 .ct_item_ops = &nvmet_ana_group_item_ops,
1789 .ct_attrs = nvmet_ana_group_attrs,
1790 .ct_owner = THIS_MODULE,
1793 static struct config_group *nvmet_ana_groups_make_group(
1794 struct config_group *group, const char *name)
1796 struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
1797 struct nvmet_ana_group *grp;
1801 ret = kstrtou32(name, 0, &grpid);
1806 if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
1810 grp = kzalloc(sizeof(*grp), GFP_KERNEL);
1816 down_write(&nvmet_ana_sem);
1817 grpid = array_index_nospec(grpid, NVMET_MAX_ANAGRPS);
1818 nvmet_ana_group_enabled[grpid]++;
1819 up_write(&nvmet_ana_sem);
1821 nvmet_port_send_ana_event(grp->port);
1823 config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
1826 return ERR_PTR(ret);
1829 static struct configfs_group_operations nvmet_ana_groups_group_ops = {
1830 .make_group = nvmet_ana_groups_make_group,
1833 static const struct config_item_type nvmet_ana_groups_type = {
1834 .ct_group_ops = &nvmet_ana_groups_group_ops,
1835 .ct_owner = THIS_MODULE,
1839 * Ports definitions.
1841 static void nvmet_port_release(struct config_item *item)
1843 struct nvmet_port *port = to_nvmet_port(item);
1845 /* Let inflight controllers teardown complete */
1846 flush_workqueue(nvmet_wq);
1847 list_del(&port->global_entry);
1849 key_put(port->keyring);
1850 kfree(port->ana_state);
1854 static struct configfs_attribute *nvmet_port_attrs[] = {
1855 &nvmet_attr_addr_adrfam,
1856 &nvmet_attr_addr_treq,
1857 &nvmet_attr_addr_traddr,
1858 &nvmet_attr_addr_trsvcid,
1859 &nvmet_attr_addr_trtype,
1860 &nvmet_attr_addr_tsas,
1861 &nvmet_attr_param_inline_data_size,
1862 #ifdef CONFIG_BLK_DEV_INTEGRITY
1863 &nvmet_attr_param_pi_enable,
1868 static struct configfs_item_operations nvmet_port_item_ops = {
1869 .release = nvmet_port_release,
1872 static const struct config_item_type nvmet_port_type = {
1873 .ct_attrs = nvmet_port_attrs,
1874 .ct_item_ops = &nvmet_port_item_ops,
1875 .ct_owner = THIS_MODULE,
1878 static struct config_group *nvmet_ports_make(struct config_group *group,
1881 struct nvmet_port *port;
1885 if (kstrtou16(name, 0, &portid))
1886 return ERR_PTR(-EINVAL);
1888 port = kzalloc(sizeof(*port), GFP_KERNEL);
1890 return ERR_PTR(-ENOMEM);
1892 port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
1893 sizeof(*port->ana_state), GFP_KERNEL);
1894 if (!port->ana_state) {
1896 return ERR_PTR(-ENOMEM);
1899 if (IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS) && nvme_keyring_id()) {
1900 port->keyring = key_lookup(nvme_keyring_id());
1901 if (IS_ERR(port->keyring)) {
1902 pr_warn("NVMe keyring not available, disabling TLS\n");
1903 port->keyring = NULL;
1907 for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
1908 if (i == NVMET_DEFAULT_ANA_GRPID)
1909 port->ana_state[1] = NVME_ANA_OPTIMIZED;
1911 port->ana_state[i] = NVME_ANA_INACCESSIBLE;
1914 list_add(&port->global_entry, &nvmet_ports_list);
1916 INIT_LIST_HEAD(&port->entry);
1917 INIT_LIST_HEAD(&port->subsystems);
1918 INIT_LIST_HEAD(&port->referrals);
1919 port->inline_data_size = -1; /* < 0 == let the transport choose */
1921 port->disc_addr.portid = cpu_to_le16(portid);
1922 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
1923 port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1924 config_group_init_type_name(&port->group, name, &nvmet_port_type);
1926 config_group_init_type_name(&port->subsys_group,
1927 "subsystems", &nvmet_port_subsys_type);
1928 configfs_add_default_group(&port->subsys_group, &port->group);
1930 config_group_init_type_name(&port->referrals_group,
1931 "referrals", &nvmet_referrals_type);
1932 configfs_add_default_group(&port->referrals_group, &port->group);
1934 config_group_init_type_name(&port->ana_groups_group,
1935 "ana_groups", &nvmet_ana_groups_type);
1936 configfs_add_default_group(&port->ana_groups_group, &port->group);
1938 port->ana_default_group.port = port;
1939 port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
1940 config_group_init_type_name(&port->ana_default_group.group,
1941 __stringify(NVMET_DEFAULT_ANA_GRPID),
1942 &nvmet_ana_group_type);
1943 configfs_add_default_group(&port->ana_default_group.group,
1944 &port->ana_groups_group);
1946 return &port->group;
1949 static struct configfs_group_operations nvmet_ports_group_ops = {
1950 .make_group = nvmet_ports_make,
1953 static const struct config_item_type nvmet_ports_type = {
1954 .ct_group_ops = &nvmet_ports_group_ops,
1955 .ct_owner = THIS_MODULE,
1958 static struct config_group nvmet_subsystems_group;
1959 static struct config_group nvmet_ports_group;
1961 #ifdef CONFIG_NVME_TARGET_AUTH
1962 static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
1965 u8 *dhchap_secret = to_host(item)->dhchap_secret;
1968 return sprintf(page, "\n");
1969 return sprintf(page, "%s\n", dhchap_secret);
1972 static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
1973 const char *page, size_t count)
1975 struct nvmet_host *host = to_host(item);
1978 ret = nvmet_auth_set_key(host, page, false);
1980 * Re-authentication is a soft state, so keep the
1981 * current authentication valid until the host
1982 * requests re-authentication.
1984 return ret < 0 ? ret : count;
1987 CONFIGFS_ATTR(nvmet_host_, dhchap_key);
1989 static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
1992 u8 *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
1995 return sprintf(page, "\n");
1996 return sprintf(page, "%s\n", dhchap_secret);
1999 static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
2000 const char *page, size_t count)
2002 struct nvmet_host *host = to_host(item);
2005 ret = nvmet_auth_set_key(host, page, true);
2007 * Re-authentication is a soft state, so keep the
2008 * current authentication valid until the host
2009 * requests re-authentication.
2011 return ret < 0 ? ret : count;
2014 CONFIGFS_ATTR(nvmet_host_, dhchap_ctrl_key);
2016 static ssize_t nvmet_host_dhchap_hash_show(struct config_item *item,
2019 struct nvmet_host *host = to_host(item);
2020 const char *hash_name = nvme_auth_hmac_name(host->dhchap_hash_id);
2022 return sprintf(page, "%s\n", hash_name ? hash_name : "none");
2025 static ssize_t nvmet_host_dhchap_hash_store(struct config_item *item,
2026 const char *page, size_t count)
2028 struct nvmet_host *host = to_host(item);
2031 hmac_id = nvme_auth_hmac_id(page);
2032 if (hmac_id == NVME_AUTH_HASH_INVALID)
2034 if (!crypto_has_shash(nvme_auth_hmac_name(hmac_id), 0, 0))
2036 host->dhchap_hash_id = hmac_id;
2040 CONFIGFS_ATTR(nvmet_host_, dhchap_hash);
2042 static ssize_t nvmet_host_dhchap_dhgroup_show(struct config_item *item,
2045 struct nvmet_host *host = to_host(item);
2046 const char *dhgroup = nvme_auth_dhgroup_name(host->dhchap_dhgroup_id);
2048 return sprintf(page, "%s\n", dhgroup ? dhgroup : "none");
2051 static ssize_t nvmet_host_dhchap_dhgroup_store(struct config_item *item,
2052 const char *page, size_t count)
2054 struct nvmet_host *host = to_host(item);
2057 dhgroup_id = nvme_auth_dhgroup_id(page);
2058 if (dhgroup_id == NVME_AUTH_DHGROUP_INVALID)
2060 if (dhgroup_id != NVME_AUTH_DHGROUP_NULL) {
2061 const char *kpp = nvme_auth_dhgroup_kpp(dhgroup_id);
2063 if (!crypto_has_kpp(kpp, 0, 0))
2066 host->dhchap_dhgroup_id = dhgroup_id;
2070 CONFIGFS_ATTR(nvmet_host_, dhchap_dhgroup);
2072 static struct configfs_attribute *nvmet_host_attrs[] = {
2073 &nvmet_host_attr_dhchap_key,
2074 &nvmet_host_attr_dhchap_ctrl_key,
2075 &nvmet_host_attr_dhchap_hash,
2076 &nvmet_host_attr_dhchap_dhgroup,
2079 #endif /* CONFIG_NVME_TARGET_AUTH */
2081 static void nvmet_host_release(struct config_item *item)
2083 struct nvmet_host *host = to_host(item);
2085 #ifdef CONFIG_NVME_TARGET_AUTH
2086 kfree(host->dhchap_secret);
2087 kfree(host->dhchap_ctrl_secret);
2092 static struct configfs_item_operations nvmet_host_item_ops = {
2093 .release = nvmet_host_release,
2096 static const struct config_item_type nvmet_host_type = {
2097 .ct_item_ops = &nvmet_host_item_ops,
2098 #ifdef CONFIG_NVME_TARGET_AUTH
2099 .ct_attrs = nvmet_host_attrs,
2101 .ct_owner = THIS_MODULE,
2104 static struct config_group *nvmet_hosts_make_group(struct config_group *group,
2107 struct nvmet_host *host;
2109 host = kzalloc(sizeof(*host), GFP_KERNEL);
2111 return ERR_PTR(-ENOMEM);
2113 #ifdef CONFIG_NVME_TARGET_AUTH
2114 /* Default to SHA256 */
2115 host->dhchap_hash_id = NVME_AUTH_HASH_SHA256;
2118 config_group_init_type_name(&host->group, name, &nvmet_host_type);
2120 return &host->group;
2123 static struct configfs_group_operations nvmet_hosts_group_ops = {
2124 .make_group = nvmet_hosts_make_group,
2127 static const struct config_item_type nvmet_hosts_type = {
2128 .ct_group_ops = &nvmet_hosts_group_ops,
2129 .ct_owner = THIS_MODULE,
2132 static struct config_group nvmet_hosts_group;
2134 static const struct config_item_type nvmet_root_type = {
2135 .ct_owner = THIS_MODULE,
2138 static struct configfs_subsystem nvmet_configfs_subsystem = {
2141 .ci_namebuf = "nvmet",
2142 .ci_type = &nvmet_root_type,
2147 int __init nvmet_init_configfs(void)
2151 config_group_init(&nvmet_configfs_subsystem.su_group);
2152 mutex_init(&nvmet_configfs_subsystem.su_mutex);
2154 config_group_init_type_name(&nvmet_subsystems_group,
2155 "subsystems", &nvmet_subsystems_type);
2156 configfs_add_default_group(&nvmet_subsystems_group,
2157 &nvmet_configfs_subsystem.su_group);
2159 config_group_init_type_name(&nvmet_ports_group,
2160 "ports", &nvmet_ports_type);
2161 configfs_add_default_group(&nvmet_ports_group,
2162 &nvmet_configfs_subsystem.su_group);
2164 config_group_init_type_name(&nvmet_hosts_group,
2165 "hosts", &nvmet_hosts_type);
2166 configfs_add_default_group(&nvmet_hosts_group,
2167 &nvmet_configfs_subsystem.su_group);
2169 ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
2171 pr_err("configfs_register_subsystem: %d\n", ret);
2178 void __exit nvmet_exit_configfs(void)
2180 configfs_unregister_subsystem(&nvmet_configfs_subsystem);