ld_thunkbus.c revision 1.8 1 1.8 jmcneill /* $NetBSD: ld_thunkbus.c,v 1.8 2011/09/03 19:07:32 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.8 jmcneill #define BROKEN_SIGINFO
30 1.8 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.8 jmcneill __KERNEL_RCSID(0, "$NetBSD: ld_thunkbus.c,v 1.8 2011/09/03 19:07:32 jmcneill Exp $");
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/proc.h>
36 1.1 jmcneill #include <sys/systm.h>
37 1.1 jmcneill #include <sys/device.h>
38 1.1 jmcneill #include <sys/buf.h>
39 1.1 jmcneill #include <sys/disk.h>
40 1.2 jmcneill #include <sys/kmem.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/ldvar.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <machine/mainbus.h>
45 1.1 jmcneill #include <machine/thunk.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.1 jmcneill static int ld_thunkbus_lddump(struct ld_softc *, void *, int, int);
52 1.1 jmcneill static int ld_thunkbus_ldflush(struct ld_softc *, int);
53 1.1 jmcneill
54 1.3 jmcneill static void ld_thunkbus_sig(int, siginfo_t *, void *);
55 1.3 jmcneill static void ld_thunkbus_complete(void *);
56 1.3 jmcneill
57 1.4 jmcneill struct ld_thunkbus_softc;
58 1.4 jmcneill
59 1.4 jmcneill struct ld_thunkbus_transfer {
60 1.4 jmcneill struct ld_thunkbus_softc *tt_sc;
61 1.4 jmcneill struct aiocb tt_aio;
62 1.4 jmcneill struct buf *tt_bp;
63 1.4 jmcneill };
64 1.2 jmcneill
65 1.1 jmcneill struct ld_thunkbus_softc {
66 1.1 jmcneill struct ld_softc sc_ld;
67 1.1 jmcneill
68 1.1 jmcneill int sc_fd;
69 1.3 jmcneill void *sc_ih;
70 1.3 jmcneill
71 1.4 jmcneill struct ld_thunkbus_transfer sc_tt;
72 1.2 jmcneill };
73 1.2 jmcneill
74 1.8 jmcneill #ifdef BROKEN_SIGINFO
75 1.8 jmcneill struct ld_thunkbus_transfer *ld_thunkbus_tt;
76 1.8 jmcneill #endif
77 1.8 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.8 jmcneill #ifdef BROKEN_SIGINFO
90 1.8 jmcneill /* We can only have one instance if siginfo doesn't work */
91 1.8 jmcneill if (ld_thunkbus_tt != NULL)
92 1.8 jmcneill return 0;
93 1.8 jmcneill #endif
94 1.8 jmcneill
95 1.1 jmcneill return 1;
96 1.1 jmcneill }
97 1.1 jmcneill
98 1.1 jmcneill static void
99 1.1 jmcneill ld_thunkbus_attach(device_t parent, device_t self, void *opaque)
100 1.1 jmcneill {
101 1.1 jmcneill struct ld_thunkbus_softc *sc = device_private(self);
102 1.1 jmcneill struct ld_softc *ld = &sc->sc_ld;
103 1.1 jmcneill struct thunkbus_attach_args *taa = opaque;
104 1.1 jmcneill const char *path = taa->u.diskimage.path;
105 1.2 jmcneill struct sigaction sa;
106 1.5 jmcneill ssize_t size, blksize;
107 1.1 jmcneill
108 1.1 jmcneill ld->sc_dv = self;
109 1.1 jmcneill
110 1.1 jmcneill sc->sc_fd = thunk_open(path, O_RDWR, 0);
111 1.1 jmcneill if (sc->sc_fd == -1) {
112 1.7 jmcneill aprint_error(": couldn't open %s: %d\n", path, thunk_geterrno());
113 1.1 jmcneill return;
114 1.1 jmcneill }
115 1.5 jmcneill if (thunk_fstat_getsize(sc->sc_fd, &size, &blksize) == -1) {
116 1.7 jmcneill aprint_error(": couldn't stat %s: %d\n", path, thunk_geterrno());
117 1.1 jmcneill return;
118 1.1 jmcneill }
119 1.1 jmcneill
120 1.8 jmcneill #ifdef BROKEN_SIGINFO
121 1.8 jmcneill ld_thunkbus_tt = &sc->sc_tt;
122 1.8 jmcneill #endif
123 1.8 jmcneill
124 1.1 jmcneill aprint_naive("\n");
125 1.5 jmcneill aprint_normal(": %s (%lld)\n", path, (long long)size);
126 1.1 jmcneill
127 1.1 jmcneill ld->sc_flags = LDF_ENABLED;
128 1.5 jmcneill ld->sc_maxxfer = blksize;
129 1.2 jmcneill ld->sc_secsize = 512;
130 1.5 jmcneill ld->sc_secperunit = size / ld->sc_secsize;
131 1.1 jmcneill ld->sc_maxqueuecnt = 1;
132 1.1 jmcneill ld->sc_start = ld_thunkbus_ldstart;
133 1.1 jmcneill ld->sc_dump = ld_thunkbus_lddump;
134 1.1 jmcneill ld->sc_flush = ld_thunkbus_ldflush;
135 1.1 jmcneill
136 1.3 jmcneill sc->sc_ih = softint_establish(SOFTINT_BIO,
137 1.3 jmcneill ld_thunkbus_complete, sc);
138 1.3 jmcneill
139 1.2 jmcneill sigemptyset(&sa.sa_mask);
140 1.2 jmcneill sa.sa_flags = SA_RESTART|SA_SIGINFO;
141 1.3 jmcneill sa.sa_sigaction = ld_thunkbus_sig;
142 1.2 jmcneill if (thunk_sigaction(SIGIO, &sa, NULL) == -1)
143 1.7 jmcneill panic("couldn't register SIGIO handler: %d", thunk_geterrno());
144 1.2 jmcneill
145 1.1 jmcneill ldattach(ld);
146 1.1 jmcneill }
147 1.1 jmcneill
148 1.2 jmcneill static void
149 1.3 jmcneill ld_thunkbus_sig(int sig, siginfo_t *info, void *ctx)
150 1.2 jmcneill {
151 1.8 jmcneill struct ld_thunkbus_transfer *tt = NULL;
152 1.2 jmcneill struct ld_thunkbus_softc *sc;
153 1.2 jmcneill
154 1.2 jmcneill curcpu()->ci_idepth++;
155 1.2 jmcneill
156 1.2 jmcneill if (info->si_signo == SIGIO) {
157 1.8 jmcneill #ifdef BROKEN_SIGINFO
158 1.8 jmcneill tt = ld_thunkbus_tt;
159 1.8 jmcneill #else
160 1.8 jmcneill if (info->si_code == SI_ASYNCIO)
161 1.8 jmcneill tt = info->si_value.sival_ptr;
162 1.8 jmcneill #endif
163 1.8 jmcneill if (tt) {
164 1.8 jmcneill sc = tt->tt_sc;
165 1.8 jmcneill softint_schedule(sc->sc_ih);
166 1.8 jmcneill }
167 1.3 jmcneill }
168 1.3 jmcneill
169 1.3 jmcneill curcpu()->ci_idepth--;
170 1.3 jmcneill }
171 1.2 jmcneill
172 1.3 jmcneill static void
173 1.3 jmcneill ld_thunkbus_complete(void *arg)
174 1.3 jmcneill {
175 1.3 jmcneill struct ld_thunkbus_softc *sc = arg;
176 1.4 jmcneill struct ld_thunkbus_transfer *tt = &sc->sc_tt;
177 1.3 jmcneill struct buf *bp = tt->tt_bp;
178 1.3 jmcneill
179 1.3 jmcneill if (thunk_aio_error(&tt->tt_aio) == 0 &&
180 1.3 jmcneill thunk_aio_return(&tt->tt_aio) != -1) {
181 1.3 jmcneill bp->b_resid = 0;
182 1.3 jmcneill } else {
183 1.7 jmcneill bp->b_error = thunk_geterrno();
184 1.3 jmcneill bp->b_resid = bp->b_bcount;
185 1.3 jmcneill }
186 1.2 jmcneill
187 1.3 jmcneill if (bp->b_error)
188 1.6 reinoud printf("error!\n");
189 1.2 jmcneill
190 1.3 jmcneill lddone(&sc->sc_ld, bp);
191 1.2 jmcneill }
192 1.2 jmcneill
193 1.1 jmcneill static int
194 1.1 jmcneill ld_thunkbus_ldstart(struct ld_softc *ld, struct buf *bp)
195 1.1 jmcneill {
196 1.1 jmcneill struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
197 1.4 jmcneill struct ld_thunkbus_transfer *tt = &sc->sc_tt;
198 1.2 jmcneill int error;
199 1.2 jmcneill
200 1.2 jmcneill tt->tt_sc = sc;
201 1.2 jmcneill tt->tt_bp = bp;
202 1.2 jmcneill memset(&tt->tt_aio, 0, sizeof(tt->tt_aio));
203 1.2 jmcneill tt->tt_aio.aio_fildes = sc->sc_fd;
204 1.2 jmcneill tt->tt_aio.aio_buf = bp->b_data;
205 1.2 jmcneill tt->tt_aio.aio_nbytes = bp->b_bcount;
206 1.2 jmcneill tt->tt_aio.aio_offset = bp->b_rawblkno * ld->sc_secsize;
207 1.2 jmcneill
208 1.2 jmcneill tt->tt_aio.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
209 1.2 jmcneill tt->tt_aio.aio_sigevent.sigev_signo = SIGIO;
210 1.2 jmcneill tt->tt_aio.aio_sigevent.sigev_value.sival_ptr = tt;
211 1.2 jmcneill
212 1.2 jmcneill #if 0
213 1.6 reinoud device_printf(sc->sc_ld.sc_dv, "%s addr %p, off=%lld, count=%lld\n",
214 1.2 jmcneill (bp->b_flags & B_READ) ? "rd" : "wr",
215 1.6 reinoud bp->b_data,
216 1.2 jmcneill (long long)bp->b_rawblkno,
217 1.2 jmcneill (long long)bp->b_bcount);
218 1.2 jmcneill #endif
219 1.1 jmcneill
220 1.1 jmcneill if (bp->b_flags & B_READ)
221 1.2 jmcneill error = thunk_aio_read(&tt->tt_aio);
222 1.1 jmcneill else
223 1.2 jmcneill error = thunk_aio_write(&tt->tt_aio);
224 1.1 jmcneill
225 1.7 jmcneill return error == -1 ? thunk_geterrno() : 0;
226 1.1 jmcneill }
227 1.1 jmcneill
228 1.1 jmcneill static int
229 1.1 jmcneill ld_thunkbus_lddump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
230 1.1 jmcneill {
231 1.1 jmcneill struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
232 1.1 jmcneill ssize_t len;
233 1.1 jmcneill
234 1.1 jmcneill len = thunk_pwrite(sc->sc_fd, data, blkcnt, blkno);
235 1.1 jmcneill if (len == -1)
236 1.7 jmcneill return thunk_geterrno();
237 1.1 jmcneill else if (len != blkcnt) {
238 1.1 jmcneill device_printf(ld->sc_dv, "%s failed (short xfer)\n", __func__);
239 1.1 jmcneill return EIO;
240 1.1 jmcneill }
241 1.1 jmcneill
242 1.1 jmcneill return 0;
243 1.1 jmcneill }
244 1.1 jmcneill
245 1.1 jmcneill static int
246 1.1 jmcneill ld_thunkbus_ldflush(struct ld_softc *ld, int flags)
247 1.1 jmcneill {
248 1.1 jmcneill struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
249 1.1 jmcneill
250 1.1 jmcneill if (thunk_fsync(sc->sc_fd) == -1)
251 1.7 jmcneill return thunk_geterrno();
252 1.1 jmcneill
253 1.1 jmcneill return 0;
254 1.1 jmcneill }
255 1.1 jmcneill
256