xref: /linux/drivers/nvme/target/configfs.c (revision ed63ba15d7830c30077dbb33c94242be01e45a18)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Configfs interface for the NVMe target.
4  * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5  */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/kstrtox.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/stat.h>
12 #include <linux/ctype.h>
13 #include <linux/pci.h>
14 #include <linux/pci-p2pdma.h>
15 #ifdef CONFIG_NVME_TARGET_AUTH
16 #include <linux/nvme-auth.h>
17 #endif
18 #include <linux/nvme-keyring.h>
19 #include <crypto/hash.h>
20 #include <crypto/kpp.h>
21 #include <linux/nospec.h>
22 
23 #include "nvmet.h"
24 
25 static const struct config_item_type nvmet_host_type;
26 static const struct config_item_type nvmet_subsys_type;
27 
28 static LIST_HEAD(nvmet_ports_list);
29 struct list_head *nvmet_ports = &nvmet_ports_list;
30 
31 struct nvmet_type_name_map {
32 	u8		type;
33 	const char	*name;
34 };
35 
36 static struct nvmet_type_name_map nvmet_transport[] = {
37 	{ NVMF_TRTYPE_RDMA,	"rdma" },
38 	{ NVMF_TRTYPE_FC,	"fc" },
39 	{ NVMF_TRTYPE_TCP,	"tcp" },
40 	{ NVMF_TRTYPE_LOOP,	"loop" },
41 };
42 
43 static const struct nvmet_type_name_map nvmet_addr_family[] = {
44 	{ NVMF_ADDR_FAMILY_PCI,		"pcie" },
45 	{ NVMF_ADDR_FAMILY_IP4,		"ipv4" },
46 	{ NVMF_ADDR_FAMILY_IP6,		"ipv6" },
47 	{ NVMF_ADDR_FAMILY_IB,		"ib" },
48 	{ NVMF_ADDR_FAMILY_FC,		"fc" },
49 	{ NVMF_ADDR_FAMILY_LOOP,	"loop" },
50 };
51 
52 static bool nvmet_is_port_enabled(struct nvmet_port *p, const char *caller)
53 {
54 	if (p->enabled)
55 		pr_err("Disable port '%u' before changing attribute in %s\n",
56 		       le16_to_cpu(p->disc_addr.portid), caller);
57 	return p->enabled;
58 }
59 
60 /*
61  * nvmet_port Generic ConfigFS definitions.
62  * Used in any place in the ConfigFS tree that refers to an address.
63  */
64 static ssize_t nvmet_addr_adrfam_show(struct config_item *item, char *page)
65 {
66 	u8 adrfam = to_nvmet_port(item)->disc_addr.adrfam;
67 	int i;
68 
69 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
70 		if (nvmet_addr_family[i].type == adrfam)
71 			return snprintf(page, PAGE_SIZE, "%s\n",
72 					nvmet_addr_family[i].name);
73 	}
74 
75 	return snprintf(page, PAGE_SIZE, "\n");
76 }
77 
78 static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
79 		const char *page, size_t count)
80 {
81 	struct nvmet_port *port = to_nvmet_port(item);
82 	int i;
83 
84 	if (nvmet_is_port_enabled(port, __func__))
85 		return -EACCES;
86 
87 	for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
88 		if (sysfs_streq(page, nvmet_addr_family[i].name))
89 			goto found;
90 	}
91 
92 	pr_err("Invalid value '%s' for adrfam\n", page);
93 	return -EINVAL;
94 
95 found:
96 	port->disc_addr.adrfam = nvmet_addr_family[i].type;
97 	return count;
98 }
99 
100 CONFIGFS_ATTR(nvmet_, addr_adrfam);
101 
102 static ssize_t nvmet_addr_portid_show(struct config_item *item,
103 		char *page)
104 {
105 	__le16 portid = to_nvmet_port(item)->disc_addr.portid;
106 
107 	return snprintf(page, PAGE_SIZE, "%d\n", le16_to_cpu(portid));
108 }
109 
110 static ssize_t nvmet_addr_portid_store(struct config_item *item,
111 		const char *page, size_t count)
112 {
113 	struct nvmet_port *port = to_nvmet_port(item);
114 	u16 portid = 0;
115 
116 	if (kstrtou16(page, 0, &portid)) {
117 		pr_err("Invalid value '%s' for portid\n", page);
118 		return -EINVAL;
119 	}
120 
121 	if (nvmet_is_port_enabled(port, __func__))
122 		return -EACCES;
123 
124 	port->disc_addr.portid = cpu_to_le16(portid);
125 	return count;
126 }
127 
128 CONFIGFS_ATTR(nvmet_, addr_portid);
129 
130 static ssize_t nvmet_addr_traddr_show(struct config_item *item,
131 		char *page)
132 {
133 	struct nvmet_port *port = to_nvmet_port(item);
134 
135 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.traddr);
136 }
137 
138 static ssize_t nvmet_addr_traddr_store(struct config_item *item,
139 		const char *page, size_t count)
140 {
141 	struct nvmet_port *port = to_nvmet_port(item);
142 
143 	if (count > NVMF_TRADDR_SIZE) {
144 		pr_err("Invalid value '%s' for traddr\n", page);
145 		return -EINVAL;
146 	}
147 
148 	if (nvmet_is_port_enabled(port, __func__))
149 		return -EACCES;
150 
151 	if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
152 		return -EINVAL;
153 	return count;
154 }
155 
156 CONFIGFS_ATTR(nvmet_, addr_traddr);
157 
158 static const struct nvmet_type_name_map nvmet_addr_treq[] = {
159 	{ NVMF_TREQ_NOT_SPECIFIED,	"not specified" },
160 	{ NVMF_TREQ_REQUIRED,		"required" },
161 	{ NVMF_TREQ_NOT_REQUIRED,	"not required" },
162 };
163 
164 static inline u8 nvmet_port_disc_addr_treq_mask(struct nvmet_port *port)
165 {
166 	return (port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK);
167 }
168 
169 static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page)
170 {
171 	u8 treq = nvmet_port_disc_addr_treq_secure_channel(to_nvmet_port(item));
172 	int i;
173 
174 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
175 		if (treq == nvmet_addr_treq[i].type)
176 			return snprintf(page, PAGE_SIZE, "%s\n",
177 					nvmet_addr_treq[i].name);
178 	}
179 
180 	return snprintf(page, PAGE_SIZE, "\n");
181 }
182 
183 static ssize_t nvmet_addr_treq_store(struct config_item *item,
184 		const char *page, size_t count)
185 {
186 	struct nvmet_port *port = to_nvmet_port(item);
187 	u8 treq = nvmet_port_disc_addr_treq_mask(port);
188 	int i;
189 
190 	if (nvmet_is_port_enabled(port, __func__))
191 		return -EACCES;
192 
193 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
194 		if (sysfs_streq(page, nvmet_addr_treq[i].name))
195 			goto found;
196 	}
197 
198 	pr_err("Invalid value '%s' for treq\n", page);
199 	return -EINVAL;
200 
201 found:
202 	if (port->disc_addr.trtype == NVMF_TRTYPE_TCP &&
203 	    port->disc_addr.tsas.tcp.sectype == NVMF_TCP_SECTYPE_TLS13) {
204 		switch (nvmet_addr_treq[i].type) {
205 		case NVMF_TREQ_NOT_SPECIFIED:
206 			pr_debug("treq '%s' not allowed for TLS1.3\n",
207 				 nvmet_addr_treq[i].name);
208 			return -EINVAL;
209 		case NVMF_TREQ_NOT_REQUIRED:
210 			pr_warn("Allow non-TLS connections while TLS1.3 is enabled\n");
211 			break;
212 		default:
213 			break;
214 		}
215 	}
216 	treq |= nvmet_addr_treq[i].type;
217 	port->disc_addr.treq = treq;
218 	return count;
219 }
220 
221 CONFIGFS_ATTR(nvmet_, addr_treq);
222 
223 static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
224 		char *page)
225 {
226 	struct nvmet_port *port = to_nvmet_port(item);
227 
228 	return snprintf(page, PAGE_SIZE, "%s\n", port->disc_addr.trsvcid);
229 }
230 
231 static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
232 		const char *page, size_t count)
233 {
234 	struct nvmet_port *port = to_nvmet_port(item);
235 
236 	if (count > NVMF_TRSVCID_SIZE) {
237 		pr_err("Invalid value '%s' for trsvcid\n", page);
238 		return -EINVAL;
239 	}
240 	if (nvmet_is_port_enabled(port, __func__))
241 		return -EACCES;
242 
243 	if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
244 		return -EINVAL;
245 	return count;
246 }
247 
248 CONFIGFS_ATTR(nvmet_, addr_trsvcid);
249 
250 static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
251 		char *page)
252 {
253 	struct nvmet_port *port = to_nvmet_port(item);
254 
255 	return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
256 }
257 
258 static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
259 		const char *page, size_t count)
260 {
261 	struct nvmet_port *port = to_nvmet_port(item);
262 	int ret;
263 
264 	if (nvmet_is_port_enabled(port, __func__))
265 		return -EACCES;
266 	ret = kstrtoint(page, 0, &port->inline_data_size);
267 	if (ret) {
268 		pr_err("Invalid value '%s' for inline_data_size\n", page);
269 		return -EINVAL;
270 	}
271 	return count;
272 }
273 
274 CONFIGFS_ATTR(nvmet_, param_inline_data_size);
275 
276 static ssize_t nvmet_param_max_queue_size_show(struct config_item *item,
277 		char *page)
278 {
279 	struct nvmet_port *port = to_nvmet_port(item);
280 
281 	return snprintf(page, PAGE_SIZE, "%d\n", port->max_queue_size);
282 }
283 
284 static ssize_t nvmet_param_max_queue_size_store(struct config_item *item,
285 		const char *page, size_t count)
286 {
287 	struct nvmet_port *port = to_nvmet_port(item);
288 	int ret;
289 
290 	if (nvmet_is_port_enabled(port, __func__))
291 		return -EACCES;
292 	ret = kstrtoint(page, 0, &port->max_queue_size);
293 	if (ret) {
294 		pr_err("Invalid value '%s' for max_queue_size\n", page);
295 		return -EINVAL;
296 	}
297 	return count;
298 }
299 
300 CONFIGFS_ATTR(nvmet_, param_max_queue_size);
301 
302 #ifdef CONFIG_BLK_DEV_INTEGRITY
303 static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
304 		char *page)
305 {
306 	struct nvmet_port *port = to_nvmet_port(item);
307 
308 	return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
309 }
310 
311 static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
312 		const char *page, size_t count)
313 {
314 	struct nvmet_port *port = to_nvmet_port(item);
315 	bool val;
316 
317 	if (kstrtobool(page, &val))
318 		return -EINVAL;
319 
320 	if (nvmet_is_port_enabled(port, __func__))
321 		return -EACCES;
322 
323 	port->pi_enable = val;
324 	return count;
325 }
326 
327 CONFIGFS_ATTR(nvmet_, param_pi_enable);
328 #endif
329 
330 static ssize_t nvmet_addr_trtype_show(struct config_item *item,
331 		char *page)
332 {
333 	struct nvmet_port *port = to_nvmet_port(item);
334 	int i;
335 
336 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
337 		if (port->disc_addr.trtype == nvmet_transport[i].type)
338 			return snprintf(page, PAGE_SIZE,
339 					"%s\n", nvmet_transport[i].name);
340 	}
341 
342 	return sprintf(page, "\n");
343 }
344 
345 static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
346 {
347 	port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
348 	port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
349 	port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
350 }
351 
352 static void nvmet_port_init_tsas_tcp(struct nvmet_port *port, int sectype)
353 {
354 	port->disc_addr.tsas.tcp.sectype = sectype;
355 }
356 
357 static ssize_t nvmet_addr_trtype_store(struct config_item *item,
358 		const char *page, size_t count)
359 {
360 	struct nvmet_port *port = to_nvmet_port(item);
361 	int i;
362 
363 	if (nvmet_is_port_enabled(port, __func__))
364 		return -EACCES;
365 
366 	for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
367 		if (sysfs_streq(page, nvmet_transport[i].name))
368 			goto found;
369 	}
370 
371 	pr_err("Invalid value '%s' for trtype\n", page);
372 	return -EINVAL;
373 
374 found:
375 	memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
376 	port->disc_addr.trtype = nvmet_transport[i].type;
377 	if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
378 		nvmet_port_init_tsas_rdma(port);
379 	else if (port->disc_addr.trtype == NVMF_TRTYPE_TCP)
380 		nvmet_port_init_tsas_tcp(port, NVMF_TCP_SECTYPE_NONE);
381 	return count;
382 }
383 
384 CONFIGFS_ATTR(nvmet_, addr_trtype);
385 
386 static const struct nvmet_type_name_map nvmet_addr_tsas_tcp[] = {
387 	{ NVMF_TCP_SECTYPE_NONE,	"none" },
388 	{ NVMF_TCP_SECTYPE_TLS13,	"tls1.3" },
389 };
390 
391 static const struct nvmet_type_name_map nvmet_addr_tsas_rdma[] = {
392 	{ NVMF_RDMA_QPTYPE_CONNECTED,	"connected" },
393 	{ NVMF_RDMA_QPTYPE_DATAGRAM,	"datagram"  },
394 };
395 
396 static ssize_t nvmet_addr_tsas_show(struct config_item *item,
397 		char *page)
398 {
399 	struct nvmet_port *port = to_nvmet_port(item);
400 	int i;
401 
402 	if (port->disc_addr.trtype == NVMF_TRTYPE_TCP) {
403 		for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_tcp); i++) {
404 			if (port->disc_addr.tsas.tcp.sectype == nvmet_addr_tsas_tcp[i].type)
405 				return sprintf(page, "%s\n", nvmet_addr_tsas_tcp[i].name);
406 		}
407 	} else if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA) {
408 		for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_rdma); i++) {
409 			if (port->disc_addr.tsas.rdma.qptype == nvmet_addr_tsas_rdma[i].type)
410 				return sprintf(page, "%s\n", nvmet_addr_tsas_rdma[i].name);
411 		}
412 	}
413 	return sprintf(page, "reserved\n");
414 }
415 
416 static ssize_t nvmet_addr_tsas_store(struct config_item *item,
417 		const char *page, size_t count)
418 {
419 	struct nvmet_port *port = to_nvmet_port(item);
420 	u8 treq = nvmet_port_disc_addr_treq_mask(port);
421 	u8 sectype;
422 	int i;
423 
424 	if (nvmet_is_port_enabled(port, __func__))
425 		return -EACCES;
426 
427 	if (port->disc_addr.trtype != NVMF_TRTYPE_TCP)
428 		return -EINVAL;
429 
430 	for (i = 0; i < ARRAY_SIZE(nvmet_addr_tsas_tcp); i++) {
431 		if (sysfs_streq(page, nvmet_addr_tsas_tcp[i].name)) {
432 			sectype = nvmet_addr_tsas_tcp[i].type;
433 			goto found;
434 		}
435 	}
436 
437 	pr_err("Invalid value '%s' for tsas\n", page);
438 	return -EINVAL;
439 
440 found:
441 	if (sectype == NVMF_TCP_SECTYPE_TLS13) {
442 		if (!IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS)) {
443 			pr_err("TLS is not supported\n");
444 			return -EINVAL;
445 		}
446 		if (!port->keyring) {
447 			pr_err("TLS keyring not configured\n");
448 			return -EINVAL;
449 		}
450 	}
451 
452 	nvmet_port_init_tsas_tcp(port, sectype);
453 	/*
454 	 * If TLS is enabled TREQ should be set to 'required' per default
455 	 */
456 	if (sectype == NVMF_TCP_SECTYPE_TLS13) {
457 		u8 sc = nvmet_port_disc_addr_treq_secure_channel(port);
458 
459 		if (sc == NVMF_TREQ_NOT_SPECIFIED)
460 			treq |= NVMF_TREQ_REQUIRED;
461 		else
462 			treq |= sc;
463 	} else {
464 		treq |= NVMF_TREQ_NOT_SPECIFIED;
465 	}
466 	port->disc_addr.treq = treq;
467 	return count;
468 }
469 
470 CONFIGFS_ATTR(nvmet_, addr_tsas);
471 
472 /*
473  * Namespace structures & file operation functions below
474  */
475 static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
476 {
477 	return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
478 }
479 
480 static ssize_t nvmet_ns_device_path_store(struct config_item *item,
481 		const char *page, size_t count)
482 {
483 	struct nvmet_ns *ns = to_nvmet_ns(item);
484 	struct nvmet_subsys *subsys = ns->subsys;
485 	size_t len;
486 	int ret;
487 
488 	mutex_lock(&subsys->lock);
489 	ret = -EBUSY;
490 	if (ns->enabled)
491 		goto out_unlock;
492 
493 	ret = -EINVAL;
494 	len = strcspn(page, "\n");
495 	if (!len)
496 		goto out_unlock;
497 
498 	kfree(ns->device_path);
499 	ret = -ENOMEM;
500 	ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
501 	if (!ns->device_path)
502 		goto out_unlock;
503 
504 	mutex_unlock(&subsys->lock);
505 	return count;
506 
507 out_unlock:
508 	mutex_unlock(&subsys->lock);
509 	return ret;
510 }
511 
512 CONFIGFS_ATTR(nvmet_ns_, device_path);
513 
514 #ifdef CONFIG_PCI_P2PDMA
515 static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
516 {
517 	struct nvmet_ns *ns = to_nvmet_ns(item);
518 
519 	return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
520 }
521 
522 static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
523 		const char *page, size_t count)
524 {
525 	struct nvmet_ns *ns = to_nvmet_ns(item);
526 	struct pci_dev *p2p_dev = NULL;
527 	bool use_p2pmem;
528 	int ret = count;
529 	int error;
530 
531 	mutex_lock(&ns->subsys->lock);
532 	if (ns->enabled) {
533 		ret = -EBUSY;
534 		goto out_unlock;
535 	}
536 
537 	error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
538 	if (error) {
539 		ret = error;
540 		goto out_unlock;
541 	}
542 
543 	ns->use_p2pmem = use_p2pmem;
544 	pci_dev_put(ns->p2p_dev);
545 	ns->p2p_dev = p2p_dev;
546 
547 out_unlock:
548 	mutex_unlock(&ns->subsys->lock);
549 
550 	return ret;
551 }
552 
553 CONFIGFS_ATTR(nvmet_ns_, p2pmem);
554 #endif /* CONFIG_PCI_P2PDMA */
555 
556 static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
557 {
558 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
559 }
560 
561 static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
562 					  const char *page, size_t count)
563 {
564 	struct nvmet_ns *ns = to_nvmet_ns(item);
565 	struct nvmet_subsys *subsys = ns->subsys;
566 	int ret = 0;
567 
568 	mutex_lock(&subsys->lock);
569 	if (ns->enabled) {
570 		ret = -EBUSY;
571 		goto out_unlock;
572 	}
573 
574 	if (uuid_parse(page, &ns->uuid))
575 		ret = -EINVAL;
576 
577 out_unlock:
578 	mutex_unlock(&subsys->lock);
579 	return ret ? ret : count;
580 }
581 
582 CONFIGFS_ATTR(nvmet_ns_, device_uuid);
583 
584 static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
585 {
586 	return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
587 }
588 
589 static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
590 		const char *page, size_t count)
591 {
592 	struct nvmet_ns *ns = to_nvmet_ns(item);
593 	struct nvmet_subsys *subsys = ns->subsys;
594 	u8 nguid[16];
595 	const char *p = page;
596 	int i;
597 	int ret = 0;
598 
599 	mutex_lock(&subsys->lock);
600 	if (ns->enabled) {
601 		ret = -EBUSY;
602 		goto out_unlock;
603 	}
604 
605 	for (i = 0; i < 16; i++) {
606 		if (p + 2 > page + count) {
607 			ret = -EINVAL;
608 			goto out_unlock;
609 		}
610 		if (!isxdigit(p[0]) || !isxdigit(p[1])) {
611 			ret = -EINVAL;
612 			goto out_unlock;
613 		}
614 
615 		nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
616 		p += 2;
617 
618 		if (*p == '-' || *p == ':')
619 			p++;
620 	}
621 
622 	memcpy(&ns->nguid, nguid, sizeof(nguid));
623 out_unlock:
624 	mutex_unlock(&subsys->lock);
625 	return ret ? ret : count;
626 }
627 
628 CONFIGFS_ATTR(nvmet_ns_, device_nguid);
629 
630 static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
631 {
632 	return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
633 }
634 
635 static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
636 		const char *page, size_t count)
637 {
638 	struct nvmet_ns *ns = to_nvmet_ns(item);
639 	u32 oldgrpid, newgrpid;
640 	int ret;
641 
642 	ret = kstrtou32(page, 0, &newgrpid);
643 	if (ret)
644 		return ret;
645 
646 	if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
647 		return -EINVAL;
648 
649 	down_write(&nvmet_ana_sem);
650 	oldgrpid = ns->anagrpid;
651 	newgrpid = array_index_nospec(newgrpid, NVMET_MAX_ANAGRPS);
652 	nvmet_ana_group_enabled[newgrpid]++;
653 	ns->anagrpid = newgrpid;
654 	nvmet_ana_group_enabled[oldgrpid]--;
655 	nvmet_ana_chgcnt++;
656 	up_write(&nvmet_ana_sem);
657 
658 	nvmet_send_ana_event(ns->subsys, NULL);
659 	return count;
660 }
661 
662 CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
663 
664 static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
665 {
666 	return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
667 }
668 
669 static ssize_t nvmet_ns_enable_store(struct config_item *item,
670 		const char *page, size_t count)
671 {
672 	struct nvmet_ns *ns = to_nvmet_ns(item);
673 	bool enable;
674 	int ret = 0;
675 
676 	if (kstrtobool(page, &enable))
677 		return -EINVAL;
678 
679 	if (enable)
680 		ret = nvmet_ns_enable(ns);
681 	else
682 		nvmet_ns_disable(ns);
683 
684 	return ret ? ret : count;
685 }
686 
687 CONFIGFS_ATTR(nvmet_ns_, enable);
688 
689 static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
690 {
691 	return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
692 }
693 
694 static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
695 		const char *page, size_t count)
696 {
697 	struct nvmet_ns *ns = to_nvmet_ns(item);
698 	bool val;
699 
700 	if (kstrtobool(page, &val))
701 		return -EINVAL;
702 
703 	mutex_lock(&ns->subsys->lock);
704 	if (ns->enabled) {
705 		pr_err("disable ns before setting buffered_io value.\n");
706 		mutex_unlock(&ns->subsys->lock);
707 		return -EINVAL;
708 	}
709 
710 	ns->buffered_io = val;
711 	mutex_unlock(&ns->subsys->lock);
712 	return count;
713 }
714 
715 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
716 
717 static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
718 		const char *page, size_t count)
719 {
720 	struct nvmet_ns *ns = to_nvmet_ns(item);
721 	bool val;
722 
723 	if (kstrtobool(page, &val))
724 		return -EINVAL;
725 
726 	if (!val)
727 		return -EINVAL;
728 
729 	mutex_lock(&ns->subsys->lock);
730 	if (!ns->enabled) {
731 		pr_err("enable ns before revalidate.\n");
732 		mutex_unlock(&ns->subsys->lock);
733 		return -EINVAL;
734 	}
735 	if (nvmet_ns_revalidate(ns))
736 		nvmet_ns_changed(ns->subsys, ns->nsid);
737 	mutex_unlock(&ns->subsys->lock);
738 	return count;
739 }
740 
741 CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
742 
743 static struct configfs_attribute *nvmet_ns_attrs[] = {
744 	&nvmet_ns_attr_device_path,
745 	&nvmet_ns_attr_device_nguid,
746 	&nvmet_ns_attr_device_uuid,
747 	&nvmet_ns_attr_ana_grpid,
748 	&nvmet_ns_attr_enable,
749 	&nvmet_ns_attr_buffered_io,
750 	&nvmet_ns_attr_revalidate_size,
751 #ifdef CONFIG_PCI_P2PDMA
752 	&nvmet_ns_attr_p2pmem,
753 #endif
754 	NULL,
755 };
756 
757 bool nvmet_subsys_nsid_exists(struct nvmet_subsys *subsys, u32 nsid)
758 {
759 	struct config_item *ns_item;
760 	char name[4] = {};
761 
762 	if (sprintf(name, "%u", nsid) <= 0)
763 		return false;
764 	mutex_lock(&subsys->namespaces_group.cg_subsys->su_mutex);
765 	ns_item = config_group_find_item(&subsys->namespaces_group, name);
766 	mutex_unlock(&subsys->namespaces_group.cg_subsys->su_mutex);
767 	return ns_item != NULL;
768 }
769 
770 static void nvmet_ns_release(struct config_item *item)
771 {
772 	struct nvmet_ns *ns = to_nvmet_ns(item);
773 
774 	nvmet_ns_free(ns);
775 }
776 
777 static struct configfs_item_operations nvmet_ns_item_ops = {
778 	.release		= nvmet_ns_release,
779 };
780 
781 static const struct config_item_type nvmet_ns_type = {
782 	.ct_item_ops		= &nvmet_ns_item_ops,
783 	.ct_attrs		= nvmet_ns_attrs,
784 	.ct_owner		= THIS_MODULE,
785 };
786 
787 static struct config_group *nvmet_ns_make(struct config_group *group,
788 		const char *name)
789 {
790 	struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
791 	struct nvmet_ns *ns;
792 	int ret;
793 	u32 nsid;
794 
795 	ret = kstrtou32(name, 0, &nsid);
796 	if (ret)
797 		goto out;
798 
799 	ret = -EINVAL;
800 	if (nsid == 0 || nsid == NVME_NSID_ALL) {
801 		pr_err("invalid nsid %#x", nsid);
802 		goto out;
803 	}
804 
805 	ret = -ENOMEM;
806 	ns = nvmet_ns_alloc(subsys, nsid);
807 	if (!ns)
808 		goto out;
809 	config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
810 
811 	pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
812 
813 	return &ns->group;
814 out:
815 	return ERR_PTR(ret);
816 }
817 
818 static struct configfs_group_operations nvmet_namespaces_group_ops = {
819 	.make_group		= nvmet_ns_make,
820 };
821 
822 static const struct config_item_type nvmet_namespaces_type = {
823 	.ct_group_ops		= &nvmet_namespaces_group_ops,
824 	.ct_owner		= THIS_MODULE,
825 };
826 
827 #ifdef CONFIG_NVME_TARGET_PASSTHRU
828 
829 static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
830 		char *page)
831 {
832 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
833 
834 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
835 }
836 
837 static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
838 		const char *page, size_t count)
839 {
840 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
841 	size_t len;
842 	int ret;
843 
844 	mutex_lock(&subsys->lock);
845 
846 	ret = -EBUSY;
847 	if (subsys->passthru_ctrl)
848 		goto out_unlock;
849 
850 	ret = -EINVAL;
851 	len = strcspn(page, "\n");
852 	if (!len)
853 		goto out_unlock;
854 
855 	kfree(subsys->passthru_ctrl_path);
856 	ret = -ENOMEM;
857 	subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
858 	if (!subsys->passthru_ctrl_path)
859 		goto out_unlock;
860 
861 	mutex_unlock(&subsys->lock);
862 
863 	return count;
864 out_unlock:
865 	mutex_unlock(&subsys->lock);
866 	return ret;
867 }
868 CONFIGFS_ATTR(nvmet_passthru_, device_path);
869 
870 static ssize_t nvmet_passthru_enable_show(struct config_item *item,
871 		char *page)
872 {
873 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
874 
875 	return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
876 }
877 
878 static ssize_t nvmet_passthru_enable_store(struct config_item *item,
879 		const char *page, size_t count)
880 {
881 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
882 	bool enable;
883 	int ret = 0;
884 
885 	if (kstrtobool(page, &enable))
886 		return -EINVAL;
887 
888 	if (enable)
889 		ret = nvmet_passthru_ctrl_enable(subsys);
890 	else
891 		nvmet_passthru_ctrl_disable(subsys);
892 
893 	return ret ? ret : count;
894 }
895 CONFIGFS_ATTR(nvmet_passthru_, enable);
896 
897 static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
898 		char *page)
899 {
900 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->admin_timeout);
901 }
902 
903 static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
904 		const char *page, size_t count)
905 {
906 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
907 	unsigned int timeout;
908 
909 	if (kstrtouint(page, 0, &timeout))
910 		return -EINVAL;
911 	subsys->admin_timeout = timeout;
912 	return count;
913 }
914 CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
915 
916 static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
917 		char *page)
918 {
919 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->io_timeout);
920 }
921 
922 static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
923 		const char *page, size_t count)
924 {
925 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
926 	unsigned int timeout;
927 
928 	if (kstrtouint(page, 0, &timeout))
929 		return -EINVAL;
930 	subsys->io_timeout = timeout;
931 	return count;
932 }
933 CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
934 
935 static ssize_t nvmet_passthru_clear_ids_show(struct config_item *item,
936 		char *page)
937 {
938 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->clear_ids);
939 }
940 
941 static ssize_t nvmet_passthru_clear_ids_store(struct config_item *item,
942 		const char *page, size_t count)
943 {
944 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
945 	unsigned int clear_ids;
946 
947 	if (kstrtouint(page, 0, &clear_ids))
948 		return -EINVAL;
949 	subsys->clear_ids = clear_ids;
950 	return count;
951 }
952 CONFIGFS_ATTR(nvmet_passthru_, clear_ids);
953 
954 static struct configfs_attribute *nvmet_passthru_attrs[] = {
955 	&nvmet_passthru_attr_device_path,
956 	&nvmet_passthru_attr_enable,
957 	&nvmet_passthru_attr_admin_timeout,
958 	&nvmet_passthru_attr_io_timeout,
959 	&nvmet_passthru_attr_clear_ids,
960 	NULL,
961 };
962 
963 static const struct config_item_type nvmet_passthru_type = {
964 	.ct_attrs		= nvmet_passthru_attrs,
965 	.ct_owner		= THIS_MODULE,
966 };
967 
968 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
969 {
970 	config_group_init_type_name(&subsys->passthru_group,
971 				    "passthru", &nvmet_passthru_type);
972 	configfs_add_default_group(&subsys->passthru_group,
973 				   &subsys->group);
974 }
975 
976 #else /* CONFIG_NVME_TARGET_PASSTHRU */
977 
978 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
979 {
980 }
981 
982 #endif /* CONFIG_NVME_TARGET_PASSTHRU */
983 
984 static int nvmet_port_subsys_allow_link(struct config_item *parent,
985 		struct config_item *target)
986 {
987 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
988 	struct nvmet_subsys *subsys;
989 	struct nvmet_subsys_link *link, *p;
990 	int ret;
991 
992 	if (target->ci_type != &nvmet_subsys_type) {
993 		pr_err("can only link subsystems into the subsystems dir.!\n");
994 		return -EINVAL;
995 	}
996 	subsys = to_subsys(target);
997 	link = kmalloc(sizeof(*link), GFP_KERNEL);
998 	if (!link)
999 		return -ENOMEM;
1000 	link->subsys = subsys;
1001 
1002 	down_write(&nvmet_config_sem);
1003 	ret = -EEXIST;
1004 	list_for_each_entry(p, &port->subsystems, entry) {
1005 		if (p->subsys == subsys)
1006 			goto out_free_link;
1007 	}
1008 
1009 	if (list_empty(&port->subsystems)) {
1010 		ret = nvmet_enable_port(port);
1011 		if (ret)
1012 			goto out_free_link;
1013 	}
1014 
1015 	list_add_tail(&link->entry, &port->subsystems);
1016 	nvmet_port_disc_changed(port, subsys);
1017 
1018 	up_write(&nvmet_config_sem);
1019 	return 0;
1020 
1021 out_free_link:
1022 	up_write(&nvmet_config_sem);
1023 	kfree(link);
1024 	return ret;
1025 }
1026 
1027 static void nvmet_port_subsys_drop_link(struct config_item *parent,
1028 		struct config_item *target)
1029 {
1030 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
1031 	struct nvmet_subsys *subsys = to_subsys(target);
1032 	struct nvmet_subsys_link *p;
1033 
1034 	down_write(&nvmet_config_sem);
1035 	list_for_each_entry(p, &port->subsystems, entry) {
1036 		if (p->subsys == subsys)
1037 			goto found;
1038 	}
1039 	up_write(&nvmet_config_sem);
1040 	return;
1041 
1042 found:
1043 	list_del(&p->entry);
1044 	nvmet_port_del_ctrls(port, subsys);
1045 	nvmet_port_disc_changed(port, subsys);
1046 
1047 	if (list_empty(&port->subsystems))
1048 		nvmet_disable_port(port);
1049 	up_write(&nvmet_config_sem);
1050 	kfree(p);
1051 }
1052 
1053 static struct configfs_item_operations nvmet_port_subsys_item_ops = {
1054 	.allow_link		= nvmet_port_subsys_allow_link,
1055 	.drop_link		= nvmet_port_subsys_drop_link,
1056 };
1057 
1058 static const struct config_item_type nvmet_port_subsys_type = {
1059 	.ct_item_ops		= &nvmet_port_subsys_item_ops,
1060 	.ct_owner		= THIS_MODULE,
1061 };
1062 
1063 static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
1064 		struct config_item *target)
1065 {
1066 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
1067 	struct nvmet_host *host;
1068 	struct nvmet_host_link *link, *p;
1069 	int ret;
1070 
1071 	if (target->ci_type != &nvmet_host_type) {
1072 		pr_err("can only link hosts into the allowed_hosts directory!\n");
1073 		return -EINVAL;
1074 	}
1075 
1076 	host = to_host(target);
1077 	link = kmalloc(sizeof(*link), GFP_KERNEL);
1078 	if (!link)
1079 		return -ENOMEM;
1080 	link->host = host;
1081 
1082 	down_write(&nvmet_config_sem);
1083 	ret = -EINVAL;
1084 	if (subsys->allow_any_host) {
1085 		pr_err("can't add hosts when allow_any_host is set!\n");
1086 		goto out_free_link;
1087 	}
1088 
1089 	ret = -EEXIST;
1090 	list_for_each_entry(p, &subsys->hosts, entry) {
1091 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
1092 			goto out_free_link;
1093 	}
1094 	list_add_tail(&link->entry, &subsys->hosts);
1095 	nvmet_subsys_disc_changed(subsys, host);
1096 
1097 	up_write(&nvmet_config_sem);
1098 	return 0;
1099 out_free_link:
1100 	up_write(&nvmet_config_sem);
1101 	kfree(link);
1102 	return ret;
1103 }
1104 
1105 static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
1106 		struct config_item *target)
1107 {
1108 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
1109 	struct nvmet_host *host = to_host(target);
1110 	struct nvmet_host_link *p;
1111 
1112 	down_write(&nvmet_config_sem);
1113 	list_for_each_entry(p, &subsys->hosts, entry) {
1114 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
1115 			goto found;
1116 	}
1117 	up_write(&nvmet_config_sem);
1118 	return;
1119 
1120 found:
1121 	list_del(&p->entry);
1122 	nvmet_subsys_disc_changed(subsys, host);
1123 
1124 	up_write(&nvmet_config_sem);
1125 	kfree(p);
1126 }
1127 
1128 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
1129 	.allow_link		= nvmet_allowed_hosts_allow_link,
1130 	.drop_link		= nvmet_allowed_hosts_drop_link,
1131 };
1132 
1133 static const struct config_item_type nvmet_allowed_hosts_type = {
1134 	.ct_item_ops		= &nvmet_allowed_hosts_item_ops,
1135 	.ct_owner		= THIS_MODULE,
1136 };
1137 
1138 static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
1139 		char *page)
1140 {
1141 	return snprintf(page, PAGE_SIZE, "%d\n",
1142 		to_subsys(item)->allow_any_host);
1143 }
1144 
1145 static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
1146 		const char *page, size_t count)
1147 {
1148 	struct nvmet_subsys *subsys = to_subsys(item);
1149 	bool allow_any_host;
1150 	int ret = 0;
1151 
1152 	if (kstrtobool(page, &allow_any_host))
1153 		return -EINVAL;
1154 
1155 	down_write(&nvmet_config_sem);
1156 	if (allow_any_host && !list_empty(&subsys->hosts)) {
1157 		pr_err("Can't set allow_any_host when explicit hosts are set!\n");
1158 		ret = -EINVAL;
1159 		goto out_unlock;
1160 	}
1161 
1162 	if (subsys->allow_any_host != allow_any_host) {
1163 		subsys->allow_any_host = allow_any_host;
1164 		nvmet_subsys_disc_changed(subsys, NULL);
1165 	}
1166 
1167 out_unlock:
1168 	up_write(&nvmet_config_sem);
1169 	return ret ? ret : count;
1170 }
1171 
1172 CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
1173 
1174 static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
1175 					      char *page)
1176 {
1177 	struct nvmet_subsys *subsys = to_subsys(item);
1178 
1179 	if (NVME_TERTIARY(subsys->ver))
1180 		return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
1181 				NVME_MAJOR(subsys->ver),
1182 				NVME_MINOR(subsys->ver),
1183 				NVME_TERTIARY(subsys->ver));
1184 
1185 	return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
1186 			NVME_MAJOR(subsys->ver),
1187 			NVME_MINOR(subsys->ver));
1188 }
1189 
1190 static ssize_t
1191 nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,
1192 		const char *page, size_t count)
1193 {
1194 	int major, minor, tertiary = 0;
1195 	int ret;
1196 
1197 	if (subsys->subsys_discovered) {
1198 		if (NVME_TERTIARY(subsys->ver))
1199 			pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
1200 			       NVME_MAJOR(subsys->ver),
1201 			       NVME_MINOR(subsys->ver),
1202 			       NVME_TERTIARY(subsys->ver));
1203 		else
1204 			pr_err("Can't set version number. %llu.%llu is already assigned\n",
1205 			       NVME_MAJOR(subsys->ver),
1206 			       NVME_MINOR(subsys->ver));
1207 		return -EINVAL;
1208 	}
1209 
1210 	/* passthru subsystems use the underlying controller's version */
1211 	if (nvmet_is_passthru_subsys(subsys))
1212 		return -EINVAL;
1213 
1214 	ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
1215 	if (ret != 2 && ret != 3)
1216 		return -EINVAL;
1217 
1218 	subsys->ver = NVME_VS(major, minor, tertiary);
1219 
1220 	return count;
1221 }
1222 
1223 static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
1224 					       const char *page, size_t count)
1225 {
1226 	struct nvmet_subsys *subsys = to_subsys(item);
1227 	ssize_t ret;
1228 
1229 	down_write(&nvmet_config_sem);
1230 	mutex_lock(&subsys->lock);
1231 	ret = nvmet_subsys_attr_version_store_locked(subsys, page, count);
1232 	mutex_unlock(&subsys->lock);
1233 	up_write(&nvmet_config_sem);
1234 
1235 	return ret;
1236 }
1237 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
1238 
1239 /* See Section 1.5 of NVMe 1.4 */
1240 static bool nvmet_is_ascii(const char c)
1241 {
1242 	return c >= 0x20 && c <= 0x7e;
1243 }
1244 
1245 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
1246 					     char *page)
1247 {
1248 	struct nvmet_subsys *subsys = to_subsys(item);
1249 
1250 	return snprintf(page, PAGE_SIZE, "%.*s\n",
1251 			NVMET_SN_MAX_SIZE, subsys->serial);
1252 }
1253 
1254 static ssize_t
1255 nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
1256 		const char *page, size_t count)
1257 {
1258 	int pos, len = strcspn(page, "\n");
1259 
1260 	if (subsys->subsys_discovered) {
1261 		pr_err("Can't set serial number. %s is already assigned\n",
1262 		       subsys->serial);
1263 		return -EINVAL;
1264 	}
1265 
1266 	if (!len || len > NVMET_SN_MAX_SIZE) {
1267 		pr_err("Serial Number can not be empty or exceed %d Bytes\n",
1268 		       NVMET_SN_MAX_SIZE);
1269 		return -EINVAL;
1270 	}
1271 
1272 	for (pos = 0; pos < len; pos++) {
1273 		if (!nvmet_is_ascii(page[pos])) {
1274 			pr_err("Serial Number must contain only ASCII strings\n");
1275 			return -EINVAL;
1276 		}
1277 	}
1278 
1279 	memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
1280 
1281 	return count;
1282 }
1283 
1284 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
1285 					      const char *page, size_t count)
1286 {
1287 	struct nvmet_subsys *subsys = to_subsys(item);
1288 	ssize_t ret;
1289 
1290 	down_write(&nvmet_config_sem);
1291 	mutex_lock(&subsys->lock);
1292 	ret = nvmet_subsys_attr_serial_store_locked(subsys, page, count);
1293 	mutex_unlock(&subsys->lock);
1294 	up_write(&nvmet_config_sem);
1295 
1296 	return ret;
1297 }
1298 CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
1299 
1300 static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
1301 						 char *page)
1302 {
1303 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
1304 }
1305 
1306 static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
1307 						  const char *page, size_t cnt)
1308 {
1309 	u16 cntlid_min;
1310 
1311 	if (sscanf(page, "%hu\n", &cntlid_min) != 1)
1312 		return -EINVAL;
1313 
1314 	if (cntlid_min == 0)
1315 		return -EINVAL;
1316 
1317 	down_write(&nvmet_config_sem);
1318 	if (cntlid_min > to_subsys(item)->cntlid_max)
1319 		goto out_unlock;
1320 	to_subsys(item)->cntlid_min = cntlid_min;
1321 	up_write(&nvmet_config_sem);
1322 	return cnt;
1323 
1324 out_unlock:
1325 	up_write(&nvmet_config_sem);
1326 	return -EINVAL;
1327 }
1328 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
1329 
1330 static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
1331 						 char *page)
1332 {
1333 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
1334 }
1335 
1336 static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
1337 						  const char *page, size_t cnt)
1338 {
1339 	u16 cntlid_max;
1340 
1341 	if (sscanf(page, "%hu\n", &cntlid_max) != 1)
1342 		return -EINVAL;
1343 
1344 	if (cntlid_max == 0)
1345 		return -EINVAL;
1346 
1347 	down_write(&nvmet_config_sem);
1348 	if (cntlid_max < to_subsys(item)->cntlid_min)
1349 		goto out_unlock;
1350 	to_subsys(item)->cntlid_max = cntlid_max;
1351 	up_write(&nvmet_config_sem);
1352 	return cnt;
1353 
1354 out_unlock:
1355 	up_write(&nvmet_config_sem);
1356 	return -EINVAL;
1357 }
1358 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
1359 
1360 static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
1361 					    char *page)
1362 {
1363 	struct nvmet_subsys *subsys = to_subsys(item);
1364 
1365 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->model_number);
1366 }
1367 
1368 static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
1369 		const char *page, size_t count)
1370 {
1371 	int pos = 0, len;
1372 	char *val;
1373 
1374 	if (subsys->subsys_discovered) {
1375 		pr_err("Can't set model number. %s is already assigned\n",
1376 		       subsys->model_number);
1377 		return -EINVAL;
1378 	}
1379 
1380 	len = strcspn(page, "\n");
1381 	if (!len)
1382 		return -EINVAL;
1383 
1384 	if (len > NVMET_MN_MAX_SIZE) {
1385 		pr_err("Model number size can not exceed %d Bytes\n",
1386 		       NVMET_MN_MAX_SIZE);
1387 		return -EINVAL;
1388 	}
1389 
1390 	for (pos = 0; pos < len; pos++) {
1391 		if (!nvmet_is_ascii(page[pos]))
1392 			return -EINVAL;
1393 	}
1394 
1395 	val = kmemdup_nul(page, len, GFP_KERNEL);
1396 	if (!val)
1397 		return -ENOMEM;
1398 	kfree(subsys->model_number);
1399 	subsys->model_number = val;
1400 	return count;
1401 }
1402 
1403 static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
1404 					     const char *page, size_t count)
1405 {
1406 	struct nvmet_subsys *subsys = to_subsys(item);
1407 	ssize_t ret;
1408 
1409 	down_write(&nvmet_config_sem);
1410 	mutex_lock(&subsys->lock);
1411 	ret = nvmet_subsys_attr_model_store_locked(subsys, page, count);
1412 	mutex_unlock(&subsys->lock);
1413 	up_write(&nvmet_config_sem);
1414 
1415 	return ret;
1416 }
1417 CONFIGFS_ATTR(nvmet_subsys_, attr_model);
1418 
1419 static ssize_t nvmet_subsys_attr_ieee_oui_show(struct config_item *item,
1420 					    char *page)
1421 {
1422 	struct nvmet_subsys *subsys = to_subsys(item);
1423 
1424 	return sysfs_emit(page, "0x%06x\n", subsys->ieee_oui);
1425 }
1426 
1427 static ssize_t nvmet_subsys_attr_ieee_oui_store_locked(struct nvmet_subsys *subsys,
1428 		const char *page, size_t count)
1429 {
1430 	uint32_t val = 0;
1431 	int ret;
1432 
1433 	if (subsys->subsys_discovered) {
1434 		pr_err("Can't set IEEE OUI. 0x%06x is already assigned\n",
1435 		      subsys->ieee_oui);
1436 		return -EINVAL;
1437 	}
1438 
1439 	ret = kstrtou32(page, 0, &val);
1440 	if (ret < 0)
1441 		return ret;
1442 
1443 	if (val >= 0x1000000)
1444 		return -EINVAL;
1445 
1446 	subsys->ieee_oui = val;
1447 
1448 	return count;
1449 }
1450 
1451 static ssize_t nvmet_subsys_attr_ieee_oui_store(struct config_item *item,
1452 					     const char *page, size_t count)
1453 {
1454 	struct nvmet_subsys *subsys = to_subsys(item);
1455 	ssize_t ret;
1456 
1457 	down_write(&nvmet_config_sem);
1458 	mutex_lock(&subsys->lock);
1459 	ret = nvmet_subsys_attr_ieee_oui_store_locked(subsys, page, count);
1460 	mutex_unlock(&subsys->lock);
1461 	up_write(&nvmet_config_sem);
1462 
1463 	return ret;
1464 }
1465 CONFIGFS_ATTR(nvmet_subsys_, attr_ieee_oui);
1466 
1467 static ssize_t nvmet_subsys_attr_firmware_show(struct config_item *item,
1468 					    char *page)
1469 {
1470 	struct nvmet_subsys *subsys = to_subsys(item);
1471 
1472 	return sysfs_emit(page, "%s\n", subsys->firmware_rev);
1473 }
1474 
1475 static ssize_t nvmet_subsys_attr_firmware_store_locked(struct nvmet_subsys *subsys,
1476 		const char *page, size_t count)
1477 {
1478 	int pos = 0, len;
1479 	char *val;
1480 
1481 	if (subsys->subsys_discovered) {
1482 		pr_err("Can't set firmware revision. %s is already assigned\n",
1483 		       subsys->firmware_rev);
1484 		return -EINVAL;
1485 	}
1486 
1487 	len = strcspn(page, "\n");
1488 	if (!len)
1489 		return -EINVAL;
1490 
1491 	if (len > NVMET_FR_MAX_SIZE) {
1492 		pr_err("Firmware revision size can not exceed %d Bytes\n",
1493 		       NVMET_FR_MAX_SIZE);
1494 		return -EINVAL;
1495 	}
1496 
1497 	for (pos = 0; pos < len; pos++) {
1498 		if (!nvmet_is_ascii(page[pos]))
1499 			return -EINVAL;
1500 	}
1501 
1502 	val = kmemdup_nul(page, len, GFP_KERNEL);
1503 	if (!val)
1504 		return -ENOMEM;
1505 
1506 	kfree(subsys->firmware_rev);
1507 
1508 	subsys->firmware_rev = val;
1509 
1510 	return count;
1511 }
1512 
1513 static ssize_t nvmet_subsys_attr_firmware_store(struct config_item *item,
1514 					     const char *page, size_t count)
1515 {
1516 	struct nvmet_subsys *subsys = to_subsys(item);
1517 	ssize_t ret;
1518 
1519 	down_write(&nvmet_config_sem);
1520 	mutex_lock(&subsys->lock);
1521 	ret = nvmet_subsys_attr_firmware_store_locked(subsys, page, count);
1522 	mutex_unlock(&subsys->lock);
1523 	up_write(&nvmet_config_sem);
1524 
1525 	return ret;
1526 }
1527 CONFIGFS_ATTR(nvmet_subsys_, attr_firmware);
1528 
1529 #ifdef CONFIG_BLK_DEV_INTEGRITY
1530 static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
1531 						char *page)
1532 {
1533 	return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
1534 }
1535 
1536 static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
1537 						 const char *page, size_t count)
1538 {
1539 	struct nvmet_subsys *subsys = to_subsys(item);
1540 	bool pi_enable;
1541 
1542 	if (kstrtobool(page, &pi_enable))
1543 		return -EINVAL;
1544 
1545 	subsys->pi_support = pi_enable;
1546 	return count;
1547 }
1548 CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
1549 #endif
1550 
1551 static ssize_t nvmet_subsys_attr_qid_max_show(struct config_item *item,
1552 					      char *page)
1553 {
1554 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->max_qid);
1555 }
1556 
1557 static ssize_t nvmet_subsys_attr_qid_max_store(struct config_item *item,
1558 					       const char *page, size_t cnt)
1559 {
1560 	struct nvmet_subsys *subsys = to_subsys(item);
1561 	struct nvmet_ctrl *ctrl;
1562 	u16 qid_max;
1563 
1564 	if (sscanf(page, "%hu\n", &qid_max) != 1)
1565 		return -EINVAL;
1566 
1567 	if (qid_max < 1 || qid_max > NVMET_NR_QUEUES)
1568 		return -EINVAL;
1569 
1570 	down_write(&nvmet_config_sem);
1571 	subsys->max_qid = qid_max;
1572 
1573 	/* Force reconnect */
1574 	list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1575 		ctrl->ops->delete_ctrl(ctrl);
1576 	up_write(&nvmet_config_sem);
1577 
1578 	return cnt;
1579 }
1580 CONFIGFS_ATTR(nvmet_subsys_, attr_qid_max);
1581 
1582 static struct configfs_attribute *nvmet_subsys_attrs[] = {
1583 	&nvmet_subsys_attr_attr_allow_any_host,
1584 	&nvmet_subsys_attr_attr_version,
1585 	&nvmet_subsys_attr_attr_serial,
1586 	&nvmet_subsys_attr_attr_cntlid_min,
1587 	&nvmet_subsys_attr_attr_cntlid_max,
1588 	&nvmet_subsys_attr_attr_model,
1589 	&nvmet_subsys_attr_attr_qid_max,
1590 	&nvmet_subsys_attr_attr_ieee_oui,
1591 	&nvmet_subsys_attr_attr_firmware,
1592 #ifdef CONFIG_BLK_DEV_INTEGRITY
1593 	&nvmet_subsys_attr_attr_pi_enable,
1594 #endif
1595 	NULL,
1596 };
1597 
1598 /*
1599  * Subsystem structures & folder operation functions below
1600  */
1601 static void nvmet_subsys_release(struct config_item *item)
1602 {
1603 	struct nvmet_subsys *subsys = to_subsys(item);
1604 
1605 	nvmet_subsys_del_ctrls(subsys);
1606 	nvmet_subsys_put(subsys);
1607 }
1608 
1609 static struct configfs_item_operations nvmet_subsys_item_ops = {
1610 	.release		= nvmet_subsys_release,
1611 };
1612 
1613 static const struct config_item_type nvmet_subsys_type = {
1614 	.ct_item_ops		= &nvmet_subsys_item_ops,
1615 	.ct_attrs		= nvmet_subsys_attrs,
1616 	.ct_owner		= THIS_MODULE,
1617 };
1618 
1619 static struct config_group *nvmet_subsys_make(struct config_group *group,
1620 		const char *name)
1621 {
1622 	struct nvmet_subsys *subsys;
1623 
1624 	if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1625 		pr_err("can't create discovery subsystem through configfs\n");
1626 		return ERR_PTR(-EINVAL);
1627 	}
1628 
1629 	if (sysfs_streq(name, nvmet_disc_subsys->subsysnqn)) {
1630 		pr_err("can't create subsystem using unique discovery NQN\n");
1631 		return ERR_PTR(-EINVAL);
1632 	}
1633 
1634 	subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
1635 	if (IS_ERR(subsys))
1636 		return ERR_CAST(subsys);
1637 
1638 	config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1639 
1640 	config_group_init_type_name(&subsys->namespaces_group,
1641 			"namespaces", &nvmet_namespaces_type);
1642 	configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1643 
1644 	config_group_init_type_name(&subsys->allowed_hosts_group,
1645 			"allowed_hosts", &nvmet_allowed_hosts_type);
1646 	configfs_add_default_group(&subsys->allowed_hosts_group,
1647 			&subsys->group);
1648 
1649 	nvmet_add_passthru_group(subsys);
1650 
1651 	return &subsys->group;
1652 }
1653 
1654 static struct configfs_group_operations nvmet_subsystems_group_ops = {
1655 	.make_group		= nvmet_subsys_make,
1656 };
1657 
1658 static const struct config_item_type nvmet_subsystems_type = {
1659 	.ct_group_ops		= &nvmet_subsystems_group_ops,
1660 	.ct_owner		= THIS_MODULE,
1661 };
1662 
1663 static ssize_t nvmet_referral_enable_show(struct config_item *item,
1664 		char *page)
1665 {
1666 	return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1667 }
1668 
1669 static ssize_t nvmet_referral_enable_store(struct config_item *item,
1670 		const char *page, size_t count)
1671 {
1672 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1673 	struct nvmet_port *port = to_nvmet_port(item);
1674 	bool enable;
1675 
1676 	if (kstrtobool(page, &enable))
1677 		goto inval;
1678 
1679 	if (enable)
1680 		nvmet_referral_enable(parent, port);
1681 	else
1682 		nvmet_referral_disable(parent, port);
1683 
1684 	return count;
1685 inval:
1686 	pr_err("Invalid value '%s' for enable\n", page);
1687 	return -EINVAL;
1688 }
1689 
1690 CONFIGFS_ATTR(nvmet_referral_, enable);
1691 
1692 /*
1693  * Discovery Service subsystem definitions
1694  */
1695 static struct configfs_attribute *nvmet_referral_attrs[] = {
1696 	&nvmet_attr_addr_adrfam,
1697 	&nvmet_attr_addr_portid,
1698 	&nvmet_attr_addr_treq,
1699 	&nvmet_attr_addr_traddr,
1700 	&nvmet_attr_addr_trsvcid,
1701 	&nvmet_attr_addr_trtype,
1702 	&nvmet_referral_attr_enable,
1703 	NULL,
1704 };
1705 
1706 static void nvmet_referral_notify(struct config_group *group,
1707 		struct config_item *item)
1708 {
1709 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1710 	struct nvmet_port *port = to_nvmet_port(item);
1711 
1712 	nvmet_referral_disable(parent, port);
1713 }
1714 
1715 static void nvmet_referral_release(struct config_item *item)
1716 {
1717 	struct nvmet_port *port = to_nvmet_port(item);
1718 
1719 	kfree(port);
1720 }
1721 
1722 static struct configfs_item_operations nvmet_referral_item_ops = {
1723 	.release	= nvmet_referral_release,
1724 };
1725 
1726 static const struct config_item_type nvmet_referral_type = {
1727 	.ct_owner	= THIS_MODULE,
1728 	.ct_attrs	= nvmet_referral_attrs,
1729 	.ct_item_ops	= &nvmet_referral_item_ops,
1730 };
1731 
1732 static struct config_group *nvmet_referral_make(
1733 		struct config_group *group, const char *name)
1734 {
1735 	struct nvmet_port *port;
1736 
1737 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1738 	if (!port)
1739 		return ERR_PTR(-ENOMEM);
1740 
1741 	INIT_LIST_HEAD(&port->entry);
1742 	config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1743 
1744 	return &port->group;
1745 }
1746 
1747 static struct configfs_group_operations nvmet_referral_group_ops = {
1748 	.make_group		= nvmet_referral_make,
1749 	.disconnect_notify	= nvmet_referral_notify,
1750 };
1751 
1752 static const struct config_item_type nvmet_referrals_type = {
1753 	.ct_owner	= THIS_MODULE,
1754 	.ct_group_ops	= &nvmet_referral_group_ops,
1755 };
1756 
1757 static struct nvmet_type_name_map nvmet_ana_state[] = {
1758 	{ NVME_ANA_OPTIMIZED,		"optimized" },
1759 	{ NVME_ANA_NONOPTIMIZED,	"non-optimized" },
1760 	{ NVME_ANA_INACCESSIBLE,	"inaccessible" },
1761 	{ NVME_ANA_PERSISTENT_LOSS,	"persistent-loss" },
1762 	{ NVME_ANA_CHANGE,		"change" },
1763 };
1764 
1765 static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
1766 		char *page)
1767 {
1768 	struct nvmet_ana_group *grp = to_ana_group(item);
1769 	enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
1770 	int i;
1771 
1772 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1773 		if (state == nvmet_ana_state[i].type)
1774 			return sprintf(page, "%s\n", nvmet_ana_state[i].name);
1775 	}
1776 
1777 	return sprintf(page, "\n");
1778 }
1779 
1780 static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
1781 		const char *page, size_t count)
1782 {
1783 	struct nvmet_ana_group *grp = to_ana_group(item);
1784 	enum nvme_ana_state *ana_state = grp->port->ana_state;
1785 	int i;
1786 
1787 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1788 		if (sysfs_streq(page, nvmet_ana_state[i].name))
1789 			goto found;
1790 	}
1791 
1792 	pr_err("Invalid value '%s' for ana_state\n", page);
1793 	return -EINVAL;
1794 
1795 found:
1796 	down_write(&nvmet_ana_sem);
1797 	ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
1798 	nvmet_ana_chgcnt++;
1799 	up_write(&nvmet_ana_sem);
1800 	nvmet_port_send_ana_event(grp->port);
1801 	return count;
1802 }
1803 
1804 CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
1805 
1806 static struct configfs_attribute *nvmet_ana_group_attrs[] = {
1807 	&nvmet_ana_group_attr_ana_state,
1808 	NULL,
1809 };
1810 
1811 static void nvmet_ana_group_release(struct config_item *item)
1812 {
1813 	struct nvmet_ana_group *grp = to_ana_group(item);
1814 
1815 	if (grp == &grp->port->ana_default_group)
1816 		return;
1817 
1818 	down_write(&nvmet_ana_sem);
1819 	grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
1820 	nvmet_ana_group_enabled[grp->grpid]--;
1821 	up_write(&nvmet_ana_sem);
1822 
1823 	nvmet_port_send_ana_event(grp->port);
1824 	kfree(grp);
1825 }
1826 
1827 static struct configfs_item_operations nvmet_ana_group_item_ops = {
1828 	.release		= nvmet_ana_group_release,
1829 };
1830 
1831 static const struct config_item_type nvmet_ana_group_type = {
1832 	.ct_item_ops		= &nvmet_ana_group_item_ops,
1833 	.ct_attrs		= nvmet_ana_group_attrs,
1834 	.ct_owner		= THIS_MODULE,
1835 };
1836 
1837 static struct config_group *nvmet_ana_groups_make_group(
1838 		struct config_group *group, const char *name)
1839 {
1840 	struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
1841 	struct nvmet_ana_group *grp;
1842 	u32 grpid;
1843 	int ret;
1844 
1845 	ret = kstrtou32(name, 0, &grpid);
1846 	if (ret)
1847 		goto out;
1848 
1849 	ret = -EINVAL;
1850 	if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
1851 		goto out;
1852 
1853 	ret = -ENOMEM;
1854 	grp = kzalloc(sizeof(*grp), GFP_KERNEL);
1855 	if (!grp)
1856 		goto out;
1857 	grp->port = port;
1858 	grp->grpid = grpid;
1859 
1860 	down_write(&nvmet_ana_sem);
1861 	grpid = array_index_nospec(grpid, NVMET_MAX_ANAGRPS);
1862 	nvmet_ana_group_enabled[grpid]++;
1863 	up_write(&nvmet_ana_sem);
1864 
1865 	nvmet_port_send_ana_event(grp->port);
1866 
1867 	config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
1868 	return &grp->group;
1869 out:
1870 	return ERR_PTR(ret);
1871 }
1872 
1873 static struct configfs_group_operations nvmet_ana_groups_group_ops = {
1874 	.make_group		= nvmet_ana_groups_make_group,
1875 };
1876 
1877 static const struct config_item_type nvmet_ana_groups_type = {
1878 	.ct_group_ops		= &nvmet_ana_groups_group_ops,
1879 	.ct_owner		= THIS_MODULE,
1880 };
1881 
1882 /*
1883  * Ports definitions.
1884  */
1885 static void nvmet_port_release(struct config_item *item)
1886 {
1887 	struct nvmet_port *port = to_nvmet_port(item);
1888 
1889 	/* Let inflight controllers teardown complete */
1890 	flush_workqueue(nvmet_wq);
1891 	list_del(&port->global_entry);
1892 
1893 	key_put(port->keyring);
1894 	kfree(port->ana_state);
1895 	kfree(port);
1896 }
1897 
1898 static struct configfs_attribute *nvmet_port_attrs[] = {
1899 	&nvmet_attr_addr_adrfam,
1900 	&nvmet_attr_addr_treq,
1901 	&nvmet_attr_addr_traddr,
1902 	&nvmet_attr_addr_trsvcid,
1903 	&nvmet_attr_addr_trtype,
1904 	&nvmet_attr_addr_tsas,
1905 	&nvmet_attr_param_inline_data_size,
1906 	&nvmet_attr_param_max_queue_size,
1907 #ifdef CONFIG_BLK_DEV_INTEGRITY
1908 	&nvmet_attr_param_pi_enable,
1909 #endif
1910 	NULL,
1911 };
1912 
1913 static struct configfs_item_operations nvmet_port_item_ops = {
1914 	.release		= nvmet_port_release,
1915 };
1916 
1917 static const struct config_item_type nvmet_port_type = {
1918 	.ct_attrs		= nvmet_port_attrs,
1919 	.ct_item_ops		= &nvmet_port_item_ops,
1920 	.ct_owner		= THIS_MODULE,
1921 };
1922 
1923 static struct config_group *nvmet_ports_make(struct config_group *group,
1924 		const char *name)
1925 {
1926 	struct nvmet_port *port;
1927 	u16 portid;
1928 	u32 i;
1929 
1930 	if (kstrtou16(name, 0, &portid))
1931 		return ERR_PTR(-EINVAL);
1932 
1933 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1934 	if (!port)
1935 		return ERR_PTR(-ENOMEM);
1936 
1937 	port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
1938 			sizeof(*port->ana_state), GFP_KERNEL);
1939 	if (!port->ana_state) {
1940 		kfree(port);
1941 		return ERR_PTR(-ENOMEM);
1942 	}
1943 
1944 	if (IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS) && nvme_keyring_id()) {
1945 		port->keyring = key_lookup(nvme_keyring_id());
1946 		if (IS_ERR(port->keyring)) {
1947 			pr_warn("NVMe keyring not available, disabling TLS\n");
1948 			port->keyring = NULL;
1949 		}
1950 	}
1951 
1952 	for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
1953 		if (i == NVMET_DEFAULT_ANA_GRPID)
1954 			port->ana_state[1] = NVME_ANA_OPTIMIZED;
1955 		else
1956 			port->ana_state[i] = NVME_ANA_INACCESSIBLE;
1957 	}
1958 
1959 	list_add(&port->global_entry, &nvmet_ports_list);
1960 
1961 	INIT_LIST_HEAD(&port->entry);
1962 	INIT_LIST_HEAD(&port->subsystems);
1963 	INIT_LIST_HEAD(&port->referrals);
1964 	port->inline_data_size = -1;	/* < 0 == let the transport choose */
1965 	port->max_queue_size = -1;	/* < 0 == let the transport choose */
1966 
1967 	port->disc_addr.portid = cpu_to_le16(portid);
1968 	port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
1969 	port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1970 	config_group_init_type_name(&port->group, name, &nvmet_port_type);
1971 
1972 	config_group_init_type_name(&port->subsys_group,
1973 			"subsystems", &nvmet_port_subsys_type);
1974 	configfs_add_default_group(&port->subsys_group, &port->group);
1975 
1976 	config_group_init_type_name(&port->referrals_group,
1977 			"referrals", &nvmet_referrals_type);
1978 	configfs_add_default_group(&port->referrals_group, &port->group);
1979 
1980 	config_group_init_type_name(&port->ana_groups_group,
1981 			"ana_groups", &nvmet_ana_groups_type);
1982 	configfs_add_default_group(&port->ana_groups_group, &port->group);
1983 
1984 	port->ana_default_group.port = port;
1985 	port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
1986 	config_group_init_type_name(&port->ana_default_group.group,
1987 			__stringify(NVMET_DEFAULT_ANA_GRPID),
1988 			&nvmet_ana_group_type);
1989 	configfs_add_default_group(&port->ana_default_group.group,
1990 			&port->ana_groups_group);
1991 
1992 	return &port->group;
1993 }
1994 
1995 static struct configfs_group_operations nvmet_ports_group_ops = {
1996 	.make_group		= nvmet_ports_make,
1997 };
1998 
1999 static const struct config_item_type nvmet_ports_type = {
2000 	.ct_group_ops		= &nvmet_ports_group_ops,
2001 	.ct_owner		= THIS_MODULE,
2002 };
2003 
2004 static struct config_group nvmet_subsystems_group;
2005 static struct config_group nvmet_ports_group;
2006 
2007 #ifdef CONFIG_NVME_TARGET_AUTH
2008 static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
2009 		char *page)
2010 {
2011 	u8 *dhchap_secret = to_host(item)->dhchap_secret;
2012 
2013 	if (!dhchap_secret)
2014 		return sprintf(page, "\n");
2015 	return sprintf(page, "%s\n", dhchap_secret);
2016 }
2017 
2018 static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
2019 		const char *page, size_t count)
2020 {
2021 	struct nvmet_host *host = to_host(item);
2022 	int ret;
2023 
2024 	ret = nvmet_auth_set_key(host, page, false);
2025 	/*
2026 	 * Re-authentication is a soft state, so keep the
2027 	 * current authentication valid until the host
2028 	 * requests re-authentication.
2029 	 */
2030 	return ret < 0 ? ret : count;
2031 }
2032 
2033 CONFIGFS_ATTR(nvmet_host_, dhchap_key);
2034 
2035 static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
2036 		char *page)
2037 {
2038 	u8 *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
2039 
2040 	if (!dhchap_secret)
2041 		return sprintf(page, "\n");
2042 	return sprintf(page, "%s\n", dhchap_secret);
2043 }
2044 
2045 static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
2046 		const char *page, size_t count)
2047 {
2048 	struct nvmet_host *host = to_host(item);
2049 	int ret;
2050 
2051 	ret = nvmet_auth_set_key(host, page, true);
2052 	/*
2053 	 * Re-authentication is a soft state, so keep the
2054 	 * current authentication valid until the host
2055 	 * requests re-authentication.
2056 	 */
2057 	return ret < 0 ? ret : count;
2058 }
2059 
2060 CONFIGFS_ATTR(nvmet_host_, dhchap_ctrl_key);
2061 
2062 static ssize_t nvmet_host_dhchap_hash_show(struct config_item *item,
2063 		char *page)
2064 {
2065 	struct nvmet_host *host = to_host(item);
2066 	const char *hash_name = nvme_auth_hmac_name(host->dhchap_hash_id);
2067 
2068 	return sprintf(page, "%s\n", hash_name ? hash_name : "none");
2069 }
2070 
2071 static ssize_t nvmet_host_dhchap_hash_store(struct config_item *item,
2072 		const char *page, size_t count)
2073 {
2074 	struct nvmet_host *host = to_host(item);
2075 	u8 hmac_id;
2076 
2077 	hmac_id = nvme_auth_hmac_id(page);
2078 	if (hmac_id == NVME_AUTH_HASH_INVALID)
2079 		return -EINVAL;
2080 	if (!crypto_has_shash(nvme_auth_hmac_name(hmac_id), 0, 0))
2081 		return -ENOTSUPP;
2082 	host->dhchap_hash_id = hmac_id;
2083 	return count;
2084 }
2085 
2086 CONFIGFS_ATTR(nvmet_host_, dhchap_hash);
2087 
2088 static ssize_t nvmet_host_dhchap_dhgroup_show(struct config_item *item,
2089 		char *page)
2090 {
2091 	struct nvmet_host *host = to_host(item);
2092 	const char *dhgroup = nvme_auth_dhgroup_name(host->dhchap_dhgroup_id);
2093 
2094 	return sprintf(page, "%s\n", dhgroup ? dhgroup : "none");
2095 }
2096 
2097 static ssize_t nvmet_host_dhchap_dhgroup_store(struct config_item *item,
2098 		const char *page, size_t count)
2099 {
2100 	struct nvmet_host *host = to_host(item);
2101 	int dhgroup_id;
2102 
2103 	dhgroup_id = nvme_auth_dhgroup_id(page);
2104 	if (dhgroup_id == NVME_AUTH_DHGROUP_INVALID)
2105 		return -EINVAL;
2106 	if (dhgroup_id != NVME_AUTH_DHGROUP_NULL) {
2107 		const char *kpp = nvme_auth_dhgroup_kpp(dhgroup_id);
2108 
2109 		if (!crypto_has_kpp(kpp, 0, 0))
2110 			return -EINVAL;
2111 	}
2112 	host->dhchap_dhgroup_id = dhgroup_id;
2113 	return count;
2114 }
2115 
2116 CONFIGFS_ATTR(nvmet_host_, dhchap_dhgroup);
2117 
2118 static struct configfs_attribute *nvmet_host_attrs[] = {
2119 	&nvmet_host_attr_dhchap_key,
2120 	&nvmet_host_attr_dhchap_ctrl_key,
2121 	&nvmet_host_attr_dhchap_hash,
2122 	&nvmet_host_attr_dhchap_dhgroup,
2123 	NULL,
2124 };
2125 #endif /* CONFIG_NVME_TARGET_AUTH */
2126 
2127 static void nvmet_host_release(struct config_item *item)
2128 {
2129 	struct nvmet_host *host = to_host(item);
2130 
2131 #ifdef CONFIG_NVME_TARGET_AUTH
2132 	kfree(host->dhchap_secret);
2133 	kfree(host->dhchap_ctrl_secret);
2134 #endif
2135 	kfree(host);
2136 }
2137 
2138 static struct configfs_item_operations nvmet_host_item_ops = {
2139 	.release		= nvmet_host_release,
2140 };
2141 
2142 static const struct config_item_type nvmet_host_type = {
2143 	.ct_item_ops		= &nvmet_host_item_ops,
2144 #ifdef CONFIG_NVME_TARGET_AUTH
2145 	.ct_attrs		= nvmet_host_attrs,
2146 #endif
2147 	.ct_owner		= THIS_MODULE,
2148 };
2149 
2150 static struct config_group *nvmet_hosts_make_group(struct config_group *group,
2151 		const char *name)
2152 {
2153 	struct nvmet_host *host;
2154 
2155 	host = kzalloc(sizeof(*host), GFP_KERNEL);
2156 	if (!host)
2157 		return ERR_PTR(-ENOMEM);
2158 
2159 #ifdef CONFIG_NVME_TARGET_AUTH
2160 	/* Default to SHA256 */
2161 	host->dhchap_hash_id = NVME_AUTH_HASH_SHA256;
2162 #endif
2163 
2164 	config_group_init_type_name(&host->group, name, &nvmet_host_type);
2165 
2166 	return &host->group;
2167 }
2168 
2169 static struct configfs_group_operations nvmet_hosts_group_ops = {
2170 	.make_group		= nvmet_hosts_make_group,
2171 };
2172 
2173 static const struct config_item_type nvmet_hosts_type = {
2174 	.ct_group_ops		= &nvmet_hosts_group_ops,
2175 	.ct_owner		= THIS_MODULE,
2176 };
2177 
2178 static struct config_group nvmet_hosts_group;
2179 
2180 static ssize_t nvmet_root_discovery_nqn_show(struct config_item *item,
2181 					     char *page)
2182 {
2183 	return snprintf(page, PAGE_SIZE, "%s\n", nvmet_disc_subsys->subsysnqn);
2184 }
2185 
2186 static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item,
2187 		const char *page, size_t count)
2188 {
2189 	struct list_head *entry;
2190 	size_t len;
2191 
2192 	len = strcspn(page, "\n");
2193 	if (!len || len > NVMF_NQN_FIELD_LEN - 1)
2194 		return -EINVAL;
2195 
2196 	down_write(&nvmet_config_sem);
2197 	list_for_each(entry, &nvmet_subsystems_group.cg_children) {
2198 		struct config_item *item =
2199 			container_of(entry, struct config_item, ci_entry);
2200 
2201 		if (!strncmp(config_item_name(item), page, len)) {
2202 			pr_err("duplicate NQN %s\n", config_item_name(item));
2203 			up_write(&nvmet_config_sem);
2204 			return -EINVAL;
2205 		}
2206 	}
2207 	memset(nvmet_disc_subsys->subsysnqn, 0, NVMF_NQN_FIELD_LEN);
2208 	memcpy(nvmet_disc_subsys->subsysnqn, page, len);
2209 	up_write(&nvmet_config_sem);
2210 
2211 	return len;
2212 }
2213 
2214 CONFIGFS_ATTR(nvmet_root_, discovery_nqn);
2215 
2216 static struct configfs_attribute *nvmet_root_attrs[] = {
2217 	&nvmet_root_attr_discovery_nqn,
2218 	NULL,
2219 };
2220 
2221 static const struct config_item_type nvmet_root_type = {
2222 	.ct_attrs		= nvmet_root_attrs,
2223 	.ct_owner		= THIS_MODULE,
2224 };
2225 
2226 static struct configfs_subsystem nvmet_configfs_subsystem = {
2227 	.su_group = {
2228 		.cg_item = {
2229 			.ci_namebuf	= "nvmet",
2230 			.ci_type	= &nvmet_root_type,
2231 		},
2232 	},
2233 };
2234 
2235 int __init nvmet_init_configfs(void)
2236 {
2237 	int ret;
2238 
2239 	config_group_init(&nvmet_configfs_subsystem.su_group);
2240 	mutex_init(&nvmet_configfs_subsystem.su_mutex);
2241 
2242 	config_group_init_type_name(&nvmet_subsystems_group,
2243 			"subsystems", &nvmet_subsystems_type);
2244 	configfs_add_default_group(&nvmet_subsystems_group,
2245 			&nvmet_configfs_subsystem.su_group);
2246 
2247 	config_group_init_type_name(&nvmet_ports_group,
2248 			"ports", &nvmet_ports_type);
2249 	configfs_add_default_group(&nvmet_ports_group,
2250 			&nvmet_configfs_subsystem.su_group);
2251 
2252 	config_group_init_type_name(&nvmet_hosts_group,
2253 			"hosts", &nvmet_hosts_type);
2254 	configfs_add_default_group(&nvmet_hosts_group,
2255 			&nvmet_configfs_subsystem.su_group);
2256 
2257 	ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
2258 	if (ret) {
2259 		pr_err("configfs_register_subsystem: %d\n", ret);
2260 		return ret;
2261 	}
2262 
2263 	return 0;
2264 }
2265 
2266 void __exit nvmet_exit_configfs(void)
2267 {
2268 	configfs_unregister_subsystem(&nvmet_configfs_subsystem);
2269 }
2270