source: mainline/uspace/srv/bd/gxe_bd/gxe_bd.c@ 875c629

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 875c629 was 7ea7db31, checked in by Jakub Jermar <jakub@…>, 15 years ago

Cease using devmap_get_phone() and devmap_hangup_phone() in drivers.

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