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