2 * Copyright (c) 2015 Netflix, Inc
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * Derived from ata_da.c:
27 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/taskqueue.h>
42 #include <sys/mutex.h>
44 #include <sys/devicestat.h>
45 #include <sys/eventhandler.h>
46 #include <sys/malloc.h>
49 #include <sys/reboot.h>
50 #include <geom/geom_disk.h>
59 #include <cam/cam_ccb.h>
60 #include <cam/cam_periph.h>
61 #include <cam/cam_xpt_periph.h>
62 #include <cam/cam_sim.h>
63 #include <cam/cam_iosched.h>
65 #include <cam/nvme/nvme_all.h>
72 NDA_FLAG_OPEN = 0x0001,
73 NDA_FLAG_DIRTY = 0x0002,
74 NDA_FLAG_SCTX_INIT = 0x0004,
82 #define NDA_Q_BIT_STRING \
87 NDA_CCB_BUFFER_IO = 0x01,
90 NDA_CCB_TYPE_MASK = 0x0F,
93 /* Offsets into our private area for storing information */
94 #define ccb_state ppriv_field0
95 #define ccb_bp ppriv_ptr1
98 TAILQ_HEAD(, bio) bps;
101 struct cam_iosched_softc *cam_iosched;
102 int outstanding_cmds; /* Number of active commands */
103 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;
113 struct trim_request trim_req;
115 struct sysctl_ctx_list sysctl_stats_ctx;
116 struct sysctl_oid *sysctl_stats_tree;
123 /* Need quirk table */
125 static disk_strategy_t ndastrategy;
126 static dumper_t ndadump;
127 static periph_init_t ndainit;
128 static void ndaasync(void *callback_arg, u_int32_t code,
129 struct cam_path *path, void *arg);
130 static void ndasysctlinit(void *context, int pending);
131 static periph_ctor_t ndaregister;
132 static periph_dtor_t ndacleanup;
133 static periph_start_t ndastart;
134 static periph_oninv_t ndaoninvalidate;
135 static void ndadone(struct cam_periph *periph,
136 union ccb *done_ccb);
137 static int ndaerror(union ccb *ccb, u_int32_t cam_flags,
138 u_int32_t sense_flags);
139 static void ndashutdown(void *arg, int howto);
140 static void ndasuspend(void *arg);
142 #ifndef NDA_DEFAULT_SEND_ORDERED
143 #define NDA_DEFAULT_SEND_ORDERED 1
145 #ifndef NDA_DEFAULT_TIMEOUT
146 #define NDA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
148 #ifndef NDA_DEFAULT_RETRY
149 #define NDA_DEFAULT_RETRY 4
153 //static int nda_retry_count = NDA_DEFAULT_RETRY;
154 static int nda_send_ordered = NDA_DEFAULT_SEND_ORDERED;
155 static int nda_default_timeout = NDA_DEFAULT_TIMEOUT;
158 * All NVMe media is non-rotational, so all nvme device instances
159 * share this to implement the sysctl.
161 static int nda_rotating_media = 0;
163 static SYSCTL_NODE(_kern_cam, OID_AUTO, nda, CTLFLAG_RD, 0,
164 "CAM Direct Access Disk driver");
166 static struct periph_driver ndadriver =
169 TAILQ_HEAD_INITIALIZER(ndadriver.units), /* generation */ 0
172 PERIPHDRIVER_DECLARE(nda, ndadriver);
174 static MALLOC_DEFINE(M_NVMEDA, "nvme_da", "nvme_da buffers");
177 * nice wrappers. Maybe these belong in nvme_all.c instead of
178 * here, but this is the only place that uses these. Should
179 * we ever grow another NVME periph, we should move them
180 * all there wholesale.
184 nda_nvme_flush(struct nda_softc *softc, struct ccb_nvmeio *nvmeio)
186 cam_fill_nvmeio(nvmeio,
188 ndadone, /* cbfcnp */
189 CAM_DIR_NONE, /* flags */
192 nda_default_timeout * 1000); /* timeout 5s */
193 nvme_ns_flush_cmd(&nvmeio->cmd, softc->nsid);
197 nda_nvme_trim(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
198 void *payload, uint32_t num_ranges)
200 cam_fill_nvmeio(nvmeio,
202 ndadone, /* cbfcnp */
203 CAM_DIR_OUT, /* flags */
204 payload, /* data_ptr */
205 num_ranges * sizeof(struct nvme_dsm_range), /* dxfer_len */
206 nda_default_timeout * 1000); /* timeout 5s */
207 nvme_ns_trim_cmd(&nvmeio->cmd, softc->nsid, num_ranges);
211 nda_nvme_write(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
212 void *payload, uint64_t lba, uint32_t len, uint32_t count)
214 cam_fill_nvmeio(nvmeio,
216 ndadone, /* cbfcnp */
217 CAM_DIR_OUT, /* flags */
218 payload, /* data_ptr */
220 nda_default_timeout * 1000); /* timeout 5s */
221 nvme_ns_write_cmd(&nvmeio->cmd, softc->nsid, lba, count);
225 nda_nvme_rw_bio(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
226 struct bio *bp, uint32_t rwcmd)
228 int flags = rwcmd == NVME_OPC_READ ? CAM_DIR_IN : CAM_DIR_OUT;
233 if (bp->bio_flags & BIO_UNMAPPED) {
234 flags |= CAM_DATA_BIO;
237 payload = bp->bio_data;
240 lba = bp->bio_pblkno;
241 count = bp->bio_bcount / softc->disk->d_sectorsize;
243 cam_fill_nvmeio(nvmeio,
245 ndadone, /* cbfcnp */
247 payload, /* data_ptr */
248 bp->bio_bcount, /* dxfer_len */
249 nda_default_timeout * 1000); /* timeout 5s */
250 nvme_ns_rw_cmd(&nvmeio->cmd, rwcmd, softc->nsid, lba, count);
254 ndaopen(struct disk *dp)
256 struct cam_periph *periph;
257 struct nda_softc *softc;
260 periph = (struct cam_periph *)dp->d_drv1;
261 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
265 cam_periph_lock(periph);
266 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
267 cam_periph_unlock(periph);
268 cam_periph_release(periph);
272 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
275 softc = (struct nda_softc *)periph->softc;
276 softc->flags |= NDA_FLAG_OPEN;
278 cam_periph_unhold(periph);
279 cam_periph_unlock(periph);
284 ndaclose(struct disk *dp)
286 struct cam_periph *periph;
287 struct nda_softc *softc;
291 periph = (struct cam_periph *)dp->d_drv1;
292 softc = (struct nda_softc *)periph->softc;
293 cam_periph_lock(periph);
295 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
298 if ((softc->flags & NDA_FLAG_DIRTY) != 0 &&
299 (periph->flags & CAM_PERIPH_INVALID) == 0 &&
300 cam_periph_hold(periph, PRIBIO) == 0) {
302 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
303 nda_nvme_flush(softc, &ccb->nvmeio);
304 error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0,
305 /*sense_flags*/0, softc->disk->d_devstat);
308 xpt_print(periph->path, "Synchronize cache failed\n");
310 softc->flags &= ~NDA_FLAG_DIRTY;
311 xpt_release_ccb(ccb);
312 cam_periph_unhold(periph);
315 softc->flags &= ~NDA_FLAG_OPEN;
317 while (softc->refcount != 0)
318 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "ndaclose", 1);
319 cam_periph_unlock(periph);
320 cam_periph_release(periph);
325 ndaschedule(struct cam_periph *periph)
327 struct nda_softc *softc = (struct nda_softc *)periph->softc;
329 if (softc->state != NDA_STATE_NORMAL)
332 cam_iosched_schedule(softc->cam_iosched, periph);
336 * Actually translate the requested transfer into one the physical driver
337 * can understand. The transfer is described by a buf and will include
338 * only one physical transfer.
341 ndastrategy(struct bio *bp)
343 struct cam_periph *periph;
344 struct nda_softc *softc;
346 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
347 softc = (struct nda_softc *)periph->softc;
349 cam_periph_lock(periph);
351 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastrategy(%p)\n", bp));
354 * If the device has been made invalid, error out
356 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
357 cam_periph_unlock(periph);
358 biofinish(bp, NULL, ENXIO);
363 * Place it in the queue of disk activities for this disk
365 cam_iosched_queue_work(softc->cam_iosched, bp);
368 * Schedule ourselves for performing the work.
371 cam_periph_unlock(periph);
377 ndadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
379 struct cam_periph *periph;
380 struct nda_softc *softc;
390 softc = (struct nda_softc *)periph->softc;
391 cam_periph_lock(periph);
392 secsize = softc->disk->d_sectorsize;
393 lba = offset / secsize;
394 count = length / secsize;
396 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
397 cam_periph_unlock(periph);
402 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
403 ccb.ccb_h.ccb_state = NDA_CCB_DUMP;
404 nda_nvme_write(softc, &ccb.nvmeio, virtual, lba, length, count);
405 xpt_polled_action(&ccb);
407 error = cam_periph_error(&ccb,
408 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
409 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
410 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
411 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
413 printf("Aborting dump due to I/O error.\n");
415 cam_periph_unlock(periph);
420 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
422 ccb.ccb_h.ccb_state = NDA_CCB_DUMP;
423 nda_nvme_flush(softc, &ccb.nvmeio);
424 xpt_polled_action(&ccb);
426 error = cam_periph_error(&ccb,
427 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
428 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
429 cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
430 /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
432 xpt_print(periph->path, "flush cmd failed\n");
433 cam_periph_unlock(periph);
443 * Install a global async callback. This callback will
444 * receive async callbacks like "new device found".
446 status = xpt_register_async(AC_FOUND_DEVICE, ndaasync, NULL, NULL);
448 if (status != CAM_REQ_CMP) {
449 printf("nda: Failed to attach master async callback "
450 "due to status 0x%x!\n", status);
451 } else if (nda_send_ordered) {
453 /* Register our event handlers */
454 if ((EVENTHANDLER_REGISTER(power_suspend, ndasuspend,
455 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
456 printf("ndainit: power event registration failed!\n");
457 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ndashutdown,
458 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
459 printf("ndainit: shutdown event registration failed!\n");
464 * Callback from GEOM, called when it has finished cleaning up its
468 ndadiskgonecb(struct disk *dp)
470 struct cam_periph *periph;
472 periph = (struct cam_periph *)dp->d_drv1;
474 cam_periph_release(periph);
478 ndaoninvalidate(struct cam_periph *periph)
480 struct nda_softc *softc;
482 softc = (struct nda_softc *)periph->softc;
485 * De-register any async callbacks.
487 xpt_register_async(0, ndaasync, periph, periph->path);
489 softc->invalidations++;
493 * Return all queued I/O with ENXIO.
494 * XXX Handle any transactions queued to the card
495 * with XPT_ABORT_CCB.
497 cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
499 disk_gone(softc->disk);
503 ndacleanup(struct cam_periph *periph)
505 struct nda_softc *softc;
507 softc = (struct nda_softc *)periph->softc;
509 cam_periph_unlock(periph);
511 cam_iosched_fini(softc->cam_iosched);
514 * If we can't free the sysctl tree, oh well...
516 if ((softc->flags & NDA_FLAG_SCTX_INIT) != 0) {
518 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
519 xpt_print(periph->path,
520 "can't remove sysctl stats context\n");
522 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
523 xpt_print(periph->path,
524 "can't remove sysctl context\n");
527 disk_destroy(softc->disk);
528 free(softc, M_DEVBUF);
529 cam_periph_lock(periph);
533 ndaasync(void *callback_arg, u_int32_t code,
534 struct cam_path *path, void *arg)
536 struct cam_periph *periph;
538 periph = (struct cam_periph *)callback_arg;
540 case AC_FOUND_DEVICE:
542 struct ccb_getdev *cgd;
545 cgd = (struct ccb_getdev *)arg;
549 if (cgd->protocol != PROTO_NVME)
553 * Allocate a peripheral instance for
554 * this device and start the probe
557 status = cam_periph_alloc(ndaregister, ndaoninvalidate,
558 ndacleanup, ndastart,
559 "nda", CAM_PERIPH_BIO,
561 AC_FOUND_DEVICE, cgd);
563 if (status != CAM_REQ_CMP
564 && status != CAM_REQ_INPROG)
565 printf("ndaasync: Unable to attach to new device "
566 "due to status 0x%x\n", status);
569 case AC_ADVINFO_CHANGED:
573 buftype = (uintptr_t)arg;
574 if (buftype == CDAI_TYPE_PHYS_PATH) {
575 struct nda_softc *softc;
577 softc = periph->softc;
578 disk_attr_changed(softc->disk, "GEOM::physpath",
585 cam_periph_async(periph, code, path, arg);
591 ndasysctlinit(void *context, int pending)
593 struct cam_periph *periph;
594 struct nda_softc *softc;
595 char tmpstr[80], tmpstr2[80];
597 periph = (struct cam_periph *)context;
599 /* periph was held for us when this task was enqueued */
600 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
601 cam_periph_release(periph);
605 softc = (struct nda_softc *)periph->softc;
606 snprintf(tmpstr, sizeof(tmpstr), "CAM NDA unit %d", periph->unit_number);
607 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
609 sysctl_ctx_init(&softc->sysctl_ctx);
610 softc->flags |= NDA_FLAG_SCTX_INIT;
611 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
612 SYSCTL_STATIC_CHILDREN(_kern_cam_nda), OID_AUTO, tmpstr2,
613 CTLFLAG_RD, 0, tmpstr);
614 if (softc->sysctl_tree == NULL) {
615 printf("ndasysctlinit: unable to allocate sysctl tree\n");
616 cam_periph_release(periph);
620 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
621 OID_AUTO, "unmapped_io", CTLFLAG_RD | CTLFLAG_MPSAFE,
622 &softc->unmappedio, 0, "Unmapped I/O leaf");
624 SYSCTL_ADD_INT(&softc->sysctl_ctx,
625 SYSCTL_CHILDREN(softc->sysctl_tree),
628 CTLFLAG_RD | CTLFLAG_MPSAFE,
634 softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
635 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
636 CTLFLAG_RD, 0, "Statistics");
637 if (softc->sysctl_stats_tree == NULL) {
638 printf("ndasysctlinit: unable to allocate sysctl tree for stats\n");
639 cam_periph_release(periph);
642 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
643 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
644 OID_AUTO, "timeouts", CTLFLAG_RD | CTLFLAG_MPSAFE,
646 "Device timeouts reported by the SIM");
647 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
648 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
649 OID_AUTO, "errors", CTLFLAG_RD | CTLFLAG_MPSAFE,
651 "Transport errors reported by the SIM.");
652 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
653 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
654 OID_AUTO, "pack_invalidations", CTLFLAG_RD | CTLFLAG_MPSAFE,
655 &softc->invalidations, 0,
656 "Device pack invalidations.");
659 cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
662 cam_periph_release(periph);
666 ndagetattr(struct bio *bp)
669 struct cam_periph *periph;
671 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
672 cam_periph_lock(periph);
673 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
675 cam_periph_unlock(periph);
677 bp->bio_completed = bp->bio_length;
682 ndaregister(struct cam_periph *periph, void *arg)
684 struct nda_softc *softc;
686 struct ccb_pathinq cpi;
687 struct ccb_getdev *cgd;
688 const struct nvme_namespace_data *nsd;
689 const struct nvme_controller_data *cd;
690 char announce_buf[80];
695 cgd = (struct ccb_getdev *)arg;
697 printf("ndaregister: no getdev CCB, can't register device\n");
698 return(CAM_REQ_CMP_ERR);
700 nsd = cgd->nvme_data;
701 cd = cgd->nvme_cdata;
703 softc = (struct nda_softc *)malloc(sizeof(*softc), M_DEVBUF,
707 printf("ndaregister: Unable to probe new device. "
708 "Unable to allocate softc\n");
709 return(CAM_REQ_CMP_ERR);
712 if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
713 printf("ndaregister: Unable to probe new device. "
714 "Unable to allocate iosched memory\n");
715 return(CAM_REQ_CMP_ERR);
718 /* ident_data parsing */
720 periph->softc = softc;
724 * See if this device has any quirks.
726 match = cam_quirkmatch((caddr_t)&cgd->ident_data,
727 (caddr_t)nda_quirk_table,
728 sizeof(nda_quirk_table)/sizeof(*nda_quirk_table),
729 sizeof(*nda_quirk_table), ata_identify_match);
731 softc->quirks = ((struct nda_quirk_entry *)match)->quirks;
734 softc->quirks = NDA_Q_NONE;
736 bzero(&cpi, sizeof(cpi));
737 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
738 cpi.ccb_h.func_code = XPT_PATH_INQ;
739 xpt_action((union ccb *)&cpi);
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 = (uint16_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 = 0; /* Spinning rust need not apply */
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 disk->d_sectorsize = 1 << nsd->lbaf[nsd->flbas.format].lbads;
781 disk->d_mediasize = (off_t)(disk->d_sectorsize * nsd->nsze);
782 disk->d_delmaxsize = disk->d_mediasize;
783 disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
784 // if (cd->oncs.dsm) // XXX broken?
785 disk->d_flags |= DISKFLAG_CANDELETE;
787 disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
788 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
789 disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
790 softc->unmappedio = 1;
793 * d_ident and d_descr are both far bigger than the length of either
794 * the serial or model number strings.
796 nvme_strvis(disk->d_descr, cd->mn,
797 sizeof(disk->d_descr), NVME_MODEL_NUMBER_LENGTH);
798 nvme_strvis(disk->d_ident, cd->sn,
799 sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH);
800 disk->d_hba_vendor = cpi.hba_vendor;
801 disk->d_hba_device = cpi.hba_device;
802 disk->d_hba_subvendor = cpi.hba_subvendor;
803 disk->d_hba_subdevice = cpi.hba_subdevice;
804 disk->d_stripesize = disk->d_sectorsize;
805 disk->d_stripeoffset = 0;
806 disk->d_devstat = devstat_new_entry(periph->periph_name,
807 periph->unit_number, disk->d_sectorsize,
808 DEVSTAT_ALL_SUPPORTED,
809 DEVSTAT_TYPE_DIRECT | XPORT_DEVSTAT_TYPE(cpi.transport),
810 DEVSTAT_PRIORITY_DISK);
813 * Acquire a reference to the periph before we register with GEOM.
814 * We'll release this reference once GEOM calls us back (via
815 * ndadiskgonecb()) telling us that our provider has been freed.
817 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
818 xpt_print(periph->path, "%s: lost periph during "
819 "registration!\n", __func__);
820 cam_periph_lock(periph);
821 return (CAM_REQ_CMP_ERR);
823 disk_create(softc->disk, DISK_VERSION);
824 cam_periph_lock(periph);
825 cam_periph_unhold(periph);
827 snprintf(announce_buf, sizeof(announce_buf),
828 "%juMB (%ju %u byte sectors)",
829 (uintmax_t)((uintmax_t)disk->d_mediasize / (1024*1024)),
830 (uintmax_t)disk->d_mediasize / disk->d_sectorsize,
832 xpt_announce_periph(periph, announce_buf);
833 xpt_announce_quirks(periph, softc->quirks, NDA_Q_BIT_STRING);
836 * Create our sysctl variables, now that we know
837 * we have successfully attached.
839 if (cam_periph_acquire(periph) == CAM_REQ_CMP)
840 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
843 * Register for device going away and info about the drive
844 * changing (though with NVMe, it can't)
846 xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
847 ndaasync, periph, periph->path);
849 softc->state = NDA_STATE_NORMAL;
854 ndastart(struct cam_periph *periph, union ccb *start_ccb)
856 struct nda_softc *softc = (struct nda_softc *)periph->softc;
857 struct ccb_nvmeio *nvmeio = &start_ccb->nvmeio;
859 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart\n"));
861 switch (softc->state) {
862 case NDA_STATE_NORMAL:
866 bp = cam_iosched_next_bio(softc->cam_iosched);
867 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart: bio %p\n", bp));
869 xpt_release_ccb(start_ccb);
873 switch (bp->bio_cmd) {
875 softc->flags |= NDA_FLAG_DIRTY;
879 #ifdef NDA_TEST_FAILURE
883 * Support the failure ioctls. If the command is a
884 * read, and there are pending forced read errors, or
885 * if a write and pending write errors, then fail this
886 * operation with EIO. This is useful for testing
887 * purposes. Also, support having every Nth read fail.
889 * This is a rather blunt tool.
891 if (bp->bio_cmd == BIO_READ) {
892 if (softc->force_read_error) {
893 softc->force_read_error--;
896 if (softc->periodic_read_error > 0) {
897 if (++softc->periodic_read_count >=
898 softc->periodic_read_error) {
899 softc->periodic_read_count = 0;
904 if (softc->force_write_error) {
905 softc->force_write_error--;
910 biofinish(bp, NULL, EIO);
911 xpt_release_ccb(start_ccb);
916 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
917 round_page(bp->bio_bcount + bp->bio_ma_offset) /
918 PAGE_SIZE == bp->bio_ma_n,
919 ("Short bio %p", bp));
920 nda_nvme_rw_bio(softc, &start_ccb->nvmeio, bp, bp->bio_cmd == BIO_READ ?
921 NVME_OPC_READ : NVME_OPC_WRITE);
926 struct nvme_dsm_range *dsm_range;
929 malloc(sizeof(*dsm_range), M_NVMEDA, M_ZERO | M_WAITOK);
931 bp->bio_bcount / softc->disk->d_sectorsize;
932 dsm_range->starting_lba =
933 bp->bio_offset / softc->disk->d_sectorsize;
934 bp->bio_driver2 = dsm_range;
935 nda_nvme_trim(softc, &start_ccb->nvmeio, dsm_range, 1);
936 start_ccb->ccb_h.ccb_state = NDA_CCB_TRIM;
937 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
938 cam_iosched_submit_trim(softc->cam_iosched); /* XXX */
942 nda_nvme_flush(softc, nvmeio);
945 start_ccb->ccb_h.ccb_state = NDA_CCB_BUFFER_IO;
946 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
948 start_ccb->ccb_h.ccb_bp = bp;
949 softc->outstanding_cmds++;
951 cam_periph_unlock(periph);
952 xpt_action(start_ccb);
953 cam_periph_lock(periph);
956 /* May have more work to do, so ensure we stay scheduled */
964 ndadone(struct cam_periph *periph, union ccb *done_ccb)
966 struct nda_softc *softc;
967 struct ccb_nvmeio *nvmeio = &done_ccb->nvmeio;
968 struct cam_path *path;
971 softc = (struct nda_softc *)periph->softc;
972 path = done_ccb->ccb_h.path;
974 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("ndadone\n"));
976 state = nvmeio->ccb_h.ccb_state & NDA_CCB_TYPE_MASK;
978 case NDA_CCB_BUFFER_IO:
984 cam_periph_lock(periph);
985 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
986 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
987 error = ndaerror(done_ccb, 0, 0);
988 if (error == ERESTART) {
989 /* A retry was scheduled, so just return. */
990 cam_periph_unlock(periph);
993 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
994 cam_release_devq(path,
1000 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1001 panic("REQ_CMP with QFRZN");
1004 bp->bio_error = error;
1006 bp->bio_resid = bp->bio_bcount;
1007 bp->bio_flags |= BIO_ERROR;
1009 if (state == NDA_CCB_TRIM)
1012 bp->bio_resid = nvmeio->resid;
1013 if (bp->bio_resid > 0)
1014 bp->bio_flags |= BIO_ERROR;
1016 if (state == NDA_CCB_TRIM)
1017 free(bp->bio_driver2, M_NVMEDA);
1018 softc->outstanding_cmds--;
1020 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
1021 xpt_release_ccb(done_ccb);
1022 if (state == NDA_CCB_TRIM) {
1024 TAILQ_HEAD(, bio) queue;
1028 TAILQ_CONCAT(&queue, &softc->trim_req.bps, bio_queue);
1030 cam_iosched_trim_done(softc->cam_iosched);
1031 ndaschedule(periph);
1032 cam_periph_unlock(periph);
1034 /* Not yet collapsing several BIO_DELETE requests into one TRIM */
1035 while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
1036 TAILQ_REMOVE(&queue, bp1, bio_queue);
1037 bp1->bio_error = error;
1039 bp1->bio_flags |= BIO_ERROR;
1040 bp1->bio_resid = bp1->bio_bcount;
1049 ndaschedule(periph);
1050 cam_periph_unlock(periph);
1056 /* No-op. We're polling */
1061 xpt_release_ccb(done_ccb);
1065 ndaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1067 struct nda_softc *softc;
1068 struct cam_periph *periph;
1070 periph = xpt_path_periph(ccb->ccb_h.path);
1071 softc = (struct nda_softc *)periph->softc;
1073 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1074 case CAM_CMD_TIMEOUT:
1079 case CAM_REQ_ABORTED:
1080 case CAM_REQ_CMP_ERR:
1081 case CAM_REQ_TERMIO:
1082 case CAM_UNREC_HBA_ERROR:
1083 case CAM_DATA_RUN_ERR:
1084 case CAM_ATA_STATUS_ERROR:
1093 return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1097 * Step through all NDA peripheral drivers, and if the device is still open,
1098 * sync the disk cache to physical media.
1103 struct cam_periph *periph;
1104 struct nda_softc *softc;
1108 CAM_PERIPH_FOREACH(periph, &ndadriver) {
1109 softc = (struct nda_softc *)periph->softc;
1110 if (SCHEDULER_STOPPED()) {
1111 /* If we paniced with the lock held, do not recurse. */
1112 if (!cam_periph_owned(periph) &&
1113 (softc->flags & NDA_FLAG_OPEN)) {
1114 ndadump(softc->disk, NULL, 0, 0, 0);
1118 cam_periph_lock(periph);
1120 * We only sync the cache if the drive is still open, and
1121 * if the drive is capable of it..
1123 if ((softc->flags & NDA_FLAG_OPEN) == 0) {
1124 cam_periph_unlock(periph);
1128 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1129 nda_nvme_flush(softc, &ccb->nvmeio);
1130 error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0,
1131 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1132 softc->disk->d_devstat);
1134 xpt_print(periph->path, "Synchronize cache failed\n");
1135 xpt_release_ccb(ccb);
1136 cam_periph_unlock(periph);
1141 ndashutdown(void *arg, int howto)
1148 ndasuspend(void *arg)