2012-09-17 19:23:01 +00:00
|
|
|
/*-
|
2013-07-09 21:22:17 +00:00
|
|
|
* Copyright (C) 2012-2013 Intel Corporation
|
2012-09-17 19:23:01 +00:00
|
|
|
* 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/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/taskqueue.h>
|
|
|
|
|
|
|
|
#include <geom/geom.h>
|
|
|
|
#include <geom/geom_disk.h>
|
|
|
|
|
|
|
|
#include <dev/nvme/nvme.h>
|
|
|
|
|
2013-07-19 21:40:57 +00:00
|
|
|
#define NVD_STR "nvd"
|
|
|
|
|
2012-09-17 19:23:01 +00:00
|
|
|
struct nvd_disk;
|
|
|
|
|
|
|
|
static disk_ioctl_t nvd_ioctl;
|
|
|
|
static disk_strategy_t nvd_strategy;
|
|
|
|
|
2013-03-26 21:45:37 +00:00
|
|
|
static void *nvd_new_disk(struct nvme_namespace *ns, void *ctrlr);
|
2012-09-17 19:23:01 +00:00
|
|
|
static void destroy_geom_disk(struct nvd_disk *ndisk);
|
|
|
|
|
2013-03-26 21:45:37 +00:00
|
|
|
static void *nvd_new_controller(struct nvme_controller *ctrlr);
|
2013-03-26 21:58:38 +00:00
|
|
|
static void nvd_controller_fail(void *ctrlr);
|
2013-03-26 21:45:37 +00:00
|
|
|
|
2012-09-17 19:23:01 +00:00
|
|
|
static int nvd_load(void);
|
|
|
|
static void nvd_unload(void);
|
|
|
|
|
|
|
|
MALLOC_DEFINE(M_NVD, "nvd", "nvd(4) allocations");
|
|
|
|
|
|
|
|
struct nvme_consumer *consumer_handle;
|
|
|
|
|
|
|
|
struct nvd_disk {
|
|
|
|
|
|
|
|
struct bio_queue_head bioq;
|
|
|
|
struct task bioqtask;
|
|
|
|
struct mtx bioqlock;
|
|
|
|
|
|
|
|
struct disk *disk;
|
|
|
|
struct taskqueue *tq;
|
|
|
|
struct nvme_namespace *ns;
|
|
|
|
|
|
|
|
uint32_t cur_depth;
|
|
|
|
|
2013-03-26 21:45:37 +00:00
|
|
|
TAILQ_ENTRY(nvd_disk) global_tailq;
|
|
|
|
TAILQ_ENTRY(nvd_disk) ctrlr_tailq;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nvd_controller {
|
|
|
|
|
|
|
|
TAILQ_ENTRY(nvd_controller) tailq;
|
|
|
|
TAILQ_HEAD(, nvd_disk) disk_head;
|
2012-09-17 19:23:01 +00:00
|
|
|
};
|
|
|
|
|
2013-03-26 21:45:37 +00:00
|
|
|
static TAILQ_HEAD(, nvd_controller) ctrlr_head;
|
|
|
|
static TAILQ_HEAD(disk_list, nvd_disk) disk_head;
|
2012-09-17 19:23:01 +00:00
|
|
|
|
|
|
|
static int nvd_modevent(module_t mod, int type, void *arg)
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
|
|
|
error = nvd_load();
|
|
|
|
break;
|
|
|
|
case MOD_UNLOAD:
|
|
|
|
nvd_unload();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
moduledata_t nvd_mod = {
|
2013-07-19 21:40:57 +00:00
|
|
|
NVD_STR,
|
2012-09-17 19:23:01 +00:00
|
|
|
(modeventhand_t)nvd_modevent,
|
2012-10-10 08:36:38 +00:00
|
|
|
0
|
2012-09-17 19:23:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DECLARE_MODULE(nvd, nvd_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
|
|
|
|
MODULE_VERSION(nvd, 1);
|
|
|
|
MODULE_DEPEND(nvd, nvme, 1, 1, 1);
|
|
|
|
|
|
|
|
static int
|
|
|
|
nvd_load()
|
|
|
|
{
|
|
|
|
|
2013-03-26 21:45:37 +00:00
|
|
|
TAILQ_INIT(&ctrlr_head);
|
|
|
|
TAILQ_INIT(&disk_head);
|
|
|
|
|
|
|
|
consumer_handle = nvme_register_consumer(nvd_new_disk,
|
2013-03-26 21:58:38 +00:00
|
|
|
nvd_new_controller, NULL, nvd_controller_fail);
|
2012-09-17 19:23:01 +00:00
|
|
|
|
|
|
|
return (consumer_handle != NULL ? 0 : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nvd_unload()
|
|
|
|
{
|
2013-03-26 21:45:37 +00:00
|
|
|
struct nvd_controller *ctrlr;
|
|
|
|
struct nvd_disk *disk;
|
|
|
|
|
|
|
|
while (!TAILQ_EMPTY(&ctrlr_head)) {
|
|
|
|
ctrlr = TAILQ_FIRST(&ctrlr_head);
|
|
|
|
TAILQ_REMOVE(&ctrlr_head, ctrlr, tailq);
|
|
|
|
free(ctrlr, M_NVD);
|
|
|
|
}
|
2012-09-17 19:23:01 +00:00
|
|
|
|
2013-03-26 21:45:37 +00:00
|
|
|
while (!TAILQ_EMPTY(&disk_head)) {
|
|
|
|
disk = TAILQ_FIRST(&disk_head);
|
|
|
|
TAILQ_REMOVE(&disk_head, disk, global_tailq);
|
|
|
|
destroy_geom_disk(disk);
|
|
|
|
free(disk, M_NVD);
|
2012-09-17 19:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nvme_unregister_consumer(consumer_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nvd_strategy(struct bio *bp)
|
|
|
|
{
|
|
|
|
struct nvd_disk *ndisk;
|
|
|
|
|
|
|
|
ndisk = (struct nvd_disk *)bp->bio_disk->d_drv1;
|
|
|
|
|
|
|
|
mtx_lock(&ndisk->bioqlock);
|
|
|
|
bioq_insert_tail(&ndisk->bioq, bp);
|
|
|
|
mtx_unlock(&ndisk->bioqlock);
|
|
|
|
taskqueue_enqueue(ndisk->tq, &ndisk->bioqtask);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nvd_ioctl(struct disk *ndisk, u_long cmd, void *data, int fflag,
|
|
|
|
struct thread *td)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
default:
|
|
|
|
ret = EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-03-26 21:00:18 +00:00
|
|
|
nvd_done(void *arg, const struct nvme_completion *cpl)
|
2012-09-17 19:23:01 +00:00
|
|
|
{
|
|
|
|
struct bio *bp;
|
|
|
|
struct nvd_disk *ndisk;
|
|
|
|
|
|
|
|
bp = (struct bio *)arg;
|
|
|
|
|
|
|
|
ndisk = bp->bio_disk->d_drv1;
|
|
|
|
|
2012-10-18 00:45:53 +00:00
|
|
|
atomic_add_int(&ndisk->cur_depth, -1);
|
2012-09-17 19:23:01 +00:00
|
|
|
|
|
|
|
biodone(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nvd_bioq_process(void *arg, int pending)
|
|
|
|
{
|
|
|
|
struct nvd_disk *ndisk = arg;
|
|
|
|
struct bio *bp;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
mtx_lock(&ndisk->bioqlock);
|
|
|
|
bp = bioq_takefirst(&ndisk->bioq);
|
|
|
|
mtx_unlock(&ndisk->bioqlock);
|
|
|
|
if (bp == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef BIO_ORDERED
|
|
|
|
/*
|
|
|
|
* BIO_ORDERED flag dictates that all outstanding bios
|
|
|
|
* must be completed before processing the bio with
|
|
|
|
* BIO_ORDERED flag set.
|
|
|
|
*/
|
|
|
|
if (bp->bio_flags & BIO_ORDERED) {
|
|
|
|
while (ndisk->cur_depth > 0) {
|
|
|
|
pause("nvd flush", 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bp->bio_driver1 = NULL;
|
2012-10-18 00:45:53 +00:00
|
|
|
atomic_add_int(&ndisk->cur_depth, 1);
|
2012-09-17 19:23:01 +00:00
|
|
|
|
|
|
|
err = nvme_ns_bio_process(ndisk->ns, bp, nvd_done);
|
|
|
|
|
|
|
|
if (err) {
|
2012-10-18 00:45:53 +00:00
|
|
|
atomic_add_int(&ndisk->cur_depth, -1);
|
2012-10-18 00:32:07 +00:00
|
|
|
bp->bio_error = err;
|
2012-09-17 19:23:01 +00:00
|
|
|
bp->bio_flags |= BIO_ERROR;
|
|
|
|
bp->bio_resid = bp->bio_bcount;
|
|
|
|
biodone(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef BIO_ORDERED
|
|
|
|
/*
|
|
|
|
* BIO_ORDERED flag dictates that the bio with BIO_ORDERED
|
|
|
|
* flag set must be completed before proceeding with
|
|
|
|
* additional bios.
|
|
|
|
*/
|
|
|
|
if (bp->bio_flags & BIO_ORDERED) {
|
|
|
|
while (ndisk->cur_depth > 0) {
|
|
|
|
pause("nvd flush", 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 18:39:54 +00:00
|
|
|
static void *
|
2013-03-26 21:45:37 +00:00
|
|
|
nvd_new_controller(struct nvme_controller *ctrlr)
|
2012-09-17 19:23:01 +00:00
|
|
|
{
|
2013-03-26 21:45:37 +00:00
|
|
|
struct nvd_controller *nvd_ctrlr;
|
|
|
|
|
|
|
|
nvd_ctrlr = malloc(sizeof(struct nvd_controller), M_NVD,
|
2013-03-26 22:11:34 +00:00
|
|
|
M_ZERO | M_WAITOK);
|
2013-03-26 21:45:37 +00:00
|
|
|
|
|
|
|
TAILQ_INIT(&nvd_ctrlr->disk_head);
|
|
|
|
TAILQ_INSERT_TAIL(&ctrlr_head, nvd_ctrlr, tailq);
|
|
|
|
|
|
|
|
return (nvd_ctrlr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
nvd_new_disk(struct nvme_namespace *ns, void *ctrlr_arg)
|
|
|
|
{
|
2013-07-19 21:40:57 +00:00
|
|
|
uint8_t descr[NVME_MODEL_NUMBER_LENGTH+1];
|
2013-03-26 21:45:37 +00:00
|
|
|
struct nvd_disk *ndisk;
|
|
|
|
struct disk *disk;
|
|
|
|
struct nvd_controller *ctrlr = ctrlr_arg;
|
2012-09-17 19:23:01 +00:00
|
|
|
|
2013-03-26 22:11:34 +00:00
|
|
|
ndisk = malloc(sizeof(struct nvd_disk), M_NVD, M_ZERO | M_WAITOK);
|
2012-09-17 19:23:01 +00:00
|
|
|
|
|
|
|
disk = disk_alloc();
|
|
|
|
disk->d_strategy = nvd_strategy;
|
|
|
|
disk->d_ioctl = nvd_ioctl;
|
2013-07-19 21:40:57 +00:00
|
|
|
disk->d_name = NVD_STR;
|
2012-09-17 19:23:01 +00:00
|
|
|
disk->d_drv1 = ndisk;
|
|
|
|
|
|
|
|
disk->d_maxsize = nvme_ns_get_max_io_xfer_size(ns);
|
|
|
|
disk->d_sectorsize = nvme_ns_get_sector_size(ns);
|
|
|
|
disk->d_mediasize = (off_t)nvme_ns_get_size(ns);
|
|
|
|
|
2013-03-26 21:45:37 +00:00
|
|
|
if (TAILQ_EMPTY(&disk_head))
|
2012-09-17 19:23:01 +00:00
|
|
|
disk->d_unit = 0;
|
|
|
|
else
|
2013-03-26 21:45:37 +00:00
|
|
|
disk->d_unit =
|
|
|
|
TAILQ_LAST(&disk_head, disk_list)->disk->d_unit + 1;
|
2012-09-17 19:23:01 +00:00
|
|
|
|
|
|
|
disk->d_flags = 0;
|
|
|
|
|
|
|
|
if (nvme_ns_get_flags(ns) & NVME_NS_DEALLOCATE_SUPPORTED)
|
|
|
|
disk->d_flags |= DISKFLAG_CANDELETE;
|
|
|
|
|
|
|
|
if (nvme_ns_get_flags(ns) & NVME_NS_FLUSH_SUPPORTED)
|
|
|
|
disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
|
|
|
|
|
2013-04-01 16:23:34 +00:00
|
|
|
/* ifdef used here to ease porting to stable branches at a later point. */
|
|
|
|
#ifdef DISKFLAG_UNMAPPED_BIO
|
|
|
|
disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
|
|
|
|
#endif
|
|
|
|
|
2013-07-17 23:23:38 +00:00
|
|
|
/*
|
|
|
|
* d_ident and d_descr are both far bigger than the length of either
|
|
|
|
* the serial or model number strings.
|
|
|
|
*/
|
2013-07-19 21:40:57 +00:00
|
|
|
nvme_strvis(disk->d_ident, nvme_ns_get_serial_number(ns),
|
|
|
|
sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH);
|
|
|
|
|
|
|
|
nvme_strvis(descr, nvme_ns_get_model_number(ns), sizeof(descr),
|
|
|
|
NVME_MODEL_NUMBER_LENGTH);
|
2012-09-17 19:23:01 +00:00
|
|
|
|
|
|
|
#if __FreeBSD_version >= 900034
|
2013-07-19 21:40:57 +00:00
|
|
|
strlcpy(disk->d_descr, descr, sizeof(descr));
|
2012-09-17 19:23:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ndisk->ns = ns;
|
|
|
|
ndisk->disk = disk;
|
|
|
|
ndisk->cur_depth = 0;
|
|
|
|
|
|
|
|
mtx_init(&ndisk->bioqlock, "NVD bioq lock", NULL, MTX_DEF);
|
|
|
|
bioq_init(&ndisk->bioq);
|
|
|
|
|
|
|
|
TASK_INIT(&ndisk->bioqtask, 0, nvd_bioq_process, ndisk);
|
|
|
|
ndisk->tq = taskqueue_create("nvd_taskq", M_WAITOK,
|
|
|
|
taskqueue_thread_enqueue, &ndisk->tq);
|
|
|
|
taskqueue_start_threads(&ndisk->tq, 1, PI_DISK, "nvd taskq");
|
|
|
|
|
2013-03-26 21:45:37 +00:00
|
|
|
TAILQ_INSERT_TAIL(&disk_head, ndisk, global_tailq);
|
|
|
|
TAILQ_INSERT_TAIL(&ctrlr->disk_head, ndisk, ctrlr_tailq);
|
2013-03-26 18:39:54 +00:00
|
|
|
|
2013-07-19 21:30:53 +00:00
|
|
|
disk_create(disk, DISK_VERSION);
|
|
|
|
|
2013-07-19 21:40:57 +00:00
|
|
|
printf(NVD_STR"%u: <%s> NVMe namespace\n", disk->d_unit, descr);
|
|
|
|
printf(NVD_STR"%u: %juMB (%ju %u byte sectors)\n", disk->d_unit,
|
|
|
|
(uintmax_t)disk->d_mediasize / (1024*1024),
|
|
|
|
(uintmax_t)disk->d_mediasize / disk->d_sectorsize,
|
|
|
|
disk->d_sectorsize);
|
|
|
|
|
2013-03-26 18:39:54 +00:00
|
|
|
return (NULL);
|
2012-09-17 19:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
destroy_geom_disk(struct nvd_disk *ndisk)
|
|
|
|
{
|
2013-07-19 21:40:57 +00:00
|
|
|
struct bio *bp;
|
|
|
|
struct disk *disk;
|
|
|
|
uint32_t unit;
|
|
|
|
int cnt = 0;
|
2012-09-17 19:23:01 +00:00
|
|
|
|
2013-07-19 21:40:57 +00:00
|
|
|
disk = ndisk->disk;
|
|
|
|
unit = disk->d_unit;
|
2012-09-17 19:23:01 +00:00
|
|
|
taskqueue_free(ndisk->tq);
|
2013-07-19 21:40:57 +00:00
|
|
|
|
2012-09-17 19:23:01 +00:00
|
|
|
disk_destroy(ndisk->disk);
|
|
|
|
|
|
|
|
mtx_lock(&ndisk->bioqlock);
|
|
|
|
for (;;) {
|
|
|
|
bp = bioq_takefirst(&ndisk->bioq);
|
|
|
|
if (bp == NULL)
|
|
|
|
break;
|
|
|
|
bp->bio_error = EIO;
|
|
|
|
bp->bio_flags |= BIO_ERROR;
|
|
|
|
bp->bio_resid = bp->bio_bcount;
|
2013-07-19 21:40:57 +00:00
|
|
|
cnt++;
|
2012-09-17 19:23:01 +00:00
|
|
|
biodone(bp);
|
|
|
|
}
|
2013-07-19 21:40:57 +00:00
|
|
|
|
|
|
|
printf(NVD_STR"%u: lost device - %d outstanding\n", unit, cnt);
|
|
|
|
printf(NVD_STR"%u: removing device entry\n", unit);
|
|
|
|
|
2012-09-17 19:23:01 +00:00
|
|
|
mtx_unlock(&ndisk->bioqlock);
|
|
|
|
|
|
|
|
mtx_destroy(&ndisk->bioqlock);
|
|
|
|
}
|
2013-03-26 21:58:38 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
nvd_controller_fail(void *ctrlr_arg)
|
|
|
|
{
|
|
|
|
struct nvd_controller *ctrlr = ctrlr_arg;
|
|
|
|
struct nvd_disk *disk;
|
|
|
|
|
|
|
|
while (!TAILQ_EMPTY(&ctrlr->disk_head)) {
|
|
|
|
disk = TAILQ_FIRST(&ctrlr->disk_head);
|
|
|
|
TAILQ_REMOVE(&disk_head, disk, global_tailq);
|
|
|
|
TAILQ_REMOVE(&ctrlr->disk_head, disk, ctrlr_tailq);
|
|
|
|
destroy_geom_disk(disk);
|
|
|
|
free(disk, M_NVD);
|
|
|
|
}
|
|
|
|
|
|
|
|
TAILQ_REMOVE(&ctrlr_head, ctrlr, tailq);
|
|
|
|
free(ctrlr, M_NVD);
|
|
|
|
}
|
|
|
|
|