2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015 Netflix, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 * Derived from ata_da.c:
28 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/sysctl.h>
41 #include <sys/taskqueue.h>
43 #include <sys/mutex.h>
45 #include <sys/devicestat.h>
46 #include <sys/eventhandler.h>
47 #include <sys/malloc.h>
50 #include <sys/reboot.h>
51 #include <geom/geom_disk.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>
66 #include <cam/nvme/nvme_all.h>
73 NDA_FLAG_OPEN = 0x0001,
74 NDA_FLAG_DIRTY = 0x0002,
75 NDA_FLAG_SCTX_INIT = 0x0004,
83 #define NDA_Q_BIT_STRING \
88 NDA_CCB_BUFFER_IO = 0x01,
91 NDA_CCB_TYPE_MASK = 0x0F,
94 /* Offsets into our private area for storing information */
95 #define ccb_state ccb_h.ppriv_field0
96 #define ccb_bp ccb_h.ppriv_ptr1 /* For NDA_CCB_BUFFER_IO */
97 #define ccb_trim ccb_h.ppriv_ptr1 /* For NDA_CCB_TRIM */
100 struct cam_iosched_softc *cam_iosched;
101 int outstanding_cmds; /* Number of active commands */
102 int refcount; /* Active xpt_action() calls */
108 uint32_t nsid; /* Namespace ID for this nda device */
110 struct task sysctl_task;
111 struct sysctl_ctx_list sysctl_ctx;
112 struct sysctl_oid *sysctl_tree;
114 uint64_t trim_ranges;
116 #ifdef CAM_TEST_FAILURE
117 int force_read_error;
118 int force_write_error;
119 int periodic_read_error;
120 int periodic_read_count;
123 struct sysctl_ctx_list sysctl_stats_ctx;
124 struct sysctl_oid *sysctl_stats_tree;
131 struct nda_trim_request {
133 struct nvme_dsm_range dsm;
134 uint8_t data[NVME_MAX_DSM_TRIM];
136 TAILQ_HEAD(, bio) bps;
139 /* Need quirk table */
141 static disk_strategy_t ndastrategy;
142 static dumper_t ndadump;
143 static periph_init_t ndainit;
144 static void ndaasync(void *callback_arg, u_int32_t code,
145 struct cam_path *path, void *arg);
146 static void ndasysctlinit(void *context, int pending);
147 static periph_ctor_t ndaregister;
148 static periph_dtor_t ndacleanup;
149 static periph_start_t ndastart;
150 static periph_oninv_t ndaoninvalidate;
151 static void ndadone(struct cam_periph *periph,
152 union ccb *done_ccb);
153 static int ndaerror(union ccb *ccb, u_int32_t cam_flags,
154 u_int32_t sense_flags);
155 static void ndashutdown(void *arg, int howto);
156 static void ndasuspend(void *arg);
158 #ifndef NDA_DEFAULT_SEND_ORDERED
159 #define NDA_DEFAULT_SEND_ORDERED 1
161 #ifndef NDA_DEFAULT_TIMEOUT
162 #define NDA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
164 #ifndef NDA_DEFAULT_RETRY
165 #define NDA_DEFAULT_RETRY 4
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 */
171 static SYSCTL_NODE(_kern_cam, OID_AUTO, nda, CTLFLAG_RD, 0,
172 "CAM Direct Access Disk driver");
174 //static int nda_retry_count = NDA_DEFAULT_RETRY;
175 static int nda_send_ordered = NDA_DEFAULT_SEND_ORDERED;
176 static int nda_default_timeout = NDA_DEFAULT_TIMEOUT;
177 static int nda_max_trim_entries = NDA_MAX_TRIM_ENTRIES;
178 SYSCTL_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.");
183 * All NVMe media is non-rotational, so all nvme device instances
184 * share this to implement the sysctl.
186 static int nda_rotating_media = 0;
188 static struct periph_driver ndadriver =
191 TAILQ_HEAD_INITIALIZER(ndadriver.units), /* generation */ 0
194 PERIPHDRIVER_DECLARE(nda, ndadriver);
196 static MALLOC_DEFINE(M_NVMEDA, "nvme_da", "nvme_da buffers");
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.
206 nda_nvme_flush(struct nda_softc *softc, struct ccb_nvmeio *nvmeio)
208 cam_fill_nvmeio(nvmeio,
210 ndadone, /* cbfcnp */
211 CAM_DIR_NONE, /* flags */
214 nda_default_timeout * 1000); /* timeout 30s */
215 nvme_ns_flush_cmd(&nvmeio->cmd, softc->nsid);
219 nda_nvme_trim(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
220 void *payload, uint32_t num_ranges)
222 cam_fill_nvmeio(nvmeio,
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);
233 nda_nvme_write(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
234 void *payload, uint64_t lba, uint32_t len, uint32_t count)
236 cam_fill_nvmeio(nvmeio,
238 ndadone, /* cbfcnp */
239 CAM_DIR_OUT, /* flags */
240 payload, /* data_ptr */
242 nda_default_timeout * 1000); /* timeout 30s */
243 nvme_ns_write_cmd(&nvmeio->cmd, softc->nsid, lba, count);
247 nda_nvme_rw_bio(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
248 struct bio *bp, uint32_t rwcmd)
250 int flags = rwcmd == NVME_OPC_READ ? CAM_DIR_IN : CAM_DIR_OUT;
255 if (bp->bio_flags & BIO_UNMAPPED) {
256 flags |= CAM_DATA_BIO;
259 payload = bp->bio_data;
262 lba = bp->bio_pblkno;
263 count = bp->bio_bcount / softc->disk->d_sectorsize;
265 cam_fill_nvmeio(nvmeio,
267 ndadone, /* cbfcnp */
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);
276 ndaopen(struct disk *dp)
278 struct cam_periph *periph;
279 struct nda_softc *softc;
282 periph = (struct cam_periph *)dp->d_drv1;
283 if (cam_periph_acquire(periph) != 0) {
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);
294 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
297 softc = (struct nda_softc *)periph->softc;
298 softc->flags |= NDA_FLAG_OPEN;
300 cam_periph_unhold(periph);
301 cam_periph_unlock(periph);
306 ndaclose(struct disk *dp)
308 struct cam_periph *periph;
309 struct nda_softc *softc;
313 periph = (struct cam_periph *)dp->d_drv1;
314 softc = (struct nda_softc *)periph->softc;
315 cam_periph_lock(periph);
317 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
320 if ((softc->flags & NDA_FLAG_DIRTY) != 0 &&
321 (periph->flags & CAM_PERIPH_INVALID) == 0 &&
322 cam_periph_hold(periph, PRIBIO) == 0) {
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);
330 xpt_print(periph->path, "Synchronize cache failed\n");
332 softc->flags &= ~NDA_FLAG_DIRTY;
333 xpt_release_ccb(ccb);
334 cam_periph_unhold(periph);
337 softc->flags &= ~NDA_FLAG_OPEN;
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);
349 ndaschedule(struct cam_periph *periph)
351 struct nda_softc *softc = (struct nda_softc *)periph->softc;
353 if (softc->state != NDA_STATE_NORMAL)
356 cam_iosched_schedule(softc->cam_iosched, periph);
360 * Actually translate the requested transfer into one the physical driver
361 * can understand. The transfer is described by a buf and will include
362 * only one physical transfer.
365 ndastrategy(struct bio *bp)
367 struct cam_periph *periph;
368 struct nda_softc *softc;
370 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
371 softc = (struct nda_softc *)periph->softc;
373 cam_periph_lock(periph);
375 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastrategy(%p)\n", bp));
378 * If the device has been made invalid, error out
380 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
381 cam_periph_unlock(periph);
382 biofinish(bp, NULL, ENXIO);
386 if (bp->bio_cmd == BIO_DELETE)
390 * Place it in the queue of disk activities for this disk
392 cam_iosched_queue_work(softc->cam_iosched, bp);
395 * Schedule ourselves for performing the work.
398 cam_periph_unlock(periph);
404 ndadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
406 struct cam_periph *periph;
407 struct nda_softc *softc;
409 struct ccb_nvmeio nvmeio;
417 softc = (struct nda_softc *)periph->softc;
418 secsize = softc->disk->d_sectorsize;
419 lba = offset / secsize;
420 count = length / secsize;
422 if ((periph->flags & CAM_PERIPH_INVALID) != 0)
425 /* xpt_get_ccb returns a zero'd allocation for the ccb, mimic that here */
426 memset(&nvmeio, 0, sizeof(nvmeio));
428 xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
429 nvmeio.ccb_state = NDA_CCB_DUMP;
430 nda_nvme_write(softc, &nvmeio, virtual, lba, length, count);
431 error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error,
432 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
434 printf("Aborting dump due to I/O error %d.\n", error);
440 xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
442 nvmeio.ccb_state = NDA_CCB_DUMP;
443 nda_nvme_flush(softc, &nvmeio);
444 error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error,
445 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
447 xpt_print(periph->path, "flush cmd failed\n");
457 * Install a global async callback. This callback will
458 * receive async callbacks like "new device found".
460 status = xpt_register_async(AC_FOUND_DEVICE, ndaasync, NULL, NULL);
462 if (status != CAM_REQ_CMP) {
463 printf("nda: Failed to attach master async callback "
464 "due to status 0x%x!\n", status);
465 } else if (nda_send_ordered) {
467 /* Register our event handlers */
468 if ((EVENTHANDLER_REGISTER(power_suspend, ndasuspend,
469 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
470 printf("ndainit: power event registration failed!\n");
471 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ndashutdown,
472 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
473 printf("ndainit: shutdown event registration failed!\n");
478 * Callback from GEOM, called when it has finished cleaning up its
482 ndadiskgonecb(struct disk *dp)
484 struct cam_periph *periph;
486 periph = (struct cam_periph *)dp->d_drv1;
488 cam_periph_release(periph);
492 ndaoninvalidate(struct cam_periph *periph)
494 struct nda_softc *softc;
496 softc = (struct nda_softc *)periph->softc;
499 * De-register any async callbacks.
501 xpt_register_async(0, ndaasync, periph, periph->path);
503 softc->invalidations++;
507 * Return all queued I/O with ENXIO.
508 * XXX Handle any transactions queued to the card
509 * with XPT_ABORT_CCB.
511 cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
513 disk_gone(softc->disk);
517 ndacleanup(struct cam_periph *periph)
519 struct nda_softc *softc;
521 softc = (struct nda_softc *)periph->softc;
523 cam_periph_unlock(periph);
525 cam_iosched_fini(softc->cam_iosched);
528 * If we can't free the sysctl tree, oh well...
530 if ((softc->flags & NDA_FLAG_SCTX_INIT) != 0) {
532 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
533 xpt_print(periph->path,
534 "can't remove sysctl stats context\n");
536 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
537 xpt_print(periph->path,
538 "can't remove sysctl context\n");
541 disk_destroy(softc->disk);
542 free(softc, M_DEVBUF);
543 cam_periph_lock(periph);
547 ndaasync(void *callback_arg, u_int32_t code,
548 struct cam_path *path, void *arg)
550 struct cam_periph *periph;
552 periph = (struct cam_periph *)callback_arg;
554 case AC_FOUND_DEVICE:
556 struct ccb_getdev *cgd;
559 cgd = (struct ccb_getdev *)arg;
563 if (cgd->protocol != PROTO_NVME)
567 * Allocate a peripheral instance for
568 * this device and start the probe
571 status = cam_periph_alloc(ndaregister, ndaoninvalidate,
572 ndacleanup, ndastart,
573 "nda", CAM_PERIPH_BIO,
575 AC_FOUND_DEVICE, cgd);
577 if (status != CAM_REQ_CMP
578 && status != CAM_REQ_INPROG)
579 printf("ndaasync: Unable to attach to new device "
580 "due to status 0x%x\n", status);
583 case AC_ADVINFO_CHANGED:
587 buftype = (uintptr_t)arg;
588 if (buftype == CDAI_TYPE_PHYS_PATH) {
589 struct nda_softc *softc;
591 softc = periph->softc;
592 disk_attr_changed(softc->disk, "GEOM::physpath",
599 cam_periph_async(periph, code, path, arg);
605 ndasysctlinit(void *context, int pending)
607 struct cam_periph *periph;
608 struct nda_softc *softc;
609 char tmpstr[32], tmpstr2[16];
611 periph = (struct cam_periph *)context;
613 /* periph was held for us when this task was enqueued */
614 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
615 cam_periph_release(periph);
619 softc = (struct nda_softc *)periph->softc;
620 snprintf(tmpstr, sizeof(tmpstr), "CAM NDA unit %d", periph->unit_number);
621 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
623 sysctl_ctx_init(&softc->sysctl_ctx);
624 softc->flags |= NDA_FLAG_SCTX_INIT;
625 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
626 SYSCTL_STATIC_CHILDREN(_kern_cam_nda), OID_AUTO, tmpstr2,
627 CTLFLAG_RD, 0, tmpstr, "device_index");
628 if (softc->sysctl_tree == NULL) {
629 printf("ndasysctlinit: unable to allocate sysctl tree\n");
630 cam_periph_release(periph);
634 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
635 OID_AUTO, "unmapped_io", CTLFLAG_RD,
636 &softc->unmappedio, 0, "Unmapped I/O leaf");
638 SYSCTL_ADD_QUAD(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
639 OID_AUTO, "deletes", CTLFLAG_RD,
640 &softc->deletes, "Number of BIO_DELETE requests");
642 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
643 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
644 "trim_count", CTLFLAG_RD, &softc->trim_count,
645 "Total number of unmap/dsm commands sent");
646 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
647 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
648 "trim_ranges", CTLFLAG_RD, &softc->trim_ranges,
649 "Total number of ranges in unmap/dsm commands");
650 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
651 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
652 "trim_lbas", CTLFLAG_RD, &softc->trim_lbas,
653 "Total lbas in the unmap/dsm commands sent");
655 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
656 OID_AUTO, "rotating", CTLFLAG_RD, &nda_rotating_media, 1,
660 softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
661 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
662 CTLFLAG_RD, 0, "Statistics");
663 if (softc->sysctl_stats_tree == NULL) {
664 printf("ndasysctlinit: unable to allocate sysctl tree for stats\n");
665 cam_periph_release(periph);
668 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
669 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
670 OID_AUTO, "timeouts", CTLFLAG_RD,
672 "Device timeouts reported by the SIM");
673 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
674 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
675 OID_AUTO, "errors", CTLFLAG_RD,
677 "Transport errors reported by the SIM.");
678 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
679 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
680 OID_AUTO, "pack_invalidations", CTLFLAG_RD,
681 &softc->invalidations, 0,
682 "Device pack invalidations.");
685 #ifdef CAM_TEST_FAILURE
686 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
687 OID_AUTO, "invalidate", CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
688 periph, 0, cam_periph_invalidate_sysctl, "I",
689 "Write 1 to invalidate the drive immediately");
692 cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
695 cam_periph_release(periph);
699 ndagetattr(struct bio *bp)
702 struct cam_periph *periph;
704 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
705 cam_periph_lock(periph);
706 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
708 cam_periph_unlock(periph);
710 bp->bio_completed = bp->bio_length;
715 ndaregister(struct cam_periph *periph, void *arg)
717 struct nda_softc *softc;
719 struct ccb_pathinq cpi;
720 const struct nvme_namespace_data *nsd;
721 const struct nvme_controller_data *cd;
722 char announce_buf[80];
723 uint8_t flbas_fmt, lbads, vwc_present;
727 nsd = nvme_get_identify_ns(periph);
728 cd = nvme_get_identify_cntrl(periph);
730 softc = (struct nda_softc *)malloc(sizeof(*softc), M_DEVBUF,
734 printf("ndaregister: Unable to probe new device. "
735 "Unable to allocate softc\n");
736 return(CAM_REQ_CMP_ERR);
739 if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
740 printf("ndaregister: Unable to probe new device. "
741 "Unable to allocate iosched memory\n");
742 free(softc, M_DEVBUF);
743 return(CAM_REQ_CMP_ERR);
746 /* ident_data parsing */
748 periph->softc = softc;
750 softc->quirks = NDA_Q_NONE;
752 xpt_path_inq(&cpi, periph->path);
754 TASK_INIT(&softc->sysctl_task, 0, ndasysctlinit, periph);
757 * The name space ID is the lun, save it for later I/O
759 softc->nsid = (uint32_t)xpt_path_lun_id(periph->path);
762 * Register this media as a disk
764 (void)cam_periph_hold(periph, PRIBIO);
765 cam_periph_unlock(periph);
766 snprintf(announce_buf, sizeof(announce_buf),
767 "kern.cam.nda.%d.quirks", periph->unit_number);
768 quirks = softc->quirks;
769 TUNABLE_INT_FETCH(announce_buf, &quirks);
770 softc->quirks = quirks;
771 cam_iosched_set_sort_queue(softc->cam_iosched, 0);
772 softc->disk = disk = disk_alloc();
773 disk->d_rotation_rate = DISK_RR_NON_ROTATING;
774 disk->d_open = ndaopen;
775 disk->d_close = ndaclose;
776 disk->d_strategy = ndastrategy;
777 disk->d_getattr = ndagetattr;
778 disk->d_dump = ndadump;
779 disk->d_gone = ndadiskgonecb;
780 disk->d_name = "nda";
781 disk->d_drv1 = periph;
782 disk->d_unit = periph->unit_number;
783 maxio = cpi.maxio; /* Honor max I/O size of SIM */
785 maxio = DFLTPHYS; /* traditional default */
786 else if (maxio > MAXPHYS)
787 maxio = MAXPHYS; /* for safety */
788 disk->d_maxsize = maxio;
789 flbas_fmt = (nsd->flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) &
790 NVME_NS_DATA_FLBAS_FORMAT_MASK;
791 lbads = (nsd->lbaf[flbas_fmt] >> NVME_NS_DATA_LBAF_LBADS_SHIFT) &
792 NVME_NS_DATA_LBAF_LBADS_MASK;
793 disk->d_sectorsize = 1 << lbads;
794 disk->d_mediasize = (off_t)(disk->d_sectorsize * nsd->nsze);
795 disk->d_delmaxsize = disk->d_mediasize;
796 disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
797 if (nvme_ctrlr_has_dataset_mgmt(cd))
798 disk->d_flags |= DISKFLAG_CANDELETE;
799 vwc_present = (cd->vwc >> NVME_CTRLR_DATA_VWC_PRESENT_SHIFT) &
800 NVME_CTRLR_DATA_VWC_PRESENT_MASK;
802 disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
803 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
804 disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
805 softc->unmappedio = 1;
808 * d_ident and d_descr are both far bigger than the length of either
809 * the serial or model number strings.
811 cam_strvis(disk->d_descr, cd->mn,
812 sizeof(disk->d_descr), NVME_MODEL_NUMBER_LENGTH);
813 cam_strvis(disk->d_ident, cd->sn,
814 sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH);
815 disk->d_hba_vendor = cpi.hba_vendor;
816 disk->d_hba_device = cpi.hba_device;
817 disk->d_hba_subvendor = cpi.hba_subvendor;
818 disk->d_hba_subdevice = cpi.hba_subdevice;
819 disk->d_stripesize = disk->d_sectorsize;
820 disk->d_stripeoffset = 0;
821 disk->d_devstat = devstat_new_entry(periph->periph_name,
822 periph->unit_number, disk->d_sectorsize,
823 DEVSTAT_ALL_SUPPORTED,
824 DEVSTAT_TYPE_DIRECT | XPORT_DEVSTAT_TYPE(cpi.transport),
825 DEVSTAT_PRIORITY_DISK);
827 * Add alias for older nvd drives to ease transition.
829 /* disk_add_alias(disk, "nvd"); Have reports of this causing problems */
832 * Acquire a reference to the periph before we register with GEOM.
833 * We'll release this reference once GEOM calls us back (via
834 * ndadiskgonecb()) telling us that our provider has been freed.
836 if (cam_periph_acquire(periph) != 0) {
837 xpt_print(periph->path, "%s: lost periph during "
838 "registration!\n", __func__);
839 cam_periph_lock(periph);
840 return (CAM_REQ_CMP_ERR);
842 disk_create(softc->disk, DISK_VERSION);
843 cam_periph_lock(periph);
844 cam_periph_unhold(periph);
846 snprintf(announce_buf, sizeof(announce_buf),
847 "%juMB (%ju %u byte sectors)",
848 (uintmax_t)((uintmax_t)disk->d_mediasize / (1024*1024)),
849 (uintmax_t)disk->d_mediasize / disk->d_sectorsize,
851 xpt_announce_periph(periph, announce_buf);
852 xpt_announce_quirks(periph, softc->quirks, NDA_Q_BIT_STRING);
855 * Create our sysctl variables, now that we know
856 * we have successfully attached.
858 if (cam_periph_acquire(periph) == 0)
859 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
862 * Register for device going away and info about the drive
863 * changing (though with NVMe, it can't)
865 xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
866 ndaasync, periph, periph->path);
868 softc->state = NDA_STATE_NORMAL;
873 ndastart(struct cam_periph *periph, union ccb *start_ccb)
875 struct nda_softc *softc = (struct nda_softc *)periph->softc;
876 struct ccb_nvmeio *nvmeio = &start_ccb->nvmeio;
878 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart\n"));
880 switch (softc->state) {
881 case NDA_STATE_NORMAL:
885 bp = cam_iosched_next_bio(softc->cam_iosched);
886 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart: bio %p\n", bp));
888 xpt_release_ccb(start_ccb);
892 switch (bp->bio_cmd) {
894 softc->flags |= NDA_FLAG_DIRTY;
898 #ifdef CAM_TEST_FAILURE
902 * Support the failure ioctls. If the command is a
903 * read, and there are pending forced read errors, or
904 * if a write and pending write errors, then fail this
905 * operation with EIO. This is useful for testing
906 * purposes. Also, support having every Nth read fail.
908 * This is a rather blunt tool.
910 if (bp->bio_cmd == BIO_READ) {
911 if (softc->force_read_error) {
912 softc->force_read_error--;
915 if (softc->periodic_read_error > 0) {
916 if (++softc->periodic_read_count >=
917 softc->periodic_read_error) {
918 softc->periodic_read_count = 0;
923 if (softc->force_write_error) {
924 softc->force_write_error--;
929 biofinish(bp, NULL, EIO);
930 xpt_release_ccb(start_ccb);
935 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
936 round_page(bp->bio_bcount + bp->bio_ma_offset) /
937 PAGE_SIZE == bp->bio_ma_n,
938 ("Short bio %p", bp));
939 nda_nvme_rw_bio(softc, &start_ccb->nvmeio, bp, bp->bio_cmd == BIO_READ ?
940 NVME_OPC_READ : NVME_OPC_WRITE);
945 struct nvme_dsm_range *dsm_range, *dsm_end;
946 struct nda_trim_request *trim;
949 uint32_t totalcount = 0, ranges = 0;
951 trim = malloc(sizeof(*trim), M_NVMEDA, M_ZERO | M_NOWAIT);
953 biofinish(bp, NULL, ENOMEM);
954 xpt_release_ccb(start_ccb);
958 TAILQ_INIT(&trim->bps);
960 ents = sizeof(trim->data) / sizeof(struct nvme_dsm_range);
961 ents = min(ents, nda_max_trim_entries);
962 dsm_range = &trim->dsm;
963 dsm_end = dsm_range + ents;
965 TAILQ_INSERT_TAIL(&trim->bps, bp1, bio_queue);
967 htole32(bp1->bio_bcount / softc->disk->d_sectorsize);
968 dsm_range->starting_lba =
969 htole64(bp1->bio_offset / softc->disk->d_sectorsize);
971 totalcount += dsm_range->length;
973 if (dsm_range >= dsm_end)
975 bp1 = cam_iosched_next_trim(softc->cam_iosched);
976 /* XXX -- Could collapse adjacent ranges, but we don't for now */
977 /* XXX -- Could limit based on total payload size */
978 } while (bp1 != NULL);
979 start_ccb->ccb_trim = trim;
980 nda_nvme_trim(softc, &start_ccb->nvmeio, &trim->dsm,
981 dsm_range - &trim->dsm);
982 start_ccb->ccb_state = NDA_CCB_TRIM;
984 softc->trim_ranges += ranges;
985 softc->trim_lbas += totalcount;
987 * Note: We can have multiple TRIMs in flight, so we don't call
988 * cam_iosched_submit_trim(softc->cam_iosched);
989 * since that forces the I/O scheduler to only schedule one at a time.
990 * On NVMe drives, this is a performance disaster.
995 nda_nvme_flush(softc, nvmeio);
998 start_ccb->ccb_state = NDA_CCB_BUFFER_IO;
999 start_ccb->ccb_bp = bp;
1001 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
1002 softc->outstanding_cmds++;
1003 softc->refcount++; /* For submission only */
1004 cam_periph_unlock(periph);
1005 xpt_action(start_ccb);
1006 cam_periph_lock(periph);
1007 softc->refcount--; /* Submission done */
1009 /* May have more work to do, so ensure we stay scheduled */
1010 ndaschedule(periph);
1017 ndadone(struct cam_periph *periph, union ccb *done_ccb)
1019 struct nda_softc *softc;
1020 struct ccb_nvmeio *nvmeio = &done_ccb->nvmeio;
1021 struct cam_path *path;
1024 softc = (struct nda_softc *)periph->softc;
1025 path = done_ccb->ccb_h.path;
1027 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("ndadone\n"));
1029 state = nvmeio->ccb_state & NDA_CCB_TYPE_MASK;
1031 case NDA_CCB_BUFFER_IO:
1036 cam_periph_lock(periph);
1037 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1038 error = ndaerror(done_ccb, 0, 0);
1039 if (error == ERESTART) {
1040 /* A retry was scheduled, so just return. */
1041 cam_periph_unlock(periph);
1044 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1045 cam_release_devq(path,
1049 /*getcount_only*/0);
1051 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1052 panic("REQ_CMP with QFRZN");
1055 if (state == NDA_CCB_BUFFER_IO) {
1058 bp = (struct bio *)done_ccb->ccb_bp;
1059 bp->bio_error = error;
1061 bp->bio_resid = bp->bio_bcount;
1062 bp->bio_flags |= BIO_ERROR;
1066 softc->outstanding_cmds--;
1069 * We need to call cam_iosched before we call biodone so that we
1070 * don't measure any activity that happens in the completion
1071 * routine, which in the case of sendfile can be quite
1074 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
1075 xpt_release_ccb(done_ccb);
1076 ndaschedule(periph);
1077 cam_periph_unlock(periph);
1079 } else { /* state == NDA_CCB_TRIM */
1080 struct nda_trim_request *trim;
1081 struct bio *bp1, *bp2;
1082 TAILQ_HEAD(, bio) queue;
1084 trim = nvmeio->ccb_trim;
1086 TAILQ_CONCAT(&queue, &trim->bps, bio_queue);
1087 free(trim, M_NVMEDA);
1090 * Since we can have multiple trims in flight, we don't
1091 * need to call this here.
1092 * cam_iosched_trim_done(softc->cam_iosched);
1095 * The the I/O scheduler that we're finishing the I/O
1096 * so we can keep book. The first one we pass in the CCB
1097 * which has the timing information. The rest we pass in NULL
1098 * so we can keep proper counts.
1100 bp1 = TAILQ_FIRST(&queue);
1101 cam_iosched_bio_complete(softc->cam_iosched, bp1, done_ccb);
1102 xpt_release_ccb(done_ccb);
1103 softc->outstanding_cmds--;
1104 ndaschedule(periph);
1105 cam_periph_unlock(periph);
1106 while ((bp2 = TAILQ_FIRST(&queue)) != NULL) {
1107 TAILQ_REMOVE(&queue, bp2, bio_queue);
1108 bp2->bio_error = error;
1110 bp2->bio_flags |= BIO_ERROR;
1111 bp2->bio_resid = bp1->bio_bcount;
1115 cam_iosched_bio_complete(softc->cam_iosched, bp2, NULL);
1122 /* No-op. We're polling */
1127 xpt_release_ccb(done_ccb);
1131 ndaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1133 struct nda_softc *softc;
1134 struct cam_periph *periph;
1136 periph = xpt_path_periph(ccb->ccb_h.path);
1137 softc = (struct nda_softc *)periph->softc;
1139 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1140 case CAM_CMD_TIMEOUT:
1145 case CAM_REQ_ABORTED:
1146 case CAM_REQ_CMP_ERR:
1147 case CAM_REQ_TERMIO:
1148 case CAM_UNREC_HBA_ERROR:
1149 case CAM_DATA_RUN_ERR:
1150 case CAM_ATA_STATUS_ERROR:
1159 return(cam_periph_error(ccb, cam_flags, sense_flags));
1163 * Step through all NDA peripheral drivers, and if the device is still open,
1164 * sync the disk cache to physical media.
1169 struct cam_periph *periph;
1170 struct nda_softc *softc;
1174 CAM_PERIPH_FOREACH(periph, &ndadriver) {
1175 softc = (struct nda_softc *)periph->softc;
1177 if (SCHEDULER_STOPPED()) {
1179 * If we paniced with the lock held or the periph is not
1180 * open, do not recurse. Otherwise, call ndadump since
1181 * that avoids the sleeping cam_periph_getccb does if no
1182 * CCBs are available.
1184 if (!cam_periph_owned(periph) &&
1185 (softc->flags & NDA_FLAG_OPEN)) {
1186 ndadump(softc->disk, NULL, 0, 0, 0);
1192 * We only sync the cache if the drive is still open
1194 cam_periph_lock(periph);
1195 if ((softc->flags & NDA_FLAG_OPEN) == 0) {
1196 cam_periph_unlock(periph);
1200 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1201 nda_nvme_flush(softc, &ccb->nvmeio);
1202 error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0,
1203 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1204 softc->disk->d_devstat);
1206 xpt_print(periph->path, "Synchronize cache failed\n");
1207 xpt_release_ccb(ccb);
1208 cam_periph_unlock(periph);
1213 ndashutdown(void *arg, int howto)
1220 ndasuspend(void *arg)