nvme_ns.c revision 294711
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: stable/10/sys/dev/nvme/nvme_ns.c 294711 2016-01-25 09:40:25Z smh $");
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
48extern int		nvme_max_optimal_sectorsize;
49
50static void		nvme_bio_child_inbed(struct bio *parent, int bio_error);
51static void		nvme_bio_child_done(void *arg,
52					    const struct nvme_completion *cpl);
53static uint32_t		nvme_get_num_segments(uint64_t addr, uint64_t size,
54					      uint32_t alignment);
55static void		nvme_free_child_bios(int num_bios,
56					     struct bio **child_bios);
57static struct bio **	nvme_allocate_child_bios(int num_bios);
58static struct bio **	nvme_construct_child_bios(struct bio *bp,
59						  uint32_t alignment,
60						  int *num_bios);
61static int		nvme_ns_split_bio(struct nvme_namespace *ns,
62					  struct bio *bp,
63					  uint32_t alignment);
64
65static int
66nvme_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
98static int
99nvme_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
110static int
111nvme_ns_close(struct cdev *dev __unused, int flags, int fmt __unused,
112    struct thread *td)
113{
114
115	return (0);
116}
117
118static void
119nvme_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
137static void
138nvme_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
155static 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
166uint32_t
167nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns)
168{
169	return ns->ctrlr->max_xfer_size;
170}
171
172uint32_t
173nvme_ns_get_sector_size(struct nvme_namespace *ns)
174{
175	return (1 << ns->data.lbaf[ns->data.flbas.format].lbads);
176}
177
178uint64_t
179nvme_ns_get_num_sectors(struct nvme_namespace *ns)
180{
181	return (ns->data.nsze);
182}
183
184uint64_t
185nvme_ns_get_size(struct nvme_namespace *ns)
186{
187	return (nvme_ns_get_num_sectors(ns) * nvme_ns_get_sector_size(ns));
188}
189
190uint32_t
191nvme_ns_get_flags(struct nvme_namespace *ns)
192{
193	return (ns->flags);
194}
195
196const char *
197nvme_ns_get_serial_number(struct nvme_namespace *ns)
198{
199	return ((const char *)ns->ctrlr->cdata.sn);
200}
201
202const char *
203nvme_ns_get_model_number(struct nvme_namespace *ns)
204{
205	return ((const char *)ns->ctrlr->cdata.mn);
206}
207
208const struct nvme_namespace_data *
209nvme_ns_get_data(struct nvme_namespace *ns)
210{
211
212	return (&ns->data);
213}
214
215uint32_t
216nvme_ns_get_stripesize(struct nvme_namespace *ns)
217{
218
219	return (ns->stripesize);
220}
221
222uint32_t
223nvme_ns_get_optimal_sector_size(struct nvme_namespace *ns)
224{
225	uint32_t stripesize;
226
227	stripesize = nvme_ns_get_stripesize(ns);
228
229	if (stripesize == 0)
230		return nvme_ns_get_sector_size(ns);
231
232	if (nvme_max_optimal_sectorsize == 0)
233		return (stripesize);
234
235	return (MIN(stripesize, nvme_max_optimal_sectorsize));
236}
237
238static void
239nvme_ns_bio_done(void *arg, const struct nvme_completion *status)
240{
241	struct bio	*bp = arg;
242	nvme_cb_fn_t	bp_cb_fn;
243
244	bp_cb_fn = bp->bio_driver1;
245
246	if (bp->bio_driver2)
247		free(bp->bio_driver2, M_NVME);
248
249	if (nvme_completion_is_error(status)) {
250		bp->bio_flags |= BIO_ERROR;
251		if (bp->bio_error == 0)
252			bp->bio_error = EIO;
253	}
254
255	if ((bp->bio_flags & BIO_ERROR) == 0)
256		bp->bio_resid = 0;
257	else
258		bp->bio_resid = bp->bio_bcount;
259
260	bp_cb_fn(bp, status);
261}
262
263static void
264nvme_bio_child_inbed(struct bio *parent, int bio_error)
265{
266	struct nvme_completion	parent_cpl;
267	int			children, inbed;
268
269	if (bio_error != 0) {
270		parent->bio_flags |= BIO_ERROR;
271		parent->bio_error = bio_error;
272	}
273
274	/*
275	 * atomic_fetchadd will return value before adding 1, so we still
276	 *  must add 1 to get the updated inbed number.  Save bio_children
277	 *  before incrementing to guard against race conditions when
278	 *  two children bios complete on different queues.
279	 */
280	children = atomic_load_acq_int(&parent->bio_children);
281	inbed = atomic_fetchadd_int(&parent->bio_inbed, 1) + 1;
282	if (inbed == children) {
283		bzero(&parent_cpl, sizeof(parent_cpl));
284		if (parent->bio_flags & BIO_ERROR)
285			parent_cpl.status.sc = NVME_SC_DATA_TRANSFER_ERROR;
286		nvme_ns_bio_done(parent, &parent_cpl);
287	}
288}
289
290static void
291nvme_bio_child_done(void *arg, const struct nvme_completion *cpl)
292{
293	struct bio		*child = arg;
294	struct bio		*parent;
295	int			bio_error;
296
297	parent = child->bio_parent;
298	g_destroy_bio(child);
299	bio_error = nvme_completion_is_error(cpl) ? EIO : 0;
300	nvme_bio_child_inbed(parent, bio_error);
301}
302
303static uint32_t
304nvme_get_num_segments(uint64_t addr, uint64_t size, uint32_t align)
305{
306	uint32_t	num_segs, offset, remainder;
307
308	if (align == 0)
309		return (1);
310
311	KASSERT((align & (align - 1)) == 0, ("alignment not power of 2\n"));
312
313	num_segs = size / align;
314	remainder = size & (align - 1);
315	offset = addr & (align - 1);
316	if (remainder > 0 || offset > 0)
317		num_segs += 1 + (remainder + offset - 1) / align;
318	return (num_segs);
319}
320
321static void
322nvme_free_child_bios(int num_bios, struct bio **child_bios)
323{
324	int i;
325
326	for (i = 0; i < num_bios; i++) {
327		if (child_bios[i] != NULL)
328			g_destroy_bio(child_bios[i]);
329	}
330
331	free(child_bios, M_NVME);
332}
333
334static struct bio **
335nvme_allocate_child_bios(int num_bios)
336{
337	struct bio **child_bios;
338	int err = 0, i;
339
340	child_bios = malloc(num_bios * sizeof(struct bio *), M_NVME, M_NOWAIT);
341	if (child_bios == NULL)
342		return (NULL);
343
344	for (i = 0; i < num_bios; i++) {
345		child_bios[i] = g_new_bio();
346		if (child_bios[i] == NULL)
347			err = ENOMEM;
348	}
349
350	if (err == ENOMEM) {
351		nvme_free_child_bios(num_bios, child_bios);
352		return (NULL);
353	}
354
355	return (child_bios);
356}
357
358static struct bio **
359nvme_construct_child_bios(struct bio *bp, uint32_t alignment, int *num_bios)
360{
361	struct bio	**child_bios;
362	struct bio	*child;
363	uint64_t	cur_offset;
364	caddr_t		data;
365	uint32_t	rem_bcount;
366	int		i;
367#ifdef NVME_UNMAPPED_BIO_SUPPORT
368	struct vm_page	**ma;
369	uint32_t	ma_offset;
370#endif
371
372	*num_bios = nvme_get_num_segments(bp->bio_offset, bp->bio_bcount,
373	    alignment);
374	child_bios = nvme_allocate_child_bios(*num_bios);
375	if (child_bios == NULL)
376		return (NULL);
377
378	bp->bio_children = *num_bios;
379	bp->bio_inbed = 0;
380	cur_offset = bp->bio_offset;
381	rem_bcount = bp->bio_bcount;
382	data = bp->bio_data;
383#ifdef NVME_UNMAPPED_BIO_SUPPORT
384	ma_offset = bp->bio_ma_offset;
385	ma = bp->bio_ma;
386#endif
387
388	for (i = 0; i < *num_bios; i++) {
389		child = child_bios[i];
390		child->bio_parent = bp;
391		child->bio_cmd = bp->bio_cmd;
392		child->bio_offset = cur_offset;
393		child->bio_bcount = min(rem_bcount,
394		    alignment - (cur_offset & (alignment - 1)));
395		child->bio_flags = bp->bio_flags;
396#ifdef NVME_UNMAPPED_BIO_SUPPORT
397		if (bp->bio_flags & BIO_UNMAPPED) {
398			child->bio_ma_offset = ma_offset;
399			child->bio_ma = ma;
400			child->bio_ma_n =
401			    nvme_get_num_segments(child->bio_ma_offset,
402				child->bio_bcount, PAGE_SIZE);
403			ma_offset = (ma_offset + child->bio_bcount) &
404			    PAGE_MASK;
405			ma += child->bio_ma_n;
406			if (ma_offset != 0)
407				ma -= 1;
408		} else
409#endif
410		{
411			child->bio_data = data;
412			data += child->bio_bcount;
413		}
414		cur_offset += child->bio_bcount;
415		rem_bcount -= child->bio_bcount;
416	}
417
418	return (child_bios);
419}
420
421static int
422nvme_ns_split_bio(struct nvme_namespace *ns, struct bio *bp,
423    uint32_t alignment)
424{
425	struct bio	*child;
426	struct bio	**child_bios;
427	int		err, i, num_bios;
428
429	child_bios = nvme_construct_child_bios(bp, alignment, &num_bios);
430	if (child_bios == NULL)
431		return (ENOMEM);
432
433	for (i = 0; i < num_bios; i++) {
434		child = child_bios[i];
435		err = nvme_ns_bio_process(ns, child, nvme_bio_child_done);
436		if (err != 0) {
437			nvme_bio_child_inbed(bp, err);
438			g_destroy_bio(child);
439		}
440	}
441
442	free(child_bios, M_NVME);
443	return (0);
444}
445
446int
447nvme_ns_bio_process(struct nvme_namespace *ns, struct bio *bp,
448	nvme_cb_fn_t cb_fn)
449{
450	struct nvme_dsm_range	*dsm_range;
451	uint32_t		num_bios;
452	int			err;
453
454	bp->bio_driver1 = cb_fn;
455
456	if (ns->stripesize > 0 &&
457	    (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
458		num_bios = nvme_get_num_segments(bp->bio_offset,
459		    bp->bio_bcount, ns->stripesize);
460		if (num_bios > 1)
461			return (nvme_ns_split_bio(ns, bp, ns->stripesize));
462	}
463
464	switch (bp->bio_cmd) {
465	case BIO_READ:
466		err = nvme_ns_cmd_read_bio(ns, bp, nvme_ns_bio_done, bp);
467		break;
468	case BIO_WRITE:
469		err = nvme_ns_cmd_write_bio(ns, bp, nvme_ns_bio_done, bp);
470		break;
471	case BIO_FLUSH:
472		err = nvme_ns_cmd_flush(ns, nvme_ns_bio_done, bp);
473		break;
474	case BIO_DELETE:
475		dsm_range =
476		    malloc(sizeof(struct nvme_dsm_range), M_NVME,
477		    M_ZERO | M_WAITOK);
478		dsm_range->length =
479		    bp->bio_bcount/nvme_ns_get_sector_size(ns);
480		dsm_range->starting_lba =
481		    bp->bio_offset/nvme_ns_get_sector_size(ns);
482		bp->bio_driver2 = dsm_range;
483		err = nvme_ns_cmd_deallocate(ns, dsm_range, 1,
484			nvme_ns_bio_done, bp);
485		if (err != 0)
486			free(dsm_range, M_NVME);
487		break;
488	default:
489		err = EIO;
490		break;
491	}
492
493	return (err);
494}
495
496int
497nvme_ns_construct(struct nvme_namespace *ns, uint16_t id,
498    struct nvme_controller *ctrlr)
499{
500	struct nvme_completion_poll_status	status;
501	int					unit;
502
503	ns->ctrlr = ctrlr;
504	ns->id = id;
505	ns->stripesize = 0;
506
507	if (pci_get_devid(ctrlr->dev) == 0x09538086 && ctrlr->cdata.vs[3] != 0)
508		ns->stripesize =
509		    (1 << ctrlr->cdata.vs[3]) * ctrlr->min_page_size;
510
511	/*
512	 * Namespaces are reconstructed after a controller reset, so check
513	 *  to make sure we only call mtx_init once on each mtx.
514	 *
515	 * TODO: Move this somewhere where it gets called at controller
516	 *  construction time, which is not invoked as part of each
517	 *  controller reset.
518	 */
519	if (!mtx_initialized(&ns->lock))
520		mtx_init(&ns->lock, "nvme ns lock", NULL, MTX_DEF);
521
522	status.done = FALSE;
523	nvme_ctrlr_cmd_identify_namespace(ctrlr, id, &ns->data,
524	    nvme_completion_poll_cb, &status);
525	while (status.done == FALSE)
526		DELAY(5);
527	if (nvme_completion_is_error(&status.cpl)) {
528		nvme_printf(ctrlr, "nvme_identify_namespace failed\n");
529		return (ENXIO);
530	}
531
532	/*
533	 * Note: format is a 0-based value, so > is appropriate here,
534	 *  not >=.
535	 */
536	if (ns->data.flbas.format > ns->data.nlbaf) {
537		printf("lba format %d exceeds number supported (%d)\n",
538		    ns->data.flbas.format, ns->data.nlbaf+1);
539		return (1);
540	}
541
542	if (ctrlr->cdata.oncs.dsm)
543		ns->flags |= NVME_NS_DEALLOCATE_SUPPORTED;
544
545	if (ctrlr->cdata.vwc.present)
546		ns->flags |= NVME_NS_FLUSH_SUPPORTED;
547
548	/*
549	 * cdev may have already been created, if we are reconstructing the
550	 *  namespace after a controller-level reset.
551	 */
552	if (ns->cdev != NULL)
553		return (0);
554
555	/*
556	 * Namespace IDs start at 1, so we need to subtract 1 to create a
557	 *  correct unit number.
558	 */
559	unit = device_get_unit(ctrlr->dev) * NVME_MAX_NAMESPACES + ns->id - 1;
560
561/*
562 * MAKEDEV_ETERNAL was added in r210923, for cdevs that will never
563 *  be destroyed.  This avoids refcounting on the cdev object.
564 *  That should be OK case here, as long as we're not supporting PCIe
565 *  surprise removal nor namespace deletion.
566 */
567#ifdef MAKEDEV_ETERNAL_KLD
568	ns->cdev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &nvme_ns_cdevsw, unit,
569	    NULL, UID_ROOT, GID_WHEEL, 0600, "nvme%dns%d",
570	    device_get_unit(ctrlr->dev), ns->id);
571#else
572	ns->cdev = make_dev_credf(0, &nvme_ns_cdevsw, unit,
573	    NULL, UID_ROOT, GID_WHEEL, 0600, "nvme%dns%d",
574	    device_get_unit(ctrlr->dev), ns->id);
575#endif
576#ifdef NVME_UNMAPPED_BIO_SUPPORT
577	ns->cdev->si_flags |= SI_UNMAPPED;
578#endif
579
580	if (ns->cdev != NULL)
581		ns->cdev->si_drv1 = ns;
582
583	return (0);
584}
585
586void nvme_ns_destruct(struct nvme_namespace *ns)
587{
588
589	if (ns->cdev != NULL)
590		destroy_dev(ns->cdev);
591}
592