xref: /openbmc/linux/drivers/nvme/target/configfs.c (revision 7a277c37)
177141dc6SChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
2a07b4970SChristoph Hellwig /*
3a07b4970SChristoph Hellwig  * Configfs interface for the NVMe target.
4a07b4970SChristoph Hellwig  * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5a07b4970SChristoph Hellwig  */
6a07b4970SChristoph Hellwig #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7a07b4970SChristoph Hellwig #include <linux/kernel.h>
8a07b4970SChristoph Hellwig #include <linux/module.h>
9a07b4970SChristoph Hellwig #include <linux/slab.h>
10a07b4970SChristoph Hellwig #include <linux/stat.h>
11a07b4970SChristoph Hellwig #include <linux/ctype.h>
12c6925093SLogan Gunthorpe #include <linux/pci.h>
13c6925093SLogan Gunthorpe #include <linux/pci-p2pdma.h>
14db1312ddSHannes Reinecke #ifdef CONFIG_NVME_TARGET_AUTH
15db1312ddSHannes Reinecke #include <linux/nvme-auth.h>
16db1312ddSHannes Reinecke #endif
17db1312ddSHannes Reinecke #include <crypto/hash.h>
18db1312ddSHannes Reinecke #include <crypto/kpp.h>
19a07b4970SChristoph Hellwig 
20a07b4970SChristoph Hellwig #include "nvmet.h"
21a07b4970SChristoph Hellwig 
2266603a31SBhumika Goyal static const struct config_item_type nvmet_host_type;
2366603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type;
24a07b4970SChristoph Hellwig 
25b662a078SJay Sternberg static LIST_HEAD(nvmet_ports_list);
26b662a078SJay Sternberg struct list_head *nvmet_ports = &nvmet_ports_list;
27b662a078SJay Sternberg 
2845e2f3c2SChaitanya Kulkarni struct nvmet_type_name_map {
29a5d18612SChristoph Hellwig 	u8		type;
30a5d18612SChristoph Hellwig 	const char	*name;
3145e2f3c2SChaitanya Kulkarni };
3245e2f3c2SChaitanya Kulkarni 
3345e2f3c2SChaitanya Kulkarni static struct nvmet_type_name_map nvmet_transport[] = {
34a5d18612SChristoph Hellwig 	{ NVMF_TRTYPE_RDMA,	"rdma" },
35a5d18612SChristoph Hellwig 	{ NVMF_TRTYPE_FC,	"fc" },
36ad4f530eSSagi Grimberg 	{ NVMF_TRTYPE_TCP,	"tcp" },
37a5d18612SChristoph Hellwig 	{ NVMF_TRTYPE_LOOP,	"loop" },
38a5d18612SChristoph Hellwig };
39a5d18612SChristoph Hellwig 
407e764179SChaitanya Kulkarni static const struct nvmet_type_name_map nvmet_addr_family[] = {
417e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_PCI,		"pcie" },
427e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_IP4,		"ipv4" },
437e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_IP6,		"ipv6" },
447e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_IB,		"ib" },
457e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_FC,		"fc" },
46d02abd19SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_LOOP,	"loop" },
477e764179SChaitanya Kulkarni };
487e764179SChaitanya Kulkarni 
493ecb5faaSChaitanya Kulkarni static bool nvmet_is_port_enabled(struct nvmet_port *p, const char *caller)
503ecb5faaSChaitanya Kulkarni {
513ecb5faaSChaitanya Kulkarni 	if (p->enabled)
523ecb5faaSChaitanya Kulkarni 		pr_err("Disable port '%u' before changing attribute in %s\n",
533ecb5faaSChaitanya Kulkarni 		       le16_to_cpu(p->disc_addr.portid), caller);
543ecb5faaSChaitanya Kulkarni 	return p->enabled;
553ecb5faaSChaitanya Kulkarni }
563ecb5faaSChaitanya Kulkarni 
57a07b4970SChristoph Hellwig /*
58a07b4970SChristoph Hellwig  * nvmet_port Generic ConfigFS definitions.
59a07b4970SChristoph Hellwig  * Used in any place in the ConfigFS tree that refers to an address.
60a07b4970SChristoph Hellwig  */
617e764179SChaitanya Kulkarni static ssize_t nvmet_addr_adrfam_show(struct config_item *item, char *page)
62a07b4970SChristoph Hellwig {
637e764179SChaitanya Kulkarni 	u8 adrfam = to_nvmet_port(item)->disc_addr.adrfam;
647e764179SChaitanya Kulkarni 	int i;
657e764179SChaitanya Kulkarni 
667e764179SChaitanya Kulkarni 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
677e764179SChaitanya Kulkarni 		if (nvmet_addr_family[i].type == adrfam)
6898152eb7SChaitanya Kulkarni 			return snprintf(page, PAGE_SIZE, "%s\n",
6998152eb7SChaitanya Kulkarni 					nvmet_addr_family[i].name);
70a07b4970SChristoph Hellwig 	}
717e764179SChaitanya Kulkarni 
7298152eb7SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "\n");
73a07b4970SChristoph Hellwig }
74a07b4970SChristoph Hellwig 
75a07b4970SChristoph Hellwig static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
76a07b4970SChristoph Hellwig 		const char *page, size_t count)
77a07b4970SChristoph Hellwig {
78a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
797e764179SChaitanya Kulkarni 	int i;
80a07b4970SChristoph Hellwig 
813ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
82a07b4970SChristoph Hellwig 		return -EACCES;
83a07b4970SChristoph Hellwig 
847e764179SChaitanya Kulkarni 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
857e764179SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_addr_family[i].name))
867e764179SChaitanya Kulkarni 			goto found;
87a07b4970SChristoph Hellwig 	}
88a07b4970SChristoph Hellwig 
897e764179SChaitanya Kulkarni 	pr_err("Invalid value '%s' for adrfam\n", page);
907e764179SChaitanya Kulkarni 	return -EINVAL;
917e764179SChaitanya Kulkarni 
927e764179SChaitanya Kulkarni found:
93d02abd19SChaitanya Kulkarni 	port->disc_addr.adrfam = nvmet_addr_family[i].type;
94a07b4970SChristoph Hellwig 	return count;
95a07b4970SChristoph Hellwig }
96a07b4970SChristoph Hellwig 
97a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_adrfam);
98a07b4970SChristoph Hellwig 
99a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_show(struct config_item *item,
100a07b4970SChristoph Hellwig 		char *page)
101a07b4970SChristoph Hellwig {
10273d77c53SChaitanya Kulkarni 	__le16 portid = to_nvmet_port(item)->disc_addr.portid;
103a07b4970SChristoph Hellwig 
10473d77c53SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%d\n", le16_to_cpu(portid));
105a07b4970SChristoph Hellwig }
106a07b4970SChristoph Hellwig 
107a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_store(struct config_item *item,
108a07b4970SChristoph Hellwig 		const char *page, size_t count)
109a07b4970SChristoph Hellwig {
110a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
111a07b4970SChristoph Hellwig 	u16 portid = 0;
112a07b4970SChristoph Hellwig 
113a07b4970SChristoph Hellwig 	if (kstrtou16(page, 0, &portid)) {
114a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for portid\n", page);
115a07b4970SChristoph Hellwig 		return -EINVAL;
116a07b4970SChristoph Hellwig 	}
117a07b4970SChristoph Hellwig 
1183ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
119a07b4970SChristoph Hellwig 		return -EACCES;
1203ecb5faaSChaitanya Kulkarni 
121a07b4970SChristoph Hellwig 	port->disc_addr.portid = cpu_to_le16(portid);
122a07b4970SChristoph Hellwig 	return count;
123a07b4970SChristoph Hellwig }
124a07b4970SChristoph Hellwig 
125a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_portid);
126a07b4970SChristoph Hellwig 
127a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_show(struct config_item *item,
128a07b4970SChristoph Hellwig 		char *page)
129a07b4970SChristoph Hellwig {
130a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
131a07b4970SChristoph Hellwig 
13273d77c53SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.traddr);
133a07b4970SChristoph Hellwig }
134a07b4970SChristoph Hellwig 
135a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_store(struct config_item *item,
136a07b4970SChristoph Hellwig 		const char *page, size_t count)
137a07b4970SChristoph Hellwig {
138a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
139a07b4970SChristoph Hellwig 
140a07b4970SChristoph Hellwig 	if (count > NVMF_TRADDR_SIZE) {
141a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for traddr\n", page);
142a07b4970SChristoph Hellwig 		return -EINVAL;
143a07b4970SChristoph Hellwig 	}
144a07b4970SChristoph Hellwig 
1453ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
146a07b4970SChristoph Hellwig 		return -EACCES;
1479ba2a5cbSSagi Grimberg 
1489ba2a5cbSSagi Grimberg 	if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
1499ba2a5cbSSagi Grimberg 		return -EINVAL;
1509ba2a5cbSSagi Grimberg 	return count;
151a07b4970SChristoph Hellwig }
152a07b4970SChristoph Hellwig 
153a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_traddr);
154a07b4970SChristoph Hellwig 
15587628e28SChaitanya Kulkarni static const struct nvmet_type_name_map nvmet_addr_treq[] = {
15687628e28SChaitanya Kulkarni 	{ NVMF_TREQ_NOT_SPECIFIED,	"not specified" },
15787628e28SChaitanya Kulkarni 	{ NVMF_TREQ_REQUIRED,		"required" },
15887628e28SChaitanya Kulkarni 	{ NVMF_TREQ_NOT_REQUIRED,	"not required" },
15987628e28SChaitanya Kulkarni };
16087628e28SChaitanya Kulkarni 
16187628e28SChaitanya Kulkarni static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page)
162a07b4970SChristoph Hellwig {
16387628e28SChaitanya Kulkarni 	u8 treq = to_nvmet_port(item)->disc_addr.treq &
16487628e28SChaitanya Kulkarni 		NVME_TREQ_SECURE_CHANNEL_MASK;
16587628e28SChaitanya Kulkarni 	int i;
16687628e28SChaitanya Kulkarni 
16787628e28SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
16887628e28SChaitanya Kulkarni 		if (treq == nvmet_addr_treq[i].type)
16998152eb7SChaitanya Kulkarni 			return snprintf(page, PAGE_SIZE, "%s\n",
17098152eb7SChaitanya Kulkarni 					nvmet_addr_treq[i].name);
171a07b4970SChristoph Hellwig 	}
17287628e28SChaitanya Kulkarni 
17398152eb7SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "\n");
174a07b4970SChristoph Hellwig }
175a07b4970SChristoph Hellwig 
176a07b4970SChristoph Hellwig static ssize_t nvmet_addr_treq_store(struct config_item *item,
177a07b4970SChristoph Hellwig 		const char *page, size_t count)
178a07b4970SChristoph Hellwig {
179a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1800445e1b5SSagi Grimberg 	u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK;
18187628e28SChaitanya Kulkarni 	int i;
182a07b4970SChristoph Hellwig 
1833ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
184a07b4970SChristoph Hellwig 		return -EACCES;
185a07b4970SChristoph Hellwig 
18687628e28SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
18787628e28SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_addr_treq[i].name))
18887628e28SChaitanya Kulkarni 			goto found;
18987628e28SChaitanya Kulkarni 	}
19087628e28SChaitanya Kulkarni 
191a07b4970SChristoph Hellwig 	pr_err("Invalid value '%s' for treq\n", page);
192a07b4970SChristoph Hellwig 	return -EINVAL;
193a07b4970SChristoph Hellwig 
19487628e28SChaitanya Kulkarni found:
19587628e28SChaitanya Kulkarni 	treq |= nvmet_addr_treq[i].type;
19687628e28SChaitanya Kulkarni 	port->disc_addr.treq = treq;
197a07b4970SChristoph Hellwig 	return count;
198a07b4970SChristoph Hellwig }
199a07b4970SChristoph Hellwig 
200a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_treq);
201a07b4970SChristoph Hellwig 
202a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
203a07b4970SChristoph Hellwig 		char *page)
204a07b4970SChristoph Hellwig {
205a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
206a07b4970SChristoph Hellwig 
20773d77c53SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.trsvcid);
208a07b4970SChristoph Hellwig }
209a07b4970SChristoph Hellwig 
210a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
211a07b4970SChristoph Hellwig 		const char *page, size_t count)
212a07b4970SChristoph Hellwig {
213a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
214a07b4970SChristoph Hellwig 
215a07b4970SChristoph Hellwig 	if (count > NVMF_TRSVCID_SIZE) {
216a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for trsvcid\n", page);
217a07b4970SChristoph Hellwig 		return -EINVAL;
218a07b4970SChristoph Hellwig 	}
2193ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
220a07b4970SChristoph Hellwig 		return -EACCES;
2219ba2a5cbSSagi Grimberg 
2229ba2a5cbSSagi Grimberg 	if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
2239ba2a5cbSSagi Grimberg 		return -EINVAL;
2249ba2a5cbSSagi Grimberg 	return count;
225a07b4970SChristoph Hellwig }
226a07b4970SChristoph Hellwig 
227a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trsvcid);
228a07b4970SChristoph Hellwig 
2290d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
2300d5ee2b2SSteve Wise 		char *page)
2310d5ee2b2SSteve Wise {
2320d5ee2b2SSteve Wise 	struct nvmet_port *port = to_nvmet_port(item);
2330d5ee2b2SSteve Wise 
2340d5ee2b2SSteve Wise 	return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
2350d5ee2b2SSteve Wise }
2360d5ee2b2SSteve Wise 
2370d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
2380d5ee2b2SSteve Wise 		const char *page, size_t count)
2390d5ee2b2SSteve Wise {
2400d5ee2b2SSteve Wise 	struct nvmet_port *port = to_nvmet_port(item);
2410d5ee2b2SSteve Wise 	int ret;
2420d5ee2b2SSteve Wise 
2433ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
2440d5ee2b2SSteve Wise 		return -EACCES;
2450d5ee2b2SSteve Wise 	ret = kstrtoint(page, 0, &port->inline_data_size);
2460d5ee2b2SSteve Wise 	if (ret) {
2470d5ee2b2SSteve Wise 		pr_err("Invalid value '%s' for inline_data_size\n", page);
2480d5ee2b2SSteve Wise 		return -EINVAL;
2490d5ee2b2SSteve Wise 	}
2500d5ee2b2SSteve Wise 	return count;
2510d5ee2b2SSteve Wise }
2520d5ee2b2SSteve Wise 
2530d5ee2b2SSteve Wise CONFIGFS_ATTR(nvmet_, param_inline_data_size);
2540d5ee2b2SSteve Wise 
255ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
256ea52ac1cSIsrael Rukshin static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
257ea52ac1cSIsrael Rukshin 		char *page)
258ea52ac1cSIsrael Rukshin {
259ea52ac1cSIsrael Rukshin 	struct nvmet_port *port = to_nvmet_port(item);
260ea52ac1cSIsrael Rukshin 
261ea52ac1cSIsrael Rukshin 	return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
262ea52ac1cSIsrael Rukshin }
263ea52ac1cSIsrael Rukshin 
264ea52ac1cSIsrael Rukshin static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
265ea52ac1cSIsrael Rukshin 		const char *page, size_t count)
266ea52ac1cSIsrael Rukshin {
267ea52ac1cSIsrael Rukshin 	struct nvmet_port *port = to_nvmet_port(item);
268ea52ac1cSIsrael Rukshin 	bool val;
269ea52ac1cSIsrael Rukshin 
270ea52ac1cSIsrael Rukshin 	if (strtobool(page, &val))
271ea52ac1cSIsrael Rukshin 		return -EINVAL;
272ea52ac1cSIsrael Rukshin 
273cc345622SIsrael Rukshin 	if (nvmet_is_port_enabled(port, __func__))
274ea52ac1cSIsrael Rukshin 		return -EACCES;
275ea52ac1cSIsrael Rukshin 
276ea52ac1cSIsrael Rukshin 	port->pi_enable = val;
277ea52ac1cSIsrael Rukshin 	return count;
278ea52ac1cSIsrael Rukshin }
279ea52ac1cSIsrael Rukshin 
280ea52ac1cSIsrael Rukshin CONFIGFS_ATTR(nvmet_, param_pi_enable);
281ea52ac1cSIsrael Rukshin #endif
282ea52ac1cSIsrael Rukshin 
283a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_show(struct config_item *item,
284a07b4970SChristoph Hellwig 		char *page)
285a07b4970SChristoph Hellwig {
286a5d18612SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
287a5d18612SChristoph Hellwig 	int i;
288a5d18612SChristoph Hellwig 
28945e2f3c2SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
29045e2f3c2SChaitanya Kulkarni 		if (port->disc_addr.trtype == nvmet_transport[i].type)
29198152eb7SChaitanya Kulkarni 			return snprintf(page, PAGE_SIZE,
29298152eb7SChaitanya Kulkarni 					"%s\n", nvmet_transport[i].name);
293a07b4970SChristoph Hellwig 	}
294a5d18612SChristoph Hellwig 
295a5d18612SChristoph Hellwig 	return sprintf(page, "\n");
296a07b4970SChristoph Hellwig }
297a07b4970SChristoph Hellwig 
298a07b4970SChristoph Hellwig static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
299a07b4970SChristoph Hellwig {
300a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
301a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
302a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
303a07b4970SChristoph Hellwig }
304a07b4970SChristoph Hellwig 
305a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_store(struct config_item *item,
306a07b4970SChristoph Hellwig 		const char *page, size_t count)
307a07b4970SChristoph Hellwig {
308a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
309a5d18612SChristoph Hellwig 	int i;
310a07b4970SChristoph Hellwig 
3113ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
312a07b4970SChristoph Hellwig 		return -EACCES;
313a07b4970SChristoph Hellwig 
31445e2f3c2SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
31545e2f3c2SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_transport[i].name))
316a5d18612SChristoph Hellwig 			goto found;
317a07b4970SChristoph Hellwig 	}
318a07b4970SChristoph Hellwig 
319a5d18612SChristoph Hellwig 	pr_err("Invalid value '%s' for trtype\n", page);
320a5d18612SChristoph Hellwig 	return -EINVAL;
3217e764179SChaitanya Kulkarni 
322a5d18612SChristoph Hellwig found:
323a5d18612SChristoph Hellwig 	memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
32445e2f3c2SChaitanya Kulkarni 	port->disc_addr.trtype = nvmet_transport[i].type;
325a5d18612SChristoph Hellwig 	if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
326a5d18612SChristoph Hellwig 		nvmet_port_init_tsas_rdma(port);
327a07b4970SChristoph Hellwig 	return count;
328a07b4970SChristoph Hellwig }
329a07b4970SChristoph Hellwig 
330a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trtype);
331a07b4970SChristoph Hellwig 
332a07b4970SChristoph Hellwig /*
333a07b4970SChristoph Hellwig  * Namespace structures & file operation functions below
334a07b4970SChristoph Hellwig  */
335a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
336a07b4970SChristoph Hellwig {
337a07b4970SChristoph Hellwig 	return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
338a07b4970SChristoph Hellwig }
339a07b4970SChristoph Hellwig 
340a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_store(struct config_item *item,
341a07b4970SChristoph Hellwig 		const char *page, size_t count)
342a07b4970SChristoph Hellwig {
343a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
344a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = ns->subsys;
3455613d312SHannes Reinecke 	size_t len;
346a07b4970SChristoph Hellwig 	int ret;
347a07b4970SChristoph Hellwig 
348a07b4970SChristoph Hellwig 	mutex_lock(&subsys->lock);
349a07b4970SChristoph Hellwig 	ret = -EBUSY;
350e4fcf07cSSolganik Alexander 	if (ns->enabled)
351a07b4970SChristoph Hellwig 		goto out_unlock;
352a07b4970SChristoph Hellwig 
3535613d312SHannes Reinecke 	ret = -EINVAL;
3545613d312SHannes Reinecke 	len = strcspn(page, "\n");
3555613d312SHannes Reinecke 	if (!len)
3565613d312SHannes Reinecke 		goto out_unlock;
357a07b4970SChristoph Hellwig 
3585613d312SHannes Reinecke 	kfree(ns->device_path);
359a07b4970SChristoph Hellwig 	ret = -ENOMEM;
36009bb8986SChen Zhou 	ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
361a07b4970SChristoph Hellwig 	if (!ns->device_path)
362a07b4970SChristoph Hellwig 		goto out_unlock;
363a07b4970SChristoph Hellwig 
364a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
365a07b4970SChristoph Hellwig 	return count;
366a07b4970SChristoph Hellwig 
367a07b4970SChristoph Hellwig out_unlock:
368a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
369a07b4970SChristoph Hellwig 	return ret;
370a07b4970SChristoph Hellwig }
371a07b4970SChristoph Hellwig 
372a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_path);
373a07b4970SChristoph Hellwig 
374c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA
375c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
376c6925093SLogan Gunthorpe {
377c6925093SLogan Gunthorpe 	struct nvmet_ns *ns = to_nvmet_ns(item);
378c6925093SLogan Gunthorpe 
379c6925093SLogan Gunthorpe 	return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
380c6925093SLogan Gunthorpe }
381c6925093SLogan Gunthorpe 
382c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
383c6925093SLogan Gunthorpe 		const char *page, size_t count)
384c6925093SLogan Gunthorpe {
385c6925093SLogan Gunthorpe 	struct nvmet_ns *ns = to_nvmet_ns(item);
386c6925093SLogan Gunthorpe 	struct pci_dev *p2p_dev = NULL;
387c6925093SLogan Gunthorpe 	bool use_p2pmem;
388c6925093SLogan Gunthorpe 	int ret = count;
389c6925093SLogan Gunthorpe 	int error;
390c6925093SLogan Gunthorpe 
391c6925093SLogan Gunthorpe 	mutex_lock(&ns->subsys->lock);
392c6925093SLogan Gunthorpe 	if (ns->enabled) {
393c6925093SLogan Gunthorpe 		ret = -EBUSY;
394c6925093SLogan Gunthorpe 		goto out_unlock;
395c6925093SLogan Gunthorpe 	}
396c6925093SLogan Gunthorpe 
397c6925093SLogan Gunthorpe 	error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
398c6925093SLogan Gunthorpe 	if (error) {
399c6925093SLogan Gunthorpe 		ret = error;
400c6925093SLogan Gunthorpe 		goto out_unlock;
401c6925093SLogan Gunthorpe 	}
402c6925093SLogan Gunthorpe 
403c6925093SLogan Gunthorpe 	ns->use_p2pmem = use_p2pmem;
404c6925093SLogan Gunthorpe 	pci_dev_put(ns->p2p_dev);
405c6925093SLogan Gunthorpe 	ns->p2p_dev = p2p_dev;
406c6925093SLogan Gunthorpe 
407c6925093SLogan Gunthorpe out_unlock:
408c6925093SLogan Gunthorpe 	mutex_unlock(&ns->subsys->lock);
409c6925093SLogan Gunthorpe 
410c6925093SLogan Gunthorpe 	return ret;
411c6925093SLogan Gunthorpe }
412c6925093SLogan Gunthorpe 
413c6925093SLogan Gunthorpe CONFIGFS_ATTR(nvmet_ns_, p2pmem);
414c6925093SLogan Gunthorpe #endif /* CONFIG_PCI_P2PDMA */
415c6925093SLogan Gunthorpe 
416430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
417430c7befSJohannes Thumshirn {
418430c7befSJohannes Thumshirn 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
419430c7befSJohannes Thumshirn }
420430c7befSJohannes Thumshirn 
421430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
422430c7befSJohannes Thumshirn 					  const char *page, size_t count)
423430c7befSJohannes Thumshirn {
424430c7befSJohannes Thumshirn 	struct nvmet_ns *ns = to_nvmet_ns(item);
425430c7befSJohannes Thumshirn 	struct nvmet_subsys *subsys = ns->subsys;
426430c7befSJohannes Thumshirn 	int ret = 0;
427430c7befSJohannes Thumshirn 
428430c7befSJohannes Thumshirn 	mutex_lock(&subsys->lock);
429430c7befSJohannes Thumshirn 	if (ns->enabled) {
430430c7befSJohannes Thumshirn 		ret = -EBUSY;
431430c7befSJohannes Thumshirn 		goto out_unlock;
432430c7befSJohannes Thumshirn 	}
433430c7befSJohannes Thumshirn 
434430c7befSJohannes Thumshirn 	if (uuid_parse(page, &ns->uuid))
435430c7befSJohannes Thumshirn 		ret = -EINVAL;
436430c7befSJohannes Thumshirn 
437430c7befSJohannes Thumshirn out_unlock:
438430c7befSJohannes Thumshirn 	mutex_unlock(&subsys->lock);
439430c7befSJohannes Thumshirn 	return ret ? ret : count;
440430c7befSJohannes Thumshirn }
441430c7befSJohannes Thumshirn 
442f871749aSMax Gurtovoy CONFIGFS_ATTR(nvmet_ns_, device_uuid);
443f871749aSMax Gurtovoy 
444a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
445a07b4970SChristoph Hellwig {
446a07b4970SChristoph Hellwig 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
447a07b4970SChristoph Hellwig }
448a07b4970SChristoph Hellwig 
449a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
450a07b4970SChristoph Hellwig 		const char *page, size_t count)
451a07b4970SChristoph Hellwig {
452a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
453a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = ns->subsys;
454a07b4970SChristoph Hellwig 	u8 nguid[16];
455a07b4970SChristoph Hellwig 	const char *p = page;
456a07b4970SChristoph Hellwig 	int i;
457a07b4970SChristoph Hellwig 	int ret = 0;
458a07b4970SChristoph Hellwig 
459a07b4970SChristoph Hellwig 	mutex_lock(&subsys->lock);
460e4fcf07cSSolganik Alexander 	if (ns->enabled) {
461a07b4970SChristoph Hellwig 		ret = -EBUSY;
462a07b4970SChristoph Hellwig 		goto out_unlock;
463a07b4970SChristoph Hellwig 	}
464a07b4970SChristoph Hellwig 
465a07b4970SChristoph Hellwig 	for (i = 0; i < 16; i++) {
466a07b4970SChristoph Hellwig 		if (p + 2 > page + count) {
467a07b4970SChristoph Hellwig 			ret = -EINVAL;
468a07b4970SChristoph Hellwig 			goto out_unlock;
469a07b4970SChristoph Hellwig 		}
470a07b4970SChristoph Hellwig 		if (!isxdigit(p[0]) || !isxdigit(p[1])) {
471a07b4970SChristoph Hellwig 			ret = -EINVAL;
472a07b4970SChristoph Hellwig 			goto out_unlock;
473a07b4970SChristoph Hellwig 		}
474a07b4970SChristoph Hellwig 
475a07b4970SChristoph Hellwig 		nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
476a07b4970SChristoph Hellwig 		p += 2;
477a07b4970SChristoph Hellwig 
478a07b4970SChristoph Hellwig 		if (*p == '-' || *p == ':')
479a07b4970SChristoph Hellwig 			p++;
480a07b4970SChristoph Hellwig 	}
481a07b4970SChristoph Hellwig 
482a07b4970SChristoph Hellwig 	memcpy(&ns->nguid, nguid, sizeof(nguid));
483a07b4970SChristoph Hellwig out_unlock:
484a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
485a07b4970SChristoph Hellwig 	return ret ? ret : count;
486a07b4970SChristoph Hellwig }
487a07b4970SChristoph Hellwig 
488a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_nguid);
489a07b4970SChristoph Hellwig 
49062ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
49162ac0d32SChristoph Hellwig {
49262ac0d32SChristoph Hellwig 	return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
49362ac0d32SChristoph Hellwig }
49462ac0d32SChristoph Hellwig 
49562ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
49662ac0d32SChristoph Hellwig 		const char *page, size_t count)
49762ac0d32SChristoph Hellwig {
49862ac0d32SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
49962ac0d32SChristoph Hellwig 	u32 oldgrpid, newgrpid;
50062ac0d32SChristoph Hellwig 	int ret;
50162ac0d32SChristoph Hellwig 
50262ac0d32SChristoph Hellwig 	ret = kstrtou32(page, 0, &newgrpid);
50362ac0d32SChristoph Hellwig 	if (ret)
50462ac0d32SChristoph Hellwig 		return ret;
50562ac0d32SChristoph Hellwig 
50662ac0d32SChristoph Hellwig 	if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
50762ac0d32SChristoph Hellwig 		return -EINVAL;
50862ac0d32SChristoph Hellwig 
50962ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
51062ac0d32SChristoph Hellwig 	oldgrpid = ns->anagrpid;
51162ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[newgrpid]++;
51262ac0d32SChristoph Hellwig 	ns->anagrpid = newgrpid;
51362ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[oldgrpid]--;
51462ac0d32SChristoph Hellwig 	nvmet_ana_chgcnt++;
51562ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
51662ac0d32SChristoph Hellwig 
51762ac0d32SChristoph Hellwig 	nvmet_send_ana_event(ns->subsys, NULL);
51862ac0d32SChristoph Hellwig 	return count;
51962ac0d32SChristoph Hellwig }
52062ac0d32SChristoph Hellwig 
52162ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
52262ac0d32SChristoph Hellwig 
523a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
524a07b4970SChristoph Hellwig {
525e4fcf07cSSolganik Alexander 	return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
526a07b4970SChristoph Hellwig }
527a07b4970SChristoph Hellwig 
528a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_store(struct config_item *item,
529a07b4970SChristoph Hellwig 		const char *page, size_t count)
530a07b4970SChristoph Hellwig {
531a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
532a07b4970SChristoph Hellwig 	bool enable;
533a07b4970SChristoph Hellwig 	int ret = 0;
534a07b4970SChristoph Hellwig 
535a07b4970SChristoph Hellwig 	if (strtobool(page, &enable))
536a07b4970SChristoph Hellwig 		return -EINVAL;
537a07b4970SChristoph Hellwig 
538a07b4970SChristoph Hellwig 	if (enable)
539a07b4970SChristoph Hellwig 		ret = nvmet_ns_enable(ns);
540a07b4970SChristoph Hellwig 	else
541a07b4970SChristoph Hellwig 		nvmet_ns_disable(ns);
542a07b4970SChristoph Hellwig 
543a07b4970SChristoph Hellwig 	return ret ? ret : count;
544a07b4970SChristoph Hellwig }
545a07b4970SChristoph Hellwig 
546a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, enable);
547a07b4970SChristoph Hellwig 
54855eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
54955eb942eSChaitanya Kulkarni {
55055eb942eSChaitanya Kulkarni 	return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
55155eb942eSChaitanya Kulkarni }
55255eb942eSChaitanya Kulkarni 
55355eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
55455eb942eSChaitanya Kulkarni 		const char *page, size_t count)
55555eb942eSChaitanya Kulkarni {
55655eb942eSChaitanya Kulkarni 	struct nvmet_ns *ns = to_nvmet_ns(item);
55755eb942eSChaitanya Kulkarni 	bool val;
55855eb942eSChaitanya Kulkarni 
55955eb942eSChaitanya Kulkarni 	if (strtobool(page, &val))
56055eb942eSChaitanya Kulkarni 		return -EINVAL;
56155eb942eSChaitanya Kulkarni 
56255eb942eSChaitanya Kulkarni 	mutex_lock(&ns->subsys->lock);
56355eb942eSChaitanya Kulkarni 	if (ns->enabled) {
56455eb942eSChaitanya Kulkarni 		pr_err("disable ns before setting buffered_io value.\n");
56555eb942eSChaitanya Kulkarni 		mutex_unlock(&ns->subsys->lock);
56655eb942eSChaitanya Kulkarni 		return -EINVAL;
56755eb942eSChaitanya Kulkarni 	}
56855eb942eSChaitanya Kulkarni 
56955eb942eSChaitanya Kulkarni 	ns->buffered_io = val;
57055eb942eSChaitanya Kulkarni 	mutex_unlock(&ns->subsys->lock);
57155eb942eSChaitanya Kulkarni 	return count;
57255eb942eSChaitanya Kulkarni }
57355eb942eSChaitanya Kulkarni 
57455eb942eSChaitanya Kulkarni CONFIGFS_ATTR(nvmet_ns_, buffered_io);
57555eb942eSChaitanya Kulkarni 
5761f357548SChaitanya Kulkarni static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
5771f357548SChaitanya Kulkarni 		const char *page, size_t count)
5781f357548SChaitanya Kulkarni {
5791f357548SChaitanya Kulkarni 	struct nvmet_ns *ns = to_nvmet_ns(item);
5801f357548SChaitanya Kulkarni 	bool val;
5811f357548SChaitanya Kulkarni 
5821f357548SChaitanya Kulkarni 	if (strtobool(page, &val))
5831f357548SChaitanya Kulkarni 		return -EINVAL;
5841f357548SChaitanya Kulkarni 
5851f357548SChaitanya Kulkarni 	if (!val)
5861f357548SChaitanya Kulkarni 		return -EINVAL;
5871f357548SChaitanya Kulkarni 
5881f357548SChaitanya Kulkarni 	mutex_lock(&ns->subsys->lock);
5891f357548SChaitanya Kulkarni 	if (!ns->enabled) {
5901f357548SChaitanya Kulkarni 		pr_err("enable ns before revalidate.\n");
5911f357548SChaitanya Kulkarni 		mutex_unlock(&ns->subsys->lock);
5921f357548SChaitanya Kulkarni 		return -EINVAL;
5931f357548SChaitanya Kulkarni 	}
594da783733SChristoph Hellwig 	if (nvmet_ns_revalidate(ns))
595da783733SChristoph Hellwig 		nvmet_ns_changed(ns->subsys, ns->nsid);
5961f357548SChaitanya Kulkarni 	mutex_unlock(&ns->subsys->lock);
5971f357548SChaitanya Kulkarni 	return count;
5981f357548SChaitanya Kulkarni }
5991f357548SChaitanya Kulkarni 
6001f357548SChaitanya Kulkarni CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
6011f357548SChaitanya Kulkarni 
602a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_ns_attrs[] = {
603a07b4970SChristoph Hellwig 	&nvmet_ns_attr_device_path,
604a07b4970SChristoph Hellwig 	&nvmet_ns_attr_device_nguid,
605430c7befSJohannes Thumshirn 	&nvmet_ns_attr_device_uuid,
60662ac0d32SChristoph Hellwig 	&nvmet_ns_attr_ana_grpid,
607a07b4970SChristoph Hellwig 	&nvmet_ns_attr_enable,
60855eb942eSChaitanya Kulkarni 	&nvmet_ns_attr_buffered_io,
6091f357548SChaitanya Kulkarni 	&nvmet_ns_attr_revalidate_size,
610c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA
611c6925093SLogan Gunthorpe 	&nvmet_ns_attr_p2pmem,
612c6925093SLogan Gunthorpe #endif
613a07b4970SChristoph Hellwig 	NULL,
614a07b4970SChristoph Hellwig };
615a07b4970SChristoph Hellwig 
616a07b4970SChristoph Hellwig static void nvmet_ns_release(struct config_item *item)
617a07b4970SChristoph Hellwig {
618a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
619a07b4970SChristoph Hellwig 
620a07b4970SChristoph Hellwig 	nvmet_ns_free(ns);
621a07b4970SChristoph Hellwig }
622a07b4970SChristoph Hellwig 
623a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_ns_item_ops = {
624a07b4970SChristoph Hellwig 	.release		= nvmet_ns_release,
625a07b4970SChristoph Hellwig };
626a07b4970SChristoph Hellwig 
62766603a31SBhumika Goyal static const struct config_item_type nvmet_ns_type = {
628a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_ns_item_ops,
629a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_ns_attrs,
630a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
631a07b4970SChristoph Hellwig };
632a07b4970SChristoph Hellwig 
633a07b4970SChristoph Hellwig static struct config_group *nvmet_ns_make(struct config_group *group,
634a07b4970SChristoph Hellwig 		const char *name)
635a07b4970SChristoph Hellwig {
636a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
637a07b4970SChristoph Hellwig 	struct nvmet_ns *ns;
638a07b4970SChristoph Hellwig 	int ret;
639a07b4970SChristoph Hellwig 	u32 nsid;
640a07b4970SChristoph Hellwig 
641a07b4970SChristoph Hellwig 	ret = kstrtou32(name, 0, &nsid);
642a07b4970SChristoph Hellwig 	if (ret)
643a07b4970SChristoph Hellwig 		goto out;
644a07b4970SChristoph Hellwig 
645a07b4970SChristoph Hellwig 	ret = -EINVAL;
6465ba89503SMikhail Skorzhinskii 	if (nsid == 0 || nsid == NVME_NSID_ALL) {
6475ba89503SMikhail Skorzhinskii 		pr_err("invalid nsid %#x", nsid);
648a07b4970SChristoph Hellwig 		goto out;
6495ba89503SMikhail Skorzhinskii 	}
650a07b4970SChristoph Hellwig 
651a07b4970SChristoph Hellwig 	ret = -ENOMEM;
652a07b4970SChristoph Hellwig 	ns = nvmet_ns_alloc(subsys, nsid);
653a07b4970SChristoph Hellwig 	if (!ns)
654a07b4970SChristoph Hellwig 		goto out;
655a07b4970SChristoph Hellwig 	config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
656a07b4970SChristoph Hellwig 
657a07b4970SChristoph Hellwig 	pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
658a07b4970SChristoph Hellwig 
659a07b4970SChristoph Hellwig 	return &ns->group;
660a07b4970SChristoph Hellwig out:
661a07b4970SChristoph Hellwig 	return ERR_PTR(ret);
662a07b4970SChristoph Hellwig }
663a07b4970SChristoph Hellwig 
664a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_namespaces_group_ops = {
665a07b4970SChristoph Hellwig 	.make_group		= nvmet_ns_make,
666a07b4970SChristoph Hellwig };
667a07b4970SChristoph Hellwig 
66866603a31SBhumika Goyal static const struct config_item_type nvmet_namespaces_type = {
669a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_namespaces_group_ops,
670a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
671a07b4970SChristoph Hellwig };
672a07b4970SChristoph Hellwig 
673cae5b01aSLogan Gunthorpe #ifdef CONFIG_NVME_TARGET_PASSTHRU
674cae5b01aSLogan Gunthorpe 
675cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
676cae5b01aSLogan Gunthorpe 		char *page)
677cae5b01aSLogan Gunthorpe {
678cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
679cae5b01aSLogan Gunthorpe 
680cae5b01aSLogan Gunthorpe 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
681cae5b01aSLogan Gunthorpe }
682cae5b01aSLogan Gunthorpe 
683cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
684cae5b01aSLogan Gunthorpe 		const char *page, size_t count)
685cae5b01aSLogan Gunthorpe {
686cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
687cae5b01aSLogan Gunthorpe 	size_t len;
688cae5b01aSLogan Gunthorpe 	int ret;
689cae5b01aSLogan Gunthorpe 
690cae5b01aSLogan Gunthorpe 	mutex_lock(&subsys->lock);
691cae5b01aSLogan Gunthorpe 
692cae5b01aSLogan Gunthorpe 	ret = -EBUSY;
693cae5b01aSLogan Gunthorpe 	if (subsys->passthru_ctrl)
694cae5b01aSLogan Gunthorpe 		goto out_unlock;
695cae5b01aSLogan Gunthorpe 
696cae5b01aSLogan Gunthorpe 	ret = -EINVAL;
697cae5b01aSLogan Gunthorpe 	len = strcspn(page, "\n");
698cae5b01aSLogan Gunthorpe 	if (!len)
699cae5b01aSLogan Gunthorpe 		goto out_unlock;
700cae5b01aSLogan Gunthorpe 
701cae5b01aSLogan Gunthorpe 	kfree(subsys->passthru_ctrl_path);
702cae5b01aSLogan Gunthorpe 	ret = -ENOMEM;
703cae5b01aSLogan Gunthorpe 	subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
704cae5b01aSLogan Gunthorpe 	if (!subsys->passthru_ctrl_path)
705cae5b01aSLogan Gunthorpe 		goto out_unlock;
706cae5b01aSLogan Gunthorpe 
707cae5b01aSLogan Gunthorpe 	mutex_unlock(&subsys->lock);
708cae5b01aSLogan Gunthorpe 
709cae5b01aSLogan Gunthorpe 	return count;
710cae5b01aSLogan Gunthorpe out_unlock:
711cae5b01aSLogan Gunthorpe 	mutex_unlock(&subsys->lock);
712cae5b01aSLogan Gunthorpe 	return ret;
713cae5b01aSLogan Gunthorpe }
714cae5b01aSLogan Gunthorpe CONFIGFS_ATTR(nvmet_passthru_, device_path);
715cae5b01aSLogan Gunthorpe 
716cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_enable_show(struct config_item *item,
717cae5b01aSLogan Gunthorpe 		char *page)
718cae5b01aSLogan Gunthorpe {
719cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
720cae5b01aSLogan Gunthorpe 
721cae5b01aSLogan Gunthorpe 	return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
722cae5b01aSLogan Gunthorpe }
723cae5b01aSLogan Gunthorpe 
724cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_enable_store(struct config_item *item,
725cae5b01aSLogan Gunthorpe 		const char *page, size_t count)
726cae5b01aSLogan Gunthorpe {
727cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
728cae5b01aSLogan Gunthorpe 	bool enable;
729cae5b01aSLogan Gunthorpe 	int ret = 0;
730cae5b01aSLogan Gunthorpe 
731cae5b01aSLogan Gunthorpe 	if (strtobool(page, &enable))
732cae5b01aSLogan Gunthorpe 		return -EINVAL;
733cae5b01aSLogan Gunthorpe 
734cae5b01aSLogan Gunthorpe 	if (enable)
735cae5b01aSLogan Gunthorpe 		ret = nvmet_passthru_ctrl_enable(subsys);
736cae5b01aSLogan Gunthorpe 	else
737cae5b01aSLogan Gunthorpe 		nvmet_passthru_ctrl_disable(subsys);
738cae5b01aSLogan Gunthorpe 
739cae5b01aSLogan Gunthorpe 	return ret ? ret : count;
740cae5b01aSLogan Gunthorpe }
741cae5b01aSLogan Gunthorpe CONFIGFS_ATTR(nvmet_passthru_, enable);
742cae5b01aSLogan Gunthorpe 
743a2f6a2b8SChaitanya Kulkarni static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
744a2f6a2b8SChaitanya Kulkarni 		char *page)
745a2f6a2b8SChaitanya Kulkarni {
746a2f6a2b8SChaitanya Kulkarni 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->admin_timeout);
747a2f6a2b8SChaitanya Kulkarni }
748a2f6a2b8SChaitanya Kulkarni 
749a2f6a2b8SChaitanya Kulkarni static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
750a2f6a2b8SChaitanya Kulkarni 		const char *page, size_t count)
751a2f6a2b8SChaitanya Kulkarni {
752a2f6a2b8SChaitanya Kulkarni 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
753a2f6a2b8SChaitanya Kulkarni 	unsigned int timeout;
754a2f6a2b8SChaitanya Kulkarni 
755a2f6a2b8SChaitanya Kulkarni 	if (kstrtouint(page, 0, &timeout))
756a2f6a2b8SChaitanya Kulkarni 		return -EINVAL;
757a2f6a2b8SChaitanya Kulkarni 	subsys->admin_timeout = timeout;
758a2f6a2b8SChaitanya Kulkarni 	return count;
759a2f6a2b8SChaitanya Kulkarni }
760a2f6a2b8SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
761a2f6a2b8SChaitanya Kulkarni 
76247e9730cSChaitanya Kulkarni static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
76347e9730cSChaitanya Kulkarni 		char *page)
76447e9730cSChaitanya Kulkarni {
76547e9730cSChaitanya Kulkarni 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->io_timeout);
76647e9730cSChaitanya Kulkarni }
76747e9730cSChaitanya Kulkarni 
76847e9730cSChaitanya Kulkarni static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
76947e9730cSChaitanya Kulkarni 		const char *page, size_t count)
77047e9730cSChaitanya Kulkarni {
77147e9730cSChaitanya Kulkarni 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
77247e9730cSChaitanya Kulkarni 	unsigned int timeout;
77347e9730cSChaitanya Kulkarni 
77447e9730cSChaitanya Kulkarni 	if (kstrtouint(page, 0, &timeout))
77547e9730cSChaitanya Kulkarni 		return -EINVAL;
77647e9730cSChaitanya Kulkarni 	subsys->io_timeout = timeout;
77747e9730cSChaitanya Kulkarni 	return count;
77847e9730cSChaitanya Kulkarni }
77947e9730cSChaitanya Kulkarni CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
78047e9730cSChaitanya Kulkarni 
78134ad6151SAlan Adamson static ssize_t nvmet_passthru_clear_ids_show(struct config_item *item,
78234ad6151SAlan Adamson 		char *page)
78334ad6151SAlan Adamson {
78434ad6151SAlan Adamson 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->clear_ids);
78534ad6151SAlan Adamson }
78634ad6151SAlan Adamson 
78734ad6151SAlan Adamson static ssize_t nvmet_passthru_clear_ids_store(struct config_item *item,
78834ad6151SAlan Adamson 		const char *page, size_t count)
78934ad6151SAlan Adamson {
79034ad6151SAlan Adamson 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
79134ad6151SAlan Adamson 	unsigned int clear_ids;
79234ad6151SAlan Adamson 
79334ad6151SAlan Adamson 	if (kstrtouint(page, 0, &clear_ids))
79434ad6151SAlan Adamson 		return -EINVAL;
79534ad6151SAlan Adamson 	subsys->clear_ids = clear_ids;
79634ad6151SAlan Adamson 	return count;
79734ad6151SAlan Adamson }
79834ad6151SAlan Adamson CONFIGFS_ATTR(nvmet_passthru_, clear_ids);
79934ad6151SAlan Adamson 
800cae5b01aSLogan Gunthorpe static struct configfs_attribute *nvmet_passthru_attrs[] = {
801cae5b01aSLogan Gunthorpe 	&nvmet_passthru_attr_device_path,
802cae5b01aSLogan Gunthorpe 	&nvmet_passthru_attr_enable,
803a2f6a2b8SChaitanya Kulkarni 	&nvmet_passthru_attr_admin_timeout,
80447e9730cSChaitanya Kulkarni 	&nvmet_passthru_attr_io_timeout,
80534ad6151SAlan Adamson 	&nvmet_passthru_attr_clear_ids,
806cae5b01aSLogan Gunthorpe 	NULL,
807cae5b01aSLogan Gunthorpe };
808cae5b01aSLogan Gunthorpe 
809cae5b01aSLogan Gunthorpe static const struct config_item_type nvmet_passthru_type = {
810cae5b01aSLogan Gunthorpe 	.ct_attrs		= nvmet_passthru_attrs,
811cae5b01aSLogan Gunthorpe 	.ct_owner		= THIS_MODULE,
812cae5b01aSLogan Gunthorpe };
813cae5b01aSLogan Gunthorpe 
814cae5b01aSLogan Gunthorpe static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
815cae5b01aSLogan Gunthorpe {
816cae5b01aSLogan Gunthorpe 	config_group_init_type_name(&subsys->passthru_group,
817cae5b01aSLogan Gunthorpe 				    "passthru", &nvmet_passthru_type);
818cae5b01aSLogan Gunthorpe 	configfs_add_default_group(&subsys->passthru_group,
819cae5b01aSLogan Gunthorpe 				   &subsys->group);
820cae5b01aSLogan Gunthorpe }
821cae5b01aSLogan Gunthorpe 
822cae5b01aSLogan Gunthorpe #else /* CONFIG_NVME_TARGET_PASSTHRU */
823cae5b01aSLogan Gunthorpe 
824cae5b01aSLogan Gunthorpe static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
825cae5b01aSLogan Gunthorpe {
826cae5b01aSLogan Gunthorpe }
827cae5b01aSLogan Gunthorpe 
828cae5b01aSLogan Gunthorpe #endif /* CONFIG_NVME_TARGET_PASSTHRU */
829cae5b01aSLogan Gunthorpe 
830a07b4970SChristoph Hellwig static int nvmet_port_subsys_allow_link(struct config_item *parent,
831a07b4970SChristoph Hellwig 		struct config_item *target)
832a07b4970SChristoph Hellwig {
833a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
834a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys;
835a07b4970SChristoph Hellwig 	struct nvmet_subsys_link *link, *p;
836a07b4970SChristoph Hellwig 	int ret;
837a07b4970SChristoph Hellwig 
838a07b4970SChristoph Hellwig 	if (target->ci_type != &nvmet_subsys_type) {
839a07b4970SChristoph Hellwig 		pr_err("can only link subsystems into the subsystems dir.!\n");
840a07b4970SChristoph Hellwig 		return -EINVAL;
841a07b4970SChristoph Hellwig 	}
842a07b4970SChristoph Hellwig 	subsys = to_subsys(target);
843a07b4970SChristoph Hellwig 	link = kmalloc(sizeof(*link), GFP_KERNEL);
844a07b4970SChristoph Hellwig 	if (!link)
845a07b4970SChristoph Hellwig 		return -ENOMEM;
846a07b4970SChristoph Hellwig 	link->subsys = subsys;
847a07b4970SChristoph Hellwig 
848a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
849a07b4970SChristoph Hellwig 	ret = -EEXIST;
850a07b4970SChristoph Hellwig 	list_for_each_entry(p, &port->subsystems, entry) {
851a07b4970SChristoph Hellwig 		if (p->subsys == subsys)
852a07b4970SChristoph Hellwig 			goto out_free_link;
853a07b4970SChristoph Hellwig 	}
854a07b4970SChristoph Hellwig 
855a07b4970SChristoph Hellwig 	if (list_empty(&port->subsystems)) {
856a07b4970SChristoph Hellwig 		ret = nvmet_enable_port(port);
857a07b4970SChristoph Hellwig 		if (ret)
858a07b4970SChristoph Hellwig 			goto out_free_link;
859a07b4970SChristoph Hellwig 	}
860a07b4970SChristoph Hellwig 
861a07b4970SChristoph Hellwig 	list_add_tail(&link->entry, &port->subsystems);
862b662a078SJay Sternberg 	nvmet_port_disc_changed(port, subsys);
863b662a078SJay Sternberg 
864a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
865a07b4970SChristoph Hellwig 	return 0;
866a07b4970SChristoph Hellwig 
867a07b4970SChristoph Hellwig out_free_link:
868a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
869a07b4970SChristoph Hellwig 	kfree(link);
870a07b4970SChristoph Hellwig 	return ret;
871a07b4970SChristoph Hellwig }
872a07b4970SChristoph Hellwig 
873e16769d4SAndrzej Pietrasiewicz static void nvmet_port_subsys_drop_link(struct config_item *parent,
874a07b4970SChristoph Hellwig 		struct config_item *target)
875a07b4970SChristoph Hellwig {
876a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
877a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(target);
878a07b4970SChristoph Hellwig 	struct nvmet_subsys_link *p;
879a07b4970SChristoph Hellwig 
880a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
881a07b4970SChristoph Hellwig 	list_for_each_entry(p, &port->subsystems, entry) {
882a07b4970SChristoph Hellwig 		if (p->subsys == subsys)
883a07b4970SChristoph Hellwig 			goto found;
884a07b4970SChristoph Hellwig 	}
885a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
886e16769d4SAndrzej Pietrasiewicz 	return;
887a07b4970SChristoph Hellwig 
888a07b4970SChristoph Hellwig found:
889a07b4970SChristoph Hellwig 	list_del(&p->entry);
8903aed8673SLogan Gunthorpe 	nvmet_port_del_ctrls(port, subsys);
891b662a078SJay Sternberg 	nvmet_port_disc_changed(port, subsys);
892b662a078SJay Sternberg 
893a07b4970SChristoph Hellwig 	if (list_empty(&port->subsystems))
894a07b4970SChristoph Hellwig 		nvmet_disable_port(port);
895a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
896a07b4970SChristoph Hellwig 	kfree(p);
897a07b4970SChristoph Hellwig }
898a07b4970SChristoph Hellwig 
899a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_subsys_item_ops = {
900a07b4970SChristoph Hellwig 	.allow_link		= nvmet_port_subsys_allow_link,
901a07b4970SChristoph Hellwig 	.drop_link		= nvmet_port_subsys_drop_link,
902a07b4970SChristoph Hellwig };
903a07b4970SChristoph Hellwig 
90466603a31SBhumika Goyal static const struct config_item_type nvmet_port_subsys_type = {
905a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_port_subsys_item_ops,
906a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
907a07b4970SChristoph Hellwig };
908a07b4970SChristoph Hellwig 
909a07b4970SChristoph Hellwig static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
910a07b4970SChristoph Hellwig 		struct config_item *target)
911a07b4970SChristoph Hellwig {
912a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
913a07b4970SChristoph Hellwig 	struct nvmet_host *host;
914a07b4970SChristoph Hellwig 	struct nvmet_host_link *link, *p;
915a07b4970SChristoph Hellwig 	int ret;
916a07b4970SChristoph Hellwig 
917a07b4970SChristoph Hellwig 	if (target->ci_type != &nvmet_host_type) {
918a07b4970SChristoph Hellwig 		pr_err("can only link hosts into the allowed_hosts directory!\n");
919a07b4970SChristoph Hellwig 		return -EINVAL;
920a07b4970SChristoph Hellwig 	}
921a07b4970SChristoph Hellwig 
922a07b4970SChristoph Hellwig 	host = to_host(target);
923a07b4970SChristoph Hellwig 	link = kmalloc(sizeof(*link), GFP_KERNEL);
924a07b4970SChristoph Hellwig 	if (!link)
925a07b4970SChristoph Hellwig 		return -ENOMEM;
926a07b4970SChristoph Hellwig 	link->host = host;
927a07b4970SChristoph Hellwig 
928a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
929a07b4970SChristoph Hellwig 	ret = -EINVAL;
930a07b4970SChristoph Hellwig 	if (subsys->allow_any_host) {
931a07b4970SChristoph Hellwig 		pr_err("can't add hosts when allow_any_host is set!\n");
932a07b4970SChristoph Hellwig 		goto out_free_link;
933a07b4970SChristoph Hellwig 	}
934a07b4970SChristoph Hellwig 
935a07b4970SChristoph Hellwig 	ret = -EEXIST;
936a07b4970SChristoph Hellwig 	list_for_each_entry(p, &subsys->hosts, entry) {
937a07b4970SChristoph Hellwig 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
938a07b4970SChristoph Hellwig 			goto out_free_link;
939a07b4970SChristoph Hellwig 	}
940a07b4970SChristoph Hellwig 	list_add_tail(&link->entry, &subsys->hosts);
941b662a078SJay Sternberg 	nvmet_subsys_disc_changed(subsys, host);
942b662a078SJay Sternberg 
943a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
944a07b4970SChristoph Hellwig 	return 0;
945a07b4970SChristoph Hellwig out_free_link:
946a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
947a07b4970SChristoph Hellwig 	kfree(link);
948a07b4970SChristoph Hellwig 	return ret;
949a07b4970SChristoph Hellwig }
950a07b4970SChristoph Hellwig 
951e16769d4SAndrzej Pietrasiewicz static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
952a07b4970SChristoph Hellwig 		struct config_item *target)
953a07b4970SChristoph Hellwig {
954a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
955a07b4970SChristoph Hellwig 	struct nvmet_host *host = to_host(target);
956a07b4970SChristoph Hellwig 	struct nvmet_host_link *p;
957a07b4970SChristoph Hellwig 
958a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
959a07b4970SChristoph Hellwig 	list_for_each_entry(p, &subsys->hosts, entry) {
960a07b4970SChristoph Hellwig 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
961a07b4970SChristoph Hellwig 			goto found;
962a07b4970SChristoph Hellwig 	}
963a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
964e16769d4SAndrzej Pietrasiewicz 	return;
965a07b4970SChristoph Hellwig 
966a07b4970SChristoph Hellwig found:
967a07b4970SChristoph Hellwig 	list_del(&p->entry);
968b662a078SJay Sternberg 	nvmet_subsys_disc_changed(subsys, host);
969b662a078SJay Sternberg 
970a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
971a07b4970SChristoph Hellwig 	kfree(p);
972a07b4970SChristoph Hellwig }
973a07b4970SChristoph Hellwig 
974a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
975a07b4970SChristoph Hellwig 	.allow_link		= nvmet_allowed_hosts_allow_link,
976a07b4970SChristoph Hellwig 	.drop_link		= nvmet_allowed_hosts_drop_link,
977a07b4970SChristoph Hellwig };
978a07b4970SChristoph Hellwig 
97966603a31SBhumika Goyal static const struct config_item_type nvmet_allowed_hosts_type = {
980a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_allowed_hosts_item_ops,
981a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
982a07b4970SChristoph Hellwig };
983a07b4970SChristoph Hellwig 
984a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
985a07b4970SChristoph Hellwig 		char *page)
986a07b4970SChristoph Hellwig {
987a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n",
988a07b4970SChristoph Hellwig 		to_subsys(item)->allow_any_host);
989a07b4970SChristoph Hellwig }
990a07b4970SChristoph Hellwig 
991a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
992a07b4970SChristoph Hellwig 		const char *page, size_t count)
993a07b4970SChristoph Hellwig {
994a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(item);
995a07b4970SChristoph Hellwig 	bool allow_any_host;
996a07b4970SChristoph Hellwig 	int ret = 0;
997a07b4970SChristoph Hellwig 
998a07b4970SChristoph Hellwig 	if (strtobool(page, &allow_any_host))
999a07b4970SChristoph Hellwig 		return -EINVAL;
1000a07b4970SChristoph Hellwig 
1001a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
1002a07b4970SChristoph Hellwig 	if (allow_any_host && !list_empty(&subsys->hosts)) {
1003a07b4970SChristoph Hellwig 		pr_err("Can't set allow_any_host when explicit hosts are set!\n");
1004a07b4970SChristoph Hellwig 		ret = -EINVAL;
1005a07b4970SChristoph Hellwig 		goto out_unlock;
1006a07b4970SChristoph Hellwig 	}
1007a07b4970SChristoph Hellwig 
1008b662a078SJay Sternberg 	if (subsys->allow_any_host != allow_any_host) {
1009a07b4970SChristoph Hellwig 		subsys->allow_any_host = allow_any_host;
1010b662a078SJay Sternberg 		nvmet_subsys_disc_changed(subsys, NULL);
1011b662a078SJay Sternberg 	}
1012b662a078SJay Sternberg 
1013a07b4970SChristoph Hellwig out_unlock:
1014a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
1015a07b4970SChristoph Hellwig 	return ret ? ret : count;
1016a07b4970SChristoph Hellwig }
1017a07b4970SChristoph Hellwig 
1018a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
1019a07b4970SChristoph Hellwig 
102041528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
1021c61d788bSJohannes Thumshirn 					      char *page)
1022c61d788bSJohannes Thumshirn {
1023c61d788bSJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
1024c61d788bSJohannes Thumshirn 
1025c61d788bSJohannes Thumshirn 	if (NVME_TERTIARY(subsys->ver))
1026a0f0dbaaSChaitanya Kulkarni 		return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
1027a0f0dbaaSChaitanya Kulkarni 				NVME_MAJOR(subsys->ver),
1028a0f0dbaaSChaitanya Kulkarni 				NVME_MINOR(subsys->ver),
1029a0f0dbaaSChaitanya Kulkarni 				NVME_TERTIARY(subsys->ver));
1030527123c7SChaitanya Kulkarni 
1031a0f0dbaaSChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
1032a0f0dbaaSChaitanya Kulkarni 			NVME_MAJOR(subsys->ver),
1033a0f0dbaaSChaitanya Kulkarni 			NVME_MINOR(subsys->ver));
1034c61d788bSJohannes Thumshirn }
1035c61d788bSJohannes Thumshirn 
103687fd4cc1SNoam Gottlieb static ssize_t
103787fd4cc1SNoam Gottlieb nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,
1038c61d788bSJohannes Thumshirn 		const char *page, size_t count)
1039c61d788bSJohannes Thumshirn {
1040c61d788bSJohannes Thumshirn 	int major, minor, tertiary = 0;
1041c61d788bSJohannes Thumshirn 	int ret;
1042c61d788bSJohannes Thumshirn 
104387fd4cc1SNoam Gottlieb 	if (subsys->subsys_discovered) {
104487fd4cc1SNoam Gottlieb 		if (NVME_TERTIARY(subsys->ver))
104587fd4cc1SNoam Gottlieb 			pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
104687fd4cc1SNoam Gottlieb 			       NVME_MAJOR(subsys->ver),
104787fd4cc1SNoam Gottlieb 			       NVME_MINOR(subsys->ver),
104887fd4cc1SNoam Gottlieb 			       NVME_TERTIARY(subsys->ver));
104987fd4cc1SNoam Gottlieb 		else
105087fd4cc1SNoam Gottlieb 			pr_err("Can't set version number. %llu.%llu is already assigned\n",
105187fd4cc1SNoam Gottlieb 			       NVME_MAJOR(subsys->ver),
105287fd4cc1SNoam Gottlieb 			       NVME_MINOR(subsys->ver));
105387fd4cc1SNoam Gottlieb 		return -EINVAL;
105487fd4cc1SNoam Gottlieb 	}
105587fd4cc1SNoam Gottlieb 
1056ba76af67SLogan Gunthorpe 	/* passthru subsystems use the underlying controller's version */
1057ab7a2737SChristoph Hellwig 	if (nvmet_is_passthru_subsys(subsys))
1058ba76af67SLogan Gunthorpe 		return -EINVAL;
1059ba76af67SLogan Gunthorpe 
1060c61d788bSJohannes Thumshirn 	ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
1061c61d788bSJohannes Thumshirn 	if (ret != 2 && ret != 3)
1062c61d788bSJohannes Thumshirn 		return -EINVAL;
1063c61d788bSJohannes Thumshirn 
1064c61d788bSJohannes Thumshirn 	subsys->ver = NVME_VS(major, minor, tertiary);
1065c61d788bSJohannes Thumshirn 
1066c61d788bSJohannes Thumshirn 	return count;
1067c61d788bSJohannes Thumshirn }
106887fd4cc1SNoam Gottlieb 
106987fd4cc1SNoam Gottlieb static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
107087fd4cc1SNoam Gottlieb 					       const char *page, size_t count)
107187fd4cc1SNoam Gottlieb {
107287fd4cc1SNoam Gottlieb 	struct nvmet_subsys *subsys = to_subsys(item);
107387fd4cc1SNoam Gottlieb 	ssize_t ret;
107487fd4cc1SNoam Gottlieb 
107587fd4cc1SNoam Gottlieb 	down_write(&nvmet_config_sem);
107687fd4cc1SNoam Gottlieb 	mutex_lock(&subsys->lock);
107787fd4cc1SNoam Gottlieb 	ret = nvmet_subsys_attr_version_store_locked(subsys, page, count);
107887fd4cc1SNoam Gottlieb 	mutex_unlock(&subsys->lock);
107987fd4cc1SNoam Gottlieb 	up_write(&nvmet_config_sem);
108087fd4cc1SNoam Gottlieb 
108187fd4cc1SNoam Gottlieb 	return ret;
108287fd4cc1SNoam Gottlieb }
108341528f80SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_version);
1084c61d788bSJohannes Thumshirn 
1085e13b0615SNoam Gottlieb /* See Section 1.5 of NVMe 1.4 */
1086e13b0615SNoam Gottlieb static bool nvmet_is_ascii(const char c)
1087e13b0615SNoam Gottlieb {
1088e13b0615SNoam Gottlieb 	return c >= 0x20 && c <= 0x7e;
1089e13b0615SNoam Gottlieb }
1090e13b0615SNoam Gottlieb 
1091fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
1092fcbc5459SJohannes Thumshirn 					     char *page)
1093fcbc5459SJohannes Thumshirn {
1094fcbc5459SJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
1095fcbc5459SJohannes Thumshirn 
10960bd46e22SDan Carpenter 	return snprintf(page, PAGE_SIZE, "%.*s\n",
1097f0406481SHannes Reinecke 			NVMET_SN_MAX_SIZE, subsys->serial);
1098fcbc5459SJohannes Thumshirn }
1099fcbc5459SJohannes Thumshirn 
11007ae023c5SNoam Gottlieb static ssize_t
11017ae023c5SNoam Gottlieb nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
1102fcbc5459SJohannes Thumshirn 		const char *page, size_t count)
1103fcbc5459SJohannes Thumshirn {
1104e13b0615SNoam Gottlieb 	int pos, len = strcspn(page, "\n");
1105d3a9b0caSChaitanya Kulkarni 
11067ae023c5SNoam Gottlieb 	if (subsys->subsys_discovered) {
11077ae023c5SNoam Gottlieb 		pr_err("Can't set serial number. %s is already assigned\n",
11087ae023c5SNoam Gottlieb 		       subsys->serial);
11097ae023c5SNoam Gottlieb 		return -EINVAL;
11107ae023c5SNoam Gottlieb 	}
11117ae023c5SNoam Gottlieb 
1112e13b0615SNoam Gottlieb 	if (!len || len > NVMET_SN_MAX_SIZE) {
1113e13b0615SNoam Gottlieb 		pr_err("Serial Number can not be empty or exceed %d Bytes\n",
1114e13b0615SNoam Gottlieb 		       NVMET_SN_MAX_SIZE);
1115d3a9b0caSChaitanya Kulkarni 		return -EINVAL;
1116e13b0615SNoam Gottlieb 	}
1117e13b0615SNoam Gottlieb 
1118e13b0615SNoam Gottlieb 	for (pos = 0; pos < len; pos++) {
1119e13b0615SNoam Gottlieb 		if (!nvmet_is_ascii(page[pos])) {
1120e13b0615SNoam Gottlieb 			pr_err("Serial Number must contain only ASCII strings\n");
1121e13b0615SNoam Gottlieb 			return -EINVAL;
1122e13b0615SNoam Gottlieb 		}
1123e13b0615SNoam Gottlieb 	}
1124fcbc5459SJohannes Thumshirn 
11257ae023c5SNoam Gottlieb 	memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
11267ae023c5SNoam Gottlieb 
11277ae023c5SNoam Gottlieb 	return count;
11287ae023c5SNoam Gottlieb }
11297ae023c5SNoam Gottlieb 
11307ae023c5SNoam Gottlieb static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
11317ae023c5SNoam Gottlieb 					      const char *page, size_t count)
11327ae023c5SNoam Gottlieb {
11337ae023c5SNoam Gottlieb 	struct nvmet_subsys *subsys = to_subsys(item);
11347ae023c5SNoam Gottlieb 	ssize_t ret;
11357ae023c5SNoam Gottlieb 
1136fcbc5459SJohannes Thumshirn 	down_write(&nvmet_config_sem);
1137e13b0615SNoam Gottlieb 	mutex_lock(&subsys->lock);
11387ae023c5SNoam Gottlieb 	ret = nvmet_subsys_attr_serial_store_locked(subsys, page, count);
1139e13b0615SNoam Gottlieb 	mutex_unlock(&subsys->lock);
1140fcbc5459SJohannes Thumshirn 	up_write(&nvmet_config_sem);
1141fcbc5459SJohannes Thumshirn 
11427ae023c5SNoam Gottlieb 	return ret;
1143fcbc5459SJohannes Thumshirn }
1144fcbc5459SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
1145fcbc5459SJohannes Thumshirn 
114694a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
114794a39d61SChaitanya Kulkarni 						 char *page)
114894a39d61SChaitanya Kulkarni {
114994a39d61SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
115094a39d61SChaitanya Kulkarni }
115194a39d61SChaitanya Kulkarni 
115294a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
115394a39d61SChaitanya Kulkarni 						  const char *page, size_t cnt)
115494a39d61SChaitanya Kulkarni {
115594a39d61SChaitanya Kulkarni 	u16 cntlid_min;
115694a39d61SChaitanya Kulkarni 
115794a39d61SChaitanya Kulkarni 	if (sscanf(page, "%hu\n", &cntlid_min) != 1)
115894a39d61SChaitanya Kulkarni 		return -EINVAL;
115994a39d61SChaitanya Kulkarni 
116094a39d61SChaitanya Kulkarni 	if (cntlid_min == 0)
116194a39d61SChaitanya Kulkarni 		return -EINVAL;
116294a39d61SChaitanya Kulkarni 
116394a39d61SChaitanya Kulkarni 	down_write(&nvmet_config_sem);
116494a39d61SChaitanya Kulkarni 	if (cntlid_min >= to_subsys(item)->cntlid_max)
116594a39d61SChaitanya Kulkarni 		goto out_unlock;
116694a39d61SChaitanya Kulkarni 	to_subsys(item)->cntlid_min = cntlid_min;
116794a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
116894a39d61SChaitanya Kulkarni 	return cnt;
116994a39d61SChaitanya Kulkarni 
117094a39d61SChaitanya Kulkarni out_unlock:
117194a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
117294a39d61SChaitanya Kulkarni 	return -EINVAL;
117394a39d61SChaitanya Kulkarni }
117494a39d61SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
117594a39d61SChaitanya Kulkarni 
117694a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
117794a39d61SChaitanya Kulkarni 						 char *page)
117894a39d61SChaitanya Kulkarni {
117994a39d61SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
118094a39d61SChaitanya Kulkarni }
118194a39d61SChaitanya Kulkarni 
118294a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
118394a39d61SChaitanya Kulkarni 						  const char *page, size_t cnt)
118494a39d61SChaitanya Kulkarni {
118594a39d61SChaitanya Kulkarni 	u16 cntlid_max;
118694a39d61SChaitanya Kulkarni 
118794a39d61SChaitanya Kulkarni 	if (sscanf(page, "%hu\n", &cntlid_max) != 1)
118894a39d61SChaitanya Kulkarni 		return -EINVAL;
118994a39d61SChaitanya Kulkarni 
119094a39d61SChaitanya Kulkarni 	if (cntlid_max == 0)
119194a39d61SChaitanya Kulkarni 		return -EINVAL;
119294a39d61SChaitanya Kulkarni 
119394a39d61SChaitanya Kulkarni 	down_write(&nvmet_config_sem);
119494a39d61SChaitanya Kulkarni 	if (cntlid_max <= to_subsys(item)->cntlid_min)
119594a39d61SChaitanya Kulkarni 		goto out_unlock;
119694a39d61SChaitanya Kulkarni 	to_subsys(item)->cntlid_max = cntlid_max;
119794a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
119894a39d61SChaitanya Kulkarni 	return cnt;
119994a39d61SChaitanya Kulkarni 
120094a39d61SChaitanya Kulkarni out_unlock:
120194a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
120294a39d61SChaitanya Kulkarni 	return -EINVAL;
120394a39d61SChaitanya Kulkarni }
120494a39d61SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
120594a39d61SChaitanya Kulkarni 
1206013b7ebeSMark Ruijter static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
1207013b7ebeSMark Ruijter 					    char *page)
1208013b7ebeSMark Ruijter {
1209013b7ebeSMark Ruijter 	struct nvmet_subsys *subsys = to_subsys(item);
1210013b7ebeSMark Ruijter 
12110d148efdSNoam Gottlieb 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->model_number);
1212013b7ebeSMark Ruijter }
1213013b7ebeSMark Ruijter 
1214d9f273b7SMax Gurtovoy static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
1215013b7ebeSMark Ruijter 		const char *page, size_t count)
1216013b7ebeSMark Ruijter {
1217013b7ebeSMark Ruijter 	int pos = 0, len;
1218013b7ebeSMark Ruijter 
12190d148efdSNoam Gottlieb 	if (subsys->subsys_discovered) {
1220d9f273b7SMax Gurtovoy 		pr_err("Can't set model number. %s is already assigned\n",
1221d9f273b7SMax Gurtovoy 		       subsys->model_number);
1222d9f273b7SMax Gurtovoy 		return -EINVAL;
1223d9f273b7SMax Gurtovoy 	}
1224d9f273b7SMax Gurtovoy 
1225013b7ebeSMark Ruijter 	len = strcspn(page, "\n");
1226013b7ebeSMark Ruijter 	if (!len)
1227013b7ebeSMark Ruijter 		return -EINVAL;
1228013b7ebeSMark Ruijter 
122948b4c010SNoam Gottlieb 	if (len > NVMET_MN_MAX_SIZE) {
1230ccc1003bSColin Ian King 		pr_err("Model number size can not exceed %d Bytes\n",
123148b4c010SNoam Gottlieb 		       NVMET_MN_MAX_SIZE);
123248b4c010SNoam Gottlieb 		return -EINVAL;
123348b4c010SNoam Gottlieb 	}
123448b4c010SNoam Gottlieb 
1235013b7ebeSMark Ruijter 	for (pos = 0; pos < len; pos++) {
1236013b7ebeSMark Ruijter 		if (!nvmet_is_ascii(page[pos]))
1237013b7ebeSMark Ruijter 			return -EINVAL;
1238013b7ebeSMark Ruijter 	}
1239013b7ebeSMark Ruijter 
1240d9f273b7SMax Gurtovoy 	subsys->model_number = kmemdup_nul(page, len, GFP_KERNEL);
1241d9f273b7SMax Gurtovoy 	if (!subsys->model_number)
1242013b7ebeSMark Ruijter 		return -ENOMEM;
1243d9f273b7SMax Gurtovoy 	return count;
1244013b7ebeSMark Ruijter }
1245d9f273b7SMax Gurtovoy 
1246d9f273b7SMax Gurtovoy static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
1247d9f273b7SMax Gurtovoy 					     const char *page, size_t count)
1248d9f273b7SMax Gurtovoy {
1249d9f273b7SMax Gurtovoy 	struct nvmet_subsys *subsys = to_subsys(item);
1250d9f273b7SMax Gurtovoy 	ssize_t ret;
1251013b7ebeSMark Ruijter 
1252013b7ebeSMark Ruijter 	down_write(&nvmet_config_sem);
1253013b7ebeSMark Ruijter 	mutex_lock(&subsys->lock);
1254d9f273b7SMax Gurtovoy 	ret = nvmet_subsys_attr_model_store_locked(subsys, page, count);
1255013b7ebeSMark Ruijter 	mutex_unlock(&subsys->lock);
1256013b7ebeSMark Ruijter 	up_write(&nvmet_config_sem);
1257013b7ebeSMark Ruijter 
1258d9f273b7SMax Gurtovoy 	return ret;
1259013b7ebeSMark Ruijter }
1260013b7ebeSMark Ruijter CONFIGFS_ATTR(nvmet_subsys_, attr_model);
1261013b7ebeSMark Ruijter 
1262ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1263ea52ac1cSIsrael Rukshin static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
1264ea52ac1cSIsrael Rukshin 						char *page)
1265ea52ac1cSIsrael Rukshin {
1266ea52ac1cSIsrael Rukshin 	return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
1267ea52ac1cSIsrael Rukshin }
1268ea52ac1cSIsrael Rukshin 
1269ea52ac1cSIsrael Rukshin static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
1270ea52ac1cSIsrael Rukshin 						 const char *page, size_t count)
1271ea52ac1cSIsrael Rukshin {
1272ea52ac1cSIsrael Rukshin 	struct nvmet_subsys *subsys = to_subsys(item);
1273ea52ac1cSIsrael Rukshin 	bool pi_enable;
1274ea52ac1cSIsrael Rukshin 
1275ea52ac1cSIsrael Rukshin 	if (strtobool(page, &pi_enable))
1276ea52ac1cSIsrael Rukshin 		return -EINVAL;
1277ea52ac1cSIsrael Rukshin 
1278ea52ac1cSIsrael Rukshin 	subsys->pi_support = pi_enable;
1279ea52ac1cSIsrael Rukshin 	return count;
1280ea52ac1cSIsrael Rukshin }
1281ea52ac1cSIsrael Rukshin CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
1282ea52ac1cSIsrael Rukshin #endif
1283ea52ac1cSIsrael Rukshin 
1284a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_subsys_attrs[] = {
1285a07b4970SChristoph Hellwig 	&nvmet_subsys_attr_attr_allow_any_host,
128641528f80SJohannes Thumshirn 	&nvmet_subsys_attr_attr_version,
1287fcbc5459SJohannes Thumshirn 	&nvmet_subsys_attr_attr_serial,
128894a39d61SChaitanya Kulkarni 	&nvmet_subsys_attr_attr_cntlid_min,
128994a39d61SChaitanya Kulkarni 	&nvmet_subsys_attr_attr_cntlid_max,
1290013b7ebeSMark Ruijter 	&nvmet_subsys_attr_attr_model,
1291ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1292ea52ac1cSIsrael Rukshin 	&nvmet_subsys_attr_attr_pi_enable,
1293ea52ac1cSIsrael Rukshin #endif
1294a07b4970SChristoph Hellwig 	NULL,
1295a07b4970SChristoph Hellwig };
1296a07b4970SChristoph Hellwig 
1297a07b4970SChristoph Hellwig /*
1298a07b4970SChristoph Hellwig  * Subsystem structures & folder operation functions below
1299a07b4970SChristoph Hellwig  */
1300a07b4970SChristoph Hellwig static void nvmet_subsys_release(struct config_item *item)
1301a07b4970SChristoph Hellwig {
1302a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(item);
1303a07b4970SChristoph Hellwig 
1304344770b0SSagi Grimberg 	nvmet_subsys_del_ctrls(subsys);
1305a07b4970SChristoph Hellwig 	nvmet_subsys_put(subsys);
1306a07b4970SChristoph Hellwig }
1307a07b4970SChristoph Hellwig 
1308a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_subsys_item_ops = {
1309a07b4970SChristoph Hellwig 	.release		= nvmet_subsys_release,
1310a07b4970SChristoph Hellwig };
1311a07b4970SChristoph Hellwig 
131266603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type = {
1313a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_subsys_item_ops,
1314a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_subsys_attrs,
1315a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1316a07b4970SChristoph Hellwig };
1317a07b4970SChristoph Hellwig 
1318a07b4970SChristoph Hellwig static struct config_group *nvmet_subsys_make(struct config_group *group,
1319a07b4970SChristoph Hellwig 		const char *name)
1320a07b4970SChristoph Hellwig {
1321a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys;
1322a07b4970SChristoph Hellwig 
1323a07b4970SChristoph Hellwig 	if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1324a07b4970SChristoph Hellwig 		pr_err("can't create discovery subsystem through configfs\n");
1325a07b4970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1326a07b4970SChristoph Hellwig 	}
1327a07b4970SChristoph Hellwig 
1328a07b4970SChristoph Hellwig 	subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
13296b7e631bSMinwoo Im 	if (IS_ERR(subsys))
13306b7e631bSMinwoo Im 		return ERR_CAST(subsys);
1331a07b4970SChristoph Hellwig 
1332a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1333a07b4970SChristoph Hellwig 
1334a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->namespaces_group,
1335a07b4970SChristoph Hellwig 			"namespaces", &nvmet_namespaces_type);
1336a07b4970SChristoph Hellwig 	configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1337a07b4970SChristoph Hellwig 
1338a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->allowed_hosts_group,
1339a07b4970SChristoph Hellwig 			"allowed_hosts", &nvmet_allowed_hosts_type);
1340a07b4970SChristoph Hellwig 	configfs_add_default_group(&subsys->allowed_hosts_group,
1341a07b4970SChristoph Hellwig 			&subsys->group);
1342a07b4970SChristoph Hellwig 
1343cae5b01aSLogan Gunthorpe 	nvmet_add_passthru_group(subsys);
1344cae5b01aSLogan Gunthorpe 
1345a07b4970SChristoph Hellwig 	return &subsys->group;
1346a07b4970SChristoph Hellwig }
1347a07b4970SChristoph Hellwig 
1348a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_subsystems_group_ops = {
1349a07b4970SChristoph Hellwig 	.make_group		= nvmet_subsys_make,
1350a07b4970SChristoph Hellwig };
1351a07b4970SChristoph Hellwig 
135266603a31SBhumika Goyal static const struct config_item_type nvmet_subsystems_type = {
1353a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_subsystems_group_ops,
1354a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1355a07b4970SChristoph Hellwig };
1356a07b4970SChristoph Hellwig 
1357a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_show(struct config_item *item,
1358a07b4970SChristoph Hellwig 		char *page)
1359a07b4970SChristoph Hellwig {
1360a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1361a07b4970SChristoph Hellwig }
1362a07b4970SChristoph Hellwig 
1363a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_store(struct config_item *item,
1364a07b4970SChristoph Hellwig 		const char *page, size_t count)
1365a07b4970SChristoph Hellwig {
1366a07b4970SChristoph Hellwig 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1367a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1368a07b4970SChristoph Hellwig 	bool enable;
1369a07b4970SChristoph Hellwig 
1370a07b4970SChristoph Hellwig 	if (strtobool(page, &enable))
1371a07b4970SChristoph Hellwig 		goto inval;
1372a07b4970SChristoph Hellwig 
1373a07b4970SChristoph Hellwig 	if (enable)
1374a07b4970SChristoph Hellwig 		nvmet_referral_enable(parent, port);
1375a07b4970SChristoph Hellwig 	else
1376b662a078SJay Sternberg 		nvmet_referral_disable(parent, port);
1377a07b4970SChristoph Hellwig 
1378a07b4970SChristoph Hellwig 	return count;
1379a07b4970SChristoph Hellwig inval:
1380a07b4970SChristoph Hellwig 	pr_err("Invalid value '%s' for enable\n", page);
1381a07b4970SChristoph Hellwig 	return -EINVAL;
1382a07b4970SChristoph Hellwig }
1383a07b4970SChristoph Hellwig 
1384a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_referral_, enable);
1385a07b4970SChristoph Hellwig 
1386a07b4970SChristoph Hellwig /*
1387a07b4970SChristoph Hellwig  * Discovery Service subsystem definitions
1388a07b4970SChristoph Hellwig  */
1389a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_referral_attrs[] = {
1390a07b4970SChristoph Hellwig 	&nvmet_attr_addr_adrfam,
1391a07b4970SChristoph Hellwig 	&nvmet_attr_addr_portid,
1392a07b4970SChristoph Hellwig 	&nvmet_attr_addr_treq,
1393a07b4970SChristoph Hellwig 	&nvmet_attr_addr_traddr,
1394a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trsvcid,
1395a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trtype,
1396a07b4970SChristoph Hellwig 	&nvmet_referral_attr_enable,
1397a07b4970SChristoph Hellwig 	NULL,
1398a07b4970SChristoph Hellwig };
1399a07b4970SChristoph Hellwig 
1400f0e656e4SSagi Grimberg static void nvmet_referral_notify(struct config_group *group,
1401f0e656e4SSagi Grimberg 		struct config_item *item)
1402a07b4970SChristoph Hellwig {
1403b662a078SJay Sternberg 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1404a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1405a07b4970SChristoph Hellwig 
1406b662a078SJay Sternberg 	nvmet_referral_disable(parent, port);
1407f0e656e4SSagi Grimberg }
1408f0e656e4SSagi Grimberg 
1409f0e656e4SSagi Grimberg static void nvmet_referral_release(struct config_item *item)
1410f0e656e4SSagi Grimberg {
1411f0e656e4SSagi Grimberg 	struct nvmet_port *port = to_nvmet_port(item);
1412f0e656e4SSagi Grimberg 
1413a07b4970SChristoph Hellwig 	kfree(port);
1414a07b4970SChristoph Hellwig }
1415a07b4970SChristoph Hellwig 
1416a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_referral_item_ops = {
1417a07b4970SChristoph Hellwig 	.release	= nvmet_referral_release,
1418a07b4970SChristoph Hellwig };
1419a07b4970SChristoph Hellwig 
142066603a31SBhumika Goyal static const struct config_item_type nvmet_referral_type = {
1421a07b4970SChristoph Hellwig 	.ct_owner	= THIS_MODULE,
1422a07b4970SChristoph Hellwig 	.ct_attrs	= nvmet_referral_attrs,
1423a07b4970SChristoph Hellwig 	.ct_item_ops	= &nvmet_referral_item_ops,
1424a07b4970SChristoph Hellwig };
1425a07b4970SChristoph Hellwig 
1426a07b4970SChristoph Hellwig static struct config_group *nvmet_referral_make(
1427a07b4970SChristoph Hellwig 		struct config_group *group, const char *name)
1428a07b4970SChristoph Hellwig {
1429a07b4970SChristoph Hellwig 	struct nvmet_port *port;
1430a07b4970SChristoph Hellwig 
1431a07b4970SChristoph Hellwig 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1432a07b4970SChristoph Hellwig 	if (!port)
1433f98d9ca1SDan Carpenter 		return ERR_PTR(-ENOMEM);
1434a07b4970SChristoph Hellwig 
1435a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->entry);
1436a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1437a07b4970SChristoph Hellwig 
1438a07b4970SChristoph Hellwig 	return &port->group;
1439a07b4970SChristoph Hellwig }
1440a07b4970SChristoph Hellwig 
1441a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_referral_group_ops = {
1442a07b4970SChristoph Hellwig 	.make_group		= nvmet_referral_make,
1443f0e656e4SSagi Grimberg 	.disconnect_notify	= nvmet_referral_notify,
1444a07b4970SChristoph Hellwig };
1445a07b4970SChristoph Hellwig 
144666603a31SBhumika Goyal static const struct config_item_type nvmet_referrals_type = {
1447a07b4970SChristoph Hellwig 	.ct_owner	= THIS_MODULE,
1448a07b4970SChristoph Hellwig 	.ct_group_ops	= &nvmet_referral_group_ops,
1449a07b4970SChristoph Hellwig };
1450a07b4970SChristoph Hellwig 
145174255969SChristoph Hellwig static struct nvmet_type_name_map nvmet_ana_state[] = {
145262ac0d32SChristoph Hellwig 	{ NVME_ANA_OPTIMIZED,		"optimized" },
145362ac0d32SChristoph Hellwig 	{ NVME_ANA_NONOPTIMIZED,	"non-optimized" },
145462ac0d32SChristoph Hellwig 	{ NVME_ANA_INACCESSIBLE,	"inaccessible" },
145562ac0d32SChristoph Hellwig 	{ NVME_ANA_PERSISTENT_LOSS,	"persistent-loss" },
145662ac0d32SChristoph Hellwig 	{ NVME_ANA_CHANGE,		"change" },
145762ac0d32SChristoph Hellwig };
145862ac0d32SChristoph Hellwig 
145962ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
146062ac0d32SChristoph Hellwig 		char *page)
146162ac0d32SChristoph Hellwig {
146262ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
146362ac0d32SChristoph Hellwig 	enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
146462ac0d32SChristoph Hellwig 	int i;
146562ac0d32SChristoph Hellwig 
146684b8d0d7SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
146784b8d0d7SChaitanya Kulkarni 		if (state == nvmet_ana_state[i].type)
146884b8d0d7SChaitanya Kulkarni 			return sprintf(page, "%s\n", nvmet_ana_state[i].name);
146962ac0d32SChristoph Hellwig 	}
147062ac0d32SChristoph Hellwig 
147162ac0d32SChristoph Hellwig 	return sprintf(page, "\n");
147262ac0d32SChristoph Hellwig }
147362ac0d32SChristoph Hellwig 
147462ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
147562ac0d32SChristoph Hellwig 		const char *page, size_t count)
147662ac0d32SChristoph Hellwig {
147762ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
147884b8d0d7SChaitanya Kulkarni 	enum nvme_ana_state *ana_state = grp->port->ana_state;
147962ac0d32SChristoph Hellwig 	int i;
148062ac0d32SChristoph Hellwig 
148184b8d0d7SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
148284b8d0d7SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_ana_state[i].name))
148362ac0d32SChristoph Hellwig 			goto found;
148462ac0d32SChristoph Hellwig 	}
148562ac0d32SChristoph Hellwig 
148662ac0d32SChristoph Hellwig 	pr_err("Invalid value '%s' for ana_state\n", page);
148762ac0d32SChristoph Hellwig 	return -EINVAL;
148862ac0d32SChristoph Hellwig 
148962ac0d32SChristoph Hellwig found:
149062ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
149184b8d0d7SChaitanya Kulkarni 	ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
149262ac0d32SChristoph Hellwig 	nvmet_ana_chgcnt++;
149362ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
149462ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
149562ac0d32SChristoph Hellwig 	return count;
149662ac0d32SChristoph Hellwig }
149762ac0d32SChristoph Hellwig 
149862ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
149962ac0d32SChristoph Hellwig 
150062ac0d32SChristoph Hellwig static struct configfs_attribute *nvmet_ana_group_attrs[] = {
150162ac0d32SChristoph Hellwig 	&nvmet_ana_group_attr_ana_state,
150262ac0d32SChristoph Hellwig 	NULL,
150362ac0d32SChristoph Hellwig };
150462ac0d32SChristoph Hellwig 
150562ac0d32SChristoph Hellwig static void nvmet_ana_group_release(struct config_item *item)
150662ac0d32SChristoph Hellwig {
150762ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
150862ac0d32SChristoph Hellwig 
150962ac0d32SChristoph Hellwig 	if (grp == &grp->port->ana_default_group)
151062ac0d32SChristoph Hellwig 		return;
151162ac0d32SChristoph Hellwig 
151262ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
151362ac0d32SChristoph Hellwig 	grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
151462ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[grp->grpid]--;
151562ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
151662ac0d32SChristoph Hellwig 
151762ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
151862ac0d32SChristoph Hellwig 	kfree(grp);
151962ac0d32SChristoph Hellwig }
152062ac0d32SChristoph Hellwig 
152162ac0d32SChristoph Hellwig static struct configfs_item_operations nvmet_ana_group_item_ops = {
152262ac0d32SChristoph Hellwig 	.release		= nvmet_ana_group_release,
152362ac0d32SChristoph Hellwig };
152462ac0d32SChristoph Hellwig 
152562ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_group_type = {
152662ac0d32SChristoph Hellwig 	.ct_item_ops		= &nvmet_ana_group_item_ops,
152762ac0d32SChristoph Hellwig 	.ct_attrs		= nvmet_ana_group_attrs,
152862ac0d32SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
152962ac0d32SChristoph Hellwig };
153062ac0d32SChristoph Hellwig 
153162ac0d32SChristoph Hellwig static struct config_group *nvmet_ana_groups_make_group(
153262ac0d32SChristoph Hellwig 		struct config_group *group, const char *name)
153362ac0d32SChristoph Hellwig {
153462ac0d32SChristoph Hellwig 	struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
153562ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp;
153662ac0d32SChristoph Hellwig 	u32 grpid;
153762ac0d32SChristoph Hellwig 	int ret;
153862ac0d32SChristoph Hellwig 
153962ac0d32SChristoph Hellwig 	ret = kstrtou32(name, 0, &grpid);
154062ac0d32SChristoph Hellwig 	if (ret)
154162ac0d32SChristoph Hellwig 		goto out;
154262ac0d32SChristoph Hellwig 
154362ac0d32SChristoph Hellwig 	ret = -EINVAL;
154462ac0d32SChristoph Hellwig 	if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
154562ac0d32SChristoph Hellwig 		goto out;
154662ac0d32SChristoph Hellwig 
154762ac0d32SChristoph Hellwig 	ret = -ENOMEM;
154862ac0d32SChristoph Hellwig 	grp = kzalloc(sizeof(*grp), GFP_KERNEL);
154962ac0d32SChristoph Hellwig 	if (!grp)
155062ac0d32SChristoph Hellwig 		goto out;
155162ac0d32SChristoph Hellwig 	grp->port = port;
155262ac0d32SChristoph Hellwig 	grp->grpid = grpid;
155362ac0d32SChristoph Hellwig 
155462ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
155562ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[grpid]++;
155662ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
155762ac0d32SChristoph Hellwig 
155862ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
155962ac0d32SChristoph Hellwig 
156062ac0d32SChristoph Hellwig 	config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
156162ac0d32SChristoph Hellwig 	return &grp->group;
156262ac0d32SChristoph Hellwig out:
156362ac0d32SChristoph Hellwig 	return ERR_PTR(ret);
156462ac0d32SChristoph Hellwig }
156562ac0d32SChristoph Hellwig 
156662ac0d32SChristoph Hellwig static struct configfs_group_operations nvmet_ana_groups_group_ops = {
156762ac0d32SChristoph Hellwig 	.make_group		= nvmet_ana_groups_make_group,
156862ac0d32SChristoph Hellwig };
156962ac0d32SChristoph Hellwig 
157062ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_groups_type = {
157162ac0d32SChristoph Hellwig 	.ct_group_ops		= &nvmet_ana_groups_group_ops,
157262ac0d32SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
157362ac0d32SChristoph Hellwig };
157462ac0d32SChristoph Hellwig 
1575a07b4970SChristoph Hellwig /*
1576a07b4970SChristoph Hellwig  * Ports definitions.
1577a07b4970SChristoph Hellwig  */
1578a07b4970SChristoph Hellwig static void nvmet_port_release(struct config_item *item)
1579a07b4970SChristoph Hellwig {
1580a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1581a07b4970SChristoph Hellwig 
1582e3e19dccSIsrael Rukshin 	/* Let inflight controllers teardown complete */
15838832cf92SSagi Grimberg 	flush_workqueue(nvmet_wq);
1584b662a078SJay Sternberg 	list_del(&port->global_entry);
1585b662a078SJay Sternberg 
158672efd25dSChristoph Hellwig 	kfree(port->ana_state);
1587a07b4970SChristoph Hellwig 	kfree(port);
1588a07b4970SChristoph Hellwig }
1589a07b4970SChristoph Hellwig 
1590a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_port_attrs[] = {
1591a07b4970SChristoph Hellwig 	&nvmet_attr_addr_adrfam,
1592a07b4970SChristoph Hellwig 	&nvmet_attr_addr_treq,
1593a07b4970SChristoph Hellwig 	&nvmet_attr_addr_traddr,
1594a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trsvcid,
1595a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trtype,
15960d5ee2b2SSteve Wise 	&nvmet_attr_param_inline_data_size,
1597ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1598ea52ac1cSIsrael Rukshin 	&nvmet_attr_param_pi_enable,
1599ea52ac1cSIsrael Rukshin #endif
1600a07b4970SChristoph Hellwig 	NULL,
1601a07b4970SChristoph Hellwig };
1602a07b4970SChristoph Hellwig 
1603a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_item_ops = {
1604a07b4970SChristoph Hellwig 	.release		= nvmet_port_release,
1605a07b4970SChristoph Hellwig };
1606a07b4970SChristoph Hellwig 
160766603a31SBhumika Goyal static const struct config_item_type nvmet_port_type = {
1608a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_port_attrs,
1609a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_port_item_ops,
1610a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1611a07b4970SChristoph Hellwig };
1612a07b4970SChristoph Hellwig 
1613a07b4970SChristoph Hellwig static struct config_group *nvmet_ports_make(struct config_group *group,
1614a07b4970SChristoph Hellwig 		const char *name)
1615a07b4970SChristoph Hellwig {
1616a07b4970SChristoph Hellwig 	struct nvmet_port *port;
1617a07b4970SChristoph Hellwig 	u16 portid;
161862ac0d32SChristoph Hellwig 	u32 i;
1619a07b4970SChristoph Hellwig 
1620a07b4970SChristoph Hellwig 	if (kstrtou16(name, 0, &portid))
1621a07b4970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1622a07b4970SChristoph Hellwig 
1623a07b4970SChristoph Hellwig 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1624a07b4970SChristoph Hellwig 	if (!port)
1625f98d9ca1SDan Carpenter 		return ERR_PTR(-ENOMEM);
1626a07b4970SChristoph Hellwig 
162772efd25dSChristoph Hellwig 	port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
162872efd25dSChristoph Hellwig 			sizeof(*port->ana_state), GFP_KERNEL);
162972efd25dSChristoph Hellwig 	if (!port->ana_state) {
163072efd25dSChristoph Hellwig 		kfree(port);
163172efd25dSChristoph Hellwig 		return ERR_PTR(-ENOMEM);
163272efd25dSChristoph Hellwig 	}
163372efd25dSChristoph Hellwig 
163462ac0d32SChristoph Hellwig 	for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
163562ac0d32SChristoph Hellwig 		if (i == NVMET_DEFAULT_ANA_GRPID)
163662ac0d32SChristoph Hellwig 			port->ana_state[1] = NVME_ANA_OPTIMIZED;
163762ac0d32SChristoph Hellwig 		else
163862ac0d32SChristoph Hellwig 			port->ana_state[i] = NVME_ANA_INACCESSIBLE;
163962ac0d32SChristoph Hellwig 	}
164072efd25dSChristoph Hellwig 
1641b662a078SJay Sternberg 	list_add(&port->global_entry, &nvmet_ports_list);
1642b662a078SJay Sternberg 
1643a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->entry);
1644a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->subsystems);
1645a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->referrals);
16460d5ee2b2SSteve Wise 	port->inline_data_size = -1;	/* < 0 == let the transport choose */
1647a07b4970SChristoph Hellwig 
1648a07b4970SChristoph Hellwig 	port->disc_addr.portid = cpu_to_le16(portid);
1649d02abd19SChaitanya Kulkarni 	port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
16509b95d2fbSSagi Grimberg 	port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1651a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->group, name, &nvmet_port_type);
1652a07b4970SChristoph Hellwig 
1653a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->subsys_group,
1654a07b4970SChristoph Hellwig 			"subsystems", &nvmet_port_subsys_type);
1655a07b4970SChristoph Hellwig 	configfs_add_default_group(&port->subsys_group, &port->group);
1656a07b4970SChristoph Hellwig 
1657a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->referrals_group,
1658a07b4970SChristoph Hellwig 			"referrals", &nvmet_referrals_type);
1659a07b4970SChristoph Hellwig 	configfs_add_default_group(&port->referrals_group, &port->group);
1660a07b4970SChristoph Hellwig 
166162ac0d32SChristoph Hellwig 	config_group_init_type_name(&port->ana_groups_group,
166262ac0d32SChristoph Hellwig 			"ana_groups", &nvmet_ana_groups_type);
166362ac0d32SChristoph Hellwig 	configfs_add_default_group(&port->ana_groups_group, &port->group);
166462ac0d32SChristoph Hellwig 
166562ac0d32SChristoph Hellwig 	port->ana_default_group.port = port;
166662ac0d32SChristoph Hellwig 	port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
166762ac0d32SChristoph Hellwig 	config_group_init_type_name(&port->ana_default_group.group,
166862ac0d32SChristoph Hellwig 			__stringify(NVMET_DEFAULT_ANA_GRPID),
166962ac0d32SChristoph Hellwig 			&nvmet_ana_group_type);
167062ac0d32SChristoph Hellwig 	configfs_add_default_group(&port->ana_default_group.group,
167162ac0d32SChristoph Hellwig 			&port->ana_groups_group);
167262ac0d32SChristoph Hellwig 
1673a07b4970SChristoph Hellwig 	return &port->group;
1674a07b4970SChristoph Hellwig }
1675a07b4970SChristoph Hellwig 
1676a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_ports_group_ops = {
1677a07b4970SChristoph Hellwig 	.make_group		= nvmet_ports_make,
1678a07b4970SChristoph Hellwig };
1679a07b4970SChristoph Hellwig 
168066603a31SBhumika Goyal static const struct config_item_type nvmet_ports_type = {
1681a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_ports_group_ops,
1682a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1683a07b4970SChristoph Hellwig };
1684a07b4970SChristoph Hellwig 
1685a07b4970SChristoph Hellwig static struct config_group nvmet_subsystems_group;
1686a07b4970SChristoph Hellwig static struct config_group nvmet_ports_group;
1687a07b4970SChristoph Hellwig 
1688db1312ddSHannes Reinecke #ifdef CONFIG_NVME_TARGET_AUTH
1689db1312ddSHannes Reinecke static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
1690db1312ddSHannes Reinecke 		char *page)
1691db1312ddSHannes Reinecke {
1692db1312ddSHannes Reinecke 	u8 *dhchap_secret = to_host(item)->dhchap_secret;
1693db1312ddSHannes Reinecke 
1694db1312ddSHannes Reinecke 	if (!dhchap_secret)
1695db1312ddSHannes Reinecke 		return sprintf(page, "\n");
1696db1312ddSHannes Reinecke 	return sprintf(page, "%s\n", dhchap_secret);
1697db1312ddSHannes Reinecke }
1698db1312ddSHannes Reinecke 
1699db1312ddSHannes Reinecke static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
1700db1312ddSHannes Reinecke 		const char *page, size_t count)
1701db1312ddSHannes Reinecke {
1702db1312ddSHannes Reinecke 	struct nvmet_host *host = to_host(item);
1703db1312ddSHannes Reinecke 	int ret;
1704db1312ddSHannes Reinecke 
1705db1312ddSHannes Reinecke 	ret = nvmet_auth_set_key(host, page, false);
1706db1312ddSHannes Reinecke 	/*
1707db1312ddSHannes Reinecke 	 * Re-authentication is a soft state, so keep the
1708db1312ddSHannes Reinecke 	 * current authentication valid until the host
1709db1312ddSHannes Reinecke 	 * requests re-authentication.
1710db1312ddSHannes Reinecke 	 */
1711db1312ddSHannes Reinecke 	return ret < 0 ? ret : count;
1712db1312ddSHannes Reinecke }
1713db1312ddSHannes Reinecke 
1714db1312ddSHannes Reinecke CONFIGFS_ATTR(nvmet_host_, dhchap_key);
1715db1312ddSHannes Reinecke 
1716db1312ddSHannes Reinecke static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
1717db1312ddSHannes Reinecke 		char *page)
1718db1312ddSHannes Reinecke {
1719db1312ddSHannes Reinecke 	u8 *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
1720db1312ddSHannes Reinecke 
1721db1312ddSHannes Reinecke 	if (!dhchap_secret)
1722db1312ddSHannes Reinecke 		return sprintf(page, "\n");
1723db1312ddSHannes Reinecke 	return sprintf(page, "%s\n", dhchap_secret);
1724db1312ddSHannes Reinecke }
1725db1312ddSHannes Reinecke 
1726db1312ddSHannes Reinecke static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
1727db1312ddSHannes Reinecke 		const char *page, size_t count)
1728db1312ddSHannes Reinecke {
1729db1312ddSHannes Reinecke 	struct nvmet_host *host = to_host(item);
1730db1312ddSHannes Reinecke 	int ret;
1731db1312ddSHannes Reinecke 
1732db1312ddSHannes Reinecke 	ret = nvmet_auth_set_key(host, page, true);
1733db1312ddSHannes Reinecke 	/*
1734db1312ddSHannes Reinecke 	 * Re-authentication is a soft state, so keep the
1735db1312ddSHannes Reinecke 	 * current authentication valid until the host
1736db1312ddSHannes Reinecke 	 * requests re-authentication.
1737db1312ddSHannes Reinecke 	 */
1738db1312ddSHannes Reinecke 	return ret < 0 ? ret : count;
1739db1312ddSHannes Reinecke }
1740db1312ddSHannes Reinecke 
1741db1312ddSHannes Reinecke CONFIGFS_ATTR(nvmet_host_, dhchap_ctrl_key);
1742db1312ddSHannes Reinecke 
1743db1312ddSHannes Reinecke static ssize_t nvmet_host_dhchap_hash_show(struct config_item *item,
1744db1312ddSHannes Reinecke 		char *page)
1745db1312ddSHannes Reinecke {
1746db1312ddSHannes Reinecke 	struct nvmet_host *host = to_host(item);
1747db1312ddSHannes Reinecke 	const char *hash_name = nvme_auth_hmac_name(host->dhchap_hash_id);
1748db1312ddSHannes Reinecke 
1749db1312ddSHannes Reinecke 	return sprintf(page, "%s\n", hash_name ? hash_name : "none");
1750db1312ddSHannes Reinecke }
1751db1312ddSHannes Reinecke 
1752db1312ddSHannes Reinecke static ssize_t nvmet_host_dhchap_hash_store(struct config_item *item,
1753db1312ddSHannes Reinecke 		const char *page, size_t count)
1754db1312ddSHannes Reinecke {
1755db1312ddSHannes Reinecke 	struct nvmet_host *host = to_host(item);
1756db1312ddSHannes Reinecke 	u8 hmac_id;
1757db1312ddSHannes Reinecke 
1758db1312ddSHannes Reinecke 	hmac_id = nvme_auth_hmac_id(page);
1759db1312ddSHannes Reinecke 	if (hmac_id == NVME_AUTH_HASH_INVALID)
1760db1312ddSHannes Reinecke 		return -EINVAL;
1761db1312ddSHannes Reinecke 	if (!crypto_has_shash(nvme_auth_hmac_name(hmac_id), 0, 0))
1762db1312ddSHannes Reinecke 		return -ENOTSUPP;
1763db1312ddSHannes Reinecke 	host->dhchap_hash_id = hmac_id;
1764db1312ddSHannes Reinecke 	return count;
1765db1312ddSHannes Reinecke }
1766db1312ddSHannes Reinecke 
1767db1312ddSHannes Reinecke CONFIGFS_ATTR(nvmet_host_, dhchap_hash);
1768db1312ddSHannes Reinecke 
1769*7a277c37SHannes Reinecke static ssize_t nvmet_host_dhchap_dhgroup_show(struct config_item *item,
1770*7a277c37SHannes Reinecke 		char *page)
1771*7a277c37SHannes Reinecke {
1772*7a277c37SHannes Reinecke 	struct nvmet_host *host = to_host(item);
1773*7a277c37SHannes Reinecke 	const char *dhgroup = nvme_auth_dhgroup_name(host->dhchap_dhgroup_id);
1774*7a277c37SHannes Reinecke 
1775*7a277c37SHannes Reinecke 	return sprintf(page, "%s\n", dhgroup ? dhgroup : "none");
1776*7a277c37SHannes Reinecke }
1777*7a277c37SHannes Reinecke 
1778*7a277c37SHannes Reinecke static ssize_t nvmet_host_dhchap_dhgroup_store(struct config_item *item,
1779*7a277c37SHannes Reinecke 		const char *page, size_t count)
1780*7a277c37SHannes Reinecke {
1781*7a277c37SHannes Reinecke 	struct nvmet_host *host = to_host(item);
1782*7a277c37SHannes Reinecke 	int dhgroup_id;
1783*7a277c37SHannes Reinecke 
1784*7a277c37SHannes Reinecke 	dhgroup_id = nvme_auth_dhgroup_id(page);
1785*7a277c37SHannes Reinecke 	if (dhgroup_id == NVME_AUTH_DHGROUP_INVALID)
1786*7a277c37SHannes Reinecke 		return -EINVAL;
1787*7a277c37SHannes Reinecke 	if (dhgroup_id != NVME_AUTH_DHGROUP_NULL) {
1788*7a277c37SHannes Reinecke 		const char *kpp = nvme_auth_dhgroup_kpp(dhgroup_id);
1789*7a277c37SHannes Reinecke 
1790*7a277c37SHannes Reinecke 		if (!crypto_has_kpp(kpp, 0, 0))
1791*7a277c37SHannes Reinecke 			return -EINVAL;
1792*7a277c37SHannes Reinecke 	}
1793*7a277c37SHannes Reinecke 	host->dhchap_dhgroup_id = dhgroup_id;
1794*7a277c37SHannes Reinecke 	return count;
1795*7a277c37SHannes Reinecke }
1796*7a277c37SHannes Reinecke 
1797*7a277c37SHannes Reinecke CONFIGFS_ATTR(nvmet_host_, dhchap_dhgroup);
1798*7a277c37SHannes Reinecke 
1799db1312ddSHannes Reinecke static struct configfs_attribute *nvmet_host_attrs[] = {
1800db1312ddSHannes Reinecke 	&nvmet_host_attr_dhchap_key,
1801db1312ddSHannes Reinecke 	&nvmet_host_attr_dhchap_ctrl_key,
1802db1312ddSHannes Reinecke 	&nvmet_host_attr_dhchap_hash,
1803*7a277c37SHannes Reinecke 	&nvmet_host_attr_dhchap_dhgroup,
1804db1312ddSHannes Reinecke 	NULL,
1805db1312ddSHannes Reinecke };
1806db1312ddSHannes Reinecke #endif /* CONFIG_NVME_TARGET_AUTH */
1807db1312ddSHannes Reinecke 
1808a07b4970SChristoph Hellwig static void nvmet_host_release(struct config_item *item)
1809a07b4970SChristoph Hellwig {
1810a07b4970SChristoph Hellwig 	struct nvmet_host *host = to_host(item);
1811db1312ddSHannes Reinecke #ifdef CONFIG_NVME_TARGET_AUTH
1812db1312ddSHannes Reinecke 	if (host->dhchap_secret)
1813db1312ddSHannes Reinecke 		kfree(host->dhchap_secret);
1814db1312ddSHannes Reinecke #endif
1815a07b4970SChristoph Hellwig 	kfree(host);
1816a07b4970SChristoph Hellwig }
1817a07b4970SChristoph Hellwig 
1818a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_host_item_ops = {
1819a07b4970SChristoph Hellwig 	.release		= nvmet_host_release,
1820a07b4970SChristoph Hellwig };
1821a07b4970SChristoph Hellwig 
182266603a31SBhumika Goyal static const struct config_item_type nvmet_host_type = {
1823a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_host_item_ops,
1824db1312ddSHannes Reinecke #ifdef CONFIG_NVME_TARGET_AUTH
1825db1312ddSHannes Reinecke 	.ct_attrs		= nvmet_host_attrs,
1826db1312ddSHannes Reinecke #endif
1827a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1828a07b4970SChristoph Hellwig };
1829a07b4970SChristoph Hellwig 
1830a07b4970SChristoph Hellwig static struct config_group *nvmet_hosts_make_group(struct config_group *group,
1831a07b4970SChristoph Hellwig 		const char *name)
1832a07b4970SChristoph Hellwig {
1833a07b4970SChristoph Hellwig 	struct nvmet_host *host;
1834a07b4970SChristoph Hellwig 
1835a07b4970SChristoph Hellwig 	host = kzalloc(sizeof(*host), GFP_KERNEL);
1836a07b4970SChristoph Hellwig 	if (!host)
1837a07b4970SChristoph Hellwig 		return ERR_PTR(-ENOMEM);
1838a07b4970SChristoph Hellwig 
1839db1312ddSHannes Reinecke #ifdef CONFIG_NVME_TARGET_AUTH
1840db1312ddSHannes Reinecke 	/* Default to SHA256 */
1841db1312ddSHannes Reinecke 	host->dhchap_hash_id = NVME_AUTH_HASH_SHA256;
1842db1312ddSHannes Reinecke #endif
1843db1312ddSHannes Reinecke 
1844a07b4970SChristoph Hellwig 	config_group_init_type_name(&host->group, name, &nvmet_host_type);
1845a07b4970SChristoph Hellwig 
1846a07b4970SChristoph Hellwig 	return &host->group;
1847a07b4970SChristoph Hellwig }
1848a07b4970SChristoph Hellwig 
1849a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_hosts_group_ops = {
1850a07b4970SChristoph Hellwig 	.make_group		= nvmet_hosts_make_group,
1851a07b4970SChristoph Hellwig };
1852a07b4970SChristoph Hellwig 
185366603a31SBhumika Goyal static const struct config_item_type nvmet_hosts_type = {
1854a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_hosts_group_ops,
1855a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1856a07b4970SChristoph Hellwig };
1857a07b4970SChristoph Hellwig 
1858a07b4970SChristoph Hellwig static struct config_group nvmet_hosts_group;
1859a07b4970SChristoph Hellwig 
186066603a31SBhumika Goyal static const struct config_item_type nvmet_root_type = {
1861a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1862a07b4970SChristoph Hellwig };
1863a07b4970SChristoph Hellwig 
1864a07b4970SChristoph Hellwig static struct configfs_subsystem nvmet_configfs_subsystem = {
1865a07b4970SChristoph Hellwig 	.su_group = {
1866a07b4970SChristoph Hellwig 		.cg_item = {
1867a07b4970SChristoph Hellwig 			.ci_namebuf	= "nvmet",
1868a07b4970SChristoph Hellwig 			.ci_type	= &nvmet_root_type,
1869a07b4970SChristoph Hellwig 		},
1870a07b4970SChristoph Hellwig 	},
1871a07b4970SChristoph Hellwig };
1872a07b4970SChristoph Hellwig 
1873a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void)
1874a07b4970SChristoph Hellwig {
1875a07b4970SChristoph Hellwig 	int ret;
1876a07b4970SChristoph Hellwig 
1877a07b4970SChristoph Hellwig 	config_group_init(&nvmet_configfs_subsystem.su_group);
1878a07b4970SChristoph Hellwig 	mutex_init(&nvmet_configfs_subsystem.su_mutex);
1879a07b4970SChristoph Hellwig 
1880a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_subsystems_group,
1881a07b4970SChristoph Hellwig 			"subsystems", &nvmet_subsystems_type);
1882a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_subsystems_group,
1883a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1884a07b4970SChristoph Hellwig 
1885a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_ports_group,
1886a07b4970SChristoph Hellwig 			"ports", &nvmet_ports_type);
1887a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_ports_group,
1888a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1889a07b4970SChristoph Hellwig 
1890a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_hosts_group,
1891a07b4970SChristoph Hellwig 			"hosts", &nvmet_hosts_type);
1892a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_hosts_group,
1893a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1894a07b4970SChristoph Hellwig 
1895a07b4970SChristoph Hellwig 	ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
1896a07b4970SChristoph Hellwig 	if (ret) {
1897a07b4970SChristoph Hellwig 		pr_err("configfs_register_subsystem: %d\n", ret);
1898a07b4970SChristoph Hellwig 		return ret;
1899a07b4970SChristoph Hellwig 	}
1900a07b4970SChristoph Hellwig 
1901a07b4970SChristoph Hellwig 	return 0;
1902a07b4970SChristoph Hellwig }
1903a07b4970SChristoph Hellwig 
1904a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void)
1905a07b4970SChristoph Hellwig {
1906a07b4970SChristoph Hellwig 	configfs_unregister_subsystem(&nvmet_configfs_subsystem);
1907a07b4970SChristoph Hellwig }
1908