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 <crypto/hash.h>
19 #include <crypto/kpp.h>
23 static const struct config_item_type nvmet_host_type;
24 static const struct config_item_type nvmet_subsys_type;
26 static LIST_HEAD(nvmet_ports_list);
27 struct list_head *nvmet_ports = &nvmet_ports_list;
29 struct nvmet_type_name_map {
34 static struct nvmet_type_name_map nvmet_transport[] = {
35 { NVMF_TRTYPE_RDMA, "rdma" },
36 { NVMF_TRTYPE_FC, "fc" },
37 { NVMF_TRTYPE_TCP, "tcp" },
38 { NVMF_TRTYPE_LOOP, "loop" },
41 static const struct nvmet_type_name_map nvmet_addr_family[] = {
42 { NVMF_ADDR_FAMILY_PCI, "pcie" },
43 { NVMF_ADDR_FAMILY_IP4, "ipv4" },
44 { NVMF_ADDR_FAMILY_IP6, "ipv6" },
45 { NVMF_ADDR_FAMILY_IB, "ib" },
46 { NVMF_ADDR_FAMILY_FC, "fc" },
47 { NVMF_ADDR_FAMILY_LOOP, "loop" },
50 static bool nvmet_is_port_enabled(struct nvmet_port *p, const char *caller)
53 pr_err("Disable port '%u' before changing attribute in %s\n",
54 le16_to_cpu(p->disc_addr.portid), caller);
59 * nvmet_port Generic ConfigFS definitions.
60 * Used in any place in the ConfigFS tree that refers to an address.
62 static ssize_t nvmet_addr_adrfam_show(struct config_item *item, char *page)
64 u8 adrfam = to_nvmet_port(item)->disc_addr.adrfam;
67 for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
68 if (nvmet_addr_family[i].type == adrfam)
69 return snprintf(page, PAGE_SIZE, "%s\n",
70 nvmet_addr_family[i].name);
73 return snprintf(page, PAGE_SIZE, "\n");
76 static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
77 const char *page, size_t count)
79 struct nvmet_port *port = to_nvmet_port(item);
82 if (nvmet_is_port_enabled(port, __func__))
85 for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
86 if (sysfs_streq(page, nvmet_addr_family[i].name))
90 pr_err("Invalid value '%s' for adrfam\n", page);
94 port->disc_addr.adrfam = nvmet_addr_family[i].type;
98 CONFIGFS_ATTR(nvmet_, addr_adrfam);
100 static ssize_t nvmet_addr_portid_show(struct config_item *item,
103 __le16 portid = to_nvmet_port(item)->disc_addr.portid;
105 return snprintf(page, PAGE_SIZE, "%d\n", le16_to_cpu(portid));
108 static ssize_t nvmet_addr_portid_store(struct config_item *item,
109 const char *page, size_t count)
111 struct nvmet_port *port = to_nvmet_port(item);
114 if (kstrtou16(page, 0, &portid)) {
115 pr_err("Invalid value '%s' for portid\n", page);
119 if (nvmet_is_port_enabled(port, __func__))
122 port->disc_addr.portid = cpu_to_le16(portid);
126 CONFIGFS_ATTR(nvmet_, addr_portid);
128 static ssize_t nvmet_addr_traddr_show(struct config_item *item,
131 struct nvmet_port *port = to_nvmet_port(item);
133 return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.traddr);
136 static ssize_t nvmet_addr_traddr_store(struct config_item *item,
137 const char *page, size_t count)
139 struct nvmet_port *port = to_nvmet_port(item);
141 if (count > NVMF_TRADDR_SIZE) {
142 pr_err("Invalid value '%s' for traddr\n", page);
146 if (nvmet_is_port_enabled(port, __func__))
149 if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
154 CONFIGFS_ATTR(nvmet_, addr_traddr);
156 static const struct nvmet_type_name_map nvmet_addr_treq[] = {
157 { NVMF_TREQ_NOT_SPECIFIED, "not specified" },
158 { NVMF_TREQ_REQUIRED, "required" },
159 { NVMF_TREQ_NOT_REQUIRED, "not required" },
162 static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page)
164 u8 treq = to_nvmet_port(item)->disc_addr.treq &
165 NVME_TREQ_SECURE_CHANNEL_MASK;
168 for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
169 if (treq == nvmet_addr_treq[i].type)
170 return snprintf(page, PAGE_SIZE, "%s\n",
171 nvmet_addr_treq[i].name);
174 return snprintf(page, PAGE_SIZE, "\n");
177 static ssize_t nvmet_addr_treq_store(struct config_item *item,
178 const char *page, size_t count)
180 struct nvmet_port *port = to_nvmet_port(item);
181 u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK;
184 if (nvmet_is_port_enabled(port, __func__))
187 for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
188 if (sysfs_streq(page, nvmet_addr_treq[i].name))
192 pr_err("Invalid value '%s' for treq\n", page);
196 treq |= nvmet_addr_treq[i].type;
197 port->disc_addr.treq = treq;
201 CONFIGFS_ATTR(nvmet_, addr_treq);
203 static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
206 struct nvmet_port *port = to_nvmet_port(item);
208 return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.trsvcid);
211 static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
212 const char *page, size_t count)
214 struct nvmet_port *port = to_nvmet_port(item);
216 if (count > NVMF_TRSVCID_SIZE) {
217 pr_err("Invalid value '%s' for trsvcid\n", page);
220 if (nvmet_is_port_enabled(port, __func__))
223 if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
228 CONFIGFS_ATTR(nvmet_, addr_trsvcid);
230 static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
233 struct nvmet_port *port = to_nvmet_port(item);
235 return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
238 static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
239 const char *page, size_t count)
241 struct nvmet_port *port = to_nvmet_port(item);
244 if (nvmet_is_port_enabled(port, __func__))
246 ret = kstrtoint(page, 0, &port->inline_data_size);
248 pr_err("Invalid value '%s' for inline_data_size\n", page);
254 CONFIGFS_ATTR(nvmet_, param_inline_data_size);
256 #ifdef CONFIG_BLK_DEV_INTEGRITY
257 static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
260 struct nvmet_port *port = to_nvmet_port(item);
262 return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
265 static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
266 const char *page, size_t count)
268 struct nvmet_port *port = to_nvmet_port(item);
271 if (kstrtobool(page, &val))
274 if (nvmet_is_port_enabled(port, __func__))
277 port->pi_enable = val;
281 CONFIGFS_ATTR(nvmet_, param_pi_enable);
284 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
287 struct nvmet_port *port = to_nvmet_port(item);
290 for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
291 if (port->disc_addr.trtype == nvmet_transport[i].type)
292 return snprintf(page, PAGE_SIZE,
293 "%s\n", nvmet_transport[i].name);
296 return sprintf(page, "\n");
299 static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
301 port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
302 port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
303 port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
306 static ssize_t nvmet_addr_trtype_store(struct config_item *item,
307 const char *page, size_t count)
309 struct nvmet_port *port = to_nvmet_port(item);
312 if (nvmet_is_port_enabled(port, __func__))
315 for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
316 if (sysfs_streq(page, nvmet_transport[i].name))
320 pr_err("Invalid value '%s' for trtype\n", page);
324 memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
325 port->disc_addr.trtype = nvmet_transport[i].type;
326 if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
327 nvmet_port_init_tsas_rdma(port);
331 CONFIGFS_ATTR(nvmet_, addr_trtype);
334 * Namespace structures & file operation functions below
336 static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
338 return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
341 static ssize_t nvmet_ns_device_path_store(struct config_item *item,
342 const char *page, size_t count)
344 struct nvmet_ns *ns = to_nvmet_ns(item);
345 struct nvmet_subsys *subsys = ns->subsys;
349 mutex_lock(&subsys->lock);
355 len = strcspn(page, "\n");
359 kfree(ns->device_path);
361 ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
362 if (!ns->device_path)
365 mutex_unlock(&subsys->lock);
369 mutex_unlock(&subsys->lock);
373 CONFIGFS_ATTR(nvmet_ns_, device_path);
375 #ifdef CONFIG_PCI_P2PDMA
376 static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
378 struct nvmet_ns *ns = to_nvmet_ns(item);
380 return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
383 static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
384 const char *page, size_t count)
386 struct nvmet_ns *ns = to_nvmet_ns(item);
387 struct pci_dev *p2p_dev = NULL;
392 mutex_lock(&ns->subsys->lock);
398 error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
404 ns->use_p2pmem = use_p2pmem;
405 pci_dev_put(ns->p2p_dev);
406 ns->p2p_dev = p2p_dev;
409 mutex_unlock(&ns->subsys->lock);
414 CONFIGFS_ATTR(nvmet_ns_, p2pmem);
415 #endif /* CONFIG_PCI_P2PDMA */
417 static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
419 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
422 static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
423 const char *page, size_t count)
425 struct nvmet_ns *ns = to_nvmet_ns(item);
426 struct nvmet_subsys *subsys = ns->subsys;
429 mutex_lock(&subsys->lock);
435 if (uuid_parse(page, &ns->uuid))
439 mutex_unlock(&subsys->lock);
440 return ret ? ret : count;
443 CONFIGFS_ATTR(nvmet_ns_, device_uuid);
445 static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
447 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
450 static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
451 const char *page, size_t count)
453 struct nvmet_ns *ns = to_nvmet_ns(item);
454 struct nvmet_subsys *subsys = ns->subsys;
456 const char *p = page;
460 mutex_lock(&subsys->lock);
466 for (i = 0; i < 16; i++) {
467 if (p + 2 > page + count) {
471 if (!isxdigit(p[0]) || !isxdigit(p[1])) {
476 nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
479 if (*p == '-' || *p == ':')
483 memcpy(&ns->nguid, nguid, sizeof(nguid));
485 mutex_unlock(&subsys->lock);
486 return ret ? ret : count;
489 CONFIGFS_ATTR(nvmet_ns_, device_nguid);
491 static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
493 return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
496 static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
497 const char *page, size_t count)
499 struct nvmet_ns *ns = to_nvmet_ns(item);
500 u32 oldgrpid, newgrpid;
503 ret = kstrtou32(page, 0, &newgrpid);
507 if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
510 down_write(&nvmet_ana_sem);
511 oldgrpid = ns->anagrpid;
512 nvmet_ana_group_enabled[newgrpid]++;
513 ns->anagrpid = newgrpid;
514 nvmet_ana_group_enabled[oldgrpid]--;
516 up_write(&nvmet_ana_sem);
518 nvmet_send_ana_event(ns->subsys, NULL);
522 CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
524 static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
526 return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
529 static ssize_t nvmet_ns_enable_store(struct config_item *item,
530 const char *page, size_t count)
532 struct nvmet_ns *ns = to_nvmet_ns(item);
536 if (kstrtobool(page, &enable))
540 ret = nvmet_ns_enable(ns);
542 nvmet_ns_disable(ns);
544 return ret ? ret : count;
547 CONFIGFS_ATTR(nvmet_ns_, enable);
549 static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
551 return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
554 static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
555 const char *page, size_t count)
557 struct nvmet_ns *ns = to_nvmet_ns(item);
560 if (kstrtobool(page, &val))
563 mutex_lock(&ns->subsys->lock);
565 pr_err("disable ns before setting buffered_io value.\n");
566 mutex_unlock(&ns->subsys->lock);
570 ns->buffered_io = val;
571 mutex_unlock(&ns->subsys->lock);
575 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
577 static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
578 const char *page, size_t count)
580 struct nvmet_ns *ns = to_nvmet_ns(item);
583 if (kstrtobool(page, &val))
589 mutex_lock(&ns->subsys->lock);
591 pr_err("enable ns before revalidate.\n");
592 mutex_unlock(&ns->subsys->lock);
595 if (nvmet_ns_revalidate(ns))
596 nvmet_ns_changed(ns->subsys, ns->nsid);
597 mutex_unlock(&ns->subsys->lock);
601 CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
603 static struct configfs_attribute *nvmet_ns_attrs[] = {
604 &nvmet_ns_attr_device_path,
605 &nvmet_ns_attr_device_nguid,
606 &nvmet_ns_attr_device_uuid,
607 &nvmet_ns_attr_ana_grpid,
608 &nvmet_ns_attr_enable,
609 &nvmet_ns_attr_buffered_io,
610 &nvmet_ns_attr_revalidate_size,
611 #ifdef CONFIG_PCI_P2PDMA
612 &nvmet_ns_attr_p2pmem,
617 static void nvmet_ns_release(struct config_item *item)
619 struct nvmet_ns *ns = to_nvmet_ns(item);
624 static struct configfs_item_operations nvmet_ns_item_ops = {
625 .release = nvmet_ns_release,
628 static const struct config_item_type nvmet_ns_type = {
629 .ct_item_ops = &nvmet_ns_item_ops,
630 .ct_attrs = nvmet_ns_attrs,
631 .ct_owner = THIS_MODULE,
634 static struct config_group *nvmet_ns_make(struct config_group *group,
637 struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
642 ret = kstrtou32(name, 0, &nsid);
647 if (nsid == 0 || nsid == NVME_NSID_ALL) {
648 pr_err("invalid nsid %#x", nsid);
653 ns = nvmet_ns_alloc(subsys, nsid);
656 config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
658 pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
665 static struct configfs_group_operations nvmet_namespaces_group_ops = {
666 .make_group = nvmet_ns_make,
669 static const struct config_item_type nvmet_namespaces_type = {
670 .ct_group_ops = &nvmet_namespaces_group_ops,
671 .ct_owner = THIS_MODULE,
674 #ifdef CONFIG_NVME_TARGET_PASSTHRU
676 static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
679 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
681 return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
684 static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
685 const char *page, size_t count)
687 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
691 mutex_lock(&subsys->lock);
694 if (subsys->passthru_ctrl)
698 len = strcspn(page, "\n");
702 kfree(subsys->passthru_ctrl_path);
704 subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
705 if (!subsys->passthru_ctrl_path)
708 mutex_unlock(&subsys->lock);
712 mutex_unlock(&subsys->lock);
715 CONFIGFS_ATTR(nvmet_passthru_, device_path);
717 static ssize_t nvmet_passthru_enable_show(struct config_item *item,
720 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
722 return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
725 static ssize_t nvmet_passthru_enable_store(struct config_item *item,
726 const char *page, size_t count)
728 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
732 if (kstrtobool(page, &enable))
736 ret = nvmet_passthru_ctrl_enable(subsys);
738 nvmet_passthru_ctrl_disable(subsys);
740 return ret ? ret : count;
742 CONFIGFS_ATTR(nvmet_passthru_, enable);
744 static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
747 return sprintf(page, "%u\n", to_subsys(item->ci_parent)->admin_timeout);
750 static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
751 const char *page, size_t count)
753 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
754 unsigned int timeout;
756 if (kstrtouint(page, 0, &timeout))
758 subsys->admin_timeout = timeout;
761 CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
763 static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
766 return sprintf(page, "%u\n", to_subsys(item->ci_parent)->io_timeout);
769 static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
770 const char *page, size_t count)
772 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
773 unsigned int timeout;
775 if (kstrtouint(page, 0, &timeout))
777 subsys->io_timeout = timeout;
780 CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
782 static ssize_t nvmet_passthru_clear_ids_show(struct config_item *item,
785 return sprintf(page, "%u\n", to_subsys(item->ci_parent)->clear_ids);
788 static ssize_t nvmet_passthru_clear_ids_store(struct config_item *item,
789 const char *page, size_t count)
791 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
792 unsigned int clear_ids;
794 if (kstrtouint(page, 0, &clear_ids))
796 subsys->clear_ids = clear_ids;
799 CONFIGFS_ATTR(nvmet_passthru_, clear_ids);
801 static struct configfs_attribute *nvmet_passthru_attrs[] = {
802 &nvmet_passthru_attr_device_path,
803 &nvmet_passthru_attr_enable,
804 &nvmet_passthru_attr_admin_timeout,
805 &nvmet_passthru_attr_io_timeout,
806 &nvmet_passthru_attr_clear_ids,
810 static const struct config_item_type nvmet_passthru_type = {
811 .ct_attrs = nvmet_passthru_attrs,
812 .ct_owner = THIS_MODULE,
815 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
817 config_group_init_type_name(&subsys->passthru_group,
818 "passthru", &nvmet_passthru_type);
819 configfs_add_default_group(&subsys->passthru_group,
823 #else /* CONFIG_NVME_TARGET_PASSTHRU */
825 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
829 #endif /* CONFIG_NVME_TARGET_PASSTHRU */
831 static int nvmet_port_subsys_allow_link(struct config_item *parent,
832 struct config_item *target)
834 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
835 struct nvmet_subsys *subsys;
836 struct nvmet_subsys_link *link, *p;
839 if (target->ci_type != &nvmet_subsys_type) {
840 pr_err("can only link subsystems into the subsystems dir.!\n");
843 subsys = to_subsys(target);
844 link = kmalloc(sizeof(*link), GFP_KERNEL);
847 link->subsys = subsys;
849 down_write(&nvmet_config_sem);
851 list_for_each_entry(p, &port->subsystems, entry) {
852 if (p->subsys == subsys)
856 if (list_empty(&port->subsystems)) {
857 ret = nvmet_enable_port(port);
862 list_add_tail(&link->entry, &port->subsystems);
863 nvmet_port_disc_changed(port, subsys);
865 up_write(&nvmet_config_sem);
869 up_write(&nvmet_config_sem);
874 static void nvmet_port_subsys_drop_link(struct config_item *parent,
875 struct config_item *target)
877 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
878 struct nvmet_subsys *subsys = to_subsys(target);
879 struct nvmet_subsys_link *p;
881 down_write(&nvmet_config_sem);
882 list_for_each_entry(p, &port->subsystems, entry) {
883 if (p->subsys == subsys)
886 up_write(&nvmet_config_sem);
891 nvmet_port_del_ctrls(port, subsys);
892 nvmet_port_disc_changed(port, subsys);
894 if (list_empty(&port->subsystems))
895 nvmet_disable_port(port);
896 up_write(&nvmet_config_sem);
900 static struct configfs_item_operations nvmet_port_subsys_item_ops = {
901 .allow_link = nvmet_port_subsys_allow_link,
902 .drop_link = nvmet_port_subsys_drop_link,
905 static const struct config_item_type nvmet_port_subsys_type = {
906 .ct_item_ops = &nvmet_port_subsys_item_ops,
907 .ct_owner = THIS_MODULE,
910 static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
911 struct config_item *target)
913 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
914 struct nvmet_host *host;
915 struct nvmet_host_link *link, *p;
918 if (target->ci_type != &nvmet_host_type) {
919 pr_err("can only link hosts into the allowed_hosts directory!\n");
923 host = to_host(target);
924 link = kmalloc(sizeof(*link), GFP_KERNEL);
929 down_write(&nvmet_config_sem);
931 if (subsys->allow_any_host) {
932 pr_err("can't add hosts when allow_any_host is set!\n");
937 list_for_each_entry(p, &subsys->hosts, entry) {
938 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
941 list_add_tail(&link->entry, &subsys->hosts);
942 nvmet_subsys_disc_changed(subsys, host);
944 up_write(&nvmet_config_sem);
947 up_write(&nvmet_config_sem);
952 static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
953 struct config_item *target)
955 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
956 struct nvmet_host *host = to_host(target);
957 struct nvmet_host_link *p;
959 down_write(&nvmet_config_sem);
960 list_for_each_entry(p, &subsys->hosts, entry) {
961 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
964 up_write(&nvmet_config_sem);
969 nvmet_subsys_disc_changed(subsys, host);
971 up_write(&nvmet_config_sem);
975 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
976 .allow_link = nvmet_allowed_hosts_allow_link,
977 .drop_link = nvmet_allowed_hosts_drop_link,
980 static const struct config_item_type nvmet_allowed_hosts_type = {
981 .ct_item_ops = &nvmet_allowed_hosts_item_ops,
982 .ct_owner = THIS_MODULE,
985 static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
988 return snprintf(page, PAGE_SIZE, "%d\n",
989 to_subsys(item)->allow_any_host);
992 static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
993 const char *page, size_t count)
995 struct nvmet_subsys *subsys = to_subsys(item);
999 if (kstrtobool(page, &allow_any_host))
1002 down_write(&nvmet_config_sem);
1003 if (allow_any_host && !list_empty(&subsys->hosts)) {
1004 pr_err("Can't set allow_any_host when explicit hosts are set!\n");
1009 if (subsys->allow_any_host != allow_any_host) {
1010 subsys->allow_any_host = allow_any_host;
1011 nvmet_subsys_disc_changed(subsys, NULL);
1015 up_write(&nvmet_config_sem);
1016 return ret ? ret : count;
1019 CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
1021 static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
1024 struct nvmet_subsys *subsys = to_subsys(item);
1026 if (NVME_TERTIARY(subsys->ver))
1027 return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
1028 NVME_MAJOR(subsys->ver),
1029 NVME_MINOR(subsys->ver),
1030 NVME_TERTIARY(subsys->ver));
1032 return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
1033 NVME_MAJOR(subsys->ver),
1034 NVME_MINOR(subsys->ver));
1038 nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,
1039 const char *page, size_t count)
1041 int major, minor, tertiary = 0;
1044 if (subsys->subsys_discovered) {
1045 if (NVME_TERTIARY(subsys->ver))
1046 pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
1047 NVME_MAJOR(subsys->ver),
1048 NVME_MINOR(subsys->ver),
1049 NVME_TERTIARY(subsys->ver));
1051 pr_err("Can't set version number. %llu.%llu is already assigned\n",
1052 NVME_MAJOR(subsys->ver),
1053 NVME_MINOR(subsys->ver));
1057 /* passthru subsystems use the underlying controller's version */
1058 if (nvmet_is_passthru_subsys(subsys))
1061 ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
1062 if (ret != 2 && ret != 3)
1065 subsys->ver = NVME_VS(major, minor, tertiary);
1070 static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
1071 const char *page, size_t count)
1073 struct nvmet_subsys *subsys = to_subsys(item);
1076 down_write(&nvmet_config_sem);
1077 mutex_lock(&subsys->lock);
1078 ret = nvmet_subsys_attr_version_store_locked(subsys, page, count);
1079 mutex_unlock(&subsys->lock);
1080 up_write(&nvmet_config_sem);
1084 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
1086 /* See Section 1.5 of NVMe 1.4 */
1087 static bool nvmet_is_ascii(const char c)
1089 return c >= 0x20 && c <= 0x7e;
1092 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
1095 struct nvmet_subsys *subsys = to_subsys(item);
1097 return snprintf(page, PAGE_SIZE, "%.*s\n",
1098 NVMET_SN_MAX_SIZE, subsys->serial);
1102 nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
1103 const char *page, size_t count)
1105 int pos, len = strcspn(page, "\n");
1107 if (subsys->subsys_discovered) {
1108 pr_err("Can't set serial number. %s is already assigned\n",
1113 if (!len || len > NVMET_SN_MAX_SIZE) {
1114 pr_err("Serial Number can not be empty or exceed %d Bytes\n",
1119 for (pos = 0; pos < len; pos++) {
1120 if (!nvmet_is_ascii(page[pos])) {
1121 pr_err("Serial Number must contain only ASCII strings\n");
1126 memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
1131 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
1132 const char *page, size_t count)
1134 struct nvmet_subsys *subsys = to_subsys(item);
1137 down_write(&nvmet_config_sem);
1138 mutex_lock(&subsys->lock);
1139 ret = nvmet_subsys_attr_serial_store_locked(subsys, page, count);
1140 mutex_unlock(&subsys->lock);
1141 up_write(&nvmet_config_sem);
1145 CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
1147 static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
1150 return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
1153 static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
1154 const char *page, size_t cnt)
1158 if (sscanf(page, "%hu\n", &cntlid_min) != 1)
1161 if (cntlid_min == 0)
1164 down_write(&nvmet_config_sem);
1165 if (cntlid_min >= to_subsys(item)->cntlid_max)
1167 to_subsys(item)->cntlid_min = cntlid_min;
1168 up_write(&nvmet_config_sem);
1172 up_write(&nvmet_config_sem);
1175 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
1177 static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
1180 return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
1183 static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
1184 const char *page, size_t cnt)
1188 if (sscanf(page, "%hu\n", &cntlid_max) != 1)
1191 if (cntlid_max == 0)
1194 down_write(&nvmet_config_sem);
1195 if (cntlid_max <= to_subsys(item)->cntlid_min)
1197 to_subsys(item)->cntlid_max = cntlid_max;
1198 up_write(&nvmet_config_sem);
1202 up_write(&nvmet_config_sem);
1205 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
1207 static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
1210 struct nvmet_subsys *subsys = to_subsys(item);
1212 return snprintf(page, PAGE_SIZE, "%s\n", subsys->model_number);
1215 static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
1216 const char *page, size_t count)
1221 if (subsys->subsys_discovered) {
1222 pr_err("Can't set model number. %s is already assigned\n",
1223 subsys->model_number);
1227 len = strcspn(page, "\n");
1231 if (len > NVMET_MN_MAX_SIZE) {
1232 pr_err("Model number size can not exceed %d Bytes\n",
1237 for (pos = 0; pos < len; pos++) {
1238 if (!nvmet_is_ascii(page[pos]))
1242 val = kmemdup_nul(page, len, GFP_KERNEL);
1245 kfree(subsys->model_number);
1246 subsys->model_number = val;
1250 static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
1251 const char *page, size_t count)
1253 struct nvmet_subsys *subsys = to_subsys(item);
1256 down_write(&nvmet_config_sem);
1257 mutex_lock(&subsys->lock);
1258 ret = nvmet_subsys_attr_model_store_locked(subsys, page, count);
1259 mutex_unlock(&subsys->lock);
1260 up_write(&nvmet_config_sem);
1264 CONFIGFS_ATTR(nvmet_subsys_, attr_model);
1266 static ssize_t nvmet_subsys_attr_ieee_oui_show(struct config_item *item,
1269 struct nvmet_subsys *subsys = to_subsys(item);
1271 return sysfs_emit(page, "0x%06x\n", subsys->ieee_oui);
1274 static ssize_t nvmet_subsys_attr_ieee_oui_store_locked(struct nvmet_subsys *subsys,
1275 const char *page, size_t count)
1280 if (subsys->subsys_discovered) {
1281 pr_err("Can't set IEEE OUI. 0x%06x is already assigned\n",
1286 ret = kstrtou32(page, 0, &val);
1290 if (val >= 0x1000000)
1293 subsys->ieee_oui = val;
1298 static ssize_t nvmet_subsys_attr_ieee_oui_store(struct config_item *item,
1299 const char *page, size_t count)
1301 struct nvmet_subsys *subsys = to_subsys(item);
1304 down_write(&nvmet_config_sem);
1305 mutex_lock(&subsys->lock);
1306 ret = nvmet_subsys_attr_ieee_oui_store_locked(subsys, page, count);
1307 mutex_unlock(&subsys->lock);
1308 up_write(&nvmet_config_sem);
1312 CONFIGFS_ATTR(nvmet_subsys_, attr_ieee_oui);
1314 static ssize_t nvmet_subsys_attr_firmware_show(struct config_item *item,
1317 struct nvmet_subsys *subsys = to_subsys(item);
1319 return sysfs_emit(page, "%s\n", subsys->firmware_rev);
1322 static ssize_t nvmet_subsys_attr_firmware_store_locked(struct nvmet_subsys *subsys,
1323 const char *page, size_t count)
1328 if (subsys->subsys_discovered) {
1329 pr_err("Can't set firmware revision. %s is already assigned\n",
1330 subsys->firmware_rev);
1334 len = strcspn(page, "\n");
1338 if (len > NVMET_FR_MAX_SIZE) {
1339 pr_err("Firmware revision size can not exceed %d Bytes\n",
1344 for (pos = 0; pos < len; pos++) {
1345 if (!nvmet_is_ascii(page[pos]))
1349 val = kmemdup_nul(page, len, GFP_KERNEL);
1353 kfree(subsys->firmware_rev);
1355 subsys->firmware_rev = val;
1360 static ssize_t nvmet_subsys_attr_firmware_store(struct config_item *item,
1361 const char *page, size_t count)
1363 struct nvmet_subsys *subsys = to_subsys(item);
1366 down_write(&nvmet_config_sem);
1367 mutex_lock(&subsys->lock);
1368 ret = nvmet_subsys_attr_firmware_store_locked(subsys, page, count);
1369 mutex_unlock(&subsys->lock);
1370 up_write(&nvmet_config_sem);
1374 CONFIGFS_ATTR(nvmet_subsys_, attr_firmware);
1376 #ifdef CONFIG_BLK_DEV_INTEGRITY
1377 static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
1380 return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
1383 static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
1384 const char *page, size_t count)
1386 struct nvmet_subsys *subsys = to_subsys(item);
1389 if (kstrtobool(page, &pi_enable))
1392 subsys->pi_support = pi_enable;
1395 CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
1398 static ssize_t nvmet_subsys_attr_qid_max_show(struct config_item *item,
1401 return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->max_qid);
1404 static ssize_t nvmet_subsys_attr_qid_max_store(struct config_item *item,
1405 const char *page, size_t cnt)
1407 struct nvmet_subsys *subsys = to_subsys(item);
1408 struct nvmet_ctrl *ctrl;
1411 if (sscanf(page, "%hu\n", &qid_max) != 1)
1414 if (qid_max < 1 || qid_max > NVMET_NR_QUEUES)
1417 down_write(&nvmet_config_sem);
1418 subsys->max_qid = qid_max;
1420 /* Force reconnect */
1421 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1422 ctrl->ops->delete_ctrl(ctrl);
1423 up_write(&nvmet_config_sem);
1427 CONFIGFS_ATTR(nvmet_subsys_, attr_qid_max);
1429 static struct configfs_attribute *nvmet_subsys_attrs[] = {
1430 &nvmet_subsys_attr_attr_allow_any_host,
1431 &nvmet_subsys_attr_attr_version,
1432 &nvmet_subsys_attr_attr_serial,
1433 &nvmet_subsys_attr_attr_cntlid_min,
1434 &nvmet_subsys_attr_attr_cntlid_max,
1435 &nvmet_subsys_attr_attr_model,
1436 &nvmet_subsys_attr_attr_qid_max,
1437 &nvmet_subsys_attr_attr_ieee_oui,
1438 &nvmet_subsys_attr_attr_firmware,
1439 #ifdef CONFIG_BLK_DEV_INTEGRITY
1440 &nvmet_subsys_attr_attr_pi_enable,
1446 * Subsystem structures & folder operation functions below
1448 static void nvmet_subsys_release(struct config_item *item)
1450 struct nvmet_subsys *subsys = to_subsys(item);
1452 nvmet_subsys_del_ctrls(subsys);
1453 nvmet_subsys_put(subsys);
1456 static struct configfs_item_operations nvmet_subsys_item_ops = {
1457 .release = nvmet_subsys_release,
1460 static const struct config_item_type nvmet_subsys_type = {
1461 .ct_item_ops = &nvmet_subsys_item_ops,
1462 .ct_attrs = nvmet_subsys_attrs,
1463 .ct_owner = THIS_MODULE,
1466 static struct config_group *nvmet_subsys_make(struct config_group *group,
1469 struct nvmet_subsys *subsys;
1471 if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1472 pr_err("can't create discovery subsystem through configfs\n");
1473 return ERR_PTR(-EINVAL);
1476 subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
1478 return ERR_CAST(subsys);
1480 config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1482 config_group_init_type_name(&subsys->namespaces_group,
1483 "namespaces", &nvmet_namespaces_type);
1484 configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1486 config_group_init_type_name(&subsys->allowed_hosts_group,
1487 "allowed_hosts", &nvmet_allowed_hosts_type);
1488 configfs_add_default_group(&subsys->allowed_hosts_group,
1491 nvmet_add_passthru_group(subsys);
1493 return &subsys->group;
1496 static struct configfs_group_operations nvmet_subsystems_group_ops = {
1497 .make_group = nvmet_subsys_make,
1500 static const struct config_item_type nvmet_subsystems_type = {
1501 .ct_group_ops = &nvmet_subsystems_group_ops,
1502 .ct_owner = THIS_MODULE,
1505 static ssize_t nvmet_referral_enable_show(struct config_item *item,
1508 return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1511 static ssize_t nvmet_referral_enable_store(struct config_item *item,
1512 const char *page, size_t count)
1514 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1515 struct nvmet_port *port = to_nvmet_port(item);
1518 if (kstrtobool(page, &enable))
1522 nvmet_referral_enable(parent, port);
1524 nvmet_referral_disable(parent, port);
1528 pr_err("Invalid value '%s' for enable\n", page);
1532 CONFIGFS_ATTR(nvmet_referral_, enable);
1535 * Discovery Service subsystem definitions
1537 static struct configfs_attribute *nvmet_referral_attrs[] = {
1538 &nvmet_attr_addr_adrfam,
1539 &nvmet_attr_addr_portid,
1540 &nvmet_attr_addr_treq,
1541 &nvmet_attr_addr_traddr,
1542 &nvmet_attr_addr_trsvcid,
1543 &nvmet_attr_addr_trtype,
1544 &nvmet_referral_attr_enable,
1548 static void nvmet_referral_notify(struct config_group *group,
1549 struct config_item *item)
1551 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1552 struct nvmet_port *port = to_nvmet_port(item);
1554 nvmet_referral_disable(parent, port);
1557 static void nvmet_referral_release(struct config_item *item)
1559 struct nvmet_port *port = to_nvmet_port(item);
1564 static struct configfs_item_operations nvmet_referral_item_ops = {
1565 .release = nvmet_referral_release,
1568 static const struct config_item_type nvmet_referral_type = {
1569 .ct_owner = THIS_MODULE,
1570 .ct_attrs = nvmet_referral_attrs,
1571 .ct_item_ops = &nvmet_referral_item_ops,
1574 static struct config_group *nvmet_referral_make(
1575 struct config_group *group, const char *name)
1577 struct nvmet_port *port;
1579 port = kzalloc(sizeof(*port), GFP_KERNEL);
1581 return ERR_PTR(-ENOMEM);
1583 INIT_LIST_HEAD(&port->entry);
1584 config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1586 return &port->group;
1589 static struct configfs_group_operations nvmet_referral_group_ops = {
1590 .make_group = nvmet_referral_make,
1591 .disconnect_notify = nvmet_referral_notify,
1594 static const struct config_item_type nvmet_referrals_type = {
1595 .ct_owner = THIS_MODULE,
1596 .ct_group_ops = &nvmet_referral_group_ops,
1599 static struct nvmet_type_name_map nvmet_ana_state[] = {
1600 { NVME_ANA_OPTIMIZED, "optimized" },
1601 { NVME_ANA_NONOPTIMIZED, "non-optimized" },
1602 { NVME_ANA_INACCESSIBLE, "inaccessible" },
1603 { NVME_ANA_PERSISTENT_LOSS, "persistent-loss" },
1604 { NVME_ANA_CHANGE, "change" },
1607 static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
1610 struct nvmet_ana_group *grp = to_ana_group(item);
1611 enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
1614 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1615 if (state == nvmet_ana_state[i].type)
1616 return sprintf(page, "%s\n", nvmet_ana_state[i].name);
1619 return sprintf(page, "\n");
1622 static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
1623 const char *page, size_t count)
1625 struct nvmet_ana_group *grp = to_ana_group(item);
1626 enum nvme_ana_state *ana_state = grp->port->ana_state;
1629 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1630 if (sysfs_streq(page, nvmet_ana_state[i].name))
1634 pr_err("Invalid value '%s' for ana_state\n", page);
1638 down_write(&nvmet_ana_sem);
1639 ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
1641 up_write(&nvmet_ana_sem);
1642 nvmet_port_send_ana_event(grp->port);
1646 CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
1648 static struct configfs_attribute *nvmet_ana_group_attrs[] = {
1649 &nvmet_ana_group_attr_ana_state,
1653 static void nvmet_ana_group_release(struct config_item *item)
1655 struct nvmet_ana_group *grp = to_ana_group(item);
1657 if (grp == &grp->port->ana_default_group)
1660 down_write(&nvmet_ana_sem);
1661 grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
1662 nvmet_ana_group_enabled[grp->grpid]--;
1663 up_write(&nvmet_ana_sem);
1665 nvmet_port_send_ana_event(grp->port);
1669 static struct configfs_item_operations nvmet_ana_group_item_ops = {
1670 .release = nvmet_ana_group_release,
1673 static const struct config_item_type nvmet_ana_group_type = {
1674 .ct_item_ops = &nvmet_ana_group_item_ops,
1675 .ct_attrs = nvmet_ana_group_attrs,
1676 .ct_owner = THIS_MODULE,
1679 static struct config_group *nvmet_ana_groups_make_group(
1680 struct config_group *group, const char *name)
1682 struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
1683 struct nvmet_ana_group *grp;
1687 ret = kstrtou32(name, 0, &grpid);
1692 if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
1696 grp = kzalloc(sizeof(*grp), GFP_KERNEL);
1702 down_write(&nvmet_ana_sem);
1703 nvmet_ana_group_enabled[grpid]++;
1704 up_write(&nvmet_ana_sem);
1706 nvmet_port_send_ana_event(grp->port);
1708 config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
1711 return ERR_PTR(ret);
1714 static struct configfs_group_operations nvmet_ana_groups_group_ops = {
1715 .make_group = nvmet_ana_groups_make_group,
1718 static const struct config_item_type nvmet_ana_groups_type = {
1719 .ct_group_ops = &nvmet_ana_groups_group_ops,
1720 .ct_owner = THIS_MODULE,
1724 * Ports definitions.
1726 static void nvmet_port_release(struct config_item *item)
1728 struct nvmet_port *port = to_nvmet_port(item);
1730 /* Let inflight controllers teardown complete */
1731 flush_workqueue(nvmet_wq);
1732 list_del(&port->global_entry);
1734 kfree(port->ana_state);
1738 static struct configfs_attribute *nvmet_port_attrs[] = {
1739 &nvmet_attr_addr_adrfam,
1740 &nvmet_attr_addr_treq,
1741 &nvmet_attr_addr_traddr,
1742 &nvmet_attr_addr_trsvcid,
1743 &nvmet_attr_addr_trtype,
1744 &nvmet_attr_param_inline_data_size,
1745 #ifdef CONFIG_BLK_DEV_INTEGRITY
1746 &nvmet_attr_param_pi_enable,
1751 static struct configfs_item_operations nvmet_port_item_ops = {
1752 .release = nvmet_port_release,
1755 static const struct config_item_type nvmet_port_type = {
1756 .ct_attrs = nvmet_port_attrs,
1757 .ct_item_ops = &nvmet_port_item_ops,
1758 .ct_owner = THIS_MODULE,
1761 static struct config_group *nvmet_ports_make(struct config_group *group,
1764 struct nvmet_port *port;
1768 if (kstrtou16(name, 0, &portid))
1769 return ERR_PTR(-EINVAL);
1771 port = kzalloc(sizeof(*port), GFP_KERNEL);
1773 return ERR_PTR(-ENOMEM);
1775 port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
1776 sizeof(*port->ana_state), GFP_KERNEL);
1777 if (!port->ana_state) {
1779 return ERR_PTR(-ENOMEM);
1782 for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
1783 if (i == NVMET_DEFAULT_ANA_GRPID)
1784 port->ana_state[1] = NVME_ANA_OPTIMIZED;
1786 port->ana_state[i] = NVME_ANA_INACCESSIBLE;
1789 list_add(&port->global_entry, &nvmet_ports_list);
1791 INIT_LIST_HEAD(&port->entry);
1792 INIT_LIST_HEAD(&port->subsystems);
1793 INIT_LIST_HEAD(&port->referrals);
1794 port->inline_data_size = -1; /* < 0 == let the transport choose */
1796 port->disc_addr.portid = cpu_to_le16(portid);
1797 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
1798 port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1799 config_group_init_type_name(&port->group, name, &nvmet_port_type);
1801 config_group_init_type_name(&port->subsys_group,
1802 "subsystems", &nvmet_port_subsys_type);
1803 configfs_add_default_group(&port->subsys_group, &port->group);
1805 config_group_init_type_name(&port->referrals_group,
1806 "referrals", &nvmet_referrals_type);
1807 configfs_add_default_group(&port->referrals_group, &port->group);
1809 config_group_init_type_name(&port->ana_groups_group,
1810 "ana_groups", &nvmet_ana_groups_type);
1811 configfs_add_default_group(&port->ana_groups_group, &port->group);
1813 port->ana_default_group.port = port;
1814 port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
1815 config_group_init_type_name(&port->ana_default_group.group,
1816 __stringify(NVMET_DEFAULT_ANA_GRPID),
1817 &nvmet_ana_group_type);
1818 configfs_add_default_group(&port->ana_default_group.group,
1819 &port->ana_groups_group);
1821 return &port->group;
1824 static struct configfs_group_operations nvmet_ports_group_ops = {
1825 .make_group = nvmet_ports_make,
1828 static const struct config_item_type nvmet_ports_type = {
1829 .ct_group_ops = &nvmet_ports_group_ops,
1830 .ct_owner = THIS_MODULE,
1833 static struct config_group nvmet_subsystems_group;
1834 static struct config_group nvmet_ports_group;
1836 #ifdef CONFIG_NVME_TARGET_AUTH
1837 static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
1840 u8 *dhchap_secret = to_host(item)->dhchap_secret;
1843 return sprintf(page, "\n");
1844 return sprintf(page, "%s\n", dhchap_secret);
1847 static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
1848 const char *page, size_t count)
1850 struct nvmet_host *host = to_host(item);
1853 ret = nvmet_auth_set_key(host, page, false);
1855 * Re-authentication is a soft state, so keep the
1856 * current authentication valid until the host
1857 * requests re-authentication.
1859 return ret < 0 ? ret : count;
1862 CONFIGFS_ATTR(nvmet_host_, dhchap_key);
1864 static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
1867 u8 *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
1870 return sprintf(page, "\n");
1871 return sprintf(page, "%s\n", dhchap_secret);
1874 static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
1875 const char *page, size_t count)
1877 struct nvmet_host *host = to_host(item);
1880 ret = nvmet_auth_set_key(host, page, true);
1882 * Re-authentication is a soft state, so keep the
1883 * current authentication valid until the host
1884 * requests re-authentication.
1886 return ret < 0 ? ret : count;
1889 CONFIGFS_ATTR(nvmet_host_, dhchap_ctrl_key);
1891 static ssize_t nvmet_host_dhchap_hash_show(struct config_item *item,
1894 struct nvmet_host *host = to_host(item);
1895 const char *hash_name = nvme_auth_hmac_name(host->dhchap_hash_id);
1897 return sprintf(page, "%s\n", hash_name ? hash_name : "none");
1900 static ssize_t nvmet_host_dhchap_hash_store(struct config_item *item,
1901 const char *page, size_t count)
1903 struct nvmet_host *host = to_host(item);
1906 hmac_id = nvme_auth_hmac_id(page);
1907 if (hmac_id == NVME_AUTH_HASH_INVALID)
1909 if (!crypto_has_shash(nvme_auth_hmac_name(hmac_id), 0, 0))
1911 host->dhchap_hash_id = hmac_id;
1915 CONFIGFS_ATTR(nvmet_host_, dhchap_hash);
1917 static ssize_t nvmet_host_dhchap_dhgroup_show(struct config_item *item,
1920 struct nvmet_host *host = to_host(item);
1921 const char *dhgroup = nvme_auth_dhgroup_name(host->dhchap_dhgroup_id);
1923 return sprintf(page, "%s\n", dhgroup ? dhgroup : "none");
1926 static ssize_t nvmet_host_dhchap_dhgroup_store(struct config_item *item,
1927 const char *page, size_t count)
1929 struct nvmet_host *host = to_host(item);
1932 dhgroup_id = nvme_auth_dhgroup_id(page);
1933 if (dhgroup_id == NVME_AUTH_DHGROUP_INVALID)
1935 if (dhgroup_id != NVME_AUTH_DHGROUP_NULL) {
1936 const char *kpp = nvme_auth_dhgroup_kpp(dhgroup_id);
1938 if (!crypto_has_kpp(kpp, 0, 0))
1941 host->dhchap_dhgroup_id = dhgroup_id;
1945 CONFIGFS_ATTR(nvmet_host_, dhchap_dhgroup);
1947 static struct configfs_attribute *nvmet_host_attrs[] = {
1948 &nvmet_host_attr_dhchap_key,
1949 &nvmet_host_attr_dhchap_ctrl_key,
1950 &nvmet_host_attr_dhchap_hash,
1951 &nvmet_host_attr_dhchap_dhgroup,
1954 #endif /* CONFIG_NVME_TARGET_AUTH */
1956 static void nvmet_host_release(struct config_item *item)
1958 struct nvmet_host *host = to_host(item);
1960 #ifdef CONFIG_NVME_TARGET_AUTH
1961 kfree(host->dhchap_secret);
1962 kfree(host->dhchap_ctrl_secret);
1967 static struct configfs_item_operations nvmet_host_item_ops = {
1968 .release = nvmet_host_release,
1971 static const struct config_item_type nvmet_host_type = {
1972 .ct_item_ops = &nvmet_host_item_ops,
1973 #ifdef CONFIG_NVME_TARGET_AUTH
1974 .ct_attrs = nvmet_host_attrs,
1976 .ct_owner = THIS_MODULE,
1979 static struct config_group *nvmet_hosts_make_group(struct config_group *group,
1982 struct nvmet_host *host;
1984 host = kzalloc(sizeof(*host), GFP_KERNEL);
1986 return ERR_PTR(-ENOMEM);
1988 #ifdef CONFIG_NVME_TARGET_AUTH
1989 /* Default to SHA256 */
1990 host->dhchap_hash_id = NVME_AUTH_HASH_SHA256;
1993 config_group_init_type_name(&host->group, name, &nvmet_host_type);
1995 return &host->group;
1998 static struct configfs_group_operations nvmet_hosts_group_ops = {
1999 .make_group = nvmet_hosts_make_group,
2002 static const struct config_item_type nvmet_hosts_type = {
2003 .ct_group_ops = &nvmet_hosts_group_ops,
2004 .ct_owner = THIS_MODULE,
2007 static struct config_group nvmet_hosts_group;
2009 static const struct config_item_type nvmet_root_type = {
2010 .ct_owner = THIS_MODULE,
2013 static struct configfs_subsystem nvmet_configfs_subsystem = {
2016 .ci_namebuf = "nvmet",
2017 .ci_type = &nvmet_root_type,
2022 int __init nvmet_init_configfs(void)
2026 config_group_init(&nvmet_configfs_subsystem.su_group);
2027 mutex_init(&nvmet_configfs_subsystem.su_mutex);
2029 config_group_init_type_name(&nvmet_subsystems_group,
2030 "subsystems", &nvmet_subsystems_type);
2031 configfs_add_default_group(&nvmet_subsystems_group,
2032 &nvmet_configfs_subsystem.su_group);
2034 config_group_init_type_name(&nvmet_ports_group,
2035 "ports", &nvmet_ports_type);
2036 configfs_add_default_group(&nvmet_ports_group,
2037 &nvmet_configfs_subsystem.su_group);
2039 config_group_init_type_name(&nvmet_hosts_group,
2040 "hosts", &nvmet_hosts_type);
2041 configfs_add_default_group(&nvmet_hosts_group,
2042 &nvmet_configfs_subsystem.su_group);
2044 ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
2046 pr_err("configfs_register_subsystem: %d\n", ret);
2053 void __exit nvmet_exit_configfs(void)
2055 configfs_unregister_subsystem(&nvmet_configfs_subsystem);