xref: /openbmc/linux/drivers/nvme/target/configfs.c (revision cae5b01a)
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 {
96a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
97a07b4970SChristoph Hellwig 
98a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n",
99a07b4970SChristoph Hellwig 			le16_to_cpu(port->disc_addr.portid));
100a07b4970SChristoph Hellwig }
101a07b4970SChristoph Hellwig 
102a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_store(struct config_item *item,
103a07b4970SChristoph Hellwig 		const char *page, size_t count)
104a07b4970SChristoph Hellwig {
105a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
106a07b4970SChristoph Hellwig 	u16 portid = 0;
107a07b4970SChristoph Hellwig 
108a07b4970SChristoph Hellwig 	if (kstrtou16(page, 0, &portid)) {
109a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for portid\n", page);
110a07b4970SChristoph Hellwig 		return -EINVAL;
111a07b4970SChristoph Hellwig 	}
112a07b4970SChristoph Hellwig 
1133ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
114a07b4970SChristoph Hellwig 		return -EACCES;
1153ecb5faaSChaitanya Kulkarni 
116a07b4970SChristoph Hellwig 	port->disc_addr.portid = cpu_to_le16(portid);
117a07b4970SChristoph Hellwig 	return count;
118a07b4970SChristoph Hellwig }
119a07b4970SChristoph Hellwig 
120a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_portid);
121a07b4970SChristoph Hellwig 
122a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_show(struct config_item *item,
123a07b4970SChristoph Hellwig 		char *page)
124a07b4970SChristoph Hellwig {
125a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
126a07b4970SChristoph Hellwig 
127a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n",
128a07b4970SChristoph Hellwig 			port->disc_addr.traddr);
129a07b4970SChristoph Hellwig }
130a07b4970SChristoph Hellwig 
131a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_store(struct config_item *item,
132a07b4970SChristoph Hellwig 		const char *page, size_t count)
133a07b4970SChristoph Hellwig {
134a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
135a07b4970SChristoph Hellwig 
136a07b4970SChristoph Hellwig 	if (count > NVMF_TRADDR_SIZE) {
137a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for traddr\n", page);
138a07b4970SChristoph Hellwig 		return -EINVAL;
139a07b4970SChristoph Hellwig 	}
140a07b4970SChristoph Hellwig 
1413ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
142a07b4970SChristoph Hellwig 		return -EACCES;
1439ba2a5cbSSagi Grimberg 
1449ba2a5cbSSagi Grimberg 	if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
1459ba2a5cbSSagi Grimberg 		return -EINVAL;
1469ba2a5cbSSagi Grimberg 	return count;
147a07b4970SChristoph Hellwig }
148a07b4970SChristoph Hellwig 
149a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_traddr);
150a07b4970SChristoph Hellwig 
15187628e28SChaitanya Kulkarni static const struct nvmet_type_name_map nvmet_addr_treq[] = {
15287628e28SChaitanya Kulkarni 	{ NVMF_TREQ_NOT_SPECIFIED,	"not specified" },
15387628e28SChaitanya Kulkarni 	{ NVMF_TREQ_REQUIRED,		"required" },
15487628e28SChaitanya Kulkarni 	{ NVMF_TREQ_NOT_REQUIRED,	"not required" },
15587628e28SChaitanya Kulkarni };
15687628e28SChaitanya Kulkarni 
15787628e28SChaitanya Kulkarni static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page)
158a07b4970SChristoph Hellwig {
15987628e28SChaitanya Kulkarni 	u8 treq = to_nvmet_port(item)->disc_addr.treq &
16087628e28SChaitanya Kulkarni 		NVME_TREQ_SECURE_CHANNEL_MASK;
16187628e28SChaitanya Kulkarni 	int i;
16287628e28SChaitanya Kulkarni 
16387628e28SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
16487628e28SChaitanya Kulkarni 		if (treq == nvmet_addr_treq[i].type)
16587628e28SChaitanya Kulkarni 			return sprintf(page, "%s\n", nvmet_addr_treq[i].name);
166a07b4970SChristoph Hellwig 	}
16787628e28SChaitanya Kulkarni 
16887628e28SChaitanya Kulkarni 	return sprintf(page, "\n");
169a07b4970SChristoph Hellwig }
170a07b4970SChristoph Hellwig 
171a07b4970SChristoph Hellwig static ssize_t nvmet_addr_treq_store(struct config_item *item,
172a07b4970SChristoph Hellwig 		const char *page, size_t count)
173a07b4970SChristoph Hellwig {
174a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1750445e1b5SSagi Grimberg 	u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK;
17687628e28SChaitanya Kulkarni 	int i;
177a07b4970SChristoph Hellwig 
1783ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
179a07b4970SChristoph Hellwig 		return -EACCES;
180a07b4970SChristoph Hellwig 
18187628e28SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
18287628e28SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_addr_treq[i].name))
18387628e28SChaitanya Kulkarni 			goto found;
18487628e28SChaitanya Kulkarni 	}
18587628e28SChaitanya Kulkarni 
186a07b4970SChristoph Hellwig 	pr_err("Invalid value '%s' for treq\n", page);
187a07b4970SChristoph Hellwig 	return -EINVAL;
188a07b4970SChristoph Hellwig 
18987628e28SChaitanya Kulkarni found:
19087628e28SChaitanya Kulkarni 	treq |= nvmet_addr_treq[i].type;
19187628e28SChaitanya Kulkarni 	port->disc_addr.treq = treq;
192a07b4970SChristoph Hellwig 	return count;
193a07b4970SChristoph Hellwig }
194a07b4970SChristoph Hellwig 
195a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_treq);
196a07b4970SChristoph Hellwig 
197a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
198a07b4970SChristoph Hellwig 		char *page)
199a07b4970SChristoph Hellwig {
200a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
201a07b4970SChristoph Hellwig 
202a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n",
203a07b4970SChristoph Hellwig 			port->disc_addr.trsvcid);
204a07b4970SChristoph Hellwig }
205a07b4970SChristoph Hellwig 
206a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
207a07b4970SChristoph Hellwig 		const char *page, size_t count)
208a07b4970SChristoph Hellwig {
209a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
210a07b4970SChristoph Hellwig 
211a07b4970SChristoph Hellwig 	if (count > NVMF_TRSVCID_SIZE) {
212a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for trsvcid\n", page);
213a07b4970SChristoph Hellwig 		return -EINVAL;
214a07b4970SChristoph Hellwig 	}
2153ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
216a07b4970SChristoph Hellwig 		return -EACCES;
2179ba2a5cbSSagi Grimberg 
2189ba2a5cbSSagi Grimberg 	if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
2199ba2a5cbSSagi Grimberg 		return -EINVAL;
2209ba2a5cbSSagi Grimberg 	return count;
221a07b4970SChristoph Hellwig }
222a07b4970SChristoph Hellwig 
223a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trsvcid);
224a07b4970SChristoph Hellwig 
2250d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
2260d5ee2b2SSteve Wise 		char *page)
2270d5ee2b2SSteve Wise {
2280d5ee2b2SSteve Wise 	struct nvmet_port *port = to_nvmet_port(item);
2290d5ee2b2SSteve Wise 
2300d5ee2b2SSteve Wise 	return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
2310d5ee2b2SSteve Wise }
2320d5ee2b2SSteve Wise 
2330d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
2340d5ee2b2SSteve Wise 		const char *page, size_t count)
2350d5ee2b2SSteve Wise {
2360d5ee2b2SSteve Wise 	struct nvmet_port *port = to_nvmet_port(item);
2370d5ee2b2SSteve Wise 	int ret;
2380d5ee2b2SSteve Wise 
2393ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
2400d5ee2b2SSteve Wise 		return -EACCES;
2410d5ee2b2SSteve Wise 	ret = kstrtoint(page, 0, &port->inline_data_size);
2420d5ee2b2SSteve Wise 	if (ret) {
2430d5ee2b2SSteve Wise 		pr_err("Invalid value '%s' for inline_data_size\n", page);
2440d5ee2b2SSteve Wise 		return -EINVAL;
2450d5ee2b2SSteve Wise 	}
2460d5ee2b2SSteve Wise 	return count;
2470d5ee2b2SSteve Wise }
2480d5ee2b2SSteve Wise 
2490d5ee2b2SSteve Wise CONFIGFS_ATTR(nvmet_, param_inline_data_size);
2500d5ee2b2SSteve Wise 
251ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
252ea52ac1cSIsrael Rukshin static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
253ea52ac1cSIsrael Rukshin 		char *page)
254ea52ac1cSIsrael Rukshin {
255ea52ac1cSIsrael Rukshin 	struct nvmet_port *port = to_nvmet_port(item);
256ea52ac1cSIsrael Rukshin 
257ea52ac1cSIsrael Rukshin 	return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
258ea52ac1cSIsrael Rukshin }
259ea52ac1cSIsrael Rukshin 
260ea52ac1cSIsrael Rukshin static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
261ea52ac1cSIsrael Rukshin 		const char *page, size_t count)
262ea52ac1cSIsrael Rukshin {
263ea52ac1cSIsrael Rukshin 	struct nvmet_port *port = to_nvmet_port(item);
264ea52ac1cSIsrael Rukshin 	bool val;
265ea52ac1cSIsrael Rukshin 
266ea52ac1cSIsrael Rukshin 	if (strtobool(page, &val))
267ea52ac1cSIsrael Rukshin 		return -EINVAL;
268ea52ac1cSIsrael Rukshin 
269ea52ac1cSIsrael Rukshin 	if (port->enabled) {
270ea52ac1cSIsrael Rukshin 		pr_err("Disable port before setting pi_enable value.\n");
271ea52ac1cSIsrael Rukshin 		return -EACCES;
272ea52ac1cSIsrael Rukshin 	}
273ea52ac1cSIsrael Rukshin 
274ea52ac1cSIsrael Rukshin 	port->pi_enable = val;
275ea52ac1cSIsrael Rukshin 	return count;
276ea52ac1cSIsrael Rukshin }
277ea52ac1cSIsrael Rukshin 
278ea52ac1cSIsrael Rukshin CONFIGFS_ATTR(nvmet_, param_pi_enable);
279ea52ac1cSIsrael Rukshin #endif
280ea52ac1cSIsrael Rukshin 
281a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_show(struct config_item *item,
282a07b4970SChristoph Hellwig 		char *page)
283a07b4970SChristoph Hellwig {
284a5d18612SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
285a5d18612SChristoph Hellwig 	int i;
286a5d18612SChristoph Hellwig 
28745e2f3c2SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
28845e2f3c2SChaitanya Kulkarni 		if (port->disc_addr.trtype == nvmet_transport[i].type)
28945e2f3c2SChaitanya Kulkarni 			return sprintf(page, "%s\n", nvmet_transport[i].name);
290a07b4970SChristoph Hellwig 	}
291a5d18612SChristoph Hellwig 
292a5d18612SChristoph Hellwig 	return sprintf(page, "\n");
293a07b4970SChristoph Hellwig }
294a07b4970SChristoph Hellwig 
295a07b4970SChristoph Hellwig static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
296a07b4970SChristoph Hellwig {
297a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
298a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
299a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
300a07b4970SChristoph Hellwig }
301a07b4970SChristoph Hellwig 
302a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_store(struct config_item *item,
303a07b4970SChristoph Hellwig 		const char *page, size_t count)
304a07b4970SChristoph Hellwig {
305a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
306a5d18612SChristoph Hellwig 	int i;
307a07b4970SChristoph Hellwig 
3083ecb5faaSChaitanya Kulkarni 	if (nvmet_is_port_enabled(port, __func__))
309a07b4970SChristoph Hellwig 		return -EACCES;
310a07b4970SChristoph Hellwig 
31145e2f3c2SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
31245e2f3c2SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_transport[i].name))
313a5d18612SChristoph Hellwig 			goto found;
314a07b4970SChristoph Hellwig 	}
315a07b4970SChristoph Hellwig 
316a5d18612SChristoph Hellwig 	pr_err("Invalid value '%s' for trtype\n", page);
317a5d18612SChristoph Hellwig 	return -EINVAL;
3187e764179SChaitanya Kulkarni 
319a5d18612SChristoph Hellwig found:
320a5d18612SChristoph Hellwig 	memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
32145e2f3c2SChaitanya Kulkarni 	port->disc_addr.trtype = nvmet_transport[i].type;
322a5d18612SChristoph Hellwig 	if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
323a5d18612SChristoph Hellwig 		nvmet_port_init_tsas_rdma(port);
324a07b4970SChristoph Hellwig 	return count;
325a07b4970SChristoph Hellwig }
326a07b4970SChristoph Hellwig 
327a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trtype);
328a07b4970SChristoph Hellwig 
329a07b4970SChristoph Hellwig /*
330a07b4970SChristoph Hellwig  * Namespace structures & file operation functions below
331a07b4970SChristoph Hellwig  */
332a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
333a07b4970SChristoph Hellwig {
334a07b4970SChristoph Hellwig 	return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
335a07b4970SChristoph Hellwig }
336a07b4970SChristoph Hellwig 
337a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_store(struct config_item *item,
338a07b4970SChristoph Hellwig 		const char *page, size_t count)
339a07b4970SChristoph Hellwig {
340a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
341a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = ns->subsys;
3425613d312SHannes Reinecke 	size_t len;
343a07b4970SChristoph Hellwig 	int ret;
344a07b4970SChristoph Hellwig 
345a07b4970SChristoph Hellwig 	mutex_lock(&subsys->lock);
346a07b4970SChristoph Hellwig 	ret = -EBUSY;
347e4fcf07cSSolganik Alexander 	if (ns->enabled)
348a07b4970SChristoph Hellwig 		goto out_unlock;
349a07b4970SChristoph Hellwig 
3505613d312SHannes Reinecke 	ret = -EINVAL;
3515613d312SHannes Reinecke 	len = strcspn(page, "\n");
3525613d312SHannes Reinecke 	if (!len)
3535613d312SHannes Reinecke 		goto out_unlock;
354a07b4970SChristoph Hellwig 
3555613d312SHannes Reinecke 	kfree(ns->device_path);
356a07b4970SChristoph Hellwig 	ret = -ENOMEM;
35709bb8986SChen Zhou 	ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
358a07b4970SChristoph Hellwig 	if (!ns->device_path)
359a07b4970SChristoph Hellwig 		goto out_unlock;
360a07b4970SChristoph Hellwig 
361a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
362a07b4970SChristoph Hellwig 	return count;
363a07b4970SChristoph Hellwig 
364a07b4970SChristoph Hellwig out_unlock:
365a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
366a07b4970SChristoph Hellwig 	return ret;
367a07b4970SChristoph Hellwig }
368a07b4970SChristoph Hellwig 
369a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_path);
370a07b4970SChristoph Hellwig 
371c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA
372c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
373c6925093SLogan Gunthorpe {
374c6925093SLogan Gunthorpe 	struct nvmet_ns *ns = to_nvmet_ns(item);
375c6925093SLogan Gunthorpe 
376c6925093SLogan Gunthorpe 	return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
377c6925093SLogan Gunthorpe }
378c6925093SLogan Gunthorpe 
379c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
380c6925093SLogan Gunthorpe 		const char *page, size_t count)
381c6925093SLogan Gunthorpe {
382c6925093SLogan Gunthorpe 	struct nvmet_ns *ns = to_nvmet_ns(item);
383c6925093SLogan Gunthorpe 	struct pci_dev *p2p_dev = NULL;
384c6925093SLogan Gunthorpe 	bool use_p2pmem;
385c6925093SLogan Gunthorpe 	int ret = count;
386c6925093SLogan Gunthorpe 	int error;
387c6925093SLogan Gunthorpe 
388c6925093SLogan Gunthorpe 	mutex_lock(&ns->subsys->lock);
389c6925093SLogan Gunthorpe 	if (ns->enabled) {
390c6925093SLogan Gunthorpe 		ret = -EBUSY;
391c6925093SLogan Gunthorpe 		goto out_unlock;
392c6925093SLogan Gunthorpe 	}
393c6925093SLogan Gunthorpe 
394c6925093SLogan Gunthorpe 	error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
395c6925093SLogan Gunthorpe 	if (error) {
396c6925093SLogan Gunthorpe 		ret = error;
397c6925093SLogan Gunthorpe 		goto out_unlock;
398c6925093SLogan Gunthorpe 	}
399c6925093SLogan Gunthorpe 
400c6925093SLogan Gunthorpe 	ns->use_p2pmem = use_p2pmem;
401c6925093SLogan Gunthorpe 	pci_dev_put(ns->p2p_dev);
402c6925093SLogan Gunthorpe 	ns->p2p_dev = p2p_dev;
403c6925093SLogan Gunthorpe 
404c6925093SLogan Gunthorpe out_unlock:
405c6925093SLogan Gunthorpe 	mutex_unlock(&ns->subsys->lock);
406c6925093SLogan Gunthorpe 
407c6925093SLogan Gunthorpe 	return ret;
408c6925093SLogan Gunthorpe }
409c6925093SLogan Gunthorpe 
410c6925093SLogan Gunthorpe CONFIGFS_ATTR(nvmet_ns_, p2pmem);
411c6925093SLogan Gunthorpe #endif /* CONFIG_PCI_P2PDMA */
412c6925093SLogan Gunthorpe 
413430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
414430c7befSJohannes Thumshirn {
415430c7befSJohannes Thumshirn 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
416430c7befSJohannes Thumshirn }
417430c7befSJohannes Thumshirn 
418430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
419430c7befSJohannes Thumshirn 					  const char *page, size_t count)
420430c7befSJohannes Thumshirn {
421430c7befSJohannes Thumshirn 	struct nvmet_ns *ns = to_nvmet_ns(item);
422430c7befSJohannes Thumshirn 	struct nvmet_subsys *subsys = ns->subsys;
423430c7befSJohannes Thumshirn 	int ret = 0;
424430c7befSJohannes Thumshirn 
425430c7befSJohannes Thumshirn 	mutex_lock(&subsys->lock);
426430c7befSJohannes Thumshirn 	if (ns->enabled) {
427430c7befSJohannes Thumshirn 		ret = -EBUSY;
428430c7befSJohannes Thumshirn 		goto out_unlock;
429430c7befSJohannes Thumshirn 	}
430430c7befSJohannes Thumshirn 
431430c7befSJohannes Thumshirn 	if (uuid_parse(page, &ns->uuid))
432430c7befSJohannes Thumshirn 		ret = -EINVAL;
433430c7befSJohannes Thumshirn 
434430c7befSJohannes Thumshirn out_unlock:
435430c7befSJohannes Thumshirn 	mutex_unlock(&subsys->lock);
436430c7befSJohannes Thumshirn 	return ret ? ret : count;
437430c7befSJohannes Thumshirn }
438430c7befSJohannes Thumshirn 
439f871749aSMax Gurtovoy CONFIGFS_ATTR(nvmet_ns_, device_uuid);
440f871749aSMax Gurtovoy 
441a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
442a07b4970SChristoph Hellwig {
443a07b4970SChristoph Hellwig 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
444a07b4970SChristoph Hellwig }
445a07b4970SChristoph Hellwig 
446a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
447a07b4970SChristoph Hellwig 		const char *page, size_t count)
448a07b4970SChristoph Hellwig {
449a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
450a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = ns->subsys;
451a07b4970SChristoph Hellwig 	u8 nguid[16];
452a07b4970SChristoph Hellwig 	const char *p = page;
453a07b4970SChristoph Hellwig 	int i;
454a07b4970SChristoph Hellwig 	int ret = 0;
455a07b4970SChristoph Hellwig 
456a07b4970SChristoph Hellwig 	mutex_lock(&subsys->lock);
457e4fcf07cSSolganik Alexander 	if (ns->enabled) {
458a07b4970SChristoph Hellwig 		ret = -EBUSY;
459a07b4970SChristoph Hellwig 		goto out_unlock;
460a07b4970SChristoph Hellwig 	}
461a07b4970SChristoph Hellwig 
462a07b4970SChristoph Hellwig 	for (i = 0; i < 16; i++) {
463a07b4970SChristoph Hellwig 		if (p + 2 > page + count) {
464a07b4970SChristoph Hellwig 			ret = -EINVAL;
465a07b4970SChristoph Hellwig 			goto out_unlock;
466a07b4970SChristoph Hellwig 		}
467a07b4970SChristoph Hellwig 		if (!isxdigit(p[0]) || !isxdigit(p[1])) {
468a07b4970SChristoph Hellwig 			ret = -EINVAL;
469a07b4970SChristoph Hellwig 			goto out_unlock;
470a07b4970SChristoph Hellwig 		}
471a07b4970SChristoph Hellwig 
472a07b4970SChristoph Hellwig 		nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
473a07b4970SChristoph Hellwig 		p += 2;
474a07b4970SChristoph Hellwig 
475a07b4970SChristoph Hellwig 		if (*p == '-' || *p == ':')
476a07b4970SChristoph Hellwig 			p++;
477a07b4970SChristoph Hellwig 	}
478a07b4970SChristoph Hellwig 
479a07b4970SChristoph Hellwig 	memcpy(&ns->nguid, nguid, sizeof(nguid));
480a07b4970SChristoph Hellwig out_unlock:
481a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
482a07b4970SChristoph Hellwig 	return ret ? ret : count;
483a07b4970SChristoph Hellwig }
484a07b4970SChristoph Hellwig 
485a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_nguid);
486a07b4970SChristoph Hellwig 
48762ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
48862ac0d32SChristoph Hellwig {
48962ac0d32SChristoph Hellwig 	return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
49062ac0d32SChristoph Hellwig }
49162ac0d32SChristoph Hellwig 
49262ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
49362ac0d32SChristoph Hellwig 		const char *page, size_t count)
49462ac0d32SChristoph Hellwig {
49562ac0d32SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
49662ac0d32SChristoph Hellwig 	u32 oldgrpid, newgrpid;
49762ac0d32SChristoph Hellwig 	int ret;
49862ac0d32SChristoph Hellwig 
49962ac0d32SChristoph Hellwig 	ret = kstrtou32(page, 0, &newgrpid);
50062ac0d32SChristoph Hellwig 	if (ret)
50162ac0d32SChristoph Hellwig 		return ret;
50262ac0d32SChristoph Hellwig 
50362ac0d32SChristoph Hellwig 	if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
50462ac0d32SChristoph Hellwig 		return -EINVAL;
50562ac0d32SChristoph Hellwig 
50662ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
50762ac0d32SChristoph Hellwig 	oldgrpid = ns->anagrpid;
50862ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[newgrpid]++;
50962ac0d32SChristoph Hellwig 	ns->anagrpid = newgrpid;
51062ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[oldgrpid]--;
51162ac0d32SChristoph Hellwig 	nvmet_ana_chgcnt++;
51262ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
51362ac0d32SChristoph Hellwig 
51462ac0d32SChristoph Hellwig 	nvmet_send_ana_event(ns->subsys, NULL);
51562ac0d32SChristoph Hellwig 	return count;
51662ac0d32SChristoph Hellwig }
51762ac0d32SChristoph Hellwig 
51862ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
51962ac0d32SChristoph Hellwig 
520a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
521a07b4970SChristoph Hellwig {
522e4fcf07cSSolganik Alexander 	return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
523a07b4970SChristoph Hellwig }
524a07b4970SChristoph Hellwig 
525a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_store(struct config_item *item,
526a07b4970SChristoph Hellwig 		const char *page, size_t count)
527a07b4970SChristoph Hellwig {
528a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
529a07b4970SChristoph Hellwig 	bool enable;
530a07b4970SChristoph Hellwig 	int ret = 0;
531a07b4970SChristoph Hellwig 
532a07b4970SChristoph Hellwig 	if (strtobool(page, &enable))
533a07b4970SChristoph Hellwig 		return -EINVAL;
534a07b4970SChristoph Hellwig 
535a07b4970SChristoph Hellwig 	if (enable)
536a07b4970SChristoph Hellwig 		ret = nvmet_ns_enable(ns);
537a07b4970SChristoph Hellwig 	else
538a07b4970SChristoph Hellwig 		nvmet_ns_disable(ns);
539a07b4970SChristoph Hellwig 
540a07b4970SChristoph Hellwig 	return ret ? ret : count;
541a07b4970SChristoph Hellwig }
542a07b4970SChristoph Hellwig 
543a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, enable);
544a07b4970SChristoph Hellwig 
54555eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
54655eb942eSChaitanya Kulkarni {
54755eb942eSChaitanya Kulkarni 	return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
54855eb942eSChaitanya Kulkarni }
54955eb942eSChaitanya Kulkarni 
55055eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
55155eb942eSChaitanya Kulkarni 		const char *page, size_t count)
55255eb942eSChaitanya Kulkarni {
55355eb942eSChaitanya Kulkarni 	struct nvmet_ns *ns = to_nvmet_ns(item);
55455eb942eSChaitanya Kulkarni 	bool val;
55555eb942eSChaitanya Kulkarni 
55655eb942eSChaitanya Kulkarni 	if (strtobool(page, &val))
55755eb942eSChaitanya Kulkarni 		return -EINVAL;
55855eb942eSChaitanya Kulkarni 
55955eb942eSChaitanya Kulkarni 	mutex_lock(&ns->subsys->lock);
56055eb942eSChaitanya Kulkarni 	if (ns->enabled) {
56155eb942eSChaitanya Kulkarni 		pr_err("disable ns before setting buffered_io value.\n");
56255eb942eSChaitanya Kulkarni 		mutex_unlock(&ns->subsys->lock);
56355eb942eSChaitanya Kulkarni 		return -EINVAL;
56455eb942eSChaitanya Kulkarni 	}
56555eb942eSChaitanya Kulkarni 
56655eb942eSChaitanya Kulkarni 	ns->buffered_io = val;
56755eb942eSChaitanya Kulkarni 	mutex_unlock(&ns->subsys->lock);
56855eb942eSChaitanya Kulkarni 	return count;
56955eb942eSChaitanya Kulkarni }
57055eb942eSChaitanya Kulkarni 
57155eb942eSChaitanya Kulkarni CONFIGFS_ATTR(nvmet_ns_, buffered_io);
57255eb942eSChaitanya Kulkarni 
5731f357548SChaitanya Kulkarni static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
5741f357548SChaitanya Kulkarni 		const char *page, size_t count)
5751f357548SChaitanya Kulkarni {
5761f357548SChaitanya Kulkarni 	struct nvmet_ns *ns = to_nvmet_ns(item);
5771f357548SChaitanya Kulkarni 	bool val;
5781f357548SChaitanya Kulkarni 
5791f357548SChaitanya Kulkarni 	if (strtobool(page, &val))
5801f357548SChaitanya Kulkarni 		return -EINVAL;
5811f357548SChaitanya Kulkarni 
5821f357548SChaitanya Kulkarni 	if (!val)
5831f357548SChaitanya Kulkarni 		return -EINVAL;
5841f357548SChaitanya Kulkarni 
5851f357548SChaitanya Kulkarni 	mutex_lock(&ns->subsys->lock);
5861f357548SChaitanya Kulkarni 	if (!ns->enabled) {
5871f357548SChaitanya Kulkarni 		pr_err("enable ns before revalidate.\n");
5881f357548SChaitanya Kulkarni 		mutex_unlock(&ns->subsys->lock);
5891f357548SChaitanya Kulkarni 		return -EINVAL;
5901f357548SChaitanya Kulkarni 	}
5911f357548SChaitanya Kulkarni 	nvmet_ns_revalidate(ns);
5921f357548SChaitanya Kulkarni 	mutex_unlock(&ns->subsys->lock);
5931f357548SChaitanya Kulkarni 	return count;
5941f357548SChaitanya Kulkarni }
5951f357548SChaitanya Kulkarni 
5961f357548SChaitanya Kulkarni CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
5971f357548SChaitanya Kulkarni 
598a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_ns_attrs[] = {
599a07b4970SChristoph Hellwig 	&nvmet_ns_attr_device_path,
600a07b4970SChristoph Hellwig 	&nvmet_ns_attr_device_nguid,
601430c7befSJohannes Thumshirn 	&nvmet_ns_attr_device_uuid,
60262ac0d32SChristoph Hellwig 	&nvmet_ns_attr_ana_grpid,
603a07b4970SChristoph Hellwig 	&nvmet_ns_attr_enable,
60455eb942eSChaitanya Kulkarni 	&nvmet_ns_attr_buffered_io,
6051f357548SChaitanya Kulkarni 	&nvmet_ns_attr_revalidate_size,
606c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA
607c6925093SLogan Gunthorpe 	&nvmet_ns_attr_p2pmem,
608c6925093SLogan Gunthorpe #endif
609a07b4970SChristoph Hellwig 	NULL,
610a07b4970SChristoph Hellwig };
611a07b4970SChristoph Hellwig 
612a07b4970SChristoph Hellwig static void nvmet_ns_release(struct config_item *item)
613a07b4970SChristoph Hellwig {
614a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
615a07b4970SChristoph Hellwig 
616a07b4970SChristoph Hellwig 	nvmet_ns_free(ns);
617a07b4970SChristoph Hellwig }
618a07b4970SChristoph Hellwig 
619a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_ns_item_ops = {
620a07b4970SChristoph Hellwig 	.release		= nvmet_ns_release,
621a07b4970SChristoph Hellwig };
622a07b4970SChristoph Hellwig 
62366603a31SBhumika Goyal static const struct config_item_type nvmet_ns_type = {
624a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_ns_item_ops,
625a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_ns_attrs,
626a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
627a07b4970SChristoph Hellwig };
628a07b4970SChristoph Hellwig 
629a07b4970SChristoph Hellwig static struct config_group *nvmet_ns_make(struct config_group *group,
630a07b4970SChristoph Hellwig 		const char *name)
631a07b4970SChristoph Hellwig {
632a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
633a07b4970SChristoph Hellwig 	struct nvmet_ns *ns;
634a07b4970SChristoph Hellwig 	int ret;
635a07b4970SChristoph Hellwig 	u32 nsid;
636a07b4970SChristoph Hellwig 
637a07b4970SChristoph Hellwig 	ret = kstrtou32(name, 0, &nsid);
638a07b4970SChristoph Hellwig 	if (ret)
639a07b4970SChristoph Hellwig 		goto out;
640a07b4970SChristoph Hellwig 
641a07b4970SChristoph Hellwig 	ret = -EINVAL;
6425ba89503SMikhail Skorzhinskii 	if (nsid == 0 || nsid == NVME_NSID_ALL) {
6435ba89503SMikhail Skorzhinskii 		pr_err("invalid nsid %#x", nsid);
644a07b4970SChristoph Hellwig 		goto out;
6455ba89503SMikhail Skorzhinskii 	}
646a07b4970SChristoph Hellwig 
647a07b4970SChristoph Hellwig 	ret = -ENOMEM;
648a07b4970SChristoph Hellwig 	ns = nvmet_ns_alloc(subsys, nsid);
649a07b4970SChristoph Hellwig 	if (!ns)
650a07b4970SChristoph Hellwig 		goto out;
651a07b4970SChristoph Hellwig 	config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
652a07b4970SChristoph Hellwig 
653a07b4970SChristoph Hellwig 	pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
654a07b4970SChristoph Hellwig 
655a07b4970SChristoph Hellwig 	return &ns->group;
656a07b4970SChristoph Hellwig out:
657a07b4970SChristoph Hellwig 	return ERR_PTR(ret);
658a07b4970SChristoph Hellwig }
659a07b4970SChristoph Hellwig 
660a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_namespaces_group_ops = {
661a07b4970SChristoph Hellwig 	.make_group		= nvmet_ns_make,
662a07b4970SChristoph Hellwig };
663a07b4970SChristoph Hellwig 
66466603a31SBhumika Goyal static const struct config_item_type nvmet_namespaces_type = {
665a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_namespaces_group_ops,
666a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
667a07b4970SChristoph Hellwig };
668a07b4970SChristoph Hellwig 
669cae5b01aSLogan Gunthorpe #ifdef CONFIG_NVME_TARGET_PASSTHRU
670cae5b01aSLogan Gunthorpe 
671cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
672cae5b01aSLogan Gunthorpe 		char *page)
673cae5b01aSLogan Gunthorpe {
674cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
675cae5b01aSLogan Gunthorpe 
676cae5b01aSLogan Gunthorpe 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
677cae5b01aSLogan Gunthorpe }
678cae5b01aSLogan Gunthorpe 
679cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
680cae5b01aSLogan Gunthorpe 		const char *page, size_t count)
681cae5b01aSLogan Gunthorpe {
682cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
683cae5b01aSLogan Gunthorpe 	size_t len;
684cae5b01aSLogan Gunthorpe 	int ret;
685cae5b01aSLogan Gunthorpe 
686cae5b01aSLogan Gunthorpe 	mutex_lock(&subsys->lock);
687cae5b01aSLogan Gunthorpe 
688cae5b01aSLogan Gunthorpe 	ret = -EBUSY;
689cae5b01aSLogan Gunthorpe 	if (subsys->passthru_ctrl)
690cae5b01aSLogan Gunthorpe 		goto out_unlock;
691cae5b01aSLogan Gunthorpe 
692cae5b01aSLogan Gunthorpe 	ret = -EINVAL;
693cae5b01aSLogan Gunthorpe 	len = strcspn(page, "\n");
694cae5b01aSLogan Gunthorpe 	if (!len)
695cae5b01aSLogan Gunthorpe 		goto out_unlock;
696cae5b01aSLogan Gunthorpe 
697cae5b01aSLogan Gunthorpe 	kfree(subsys->passthru_ctrl_path);
698cae5b01aSLogan Gunthorpe 	ret = -ENOMEM;
699cae5b01aSLogan Gunthorpe 	subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
700cae5b01aSLogan Gunthorpe 	if (!subsys->passthru_ctrl_path)
701cae5b01aSLogan Gunthorpe 		goto out_unlock;
702cae5b01aSLogan Gunthorpe 
703cae5b01aSLogan Gunthorpe 	mutex_unlock(&subsys->lock);
704cae5b01aSLogan Gunthorpe 
705cae5b01aSLogan Gunthorpe 	return count;
706cae5b01aSLogan Gunthorpe out_unlock:
707cae5b01aSLogan Gunthorpe 	mutex_unlock(&subsys->lock);
708cae5b01aSLogan Gunthorpe 	return ret;
709cae5b01aSLogan Gunthorpe }
710cae5b01aSLogan Gunthorpe CONFIGFS_ATTR(nvmet_passthru_, device_path);
711cae5b01aSLogan Gunthorpe 
712cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_enable_show(struct config_item *item,
713cae5b01aSLogan Gunthorpe 		char *page)
714cae5b01aSLogan Gunthorpe {
715cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
716cae5b01aSLogan Gunthorpe 
717cae5b01aSLogan Gunthorpe 	return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
718cae5b01aSLogan Gunthorpe }
719cae5b01aSLogan Gunthorpe 
720cae5b01aSLogan Gunthorpe static ssize_t nvmet_passthru_enable_store(struct config_item *item,
721cae5b01aSLogan Gunthorpe 		const char *page, size_t count)
722cae5b01aSLogan Gunthorpe {
723cae5b01aSLogan Gunthorpe 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
724cae5b01aSLogan Gunthorpe 	bool enable;
725cae5b01aSLogan Gunthorpe 	int ret = 0;
726cae5b01aSLogan Gunthorpe 
727cae5b01aSLogan Gunthorpe 	if (strtobool(page, &enable))
728cae5b01aSLogan Gunthorpe 		return -EINVAL;
729cae5b01aSLogan Gunthorpe 
730cae5b01aSLogan Gunthorpe 	if (enable)
731cae5b01aSLogan Gunthorpe 		ret = nvmet_passthru_ctrl_enable(subsys);
732cae5b01aSLogan Gunthorpe 	else
733cae5b01aSLogan Gunthorpe 		nvmet_passthru_ctrl_disable(subsys);
734cae5b01aSLogan Gunthorpe 
735cae5b01aSLogan Gunthorpe 	return ret ? ret : count;
736cae5b01aSLogan Gunthorpe }
737cae5b01aSLogan Gunthorpe CONFIGFS_ATTR(nvmet_passthru_, enable);
738cae5b01aSLogan Gunthorpe 
739cae5b01aSLogan Gunthorpe static struct configfs_attribute *nvmet_passthru_attrs[] = {
740cae5b01aSLogan Gunthorpe 	&nvmet_passthru_attr_device_path,
741cae5b01aSLogan Gunthorpe 	&nvmet_passthru_attr_enable,
742cae5b01aSLogan Gunthorpe 	NULL,
743cae5b01aSLogan Gunthorpe };
744cae5b01aSLogan Gunthorpe 
745cae5b01aSLogan Gunthorpe static const struct config_item_type nvmet_passthru_type = {
746cae5b01aSLogan Gunthorpe 	.ct_attrs		= nvmet_passthru_attrs,
747cae5b01aSLogan Gunthorpe 	.ct_owner		= THIS_MODULE,
748cae5b01aSLogan Gunthorpe };
749cae5b01aSLogan Gunthorpe 
750cae5b01aSLogan Gunthorpe static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
751cae5b01aSLogan Gunthorpe {
752cae5b01aSLogan Gunthorpe 	config_group_init_type_name(&subsys->passthru_group,
753cae5b01aSLogan Gunthorpe 				    "passthru", &nvmet_passthru_type);
754cae5b01aSLogan Gunthorpe 	configfs_add_default_group(&subsys->passthru_group,
755cae5b01aSLogan Gunthorpe 				   &subsys->group);
756cae5b01aSLogan Gunthorpe }
757cae5b01aSLogan Gunthorpe 
758cae5b01aSLogan Gunthorpe #else /* CONFIG_NVME_TARGET_PASSTHRU */
759cae5b01aSLogan Gunthorpe 
760cae5b01aSLogan Gunthorpe static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
761cae5b01aSLogan Gunthorpe {
762cae5b01aSLogan Gunthorpe }
763cae5b01aSLogan Gunthorpe 
764cae5b01aSLogan Gunthorpe #endif /* CONFIG_NVME_TARGET_PASSTHRU */
765cae5b01aSLogan Gunthorpe 
766a07b4970SChristoph Hellwig static int nvmet_port_subsys_allow_link(struct config_item *parent,
767a07b4970SChristoph Hellwig 		struct config_item *target)
768a07b4970SChristoph Hellwig {
769a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
770a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys;
771a07b4970SChristoph Hellwig 	struct nvmet_subsys_link *link, *p;
772a07b4970SChristoph Hellwig 	int ret;
773a07b4970SChristoph Hellwig 
774a07b4970SChristoph Hellwig 	if (target->ci_type != &nvmet_subsys_type) {
775a07b4970SChristoph Hellwig 		pr_err("can only link subsystems into the subsystems dir.!\n");
776a07b4970SChristoph Hellwig 		return -EINVAL;
777a07b4970SChristoph Hellwig 	}
778a07b4970SChristoph Hellwig 	subsys = to_subsys(target);
779a07b4970SChristoph Hellwig 	link = kmalloc(sizeof(*link), GFP_KERNEL);
780a07b4970SChristoph Hellwig 	if (!link)
781a07b4970SChristoph Hellwig 		return -ENOMEM;
782a07b4970SChristoph Hellwig 	link->subsys = subsys;
783a07b4970SChristoph Hellwig 
784a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
785a07b4970SChristoph Hellwig 	ret = -EEXIST;
786a07b4970SChristoph Hellwig 	list_for_each_entry(p, &port->subsystems, entry) {
787a07b4970SChristoph Hellwig 		if (p->subsys == subsys)
788a07b4970SChristoph Hellwig 			goto out_free_link;
789a07b4970SChristoph Hellwig 	}
790a07b4970SChristoph Hellwig 
791a07b4970SChristoph Hellwig 	if (list_empty(&port->subsystems)) {
792a07b4970SChristoph Hellwig 		ret = nvmet_enable_port(port);
793a07b4970SChristoph Hellwig 		if (ret)
794a07b4970SChristoph Hellwig 			goto out_free_link;
795a07b4970SChristoph Hellwig 	}
796a07b4970SChristoph Hellwig 
797a07b4970SChristoph Hellwig 	list_add_tail(&link->entry, &port->subsystems);
798b662a078SJay Sternberg 	nvmet_port_disc_changed(port, subsys);
799b662a078SJay Sternberg 
800a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
801a07b4970SChristoph Hellwig 	return 0;
802a07b4970SChristoph Hellwig 
803a07b4970SChristoph Hellwig out_free_link:
804a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
805a07b4970SChristoph Hellwig 	kfree(link);
806a07b4970SChristoph Hellwig 	return ret;
807a07b4970SChristoph Hellwig }
808a07b4970SChristoph Hellwig 
809e16769d4SAndrzej Pietrasiewicz static void nvmet_port_subsys_drop_link(struct config_item *parent,
810a07b4970SChristoph Hellwig 		struct config_item *target)
811a07b4970SChristoph Hellwig {
812a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
813a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(target);
814a07b4970SChristoph Hellwig 	struct nvmet_subsys_link *p;
815a07b4970SChristoph Hellwig 
816a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
817a07b4970SChristoph Hellwig 	list_for_each_entry(p, &port->subsystems, entry) {
818a07b4970SChristoph Hellwig 		if (p->subsys == subsys)
819a07b4970SChristoph Hellwig 			goto found;
820a07b4970SChristoph Hellwig 	}
821a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
822e16769d4SAndrzej Pietrasiewicz 	return;
823a07b4970SChristoph Hellwig 
824a07b4970SChristoph Hellwig found:
825a07b4970SChristoph Hellwig 	list_del(&p->entry);
8263aed8673SLogan Gunthorpe 	nvmet_port_del_ctrls(port, subsys);
827b662a078SJay Sternberg 	nvmet_port_disc_changed(port, subsys);
828b662a078SJay Sternberg 
829a07b4970SChristoph Hellwig 	if (list_empty(&port->subsystems))
830a07b4970SChristoph Hellwig 		nvmet_disable_port(port);
831a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
832a07b4970SChristoph Hellwig 	kfree(p);
833a07b4970SChristoph Hellwig }
834a07b4970SChristoph Hellwig 
835a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_subsys_item_ops = {
836a07b4970SChristoph Hellwig 	.allow_link		= nvmet_port_subsys_allow_link,
837a07b4970SChristoph Hellwig 	.drop_link		= nvmet_port_subsys_drop_link,
838a07b4970SChristoph Hellwig };
839a07b4970SChristoph Hellwig 
84066603a31SBhumika Goyal static const struct config_item_type nvmet_port_subsys_type = {
841a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_port_subsys_item_ops,
842a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
843a07b4970SChristoph Hellwig };
844a07b4970SChristoph Hellwig 
845a07b4970SChristoph Hellwig static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
846a07b4970SChristoph Hellwig 		struct config_item *target)
847a07b4970SChristoph Hellwig {
848a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
849a07b4970SChristoph Hellwig 	struct nvmet_host *host;
850a07b4970SChristoph Hellwig 	struct nvmet_host_link *link, *p;
851a07b4970SChristoph Hellwig 	int ret;
852a07b4970SChristoph Hellwig 
853a07b4970SChristoph Hellwig 	if (target->ci_type != &nvmet_host_type) {
854a07b4970SChristoph Hellwig 		pr_err("can only link hosts into the allowed_hosts directory!\n");
855a07b4970SChristoph Hellwig 		return -EINVAL;
856a07b4970SChristoph Hellwig 	}
857a07b4970SChristoph Hellwig 
858a07b4970SChristoph Hellwig 	host = to_host(target);
859a07b4970SChristoph Hellwig 	link = kmalloc(sizeof(*link), GFP_KERNEL);
860a07b4970SChristoph Hellwig 	if (!link)
861a07b4970SChristoph Hellwig 		return -ENOMEM;
862a07b4970SChristoph Hellwig 	link->host = host;
863a07b4970SChristoph Hellwig 
864a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
865a07b4970SChristoph Hellwig 	ret = -EINVAL;
866a07b4970SChristoph Hellwig 	if (subsys->allow_any_host) {
867a07b4970SChristoph Hellwig 		pr_err("can't add hosts when allow_any_host is set!\n");
868a07b4970SChristoph Hellwig 		goto out_free_link;
869a07b4970SChristoph Hellwig 	}
870a07b4970SChristoph Hellwig 
871a07b4970SChristoph Hellwig 	ret = -EEXIST;
872a07b4970SChristoph Hellwig 	list_for_each_entry(p, &subsys->hosts, entry) {
873a07b4970SChristoph Hellwig 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
874a07b4970SChristoph Hellwig 			goto out_free_link;
875a07b4970SChristoph Hellwig 	}
876a07b4970SChristoph Hellwig 	list_add_tail(&link->entry, &subsys->hosts);
877b662a078SJay Sternberg 	nvmet_subsys_disc_changed(subsys, host);
878b662a078SJay Sternberg 
879a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
880a07b4970SChristoph Hellwig 	return 0;
881a07b4970SChristoph Hellwig out_free_link:
882a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
883a07b4970SChristoph Hellwig 	kfree(link);
884a07b4970SChristoph Hellwig 	return ret;
885a07b4970SChristoph Hellwig }
886a07b4970SChristoph Hellwig 
887e16769d4SAndrzej Pietrasiewicz static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
888a07b4970SChristoph Hellwig 		struct config_item *target)
889a07b4970SChristoph Hellwig {
890a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
891a07b4970SChristoph Hellwig 	struct nvmet_host *host = to_host(target);
892a07b4970SChristoph Hellwig 	struct nvmet_host_link *p;
893a07b4970SChristoph Hellwig 
894a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
895a07b4970SChristoph Hellwig 	list_for_each_entry(p, &subsys->hosts, entry) {
896a07b4970SChristoph Hellwig 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
897a07b4970SChristoph Hellwig 			goto found;
898a07b4970SChristoph Hellwig 	}
899a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
900e16769d4SAndrzej Pietrasiewicz 	return;
901a07b4970SChristoph Hellwig 
902a07b4970SChristoph Hellwig found:
903a07b4970SChristoph Hellwig 	list_del(&p->entry);
904b662a078SJay Sternberg 	nvmet_subsys_disc_changed(subsys, host);
905b662a078SJay Sternberg 
906a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
907a07b4970SChristoph Hellwig 	kfree(p);
908a07b4970SChristoph Hellwig }
909a07b4970SChristoph Hellwig 
910a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
911a07b4970SChristoph Hellwig 	.allow_link		= nvmet_allowed_hosts_allow_link,
912a07b4970SChristoph Hellwig 	.drop_link		= nvmet_allowed_hosts_drop_link,
913a07b4970SChristoph Hellwig };
914a07b4970SChristoph Hellwig 
91566603a31SBhumika Goyal static const struct config_item_type nvmet_allowed_hosts_type = {
916a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_allowed_hosts_item_ops,
917a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
918a07b4970SChristoph Hellwig };
919a07b4970SChristoph Hellwig 
920a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
921a07b4970SChristoph Hellwig 		char *page)
922a07b4970SChristoph Hellwig {
923a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n",
924a07b4970SChristoph Hellwig 		to_subsys(item)->allow_any_host);
925a07b4970SChristoph Hellwig }
926a07b4970SChristoph Hellwig 
927a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
928a07b4970SChristoph Hellwig 		const char *page, size_t count)
929a07b4970SChristoph Hellwig {
930a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(item);
931a07b4970SChristoph Hellwig 	bool allow_any_host;
932a07b4970SChristoph Hellwig 	int ret = 0;
933a07b4970SChristoph Hellwig 
934a07b4970SChristoph Hellwig 	if (strtobool(page, &allow_any_host))
935a07b4970SChristoph Hellwig 		return -EINVAL;
936a07b4970SChristoph Hellwig 
937a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
938a07b4970SChristoph Hellwig 	if (allow_any_host && !list_empty(&subsys->hosts)) {
939a07b4970SChristoph Hellwig 		pr_err("Can't set allow_any_host when explicit hosts are set!\n");
940a07b4970SChristoph Hellwig 		ret = -EINVAL;
941a07b4970SChristoph Hellwig 		goto out_unlock;
942a07b4970SChristoph Hellwig 	}
943a07b4970SChristoph Hellwig 
944b662a078SJay Sternberg 	if (subsys->allow_any_host != allow_any_host) {
945a07b4970SChristoph Hellwig 		subsys->allow_any_host = allow_any_host;
946b662a078SJay Sternberg 		nvmet_subsys_disc_changed(subsys, NULL);
947b662a078SJay Sternberg 	}
948b662a078SJay Sternberg 
949a07b4970SChristoph Hellwig out_unlock:
950a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
951a07b4970SChristoph Hellwig 	return ret ? ret : count;
952a07b4970SChristoph Hellwig }
953a07b4970SChristoph Hellwig 
954a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
955a07b4970SChristoph Hellwig 
95641528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
957c61d788bSJohannes Thumshirn 					      char *page)
958c61d788bSJohannes Thumshirn {
959c61d788bSJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
960c61d788bSJohannes Thumshirn 
961c61d788bSJohannes Thumshirn 	if (NVME_TERTIARY(subsys->ver))
962a0f0dbaaSChaitanya Kulkarni 		return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
963a0f0dbaaSChaitanya Kulkarni 				NVME_MAJOR(subsys->ver),
964a0f0dbaaSChaitanya Kulkarni 				NVME_MINOR(subsys->ver),
965a0f0dbaaSChaitanya Kulkarni 				NVME_TERTIARY(subsys->ver));
966527123c7SChaitanya Kulkarni 
967a0f0dbaaSChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
968a0f0dbaaSChaitanya Kulkarni 			NVME_MAJOR(subsys->ver),
969a0f0dbaaSChaitanya Kulkarni 			NVME_MINOR(subsys->ver));
970c61d788bSJohannes Thumshirn }
971c61d788bSJohannes Thumshirn 
97241528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
973c61d788bSJohannes Thumshirn 					       const char *page, size_t count)
974c61d788bSJohannes Thumshirn {
975c61d788bSJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
976c61d788bSJohannes Thumshirn 	int major, minor, tertiary = 0;
977c61d788bSJohannes Thumshirn 	int ret;
978c61d788bSJohannes Thumshirn 
979ba76af67SLogan Gunthorpe 	/* passthru subsystems use the underlying controller's version */
980ba76af67SLogan Gunthorpe 	if (nvmet_passthru_ctrl(subsys))
981ba76af67SLogan Gunthorpe 		return -EINVAL;
982ba76af67SLogan Gunthorpe 
983c61d788bSJohannes Thumshirn 	ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
984c61d788bSJohannes Thumshirn 	if (ret != 2 && ret != 3)
985c61d788bSJohannes Thumshirn 		return -EINVAL;
986c61d788bSJohannes Thumshirn 
987c61d788bSJohannes Thumshirn 	down_write(&nvmet_config_sem);
988c61d788bSJohannes Thumshirn 	subsys->ver = NVME_VS(major, minor, tertiary);
989c61d788bSJohannes Thumshirn 	up_write(&nvmet_config_sem);
990c61d788bSJohannes Thumshirn 
991c61d788bSJohannes Thumshirn 	return count;
992c61d788bSJohannes Thumshirn }
99341528f80SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_version);
994c61d788bSJohannes Thumshirn 
995fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
996fcbc5459SJohannes Thumshirn 					     char *page)
997fcbc5459SJohannes Thumshirn {
998fcbc5459SJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
999fcbc5459SJohannes Thumshirn 
1000fcbc5459SJohannes Thumshirn 	return snprintf(page, PAGE_SIZE, "%llx\n", subsys->serial);
1001fcbc5459SJohannes Thumshirn }
1002fcbc5459SJohannes Thumshirn 
1003fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
1004fcbc5459SJohannes Thumshirn 					      const char *page, size_t count)
1005fcbc5459SJohannes Thumshirn {
1006d3a9b0caSChaitanya Kulkarni 	u64 serial;
1007d3a9b0caSChaitanya Kulkarni 
1008d3a9b0caSChaitanya Kulkarni 	if (sscanf(page, "%llx\n", &serial) != 1)
1009d3a9b0caSChaitanya Kulkarni 		return -EINVAL;
1010fcbc5459SJohannes Thumshirn 
1011fcbc5459SJohannes Thumshirn 	down_write(&nvmet_config_sem);
1012d3a9b0caSChaitanya Kulkarni 	to_subsys(item)->serial = serial;
1013fcbc5459SJohannes Thumshirn 	up_write(&nvmet_config_sem);
1014fcbc5459SJohannes Thumshirn 
1015fcbc5459SJohannes Thumshirn 	return count;
1016fcbc5459SJohannes Thumshirn }
1017fcbc5459SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
1018fcbc5459SJohannes Thumshirn 
101994a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
102094a39d61SChaitanya Kulkarni 						 char *page)
102194a39d61SChaitanya Kulkarni {
102294a39d61SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
102394a39d61SChaitanya Kulkarni }
102494a39d61SChaitanya Kulkarni 
102594a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
102694a39d61SChaitanya Kulkarni 						  const char *page, size_t cnt)
102794a39d61SChaitanya Kulkarni {
102894a39d61SChaitanya Kulkarni 	u16 cntlid_min;
102994a39d61SChaitanya Kulkarni 
103094a39d61SChaitanya Kulkarni 	if (sscanf(page, "%hu\n", &cntlid_min) != 1)
103194a39d61SChaitanya Kulkarni 		return -EINVAL;
103294a39d61SChaitanya Kulkarni 
103394a39d61SChaitanya Kulkarni 	if (cntlid_min == 0)
103494a39d61SChaitanya Kulkarni 		return -EINVAL;
103594a39d61SChaitanya Kulkarni 
103694a39d61SChaitanya Kulkarni 	down_write(&nvmet_config_sem);
103794a39d61SChaitanya Kulkarni 	if (cntlid_min >= to_subsys(item)->cntlid_max)
103894a39d61SChaitanya Kulkarni 		goto out_unlock;
103994a39d61SChaitanya Kulkarni 	to_subsys(item)->cntlid_min = cntlid_min;
104094a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
104194a39d61SChaitanya Kulkarni 	return cnt;
104294a39d61SChaitanya Kulkarni 
104394a39d61SChaitanya Kulkarni out_unlock:
104494a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
104594a39d61SChaitanya Kulkarni 	return -EINVAL;
104694a39d61SChaitanya Kulkarni }
104794a39d61SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
104894a39d61SChaitanya Kulkarni 
104994a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
105094a39d61SChaitanya Kulkarni 						 char *page)
105194a39d61SChaitanya Kulkarni {
105294a39d61SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
105394a39d61SChaitanya Kulkarni }
105494a39d61SChaitanya Kulkarni 
105594a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
105694a39d61SChaitanya Kulkarni 						  const char *page, size_t cnt)
105794a39d61SChaitanya Kulkarni {
105894a39d61SChaitanya Kulkarni 	u16 cntlid_max;
105994a39d61SChaitanya Kulkarni 
106094a39d61SChaitanya Kulkarni 	if (sscanf(page, "%hu\n", &cntlid_max) != 1)
106194a39d61SChaitanya Kulkarni 		return -EINVAL;
106294a39d61SChaitanya Kulkarni 
106394a39d61SChaitanya Kulkarni 	if (cntlid_max == 0)
106494a39d61SChaitanya Kulkarni 		return -EINVAL;
106594a39d61SChaitanya Kulkarni 
106694a39d61SChaitanya Kulkarni 	down_write(&nvmet_config_sem);
106794a39d61SChaitanya Kulkarni 	if (cntlid_max <= to_subsys(item)->cntlid_min)
106894a39d61SChaitanya Kulkarni 		goto out_unlock;
106994a39d61SChaitanya Kulkarni 	to_subsys(item)->cntlid_max = cntlid_max;
107094a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
107194a39d61SChaitanya Kulkarni 	return cnt;
107294a39d61SChaitanya Kulkarni 
107394a39d61SChaitanya Kulkarni out_unlock:
107494a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
107594a39d61SChaitanya Kulkarni 	return -EINVAL;
107694a39d61SChaitanya Kulkarni }
107794a39d61SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
107894a39d61SChaitanya Kulkarni 
1079013b7ebeSMark Ruijter static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
1080013b7ebeSMark Ruijter 					    char *page)
1081013b7ebeSMark Ruijter {
1082013b7ebeSMark Ruijter 	struct nvmet_subsys *subsys = to_subsys(item);
1083013b7ebeSMark Ruijter 	struct nvmet_subsys_model *subsys_model;
1084013b7ebeSMark Ruijter 	char *model = NVMET_DEFAULT_CTRL_MODEL;
1085013b7ebeSMark Ruijter 	int ret;
1086013b7ebeSMark Ruijter 
1087013b7ebeSMark Ruijter 	rcu_read_lock();
1088013b7ebeSMark Ruijter 	subsys_model = rcu_dereference(subsys->model);
1089013b7ebeSMark Ruijter 	if (subsys_model)
1090013b7ebeSMark Ruijter 		model = subsys_model->number;
1091013b7ebeSMark Ruijter 	ret = snprintf(page, PAGE_SIZE, "%s\n", model);
1092013b7ebeSMark Ruijter 	rcu_read_unlock();
1093013b7ebeSMark Ruijter 
1094013b7ebeSMark Ruijter 	return ret;
1095013b7ebeSMark Ruijter }
1096013b7ebeSMark Ruijter 
1097013b7ebeSMark Ruijter /* See Section 1.5 of NVMe 1.4 */
1098013b7ebeSMark Ruijter static bool nvmet_is_ascii(const char c)
1099013b7ebeSMark Ruijter {
1100013b7ebeSMark Ruijter 	return c >= 0x20 && c <= 0x7e;
1101013b7ebeSMark Ruijter }
1102013b7ebeSMark Ruijter 
1103013b7ebeSMark Ruijter static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
1104013b7ebeSMark Ruijter 					     const char *page, size_t count)
1105013b7ebeSMark Ruijter {
1106013b7ebeSMark Ruijter 	struct nvmet_subsys *subsys = to_subsys(item);
1107013b7ebeSMark Ruijter 	struct nvmet_subsys_model *new_model;
1108013b7ebeSMark Ruijter 	char *new_model_number;
1109013b7ebeSMark Ruijter 	int pos = 0, len;
1110013b7ebeSMark Ruijter 
1111013b7ebeSMark Ruijter 	len = strcspn(page, "\n");
1112013b7ebeSMark Ruijter 	if (!len)
1113013b7ebeSMark Ruijter 		return -EINVAL;
1114013b7ebeSMark Ruijter 
1115013b7ebeSMark Ruijter 	for (pos = 0; pos < len; pos++) {
1116013b7ebeSMark Ruijter 		if (!nvmet_is_ascii(page[pos]))
1117013b7ebeSMark Ruijter 			return -EINVAL;
1118013b7ebeSMark Ruijter 	}
1119013b7ebeSMark Ruijter 
112009bb8986SChen Zhou 	new_model_number = kmemdup_nul(page, len, GFP_KERNEL);
1121013b7ebeSMark Ruijter 	if (!new_model_number)
1122013b7ebeSMark Ruijter 		return -ENOMEM;
1123013b7ebeSMark Ruijter 
1124013b7ebeSMark Ruijter 	new_model = kzalloc(sizeof(*new_model) + len + 1, GFP_KERNEL);
1125013b7ebeSMark Ruijter 	if (!new_model) {
1126013b7ebeSMark Ruijter 		kfree(new_model_number);
1127013b7ebeSMark Ruijter 		return -ENOMEM;
1128013b7ebeSMark Ruijter 	}
1129013b7ebeSMark Ruijter 	memcpy(new_model->number, new_model_number, len);
1130013b7ebeSMark Ruijter 
1131013b7ebeSMark Ruijter 	down_write(&nvmet_config_sem);
1132013b7ebeSMark Ruijter 	mutex_lock(&subsys->lock);
1133013b7ebeSMark Ruijter 	new_model = rcu_replace_pointer(subsys->model, new_model,
1134013b7ebeSMark Ruijter 					mutex_is_locked(&subsys->lock));
1135013b7ebeSMark Ruijter 	mutex_unlock(&subsys->lock);
1136013b7ebeSMark Ruijter 	up_write(&nvmet_config_sem);
1137013b7ebeSMark Ruijter 
1138013b7ebeSMark Ruijter 	kfree_rcu(new_model, rcuhead);
1139013b7ebeSMark Ruijter 
1140013b7ebeSMark Ruijter 	return count;
1141013b7ebeSMark Ruijter }
1142013b7ebeSMark Ruijter CONFIGFS_ATTR(nvmet_subsys_, attr_model);
1143013b7ebeSMark Ruijter 
1144ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1145ea52ac1cSIsrael Rukshin static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
1146ea52ac1cSIsrael Rukshin 						char *page)
1147ea52ac1cSIsrael Rukshin {
1148ea52ac1cSIsrael Rukshin 	return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
1149ea52ac1cSIsrael Rukshin }
1150ea52ac1cSIsrael Rukshin 
1151ea52ac1cSIsrael Rukshin static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
1152ea52ac1cSIsrael Rukshin 						 const char *page, size_t count)
1153ea52ac1cSIsrael Rukshin {
1154ea52ac1cSIsrael Rukshin 	struct nvmet_subsys *subsys = to_subsys(item);
1155ea52ac1cSIsrael Rukshin 	bool pi_enable;
1156ea52ac1cSIsrael Rukshin 
1157ea52ac1cSIsrael Rukshin 	if (strtobool(page, &pi_enable))
1158ea52ac1cSIsrael Rukshin 		return -EINVAL;
1159ea52ac1cSIsrael Rukshin 
1160ea52ac1cSIsrael Rukshin 	subsys->pi_support = pi_enable;
1161ea52ac1cSIsrael Rukshin 	return count;
1162ea52ac1cSIsrael Rukshin }
1163ea52ac1cSIsrael Rukshin CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
1164ea52ac1cSIsrael Rukshin #endif
1165ea52ac1cSIsrael Rukshin 
1166a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_subsys_attrs[] = {
1167a07b4970SChristoph Hellwig 	&nvmet_subsys_attr_attr_allow_any_host,
116841528f80SJohannes Thumshirn 	&nvmet_subsys_attr_attr_version,
1169fcbc5459SJohannes Thumshirn 	&nvmet_subsys_attr_attr_serial,
117094a39d61SChaitanya Kulkarni 	&nvmet_subsys_attr_attr_cntlid_min,
117194a39d61SChaitanya Kulkarni 	&nvmet_subsys_attr_attr_cntlid_max,
1172013b7ebeSMark Ruijter 	&nvmet_subsys_attr_attr_model,
1173ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1174ea52ac1cSIsrael Rukshin 	&nvmet_subsys_attr_attr_pi_enable,
1175ea52ac1cSIsrael Rukshin #endif
1176a07b4970SChristoph Hellwig 	NULL,
1177a07b4970SChristoph Hellwig };
1178a07b4970SChristoph Hellwig 
1179a07b4970SChristoph Hellwig /*
1180a07b4970SChristoph Hellwig  * Subsystem structures & folder operation functions below
1181a07b4970SChristoph Hellwig  */
1182a07b4970SChristoph Hellwig static void nvmet_subsys_release(struct config_item *item)
1183a07b4970SChristoph Hellwig {
1184a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(item);
1185a07b4970SChristoph Hellwig 
1186344770b0SSagi Grimberg 	nvmet_subsys_del_ctrls(subsys);
1187a07b4970SChristoph Hellwig 	nvmet_subsys_put(subsys);
1188a07b4970SChristoph Hellwig }
1189a07b4970SChristoph Hellwig 
1190a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_subsys_item_ops = {
1191a07b4970SChristoph Hellwig 	.release		= nvmet_subsys_release,
1192a07b4970SChristoph Hellwig };
1193a07b4970SChristoph Hellwig 
119466603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type = {
1195a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_subsys_item_ops,
1196a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_subsys_attrs,
1197a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1198a07b4970SChristoph Hellwig };
1199a07b4970SChristoph Hellwig 
1200a07b4970SChristoph Hellwig static struct config_group *nvmet_subsys_make(struct config_group *group,
1201a07b4970SChristoph Hellwig 		const char *name)
1202a07b4970SChristoph Hellwig {
1203a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys;
1204a07b4970SChristoph Hellwig 
1205a07b4970SChristoph Hellwig 	if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1206a07b4970SChristoph Hellwig 		pr_err("can't create discovery subsystem through configfs\n");
1207a07b4970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1208a07b4970SChristoph Hellwig 	}
1209a07b4970SChristoph Hellwig 
1210a07b4970SChristoph Hellwig 	subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
12116b7e631bSMinwoo Im 	if (IS_ERR(subsys))
12126b7e631bSMinwoo Im 		return ERR_CAST(subsys);
1213a07b4970SChristoph Hellwig 
1214a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1215a07b4970SChristoph Hellwig 
1216a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->namespaces_group,
1217a07b4970SChristoph Hellwig 			"namespaces", &nvmet_namespaces_type);
1218a07b4970SChristoph Hellwig 	configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1219a07b4970SChristoph Hellwig 
1220a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->allowed_hosts_group,
1221a07b4970SChristoph Hellwig 			"allowed_hosts", &nvmet_allowed_hosts_type);
1222a07b4970SChristoph Hellwig 	configfs_add_default_group(&subsys->allowed_hosts_group,
1223a07b4970SChristoph Hellwig 			&subsys->group);
1224a07b4970SChristoph Hellwig 
1225cae5b01aSLogan Gunthorpe 	nvmet_add_passthru_group(subsys);
1226cae5b01aSLogan Gunthorpe 
1227a07b4970SChristoph Hellwig 	return &subsys->group;
1228a07b4970SChristoph Hellwig }
1229a07b4970SChristoph Hellwig 
1230a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_subsystems_group_ops = {
1231a07b4970SChristoph Hellwig 	.make_group		= nvmet_subsys_make,
1232a07b4970SChristoph Hellwig };
1233a07b4970SChristoph Hellwig 
123466603a31SBhumika Goyal static const struct config_item_type nvmet_subsystems_type = {
1235a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_subsystems_group_ops,
1236a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1237a07b4970SChristoph Hellwig };
1238a07b4970SChristoph Hellwig 
1239a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_show(struct config_item *item,
1240a07b4970SChristoph Hellwig 		char *page)
1241a07b4970SChristoph Hellwig {
1242a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1243a07b4970SChristoph Hellwig }
1244a07b4970SChristoph Hellwig 
1245a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_store(struct config_item *item,
1246a07b4970SChristoph Hellwig 		const char *page, size_t count)
1247a07b4970SChristoph Hellwig {
1248a07b4970SChristoph Hellwig 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1249a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1250a07b4970SChristoph Hellwig 	bool enable;
1251a07b4970SChristoph Hellwig 
1252a07b4970SChristoph Hellwig 	if (strtobool(page, &enable))
1253a07b4970SChristoph Hellwig 		goto inval;
1254a07b4970SChristoph Hellwig 
1255a07b4970SChristoph Hellwig 	if (enable)
1256a07b4970SChristoph Hellwig 		nvmet_referral_enable(parent, port);
1257a07b4970SChristoph Hellwig 	else
1258b662a078SJay Sternberg 		nvmet_referral_disable(parent, port);
1259a07b4970SChristoph Hellwig 
1260a07b4970SChristoph Hellwig 	return count;
1261a07b4970SChristoph Hellwig inval:
1262a07b4970SChristoph Hellwig 	pr_err("Invalid value '%s' for enable\n", page);
1263a07b4970SChristoph Hellwig 	return -EINVAL;
1264a07b4970SChristoph Hellwig }
1265a07b4970SChristoph Hellwig 
1266a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_referral_, enable);
1267a07b4970SChristoph Hellwig 
1268a07b4970SChristoph Hellwig /*
1269a07b4970SChristoph Hellwig  * Discovery Service subsystem definitions
1270a07b4970SChristoph Hellwig  */
1271a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_referral_attrs[] = {
1272a07b4970SChristoph Hellwig 	&nvmet_attr_addr_adrfam,
1273a07b4970SChristoph Hellwig 	&nvmet_attr_addr_portid,
1274a07b4970SChristoph Hellwig 	&nvmet_attr_addr_treq,
1275a07b4970SChristoph Hellwig 	&nvmet_attr_addr_traddr,
1276a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trsvcid,
1277a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trtype,
1278a07b4970SChristoph Hellwig 	&nvmet_referral_attr_enable,
1279a07b4970SChristoph Hellwig 	NULL,
1280a07b4970SChristoph Hellwig };
1281a07b4970SChristoph Hellwig 
1282f0e656e4SSagi Grimberg static void nvmet_referral_notify(struct config_group *group,
1283f0e656e4SSagi Grimberg 		struct config_item *item)
1284a07b4970SChristoph Hellwig {
1285b662a078SJay Sternberg 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1286a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1287a07b4970SChristoph Hellwig 
1288b662a078SJay Sternberg 	nvmet_referral_disable(parent, port);
1289f0e656e4SSagi Grimberg }
1290f0e656e4SSagi Grimberg 
1291f0e656e4SSagi Grimberg static void nvmet_referral_release(struct config_item *item)
1292f0e656e4SSagi Grimberg {
1293f0e656e4SSagi Grimberg 	struct nvmet_port *port = to_nvmet_port(item);
1294f0e656e4SSagi Grimberg 
1295a07b4970SChristoph Hellwig 	kfree(port);
1296a07b4970SChristoph Hellwig }
1297a07b4970SChristoph Hellwig 
1298a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_referral_item_ops = {
1299a07b4970SChristoph Hellwig 	.release	= nvmet_referral_release,
1300a07b4970SChristoph Hellwig };
1301a07b4970SChristoph Hellwig 
130266603a31SBhumika Goyal static const struct config_item_type nvmet_referral_type = {
1303a07b4970SChristoph Hellwig 	.ct_owner	= THIS_MODULE,
1304a07b4970SChristoph Hellwig 	.ct_attrs	= nvmet_referral_attrs,
1305a07b4970SChristoph Hellwig 	.ct_item_ops	= &nvmet_referral_item_ops,
1306a07b4970SChristoph Hellwig };
1307a07b4970SChristoph Hellwig 
1308a07b4970SChristoph Hellwig static struct config_group *nvmet_referral_make(
1309a07b4970SChristoph Hellwig 		struct config_group *group, const char *name)
1310a07b4970SChristoph Hellwig {
1311a07b4970SChristoph Hellwig 	struct nvmet_port *port;
1312a07b4970SChristoph Hellwig 
1313a07b4970SChristoph Hellwig 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1314a07b4970SChristoph Hellwig 	if (!port)
1315f98d9ca1SDan Carpenter 		return ERR_PTR(-ENOMEM);
1316a07b4970SChristoph Hellwig 
1317a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->entry);
1318a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1319a07b4970SChristoph Hellwig 
1320a07b4970SChristoph Hellwig 	return &port->group;
1321a07b4970SChristoph Hellwig }
1322a07b4970SChristoph Hellwig 
1323a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_referral_group_ops = {
1324a07b4970SChristoph Hellwig 	.make_group		= nvmet_referral_make,
1325f0e656e4SSagi Grimberg 	.disconnect_notify	= nvmet_referral_notify,
1326a07b4970SChristoph Hellwig };
1327a07b4970SChristoph Hellwig 
132866603a31SBhumika Goyal static const struct config_item_type nvmet_referrals_type = {
1329a07b4970SChristoph Hellwig 	.ct_owner	= THIS_MODULE,
1330a07b4970SChristoph Hellwig 	.ct_group_ops	= &nvmet_referral_group_ops,
1331a07b4970SChristoph Hellwig };
1332a07b4970SChristoph Hellwig 
133374255969SChristoph Hellwig static struct nvmet_type_name_map nvmet_ana_state[] = {
133462ac0d32SChristoph Hellwig 	{ NVME_ANA_OPTIMIZED,		"optimized" },
133562ac0d32SChristoph Hellwig 	{ NVME_ANA_NONOPTIMIZED,	"non-optimized" },
133662ac0d32SChristoph Hellwig 	{ NVME_ANA_INACCESSIBLE,	"inaccessible" },
133762ac0d32SChristoph Hellwig 	{ NVME_ANA_PERSISTENT_LOSS,	"persistent-loss" },
133862ac0d32SChristoph Hellwig 	{ NVME_ANA_CHANGE,		"change" },
133962ac0d32SChristoph Hellwig };
134062ac0d32SChristoph Hellwig 
134162ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
134262ac0d32SChristoph Hellwig 		char *page)
134362ac0d32SChristoph Hellwig {
134462ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
134562ac0d32SChristoph Hellwig 	enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
134662ac0d32SChristoph Hellwig 	int i;
134762ac0d32SChristoph Hellwig 
134884b8d0d7SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
134984b8d0d7SChaitanya Kulkarni 		if (state == nvmet_ana_state[i].type)
135084b8d0d7SChaitanya Kulkarni 			return sprintf(page, "%s\n", nvmet_ana_state[i].name);
135162ac0d32SChristoph Hellwig 	}
135262ac0d32SChristoph Hellwig 
135362ac0d32SChristoph Hellwig 	return sprintf(page, "\n");
135462ac0d32SChristoph Hellwig }
135562ac0d32SChristoph Hellwig 
135662ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
135762ac0d32SChristoph Hellwig 		const char *page, size_t count)
135862ac0d32SChristoph Hellwig {
135962ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
136084b8d0d7SChaitanya Kulkarni 	enum nvme_ana_state *ana_state = grp->port->ana_state;
136162ac0d32SChristoph Hellwig 	int i;
136262ac0d32SChristoph Hellwig 
136384b8d0d7SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
136484b8d0d7SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_ana_state[i].name))
136562ac0d32SChristoph Hellwig 			goto found;
136662ac0d32SChristoph Hellwig 	}
136762ac0d32SChristoph Hellwig 
136862ac0d32SChristoph Hellwig 	pr_err("Invalid value '%s' for ana_state\n", page);
136962ac0d32SChristoph Hellwig 	return -EINVAL;
137062ac0d32SChristoph Hellwig 
137162ac0d32SChristoph Hellwig found:
137262ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
137384b8d0d7SChaitanya Kulkarni 	ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
137462ac0d32SChristoph Hellwig 	nvmet_ana_chgcnt++;
137562ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
137662ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
137762ac0d32SChristoph Hellwig 	return count;
137862ac0d32SChristoph Hellwig }
137962ac0d32SChristoph Hellwig 
138062ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
138162ac0d32SChristoph Hellwig 
138262ac0d32SChristoph Hellwig static struct configfs_attribute *nvmet_ana_group_attrs[] = {
138362ac0d32SChristoph Hellwig 	&nvmet_ana_group_attr_ana_state,
138462ac0d32SChristoph Hellwig 	NULL,
138562ac0d32SChristoph Hellwig };
138662ac0d32SChristoph Hellwig 
138762ac0d32SChristoph Hellwig static void nvmet_ana_group_release(struct config_item *item)
138862ac0d32SChristoph Hellwig {
138962ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
139062ac0d32SChristoph Hellwig 
139162ac0d32SChristoph Hellwig 	if (grp == &grp->port->ana_default_group)
139262ac0d32SChristoph Hellwig 		return;
139362ac0d32SChristoph Hellwig 
139462ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
139562ac0d32SChristoph Hellwig 	grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
139662ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[grp->grpid]--;
139762ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
139862ac0d32SChristoph Hellwig 
139962ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
140062ac0d32SChristoph Hellwig 	kfree(grp);
140162ac0d32SChristoph Hellwig }
140262ac0d32SChristoph Hellwig 
140362ac0d32SChristoph Hellwig static struct configfs_item_operations nvmet_ana_group_item_ops = {
140462ac0d32SChristoph Hellwig 	.release		= nvmet_ana_group_release,
140562ac0d32SChristoph Hellwig };
140662ac0d32SChristoph Hellwig 
140762ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_group_type = {
140862ac0d32SChristoph Hellwig 	.ct_item_ops		= &nvmet_ana_group_item_ops,
140962ac0d32SChristoph Hellwig 	.ct_attrs		= nvmet_ana_group_attrs,
141062ac0d32SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
141162ac0d32SChristoph Hellwig };
141262ac0d32SChristoph Hellwig 
141362ac0d32SChristoph Hellwig static struct config_group *nvmet_ana_groups_make_group(
141462ac0d32SChristoph Hellwig 		struct config_group *group, const char *name)
141562ac0d32SChristoph Hellwig {
141662ac0d32SChristoph Hellwig 	struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
141762ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp;
141862ac0d32SChristoph Hellwig 	u32 grpid;
141962ac0d32SChristoph Hellwig 	int ret;
142062ac0d32SChristoph Hellwig 
142162ac0d32SChristoph Hellwig 	ret = kstrtou32(name, 0, &grpid);
142262ac0d32SChristoph Hellwig 	if (ret)
142362ac0d32SChristoph Hellwig 		goto out;
142462ac0d32SChristoph Hellwig 
142562ac0d32SChristoph Hellwig 	ret = -EINVAL;
142662ac0d32SChristoph Hellwig 	if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
142762ac0d32SChristoph Hellwig 		goto out;
142862ac0d32SChristoph Hellwig 
142962ac0d32SChristoph Hellwig 	ret = -ENOMEM;
143062ac0d32SChristoph Hellwig 	grp = kzalloc(sizeof(*grp), GFP_KERNEL);
143162ac0d32SChristoph Hellwig 	if (!grp)
143262ac0d32SChristoph Hellwig 		goto out;
143362ac0d32SChristoph Hellwig 	grp->port = port;
143462ac0d32SChristoph Hellwig 	grp->grpid = grpid;
143562ac0d32SChristoph Hellwig 
143662ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
143762ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[grpid]++;
143862ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
143962ac0d32SChristoph Hellwig 
144062ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
144162ac0d32SChristoph Hellwig 
144262ac0d32SChristoph Hellwig 	config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
144362ac0d32SChristoph Hellwig 	return &grp->group;
144462ac0d32SChristoph Hellwig out:
144562ac0d32SChristoph Hellwig 	return ERR_PTR(ret);
144662ac0d32SChristoph Hellwig }
144762ac0d32SChristoph Hellwig 
144862ac0d32SChristoph Hellwig static struct configfs_group_operations nvmet_ana_groups_group_ops = {
144962ac0d32SChristoph Hellwig 	.make_group		= nvmet_ana_groups_make_group,
145062ac0d32SChristoph Hellwig };
145162ac0d32SChristoph Hellwig 
145262ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_groups_type = {
145362ac0d32SChristoph Hellwig 	.ct_group_ops		= &nvmet_ana_groups_group_ops,
145462ac0d32SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
145562ac0d32SChristoph Hellwig };
145662ac0d32SChristoph Hellwig 
1457a07b4970SChristoph Hellwig /*
1458a07b4970SChristoph Hellwig  * Ports definitions.
1459a07b4970SChristoph Hellwig  */
1460a07b4970SChristoph Hellwig static void nvmet_port_release(struct config_item *item)
1461a07b4970SChristoph Hellwig {
1462a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1463a07b4970SChristoph Hellwig 
1464b662a078SJay Sternberg 	list_del(&port->global_entry);
1465b662a078SJay Sternberg 
146672efd25dSChristoph Hellwig 	kfree(port->ana_state);
1467a07b4970SChristoph Hellwig 	kfree(port);
1468a07b4970SChristoph Hellwig }
1469a07b4970SChristoph Hellwig 
1470a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_port_attrs[] = {
1471a07b4970SChristoph Hellwig 	&nvmet_attr_addr_adrfam,
1472a07b4970SChristoph Hellwig 	&nvmet_attr_addr_treq,
1473a07b4970SChristoph Hellwig 	&nvmet_attr_addr_traddr,
1474a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trsvcid,
1475a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trtype,
14760d5ee2b2SSteve Wise 	&nvmet_attr_param_inline_data_size,
1477ea52ac1cSIsrael Rukshin #ifdef CONFIG_BLK_DEV_INTEGRITY
1478ea52ac1cSIsrael Rukshin 	&nvmet_attr_param_pi_enable,
1479ea52ac1cSIsrael Rukshin #endif
1480a07b4970SChristoph Hellwig 	NULL,
1481a07b4970SChristoph Hellwig };
1482a07b4970SChristoph Hellwig 
1483a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_item_ops = {
1484a07b4970SChristoph Hellwig 	.release		= nvmet_port_release,
1485a07b4970SChristoph Hellwig };
1486a07b4970SChristoph Hellwig 
148766603a31SBhumika Goyal static const struct config_item_type nvmet_port_type = {
1488a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_port_attrs,
1489a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_port_item_ops,
1490a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1491a07b4970SChristoph Hellwig };
1492a07b4970SChristoph Hellwig 
1493a07b4970SChristoph Hellwig static struct config_group *nvmet_ports_make(struct config_group *group,
1494a07b4970SChristoph Hellwig 		const char *name)
1495a07b4970SChristoph Hellwig {
1496a07b4970SChristoph Hellwig 	struct nvmet_port *port;
1497a07b4970SChristoph Hellwig 	u16 portid;
149862ac0d32SChristoph Hellwig 	u32 i;
1499a07b4970SChristoph Hellwig 
1500a07b4970SChristoph Hellwig 	if (kstrtou16(name, 0, &portid))
1501a07b4970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1502a07b4970SChristoph Hellwig 
1503a07b4970SChristoph Hellwig 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1504a07b4970SChristoph Hellwig 	if (!port)
1505f98d9ca1SDan Carpenter 		return ERR_PTR(-ENOMEM);
1506a07b4970SChristoph Hellwig 
150772efd25dSChristoph Hellwig 	port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
150872efd25dSChristoph Hellwig 			sizeof(*port->ana_state), GFP_KERNEL);
150972efd25dSChristoph Hellwig 	if (!port->ana_state) {
151072efd25dSChristoph Hellwig 		kfree(port);
151172efd25dSChristoph Hellwig 		return ERR_PTR(-ENOMEM);
151272efd25dSChristoph Hellwig 	}
151372efd25dSChristoph Hellwig 
151462ac0d32SChristoph Hellwig 	for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
151562ac0d32SChristoph Hellwig 		if (i == NVMET_DEFAULT_ANA_GRPID)
151662ac0d32SChristoph Hellwig 			port->ana_state[1] = NVME_ANA_OPTIMIZED;
151762ac0d32SChristoph Hellwig 		else
151862ac0d32SChristoph Hellwig 			port->ana_state[i] = NVME_ANA_INACCESSIBLE;
151962ac0d32SChristoph Hellwig 	}
152072efd25dSChristoph Hellwig 
1521b662a078SJay Sternberg 	list_add(&port->global_entry, &nvmet_ports_list);
1522b662a078SJay Sternberg 
1523a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->entry);
1524a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->subsystems);
1525a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->referrals);
15260d5ee2b2SSteve Wise 	port->inline_data_size = -1;	/* < 0 == let the transport choose */
1527a07b4970SChristoph Hellwig 
1528a07b4970SChristoph Hellwig 	port->disc_addr.portid = cpu_to_le16(portid);
1529d02abd19SChaitanya Kulkarni 	port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
15309b95d2fbSSagi Grimberg 	port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1531a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->group, name, &nvmet_port_type);
1532a07b4970SChristoph Hellwig 
1533a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->subsys_group,
1534a07b4970SChristoph Hellwig 			"subsystems", &nvmet_port_subsys_type);
1535a07b4970SChristoph Hellwig 	configfs_add_default_group(&port->subsys_group, &port->group);
1536a07b4970SChristoph Hellwig 
1537a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->referrals_group,
1538a07b4970SChristoph Hellwig 			"referrals", &nvmet_referrals_type);
1539a07b4970SChristoph Hellwig 	configfs_add_default_group(&port->referrals_group, &port->group);
1540a07b4970SChristoph Hellwig 
154162ac0d32SChristoph Hellwig 	config_group_init_type_name(&port->ana_groups_group,
154262ac0d32SChristoph Hellwig 			"ana_groups", &nvmet_ana_groups_type);
154362ac0d32SChristoph Hellwig 	configfs_add_default_group(&port->ana_groups_group, &port->group);
154462ac0d32SChristoph Hellwig 
154562ac0d32SChristoph Hellwig 	port->ana_default_group.port = port;
154662ac0d32SChristoph Hellwig 	port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
154762ac0d32SChristoph Hellwig 	config_group_init_type_name(&port->ana_default_group.group,
154862ac0d32SChristoph Hellwig 			__stringify(NVMET_DEFAULT_ANA_GRPID),
154962ac0d32SChristoph Hellwig 			&nvmet_ana_group_type);
155062ac0d32SChristoph Hellwig 	configfs_add_default_group(&port->ana_default_group.group,
155162ac0d32SChristoph Hellwig 			&port->ana_groups_group);
155262ac0d32SChristoph Hellwig 
1553a07b4970SChristoph Hellwig 	return &port->group;
1554a07b4970SChristoph Hellwig }
1555a07b4970SChristoph Hellwig 
1556a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_ports_group_ops = {
1557a07b4970SChristoph Hellwig 	.make_group		= nvmet_ports_make,
1558a07b4970SChristoph Hellwig };
1559a07b4970SChristoph Hellwig 
156066603a31SBhumika Goyal static const struct config_item_type nvmet_ports_type = {
1561a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_ports_group_ops,
1562a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1563a07b4970SChristoph Hellwig };
1564a07b4970SChristoph Hellwig 
1565a07b4970SChristoph Hellwig static struct config_group nvmet_subsystems_group;
1566a07b4970SChristoph Hellwig static struct config_group nvmet_ports_group;
1567a07b4970SChristoph Hellwig 
1568a07b4970SChristoph Hellwig static void nvmet_host_release(struct config_item *item)
1569a07b4970SChristoph Hellwig {
1570a07b4970SChristoph Hellwig 	struct nvmet_host *host = to_host(item);
1571a07b4970SChristoph Hellwig 
1572a07b4970SChristoph Hellwig 	kfree(host);
1573a07b4970SChristoph Hellwig }
1574a07b4970SChristoph Hellwig 
1575a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_host_item_ops = {
1576a07b4970SChristoph Hellwig 	.release		= nvmet_host_release,
1577a07b4970SChristoph Hellwig };
1578a07b4970SChristoph Hellwig 
157966603a31SBhumika Goyal static const struct config_item_type nvmet_host_type = {
1580a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_host_item_ops,
1581a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1582a07b4970SChristoph Hellwig };
1583a07b4970SChristoph Hellwig 
1584a07b4970SChristoph Hellwig static struct config_group *nvmet_hosts_make_group(struct config_group *group,
1585a07b4970SChristoph Hellwig 		const char *name)
1586a07b4970SChristoph Hellwig {
1587a07b4970SChristoph Hellwig 	struct nvmet_host *host;
1588a07b4970SChristoph Hellwig 
1589a07b4970SChristoph Hellwig 	host = kzalloc(sizeof(*host), GFP_KERNEL);
1590a07b4970SChristoph Hellwig 	if (!host)
1591a07b4970SChristoph Hellwig 		return ERR_PTR(-ENOMEM);
1592a07b4970SChristoph Hellwig 
1593a07b4970SChristoph Hellwig 	config_group_init_type_name(&host->group, name, &nvmet_host_type);
1594a07b4970SChristoph Hellwig 
1595a07b4970SChristoph Hellwig 	return &host->group;
1596a07b4970SChristoph Hellwig }
1597a07b4970SChristoph Hellwig 
1598a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_hosts_group_ops = {
1599a07b4970SChristoph Hellwig 	.make_group		= nvmet_hosts_make_group,
1600a07b4970SChristoph Hellwig };
1601a07b4970SChristoph Hellwig 
160266603a31SBhumika Goyal static const struct config_item_type nvmet_hosts_type = {
1603a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_hosts_group_ops,
1604a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1605a07b4970SChristoph Hellwig };
1606a07b4970SChristoph Hellwig 
1607a07b4970SChristoph Hellwig static struct config_group nvmet_hosts_group;
1608a07b4970SChristoph Hellwig 
160966603a31SBhumika Goyal static const struct config_item_type nvmet_root_type = {
1610a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1611a07b4970SChristoph Hellwig };
1612a07b4970SChristoph Hellwig 
1613a07b4970SChristoph Hellwig static struct configfs_subsystem nvmet_configfs_subsystem = {
1614a07b4970SChristoph Hellwig 	.su_group = {
1615a07b4970SChristoph Hellwig 		.cg_item = {
1616a07b4970SChristoph Hellwig 			.ci_namebuf	= "nvmet",
1617a07b4970SChristoph Hellwig 			.ci_type	= &nvmet_root_type,
1618a07b4970SChristoph Hellwig 		},
1619a07b4970SChristoph Hellwig 	},
1620a07b4970SChristoph Hellwig };
1621a07b4970SChristoph Hellwig 
1622a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void)
1623a07b4970SChristoph Hellwig {
1624a07b4970SChristoph Hellwig 	int ret;
1625a07b4970SChristoph Hellwig 
1626a07b4970SChristoph Hellwig 	config_group_init(&nvmet_configfs_subsystem.su_group);
1627a07b4970SChristoph Hellwig 	mutex_init(&nvmet_configfs_subsystem.su_mutex);
1628a07b4970SChristoph Hellwig 
1629a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_subsystems_group,
1630a07b4970SChristoph Hellwig 			"subsystems", &nvmet_subsystems_type);
1631a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_subsystems_group,
1632a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1633a07b4970SChristoph Hellwig 
1634a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_ports_group,
1635a07b4970SChristoph Hellwig 			"ports", &nvmet_ports_type);
1636a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_ports_group,
1637a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1638a07b4970SChristoph Hellwig 
1639a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_hosts_group,
1640a07b4970SChristoph Hellwig 			"hosts", &nvmet_hosts_type);
1641a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_hosts_group,
1642a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1643a07b4970SChristoph Hellwig 
1644a07b4970SChristoph Hellwig 	ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
1645a07b4970SChristoph Hellwig 	if (ret) {
1646a07b4970SChristoph Hellwig 		pr_err("configfs_register_subsystem: %d\n", ret);
1647a07b4970SChristoph Hellwig 		return ret;
1648a07b4970SChristoph Hellwig 	}
1649a07b4970SChristoph Hellwig 
1650a07b4970SChristoph Hellwig 	return 0;
1651a07b4970SChristoph Hellwig }
1652a07b4970SChristoph Hellwig 
1653a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void)
1654a07b4970SChristoph Hellwig {
1655a07b4970SChristoph Hellwig 	configfs_unregister_subsystem(&nvmet_configfs_subsystem);
1656a07b4970SChristoph Hellwig }
1657