source: mainline/uspace/srv/bd/sata_bd/sata_bd.c@ dbf2ca4

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

libblock.{c|h} → block.{c|h}

  • Property mode set to 100644
File size: 6.7 KB
RevLine 
[9904eb90]1/*
2 * Copyright (c) 2012 Petr Jerman
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 SATA disk driver
36 *
37 */
38
39#include <sys/types.h>
[4802dd7]40#include <bd_srv.h>
[9904eb90]41#include <errno.h>
42#include <stdio.h>
43#include <str.h>
44#include <loc.h>
45#include <macros.h>
46
47#include <device/ahci.h>
48#include "sata_bd.h"
49
50#define NAME "sata_bd"
51#define NAMESPACE "bd"
52
[ae3ff9f5]53/** Maximum number of disks handled */
54#define MAXDISKS 256
[9904eb90]55
[ae3ff9f5]56static sata_bd_dev_t disk[MAXDISKS];
[9904eb90]57static int disk_count;
58
[135486d]59static int sata_bd_open(bd_srvs_t *, bd_srv_t *);
[4802dd7]60static int sata_bd_close(bd_srv_t *);
61static int sata_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
62static int sata_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
63static int sata_bd_get_block_size(bd_srv_t *, size_t *);
64static int sata_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
65
66static bd_ops_t sata_bd_ops = {
67 .open = sata_bd_open,
68 .close = sata_bd_close,
69 .read_blocks = sata_bd_read_blocks,
70 .write_blocks = sata_bd_write_blocks,
71 .get_block_size = sata_bd_get_block_size,
72 .get_num_blocks = sata_bd_get_num_blocks
73};
74
75static sata_bd_dev_t *bd_srv_sata(bd_srv_t *bd)
76{
[f73b291]77 return (sata_bd_dev_t *) bd->srvs->sarg;
[4802dd7]78}
79
[ae3ff9f5]80/** Find SATA devices in device tree.
81 *
82 * @param Device manager handle describing container for searching.
83 *
84 * @return EOK if succeed, error code otherwise.
85 *
86 */
[9904eb90]87static int scan_device_tree(devman_handle_t funh)
88{
89 devman_handle_t devh;
90 devman_handle_t *cfuns;
91 size_t count, i;
92 int rc;
93
94 /* If device is SATA, add device to the disk array. */
95 disk[disk_count].sess = ahci_get_sess(funh, &disk[disk_count].dev_name);
96 if(disk[disk_count].sess != NULL) {
97
98 ahci_get_sata_device_name(disk[disk_count].sess,
99 SATA_DEV_NAME_LENGTH, disk[disk_count].sata_dev_name);
100
101 ahci_get_block_size(disk[disk_count].sess,
102 &disk[disk_count].block_size);
103
104 ahci_get_num_blocks(disk[disk_count].sess, &disk[disk_count].blocks);
[4802dd7]105
[135486d]106 bd_srvs_init(&disk[disk_count].bds);
107 disk[disk_count].bds.ops = &sata_bd_ops;
108 disk[disk_count].bds.sarg = &disk[disk_count];
[4802dd7]109
[9904eb90]110 printf("Device %s - %s , blocks: %lu, block_size: %lu\n",
111 disk[disk_count].dev_name, disk[disk_count].sata_dev_name,
112 (long unsigned int) disk[disk_count].blocks,
113 (long unsigned int) disk[disk_count].block_size);
114
115 ++disk_count;
116 }
117
118 /* search children */
119 rc = devman_fun_get_child(funh, &devh);
120 if (rc == ENOENT)
121 return EOK;
122
123 if (rc != EOK) {
124 printf(NAME ": Failed getting child device for function %s.\n", "xxx");
125 return rc;
126 }
127
128 rc = devman_dev_get_functions(devh, &cfuns, &count);
129 if (rc != EOK) {
130 printf(NAME ": Failed getting list of functions for device %s.\n",
131 "xxx");
132 return rc;
133 }
134
135 for (i = 0; i < count; i++)
136 scan_device_tree(cfuns[i]);
137
138 free(cfuns);
139 return EOK;
140}
141
[ae3ff9f5]142/** Find sata devices in device tree from root.
143 *
144 * @return EOK if succeed, error code otherwise.
145 *
146 */
[9904eb90]147static int get_sata_disks()
148{
149 devman_handle_t root_fun;
150 int rc;
151
152 disk_count = 0;
153
154 rc = devman_fun_get_handle("/", &root_fun, 0);
155 if (rc != EOK) {
156 printf(NAME ": Error resolving root function.\n");
157 return EIO;
158 }
159
160 scan_device_tree(root_fun);
161
162 return EOK;
163}
164
165/** Block device connection handler. */
166static void sata_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
167{
168 service_id_t dsid;
169 int disk_id, i;
170
171 /* Get the device service ID. */
172 dsid = IPC_GET_ARG1(*icall);
173
174 /* Determine which disk device is the client connecting to. */
175 disk_id = -1;
176 for (i = 0; i < MAXDISKS; i++)
177 if (disk[i].service_id == dsid)
178 disk_id = i;
179
180 if (disk_id < 0) {
181 async_answer_0(iid, EINVAL);
182 return;
183 }
184
[135486d]185 bd_conn(iid, icall, &disk[disk_id].bds);
[4802dd7]186}
[9904eb90]187
[4802dd7]188/** Open device. */
[135486d]189static int sata_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
[4802dd7]190{
191 return EOK;
192}
[9904eb90]193
[4802dd7]194/** Close device. */
195static int sata_bd_close(bd_srv_t *bd)
196{
197 return EOK;
198}
[9904eb90]199
[4802dd7]200/** Read blocks from partition. */
201static int sata_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt, void *buf,
202 size_t size)
203{
204 sata_bd_dev_t *sbd = bd_srv_sata(bd);
205
206 if (size < cnt * sbd->block_size)
207 return EINVAL;
208
209 return ahci_read_blocks(sbd->sess, ba, cnt, buf);
210}
211
212/** Write blocks to partition. */
213static int sata_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
214 const void *buf, size_t size)
215{
216 sata_bd_dev_t *sbd = bd_srv_sata(bd);
217
218 if (size < cnt * sbd->block_size)
219 return EINVAL;
220
221 return ahci_write_blocks(sbd->sess, ba, cnt, (void *)buf);
222}
223
224/** Get device block size. */
225static int sata_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
226{
227 sata_bd_dev_t *sbd = bd_srv_sata(bd);
228
229 *rsize = sbd->block_size;
230 return EOK;
231}
232
233/** Get number of blocks on device. */
234static int sata_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
235{
236 sata_bd_dev_t *sbd = bd_srv_sata(bd);
237
238 *rnb = sbd->blocks;
239 return EOK;
[9904eb90]240}
241
[4802dd7]242
[9904eb90]243int main(int argc, char **argv)
244{
245 int rc;
246
247 async_set_client_connection(sata_bd_connection);
248 rc = loc_server_register(NAME);
249 if (rc < 0) {
250 printf(NAME ": Unable to register driver.\n");
251 return rc;
252 }
253
254 rc = get_sata_disks();
255 if (rc != EOK) {
256 return rc;
257 }
258
259 for(int i=0; i < disk_count; i++) {
260 char name[1024];
261 snprintf(name, 1024, "%s/%s", NAMESPACE, disk[i].dev_name);
262 rc = loc_service_register(name, &disk[i].service_id);
263 if (rc != EOK) {
264 printf(NAME ": Unable to register device %s.\n", name);
265 return rc;
266 }
267 }
268
269 printf(NAME ": Accepting connections\n");
270 task_retval(0);
271 async_manager();
272
273 /* Not reached */
274 return 0;
275}
276
277/**
278 * @}
279 */
Note: See TracBrowser for help on using the repository browser.