Home | History | Annotate | Line # | Download | only in ic
ld_nvme.c revision 1.10
      1 /*	$NetBSD: ld_nvme.c,v 1.10 2016/11/01 14:39:38 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.10 2016/11/01 14:39:38 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 #include <sys/module.h>
     40 
     41 #include <dev/ldvar.h>
     42 #include <dev/ic/nvmereg.h>
     43 #include <dev/ic/nvmevar.h>
     44 
     45 #include "ioconf.h"
     46 
     47 struct ld_nvme_softc {
     48 	struct ld_softc		sc_ld;
     49 	struct nvme_softc	*sc_nvme;
     50 
     51 	uint16_t		sc_nsid;
     52 };
     53 
     54 static int	ld_nvme_match(device_t, cfdata_t, void *);
     55 static void	ld_nvme_attach(device_t, device_t, void *);
     56 static int	ld_nvme_detach(device_t, int);
     57 
     58 CFATTACH_DECL_NEW(ld_nvme, sizeof(struct ld_nvme_softc),
     59     ld_nvme_match, ld_nvme_attach, ld_nvme_detach, NULL);
     60 
     61 static int	ld_nvme_start(struct ld_softc *, struct buf *);
     62 static int	ld_nvme_dump(struct ld_softc *, void *, int, int);
     63 static int	ld_nvme_flush(struct ld_softc *, int);
     64 
     65 static void	ld_nvme_biodone(void *, struct buf *, uint16_t);
     66 static void	ld_nvme_syncdone(void *, struct buf *, uint16_t);
     67 
     68 static int
     69 ld_nvme_match(device_t parent, cfdata_t match, void *aux)
     70 {
     71 	struct nvme_attach_args *naa = aux;
     72 
     73 	if (naa->naa_nsid == 0)
     74 		return 0;
     75 
     76 	return 1;
     77 }
     78 
     79 static void
     80 ld_nvme_attach(device_t parent, device_t self, void *aux)
     81 {
     82 	struct ld_nvme_softc *sc = device_private(self);
     83 	struct ld_softc *ld = &sc->sc_ld;
     84 	struct nvme_softc *nsc = device_private(parent);
     85 	struct nvme_attach_args *naa = aux;
     86 	struct nvme_namespace *ns;
     87 	struct nvm_namespace_format *f;
     88 	uint64_t nsze;
     89 	int error;
     90 
     91 	ld->sc_dv = self;
     92 	sc->sc_nvme = nsc;
     93 	sc->sc_nsid = naa->naa_nsid;
     94 
     95 	aprint_naive("\n");
     96 	aprint_normal("\n");
     97 
     98 	error = nvme_ns_identify(sc->sc_nvme, sc->sc_nsid);
     99 	if (error) {
    100 		aprint_error_dev(self, "couldn't identify namespace\n");
    101 		return;
    102 	}
    103 
    104 	ns = nvme_ns_get(sc->sc_nvme, sc->sc_nsid);
    105 	KASSERT(ns);
    106 	nsze = lemtoh64(&ns->ident->nsze);
    107 	f = &ns->ident->lbaf[NVME_ID_NS_FLBAS(ns->ident->flbas)];
    108 
    109 	ld->sc_secsize = 1 << f->lbads;
    110 	ld->sc_secperunit = nsze;
    111 	ld->sc_maxxfer = naa->naa_maxphys;
    112 	ld->sc_maxqueuecnt = naa->naa_qentries;
    113 	ld->sc_start = ld_nvme_start;
    114 	ld->sc_dump = ld_nvme_dump;
    115 	ld->sc_flush = ld_nvme_flush;
    116 	ld->sc_flags = LDF_ENABLED;
    117 	ldattach(ld, "fcfs");
    118 }
    119 
    120 static int
    121 ld_nvme_detach(device_t self, int flags)
    122 {
    123 	struct ld_nvme_softc *sc = device_private(self);
    124 	struct ld_softc *ld = &sc->sc_ld;
    125 	int rv;
    126 
    127 	if ((rv = ldbegindetach(ld, flags)) != 0)
    128 		return rv;
    129 	ldenddetach(ld);
    130 
    131 	nvme_ns_free(sc->sc_nvme, sc->sc_nsid);
    132 
    133 	return 0;
    134 }
    135 
    136 static int
    137 ld_nvme_start(struct ld_softc *ld, struct buf *bp)
    138 {
    139 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
    140 
    141 	return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
    142 	    bp, bp->b_data, bp->b_bcount,
    143 	    sc->sc_ld.sc_secsize, bp->b_rawblkno,
    144 	    BUF_ISWRITE(bp) ? 0 : NVME_NS_CTX_F_READ,
    145 	    ld_nvme_biodone);
    146 }
    147 
    148 static int
    149 ld_nvme_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    150 {
    151 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
    152 
    153 	return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
    154 	    NULL, data, blkcnt * ld->sc_secsize,
    155 	    sc->sc_ld.sc_secsize, blkno,
    156 	    NVME_NS_CTX_F_POLL,
    157 	    ld_nvme_biodone);
    158 }
    159 
    160 static void
    161 ld_nvme_biodone(void *xc, struct buf *bp, uint16_t cmd_status)
    162 {
    163 	struct ld_nvme_softc *sc = xc;
    164 	uint16_t status = NVME_CQE_SC(cmd_status);
    165 
    166 	if (bp != NULL) {
    167 		if (status != NVME_CQE_SC_SUCCESS) {
    168 			bp->b_error = EIO;
    169 			bp->b_resid = bp->b_bcount;
    170 			aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
    171 		} else {
    172 			bp->b_resid = 0;
    173 		}
    174 		lddone(&sc->sc_ld, bp);
    175 	} else {
    176 		if (status != NVME_CQE_SC_SUCCESS) {
    177 			aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
    178 		}
    179 	}
    180 }
    181 
    182 static int
    183 ld_nvme_flush(struct ld_softc *ld, int flags)
    184 {
    185 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
    186 
    187 	/* wait for the sync to finish */
    188 	return nvme_ns_sync(sc->sc_nvme, sc->sc_nsid, sc,
    189 	    (flags & LDFL_POLL) ? NVME_NS_CTX_F_POLL : 0,
    190 	    ld_nvme_syncdone);
    191 }
    192 
    193 static void
    194 ld_nvme_syncdone(void *xc, struct buf *bp, uint16_t cmd_status)
    195 {
    196 	/* nothing to do */
    197 }
    198 
    199 MODULE(MODULE_CLASS_DRIVER, ld_nvme, "ld,nvme");
    200 
    201 #ifdef _MODULE
    202 /*
    203  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
    204  * XXX it will be defined in the common-code module
    205  */
    206 #undef	CFDRIVER_DECL
    207 #define	CFDRIVER_DECL(name, class, attr)
    208 #include "ioconf.c"
    209 #endif
    210 
    211 static int
    212 ld_nvme_modcmd(modcmd_t cmd, void *opaque)
    213 {
    214 #ifdef _MODULE
    215 	/*
    216 	 * We ignore the cfdriver_vec[] that ioconf provides, since
    217 	 * the cfdrivers are attached already.
    218 	 */
    219 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
    220 #endif
    221 	int error = 0;
    222 
    223 #ifdef _MODULE
    224 	switch (cmd) {
    225 	case MODULE_CMD_INIT:
    226 		error = config_init_component(no_cfdriver_vec,
    227 		    cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
    228 		break;
    229 	case MODULE_CMD_FINI:
    230 		error = config_fini_component(no_cfdriver_vec,
    231 		    cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
    232 		break;
    233 	default:
    234 		error = ENOTTY;
    235 		break;
    236 	}
    237 #endif
    238 
    239 	return error;
    240 }
    241