xref: /freebsd/sys/dev/nvdimm/nvdimm.c (revision f976241773df2260e6170317080761d1c5814fe5)
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/bio.h>
39 #include <sys/bitstring.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/uuid.h>
46 
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acuuid.h>
50 #include <dev/acpica/acpivar.h>
51 
52 #include <dev/nvdimm/nvdimm_var.h>
53 
54 #define _COMPONENT	ACPI_OEM
55 ACPI_MODULE_NAME("NVDIMM")
56 
57 static struct uuid intel_nvdimm_dsm_uuid =
58     {0x4309AC30,0x0D11,0x11E4,0x91,0x91,{0x08,0x00,0x20,0x0C,0x9A,0x66}};
59 #define INTEL_NVDIMM_DSM_REV 1
60 #define INTEL_NVDIMM_DSM_GET_LABEL_SIZE 4
61 #define INTEL_NVDIMM_DSM_GET_LABEL_DATA 5
62 
63 static devclass_t nvdimm_devclass;
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 	__unreachable();
240 }
241 
242 static int
243 read_labels(struct nvdimm_dev *nv)
244 {
245 	struct nvdimm_label_index *indices, *index1;
246 	size_t bitfield_size, 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 	bitfield_size = roundup2(num_labels, 8) / 8;
260 	indices = malloc(2 * index_size, M_NVDIMM, M_WAITOK);
261 	index1 = (void *)((uint8_t *)indices + index_size);
262 	error = read_label_area(nv, (void *)indices, 0, 2 * index_size);
263 	if (error != 0) {
264 		free(indices, M_NVDIMM);
265 		return (error);
266 	}
267 	index_0_valid = label_index_is_valid(indices, num_labels, index_size,
268 	    0);
269 	index_1_valid = label_index_is_valid(indices, num_labels, index_size,
270 	    1);
271 	if (!index_0_valid && !index_1_valid) {
272 		free(indices, M_NVDIMM);
273 		return (ENXIO);
274 	}
275 	if (index_0_valid && index_1_valid) {
276 		if (((int)indices->seq - (int)index1->seq + 3) % 3 == 1) {
277 			/* index 0 was more recently updated */
278 			index_1_valid = false;
279 		} else {
280 			/*
281 			 * either index 1 was more recently updated,
282 			 * or the sequence numbers are equal, in which
283 			 * case the specification says the block with
284 			 * the higher offset is to be treated as valid
285 			 */
286 			index_0_valid = false;
287 		}
288 	}
289 	nv->label_index = malloc(index_size, M_NVDIMM, M_WAITOK);
290 	bcopy(index_0_valid ? indices : index1, nv->label_index, index_size);
291 	free(indices, M_NVDIMM);
292 	bit_ffc_at((bitstr_t *)nv->label_index->free, 0,
293 	    nv->label_index->slot_cnt, &n);
294 	while (n >= 0) {
295 		read_label(nv, n);
296 		bit_ffc_at((bitstr_t *)nv->label_index->free, n + 1,
297 		    nv->label_index->slot_cnt, &n);
298 	}
299 	return (0);
300 }
301 
302 struct nvdimm_dev *
303 nvdimm_find_by_handle(nfit_handle_t nv_handle)
304 {
305 	struct nvdimm_dev *res;
306 	device_t *dimms;
307 	int i, error, num_dimms;
308 
309 	res = NULL;
310 	error = devclass_get_devices(nvdimm_devclass, &dimms, &num_dimms);
311 	if (error != 0)
312 		return (NULL);
313 	for (i = 0; i < num_dimms; i++) {
314 		if (nvdimm_root_get_device_handle(dimms[i]) == nv_handle) {
315 			res = device_get_softc(dimms[i]);
316 			break;
317 		}
318 	}
319 	free(dimms, M_TEMP);
320 	return (res);
321 }
322 
323 static int
324 nvdimm_probe(device_t dev)
325 {
326 
327 	return (BUS_PROBE_NOWILDCARD);
328 }
329 
330 static int
331 nvdimm_attach(device_t dev)
332 {
333 	struct nvdimm_dev *nv;
334 	ACPI_TABLE_NFIT *nfitbl;
335 	ACPI_HANDLE handle;
336 	ACPI_STATUS status;
337 	int error;
338 
339 	nv = device_get_softc(dev);
340 	handle = nvdimm_root_get_acpi_handle(dev);
341 	MPASS(handle != NULL);
342 	nv->nv_dev = dev;
343 	nv->nv_handle = nvdimm_root_get_device_handle(dev);
344 
345 	status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
346 	if (ACPI_FAILURE(status)) {
347 		if (bootverbose)
348 			device_printf(dev, "cannot get NFIT\n");
349 		return (ENXIO);
350 	}
351 	acpi_nfit_get_flush_addrs(nfitbl, nv->nv_handle, &nv->nv_flush_addr,
352 	    &nv->nv_flush_addr_cnt);
353 	AcpiPutTable(&nfitbl->Header);
354 	error = read_label_area_size(nv);
355 	if (error == 0) {
356 		/*
357 		 * Ignoring errors reading labels. Not all NVDIMMs
358 		 * support labels and namespaces.
359 		 */
360 		read_labels(nv);
361 	}
362 	return (0);
363 }
364 
365 static int
366 nvdimm_detach(device_t dev)
367 {
368 	struct nvdimm_dev *nv;
369 	struct nvdimm_label_entry *label, *next;
370 
371 	nv = device_get_softc(dev);
372 	free(nv->nv_flush_addr, M_NVDIMM);
373 	free(nv->label_index, M_NVDIMM);
374 	SLIST_FOREACH_SAFE(label, &nv->labels, link, next) {
375 		SLIST_REMOVE_HEAD(&nv->labels, link);
376 		free(label, M_NVDIMM);
377 	}
378 	return (0);
379 }
380 
381 static int
382 nvdimm_suspend(device_t dev)
383 {
384 
385 	return (0);
386 }
387 
388 static int
389 nvdimm_resume(device_t dev)
390 {
391 
392 	return (0);
393 }
394 
395 static device_method_t nvdimm_methods[] = {
396 	DEVMETHOD(device_probe, nvdimm_probe),
397 	DEVMETHOD(device_attach, nvdimm_attach),
398 	DEVMETHOD(device_detach, nvdimm_detach),
399 	DEVMETHOD(device_suspend, nvdimm_suspend),
400 	DEVMETHOD(device_resume, nvdimm_resume),
401 	DEVMETHOD_END
402 };
403 
404 static driver_t	nvdimm_driver = {
405 	"nvdimm",
406 	nvdimm_methods,
407 	sizeof(struct nvdimm_dev),
408 };
409 
410 DRIVER_MODULE(nvdimm, nvdimm_acpi_root, nvdimm_driver, nvdimm_devclass, NULL,
411     NULL);
412 MODULE_DEPEND(nvdimm, acpi, 1, 1, 1);
413