ld_thunkbus.c revision 1.21 1 1.21 jmcneill /* $NetBSD: ld_thunkbus.c,v 1.21 2011/12/13 13:32:15 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.21 jmcneill __KERNEL_RCSID(0, "$NetBSD: ld_thunkbus.c,v 1.21 2011/12/13 13:32:15 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.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 static void ld_thunkbus_complete(void *arg);
54 1.3 jmcneill
55 1.4 jmcneill struct ld_thunkbus_softc;
56 1.4 jmcneill
57 1.4 jmcneill struct ld_thunkbus_transfer {
58 1.4 jmcneill struct ld_thunkbus_softc *tt_sc;
59 1.4 jmcneill struct buf *tt_bp;
60 1.4 jmcneill };
61 1.2 jmcneill
62 1.1 jmcneill struct ld_thunkbus_softc {
63 1.1 jmcneill struct ld_softc sc_ld;
64 1.1 jmcneill
65 1.1 jmcneill int sc_fd;
66 1.3 jmcneill void *sc_ih;
67 1.3 jmcneill
68 1.21 jmcneill uint8_t sc_bbuf[MAXBSIZE];
69 1.21 jmcneill
70 1.4 jmcneill struct ld_thunkbus_transfer sc_tt;
71 1.15 reinoud bool busy;
72 1.2 jmcneill };
73 1.2 jmcneill
74 1.1 jmcneill CFATTACH_DECL_NEW(ld_thunkbus, sizeof(struct ld_thunkbus_softc),
75 1.1 jmcneill ld_thunkbus_match, ld_thunkbus_attach, NULL, NULL);
76 1.1 jmcneill
77 1.1 jmcneill static int
78 1.1 jmcneill ld_thunkbus_match(device_t parent, cfdata_t match, void *opaque)
79 1.1 jmcneill {
80 1.1 jmcneill struct thunkbus_attach_args *taa = opaque;
81 1.1 jmcneill
82 1.1 jmcneill if (taa->taa_type != THUNKBUS_TYPE_DISKIMAGE)
83 1.1 jmcneill return 0;
84 1.1 jmcneill
85 1.1 jmcneill return 1;
86 1.1 jmcneill }
87 1.1 jmcneill
88 1.1 jmcneill static void
89 1.1 jmcneill ld_thunkbus_attach(device_t parent, device_t self, void *opaque)
90 1.1 jmcneill {
91 1.1 jmcneill struct ld_thunkbus_softc *sc = device_private(self);
92 1.1 jmcneill struct ld_softc *ld = &sc->sc_ld;
93 1.1 jmcneill struct thunkbus_attach_args *taa = opaque;
94 1.1 jmcneill const char *path = taa->u.diskimage.path;
95 1.5 jmcneill ssize_t size, blksize;
96 1.1 jmcneill
97 1.1 jmcneill ld->sc_dv = self;
98 1.1 jmcneill
99 1.1 jmcneill sc->sc_fd = thunk_open(path, O_RDWR, 0);
100 1.1 jmcneill if (sc->sc_fd == -1) {
101 1.7 jmcneill aprint_error(": couldn't open %s: %d\n", path, thunk_geterrno());
102 1.1 jmcneill return;
103 1.1 jmcneill }
104 1.5 jmcneill if (thunk_fstat_getsize(sc->sc_fd, &size, &blksize) == -1) {
105 1.7 jmcneill aprint_error(": couldn't stat %s: %d\n", path, thunk_geterrno());
106 1.1 jmcneill return;
107 1.1 jmcneill }
108 1.1 jmcneill
109 1.1 jmcneill aprint_naive("\n");
110 1.5 jmcneill aprint_normal(": %s (%lld)\n", path, (long long)size);
111 1.1 jmcneill
112 1.1 jmcneill ld->sc_flags = LDF_ENABLED;
113 1.5 jmcneill ld->sc_maxxfer = blksize;
114 1.2 jmcneill ld->sc_secsize = 512;
115 1.5 jmcneill ld->sc_secperunit = size / ld->sc_secsize;
116 1.1 jmcneill ld->sc_maxqueuecnt = 1;
117 1.1 jmcneill ld->sc_start = ld_thunkbus_ldstart;
118 1.1 jmcneill ld->sc_dump = ld_thunkbus_lddump;
119 1.1 jmcneill ld->sc_flush = ld_thunkbus_ldflush;
120 1.1 jmcneill
121 1.3 jmcneill sc->sc_ih = softint_establish(SOFTINT_BIO,
122 1.21 jmcneill ld_thunkbus_complete, ld);
123 1.2 jmcneill
124 1.15 reinoud sc->busy = false;
125 1.15 reinoud
126 1.1 jmcneill ldattach(ld);
127 1.1 jmcneill }
128 1.1 jmcneill
129 1.1 jmcneill static int
130 1.1 jmcneill ld_thunkbus_ldstart(struct ld_softc *ld, struct buf *bp)
131 1.1 jmcneill {
132 1.1 jmcneill struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
133 1.4 jmcneill struct ld_thunkbus_transfer *tt = &sc->sc_tt;
134 1.2 jmcneill int error;
135 1.2 jmcneill
136 1.2 jmcneill tt->tt_sc = sc;
137 1.2 jmcneill tt->tt_bp = bp;
138 1.15 reinoud
139 1.17 reinoud /* let the softint do the work */
140 1.21 jmcneill sc->busy = true;
141 1.17 reinoud spl_intr(IPL_BIO, softint_schedule, sc->sc_ih);
142 1.17 reinoud error = 0;
143 1.21 jmcneill
144 1.17 reinoud return error == -1 ? thunk_geterrno() : 0;
145 1.17 reinoud }
146 1.17 reinoud
147 1.17 reinoud static void
148 1.17 reinoud ld_thunkbus_complete(void *arg)
149 1.17 reinoud {
150 1.21 jmcneill struct ld_softc *ld = arg;
151 1.21 jmcneill struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
152 1.17 reinoud struct ld_thunkbus_transfer *tt = &sc->sc_tt;
153 1.17 reinoud struct buf *bp = tt->tt_bp;
154 1.21 jmcneill off_t offset = bp->b_rawblkno * ld->sc_secsize;
155 1.21 jmcneill void *bbuf = sc->sc_bbuf;
156 1.21 jmcneill size_t ret;
157 1.17 reinoud
158 1.17 reinoud if (!sc->busy)
159 1.17 reinoud panic("%s: but not busy?\n", __func__);
160 1.17 reinoud
161 1.21 jmcneill if (offset < 0 ||
162 1.21 jmcneill offset + bp->b_bcount > ld->sc_secsize * ld->sc_secperunit) {
163 1.21 jmcneill bp->b_error = EIO;
164 1.17 reinoud bp->b_resid = bp->b_bcount;
165 1.21 jmcneill goto done;
166 1.17 reinoud }
167 1.21 jmcneill
168 1.21 jmcneill //printf("ld: %s %u @ %lld -> %p (flags 0x%08x)\n", bp->b_flags & B_READ ? "read" : "write",
169 1.21 jmcneill // (unsigned int)bp->b_bcount, (long long)offset, bp->b_data, bp->b_flags);
170 1.17 reinoud
171 1.17 reinoud /* read/write the request */
172 1.21 jmcneill if (bp->b_flags & B_READ) {
173 1.21 jmcneill if (bp->b_flags & B_PHYS) {
174 1.21 jmcneill /* read to bounce buffer and copy out */
175 1.21 jmcneill ret = thunk_pread(sc->sc_fd, bbuf, bp->b_bcount, offset);
176 1.21 jmcneill if (ret > 0)
177 1.21 jmcneill copyout(bbuf, bp->b_data, ret);
178 1.21 jmcneill } else {
179 1.21 jmcneill /* just read it */
180 1.21 jmcneill ret = thunk_pread(sc->sc_fd, bp->b_data, bp->b_bcount, offset);
181 1.21 jmcneill }
182 1.21 jmcneill } else {
183 1.21 jmcneill if (bp->b_flags & B_PHYS) {
184 1.21 jmcneill /* copy in to bounce buffer and write it */
185 1.21 jmcneill copyin(bp->b_data, bbuf, bp->b_bcount);
186 1.21 jmcneill ret = thunk_pwrite(sc->sc_fd, bbuf, bp->b_bcount, offset);
187 1.21 jmcneill } else {
188 1.21 jmcneill /* just write it */
189 1.21 jmcneill ret = thunk_pwrite(sc->sc_fd, bp->b_data, bp->b_bcount, offset);
190 1.21 jmcneill }
191 1.21 jmcneill }
192 1.21 jmcneill
193 1.21 jmcneill //if (ret == -1)
194 1.21 jmcneill // printf("ld: errno = %d\n", thunk_geterrno());
195 1.17 reinoud
196 1.17 reinoud /* setup return params */
197 1.17 reinoud if ((ret >= 0) && (ret == bp->b_bcount)) {
198 1.17 reinoud bp->b_resid = 0;
199 1.17 reinoud } else {
200 1.17 reinoud bp->b_error = thunk_geterrno();
201 1.17 reinoud bp->b_resid = bp->b_bcount;
202 1.17 reinoud }
203 1.1 jmcneill
204 1.21 jmcneill done:
205 1.17 reinoud dprintf_debug("\tfin\n");
206 1.17 reinoud if (bp->b_error)
207 1.17 reinoud dprintf_debug("error!\n");
208 1.17 reinoud
209 1.17 reinoud sc->busy = false;
210 1.17 reinoud lddone(&sc->sc_ld, bp);
211 1.1 jmcneill }
212 1.1 jmcneill
213 1.1 jmcneill static int
214 1.1 jmcneill ld_thunkbus_lddump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
215 1.1 jmcneill {
216 1.1 jmcneill struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
217 1.1 jmcneill ssize_t len;
218 1.1 jmcneill
219 1.21 jmcneill len = thunk_pwrite(sc->sc_fd, data, blkcnt, blkno * ld->sc_secsize);
220 1.1 jmcneill if (len == -1)
221 1.7 jmcneill return thunk_geterrno();
222 1.1 jmcneill else if (len != blkcnt) {
223 1.1 jmcneill device_printf(ld->sc_dv, "%s failed (short xfer)\n", __func__);
224 1.1 jmcneill return EIO;
225 1.1 jmcneill }
226 1.1 jmcneill
227 1.1 jmcneill return 0;
228 1.1 jmcneill }
229 1.1 jmcneill
230 1.1 jmcneill static int
231 1.1 jmcneill ld_thunkbus_ldflush(struct ld_softc *ld, int flags)
232 1.1 jmcneill {
233 1.1 jmcneill struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
234 1.1 jmcneill
235 1.1 jmcneill if (thunk_fsync(sc->sc_fd) == -1)
236 1.7 jmcneill return thunk_geterrno();
237 1.1 jmcneill
238 1.1 jmcneill return 0;
239 1.1 jmcneill }
240