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