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