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