]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/nvdimm/nvdimm.c
zfs: merge openzfs/zfs@afa7b3484 (master) into main
[FreeBSD/FreeBSD.git] / sys / dev / nvdimm / nvdimm.c
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 bitfield_size, 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         bitfield_size = roundup2(num_labels, 8) / 8;
263         indices = malloc(2 * index_size, M_NVDIMM, M_WAITOK);
264         index1 = (void *)((uint8_t *)indices + index_size);
265         error = read_label_area(nv, (void *)indices, 0, 2 * index_size);
266         if (error != 0) {
267                 free(indices, M_NVDIMM);
268                 return (error);
269         }
270         index_0_valid = label_index_is_valid(indices, num_labels, index_size,
271             0);
272         index_1_valid = label_index_is_valid(indices, num_labels, index_size,
273             1);
274         if (!index_0_valid && !index_1_valid) {
275                 free(indices, M_NVDIMM);
276                 return (ENXIO);
277         }
278         if (index_0_valid && index_1_valid) {
279                 if (((int)indices->seq - (int)index1->seq + 3) % 3 == 1) {
280                         /* index 0 was more recently updated */
281                         index_1_valid = false;
282                 } else {
283                         /*
284                          * either index 1 was more recently updated,
285                          * or the sequence numbers are equal, in which
286                          * case the specification says the block with
287                          * the higher offset is to be treated as valid
288                          */
289                         index_0_valid = false;
290                 }
291         }
292         nv->label_index = malloc(index_size, M_NVDIMM, M_WAITOK);
293         bcopy(index_0_valid ? indices : index1, nv->label_index, index_size);
294         free(indices, M_NVDIMM);
295         bit_ffc_at((bitstr_t *)nv->label_index->free, 0,
296             nv->label_index->slot_cnt, &n);
297         while (n >= 0) {
298                 read_label(nv, n);
299                 bit_ffc_at((bitstr_t *)nv->label_index->free, n + 1,
300                     nv->label_index->slot_cnt, &n);
301         }
302         return (0);
303 }
304
305 struct nvdimm_dev *
306 nvdimm_find_by_handle(nfit_handle_t nv_handle)
307 {
308         struct nvdimm_dev *res;
309         device_t *dimms;
310         int i, error, num_dimms;
311
312         res = NULL;
313         error = devclass_get_devices(nvdimm_devclass, &dimms, &num_dimms);
314         if (error != 0)
315                 return (NULL);
316         for (i = 0; i < num_dimms; i++) {
317                 if (nvdimm_root_get_device_handle(dimms[i]) == nv_handle) {
318                         res = device_get_softc(dimms[i]);
319                         break;
320                 }
321         }
322         free(dimms, M_TEMP);
323         return (res);
324 }
325
326 static int
327 nvdimm_probe(device_t dev)
328 {
329
330         return (BUS_PROBE_NOWILDCARD);
331 }
332
333 static int
334 nvdimm_attach(device_t dev)
335 {
336         struct nvdimm_dev *nv;
337         struct sysctl_ctx_list *ctx;
338         struct sysctl_oid *oid;
339         struct sysctl_oid_list *children;
340         struct sbuf *sb;
341         ACPI_TABLE_NFIT *nfitbl;
342         ACPI_HANDLE handle;
343         ACPI_STATUS status;
344         ACPI_NFIT_MEMORY_MAP **maps;
345         int error, i, num_maps;
346         uint16_t flags;
347
348         nv = device_get_softc(dev);
349         ctx = device_get_sysctl_ctx(dev);
350         oid = device_get_sysctl_tree(dev);
351         children = SYSCTL_CHILDREN(oid);
352         handle = nvdimm_root_get_acpi_handle(dev);
353         MPASS(handle != NULL);
354         nv->nv_dev = dev;
355         nv->nv_handle = nvdimm_root_get_device_handle(dev);
356
357         status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
358         if (ACPI_FAILURE(status)) {
359                 if (bootverbose)
360                         device_printf(dev, "cannot get NFIT\n");
361                 return (ENXIO);
362         }
363         acpi_nfit_get_flush_addrs(nfitbl, nv->nv_handle, &nv->nv_flush_addr,
364             &nv->nv_flush_addr_cnt);
365
366         /*
367          * Each NVDIMM should have at least one memory map associated with it.
368          * If any of the maps have one of the error flags set, reflect that in
369          * the overall status.
370          */
371         acpi_nfit_get_memory_maps_by_dimm(nfitbl, nv->nv_handle, &maps,
372             &num_maps);
373         if (num_maps == 0) {
374                 free(nv->nv_flush_addr, M_NVDIMM);
375                 free(maps, M_NVDIMM);
376                 device_printf(dev, "cannot find memory map\n");
377                 return (ENXIO);
378         }
379         flags = 0;
380         for (i = 0; i < num_maps; i++) {
381                 flags |= maps[i]->Flags;
382         }
383         free(maps, M_NVDIMM);
384
385         /* sbuf_new_auto(9) is M_WAITOK; no need to check for NULL. */
386         sb = sbuf_new_auto();
387         (void) sbuf_printf(sb, "0x%b", flags,
388             "\20"
389             "\001SAVE_FAILED"
390             "\002RESTORE_FAILED"
391             "\003FLUSH_FAILED"
392             "\004NOT_ARMED"
393             "\005HEALTH_OBSERVED"
394             "\006HEALTH_ENABLED"
395             "\007MAP_FAILED");
396         error = sbuf_finish(sb);
397         if (error != 0) {
398                 sbuf_delete(sb);
399                 free(nv->nv_flush_addr, M_NVDIMM);
400                 device_printf(dev, "cannot convert flags to string\n");
401                 return (error);
402         }
403         /* strdup(9) is M_WAITOK; no need to check for NULL. */
404         nv->nv_flags_str = strdup(sbuf_data(sb), M_NVDIMM);
405         sbuf_delete(sb);
406         SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "flags",
407             CTLFLAG_RD | CTLFLAG_MPSAFE, nv->nv_flags_str, 0,
408             "NVDIMM State Flags");
409         /*
410          * Anything other than HEALTH_ENABLED indicates a fault condition of
411          * some kind, so log if that's seen.
412          */
413         if ((flags & ~ACPI_NFIT_MEM_HEALTH_ENABLED) != 0)
414                 device_printf(dev, "flags: %s\n", nv->nv_flags_str);
415
416         AcpiPutTable(&nfitbl->Header);
417         error = read_label_area_size(nv);
418         if (error == 0) {
419                 /*
420                  * Ignoring errors reading labels. Not all NVDIMMs
421                  * support labels and namespaces.
422                  */
423                 read_labels(nv);
424         }
425         return (0);
426 }
427
428 static int
429 nvdimm_detach(device_t dev)
430 {
431         struct nvdimm_dev *nv;
432         struct nvdimm_label_entry *label, *next;
433
434         nv = device_get_softc(dev);
435         free(nv->nv_flags_str, M_NVDIMM);
436         free(nv->nv_flush_addr, M_NVDIMM);
437         free(nv->label_index, M_NVDIMM);
438         SLIST_FOREACH_SAFE(label, &nv->labels, link, next) {
439                 SLIST_REMOVE_HEAD(&nv->labels, link);
440                 free(label, M_NVDIMM);
441         }
442         return (0);
443 }
444
445 static int
446 nvdimm_suspend(device_t dev)
447 {
448
449         return (0);
450 }
451
452 static int
453 nvdimm_resume(device_t dev)
454 {
455
456         return (0);
457 }
458
459 static device_method_t nvdimm_methods[] = {
460         DEVMETHOD(device_probe, nvdimm_probe),
461         DEVMETHOD(device_attach, nvdimm_attach),
462         DEVMETHOD(device_detach, nvdimm_detach),
463         DEVMETHOD(device_suspend, nvdimm_suspend),
464         DEVMETHOD(device_resume, nvdimm_resume),
465         DEVMETHOD_END
466 };
467
468 static driver_t nvdimm_driver = {
469         "nvdimm",
470         nvdimm_methods,
471         sizeof(struct nvdimm_dev),
472 };
473
474 DRIVER_MODULE(nvdimm, nvdimm_acpi_root, nvdimm_driver, nvdimm_devclass, NULL,
475     NULL);
476 MODULE_DEPEND(nvdimm, acpi, 1, 1, 1);