Home | History | Annotate | Line # | Download | only in ic
ld_nvme.c revision 1.14
      1 /*	$NetBSD: ld_nvme.c,v 1.14 2017/02/28 20:55:09 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.14 2017/02/28 20:55:09 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 	/* getcache handling */
     54 	kmutex_t		sc_getcache_lock;
     55 	kcondvar_t		sc_getcache_cv;
     56 	kcondvar_t		sc_getcache_ready_cv;
     57 	bool			sc_getcache_waiting;
     58 	bool			sc_getcache_ready;
     59 	int			sc_getcache_result;
     60 };
     61 
     62 static int	ld_nvme_match(device_t, cfdata_t, void *);
     63 static void	ld_nvme_attach(device_t, device_t, void *);
     64 static int	ld_nvme_detach(device_t, int);
     65 
     66 CFATTACH_DECL_NEW(ld_nvme, sizeof(struct ld_nvme_softc),
     67     ld_nvme_match, ld_nvme_attach, ld_nvme_detach, NULL);
     68 
     69 static int	ld_nvme_start(struct ld_softc *, struct buf *);
     70 static int	ld_nvme_dump(struct ld_softc *, void *, int, int);
     71 static int	ld_nvme_flush(struct ld_softc *, bool);
     72 static int	ld_nvme_getcache(struct ld_softc *, int *);
     73 static int	ld_nvme_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
     74 
     75 static void	ld_nvme_biodone(void *, struct buf *, uint16_t, uint32_t);
     76 static void	ld_nvme_syncdone(void *, struct buf *, uint16_t, uint32_t);
     77 static void	ld_nvme_getcache_done(void *, struct buf *, uint16_t, uint32_t);
     78 
     79 static int
     80 ld_nvme_match(device_t parent, cfdata_t match, void *aux)
     81 {
     82 	struct nvme_attach_args *naa = aux;
     83 
     84 	if (naa->naa_nsid == 0)
     85 		return 0;
     86 
     87 	return 1;
     88 }
     89 
     90 static void
     91 ld_nvme_attach(device_t parent, device_t self, void *aux)
     92 {
     93 	struct ld_nvme_softc *sc = device_private(self);
     94 	struct ld_softc *ld = &sc->sc_ld;
     95 	struct nvme_softc *nsc = device_private(parent);
     96 	struct nvme_attach_args *naa = aux;
     97 	struct nvme_namespace *ns;
     98 	struct nvm_namespace_format *f;
     99 	uint64_t nsze;
    100 	int error;
    101 
    102 	ld->sc_dv = self;
    103 	sc->sc_nvme = nsc;
    104 	sc->sc_nsid = naa->naa_nsid;
    105 
    106 	mutex_init(&sc->sc_getcache_lock, MUTEX_DEFAULT, IPL_SOFTBIO);
    107 	cv_init(&sc->sc_getcache_cv, "nvmegcq");
    108 	cv_init(&sc->sc_getcache_ready_cv, "nvmegcr");
    109 
    110 	aprint_naive("\n");
    111 	aprint_normal("\n");
    112 
    113 	error = nvme_ns_identify(sc->sc_nvme, sc->sc_nsid);
    114 	if (error) {
    115 		aprint_error_dev(self, "couldn't identify namespace\n");
    116 		return;
    117 	}
    118 
    119 	ns = nvme_ns_get(sc->sc_nvme, sc->sc_nsid);
    120 	KASSERT(ns);
    121 	nsze = lemtoh64(&ns->ident->nsze);
    122 	f = &ns->ident->lbaf[NVME_ID_NS_FLBAS(ns->ident->flbas)];
    123 
    124 	ld->sc_secsize = 1 << f->lbads;
    125 	ld->sc_secperunit = nsze;
    126 	ld->sc_maxxfer = naa->naa_maxphys;
    127 	ld->sc_maxqueuecnt = naa->naa_qentries;
    128 	ld->sc_start = ld_nvme_start;
    129 	ld->sc_dump = ld_nvme_dump;
    130 	ld->sc_ioctl = ld_nvme_ioctl;
    131 	ld->sc_flags = LDF_ENABLED;
    132 	ldattach(ld, "fcfs");
    133 }
    134 
    135 static int
    136 ld_nvme_detach(device_t self, int flags)
    137 {
    138 	struct ld_nvme_softc *sc = device_private(self);
    139 	struct ld_softc *ld = &sc->sc_ld;
    140 	int rv;
    141 
    142 	if ((rv = ldbegindetach(ld, flags)) != 0)
    143 		return rv;
    144 	ldenddetach(ld);
    145 
    146 	nvme_ns_free(sc->sc_nvme, sc->sc_nsid);
    147 
    148 	return 0;
    149 }
    150 
    151 static int
    152 ld_nvme_start(struct ld_softc *ld, struct buf *bp)
    153 {
    154 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
    155 
    156 	return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
    157 	    bp, bp->b_data, bp->b_bcount,
    158 	    sc->sc_ld.sc_secsize, bp->b_rawblkno,
    159 	    BUF_ISWRITE(bp) ? 0 : NVME_NS_CTX_F_READ,
    160 	    ld_nvme_biodone);
    161 }
    162 
    163 static int
    164 ld_nvme_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    165 {
    166 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
    167 
    168 	return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
    169 	    NULL, data, blkcnt * ld->sc_secsize,
    170 	    sc->sc_ld.sc_secsize, blkno,
    171 	    NVME_NS_CTX_F_POLL,
    172 	    ld_nvme_biodone);
    173 }
    174 
    175 static void
    176 ld_nvme_biodone(void *xc, struct buf *bp, uint16_t cmd_status, uint32_t cdw0)
    177 {
    178 	struct ld_nvme_softc *sc = xc;
    179 	uint16_t status = NVME_CQE_SC(cmd_status);
    180 
    181 	if (bp != NULL) {
    182 		if (status != NVME_CQE_SC_SUCCESS) {
    183 			bp->b_error = EIO;
    184 			bp->b_resid = bp->b_bcount;
    185 			aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
    186 		} else {
    187 			bp->b_resid = 0;
    188 		}
    189 		lddone(&sc->sc_ld, bp);
    190 	} else {
    191 		if (status != NVME_CQE_SC_SUCCESS) {
    192 			aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
    193 		}
    194 	}
    195 }
    196 
    197 static int
    198 ld_nvme_flush(struct ld_softc *ld, bool poll)
    199 {
    200 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
    201 
    202 	if (!nvme_has_volatile_write_cache(sc->sc_nvme)) {
    203 		/* cache not present, no value in trying to flush it */
    204 		return 0;
    205 	}
    206 
    207 	return nvme_ns_sync(sc->sc_nvme, sc->sc_nsid, sc,
    208 	    poll ? NVME_NS_CTX_F_POLL : 0,
    209 	    ld_nvme_syncdone);
    210 }
    211 
    212 static void
    213 ld_nvme_syncdone(void *xc, struct buf *bp, uint16_t cmd_status, uint32_t cdw0)
    214 {
    215 	/* nothing to do */
    216 }
    217 
    218 static int
    219 ld_nvme_getcache(struct ld_softc *ld, int *addr)
    220 {
    221 	int error;
    222 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
    223 
    224 	*addr = 0;
    225 
    226 	if (!nvme_has_volatile_write_cache(sc->sc_nvme)) {
    227 		/* cache simply not present */
    228 		return 0;
    229 	}
    230 
    231 	/*
    232 	 * This is admin queue request. The queue is relatively limited in size,
    233 	 * and this is not performance critical call, so have at most one pending
    234 	 * cache request at a time to avoid spurious EWOULDBLOCK failures.
    235 	 */
    236 	mutex_enter(&sc->sc_getcache_lock);
    237 	while (sc->sc_getcache_waiting) {
    238 		error = cv_wait_sig(&sc->sc_getcache_cv, &sc->sc_getcache_lock);
    239 		if (error)
    240 			goto out;
    241 	}
    242 	sc->sc_getcache_waiting = true;
    243 	sc->sc_getcache_ready = false;
    244 	mutex_exit(&sc->sc_getcache_lock);
    245 
    246 	error = nvme_admin_getcache(sc->sc_nvme, sc, ld_nvme_getcache_done);
    247 	if (error) {
    248 		mutex_enter(&sc->sc_getcache_lock);
    249 		goto out;
    250 	}
    251 
    252 	mutex_enter(&sc->sc_getcache_lock);
    253 	while (!sc->sc_getcache_ready) {
    254 		error = cv_wait_sig(&sc->sc_getcache_ready_cv,
    255 		    &sc->sc_getcache_lock);
    256 		if (error)
    257 			goto out;
    258 	}
    259 
    260 	KDASSERT(sc->sc_getcache_ready);
    261 
    262 	if (sc->sc_getcache_result >= 0)
    263 		*addr |= sc->sc_getcache_result;
    264 	else
    265 		error = EINVAL;
    266 
    267     out:
    268 	sc->sc_getcache_waiting = false;
    269 
    270 	/* wake one of eventual waiters */
    271 	cv_signal(&sc->sc_getcache_cv);
    272 
    273 	mutex_exit(&sc->sc_getcache_lock);
    274 
    275 	return error;
    276 }
    277 
    278 static void
    279 ld_nvme_getcache_done(void *xc, struct buf *bp, uint16_t cmd_status, uint32_t cdw0)
    280 {
    281 	struct ld_nvme_softc *sc = xc;
    282 	uint16_t status = NVME_CQE_SC(cmd_status);
    283 	int result;
    284 
    285 	if (status == NVME_CQE_SC_SUCCESS) {
    286 		result = 0;
    287 
    288 		if (cdw0 & NVME_CQE_CDW0_VWC_WCE)
    289 			result |= DKCACHE_WRITE;
    290 
    291 		/*
    292 		 * If volatile write cache is present, the flag shall also be
    293 		 * settable.
    294 		 */
    295 		result |= DKCACHE_WCHANGE;
    296 	} else {
    297 		result = -1;
    298 	}
    299 
    300 	mutex_enter(&sc->sc_getcache_lock);
    301 	sc->sc_getcache_result = result;
    302 	sc->sc_getcache_ready = true;
    303 
    304 	/* wake up the waiter */
    305 	cv_signal(&sc->sc_getcache_ready_cv);
    306 
    307 	mutex_exit(&sc->sc_getcache_lock);
    308 }
    309 
    310 static int
    311 ld_nvme_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag, bool poll)
    312 {
    313 	int error;
    314 
    315 	switch (cmd) {
    316 	case DIOCCACHESYNC:
    317 		error = ld_nvme_flush(ld, poll);
    318 		break;
    319 
    320 	case DIOCGCACHE:
    321 		error = ld_nvme_getcache(ld, (int *)addr);
    322 		break;
    323 
    324 	default:
    325 		error = EPASSTHROUGH;
    326 		break;
    327 	}
    328 
    329 	return error;
    330 }
    331 
    332 MODULE(MODULE_CLASS_DRIVER, ld_nvme, "ld,nvme");
    333 
    334 #ifdef _MODULE
    335 /*
    336  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
    337  * XXX it will be defined in the common-code module
    338  */
    339 #undef	CFDRIVER_DECL
    340 #define	CFDRIVER_DECL(name, class, attr)
    341 #include "ioconf.c"
    342 #endif
    343 
    344 static int
    345 ld_nvme_modcmd(modcmd_t cmd, void *opaque)
    346 {
    347 #ifdef _MODULE
    348 	/*
    349 	 * We ignore the cfdriver_vec[] that ioconf provides, since
    350 	 * the cfdrivers are attached already.
    351 	 */
    352 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
    353 #endif
    354 	int error = 0;
    355 
    356 #ifdef _MODULE
    357 	switch (cmd) {
    358 	case MODULE_CMD_INIT:
    359 		error = config_init_component(no_cfdriver_vec,
    360 		    cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
    361 		break;
    362 	case MODULE_CMD_FINI:
    363 		error = config_fini_component(no_cfdriver_vec,
    364 		    cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
    365 		break;
    366 	default:
    367 		error = ENOTTY;
    368 		break;
    369 	}
    370 #endif
    371 
    372 	return error;
    373 }
    374