xref: /freebsd/sys/dev/nvdimm/nvdimm_ns.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /*-
2  * Copyright (c) 2018 Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/bus.h>
33 #include <sys/malloc.h>
34 #include <sys/uuid.h>
35 
36 #include <contrib/dev/acpica/include/acpi.h>
37 #include <dev/acpica/acpivar.h>
38 #include <dev/nvdimm/nvdimm_var.h>
39 
40 int
41 nvdimm_create_namespaces(struct SPA_mapping *spa, ACPI_TABLE_NFIT *nfitbl)
42 {
43 	ACPI_NFIT_MEMORY_MAP **regions;
44 	struct nvdimm_dev *nv;
45 	struct nvdimm_label_entry *e;
46 	struct nvdimm_namespace *ns;
47 	nfit_handle_t dimm_handle;
48 	char *name;
49 	int i, error, num_regions;
50 
51 	acpi_nfit_get_region_mappings_by_spa_range(nfitbl, spa->spa_nfit_idx,
52 	    &regions, &num_regions);
53 	if (num_regions == 0 || num_regions != regions[0]->InterleaveWays) {
54 		free(regions, M_NVDIMM);
55 		return (ENXIO);
56 	}
57 	dimm_handle = regions[0]->DeviceHandle;
58 	nv = nvdimm_find_by_handle(dimm_handle);
59 	if (nv == NULL) {
60 		free(regions, M_NVDIMM);
61 		return (ENXIO);
62 	}
63 	i = 0;
64 	error = 0;
65 	SLIST_FOREACH(e, &nv->labels, link) {
66 		ns = malloc(sizeof(struct nvdimm_namespace), M_NVDIMM,
67 		    M_WAITOK | M_ZERO);
68 		ns->dev.spa_domain = spa->dev.spa_domain;
69 		ns->dev.spa_phys_base = spa->dev.spa_phys_base +
70 		    regions[0]->RegionOffset +
71 		    num_regions *
72 		    (e->label.dimm_phys_addr - regions[0]->Address);
73 		ns->dev.spa_len = num_regions * e->label.raw_size;
74 		ns->dev.spa_efi_mem_flags = spa->dev.spa_efi_mem_flags;
75 		ns->dev.spa_memattr = spa->dev.spa_memattr;
76 		asprintf(&name, M_NVDIMM, "spa%dns%d", spa->spa_nfit_idx, i);
77 		error = nvdimm_spa_dev_init(&ns->dev, name, i);
78 		free(name, M_NVDIMM);
79 		if (error != 0)
80 			break;
81 		SLIST_INSERT_HEAD(&spa->namespaces, ns, link);
82 		i++;
83 	}
84 	free(regions, M_NVDIMM);
85 	return (error);
86 }
87 
88 void
89 nvdimm_destroy_namespaces(struct SPA_mapping *spa)
90 {
91 	struct nvdimm_namespace *ns, *next;
92 
93 	SLIST_FOREACH_SAFE(ns, &spa->namespaces, link, next) {
94 		SLIST_REMOVE_HEAD(&spa->namespaces, link);
95 		nvdimm_spa_dev_fini(&ns->dev);
96 		free(ns, M_NVDIMM);
97 	}
98 }
99