Home | History | Annotate | Line # | Download | only in dev
ld_thunkbus.c revision 1.2
      1  1.2  jmcneill /* $NetBSD: ld_thunkbus.c,v 1.2 2011/08/13 10:33:52 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2011 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: ld_thunkbus.c,v 1.2 2011/08/13 10:33:52 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/proc.h>
     34  1.1  jmcneill #include <sys/systm.h>
     35  1.1  jmcneill #include <sys/device.h>
     36  1.1  jmcneill #include <sys/buf.h>
     37  1.1  jmcneill #include <sys/disk.h>
     38  1.2  jmcneill #include <sys/kmem.h>
     39  1.1  jmcneill 
     40  1.1  jmcneill #include <dev/ldvar.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #include <machine/mainbus.h>
     43  1.1  jmcneill #include <machine/thunk.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill static int	ld_thunkbus_match(device_t, cfdata_t, void *);
     46  1.1  jmcneill static void	ld_thunkbus_attach(device_t, device_t, void *);
     47  1.1  jmcneill 
     48  1.1  jmcneill static int	ld_thunkbus_ldstart(struct ld_softc *, struct buf *);
     49  1.1  jmcneill static int	ld_thunkbus_lddump(struct ld_softc *, void *, int, int);
     50  1.1  jmcneill static int	ld_thunkbus_ldflush(struct ld_softc *, int);
     51  1.1  jmcneill 
     52  1.2  jmcneill static void	ld_thunkbus_complete(int, siginfo_t *, void *);
     53  1.2  jmcneill 
     54  1.1  jmcneill struct ld_thunkbus_softc {
     55  1.1  jmcneill 	struct ld_softc	sc_ld;
     56  1.1  jmcneill 
     57  1.1  jmcneill 	int		sc_fd;
     58  1.1  jmcneill 	struct stat	sc_st;
     59  1.1  jmcneill };
     60  1.1  jmcneill 
     61  1.2  jmcneill struct ld_thunkbus_transfer {
     62  1.2  jmcneill 	struct ld_thunkbus_softc *tt_sc;
     63  1.2  jmcneill 	struct aiocb	tt_aio;
     64  1.2  jmcneill 	struct buf	*tt_bp;
     65  1.2  jmcneill };
     66  1.2  jmcneill 
     67  1.1  jmcneill CFATTACH_DECL_NEW(ld_thunkbus, sizeof(struct ld_thunkbus_softc),
     68  1.1  jmcneill     ld_thunkbus_match, ld_thunkbus_attach, NULL, NULL);
     69  1.1  jmcneill 
     70  1.1  jmcneill extern int errno;
     71  1.1  jmcneill 
     72  1.1  jmcneill static int
     73  1.1  jmcneill ld_thunkbus_match(device_t parent, cfdata_t match, void *opaque)
     74  1.1  jmcneill {
     75  1.1  jmcneill 	struct thunkbus_attach_args *taa = opaque;
     76  1.1  jmcneill 
     77  1.1  jmcneill 	if (taa->taa_type != THUNKBUS_TYPE_DISKIMAGE)
     78  1.1  jmcneill 		return 0;
     79  1.1  jmcneill 
     80  1.1  jmcneill 	return 1;
     81  1.1  jmcneill }
     82  1.1  jmcneill 
     83  1.1  jmcneill static void
     84  1.1  jmcneill ld_thunkbus_attach(device_t parent, device_t self, void *opaque)
     85  1.1  jmcneill {
     86  1.1  jmcneill 	struct ld_thunkbus_softc *sc = device_private(self);
     87  1.1  jmcneill 	struct ld_softc *ld = &sc->sc_ld;
     88  1.1  jmcneill 	struct thunkbus_attach_args *taa = opaque;
     89  1.1  jmcneill 	const char *path = taa->u.diskimage.path;
     90  1.2  jmcneill 	struct sigaction sa;
     91  1.1  jmcneill 
     92  1.1  jmcneill 	ld->sc_dv = self;
     93  1.1  jmcneill 
     94  1.1  jmcneill 	sc->sc_fd = thunk_open(path, O_RDWR, 0);
     95  1.1  jmcneill 	if (sc->sc_fd == -1) {
     96  1.1  jmcneill 		aprint_error(": couldn't open %s: %d\n", path, errno);
     97  1.1  jmcneill 		return;
     98  1.1  jmcneill 	}
     99  1.1  jmcneill 	if (thunk_fstat(sc->sc_fd, &sc->sc_st) == -1) {
    100  1.1  jmcneill 		aprint_error(": couldn't stat %s: %d\n", path, errno);
    101  1.1  jmcneill 		return;
    102  1.1  jmcneill 	}
    103  1.1  jmcneill 
    104  1.1  jmcneill 	aprint_naive("\n");
    105  1.1  jmcneill 	aprint_normal(": %s (%lld)\n", path, (long long)sc->sc_st.st_size);
    106  1.1  jmcneill 
    107  1.1  jmcneill 	ld->sc_flags = LDF_ENABLED;
    108  1.1  jmcneill 	ld->sc_maxxfer = sc->sc_st.st_blksize;
    109  1.2  jmcneill 	ld->sc_secsize = 512;
    110  1.2  jmcneill 	ld->sc_secperunit = sc->sc_st.st_size / 512;
    111  1.1  jmcneill 	ld->sc_maxqueuecnt = 1;
    112  1.1  jmcneill 	ld->sc_start = ld_thunkbus_ldstart;
    113  1.1  jmcneill 	ld->sc_dump = ld_thunkbus_lddump;
    114  1.1  jmcneill 	ld->sc_flush = ld_thunkbus_ldflush;
    115  1.1  jmcneill 
    116  1.2  jmcneill 	sigemptyset(&sa.sa_mask);
    117  1.2  jmcneill 	sa.sa_flags = SA_RESTART|SA_SIGINFO;
    118  1.2  jmcneill 	sa.sa_sigaction = ld_thunkbus_complete;
    119  1.2  jmcneill 	if (thunk_sigaction(SIGIO, &sa, NULL) == -1)
    120  1.2  jmcneill 		panic("couldn't register SIGIO handler: %d", errno);
    121  1.2  jmcneill 
    122  1.1  jmcneill 	ldattach(ld);
    123  1.1  jmcneill }
    124  1.1  jmcneill 
    125  1.2  jmcneill static void
    126  1.2  jmcneill ld_thunkbus_complete(int sig, siginfo_t *info, void *ctx)
    127  1.2  jmcneill {
    128  1.2  jmcneill 	struct ld_thunkbus_transfer *tt;
    129  1.2  jmcneill 	struct ld_thunkbus_softc *sc;
    130  1.2  jmcneill 	struct buf *bp;
    131  1.2  jmcneill 
    132  1.2  jmcneill 	curcpu()->ci_idepth++;
    133  1.2  jmcneill 
    134  1.2  jmcneill 	if (info->si_signo == SIGIO) {
    135  1.2  jmcneill 		tt = info->si_value.sival_ptr;
    136  1.2  jmcneill 		sc = tt->tt_sc;
    137  1.2  jmcneill 		bp = tt->tt_bp;
    138  1.2  jmcneill 		if (thunk_aio_error(&tt->tt_aio) == 0 &&
    139  1.2  jmcneill 		    thunk_aio_return(&tt->tt_aio) != -1) {
    140  1.2  jmcneill 			bp->b_resid = 0;
    141  1.2  jmcneill 		} else {
    142  1.2  jmcneill 			bp->b_error = errno;
    143  1.2  jmcneill 			bp->b_resid = bp->b_bcount;
    144  1.2  jmcneill 		}
    145  1.2  jmcneill 
    146  1.2  jmcneill 		kmem_free(tt, sizeof(*tt));
    147  1.2  jmcneill 
    148  1.2  jmcneill 		if (bp->b_error)
    149  1.2  jmcneill 			printf("errpr!\n");
    150  1.2  jmcneill 
    151  1.2  jmcneill 		lddone(&sc->sc_ld, bp);
    152  1.2  jmcneill 	}
    153  1.2  jmcneill 
    154  1.2  jmcneill 	curcpu()->ci_idepth--;
    155  1.2  jmcneill }
    156  1.2  jmcneill 
    157  1.1  jmcneill static int
    158  1.1  jmcneill ld_thunkbus_ldstart(struct ld_softc *ld, struct buf *bp)
    159  1.1  jmcneill {
    160  1.1  jmcneill 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    161  1.2  jmcneill 	struct ld_thunkbus_transfer *tt;
    162  1.2  jmcneill 	int error;
    163  1.2  jmcneill 
    164  1.2  jmcneill 	tt = kmem_alloc(sizeof(*tt), KM_SLEEP);
    165  1.2  jmcneill 	if (tt == NULL)
    166  1.2  jmcneill 		return ENOMEM;
    167  1.2  jmcneill 
    168  1.2  jmcneill 	tt->tt_sc = sc;
    169  1.2  jmcneill 	tt->tt_bp = bp;
    170  1.2  jmcneill 	memset(&tt->tt_aio, 0, sizeof(tt->tt_aio));
    171  1.2  jmcneill 	tt->tt_aio.aio_fildes = sc->sc_fd;
    172  1.2  jmcneill 	tt->tt_aio.aio_buf = bp->b_data;
    173  1.2  jmcneill 	tt->tt_aio.aio_nbytes = bp->b_bcount;
    174  1.2  jmcneill 	tt->tt_aio.aio_offset = bp->b_rawblkno * ld->sc_secsize;
    175  1.2  jmcneill 
    176  1.2  jmcneill 	tt->tt_aio.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
    177  1.2  jmcneill 	tt->tt_aio.aio_sigevent.sigev_signo = SIGIO;
    178  1.2  jmcneill 	tt->tt_aio.aio_sigevent.sigev_value.sival_ptr = tt;
    179  1.2  jmcneill 
    180  1.2  jmcneill #if 0
    181  1.2  jmcneill 	device_printf(sc->sc_ld.sc_dv, "%s off=%lld count=%lld\n",
    182  1.2  jmcneill 	    (bp->b_flags & B_READ) ? "rd" : "wr",
    183  1.2  jmcneill 	    (long long)bp->b_rawblkno,
    184  1.2  jmcneill 	    (long long)bp->b_bcount);
    185  1.2  jmcneill #endif
    186  1.1  jmcneill 
    187  1.1  jmcneill 	if (bp->b_flags & B_READ)
    188  1.2  jmcneill 		error = thunk_aio_read(&tt->tt_aio);
    189  1.1  jmcneill 	else
    190  1.2  jmcneill 		error = thunk_aio_write(&tt->tt_aio);
    191  1.1  jmcneill 
    192  1.2  jmcneill 	return error == -1 ? errno : 0;
    193  1.1  jmcneill }
    194  1.1  jmcneill 
    195  1.1  jmcneill static int
    196  1.1  jmcneill ld_thunkbus_lddump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    197  1.1  jmcneill {
    198  1.1  jmcneill 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    199  1.1  jmcneill 	ssize_t len;
    200  1.1  jmcneill 
    201  1.1  jmcneill 	len = thunk_pwrite(sc->sc_fd, data, blkcnt, blkno);
    202  1.1  jmcneill 	if (len == -1)
    203  1.1  jmcneill 		return errno;
    204  1.1  jmcneill 	else if (len != blkcnt) {
    205  1.1  jmcneill 		device_printf(ld->sc_dv, "%s failed (short xfer)\n", __func__);
    206  1.1  jmcneill 		return EIO;
    207  1.1  jmcneill 	}
    208  1.1  jmcneill 
    209  1.1  jmcneill 	return 0;
    210  1.1  jmcneill }
    211  1.1  jmcneill 
    212  1.1  jmcneill static int
    213  1.1  jmcneill ld_thunkbus_ldflush(struct ld_softc *ld, int flags)
    214  1.1  jmcneill {
    215  1.1  jmcneill 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    216  1.1  jmcneill 
    217  1.1  jmcneill 	if (thunk_fsync(sc->sc_fd) == -1)
    218  1.1  jmcneill 		return errno;
    219  1.1  jmcneill 
    220  1.1  jmcneill 	return 0;
    221  1.1  jmcneill }
    222  1.1  jmcneill 
    223