xref: /openbmc/linux/drivers/nvme/target/configfs.c (revision ca3b4293)
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
799722c8aSChristophe JAILLET #include <linux/kstrtox.h>
8a07b4970SChristoph Hellwig #include <linux/kernel.h>
9a07b4970SChristoph Hellwig #include <linux/module.h>
10a07b4970SChristoph Hellwig #include <linux/slab.h>
11a07b4970SChristoph Hellwig #include <linux/stat.h>
12a07b4970SChristoph Hellwig #include <linux/ctype.h>
13c6925093SLogan Gunthorpe #include <linux/pci.h>
14c6925093SLogan Gunthorpe #include <linux/pci-p2pdma.h>
15db1312ddSHannes Reinecke #ifdef CONFIG_NVME_TARGET_AUTH
16db1312ddSHannes Reinecke #include <linux/nvme-auth.h>
17db1312ddSHannes Reinecke #endif
18db1312ddSHannes Reinecke #include <crypto/hash.h>
19db1312ddSHannes Reinecke #include <crypto/kpp.h>
20946fd64bSNitesh Shetty #include <linux/nospec.h>
21a07b4970SChristoph Hellwig 
22a07b4970SChristoph Hellwig #include "nvmet.h"
23a07b4970SChristoph Hellwig 
2466603a31SBhumika Goyal static const struct config_item_type nvmet_host_type;
2566603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type;
26a07b4970SChristoph Hellwig 
27b662a078SJay Sternberg static LIST_HEAD(nvmet_ports_list);
28b662a078SJay Sternberg struct list_head *nvmet_ports = &nvmet_ports_list;
29b662a078SJay Sternberg 
3045e2f3c2SChaitanya Kulkarni struct nvmet_type_name_map {
31a5d18612SChristoph Hellwig 	u8		type;
32a5d18612SChristoph Hellwig 	const char	*name;
3345e2f3c2SChaitanya Kulkarni };
3445e2f3c2SChaitanya Kulkarni 
3545e2f3c2SChaitanya Kulkarni static struct nvmet_type_name_map nvmet_transport[] = {
36a5d18612SChristoph Hellwig 	{ NVMF_TRTYPE_RDMA,	"rdma" },
37a5d18612SChristoph Hellwig 	{ NVMF_TRTYPE_FC,	"fc" },
38ad4f530eSSagi Grimberg 	{ NVMF_TRTYPE_TCP,	"tcp" },
39a5d18612SChristoph Hellwig 	{ NVMF_TRTYPE_LOOP,	"loop" },
40a5d18612SChristoph Hellwig };
41a5d18612SChristoph Hellwig 
427e764179SChaitanya Kulkarni static const struct nvmet_type_name_map nvmet_addr_family[] = {
437e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_PCI,		"pcie" },
447e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_IP4,		"ipv4" },
457e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_IP6,		"ipv6" },
467e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_IB,		"ib" },
477e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_FC,		"fc" },
48d02abd19SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_LOOP,	"loop" },
497e764179SChaitanya Kulkarni };
507e764179SChaitanya Kulkarni 
nvmet_is_port_enabled(struct nvmet_port * p,const char * caller)513ecb5faaSChaitanya Kulkarni static bool nvmet_is_port_enabled(struct nvmet_port *p, const char *caller)
523ecb5faaSChaitanya Kulkarni {
533ecb5faaSChaitanya Kulkarni 	if (p->enabled)
543ecb5faaSChaitanya Kulkarni 		pr_err("Disable port '%u' before changing attribute in %s\n",
553ecb5faaSChaitanya Kulkarni 		       le16_to_cpu(p->disc_addr.portid), caller);
563ecb5faaSChaitanya Kulkarni 	return p->enabled;
573ecb5faaSChaitanya Kulkarni }
583ecb5faaSChaitanya Kulkarni 
59a07b4970SChristoph Hellwig /*
60a07b4970SChristoph Hellwig  * nvmet_port Generic ConfigFS definitions.
61a07b4970SChristoph Hellwig  * Used in any place in the ConfigFS tree that refers to an address.
62a07b4970SChristoph Hellwig  */
nvmet_addr_adrfam_show(struct config_item * item,char * page)637e764179SChaitanya Kulkarni static ssize_t nvmet_addr_adrfam_show(struct config_item *item, char *page)
64a07b4970SChristoph Hellwig {
657e764179SChaitanya Kulkarni 	u8 adrfam = to_nvmet_port(item)->disc_addr.adrfam;
667e764179SChaitanya Kulkarni 	int i;
677e764179SChaitanya Kulkarni 
687e764179SChaitanya Kulkarni 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
697e764179SChaitanya Kulkarni 		if (nvmet_addr_family[i].type == adrfam)
7098152eb7SChaitanya Kulkarni 			return snprintf(page, PAGE_SIZE, "%s\n",
7198152eb7SChaitanya Kulkarni 					nvmet_addr_family[i].name);
72a07b4970SChristoph Hellwig 	}
737e764179SChaitanya Kulkarni 
7498152eb7SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "\n");
75a07b4970SChristoph Hellwig }
76a07b4970SChristoph Hellwig 
nvmet_addr_adrfam_store(struct config_item * item,const char * page,size_t count)77a07b4970SChristoph Hellwig static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
78a07b4970SChristoph Hellwig 		const char *page, size_t count)
79a07b4970SChristoph Hellwig {
80a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
817e764179SChaitanya Kulkarni 	int i;
82a07b4970SChristoph Hellwig 
833ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
84a07b4970SChristoph Hellwig 		return -EACCES;
85a07b4970SChristoph Hellwig 
867e764179SChaitanya Kulkarni 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
877e764179SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_addr_family[i].name))
887e764179SChaitanya Kulkarni 			goto found;
89a07b4970SChristoph Hellwig 	}
90a07b4970SChristoph Hellwig 
917e764179SChaitanya Kulkarni 	pr_err("Invalid value '%s' for adrfam\n", page);
927e764179SChaitanya Kulkarni 	return -EINVAL;
937e764179SChaitanya Kulkarni 
947e764179SChaitanya Kulkarni found:
95d02abd19SChaitanya Kulkarni 	port->disc_addr.adrfam = nvmet_addr_family[i].type;
96a07b4970SChristoph Hellwig 	return count;
97a07b4970SChristoph Hellwig }
98a07b4970SChristoph Hellwig 
99a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_adrfam);
100a07b4970SChristoph Hellwig 
nvmet_addr_portid_show(struct config_item * item,char * page)101a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_show(struct config_item *item,
102a07b4970SChristoph Hellwig 		char *page)
103a07b4970SChristoph Hellwig {
10473d77c53SChaitanya Kulkarni 	__le16 portid = to_nvmet_port(item)->disc_addr.portid;
105a07b4970SChristoph Hellwig 
10673d77c53SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%d\n", le16_to_cpu(portid));
107a07b4970SChristoph Hellwig }
108a07b4970SChristoph Hellwig 
nvmet_addr_portid_store(struct config_item * item,const char * page,size_t count)109a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_store(struct config_item *item,
110a07b4970SChristoph Hellwig 		const char *page, size_t count)
111a07b4970SChristoph Hellwig {
112a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
113a07b4970SChristoph Hellwig 	u16 portid = 0;
114a07b4970SChristoph Hellwig 
115a07b4970SChristoph Hellwig 	if (kstrtou16(page, 0, &portid)) {
116a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for portid\n", page);
117a07b4970SChristoph Hellwig 		return -EINVAL;
118a07b4970SChristoph Hellwig 	}
119a07b4970SChristoph Hellwig 
1203ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
121a07b4970SChristoph Hellwig 		return -EACCES;
1223ecb5faaSChaitanya Kulkarni 
123a07b4970SChristoph Hellwig 	port->disc_addr.portid = cpu_to_le16(portid);
124a07b4970SChristoph Hellwig 	return count;
125a07b4970SChristoph Hellwig }
126a07b4970SChristoph Hellwig 
127a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_portid);
128a07b4970SChristoph Hellwig 
nvmet_addr_traddr_show(struct config_item * item,char * page)129a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_show(struct config_item *item,
130a07b4970SChristoph Hellwig 		char *page)
131a07b4970SChristoph Hellwig {
132a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
133a07b4970SChristoph Hellwig 
13473d77c53SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.traddr);
135a07b4970SChristoph Hellwig }
136a07b4970SChristoph Hellwig 
nvmet_addr_traddr_store(struct config_item * item,const char * page,size_t count)137a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_store(struct config_item *item,
138a07b4970SChristoph Hellwig 		const char *page, size_t count)
139a07b4970SChristoph Hellwig {
140a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
141a07b4970SChristoph Hellwig 
142a07b4970SChristoph Hellwig 	if (count > NVMF_TRADDR_SIZE) {
143a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for traddr\n", page);
144a07b4970SChristoph Hellwig 		return -EINVAL;
145a07b4970SChristoph Hellwig 	}
146a07b4970SChristoph Hellwig 
1473ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
148a07b4970SChristoph Hellwig 		return -EACCES;
1499ba2a5cbSSagi Grimberg 
1509ba2a5cbSSagi Grimberg 	if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
1519ba2a5cbSSagi Grimberg 		return -EINVAL;
1529ba2a5cbSSagi Grimberg 	return count;
153a07b4970SChristoph Hellwig }
154a07b4970SChristoph Hellwig 
155a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_traddr);
156a07b4970SChristoph Hellwig 
15787628e28SChaitanya Kulkarni static const struct nvmet_type_name_map nvmet_addr_treq[] = {
15887628e28SChaitanya Kulkarni 	{ NVMF_TREQ_NOT_SPECIFIED,	"not specified" },
15987628e28SChaitanya Kulkarni 	{ NVMF_TREQ_REQUIRED,		"required" },
16087628e28SChaitanya Kulkarni 	{ NVMF_TREQ_NOT_REQUIRED,	"not required" },
16187628e28SChaitanya Kulkarni };
16287628e28SChaitanya Kulkarni 
nvmet_addr_treq_show(struct config_item * item,char * page)16387628e28SChaitanya Kulkarni static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page)
164a07b4970SChristoph Hellwig {
16587628e28SChaitanya Kulkarni 	u8 treq = to_nvmet_port(item)->disc_addr.treq &
16687628e28SChaitanya Kulkarni 		NVME_TREQ_SECURE_CHANNEL_MASK;
16787628e28SChaitanya Kulkarni 	int i;
16887628e28SChaitanya Kulkarni 
16987628e28SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
17087628e28SChaitanya Kulkarni 		if (treq == nvmet_addr_treq[i].type)
17198152eb7SChaitanya Kulkarni 			return snprintf(page, PAGE_SIZE, "%s\n",
17298152eb7SChaitanya Kulkarni 					nvmet_addr_treq[i].name);
173a07b4970SChristoph Hellwig 	}
17487628e28SChaitanya Kulkarni 
17598152eb7SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "\n");
176a07b4970SChristoph Hellwig }
177a07b4970SChristoph Hellwig 
nvmet_addr_treq_store(struct config_item * item,const char * page,size_t count)178a07b4970SChristoph Hellwig static ssize_t nvmet_addr_treq_store(struct config_item *item,
179a07b4970SChristoph Hellwig 		const char *page, size_t count)
180a07b4970SChristoph Hellwig {
181a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1820445e1b5SSagi Grimberg 	u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK;
18387628e28SChaitanya Kulkarni 	int i;
184a07b4970SChristoph Hellwig 
1853ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
186a07b4970SChristoph Hellwig 		return -EACCES;
187a07b4970SChristoph Hellwig 
18887628e28SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
18987628e28SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_addr_treq[i].name))
19087628e28SChaitanya Kulkarni 			goto found;
19187628e28SChaitanya Kulkarni 	}
19287628e28SChaitanya Kulkarni 
193a07b4970SChristoph Hellwig 	pr_err("Invalid value '%s' for treq\n", page);
194a07b4970SChristoph Hellwig 	return -EINVAL;
195a07b4970SChristoph Hellwig 
19687628e28SChaitanya Kulkarni found:
19787628e28SChaitanya Kulkarni 	treq |= nvmet_addr_treq[i].type;
19887628e28SChaitanya Kulkarni 	port->disc_addr.treq = treq;
199a07b4970SChristoph Hellwig 	return count;
200a07b4970SChristoph Hellwig }
201a07b4970SChristoph Hellwig 
202a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_treq);
203a07b4970SChristoph Hellwig 
nvmet_addr_trsvcid_show(struct config_item * item,char * page)204a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
205a07b4970SChristoph Hellwig 		char *page)
206a07b4970SChristoph Hellwig {
207a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
208a07b4970SChristoph Hellwig 
20973d77c53SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.trsvcid);
210a07b4970SChristoph Hellwig }
211a07b4970SChristoph Hellwig 
nvmet_addr_trsvcid_store(struct config_item * item,const char * page,size_t count)212a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
213a07b4970SChristoph Hellwig 		const char *page, size_t count)
214a07b4970SChristoph Hellwig {
215a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
216a07b4970SChristoph Hellwig 
217a07b4970SChristoph Hellwig 	if (count > NVMF_TRSVCID_SIZE) {
218a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for trsvcid\n", page);
219a07b4970SChristoph Hellwig 		return -EINVAL;
220a07b4970SChristoph Hellwig 	}
2213ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
222a07b4970SChristoph Hellwig 		return -EACCES;
2239ba2a5cbSSagi Grimberg 
2249ba2a5cbSSagi Grimberg 	if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
2259ba2a5cbSSagi Grimberg 		return -EINVAL;
2269ba2a5cbSSagi Grimberg 	return count;
227a07b4970SChristoph Hellwig }
228a07b4970SChristoph Hellwig 
229a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trsvcid);
230a07b4970SChristoph Hellwig 
nvmet_param_inline_data_size_show(struct config_item * item,char * page)2310d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
2320d5ee2b2SSteve Wise 		char *page)
2330d5ee2b2SSteve Wise {
2340d5ee2b2SSteve Wise 	struct nvmet_port *port = to_nvmet_port(item);
2350d5ee2b2SSteve Wise 
2360d5ee2b2SSteve Wise 	return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
2370d5ee2b2SSteve Wise }
2380d5ee2b2SSteve Wise 
nvmet_param_inline_data_size_store(struct config_item * item,const char * page,size_t count)2390d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
2400d5ee2b2SSteve Wise 		const char *page, size_t count)
2410d5ee2b2SSteve Wise {
2420d5ee2b2SSteve Wise 	struct nvmet_port *port = to_nvmet_port(item);
2430d5ee2b2SSteve Wise 	int ret;
2440d5ee2b2SSteve Wise 
2453ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
2460d5ee2b2SSteve Wise 		return -EACCES;
2470d5ee2b2SSteve Wise 	ret = kstrtoint(page, 0, &port->inline_data_size);
2480d5ee2b2SSteve Wise 	if (ret) {
2490d5ee2b2SSteve Wise 		pr_err("Invalid value '%s' for inline_data_size\n", page);
2500d5ee2b2SSteve Wise 		return -EINVAL;
2510d5ee2b2SSteve Wise 	}
2520d5ee2b2SSteve Wise 	return count;
2530d5ee2b2SSteve Wise }
2540d5ee2b2SSteve Wise 
2550d5ee2b2SSteve Wise CONFIGFS_ATTR(nvmet_, param_inline_data_size);
2560d5ee2b2SSteve Wise 
257ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
nvmet_param_pi_enable_show(struct config_item * item,char * page)258ea52ac1cSIsrael Rukshin static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
259ea52ac1cSIsrael Rukshin 		char *page)
260ea52ac1cSIsrael Rukshin {
261ea52ac1cSIsrael Rukshin 	struct nvmet_port *port = to_nvmet_port(item);
262ea52ac1cSIsrael Rukshin 
263ea52ac1cSIsrael Rukshin 	return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
264ea52ac1cSIsrael Rukshin }
265ea52ac1cSIsrael Rukshin 
nvmet_param_pi_enable_store(struct config_item * item,const char * page,size_t count)266ea52ac1cSIsrael Rukshin static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
267ea52ac1cSIsrael Rukshin 		const char *page, size_t count)
268ea52ac1cSIsrael Rukshin {
269ea52ac1cSIsrael Rukshin 	struct nvmet_port *port = to_nvmet_port(item);
270ea52ac1cSIsrael Rukshin 	bool val;
271ea52ac1cSIsrael Rukshin 
27299722c8aSChristophe JAILLET 	if (kstrtobool(page, &val))
273ea52ac1cSIsrael Rukshin 		return -EINVAL;
274ea52ac1cSIsrael Rukshin 
275cc345622SIsrael Rukshin 	if (nvmet_is_port_enabled(port, __func__))
276ea52ac1cSIsrael Rukshin 		return -EACCES;
277ea52ac1cSIsrael Rukshin 
278ea52ac1cSIsrael Rukshin 	port->pi_enable = val;
279ea52ac1cSIsrael Rukshin 	return count;
280ea52ac1cSIsrael Rukshin }
281ea52ac1cSIsrael Rukshin 
282ea52ac1cSIsrael Rukshin CONFIGFS_ATTR(nvmet_, param_pi_enable);
283ea52ac1cSIsrael Rukshin #endif
284ea52ac1cSIsrael Rukshin 
nvmet_addr_trtype_show(struct config_item * item,char * page)285a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_show(struct config_item *item,
286a07b4970SChristoph Hellwig 		char *page)
287a07b4970SChristoph Hellwig {
288a5d18612SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
289a5d18612SChristoph Hellwig 	int i;
290a5d18612SChristoph Hellwig 
29145e2f3c2SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
29245e2f3c2SChaitanya Kulkarni 		if (port->disc_addr.trtype == nvmet_transport[i].type)
29398152eb7SChaitanya Kulkarni 			return snprintf(page, PAGE_SIZE,
29498152eb7SChaitanya Kulkarni 					"%s\n", nvmet_transport[i].name);
295a07b4970SChristoph Hellwig 	}
296a5d18612SChristoph Hellwig 
297a5d18612SChristoph Hellwig 	return sprintf(page, "\n");
298a07b4970SChristoph Hellwig }
299a07b4970SChristoph Hellwig 
nvmet_port_init_tsas_rdma(struct nvmet_port * port)300a07b4970SChristoph Hellwig static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
301a07b4970SChristoph Hellwig {
302a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
303a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
304a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
305a07b4970SChristoph Hellwig }
306a07b4970SChristoph Hellwig 
nvmet_addr_trtype_store(struct config_item * item,const char * page,size_t count)307a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_store(struct config_item *item,
308a07b4970SChristoph Hellwig 		const char *page, size_t count)
309a07b4970SChristoph Hellwig {
310a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
311a5d18612SChristoph Hellwig 	int i;
312a07b4970SChristoph Hellwig 
3133ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
314a07b4970SChristoph Hellwig 		return -EACCES;
315a07b4970SChristoph Hellwig 
31645e2f3c2SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
31745e2f3c2SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_transport[i].name))
318a5d18612SChristoph Hellwig 			goto found;
319a07b4970SChristoph Hellwig 	}
320a07b4970SChristoph Hellwig 
321a5d18612SChristoph Hellwig 	pr_err("Invalid value '%s' for trtype\n", page);
322a5d18612SChristoph Hellwig 	return -EINVAL;
3237e764179SChaitanya Kulkarni 
324a5d18612SChristoph Hellwig found:
325a5d18612SChristoph Hellwig 	memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
32645e2f3c2SChaitanya Kulkarni 	port->disc_addr.trtype = nvmet_transport[i].type;
327a5d18612SChristoph Hellwig 	if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
328a5d18612SChristoph Hellwig 		nvmet_port_init_tsas_rdma(port);
329a07b4970SChristoph Hellwig 	return count;
330a07b4970SChristoph Hellwig }
331a07b4970SChristoph Hellwig 
332a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trtype);
333a07b4970SChristoph Hellwig 
334a07b4970SChristoph Hellwig /*
335a07b4970SChristoph Hellwig  * Namespace structures & file operation functions below
336a07b4970SChristoph Hellwig  */
nvmet_ns_device_path_show(struct config_item * item,char * page)337a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
338a07b4970SChristoph Hellwig {
339a07b4970SChristoph Hellwig 	return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
340a07b4970SChristoph Hellwig }
341a07b4970SChristoph Hellwig 
nvmet_ns_device_path_store(struct config_item * item,const char * page,size_t count)342a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_store(struct config_item *item,
343a07b4970SChristoph Hellwig 		const char *page, size_t count)
344a07b4970SChristoph Hellwig {
345a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
346a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = ns->subsys;
3475613d312SHannes Reinecke 	size_t len;
348a07b4970SChristoph Hellwig 	int ret;
349a07b4970SChristoph Hellwig 
350a07b4970SChristoph Hellwig 	mutex_lock(&subsys->lock);
351a07b4970SChristoph Hellwig 	ret = -EBUSY;
352e4fcf07cSSolganik Alexander 	if (ns->enabled)
353a07b4970SChristoph Hellwig 		goto out_unlock;
354a07b4970SChristoph Hellwig 
3555613d312SHannes Reinecke 	ret = -EINVAL;
3565613d312SHannes Reinecke 	len = strcspn(page, "\n");
3575613d312SHannes Reinecke 	if (!len)
3585613d312SHannes Reinecke 		goto out_unlock;
359a07b4970SChristoph Hellwig 
3605613d312SHannes Reinecke 	kfree(ns->device_path);
361a07b4970SChristoph Hellwig 	ret = -ENOMEM;
36209bb8986SChen Zhou 	ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
363a07b4970SChristoph Hellwig 	if (!ns->device_path)
364a07b4970SChristoph Hellwig 		goto out_unlock;
365a07b4970SChristoph Hellwig 
366a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
367a07b4970SChristoph Hellwig 	return count;
368a07b4970SChristoph Hellwig 
369a07b4970SChristoph Hellwig out_unlock:
370a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
371a07b4970SChristoph Hellwig 	return ret;
372a07b4970SChristoph Hellwig }
373a07b4970SChristoph Hellwig 
374a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_path);
375a07b4970SChristoph Hellwig 
376c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA
nvmet_ns_p2pmem_show(struct config_item * item,char * page)377c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
378c6925093SLogan Gunthorpe {
379c6925093SLogan Gunthorpe 	struct nvmet_ns *ns = to_nvmet_ns(item);
380c6925093SLogan Gunthorpe 
381c6925093SLogan Gunthorpe 	return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
382c6925093SLogan Gunthorpe }
383c6925093SLogan Gunthorpe 
nvmet_ns_p2pmem_store(struct config_item * item,const char * page,size_t count)384c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
385c6925093SLogan Gunthorpe 		const char *page, size_t count)
386c6925093SLogan Gunthorpe {
387c6925093SLogan Gunthorpe 	struct nvmet_ns *ns = to_nvmet_ns(item);
388c6925093SLogan Gunthorpe 	struct pci_dev *p2p_dev = NULL;
389c6925093SLogan Gunthorpe 	bool use_p2pmem;
390c6925093SLogan Gunthorpe 	int ret = count;
391c6925093SLogan Gunthorpe 	int error;
392c6925093SLogan Gunthorpe 
393c6925093SLogan Gunthorpe 	mutex_lock(&ns->subsys->lock);
394c6925093SLogan Gunthorpe 	if (ns->enabled) {
395c6925093SLogan Gunthorpe 		ret = -EBUSY;
396c6925093SLogan Gunthorpe 		goto out_unlock;
397c6925093SLogan Gunthorpe 	}
398c6925093SLogan Gunthorpe 
399c6925093SLogan Gunthorpe 	error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
400c6925093SLogan Gunthorpe 	if (error) {
401c6925093SLogan Gunthorpe 		ret = error;
402c6925093SLogan Gunthorpe 		goto out_unlock;
403c6925093SLogan Gunthorpe 	}
404c6925093SLogan Gunthorpe 
405c6925093SLogan Gunthorpe 	ns->use_p2pmem = use_p2pmem;
406c6925093SLogan Gunthorpe 	pci_dev_put(ns->p2p_dev);
407c6925093SLogan Gunthorpe 	ns->p2p_dev = p2p_dev;
408c6925093SLogan Gunthorpe 
409c6925093SLogan Gunthorpe out_unlock:
410c6925093SLogan Gunthorpe 	mutex_unlock(&ns->subsys->lock);
411c6925093SLogan Gunthorpe 
412c6925093SLogan Gunthorpe 	return ret;
413c6925093SLogan Gunthorpe }
414c6925093SLogan Gunthorpe 
415c6925093SLogan Gunthorpe CONFIGFS_ATTR(nvmet_ns_, p2pmem);
416c6925093SLogan Gunthorpe #endif /* CONFIG_PCI_P2PDMA */
417c6925093SLogan Gunthorpe 
nvmet_ns_device_uuid_show(struct config_item * item,char * page)418430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
419430c7befSJohannes Thumshirn {
420430c7befSJohannes Thumshirn 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
421430c7befSJohannes Thumshirn }
422430c7befSJohannes Thumshirn 
nvmet_ns_device_uuid_store(struct config_item * item,const char * page,size_t count)423430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
424430c7befSJohannes Thumshirn 					  const char *page, size_t count)
425430c7befSJohannes Thumshirn {
426430c7befSJohannes Thumshirn 	struct nvmet_ns *ns = to_nvmet_ns(item);
427430c7befSJohannes Thumshirn 	struct nvmet_subsys *subsys = ns->subsys;
428430c7befSJohannes Thumshirn 	int ret = 0;
429430c7befSJohannes Thumshirn 
430430c7befSJohannes Thumshirn 	mutex_lock(&subsys->lock);
431430c7befSJohannes Thumshirn 	if (ns->enabled) {
432430c7befSJohannes Thumshirn 		ret = -EBUSY;
433430c7befSJohannes Thumshirn 		goto out_unlock;
434430c7befSJohannes Thumshirn 	}
435430c7befSJohannes Thumshirn 
436430c7befSJohannes Thumshirn 	if (uuid_parse(page, &ns->uuid))
437430c7befSJohannes Thumshirn 		ret = -EINVAL;
438430c7befSJohannes Thumshirn 
439430c7befSJohannes Thumshirn out_unlock:
440430c7befSJohannes Thumshirn 	mutex_unlock(&subsys->lock);
441430c7befSJohannes Thumshirn 	return ret ? ret : count;
442430c7befSJohannes Thumshirn }
443430c7befSJohannes Thumshirn 
444f871749aSMax Gurtovoy CONFIGFS_ATTR(nvmet_ns_, device_uuid);
445f871749aSMax Gurtovoy 
nvmet_ns_device_nguid_show(struct config_item * item,char * page)446a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
447a07b4970SChristoph Hellwig {
448a07b4970SChristoph Hellwig 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
449a07b4970SChristoph Hellwig }
450a07b4970SChristoph Hellwig 
nvmet_ns_device_nguid_store(struct config_item * item,const char * page,size_t count)451a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
452a07b4970SChristoph Hellwig 		const char *page, size_t count)
453a07b4970SChristoph Hellwig {
454a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
455a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = ns->subsys;
456a07b4970SChristoph Hellwig 	u8 nguid[16];
457a07b4970SChristoph Hellwig 	const char *p = page;
458a07b4970SChristoph Hellwig 	int i;
459a07b4970SChristoph Hellwig 	int ret = 0;
460a07b4970SChristoph Hellwig 
461a07b4970SChristoph Hellwig 	mutex_lock(&subsys->lock);
462e4fcf07cSSolganik Alexander 	if (ns->enabled) {
463a07b4970SChristoph Hellwig 		ret = -EBUSY;
464a07b4970SChristoph Hellwig 		goto out_unlock;
465a07b4970SChristoph Hellwig 	}
466a07b4970SChristoph Hellwig 
467a07b4970SChristoph Hellwig 	for (i = 0; i < 16; i++) {
468a07b4970SChristoph Hellwig 		if (p + 2 > page + count) {
469a07b4970SChristoph Hellwig 			ret = -EINVAL;
470a07b4970SChristoph Hellwig 			goto out_unlock;
471a07b4970SChristoph Hellwig 		}
472a07b4970SChristoph Hellwig 		if (!isxdigit(p[0]) || !isxdigit(p[1])) {
473a07b4970SChristoph Hellwig 			ret = -EINVAL;
474a07b4970SChristoph Hellwig 			goto out_unlock;
475a07b4970SChristoph Hellwig 		}
476a07b4970SChristoph Hellwig 
477a07b4970SChristoph Hellwig 		nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
478a07b4970SChristoph Hellwig 		p += 2;
479a07b4970SChristoph Hellwig 
480a07b4970SChristoph Hellwig 		if (*p == '-' || *p == ':')
481a07b4970SChristoph Hellwig 			p++;
482a07b4970SChristoph Hellwig 	}
483a07b4970SChristoph Hellwig 
484a07b4970SChristoph Hellwig 	memcpy(&ns->nguid, nguid, sizeof(nguid));
485a07b4970SChristoph Hellwig out_unlock:
486a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
487a07b4970SChristoph Hellwig 	return ret ? ret : count;
488a07b4970SChristoph Hellwig }
489a07b4970SChristoph Hellwig 
490a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_nguid);
491a07b4970SChristoph Hellwig 
nvmet_ns_ana_grpid_show(struct config_item * item,char * page)49262ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
49362ac0d32SChristoph Hellwig {
49462ac0d32SChristoph Hellwig 	return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
49562ac0d32SChristoph Hellwig }
49662ac0d32SChristoph Hellwig 
nvmet_ns_ana_grpid_store(struct config_item * item,const char * page,size_t count)49762ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
49862ac0d32SChristoph Hellwig 		const char *page, size_t count)
49962ac0d32SChristoph Hellwig {
50062ac0d32SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
50162ac0d32SChristoph Hellwig 	u32 oldgrpid, newgrpid;
50262ac0d32SChristoph Hellwig 	int ret;
50362ac0d32SChristoph Hellwig 
50462ac0d32SChristoph Hellwig 	ret = kstrtou32(page, 0, &newgrpid);
50562ac0d32SChristoph Hellwig 	if (ret)
50662ac0d32SChristoph Hellwig 		return ret;
50762ac0d32SChristoph Hellwig 
50862ac0d32SChristoph Hellwig 	if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
50962ac0d32SChristoph Hellwig 		return -EINVAL;
51062ac0d32SChristoph Hellwig 
51162ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
51262ac0d32SChristoph Hellwig 	oldgrpid = ns->anagrpid;
513946fd64bSNitesh Shetty 	newgrpid = array_index_nospec(newgrpid, NVMET_MAX_ANAGRPS);
51462ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[newgrpid]++;
51562ac0d32SChristoph Hellwig 	ns->anagrpid = newgrpid;
51662ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[oldgrpid]--;
51762ac0d32SChristoph Hellwig 	nvmet_ana_chgcnt++;
51862ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
51962ac0d32SChristoph Hellwig 
52062ac0d32SChristoph Hellwig 	nvmet_send_ana_event(ns->subsys, NULL);
52162ac0d32SChristoph Hellwig 	return count;
52262ac0d32SChristoph Hellwig }
52362ac0d32SChristoph Hellwig 
52462ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
52562ac0d32SChristoph Hellwig 
nvmet_ns_enable_show(struct config_item * item,char * page)526a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
527a07b4970SChristoph Hellwig {
528e4fcf07cSSolganik Alexander 	return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
529a07b4970SChristoph Hellwig }
530a07b4970SChristoph Hellwig 
nvmet_ns_enable_store(struct config_item * item,const char * page,size_t count)531a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_store(struct config_item *item,
532a07b4970SChristoph Hellwig 		const char *page, size_t count)
533a07b4970SChristoph Hellwig {
534a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
535a07b4970SChristoph Hellwig 	bool enable;
536a07b4970SChristoph Hellwig 	int ret = 0;
537a07b4970SChristoph Hellwig 
53899722c8aSChristophe JAILLET 	if (kstrtobool(page, &enable))
539a07b4970SChristoph Hellwig 		return -EINVAL;
540a07b4970SChristoph Hellwig 
541ca3b4293SSagi Grimberg 	/*
542ca3b4293SSagi Grimberg 	 * take a global nvmet_config_sem because the disable routine has a
543ca3b4293SSagi Grimberg 	 * window where it releases the subsys-lock, giving a chance to
544ca3b4293SSagi Grimberg 	 * a parallel enable to concurrently execute causing the disable to
545ca3b4293SSagi Grimberg 	 * have a misaccounting of the ns percpu_ref.
546ca3b4293SSagi Grimberg 	 */
547ca3b4293SSagi Grimberg 	down_write(&nvmet_config_sem);
548a07b4970SChristoph Hellwig 	if (enable)
549a07b4970SChristoph Hellwig 		ret = nvmet_ns_enable(ns);
550a07b4970SChristoph Hellwig 	else
551a07b4970SChristoph Hellwig 		nvmet_ns_disable(ns);
552ca3b4293SSagi Grimberg 	up_write(&nvmet_config_sem);
553a07b4970SChristoph Hellwig 
554a07b4970SChristoph Hellwig 	return ret ? ret : count;
555a07b4970SChristoph Hellwig }
556a07b4970SChristoph Hellwig 
557a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, enable);
558a07b4970SChristoph Hellwig 
nvmet_ns_buffered_io_show(struct config_item * item,char * page)55955eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
56055eb942eSChaitanya Kulkarni {
56155eb942eSChaitanya Kulkarni 	return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
56255eb942eSChaitanya Kulkarni }
56355eb942eSChaitanya Kulkarni 
nvmet_ns_buffered_io_store(struct config_item * item,const char * page,size_t count)56455eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
56555eb942eSChaitanya Kulkarni 		const char *page, size_t count)
56655eb942eSChaitanya Kulkarni {
56755eb942eSChaitanya Kulkarni 	struct nvmet_ns *ns = to_nvmet_ns(item);
56855eb942eSChaitanya Kulkarni 	bool val;
56955eb942eSChaitanya Kulkarni 
57099722c8aSChristophe JAILLET 	if (kstrtobool(page, &val))
57155eb942eSChaitanya Kulkarni 		return -EINVAL;
57255eb942eSChaitanya Kulkarni 
57355eb942eSChaitanya Kulkarni 	mutex_lock(&ns->subsys->lock);
57455eb942eSChaitanya Kulkarni 	if (ns->enabled) {
57555eb942eSChaitanya Kulkarni 		pr_err("disable ns before setting buffered_io value.\n");
57655eb942eSChaitanya Kulkarni 		mutex_unlock(&ns->subsys->lock);
57755eb942eSChaitanya Kulkarni 		return -EINVAL;
57855eb942eSChaitanya Kulkarni 	}
57955eb942eSChaitanya Kulkarni 
58055eb942eSChaitanya Kulkarni 	ns->buffered_io = val;
58155eb942eSChaitanya Kulkarni 	mutex_unlock(&ns->subsys->lock);
58255eb942eSChaitanya Kulkarni 	return count;
58355eb942eSChaitanya Kulkarni }
58455eb942eSChaitanya Kulkarni 
58555eb942eSChaitanya Kulkarni CONFIGFS_ATTR(nvmet_ns_, buffered_io);
58655eb942eSChaitanya Kulkarni 
nvmet_ns_revalidate_size_store(struct config_item * item,const char * page,size_t count)5871f357548SChaitanya Kulkarni static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
5881f357548SChaitanya Kulkarni 		const char *page, size_t count)
5891f357548SChaitanya Kulkarni {
5901f357548SChaitanya Kulkarni 	struct nvmet_ns *ns = to_nvmet_ns(item);
5911f357548SChaitanya Kulkarni 	bool val;
5921f357548SChaitanya Kulkarni 
59399722c8aSChristophe JAILLET 	if (kstrtobool(page, &val))
5941f357548SChaitanya Kulkarni 		return -EINVAL;
5951f357548SChaitanya Kulkarni 
5961f357548SChaitanya Kulkarni 	if (!val)
5971f357548SChaitanya Kulkarni 		return -EINVAL;
5981f357548SChaitanya Kulkarni 
5991f357548SChaitanya Kulkarni 	mutex_lock(&ns->subsys->lock);
6001f357548SChaitanya Kulkarni 	if (!ns->enabled) {
6011f357548SChaitanya Kulkarni 		pr_err("enable ns before revalidate.\n");
6021f357548SChaitanya Kulkarni 		mutex_unlock(&ns->subsys->lock);
6031f357548SChaitanya Kulkarni 		return -EINVAL;
6041f357548SChaitanya Kulkarni 	}
605da783733SChristoph Hellwig 	if (nvmet_ns_revalidate(ns))
606da783733SChristoph Hellwig 		nvmet_ns_changed(ns->subsys, ns->nsid);
6071f357548SChaitanya Kulkarni 	mutex_unlock(&ns->subsys->lock);
6081f357548SChaitanya Kulkarni 	return count;
6091f357548SChaitanya Kulkarni }
6101f357548SChaitanya Kulkarni 
6111f357548SChaitanya Kulkarni CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
6121f357548SChaitanya Kulkarni 
613a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_ns_attrs[] = {
614a07b4970SChristoph Hellwig 	&nvmet_ns_attr_device_path,
615a07b4970SChristoph Hellwig 	&nvmet_ns_attr_device_nguid,
616430c7befSJohannes Thumshirn 	&nvmet_ns_attr_device_uuid,
61762ac0d32SChristoph Hellwig 	&nvmet_ns_attr_ana_grpid,
618a07b4970SChristoph Hellwig 	&nvmet_ns_attr_enable,
61955eb942eSChaitanya Kulkarni 	&nvmet_ns_attr_buffered_io,
6201f357548SChaitanya Kulkarni 	&nvmet_ns_attr_revalidate_size,
621c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA
622c6925093SLogan Gunthorpe 	&nvmet_ns_attr_p2pmem,
623c6925093SLogan Gunthorpe #endif
624a07b4970SChristoph Hellwig 	NULL,
625a07b4970SChristoph Hellwig };
626a07b4970SChristoph Hellwig 
nvmet_subsys_nsid_exists(struct nvmet_subsys * subsys,u32 nsid)62771de5fc3SSagi Grimberg bool nvmet_subsys_nsid_exists(struct nvmet_subsys *subsys, u32 nsid)
62871de5fc3SSagi Grimberg {
62971de5fc3SSagi Grimberg 	struct config_item *ns_item;
630396bc5e5SDan Carpenter 	char name[12];
63171de5fc3SSagi Grimberg 
632396bc5e5SDan Carpenter 	snprintf(name, sizeof(name), "%u", nsid);
63371de5fc3SSagi Grimberg 	mutex_lock(&subsys->namespaces_group.cg_subsys->su_mutex);
63471de5fc3SSagi Grimberg 	ns_item = config_group_find_item(&subsys->namespaces_group, name);
63571de5fc3SSagi Grimberg 	mutex_unlock(&subsys->namespaces_group.cg_subsys->su_mutex);
63671de5fc3SSagi Grimberg 	return ns_item != NULL;
63771de5fc3SSagi Grimberg }
63871de5fc3SSagi Grimberg 
nvmet_ns_release(struct config_item * item)639a07b4970SChristoph Hellwig static void nvmet_ns_release(struct config_item *item)
640a07b4970SChristoph Hellwig {
641a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
642a07b4970SChristoph Hellwig 
643a07b4970SChristoph Hellwig 	nvmet_ns_free(ns);
644a07b4970SChristoph Hellwig }
645a07b4970SChristoph Hellwig 
646a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_ns_item_ops = {
647a07b4970SChristoph Hellwig 	.release		= nvmet_ns_release,
648a07b4970SChristoph Hellwig };
649a07b4970SChristoph Hellwig 
65066603a31SBhumika Goyal static const struct config_item_type nvmet_ns_type = {
651a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_ns_item_ops,
652a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_ns_attrs,
653a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
654a07b4970SChristoph Hellwig };
655a07b4970SChristoph Hellwig 
nvmet_ns_make(struct config_group * group,const char * name)656a07b4970SChristoph Hellwig static struct config_group *nvmet_ns_make(struct config_group *group,
657a07b4970SChristoph Hellwig 		const char *name)
658a07b4970SChristoph Hellwig {
659a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
660a07b4970SChristoph Hellwig 	struct nvmet_ns *ns;
661a07b4970SChristoph Hellwig 	int ret;
662a07b4970SChristoph Hellwig 	u32 nsid;
663a07b4970SChristoph Hellwig 
664a07b4970SChristoph Hellwig 	ret = kstrtou32(name, 0, &nsid);
665a07b4970SChristoph Hellwig 	if (ret)
666a07b4970SChristoph Hellwig 		goto out;
667a07b4970SChristoph Hellwig 
668a07b4970SChristoph Hellwig 	ret = -EINVAL;
6695ba89503SMikhail Skorzhinskii 	if (nsid == 0 || nsid == NVME_NSID_ALL) {
6705ba89503SMikhail Skorzhinskii 		pr_err("invalid nsid %#x", nsid);
671a07b4970SChristoph Hellwig 		goto out;
6725ba89503SMikhail Skorzhinskii 	}
673a07b4970SChristoph Hellwig 
674a07b4970SChristoph Hellwig 	ret = -ENOMEM;
675a07b4970SChristoph Hellwig 	ns = nvmet_ns_alloc(subsys, nsid);
676a07b4970SChristoph Hellwig 	if (!ns)
677a07b4970SChristoph Hellwig 		goto out;
678a07b4970SChristoph Hellwig 	config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
679a07b4970SChristoph Hellwig 
680a07b4970SChristoph Hellwig 	pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
681a07b4970SChristoph Hellwig 
682a07b4970SChristoph Hellwig 	return &ns->group;
683a07b4970SChristoph Hellwig out:
684a07b4970SChristoph Hellwig 	return ERR_PTR(ret);
685a07b4970SChristoph Hellwig }
686a07b4970SChristoph Hellwig 
687a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_namespaces_group_ops = {
688a07b4970SChristoph Hellwig 	.make_group		= nvmet_ns_make,
689a07b4970SChristoph Hellwig };
690a07b4970SChristoph Hellwig 
69166603a31SBhumika Goyal static const struct config_item_type nvmet_namespaces_type = {
692a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_namespaces_group_ops,
693a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
694a07b4970SChristoph Hellwig };
695a07b4970SChristoph Hellwig 
696cae5b01aSLogan Gunthorpe #ifdef CONFIG_NVME_TARGET_PASSTHRU
697cae5b01aSLogan Gunthorpe 
nvmet_passthru_device_path_show(struct config_item * item,char * page)698cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
699cae5b01aSLogan Gunthorpe 		char *page)
700cae5b01aSLogan Gunthorpe {
701cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
702cae5b01aSLogan Gunthorpe 
703cae5b01aSLogan Gunthorpe 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
704cae5b01aSLogan Gunthorpe }
705cae5b01aSLogan Gunthorpe 
nvmet_passthru_device_path_store(struct config_item * item,const char * page,size_t count)706cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
707cae5b01aSLogan Gunthorpe 		const char *page, size_t count)
708cae5b01aSLogan Gunthorpe {
709cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
710cae5b01aSLogan Gunthorpe 	size_t len;
711cae5b01aSLogan Gunthorpe 	int ret;
712cae5b01aSLogan Gunthorpe 
713cae5b01aSLogan Gunthorpe 	mutex_lock(&subsys->lock);
714cae5b01aSLogan Gunthorpe 
715cae5b01aSLogan Gunthorpe 	ret = -EBUSY;
716cae5b01aSLogan Gunthorpe 	if (subsys->passthru_ctrl)
717cae5b01aSLogan Gunthorpe 		goto out_unlock;
718cae5b01aSLogan Gunthorpe 
719cae5b01aSLogan Gunthorpe 	ret = -EINVAL;
720cae5b01aSLogan Gunthorpe 	len = strcspn(page, "\n");
721cae5b01aSLogan Gunthorpe 	if (!len)
722cae5b01aSLogan Gunthorpe 		goto out_unlock;
723cae5b01aSLogan Gunthorpe 
724cae5b01aSLogan Gunthorpe 	kfree(subsys->passthru_ctrl_path);
725cae5b01aSLogan Gunthorpe 	ret = -ENOMEM;
726cae5b01aSLogan Gunthorpe 	subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
727cae5b01aSLogan Gunthorpe 	if (!subsys->passthru_ctrl_path)
728cae5b01aSLogan Gunthorpe 		goto out_unlock;
729cae5b01aSLogan Gunthorpe 
730cae5b01aSLogan Gunthorpe 	mutex_unlock(&subsys->lock);
731cae5b01aSLogan Gunthorpe 
732cae5b01aSLogan Gunthorpe 	return count;
733cae5b01aSLogan Gunthorpe out_unlock:
734cae5b01aSLogan Gunthorpe 	mutex_unlock(&subsys->lock);
735cae5b01aSLogan Gunthorpe 	return ret;
736cae5b01aSLogan Gunthorpe }
737cae5b01aSLogan Gunthorpe CONFIGFS_ATTR(nvmet_passthru_, device_path);
738cae5b01aSLogan Gunthorpe 
nvmet_passthru_enable_show(struct config_item * item,char * page)739cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_enable_show(struct config_item *item,
740cae5b01aSLogan Gunthorpe 		char *page)
741cae5b01aSLogan Gunthorpe {
742cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
743cae5b01aSLogan Gunthorpe 
744cae5b01aSLogan Gunthorpe 	return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
745cae5b01aSLogan Gunthorpe }
746cae5b01aSLogan Gunthorpe 
nvmet_passthru_enable_store(struct config_item * item,const char * page,size_t count)747cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_enable_store(struct config_item *item,
748cae5b01aSLogan Gunthorpe 		const char *page, size_t count)
749cae5b01aSLogan Gunthorpe {
750cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
751cae5b01aSLogan Gunthorpe 	bool enable;
752cae5b01aSLogan Gunthorpe 	int ret = 0;
753cae5b01aSLogan Gunthorpe 
75499722c8aSChristophe JAILLET 	if (kstrtobool(page, &enable))
755cae5b01aSLogan Gunthorpe 		return -EINVAL;
756cae5b01aSLogan Gunthorpe 
757cae5b01aSLogan Gunthorpe 	if (enable)
758cae5b01aSLogan Gunthorpe 		ret = nvmet_passthru_ctrl_enable(subsys);
759cae5b01aSLogan Gunthorpe 	else
760cae5b01aSLogan Gunthorpe 		nvmet_passthru_ctrl_disable(subsys);
761cae5b01aSLogan Gunthorpe 
762cae5b01aSLogan Gunthorpe 	return ret ? ret : count;
763cae5b01aSLogan Gunthorpe }
764cae5b01aSLogan Gunthorpe CONFIGFS_ATTR(nvmet_passthru_, enable);
765cae5b01aSLogan Gunthorpe 
nvmet_passthru_admin_timeout_show(struct config_item * item,char * page)766a2f6a2b8SChaitanya Kulkarni static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
767a2f6a2b8SChaitanya Kulkarni 		char *page)
768a2f6a2b8SChaitanya Kulkarni {
769a2f6a2b8SChaitanya Kulkarni 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->admin_timeout);
770a2f6a2b8SChaitanya Kulkarni }
771a2f6a2b8SChaitanya Kulkarni 
nvmet_passthru_admin_timeout_store(struct config_item * item,const char * page,size_t count)772a2f6a2b8SChaitanya Kulkarni static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
773a2f6a2b8SChaitanya Kulkarni 		const char *page, size_t count)
774a2f6a2b8SChaitanya Kulkarni {
775a2f6a2b8SChaitanya Kulkarni 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
776a2f6a2b8SChaitanya Kulkarni 	unsigned int timeout;
777a2f6a2b8SChaitanya Kulkarni 
778a2f6a2b8SChaitanya Kulkarni 	if (kstrtouint(page, 0, &timeout))
779a2f6a2b8SChaitanya Kulkarni 		return -EINVAL;
780a2f6a2b8SChaitanya Kulkarni 	subsys->admin_timeout = timeout;
781a2f6a2b8SChaitanya Kulkarni 	return count;
782a2f6a2b8SChaitanya Kulkarni }
783a2f6a2b8SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
784a2f6a2b8SChaitanya Kulkarni 
nvmet_passthru_io_timeout_show(struct config_item * item,char * page)78547e9730cSChaitanya Kulkarni static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
78647e9730cSChaitanya Kulkarni 		char *page)
78747e9730cSChaitanya Kulkarni {
78847e9730cSChaitanya Kulkarni 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->io_timeout);
78947e9730cSChaitanya Kulkarni }
79047e9730cSChaitanya Kulkarni 
nvmet_passthru_io_timeout_store(struct config_item * item,const char * page,size_t count)79147e9730cSChaitanya Kulkarni static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
79247e9730cSChaitanya Kulkarni 		const char *page, size_t count)
79347e9730cSChaitanya Kulkarni {
79447e9730cSChaitanya Kulkarni 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
79547e9730cSChaitanya Kulkarni 	unsigned int timeout;
79647e9730cSChaitanya Kulkarni 
79747e9730cSChaitanya Kulkarni 	if (kstrtouint(page, 0, &timeout))
79847e9730cSChaitanya Kulkarni 		return -EINVAL;
79947e9730cSChaitanya Kulkarni 	subsys->io_timeout = timeout;
80047e9730cSChaitanya Kulkarni 	return count;
80147e9730cSChaitanya Kulkarni }
80247e9730cSChaitanya Kulkarni CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
80347e9730cSChaitanya Kulkarni 
nvmet_passthru_clear_ids_show(struct config_item * item,char * page)80434ad6151SAlan Adamson static ssize_t nvmet_passthru_clear_ids_show(struct config_item *item,
80534ad6151SAlan Adamson 		char *page)
80634ad6151SAlan Adamson {
80734ad6151SAlan Adamson 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->clear_ids);
80834ad6151SAlan Adamson }
80934ad6151SAlan Adamson 
nvmet_passthru_clear_ids_store(struct config_item * item,const char * page,size_t count)81034ad6151SAlan Adamson static ssize_t nvmet_passthru_clear_ids_store(struct config_item *item,
81134ad6151SAlan Adamson 		const char *page, size_t count)
81234ad6151SAlan Adamson {
81334ad6151SAlan Adamson 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
81434ad6151SAlan Adamson 	unsigned int clear_ids;
81534ad6151SAlan Adamson 
81634ad6151SAlan Adamson 	if (kstrtouint(page, 0, &clear_ids))
81734ad6151SAlan Adamson 		return -EINVAL;
81834ad6151SAlan Adamson 	subsys->clear_ids = clear_ids;
81934ad6151SAlan Adamson 	return count;
82034ad6151SAlan Adamson }
82134ad6151SAlan Adamson CONFIGFS_ATTR(nvmet_passthru_, clear_ids);
82234ad6151SAlan Adamson 
823cae5b01aSLogan Gunthorpe static struct configfs_attribute *nvmet_passthru_attrs[] = {
824cae5b01aSLogan Gunthorpe 	&nvmet_passthru_attr_device_path,
825cae5b01aSLogan Gunthorpe 	&nvmet_passthru_attr_enable,
826a2f6a2b8SChaitanya Kulkarni 	&nvmet_passthru_attr_admin_timeout,
82747e9730cSChaitanya Kulkarni 	&nvmet_passthru_attr_io_timeout,
82834ad6151SAlan Adamson 	&nvmet_passthru_attr_clear_ids,
829cae5b01aSLogan Gunthorpe 	NULL,
830cae5b01aSLogan Gunthorpe };
831cae5b01aSLogan Gunthorpe 
832cae5b01aSLogan Gunthorpe static const struct config_item_type nvmet_passthru_type = {
833cae5b01aSLogan Gunthorpe 	.ct_attrs		= nvmet_passthru_attrs,
834cae5b01aSLogan Gunthorpe 	.ct_owner		= THIS_MODULE,
835cae5b01aSLogan Gunthorpe };
836cae5b01aSLogan Gunthorpe 
nvmet_add_passthru_group(struct nvmet_subsys * subsys)837cae5b01aSLogan Gunthorpe static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
838cae5b01aSLogan Gunthorpe {
839cae5b01aSLogan Gunthorpe 	config_group_init_type_name(&subsys->passthru_group,
840cae5b01aSLogan Gunthorpe 				    "passthru", &nvmet_passthru_type);
841cae5b01aSLogan Gunthorpe 	configfs_add_default_group(&subsys->passthru_group,
842cae5b01aSLogan Gunthorpe 				   &subsys->group);
843cae5b01aSLogan Gunthorpe }
844cae5b01aSLogan Gunthorpe 
845cae5b01aSLogan Gunthorpe #else /* CONFIG_NVME_TARGET_PASSTHRU */
846cae5b01aSLogan Gunthorpe 
nvmet_add_passthru_group(struct nvmet_subsys * subsys)847cae5b01aSLogan Gunthorpe static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
848cae5b01aSLogan Gunthorpe {
849cae5b01aSLogan Gunthorpe }
850cae5b01aSLogan Gunthorpe 
851cae5b01aSLogan Gunthorpe #endif /* CONFIG_NVME_TARGET_PASSTHRU */
852cae5b01aSLogan Gunthorpe 
nvmet_port_subsys_allow_link(struct config_item * parent,struct config_item * target)853a07b4970SChristoph Hellwig static int nvmet_port_subsys_allow_link(struct config_item *parent,
854a07b4970SChristoph Hellwig 		struct config_item *target)
855a07b4970SChristoph Hellwig {
856a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
857a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys;
858a07b4970SChristoph Hellwig 	struct nvmet_subsys_link *link, *p;
859a07b4970SChristoph Hellwig 	int ret;
860a07b4970SChristoph Hellwig 
861a07b4970SChristoph Hellwig 	if (target->ci_type != &nvmet_subsys_type) {
862a07b4970SChristoph Hellwig 		pr_err("can only link subsystems into the subsystems dir.!\n");
863a07b4970SChristoph Hellwig 		return -EINVAL;
864a07b4970SChristoph Hellwig 	}
865a07b4970SChristoph Hellwig 	subsys = to_subsys(target);
866a07b4970SChristoph Hellwig 	link = kmalloc(sizeof(*link), GFP_KERNEL);
867a07b4970SChristoph Hellwig 	if (!link)
868a07b4970SChristoph Hellwig 		return -ENOMEM;
869a07b4970SChristoph Hellwig 	link->subsys = subsys;
870a07b4970SChristoph Hellwig 
871a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
872a07b4970SChristoph Hellwig 	ret = -EEXIST;
873a07b4970SChristoph Hellwig 	list_for_each_entry(p, &port->subsystems, entry) {
874a07b4970SChristoph Hellwig 		if (p->subsys == subsys)
875a07b4970SChristoph Hellwig 			goto out_free_link;
876a07b4970SChristoph Hellwig 	}
877a07b4970SChristoph Hellwig 
878a07b4970SChristoph Hellwig 	if (list_empty(&port->subsystems)) {
879a07b4970SChristoph Hellwig 		ret = nvmet_enable_port(port);
880a07b4970SChristoph Hellwig 		if (ret)
881a07b4970SChristoph Hellwig 			goto out_free_link;
882a07b4970SChristoph Hellwig 	}
883a07b4970SChristoph Hellwig 
884a07b4970SChristoph Hellwig 	list_add_tail(&link->entry, &port->subsystems);
885b662a078SJay Sternberg 	nvmet_port_disc_changed(port, subsys);
886b662a078SJay Sternberg 
887a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
888a07b4970SChristoph Hellwig 	return 0;
889a07b4970SChristoph Hellwig 
890a07b4970SChristoph Hellwig out_free_link:
891a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
892a07b4970SChristoph Hellwig 	kfree(link);
893a07b4970SChristoph Hellwig 	return ret;
894a07b4970SChristoph Hellwig }
895a07b4970SChristoph Hellwig 
nvmet_port_subsys_drop_link(struct config_item * parent,struct config_item * target)896e16769d4SAndrzej Pietrasiewicz static void nvmet_port_subsys_drop_link(struct config_item *parent,
897a07b4970SChristoph Hellwig 		struct config_item *target)
898a07b4970SChristoph Hellwig {
899a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
900a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(target);
901a07b4970SChristoph Hellwig 	struct nvmet_subsys_link *p;
902a07b4970SChristoph Hellwig 
903a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
904a07b4970SChristoph Hellwig 	list_for_each_entry(p, &port->subsystems, entry) {
905a07b4970SChristoph Hellwig 		if (p->subsys == subsys)
906a07b4970SChristoph Hellwig 			goto found;
907a07b4970SChristoph Hellwig 	}
908a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
909e16769d4SAndrzej Pietrasiewicz 	return;
910a07b4970SChristoph Hellwig 
911a07b4970SChristoph Hellwig found:
912a07b4970SChristoph Hellwig 	list_del(&p->entry);
9133aed8673SLogan Gunthorpe 	nvmet_port_del_ctrls(port, subsys);
914b662a078SJay Sternberg 	nvmet_port_disc_changed(port, subsys);
915b662a078SJay Sternberg 
916a07b4970SChristoph Hellwig 	if (list_empty(&port->subsystems))
917a07b4970SChristoph Hellwig 		nvmet_disable_port(port);
918a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
919a07b4970SChristoph Hellwig 	kfree(p);
920a07b4970SChristoph Hellwig }
921a07b4970SChristoph Hellwig 
922a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_subsys_item_ops = {
923a07b4970SChristoph Hellwig 	.allow_link		= nvmet_port_subsys_allow_link,
924a07b4970SChristoph Hellwig 	.drop_link		= nvmet_port_subsys_drop_link,
925a07b4970SChristoph Hellwig };
926a07b4970SChristoph Hellwig 
92766603a31SBhumika Goyal static const struct config_item_type nvmet_port_subsys_type = {
928a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_port_subsys_item_ops,
929a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
930a07b4970SChristoph Hellwig };
931a07b4970SChristoph Hellwig 
nvmet_allowed_hosts_allow_link(struct config_item * parent,struct config_item * target)932a07b4970SChristoph Hellwig static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
933a07b4970SChristoph Hellwig 		struct config_item *target)
934a07b4970SChristoph Hellwig {
935a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
936a07b4970SChristoph Hellwig 	struct nvmet_host *host;
937a07b4970SChristoph Hellwig 	struct nvmet_host_link *link, *p;
938a07b4970SChristoph Hellwig 	int ret;
939a07b4970SChristoph Hellwig 
940a07b4970SChristoph Hellwig 	if (target->ci_type != &nvmet_host_type) {
941a07b4970SChristoph Hellwig 		pr_err("can only link hosts into the allowed_hosts directory!\n");
942a07b4970SChristoph Hellwig 		return -EINVAL;
943a07b4970SChristoph Hellwig 	}
944a07b4970SChristoph Hellwig 
945a07b4970SChristoph Hellwig 	host = to_host(target);
946a07b4970SChristoph Hellwig 	link = kmalloc(sizeof(*link), GFP_KERNEL);
947a07b4970SChristoph Hellwig 	if (!link)
948a07b4970SChristoph Hellwig 		return -ENOMEM;
949a07b4970SChristoph Hellwig 	link->host = host;
950a07b4970SChristoph Hellwig 
951a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
952a07b4970SChristoph Hellwig 	ret = -EINVAL;
953a07b4970SChristoph Hellwig 	if (subsys->allow_any_host) {
954a07b4970SChristoph Hellwig 		pr_err("can't add hosts when allow_any_host is set!\n");
955a07b4970SChristoph Hellwig 		goto out_free_link;
956a07b4970SChristoph Hellwig 	}
957a07b4970SChristoph Hellwig 
958a07b4970SChristoph Hellwig 	ret = -EEXIST;
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 out_free_link;
962a07b4970SChristoph Hellwig 	}
963a07b4970SChristoph Hellwig 	list_add_tail(&link->entry, &subsys->hosts);
964b662a078SJay Sternberg 	nvmet_subsys_disc_changed(subsys, host);
965b662a078SJay Sternberg 
966a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
967a07b4970SChristoph Hellwig 	return 0;
968a07b4970SChristoph Hellwig out_free_link:
969a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
970a07b4970SChristoph Hellwig 	kfree(link);
971a07b4970SChristoph Hellwig 	return ret;
972a07b4970SChristoph Hellwig }
973a07b4970SChristoph Hellwig 
nvmet_allowed_hosts_drop_link(struct config_item * parent,struct config_item * target)974e16769d4SAndrzej Pietrasiewicz static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
975a07b4970SChristoph Hellwig 		struct config_item *target)
976a07b4970SChristoph Hellwig {
977a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
978a07b4970SChristoph Hellwig 	struct nvmet_host *host = to_host(target);
979a07b4970SChristoph Hellwig 	struct nvmet_host_link *p;
980a07b4970SChristoph Hellwig 
981a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
982a07b4970SChristoph Hellwig 	list_for_each_entry(p, &subsys->hosts, entry) {
983a07b4970SChristoph Hellwig 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
984a07b4970SChristoph Hellwig 			goto found;
985a07b4970SChristoph Hellwig 	}
986a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
987e16769d4SAndrzej Pietrasiewicz 	return;
988a07b4970SChristoph Hellwig 
989a07b4970SChristoph Hellwig found:
990a07b4970SChristoph Hellwig 	list_del(&p->entry);
991b662a078SJay Sternberg 	nvmet_subsys_disc_changed(subsys, host);
992b662a078SJay Sternberg 
993a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
994a07b4970SChristoph Hellwig 	kfree(p);
995a07b4970SChristoph Hellwig }
996a07b4970SChristoph Hellwig 
997a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
998a07b4970SChristoph Hellwig 	.allow_link		= nvmet_allowed_hosts_allow_link,
999a07b4970SChristoph Hellwig 	.drop_link		= nvmet_allowed_hosts_drop_link,
1000a07b4970SChristoph Hellwig };
1001a07b4970SChristoph Hellwig 
100266603a31SBhumika Goyal static const struct config_item_type nvmet_allowed_hosts_type = {
1003a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_allowed_hosts_item_ops,
1004a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1005a07b4970SChristoph Hellwig };
1006a07b4970SChristoph Hellwig 
nvmet_subsys_attr_allow_any_host_show(struct config_item * item,char * page)1007a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
1008a07b4970SChristoph Hellwig 		char *page)
1009a07b4970SChristoph Hellwig {
1010a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n",
1011a07b4970SChristoph Hellwig 		to_subsys(item)->allow_any_host);
1012a07b4970SChristoph Hellwig }
1013a07b4970SChristoph Hellwig 
nvmet_subsys_attr_allow_any_host_store(struct config_item * item,const char * page,size_t count)1014a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
1015a07b4970SChristoph Hellwig 		const char *page, size_t count)
1016a07b4970SChristoph Hellwig {
1017a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(item);
1018a07b4970SChristoph Hellwig 	bool allow_any_host;
1019a07b4970SChristoph Hellwig 	int ret = 0;
1020a07b4970SChristoph Hellwig 
102199722c8aSChristophe JAILLET 	if (kstrtobool(page, &allow_any_host))
1022a07b4970SChristoph Hellwig 		return -EINVAL;
1023a07b4970SChristoph Hellwig 
1024a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
1025a07b4970SChristoph Hellwig 	if (allow_any_host && !list_empty(&subsys->hosts)) {
1026a07b4970SChristoph Hellwig 		pr_err("Can't set allow_any_host when explicit hosts are set!\n");
1027a07b4970SChristoph Hellwig 		ret = -EINVAL;
1028a07b4970SChristoph Hellwig 		goto out_unlock;
1029a07b4970SChristoph Hellwig 	}
1030a07b4970SChristoph Hellwig 
1031b662a078SJay Sternberg 	if (subsys->allow_any_host != allow_any_host) {
1032a07b4970SChristoph Hellwig 		subsys->allow_any_host = allow_any_host;
1033b662a078SJay Sternberg 		nvmet_subsys_disc_changed(subsys, NULL);
1034b662a078SJay Sternberg 	}
1035b662a078SJay Sternberg 
1036a07b4970SChristoph Hellwig out_unlock:
1037a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
1038a07b4970SChristoph Hellwig 	return ret ? ret : count;
1039a07b4970SChristoph Hellwig }
1040a07b4970SChristoph Hellwig 
1041a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
1042a07b4970SChristoph Hellwig 
nvmet_subsys_attr_version_show(struct config_item * item,char * page)104341528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
1044c61d788bSJohannes Thumshirn 					      char *page)
1045c61d788bSJohannes Thumshirn {
1046c61d788bSJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
1047c61d788bSJohannes Thumshirn 
1048c61d788bSJohannes Thumshirn 	if (NVME_TERTIARY(subsys->ver))
1049a0f0dbaaSChaitanya Kulkarni 		return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
1050a0f0dbaaSChaitanya Kulkarni 				NVME_MAJOR(subsys->ver),
1051a0f0dbaaSChaitanya Kulkarni 				NVME_MINOR(subsys->ver),
1052a0f0dbaaSChaitanya Kulkarni 				NVME_TERTIARY(subsys->ver));
1053527123c7SChaitanya Kulkarni 
1054a0f0dbaaSChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
1055a0f0dbaaSChaitanya Kulkarni 			NVME_MAJOR(subsys->ver),
1056a0f0dbaaSChaitanya Kulkarni 			NVME_MINOR(subsys->ver));
1057c61d788bSJohannes Thumshirn }
1058c61d788bSJohannes Thumshirn 
105987fd4cc1SNoam Gottlieb static ssize_t
nvmet_subsys_attr_version_store_locked(struct nvmet_subsys * subsys,const char * page,size_t count)106087fd4cc1SNoam Gottlieb nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,
1061c61d788bSJohannes Thumshirn 		const char *page, size_t count)
1062c61d788bSJohannes Thumshirn {
1063c61d788bSJohannes Thumshirn 	int major, minor, tertiary = 0;
1064c61d788bSJohannes Thumshirn 	int ret;
1065c61d788bSJohannes Thumshirn 
106687fd4cc1SNoam Gottlieb 	if (subsys->subsys_discovered) {
106787fd4cc1SNoam Gottlieb 		if (NVME_TERTIARY(subsys->ver))
106887fd4cc1SNoam Gottlieb 			pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
106987fd4cc1SNoam Gottlieb 			       NVME_MAJOR(subsys->ver),
107087fd4cc1SNoam Gottlieb 			       NVME_MINOR(subsys->ver),
107187fd4cc1SNoam Gottlieb 			       NVME_TERTIARY(subsys->ver));
107287fd4cc1SNoam Gottlieb 		else
107387fd4cc1SNoam Gottlieb 			pr_err("Can't set version number. %llu.%llu is already assigned\n",
107487fd4cc1SNoam Gottlieb 			       NVME_MAJOR(subsys->ver),
107587fd4cc1SNoam Gottlieb 			       NVME_MINOR(subsys->ver));
107687fd4cc1SNoam Gottlieb 		return -EINVAL;
107787fd4cc1SNoam Gottlieb 	}
107887fd4cc1SNoam Gottlieb 
1079ba76af67SLogan Gunthorpe 	/* passthru subsystems use the underlying controller's version */
1080ab7a2737SChristoph Hellwig 	if (nvmet_is_passthru_subsys(subsys))
1081ba76af67SLogan Gunthorpe 		return -EINVAL;
1082ba76af67SLogan Gunthorpe 
1083c61d788bSJohannes Thumshirn 	ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
1084c61d788bSJohannes Thumshirn 	if (ret != 2 && ret != 3)
1085c61d788bSJohannes Thumshirn 		return -EINVAL;
1086c61d788bSJohannes Thumshirn 
1087c61d788bSJohannes Thumshirn 	subsys->ver = NVME_VS(major, minor, tertiary);
1088c61d788bSJohannes Thumshirn 
1089c61d788bSJohannes Thumshirn 	return count;
1090c61d788bSJohannes Thumshirn }
109187fd4cc1SNoam Gottlieb 
nvmet_subsys_attr_version_store(struct config_item * item,const char * page,size_t count)109287fd4cc1SNoam Gottlieb static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
109387fd4cc1SNoam Gottlieb 					       const char *page, size_t count)
109487fd4cc1SNoam Gottlieb {
109587fd4cc1SNoam Gottlieb 	struct nvmet_subsys *subsys = to_subsys(item);
109687fd4cc1SNoam Gottlieb 	ssize_t ret;
109787fd4cc1SNoam Gottlieb 
109887fd4cc1SNoam Gottlieb 	down_write(&nvmet_config_sem);
109987fd4cc1SNoam Gottlieb 	mutex_lock(&subsys->lock);
110087fd4cc1SNoam Gottlieb 	ret = nvmet_subsys_attr_version_store_locked(subsys, page, count);
110187fd4cc1SNoam Gottlieb 	mutex_unlock(&subsys->lock);
110287fd4cc1SNoam Gottlieb 	up_write(&nvmet_config_sem);
110387fd4cc1SNoam Gottlieb 
110487fd4cc1SNoam Gottlieb 	return ret;
110587fd4cc1SNoam Gottlieb }
110641528f80SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_version);
1107c61d788bSJohannes Thumshirn 
1108e13b0615SNoam Gottlieb /* See Section 1.5 of NVMe 1.4 */
nvmet_is_ascii(const char c)1109e13b0615SNoam Gottlieb static bool nvmet_is_ascii(const char c)
1110e13b0615SNoam Gottlieb {
1111e13b0615SNoam Gottlieb 	return c >= 0x20 && c <= 0x7e;
1112e13b0615SNoam Gottlieb }
1113e13b0615SNoam Gottlieb 
nvmet_subsys_attr_serial_show(struct config_item * item,char * page)1114fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
1115fcbc5459SJohannes Thumshirn 					     char *page)
1116fcbc5459SJohannes Thumshirn {
1117fcbc5459SJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
1118fcbc5459SJohannes Thumshirn 
11190bd46e22SDan Carpenter 	return snprintf(page, PAGE_SIZE, "%.*s\n",
1120f0406481SHannes Reinecke 			NVMET_SN_MAX_SIZE, subsys->serial);
1121fcbc5459SJohannes Thumshirn }
1122fcbc5459SJohannes Thumshirn 
11237ae023c5SNoam Gottlieb static ssize_t
nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys * subsys,const char * page,size_t count)11247ae023c5SNoam Gottlieb nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
1125fcbc5459SJohannes Thumshirn 		const char *page, size_t count)
1126fcbc5459SJohannes Thumshirn {
1127e13b0615SNoam Gottlieb 	int pos, len = strcspn(page, "\n");
1128d3a9b0caSChaitanya Kulkarni 
11297ae023c5SNoam Gottlieb 	if (subsys->subsys_discovered) {
11307ae023c5SNoam Gottlieb 		pr_err("Can't set serial number. %s is already assigned\n",
11317ae023c5SNoam Gottlieb 		       subsys->serial);
11327ae023c5SNoam Gottlieb 		return -EINVAL;
11337ae023c5SNoam Gottlieb 	}
11347ae023c5SNoam Gottlieb 
1135e13b0615SNoam Gottlieb 	if (!len || len > NVMET_SN_MAX_SIZE) {
1136e13b0615SNoam Gottlieb 		pr_err("Serial Number can not be empty or exceed %d Bytes\n",
1137e13b0615SNoam Gottlieb 		       NVMET_SN_MAX_SIZE);
1138d3a9b0caSChaitanya Kulkarni 		return -EINVAL;
1139e13b0615SNoam Gottlieb 	}
1140e13b0615SNoam Gottlieb 
1141e13b0615SNoam Gottlieb 	for (pos = 0; pos < len; pos++) {
1142e13b0615SNoam Gottlieb 		if (!nvmet_is_ascii(page[pos])) {
1143e13b0615SNoam Gottlieb 			pr_err("Serial Number must contain only ASCII strings\n");
1144e13b0615SNoam Gottlieb 			return -EINVAL;
1145e13b0615SNoam Gottlieb 		}
1146e13b0615SNoam Gottlieb 	}
1147fcbc5459SJohannes Thumshirn 
11487ae023c5SNoam Gottlieb 	memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
11497ae023c5SNoam Gottlieb 
11507ae023c5SNoam Gottlieb 	return count;
11517ae023c5SNoam Gottlieb }
11527ae023c5SNoam Gottlieb 
nvmet_subsys_attr_serial_store(struct config_item * item,const char * page,size_t count)11537ae023c5SNoam Gottlieb static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
11547ae023c5SNoam Gottlieb 					      const char *page, size_t count)
11557ae023c5SNoam Gottlieb {
11567ae023c5SNoam Gottlieb 	struct nvmet_subsys *subsys = to_subsys(item);
11577ae023c5SNoam Gottlieb 	ssize_t ret;
11587ae023c5SNoam Gottlieb 
1159fcbc5459SJohannes Thumshirn 	down_write(&nvmet_config_sem);
1160e13b0615SNoam Gottlieb 	mutex_lock(&subsys->lock);
11617ae023c5SNoam Gottlieb 	ret = nvmet_subsys_attr_serial_store_locked(subsys, page, count);
1162e13b0615SNoam Gottlieb 	mutex_unlock(&subsys->lock);
1163fcbc5459SJohannes Thumshirn 	up_write(&nvmet_config_sem);
1164fcbc5459SJohannes Thumshirn 
11657ae023c5SNoam Gottlieb 	return ret;
1166fcbc5459SJohannes Thumshirn }
1167fcbc5459SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
1168fcbc5459SJohannes Thumshirn 
nvmet_subsys_attr_cntlid_min_show(struct config_item * item,char * page)116994a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
117094a39d61SChaitanya Kulkarni 						 char *page)
117194a39d61SChaitanya Kulkarni {
117294a39d61SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
117394a39d61SChaitanya Kulkarni }
117494a39d61SChaitanya Kulkarni 
nvmet_subsys_attr_cntlid_min_store(struct config_item * item,const char * page,size_t cnt)117594a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
117694a39d61SChaitanya Kulkarni 						  const char *page, size_t cnt)
117794a39d61SChaitanya Kulkarni {
117894a39d61SChaitanya Kulkarni 	u16 cntlid_min;
117994a39d61SChaitanya Kulkarni 
118094a39d61SChaitanya Kulkarni 	if (sscanf(page, "%hu\n", &cntlid_min) != 1)
118194a39d61SChaitanya Kulkarni 		return -EINVAL;
118294a39d61SChaitanya Kulkarni 
118394a39d61SChaitanya Kulkarni 	if (cntlid_min == 0)
118494a39d61SChaitanya Kulkarni 		return -EINVAL;
118594a39d61SChaitanya Kulkarni 
118694a39d61SChaitanya Kulkarni 	down_write(&nvmet_config_sem);
118794a39d61SChaitanya Kulkarni 	if (cntlid_min >= to_subsys(item)->cntlid_max)
118894a39d61SChaitanya Kulkarni 		goto out_unlock;
118994a39d61SChaitanya Kulkarni 	to_subsys(item)->cntlid_min = cntlid_min;
119094a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
119194a39d61SChaitanya Kulkarni 	return cnt;
119294a39d61SChaitanya Kulkarni 
119394a39d61SChaitanya Kulkarni out_unlock:
119494a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
119594a39d61SChaitanya Kulkarni 	return -EINVAL;
119694a39d61SChaitanya Kulkarni }
119794a39d61SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
119894a39d61SChaitanya Kulkarni 
nvmet_subsys_attr_cntlid_max_show(struct config_item * item,char * page)119994a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
120094a39d61SChaitanya Kulkarni 						 char *page)
120194a39d61SChaitanya Kulkarni {
120294a39d61SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
120394a39d61SChaitanya Kulkarni }
120494a39d61SChaitanya Kulkarni 
nvmet_subsys_attr_cntlid_max_store(struct config_item * item,const char * page,size_t cnt)120594a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
120694a39d61SChaitanya Kulkarni 						  const char *page, size_t cnt)
120794a39d61SChaitanya Kulkarni {
120894a39d61SChaitanya Kulkarni 	u16 cntlid_max;
120994a39d61SChaitanya Kulkarni 
121094a39d61SChaitanya Kulkarni 	if (sscanf(page, "%hu\n", &cntlid_max) != 1)
121194a39d61SChaitanya Kulkarni 		return -EINVAL;
121294a39d61SChaitanya Kulkarni 
121394a39d61SChaitanya Kulkarni 	if (cntlid_max == 0)
121494a39d61SChaitanya Kulkarni 		return -EINVAL;
121594a39d61SChaitanya Kulkarni 
121694a39d61SChaitanya Kulkarni 	down_write(&nvmet_config_sem);
121794a39d61SChaitanya Kulkarni 	if (cntlid_max <= to_subsys(item)->cntlid_min)
121894a39d61SChaitanya Kulkarni 		goto out_unlock;
121994a39d61SChaitanya Kulkarni 	to_subsys(item)->cntlid_max = cntlid_max;
122094a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
122194a39d61SChaitanya Kulkarni 	return cnt;
122294a39d61SChaitanya Kulkarni 
122394a39d61SChaitanya Kulkarni out_unlock:
122494a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
122594a39d61SChaitanya Kulkarni 	return -EINVAL;
122694a39d61SChaitanya Kulkarni }
122794a39d61SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
122894a39d61SChaitanya Kulkarni 
nvmet_subsys_attr_model_show(struct config_item * item,char * page)1229013b7ebeSMark Ruijter static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
1230013b7ebeSMark Ruijter 					    char *page)
1231013b7ebeSMark Ruijter {
1232013b7ebeSMark Ruijter 	struct nvmet_subsys *subsys = to_subsys(item);
1233013b7ebeSMark Ruijter 
12340d148efdSNoam Gottlieb 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->model_number);
1235013b7ebeSMark Ruijter }
1236013b7ebeSMark Ruijter 
nvmet_subsys_attr_model_store_locked(struct nvmet_subsys * subsys,const char * page,size_t count)1237d9f273b7SMax Gurtovoy static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
1238013b7ebeSMark Ruijter 		const char *page, size_t count)
1239013b7ebeSMark Ruijter {
1240013b7ebeSMark Ruijter 	int pos = 0, len;
1241becc4cacSAleksandr Miloserdov 	char *val;
1242013b7ebeSMark Ruijter 
12430d148efdSNoam Gottlieb 	if (subsys->subsys_discovered) {
1244d9f273b7SMax Gurtovoy 		pr_err("Can't set model number. %s is already assigned\n",
1245d9f273b7SMax Gurtovoy 		       subsys->model_number);
1246d9f273b7SMax Gurtovoy 		return -EINVAL;
1247d9f273b7SMax Gurtovoy 	}
1248d9f273b7SMax Gurtovoy 
1249013b7ebeSMark Ruijter 	len = strcspn(page, "\n");
1250013b7ebeSMark Ruijter 	if (!len)
1251013b7ebeSMark Ruijter 		return -EINVAL;
1252013b7ebeSMark Ruijter 
125348b4c010SNoam Gottlieb 	if (len > NVMET_MN_MAX_SIZE) {
1254ccc1003bSColin Ian King 		pr_err("Model number size can not exceed %d Bytes\n",
125548b4c010SNoam Gottlieb 		       NVMET_MN_MAX_SIZE);
125648b4c010SNoam Gottlieb 		return -EINVAL;
125748b4c010SNoam Gottlieb 	}
125848b4c010SNoam Gottlieb 
1259013b7ebeSMark Ruijter 	for (pos = 0; pos < len; pos++) {
1260013b7ebeSMark Ruijter 		if (!nvmet_is_ascii(page[pos]))
1261013b7ebeSMark Ruijter 			return -EINVAL;
1262013b7ebeSMark Ruijter 	}
1263013b7ebeSMark Ruijter 
1264becc4cacSAleksandr Miloserdov 	val = kmemdup_nul(page, len, GFP_KERNEL);
1265becc4cacSAleksandr Miloserdov 	if (!val)
1266013b7ebeSMark Ruijter 		return -ENOMEM;
1267becc4cacSAleksandr Miloserdov 	kfree(subsys->model_number);
1268becc4cacSAleksandr Miloserdov 	subsys->model_number = val;
1269d9f273b7SMax Gurtovoy 	return count;
1270013b7ebeSMark Ruijter }
1271d9f273b7SMax Gurtovoy 
nvmet_subsys_attr_model_store(struct config_item * item,const char * page,size_t count)1272d9f273b7SMax Gurtovoy static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
1273d9f273b7SMax Gurtovoy 					     const char *page, size_t count)
1274d9f273b7SMax Gurtovoy {
1275d9f273b7SMax Gurtovoy 	struct nvmet_subsys *subsys = to_subsys(item);
1276d9f273b7SMax Gurtovoy 	ssize_t ret;
1277013b7ebeSMark Ruijter 
1278013b7ebeSMark Ruijter 	down_write(&nvmet_config_sem);
1279013b7ebeSMark Ruijter 	mutex_lock(&subsys->lock);
1280d9f273b7SMax Gurtovoy 	ret = nvmet_subsys_attr_model_store_locked(subsys, page, count);
1281013b7ebeSMark Ruijter 	mutex_unlock(&subsys->lock);
1282013b7ebeSMark Ruijter 	up_write(&nvmet_config_sem);
1283013b7ebeSMark Ruijter 
1284d9f273b7SMax Gurtovoy 	return ret;
1285013b7ebeSMark Ruijter }
1286013b7ebeSMark Ruijter CONFIGFS_ATTR(nvmet_subsys_, attr_model);
1287013b7ebeSMark Ruijter 
nvmet_subsys_attr_ieee_oui_show(struct config_item * item,char * page)128823855abdSAleksandr Miloserdov static ssize_t nvmet_subsys_attr_ieee_oui_show(struct config_item *item,
128923855abdSAleksandr Miloserdov 					    char *page)
129023855abdSAleksandr Miloserdov {
129123855abdSAleksandr Miloserdov 	struct nvmet_subsys *subsys = to_subsys(item);
129223855abdSAleksandr Miloserdov 
129323855abdSAleksandr Miloserdov 	return sysfs_emit(page, "0x%06x\n", subsys->ieee_oui);
129423855abdSAleksandr Miloserdov }
129523855abdSAleksandr Miloserdov 
nvmet_subsys_attr_ieee_oui_store_locked(struct nvmet_subsys * subsys,const char * page,size_t count)129623855abdSAleksandr Miloserdov static ssize_t nvmet_subsys_attr_ieee_oui_store_locked(struct nvmet_subsys *subsys,
129723855abdSAleksandr Miloserdov 		const char *page, size_t count)
129823855abdSAleksandr Miloserdov {
129923855abdSAleksandr Miloserdov 	uint32_t val = 0;
130023855abdSAleksandr Miloserdov 	int ret;
130123855abdSAleksandr Miloserdov 
130223855abdSAleksandr Miloserdov 	if (subsys->subsys_discovered) {
130323855abdSAleksandr Miloserdov 		pr_err("Can't set IEEE OUI. 0x%06x is already assigned\n",
130423855abdSAleksandr Miloserdov 		      subsys->ieee_oui);
130523855abdSAleksandr Miloserdov 		return -EINVAL;
130623855abdSAleksandr Miloserdov 	}
130723855abdSAleksandr Miloserdov 
130823855abdSAleksandr Miloserdov 	ret = kstrtou32(page, 0, &val);
130923855abdSAleksandr Miloserdov 	if (ret < 0)
131023855abdSAleksandr Miloserdov 		return ret;
131123855abdSAleksandr Miloserdov 
131223855abdSAleksandr Miloserdov 	if (val >= 0x1000000)
131323855abdSAleksandr Miloserdov 		return -EINVAL;
131423855abdSAleksandr Miloserdov 
131523855abdSAleksandr Miloserdov 	subsys->ieee_oui = val;
131623855abdSAleksandr Miloserdov 
131723855abdSAleksandr Miloserdov 	return count;
131823855abdSAleksandr Miloserdov }
131923855abdSAleksandr Miloserdov 
nvmet_subsys_attr_ieee_oui_store(struct config_item * item,const char * page,size_t count)132023855abdSAleksandr Miloserdov static ssize_t nvmet_subsys_attr_ieee_oui_store(struct config_item *item,
132123855abdSAleksandr Miloserdov 					     const char *page, size_t count)
132223855abdSAleksandr Miloserdov {
132323855abdSAleksandr Miloserdov 	struct nvmet_subsys *subsys = to_subsys(item);
132423855abdSAleksandr Miloserdov 	ssize_t ret;
132523855abdSAleksandr Miloserdov 
132623855abdSAleksandr Miloserdov 	down_write(&nvmet_config_sem);
132723855abdSAleksandr Miloserdov 	mutex_lock(&subsys->lock);
132823855abdSAleksandr Miloserdov 	ret = nvmet_subsys_attr_ieee_oui_store_locked(subsys, page, count);
132923855abdSAleksandr Miloserdov 	mutex_unlock(&subsys->lock);
133023855abdSAleksandr Miloserdov 	up_write(&nvmet_config_sem);
133123855abdSAleksandr Miloserdov 
133223855abdSAleksandr Miloserdov 	return ret;
133323855abdSAleksandr Miloserdov }
133423855abdSAleksandr Miloserdov CONFIGFS_ATTR(nvmet_subsys_, attr_ieee_oui);
133523855abdSAleksandr Miloserdov 
nvmet_subsys_attr_firmware_show(struct config_item * item,char * page)133668c5444cSAleksandr Miloserdov static ssize_t nvmet_subsys_attr_firmware_show(struct config_item *item,
133768c5444cSAleksandr Miloserdov 					    char *page)
133868c5444cSAleksandr Miloserdov {
133968c5444cSAleksandr Miloserdov 	struct nvmet_subsys *subsys = to_subsys(item);
134068c5444cSAleksandr Miloserdov 
134168c5444cSAleksandr Miloserdov 	return sysfs_emit(page, "%s\n", subsys->firmware_rev);
134268c5444cSAleksandr Miloserdov }
134368c5444cSAleksandr Miloserdov 
nvmet_subsys_attr_firmware_store_locked(struct nvmet_subsys * subsys,const char * page,size_t count)134468c5444cSAleksandr Miloserdov static ssize_t nvmet_subsys_attr_firmware_store_locked(struct nvmet_subsys *subsys,
134568c5444cSAleksandr Miloserdov 		const char *page, size_t count)
134668c5444cSAleksandr Miloserdov {
134768c5444cSAleksandr Miloserdov 	int pos = 0, len;
134868c5444cSAleksandr Miloserdov 	char *val;
134968c5444cSAleksandr Miloserdov 
135068c5444cSAleksandr Miloserdov 	if (subsys->subsys_discovered) {
135168c5444cSAleksandr Miloserdov 		pr_err("Can't set firmware revision. %s is already assigned\n",
135268c5444cSAleksandr Miloserdov 		       subsys->firmware_rev);
135368c5444cSAleksandr Miloserdov 		return -EINVAL;
135468c5444cSAleksandr Miloserdov 	}
135568c5444cSAleksandr Miloserdov 
135668c5444cSAleksandr Miloserdov 	len = strcspn(page, "\n");
135768c5444cSAleksandr Miloserdov 	if (!len)
135868c5444cSAleksandr Miloserdov 		return -EINVAL;
135968c5444cSAleksandr Miloserdov 
136068c5444cSAleksandr Miloserdov 	if (len > NVMET_FR_MAX_SIZE) {
136168c5444cSAleksandr Miloserdov 		pr_err("Firmware revision size can not exceed %d Bytes\n",
136268c5444cSAleksandr Miloserdov 		       NVMET_FR_MAX_SIZE);
136368c5444cSAleksandr Miloserdov 		return -EINVAL;
136468c5444cSAleksandr Miloserdov 	}
136568c5444cSAleksandr Miloserdov 
136668c5444cSAleksandr Miloserdov 	for (pos = 0; pos < len; pos++) {
136768c5444cSAleksandr Miloserdov 		if (!nvmet_is_ascii(page[pos]))
136868c5444cSAleksandr Miloserdov 			return -EINVAL;
136968c5444cSAleksandr Miloserdov 	}
137068c5444cSAleksandr Miloserdov 
137168c5444cSAleksandr Miloserdov 	val = kmemdup_nul(page, len, GFP_KERNEL);
137268c5444cSAleksandr Miloserdov 	if (!val)
137368c5444cSAleksandr Miloserdov 		return -ENOMEM;
137468c5444cSAleksandr Miloserdov 
137568c5444cSAleksandr Miloserdov 	kfree(subsys->firmware_rev);
137668c5444cSAleksandr Miloserdov 
137768c5444cSAleksandr Miloserdov 	subsys->firmware_rev = val;
137868c5444cSAleksandr Miloserdov 
137968c5444cSAleksandr Miloserdov 	return count;
138068c5444cSAleksandr Miloserdov }
138168c5444cSAleksandr Miloserdov 
nvmet_subsys_attr_firmware_store(struct config_item * item,const char * page,size_t count)138268c5444cSAleksandr Miloserdov static ssize_t nvmet_subsys_attr_firmware_store(struct config_item *item,
138368c5444cSAleksandr Miloserdov 					     const char *page, size_t count)
138468c5444cSAleksandr Miloserdov {
138568c5444cSAleksandr Miloserdov 	struct nvmet_subsys *subsys = to_subsys(item);
138668c5444cSAleksandr Miloserdov 	ssize_t ret;
138768c5444cSAleksandr Miloserdov 
138868c5444cSAleksandr Miloserdov 	down_write(&nvmet_config_sem);
138968c5444cSAleksandr Miloserdov 	mutex_lock(&subsys->lock);
139068c5444cSAleksandr Miloserdov 	ret = nvmet_subsys_attr_firmware_store_locked(subsys, page, count);
139168c5444cSAleksandr Miloserdov 	mutex_unlock(&subsys->lock);
139268c5444cSAleksandr Miloserdov 	up_write(&nvmet_config_sem);
139368c5444cSAleksandr Miloserdov 
139468c5444cSAleksandr Miloserdov 	return ret;
139568c5444cSAleksandr Miloserdov }
139668c5444cSAleksandr Miloserdov CONFIGFS_ATTR(nvmet_subsys_, attr_firmware);
139768c5444cSAleksandr Miloserdov 
1398ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
nvmet_subsys_attr_pi_enable_show(struct config_item * item,char * page)1399ea52ac1cSIsrael Rukshin static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
1400ea52ac1cSIsrael Rukshin 						char *page)
1401ea52ac1cSIsrael Rukshin {
1402ea52ac1cSIsrael Rukshin 	return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
1403ea52ac1cSIsrael Rukshin }
1404ea52ac1cSIsrael Rukshin 
nvmet_subsys_attr_pi_enable_store(struct config_item * item,const char * page,size_t count)1405ea52ac1cSIsrael Rukshin static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
1406ea52ac1cSIsrael Rukshin 						 const char *page, size_t count)
1407ea52ac1cSIsrael Rukshin {
1408ea52ac1cSIsrael Rukshin 	struct nvmet_subsys *subsys = to_subsys(item);
1409ea52ac1cSIsrael Rukshin 	bool pi_enable;
1410ea52ac1cSIsrael Rukshin 
141199722c8aSChristophe JAILLET 	if (kstrtobool(page, &pi_enable))
1412ea52ac1cSIsrael Rukshin 		return -EINVAL;
1413ea52ac1cSIsrael Rukshin 
1414ea52ac1cSIsrael Rukshin 	subsys->pi_support = pi_enable;
1415ea52ac1cSIsrael Rukshin 	return count;
1416ea52ac1cSIsrael Rukshin }
1417ea52ac1cSIsrael Rukshin CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
1418ea52ac1cSIsrael Rukshin #endif
1419ea52ac1cSIsrael Rukshin 
nvmet_subsys_attr_qid_max_show(struct config_item * item,char * page)14203e980f59SDaniel Wagner static ssize_t nvmet_subsys_attr_qid_max_show(struct config_item *item,
14213e980f59SDaniel Wagner 					      char *page)
14223e980f59SDaniel Wagner {
14233e980f59SDaniel Wagner 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->max_qid);
14243e980f59SDaniel Wagner }
14253e980f59SDaniel Wagner 
nvmet_subsys_attr_qid_max_store(struct config_item * item,const char * page,size_t cnt)14263e980f59SDaniel Wagner static ssize_t nvmet_subsys_attr_qid_max_store(struct config_item *item,
14273e980f59SDaniel Wagner 					       const char *page, size_t cnt)
14283e980f59SDaniel Wagner {
14292be2cd52SDaniel Wagner 	struct nvmet_subsys *subsys = to_subsys(item);
14302be2cd52SDaniel Wagner 	struct nvmet_ctrl *ctrl;
14313e980f59SDaniel Wagner 	u16 qid_max;
14323e980f59SDaniel Wagner 
14333e980f59SDaniel Wagner 	if (sscanf(page, "%hu\n", &qid_max) != 1)
14343e980f59SDaniel Wagner 		return -EINVAL;
14353e980f59SDaniel Wagner 
14363e980f59SDaniel Wagner 	if (qid_max < 1 || qid_max > NVMET_NR_QUEUES)
14373e980f59SDaniel Wagner 		return -EINVAL;
14383e980f59SDaniel Wagner 
14393e980f59SDaniel Wagner 	down_write(&nvmet_config_sem);
14402be2cd52SDaniel Wagner 	subsys->max_qid = qid_max;
14412be2cd52SDaniel Wagner 
14422be2cd52SDaniel Wagner 	/* Force reconnect */
14432be2cd52SDaniel Wagner 	list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
14442be2cd52SDaniel Wagner 		ctrl->ops->delete_ctrl(ctrl);
14453e980f59SDaniel Wagner 	up_write(&nvmet_config_sem);
14462be2cd52SDaniel Wagner 
14473e980f59SDaniel Wagner 	return cnt;
14483e980f59SDaniel Wagner }
14493e980f59SDaniel Wagner CONFIGFS_ATTR(nvmet_subsys_, attr_qid_max);
14503e980f59SDaniel Wagner 
1451a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_subsys_attrs[] = {
1452a07b4970SChristoph Hellwig 	&nvmet_subsys_attr_attr_allow_any_host,
145341528f80SJohannes Thumshirn 	&nvmet_subsys_attr_attr_version,
1454fcbc5459SJohannes Thumshirn 	&nvmet_subsys_attr_attr_serial,
145594a39d61SChaitanya Kulkarni 	&nvmet_subsys_attr_attr_cntlid_min,
145694a39d61SChaitanya Kulkarni 	&nvmet_subsys_attr_attr_cntlid_max,
1457013b7ebeSMark Ruijter 	&nvmet_subsys_attr_attr_model,
14583e980f59SDaniel Wagner 	&nvmet_subsys_attr_attr_qid_max,
145923855abdSAleksandr Miloserdov 	&nvmet_subsys_attr_attr_ieee_oui,
146068c5444cSAleksandr Miloserdov 	&nvmet_subsys_attr_attr_firmware,
1461ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1462ea52ac1cSIsrael Rukshin 	&nvmet_subsys_attr_attr_pi_enable,
1463ea52ac1cSIsrael Rukshin #endif
1464a07b4970SChristoph Hellwig 	NULL,
1465a07b4970SChristoph Hellwig };
1466a07b4970SChristoph Hellwig 
1467a07b4970SChristoph Hellwig /*
1468a07b4970SChristoph Hellwig  * Subsystem structures & folder operation functions below
1469a07b4970SChristoph Hellwig  */
nvmet_subsys_release(struct config_item * item)1470a07b4970SChristoph Hellwig static void nvmet_subsys_release(struct config_item *item)
1471a07b4970SChristoph Hellwig {
1472a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(item);
1473a07b4970SChristoph Hellwig 
1474344770b0SSagi Grimberg 	nvmet_subsys_del_ctrls(subsys);
1475a07b4970SChristoph Hellwig 	nvmet_subsys_put(subsys);
1476a07b4970SChristoph Hellwig }
1477a07b4970SChristoph Hellwig 
1478a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_subsys_item_ops = {
1479a07b4970SChristoph Hellwig 	.release		= nvmet_subsys_release,
1480a07b4970SChristoph Hellwig };
1481a07b4970SChristoph Hellwig 
148266603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type = {
1483a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_subsys_item_ops,
1484a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_subsys_attrs,
1485a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1486a07b4970SChristoph Hellwig };
1487a07b4970SChristoph Hellwig 
nvmet_subsys_make(struct config_group * group,const char * name)1488a07b4970SChristoph Hellwig static struct config_group *nvmet_subsys_make(struct config_group *group,
1489a07b4970SChristoph Hellwig 		const char *name)
1490a07b4970SChristoph Hellwig {
1491a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys;
1492a07b4970SChristoph Hellwig 
1493a07b4970SChristoph Hellwig 	if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1494a07b4970SChristoph Hellwig 		pr_err("can't create discovery subsystem through configfs\n");
1495a07b4970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1496a07b4970SChristoph Hellwig 	}
1497a07b4970SChristoph Hellwig 
1498a07b4970SChristoph Hellwig 	subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
14996b7e631bSMinwoo Im 	if (IS_ERR(subsys))
15006b7e631bSMinwoo Im 		return ERR_CAST(subsys);
1501a07b4970SChristoph Hellwig 
1502a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1503a07b4970SChristoph Hellwig 
1504a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->namespaces_group,
1505a07b4970SChristoph Hellwig 			"namespaces", &nvmet_namespaces_type);
1506a07b4970SChristoph Hellwig 	configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1507a07b4970SChristoph Hellwig 
1508a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->allowed_hosts_group,
1509a07b4970SChristoph Hellwig 			"allowed_hosts", &nvmet_allowed_hosts_type);
1510a07b4970SChristoph Hellwig 	configfs_add_default_group(&subsys->allowed_hosts_group,
1511a07b4970SChristoph Hellwig 			&subsys->group);
1512a07b4970SChristoph Hellwig 
1513cae5b01aSLogan Gunthorpe 	nvmet_add_passthru_group(subsys);
1514cae5b01aSLogan Gunthorpe 
1515a07b4970SChristoph Hellwig 	return &subsys->group;
1516a07b4970SChristoph Hellwig }
1517a07b4970SChristoph Hellwig 
1518a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_subsystems_group_ops = {
1519a07b4970SChristoph Hellwig 	.make_group		= nvmet_subsys_make,
1520a07b4970SChristoph Hellwig };
1521a07b4970SChristoph Hellwig 
152266603a31SBhumika Goyal static const struct config_item_type nvmet_subsystems_type = {
1523a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_subsystems_group_ops,
1524a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1525a07b4970SChristoph Hellwig };
1526a07b4970SChristoph Hellwig 
nvmet_referral_enable_show(struct config_item * item,char * page)1527a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_show(struct config_item *item,
1528a07b4970SChristoph Hellwig 		char *page)
1529a07b4970SChristoph Hellwig {
1530a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1531a07b4970SChristoph Hellwig }
1532a07b4970SChristoph Hellwig 
nvmet_referral_enable_store(struct config_item * item,const char * page,size_t count)1533a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_store(struct config_item *item,
1534a07b4970SChristoph Hellwig 		const char *page, size_t count)
1535a07b4970SChristoph Hellwig {
1536a07b4970SChristoph Hellwig 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1537a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1538a07b4970SChristoph Hellwig 	bool enable;
1539a07b4970SChristoph Hellwig 
154099722c8aSChristophe JAILLET 	if (kstrtobool(page, &enable))
1541a07b4970SChristoph Hellwig 		goto inval;
1542a07b4970SChristoph Hellwig 
1543a07b4970SChristoph Hellwig 	if (enable)
1544a07b4970SChristoph Hellwig 		nvmet_referral_enable(parent, port);
1545a07b4970SChristoph Hellwig 	else
1546b662a078SJay Sternberg 		nvmet_referral_disable(parent, port);
1547a07b4970SChristoph Hellwig 
1548a07b4970SChristoph Hellwig 	return count;
1549a07b4970SChristoph Hellwig inval:
1550a07b4970SChristoph Hellwig 	pr_err("Invalid value '%s' for enable\n", page);
1551a07b4970SChristoph Hellwig 	return -EINVAL;
1552a07b4970SChristoph Hellwig }
1553a07b4970SChristoph Hellwig 
1554a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_referral_, enable);
1555a07b4970SChristoph Hellwig 
1556a07b4970SChristoph Hellwig /*
1557a07b4970SChristoph Hellwig  * Discovery Service subsystem definitions
1558a07b4970SChristoph Hellwig  */
1559a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_referral_attrs[] = {
1560a07b4970SChristoph Hellwig 	&nvmet_attr_addr_adrfam,
1561a07b4970SChristoph Hellwig 	&nvmet_attr_addr_portid,
1562a07b4970SChristoph Hellwig 	&nvmet_attr_addr_treq,
1563a07b4970SChristoph Hellwig 	&nvmet_attr_addr_traddr,
1564a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trsvcid,
1565a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trtype,
1566a07b4970SChristoph Hellwig 	&nvmet_referral_attr_enable,
1567a07b4970SChristoph Hellwig 	NULL,
1568a07b4970SChristoph Hellwig };
1569a07b4970SChristoph Hellwig 
nvmet_referral_notify(struct config_group * group,struct config_item * item)1570f0e656e4SSagi Grimberg static void nvmet_referral_notify(struct config_group *group,
1571f0e656e4SSagi Grimberg 		struct config_item *item)
1572a07b4970SChristoph Hellwig {
1573b662a078SJay Sternberg 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1574a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1575a07b4970SChristoph Hellwig 
1576b662a078SJay Sternberg 	nvmet_referral_disable(parent, port);
1577f0e656e4SSagi Grimberg }
1578f0e656e4SSagi Grimberg 
nvmet_referral_release(struct config_item * item)1579f0e656e4SSagi Grimberg static void nvmet_referral_release(struct config_item *item)
1580f0e656e4SSagi Grimberg {
1581f0e656e4SSagi Grimberg 	struct nvmet_port *port = to_nvmet_port(item);
1582f0e656e4SSagi Grimberg 
1583a07b4970SChristoph Hellwig 	kfree(port);
1584a07b4970SChristoph Hellwig }
1585a07b4970SChristoph Hellwig 
1586a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_referral_item_ops = {
1587a07b4970SChristoph Hellwig 	.release	= nvmet_referral_release,
1588a07b4970SChristoph Hellwig };
1589a07b4970SChristoph Hellwig 
159066603a31SBhumika Goyal static const struct config_item_type nvmet_referral_type = {
1591a07b4970SChristoph Hellwig 	.ct_owner	= THIS_MODULE,
1592a07b4970SChristoph Hellwig 	.ct_attrs	= nvmet_referral_attrs,
1593a07b4970SChristoph Hellwig 	.ct_item_ops	= &nvmet_referral_item_ops,
1594a07b4970SChristoph Hellwig };
1595a07b4970SChristoph Hellwig 
nvmet_referral_make(struct config_group * group,const char * name)1596a07b4970SChristoph Hellwig static struct config_group *nvmet_referral_make(
1597a07b4970SChristoph Hellwig 		struct config_group *group, const char *name)
1598a07b4970SChristoph Hellwig {
1599a07b4970SChristoph Hellwig 	struct nvmet_port *port;
1600a07b4970SChristoph Hellwig 
1601a07b4970SChristoph Hellwig 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1602a07b4970SChristoph Hellwig 	if (!port)
1603f98d9ca1SDan Carpenter 		return ERR_PTR(-ENOMEM);
1604a07b4970SChristoph Hellwig 
1605a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->entry);
1606a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1607a07b4970SChristoph Hellwig 
1608a07b4970SChristoph Hellwig 	return &port->group;
1609a07b4970SChristoph Hellwig }
1610a07b4970SChristoph Hellwig 
1611a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_referral_group_ops = {
1612a07b4970SChristoph Hellwig 	.make_group		= nvmet_referral_make,
1613f0e656e4SSagi Grimberg 	.disconnect_notify	= nvmet_referral_notify,
1614a07b4970SChristoph Hellwig };
1615a07b4970SChristoph Hellwig 
161666603a31SBhumika Goyal static const struct config_item_type nvmet_referrals_type = {
1617a07b4970SChristoph Hellwig 	.ct_owner	= THIS_MODULE,
1618a07b4970SChristoph Hellwig 	.ct_group_ops	= &nvmet_referral_group_ops,
1619a07b4970SChristoph Hellwig };
1620a07b4970SChristoph Hellwig 
162174255969SChristoph Hellwig static struct nvmet_type_name_map nvmet_ana_state[] = {
162262ac0d32SChristoph Hellwig 	{ NVME_ANA_OPTIMIZED,		"optimized" },
162362ac0d32SChristoph Hellwig 	{ NVME_ANA_NONOPTIMIZED,	"non-optimized" },
162462ac0d32SChristoph Hellwig 	{ NVME_ANA_INACCESSIBLE,	"inaccessible" },
162562ac0d32SChristoph Hellwig 	{ NVME_ANA_PERSISTENT_LOSS,	"persistent-loss" },
162662ac0d32SChristoph Hellwig 	{ NVME_ANA_CHANGE,		"change" },
162762ac0d32SChristoph Hellwig };
162862ac0d32SChristoph Hellwig 
nvmet_ana_group_ana_state_show(struct config_item * item,char * page)162962ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
163062ac0d32SChristoph Hellwig 		char *page)
163162ac0d32SChristoph Hellwig {
163262ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
163362ac0d32SChristoph Hellwig 	enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
163462ac0d32SChristoph Hellwig 	int i;
163562ac0d32SChristoph Hellwig 
163684b8d0d7SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
163784b8d0d7SChaitanya Kulkarni 		if (state == nvmet_ana_state[i].type)
163884b8d0d7SChaitanya Kulkarni 			return sprintf(page, "%s\n", nvmet_ana_state[i].name);
163962ac0d32SChristoph Hellwig 	}
164062ac0d32SChristoph Hellwig 
164162ac0d32SChristoph Hellwig 	return sprintf(page, "\n");
164262ac0d32SChristoph Hellwig }
164362ac0d32SChristoph Hellwig 
nvmet_ana_group_ana_state_store(struct config_item * item,const char * page,size_t count)164462ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
164562ac0d32SChristoph Hellwig 		const char *page, size_t count)
164662ac0d32SChristoph Hellwig {
164762ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
164884b8d0d7SChaitanya Kulkarni 	enum nvme_ana_state *ana_state = grp->port->ana_state;
164962ac0d32SChristoph Hellwig 	int i;
165062ac0d32SChristoph Hellwig 
165184b8d0d7SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
165284b8d0d7SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_ana_state[i].name))
165362ac0d32SChristoph Hellwig 			goto found;
165462ac0d32SChristoph Hellwig 	}
165562ac0d32SChristoph Hellwig 
165662ac0d32SChristoph Hellwig 	pr_err("Invalid value '%s' for ana_state\n", page);
165762ac0d32SChristoph Hellwig 	return -EINVAL;
165862ac0d32SChristoph Hellwig 
165962ac0d32SChristoph Hellwig found:
166062ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
166184b8d0d7SChaitanya Kulkarni 	ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
166262ac0d32SChristoph Hellwig 	nvmet_ana_chgcnt++;
166362ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
166462ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
166562ac0d32SChristoph Hellwig 	return count;
166662ac0d32SChristoph Hellwig }
166762ac0d32SChristoph Hellwig 
166862ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
166962ac0d32SChristoph Hellwig 
167062ac0d32SChristoph Hellwig static struct configfs_attribute *nvmet_ana_group_attrs[] = {
167162ac0d32SChristoph Hellwig 	&nvmet_ana_group_attr_ana_state,
167262ac0d32SChristoph Hellwig 	NULL,
167362ac0d32SChristoph Hellwig };
167462ac0d32SChristoph Hellwig 
nvmet_ana_group_release(struct config_item * item)167562ac0d32SChristoph Hellwig static void nvmet_ana_group_release(struct config_item *item)
167662ac0d32SChristoph Hellwig {
167762ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
167862ac0d32SChristoph Hellwig 
167962ac0d32SChristoph Hellwig 	if (grp == &grp->port->ana_default_group)
168062ac0d32SChristoph Hellwig 		return;
168162ac0d32SChristoph Hellwig 
168262ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
168362ac0d32SChristoph Hellwig 	grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
168462ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[grp->grpid]--;
168562ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
168662ac0d32SChristoph Hellwig 
168762ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
168862ac0d32SChristoph Hellwig 	kfree(grp);
168962ac0d32SChristoph Hellwig }
169062ac0d32SChristoph Hellwig 
169162ac0d32SChristoph Hellwig static struct configfs_item_operations nvmet_ana_group_item_ops = {
169262ac0d32SChristoph Hellwig 	.release		= nvmet_ana_group_release,
169362ac0d32SChristoph Hellwig };
169462ac0d32SChristoph Hellwig 
169562ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_group_type = {
169662ac0d32SChristoph Hellwig 	.ct_item_ops		= &nvmet_ana_group_item_ops,
169762ac0d32SChristoph Hellwig 	.ct_attrs		= nvmet_ana_group_attrs,
169862ac0d32SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
169962ac0d32SChristoph Hellwig };
170062ac0d32SChristoph Hellwig 
nvmet_ana_groups_make_group(struct config_group * group,const char * name)170162ac0d32SChristoph Hellwig static struct config_group *nvmet_ana_groups_make_group(
170262ac0d32SChristoph Hellwig 		struct config_group *group, const char *name)
170362ac0d32SChristoph Hellwig {
170462ac0d32SChristoph Hellwig 	struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
170562ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp;
170662ac0d32SChristoph Hellwig 	u32 grpid;
170762ac0d32SChristoph Hellwig 	int ret;
170862ac0d32SChristoph Hellwig 
170962ac0d32SChristoph Hellwig 	ret = kstrtou32(name, 0, &grpid);
171062ac0d32SChristoph Hellwig 	if (ret)
171162ac0d32SChristoph Hellwig 		goto out;
171262ac0d32SChristoph Hellwig 
171362ac0d32SChristoph Hellwig 	ret = -EINVAL;
171462ac0d32SChristoph Hellwig 	if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
171562ac0d32SChristoph Hellwig 		goto out;
171662ac0d32SChristoph Hellwig 
171762ac0d32SChristoph Hellwig 	ret = -ENOMEM;
171862ac0d32SChristoph Hellwig 	grp = kzalloc(sizeof(*grp), GFP_KERNEL);
171962ac0d32SChristoph Hellwig 	if (!grp)
172062ac0d32SChristoph Hellwig 		goto out;
172162ac0d32SChristoph Hellwig 	grp->port = port;
172262ac0d32SChristoph Hellwig 	grp->grpid = grpid;
172362ac0d32SChristoph Hellwig 
172462ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
1725946fd64bSNitesh Shetty 	grpid = array_index_nospec(grpid, NVMET_MAX_ANAGRPS);
172662ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[grpid]++;
172762ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
172862ac0d32SChristoph Hellwig 
172962ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
173062ac0d32SChristoph Hellwig 
173162ac0d32SChristoph Hellwig 	config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
173262ac0d32SChristoph Hellwig 	return &grp->group;
173362ac0d32SChristoph Hellwig out:
173462ac0d32SChristoph Hellwig 	return ERR_PTR(ret);
173562ac0d32SChristoph Hellwig }
173662ac0d32SChristoph Hellwig 
173762ac0d32SChristoph Hellwig static struct configfs_group_operations nvmet_ana_groups_group_ops = {
173862ac0d32SChristoph Hellwig 	.make_group		= nvmet_ana_groups_make_group,
173962ac0d32SChristoph Hellwig };
174062ac0d32SChristoph Hellwig 
174162ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_groups_type = {
174262ac0d32SChristoph Hellwig 	.ct_group_ops		= &nvmet_ana_groups_group_ops,
174362ac0d32SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
174462ac0d32SChristoph Hellwig };
174562ac0d32SChristoph Hellwig 
1746a07b4970SChristoph Hellwig /*
1747a07b4970SChristoph Hellwig  * Ports definitions.
1748a07b4970SChristoph Hellwig  */
nvmet_port_release(struct config_item * item)1749a07b4970SChristoph Hellwig static void nvmet_port_release(struct config_item *item)
1750a07b4970SChristoph Hellwig {
1751a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1752a07b4970SChristoph Hellwig 
1753e3e19dccSIsrael Rukshin 	/* Let inflight controllers teardown complete */
17548832cf92SSagi Grimberg 	flush_workqueue(nvmet_wq);
1755b662a078SJay Sternberg 	list_del(&port->global_entry);
1756b662a078SJay Sternberg 
175772efd25dSChristoph Hellwig 	kfree(port->ana_state);
1758a07b4970SChristoph Hellwig 	kfree(port);
1759a07b4970SChristoph Hellwig }
1760a07b4970SChristoph Hellwig 
1761a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_port_attrs[] = {
1762a07b4970SChristoph Hellwig 	&nvmet_attr_addr_adrfam,
1763a07b4970SChristoph Hellwig 	&nvmet_attr_addr_treq,
1764a07b4970SChristoph Hellwig 	&nvmet_attr_addr_traddr,
1765a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trsvcid,
1766a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trtype,
17670d5ee2b2SSteve Wise 	&nvmet_attr_param_inline_data_size,
1768ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1769ea52ac1cSIsrael Rukshin 	&nvmet_attr_param_pi_enable,
1770ea52ac1cSIsrael Rukshin #endif
1771a07b4970SChristoph Hellwig 	NULL,
1772a07b4970SChristoph Hellwig };
1773a07b4970SChristoph Hellwig 
1774a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_item_ops = {
1775a07b4970SChristoph Hellwig 	.release		= nvmet_port_release,
1776a07b4970SChristoph Hellwig };
1777a07b4970SChristoph Hellwig 
177866603a31SBhumika Goyal static const struct config_item_type nvmet_port_type = {
1779a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_port_attrs,
1780a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_port_item_ops,
1781a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1782a07b4970SChristoph Hellwig };
1783a07b4970SChristoph Hellwig 
nvmet_ports_make(struct config_group * group,const char * name)1784a07b4970SChristoph Hellwig static struct config_group *nvmet_ports_make(struct config_group *group,
1785a07b4970SChristoph Hellwig 		const char *name)
1786a07b4970SChristoph Hellwig {
1787a07b4970SChristoph Hellwig 	struct nvmet_port *port;
1788a07b4970SChristoph Hellwig 	u16 portid;
178962ac0d32SChristoph Hellwig 	u32 i;
1790a07b4970SChristoph Hellwig 
1791a07b4970SChristoph Hellwig 	if (kstrtou16(name, 0, &portid))
1792a07b4970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1793a07b4970SChristoph Hellwig 
1794a07b4970SChristoph Hellwig 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1795a07b4970SChristoph Hellwig 	if (!port)
1796f98d9ca1SDan Carpenter 		return ERR_PTR(-ENOMEM);
1797a07b4970SChristoph Hellwig 
179872efd25dSChristoph Hellwig 	port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
179972efd25dSChristoph Hellwig 			sizeof(*port->ana_state), GFP_KERNEL);
180072efd25dSChristoph Hellwig 	if (!port->ana_state) {
180172efd25dSChristoph Hellwig 		kfree(port);
180272efd25dSChristoph Hellwig 		return ERR_PTR(-ENOMEM);
180372efd25dSChristoph Hellwig 	}
180472efd25dSChristoph Hellwig 
180562ac0d32SChristoph Hellwig 	for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
180662ac0d32SChristoph Hellwig 		if (i == NVMET_DEFAULT_ANA_GRPID)
180762ac0d32SChristoph Hellwig 			port->ana_state[1] = NVME_ANA_OPTIMIZED;
180862ac0d32SChristoph Hellwig 		else
180962ac0d32SChristoph Hellwig 			port->ana_state[i] = NVME_ANA_INACCESSIBLE;
181062ac0d32SChristoph Hellwig 	}
181172efd25dSChristoph Hellwig 
1812b662a078SJay Sternberg 	list_add(&port->global_entry, &nvmet_ports_list);
1813b662a078SJay Sternberg 
1814a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->entry);
1815a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->subsystems);
1816a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->referrals);
18170d5ee2b2SSteve Wise 	port->inline_data_size = -1;	/* < 0 == let the transport choose */
1818a07b4970SChristoph Hellwig 
1819a07b4970SChristoph Hellwig 	port->disc_addr.portid = cpu_to_le16(portid);
1820d02abd19SChaitanya Kulkarni 	port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
18219b95d2fbSSagi Grimberg 	port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1822a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->group, name, &nvmet_port_type);
1823a07b4970SChristoph Hellwig 
1824a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->subsys_group,
1825a07b4970SChristoph Hellwig 			"subsystems", &nvmet_port_subsys_type);
1826a07b4970SChristoph Hellwig 	configfs_add_default_group(&port->subsys_group, &port->group);
1827a07b4970SChristoph Hellwig 
1828a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->referrals_group,
1829a07b4970SChristoph Hellwig 			"referrals", &nvmet_referrals_type);
1830a07b4970SChristoph Hellwig 	configfs_add_default_group(&port->referrals_group, &port->group);
1831a07b4970SChristoph Hellwig 
183262ac0d32SChristoph Hellwig 	config_group_init_type_name(&port->ana_groups_group,
183362ac0d32SChristoph Hellwig 			"ana_groups", &nvmet_ana_groups_type);
183462ac0d32SChristoph Hellwig 	configfs_add_default_group(&port->ana_groups_group, &port->group);
183562ac0d32SChristoph Hellwig 
183662ac0d32SChristoph Hellwig 	port->ana_default_group.port = port;
183762ac0d32SChristoph Hellwig 	port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
183862ac0d32SChristoph Hellwig 	config_group_init_type_name(&port->ana_default_group.group,
183962ac0d32SChristoph Hellwig 			__stringify(NVMET_DEFAULT_ANA_GRPID),
184062ac0d32SChristoph Hellwig 			&nvmet_ana_group_type);
184162ac0d32SChristoph Hellwig 	configfs_add_default_group(&port->ana_default_group.group,
184262ac0d32SChristoph Hellwig 			&port->ana_groups_group);
184362ac0d32SChristoph Hellwig 
1844a07b4970SChristoph Hellwig 	return &port->group;
1845a07b4970SChristoph Hellwig }
1846a07b4970SChristoph Hellwig 
1847a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_ports_group_ops = {
1848a07b4970SChristoph Hellwig 	.make_group		= nvmet_ports_make,
1849a07b4970SChristoph Hellwig };
1850a07b4970SChristoph Hellwig 
185166603a31SBhumika Goyal static const struct config_item_type nvmet_ports_type = {
1852a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_ports_group_ops,
1853a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1854a07b4970SChristoph Hellwig };
1855a07b4970SChristoph Hellwig 
1856a07b4970SChristoph Hellwig static struct config_group nvmet_subsystems_group;
1857a07b4970SChristoph Hellwig static struct config_group nvmet_ports_group;
1858a07b4970SChristoph Hellwig 
1859db1312ddSHannes Reinecke #ifdef CONFIG_NVME_TARGET_AUTH
nvmet_host_dhchap_key_show(struct config_item * item,char * page)1860db1312ddSHannes Reinecke static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
1861db1312ddSHannes Reinecke 		char *page)
1862db1312ddSHannes Reinecke {
1863db1312ddSHannes Reinecke 	u8 *dhchap_secret = to_host(item)->dhchap_secret;
1864db1312ddSHannes Reinecke 
1865db1312ddSHannes Reinecke 	if (!dhchap_secret)
1866db1312ddSHannes Reinecke 		return sprintf(page, "\n");
1867db1312ddSHannes Reinecke 	return sprintf(page, "%s\n", dhchap_secret);
1868db1312ddSHannes Reinecke }
1869db1312ddSHannes Reinecke 
nvmet_host_dhchap_key_store(struct config_item * item,const char * page,size_t count)1870db1312ddSHannes Reinecke static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
1871db1312ddSHannes Reinecke 		const char *page, size_t count)
1872db1312ddSHannes Reinecke {
1873db1312ddSHannes Reinecke 	struct nvmet_host *host = to_host(item);
1874db1312ddSHannes Reinecke 	int ret;
1875db1312ddSHannes Reinecke 
1876db1312ddSHannes Reinecke 	ret = nvmet_auth_set_key(host, page, false);
1877db1312ddSHannes Reinecke 	/*
1878db1312ddSHannes Reinecke 	 * Re-authentication is a soft state, so keep the
1879db1312ddSHannes Reinecke 	 * current authentication valid until the host
1880db1312ddSHannes Reinecke 	 * requests re-authentication.
1881db1312ddSHannes Reinecke 	 */
1882db1312ddSHannes Reinecke 	return ret < 0 ? ret : count;
1883db1312ddSHannes Reinecke }
1884db1312ddSHannes Reinecke 
1885db1312ddSHannes Reinecke CONFIGFS_ATTR(nvmet_host_, dhchap_key);
1886db1312ddSHannes Reinecke 
nvmet_host_dhchap_ctrl_key_show(struct config_item * item,char * page)1887db1312ddSHannes Reinecke static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
1888db1312ddSHannes Reinecke 		char *page)
1889db1312ddSHannes Reinecke {
1890db1312ddSHannes Reinecke 	u8 *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
1891db1312ddSHannes Reinecke 
1892db1312ddSHannes Reinecke 	if (!dhchap_secret)
1893db1312ddSHannes Reinecke 		return sprintf(page, "\n");
1894db1312ddSHannes Reinecke 	return sprintf(page, "%s\n", dhchap_secret);
1895db1312ddSHannes Reinecke }
1896db1312ddSHannes Reinecke 
nvmet_host_dhchap_ctrl_key_store(struct config_item * item,const char * page,size_t count)1897db1312ddSHannes Reinecke static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
1898db1312ddSHannes Reinecke 		const char *page, size_t count)
1899db1312ddSHannes Reinecke {
1900db1312ddSHannes Reinecke 	struct nvmet_host *host = to_host(item);
1901db1312ddSHannes Reinecke 	int ret;
1902db1312ddSHannes Reinecke 
1903db1312ddSHannes Reinecke 	ret = nvmet_auth_set_key(host, page, true);
1904db1312ddSHannes Reinecke 	/*
1905db1312ddSHannes Reinecke 	 * Re-authentication is a soft state, so keep the
1906db1312ddSHannes Reinecke 	 * current authentication valid until the host
1907db1312ddSHannes Reinecke 	 * requests re-authentication.
1908db1312ddSHannes Reinecke 	 */
1909db1312ddSHannes Reinecke 	return ret < 0 ? ret : count;
1910db1312ddSHannes Reinecke }
1911db1312ddSHannes Reinecke 
1912db1312ddSHannes Reinecke CONFIGFS_ATTR(nvmet_host_, dhchap_ctrl_key);
1913db1312ddSHannes Reinecke 
nvmet_host_dhchap_hash_show(struct config_item * item,char * page)1914db1312ddSHannes Reinecke static ssize_t nvmet_host_dhchap_hash_show(struct config_item *item,
1915db1312ddSHannes Reinecke 		char *page)
1916db1312ddSHannes Reinecke {
1917db1312ddSHannes Reinecke 	struct nvmet_host *host = to_host(item);
1918db1312ddSHannes Reinecke 	const char *hash_name = nvme_auth_hmac_name(host->dhchap_hash_id);
1919db1312ddSHannes Reinecke 
1920db1312ddSHannes Reinecke 	return sprintf(page, "%s\n", hash_name ? hash_name : "none");
1921db1312ddSHannes Reinecke }
1922db1312ddSHannes Reinecke 
nvmet_host_dhchap_hash_store(struct config_item * item,const char * page,size_t count)1923db1312ddSHannes Reinecke static ssize_t nvmet_host_dhchap_hash_store(struct config_item *item,
1924db1312ddSHannes Reinecke 		const char *page, size_t count)
1925db1312ddSHannes Reinecke {
1926db1312ddSHannes Reinecke 	struct nvmet_host *host = to_host(item);
1927db1312ddSHannes Reinecke 	u8 hmac_id;
1928db1312ddSHannes Reinecke 
1929db1312ddSHannes Reinecke 	hmac_id = nvme_auth_hmac_id(page);
1930db1312ddSHannes Reinecke 	if (hmac_id == NVME_AUTH_HASH_INVALID)
1931db1312ddSHannes Reinecke 		return -EINVAL;
1932db1312ddSHannes Reinecke 	if (!crypto_has_shash(nvme_auth_hmac_name(hmac_id), 0, 0))
1933db1312ddSHannes Reinecke 		return -ENOTSUPP;
1934db1312ddSHannes Reinecke 	host->dhchap_hash_id = hmac_id;
1935db1312ddSHannes Reinecke 	return count;
1936db1312ddSHannes Reinecke }
1937db1312ddSHannes Reinecke 
1938db1312ddSHannes Reinecke CONFIGFS_ATTR(nvmet_host_, dhchap_hash);
1939db1312ddSHannes Reinecke 
nvmet_host_dhchap_dhgroup_show(struct config_item * item,char * page)19407a277c37SHannes Reinecke static ssize_t nvmet_host_dhchap_dhgroup_show(struct config_item *item,
19417a277c37SHannes Reinecke 		char *page)
19427a277c37SHannes Reinecke {
19437a277c37SHannes Reinecke 	struct nvmet_host *host = to_host(item);
19447a277c37SHannes Reinecke 	const char *dhgroup = nvme_auth_dhgroup_name(host->dhchap_dhgroup_id);
19457a277c37SHannes Reinecke 
19467a277c37SHannes Reinecke 	return sprintf(page, "%s\n", dhgroup ? dhgroup : "none");
19477a277c37SHannes Reinecke }
19487a277c37SHannes Reinecke 
nvmet_host_dhchap_dhgroup_store(struct config_item * item,const char * page,size_t count)19497a277c37SHannes Reinecke static ssize_t nvmet_host_dhchap_dhgroup_store(struct config_item *item,
19507a277c37SHannes Reinecke 		const char *page, size_t count)
19517a277c37SHannes Reinecke {
19527a277c37SHannes Reinecke 	struct nvmet_host *host = to_host(item);
19537a277c37SHannes Reinecke 	int dhgroup_id;
19547a277c37SHannes Reinecke 
19557a277c37SHannes Reinecke 	dhgroup_id = nvme_auth_dhgroup_id(page);
19567a277c37SHannes Reinecke 	if (dhgroup_id == NVME_AUTH_DHGROUP_INVALID)
19577a277c37SHannes Reinecke 		return -EINVAL;
19587a277c37SHannes Reinecke 	if (dhgroup_id != NVME_AUTH_DHGROUP_NULL) {
19597a277c37SHannes Reinecke 		const char *kpp = nvme_auth_dhgroup_kpp(dhgroup_id);
19607a277c37SHannes Reinecke 
19617a277c37SHannes Reinecke 		if (!crypto_has_kpp(kpp, 0, 0))
19627a277c37SHannes Reinecke 			return -EINVAL;
19637a277c37SHannes Reinecke 	}
19647a277c37SHannes Reinecke 	host->dhchap_dhgroup_id = dhgroup_id;
19657a277c37SHannes Reinecke 	return count;
19667a277c37SHannes Reinecke }
19677a277c37SHannes Reinecke 
19687a277c37SHannes Reinecke CONFIGFS_ATTR(nvmet_host_, dhchap_dhgroup);
19697a277c37SHannes Reinecke 
1970db1312ddSHannes Reinecke static struct configfs_attribute *nvmet_host_attrs[] = {
1971db1312ddSHannes Reinecke 	&nvmet_host_attr_dhchap_key,
1972db1312ddSHannes Reinecke 	&nvmet_host_attr_dhchap_ctrl_key,
1973db1312ddSHannes Reinecke 	&nvmet_host_attr_dhchap_hash,
19747a277c37SHannes Reinecke 	&nvmet_host_attr_dhchap_dhgroup,
1975db1312ddSHannes Reinecke 	NULL,
1976db1312ddSHannes Reinecke };
1977db1312ddSHannes Reinecke #endif /* CONFIG_NVME_TARGET_AUTH */
1978db1312ddSHannes Reinecke 
nvmet_host_release(struct config_item * item)1979a07b4970SChristoph Hellwig static void nvmet_host_release(struct config_item *item)
1980a07b4970SChristoph Hellwig {
1981a07b4970SChristoph Hellwig 	struct nvmet_host *host = to_host(item);
1982ee8cd008SChristoph Hellwig 
1983db1312ddSHannes Reinecke #ifdef CONFIG_NVME_TARGET_AUTH
1984db1312ddSHannes Reinecke 	kfree(host->dhchap_secret);
1985e65fdf53SSagi Grimberg 	kfree(host->dhchap_ctrl_secret);
1986db1312ddSHannes Reinecke #endif
1987a07b4970SChristoph Hellwig 	kfree(host);
1988a07b4970SChristoph Hellwig }
1989a07b4970SChristoph Hellwig 
1990a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_host_item_ops = {
1991a07b4970SChristoph Hellwig 	.release		= nvmet_host_release,
1992a07b4970SChristoph Hellwig };
1993a07b4970SChristoph Hellwig 
199466603a31SBhumika Goyal static const struct config_item_type nvmet_host_type = {
1995a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_host_item_ops,
1996db1312ddSHannes Reinecke #ifdef CONFIG_NVME_TARGET_AUTH
1997db1312ddSHannes Reinecke 	.ct_attrs		= nvmet_host_attrs,
1998db1312ddSHannes Reinecke #endif
1999a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
2000a07b4970SChristoph Hellwig };
2001a07b4970SChristoph Hellwig 
nvmet_hosts_make_group(struct config_group * group,const char * name)2002a07b4970SChristoph Hellwig static struct config_group *nvmet_hosts_make_group(struct config_group *group,
2003a07b4970SChristoph Hellwig 		const char *name)
2004a07b4970SChristoph Hellwig {
2005a07b4970SChristoph Hellwig 	struct nvmet_host *host;
2006a07b4970SChristoph Hellwig 
2007a07b4970SChristoph Hellwig 	host = kzalloc(sizeof(*host), GFP_KERNEL);
2008a07b4970SChristoph Hellwig 	if (!host)
2009a07b4970SChristoph Hellwig 		return ERR_PTR(-ENOMEM);
2010a07b4970SChristoph Hellwig 
2011db1312ddSHannes Reinecke #ifdef CONFIG_NVME_TARGET_AUTH
2012db1312ddSHannes Reinecke 	/* Default to SHA256 */
2013db1312ddSHannes Reinecke 	host->dhchap_hash_id = NVME_AUTH_HASH_SHA256;
2014db1312ddSHannes Reinecke #endif
2015db1312ddSHannes Reinecke 
2016a07b4970SChristoph Hellwig 	config_group_init_type_name(&host->group, name, &nvmet_host_type);
2017a07b4970SChristoph Hellwig 
2018a07b4970SChristoph Hellwig 	return &host->group;
2019a07b4970SChristoph Hellwig }
2020a07b4970SChristoph Hellwig 
2021a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_hosts_group_ops = {
2022a07b4970SChristoph Hellwig 	.make_group		= nvmet_hosts_make_group,
2023a07b4970SChristoph Hellwig };
2024a07b4970SChristoph Hellwig 
202566603a31SBhumika Goyal static const struct config_item_type nvmet_hosts_type = {
2026a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_hosts_group_ops,
2027a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
2028a07b4970SChristoph Hellwig };
2029a07b4970SChristoph Hellwig 
2030a07b4970SChristoph Hellwig static struct config_group nvmet_hosts_group;
2031a07b4970SChristoph Hellwig 
203266603a31SBhumika Goyal static const struct config_item_type nvmet_root_type = {
2033a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
2034a07b4970SChristoph Hellwig };
2035a07b4970SChristoph Hellwig 
2036a07b4970SChristoph Hellwig static struct configfs_subsystem nvmet_configfs_subsystem = {
2037a07b4970SChristoph Hellwig 	.su_group = {
2038a07b4970SChristoph Hellwig 		.cg_item = {
2039a07b4970SChristoph Hellwig 			.ci_namebuf	= "nvmet",
2040a07b4970SChristoph Hellwig 			.ci_type	= &nvmet_root_type,
2041a07b4970SChristoph Hellwig 		},
2042a07b4970SChristoph Hellwig 	},
2043a07b4970SChristoph Hellwig };
2044a07b4970SChristoph Hellwig 
nvmet_init_configfs(void)2045a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void)
2046a07b4970SChristoph Hellwig {
2047a07b4970SChristoph Hellwig 	int ret;
2048a07b4970SChristoph Hellwig 
2049a07b4970SChristoph Hellwig 	config_group_init(&nvmet_configfs_subsystem.su_group);
2050a07b4970SChristoph Hellwig 	mutex_init(&nvmet_configfs_subsystem.su_mutex);
2051a07b4970SChristoph Hellwig 
2052a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_subsystems_group,
2053a07b4970SChristoph Hellwig 			"subsystems", &nvmet_subsystems_type);
2054a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_subsystems_group,
2055a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
2056a07b4970SChristoph Hellwig 
2057a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_ports_group,
2058a07b4970SChristoph Hellwig 			"ports", &nvmet_ports_type);
2059a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_ports_group,
2060a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
2061a07b4970SChristoph Hellwig 
2062a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_hosts_group,
2063a07b4970SChristoph Hellwig 			"hosts", &nvmet_hosts_type);
2064a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_hosts_group,
2065a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
2066a07b4970SChristoph Hellwig 
2067a07b4970SChristoph Hellwig 	ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
2068a07b4970SChristoph Hellwig 	if (ret) {
2069a07b4970SChristoph Hellwig 		pr_err("configfs_register_subsystem: %d\n", ret);
2070a07b4970SChristoph Hellwig 		return ret;
2071a07b4970SChristoph Hellwig 	}
2072a07b4970SChristoph Hellwig 
2073a07b4970SChristoph Hellwig 	return 0;
2074a07b4970SChristoph Hellwig }
2075a07b4970SChristoph Hellwig 
nvmet_exit_configfs(void)2076a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void)
2077a07b4970SChristoph Hellwig {
2078a07b4970SChristoph Hellwig 	configfs_unregister_subsystem(&nvmet_configfs_subsystem);
2079a07b4970SChristoph Hellwig }
2080