2 * Copyright (C) 2012-2016 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>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <sys/sysctl.h>
36 #include <sys/systm.h>
37 #include <sys/taskqueue.h>
39 #include <geom/geom.h>
40 #include <geom/geom_disk.h>
42 #include <dev/nvme/nvme.h>
48 static disk_ioctl_t nvd_ioctl;
49 static disk_strategy_t nvd_strategy;
50 static dumper_t nvd_dump;
52 static void nvd_done(void *arg, const struct nvme_completion *cpl);
54 static void *nvd_new_disk(struct nvme_namespace *ns, void *ctrlr);
55 static void destroy_geom_disk(struct nvd_disk *ndisk);
57 static void *nvd_new_controller(struct nvme_controller *ctrlr);
58 static void nvd_controller_fail(void *ctrlr);
60 static int nvd_load(void);
61 static void nvd_unload(void);
63 MALLOC_DEFINE(M_NVD, "nvd", "nvd(4) allocations");
65 struct nvme_consumer *consumer_handle;
69 struct bio_queue_head bioq;
75 struct nvme_namespace *ns;
78 uint32_t ordered_in_flight;
80 TAILQ_ENTRY(nvd_disk) global_tailq;
81 TAILQ_ENTRY(nvd_disk) ctrlr_tailq;
84 struct nvd_controller {
86 TAILQ_ENTRY(nvd_controller) tailq;
87 TAILQ_HEAD(, nvd_disk) disk_head;
90 static TAILQ_HEAD(, nvd_controller) ctrlr_head;
91 static TAILQ_HEAD(disk_list, nvd_disk) disk_head;
93 static SYSCTL_NODE(_hw, OID_AUTO, nvd, CTLFLAG_RD, 0, "nvd driver parameters");
95 * The NVMe specification does not define a maximum or optimal delete size, so
96 * technically max delete size is min(full size of the namespace, 2^32 - 1
97 * LBAs). A single delete for a multi-TB NVMe namespace though may take much
98 * longer to complete than the nvme(4) I/O timeout period. So choose a sensible
99 * default here that is still suitably large to minimize the number of overall
102 static uint64_t nvd_delete_max = (1024 * 1024 * 1024); /* 1GB */
103 SYSCTL_UQUAD(_hw_nvd, OID_AUTO, delete_max, CTLFLAG_RDTUN, &nvd_delete_max, 0,
104 "nvd maximum BIO_DELETE size in bytes");
106 static int nvd_modevent(module_t mod, int type, void *arg)
124 moduledata_t nvd_mod = {
126 (modeventhand_t)nvd_modevent,
130 DECLARE_MODULE(nvd, nvd_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
131 MODULE_VERSION(nvd, 1);
132 MODULE_DEPEND(nvd, nvme, 1, 1, 1);
138 TAILQ_INIT(&ctrlr_head);
139 TAILQ_INIT(&disk_head);
141 consumer_handle = nvme_register_consumer(nvd_new_disk,
142 nvd_new_controller, NULL, nvd_controller_fail);
144 return (consumer_handle != NULL ? 0 : -1);
150 struct nvd_controller *ctrlr;
151 struct nvd_disk *disk;
153 while (!TAILQ_EMPTY(&ctrlr_head)) {
154 ctrlr = TAILQ_FIRST(&ctrlr_head);
155 TAILQ_REMOVE(&ctrlr_head, ctrlr, tailq);
159 while (!TAILQ_EMPTY(&disk_head)) {
160 disk = TAILQ_FIRST(&disk_head);
161 TAILQ_REMOVE(&disk_head, disk, global_tailq);
162 destroy_geom_disk(disk);
166 nvme_unregister_consumer(consumer_handle);
170 nvd_bio_submit(struct nvd_disk *ndisk, struct bio *bp)
174 bp->bio_driver1 = NULL;
175 atomic_add_int(&ndisk->cur_depth, 1);
176 err = nvme_ns_bio_process(ndisk->ns, bp, nvd_done);
178 atomic_add_int(&ndisk->cur_depth, -1);
179 if (__predict_false(bp->bio_flags & BIO_ORDERED))
180 atomic_add_int(&ndisk->ordered_in_flight, -1);
182 bp->bio_flags |= BIO_ERROR;
183 bp->bio_resid = bp->bio_bcount;
192 nvd_strategy(struct bio *bp)
194 struct nvd_disk *ndisk;
196 ndisk = (struct nvd_disk *)bp->bio_disk->d_drv1;
198 if (__predict_false(bp->bio_flags & BIO_ORDERED))
199 atomic_add_int(&ndisk->ordered_in_flight, 1);
201 if (__predict_true(ndisk->ordered_in_flight == 0)) {
202 nvd_bio_submit(ndisk, bp);
207 * There are ordered bios in flight, so we need to submit
208 * bios through the task queue to enforce ordering.
210 mtx_lock(&ndisk->bioqlock);
211 bioq_insert_tail(&ndisk->bioq, bp);
212 mtx_unlock(&ndisk->bioqlock);
213 taskqueue_enqueue(ndisk->tq, &ndisk->bioqtask);
217 nvd_ioctl(struct disk *ndisk, u_long cmd, void *data, int fflag,
231 nvd_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len)
233 struct nvd_disk *ndisk;
239 return (nvme_ns_dump(ndisk->ns, virt, offset, len));
243 nvd_done(void *arg, const struct nvme_completion *cpl)
246 struct nvd_disk *ndisk;
248 bp = (struct bio *)arg;
250 ndisk = bp->bio_disk->d_drv1;
252 atomic_add_int(&ndisk->cur_depth, -1);
253 if (__predict_false(bp->bio_flags & BIO_ORDERED))
254 atomic_add_int(&ndisk->ordered_in_flight, -1);
260 nvd_bioq_process(void *arg, int pending)
262 struct nvd_disk *ndisk = arg;
266 mtx_lock(&ndisk->bioqlock);
267 bp = bioq_takefirst(&ndisk->bioq);
268 mtx_unlock(&ndisk->bioqlock);
272 if (nvd_bio_submit(ndisk, bp) != 0) {
278 * BIO_ORDERED flag dictates that the bio with BIO_ORDERED
279 * flag set must be completed before proceeding with
282 if (bp->bio_flags & BIO_ORDERED) {
283 while (ndisk->cur_depth > 0) {
284 pause("nvd flush", 1);
292 nvd_new_controller(struct nvme_controller *ctrlr)
294 struct nvd_controller *nvd_ctrlr;
296 nvd_ctrlr = malloc(sizeof(struct nvd_controller), M_NVD,
299 TAILQ_INIT(&nvd_ctrlr->disk_head);
300 TAILQ_INSERT_TAIL(&ctrlr_head, nvd_ctrlr, tailq);
306 nvd_new_disk(struct nvme_namespace *ns, void *ctrlr_arg)
308 uint8_t descr[NVME_MODEL_NUMBER_LENGTH+1];
309 struct nvd_disk *ndisk;
311 struct nvd_controller *ctrlr = ctrlr_arg;
313 ndisk = malloc(sizeof(struct nvd_disk), M_NVD, M_ZERO | M_WAITOK);
316 disk->d_strategy = nvd_strategy;
317 disk->d_ioctl = nvd_ioctl;
318 disk->d_dump = nvd_dump;
319 disk->d_name = NVD_STR;
320 disk->d_drv1 = ndisk;
322 disk->d_maxsize = nvme_ns_get_max_io_xfer_size(ns);
323 disk->d_sectorsize = nvme_ns_get_sector_size(ns);
324 disk->d_mediasize = (off_t)nvme_ns_get_size(ns);
325 disk->d_delmaxsize = (off_t)nvme_ns_get_size(ns);
326 if (disk->d_delmaxsize > nvd_delete_max)
327 disk->d_delmaxsize = nvd_delete_max;
328 disk->d_stripesize = nvme_ns_get_stripesize(ns);
330 if (TAILQ_EMPTY(&disk_head))
334 TAILQ_LAST(&disk_head, disk_list)->disk->d_unit + 1;
336 disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
338 if (nvme_ns_get_flags(ns) & NVME_NS_DEALLOCATE_SUPPORTED)
339 disk->d_flags |= DISKFLAG_CANDELETE;
341 if (nvme_ns_get_flags(ns) & NVME_NS_FLUSH_SUPPORTED)
342 disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
344 /* ifdef used here to ease porting to stable branches at a later point. */
345 #ifdef DISKFLAG_UNMAPPED_BIO
346 disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
350 * d_ident and d_descr are both far bigger than the length of either
351 * the serial or model number strings.
353 nvme_strvis(disk->d_ident, nvme_ns_get_serial_number(ns),
354 sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH);
356 nvme_strvis(descr, nvme_ns_get_model_number(ns), sizeof(descr),
357 NVME_MODEL_NUMBER_LENGTH);
359 #if __FreeBSD_version >= 900034
360 strlcpy(disk->d_descr, descr, sizeof(descr));
365 ndisk->cur_depth = 0;
366 ndisk->ordered_in_flight = 0;
368 mtx_init(&ndisk->bioqlock, "NVD bioq lock", NULL, MTX_DEF);
369 bioq_init(&ndisk->bioq);
371 TASK_INIT(&ndisk->bioqtask, 0, nvd_bioq_process, ndisk);
372 ndisk->tq = taskqueue_create("nvd_taskq", M_WAITOK,
373 taskqueue_thread_enqueue, &ndisk->tq);
374 taskqueue_start_threads(&ndisk->tq, 1, PI_DISK, "nvd taskq");
376 TAILQ_INSERT_TAIL(&disk_head, ndisk, global_tailq);
377 TAILQ_INSERT_TAIL(&ctrlr->disk_head, ndisk, ctrlr_tailq);
379 disk_create(disk, DISK_VERSION);
381 printf(NVD_STR"%u: <%s> NVMe namespace\n", disk->d_unit, descr);
382 printf(NVD_STR"%u: %juMB (%ju %u byte sectors)\n", disk->d_unit,
383 (uintmax_t)disk->d_mediasize / (1024*1024),
384 (uintmax_t)disk->d_mediasize / disk->d_sectorsize,
391 destroy_geom_disk(struct nvd_disk *ndisk)
400 taskqueue_free(ndisk->tq);
402 disk_destroy(ndisk->disk);
404 mtx_lock(&ndisk->bioqlock);
406 bp = bioq_takefirst(&ndisk->bioq);
410 bp->bio_flags |= BIO_ERROR;
411 bp->bio_resid = bp->bio_bcount;
416 printf(NVD_STR"%u: lost device - %d outstanding\n", unit, cnt);
417 printf(NVD_STR"%u: removing device entry\n", unit);
419 mtx_unlock(&ndisk->bioqlock);
421 mtx_destroy(&ndisk->bioqlock);
425 nvd_controller_fail(void *ctrlr_arg)
427 struct nvd_controller *ctrlr = ctrlr_arg;
428 struct nvd_disk *disk;
430 while (!TAILQ_EMPTY(&ctrlr->disk_head)) {
431 disk = TAILQ_FIRST(&ctrlr->disk_head);
432 TAILQ_REMOVE(&disk_head, disk, global_tailq);
433 TAILQ_REMOVE(&ctrlr->disk_head, disk, ctrlr_tailq);
434 destroy_geom_disk(disk);
438 TAILQ_REMOVE(&ctrlr_head, ctrlr, tailq);