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