2 * Copyright (C) 2012-2013 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
35 #include <sys/fcntl.h>
36 #include <sys/ioccom.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
40 #include <sys/systm.h>
42 #include <dev/pci/pcivar.h>
44 #include <geom/geom.h>
46 #include "nvme_private.h"
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,
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,
59 static int nvme_ns_split_bio(struct nvme_namespace *ns,
64 nvme_ns_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag,
67 struct nvme_namespace *ns;
68 struct nvme_controller *ctrlr;
69 struct nvme_pt_command *pt;
77 nvme_ns_test(ns, cmd, arg);
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 */));
84 *(off_t *)arg = (off_t)nvme_ns_get_size(ns);
87 *(u_int *)arg = nvme_ns_get_sector_size(ns);
97 nvme_ns_open(struct cdev *dev __unused, int flags, int fmt __unused,
103 error = securelevel_gt(td->td_ucred, 0);
109 nvme_ns_close(struct cdev *dev __unused, int flags, int fmt __unused,
117 nvme_ns_strategy_done(void *arg, const struct nvme_completion *cpl)
119 struct bio *bp = arg;
122 * TODO: add more extensive translation of NVMe status codes
123 * to different bio error codes (i.e. EIO, EINVAL, etc.)
125 if (nvme_completion_is_error(cpl)) {
127 bp->bio_flags |= BIO_ERROR;
128 bp->bio_resid = bp->bio_bcount;
136 nvme_ns_strategy(struct bio *bp)
138 struct nvme_namespace *ns;
141 ns = bp->bio_dev->si_drv1;
142 err = nvme_ns_bio_process(ns, bp, nvme_ns_strategy_done);
146 bp->bio_flags |= BIO_ERROR;
147 bp->bio_resid = bp->bio_bcount;
153 static struct cdevsw nvme_ns_cdevsw = {
154 .d_version = D_VERSION,
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
165 nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns)
167 return ns->ctrlr->max_xfer_size;
171 nvme_ns_get_sector_size(struct nvme_namespace *ns)
173 return (1 << ns->data.lbaf[ns->data.flbas.format].lbads);
177 nvme_ns_get_num_sectors(struct nvme_namespace *ns)
179 return (ns->data.nsze);
183 nvme_ns_get_size(struct nvme_namespace *ns)
185 return (nvme_ns_get_num_sectors(ns) * nvme_ns_get_sector_size(ns));
189 nvme_ns_get_flags(struct nvme_namespace *ns)
195 nvme_ns_get_serial_number(struct nvme_namespace *ns)
197 return ((const char *)ns->ctrlr->cdata.sn);
201 nvme_ns_get_model_number(struct nvme_namespace *ns)
203 return ((const char *)ns->ctrlr->cdata.mn);
206 const struct nvme_namespace_data *
207 nvme_ns_get_data(struct nvme_namespace *ns)
214 nvme_ns_get_stripesize(struct nvme_namespace *ns)
217 return (ns->stripesize);
221 nvme_ns_bio_done(void *arg, const struct nvme_completion *status)
223 struct bio *bp = arg;
224 nvme_cb_fn_t bp_cb_fn;
226 bp_cb_fn = bp->bio_driver1;
229 free(bp->bio_driver2, M_NVME);
231 if (nvme_completion_is_error(status)) {
232 bp->bio_flags |= BIO_ERROR;
233 if (bp->bio_error == 0)
237 if ((bp->bio_flags & BIO_ERROR) == 0)
240 bp->bio_resid = bp->bio_bcount;
242 bp_cb_fn(bp, status);
246 nvme_bio_child_inbed(struct bio *parent, int bio_error)
248 struct nvme_completion parent_cpl;
251 if (bio_error != 0) {
252 parent->bio_flags |= BIO_ERROR;
253 parent->bio_error = bio_error;
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.
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);
273 nvme_bio_child_done(void *arg, const struct nvme_completion *cpl)
275 struct bio *child = arg;
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);
286 nvme_get_num_segments(uint64_t addr, uint64_t size, uint32_t align)
288 uint32_t num_segs, offset, remainder;
293 KASSERT((align & (align - 1)) == 0, ("alignment not power of 2\n"));
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;
304 nvme_free_child_bios(int num_bios, struct bio **child_bios)
308 for (i = 0; i < num_bios; i++) {
309 if (child_bios[i] != NULL)
310 g_destroy_bio(child_bios[i]);
313 free(child_bios, M_NVME);
317 nvme_allocate_child_bios(int num_bios)
319 struct bio **child_bios;
322 child_bios = malloc(num_bios * sizeof(struct bio *), M_NVME, M_NOWAIT);
323 if (child_bios == NULL)
326 for (i = 0; i < num_bios; i++) {
327 child_bios[i] = g_new_bio();
328 if (child_bios[i] == NULL)
333 nvme_free_child_bios(num_bios, child_bios);
341 nvme_construct_child_bios(struct bio *bp, uint32_t alignment, int *num_bios)
343 struct bio **child_bios;
349 #ifdef NVME_UNMAPPED_BIO_SUPPORT
354 *num_bios = nvme_get_num_segments(bp->bio_offset, bp->bio_bcount,
356 child_bios = nvme_allocate_child_bios(*num_bios);
357 if (child_bios == NULL)
360 bp->bio_children = *num_bios;
362 cur_offset = bp->bio_offset;
363 rem_bcount = bp->bio_bcount;
365 #ifdef NVME_UNMAPPED_BIO_SUPPORT
366 ma_offset = bp->bio_ma_offset;
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;
383 nvme_get_num_segments(child->bio_ma_offset,
384 child->bio_bcount, PAGE_SIZE);
385 ma_offset = (ma_offset + child->bio_bcount) &
387 ma += child->bio_ma_n;
393 child->bio_data = data;
394 data += child->bio_bcount;
396 cur_offset += child->bio_bcount;
397 rem_bcount -= child->bio_bcount;
404 nvme_ns_split_bio(struct nvme_namespace *ns, struct bio *bp,
408 struct bio **child_bios;
409 int err, i, num_bios;
411 child_bios = nvme_construct_child_bios(bp, alignment, &num_bios);
412 if (child_bios == NULL)
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);
419 nvme_bio_child_inbed(bp, err);
420 g_destroy_bio(child);
424 free(child_bios, M_NVME);
429 nvme_ns_bio_process(struct nvme_namespace *ns, struct bio *bp,
432 struct nvme_dsm_range *dsm_range;
436 bp->bio_driver1 = cb_fn;
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);
443 return (nvme_ns_split_bio(ns, bp, ns->stripesize));
446 switch (bp->bio_cmd) {
448 err = nvme_ns_cmd_read_bio(ns, bp, nvme_ns_bio_done, bp);
451 err = nvme_ns_cmd_write_bio(ns, bp, nvme_ns_bio_done, bp);
454 err = nvme_ns_cmd_flush(ns, nvme_ns_bio_done, bp);
458 malloc(sizeof(struct nvme_dsm_range), M_NVME,
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);
468 free(dsm_range, M_NVME);
479 nvme_ns_construct(struct nvme_namespace *ns, uint16_t id,
480 struct nvme_controller *ctrlr)
482 struct nvme_completion_poll_status status;
489 if (pci_get_devid(ctrlr->dev) == 0x09538086 && ctrlr->cdata.vs[3] != 0)
491 (1 << ctrlr->cdata.vs[3]) * ctrlr->min_page_size;
494 * Namespaces are reconstructed after a controller reset, so check
495 * to make sure we only call mtx_init once on each mtx.
497 * TODO: Move this somewhere where it gets called at controller
498 * construction time, which is not invoked as part of each
501 if (!mtx_initialized(&ns->lock))
502 mtx_init(&ns->lock, "nvme ns lock", NULL, MTX_DEF);
505 nvme_ctrlr_cmd_identify_namespace(ctrlr, id, &ns->data,
506 nvme_completion_poll_cb, &status);
507 while (status.done == FALSE)
509 if (nvme_completion_is_error(&status.cpl)) {
510 nvme_printf(ctrlr, "nvme_identify_namespace failed\n");
515 * Note: format is a 0-based value, so > is appropriate here,
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);
524 if (ctrlr->cdata.oncs.dsm)
525 ns->flags |= NVME_NS_DEALLOCATE_SUPPORTED;
527 if (ctrlr->cdata.vwc.present)
528 ns->flags |= NVME_NS_FLUSH_SUPPORTED;
531 * cdev may have already been created, if we are reconstructing the
532 * namespace after a controller-level reset.
534 if (ns->cdev != NULL)
538 * Namespace IDs start at 1, so we need to subtract 1 to create a
539 * correct unit number.
541 unit = device_get_unit(ctrlr->dev) * NVME_MAX_NAMESPACES + ns->id - 1;
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.
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);
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);
558 #ifdef NVME_UNMAPPED_BIO_SUPPORT
559 ns->cdev->si_flags |= SI_UNMAPPED;
562 if (ns->cdev != NULL)
563 ns->cdev->si_drv1 = ns;
568 void nvme_ns_destruct(struct nvme_namespace *ns)
571 if (ns->cdev != NULL)
572 destroy_dev(ns->cdev);