2 * Copyright (c) 2017 The FreeBSD Foundation
4 * Copyright (c) 2018, 2019 Intel Corporation
6 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/bitstring.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
47 #include <sys/sysctl.h>
50 #include <contrib/dev/acpica/include/acpi.h>
51 #include <contrib/dev/acpica/include/accommon.h>
52 #include <contrib/dev/acpica/include/acuuid.h>
53 #include <dev/acpica/acpivar.h>
55 #include <dev/nvdimm/nvdimm_var.h>
57 #define _COMPONENT ACPI_OEM
58 ACPI_MODULE_NAME("NVDIMM")
60 static struct uuid intel_nvdimm_dsm_uuid =
61 {0x4309AC30,0x0D11,0x11E4,0x91,0x91,{0x08,0x00,0x20,0x0C,0x9A,0x66}};
62 #define INTEL_NVDIMM_DSM_REV 1
63 #define INTEL_NVDIMM_DSM_GET_LABEL_SIZE 4
64 #define INTEL_NVDIMM_DSM_GET_LABEL_DATA 5
66 static devclass_t nvdimm_devclass;
67 MALLOC_DEFINE(M_NVDIMM, "nvdimm", "NVDIMM driver memory");
70 read_label_area_size(struct nvdimm_dev *nv)
72 ACPI_OBJECT *result_buffer;
79 handle = nvdimm_root_get_acpi_handle(nv->nv_dev);
82 result.Length = ACPI_ALLOCATE_BUFFER;
83 result.Pointer = NULL;
84 status = acpi_EvaluateDSM(handle, (uint8_t *)&intel_nvdimm_dsm_uuid,
85 INTEL_NVDIMM_DSM_REV, INTEL_NVDIMM_DSM_GET_LABEL_SIZE, NULL,
88 if (ACPI_SUCCESS(status) && result.Pointer != NULL &&
89 result.Length >= sizeof(ACPI_OBJECT)) {
90 result_buffer = result.Pointer;
91 if (result_buffer->Type == ACPI_TYPE_BUFFER &&
92 result_buffer->Buffer.Length >= 12) {
93 out = (uint32_t *)result_buffer->Buffer.Pointer;
94 nv->label_area_size = out[1];
95 nv->max_label_xfer = out[2];
99 if (result.Pointer != NULL)
100 AcpiOsFree(result.Pointer);
105 read_label_area(struct nvdimm_dev *nv, uint8_t *dest, off_t offset,
110 ACPI_OBJECT params_pkg, params_buf, *result_buf;
117 handle = nvdimm_root_get_acpi_handle(nv->nv_dev);
118 if (offset < 0 || length <= 0 ||
119 offset + length > nv->label_area_size ||
122 params_pkg.Type = ACPI_TYPE_PACKAGE;
123 params_pkg.Package.Count = 1;
124 params_pkg.Package.Elements = ¶ms_buf;
125 params_buf.Type = ACPI_TYPE_BUFFER;
126 params_buf.Buffer.Length = sizeof(params);
127 params_buf.Buffer.Pointer = (UINT8 *)params;
129 to_read = MIN(length, nv->max_label_xfer);
132 result.Length = ACPI_ALLOCATE_BUFFER;
133 result.Pointer = NULL;
134 status = acpi_EvaluateDSM(handle,
135 (uint8_t *)&intel_nvdimm_dsm_uuid, INTEL_NVDIMM_DSM_REV,
136 INTEL_NVDIMM_DSM_GET_LABEL_DATA, ¶ms_pkg, &result);
137 if (ACPI_FAILURE(status) ||
138 result.Length < sizeof(ACPI_OBJECT) ||
139 result.Pointer == NULL) {
143 result_buf = (ACPI_OBJECT *)result.Pointer;
144 if (result_buf->Type != ACPI_TYPE_BUFFER ||
145 result_buf->Buffer.Pointer == NULL ||
146 result_buf->Buffer.Length != 4 + to_read ||
147 ((uint16_t *)result_buf->Buffer.Pointer)[0] != 0) {
151 bcopy(result_buf->Buffer.Pointer + 4, dest, to_read);
155 if (result.Pointer != NULL) {
156 AcpiOsFree(result.Pointer);
157 result.Pointer = NULL;
160 if (result.Pointer != NULL)
161 AcpiOsFree(result.Pointer);
166 fletcher64(const void *data, size_t length)
174 d = (const uint32_t *)data;
175 length = length / sizeof(uint32_t);
176 for (i = 0; i < length; i++) {
180 return ((uint64_t)b << 32 | a);
184 label_index_is_valid(struct nvdimm_label_index *index, uint32_t max_labels,
185 size_t size, size_t offset)
189 index = (struct nvdimm_label_index *)((uint8_t *)index + size * offset);
190 if (strcmp(index->signature, NVDIMM_INDEX_BLOCK_SIGNATURE) != 0)
192 checksum = index->checksum;
194 if (checksum != fletcher64(index, size) ||
195 index->this_offset != size * offset || index->this_size != size ||
196 index->other_offset != size * (offset == 0 ? 1 : 0) ||
197 index->seq == 0 || index->seq > 3 || index->slot_cnt > max_labels ||
198 index->label_size != 1)
204 read_label(struct nvdimm_dev *nv, int num)
206 struct nvdimm_label_entry *entry, *i, *next;
211 offset = nv->label_index->label_offset +
212 num * (128 << nv->label_index->label_size);
213 entry = malloc(sizeof(*entry), M_NVDIMM, M_WAITOK);
214 error = read_label_area(nv, (uint8_t *)&entry->label, offset,
215 sizeof(struct nvdimm_label));
217 free(entry, M_NVDIMM);
220 checksum = entry->label.checksum;
221 entry->label.checksum = 0;
222 if (checksum != fletcher64(&entry->label, sizeof(entry->label)) ||
223 entry->label.slot != num) {
224 free(entry, M_NVDIMM);
228 /* Insertion ordered by dimm_phys_addr */
229 if (SLIST_EMPTY(&nv->labels) ||
230 entry->label.dimm_phys_addr <=
231 SLIST_FIRST(&nv->labels)->label.dimm_phys_addr) {
232 SLIST_INSERT_HEAD(&nv->labels, entry, link);
235 SLIST_FOREACH_SAFE(i, &nv->labels, link, next) {
237 entry->label.dimm_phys_addr <= next->label.dimm_phys_addr) {
238 SLIST_INSERT_AFTER(i, entry, link);
242 __assert_unreachable();
246 read_labels(struct nvdimm_dev *nv)
248 struct nvdimm_label_index *indices, *index1;
249 size_t index_size, num_labels;
251 bool index_0_valid, index_1_valid;
253 for (index_size = 256; ; index_size += 256) {
254 num_labels = 8 * (index_size -
255 sizeof(struct nvdimm_label_index));
256 if (index_size + num_labels * sizeof(struct nvdimm_label) >=
260 num_labels = (nv->label_area_size - index_size) /
261 sizeof(struct nvdimm_label);
262 indices = malloc(2 * index_size, M_NVDIMM, M_WAITOK);
263 index1 = (void *)((uint8_t *)indices + index_size);
264 error = read_label_area(nv, (void *)indices, 0, 2 * index_size);
266 free(indices, M_NVDIMM);
269 index_0_valid = label_index_is_valid(indices, num_labels, index_size,
271 index_1_valid = label_index_is_valid(indices, num_labels, index_size,
273 if (!index_0_valid && !index_1_valid) {
274 free(indices, M_NVDIMM);
277 if (index_0_valid && index_1_valid) {
278 if (((int)indices->seq - (int)index1->seq + 3) % 3 == 1) {
279 /* index 0 was more recently updated */
280 index_1_valid = false;
283 * either index 1 was more recently updated,
284 * or the sequence numbers are equal, in which
285 * case the specification says the block with
286 * the higher offset is to be treated as valid
288 index_0_valid = false;
291 nv->label_index = malloc(index_size, M_NVDIMM, M_WAITOK);
292 bcopy(index_0_valid ? indices : index1, nv->label_index, index_size);
293 free(indices, M_NVDIMM);
294 bit_ffc_at((bitstr_t *)nv->label_index->free, 0,
295 nv->label_index->slot_cnt, &n);
298 bit_ffc_at((bitstr_t *)nv->label_index->free, n + 1,
299 nv->label_index->slot_cnt, &n);
305 nvdimm_find_by_handle(nfit_handle_t nv_handle)
307 struct nvdimm_dev *res;
309 int i, error, num_dimms;
312 error = devclass_get_devices(nvdimm_devclass, &dimms, &num_dimms);
315 for (i = 0; i < num_dimms; i++) {
316 if (nvdimm_root_get_device_handle(dimms[i]) == nv_handle) {
317 res = device_get_softc(dimms[i]);
326 nvdimm_probe(device_t dev)
329 return (BUS_PROBE_NOWILDCARD);
333 nvdimm_attach(device_t dev)
335 struct nvdimm_dev *nv;
336 struct sysctl_ctx_list *ctx;
337 struct sysctl_oid *oid;
338 struct sysctl_oid_list *children;
340 ACPI_TABLE_NFIT *nfitbl;
342 ACPI_NFIT_MEMORY_MAP **maps;
343 int error, i, num_maps;
346 nv = device_get_softc(dev);
347 ctx = device_get_sysctl_ctx(dev);
348 oid = device_get_sysctl_tree(dev);
349 children = SYSCTL_CHILDREN(oid);
350 MPASS(nvdimm_root_get_acpi_handle(dev) != NULL);
352 nv->nv_handle = nvdimm_root_get_device_handle(dev);
354 status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
355 if (ACPI_FAILURE(status)) {
357 device_printf(dev, "cannot get NFIT\n");
360 acpi_nfit_get_flush_addrs(nfitbl, nv->nv_handle, &nv->nv_flush_addr,
361 &nv->nv_flush_addr_cnt);
364 * Each NVDIMM should have at least one memory map associated with it.
365 * If any of the maps have one of the error flags set, reflect that in
366 * the overall status.
368 acpi_nfit_get_memory_maps_by_dimm(nfitbl, nv->nv_handle, &maps,
371 free(nv->nv_flush_addr, M_NVDIMM);
372 free(maps, M_NVDIMM);
373 device_printf(dev, "cannot find memory map\n");
377 for (i = 0; i < num_maps; i++) {
378 flags |= maps[i]->Flags;
380 free(maps, M_NVDIMM);
382 /* sbuf_new_auto(9) is M_WAITOK; no need to check for NULL. */
383 sb = sbuf_new_auto();
384 (void) sbuf_printf(sb, "0x%b", flags,
390 "\005HEALTH_OBSERVED"
393 error = sbuf_finish(sb);
396 free(nv->nv_flush_addr, M_NVDIMM);
397 device_printf(dev, "cannot convert flags to string\n");
400 /* strdup(9) is M_WAITOK; no need to check for NULL. */
401 nv->nv_flags_str = strdup(sbuf_data(sb), M_NVDIMM);
403 SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "flags",
404 CTLFLAG_RD | CTLFLAG_MPSAFE, nv->nv_flags_str, 0,
405 "NVDIMM State Flags");
407 * Anything other than HEALTH_ENABLED indicates a fault condition of
408 * some kind, so log if that's seen.
410 if ((flags & ~ACPI_NFIT_MEM_HEALTH_ENABLED) != 0)
411 device_printf(dev, "flags: %s\n", nv->nv_flags_str);
413 AcpiPutTable(&nfitbl->Header);
414 error = read_label_area_size(nv);
417 * Ignoring errors reading labels. Not all NVDIMMs
418 * support labels and namespaces.
426 nvdimm_detach(device_t dev)
428 struct nvdimm_dev *nv;
429 struct nvdimm_label_entry *label, *next;
431 nv = device_get_softc(dev);
432 free(nv->nv_flags_str, M_NVDIMM);
433 free(nv->nv_flush_addr, M_NVDIMM);
434 free(nv->label_index, M_NVDIMM);
435 SLIST_FOREACH_SAFE(label, &nv->labels, link, next) {
436 SLIST_REMOVE_HEAD(&nv->labels, link);
437 free(label, M_NVDIMM);
443 nvdimm_suspend(device_t dev)
450 nvdimm_resume(device_t dev)
456 static device_method_t nvdimm_methods[] = {
457 DEVMETHOD(device_probe, nvdimm_probe),
458 DEVMETHOD(device_attach, nvdimm_attach),
459 DEVMETHOD(device_detach, nvdimm_detach),
460 DEVMETHOD(device_suspend, nvdimm_suspend),
461 DEVMETHOD(device_resume, nvdimm_resume),
465 static driver_t nvdimm_driver = {
468 sizeof(struct nvdimm_dev),
471 DRIVER_MODULE(nvdimm, nvdimm_acpi_root, nvdimm_driver, nvdimm_devclass, NULL,
473 MODULE_DEPEND(nvdimm, acpi, 1, 1, 1);