xref: /openbmc/linux/drivers/nvme/target/configfs.c (revision 34ad6151)
177141dc6SChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
2a07b4970SChristoph Hellwig /*
3a07b4970SChristoph Hellwig  * Configfs interface for the NVMe target.
4a07b4970SChristoph Hellwig  * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5a07b4970SChristoph Hellwig  */
6a07b4970SChristoph Hellwig #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7a07b4970SChristoph Hellwig #include <linux/kernel.h>
8a07b4970SChristoph Hellwig #include <linux/module.h>
9a07b4970SChristoph Hellwig #include <linux/slab.h>
10a07b4970SChristoph Hellwig #include <linux/stat.h>
11a07b4970SChristoph Hellwig #include <linux/ctype.h>
12c6925093SLogan Gunthorpe #include <linux/pci.h>
13c6925093SLogan Gunthorpe #include <linux/pci-p2pdma.h>
14a07b4970SChristoph Hellwig 
15a07b4970SChristoph Hellwig #include "nvmet.h"
16a07b4970SChristoph Hellwig 
1766603a31SBhumika Goyal static const struct config_item_type nvmet_host_type;
1866603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type;
19a07b4970SChristoph Hellwig 
20b662a078SJay Sternberg static LIST_HEAD(nvmet_ports_list);
21b662a078SJay Sternberg struct list_head *nvmet_ports = &nvmet_ports_list;
22b662a078SJay Sternberg 
2345e2f3c2SChaitanya Kulkarni struct nvmet_type_name_map {
24a5d18612SChristoph Hellwig 	u8		type;
25a5d18612SChristoph Hellwig 	const char	*name;
2645e2f3c2SChaitanya Kulkarni };
2745e2f3c2SChaitanya Kulkarni 
2845e2f3c2SChaitanya Kulkarni static struct nvmet_type_name_map nvmet_transport[] = {
29a5d18612SChristoph Hellwig 	{ NVMF_TRTYPE_RDMA,	"rdma" },
30a5d18612SChristoph Hellwig 	{ NVMF_TRTYPE_FC,	"fc" },
31ad4f530eSSagi Grimberg 	{ NVMF_TRTYPE_TCP,	"tcp" },
32a5d18612SChristoph Hellwig 	{ NVMF_TRTYPE_LOOP,	"loop" },
33a5d18612SChristoph Hellwig };
34a5d18612SChristoph Hellwig 
357e764179SChaitanya Kulkarni static const struct nvmet_type_name_map nvmet_addr_family[] = {
367e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_PCI,		"pcie" },
377e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_IP4,		"ipv4" },
387e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_IP6,		"ipv6" },
397e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_IB,		"ib" },
407e764179SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_FC,		"fc" },
41d02abd19SChaitanya Kulkarni 	{ NVMF_ADDR_FAMILY_LOOP,	"loop" },
427e764179SChaitanya Kulkarni };
437e764179SChaitanya Kulkarni 
443ecb5faaSChaitanya Kulkarni static bool nvmet_is_port_enabled(struct nvmet_port *p, const char *caller)
453ecb5faaSChaitanya Kulkarni {
463ecb5faaSChaitanya Kulkarni 	if (p->enabled)
473ecb5faaSChaitanya Kulkarni 		pr_err("Disable port '%u' before changing attribute in %s\n",
483ecb5faaSChaitanya Kulkarni 		       le16_to_cpu(p->disc_addr.portid), caller);
493ecb5faaSChaitanya Kulkarni 	return p->enabled;
503ecb5faaSChaitanya Kulkarni }
513ecb5faaSChaitanya Kulkarni 
52a07b4970SChristoph Hellwig /*
53a07b4970SChristoph Hellwig  * nvmet_port Generic ConfigFS definitions.
54a07b4970SChristoph Hellwig  * Used in any place in the ConfigFS tree that refers to an address.
55a07b4970SChristoph Hellwig  */
567e764179SChaitanya Kulkarni static ssize_t nvmet_addr_adrfam_show(struct config_item *item, char *page)
57a07b4970SChristoph Hellwig {
587e764179SChaitanya Kulkarni 	u8 adrfam = to_nvmet_port(item)->disc_addr.adrfam;
597e764179SChaitanya Kulkarni 	int i;
607e764179SChaitanya Kulkarni 
617e764179SChaitanya Kulkarni 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
627e764179SChaitanya Kulkarni 		if (nvmet_addr_family[i].type == adrfam)
6398152eb7SChaitanya Kulkarni 			return snprintf(page, PAGE_SIZE, "%s\n",
6498152eb7SChaitanya Kulkarni 					nvmet_addr_family[i].name);
65a07b4970SChristoph Hellwig 	}
667e764179SChaitanya Kulkarni 
6798152eb7SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "\n");
68a07b4970SChristoph Hellwig }
69a07b4970SChristoph Hellwig 
70a07b4970SChristoph Hellwig static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
71a07b4970SChristoph Hellwig 		const char *page, size_t count)
72a07b4970SChristoph Hellwig {
73a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
747e764179SChaitanya Kulkarni 	int i;
75a07b4970SChristoph Hellwig 
763ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
77a07b4970SChristoph Hellwig 		return -EACCES;
78a07b4970SChristoph Hellwig 
797e764179SChaitanya Kulkarni 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
807e764179SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_addr_family[i].name))
817e764179SChaitanya Kulkarni 			goto found;
82a07b4970SChristoph Hellwig 	}
83a07b4970SChristoph Hellwig 
847e764179SChaitanya Kulkarni 	pr_err("Invalid value '%s' for adrfam\n", page);
857e764179SChaitanya Kulkarni 	return -EINVAL;
867e764179SChaitanya Kulkarni 
877e764179SChaitanya Kulkarni found:
88d02abd19SChaitanya Kulkarni 	port->disc_addr.adrfam = nvmet_addr_family[i].type;
89a07b4970SChristoph Hellwig 	return count;
90a07b4970SChristoph Hellwig }
91a07b4970SChristoph Hellwig 
92a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_adrfam);
93a07b4970SChristoph Hellwig 
94a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_show(struct config_item *item,
95a07b4970SChristoph Hellwig 		char *page)
96a07b4970SChristoph Hellwig {
9773d77c53SChaitanya Kulkarni 	__le16 portid = to_nvmet_port(item)->disc_addr.portid;
98a07b4970SChristoph Hellwig 
9973d77c53SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%d\n", le16_to_cpu(portid));
100a07b4970SChristoph Hellwig }
101a07b4970SChristoph Hellwig 
102a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_store(struct config_item *item,
103a07b4970SChristoph Hellwig 		const char *page, size_t count)
104a07b4970SChristoph Hellwig {
105a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
106a07b4970SChristoph Hellwig 	u16 portid = 0;
107a07b4970SChristoph Hellwig 
108a07b4970SChristoph Hellwig 	if (kstrtou16(page, 0, &portid)) {
109a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for portid\n", page);
110a07b4970SChristoph Hellwig 		return -EINVAL;
111a07b4970SChristoph Hellwig 	}
112a07b4970SChristoph Hellwig 
1133ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
114a07b4970SChristoph Hellwig 		return -EACCES;
1153ecb5faaSChaitanya Kulkarni 
116a07b4970SChristoph Hellwig 	port->disc_addr.portid = cpu_to_le16(portid);
117a07b4970SChristoph Hellwig 	return count;
118a07b4970SChristoph Hellwig }
119a07b4970SChristoph Hellwig 
120a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_portid);
121a07b4970SChristoph Hellwig 
122a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_show(struct config_item *item,
123a07b4970SChristoph Hellwig 		char *page)
124a07b4970SChristoph Hellwig {
125a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
126a07b4970SChristoph Hellwig 
12773d77c53SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.traddr);
128a07b4970SChristoph Hellwig }
129a07b4970SChristoph Hellwig 
130a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_store(struct config_item *item,
131a07b4970SChristoph Hellwig 		const char *page, size_t count)
132a07b4970SChristoph Hellwig {
133a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
134a07b4970SChristoph Hellwig 
135a07b4970SChristoph Hellwig 	if (count > NVMF_TRADDR_SIZE) {
136a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for traddr\n", page);
137a07b4970SChristoph Hellwig 		return -EINVAL;
138a07b4970SChristoph Hellwig 	}
139a07b4970SChristoph Hellwig 
1403ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
141a07b4970SChristoph Hellwig 		return -EACCES;
1429ba2a5cbSSagi Grimberg 
1439ba2a5cbSSagi Grimberg 	if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
1449ba2a5cbSSagi Grimberg 		return -EINVAL;
1459ba2a5cbSSagi Grimberg 	return count;
146a07b4970SChristoph Hellwig }
147a07b4970SChristoph Hellwig 
148a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_traddr);
149a07b4970SChristoph Hellwig 
15087628e28SChaitanya Kulkarni static const struct nvmet_type_name_map nvmet_addr_treq[] = {
15187628e28SChaitanya Kulkarni 	{ NVMF_TREQ_NOT_SPECIFIED,	"not specified" },
15287628e28SChaitanya Kulkarni 	{ NVMF_TREQ_REQUIRED,		"required" },
15387628e28SChaitanya Kulkarni 	{ NVMF_TREQ_NOT_REQUIRED,	"not required" },
15487628e28SChaitanya Kulkarni };
15587628e28SChaitanya Kulkarni 
15687628e28SChaitanya Kulkarni static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page)
157a07b4970SChristoph Hellwig {
15887628e28SChaitanya Kulkarni 	u8 treq = to_nvmet_port(item)->disc_addr.treq &
15987628e28SChaitanya Kulkarni 		NVME_TREQ_SECURE_CHANNEL_MASK;
16087628e28SChaitanya Kulkarni 	int i;
16187628e28SChaitanya Kulkarni 
16287628e28SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
16387628e28SChaitanya Kulkarni 		if (treq == nvmet_addr_treq[i].type)
16498152eb7SChaitanya Kulkarni 			return snprintf(page, PAGE_SIZE, "%s\n",
16598152eb7SChaitanya Kulkarni 					nvmet_addr_treq[i].name);
166a07b4970SChristoph Hellwig 	}
16787628e28SChaitanya Kulkarni 
16898152eb7SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "\n");
169a07b4970SChristoph Hellwig }
170a07b4970SChristoph Hellwig 
171a07b4970SChristoph Hellwig static ssize_t nvmet_addr_treq_store(struct config_item *item,
172a07b4970SChristoph Hellwig 		const char *page, size_t count)
173a07b4970SChristoph Hellwig {
174a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1750445e1b5SSagi Grimberg 	u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK;
17687628e28SChaitanya Kulkarni 	int i;
177a07b4970SChristoph Hellwig 
1783ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
179a07b4970SChristoph Hellwig 		return -EACCES;
180a07b4970SChristoph Hellwig 
18187628e28SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
18287628e28SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_addr_treq[i].name))
18387628e28SChaitanya Kulkarni 			goto found;
18487628e28SChaitanya Kulkarni 	}
18587628e28SChaitanya Kulkarni 
186a07b4970SChristoph Hellwig 	pr_err("Invalid value '%s' for treq\n", page);
187a07b4970SChristoph Hellwig 	return -EINVAL;
188a07b4970SChristoph Hellwig 
18987628e28SChaitanya Kulkarni found:
19087628e28SChaitanya Kulkarni 	treq |= nvmet_addr_treq[i].type;
19187628e28SChaitanya Kulkarni 	port->disc_addr.treq = treq;
192a07b4970SChristoph Hellwig 	return count;
193a07b4970SChristoph Hellwig }
194a07b4970SChristoph Hellwig 
195a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_treq);
196a07b4970SChristoph Hellwig 
197a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
198a07b4970SChristoph Hellwig 		char *page)
199a07b4970SChristoph Hellwig {
200a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
201a07b4970SChristoph Hellwig 
20273d77c53SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.trsvcid);
203a07b4970SChristoph Hellwig }
204a07b4970SChristoph Hellwig 
205a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
206a07b4970SChristoph Hellwig 		const char *page, size_t count)
207a07b4970SChristoph Hellwig {
208a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
209a07b4970SChristoph Hellwig 
210a07b4970SChristoph Hellwig 	if (count > NVMF_TRSVCID_SIZE) {
211a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for trsvcid\n", page);
212a07b4970SChristoph Hellwig 		return -EINVAL;
213a07b4970SChristoph Hellwig 	}
2143ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
215a07b4970SChristoph Hellwig 		return -EACCES;
2169ba2a5cbSSagi Grimberg 
2179ba2a5cbSSagi Grimberg 	if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
2189ba2a5cbSSagi Grimberg 		return -EINVAL;
2199ba2a5cbSSagi Grimberg 	return count;
220a07b4970SChristoph Hellwig }
221a07b4970SChristoph Hellwig 
222a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trsvcid);
223a07b4970SChristoph Hellwig 
2240d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
2250d5ee2b2SSteve Wise 		char *page)
2260d5ee2b2SSteve Wise {
2270d5ee2b2SSteve Wise 	struct nvmet_port *port = to_nvmet_port(item);
2280d5ee2b2SSteve Wise 
2290d5ee2b2SSteve Wise 	return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
2300d5ee2b2SSteve Wise }
2310d5ee2b2SSteve Wise 
2320d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
2330d5ee2b2SSteve Wise 		const char *page, size_t count)
2340d5ee2b2SSteve Wise {
2350d5ee2b2SSteve Wise 	struct nvmet_port *port = to_nvmet_port(item);
2360d5ee2b2SSteve Wise 	int ret;
2370d5ee2b2SSteve Wise 
2383ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
2390d5ee2b2SSteve Wise 		return -EACCES;
2400d5ee2b2SSteve Wise 	ret = kstrtoint(page, 0, &port->inline_data_size);
2410d5ee2b2SSteve Wise 	if (ret) {
2420d5ee2b2SSteve Wise 		pr_err("Invalid value '%s' for inline_data_size\n", page);
2430d5ee2b2SSteve Wise 		return -EINVAL;
2440d5ee2b2SSteve Wise 	}
2450d5ee2b2SSteve Wise 	return count;
2460d5ee2b2SSteve Wise }
2470d5ee2b2SSteve Wise 
2480d5ee2b2SSteve Wise CONFIGFS_ATTR(nvmet_, param_inline_data_size);
2490d5ee2b2SSteve Wise 
250ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
251ea52ac1cSIsrael Rukshin static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
252ea52ac1cSIsrael Rukshin 		char *page)
253ea52ac1cSIsrael Rukshin {
254ea52ac1cSIsrael Rukshin 	struct nvmet_port *port = to_nvmet_port(item);
255ea52ac1cSIsrael Rukshin 
256ea52ac1cSIsrael Rukshin 	return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
257ea52ac1cSIsrael Rukshin }
258ea52ac1cSIsrael Rukshin 
259ea52ac1cSIsrael Rukshin static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
260ea52ac1cSIsrael Rukshin 		const char *page, size_t count)
261ea52ac1cSIsrael Rukshin {
262ea52ac1cSIsrael Rukshin 	struct nvmet_port *port = to_nvmet_port(item);
263ea52ac1cSIsrael Rukshin 	bool val;
264ea52ac1cSIsrael Rukshin 
265ea52ac1cSIsrael Rukshin 	if (strtobool(page, &val))
266ea52ac1cSIsrael Rukshin 		return -EINVAL;
267ea52ac1cSIsrael Rukshin 
268cc345622SIsrael Rukshin 	if (nvmet_is_port_enabled(port, __func__))
269ea52ac1cSIsrael Rukshin 		return -EACCES;
270ea52ac1cSIsrael Rukshin 
271ea52ac1cSIsrael Rukshin 	port->pi_enable = val;
272ea52ac1cSIsrael Rukshin 	return count;
273ea52ac1cSIsrael Rukshin }
274ea52ac1cSIsrael Rukshin 
275ea52ac1cSIsrael Rukshin CONFIGFS_ATTR(nvmet_, param_pi_enable);
276ea52ac1cSIsrael Rukshin #endif
277ea52ac1cSIsrael Rukshin 
278a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_show(struct config_item *item,
279a07b4970SChristoph Hellwig 		char *page)
280a07b4970SChristoph Hellwig {
281a5d18612SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
282a5d18612SChristoph Hellwig 	int i;
283a5d18612SChristoph Hellwig 
28445e2f3c2SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
28545e2f3c2SChaitanya Kulkarni 		if (port->disc_addr.trtype == nvmet_transport[i].type)
28698152eb7SChaitanya Kulkarni 			return snprintf(page, PAGE_SIZE,
28798152eb7SChaitanya Kulkarni 					"%s\n", nvmet_transport[i].name);
288a07b4970SChristoph Hellwig 	}
289a5d18612SChristoph Hellwig 
290a5d18612SChristoph Hellwig 	return sprintf(page, "\n");
291a07b4970SChristoph Hellwig }
292a07b4970SChristoph Hellwig 
293a07b4970SChristoph Hellwig static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
294a07b4970SChristoph Hellwig {
295a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
296a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
297a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
298a07b4970SChristoph Hellwig }
299a07b4970SChristoph Hellwig 
300a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_store(struct config_item *item,
301a07b4970SChristoph Hellwig 		const char *page, size_t count)
302a07b4970SChristoph Hellwig {
303a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
304a5d18612SChristoph Hellwig 	int i;
305a07b4970SChristoph Hellwig 
3063ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
307a07b4970SChristoph Hellwig 		return -EACCES;
308a07b4970SChristoph Hellwig 
30945e2f3c2SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
31045e2f3c2SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_transport[i].name))
311a5d18612SChristoph Hellwig 			goto found;
312a07b4970SChristoph Hellwig 	}
313a07b4970SChristoph Hellwig 
314a5d18612SChristoph Hellwig 	pr_err("Invalid value '%s' for trtype\n", page);
315a5d18612SChristoph Hellwig 	return -EINVAL;
3167e764179SChaitanya Kulkarni 
317a5d18612SChristoph Hellwig found:
318a5d18612SChristoph Hellwig 	memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
31945e2f3c2SChaitanya Kulkarni 	port->disc_addr.trtype = nvmet_transport[i].type;
320a5d18612SChristoph Hellwig 	if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
321a5d18612SChristoph Hellwig 		nvmet_port_init_tsas_rdma(port);
322a07b4970SChristoph Hellwig 	return count;
323a07b4970SChristoph Hellwig }
324a07b4970SChristoph Hellwig 
325a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trtype);
326a07b4970SChristoph Hellwig 
327a07b4970SChristoph Hellwig /*
328a07b4970SChristoph Hellwig  * Namespace structures & file operation functions below
329a07b4970SChristoph Hellwig  */
330a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
331a07b4970SChristoph Hellwig {
332a07b4970SChristoph Hellwig 	return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
333a07b4970SChristoph Hellwig }
334a07b4970SChristoph Hellwig 
335a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_store(struct config_item *item,
336a07b4970SChristoph Hellwig 		const char *page, size_t count)
337a07b4970SChristoph Hellwig {
338a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
339a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = ns->subsys;
3405613d312SHannes Reinecke 	size_t len;
341a07b4970SChristoph Hellwig 	int ret;
342a07b4970SChristoph Hellwig 
343a07b4970SChristoph Hellwig 	mutex_lock(&subsys->lock);
344a07b4970SChristoph Hellwig 	ret = -EBUSY;
345e4fcf07cSSolganik Alexander 	if (ns->enabled)
346a07b4970SChristoph Hellwig 		goto out_unlock;
347a07b4970SChristoph Hellwig 
3485613d312SHannes Reinecke 	ret = -EINVAL;
3495613d312SHannes Reinecke 	len = strcspn(page, "\n");
3505613d312SHannes Reinecke 	if (!len)
3515613d312SHannes Reinecke 		goto out_unlock;
352a07b4970SChristoph Hellwig 
3535613d312SHannes Reinecke 	kfree(ns->device_path);
354a07b4970SChristoph Hellwig 	ret = -ENOMEM;
35509bb8986SChen Zhou 	ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
356a07b4970SChristoph Hellwig 	if (!ns->device_path)
357a07b4970SChristoph Hellwig 		goto out_unlock;
358a07b4970SChristoph Hellwig 
359a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
360a07b4970SChristoph Hellwig 	return count;
361a07b4970SChristoph Hellwig 
362a07b4970SChristoph Hellwig out_unlock:
363a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
364a07b4970SChristoph Hellwig 	return ret;
365a07b4970SChristoph Hellwig }
366a07b4970SChristoph Hellwig 
367a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_path);
368a07b4970SChristoph Hellwig 
369c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA
370c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
371c6925093SLogan Gunthorpe {
372c6925093SLogan Gunthorpe 	struct nvmet_ns *ns = to_nvmet_ns(item);
373c6925093SLogan Gunthorpe 
374c6925093SLogan Gunthorpe 	return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
375c6925093SLogan Gunthorpe }
376c6925093SLogan Gunthorpe 
377c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
378c6925093SLogan Gunthorpe 		const char *page, size_t count)
379c6925093SLogan Gunthorpe {
380c6925093SLogan Gunthorpe 	struct nvmet_ns *ns = to_nvmet_ns(item);
381c6925093SLogan Gunthorpe 	struct pci_dev *p2p_dev = NULL;
382c6925093SLogan Gunthorpe 	bool use_p2pmem;
383c6925093SLogan Gunthorpe 	int ret = count;
384c6925093SLogan Gunthorpe 	int error;
385c6925093SLogan Gunthorpe 
386c6925093SLogan Gunthorpe 	mutex_lock(&ns->subsys->lock);
387c6925093SLogan Gunthorpe 	if (ns->enabled) {
388c6925093SLogan Gunthorpe 		ret = -EBUSY;
389c6925093SLogan Gunthorpe 		goto out_unlock;
390c6925093SLogan Gunthorpe 	}
391c6925093SLogan Gunthorpe 
392c6925093SLogan Gunthorpe 	error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
393c6925093SLogan Gunthorpe 	if (error) {
394c6925093SLogan Gunthorpe 		ret = error;
395c6925093SLogan Gunthorpe 		goto out_unlock;
396c6925093SLogan Gunthorpe 	}
397c6925093SLogan Gunthorpe 
398c6925093SLogan Gunthorpe 	ns->use_p2pmem = use_p2pmem;
399c6925093SLogan Gunthorpe 	pci_dev_put(ns->p2p_dev);
400c6925093SLogan Gunthorpe 	ns->p2p_dev = p2p_dev;
401c6925093SLogan Gunthorpe 
402c6925093SLogan Gunthorpe out_unlock:
403c6925093SLogan Gunthorpe 	mutex_unlock(&ns->subsys->lock);
404c6925093SLogan Gunthorpe 
405c6925093SLogan Gunthorpe 	return ret;
406c6925093SLogan Gunthorpe }
407c6925093SLogan Gunthorpe 
408c6925093SLogan Gunthorpe CONFIGFS_ATTR(nvmet_ns_, p2pmem);
409c6925093SLogan Gunthorpe #endif /* CONFIG_PCI_P2PDMA */
410c6925093SLogan Gunthorpe 
411430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
412430c7befSJohannes Thumshirn {
413430c7befSJohannes Thumshirn 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
414430c7befSJohannes Thumshirn }
415430c7befSJohannes Thumshirn 
416430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
417430c7befSJohannes Thumshirn 					  const char *page, size_t count)
418430c7befSJohannes Thumshirn {
419430c7befSJohannes Thumshirn 	struct nvmet_ns *ns = to_nvmet_ns(item);
420430c7befSJohannes Thumshirn 	struct nvmet_subsys *subsys = ns->subsys;
421430c7befSJohannes Thumshirn 	int ret = 0;
422430c7befSJohannes Thumshirn 
423430c7befSJohannes Thumshirn 	mutex_lock(&subsys->lock);
424430c7befSJohannes Thumshirn 	if (ns->enabled) {
425430c7befSJohannes Thumshirn 		ret = -EBUSY;
426430c7befSJohannes Thumshirn 		goto out_unlock;
427430c7befSJohannes Thumshirn 	}
428430c7befSJohannes Thumshirn 
429430c7befSJohannes Thumshirn 	if (uuid_parse(page, &ns->uuid))
430430c7befSJohannes Thumshirn 		ret = -EINVAL;
431430c7befSJohannes Thumshirn 
432430c7befSJohannes Thumshirn out_unlock:
433430c7befSJohannes Thumshirn 	mutex_unlock(&subsys->lock);
434430c7befSJohannes Thumshirn 	return ret ? ret : count;
435430c7befSJohannes Thumshirn }
436430c7befSJohannes Thumshirn 
437f871749aSMax Gurtovoy CONFIGFS_ATTR(nvmet_ns_, device_uuid);
438f871749aSMax Gurtovoy 
439a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
440a07b4970SChristoph Hellwig {
441a07b4970SChristoph Hellwig 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
442a07b4970SChristoph Hellwig }
443a07b4970SChristoph Hellwig 
444a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
445a07b4970SChristoph Hellwig 		const char *page, size_t count)
446a07b4970SChristoph Hellwig {
447a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
448a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = ns->subsys;
449a07b4970SChristoph Hellwig 	u8 nguid[16];
450a07b4970SChristoph Hellwig 	const char *p = page;
451a07b4970SChristoph Hellwig 	int i;
452a07b4970SChristoph Hellwig 	int ret = 0;
453a07b4970SChristoph Hellwig 
454a07b4970SChristoph Hellwig 	mutex_lock(&subsys->lock);
455e4fcf07cSSolganik Alexander 	if (ns->enabled) {
456a07b4970SChristoph Hellwig 		ret = -EBUSY;
457a07b4970SChristoph Hellwig 		goto out_unlock;
458a07b4970SChristoph Hellwig 	}
459a07b4970SChristoph Hellwig 
460a07b4970SChristoph Hellwig 	for (i = 0; i < 16; i++) {
461a07b4970SChristoph Hellwig 		if (p + 2 > page + count) {
462a07b4970SChristoph Hellwig 			ret = -EINVAL;
463a07b4970SChristoph Hellwig 			goto out_unlock;
464a07b4970SChristoph Hellwig 		}
465a07b4970SChristoph Hellwig 		if (!isxdigit(p[0]) || !isxdigit(p[1])) {
466a07b4970SChristoph Hellwig 			ret = -EINVAL;
467a07b4970SChristoph Hellwig 			goto out_unlock;
468a07b4970SChristoph Hellwig 		}
469a07b4970SChristoph Hellwig 
470a07b4970SChristoph Hellwig 		nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
471a07b4970SChristoph Hellwig 		p += 2;
472a07b4970SChristoph Hellwig 
473a07b4970SChristoph Hellwig 		if (*p == '-' || *p == ':')
474a07b4970SChristoph Hellwig 			p++;
475a07b4970SChristoph Hellwig 	}
476a07b4970SChristoph Hellwig 
477a07b4970SChristoph Hellwig 	memcpy(&ns->nguid, nguid, sizeof(nguid));
478a07b4970SChristoph Hellwig out_unlock:
479a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
480a07b4970SChristoph Hellwig 	return ret ? ret : count;
481a07b4970SChristoph Hellwig }
482a07b4970SChristoph Hellwig 
483a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_nguid);
484a07b4970SChristoph Hellwig 
48562ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
48662ac0d32SChristoph Hellwig {
48762ac0d32SChristoph Hellwig 	return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
48862ac0d32SChristoph Hellwig }
48962ac0d32SChristoph Hellwig 
49062ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
49162ac0d32SChristoph Hellwig 		const char *page, size_t count)
49262ac0d32SChristoph Hellwig {
49362ac0d32SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
49462ac0d32SChristoph Hellwig 	u32 oldgrpid, newgrpid;
49562ac0d32SChristoph Hellwig 	int ret;
49662ac0d32SChristoph Hellwig 
49762ac0d32SChristoph Hellwig 	ret = kstrtou32(page, 0, &newgrpid);
49862ac0d32SChristoph Hellwig 	if (ret)
49962ac0d32SChristoph Hellwig 		return ret;
50062ac0d32SChristoph Hellwig 
50162ac0d32SChristoph Hellwig 	if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
50262ac0d32SChristoph Hellwig 		return -EINVAL;
50362ac0d32SChristoph Hellwig 
50462ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
50562ac0d32SChristoph Hellwig 	oldgrpid = ns->anagrpid;
50662ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[newgrpid]++;
50762ac0d32SChristoph Hellwig 	ns->anagrpid = newgrpid;
50862ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[oldgrpid]--;
50962ac0d32SChristoph Hellwig 	nvmet_ana_chgcnt++;
51062ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
51162ac0d32SChristoph Hellwig 
51262ac0d32SChristoph Hellwig 	nvmet_send_ana_event(ns->subsys, NULL);
51362ac0d32SChristoph Hellwig 	return count;
51462ac0d32SChristoph Hellwig }
51562ac0d32SChristoph Hellwig 
51662ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
51762ac0d32SChristoph Hellwig 
518a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
519a07b4970SChristoph Hellwig {
520e4fcf07cSSolganik Alexander 	return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
521a07b4970SChristoph Hellwig }
522a07b4970SChristoph Hellwig 
523a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_store(struct config_item *item,
524a07b4970SChristoph Hellwig 		const char *page, size_t count)
525a07b4970SChristoph Hellwig {
526a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
527a07b4970SChristoph Hellwig 	bool enable;
528a07b4970SChristoph Hellwig 	int ret = 0;
529a07b4970SChristoph Hellwig 
530a07b4970SChristoph Hellwig 	if (strtobool(page, &enable))
531a07b4970SChristoph Hellwig 		return -EINVAL;
532a07b4970SChristoph Hellwig 
533a07b4970SChristoph Hellwig 	if (enable)
534a07b4970SChristoph Hellwig 		ret = nvmet_ns_enable(ns);
535a07b4970SChristoph Hellwig 	else
536a07b4970SChristoph Hellwig 		nvmet_ns_disable(ns);
537a07b4970SChristoph Hellwig 
538a07b4970SChristoph Hellwig 	return ret ? ret : count;
539a07b4970SChristoph Hellwig }
540a07b4970SChristoph Hellwig 
541a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, enable);
542a07b4970SChristoph Hellwig 
54355eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
54455eb942eSChaitanya Kulkarni {
54555eb942eSChaitanya Kulkarni 	return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
54655eb942eSChaitanya Kulkarni }
54755eb942eSChaitanya Kulkarni 
54855eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
54955eb942eSChaitanya Kulkarni 		const char *page, size_t count)
55055eb942eSChaitanya Kulkarni {
55155eb942eSChaitanya Kulkarni 	struct nvmet_ns *ns = to_nvmet_ns(item);
55255eb942eSChaitanya Kulkarni 	bool val;
55355eb942eSChaitanya Kulkarni 
55455eb942eSChaitanya Kulkarni 	if (strtobool(page, &val))
55555eb942eSChaitanya Kulkarni 		return -EINVAL;
55655eb942eSChaitanya Kulkarni 
55755eb942eSChaitanya Kulkarni 	mutex_lock(&ns->subsys->lock);
55855eb942eSChaitanya Kulkarni 	if (ns->enabled) {
55955eb942eSChaitanya Kulkarni 		pr_err("disable ns before setting buffered_io value.\n");
56055eb942eSChaitanya Kulkarni 		mutex_unlock(&ns->subsys->lock);
56155eb942eSChaitanya Kulkarni 		return -EINVAL;
56255eb942eSChaitanya Kulkarni 	}
56355eb942eSChaitanya Kulkarni 
56455eb942eSChaitanya Kulkarni 	ns->buffered_io = val;
56555eb942eSChaitanya Kulkarni 	mutex_unlock(&ns->subsys->lock);
56655eb942eSChaitanya Kulkarni 	return count;
56755eb942eSChaitanya Kulkarni }
56855eb942eSChaitanya Kulkarni 
56955eb942eSChaitanya Kulkarni CONFIGFS_ATTR(nvmet_ns_, buffered_io);
57055eb942eSChaitanya Kulkarni 
5711f357548SChaitanya Kulkarni static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
5721f357548SChaitanya Kulkarni 		const char *page, size_t count)
5731f357548SChaitanya Kulkarni {
5741f357548SChaitanya Kulkarni 	struct nvmet_ns *ns = to_nvmet_ns(item);
5751f357548SChaitanya Kulkarni 	bool val;
5761f357548SChaitanya Kulkarni 
5771f357548SChaitanya Kulkarni 	if (strtobool(page, &val))
5781f357548SChaitanya Kulkarni 		return -EINVAL;
5791f357548SChaitanya Kulkarni 
5801f357548SChaitanya Kulkarni 	if (!val)
5811f357548SChaitanya Kulkarni 		return -EINVAL;
5821f357548SChaitanya Kulkarni 
5831f357548SChaitanya Kulkarni 	mutex_lock(&ns->subsys->lock);
5841f357548SChaitanya Kulkarni 	if (!ns->enabled) {
5851f357548SChaitanya Kulkarni 		pr_err("enable ns before revalidate.\n");
5861f357548SChaitanya Kulkarni 		mutex_unlock(&ns->subsys->lock);
5871f357548SChaitanya Kulkarni 		return -EINVAL;
5881f357548SChaitanya Kulkarni 	}
589da783733SChristoph Hellwig 	if (nvmet_ns_revalidate(ns))
590da783733SChristoph Hellwig 		nvmet_ns_changed(ns->subsys, ns->nsid);
5911f357548SChaitanya Kulkarni 	mutex_unlock(&ns->subsys->lock);
5921f357548SChaitanya Kulkarni 	return count;
5931f357548SChaitanya Kulkarni }
5941f357548SChaitanya Kulkarni 
5951f357548SChaitanya Kulkarni CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
5961f357548SChaitanya Kulkarni 
597a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_ns_attrs[] = {
598a07b4970SChristoph Hellwig 	&nvmet_ns_attr_device_path,
599a07b4970SChristoph Hellwig 	&nvmet_ns_attr_device_nguid,
600430c7befSJohannes Thumshirn 	&nvmet_ns_attr_device_uuid,
60162ac0d32SChristoph Hellwig 	&nvmet_ns_attr_ana_grpid,
602a07b4970SChristoph Hellwig 	&nvmet_ns_attr_enable,
60355eb942eSChaitanya Kulkarni 	&nvmet_ns_attr_buffered_io,
6041f357548SChaitanya Kulkarni 	&nvmet_ns_attr_revalidate_size,
605c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA
606c6925093SLogan Gunthorpe 	&nvmet_ns_attr_p2pmem,
607c6925093SLogan Gunthorpe #endif
608a07b4970SChristoph Hellwig 	NULL,
609a07b4970SChristoph Hellwig };
610a07b4970SChristoph Hellwig 
611a07b4970SChristoph Hellwig static void nvmet_ns_release(struct config_item *item)
612a07b4970SChristoph Hellwig {
613a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
614a07b4970SChristoph Hellwig 
615a07b4970SChristoph Hellwig 	nvmet_ns_free(ns);
616a07b4970SChristoph Hellwig }
617a07b4970SChristoph Hellwig 
618a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_ns_item_ops = {
619a07b4970SChristoph Hellwig 	.release		= nvmet_ns_release,
620a07b4970SChristoph Hellwig };
621a07b4970SChristoph Hellwig 
62266603a31SBhumika Goyal static const struct config_item_type nvmet_ns_type = {
623a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_ns_item_ops,
624a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_ns_attrs,
625a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
626a07b4970SChristoph Hellwig };
627a07b4970SChristoph Hellwig 
628a07b4970SChristoph Hellwig static struct config_group *nvmet_ns_make(struct config_group *group,
629a07b4970SChristoph Hellwig 		const char *name)
630a07b4970SChristoph Hellwig {
631a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
632a07b4970SChristoph Hellwig 	struct nvmet_ns *ns;
633a07b4970SChristoph Hellwig 	int ret;
634a07b4970SChristoph Hellwig 	u32 nsid;
635a07b4970SChristoph Hellwig 
636a07b4970SChristoph Hellwig 	ret = kstrtou32(name, 0, &nsid);
637a07b4970SChristoph Hellwig 	if (ret)
638a07b4970SChristoph Hellwig 		goto out;
639a07b4970SChristoph Hellwig 
640a07b4970SChristoph Hellwig 	ret = -EINVAL;
6415ba89503SMikhail Skorzhinskii 	if (nsid == 0 || nsid == NVME_NSID_ALL) {
6425ba89503SMikhail Skorzhinskii 		pr_err("invalid nsid %#x", nsid);
643a07b4970SChristoph Hellwig 		goto out;
6445ba89503SMikhail Skorzhinskii 	}
645a07b4970SChristoph Hellwig 
646a07b4970SChristoph Hellwig 	ret = -ENOMEM;
647a07b4970SChristoph Hellwig 	ns = nvmet_ns_alloc(subsys, nsid);
648a07b4970SChristoph Hellwig 	if (!ns)
649a07b4970SChristoph Hellwig 		goto out;
650a07b4970SChristoph Hellwig 	config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
651a07b4970SChristoph Hellwig 
652a07b4970SChristoph Hellwig 	pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
653a07b4970SChristoph Hellwig 
654a07b4970SChristoph Hellwig 	return &ns->group;
655a07b4970SChristoph Hellwig out:
656a07b4970SChristoph Hellwig 	return ERR_PTR(ret);
657a07b4970SChristoph Hellwig }
658a07b4970SChristoph Hellwig 
659a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_namespaces_group_ops = {
660a07b4970SChristoph Hellwig 	.make_group		= nvmet_ns_make,
661a07b4970SChristoph Hellwig };
662a07b4970SChristoph Hellwig 
66366603a31SBhumika Goyal static const struct config_item_type nvmet_namespaces_type = {
664a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_namespaces_group_ops,
665a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
666a07b4970SChristoph Hellwig };
667a07b4970SChristoph Hellwig 
668cae5b01aSLogan Gunthorpe #ifdef CONFIG_NVME_TARGET_PASSTHRU
669cae5b01aSLogan Gunthorpe 
670cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
671cae5b01aSLogan Gunthorpe 		char *page)
672cae5b01aSLogan Gunthorpe {
673cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
674cae5b01aSLogan Gunthorpe 
675cae5b01aSLogan Gunthorpe 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
676cae5b01aSLogan Gunthorpe }
677cae5b01aSLogan Gunthorpe 
678cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
679cae5b01aSLogan Gunthorpe 		const char *page, size_t count)
680cae5b01aSLogan Gunthorpe {
681cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
682cae5b01aSLogan Gunthorpe 	size_t len;
683cae5b01aSLogan Gunthorpe 	int ret;
684cae5b01aSLogan Gunthorpe 
685cae5b01aSLogan Gunthorpe 	mutex_lock(&subsys->lock);
686cae5b01aSLogan Gunthorpe 
687cae5b01aSLogan Gunthorpe 	ret = -EBUSY;
688cae5b01aSLogan Gunthorpe 	if (subsys->passthru_ctrl)
689cae5b01aSLogan Gunthorpe 		goto out_unlock;
690cae5b01aSLogan Gunthorpe 
691cae5b01aSLogan Gunthorpe 	ret = -EINVAL;
692cae5b01aSLogan Gunthorpe 	len = strcspn(page, "\n");
693cae5b01aSLogan Gunthorpe 	if (!len)
694cae5b01aSLogan Gunthorpe 		goto out_unlock;
695cae5b01aSLogan Gunthorpe 
696cae5b01aSLogan Gunthorpe 	kfree(subsys->passthru_ctrl_path);
697cae5b01aSLogan Gunthorpe 	ret = -ENOMEM;
698cae5b01aSLogan Gunthorpe 	subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
699cae5b01aSLogan Gunthorpe 	if (!subsys->passthru_ctrl_path)
700cae5b01aSLogan Gunthorpe 		goto out_unlock;
701cae5b01aSLogan Gunthorpe 
702cae5b01aSLogan Gunthorpe 	mutex_unlock(&subsys->lock);
703cae5b01aSLogan Gunthorpe 
704cae5b01aSLogan Gunthorpe 	return count;
705cae5b01aSLogan Gunthorpe out_unlock:
706cae5b01aSLogan Gunthorpe 	mutex_unlock(&subsys->lock);
707cae5b01aSLogan Gunthorpe 	return ret;
708cae5b01aSLogan Gunthorpe }
709cae5b01aSLogan Gunthorpe CONFIGFS_ATTR(nvmet_passthru_, device_path);
710cae5b01aSLogan Gunthorpe 
711cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_enable_show(struct config_item *item,
712cae5b01aSLogan Gunthorpe 		char *page)
713cae5b01aSLogan Gunthorpe {
714cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
715cae5b01aSLogan Gunthorpe 
716cae5b01aSLogan Gunthorpe 	return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
717cae5b01aSLogan Gunthorpe }
718cae5b01aSLogan Gunthorpe 
719cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_enable_store(struct config_item *item,
720cae5b01aSLogan Gunthorpe 		const char *page, size_t count)
721cae5b01aSLogan Gunthorpe {
722cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
723cae5b01aSLogan Gunthorpe 	bool enable;
724cae5b01aSLogan Gunthorpe 	int ret = 0;
725cae5b01aSLogan Gunthorpe 
726cae5b01aSLogan Gunthorpe 	if (strtobool(page, &enable))
727cae5b01aSLogan Gunthorpe 		return -EINVAL;
728cae5b01aSLogan Gunthorpe 
729cae5b01aSLogan Gunthorpe 	if (enable)
730cae5b01aSLogan Gunthorpe 		ret = nvmet_passthru_ctrl_enable(subsys);
731cae5b01aSLogan Gunthorpe 	else
732cae5b01aSLogan Gunthorpe 		nvmet_passthru_ctrl_disable(subsys);
733cae5b01aSLogan Gunthorpe 
734cae5b01aSLogan Gunthorpe 	return ret ? ret : count;
735cae5b01aSLogan Gunthorpe }
736cae5b01aSLogan Gunthorpe CONFIGFS_ATTR(nvmet_passthru_, enable);
737cae5b01aSLogan Gunthorpe 
738a2f6a2b8SChaitanya Kulkarni static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
739a2f6a2b8SChaitanya Kulkarni 		char *page)
740a2f6a2b8SChaitanya Kulkarni {
741a2f6a2b8SChaitanya Kulkarni 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->admin_timeout);
742a2f6a2b8SChaitanya Kulkarni }
743a2f6a2b8SChaitanya Kulkarni 
744a2f6a2b8SChaitanya Kulkarni static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
745a2f6a2b8SChaitanya Kulkarni 		const char *page, size_t count)
746a2f6a2b8SChaitanya Kulkarni {
747a2f6a2b8SChaitanya Kulkarni 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
748a2f6a2b8SChaitanya Kulkarni 	unsigned int timeout;
749a2f6a2b8SChaitanya Kulkarni 
750a2f6a2b8SChaitanya Kulkarni 	if (kstrtouint(page, 0, &timeout))
751a2f6a2b8SChaitanya Kulkarni 		return -EINVAL;
752a2f6a2b8SChaitanya Kulkarni 	subsys->admin_timeout = timeout;
753a2f6a2b8SChaitanya Kulkarni 	return count;
754a2f6a2b8SChaitanya Kulkarni }
755a2f6a2b8SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
756a2f6a2b8SChaitanya Kulkarni 
75747e9730cSChaitanya Kulkarni static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
75847e9730cSChaitanya Kulkarni 		char *page)
75947e9730cSChaitanya Kulkarni {
76047e9730cSChaitanya Kulkarni 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->io_timeout);
76147e9730cSChaitanya Kulkarni }
76247e9730cSChaitanya Kulkarni 
76347e9730cSChaitanya Kulkarni static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
76447e9730cSChaitanya Kulkarni 		const char *page, size_t count)
76547e9730cSChaitanya Kulkarni {
76647e9730cSChaitanya Kulkarni 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
76747e9730cSChaitanya Kulkarni 	unsigned int timeout;
76847e9730cSChaitanya Kulkarni 
76947e9730cSChaitanya Kulkarni 	if (kstrtouint(page, 0, &timeout))
77047e9730cSChaitanya Kulkarni 		return -EINVAL;
77147e9730cSChaitanya Kulkarni 	subsys->io_timeout = timeout;
77247e9730cSChaitanya Kulkarni 	return count;
77347e9730cSChaitanya Kulkarni }
77447e9730cSChaitanya Kulkarni CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
77547e9730cSChaitanya Kulkarni 
776*34ad6151SAlan Adamson static ssize_t nvmet_passthru_clear_ids_show(struct config_item *item,
777*34ad6151SAlan Adamson 		char *page)
778*34ad6151SAlan Adamson {
779*34ad6151SAlan Adamson 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->clear_ids);
780*34ad6151SAlan Adamson }
781*34ad6151SAlan Adamson 
782*34ad6151SAlan Adamson static ssize_t nvmet_passthru_clear_ids_store(struct config_item *item,
783*34ad6151SAlan Adamson 		const char *page, size_t count)
784*34ad6151SAlan Adamson {
785*34ad6151SAlan Adamson 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
786*34ad6151SAlan Adamson 	unsigned int clear_ids;
787*34ad6151SAlan Adamson 
788*34ad6151SAlan Adamson 	if (kstrtouint(page, 0, &clear_ids))
789*34ad6151SAlan Adamson 		return -EINVAL;
790*34ad6151SAlan Adamson 	subsys->clear_ids = clear_ids;
791*34ad6151SAlan Adamson 	return count;
792*34ad6151SAlan Adamson }
793*34ad6151SAlan Adamson CONFIGFS_ATTR(nvmet_passthru_, clear_ids);
794*34ad6151SAlan Adamson 
795cae5b01aSLogan Gunthorpe static struct configfs_attribute *nvmet_passthru_attrs[] = {
796cae5b01aSLogan Gunthorpe 	&nvmet_passthru_attr_device_path,
797cae5b01aSLogan Gunthorpe 	&nvmet_passthru_attr_enable,
798a2f6a2b8SChaitanya Kulkarni 	&nvmet_passthru_attr_admin_timeout,
79947e9730cSChaitanya Kulkarni 	&nvmet_passthru_attr_io_timeout,
800*34ad6151SAlan Adamson 	&nvmet_passthru_attr_clear_ids,
801cae5b01aSLogan Gunthorpe 	NULL,
802cae5b01aSLogan Gunthorpe };
803cae5b01aSLogan Gunthorpe 
804cae5b01aSLogan Gunthorpe static const struct config_item_type nvmet_passthru_type = {
805cae5b01aSLogan Gunthorpe 	.ct_attrs		= nvmet_passthru_attrs,
806cae5b01aSLogan Gunthorpe 	.ct_owner		= THIS_MODULE,
807cae5b01aSLogan Gunthorpe };
808cae5b01aSLogan Gunthorpe 
809cae5b01aSLogan Gunthorpe static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
810cae5b01aSLogan Gunthorpe {
811cae5b01aSLogan Gunthorpe 	config_group_init_type_name(&subsys->passthru_group,
812cae5b01aSLogan Gunthorpe 				    "passthru", &nvmet_passthru_type);
813cae5b01aSLogan Gunthorpe 	configfs_add_default_group(&subsys->passthru_group,
814cae5b01aSLogan Gunthorpe 				   &subsys->group);
815cae5b01aSLogan Gunthorpe }
816cae5b01aSLogan Gunthorpe 
817cae5b01aSLogan Gunthorpe #else /* CONFIG_NVME_TARGET_PASSTHRU */
818cae5b01aSLogan Gunthorpe 
819cae5b01aSLogan Gunthorpe static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
820cae5b01aSLogan Gunthorpe {
821cae5b01aSLogan Gunthorpe }
822cae5b01aSLogan Gunthorpe 
823cae5b01aSLogan Gunthorpe #endif /* CONFIG_NVME_TARGET_PASSTHRU */
824cae5b01aSLogan Gunthorpe 
825a07b4970SChristoph Hellwig static int nvmet_port_subsys_allow_link(struct config_item *parent,
826a07b4970SChristoph Hellwig 		struct config_item *target)
827a07b4970SChristoph Hellwig {
828a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
829a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys;
830a07b4970SChristoph Hellwig 	struct nvmet_subsys_link *link, *p;
831a07b4970SChristoph Hellwig 	int ret;
832a07b4970SChristoph Hellwig 
833a07b4970SChristoph Hellwig 	if (target->ci_type != &nvmet_subsys_type) {
834a07b4970SChristoph Hellwig 		pr_err("can only link subsystems into the subsystems dir.!\n");
835a07b4970SChristoph Hellwig 		return -EINVAL;
836a07b4970SChristoph Hellwig 	}
837a07b4970SChristoph Hellwig 	subsys = to_subsys(target);
838a07b4970SChristoph Hellwig 	link = kmalloc(sizeof(*link), GFP_KERNEL);
839a07b4970SChristoph Hellwig 	if (!link)
840a07b4970SChristoph Hellwig 		return -ENOMEM;
841a07b4970SChristoph Hellwig 	link->subsys = subsys;
842a07b4970SChristoph Hellwig 
843a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
844a07b4970SChristoph Hellwig 	ret = -EEXIST;
845a07b4970SChristoph Hellwig 	list_for_each_entry(p, &port->subsystems, entry) {
846a07b4970SChristoph Hellwig 		if (p->subsys == subsys)
847a07b4970SChristoph Hellwig 			goto out_free_link;
848a07b4970SChristoph Hellwig 	}
849a07b4970SChristoph Hellwig 
850a07b4970SChristoph Hellwig 	if (list_empty(&port->subsystems)) {
851a07b4970SChristoph Hellwig 		ret = nvmet_enable_port(port);
852a07b4970SChristoph Hellwig 		if (ret)
853a07b4970SChristoph Hellwig 			goto out_free_link;
854a07b4970SChristoph Hellwig 	}
855a07b4970SChristoph Hellwig 
856a07b4970SChristoph Hellwig 	list_add_tail(&link->entry, &port->subsystems);
857b662a078SJay Sternberg 	nvmet_port_disc_changed(port, subsys);
858b662a078SJay Sternberg 
859a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
860a07b4970SChristoph Hellwig 	return 0;
861a07b4970SChristoph Hellwig 
862a07b4970SChristoph Hellwig out_free_link:
863a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
864a07b4970SChristoph Hellwig 	kfree(link);
865a07b4970SChristoph Hellwig 	return ret;
866a07b4970SChristoph Hellwig }
867a07b4970SChristoph Hellwig 
868e16769d4SAndrzej Pietrasiewicz static void nvmet_port_subsys_drop_link(struct config_item *parent,
869a07b4970SChristoph Hellwig 		struct config_item *target)
870a07b4970SChristoph Hellwig {
871a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
872a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(target);
873a07b4970SChristoph Hellwig 	struct nvmet_subsys_link *p;
874a07b4970SChristoph Hellwig 
875a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
876a07b4970SChristoph Hellwig 	list_for_each_entry(p, &port->subsystems, entry) {
877a07b4970SChristoph Hellwig 		if (p->subsys == subsys)
878a07b4970SChristoph Hellwig 			goto found;
879a07b4970SChristoph Hellwig 	}
880a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
881e16769d4SAndrzej Pietrasiewicz 	return;
882a07b4970SChristoph Hellwig 
883a07b4970SChristoph Hellwig found:
884a07b4970SChristoph Hellwig 	list_del(&p->entry);
8853aed8673SLogan Gunthorpe 	nvmet_port_del_ctrls(port, subsys);
886b662a078SJay Sternberg 	nvmet_port_disc_changed(port, subsys);
887b662a078SJay Sternberg 
888a07b4970SChristoph Hellwig 	if (list_empty(&port->subsystems))
889a07b4970SChristoph Hellwig 		nvmet_disable_port(port);
890a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
891a07b4970SChristoph Hellwig 	kfree(p);
892a07b4970SChristoph Hellwig }
893a07b4970SChristoph Hellwig 
894a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_subsys_item_ops = {
895a07b4970SChristoph Hellwig 	.allow_link		= nvmet_port_subsys_allow_link,
896a07b4970SChristoph Hellwig 	.drop_link		= nvmet_port_subsys_drop_link,
897a07b4970SChristoph Hellwig };
898a07b4970SChristoph Hellwig 
89966603a31SBhumika Goyal static const struct config_item_type nvmet_port_subsys_type = {
900a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_port_subsys_item_ops,
901a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
902a07b4970SChristoph Hellwig };
903a07b4970SChristoph Hellwig 
904a07b4970SChristoph Hellwig static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
905a07b4970SChristoph Hellwig 		struct config_item *target)
906a07b4970SChristoph Hellwig {
907a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
908a07b4970SChristoph Hellwig 	struct nvmet_host *host;
909a07b4970SChristoph Hellwig 	struct nvmet_host_link *link, *p;
910a07b4970SChristoph Hellwig 	int ret;
911a07b4970SChristoph Hellwig 
912a07b4970SChristoph Hellwig 	if (target->ci_type != &nvmet_host_type) {
913a07b4970SChristoph Hellwig 		pr_err("can only link hosts into the allowed_hosts directory!\n");
914a07b4970SChristoph Hellwig 		return -EINVAL;
915a07b4970SChristoph Hellwig 	}
916a07b4970SChristoph Hellwig 
917a07b4970SChristoph Hellwig 	host = to_host(target);
918a07b4970SChristoph Hellwig 	link = kmalloc(sizeof(*link), GFP_KERNEL);
919a07b4970SChristoph Hellwig 	if (!link)
920a07b4970SChristoph Hellwig 		return -ENOMEM;
921a07b4970SChristoph Hellwig 	link->host = host;
922a07b4970SChristoph Hellwig 
923a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
924a07b4970SChristoph Hellwig 	ret = -EINVAL;
925a07b4970SChristoph Hellwig 	if (subsys->allow_any_host) {
926a07b4970SChristoph Hellwig 		pr_err("can't add hosts when allow_any_host is set!\n");
927a07b4970SChristoph Hellwig 		goto out_free_link;
928a07b4970SChristoph Hellwig 	}
929a07b4970SChristoph Hellwig 
930a07b4970SChristoph Hellwig 	ret = -EEXIST;
931a07b4970SChristoph Hellwig 	list_for_each_entry(p, &subsys->hosts, entry) {
932a07b4970SChristoph Hellwig 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
933a07b4970SChristoph Hellwig 			goto out_free_link;
934a07b4970SChristoph Hellwig 	}
935a07b4970SChristoph Hellwig 	list_add_tail(&link->entry, &subsys->hosts);
936b662a078SJay Sternberg 	nvmet_subsys_disc_changed(subsys, host);
937b662a078SJay Sternberg 
938a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
939a07b4970SChristoph Hellwig 	return 0;
940a07b4970SChristoph Hellwig out_free_link:
941a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
942a07b4970SChristoph Hellwig 	kfree(link);
943a07b4970SChristoph Hellwig 	return ret;
944a07b4970SChristoph Hellwig }
945a07b4970SChristoph Hellwig 
946e16769d4SAndrzej Pietrasiewicz static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
947a07b4970SChristoph Hellwig 		struct config_item *target)
948a07b4970SChristoph Hellwig {
949a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
950a07b4970SChristoph Hellwig 	struct nvmet_host *host = to_host(target);
951a07b4970SChristoph Hellwig 	struct nvmet_host_link *p;
952a07b4970SChristoph Hellwig 
953a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
954a07b4970SChristoph Hellwig 	list_for_each_entry(p, &subsys->hosts, entry) {
955a07b4970SChristoph Hellwig 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
956a07b4970SChristoph Hellwig 			goto found;
957a07b4970SChristoph Hellwig 	}
958a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
959e16769d4SAndrzej Pietrasiewicz 	return;
960a07b4970SChristoph Hellwig 
961a07b4970SChristoph Hellwig found:
962a07b4970SChristoph Hellwig 	list_del(&p->entry);
963b662a078SJay Sternberg 	nvmet_subsys_disc_changed(subsys, host);
964b662a078SJay Sternberg 
965a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
966a07b4970SChristoph Hellwig 	kfree(p);
967a07b4970SChristoph Hellwig }
968a07b4970SChristoph Hellwig 
969a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
970a07b4970SChristoph Hellwig 	.allow_link		= nvmet_allowed_hosts_allow_link,
971a07b4970SChristoph Hellwig 	.drop_link		= nvmet_allowed_hosts_drop_link,
972a07b4970SChristoph Hellwig };
973a07b4970SChristoph Hellwig 
97466603a31SBhumika Goyal static const struct config_item_type nvmet_allowed_hosts_type = {
975a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_allowed_hosts_item_ops,
976a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
977a07b4970SChristoph Hellwig };
978a07b4970SChristoph Hellwig 
979a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
980a07b4970SChristoph Hellwig 		char *page)
981a07b4970SChristoph Hellwig {
982a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n",
983a07b4970SChristoph Hellwig 		to_subsys(item)->allow_any_host);
984a07b4970SChristoph Hellwig }
985a07b4970SChristoph Hellwig 
986a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
987a07b4970SChristoph Hellwig 		const char *page, size_t count)
988a07b4970SChristoph Hellwig {
989a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(item);
990a07b4970SChristoph Hellwig 	bool allow_any_host;
991a07b4970SChristoph Hellwig 	int ret = 0;
992a07b4970SChristoph Hellwig 
993a07b4970SChristoph Hellwig 	if (strtobool(page, &allow_any_host))
994a07b4970SChristoph Hellwig 		return -EINVAL;
995a07b4970SChristoph Hellwig 
996a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
997a07b4970SChristoph Hellwig 	if (allow_any_host && !list_empty(&subsys->hosts)) {
998a07b4970SChristoph Hellwig 		pr_err("Can't set allow_any_host when explicit hosts are set!\n");
999a07b4970SChristoph Hellwig 		ret = -EINVAL;
1000a07b4970SChristoph Hellwig 		goto out_unlock;
1001a07b4970SChristoph Hellwig 	}
1002a07b4970SChristoph Hellwig 
1003b662a078SJay Sternberg 	if (subsys->allow_any_host != allow_any_host) {
1004a07b4970SChristoph Hellwig 		subsys->allow_any_host = allow_any_host;
1005b662a078SJay Sternberg 		nvmet_subsys_disc_changed(subsys, NULL);
1006b662a078SJay Sternberg 	}
1007b662a078SJay Sternberg 
1008a07b4970SChristoph Hellwig out_unlock:
1009a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
1010a07b4970SChristoph Hellwig 	return ret ? ret : count;
1011a07b4970SChristoph Hellwig }
1012a07b4970SChristoph Hellwig 
1013a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
1014a07b4970SChristoph Hellwig 
101541528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
1016c61d788bSJohannes Thumshirn 					      char *page)
1017c61d788bSJohannes Thumshirn {
1018c61d788bSJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
1019c61d788bSJohannes Thumshirn 
1020c61d788bSJohannes Thumshirn 	if (NVME_TERTIARY(subsys->ver))
1021a0f0dbaaSChaitanya Kulkarni 		return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
1022a0f0dbaaSChaitanya Kulkarni 				NVME_MAJOR(subsys->ver),
1023a0f0dbaaSChaitanya Kulkarni 				NVME_MINOR(subsys->ver),
1024a0f0dbaaSChaitanya Kulkarni 				NVME_TERTIARY(subsys->ver));
1025527123c7SChaitanya Kulkarni 
1026a0f0dbaaSChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
1027a0f0dbaaSChaitanya Kulkarni 			NVME_MAJOR(subsys->ver),
1028a0f0dbaaSChaitanya Kulkarni 			NVME_MINOR(subsys->ver));
1029c61d788bSJohannes Thumshirn }
1030c61d788bSJohannes Thumshirn 
103187fd4cc1SNoam Gottlieb static ssize_t
103287fd4cc1SNoam Gottlieb nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,
1033c61d788bSJohannes Thumshirn 		const char *page, size_t count)
1034c61d788bSJohannes Thumshirn {
1035c61d788bSJohannes Thumshirn 	int major, minor, tertiary = 0;
1036c61d788bSJohannes Thumshirn 	int ret;
1037c61d788bSJohannes Thumshirn 
103887fd4cc1SNoam Gottlieb 	if (subsys->subsys_discovered) {
103987fd4cc1SNoam Gottlieb 		if (NVME_TERTIARY(subsys->ver))
104087fd4cc1SNoam Gottlieb 			pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
104187fd4cc1SNoam Gottlieb 			       NVME_MAJOR(subsys->ver),
104287fd4cc1SNoam Gottlieb 			       NVME_MINOR(subsys->ver),
104387fd4cc1SNoam Gottlieb 			       NVME_TERTIARY(subsys->ver));
104487fd4cc1SNoam Gottlieb 		else
104587fd4cc1SNoam Gottlieb 			pr_err("Can't set version number. %llu.%llu is already assigned\n",
104687fd4cc1SNoam Gottlieb 			       NVME_MAJOR(subsys->ver),
104787fd4cc1SNoam Gottlieb 			       NVME_MINOR(subsys->ver));
104887fd4cc1SNoam Gottlieb 		return -EINVAL;
104987fd4cc1SNoam Gottlieb 	}
105087fd4cc1SNoam Gottlieb 
1051ba76af67SLogan Gunthorpe 	/* passthru subsystems use the underlying controller's version */
1052ab7a2737SChristoph Hellwig 	if (nvmet_is_passthru_subsys(subsys))
1053ba76af67SLogan Gunthorpe 		return -EINVAL;
1054ba76af67SLogan Gunthorpe 
1055c61d788bSJohannes Thumshirn 	ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
1056c61d788bSJohannes Thumshirn 	if (ret != 2 && ret != 3)
1057c61d788bSJohannes Thumshirn 		return -EINVAL;
1058c61d788bSJohannes Thumshirn 
1059c61d788bSJohannes Thumshirn 	subsys->ver = NVME_VS(major, minor, tertiary);
1060c61d788bSJohannes Thumshirn 
1061c61d788bSJohannes Thumshirn 	return count;
1062c61d788bSJohannes Thumshirn }
106387fd4cc1SNoam Gottlieb 
106487fd4cc1SNoam Gottlieb static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
106587fd4cc1SNoam Gottlieb 					       const char *page, size_t count)
106687fd4cc1SNoam Gottlieb {
106787fd4cc1SNoam Gottlieb 	struct nvmet_subsys *subsys = to_subsys(item);
106887fd4cc1SNoam Gottlieb 	ssize_t ret;
106987fd4cc1SNoam Gottlieb 
107087fd4cc1SNoam Gottlieb 	down_write(&nvmet_config_sem);
107187fd4cc1SNoam Gottlieb 	mutex_lock(&subsys->lock);
107287fd4cc1SNoam Gottlieb 	ret = nvmet_subsys_attr_version_store_locked(subsys, page, count);
107387fd4cc1SNoam Gottlieb 	mutex_unlock(&subsys->lock);
107487fd4cc1SNoam Gottlieb 	up_write(&nvmet_config_sem);
107587fd4cc1SNoam Gottlieb 
107687fd4cc1SNoam Gottlieb 	return ret;
107787fd4cc1SNoam Gottlieb }
107841528f80SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_version);
1079c61d788bSJohannes Thumshirn 
1080e13b0615SNoam Gottlieb /* See Section 1.5 of NVMe 1.4 */
1081e13b0615SNoam Gottlieb static bool nvmet_is_ascii(const char c)
1082e13b0615SNoam Gottlieb {
1083e13b0615SNoam Gottlieb 	return c >= 0x20 && c <= 0x7e;
1084e13b0615SNoam Gottlieb }
1085e13b0615SNoam Gottlieb 
1086fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
1087fcbc5459SJohannes Thumshirn 					     char *page)
1088fcbc5459SJohannes Thumshirn {
1089fcbc5459SJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
1090fcbc5459SJohannes Thumshirn 
10910bd46e22SDan Carpenter 	return snprintf(page, PAGE_SIZE, "%.*s\n",
1092f0406481SHannes Reinecke 			NVMET_SN_MAX_SIZE, subsys->serial);
1093fcbc5459SJohannes Thumshirn }
1094fcbc5459SJohannes Thumshirn 
10957ae023c5SNoam Gottlieb static ssize_t
10967ae023c5SNoam Gottlieb nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
1097fcbc5459SJohannes Thumshirn 		const char *page, size_t count)
1098fcbc5459SJohannes Thumshirn {
1099e13b0615SNoam Gottlieb 	int pos, len = strcspn(page, "\n");
1100d3a9b0caSChaitanya Kulkarni 
11017ae023c5SNoam Gottlieb 	if (subsys->subsys_discovered) {
11027ae023c5SNoam Gottlieb 		pr_err("Can't set serial number. %s is already assigned\n",
11037ae023c5SNoam Gottlieb 		       subsys->serial);
11047ae023c5SNoam Gottlieb 		return -EINVAL;
11057ae023c5SNoam Gottlieb 	}
11067ae023c5SNoam Gottlieb 
1107e13b0615SNoam Gottlieb 	if (!len || len > NVMET_SN_MAX_SIZE) {
1108e13b0615SNoam Gottlieb 		pr_err("Serial Number can not be empty or exceed %d Bytes\n",
1109e13b0615SNoam Gottlieb 		       NVMET_SN_MAX_SIZE);
1110d3a9b0caSChaitanya Kulkarni 		return -EINVAL;
1111e13b0615SNoam Gottlieb 	}
1112e13b0615SNoam Gottlieb 
1113e13b0615SNoam Gottlieb 	for (pos = 0; pos < len; pos++) {
1114e13b0615SNoam Gottlieb 		if (!nvmet_is_ascii(page[pos])) {
1115e13b0615SNoam Gottlieb 			pr_err("Serial Number must contain only ASCII strings\n");
1116e13b0615SNoam Gottlieb 			return -EINVAL;
1117e13b0615SNoam Gottlieb 		}
1118e13b0615SNoam Gottlieb 	}
1119fcbc5459SJohannes Thumshirn 
11207ae023c5SNoam Gottlieb 	memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
11217ae023c5SNoam Gottlieb 
11227ae023c5SNoam Gottlieb 	return count;
11237ae023c5SNoam Gottlieb }
11247ae023c5SNoam Gottlieb 
11257ae023c5SNoam Gottlieb static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
11267ae023c5SNoam Gottlieb 					      const char *page, size_t count)
11277ae023c5SNoam Gottlieb {
11287ae023c5SNoam Gottlieb 	struct nvmet_subsys *subsys = to_subsys(item);
11297ae023c5SNoam Gottlieb 	ssize_t ret;
11307ae023c5SNoam Gottlieb 
1131fcbc5459SJohannes Thumshirn 	down_write(&nvmet_config_sem);
1132e13b0615SNoam Gottlieb 	mutex_lock(&subsys->lock);
11337ae023c5SNoam Gottlieb 	ret = nvmet_subsys_attr_serial_store_locked(subsys, page, count);
1134e13b0615SNoam Gottlieb 	mutex_unlock(&subsys->lock);
1135fcbc5459SJohannes Thumshirn 	up_write(&nvmet_config_sem);
1136fcbc5459SJohannes Thumshirn 
11377ae023c5SNoam Gottlieb 	return ret;
1138fcbc5459SJohannes Thumshirn }
1139fcbc5459SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
1140fcbc5459SJohannes Thumshirn 
114194a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
114294a39d61SChaitanya Kulkarni 						 char *page)
114394a39d61SChaitanya Kulkarni {
114494a39d61SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
114594a39d61SChaitanya Kulkarni }
114694a39d61SChaitanya Kulkarni 
114794a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
114894a39d61SChaitanya Kulkarni 						  const char *page, size_t cnt)
114994a39d61SChaitanya Kulkarni {
115094a39d61SChaitanya Kulkarni 	u16 cntlid_min;
115194a39d61SChaitanya Kulkarni 
115294a39d61SChaitanya Kulkarni 	if (sscanf(page, "%hu\n", &cntlid_min) != 1)
115394a39d61SChaitanya Kulkarni 		return -EINVAL;
115494a39d61SChaitanya Kulkarni 
115594a39d61SChaitanya Kulkarni 	if (cntlid_min == 0)
115694a39d61SChaitanya Kulkarni 		return -EINVAL;
115794a39d61SChaitanya Kulkarni 
115894a39d61SChaitanya Kulkarni 	down_write(&nvmet_config_sem);
115994a39d61SChaitanya Kulkarni 	if (cntlid_min >= to_subsys(item)->cntlid_max)
116094a39d61SChaitanya Kulkarni 		goto out_unlock;
116194a39d61SChaitanya Kulkarni 	to_subsys(item)->cntlid_min = cntlid_min;
116294a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
116394a39d61SChaitanya Kulkarni 	return cnt;
116494a39d61SChaitanya Kulkarni 
116594a39d61SChaitanya Kulkarni out_unlock:
116694a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
116794a39d61SChaitanya Kulkarni 	return -EINVAL;
116894a39d61SChaitanya Kulkarni }
116994a39d61SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
117094a39d61SChaitanya Kulkarni 
117194a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
117294a39d61SChaitanya Kulkarni 						 char *page)
117394a39d61SChaitanya Kulkarni {
117494a39d61SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
117594a39d61SChaitanya Kulkarni }
117694a39d61SChaitanya Kulkarni 
117794a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
117894a39d61SChaitanya Kulkarni 						  const char *page, size_t cnt)
117994a39d61SChaitanya Kulkarni {
118094a39d61SChaitanya Kulkarni 	u16 cntlid_max;
118194a39d61SChaitanya Kulkarni 
118294a39d61SChaitanya Kulkarni 	if (sscanf(page, "%hu\n", &cntlid_max) != 1)
118394a39d61SChaitanya Kulkarni 		return -EINVAL;
118494a39d61SChaitanya Kulkarni 
118594a39d61SChaitanya Kulkarni 	if (cntlid_max == 0)
118694a39d61SChaitanya Kulkarni 		return -EINVAL;
118794a39d61SChaitanya Kulkarni 
118894a39d61SChaitanya Kulkarni 	down_write(&nvmet_config_sem);
118994a39d61SChaitanya Kulkarni 	if (cntlid_max <= to_subsys(item)->cntlid_min)
119094a39d61SChaitanya Kulkarni 		goto out_unlock;
119194a39d61SChaitanya Kulkarni 	to_subsys(item)->cntlid_max = cntlid_max;
119294a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
119394a39d61SChaitanya Kulkarni 	return cnt;
119494a39d61SChaitanya Kulkarni 
119594a39d61SChaitanya Kulkarni out_unlock:
119694a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
119794a39d61SChaitanya Kulkarni 	return -EINVAL;
119894a39d61SChaitanya Kulkarni }
119994a39d61SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
120094a39d61SChaitanya Kulkarni 
1201013b7ebeSMark Ruijter static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
1202013b7ebeSMark Ruijter 					    char *page)
1203013b7ebeSMark Ruijter {
1204013b7ebeSMark Ruijter 	struct nvmet_subsys *subsys = to_subsys(item);
1205013b7ebeSMark Ruijter 
12060d148efdSNoam Gottlieb 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->model_number);
1207013b7ebeSMark Ruijter }
1208013b7ebeSMark Ruijter 
1209d9f273b7SMax Gurtovoy static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
1210013b7ebeSMark Ruijter 		const char *page, size_t count)
1211013b7ebeSMark Ruijter {
1212013b7ebeSMark Ruijter 	int pos = 0, len;
1213013b7ebeSMark Ruijter 
12140d148efdSNoam Gottlieb 	if (subsys->subsys_discovered) {
1215d9f273b7SMax Gurtovoy 		pr_err("Can't set model number. %s is already assigned\n",
1216d9f273b7SMax Gurtovoy 		       subsys->model_number);
1217d9f273b7SMax Gurtovoy 		return -EINVAL;
1218d9f273b7SMax Gurtovoy 	}
1219d9f273b7SMax Gurtovoy 
1220013b7ebeSMark Ruijter 	len = strcspn(page, "\n");
1221013b7ebeSMark Ruijter 	if (!len)
1222013b7ebeSMark Ruijter 		return -EINVAL;
1223013b7ebeSMark Ruijter 
122448b4c010SNoam Gottlieb 	if (len > NVMET_MN_MAX_SIZE) {
1225ccc1003bSColin Ian King 		pr_err("Model number size can not exceed %d Bytes\n",
122648b4c010SNoam Gottlieb 		       NVMET_MN_MAX_SIZE);
122748b4c010SNoam Gottlieb 		return -EINVAL;
122848b4c010SNoam Gottlieb 	}
122948b4c010SNoam Gottlieb 
1230013b7ebeSMark Ruijter 	for (pos = 0; pos < len; pos++) {
1231013b7ebeSMark Ruijter 		if (!nvmet_is_ascii(page[pos]))
1232013b7ebeSMark Ruijter 			return -EINVAL;
1233013b7ebeSMark Ruijter 	}
1234013b7ebeSMark Ruijter 
1235d9f273b7SMax Gurtovoy 	subsys->model_number = kmemdup_nul(page, len, GFP_KERNEL);
1236d9f273b7SMax Gurtovoy 	if (!subsys->model_number)
1237013b7ebeSMark Ruijter 		return -ENOMEM;
1238d9f273b7SMax Gurtovoy 	return count;
1239013b7ebeSMark Ruijter }
1240d9f273b7SMax Gurtovoy 
1241d9f273b7SMax Gurtovoy static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
1242d9f273b7SMax Gurtovoy 					     const char *page, size_t count)
1243d9f273b7SMax Gurtovoy {
1244d9f273b7SMax Gurtovoy 	struct nvmet_subsys *subsys = to_subsys(item);
1245d9f273b7SMax Gurtovoy 	ssize_t ret;
1246013b7ebeSMark Ruijter 
1247013b7ebeSMark Ruijter 	down_write(&nvmet_config_sem);
1248013b7ebeSMark Ruijter 	mutex_lock(&subsys->lock);
1249d9f273b7SMax Gurtovoy 	ret = nvmet_subsys_attr_model_store_locked(subsys, page, count);
1250013b7ebeSMark Ruijter 	mutex_unlock(&subsys->lock);
1251013b7ebeSMark Ruijter 	up_write(&nvmet_config_sem);
1252013b7ebeSMark Ruijter 
1253d9f273b7SMax Gurtovoy 	return ret;
1254013b7ebeSMark Ruijter }
1255013b7ebeSMark Ruijter CONFIGFS_ATTR(nvmet_subsys_, attr_model);
1256013b7ebeSMark Ruijter 
1257ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1258ea52ac1cSIsrael Rukshin static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
1259ea52ac1cSIsrael Rukshin 						char *page)
1260ea52ac1cSIsrael Rukshin {
1261ea52ac1cSIsrael Rukshin 	return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
1262ea52ac1cSIsrael Rukshin }
1263ea52ac1cSIsrael Rukshin 
1264ea52ac1cSIsrael Rukshin static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
1265ea52ac1cSIsrael Rukshin 						 const char *page, size_t count)
1266ea52ac1cSIsrael Rukshin {
1267ea52ac1cSIsrael Rukshin 	struct nvmet_subsys *subsys = to_subsys(item);
1268ea52ac1cSIsrael Rukshin 	bool pi_enable;
1269ea52ac1cSIsrael Rukshin 
1270ea52ac1cSIsrael Rukshin 	if (strtobool(page, &pi_enable))
1271ea52ac1cSIsrael Rukshin 		return -EINVAL;
1272ea52ac1cSIsrael Rukshin 
1273ea52ac1cSIsrael Rukshin 	subsys->pi_support = pi_enable;
1274ea52ac1cSIsrael Rukshin 	return count;
1275ea52ac1cSIsrael Rukshin }
1276ea52ac1cSIsrael Rukshin CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
1277ea52ac1cSIsrael Rukshin #endif
1278ea52ac1cSIsrael Rukshin 
1279a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_subsys_attrs[] = {
1280a07b4970SChristoph Hellwig 	&nvmet_subsys_attr_attr_allow_any_host,
128141528f80SJohannes Thumshirn 	&nvmet_subsys_attr_attr_version,
1282fcbc5459SJohannes Thumshirn 	&nvmet_subsys_attr_attr_serial,
128394a39d61SChaitanya Kulkarni 	&nvmet_subsys_attr_attr_cntlid_min,
128494a39d61SChaitanya Kulkarni 	&nvmet_subsys_attr_attr_cntlid_max,
1285013b7ebeSMark Ruijter 	&nvmet_subsys_attr_attr_model,
1286ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1287ea52ac1cSIsrael Rukshin 	&nvmet_subsys_attr_attr_pi_enable,
1288ea52ac1cSIsrael Rukshin #endif
1289a07b4970SChristoph Hellwig 	NULL,
1290a07b4970SChristoph Hellwig };
1291a07b4970SChristoph Hellwig 
1292a07b4970SChristoph Hellwig /*
1293a07b4970SChristoph Hellwig  * Subsystem structures & folder operation functions below
1294a07b4970SChristoph Hellwig  */
1295a07b4970SChristoph Hellwig static void nvmet_subsys_release(struct config_item *item)
1296a07b4970SChristoph Hellwig {
1297a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(item);
1298a07b4970SChristoph Hellwig 
1299344770b0SSagi Grimberg 	nvmet_subsys_del_ctrls(subsys);
1300a07b4970SChristoph Hellwig 	nvmet_subsys_put(subsys);
1301a07b4970SChristoph Hellwig }
1302a07b4970SChristoph Hellwig 
1303a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_subsys_item_ops = {
1304a07b4970SChristoph Hellwig 	.release		= nvmet_subsys_release,
1305a07b4970SChristoph Hellwig };
1306a07b4970SChristoph Hellwig 
130766603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type = {
1308a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_subsys_item_ops,
1309a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_subsys_attrs,
1310a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1311a07b4970SChristoph Hellwig };
1312a07b4970SChristoph Hellwig 
1313a07b4970SChristoph Hellwig static struct config_group *nvmet_subsys_make(struct config_group *group,
1314a07b4970SChristoph Hellwig 		const char *name)
1315a07b4970SChristoph Hellwig {
1316a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys;
1317a07b4970SChristoph Hellwig 
1318a07b4970SChristoph Hellwig 	if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1319a07b4970SChristoph Hellwig 		pr_err("can't create discovery subsystem through configfs\n");
1320a07b4970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1321a07b4970SChristoph Hellwig 	}
1322a07b4970SChristoph Hellwig 
1323a07b4970SChristoph Hellwig 	subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
13246b7e631bSMinwoo Im 	if (IS_ERR(subsys))
13256b7e631bSMinwoo Im 		return ERR_CAST(subsys);
1326a07b4970SChristoph Hellwig 
1327a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1328a07b4970SChristoph Hellwig 
1329a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->namespaces_group,
1330a07b4970SChristoph Hellwig 			"namespaces", &nvmet_namespaces_type);
1331a07b4970SChristoph Hellwig 	configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1332a07b4970SChristoph Hellwig 
1333a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->allowed_hosts_group,
1334a07b4970SChristoph Hellwig 			"allowed_hosts", &nvmet_allowed_hosts_type);
1335a07b4970SChristoph Hellwig 	configfs_add_default_group(&subsys->allowed_hosts_group,
1336a07b4970SChristoph Hellwig 			&subsys->group);
1337a07b4970SChristoph Hellwig 
1338cae5b01aSLogan Gunthorpe 	nvmet_add_passthru_group(subsys);
1339cae5b01aSLogan Gunthorpe 
1340a07b4970SChristoph Hellwig 	return &subsys->group;
1341a07b4970SChristoph Hellwig }
1342a07b4970SChristoph Hellwig 
1343a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_subsystems_group_ops = {
1344a07b4970SChristoph Hellwig 	.make_group		= nvmet_subsys_make,
1345a07b4970SChristoph Hellwig };
1346a07b4970SChristoph Hellwig 
134766603a31SBhumika Goyal static const struct config_item_type nvmet_subsystems_type = {
1348a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_subsystems_group_ops,
1349a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1350a07b4970SChristoph Hellwig };
1351a07b4970SChristoph Hellwig 
1352a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_show(struct config_item *item,
1353a07b4970SChristoph Hellwig 		char *page)
1354a07b4970SChristoph Hellwig {
1355a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1356a07b4970SChristoph Hellwig }
1357a07b4970SChristoph Hellwig 
1358a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_store(struct config_item *item,
1359a07b4970SChristoph Hellwig 		const char *page, size_t count)
1360a07b4970SChristoph Hellwig {
1361a07b4970SChristoph Hellwig 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1362a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1363a07b4970SChristoph Hellwig 	bool enable;
1364a07b4970SChristoph Hellwig 
1365a07b4970SChristoph Hellwig 	if (strtobool(page, &enable))
1366a07b4970SChristoph Hellwig 		goto inval;
1367a07b4970SChristoph Hellwig 
1368a07b4970SChristoph Hellwig 	if (enable)
1369a07b4970SChristoph Hellwig 		nvmet_referral_enable(parent, port);
1370a07b4970SChristoph Hellwig 	else
1371b662a078SJay Sternberg 		nvmet_referral_disable(parent, port);
1372a07b4970SChristoph Hellwig 
1373a07b4970SChristoph Hellwig 	return count;
1374a07b4970SChristoph Hellwig inval:
1375a07b4970SChristoph Hellwig 	pr_err("Invalid value '%s' for enable\n", page);
1376a07b4970SChristoph Hellwig 	return -EINVAL;
1377a07b4970SChristoph Hellwig }
1378a07b4970SChristoph Hellwig 
1379a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_referral_, enable);
1380a07b4970SChristoph Hellwig 
1381a07b4970SChristoph Hellwig /*
1382a07b4970SChristoph Hellwig  * Discovery Service subsystem definitions
1383a07b4970SChristoph Hellwig  */
1384a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_referral_attrs[] = {
1385a07b4970SChristoph Hellwig 	&nvmet_attr_addr_adrfam,
1386a07b4970SChristoph Hellwig 	&nvmet_attr_addr_portid,
1387a07b4970SChristoph Hellwig 	&nvmet_attr_addr_treq,
1388a07b4970SChristoph Hellwig 	&nvmet_attr_addr_traddr,
1389a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trsvcid,
1390a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trtype,
1391a07b4970SChristoph Hellwig 	&nvmet_referral_attr_enable,
1392a07b4970SChristoph Hellwig 	NULL,
1393a07b4970SChristoph Hellwig };
1394a07b4970SChristoph Hellwig 
1395f0e656e4SSagi Grimberg static void nvmet_referral_notify(struct config_group *group,
1396f0e656e4SSagi Grimberg 		struct config_item *item)
1397a07b4970SChristoph Hellwig {
1398b662a078SJay Sternberg 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1399a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1400a07b4970SChristoph Hellwig 
1401b662a078SJay Sternberg 	nvmet_referral_disable(parent, port);
1402f0e656e4SSagi Grimberg }
1403f0e656e4SSagi Grimberg 
1404f0e656e4SSagi Grimberg static void nvmet_referral_release(struct config_item *item)
1405f0e656e4SSagi Grimberg {
1406f0e656e4SSagi Grimberg 	struct nvmet_port *port = to_nvmet_port(item);
1407f0e656e4SSagi Grimberg 
1408a07b4970SChristoph Hellwig 	kfree(port);
1409a07b4970SChristoph Hellwig }
1410a07b4970SChristoph Hellwig 
1411a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_referral_item_ops = {
1412a07b4970SChristoph Hellwig 	.release	= nvmet_referral_release,
1413a07b4970SChristoph Hellwig };
1414a07b4970SChristoph Hellwig 
141566603a31SBhumika Goyal static const struct config_item_type nvmet_referral_type = {
1416a07b4970SChristoph Hellwig 	.ct_owner	= THIS_MODULE,
1417a07b4970SChristoph Hellwig 	.ct_attrs	= nvmet_referral_attrs,
1418a07b4970SChristoph Hellwig 	.ct_item_ops	= &nvmet_referral_item_ops,
1419a07b4970SChristoph Hellwig };
1420a07b4970SChristoph Hellwig 
1421a07b4970SChristoph Hellwig static struct config_group *nvmet_referral_make(
1422a07b4970SChristoph Hellwig 		struct config_group *group, const char *name)
1423a07b4970SChristoph Hellwig {
1424a07b4970SChristoph Hellwig 	struct nvmet_port *port;
1425a07b4970SChristoph Hellwig 
1426a07b4970SChristoph Hellwig 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1427a07b4970SChristoph Hellwig 	if (!port)
1428f98d9ca1SDan Carpenter 		return ERR_PTR(-ENOMEM);
1429a07b4970SChristoph Hellwig 
1430a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->entry);
1431a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1432a07b4970SChristoph Hellwig 
1433a07b4970SChristoph Hellwig 	return &port->group;
1434a07b4970SChristoph Hellwig }
1435a07b4970SChristoph Hellwig 
1436a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_referral_group_ops = {
1437a07b4970SChristoph Hellwig 	.make_group		= nvmet_referral_make,
1438f0e656e4SSagi Grimberg 	.disconnect_notify	= nvmet_referral_notify,
1439a07b4970SChristoph Hellwig };
1440a07b4970SChristoph Hellwig 
144166603a31SBhumika Goyal static const struct config_item_type nvmet_referrals_type = {
1442a07b4970SChristoph Hellwig 	.ct_owner	= THIS_MODULE,
1443a07b4970SChristoph Hellwig 	.ct_group_ops	= &nvmet_referral_group_ops,
1444a07b4970SChristoph Hellwig };
1445a07b4970SChristoph Hellwig 
144674255969SChristoph Hellwig static struct nvmet_type_name_map nvmet_ana_state[] = {
144762ac0d32SChristoph Hellwig 	{ NVME_ANA_OPTIMIZED,		"optimized" },
144862ac0d32SChristoph Hellwig 	{ NVME_ANA_NONOPTIMIZED,	"non-optimized" },
144962ac0d32SChristoph Hellwig 	{ NVME_ANA_INACCESSIBLE,	"inaccessible" },
145062ac0d32SChristoph Hellwig 	{ NVME_ANA_PERSISTENT_LOSS,	"persistent-loss" },
145162ac0d32SChristoph Hellwig 	{ NVME_ANA_CHANGE,		"change" },
145262ac0d32SChristoph Hellwig };
145362ac0d32SChristoph Hellwig 
145462ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
145562ac0d32SChristoph Hellwig 		char *page)
145662ac0d32SChristoph Hellwig {
145762ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
145862ac0d32SChristoph Hellwig 	enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
145962ac0d32SChristoph Hellwig 	int i;
146062ac0d32SChristoph Hellwig 
146184b8d0d7SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
146284b8d0d7SChaitanya Kulkarni 		if (state == nvmet_ana_state[i].type)
146384b8d0d7SChaitanya Kulkarni 			return sprintf(page, "%s\n", nvmet_ana_state[i].name);
146462ac0d32SChristoph Hellwig 	}
146562ac0d32SChristoph Hellwig 
146662ac0d32SChristoph Hellwig 	return sprintf(page, "\n");
146762ac0d32SChristoph Hellwig }
146862ac0d32SChristoph Hellwig 
146962ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
147062ac0d32SChristoph Hellwig 		const char *page, size_t count)
147162ac0d32SChristoph Hellwig {
147262ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
147384b8d0d7SChaitanya Kulkarni 	enum nvme_ana_state *ana_state = grp->port->ana_state;
147462ac0d32SChristoph Hellwig 	int i;
147562ac0d32SChristoph Hellwig 
147684b8d0d7SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
147784b8d0d7SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_ana_state[i].name))
147862ac0d32SChristoph Hellwig 			goto found;
147962ac0d32SChristoph Hellwig 	}
148062ac0d32SChristoph Hellwig 
148162ac0d32SChristoph Hellwig 	pr_err("Invalid value '%s' for ana_state\n", page);
148262ac0d32SChristoph Hellwig 	return -EINVAL;
148362ac0d32SChristoph Hellwig 
148462ac0d32SChristoph Hellwig found:
148562ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
148684b8d0d7SChaitanya Kulkarni 	ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
148762ac0d32SChristoph Hellwig 	nvmet_ana_chgcnt++;
148862ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
148962ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
149062ac0d32SChristoph Hellwig 	return count;
149162ac0d32SChristoph Hellwig }
149262ac0d32SChristoph Hellwig 
149362ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
149462ac0d32SChristoph Hellwig 
149562ac0d32SChristoph Hellwig static struct configfs_attribute *nvmet_ana_group_attrs[] = {
149662ac0d32SChristoph Hellwig 	&nvmet_ana_group_attr_ana_state,
149762ac0d32SChristoph Hellwig 	NULL,
149862ac0d32SChristoph Hellwig };
149962ac0d32SChristoph Hellwig 
150062ac0d32SChristoph Hellwig static void nvmet_ana_group_release(struct config_item *item)
150162ac0d32SChristoph Hellwig {
150262ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
150362ac0d32SChristoph Hellwig 
150462ac0d32SChristoph Hellwig 	if (grp == &grp->port->ana_default_group)
150562ac0d32SChristoph Hellwig 		return;
150662ac0d32SChristoph Hellwig 
150762ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
150862ac0d32SChristoph Hellwig 	grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
150962ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[grp->grpid]--;
151062ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
151162ac0d32SChristoph Hellwig 
151262ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
151362ac0d32SChristoph Hellwig 	kfree(grp);
151462ac0d32SChristoph Hellwig }
151562ac0d32SChristoph Hellwig 
151662ac0d32SChristoph Hellwig static struct configfs_item_operations nvmet_ana_group_item_ops = {
151762ac0d32SChristoph Hellwig 	.release		= nvmet_ana_group_release,
151862ac0d32SChristoph Hellwig };
151962ac0d32SChristoph Hellwig 
152062ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_group_type = {
152162ac0d32SChristoph Hellwig 	.ct_item_ops		= &nvmet_ana_group_item_ops,
152262ac0d32SChristoph Hellwig 	.ct_attrs		= nvmet_ana_group_attrs,
152362ac0d32SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
152462ac0d32SChristoph Hellwig };
152562ac0d32SChristoph Hellwig 
152662ac0d32SChristoph Hellwig static struct config_group *nvmet_ana_groups_make_group(
152762ac0d32SChristoph Hellwig 		struct config_group *group, const char *name)
152862ac0d32SChristoph Hellwig {
152962ac0d32SChristoph Hellwig 	struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
153062ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp;
153162ac0d32SChristoph Hellwig 	u32 grpid;
153262ac0d32SChristoph Hellwig 	int ret;
153362ac0d32SChristoph Hellwig 
153462ac0d32SChristoph Hellwig 	ret = kstrtou32(name, 0, &grpid);
153562ac0d32SChristoph Hellwig 	if (ret)
153662ac0d32SChristoph Hellwig 		goto out;
153762ac0d32SChristoph Hellwig 
153862ac0d32SChristoph Hellwig 	ret = -EINVAL;
153962ac0d32SChristoph Hellwig 	if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
154062ac0d32SChristoph Hellwig 		goto out;
154162ac0d32SChristoph Hellwig 
154262ac0d32SChristoph Hellwig 	ret = -ENOMEM;
154362ac0d32SChristoph Hellwig 	grp = kzalloc(sizeof(*grp), GFP_KERNEL);
154462ac0d32SChristoph Hellwig 	if (!grp)
154562ac0d32SChristoph Hellwig 		goto out;
154662ac0d32SChristoph Hellwig 	grp->port = port;
154762ac0d32SChristoph Hellwig 	grp->grpid = grpid;
154862ac0d32SChristoph Hellwig 
154962ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
155062ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[grpid]++;
155162ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
155262ac0d32SChristoph Hellwig 
155362ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
155462ac0d32SChristoph Hellwig 
155562ac0d32SChristoph Hellwig 	config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
155662ac0d32SChristoph Hellwig 	return &grp->group;
155762ac0d32SChristoph Hellwig out:
155862ac0d32SChristoph Hellwig 	return ERR_PTR(ret);
155962ac0d32SChristoph Hellwig }
156062ac0d32SChristoph Hellwig 
156162ac0d32SChristoph Hellwig static struct configfs_group_operations nvmet_ana_groups_group_ops = {
156262ac0d32SChristoph Hellwig 	.make_group		= nvmet_ana_groups_make_group,
156362ac0d32SChristoph Hellwig };
156462ac0d32SChristoph Hellwig 
156562ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_groups_type = {
156662ac0d32SChristoph Hellwig 	.ct_group_ops		= &nvmet_ana_groups_group_ops,
156762ac0d32SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
156862ac0d32SChristoph Hellwig };
156962ac0d32SChristoph Hellwig 
1570a07b4970SChristoph Hellwig /*
1571a07b4970SChristoph Hellwig  * Ports definitions.
1572a07b4970SChristoph Hellwig  */
1573a07b4970SChristoph Hellwig static void nvmet_port_release(struct config_item *item)
1574a07b4970SChristoph Hellwig {
1575a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1576a07b4970SChristoph Hellwig 
1577e3e19dccSIsrael Rukshin 	/* Let inflight controllers teardown complete */
15788832cf92SSagi Grimberg 	flush_workqueue(nvmet_wq);
1579b662a078SJay Sternberg 	list_del(&port->global_entry);
1580b662a078SJay Sternberg 
158172efd25dSChristoph Hellwig 	kfree(port->ana_state);
1582a07b4970SChristoph Hellwig 	kfree(port);
1583a07b4970SChristoph Hellwig }
1584a07b4970SChristoph Hellwig 
1585a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_port_attrs[] = {
1586a07b4970SChristoph Hellwig 	&nvmet_attr_addr_adrfam,
1587a07b4970SChristoph Hellwig 	&nvmet_attr_addr_treq,
1588a07b4970SChristoph Hellwig 	&nvmet_attr_addr_traddr,
1589a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trsvcid,
1590a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trtype,
15910d5ee2b2SSteve Wise 	&nvmet_attr_param_inline_data_size,
1592ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1593ea52ac1cSIsrael Rukshin 	&nvmet_attr_param_pi_enable,
1594ea52ac1cSIsrael Rukshin #endif
1595a07b4970SChristoph Hellwig 	NULL,
1596a07b4970SChristoph Hellwig };
1597a07b4970SChristoph Hellwig 
1598a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_item_ops = {
1599a07b4970SChristoph Hellwig 	.release		= nvmet_port_release,
1600a07b4970SChristoph Hellwig };
1601a07b4970SChristoph Hellwig 
160266603a31SBhumika Goyal static const struct config_item_type nvmet_port_type = {
1603a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_port_attrs,
1604a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_port_item_ops,
1605a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1606a07b4970SChristoph Hellwig };
1607a07b4970SChristoph Hellwig 
1608a07b4970SChristoph Hellwig static struct config_group *nvmet_ports_make(struct config_group *group,
1609a07b4970SChristoph Hellwig 		const char *name)
1610a07b4970SChristoph Hellwig {
1611a07b4970SChristoph Hellwig 	struct nvmet_port *port;
1612a07b4970SChristoph Hellwig 	u16 portid;
161362ac0d32SChristoph Hellwig 	u32 i;
1614a07b4970SChristoph Hellwig 
1615a07b4970SChristoph Hellwig 	if (kstrtou16(name, 0, &portid))
1616a07b4970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1617a07b4970SChristoph Hellwig 
1618a07b4970SChristoph Hellwig 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1619a07b4970SChristoph Hellwig 	if (!port)
1620f98d9ca1SDan Carpenter 		return ERR_PTR(-ENOMEM);
1621a07b4970SChristoph Hellwig 
162272efd25dSChristoph Hellwig 	port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
162372efd25dSChristoph Hellwig 			sizeof(*port->ana_state), GFP_KERNEL);
162472efd25dSChristoph Hellwig 	if (!port->ana_state) {
162572efd25dSChristoph Hellwig 		kfree(port);
162672efd25dSChristoph Hellwig 		return ERR_PTR(-ENOMEM);
162772efd25dSChristoph Hellwig 	}
162872efd25dSChristoph Hellwig 
162962ac0d32SChristoph Hellwig 	for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
163062ac0d32SChristoph Hellwig 		if (i == NVMET_DEFAULT_ANA_GRPID)
163162ac0d32SChristoph Hellwig 			port->ana_state[1] = NVME_ANA_OPTIMIZED;
163262ac0d32SChristoph Hellwig 		else
163362ac0d32SChristoph Hellwig 			port->ana_state[i] = NVME_ANA_INACCESSIBLE;
163462ac0d32SChristoph Hellwig 	}
163572efd25dSChristoph Hellwig 
1636b662a078SJay Sternberg 	list_add(&port->global_entry, &nvmet_ports_list);
1637b662a078SJay Sternberg 
1638a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->entry);
1639a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->subsystems);
1640a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->referrals);
16410d5ee2b2SSteve Wise 	port->inline_data_size = -1;	/* < 0 == let the transport choose */
1642a07b4970SChristoph Hellwig 
1643a07b4970SChristoph Hellwig 	port->disc_addr.portid = cpu_to_le16(portid);
1644d02abd19SChaitanya Kulkarni 	port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
16459b95d2fbSSagi Grimberg 	port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1646a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->group, name, &nvmet_port_type);
1647a07b4970SChristoph Hellwig 
1648a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->subsys_group,
1649a07b4970SChristoph Hellwig 			"subsystems", &nvmet_port_subsys_type);
1650a07b4970SChristoph Hellwig 	configfs_add_default_group(&port->subsys_group, &port->group);
1651a07b4970SChristoph Hellwig 
1652a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->referrals_group,
1653a07b4970SChristoph Hellwig 			"referrals", &nvmet_referrals_type);
1654a07b4970SChristoph Hellwig 	configfs_add_default_group(&port->referrals_group, &port->group);
1655a07b4970SChristoph Hellwig 
165662ac0d32SChristoph Hellwig 	config_group_init_type_name(&port->ana_groups_group,
165762ac0d32SChristoph Hellwig 			"ana_groups", &nvmet_ana_groups_type);
165862ac0d32SChristoph Hellwig 	configfs_add_default_group(&port->ana_groups_group, &port->group);
165962ac0d32SChristoph Hellwig 
166062ac0d32SChristoph Hellwig 	port->ana_default_group.port = port;
166162ac0d32SChristoph Hellwig 	port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
166262ac0d32SChristoph Hellwig 	config_group_init_type_name(&port->ana_default_group.group,
166362ac0d32SChristoph Hellwig 			__stringify(NVMET_DEFAULT_ANA_GRPID),
166462ac0d32SChristoph Hellwig 			&nvmet_ana_group_type);
166562ac0d32SChristoph Hellwig 	configfs_add_default_group(&port->ana_default_group.group,
166662ac0d32SChristoph Hellwig 			&port->ana_groups_group);
166762ac0d32SChristoph Hellwig 
1668a07b4970SChristoph Hellwig 	return &port->group;
1669a07b4970SChristoph Hellwig }
1670a07b4970SChristoph Hellwig 
1671a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_ports_group_ops = {
1672a07b4970SChristoph Hellwig 	.make_group		= nvmet_ports_make,
1673a07b4970SChristoph Hellwig };
1674a07b4970SChristoph Hellwig 
167566603a31SBhumika Goyal static const struct config_item_type nvmet_ports_type = {
1676a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_ports_group_ops,
1677a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1678a07b4970SChristoph Hellwig };
1679a07b4970SChristoph Hellwig 
1680a07b4970SChristoph Hellwig static struct config_group nvmet_subsystems_group;
1681a07b4970SChristoph Hellwig static struct config_group nvmet_ports_group;
1682a07b4970SChristoph Hellwig 
1683a07b4970SChristoph Hellwig static void nvmet_host_release(struct config_item *item)
1684a07b4970SChristoph Hellwig {
1685a07b4970SChristoph Hellwig 	struct nvmet_host *host = to_host(item);
1686a07b4970SChristoph Hellwig 
1687a07b4970SChristoph Hellwig 	kfree(host);
1688a07b4970SChristoph Hellwig }
1689a07b4970SChristoph Hellwig 
1690a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_host_item_ops = {
1691a07b4970SChristoph Hellwig 	.release		= nvmet_host_release,
1692a07b4970SChristoph Hellwig };
1693a07b4970SChristoph Hellwig 
169466603a31SBhumika Goyal static const struct config_item_type nvmet_host_type = {
1695a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_host_item_ops,
1696a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1697a07b4970SChristoph Hellwig };
1698a07b4970SChristoph Hellwig 
1699a07b4970SChristoph Hellwig static struct config_group *nvmet_hosts_make_group(struct config_group *group,
1700a07b4970SChristoph Hellwig 		const char *name)
1701a07b4970SChristoph Hellwig {
1702a07b4970SChristoph Hellwig 	struct nvmet_host *host;
1703a07b4970SChristoph Hellwig 
1704a07b4970SChristoph Hellwig 	host = kzalloc(sizeof(*host), GFP_KERNEL);
1705a07b4970SChristoph Hellwig 	if (!host)
1706a07b4970SChristoph Hellwig 		return ERR_PTR(-ENOMEM);
1707a07b4970SChristoph Hellwig 
1708a07b4970SChristoph Hellwig 	config_group_init_type_name(&host->group, name, &nvmet_host_type);
1709a07b4970SChristoph Hellwig 
1710a07b4970SChristoph Hellwig 	return &host->group;
1711a07b4970SChristoph Hellwig }
1712a07b4970SChristoph Hellwig 
1713a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_hosts_group_ops = {
1714a07b4970SChristoph Hellwig 	.make_group		= nvmet_hosts_make_group,
1715a07b4970SChristoph Hellwig };
1716a07b4970SChristoph Hellwig 
171766603a31SBhumika Goyal static const struct config_item_type nvmet_hosts_type = {
1718a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_hosts_group_ops,
1719a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1720a07b4970SChristoph Hellwig };
1721a07b4970SChristoph Hellwig 
1722a07b4970SChristoph Hellwig static struct config_group nvmet_hosts_group;
1723a07b4970SChristoph Hellwig 
172466603a31SBhumika Goyal static const struct config_item_type nvmet_root_type = {
1725a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1726a07b4970SChristoph Hellwig };
1727a07b4970SChristoph Hellwig 
1728a07b4970SChristoph Hellwig static struct configfs_subsystem nvmet_configfs_subsystem = {
1729a07b4970SChristoph Hellwig 	.su_group = {
1730a07b4970SChristoph Hellwig 		.cg_item = {
1731a07b4970SChristoph Hellwig 			.ci_namebuf	= "nvmet",
1732a07b4970SChristoph Hellwig 			.ci_type	= &nvmet_root_type,
1733a07b4970SChristoph Hellwig 		},
1734a07b4970SChristoph Hellwig 	},
1735a07b4970SChristoph Hellwig };
1736a07b4970SChristoph Hellwig 
1737a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void)
1738a07b4970SChristoph Hellwig {
1739a07b4970SChristoph Hellwig 	int ret;
1740a07b4970SChristoph Hellwig 
1741a07b4970SChristoph Hellwig 	config_group_init(&nvmet_configfs_subsystem.su_group);
1742a07b4970SChristoph Hellwig 	mutex_init(&nvmet_configfs_subsystem.su_mutex);
1743a07b4970SChristoph Hellwig 
1744a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_subsystems_group,
1745a07b4970SChristoph Hellwig 			"subsystems", &nvmet_subsystems_type);
1746a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_subsystems_group,
1747a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1748a07b4970SChristoph Hellwig 
1749a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_ports_group,
1750a07b4970SChristoph Hellwig 			"ports", &nvmet_ports_type);
1751a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_ports_group,
1752a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1753a07b4970SChristoph Hellwig 
1754a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_hosts_group,
1755a07b4970SChristoph Hellwig 			"hosts", &nvmet_hosts_type);
1756a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_hosts_group,
1757a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1758a07b4970SChristoph Hellwig 
1759a07b4970SChristoph Hellwig 	ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
1760a07b4970SChristoph Hellwig 	if (ret) {
1761a07b4970SChristoph Hellwig 		pr_err("configfs_register_subsystem: %d\n", ret);
1762a07b4970SChristoph Hellwig 		return ret;
1763a07b4970SChristoph Hellwig 	}
1764a07b4970SChristoph Hellwig 
1765a07b4970SChristoph Hellwig 	return 0;
1766a07b4970SChristoph Hellwig }
1767a07b4970SChristoph Hellwig 
1768a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void)
1769a07b4970SChristoph Hellwig {
1770a07b4970SChristoph Hellwig 	configfs_unregister_subsystem(&nvmet_configfs_subsystem);
1771a07b4970SChristoph Hellwig }
1772