xref: /freebsd/sys/dev/nvme/nvme_ns.c (revision 1bce7cd885e7e5b376a60367629a0f76ff7f0167)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2012-2013 Intel Corporation
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/bio.h>
31 #include <sys/bus.h>
32 #include <sys/conf.h>
33 #include <sys/disk.h>
34 #include <sys/fcntl.h>
35 #include <sys/ioccom.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/proc.h>
39 #include <sys/systm.h>
40 
41 #include <dev/pci/pcivar.h>
42 
43 #include <geom/geom.h>
44 
45 #include "nvme_private.h"
46 #include "nvme_linux.h"
47 
48 static void		nvme_bio_child_inbed(struct bio *parent, int bio_error);
49 static void		nvme_bio_child_done(void *arg,
50 					    const struct nvme_completion *cpl);
51 static uint32_t		nvme_get_num_segments(uint64_t addr, uint64_t size,
52 					      uint32_t alignment);
53 static void		nvme_free_child_bios(int num_bios,
54 					     struct bio **child_bios);
55 static struct bio **	nvme_allocate_child_bios(int num_bios);
56 static struct bio **	nvme_construct_child_bios(struct bio *bp,
57 						  uint32_t alignment,
58 						  int *num_bios);
59 static int		nvme_ns_split_bio(struct nvme_namespace *ns,
60 					  struct bio *bp,
61 					  uint32_t alignment);
62 
63 static int
nvme_ns_ioctl(struct cdev * cdev,u_long cmd,caddr_t arg,int flag,struct thread * td)64 nvme_ns_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag,
65     struct thread *td)
66 {
67 	struct nvme_namespace			*ns;
68 	struct nvme_controller			*ctrlr;
69 	struct nvme_pt_command			*pt;
70 
71 	ns = cdev->si_drv1;
72 	ctrlr = ns->ctrlr;
73 
74 	switch (cmd) {
75 	case NVME_IO_TEST:
76 	case NVME_BIO_TEST:
77 		nvme_ns_test(ns, cmd, arg);
78 		break;
79 	case NVME_PASSTHROUGH_CMD:
80 		pt = (struct nvme_pt_command *)arg;
81 		return (nvme_ctrlr_passthrough_cmd(ctrlr, pt, ns->id,
82 		    1 /* is_user_buffer */, 0 /* is_admin_cmd */));
83 	case NVME_GET_NSID:
84 	{
85 		struct nvme_get_nsid *gnsid = (struct nvme_get_nsid *)arg;
86 		strlcpy(gnsid->cdev, device_get_nameunit(ctrlr->dev),
87 		    sizeof(gnsid->cdev));
88 		gnsid->nsid = ns->id;
89 		break;
90 	}
91 	case DIOCGMEDIASIZE:
92 		*(off_t *)arg = (off_t)nvme_ns_get_size(ns);
93 		break;
94 	case DIOCGSECTORSIZE:
95 		*(u_int *)arg = nvme_ns_get_sector_size(ns);
96 		break;
97 	/* Linux Compatible (see nvme_linux.h) */
98 	case NVME_IOCTL_ID:
99 		td->td_retval[0] = ns->id;
100 		return (0);
101 
102 	case NVME_IOCTL_ADMIN_CMD:
103 	case NVME_IOCTL_IO_CMD: {
104 		struct nvme_passthru_cmd *npc = (struct nvme_passthru_cmd *)arg;
105 
106 		return (nvme_ctrlr_linux_passthru_cmd(ctrlr, npc, ns->id, true,
107 		    cmd == NVME_IOCTL_ADMIN_CMD));
108 	}
109 	default:
110 		return (ENOTTY);
111 	}
112 
113 	return (0);
114 }
115 
116 static int
nvme_ns_open(struct cdev * dev __unused,int flags,int fmt __unused,struct thread * td)117 nvme_ns_open(struct cdev *dev __unused, int flags, int fmt __unused,
118     struct thread *td)
119 {
120 	int error = 0;
121 
122 	if (flags & FWRITE)
123 		error = securelevel_gt(td->td_ucred, 0);
124 
125 	return (error);
126 }
127 
128 static int
nvme_ns_close(struct cdev * dev __unused,int flags,int fmt __unused,struct thread * td)129 nvme_ns_close(struct cdev *dev __unused, int flags, int fmt __unused,
130     struct thread *td)
131 {
132 
133 	return (0);
134 }
135 
136 static void
nvme_ns_strategy_done(void * arg,const struct nvme_completion * cpl)137 nvme_ns_strategy_done(void *arg, const struct nvme_completion *cpl)
138 {
139 	struct bio *bp = arg;
140 
141 	/*
142 	 * TODO: add more extensive translation of NVMe status codes
143 	 *  to different bio error codes (i.e. EIO, EINVAL, etc.)
144 	 */
145 	if (nvme_completion_is_error(cpl)) {
146 		bp->bio_error = EIO;
147 		bp->bio_flags |= BIO_ERROR;
148 		bp->bio_resid = bp->bio_bcount;
149 	} else
150 		bp->bio_resid = 0;
151 
152 	biodone(bp);
153 }
154 
155 static void
nvme_ns_strategy(struct bio * bp)156 nvme_ns_strategy(struct bio *bp)
157 {
158 	struct nvme_namespace	*ns;
159 	int			err;
160 
161 	ns = bp->bio_dev->si_drv1;
162 	err = nvme_ns_bio_process(ns, bp, nvme_ns_strategy_done);
163 
164 	if (err) {
165 		bp->bio_error = err;
166 		bp->bio_flags |= BIO_ERROR;
167 		bp->bio_resid = bp->bio_bcount;
168 		biodone(bp);
169 	}
170 
171 }
172 
173 static struct cdevsw nvme_ns_cdevsw = {
174 	.d_version =	D_VERSION,
175 	.d_flags =	D_DISK,
176 	.d_read =	physread,
177 	.d_write =	physwrite,
178 	.d_open =	nvme_ns_open,
179 	.d_close =	nvme_ns_close,
180 	.d_strategy =	nvme_ns_strategy,
181 	.d_ioctl =	nvme_ns_ioctl
182 };
183 
184 uint32_t
nvme_ns_get_max_io_xfer_size(struct nvme_namespace * ns)185 nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns)
186 {
187 	return ns->ctrlr->max_xfer_size;
188 }
189 
190 uint32_t
nvme_ns_get_sector_size(struct nvme_namespace * ns)191 nvme_ns_get_sector_size(struct nvme_namespace *ns)
192 {
193 	uint8_t flbas_fmt, lbads;
194 
195 	flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, ns->data.flbas);
196 	lbads = NVMEV(NVME_NS_DATA_LBAF_LBADS, ns->data.lbaf[flbas_fmt]);
197 
198 	return (1 << lbads);
199 }
200 
201 uint64_t
nvme_ns_get_num_sectors(struct nvme_namespace * ns)202 nvme_ns_get_num_sectors(struct nvme_namespace *ns)
203 {
204 	return (ns->data.nsze);
205 }
206 
207 uint64_t
nvme_ns_get_size(struct nvme_namespace * ns)208 nvme_ns_get_size(struct nvme_namespace *ns)
209 {
210 	return (nvme_ns_get_num_sectors(ns) * nvme_ns_get_sector_size(ns));
211 }
212 
213 uint32_t
nvme_ns_get_flags(struct nvme_namespace * ns)214 nvme_ns_get_flags(struct nvme_namespace *ns)
215 {
216 	return (ns->flags);
217 }
218 
219 const char *
nvme_ns_get_serial_number(struct nvme_namespace * ns)220 nvme_ns_get_serial_number(struct nvme_namespace *ns)
221 {
222 	return ((const char *)ns->ctrlr->cdata.sn);
223 }
224 
225 const char *
nvme_ns_get_model_number(struct nvme_namespace * ns)226 nvme_ns_get_model_number(struct nvme_namespace *ns)
227 {
228 	return ((const char *)ns->ctrlr->cdata.mn);
229 }
230 
231 const struct nvme_namespace_data *
nvme_ns_get_data(struct nvme_namespace * ns)232 nvme_ns_get_data(struct nvme_namespace *ns)
233 {
234 
235 	return (&ns->data);
236 }
237 
238 uint32_t
nvme_ns_get_stripesize(struct nvme_namespace * ns)239 nvme_ns_get_stripesize(struct nvme_namespace *ns)
240 {
241 	uint32_t ss;
242 
243 	if (NVMEV(NVME_NS_DATA_NSFEAT_NPVALID, ns->data.nsfeat) != 0) {
244 		ss = nvme_ns_get_sector_size(ns);
245 		if (ns->data.npwa != 0)
246 			return ((ns->data.npwa + 1) * ss);
247 		else if (ns->data.npwg != 0)
248 			return ((ns->data.npwg + 1) * ss);
249 	}
250 	return (ns->boundary);
251 }
252 
253 static void
nvme_ns_bio_done(void * arg,const struct nvme_completion * status)254 nvme_ns_bio_done(void *arg, const struct nvme_completion *status)
255 {
256 	struct bio	*bp = arg;
257 	nvme_cb_fn_t	bp_cb_fn;
258 
259 	bp_cb_fn = bp->bio_driver1;
260 
261 	if (bp->bio_driver2)
262 		free(bp->bio_driver2, M_NVME);
263 
264 	if (nvme_completion_is_error(status)) {
265 		bp->bio_flags |= BIO_ERROR;
266 		if (bp->bio_error == 0)
267 			bp->bio_error = EIO;
268 	}
269 
270 	if ((bp->bio_flags & BIO_ERROR) == 0)
271 		bp->bio_resid = 0;
272 	else
273 		bp->bio_resid = bp->bio_bcount;
274 
275 	bp_cb_fn(bp, status);
276 }
277 
278 static void
nvme_bio_child_inbed(struct bio * parent,int bio_error)279 nvme_bio_child_inbed(struct bio *parent, int bio_error)
280 {
281 	struct nvme_completion	parent_cpl;
282 	int			children, inbed;
283 
284 	if (bio_error != 0) {
285 		parent->bio_flags |= BIO_ERROR;
286 		parent->bio_error = bio_error;
287 	}
288 
289 	/*
290 	 * atomic_fetchadd will return value before adding 1, so we still
291 	 *  must add 1 to get the updated inbed number.  Save bio_children
292 	 *  before incrementing to guard against race conditions when
293 	 *  two children bios complete on different queues.
294 	 */
295 	children = atomic_load_acq_int(&parent->bio_children);
296 	inbed = atomic_fetchadd_int(&parent->bio_inbed, 1) + 1;
297 	if (inbed == children) {
298 		bzero(&parent_cpl, sizeof(parent_cpl));
299 		if (parent->bio_flags & BIO_ERROR) {
300 			parent_cpl.status &= ~NVMEM(NVME_STATUS_SC);
301 			parent_cpl.status |= NVMEF(NVME_STATUS_SC,
302 			    NVME_SC_DATA_TRANSFER_ERROR);
303 		}
304 		nvme_ns_bio_done(parent, &parent_cpl);
305 	}
306 }
307 
308 static void
nvme_bio_child_done(void * arg,const struct nvme_completion * cpl)309 nvme_bio_child_done(void *arg, const struct nvme_completion *cpl)
310 {
311 	struct bio		*child = arg;
312 	struct bio		*parent;
313 	int			bio_error;
314 
315 	parent = child->bio_parent;
316 	g_destroy_bio(child);
317 	bio_error = nvme_completion_is_error(cpl) ? EIO : 0;
318 	nvme_bio_child_inbed(parent, bio_error);
319 }
320 
321 static uint32_t
nvme_get_num_segments(uint64_t addr,uint64_t size,uint32_t align)322 nvme_get_num_segments(uint64_t addr, uint64_t size, uint32_t align)
323 {
324 	uint32_t	num_segs, offset, remainder;
325 
326 	if (align == 0)
327 		return (1);
328 
329 	KASSERT((align & (align - 1)) == 0, ("alignment not power of 2\n"));
330 
331 	num_segs = size / align;
332 	remainder = size & (align - 1);
333 	offset = addr & (align - 1);
334 	if (remainder > 0 || offset > 0)
335 		num_segs += 1 + (remainder + offset - 1) / align;
336 	return (num_segs);
337 }
338 
339 static void
nvme_free_child_bios(int num_bios,struct bio ** child_bios)340 nvme_free_child_bios(int num_bios, struct bio **child_bios)
341 {
342 	int i;
343 
344 	for (i = 0; i < num_bios; i++) {
345 		if (child_bios[i] != NULL)
346 			g_destroy_bio(child_bios[i]);
347 	}
348 
349 	free(child_bios, M_NVME);
350 }
351 
352 static struct bio **
nvme_allocate_child_bios(int num_bios)353 nvme_allocate_child_bios(int num_bios)
354 {
355 	struct bio **child_bios;
356 	int err = 0, i;
357 
358 	child_bios = malloc(num_bios * sizeof(struct bio *), M_NVME, M_NOWAIT);
359 	if (child_bios == NULL)
360 		return (NULL);
361 
362 	for (i = 0; i < num_bios; i++) {
363 		child_bios[i] = g_new_bio();
364 		if (child_bios[i] == NULL)
365 			err = ENOMEM;
366 	}
367 
368 	if (err == ENOMEM) {
369 		nvme_free_child_bios(num_bios, child_bios);
370 		return (NULL);
371 	}
372 
373 	return (child_bios);
374 }
375 
376 static struct bio **
nvme_construct_child_bios(struct bio * bp,uint32_t alignment,int * num_bios)377 nvme_construct_child_bios(struct bio *bp, uint32_t alignment, int *num_bios)
378 {
379 	struct bio	**child_bios;
380 	struct bio	*child;
381 	uint64_t	cur_offset;
382 	caddr_t		data;
383 	uint32_t	rem_bcount;
384 	int		i;
385 	struct vm_page	**ma;
386 	uint32_t	ma_offset;
387 
388 	*num_bios = nvme_get_num_segments(bp->bio_offset, bp->bio_bcount,
389 	    alignment);
390 	child_bios = nvme_allocate_child_bios(*num_bios);
391 	if (child_bios == NULL)
392 		return (NULL);
393 
394 	bp->bio_children = *num_bios;
395 	bp->bio_inbed = 0;
396 	cur_offset = bp->bio_offset;
397 	rem_bcount = bp->bio_bcount;
398 	data = bp->bio_data;
399 	ma_offset = bp->bio_ma_offset;
400 	ma = bp->bio_ma;
401 
402 	for (i = 0; i < *num_bios; i++) {
403 		child = child_bios[i];
404 		child->bio_parent = bp;
405 		child->bio_cmd = bp->bio_cmd;
406 		child->bio_offset = cur_offset;
407 		child->bio_bcount = min(rem_bcount,
408 		    alignment - (cur_offset & (alignment - 1)));
409 		child->bio_flags = bp->bio_flags;
410 		if (bp->bio_flags & BIO_UNMAPPED) {
411 			child->bio_ma_offset = ma_offset;
412 			child->bio_ma = ma;
413 			child->bio_ma_n =
414 			    nvme_get_num_segments(child->bio_ma_offset,
415 				child->bio_bcount, PAGE_SIZE);
416 			ma_offset = (ma_offset + child->bio_bcount) &
417 			    PAGE_MASK;
418 			ma += child->bio_ma_n;
419 			if (ma_offset != 0)
420 				ma -= 1;
421 		} else {
422 			child->bio_data = data;
423 			data += child->bio_bcount;
424 		}
425 		cur_offset += child->bio_bcount;
426 		rem_bcount -= child->bio_bcount;
427 	}
428 
429 	return (child_bios);
430 }
431 
432 static int
nvme_ns_split_bio(struct nvme_namespace * ns,struct bio * bp,uint32_t alignment)433 nvme_ns_split_bio(struct nvme_namespace *ns, struct bio *bp,
434     uint32_t alignment)
435 {
436 	struct bio	*child;
437 	struct bio	**child_bios;
438 	int		err, i, num_bios;
439 
440 	child_bios = nvme_construct_child_bios(bp, alignment, &num_bios);
441 	if (child_bios == NULL)
442 		return (ENOMEM);
443 
444 	counter_u64_add(ns->ctrlr->alignment_splits, 1);
445 	for (i = 0; i < num_bios; i++) {
446 		child = child_bios[i];
447 		err = nvme_ns_bio_process(ns, child, nvme_bio_child_done);
448 		if (err != 0) {
449 			nvme_bio_child_inbed(bp, err);
450 			g_destroy_bio(child);
451 		}
452 	}
453 
454 	free(child_bios, M_NVME);
455 	return (0);
456 }
457 
458 int
nvme_ns_bio_process(struct nvme_namespace * ns,struct bio * bp,nvme_cb_fn_t cb_fn)459 nvme_ns_bio_process(struct nvme_namespace *ns, struct bio *bp,
460 	nvme_cb_fn_t cb_fn)
461 {
462 	struct nvme_dsm_range	*dsm_range;
463 	uint32_t		num_bios;
464 	int			err;
465 
466 	bp->bio_driver1 = cb_fn;
467 
468 	if (ns->boundary > 0 &&
469 	    (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
470 		num_bios = nvme_get_num_segments(bp->bio_offset,
471 		    bp->bio_bcount, ns->boundary);
472 		if (num_bios > 1)
473 			return (nvme_ns_split_bio(ns, bp, ns->boundary));
474 	}
475 
476 	switch (bp->bio_cmd) {
477 	case BIO_READ:
478 		err = nvme_ns_cmd_read_bio(ns, bp, nvme_ns_bio_done, bp);
479 		break;
480 	case BIO_WRITE:
481 		err = nvme_ns_cmd_write_bio(ns, bp, nvme_ns_bio_done, bp);
482 		break;
483 	case BIO_FLUSH:
484 		err = nvme_ns_cmd_flush(ns, nvme_ns_bio_done, bp);
485 		break;
486 	case BIO_DELETE:
487 		dsm_range =
488 		    malloc(sizeof(struct nvme_dsm_range), M_NVME,
489 		    M_ZERO | M_NOWAIT);
490 		if (!dsm_range) {
491 			err = ENOMEM;
492 			break;
493 		}
494 		dsm_range->length =
495 		    htole32(bp->bio_bcount/nvme_ns_get_sector_size(ns));
496 		dsm_range->starting_lba =
497 		    htole64(bp->bio_offset/nvme_ns_get_sector_size(ns));
498 		bp->bio_driver2 = dsm_range;
499 		err = nvme_ns_cmd_deallocate(ns, dsm_range, 1,
500 			nvme_ns_bio_done, bp);
501 		if (err != 0)
502 			free(dsm_range, M_NVME);
503 		break;
504 	default:
505 		err = EOPNOTSUPP;
506 		break;
507 	}
508 
509 	return (err);
510 }
511 
512 int
nvme_ns_ioctl_process(struct nvme_namespace * ns,u_long cmd,caddr_t arg,int flag,struct thread * td)513 nvme_ns_ioctl_process(struct nvme_namespace *ns, u_long cmd, caddr_t arg,
514     int flag, struct thread *td)
515 {
516 	return (nvme_ns_ioctl(ns->cdev, cmd, arg, flag, td));
517 }
518 
519 int
nvme_ns_construct(struct nvme_namespace * ns,uint32_t id,struct nvme_controller * ctrlr)520 nvme_ns_construct(struct nvme_namespace *ns, uint32_t id,
521     struct nvme_controller *ctrlr)
522 {
523 	struct make_dev_args                    md_args;
524 	struct nvme_completion_poll_status	status;
525 	int                                     res;
526 	int					unit;
527 	uint8_t					flbas_fmt;
528 	uint8_t					vwc_present;
529 
530 	ns->ctrlr = ctrlr;
531 	ns->id = id;
532 
533 	/*
534 	 * Namespaces are reconstructed after a controller reset, so check
535 	 *  to make sure we only call mtx_init once on each mtx.
536 	 *
537 	 * TODO: Move this somewhere where it gets called at controller
538 	 *  construction time, which is not invoked as part of each
539 	 *  controller reset.
540 	 */
541 	if (!mtx_initialized(&ns->lock))
542 		mtx_init(&ns->lock, "nvme ns lock", NULL, MTX_DEF);
543 
544 	status.done = 0;
545 	nvme_ctrlr_cmd_identify_namespace(ctrlr, id, &ns->data,
546 	    nvme_completion_poll_cb, &status);
547 	nvme_completion_poll(&status);
548 	if (nvme_completion_is_error(&status.cpl)) {
549 		nvme_printf(ctrlr, "nvme_identify_namespace failed\n");
550 		return (ENXIO);
551 	}
552 
553 	/* Convert data to host endian */
554 	nvme_namespace_data_swapbytes(&ns->data);
555 
556 	/*
557 	 * If the size of is zero, chances are this isn't a valid
558 	 * namespace (eg one that's not been configured yet). The
559 	 * standard says the entire id will be zeros, so this is a
560 	 * cheap way to test for that.
561 	 */
562 	if (ns->data.nsze == 0)
563 		return (ENXIO);
564 
565 	flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, ns->data.flbas);
566 
567 	/*
568 	 * Note: format is a 0-based value, so > is appropriate here,
569 	 *  not >=.
570 	 */
571 	if (flbas_fmt > ns->data.nlbaf) {
572 		nvme_printf(ctrlr,
573 		    "lba format %d exceeds number supported (%d)\n",
574 		    flbas_fmt, ns->data.nlbaf + 1);
575 		return (ENXIO);
576 	}
577 
578 	/*
579 	 * Older Intel devices (like the PC35xxx and P45xx series) advertise in
580 	 * vendor specific space an alignment that improves performance.  If
581 	 * present use for the stripe size.  NVMe 1.3 standardized this as
582 	 * NOIOB, and newer Intel drives use that.
583 	 */
584 	if ((ctrlr->quirks & QUIRK_INTEL_ALIGNMENT) != 0) {
585 		if (ctrlr->cdata.vs[3] != 0)
586 			ns->boundary =
587 			    1 << (ctrlr->cdata.vs[3] + NVME_MPS_SHIFT +
588 				NVME_CAP_HI_MPSMIN(ctrlr->cap_hi));
589 		else
590 			ns->boundary = 0;
591 	} else {
592 		ns->boundary = ns->data.noiob * nvme_ns_get_sector_size(ns);
593 	}
594 
595 	if (nvme_ctrlr_has_dataset_mgmt(&ctrlr->cdata))
596 		ns->flags |= NVME_NS_DEALLOCATE_SUPPORTED;
597 
598 	vwc_present = NVMEV(NVME_CTRLR_DATA_VWC_PRESENT, ctrlr->cdata.vwc);
599 	if (vwc_present)
600 		ns->flags |= NVME_NS_FLUSH_SUPPORTED;
601 
602 	/*
603 	 * cdev may have already been created, if we are reconstructing the
604 	 *  namespace after a controller-level reset.
605 	 */
606 	if (ns->cdev != NULL)
607 		return (0);
608 
609 	/*
610 	 * Namespace IDs start at 1, so we need to subtract 1 to create a
611 	 *  correct unit number.
612 	 */
613 	unit = device_get_unit(ctrlr->dev) * NVME_MAX_NAMESPACES + ns->id - 1;
614 
615 	make_dev_args_init(&md_args);
616 	md_args.mda_devsw = &nvme_ns_cdevsw;
617 	md_args.mda_unit = unit;
618 	md_args.mda_mode = 0600;
619 	md_args.mda_si_drv1 = ns;
620 	res = make_dev_s(&md_args, &ns->cdev, "%sn%d",
621 	    device_get_nameunit(ctrlr->dev), ns->id);
622 	if (res != 0)
623 		return (ENXIO);
624 	ns->cdev->si_drv2 = make_dev_alias(ns->cdev, "%sns%d",
625 	    device_get_nameunit(ctrlr->dev), ns->id);
626 	ns->cdev->si_flags |= SI_UNMAPPED;
627 
628 	return (0);
629 }
630 
631 void
nvme_ns_destruct(struct nvme_namespace * ns)632 nvme_ns_destruct(struct nvme_namespace *ns)
633 {
634 
635 	if (ns->cdev != NULL) {
636 		if (ns->cdev->si_drv2 != NULL)
637 			destroy_dev(ns->cdev->si_drv2);
638 		destroy_dev(ns->cdev);
639 	}
640 }
641