source: mainline/uspace/srv/bd/gxe_bd/gxe_bd.c@ 1038a9c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1038a9c was f302586, checked in by Martin Decky <martin@…>, 14 years ago

make sure the client_connection is explicitly set at most once
it is not a mutable variable, it is a weak symbol

  • Property mode set to 100644
File size: 7.6 KB
Line 
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/bd.h>
42#include <async.h>
43#include <as.h>
44#include <fibril_synch.h>
45#include <loc.h>
46#include <sys/types.h>
47#include <errno.h>
48#include <macros.h>
49#include <task.h>
50
51#define NAME "gxe_bd"
52#define NAMESPACE "bd"
53
54enum {
55 CTL_READ_START = 0,
56 CTL_WRITE_START = 1,
57};
58
59enum {
60 STATUS_FAILURE = 0
61};
62
63enum {
64 MAX_DISKS = 2
65};
66
67typedef struct {
68 uint32_t offset_lo;
69 uint32_t pad0;
70 uint32_t offset_hi;
71 uint32_t pad1;
72
73 uint32_t disk_id;
74 uint32_t pad2[3];
75
76 uint32_t control;
77 uint32_t pad3[3];
78
79 uint32_t status;
80
81 uint32_t pad4[3];
82 uint8_t pad5[0x3fc0];
83
84 uint8_t buffer[512];
85} gxe_bd_t;
86
87
88static const size_t block_size = 512;
89static size_t comm_size;
90
91static uintptr_t dev_physical = 0x13000000;
92static gxe_bd_t *dev;
93
94static service_id_t service_id[MAX_DISKS];
95
96static fibril_mutex_t dev_lock[MAX_DISKS];
97
98static int gxe_bd_init(void);
99static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *);
100static int gxe_bd_read_blocks(int disk_id, uint64_t ba, unsigned cnt,
101 void *buf);
102static int gxe_bd_write_blocks(int disk_id, uint64_t ba, unsigned cnt,
103 const void *buf);
104static int gxe_bd_read_block(int disk_id, uint64_t ba, void *buf);
105static int gxe_bd_write_block(int disk_id, uint64_t ba, const void *buf);
106
107int main(int argc, char **argv)
108{
109 printf(NAME ": GXemul disk driver\n");
110
111 if (gxe_bd_init() != EOK)
112 return -1;
113
114 printf(NAME ": Accepting connections\n");
115 task_retval(0);
116 async_manager();
117
118 /* Not reached */
119 return 0;
120}
121
122static int gxe_bd_init(void)
123{
124 void *vaddr;
125 int rc, i;
126 char name[16];
127
128 async_set_client_connection(gxe_bd_connection);
129 rc = loc_server_register(NAME);
130 if (rc < 0) {
131 printf(NAME ": Unable to register driver.\n");
132 return rc;
133 }
134
135 rc = pio_enable((void *) dev_physical, sizeof(gxe_bd_t), &vaddr);
136 if (rc != EOK) {
137 printf(NAME ": Could not initialize device I/O space.\n");
138 return rc;
139 }
140
141 dev = vaddr;
142
143 for (i = 0; i < MAX_DISKS; i++) {
144 snprintf(name, 16, "%s/disk%d", NAMESPACE, i);
145 rc = loc_service_register(name, &service_id[i]);
146 if (rc != EOK) {
147 printf(NAME ": Unable to register device %s.\n", name);
148 return rc;
149 }
150 fibril_mutex_initialize(&dev_lock[i]);
151 }
152
153 return EOK;
154}
155
156static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
157{
158 void *fs_va = NULL;
159 ipc_callid_t callid;
160 ipc_call_t call;
161 sysarg_t method;
162 service_id_t dsid;
163 unsigned int flags;
164 int retval;
165 uint64_t ba;
166 unsigned cnt;
167 int disk_id, i;
168
169 /* Get the device handle. */
170 dsid = IPC_GET_ARG1(*icall);
171
172 /* Determine which disk device is the client connecting to. */
173 disk_id = -1;
174 for (i = 0; i < MAX_DISKS; i++)
175 if (service_id[i] == dsid)
176 disk_id = i;
177
178 if (disk_id < 0) {
179 async_answer_0(iid, EINVAL);
180 return;
181 }
182
183 /* Answer the IPC_M_CONNECT_ME_TO call. */
184 async_answer_0(iid, EOK);
185
186 if (!async_share_out_receive(&callid, &comm_size, &flags)) {
187 async_answer_0(callid, EHANGUP);
188 return;
189 }
190
191 if (comm_size < block_size) {
192 async_answer_0(callid, EHANGUP);
193 return;
194 }
195
196 (void) async_share_out_finalize(callid, &fs_va);
197 if (fs_va == (void *) -1) {
198 async_answer_0(callid, EHANGUP);
199 return;
200 }
201
202 while (true) {
203 callid = async_get_call(&call);
204 method = IPC_GET_IMETHOD(call);
205
206 if (!method) {
207 /* The other side has hung up. */
208 async_answer_0(callid, EOK);
209 return;
210 }
211
212 switch (method) {
213 case BD_READ_BLOCKS:
214 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
215 IPC_GET_ARG2(call));
216 cnt = IPC_GET_ARG3(call);
217 if (cnt * block_size > comm_size) {
218 retval = ELIMIT;
219 break;
220 }
221 retval = gxe_bd_read_blocks(disk_id, ba, cnt, fs_va);
222 break;
223 case BD_WRITE_BLOCKS:
224 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
225 IPC_GET_ARG2(call));
226 cnt = IPC_GET_ARG3(call);
227 if (cnt * block_size > comm_size) {
228 retval = ELIMIT;
229 break;
230 }
231 retval = gxe_bd_write_blocks(disk_id, ba, cnt, fs_va);
232 break;
233 case BD_GET_BLOCK_SIZE:
234 async_answer_1(callid, EOK, block_size);
235 continue;
236 case BD_GET_NUM_BLOCKS:
237 retval = ENOTSUP;
238 break;
239 default:
240 retval = EINVAL;
241 break;
242 }
243 async_answer_0(callid, retval);
244 }
245}
246
247/** Read multiple blocks from the device. */
248static int gxe_bd_read_blocks(int disk_id, uint64_t ba, unsigned cnt,
249 void *buf) {
250
251 int rc;
252
253 while (cnt > 0) {
254 rc = gxe_bd_read_block(disk_id, ba, buf);
255 if (rc != EOK)
256 return rc;
257
258 ++ba;
259 --cnt;
260 buf += block_size;
261 }
262
263 return EOK;
264}
265
266/** Write multiple blocks to the device. */
267static int gxe_bd_write_blocks(int disk_id, uint64_t ba, unsigned cnt,
268 const void *buf) {
269
270 int rc;
271
272 while (cnt > 0) {
273 rc = gxe_bd_write_block(disk_id, ba, buf);
274 if (rc != EOK)
275 return rc;
276
277 ++ba;
278 --cnt;
279 buf += block_size;
280 }
281
282 return EOK;
283}
284
285/** Read a block from the device. */
286static int gxe_bd_read_block(int disk_id, uint64_t ba, void *buf)
287{
288 uint32_t status;
289 uint64_t byte_addr;
290 size_t i;
291 uint32_t w;
292
293 byte_addr = ba * block_size;
294
295 fibril_mutex_lock(&dev_lock[disk_id]);
296 pio_write_32(&dev->offset_lo, (uint32_t) byte_addr);
297 pio_write_32(&dev->offset_hi, byte_addr >> 32);
298 pio_write_32(&dev->disk_id, disk_id);
299 pio_write_32(&dev->control, CTL_READ_START);
300
301 status = pio_read_32(&dev->status);
302 if (status == STATUS_FAILURE) {
303 fibril_mutex_unlock(&dev_lock[disk_id]);
304 return EIO;
305 }
306
307 for (i = 0; i < block_size; i++) {
308 ((uint8_t *) buf)[i] = w = pio_read_8(&dev->buffer[i]);
309 }
310
311 fibril_mutex_unlock(&dev_lock[disk_id]);
312 return EOK;
313}
314
315/** Write a block to the device. */
316static int gxe_bd_write_block(int disk_id, uint64_t ba, const void *buf)
317{
318 uint32_t status;
319 uint64_t byte_addr;
320 size_t i;
321
322 byte_addr = ba * block_size;
323
324 fibril_mutex_lock(&dev_lock[disk_id]);
325
326 for (i = 0; i < block_size; i++) {
327 pio_write_8(&dev->buffer[i], ((const uint8_t *) buf)[i]);
328 }
329
330 pio_write_32(&dev->offset_lo, (uint32_t) byte_addr);
331 pio_write_32(&dev->offset_hi, byte_addr >> 32);
332 pio_write_32(&dev->disk_id, disk_id);
333 pio_write_32(&dev->control, CTL_WRITE_START);
334
335 status = pio_read_32(&dev->status);
336 if (status == STATUS_FAILURE) {
337 fibril_mutex_unlock(&dev_lock[disk_id]);
338 return EIO;
339 }
340
341 fibril_mutex_unlock(&dev_lock[disk_id]);
342 return EOK;
343}
344
345/**
346 * @}
347 */
Note: See TracBrowser for help on using the repository browser.