Rev 4428 | Rev 4442 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4428 | Rev 4440 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2009 Jiri Svoboda |
2 | * Copyright (c) 2009 Jiri Svoboda |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup bd |
29 | /** @addtogroup bd |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief GXemul disk driver |
35 | * @brief GXemul disk driver |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <stdio.h> |
38 | #include <stdio.h> |
39 | #include <libarch/ddi.h> |
39 | #include <libarch/ddi.h> |
40 | #include <ddi.h> |
40 | #include <ddi.h> |
41 | #include <ipc/ipc.h> |
41 | #include <ipc/ipc.h> |
42 | #include <ipc/bd.h> |
42 | #include <ipc/bd.h> |
43 | #include <async.h> |
43 | #include <async.h> |
44 | #include <as.h> |
44 | #include <as.h> |
45 | #include <futex.h> |
45 | #include <futex.h> |
46 | #include <devmap.h> |
46 | #include <devmap.h> |
47 | #include <sys/types.h> |
47 | #include <sys/types.h> |
48 | #include <errno.h> |
48 | #include <errno.h> |
49 | 49 | ||
50 | #define NAME "gxe_bd" |
50 | #define NAME "gxe_bd" |
51 | 51 | ||
52 | enum { |
52 | enum { |
53 | CTL_READ_START = 0, |
53 | CTL_READ_START = 0, |
54 | CTL_WRITE_START = 1, |
54 | CTL_WRITE_START = 1, |
55 | }; |
55 | }; |
56 | 56 | ||
57 | enum { |
57 | enum { |
58 | STATUS_FAILURE = 0 |
58 | STATUS_FAILURE = 0 |
59 | }; |
59 | }; |
60 | 60 | ||
61 | typedef struct { |
61 | typedef struct { |
62 | uint32_t offset_lo; |
62 | uint32_t offset_lo; |
63 | uint32_t pad0; |
63 | uint32_t pad0; |
64 | uint32_t offset_hi; |
64 | uint32_t offset_hi; |
65 | uint32_t pad1; |
65 | uint32_t pad1; |
66 | 66 | ||
67 | uint32_t disk_id; |
67 | uint32_t disk_id; |
68 | uint32_t pad2[3]; |
68 | uint32_t pad2[3]; |
69 | 69 | ||
70 | uint32_t control; |
70 | uint32_t control; |
71 | uint32_t pad3[3]; |
71 | uint32_t pad3[3]; |
72 | 72 | ||
73 | uint32_t status; |
73 | uint32_t status; |
74 | 74 | ||
75 | /* FIXME: Need to fix pio_enable() to support >= page_size areas. |
- | |
76 | - | ||
77 | uint32_t pad4[3]; |
75 | uint32_t pad4[3]; |
78 | uint32_t pad5[0x3fc0]; |
76 | uint8_t pad5[0x3fc0]; |
79 | 77 | ||
80 | uint8_t buffer[512];*/ |
78 | uint8_t buffer[512]; |
81 | } gxe_bd_t; |
79 | } gxe_bd_t; |
82 | 80 | ||
83 | typedef struct { |
- | |
84 | uint8_t buffer[512]; |
- | |
85 | } gxe_buf_t; |
- | |
86 | 81 | ||
87 | static const size_t block_size = 512; |
82 | static const size_t block_size = 512; |
88 | static size_t comm_size; |
83 | static size_t comm_size; |
89 | 84 | ||
90 | static uintptr_t dev_physical = 0x13000000; |
85 | static uintptr_t dev_physical = 0x13000000; |
91 | static gxe_bd_t *dev; |
86 | static gxe_bd_t *dev; |
92 | static gxe_buf_t *devbuf; |
- | |
93 | 87 | ||
94 | static uint32_t disk_id = 0; |
88 | static uint32_t disk_id = 0; |
95 | 89 | ||
96 | static atomic_t dev_futex = FUTEX_INITIALIZER; |
90 | static atomic_t dev_futex = FUTEX_INITIALIZER; |
97 | 91 | ||
98 | static int gxe_bd_init(void); |
92 | static int gxe_bd_init(void); |
99 | static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
93 | static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
100 | static int gx_bd_rdwr(ipcarg_t method, off_t offset, off_t size, void *buf); |
94 | static int gx_bd_rdwr(ipcarg_t method, off_t offset, off_t size, void *buf); |
101 | static int gxe_bd_read_block(uint64_t offset, size_t size, void *buf); |
95 | static int gxe_bd_read_block(uint64_t offset, size_t size, void *buf); |
102 | static int gxe_bd_write_block(uint64_t offset, size_t size, const void *buf); |
96 | static int gxe_bd_write_block(uint64_t offset, size_t size, const void *buf); |
103 | 97 | ||
104 | int main(int argc, char **argv) |
98 | int main(int argc, char **argv) |
105 | { |
99 | { |
106 | printf(NAME ": GXemul disk driver\n"); |
100 | printf(NAME ": GXemul disk driver\n"); |
107 | 101 | ||
108 | if (gxe_bd_init() != EOK) |
102 | if (gxe_bd_init() != EOK) |
109 | return -1; |
103 | return -1; |
110 | 104 | ||
111 | printf(NAME ": Accepting connections\n"); |
105 | printf(NAME ": Accepting connections\n"); |
112 | async_manager(); |
106 | async_manager(); |
113 | 107 | ||
114 | /* Not reached */ |
108 | /* Not reached */ |
115 | return 0; |
109 | return 0; |
116 | } |
110 | } |
117 | 111 | ||
118 | static int gxe_bd_init(void) |
112 | static int gxe_bd_init(void) |
119 | { |
113 | { |
120 | dev_handle_t dev_handle; |
114 | dev_handle_t dev_handle; |
121 | void *vaddr; |
115 | void *vaddr; |
122 | int rc; |
116 | int rc; |
123 | 117 | ||
124 | rc = devmap_driver_register(NAME, gxe_bd_connection); |
118 | rc = devmap_driver_register(NAME, gxe_bd_connection); |
125 | if (rc < 0) { |
119 | if (rc < 0) { |
126 | printf(NAME ": Unable to register driver.\n"); |
120 | printf(NAME ": Unable to register driver.\n"); |
127 | return rc; |
121 | return rc; |
128 | } |
122 | } |
129 | 123 | ||
130 | rc = pio_enable((void *) dev_physical, sizeof(gxe_bd_t), &vaddr); |
124 | rc = pio_enable((void *) dev_physical, sizeof(gxe_bd_t), &vaddr); |
131 | if (rc != EOK) { |
125 | if (rc != EOK) { |
132 | printf(NAME ": Could not initialize device I/O space.\n"); |
126 | printf(NAME ": Could not initialize device I/O space.\n"); |
133 | return rc; |
127 | return rc; |
134 | } |
128 | } |
135 | 129 | ||
136 | dev = vaddr; |
130 | dev = vaddr; |
137 | 131 | ||
138 | rc = pio_enable((void *) dev_physical + 0x4000, sizeof(gxe_buf_t), &vaddr); |
- | |
139 | if (rc != EOK) { |
- | |
140 | printf(NAME ": Could not initialize device I/O space.\n"); |
- | |
141 | return rc; |
- | |
142 | } |
- | |
143 | - | ||
144 | devbuf = vaddr; |
- | |
145 | - | ||
146 | rc = devmap_device_register("disk0", &dev_handle); |
132 | rc = devmap_device_register("disk0", &dev_handle); |
147 | if (rc != EOK) { |
133 | if (rc != EOK) { |
148 | devmap_hangup_phone(DEVMAP_DRIVER); |
134 | devmap_hangup_phone(DEVMAP_DRIVER); |
149 | printf(NAME ": Unable to register device.\n"); |
135 | printf(NAME ": Unable to register device.\n"); |
150 | return rc; |
136 | return rc; |
151 | } |
137 | } |
152 | 138 | ||
153 | return EOK; |
139 | return EOK; |
154 | } |
140 | } |
155 | 141 | ||
156 | static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall) |
142 | static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall) |
157 | { |
143 | { |
158 | void *fs_va = NULL; |
144 | void *fs_va = NULL; |
159 | ipc_callid_t callid; |
145 | ipc_callid_t callid; |
160 | ipc_call_t call; |
146 | ipc_call_t call; |
161 | ipcarg_t method; |
147 | ipcarg_t method; |
162 | int flags; |
148 | int flags; |
163 | int retval; |
149 | int retval; |
164 | off_t idx; |
150 | off_t idx; |
165 | off_t size; |
151 | off_t size; |
166 | 152 | ||
167 | /* Answer the IPC_M_CONNECT_ME_TO call. */ |
153 | /* Answer the IPC_M_CONNECT_ME_TO call. */ |
168 | ipc_answer_0(iid, EOK); |
154 | ipc_answer_0(iid, EOK); |
169 | 155 | ||
170 | if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { |
156 | if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { |
171 | ipc_answer_0(callid, EHANGUP); |
157 | ipc_answer_0(callid, EHANGUP); |
172 | return; |
158 | return; |
173 | } |
159 | } |
174 | 160 | ||
175 | fs_va = as_get_mappable_page(comm_size); |
161 | fs_va = as_get_mappable_page(comm_size); |
176 | if (fs_va == NULL) { |
162 | if (fs_va == NULL) { |
177 | ipc_answer_0(callid, EHANGUP); |
163 | ipc_answer_0(callid, EHANGUP); |
178 | return; |
164 | return; |
179 | } |
165 | } |
180 | 166 | ||
181 | (void) ipc_share_out_finalize(callid, fs_va); |
167 | (void) ipc_share_out_finalize(callid, fs_va); |
182 | 168 | ||
183 | while (1) { |
169 | while (1) { |
184 | callid = async_get_call(&call); |
170 | callid = async_get_call(&call); |
185 | method = IPC_GET_METHOD(call); |
171 | method = IPC_GET_METHOD(call); |
186 | switch (method) { |
172 | switch (method) { |
187 | case IPC_M_PHONE_HUNGUP: |
173 | case IPC_M_PHONE_HUNGUP: |
188 | /* The other side has hung up. */ |
174 | /* The other side has hung up. */ |
189 | ipc_answer_0(callid, EOK); |
175 | ipc_answer_0(callid, EOK); |
190 | return; |
176 | return; |
191 | case BD_READ_BLOCK: |
177 | case BD_READ_BLOCK: |
192 | case BD_WRITE_BLOCK: |
178 | case BD_WRITE_BLOCK: |
193 | idx = IPC_GET_ARG1(call); |
179 | idx = IPC_GET_ARG1(call); |
194 | size = IPC_GET_ARG2(call); |
180 | size = IPC_GET_ARG2(call); |
195 | if (size > comm_size) { |
181 | if (size > comm_size) { |
196 | retval = EINVAL; |
182 | retval = EINVAL; |
197 | break; |
183 | break; |
198 | } |
184 | } |
199 | retval = gx_bd_rdwr(method, idx * size, size, fs_va); |
185 | retval = gx_bd_rdwr(method, idx * size, size, fs_va); |
200 | break; |
186 | break; |
201 | default: |
187 | default: |
202 | retval = EINVAL; |
188 | retval = EINVAL; |
203 | break; |
189 | break; |
204 | } |
190 | } |
205 | ipc_answer_0(callid, retval); |
191 | ipc_answer_0(callid, retval); |
206 | } |
192 | } |
207 | } |
193 | } |
208 | 194 | ||
209 | static int gx_bd_rdwr(ipcarg_t method, off_t offset, off_t size, void *buf) |
195 | static int gx_bd_rdwr(ipcarg_t method, off_t offset, off_t size, void *buf) |
210 | { |
196 | { |
211 | int rc; |
197 | int rc; |
212 | size_t now; |
198 | size_t now; |
213 | 199 | ||
214 | while (size > 0) { |
200 | while (size > 0) { |
215 | now = size < block_size ? size : block_size; |
201 | now = size < block_size ? size : block_size; |
216 | 202 | ||
217 | if (method == BD_READ_BLOCK) |
203 | if (method == BD_READ_BLOCK) |
218 | rc = gxe_bd_read_block(offset, now, buf); |
204 | rc = gxe_bd_read_block(offset, now, buf); |
219 | else |
205 | else |
220 | rc = gxe_bd_write_block(offset, now, buf); |
206 | rc = gxe_bd_write_block(offset, now, buf); |
221 | 207 | ||
222 | if (rc != EOK) |
208 | if (rc != EOK) |
223 | return rc; |
209 | return rc; |
224 | 210 | ||
225 | buf += block_size; |
211 | buf += block_size; |
226 | offset += block_size; |
212 | offset += block_size; |
227 | 213 | ||
228 | if (size > block_size) |
214 | if (size > block_size) |
229 | size -= block_size; |
215 | size -= block_size; |
230 | else |
216 | else |
231 | size = 0; |
217 | size = 0; |
232 | } |
218 | } |
233 | 219 | ||
234 | return EOK; |
220 | return EOK; |
235 | } |
221 | } |
236 | 222 | ||
237 | static int gxe_bd_read_block(uint64_t offset, size_t size, void *buf) |
223 | static int gxe_bd_read_block(uint64_t offset, size_t size, void *buf) |
238 | { |
224 | { |
239 | uint32_t status; |
225 | uint32_t status; |
240 | size_t i; |
226 | size_t i; |
241 | uint32_t w; |
227 | uint32_t w; |
242 | 228 | ||
243 | futex_down(&dev_futex); |
229 | futex_down(&dev_futex); |
244 | pio_write_32(&dev->offset_lo, (uint32_t) offset); |
230 | pio_write_32(&dev->offset_lo, (uint32_t) offset); |
245 | pio_write_32(&dev->offset_hi, offset >> 32); |
231 | pio_write_32(&dev->offset_hi, offset >> 32); |
246 | pio_write_32(&dev->disk_id, disk_id); |
232 | pio_write_32(&dev->disk_id, disk_id); |
247 | pio_write_32(&dev->control, CTL_READ_START); |
233 | pio_write_32(&dev->control, CTL_READ_START); |
248 | 234 | ||
249 | status = pio_read_32(&dev->status); |
235 | status = pio_read_32(&dev->status); |
250 | if (status == STATUS_FAILURE) { |
236 | if (status == STATUS_FAILURE) { |
251 | return EIO; |
237 | return EIO; |
252 | } |
238 | } |
253 | 239 | ||
254 | for (i = 0; i < size; i++) { |
240 | for (i = 0; i < size; i++) { |
255 | ((uint8_t *) buf)[i] = w = |
- | |
256 | pio_read_8(&devbuf->buffer[i]); |
241 | ((uint8_t *) buf)[i] = w = pio_read_8(&dev->buffer[i]); |
257 | } |
242 | } |
258 | 243 | ||
259 | futex_up(&dev_futex); |
244 | futex_up(&dev_futex); |
260 | return EOK; |
245 | return EOK; |
261 | } |
246 | } |
262 | 247 | ||
263 | static int gxe_bd_write_block(uint64_t offset, size_t size, const void *buf) |
248 | static int gxe_bd_write_block(uint64_t offset, size_t size, const void *buf) |
264 | { |
249 | { |
265 | uint32_t status; |
250 | uint32_t status; |
266 | size_t i; |
251 | size_t i; |
267 | uint32_t w; |
252 | uint32_t w; |
268 | 253 | ||
269 | for (i = 0; i < size; i++) { |
254 | for (i = 0; i < size; i++) { |
270 | pio_write_8(&devbuf->buffer[i], ((const uint8_t *) buf)[i]); |
255 | pio_write_8(&dev->buffer[i], ((const uint8_t *) buf)[i]); |
271 | } |
256 | } |
272 | 257 | ||
273 | futex_down(&dev_futex); |
258 | futex_down(&dev_futex); |
274 | pio_write_32(&dev->offset_lo, (uint32_t) offset); |
259 | pio_write_32(&dev->offset_lo, (uint32_t) offset); |
275 | pio_write_32(&dev->offset_hi, offset >> 32); |
260 | pio_write_32(&dev->offset_hi, offset >> 32); |
276 | pio_write_32(&dev->disk_id, disk_id); |
261 | pio_write_32(&dev->disk_id, disk_id); |
277 | pio_write_32(&dev->control, CTL_WRITE_START); |
262 | pio_write_32(&dev->control, CTL_WRITE_START); |
278 | 263 | ||
279 | status = pio_read_32(&dev->status); |
264 | status = pio_read_32(&dev->status); |
280 | if (status == STATUS_FAILURE) { |
265 | if (status == STATUS_FAILURE) { |
281 | return EIO; |
266 | return EIO; |
282 | } |
267 | } |
283 | 268 | ||
284 | futex_up(&dev_futex); |
269 | futex_up(&dev_futex); |
285 | return EOK; |
270 | return EOK; |
286 | } |
271 | } |
287 | 272 | ||
288 | /** |
273 | /** |
289 | * @} |
274 | * @} |
290 | */ |
275 | */ |
291 | 276 |