2 * Copyright (c) 2018 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
33 #include <sys/malloc.h>
36 #include <contrib/dev/acpica/include/acpi.h>
37 #include <dev/acpica/acpivar.h>
38 #include <dev/nvdimm/nvdimm_var.h>
41 nvdimm_create_namespaces(struct SPA_mapping *spa, ACPI_TABLE_NFIT *nfitbl)
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;
49 int i, error, num_regions;
51 acpi_nfit_get_region_mappings_by_spa_range(nfitbl, spa->spa_nfit_idx,
52 ®ions, &num_regions);
53 if (num_regions == 0 || num_regions != regions[0]->InterleaveWays) {
54 free(regions, M_NVDIMM);
57 dimm_handle = regions[0]->DeviceHandle;
58 nv = nvdimm_find_by_handle(dimm_handle);
60 free(regions, M_NVDIMM);
65 SLIST_FOREACH(e, &nv->labels, link) {
66 ns = malloc(sizeof(struct nvdimm_namespace), M_NVDIMM,
68 ns->dev.spa_domain = spa->dev.spa_domain;
69 ns->dev.spa_phys_base = spa->dev.spa_phys_base +
70 regions[0]->RegionOffset +
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);
81 SLIST_INSERT_HEAD(&spa->namespaces, ns, link);
84 free(regions, M_NVDIMM);
89 nvdimm_destroy_namespaces(struct SPA_mapping *spa)
91 struct nvdimm_namespace *ns, *next;
93 SLIST_FOREACH_SAFE(ns, &spa->namespaces, link, next) {
94 SLIST_REMOVE_HEAD(&spa->namespaces, link);
95 nvdimm_spa_dev_fini(&ns->dev);