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>
48 #include <contrib/dev/acpica/include/acpi.h>
49 #include <contrib/dev/acpica/include/accommon.h>
50 #include <contrib/dev/acpica/include/acuuid.h>
51 #include <dev/acpica/acpivar.h>
53 #include <dev/nvdimm/nvdimm_var.h>
55 #define _COMPONENT ACPI_OEM
56 ACPI_MODULE_NAME("NVDIMM")
58 static struct uuid intel_nvdimm_dsm_uuid =
59 {0x4309AC30,0x0D11,0x11E4,0x91,0x91,{0x08,0x00,0x20,0x0C,0x9A,0x66}};
60 #define INTEL_NVDIMM_DSM_REV 1
61 #define INTEL_NVDIMM_DSM_GET_LABEL_SIZE 4
62 #define INTEL_NVDIMM_DSM_GET_LABEL_DATA 5
64 static devclass_t nvdimm_devclass;
65 MALLOC_DEFINE(M_NVDIMM, "nvdimm", "NVDIMM driver memory");
68 read_label_area_size(struct nvdimm_dev *nv)
70 ACPI_OBJECT *result_buffer;
77 handle = nvdimm_root_get_acpi_handle(nv->nv_dev);
80 result.Length = ACPI_ALLOCATE_BUFFER;
81 result.Pointer = NULL;
82 status = acpi_EvaluateDSM(handle, (uint8_t *)&intel_nvdimm_dsm_uuid,
83 INTEL_NVDIMM_DSM_REV, INTEL_NVDIMM_DSM_GET_LABEL_SIZE, NULL,
86 if (ACPI_SUCCESS(status) && result.Pointer != NULL &&
87 result.Length >= sizeof(ACPI_OBJECT)) {
88 result_buffer = result.Pointer;
89 if (result_buffer->Type == ACPI_TYPE_BUFFER &&
90 result_buffer->Buffer.Length >= 12) {
91 out = (uint32_t *)result_buffer->Buffer.Pointer;
92 nv->label_area_size = out[1];
93 nv->max_label_xfer = out[2];
97 if (result.Pointer != NULL)
98 AcpiOsFree(result.Pointer);
103 read_label_area(struct nvdimm_dev *nv, uint8_t *dest, off_t offset,
108 ACPI_OBJECT params_pkg, params_buf, *result_buf;
115 handle = nvdimm_root_get_acpi_handle(nv->nv_dev);
116 if (offset < 0 || length <= 0 ||
117 offset + length > nv->label_area_size ||
120 params_pkg.Type = ACPI_TYPE_PACKAGE;
121 params_pkg.Package.Count = 1;
122 params_pkg.Package.Elements = ¶ms_buf;
123 params_buf.Type = ACPI_TYPE_BUFFER;
124 params_buf.Buffer.Length = sizeof(params);
125 params_buf.Buffer.Pointer = (UINT8 *)params;
127 to_read = MIN(length, nv->max_label_xfer);
130 result.Length = ACPI_ALLOCATE_BUFFER;
131 result.Pointer = NULL;
132 status = acpi_EvaluateDSM(handle,
133 (uint8_t *)&intel_nvdimm_dsm_uuid, INTEL_NVDIMM_DSM_REV,
134 INTEL_NVDIMM_DSM_GET_LABEL_DATA, ¶ms_pkg, &result);
135 if (ACPI_FAILURE(status) ||
136 result.Length < sizeof(ACPI_OBJECT) ||
137 result.Pointer == NULL) {
141 result_buf = (ACPI_OBJECT *)result.Pointer;
142 if (result_buf->Type != ACPI_TYPE_BUFFER ||
143 result_buf->Buffer.Pointer == NULL ||
144 result_buf->Buffer.Length != 4 + to_read ||
145 ((uint16_t *)result_buf->Buffer.Pointer)[0] != 0) {
149 bcopy(result_buf->Buffer.Pointer + 4, dest, to_read);
153 if (result.Pointer != NULL) {
154 AcpiOsFree(result.Pointer);
155 result.Pointer = NULL;
158 if (result.Pointer != NULL)
159 AcpiOsFree(result.Pointer);
164 fletcher64(const void *data, size_t length)
172 d = (const uint32_t *)data;
173 length = length / sizeof(uint32_t);
174 for (i = 0; i < length; i++) {
178 return ((uint64_t)b << 32 | a);
182 label_index_is_valid(struct nvdimm_label_index *index, uint32_t max_labels,
183 size_t size, size_t offset)
187 index = (struct nvdimm_label_index *)((uint8_t *)index + size * offset);
188 if (strcmp(index->signature, NVDIMM_INDEX_BLOCK_SIGNATURE) != 0)
190 checksum = index->checksum;
192 if (checksum != fletcher64(index, size) ||
193 index->this_offset != size * offset || index->this_size != size ||
194 index->other_offset != size * (offset == 0 ? 1 : 0) ||
195 index->seq == 0 || index->seq > 3 || index->slot_cnt > max_labels ||
196 index->label_size != 1)
202 read_label(struct nvdimm_dev *nv, int num)
204 struct nvdimm_label_entry *entry, *i, *next;
209 offset = nv->label_index->label_offset +
210 num * (128 << nv->label_index->label_size);
211 entry = malloc(sizeof(*entry), M_NVDIMM, M_WAITOK);
212 error = read_label_area(nv, (uint8_t *)&entry->label, offset,
213 sizeof(struct nvdimm_label));
215 free(entry, M_NVDIMM);
218 checksum = entry->label.checksum;
219 entry->label.checksum = 0;
220 if (checksum != fletcher64(&entry->label, sizeof(entry->label)) ||
221 entry->label.slot != num) {
222 free(entry, M_NVDIMM);
226 /* Insertion ordered by dimm_phys_addr */
227 if (SLIST_EMPTY(&nv->labels) ||
228 entry->label.dimm_phys_addr <=
229 SLIST_FIRST(&nv->labels)->label.dimm_phys_addr) {
230 SLIST_INSERT_HEAD(&nv->labels, entry, link);
233 SLIST_FOREACH_SAFE(i, &nv->labels, link, next) {
235 entry->label.dimm_phys_addr <= next->label.dimm_phys_addr) {
236 SLIST_INSERT_AFTER(i, entry, link);
240 __assert_unreachable();
244 read_labels(struct nvdimm_dev *nv)
246 struct nvdimm_label_index *indices, *index1;
247 size_t bitfield_size, index_size, num_labels;
249 bool index_0_valid, index_1_valid;
251 for (index_size = 256; ; index_size += 256) {
252 num_labels = 8 * (index_size -
253 sizeof(struct nvdimm_label_index));
254 if (index_size + num_labels * sizeof(struct nvdimm_label) >=
258 num_labels = (nv->label_area_size - index_size) /
259 sizeof(struct nvdimm_label);
260 bitfield_size = roundup2(num_labels, 8) / 8;
261 indices = malloc(2 * index_size, M_NVDIMM, M_WAITOK);
262 index1 = (void *)((uint8_t *)indices + index_size);
263 error = read_label_area(nv, (void *)indices, 0, 2 * index_size);
265 free(indices, M_NVDIMM);
268 index_0_valid = label_index_is_valid(indices, num_labels, index_size,
270 index_1_valid = label_index_is_valid(indices, num_labels, index_size,
272 if (!index_0_valid && !index_1_valid) {
273 free(indices, M_NVDIMM);
276 if (index_0_valid && index_1_valid) {
277 if (((int)indices->seq - (int)index1->seq + 3) % 3 == 1) {
278 /* index 0 was more recently updated */
279 index_1_valid = false;
282 * either index 1 was more recently updated,
283 * or the sequence numbers are equal, in which
284 * case the specification says the block with
285 * the higher offset is to be treated as valid
287 index_0_valid = false;
290 nv->label_index = malloc(index_size, M_NVDIMM, M_WAITOK);
291 bcopy(index_0_valid ? indices : index1, nv->label_index, index_size);
292 free(indices, M_NVDIMM);
293 bit_ffc_at((bitstr_t *)nv->label_index->free, 0,
294 nv->label_index->slot_cnt, &n);
297 bit_ffc_at((bitstr_t *)nv->label_index->free, n + 1,
298 nv->label_index->slot_cnt, &n);
304 nvdimm_find_by_handle(nfit_handle_t nv_handle)
306 struct nvdimm_dev *res;
308 int i, error, num_dimms;
311 error = devclass_get_devices(nvdimm_devclass, &dimms, &num_dimms);
314 for (i = 0; i < num_dimms; i++) {
315 if (nvdimm_root_get_device_handle(dimms[i]) == nv_handle) {
316 res = device_get_softc(dimms[i]);
325 nvdimm_probe(device_t dev)
328 return (BUS_PROBE_NOWILDCARD);
332 nvdimm_attach(device_t dev)
334 struct nvdimm_dev *nv;
335 ACPI_TABLE_NFIT *nfitbl;
340 nv = device_get_softc(dev);
341 handle = nvdimm_root_get_acpi_handle(dev);
342 MPASS(handle != NULL);
344 nv->nv_handle = nvdimm_root_get_device_handle(dev);
346 status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
347 if (ACPI_FAILURE(status)) {
349 device_printf(dev, "cannot get NFIT\n");
352 acpi_nfit_get_flush_addrs(nfitbl, nv->nv_handle, &nv->nv_flush_addr,
353 &nv->nv_flush_addr_cnt);
354 AcpiPutTable(&nfitbl->Header);
355 error = read_label_area_size(nv);
358 * Ignoring errors reading labels. Not all NVDIMMs
359 * support labels and namespaces.
367 nvdimm_detach(device_t dev)
369 struct nvdimm_dev *nv;
370 struct nvdimm_label_entry *label, *next;
372 nv = device_get_softc(dev);
373 free(nv->nv_flush_addr, M_NVDIMM);
374 free(nv->label_index, M_NVDIMM);
375 SLIST_FOREACH_SAFE(label, &nv->labels, link, next) {
376 SLIST_REMOVE_HEAD(&nv->labels, link);
377 free(label, M_NVDIMM);
383 nvdimm_suspend(device_t dev)
390 nvdimm_resume(device_t dev)
396 static device_method_t nvdimm_methods[] = {
397 DEVMETHOD(device_probe, nvdimm_probe),
398 DEVMETHOD(device_attach, nvdimm_attach),
399 DEVMETHOD(device_detach, nvdimm_detach),
400 DEVMETHOD(device_suspend, nvdimm_suspend),
401 DEVMETHOD(device_resume, nvdimm_resume),
405 static driver_t nvdimm_driver = {
408 sizeof(struct nvdimm_dev),
411 DRIVER_MODULE(nvdimm, nvdimm_acpi_root, nvdimm_driver, nvdimm_devclass, NULL,
413 MODULE_DEPEND(nvdimm, acpi, 1, 1, 1);