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