source: mainline/uspace/srv/bd/sata_bd/sata_bd.c@ 1938b381

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1938b381 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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