source: mainline/uspace/srv/bd/gxe_bd/gxe_bd.c@ b33ec43

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b33ec43 was 9934f7d, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Add extra argument to async connection handlers that can be used for passing
information from async_connect_to_me() to the handler.

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