ld_thunkbus.c revision 1.23 1 /* $NetBSD: ld_thunkbus.c,v 1.23 2011/12/13 15:53:47 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.23 2011/12/13 15:53:47 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_complete(void *arg);
54
55 struct ld_thunkbus_softc;
56
57 struct ld_thunkbus_transfer {
58 struct ld_thunkbus_softc *tt_sc;
59 struct buf *tt_bp;
60 };
61
62 struct ld_thunkbus_softc {
63 struct ld_softc sc_ld;
64
65 int sc_fd;
66 void *sc_ih;
67
68 struct ld_thunkbus_transfer sc_tt;
69 bool busy;
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 ssize_t size, blksize;
94
95 ld->sc_dv = self;
96
97 sc->sc_fd = thunk_open(path, O_RDWR, 0);
98 if (sc->sc_fd == -1) {
99 aprint_error(": couldn't open %s: %d\n", path, thunk_geterrno());
100 return;
101 }
102 if (thunk_fstat_getsize(sc->sc_fd, &size, &blksize) == -1) {
103 aprint_error(": couldn't stat %s: %d\n", path, thunk_geterrno());
104 return;
105 }
106
107 aprint_naive("\n");
108 aprint_normal(": %s (%lld)\n", path, (long long)size);
109
110 ld->sc_flags = LDF_ENABLED;
111 ld->sc_maxxfer = blksize;
112 ld->sc_secsize = 512;
113 ld->sc_secperunit = size / ld->sc_secsize;
114 ld->sc_maxqueuecnt = 1;
115 ld->sc_start = ld_thunkbus_ldstart;
116 ld->sc_dump = ld_thunkbus_lddump;
117 ld->sc_flush = ld_thunkbus_ldflush;
118
119 sc->sc_ih = softint_establish(SOFTINT_BIO,
120 ld_thunkbus_complete, ld);
121
122 sc->busy = false;
123
124 ldattach(ld);
125 }
126
127 static int
128 ld_thunkbus_ldstart(struct ld_softc *ld, struct buf *bp)
129 {
130 struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
131 struct ld_thunkbus_transfer *tt = &sc->sc_tt;
132
133 tt->tt_sc = sc;
134 tt->tt_bp = bp;
135
136 /* let the softint do the work */
137 sc->busy = true;
138 spl_intr(IPL_BIO, softint_schedule, sc->sc_ih);
139
140 return 0;
141 }
142
143 static void
144 ld_thunkbus_complete(void *arg)
145 {
146 struct ld_softc *ld = arg;
147 struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
148 struct ld_thunkbus_transfer *tt = &sc->sc_tt;
149 struct buf *bp = tt->tt_bp;
150 off_t offset = bp->b_rawblkno * ld->sc_secsize;
151 size_t ret;
152
153 if (!sc->busy)
154 panic("%s: but not busy?\n", __func__);
155
156 if (offset < 0 ||
157 offset + bp->b_bcount > ld->sc_secsize * ld->sc_secperunit) {
158 bp->b_error = EIO;
159 bp->b_resid = bp->b_bcount;
160 goto done;
161 }
162
163 //printf("ld: %s %u @ %lld -> %p (flags 0x%08x)\n", bp->b_flags & B_READ ? "read" : "write",
164 // (unsigned int)bp->b_bcount, (long long)offset, bp->b_data, bp->b_flags);
165
166 /* read/write the request */
167 if (bp->b_flags & B_READ) {
168 ret = thunk_pread(sc->sc_fd, bp->b_data, bp->b_bcount, offset);
169 } else {
170 ret = thunk_pwrite(sc->sc_fd, bp->b_data, bp->b_bcount, offset);
171 }
172
173 //if (ret == -1)
174 // printf("ld: errno = %d\n", thunk_geterrno());
175
176 /* setup return params */
177 if ((ret >= 0) && (ret == bp->b_bcount)) {
178 bp->b_resid = 0;
179 } else {
180 bp->b_error = thunk_geterrno();
181 bp->b_resid = bp->b_bcount;
182 }
183
184 done:
185 dprintf_debug("\tfin\n");
186 if (bp->b_error)
187 dprintf_debug("error!\n");
188
189 sc->busy = false;
190 lddone(&sc->sc_ld, bp);
191 }
192
193 static int
194 ld_thunkbus_lddump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
195 {
196 struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
197 ssize_t len;
198
199 len = thunk_pwrite(sc->sc_fd, data, blkcnt, blkno * ld->sc_secsize);
200 if (len == -1)
201 return thunk_geterrno();
202 else if (len != blkcnt) {
203 device_printf(ld->sc_dv, "%s failed (short xfer)\n", __func__);
204 return EIO;
205 }
206
207 return 0;
208 }
209
210 static int
211 ld_thunkbus_ldflush(struct ld_softc *ld, int flags)
212 {
213 struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
214
215 if (thunk_fsync(sc->sc_fd) == -1)
216 return thunk_geterrno();
217
218 return 0;
219 }
220