xref: /freebsd/sys/dev/nvdimm/nvdimm_spa.c (revision 312809fe7fefbc8d5caa2b59089a5d9266378057)
1 /*-
2  * Copyright (c) 2017, 2018 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/bus.h>
41 #include <sys/conf.h>
42 #include <sys/devicestat.h>
43 #include <sys/disk.h>
44 #include <sys/efi.h>
45 #include <sys/kernel.h>
46 #include <sys/kthread.h>
47 #include <sys/limits.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/module.h>
51 #include <sys/rwlock.h>
52 #include <sys/sglist.h>
53 #include <sys/uio.h>
54 #include <sys/uuid.h>
55 #include <geom/geom.h>
56 #include <geom/geom_int.h>
57 #include <machine/vmparam.h>
58 #include <vm/vm.h>
59 #include <vm/vm_object.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_pager.h>
62 #include <contrib/dev/acpica/include/acpi.h>
63 #include <contrib/dev/acpica/include/accommon.h>
64 #include <contrib/dev/acpica/include/acuuid.h>
65 #include <dev/acpica/acpivar.h>
66 #include <dev/nvdimm/nvdimm_var.h>
67 
68 #define UUID_INITIALIZER_VOLATILE_MEMORY \
69     {0x7305944f,0xfdda,0x44e3,0xb1,0x6c,{0x3f,0x22,0xd2,0x52,0xe5,0xd0}}
70 #define UUID_INITIALIZER_PERSISTENT_MEMORY \
71     {0x66f0d379,0xb4f3,0x4074,0xac,0x43,{0x0d,0x33,0x18,0xb7,0x8c,0xdb}}
72 #define UUID_INITIALIZER_CONTROL_REGION \
73     {0x92f701f6,0x13b4,0x405d,0x91,0x0b,{0x29,0x93,0x67,0xe8,0x23,0x4c}}
74 #define UUID_INITIALIZER_DATA_REGION \
75     {0x91af0530,0x5d86,0x470e,0xa6,0xb0,{0x0a,0x2d,0xb9,0x40,0x82,0x49}}
76 #define UUID_INITIALIZER_VOLATILE_VIRTUAL_DISK \
77     {0x77ab535a,0x45fc,0x624b,0x55,0x60,{0xf7,0xb2,0x81,0xd1,0xf9,0x6e}}
78 #define UUID_INITIALIZER_VOLATILE_VIRTUAL_CD \
79     {0x3d5abd30,0x4175,0x87ce,0x6d,0x64,{0xd2,0xad,0xe5,0x23,0xc4,0xbb}}
80 #define UUID_INITIALIZER_PERSISTENT_VIRTUAL_DISK \
81     {0x5cea02c9,0x4d07,0x69d3,0x26,0x9f,{0x44,0x96,0xfb,0xe0,0x96,0xf9}}
82 #define UUID_INITIALIZER_PERSISTENT_VIRTUAL_CD \
83     {0x08018188,0x42cd,0xbb48,0x10,0x0f,{0x53,0x87,0xd5,0x3d,0xed,0x3d}}
84 
85 static struct nvdimm_SPA_uuid_list_elm {
86 	const char		*u_name;
87 	struct uuid		u_id;
88 	const bool		u_usr_acc;
89 } nvdimm_SPA_uuid_list[] = {
90 	[SPA_TYPE_VOLATILE_MEMORY] = {
91 		.u_name =	"VOLA MEM ",
92 		.u_id =		UUID_INITIALIZER_VOLATILE_MEMORY,
93 		.u_usr_acc =	true,
94 	},
95 	[SPA_TYPE_PERSISTENT_MEMORY] = {
96 		.u_name =	"PERS MEM",
97 		.u_id =		UUID_INITIALIZER_PERSISTENT_MEMORY,
98 		.u_usr_acc =	true,
99 	},
100 	[SPA_TYPE_CONTROL_REGION] = {
101 		.u_name =	"CTRL RG ",
102 		.u_id =		UUID_INITIALIZER_CONTROL_REGION,
103 		.u_usr_acc =	false,
104 	},
105 	[SPA_TYPE_DATA_REGION] = {
106 		.u_name =	"DATA RG ",
107 		.u_id =		UUID_INITIALIZER_DATA_REGION,
108 		.u_usr_acc =	true,
109 	},
110 	[SPA_TYPE_VOLATILE_VIRTUAL_DISK] = {
111 		.u_name =	"VIRT DSK",
112 		.u_id =		UUID_INITIALIZER_VOLATILE_VIRTUAL_DISK,
113 		.u_usr_acc =	true,
114 	},
115 	[SPA_TYPE_VOLATILE_VIRTUAL_CD] = {
116 		.u_name =	"VIRT CD ",
117 		.u_id =		UUID_INITIALIZER_VOLATILE_VIRTUAL_CD,
118 		.u_usr_acc =	true,
119 	},
120 	[SPA_TYPE_PERSISTENT_VIRTUAL_DISK] = {
121 		.u_name =	"PV DSK  ",
122 		.u_id =		UUID_INITIALIZER_PERSISTENT_VIRTUAL_DISK,
123 		.u_usr_acc =	true,
124 	},
125 	[SPA_TYPE_PERSISTENT_VIRTUAL_CD] = {
126 		.u_name =	"PV CD   ",
127 		.u_id =		UUID_INITIALIZER_PERSISTENT_VIRTUAL_CD,
128 		.u_usr_acc =	true,
129 	},
130 };
131 
132 enum SPA_mapping_type
133 nvdimm_spa_type_from_name(const char *name)
134 {
135 	int j;
136 
137 	for (j = 0; j < nitems(nvdimm_SPA_uuid_list); j++) {
138 		if (strcmp(name, nvdimm_SPA_uuid_list[j].u_name) != 0)
139 			continue;
140 		return (j);
141 	}
142 	return (SPA_TYPE_UNKNOWN);
143 }
144 
145 enum SPA_mapping_type
146 nvdimm_spa_type_from_uuid(struct uuid *uuid)
147 {
148 	int j;
149 
150 	for (j = 0; j < nitems(nvdimm_SPA_uuid_list); j++) {
151 		if (uuidcmp(uuid, &nvdimm_SPA_uuid_list[j].u_id) != 0)
152 			continue;
153 		return (j);
154 	}
155 	return (SPA_TYPE_UNKNOWN);
156 }
157 
158 bool
159 nvdimm_spa_type_user_accessible(enum SPA_mapping_type spa_type)
160 {
161 
162 	if ((int)spa_type < 0 || spa_type >= nitems(nvdimm_SPA_uuid_list))
163 		return (false);
164 	return (nvdimm_SPA_uuid_list[spa_type].u_usr_acc);
165 }
166 
167 static vm_memattr_t
168 nvdimm_spa_memattr(uint64_t efi_mem_flags)
169 {
170 	vm_memattr_t mode;
171 
172 	if ((efi_mem_flags & EFI_MD_ATTR_WB) != 0)
173 		mode = VM_MEMATTR_WRITE_BACK;
174 	else if ((efi_mem_flags & EFI_MD_ATTR_WT) != 0)
175 		mode = VM_MEMATTR_WRITE_THROUGH;
176 	else if ((efi_mem_flags & EFI_MD_ATTR_WC) != 0)
177 		mode = VM_MEMATTR_WRITE_COMBINING;
178 	else if ((efi_mem_flags & EFI_MD_ATTR_WP) != 0)
179 		mode = VM_MEMATTR_WRITE_PROTECTED;
180 	else if ((efi_mem_flags & EFI_MD_ATTR_UC) != 0)
181 		mode = VM_MEMATTR_UNCACHEABLE;
182 	else {
183 		if (bootverbose)
184 			printf("SPA mapping attr %#lx unsupported\n",
185 			    efi_mem_flags);
186 		mode = VM_MEMATTR_UNCACHEABLE;
187 	}
188 	return (mode);
189 }
190 
191 static int
192 nvdimm_spa_uio(struct nvdimm_spa_dev *dev, struct uio *uio)
193 {
194 	struct vm_page m, *ma;
195 	off_t off;
196 	vm_memattr_t mattr;
197 	int error, n;
198 
199 	error = 0;
200 	if (dev->spa_kva == NULL) {
201 		mattr = dev->spa_memattr;
202 		bzero(&m, sizeof(m));
203 		vm_page_initfake(&m, 0, mattr);
204 		ma = &m;
205 		while (uio->uio_resid > 0) {
206 			if (uio->uio_offset >= dev->spa_len)
207 				break;
208 			off = dev->spa_phys_base + uio->uio_offset;
209 			vm_page_updatefake(&m, trunc_page(off), mattr);
210 			n = PAGE_SIZE;
211 			if (n > uio->uio_resid)
212 				n = uio->uio_resid;
213 			error = uiomove_fromphys(&ma, off & PAGE_MASK, n, uio);
214 			if (error != 0)
215 				break;
216 		}
217 	} else {
218 		while (uio->uio_resid > 0) {
219 			if (uio->uio_offset >= dev->spa_len)
220 				break;
221 			n = INT_MAX;
222 			if (n > uio->uio_resid)
223 				n = uio->uio_resid;
224 			if (uio->uio_offset + n > dev->spa_len)
225 				n = dev->spa_len - uio->uio_offset;
226 			error = uiomove((char *)dev->spa_kva + uio->uio_offset,
227 			    n, uio);
228 			if (error != 0)
229 				break;
230 		}
231 	}
232 	return (error);
233 }
234 
235 static int
236 nvdimm_spa_rw(struct cdev *dev, struct uio *uio, int ioflag)
237 {
238 
239 	return (nvdimm_spa_uio(dev->si_drv1, uio));
240 }
241 
242 static int
243 nvdimm_spa_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
244     struct thread *td)
245 {
246 	struct nvdimm_spa_dev *dev;
247 	int error;
248 
249 	dev = cdev->si_drv1;
250 	error = 0;
251 	switch (cmd) {
252 	case DIOCGSECTORSIZE:
253 		*(u_int *)data = DEV_BSIZE;
254 		break;
255 	case DIOCGMEDIASIZE:
256 		*(off_t *)data = dev->spa_len;
257 		break;
258 	default:
259 		error = ENOTTY;
260 		break;
261 	}
262 	return (error);
263 }
264 
265 static int
266 nvdimm_spa_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
267     vm_object_t *objp, int nprot)
268 {
269 	struct nvdimm_spa_dev *dev;
270 
271 	dev = cdev->si_drv1;
272 	if (dev->spa_obj == NULL)
273 		return (ENXIO);
274 	if (*offset >= dev->spa_len || *offset + size < *offset ||
275 	    *offset + size > dev->spa_len)
276 		return (EINVAL);
277 	vm_object_reference(dev->spa_obj);
278 	*objp = dev->spa_obj;
279 	return (0);
280 }
281 
282 static struct cdevsw spa_cdevsw = {
283 	.d_version =	D_VERSION,
284 	.d_flags =	D_DISK,
285 	.d_name =	"nvdimm_spa",
286 	.d_read =	nvdimm_spa_rw,
287 	.d_write =	nvdimm_spa_rw,
288 	.d_ioctl =	nvdimm_spa_ioctl,
289 	.d_mmap_single = nvdimm_spa_mmap_single,
290 };
291 
292 static void
293 nvdimm_spa_g_all_unmapped(struct nvdimm_spa_dev *dev, struct bio *bp, int rw)
294 {
295 	struct vm_page maa[bp->bio_ma_n];
296 	vm_page_t ma[bp->bio_ma_n];
297 	vm_memattr_t mattr;
298 	int i;
299 
300 	mattr = dev->spa_memattr;
301 	for (i = 0; i < nitems(ma); i++) {
302 		bzero(&maa[i], sizeof(maa[i]));
303 		vm_page_initfake(&maa[i], dev->spa_phys_base +
304 		    trunc_page(bp->bio_offset) + PAGE_SIZE * i, mattr);
305 		ma[i] = &maa[i];
306 	}
307 	if (rw == BIO_READ)
308 		pmap_copy_pages(ma, bp->bio_offset & PAGE_MASK, bp->bio_ma,
309 		    bp->bio_ma_offset, bp->bio_length);
310 	else
311 		pmap_copy_pages(bp->bio_ma, bp->bio_ma_offset, ma,
312 		    bp->bio_offset & PAGE_MASK, bp->bio_length);
313 }
314 
315 static void
316 nvdimm_spa_g_thread(void *arg)
317 {
318 	struct g_spa *sc;
319 	struct bio *bp;
320 	struct uio auio;
321 	struct iovec aiovec;
322 	int error;
323 
324 	sc = arg;
325 	for (;;) {
326 		mtx_lock(&sc->spa_g_mtx);
327 		for (;;) {
328 			bp = bioq_takefirst(&sc->spa_g_queue);
329 			if (bp != NULL)
330 				break;
331 			msleep(&sc->spa_g_queue, &sc->spa_g_mtx, PRIBIO,
332 			    "spa_g", 0);
333 			if (!sc->spa_g_proc_run) {
334 				sc->spa_g_proc_exiting = true;
335 				wakeup(&sc->spa_g_queue);
336 				mtx_unlock(&sc->spa_g_mtx);
337 				kproc_exit(0);
338 			}
339 			continue;
340 		}
341 		mtx_unlock(&sc->spa_g_mtx);
342 		if (bp->bio_cmd != BIO_READ && bp->bio_cmd != BIO_WRITE &&
343 		    bp->bio_cmd != BIO_FLUSH) {
344 			error = EOPNOTSUPP;
345 			goto completed;
346 		}
347 
348 		error = 0;
349 		if (bp->bio_cmd == BIO_FLUSH) {
350 			if (sc->dev->spa_kva != NULL) {
351 				pmap_large_map_wb(sc->dev->spa_kva,
352 				    sc->dev->spa_len);
353 			} else {
354 				pmap_flush_cache_phys_range(
355 				    (vm_paddr_t)sc->dev->spa_phys_base,
356 				    (vm_paddr_t)sc->dev->spa_phys_base +
357 				    sc->dev->spa_len, sc->dev->spa_memattr);
358 			}
359 			/*
360 			 * XXX flush IMC
361 			 */
362 			goto completed;
363 		}
364 
365 		if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
366 			if (sc->dev->spa_kva != NULL) {
367 				aiovec.iov_base = (char *)sc->dev->spa_kva +
368 				    bp->bio_offset;
369 				aiovec.iov_len = bp->bio_length;
370 				auio.uio_iov = &aiovec;
371 				auio.uio_iovcnt = 1;
372 				auio.uio_resid = bp->bio_length;
373 				auio.uio_offset = bp->bio_offset;
374 				auio.uio_segflg = UIO_SYSSPACE;
375 				auio.uio_rw = bp->bio_cmd == BIO_READ ?
376 				    UIO_WRITE : UIO_READ;
377 				auio.uio_td = curthread;
378 				error = uiomove_fromphys(bp->bio_ma,
379 				    bp->bio_ma_offset, bp->bio_length, &auio);
380 				bp->bio_resid = auio.uio_resid;
381 			} else {
382 				nvdimm_spa_g_all_unmapped(sc->dev, bp,
383 				    bp->bio_cmd);
384 				bp->bio_resid = bp->bio_length;
385 				error = 0;
386 			}
387 		} else {
388 			aiovec.iov_base = bp->bio_data;
389 			aiovec.iov_len = bp->bio_length;
390 			auio.uio_iov = &aiovec;
391 			auio.uio_iovcnt = 1;
392 			auio.uio_resid = bp->bio_length;
393 			auio.uio_offset = bp->bio_offset;
394 			auio.uio_segflg = UIO_SYSSPACE;
395 			auio.uio_rw = bp->bio_cmd == BIO_READ ? UIO_READ :
396 			    UIO_WRITE;
397 			auio.uio_td = curthread;
398 			error = nvdimm_spa_uio(sc->dev, &auio);
399 			bp->bio_resid = auio.uio_resid;
400 		}
401 		bp->bio_bcount = bp->bio_length;
402 		devstat_end_transaction_bio(sc->spa_g_devstat, bp);
403 completed:
404 		bp->bio_completed = bp->bio_length;
405 		g_io_deliver(bp, error);
406 	}
407 }
408 
409 static void
410 nvdimm_spa_g_start(struct bio *bp)
411 {
412 	struct g_spa *sc;
413 
414 	sc = bp->bio_to->geom->softc;
415 	if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
416 		mtx_lock(&sc->spa_g_stat_mtx);
417 		devstat_start_transaction_bio(sc->spa_g_devstat, bp);
418 		mtx_unlock(&sc->spa_g_stat_mtx);
419 	}
420 	mtx_lock(&sc->spa_g_mtx);
421 	bioq_disksort(&sc->spa_g_queue, bp);
422 	wakeup(&sc->spa_g_queue);
423 	mtx_unlock(&sc->spa_g_mtx);
424 }
425 
426 static int
427 nvdimm_spa_g_access(struct g_provider *pp, int r, int w, int e)
428 {
429 
430 	return (0);
431 }
432 
433 static struct g_geom * nvdimm_spa_g_create(struct nvdimm_spa_dev *dev,
434     const char *name);
435 static g_ctl_destroy_geom_t nvdimm_spa_g_destroy_geom;
436 
437 struct g_class nvdimm_spa_g_class = {
438 	.name =		"SPA",
439 	.version =	G_VERSION,
440 	.start =	nvdimm_spa_g_start,
441 	.access =	nvdimm_spa_g_access,
442 	.destroy_geom =	nvdimm_spa_g_destroy_geom,
443 };
444 DECLARE_GEOM_CLASS(nvdimm_spa_g_class, g_spa);
445 
446 int
447 nvdimm_spa_init(struct SPA_mapping *spa, ACPI_NFIT_SYSTEM_ADDRESS *nfitaddr,
448     enum SPA_mapping_type spa_type)
449 {
450 	char *name;
451 	int error;
452 
453 	spa->spa_type = spa_type;
454 	spa->spa_nfit_idx = nfitaddr->RangeIndex;
455 	spa->dev.spa_domain =
456 	    ((nfitaddr->Flags & ACPI_NFIT_PROXIMITY_VALID) != 0) ?
457 	    nfitaddr->ProximityDomain : -1;
458 	spa->dev.spa_phys_base = nfitaddr->Address;
459 	spa->dev.spa_len = nfitaddr->Length;
460 	spa->dev.spa_efi_mem_flags = nfitaddr->MemoryMapping;
461 	if (bootverbose) {
462 		printf("NVDIMM SPA%d base %#016jx len %#016jx %s fl %#jx\n",
463 		    spa->spa_nfit_idx,
464 		    (uintmax_t)spa->dev.spa_phys_base,
465 		    (uintmax_t)spa->dev.spa_len,
466 		    nvdimm_SPA_uuid_list[spa_type].u_name,
467 		    spa->dev.spa_efi_mem_flags);
468 	}
469 	spa->dev.spa_memattr = nvdimm_spa_memattr(nfitaddr->MemoryMapping);
470 	if (!nvdimm_SPA_uuid_list[spa_type].u_usr_acc)
471 		return (0);
472 
473 	asprintf(&name, M_NVDIMM, "spa%d", spa->spa_nfit_idx);
474 	error = nvdimm_spa_dev_init(&spa->dev, name, spa->spa_nfit_idx);
475 	free(name, M_NVDIMM);
476 	return (error);
477 }
478 
479 int
480 nvdimm_spa_dev_init(struct nvdimm_spa_dev *dev, const char *name, int unit)
481 {
482 	struct make_dev_args mda;
483 	struct sglist *spa_sg;
484 	char *devname;
485 	int error, error1;
486 
487 	error1 = pmap_large_map(dev->spa_phys_base, dev->spa_len,
488 	    &dev->spa_kva, dev->spa_memattr);
489 	if (error1 != 0) {
490 		printf("NVDIMM %s cannot map into KVA, error %d\n", name,
491 		    error1);
492 		dev->spa_kva = NULL;
493 	}
494 
495 	spa_sg = sglist_alloc(1, M_WAITOK);
496 	error = sglist_append_phys(spa_sg, dev->spa_phys_base,
497 	    dev->spa_len);
498 	if (error == 0) {
499 		dev->spa_obj = vm_pager_allocate(OBJT_SG, spa_sg, dev->spa_len,
500 		    VM_PROT_ALL, 0, NULL);
501 		if (dev->spa_obj == NULL) {
502 			printf("NVDIMM %s failed to alloc vm object", name);
503 			sglist_free(spa_sg);
504 		}
505 	} else {
506 		printf("NVDIMM %s failed to init sglist, error %d", name,
507 		    error);
508 		sglist_free(spa_sg);
509 	}
510 
511 	make_dev_args_init(&mda);
512 	mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME;
513 	mda.mda_devsw = &spa_cdevsw;
514 	mda.mda_cr = NULL;
515 	mda.mda_uid = UID_ROOT;
516 	mda.mda_gid = GID_OPERATOR;
517 	mda.mda_mode = 0660;
518 	mda.mda_si_drv1 = dev;
519 	mda.mda_unit = unit;
520 	asprintf(&devname, M_NVDIMM, "nvdimm_%s", name);
521 	error = make_dev_s(&mda, &dev->spa_dev, "%s", devname);
522 	free(devname, M_NVDIMM);
523 	if (error != 0) {
524 		printf("NVDIMM %s cannot create devfs node, error %d\n", name,
525 		    error);
526 		if (error1 == 0)
527 			error1 = error;
528 	}
529 	dev->spa_g = nvdimm_spa_g_create(dev, name);
530 	if (dev->spa_g == NULL && error1 == 0)
531 		error1 = ENXIO;
532 	return (error1);
533 }
534 
535 static struct g_geom *
536 nvdimm_spa_g_create(struct nvdimm_spa_dev *dev, const char *name)
537 {
538 	struct g_geom *gp;
539 	struct g_spa *sc;
540 	int error;
541 
542 	gp = NULL;
543 	sc = malloc(sizeof(struct g_spa), M_NVDIMM, M_WAITOK | M_ZERO);
544 	sc->dev = dev;
545 	bioq_init(&sc->spa_g_queue);
546 	mtx_init(&sc->spa_g_mtx, "spag", NULL, MTX_DEF);
547 	mtx_init(&sc->spa_g_stat_mtx, "spagst", NULL, MTX_DEF);
548 	sc->spa_g_proc_run = true;
549 	sc->spa_g_proc_exiting = false;
550 	error = kproc_create(nvdimm_spa_g_thread, sc, &sc->spa_g_proc, 0, 0,
551 	    "g_spa");
552 	if (error != 0) {
553 		mtx_destroy(&sc->spa_g_mtx);
554 		mtx_destroy(&sc->spa_g_stat_mtx);
555 		free(sc, M_NVDIMM);
556 		printf("NVDIMM %s cannot create geom worker, error %d\n", name,
557 		    error);
558 	} else {
559 		g_topology_lock();
560 		gp = g_new_geomf(&nvdimm_spa_g_class, "%s", name);
561 		gp->softc = sc;
562 		sc->spa_p = g_new_providerf(gp, "%s", name);
563 		sc->spa_p->mediasize = dev->spa_len;
564 		sc->spa_p->sectorsize = DEV_BSIZE;
565 		sc->spa_p->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE |
566 		    G_PF_ACCEPT_UNMAPPED;
567 		g_error_provider(sc->spa_p, 0);
568 		sc->spa_g_devstat = devstat_new_entry("spa", -1, DEV_BSIZE,
569 		    DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT,
570 		    DEVSTAT_PRIORITY_MAX);
571 		g_topology_unlock();
572 	}
573 	return (gp);
574 }
575 
576 void
577 nvdimm_spa_fini(struct SPA_mapping *spa)
578 {
579 
580 	nvdimm_spa_dev_fini(&spa->dev);
581 }
582 
583 void
584 nvdimm_spa_dev_fini(struct nvdimm_spa_dev *dev)
585 {
586 
587 	if (dev->spa_g != NULL) {
588 		g_topology_lock();
589 		nvdimm_spa_g_destroy_geom(NULL, dev->spa_g->class, dev->spa_g);
590 		g_topology_unlock();
591 	}
592 	if (dev->spa_dev != NULL) {
593 		destroy_dev(dev->spa_dev);
594 		dev->spa_dev = NULL;
595 	}
596 	vm_object_deallocate(dev->spa_obj);
597 	if (dev->spa_kva != NULL) {
598 		pmap_large_unmap(dev->spa_kva, dev->spa_len);
599 		dev->spa_kva = NULL;
600 	}
601 }
602 
603 static int
604 nvdimm_spa_g_destroy_geom(struct gctl_req *req, struct g_class *cp,
605     struct g_geom *gp)
606 {
607 	struct g_spa *sc;
608 
609 	sc = gp->softc;
610 	mtx_lock(&sc->spa_g_mtx);
611 	sc->spa_g_proc_run = false;
612 	wakeup(&sc->spa_g_queue);
613 	while (!sc->spa_g_proc_exiting)
614 		msleep(&sc->spa_g_queue, &sc->spa_g_mtx, PRIBIO, "spa_e", 0);
615 	mtx_unlock(&sc->spa_g_mtx);
616 	g_topology_assert();
617 	g_wither_geom(gp, ENXIO);
618 	sc->spa_p = NULL;
619 	if (sc->spa_g_devstat != NULL) {
620 		devstat_remove_entry(sc->spa_g_devstat);
621 		sc->spa_g_devstat = NULL;
622 	}
623 	mtx_destroy(&sc->spa_g_mtx);
624 	mtx_destroy(&sc->spa_g_stat_mtx);
625 	free(sc, M_NVDIMM);
626 	return (0);
627 }
628