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 cam_periph_unlock(periph);
340 cam_periph_release(periph);
345 ndaschedule(struct cam_periph *periph)
347 struct nda_softc *softc = (struct nda_softc *)periph->softc;
349 if (softc->state != NDA_STATE_NORMAL)
352 cam_iosched_schedule(softc->cam_iosched, periph);
356 * Actually translate the requested transfer into one the physical driver
357 * can understand. The transfer is described by a buf and will include
358 * only one physical transfer.
361 ndastrategy(struct bio *bp)
363 struct cam_periph *periph;
364 struct nda_softc *softc;
366 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
367 softc = (struct nda_softc *)periph->softc;
369 cam_periph_lock(periph);
371 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastrategy(%p)\n", bp));
374 * If the device has been made invalid, error out
376 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
377 cam_periph_unlock(periph);
378 biofinish(bp, NULL, ENXIO);
382 if (bp->bio_cmd == BIO_DELETE)
386 * Place it in the queue of disk activities for this disk
388 cam_iosched_queue_work(softc->cam_iosched, bp);
391 * Schedule ourselves for performing the work.
394 cam_periph_unlock(periph);
400 ndadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
402 struct cam_periph *periph;
403 struct nda_softc *softc;
405 struct ccb_nvmeio nvmeio;
413 softc = (struct nda_softc *)periph->softc;
414 secsize = softc->disk->d_sectorsize;
415 lba = offset / secsize;
416 count = length / secsize;
418 if ((periph->flags & CAM_PERIPH_INVALID) != 0)
421 /* xpt_get_ccb returns a zero'd allocation for the ccb, mimic that here */
422 memset(&nvmeio, 0, sizeof(nvmeio));
424 xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
425 nvmeio.ccb_state = NDA_CCB_DUMP;
426 nda_nvme_write(softc, &nvmeio, virtual, lba, length, count);
427 error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error,
428 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
430 printf("Aborting dump due to I/O error %d.\n", error);
436 xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
438 nvmeio.ccb_state = NDA_CCB_DUMP;
439 nda_nvme_flush(softc, &nvmeio);
440 error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error,
441 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
443 xpt_print(periph->path, "flush cmd failed\n");
453 * Install a global async callback. This callback will
454 * receive async callbacks like "new device found".
456 status = xpt_register_async(AC_FOUND_DEVICE, ndaasync, NULL, NULL);
458 if (status != CAM_REQ_CMP) {
459 printf("nda: Failed to attach master async callback "
460 "due to status 0x%x!\n", status);
461 } else if (nda_send_ordered) {
463 /* Register our event handlers */
464 if ((EVENTHANDLER_REGISTER(power_suspend, ndasuspend,
465 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
466 printf("ndainit: power event registration failed!\n");
467 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ndashutdown,
468 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
469 printf("ndainit: shutdown event registration failed!\n");
474 * Callback from GEOM, called when it has finished cleaning up its
478 ndadiskgonecb(struct disk *dp)
480 struct cam_periph *periph;
482 periph = (struct cam_periph *)dp->d_drv1;
484 cam_periph_release(periph);
488 ndaoninvalidate(struct cam_periph *periph)
490 struct nda_softc *softc;
492 softc = (struct nda_softc *)periph->softc;
495 * De-register any async callbacks.
497 xpt_register_async(0, ndaasync, periph, periph->path);
499 softc->invalidations++;
503 * Return all queued I/O with ENXIO.
504 * XXX Handle any transactions queued to the card
505 * with XPT_ABORT_CCB.
507 cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
509 disk_gone(softc->disk);
513 ndacleanup(struct cam_periph *periph)
515 struct nda_softc *softc;
517 softc = (struct nda_softc *)periph->softc;
519 cam_periph_unlock(periph);
521 cam_iosched_fini(softc->cam_iosched);
524 * If we can't free the sysctl tree, oh well...
526 if ((softc->flags & NDA_FLAG_SCTX_INIT) != 0) {
528 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
529 xpt_print(periph->path,
530 "can't remove sysctl stats context\n");
532 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
533 xpt_print(periph->path,
534 "can't remove sysctl context\n");
537 disk_destroy(softc->disk);
538 free(softc, M_DEVBUF);
539 cam_periph_lock(periph);
543 ndaasync(void *callback_arg, u_int32_t code,
544 struct cam_path *path, void *arg)
546 struct cam_periph *periph;
548 periph = (struct cam_periph *)callback_arg;
550 case AC_FOUND_DEVICE:
552 struct ccb_getdev *cgd;
555 cgd = (struct ccb_getdev *)arg;
559 if (cgd->protocol != PROTO_NVME)
563 * Allocate a peripheral instance for
564 * this device and start the probe
567 status = cam_periph_alloc(ndaregister, ndaoninvalidate,
568 ndacleanup, ndastart,
569 "nda", CAM_PERIPH_BIO,
571 AC_FOUND_DEVICE, cgd);
573 if (status != CAM_REQ_CMP
574 && status != CAM_REQ_INPROG)
575 printf("ndaasync: Unable to attach to new device "
576 "due to status 0x%x\n", status);
579 case AC_ADVINFO_CHANGED:
583 buftype = (uintptr_t)arg;
584 if (buftype == CDAI_TYPE_PHYS_PATH) {
585 struct nda_softc *softc;
587 softc = periph->softc;
588 disk_attr_changed(softc->disk, "GEOM::physpath",
595 cam_periph_async(periph, code, path, arg);
601 ndasysctlinit(void *context, int pending)
603 struct cam_periph *periph;
604 struct nda_softc *softc;
605 char tmpstr[32], tmpstr2[16];
607 periph = (struct cam_periph *)context;
609 /* periph was held for us when this task was enqueued */
610 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
611 cam_periph_release(periph);
615 softc = (struct nda_softc *)periph->softc;
616 snprintf(tmpstr, sizeof(tmpstr), "CAM NDA unit %d", periph->unit_number);
617 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
619 sysctl_ctx_init(&softc->sysctl_ctx);
620 softc->flags |= NDA_FLAG_SCTX_INIT;
621 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
622 SYSCTL_STATIC_CHILDREN(_kern_cam_nda), OID_AUTO, tmpstr2,
623 CTLFLAG_RD, 0, tmpstr, "device_index");
624 if (softc->sysctl_tree == NULL) {
625 printf("ndasysctlinit: unable to allocate sysctl tree\n");
626 cam_periph_release(periph);
630 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
631 OID_AUTO, "unmapped_io", CTLFLAG_RD,
632 &softc->unmappedio, 0, "Unmapped I/O leaf");
634 SYSCTL_ADD_QUAD(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
635 OID_AUTO, "deletes", CTLFLAG_RD,
636 &softc->deletes, "Number of BIO_DELETE requests");
638 SYSCTL_ADD_QUAD(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
639 OID_AUTO, "dsm_req", CTLFLAG_RD,
640 &softc->dsm_req, "Number of DSM requests sent to SIM");
642 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
643 OID_AUTO, "rotating", CTLFLAG_RD, &nda_rotating_media, 1,
647 softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
648 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
649 CTLFLAG_RD, 0, "Statistics");
650 if (softc->sysctl_stats_tree == NULL) {
651 printf("ndasysctlinit: unable to allocate sysctl tree for stats\n");
652 cam_periph_release(periph);
655 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
656 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
657 OID_AUTO, "timeouts", CTLFLAG_RD,
659 "Device timeouts reported by the SIM");
660 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
661 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
662 OID_AUTO, "errors", CTLFLAG_RD,
664 "Transport errors reported by the SIM.");
665 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
666 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
667 OID_AUTO, "pack_invalidations", CTLFLAG_RD,
668 &softc->invalidations, 0,
669 "Device pack invalidations.");
672 #ifdef CAM_TEST_FAILURE
673 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
674 OID_AUTO, "invalidate", CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
675 periph, 0, cam_periph_invalidate_sysctl, "I",
676 "Write 1 to invalidate the drive immediately");
679 cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
682 cam_periph_release(periph);
686 ndagetattr(struct bio *bp)
689 struct cam_periph *periph;
691 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
692 cam_periph_lock(periph);
693 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
695 cam_periph_unlock(periph);
697 bp->bio_completed = bp->bio_length;
702 ndaregister(struct cam_periph *periph, void *arg)
704 struct nda_softc *softc;
706 struct ccb_pathinq cpi;
707 const struct nvme_namespace_data *nsd;
708 const struct nvme_controller_data *cd;
709 char announce_buf[80];
710 uint8_t flbas_fmt, lbads, vwc_present;
714 nsd = nvme_get_identify_ns(periph);
715 cd = nvme_get_identify_cntrl(periph);
717 softc = (struct nda_softc *)malloc(sizeof(*softc), M_DEVBUF,
721 printf("ndaregister: Unable to probe new device. "
722 "Unable to allocate softc\n");
723 return(CAM_REQ_CMP_ERR);
726 if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
727 printf("ndaregister: Unable to probe new device. "
728 "Unable to allocate iosched memory\n");
729 free(softc, M_DEVBUF);
730 return(CAM_REQ_CMP_ERR);
733 /* ident_data parsing */
735 periph->softc = softc;
737 softc->quirks = NDA_Q_NONE;
739 xpt_path_inq(&cpi, periph->path);
741 TASK_INIT(&softc->sysctl_task, 0, ndasysctlinit, periph);
744 * The name space ID is the lun, save it for later I/O
746 softc->nsid = (uint32_t)xpt_path_lun_id(periph->path);
749 * Register this media as a disk
751 (void)cam_periph_hold(periph, PRIBIO);
752 cam_periph_unlock(periph);
753 snprintf(announce_buf, sizeof(announce_buf),
754 "kern.cam.nda.%d.quirks", periph->unit_number);
755 quirks = softc->quirks;
756 TUNABLE_INT_FETCH(announce_buf, &quirks);
757 softc->quirks = quirks;
758 cam_iosched_set_sort_queue(softc->cam_iosched, 0);
759 softc->disk = disk = disk_alloc();
760 strlcpy(softc->disk->d_descr, cd->mn,
761 MIN(sizeof(softc->disk->d_descr), sizeof(cd->mn)));
762 strlcpy(softc->disk->d_ident, cd->sn,
763 MIN(sizeof(softc->disk->d_ident), sizeof(cd->sn)));
764 disk->d_rotation_rate = DISK_RR_NON_ROTATING;
765 disk->d_open = ndaopen;
766 disk->d_close = ndaclose;
767 disk->d_strategy = ndastrategy;
768 disk->d_getattr = ndagetattr;
769 disk->d_dump = ndadump;
770 disk->d_gone = ndadiskgonecb;
771 disk->d_name = "nda";
772 disk->d_drv1 = periph;
773 disk->d_unit = periph->unit_number;
774 maxio = cpi.maxio; /* Honor max I/O size of SIM */
776 maxio = DFLTPHYS; /* traditional default */
777 else if (maxio > MAXPHYS)
778 maxio = MAXPHYS; /* for safety */
779 disk->d_maxsize = maxio;
780 flbas_fmt = (nsd->flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) &
781 NVME_NS_DATA_FLBAS_FORMAT_MASK;
782 lbads = (nsd->lbaf[flbas_fmt] >> NVME_NS_DATA_LBAF_LBADS_SHIFT) &
783 NVME_NS_DATA_LBAF_LBADS_MASK;
784 disk->d_sectorsize = 1 << lbads;
785 disk->d_mediasize = (off_t)(disk->d_sectorsize * nsd->nsze);
786 disk->d_delmaxsize = disk->d_mediasize;
787 disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
788 // if (cd->oncs.dsm) // XXX broken?
789 disk->d_flags |= DISKFLAG_CANDELETE;
790 vwc_present = (cd->vwc >> NVME_CTRLR_DATA_VWC_PRESENT_SHIFT) &
791 NVME_CTRLR_DATA_VWC_PRESENT_MASK;
793 disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
794 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
795 disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
796 softc->unmappedio = 1;
799 * d_ident and d_descr are both far bigger than the length of either
800 * the serial or model number strings.
802 nvme_strvis(disk->d_descr, cd->mn,
803 sizeof(disk->d_descr), NVME_MODEL_NUMBER_LENGTH);
804 nvme_strvis(disk->d_ident, cd->sn,
805 sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH);
806 disk->d_hba_vendor = cpi.hba_vendor;
807 disk->d_hba_device = cpi.hba_device;
808 disk->d_hba_subvendor = cpi.hba_subvendor;
809 disk->d_hba_subdevice = cpi.hba_subdevice;
810 disk->d_stripesize = disk->d_sectorsize;
811 disk->d_stripeoffset = 0;
812 disk->d_devstat = devstat_new_entry(periph->periph_name,
813 periph->unit_number, disk->d_sectorsize,
814 DEVSTAT_ALL_SUPPORTED,
815 DEVSTAT_TYPE_DIRECT | XPORT_DEVSTAT_TYPE(cpi.transport),
816 DEVSTAT_PRIORITY_DISK);
818 * Add alias for older nvd drives to ease transition.
820 /* disk_add_alias(disk, "nvd"); Have reports of this causing problems */
823 * Acquire a reference to the periph before we register with GEOM.
824 * We'll release this reference once GEOM calls us back (via
825 * ndadiskgonecb()) telling us that our provider has been freed.
827 if (cam_periph_acquire(periph) != 0) {
828 xpt_print(periph->path, "%s: lost periph during "
829 "registration!\n", __func__);
830 cam_periph_lock(periph);
831 return (CAM_REQ_CMP_ERR);
833 disk_create(softc->disk, DISK_VERSION);
834 cam_periph_lock(periph);
835 cam_periph_unhold(periph);
837 snprintf(announce_buf, sizeof(announce_buf),
838 "%juMB (%ju %u byte sectors)",
839 (uintmax_t)((uintmax_t)disk->d_mediasize / (1024*1024)),
840 (uintmax_t)disk->d_mediasize / disk->d_sectorsize,
842 xpt_announce_periph(periph, announce_buf);
843 xpt_announce_quirks(periph, softc->quirks, NDA_Q_BIT_STRING);
846 * Create our sysctl variables, now that we know
847 * we have successfully attached.
849 if (cam_periph_acquire(periph) == 0)
850 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
853 * Register for device going away and info about the drive
854 * changing (though with NVMe, it can't)
856 xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
857 ndaasync, periph, periph->path);
859 softc->state = NDA_STATE_NORMAL;
864 ndastart(struct cam_periph *periph, union ccb *start_ccb)
866 struct nda_softc *softc = (struct nda_softc *)periph->softc;
867 struct ccb_nvmeio *nvmeio = &start_ccb->nvmeio;
869 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart\n"));
871 switch (softc->state) {
872 case NDA_STATE_NORMAL:
876 bp = cam_iosched_next_bio(softc->cam_iosched);
877 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart: bio %p\n", bp));
879 xpt_release_ccb(start_ccb);
883 switch (bp->bio_cmd) {
885 softc->flags |= NDA_FLAG_DIRTY;
889 #ifdef CAM_TEST_FAILURE
893 * Support the failure ioctls. If the command is a
894 * read, and there are pending forced read errors, or
895 * if a write and pending write errors, then fail this
896 * operation with EIO. This is useful for testing
897 * purposes. Also, support having every Nth read fail.
899 * This is a rather blunt tool.
901 if (bp->bio_cmd == BIO_READ) {
902 if (softc->force_read_error) {
903 softc->force_read_error--;
906 if (softc->periodic_read_error > 0) {
907 if (++softc->periodic_read_count >=
908 softc->periodic_read_error) {
909 softc->periodic_read_count = 0;
914 if (softc->force_write_error) {
915 softc->force_write_error--;
920 biofinish(bp, NULL, EIO);
921 xpt_release_ccb(start_ccb);
926 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
927 round_page(bp->bio_bcount + bp->bio_ma_offset) /
928 PAGE_SIZE == bp->bio_ma_n,
929 ("Short bio %p", bp));
930 nda_nvme_rw_bio(softc, &start_ccb->nvmeio, bp, bp->bio_cmd == BIO_READ ?
931 NVME_OPC_READ : NVME_OPC_WRITE);
936 struct nvme_dsm_range *dsm_range, *dsm_end;
937 struct nda_trim_request *trim;
941 trim = malloc(sizeof(*trim), M_NVMEDA, M_ZERO | M_NOWAIT);
943 biofinish(bp, NULL, ENOMEM);
944 xpt_release_ccb(start_ccb);
948 TAILQ_INIT(&trim->bps);
950 ents = sizeof(trim->data) / sizeof(struct nvme_dsm_range);
951 ents = min(ents, nda_max_trim_entries);
952 dsm_range = &trim->dsm;
953 dsm_end = dsm_range + ents;
955 TAILQ_INSERT_TAIL(&trim->bps, bp1, bio_queue);
957 htole32(bp1->bio_bcount / softc->disk->d_sectorsize);
958 dsm_range->starting_lba =
959 htole64(bp1->bio_offset / softc->disk->d_sectorsize);
961 if (dsm_range >= dsm_end)
963 bp1 = cam_iosched_next_trim(softc->cam_iosched);
964 /* XXX -- Could collapse adjacent ranges, but we don't for now */
965 /* XXX -- Could limit based on total payload size */
966 } while (bp1 != NULL);
967 start_ccb->ccb_trim = trim;
969 nda_nvme_trim(softc, &start_ccb->nvmeio, &trim->dsm,
970 dsm_range - &trim->dsm);
971 start_ccb->ccb_state = NDA_CCB_TRIM;
973 * Note: We can have multiple TRIMs in flight, so we don't call
974 * cam_iosched_submit_trim(softc->cam_iosched);
975 * since that forces the I/O scheduler to only schedule one at a time.
976 * On NVMe drives, this is a performance disaster.
981 nda_nvme_flush(softc, nvmeio);
984 start_ccb->ccb_state = NDA_CCB_BUFFER_IO;
985 start_ccb->ccb_bp = bp;
987 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
988 softc->outstanding_cmds++;
990 cam_periph_unlock(periph);
991 xpt_action(start_ccb);
992 cam_periph_lock(periph);
994 /* May have more work to do, so ensure we stay scheduled */
1002 ndadone(struct cam_periph *periph, union ccb *done_ccb)
1004 struct nda_softc *softc;
1005 struct ccb_nvmeio *nvmeio = &done_ccb->nvmeio;
1006 struct cam_path *path;
1009 softc = (struct nda_softc *)periph->softc;
1010 path = done_ccb->ccb_h.path;
1012 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("ndadone\n"));
1014 state = nvmeio->ccb_state & NDA_CCB_TYPE_MASK;
1016 case NDA_CCB_BUFFER_IO:
1021 cam_periph_lock(periph);
1022 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1023 error = ndaerror(done_ccb, 0, 0);
1024 if (error == ERESTART) {
1025 /* A retry was scheduled, so just return. */
1026 cam_periph_unlock(periph);
1029 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1030 cam_release_devq(path,
1034 /*getcount_only*/0);
1036 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1037 panic("REQ_CMP with QFRZN");
1040 if (state == NDA_CCB_BUFFER_IO) {
1043 bp = (struct bio *)done_ccb->ccb_bp;
1044 bp->bio_error = error;
1046 bp->bio_resid = bp->bio_bcount;
1047 bp->bio_flags |= BIO_ERROR;
1051 softc->outstanding_cmds--;
1054 * We need to call cam_iosched before we call biodone so that we
1055 * don't measure any activity that happens in the completion
1056 * routine, which in the case of sendfile can be quite
1059 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
1060 xpt_release_ccb(done_ccb);
1061 ndaschedule(periph);
1062 cam_periph_unlock(periph);
1064 } else { /* state == NDA_CCB_TRIM */
1065 struct nda_trim_request *trim;
1066 struct bio *bp1, *bp2;
1067 TAILQ_HEAD(, bio) queue;
1069 trim = nvmeio->ccb_trim;
1071 TAILQ_CONCAT(&queue, &trim->bps, bio_queue);
1072 free(trim, M_NVMEDA);
1075 * Since we can have multiple trims in flight, we don't
1076 * need to call this here.
1077 * cam_iosched_trim_done(softc->cam_iosched);
1080 * The the I/O scheduler that we're finishing the I/O
1081 * so we can keep book. The first one we pass in the CCB
1082 * which has the timing information. The rest we pass in NULL
1083 * so we can keep proper counts.
1085 bp1 = TAILQ_FIRST(&queue);
1086 cam_iosched_bio_complete(softc->cam_iosched, bp1, done_ccb);
1087 xpt_release_ccb(done_ccb);
1088 ndaschedule(periph);
1089 cam_periph_unlock(periph);
1090 while ((bp2 = TAILQ_FIRST(&queue)) != NULL) {
1091 TAILQ_REMOVE(&queue, bp2, bio_queue);
1092 bp2->bio_error = error;
1094 bp2->bio_flags |= BIO_ERROR;
1095 bp2->bio_resid = bp1->bio_bcount;
1099 cam_iosched_bio_complete(softc->cam_iosched, bp2, NULL);
1104 * Release the periph refcount taken in mdastart() for each CCB.
1106 KASSERT(softc->refcount >= 1, ("ndadone softc %p refcount %d", softc, softc->refcount));
1111 /* No-op. We're polling */
1116 xpt_release_ccb(done_ccb);
1120 ndaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1122 struct nda_softc *softc;
1123 struct cam_periph *periph;
1125 periph = xpt_path_periph(ccb->ccb_h.path);
1126 softc = (struct nda_softc *)periph->softc;
1128 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1129 case CAM_CMD_TIMEOUT:
1134 case CAM_REQ_ABORTED:
1135 case CAM_REQ_CMP_ERR:
1136 case CAM_REQ_TERMIO:
1137 case CAM_UNREC_HBA_ERROR:
1138 case CAM_DATA_RUN_ERR:
1139 case CAM_ATA_STATUS_ERROR:
1148 return(cam_periph_error(ccb, cam_flags, sense_flags));
1152 * Step through all NDA peripheral drivers, and if the device is still open,
1153 * sync the disk cache to physical media.
1158 struct cam_periph *periph;
1159 struct nda_softc *softc;
1163 CAM_PERIPH_FOREACH(periph, &ndadriver) {
1164 softc = (struct nda_softc *)periph->softc;
1166 if (SCHEDULER_STOPPED()) {
1168 * If we paniced with the lock held or the periph is not
1169 * open, do not recurse. Otherwise, call ndadump since
1170 * that avoids the sleeping cam_periph_getccb does if no
1171 * CCBs are available.
1173 if (!cam_periph_owned(periph) &&
1174 (softc->flags & NDA_FLAG_OPEN)) {
1175 ndadump(softc->disk, NULL, 0, 0, 0);
1181 * We only sync the cache if the drive is still open
1183 cam_periph_lock(periph);
1184 if ((softc->flags & NDA_FLAG_OPEN) == 0) {
1185 cam_periph_unlock(periph);
1189 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1190 nda_nvme_flush(softc, &ccb->nvmeio);
1191 error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0,
1192 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1193 softc->disk->d_devstat);
1195 xpt_print(periph->path, "Synchronize cache failed\n");
1196 xpt_release_ccb(ccb);
1197 cam_periph_unlock(periph);
1202 ndashutdown(void *arg, int howto)
1209 ndasuspend(void *arg)