ld_thunkbus.c revision 1.1 1 1.1 jmcneill /* $NetBSD: ld_thunkbus.c,v 1.1 2011/08/12 12:59:13 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.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: ld_thunkbus.c,v 1.1 2011/08/12 12:59:13 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.1 jmcneill
39 1.1 jmcneill #include <dev/ldvar.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <machine/mainbus.h>
42 1.1 jmcneill #include <machine/thunk.h>
43 1.1 jmcneill
44 1.1 jmcneill static int ld_thunkbus_match(device_t, cfdata_t, void *);
45 1.1 jmcneill static void ld_thunkbus_attach(device_t, device_t, void *);
46 1.1 jmcneill
47 1.1 jmcneill static int ld_thunkbus_ldstart(struct ld_softc *, struct buf *);
48 1.1 jmcneill static int ld_thunkbus_lddump(struct ld_softc *, void *, int, int);
49 1.1 jmcneill static int ld_thunkbus_ldflush(struct ld_softc *, int);
50 1.1 jmcneill
51 1.1 jmcneill struct ld_thunkbus_softc {
52 1.1 jmcneill struct ld_softc sc_ld;
53 1.1 jmcneill
54 1.1 jmcneill int sc_fd;
55 1.1 jmcneill struct stat sc_st;
56 1.1 jmcneill };
57 1.1 jmcneill
58 1.1 jmcneill CFATTACH_DECL_NEW(ld_thunkbus, sizeof(struct ld_thunkbus_softc),
59 1.1 jmcneill ld_thunkbus_match, ld_thunkbus_attach, NULL, NULL);
60 1.1 jmcneill
61 1.1 jmcneill extern int errno;
62 1.1 jmcneill
63 1.1 jmcneill static int
64 1.1 jmcneill ld_thunkbus_match(device_t parent, cfdata_t match, void *opaque)
65 1.1 jmcneill {
66 1.1 jmcneill struct thunkbus_attach_args *taa = opaque;
67 1.1 jmcneill
68 1.1 jmcneill if (taa->taa_type != THUNKBUS_TYPE_DISKIMAGE)
69 1.1 jmcneill return 0;
70 1.1 jmcneill
71 1.1 jmcneill return 1;
72 1.1 jmcneill }
73 1.1 jmcneill
74 1.1 jmcneill static void
75 1.1 jmcneill ld_thunkbus_attach(device_t parent, device_t self, void *opaque)
76 1.1 jmcneill {
77 1.1 jmcneill struct ld_thunkbus_softc *sc = device_private(self);
78 1.1 jmcneill struct ld_softc *ld = &sc->sc_ld;
79 1.1 jmcneill struct thunkbus_attach_args *taa = opaque;
80 1.1 jmcneill const char *path = taa->u.diskimage.path;
81 1.1 jmcneill
82 1.1 jmcneill ld->sc_dv = self;
83 1.1 jmcneill
84 1.1 jmcneill sc->sc_fd = thunk_open(path, O_RDWR, 0);
85 1.1 jmcneill if (sc->sc_fd == -1) {
86 1.1 jmcneill aprint_error(": couldn't open %s: %d\n", path, errno);
87 1.1 jmcneill return;
88 1.1 jmcneill }
89 1.1 jmcneill if (thunk_fstat(sc->sc_fd, &sc->sc_st) == -1) {
90 1.1 jmcneill aprint_error(": couldn't stat %s: %d\n", path, errno);
91 1.1 jmcneill return;
92 1.1 jmcneill }
93 1.1 jmcneill
94 1.1 jmcneill aprint_naive("\n");
95 1.1 jmcneill aprint_normal(": %s (%lld)\n", path, (long long)sc->sc_st.st_size);
96 1.1 jmcneill
97 1.1 jmcneill ld->sc_flags = LDF_ENABLED;
98 1.1 jmcneill ld->sc_maxxfer = sc->sc_st.st_blksize;
99 1.1 jmcneill ld->sc_secsize = 1;
100 1.1 jmcneill ld->sc_secperunit = sc->sc_st.st_size;
101 1.1 jmcneill ld->sc_maxqueuecnt = 1;
102 1.1 jmcneill ld->sc_start = ld_thunkbus_ldstart;
103 1.1 jmcneill ld->sc_dump = ld_thunkbus_lddump;
104 1.1 jmcneill ld->sc_flush = ld_thunkbus_ldflush;
105 1.1 jmcneill
106 1.1 jmcneill ldattach(ld);
107 1.1 jmcneill }
108 1.1 jmcneill
109 1.1 jmcneill static int
110 1.1 jmcneill ld_thunkbus_ldstart(struct ld_softc *ld, struct buf *bp)
111 1.1 jmcneill {
112 1.1 jmcneill struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
113 1.1 jmcneill ssize_t len;
114 1.1 jmcneill
115 1.1 jmcneill if (bp->b_flags & B_READ)
116 1.1 jmcneill len = thunk_pread(sc->sc_fd, bp->b_data, bp->b_bcount,
117 1.1 jmcneill bp->b_rawblkno);
118 1.1 jmcneill else
119 1.1 jmcneill len = thunk_pwrite(sc->sc_fd, bp->b_data, bp->b_bcount,
120 1.1 jmcneill bp->b_rawblkno);
121 1.1 jmcneill
122 1.1 jmcneill if (len == -1)
123 1.1 jmcneill return errno;
124 1.1 jmcneill else if (len != bp->b_bcount)
125 1.1 jmcneill panic("%s: short xfer", __func__);
126 1.1 jmcneill return 0;
127 1.1 jmcneill }
128 1.1 jmcneill
129 1.1 jmcneill static int
130 1.1 jmcneill ld_thunkbus_lddump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
131 1.1 jmcneill {
132 1.1 jmcneill struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
133 1.1 jmcneill ssize_t len;
134 1.1 jmcneill
135 1.1 jmcneill len = thunk_pwrite(sc->sc_fd, data, blkcnt, blkno);
136 1.1 jmcneill if (len == -1)
137 1.1 jmcneill return errno;
138 1.1 jmcneill else if (len != blkcnt) {
139 1.1 jmcneill device_printf(ld->sc_dv, "%s failed (short xfer)\n", __func__);
140 1.1 jmcneill return EIO;
141 1.1 jmcneill }
142 1.1 jmcneill
143 1.1 jmcneill return 0;
144 1.1 jmcneill }
145 1.1 jmcneill
146 1.1 jmcneill static int
147 1.1 jmcneill ld_thunkbus_ldflush(struct ld_softc *ld, int flags)
148 1.1 jmcneill {
149 1.1 jmcneill struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
150 1.1 jmcneill
151 1.1 jmcneill if (thunk_fsync(sc->sc_fd) == -1)
152 1.1 jmcneill return errno;
153 1.1 jmcneill
154 1.1 jmcneill return 0;
155 1.1 jmcneill }
156 1.1 jmcneill
157