ld_thunkbus.c revision 1.27 1 /* $NetBSD: ld_thunkbus.c,v 1.27 2012/01/06 20:40: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.27 2012/01/06 20:40: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_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 blksize;
94 off_t size;
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 = MAXPHYS;
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, ld);
122
123 sc->busy = false;
124
125 ldattach(ld);
126 }
127
128 static int
129 ld_thunkbus_ldstart(struct ld_softc *ld, struct buf *bp)
130 {
131 struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
132 struct ld_thunkbus_transfer *tt = &sc->sc_tt;
133
134 tt->tt_sc = sc;
135 tt->tt_bp = bp;
136
137 /* let the softint do the work */
138 sc->busy = true;
139 spl_intr(IPL_BIO, softint_schedule, sc->sc_ih);
140
141 return 0;
142 }
143
144 static void
145 ld_thunkbus_complete(void *arg)
146 {
147 struct ld_softc *ld = arg;
148 struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
149 struct ld_thunkbus_transfer *tt = &sc->sc_tt;
150 struct buf *bp = tt->tt_bp;
151 off_t offset = bp->b_rawblkno * ld->sc_secsize;
152 size_t ret;
153
154 if (!sc->busy)
155 panic("%s: but not busy?\n", __func__);
156
157 if (offset < 0 ||
158 offset + bp->b_bcount > ld->sc_secsize * ld->sc_secperunit) {
159 bp->b_error = EIO;
160 bp->b_resid = bp->b_bcount;
161 goto done;
162 }
163
164 //printf("%s: %s %u @ %lld -> %p (flags 0x%08x)\n", __func__,
165 // bp->b_flags & B_READ ? "read" : "write",
166 // (unsigned int)bp->b_bcount, (long long)offset, bp->b_data, bp->b_flags);
167
168 /* read/write the request */
169 if (bp->b_flags & B_READ) {
170 ret = thunk_pread(sc->sc_fd, bp->b_data, bp->b_bcount, offset);
171 } else {
172 ret = thunk_pwrite(sc->sc_fd, bp->b_data, bp->b_bcount, offset);
173 }
174
175 //if (ret == -1)
176 // printf("%s: errno = %d\n", __func__, thunk_geterrno());
177
178 /* setup return params */
179 if ((ret >= 0) && (ret == bp->b_bcount)) {
180 bp->b_resid = 0;
181 } else {
182 bp->b_error = thunk_geterrno();
183 bp->b_resid = bp->b_bcount;
184 }
185
186 done:
187 thunk_printf_debug("\tfin\n");
188 if (bp->b_error)
189 thunk_printf_debug("error!\n");
190
191 sc->busy = false;
192 lddone(&sc->sc_ld, bp);
193 }
194
195 static int
196 ld_thunkbus_lddump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
197 {
198 struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
199 ssize_t len;
200
201 len = thunk_pwrite(sc->sc_fd, data, blkcnt, blkno * ld->sc_secsize);
202 if (len == -1)
203 return thunk_geterrno();
204 else if (len != blkcnt) {
205 device_printf(ld->sc_dv, "%s failed (short xfer)\n", __func__);
206 return EIO;
207 }
208
209 return 0;
210 }
211
212 static int
213 ld_thunkbus_ldflush(struct ld_softc *ld, int flags)
214 {
215 struct ld_thunkbus_softc *sc = (struct ld_thunkbus_softc *)ld;
216
217 if (thunk_fsync(sc->sc_fd) == -1)
218 return thunk_geterrno();
219
220 return 0;
221 }
222