xref: /freebsd/sys/dev/nvdimm/nvdimm.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * Copyright (c) 2017 The FreeBSD Foundation
3  * All rights reserved.
4  * Copyright (c) 2018, 2019 Intel Corporation
5  *
6  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #include "opt_acpi.h"
33 #include "opt_ddb.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bio.h>
38 #include <sys/bitstring.h>
39 #include <sys/bus.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/sbuf.h>
45 #include <sys/sysctl.h>
46 #include <sys/uuid.h>
47 
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>
52 
53 #include <dev/nvdimm/nvdimm_var.h>
54 
55 #define _COMPONENT	ACPI_OEM
56 ACPI_MODULE_NAME("NVDIMM")
57 
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
63 
64 MALLOC_DEFINE(M_NVDIMM, "nvdimm", "NVDIMM driver memory");
65 
66 static int
67 read_label_area_size(struct nvdimm_dev *nv)
68 {
69 	ACPI_OBJECT *result_buffer;
70 	ACPI_HANDLE handle;
71 	ACPI_STATUS status;
72 	ACPI_BUFFER result;
73 	uint32_t *out;
74 	int error;
75 
76 	handle = nvdimm_root_get_acpi_handle(nv->nv_dev);
77 	if (handle == NULL)
78 		return (ENODEV);
79 	result.Length = ACPI_ALLOCATE_BUFFER;
80 	result.Pointer = NULL;
81 	status = acpi_EvaluateDSM(handle, (uint8_t *)&intel_nvdimm_dsm_uuid,
82 	    INTEL_NVDIMM_DSM_REV, INTEL_NVDIMM_DSM_GET_LABEL_SIZE, NULL,
83 	    &result);
84 	error = ENXIO;
85 	if (ACPI_SUCCESS(status) && result.Pointer != NULL &&
86 	    result.Length >= sizeof(ACPI_OBJECT)) {
87 		result_buffer = result.Pointer;
88 		if (result_buffer->Type == ACPI_TYPE_BUFFER &&
89 		    result_buffer->Buffer.Length >= 12) {
90 			out = (uint32_t *)result_buffer->Buffer.Pointer;
91 			nv->label_area_size = out[1];
92 			nv->max_label_xfer = out[2];
93 			error = 0;
94 		}
95 	}
96 	if (result.Pointer != NULL)
97 		AcpiOsFree(result.Pointer);
98 	return (error);
99 }
100 
101 static int
102 read_label_area(struct nvdimm_dev *nv, uint8_t *dest, off_t offset,
103     off_t length)
104 {
105 	ACPI_BUFFER result;
106 	ACPI_HANDLE handle;
107 	ACPI_OBJECT params_pkg, params_buf, *result_buf;
108 	ACPI_STATUS status;
109 	uint32_t params[2];
110 	off_t to_read;
111 	int error;
112 
113 	error = 0;
114 	handle = nvdimm_root_get_acpi_handle(nv->nv_dev);
115 	if (offset < 0 || length <= 0 ||
116 	    offset + length > nv->label_area_size ||
117 	    handle == NULL)
118 		return (ENODEV);
119 	params_pkg.Type = ACPI_TYPE_PACKAGE;
120 	params_pkg.Package.Count = 1;
121 	params_pkg.Package.Elements = &params_buf;
122 	params_buf.Type = ACPI_TYPE_BUFFER;
123 	params_buf.Buffer.Length = sizeof(params);
124 	params_buf.Buffer.Pointer = (UINT8 *)params;
125 	while (length > 0) {
126 		to_read = MIN(length, nv->max_label_xfer);
127 		params[0] = offset;
128 		params[1] = to_read;
129 		result.Length = ACPI_ALLOCATE_BUFFER;
130 		result.Pointer = NULL;
131 		status = acpi_EvaluateDSM(handle,
132 		    (uint8_t *)&intel_nvdimm_dsm_uuid, INTEL_NVDIMM_DSM_REV,
133 		    INTEL_NVDIMM_DSM_GET_LABEL_DATA, &params_pkg, &result);
134 		if (ACPI_FAILURE(status) ||
135 		    result.Length < sizeof(ACPI_OBJECT) ||
136 		    result.Pointer == NULL) {
137 			error = ENXIO;
138 			break;
139 		}
140 		result_buf = (ACPI_OBJECT *)result.Pointer;
141 		if (result_buf->Type != ACPI_TYPE_BUFFER ||
142 		    result_buf->Buffer.Pointer == NULL ||
143 		    result_buf->Buffer.Length != 4 + to_read ||
144 		    ((uint16_t *)result_buf->Buffer.Pointer)[0] != 0) {
145 			error = ENXIO;
146 			break;
147 		}
148 		bcopy(result_buf->Buffer.Pointer + 4, dest, to_read);
149 		dest += to_read;
150 		offset += to_read;
151 		length -= to_read;
152 		if (result.Pointer != NULL) {
153 			AcpiOsFree(result.Pointer);
154 			result.Pointer = NULL;
155 		}
156 	}
157 	if (result.Pointer != NULL)
158 		AcpiOsFree(result.Pointer);
159 	return (error);
160 }
161 
162 static uint64_t
163 fletcher64(const void *data, size_t length)
164 {
165 	size_t i;
166 	uint32_t a, b;
167 	const uint32_t *d;
168 
169 	a = 0;
170 	b = 0;
171 	d = (const uint32_t *)data;
172 	length = length / sizeof(uint32_t);
173 	for (i = 0; i < length; i++) {
174 		a += d[i];
175 		b += a;
176 	}
177 	return ((uint64_t)b << 32 | a);
178 }
179 
180 static bool
181 label_index_is_valid(struct nvdimm_label_index *index, uint32_t max_labels,
182     size_t size, size_t offset)
183 {
184 	uint64_t checksum;
185 
186 	index = (struct nvdimm_label_index *)((uint8_t *)index + size * offset);
187 	if (strcmp(index->signature, NVDIMM_INDEX_BLOCK_SIGNATURE) != 0)
188 		return false;
189 	checksum = index->checksum;
190 	index->checksum = 0;
191 	if (checksum != fletcher64(index, size) ||
192 	    index->this_offset != size * offset || index->this_size != size ||
193 	    index->other_offset != size * (offset == 0 ? 1 : 0) ||
194 	    index->seq == 0 || index->seq > 3 || index->slot_cnt > max_labels ||
195 	    index->label_size != 1)
196 		return false;
197 	return true;
198 }
199 
200 static int
201 read_label(struct nvdimm_dev *nv, int num)
202 {
203 	struct nvdimm_label_entry *entry, *i, *next;
204 	uint64_t checksum;
205 	off_t offset;
206 	int error;
207 
208 	offset = nv->label_index->label_offset +
209 	    num * (128 << nv->label_index->label_size);
210 	entry = malloc(sizeof(*entry), M_NVDIMM, M_WAITOK);
211 	error = read_label_area(nv, (uint8_t *)&entry->label, offset,
212 	    sizeof(struct nvdimm_label));
213 	if (error != 0) {
214 		free(entry, M_NVDIMM);
215 		return (error);
216 	}
217 	checksum = entry->label.checksum;
218 	entry->label.checksum = 0;
219 	if (checksum != fletcher64(&entry->label, sizeof(entry->label)) ||
220 	    entry->label.slot != num) {
221 		free(entry, M_NVDIMM);
222 		return (ENXIO);
223 	}
224 
225 	/* Insertion ordered by dimm_phys_addr */
226 	if (SLIST_EMPTY(&nv->labels) ||
227 	    entry->label.dimm_phys_addr <=
228 	    SLIST_FIRST(&nv->labels)->label.dimm_phys_addr) {
229 		SLIST_INSERT_HEAD(&nv->labels, entry, link);
230 		return (0);
231 	}
232 	SLIST_FOREACH_SAFE(i, &nv->labels, link, next) {
233 		if (next == NULL ||
234 		    entry->label.dimm_phys_addr <= next->label.dimm_phys_addr) {
235 			SLIST_INSERT_AFTER(i, entry, link);
236 			return (0);
237 		}
238 	}
239 	__assert_unreachable();
240 }
241 
242 static int
243 read_labels(struct nvdimm_dev *nv)
244 {
245 	struct nvdimm_label_index *indices, *index1;
246 	size_t index_size, num_labels;
247 	int error, n;
248 	bool index_0_valid, index_1_valid;
249 
250 	for (index_size = 256; ; index_size += 256) {
251 		num_labels = 8 * (index_size -
252 		    sizeof(struct nvdimm_label_index));
253 		if (index_size + num_labels * sizeof(struct nvdimm_label) >=
254 		    nv->label_area_size)
255 			break;
256 	}
257 	num_labels = (nv->label_area_size - index_size) /
258 	    sizeof(struct nvdimm_label);
259 	indices = malloc(2 * index_size, M_NVDIMM, M_WAITOK);
260 	index1 = (void *)((uint8_t *)indices + index_size);
261 	error = read_label_area(nv, (void *)indices, 0, 2 * index_size);
262 	if (error != 0) {
263 		free(indices, M_NVDIMM);
264 		return (error);
265 	}
266 	index_0_valid = label_index_is_valid(indices, num_labels, index_size,
267 	    0);
268 	index_1_valid = label_index_is_valid(indices, num_labels, index_size,
269 	    1);
270 	if (!index_0_valid && !index_1_valid) {
271 		free(indices, M_NVDIMM);
272 		return (ENXIO);
273 	}
274 	if (index_0_valid && index_1_valid) {
275 		if (((int)indices->seq - (int)index1->seq + 3) % 3 == 1) {
276 			/* index 0 was more recently updated */
277 			index_1_valid = false;
278 		} else {
279 			/*
280 			 * either index 1 was more recently updated,
281 			 * or the sequence numbers are equal, in which
282 			 * case the specification says the block with
283 			 * the higher offset is to be treated as valid
284 			 */
285 			index_0_valid = false;
286 		}
287 	}
288 	nv->label_index = malloc(index_size, M_NVDIMM, M_WAITOK);
289 	bcopy(index_0_valid ? indices : index1, nv->label_index, index_size);
290 	free(indices, M_NVDIMM);
291 	bit_ffc_at((bitstr_t *)nv->label_index->free, 0,
292 	    nv->label_index->slot_cnt, &n);
293 	while (n >= 0) {
294 		read_label(nv, n);
295 		bit_ffc_at((bitstr_t *)nv->label_index->free, n + 1,
296 		    nv->label_index->slot_cnt, &n);
297 	}
298 	return (0);
299 }
300 
301 static int
302 nvdimm_probe(device_t dev)
303 {
304 
305 	return (BUS_PROBE_NOWILDCARD);
306 }
307 
308 static int
309 nvdimm_attach(device_t dev)
310 {
311 	struct nvdimm_dev *nv;
312 	struct sysctl_ctx_list *ctx;
313 	struct sysctl_oid *oid;
314 	struct sysctl_oid_list *children;
315 	struct sbuf *sb;
316 	ACPI_TABLE_NFIT *nfitbl;
317 	ACPI_STATUS status;
318 	ACPI_NFIT_MEMORY_MAP **maps;
319 	int error, i, num_maps;
320 	uint16_t flags;
321 
322 	nv = device_get_softc(dev);
323 	ctx = device_get_sysctl_ctx(dev);
324 	oid = device_get_sysctl_tree(dev);
325 	children = SYSCTL_CHILDREN(oid);
326 	MPASS(nvdimm_root_get_acpi_handle(dev) != NULL);
327 	nv->nv_dev = dev;
328 	nv->nv_handle = nvdimm_root_get_device_handle(dev);
329 
330 	status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
331 	if (ACPI_FAILURE(status)) {
332 		if (bootverbose)
333 			device_printf(dev, "cannot get NFIT\n");
334 		return (ENXIO);
335 	}
336 	acpi_nfit_get_flush_addrs(nfitbl, nv->nv_handle, &nv->nv_flush_addr,
337 	    &nv->nv_flush_addr_cnt);
338 
339 	/*
340 	 * Each NVDIMM should have at least one memory map associated with it.
341 	 * If any of the maps have one of the error flags set, reflect that in
342 	 * the overall status.
343 	 */
344 	acpi_nfit_get_memory_maps_by_dimm(nfitbl, nv->nv_handle, &maps,
345 	    &num_maps);
346 	if (num_maps == 0) {
347 		free(nv->nv_flush_addr, M_NVDIMM);
348 		free(maps, M_NVDIMM);
349 		device_printf(dev, "cannot find memory map\n");
350 		return (ENXIO);
351 	}
352 	flags = 0;
353 	for (i = 0; i < num_maps; i++) {
354 		flags |= maps[i]->Flags;
355 	}
356 	free(maps, M_NVDIMM);
357 
358 	/* sbuf_new_auto(9) is M_WAITOK; no need to check for NULL. */
359 	sb = sbuf_new_auto();
360 	(void) sbuf_printf(sb, "0x%b", flags,
361 	    "\20"
362 	    "\001SAVE_FAILED"
363 	    "\002RESTORE_FAILED"
364 	    "\003FLUSH_FAILED"
365 	    "\004NOT_ARMED"
366 	    "\005HEALTH_OBSERVED"
367 	    "\006HEALTH_ENABLED"
368 	    "\007MAP_FAILED");
369 	error = sbuf_finish(sb);
370 	if (error != 0) {
371 		sbuf_delete(sb);
372 		free(nv->nv_flush_addr, M_NVDIMM);
373 		device_printf(dev, "cannot convert flags to string\n");
374 		return (error);
375 	}
376 	/* strdup(9) is M_WAITOK; no need to check for NULL. */
377 	nv->nv_flags_str = strdup(sbuf_data(sb), M_NVDIMM);
378 	sbuf_delete(sb);
379 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "flags",
380 	    CTLFLAG_RD | CTLFLAG_MPSAFE, nv->nv_flags_str, 0,
381 	    "NVDIMM State Flags");
382 	/*
383 	 * Anything other than HEALTH_ENABLED indicates a fault condition of
384 	 * some kind, so log if that's seen.
385 	 */
386 	if ((flags & ~ACPI_NFIT_MEM_HEALTH_ENABLED) != 0)
387 		device_printf(dev, "flags: %s\n", nv->nv_flags_str);
388 
389 	AcpiPutTable(&nfitbl->Header);
390 	error = read_label_area_size(nv);
391 	if (error == 0) {
392 		/*
393 		 * Ignoring errors reading labels. Not all NVDIMMs
394 		 * support labels and namespaces.
395 		 */
396 		read_labels(nv);
397 	}
398 	return (0);
399 }
400 
401 static int
402 nvdimm_detach(device_t dev)
403 {
404 	struct nvdimm_dev *nv;
405 	struct nvdimm_label_entry *label, *next;
406 
407 	nv = device_get_softc(dev);
408 	free(nv->nv_flags_str, M_NVDIMM);
409 	free(nv->nv_flush_addr, M_NVDIMM);
410 	free(nv->label_index, M_NVDIMM);
411 	SLIST_FOREACH_SAFE(label, &nv->labels, link, next) {
412 		SLIST_REMOVE_HEAD(&nv->labels, link);
413 		free(label, M_NVDIMM);
414 	}
415 	return (0);
416 }
417 
418 static int
419 nvdimm_suspend(device_t dev)
420 {
421 
422 	return (0);
423 }
424 
425 static int
426 nvdimm_resume(device_t dev)
427 {
428 
429 	return (0);
430 }
431 
432 static device_method_t nvdimm_methods[] = {
433 	DEVMETHOD(device_probe, nvdimm_probe),
434 	DEVMETHOD(device_attach, nvdimm_attach),
435 	DEVMETHOD(device_detach, nvdimm_detach),
436 	DEVMETHOD(device_suspend, nvdimm_suspend),
437 	DEVMETHOD(device_resume, nvdimm_resume),
438 	DEVMETHOD_END
439 };
440 
441 static driver_t	nvdimm_driver = {
442 	"nvdimm",
443 	nvdimm_methods,
444 	sizeof(struct nvdimm_dev),
445 };
446 
447 struct nvdimm_dev *
448 nvdimm_find_by_handle(nfit_handle_t nv_handle)
449 {
450 	struct nvdimm_dev *res;
451 	device_t *dimms;
452 	int i, error, num_dimms;
453 
454 	res = NULL;
455 	error = devclass_get_devices(devclass_find(nvdimm_driver.name), &dimms,
456 	    &num_dimms);
457 	if (error != 0)
458 		return (NULL);
459 	for (i = 0; i < num_dimms; i++) {
460 		if (nvdimm_root_get_device_handle(dimms[i]) == nv_handle) {
461 			res = device_get_softc(dimms[i]);
462 			break;
463 		}
464 	}
465 	free(dimms, M_TEMP);
466 	return (res);
467 }
468 
469 DRIVER_MODULE(nvdimm, nvdimm_acpi_root, nvdimm_driver, NULL, NULL);
470 MODULE_DEPEND(nvdimm, acpi, 1, 1, 1);
471