2
* Copyright (c) 2009 Jiri Svoboda
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
35
* @brief GXemul disk driver
39
#include <libarch/ddi.h>
45
#include <fibril_sync.h>
47
#include <sys/types.h>
87
static const size_t block_size = 512;
88
static size_t comm_size;
90
static uintptr_t dev_physical = 0x13000000;
93
static dev_handle_t dev_handle[MAX_DISKS];
95
static fibril_mutex_t dev_lock[MAX_DISKS];
97
static int gxe_bd_init(void);
98
static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
99
static int gx_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, size_t size,
101
static int gxe_bd_read_block(int disk_id, uint64_t offset, size_t size,
103
static int gxe_bd_write_block(int disk_id, uint64_t offset, size_t size,
106
int main(int argc, char **argv)
108
printf(NAME ": GXemul disk driver\n");
110
if (gxe_bd_init() != EOK)
113
printf(NAME ": Accepting connections\n");
121
static int gxe_bd_init(void)
127
rc = devmap_driver_register(NAME, gxe_bd_connection);
129
printf(NAME ": Unable to register driver.\n");
133
rc = pio_enable((void *) dev_physical, sizeof(gxe_bd_t), &vaddr);
135
printf(NAME ": Could not initialize device I/O space.\n");
141
for (i = 0; i < MAX_DISKS; i++) {
142
snprintf(name, 16, "disk%d", i);
143
rc = devmap_device_register(name, &dev_handle[i]);
145
devmap_hangup_phone(DEVMAP_DRIVER);
146
printf(NAME ": Unable to register device %s.\n",
150
fibril_mutex_initialize(&dev_lock[i]);
156
static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
169
/* Get the device handle. */
170
dh = IPC_GET_ARG1(*icall);
172
/* Determine which disk device is the client connecting to. */
174
for (i = 0; i < MAX_DISKS; i++)
175
if (dev_handle[i] == dh)
179
ipc_answer_0(iid, EINVAL);
183
/* Answer the IPC_M_CONNECT_ME_TO call. */
184
ipc_answer_0(iid, EOK);
186
if (!ipc_share_out_receive(&callid, &comm_size, &flags)) {
187
ipc_answer_0(callid, EHANGUP);
191
fs_va = as_get_mappable_page(comm_size);
193
ipc_answer_0(callid, EHANGUP);
197
(void) ipc_share_out_finalize(callid, fs_va);
200
callid = async_get_call(&call);
201
method = IPC_GET_METHOD(call);
203
case IPC_M_PHONE_HUNGUP:
204
/* The other side has hung up. */
205
ipc_answer_0(callid, EOK);
209
idx = IPC_GET_ARG1(call);
210
size = IPC_GET_ARG2(call);
211
if (size > comm_size) {
215
retval = gx_bd_rdwr(disk_id, method, idx * size,
222
ipc_answer_0(callid, retval);
226
static int gx_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, size_t size,
233
now = size < block_size ? size : block_size;
235
if (method == BD_READ_BLOCK)
236
rc = gxe_bd_read_block(disk_id, offset, now, buf);
238
rc = gxe_bd_write_block(disk_id, offset, now, buf);
244
offset += block_size;
246
if (size > block_size)
255
static int gxe_bd_read_block(int disk_id, uint64_t offset, size_t size,
262
fibril_mutex_lock(&dev_lock[disk_id]);
263
pio_write_32(&dev->offset_lo, (uint32_t) offset);
264
pio_write_32(&dev->offset_hi, offset >> 32);
265
pio_write_32(&dev->disk_id, disk_id);
266
pio_write_32(&dev->control, CTL_READ_START);
268
status = pio_read_32(&dev->status);
269
if (status == STATUS_FAILURE) {
270
fibril_mutex_unlock(&dev_lock[disk_id]);
274
for (i = 0; i < size; i++) {
275
((uint8_t *) buf)[i] = w = pio_read_8(&dev->buffer[i]);
278
fibril_mutex_unlock(&dev_lock[disk_id]);
282
static int gxe_bd_write_block(int disk_id, uint64_t offset, size_t size,
288
for (i = 0; i < size; i++) {
289
pio_write_8(&dev->buffer[i], ((const uint8_t *) buf)[i]);
292
fibril_mutex_lock(&dev_lock[disk_id]);
293
pio_write_32(&dev->offset_lo, (uint32_t) offset);
294
pio_write_32(&dev->offset_hi, offset >> 32);
295
pio_write_32(&dev->disk_id, disk_id);
296
pio_write_32(&dev->control, CTL_WRITE_START);
298
status = pio_read_32(&dev->status);
299
if (status == STATUS_FAILURE) {
300
fibril_mutex_unlock(&dev_lock[disk_id]);
304
fibril_mutex_unlock(&dev_lock[disk_id]);