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