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