1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2015 Netflix, Inc. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer, 11 * without modification, immediately at the beginning of the file. 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. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * Derived from ata_da.c: 28 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 36 #ifdef _KERNEL 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/bio.h> 40 #include <sys/sysctl.h> 41 #include <sys/taskqueue.h> 42 #include <sys/lock.h> 43 #include <sys/mutex.h> 44 #include <sys/conf.h> 45 #include <sys/devicestat.h> 46 #include <sys/eventhandler.h> 47 #include <sys/malloc.h> 48 #include <sys/cons.h> 49 #include <sys/proc.h> 50 #include <sys/reboot.h> 51 #include <geom/geom.h> 52 #include <geom/geom_disk.h> 53 #endif /* _KERNEL */ 54 55 #ifndef _KERNEL 56 #include <stdio.h> 57 #include <string.h> 58 #endif /* _KERNEL */ 59 60 #include <cam/cam.h> 61 #include <cam/cam_ccb.h> 62 #include <cam/cam_periph.h> 63 #include <cam/cam_xpt_periph.h> 64 #include <cam/cam_sim.h> 65 #include <cam/cam_iosched.h> 66 67 #include <cam/nvme/nvme_all.h> 68 69 typedef enum { 70 NDA_STATE_NORMAL 71 } nda_state; 72 73 typedef enum { 74 NDA_FLAG_OPEN = 0x0001, 75 NDA_FLAG_DIRTY = 0x0002, 76 NDA_FLAG_SCTX_INIT = 0x0004, 77 } nda_flags; 78 79 typedef enum { 80 NDA_Q_4K = 0x01, 81 NDA_Q_NONE = 0x00, 82 } nda_quirks; 83 84 #define NDA_Q_BIT_STRING \ 85 "\020" \ 86 "\001Bit 0" 87 88 typedef enum { 89 NDA_CCB_BUFFER_IO = 0x01, 90 NDA_CCB_DUMP = 0x02, 91 NDA_CCB_TRIM = 0x03, 92 NDA_CCB_TYPE_MASK = 0x0F, 93 } nda_ccb_state; 94 95 /* Offsets into our private area for storing information */ 96 #define ccb_state ccb_h.ppriv_field0 97 #define ccb_bp ccb_h.ppriv_ptr1 /* For NDA_CCB_BUFFER_IO */ 98 #define ccb_trim ccb_h.ppriv_ptr1 /* For NDA_CCB_TRIM */ 99 100 struct nda_softc { 101 struct cam_iosched_softc *cam_iosched; 102 int outstanding_cmds; /* Number of active commands */ 103 int refcount; /* Active xpt_action() calls */ 104 nda_state state; 105 nda_flags flags; 106 nda_quirks quirks; 107 int unmappedio; 108 quad_t deletes; 109 uint32_t nsid; /* Namespace ID for this nda device */ 110 struct disk *disk; 111 struct task sysctl_task; 112 struct sysctl_ctx_list sysctl_ctx; 113 struct sysctl_oid *sysctl_tree; 114 uint64_t trim_count; 115 uint64_t trim_ranges; 116 uint64_t trim_lbas; 117 #ifdef CAM_TEST_FAILURE 118 int force_read_error; 119 int force_write_error; 120 int periodic_read_error; 121 int periodic_read_count; 122 #endif 123 #ifdef CAM_IO_STATS 124 struct sysctl_ctx_list sysctl_stats_ctx; 125 struct sysctl_oid *sysctl_stats_tree; 126 u_int timeouts; 127 u_int errors; 128 u_int invalidations; 129 #endif 130 }; 131 132 struct nda_trim_request { 133 struct nvme_dsm_range dsm[NVME_MAX_DSM_TRIM / sizeof(struct nvme_dsm_range)]; 134 TAILQ_HEAD(, bio) bps; 135 }; 136 _Static_assert(NVME_MAX_DSM_TRIM % sizeof(struct nvme_dsm_range) == 0, 137 "NVME_MAX_DSM_TRIM must be an integral number of ranges"); 138 139 /* Need quirk table */ 140 141 static disk_strategy_t ndastrategy; 142 static dumper_t ndadump; 143 static periph_init_t ndainit; 144 static void ndaasync(void *callback_arg, u_int32_t code, 145 struct cam_path *path, void *arg); 146 static void ndasysctlinit(void *context, int pending); 147 static periph_ctor_t ndaregister; 148 static periph_dtor_t ndacleanup; 149 static periph_start_t ndastart; 150 static periph_oninv_t ndaoninvalidate; 151 static void ndadone(struct cam_periph *periph, 152 union ccb *done_ccb); 153 static int ndaerror(union ccb *ccb, u_int32_t cam_flags, 154 u_int32_t sense_flags); 155 static void ndashutdown(void *arg, int howto); 156 static void ndasuspend(void *arg); 157 158 #ifndef NDA_DEFAULT_SEND_ORDERED 159 #define NDA_DEFAULT_SEND_ORDERED 1 160 #endif 161 #ifndef NDA_DEFAULT_TIMEOUT 162 #define NDA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */ 163 #endif 164 #ifndef NDA_DEFAULT_RETRY 165 #define NDA_DEFAULT_RETRY 4 166 #endif 167 #ifndef NDA_MAX_TRIM_ENTRIES 168 #define NDA_MAX_TRIM_ENTRIES (NVME_MAX_DSM_TRIM / sizeof(struct nvme_dsm_range))/* Number of DSM trims to use, max 256 */ 169 #endif 170 171 static SYSCTL_NODE(_kern_cam, OID_AUTO, nda, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 172 "CAM Direct Access Disk driver"); 173 174 //static int nda_retry_count = NDA_DEFAULT_RETRY; 175 static int nda_send_ordered = NDA_DEFAULT_SEND_ORDERED; 176 static int nda_default_timeout = NDA_DEFAULT_TIMEOUT; 177 static int nda_max_trim_entries = NDA_MAX_TRIM_ENTRIES; 178 static int nda_enable_biospeedup = 1; 179 SYSCTL_INT(_kern_cam_nda, OID_AUTO, max_trim, CTLFLAG_RDTUN, 180 &nda_max_trim_entries, NDA_MAX_TRIM_ENTRIES, 181 "Maximum number of BIO_DELETE to send down as a DSM TRIM."); 182 SYSCTL_INT(_kern_cam_nda, OID_AUTO, enable_biospeedup, CTLFLAG_RDTUN, 183 &nda_enable_biospeedup, 0, "Enable BIO_SPEEDUP processing"); 184 185 /* 186 * All NVMe media is non-rotational, so all nvme device instances 187 * share this to implement the sysctl. 188 */ 189 static int nda_rotating_media = 0; 190 191 static struct periph_driver ndadriver = 192 { 193 ndainit, "nda", 194 TAILQ_HEAD_INITIALIZER(ndadriver.units), /* generation */ 0 195 }; 196 197 PERIPHDRIVER_DECLARE(nda, ndadriver); 198 199 static MALLOC_DEFINE(M_NVMEDA, "nvme_da", "nvme_da buffers"); 200 201 /* 202 * nice wrappers. Maybe these belong in nvme_all.c instead of 203 * here, but this is the only place that uses these. Should 204 * we ever grow another NVME periph, we should move them 205 * all there wholesale. 206 */ 207 208 static void 209 nda_nvme_flush(struct nda_softc *softc, struct ccb_nvmeio *nvmeio) 210 { 211 cam_fill_nvmeio(nvmeio, 212 0, /* retries */ 213 ndadone, /* cbfcnp */ 214 CAM_DIR_NONE, /* flags */ 215 NULL, /* data_ptr */ 216 0, /* dxfer_len */ 217 nda_default_timeout * 1000); /* timeout 30s */ 218 nvme_ns_flush_cmd(&nvmeio->cmd, softc->nsid); 219 } 220 221 static void 222 nda_nvme_trim(struct nda_softc *softc, struct ccb_nvmeio *nvmeio, 223 void *payload, uint32_t num_ranges) 224 { 225 cam_fill_nvmeio(nvmeio, 226 0, /* retries */ 227 ndadone, /* cbfcnp */ 228 CAM_DIR_OUT, /* flags */ 229 payload, /* data_ptr */ 230 num_ranges * sizeof(struct nvme_dsm_range), /* dxfer_len */ 231 nda_default_timeout * 1000); /* timeout 30s */ 232 nvme_ns_trim_cmd(&nvmeio->cmd, softc->nsid, num_ranges); 233 } 234 235 static void 236 nda_nvme_write(struct nda_softc *softc, struct ccb_nvmeio *nvmeio, 237 void *payload, uint64_t lba, uint32_t len, uint32_t count) 238 { 239 cam_fill_nvmeio(nvmeio, 240 0, /* retries */ 241 ndadone, /* cbfcnp */ 242 CAM_DIR_OUT, /* flags */ 243 payload, /* data_ptr */ 244 len, /* dxfer_len */ 245 nda_default_timeout * 1000); /* timeout 30s */ 246 nvme_ns_write_cmd(&nvmeio->cmd, softc->nsid, lba, count); 247 } 248 249 static void 250 nda_nvme_rw_bio(struct nda_softc *softc, struct ccb_nvmeio *nvmeio, 251 struct bio *bp, uint32_t rwcmd) 252 { 253 int flags = rwcmd == NVME_OPC_READ ? CAM_DIR_IN : CAM_DIR_OUT; 254 void *payload; 255 uint64_t lba; 256 uint32_t count; 257 258 if (bp->bio_flags & BIO_UNMAPPED) { 259 flags |= CAM_DATA_BIO; 260 payload = bp; 261 } else { 262 payload = bp->bio_data; 263 } 264 265 lba = bp->bio_pblkno; 266 count = bp->bio_bcount / softc->disk->d_sectorsize; 267 268 cam_fill_nvmeio(nvmeio, 269 0, /* retries */ 270 ndadone, /* cbfcnp */ 271 flags, /* flags */ 272 payload, /* data_ptr */ 273 bp->bio_bcount, /* dxfer_len */ 274 nda_default_timeout * 1000); /* timeout 30s */ 275 nvme_ns_rw_cmd(&nvmeio->cmd, rwcmd, softc->nsid, lba, count); 276 } 277 278 static int 279 ndaopen(struct disk *dp) 280 { 281 struct cam_periph *periph; 282 struct nda_softc *softc; 283 int error; 284 285 periph = (struct cam_periph *)dp->d_drv1; 286 if (cam_periph_acquire(periph) != 0) { 287 return(ENXIO); 288 } 289 290 cam_periph_lock(periph); 291 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) { 292 cam_periph_unlock(periph); 293 cam_periph_release(periph); 294 return (error); 295 } 296 297 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 298 ("ndaopen\n")); 299 300 softc = (struct nda_softc *)periph->softc; 301 softc->flags |= NDA_FLAG_OPEN; 302 303 cam_periph_unhold(periph); 304 cam_periph_unlock(periph); 305 return (0); 306 } 307 308 static int 309 ndaclose(struct disk *dp) 310 { 311 struct cam_periph *periph; 312 struct nda_softc *softc; 313 union ccb *ccb; 314 int error; 315 316 periph = (struct cam_periph *)dp->d_drv1; 317 softc = (struct nda_softc *)periph->softc; 318 cam_periph_lock(periph); 319 320 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH, 321 ("ndaclose\n")); 322 323 if ((softc->flags & NDA_FLAG_DIRTY) != 0 && 324 (periph->flags & CAM_PERIPH_INVALID) == 0 && 325 cam_periph_hold(periph, PRIBIO) == 0) { 326 327 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 328 nda_nvme_flush(softc, &ccb->nvmeio); 329 error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0, 330 /*sense_flags*/0, softc->disk->d_devstat); 331 332 if (error != 0) 333 xpt_print(periph->path, "Synchronize cache failed\n"); 334 else 335 softc->flags &= ~NDA_FLAG_DIRTY; 336 xpt_release_ccb(ccb); 337 cam_periph_unhold(periph); 338 } 339 340 softc->flags &= ~NDA_FLAG_OPEN; 341 342 while (softc->refcount != 0) 343 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "ndaclose", 1); 344 KASSERT(softc->outstanding_cmds == 0, 345 ("nda %d outstanding commands", softc->outstanding_cmds)); 346 cam_periph_unlock(periph); 347 cam_periph_release(periph); 348 return (0); 349 } 350 351 static void 352 ndaschedule(struct cam_periph *periph) 353 { 354 struct nda_softc *softc = (struct nda_softc *)periph->softc; 355 356 if (softc->state != NDA_STATE_NORMAL) 357 return; 358 359 cam_iosched_schedule(softc->cam_iosched, periph); 360 } 361 362 /* 363 * Actually translate the requested transfer into one the physical driver 364 * can understand. The transfer is described by a buf and will include 365 * only one physical transfer. 366 */ 367 static void 368 ndastrategy(struct bio *bp) 369 { 370 struct cam_periph *periph; 371 struct nda_softc *softc; 372 373 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 374 softc = (struct nda_softc *)periph->softc; 375 376 cam_periph_lock(periph); 377 378 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastrategy(%p)\n", bp)); 379 380 /* 381 * If the device has been made invalid, error out 382 */ 383 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 384 cam_periph_unlock(periph); 385 biofinish(bp, NULL, ENXIO); 386 return; 387 } 388 389 if (bp->bio_cmd == BIO_DELETE) 390 softc->deletes++; 391 392 /* 393 * Place it in the queue of disk activities for this disk 394 */ 395 cam_iosched_queue_work(softc->cam_iosched, bp); 396 397 /* 398 * Schedule ourselves for performing the work. 399 */ 400 ndaschedule(periph); 401 cam_periph_unlock(periph); 402 403 return; 404 } 405 406 static int 407 ndadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 408 { 409 struct cam_periph *periph; 410 struct nda_softc *softc; 411 u_int secsize; 412 struct ccb_nvmeio nvmeio; 413 struct disk *dp; 414 uint64_t lba; 415 uint32_t count; 416 int error = 0; 417 418 dp = arg; 419 periph = dp->d_drv1; 420 softc = (struct nda_softc *)periph->softc; 421 secsize = softc->disk->d_sectorsize; 422 lba = offset / secsize; 423 count = length / secsize; 424 425 if ((periph->flags & CAM_PERIPH_INVALID) != 0) 426 return (ENXIO); 427 428 /* xpt_get_ccb returns a zero'd allocation for the ccb, mimic that here */ 429 memset(&nvmeio, 0, sizeof(nvmeio)); 430 if (length > 0) { 431 xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 432 nvmeio.ccb_state = NDA_CCB_DUMP; 433 nda_nvme_write(softc, &nvmeio, virtual, lba, length, count); 434 error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error, 435 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 436 if (error != 0) 437 printf("Aborting dump due to I/O error %d.\n", error); 438 439 return (error); 440 } 441 442 /* Flush */ 443 xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 444 445 nvmeio.ccb_state = NDA_CCB_DUMP; 446 nda_nvme_flush(softc, &nvmeio); 447 error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error, 448 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL); 449 if (error != 0) 450 xpt_print(periph->path, "flush cmd failed\n"); 451 return (error); 452 } 453 454 static void 455 ndainit(void) 456 { 457 cam_status status; 458 459 /* 460 * Install a global async callback. This callback will 461 * receive async callbacks like "new device found". 462 */ 463 status = xpt_register_async(AC_FOUND_DEVICE, ndaasync, NULL, NULL); 464 465 if (status != CAM_REQ_CMP) { 466 printf("nda: Failed to attach master async callback " 467 "due to status 0x%x!\n", status); 468 } else if (nda_send_ordered) { 469 470 /* Register our event handlers */ 471 if ((EVENTHANDLER_REGISTER(power_suspend, ndasuspend, 472 NULL, EVENTHANDLER_PRI_LAST)) == NULL) 473 printf("ndainit: power event registration failed!\n"); 474 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ndashutdown, 475 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) 476 printf("ndainit: shutdown event registration failed!\n"); 477 } 478 } 479 480 /* 481 * Callback from GEOM, called when it has finished cleaning up its 482 * resources. 483 */ 484 static void 485 ndadiskgonecb(struct disk *dp) 486 { 487 struct cam_periph *periph; 488 489 periph = (struct cam_periph *)dp->d_drv1; 490 491 cam_periph_release(periph); 492 } 493 494 static void 495 ndaoninvalidate(struct cam_periph *periph) 496 { 497 struct nda_softc *softc; 498 499 softc = (struct nda_softc *)periph->softc; 500 501 /* 502 * De-register any async callbacks. 503 */ 504 xpt_register_async(0, ndaasync, periph, periph->path); 505 #ifdef CAM_IO_STATS 506 softc->invalidations++; 507 #endif 508 509 /* 510 * Return all queued I/O with ENXIO. 511 * XXX Handle any transactions queued to the card 512 * with XPT_ABORT_CCB. 513 */ 514 cam_iosched_flush(softc->cam_iosched, NULL, ENXIO); 515 516 disk_gone(softc->disk); 517 } 518 519 static void 520 ndacleanup(struct cam_periph *periph) 521 { 522 struct nda_softc *softc; 523 524 softc = (struct nda_softc *)periph->softc; 525 526 cam_periph_unlock(periph); 527 528 cam_iosched_fini(softc->cam_iosched); 529 530 /* 531 * If we can't free the sysctl tree, oh well... 532 */ 533 if ((softc->flags & NDA_FLAG_SCTX_INIT) != 0) { 534 #ifdef CAM_IO_STATS 535 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0) 536 xpt_print(periph->path, 537 "can't remove sysctl stats context\n"); 538 #endif 539 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0) 540 xpt_print(periph->path, 541 "can't remove sysctl context\n"); 542 } 543 544 disk_destroy(softc->disk); 545 free(softc, M_DEVBUF); 546 cam_periph_lock(periph); 547 } 548 549 static void 550 ndaasync(void *callback_arg, u_int32_t code, 551 struct cam_path *path, void *arg) 552 { 553 struct cam_periph *periph; 554 555 periph = (struct cam_periph *)callback_arg; 556 switch (code) { 557 case AC_FOUND_DEVICE: 558 { 559 struct ccb_getdev *cgd; 560 cam_status status; 561 562 cgd = (struct ccb_getdev *)arg; 563 if (cgd == NULL) 564 break; 565 566 if (cgd->protocol != PROTO_NVME) 567 break; 568 569 /* 570 * Allocate a peripheral instance for 571 * this device and start the probe 572 * process. 573 */ 574 status = cam_periph_alloc(ndaregister, ndaoninvalidate, 575 ndacleanup, ndastart, 576 "nda", CAM_PERIPH_BIO, 577 path, ndaasync, 578 AC_FOUND_DEVICE, cgd); 579 580 if (status != CAM_REQ_CMP 581 && status != CAM_REQ_INPROG) 582 printf("ndaasync: Unable to attach to new device " 583 "due to status 0x%x\n", status); 584 break; 585 } 586 case AC_ADVINFO_CHANGED: 587 { 588 uintptr_t buftype; 589 590 buftype = (uintptr_t)arg; 591 if (buftype == CDAI_TYPE_PHYS_PATH) { 592 struct nda_softc *softc; 593 594 softc = periph->softc; 595 disk_attr_changed(softc->disk, "GEOM::physpath", 596 M_NOWAIT); 597 } 598 break; 599 } 600 case AC_LOST_DEVICE: 601 default: 602 cam_periph_async(periph, code, path, arg); 603 break; 604 } 605 } 606 607 static void 608 ndasysctlinit(void *context, int pending) 609 { 610 struct cam_periph *periph; 611 struct nda_softc *softc; 612 char tmpstr[32], tmpstr2[16]; 613 614 periph = (struct cam_periph *)context; 615 616 /* periph was held for us when this task was enqueued */ 617 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 618 cam_periph_release(periph); 619 return; 620 } 621 622 softc = (struct nda_softc *)periph->softc; 623 snprintf(tmpstr, sizeof(tmpstr), "CAM NDA unit %d", periph->unit_number); 624 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); 625 626 sysctl_ctx_init(&softc->sysctl_ctx); 627 softc->flags |= NDA_FLAG_SCTX_INIT; 628 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx, 629 SYSCTL_STATIC_CHILDREN(_kern_cam_nda), OID_AUTO, tmpstr2, 630 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr, "device_index"); 631 if (softc->sysctl_tree == NULL) { 632 printf("ndasysctlinit: unable to allocate sysctl tree\n"); 633 cam_periph_release(periph); 634 return; 635 } 636 637 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 638 OID_AUTO, "unmapped_io", CTLFLAG_RD, 639 &softc->unmappedio, 0, "Unmapped I/O leaf"); 640 641 SYSCTL_ADD_QUAD(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 642 OID_AUTO, "deletes", CTLFLAG_RD, 643 &softc->deletes, "Number of BIO_DELETE requests"); 644 645 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 646 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 647 "trim_count", CTLFLAG_RD, &softc->trim_count, 648 "Total number of unmap/dsm commands sent"); 649 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 650 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 651 "trim_ranges", CTLFLAG_RD, &softc->trim_ranges, 652 "Total number of ranges in unmap/dsm commands"); 653 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx, 654 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 655 "trim_lbas", CTLFLAG_RD, &softc->trim_lbas, 656 "Total lbas in the unmap/dsm commands sent"); 657 658 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 659 OID_AUTO, "rotating", CTLFLAG_RD, &nda_rotating_media, 1, 660 "Rotating media"); 661 662 #ifdef CAM_IO_STATS 663 softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx, 664 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats", 665 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Statistics"); 666 if (softc->sysctl_stats_tree == NULL) { 667 printf("ndasysctlinit: unable to allocate sysctl tree for stats\n"); 668 cam_periph_release(periph); 669 return; 670 } 671 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx, 672 SYSCTL_CHILDREN(softc->sysctl_stats_tree), 673 OID_AUTO, "timeouts", CTLFLAG_RD, 674 &softc->timeouts, 0, 675 "Device timeouts reported by the SIM"); 676 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx, 677 SYSCTL_CHILDREN(softc->sysctl_stats_tree), 678 OID_AUTO, "errors", CTLFLAG_RD, 679 &softc->errors, 0, 680 "Transport errors reported by the SIM."); 681 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx, 682 SYSCTL_CHILDREN(softc->sysctl_stats_tree), 683 OID_AUTO, "pack_invalidations", CTLFLAG_RD, 684 &softc->invalidations, 0, 685 "Device pack invalidations."); 686 #endif 687 688 #ifdef CAM_TEST_FAILURE 689 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 690 OID_AUTO, "invalidate", CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, 691 periph, 0, cam_periph_invalidate_sysctl, "I", 692 "Write 1 to invalidate the drive immediately"); 693 #endif 694 695 cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx, 696 softc->sysctl_tree); 697 698 cam_periph_release(periph); 699 } 700 701 static int 702 ndagetattr(struct bio *bp) 703 { 704 int ret; 705 struct cam_periph *periph; 706 707 if (g_handleattr_int(bp, "GEOM::canspeedup", nda_enable_biospeedup)) 708 return (EJUSTRETURN); 709 710 periph = (struct cam_periph *)bp->bio_disk->d_drv1; 711 cam_periph_lock(periph); 712 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute, 713 periph->path); 714 cam_periph_unlock(periph); 715 if (ret == 0) 716 bp->bio_completed = bp->bio_length; 717 return ret; 718 } 719 720 static cam_status 721 ndaregister(struct cam_periph *periph, void *arg) 722 { 723 struct nda_softc *softc; 724 struct disk *disk; 725 struct ccb_pathinq cpi; 726 const struct nvme_namespace_data *nsd; 727 const struct nvme_controller_data *cd; 728 char announce_buf[80]; 729 uint8_t flbas_fmt, lbads, vwc_present; 730 u_int maxio; 731 int quirks; 732 733 nsd = nvme_get_identify_ns(periph); 734 cd = nvme_get_identify_cntrl(periph); 735 736 softc = (struct nda_softc *)malloc(sizeof(*softc), M_DEVBUF, 737 M_NOWAIT | M_ZERO); 738 739 if (softc == NULL) { 740 printf("ndaregister: Unable to probe new device. " 741 "Unable to allocate softc\n"); 742 return(CAM_REQ_CMP_ERR); 743 } 744 745 if (cam_iosched_init(&softc->cam_iosched, periph) != 0) { 746 printf("ndaregister: Unable to probe new device. " 747 "Unable to allocate iosched memory\n"); 748 free(softc, M_DEVBUF); 749 return(CAM_REQ_CMP_ERR); 750 } 751 752 /* ident_data parsing */ 753 754 periph->softc = softc; 755 756 softc->quirks = NDA_Q_NONE; 757 758 xpt_path_inq(&cpi, periph->path); 759 760 TASK_INIT(&softc->sysctl_task, 0, ndasysctlinit, periph); 761 762 /* 763 * The name space ID is the lun, save it for later I/O 764 */ 765 softc->nsid = (uint32_t)xpt_path_lun_id(periph->path); 766 767 /* 768 * Register this media as a disk 769 */ 770 (void)cam_periph_hold(periph, PRIBIO); 771 cam_periph_unlock(periph); 772 snprintf(announce_buf, sizeof(announce_buf), 773 "kern.cam.nda.%d.quirks", periph->unit_number); 774 quirks = softc->quirks; 775 TUNABLE_INT_FETCH(announce_buf, &quirks); 776 softc->quirks = quirks; 777 cam_iosched_set_sort_queue(softc->cam_iosched, 0); 778 softc->disk = disk = disk_alloc(); 779 disk->d_rotation_rate = DISK_RR_NON_ROTATING; 780 disk->d_open = ndaopen; 781 disk->d_close = ndaclose; 782 disk->d_strategy = ndastrategy; 783 disk->d_getattr = ndagetattr; 784 disk->d_dump = ndadump; 785 disk->d_gone = ndadiskgonecb; 786 disk->d_name = "nda"; 787 disk->d_drv1 = periph; 788 disk->d_unit = periph->unit_number; 789 maxio = cpi.maxio; /* Honor max I/O size of SIM */ 790 if (maxio == 0) 791 maxio = DFLTPHYS; /* traditional default */ 792 else if (maxio > MAXPHYS) 793 maxio = MAXPHYS; /* for safety */ 794 disk->d_maxsize = maxio; 795 flbas_fmt = (nsd->flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) & 796 NVME_NS_DATA_FLBAS_FORMAT_MASK; 797 lbads = (nsd->lbaf[flbas_fmt] >> NVME_NS_DATA_LBAF_LBADS_SHIFT) & 798 NVME_NS_DATA_LBAF_LBADS_MASK; 799 disk->d_sectorsize = 1 << lbads; 800 disk->d_mediasize = (off_t)(disk->d_sectorsize * nsd->nsze); 801 disk->d_delmaxsize = disk->d_mediasize; 802 disk->d_flags = DISKFLAG_DIRECT_COMPLETION; 803 if (nvme_ctrlr_has_dataset_mgmt(cd)) 804 disk->d_flags |= DISKFLAG_CANDELETE; 805 vwc_present = (cd->vwc >> NVME_CTRLR_DATA_VWC_PRESENT_SHIFT) & 806 NVME_CTRLR_DATA_VWC_PRESENT_MASK; 807 if (vwc_present) 808 disk->d_flags |= DISKFLAG_CANFLUSHCACHE; 809 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) { 810 disk->d_flags |= DISKFLAG_UNMAPPED_BIO; 811 softc->unmappedio = 1; 812 } 813 /* 814 * d_ident and d_descr are both far bigger than the length of either 815 * the serial or model number strings. 816 */ 817 cam_strvis(disk->d_descr, cd->mn, 818 NVME_MODEL_NUMBER_LENGTH, sizeof(disk->d_descr)); 819 cam_strvis(disk->d_ident, cd->sn, 820 NVME_SERIAL_NUMBER_LENGTH, sizeof(disk->d_ident)); 821 disk->d_hba_vendor = cpi.hba_vendor; 822 disk->d_hba_device = cpi.hba_device; 823 disk->d_hba_subvendor = cpi.hba_subvendor; 824 disk->d_hba_subdevice = cpi.hba_subdevice; 825 snprintf(disk->d_attachment, sizeof(disk->d_attachment), 826 "%s%d", cpi.dev_name, cpi.unit_number); 827 disk->d_stripesize = disk->d_sectorsize; 828 disk->d_stripeoffset = 0; 829 disk->d_devstat = devstat_new_entry(periph->periph_name, 830 periph->unit_number, disk->d_sectorsize, 831 DEVSTAT_ALL_SUPPORTED, 832 DEVSTAT_TYPE_DIRECT | XPORT_DEVSTAT_TYPE(cpi.transport), 833 DEVSTAT_PRIORITY_DISK); 834 /* 835 * Add alias for older nvd drives to ease transition. 836 */ 837 /* disk_add_alias(disk, "nvd"); Have reports of this causing problems */ 838 839 /* 840 * Acquire a reference to the periph before we register with GEOM. 841 * We'll release this reference once GEOM calls us back (via 842 * ndadiskgonecb()) telling us that our provider has been freed. 843 */ 844 if (cam_periph_acquire(periph) != 0) { 845 xpt_print(periph->path, "%s: lost periph during " 846 "registration!\n", __func__); 847 cam_periph_lock(periph); 848 return (CAM_REQ_CMP_ERR); 849 } 850 disk_create(softc->disk, DISK_VERSION); 851 cam_periph_lock(periph); 852 cam_periph_unhold(periph); 853 854 snprintf(announce_buf, sizeof(announce_buf), 855 "%juMB (%ju %u byte sectors)", 856 (uintmax_t)((uintmax_t)disk->d_mediasize / (1024*1024)), 857 (uintmax_t)disk->d_mediasize / disk->d_sectorsize, 858 disk->d_sectorsize); 859 xpt_announce_periph(periph, announce_buf); 860 xpt_announce_quirks(periph, softc->quirks, NDA_Q_BIT_STRING); 861 862 /* 863 * Create our sysctl variables, now that we know 864 * we have successfully attached. 865 */ 866 if (cam_periph_acquire(periph) == 0) 867 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task); 868 869 /* 870 * Register for device going away and info about the drive 871 * changing (though with NVMe, it can't) 872 */ 873 xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED, 874 ndaasync, periph, periph->path); 875 876 softc->state = NDA_STATE_NORMAL; 877 return(CAM_REQ_CMP); 878 } 879 880 static void 881 ndastart(struct cam_periph *periph, union ccb *start_ccb) 882 { 883 struct nda_softc *softc = (struct nda_softc *)periph->softc; 884 struct ccb_nvmeio *nvmeio = &start_ccb->nvmeio; 885 886 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart\n")); 887 888 switch (softc->state) { 889 case NDA_STATE_NORMAL: 890 { 891 struct bio *bp; 892 893 bp = cam_iosched_next_bio(softc->cam_iosched); 894 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart: bio %p\n", bp)); 895 if (bp == NULL) { 896 xpt_release_ccb(start_ccb); 897 break; 898 } 899 900 switch (bp->bio_cmd) { 901 case BIO_WRITE: 902 softc->flags |= NDA_FLAG_DIRTY; 903 /* FALLTHROUGH */ 904 case BIO_READ: 905 { 906 #ifdef CAM_TEST_FAILURE 907 int fail = 0; 908 909 /* 910 * Support the failure ioctls. If the command is a 911 * read, and there are pending forced read errors, or 912 * if a write and pending write errors, then fail this 913 * operation with EIO. This is useful for testing 914 * purposes. Also, support having every Nth read fail. 915 * 916 * This is a rather blunt tool. 917 */ 918 if (bp->bio_cmd == BIO_READ) { 919 if (softc->force_read_error) { 920 softc->force_read_error--; 921 fail = 1; 922 } 923 if (softc->periodic_read_error > 0) { 924 if (++softc->periodic_read_count >= 925 softc->periodic_read_error) { 926 softc->periodic_read_count = 0; 927 fail = 1; 928 } 929 } 930 } else { 931 if (softc->force_write_error) { 932 softc->force_write_error--; 933 fail = 1; 934 } 935 } 936 if (fail) { 937 biofinish(bp, NULL, EIO); 938 xpt_release_ccb(start_ccb); 939 ndaschedule(periph); 940 return; 941 } 942 #endif 943 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 || 944 round_page(bp->bio_bcount + bp->bio_ma_offset) / 945 PAGE_SIZE == bp->bio_ma_n, 946 ("Short bio %p", bp)); 947 nda_nvme_rw_bio(softc, &start_ccb->nvmeio, bp, bp->bio_cmd == BIO_READ ? 948 NVME_OPC_READ : NVME_OPC_WRITE); 949 break; 950 } 951 case BIO_DELETE: 952 { 953 struct nvme_dsm_range *dsm_range, *dsm_end; 954 struct nda_trim_request *trim; 955 struct bio *bp1; 956 int ents; 957 uint32_t totalcount = 0, ranges = 0; 958 959 trim = malloc(sizeof(*trim), M_NVMEDA, M_ZERO | M_NOWAIT); 960 if (trim == NULL) { 961 biofinish(bp, NULL, ENOMEM); 962 xpt_release_ccb(start_ccb); 963 ndaschedule(periph); 964 return; 965 } 966 TAILQ_INIT(&trim->bps); 967 bp1 = bp; 968 ents = min(nitems(trim->dsm), nda_max_trim_entries); 969 dsm_range = trim->dsm; 970 dsm_end = dsm_range + ents; 971 do { 972 TAILQ_INSERT_TAIL(&trim->bps, bp1, bio_queue); 973 dsm_range->length = 974 htole32(bp1->bio_bcount / softc->disk->d_sectorsize); 975 dsm_range->starting_lba = 976 htole64(bp1->bio_offset / softc->disk->d_sectorsize); 977 ranges++; 978 totalcount += dsm_range->length; 979 dsm_range++; 980 if (dsm_range >= dsm_end) 981 break; 982 bp1 = cam_iosched_next_trim(softc->cam_iosched); 983 /* XXX -- Could collapse adjacent ranges, but we don't for now */ 984 /* XXX -- Could limit based on total payload size */ 985 } while (bp1 != NULL); 986 start_ccb->ccb_trim = trim; 987 nda_nvme_trim(softc, &start_ccb->nvmeio, trim->dsm, 988 dsm_range - trim->dsm); 989 start_ccb->ccb_state = NDA_CCB_TRIM; 990 softc->trim_count++; 991 softc->trim_ranges += ranges; 992 softc->trim_lbas += totalcount; 993 /* 994 * Note: We can have multiple TRIMs in flight, so we don't call 995 * cam_iosched_submit_trim(softc->cam_iosched); 996 * since that forces the I/O scheduler to only schedule one at a time. 997 * On NVMe drives, this is a performance disaster. 998 */ 999 goto out; 1000 } 1001 case BIO_FLUSH: 1002 nda_nvme_flush(softc, nvmeio); 1003 break; 1004 default: 1005 biofinish(bp, NULL, EOPNOTSUPP); 1006 xpt_release_ccb(start_ccb); 1007 ndaschedule(periph); 1008 return; 1009 } 1010 start_ccb->ccb_state = NDA_CCB_BUFFER_IO; 1011 start_ccb->ccb_bp = bp; 1012 out: 1013 start_ccb->ccb_h.flags |= CAM_UNLOCKED; 1014 softc->outstanding_cmds++; 1015 softc->refcount++; /* For submission only */ 1016 cam_periph_unlock(periph); 1017 xpt_action(start_ccb); 1018 cam_periph_lock(periph); 1019 softc->refcount--; /* Submission done */ 1020 1021 /* May have more work to do, so ensure we stay scheduled */ 1022 ndaschedule(periph); 1023 break; 1024 } 1025 } 1026 } 1027 1028 static void 1029 ndadone(struct cam_periph *periph, union ccb *done_ccb) 1030 { 1031 struct nda_softc *softc; 1032 struct ccb_nvmeio *nvmeio = &done_ccb->nvmeio; 1033 struct cam_path *path; 1034 int state; 1035 1036 softc = (struct nda_softc *)periph->softc; 1037 path = done_ccb->ccb_h.path; 1038 1039 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("ndadone\n")); 1040 1041 state = nvmeio->ccb_state & NDA_CCB_TYPE_MASK; 1042 switch (state) { 1043 case NDA_CCB_BUFFER_IO: 1044 case NDA_CCB_TRIM: 1045 { 1046 int error; 1047 1048 cam_periph_lock(periph); 1049 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1050 error = ndaerror(done_ccb, 0, 0); 1051 if (error == ERESTART) { 1052 /* A retry was scheduled, so just return. */ 1053 cam_periph_unlock(periph); 1054 return; 1055 } 1056 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1057 cam_release_devq(path, 1058 /*relsim_flags*/0, 1059 /*reduction*/0, 1060 /*timeout*/0, 1061 /*getcount_only*/0); 1062 } else { 1063 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 1064 panic("REQ_CMP with QFRZN"); 1065 error = 0; 1066 } 1067 if (state == NDA_CCB_BUFFER_IO) { 1068 struct bio *bp; 1069 1070 bp = (struct bio *)done_ccb->ccb_bp; 1071 bp->bio_error = error; 1072 if (error != 0) { 1073 bp->bio_resid = bp->bio_bcount; 1074 bp->bio_flags |= BIO_ERROR; 1075 } else { 1076 bp->bio_resid = 0; 1077 } 1078 softc->outstanding_cmds--; 1079 1080 /* 1081 * We need to call cam_iosched before we call biodone so that we 1082 * don't measure any activity that happens in the completion 1083 * routine, which in the case of sendfile can be quite 1084 * extensive. 1085 */ 1086 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb); 1087 xpt_release_ccb(done_ccb); 1088 ndaschedule(periph); 1089 cam_periph_unlock(periph); 1090 biodone(bp); 1091 } else { /* state == NDA_CCB_TRIM */ 1092 struct nda_trim_request *trim; 1093 struct bio *bp1, *bp2; 1094 TAILQ_HEAD(, bio) queue; 1095 1096 trim = nvmeio->ccb_trim; 1097 TAILQ_INIT(&queue); 1098 TAILQ_CONCAT(&queue, &trim->bps, bio_queue); 1099 free(trim, M_NVMEDA); 1100 1101 /* 1102 * Since we can have multiple trims in flight, we don't 1103 * need to call this here. 1104 * cam_iosched_trim_done(softc->cam_iosched); 1105 */ 1106 /* 1107 * The the I/O scheduler that we're finishing the I/O 1108 * so we can keep book. The first one we pass in the CCB 1109 * which has the timing information. The rest we pass in NULL 1110 * so we can keep proper counts. 1111 */ 1112 bp1 = TAILQ_FIRST(&queue); 1113 cam_iosched_bio_complete(softc->cam_iosched, bp1, done_ccb); 1114 xpt_release_ccb(done_ccb); 1115 softc->outstanding_cmds--; 1116 ndaschedule(periph); 1117 cam_periph_unlock(periph); 1118 while ((bp2 = TAILQ_FIRST(&queue)) != NULL) { 1119 TAILQ_REMOVE(&queue, bp2, bio_queue); 1120 bp2->bio_error = error; 1121 if (error != 0) { 1122 bp2->bio_flags |= BIO_ERROR; 1123 bp2->bio_resid = bp1->bio_bcount; 1124 } else 1125 bp2->bio_resid = 0; 1126 if (bp1 != bp2) 1127 cam_iosched_bio_complete(softc->cam_iosched, bp2, NULL); 1128 biodone(bp2); 1129 } 1130 } 1131 return; 1132 } 1133 case NDA_CCB_DUMP: 1134 /* No-op. We're polling */ 1135 return; 1136 default: 1137 break; 1138 } 1139 xpt_release_ccb(done_ccb); 1140 } 1141 1142 static int 1143 ndaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 1144 { 1145 struct nda_softc *softc; 1146 struct cam_periph *periph; 1147 1148 periph = xpt_path_periph(ccb->ccb_h.path); 1149 softc = (struct nda_softc *)periph->softc; 1150 1151 switch (ccb->ccb_h.status & CAM_STATUS_MASK) { 1152 case CAM_CMD_TIMEOUT: 1153 #ifdef CAM_IO_STATS 1154 softc->timeouts++; 1155 #endif 1156 break; 1157 case CAM_REQ_ABORTED: 1158 case CAM_REQ_CMP_ERR: 1159 case CAM_REQ_TERMIO: 1160 case CAM_UNREC_HBA_ERROR: 1161 case CAM_DATA_RUN_ERR: 1162 case CAM_ATA_STATUS_ERROR: 1163 #ifdef CAM_IO_STATS 1164 softc->errors++; 1165 #endif 1166 break; 1167 default: 1168 break; 1169 } 1170 1171 return(cam_periph_error(ccb, cam_flags, sense_flags)); 1172 } 1173 1174 /* 1175 * Step through all NDA peripheral drivers, and if the device is still open, 1176 * sync the disk cache to physical media. 1177 */ 1178 static void 1179 ndaflush(void) 1180 { 1181 struct cam_periph *periph; 1182 struct nda_softc *softc; 1183 union ccb *ccb; 1184 int error; 1185 1186 CAM_PERIPH_FOREACH(periph, &ndadriver) { 1187 softc = (struct nda_softc *)periph->softc; 1188 1189 if (SCHEDULER_STOPPED()) { 1190 /* 1191 * If we paniced with the lock held or the periph is not 1192 * open, do not recurse. Otherwise, call ndadump since 1193 * that avoids the sleeping cam_periph_getccb does if no 1194 * CCBs are available. 1195 */ 1196 if (!cam_periph_owned(periph) && 1197 (softc->flags & NDA_FLAG_OPEN)) { 1198 ndadump(softc->disk, NULL, 0, 0, 0); 1199 } 1200 continue; 1201 } 1202 1203 /* 1204 * We only sync the cache if the drive is still open 1205 */ 1206 cam_periph_lock(periph); 1207 if ((softc->flags & NDA_FLAG_OPEN) == 0) { 1208 cam_periph_unlock(periph); 1209 continue; 1210 } 1211 1212 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1213 nda_nvme_flush(softc, &ccb->nvmeio); 1214 error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0, 1215 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY, 1216 softc->disk->d_devstat); 1217 if (error != 0) 1218 xpt_print(periph->path, "Synchronize cache failed\n"); 1219 xpt_release_ccb(ccb); 1220 cam_periph_unlock(periph); 1221 } 1222 } 1223 1224 static void 1225 ndashutdown(void *arg, int howto) 1226 { 1227 1228 ndaflush(); 1229 } 1230 1231 static void 1232 ndasuspend(void *arg) 1233 { 1234 1235 ndaflush(); 1236 } 1237