xref: /freebsd/sys/cam/nvme/nvme_da.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2015 Netflix, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer,
11  *    without modification, immediately at the beginning of the file.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * Derived from ata_da.c:
28  * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 
36 #ifdef _KERNEL
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/bio.h>
40 #include <sys/sysctl.h>
41 #include <sys/taskqueue.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/conf.h>
45 #include <sys/devicestat.h>
46 #include <sys/eventhandler.h>
47 #include <sys/malloc.h>
48 #include <sys/cons.h>
49 #include <sys/proc.h>
50 #include <sys/reboot.h>
51 #include <geom/geom_disk.h>
52 #endif /* _KERNEL */
53 
54 #ifndef _KERNEL
55 #include <stdio.h>
56 #include <string.h>
57 #endif /* _KERNEL */
58 
59 #include <cam/cam.h>
60 #include <cam/cam_ccb.h>
61 #include <cam/cam_periph.h>
62 #include <cam/cam_xpt_periph.h>
63 #include <cam/cam_sim.h>
64 #include <cam/cam_iosched.h>
65 
66 #include <cam/nvme/nvme_all.h>
67 
68 typedef enum {
69 	NDA_STATE_NORMAL
70 } nda_state;
71 
72 typedef enum {
73 	NDA_FLAG_OPEN		= 0x0001,
74 	NDA_FLAG_DIRTY		= 0x0002,
75 	NDA_FLAG_SCTX_INIT	= 0x0004,
76 } nda_flags;
77 
78 typedef enum {
79 	NDA_Q_4K   = 0x01,
80 	NDA_Q_NONE = 0x00,
81 } nda_quirks;
82 
83 #define NDA_Q_BIT_STRING	\
84 	"\020"			\
85 	"\001Bit 0"
86 
87 typedef enum {
88 	NDA_CCB_BUFFER_IO	= 0x01,
89 	NDA_CCB_DUMP            = 0x02,
90 	NDA_CCB_TRIM            = 0x03,
91 	NDA_CCB_TYPE_MASK	= 0x0F,
92 } nda_ccb_state;
93 
94 /* Offsets into our private area for storing information */
95 #define ccb_state	ccb_h.ppriv_field0
96 #define ccb_bp		ccb_h.ppriv_ptr1	/* For NDA_CCB_BUFFER_IO */
97 #define ccb_trim	ccb_h.ppriv_ptr1	/* For NDA_CCB_TRIM */
98 
99 struct nda_softc {
100 	struct   cam_iosched_softc *cam_iosched;
101 	int			outstanding_cmds;	/* Number of active commands */
102 	int			refcount;		/* Active xpt_action() calls */
103 	nda_state		state;
104 	nda_flags		flags;
105 	nda_quirks		quirks;
106 	int			unmappedio;
107 	quad_t			deletes;
108 	uint32_t		nsid;			/* Namespace ID for this nda device */
109 	struct disk		*disk;
110 	struct task		sysctl_task;
111 	struct sysctl_ctx_list	sysctl_ctx;
112 	struct sysctl_oid	*sysctl_tree;
113 	uint64_t		trim_count;
114 	uint64_t		trim_ranges;
115 	uint64_t		trim_lbas;
116 #ifdef CAM_TEST_FAILURE
117 	int			force_read_error;
118 	int			force_write_error;
119 	int			periodic_read_error;
120 	int			periodic_read_count;
121 #endif
122 #ifdef CAM_IO_STATS
123 	struct sysctl_ctx_list	sysctl_stats_ctx;
124 	struct sysctl_oid	*sysctl_stats_tree;
125 	u_int			timeouts;
126 	u_int			errors;
127 	u_int			invalidations;
128 #endif
129 };
130 
131 struct nda_trim_request {
132 	union {
133 		struct nvme_dsm_range dsm;
134 		uint8_t		data[NVME_MAX_DSM_TRIM];
135 	};
136 	TAILQ_HEAD(, bio) bps;
137 };
138 
139 /* Need quirk table */
140 
141 static	disk_strategy_t	ndastrategy;
142 static	dumper_t	ndadump;
143 static	periph_init_t	ndainit;
144 static	void		ndaasync(void *callback_arg, u_int32_t code,
145 				struct cam_path *path, void *arg);
146 static	void		ndasysctlinit(void *context, int pending);
147 static	periph_ctor_t	ndaregister;
148 static	periph_dtor_t	ndacleanup;
149 static	periph_start_t	ndastart;
150 static	periph_oninv_t	ndaoninvalidate;
151 static	void		ndadone(struct cam_periph *periph,
152 			       union ccb *done_ccb);
153 static  int		ndaerror(union ccb *ccb, u_int32_t cam_flags,
154 				u_int32_t sense_flags);
155 static void		ndashutdown(void *arg, int howto);
156 static void		ndasuspend(void *arg);
157 
158 #ifndef	NDA_DEFAULT_SEND_ORDERED
159 #define	NDA_DEFAULT_SEND_ORDERED	1
160 #endif
161 #ifndef NDA_DEFAULT_TIMEOUT
162 #define NDA_DEFAULT_TIMEOUT 30	/* Timeout in seconds */
163 #endif
164 #ifndef	NDA_DEFAULT_RETRY
165 #define	NDA_DEFAULT_RETRY	4
166 #endif
167 #ifndef NDA_MAX_TRIM_ENTRIES
168 #define NDA_MAX_TRIM_ENTRIES  (NVME_MAX_DSM_TRIM / sizeof(struct nvme_dsm_range))/* Number of DSM trims to use, max 256 */
169 #endif
170 
171 static SYSCTL_NODE(_kern_cam, OID_AUTO, nda, CTLFLAG_RD, 0,
172             "CAM Direct Access Disk driver");
173 
174 //static int nda_retry_count = NDA_DEFAULT_RETRY;
175 static int nda_send_ordered = NDA_DEFAULT_SEND_ORDERED;
176 static int nda_default_timeout = NDA_DEFAULT_TIMEOUT;
177 static int nda_max_trim_entries = NDA_MAX_TRIM_ENTRIES;
178 SYSCTL_INT(_kern_cam_nda, OID_AUTO, max_trim, CTLFLAG_RDTUN,
179     &nda_max_trim_entries, NDA_MAX_TRIM_ENTRIES,
180     "Maximum number of BIO_DELETE to send down as a DSM TRIM.");
181 
182 /*
183  * All NVMe media is non-rotational, so all nvme device instances
184  * share this to implement the sysctl.
185  */
186 static int nda_rotating_media = 0;
187 
188 static struct periph_driver ndadriver =
189 {
190 	ndainit, "nda",
191 	TAILQ_HEAD_INITIALIZER(ndadriver.units), /* generation */ 0
192 };
193 
194 PERIPHDRIVER_DECLARE(nda, ndadriver);
195 
196 static MALLOC_DEFINE(M_NVMEDA, "nvme_da", "nvme_da buffers");
197 
198 /*
199  * nice wrappers. Maybe these belong in nvme_all.c instead of
200  * here, but this is the only place that uses these. Should
201  * we ever grow another NVME periph, we should move them
202  * all there wholesale.
203  */
204 
205 static void
206 nda_nvme_flush(struct nda_softc *softc, struct ccb_nvmeio *nvmeio)
207 {
208 	cam_fill_nvmeio(nvmeio,
209 	    0,			/* retries */
210 	    ndadone,		/* cbfcnp */
211 	    CAM_DIR_NONE,	/* flags */
212 	    NULL,		/* data_ptr */
213 	    0,			/* dxfer_len */
214 	    nda_default_timeout * 1000); /* timeout 30s */
215 	nvme_ns_flush_cmd(&nvmeio->cmd, softc->nsid);
216 }
217 
218 static void
219 nda_nvme_trim(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
220     void *payload, uint32_t num_ranges)
221 {
222 	cam_fill_nvmeio(nvmeio,
223 	    0,			/* retries */
224 	    ndadone,		/* cbfcnp */
225 	    CAM_DIR_OUT,	/* flags */
226 	    payload,		/* data_ptr */
227 	    num_ranges * sizeof(struct nvme_dsm_range), /* dxfer_len */
228 	    nda_default_timeout * 1000); /* timeout 30s */
229 	nvme_ns_trim_cmd(&nvmeio->cmd, softc->nsid, num_ranges);
230 }
231 
232 static void
233 nda_nvme_write(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
234     void *payload, uint64_t lba, uint32_t len, uint32_t count)
235 {
236 	cam_fill_nvmeio(nvmeio,
237 	    0,			/* retries */
238 	    ndadone,		/* cbfcnp */
239 	    CAM_DIR_OUT,	/* flags */
240 	    payload,		/* data_ptr */
241 	    len,		/* dxfer_len */
242 	    nda_default_timeout * 1000); /* timeout 30s */
243 	nvme_ns_write_cmd(&nvmeio->cmd, softc->nsid, lba, count);
244 }
245 
246 static void
247 nda_nvme_rw_bio(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
248     struct bio *bp, uint32_t rwcmd)
249 {
250 	int flags = rwcmd == NVME_OPC_READ ? CAM_DIR_IN : CAM_DIR_OUT;
251 	void *payload;
252 	uint64_t lba;
253 	uint32_t count;
254 
255 	if (bp->bio_flags & BIO_UNMAPPED) {
256 		flags |= CAM_DATA_BIO;
257 		payload = bp;
258 	} else {
259 		payload = bp->bio_data;
260 	}
261 
262 	lba = bp->bio_pblkno;
263 	count = bp->bio_bcount / softc->disk->d_sectorsize;
264 
265 	cam_fill_nvmeio(nvmeio,
266 	    0,			/* retries */
267 	    ndadone,		/* cbfcnp */
268 	    flags,		/* flags */
269 	    payload,		/* data_ptr */
270 	    bp->bio_bcount,	/* dxfer_len */
271 	    nda_default_timeout * 1000); /* timeout 30s */
272 	nvme_ns_rw_cmd(&nvmeio->cmd, rwcmd, softc->nsid, lba, count);
273 }
274 
275 static int
276 ndaopen(struct disk *dp)
277 {
278 	struct cam_periph *periph;
279 	struct nda_softc *softc;
280 	int error;
281 
282 	periph = (struct cam_periph *)dp->d_drv1;
283 	if (cam_periph_acquire(periph) != 0) {
284 		return(ENXIO);
285 	}
286 
287 	cam_periph_lock(periph);
288 	if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
289 		cam_periph_unlock(periph);
290 		cam_periph_release(periph);
291 		return (error);
292 	}
293 
294 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
295 	    ("ndaopen\n"));
296 
297 	softc = (struct nda_softc *)periph->softc;
298 	softc->flags |= NDA_FLAG_OPEN;
299 
300 	cam_periph_unhold(periph);
301 	cam_periph_unlock(periph);
302 	return (0);
303 }
304 
305 static int
306 ndaclose(struct disk *dp)
307 {
308 	struct	cam_periph *periph;
309 	struct	nda_softc *softc;
310 	union ccb *ccb;
311 	int error;
312 
313 	periph = (struct cam_periph *)dp->d_drv1;
314 	softc = (struct nda_softc *)periph->softc;
315 	cam_periph_lock(periph);
316 
317 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
318 	    ("ndaclose\n"));
319 
320 	if ((softc->flags & NDA_FLAG_DIRTY) != 0 &&
321 	    (periph->flags & CAM_PERIPH_INVALID) == 0 &&
322 	    cam_periph_hold(periph, PRIBIO) == 0) {
323 
324 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
325 		nda_nvme_flush(softc, &ccb->nvmeio);
326 		error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0,
327 		    /*sense_flags*/0, softc->disk->d_devstat);
328 
329 		if (error != 0)
330 			xpt_print(periph->path, "Synchronize cache failed\n");
331 		else
332 			softc->flags &= ~NDA_FLAG_DIRTY;
333 		xpt_release_ccb(ccb);
334 		cam_periph_unhold(periph);
335 	}
336 
337 	softc->flags &= ~NDA_FLAG_OPEN;
338 
339 	while (softc->refcount != 0)
340 		cam_periph_sleep(periph, &softc->refcount, PRIBIO, "ndaclose", 1);
341 	KASSERT(softc->outstanding_cmds == 0,
342 	    ("nda %d outstanding commands", softc->outstanding_cmds));
343 	cam_periph_unlock(periph);
344 	cam_periph_release(periph);
345 	return (0);
346 }
347 
348 static void
349 ndaschedule(struct cam_periph *periph)
350 {
351 	struct nda_softc *softc = (struct nda_softc *)periph->softc;
352 
353 	if (softc->state != NDA_STATE_NORMAL)
354 		return;
355 
356 	cam_iosched_schedule(softc->cam_iosched, periph);
357 }
358 
359 /*
360  * Actually translate the requested transfer into one the physical driver
361  * can understand.  The transfer is described by a buf and will include
362  * only one physical transfer.
363  */
364 static void
365 ndastrategy(struct bio *bp)
366 {
367 	struct cam_periph *periph;
368 	struct nda_softc *softc;
369 
370 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
371 	softc = (struct nda_softc *)periph->softc;
372 
373 	cam_periph_lock(periph);
374 
375 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastrategy(%p)\n", bp));
376 
377 	/*
378 	 * If the device has been made invalid, error out
379 	 */
380 	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
381 		cam_periph_unlock(periph);
382 		biofinish(bp, NULL, ENXIO);
383 		return;
384 	}
385 
386 	if (bp->bio_cmd == BIO_DELETE)
387 		softc->deletes++;
388 
389 	/*
390 	 * Place it in the queue of disk activities for this disk
391 	 */
392 	cam_iosched_queue_work(softc->cam_iosched, bp);
393 
394 	/*
395 	 * Schedule ourselves for performing the work.
396 	 */
397 	ndaschedule(periph);
398 	cam_periph_unlock(periph);
399 
400 	return;
401 }
402 
403 static int
404 ndadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
405 {
406 	struct	    cam_periph *periph;
407 	struct	    nda_softc *softc;
408 	u_int	    secsize;
409 	struct ccb_nvmeio nvmeio;
410 	struct	    disk *dp;
411 	uint64_t    lba;
412 	uint32_t    count;
413 	int	    error = 0;
414 
415 	dp = arg;
416 	periph = dp->d_drv1;
417 	softc = (struct nda_softc *)periph->softc;
418 	secsize = softc->disk->d_sectorsize;
419 	lba = offset / secsize;
420 	count = length / secsize;
421 
422 	if ((periph->flags & CAM_PERIPH_INVALID) != 0)
423 		return (ENXIO);
424 
425 	/* xpt_get_ccb returns a zero'd allocation for the ccb, mimic that here */
426 	memset(&nvmeio, 0, sizeof(nvmeio));
427 	if (length > 0) {
428 		xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
429 		nvmeio.ccb_state = NDA_CCB_DUMP;
430 		nda_nvme_write(softc, &nvmeio, virtual, lba, length, count);
431 		error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error,
432 		    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
433 		if (error != 0)
434 			printf("Aborting dump due to I/O error %d.\n", error);
435 
436 		return (error);
437 	}
438 
439 	/* Flush */
440 	xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
441 
442 	nvmeio.ccb_state = NDA_CCB_DUMP;
443 	nda_nvme_flush(softc, &nvmeio);
444 	error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error,
445 	    0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
446 	if (error != 0)
447 		xpt_print(periph->path, "flush cmd failed\n");
448 	return (error);
449 }
450 
451 static void
452 ndainit(void)
453 {
454 	cam_status status;
455 
456 	/*
457 	 * Install a global async callback.  This callback will
458 	 * receive async callbacks like "new device found".
459 	 */
460 	status = xpt_register_async(AC_FOUND_DEVICE, ndaasync, NULL, NULL);
461 
462 	if (status != CAM_REQ_CMP) {
463 		printf("nda: Failed to attach master async callback "
464 		       "due to status 0x%x!\n", status);
465 	} else if (nda_send_ordered) {
466 
467 		/* Register our event handlers */
468 		if ((EVENTHANDLER_REGISTER(power_suspend, ndasuspend,
469 					   NULL, EVENTHANDLER_PRI_LAST)) == NULL)
470 		    printf("ndainit: power event registration failed!\n");
471 		if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ndashutdown,
472 					   NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
473 		    printf("ndainit: shutdown event registration failed!\n");
474 	}
475 }
476 
477 /*
478  * Callback from GEOM, called when it has finished cleaning up its
479  * resources.
480  */
481 static void
482 ndadiskgonecb(struct disk *dp)
483 {
484 	struct cam_periph *periph;
485 
486 	periph = (struct cam_periph *)dp->d_drv1;
487 
488 	cam_periph_release(periph);
489 }
490 
491 static void
492 ndaoninvalidate(struct cam_periph *periph)
493 {
494 	struct nda_softc *softc;
495 
496 	softc = (struct nda_softc *)periph->softc;
497 
498 	/*
499 	 * De-register any async callbacks.
500 	 */
501 	xpt_register_async(0, ndaasync, periph, periph->path);
502 #ifdef CAM_IO_STATS
503 	softc->invalidations++;
504 #endif
505 
506 	/*
507 	 * Return all queued I/O with ENXIO.
508 	 * XXX Handle any transactions queued to the card
509 	 *     with XPT_ABORT_CCB.
510 	 */
511 	cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
512 
513 	disk_gone(softc->disk);
514 }
515 
516 static void
517 ndacleanup(struct cam_periph *periph)
518 {
519 	struct nda_softc *softc;
520 
521 	softc = (struct nda_softc *)periph->softc;
522 
523 	cam_periph_unlock(periph);
524 
525 	cam_iosched_fini(softc->cam_iosched);
526 
527 	/*
528 	 * If we can't free the sysctl tree, oh well...
529 	 */
530 	if ((softc->flags & NDA_FLAG_SCTX_INIT) != 0) {
531 #ifdef CAM_IO_STATS
532 		if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
533 			xpt_print(periph->path,
534 			    "can't remove sysctl stats context\n");
535 #endif
536 		if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
537 			xpt_print(periph->path,
538 			    "can't remove sysctl context\n");
539 	}
540 
541 	disk_destroy(softc->disk);
542 	free(softc, M_DEVBUF);
543 	cam_periph_lock(periph);
544 }
545 
546 static void
547 ndaasync(void *callback_arg, u_int32_t code,
548 	struct cam_path *path, void *arg)
549 {
550 	struct cam_periph *periph;
551 
552 	periph = (struct cam_periph *)callback_arg;
553 	switch (code) {
554 	case AC_FOUND_DEVICE:
555 	{
556 		struct ccb_getdev *cgd;
557 		cam_status status;
558 
559 		cgd = (struct ccb_getdev *)arg;
560 		if (cgd == NULL)
561 			break;
562 
563 		if (cgd->protocol != PROTO_NVME)
564 			break;
565 
566 		/*
567 		 * Allocate a peripheral instance for
568 		 * this device and start the probe
569 		 * process.
570 		 */
571 		status = cam_periph_alloc(ndaregister, ndaoninvalidate,
572 					  ndacleanup, ndastart,
573 					  "nda", CAM_PERIPH_BIO,
574 					  path, ndaasync,
575 					  AC_FOUND_DEVICE, cgd);
576 
577 		if (status != CAM_REQ_CMP
578 		 && status != CAM_REQ_INPROG)
579 			printf("ndaasync: Unable to attach to new device "
580 				"due to status 0x%x\n", status);
581 		break;
582 	}
583 	case AC_ADVINFO_CHANGED:
584 	{
585 		uintptr_t buftype;
586 
587 		buftype = (uintptr_t)arg;
588 		if (buftype == CDAI_TYPE_PHYS_PATH) {
589 			struct nda_softc *softc;
590 
591 			softc = periph->softc;
592 			disk_attr_changed(softc->disk, "GEOM::physpath",
593 					  M_NOWAIT);
594 		}
595 		break;
596 	}
597 	case AC_LOST_DEVICE:
598 	default:
599 		cam_periph_async(periph, code, path, arg);
600 		break;
601 	}
602 }
603 
604 static void
605 ndasysctlinit(void *context, int pending)
606 {
607 	struct cam_periph *periph;
608 	struct nda_softc *softc;
609 	char tmpstr[32], tmpstr2[16];
610 
611 	periph = (struct cam_periph *)context;
612 
613 	/* periph was held for us when this task was enqueued */
614 	if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
615 		cam_periph_release(periph);
616 		return;
617 	}
618 
619 	softc = (struct nda_softc *)periph->softc;
620 	snprintf(tmpstr, sizeof(tmpstr), "CAM NDA unit %d", periph->unit_number);
621 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
622 
623 	sysctl_ctx_init(&softc->sysctl_ctx);
624 	softc->flags |= NDA_FLAG_SCTX_INIT;
625 	softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
626 		SYSCTL_STATIC_CHILDREN(_kern_cam_nda), OID_AUTO, tmpstr2,
627 		CTLFLAG_RD, 0, tmpstr, "device_index");
628 	if (softc->sysctl_tree == NULL) {
629 		printf("ndasysctlinit: unable to allocate sysctl tree\n");
630 		cam_periph_release(periph);
631 		return;
632 	}
633 
634 	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
635 	    OID_AUTO, "unmapped_io", CTLFLAG_RD,
636 	    &softc->unmappedio, 0, "Unmapped I/O leaf");
637 
638 	SYSCTL_ADD_QUAD(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
639 	    OID_AUTO, "deletes", CTLFLAG_RD,
640 	    &softc->deletes, "Number of BIO_DELETE requests");
641 
642 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
643 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
644 		"trim_count", CTLFLAG_RD, &softc->trim_count,
645 		"Total number of unmap/dsm commands sent");
646 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
647 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
648 		"trim_ranges", CTLFLAG_RD, &softc->trim_ranges,
649 		"Total number of ranges in unmap/dsm commands");
650 	SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
651 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
652 		"trim_lbas", CTLFLAG_RD, &softc->trim_lbas,
653 		"Total lbas in the unmap/dsm commands sent");
654 
655 	SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
656 	    OID_AUTO, "rotating", CTLFLAG_RD, &nda_rotating_media, 1,
657 	    "Rotating media");
658 
659 #ifdef CAM_IO_STATS
660 	softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
661 		SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
662 		CTLFLAG_RD, 0, "Statistics");
663 	if (softc->sysctl_stats_tree == NULL) {
664 		printf("ndasysctlinit: unable to allocate sysctl tree for stats\n");
665 		cam_periph_release(periph);
666 		return;
667 	}
668 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
669 		SYSCTL_CHILDREN(softc->sysctl_stats_tree),
670 		OID_AUTO, "timeouts", CTLFLAG_RD,
671 		&softc->timeouts, 0,
672 		"Device timeouts reported by the SIM");
673 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
674 		SYSCTL_CHILDREN(softc->sysctl_stats_tree),
675 		OID_AUTO, "errors", CTLFLAG_RD,
676 		&softc->errors, 0,
677 		"Transport errors reported by the SIM.");
678 	SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
679 		SYSCTL_CHILDREN(softc->sysctl_stats_tree),
680 		OID_AUTO, "pack_invalidations", CTLFLAG_RD,
681 		&softc->invalidations, 0,
682 		"Device pack invalidations.");
683 #endif
684 
685 #ifdef CAM_TEST_FAILURE
686 	SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
687 		OID_AUTO, "invalidate", CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
688 		periph, 0, cam_periph_invalidate_sysctl, "I",
689 		"Write 1 to invalidate the drive immediately");
690 #endif
691 
692 	cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
693 	    softc->sysctl_tree);
694 
695 	cam_periph_release(periph);
696 }
697 
698 static int
699 ndagetattr(struct bio *bp)
700 {
701 	int ret;
702 	struct cam_periph *periph;
703 
704 	periph = (struct cam_periph *)bp->bio_disk->d_drv1;
705 	cam_periph_lock(periph);
706 	ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
707 	    periph->path);
708 	cam_periph_unlock(periph);
709 	if (ret == 0)
710 		bp->bio_completed = bp->bio_length;
711 	return ret;
712 }
713 
714 static cam_status
715 ndaregister(struct cam_periph *periph, void *arg)
716 {
717 	struct nda_softc *softc;
718 	struct disk *disk;
719 	struct ccb_pathinq cpi;
720 	const struct nvme_namespace_data *nsd;
721 	const struct nvme_controller_data *cd;
722 	char   announce_buf[80];
723 	uint8_t flbas_fmt, lbads, vwc_present;
724 	u_int maxio;
725 	int quirks;
726 
727 	nsd = nvme_get_identify_ns(periph);
728 	cd = nvme_get_identify_cntrl(periph);
729 
730 	softc = (struct nda_softc *)malloc(sizeof(*softc), M_DEVBUF,
731 	    M_NOWAIT | M_ZERO);
732 
733 	if (softc == NULL) {
734 		printf("ndaregister: Unable to probe new device. "
735 		    "Unable to allocate softc\n");
736 		return(CAM_REQ_CMP_ERR);
737 	}
738 
739 	if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
740 		printf("ndaregister: Unable to probe new device. "
741 		       "Unable to allocate iosched memory\n");
742 		free(softc, M_DEVBUF);
743 		return(CAM_REQ_CMP_ERR);
744 	}
745 
746 	/* ident_data parsing */
747 
748 	periph->softc = softc;
749 
750 	softc->quirks = NDA_Q_NONE;
751 
752 	xpt_path_inq(&cpi, periph->path);
753 
754 	TASK_INIT(&softc->sysctl_task, 0, ndasysctlinit, periph);
755 
756 	/*
757 	 * The name space ID is the lun, save it for later I/O
758 	 */
759 	softc->nsid = (uint32_t)xpt_path_lun_id(periph->path);
760 
761 	/*
762 	 * Register this media as a disk
763 	 */
764 	(void)cam_periph_hold(periph, PRIBIO);
765 	cam_periph_unlock(periph);
766 	snprintf(announce_buf, sizeof(announce_buf),
767 	    "kern.cam.nda.%d.quirks", periph->unit_number);
768 	quirks = softc->quirks;
769 	TUNABLE_INT_FETCH(announce_buf, &quirks);
770 	softc->quirks = quirks;
771 	cam_iosched_set_sort_queue(softc->cam_iosched, 0);
772 	softc->disk = disk = disk_alloc();
773 	strlcpy(softc->disk->d_descr, cd->mn,
774 	    MIN(sizeof(softc->disk->d_descr), sizeof(cd->mn)));
775 	strlcpy(softc->disk->d_ident, cd->sn,
776 	    MIN(sizeof(softc->disk->d_ident), sizeof(cd->sn)));
777 	disk->d_rotation_rate = DISK_RR_NON_ROTATING;
778 	disk->d_open = ndaopen;
779 	disk->d_close = ndaclose;
780 	disk->d_strategy = ndastrategy;
781 	disk->d_getattr = ndagetattr;
782 	disk->d_dump = ndadump;
783 	disk->d_gone = ndadiskgonecb;
784 	disk->d_name = "nda";
785 	disk->d_drv1 = periph;
786 	disk->d_unit = periph->unit_number;
787 	maxio = cpi.maxio;		/* Honor max I/O size of SIM */
788 	if (maxio == 0)
789 		maxio = DFLTPHYS;	/* traditional default */
790 	else if (maxio > MAXPHYS)
791 		maxio = MAXPHYS;	/* for safety */
792 	disk->d_maxsize = maxio;
793 	flbas_fmt = (nsd->flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) &
794 		NVME_NS_DATA_FLBAS_FORMAT_MASK;
795 	lbads = (nsd->lbaf[flbas_fmt] >> NVME_NS_DATA_LBAF_LBADS_SHIFT) &
796 		NVME_NS_DATA_LBAF_LBADS_MASK;
797 	disk->d_sectorsize = 1 << lbads;
798 	disk->d_mediasize = (off_t)(disk->d_sectorsize * nsd->nsze);
799 	disk->d_delmaxsize = disk->d_mediasize;
800 	disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
801 	if (nvme_ctrlr_has_dataset_mgmt(cd))
802 		disk->d_flags |= DISKFLAG_CANDELETE;
803 	vwc_present = (cd->vwc >> NVME_CTRLR_DATA_VWC_PRESENT_SHIFT) &
804 		NVME_CTRLR_DATA_VWC_PRESENT_MASK;
805 	if (vwc_present)
806 		disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
807 	if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
808 		disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
809 		softc->unmappedio = 1;
810 	}
811 	/*
812 	 * d_ident and d_descr are both far bigger than the length of either
813 	 *  the serial or model number strings.
814 	 */
815 	nvme_strvis(disk->d_descr, cd->mn,
816 	    sizeof(disk->d_descr), NVME_MODEL_NUMBER_LENGTH);
817 	nvme_strvis(disk->d_ident, cd->sn,
818 	    sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH);
819 	disk->d_hba_vendor = cpi.hba_vendor;
820 	disk->d_hba_device = cpi.hba_device;
821 	disk->d_hba_subvendor = cpi.hba_subvendor;
822 	disk->d_hba_subdevice = cpi.hba_subdevice;
823 	disk->d_stripesize = disk->d_sectorsize;
824 	disk->d_stripeoffset = 0;
825 	disk->d_devstat = devstat_new_entry(periph->periph_name,
826 	    periph->unit_number, disk->d_sectorsize,
827 	    DEVSTAT_ALL_SUPPORTED,
828 	    DEVSTAT_TYPE_DIRECT | XPORT_DEVSTAT_TYPE(cpi.transport),
829 	    DEVSTAT_PRIORITY_DISK);
830 	/*
831 	 * Add alias for older nvd drives to ease transition.
832 	 */
833 	/* disk_add_alias(disk, "nvd"); Have reports of this causing problems */
834 
835 	/*
836 	 * Acquire a reference to the periph before we register with GEOM.
837 	 * We'll release this reference once GEOM calls us back (via
838 	 * ndadiskgonecb()) telling us that our provider has been freed.
839 	 */
840 	if (cam_periph_acquire(periph) != 0) {
841 		xpt_print(periph->path, "%s: lost periph during "
842 			  "registration!\n", __func__);
843 		cam_periph_lock(periph);
844 		return (CAM_REQ_CMP_ERR);
845 	}
846 	disk_create(softc->disk, DISK_VERSION);
847 	cam_periph_lock(periph);
848 	cam_periph_unhold(periph);
849 
850 	snprintf(announce_buf, sizeof(announce_buf),
851 		"%juMB (%ju %u byte sectors)",
852 	    (uintmax_t)((uintmax_t)disk->d_mediasize / (1024*1024)),
853 		(uintmax_t)disk->d_mediasize / disk->d_sectorsize,
854 		disk->d_sectorsize);
855 	xpt_announce_periph(periph, announce_buf);
856 	xpt_announce_quirks(periph, softc->quirks, NDA_Q_BIT_STRING);
857 
858 	/*
859 	 * Create our sysctl variables, now that we know
860 	 * we have successfully attached.
861 	 */
862 	if (cam_periph_acquire(periph) == 0)
863 		taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
864 
865 	/*
866 	 * Register for device going away and info about the drive
867 	 * changing (though with NVMe, it can't)
868 	 */
869 	xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
870 	    ndaasync, periph, periph->path);
871 
872 	softc->state = NDA_STATE_NORMAL;
873 	return(CAM_REQ_CMP);
874 }
875 
876 static void
877 ndastart(struct cam_periph *periph, union ccb *start_ccb)
878 {
879 	struct nda_softc *softc = (struct nda_softc *)periph->softc;
880 	struct ccb_nvmeio *nvmeio = &start_ccb->nvmeio;
881 
882 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart\n"));
883 
884 	switch (softc->state) {
885 	case NDA_STATE_NORMAL:
886 	{
887 		struct bio *bp;
888 
889 		bp = cam_iosched_next_bio(softc->cam_iosched);
890 		CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart: bio %p\n", bp));
891 		if (bp == NULL) {
892 			xpt_release_ccb(start_ccb);
893 			break;
894 		}
895 
896 		switch (bp->bio_cmd) {
897 		case BIO_WRITE:
898 			softc->flags |= NDA_FLAG_DIRTY;
899 			/* FALLTHROUGH */
900 		case BIO_READ:
901 		{
902 #ifdef CAM_TEST_FAILURE
903 			int fail = 0;
904 
905 			/*
906 			 * Support the failure ioctls.  If the command is a
907 			 * read, and there are pending forced read errors, or
908 			 * if a write and pending write errors, then fail this
909 			 * operation with EIO.  This is useful for testing
910 			 * purposes.  Also, support having every Nth read fail.
911 			 *
912 			 * This is a rather blunt tool.
913 			 */
914 			if (bp->bio_cmd == BIO_READ) {
915 				if (softc->force_read_error) {
916 					softc->force_read_error--;
917 					fail = 1;
918 				}
919 				if (softc->periodic_read_error > 0) {
920 					if (++softc->periodic_read_count >=
921 					    softc->periodic_read_error) {
922 						softc->periodic_read_count = 0;
923 						fail = 1;
924 					}
925 				}
926 			} else {
927 				if (softc->force_write_error) {
928 					softc->force_write_error--;
929 					fail = 1;
930 				}
931 			}
932 			if (fail) {
933 				biofinish(bp, NULL, EIO);
934 				xpt_release_ccb(start_ccb);
935 				ndaschedule(periph);
936 				return;
937 			}
938 #endif
939 			KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
940 			    round_page(bp->bio_bcount + bp->bio_ma_offset) /
941 			    PAGE_SIZE == bp->bio_ma_n,
942 			    ("Short bio %p", bp));
943 			nda_nvme_rw_bio(softc, &start_ccb->nvmeio, bp, bp->bio_cmd == BIO_READ ?
944 			    NVME_OPC_READ : NVME_OPC_WRITE);
945 			break;
946 		}
947 		case BIO_DELETE:
948 		{
949 			struct nvme_dsm_range *dsm_range, *dsm_end;
950 			struct nda_trim_request *trim;
951 			struct bio *bp1;
952 			int ents;
953 			uint32_t totalcount = 0, ranges = 0;
954 
955 			trim = malloc(sizeof(*trim), M_NVMEDA, M_ZERO | M_NOWAIT);
956 			if (trim == NULL) {
957 				biofinish(bp, NULL, ENOMEM);
958 				xpt_release_ccb(start_ccb);
959 				ndaschedule(periph);
960 				return;
961 			}
962 			TAILQ_INIT(&trim->bps);
963 			bp1 = bp;
964 			ents = sizeof(trim->data) / sizeof(struct nvme_dsm_range);
965 			ents = min(ents, nda_max_trim_entries);
966 			dsm_range = &trim->dsm;
967 			dsm_end = dsm_range + ents;
968 			do {
969 				TAILQ_INSERT_TAIL(&trim->bps, bp1, bio_queue);
970 				dsm_range->length =
971 				    htole32(bp1->bio_bcount / softc->disk->d_sectorsize);
972 				dsm_range->starting_lba =
973 				    htole64(bp1->bio_offset / softc->disk->d_sectorsize);
974 				ranges++;
975 				totalcount += dsm_range->length;
976 				dsm_range++;
977 				if (dsm_range >= dsm_end)
978 					break;
979 				bp1 = cam_iosched_next_trim(softc->cam_iosched);
980 				/* XXX -- Could collapse adjacent ranges, but we don't for now */
981 				/* XXX -- Could limit based on total payload size */
982 			} while (bp1 != NULL);
983 			start_ccb->ccb_trim = trim;
984 			nda_nvme_trim(softc, &start_ccb->nvmeio, &trim->dsm,
985 			    dsm_range - &trim->dsm);
986 			start_ccb->ccb_state = NDA_CCB_TRIM;
987 			softc->trim_count++;
988 			softc->trim_ranges += ranges;
989 			softc->trim_lbas += totalcount;
990 			/*
991 			 * Note: We can have multiple TRIMs in flight, so we don't call
992 			 * cam_iosched_submit_trim(softc->cam_iosched);
993 			 * since that forces the I/O scheduler to only schedule one at a time.
994 			 * On NVMe drives, this is a performance disaster.
995 			 */
996 			goto out;
997 		}
998 		case BIO_FLUSH:
999 			nda_nvme_flush(softc, nvmeio);
1000 			break;
1001 		}
1002 		start_ccb->ccb_state = NDA_CCB_BUFFER_IO;
1003 		start_ccb->ccb_bp = bp;
1004 out:
1005 		start_ccb->ccb_h.flags |= CAM_UNLOCKED;
1006 		softc->outstanding_cmds++;
1007 		softc->refcount++;			/* For submission only */
1008 		cam_periph_unlock(periph);
1009 		xpt_action(start_ccb);
1010 		cam_periph_lock(periph);
1011 		softc->refcount--;			/* Submission done */
1012 
1013 		/* May have more work to do, so ensure we stay scheduled */
1014 		ndaschedule(periph);
1015 		break;
1016 		}
1017 	}
1018 }
1019 
1020 static void
1021 ndadone(struct cam_periph *periph, union ccb *done_ccb)
1022 {
1023 	struct nda_softc *softc;
1024 	struct ccb_nvmeio *nvmeio = &done_ccb->nvmeio;
1025 	struct cam_path *path;
1026 	int state;
1027 
1028 	softc = (struct nda_softc *)periph->softc;
1029 	path = done_ccb->ccb_h.path;
1030 
1031 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("ndadone\n"));
1032 
1033 	state = nvmeio->ccb_state & NDA_CCB_TYPE_MASK;
1034 	switch (state) {
1035 	case NDA_CCB_BUFFER_IO:
1036 	case NDA_CCB_TRIM:
1037 	{
1038 		int error;
1039 
1040 		cam_periph_lock(periph);
1041 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1042 			error = ndaerror(done_ccb, 0, 0);
1043 			if (error == ERESTART) {
1044 				/* A retry was scheduled, so just return. */
1045 				cam_periph_unlock(periph);
1046 				return;
1047 			}
1048 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1049 				cam_release_devq(path,
1050 						 /*relsim_flags*/0,
1051 						 /*reduction*/0,
1052 						 /*timeout*/0,
1053 						 /*getcount_only*/0);
1054 		} else {
1055 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1056 				panic("REQ_CMP with QFRZN");
1057 			error = 0;
1058 		}
1059 		if (state == NDA_CCB_BUFFER_IO) {
1060 			struct bio *bp;
1061 
1062 			bp = (struct bio *)done_ccb->ccb_bp;
1063 			bp->bio_error = error;
1064 			if (error != 0) {
1065 				bp->bio_resid = bp->bio_bcount;
1066 				bp->bio_flags |= BIO_ERROR;
1067 			} else {
1068 				bp->bio_resid = 0;
1069 			}
1070 			softc->outstanding_cmds--;
1071 
1072 			/*
1073 			 * We need to call cam_iosched before we call biodone so that we
1074 			 * don't measure any activity that happens in the completion
1075 			 * routine, which in the case of sendfile can be quite
1076 			 * extensive.
1077 			 */
1078 			cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
1079 			xpt_release_ccb(done_ccb);
1080 			ndaschedule(periph);
1081 			cam_periph_unlock(periph);
1082 			biodone(bp);
1083 		} else { /* state == NDA_CCB_TRIM */
1084 			struct nda_trim_request *trim;
1085 			struct bio *bp1, *bp2;
1086 			TAILQ_HEAD(, bio) queue;
1087 
1088 			trim = nvmeio->ccb_trim;
1089 			TAILQ_INIT(&queue);
1090 			TAILQ_CONCAT(&queue, &trim->bps, bio_queue);
1091 			free(trim, M_NVMEDA);
1092 
1093 			/*
1094 			 * Since we can have multiple trims in flight, we don't
1095 			 * need to call this here.
1096 			 * cam_iosched_trim_done(softc->cam_iosched);
1097 			 */
1098 			/*
1099 			 * The the I/O scheduler that we're finishing the I/O
1100 			 * so we can keep book. The first one we pass in the CCB
1101 			 * which has the timing information. The rest we pass in NULL
1102 			 * so we can keep proper counts.
1103 			 */
1104 			bp1 = TAILQ_FIRST(&queue);
1105 			cam_iosched_bio_complete(softc->cam_iosched, bp1, done_ccb);
1106 			xpt_release_ccb(done_ccb);
1107 			softc->outstanding_cmds--;
1108 			ndaschedule(periph);
1109 			cam_periph_unlock(periph);
1110 			while ((bp2 = TAILQ_FIRST(&queue)) != NULL) {
1111 				TAILQ_REMOVE(&queue, bp2, bio_queue);
1112 				bp2->bio_error = error;
1113 				if (error != 0) {
1114 					bp2->bio_flags |= BIO_ERROR;
1115 					bp2->bio_resid = bp1->bio_bcount;
1116 				} else
1117 					bp2->bio_resid = 0;
1118 				if (bp1 != bp2)
1119 					cam_iosched_bio_complete(softc->cam_iosched, bp2, NULL);
1120 				biodone(bp2);
1121 			}
1122 		}
1123 		return;
1124 	}
1125 	case NDA_CCB_DUMP:
1126 		/* No-op.  We're polling */
1127 		return;
1128 	default:
1129 		break;
1130 	}
1131 	xpt_release_ccb(done_ccb);
1132 }
1133 
1134 static int
1135 ndaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1136 {
1137 	struct nda_softc *softc;
1138 	struct cam_periph *periph;
1139 
1140 	periph = xpt_path_periph(ccb->ccb_h.path);
1141 	softc = (struct nda_softc *)periph->softc;
1142 
1143 	switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1144 	case CAM_CMD_TIMEOUT:
1145 #ifdef CAM_IO_STATS
1146 		softc->timeouts++;
1147 #endif
1148 		break;
1149 	case CAM_REQ_ABORTED:
1150 	case CAM_REQ_CMP_ERR:
1151 	case CAM_REQ_TERMIO:
1152 	case CAM_UNREC_HBA_ERROR:
1153 	case CAM_DATA_RUN_ERR:
1154 	case CAM_ATA_STATUS_ERROR:
1155 #ifdef CAM_IO_STATS
1156 		softc->errors++;
1157 #endif
1158 		break;
1159 	default:
1160 		break;
1161 	}
1162 
1163 	return(cam_periph_error(ccb, cam_flags, sense_flags));
1164 }
1165 
1166 /*
1167  * Step through all NDA peripheral drivers, and if the device is still open,
1168  * sync the disk cache to physical media.
1169  */
1170 static void
1171 ndaflush(void)
1172 {
1173 	struct cam_periph *periph;
1174 	struct nda_softc *softc;
1175 	union ccb *ccb;
1176 	int error;
1177 
1178 	CAM_PERIPH_FOREACH(periph, &ndadriver) {
1179 		softc = (struct nda_softc *)periph->softc;
1180 
1181 		if (SCHEDULER_STOPPED()) {
1182 			/*
1183 			 * If we paniced with the lock held or the periph is not
1184 			 * open, do not recurse.  Otherwise, call ndadump since
1185 			 * that avoids the sleeping cam_periph_getccb does if no
1186 			 * CCBs are available.
1187 			 */
1188 			if (!cam_periph_owned(periph) &&
1189 			    (softc->flags & NDA_FLAG_OPEN)) {
1190 				ndadump(softc->disk, NULL, 0, 0, 0);
1191 			}
1192 			continue;
1193 		}
1194 
1195 		/*
1196 		 * We only sync the cache if the drive is still open
1197 		 */
1198 		cam_periph_lock(periph);
1199 		if ((softc->flags & NDA_FLAG_OPEN) == 0) {
1200 			cam_periph_unlock(periph);
1201 			continue;
1202 		}
1203 
1204 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1205 		nda_nvme_flush(softc, &ccb->nvmeio);
1206 		error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0,
1207 		    /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1208 		    softc->disk->d_devstat);
1209 		if (error != 0)
1210 			xpt_print(periph->path, "Synchronize cache failed\n");
1211 		xpt_release_ccb(ccb);
1212 		cam_periph_unlock(periph);
1213 	}
1214 }
1215 
1216 static void
1217 ndashutdown(void *arg, int howto)
1218 {
1219 
1220 	ndaflush();
1221 }
1222 
1223 static void
1224 ndasuspend(void *arg)
1225 {
1226 
1227 	ndaflush();
1228 }
1229