xref: /freebsd/sys/dev/nvdimm/nvdimm.c (revision 7877fdebeeb35fad1cbbafce22598b1bdf97c786)
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/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 static devclass_t nvdimm_devclass;
65 MALLOC_DEFINE(M_NVDIMM, "nvdimm", "NVDIMM driver memory");
66 
67 static int
68 read_label_area_size(struct nvdimm_dev *nv)
69 {
70 	ACPI_OBJECT *result_buffer;
71 	ACPI_HANDLE handle;
72 	ACPI_STATUS status;
73 	ACPI_BUFFER result;
74 	uint32_t *out;
75 	int error;
76 
77 	handle = nvdimm_root_get_acpi_handle(nv->nv_dev);
78 	if (handle == NULL)
79 		return (ENODEV);
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,
84 	    &result);
85 	error = ENXIO;
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];
94 			error = 0;
95 		}
96 	}
97 	if (result.Pointer != NULL)
98 		AcpiOsFree(result.Pointer);
99 	return (error);
100 }
101 
102 static int
103 read_label_area(struct nvdimm_dev *nv, uint8_t *dest, off_t offset,
104     off_t length)
105 {
106 	ACPI_BUFFER result;
107 	ACPI_HANDLE handle;
108 	ACPI_OBJECT params_pkg, params_buf, *result_buf;
109 	ACPI_STATUS status;
110 	uint32_t params[2];
111 	off_t to_read;
112 	int error;
113 
114 	error = 0;
115 	handle = nvdimm_root_get_acpi_handle(nv->nv_dev);
116 	if (offset < 0 || length <= 0 ||
117 	    offset + length > nv->label_area_size ||
118 	    handle == NULL)
119 		return (ENODEV);
120 	params_pkg.Type = ACPI_TYPE_PACKAGE;
121 	params_pkg.Package.Count = 1;
122 	params_pkg.Package.Elements = &params_buf;
123 	params_buf.Type = ACPI_TYPE_BUFFER;
124 	params_buf.Buffer.Length = sizeof(params);
125 	params_buf.Buffer.Pointer = (UINT8 *)params;
126 	while (length > 0) {
127 		to_read = MIN(length, nv->max_label_xfer);
128 		params[0] = offset;
129 		params[1] = to_read;
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, &params_pkg, &result);
135 		if (ACPI_FAILURE(status) ||
136 		    result.Length < sizeof(ACPI_OBJECT) ||
137 		    result.Pointer == NULL) {
138 			error = ENXIO;
139 			break;
140 		}
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) {
146 			error = ENXIO;
147 			break;
148 		}
149 		bcopy(result_buf->Buffer.Pointer + 4, dest, to_read);
150 		dest += to_read;
151 		offset += to_read;
152 		length -= to_read;
153 		if (result.Pointer != NULL) {
154 			AcpiOsFree(result.Pointer);
155 			result.Pointer = NULL;
156 		}
157 	}
158 	if (result.Pointer != NULL)
159 		AcpiOsFree(result.Pointer);
160 	return (error);
161 }
162 
163 static uint64_t
164 fletcher64(const void *data, size_t length)
165 {
166 	size_t i;
167 	uint32_t a, b;
168 	const uint32_t *d;
169 
170 	a = 0;
171 	b = 0;
172 	d = (const uint32_t *)data;
173 	length = length / sizeof(uint32_t);
174 	for (i = 0; i < length; i++) {
175 		a += d[i];
176 		b += a;
177 	}
178 	return ((uint64_t)b << 32 | a);
179 }
180 
181 static bool
182 label_index_is_valid(struct nvdimm_label_index *index, uint32_t max_labels,
183     size_t size, size_t offset)
184 {
185 	uint64_t checksum;
186 
187 	index = (struct nvdimm_label_index *)((uint8_t *)index + size * offset);
188 	if (strcmp(index->signature, NVDIMM_INDEX_BLOCK_SIGNATURE) != 0)
189 		return false;
190 	checksum = index->checksum;
191 	index->checksum = 0;
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)
197 		return false;
198 	return true;
199 }
200 
201 static int
202 read_label(struct nvdimm_dev *nv, int num)
203 {
204 	struct nvdimm_label_entry *entry, *i, *next;
205 	uint64_t checksum;
206 	off_t offset;
207 	int error;
208 
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));
214 	if (error != 0) {
215 		free(entry, M_NVDIMM);
216 		return (error);
217 	}
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);
223 		return (ENXIO);
224 	}
225 
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);
231 		return (0);
232 	}
233 	SLIST_FOREACH_SAFE(i, &nv->labels, link, next) {
234 		if (next == NULL ||
235 		    entry->label.dimm_phys_addr <= next->label.dimm_phys_addr) {
236 			SLIST_INSERT_AFTER(i, entry, link);
237 			return (0);
238 		}
239 	}
240 	__assert_unreachable();
241 }
242 
243 static int
244 read_labels(struct nvdimm_dev *nv)
245 {
246 	struct nvdimm_label_index *indices, *index1;
247 	size_t bitfield_size, index_size, num_labels;
248 	int error, n;
249 	bool index_0_valid, index_1_valid;
250 
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) >=
255 		    nv->label_area_size)
256 			break;
257 	}
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);
264 	if (error != 0) {
265 		free(indices, M_NVDIMM);
266 		return (error);
267 	}
268 	index_0_valid = label_index_is_valid(indices, num_labels, index_size,
269 	    0);
270 	index_1_valid = label_index_is_valid(indices, num_labels, index_size,
271 	    1);
272 	if (!index_0_valid && !index_1_valid) {
273 		free(indices, M_NVDIMM);
274 		return (ENXIO);
275 	}
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;
280 		} else {
281 			/*
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
286 			 */
287 			index_0_valid = false;
288 		}
289 	}
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);
295 	while (n >= 0) {
296 		read_label(nv, n);
297 		bit_ffc_at((bitstr_t *)nv->label_index->free, n + 1,
298 		    nv->label_index->slot_cnt, &n);
299 	}
300 	return (0);
301 }
302 
303 struct nvdimm_dev *
304 nvdimm_find_by_handle(nfit_handle_t nv_handle)
305 {
306 	struct nvdimm_dev *res;
307 	device_t *dimms;
308 	int i, error, num_dimms;
309 
310 	res = NULL;
311 	error = devclass_get_devices(nvdimm_devclass, &dimms, &num_dimms);
312 	if (error != 0)
313 		return (NULL);
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]);
317 			break;
318 		}
319 	}
320 	free(dimms, M_TEMP);
321 	return (res);
322 }
323 
324 static int
325 nvdimm_probe(device_t dev)
326 {
327 
328 	return (BUS_PROBE_NOWILDCARD);
329 }
330 
331 static int
332 nvdimm_attach(device_t dev)
333 {
334 	struct nvdimm_dev *nv;
335 	ACPI_TABLE_NFIT *nfitbl;
336 	ACPI_HANDLE handle;
337 	ACPI_STATUS status;
338 	int error;
339 
340 	nv = device_get_softc(dev);
341 	handle = nvdimm_root_get_acpi_handle(dev);
342 	MPASS(handle != NULL);
343 	nv->nv_dev = dev;
344 	nv->nv_handle = nvdimm_root_get_device_handle(dev);
345 
346 	status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
347 	if (ACPI_FAILURE(status)) {
348 		if (bootverbose)
349 			device_printf(dev, "cannot get NFIT\n");
350 		return (ENXIO);
351 	}
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);
356 	if (error == 0) {
357 		/*
358 		 * Ignoring errors reading labels. Not all NVDIMMs
359 		 * support labels and namespaces.
360 		 */
361 		read_labels(nv);
362 	}
363 	return (0);
364 }
365 
366 static int
367 nvdimm_detach(device_t dev)
368 {
369 	struct nvdimm_dev *nv;
370 	struct nvdimm_label_entry *label, *next;
371 
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);
378 	}
379 	return (0);
380 }
381 
382 static int
383 nvdimm_suspend(device_t dev)
384 {
385 
386 	return (0);
387 }
388 
389 static int
390 nvdimm_resume(device_t dev)
391 {
392 
393 	return (0);
394 }
395 
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),
402 	DEVMETHOD_END
403 };
404 
405 static driver_t	nvdimm_driver = {
406 	"nvdimm",
407 	nvdimm_methods,
408 	sizeof(struct nvdimm_dev),
409 };
410 
411 DRIVER_MODULE(nvdimm, nvdimm_acpi_root, nvdimm_driver, nvdimm_devclass, NULL,
412     NULL);
413 MODULE_DEPEND(nvdimm, acpi, 1, 1, 1);
414