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