Home | History | Annotate | Line # | Download | only in dev
ld_thunkbus.c revision 1.17
      1 /* $NetBSD: ld_thunkbus.c,v 1.17 2011/11/27 20:08:23 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.17 2011/11/27 20:08:23 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 void	ld_thunkbus_sig(int, siginfo_t *, 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 
     74 	struct ld_thunkbus_transfer sc_tt;
     75 	bool		busy;
     76 };
     77 
     78 CFATTACH_DECL_NEW(ld_thunkbus, sizeof(struct ld_thunkbus_softc),
     79     ld_thunkbus_match, ld_thunkbus_attach, NULL, NULL);
     80 
     81 static int
     82 ld_thunkbus_match(device_t parent, cfdata_t match, void *opaque)
     83 {
     84 	struct thunkbus_attach_args *taa = opaque;
     85 
     86 	if (taa->taa_type != THUNKBUS_TYPE_DISKIMAGE)
     87 		return 0;
     88 
     89 	return 1;
     90 }
     91 
     92 static void
     93 ld_thunkbus_attach(device_t parent, device_t self, void *opaque)
     94 {
     95 	struct ld_thunkbus_softc *sc = device_private(self);
     96 	struct ld_softc *ld = &sc->sc_ld;
     97 	struct thunkbus_attach_args *taa = opaque;
     98 	const char *path = taa->u.diskimage.path;
     99 	ssize_t size, blksize;
    100 
    101 	ld->sc_dv = self;
    102 
    103 	sc->sc_fd = thunk_open(path, O_RDWR, 0);
    104 	if (sc->sc_fd == -1) {
    105 		aprint_error(": couldn't open %s: %d\n", path, thunk_geterrno());
    106 		return;
    107 	}
    108 	if (thunk_fstat_getsize(sc->sc_fd, &size, &blksize) == -1) {
    109 		aprint_error(": couldn't stat %s: %d\n", path, thunk_geterrno());
    110 		return;
    111 	}
    112 
    113 	aprint_naive("\n");
    114 	aprint_normal(": %s (%lld)\n", path, (long long)size);
    115 
    116 	ld->sc_flags = LDF_ENABLED;
    117 	ld->sc_maxxfer = blksize;
    118 	ld->sc_secsize = 512;
    119 	ld->sc_secperunit = size / ld->sc_secsize;
    120 	ld->sc_maxqueuecnt = 1;
    121 	ld->sc_start = ld_thunkbus_ldstart;
    122 	ld->sc_dump = ld_thunkbus_lddump;
    123 	ld->sc_flush = ld_thunkbus_ldflush;
    124 
    125 	sc->sc_ih = softint_establish(SOFTINT_BIO,
    126 	    ld_thunkbus_complete, sc);
    127 
    128 #ifdef LD_USE_AIO
    129 	struct sigaction sa;
    130 
    131 	sa.sa_flags = SA_RESTART | SA_SIGINFO;
    132 	sa.sa_sigaction = ld_thunkbus_sig;
    133 	thunk_sigemptyset(&sa.sa_mask);
    134 //	thunk_sigaddset(&sa.sa_mask, SIGALRM);
    135 	if (thunk_sigaction(SIGIO, &sa, NULL) == -1)
    136 		panic("couldn't register SIGIO handler: %d", thunk_geterrno());
    137 #endif
    138 
    139 	sc->busy = false;
    140 
    141 	ldattach(ld);
    142 }
    143 
    144 #ifdef LD_USE_AIO
    145 static void
    146 ld_thunkbus_sig(int sig, siginfo_t *info, void *ctx)
    147 {
    148 	struct ld_thunkbus_transfer *tt = NULL;
    149 	struct ld_thunkbus_softc *sc;
    150 
    151 	curcpu()->ci_idepth++;
    152 
    153 	if (info->si_signo == SIGIO) {
    154 		if (info->si_code == SI_ASYNCIO)
    155 			tt = info->si_value.sival_ptr;
    156 		if (tt) {
    157 			sc = tt->tt_sc;
    158 			spl_intr(IPL_BIO, softint_schedule, sc->sc_ih);
    159 			// spl_intr(IPL_BIO, ld_thunkbus_complete, sc);
    160 			// softint_schedule(sc->sc_ih);
    161 		}
    162 	}
    163 
    164 	curcpu()->ci_idepth--;
    165 }
    166 #endif
    167 
    168 static int
    169 ld_thunkbus_ldstart(struct ld_softc *ld, struct buf *bp)
    170 {
    171 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    172 	struct ld_thunkbus_transfer *tt = &sc->sc_tt;
    173 	int error;
    174 
    175 	tt->tt_sc = sc;
    176 	tt->tt_bp = bp;
    177 	memset(&tt->tt_aio, 0, sizeof(tt->tt_aio));
    178 	tt->tt_aio.aio_fildes = sc->sc_fd;
    179 	tt->tt_aio.aio_buf = bp->b_data;
    180 	tt->tt_aio.aio_nbytes = bp->b_bcount;
    181 	tt->tt_aio.aio_offset = bp->b_rawblkno * ld->sc_secsize;
    182 
    183 	tt->tt_aio.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
    184 	tt->tt_aio.aio_sigevent.sigev_signo = SIGIO;
    185 	tt->tt_aio.aio_sigevent.sigev_value.sival_ptr = tt;
    186 #ifdef LD_USE_AIO
    187 #if 0
    188 	device_printf(sc->sc_ld.sc_dv, "%s addr %p, off=%lld, count=%lld\n",
    189 	    (bp->b_flags & B_READ) ? "rd" : "wr",
    190 	    bp->b_data,
    191 	    (long long)bp->b_rawblkno,
    192 	    (long long)bp->b_bcount);
    193 #endif
    194 	if (sc->busy)
    195 		panic("%s: reentry", __func__);
    196 	sc->busy = true;
    197 
    198 	if (bp->b_flags & B_READ)
    199 		error = thunk_aio_read(&tt->tt_aio);
    200 	else
    201 		error = thunk_aio_write(&tt->tt_aio);
    202 #else
    203 	/* let the softint do the work */
    204 	spl_intr(IPL_BIO, softint_schedule, sc->sc_ih);
    205 	sc->busy = true;
    206 	error = 0;
    207 #endif
    208 	return error == -1 ? thunk_geterrno() : 0;
    209 }
    210 
    211 static void
    212 ld_thunkbus_complete(void *arg)
    213 {
    214 	struct ld_thunkbus_softc *sc = arg;
    215 	struct ld_thunkbus_transfer *tt = &sc->sc_tt;
    216 	struct buf *bp = tt->tt_bp;
    217 
    218 	if (!sc->busy)
    219 		panic("%s: but not busy?\n", __func__);
    220 
    221 #ifdef LD_USE_AIO
    222 	if (thunk_aio_error(&tt->tt_aio) == 0 &&
    223 	    thunk_aio_return(&tt->tt_aio) != -1) {
    224 		bp->b_resid = 0;
    225 	} else {
    226 		bp->b_error = thunk_geterrno();
    227 		bp->b_resid = bp->b_bcount;
    228 	}
    229 #else
    230 	size_t ret;
    231 	off_t offset = tt->tt_aio.aio_offset;
    232 
    233 	/* read/write the request */
    234 	if (bp->b_flags & B_READ)
    235 		ret = thunk_pread(sc->sc_fd, bp->b_data, bp->b_bcount, offset);
    236 	else
    237 		ret = thunk_pwrite(sc->sc_fd, bp->b_data, bp->b_bcount, offset);
    238 
    239 	/* setup return params */
    240 	if ((ret >= 0) && (ret == bp->b_bcount)) {
    241 		bp->b_resid = 0;
    242 	} else {
    243 		bp->b_error = thunk_geterrno();
    244 		bp->b_resid = bp->b_bcount;
    245 	}
    246 #endif
    247 
    248 	dprintf_debug("\tfin\n");
    249 	if (bp->b_error)
    250 		dprintf_debug("error!\n");
    251 
    252 	sc->busy = false;
    253 	lddone(&sc->sc_ld, bp);
    254 }
    255 
    256 static int
    257 ld_thunkbus_lddump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    258 {
    259 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    260 	ssize_t len;
    261 
    262 	len = thunk_pwrite(sc->sc_fd, data, blkcnt, blkno);
    263 	if (len == -1)
    264 		return thunk_geterrno();
    265 	else if (len != blkcnt) {
    266 		device_printf(ld->sc_dv, "%s failed (short xfer)\n", __func__);
    267 		return EIO;
    268 	}
    269 
    270 	return 0;
    271 }
    272 
    273 static int
    274 ld_thunkbus_ldflush(struct ld_softc *ld, int flags)
    275 {
    276 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    277 
    278 	if (thunk_fsync(sc->sc_fd) == -1)
    279 		return thunk_geterrno();
    280 
    281 	return 0;
    282 }
    283 
    284