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