Home | History | Annotate | Line # | Download | only in dev
ld_thunkbus.c revision 1.12
      1 /* $NetBSD: ld_thunkbus.c,v 1.12 2011/09/05 18:17:08 jmcneill 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.12 2011/09/05 18:17:08 jmcneill 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 
     45 static int	ld_thunkbus_match(device_t, cfdata_t, void *);
     46 static void	ld_thunkbus_attach(device_t, device_t, void *);
     47 
     48 static int	ld_thunkbus_ldstart(struct ld_softc *, struct buf *);
     49 static int	ld_thunkbus_lddump(struct ld_softc *, void *, int, int);
     50 static int	ld_thunkbus_ldflush(struct ld_softc *, int);
     51 
     52 static void	ld_thunkbus_sig(int, siginfo_t *, void *);
     53 static void	ld_thunkbus_complete(void *);
     54 
     55 struct ld_thunkbus_softc;
     56 
     57 struct ld_thunkbus_transfer {
     58 	struct ld_thunkbus_softc *tt_sc;
     59 	struct aiocb	tt_aio;
     60 	struct buf	*tt_bp;
     61 };
     62 
     63 struct ld_thunkbus_softc {
     64 	struct ld_softc	sc_ld;
     65 
     66 	int		sc_fd;
     67 	void		*sc_ih;
     68 
     69 	struct ld_thunkbus_transfer sc_tt;
     70 };
     71 
     72 CFATTACH_DECL_NEW(ld_thunkbus, sizeof(struct ld_thunkbus_softc),
     73     ld_thunkbus_match, ld_thunkbus_attach, NULL, NULL);
     74 
     75 static int
     76 ld_thunkbus_match(device_t parent, cfdata_t match, void *opaque)
     77 {
     78 	struct thunkbus_attach_args *taa = opaque;
     79 
     80 	if (taa->taa_type != THUNKBUS_TYPE_DISKIMAGE)
     81 		return 0;
     82 
     83 	return 1;
     84 }
     85 
     86 static void
     87 ld_thunkbus_attach(device_t parent, device_t self, void *opaque)
     88 {
     89 	struct ld_thunkbus_softc *sc = device_private(self);
     90 	struct ld_softc *ld = &sc->sc_ld;
     91 	struct thunkbus_attach_args *taa = opaque;
     92 	const char *path = taa->u.diskimage.path;
     93 	struct sigaction sa;
     94 	stack_t ss;
     95 	ssize_t size, blksize;
     96 
     97 	ld->sc_dv = self;
     98 
     99 	sc->sc_fd = thunk_open(path, O_RDWR, 0);
    100 	if (sc->sc_fd == -1) {
    101 		aprint_error(": couldn't open %s: %d\n", path, thunk_geterrno());
    102 		return;
    103 	}
    104 	if (thunk_fstat_getsize(sc->sc_fd, &size, &blksize) == -1) {
    105 		aprint_error(": couldn't stat %s: %d\n", path, thunk_geterrno());
    106 		return;
    107 	}
    108 
    109 	aprint_naive("\n");
    110 	aprint_normal(": %s (%lld)\n", path, (long long)size);
    111 
    112 	ld->sc_flags = LDF_ENABLED;
    113 	ld->sc_maxxfer = blksize;
    114 	ld->sc_secsize = 512;
    115 	ld->sc_secperunit = size / ld->sc_secsize;
    116 	ld->sc_maxqueuecnt = 1;
    117 	ld->sc_start = ld_thunkbus_ldstart;
    118 	ld->sc_dump = ld_thunkbus_lddump;
    119 	ld->sc_flush = ld_thunkbus_ldflush;
    120 
    121 	sc->sc_ih = softint_establish(SOFTINT_BIO,
    122 	    ld_thunkbus_complete, sc);
    123 
    124 	ss.ss_sp = thunk_malloc(SIGSTKSZ);
    125 	if (ss.ss_sp == NULL)
    126 		panic("%s: couldn't allocate signal stack", __func__);
    127 	ss.ss_size = SIGSTKSZ;
    128 	ss.ss_flags = 0;
    129 	if (thunk_sigaltstack(&ss, NULL) == -1)
    130 		panic("%s: couldn't setup signal stack", __func__);
    131 
    132 	sigemptyset(&sa.sa_mask);
    133 	sa.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
    134 	sa.sa_sigaction = ld_thunkbus_sig;
    135 	thunk_sigaddset(&sa.sa_mask, SIGALRM);
    136 	if (thunk_sigaction(SIGIO, &sa, NULL) == -1)
    137 		panic("couldn't register SIGIO handler: %d", thunk_geterrno());
    138 
    139 	ldattach(ld);
    140 }
    141 
    142 static void
    143 ld_thunkbus_sig(int sig, siginfo_t *info, void *ctx)
    144 {
    145 	struct ld_thunkbus_transfer *tt = NULL;
    146 	struct ld_thunkbus_softc *sc;
    147 
    148 	curcpu()->ci_idepth++;
    149 
    150 	if (info->si_signo == SIGIO) {
    151 		if (info->si_code == SI_ASYNCIO)
    152 			tt = info->si_value.sival_ptr;
    153 		if (tt) {
    154 			sc = tt->tt_sc;
    155 			softint_schedule(sc->sc_ih);
    156 		}
    157 	}
    158 
    159 	curcpu()->ci_idepth--;
    160 }
    161 
    162 static void
    163 ld_thunkbus_complete(void *arg)
    164 {
    165 	struct ld_thunkbus_softc *sc = arg;
    166 	struct ld_thunkbus_transfer *tt = &sc->sc_tt;
    167 	struct buf *bp = tt->tt_bp;
    168 
    169 	if (thunk_aio_error(&tt->tt_aio) == 0 &&
    170 	    thunk_aio_return(&tt->tt_aio) != -1) {
    171 		bp->b_resid = 0;
    172 	} else {
    173 		bp->b_error = thunk_geterrno();
    174 		bp->b_resid = bp->b_bcount;
    175 	}
    176 
    177 	if (bp->b_error)
    178 		printf("error!\n");
    179 
    180 	lddone(&sc->sc_ld, bp);
    181 }
    182 
    183 static int
    184 ld_thunkbus_ldstart(struct ld_softc *ld, struct buf *bp)
    185 {
    186 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    187 	struct ld_thunkbus_transfer *tt = &sc->sc_tt;
    188 	int error;
    189 
    190 	tt->tt_sc = sc;
    191 	tt->tt_bp = bp;
    192 	memset(&tt->tt_aio, 0, sizeof(tt->tt_aio));
    193 	tt->tt_aio.aio_fildes = sc->sc_fd;
    194 	tt->tt_aio.aio_buf = bp->b_data;
    195 	tt->tt_aio.aio_nbytes = bp->b_bcount;
    196 	tt->tt_aio.aio_offset = bp->b_rawblkno * ld->sc_secsize;
    197 
    198 	tt->tt_aio.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
    199 	tt->tt_aio.aio_sigevent.sigev_signo = SIGIO;
    200 	tt->tt_aio.aio_sigevent.sigev_value.sival_ptr = tt;
    201 
    202 #if 0
    203 	device_printf(sc->sc_ld.sc_dv, "%s addr %p, off=%lld, count=%lld\n",
    204 	    (bp->b_flags & B_READ) ? "rd" : "wr",
    205 	    bp->b_data,
    206 	    (long long)bp->b_rawblkno,
    207 	    (long long)bp->b_bcount);
    208 #endif
    209 
    210 	if (bp->b_flags & B_READ)
    211 		error = thunk_aio_read(&tt->tt_aio);
    212 	else
    213 		error = thunk_aio_write(&tt->tt_aio);
    214 
    215 	return error == -1 ? thunk_geterrno() : 0;
    216 }
    217 
    218 static int
    219 ld_thunkbus_lddump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    220 {
    221 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    222 	ssize_t len;
    223 
    224 	len = thunk_pwrite(sc->sc_fd, data, blkcnt, blkno);
    225 	if (len == -1)
    226 		return thunk_geterrno();
    227 	else if (len != blkcnt) {
    228 		device_printf(ld->sc_dv, "%s failed (short xfer)\n", __func__);
    229 		return EIO;
    230 	}
    231 
    232 	return 0;
    233 }
    234 
    235 static int
    236 ld_thunkbus_ldflush(struct ld_softc *ld, int flags)
    237 {
    238 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    239 
    240 	if (thunk_fsync(sc->sc_fd) == -1)
    241 		return thunk_geterrno();
    242 
    243 	return 0;
    244 }
    245 
    246