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 static ssize_t nvmet_param_max_queue_size_show(struct config_item *item,
279 struct nvmet_port *port = to_nvmet_port(item);
281 return snprintf(page, PAGE_SIZE, "%d\n", port->max_queue_size);
284 static ssize_t nvmet_param_max_queue_size_store(struct config_item *item,
285 const char *page, size_t count)
287 struct nvmet_port *port = to_nvmet_port(item);
290 if (nvmet_is_port_enabled(port, __func__))
292 ret = kstrtoint(page, 0, &port->max_queue_size);
294 pr_err("Invalid value '%s' for max_queue_size\n", page);
300 CONFIGFS_ATTR(nvmet_, param_max_queue_size);
302 #ifdef CONFIG_BLK_DEV_INTEGRITY
303 static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
306 struct nvmet_port *port = to_nvmet_port(item);
308 return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
311 static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
312 const char *page, size_t count)
314 struct nvmet_port *port = to_nvmet_port(item);
317 if (kstrtobool(page, &val))
320 if (nvmet_is_port_enabled(port, __func__))
323 port->pi_enable = val;
327 CONFIGFS_ATTR(nvmet_, param_pi_enable);
330 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
333 struct nvmet_port *port = to_nvmet_port(item);
336 for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
337 if (port->disc_addr.trtype == nvmet_transport[i].type)
338 return snprintf(page, PAGE_SIZE,
339 "%s\n", nvmet_transport[i].name);
342 return sprintf(page, "\n");
345 static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
347 port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
348 port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
349 port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
352 static void nvmet_port_init_tsas_tcp(struct nvmet_port *port, int sectype)
354 port->disc_addr.tsas.tcp.sectype = sectype;
357 static ssize_t nvmet_addr_trtype_store(struct config_item *item,
358 const char *page, size_t count)
360 struct nvmet_port *port = to_nvmet_port(item);
363 if (nvmet_is_port_enabled(port, __func__))
366 for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
367 if (sysfs_streq(page, nvmet_transport[i].name))
371 pr_err("Invalid value '%s' for trtype\n", page);
375 memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
376 port->disc_addr.trtype = nvmet_transport[i].type;
377 if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
378 nvmet_port_init_tsas_rdma(port);
379 else if (port->disc_addr.trtype == NVMF_TRTYPE_TCP)
380 nvmet_port_init_tsas_tcp(port, NVMF_TCP_SECTYPE_NONE);
384 CONFIGFS_ATTR(nvmet_, addr_trtype);
386 static const struct nvmet_type_name_map nvmet_addr_tsas_tcp[] = {
387 { NVMF_TCP_SECTYPE_NONE, "none" },
388 { NVMF_TCP_SECTYPE_TLS13, "tls1.3" },
391 static const struct nvmet_type_name_map nvmet_addr_tsas_rdma[] = {
392 { NVMF_RDMA_QPTYPE_CONNECTED, "connected" },
393 { NVMF_RDMA_QPTYPE_DATAGRAM, "datagram" },
396 static ssize_t nvmet_addr_tsas_show(struct config_item *item,
399 struct nvmet_port *port = to_nvmet_port(item);
402 if (port->disc_addr.trtype == NVMF_TRTYPE_TCP) {
403 for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_tcp); i++) {
404 if (port->disc_addr.tsas.tcp.sectype == nvmet_addr_tsas_tcp[i].type)
405 return sprintf(page, "%s\n", nvmet_addr_tsas_tcp[i].name);
407 } else if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA) {
408 for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_rdma); i++) {
409 if (port->disc_addr.tsas.rdma.qptype == nvmet_addr_tsas_rdma[i].type)
410 return sprintf(page, "%s\n", nvmet_addr_tsas_rdma[i].name);
413 return sprintf(page, "\n");
416 static u8 nvmet_addr_tsas_rdma_store(const char *page)
420 for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_rdma); i++) {
421 if (sysfs_streq(page, nvmet_addr_tsas_rdma[i].name))
422 return nvmet_addr_tsas_rdma[i].type;
424 return NVMF_RDMA_QPTYPE_INVALID;
427 static u8 nvmet_addr_tsas_tcp_store(const char *page)
431 for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_tcp); i++) {
432 if (sysfs_streq(page, nvmet_addr_tsas_tcp[i].name))
433 return nvmet_addr_tsas_tcp[i].type;
435 return NVMF_TCP_SECTYPE_INVALID;
438 static ssize_t nvmet_addr_tsas_store(struct config_item *item,
439 const char *page, size_t count)
441 struct nvmet_port *port = to_nvmet_port(item);
442 u8 treq = nvmet_port_disc_addr_treq_mask(port);
445 if (nvmet_is_port_enabled(port, __func__))
448 if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA) {
449 qptype = nvmet_addr_tsas_rdma_store(page);
450 if (qptype == port->disc_addr.tsas.rdma.qptype)
452 } else if (port->disc_addr.trtype == NVMF_TRTYPE_TCP) {
453 sectype = nvmet_addr_tsas_tcp_store(page);
454 if (sectype != NVMF_TCP_SECTYPE_INVALID)
458 pr_err("Invalid value '%s' for tsas\n", page);
462 if (sectype == NVMF_TCP_SECTYPE_TLS13) {
463 if (!IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS)) {
464 pr_err("TLS is not supported\n");
467 if (!port->keyring) {
468 pr_err("TLS keyring not configured\n");
473 nvmet_port_init_tsas_tcp(port, sectype);
475 * If TLS is enabled TREQ should be set to 'required' per default
477 if (sectype == NVMF_TCP_SECTYPE_TLS13) {
478 u8 sc = nvmet_port_disc_addr_treq_secure_channel(port);
480 if (sc == NVMF_TREQ_NOT_SPECIFIED)
481 treq |= NVMF_TREQ_REQUIRED;
485 treq |= NVMF_TREQ_NOT_SPECIFIED;
487 port->disc_addr.treq = treq;
491 CONFIGFS_ATTR(nvmet_, addr_tsas);
494 * Namespace structures & file operation functions below
496 static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
498 return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
501 static ssize_t nvmet_ns_device_path_store(struct config_item *item,
502 const char *page, size_t count)
504 struct nvmet_ns *ns = to_nvmet_ns(item);
505 struct nvmet_subsys *subsys = ns->subsys;
509 mutex_lock(&subsys->lock);
515 len = strcspn(page, "\n");
519 kfree(ns->device_path);
521 ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
522 if (!ns->device_path)
525 mutex_unlock(&subsys->lock);
529 mutex_unlock(&subsys->lock);
533 CONFIGFS_ATTR(nvmet_ns_, device_path);
535 #ifdef CONFIG_PCI_P2PDMA
536 static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
538 struct nvmet_ns *ns = to_nvmet_ns(item);
540 return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
543 static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
544 const char *page, size_t count)
546 struct nvmet_ns *ns = to_nvmet_ns(item);
547 struct pci_dev *p2p_dev = NULL;
552 mutex_lock(&ns->subsys->lock);
558 error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
564 ns->use_p2pmem = use_p2pmem;
565 pci_dev_put(ns->p2p_dev);
566 ns->p2p_dev = p2p_dev;
569 mutex_unlock(&ns->subsys->lock);
574 CONFIGFS_ATTR(nvmet_ns_, p2pmem);
575 #endif /* CONFIG_PCI_P2PDMA */
577 static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
579 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
582 static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
583 const char *page, size_t count)
585 struct nvmet_ns *ns = to_nvmet_ns(item);
586 struct nvmet_subsys *subsys = ns->subsys;
589 mutex_lock(&subsys->lock);
595 if (uuid_parse(page, &ns->uuid))
599 mutex_unlock(&subsys->lock);
600 return ret ? ret : count;
603 CONFIGFS_ATTR(nvmet_ns_, device_uuid);
605 static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
607 return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
610 static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
611 const char *page, size_t count)
613 struct nvmet_ns *ns = to_nvmet_ns(item);
614 struct nvmet_subsys *subsys = ns->subsys;
616 const char *p = page;
620 mutex_lock(&subsys->lock);
626 for (i = 0; i < 16; i++) {
627 if (p + 2 > page + count) {
631 if (!isxdigit(p[0]) || !isxdigit(p[1])) {
636 nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
639 if (*p == '-' || *p == ':')
643 memcpy(&ns->nguid, nguid, sizeof(nguid));
645 mutex_unlock(&subsys->lock);
646 return ret ? ret : count;
649 CONFIGFS_ATTR(nvmet_ns_, device_nguid);
651 static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
653 return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
656 static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
657 const char *page, size_t count)
659 struct nvmet_ns *ns = to_nvmet_ns(item);
660 u32 oldgrpid, newgrpid;
663 ret = kstrtou32(page, 0, &newgrpid);
667 if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
670 down_write(&nvmet_ana_sem);
671 oldgrpid = ns->anagrpid;
672 newgrpid = array_index_nospec(newgrpid, NVMET_MAX_ANAGRPS);
673 nvmet_ana_group_enabled[newgrpid]++;
674 ns->anagrpid = newgrpid;
675 nvmet_ana_group_enabled[oldgrpid]--;
677 up_write(&nvmet_ana_sem);
679 nvmet_send_ana_event(ns->subsys, NULL);
683 CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
685 static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
687 return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
690 static ssize_t nvmet_ns_enable_store(struct config_item *item,
691 const char *page, size_t count)
693 struct nvmet_ns *ns = to_nvmet_ns(item);
697 if (kstrtobool(page, &enable))
701 * take a global nvmet_config_sem because the disable routine has a
702 * window where it releases the subsys-lock, giving a chance to
703 * a parallel enable to concurrently execute causing the disable to
704 * have a misaccounting of the ns percpu_ref.
706 down_write(&nvmet_config_sem);
708 ret = nvmet_ns_enable(ns);
710 nvmet_ns_disable(ns);
711 up_write(&nvmet_config_sem);
713 return ret ? ret : count;
716 CONFIGFS_ATTR(nvmet_ns_, enable);
718 static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
720 return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
723 static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
724 const char *page, size_t count)
726 struct nvmet_ns *ns = to_nvmet_ns(item);
729 if (kstrtobool(page, &val))
732 mutex_lock(&ns->subsys->lock);
734 pr_err("disable ns before setting buffered_io value.\n");
735 mutex_unlock(&ns->subsys->lock);
739 ns->buffered_io = val;
740 mutex_unlock(&ns->subsys->lock);
744 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
746 static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
747 const char *page, size_t count)
749 struct nvmet_ns *ns = to_nvmet_ns(item);
752 if (kstrtobool(page, &val))
758 mutex_lock(&ns->subsys->lock);
760 pr_err("enable ns before revalidate.\n");
761 mutex_unlock(&ns->subsys->lock);
764 if (nvmet_ns_revalidate(ns))
765 nvmet_ns_changed(ns->subsys, ns->nsid);
766 mutex_unlock(&ns->subsys->lock);
770 CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
772 static struct configfs_attribute *nvmet_ns_attrs[] = {
773 &nvmet_ns_attr_device_path,
774 &nvmet_ns_attr_device_nguid,
775 &nvmet_ns_attr_device_uuid,
776 &nvmet_ns_attr_ana_grpid,
777 &nvmet_ns_attr_enable,
778 &nvmet_ns_attr_buffered_io,
779 &nvmet_ns_attr_revalidate_size,
780 #ifdef CONFIG_PCI_P2PDMA
781 &nvmet_ns_attr_p2pmem,
786 bool nvmet_subsys_nsid_exists(struct nvmet_subsys *subsys, u32 nsid)
788 struct config_item *ns_item;
791 snprintf(name, sizeof(name), "%u", nsid);
792 mutex_lock(&subsys->namespaces_group.cg_subsys->su_mutex);
793 ns_item = config_group_find_item(&subsys->namespaces_group, name);
794 mutex_unlock(&subsys->namespaces_group.cg_subsys->su_mutex);
795 return ns_item != NULL;
798 static void nvmet_ns_release(struct config_item *item)
800 struct nvmet_ns *ns = to_nvmet_ns(item);
805 static struct configfs_item_operations nvmet_ns_item_ops = {
806 .release = nvmet_ns_release,
809 static const struct config_item_type nvmet_ns_type = {
810 .ct_item_ops = &nvmet_ns_item_ops,
811 .ct_attrs = nvmet_ns_attrs,
812 .ct_owner = THIS_MODULE,
815 static struct config_group *nvmet_ns_make(struct config_group *group,
818 struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
823 ret = kstrtou32(name, 0, &nsid);
828 if (nsid == 0 || nsid == NVME_NSID_ALL) {
829 pr_err("invalid nsid %#x", nsid);
834 ns = nvmet_ns_alloc(subsys, nsid);
837 config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
839 pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
846 static struct configfs_group_operations nvmet_namespaces_group_ops = {
847 .make_group = nvmet_ns_make,
850 static const struct config_item_type nvmet_namespaces_type = {
851 .ct_group_ops = &nvmet_namespaces_group_ops,
852 .ct_owner = THIS_MODULE,
855 #ifdef CONFIG_NVME_TARGET_PASSTHRU
857 static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
860 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
862 return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
865 static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
866 const char *page, size_t count)
868 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
872 mutex_lock(&subsys->lock);
875 if (subsys->passthru_ctrl)
879 len = strcspn(page, "\n");
883 kfree(subsys->passthru_ctrl_path);
885 subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
886 if (!subsys->passthru_ctrl_path)
889 mutex_unlock(&subsys->lock);
893 mutex_unlock(&subsys->lock);
896 CONFIGFS_ATTR(nvmet_passthru_, device_path);
898 static ssize_t nvmet_passthru_enable_show(struct config_item *item,
901 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
903 return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
906 static ssize_t nvmet_passthru_enable_store(struct config_item *item,
907 const char *page, size_t count)
909 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
913 if (kstrtobool(page, &enable))
917 ret = nvmet_passthru_ctrl_enable(subsys);
919 nvmet_passthru_ctrl_disable(subsys);
921 return ret ? ret : count;
923 CONFIGFS_ATTR(nvmet_passthru_, enable);
925 static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
928 return sprintf(page, "%u\n", to_subsys(item->ci_parent)->admin_timeout);
931 static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
932 const char *page, size_t count)
934 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
935 unsigned int timeout;
937 if (kstrtouint(page, 0, &timeout))
939 subsys->admin_timeout = timeout;
942 CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
944 static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
947 return sprintf(page, "%u\n", to_subsys(item->ci_parent)->io_timeout);
950 static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
951 const char *page, size_t count)
953 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
954 unsigned int timeout;
956 if (kstrtouint(page, 0, &timeout))
958 subsys->io_timeout = timeout;
961 CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
963 static ssize_t nvmet_passthru_clear_ids_show(struct config_item *item,
966 return sprintf(page, "%u\n", to_subsys(item->ci_parent)->clear_ids);
969 static ssize_t nvmet_passthru_clear_ids_store(struct config_item *item,
970 const char *page, size_t count)
972 struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
973 unsigned int clear_ids;
975 if (kstrtouint(page, 0, &clear_ids))
977 subsys->clear_ids = clear_ids;
980 CONFIGFS_ATTR(nvmet_passthru_, clear_ids);
982 static struct configfs_attribute *nvmet_passthru_attrs[] = {
983 &nvmet_passthru_attr_device_path,
984 &nvmet_passthru_attr_enable,
985 &nvmet_passthru_attr_admin_timeout,
986 &nvmet_passthru_attr_io_timeout,
987 &nvmet_passthru_attr_clear_ids,
991 static const struct config_item_type nvmet_passthru_type = {
992 .ct_attrs = nvmet_passthru_attrs,
993 .ct_owner = THIS_MODULE,
996 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
998 config_group_init_type_name(&subsys->passthru_group,
999 "passthru", &nvmet_passthru_type);
1000 configfs_add_default_group(&subsys->passthru_group,
1004 #else /* CONFIG_NVME_TARGET_PASSTHRU */
1006 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
1010 #endif /* CONFIG_NVME_TARGET_PASSTHRU */
1012 static int nvmet_port_subsys_allow_link(struct config_item *parent,
1013 struct config_item *target)
1015 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
1016 struct nvmet_subsys *subsys;
1017 struct nvmet_subsys_link *link, *p;
1020 if (target->ci_type != &nvmet_subsys_type) {
1021 pr_err("can only link subsystems into the subsystems dir.!\n");
1024 subsys = to_subsys(target);
1025 link = kmalloc(sizeof(*link), GFP_KERNEL);
1028 link->subsys = subsys;
1030 down_write(&nvmet_config_sem);
1032 list_for_each_entry(p, &port->subsystems, entry) {
1033 if (p->subsys == subsys)
1037 if (list_empty(&port->subsystems)) {
1038 ret = nvmet_enable_port(port);
1043 list_add_tail(&link->entry, &port->subsystems);
1044 nvmet_port_disc_changed(port, subsys);
1046 up_write(&nvmet_config_sem);
1050 up_write(&nvmet_config_sem);
1055 static void nvmet_port_subsys_drop_link(struct config_item *parent,
1056 struct config_item *target)
1058 struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
1059 struct nvmet_subsys *subsys = to_subsys(target);
1060 struct nvmet_subsys_link *p;
1062 down_write(&nvmet_config_sem);
1063 list_for_each_entry(p, &port->subsystems, entry) {
1064 if (p->subsys == subsys)
1067 up_write(&nvmet_config_sem);
1071 list_del(&p->entry);
1072 nvmet_port_del_ctrls(port, subsys);
1073 nvmet_port_disc_changed(port, subsys);
1075 if (list_empty(&port->subsystems))
1076 nvmet_disable_port(port);
1077 up_write(&nvmet_config_sem);
1081 static struct configfs_item_operations nvmet_port_subsys_item_ops = {
1082 .allow_link = nvmet_port_subsys_allow_link,
1083 .drop_link = nvmet_port_subsys_drop_link,
1086 static const struct config_item_type nvmet_port_subsys_type = {
1087 .ct_item_ops = &nvmet_port_subsys_item_ops,
1088 .ct_owner = THIS_MODULE,
1091 static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
1092 struct config_item *target)
1094 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
1095 struct nvmet_host *host;
1096 struct nvmet_host_link *link, *p;
1099 if (target->ci_type != &nvmet_host_type) {
1100 pr_err("can only link hosts into the allowed_hosts directory!\n");
1104 host = to_host(target);
1105 link = kmalloc(sizeof(*link), GFP_KERNEL);
1110 down_write(&nvmet_config_sem);
1112 if (subsys->allow_any_host) {
1113 pr_err("can't add hosts when allow_any_host is set!\n");
1118 list_for_each_entry(p, &subsys->hosts, entry) {
1119 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
1122 list_add_tail(&link->entry, &subsys->hosts);
1123 nvmet_subsys_disc_changed(subsys, host);
1125 up_write(&nvmet_config_sem);
1128 up_write(&nvmet_config_sem);
1133 static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
1134 struct config_item *target)
1136 struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
1137 struct nvmet_host *host = to_host(target);
1138 struct nvmet_host_link *p;
1140 down_write(&nvmet_config_sem);
1141 list_for_each_entry(p, &subsys->hosts, entry) {
1142 if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
1145 up_write(&nvmet_config_sem);
1149 list_del(&p->entry);
1150 nvmet_subsys_disc_changed(subsys, host);
1152 up_write(&nvmet_config_sem);
1156 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
1157 .allow_link = nvmet_allowed_hosts_allow_link,
1158 .drop_link = nvmet_allowed_hosts_drop_link,
1161 static const struct config_item_type nvmet_allowed_hosts_type = {
1162 .ct_item_ops = &nvmet_allowed_hosts_item_ops,
1163 .ct_owner = THIS_MODULE,
1166 static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
1169 return snprintf(page, PAGE_SIZE, "%d\n",
1170 to_subsys(item)->allow_any_host);
1173 static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
1174 const char *page, size_t count)
1176 struct nvmet_subsys *subsys = to_subsys(item);
1177 bool allow_any_host;
1180 if (kstrtobool(page, &allow_any_host))
1183 down_write(&nvmet_config_sem);
1184 if (allow_any_host && !list_empty(&subsys->hosts)) {
1185 pr_err("Can't set allow_any_host when explicit hosts are set!\n");
1190 if (subsys->allow_any_host != allow_any_host) {
1191 subsys->allow_any_host = allow_any_host;
1192 nvmet_subsys_disc_changed(subsys, NULL);
1196 up_write(&nvmet_config_sem);
1197 return ret ? ret : count;
1200 CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
1202 static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
1205 struct nvmet_subsys *subsys = to_subsys(item);
1207 if (NVME_TERTIARY(subsys->ver))
1208 return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
1209 NVME_MAJOR(subsys->ver),
1210 NVME_MINOR(subsys->ver),
1211 NVME_TERTIARY(subsys->ver));
1213 return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
1214 NVME_MAJOR(subsys->ver),
1215 NVME_MINOR(subsys->ver));
1219 nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,
1220 const char *page, size_t count)
1222 int major, minor, tertiary = 0;
1225 if (subsys->subsys_discovered) {
1226 if (NVME_TERTIARY(subsys->ver))
1227 pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
1228 NVME_MAJOR(subsys->ver),
1229 NVME_MINOR(subsys->ver),
1230 NVME_TERTIARY(subsys->ver));
1232 pr_err("Can't set version number. %llu.%llu is already assigned\n",
1233 NVME_MAJOR(subsys->ver),
1234 NVME_MINOR(subsys->ver));
1238 /* passthru subsystems use the underlying controller's version */
1239 if (nvmet_is_passthru_subsys(subsys))
1242 ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
1243 if (ret != 2 && ret != 3)
1246 subsys->ver = NVME_VS(major, minor, tertiary);
1251 static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
1252 const char *page, size_t count)
1254 struct nvmet_subsys *subsys = to_subsys(item);
1257 down_write(&nvmet_config_sem);
1258 mutex_lock(&subsys->lock);
1259 ret = nvmet_subsys_attr_version_store_locked(subsys, page, count);
1260 mutex_unlock(&subsys->lock);
1261 up_write(&nvmet_config_sem);
1265 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
1267 /* See Section 1.5 of NVMe 1.4 */
1268 static bool nvmet_is_ascii(const char c)
1270 return c >= 0x20 && c <= 0x7e;
1273 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
1276 struct nvmet_subsys *subsys = to_subsys(item);
1278 return snprintf(page, PAGE_SIZE, "%.*s\n",
1279 NVMET_SN_MAX_SIZE, subsys->serial);
1283 nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
1284 const char *page, size_t count)
1286 int pos, len = strcspn(page, "\n");
1288 if (subsys->subsys_discovered) {
1289 pr_err("Can't set serial number. %s is already assigned\n",
1294 if (!len || len > NVMET_SN_MAX_SIZE) {
1295 pr_err("Serial Number can not be empty or exceed %d Bytes\n",
1300 for (pos = 0; pos < len; pos++) {
1301 if (!nvmet_is_ascii(page[pos])) {
1302 pr_err("Serial Number must contain only ASCII strings\n");
1307 memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
1312 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
1313 const char *page, size_t count)
1315 struct nvmet_subsys *subsys = to_subsys(item);
1318 down_write(&nvmet_config_sem);
1319 mutex_lock(&subsys->lock);
1320 ret = nvmet_subsys_attr_serial_store_locked(subsys, page, count);
1321 mutex_unlock(&subsys->lock);
1322 up_write(&nvmet_config_sem);
1326 CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
1328 static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
1331 return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
1334 static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
1335 const char *page, size_t cnt)
1339 if (sscanf(page, "%hu\n", &cntlid_min) != 1)
1342 if (cntlid_min == 0)
1345 down_write(&nvmet_config_sem);
1346 if (cntlid_min > to_subsys(item)->cntlid_max)
1348 to_subsys(item)->cntlid_min = cntlid_min;
1349 up_write(&nvmet_config_sem);
1353 up_write(&nvmet_config_sem);
1356 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
1358 static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
1361 return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
1364 static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
1365 const char *page, size_t cnt)
1369 if (sscanf(page, "%hu\n", &cntlid_max) != 1)
1372 if (cntlid_max == 0)
1375 down_write(&nvmet_config_sem);
1376 if (cntlid_max < to_subsys(item)->cntlid_min)
1378 to_subsys(item)->cntlid_max = cntlid_max;
1379 up_write(&nvmet_config_sem);
1383 up_write(&nvmet_config_sem);
1386 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
1388 static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
1391 struct nvmet_subsys *subsys = to_subsys(item);
1393 return snprintf(page, PAGE_SIZE, "%s\n", subsys->model_number);
1396 static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
1397 const char *page, size_t count)
1402 if (subsys->subsys_discovered) {
1403 pr_err("Can't set model number. %s is already assigned\n",
1404 subsys->model_number);
1408 len = strcspn(page, "\n");
1412 if (len > NVMET_MN_MAX_SIZE) {
1413 pr_err("Model number size can not exceed %d Bytes\n",
1418 for (pos = 0; pos < len; pos++) {
1419 if (!nvmet_is_ascii(page[pos]))
1423 val = kmemdup_nul(page, len, GFP_KERNEL);
1426 kfree(subsys->model_number);
1427 subsys->model_number = val;
1431 static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
1432 const char *page, size_t count)
1434 struct nvmet_subsys *subsys = to_subsys(item);
1437 down_write(&nvmet_config_sem);
1438 mutex_lock(&subsys->lock);
1439 ret = nvmet_subsys_attr_model_store_locked(subsys, page, count);
1440 mutex_unlock(&subsys->lock);
1441 up_write(&nvmet_config_sem);
1445 CONFIGFS_ATTR(nvmet_subsys_, attr_model);
1447 static ssize_t nvmet_subsys_attr_ieee_oui_show(struct config_item *item,
1450 struct nvmet_subsys *subsys = to_subsys(item);
1452 return sysfs_emit(page, "0x%06x\n", subsys->ieee_oui);
1455 static ssize_t nvmet_subsys_attr_ieee_oui_store_locked(struct nvmet_subsys *subsys,
1456 const char *page, size_t count)
1461 if (subsys->subsys_discovered) {
1462 pr_err("Can't set IEEE OUI. 0x%06x is already assigned\n",
1467 ret = kstrtou32(page, 0, &val);
1471 if (val >= 0x1000000)
1474 subsys->ieee_oui = val;
1479 static ssize_t nvmet_subsys_attr_ieee_oui_store(struct config_item *item,
1480 const char *page, size_t count)
1482 struct nvmet_subsys *subsys = to_subsys(item);
1485 down_write(&nvmet_config_sem);
1486 mutex_lock(&subsys->lock);
1487 ret = nvmet_subsys_attr_ieee_oui_store_locked(subsys, page, count);
1488 mutex_unlock(&subsys->lock);
1489 up_write(&nvmet_config_sem);
1493 CONFIGFS_ATTR(nvmet_subsys_, attr_ieee_oui);
1495 static ssize_t nvmet_subsys_attr_firmware_show(struct config_item *item,
1498 struct nvmet_subsys *subsys = to_subsys(item);
1500 return sysfs_emit(page, "%s\n", subsys->firmware_rev);
1503 static ssize_t nvmet_subsys_attr_firmware_store_locked(struct nvmet_subsys *subsys,
1504 const char *page, size_t count)
1509 if (subsys->subsys_discovered) {
1510 pr_err("Can't set firmware revision. %s is already assigned\n",
1511 subsys->firmware_rev);
1515 len = strcspn(page, "\n");
1519 if (len > NVMET_FR_MAX_SIZE) {
1520 pr_err("Firmware revision size can not exceed %d Bytes\n",
1525 for (pos = 0; pos < len; pos++) {
1526 if (!nvmet_is_ascii(page[pos]))
1530 val = kmemdup_nul(page, len, GFP_KERNEL);
1534 kfree(subsys->firmware_rev);
1536 subsys->firmware_rev = val;
1541 static ssize_t nvmet_subsys_attr_firmware_store(struct config_item *item,
1542 const char *page, size_t count)
1544 struct nvmet_subsys *subsys = to_subsys(item);
1547 down_write(&nvmet_config_sem);
1548 mutex_lock(&subsys->lock);
1549 ret = nvmet_subsys_attr_firmware_store_locked(subsys, page, count);
1550 mutex_unlock(&subsys->lock);
1551 up_write(&nvmet_config_sem);
1555 CONFIGFS_ATTR(nvmet_subsys_, attr_firmware);
1557 #ifdef CONFIG_BLK_DEV_INTEGRITY
1558 static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
1561 return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
1564 static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
1565 const char *page, size_t count)
1567 struct nvmet_subsys *subsys = to_subsys(item);
1570 if (kstrtobool(page, &pi_enable))
1573 subsys->pi_support = pi_enable;
1576 CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
1579 static ssize_t nvmet_subsys_attr_qid_max_show(struct config_item *item,
1582 return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->max_qid);
1585 static ssize_t nvmet_subsys_attr_qid_max_store(struct config_item *item,
1586 const char *page, size_t cnt)
1588 struct nvmet_subsys *subsys = to_subsys(item);
1589 struct nvmet_ctrl *ctrl;
1592 if (sscanf(page, "%hu\n", &qid_max) != 1)
1595 if (qid_max < 1 || qid_max > NVMET_NR_QUEUES)
1598 down_write(&nvmet_config_sem);
1599 subsys->max_qid = qid_max;
1601 /* Force reconnect */
1602 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1603 ctrl->ops->delete_ctrl(ctrl);
1604 up_write(&nvmet_config_sem);
1608 CONFIGFS_ATTR(nvmet_subsys_, attr_qid_max);
1610 static struct configfs_attribute *nvmet_subsys_attrs[] = {
1611 &nvmet_subsys_attr_attr_allow_any_host,
1612 &nvmet_subsys_attr_attr_version,
1613 &nvmet_subsys_attr_attr_serial,
1614 &nvmet_subsys_attr_attr_cntlid_min,
1615 &nvmet_subsys_attr_attr_cntlid_max,
1616 &nvmet_subsys_attr_attr_model,
1617 &nvmet_subsys_attr_attr_qid_max,
1618 &nvmet_subsys_attr_attr_ieee_oui,
1619 &nvmet_subsys_attr_attr_firmware,
1620 #ifdef CONFIG_BLK_DEV_INTEGRITY
1621 &nvmet_subsys_attr_attr_pi_enable,
1627 * Subsystem structures & folder operation functions below
1629 static void nvmet_subsys_release(struct config_item *item)
1631 struct nvmet_subsys *subsys = to_subsys(item);
1633 nvmet_subsys_del_ctrls(subsys);
1634 nvmet_subsys_put(subsys);
1637 static struct configfs_item_operations nvmet_subsys_item_ops = {
1638 .release = nvmet_subsys_release,
1641 static const struct config_item_type nvmet_subsys_type = {
1642 .ct_item_ops = &nvmet_subsys_item_ops,
1643 .ct_attrs = nvmet_subsys_attrs,
1644 .ct_owner = THIS_MODULE,
1647 static struct config_group *nvmet_subsys_make(struct config_group *group,
1650 struct nvmet_subsys *subsys;
1652 if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1653 pr_err("can't create discovery subsystem through configfs\n");
1654 return ERR_PTR(-EINVAL);
1657 if (sysfs_streq(name, nvmet_disc_subsys->subsysnqn)) {
1658 pr_err("can't create subsystem using unique discovery NQN\n");
1659 return ERR_PTR(-EINVAL);
1662 subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
1664 return ERR_CAST(subsys);
1666 config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1668 config_group_init_type_name(&subsys->namespaces_group,
1669 "namespaces", &nvmet_namespaces_type);
1670 configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1672 config_group_init_type_name(&subsys->allowed_hosts_group,
1673 "allowed_hosts", &nvmet_allowed_hosts_type);
1674 configfs_add_default_group(&subsys->allowed_hosts_group,
1677 nvmet_add_passthru_group(subsys);
1679 return &subsys->group;
1682 static struct configfs_group_operations nvmet_subsystems_group_ops = {
1683 .make_group = nvmet_subsys_make,
1686 static const struct config_item_type nvmet_subsystems_type = {
1687 .ct_group_ops = &nvmet_subsystems_group_ops,
1688 .ct_owner = THIS_MODULE,
1691 static ssize_t nvmet_referral_enable_show(struct config_item *item,
1694 return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1697 static ssize_t nvmet_referral_enable_store(struct config_item *item,
1698 const char *page, size_t count)
1700 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1701 struct nvmet_port *port = to_nvmet_port(item);
1704 if (kstrtobool(page, &enable))
1708 nvmet_referral_enable(parent, port);
1710 nvmet_referral_disable(parent, port);
1714 pr_err("Invalid value '%s' for enable\n", page);
1718 CONFIGFS_ATTR(nvmet_referral_, enable);
1721 * Discovery Service subsystem definitions
1723 static struct configfs_attribute *nvmet_referral_attrs[] = {
1724 &nvmet_attr_addr_adrfam,
1725 &nvmet_attr_addr_portid,
1726 &nvmet_attr_addr_treq,
1727 &nvmet_attr_addr_traddr,
1728 &nvmet_attr_addr_trsvcid,
1729 &nvmet_attr_addr_trtype,
1730 &nvmet_referral_attr_enable,
1734 static void nvmet_referral_notify(struct config_group *group,
1735 struct config_item *item)
1737 struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1738 struct nvmet_port *port = to_nvmet_port(item);
1740 nvmet_referral_disable(parent, port);
1743 static void nvmet_referral_release(struct config_item *item)
1745 struct nvmet_port *port = to_nvmet_port(item);
1750 static struct configfs_item_operations nvmet_referral_item_ops = {
1751 .release = nvmet_referral_release,
1754 static const struct config_item_type nvmet_referral_type = {
1755 .ct_owner = THIS_MODULE,
1756 .ct_attrs = nvmet_referral_attrs,
1757 .ct_item_ops = &nvmet_referral_item_ops,
1760 static struct config_group *nvmet_referral_make(
1761 struct config_group *group, const char *name)
1763 struct nvmet_port *port;
1765 port = kzalloc(sizeof(*port), GFP_KERNEL);
1767 return ERR_PTR(-ENOMEM);
1769 INIT_LIST_HEAD(&port->entry);
1770 config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1772 return &port->group;
1775 static struct configfs_group_operations nvmet_referral_group_ops = {
1776 .make_group = nvmet_referral_make,
1777 .disconnect_notify = nvmet_referral_notify,
1780 static const struct config_item_type nvmet_referrals_type = {
1781 .ct_owner = THIS_MODULE,
1782 .ct_group_ops = &nvmet_referral_group_ops,
1785 static struct nvmet_type_name_map nvmet_ana_state[] = {
1786 { NVME_ANA_OPTIMIZED, "optimized" },
1787 { NVME_ANA_NONOPTIMIZED, "non-optimized" },
1788 { NVME_ANA_INACCESSIBLE, "inaccessible" },
1789 { NVME_ANA_PERSISTENT_LOSS, "persistent-loss" },
1790 { NVME_ANA_CHANGE, "change" },
1793 static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
1796 struct nvmet_ana_group *grp = to_ana_group(item);
1797 enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
1800 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1801 if (state == nvmet_ana_state[i].type)
1802 return sprintf(page, "%s\n", nvmet_ana_state[i].name);
1805 return sprintf(page, "\n");
1808 static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
1809 const char *page, size_t count)
1811 struct nvmet_ana_group *grp = to_ana_group(item);
1812 enum nvme_ana_state *ana_state = grp->port->ana_state;
1815 for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1816 if (sysfs_streq(page, nvmet_ana_state[i].name))
1820 pr_err("Invalid value '%s' for ana_state\n", page);
1824 down_write(&nvmet_ana_sem);
1825 ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
1827 up_write(&nvmet_ana_sem);
1828 nvmet_port_send_ana_event(grp->port);
1832 CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
1834 static struct configfs_attribute *nvmet_ana_group_attrs[] = {
1835 &nvmet_ana_group_attr_ana_state,
1839 static void nvmet_ana_group_release(struct config_item *item)
1841 struct nvmet_ana_group *grp = to_ana_group(item);
1843 if (grp == &grp->port->ana_default_group)
1846 down_write(&nvmet_ana_sem);
1847 grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
1848 nvmet_ana_group_enabled[grp->grpid]--;
1849 up_write(&nvmet_ana_sem);
1851 nvmet_port_send_ana_event(grp->port);
1855 static struct configfs_item_operations nvmet_ana_group_item_ops = {
1856 .release = nvmet_ana_group_release,
1859 static const struct config_item_type nvmet_ana_group_type = {
1860 .ct_item_ops = &nvmet_ana_group_item_ops,
1861 .ct_attrs = nvmet_ana_group_attrs,
1862 .ct_owner = THIS_MODULE,
1865 static struct config_group *nvmet_ana_groups_make_group(
1866 struct config_group *group, const char *name)
1868 struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
1869 struct nvmet_ana_group *grp;
1873 ret = kstrtou32(name, 0, &grpid);
1878 if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
1882 grp = kzalloc(sizeof(*grp), GFP_KERNEL);
1888 down_write(&nvmet_ana_sem);
1889 grpid = array_index_nospec(grpid, NVMET_MAX_ANAGRPS);
1890 nvmet_ana_group_enabled[grpid]++;
1891 up_write(&nvmet_ana_sem);
1893 nvmet_port_send_ana_event(grp->port);
1895 config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
1898 return ERR_PTR(ret);
1901 static struct configfs_group_operations nvmet_ana_groups_group_ops = {
1902 .make_group = nvmet_ana_groups_make_group,
1905 static const struct config_item_type nvmet_ana_groups_type = {
1906 .ct_group_ops = &nvmet_ana_groups_group_ops,
1907 .ct_owner = THIS_MODULE,
1911 * Ports definitions.
1913 static void nvmet_port_release(struct config_item *item)
1915 struct nvmet_port *port = to_nvmet_port(item);
1917 /* Let inflight controllers teardown complete */
1918 flush_workqueue(nvmet_wq);
1919 list_del(&port->global_entry);
1921 key_put(port->keyring);
1922 kfree(port->ana_state);
1926 static struct configfs_attribute *nvmet_port_attrs[] = {
1927 &nvmet_attr_addr_adrfam,
1928 &nvmet_attr_addr_treq,
1929 &nvmet_attr_addr_traddr,
1930 &nvmet_attr_addr_trsvcid,
1931 &nvmet_attr_addr_trtype,
1932 &nvmet_attr_addr_tsas,
1933 &nvmet_attr_param_inline_data_size,
1934 &nvmet_attr_param_max_queue_size,
1935 #ifdef CONFIG_BLK_DEV_INTEGRITY
1936 &nvmet_attr_param_pi_enable,
1941 static struct configfs_item_operations nvmet_port_item_ops = {
1942 .release = nvmet_port_release,
1945 static const struct config_item_type nvmet_port_type = {
1946 .ct_attrs = nvmet_port_attrs,
1947 .ct_item_ops = &nvmet_port_item_ops,
1948 .ct_owner = THIS_MODULE,
1951 static struct config_group *nvmet_ports_make(struct config_group *group,
1954 struct nvmet_port *port;
1958 if (kstrtou16(name, 0, &portid))
1959 return ERR_PTR(-EINVAL);
1961 port = kzalloc(sizeof(*port), GFP_KERNEL);
1963 return ERR_PTR(-ENOMEM);
1965 port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
1966 sizeof(*port->ana_state), GFP_KERNEL);
1967 if (!port->ana_state) {
1969 return ERR_PTR(-ENOMEM);
1972 if (IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS) && nvme_keyring_id()) {
1973 port->keyring = key_lookup(nvme_keyring_id());
1974 if (IS_ERR(port->keyring)) {
1975 pr_warn("NVMe keyring not available, disabling TLS\n");
1976 port->keyring = NULL;
1980 for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
1981 if (i == NVMET_DEFAULT_ANA_GRPID)
1982 port->ana_state[1] = NVME_ANA_OPTIMIZED;
1984 port->ana_state[i] = NVME_ANA_INACCESSIBLE;
1987 list_add(&port->global_entry, &nvmet_ports_list);
1989 INIT_LIST_HEAD(&port->entry);
1990 INIT_LIST_HEAD(&port->subsystems);
1991 INIT_LIST_HEAD(&port->referrals);
1992 port->inline_data_size = -1; /* < 0 == let the transport choose */
1993 port->max_queue_size = -1; /* < 0 == let the transport choose */
1995 port->disc_addr.portid = cpu_to_le16(portid);
1996 port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
1997 port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1998 config_group_init_type_name(&port->group, name, &nvmet_port_type);
2000 config_group_init_type_name(&port->subsys_group,
2001 "subsystems", &nvmet_port_subsys_type);
2002 configfs_add_default_group(&port->subsys_group, &port->group);
2004 config_group_init_type_name(&port->referrals_group,
2005 "referrals", &nvmet_referrals_type);
2006 configfs_add_default_group(&port->referrals_group, &port->group);
2008 config_group_init_type_name(&port->ana_groups_group,
2009 "ana_groups", &nvmet_ana_groups_type);
2010 configfs_add_default_group(&port->ana_groups_group, &port->group);
2012 port->ana_default_group.port = port;
2013 port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
2014 config_group_init_type_name(&port->ana_default_group.group,
2015 __stringify(NVMET_DEFAULT_ANA_GRPID),
2016 &nvmet_ana_group_type);
2017 configfs_add_default_group(&port->ana_default_group.group,
2018 &port->ana_groups_group);
2020 return &port->group;
2023 static struct configfs_group_operations nvmet_ports_group_ops = {
2024 .make_group = nvmet_ports_make,
2027 static const struct config_item_type nvmet_ports_type = {
2028 .ct_group_ops = &nvmet_ports_group_ops,
2029 .ct_owner = THIS_MODULE,
2032 static struct config_group nvmet_subsystems_group;
2033 static struct config_group nvmet_ports_group;
2035 #ifdef CONFIG_NVME_TARGET_AUTH
2036 static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
2042 down_read(&nvmet_config_sem);
2043 dhchap_secret = to_host(item)->dhchap_secret;
2045 ret = sprintf(page, "\n");
2047 ret = sprintf(page, "%s\n", dhchap_secret);
2048 up_read(&nvmet_config_sem);
2052 static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
2053 const char *page, size_t count)
2055 struct nvmet_host *host = to_host(item);
2058 ret = nvmet_auth_set_key(host, page, false);
2060 * Re-authentication is a soft state, so keep the
2061 * current authentication valid until the host
2062 * requests re-authentication.
2064 return ret < 0 ? ret : count;
2067 CONFIGFS_ATTR(nvmet_host_, dhchap_key);
2069 static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
2072 u8 *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
2075 down_read(&nvmet_config_sem);
2076 dhchap_secret = to_host(item)->dhchap_ctrl_secret;
2078 ret = sprintf(page, "\n");
2080 ret = sprintf(page, "%s\n", dhchap_secret);
2081 up_read(&nvmet_config_sem);
2085 static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
2086 const char *page, size_t count)
2088 struct nvmet_host *host = to_host(item);
2091 ret = nvmet_auth_set_key(host, page, true);
2093 * Re-authentication is a soft state, so keep the
2094 * current authentication valid until the host
2095 * requests re-authentication.
2097 return ret < 0 ? ret : count;
2100 CONFIGFS_ATTR(nvmet_host_, dhchap_ctrl_key);
2102 static ssize_t nvmet_host_dhchap_hash_show(struct config_item *item,
2105 struct nvmet_host *host = to_host(item);
2106 const char *hash_name = nvme_auth_hmac_name(host->dhchap_hash_id);
2108 return sprintf(page, "%s\n", hash_name ? hash_name : "none");
2111 static ssize_t nvmet_host_dhchap_hash_store(struct config_item *item,
2112 const char *page, size_t count)
2114 struct nvmet_host *host = to_host(item);
2117 hmac_id = nvme_auth_hmac_id(page);
2118 if (hmac_id == NVME_AUTH_HASH_INVALID)
2120 if (!crypto_has_shash(nvme_auth_hmac_name(hmac_id), 0, 0))
2122 host->dhchap_hash_id = hmac_id;
2126 CONFIGFS_ATTR(nvmet_host_, dhchap_hash);
2128 static ssize_t nvmet_host_dhchap_dhgroup_show(struct config_item *item,
2131 struct nvmet_host *host = to_host(item);
2132 const char *dhgroup = nvme_auth_dhgroup_name(host->dhchap_dhgroup_id);
2134 return sprintf(page, "%s\n", dhgroup ? dhgroup : "none");
2137 static ssize_t nvmet_host_dhchap_dhgroup_store(struct config_item *item,
2138 const char *page, size_t count)
2140 struct nvmet_host *host = to_host(item);
2143 dhgroup_id = nvme_auth_dhgroup_id(page);
2144 if (dhgroup_id == NVME_AUTH_DHGROUP_INVALID)
2146 if (dhgroup_id != NVME_AUTH_DHGROUP_NULL) {
2147 const char *kpp = nvme_auth_dhgroup_kpp(dhgroup_id);
2149 if (!crypto_has_kpp(kpp, 0, 0))
2152 host->dhchap_dhgroup_id = dhgroup_id;
2156 CONFIGFS_ATTR(nvmet_host_, dhchap_dhgroup);
2158 static struct configfs_attribute *nvmet_host_attrs[] = {
2159 &nvmet_host_attr_dhchap_key,
2160 &nvmet_host_attr_dhchap_ctrl_key,
2161 &nvmet_host_attr_dhchap_hash,
2162 &nvmet_host_attr_dhchap_dhgroup,
2165 #endif /* CONFIG_NVME_TARGET_AUTH */
2167 static void nvmet_host_release(struct config_item *item)
2169 struct nvmet_host *host = to_host(item);
2171 #ifdef CONFIG_NVME_TARGET_AUTH
2172 kfree(host->dhchap_secret);
2173 kfree(host->dhchap_ctrl_secret);
2178 static struct configfs_item_operations nvmet_host_item_ops = {
2179 .release = nvmet_host_release,
2182 static const struct config_item_type nvmet_host_type = {
2183 .ct_item_ops = &nvmet_host_item_ops,
2184 #ifdef CONFIG_NVME_TARGET_AUTH
2185 .ct_attrs = nvmet_host_attrs,
2187 .ct_owner = THIS_MODULE,
2190 static struct config_group *nvmet_hosts_make_group(struct config_group *group,
2193 struct nvmet_host *host;
2195 host = kzalloc(sizeof(*host), GFP_KERNEL);
2197 return ERR_PTR(-ENOMEM);
2199 #ifdef CONFIG_NVME_TARGET_AUTH
2200 /* Default to SHA256 */
2201 host->dhchap_hash_id = NVME_AUTH_HASH_SHA256;
2204 config_group_init_type_name(&host->group, name, &nvmet_host_type);
2206 return &host->group;
2209 static struct configfs_group_operations nvmet_hosts_group_ops = {
2210 .make_group = nvmet_hosts_make_group,
2213 static const struct config_item_type nvmet_hosts_type = {
2214 .ct_group_ops = &nvmet_hosts_group_ops,
2215 .ct_owner = THIS_MODULE,
2218 static struct config_group nvmet_hosts_group;
2220 static ssize_t nvmet_root_discovery_nqn_show(struct config_item *item,
2223 return snprintf(page, PAGE_SIZE, "%s\n", nvmet_disc_subsys->subsysnqn);
2226 static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item,
2227 const char *page, size_t count)
2229 struct list_head *entry;
2232 len = strcspn(page, "\n");
2233 if (!len || len > NVMF_NQN_FIELD_LEN - 1)
2236 down_write(&nvmet_config_sem);
2237 list_for_each(entry, &nvmet_subsystems_group.cg_children) {
2238 struct config_item *item =
2239 container_of(entry, struct config_item, ci_entry);
2241 if (!strncmp(config_item_name(item), page, len)) {
2242 pr_err("duplicate NQN %s\n", config_item_name(item));
2243 up_write(&nvmet_config_sem);
2247 memset(nvmet_disc_subsys->subsysnqn, 0, NVMF_NQN_FIELD_LEN);
2248 memcpy(nvmet_disc_subsys->subsysnqn, page, len);
2249 up_write(&nvmet_config_sem);
2254 CONFIGFS_ATTR(nvmet_root_, discovery_nqn);
2256 static struct configfs_attribute *nvmet_root_attrs[] = {
2257 &nvmet_root_attr_discovery_nqn,
2261 static const struct config_item_type nvmet_root_type = {
2262 .ct_attrs = nvmet_root_attrs,
2263 .ct_owner = THIS_MODULE,
2266 static struct configfs_subsystem nvmet_configfs_subsystem = {
2269 .ci_namebuf = "nvmet",
2270 .ci_type = &nvmet_root_type,
2275 int __init nvmet_init_configfs(void)
2279 config_group_init(&nvmet_configfs_subsystem.su_group);
2280 mutex_init(&nvmet_configfs_subsystem.su_mutex);
2282 config_group_init_type_name(&nvmet_subsystems_group,
2283 "subsystems", &nvmet_subsystems_type);
2284 configfs_add_default_group(&nvmet_subsystems_group,
2285 &nvmet_configfs_subsystem.su_group);
2287 config_group_init_type_name(&nvmet_ports_group,
2288 "ports", &nvmet_ports_type);
2289 configfs_add_default_group(&nvmet_ports_group,
2290 &nvmet_configfs_subsystem.su_group);
2292 config_group_init_type_name(&nvmet_hosts_group,
2293 "hosts", &nvmet_hosts_type);
2294 configfs_add_default_group(&nvmet_hosts_group,
2295 &nvmet_configfs_subsystem.su_group);
2297 ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
2299 pr_err("configfs_register_subsystem: %d\n", ret);
2306 void __exit nvmet_exit_configfs(void)
2308 configfs_unregister_subsystem(&nvmet_configfs_subsystem);