2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2012-2013 Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
37 #include <sys/fcntl.h>
38 #include <sys/ioccom.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
42 #include <sys/systm.h>
44 #include <dev/pci/pcivar.h>
46 #include <geom/geom.h>
48 #include "nvme_private.h"
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,
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,
61 static int nvme_ns_split_bio(struct nvme_namespace *ns,
66 nvme_ns_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag,
69 struct nvme_namespace *ns;
70 struct nvme_controller *ctrlr;
71 struct nvme_pt_command *pt;
79 nvme_ns_test(ns, cmd, arg);
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 */));
87 struct nvme_get_nsid *gnsid = (struct nvme_get_nsid *)arg;
88 strncpy(gnsid->cdev, device_get_nameunit(ctrlr->dev),
90 gnsid->cdev[sizeof(gnsid->cdev) - 1] = '\0';
95 *(off_t *)arg = (off_t)nvme_ns_get_size(ns);
98 *(u_int *)arg = nvme_ns_get_sector_size(ns);
108 nvme_ns_open(struct cdev *dev __unused, int flags, int fmt __unused,
114 error = securelevel_gt(td->td_ucred, 0);
120 nvme_ns_close(struct cdev *dev __unused, int flags, int fmt __unused,
128 nvme_ns_strategy_done(void *arg, const struct nvme_completion *cpl)
130 struct bio *bp = arg;
133 * TODO: add more extensive translation of NVMe status codes
134 * to different bio error codes (i.e. EIO, EINVAL, etc.)
136 if (nvme_completion_is_error(cpl)) {
138 bp->bio_flags |= BIO_ERROR;
139 bp->bio_resid = bp->bio_bcount;
147 nvme_ns_strategy(struct bio *bp)
149 struct nvme_namespace *ns;
152 ns = bp->bio_dev->si_drv1;
153 err = nvme_ns_bio_process(ns, bp, nvme_ns_strategy_done);
157 bp->bio_flags |= BIO_ERROR;
158 bp->bio_resid = bp->bio_bcount;
164 static struct cdevsw nvme_ns_cdevsw = {
165 .d_version = D_VERSION,
168 .d_write = physwrite,
169 .d_open = nvme_ns_open,
170 .d_close = nvme_ns_close,
171 .d_strategy = nvme_ns_strategy,
172 .d_ioctl = nvme_ns_ioctl
176 nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns)
178 return ns->ctrlr->max_xfer_size;
182 nvme_ns_get_sector_size(struct nvme_namespace *ns)
184 uint8_t flbas_fmt, lbads;
186 flbas_fmt = (ns->data.flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) &
187 NVME_NS_DATA_FLBAS_FORMAT_MASK;
188 lbads = (ns->data.lbaf[flbas_fmt] >> NVME_NS_DATA_LBAF_LBADS_SHIFT) &
189 NVME_NS_DATA_LBAF_LBADS_MASK;
195 nvme_ns_get_num_sectors(struct nvme_namespace *ns)
197 return (ns->data.nsze);
201 nvme_ns_get_size(struct nvme_namespace *ns)
203 return (nvme_ns_get_num_sectors(ns) * nvme_ns_get_sector_size(ns));
207 nvme_ns_get_flags(struct nvme_namespace *ns)
213 nvme_ns_get_serial_number(struct nvme_namespace *ns)
215 return ((const char *)ns->ctrlr->cdata.sn);
219 nvme_ns_get_model_number(struct nvme_namespace *ns)
221 return ((const char *)ns->ctrlr->cdata.mn);
224 const struct nvme_namespace_data *
225 nvme_ns_get_data(struct nvme_namespace *ns)
232 nvme_ns_get_stripesize(struct nvme_namespace *ns)
235 if (((ns->data.nsfeat >> NVME_NS_DATA_NSFEAT_NPVALID_SHIFT) &
236 NVME_NS_DATA_NSFEAT_NPVALID_MASK) != 0 && ns->data.npwg != 0) {
237 return ((ns->data.npwg + 1) * nvme_ns_get_sector_size(ns));
239 return (ns->boundary);
243 nvme_ns_bio_done(void *arg, const struct nvme_completion *status)
245 struct bio *bp = arg;
246 nvme_cb_fn_t bp_cb_fn;
248 bp_cb_fn = bp->bio_driver1;
251 free(bp->bio_driver2, M_NVME);
253 if (nvme_completion_is_error(status)) {
254 bp->bio_flags |= BIO_ERROR;
255 if (bp->bio_error == 0)
259 if ((bp->bio_flags & BIO_ERROR) == 0)
262 bp->bio_resid = bp->bio_bcount;
264 bp_cb_fn(bp, status);
268 nvme_bio_child_inbed(struct bio *parent, int bio_error)
270 struct nvme_completion parent_cpl;
273 if (bio_error != 0) {
274 parent->bio_flags |= BIO_ERROR;
275 parent->bio_error = bio_error;
279 * atomic_fetchadd will return value before adding 1, so we still
280 * must add 1 to get the updated inbed number. Save bio_children
281 * before incrementing to guard against race conditions when
282 * two children bios complete on different queues.
284 children = atomic_load_acq_int(&parent->bio_children);
285 inbed = atomic_fetchadd_int(&parent->bio_inbed, 1) + 1;
286 if (inbed == children) {
287 bzero(&parent_cpl, sizeof(parent_cpl));
288 if (parent->bio_flags & BIO_ERROR) {
289 parent_cpl.status &= ~(NVME_STATUS_SC_MASK << NVME_STATUS_SC_SHIFT);
290 parent_cpl.status |= (NVME_SC_DATA_TRANSFER_ERROR) << NVME_STATUS_SC_SHIFT;
292 nvme_ns_bio_done(parent, &parent_cpl);
297 nvme_bio_child_done(void *arg, const struct nvme_completion *cpl)
299 struct bio *child = arg;
303 parent = child->bio_parent;
304 g_destroy_bio(child);
305 bio_error = nvme_completion_is_error(cpl) ? EIO : 0;
306 nvme_bio_child_inbed(parent, bio_error);
310 nvme_get_num_segments(uint64_t addr, uint64_t size, uint32_t align)
312 uint32_t num_segs, offset, remainder;
317 KASSERT((align & (align - 1)) == 0, ("alignment not power of 2\n"));
319 num_segs = size / align;
320 remainder = size & (align - 1);
321 offset = addr & (align - 1);
322 if (remainder > 0 || offset > 0)
323 num_segs += 1 + (remainder + offset - 1) / align;
328 nvme_free_child_bios(int num_bios, struct bio **child_bios)
332 for (i = 0; i < num_bios; i++) {
333 if (child_bios[i] != NULL)
334 g_destroy_bio(child_bios[i]);
337 free(child_bios, M_NVME);
341 nvme_allocate_child_bios(int num_bios)
343 struct bio **child_bios;
346 child_bios = malloc(num_bios * sizeof(struct bio *), M_NVME, M_NOWAIT);
347 if (child_bios == NULL)
350 for (i = 0; i < num_bios; i++) {
351 child_bios[i] = g_new_bio();
352 if (child_bios[i] == NULL)
357 nvme_free_child_bios(num_bios, child_bios);
365 nvme_construct_child_bios(struct bio *bp, uint32_t alignment, int *num_bios)
367 struct bio **child_bios;
376 *num_bios = nvme_get_num_segments(bp->bio_offset, bp->bio_bcount,
378 child_bios = nvme_allocate_child_bios(*num_bios);
379 if (child_bios == NULL)
382 bp->bio_children = *num_bios;
384 cur_offset = bp->bio_offset;
385 rem_bcount = bp->bio_bcount;
387 ma_offset = bp->bio_ma_offset;
390 for (i = 0; i < *num_bios; i++) {
391 child = child_bios[i];
392 child->bio_parent = bp;
393 child->bio_cmd = bp->bio_cmd;
394 child->bio_offset = cur_offset;
395 child->bio_bcount = min(rem_bcount,
396 alignment - (cur_offset & (alignment - 1)));
397 child->bio_flags = bp->bio_flags;
398 if (bp->bio_flags & BIO_UNMAPPED) {
399 child->bio_ma_offset = ma_offset;
402 nvme_get_num_segments(child->bio_ma_offset,
403 child->bio_bcount, PAGE_SIZE);
404 ma_offset = (ma_offset + child->bio_bcount) &
406 ma += child->bio_ma_n;
410 child->bio_data = data;
411 data += child->bio_bcount;
413 cur_offset += child->bio_bcount;
414 rem_bcount -= child->bio_bcount;
421 nvme_ns_split_bio(struct nvme_namespace *ns, struct bio *bp,
425 struct bio **child_bios;
426 int err, i, num_bios;
428 child_bios = nvme_construct_child_bios(bp, alignment, &num_bios);
429 if (child_bios == NULL)
432 for (i = 0; i < num_bios; i++) {
433 child = child_bios[i];
434 err = nvme_ns_bio_process(ns, child, nvme_bio_child_done);
436 nvme_bio_child_inbed(bp, err);
437 g_destroy_bio(child);
441 free(child_bios, M_NVME);
446 nvme_ns_bio_process(struct nvme_namespace *ns, struct bio *bp,
449 struct nvme_dsm_range *dsm_range;
453 bp->bio_driver1 = cb_fn;
455 if (ns->boundary > 0 &&
456 (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
457 num_bios = nvme_get_num_segments(bp->bio_offset,
458 bp->bio_bcount, ns->boundary);
460 return (nvme_ns_split_bio(ns, bp, ns->boundary));
463 switch (bp->bio_cmd) {
465 err = nvme_ns_cmd_read_bio(ns, bp, nvme_ns_bio_done, bp);
468 err = nvme_ns_cmd_write_bio(ns, bp, nvme_ns_bio_done, bp);
471 err = nvme_ns_cmd_flush(ns, nvme_ns_bio_done, bp);
475 malloc(sizeof(struct nvme_dsm_range), M_NVME,
482 htole32(bp->bio_bcount/nvme_ns_get_sector_size(ns));
483 dsm_range->starting_lba =
484 htole64(bp->bio_offset/nvme_ns_get_sector_size(ns));
485 bp->bio_driver2 = dsm_range;
486 err = nvme_ns_cmd_deallocate(ns, dsm_range, 1,
487 nvme_ns_bio_done, bp);
489 free(dsm_range, M_NVME);
500 nvme_ns_ioctl_process(struct nvme_namespace *ns, u_long cmd, caddr_t arg,
501 int flag, struct thread *td)
503 return (nvme_ns_ioctl(ns->cdev, cmd, arg, flag, td));
507 nvme_ns_construct(struct nvme_namespace *ns, uint32_t id,
508 struct nvme_controller *ctrlr)
510 struct make_dev_args md_args;
511 struct nvme_completion_poll_status status;
521 * Namespaces are reconstructed after a controller reset, so check
522 * to make sure we only call mtx_init once on each mtx.
524 * TODO: Move this somewhere where it gets called at controller
525 * construction time, which is not invoked as part of each
528 if (!mtx_initialized(&ns->lock))
529 mtx_init(&ns->lock, "nvme ns lock", NULL, MTX_DEF);
532 nvme_ctrlr_cmd_identify_namespace(ctrlr, id, &ns->data,
533 nvme_completion_poll_cb, &status);
534 nvme_completion_poll(&status);
535 if (nvme_completion_is_error(&status.cpl)) {
536 nvme_printf(ctrlr, "nvme_identify_namespace failed\n");
540 /* Convert data to host endian */
541 nvme_namespace_data_swapbytes(&ns->data);
544 * If the size of is zero, chances are this isn't a valid
545 * namespace (eg one that's not been configured yet). The
546 * standard says the entire id will be zeros, so this is a
547 * cheap way to test for that.
549 if (ns->data.nsze == 0)
552 flbas_fmt = (ns->data.flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) &
553 NVME_NS_DATA_FLBAS_FORMAT_MASK;
555 * Note: format is a 0-based value, so > is appropriate here,
558 if (flbas_fmt > ns->data.nlbaf) {
559 printf("lba format %d exceeds number supported (%d)\n",
560 flbas_fmt, ns->data.nlbaf + 1);
565 * Older Intel devices advertise in vendor specific space an alignment
566 * that improves performance. If present use for the stripe size. NVMe
567 * 1.3 standardized this as NOIOB, and newer Intel drives use that.
569 switch (pci_get_devid(ctrlr->dev)) {
570 case 0x09538086: /* Intel DC PC3500 */
571 case 0x0a538086: /* Intel DC PC3520 */
572 case 0x0a548086: /* Intel DC PC4500 */
573 case 0x0a558086: /* Dell Intel P4600 */
574 if (ctrlr->cdata.vs[3] != 0)
576 (1 << ctrlr->cdata.vs[3]) * ctrlr->min_page_size;
581 ns->boundary = ns->data.noiob * nvme_ns_get_sector_size(ns);
585 if (nvme_ctrlr_has_dataset_mgmt(&ctrlr->cdata))
586 ns->flags |= NVME_NS_DEALLOCATE_SUPPORTED;
588 vwc_present = (ctrlr->cdata.vwc >> NVME_CTRLR_DATA_VWC_PRESENT_SHIFT) &
589 NVME_CTRLR_DATA_VWC_PRESENT_MASK;
591 ns->flags |= NVME_NS_FLUSH_SUPPORTED;
594 * cdev may have already been created, if we are reconstructing the
595 * namespace after a controller-level reset.
597 if (ns->cdev != NULL)
601 * Namespace IDs start at 1, so we need to subtract 1 to create a
602 * correct unit number.
604 unit = device_get_unit(ctrlr->dev) * NVME_MAX_NAMESPACES + ns->id - 1;
606 make_dev_args_init(&md_args);
607 md_args.mda_devsw = &nvme_ns_cdevsw;
608 md_args.mda_unit = unit;
609 md_args.mda_mode = 0600;
610 md_args.mda_si_drv1 = ns;
611 res = make_dev_s(&md_args, &ns->cdev, "nvme%dns%d",
612 device_get_unit(ctrlr->dev), ns->id);
616 ns->cdev->si_flags |= SI_UNMAPPED;
622 nvme_ns_destruct(struct nvme_namespace *ns)
625 if (ns->cdev != NULL)
626 destroy_dev(ns->cdev);