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