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