Author: imp
Date: Thu Jun  9 22:39:02 2016
New Revision: 301771
URL: https://svnweb.freebsd.org/changeset/base/301771

Log:
  New NVMe front end (nda).

Added:
  head/sys/cam/nvme/
  head/sys/cam/nvme/nvme_all.c   (contents, props changed)
  head/sys/cam/nvme/nvme_all.h   (contents, props changed)
  head/sys/cam/nvme/nvme_da.c   (contents, props changed)
  head/sys/cam/nvme/nvme_xpt.c   (contents, props changed)
Modified:
  head/sys/cam/cam_ccb.h
  head/sys/cam/cam_xpt_internal.h
  head/sys/conf/files
  head/sys/conf/files.amd64

Modified: head/sys/cam/cam_ccb.h
==============================================================================
--- head/sys/cam/cam_ccb.h      Thu Jun  9 22:25:00 2016        (r301770)
+++ head/sys/cam/cam_ccb.h      Thu Jun  9 22:39:02 2016        (r301771)
@@ -41,6 +41,7 @@
 #include <cam/cam_debug.h>
 #include <cam/scsi/scsi_all.h>
 #include <cam/ata/ata_all.h>
+#include <cam/nvme/nvme_all.h>
 
 /* General allocation length definitions for CCB structures */
 #define        IOCDBLEN        CAM_MAX_CDBLEN  /* Space for CDB bytes/pointer 
*/
@@ -265,6 +266,7 @@ typedef enum {
        PROTO_ATAPI,    /* AT Attachment Packetized Interface */
        PROTO_SATAPM,   /* SATA Port Multiplier */
        PROTO_SEMB,     /* SATA Enclosure Management Bridge */
+       PROTO_NVME,     /* NVME */
 } cam_proto;
 
 typedef enum {
@@ -280,6 +282,7 @@ typedef enum {
        XPORT_SATA,     /* Serial AT Attachment */
        XPORT_ISCSI,    /* iSCSI */
        XPORT_SRP,      /* SCSI RDMA Protocol */
+       XPORT_NVME,     /* NVMe over PCIe */
 } cam_xport;
 
 #define XPORT_IS_ATA(t)                ((t) == XPORT_ATA || (t) == XPORT_SATA)
@@ -783,6 +786,19 @@ struct ccb_relsim {
 };
 
 /*
+ * NVMe I/O Request CCB used for the XPT_NVME_IO function code.
+ */
+struct ccb_nvmeio {
+       struct     ccb_hdr ccb_h;
+       union      ccb *next_ccb;       /* Ptr for next CCB for action */
+       struct nvme_command cmd;        /* NVME command, per NVME standard */
+       struct nvme_completion cpl;     /* NVME completion, per NVME standard */
+       uint8_t   *data_ptr;            /* Ptr to the data buf/SG list */
+       uint32_t  dxfer_len;            /* Data transfer length */
+       uint32_t  resid;                /* Transfer residual length: 2's comp 
unused ?*/
+};
+
+/*
  * Definitions for the asynchronous callback CCB fields.
  */
 typedef enum {
@@ -1234,6 +1250,7 @@ union ccb {
        struct  ccb_ataio               ataio;
        struct  ccb_dev_advinfo         cdai;
        struct  ccb_async               casync;
+       struct  ccb_nvmeio              nvmeio;
 };
 
 #define CCB_CLEAR_ALL_EXCEPT_HDR(ccbp)                 \
@@ -1250,6 +1267,12 @@ cam_fill_csio(struct ccb_scsiio *csio, u
              u_int32_t timeout);
 
 static __inline void
+cam_fill_nvmeio(struct ccb_nvmeio *nvmeio, u_int32_t retries,
+             void (*cbfcnp)(struct cam_periph *, union ccb *),
+             u_int32_t flags, u_int8_t *data_ptr, u_int32_t dxfer_len,
+             u_int32_t timeout);
+
+static __inline void
 cam_fill_ctio(struct ccb_scsiio *csio, u_int32_t retries,
              void (*cbfcnp)(struct cam_periph *, union ccb *),
              u_int32_t flags, u_int tag_action, u_int tag_id,
@@ -1370,6 +1393,20 @@ cam_ccb_status(union ccb *ccb)
 
 void cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended);
 
+static __inline void
+cam_fill_nvmeio(struct ccb_nvmeio *nvmeio, u_int32_t retries,
+             void (*cbfcnp)(struct cam_periph *, union ccb *),
+             u_int32_t flags, u_int8_t *data_ptr, u_int32_t dxfer_len,
+             u_int32_t timeout)
+{
+       nvmeio->ccb_h.func_code = XPT_NVME_IO;
+       nvmeio->ccb_h.flags = flags;
+       nvmeio->ccb_h.retry_count = retries;
+       nvmeio->ccb_h.cbfcnp = cbfcnp;
+       nvmeio->ccb_h.timeout = timeout;
+       nvmeio->data_ptr = data_ptr;
+       nvmeio->dxfer_len = dxfer_len;
+}
 __END_DECLS
 
 #endif /* _CAM_CAM_CCB_H */

Modified: head/sys/cam/cam_xpt_internal.h
==============================================================================
--- head/sys/cam/cam_xpt_internal.h     Thu Jun  9 22:25:00 2016        
(r301770)
+++ head/sys/cam/cam_xpt_internal.h     Thu Jun  9 22:39:02 2016        
(r301771)
@@ -117,6 +117,8 @@ struct cam_ed {
        STAILQ_ENTRY(cam_ed) highpowerq_entry;
        struct mtx       device_mtx;
        struct task      device_destroy_task;
+       const struct     nvme_controller_data *nvme_cdata;
+       const struct     nvme_namespace_data *nvme_data;
 };
 
 /*
@@ -167,6 +169,7 @@ struct cam_path {
 
 struct xpt_xport *     scsi_get_xport(void);
 struct xpt_xport *     ata_get_xport(void);
+struct xpt_xport *     nvme_get_xport(void);
 
 struct cam_ed *                xpt_alloc_device(struct cam_eb *bus,
                                         struct cam_et *target,

Added: head/sys/cam/nvme/nvme_all.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/cam/nvme/nvme_all.c        Thu Jun  9 22:39:02 2016        
(r301771)
@@ -0,0 +1,124 @@
+/*-
+ * Copyright (c) 2015 Netflix, Inc
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification, immediately at the beginning of the file.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+
+#ifdef _KERNEL
+#include "opt_scsi.h"
+
+#include <sys/systm.h>
+#include <sys/libkern.h>
+#include <sys/kernel.h>
+#include <sys/sysctl.h>
+#else
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#ifndef min
+#define min(a,b) (((a)<(b))?(a):(b))
+#endif
+#endif
+
+#include <cam/cam.h>
+#include <cam/cam_ccb.h>
+#include <cam/cam_queue.h>
+#include <cam/cam_xpt.h>
+#include <cam/nvme/nvme_all.h>
+#include <sys/sbuf.h>
+#include <sys/endian.h>
+
+void
+nvme_ns_cmd(struct ccb_nvmeio *nvmeio, uint8_t cmd, uint32_t nsid,
+    uint32_t cdw10, uint32_t cdw11, uint32_t cdw12, uint32_t cdw13,
+    uint32_t cdw14, uint32_t cdw15)
+{
+       bzero(&nvmeio->cmd, sizeof(struct nvme_command));
+       nvmeio->cmd.opc = cmd;
+       nvmeio->cmd.nsid = nsid;
+       nvmeio->cmd.cdw10 = cdw10;
+       nvmeio->cmd.cdw11 = cdw11;
+       nvmeio->cmd.cdw12 = cdw12;
+       nvmeio->cmd.cdw13 = cdw13;
+       nvmeio->cmd.cdw14 = cdw14;
+       nvmeio->cmd.cdw15 = cdw15;
+}
+
+int
+nvme_identify_match(caddr_t identbuffer, caddr_t table_entry)
+{
+       return 0;
+}
+
+
+void
+nvme_print_ident(const struct nvme_controller_data *cdata,
+    const struct nvme_namespace_data *data)
+{
+       printf("I'm a pretty NVME drive\n");
+}
+
+/* XXX need to do nvme admin opcodes too, but those aren't used yet by nda */
+static const char *
+nvme_opc2str[] = {
+       "FLUSH",
+       "WRITE",
+       "READ",
+       "RSVD-3",
+       "WRITE_UNCORRECTABLE",
+       "COMPARE",
+       "RSVD-6",
+       "RSVD-7",
+       "DATASET_MANAGEMENT"
+};
+
+const char *
+nvme_op_string(const struct nvme_command *cmd)
+{
+       if (cmd->opc > nitems(nvme_opc2str))
+               return "UNKNOWN";
+
+       return nvme_opc2str[cmd->opc];
+}
+
+const char *
+nvme_cmd_string(const struct nvme_command *cmd, char *cmd_string, size_t len)
+{
+       /*
+        * cid, rsvd areas and mptr not printed, since they are used
+        * only internally by the SIM.
+        */
+       snprintf(cmd_string, len,
+           "opc=%x fuse=%x nsid=%x prp1=%llx prp2=%llx cdw=%x %x %x %x %x %x",
+           cmd->opc, cmd->fuse, cmd->nsid,
+           (unsigned long long)cmd->prp1, (unsigned long long)cmd->prp2,
+           cmd->cdw10, cmd->cdw11, cmd->cdw12, cmd->cdw13, cmd->cdw14, 
cmd->cdw15);
+
+       return cmd_string;
+}

Added: head/sys/cam/nvme/nvme_all.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/cam/nvme/nvme_all.h        Thu Jun  9 22:39:02 2016        
(r301771)
@@ -0,0 +1,48 @@
+/*-
+ * Copyright (c) 2015 Netflix, Inc
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification, immediately at the beginning of the file.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef CAM_NVME_NVME_ALL_H
+#define CAM_NVME_NVME_ALL_H 1
+
+#include <dev/nvme/nvme.h>
+
+struct ccb_nvmeio;
+
+#define NVME_REV_1     1       /* Supports NVMe 1.2 or earlier */
+
+void   nvme_ns_cmd(struct ccb_nvmeio *nvmeio, uint8_t cmd, uint32_t nsid,
+    uint32_t cdw10, uint32_t cdw11, uint32_t cdw12, uint32_t cdw13,
+    uint32_t cdw14, uint32_t cdw15);
+
+int    nvme_identify_match(caddr_t identbuffer, caddr_t table_entry);
+
+void   nvme_print_ident(const struct nvme_controller_data *, const struct 
nvme_namespace_data *);
+const char *nvme_op_string(const struct nvme_command *);
+const char *nvme_cmd_string(const struct nvme_command *, char *, size_t);
+
+#endif /* CAM_NVME_NVME_ALL_H */

Added: head/sys/cam/nvme/nvme_da.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/cam/nvme/nvme_da.c Thu Jun  9 22:39:02 2016        (r301771)
@@ -0,0 +1,1152 @@
+/*-
+ * Copyright (c) 2015 Netflix, Inc
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification, immediately at the beginning of the file.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Derived from ata_da.c:
+ * Copyright (c) 2009 Alexander Motin <m...@freebsd.org>
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+
+#ifdef _KERNEL
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/bio.h>
+#include <sys/sysctl.h>
+#include <sys/taskqueue.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/conf.h>
+#include <sys/devicestat.h>
+#include <sys/eventhandler.h>
+#include <sys/malloc.h>
+#include <sys/cons.h>
+#include <sys/proc.h>
+#include <sys/reboot.h>
+#include <geom/geom_disk.h>
+#endif /* _KERNEL */
+
+#ifndef _KERNEL
+#include <stdio.h>
+#include <string.h>
+#endif /* _KERNEL */
+
+#include <cam/cam.h>
+#include <cam/cam_ccb.h>
+#include <cam/cam_periph.h>
+#include <cam/cam_xpt_periph.h>
+#include <cam/cam_sim.h>
+#include <cam/cam_iosched.h>
+
+#include <cam/nvme/nvme_all.h>
+
+typedef enum {
+       NDA_STATE_NORMAL
+} nda_state;
+
+typedef enum {
+       NDA_FLAG_OPEN           = 0x0001,
+       NDA_FLAG_DIRTY          = 0x0002,
+       NDA_FLAG_SCTX_INIT      = 0x0004,
+} nda_flags;
+
+typedef enum {
+       NDA_Q_4K   = 0x01,
+       NDA_Q_NONE = 0x00,
+} nda_quirks;
+       
+#define NDA_Q_BIT_STRING       \
+       "\020"                  \
+       "\001Bit 0"
+
+typedef enum {
+       NDA_CCB_BUFFER_IO       = 0x01,
+       NDA_CCB_DUMP            = 0x02,
+       NDA_CCB_TRIM            = 0x03,
+       NDA_CCB_TYPE_MASK       = 0x0F,
+} nda_ccb_state;
+
+/* Offsets into our private area for storing information */
+#define ccb_state      ppriv_field0
+#define ccb_bp         ppriv_ptr1
+
+struct trim_request {
+       TAILQ_HEAD(, bio) bps;
+};
+struct nda_softc {
+       struct   cam_iosched_softc *cam_iosched;
+       int      outstanding_cmds;      /* Number of active commands */
+       int      refcount;              /* Active xpt_action() calls */
+       nda_state state;
+       nda_flags flags;
+       nda_quirks quirks;
+       int      unmappedio;
+       uint32_t  nsid;                 /* Namespace ID for this nda device */
+       struct disk *disk;
+       struct task             sysctl_task;
+       struct sysctl_ctx_list  sysctl_ctx;
+       struct sysctl_oid       *sysctl_tree;
+       struct trim_request     trim_req;
+#ifdef CAM_IO_STATS
+       struct sysctl_ctx_list  sysctl_stats_ctx;
+       struct sysctl_oid       *sysctl_stats_tree;
+       u_int   timeouts;
+       u_int   errors;
+       u_int   invalidations;
+#endif
+};
+
+/* Need quirk table */
+
+static disk_strategy_t ndastrategy;
+static dumper_t        ndadump;
+static periph_init_t   ndainit;
+static void            ndaasync(void *callback_arg, u_int32_t code,
+                               struct cam_path *path, void *arg);
+static void            ndasysctlinit(void *context, int pending);
+static periph_ctor_t   ndaregister;
+static periph_dtor_t   ndacleanup;
+static periph_start_t  ndastart;
+static periph_oninv_t  ndaoninvalidate;
+static void            ndadone(struct cam_periph *periph,
+                              union ccb *done_ccb);
+static  int            ndaerror(union ccb *ccb, u_int32_t cam_flags,
+                               u_int32_t sense_flags);
+static void            ndashutdown(void *arg, int howto);
+static void            ndasuspend(void *arg);
+
+#ifndef        NDA_DEFAULT_SEND_ORDERED
+#define        NDA_DEFAULT_SEND_ORDERED        1
+#endif
+#ifndef NDA_DEFAULT_TIMEOUT
+#define NDA_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
+#endif
+#ifndef        NDA_DEFAULT_RETRY
+#define        NDA_DEFAULT_RETRY       4
+#endif
+
+
+//static int nda_retry_count = NDA_DEFAULT_RETRY;
+static int nda_send_ordered = NDA_DEFAULT_SEND_ORDERED;
+static int nda_default_timeout = NDA_DEFAULT_TIMEOUT;
+
+/*
+ * All NVMe media is non-rotational, so all nvme device instances
+ * share this to implement the sysctl.
+ */
+static int nda_rotating_media = 0;
+
+static SYSCTL_NODE(_kern_cam, OID_AUTO, nda, CTLFLAG_RD, 0,
+            "CAM Direct Access Disk driver");
+
+static struct periph_driver ndadriver =
+{
+       ndainit, "nda",
+       TAILQ_HEAD_INITIALIZER(ndadriver.units), /* generation */ 0
+};
+
+PERIPHDRIVER_DECLARE(nda, ndadriver);
+
+static MALLOC_DEFINE(M_NVMEDA, "nvme_da", "nvme_da buffers");
+
+/*
+ * nice wrappers. Maybe these belong in nvme_all.c instead of
+ * here, but this is the only place that uses these. Should
+ * we ever grow another NVME periph, we should move them
+ * all there wholesale.
+ */
+
+static void
+nda_nvme_flush(struct nda_softc *softc, struct ccb_nvmeio *nvmeio)
+{
+       cam_fill_nvmeio(nvmeio,
+           0,                  /* retries */
+           ndadone,            /* cbfcnp */
+           CAM_DIR_NONE,       /* flags */
+           NULL,               /* data_ptr */
+           0,                  /* dxfer_len */
+           nda_default_timeout * 1000); /* timeout 5s */
+       nvme_ns_flush_cmd(&nvmeio->cmd, softc->nsid);
+}
+
+static void
+nda_nvme_trim(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
+    void *payload, uint32_t num_ranges)
+{
+       cam_fill_nvmeio(nvmeio,
+           0,                  /* retries */
+           ndadone,            /* cbfcnp */
+           CAM_DIR_OUT,        /* flags */
+           payload,            /* data_ptr */
+           num_ranges * sizeof(struct nvme_dsm_range), /* dxfer_len */
+           nda_default_timeout * 1000); /* timeout 5s */
+       nvme_ns_trim_cmd(&nvmeio->cmd, softc->nsid, num_ranges);
+}
+
+static void
+nda_nvme_write(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
+    void *payload, uint64_t lba, uint32_t len, uint32_t count)
+{
+       cam_fill_nvmeio(nvmeio,
+           0,                  /* retries */
+           ndadone,            /* cbfcnp */
+           CAM_DIR_OUT,        /* flags */
+           payload,            /* data_ptr */
+           len,                /* dxfer_len */
+           nda_default_timeout * 1000); /* timeout 5s */
+       nvme_ns_write_cmd(&nvmeio->cmd, softc->nsid, lba, count);
+}
+
+static void
+nda_nvme_rw_bio(struct nda_softc *softc, struct ccb_nvmeio *nvmeio,
+    struct bio *bp, uint32_t rwcmd)
+{
+       int flags = rwcmd == NVME_OPC_READ ? CAM_DIR_IN : CAM_DIR_OUT;
+       void *payload;
+       uint64_t lba;
+       uint32_t count;
+
+       if (bp->bio_flags & BIO_UNMAPPED) {
+               flags |= CAM_DATA_BIO;
+               payload = bp;
+       } else {
+               payload = bp->bio_data;
+       }
+
+       lba = bp->bio_pblkno;
+       count = bp->bio_bcount / softc->disk->d_sectorsize;
+
+       cam_fill_nvmeio(nvmeio,
+           0,                  /* retries */
+           ndadone,            /* cbfcnp */
+           flags,              /* flags */
+           payload,            /* data_ptr */
+           bp->bio_bcount,     /* dxfer_len */
+           nda_default_timeout * 1000);                /* timeout 5s */
+       nvme_ns_rw_cmd(&nvmeio->cmd, rwcmd, softc->nsid, lba, count);
+}
+
+static int
+ndaopen(struct disk *dp)
+{
+       struct cam_periph *periph;
+       struct nda_softc *softc;
+       int error;
+
+       periph = (struct cam_periph *)dp->d_drv1;
+       if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
+               return(ENXIO);
+       }
+
+       cam_periph_lock(periph);
+       if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
+               cam_periph_unlock(periph);
+               cam_periph_release(periph);
+               return (error);
+       }
+
+       CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
+           ("ndaopen\n"));
+
+       softc = (struct nda_softc *)periph->softc;
+       softc->flags |= NDA_FLAG_OPEN;
+
+       cam_periph_unhold(periph);
+       cam_periph_unlock(periph);
+       return (0);
+}
+
+static int
+ndaclose(struct disk *dp)
+{
+       struct  cam_periph *periph;
+       struct  nda_softc *softc;
+       union ccb *ccb;
+       int error;
+
+       periph = (struct cam_periph *)dp->d_drv1;
+       softc = (struct nda_softc *)periph->softc;
+       cam_periph_lock(periph);
+
+       CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
+           ("ndaclose\n"));
+
+       if ((softc->flags & NDA_FLAG_DIRTY) != 0 &&
+           (periph->flags & CAM_PERIPH_INVALID) == 0 &&
+           cam_periph_hold(periph, PRIBIO) == 0) {
+
+               ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
+               nda_nvme_flush(softc, &ccb->nvmeio);
+               error = cam_periph_runccb(ccb, ndaerror, /*cam_flags*/0,
+                   /*sense_flags*/0, softc->disk->d_devstat);
+
+               if (error != 0)
+                       xpt_print(periph->path, "Synchronize cache failed\n");
+               else
+                       softc->flags &= ~NDA_FLAG_DIRTY;
+               xpt_release_ccb(ccb);
+               cam_periph_unhold(periph);
+       }
+
+       softc->flags &= ~NDA_FLAG_OPEN;
+
+       while (softc->refcount != 0)
+               cam_periph_sleep(periph, &softc->refcount, PRIBIO, "ndaclose", 
1);
+       cam_periph_unlock(periph);
+       cam_periph_release(periph);
+       return (0);     
+}
+
+static void
+ndaschedule(struct cam_periph *periph)
+{
+       struct nda_softc *softc = (struct nda_softc *)periph->softc;
+
+       if (softc->state != NDA_STATE_NORMAL)
+               return;
+
+       cam_iosched_schedule(softc->cam_iosched, periph);
+}
+
+/*
+ * Actually translate the requested transfer into one the physical driver
+ * can understand.  The transfer is described by a buf and will include
+ * only one physical transfer.
+ */
+static void
+ndastrategy(struct bio *bp)
+{
+       struct cam_periph *periph;
+       struct nda_softc *softc;
+       
+       periph = (struct cam_periph *)bp->bio_disk->d_drv1;
+       softc = (struct nda_softc *)periph->softc;
+
+       cam_periph_lock(periph);
+
+       CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ndastrategy(%p)\n", bp));
+
+       /*
+        * If the device has been made invalid, error out
+        */
+       if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
+               cam_periph_unlock(periph);
+               biofinish(bp, NULL, ENXIO);
+               return;
+       }
+       
+       /*
+        * Place it in the queue of disk activities for this disk
+        */
+       cam_iosched_queue_work(softc->cam_iosched, bp);
+
+       /*
+        * Schedule ourselves for performing the work.
+        */
+       ndaschedule(periph);
+       cam_periph_unlock(periph);
+
+       return;
+}
+
+static int
+ndadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t 
length)
+{
+       struct      cam_periph *periph;
+       struct      nda_softc *softc;
+       u_int       secsize;
+       union       ccb ccb;
+       struct      disk *dp;
+       uint64_t    lba;
+       uint32_t    count;
+       int         error = 0;
+
+       dp = arg;
+       periph = dp->d_drv1;
+       softc = (struct nda_softc *)periph->softc;
+       cam_periph_lock(periph);
+       secsize = softc->disk->d_sectorsize;
+       lba = offset / secsize;
+       count = length / secsize;
+       
+       if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
+               cam_periph_unlock(periph);
+               return (ENXIO);
+       }
+
+       if (length > 0) {
+               xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
+               ccb.ccb_h.ccb_state = NDA_CCB_DUMP;
+               nda_nvme_write(softc, &ccb.nvmeio, virtual, lba, length, count);
+               xpt_polled_action(&ccb);
+
+               error = cam_periph_error(&ccb,
+                   0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
+               if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
+                       cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
+                           /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
+               if (error != 0)
+                       printf("Aborting dump due to I/O error.\n");
+
+               cam_periph_unlock(periph);
+               return (error);
+       }
+       
+       /* Flush */
+       xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
+
+       ccb.ccb_h.ccb_state = NDA_CCB_DUMP;
+       nda_nvme_flush(softc, &ccb.nvmeio);
+       xpt_polled_action(&ccb);
+
+       error = cam_periph_error(&ccb,
+           0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
+       if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
+               cam_release_devq(ccb.ccb_h.path, /*relsim_flags*/0,
+                   /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
+       if (error != 0)
+               xpt_print(periph->path, "flush cmd failed\n");
+       cam_periph_unlock(periph);
+       return (error);
+}
+
+static void
+ndainit(void)
+{
+       cam_status status;
+
+       /*
+        * Install a global async callback.  This callback will
+        * receive async callbacks like "new device found".
+        */
+       status = xpt_register_async(AC_FOUND_DEVICE, ndaasync, NULL, NULL);
+
+       if (status != CAM_REQ_CMP) {
+               printf("nda: Failed to attach master async callback "
+                      "due to status 0x%x!\n", status);
+       } else if (nda_send_ordered) {
+
+               /* Register our event handlers */
+               if ((EVENTHANDLER_REGISTER(power_suspend, ndasuspend,
+                                          NULL, EVENTHANDLER_PRI_LAST)) == 
NULL)
+                   printf("ndainit: power event registration failed!\n");
+               if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ndashutdown,
+                                          NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
+                   printf("ndainit: shutdown event registration failed!\n");
+       }
+}
+
+/*
+ * Callback from GEOM, called when it has finished cleaning up its
+ * resources.
+ */
+static void
+ndadiskgonecb(struct disk *dp)
+{
+       struct cam_periph *periph;
+
+       periph = (struct cam_periph *)dp->d_drv1;
+
+       cam_periph_release(periph);
+}
+
+static void
+ndaoninvalidate(struct cam_periph *periph)
+{
+       struct nda_softc *softc;
+
+       softc = (struct nda_softc *)periph->softc;
+
+       /*
+        * De-register any async callbacks.
+        */
+       xpt_register_async(0, ndaasync, periph, periph->path);
+#ifdef CAM_IO_STATS
+       softc->invalidations++;
+#endif
+
+       /*
+        * Return all queued I/O with ENXIO.
+        * XXX Handle any transactions queued to the card
+        *     with XPT_ABORT_CCB.
+        */
+       cam_iosched_flush(softc->cam_iosched, NULL, ENXIO);
+
+       disk_gone(softc->disk);
+}
+
+static void
+ndacleanup(struct cam_periph *periph)
+{
+       struct nda_softc *softc;
+
+       softc = (struct nda_softc *)periph->softc;
+
+       cam_periph_unlock(periph);
+
+       cam_iosched_fini(softc->cam_iosched);
+
+       /*
+        * If we can't free the sysctl tree, oh well...
+        */
+       if ((softc->flags & NDA_FLAG_SCTX_INIT) != 0) {
+#ifdef CAM_IO_STATS
+               if (sysctl_ctx_free(&softc->sysctl_stats_ctx) != 0)
+                       xpt_print(periph->path,
+                           "can't remove sysctl stats context\n");
+#endif
+               if (sysctl_ctx_free(&softc->sysctl_ctx) != 0)
+                       xpt_print(periph->path,
+                           "can't remove sysctl context\n");
+       }
+
+       disk_destroy(softc->disk);
+       free(softc, M_DEVBUF);
+       cam_periph_lock(periph);
+}
+
+static void
+ndaasync(void *callback_arg, u_int32_t code,
+       struct cam_path *path, void *arg)
+{
+       struct cam_periph *periph;
+
+       periph = (struct cam_periph *)callback_arg;
+       switch (code) {
+       case AC_FOUND_DEVICE:
+       {
+               struct ccb_getdev *cgd;
+               cam_status status;
+ 
+               cgd = (struct ccb_getdev *)arg;
+               if (cgd == NULL)
+                       break;
+
+               if (cgd->protocol != PROTO_NVME)
+                       break;
+
+               /*
+                * Allocate a peripheral instance for
+                * this device and start the probe
+                * process.
+                */
+               status = cam_periph_alloc(ndaregister, ndaoninvalidate,
+                                         ndacleanup, ndastart,
+                                         "nda", CAM_PERIPH_BIO,
+                                         path, ndaasync,
+                                         AC_FOUND_DEVICE, cgd);
+
+               if (status != CAM_REQ_CMP
+                && status != CAM_REQ_INPROG)
+                       printf("ndaasync: Unable to attach to new device "
+                               "due to status 0x%x\n", status);
+               break;
+       }
+       case AC_ADVINFO_CHANGED:
+       {
+               uintptr_t buftype;
+
+               buftype = (uintptr_t)arg;
+               if (buftype == CDAI_TYPE_PHYS_PATH) {
+                       struct nda_softc *softc;
+
+                       softc = periph->softc;
+                       disk_attr_changed(softc->disk, "GEOM::physpath",
+                                         M_NOWAIT);
+               }
+               break;
+       }
+       case AC_LOST_DEVICE:
+       default:
+               cam_periph_async(periph, code, path, arg);
+               break;
+       }
+}
+
+static void
+ndasysctlinit(void *context, int pending)
+{
+       struct cam_periph *periph;
+       struct nda_softc *softc;
+       char tmpstr[80], tmpstr2[80];
+
+       periph = (struct cam_periph *)context;
+
+       /* periph was held for us when this task was enqueued */
+       if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
+               cam_periph_release(periph);
+               return;
+       }
+
+       softc = (struct nda_softc *)periph->softc;
+       snprintf(tmpstr, sizeof(tmpstr), "CAM NDA unit %d", 
periph->unit_number);
+       snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
+
+       sysctl_ctx_init(&softc->sysctl_ctx);
+       softc->flags |= NDA_FLAG_SCTX_INIT;
+       softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
+               SYSCTL_STATIC_CHILDREN(_kern_cam_nda), OID_AUTO, tmpstr2,
+               CTLFLAG_RD, 0, tmpstr);
+       if (softc->sysctl_tree == NULL) {
+               printf("ndasysctlinit: unable to allocate sysctl tree\n");
+               cam_periph_release(periph);
+               return;
+       }
+
+       SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
+               OID_AUTO, "unmapped_io", CTLFLAG_RD | CTLFLAG_MPSAFE,
+               &softc->unmappedio, 0, "Unmapped I/O leaf");
+
+       SYSCTL_ADD_INT(&softc->sysctl_ctx,
+                      SYSCTL_CHILDREN(softc->sysctl_tree),
+                      OID_AUTO,
+                      "rotating",
+                      CTLFLAG_RD | CTLFLAG_MPSAFE, 
+                      &nda_rotating_media,
+                      0,
+                      "Rotating media");
+
+#ifdef CAM_IO_STATS
+       softc->sysctl_stats_tree = SYSCTL_ADD_NODE(&softc->sysctl_stats_ctx,
+               SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, "stats",
+               CTLFLAG_RD, 0, "Statistics");
+       if (softc->sysctl_stats_tree == NULL) {
+               printf("ndasysctlinit: unable to allocate sysctl tree for 
stats\n");
+               cam_periph_release(periph);
+               return;
+       }
+       SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
+               SYSCTL_CHILDREN(softc->sysctl_stats_tree),
+               OID_AUTO, "timeouts", CTLFLAG_RD | CTLFLAG_MPSAFE,
+               &softc->timeouts, 0,
+               "Device timeouts reported by the SIM");
+       SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
+               SYSCTL_CHILDREN(softc->sysctl_stats_tree),
+               OID_AUTO, "errors", CTLFLAG_RD | CTLFLAG_MPSAFE,
+               &softc->errors, 0,
+               "Transport errors reported by the SIM.");
+       SYSCTL_ADD_INT(&softc->sysctl_stats_ctx,
+               SYSCTL_CHILDREN(softc->sysctl_stats_tree),
+               OID_AUTO, "pack_invalidations", CTLFLAG_RD | CTLFLAG_MPSAFE,
+               &softc->invalidations, 0,
+               "Device pack invalidations.");
+#endif
+
+       cam_iosched_sysctl_init(softc->cam_iosched, &softc->sysctl_ctx,
+           softc->sysctl_tree);
+
+       cam_periph_release(periph);
+}
+
+static int
+ndagetattr(struct bio *bp)
+{
+       int ret;
+       struct cam_periph *periph;
+
+       periph = (struct cam_periph *)bp->bio_disk->d_drv1;
+       cam_periph_lock(periph);
+       ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
+           periph->path);
+       cam_periph_unlock(periph);
+       if (ret == 0)
+               bp->bio_completed = bp->bio_length;
+       return ret;
+}
+
+static cam_status
+ndaregister(struct cam_periph *periph, void *arg)
+{
+       struct nda_softc *softc;
+       struct disk *disk;
+       struct ccb_pathinq cpi;
+       struct ccb_getdev *cgd;
+       const struct nvme_namespace_data *nsd;
+       const struct nvme_controller_data *cd;
+       char   announce_buf[80];
+//     caddr_t match;
+       u_int maxio;
+       int quirks;
+
+       cgd = (struct ccb_getdev *)arg;
+       if (cgd == NULL) {
+               printf("ndaregister: no getdev CCB, can't register device\n");
+               return(CAM_REQ_CMP_ERR);
+       }
+       nsd = cgd->nvme_data;
+       cd = cgd->nvme_cdata;
+
+       softc = (struct nda_softc *)malloc(sizeof(*softc), M_DEVBUF,
+           M_NOWAIT | M_ZERO);
+
+       if (softc == NULL) {
+               printf("ndaregister: Unable to probe new device. "
+                   "Unable to allocate softc\n");
+               return(CAM_REQ_CMP_ERR);
+       }
+
+       if (cam_iosched_init(&softc->cam_iosched, periph) != 0) {

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to