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