xref: /linux/drivers/nvme/target/configfs.c (revision eb01fe7abbe2d0b38824d2a93fdb4cc3eaf2ccc1)
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 static void nvmet_ns_release(struct config_item *item)
758 {
759 	struct nvmet_ns *ns = to_nvmet_ns(item);
760 
761 	nvmet_ns_free(ns);
762 }
763 
764 static struct configfs_item_operations nvmet_ns_item_ops = {
765 	.release		= nvmet_ns_release,
766 };
767 
768 static const struct config_item_type nvmet_ns_type = {
769 	.ct_item_ops		= &nvmet_ns_item_ops,
770 	.ct_attrs		= nvmet_ns_attrs,
771 	.ct_owner		= THIS_MODULE,
772 };
773 
774 static struct config_group *nvmet_ns_make(struct config_group *group,
775 		const char *name)
776 {
777 	struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
778 	struct nvmet_ns *ns;
779 	int ret;
780 	u32 nsid;
781 
782 	ret = kstrtou32(name, 0, &nsid);
783 	if (ret)
784 		goto out;
785 
786 	ret = -EINVAL;
787 	if (nsid == 0 || nsid == NVME_NSID_ALL) {
788 		pr_err("invalid nsid %#x", nsid);
789 		goto out;
790 	}
791 
792 	ret = -ENOMEM;
793 	ns = nvmet_ns_alloc(subsys, nsid);
794 	if (!ns)
795 		goto out;
796 	config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
797 
798 	pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
799 
800 	return &ns->group;
801 out:
802 	return ERR_PTR(ret);
803 }
804 
805 static struct configfs_group_operations nvmet_namespaces_group_ops = {
806 	.make_group		= nvmet_ns_make,
807 };
808 
809 static const struct config_item_type nvmet_namespaces_type = {
810 	.ct_group_ops		= &nvmet_namespaces_group_ops,
811 	.ct_owner		= THIS_MODULE,
812 };
813 
814 #ifdef CONFIG_NVME_TARGET_PASSTHRU
815 
816 static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
817 		char *page)
818 {
819 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
820 
821 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
822 }
823 
824 static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
825 		const char *page, size_t count)
826 {
827 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
828 	size_t len;
829 	int ret;
830 
831 	mutex_lock(&subsys->lock);
832 
833 	ret = -EBUSY;
834 	if (subsys->passthru_ctrl)
835 		goto out_unlock;
836 
837 	ret = -EINVAL;
838 	len = strcspn(page, "\n");
839 	if (!len)
840 		goto out_unlock;
841 
842 	kfree(subsys->passthru_ctrl_path);
843 	ret = -ENOMEM;
844 	subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
845 	if (!subsys->passthru_ctrl_path)
846 		goto out_unlock;
847 
848 	mutex_unlock(&subsys->lock);
849 
850 	return count;
851 out_unlock:
852 	mutex_unlock(&subsys->lock);
853 	return ret;
854 }
855 CONFIGFS_ATTR(nvmet_passthru_, device_path);
856 
857 static ssize_t nvmet_passthru_enable_show(struct config_item *item,
858 		char *page)
859 {
860 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
861 
862 	return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
863 }
864 
865 static ssize_t nvmet_passthru_enable_store(struct config_item *item,
866 		const char *page, size_t count)
867 {
868 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
869 	bool enable;
870 	int ret = 0;
871 
872 	if (kstrtobool(page, &enable))
873 		return -EINVAL;
874 
875 	if (enable)
876 		ret = nvmet_passthru_ctrl_enable(subsys);
877 	else
878 		nvmet_passthru_ctrl_disable(subsys);
879 
880 	return ret ? ret : count;
881 }
882 CONFIGFS_ATTR(nvmet_passthru_, enable);
883 
884 static ssize_t nvmet_passthru_admin_timeout_show(struct config_item *item,
885 		char *page)
886 {
887 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->admin_timeout);
888 }
889 
890 static ssize_t nvmet_passthru_admin_timeout_store(struct config_item *item,
891 		const char *page, size_t count)
892 {
893 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
894 	unsigned int timeout;
895 
896 	if (kstrtouint(page, 0, &timeout))
897 		return -EINVAL;
898 	subsys->admin_timeout = timeout;
899 	return count;
900 }
901 CONFIGFS_ATTR(nvmet_passthru_, admin_timeout);
902 
903 static ssize_t nvmet_passthru_io_timeout_show(struct config_item *item,
904 		char *page)
905 {
906 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->io_timeout);
907 }
908 
909 static ssize_t nvmet_passthru_io_timeout_store(struct config_item *item,
910 		const char *page, size_t count)
911 {
912 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
913 	unsigned int timeout;
914 
915 	if (kstrtouint(page, 0, &timeout))
916 		return -EINVAL;
917 	subsys->io_timeout = timeout;
918 	return count;
919 }
920 CONFIGFS_ATTR(nvmet_passthru_, io_timeout);
921 
922 static ssize_t nvmet_passthru_clear_ids_show(struct config_item *item,
923 		char *page)
924 {
925 	return sprintf(page, "%u\n", to_subsys(item->ci_parent)->clear_ids);
926 }
927 
928 static ssize_t nvmet_passthru_clear_ids_store(struct config_item *item,
929 		const char *page, size_t count)
930 {
931 	struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
932 	unsigned int clear_ids;
933 
934 	if (kstrtouint(page, 0, &clear_ids))
935 		return -EINVAL;
936 	subsys->clear_ids = clear_ids;
937 	return count;
938 }
939 CONFIGFS_ATTR(nvmet_passthru_, clear_ids);
940 
941 static struct configfs_attribute *nvmet_passthru_attrs[] = {
942 	&nvmet_passthru_attr_device_path,
943 	&nvmet_passthru_attr_enable,
944 	&nvmet_passthru_attr_admin_timeout,
945 	&nvmet_passthru_attr_io_timeout,
946 	&nvmet_passthru_attr_clear_ids,
947 	NULL,
948 };
949 
950 static const struct config_item_type nvmet_passthru_type = {
951 	.ct_attrs		= nvmet_passthru_attrs,
952 	.ct_owner		= THIS_MODULE,
953 };
954 
955 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
956 {
957 	config_group_init_type_name(&subsys->passthru_group,
958 				    "passthru", &nvmet_passthru_type);
959 	configfs_add_default_group(&subsys->passthru_group,
960 				   &subsys->group);
961 }
962 
963 #else /* CONFIG_NVME_TARGET_PASSTHRU */
964 
965 static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
966 {
967 }
968 
969 #endif /* CONFIG_NVME_TARGET_PASSTHRU */
970 
971 static int nvmet_port_subsys_allow_link(struct config_item *parent,
972 		struct config_item *target)
973 {
974 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
975 	struct nvmet_subsys *subsys;
976 	struct nvmet_subsys_link *link, *p;
977 	int ret;
978 
979 	if (target->ci_type != &nvmet_subsys_type) {
980 		pr_err("can only link subsystems into the subsystems dir.!\n");
981 		return -EINVAL;
982 	}
983 	subsys = to_subsys(target);
984 	link = kmalloc(sizeof(*link), GFP_KERNEL);
985 	if (!link)
986 		return -ENOMEM;
987 	link->subsys = subsys;
988 
989 	down_write(&nvmet_config_sem);
990 	ret = -EEXIST;
991 	list_for_each_entry(p, &port->subsystems, entry) {
992 		if (p->subsys == subsys)
993 			goto out_free_link;
994 	}
995 
996 	if (list_empty(&port->subsystems)) {
997 		ret = nvmet_enable_port(port);
998 		if (ret)
999 			goto out_free_link;
1000 	}
1001 
1002 	list_add_tail(&link->entry, &port->subsystems);
1003 	nvmet_port_disc_changed(port, subsys);
1004 
1005 	up_write(&nvmet_config_sem);
1006 	return 0;
1007 
1008 out_free_link:
1009 	up_write(&nvmet_config_sem);
1010 	kfree(link);
1011 	return ret;
1012 }
1013 
1014 static void nvmet_port_subsys_drop_link(struct config_item *parent,
1015 		struct config_item *target)
1016 {
1017 	struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
1018 	struct nvmet_subsys *subsys = to_subsys(target);
1019 	struct nvmet_subsys_link *p;
1020 
1021 	down_write(&nvmet_config_sem);
1022 	list_for_each_entry(p, &port->subsystems, entry) {
1023 		if (p->subsys == subsys)
1024 			goto found;
1025 	}
1026 	up_write(&nvmet_config_sem);
1027 	return;
1028 
1029 found:
1030 	list_del(&p->entry);
1031 	nvmet_port_del_ctrls(port, subsys);
1032 	nvmet_port_disc_changed(port, subsys);
1033 
1034 	if (list_empty(&port->subsystems))
1035 		nvmet_disable_port(port);
1036 	up_write(&nvmet_config_sem);
1037 	kfree(p);
1038 }
1039 
1040 static struct configfs_item_operations nvmet_port_subsys_item_ops = {
1041 	.allow_link		= nvmet_port_subsys_allow_link,
1042 	.drop_link		= nvmet_port_subsys_drop_link,
1043 };
1044 
1045 static const struct config_item_type nvmet_port_subsys_type = {
1046 	.ct_item_ops		= &nvmet_port_subsys_item_ops,
1047 	.ct_owner		= THIS_MODULE,
1048 };
1049 
1050 static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
1051 		struct config_item *target)
1052 {
1053 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
1054 	struct nvmet_host *host;
1055 	struct nvmet_host_link *link, *p;
1056 	int ret;
1057 
1058 	if (target->ci_type != &nvmet_host_type) {
1059 		pr_err("can only link hosts into the allowed_hosts directory!\n");
1060 		return -EINVAL;
1061 	}
1062 
1063 	host = to_host(target);
1064 	link = kmalloc(sizeof(*link), GFP_KERNEL);
1065 	if (!link)
1066 		return -ENOMEM;
1067 	link->host = host;
1068 
1069 	down_write(&nvmet_config_sem);
1070 	ret = -EINVAL;
1071 	if (subsys->allow_any_host) {
1072 		pr_err("can't add hosts when allow_any_host is set!\n");
1073 		goto out_free_link;
1074 	}
1075 
1076 	ret = -EEXIST;
1077 	list_for_each_entry(p, &subsys->hosts, entry) {
1078 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
1079 			goto out_free_link;
1080 	}
1081 	list_add_tail(&link->entry, &subsys->hosts);
1082 	nvmet_subsys_disc_changed(subsys, host);
1083 
1084 	up_write(&nvmet_config_sem);
1085 	return 0;
1086 out_free_link:
1087 	up_write(&nvmet_config_sem);
1088 	kfree(link);
1089 	return ret;
1090 }
1091 
1092 static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
1093 		struct config_item *target)
1094 {
1095 	struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
1096 	struct nvmet_host *host = to_host(target);
1097 	struct nvmet_host_link *p;
1098 
1099 	down_write(&nvmet_config_sem);
1100 	list_for_each_entry(p, &subsys->hosts, entry) {
1101 		if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
1102 			goto found;
1103 	}
1104 	up_write(&nvmet_config_sem);
1105 	return;
1106 
1107 found:
1108 	list_del(&p->entry);
1109 	nvmet_subsys_disc_changed(subsys, host);
1110 
1111 	up_write(&nvmet_config_sem);
1112 	kfree(p);
1113 }
1114 
1115 static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
1116 	.allow_link		= nvmet_allowed_hosts_allow_link,
1117 	.drop_link		= nvmet_allowed_hosts_drop_link,
1118 };
1119 
1120 static const struct config_item_type nvmet_allowed_hosts_type = {
1121 	.ct_item_ops		= &nvmet_allowed_hosts_item_ops,
1122 	.ct_owner		= THIS_MODULE,
1123 };
1124 
1125 static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
1126 		char *page)
1127 {
1128 	return snprintf(page, PAGE_SIZE, "%d\n",
1129 		to_subsys(item)->allow_any_host);
1130 }
1131 
1132 static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
1133 		const char *page, size_t count)
1134 {
1135 	struct nvmet_subsys *subsys = to_subsys(item);
1136 	bool allow_any_host;
1137 	int ret = 0;
1138 
1139 	if (kstrtobool(page, &allow_any_host))
1140 		return -EINVAL;
1141 
1142 	down_write(&nvmet_config_sem);
1143 	if (allow_any_host && !list_empty(&subsys->hosts)) {
1144 		pr_err("Can't set allow_any_host when explicit hosts are set!\n");
1145 		ret = -EINVAL;
1146 		goto out_unlock;
1147 	}
1148 
1149 	if (subsys->allow_any_host != allow_any_host) {
1150 		subsys->allow_any_host = allow_any_host;
1151 		nvmet_subsys_disc_changed(subsys, NULL);
1152 	}
1153 
1154 out_unlock:
1155 	up_write(&nvmet_config_sem);
1156 	return ret ? ret : count;
1157 }
1158 
1159 CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
1160 
1161 static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
1162 					      char *page)
1163 {
1164 	struct nvmet_subsys *subsys = to_subsys(item);
1165 
1166 	if (NVME_TERTIARY(subsys->ver))
1167 		return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
1168 				NVME_MAJOR(subsys->ver),
1169 				NVME_MINOR(subsys->ver),
1170 				NVME_TERTIARY(subsys->ver));
1171 
1172 	return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
1173 			NVME_MAJOR(subsys->ver),
1174 			NVME_MINOR(subsys->ver));
1175 }
1176 
1177 static ssize_t
1178 nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,
1179 		const char *page, size_t count)
1180 {
1181 	int major, minor, tertiary = 0;
1182 	int ret;
1183 
1184 	if (subsys->subsys_discovered) {
1185 		if (NVME_TERTIARY(subsys->ver))
1186 			pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
1187 			       NVME_MAJOR(subsys->ver),
1188 			       NVME_MINOR(subsys->ver),
1189 			       NVME_TERTIARY(subsys->ver));
1190 		else
1191 			pr_err("Can't set version number. %llu.%llu is already assigned\n",
1192 			       NVME_MAJOR(subsys->ver),
1193 			       NVME_MINOR(subsys->ver));
1194 		return -EINVAL;
1195 	}
1196 
1197 	/* passthru subsystems use the underlying controller's version */
1198 	if (nvmet_is_passthru_subsys(subsys))
1199 		return -EINVAL;
1200 
1201 	ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
1202 	if (ret != 2 && ret != 3)
1203 		return -EINVAL;
1204 
1205 	subsys->ver = NVME_VS(major, minor, tertiary);
1206 
1207 	return count;
1208 }
1209 
1210 static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
1211 					       const char *page, size_t count)
1212 {
1213 	struct nvmet_subsys *subsys = to_subsys(item);
1214 	ssize_t ret;
1215 
1216 	down_write(&nvmet_config_sem);
1217 	mutex_lock(&subsys->lock);
1218 	ret = nvmet_subsys_attr_version_store_locked(subsys, page, count);
1219 	mutex_unlock(&subsys->lock);
1220 	up_write(&nvmet_config_sem);
1221 
1222 	return ret;
1223 }
1224 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
1225 
1226 /* See Section 1.5 of NVMe 1.4 */
1227 static bool nvmet_is_ascii(const char c)
1228 {
1229 	return c >= 0x20 && c <= 0x7e;
1230 }
1231 
1232 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
1233 					     char *page)
1234 {
1235 	struct nvmet_subsys *subsys = to_subsys(item);
1236 
1237 	return snprintf(page, PAGE_SIZE, "%.*s\n",
1238 			NVMET_SN_MAX_SIZE, subsys->serial);
1239 }
1240 
1241 static ssize_t
1242 nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
1243 		const char *page, size_t count)
1244 {
1245 	int pos, len = strcspn(page, "\n");
1246 
1247 	if (subsys->subsys_discovered) {
1248 		pr_err("Can't set serial number. %s is already assigned\n",
1249 		       subsys->serial);
1250 		return -EINVAL;
1251 	}
1252 
1253 	if (!len || len > NVMET_SN_MAX_SIZE) {
1254 		pr_err("Serial Number can not be empty or exceed %d Bytes\n",
1255 		       NVMET_SN_MAX_SIZE);
1256 		return -EINVAL;
1257 	}
1258 
1259 	for (pos = 0; pos < len; pos++) {
1260 		if (!nvmet_is_ascii(page[pos])) {
1261 			pr_err("Serial Number must contain only ASCII strings\n");
1262 			return -EINVAL;
1263 		}
1264 	}
1265 
1266 	memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
1267 
1268 	return count;
1269 }
1270 
1271 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
1272 					      const char *page, size_t count)
1273 {
1274 	struct nvmet_subsys *subsys = to_subsys(item);
1275 	ssize_t ret;
1276 
1277 	down_write(&nvmet_config_sem);
1278 	mutex_lock(&subsys->lock);
1279 	ret = nvmet_subsys_attr_serial_store_locked(subsys, page, count);
1280 	mutex_unlock(&subsys->lock);
1281 	up_write(&nvmet_config_sem);
1282 
1283 	return ret;
1284 }
1285 CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
1286 
1287 static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
1288 						 char *page)
1289 {
1290 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
1291 }
1292 
1293 static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
1294 						  const char *page, size_t cnt)
1295 {
1296 	u16 cntlid_min;
1297 
1298 	if (sscanf(page, "%hu\n", &cntlid_min) != 1)
1299 		return -EINVAL;
1300 
1301 	if (cntlid_min == 0)
1302 		return -EINVAL;
1303 
1304 	down_write(&nvmet_config_sem);
1305 	if (cntlid_min > to_subsys(item)->cntlid_max)
1306 		goto out_unlock;
1307 	to_subsys(item)->cntlid_min = cntlid_min;
1308 	up_write(&nvmet_config_sem);
1309 	return cnt;
1310 
1311 out_unlock:
1312 	up_write(&nvmet_config_sem);
1313 	return -EINVAL;
1314 }
1315 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
1316 
1317 static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
1318 						 char *page)
1319 {
1320 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
1321 }
1322 
1323 static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
1324 						  const char *page, size_t cnt)
1325 {
1326 	u16 cntlid_max;
1327 
1328 	if (sscanf(page, "%hu\n", &cntlid_max) != 1)
1329 		return -EINVAL;
1330 
1331 	if (cntlid_max == 0)
1332 		return -EINVAL;
1333 
1334 	down_write(&nvmet_config_sem);
1335 	if (cntlid_max < to_subsys(item)->cntlid_min)
1336 		goto out_unlock;
1337 	to_subsys(item)->cntlid_max = cntlid_max;
1338 	up_write(&nvmet_config_sem);
1339 	return cnt;
1340 
1341 out_unlock:
1342 	up_write(&nvmet_config_sem);
1343 	return -EINVAL;
1344 }
1345 CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
1346 
1347 static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
1348 					    char *page)
1349 {
1350 	struct nvmet_subsys *subsys = to_subsys(item);
1351 
1352 	return snprintf(page, PAGE_SIZE, "%s\n", subsys->model_number);
1353 }
1354 
1355 static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
1356 		const char *page, size_t count)
1357 {
1358 	int pos = 0, len;
1359 	char *val;
1360 
1361 	if (subsys->subsys_discovered) {
1362 		pr_err("Can't set model number. %s is already assigned\n",
1363 		       subsys->model_number);
1364 		return -EINVAL;
1365 	}
1366 
1367 	len = strcspn(page, "\n");
1368 	if (!len)
1369 		return -EINVAL;
1370 
1371 	if (len > NVMET_MN_MAX_SIZE) {
1372 		pr_err("Model number size can not exceed %d Bytes\n",
1373 		       NVMET_MN_MAX_SIZE);
1374 		return -EINVAL;
1375 	}
1376 
1377 	for (pos = 0; pos < len; pos++) {
1378 		if (!nvmet_is_ascii(page[pos]))
1379 			return -EINVAL;
1380 	}
1381 
1382 	val = kmemdup_nul(page, len, GFP_KERNEL);
1383 	if (!val)
1384 		return -ENOMEM;
1385 	kfree(subsys->model_number);
1386 	subsys->model_number = val;
1387 	return count;
1388 }
1389 
1390 static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
1391 					     const char *page, size_t count)
1392 {
1393 	struct nvmet_subsys *subsys = to_subsys(item);
1394 	ssize_t ret;
1395 
1396 	down_write(&nvmet_config_sem);
1397 	mutex_lock(&subsys->lock);
1398 	ret = nvmet_subsys_attr_model_store_locked(subsys, page, count);
1399 	mutex_unlock(&subsys->lock);
1400 	up_write(&nvmet_config_sem);
1401 
1402 	return ret;
1403 }
1404 CONFIGFS_ATTR(nvmet_subsys_, attr_model);
1405 
1406 static ssize_t nvmet_subsys_attr_ieee_oui_show(struct config_item *item,
1407 					    char *page)
1408 {
1409 	struct nvmet_subsys *subsys = to_subsys(item);
1410 
1411 	return sysfs_emit(page, "0x%06x\n", subsys->ieee_oui);
1412 }
1413 
1414 static ssize_t nvmet_subsys_attr_ieee_oui_store_locked(struct nvmet_subsys *subsys,
1415 		const char *page, size_t count)
1416 {
1417 	uint32_t val = 0;
1418 	int ret;
1419 
1420 	if (subsys->subsys_discovered) {
1421 		pr_err("Can't set IEEE OUI. 0x%06x is already assigned\n",
1422 		      subsys->ieee_oui);
1423 		return -EINVAL;
1424 	}
1425 
1426 	ret = kstrtou32(page, 0, &val);
1427 	if (ret < 0)
1428 		return ret;
1429 
1430 	if (val >= 0x1000000)
1431 		return -EINVAL;
1432 
1433 	subsys->ieee_oui = val;
1434 
1435 	return count;
1436 }
1437 
1438 static ssize_t nvmet_subsys_attr_ieee_oui_store(struct config_item *item,
1439 					     const char *page, size_t count)
1440 {
1441 	struct nvmet_subsys *subsys = to_subsys(item);
1442 	ssize_t ret;
1443 
1444 	down_write(&nvmet_config_sem);
1445 	mutex_lock(&subsys->lock);
1446 	ret = nvmet_subsys_attr_ieee_oui_store_locked(subsys, page, count);
1447 	mutex_unlock(&subsys->lock);
1448 	up_write(&nvmet_config_sem);
1449 
1450 	return ret;
1451 }
1452 CONFIGFS_ATTR(nvmet_subsys_, attr_ieee_oui);
1453 
1454 static ssize_t nvmet_subsys_attr_firmware_show(struct config_item *item,
1455 					    char *page)
1456 {
1457 	struct nvmet_subsys *subsys = to_subsys(item);
1458 
1459 	return sysfs_emit(page, "%s\n", subsys->firmware_rev);
1460 }
1461 
1462 static ssize_t nvmet_subsys_attr_firmware_store_locked(struct nvmet_subsys *subsys,
1463 		const char *page, size_t count)
1464 {
1465 	int pos = 0, len;
1466 	char *val;
1467 
1468 	if (subsys->subsys_discovered) {
1469 		pr_err("Can't set firmware revision. %s is already assigned\n",
1470 		       subsys->firmware_rev);
1471 		return -EINVAL;
1472 	}
1473 
1474 	len = strcspn(page, "\n");
1475 	if (!len)
1476 		return -EINVAL;
1477 
1478 	if (len > NVMET_FR_MAX_SIZE) {
1479 		pr_err("Firmware revision size can not exceed %d Bytes\n",
1480 		       NVMET_FR_MAX_SIZE);
1481 		return -EINVAL;
1482 	}
1483 
1484 	for (pos = 0; pos < len; pos++) {
1485 		if (!nvmet_is_ascii(page[pos]))
1486 			return -EINVAL;
1487 	}
1488 
1489 	val = kmemdup_nul(page, len, GFP_KERNEL);
1490 	if (!val)
1491 		return -ENOMEM;
1492 
1493 	kfree(subsys->firmware_rev);
1494 
1495 	subsys->firmware_rev = val;
1496 
1497 	return count;
1498 }
1499 
1500 static ssize_t nvmet_subsys_attr_firmware_store(struct config_item *item,
1501 					     const char *page, size_t count)
1502 {
1503 	struct nvmet_subsys *subsys = to_subsys(item);
1504 	ssize_t ret;
1505 
1506 	down_write(&nvmet_config_sem);
1507 	mutex_lock(&subsys->lock);
1508 	ret = nvmet_subsys_attr_firmware_store_locked(subsys, page, count);
1509 	mutex_unlock(&subsys->lock);
1510 	up_write(&nvmet_config_sem);
1511 
1512 	return ret;
1513 }
1514 CONFIGFS_ATTR(nvmet_subsys_, attr_firmware);
1515 
1516 #ifdef CONFIG_BLK_DEV_INTEGRITY
1517 static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
1518 						char *page)
1519 {
1520 	return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
1521 }
1522 
1523 static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
1524 						 const char *page, size_t count)
1525 {
1526 	struct nvmet_subsys *subsys = to_subsys(item);
1527 	bool pi_enable;
1528 
1529 	if (kstrtobool(page, &pi_enable))
1530 		return -EINVAL;
1531 
1532 	subsys->pi_support = pi_enable;
1533 	return count;
1534 }
1535 CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
1536 #endif
1537 
1538 static ssize_t nvmet_subsys_attr_qid_max_show(struct config_item *item,
1539 					      char *page)
1540 {
1541 	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->max_qid);
1542 }
1543 
1544 static ssize_t nvmet_subsys_attr_qid_max_store(struct config_item *item,
1545 					       const char *page, size_t cnt)
1546 {
1547 	struct nvmet_subsys *subsys = to_subsys(item);
1548 	struct nvmet_ctrl *ctrl;
1549 	u16 qid_max;
1550 
1551 	if (sscanf(page, "%hu\n", &qid_max) != 1)
1552 		return -EINVAL;
1553 
1554 	if (qid_max < 1 || qid_max > NVMET_NR_QUEUES)
1555 		return -EINVAL;
1556 
1557 	down_write(&nvmet_config_sem);
1558 	subsys->max_qid = qid_max;
1559 
1560 	/* Force reconnect */
1561 	list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
1562 		ctrl->ops->delete_ctrl(ctrl);
1563 	up_write(&nvmet_config_sem);
1564 
1565 	return cnt;
1566 }
1567 CONFIGFS_ATTR(nvmet_subsys_, attr_qid_max);
1568 
1569 static struct configfs_attribute *nvmet_subsys_attrs[] = {
1570 	&nvmet_subsys_attr_attr_allow_any_host,
1571 	&nvmet_subsys_attr_attr_version,
1572 	&nvmet_subsys_attr_attr_serial,
1573 	&nvmet_subsys_attr_attr_cntlid_min,
1574 	&nvmet_subsys_attr_attr_cntlid_max,
1575 	&nvmet_subsys_attr_attr_model,
1576 	&nvmet_subsys_attr_attr_qid_max,
1577 	&nvmet_subsys_attr_attr_ieee_oui,
1578 	&nvmet_subsys_attr_attr_firmware,
1579 #ifdef CONFIG_BLK_DEV_INTEGRITY
1580 	&nvmet_subsys_attr_attr_pi_enable,
1581 #endif
1582 	NULL,
1583 };
1584 
1585 /*
1586  * Subsystem structures & folder operation functions below
1587  */
1588 static void nvmet_subsys_release(struct config_item *item)
1589 {
1590 	struct nvmet_subsys *subsys = to_subsys(item);
1591 
1592 	nvmet_subsys_del_ctrls(subsys);
1593 	nvmet_subsys_put(subsys);
1594 }
1595 
1596 static struct configfs_item_operations nvmet_subsys_item_ops = {
1597 	.release		= nvmet_subsys_release,
1598 };
1599 
1600 static const struct config_item_type nvmet_subsys_type = {
1601 	.ct_item_ops		= &nvmet_subsys_item_ops,
1602 	.ct_attrs		= nvmet_subsys_attrs,
1603 	.ct_owner		= THIS_MODULE,
1604 };
1605 
1606 static struct config_group *nvmet_subsys_make(struct config_group *group,
1607 		const char *name)
1608 {
1609 	struct nvmet_subsys *subsys;
1610 
1611 	if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
1612 		pr_err("can't create discovery subsystem through configfs\n");
1613 		return ERR_PTR(-EINVAL);
1614 	}
1615 
1616 	subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
1617 	if (IS_ERR(subsys))
1618 		return ERR_CAST(subsys);
1619 
1620 	config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
1621 
1622 	config_group_init_type_name(&subsys->namespaces_group,
1623 			"namespaces", &nvmet_namespaces_type);
1624 	configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
1625 
1626 	config_group_init_type_name(&subsys->allowed_hosts_group,
1627 			"allowed_hosts", &nvmet_allowed_hosts_type);
1628 	configfs_add_default_group(&subsys->allowed_hosts_group,
1629 			&subsys->group);
1630 
1631 	nvmet_add_passthru_group(subsys);
1632 
1633 	return &subsys->group;
1634 }
1635 
1636 static struct configfs_group_operations nvmet_subsystems_group_ops = {
1637 	.make_group		= nvmet_subsys_make,
1638 };
1639 
1640 static const struct config_item_type nvmet_subsystems_type = {
1641 	.ct_group_ops		= &nvmet_subsystems_group_ops,
1642 	.ct_owner		= THIS_MODULE,
1643 };
1644 
1645 static ssize_t nvmet_referral_enable_show(struct config_item *item,
1646 		char *page)
1647 {
1648 	return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
1649 }
1650 
1651 static ssize_t nvmet_referral_enable_store(struct config_item *item,
1652 		const char *page, size_t count)
1653 {
1654 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1655 	struct nvmet_port *port = to_nvmet_port(item);
1656 	bool enable;
1657 
1658 	if (kstrtobool(page, &enable))
1659 		goto inval;
1660 
1661 	if (enable)
1662 		nvmet_referral_enable(parent, port);
1663 	else
1664 		nvmet_referral_disable(parent, port);
1665 
1666 	return count;
1667 inval:
1668 	pr_err("Invalid value '%s' for enable\n", page);
1669 	return -EINVAL;
1670 }
1671 
1672 CONFIGFS_ATTR(nvmet_referral_, enable);
1673 
1674 /*
1675  * Discovery Service subsystem definitions
1676  */
1677 static struct configfs_attribute *nvmet_referral_attrs[] = {
1678 	&nvmet_attr_addr_adrfam,
1679 	&nvmet_attr_addr_portid,
1680 	&nvmet_attr_addr_treq,
1681 	&nvmet_attr_addr_traddr,
1682 	&nvmet_attr_addr_trsvcid,
1683 	&nvmet_attr_addr_trtype,
1684 	&nvmet_referral_attr_enable,
1685 	NULL,
1686 };
1687 
1688 static void nvmet_referral_notify(struct config_group *group,
1689 		struct config_item *item)
1690 {
1691 	struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
1692 	struct nvmet_port *port = to_nvmet_port(item);
1693 
1694 	nvmet_referral_disable(parent, port);
1695 }
1696 
1697 static void nvmet_referral_release(struct config_item *item)
1698 {
1699 	struct nvmet_port *port = to_nvmet_port(item);
1700 
1701 	kfree(port);
1702 }
1703 
1704 static struct configfs_item_operations nvmet_referral_item_ops = {
1705 	.release	= nvmet_referral_release,
1706 };
1707 
1708 static const struct config_item_type nvmet_referral_type = {
1709 	.ct_owner	= THIS_MODULE,
1710 	.ct_attrs	= nvmet_referral_attrs,
1711 	.ct_item_ops	= &nvmet_referral_item_ops,
1712 };
1713 
1714 static struct config_group *nvmet_referral_make(
1715 		struct config_group *group, const char *name)
1716 {
1717 	struct nvmet_port *port;
1718 
1719 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1720 	if (!port)
1721 		return ERR_PTR(-ENOMEM);
1722 
1723 	INIT_LIST_HEAD(&port->entry);
1724 	config_group_init_type_name(&port->group, name, &nvmet_referral_type);
1725 
1726 	return &port->group;
1727 }
1728 
1729 static struct configfs_group_operations nvmet_referral_group_ops = {
1730 	.make_group		= nvmet_referral_make,
1731 	.disconnect_notify	= nvmet_referral_notify,
1732 };
1733 
1734 static const struct config_item_type nvmet_referrals_type = {
1735 	.ct_owner	= THIS_MODULE,
1736 	.ct_group_ops	= &nvmet_referral_group_ops,
1737 };
1738 
1739 static struct nvmet_type_name_map nvmet_ana_state[] = {
1740 	{ NVME_ANA_OPTIMIZED,		"optimized" },
1741 	{ NVME_ANA_NONOPTIMIZED,	"non-optimized" },
1742 	{ NVME_ANA_INACCESSIBLE,	"inaccessible" },
1743 	{ NVME_ANA_PERSISTENT_LOSS,	"persistent-loss" },
1744 	{ NVME_ANA_CHANGE,		"change" },
1745 };
1746 
1747 static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
1748 		char *page)
1749 {
1750 	struct nvmet_ana_group *grp = to_ana_group(item);
1751 	enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
1752 	int i;
1753 
1754 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1755 		if (state == nvmet_ana_state[i].type)
1756 			return sprintf(page, "%s\n", nvmet_ana_state[i].name);
1757 	}
1758 
1759 	return sprintf(page, "\n");
1760 }
1761 
1762 static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
1763 		const char *page, size_t count)
1764 {
1765 	struct nvmet_ana_group *grp = to_ana_group(item);
1766 	enum nvme_ana_state *ana_state = grp->port->ana_state;
1767 	int i;
1768 
1769 	for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
1770 		if (sysfs_streq(page, nvmet_ana_state[i].name))
1771 			goto found;
1772 	}
1773 
1774 	pr_err("Invalid value '%s' for ana_state\n", page);
1775 	return -EINVAL;
1776 
1777 found:
1778 	down_write(&nvmet_ana_sem);
1779 	ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
1780 	nvmet_ana_chgcnt++;
1781 	up_write(&nvmet_ana_sem);
1782 	nvmet_port_send_ana_event(grp->port);
1783 	return count;
1784 }
1785 
1786 CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
1787 
1788 static struct configfs_attribute *nvmet_ana_group_attrs[] = {
1789 	&nvmet_ana_group_attr_ana_state,
1790 	NULL,
1791 };
1792 
1793 static void nvmet_ana_group_release(struct config_item *item)
1794 {
1795 	struct nvmet_ana_group *grp = to_ana_group(item);
1796 
1797 	if (grp == &grp->port->ana_default_group)
1798 		return;
1799 
1800 	down_write(&nvmet_ana_sem);
1801 	grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
1802 	nvmet_ana_group_enabled[grp->grpid]--;
1803 	up_write(&nvmet_ana_sem);
1804 
1805 	nvmet_port_send_ana_event(grp->port);
1806 	kfree(grp);
1807 }
1808 
1809 static struct configfs_item_operations nvmet_ana_group_item_ops = {
1810 	.release		= nvmet_ana_group_release,
1811 };
1812 
1813 static const struct config_item_type nvmet_ana_group_type = {
1814 	.ct_item_ops		= &nvmet_ana_group_item_ops,
1815 	.ct_attrs		= nvmet_ana_group_attrs,
1816 	.ct_owner		= THIS_MODULE,
1817 };
1818 
1819 static struct config_group *nvmet_ana_groups_make_group(
1820 		struct config_group *group, const char *name)
1821 {
1822 	struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
1823 	struct nvmet_ana_group *grp;
1824 	u32 grpid;
1825 	int ret;
1826 
1827 	ret = kstrtou32(name, 0, &grpid);
1828 	if (ret)
1829 		goto out;
1830 
1831 	ret = -EINVAL;
1832 	if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
1833 		goto out;
1834 
1835 	ret = -ENOMEM;
1836 	grp = kzalloc(sizeof(*grp), GFP_KERNEL);
1837 	if (!grp)
1838 		goto out;
1839 	grp->port = port;
1840 	grp->grpid = grpid;
1841 
1842 	down_write(&nvmet_ana_sem);
1843 	grpid = array_index_nospec(grpid, NVMET_MAX_ANAGRPS);
1844 	nvmet_ana_group_enabled[grpid]++;
1845 	up_write(&nvmet_ana_sem);
1846 
1847 	nvmet_port_send_ana_event(grp->port);
1848 
1849 	config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
1850 	return &grp->group;
1851 out:
1852 	return ERR_PTR(ret);
1853 }
1854 
1855 static struct configfs_group_operations nvmet_ana_groups_group_ops = {
1856 	.make_group		= nvmet_ana_groups_make_group,
1857 };
1858 
1859 static const struct config_item_type nvmet_ana_groups_type = {
1860 	.ct_group_ops		= &nvmet_ana_groups_group_ops,
1861 	.ct_owner		= THIS_MODULE,
1862 };
1863 
1864 /*
1865  * Ports definitions.
1866  */
1867 static void nvmet_port_release(struct config_item *item)
1868 {
1869 	struct nvmet_port *port = to_nvmet_port(item);
1870 
1871 	/* Let inflight controllers teardown complete */
1872 	flush_workqueue(nvmet_wq);
1873 	list_del(&port->global_entry);
1874 
1875 	key_put(port->keyring);
1876 	kfree(port->ana_state);
1877 	kfree(port);
1878 }
1879 
1880 static struct configfs_attribute *nvmet_port_attrs[] = {
1881 	&nvmet_attr_addr_adrfam,
1882 	&nvmet_attr_addr_treq,
1883 	&nvmet_attr_addr_traddr,
1884 	&nvmet_attr_addr_trsvcid,
1885 	&nvmet_attr_addr_trtype,
1886 	&nvmet_attr_addr_tsas,
1887 	&nvmet_attr_param_inline_data_size,
1888 	&nvmet_attr_param_max_queue_size,
1889 #ifdef CONFIG_BLK_DEV_INTEGRITY
1890 	&nvmet_attr_param_pi_enable,
1891 #endif
1892 	NULL,
1893 };
1894 
1895 static struct configfs_item_operations nvmet_port_item_ops = {
1896 	.release		= nvmet_port_release,
1897 };
1898 
1899 static const struct config_item_type nvmet_port_type = {
1900 	.ct_attrs		= nvmet_port_attrs,
1901 	.ct_item_ops		= &nvmet_port_item_ops,
1902 	.ct_owner		= THIS_MODULE,
1903 };
1904 
1905 static struct config_group *nvmet_ports_make(struct config_group *group,
1906 		const char *name)
1907 {
1908 	struct nvmet_port *port;
1909 	u16 portid;
1910 	u32 i;
1911 
1912 	if (kstrtou16(name, 0, &portid))
1913 		return ERR_PTR(-EINVAL);
1914 
1915 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1916 	if (!port)
1917 		return ERR_PTR(-ENOMEM);
1918 
1919 	port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
1920 			sizeof(*port->ana_state), GFP_KERNEL);
1921 	if (!port->ana_state) {
1922 		kfree(port);
1923 		return ERR_PTR(-ENOMEM);
1924 	}
1925 
1926 	if (IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS) && nvme_keyring_id()) {
1927 		port->keyring = key_lookup(nvme_keyring_id());
1928 		if (IS_ERR(port->keyring)) {
1929 			pr_warn("NVMe keyring not available, disabling TLS\n");
1930 			port->keyring = NULL;
1931 		}
1932 	}
1933 
1934 	for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
1935 		if (i == NVMET_DEFAULT_ANA_GRPID)
1936 			port->ana_state[1] = NVME_ANA_OPTIMIZED;
1937 		else
1938 			port->ana_state[i] = NVME_ANA_INACCESSIBLE;
1939 	}
1940 
1941 	list_add(&port->global_entry, &nvmet_ports_list);
1942 
1943 	INIT_LIST_HEAD(&port->entry);
1944 	INIT_LIST_HEAD(&port->subsystems);
1945 	INIT_LIST_HEAD(&port->referrals);
1946 	port->inline_data_size = -1;	/* < 0 == let the transport choose */
1947 	port->max_queue_size = -1;	/* < 0 == let the transport choose */
1948 
1949 	port->disc_addr.portid = cpu_to_le16(portid);
1950 	port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
1951 	port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
1952 	config_group_init_type_name(&port->group, name, &nvmet_port_type);
1953 
1954 	config_group_init_type_name(&port->subsys_group,
1955 			"subsystems", &nvmet_port_subsys_type);
1956 	configfs_add_default_group(&port->subsys_group, &port->group);
1957 
1958 	config_group_init_type_name(&port->referrals_group,
1959 			"referrals", &nvmet_referrals_type);
1960 	configfs_add_default_group(&port->referrals_group, &port->group);
1961 
1962 	config_group_init_type_name(&port->ana_groups_group,
1963 			"ana_groups", &nvmet_ana_groups_type);
1964 	configfs_add_default_group(&port->ana_groups_group, &port->group);
1965 
1966 	port->ana_default_group.port = port;
1967 	port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
1968 	config_group_init_type_name(&port->ana_default_group.group,
1969 			__stringify(NVMET_DEFAULT_ANA_GRPID),
1970 			&nvmet_ana_group_type);
1971 	configfs_add_default_group(&port->ana_default_group.group,
1972 			&port->ana_groups_group);
1973 
1974 	return &port->group;
1975 }
1976 
1977 static struct configfs_group_operations nvmet_ports_group_ops = {
1978 	.make_group		= nvmet_ports_make,
1979 };
1980 
1981 static const struct config_item_type nvmet_ports_type = {
1982 	.ct_group_ops		= &nvmet_ports_group_ops,
1983 	.ct_owner		= THIS_MODULE,
1984 };
1985 
1986 static struct config_group nvmet_subsystems_group;
1987 static struct config_group nvmet_ports_group;
1988 
1989 #ifdef CONFIG_NVME_TARGET_AUTH
1990 static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
1991 		char *page)
1992 {
1993 	u8 *dhchap_secret = to_host(item)->dhchap_secret;
1994 
1995 	if (!dhchap_secret)
1996 		return sprintf(page, "\n");
1997 	return sprintf(page, "%s\n", dhchap_secret);
1998 }
1999 
2000 static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
2001 		const char *page, size_t count)
2002 {
2003 	struct nvmet_host *host = to_host(item);
2004 	int ret;
2005 
2006 	ret = nvmet_auth_set_key(host, page, false);
2007 	/*
2008 	 * Re-authentication is a soft state, so keep the
2009 	 * current authentication valid until the host
2010 	 * requests re-authentication.
2011 	 */
2012 	return ret < 0 ? ret : count;
2013 }
2014 
2015 CONFIGFS_ATTR(nvmet_host_, dhchap_key);
2016 
2017 static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
2018 		char *page)
2019 {
2020 	u8 *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
2021 
2022 	if (!dhchap_secret)
2023 		return sprintf(page, "\n");
2024 	return sprintf(page, "%s\n", dhchap_secret);
2025 }
2026 
2027 static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
2028 		const char *page, size_t count)
2029 {
2030 	struct nvmet_host *host = to_host(item);
2031 	int ret;
2032 
2033 	ret = nvmet_auth_set_key(host, page, true);
2034 	/*
2035 	 * Re-authentication is a soft state, so keep the
2036 	 * current authentication valid until the host
2037 	 * requests re-authentication.
2038 	 */
2039 	return ret < 0 ? ret : count;
2040 }
2041 
2042 CONFIGFS_ATTR(nvmet_host_, dhchap_ctrl_key);
2043 
2044 static ssize_t nvmet_host_dhchap_hash_show(struct config_item *item,
2045 		char *page)
2046 {
2047 	struct nvmet_host *host = to_host(item);
2048 	const char *hash_name = nvme_auth_hmac_name(host->dhchap_hash_id);
2049 
2050 	return sprintf(page, "%s\n", hash_name ? hash_name : "none");
2051 }
2052 
2053 static ssize_t nvmet_host_dhchap_hash_store(struct config_item *item,
2054 		const char *page, size_t count)
2055 {
2056 	struct nvmet_host *host = to_host(item);
2057 	u8 hmac_id;
2058 
2059 	hmac_id = nvme_auth_hmac_id(page);
2060 	if (hmac_id == NVME_AUTH_HASH_INVALID)
2061 		return -EINVAL;
2062 	if (!crypto_has_shash(nvme_auth_hmac_name(hmac_id), 0, 0))
2063 		return -ENOTSUPP;
2064 	host->dhchap_hash_id = hmac_id;
2065 	return count;
2066 }
2067 
2068 CONFIGFS_ATTR(nvmet_host_, dhchap_hash);
2069 
2070 static ssize_t nvmet_host_dhchap_dhgroup_show(struct config_item *item,
2071 		char *page)
2072 {
2073 	struct nvmet_host *host = to_host(item);
2074 	const char *dhgroup = nvme_auth_dhgroup_name(host->dhchap_dhgroup_id);
2075 
2076 	return sprintf(page, "%s\n", dhgroup ? dhgroup : "none");
2077 }
2078 
2079 static ssize_t nvmet_host_dhchap_dhgroup_store(struct config_item *item,
2080 		const char *page, size_t count)
2081 {
2082 	struct nvmet_host *host = to_host(item);
2083 	int dhgroup_id;
2084 
2085 	dhgroup_id = nvme_auth_dhgroup_id(page);
2086 	if (dhgroup_id == NVME_AUTH_DHGROUP_INVALID)
2087 		return -EINVAL;
2088 	if (dhgroup_id != NVME_AUTH_DHGROUP_NULL) {
2089 		const char *kpp = nvme_auth_dhgroup_kpp(dhgroup_id);
2090 
2091 		if (!crypto_has_kpp(kpp, 0, 0))
2092 			return -EINVAL;
2093 	}
2094 	host->dhchap_dhgroup_id = dhgroup_id;
2095 	return count;
2096 }
2097 
2098 CONFIGFS_ATTR(nvmet_host_, dhchap_dhgroup);
2099 
2100 static struct configfs_attribute *nvmet_host_attrs[] = {
2101 	&nvmet_host_attr_dhchap_key,
2102 	&nvmet_host_attr_dhchap_ctrl_key,
2103 	&nvmet_host_attr_dhchap_hash,
2104 	&nvmet_host_attr_dhchap_dhgroup,
2105 	NULL,
2106 };
2107 #endif /* CONFIG_NVME_TARGET_AUTH */
2108 
2109 static void nvmet_host_release(struct config_item *item)
2110 {
2111 	struct nvmet_host *host = to_host(item);
2112 
2113 #ifdef CONFIG_NVME_TARGET_AUTH
2114 	kfree(host->dhchap_secret);
2115 	kfree(host->dhchap_ctrl_secret);
2116 #endif
2117 	kfree(host);
2118 }
2119 
2120 static struct configfs_item_operations nvmet_host_item_ops = {
2121 	.release		= nvmet_host_release,
2122 };
2123 
2124 static const struct config_item_type nvmet_host_type = {
2125 	.ct_item_ops		= &nvmet_host_item_ops,
2126 #ifdef CONFIG_NVME_TARGET_AUTH
2127 	.ct_attrs		= nvmet_host_attrs,
2128 #endif
2129 	.ct_owner		= THIS_MODULE,
2130 };
2131 
2132 static struct config_group *nvmet_hosts_make_group(struct config_group *group,
2133 		const char *name)
2134 {
2135 	struct nvmet_host *host;
2136 
2137 	host = kzalloc(sizeof(*host), GFP_KERNEL);
2138 	if (!host)
2139 		return ERR_PTR(-ENOMEM);
2140 
2141 #ifdef CONFIG_NVME_TARGET_AUTH
2142 	/* Default to SHA256 */
2143 	host->dhchap_hash_id = NVME_AUTH_HASH_SHA256;
2144 #endif
2145 
2146 	config_group_init_type_name(&host->group, name, &nvmet_host_type);
2147 
2148 	return &host->group;
2149 }
2150 
2151 static struct configfs_group_operations nvmet_hosts_group_ops = {
2152 	.make_group		= nvmet_hosts_make_group,
2153 };
2154 
2155 static const struct config_item_type nvmet_hosts_type = {
2156 	.ct_group_ops		= &nvmet_hosts_group_ops,
2157 	.ct_owner		= THIS_MODULE,
2158 };
2159 
2160 static struct config_group nvmet_hosts_group;
2161 
2162 static const struct config_item_type nvmet_root_type = {
2163 	.ct_owner		= THIS_MODULE,
2164 };
2165 
2166 static struct configfs_subsystem nvmet_configfs_subsystem = {
2167 	.su_group = {
2168 		.cg_item = {
2169 			.ci_namebuf	= "nvmet",
2170 			.ci_type	= &nvmet_root_type,
2171 		},
2172 	},
2173 };
2174 
2175 int __init nvmet_init_configfs(void)
2176 {
2177 	int ret;
2178 
2179 	config_group_init(&nvmet_configfs_subsystem.su_group);
2180 	mutex_init(&nvmet_configfs_subsystem.su_mutex);
2181 
2182 	config_group_init_type_name(&nvmet_subsystems_group,
2183 			"subsystems", &nvmet_subsystems_type);
2184 	configfs_add_default_group(&nvmet_subsystems_group,
2185 			&nvmet_configfs_subsystem.su_group);
2186 
2187 	config_group_init_type_name(&nvmet_ports_group,
2188 			"ports", &nvmet_ports_type);
2189 	configfs_add_default_group(&nvmet_ports_group,
2190 			&nvmet_configfs_subsystem.su_group);
2191 
2192 	config_group_init_type_name(&nvmet_hosts_group,
2193 			"hosts", &nvmet_hosts_type);
2194 	configfs_add_default_group(&nvmet_hosts_group,
2195 			&nvmet_configfs_subsystem.su_group);
2196 
2197 	ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
2198 	if (ret) {
2199 		pr_err("configfs_register_subsystem: %d\n", ret);
2200 		return ret;
2201 	}
2202 
2203 	return 0;
2204 }
2205 
2206 void __exit nvmet_exit_configfs(void)
2207 {
2208 	configfs_unregister_subsystem(&nvmet_configfs_subsystem);
2209 }
2210