xref: /freebsd/sys/dev/nvme/nvme_ns.c (revision eb1145f0c4d4a35f998b9ff115dd54ef761f3bf4)
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 	return (0);
133 }
134 
135 static void
nvme_ns_strategy_done(void * arg,const struct nvme_completion * cpl)136 nvme_ns_strategy_done(void *arg, const struct nvme_completion *cpl)
137 {
138 	struct bio *bp = arg;
139 
140 	/*
141 	 * TODO: add more extensive translation of NVMe status codes
142 	 *  to different bio error codes (i.e. EIO, EINVAL, etc.)
143 	 */
144 	if (nvme_completion_is_error(cpl)) {
145 		bp->bio_error = EIO;
146 		bp->bio_flags |= BIO_ERROR;
147 		bp->bio_resid = bp->bio_bcount;
148 	} else
149 		bp->bio_resid = 0;
150 
151 	biodone(bp);
152 }
153 
154 static void
nvme_ns_strategy(struct bio * bp)155 nvme_ns_strategy(struct bio *bp)
156 {
157 	struct nvme_namespace	*ns;
158 	int			err;
159 
160 	ns = bp->bio_dev->si_drv1;
161 	err = nvme_ns_bio_process(ns, bp, nvme_ns_strategy_done);
162 
163 	if (err) {
164 		bp->bio_error = err;
165 		bp->bio_flags |= BIO_ERROR;
166 		bp->bio_resid = bp->bio_bcount;
167 		biodone(bp);
168 	}
169 
170 }
171 
172 static struct cdevsw nvme_ns_cdevsw = {
173 	.d_version =	D_VERSION,
174 	.d_flags =	D_DISK,
175 	.d_read =	physread,
176 	.d_write =	physwrite,
177 	.d_open =	nvme_ns_open,
178 	.d_close =	nvme_ns_close,
179 	.d_strategy =	nvme_ns_strategy,
180 	.d_ioctl =	nvme_ns_ioctl
181 };
182 
183 uint32_t
nvme_ns_get_max_io_xfer_size(struct nvme_namespace * ns)184 nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns)
185 {
186 	return ns->ctrlr->max_xfer_size;
187 }
188 
189 uint32_t
nvme_ns_get_sector_size(struct nvme_namespace * ns)190 nvme_ns_get_sector_size(struct nvme_namespace *ns)
191 {
192 	uint8_t flbas_fmt, lbads;
193 
194 	flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, ns->data.flbas);
195 	lbads = NVMEV(NVME_NS_DATA_LBAF_LBADS, ns->data.lbaf[flbas_fmt]);
196 
197 	return (1 << lbads);
198 }
199 
200 uint64_t
nvme_ns_get_num_sectors(struct nvme_namespace * ns)201 nvme_ns_get_num_sectors(struct nvme_namespace *ns)
202 {
203 	return (ns->data.nsze);
204 }
205 
206 uint64_t
nvme_ns_get_size(struct nvme_namespace * ns)207 nvme_ns_get_size(struct nvme_namespace *ns)
208 {
209 	return (nvme_ns_get_num_sectors(ns) * nvme_ns_get_sector_size(ns));
210 }
211 
212 uint32_t
nvme_ns_get_flags(struct nvme_namespace * ns)213 nvme_ns_get_flags(struct nvme_namespace *ns)
214 {
215 	return (ns->flags);
216 }
217 
218 const char *
nvme_ns_get_serial_number(struct nvme_namespace * ns)219 nvme_ns_get_serial_number(struct nvme_namespace *ns)
220 {
221 	return ((const char *)ns->ctrlr->cdata.sn);
222 }
223 
224 const char *
nvme_ns_get_model_number(struct nvme_namespace * ns)225 nvme_ns_get_model_number(struct nvme_namespace *ns)
226 {
227 	return ((const char *)ns->ctrlr->cdata.mn);
228 }
229 
230 const struct nvme_namespace_data *
nvme_ns_get_data(struct nvme_namespace * ns)231 nvme_ns_get_data(struct nvme_namespace *ns)
232 {
233 	return (&ns->data);
234 }
235 
236 uint32_t
nvme_ns_get_stripesize(struct nvme_namespace * ns)237 nvme_ns_get_stripesize(struct nvme_namespace *ns)
238 {
239 	uint32_t ss;
240 
241 	if (NVMEV(NVME_NS_DATA_NSFEAT_NPVALID, ns->data.nsfeat) != 0) {
242 		ss = nvme_ns_get_sector_size(ns);
243 		if (ns->data.npwa != 0)
244 			return ((ns->data.npwa + 1) * ss);
245 		else if (ns->data.npwg != 0)
246 			return ((ns->data.npwg + 1) * ss);
247 	}
248 	return (ns->boundary);
249 }
250 
251 static void
nvme_ns_bio_done(void * arg,const struct nvme_completion * status)252 nvme_ns_bio_done(void *arg, const struct nvme_completion *status)
253 {
254 	struct bio	*bp = arg;
255 	nvme_cb_fn_t	bp_cb_fn;
256 
257 	bp_cb_fn = bp->bio_driver1;
258 
259 	if (bp->bio_driver2)
260 		free(bp->bio_driver2, M_NVME);
261 
262 	if (nvme_completion_is_error(status)) {
263 		bp->bio_flags |= BIO_ERROR;
264 		if (bp->bio_error == 0)
265 			bp->bio_error = EIO;
266 	}
267 
268 	if ((bp->bio_flags & BIO_ERROR) == 0)
269 		bp->bio_resid = 0;
270 	else
271 		bp->bio_resid = bp->bio_bcount;
272 
273 	bp_cb_fn(bp, status);
274 }
275 
276 static void
nvme_bio_child_inbed(struct bio * parent,int bio_error)277 nvme_bio_child_inbed(struct bio *parent, int bio_error)
278 {
279 	struct nvme_completion	parent_cpl;
280 	int			children, inbed;
281 
282 	if (bio_error != 0) {
283 		parent->bio_flags |= BIO_ERROR;
284 		parent->bio_error = bio_error;
285 	}
286 
287 	/*
288 	 * atomic_fetchadd will return value before adding 1, so we still
289 	 *  must add 1 to get the updated inbed number.  Save bio_children
290 	 *  before incrementing to guard against race conditions when
291 	 *  two children bios complete on different queues.
292 	 */
293 	children = atomic_load_acq_int(&parent->bio_children);
294 	inbed = atomic_fetchadd_int(&parent->bio_inbed, 1) + 1;
295 	if (inbed == children) {
296 		bzero(&parent_cpl, sizeof(parent_cpl));
297 		if (parent->bio_flags & BIO_ERROR) {
298 			parent_cpl.status &= ~NVMEM(NVME_STATUS_SC);
299 			parent_cpl.status |= NVMEF(NVME_STATUS_SC,
300 			    NVME_SC_DATA_TRANSFER_ERROR);
301 		}
302 		nvme_ns_bio_done(parent, &parent_cpl);
303 	}
304 }
305 
306 static void
nvme_bio_child_done(void * arg,const struct nvme_completion * cpl)307 nvme_bio_child_done(void *arg, const struct nvme_completion *cpl)
308 {
309 	struct bio		*child = arg;
310 	struct bio		*parent;
311 	int			bio_error;
312 
313 	parent = child->bio_parent;
314 	g_destroy_bio(child);
315 	bio_error = nvme_completion_is_error(cpl) ? EIO : 0;
316 	nvme_bio_child_inbed(parent, bio_error);
317 }
318 
319 static uint32_t
nvme_get_num_segments(uint64_t addr,uint64_t size,uint32_t align)320 nvme_get_num_segments(uint64_t addr, uint64_t size, uint32_t align)
321 {
322 	uint32_t	num_segs, offset, remainder;
323 
324 	if (align == 0)
325 		return (1);
326 
327 	KASSERT((align & (align - 1)) == 0, ("alignment not power of 2\n"));
328 
329 	num_segs = size / align;
330 	remainder = size & (align - 1);
331 	offset = addr & (align - 1);
332 	if (remainder > 0 || offset > 0)
333 		num_segs += 1 + (remainder + offset - 1) / align;
334 	return (num_segs);
335 }
336 
337 static void
nvme_free_child_bios(int num_bios,struct bio ** child_bios)338 nvme_free_child_bios(int num_bios, struct bio **child_bios)
339 {
340 	int i;
341 
342 	for (i = 0; i < num_bios; i++) {
343 		if (child_bios[i] != NULL)
344 			g_destroy_bio(child_bios[i]);
345 	}
346 
347 	free(child_bios, M_NVME);
348 }
349 
350 static struct bio **
nvme_allocate_child_bios(int num_bios)351 nvme_allocate_child_bios(int num_bios)
352 {
353 	struct bio **child_bios;
354 	int err = 0, i;
355 
356 	child_bios = malloc(num_bios * sizeof(struct bio *), M_NVME, M_NOWAIT);
357 	if (child_bios == NULL)
358 		return (NULL);
359 
360 	for (i = 0; i < num_bios; i++) {
361 		child_bios[i] = g_new_bio();
362 		if (child_bios[i] == NULL)
363 			err = ENOMEM;
364 	}
365 
366 	if (err == ENOMEM) {
367 		nvme_free_child_bios(num_bios, child_bios);
368 		return (NULL);
369 	}
370 
371 	return (child_bios);
372 }
373 
374 static struct bio **
nvme_construct_child_bios(struct bio * bp,uint32_t alignment,int * num_bios)375 nvme_construct_child_bios(struct bio *bp, uint32_t alignment, int *num_bios)
376 {
377 	struct bio	**child_bios;
378 	struct bio	*child;
379 	uint64_t	cur_offset;
380 	caddr_t		data;
381 	uint32_t	rem_bcount;
382 	int		i;
383 	struct vm_page	**ma;
384 	uint32_t	ma_offset;
385 
386 	*num_bios = nvme_get_num_segments(bp->bio_offset, bp->bio_bcount,
387 	    alignment);
388 	child_bios = nvme_allocate_child_bios(*num_bios);
389 	if (child_bios == NULL)
390 		return (NULL);
391 
392 	bp->bio_children = *num_bios;
393 	bp->bio_inbed = 0;
394 	cur_offset = bp->bio_offset;
395 	rem_bcount = bp->bio_bcount;
396 	data = bp->bio_data;
397 	ma_offset = bp->bio_ma_offset;
398 	ma = bp->bio_ma;
399 
400 	for (i = 0; i < *num_bios; i++) {
401 		child = child_bios[i];
402 		child->bio_parent = bp;
403 		child->bio_cmd = bp->bio_cmd;
404 		child->bio_offset = cur_offset;
405 		child->bio_bcount = min(rem_bcount,
406 		    alignment - (cur_offset & (alignment - 1)));
407 		child->bio_flags = bp->bio_flags;
408 		if (bp->bio_flags & BIO_UNMAPPED) {
409 			child->bio_ma_offset = ma_offset;
410 			child->bio_ma = ma;
411 			child->bio_ma_n =
412 			    nvme_get_num_segments(child->bio_ma_offset,
413 				child->bio_bcount, PAGE_SIZE);
414 			ma_offset = (ma_offset + child->bio_bcount) &
415 			    PAGE_MASK;
416 			ma += child->bio_ma_n;
417 			if (ma_offset != 0)
418 				ma -= 1;
419 		} else {
420 			child->bio_data = data;
421 			data += child->bio_bcount;
422 		}
423 		cur_offset += child->bio_bcount;
424 		rem_bcount -= child->bio_bcount;
425 	}
426 
427 	return (child_bios);
428 }
429 
430 static int
nvme_ns_split_bio(struct nvme_namespace * ns,struct bio * bp,uint32_t alignment)431 nvme_ns_split_bio(struct nvme_namespace *ns, struct bio *bp,
432     uint32_t alignment)
433 {
434 	struct bio	*child;
435 	struct bio	**child_bios;
436 	int		err, i, num_bios;
437 
438 	child_bios = nvme_construct_child_bios(bp, alignment, &num_bios);
439 	if (child_bios == NULL)
440 		return (ENOMEM);
441 
442 	counter_u64_add(ns->ctrlr->alignment_splits, 1);
443 	for (i = 0; i < num_bios; i++) {
444 		child = child_bios[i];
445 		err = nvme_ns_bio_process(ns, child, nvme_bio_child_done);
446 		if (err != 0) {
447 			nvme_bio_child_inbed(bp, err);
448 			g_destroy_bio(child);
449 		}
450 	}
451 
452 	free(child_bios, M_NVME);
453 	return (0);
454 }
455 
456 int
nvme_ns_bio_process(struct nvme_namespace * ns,struct bio * bp,nvme_cb_fn_t cb_fn)457 nvme_ns_bio_process(struct nvme_namespace *ns, struct bio *bp,
458 	nvme_cb_fn_t cb_fn)
459 {
460 	struct nvme_dsm_range	*dsm_range;
461 	uint32_t		num_bios;
462 	int			err;
463 
464 	bp->bio_driver1 = cb_fn;
465 
466 	if (ns->boundary > 0 &&
467 	    (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
468 		num_bios = nvme_get_num_segments(bp->bio_offset,
469 		    bp->bio_bcount, ns->boundary);
470 		if (num_bios > 1)
471 			return (nvme_ns_split_bio(ns, bp, ns->boundary));
472 	}
473 
474 	switch (bp->bio_cmd) {
475 	case BIO_READ:
476 		err = nvme_ns_cmd_read_bio(ns, bp, nvme_ns_bio_done, bp);
477 		break;
478 	case BIO_WRITE:
479 		err = nvme_ns_cmd_write_bio(ns, bp, nvme_ns_bio_done, bp);
480 		break;
481 	case BIO_FLUSH:
482 		err = nvme_ns_cmd_flush(ns, nvme_ns_bio_done, bp);
483 		break;
484 	case BIO_DELETE:
485 		dsm_range =
486 		    malloc(sizeof(struct nvme_dsm_range), M_NVME,
487 		    M_ZERO | M_NOWAIT);
488 		if (!dsm_range) {
489 			err = ENOMEM;
490 			break;
491 		}
492 		dsm_range->length =
493 		    htole32(bp->bio_bcount/nvme_ns_get_sector_size(ns));
494 		dsm_range->starting_lba =
495 		    htole64(bp->bio_offset/nvme_ns_get_sector_size(ns));
496 		bp->bio_driver2 = dsm_range;
497 		err = nvme_ns_cmd_deallocate(ns, dsm_range, 1,
498 			nvme_ns_bio_done, bp);
499 		if (err != 0)
500 			free(dsm_range, M_NVME);
501 		break;
502 	default:
503 		err = EOPNOTSUPP;
504 		break;
505 	}
506 
507 	return (err);
508 }
509 
510 int
nvme_ns_ioctl_process(struct nvme_namespace * ns,u_long cmd,caddr_t arg,int flag,struct thread * td)511 nvme_ns_ioctl_process(struct nvme_namespace *ns, u_long cmd, caddr_t arg,
512     int flag, struct thread *td)
513 {
514 	return (nvme_ns_ioctl(ns->cdev, cmd, arg, flag, td));
515 }
516 
517 int
nvme_ns_construct(struct nvme_namespace * ns,uint32_t id,struct nvme_controller * ctrlr)518 nvme_ns_construct(struct nvme_namespace *ns, uint32_t id,
519     struct nvme_controller *ctrlr)
520 {
521 	struct make_dev_args                    md_args;
522 	struct nvme_completion_poll_status	status;
523 	int                                     res;
524 	int					unit;
525 	uint8_t					flbas_fmt;
526 	uint8_t					vwc_present;
527 
528 	ns->ctrlr = ctrlr;
529 	ns->id = id;
530 
531 	/*
532 	 * Namespaces are reconstructed after a controller reset, so check
533 	 *  to make sure we only call mtx_init once on each mtx.
534 	 *
535 	 * TODO: Move this somewhere where it gets called at controller
536 	 *  construction time, which is not invoked as part of each
537 	 *  controller reset.
538 	 */
539 	if (!mtx_initialized(&ns->lock))
540 		mtx_init(&ns->lock, "nvme ns lock", NULL, MTX_DEF);
541 
542 	status.done = 0;
543 	nvme_ctrlr_cmd_identify_namespace(ctrlr, id, &ns->data,
544 	    nvme_completion_poll_cb, &status);
545 	nvme_completion_poll(&status);
546 	if (nvme_completion_is_error(&status.cpl)) {
547 		nvme_printf(ctrlr, "nvme_identify_namespace failed\n");
548 		return (ENXIO);
549 	}
550 
551 	/* Convert data to host endian */
552 	nvme_namespace_data_swapbytes(&ns->data);
553 
554 	/*
555 	 * If the size of is zero, chances are this isn't a valid
556 	 * namespace (eg one that's not been configured yet). The
557 	 * standard says the entire id will be zeros, so this is a
558 	 * cheap way to test for that.
559 	 */
560 	if (ns->data.nsze == 0)
561 		return (ENXIO);
562 
563 	flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, ns->data.flbas);
564 
565 	/*
566 	 * Note: format is a 0-based value, so > is appropriate here,
567 	 *  not >=.
568 	 */
569 	if (flbas_fmt > ns->data.nlbaf) {
570 		nvme_printf(ctrlr,
571 		    "lba format %d exceeds number supported (%d)\n",
572 		    flbas_fmt, ns->data.nlbaf + 1);
573 		return (ENXIO);
574 	}
575 
576 	/*
577 	 * Older Intel devices (like the PC35xxx and P45xx series) advertise in
578 	 * vendor specific space an alignment that improves performance.  If
579 	 * present use for the stripe size.  NVMe 1.3 standardized this as
580 	 * NOIOB, and newer Intel drives use that.
581 	 */
582 	if ((ctrlr->quirks & QUIRK_INTEL_ALIGNMENT) != 0) {
583 		if (ctrlr->cdata.vs[3] != 0)
584 			ns->boundary =
585 			    1 << (ctrlr->cdata.vs[3] + NVME_MPS_SHIFT +
586 				NVME_CAP_HI_MPSMIN(ctrlr->cap_hi));
587 		else
588 			ns->boundary = 0;
589 	} else {
590 		ns->boundary = ns->data.noiob * nvme_ns_get_sector_size(ns);
591 	}
592 
593 	if (nvme_ctrlr_has_dataset_mgmt(&ctrlr->cdata))
594 		ns->flags |= NVME_NS_DEALLOCATE_SUPPORTED;
595 
596 	vwc_present = NVMEV(NVME_CTRLR_DATA_VWC_PRESENT, ctrlr->cdata.vwc);
597 	if (vwc_present)
598 		ns->flags |= NVME_NS_FLUSH_SUPPORTED;
599 
600 	/*
601 	 * cdev may have already been created, if we are reconstructing the
602 	 *  namespace after a controller-level reset.
603 	 */
604 	if (ns->cdev != NULL)
605 		return (0);
606 
607 	/*
608 	 * Namespace IDs start at 1, so we need to subtract 1 to create a
609 	 *  correct unit number.
610 	 */
611 	unit = device_get_unit(ctrlr->dev) * NVME_MAX_NAMESPACES + ns->id - 1;
612 
613 	make_dev_args_init(&md_args);
614 	md_args.mda_devsw = &nvme_ns_cdevsw;
615 	md_args.mda_unit = unit;
616 	md_args.mda_mode = 0600;
617 	md_args.mda_si_drv1 = ns;
618 	res = make_dev_s(&md_args, &ns->cdev, "%sn%d",
619 	    device_get_nameunit(ctrlr->dev), ns->id);
620 	if (res != 0)
621 		return (ENXIO);
622 	ns->cdev->si_drv2 = make_dev_alias(ns->cdev, "%sns%d",
623 	    device_get_nameunit(ctrlr->dev), ns->id);
624 	ns->cdev->si_flags |= SI_UNMAPPED;
625 
626 	return (0);
627 }
628 
629 void
nvme_ns_destruct(struct nvme_namespace * ns)630 nvme_ns_destruct(struct nvme_namespace *ns)
631 {
632 	if (ns->cdev != NULL) {
633 		if (ns->cdev->si_drv2 != NULL)
634 			destroy_dev(ns->cdev->si_drv2);
635 		destroy_dev(ns->cdev);
636 	}
637 }
638