Home | History | Annotate | Line # | Download | only in ic
ld_nvme.c revision 1.7
      1 /*	$NetBSD: ld_nvme.c,v 1.7 2016/09/20 21:18:08 jdolecek Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 2016 NONAKA Kimihiro <nonaka (at) netbsd.org>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     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.
     15  *
     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.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: ld_nvme.c,v 1.7 2016/09/20 21:18:08 jdolecek Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/systm.h>
     33 #include <sys/kernel.h>
     34 #include <sys/device.h>
     35 #include <sys/buf.h>
     36 #include <sys/bufq.h>
     37 #include <sys/disk.h>
     38 #include <sys/kmem.h>
     39 
     40 #include <dev/ldvar.h>
     41 #include <dev/ic/nvmereg.h>
     42 #include <dev/ic/nvmevar.h>
     43 
     44 struct ld_nvme_softc {
     45 	struct ld_softc		sc_ld;
     46 	struct nvme_softc	*sc_nvme;
     47 
     48 	uint16_t		sc_nsid;
     49 };
     50 
     51 static int	ld_nvme_match(device_t, cfdata_t, void *);
     52 static void	ld_nvme_attach(device_t, device_t, void *);
     53 static int	ld_nvme_detach(device_t, int);
     54 
     55 CFATTACH_DECL_NEW(ld_nvme, sizeof(struct ld_nvme_softc),
     56     ld_nvme_match, ld_nvme_attach, ld_nvme_detach, NULL);
     57 
     58 static int	ld_nvme_start(struct ld_softc *, struct buf *);
     59 static int	ld_nvme_dump(struct ld_softc *, void *, int, int);
     60 static int	ld_nvme_flush(struct ld_softc *, int);
     61 
     62 static void	ld_nvme_biodone(void *, struct buf *, uint16_t);
     63 static void	ld_nvme_syncdone(void *, struct buf *, uint16_t);
     64 
     65 static int
     66 ld_nvme_match(device_t parent, cfdata_t match, void *aux)
     67 {
     68 	struct nvme_attach_args *naa = aux;
     69 
     70 	if (naa->naa_nsid == 0)
     71 		return 0;
     72 
     73 	return 1;
     74 }
     75 
     76 static void
     77 ld_nvme_attach(device_t parent, device_t self, void *aux)
     78 {
     79 	struct ld_nvme_softc *sc = device_private(self);
     80 	struct ld_softc *ld = &sc->sc_ld;
     81 	struct nvme_softc *nsc = device_private(parent);
     82 	struct nvme_attach_args *naa = aux;
     83 	struct nvme_namespace *ns;
     84 	struct nvm_namespace_format *f;
     85 	uint64_t nsze;
     86 	int error;
     87 
     88 	ld->sc_dv = self;
     89 	sc->sc_nvme = nsc;
     90 	sc->sc_nsid = naa->naa_nsid;
     91 
     92 	aprint_naive("\n");
     93 	aprint_normal("\n");
     94 
     95 	error = nvme_ns_identify(sc->sc_nvme, sc->sc_nsid);
     96 	if (error) {
     97 		aprint_error_dev(self, "couldn't identify namespace\n");
     98 		return;
     99 	}
    100 
    101 	ns = nvme_ns_get(sc->sc_nvme, sc->sc_nsid);
    102 	KASSERT(ns);
    103 	nsze = lemtoh64(&ns->ident->nsze);
    104 	f = &ns->ident->lbaf[NVME_ID_NS_FLBAS(ns->ident->flbas)];
    105 
    106 	ld->sc_secsize = 1 << f->lbads;
    107 	ld->sc_secperunit = nsze;
    108 	ld->sc_maxxfer = MAXPHYS; /* XXX set according to device */
    109 	ld->sc_maxqueuecnt = naa->naa_qentries; /* XXX set to max allowed by device, not current config */
    110 	ld->sc_start = ld_nvme_start;
    111 	ld->sc_dump = ld_nvme_dump;
    112 	ld->sc_flush = ld_nvme_flush;
    113 	ld->sc_flags = LDF_ENABLED;
    114 	ldattach(ld, "fcfs");
    115 }
    116 
    117 static int
    118 ld_nvme_detach(device_t self, int flags)
    119 {
    120 	struct ld_nvme_softc *sc = device_private(self);
    121 	struct ld_softc *ld = &sc->sc_ld;
    122 	int rv;
    123 
    124 	if ((rv = ldbegindetach(ld, flags)) != 0)
    125 		return rv;
    126 	ldenddetach(ld);
    127 
    128 	nvme_ns_free(sc->sc_nvme, sc->sc_nsid);
    129 
    130 	return 0;
    131 }
    132 
    133 static int
    134 ld_nvme_start(struct ld_softc *ld, struct buf *bp)
    135 {
    136 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
    137 
    138 	return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
    139 	    bp, bp->b_data, bp->b_bcount,
    140 	    sc->sc_ld.sc_secsize, bp->b_rawblkno,
    141 	    BUF_ISWRITE(bp) ? 0 : NVME_NS_CTX_F_READ,
    142 	    ld_nvme_biodone);
    143 }
    144 
    145 static int
    146 ld_nvme_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    147 {
    148 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
    149 
    150 	return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
    151 	    NULL, data, blkcnt * ld->sc_secsize,
    152 	    sc->sc_ld.sc_secsize, blkno,
    153 	    NVME_NS_CTX_F_POLL,
    154 	    ld_nvme_biodone);
    155 }
    156 
    157 static void
    158 ld_nvme_biodone(void *xc, struct buf *bp, uint16_t cmd_status)
    159 {
    160 	struct ld_nvme_softc *sc = xc;
    161 	uint16_t status = NVME_CQE_SC(cmd_status);
    162 
    163 	if (bp != NULL) {
    164 		if (status != NVME_CQE_SC_SUCCESS) {
    165 			bp->b_error = EIO;
    166 			bp->b_resid = bp->b_bcount;
    167 			aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
    168 		} else {
    169 			bp->b_resid = 0;
    170 		}
    171 		lddone(&sc->sc_ld, bp);
    172 	} else {
    173 		if (status != NVME_CQE_SC_SUCCESS) {
    174 			aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
    175 		}
    176 	}
    177 }
    178 
    179 static int
    180 ld_nvme_flush(struct ld_softc *ld, int flags)
    181 {
    182 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
    183 
    184 	/* wait for the sync to finish */
    185 	return nvme_ns_sync(sc->sc_nvme, sc->sc_nsid, sc,
    186 	    NVME_NS_CTX_F_POLL, ld_nvme_syncdone);
    187 }
    188 
    189 static void
    190 ld_nvme_syncdone(void *xc, struct buf *bp, uint16_t cmd_status)
    191 {
    192 	/* nothing to do */
    193 }
    194