]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/nvdimm/nvdimm_spa.c
MFV r341618:
[FreeBSD/FreeBSD.git] / sys / dev / nvdimm / nvdimm_spa.c
1 /*-
2  * Copyright (c) 2017, 2018 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
6  * under sponsorship from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_acpi.h"
34 #include "opt_ddb.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bio.h>
39 #include <sys/bus.h>
40 #include <sys/conf.h>
41 #include <sys/devicestat.h>
42 #include <sys/disk.h>
43 #include <sys/efi.h>
44 #include <sys/kernel.h>
45 #include <sys/kthread.h>
46 #include <sys/limits.h>
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/rwlock.h>
51 #include <sys/sglist.h>
52 #include <sys/uio.h>
53 #include <sys/uuid.h>
54 #include <geom/geom.h>
55 #include <geom/geom_int.h>
56 #include <machine/vmparam.h>
57 #include <vm/vm.h>
58 #include <vm/vm_object.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_pager.h>
61 #include <contrib/dev/acpica/include/acpi.h>
62 #include <contrib/dev/acpica/include/accommon.h>
63 #include <contrib/dev/acpica/include/acuuid.h>
64 #include <dev/acpica/acpivar.h>
65 #include <dev/nvdimm/nvdimm_var.h>
66
67 struct SPA_mapping *spa_mappings;
68 int spa_mappings_cnt;
69
70 static int
71 nvdimm_spa_count(void *nfitsubtbl __unused, void *arg)
72 {
73         int *cnt;
74
75         cnt = arg;
76         (*cnt)++;
77         return (0);
78 }
79
80 static struct nvdimm_SPA_uuid_list_elm {
81         const char              *u_name;
82         const char              *u_id_str;
83         struct uuid             u_id;
84         const bool              u_usr_acc;
85 } nvdimm_SPA_uuid_list[] = {
86         [SPA_TYPE_VOLATILE_MEMORY] = {
87                 .u_name =       "VOLA MEM ",
88                 .u_id_str =     UUID_VOLATILE_MEMORY,
89                 .u_usr_acc =    true,
90         },
91         [SPA_TYPE_PERSISTENT_MEMORY] = {
92                 .u_name =       "PERS MEM",
93                 .u_id_str =     UUID_PERSISTENT_MEMORY,
94                 .u_usr_acc =    true,
95         },
96         [SPA_TYPE_CONTROL_REGION] = {
97                 .u_name =       "CTRL RG ",
98                 .u_id_str =     UUID_CONTROL_REGION,
99                 .u_usr_acc =    false,
100         },
101         [SPA_TYPE_DATA_REGION] = {
102                 .u_name =       "DATA RG ",
103                 .u_id_str =     UUID_DATA_REGION,
104                 .u_usr_acc =    true,
105         },
106         [SPA_TYPE_VOLATILE_VIRTUAL_DISK] = {
107                 .u_name =       "VIRT DSK",
108                 .u_id_str =     UUID_VOLATILE_VIRTUAL_DISK,
109                 .u_usr_acc =    true,
110         },
111         [SPA_TYPE_VOLATILE_VIRTUAL_CD] = {
112                 .u_name =       "VIRT CD ",
113                 .u_id_str =     UUID_VOLATILE_VIRTUAL_CD,
114                 .u_usr_acc =    true,
115         },
116         [SPA_TYPE_PERSISTENT_VIRTUAL_DISK] = {
117                 .u_name =       "PV DSK  ",
118                 .u_id_str =     UUID_PERSISTENT_VIRTUAL_DISK,
119                 .u_usr_acc =    true,
120         },
121         [SPA_TYPE_PERSISTENT_VIRTUAL_CD] = {
122                 .u_name =       "PV CD   ",
123                 .u_id_str =     UUID_PERSISTENT_VIRTUAL_CD,
124                 .u_usr_acc =    true,
125         },
126 };
127
128 static vm_memattr_t
129 nvdimm_spa_memattr(struct SPA_mapping *spa)
130 {
131         vm_memattr_t mode;
132
133         if ((spa->spa_efi_mem_flags & EFI_MD_ATTR_WB) != 0)
134                 mode = VM_MEMATTR_WRITE_BACK;
135         else if ((spa->spa_efi_mem_flags & EFI_MD_ATTR_WT) != 0)
136                 mode = VM_MEMATTR_WRITE_THROUGH;
137         else if ((spa->spa_efi_mem_flags & EFI_MD_ATTR_WC) != 0)
138                 mode = VM_MEMATTR_WRITE_COMBINING;
139         else if ((spa->spa_efi_mem_flags & EFI_MD_ATTR_WP) != 0)
140                 mode = VM_MEMATTR_WRITE_PROTECTED;
141         else if ((spa->spa_efi_mem_flags & EFI_MD_ATTR_UC) != 0)
142                 mode = VM_MEMATTR_UNCACHEABLE;
143         else {
144                 if (bootverbose)
145                         printf("SPA%d mapping attr unsupported\n",
146                             spa->spa_nfit_idx);
147                 mode = VM_MEMATTR_UNCACHEABLE;
148         }
149         return (mode);
150 }
151
152 static int
153 nvdimm_spa_uio(struct SPA_mapping *spa, struct uio *uio)
154 {
155         struct vm_page m, *ma;
156         off_t off;
157         vm_memattr_t mattr;
158         int error, n;
159
160         error = 0;
161         if (spa->spa_kva == NULL) {
162                 mattr = nvdimm_spa_memattr(spa);
163                 vm_page_initfake(&m, 0, mattr);
164                 ma = &m;
165                 while (uio->uio_resid > 0) {
166                         if (uio->uio_offset >= spa->spa_len)
167                                 break;
168                         off = spa->spa_phys_base + uio->uio_offset;
169                         vm_page_updatefake(&m, trunc_page(off), mattr);
170                         n = PAGE_SIZE;
171                         if (n > uio->uio_resid)
172                                 n = uio->uio_resid;
173                         error = uiomove_fromphys(&ma, off & PAGE_MASK, n, uio);
174                         if (error != 0)
175                                 break;
176                 }
177         } else {
178                 while (uio->uio_resid > 0) {
179                         if (uio->uio_offset >= spa->spa_len)
180                                 break;
181                         n = INT_MAX;
182                         if (n > uio->uio_resid)
183                                 n = uio->uio_resid;
184                         if (uio->uio_offset + n > spa->spa_len)
185                                 n = spa->spa_len - uio->uio_offset;
186                         error = uiomove((char *)spa->spa_kva + uio->uio_offset,
187                             n, uio);
188                         if (error != 0)
189                                 break;
190                 }
191         }
192         return (error);
193 }
194
195 static int
196 nvdimm_spa_rw(struct cdev *dev, struct uio *uio, int ioflag)
197 {
198
199         return (nvdimm_spa_uio(dev->si_drv1, uio));
200 }
201
202 static int
203 nvdimm_spa_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
204     struct thread *td)
205 {
206         struct SPA_mapping *spa;
207         int error;
208
209         spa = dev->si_drv1;
210         error = 0;
211         switch (cmd) {
212         case DIOCGSECTORSIZE:
213                 *(u_int *)data = DEV_BSIZE;
214                 break;
215         case DIOCGMEDIASIZE:
216                 *(off_t *)data = spa->spa_len;
217                 break;
218         default:
219                 error = ENOTTY;
220                 break;
221         }
222         return (error);
223 }
224
225 static int
226 nvdimm_spa_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size,
227     vm_object_t *objp, int nprot)
228 {
229         struct SPA_mapping *spa;
230
231         spa = dev->si_drv1;
232         if (spa->spa_obj == NULL)
233                 return (ENXIO);
234         if (*offset >= spa->spa_len || *offset + size < *offset ||
235             *offset + size > spa->spa_len)
236                 return (EINVAL);
237         vm_object_reference(spa->spa_obj);
238         *objp = spa->spa_obj;
239         return (0);
240 }
241
242 static struct cdevsw spa_cdevsw = {
243         .d_version =    D_VERSION,
244         .d_flags =      D_DISK,
245         .d_name =       "nvdimm_spa",
246         .d_read =       nvdimm_spa_rw,
247         .d_write =      nvdimm_spa_rw,
248         .d_ioctl =      nvdimm_spa_ioctl,
249         .d_mmap_single = nvdimm_spa_mmap_single,
250 };
251
252 static void
253 nvdimm_spa_g_all_unmapped(struct SPA_mapping *spa, struct bio *bp,
254     int rw)
255 {
256         struct vm_page maa[bp->bio_ma_n];
257         vm_page_t ma[bp->bio_ma_n];
258         vm_memattr_t mattr;
259         int i;
260
261         mattr = nvdimm_spa_memattr(spa);
262         for (i = 0; i < nitems(ma); i++) {
263                 maa[i].flags = 0;
264                 vm_page_initfake(&maa[i], spa->spa_phys_base +
265                     trunc_page(bp->bio_offset) + PAGE_SIZE * i, mattr);
266                 ma[i] = &maa[i];
267         }
268         if (rw == BIO_READ)
269                 pmap_copy_pages(ma, bp->bio_offset & PAGE_MASK, bp->bio_ma,
270                     bp->bio_ma_offset, bp->bio_length);
271         else
272                 pmap_copy_pages(bp->bio_ma, bp->bio_ma_offset, ma,
273                     bp->bio_offset & PAGE_MASK, bp->bio_length);
274 }
275
276 static void
277 nvdimm_spa_g_thread(void *arg)
278 {
279         struct SPA_mapping *spa;
280         struct bio *bp;
281         struct uio auio;
282         struct iovec aiovec;
283         int error;
284
285         spa = arg;
286         for (;;) {
287                 mtx_lock(&spa->spa_g_mtx);
288                 for (;;) {
289                         bp = bioq_takefirst(&spa->spa_g_queue);
290                         if (bp != NULL)
291                                 break;
292                         msleep(&spa->spa_g_queue, &spa->spa_g_mtx, PRIBIO,
293                             "spa_g", 0);
294                         if (!spa->spa_g_proc_run) {
295                                 spa->spa_g_proc_exiting = true;
296                                 wakeup(&spa->spa_g_queue);
297                                 mtx_unlock(&spa->spa_g_mtx);
298                                 kproc_exit(0);
299                         }
300                         continue;
301                 }
302                 mtx_unlock(&spa->spa_g_mtx);
303                 if (bp->bio_cmd != BIO_READ && bp->bio_cmd != BIO_WRITE &&
304                     bp->bio_cmd != BIO_FLUSH) {
305                         error = EOPNOTSUPP;
306                         goto completed;
307                 }
308
309                 error = 0;
310                 if (bp->bio_cmd == BIO_FLUSH) {
311                         if (spa->spa_kva != NULL) {
312                                 pmap_large_map_wb(spa->spa_kva, spa->spa_len);
313                         } else {
314                                 pmap_flush_cache_phys_range(
315                                     (vm_paddr_t)spa->spa_phys_base,
316                                     (vm_paddr_t)spa->spa_phys_base +
317                                     spa->spa_len, nvdimm_spa_memattr(spa));
318                         }
319                         /*
320                          * XXX flush IMC
321                          */
322                         goto completed;
323                 }
324                 
325                 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
326                         if (spa->spa_kva != NULL) {
327                                 aiovec.iov_base = (char *)spa->spa_kva +
328                                     bp->bio_offset;
329                                 aiovec.iov_len = bp->bio_length;
330                                 auio.uio_iov = &aiovec;
331                                 auio.uio_iovcnt = 1;
332                                 auio.uio_resid = bp->bio_length;
333                                 auio.uio_offset = bp->bio_offset;
334                                 auio.uio_segflg = UIO_SYSSPACE;
335                                 auio.uio_rw = bp->bio_cmd == BIO_READ ?
336                                     UIO_WRITE : UIO_READ;
337                                 auio.uio_td = curthread;
338                                 error = uiomove_fromphys(bp->bio_ma,
339                                     bp->bio_ma_offset, bp->bio_length, &auio);
340                         } else {
341                                 nvdimm_spa_g_all_unmapped(spa, bp, bp->bio_cmd);
342                                 error = 0;
343                         }
344                 } else {
345                         aiovec.iov_base = bp->bio_data;
346                         aiovec.iov_len = bp->bio_length;
347                         auio.uio_iov = &aiovec;
348                         auio.uio_iovcnt = 1;
349                         auio.uio_resid = bp->bio_length;
350                         auio.uio_offset = bp->bio_offset;
351                         auio.uio_segflg = UIO_SYSSPACE;
352                         auio.uio_rw = bp->bio_cmd == BIO_READ ? UIO_READ :
353                             UIO_WRITE;
354                         auio.uio_td = curthread;
355                         error = nvdimm_spa_uio(spa, &auio);
356                 }
357                 devstat_end_transaction_bio(spa->spa_g_devstat, bp);
358 completed:
359                 bp->bio_completed = bp->bio_length;
360                 g_io_deliver(bp, error);
361         }
362 }
363
364 static void
365 nvdimm_spa_g_start(struct bio *bp)
366 {
367         struct SPA_mapping *spa;
368
369         spa = bp->bio_to->geom->softc;
370         if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
371                 mtx_lock(&spa->spa_g_stat_mtx);
372                 devstat_start_transaction_bio(spa->spa_g_devstat, bp);
373                 mtx_unlock(&spa->spa_g_stat_mtx);
374         }
375         mtx_lock(&spa->spa_g_mtx);
376         bioq_disksort(&spa->spa_g_queue, bp);
377         wakeup(&spa->spa_g_queue);
378         mtx_unlock(&spa->spa_g_mtx);
379 }
380
381 static int
382 nvdimm_spa_g_access(struct g_provider *pp, int r, int w, int e)
383 {
384
385         return (0);
386 }
387
388 static g_init_t nvdimm_spa_g_init;
389 static g_fini_t nvdimm_spa_g_fini;
390
391 struct g_class nvdimm_spa_g_class = {
392         .name =         "SPA",
393         .version =      G_VERSION,
394         .start =        nvdimm_spa_g_start,
395         .access =       nvdimm_spa_g_access,
396         .init =         nvdimm_spa_g_init,
397         .fini =         nvdimm_spa_g_fini,
398 };
399 DECLARE_GEOM_CLASS(nvdimm_spa_g_class, g_spa);
400
401 static int
402 nvdimm_spa_init_one(struct SPA_mapping *spa, ACPI_NFIT_SYSTEM_ADDRESS *nfitaddr,
403     int spa_type)
404 {
405         struct make_dev_args mda;
406         struct sglist *spa_sg;
407         int error, error1;
408
409         spa->spa_type = spa_type;
410         spa->spa_domain = ((nfitaddr->Flags & ACPI_NFIT_PROXIMITY_VALID) != 0) ?
411             nfitaddr->ProximityDomain : -1;
412         spa->spa_nfit_idx = nfitaddr->RangeIndex;
413         spa->spa_phys_base = nfitaddr->Address;
414         spa->spa_len = nfitaddr->Length;
415         spa->spa_efi_mem_flags = nfitaddr->MemoryMapping;
416         if (bootverbose) {
417                 printf("NVDIMM SPA%d base %#016jx len %#016jx %s fl %#jx\n",
418                     spa->spa_nfit_idx,
419                     (uintmax_t)spa->spa_phys_base, (uintmax_t)spa->spa_len,
420                     nvdimm_SPA_uuid_list[spa_type].u_name,
421                     spa->spa_efi_mem_flags);
422         }
423         if (!nvdimm_SPA_uuid_list[spa_type].u_usr_acc)
424                 return (0);
425
426         error1 = pmap_large_map(spa->spa_phys_base, spa->spa_len,
427             &spa->spa_kva, nvdimm_spa_memattr(spa));
428         if (error1 != 0) {
429                 printf("NVDIMM SPA%d cannot map into KVA, error %d\n",
430                     spa->spa_nfit_idx, error1);
431                 spa->spa_kva = NULL;
432         }
433
434         spa_sg = sglist_alloc(1, M_WAITOK);
435         error = sglist_append_phys(spa_sg, spa->spa_phys_base,
436             spa->spa_len);
437         if (error == 0) {
438                 spa->spa_obj = vm_pager_allocate(OBJT_SG, spa_sg, spa->spa_len,
439                     VM_PROT_ALL, 0, NULL);
440                 if (spa->spa_obj == NULL) {
441                         printf("NVDIMM SPA%d failed to alloc vm object",
442                             spa->spa_nfit_idx);
443                         sglist_free(spa_sg);
444                 }
445         } else {
446                 printf("NVDIMM SPA%d failed to init sglist, error %d",
447                     spa->spa_nfit_idx, error);
448                 sglist_free(spa_sg);
449         }
450
451         make_dev_args_init(&mda);
452         mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME;
453         mda.mda_devsw = &spa_cdevsw;
454         mda.mda_cr = NULL;
455         mda.mda_uid = UID_ROOT;
456         mda.mda_gid = GID_OPERATOR;
457         mda.mda_mode = 0660;
458         mda.mda_si_drv1 = spa;
459         error = make_dev_s(&mda, &spa->spa_dev, "nvdimm_spa%d",
460             spa->spa_nfit_idx);
461         if (error != 0) {
462                 printf("NVDIMM SPA%d cannot create devfs node, error %d\n",
463                     spa->spa_nfit_idx, error);
464                 if (error1 == 0)
465                         error1 = error;
466         }
467
468         bioq_init(&spa->spa_g_queue);
469         mtx_init(&spa->spa_g_mtx, "spag", NULL, MTX_DEF);
470         mtx_init(&spa->spa_g_stat_mtx, "spagst", NULL, MTX_DEF);
471         spa->spa_g_proc_run = true;
472         spa->spa_g_proc_exiting = false;
473         error = kproc_create(nvdimm_spa_g_thread, spa, &spa->spa_g_proc, 0, 0,
474             "g_spa%d", spa->spa_nfit_idx);
475         if (error != 0) {
476                 printf("NVDIMM SPA%d cannot create geom worker, error %d\n",
477                     spa->spa_nfit_idx, error);
478                 if (error1 == 0)
479                         error1 = error;
480         } else {
481                 g_topology_assert();
482                 spa->spa_g = g_new_geomf(&nvdimm_spa_g_class, "spa%d",
483                     spa->spa_nfit_idx);
484                 spa->spa_g->softc = spa;
485                 spa->spa_p = g_new_providerf(spa->spa_g, "spa%d",
486                     spa->spa_nfit_idx);
487                 spa->spa_p->mediasize = spa->spa_len;
488                 spa->spa_p->sectorsize = DEV_BSIZE;
489                 spa->spa_p->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE |
490                     G_PF_ACCEPT_UNMAPPED;
491                 g_error_provider(spa->spa_p, 0);
492                 spa->spa_g_devstat = devstat_new_entry("spa", spa->spa_nfit_idx,
493                     DEV_BSIZE, DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT,
494                     DEVSTAT_PRIORITY_MAX);
495         }
496         return (error1);
497 }
498
499 static void
500 nvdimm_spa_fini_one(struct SPA_mapping *spa)
501 {
502
503         mtx_lock(&spa->spa_g_mtx);
504         spa->spa_g_proc_run = false;
505         wakeup(&spa->spa_g_queue);
506         while (!spa->spa_g_proc_exiting)
507                 msleep(&spa->spa_g_queue, &spa->spa_g_mtx, PRIBIO, "spa_e", 0);
508         mtx_unlock(&spa->spa_g_mtx);
509         if (spa->spa_g != NULL) {
510                 g_topology_lock();
511                 g_wither_geom(spa->spa_g, ENXIO);
512                 g_topology_unlock();
513                 spa->spa_g = NULL;
514                 spa->spa_p = NULL;
515         }
516         if (spa->spa_g_devstat != NULL) {
517                 devstat_remove_entry(spa->spa_g_devstat);
518                 spa->spa_g_devstat = NULL;
519         }
520         if (spa->spa_dev != NULL) {
521                 destroy_dev(spa->spa_dev);
522                 spa->spa_dev = NULL;
523         }
524         vm_object_deallocate(spa->spa_obj);
525         if (spa->spa_kva != NULL) {
526                 pmap_large_unmap(spa->spa_kva, spa->spa_len);
527                 spa->spa_kva = NULL;
528         }
529         mtx_destroy(&spa->spa_g_mtx);
530         mtx_destroy(&spa->spa_g_stat_mtx);
531 }
532
533 static int
534 nvdimm_spa_parse(void *nfitsubtbl, void *arg)
535 {
536         ACPI_NFIT_SYSTEM_ADDRESS *nfitaddr;
537         struct SPA_mapping *spa;
538         int error, *i, j;
539
540         i = arg;
541         spa = &spa_mappings[*i];
542         nfitaddr = nfitsubtbl;
543
544         for (j = 0; j < nitems(nvdimm_SPA_uuid_list); j++) {
545                 /* XXXKIB: is ACPI UUID representation compatible ? */
546                 if (uuidcmp((struct uuid *)&nfitaddr->RangeGuid,
547                     &nvdimm_SPA_uuid_list[j].u_id) != 0)
548                         continue;
549                 error = nvdimm_spa_init_one(spa, nfitaddr, j);
550                 if (error != 0)
551                         nvdimm_spa_fini_one(spa);
552                 break;
553         }
554         if (j == nitems(nvdimm_SPA_uuid_list) && bootverbose) {
555                 printf("Unknown SPA UUID %d ", nfitaddr->RangeIndex);
556                 printf_uuid((struct uuid *)&nfitaddr->RangeGuid);
557                 printf("\n");
558         }
559         (*i)++;
560         return (0);
561 }
562
563 static int
564 nvdimm_spa_init1(ACPI_TABLE_NFIT *nfitbl)
565 {
566         struct nvdimm_SPA_uuid_list_elm *sle;
567         int error, i;
568
569         for (i = 0; i < nitems(nvdimm_SPA_uuid_list); i++) {
570                 sle = &nvdimm_SPA_uuid_list[i];
571                 error = parse_uuid(sle->u_id_str, &sle->u_id);
572                 if (error != 0) {
573                         if (bootverbose)
574                                 printf("nvdimm_identify: error %d parsing "
575                                     "known SPA UUID %d %s\n", error, i,
576                                     sle->u_id_str);
577                         return (error);
578                 }
579         }
580
581         error = nvdimm_iterate_nfit(nfitbl, ACPI_NFIT_TYPE_SYSTEM_ADDRESS,
582             nvdimm_spa_count, &spa_mappings_cnt);
583         if (error != 0)
584                 return (error);
585         spa_mappings = malloc(sizeof(struct SPA_mapping) * spa_mappings_cnt,
586             M_NVDIMM, M_WAITOK | M_ZERO);
587         i = 0;
588         error = nvdimm_iterate_nfit(nfitbl, ACPI_NFIT_TYPE_SYSTEM_ADDRESS,
589             nvdimm_spa_parse, &i);
590         if (error != 0) {
591                 free(spa_mappings, M_NVDIMM);
592                 spa_mappings = NULL;
593                 return (error);
594         }
595         return (0);
596 }
597
598 static void
599 nvdimm_spa_g_init(struct g_class *mp __unused)
600 {
601         ACPI_TABLE_NFIT *nfitbl;
602         ACPI_STATUS status;
603         int error;
604
605         spa_mappings_cnt = 0;
606         spa_mappings = NULL;
607         if (acpi_disabled("nvdimm"))
608                 return;
609         status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
610         if (ACPI_FAILURE(status)) {
611                 if (bootverbose)
612                         printf("nvdimm_spa_g_init: cannot find NFIT\n");
613                 return;
614         }
615         error = nvdimm_spa_init1(nfitbl);
616         if (error != 0)
617                 printf("nvdimm_spa_g_init: error %d\n", error);
618         AcpiPutTable(&nfitbl->Header);
619 }
620
621 static void
622 nvdimm_spa_g_fini(struct g_class *mp __unused)
623 {
624         int i;
625
626         if (spa_mappings == NULL)
627                 return;
628         for (i = 0; i < spa_mappings_cnt; i++)
629                 nvdimm_spa_fini_one(&spa_mappings[i]);
630         free(spa_mappings, M_NVDIMM);
631         spa_mappings = NULL;
632         spa_mappings_cnt = 0;
633 }