xref: /freebsd/sys/dev/nvdimm/nvdimm_spa.c (revision 50757b1452ca4c8df14ee66227cd417c15089d73)
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 				bp->bio_resid = auio.uio_resid;
341 			} else {
342 				nvdimm_spa_g_all_unmapped(spa, bp, bp->bio_cmd);
343 				bp->bio_resid = bp->bio_length;
344 				error = 0;
345 			}
346 		} else {
347 			aiovec.iov_base = bp->bio_data;
348 			aiovec.iov_len = bp->bio_length;
349 			auio.uio_iov = &aiovec;
350 			auio.uio_iovcnt = 1;
351 			auio.uio_resid = bp->bio_length;
352 			auio.uio_offset = bp->bio_offset;
353 			auio.uio_segflg = UIO_SYSSPACE;
354 			auio.uio_rw = bp->bio_cmd == BIO_READ ? UIO_READ :
355 			    UIO_WRITE;
356 			auio.uio_td = curthread;
357 			error = nvdimm_spa_uio(spa, &auio);
358 			bp->bio_resid = auio.uio_resid;
359 		}
360 		bp->bio_bcount = bp->bio_length;
361 		devstat_end_transaction_bio(spa->spa_g_devstat, bp);
362 completed:
363 		bp->bio_completed = bp->bio_length;
364 		g_io_deliver(bp, error);
365 	}
366 }
367 
368 static void
369 nvdimm_spa_g_start(struct bio *bp)
370 {
371 	struct SPA_mapping *spa;
372 
373 	spa = bp->bio_to->geom->softc;
374 	if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
375 		mtx_lock(&spa->spa_g_stat_mtx);
376 		devstat_start_transaction_bio(spa->spa_g_devstat, bp);
377 		mtx_unlock(&spa->spa_g_stat_mtx);
378 	}
379 	mtx_lock(&spa->spa_g_mtx);
380 	bioq_disksort(&spa->spa_g_queue, bp);
381 	wakeup(&spa->spa_g_queue);
382 	mtx_unlock(&spa->spa_g_mtx);
383 }
384 
385 static int
386 nvdimm_spa_g_access(struct g_provider *pp, int r, int w, int e)
387 {
388 
389 	return (0);
390 }
391 
392 static g_init_t nvdimm_spa_g_init;
393 static g_fini_t nvdimm_spa_g_fini;
394 
395 struct g_class nvdimm_spa_g_class = {
396 	.name =		"SPA",
397 	.version =	G_VERSION,
398 	.start =	nvdimm_spa_g_start,
399 	.access =	nvdimm_spa_g_access,
400 	.init =		nvdimm_spa_g_init,
401 	.fini =		nvdimm_spa_g_fini,
402 };
403 DECLARE_GEOM_CLASS(nvdimm_spa_g_class, g_spa);
404 
405 static int
406 nvdimm_spa_init_one(struct SPA_mapping *spa, ACPI_NFIT_SYSTEM_ADDRESS *nfitaddr,
407     int spa_type)
408 {
409 	struct make_dev_args mda;
410 	struct sglist *spa_sg;
411 	int error, error1;
412 
413 	spa->spa_type = spa_type;
414 	spa->spa_domain = ((nfitaddr->Flags & ACPI_NFIT_PROXIMITY_VALID) != 0) ?
415 	    nfitaddr->ProximityDomain : -1;
416 	spa->spa_nfit_idx = nfitaddr->RangeIndex;
417 	spa->spa_phys_base = nfitaddr->Address;
418 	spa->spa_len = nfitaddr->Length;
419 	spa->spa_efi_mem_flags = nfitaddr->MemoryMapping;
420 	if (bootverbose) {
421 		printf("NVDIMM SPA%d base %#016jx len %#016jx %s fl %#jx\n",
422 		    spa->spa_nfit_idx,
423 		    (uintmax_t)spa->spa_phys_base, (uintmax_t)spa->spa_len,
424 		    nvdimm_SPA_uuid_list[spa_type].u_name,
425 		    spa->spa_efi_mem_flags);
426 	}
427 	if (!nvdimm_SPA_uuid_list[spa_type].u_usr_acc)
428 		return (0);
429 
430 	error1 = pmap_large_map(spa->spa_phys_base, spa->spa_len,
431 	    &spa->spa_kva, nvdimm_spa_memattr(spa));
432 	if (error1 != 0) {
433 		printf("NVDIMM SPA%d cannot map into KVA, error %d\n",
434 		    spa->spa_nfit_idx, error1);
435 		spa->spa_kva = NULL;
436 	}
437 
438 	spa_sg = sglist_alloc(1, M_WAITOK);
439 	error = sglist_append_phys(spa_sg, spa->spa_phys_base,
440 	    spa->spa_len);
441 	if (error == 0) {
442 		spa->spa_obj = vm_pager_allocate(OBJT_SG, spa_sg, spa->spa_len,
443 		    VM_PROT_ALL, 0, NULL);
444 		if (spa->spa_obj == NULL) {
445 			printf("NVDIMM SPA%d failed to alloc vm object",
446 			    spa->spa_nfit_idx);
447 			sglist_free(spa_sg);
448 		}
449 	} else {
450 		printf("NVDIMM SPA%d failed to init sglist, error %d",
451 		    spa->spa_nfit_idx, error);
452 		sglist_free(spa_sg);
453 	}
454 
455 	make_dev_args_init(&mda);
456 	mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME;
457 	mda.mda_devsw = &spa_cdevsw;
458 	mda.mda_cr = NULL;
459 	mda.mda_uid = UID_ROOT;
460 	mda.mda_gid = GID_OPERATOR;
461 	mda.mda_mode = 0660;
462 	mda.mda_si_drv1 = spa;
463 	error = make_dev_s(&mda, &spa->spa_dev, "nvdimm_spa%d",
464 	    spa->spa_nfit_idx);
465 	if (error != 0) {
466 		printf("NVDIMM SPA%d cannot create devfs node, error %d\n",
467 		    spa->spa_nfit_idx, error);
468 		if (error1 == 0)
469 			error1 = error;
470 	}
471 
472 	bioq_init(&spa->spa_g_queue);
473 	mtx_init(&spa->spa_g_mtx, "spag", NULL, MTX_DEF);
474 	mtx_init(&spa->spa_g_stat_mtx, "spagst", NULL, MTX_DEF);
475 	spa->spa_g_proc_run = true;
476 	spa->spa_g_proc_exiting = false;
477 	error = kproc_create(nvdimm_spa_g_thread, spa, &spa->spa_g_proc, 0, 0,
478 	    "g_spa%d", spa->spa_nfit_idx);
479 	if (error != 0) {
480 		printf("NVDIMM SPA%d cannot create geom worker, error %d\n",
481 		    spa->spa_nfit_idx, error);
482 		if (error1 == 0)
483 			error1 = error;
484 	} else {
485 		g_topology_assert();
486 		spa->spa_g = g_new_geomf(&nvdimm_spa_g_class, "spa%d",
487 		    spa->spa_nfit_idx);
488 		spa->spa_g->softc = spa;
489 		spa->spa_p = g_new_providerf(spa->spa_g, "spa%d",
490 		    spa->spa_nfit_idx);
491 		spa->spa_p->mediasize = spa->spa_len;
492 		spa->spa_p->sectorsize = DEV_BSIZE;
493 		spa->spa_p->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE |
494 		    G_PF_ACCEPT_UNMAPPED;
495 		g_error_provider(spa->spa_p, 0);
496 		spa->spa_g_devstat = devstat_new_entry("spa", spa->spa_nfit_idx,
497 		    DEV_BSIZE, DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT,
498 		    DEVSTAT_PRIORITY_MAX);
499 	}
500 	return (error1);
501 }
502 
503 static void
504 nvdimm_spa_fini_one(struct SPA_mapping *spa)
505 {
506 
507 	mtx_lock(&spa->spa_g_mtx);
508 	spa->spa_g_proc_run = false;
509 	wakeup(&spa->spa_g_queue);
510 	while (!spa->spa_g_proc_exiting)
511 		msleep(&spa->spa_g_queue, &spa->spa_g_mtx, PRIBIO, "spa_e", 0);
512 	mtx_unlock(&spa->spa_g_mtx);
513 	if (spa->spa_g != NULL) {
514 		g_topology_lock();
515 		g_wither_geom(spa->spa_g, ENXIO);
516 		g_topology_unlock();
517 		spa->spa_g = NULL;
518 		spa->spa_p = NULL;
519 	}
520 	if (spa->spa_g_devstat != NULL) {
521 		devstat_remove_entry(spa->spa_g_devstat);
522 		spa->spa_g_devstat = NULL;
523 	}
524 	if (spa->spa_dev != NULL) {
525 		destroy_dev(spa->spa_dev);
526 		spa->spa_dev = NULL;
527 	}
528 	vm_object_deallocate(spa->spa_obj);
529 	if (spa->spa_kva != NULL) {
530 		pmap_large_unmap(spa->spa_kva, spa->spa_len);
531 		spa->spa_kva = NULL;
532 	}
533 	mtx_destroy(&spa->spa_g_mtx);
534 	mtx_destroy(&spa->spa_g_stat_mtx);
535 }
536 
537 static int
538 nvdimm_spa_parse(void *nfitsubtbl, void *arg)
539 {
540 	ACPI_NFIT_SYSTEM_ADDRESS *nfitaddr;
541 	struct SPA_mapping *spa;
542 	int error, *i, j;
543 
544 	i = arg;
545 	spa = &spa_mappings[*i];
546 	nfitaddr = nfitsubtbl;
547 
548 	for (j = 0; j < nitems(nvdimm_SPA_uuid_list); j++) {
549 		/* XXXKIB: is ACPI UUID representation compatible ? */
550 		if (uuidcmp((struct uuid *)&nfitaddr->RangeGuid,
551 		    &nvdimm_SPA_uuid_list[j].u_id) != 0)
552 			continue;
553 		error = nvdimm_spa_init_one(spa, nfitaddr, j);
554 		if (error != 0)
555 			nvdimm_spa_fini_one(spa);
556 		break;
557 	}
558 	if (j == nitems(nvdimm_SPA_uuid_list) && bootverbose) {
559 		printf("Unknown SPA UUID %d ", nfitaddr->RangeIndex);
560 		printf_uuid((struct uuid *)&nfitaddr->RangeGuid);
561 		printf("\n");
562 	}
563 	(*i)++;
564 	return (0);
565 }
566 
567 static int
568 nvdimm_spa_init1(ACPI_TABLE_NFIT *nfitbl)
569 {
570 	struct nvdimm_SPA_uuid_list_elm *sle;
571 	int error, i;
572 
573 	for (i = 0; i < nitems(nvdimm_SPA_uuid_list); i++) {
574 		sle = &nvdimm_SPA_uuid_list[i];
575 		error = parse_uuid(sle->u_id_str, &sle->u_id);
576 		if (error != 0) {
577 			if (bootverbose)
578 				printf("nvdimm_identify: error %d parsing "
579 				    "known SPA UUID %d %s\n", error, i,
580 				    sle->u_id_str);
581 			return (error);
582 		}
583 	}
584 
585 	error = nvdimm_iterate_nfit(nfitbl, ACPI_NFIT_TYPE_SYSTEM_ADDRESS,
586 	    nvdimm_spa_count, &spa_mappings_cnt);
587 	if (error != 0)
588 		return (error);
589 	spa_mappings = malloc(sizeof(struct SPA_mapping) * spa_mappings_cnt,
590 	    M_NVDIMM, M_WAITOK | M_ZERO);
591 	i = 0;
592 	error = nvdimm_iterate_nfit(nfitbl, ACPI_NFIT_TYPE_SYSTEM_ADDRESS,
593 	    nvdimm_spa_parse, &i);
594 	if (error != 0) {
595 		free(spa_mappings, M_NVDIMM);
596 		spa_mappings = NULL;
597 		return (error);
598 	}
599 	return (0);
600 }
601 
602 static void
603 nvdimm_spa_g_init(struct g_class *mp __unused)
604 {
605 	ACPI_TABLE_NFIT *nfitbl;
606 	ACPI_STATUS status;
607 	int error;
608 
609 	spa_mappings_cnt = 0;
610 	spa_mappings = NULL;
611 	if (acpi_disabled("nvdimm"))
612 		return;
613 	status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
614 	if (ACPI_FAILURE(status)) {
615 		if (bootverbose)
616 			printf("nvdimm_spa_g_init: cannot find NFIT\n");
617 		return;
618 	}
619 	error = nvdimm_spa_init1(nfitbl);
620 	if (error != 0)
621 		printf("nvdimm_spa_g_init: error %d\n", error);
622 	AcpiPutTable(&nfitbl->Header);
623 }
624 
625 static void
626 nvdimm_spa_g_fini(struct g_class *mp __unused)
627 {
628 	int i;
629 
630 	if (spa_mappings == NULL)
631 		return;
632 	for (i = 0; i < spa_mappings_cnt; i++)
633 		nvdimm_spa_fini_one(&spa_mappings[i]);
634 	free(spa_mappings, M_NVDIMM);
635 	spa_mappings = NULL;
636 	spa_mappings_cnt = 0;
637 }
638