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 */
109 uint32_t nsid; /* Namespace ID for this nda device */
111 struct task sysctl_task;
112 struct sysctl_ctx_list sysctl_ctx;
113 struct sysctl_oid *sysctl_tree;
114 #ifdef CAM_TEST_FAILURE
115 int force_read_error;
116 int force_write_error;
117 int periodic_read_error;
118 int periodic_read_count;
121 struct sysctl_ctx_list sysctl_stats_ctx;
122 struct sysctl_oid *sysctl_stats_tree;
129 struct nda_trim_request {
131 struct nvme_dsm_range dsm;
132 uint8_t data[NVME_MAX_DSM_TRIM];
134 TAILQ_HEAD(, bio) bps;
137 /* Need quirk table */
139 static disk_strategy_t ndastrategy;
140 static dumper_t ndadump;
141 static periph_init_t ndainit;
142 static void ndaasync(void *callback_arg, u_int32_t code,
143 struct cam_path *path, void *arg);
144 static void ndasysctlinit(void *context, int pending);
145 static periph_ctor_t ndaregister;
146 static periph_dtor_t ndacleanup;
147 static periph_start_t ndastart;
148 static periph_oninv_t ndaoninvalidate;
149 static void ndadone(struct cam_periph *periph,
150 union ccb *done_ccb);
151 static int ndaerror(union ccb *ccb, u_int32_t cam_flags,
152 u_int32_t sense_flags);
153 static void ndashutdown(void *arg, int howto);
154 static void ndasuspend(void *arg);
156 #ifndef NDA_DEFAULT_SEND_ORDERED
157 #define NDA_DEFAULT_SEND_ORDERED 1
159 #ifndef NDA_DEFAULT_TIMEOUT
160 #define NDA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
162 #ifndef NDA_DEFAULT_RETRY
163 #define NDA_DEFAULT_RETRY 4
165 #ifndef NDA_MAX_TRIM_ENTRIES
166 #define NDA_MAX_TRIM_ENTRIES (NVME_MAX_DSM_TRIM / sizeof(struct nvme_dsm_range))/* Number of DSM trims to use, max 256 */
169 static SYSCTL_NODE(_kern_cam, OID_AUTO, nda, CTLFLAG_RD, 0,
170 "CAM Direct Access Disk driver");
172 //static int nda_retry_count = NDA_DEFAULT_RETRY;
173 static int nda_send_ordered = NDA_DEFAULT_SEND_ORDERED;
174 static int nda_default_timeout = NDA_DEFAULT_TIMEOUT;
175 static int nda_max_trim_entries = NDA_MAX_TRIM_ENTRIES;
176 SYSCTL_INT(_kern_cam_nda, OID_AUTO, max_trim, CTLFLAG_RDTUN,
177 &nda_max_trim_entries, NDA_MAX_TRIM_ENTRIES,
178 "Maximum number of BIO_DELETE to send down as a DSM TRIM.");
181 * All NVMe media is non-rotational, so all nvme device instances
182 * share this to implement the sysctl.
184 static int nda_rotating_media = 0;
186 static struct periph_driver ndadriver =
189 TAILQ_HEAD_INITIALIZER(ndadriver.units), /* generation */ 0
192 PERIPHDRIVER_DECLARE(nda, ndadriver);
194 static MALLOC_DEFINE(M_NVMEDA, "nvme_da", "nvme_da buffers");
197 * nice wrappers. Maybe these belong in nvme_all.c instead of
198 * here, but this is the only place that uses these. Should
199 * we ever grow another NVME periph, we should move them
200 * all there wholesale.
204 nda_nvme_flush(struct nda_softc *softc, struct ccb_nvmeio *nvmeio)
206 cam_fill_nvmeio(nvmeio,
208 ndadone, /* cbfcnp */
209 CAM_DIR_NONE, /* flags */
212 nda_default_timeout * 1000); /* timeout 30s */
213 nvme_ns_flush_cmd(&nvmeio->cmd, softc->nsid);
217 nda_nvme_trim(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
218 void *payload, uint32_t num_ranges)
220 cam_fill_nvmeio(nvmeio,
222 ndadone, /* cbfcnp */
223 CAM_DIR_OUT, /* flags */
224 payload, /* data_ptr */
225 num_ranges * sizeof(struct nvme_dsm_range), /* dxfer_len */
226 nda_default_timeout * 1000); /* timeout 30s */
227 nvme_ns_trim_cmd(&nvmeio->cmd, softc->nsid, num_ranges);
231 nda_nvme_write(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
232 void *payload, uint64_t lba, uint32_t len, uint32_t count)
234 cam_fill_nvmeio(nvmeio,
236 ndadone, /* cbfcnp */
237 CAM_DIR_OUT, /* flags */
238 payload, /* data_ptr */
240 nda_default_timeout * 1000); /* timeout 30s */
241 nvme_ns_write_cmd(&nvmeio->cmd, softc->nsid, lba, count);
245 nda_nvme_rw_bio(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
246 struct bio *bp, uint32_t rwcmd)
248 int flags = rwcmd == NVME_OPC_READ ? CAM_DIR_IN : CAM_DIR_OUT;
253 if (bp->bio_flags & BIO_UNMAPPED) {
254 flags |= CAM_DATA_BIO;
257 payload = bp->bio_data;
260 lba = bp->bio_pblkno;
261 count = bp->bio_bcount / softc->disk->d_sectorsize;
263 cam_fill_nvmeio(nvmeio,
265 ndadone, /* cbfcnp */
267 payload, /* data_ptr */
268 bp->bio_bcount, /* dxfer_len */
269 nda_default_timeout * 1000); /* timeout 30s */
270 nvme_ns_rw_cmd(&nvmeio->cmd, rwcmd, softc->nsid, lba, count);
274 ndaopen(struct disk *dp)
276 struct cam_periph *periph;
277 struct nda_softc *softc;
280 periph = (struct cam_periph *)dp->d_drv1;
281 if (cam_periph_acquire(periph) != 0) {
285 cam_periph_lock(periph);
286 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
287 cam_periph_unlock(periph);
288 cam_periph_release(periph);
292 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
295 softc = (struct nda_softc *)periph->softc;
296 softc->flags |= NDA_FLAG_OPEN;
298 cam_periph_unhold(periph);
299 cam_periph_unlock(periph);
304 ndaclose(struct disk *dp)
306 struct cam_periph *periph;
307 struct nda_softc *softc;
311 periph = (struct cam_periph *)dp->d_drv1;
312 softc = (struct nda_softc *)periph->softc;
313 cam_periph_lock(periph);
315 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
318 if ((softc->flags & NDA_FLAG_DIRTY) != 0 &&
319 (periph->flags & CAM_PERIPH_INVALID) == 0 &&
320 cam_periph_hold(periph, PRIBIO) == 0) {
322 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
323 nda_nvme_flush(softc, &ccb->nvmeio);
324 error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0,
325 /*sense_flags*/0, softc->disk->d_devstat);
328 xpt_print(periph->path, "Synchronize cache failed\n");
330 softc->flags &= ~NDA_FLAG_DIRTY;
331 xpt_release_ccb(ccb);
332 cam_periph_unhold(periph);
335 softc->flags &= ~NDA_FLAG_OPEN;
337 while (softc->refcount != 0)
338 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "ndaclose", 1);
339 KASSERT(softc->outstanding_cmds == 0,
340 ("nda %d outstanding commands", softc->outstanding_cmds));
341 cam_periph_unlock(periph);
342 cam_periph_release(periph);
347 ndaschedule(struct cam_periph *periph)
349 struct nda_softc *softc = (struct nda_softc *)periph->softc;
351 if (softc->state != NDA_STATE_NORMAL)
354 cam_iosched_schedule(softc->cam_iosched, periph);
358 * Actually translate the requested transfer into one the physical driver
359 * can understand. The transfer is described by a buf and will include
360 * only one physical transfer.
363 ndastrategy(struct bio *bp)
365 struct cam_periph *periph;
366 struct nda_softc *softc;
368 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
369 softc = (struct nda_softc *)periph->softc;
371 cam_periph_lock(periph);
373 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastrategy(%p)\n", bp));
376 * If the device has been made invalid, error out
378 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
379 cam_periph_unlock(periph);
380 biofinish(bp, NULL, ENXIO);
384 if (bp->bio_cmd == BIO_DELETE)
388 * Place it in the queue of disk activities for this disk
390 cam_iosched_queue_work(softc->cam_iosched, bp);
393 * Schedule ourselves for performing the work.
396 cam_periph_unlock(periph);
402 ndadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
404 struct cam_periph *periph;
405 struct nda_softc *softc;
407 struct ccb_nvmeio nvmeio;
415 softc = (struct nda_softc *)periph->softc;
416 secsize = softc->disk->d_sectorsize;
417 lba = offset / secsize;
418 count = length / secsize;
420 if ((periph->flags & CAM_PERIPH_INVALID) != 0)
423 /* xpt_get_ccb returns a zero'd allocation for the ccb, mimic that here */
424 memset(&nvmeio, 0, sizeof(nvmeio));
426 xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
427 nvmeio.ccb_state = NDA_CCB_DUMP;
428 nda_nvme_write(softc, &nvmeio, virtual, lba, length, count);
429 error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error,
430 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
432 printf("Aborting dump due to I/O error %d.\n", error);
438 xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
440 nvmeio.ccb_state = NDA_CCB_DUMP;
441 nda_nvme_flush(softc, &nvmeio);
442 error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error,
443 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
445 xpt_print(periph->path, "flush cmd failed\n");
455 * Install a global async callback. This callback will
456 * receive async callbacks like "new device found".
458 status = xpt_register_async(AC_FOUND_DEVICE, ndaasync, NULL, NULL);
460 if (status != CAM_REQ_CMP) {
461 printf("nda: Failed to attach master async callback "
462 "due to status 0x%x!\n", status);
463 } else if (nda_send_ordered) {
465 /* Register our event handlers */
466 if ((EVENTHANDLER_REGISTER(power_suspend, ndasuspend,
467 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
468 printf("ndainit: power event registration failed!\n");
469 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ndashutdown,
470 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
471 printf("ndainit: shutdown event registration failed!\n");
476 * Callback from GEOM, called when it has finished cleaning up its
480 ndadiskgonecb(struct disk *dp)
482 struct cam_periph *periph;
484 periph = (struct cam_periph *)dp->d_drv1;
486 cam_periph_release(periph);
490 ndaoninvalidate(struct cam_periph *periph)
492 struct nda_softc *softc;
494 softc = (struct nda_softc *)periph->softc;
497 * De-register any async callbacks.
499 xpt_register_async(0, ndaasync, periph, periph->path);
501 softc->invalidations++;
505 * Return all queued I/O with ENXIO.
506 * XXX Handle any transactions queued to the card
507 * with XPT_ABORT_CCB.
509 cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
511 disk_gone(softc->disk);
515 ndacleanup(struct cam_periph *periph)
517 struct nda_softc *softc;
519 softc = (struct nda_softc *)periph->softc;
521 cam_periph_unlock(periph);
523 cam_iosched_fini(softc->cam_iosched);
526 * If we can't free the sysctl tree, oh well...
528 if ((softc->flags & NDA_FLAG_SCTX_INIT) != 0) {
530 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
531 xpt_print(periph->path,
532 "can't remove sysctl stats context\n");
534 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
535 xpt_print(periph->path,
536 "can't remove sysctl context\n");
539 disk_destroy(softc->disk);
540 free(softc, M_DEVBUF);
541 cam_periph_lock(periph);
545 ndaasync(void *callback_arg, u_int32_t code,
546 struct cam_path *path, void *arg)
548 struct cam_periph *periph;
550 periph = (struct cam_periph *)callback_arg;
552 case AC_FOUND_DEVICE:
554 struct ccb_getdev *cgd;
557 cgd = (struct ccb_getdev *)arg;
561 if (cgd->protocol != PROTO_NVME)
565 * Allocate a peripheral instance for
566 * this device and start the probe
569 status = cam_periph_alloc(ndaregister, ndaoninvalidate,
570 ndacleanup, ndastart,
571 "nda", CAM_PERIPH_BIO,
573 AC_FOUND_DEVICE, cgd);
575 if (status != CAM_REQ_CMP
576 && status != CAM_REQ_INPROG)
577 printf("ndaasync: Unable to attach to new device "
578 "due to status 0x%x\n", status);
581 case AC_ADVINFO_CHANGED:
585 buftype = (uintptr_t)arg;
586 if (buftype == CDAI_TYPE_PHYS_PATH) {
587 struct nda_softc *softc;
589 softc = periph->softc;
590 disk_attr_changed(softc->disk, "GEOM::physpath",
597 cam_periph_async(periph, code, path, arg);
603 ndasysctlinit(void *context, int pending)
605 struct cam_periph *periph;
606 struct nda_softc *softc;
607 char tmpstr[32], tmpstr2[16];
609 periph = (struct cam_periph *)context;
611 /* periph was held for us when this task was enqueued */
612 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
613 cam_periph_release(periph);
617 softc = (struct nda_softc *)periph->softc;
618 snprintf(tmpstr, sizeof(tmpstr), "CAM NDA unit %d", periph->unit_number);
619 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
621 sysctl_ctx_init(&softc->sysctl_ctx);
622 softc->flags |= NDA_FLAG_SCTX_INIT;
623 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
624 SYSCTL_STATIC_CHILDREN(_kern_cam_nda), OID_AUTO, tmpstr2,
625 CTLFLAG_RD, 0, tmpstr, "device_index");
626 if (softc->sysctl_tree == NULL) {
627 printf("ndasysctlinit: unable to allocate sysctl tree\n");
628 cam_periph_release(periph);
632 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
633 OID_AUTO, "unmapped_io", CTLFLAG_RD,
634 &softc->unmappedio, 0, "Unmapped I/O leaf");
636 SYSCTL_ADD_QUAD(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
637 OID_AUTO, "deletes", CTLFLAG_RD,
638 &softc->deletes, "Number of BIO_DELETE requests");
640 SYSCTL_ADD_QUAD(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
641 OID_AUTO, "dsm_req", CTLFLAG_RD,
642 &softc->dsm_req, "Number of DSM requests sent to SIM");
644 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
645 OID_AUTO, "rotating", CTLFLAG_RD, &nda_rotating_media, 1,
649 softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
650 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
651 CTLFLAG_RD, 0, "Statistics");
652 if (softc->sysctl_stats_tree == NULL) {
653 printf("ndasysctlinit: unable to allocate sysctl tree for stats\n");
654 cam_periph_release(periph);
657 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
658 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
659 OID_AUTO, "timeouts", CTLFLAG_RD,
661 "Device timeouts reported by the SIM");
662 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
663 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
664 OID_AUTO, "errors", CTLFLAG_RD,
666 "Transport errors reported by the SIM.");
667 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
668 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
669 OID_AUTO, "pack_invalidations", CTLFLAG_RD,
670 &softc->invalidations, 0,
671 "Device pack invalidations.");
674 #ifdef CAM_TEST_FAILURE
675 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
676 OID_AUTO, "invalidate", CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
677 periph, 0, cam_periph_invalidate_sysctl, "I",
678 "Write 1 to invalidate the drive immediately");
681 cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
684 cam_periph_release(periph);
688 ndagetattr(struct bio *bp)
691 struct cam_periph *periph;
693 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
694 cam_periph_lock(periph);
695 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
697 cam_periph_unlock(periph);
699 bp->bio_completed = bp->bio_length;
704 ndaregister(struct cam_periph *periph, void *arg)
706 struct nda_softc *softc;
708 struct ccb_pathinq cpi;
709 const struct nvme_namespace_data *nsd;
710 const struct nvme_controller_data *cd;
711 char announce_buf[80];
712 uint8_t flbas_fmt, lbads, vwc_present;
716 nsd = nvme_get_identify_ns(periph);
717 cd = nvme_get_identify_cntrl(periph);
719 softc = (struct nda_softc *)malloc(sizeof(*softc), M_DEVBUF,
723 printf("ndaregister: Unable to probe new device. "
724 "Unable to allocate softc\n");
725 return(CAM_REQ_CMP_ERR);
728 if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
729 printf("ndaregister: Unable to probe new device. "
730 "Unable to allocate iosched memory\n");
731 free(softc, M_DEVBUF);
732 return(CAM_REQ_CMP_ERR);
735 /* ident_data parsing */
737 periph->softc = softc;
739 softc->quirks = NDA_Q_NONE;
741 xpt_path_inq(&cpi, periph->path);
743 TASK_INIT(&softc->sysctl_task, 0, ndasysctlinit, periph);
746 * The name space ID is the lun, save it for later I/O
748 softc->nsid = (uint32_t)xpt_path_lun_id(periph->path);
751 * Register this media as a disk
753 (void)cam_periph_hold(periph, PRIBIO);
754 cam_periph_unlock(periph);
755 snprintf(announce_buf, sizeof(announce_buf),
756 "kern.cam.nda.%d.quirks", periph->unit_number);
757 quirks = softc->quirks;
758 TUNABLE_INT_FETCH(announce_buf, &quirks);
759 softc->quirks = quirks;
760 cam_iosched_set_sort_queue(softc->cam_iosched, 0);
761 softc->disk = disk = disk_alloc();
762 strlcpy(softc->disk->d_descr, cd->mn,
763 MIN(sizeof(softc->disk->d_descr), sizeof(cd->mn)));
764 strlcpy(softc->disk->d_ident, cd->sn,
765 MIN(sizeof(softc->disk->d_ident), sizeof(cd->sn)));
766 disk->d_rotation_rate = DISK_RR_NON_ROTATING;
767 disk->d_open = ndaopen;
768 disk->d_close = ndaclose;
769 disk->d_strategy = ndastrategy;
770 disk->d_getattr = ndagetattr;
771 disk->d_dump = ndadump;
772 disk->d_gone = ndadiskgonecb;
773 disk->d_name = "nda";
774 disk->d_drv1 = periph;
775 disk->d_unit = periph->unit_number;
776 maxio = cpi.maxio; /* Honor max I/O size of SIM */
778 maxio = DFLTPHYS; /* traditional default */
779 else if (maxio > MAXPHYS)
780 maxio = MAXPHYS; /* for safety */
781 disk->d_maxsize = maxio;
782 flbas_fmt = (nsd->flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) &
783 NVME_NS_DATA_FLBAS_FORMAT_MASK;
784 lbads = (nsd->lbaf[flbas_fmt] >> NVME_NS_DATA_LBAF_LBADS_SHIFT) &
785 NVME_NS_DATA_LBAF_LBADS_MASK;
786 disk->d_sectorsize = 1 << lbads;
787 disk->d_mediasize = (off_t)(disk->d_sectorsize * nsd->nsze);
788 disk->d_delmaxsize = disk->d_mediasize;
789 disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
790 // if (cd->oncs.dsm) // XXX broken?
791 disk->d_flags |= DISKFLAG_CANDELETE;
792 vwc_present = (cd->vwc >> NVME_CTRLR_DATA_VWC_PRESENT_SHIFT) &
793 NVME_CTRLR_DATA_VWC_PRESENT_MASK;
795 disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
796 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
797 disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
798 softc->unmappedio = 1;
801 * d_ident and d_descr are both far bigger than the length of either
802 * the serial or model number strings.
804 nvme_strvis(disk->d_descr, cd->mn,
805 sizeof(disk->d_descr), NVME_MODEL_NUMBER_LENGTH);
806 nvme_strvis(disk->d_ident, cd->sn,
807 sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH);
808 disk->d_hba_vendor = cpi.hba_vendor;
809 disk->d_hba_device = cpi.hba_device;
810 disk->d_hba_subvendor = cpi.hba_subvendor;
811 disk->d_hba_subdevice = cpi.hba_subdevice;
812 disk->d_stripesize = disk->d_sectorsize;
813 disk->d_stripeoffset = 0;
814 disk->d_devstat = devstat_new_entry(periph->periph_name,
815 periph->unit_number, disk->d_sectorsize,
816 DEVSTAT_ALL_SUPPORTED,
817 DEVSTAT_TYPE_DIRECT | XPORT_DEVSTAT_TYPE(cpi.transport),
818 DEVSTAT_PRIORITY_DISK);
820 * Add alias for older nvd drives to ease transition.
822 /* disk_add_alias(disk, "nvd"); Have reports of this causing problems */
825 * Acquire a reference to the periph before we register with GEOM.
826 * We'll release this reference once GEOM calls us back (via
827 * ndadiskgonecb()) telling us that our provider has been freed.
829 if (cam_periph_acquire(periph) != 0) {
830 xpt_print(periph->path, "%s: lost periph during "
831 "registration!\n", __func__);
832 cam_periph_lock(periph);
833 return (CAM_REQ_CMP_ERR);
835 disk_create(softc->disk, DISK_VERSION);
836 cam_periph_lock(periph);
837 cam_periph_unhold(periph);
839 snprintf(announce_buf, sizeof(announce_buf),
840 "%juMB (%ju %u byte sectors)",
841 (uintmax_t)((uintmax_t)disk->d_mediasize / (1024*1024)),
842 (uintmax_t)disk->d_mediasize / disk->d_sectorsize,
844 xpt_announce_periph(periph, announce_buf);
845 xpt_announce_quirks(periph, softc->quirks, NDA_Q_BIT_STRING);
848 * Create our sysctl variables, now that we know
849 * we have successfully attached.
851 if (cam_periph_acquire(periph) == 0)
852 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
855 * Register for device going away and info about the drive
856 * changing (though with NVMe, it can't)
858 xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
859 ndaasync, periph, periph->path);
861 softc->state = NDA_STATE_NORMAL;
866 ndastart(struct cam_periph *periph, union ccb *start_ccb)
868 struct nda_softc *softc = (struct nda_softc *)periph->softc;
869 struct ccb_nvmeio *nvmeio = &start_ccb->nvmeio;
871 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart\n"));
873 switch (softc->state) {
874 case NDA_STATE_NORMAL:
878 bp = cam_iosched_next_bio(softc->cam_iosched);
879 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart: bio %p\n", bp));
881 xpt_release_ccb(start_ccb);
885 switch (bp->bio_cmd) {
887 softc->flags |= NDA_FLAG_DIRTY;
891 #ifdef CAM_TEST_FAILURE
895 * Support the failure ioctls. If the command is a
896 * read, and there are pending forced read errors, or
897 * if a write and pending write errors, then fail this
898 * operation with EIO. This is useful for testing
899 * purposes. Also, support having every Nth read fail.
901 * This is a rather blunt tool.
903 if (bp->bio_cmd == BIO_READ) {
904 if (softc->force_read_error) {
905 softc->force_read_error--;
908 if (softc->periodic_read_error > 0) {
909 if (++softc->periodic_read_count >=
910 softc->periodic_read_error) {
911 softc->periodic_read_count = 0;
916 if (softc->force_write_error) {
917 softc->force_write_error--;
922 biofinish(bp, NULL, EIO);
923 xpt_release_ccb(start_ccb);
928 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
929 round_page(bp->bio_bcount + bp->bio_ma_offset) /
930 PAGE_SIZE == bp->bio_ma_n,
931 ("Short bio %p", bp));
932 nda_nvme_rw_bio(softc, &start_ccb->nvmeio, bp, bp->bio_cmd == BIO_READ ?
933 NVME_OPC_READ : NVME_OPC_WRITE);
938 struct nvme_dsm_range *dsm_range, *dsm_end;
939 struct nda_trim_request *trim;
943 trim = malloc(sizeof(*trim), M_NVMEDA, M_ZERO | M_NOWAIT);
945 biofinish(bp, NULL, ENOMEM);
946 xpt_release_ccb(start_ccb);
950 TAILQ_INIT(&trim->bps);
952 ents = sizeof(trim->data) / sizeof(struct nvme_dsm_range);
953 ents = min(ents, nda_max_trim_entries);
954 dsm_range = &trim->dsm;
955 dsm_end = dsm_range + ents;
957 TAILQ_INSERT_TAIL(&trim->bps, bp1, bio_queue);
959 htole32(bp1->bio_bcount / softc->disk->d_sectorsize);
960 dsm_range->starting_lba =
961 htole64(bp1->bio_offset / softc->disk->d_sectorsize);
963 if (dsm_range >= dsm_end)
965 bp1 = cam_iosched_next_trim(softc->cam_iosched);
966 /* XXX -- Could collapse adjacent ranges, but we don't for now */
967 /* XXX -- Could limit based on total payload size */
968 } while (bp1 != NULL);
969 start_ccb->ccb_trim = trim;
971 nda_nvme_trim(softc, &start_ccb->nvmeio, &trim->dsm,
972 dsm_range - &trim->dsm);
973 start_ccb->ccb_state = NDA_CCB_TRIM;
975 * Note: We can have multiple TRIMs in flight, so we don't call
976 * cam_iosched_submit_trim(softc->cam_iosched);
977 * since that forces the I/O scheduler to only schedule one at a time.
978 * On NVMe drives, this is a performance disaster.
983 nda_nvme_flush(softc, nvmeio);
986 start_ccb->ccb_state = NDA_CCB_BUFFER_IO;
987 start_ccb->ccb_bp = bp;
989 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
990 softc->outstanding_cmds++;
991 softc->refcount++; /* For submission only */
992 cam_periph_unlock(periph);
993 xpt_action(start_ccb);
994 cam_periph_lock(periph);
995 softc->refcount--; /* Submission done */
997 /* May have more work to do, so ensure we stay scheduled */
1005 ndadone(struct cam_periph *periph, union ccb *done_ccb)
1007 struct nda_softc *softc;
1008 struct ccb_nvmeio *nvmeio = &done_ccb->nvmeio;
1009 struct cam_path *path;
1012 softc = (struct nda_softc *)periph->softc;
1013 path = done_ccb->ccb_h.path;
1015 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("ndadone\n"));
1017 state = nvmeio->ccb_state & NDA_CCB_TYPE_MASK;
1019 case NDA_CCB_BUFFER_IO:
1024 cam_periph_lock(periph);
1025 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1026 error = ndaerror(done_ccb, 0, 0);
1027 if (error == ERESTART) {
1028 /* A retry was scheduled, so just return. */
1029 cam_periph_unlock(periph);
1032 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1033 cam_release_devq(path,
1037 /*getcount_only*/0);
1039 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1040 panic("REQ_CMP with QFRZN");
1043 if (state == NDA_CCB_BUFFER_IO) {
1046 bp = (struct bio *)done_ccb->ccb_bp;
1047 bp->bio_error = error;
1049 bp->bio_resid = bp->bio_bcount;
1050 bp->bio_flags |= BIO_ERROR;
1054 softc->outstanding_cmds--;
1057 * We need to call cam_iosched before we call biodone so that we
1058 * don't measure any activity that happens in the completion
1059 * routine, which in the case of sendfile can be quite
1062 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
1063 xpt_release_ccb(done_ccb);
1064 ndaschedule(periph);
1065 cam_periph_unlock(periph);
1067 } else { /* state == NDA_CCB_TRIM */
1068 struct nda_trim_request *trim;
1069 struct bio *bp1, *bp2;
1070 TAILQ_HEAD(, bio) queue;
1072 trim = nvmeio->ccb_trim;
1074 TAILQ_CONCAT(&queue, &trim->bps, bio_queue);
1075 free(trim, M_NVMEDA);
1078 * Since we can have multiple trims in flight, we don't
1079 * need to call this here.
1080 * cam_iosched_trim_done(softc->cam_iosched);
1083 * The the I/O scheduler that we're finishing the I/O
1084 * so we can keep book. The first one we pass in the CCB
1085 * which has the timing information. The rest we pass in NULL
1086 * so we can keep proper counts.
1088 bp1 = TAILQ_FIRST(&queue);
1089 cam_iosched_bio_complete(softc->cam_iosched, bp1, done_ccb);
1090 xpt_release_ccb(done_ccb);
1091 softc->outstanding_cmds--;
1092 ndaschedule(periph);
1093 cam_periph_unlock(periph);
1094 while ((bp2 = TAILQ_FIRST(&queue)) != NULL) {
1095 TAILQ_REMOVE(&queue, bp2, bio_queue);
1096 bp2->bio_error = error;
1098 bp2->bio_flags |= BIO_ERROR;
1099 bp2->bio_resid = bp1->bio_bcount;
1103 cam_iosched_bio_complete(softc->cam_iosched, bp2, NULL);
1110 /* No-op. We're polling */
1115 xpt_release_ccb(done_ccb);
1119 ndaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1121 struct nda_softc *softc;
1122 struct cam_periph *periph;
1124 periph = xpt_path_periph(ccb->ccb_h.path);
1125 softc = (struct nda_softc *)periph->softc;
1127 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1128 case CAM_CMD_TIMEOUT:
1133 case CAM_REQ_ABORTED:
1134 case CAM_REQ_CMP_ERR:
1135 case CAM_REQ_TERMIO:
1136 case CAM_UNREC_HBA_ERROR:
1137 case CAM_DATA_RUN_ERR:
1138 case CAM_ATA_STATUS_ERROR:
1147 return(cam_periph_error(ccb, cam_flags, sense_flags));
1151 * Step through all NDA peripheral drivers, and if the device is still open,
1152 * sync the disk cache to physical media.
1157 struct cam_periph *periph;
1158 struct nda_softc *softc;
1162 CAM_PERIPH_FOREACH(periph, &ndadriver) {
1163 softc = (struct nda_softc *)periph->softc;
1165 if (SCHEDULER_STOPPED()) {
1167 * If we paniced with the lock held or the periph is not
1168 * open, do not recurse. Otherwise, call ndadump since
1169 * that avoids the sleeping cam_periph_getccb does if no
1170 * CCBs are available.
1172 if (!cam_periph_owned(periph) &&
1173 (softc->flags & NDA_FLAG_OPEN)) {
1174 ndadump(softc->disk, NULL, 0, 0, 0);
1180 * We only sync the cache if the drive is still open
1182 cam_periph_lock(periph);
1183 if ((softc->flags & NDA_FLAG_OPEN) == 0) {
1184 cam_periph_unlock(periph);
1188 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1189 nda_nvme_flush(softc, &ccb->nvmeio);
1190 error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0,
1191 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1192 softc->disk->d_devstat);
1194 xpt_print(periph->path, "Synchronize cache failed\n");
1195 xpt_release_ccb(ccb);
1196 cam_periph_unlock(periph);
1201 ndashutdown(void *arg, int howto)
1208 ndasuspend(void *arg)