Home | History | Annotate | Line # | Download | only in dev
      1  1.34       rin /* $NetBSD: ld_thunkbus.c,v 1.34 2025/04/13 02:34:02 rin 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.34       rin __KERNEL_RCSID(0, "$NetBSD: ld_thunkbus.c,v 1.34 2025/04/13 02:34:02 rin 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.32   reinoud #include <sys/bufq.h>
     38   1.1  jmcneill #include <sys/disk.h>
     39   1.2  jmcneill #include <sys/kmem.h>
     40   1.1  jmcneill 
     41   1.1  jmcneill #include <dev/ldvar.h>
     42   1.1  jmcneill 
     43   1.1  jmcneill #include <machine/mainbus.h>
     44   1.1  jmcneill #include <machine/thunk.h>
     45  1.13   reinoud #include <machine/intr.h>
     46   1.1  jmcneill 
     47   1.1  jmcneill static int	ld_thunkbus_match(device_t, cfdata_t, void *);
     48   1.1  jmcneill static void	ld_thunkbus_attach(device_t, device_t, void *);
     49   1.1  jmcneill 
     50   1.1  jmcneill static int	ld_thunkbus_ldstart(struct ld_softc *, struct buf *);
     51  1.34       rin static int	ld_thunkbus_lddump(struct ld_softc *, void *, daddr_t, int);
     52  1.32   reinoud static int	ld_thunkbus_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
     53   1.1  jmcneill 
     54  1.29   reinoud //#define LD_USE_AIO
     55  1.29   reinoud 
     56  1.29   reinoud #ifdef LD_USE_AIO
     57  1.29   reinoud static int	ld_aio_sig(void *);
     58  1.29   reinoud #endif
     59  1.17   reinoud static void	ld_thunkbus_complete(void *arg);
     60   1.3  jmcneill 
     61   1.4  jmcneill struct ld_thunkbus_softc;
     62   1.4  jmcneill 
     63   1.4  jmcneill struct ld_thunkbus_transfer {
     64   1.4  jmcneill 	struct ld_thunkbus_softc *tt_sc;
     65  1.29   reinoud 	struct aiocb	tt_aio;
     66   1.4  jmcneill 	struct buf	*tt_bp;
     67   1.4  jmcneill };
     68   1.2  jmcneill 
     69   1.1  jmcneill struct ld_thunkbus_softc {
     70   1.1  jmcneill 	struct ld_softc	sc_ld;
     71   1.1  jmcneill 
     72   1.1  jmcneill 	int		sc_fd;
     73   1.3  jmcneill 	void		*sc_ih;
     74  1.29   reinoud 	void		*sc_aio_ih;
     75   1.3  jmcneill 
     76   1.4  jmcneill 	struct ld_thunkbus_transfer sc_tt;
     77  1.15   reinoud 	bool		busy;
     78   1.2  jmcneill };
     79   1.2  jmcneill 
     80   1.1  jmcneill CFATTACH_DECL_NEW(ld_thunkbus, sizeof(struct ld_thunkbus_softc),
     81   1.1  jmcneill     ld_thunkbus_match, ld_thunkbus_attach, NULL, NULL);
     82   1.1  jmcneill 
     83   1.1  jmcneill static int
     84   1.1  jmcneill ld_thunkbus_match(device_t parent, cfdata_t match, void *opaque)
     85   1.1  jmcneill {
     86   1.1  jmcneill 	struct thunkbus_attach_args *taa = opaque;
     87   1.1  jmcneill 
     88   1.1  jmcneill 	if (taa->taa_type != THUNKBUS_TYPE_DISKIMAGE)
     89   1.1  jmcneill 		return 0;
     90   1.1  jmcneill 
     91   1.1  jmcneill 	return 1;
     92   1.1  jmcneill }
     93   1.1  jmcneill 
     94   1.1  jmcneill static void
     95   1.1  jmcneill ld_thunkbus_attach(device_t parent, device_t self, void *opaque)
     96   1.1  jmcneill {
     97   1.1  jmcneill 	struct ld_thunkbus_softc *sc = device_private(self);
     98   1.1  jmcneill 	struct ld_softc *ld = &sc->sc_ld;
     99   1.1  jmcneill 	struct thunkbus_attach_args *taa = opaque;
    100   1.1  jmcneill 	const char *path = taa->u.diskimage.path;
    101  1.26  jmcneill 	ssize_t blksize;
    102  1.26  jmcneill 	off_t size;
    103   1.1  jmcneill 
    104   1.1  jmcneill 	ld->sc_dv = self;
    105   1.1  jmcneill 
    106   1.1  jmcneill 	sc->sc_fd = thunk_open(path, O_RDWR, 0);
    107   1.1  jmcneill 	if (sc->sc_fd == -1) {
    108   1.7  jmcneill 		aprint_error(": couldn't open %s: %d\n", path, thunk_geterrno());
    109   1.1  jmcneill 		return;
    110   1.1  jmcneill 	}
    111   1.5  jmcneill 	if (thunk_fstat_getsize(sc->sc_fd, &size, &blksize) == -1) {
    112   1.7  jmcneill 		aprint_error(": couldn't stat %s: %d\n", path, thunk_geterrno());
    113   1.1  jmcneill 		return;
    114   1.1  jmcneill 	}
    115   1.1  jmcneill 
    116   1.1  jmcneill 	aprint_naive("\n");
    117   1.5  jmcneill 	aprint_normal(": %s (%lld)\n", path, (long long)size);
    118   1.1  jmcneill 
    119   1.1  jmcneill 	ld->sc_flags = LDF_ENABLED;
    120  1.24  jmcneill 	ld->sc_maxxfer = MAXPHYS;
    121   1.2  jmcneill 	ld->sc_secsize = 512;
    122   1.5  jmcneill 	ld->sc_secperunit = size / ld->sc_secsize;
    123   1.1  jmcneill 	ld->sc_maxqueuecnt = 1;
    124   1.1  jmcneill 	ld->sc_start = ld_thunkbus_ldstart;
    125   1.1  jmcneill 	ld->sc_dump = ld_thunkbus_lddump;
    126  1.32   reinoud 	ld->sc_ioctl = ld_thunkbus_ioctl;
    127   1.1  jmcneill 
    128   1.3  jmcneill 	sc->sc_ih = softint_establish(SOFTINT_BIO,
    129  1.21  jmcneill 	    ld_thunkbus_complete, ld);
    130   1.2  jmcneill 
    131  1.29   reinoud #ifdef LD_USE_AIO
    132  1.29   reinoud 	sc->sc_aio_ih = sigio_intr_establish(ld_aio_sig, sc);
    133  1.29   reinoud 	if (sc->sc_aio_ih == NULL)
    134  1.29   reinoud 		panic("couldn't establish aio sig interrupt");
    135  1.29   reinoud #endif
    136  1.29   reinoud 
    137  1.15   reinoud 	sc->busy = false;
    138  1.15   reinoud 
    139  1.31   reinoud 	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
    140   1.1  jmcneill }
    141   1.1  jmcneill 
    142  1.29   reinoud #ifdef LD_USE_AIO
    143  1.29   reinoud static int
    144  1.29   reinoud ld_aio_sig(void *arg)
    145  1.29   reinoud {
    146  1.29   reinoud 	struct ld_softc *ld = arg;
    147  1.29   reinoud 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    148  1.29   reinoud 
    149  1.30   reinoud 	softint_schedule(sc->sc_ih);
    150  1.29   reinoud 
    151  1.29   reinoud 	return 0;
    152  1.29   reinoud }
    153  1.29   reinoud 
    154  1.29   reinoud static int
    155  1.29   reinoud ld_thunkbus_ldstart(struct ld_softc *ld, struct buf *bp)
    156  1.29   reinoud {
    157  1.29   reinoud 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    158  1.29   reinoud 	struct ld_thunkbus_transfer *tt = &sc->sc_tt;
    159  1.29   reinoud 	off_t offset = bp->b_rawblkno * ld->sc_secsize;
    160  1.29   reinoud 	off_t disksize = ld->sc_secsize * ld->sc_secperunit;
    161  1.29   reinoud 	int error;
    162  1.29   reinoud 
    163  1.29   reinoud 	tt->tt_sc = sc;
    164  1.29   reinoud 	tt->tt_bp = bp;
    165  1.29   reinoud 
    166  1.29   reinoud 	memset(&tt->tt_aio, 0, sizeof(tt->tt_aio));
    167  1.29   reinoud 	tt->tt_aio.aio_fildes = sc->sc_fd;
    168  1.29   reinoud 	tt->tt_aio.aio_buf = bp->b_data;
    169  1.29   reinoud 	tt->tt_aio.aio_nbytes = bp->b_bcount;
    170  1.29   reinoud 	tt->tt_aio.aio_offset = offset;
    171  1.29   reinoud 
    172  1.29   reinoud 	tt->tt_aio.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
    173  1.29   reinoud 	tt->tt_aio.aio_sigevent.sigev_signo = SIGIO;
    174  1.29   reinoud 	tt->tt_aio.aio_sigevent.sigev_value.sival_ptr = tt;
    175  1.29   reinoud #if 0
    176  1.29   reinoud 	device_printf(sc->sc_ld.sc_dv, "%s addr %p, off=%lld, count=%lld\n",
    177  1.29   reinoud 	    (bp->b_flags & B_READ) ? "rd" : "wr",
    178  1.29   reinoud 	    bp->b_data,
    179  1.29   reinoud 	    (long long)bp->b_rawblkno,
    180  1.29   reinoud 	    (long long)bp->b_bcount);
    181  1.29   reinoud #endif
    182  1.29   reinoud 	if (sc->busy)
    183  1.29   reinoud 		panic("%s: reentry", __func__);
    184  1.29   reinoud 
    185  1.29   reinoud 	if ((offset < 0) || (offset + bp->b_bcount > disksize)) {
    186  1.29   reinoud 		error = EIO;
    187  1.29   reinoud 		bp->b_error = error;
    188  1.29   reinoud 		bp->b_resid = bp->b_bcount;
    189  1.29   reinoud 	} else {
    190  1.29   reinoud 		sc->busy = true;
    191  1.29   reinoud 		if (bp->b_flags & B_READ)
    192  1.29   reinoud 			error = thunk_aio_read(&tt->tt_aio);
    193  1.29   reinoud 		else
    194  1.29   reinoud 			error = thunk_aio_write(&tt->tt_aio);
    195  1.29   reinoud 	}
    196  1.29   reinoud 	return error;
    197  1.29   reinoud }
    198  1.29   reinoud 
    199  1.29   reinoud static void
    200  1.29   reinoud ld_thunkbus_complete(void *arg)
    201  1.29   reinoud {
    202  1.29   reinoud 	struct ld_softc *ld = arg;
    203  1.29   reinoud 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    204  1.29   reinoud 	struct ld_thunkbus_transfer *tt = &sc->sc_tt;
    205  1.29   reinoud 	struct buf *bp = tt->tt_bp;
    206  1.29   reinoud 	int error;
    207  1.29   reinoud 
    208  1.29   reinoud 	/*
    209  1.29   reinoud 	 * check if our aio has finished, we could be called for whatever
    210  1.29   reinoud 	 * reason, for whatever SIGIO since signals can be missed.
    211  1.29   reinoud 	 */
    212  1.29   reinoud 	if (!sc->busy)
    213  1.29   reinoud 		return;
    214  1.29   reinoud 
    215  1.29   reinoud 	/* check if it was OUR sigio */
    216  1.29   reinoud 	error = thunk_aio_error(&tt->tt_aio);
    217  1.29   reinoud 	if (error == EINPROGRESS)
    218  1.29   reinoud 		return;
    219  1.29   reinoud 
    220  1.29   reinoud 	/* use the result */
    221  1.29   reinoud 	if ((error == 0) &&
    222  1.29   reinoud 	    thunk_aio_return(&tt->tt_aio) != -1) {
    223  1.29   reinoud 		bp->b_resid = 0;
    224  1.29   reinoud 	} else {
    225  1.29   reinoud 		bp->b_error = error;
    226  1.29   reinoud 		bp->b_resid = bp->b_bcount;
    227  1.29   reinoud 	}
    228  1.29   reinoud 
    229  1.29   reinoud 	thunk_printf_debug("\tfin\n");
    230  1.29   reinoud 	if (bp->b_error)
    231  1.29   reinoud 		thunk_printf_debug("error!\n");
    232  1.29   reinoud 
    233  1.29   reinoud 	sc->busy = false;
    234  1.29   reinoud 	lddone(&sc->sc_ld, bp);
    235  1.29   reinoud }
    236  1.29   reinoud 
    237  1.29   reinoud 
    238  1.29   reinoud #else /* LD_USE_AIO */
    239  1.29   reinoud 
    240  1.29   reinoud 
    241   1.1  jmcneill static int
    242   1.1  jmcneill ld_thunkbus_ldstart(struct ld_softc *ld, struct buf *bp)
    243   1.1  jmcneill {
    244   1.1  jmcneill 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    245   1.4  jmcneill 	struct ld_thunkbus_transfer *tt = &sc->sc_tt;
    246   1.2  jmcneill 
    247   1.2  jmcneill 	tt->tt_sc = sc;
    248   1.2  jmcneill 	tt->tt_bp = bp;
    249  1.15   reinoud 
    250  1.17   reinoud 	/* let the softint do the work */
    251  1.21  jmcneill 	sc->busy = true;
    252  1.30   reinoud 	softint_schedule(sc->sc_ih);
    253  1.21  jmcneill 
    254  1.23   reinoud 	return 0;
    255  1.17   reinoud }
    256  1.17   reinoud 
    257  1.17   reinoud static void
    258  1.17   reinoud ld_thunkbus_complete(void *arg)
    259  1.17   reinoud {
    260  1.21  jmcneill 	struct ld_softc *ld = arg;
    261  1.21  jmcneill 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    262  1.17   reinoud 	struct ld_thunkbus_transfer *tt = &sc->sc_tt;
    263  1.17   reinoud 	struct buf *bp = tt->tt_bp;
    264  1.31   reinoud 	off_t offset = (off_t) bp->b_rawblkno * ld->sc_secsize;
    265  1.28   reinoud 	int64_t ret;
    266  1.17   reinoud 
    267  1.17   reinoud 	if (!sc->busy)
    268  1.17   reinoud 		panic("%s: but not busy?\n", __func__);
    269  1.17   reinoud 
    270  1.27   reinoud 	//printf("%s: %s %u @ %lld -> %p (flags 0x%08x)\n", __func__,
    271  1.27   reinoud 	//    bp->b_flags & B_READ ? "read" : "write",
    272  1.21  jmcneill 	//    (unsigned int)bp->b_bcount, (long long)offset, bp->b_data, bp->b_flags);
    273  1.17   reinoud 
    274  1.33   reinoud 	/* this is silly, but better make sure */
    275  1.33   reinoud 	thunk_assert_presence((vaddr_t) bp->b_data, (size_t) bp->b_bcount);
    276  1.33   reinoud 
    277  1.17   reinoud 	/* read/write the request */
    278  1.21  jmcneill 	if (bp->b_flags & B_READ) {
    279  1.22   reinoud 		ret = thunk_pread(sc->sc_fd, bp->b_data, bp->b_bcount, offset);
    280  1.21  jmcneill 	} else {
    281  1.22   reinoud 		ret = thunk_pwrite(sc->sc_fd, bp->b_data, bp->b_bcount, offset);
    282  1.21  jmcneill 	}
    283  1.21  jmcneill 
    284  1.21  jmcneill 	//if (ret == -1)
    285  1.27   reinoud 	//	printf("%s: errno = %d\n", __func__, thunk_geterrno());
    286  1.17   reinoud 
    287  1.17   reinoud 	/* setup return params */
    288  1.17   reinoud 	if ((ret >= 0) && (ret == bp->b_bcount)) {
    289  1.17   reinoud 		bp->b_resid = 0;
    290  1.17   reinoud 	} else {
    291  1.33   reinoud 		// printf("ret = %d, errno %d?\n",(int) ret, thunk_geterrno());
    292  1.17   reinoud 		bp->b_error = thunk_geterrno();
    293  1.17   reinoud 		bp->b_resid = bp->b_bcount;
    294  1.17   reinoud 	}
    295  1.25   reinoud 	thunk_printf_debug("\tfin\n");
    296  1.17   reinoud 	if (bp->b_error)
    297  1.25   reinoud 		thunk_printf_debug("error!\n");
    298  1.17   reinoud 
    299  1.17   reinoud 	sc->busy = false;
    300  1.17   reinoud 	lddone(&sc->sc_ld, bp);
    301   1.1  jmcneill }
    302   1.1  jmcneill 
    303  1.29   reinoud #endif /* LD_USE_AIO */
    304  1.29   reinoud 
    305  1.29   reinoud 
    306   1.1  jmcneill static int
    307  1.34       rin ld_thunkbus_lddump(struct ld_softc *ld, void *data, daddr_t blkno, int blkcnt)
    308   1.1  jmcneill {
    309   1.1  jmcneill 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    310   1.1  jmcneill 	ssize_t len;
    311   1.1  jmcneill 
    312  1.21  jmcneill 	len = thunk_pwrite(sc->sc_fd, data, blkcnt, blkno * ld->sc_secsize);
    313   1.1  jmcneill 	if (len == -1)
    314   1.7  jmcneill 		return thunk_geterrno();
    315   1.1  jmcneill 	else if (len != blkcnt) {
    316   1.1  jmcneill 		device_printf(ld->sc_dv, "%s failed (short xfer)\n", __func__);
    317   1.1  jmcneill 		return EIO;
    318   1.1  jmcneill 	}
    319   1.1  jmcneill 
    320   1.1  jmcneill 	return 0;
    321   1.1  jmcneill }
    322   1.1  jmcneill 
    323  1.32   reinoud 
    324   1.1  jmcneill static int
    325  1.32   reinoud ld_thunkbus_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag,
    326  1.32   reinoud     bool poll)
    327   1.1  jmcneill {
    328   1.1  jmcneill 	struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
    329   1.1  jmcneill 
    330  1.32   reinoud 	switch (cmd) {
    331  1.32   reinoud 	case DIOCCACHESYNC:
    332  1.32   reinoud 		if (thunk_fsync(sc->sc_fd) == -1)
    333  1.32   reinoud 			return thunk_geterrno();
    334  1.32   reinoud 		return 0;
    335  1.32   reinoud 	default:
    336  1.32   reinoud 		return EPASSTHROUGH;
    337  1.32   reinoud 	}
    338   1.1  jmcneill }
    339