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