summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/dev/nvme/nvme_ns.c
blob: d23061dae6b1f91718d767b8e205510b072759e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#include <machine/rtems-bsd-kernel-space.h>

/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (C) 2012-2013 Intel Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/bio.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/disk.h>
#include <sys/fcntl.h>
#include <sys/ioccom.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/proc.h>
#include <sys/systm.h>

#include <dev/pci/pcivar.h>

#include <geom/geom.h>

#include "nvme_private.h"

#ifndef __rtems__
static void		nvme_bio_child_inbed(struct bio *parent, int bio_error);
static void		nvme_bio_child_done(void *arg,
					    const struct nvme_completion *cpl);
static uint32_t		nvme_get_num_segments(uint64_t addr, uint64_t size,
					      uint32_t alignment);
static void		nvme_free_child_bios(int num_bios,
					     struct bio **child_bios);
static struct bio **	nvme_allocate_child_bios(int num_bios);
static struct bio **	nvme_construct_child_bios(struct bio *bp,
						  uint32_t alignment,
						  int *num_bios);
static int		nvme_ns_split_bio(struct nvme_namespace *ns,
					  struct bio *bp,
					  uint32_t alignment);
#endif /* __rtems__ */

static int
nvme_ns_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag,
    struct thread *td)
{
	struct nvme_namespace			*ns;
	struct nvme_controller			*ctrlr;
	struct nvme_pt_command			*pt;

	ns = cdev->si_drv1;
	ctrlr = ns->ctrlr;

	switch (cmd) {
#ifndef __rtems__
	case NVME_IO_TEST:
	case NVME_BIO_TEST:
		nvme_ns_test(ns, cmd, arg);
		break;
#endif /* __rtems__ */
	case NVME_PASSTHROUGH_CMD:
		pt = (struct nvme_pt_command *)arg;
		return (nvme_ctrlr_passthrough_cmd(ctrlr, pt, ns->id, 
		    1 /* is_user_buffer */, 0 /* is_admin_cmd */));
	case NVME_GET_NSID:
	{
		struct nvme_get_nsid *gnsid = (struct nvme_get_nsid *)arg;
		strncpy(gnsid->cdev, device_get_nameunit(ctrlr->dev),
		    sizeof(gnsid->cdev));
		gnsid->nsid = ns->id;
		break;
	}
	case DIOCGMEDIASIZE:
		*(off_t *)arg = (off_t)nvme_ns_get_size(ns);
		break;
	case DIOCGSECTORSIZE:
		*(u_int *)arg = nvme_ns_get_sector_size(ns);
		break;
	default:
		return (ENOTTY);
	}

	return (0);
}

static int
nvme_ns_open(struct cdev *dev __unused, int flags, int fmt __unused,
    struct thread *td)
{
	int error = 0;

	if (flags & FWRITE)
		error = securelevel_gt(td->td_ucred, 0);

	return (error);
}

static int
nvme_ns_close(struct cdev *dev __unused, int flags, int fmt __unused,
    struct thread *td)
{

	return (0);
}

#ifndef __rtems__
static void
nvme_ns_strategy_done(void *arg, const struct nvme_completion *cpl)
{
	struct bio *bp = arg;

	/*
	 * TODO: add more extensive translation of NVMe status codes
	 *  to different bio error codes (i.e. EIO, EINVAL, etc.)
	 */
	if (nvme_completion_is_error(cpl)) {
		bp->bio_error = EIO;
		bp->bio_flags |= BIO_ERROR;
		bp->bio_resid = bp->bio_bcount;
	} else
		bp->bio_resid = 0;

	biodone(bp);
}

static void
nvme_ns_strategy(struct bio *bp)
{
	struct nvme_namespace	*ns;
	int			err;

	ns = bp->bio_dev->si_drv1;
	err = nvme_ns_bio_process(ns, bp, nvme_ns_strategy_done);

	if (err) {
		bp->bio_error = err;
		bp->bio_flags |= BIO_ERROR;
		bp->bio_resid = bp->bio_bcount;
		biodone(bp);
	}

}
#endif /* __rtems__ */

static struct cdevsw nvme_ns_cdevsw = {
	.d_version =	D_VERSION,
	.d_flags =	D_DISK,
#ifndef __rtems__
	.d_read =	physread,
	.d_write =	physwrite,
#endif /* __rtems__ */
	.d_open =	nvme_ns_open,
	.d_close =	nvme_ns_close,
#ifndef __rtems__
	.d_strategy =	nvme_ns_strategy,
#endif /* __rtems__ */
	.d_ioctl =	nvme_ns_ioctl
};

uint32_t
nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns)
{
	return ns->ctrlr->max_xfer_size;
}

uint32_t
nvme_ns_get_sector_size(struct nvme_namespace *ns)
{
	uint8_t flbas_fmt, lbads;

	flbas_fmt = (ns->data.flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) &
		NVME_NS_DATA_FLBAS_FORMAT_MASK;
	lbads = (ns->data.lbaf[flbas_fmt] >> NVME_NS_DATA_LBAF_LBADS_SHIFT) &
		NVME_NS_DATA_LBAF_LBADS_MASK;

	return (1 << lbads);
}

uint64_t
nvme_ns_get_num_sectors(struct nvme_namespace *ns)
{
	return (ns->data.nsze);
}

uint64_t
nvme_ns_get_size(struct nvme_namespace *ns)
{
	return (nvme_ns_get_num_sectors(ns) * nvme_ns_get_sector_size(ns));
}

uint32_t
nvme_ns_get_flags(struct nvme_namespace *ns)
{
	return (ns->flags);
}

const char *
nvme_ns_get_serial_number(struct nvme_namespace *ns)
{
	return ((const char *)ns->ctrlr->cdata.sn);
}

const char *
nvme_ns_get_model_number(struct nvme_namespace *ns)
{
	return ((const char *)ns->ctrlr->cdata.mn);
}

const struct nvme_namespace_data *
nvme_ns_get_data(struct nvme_namespace *ns)
{

	return (&ns->data);
}

uint32_t
nvme_ns_get_stripesize(struct nvme_namespace *ns)
{

	if (((ns->data.nsfeat >> NVME_NS_DATA_NSFEAT_NPVALID_SHIFT) &
	    NVME_NS_DATA_NSFEAT_NPVALID_MASK) != 0 && ns->data.npwg != 0) {
		return ((ns->data.npwg + 1) * nvme_ns_get_sector_size(ns));
	}
	return (ns->boundary);
}

#ifndef __rtems__
static void
nvme_ns_bio_done(void *arg, const struct nvme_completion *status)
{
	struct bio	*bp = arg;
	nvme_cb_fn_t	bp_cb_fn;

	bp_cb_fn = bp->bio_driver1;

	if (bp->bio_driver2)
		free(bp->bio_driver2, M_NVME);

	if (nvme_completion_is_error(status)) {
		bp->bio_flags |= BIO_ERROR;
		if (bp->bio_error == 0)
			bp->bio_error = EIO;
	}

	if ((bp->bio_flags & BIO_ERROR) == 0)
		bp->bio_resid = 0;
	else
		bp->bio_resid = bp->bio_bcount;

	bp_cb_fn(bp, status);
}

static void
nvme_bio_child_inbed(struct bio *parent, int bio_error)
{
	struct nvme_completion	parent_cpl;
	int			children, inbed;

	if (bio_error != 0) {
		parent->bio_flags |= BIO_ERROR;
		parent->bio_error = bio_error;
	}

	/*
	 * atomic_fetchadd will return value before adding 1, so we still
	 *  must add 1 to get the updated inbed number.  Save bio_children
	 *  before incrementing to guard against race conditions when
	 *  two children bios complete on different queues.
	 */
	children = atomic_load_acq_int(&parent->bio_children);
	inbed = atomic_fetchadd_int(&parent->bio_inbed, 1) + 1;
	if (inbed == children) {
		bzero(&parent_cpl, sizeof(parent_cpl));
		if (parent->bio_flags & BIO_ERROR) {
			parent_cpl.status &= ~(NVME_STATUS_SC_MASK << NVME_STATUS_SC_SHIFT);
			parent_cpl.status |= (NVME_SC_DATA_TRANSFER_ERROR) << NVME_STATUS_SC_SHIFT;
		}
		nvme_ns_bio_done(parent, &parent_cpl);
	}
}

static void
nvme_bio_child_done(void *arg, const struct nvme_completion *cpl)
{
	struct bio		*child = arg;
	struct bio		*parent;
	int			bio_error;

	parent = child->bio_parent;
	g_destroy_bio(child);
	bio_error = nvme_completion_is_error(cpl) ? EIO : 0;
	nvme_bio_child_inbed(parent, bio_error);
}

static uint32_t
nvme_get_num_segments(uint64_t addr, uint64_t size, uint32_t align)
{
	uint32_t	num_segs, offset, remainder;

	if (align == 0)
		return (1);

	KASSERT((align & (align - 1)) == 0, ("alignment not power of 2\n"));

	num_segs = size / align;
	remainder = size & (align - 1);
	offset = addr & (align - 1);
	if (remainder > 0 || offset > 0)
		num_segs += 1 + (remainder + offset - 1) / align;
	return (num_segs);
}

static void
nvme_free_child_bios(int num_bios, struct bio **child_bios)
{
	int i;

	for (i = 0; i < num_bios; i++) {
		if (child_bios[i] != NULL)
			g_destroy_bio(child_bios[i]);
	}

	free(child_bios, M_NVME);
}

static struct bio **
nvme_allocate_child_bios(int num_bios)
{
	struct bio **child_bios;
	int err = 0, i;

	child_bios = malloc(num_bios * sizeof(struct bio *), M_NVME, M_NOWAIT);
	if (child_bios == NULL)
		return (NULL);

	for (i = 0; i < num_bios; i++) {
		child_bios[i] = g_new_bio();
		if (child_bios[i] == NULL)
			err = ENOMEM;
	}

	if (err == ENOMEM) {
		nvme_free_child_bios(num_bios, child_bios);
		return (NULL);
	}

	return (child_bios);
}

static struct bio **
nvme_construct_child_bios(struct bio *bp, uint32_t alignment, int *num_bios)
{
	struct bio	**child_bios;
	struct bio	*child;
	uint64_t	cur_offset;
	caddr_t		data;
	uint32_t	rem_bcount;
	int		i;
	struct vm_page	**ma;
	uint32_t	ma_offset;

	*num_bios = nvme_get_num_segments(bp->bio_offset, bp->bio_bcount,
	    alignment);
	child_bios = nvme_allocate_child_bios(*num_bios);
	if (child_bios == NULL)
		return (NULL);

	bp->bio_children = *num_bios;
	bp->bio_inbed = 0;
	cur_offset = bp->bio_offset;
	rem_bcount = bp->bio_bcount;
	data = bp->bio_data;
	ma_offset = bp->bio_ma_offset;
	ma = bp->bio_ma;

	for (i = 0; i < *num_bios; i++) {
		child = child_bios[i];
		child->bio_parent = bp;
		child->bio_cmd = bp->bio_cmd;
		child->bio_offset = cur_offset;
		child->bio_bcount = min(rem_bcount,
		    alignment - (cur_offset & (alignment - 1)));
		child->bio_flags = bp->bio_flags;
		if (bp->bio_flags & BIO_UNMAPPED) {
			child->bio_ma_offset = ma_offset;
			child->bio_ma = ma;
			child->bio_ma_n =
			    nvme_get_num_segments(child->bio_ma_offset,
				child->bio_bcount, PAGE_SIZE);
			ma_offset = (ma_offset + child->bio_bcount) &
			    PAGE_MASK;
			ma += child->bio_ma_n;
			if (ma_offset != 0)
				ma -= 1;
		} else {
			child->bio_data = data;
			data += child->bio_bcount;
		}
		cur_offset += child->bio_bcount;
		rem_bcount -= child->bio_bcount;
	}

	return (child_bios);
}

static int
nvme_ns_split_bio(struct nvme_namespace *ns, struct bio *bp,
    uint32_t alignment)
{
	struct bio	*child;
	struct bio	**child_bios;
	int		err, i, num_bios;

	child_bios = nvme_construct_child_bios(bp, alignment, &num_bios);
	if (child_bios == NULL)
		return (ENOMEM);

	for (i = 0; i < num_bios; i++) {
		child = child_bios[i];
		err = nvme_ns_bio_process(ns, child, nvme_bio_child_done);
		if (err != 0) {
			nvme_bio_child_inbed(bp, err);
			g_destroy_bio(child);
		}
	}

	free(child_bios, M_NVME);
	return (0);
}

int
nvme_ns_bio_process(struct nvme_namespace *ns, struct bio *bp,
	nvme_cb_fn_t cb_fn)
{
	struct nvme_dsm_range	*dsm_range;
	uint32_t		num_bios;
	int			err;

	bp->bio_driver1 = cb_fn;

	if (ns->boundary > 0 &&
	    (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
		num_bios = nvme_get_num_segments(bp->bio_offset,
		    bp->bio_bcount, ns->boundary);
		if (num_bios > 1)
			return (nvme_ns_split_bio(ns, bp, ns->boundary));
	}

	switch (bp->bio_cmd) {
	case BIO_READ:
		err = nvme_ns_cmd_read_bio(ns, bp, nvme_ns_bio_done, bp);
		break;
	case BIO_WRITE:
		err = nvme_ns_cmd_write_bio(ns, bp, nvme_ns_bio_done, bp);
		break;
	case BIO_FLUSH:
		err = nvme_ns_cmd_flush(ns, nvme_ns_bio_done, bp);
		break;
	case BIO_DELETE:
		dsm_range =
		    malloc(sizeof(struct nvme_dsm_range), M_NVME,
		    M_ZERO | M_WAITOK);
		if (!dsm_range) {
			err = ENOMEM;
			break;
		}
		dsm_range->length =
		    htole32(bp->bio_bcount/nvme_ns_get_sector_size(ns));
		dsm_range->starting_lba =
		    htole64(bp->bio_offset/nvme_ns_get_sector_size(ns));
		bp->bio_driver2 = dsm_range;
		err = nvme_ns_cmd_deallocate(ns, dsm_range, 1,
			nvme_ns_bio_done, bp);
		if (err != 0)
			free(dsm_range, M_NVME);
		break;
	default:
		err = EIO;
		break;
	}

	return (err);
}
#endif /* __rtems__ */

int
nvme_ns_ioctl_process(struct nvme_namespace *ns, u_long cmd, caddr_t arg,
    int flag, struct thread *td)
{
	return (nvme_ns_ioctl(ns->cdev, cmd, arg, flag, td));
}

int
nvme_ns_construct(struct nvme_namespace *ns, uint32_t id,
    struct nvme_controller *ctrlr)
{
	struct make_dev_args                    md_args;
	struct nvme_completion_poll_status	status;
	int                                     res;
	int					unit;
	uint8_t					flbas_fmt;
	uint8_t					vwc_present;

	ns->ctrlr = ctrlr;
	ns->id = id;

	/*
	 * Namespaces are reconstructed after a controller reset, so check
	 *  to make sure we only call mtx_init once on each mtx.
	 *
	 * TODO: Move this somewhere where it gets called at controller
	 *  construction time, which is not invoked as part of each
	 *  controller reset.
	 */
	if (!mtx_initialized(&ns->lock))
		mtx_init(&ns->lock, "nvme ns lock", NULL, MTX_DEF);

	status.done = 0;
	nvme_ctrlr_cmd_identify_namespace(ctrlr, id, &ns->data,
	    nvme_completion_poll_cb, &status);
	nvme_completion_poll(&status);
	if (nvme_completion_is_error(&status.cpl)) {
		nvme_printf(ctrlr, "nvme_identify_namespace failed\n");
		return (ENXIO);
	}

	/* Convert data to host endian */
	nvme_namespace_data_swapbytes(&ns->data);

	/*
	 * If the size of is zero, chances are this isn't a valid
	 * namespace (eg one that's not been configured yet). The
	 * standard says the entire id will be zeros, so this is a
	 * cheap way to test for that.
	 */
	if (ns->data.nsze == 0)
		return (ENXIO);

	flbas_fmt = (ns->data.flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) &
		NVME_NS_DATA_FLBAS_FORMAT_MASK;
	/*
	 * Note: format is a 0-based value, so > is appropriate here,
	 *  not >=.
	 */
	if (flbas_fmt > ns->data.nlbaf) {
		printf("lba format %d exceeds number supported (%d)\n",
		    flbas_fmt, ns->data.nlbaf + 1);
		return (ENXIO);
	}

	/*
	 * Older Intel devices advertise in vendor specific space an alignment
	 * that improves performance.  If present use for the stripe size.  NVMe
	 * 1.3 standardized this as NOIOB, and newer Intel drives use that.
	 */
	switch (pci_get_devid(ctrlr->dev)) {
	case 0x09538086:		/* Intel DC PC3500 */
	case 0x0a538086:		/* Intel DC PC3520 */
	case 0x0a548086:		/* Intel DC PC4500 */
	case 0x0a558086:		/* Dell Intel P4600 */
		if (ctrlr->cdata.vs[3] != 0)
			ns->boundary =
			    (1 << ctrlr->cdata.vs[3]) * ctrlr->min_page_size;
		else
			ns->boundary = 0;
		break;
	default:
		ns->boundary = ns->data.noiob * nvme_ns_get_sector_size(ns);
		break;
	}

	if (nvme_ctrlr_has_dataset_mgmt(&ctrlr->cdata))
		ns->flags |= NVME_NS_DEALLOCATE_SUPPORTED;

	vwc_present = (ctrlr->cdata.vwc >> NVME_CTRLR_DATA_VWC_PRESENT_SHIFT) &
		NVME_CTRLR_DATA_VWC_PRESENT_MASK;
	if (vwc_present)
		ns->flags |= NVME_NS_FLUSH_SUPPORTED;

	/*
	 * cdev may have already been created, if we are reconstructing the
	 *  namespace after a controller-level reset.
	 */
	if (ns->cdev != NULL)
		return (0);

	/*
	 * Namespace IDs start at 1, so we need to subtract 1 to create a
	 *  correct unit number.
	 */
	unit = device_get_unit(ctrlr->dev) * NVME_MAX_NAMESPACES + ns->id - 1;

	make_dev_args_init(&md_args);
	md_args.mda_devsw = &nvme_ns_cdevsw;
	md_args.mda_unit = unit;
	md_args.mda_mode = 0600;
	md_args.mda_si_drv1 = ns;
	res = make_dev_s(&md_args, &ns->cdev, "nvme%dns%d",
	    device_get_unit(ctrlr->dev), ns->id);
	if (res != 0)
		return (ENXIO);

	ns->cdev->si_flags |= SI_UNMAPPED;

	return (0);
}

void nvme_ns_destruct(struct nvme_namespace *ns)
{

	if (ns->cdev != NULL)
		destroy_dev(ns->cdev);
}