xref: /openbmc/linux/drivers/nvme/target/configfs.c (revision 7e764179)
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" },
417e764179SChaitanya Kulkarni };
427e764179SChaitanya Kulkarni 
43a07b4970SChristoph Hellwig /*
44a07b4970SChristoph Hellwig  * nvmet_port Generic ConfigFS definitions.
45a07b4970SChristoph Hellwig  * Used in any place in the ConfigFS tree that refers to an address.
46a07b4970SChristoph Hellwig  */
477e764179SChaitanya Kulkarni static ssize_t nvmet_addr_adrfam_show(struct config_item *item, char *page)
48a07b4970SChristoph Hellwig {
497e764179SChaitanya Kulkarni 	u8 adrfam = to_nvmet_port(item)->disc_addr.adrfam;
507e764179SChaitanya Kulkarni 	int i;
517e764179SChaitanya Kulkarni 
527e764179SChaitanya Kulkarni 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
537e764179SChaitanya Kulkarni 		if (nvmet_addr_family[i].type == adrfam)
547e764179SChaitanya Kulkarni 			return sprintf(page, "%s\n", nvmet_addr_family[i].name);
55a07b4970SChristoph Hellwig 	}
567e764179SChaitanya Kulkarni 
577e764179SChaitanya Kulkarni 	return sprintf(page, "\n");
58a07b4970SChristoph Hellwig }
59a07b4970SChristoph Hellwig 
60a07b4970SChristoph Hellwig static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
61a07b4970SChristoph Hellwig 		const char *page, size_t count)
62a07b4970SChristoph Hellwig {
63a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
647e764179SChaitanya Kulkarni 	int i;
65a07b4970SChristoph Hellwig 
66a07b4970SChristoph Hellwig 	if (port->enabled) {
67a07b4970SChristoph Hellwig 		pr_err("Cannot modify address while enabled\n");
68a07b4970SChristoph Hellwig 		pr_err("Disable the address before modifying\n");
69a07b4970SChristoph Hellwig 		return -EACCES;
70a07b4970SChristoph Hellwig 	}
71a07b4970SChristoph Hellwig 
727e764179SChaitanya Kulkarni 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
737e764179SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_addr_family[i].name))
747e764179SChaitanya Kulkarni 			goto found;
75a07b4970SChristoph Hellwig 	}
76a07b4970SChristoph Hellwig 
777e764179SChaitanya Kulkarni 	pr_err("Invalid value '%s' for adrfam\n", page);
787e764179SChaitanya Kulkarni 	return -EINVAL;
797e764179SChaitanya Kulkarni 
807e764179SChaitanya Kulkarni found:
817e764179SChaitanya Kulkarni 	port->disc_addr.adrfam = i;
82a07b4970SChristoph Hellwig 	return count;
83a07b4970SChristoph Hellwig }
84a07b4970SChristoph Hellwig 
85a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_adrfam);
86a07b4970SChristoph Hellwig 
87a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_show(struct config_item *item,
88a07b4970SChristoph Hellwig 		char *page)
89a07b4970SChristoph Hellwig {
90a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
91a07b4970SChristoph Hellwig 
92a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n",
93a07b4970SChristoph Hellwig 			le16_to_cpu(port->disc_addr.portid));
94a07b4970SChristoph Hellwig }
95a07b4970SChristoph Hellwig 
96a07b4970SChristoph Hellwig static ssize_t nvmet_addr_portid_store(struct config_item *item,
97a07b4970SChristoph Hellwig 		const char *page, size_t count)
98a07b4970SChristoph Hellwig {
99a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
100a07b4970SChristoph Hellwig 	u16 portid = 0;
101a07b4970SChristoph Hellwig 
102a07b4970SChristoph Hellwig 	if (kstrtou16(page, 0, &portid)) {
103a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for portid\n", page);
104a07b4970SChristoph Hellwig 		return -EINVAL;
105a07b4970SChristoph Hellwig 	}
106a07b4970SChristoph Hellwig 
107a07b4970SChristoph Hellwig 	if (port->enabled) {
108a07b4970SChristoph Hellwig 		pr_err("Cannot modify address while enabled\n");
109a07b4970SChristoph Hellwig 		pr_err("Disable the address before modifying\n");
110a07b4970SChristoph Hellwig 		return -EACCES;
111a07b4970SChristoph Hellwig 	}
112a07b4970SChristoph Hellwig 	port->disc_addr.portid = cpu_to_le16(portid);
113a07b4970SChristoph Hellwig 	return count;
114a07b4970SChristoph Hellwig }
115a07b4970SChristoph Hellwig 
116a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_portid);
117a07b4970SChristoph Hellwig 
118a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_show(struct config_item *item,
119a07b4970SChristoph Hellwig 		char *page)
120a07b4970SChristoph Hellwig {
121a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
122a07b4970SChristoph Hellwig 
123a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n",
124a07b4970SChristoph Hellwig 			port->disc_addr.traddr);
125a07b4970SChristoph Hellwig }
126a07b4970SChristoph Hellwig 
127a07b4970SChristoph Hellwig static ssize_t nvmet_addr_traddr_store(struct config_item *item,
128a07b4970SChristoph Hellwig 		const char *page, size_t count)
129a07b4970SChristoph Hellwig {
130a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
131a07b4970SChristoph Hellwig 
132a07b4970SChristoph Hellwig 	if (count > NVMF_TRADDR_SIZE) {
133a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for traddr\n", page);
134a07b4970SChristoph Hellwig 		return -EINVAL;
135a07b4970SChristoph Hellwig 	}
136a07b4970SChristoph Hellwig 
137a07b4970SChristoph Hellwig 	if (port->enabled) {
138a07b4970SChristoph Hellwig 		pr_err("Cannot modify address while enabled\n");
139a07b4970SChristoph Hellwig 		pr_err("Disable the address before modifying\n");
140a07b4970SChristoph Hellwig 		return -EACCES;
141a07b4970SChristoph Hellwig 	}
1429ba2a5cbSSagi Grimberg 
1439ba2a5cbSSagi Grimberg 	if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
1449ba2a5cbSSagi Grimberg 		return -EINVAL;
1459ba2a5cbSSagi Grimberg 	return count;
146a07b4970SChristoph Hellwig }
147a07b4970SChristoph Hellwig 
148a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_traddr);
149a07b4970SChristoph Hellwig 
150a07b4970SChristoph Hellwig static ssize_t nvmet_addr_treq_show(struct config_item *item,
151a07b4970SChristoph Hellwig 		char *page)
152a07b4970SChristoph Hellwig {
1530445e1b5SSagi Grimberg 	switch (to_nvmet_port(item)->disc_addr.treq &
1540445e1b5SSagi Grimberg 		NVME_TREQ_SECURE_CHANNEL_MASK) {
155a07b4970SChristoph Hellwig 	case NVMF_TREQ_NOT_SPECIFIED:
156a07b4970SChristoph Hellwig 		return sprintf(page, "not specified\n");
157a07b4970SChristoph Hellwig 	case NVMF_TREQ_REQUIRED:
158a07b4970SChristoph Hellwig 		return sprintf(page, "required\n");
159a07b4970SChristoph Hellwig 	case NVMF_TREQ_NOT_REQUIRED:
160a07b4970SChristoph Hellwig 		return sprintf(page, "not required\n");
161a07b4970SChristoph Hellwig 	default:
162a07b4970SChristoph Hellwig 		return sprintf(page, "\n");
163a07b4970SChristoph Hellwig 	}
164a07b4970SChristoph Hellwig }
165a07b4970SChristoph Hellwig 
166a07b4970SChristoph Hellwig static ssize_t nvmet_addr_treq_store(struct config_item *item,
167a07b4970SChristoph Hellwig 		const char *page, size_t count)
168a07b4970SChristoph Hellwig {
169a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1700445e1b5SSagi Grimberg 	u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK;
171a07b4970SChristoph Hellwig 
172a07b4970SChristoph Hellwig 	if (port->enabled) {
173a07b4970SChristoph Hellwig 		pr_err("Cannot modify address while enabled\n");
174a07b4970SChristoph Hellwig 		pr_err("Disable the address before modifying\n");
175a07b4970SChristoph Hellwig 		return -EACCES;
176a07b4970SChristoph Hellwig 	}
177a07b4970SChristoph Hellwig 
178a07b4970SChristoph Hellwig 	if (sysfs_streq(page, "not specified")) {
1790445e1b5SSagi Grimberg 		treq |= NVMF_TREQ_NOT_SPECIFIED;
180a07b4970SChristoph Hellwig 	} else if (sysfs_streq(page, "required")) {
1810445e1b5SSagi Grimberg 		treq |= NVMF_TREQ_REQUIRED;
182a07b4970SChristoph Hellwig 	} else if (sysfs_streq(page, "not required")) {
1830445e1b5SSagi Grimberg 		treq |= NVMF_TREQ_NOT_REQUIRED;
184a07b4970SChristoph Hellwig 	} else {
185a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for treq\n", page);
186a07b4970SChristoph Hellwig 		return -EINVAL;
187a07b4970SChristoph Hellwig 	}
1880445e1b5SSagi Grimberg 	port->disc_addr.treq = treq;
189a07b4970SChristoph Hellwig 
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 
200a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%s\n",
201a07b4970SChristoph Hellwig 			port->disc_addr.trsvcid);
202a07b4970SChristoph Hellwig }
203a07b4970SChristoph Hellwig 
204a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
205a07b4970SChristoph Hellwig 		const char *page, size_t count)
206a07b4970SChristoph Hellwig {
207a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
208a07b4970SChristoph Hellwig 
209a07b4970SChristoph Hellwig 	if (count > NVMF_TRSVCID_SIZE) {
210a07b4970SChristoph Hellwig 		pr_err("Invalid value '%s' for trsvcid\n", page);
211a07b4970SChristoph Hellwig 		return -EINVAL;
212a07b4970SChristoph Hellwig 	}
213a07b4970SChristoph Hellwig 	if (port->enabled) {
214a07b4970SChristoph Hellwig 		pr_err("Cannot modify address while enabled\n");
215a07b4970SChristoph Hellwig 		pr_err("Disable the address before modifying\n");
216a07b4970SChristoph Hellwig 		return -EACCES;
217a07b4970SChristoph Hellwig 	}
2189ba2a5cbSSagi Grimberg 
2199ba2a5cbSSagi Grimberg 	if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
2209ba2a5cbSSagi Grimberg 		return -EINVAL;
2219ba2a5cbSSagi Grimberg 	return count;
222a07b4970SChristoph Hellwig }
223a07b4970SChristoph Hellwig 
224a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trsvcid);
225a07b4970SChristoph Hellwig 
2260d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
2270d5ee2b2SSteve Wise 		char *page)
2280d5ee2b2SSteve Wise {
2290d5ee2b2SSteve Wise 	struct nvmet_port *port = to_nvmet_port(item);
2300d5ee2b2SSteve Wise 
2310d5ee2b2SSteve Wise 	return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
2320d5ee2b2SSteve Wise }
2330d5ee2b2SSteve Wise 
2340d5ee2b2SSteve Wise static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
2350d5ee2b2SSteve Wise 		const char *page, size_t count)
2360d5ee2b2SSteve Wise {
2370d5ee2b2SSteve Wise 	struct nvmet_port *port = to_nvmet_port(item);
2380d5ee2b2SSteve Wise 	int ret;
2390d5ee2b2SSteve Wise 
2400d5ee2b2SSteve Wise 	if (port->enabled) {
2410d5ee2b2SSteve Wise 		pr_err("Cannot modify inline_data_size while port enabled\n");
2420d5ee2b2SSteve Wise 		pr_err("Disable the port before modifying\n");
2430d5ee2b2SSteve Wise 		return -EACCES;
2440d5ee2b2SSteve Wise 	}
2450d5ee2b2SSteve Wise 	ret = kstrtoint(page, 0, &port->inline_data_size);
2460d5ee2b2SSteve Wise 	if (ret) {
2470d5ee2b2SSteve Wise 		pr_err("Invalid value '%s' for inline_data_size\n", page);
2480d5ee2b2SSteve Wise 		return -EINVAL;
2490d5ee2b2SSteve Wise 	}
2500d5ee2b2SSteve Wise 	return count;
2510d5ee2b2SSteve Wise }
2520d5ee2b2SSteve Wise 
2530d5ee2b2SSteve Wise CONFIGFS_ATTR(nvmet_, param_inline_data_size);
2540d5ee2b2SSteve Wise 
255a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_show(struct config_item *item,
256a07b4970SChristoph Hellwig 		char *page)
257a07b4970SChristoph Hellwig {
258a5d18612SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
259a5d18612SChristoph Hellwig 	int i;
260a5d18612SChristoph Hellwig 
26145e2f3c2SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
26245e2f3c2SChaitanya Kulkarni 		if (port->disc_addr.trtype == nvmet_transport[i].type)
26345e2f3c2SChaitanya Kulkarni 			return sprintf(page, "%s\n", nvmet_transport[i].name);
264a07b4970SChristoph Hellwig 	}
265a5d18612SChristoph Hellwig 
266a5d18612SChristoph Hellwig 	return sprintf(page, "\n");
267a07b4970SChristoph Hellwig }
268a07b4970SChristoph Hellwig 
269a07b4970SChristoph Hellwig static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
270a07b4970SChristoph Hellwig {
271a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
272a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
273a07b4970SChristoph Hellwig 	port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
274a07b4970SChristoph Hellwig }
275a07b4970SChristoph Hellwig 
276a07b4970SChristoph Hellwig static ssize_t nvmet_addr_trtype_store(struct config_item *item,
277a07b4970SChristoph Hellwig 		const char *page, size_t count)
278a07b4970SChristoph Hellwig {
279a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
280a5d18612SChristoph Hellwig 	int i;
281a07b4970SChristoph Hellwig 
282a07b4970SChristoph Hellwig 	if (port->enabled) {
283a07b4970SChristoph Hellwig 		pr_err("Cannot modify address while enabled\n");
284a07b4970SChristoph Hellwig 		pr_err("Disable the address before modifying\n");
285a07b4970SChristoph Hellwig 		return -EACCES;
286a07b4970SChristoph Hellwig 	}
287a07b4970SChristoph Hellwig 
28845e2f3c2SChaitanya Kulkarni 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
28945e2f3c2SChaitanya Kulkarni 		if (sysfs_streq(page, nvmet_transport[i].name))
290a5d18612SChristoph Hellwig 			goto found;
291a07b4970SChristoph Hellwig 	}
292a07b4970SChristoph Hellwig 
293a5d18612SChristoph Hellwig 	pr_err("Invalid value '%s' for trtype\n", page);
294a5d18612SChristoph Hellwig 	return -EINVAL;
2957e764179SChaitanya Kulkarni 
296a5d18612SChristoph Hellwig found:
297a5d18612SChristoph Hellwig 	memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
29845e2f3c2SChaitanya Kulkarni 	port->disc_addr.trtype = nvmet_transport[i].type;
299a5d18612SChristoph Hellwig 	if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
300a5d18612SChristoph Hellwig 		nvmet_port_init_tsas_rdma(port);
301a07b4970SChristoph Hellwig 	return count;
302a07b4970SChristoph Hellwig }
303a07b4970SChristoph Hellwig 
304a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_, addr_trtype);
305a07b4970SChristoph Hellwig 
306a07b4970SChristoph Hellwig /*
307a07b4970SChristoph Hellwig  * Namespace structures & file operation functions below
308a07b4970SChristoph Hellwig  */
309a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
310a07b4970SChristoph Hellwig {
311a07b4970SChristoph Hellwig 	return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
312a07b4970SChristoph Hellwig }
313a07b4970SChristoph Hellwig 
314a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_path_store(struct config_item *item,
315a07b4970SChristoph Hellwig 		const char *page, size_t count)
316a07b4970SChristoph Hellwig {
317a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
318a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = ns->subsys;
3195613d312SHannes Reinecke 	size_t len;
320a07b4970SChristoph Hellwig 	int ret;
321a07b4970SChristoph Hellwig 
322a07b4970SChristoph Hellwig 	mutex_lock(&subsys->lock);
323a07b4970SChristoph Hellwig 	ret = -EBUSY;
324e4fcf07cSSolganik Alexander 	if (ns->enabled)
325a07b4970SChristoph Hellwig 		goto out_unlock;
326a07b4970SChristoph Hellwig 
3275613d312SHannes Reinecke 	ret = -EINVAL;
3285613d312SHannes Reinecke 	len = strcspn(page, "\n");
3295613d312SHannes Reinecke 	if (!len)
3305613d312SHannes Reinecke 		goto out_unlock;
331a07b4970SChristoph Hellwig 
3325613d312SHannes Reinecke 	kfree(ns->device_path);
333a07b4970SChristoph Hellwig 	ret = -ENOMEM;
3345613d312SHannes Reinecke 	ns->device_path = kstrndup(page, len, GFP_KERNEL);
335a07b4970SChristoph Hellwig 	if (!ns->device_path)
336a07b4970SChristoph Hellwig 		goto out_unlock;
337a07b4970SChristoph Hellwig 
338a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
339a07b4970SChristoph Hellwig 	return count;
340a07b4970SChristoph Hellwig 
341a07b4970SChristoph Hellwig out_unlock:
342a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
343a07b4970SChristoph Hellwig 	return ret;
344a07b4970SChristoph Hellwig }
345a07b4970SChristoph Hellwig 
346a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_path);
347a07b4970SChristoph Hellwig 
348c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA
349c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
350c6925093SLogan Gunthorpe {
351c6925093SLogan Gunthorpe 	struct nvmet_ns *ns = to_nvmet_ns(item);
352c6925093SLogan Gunthorpe 
353c6925093SLogan Gunthorpe 	return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
354c6925093SLogan Gunthorpe }
355c6925093SLogan Gunthorpe 
356c6925093SLogan Gunthorpe static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
357c6925093SLogan Gunthorpe 		const char *page, size_t count)
358c6925093SLogan Gunthorpe {
359c6925093SLogan Gunthorpe 	struct nvmet_ns *ns = to_nvmet_ns(item);
360c6925093SLogan Gunthorpe 	struct pci_dev *p2p_dev = NULL;
361c6925093SLogan Gunthorpe 	bool use_p2pmem;
362c6925093SLogan Gunthorpe 	int ret = count;
363c6925093SLogan Gunthorpe 	int error;
364c6925093SLogan Gunthorpe 
365c6925093SLogan Gunthorpe 	mutex_lock(&ns->subsys->lock);
366c6925093SLogan Gunthorpe 	if (ns->enabled) {
367c6925093SLogan Gunthorpe 		ret = -EBUSY;
368c6925093SLogan Gunthorpe 		goto out_unlock;
369c6925093SLogan Gunthorpe 	}
370c6925093SLogan Gunthorpe 
371c6925093SLogan Gunthorpe 	error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
372c6925093SLogan Gunthorpe 	if (error) {
373c6925093SLogan Gunthorpe 		ret = error;
374c6925093SLogan Gunthorpe 		goto out_unlock;
375c6925093SLogan Gunthorpe 	}
376c6925093SLogan Gunthorpe 
377c6925093SLogan Gunthorpe 	ns->use_p2pmem = use_p2pmem;
378c6925093SLogan Gunthorpe 	pci_dev_put(ns->p2p_dev);
379c6925093SLogan Gunthorpe 	ns->p2p_dev = p2p_dev;
380c6925093SLogan Gunthorpe 
381c6925093SLogan Gunthorpe out_unlock:
382c6925093SLogan Gunthorpe 	mutex_unlock(&ns->subsys->lock);
383c6925093SLogan Gunthorpe 
384c6925093SLogan Gunthorpe 	return ret;
385c6925093SLogan Gunthorpe }
386c6925093SLogan Gunthorpe 
387c6925093SLogan Gunthorpe CONFIGFS_ATTR(nvmet_ns_, p2pmem);
388c6925093SLogan Gunthorpe #endif /* CONFIG_PCI_P2PDMA */
389c6925093SLogan Gunthorpe 
390430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
391430c7befSJohannes Thumshirn {
392430c7befSJohannes Thumshirn 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
393430c7befSJohannes Thumshirn }
394430c7befSJohannes Thumshirn 
395430c7befSJohannes Thumshirn static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
396430c7befSJohannes Thumshirn 					  const char *page, size_t count)
397430c7befSJohannes Thumshirn {
398430c7befSJohannes Thumshirn 	struct nvmet_ns *ns = to_nvmet_ns(item);
399430c7befSJohannes Thumshirn 	struct nvmet_subsys *subsys = ns->subsys;
400430c7befSJohannes Thumshirn 	int ret = 0;
401430c7befSJohannes Thumshirn 
402430c7befSJohannes Thumshirn 	mutex_lock(&subsys->lock);
403430c7befSJohannes Thumshirn 	if (ns->enabled) {
404430c7befSJohannes Thumshirn 		ret = -EBUSY;
405430c7befSJohannes Thumshirn 		goto out_unlock;
406430c7befSJohannes Thumshirn 	}
407430c7befSJohannes Thumshirn 
408430c7befSJohannes Thumshirn 	if (uuid_parse(page, &ns->uuid))
409430c7befSJohannes Thumshirn 		ret = -EINVAL;
410430c7befSJohannes Thumshirn 
411430c7befSJohannes Thumshirn out_unlock:
412430c7befSJohannes Thumshirn 	mutex_unlock(&subsys->lock);
413430c7befSJohannes Thumshirn 	return ret ? ret : count;
414430c7befSJohannes Thumshirn }
415430c7befSJohannes Thumshirn 
416f871749aSMax Gurtovoy CONFIGFS_ATTR(nvmet_ns_, device_uuid);
417f871749aSMax Gurtovoy 
418a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
419a07b4970SChristoph Hellwig {
420a07b4970SChristoph Hellwig 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
421a07b4970SChristoph Hellwig }
422a07b4970SChristoph Hellwig 
423a07b4970SChristoph Hellwig static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
424a07b4970SChristoph Hellwig 		const char *page, size_t count)
425a07b4970SChristoph Hellwig {
426a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
427a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = ns->subsys;
428a07b4970SChristoph Hellwig 	u8 nguid[16];
429a07b4970SChristoph Hellwig 	const char *p = page;
430a07b4970SChristoph Hellwig 	int i;
431a07b4970SChristoph Hellwig 	int ret = 0;
432a07b4970SChristoph Hellwig 
433a07b4970SChristoph Hellwig 	mutex_lock(&subsys->lock);
434e4fcf07cSSolganik Alexander 	if (ns->enabled) {
435a07b4970SChristoph Hellwig 		ret = -EBUSY;
436a07b4970SChristoph Hellwig 		goto out_unlock;
437a07b4970SChristoph Hellwig 	}
438a07b4970SChristoph Hellwig 
439a07b4970SChristoph Hellwig 	for (i = 0; i < 16; i++) {
440a07b4970SChristoph Hellwig 		if (p + 2 > page + count) {
441a07b4970SChristoph Hellwig 			ret = -EINVAL;
442a07b4970SChristoph Hellwig 			goto out_unlock;
443a07b4970SChristoph Hellwig 		}
444a07b4970SChristoph Hellwig 		if (!isxdigit(p[0]) || !isxdigit(p[1])) {
445a07b4970SChristoph Hellwig 			ret = -EINVAL;
446a07b4970SChristoph Hellwig 			goto out_unlock;
447a07b4970SChristoph Hellwig 		}
448a07b4970SChristoph Hellwig 
449a07b4970SChristoph Hellwig 		nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
450a07b4970SChristoph Hellwig 		p += 2;
451a07b4970SChristoph Hellwig 
452a07b4970SChristoph Hellwig 		if (*p == '-' || *p == ':')
453a07b4970SChristoph Hellwig 			p++;
454a07b4970SChristoph Hellwig 	}
455a07b4970SChristoph Hellwig 
456a07b4970SChristoph Hellwig 	memcpy(&ns->nguid, nguid, sizeof(nguid));
457a07b4970SChristoph Hellwig out_unlock:
458a07b4970SChristoph Hellwig 	mutex_unlock(&subsys->lock);
459a07b4970SChristoph Hellwig 	return ret ? ret : count;
460a07b4970SChristoph Hellwig }
461a07b4970SChristoph Hellwig 
462a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, device_nguid);
463a07b4970SChristoph Hellwig 
46462ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
46562ac0d32SChristoph Hellwig {
46662ac0d32SChristoph Hellwig 	return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
46762ac0d32SChristoph Hellwig }
46862ac0d32SChristoph Hellwig 
46962ac0d32SChristoph Hellwig static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
47062ac0d32SChristoph Hellwig 		const char *page, size_t count)
47162ac0d32SChristoph Hellwig {
47262ac0d32SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
47362ac0d32SChristoph Hellwig 	u32 oldgrpid, newgrpid;
47462ac0d32SChristoph Hellwig 	int ret;
47562ac0d32SChristoph Hellwig 
47662ac0d32SChristoph Hellwig 	ret = kstrtou32(page, 0, &newgrpid);
47762ac0d32SChristoph Hellwig 	if (ret)
47862ac0d32SChristoph Hellwig 		return ret;
47962ac0d32SChristoph Hellwig 
48062ac0d32SChristoph Hellwig 	if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
48162ac0d32SChristoph Hellwig 		return -EINVAL;
48262ac0d32SChristoph Hellwig 
48362ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
48462ac0d32SChristoph Hellwig 	oldgrpid = ns->anagrpid;
48562ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[newgrpid]++;
48662ac0d32SChristoph Hellwig 	ns->anagrpid = newgrpid;
48762ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[oldgrpid]--;
48862ac0d32SChristoph Hellwig 	nvmet_ana_chgcnt++;
48962ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
49062ac0d32SChristoph Hellwig 
49162ac0d32SChristoph Hellwig 	nvmet_send_ana_event(ns->subsys, NULL);
49262ac0d32SChristoph Hellwig 	return count;
49362ac0d32SChristoph Hellwig }
49462ac0d32SChristoph Hellwig 
49562ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
49662ac0d32SChristoph Hellwig 
497a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
498a07b4970SChristoph Hellwig {
499e4fcf07cSSolganik Alexander 	return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
500a07b4970SChristoph Hellwig }
501a07b4970SChristoph Hellwig 
502a07b4970SChristoph Hellwig static ssize_t nvmet_ns_enable_store(struct config_item *item,
503a07b4970SChristoph Hellwig 		const char *page, size_t count)
504a07b4970SChristoph Hellwig {
505a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
506a07b4970SChristoph Hellwig 	bool enable;
507a07b4970SChristoph Hellwig 	int ret = 0;
508a07b4970SChristoph Hellwig 
509a07b4970SChristoph Hellwig 	if (strtobool(page, &enable))
510a07b4970SChristoph Hellwig 		return -EINVAL;
511a07b4970SChristoph Hellwig 
512a07b4970SChristoph Hellwig 	if (enable)
513a07b4970SChristoph Hellwig 		ret = nvmet_ns_enable(ns);
514a07b4970SChristoph Hellwig 	else
515a07b4970SChristoph Hellwig 		nvmet_ns_disable(ns);
516a07b4970SChristoph Hellwig 
517a07b4970SChristoph Hellwig 	return ret ? ret : count;
518a07b4970SChristoph Hellwig }
519a07b4970SChristoph Hellwig 
520a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_ns_, enable);
521a07b4970SChristoph Hellwig 
52255eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
52355eb942eSChaitanya Kulkarni {
52455eb942eSChaitanya Kulkarni 	return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
52555eb942eSChaitanya Kulkarni }
52655eb942eSChaitanya Kulkarni 
52755eb942eSChaitanya Kulkarni static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
52855eb942eSChaitanya Kulkarni 		const char *page, size_t count)
52955eb942eSChaitanya Kulkarni {
53055eb942eSChaitanya Kulkarni 	struct nvmet_ns *ns = to_nvmet_ns(item);
53155eb942eSChaitanya Kulkarni 	bool val;
53255eb942eSChaitanya Kulkarni 
53355eb942eSChaitanya Kulkarni 	if (strtobool(page, &val))
53455eb942eSChaitanya Kulkarni 		return -EINVAL;
53555eb942eSChaitanya Kulkarni 
53655eb942eSChaitanya Kulkarni 	mutex_lock(&ns->subsys->lock);
53755eb942eSChaitanya Kulkarni 	if (ns->enabled) {
53855eb942eSChaitanya Kulkarni 		pr_err("disable ns before setting buffered_io value.\n");
53955eb942eSChaitanya Kulkarni 		mutex_unlock(&ns->subsys->lock);
54055eb942eSChaitanya Kulkarni 		return -EINVAL;
54155eb942eSChaitanya Kulkarni 	}
54255eb942eSChaitanya Kulkarni 
54355eb942eSChaitanya Kulkarni 	ns->buffered_io = val;
54455eb942eSChaitanya Kulkarni 	mutex_unlock(&ns->subsys->lock);
54555eb942eSChaitanya Kulkarni 	return count;
54655eb942eSChaitanya Kulkarni }
54755eb942eSChaitanya Kulkarni 
54855eb942eSChaitanya Kulkarni CONFIGFS_ATTR(nvmet_ns_, buffered_io);
54955eb942eSChaitanya Kulkarni 
550a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_ns_attrs[] = {
551a07b4970SChristoph Hellwig 	&nvmet_ns_attr_device_path,
552a07b4970SChristoph Hellwig 	&nvmet_ns_attr_device_nguid,
553430c7befSJohannes Thumshirn 	&nvmet_ns_attr_device_uuid,
55462ac0d32SChristoph Hellwig 	&nvmet_ns_attr_ana_grpid,
555a07b4970SChristoph Hellwig 	&nvmet_ns_attr_enable,
55655eb942eSChaitanya Kulkarni 	&nvmet_ns_attr_buffered_io,
557c6925093SLogan Gunthorpe #ifdef CONFIG_PCI_P2PDMA
558c6925093SLogan Gunthorpe 	&nvmet_ns_attr_p2pmem,
559c6925093SLogan Gunthorpe #endif
560a07b4970SChristoph Hellwig 	NULL,
561a07b4970SChristoph Hellwig };
562a07b4970SChristoph Hellwig 
563a07b4970SChristoph Hellwig static void nvmet_ns_release(struct config_item *item)
564a07b4970SChristoph Hellwig {
565a07b4970SChristoph Hellwig 	struct nvmet_ns *ns = to_nvmet_ns(item);
566a07b4970SChristoph Hellwig 
567a07b4970SChristoph Hellwig 	nvmet_ns_free(ns);
568a07b4970SChristoph Hellwig }
569a07b4970SChristoph Hellwig 
570a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_ns_item_ops = {
571a07b4970SChristoph Hellwig 	.release		= nvmet_ns_release,
572a07b4970SChristoph Hellwig };
573a07b4970SChristoph Hellwig 
57466603a31SBhumika Goyal static const struct config_item_type nvmet_ns_type = {
575a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_ns_item_ops,
576a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_ns_attrs,
577a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
578a07b4970SChristoph Hellwig };
579a07b4970SChristoph Hellwig 
580a07b4970SChristoph Hellwig static struct config_group *nvmet_ns_make(struct config_group *group,
581a07b4970SChristoph Hellwig 		const char *name)
582a07b4970SChristoph Hellwig {
583a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
584a07b4970SChristoph Hellwig 	struct nvmet_ns *ns;
585a07b4970SChristoph Hellwig 	int ret;
586a07b4970SChristoph Hellwig 	u32 nsid;
587a07b4970SChristoph Hellwig 
588a07b4970SChristoph Hellwig 	ret = kstrtou32(name, 0, &nsid);
589a07b4970SChristoph Hellwig 	if (ret)
590a07b4970SChristoph Hellwig 		goto out;
591a07b4970SChristoph Hellwig 
592a07b4970SChristoph Hellwig 	ret = -EINVAL;
5935ba89503SMikhail Skorzhinskii 	if (nsid == 0 || nsid == NVME_NSID_ALL) {
5945ba89503SMikhail Skorzhinskii 		pr_err("invalid nsid %#x", nsid);
595a07b4970SChristoph Hellwig 		goto out;
5965ba89503SMikhail Skorzhinskii 	}
597a07b4970SChristoph Hellwig 
598a07b4970SChristoph Hellwig 	ret = -ENOMEM;
599a07b4970SChristoph Hellwig 	ns = nvmet_ns_alloc(subsys, nsid);
600a07b4970SChristoph Hellwig 	if (!ns)
601a07b4970SChristoph Hellwig 		goto out;
602a07b4970SChristoph Hellwig 	config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
603a07b4970SChristoph Hellwig 
604a07b4970SChristoph Hellwig 	pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
605a07b4970SChristoph Hellwig 
606a07b4970SChristoph Hellwig 	return &ns->group;
607a07b4970SChristoph Hellwig out:
608a07b4970SChristoph Hellwig 	return ERR_PTR(ret);
609a07b4970SChristoph Hellwig }
610a07b4970SChristoph Hellwig 
611a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_namespaces_group_ops = {
612a07b4970SChristoph Hellwig 	.make_group		= nvmet_ns_make,
613a07b4970SChristoph Hellwig };
614a07b4970SChristoph Hellwig 
61566603a31SBhumika Goyal static const struct config_item_type nvmet_namespaces_type = {
616a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_namespaces_group_ops,
617a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
618a07b4970SChristoph Hellwig };
619a07b4970SChristoph Hellwig 
620a07b4970SChristoph Hellwig static int nvmet_port_subsys_allow_link(struct config_item *parent,
621a07b4970SChristoph Hellwig 		struct config_item *target)
622a07b4970SChristoph Hellwig {
623a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
624a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys;
625a07b4970SChristoph Hellwig 	struct nvmet_subsys_link *link, *p;
626a07b4970SChristoph Hellwig 	int ret;
627a07b4970SChristoph Hellwig 
628a07b4970SChristoph Hellwig 	if (target->ci_type != &nvmet_subsys_type) {
629a07b4970SChristoph Hellwig 		pr_err("can only link subsystems into the subsystems dir.!\n");
630a07b4970SChristoph Hellwig 		return -EINVAL;
631a07b4970SChristoph Hellwig 	}
632a07b4970SChristoph Hellwig 	subsys = to_subsys(target);
633a07b4970SChristoph Hellwig 	link = kmalloc(sizeof(*link), GFP_KERNEL);
634a07b4970SChristoph Hellwig 	if (!link)
635a07b4970SChristoph Hellwig 		return -ENOMEM;
636a07b4970SChristoph Hellwig 	link->subsys = subsys;
637a07b4970SChristoph Hellwig 
638a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
639a07b4970SChristoph Hellwig 	ret = -EEXIST;
640a07b4970SChristoph Hellwig 	list_for_each_entry(p, &port->subsystems, entry) {
641a07b4970SChristoph Hellwig 		if (p->subsys == subsys)
642a07b4970SChristoph Hellwig 			goto out_free_link;
643a07b4970SChristoph Hellwig 	}
644a07b4970SChristoph Hellwig 
645a07b4970SChristoph Hellwig 	if (list_empty(&port->subsystems)) {
646a07b4970SChristoph Hellwig 		ret = nvmet_enable_port(port);
647a07b4970SChristoph Hellwig 		if (ret)
648a07b4970SChristoph Hellwig 			goto out_free_link;
649a07b4970SChristoph Hellwig 	}
650a07b4970SChristoph Hellwig 
651a07b4970SChristoph Hellwig 	list_add_tail(&link->entry, &port->subsystems);
652b662a078SJay Sternberg 	nvmet_port_disc_changed(port, subsys);
653b662a078SJay Sternberg 
654a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
655a07b4970SChristoph Hellwig 	return 0;
656a07b4970SChristoph Hellwig 
657a07b4970SChristoph Hellwig out_free_link:
658a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
659a07b4970SChristoph Hellwig 	kfree(link);
660a07b4970SChristoph Hellwig 	return ret;
661a07b4970SChristoph Hellwig }
662a07b4970SChristoph Hellwig 
663e16769d4SAndrzej Pietrasiewicz static void nvmet_port_subsys_drop_link(struct config_item *parent,
664a07b4970SChristoph Hellwig 		struct config_item *target)
665a07b4970SChristoph Hellwig {
666a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
667a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(target);
668a07b4970SChristoph Hellwig 	struct nvmet_subsys_link *p;
669a07b4970SChristoph Hellwig 
670a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
671a07b4970SChristoph Hellwig 	list_for_each_entry(p, &port->subsystems, entry) {
672a07b4970SChristoph Hellwig 		if (p->subsys == subsys)
673a07b4970SChristoph Hellwig 			goto found;
674a07b4970SChristoph Hellwig 	}
675a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
676e16769d4SAndrzej Pietrasiewicz 	return;
677a07b4970SChristoph Hellwig 
678a07b4970SChristoph Hellwig found:
679a07b4970SChristoph Hellwig 	list_del(&p->entry);
6803aed8673SLogan Gunthorpe 	nvmet_port_del_ctrls(port, subsys);
681b662a078SJay Sternberg 	nvmet_port_disc_changed(port, subsys);
682b662a078SJay Sternberg 
683a07b4970SChristoph Hellwig 	if (list_empty(&port->subsystems))
684a07b4970SChristoph Hellwig 		nvmet_disable_port(port);
685a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
686a07b4970SChristoph Hellwig 	kfree(p);
687a07b4970SChristoph Hellwig }
688a07b4970SChristoph Hellwig 
689a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_subsys_item_ops = {
690a07b4970SChristoph Hellwig 	.allow_link		= nvmet_port_subsys_allow_link,
691a07b4970SChristoph Hellwig 	.drop_link		= nvmet_port_subsys_drop_link,
692a07b4970SChristoph Hellwig };
693a07b4970SChristoph Hellwig 
69466603a31SBhumika Goyal static const struct config_item_type nvmet_port_subsys_type = {
695a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_port_subsys_item_ops,
696a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
697a07b4970SChristoph Hellwig };
698a07b4970SChristoph Hellwig 
699a07b4970SChristoph Hellwig static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
700a07b4970SChristoph Hellwig 		struct config_item *target)
701a07b4970SChristoph Hellwig {
702a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
703a07b4970SChristoph Hellwig 	struct nvmet_host *host;
704a07b4970SChristoph Hellwig 	struct nvmet_host_link *link, *p;
705a07b4970SChristoph Hellwig 	int ret;
706a07b4970SChristoph Hellwig 
707a07b4970SChristoph Hellwig 	if (target->ci_type != &nvmet_host_type) {
708a07b4970SChristoph Hellwig 		pr_err("can only link hosts into the allowed_hosts directory!\n");
709a07b4970SChristoph Hellwig 		return -EINVAL;
710a07b4970SChristoph Hellwig 	}
711a07b4970SChristoph Hellwig 
712a07b4970SChristoph Hellwig 	host = to_host(target);
713a07b4970SChristoph Hellwig 	link = kmalloc(sizeof(*link), GFP_KERNEL);
714a07b4970SChristoph Hellwig 	if (!link)
715a07b4970SChristoph Hellwig 		return -ENOMEM;
716a07b4970SChristoph Hellwig 	link->host = host;
717a07b4970SChristoph Hellwig 
718a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
719a07b4970SChristoph Hellwig 	ret = -EINVAL;
720a07b4970SChristoph Hellwig 	if (subsys->allow_any_host) {
721a07b4970SChristoph Hellwig 		pr_err("can't add hosts when allow_any_host is set!\n");
722a07b4970SChristoph Hellwig 		goto out_free_link;
723a07b4970SChristoph Hellwig 	}
724a07b4970SChristoph Hellwig 
725a07b4970SChristoph Hellwig 	ret = -EEXIST;
726a07b4970SChristoph Hellwig 	list_for_each_entry(p, &subsys->hosts, entry) {
727a07b4970SChristoph Hellwig 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
728a07b4970SChristoph Hellwig 			goto out_free_link;
729a07b4970SChristoph Hellwig 	}
730a07b4970SChristoph Hellwig 	list_add_tail(&link->entry, &subsys->hosts);
731b662a078SJay Sternberg 	nvmet_subsys_disc_changed(subsys, host);
732b662a078SJay Sternberg 
733a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
734a07b4970SChristoph Hellwig 	return 0;
735a07b4970SChristoph Hellwig out_free_link:
736a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
737a07b4970SChristoph Hellwig 	kfree(link);
738a07b4970SChristoph Hellwig 	return ret;
739a07b4970SChristoph Hellwig }
740a07b4970SChristoph Hellwig 
741e16769d4SAndrzej Pietrasiewicz static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
742a07b4970SChristoph Hellwig 		struct config_item *target)
743a07b4970SChristoph Hellwig {
744a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
745a07b4970SChristoph Hellwig 	struct nvmet_host *host = to_host(target);
746a07b4970SChristoph Hellwig 	struct nvmet_host_link *p;
747a07b4970SChristoph Hellwig 
748a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
749a07b4970SChristoph Hellwig 	list_for_each_entry(p, &subsys->hosts, entry) {
750a07b4970SChristoph Hellwig 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
751a07b4970SChristoph Hellwig 			goto found;
752a07b4970SChristoph Hellwig 	}
753a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
754e16769d4SAndrzej Pietrasiewicz 	return;
755a07b4970SChristoph Hellwig 
756a07b4970SChristoph Hellwig found:
757a07b4970SChristoph Hellwig 	list_del(&p->entry);
758b662a078SJay Sternberg 	nvmet_subsys_disc_changed(subsys, host);
759b662a078SJay Sternberg 
760a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
761a07b4970SChristoph Hellwig 	kfree(p);
762a07b4970SChristoph Hellwig }
763a07b4970SChristoph Hellwig 
764a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
765a07b4970SChristoph Hellwig 	.allow_link		= nvmet_allowed_hosts_allow_link,
766a07b4970SChristoph Hellwig 	.drop_link		= nvmet_allowed_hosts_drop_link,
767a07b4970SChristoph Hellwig };
768a07b4970SChristoph Hellwig 
76966603a31SBhumika Goyal static const struct config_item_type nvmet_allowed_hosts_type = {
770a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_allowed_hosts_item_ops,
771a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
772a07b4970SChristoph Hellwig };
773a07b4970SChristoph Hellwig 
774a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
775a07b4970SChristoph Hellwig 		char *page)
776a07b4970SChristoph Hellwig {
777a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n",
778a07b4970SChristoph Hellwig 		to_subsys(item)->allow_any_host);
779a07b4970SChristoph Hellwig }
780a07b4970SChristoph Hellwig 
781a07b4970SChristoph Hellwig static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
782a07b4970SChristoph Hellwig 		const char *page, size_t count)
783a07b4970SChristoph Hellwig {
784a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(item);
785a07b4970SChristoph Hellwig 	bool allow_any_host;
786a07b4970SChristoph Hellwig 	int ret = 0;
787a07b4970SChristoph Hellwig 
788a07b4970SChristoph Hellwig 	if (strtobool(page, &allow_any_host))
789a07b4970SChristoph Hellwig 		return -EINVAL;
790a07b4970SChristoph Hellwig 
791a07b4970SChristoph Hellwig 	down_write(&nvmet_config_sem);
792a07b4970SChristoph Hellwig 	if (allow_any_host && !list_empty(&subsys->hosts)) {
793a07b4970SChristoph Hellwig 		pr_err("Can't set allow_any_host when explicit hosts are set!\n");
794a07b4970SChristoph Hellwig 		ret = -EINVAL;
795a07b4970SChristoph Hellwig 		goto out_unlock;
796a07b4970SChristoph Hellwig 	}
797a07b4970SChristoph Hellwig 
798b662a078SJay Sternberg 	if (subsys->allow_any_host != allow_any_host) {
799a07b4970SChristoph Hellwig 		subsys->allow_any_host = allow_any_host;
800b662a078SJay Sternberg 		nvmet_subsys_disc_changed(subsys, NULL);
801b662a078SJay Sternberg 	}
802b662a078SJay Sternberg 
803a07b4970SChristoph Hellwig out_unlock:
804a07b4970SChristoph Hellwig 	up_write(&nvmet_config_sem);
805a07b4970SChristoph Hellwig 	return ret ? ret : count;
806a07b4970SChristoph Hellwig }
807a07b4970SChristoph Hellwig 
808a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
809a07b4970SChristoph Hellwig 
81041528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
811c61d788bSJohannes Thumshirn 					      char *page)
812c61d788bSJohannes Thumshirn {
813c61d788bSJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
814c61d788bSJohannes Thumshirn 
815c61d788bSJohannes Thumshirn 	if (NVME_TERTIARY(subsys->ver))
816c61d788bSJohannes Thumshirn 		return snprintf(page, PAGE_SIZE, "%d.%d.%d\n",
817c61d788bSJohannes Thumshirn 				(int)NVME_MAJOR(subsys->ver),
818c61d788bSJohannes Thumshirn 				(int)NVME_MINOR(subsys->ver),
819c61d788bSJohannes Thumshirn 				(int)NVME_TERTIARY(subsys->ver));
820527123c7SChaitanya Kulkarni 
821c61d788bSJohannes Thumshirn 	return snprintf(page, PAGE_SIZE, "%d.%d\n",
822c61d788bSJohannes Thumshirn 			(int)NVME_MAJOR(subsys->ver),
823c61d788bSJohannes Thumshirn 			(int)NVME_MINOR(subsys->ver));
824c61d788bSJohannes Thumshirn }
825c61d788bSJohannes Thumshirn 
82641528f80SJohannes Thumshirn static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
827c61d788bSJohannes Thumshirn 					       const char *page, size_t count)
828c61d788bSJohannes Thumshirn {
829c61d788bSJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
830c61d788bSJohannes Thumshirn 	int major, minor, tertiary = 0;
831c61d788bSJohannes Thumshirn 	int ret;
832c61d788bSJohannes Thumshirn 
833c61d788bSJohannes Thumshirn 	ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
834c61d788bSJohannes Thumshirn 	if (ret != 2 && ret != 3)
835c61d788bSJohannes Thumshirn 		return -EINVAL;
836c61d788bSJohannes Thumshirn 
837c61d788bSJohannes Thumshirn 	down_write(&nvmet_config_sem);
838c61d788bSJohannes Thumshirn 	subsys->ver = NVME_VS(major, minor, tertiary);
839c61d788bSJohannes Thumshirn 	up_write(&nvmet_config_sem);
840c61d788bSJohannes Thumshirn 
841c61d788bSJohannes Thumshirn 	return count;
842c61d788bSJohannes Thumshirn }
84341528f80SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_version);
844c61d788bSJohannes Thumshirn 
845fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
846fcbc5459SJohannes Thumshirn 					     char *page)
847fcbc5459SJohannes Thumshirn {
848fcbc5459SJohannes Thumshirn 	struct nvmet_subsys *subsys = to_subsys(item);
849fcbc5459SJohannes Thumshirn 
850fcbc5459SJohannes Thumshirn 	return snprintf(page, PAGE_SIZE, "%llx\n", subsys->serial);
851fcbc5459SJohannes Thumshirn }
852fcbc5459SJohannes Thumshirn 
853fcbc5459SJohannes Thumshirn static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
854fcbc5459SJohannes Thumshirn 					      const char *page, size_t count)
855fcbc5459SJohannes Thumshirn {
856d3a9b0caSChaitanya Kulkarni 	u64 serial;
857d3a9b0caSChaitanya Kulkarni 
858d3a9b0caSChaitanya Kulkarni 	if (sscanf(page, "%llx\n", &serial) != 1)
859d3a9b0caSChaitanya Kulkarni 		return -EINVAL;
860fcbc5459SJohannes Thumshirn 
861fcbc5459SJohannes Thumshirn 	down_write(&nvmet_config_sem);
862d3a9b0caSChaitanya Kulkarni 	to_subsys(item)->serial = serial;
863fcbc5459SJohannes Thumshirn 	up_write(&nvmet_config_sem);
864fcbc5459SJohannes Thumshirn 
865fcbc5459SJohannes Thumshirn 	return count;
866fcbc5459SJohannes Thumshirn }
867fcbc5459SJohannes Thumshirn CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
868fcbc5459SJohannes Thumshirn 
86994a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
87094a39d61SChaitanya Kulkarni 						 char *page)
87194a39d61SChaitanya Kulkarni {
87294a39d61SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
87394a39d61SChaitanya Kulkarni }
87494a39d61SChaitanya Kulkarni 
87594a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
87694a39d61SChaitanya Kulkarni 						  const char *page, size_t cnt)
87794a39d61SChaitanya Kulkarni {
87894a39d61SChaitanya Kulkarni 	u16 cntlid_min;
87994a39d61SChaitanya Kulkarni 
88094a39d61SChaitanya Kulkarni 	if (sscanf(page, "%hu\n", &cntlid_min) != 1)
88194a39d61SChaitanya Kulkarni 		return -EINVAL;
88294a39d61SChaitanya Kulkarni 
88394a39d61SChaitanya Kulkarni 	if (cntlid_min == 0)
88494a39d61SChaitanya Kulkarni 		return -EINVAL;
88594a39d61SChaitanya Kulkarni 
88694a39d61SChaitanya Kulkarni 	down_write(&nvmet_config_sem);
88794a39d61SChaitanya Kulkarni 	if (cntlid_min >= to_subsys(item)->cntlid_max)
88894a39d61SChaitanya Kulkarni 		goto out_unlock;
88994a39d61SChaitanya Kulkarni 	to_subsys(item)->cntlid_min = cntlid_min;
89094a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
89194a39d61SChaitanya Kulkarni 	return cnt;
89294a39d61SChaitanya Kulkarni 
89394a39d61SChaitanya Kulkarni out_unlock:
89494a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
89594a39d61SChaitanya Kulkarni 	return -EINVAL;
89694a39d61SChaitanya Kulkarni }
89794a39d61SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
89894a39d61SChaitanya Kulkarni 
89994a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
90094a39d61SChaitanya Kulkarni 						 char *page)
90194a39d61SChaitanya Kulkarni {
90294a39d61SChaitanya Kulkarni 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
90394a39d61SChaitanya Kulkarni }
90494a39d61SChaitanya Kulkarni 
90594a39d61SChaitanya Kulkarni static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
90694a39d61SChaitanya Kulkarni 						  const char *page, size_t cnt)
90794a39d61SChaitanya Kulkarni {
90894a39d61SChaitanya Kulkarni 	u16 cntlid_max;
90994a39d61SChaitanya Kulkarni 
91094a39d61SChaitanya Kulkarni 	if (sscanf(page, "%hu\n", &cntlid_max) != 1)
91194a39d61SChaitanya Kulkarni 		return -EINVAL;
91294a39d61SChaitanya Kulkarni 
91394a39d61SChaitanya Kulkarni 	if (cntlid_max == 0)
91494a39d61SChaitanya Kulkarni 		return -EINVAL;
91594a39d61SChaitanya Kulkarni 
91694a39d61SChaitanya Kulkarni 	down_write(&nvmet_config_sem);
91794a39d61SChaitanya Kulkarni 	if (cntlid_max <= to_subsys(item)->cntlid_min)
91894a39d61SChaitanya Kulkarni 		goto out_unlock;
91994a39d61SChaitanya Kulkarni 	to_subsys(item)->cntlid_max = cntlid_max;
92094a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
92194a39d61SChaitanya Kulkarni 	return cnt;
92294a39d61SChaitanya Kulkarni 
92394a39d61SChaitanya Kulkarni out_unlock:
92494a39d61SChaitanya Kulkarni 	up_write(&nvmet_config_sem);
92594a39d61SChaitanya Kulkarni 	return -EINVAL;
92694a39d61SChaitanya Kulkarni }
92794a39d61SChaitanya Kulkarni CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
92894a39d61SChaitanya Kulkarni 
929013b7ebeSMark Ruijter static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
930013b7ebeSMark Ruijter 					    char *page)
931013b7ebeSMark Ruijter {
932013b7ebeSMark Ruijter 	struct nvmet_subsys *subsys = to_subsys(item);
933013b7ebeSMark Ruijter 	struct nvmet_subsys_model *subsys_model;
934013b7ebeSMark Ruijter 	char *model = NVMET_DEFAULT_CTRL_MODEL;
935013b7ebeSMark Ruijter 	int ret;
936013b7ebeSMark Ruijter 
937013b7ebeSMark Ruijter 	rcu_read_lock();
938013b7ebeSMark Ruijter 	subsys_model = rcu_dereference(subsys->model);
939013b7ebeSMark Ruijter 	if (subsys_model)
940013b7ebeSMark Ruijter 		model = subsys_model->number;
941013b7ebeSMark Ruijter 	ret = snprintf(page, PAGE_SIZE, "%s\n", model);
942013b7ebeSMark Ruijter 	rcu_read_unlock();
943013b7ebeSMark Ruijter 
944013b7ebeSMark Ruijter 	return ret;
945013b7ebeSMark Ruijter }
946013b7ebeSMark Ruijter 
947013b7ebeSMark Ruijter /* See Section 1.5 of NVMe 1.4 */
948013b7ebeSMark Ruijter static bool nvmet_is_ascii(const char c)
949013b7ebeSMark Ruijter {
950013b7ebeSMark Ruijter 	return c >= 0x20 && c <= 0x7e;
951013b7ebeSMark Ruijter }
952013b7ebeSMark Ruijter 
953013b7ebeSMark Ruijter static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
954013b7ebeSMark Ruijter 					     const char *page, size_t count)
955013b7ebeSMark Ruijter {
956013b7ebeSMark Ruijter 	struct nvmet_subsys *subsys = to_subsys(item);
957013b7ebeSMark Ruijter 	struct nvmet_subsys_model *new_model;
958013b7ebeSMark Ruijter 	char *new_model_number;
959013b7ebeSMark Ruijter 	int pos = 0, len;
960013b7ebeSMark Ruijter 
961013b7ebeSMark Ruijter 	len = strcspn(page, "\n");
962013b7ebeSMark Ruijter 	if (!len)
963013b7ebeSMark Ruijter 		return -EINVAL;
964013b7ebeSMark Ruijter 
965013b7ebeSMark Ruijter 	for (pos = 0; pos < len; pos++) {
966013b7ebeSMark Ruijter 		if (!nvmet_is_ascii(page[pos]))
967013b7ebeSMark Ruijter 			return -EINVAL;
968013b7ebeSMark Ruijter 	}
969013b7ebeSMark Ruijter 
970013b7ebeSMark Ruijter 	new_model_number = kstrndup(page, len, GFP_KERNEL);
971013b7ebeSMark Ruijter 	if (!new_model_number)
972013b7ebeSMark Ruijter 		return -ENOMEM;
973013b7ebeSMark Ruijter 
974013b7ebeSMark Ruijter 	new_model = kzalloc(sizeof(*new_model) + len + 1, GFP_KERNEL);
975013b7ebeSMark Ruijter 	if (!new_model) {
976013b7ebeSMark Ruijter 		kfree(new_model_number);
977013b7ebeSMark Ruijter 		return -ENOMEM;
978013b7ebeSMark Ruijter 	}
979013b7ebeSMark Ruijter 	memcpy(new_model->number, new_model_number, len);
980013b7ebeSMark Ruijter 
981013b7ebeSMark Ruijter 	down_write(&nvmet_config_sem);
982013b7ebeSMark Ruijter 	mutex_lock(&subsys->lock);
983013b7ebeSMark Ruijter 	new_model = rcu_replace_pointer(subsys->model, new_model,
984013b7ebeSMark Ruijter 					mutex_is_locked(&subsys->lock));
985013b7ebeSMark Ruijter 	mutex_unlock(&subsys->lock);
986013b7ebeSMark Ruijter 	up_write(&nvmet_config_sem);
987013b7ebeSMark Ruijter 
988013b7ebeSMark Ruijter 	kfree_rcu(new_model, rcuhead);
989013b7ebeSMark Ruijter 
990013b7ebeSMark Ruijter 	return count;
991013b7ebeSMark Ruijter }
992013b7ebeSMark Ruijter CONFIGFS_ATTR(nvmet_subsys_, attr_model);
993013b7ebeSMark Ruijter 
994a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_subsys_attrs[] = {
995a07b4970SChristoph Hellwig 	&nvmet_subsys_attr_attr_allow_any_host,
99641528f80SJohannes Thumshirn 	&nvmet_subsys_attr_attr_version,
997fcbc5459SJohannes Thumshirn 	&nvmet_subsys_attr_attr_serial,
99894a39d61SChaitanya Kulkarni 	&nvmet_subsys_attr_attr_cntlid_min,
99994a39d61SChaitanya Kulkarni 	&nvmet_subsys_attr_attr_cntlid_max,
1000013b7ebeSMark Ruijter 	&nvmet_subsys_attr_attr_model,
1001a07b4970SChristoph Hellwig 	NULL,
1002a07b4970SChristoph Hellwig };
1003a07b4970SChristoph Hellwig 
1004a07b4970SChristoph Hellwig /*
1005a07b4970SChristoph Hellwig  * Subsystem structures & folder operation functions below
1006a07b4970SChristoph Hellwig  */
1007a07b4970SChristoph Hellwig static void nvmet_subsys_release(struct config_item *item)
1008a07b4970SChristoph Hellwig {
1009a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys = to_subsys(item);
1010a07b4970SChristoph Hellwig 
1011344770b0SSagi Grimberg 	nvmet_subsys_del_ctrls(subsys);
1012a07b4970SChristoph Hellwig 	nvmet_subsys_put(subsys);
1013a07b4970SChristoph Hellwig }
1014a07b4970SChristoph Hellwig 
1015a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_subsys_item_ops = {
1016a07b4970SChristoph Hellwig 	.release		= nvmet_subsys_release,
1017a07b4970SChristoph Hellwig };
1018a07b4970SChristoph Hellwig 
101966603a31SBhumika Goyal static const struct config_item_type nvmet_subsys_type = {
1020a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_subsys_item_ops,
1021a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_subsys_attrs,
1022a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1023a07b4970SChristoph Hellwig };
1024a07b4970SChristoph Hellwig 
1025a07b4970SChristoph Hellwig static struct config_group *nvmet_subsys_make(struct config_group *group,
1026a07b4970SChristoph Hellwig 		const char *name)
1027a07b4970SChristoph Hellwig {
1028a07b4970SChristoph Hellwig 	struct nvmet_subsys *subsys;
1029a07b4970SChristoph Hellwig 
1030a07b4970SChristoph Hellwig 	if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1031a07b4970SChristoph Hellwig 		pr_err("can't create discovery subsystem through configfs\n");
1032a07b4970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1033a07b4970SChristoph Hellwig 	}
1034a07b4970SChristoph Hellwig 
1035a07b4970SChristoph Hellwig 	subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
10366b7e631bSMinwoo Im 	if (IS_ERR(subsys))
10376b7e631bSMinwoo Im 		return ERR_CAST(subsys);
1038a07b4970SChristoph Hellwig 
1039a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1040a07b4970SChristoph Hellwig 
1041a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->namespaces_group,
1042a07b4970SChristoph Hellwig 			"namespaces", &nvmet_namespaces_type);
1043a07b4970SChristoph Hellwig 	configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1044a07b4970SChristoph Hellwig 
1045a07b4970SChristoph Hellwig 	config_group_init_type_name(&subsys->allowed_hosts_group,
1046a07b4970SChristoph Hellwig 			"allowed_hosts", &nvmet_allowed_hosts_type);
1047a07b4970SChristoph Hellwig 	configfs_add_default_group(&subsys->allowed_hosts_group,
1048a07b4970SChristoph Hellwig 			&subsys->group);
1049a07b4970SChristoph Hellwig 
1050a07b4970SChristoph Hellwig 	return &subsys->group;
1051a07b4970SChristoph Hellwig }
1052a07b4970SChristoph Hellwig 
1053a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_subsystems_group_ops = {
1054a07b4970SChristoph Hellwig 	.make_group		= nvmet_subsys_make,
1055a07b4970SChristoph Hellwig };
1056a07b4970SChristoph Hellwig 
105766603a31SBhumika Goyal static const struct config_item_type nvmet_subsystems_type = {
1058a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_subsystems_group_ops,
1059a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1060a07b4970SChristoph Hellwig };
1061a07b4970SChristoph Hellwig 
1062a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_show(struct config_item *item,
1063a07b4970SChristoph Hellwig 		char *page)
1064a07b4970SChristoph Hellwig {
1065a07b4970SChristoph Hellwig 	return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1066a07b4970SChristoph Hellwig }
1067a07b4970SChristoph Hellwig 
1068a07b4970SChristoph Hellwig static ssize_t nvmet_referral_enable_store(struct config_item *item,
1069a07b4970SChristoph Hellwig 		const char *page, size_t count)
1070a07b4970SChristoph Hellwig {
1071a07b4970SChristoph Hellwig 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1072a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1073a07b4970SChristoph Hellwig 	bool enable;
1074a07b4970SChristoph Hellwig 
1075a07b4970SChristoph Hellwig 	if (strtobool(page, &enable))
1076a07b4970SChristoph Hellwig 		goto inval;
1077a07b4970SChristoph Hellwig 
1078a07b4970SChristoph Hellwig 	if (enable)
1079a07b4970SChristoph Hellwig 		nvmet_referral_enable(parent, port);
1080a07b4970SChristoph Hellwig 	else
1081b662a078SJay Sternberg 		nvmet_referral_disable(parent, port);
1082a07b4970SChristoph Hellwig 
1083a07b4970SChristoph Hellwig 	return count;
1084a07b4970SChristoph Hellwig inval:
1085a07b4970SChristoph Hellwig 	pr_err("Invalid value '%s' for enable\n", page);
1086a07b4970SChristoph Hellwig 	return -EINVAL;
1087a07b4970SChristoph Hellwig }
1088a07b4970SChristoph Hellwig 
1089a07b4970SChristoph Hellwig CONFIGFS_ATTR(nvmet_referral_, enable);
1090a07b4970SChristoph Hellwig 
1091a07b4970SChristoph Hellwig /*
1092a07b4970SChristoph Hellwig  * Discovery Service subsystem definitions
1093a07b4970SChristoph Hellwig  */
1094a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_referral_attrs[] = {
1095a07b4970SChristoph Hellwig 	&nvmet_attr_addr_adrfam,
1096a07b4970SChristoph Hellwig 	&nvmet_attr_addr_portid,
1097a07b4970SChristoph Hellwig 	&nvmet_attr_addr_treq,
1098a07b4970SChristoph Hellwig 	&nvmet_attr_addr_traddr,
1099a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trsvcid,
1100a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trtype,
1101a07b4970SChristoph Hellwig 	&nvmet_referral_attr_enable,
1102a07b4970SChristoph Hellwig 	NULL,
1103a07b4970SChristoph Hellwig };
1104a07b4970SChristoph Hellwig 
1105f0e656e4SSagi Grimberg static void nvmet_referral_notify(struct config_group *group,
1106f0e656e4SSagi Grimberg 		struct config_item *item)
1107a07b4970SChristoph Hellwig {
1108b662a078SJay Sternberg 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1109a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1110a07b4970SChristoph Hellwig 
1111b662a078SJay Sternberg 	nvmet_referral_disable(parent, port);
1112f0e656e4SSagi Grimberg }
1113f0e656e4SSagi Grimberg 
1114f0e656e4SSagi Grimberg static void nvmet_referral_release(struct config_item *item)
1115f0e656e4SSagi Grimberg {
1116f0e656e4SSagi Grimberg 	struct nvmet_port *port = to_nvmet_port(item);
1117f0e656e4SSagi Grimberg 
1118a07b4970SChristoph Hellwig 	kfree(port);
1119a07b4970SChristoph Hellwig }
1120a07b4970SChristoph Hellwig 
1121a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_referral_item_ops = {
1122a07b4970SChristoph Hellwig 	.release	= nvmet_referral_release,
1123a07b4970SChristoph Hellwig };
1124a07b4970SChristoph Hellwig 
112566603a31SBhumika Goyal static const struct config_item_type nvmet_referral_type = {
1126a07b4970SChristoph Hellwig 	.ct_owner	= THIS_MODULE,
1127a07b4970SChristoph Hellwig 	.ct_attrs	= nvmet_referral_attrs,
1128a07b4970SChristoph Hellwig 	.ct_item_ops	= &nvmet_referral_item_ops,
1129a07b4970SChristoph Hellwig };
1130a07b4970SChristoph Hellwig 
1131a07b4970SChristoph Hellwig static struct config_group *nvmet_referral_make(
1132a07b4970SChristoph Hellwig 		struct config_group *group, const char *name)
1133a07b4970SChristoph Hellwig {
1134a07b4970SChristoph Hellwig 	struct nvmet_port *port;
1135a07b4970SChristoph Hellwig 
1136a07b4970SChristoph Hellwig 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1137a07b4970SChristoph Hellwig 	if (!port)
1138f98d9ca1SDan Carpenter 		return ERR_PTR(-ENOMEM);
1139a07b4970SChristoph Hellwig 
1140a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->entry);
1141a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1142a07b4970SChristoph Hellwig 
1143a07b4970SChristoph Hellwig 	return &port->group;
1144a07b4970SChristoph Hellwig }
1145a07b4970SChristoph Hellwig 
1146a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_referral_group_ops = {
1147a07b4970SChristoph Hellwig 	.make_group		= nvmet_referral_make,
1148f0e656e4SSagi Grimberg 	.disconnect_notify	= nvmet_referral_notify,
1149a07b4970SChristoph Hellwig };
1150a07b4970SChristoph Hellwig 
115166603a31SBhumika Goyal static const struct config_item_type nvmet_referrals_type = {
1152a07b4970SChristoph Hellwig 	.ct_owner	= THIS_MODULE,
1153a07b4970SChristoph Hellwig 	.ct_group_ops	= &nvmet_referral_group_ops,
1154a07b4970SChristoph Hellwig };
1155a07b4970SChristoph Hellwig 
115662ac0d32SChristoph Hellwig static struct {
115762ac0d32SChristoph Hellwig 	enum nvme_ana_state	state;
115862ac0d32SChristoph Hellwig 	const char		*name;
115962ac0d32SChristoph Hellwig } nvmet_ana_state_names[] = {
116062ac0d32SChristoph Hellwig 	{ NVME_ANA_OPTIMIZED,		"optimized" },
116162ac0d32SChristoph Hellwig 	{ NVME_ANA_NONOPTIMIZED,	"non-optimized" },
116262ac0d32SChristoph Hellwig 	{ NVME_ANA_INACCESSIBLE,	"inaccessible" },
116362ac0d32SChristoph Hellwig 	{ NVME_ANA_PERSISTENT_LOSS,	"persistent-loss" },
116462ac0d32SChristoph Hellwig 	{ NVME_ANA_CHANGE,		"change" },
116562ac0d32SChristoph Hellwig };
116662ac0d32SChristoph Hellwig 
116762ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
116862ac0d32SChristoph Hellwig 		char *page)
116962ac0d32SChristoph Hellwig {
117062ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
117162ac0d32SChristoph Hellwig 	enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
117262ac0d32SChristoph Hellwig 	int i;
117362ac0d32SChristoph Hellwig 
117462ac0d32SChristoph Hellwig 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state_names); i++) {
117562ac0d32SChristoph Hellwig 		if (state != nvmet_ana_state_names[i].state)
117662ac0d32SChristoph Hellwig 			continue;
117762ac0d32SChristoph Hellwig 		return sprintf(page, "%s\n", nvmet_ana_state_names[i].name);
117862ac0d32SChristoph Hellwig 	}
117962ac0d32SChristoph Hellwig 
118062ac0d32SChristoph Hellwig 	return sprintf(page, "\n");
118162ac0d32SChristoph Hellwig }
118262ac0d32SChristoph Hellwig 
118362ac0d32SChristoph Hellwig static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
118462ac0d32SChristoph Hellwig 		const char *page, size_t count)
118562ac0d32SChristoph Hellwig {
118662ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
118762ac0d32SChristoph Hellwig 	int i;
118862ac0d32SChristoph Hellwig 
118962ac0d32SChristoph Hellwig 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state_names); i++) {
119062ac0d32SChristoph Hellwig 		if (sysfs_streq(page, nvmet_ana_state_names[i].name))
119162ac0d32SChristoph Hellwig 			goto found;
119262ac0d32SChristoph Hellwig 	}
119362ac0d32SChristoph Hellwig 
119462ac0d32SChristoph Hellwig 	pr_err("Invalid value '%s' for ana_state\n", page);
119562ac0d32SChristoph Hellwig 	return -EINVAL;
119662ac0d32SChristoph Hellwig 
119762ac0d32SChristoph Hellwig found:
119862ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
119962ac0d32SChristoph Hellwig 	grp->port->ana_state[grp->grpid] = nvmet_ana_state_names[i].state;
120062ac0d32SChristoph Hellwig 	nvmet_ana_chgcnt++;
120162ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
120262ac0d32SChristoph Hellwig 
120362ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
120462ac0d32SChristoph Hellwig 	return count;
120562ac0d32SChristoph Hellwig }
120662ac0d32SChristoph Hellwig 
120762ac0d32SChristoph Hellwig CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
120862ac0d32SChristoph Hellwig 
120962ac0d32SChristoph Hellwig static struct configfs_attribute *nvmet_ana_group_attrs[] = {
121062ac0d32SChristoph Hellwig 	&nvmet_ana_group_attr_ana_state,
121162ac0d32SChristoph Hellwig 	NULL,
121262ac0d32SChristoph Hellwig };
121362ac0d32SChristoph Hellwig 
121462ac0d32SChristoph Hellwig static void nvmet_ana_group_release(struct config_item *item)
121562ac0d32SChristoph Hellwig {
121662ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp = to_ana_group(item);
121762ac0d32SChristoph Hellwig 
121862ac0d32SChristoph Hellwig 	if (grp == &grp->port->ana_default_group)
121962ac0d32SChristoph Hellwig 		return;
122062ac0d32SChristoph Hellwig 
122162ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
122262ac0d32SChristoph Hellwig 	grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
122362ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[grp->grpid]--;
122462ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
122562ac0d32SChristoph Hellwig 
122662ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
122762ac0d32SChristoph Hellwig 	kfree(grp);
122862ac0d32SChristoph Hellwig }
122962ac0d32SChristoph Hellwig 
123062ac0d32SChristoph Hellwig static struct configfs_item_operations nvmet_ana_group_item_ops = {
123162ac0d32SChristoph Hellwig 	.release		= nvmet_ana_group_release,
123262ac0d32SChristoph Hellwig };
123362ac0d32SChristoph Hellwig 
123462ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_group_type = {
123562ac0d32SChristoph Hellwig 	.ct_item_ops		= &nvmet_ana_group_item_ops,
123662ac0d32SChristoph Hellwig 	.ct_attrs		= nvmet_ana_group_attrs,
123762ac0d32SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
123862ac0d32SChristoph Hellwig };
123962ac0d32SChristoph Hellwig 
124062ac0d32SChristoph Hellwig static struct config_group *nvmet_ana_groups_make_group(
124162ac0d32SChristoph Hellwig 		struct config_group *group, const char *name)
124262ac0d32SChristoph Hellwig {
124362ac0d32SChristoph Hellwig 	struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
124462ac0d32SChristoph Hellwig 	struct nvmet_ana_group *grp;
124562ac0d32SChristoph Hellwig 	u32 grpid;
124662ac0d32SChristoph Hellwig 	int ret;
124762ac0d32SChristoph Hellwig 
124862ac0d32SChristoph Hellwig 	ret = kstrtou32(name, 0, &grpid);
124962ac0d32SChristoph Hellwig 	if (ret)
125062ac0d32SChristoph Hellwig 		goto out;
125162ac0d32SChristoph Hellwig 
125262ac0d32SChristoph Hellwig 	ret = -EINVAL;
125362ac0d32SChristoph Hellwig 	if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
125462ac0d32SChristoph Hellwig 		goto out;
125562ac0d32SChristoph Hellwig 
125662ac0d32SChristoph Hellwig 	ret = -ENOMEM;
125762ac0d32SChristoph Hellwig 	grp = kzalloc(sizeof(*grp), GFP_KERNEL);
125862ac0d32SChristoph Hellwig 	if (!grp)
125962ac0d32SChristoph Hellwig 		goto out;
126062ac0d32SChristoph Hellwig 	grp->port = port;
126162ac0d32SChristoph Hellwig 	grp->grpid = grpid;
126262ac0d32SChristoph Hellwig 
126362ac0d32SChristoph Hellwig 	down_write(&nvmet_ana_sem);
126462ac0d32SChristoph Hellwig 	nvmet_ana_group_enabled[grpid]++;
126562ac0d32SChristoph Hellwig 	up_write(&nvmet_ana_sem);
126662ac0d32SChristoph Hellwig 
126762ac0d32SChristoph Hellwig 	nvmet_port_send_ana_event(grp->port);
126862ac0d32SChristoph Hellwig 
126962ac0d32SChristoph Hellwig 	config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
127062ac0d32SChristoph Hellwig 	return &grp->group;
127162ac0d32SChristoph Hellwig out:
127262ac0d32SChristoph Hellwig 	return ERR_PTR(ret);
127362ac0d32SChristoph Hellwig }
127462ac0d32SChristoph Hellwig 
127562ac0d32SChristoph Hellwig static struct configfs_group_operations nvmet_ana_groups_group_ops = {
127662ac0d32SChristoph Hellwig 	.make_group		= nvmet_ana_groups_make_group,
127762ac0d32SChristoph Hellwig };
127862ac0d32SChristoph Hellwig 
127962ac0d32SChristoph Hellwig static const struct config_item_type nvmet_ana_groups_type = {
128062ac0d32SChristoph Hellwig 	.ct_group_ops		= &nvmet_ana_groups_group_ops,
128162ac0d32SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
128262ac0d32SChristoph Hellwig };
128362ac0d32SChristoph Hellwig 
1284a07b4970SChristoph Hellwig /*
1285a07b4970SChristoph Hellwig  * Ports definitions.
1286a07b4970SChristoph Hellwig  */
1287a07b4970SChristoph Hellwig static void nvmet_port_release(struct config_item *item)
1288a07b4970SChristoph Hellwig {
1289a07b4970SChristoph Hellwig 	struct nvmet_port *port = to_nvmet_port(item);
1290a07b4970SChristoph Hellwig 
1291b662a078SJay Sternberg 	list_del(&port->global_entry);
1292b662a078SJay Sternberg 
129372efd25dSChristoph Hellwig 	kfree(port->ana_state);
1294a07b4970SChristoph Hellwig 	kfree(port);
1295a07b4970SChristoph Hellwig }
1296a07b4970SChristoph Hellwig 
1297a07b4970SChristoph Hellwig static struct configfs_attribute *nvmet_port_attrs[] = {
1298a07b4970SChristoph Hellwig 	&nvmet_attr_addr_adrfam,
1299a07b4970SChristoph Hellwig 	&nvmet_attr_addr_treq,
1300a07b4970SChristoph Hellwig 	&nvmet_attr_addr_traddr,
1301a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trsvcid,
1302a07b4970SChristoph Hellwig 	&nvmet_attr_addr_trtype,
13030d5ee2b2SSteve Wise 	&nvmet_attr_param_inline_data_size,
1304a07b4970SChristoph Hellwig 	NULL,
1305a07b4970SChristoph Hellwig };
1306a07b4970SChristoph Hellwig 
1307a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_port_item_ops = {
1308a07b4970SChristoph Hellwig 	.release		= nvmet_port_release,
1309a07b4970SChristoph Hellwig };
1310a07b4970SChristoph Hellwig 
131166603a31SBhumika Goyal static const struct config_item_type nvmet_port_type = {
1312a07b4970SChristoph Hellwig 	.ct_attrs		= nvmet_port_attrs,
1313a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_port_item_ops,
1314a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1315a07b4970SChristoph Hellwig };
1316a07b4970SChristoph Hellwig 
1317a07b4970SChristoph Hellwig static struct config_group *nvmet_ports_make(struct config_group *group,
1318a07b4970SChristoph Hellwig 		const char *name)
1319a07b4970SChristoph Hellwig {
1320a07b4970SChristoph Hellwig 	struct nvmet_port *port;
1321a07b4970SChristoph Hellwig 	u16 portid;
132262ac0d32SChristoph Hellwig 	u32 i;
1323a07b4970SChristoph Hellwig 
1324a07b4970SChristoph Hellwig 	if (kstrtou16(name, 0, &portid))
1325a07b4970SChristoph Hellwig 		return ERR_PTR(-EINVAL);
1326a07b4970SChristoph Hellwig 
1327a07b4970SChristoph Hellwig 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1328a07b4970SChristoph Hellwig 	if (!port)
1329f98d9ca1SDan Carpenter 		return ERR_PTR(-ENOMEM);
1330a07b4970SChristoph Hellwig 
133172efd25dSChristoph Hellwig 	port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
133272efd25dSChristoph Hellwig 			sizeof(*port->ana_state), GFP_KERNEL);
133372efd25dSChristoph Hellwig 	if (!port->ana_state) {
133472efd25dSChristoph Hellwig 		kfree(port);
133572efd25dSChristoph Hellwig 		return ERR_PTR(-ENOMEM);
133672efd25dSChristoph Hellwig 	}
133772efd25dSChristoph Hellwig 
133862ac0d32SChristoph Hellwig 	for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
133962ac0d32SChristoph Hellwig 		if (i == NVMET_DEFAULT_ANA_GRPID)
134062ac0d32SChristoph Hellwig 			port->ana_state[1] = NVME_ANA_OPTIMIZED;
134162ac0d32SChristoph Hellwig 		else
134262ac0d32SChristoph Hellwig 			port->ana_state[i] = NVME_ANA_INACCESSIBLE;
134362ac0d32SChristoph Hellwig 	}
134472efd25dSChristoph Hellwig 
1345b662a078SJay Sternberg 	list_add(&port->global_entry, &nvmet_ports_list);
1346b662a078SJay Sternberg 
1347a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->entry);
1348a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->subsystems);
1349a07b4970SChristoph Hellwig 	INIT_LIST_HEAD(&port->referrals);
13500d5ee2b2SSteve Wise 	port->inline_data_size = -1;	/* < 0 == let the transport choose */
1351a07b4970SChristoph Hellwig 
1352a07b4970SChristoph Hellwig 	port->disc_addr.portid = cpu_to_le16(portid);
13539b95d2fbSSagi Grimberg 	port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1354a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->group, name, &nvmet_port_type);
1355a07b4970SChristoph Hellwig 
1356a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->subsys_group,
1357a07b4970SChristoph Hellwig 			"subsystems", &nvmet_port_subsys_type);
1358a07b4970SChristoph Hellwig 	configfs_add_default_group(&port->subsys_group, &port->group);
1359a07b4970SChristoph Hellwig 
1360a07b4970SChristoph Hellwig 	config_group_init_type_name(&port->referrals_group,
1361a07b4970SChristoph Hellwig 			"referrals", &nvmet_referrals_type);
1362a07b4970SChristoph Hellwig 	configfs_add_default_group(&port->referrals_group, &port->group);
1363a07b4970SChristoph Hellwig 
136462ac0d32SChristoph Hellwig 	config_group_init_type_name(&port->ana_groups_group,
136562ac0d32SChristoph Hellwig 			"ana_groups", &nvmet_ana_groups_type);
136662ac0d32SChristoph Hellwig 	configfs_add_default_group(&port->ana_groups_group, &port->group);
136762ac0d32SChristoph Hellwig 
136862ac0d32SChristoph Hellwig 	port->ana_default_group.port = port;
136962ac0d32SChristoph Hellwig 	port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
137062ac0d32SChristoph Hellwig 	config_group_init_type_name(&port->ana_default_group.group,
137162ac0d32SChristoph Hellwig 			__stringify(NVMET_DEFAULT_ANA_GRPID),
137262ac0d32SChristoph Hellwig 			&nvmet_ana_group_type);
137362ac0d32SChristoph Hellwig 	configfs_add_default_group(&port->ana_default_group.group,
137462ac0d32SChristoph Hellwig 			&port->ana_groups_group);
137562ac0d32SChristoph Hellwig 
1376a07b4970SChristoph Hellwig 	return &port->group;
1377a07b4970SChristoph Hellwig }
1378a07b4970SChristoph Hellwig 
1379a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_ports_group_ops = {
1380a07b4970SChristoph Hellwig 	.make_group		= nvmet_ports_make,
1381a07b4970SChristoph Hellwig };
1382a07b4970SChristoph Hellwig 
138366603a31SBhumika Goyal static const struct config_item_type nvmet_ports_type = {
1384a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_ports_group_ops,
1385a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1386a07b4970SChristoph Hellwig };
1387a07b4970SChristoph Hellwig 
1388a07b4970SChristoph Hellwig static struct config_group nvmet_subsystems_group;
1389a07b4970SChristoph Hellwig static struct config_group nvmet_ports_group;
1390a07b4970SChristoph Hellwig 
1391a07b4970SChristoph Hellwig static void nvmet_host_release(struct config_item *item)
1392a07b4970SChristoph Hellwig {
1393a07b4970SChristoph Hellwig 	struct nvmet_host *host = to_host(item);
1394a07b4970SChristoph Hellwig 
1395a07b4970SChristoph Hellwig 	kfree(host);
1396a07b4970SChristoph Hellwig }
1397a07b4970SChristoph Hellwig 
1398a07b4970SChristoph Hellwig static struct configfs_item_operations nvmet_host_item_ops = {
1399a07b4970SChristoph Hellwig 	.release		= nvmet_host_release,
1400a07b4970SChristoph Hellwig };
1401a07b4970SChristoph Hellwig 
140266603a31SBhumika Goyal static const struct config_item_type nvmet_host_type = {
1403a07b4970SChristoph Hellwig 	.ct_item_ops		= &nvmet_host_item_ops,
1404a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1405a07b4970SChristoph Hellwig };
1406a07b4970SChristoph Hellwig 
1407a07b4970SChristoph Hellwig static struct config_group *nvmet_hosts_make_group(struct config_group *group,
1408a07b4970SChristoph Hellwig 		const char *name)
1409a07b4970SChristoph Hellwig {
1410a07b4970SChristoph Hellwig 	struct nvmet_host *host;
1411a07b4970SChristoph Hellwig 
1412a07b4970SChristoph Hellwig 	host = kzalloc(sizeof(*host), GFP_KERNEL);
1413a07b4970SChristoph Hellwig 	if (!host)
1414a07b4970SChristoph Hellwig 		return ERR_PTR(-ENOMEM);
1415a07b4970SChristoph Hellwig 
1416a07b4970SChristoph Hellwig 	config_group_init_type_name(&host->group, name, &nvmet_host_type);
1417a07b4970SChristoph Hellwig 
1418a07b4970SChristoph Hellwig 	return &host->group;
1419a07b4970SChristoph Hellwig }
1420a07b4970SChristoph Hellwig 
1421a07b4970SChristoph Hellwig static struct configfs_group_operations nvmet_hosts_group_ops = {
1422a07b4970SChristoph Hellwig 	.make_group		= nvmet_hosts_make_group,
1423a07b4970SChristoph Hellwig };
1424a07b4970SChristoph Hellwig 
142566603a31SBhumika Goyal static const struct config_item_type nvmet_hosts_type = {
1426a07b4970SChristoph Hellwig 	.ct_group_ops		= &nvmet_hosts_group_ops,
1427a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1428a07b4970SChristoph Hellwig };
1429a07b4970SChristoph Hellwig 
1430a07b4970SChristoph Hellwig static struct config_group nvmet_hosts_group;
1431a07b4970SChristoph Hellwig 
143266603a31SBhumika Goyal static const struct config_item_type nvmet_root_type = {
1433a07b4970SChristoph Hellwig 	.ct_owner		= THIS_MODULE,
1434a07b4970SChristoph Hellwig };
1435a07b4970SChristoph Hellwig 
1436a07b4970SChristoph Hellwig static struct configfs_subsystem nvmet_configfs_subsystem = {
1437a07b4970SChristoph Hellwig 	.su_group = {
1438a07b4970SChristoph Hellwig 		.cg_item = {
1439a07b4970SChristoph Hellwig 			.ci_namebuf	= "nvmet",
1440a07b4970SChristoph Hellwig 			.ci_type	= &nvmet_root_type,
1441a07b4970SChristoph Hellwig 		},
1442a07b4970SChristoph Hellwig 	},
1443a07b4970SChristoph Hellwig };
1444a07b4970SChristoph Hellwig 
1445a07b4970SChristoph Hellwig int __init nvmet_init_configfs(void)
1446a07b4970SChristoph Hellwig {
1447a07b4970SChristoph Hellwig 	int ret;
1448a07b4970SChristoph Hellwig 
1449a07b4970SChristoph Hellwig 	config_group_init(&nvmet_configfs_subsystem.su_group);
1450a07b4970SChristoph Hellwig 	mutex_init(&nvmet_configfs_subsystem.su_mutex);
1451a07b4970SChristoph Hellwig 
1452a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_subsystems_group,
1453a07b4970SChristoph Hellwig 			"subsystems", &nvmet_subsystems_type);
1454a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_subsystems_group,
1455a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1456a07b4970SChristoph Hellwig 
1457a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_ports_group,
1458a07b4970SChristoph Hellwig 			"ports", &nvmet_ports_type);
1459a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_ports_group,
1460a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1461a07b4970SChristoph Hellwig 
1462a07b4970SChristoph Hellwig 	config_group_init_type_name(&nvmet_hosts_group,
1463a07b4970SChristoph Hellwig 			"hosts", &nvmet_hosts_type);
1464a07b4970SChristoph Hellwig 	configfs_add_default_group(&nvmet_hosts_group,
1465a07b4970SChristoph Hellwig 			&nvmet_configfs_subsystem.su_group);
1466a07b4970SChristoph Hellwig 
1467a07b4970SChristoph Hellwig 	ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
1468a07b4970SChristoph Hellwig 	if (ret) {
1469a07b4970SChristoph Hellwig 		pr_err("configfs_register_subsystem: %d\n", ret);
1470a07b4970SChristoph Hellwig 		return ret;
1471a07b4970SChristoph Hellwig 	}
1472a07b4970SChristoph Hellwig 
1473a07b4970SChristoph Hellwig 	return 0;
1474a07b4970SChristoph Hellwig }
1475a07b4970SChristoph Hellwig 
1476a07b4970SChristoph Hellwig void __exit nvmet_exit_configfs(void)
1477a07b4970SChristoph Hellwig {
1478a07b4970SChristoph Hellwig 	configfs_unregister_subsystem(&nvmet_configfs_subsystem);
1479a07b4970SChristoph Hellwig }
1480