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