Rev 4440 | Rev 4545 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4440 | Rev 4442 | ||
---|---|---|---|
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 | enum { |
|
- | 62 | MAX_DISKS = 2 |
|
- | 63 | }; |
|
- | 64 | ||
61 | typedef struct { |
65 | typedef struct { |
62 | uint32_t offset_lo; |
66 | uint32_t offset_lo; |
63 | uint32_t pad0; |
67 | uint32_t pad0; |
64 | uint32_t offset_hi; |
68 | uint32_t offset_hi; |
65 | uint32_t pad1; |
69 | uint32_t pad1; |
66 | 70 | ||
67 | uint32_t disk_id; |
71 | uint32_t disk_id; |
68 | uint32_t pad2[3]; |
72 | uint32_t pad2[3]; |
69 | 73 | ||
70 | uint32_t control; |
74 | uint32_t control; |
71 | uint32_t pad3[3]; |
75 | uint32_t pad3[3]; |
72 | 76 | ||
73 | uint32_t status; |
77 | uint32_t status; |
74 | 78 | ||
75 | uint32_t pad4[3]; |
79 | uint32_t pad4[3]; |
76 | uint8_t pad5[0x3fc0]; |
80 | uint8_t pad5[0x3fc0]; |
77 | 81 | ||
78 | uint8_t buffer[512]; |
82 | uint8_t buffer[512]; |
79 | } gxe_bd_t; |
83 | } gxe_bd_t; |
80 | 84 | ||
81 | 85 | ||
82 | static const size_t block_size = 512; |
86 | static const size_t block_size = 512; |
83 | static size_t comm_size; |
87 | static size_t comm_size; |
84 | 88 | ||
85 | static uintptr_t dev_physical = 0x13000000; |
89 | static uintptr_t dev_physical = 0x13000000; |
86 | static gxe_bd_t *dev; |
90 | static gxe_bd_t *dev; |
87 | 91 | ||
88 | static uint32_t disk_id = 0; |
92 | static dev_handle_t dev_handle[MAX_DISKS]; |
89 | 93 | ||
90 | static atomic_t dev_futex = FUTEX_INITIALIZER; |
94 | static atomic_t dev_futex = FUTEX_INITIALIZER; |
91 | 95 | ||
92 | static int gxe_bd_init(void); |
96 | static int gxe_bd_init(void); |
93 | static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
97 | static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall); |
94 | static int gx_bd_rdwr(ipcarg_t method, off_t offset, off_t size, void *buf); |
98 | static int gx_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, off_t size, |
- | 99 | void *buf); |
|
95 | static int gxe_bd_read_block(uint64_t offset, size_t size, void *buf); |
100 | static int gxe_bd_read_block(int disk_id, uint64_t offset, size_t size, |
- | 101 | void *buf); |
|
96 | static int gxe_bd_write_block(uint64_t offset, size_t size, const void *buf); |
102 | static int gxe_bd_write_block(int disk_id, uint64_t offset, size_t size, |
- | 103 | const void *buf); |
|
97 | 104 | ||
98 | int main(int argc, char **argv) |
105 | int main(int argc, char **argv) |
99 | { |
106 | { |
100 | printf(NAME ": GXemul disk driver\n"); |
107 | printf(NAME ": GXemul disk driver\n"); |
101 | 108 | ||
102 | if (gxe_bd_init() != EOK) |
109 | if (gxe_bd_init() != EOK) |
103 | return -1; |
110 | return -1; |
104 | 111 | ||
105 | printf(NAME ": Accepting connections\n"); |
112 | printf(NAME ": Accepting connections\n"); |
106 | async_manager(); |
113 | async_manager(); |
107 | 114 | ||
108 | /* Not reached */ |
115 | /* Not reached */ |
109 | return 0; |
116 | return 0; |
110 | } |
117 | } |
111 | 118 | ||
112 | static int gxe_bd_init(void) |
119 | static int gxe_bd_init(void) |
113 | { |
120 | { |
114 | dev_handle_t dev_handle; |
- | |
115 | void *vaddr; |
121 | void *vaddr; |
116 | int rc; |
122 | int rc, i; |
- | 123 | char name[16]; |
|
117 | 124 | ||
118 | rc = devmap_driver_register(NAME, gxe_bd_connection); |
125 | rc = devmap_driver_register(NAME, gxe_bd_connection); |
119 | if (rc < 0) { |
126 | if (rc < 0) { |
120 | printf(NAME ": Unable to register driver.\n"); |
127 | printf(NAME ": Unable to register driver.\n"); |
121 | return rc; |
128 | return rc; |
122 | } |
129 | } |
123 | 130 | ||
124 | rc = pio_enable((void *) dev_physical, sizeof(gxe_bd_t), &vaddr); |
131 | rc = pio_enable((void *) dev_physical, sizeof(gxe_bd_t), &vaddr); |
125 | if (rc != EOK) { |
132 | if (rc != EOK) { |
126 | printf(NAME ": Could not initialize device I/O space.\n"); |
133 | printf(NAME ": Could not initialize device I/O space.\n"); |
127 | return rc; |
134 | return rc; |
128 | } |
135 | } |
129 | 136 | ||
130 | dev = vaddr; |
137 | dev = vaddr; |
131 | 138 | ||
- | 139 | for (i = 0; i < MAX_DISKS; i++) { |
|
- | 140 | snprintf(name, 16, "disk%d", i); |
|
132 | rc = devmap_device_register("disk0", &dev_handle); |
141 | rc = devmap_device_register(name, &dev_handle[i]); |
133 | if (rc != EOK) { |
142 | if (rc != EOK) { |
134 | devmap_hangup_phone(DEVMAP_DRIVER); |
143 | devmap_hangup_phone(DEVMAP_DRIVER); |
135 | printf(NAME ": Unable to register device.\n"); |
144 | printf(NAME ": Unable to register device %s.\n", |
- | 145 | name); |
|
136 | return rc; |
146 | return rc; |
- | 147 | } |
|
137 | } |
148 | } |
138 | 149 | ||
139 | return EOK; |
150 | return EOK; |
140 | } |
151 | } |
141 | 152 | ||
142 | static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall) |
153 | static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall) |
143 | { |
154 | { |
144 | void *fs_va = NULL; |
155 | void *fs_va = NULL; |
145 | ipc_callid_t callid; |
156 | ipc_callid_t callid; |
146 | ipc_call_t call; |
157 | ipc_call_t call; |
147 | ipcarg_t method; |
158 | ipcarg_t method; |
- | 159 | dev_handle_t dh; |
|
148 | int flags; |
160 | int flags; |
149 | int retval; |
161 | int retval; |
150 | off_t idx; |
162 | off_t idx; |
151 | off_t size; |
163 | off_t size; |
- | 164 | int disk_id, i; |
|
- | 165 | ||
- | 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 | } |
|
152 | 179 | ||
153 | /* Answer the IPC_M_CONNECT_ME_TO call. */ |
180 | /* Answer the IPC_M_CONNECT_ME_TO call. */ |
154 | ipc_answer_0(iid, EOK); |
181 | ipc_answer_0(iid, EOK); |
155 | 182 | ||
156 | if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { |
183 | if (!ipc_share_out_receive(&callid, &comm_size, &flags)) { |
157 | ipc_answer_0(callid, EHANGUP); |
184 | ipc_answer_0(callid, EHANGUP); |
158 | return; |
185 | return; |
159 | } |
186 | } |
160 | 187 | ||
161 | fs_va = as_get_mappable_page(comm_size); |
188 | fs_va = as_get_mappable_page(comm_size); |
162 | if (fs_va == NULL) { |
189 | if (fs_va == NULL) { |
163 | ipc_answer_0(callid, EHANGUP); |
190 | ipc_answer_0(callid, EHANGUP); |
164 | return; |
191 | return; |
165 | } |
192 | } |
166 | 193 | ||
167 | (void) ipc_share_out_finalize(callid, fs_va); |
194 | (void) ipc_share_out_finalize(callid, fs_va); |
168 | 195 | ||
169 | while (1) { |
196 | while (1) { |
170 | callid = async_get_call(&call); |
197 | callid = async_get_call(&call); |
171 | method = IPC_GET_METHOD(call); |
198 | method = IPC_GET_METHOD(call); |
172 | switch (method) { |
199 | switch (method) { |
173 | case IPC_M_PHONE_HUNGUP: |
200 | case IPC_M_PHONE_HUNGUP: |
174 | /* The other side has hung up. */ |
201 | /* The other side has hung up. */ |
175 | ipc_answer_0(callid, EOK); |
202 | ipc_answer_0(callid, EOK); |
176 | return; |
203 | return; |
177 | case BD_READ_BLOCK: |
204 | case BD_READ_BLOCK: |
178 | case BD_WRITE_BLOCK: |
205 | case BD_WRITE_BLOCK: |
179 | idx = IPC_GET_ARG1(call); |
206 | idx = IPC_GET_ARG1(call); |
180 | size = IPC_GET_ARG2(call); |
207 | size = IPC_GET_ARG2(call); |
181 | if (size > comm_size) { |
208 | if (size > comm_size) { |
182 | retval = EINVAL; |
209 | retval = EINVAL; |
183 | break; |
210 | break; |
184 | } |
211 | } |
185 | retval = gx_bd_rdwr(method, idx * size, size, fs_va); |
212 | retval = gx_bd_rdwr(disk_id, method, idx * size, |
- | 213 | size, fs_va); |
|
186 | break; |
214 | break; |
187 | default: |
215 | default: |
188 | retval = EINVAL; |
216 | retval = EINVAL; |
189 | break; |
217 | break; |
190 | } |
218 | } |
191 | ipc_answer_0(callid, retval); |
219 | ipc_answer_0(callid, retval); |
192 | } |
220 | } |
193 | } |
221 | } |
194 | 222 | ||
195 | static int gx_bd_rdwr(ipcarg_t method, off_t offset, off_t size, void *buf) |
223 | static int gx_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, off_t size, |
- | 224 | void *buf) |
|
196 | { |
225 | { |
197 | int rc; |
226 | int rc; |
198 | size_t now; |
227 | size_t now; |
199 | 228 | ||
200 | while (size > 0) { |
229 | while (size > 0) { |
201 | now = size < block_size ? size : block_size; |
230 | now = size < block_size ? size : block_size; |
202 | 231 | ||
203 | if (method == BD_READ_BLOCK) |
232 | if (method == BD_READ_BLOCK) |
204 | rc = gxe_bd_read_block(offset, now, buf); |
233 | rc = gxe_bd_read_block(disk_id, offset, now, buf); |
205 | else |
234 | else |
206 | rc = gxe_bd_write_block(offset, now, buf); |
235 | rc = gxe_bd_write_block(disk_id, offset, now, buf); |
207 | 236 | ||
208 | if (rc != EOK) |
237 | if (rc != EOK) |
209 | return rc; |
238 | return rc; |
210 | 239 | ||
211 | buf += block_size; |
240 | buf += block_size; |
212 | offset += block_size; |
241 | offset += block_size; |
213 | 242 | ||
214 | if (size > block_size) |
243 | if (size > block_size) |
215 | size -= block_size; |
244 | size -= block_size; |
216 | else |
245 | else |
217 | size = 0; |
246 | size = 0; |
218 | } |
247 | } |
219 | 248 | ||
220 | return EOK; |
249 | return EOK; |
221 | } |
250 | } |
222 | 251 | ||
223 | static int gxe_bd_read_block(uint64_t offset, size_t size, void *buf) |
252 | static int gxe_bd_read_block(int disk_id, uint64_t offset, size_t size, |
- | 253 | void *buf) |
|
224 | { |
254 | { |
225 | uint32_t status; |
255 | uint32_t status; |
226 | size_t i; |
256 | size_t i; |
227 | uint32_t w; |
257 | uint32_t w; |
228 | 258 | ||
229 | futex_down(&dev_futex); |
259 | futex_down(&dev_futex); |
230 | pio_write_32(&dev->offset_lo, (uint32_t) offset); |
260 | pio_write_32(&dev->offset_lo, (uint32_t) offset); |
231 | pio_write_32(&dev->offset_hi, offset >> 32); |
261 | pio_write_32(&dev->offset_hi, offset >> 32); |
232 | pio_write_32(&dev->disk_id, disk_id); |
262 | pio_write_32(&dev->disk_id, disk_id); |
233 | pio_write_32(&dev->control, CTL_READ_START); |
263 | pio_write_32(&dev->control, CTL_READ_START); |
234 | 264 | ||
235 | status = pio_read_32(&dev->status); |
265 | status = pio_read_32(&dev->status); |
236 | if (status == STATUS_FAILURE) { |
266 | if (status == STATUS_FAILURE) { |
237 | return EIO; |
267 | return EIO; |
238 | } |
268 | } |
239 | 269 | ||
240 | for (i = 0; i < size; i++) { |
270 | for (i = 0; i < size; i++) { |
241 | ((uint8_t *) buf)[i] = w = pio_read_8(&dev->buffer[i]); |
271 | ((uint8_t *) buf)[i] = w = pio_read_8(&dev->buffer[i]); |
242 | } |
272 | } |
243 | 273 | ||
244 | futex_up(&dev_futex); |
274 | futex_up(&dev_futex); |
245 | return EOK; |
275 | return EOK; |
246 | } |
276 | } |
247 | 277 | ||
248 | static int gxe_bd_write_block(uint64_t offset, size_t size, const void *buf) |
278 | static int gxe_bd_write_block(int disk_id, uint64_t offset, size_t size, |
- | 279 | const void *buf) |
|
249 | { |
280 | { |
250 | uint32_t status; |
281 | uint32_t status; |
251 | size_t i; |
282 | size_t i; |
252 | uint32_t w; |
- | |
253 | 283 | ||
254 | for (i = 0; i < size; i++) { |
284 | for (i = 0; i < size; i++) { |
255 | pio_write_8(&dev->buffer[i], ((const uint8_t *) buf)[i]); |
285 | pio_write_8(&dev->buffer[i], ((const uint8_t *) buf)[i]); |
256 | } |
286 | } |
257 | 287 | ||
258 | futex_down(&dev_futex); |
288 | futex_down(&dev_futex); |
259 | pio_write_32(&dev->offset_lo, (uint32_t) offset); |
289 | pio_write_32(&dev->offset_lo, (uint32_t) offset); |
260 | pio_write_32(&dev->offset_hi, offset >> 32); |
290 | pio_write_32(&dev->offset_hi, offset >> 32); |
261 | pio_write_32(&dev->disk_id, disk_id); |
291 | pio_write_32(&dev->disk_id, disk_id); |
262 | pio_write_32(&dev->control, CTL_WRITE_START); |
292 | pio_write_32(&dev->control, CTL_WRITE_START); |
263 | 293 | ||
264 | status = pio_read_32(&dev->status); |
294 | status = pio_read_32(&dev->status); |
265 | if (status == STATUS_FAILURE) { |
295 | if (status == STATUS_FAILURE) { |
266 | return EIO; |
296 | return EIO; |
267 | } |
297 | } |
268 | 298 | ||
269 | futex_up(&dev_futex); |
299 | futex_up(&dev_futex); |
270 | return EOK; |
300 | return EOK; |
271 | } |
301 | } |
272 | 302 | ||
273 | /** |
303 | /** |
274 | * @} |
304 | * @} |
275 | */ |
305 | */ |
276 | 306 |