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