1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 2012-2016 Intel Corporation 5 * All rights reserved. 6 * Copyright (C) 2018 Alexander Motin <mav@FreeBSD.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/bio.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/queue.h> 39 #include <sys/sysctl.h> 40 #include <sys/systm.h> 41 #include <sys/taskqueue.h> 42 #include <machine/atomic.h> 43 44 #include <geom/geom.h> 45 #include <geom/geom_disk.h> 46 47 #include <dev/nvme/nvme.h> 48 49 #define NVD_STR "nvd" 50 51 struct nvd_disk; 52 struct nvd_controller; 53 54 static disk_ioctl_t nvd_ioctl; 55 static disk_strategy_t nvd_strategy; 56 static dumper_t nvd_dump; 57 static disk_getattr_t nvd_getattr; 58 59 static void nvd_done(void *arg, const struct nvme_completion *cpl); 60 static void nvd_gone(struct nvd_disk *ndisk); 61 62 static void *nvd_new_disk(struct nvme_namespace *ns, void *ctrlr); 63 64 static void *nvd_new_controller(struct nvme_controller *ctrlr); 65 static void nvd_controller_fail(void *ctrlr); 66 67 static int nvd_load(void); 68 static void nvd_unload(void); 69 70 MALLOC_DEFINE(M_NVD, "nvd", "nvd(4) allocations"); 71 72 struct nvme_consumer *consumer_handle; 73 74 struct nvd_disk { 75 struct nvd_controller *ctrlr; 76 77 struct bio_queue_head bioq; 78 struct task bioqtask; 79 struct mtx bioqlock; 80 81 struct disk *disk; 82 struct taskqueue *tq; 83 struct nvme_namespace *ns; 84 85 uint32_t cur_depth; 86 #define NVD_ODEPTH (1 << 30) 87 uint32_t ordered_in_flight; 88 u_int unit; 89 90 TAILQ_ENTRY(nvd_disk) global_tailq; 91 TAILQ_ENTRY(nvd_disk) ctrlr_tailq; 92 }; 93 94 struct nvd_controller { 95 96 TAILQ_ENTRY(nvd_controller) tailq; 97 TAILQ_HEAD(, nvd_disk) disk_head; 98 }; 99 100 static struct mtx nvd_lock; 101 static TAILQ_HEAD(, nvd_controller) ctrlr_head; 102 static TAILQ_HEAD(disk_list, nvd_disk) disk_head; 103 104 static SYSCTL_NODE(_hw, OID_AUTO, nvd, CTLFLAG_RD, 0, "nvd driver parameters"); 105 /* 106 * The NVMe specification does not define a maximum or optimal delete size, so 107 * technically max delete size is min(full size of the namespace, 2^32 - 1 108 * LBAs). A single delete for a multi-TB NVMe namespace though may take much 109 * longer to complete than the nvme(4) I/O timeout period. So choose a sensible 110 * default here that is still suitably large to minimize the number of overall 111 * delete operations. 112 */ 113 static uint64_t nvd_delete_max = (1024 * 1024 * 1024); /* 1GB */ 114 SYSCTL_UQUAD(_hw_nvd, OID_AUTO, delete_max, CTLFLAG_RDTUN, &nvd_delete_max, 0, 115 "nvd maximum BIO_DELETE size in bytes"); 116 117 static int nvd_modevent(module_t mod, int type, void *arg) 118 { 119 int error = 0; 120 121 switch (type) { 122 case MOD_LOAD: 123 error = nvd_load(); 124 break; 125 case MOD_UNLOAD: 126 nvd_unload(); 127 break; 128 default: 129 break; 130 } 131 132 return (error); 133 } 134 135 moduledata_t nvd_mod = { 136 NVD_STR, 137 (modeventhand_t)nvd_modevent, 138 0 139 }; 140 141 DECLARE_MODULE(nvd, nvd_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); 142 MODULE_VERSION(nvd, 1); 143 MODULE_DEPEND(nvd, nvme, 1, 1, 1); 144 145 static int 146 nvd_load() 147 { 148 if (!nvme_use_nvd) 149 return 0; 150 151 mtx_init(&nvd_lock, "nvd_lock", NULL, MTX_DEF); 152 TAILQ_INIT(&ctrlr_head); 153 TAILQ_INIT(&disk_head); 154 155 consumer_handle = nvme_register_consumer(nvd_new_disk, 156 nvd_new_controller, NULL, nvd_controller_fail); 157 158 return (consumer_handle != NULL ? 0 : -1); 159 } 160 161 static void 162 nvd_unload() 163 { 164 struct nvd_controller *ctrlr; 165 struct nvd_disk *ndisk; 166 167 if (!nvme_use_nvd) 168 return; 169 170 mtx_lock(&nvd_lock); 171 while ((ctrlr = TAILQ_FIRST(&ctrlr_head)) != NULL) { 172 TAILQ_REMOVE(&ctrlr_head, ctrlr, tailq); 173 TAILQ_FOREACH(ndisk, &ctrlr->disk_head, ctrlr_tailq) 174 nvd_gone(ndisk); 175 while (!TAILQ_EMPTY(&ctrlr->disk_head)) 176 msleep(&ctrlr->disk_head, &nvd_lock, 0, "nvd_unload",0); 177 free(ctrlr, M_NVD); 178 } 179 mtx_unlock(&nvd_lock); 180 181 nvme_unregister_consumer(consumer_handle); 182 183 mtx_destroy(&nvd_lock); 184 } 185 186 static void 187 nvd_bio_submit(struct nvd_disk *ndisk, struct bio *bp) 188 { 189 int err; 190 191 bp->bio_driver1 = NULL; 192 if (__predict_false(bp->bio_flags & BIO_ORDERED)) 193 atomic_add_int(&ndisk->cur_depth, NVD_ODEPTH); 194 else 195 atomic_add_int(&ndisk->cur_depth, 1); 196 err = nvme_ns_bio_process(ndisk->ns, bp, nvd_done); 197 if (err) { 198 if (__predict_false(bp->bio_flags & BIO_ORDERED)) { 199 atomic_add_int(&ndisk->cur_depth, -NVD_ODEPTH); 200 atomic_add_int(&ndisk->ordered_in_flight, -1); 201 wakeup(&ndisk->cur_depth); 202 } else { 203 if (atomic_fetchadd_int(&ndisk->cur_depth, -1) == 1 && 204 __predict_false(ndisk->ordered_in_flight != 0)) 205 wakeup(&ndisk->cur_depth); 206 } 207 bp->bio_error = err; 208 bp->bio_flags |= BIO_ERROR; 209 bp->bio_resid = bp->bio_bcount; 210 biodone(bp); 211 } 212 } 213 214 static void 215 nvd_strategy(struct bio *bp) 216 { 217 struct nvd_disk *ndisk = (struct nvd_disk *)bp->bio_disk->d_drv1; 218 219 /* 220 * bio with BIO_ORDERED flag must be executed after all previous 221 * bios in the queue, and before any successive bios. 222 */ 223 if (__predict_false(bp->bio_flags & BIO_ORDERED)) { 224 if (atomic_fetchadd_int(&ndisk->ordered_in_flight, 1) == 0 && 225 ndisk->cur_depth == 0 && bioq_first(&ndisk->bioq) == NULL) { 226 nvd_bio_submit(ndisk, bp); 227 return; 228 } 229 } else if (__predict_true(ndisk->ordered_in_flight == 0)) { 230 nvd_bio_submit(ndisk, bp); 231 return; 232 } 233 234 /* 235 * There are ordered bios in flight, so we need to submit 236 * bios through the task queue to enforce ordering. 237 */ 238 mtx_lock(&ndisk->bioqlock); 239 bioq_insert_tail(&ndisk->bioq, bp); 240 mtx_unlock(&ndisk->bioqlock); 241 taskqueue_enqueue(ndisk->tq, &ndisk->bioqtask); 242 } 243 244 static void 245 nvd_gone(struct nvd_disk *ndisk) 246 { 247 struct bio *bp; 248 249 printf(NVD_STR"%u: detached\n", ndisk->unit); 250 mtx_lock(&ndisk->bioqlock); 251 disk_gone(ndisk->disk); 252 while ((bp = bioq_takefirst(&ndisk->bioq)) != NULL) { 253 if (__predict_false(bp->bio_flags & BIO_ORDERED)) 254 atomic_add_int(&ndisk->ordered_in_flight, -1); 255 bp->bio_error = ENXIO; 256 bp->bio_flags |= BIO_ERROR; 257 bp->bio_resid = bp->bio_bcount; 258 biodone(bp); 259 } 260 mtx_unlock(&ndisk->bioqlock); 261 } 262 263 static void 264 nvd_gonecb(struct disk *dp) 265 { 266 struct nvd_disk *ndisk = (struct nvd_disk *)dp->d_drv1; 267 268 disk_destroy(ndisk->disk); 269 mtx_lock(&nvd_lock); 270 TAILQ_REMOVE(&disk_head, ndisk, global_tailq); 271 TAILQ_REMOVE(&ndisk->ctrlr->disk_head, ndisk, ctrlr_tailq); 272 if (TAILQ_EMPTY(&ndisk->ctrlr->disk_head)) 273 wakeup(&ndisk->ctrlr->disk_head); 274 mtx_unlock(&nvd_lock); 275 taskqueue_free(ndisk->tq); 276 mtx_destroy(&ndisk->bioqlock); 277 free(ndisk, M_NVD); 278 } 279 280 static int 281 nvd_ioctl(struct disk *dp, u_long cmd, void *data, int fflag, 282 struct thread *td) 283 { 284 struct nvd_disk *ndisk = dp->d_drv1; 285 286 return (nvme_ns_ioctl_process(ndisk->ns, cmd, data, fflag, td)); 287 } 288 289 static int 290 nvd_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len) 291 { 292 struct disk *dp = arg; 293 struct nvd_disk *ndisk = dp->d_drv1; 294 295 return (nvme_ns_dump(ndisk->ns, virt, offset, len)); 296 } 297 298 static int 299 nvd_getattr(struct bio *bp) 300 { 301 struct nvd_disk *ndisk = (struct nvd_disk *)bp->bio_disk->d_drv1; 302 const struct nvme_namespace_data *nsdata; 303 u_int i; 304 305 if (!strcmp("GEOM::lunid", bp->bio_attribute)) { 306 nsdata = nvme_ns_get_data(ndisk->ns); 307 308 /* Try to return NGUID as lunid. */ 309 for (i = 0; i < sizeof(nsdata->nguid); i++) 310 if (nsdata->nguid[i] != 0) 311 if (i < sizeof(nsdata->nguid)) { 312 if (bp->bio_length < sizeof(nsdata->nguid) * 2 + 1) 313 return (EFAULT); 314 for (i = 0; i < sizeof(nsdata->nguid); i++) { 315 sprintf(&bp->bio_data[i * 2], "%02x", 316 nsdata->nguid[i]); 317 } 318 bp->bio_completed = bp->bio_length; 319 return (0); 320 } 321 322 /* Try to return EUI64 as lunid. */ 323 for (i = 0; i < sizeof(nsdata->eui64); i++) 324 if (nsdata->eui64[i] != 0) 325 if (i < sizeof(nsdata->eui64)) { 326 if (bp->bio_length < sizeof(nsdata->eui64) * 2 + 1) 327 return (EFAULT); 328 for (i = 0; i < sizeof(nsdata->eui64); i++) { 329 sprintf(&bp->bio_data[i * 2], "%02x", 330 nsdata->eui64[i]); 331 } 332 bp->bio_completed = bp->bio_length; 333 return (0); 334 } 335 } 336 return (-1); 337 } 338 339 static void 340 nvd_done(void *arg, const struct nvme_completion *cpl) 341 { 342 struct bio *bp = (struct bio *)arg; 343 struct nvd_disk *ndisk = bp->bio_disk->d_drv1; 344 345 if (__predict_false(bp->bio_flags & BIO_ORDERED)) { 346 atomic_add_int(&ndisk->cur_depth, -NVD_ODEPTH); 347 atomic_add_int(&ndisk->ordered_in_flight, -1); 348 wakeup(&ndisk->cur_depth); 349 } else { 350 if (atomic_fetchadd_int(&ndisk->cur_depth, -1) == 1 && 351 __predict_false(ndisk->ordered_in_flight != 0)) 352 wakeup(&ndisk->cur_depth); 353 } 354 355 biodone(bp); 356 } 357 358 static void 359 nvd_bioq_process(void *arg, int pending) 360 { 361 struct nvd_disk *ndisk = arg; 362 struct bio *bp; 363 364 for (;;) { 365 mtx_lock(&ndisk->bioqlock); 366 bp = bioq_takefirst(&ndisk->bioq); 367 mtx_unlock(&ndisk->bioqlock); 368 if (bp == NULL) 369 break; 370 371 if (__predict_false(bp->bio_flags & BIO_ORDERED)) { 372 /* 373 * bio with BIO_ORDERED flag set must be executed 374 * after all previous bios. 375 */ 376 while (ndisk->cur_depth > 0) 377 tsleep(&ndisk->cur_depth, 0, "nvdorb", 1); 378 } else { 379 /* 380 * bio with BIO_ORDERED flag set must be completed 381 * before proceeding with additional bios. 382 */ 383 while (ndisk->cur_depth >= NVD_ODEPTH) 384 tsleep(&ndisk->cur_depth, 0, "nvdora", 1); 385 } 386 387 nvd_bio_submit(ndisk, bp); 388 } 389 } 390 391 static void * 392 nvd_new_controller(struct nvme_controller *ctrlr) 393 { 394 struct nvd_controller *nvd_ctrlr; 395 396 nvd_ctrlr = malloc(sizeof(struct nvd_controller), M_NVD, 397 M_ZERO | M_WAITOK); 398 399 TAILQ_INIT(&nvd_ctrlr->disk_head); 400 mtx_lock(&nvd_lock); 401 TAILQ_INSERT_TAIL(&ctrlr_head, nvd_ctrlr, tailq); 402 mtx_unlock(&nvd_lock); 403 404 return (nvd_ctrlr); 405 } 406 407 static void * 408 nvd_new_disk(struct nvme_namespace *ns, void *ctrlr_arg) 409 { 410 uint8_t descr[NVME_MODEL_NUMBER_LENGTH+1]; 411 struct nvd_disk *ndisk, *tnd; 412 struct disk *disk; 413 struct nvd_controller *ctrlr = ctrlr_arg; 414 int unit; 415 416 ndisk = malloc(sizeof(struct nvd_disk), M_NVD, M_ZERO | M_WAITOK); 417 ndisk->ctrlr = ctrlr; 418 ndisk->ns = ns; 419 ndisk->cur_depth = 0; 420 ndisk->ordered_in_flight = 0; 421 mtx_init(&ndisk->bioqlock, "nvd bioq lock", NULL, MTX_DEF); 422 bioq_init(&ndisk->bioq); 423 TASK_INIT(&ndisk->bioqtask, 0, nvd_bioq_process, ndisk); 424 425 mtx_lock(&nvd_lock); 426 unit = 0; 427 TAILQ_FOREACH(tnd, &disk_head, global_tailq) { 428 if (tnd->unit > unit) 429 break; 430 unit = tnd->unit + 1; 431 } 432 ndisk->unit = unit; 433 if (tnd != NULL) 434 TAILQ_INSERT_BEFORE(tnd, ndisk, global_tailq); 435 else 436 TAILQ_INSERT_TAIL(&disk_head, ndisk, global_tailq); 437 TAILQ_INSERT_TAIL(&ctrlr->disk_head, ndisk, ctrlr_tailq); 438 mtx_unlock(&nvd_lock); 439 440 ndisk->tq = taskqueue_create("nvd_taskq", M_WAITOK, 441 taskqueue_thread_enqueue, &ndisk->tq); 442 taskqueue_start_threads(&ndisk->tq, 1, PI_DISK, "nvd taskq"); 443 444 disk = ndisk->disk = disk_alloc(); 445 disk->d_strategy = nvd_strategy; 446 disk->d_ioctl = nvd_ioctl; 447 disk->d_dump = nvd_dump; 448 disk->d_getattr = nvd_getattr; 449 disk->d_gone = nvd_gonecb; 450 disk->d_name = NVD_STR; 451 disk->d_unit = ndisk->unit; 452 disk->d_drv1 = ndisk; 453 454 disk->d_sectorsize = nvme_ns_get_sector_size(ns); 455 disk->d_mediasize = (off_t)nvme_ns_get_size(ns); 456 disk->d_maxsize = nvme_ns_get_max_io_xfer_size(ns); 457 disk->d_delmaxsize = (off_t)nvme_ns_get_size(ns); 458 if (disk->d_delmaxsize > nvd_delete_max) 459 disk->d_delmaxsize = nvd_delete_max; 460 disk->d_stripesize = nvme_ns_get_stripesize(ns); 461 disk->d_flags = DISKFLAG_UNMAPPED_BIO | DISKFLAG_DIRECT_COMPLETION; 462 if (nvme_ns_get_flags(ns) & NVME_NS_DEALLOCATE_SUPPORTED) 463 disk->d_flags |= DISKFLAG_CANDELETE; 464 if (nvme_ns_get_flags(ns) & NVME_NS_FLUSH_SUPPORTED) 465 disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 466 467 /* 468 * d_ident and d_descr are both far bigger than the length of either 469 * the serial or model number strings. 470 */ 471 nvme_strvis(disk->d_ident, nvme_ns_get_serial_number(ns), 472 sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH); 473 nvme_strvis(descr, nvme_ns_get_model_number(ns), sizeof(descr), 474 NVME_MODEL_NUMBER_LENGTH); 475 strlcpy(disk->d_descr, descr, sizeof(descr)); 476 477 disk->d_rotation_rate = DISK_RR_NON_ROTATING; 478 479 disk_create(disk, DISK_VERSION); 480 481 printf(NVD_STR"%u: <%s> NVMe namespace\n", disk->d_unit, descr); 482 printf(NVD_STR"%u: %juMB (%ju %u byte sectors)\n", disk->d_unit, 483 (uintmax_t)disk->d_mediasize / (1024*1024), 484 (uintmax_t)disk->d_mediasize / disk->d_sectorsize, 485 disk->d_sectorsize); 486 487 return (ndisk); 488 } 489 490 static void 491 nvd_controller_fail(void *ctrlr_arg) 492 { 493 struct nvd_controller *ctrlr = ctrlr_arg; 494 struct nvd_disk *ndisk; 495 496 mtx_lock(&nvd_lock); 497 TAILQ_REMOVE(&ctrlr_head, ctrlr, tailq); 498 TAILQ_FOREACH(ndisk, &ctrlr->disk_head, ctrlr_tailq) 499 nvd_gone(ndisk); 500 while (!TAILQ_EMPTY(&ctrlr->disk_head)) 501 msleep(&ctrlr->disk_head, &nvd_lock, 0, "nvd_fail", 0); 502 mtx_unlock(&nvd_lock); 503 free(ctrlr, M_NVD); 504 } 505 506