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