xref: /freebsd/sys/dev/nvdimm/nvdimm.c (revision b9e5884ef786a6c0fd203064baa07ef308025707)
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 __FBSDID("$FreeBSD$");
33 
34 #include "opt_acpi.h"
35 #include "opt_ddb.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bio.h>
40 #include <sys/bitstring.h>
41 #include <sys/bus.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/sbuf.h>
47 #include <sys/sysctl.h>
48 #include <sys/uuid.h>
49 
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>
54 
55 #include <dev/nvdimm/nvdimm_var.h>
56 
57 #define _COMPONENT	ACPI_OEM
58 ACPI_MODULE_NAME("NVDIMM")
59 
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
65 
66 static devclass_t nvdimm_devclass;
67 MALLOC_DEFINE(M_NVDIMM, "nvdimm", "NVDIMM driver memory");
68 
69 static int
70 read_label_area_size(struct nvdimm_dev *nv)
71 {
72 	ACPI_OBJECT *result_buffer;
73 	ACPI_HANDLE handle;
74 	ACPI_STATUS status;
75 	ACPI_BUFFER result;
76 	uint32_t *out;
77 	int error;
78 
79 	handle = nvdimm_root_get_acpi_handle(nv->nv_dev);
80 	if (handle == NULL)
81 		return (ENODEV);
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,
86 	    &result);
87 	error = ENXIO;
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];
96 			error = 0;
97 		}
98 	}
99 	if (result.Pointer != NULL)
100 		AcpiOsFree(result.Pointer);
101 	return (error);
102 }
103 
104 static int
105 read_label_area(struct nvdimm_dev *nv, uint8_t *dest, off_t offset,
106     off_t length)
107 {
108 	ACPI_BUFFER result;
109 	ACPI_HANDLE handle;
110 	ACPI_OBJECT params_pkg, params_buf, *result_buf;
111 	ACPI_STATUS status;
112 	uint32_t params[2];
113 	off_t to_read;
114 	int error;
115 
116 	error = 0;
117 	handle = nvdimm_root_get_acpi_handle(nv->nv_dev);
118 	if (offset < 0 || length <= 0 ||
119 	    offset + length > nv->label_area_size ||
120 	    handle == NULL)
121 		return (ENODEV);
122 	params_pkg.Type = ACPI_TYPE_PACKAGE;
123 	params_pkg.Package.Count = 1;
124 	params_pkg.Package.Elements = &params_buf;
125 	params_buf.Type = ACPI_TYPE_BUFFER;
126 	params_buf.Buffer.Length = sizeof(params);
127 	params_buf.Buffer.Pointer = (UINT8 *)params;
128 	while (length > 0) {
129 		to_read = MIN(length, nv->max_label_xfer);
130 		params[0] = offset;
131 		params[1] = to_read;
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, &params_pkg, &result);
137 		if (ACPI_FAILURE(status) ||
138 		    result.Length < sizeof(ACPI_OBJECT) ||
139 		    result.Pointer == NULL) {
140 			error = ENXIO;
141 			break;
142 		}
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) {
148 			error = ENXIO;
149 			break;
150 		}
151 		bcopy(result_buf->Buffer.Pointer + 4, dest, to_read);
152 		dest += to_read;
153 		offset += to_read;
154 		length -= to_read;
155 		if (result.Pointer != NULL) {
156 			AcpiOsFree(result.Pointer);
157 			result.Pointer = NULL;
158 		}
159 	}
160 	if (result.Pointer != NULL)
161 		AcpiOsFree(result.Pointer);
162 	return (error);
163 }
164 
165 static uint64_t
166 fletcher64(const void *data, size_t length)
167 {
168 	size_t i;
169 	uint32_t a, b;
170 	const uint32_t *d;
171 
172 	a = 0;
173 	b = 0;
174 	d = (const uint32_t *)data;
175 	length = length / sizeof(uint32_t);
176 	for (i = 0; i < length; i++) {
177 		a += d[i];
178 		b += a;
179 	}
180 	return ((uint64_t)b << 32 | a);
181 }
182 
183 static bool
184 label_index_is_valid(struct nvdimm_label_index *index, uint32_t max_labels,
185     size_t size, size_t offset)
186 {
187 	uint64_t checksum;
188 
189 	index = (struct nvdimm_label_index *)((uint8_t *)index + size * offset);
190 	if (strcmp(index->signature, NVDIMM_INDEX_BLOCK_SIGNATURE) != 0)
191 		return false;
192 	checksum = index->checksum;
193 	index->checksum = 0;
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)
199 		return false;
200 	return true;
201 }
202 
203 static int
204 read_label(struct nvdimm_dev *nv, int num)
205 {
206 	struct nvdimm_label_entry *entry, *i, *next;
207 	uint64_t checksum;
208 	off_t offset;
209 	int error;
210 
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));
216 	if (error != 0) {
217 		free(entry, M_NVDIMM);
218 		return (error);
219 	}
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);
225 		return (ENXIO);
226 	}
227 
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);
233 		return (0);
234 	}
235 	SLIST_FOREACH_SAFE(i, &nv->labels, link, next) {
236 		if (next == NULL ||
237 		    entry->label.dimm_phys_addr <= next->label.dimm_phys_addr) {
238 			SLIST_INSERT_AFTER(i, entry, link);
239 			return (0);
240 		}
241 	}
242 	__assert_unreachable();
243 }
244 
245 static int
246 read_labels(struct nvdimm_dev *nv)
247 {
248 	struct nvdimm_label_index *indices, *index1;
249 	size_t index_size, num_labels;
250 	int error, n;
251 	bool index_0_valid, index_1_valid;
252 
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) >=
257 		    nv->label_area_size)
258 			break;
259 	}
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);
265 	if (error != 0) {
266 		free(indices, M_NVDIMM);
267 		return (error);
268 	}
269 	index_0_valid = label_index_is_valid(indices, num_labels, index_size,
270 	    0);
271 	index_1_valid = label_index_is_valid(indices, num_labels, index_size,
272 	    1);
273 	if (!index_0_valid && !index_1_valid) {
274 		free(indices, M_NVDIMM);
275 		return (ENXIO);
276 	}
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;
281 		} else {
282 			/*
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
287 			 */
288 			index_0_valid = false;
289 		}
290 	}
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);
296 	while (n >= 0) {
297 		read_label(nv, n);
298 		bit_ffc_at((bitstr_t *)nv->label_index->free, n + 1,
299 		    nv->label_index->slot_cnt, &n);
300 	}
301 	return (0);
302 }
303 
304 struct nvdimm_dev *
305 nvdimm_find_by_handle(nfit_handle_t nv_handle)
306 {
307 	struct nvdimm_dev *res;
308 	device_t *dimms;
309 	int i, error, num_dimms;
310 
311 	res = NULL;
312 	error = devclass_get_devices(nvdimm_devclass, &dimms, &num_dimms);
313 	if (error != 0)
314 		return (NULL);
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]);
318 			break;
319 		}
320 	}
321 	free(dimms, M_TEMP);
322 	return (res);
323 }
324 
325 static int
326 nvdimm_probe(device_t dev)
327 {
328 
329 	return (BUS_PROBE_NOWILDCARD);
330 }
331 
332 static int
333 nvdimm_attach(device_t dev)
334 {
335 	struct nvdimm_dev *nv;
336 	struct sysctl_ctx_list *ctx;
337 	struct sysctl_oid *oid;
338 	struct sysctl_oid_list *children;
339 	struct sbuf *sb;
340 	ACPI_TABLE_NFIT *nfitbl;
341 	ACPI_STATUS status;
342 	ACPI_NFIT_MEMORY_MAP **maps;
343 	int error, i, num_maps;
344 	uint16_t flags;
345 
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);
351 	nv->nv_dev = dev;
352 	nv->nv_handle = nvdimm_root_get_device_handle(dev);
353 
354 	status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
355 	if (ACPI_FAILURE(status)) {
356 		if (bootverbose)
357 			device_printf(dev, "cannot get NFIT\n");
358 		return (ENXIO);
359 	}
360 	acpi_nfit_get_flush_addrs(nfitbl, nv->nv_handle, &nv->nv_flush_addr,
361 	    &nv->nv_flush_addr_cnt);
362 
363 	/*
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.
367 	 */
368 	acpi_nfit_get_memory_maps_by_dimm(nfitbl, nv->nv_handle, &maps,
369 	    &num_maps);
370 	if (num_maps == 0) {
371 		free(nv->nv_flush_addr, M_NVDIMM);
372 		free(maps, M_NVDIMM);
373 		device_printf(dev, "cannot find memory map\n");
374 		return (ENXIO);
375 	}
376 	flags = 0;
377 	for (i = 0; i < num_maps; i++) {
378 		flags |= maps[i]->Flags;
379 	}
380 	free(maps, M_NVDIMM);
381 
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,
385 	    "\20"
386 	    "\001SAVE_FAILED"
387 	    "\002RESTORE_FAILED"
388 	    "\003FLUSH_FAILED"
389 	    "\004NOT_ARMED"
390 	    "\005HEALTH_OBSERVED"
391 	    "\006HEALTH_ENABLED"
392 	    "\007MAP_FAILED");
393 	error = sbuf_finish(sb);
394 	if (error != 0) {
395 		sbuf_delete(sb);
396 		free(nv->nv_flush_addr, M_NVDIMM);
397 		device_printf(dev, "cannot convert flags to string\n");
398 		return (error);
399 	}
400 	/* strdup(9) is M_WAITOK; no need to check for NULL. */
401 	nv->nv_flags_str = strdup(sbuf_data(sb), M_NVDIMM);
402 	sbuf_delete(sb);
403 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "flags",
404 	    CTLFLAG_RD | CTLFLAG_MPSAFE, nv->nv_flags_str, 0,
405 	    "NVDIMM State Flags");
406 	/*
407 	 * Anything other than HEALTH_ENABLED indicates a fault condition of
408 	 * some kind, so log if that's seen.
409 	 */
410 	if ((flags & ~ACPI_NFIT_MEM_HEALTH_ENABLED) != 0)
411 		device_printf(dev, "flags: %s\n", nv->nv_flags_str);
412 
413 	AcpiPutTable(&nfitbl->Header);
414 	error = read_label_area_size(nv);
415 	if (error == 0) {
416 		/*
417 		 * Ignoring errors reading labels. Not all NVDIMMs
418 		 * support labels and namespaces.
419 		 */
420 		read_labels(nv);
421 	}
422 	return (0);
423 }
424 
425 static int
426 nvdimm_detach(device_t dev)
427 {
428 	struct nvdimm_dev *nv;
429 	struct nvdimm_label_entry *label, *next;
430 
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);
438 	}
439 	return (0);
440 }
441 
442 static int
443 nvdimm_suspend(device_t dev)
444 {
445 
446 	return (0);
447 }
448 
449 static int
450 nvdimm_resume(device_t dev)
451 {
452 
453 	return (0);
454 }
455 
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),
462 	DEVMETHOD_END
463 };
464 
465 static driver_t	nvdimm_driver = {
466 	"nvdimm",
467 	nvdimm_methods,
468 	sizeof(struct nvdimm_dev),
469 };
470 
471 DRIVER_MODULE(nvdimm, nvdimm_acpi_root, nvdimm_driver, nvdimm_devclass, NULL,
472     NULL);
473 MODULE_DEPEND(nvdimm, acpi, 1, 1, 1);
474