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

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

Obtain block device size automatically, if possible. Implement ftell(). Fix seek implementation in VFS if whence == SEEK_END.

  • 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/ipc.h>
42#include <ipc/bd.h>
43#include <async.h>
44#include <as.h>
[1e4cada]45#include <fibril_synch.h>
[44a53fd]46#include <devmap.h>
47#include <sys/types.h>
48#include <errno.h>
[1ee00b7]49#include <macros.h>
[95bc57c]50#include <task.h>
[44a53fd]51
[1313ee9]52#define NAME "gxe_bd"
53#define NAMESPACE "bd"
[44a53fd]54
55enum {
56 CTL_READ_START = 0,
57 CTL_WRITE_START = 1,
58};
59
60enum {
61 STATUS_FAILURE = 0
62};
63
[84adbf0]64enum {
65 MAX_DISKS = 2
66};
67
[44a53fd]68typedef struct {
69 uint32_t offset_lo;
70 uint32_t pad0;
71 uint32_t offset_hi;
72 uint32_t pad1;
73
74 uint32_t disk_id;
75 uint32_t pad2[3];
76
77 uint32_t control;
78 uint32_t pad3[3];
79
80 uint32_t status;
81
82 uint32_t pad4[3];
[39580667]83 uint8_t pad5[0x3fc0];
[44a53fd]84
[39580667]85 uint8_t buffer[512];
[44a53fd]86} gxe_bd_t;
87
88
[3ecc02e]89static const size_t block_size = 512;
90static size_t comm_size;
91
[44a53fd]92static uintptr_t dev_physical = 0x13000000;
93static gxe_bd_t *dev;
94
[84adbf0]95static dev_handle_t dev_handle[MAX_DISKS];
[44a53fd]96
[12956e57]97static fibril_mutex_t dev_lock[MAX_DISKS];
[44a53fd]98
99static int gxe_bd_init(void);
100static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
[1ee00b7]101static int gxe_bd_read_blocks(int disk_id, uint64_t ba, unsigned cnt,
[84adbf0]102 void *buf);
[1ee00b7]103static int gxe_bd_write_blocks(int disk_id, uint64_t ba, unsigned cnt,
[84adbf0]104 const void *buf);
[1ee00b7]105static int gxe_bd_read_block(int disk_id, uint64_t ba, void *buf);
106static int gxe_bd_write_block(int disk_id, uint64_t ba, const void *buf);
[44a53fd]107
108int main(int argc, char **argv)
109{
110 printf(NAME ": GXemul disk driver\n");
111
112 if (gxe_bd_init() != EOK)
113 return -1;
114
115 printf(NAME ": Accepting connections\n");
[95bc57c]116 task_retval(0);
[44a53fd]117 async_manager();
118
119 /* Not reached */
120 return 0;
121}
122
123static int gxe_bd_init(void)
124{
125 void *vaddr;
[84adbf0]126 int rc, i;
127 char name[16];
[44a53fd]128
129 rc = devmap_driver_register(NAME, gxe_bd_connection);
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
[84adbf0]143 for (i = 0; i < MAX_DISKS; i++) {
[1313ee9]144 snprintf(name, 16, "%s/disk%d", NAMESPACE, i);
[84adbf0]145 rc = devmap_device_register(name, &dev_handle[i]);
146 if (rc != EOK) {
147 devmap_hangup_phone(DEVMAP_DRIVER);
[1313ee9]148 printf(NAME ": Unable to register device %s.\n", name);
[84adbf0]149 return rc;
150 }
[12956e57]151 fibril_mutex_initialize(&dev_lock[i]);
[44a53fd]152 }
153
154 return EOK;
155}
156
157static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
158{
159 void *fs_va = NULL;
160 ipc_callid_t callid;
161 ipc_call_t call;
[3ecc02e]162 ipcarg_t method;
[84adbf0]163 dev_handle_t dh;
[44a53fd]164 int flags;
165 int retval;
[1ee00b7]166 uint64_t ba;
167 unsigned cnt;
[84adbf0]168 int disk_id, i;
169
170 /* Get the device handle. */
171 dh = IPC_GET_ARG1(*icall);
172
173 /* Determine which disk device is the client connecting to. */
174 disk_id = -1;
175 for (i = 0; i < MAX_DISKS; i++)
176 if (dev_handle[i] == dh)
177 disk_id = i;
178
179 if (disk_id < 0) {
180 ipc_answer_0(iid, EINVAL);
181 return;
182 }
[44a53fd]183
184 /* Answer the IPC_M_CONNECT_ME_TO call. */
185 ipc_answer_0(iid, EOK);
186
[0da4e41]187 if (!async_share_out_receive(&callid, &comm_size, &flags)) {
[44a53fd]188 ipc_answer_0(callid, EHANGUP);
189 return;
190 }
191
[1ee00b7]192 if (comm_size < block_size) {
193 ipc_answer_0(callid, EHANGUP);
194 return;
195 }
196
[3ecc02e]197 fs_va = as_get_mappable_page(comm_size);
[44a53fd]198 if (fs_va == NULL) {
199 ipc_answer_0(callid, EHANGUP);
200 return;
201 }
202
[0da4e41]203 (void) async_share_out_finalize(callid, fs_va);
[44a53fd]204
205 while (1) {
206 callid = async_get_call(&call);
[3ecc02e]207 method = IPC_GET_METHOD(call);
208 switch (method) {
[44a53fd]209 case IPC_M_PHONE_HUNGUP:
210 /* The other side has hung up. */
211 ipc_answer_0(callid, EOK);
212 return;
[1ee00b7]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;
[3ecc02e]219 break;
220 }
[1ee00b7]221 retval = gxe_bd_read_blocks(disk_id, ba, cnt, fs_va);
[44a53fd]222 break;
[1ee00b7]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 ipc_answer_1(callid, EOK, block_size);
235 continue;
[08232ee]236 case BD_GET_NUM_BLOCKS:
237 retval = ENOTSUP;
238 break;
[44a53fd]239 default:
240 retval = EINVAL;
241 break;
242 }
243 ipc_answer_0(callid, retval);
244 }
245}
246
[1ee00b7]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
[3ecc02e]251 int rc;
252
[1ee00b7]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 }
[3ecc02e]262
[1ee00b7]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;
[3ecc02e]271
[1ee00b7]272 while (cnt > 0) {
273 rc = gxe_bd_write_block(disk_id, ba, buf);
[3ecc02e]274 if (rc != EOK)
275 return rc;
276
[1ee00b7]277 ++ba;
278 --cnt;
[3ecc02e]279 buf += block_size;
280 }
281
282 return EOK;
283}
284
[1ee00b7]285/** Read a block from the device. */
286static int gxe_bd_read_block(int disk_id, uint64_t ba, void *buf)
[44a53fd]287{
288 uint32_t status;
[1ee00b7]289 uint64_t byte_addr;
[44a53fd]290 size_t i;
291 uint32_t w;
292
[1ee00b7]293 byte_addr = ba * block_size;
294
[12956e57]295 fibril_mutex_lock(&dev_lock[disk_id]);
[1ee00b7]296 pio_write_32(&dev->offset_lo, (uint32_t) byte_addr);
297 pio_write_32(&dev->offset_hi, byte_addr >> 32);
[44a53fd]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) {
[12956e57]303 fibril_mutex_unlock(&dev_lock[disk_id]);
[44a53fd]304 return EIO;
305 }
306
[1ee00b7]307 for (i = 0; i < block_size; i++) {
[39580667]308 ((uint8_t *) buf)[i] = w = pio_read_8(&dev->buffer[i]);
[44a53fd]309 }
310
[12956e57]311 fibril_mutex_unlock(&dev_lock[disk_id]);
[44a53fd]312 return EOK;
313}
314
[1ee00b7]315/** Write a block to the device. */
316static int gxe_bd_write_block(int disk_id, uint64_t ba, const void *buf)
[44a53fd]317{
318 uint32_t status;
[1ee00b7]319 uint64_t byte_addr;
[44a53fd]320 size_t i;
321
[1ee00b7]322 byte_addr = ba * block_size;
323
324 fibril_mutex_lock(&dev_lock[disk_id]);
325
326 for (i = 0; i < block_size; i++) {
[39580667]327 pio_write_8(&dev->buffer[i], ((const uint8_t *) buf)[i]);
[44a53fd]328 }
329
[1ee00b7]330 pio_write_32(&dev->offset_lo, (uint32_t) byte_addr);
331 pio_write_32(&dev->offset_hi, byte_addr >> 32);
[44a53fd]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) {
[12956e57]337 fibril_mutex_unlock(&dev_lock[disk_id]);
[44a53fd]338 return EIO;
339 }
340
[12956e57]341 fibril_mutex_unlock(&dev_lock[disk_id]);
[44a53fd]342 return EOK;
343}
344
345/**
346 * @}
347 */
Note: See TracBrowser for help on using the repository browser.