ld_thunkbus.c revision 1.11 1 /* $NetBSD: ld_thunkbus.c,v 1.11 2011/09/05 11:25:29 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.11 2011/09/05 11:25:29 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
45 static int ld_thunkbus_match(device_t, cfdata_t, void *);
46 static void ld_thunkbus_attach(device_t, device_t, void *);
47
48 static int ld_thunkbus_ldstart(struct ld_softc *, struct buf *);
49 static int ld_thunkbus_lddump(struct ld_softc *, void *, int, int);
50 static int ld_thunkbus_ldflush(struct ld_softc *, int);
51
52 static void ld_thunkbus_sig(int, siginfo_t *, void *);
53 static void ld_thunkbus_complete(void *);
54
55 struct ld_thunkbus_softc;
56
57 struct ld_thunkbus_transfer {
58 struct ld_thunkbus_softc *tt_sc;
59 struct aiocb tt_aio;
60 struct buf *tt_bp;
61 };
62
63 struct ld_thunkbus_softc {
64 struct ld_softc sc_ld;
65
66 int sc_fd;
67 void *sc_ih;
68
69 struct ld_thunkbus_transfer sc_tt;
70 };
71
72 CFATTACH_DECL_NEW(ld_thunkbus, sizeof(struct ld_thunkbus_softc),
73 ld_thunkbus_match, ld_thunkbus_attach, NULL, NULL);
74
75 static int
76 ld_thunkbus_match(device_t parent, cfdata_t match, void *opaque)
77 {
78 struct thunkbus_attach_args *taa = opaque;
79
80 if (taa->taa_type != THUNKBUS_TYPE_DISKIMAGE)
81 return 0;
82
83 return 1;
84 }
85
86 static void
87 ld_thunkbus_attach(device_t parent, device_t self, void *opaque)
88 {
89 struct ld_thunkbus_softc *sc = device_private(self);
90 struct ld_softc *ld = &sc->sc_ld;
91 struct thunkbus_attach_args *taa = opaque;
92 const char *path = taa->u.diskimage.path;
93 struct sigaction sa;
94 ssize_t size, blksize;
95
96 ld->sc_dv = self;
97
98 sc->sc_fd = thunk_open(path, O_RDWR, 0);
99 if (sc->sc_fd == -1) {
100 aprint_error(": couldn't open %s: %d\n", path, thunk_geterrno());
101 return;
102 }
103 if (thunk_fstat_getsize(sc->sc_fd, &size, &blksize) == -1) {
104 aprint_error(": couldn't stat %s: %d\n", path, thunk_geterrno());
105 return;
106 }
107
108 aprint_naive("\n");
109 aprint_normal(": %s (%lld)\n", path, (long long)size);
110
111 ld->sc_flags = LDF_ENABLED;
112 ld->sc_maxxfer = blksize;
113 ld->sc_secsize = 512;
114 ld->sc_secperunit = size / ld->sc_secsize;
115 ld->sc_maxqueuecnt = 1;
116 ld->sc_start = ld_thunkbus_ldstart;
117 ld->sc_dump = ld_thunkbus_lddump;
118 ld->sc_flush = ld_thunkbus_ldflush;
119
120 sc->sc_ih = softint_establish(SOFTINT_BIO,
121 ld_thunkbus_complete, sc);
122
123 sigemptyset(&sa.sa_mask);
124 sa.sa_flags = SA_RESTART | SA_SIGINFO;
125 sa.sa_sigaction = ld_thunkbus_sig;
126 thunk_sigaddset(&sa.sa_mask, SIGALRM);
127 if (thunk_sigaction(SIGIO, &sa, NULL) == -1)
128 panic("couldn't register SIGIO handler: %d", thunk_geterrno());
129
130 ldattach(ld);
131 }
132
133 static void
134 ld_thunkbus_sig(int sig, siginfo_t *info, void *ctx)
135 {
136 struct ld_thunkbus_transfer *tt = NULL;
137 struct ld_thunkbus_softc *sc;
138
139 curcpu()->ci_idepth++;
140
141 if (info->si_signo == SIGIO) {
142 if (info->si_code == SI_ASYNCIO)
143 tt = info->si_value.sival_ptr;
144 if (tt) {
145 sc = tt->tt_sc;
146 softint_schedule(sc->sc_ih);
147 }
148 }
149
150 curcpu()->ci_idepth--;
151 }
152
153 static void
154 ld_thunkbus_complete(void *arg)
155 {
156 struct ld_thunkbus_softc *sc = arg;
157 struct ld_thunkbus_transfer *tt = &sc->sc_tt;
158 struct buf *bp = tt->tt_bp;
159
160 if (thunk_aio_error(&tt->tt_aio) == 0 &&
161 thunk_aio_return(&tt->tt_aio) != -1) {
162 bp->b_resid = 0;
163 } else {
164 bp->b_error = thunk_geterrno();
165 bp->b_resid = bp->b_bcount;
166 }
167
168 if (bp->b_error)
169 printf("error!\n");
170
171 lddone(&sc->sc_ld, bp);
172 }
173
174 static int
175 ld_thunkbus_ldstart(struct ld_softc *ld, struct buf *bp)
176 {
177 struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
178 struct ld_thunkbus_transfer *tt = &sc->sc_tt;
179 int error;
180
181 tt->tt_sc = sc;
182 tt->tt_bp = bp;
183 memset(&tt->tt_aio, 0, sizeof(tt->tt_aio));
184 tt->tt_aio.aio_fildes = sc->sc_fd;
185 tt->tt_aio.aio_buf = bp->b_data;
186 tt->tt_aio.aio_nbytes = bp->b_bcount;
187 tt->tt_aio.aio_offset = bp->b_rawblkno * ld->sc_secsize;
188
189 tt->tt_aio.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
190 tt->tt_aio.aio_sigevent.sigev_signo = SIGIO;
191 tt->tt_aio.aio_sigevent.sigev_value.sival_ptr = tt;
192
193 #if 0
194 device_printf(sc->sc_ld.sc_dv, "%s addr %p, off=%lld, count=%lld\n",
195 (bp->b_flags & B_READ) ? "rd" : "wr",
196 bp->b_data,
197 (long long)bp->b_rawblkno,
198 (long long)bp->b_bcount);
199 #endif
200
201 if (bp->b_flags & B_READ)
202 error = thunk_aio_read(&tt->tt_aio);
203 else
204 error = thunk_aio_write(&tt->tt_aio);
205
206 return error == -1 ? thunk_geterrno() : 0;
207 }
208
209 static int
210 ld_thunkbus_lddump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
211 {
212 struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
213 ssize_t len;
214
215 len = thunk_pwrite(sc->sc_fd, data, blkcnt, blkno);
216 if (len == -1)
217 return thunk_geterrno();
218 else if (len != blkcnt) {
219 device_printf(ld->sc_dv, "%s failed (short xfer)\n", __func__);
220 return EIO;
221 }
222
223 return 0;
224 }
225
226 static int
227 ld_thunkbus_ldflush(struct ld_softc *ld, int flags)
228 {
229 struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
230
231 if (thunk_fsync(sc->sc_fd) == -1)
232 return thunk_geterrno();
233
234 return 0;
235 }
236
237