xref: /openbmc/linux/drivers/nvme/target/configfs.c (revision 73d77c53)
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)
637e764179SChaitanya Kulkarni 			return sprintf(page, "%s\n", nvmet_addr_family[i].name);
64a07b4970SChristoph Hellwig 	}
657e764179SChaitanya Kulkarni 
667e764179SChaitanya Kulkarni 	return sprintf(page, "\n");
67a07b4970SChristoph Hellwig }
68a07b4970SChristoph Hellwig 
69a07b4970SChristoph Hellwig static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
70a07b4970SChristoph Hellwig 		const char *page, size_t count)
71a07b4970SChristoph Hellwig {
72a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
737e764179SChaitanya Kulkarni 	int i;
74a07b4970SChristoph Hellwig 
753ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
76a07b4970SChristoph Hellwig 		return -EACCES;
77a07b4970SChristoph Hellwig 
787e764179SChaitanya Kulkarni 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
797e764179SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_addr_family[i].name))
807e764179SChaitanya Kulkarni 			goto found;
81a07b4970SChristoph Hellwig 	}
82a07b4970SChristoph Hellwig 
837e764179SChaitanya Kulkarni 	pr_err("Invalid value '%s' for adrfam\n", page);
847e764179SChaitanya Kulkarni 	return -EINVAL;
857e764179SChaitanya Kulkarni 
867e764179SChaitanya Kulkarni found:
87d02abd19SChaitanya Kulkarni 	port->disc_addr.adrfam = nvmet_addr_family[i].type;
88a07b4970SChristoph Hellwig 	return count;
89a07b4970SChristoph Hellwig }
90a07b4970SChristoph Hellwig 
91a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_adrfam);
92a07b4970SChristoph Hellwig 
93a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_show(struct config_item *item,
94a07b4970SChristoph Hellwig 		char *page)
95a07b4970SChristoph Hellwig {
96*73d77c53SChaitanya Kulkarni 	__le16 portid = to_nvmet_port(item)->disc_addr.portid;
97a07b4970SChristoph Hellwig 
98*73d77c53SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%d\n", le16_to_cpu(portid));
99a07b4970SChristoph Hellwig }
100a07b4970SChristoph Hellwig 
101a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_store(struct config_item *item,
102a07b4970SChristoph Hellwig 		const char *page, size_t count)
103a07b4970SChristoph Hellwig {
104a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
105a07b4970SChristoph Hellwig 	u16 portid = 0;
106a07b4970SChristoph Hellwig 
107a07b4970SChristoph Hellwig 	if (kstrtou16(page, 0, &portid)) {
108a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for portid\n", page);
109a07b4970SChristoph Hellwig 		return -EINVAL;
110a07b4970SChristoph Hellwig 	}
111a07b4970SChristoph Hellwig 
1123ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
113a07b4970SChristoph Hellwig 		return -EACCES;
1143ecb5faaSChaitanya Kulkarni 
115a07b4970SChristoph Hellwig 	port->disc_addr.portid = cpu_to_le16(portid);
116a07b4970SChristoph Hellwig 	return count;
117a07b4970SChristoph Hellwig }
118a07b4970SChristoph Hellwig 
119a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_portid);
120a07b4970SChristoph Hellwig 
121a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_show(struct config_item *item,
122a07b4970SChristoph Hellwig 		char *page)
123a07b4970SChristoph Hellwig {
124a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
125a07b4970SChristoph Hellwig 
126*73d77c53SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.traddr);
127a07b4970SChristoph Hellwig }
128a07b4970SChristoph Hellwig 
129a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_store(struct config_item *item,
130a07b4970SChristoph Hellwig 		const char *page, size_t count)
131a07b4970SChristoph Hellwig {
132a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
133a07b4970SChristoph Hellwig 
134a07b4970SChristoph Hellwig 	if (count > NVMF_TRADDR_SIZE) {
135a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for traddr\n", page);
136a07b4970SChristoph Hellwig 		return -EINVAL;
137a07b4970SChristoph Hellwig 	}
138a07b4970SChristoph Hellwig 
1393ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
140a07b4970SChristoph Hellwig 		return -EACCES;
1419ba2a5cbSSagi Grimberg 
1429ba2a5cbSSagi Grimberg 	if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
1439ba2a5cbSSagi Grimberg 		return -EINVAL;
1449ba2a5cbSSagi Grimberg 	return count;
145a07b4970SChristoph Hellwig }
146a07b4970SChristoph Hellwig 
147a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_traddr);
148a07b4970SChristoph Hellwig 
14987628e28SChaitanya Kulkarni static const struct nvmet_type_name_map nvmet_addr_treq[] = {
15087628e28SChaitanya Kulkarni 	{ NVMF_TREQ_NOT_SPECIFIED,	"not specified" },
15187628e28SChaitanya Kulkarni 	{ NVMF_TREQ_REQUIRED,		"required" },
15287628e28SChaitanya Kulkarni 	{ NVMF_TREQ_NOT_REQUIRED,	"not required" },
15387628e28SChaitanya Kulkarni };
15487628e28SChaitanya Kulkarni 
15587628e28SChaitanya Kulkarni static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page)
156a07b4970SChristoph Hellwig {
15787628e28SChaitanya Kulkarni 	u8 treq = to_nvmet_port(item)->disc_addr.treq &
15887628e28SChaitanya Kulkarni 		NVME_TREQ_SECURE_CHANNEL_MASK;
15987628e28SChaitanya Kulkarni 	int i;
16087628e28SChaitanya Kulkarni 
16187628e28SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
16287628e28SChaitanya Kulkarni 		if (treq == nvmet_addr_treq[i].type)
16387628e28SChaitanya Kulkarni 			return sprintf(page, "%s\n", nvmet_addr_treq[i].name);
164a07b4970SChristoph Hellwig 	}
16587628e28SChaitanya Kulkarni 
16687628e28SChaitanya Kulkarni 	return sprintf(page, "\n");
167a07b4970SChristoph Hellwig }
168a07b4970SChristoph Hellwig 
169a07b4970SChristoph Hellwig static ssize_t nvmet_addr_treq_store(struct config_item *item,
170a07b4970SChristoph Hellwig 		const char *page, size_t count)
171a07b4970SChristoph Hellwig {
172a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1730445e1b5SSagi Grimberg 	u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK;
17487628e28SChaitanya Kulkarni 	int i;
175a07b4970SChristoph Hellwig 
1763ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
177a07b4970SChristoph Hellwig 		return -EACCES;
178a07b4970SChristoph Hellwig 
17987628e28SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
18087628e28SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_addr_treq[i].name))
18187628e28SChaitanya Kulkarni 			goto found;
18287628e28SChaitanya Kulkarni 	}
18387628e28SChaitanya Kulkarni 
184a07b4970SChristoph Hellwig 	pr_err("Invalid value '%s' for treq\n", page);
185a07b4970SChristoph Hellwig 	return -EINVAL;
186a07b4970SChristoph Hellwig 
18787628e28SChaitanya Kulkarni found:
18887628e28SChaitanya Kulkarni 	treq |= nvmet_addr_treq[i].type;
18987628e28SChaitanya Kulkarni 	port->disc_addr.treq = treq;
190a07b4970SChristoph Hellwig 	return count;
191a07b4970SChristoph Hellwig }
192a07b4970SChristoph Hellwig 
193a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_treq);
194a07b4970SChristoph Hellwig 
195a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
196a07b4970SChristoph Hellwig 		char *page)
197a07b4970SChristoph Hellwig {
198a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
199a07b4970SChristoph Hellwig 
200*73d77c53SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.trsvcid);
201a07b4970SChristoph Hellwig }
202a07b4970SChristoph Hellwig 
203a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
204a07b4970SChristoph Hellwig 		const char *page, size_t count)
205a07b4970SChristoph Hellwig {
206a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
207a07b4970SChristoph Hellwig 
208a07b4970SChristoph Hellwig 	if (count > NVMF_TRSVCID_SIZE) {
209a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for trsvcid\n", page);
210a07b4970SChristoph Hellwig 		return -EINVAL;
211a07b4970SChristoph Hellwig 	}
2123ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
213a07b4970SChristoph Hellwig 		return -EACCES;
2149ba2a5cbSSagi Grimberg 
2159ba2a5cbSSagi Grimberg 	if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
2169ba2a5cbSSagi Grimberg 		return -EINVAL;
2179ba2a5cbSSagi Grimberg 	return count;
218a07b4970SChristoph Hellwig }
219a07b4970SChristoph Hellwig 
220a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trsvcid);
221a07b4970SChristoph Hellwig 
2220d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
2230d5ee2b2SSteve Wise 		char *page)
2240d5ee2b2SSteve Wise {
2250d5ee2b2SSteve Wise 	struct nvmet_port *port = to_nvmet_port(item);
2260d5ee2b2SSteve Wise 
2270d5ee2b2SSteve Wise 	return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
2280d5ee2b2SSteve Wise }
2290d5ee2b2SSteve Wise 
2300d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
2310d5ee2b2SSteve Wise 		const char *page, size_t count)
2320d5ee2b2SSteve Wise {
2330d5ee2b2SSteve Wise 	struct nvmet_port *port = to_nvmet_port(item);
2340d5ee2b2SSteve Wise 	int ret;
2350d5ee2b2SSteve Wise 
2363ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
2370d5ee2b2SSteve Wise 		return -EACCES;
2380d5ee2b2SSteve Wise 	ret = kstrtoint(page, 0, &port->inline_data_size);
2390d5ee2b2SSteve Wise 	if (ret) {
2400d5ee2b2SSteve Wise 		pr_err("Invalid value '%s' for inline_data_size\n", page);
2410d5ee2b2SSteve Wise 		return -EINVAL;
2420d5ee2b2SSteve Wise 	}
2430d5ee2b2SSteve Wise 	return count;
2440d5ee2b2SSteve Wise }
2450d5ee2b2SSteve Wise 
2460d5ee2b2SSteve Wise CONFIGFS_ATTR(nvmet_, param_inline_data_size);
2470d5ee2b2SSteve Wise 
248ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
249ea52ac1cSIsrael Rukshin static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
250ea52ac1cSIsrael Rukshin 		char *page)
251ea52ac1cSIsrael Rukshin {
252ea52ac1cSIsrael Rukshin 	struct nvmet_port *port = to_nvmet_port(item);
253ea52ac1cSIsrael Rukshin 
254ea52ac1cSIsrael Rukshin 	return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
255ea52ac1cSIsrael Rukshin }
256ea52ac1cSIsrael Rukshin 
257ea52ac1cSIsrael Rukshin static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
258ea52ac1cSIsrael Rukshin 		const char *page, size_t count)
259ea52ac1cSIsrael Rukshin {
260ea52ac1cSIsrael Rukshin 	struct nvmet_port *port = to_nvmet_port(item);
261ea52ac1cSIsrael Rukshin 	bool val;
262ea52ac1cSIsrael Rukshin 
263ea52ac1cSIsrael Rukshin 	if (strtobool(page, &val))
264ea52ac1cSIsrael Rukshin 		return -EINVAL;
265ea52ac1cSIsrael Rukshin 
266cc345622SIsrael Rukshin 	if (nvmet_is_port_enabled(port, __func__))
267ea52ac1cSIsrael Rukshin 		return -EACCES;
268ea52ac1cSIsrael Rukshin 
269ea52ac1cSIsrael Rukshin 	port->pi_enable = val;
270ea52ac1cSIsrael Rukshin 	return count;
271ea52ac1cSIsrael Rukshin }
272ea52ac1cSIsrael Rukshin 
273ea52ac1cSIsrael Rukshin CONFIGFS_ATTR(nvmet_, param_pi_enable);
274ea52ac1cSIsrael Rukshin #endif
275ea52ac1cSIsrael Rukshin 
276a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_show(struct config_item *item,
277a07b4970SChristoph Hellwig 		char *page)
278a07b4970SChristoph Hellwig {
279a5d18612SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
280a5d18612SChristoph Hellwig 	int i;
281a5d18612SChristoph Hellwig 
28245e2f3c2SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
28345e2f3c2SChaitanya Kulkarni 		if (port->disc_addr.trtype == nvmet_transport[i].type)
28445e2f3c2SChaitanya Kulkarni 			return sprintf(page, "%s\n", nvmet_transport[i].name);
285a07b4970SChristoph Hellwig 	}
286a5d18612SChristoph Hellwig 
287a5d18612SChristoph Hellwig 	return sprintf(page, "\n");
288a07b4970SChristoph Hellwig }
289a07b4970SChristoph Hellwig 
290a07b4970SChristoph Hellwig static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
291a07b4970SChristoph Hellwig {
292a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
293a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
294a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
295a07b4970SChristoph Hellwig }
296a07b4970SChristoph Hellwig 
297a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_store(struct config_item *item,
298a07b4970SChristoph Hellwig 		const char *page, size_t count)
299a07b4970SChristoph Hellwig {
300a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
301a5d18612SChristoph Hellwig 	int i;
302a07b4970SChristoph Hellwig 
3033ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
304a07b4970SChristoph Hellwig 		return -EACCES;
305a07b4970SChristoph Hellwig 
30645e2f3c2SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
30745e2f3c2SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_transport[i].name))
308a5d18612SChristoph Hellwig 			goto found;
309a07b4970SChristoph Hellwig 	}
310a07b4970SChristoph Hellwig 
311a5d18612SChristoph Hellwig 	pr_err("Invalid value '%s' for trtype\n", page);
312a5d18612SChristoph Hellwig 	return -EINVAL;
3137e764179SChaitanya Kulkarni 
314a5d18612SChristoph Hellwig found:
315a5d18612SChristoph Hellwig 	memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
31645e2f3c2SChaitanya Kulkarni 	port->disc_addr.trtype = nvmet_transport[i].type;
317a5d18612SChristoph Hellwig 	if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
318a5d18612SChristoph Hellwig 		nvmet_port_init_tsas_rdma(port);
319a07b4970SChristoph Hellwig 	return count;
320a07b4970SChristoph Hellwig }
321a07b4970SChristoph Hellwig 
322a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trtype);
323a07b4970SChristoph Hellwig 
324a07b4970SChristoph Hellwig /*
325a07b4970SChristoph Hellwig  * Namespace structures & file operation functions below
326a07b4970SChristoph Hellwig  */
327a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
328a07b4970SChristoph Hellwig {
329a07b4970SChristoph Hellwig 	return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
330a07b4970SChristoph Hellwig }
331a07b4970SChristoph Hellwig 
332a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_store(struct config_item *item,
333a07b4970SChristoph Hellwig 		const char *page, size_t count)
334a07b4970SChristoph Hellwig {
335a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
336a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = ns->subsys;
3375613d312SHannes Reinecke 	size_t len;
338a07b4970SChristoph Hellwig 	int ret;
339a07b4970SChristoph Hellwig 
340a07b4970SChristoph Hellwig 	mutex_lock(&subsys->lock);
341a07b4970SChristoph Hellwig 	ret = -EBUSY;
342e4fcf07cSSolganik Alexander 	if (ns->enabled)
343a07b4970SChristoph Hellwig 		goto out_unlock;
344a07b4970SChristoph Hellwig 
3455613d312SHannes Reinecke 	ret = -EINVAL;
3465613d312SHannes Reinecke 	len = strcspn(page, "\n");
3475613d312SHannes Reinecke 	if (!len)
3485613d312SHannes Reinecke 		goto out_unlock;
349a07b4970SChristoph Hellwig 
3505613d312SHannes Reinecke 	kfree(ns->device_path);
351a07b4970SChristoph Hellwig 	ret = -ENOMEM;
35209bb8986SChen Zhou 	ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
353a07b4970SChristoph Hellwig 	if (!ns->device_path)
354a07b4970SChristoph Hellwig 		goto out_unlock;
355a07b4970SChristoph Hellwig 
356a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
357a07b4970SChristoph Hellwig 	return count;
358a07b4970SChristoph Hellwig 
359a07b4970SChristoph Hellwig out_unlock:
360a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
361a07b4970SChristoph Hellwig 	return ret;
362a07b4970SChristoph Hellwig }
363a07b4970SChristoph Hellwig 
364a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_path);
365a07b4970SChristoph Hellwig 
366c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA
367c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
368c6925093SLogan Gunthorpe {
369c6925093SLogan Gunthorpe 	struct nvmet_ns *ns = to_nvmet_ns(item);
370c6925093SLogan Gunthorpe 
371c6925093SLogan Gunthorpe 	return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
372c6925093SLogan Gunthorpe }
373c6925093SLogan Gunthorpe 
374c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
375c6925093SLogan Gunthorpe 		const char *page, size_t count)
376c6925093SLogan Gunthorpe {
377c6925093SLogan Gunthorpe 	struct nvmet_ns *ns = to_nvmet_ns(item);
378c6925093SLogan Gunthorpe 	struct pci_dev *p2p_dev = NULL;
379c6925093SLogan Gunthorpe 	bool use_p2pmem;
380c6925093SLogan Gunthorpe 	int ret = count;
381c6925093SLogan Gunthorpe 	int error;
382c6925093SLogan Gunthorpe 
383c6925093SLogan Gunthorpe 	mutex_lock(&ns->subsys->lock);
384c6925093SLogan Gunthorpe 	if (ns->enabled) {
385c6925093SLogan Gunthorpe 		ret = -EBUSY;
386c6925093SLogan Gunthorpe 		goto out_unlock;
387c6925093SLogan Gunthorpe 	}
388c6925093SLogan Gunthorpe 
389c6925093SLogan Gunthorpe 	error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
390c6925093SLogan Gunthorpe 	if (error) {
391c6925093SLogan Gunthorpe 		ret = error;
392c6925093SLogan Gunthorpe 		goto out_unlock;
393c6925093SLogan Gunthorpe 	}
394c6925093SLogan Gunthorpe 
395c6925093SLogan Gunthorpe 	ns->use_p2pmem = use_p2pmem;
396c6925093SLogan Gunthorpe 	pci_dev_put(ns->p2p_dev);
397c6925093SLogan Gunthorpe 	ns->p2p_dev = p2p_dev;
398c6925093SLogan Gunthorpe 
399c6925093SLogan Gunthorpe out_unlock:
400c6925093SLogan Gunthorpe 	mutex_unlock(&ns->subsys->lock);
401c6925093SLogan Gunthorpe 
402c6925093SLogan Gunthorpe 	return ret;
403c6925093SLogan Gunthorpe }
404c6925093SLogan Gunthorpe 
405c6925093SLogan Gunthorpe CONFIGFS_ATTR(nvmet_ns_, p2pmem);
406c6925093SLogan Gunthorpe #endif /* CONFIG_PCI_P2PDMA */
407c6925093SLogan Gunthorpe 
408430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
409430c7befSJohannes Thumshirn {
410430c7befSJohannes Thumshirn 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
411430c7befSJohannes Thumshirn }
412430c7befSJohannes Thumshirn 
413430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
414430c7befSJohannes Thumshirn 					  const char *page, size_t count)
415430c7befSJohannes Thumshirn {
416430c7befSJohannes Thumshirn 	struct nvmet_ns *ns = to_nvmet_ns(item);
417430c7befSJohannes Thumshirn 	struct nvmet_subsys *subsys = ns->subsys;
418430c7befSJohannes Thumshirn 	int ret = 0;
419430c7befSJohannes Thumshirn 
420430c7befSJohannes Thumshirn 	mutex_lock(&subsys->lock);
421430c7befSJohannes Thumshirn 	if (ns->enabled) {
422430c7befSJohannes Thumshirn 		ret = -EBUSY;
423430c7befSJohannes Thumshirn 		goto out_unlock;
424430c7befSJohannes Thumshirn 	}
425430c7befSJohannes Thumshirn 
426430c7befSJohannes Thumshirn 	if (uuid_parse(page, &ns->uuid))
427430c7befSJohannes Thumshirn 		ret = -EINVAL;
428430c7befSJohannes Thumshirn 
429430c7befSJohannes Thumshirn out_unlock:
430430c7befSJohannes Thumshirn 	mutex_unlock(&subsys->lock);
431430c7befSJohannes Thumshirn 	return ret ? ret : count;
432430c7befSJohannes Thumshirn }
433430c7befSJohannes Thumshirn 
434f871749aSMax Gurtovoy CONFIGFS_ATTR(nvmet_ns_, device_uuid);
435f871749aSMax Gurtovoy 
436a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
437a07b4970SChristoph Hellwig {
438a07b4970SChristoph Hellwig 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
439a07b4970SChristoph Hellwig }
440a07b4970SChristoph Hellwig 
441a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
442a07b4970SChristoph Hellwig 		const char *page, size_t count)
443a07b4970SChristoph Hellwig {
444a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
445a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = ns->subsys;
446a07b4970SChristoph Hellwig 	u8 nguid[16];
447a07b4970SChristoph Hellwig 	const char *p = page;
448a07b4970SChristoph Hellwig 	int i;
449a07b4970SChristoph Hellwig 	int ret = 0;
450a07b4970SChristoph Hellwig 
451a07b4970SChristoph Hellwig 	mutex_lock(&subsys->lock);
452e4fcf07cSSolganik Alexander 	if (ns->enabled) {
453a07b4970SChristoph Hellwig 		ret = -EBUSY;
454a07b4970SChristoph Hellwig 		goto out_unlock;
455a07b4970SChristoph Hellwig 	}
456a07b4970SChristoph Hellwig 
457a07b4970SChristoph Hellwig 	for (i = 0; i < 16; i++) {
458a07b4970SChristoph Hellwig 		if (p + 2 > page + count) {
459a07b4970SChristoph Hellwig 			ret = -EINVAL;
460a07b4970SChristoph Hellwig 			goto out_unlock;
461a07b4970SChristoph Hellwig 		}
462a07b4970SChristoph Hellwig 		if (!isxdigit(p[0]) || !isxdigit(p[1])) {
463a07b4970SChristoph Hellwig 			ret = -EINVAL;
464a07b4970SChristoph Hellwig 			goto out_unlock;
465a07b4970SChristoph Hellwig 		}
466a07b4970SChristoph Hellwig 
467a07b4970SChristoph Hellwig 		nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
468a07b4970SChristoph Hellwig 		p += 2;
469a07b4970SChristoph Hellwig 
470a07b4970SChristoph Hellwig 		if (*p == '-' || *p == ':')
471a07b4970SChristoph Hellwig 			p++;
472a07b4970SChristoph Hellwig 	}
473a07b4970SChristoph Hellwig 
474a07b4970SChristoph Hellwig 	memcpy(&ns->nguid, nguid, sizeof(nguid));
475a07b4970SChristoph Hellwig out_unlock:
476a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
477a07b4970SChristoph Hellwig 	return ret ? ret : count;
478a07b4970SChristoph Hellwig }
479a07b4970SChristoph Hellwig 
480a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_nguid);
481a07b4970SChristoph Hellwig 
48262ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
48362ac0d32SChristoph Hellwig {
48462ac0d32SChristoph Hellwig 	return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
48562ac0d32SChristoph Hellwig }
48662ac0d32SChristoph Hellwig 
48762ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
48862ac0d32SChristoph Hellwig 		const char *page, size_t count)
48962ac0d32SChristoph Hellwig {
49062ac0d32SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
49162ac0d32SChristoph Hellwig 	u32 oldgrpid, newgrpid;
49262ac0d32SChristoph Hellwig 	int ret;
49362ac0d32SChristoph Hellwig 
49462ac0d32SChristoph Hellwig 	ret = kstrtou32(page, 0, &newgrpid);
49562ac0d32SChristoph Hellwig 	if (ret)
49662ac0d32SChristoph Hellwig 		return ret;
49762ac0d32SChristoph Hellwig 
49862ac0d32SChristoph Hellwig 	if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
49962ac0d32SChristoph Hellwig 		return -EINVAL;
50062ac0d32SChristoph Hellwig 
50162ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
50262ac0d32SChristoph Hellwig 	oldgrpid = ns->anagrpid;
50362ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[newgrpid]++;
50462ac0d32SChristoph Hellwig 	ns->anagrpid = newgrpid;
50562ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[oldgrpid]--;
50662ac0d32SChristoph Hellwig 	nvmet_ana_chgcnt++;
50762ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
50862ac0d32SChristoph Hellwig 
50962ac0d32SChristoph Hellwig 	nvmet_send_ana_event(ns->subsys, NULL);
51062ac0d32SChristoph Hellwig 	return count;
51162ac0d32SChristoph Hellwig }
51262ac0d32SChristoph Hellwig 
51362ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
51462ac0d32SChristoph Hellwig 
515a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
516a07b4970SChristoph Hellwig {
517e4fcf07cSSolganik Alexander 	return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
518a07b4970SChristoph Hellwig }
519a07b4970SChristoph Hellwig 
520a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_store(struct config_item *item,
521a07b4970SChristoph Hellwig 		const char *page, size_t count)
522a07b4970SChristoph Hellwig {
523a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
524a07b4970SChristoph Hellwig 	bool enable;
525a07b4970SChristoph Hellwig 	int ret = 0;
526a07b4970SChristoph Hellwig 
527a07b4970SChristoph Hellwig 	if (strtobool(page, &enable))
528a07b4970SChristoph Hellwig 		return -EINVAL;
529a07b4970SChristoph Hellwig 
530a07b4970SChristoph Hellwig 	if (enable)
531a07b4970SChristoph Hellwig 		ret = nvmet_ns_enable(ns);
532a07b4970SChristoph Hellwig 	else
533a07b4970SChristoph Hellwig 		nvmet_ns_disable(ns);
534a07b4970SChristoph Hellwig 
535a07b4970SChristoph Hellwig 	return ret ? ret : count;
536a07b4970SChristoph Hellwig }
537a07b4970SChristoph Hellwig 
538a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, enable);
539a07b4970SChristoph Hellwig 
54055eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
54155eb942eSChaitanya Kulkarni {
54255eb942eSChaitanya Kulkarni 	return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
54355eb942eSChaitanya Kulkarni }
54455eb942eSChaitanya Kulkarni 
54555eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
54655eb942eSChaitanya Kulkarni 		const char *page, size_t count)
54755eb942eSChaitanya Kulkarni {
54855eb942eSChaitanya Kulkarni 	struct nvmet_ns *ns = to_nvmet_ns(item);
54955eb942eSChaitanya Kulkarni 	bool val;
55055eb942eSChaitanya Kulkarni 
55155eb942eSChaitanya Kulkarni 	if (strtobool(page, &val))
55255eb942eSChaitanya Kulkarni 		return -EINVAL;
55355eb942eSChaitanya Kulkarni 
55455eb942eSChaitanya Kulkarni 	mutex_lock(&ns->subsys->lock);
55555eb942eSChaitanya Kulkarni 	if (ns->enabled) {
55655eb942eSChaitanya Kulkarni 		pr_err("disable ns before setting buffered_io value.\n");
55755eb942eSChaitanya Kulkarni 		mutex_unlock(&ns->subsys->lock);
55855eb942eSChaitanya Kulkarni 		return -EINVAL;
55955eb942eSChaitanya Kulkarni 	}
56055eb942eSChaitanya Kulkarni 
56155eb942eSChaitanya Kulkarni 	ns->buffered_io = val;
56255eb942eSChaitanya Kulkarni 	mutex_unlock(&ns->subsys->lock);
56355eb942eSChaitanya Kulkarni 	return count;
56455eb942eSChaitanya Kulkarni }
56555eb942eSChaitanya Kulkarni 
56655eb942eSChaitanya Kulkarni CONFIGFS_ATTR(nvmet_ns_, buffered_io);
56755eb942eSChaitanya Kulkarni 
5681f357548SChaitanya Kulkarni static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
5691f357548SChaitanya Kulkarni 		const char *page, size_t count)
5701f357548SChaitanya Kulkarni {
5711f357548SChaitanya Kulkarni 	struct nvmet_ns *ns = to_nvmet_ns(item);
5721f357548SChaitanya Kulkarni 	bool val;
5731f357548SChaitanya Kulkarni 
5741f357548SChaitanya Kulkarni 	if (strtobool(page, &val))
5751f357548SChaitanya Kulkarni 		return -EINVAL;
5761f357548SChaitanya Kulkarni 
5771f357548SChaitanya Kulkarni 	if (!val)
5781f357548SChaitanya Kulkarni 		return -EINVAL;
5791f357548SChaitanya Kulkarni 
5801f357548SChaitanya Kulkarni 	mutex_lock(&ns->subsys->lock);
5811f357548SChaitanya Kulkarni 	if (!ns->enabled) {
5821f357548SChaitanya Kulkarni 		pr_err("enable ns before revalidate.\n");
5831f357548SChaitanya Kulkarni 		mutex_unlock(&ns->subsys->lock);
5841f357548SChaitanya Kulkarni 		return -EINVAL;
5851f357548SChaitanya Kulkarni 	}
5861f357548SChaitanya Kulkarni 	nvmet_ns_revalidate(ns);
5871f357548SChaitanya Kulkarni 	mutex_unlock(&ns->subsys->lock);
5881f357548SChaitanya Kulkarni 	return count;
5891f357548SChaitanya Kulkarni }
5901f357548SChaitanya Kulkarni 
5911f357548SChaitanya Kulkarni CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
5921f357548SChaitanya Kulkarni 
593a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_ns_attrs[] = {
594a07b4970SChristoph Hellwig 	&nvmet_ns_attr_device_path,
595a07b4970SChristoph Hellwig 	&nvmet_ns_attr_device_nguid,
596430c7befSJohannes Thumshirn 	&nvmet_ns_attr_device_uuid,
59762ac0d32SChristoph Hellwig 	&nvmet_ns_attr_ana_grpid,
598a07b4970SChristoph Hellwig 	&nvmet_ns_attr_enable,
59955eb942eSChaitanya Kulkarni 	&nvmet_ns_attr_buffered_io,
6001f357548SChaitanya Kulkarni 	&nvmet_ns_attr_revalidate_size,
601c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA
602c6925093SLogan Gunthorpe 	&nvmet_ns_attr_p2pmem,
603c6925093SLogan Gunthorpe #endif
604a07b4970SChristoph Hellwig 	NULL,
605a07b4970SChristoph Hellwig };
606a07b4970SChristoph Hellwig 
607a07b4970SChristoph Hellwig static void nvmet_ns_release(struct config_item *item)
608a07b4970SChristoph Hellwig {
609a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
610a07b4970SChristoph Hellwig 
611a07b4970SChristoph Hellwig 	nvmet_ns_free(ns);
612a07b4970SChristoph Hellwig }
613a07b4970SChristoph Hellwig 
614a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_ns_item_ops = {
615a07b4970SChristoph Hellwig 	.release		= nvmet_ns_release,
616a07b4970SChristoph Hellwig };
617a07b4970SChristoph Hellwig 
61866603a31SBhumika Goyal static const struct config_item_type nvmet_ns_type = {
619a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_ns_item_ops,
620a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_ns_attrs,
621a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
622a07b4970SChristoph Hellwig };
623a07b4970SChristoph Hellwig 
624a07b4970SChristoph Hellwig static struct config_group *nvmet_ns_make(struct config_group *group,
625a07b4970SChristoph Hellwig 		const char *name)
626a07b4970SChristoph Hellwig {
627a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
628a07b4970SChristoph Hellwig 	struct nvmet_ns *ns;
629a07b4970SChristoph Hellwig 	int ret;
630a07b4970SChristoph Hellwig 	u32 nsid;
631a07b4970SChristoph Hellwig 
632a07b4970SChristoph Hellwig 	ret = kstrtou32(name, 0, &nsid);
633a07b4970SChristoph Hellwig 	if (ret)
634a07b4970SChristoph Hellwig 		goto out;
635a07b4970SChristoph Hellwig 
636a07b4970SChristoph Hellwig 	ret = -EINVAL;
6375ba89503SMikhail Skorzhinskii 	if (nsid == 0 || nsid == NVME_NSID_ALL) {
6385ba89503SMikhail Skorzhinskii 		pr_err("invalid nsid %#x", nsid);
639a07b4970SChristoph Hellwig 		goto out;
6405ba89503SMikhail Skorzhinskii 	}
641a07b4970SChristoph Hellwig 
642a07b4970SChristoph Hellwig 	ret = -ENOMEM;
643a07b4970SChristoph Hellwig 	ns = nvmet_ns_alloc(subsys, nsid);
644a07b4970SChristoph Hellwig 	if (!ns)
645a07b4970SChristoph Hellwig 		goto out;
646a07b4970SChristoph Hellwig 	config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
647a07b4970SChristoph Hellwig 
648a07b4970SChristoph Hellwig 	pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
649a07b4970SChristoph Hellwig 
650a07b4970SChristoph Hellwig 	return &ns->group;
651a07b4970SChristoph Hellwig out:
652a07b4970SChristoph Hellwig 	return ERR_PTR(ret);
653a07b4970SChristoph Hellwig }
654a07b4970SChristoph Hellwig 
655a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_namespaces_group_ops = {
656a07b4970SChristoph Hellwig 	.make_group		= nvmet_ns_make,
657a07b4970SChristoph Hellwig };
658a07b4970SChristoph Hellwig 
65966603a31SBhumika Goyal static const struct config_item_type nvmet_namespaces_type = {
660a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_namespaces_group_ops,
661a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
662a07b4970SChristoph Hellwig };
663a07b4970SChristoph Hellwig 
664cae5b01aSLogan Gunthorpe #ifdef CONFIG_NVME_TARGET_PASSTHRU
665cae5b01aSLogan Gunthorpe 
666cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
667cae5b01aSLogan Gunthorpe 		char *page)
668cae5b01aSLogan Gunthorpe {
669cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
670cae5b01aSLogan Gunthorpe 
671cae5b01aSLogan Gunthorpe 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
672cae5b01aSLogan Gunthorpe }
673cae5b01aSLogan Gunthorpe 
674cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
675cae5b01aSLogan Gunthorpe 		const char *page, size_t count)
676cae5b01aSLogan Gunthorpe {
677cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
678cae5b01aSLogan Gunthorpe 	size_t len;
679cae5b01aSLogan Gunthorpe 	int ret;
680cae5b01aSLogan Gunthorpe 
681cae5b01aSLogan Gunthorpe 	mutex_lock(&subsys->lock);
682cae5b01aSLogan Gunthorpe 
683cae5b01aSLogan Gunthorpe 	ret = -EBUSY;
684cae5b01aSLogan Gunthorpe 	if (subsys->passthru_ctrl)
685cae5b01aSLogan Gunthorpe 		goto out_unlock;
686cae5b01aSLogan Gunthorpe 
687cae5b01aSLogan Gunthorpe 	ret = -EINVAL;
688cae5b01aSLogan Gunthorpe 	len = strcspn(page, "\n");
689cae5b01aSLogan Gunthorpe 	if (!len)
690cae5b01aSLogan Gunthorpe 		goto out_unlock;
691cae5b01aSLogan Gunthorpe 
692cae5b01aSLogan Gunthorpe 	kfree(subsys->passthru_ctrl_path);
693cae5b01aSLogan Gunthorpe 	ret = -ENOMEM;
694cae5b01aSLogan Gunthorpe 	subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
695cae5b01aSLogan Gunthorpe 	if (!subsys->passthru_ctrl_path)
696cae5b01aSLogan Gunthorpe 		goto out_unlock;
697cae5b01aSLogan Gunthorpe 
698cae5b01aSLogan Gunthorpe 	mutex_unlock(&subsys->lock);
699cae5b01aSLogan Gunthorpe 
700cae5b01aSLogan Gunthorpe 	return count;
701cae5b01aSLogan Gunthorpe out_unlock:
702cae5b01aSLogan Gunthorpe 	mutex_unlock(&subsys->lock);
703cae5b01aSLogan Gunthorpe 	return ret;
704cae5b01aSLogan Gunthorpe }
705cae5b01aSLogan Gunthorpe CONFIGFS_ATTR(nvmet_passthru_, device_path);
706cae5b01aSLogan Gunthorpe 
707cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_enable_show(struct config_item *item,
708cae5b01aSLogan Gunthorpe 		char *page)
709cae5b01aSLogan Gunthorpe {
710cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
711cae5b01aSLogan Gunthorpe 
712cae5b01aSLogan Gunthorpe 	return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
713cae5b01aSLogan Gunthorpe }
714cae5b01aSLogan Gunthorpe 
715cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_enable_store(struct config_item *item,
716cae5b01aSLogan Gunthorpe 		const char *page, size_t count)
717cae5b01aSLogan Gunthorpe {
718cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
719cae5b01aSLogan Gunthorpe 	bool enable;
720cae5b01aSLogan Gunthorpe 	int ret = 0;
721cae5b01aSLogan Gunthorpe 
722cae5b01aSLogan Gunthorpe 	if (strtobool(page, &enable))
723cae5b01aSLogan Gunthorpe 		return -EINVAL;
724cae5b01aSLogan Gunthorpe 
725cae5b01aSLogan Gunthorpe 	if (enable)
726cae5b01aSLogan Gunthorpe 		ret = nvmet_passthru_ctrl_enable(subsys);
727cae5b01aSLogan Gunthorpe 	else
728cae5b01aSLogan Gunthorpe 		nvmet_passthru_ctrl_disable(subsys);
729cae5b01aSLogan Gunthorpe 
730cae5b01aSLogan Gunthorpe 	return ret ? ret : count;
731cae5b01aSLogan Gunthorpe }
732cae5b01aSLogan Gunthorpe CONFIGFS_ATTR(nvmet_passthru_, enable);
733cae5b01aSLogan Gunthorpe 
734a2f6a2b8SChaitanya Kulkarni static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
735a2f6a2b8SChaitanya Kulkarni 		char *page)
736a2f6a2b8SChaitanya Kulkarni {
737a2f6a2b8SChaitanya Kulkarni 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->admin_timeout);
738a2f6a2b8SChaitanya Kulkarni }
739a2f6a2b8SChaitanya Kulkarni 
740a2f6a2b8SChaitanya Kulkarni static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
741a2f6a2b8SChaitanya Kulkarni 		const char *page, size_t count)
742a2f6a2b8SChaitanya Kulkarni {
743a2f6a2b8SChaitanya Kulkarni 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
744a2f6a2b8SChaitanya Kulkarni 	unsigned int timeout;
745a2f6a2b8SChaitanya Kulkarni 
746a2f6a2b8SChaitanya Kulkarni 	if (kstrtouint(page, 0, &timeout))
747a2f6a2b8SChaitanya Kulkarni 		return -EINVAL;
748a2f6a2b8SChaitanya Kulkarni 	subsys->admin_timeout = timeout;
749a2f6a2b8SChaitanya Kulkarni 	return count;
750a2f6a2b8SChaitanya Kulkarni }
751a2f6a2b8SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
752a2f6a2b8SChaitanya Kulkarni 
75347e9730cSChaitanya Kulkarni static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
75447e9730cSChaitanya Kulkarni 		char *page)
75547e9730cSChaitanya Kulkarni {
75647e9730cSChaitanya Kulkarni 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->io_timeout);
75747e9730cSChaitanya Kulkarni }
75847e9730cSChaitanya Kulkarni 
75947e9730cSChaitanya Kulkarni static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
76047e9730cSChaitanya Kulkarni 		const char *page, size_t count)
76147e9730cSChaitanya Kulkarni {
76247e9730cSChaitanya Kulkarni 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
76347e9730cSChaitanya Kulkarni 	unsigned int timeout;
76447e9730cSChaitanya Kulkarni 
76547e9730cSChaitanya Kulkarni 	if (kstrtouint(page, 0, &timeout))
76647e9730cSChaitanya Kulkarni 		return -EINVAL;
76747e9730cSChaitanya Kulkarni 	subsys->io_timeout = timeout;
76847e9730cSChaitanya Kulkarni 	return count;
76947e9730cSChaitanya Kulkarni }
77047e9730cSChaitanya Kulkarni CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
77147e9730cSChaitanya Kulkarni 
772cae5b01aSLogan Gunthorpe static struct configfs_attribute *nvmet_passthru_attrs[] = {
773cae5b01aSLogan Gunthorpe 	&nvmet_passthru_attr_device_path,
774cae5b01aSLogan Gunthorpe 	&nvmet_passthru_attr_enable,
775a2f6a2b8SChaitanya Kulkarni 	&nvmet_passthru_attr_admin_timeout,
77647e9730cSChaitanya Kulkarni 	&nvmet_passthru_attr_io_timeout,
777cae5b01aSLogan Gunthorpe 	NULL,
778cae5b01aSLogan Gunthorpe };
779cae5b01aSLogan Gunthorpe 
780cae5b01aSLogan Gunthorpe static const struct config_item_type nvmet_passthru_type = {
781cae5b01aSLogan Gunthorpe 	.ct_attrs		= nvmet_passthru_attrs,
782cae5b01aSLogan Gunthorpe 	.ct_owner		= THIS_MODULE,
783cae5b01aSLogan Gunthorpe };
784cae5b01aSLogan Gunthorpe 
785cae5b01aSLogan Gunthorpe static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
786cae5b01aSLogan Gunthorpe {
787cae5b01aSLogan Gunthorpe 	config_group_init_type_name(&subsys->passthru_group,
788cae5b01aSLogan Gunthorpe 				    "passthru", &nvmet_passthru_type);
789cae5b01aSLogan Gunthorpe 	configfs_add_default_group(&subsys->passthru_group,
790cae5b01aSLogan Gunthorpe 				   &subsys->group);
791cae5b01aSLogan Gunthorpe }
792cae5b01aSLogan Gunthorpe 
793cae5b01aSLogan Gunthorpe #else /* CONFIG_NVME_TARGET_PASSTHRU */
794cae5b01aSLogan Gunthorpe 
795cae5b01aSLogan Gunthorpe static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
796cae5b01aSLogan Gunthorpe {
797cae5b01aSLogan Gunthorpe }
798cae5b01aSLogan Gunthorpe 
799cae5b01aSLogan Gunthorpe #endif /* CONFIG_NVME_TARGET_PASSTHRU */
800cae5b01aSLogan Gunthorpe 
801a07b4970SChristoph Hellwig static int nvmet_port_subsys_allow_link(struct config_item *parent,
802a07b4970SChristoph Hellwig 		struct config_item *target)
803a07b4970SChristoph Hellwig {
804a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
805a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys;
806a07b4970SChristoph Hellwig 	struct nvmet_subsys_link *link, *p;
807a07b4970SChristoph Hellwig 	int ret;
808a07b4970SChristoph Hellwig 
809a07b4970SChristoph Hellwig 	if (target->ci_type != &nvmet_subsys_type) {
810a07b4970SChristoph Hellwig 		pr_err("can only link subsystems into the subsystems dir.!\n");
811a07b4970SChristoph Hellwig 		return -EINVAL;
812a07b4970SChristoph Hellwig 	}
813a07b4970SChristoph Hellwig 	subsys = to_subsys(target);
814a07b4970SChristoph Hellwig 	link = kmalloc(sizeof(*link), GFP_KERNEL);
815a07b4970SChristoph Hellwig 	if (!link)
816a07b4970SChristoph Hellwig 		return -ENOMEM;
817a07b4970SChristoph Hellwig 	link->subsys = subsys;
818a07b4970SChristoph Hellwig 
819a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
820a07b4970SChristoph Hellwig 	ret = -EEXIST;
821a07b4970SChristoph Hellwig 	list_for_each_entry(p, &port->subsystems, entry) {
822a07b4970SChristoph Hellwig 		if (p->subsys == subsys)
823a07b4970SChristoph Hellwig 			goto out_free_link;
824a07b4970SChristoph Hellwig 	}
825a07b4970SChristoph Hellwig 
826a07b4970SChristoph Hellwig 	if (list_empty(&port->subsystems)) {
827a07b4970SChristoph Hellwig 		ret = nvmet_enable_port(port);
828a07b4970SChristoph Hellwig 		if (ret)
829a07b4970SChristoph Hellwig 			goto out_free_link;
830a07b4970SChristoph Hellwig 	}
831a07b4970SChristoph Hellwig 
832a07b4970SChristoph Hellwig 	list_add_tail(&link->entry, &port->subsystems);
833b662a078SJay Sternberg 	nvmet_port_disc_changed(port, subsys);
834b662a078SJay Sternberg 
835a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
836a07b4970SChristoph Hellwig 	return 0;
837a07b4970SChristoph Hellwig 
838a07b4970SChristoph Hellwig out_free_link:
839a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
840a07b4970SChristoph Hellwig 	kfree(link);
841a07b4970SChristoph Hellwig 	return ret;
842a07b4970SChristoph Hellwig }
843a07b4970SChristoph Hellwig 
844e16769d4SAndrzej Pietrasiewicz static void nvmet_port_subsys_drop_link(struct config_item *parent,
845a07b4970SChristoph Hellwig 		struct config_item *target)
846a07b4970SChristoph Hellwig {
847a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
848a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(target);
849a07b4970SChristoph Hellwig 	struct nvmet_subsys_link *p;
850a07b4970SChristoph Hellwig 
851a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
852a07b4970SChristoph Hellwig 	list_for_each_entry(p, &port->subsystems, entry) {
853a07b4970SChristoph Hellwig 		if (p->subsys == subsys)
854a07b4970SChristoph Hellwig 			goto found;
855a07b4970SChristoph Hellwig 	}
856a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
857e16769d4SAndrzej Pietrasiewicz 	return;
858a07b4970SChristoph Hellwig 
859a07b4970SChristoph Hellwig found:
860a07b4970SChristoph Hellwig 	list_del(&p->entry);
8613aed8673SLogan Gunthorpe 	nvmet_port_del_ctrls(port, subsys);
862b662a078SJay Sternberg 	nvmet_port_disc_changed(port, subsys);
863b662a078SJay Sternberg 
864a07b4970SChristoph Hellwig 	if (list_empty(&port->subsystems))
865a07b4970SChristoph Hellwig 		nvmet_disable_port(port);
866a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
867a07b4970SChristoph Hellwig 	kfree(p);
868a07b4970SChristoph Hellwig }
869a07b4970SChristoph Hellwig 
870a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_subsys_item_ops = {
871a07b4970SChristoph Hellwig 	.allow_link		= nvmet_port_subsys_allow_link,
872a07b4970SChristoph Hellwig 	.drop_link		= nvmet_port_subsys_drop_link,
873a07b4970SChristoph Hellwig };
874a07b4970SChristoph Hellwig 
87566603a31SBhumika Goyal static const struct config_item_type nvmet_port_subsys_type = {
876a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_port_subsys_item_ops,
877a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
878a07b4970SChristoph Hellwig };
879a07b4970SChristoph Hellwig 
880a07b4970SChristoph Hellwig static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
881a07b4970SChristoph Hellwig 		struct config_item *target)
882a07b4970SChristoph Hellwig {
883a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
884a07b4970SChristoph Hellwig 	struct nvmet_host *host;
885a07b4970SChristoph Hellwig 	struct nvmet_host_link *link, *p;
886a07b4970SChristoph Hellwig 	int ret;
887a07b4970SChristoph Hellwig 
888a07b4970SChristoph Hellwig 	if (target->ci_type != &nvmet_host_type) {
889a07b4970SChristoph Hellwig 		pr_err("can only link hosts into the allowed_hosts directory!\n");
890a07b4970SChristoph Hellwig 		return -EINVAL;
891a07b4970SChristoph Hellwig 	}
892a07b4970SChristoph Hellwig 
893a07b4970SChristoph Hellwig 	host = to_host(target);
894a07b4970SChristoph Hellwig 	link = kmalloc(sizeof(*link), GFP_KERNEL);
895a07b4970SChristoph Hellwig 	if (!link)
896a07b4970SChristoph Hellwig 		return -ENOMEM;
897a07b4970SChristoph Hellwig 	link->host = host;
898a07b4970SChristoph Hellwig 
899a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
900a07b4970SChristoph Hellwig 	ret = -EINVAL;
901a07b4970SChristoph Hellwig 	if (subsys->allow_any_host) {
902a07b4970SChristoph Hellwig 		pr_err("can't add hosts when allow_any_host is set!\n");
903a07b4970SChristoph Hellwig 		goto out_free_link;
904a07b4970SChristoph Hellwig 	}
905a07b4970SChristoph Hellwig 
906a07b4970SChristoph Hellwig 	ret = -EEXIST;
907a07b4970SChristoph Hellwig 	list_for_each_entry(p, &subsys->hosts, entry) {
908a07b4970SChristoph Hellwig 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
909a07b4970SChristoph Hellwig 			goto out_free_link;
910a07b4970SChristoph Hellwig 	}
911a07b4970SChristoph Hellwig 	list_add_tail(&link->entry, &subsys->hosts);
912b662a078SJay Sternberg 	nvmet_subsys_disc_changed(subsys, host);
913b662a078SJay Sternberg 
914a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
915a07b4970SChristoph Hellwig 	return 0;
916a07b4970SChristoph Hellwig out_free_link:
917a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
918a07b4970SChristoph Hellwig 	kfree(link);
919a07b4970SChristoph Hellwig 	return ret;
920a07b4970SChristoph Hellwig }
921a07b4970SChristoph Hellwig 
922e16769d4SAndrzej Pietrasiewicz static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
923a07b4970SChristoph Hellwig 		struct config_item *target)
924a07b4970SChristoph Hellwig {
925a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
926a07b4970SChristoph Hellwig 	struct nvmet_host *host = to_host(target);
927a07b4970SChristoph Hellwig 	struct nvmet_host_link *p;
928a07b4970SChristoph Hellwig 
929a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
930a07b4970SChristoph Hellwig 	list_for_each_entry(p, &subsys->hosts, entry) {
931a07b4970SChristoph Hellwig 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
932a07b4970SChristoph Hellwig 			goto found;
933a07b4970SChristoph Hellwig 	}
934a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
935e16769d4SAndrzej Pietrasiewicz 	return;
936a07b4970SChristoph Hellwig 
937a07b4970SChristoph Hellwig found:
938a07b4970SChristoph Hellwig 	list_del(&p->entry);
939b662a078SJay Sternberg 	nvmet_subsys_disc_changed(subsys, host);
940b662a078SJay Sternberg 
941a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
942a07b4970SChristoph Hellwig 	kfree(p);
943a07b4970SChristoph Hellwig }
944a07b4970SChristoph Hellwig 
945a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
946a07b4970SChristoph Hellwig 	.allow_link		= nvmet_allowed_hosts_allow_link,
947a07b4970SChristoph Hellwig 	.drop_link		= nvmet_allowed_hosts_drop_link,
948a07b4970SChristoph Hellwig };
949a07b4970SChristoph Hellwig 
95066603a31SBhumika Goyal static const struct config_item_type nvmet_allowed_hosts_type = {
951a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_allowed_hosts_item_ops,
952a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
953a07b4970SChristoph Hellwig };
954a07b4970SChristoph Hellwig 
955a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
956a07b4970SChristoph Hellwig 		char *page)
957a07b4970SChristoph Hellwig {
958a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n",
959a07b4970SChristoph Hellwig 		to_subsys(item)->allow_any_host);
960a07b4970SChristoph Hellwig }
961a07b4970SChristoph Hellwig 
962a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
963a07b4970SChristoph Hellwig 		const char *page, size_t count)
964a07b4970SChristoph Hellwig {
965a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(item);
966a07b4970SChristoph Hellwig 	bool allow_any_host;
967a07b4970SChristoph Hellwig 	int ret = 0;
968a07b4970SChristoph Hellwig 
969a07b4970SChristoph Hellwig 	if (strtobool(page, &allow_any_host))
970a07b4970SChristoph Hellwig 		return -EINVAL;
971a07b4970SChristoph Hellwig 
972a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
973a07b4970SChristoph Hellwig 	if (allow_any_host && !list_empty(&subsys->hosts)) {
974a07b4970SChristoph Hellwig 		pr_err("Can't set allow_any_host when explicit hosts are set!\n");
975a07b4970SChristoph Hellwig 		ret = -EINVAL;
976a07b4970SChristoph Hellwig 		goto out_unlock;
977a07b4970SChristoph Hellwig 	}
978a07b4970SChristoph Hellwig 
979b662a078SJay Sternberg 	if (subsys->allow_any_host != allow_any_host) {
980a07b4970SChristoph Hellwig 		subsys->allow_any_host = allow_any_host;
981b662a078SJay Sternberg 		nvmet_subsys_disc_changed(subsys, NULL);
982b662a078SJay Sternberg 	}
983b662a078SJay Sternberg 
984a07b4970SChristoph Hellwig out_unlock:
985a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
986a07b4970SChristoph Hellwig 	return ret ? ret : count;
987a07b4970SChristoph Hellwig }
988a07b4970SChristoph Hellwig 
989a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
990a07b4970SChristoph Hellwig 
99141528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
992c61d788bSJohannes Thumshirn 					      char *page)
993c61d788bSJohannes Thumshirn {
994c61d788bSJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
995c61d788bSJohannes Thumshirn 
996c61d788bSJohannes Thumshirn 	if (NVME_TERTIARY(subsys->ver))
997a0f0dbaaSChaitanya Kulkarni 		return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
998a0f0dbaaSChaitanya Kulkarni 				NVME_MAJOR(subsys->ver),
999a0f0dbaaSChaitanya Kulkarni 				NVME_MINOR(subsys->ver),
1000a0f0dbaaSChaitanya Kulkarni 				NVME_TERTIARY(subsys->ver));
1001527123c7SChaitanya Kulkarni 
1002a0f0dbaaSChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
1003a0f0dbaaSChaitanya Kulkarni 			NVME_MAJOR(subsys->ver),
1004a0f0dbaaSChaitanya Kulkarni 			NVME_MINOR(subsys->ver));
1005c61d788bSJohannes Thumshirn }
1006c61d788bSJohannes Thumshirn 
100787fd4cc1SNoam Gottlieb static ssize_t
100887fd4cc1SNoam Gottlieb nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,
1009c61d788bSJohannes Thumshirn 		const char *page, size_t count)
1010c61d788bSJohannes Thumshirn {
1011c61d788bSJohannes Thumshirn 	int major, minor, tertiary = 0;
1012c61d788bSJohannes Thumshirn 	int ret;
1013c61d788bSJohannes Thumshirn 
101487fd4cc1SNoam Gottlieb 	if (subsys->subsys_discovered) {
101587fd4cc1SNoam Gottlieb 		if (NVME_TERTIARY(subsys->ver))
101687fd4cc1SNoam Gottlieb 			pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
101787fd4cc1SNoam Gottlieb 			       NVME_MAJOR(subsys->ver),
101887fd4cc1SNoam Gottlieb 			       NVME_MINOR(subsys->ver),
101987fd4cc1SNoam Gottlieb 			       NVME_TERTIARY(subsys->ver));
102087fd4cc1SNoam Gottlieb 		else
102187fd4cc1SNoam Gottlieb 			pr_err("Can't set version number. %llu.%llu is already assigned\n",
102287fd4cc1SNoam Gottlieb 			       NVME_MAJOR(subsys->ver),
102387fd4cc1SNoam Gottlieb 			       NVME_MINOR(subsys->ver));
102487fd4cc1SNoam Gottlieb 		return -EINVAL;
102587fd4cc1SNoam Gottlieb 	}
102687fd4cc1SNoam Gottlieb 
1027ba76af67SLogan Gunthorpe 	/* passthru subsystems use the underlying controller's version */
1028ab7a2737SChristoph Hellwig 	if (nvmet_is_passthru_subsys(subsys))
1029ba76af67SLogan Gunthorpe 		return -EINVAL;
1030ba76af67SLogan Gunthorpe 
1031c61d788bSJohannes Thumshirn 	ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
1032c61d788bSJohannes Thumshirn 	if (ret != 2 && ret != 3)
1033c61d788bSJohannes Thumshirn 		return -EINVAL;
1034c61d788bSJohannes Thumshirn 
1035c61d788bSJohannes Thumshirn 	subsys->ver = NVME_VS(major, minor, tertiary);
1036c61d788bSJohannes Thumshirn 
1037c61d788bSJohannes Thumshirn 	return count;
1038c61d788bSJohannes Thumshirn }
103987fd4cc1SNoam Gottlieb 
104087fd4cc1SNoam Gottlieb static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
104187fd4cc1SNoam Gottlieb 					       const char *page, size_t count)
104287fd4cc1SNoam Gottlieb {
104387fd4cc1SNoam Gottlieb 	struct nvmet_subsys *subsys = to_subsys(item);
104487fd4cc1SNoam Gottlieb 	ssize_t ret;
104587fd4cc1SNoam Gottlieb 
104687fd4cc1SNoam Gottlieb 	down_write(&nvmet_config_sem);
104787fd4cc1SNoam Gottlieb 	mutex_lock(&subsys->lock);
104887fd4cc1SNoam Gottlieb 	ret = nvmet_subsys_attr_version_store_locked(subsys, page, count);
104987fd4cc1SNoam Gottlieb 	mutex_unlock(&subsys->lock);
105087fd4cc1SNoam Gottlieb 	up_write(&nvmet_config_sem);
105187fd4cc1SNoam Gottlieb 
105287fd4cc1SNoam Gottlieb 	return ret;
105387fd4cc1SNoam Gottlieb }
105441528f80SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_version);
1055c61d788bSJohannes Thumshirn 
1056e13b0615SNoam Gottlieb /* See Section 1.5 of NVMe 1.4 */
1057e13b0615SNoam Gottlieb static bool nvmet_is_ascii(const char c)
1058e13b0615SNoam Gottlieb {
1059e13b0615SNoam Gottlieb 	return c >= 0x20 && c <= 0x7e;
1060e13b0615SNoam Gottlieb }
1061e13b0615SNoam Gottlieb 
1062fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
1063fcbc5459SJohannes Thumshirn 					     char *page)
1064fcbc5459SJohannes Thumshirn {
1065fcbc5459SJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
1066fcbc5459SJohannes Thumshirn 
10670bd46e22SDan Carpenter 	return snprintf(page, PAGE_SIZE, "%.*s\n",
1068f0406481SHannes Reinecke 			NVMET_SN_MAX_SIZE, subsys->serial);
1069fcbc5459SJohannes Thumshirn }
1070fcbc5459SJohannes Thumshirn 
10717ae023c5SNoam Gottlieb static ssize_t
10727ae023c5SNoam Gottlieb nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
1073fcbc5459SJohannes Thumshirn 		const char *page, size_t count)
1074fcbc5459SJohannes Thumshirn {
1075e13b0615SNoam Gottlieb 	int pos, len = strcspn(page, "\n");
1076d3a9b0caSChaitanya Kulkarni 
10777ae023c5SNoam Gottlieb 	if (subsys->subsys_discovered) {
10787ae023c5SNoam Gottlieb 		pr_err("Can't set serial number. %s is already assigned\n",
10797ae023c5SNoam Gottlieb 		       subsys->serial);
10807ae023c5SNoam Gottlieb 		return -EINVAL;
10817ae023c5SNoam Gottlieb 	}
10827ae023c5SNoam Gottlieb 
1083e13b0615SNoam Gottlieb 	if (!len || len > NVMET_SN_MAX_SIZE) {
1084e13b0615SNoam Gottlieb 		pr_err("Serial Number can not be empty or exceed %d Bytes\n",
1085e13b0615SNoam Gottlieb 		       NVMET_SN_MAX_SIZE);
1086d3a9b0caSChaitanya Kulkarni 		return -EINVAL;
1087e13b0615SNoam Gottlieb 	}
1088e13b0615SNoam Gottlieb 
1089e13b0615SNoam Gottlieb 	for (pos = 0; pos < len; pos++) {
1090e13b0615SNoam Gottlieb 		if (!nvmet_is_ascii(page[pos])) {
1091e13b0615SNoam Gottlieb 			pr_err("Serial Number must contain only ASCII strings\n");
1092e13b0615SNoam Gottlieb 			return -EINVAL;
1093e13b0615SNoam Gottlieb 		}
1094e13b0615SNoam Gottlieb 	}
1095fcbc5459SJohannes Thumshirn 
10967ae023c5SNoam Gottlieb 	memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
10977ae023c5SNoam Gottlieb 
10987ae023c5SNoam Gottlieb 	return count;
10997ae023c5SNoam Gottlieb }
11007ae023c5SNoam Gottlieb 
11017ae023c5SNoam Gottlieb static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
11027ae023c5SNoam Gottlieb 					      const char *page, size_t count)
11037ae023c5SNoam Gottlieb {
11047ae023c5SNoam Gottlieb 	struct nvmet_subsys *subsys = to_subsys(item);
11057ae023c5SNoam Gottlieb 	ssize_t ret;
11067ae023c5SNoam Gottlieb 
1107fcbc5459SJohannes Thumshirn 	down_write(&nvmet_config_sem);
1108e13b0615SNoam Gottlieb 	mutex_lock(&subsys->lock);
11097ae023c5SNoam Gottlieb 	ret = nvmet_subsys_attr_serial_store_locked(subsys, page, count);
1110e13b0615SNoam Gottlieb 	mutex_unlock(&subsys->lock);
1111fcbc5459SJohannes Thumshirn 	up_write(&nvmet_config_sem);
1112fcbc5459SJohannes Thumshirn 
11137ae023c5SNoam Gottlieb 	return ret;
1114fcbc5459SJohannes Thumshirn }
1115fcbc5459SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
1116fcbc5459SJohannes Thumshirn 
111794a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
111894a39d61SChaitanya Kulkarni 						 char *page)
111994a39d61SChaitanya Kulkarni {
112094a39d61SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
112194a39d61SChaitanya Kulkarni }
112294a39d61SChaitanya Kulkarni 
112394a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
112494a39d61SChaitanya Kulkarni 						  const char *page, size_t cnt)
112594a39d61SChaitanya Kulkarni {
112694a39d61SChaitanya Kulkarni 	u16 cntlid_min;
112794a39d61SChaitanya Kulkarni 
112894a39d61SChaitanya Kulkarni 	if (sscanf(page, "%hu\n", &cntlid_min) != 1)
112994a39d61SChaitanya Kulkarni 		return -EINVAL;
113094a39d61SChaitanya Kulkarni 
113194a39d61SChaitanya Kulkarni 	if (cntlid_min == 0)
113294a39d61SChaitanya Kulkarni 		return -EINVAL;
113394a39d61SChaitanya Kulkarni 
113494a39d61SChaitanya Kulkarni 	down_write(&nvmet_config_sem);
113594a39d61SChaitanya Kulkarni 	if (cntlid_min >= to_subsys(item)->cntlid_max)
113694a39d61SChaitanya Kulkarni 		goto out_unlock;
113794a39d61SChaitanya Kulkarni 	to_subsys(item)->cntlid_min = cntlid_min;
113894a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
113994a39d61SChaitanya Kulkarni 	return cnt;
114094a39d61SChaitanya Kulkarni 
114194a39d61SChaitanya Kulkarni out_unlock:
114294a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
114394a39d61SChaitanya Kulkarni 	return -EINVAL;
114494a39d61SChaitanya Kulkarni }
114594a39d61SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
114694a39d61SChaitanya Kulkarni 
114794a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
114894a39d61SChaitanya Kulkarni 						 char *page)
114994a39d61SChaitanya Kulkarni {
115094a39d61SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
115194a39d61SChaitanya Kulkarni }
115294a39d61SChaitanya Kulkarni 
115394a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
115494a39d61SChaitanya Kulkarni 						  const char *page, size_t cnt)
115594a39d61SChaitanya Kulkarni {
115694a39d61SChaitanya Kulkarni 	u16 cntlid_max;
115794a39d61SChaitanya Kulkarni 
115894a39d61SChaitanya Kulkarni 	if (sscanf(page, "%hu\n", &cntlid_max) != 1)
115994a39d61SChaitanya Kulkarni 		return -EINVAL;
116094a39d61SChaitanya Kulkarni 
116194a39d61SChaitanya Kulkarni 	if (cntlid_max == 0)
116294a39d61SChaitanya Kulkarni 		return -EINVAL;
116394a39d61SChaitanya Kulkarni 
116494a39d61SChaitanya Kulkarni 	down_write(&nvmet_config_sem);
116594a39d61SChaitanya Kulkarni 	if (cntlid_max <= to_subsys(item)->cntlid_min)
116694a39d61SChaitanya Kulkarni 		goto out_unlock;
116794a39d61SChaitanya Kulkarni 	to_subsys(item)->cntlid_max = cntlid_max;
116894a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
116994a39d61SChaitanya Kulkarni 	return cnt;
117094a39d61SChaitanya Kulkarni 
117194a39d61SChaitanya Kulkarni out_unlock:
117294a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
117394a39d61SChaitanya Kulkarni 	return -EINVAL;
117494a39d61SChaitanya Kulkarni }
117594a39d61SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
117694a39d61SChaitanya Kulkarni 
1177013b7ebeSMark Ruijter static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
1178013b7ebeSMark Ruijter 					    char *page)
1179013b7ebeSMark Ruijter {
1180013b7ebeSMark Ruijter 	struct nvmet_subsys *subsys = to_subsys(item);
1181013b7ebeSMark Ruijter 
11820d148efdSNoam Gottlieb 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->model_number);
1183013b7ebeSMark Ruijter }
1184013b7ebeSMark Ruijter 
1185d9f273b7SMax Gurtovoy static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
1186013b7ebeSMark Ruijter 		const char *page, size_t count)
1187013b7ebeSMark Ruijter {
1188013b7ebeSMark Ruijter 	int pos = 0, len;
1189013b7ebeSMark Ruijter 
11900d148efdSNoam Gottlieb 	if (subsys->subsys_discovered) {
1191d9f273b7SMax Gurtovoy 		pr_err("Can't set model number. %s is already assigned\n",
1192d9f273b7SMax Gurtovoy 		       subsys->model_number);
1193d9f273b7SMax Gurtovoy 		return -EINVAL;
1194d9f273b7SMax Gurtovoy 	}
1195d9f273b7SMax Gurtovoy 
1196013b7ebeSMark Ruijter 	len = strcspn(page, "\n");
1197013b7ebeSMark Ruijter 	if (!len)
1198013b7ebeSMark Ruijter 		return -EINVAL;
1199013b7ebeSMark Ruijter 
120048b4c010SNoam Gottlieb 	if (len > NVMET_MN_MAX_SIZE) {
1201ccc1003bSColin Ian King 		pr_err("Model number size can not exceed %d Bytes\n",
120248b4c010SNoam Gottlieb 		       NVMET_MN_MAX_SIZE);
120348b4c010SNoam Gottlieb 		return -EINVAL;
120448b4c010SNoam Gottlieb 	}
120548b4c010SNoam Gottlieb 
1206013b7ebeSMark Ruijter 	for (pos = 0; pos < len; pos++) {
1207013b7ebeSMark Ruijter 		if (!nvmet_is_ascii(page[pos]))
1208013b7ebeSMark Ruijter 			return -EINVAL;
1209013b7ebeSMark Ruijter 	}
1210013b7ebeSMark Ruijter 
1211d9f273b7SMax Gurtovoy 	subsys->model_number = kmemdup_nul(page, len, GFP_KERNEL);
1212d9f273b7SMax Gurtovoy 	if (!subsys->model_number)
1213013b7ebeSMark Ruijter 		return -ENOMEM;
1214d9f273b7SMax Gurtovoy 	return count;
1215013b7ebeSMark Ruijter }
1216d9f273b7SMax Gurtovoy 
1217d9f273b7SMax Gurtovoy static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
1218d9f273b7SMax Gurtovoy 					     const char *page, size_t count)
1219d9f273b7SMax Gurtovoy {
1220d9f273b7SMax Gurtovoy 	struct nvmet_subsys *subsys = to_subsys(item);
1221d9f273b7SMax Gurtovoy 	ssize_t ret;
1222013b7ebeSMark Ruijter 
1223013b7ebeSMark Ruijter 	down_write(&nvmet_config_sem);
1224013b7ebeSMark Ruijter 	mutex_lock(&subsys->lock);
1225d9f273b7SMax Gurtovoy 	ret = nvmet_subsys_attr_model_store_locked(subsys, page, count);
1226013b7ebeSMark Ruijter 	mutex_unlock(&subsys->lock);
1227013b7ebeSMark Ruijter 	up_write(&nvmet_config_sem);
1228013b7ebeSMark Ruijter 
1229d9f273b7SMax Gurtovoy 	return ret;
1230013b7ebeSMark Ruijter }
1231013b7ebeSMark Ruijter CONFIGFS_ATTR(nvmet_subsys_, attr_model);
1232013b7ebeSMark Ruijter 
1233626851e9SHannes Reinecke static ssize_t nvmet_subsys_attr_discovery_nqn_show(struct config_item *item,
1234626851e9SHannes Reinecke 			char *page)
1235626851e9SHannes Reinecke {
1236*73d77c53SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%s\n", nvmet_disc_subsys->subsysnqn);
1237626851e9SHannes Reinecke }
1238626851e9SHannes Reinecke 
1239626851e9SHannes Reinecke static ssize_t nvmet_subsys_attr_discovery_nqn_store(struct config_item *item,
1240626851e9SHannes Reinecke 			const char *page, size_t count)
1241626851e9SHannes Reinecke {
1242626851e9SHannes Reinecke 	struct nvmet_subsys *subsys = to_subsys(item);
1243626851e9SHannes Reinecke 	char *subsysnqn;
1244626851e9SHannes Reinecke 	int len;
1245626851e9SHannes Reinecke 
1246626851e9SHannes Reinecke 	len = strcspn(page, "\n");
1247626851e9SHannes Reinecke 	if (!len)
1248626851e9SHannes Reinecke 		return -EINVAL;
1249626851e9SHannes Reinecke 
1250626851e9SHannes Reinecke 	subsysnqn = kmemdup_nul(page, len, GFP_KERNEL);
1251626851e9SHannes Reinecke 	if (!subsysnqn)
1252626851e9SHannes Reinecke 		return -ENOMEM;
1253626851e9SHannes Reinecke 
1254626851e9SHannes Reinecke 	/*
1255626851e9SHannes Reinecke 	 * The discovery NQN must be different from subsystem NQN.
1256626851e9SHannes Reinecke 	 */
1257626851e9SHannes Reinecke 	if (!strcmp(subsysnqn, subsys->subsysnqn)) {
1258626851e9SHannes Reinecke 		kfree(subsysnqn);
1259626851e9SHannes Reinecke 		return -EBUSY;
1260626851e9SHannes Reinecke 	}
1261626851e9SHannes Reinecke 	down_write(&nvmet_config_sem);
1262626851e9SHannes Reinecke 	kfree(nvmet_disc_subsys->subsysnqn);
1263626851e9SHannes Reinecke 	nvmet_disc_subsys->subsysnqn = subsysnqn;
1264626851e9SHannes Reinecke 	up_write(&nvmet_config_sem);
1265626851e9SHannes Reinecke 
1266626851e9SHannes Reinecke 	return count;
1267626851e9SHannes Reinecke }
1268626851e9SHannes Reinecke CONFIGFS_ATTR(nvmet_subsys_, attr_discovery_nqn);
1269626851e9SHannes Reinecke 
1270ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1271ea52ac1cSIsrael Rukshin static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
1272ea52ac1cSIsrael Rukshin 						char *page)
1273ea52ac1cSIsrael Rukshin {
1274ea52ac1cSIsrael Rukshin 	return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
1275ea52ac1cSIsrael Rukshin }
1276ea52ac1cSIsrael Rukshin 
1277ea52ac1cSIsrael Rukshin static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
1278ea52ac1cSIsrael Rukshin 						 const char *page, size_t count)
1279ea52ac1cSIsrael Rukshin {
1280ea52ac1cSIsrael Rukshin 	struct nvmet_subsys *subsys = to_subsys(item);
1281ea52ac1cSIsrael Rukshin 	bool pi_enable;
1282ea52ac1cSIsrael Rukshin 
1283ea52ac1cSIsrael Rukshin 	if (strtobool(page, &pi_enable))
1284ea52ac1cSIsrael Rukshin 		return -EINVAL;
1285ea52ac1cSIsrael Rukshin 
1286ea52ac1cSIsrael Rukshin 	subsys->pi_support = pi_enable;
1287ea52ac1cSIsrael Rukshin 	return count;
1288ea52ac1cSIsrael Rukshin }
1289ea52ac1cSIsrael Rukshin CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
1290ea52ac1cSIsrael Rukshin #endif
1291ea52ac1cSIsrael Rukshin 
1292a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_subsys_attrs[] = {
1293a07b4970SChristoph Hellwig 	&nvmet_subsys_attr_attr_allow_any_host,
129441528f80SJohannes Thumshirn 	&nvmet_subsys_attr_attr_version,
1295fcbc5459SJohannes Thumshirn 	&nvmet_subsys_attr_attr_serial,
129694a39d61SChaitanya Kulkarni 	&nvmet_subsys_attr_attr_cntlid_min,
129794a39d61SChaitanya Kulkarni 	&nvmet_subsys_attr_attr_cntlid_max,
1298013b7ebeSMark Ruijter 	&nvmet_subsys_attr_attr_model,
1299626851e9SHannes Reinecke 	&nvmet_subsys_attr_attr_discovery_nqn,
1300ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1301ea52ac1cSIsrael Rukshin 	&nvmet_subsys_attr_attr_pi_enable,
1302ea52ac1cSIsrael Rukshin #endif
1303a07b4970SChristoph Hellwig 	NULL,
1304a07b4970SChristoph Hellwig };
1305a07b4970SChristoph Hellwig 
1306a07b4970SChristoph Hellwig /*
1307a07b4970SChristoph Hellwig  * Subsystem structures & folder operation functions below
1308a07b4970SChristoph Hellwig  */
1309a07b4970SChristoph Hellwig static void nvmet_subsys_release(struct config_item *item)
1310a07b4970SChristoph Hellwig {
1311a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(item);
1312a07b4970SChristoph Hellwig 
1313344770b0SSagi Grimberg 	nvmet_subsys_del_ctrls(subsys);
1314a07b4970SChristoph Hellwig 	nvmet_subsys_put(subsys);
1315a07b4970SChristoph Hellwig }
1316a07b4970SChristoph Hellwig 
1317a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_subsys_item_ops = {
1318a07b4970SChristoph Hellwig 	.release		= nvmet_subsys_release,
1319a07b4970SChristoph Hellwig };
1320a07b4970SChristoph Hellwig 
132166603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type = {
1322a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_subsys_item_ops,
1323a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_subsys_attrs,
1324a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1325a07b4970SChristoph Hellwig };
1326a07b4970SChristoph Hellwig 
1327a07b4970SChristoph Hellwig static struct config_group *nvmet_subsys_make(struct config_group *group,
1328a07b4970SChristoph Hellwig 		const char *name)
1329a07b4970SChristoph Hellwig {
1330a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys;
1331a07b4970SChristoph Hellwig 
1332a07b4970SChristoph Hellwig 	if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1333a07b4970SChristoph Hellwig 		pr_err("can't create discovery subsystem through configfs\n");
1334a07b4970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1335a07b4970SChristoph Hellwig 	}
1336a07b4970SChristoph Hellwig 
1337a07b4970SChristoph Hellwig 	subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
13386b7e631bSMinwoo Im 	if (IS_ERR(subsys))
13396b7e631bSMinwoo Im 		return ERR_CAST(subsys);
1340a07b4970SChristoph Hellwig 
1341a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1342a07b4970SChristoph Hellwig 
1343a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->namespaces_group,
1344a07b4970SChristoph Hellwig 			"namespaces", &nvmet_namespaces_type);
1345a07b4970SChristoph Hellwig 	configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1346a07b4970SChristoph Hellwig 
1347a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->allowed_hosts_group,
1348a07b4970SChristoph Hellwig 			"allowed_hosts", &nvmet_allowed_hosts_type);
1349a07b4970SChristoph Hellwig 	configfs_add_default_group(&subsys->allowed_hosts_group,
1350a07b4970SChristoph Hellwig 			&subsys->group);
1351a07b4970SChristoph Hellwig 
1352cae5b01aSLogan Gunthorpe 	nvmet_add_passthru_group(subsys);
1353cae5b01aSLogan Gunthorpe 
1354a07b4970SChristoph Hellwig 	return &subsys->group;
1355a07b4970SChristoph Hellwig }
1356a07b4970SChristoph Hellwig 
1357a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_subsystems_group_ops = {
1358a07b4970SChristoph Hellwig 	.make_group		= nvmet_subsys_make,
1359a07b4970SChristoph Hellwig };
1360a07b4970SChristoph Hellwig 
136166603a31SBhumika Goyal static const struct config_item_type nvmet_subsystems_type = {
1362a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_subsystems_group_ops,
1363a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1364a07b4970SChristoph Hellwig };
1365a07b4970SChristoph Hellwig 
1366a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_show(struct config_item *item,
1367a07b4970SChristoph Hellwig 		char *page)
1368a07b4970SChristoph Hellwig {
1369a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1370a07b4970SChristoph Hellwig }
1371a07b4970SChristoph Hellwig 
1372a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_store(struct config_item *item,
1373a07b4970SChristoph Hellwig 		const char *page, size_t count)
1374a07b4970SChristoph Hellwig {
1375a07b4970SChristoph Hellwig 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1376a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1377a07b4970SChristoph Hellwig 	bool enable;
1378a07b4970SChristoph Hellwig 
1379a07b4970SChristoph Hellwig 	if (strtobool(page, &enable))
1380a07b4970SChristoph Hellwig 		goto inval;
1381a07b4970SChristoph Hellwig 
1382a07b4970SChristoph Hellwig 	if (enable)
1383a07b4970SChristoph Hellwig 		nvmet_referral_enable(parent, port);
1384a07b4970SChristoph Hellwig 	else
1385b662a078SJay Sternberg 		nvmet_referral_disable(parent, port);
1386a07b4970SChristoph Hellwig 
1387a07b4970SChristoph Hellwig 	return count;
1388a07b4970SChristoph Hellwig inval:
1389a07b4970SChristoph Hellwig 	pr_err("Invalid value '%s' for enable\n", page);
1390a07b4970SChristoph Hellwig 	return -EINVAL;
1391a07b4970SChristoph Hellwig }
1392a07b4970SChristoph Hellwig 
1393a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_referral_, enable);
1394a07b4970SChristoph Hellwig 
1395a07b4970SChristoph Hellwig /*
1396a07b4970SChristoph Hellwig  * Discovery Service subsystem definitions
1397a07b4970SChristoph Hellwig  */
1398a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_referral_attrs[] = {
1399a07b4970SChristoph Hellwig 	&nvmet_attr_addr_adrfam,
1400a07b4970SChristoph Hellwig 	&nvmet_attr_addr_portid,
1401a07b4970SChristoph Hellwig 	&nvmet_attr_addr_treq,
1402a07b4970SChristoph Hellwig 	&nvmet_attr_addr_traddr,
1403a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trsvcid,
1404a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trtype,
1405a07b4970SChristoph Hellwig 	&nvmet_referral_attr_enable,
1406a07b4970SChristoph Hellwig 	NULL,
1407a07b4970SChristoph Hellwig };
1408a07b4970SChristoph Hellwig 
1409f0e656e4SSagi Grimberg static void nvmet_referral_notify(struct config_group *group,
1410f0e656e4SSagi Grimberg 		struct config_item *item)
1411a07b4970SChristoph Hellwig {
1412b662a078SJay Sternberg 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1413a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1414a07b4970SChristoph Hellwig 
1415b662a078SJay Sternberg 	nvmet_referral_disable(parent, port);
1416f0e656e4SSagi Grimberg }
1417f0e656e4SSagi Grimberg 
1418f0e656e4SSagi Grimberg static void nvmet_referral_release(struct config_item *item)
1419f0e656e4SSagi Grimberg {
1420f0e656e4SSagi Grimberg 	struct nvmet_port *port = to_nvmet_port(item);
1421f0e656e4SSagi Grimberg 
1422a07b4970SChristoph Hellwig 	kfree(port);
1423a07b4970SChristoph Hellwig }
1424a07b4970SChristoph Hellwig 
1425a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_referral_item_ops = {
1426a07b4970SChristoph Hellwig 	.release	= nvmet_referral_release,
1427a07b4970SChristoph Hellwig };
1428a07b4970SChristoph Hellwig 
142966603a31SBhumika Goyal static const struct config_item_type nvmet_referral_type = {
1430a07b4970SChristoph Hellwig 	.ct_owner	= THIS_MODULE,
1431a07b4970SChristoph Hellwig 	.ct_attrs	= nvmet_referral_attrs,
1432a07b4970SChristoph Hellwig 	.ct_item_ops	= &nvmet_referral_item_ops,
1433a07b4970SChristoph Hellwig };
1434a07b4970SChristoph Hellwig 
1435a07b4970SChristoph Hellwig static struct config_group *nvmet_referral_make(
1436a07b4970SChristoph Hellwig 		struct config_group *group, const char *name)
1437a07b4970SChristoph Hellwig {
1438a07b4970SChristoph Hellwig 	struct nvmet_port *port;
1439a07b4970SChristoph Hellwig 
1440a07b4970SChristoph Hellwig 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1441a07b4970SChristoph Hellwig 	if (!port)
1442f98d9ca1SDan Carpenter 		return ERR_PTR(-ENOMEM);
1443a07b4970SChristoph Hellwig 
1444a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->entry);
1445a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1446a07b4970SChristoph Hellwig 
1447a07b4970SChristoph Hellwig 	return &port->group;
1448a07b4970SChristoph Hellwig }
1449a07b4970SChristoph Hellwig 
1450a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_referral_group_ops = {
1451a07b4970SChristoph Hellwig 	.make_group		= nvmet_referral_make,
1452f0e656e4SSagi Grimberg 	.disconnect_notify	= nvmet_referral_notify,
1453a07b4970SChristoph Hellwig };
1454a07b4970SChristoph Hellwig 
145566603a31SBhumika Goyal static const struct config_item_type nvmet_referrals_type = {
1456a07b4970SChristoph Hellwig 	.ct_owner	= THIS_MODULE,
1457a07b4970SChristoph Hellwig 	.ct_group_ops	= &nvmet_referral_group_ops,
1458a07b4970SChristoph Hellwig };
1459a07b4970SChristoph Hellwig 
146074255969SChristoph Hellwig static struct nvmet_type_name_map nvmet_ana_state[] = {
146162ac0d32SChristoph Hellwig 	{ NVME_ANA_OPTIMIZED,		"optimized" },
146262ac0d32SChristoph Hellwig 	{ NVME_ANA_NONOPTIMIZED,	"non-optimized" },
146362ac0d32SChristoph Hellwig 	{ NVME_ANA_INACCESSIBLE,	"inaccessible" },
146462ac0d32SChristoph Hellwig 	{ NVME_ANA_PERSISTENT_LOSS,	"persistent-loss" },
146562ac0d32SChristoph Hellwig 	{ NVME_ANA_CHANGE,		"change" },
146662ac0d32SChristoph Hellwig };
146762ac0d32SChristoph Hellwig 
146862ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
146962ac0d32SChristoph Hellwig 		char *page)
147062ac0d32SChristoph Hellwig {
147162ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
147262ac0d32SChristoph Hellwig 	enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
147362ac0d32SChristoph Hellwig 	int i;
147462ac0d32SChristoph Hellwig 
147584b8d0d7SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
147684b8d0d7SChaitanya Kulkarni 		if (state == nvmet_ana_state[i].type)
147784b8d0d7SChaitanya Kulkarni 			return sprintf(page, "%s\n", nvmet_ana_state[i].name);
147862ac0d32SChristoph Hellwig 	}
147962ac0d32SChristoph Hellwig 
148062ac0d32SChristoph Hellwig 	return sprintf(page, "\n");
148162ac0d32SChristoph Hellwig }
148262ac0d32SChristoph Hellwig 
148362ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
148462ac0d32SChristoph Hellwig 		const char *page, size_t count)
148562ac0d32SChristoph Hellwig {
148662ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
148784b8d0d7SChaitanya Kulkarni 	enum nvme_ana_state *ana_state = grp->port->ana_state;
148862ac0d32SChristoph Hellwig 	int i;
148962ac0d32SChristoph Hellwig 
149084b8d0d7SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
149184b8d0d7SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_ana_state[i].name))
149262ac0d32SChristoph Hellwig 			goto found;
149362ac0d32SChristoph Hellwig 	}
149462ac0d32SChristoph Hellwig 
149562ac0d32SChristoph Hellwig 	pr_err("Invalid value '%s' for ana_state\n", page);
149662ac0d32SChristoph Hellwig 	return -EINVAL;
149762ac0d32SChristoph Hellwig 
149862ac0d32SChristoph Hellwig found:
149962ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
150084b8d0d7SChaitanya Kulkarni 	ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
150162ac0d32SChristoph Hellwig 	nvmet_ana_chgcnt++;
150262ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
150362ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
150462ac0d32SChristoph Hellwig 	return count;
150562ac0d32SChristoph Hellwig }
150662ac0d32SChristoph Hellwig 
150762ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
150862ac0d32SChristoph Hellwig 
150962ac0d32SChristoph Hellwig static struct configfs_attribute *nvmet_ana_group_attrs[] = {
151062ac0d32SChristoph Hellwig 	&nvmet_ana_group_attr_ana_state,
151162ac0d32SChristoph Hellwig 	NULL,
151262ac0d32SChristoph Hellwig };
151362ac0d32SChristoph Hellwig 
151462ac0d32SChristoph Hellwig static void nvmet_ana_group_release(struct config_item *item)
151562ac0d32SChristoph Hellwig {
151662ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
151762ac0d32SChristoph Hellwig 
151862ac0d32SChristoph Hellwig 	if (grp == &grp->port->ana_default_group)
151962ac0d32SChristoph Hellwig 		return;
152062ac0d32SChristoph Hellwig 
152162ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
152262ac0d32SChristoph Hellwig 	grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
152362ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[grp->grpid]--;
152462ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
152562ac0d32SChristoph Hellwig 
152662ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
152762ac0d32SChristoph Hellwig 	kfree(grp);
152862ac0d32SChristoph Hellwig }
152962ac0d32SChristoph Hellwig 
153062ac0d32SChristoph Hellwig static struct configfs_item_operations nvmet_ana_group_item_ops = {
153162ac0d32SChristoph Hellwig 	.release		= nvmet_ana_group_release,
153262ac0d32SChristoph Hellwig };
153362ac0d32SChristoph Hellwig 
153462ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_group_type = {
153562ac0d32SChristoph Hellwig 	.ct_item_ops		= &nvmet_ana_group_item_ops,
153662ac0d32SChristoph Hellwig 	.ct_attrs		= nvmet_ana_group_attrs,
153762ac0d32SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
153862ac0d32SChristoph Hellwig };
153962ac0d32SChristoph Hellwig 
154062ac0d32SChristoph Hellwig static struct config_group *nvmet_ana_groups_make_group(
154162ac0d32SChristoph Hellwig 		struct config_group *group, const char *name)
154262ac0d32SChristoph Hellwig {
154362ac0d32SChristoph Hellwig 	struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
154462ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp;
154562ac0d32SChristoph Hellwig 	u32 grpid;
154662ac0d32SChristoph Hellwig 	int ret;
154762ac0d32SChristoph Hellwig 
154862ac0d32SChristoph Hellwig 	ret = kstrtou32(name, 0, &grpid);
154962ac0d32SChristoph Hellwig 	if (ret)
155062ac0d32SChristoph Hellwig 		goto out;
155162ac0d32SChristoph Hellwig 
155262ac0d32SChristoph Hellwig 	ret = -EINVAL;
155362ac0d32SChristoph Hellwig 	if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
155462ac0d32SChristoph Hellwig 		goto out;
155562ac0d32SChristoph Hellwig 
155662ac0d32SChristoph Hellwig 	ret = -ENOMEM;
155762ac0d32SChristoph Hellwig 	grp = kzalloc(sizeof(*grp), GFP_KERNEL);
155862ac0d32SChristoph Hellwig 	if (!grp)
155962ac0d32SChristoph Hellwig 		goto out;
156062ac0d32SChristoph Hellwig 	grp->port = port;
156162ac0d32SChristoph Hellwig 	grp->grpid = grpid;
156262ac0d32SChristoph Hellwig 
156362ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
156462ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[grpid]++;
156562ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
156662ac0d32SChristoph Hellwig 
156762ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
156862ac0d32SChristoph Hellwig 
156962ac0d32SChristoph Hellwig 	config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
157062ac0d32SChristoph Hellwig 	return &grp->group;
157162ac0d32SChristoph Hellwig out:
157262ac0d32SChristoph Hellwig 	return ERR_PTR(ret);
157362ac0d32SChristoph Hellwig }
157462ac0d32SChristoph Hellwig 
157562ac0d32SChristoph Hellwig static struct configfs_group_operations nvmet_ana_groups_group_ops = {
157662ac0d32SChristoph Hellwig 	.make_group		= nvmet_ana_groups_make_group,
157762ac0d32SChristoph Hellwig };
157862ac0d32SChristoph Hellwig 
157962ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_groups_type = {
158062ac0d32SChristoph Hellwig 	.ct_group_ops		= &nvmet_ana_groups_group_ops,
158162ac0d32SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
158262ac0d32SChristoph Hellwig };
158362ac0d32SChristoph Hellwig 
1584a07b4970SChristoph Hellwig /*
1585a07b4970SChristoph Hellwig  * Ports definitions.
1586a07b4970SChristoph Hellwig  */
1587a07b4970SChristoph Hellwig static void nvmet_port_release(struct config_item *item)
1588a07b4970SChristoph Hellwig {
1589a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1590a07b4970SChristoph Hellwig 
1591e3e19dccSIsrael Rukshin 	/* Let inflight controllers teardown complete */
1592e3e19dccSIsrael Rukshin 	flush_scheduled_work();
1593b662a078SJay Sternberg 	list_del(&port->global_entry);
1594b662a078SJay Sternberg 
159572efd25dSChristoph Hellwig 	kfree(port->ana_state);
1596a07b4970SChristoph Hellwig 	kfree(port);
1597a07b4970SChristoph Hellwig }
1598a07b4970SChristoph Hellwig 
1599a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_port_attrs[] = {
1600a07b4970SChristoph Hellwig 	&nvmet_attr_addr_adrfam,
1601a07b4970SChristoph Hellwig 	&nvmet_attr_addr_treq,
1602a07b4970SChristoph Hellwig 	&nvmet_attr_addr_traddr,
1603a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trsvcid,
1604a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trtype,
16050d5ee2b2SSteve Wise 	&nvmet_attr_param_inline_data_size,
1606ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1607ea52ac1cSIsrael Rukshin 	&nvmet_attr_param_pi_enable,
1608ea52ac1cSIsrael Rukshin #endif
1609a07b4970SChristoph Hellwig 	NULL,
1610a07b4970SChristoph Hellwig };
1611a07b4970SChristoph Hellwig 
1612a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_item_ops = {
1613a07b4970SChristoph Hellwig 	.release		= nvmet_port_release,
1614a07b4970SChristoph Hellwig };
1615a07b4970SChristoph Hellwig 
161666603a31SBhumika Goyal static const struct config_item_type nvmet_port_type = {
1617a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_port_attrs,
1618a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_port_item_ops,
1619a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1620a07b4970SChristoph Hellwig };
1621a07b4970SChristoph Hellwig 
1622a07b4970SChristoph Hellwig static struct config_group *nvmet_ports_make(struct config_group *group,
1623a07b4970SChristoph Hellwig 		const char *name)
1624a07b4970SChristoph Hellwig {
1625a07b4970SChristoph Hellwig 	struct nvmet_port *port;
1626a07b4970SChristoph Hellwig 	u16 portid;
162762ac0d32SChristoph Hellwig 	u32 i;
1628a07b4970SChristoph Hellwig 
1629a07b4970SChristoph Hellwig 	if (kstrtou16(name, 0, &portid))
1630a07b4970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1631a07b4970SChristoph Hellwig 
1632a07b4970SChristoph Hellwig 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1633a07b4970SChristoph Hellwig 	if (!port)
1634f98d9ca1SDan Carpenter 		return ERR_PTR(-ENOMEM);
1635a07b4970SChristoph Hellwig 
163672efd25dSChristoph Hellwig 	port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
163772efd25dSChristoph Hellwig 			sizeof(*port->ana_state), GFP_KERNEL);
163872efd25dSChristoph Hellwig 	if (!port->ana_state) {
163972efd25dSChristoph Hellwig 		kfree(port);
164072efd25dSChristoph Hellwig 		return ERR_PTR(-ENOMEM);
164172efd25dSChristoph Hellwig 	}
164272efd25dSChristoph Hellwig 
164362ac0d32SChristoph Hellwig 	for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
164462ac0d32SChristoph Hellwig 		if (i == NVMET_DEFAULT_ANA_GRPID)
164562ac0d32SChristoph Hellwig 			port->ana_state[1] = NVME_ANA_OPTIMIZED;
164662ac0d32SChristoph Hellwig 		else
164762ac0d32SChristoph Hellwig 			port->ana_state[i] = NVME_ANA_INACCESSIBLE;
164862ac0d32SChristoph Hellwig 	}
164972efd25dSChristoph Hellwig 
1650b662a078SJay Sternberg 	list_add(&port->global_entry, &nvmet_ports_list);
1651b662a078SJay Sternberg 
1652a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->entry);
1653a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->subsystems);
1654a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->referrals);
16550d5ee2b2SSteve Wise 	port->inline_data_size = -1;	/* < 0 == let the transport choose */
1656a07b4970SChristoph Hellwig 
1657a07b4970SChristoph Hellwig 	port->disc_addr.portid = cpu_to_le16(portid);
1658d02abd19SChaitanya Kulkarni 	port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
16599b95d2fbSSagi Grimberg 	port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1660a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->group, name, &nvmet_port_type);
1661a07b4970SChristoph Hellwig 
1662a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->subsys_group,
1663a07b4970SChristoph Hellwig 			"subsystems", &nvmet_port_subsys_type);
1664a07b4970SChristoph Hellwig 	configfs_add_default_group(&port->subsys_group, &port->group);
1665a07b4970SChristoph Hellwig 
1666a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->referrals_group,
1667a07b4970SChristoph Hellwig 			"referrals", &nvmet_referrals_type);
1668a07b4970SChristoph Hellwig 	configfs_add_default_group(&port->referrals_group, &port->group);
1669a07b4970SChristoph Hellwig 
167062ac0d32SChristoph Hellwig 	config_group_init_type_name(&port->ana_groups_group,
167162ac0d32SChristoph Hellwig 			"ana_groups", &nvmet_ana_groups_type);
167262ac0d32SChristoph Hellwig 	configfs_add_default_group(&port->ana_groups_group, &port->group);
167362ac0d32SChristoph Hellwig 
167462ac0d32SChristoph Hellwig 	port->ana_default_group.port = port;
167562ac0d32SChristoph Hellwig 	port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
167662ac0d32SChristoph Hellwig 	config_group_init_type_name(&port->ana_default_group.group,
167762ac0d32SChristoph Hellwig 			__stringify(NVMET_DEFAULT_ANA_GRPID),
167862ac0d32SChristoph Hellwig 			&nvmet_ana_group_type);
167962ac0d32SChristoph Hellwig 	configfs_add_default_group(&port->ana_default_group.group,
168062ac0d32SChristoph Hellwig 			&port->ana_groups_group);
168162ac0d32SChristoph Hellwig 
1682a07b4970SChristoph Hellwig 	return &port->group;
1683a07b4970SChristoph Hellwig }
1684a07b4970SChristoph Hellwig 
1685a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_ports_group_ops = {
1686a07b4970SChristoph Hellwig 	.make_group		= nvmet_ports_make,
1687a07b4970SChristoph Hellwig };
1688a07b4970SChristoph Hellwig 
168966603a31SBhumika Goyal static const struct config_item_type nvmet_ports_type = {
1690a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_ports_group_ops,
1691a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1692a07b4970SChristoph Hellwig };
1693a07b4970SChristoph Hellwig 
1694a07b4970SChristoph Hellwig static struct config_group nvmet_subsystems_group;
1695a07b4970SChristoph Hellwig static struct config_group nvmet_ports_group;
1696a07b4970SChristoph Hellwig 
1697a07b4970SChristoph Hellwig static void nvmet_host_release(struct config_item *item)
1698a07b4970SChristoph Hellwig {
1699a07b4970SChristoph Hellwig 	struct nvmet_host *host = to_host(item);
1700a07b4970SChristoph Hellwig 
1701a07b4970SChristoph Hellwig 	kfree(host);
1702a07b4970SChristoph Hellwig }
1703a07b4970SChristoph Hellwig 
1704a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_host_item_ops = {
1705a07b4970SChristoph Hellwig 	.release		= nvmet_host_release,
1706a07b4970SChristoph Hellwig };
1707a07b4970SChristoph Hellwig 
170866603a31SBhumika Goyal static const struct config_item_type nvmet_host_type = {
1709a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_host_item_ops,
1710a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1711a07b4970SChristoph Hellwig };
1712a07b4970SChristoph Hellwig 
1713a07b4970SChristoph Hellwig static struct config_group *nvmet_hosts_make_group(struct config_group *group,
1714a07b4970SChristoph Hellwig 		const char *name)
1715a07b4970SChristoph Hellwig {
1716a07b4970SChristoph Hellwig 	struct nvmet_host *host;
1717a07b4970SChristoph Hellwig 
1718a07b4970SChristoph Hellwig 	host = kzalloc(sizeof(*host), GFP_KERNEL);
1719a07b4970SChristoph Hellwig 	if (!host)
1720a07b4970SChristoph Hellwig 		return ERR_PTR(-ENOMEM);
1721a07b4970SChristoph Hellwig 
1722a07b4970SChristoph Hellwig 	config_group_init_type_name(&host->group, name, &nvmet_host_type);
1723a07b4970SChristoph Hellwig 
1724a07b4970SChristoph Hellwig 	return &host->group;
1725a07b4970SChristoph Hellwig }
1726a07b4970SChristoph Hellwig 
1727a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_hosts_group_ops = {
1728a07b4970SChristoph Hellwig 	.make_group		= nvmet_hosts_make_group,
1729a07b4970SChristoph Hellwig };
1730a07b4970SChristoph Hellwig 
173166603a31SBhumika Goyal static const struct config_item_type nvmet_hosts_type = {
1732a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_hosts_group_ops,
1733a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1734a07b4970SChristoph Hellwig };
1735a07b4970SChristoph Hellwig 
1736a07b4970SChristoph Hellwig static struct config_group nvmet_hosts_group;
1737a07b4970SChristoph Hellwig 
173866603a31SBhumika Goyal static const struct config_item_type nvmet_root_type = {
1739a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1740a07b4970SChristoph Hellwig };
1741a07b4970SChristoph Hellwig 
1742a07b4970SChristoph Hellwig static struct configfs_subsystem nvmet_configfs_subsystem = {
1743a07b4970SChristoph Hellwig 	.su_group = {
1744a07b4970SChristoph Hellwig 		.cg_item = {
1745a07b4970SChristoph Hellwig 			.ci_namebuf	= "nvmet",
1746a07b4970SChristoph Hellwig 			.ci_type	= &nvmet_root_type,
1747a07b4970SChristoph Hellwig 		},
1748a07b4970SChristoph Hellwig 	},
1749a07b4970SChristoph Hellwig };
1750a07b4970SChristoph Hellwig 
1751a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void)
1752a07b4970SChristoph Hellwig {
1753a07b4970SChristoph Hellwig 	int ret;
1754a07b4970SChristoph Hellwig 
1755a07b4970SChristoph Hellwig 	config_group_init(&nvmet_configfs_subsystem.su_group);
1756a07b4970SChristoph Hellwig 	mutex_init(&nvmet_configfs_subsystem.su_mutex);
1757a07b4970SChristoph Hellwig 
1758a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_subsystems_group,
1759a07b4970SChristoph Hellwig 			"subsystems", &nvmet_subsystems_type);
1760a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_subsystems_group,
1761a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1762a07b4970SChristoph Hellwig 
1763a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_ports_group,
1764a07b4970SChristoph Hellwig 			"ports", &nvmet_ports_type);
1765a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_ports_group,
1766a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1767a07b4970SChristoph Hellwig 
1768a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_hosts_group,
1769a07b4970SChristoph Hellwig 			"hosts", &nvmet_hosts_type);
1770a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_hosts_group,
1771a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1772a07b4970SChristoph Hellwig 
1773a07b4970SChristoph Hellwig 	ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
1774a07b4970SChristoph Hellwig 	if (ret) {
1775a07b4970SChristoph Hellwig 		pr_err("configfs_register_subsystem: %d\n", ret);
1776a07b4970SChristoph Hellwig 		return ret;
1777a07b4970SChristoph Hellwig 	}
1778a07b4970SChristoph Hellwig 
1779a07b4970SChristoph Hellwig 	return 0;
1780a07b4970SChristoph Hellwig }
1781a07b4970SChristoph Hellwig 
1782a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void)
1783a07b4970SChristoph Hellwig {
1784a07b4970SChristoph Hellwig 	configfs_unregister_subsystem(&nvmet_configfs_subsystem);
1785a07b4970SChristoph Hellwig }
1786