Home | History | Annotate | Line # | Download | only in dev
ld_thunkbus.c revision 1.31
      1 /* $NetBSD: ld_thunkbus.c,v 1.31 2018/01/13 10:08:35 reinoud Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2011 Jared D. McNeill <jmcneill (at) invisible.ca>
      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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: ld_thunkbus.c,v 1.31 2018/01/13 10:08:35 reinoud Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/proc.h>
     34 #include <sys/systm.h>
     35 #include <sys/device.h>
     36 #include <sys/buf.h>
     37 #include <sys/disk.h>
     38 #include <sys/kmem.h>
     39 
     40 #include <dev/ldvar.h>
     41 
     42 #include <machine/mainbus.h>
     43 #include <machine/thunk.h>
     44 #include <machine/intr.h>
     45 
     46 static int	ld_thunkbus_match(device_t, cfdata_t, void *);
     47 static void	ld_thunkbus_attach(device_t, device_t, void *);
     48 
     49 static int	ld_thunkbus_ldstart(struct ld_softc *, struct buf *);
     50 static int	ld_thunkbus_lddump(struct ld_softc *, void *, int, int);
     51 static int	ld_thunkbus_ldflush(struct ld_softc *, int);
     52 
     53 //#define LD_USE_AIO
     54 
     55 #ifdef LD_USE_AIO
     56 static int	ld_aio_sig(void *);
     57 #endif
     58 static void	ld_thunkbus_complete(void *arg);
     59 
     60 struct ld_thunkbus_softc;
     61 
     62 struct ld_thunkbus_transfer {
     63 	struct ld_thunkbus_softc *tt_sc;
     64 	struct aiocb	tt_aio;
     65 	struct buf	*tt_bp;
     66 };
     67 
     68 struct ld_thunkbus_softc {
     69 	struct ld_softc	sc_ld;
     70 
     71 	int		sc_fd;
     72 	void		*sc_ih;
     73 	void		*sc_aio_ih;
     74 
     75 	struct ld_thunkbus_transfer sc_tt;
     76 	bool		busy;
     77 };
     78 
     79 CFATTACH_DECL_NEW(ld_thunkbus, sizeof(struct ld_thunkbus_softc),
     80     ld_thunkbus_match, ld_thunkbus_attach, NULL, NULL);
     81 
     82 static int
     83 ld_thunkbus_match(device_t parent, cfdata_t match, void *opaque)
     84 {
     85 	struct thunkbus_attach_args *taa = opaque;
     86 
     87 	if (taa->taa_type != THUNKBUS_TYPE_DISKIMAGE)
     88 		return 0;
     89 
     90 	return 1;
     91 }
     92 
     93 static void
     94 ld_thunkbus_attach(device_t parent, device_t self, void *opaque)
     95 {
     96 	struct ld_thunkbus_softc *sc = device_private(self);
     97 	struct ld_softc *ld = &sc->sc_ld;
     98 	struct thunkbus_attach_args *taa = opaque;
     99 	const char *path = taa->u.diskimage.path;
    100 	ssize_t blksize;
    101 	off_t size;
    102 
    103 	ld->sc_dv = self;
    104 
    105 	sc->sc_fd = thunk_open(path, O_RDWR, 0);
    106 	if (sc->sc_fd == -1) {
    107 		aprint_error(": couldn't open %s: %d\n", path, thunk_geterrno());
    108 		return;
    109 	}
    110 	if (thunk_fstat_getsize(sc->sc_fd, &size, &blksize) == -1) {
    111 		aprint_error(": couldn't stat %s: %d\n", path, thunk_geterrno());
    112 		return;
    113 	}
    114 
    115 	aprint_naive("\n");
    116 	aprint_normal(": %s (%lld)\n", path, (long long)size);
    117 
    118 	ld->sc_flags = LDF_ENABLED;
    119 	ld->sc_maxxfer = MAXPHYS;
    120 	ld->sc_secsize = 512;
    121 	ld->sc_secperunit = size / ld->sc_secsize;
    122 	ld->sc_maxqueuecnt = 1;
    123 	ld->sc_start = ld_thunkbus_ldstart;
    124 	ld->sc_dump = ld_thunkbus_lddump;
    125 	ld->sc_flush = ld_thunkbus_ldflush;
    126 
    127 	sc->sc_ih = softint_establish(SOFTINT_BIO,
    128 	    ld_thunkbus_complete, ld);
    129 
    130 #ifdef LD_USE_AIO
    131 	sc->sc_aio_ih = sigio_intr_establish(ld_aio_sig, sc);
    132 	if (sc->sc_aio_ih == NULL)
    133 		panic("couldn't establish aio sig interrupt");
    134 #endif
    135 
    136 	sc->busy = false;
    137 
    138 	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
    139 }
    140 
    141 #ifdef LD_USE_AIO
    142 static int
    143 ld_aio_sig(void *arg)
    144 {
    145 	struct ld_softc *ld = arg;
    146 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    147 
    148 	softint_schedule(sc->sc_ih);
    149 
    150 	return 0;
    151 }
    152 
    153 static int
    154 ld_thunkbus_ldstart(struct ld_softc *ld, struct buf *bp)
    155 {
    156 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    157 	struct ld_thunkbus_transfer *tt = &sc->sc_tt;
    158 	off_t offset = bp->b_rawblkno * ld->sc_secsize;
    159 	off_t disksize = ld->sc_secsize * ld->sc_secperunit;
    160 	int error;
    161 
    162 	tt->tt_sc = sc;
    163 	tt->tt_bp = bp;
    164 
    165 	memset(&tt->tt_aio, 0, sizeof(tt->tt_aio));
    166 	tt->tt_aio.aio_fildes = sc->sc_fd;
    167 	tt->tt_aio.aio_buf = bp->b_data;
    168 	tt->tt_aio.aio_nbytes = bp->b_bcount;
    169 	tt->tt_aio.aio_offset = offset;
    170 
    171 	tt->tt_aio.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
    172 	tt->tt_aio.aio_sigevent.sigev_signo = SIGIO;
    173 	tt->tt_aio.aio_sigevent.sigev_value.sival_ptr = tt;
    174 #if 0
    175 	device_printf(sc->sc_ld.sc_dv, "%s addr %p, off=%lld, count=%lld\n",
    176 	    (bp->b_flags & B_READ) ? "rd" : "wr",
    177 	    bp->b_data,
    178 	    (long long)bp->b_rawblkno,
    179 	    (long long)bp->b_bcount);
    180 #endif
    181 	if (sc->busy)
    182 		panic("%s: reentry", __func__);
    183 
    184 	if ((offset < 0) || (offset + bp->b_bcount > disksize)) {
    185 		error = EIO;
    186 		bp->b_error = error;
    187 		bp->b_resid = bp->b_bcount;
    188 	} else {
    189 		sc->busy = true;
    190 		if (bp->b_flags & B_READ)
    191 			error = thunk_aio_read(&tt->tt_aio);
    192 		else
    193 			error = thunk_aio_write(&tt->tt_aio);
    194 	}
    195 	return error;
    196 }
    197 
    198 static void
    199 ld_thunkbus_complete(void *arg)
    200 {
    201 	struct ld_softc *ld = arg;
    202 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    203 	struct ld_thunkbus_transfer *tt = &sc->sc_tt;
    204 	struct buf *bp = tt->tt_bp;
    205 	int error;
    206 
    207 	/*
    208 	 * check if our aio has finished, we could be called for whatever
    209 	 * reason, for whatever SIGIO since signals can be missed.
    210 	 */
    211 	if (!sc->busy)
    212 		return;
    213 
    214 	/* check if it was OUR sigio */
    215 	error = thunk_aio_error(&tt->tt_aio);
    216 	if (error == EINPROGRESS)
    217 		return;
    218 
    219 	/* use the result */
    220 	if ((error == 0) &&
    221 	    thunk_aio_return(&tt->tt_aio) != -1) {
    222 		bp->b_resid = 0;
    223 	} else {
    224 		bp->b_error = error;
    225 		bp->b_resid = bp->b_bcount;
    226 	}
    227 
    228 	thunk_printf_debug("\tfin\n");
    229 	if (bp->b_error)
    230 		thunk_printf_debug("error!\n");
    231 
    232 	sc->busy = false;
    233 	lddone(&sc->sc_ld, bp);
    234 }
    235 
    236 
    237 #else /* LD_USE_AIO */
    238 
    239 
    240 static int
    241 ld_thunkbus_ldstart(struct ld_softc *ld, struct buf *bp)
    242 {
    243 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    244 	struct ld_thunkbus_transfer *tt = &sc->sc_tt;
    245 
    246 	tt->tt_sc = sc;
    247 	tt->tt_bp = bp;
    248 
    249 	/* let the softint do the work */
    250 	sc->busy = true;
    251 	softint_schedule(sc->sc_ih);
    252 
    253 	return 0;
    254 }
    255 
    256 static void
    257 ld_thunkbus_complete(void *arg)
    258 {
    259 	struct ld_softc *ld = arg;
    260 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    261 	struct ld_thunkbus_transfer *tt = &sc->sc_tt;
    262 	struct buf *bp = tt->tt_bp;
    263 	off_t offset = (off_t) bp->b_rawblkno * ld->sc_secsize;
    264 	int64_t ret;
    265 
    266 	if (!sc->busy)
    267 		panic("%s: but not busy?\n", __func__);
    268 
    269 	//printf("%s: %s %u @ %lld -> %p (flags 0x%08x)\n", __func__,
    270 	//    bp->b_flags & B_READ ? "read" : "write",
    271 	//    (unsigned int)bp->b_bcount, (long long)offset, bp->b_data, bp->b_flags);
    272 
    273 	/* read/write the request */
    274 	if (bp->b_flags & B_READ) {
    275 		ret = thunk_pread(sc->sc_fd, bp->b_data, bp->b_bcount, offset);
    276 	} else {
    277 		ret = thunk_pwrite(sc->sc_fd, bp->b_data, bp->b_bcount, offset);
    278 	}
    279 
    280 	//if (ret == -1)
    281 	//	printf("%s: errno = %d\n", __func__, thunk_geterrno());
    282 
    283 	/* setup return params */
    284 	if ((ret >= 0) && (ret == bp->b_bcount)) {
    285 		bp->b_resid = 0;
    286 	} else {
    287 		bp->b_error = thunk_geterrno();
    288 		bp->b_resid = bp->b_bcount;
    289 	}
    290 	thunk_printf_debug("\tfin\n");
    291 	if (bp->b_error)
    292 		thunk_printf_debug("error!\n");
    293 
    294 	sc->busy = false;
    295 	lddone(&sc->sc_ld, bp);
    296 }
    297 
    298 #endif /* LD_USE_AIO */
    299 
    300 
    301 static int
    302 ld_thunkbus_lddump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    303 {
    304 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    305 	ssize_t len;
    306 
    307 	len = thunk_pwrite(sc->sc_fd, data, blkcnt, blkno * ld->sc_secsize);
    308 	if (len == -1)
    309 		return thunk_geterrno();
    310 	else if (len != blkcnt) {
    311 		device_printf(ld->sc_dv, "%s failed (short xfer)\n", __func__);
    312 		return EIO;
    313 	}
    314 
    315 	return 0;
    316 }
    317 
    318 static int
    319 ld_thunkbus_ldflush(struct ld_softc *ld, int flags)
    320 {
    321 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    322 
    323 	if (thunk_fsync(sc->sc_fd) == -1)
    324 		return thunk_geterrno();
    325 
    326 	return 0;
    327 }
    328