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.h>
52 #include <geom/geom_disk.h>
61 #include <cam/cam_ccb.h>
62 #include <cam/cam_periph.h>
63 #include <cam/cam_xpt_periph.h>
64 #include <cam/cam_sim.h>
65 #include <cam/cam_iosched.h>
67 #include <cam/nvme/nvme_all.h>
74 NDA_FLAG_OPEN = 0x0001,
75 NDA_FLAG_DIRTY = 0x0002,
76 NDA_FLAG_SCTX_INIT = 0x0004,
84 #define NDA_Q_BIT_STRING \
89 NDA_CCB_BUFFER_IO = 0x01,
92 NDA_CCB_TYPE_MASK = 0x0F,
95 /* Offsets into our private area for storing information */
96 #define ccb_state ccb_h.ppriv_field0
97 #define ccb_bp ccb_h.ppriv_ptr1 /* For NDA_CCB_BUFFER_IO */
98 #define ccb_trim ccb_h.ppriv_ptr1 /* For NDA_CCB_TRIM */
101 struct cam_iosched_softc *cam_iosched;
102 int outstanding_cmds; /* Number of active commands */
103 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;
115 uint64_t trim_ranges;
117 #ifdef CAM_TEST_FAILURE
118 int force_read_error;
119 int force_write_error;
120 int periodic_read_error;
121 int periodic_read_count;
124 struct sysctl_ctx_list sysctl_stats_ctx;
125 struct sysctl_oid *sysctl_stats_tree;
132 struct nda_trim_request {
133 struct nvme_dsm_range dsm[NVME_MAX_DSM_TRIM / sizeof(struct nvme_dsm_range)];
134 TAILQ_HEAD(, bio) bps;
136 _Static_assert(NVME_MAX_DSM_TRIM % sizeof(struct nvme_dsm_range) == 0,
137 "NVME_MAX_DSM_TRIM must be an integral number of ranges");
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 static int nda_enable_biospeedup = 1;
179 SYSCTL_INT(_kern_cam_nda, OID_AUTO, max_trim, CTLFLAG_RDTUN,
180 &nda_max_trim_entries, NDA_MAX_TRIM_ENTRIES,
181 "Maximum number of BIO_DELETE to send down as a DSM TRIM.");
182 SYSCTL_INT(_kern_cam_nda, OID_AUTO, enable_biospeedup, CTLFLAG_RDTUN,
183 &nda_enable_biospeedup, 0, "Enable BIO_SPEEDUP processing");
186 * All NVMe media is non-rotational, so all nvme device instances
187 * share this to implement the sysctl.
189 static int nda_rotating_media = 0;
191 static struct periph_driver ndadriver =
194 TAILQ_HEAD_INITIALIZER(ndadriver.units), /* generation */ 0
197 PERIPHDRIVER_DECLARE(nda, ndadriver);
199 static MALLOC_DEFINE(M_NVMEDA, "nvme_da", "nvme_da buffers");
202 * nice wrappers. Maybe these belong in nvme_all.c instead of
203 * here, but this is the only place that uses these. Should
204 * we ever grow another NVME periph, we should move them
205 * all there wholesale.
209 nda_nvme_flush(struct nda_softc *softc, struct ccb_nvmeio *nvmeio)
211 cam_fill_nvmeio(nvmeio,
213 ndadone, /* cbfcnp */
214 CAM_DIR_NONE, /* flags */
217 nda_default_timeout * 1000); /* timeout 30s */
218 nvme_ns_flush_cmd(&nvmeio->cmd, softc->nsid);
222 nda_nvme_trim(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
223 void *payload, uint32_t num_ranges)
225 cam_fill_nvmeio(nvmeio,
227 ndadone, /* cbfcnp */
228 CAM_DIR_OUT, /* flags */
229 payload, /* data_ptr */
230 num_ranges * sizeof(struct nvme_dsm_range), /* dxfer_len */
231 nda_default_timeout * 1000); /* timeout 30s */
232 nvme_ns_trim_cmd(&nvmeio->cmd, softc->nsid, num_ranges);
236 nda_nvme_write(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
237 void *payload, uint64_t lba, uint32_t len, uint32_t count)
239 cam_fill_nvmeio(nvmeio,
241 ndadone, /* cbfcnp */
242 CAM_DIR_OUT, /* flags */
243 payload, /* data_ptr */
245 nda_default_timeout * 1000); /* timeout 30s */
246 nvme_ns_write_cmd(&nvmeio->cmd, softc->nsid, lba, count);
250 nda_nvme_rw_bio(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
251 struct bio *bp, uint32_t rwcmd)
253 int flags = rwcmd == NVME_OPC_READ ? CAM_DIR_IN : CAM_DIR_OUT;
258 if (bp->bio_flags & BIO_UNMAPPED) {
259 flags |= CAM_DATA_BIO;
262 payload = bp->bio_data;
265 lba = bp->bio_pblkno;
266 count = bp->bio_bcount / softc->disk->d_sectorsize;
268 cam_fill_nvmeio(nvmeio,
270 ndadone, /* cbfcnp */
272 payload, /* data_ptr */
273 bp->bio_bcount, /* dxfer_len */
274 nda_default_timeout * 1000); /* timeout 30s */
275 nvme_ns_rw_cmd(&nvmeio->cmd, rwcmd, softc->nsid, lba, count);
279 ndaopen(struct disk *dp)
281 struct cam_periph *periph;
282 struct nda_softc *softc;
285 periph = (struct cam_periph *)dp->d_drv1;
286 if (cam_periph_acquire(periph) != 0) {
290 cam_periph_lock(periph);
291 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
292 cam_periph_unlock(periph);
293 cam_periph_release(periph);
297 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
300 softc = (struct nda_softc *)periph->softc;
301 softc->flags |= NDA_FLAG_OPEN;
303 cam_periph_unhold(periph);
304 cam_periph_unlock(periph);
309 ndaclose(struct disk *dp)
311 struct cam_periph *periph;
312 struct nda_softc *softc;
316 periph = (struct cam_periph *)dp->d_drv1;
317 softc = (struct nda_softc *)periph->softc;
318 cam_periph_lock(periph);
320 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
323 if ((softc->flags & NDA_FLAG_DIRTY) != 0 &&
324 (periph->flags & CAM_PERIPH_INVALID) == 0 &&
325 cam_periph_hold(periph, PRIBIO) == 0) {
327 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
328 nda_nvme_flush(softc, &ccb->nvmeio);
329 error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0,
330 /*sense_flags*/0, softc->disk->d_devstat);
333 xpt_print(periph->path, "Synchronize cache failed\n");
335 softc->flags &= ~NDA_FLAG_DIRTY;
336 xpt_release_ccb(ccb);
337 cam_periph_unhold(periph);
340 softc->flags &= ~NDA_FLAG_OPEN;
342 while (softc->refcount != 0)
343 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "ndaclose", 1);
344 KASSERT(softc->outstanding_cmds == 0,
345 ("nda %d outstanding commands", softc->outstanding_cmds));
346 cam_periph_unlock(periph);
347 cam_periph_release(periph);
352 ndaschedule(struct cam_periph *periph)
354 struct nda_softc *softc = (struct nda_softc *)periph->softc;
356 if (softc->state != NDA_STATE_NORMAL)
359 cam_iosched_schedule(softc->cam_iosched, periph);
363 * Actually translate the requested transfer into one the physical driver
364 * can understand. The transfer is described by a buf and will include
365 * only one physical transfer.
368 ndastrategy(struct bio *bp)
370 struct cam_periph *periph;
371 struct nda_softc *softc;
373 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
374 softc = (struct nda_softc *)periph->softc;
376 cam_periph_lock(periph);
378 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastrategy(%p)\n", bp));
381 * If the device has been made invalid, error out
383 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
384 cam_periph_unlock(periph);
385 biofinish(bp, NULL, ENXIO);
389 if (bp->bio_cmd == BIO_DELETE)
393 * Place it in the queue of disk activities for this disk
395 cam_iosched_queue_work(softc->cam_iosched, bp);
398 * Schedule ourselves for performing the work.
401 cam_periph_unlock(periph);
407 ndadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
409 struct cam_periph *periph;
410 struct nda_softc *softc;
412 struct ccb_nvmeio nvmeio;
420 softc = (struct nda_softc *)periph->softc;
421 secsize = softc->disk->d_sectorsize;
422 lba = offset / secsize;
423 count = length / secsize;
425 if ((periph->flags & CAM_PERIPH_INVALID) != 0)
428 /* xpt_get_ccb returns a zero'd allocation for the ccb, mimic that here */
429 memset(&nvmeio, 0, sizeof(nvmeio));
431 xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
432 nvmeio.ccb_state = NDA_CCB_DUMP;
433 nda_nvme_write(softc, &nvmeio, virtual, lba, length, count);
434 error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error,
435 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
437 printf("Aborting dump due to I/O error %d.\n", error);
443 xpt_setup_ccb(&nvmeio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
445 nvmeio.ccb_state = NDA_CCB_DUMP;
446 nda_nvme_flush(softc, &nvmeio);
447 error = cam_periph_runccb((union ccb *)&nvmeio, cam_periph_error,
448 0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
450 xpt_print(periph->path, "flush cmd failed\n");
460 * Install a global async callback. This callback will
461 * receive async callbacks like "new device found".
463 status = xpt_register_async(AC_FOUND_DEVICE, ndaasync, NULL, NULL);
465 if (status != CAM_REQ_CMP) {
466 printf("nda: Failed to attach master async callback "
467 "due to status 0x%x!\n", status);
468 } else if (nda_send_ordered) {
470 /* Register our event handlers */
471 if ((EVENTHANDLER_REGISTER(power_suspend, ndasuspend,
472 NULL, EVENTHANDLER_PRI_LAST)) == NULL)
473 printf("ndainit: power event registration failed!\n");
474 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ndashutdown,
475 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
476 printf("ndainit: shutdown event registration failed!\n");
481 * Callback from GEOM, called when it has finished cleaning up its
485 ndadiskgonecb(struct disk *dp)
487 struct cam_periph *periph;
489 periph = (struct cam_periph *)dp->d_drv1;
491 cam_periph_release(periph);
495 ndaoninvalidate(struct cam_periph *periph)
497 struct nda_softc *softc;
499 softc = (struct nda_softc *)periph->softc;
502 * De-register any async callbacks.
504 xpt_register_async(0, ndaasync, periph, periph->path);
506 softc->invalidations++;
510 * Return all queued I/O with ENXIO.
511 * XXX Handle any transactions queued to the card
512 * with XPT_ABORT_CCB.
514 cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
516 disk_gone(softc->disk);
520 ndacleanup(struct cam_periph *periph)
522 struct nda_softc *softc;
524 softc = (struct nda_softc *)periph->softc;
526 cam_periph_unlock(periph);
528 cam_iosched_fini(softc->cam_iosched);
531 * If we can't free the sysctl tree, oh well...
533 if ((softc->flags & NDA_FLAG_SCTX_INIT) != 0) {
535 if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
536 xpt_print(periph->path,
537 "can't remove sysctl stats context\n");
539 if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
540 xpt_print(periph->path,
541 "can't remove sysctl context\n");
544 disk_destroy(softc->disk);
545 free(softc, M_DEVBUF);
546 cam_periph_lock(periph);
550 ndaasync(void *callback_arg, u_int32_t code,
551 struct cam_path *path, void *arg)
553 struct cam_periph *periph;
555 periph = (struct cam_periph *)callback_arg;
557 case AC_FOUND_DEVICE:
559 struct ccb_getdev *cgd;
562 cgd = (struct ccb_getdev *)arg;
566 if (cgd->protocol != PROTO_NVME)
570 * Allocate a peripheral instance for
571 * this device and start the probe
574 status = cam_periph_alloc(ndaregister, ndaoninvalidate,
575 ndacleanup, ndastart,
576 "nda", CAM_PERIPH_BIO,
578 AC_FOUND_DEVICE, cgd);
580 if (status != CAM_REQ_CMP
581 && status != CAM_REQ_INPROG)
582 printf("ndaasync: Unable to attach to new device "
583 "due to status 0x%x\n", status);
586 case AC_ADVINFO_CHANGED:
590 buftype = (uintptr_t)arg;
591 if (buftype == CDAI_TYPE_PHYS_PATH) {
592 struct nda_softc *softc;
594 softc = periph->softc;
595 disk_attr_changed(softc->disk, "GEOM::physpath",
602 cam_periph_async(periph, code, path, arg);
608 ndasysctlinit(void *context, int pending)
610 struct cam_periph *periph;
611 struct nda_softc *softc;
612 char tmpstr[32], tmpstr2[16];
614 periph = (struct cam_periph *)context;
616 /* periph was held for us when this task was enqueued */
617 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
618 cam_periph_release(periph);
622 softc = (struct nda_softc *)periph->softc;
623 snprintf(tmpstr, sizeof(tmpstr), "CAM NDA unit %d", periph->unit_number);
624 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
626 sysctl_ctx_init(&softc->sysctl_ctx);
627 softc->flags |= NDA_FLAG_SCTX_INIT;
628 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
629 SYSCTL_STATIC_CHILDREN(_kern_cam_nda), OID_AUTO, tmpstr2,
630 CTLFLAG_RD, 0, tmpstr, "device_index");
631 if (softc->sysctl_tree == NULL) {
632 printf("ndasysctlinit: unable to allocate sysctl tree\n");
633 cam_periph_release(periph);
637 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
638 OID_AUTO, "unmapped_io", CTLFLAG_RD,
639 &softc->unmappedio, 0, "Unmapped I/O leaf");
641 SYSCTL_ADD_QUAD(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
642 OID_AUTO, "deletes", CTLFLAG_RD,
643 &softc->deletes, "Number of BIO_DELETE requests");
645 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
646 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
647 "trim_count", CTLFLAG_RD, &softc->trim_count,
648 "Total number of unmap/dsm commands sent");
649 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
650 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
651 "trim_ranges", CTLFLAG_RD, &softc->trim_ranges,
652 "Total number of ranges in unmap/dsm commands");
653 SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
654 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
655 "trim_lbas", CTLFLAG_RD, &softc->trim_lbas,
656 "Total lbas in the unmap/dsm commands sent");
658 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
659 OID_AUTO, "rotating", CTLFLAG_RD, &nda_rotating_media, 1,
663 softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
664 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
665 CTLFLAG_RD, 0, "Statistics");
666 if (softc->sysctl_stats_tree == NULL) {
667 printf("ndasysctlinit: unable to allocate sysctl tree for stats\n");
668 cam_periph_release(periph);
671 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
672 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
673 OID_AUTO, "timeouts", CTLFLAG_RD,
675 "Device timeouts reported by the SIM");
676 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
677 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
678 OID_AUTO, "errors", CTLFLAG_RD,
680 "Transport errors reported by the SIM.");
681 SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
682 SYSCTL_CHILDREN(softc->sysctl_stats_tree),
683 OID_AUTO, "pack_invalidations", CTLFLAG_RD,
684 &softc->invalidations, 0,
685 "Device pack invalidations.");
688 #ifdef CAM_TEST_FAILURE
689 SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
690 OID_AUTO, "invalidate", CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE,
691 periph, 0, cam_periph_invalidate_sysctl, "I",
692 "Write 1 to invalidate the drive immediately");
695 cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
698 cam_periph_release(periph);
702 ndagetattr(struct bio *bp)
705 struct cam_periph *periph;
707 if (g_handleattr_int(bp, "GEOM::canspeedup", nda_enable_biospeedup))
708 return (EJUSTRETURN);
710 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
711 cam_periph_lock(periph);
712 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
714 cam_periph_unlock(periph);
716 bp->bio_completed = bp->bio_length;
721 ndaregister(struct cam_periph *periph, void *arg)
723 struct nda_softc *softc;
725 struct ccb_pathinq cpi;
726 const struct nvme_namespace_data *nsd;
727 const struct nvme_controller_data *cd;
728 char announce_buf[80];
729 uint8_t flbas_fmt, lbads, vwc_present;
733 nsd = nvme_get_identify_ns(periph);
734 cd = nvme_get_identify_cntrl(periph);
736 softc = (struct nda_softc *)malloc(sizeof(*softc), M_DEVBUF,
740 printf("ndaregister: Unable to probe new device. "
741 "Unable to allocate softc\n");
742 return(CAM_REQ_CMP_ERR);
745 if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {
746 printf("ndaregister: Unable to probe new device. "
747 "Unable to allocate iosched memory\n");
748 free(softc, M_DEVBUF);
749 return(CAM_REQ_CMP_ERR);
752 /* ident_data parsing */
754 periph->softc = softc;
756 softc->quirks = NDA_Q_NONE;
758 xpt_path_inq(&cpi, periph->path);
760 TASK_INIT(&softc->sysctl_task, 0, ndasysctlinit, periph);
763 * The name space ID is the lun, save it for later I/O
765 softc->nsid = (uint32_t)xpt_path_lun_id(periph->path);
768 * Register this media as a disk
770 (void)cam_periph_hold(periph, PRIBIO);
771 cam_periph_unlock(periph);
772 snprintf(announce_buf, sizeof(announce_buf),
773 "kern.cam.nda.%d.quirks", periph->unit_number);
774 quirks = softc->quirks;
775 TUNABLE_INT_FETCH(announce_buf, &quirks);
776 softc->quirks = quirks;
777 cam_iosched_set_sort_queue(softc->cam_iosched, 0);
778 softc->disk = disk = disk_alloc();
779 disk->d_rotation_rate = DISK_RR_NON_ROTATING;
780 disk->d_open = ndaopen;
781 disk->d_close = ndaclose;
782 disk->d_strategy = ndastrategy;
783 disk->d_getattr = ndagetattr;
784 disk->d_dump = ndadump;
785 disk->d_gone = ndadiskgonecb;
786 disk->d_name = "nda";
787 disk->d_drv1 = periph;
788 disk->d_unit = periph->unit_number;
789 maxio = cpi.maxio; /* Honor max I/O size of SIM */
791 maxio = DFLTPHYS; /* traditional default */
792 else if (maxio > MAXPHYS)
793 maxio = MAXPHYS; /* for safety */
794 disk->d_maxsize = maxio;
795 flbas_fmt = (nsd->flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) &
796 NVME_NS_DATA_FLBAS_FORMAT_MASK;
797 lbads = (nsd->lbaf[flbas_fmt] >> NVME_NS_DATA_LBAF_LBADS_SHIFT) &
798 NVME_NS_DATA_LBAF_LBADS_MASK;
799 disk->d_sectorsize = 1 << lbads;
800 disk->d_mediasize = (off_t)(disk->d_sectorsize * nsd->nsze);
801 disk->d_delmaxsize = disk->d_mediasize;
802 disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
803 if (nvme_ctrlr_has_dataset_mgmt(cd))
804 disk->d_flags |= DISKFLAG_CANDELETE;
805 vwc_present = (cd->vwc >> NVME_CTRLR_DATA_VWC_PRESENT_SHIFT) &
806 NVME_CTRLR_DATA_VWC_PRESENT_MASK;
808 disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
809 if ((cpi.hba_misc & PIM_UNMAPPED) != 0) {
810 disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
811 softc->unmappedio = 1;
814 * d_ident and d_descr are both far bigger than the length of either
815 * the serial or model number strings.
817 cam_strvis(disk->d_descr, cd->mn,
818 sizeof(disk->d_descr), NVME_MODEL_NUMBER_LENGTH);
819 cam_strvis(disk->d_ident, cd->sn,
820 sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH);
821 disk->d_hba_vendor = cpi.hba_vendor;
822 disk->d_hba_device = cpi.hba_device;
823 disk->d_hba_subvendor = cpi.hba_subvendor;
824 disk->d_hba_subdevice = cpi.hba_subdevice;
825 snprintf(disk->d_attachment, sizeof(disk->d_attachment),
826 "%s%d", cpi.dev_name, cpi.unit_number);
827 disk->d_stripesize = disk->d_sectorsize;
828 disk->d_stripeoffset = 0;
829 disk->d_devstat = devstat_new_entry(periph->periph_name,
830 periph->unit_number, disk->d_sectorsize,
831 DEVSTAT_ALL_SUPPORTED,
832 DEVSTAT_TYPE_DIRECT | XPORT_DEVSTAT_TYPE(cpi.transport),
833 DEVSTAT_PRIORITY_DISK);
835 * Add alias for older nvd drives to ease transition.
837 /* disk_add_alias(disk, "nvd"); Have reports of this causing problems */
840 * Acquire a reference to the periph before we register with GEOM.
841 * We'll release this reference once GEOM calls us back (via
842 * ndadiskgonecb()) telling us that our provider has been freed.
844 if (cam_periph_acquire(periph) != 0) {
845 xpt_print(periph->path, "%s: lost periph during "
846 "registration!\n", __func__);
847 cam_periph_lock(periph);
848 return (CAM_REQ_CMP_ERR);
850 disk_create(softc->disk, DISK_VERSION);
851 cam_periph_lock(periph);
852 cam_periph_unhold(periph);
854 snprintf(announce_buf, sizeof(announce_buf),
855 "%juMB (%ju %u byte sectors)",
856 (uintmax_t)((uintmax_t)disk->d_mediasize / (1024*1024)),
857 (uintmax_t)disk->d_mediasize / disk->d_sectorsize,
859 xpt_announce_periph(periph, announce_buf);
860 xpt_announce_quirks(periph, softc->quirks, NDA_Q_BIT_STRING);
863 * Create our sysctl variables, now that we know
864 * we have successfully attached.
866 if (cam_periph_acquire(periph) == 0)
867 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
870 * Register for device going away and info about the drive
871 * changing (though with NVMe, it can't)
873 xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
874 ndaasync, periph, periph->path);
876 softc->state = NDA_STATE_NORMAL;
881 ndastart(struct cam_periph *periph, union ccb *start_ccb)
883 struct nda_softc *softc = (struct nda_softc *)periph->softc;
884 struct ccb_nvmeio *nvmeio = &start_ccb->nvmeio;
886 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart\n"));
888 switch (softc->state) {
889 case NDA_STATE_NORMAL:
893 bp = cam_iosched_next_bio(softc->cam_iosched);
894 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastart: bio %p\n", bp));
896 xpt_release_ccb(start_ccb);
900 switch (bp->bio_cmd) {
902 softc->flags |= NDA_FLAG_DIRTY;
906 #ifdef CAM_TEST_FAILURE
910 * Support the failure ioctls. If the command is a
911 * read, and there are pending forced read errors, or
912 * if a write and pending write errors, then fail this
913 * operation with EIO. This is useful for testing
914 * purposes. Also, support having every Nth read fail.
916 * This is a rather blunt tool.
918 if (bp->bio_cmd == BIO_READ) {
919 if (softc->force_read_error) {
920 softc->force_read_error--;
923 if (softc->periodic_read_error > 0) {
924 if (++softc->periodic_read_count >=
925 softc->periodic_read_error) {
926 softc->periodic_read_count = 0;
931 if (softc->force_write_error) {
932 softc->force_write_error--;
937 biofinish(bp, NULL, EIO);
938 xpt_release_ccb(start_ccb);
943 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
944 round_page(bp->bio_bcount + bp->bio_ma_offset) /
945 PAGE_SIZE == bp->bio_ma_n,
946 ("Short bio %p", bp));
947 nda_nvme_rw_bio(softc, &start_ccb->nvmeio, bp, bp->bio_cmd == BIO_READ ?
948 NVME_OPC_READ : NVME_OPC_WRITE);
953 struct nvme_dsm_range *dsm_range, *dsm_end;
954 struct nda_trim_request *trim;
957 uint32_t totalcount = 0, ranges = 0;
959 trim = malloc(sizeof(*trim), M_NVMEDA, M_ZERO | M_NOWAIT);
961 biofinish(bp, NULL, ENOMEM);
962 xpt_release_ccb(start_ccb);
966 TAILQ_INIT(&trim->bps);
968 ents = min(nitems(trim->dsm), nda_max_trim_entries);
969 dsm_range = trim->dsm;
970 dsm_end = dsm_range + ents;
972 TAILQ_INSERT_TAIL(&trim->bps, bp1, bio_queue);
974 htole32(bp1->bio_bcount / softc->disk->d_sectorsize);
975 dsm_range->starting_lba =
976 htole64(bp1->bio_offset / softc->disk->d_sectorsize);
978 totalcount += dsm_range->length;
980 if (dsm_range >= dsm_end)
982 bp1 = cam_iosched_next_trim(softc->cam_iosched);
983 /* XXX -- Could collapse adjacent ranges, but we don't for now */
984 /* XXX -- Could limit based on total payload size */
985 } while (bp1 != NULL);
986 start_ccb->ccb_trim = trim;
987 nda_nvme_trim(softc, &start_ccb->nvmeio, trim->dsm,
988 dsm_range - trim->dsm);
989 start_ccb->ccb_state = NDA_CCB_TRIM;
991 softc->trim_ranges += ranges;
992 softc->trim_lbas += totalcount;
994 * Note: We can have multiple TRIMs in flight, so we don't call
995 * cam_iosched_submit_trim(softc->cam_iosched);
996 * since that forces the I/O scheduler to only schedule one at a time.
997 * On NVMe drives, this is a performance disaster.
1002 nda_nvme_flush(softc, nvmeio);
1005 biofinish(bp, NULL, EOPNOTSUPP);
1006 xpt_release_ccb(start_ccb);
1007 ndaschedule(periph);
1010 start_ccb->ccb_state = NDA_CCB_BUFFER_IO;
1011 start_ccb->ccb_bp = bp;
1013 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
1014 softc->outstanding_cmds++;
1015 softc->refcount++; /* For submission only */
1016 cam_periph_unlock(periph);
1017 xpt_action(start_ccb);
1018 cam_periph_lock(periph);
1019 softc->refcount--; /* Submission done */
1021 /* May have more work to do, so ensure we stay scheduled */
1022 ndaschedule(periph);
1029 ndadone(struct cam_periph *periph, union ccb *done_ccb)
1031 struct nda_softc *softc;
1032 struct ccb_nvmeio *nvmeio = &done_ccb->nvmeio;
1033 struct cam_path *path;
1036 softc = (struct nda_softc *)periph->softc;
1037 path = done_ccb->ccb_h.path;
1039 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("ndadone\n"));
1041 state = nvmeio->ccb_state & NDA_CCB_TYPE_MASK;
1043 case NDA_CCB_BUFFER_IO:
1048 cam_periph_lock(periph);
1049 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1050 error = ndaerror(done_ccb, 0, 0);
1051 if (error == ERESTART) {
1052 /* A retry was scheduled, so just return. */
1053 cam_periph_unlock(periph);
1056 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1057 cam_release_devq(path,
1061 /*getcount_only*/0);
1063 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1064 panic("REQ_CMP with QFRZN");
1067 if (state == NDA_CCB_BUFFER_IO) {
1070 bp = (struct bio *)done_ccb->ccb_bp;
1071 bp->bio_error = error;
1073 bp->bio_resid = bp->bio_bcount;
1074 bp->bio_flags |= BIO_ERROR;
1078 softc->outstanding_cmds--;
1081 * We need to call cam_iosched before we call biodone so that we
1082 * don't measure any activity that happens in the completion
1083 * routine, which in the case of sendfile can be quite
1086 cam_iosched_bio_complete(softc->cam_iosched, bp, done_ccb);
1087 xpt_release_ccb(done_ccb);
1088 ndaschedule(periph);
1089 cam_periph_unlock(periph);
1091 } else { /* state == NDA_CCB_TRIM */
1092 struct nda_trim_request *trim;
1093 struct bio *bp1, *bp2;
1094 TAILQ_HEAD(, bio) queue;
1096 trim = nvmeio->ccb_trim;
1098 TAILQ_CONCAT(&queue, &trim->bps, bio_queue);
1099 free(trim, M_NVMEDA);
1102 * Since we can have multiple trims in flight, we don't
1103 * need to call this here.
1104 * cam_iosched_trim_done(softc->cam_iosched);
1107 * The the I/O scheduler that we're finishing the I/O
1108 * so we can keep book. The first one we pass in the CCB
1109 * which has the timing information. The rest we pass in NULL
1110 * so we can keep proper counts.
1112 bp1 = TAILQ_FIRST(&queue);
1113 cam_iosched_bio_complete(softc->cam_iosched, bp1, done_ccb);
1114 xpt_release_ccb(done_ccb);
1115 softc->outstanding_cmds--;
1116 ndaschedule(periph);
1117 cam_periph_unlock(periph);
1118 while ((bp2 = TAILQ_FIRST(&queue)) != NULL) {
1119 TAILQ_REMOVE(&queue, bp2, bio_queue);
1120 bp2->bio_error = error;
1122 bp2->bio_flags |= BIO_ERROR;
1123 bp2->bio_resid = bp1->bio_bcount;
1127 cam_iosched_bio_complete(softc->cam_iosched, bp2, NULL);
1134 /* No-op. We're polling */
1139 xpt_release_ccb(done_ccb);
1143 ndaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1145 struct nda_softc *softc;
1146 struct cam_periph *periph;
1148 periph = xpt_path_periph(ccb->ccb_h.path);
1149 softc = (struct nda_softc *)periph->softc;
1151 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
1152 case CAM_CMD_TIMEOUT:
1157 case CAM_REQ_ABORTED:
1158 case CAM_REQ_CMP_ERR:
1159 case CAM_REQ_TERMIO:
1160 case CAM_UNREC_HBA_ERROR:
1161 case CAM_DATA_RUN_ERR:
1162 case CAM_ATA_STATUS_ERROR:
1171 return(cam_periph_error(ccb, cam_flags, sense_flags));
1175 * Step through all NDA peripheral drivers, and if the device is still open,
1176 * sync the disk cache to physical media.
1181 struct cam_periph *periph;
1182 struct nda_softc *softc;
1186 CAM_PERIPH_FOREACH(periph, &ndadriver) {
1187 softc = (struct nda_softc *)periph->softc;
1189 if (SCHEDULER_STOPPED()) {
1191 * If we paniced with the lock held or the periph is not
1192 * open, do not recurse. Otherwise, call ndadump since
1193 * that avoids the sleeping cam_periph_getccb does if no
1194 * CCBs are available.
1196 if (!cam_periph_owned(periph) &&
1197 (softc->flags & NDA_FLAG_OPEN)) {
1198 ndadump(softc->disk, NULL, 0, 0, 0);
1204 * We only sync the cache if the drive is still open
1206 cam_periph_lock(periph);
1207 if ((softc->flags & NDA_FLAG_OPEN) == 0) {
1208 cam_periph_unlock(periph);
1212 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1213 nda_nvme_flush(softc, &ccb->nvmeio);
1214 error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0,
1215 /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY,
1216 softc->disk->d_devstat);
1218 xpt_print(periph->path, "Synchronize cache failed\n");
1219 xpt_release_ccb(ccb);
1220 cam_periph_unlock(periph);
1225 ndashutdown(void *arg, int howto)
1232 ndasuspend(void *arg)