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 libdrv
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <as.h>
|
---|
36 | #include <async.h>
|
---|
37 | #include <devman.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <macros.h>
|
---|
41 | #include "ahci_iface.h"
|
---|
42 | #include "ddf/driver.h"
|
---|
43 |
|
---|
44 | typedef enum {
|
---|
45 | IPC_M_AHCI_GET_SATA_DEVICE_NAME,
|
---|
46 | IPC_M_AHCI_GET_NUM_BLOCKS,
|
---|
47 | IPC_M_AHCI_GET_BLOCK_SIZE,
|
---|
48 | IPC_M_AHCI_READ_BLOCKS,
|
---|
49 | IPC_M_AHCI_WRITE_BLOCKS
|
---|
50 | } ahci_iface_funcs_t;
|
---|
51 |
|
---|
52 | #define MAX_NAME_LENGTH 1024
|
---|
53 |
|
---|
54 | #define LO(ptr) \
|
---|
55 | ((uint32_t) (((uint64_t) ((uintptr_t) (ptr))) & 0xffffffff))
|
---|
56 |
|
---|
57 | #define HI(ptr) \
|
---|
58 | ((uint32_t) (((uint64_t) ((uintptr_t) (ptr))) >> 32))
|
---|
59 |
|
---|
60 | async_sess_t* ahci_get_sess(devman_handle_t funh, char **name)
|
---|
61 | {
|
---|
62 | // FIXME: Use a better way than substring match
|
---|
63 |
|
---|
64 | *name = NULL;
|
---|
65 |
|
---|
66 | char devn[MAX_NAME_LENGTH];
|
---|
67 | int rc = devman_fun_get_name(funh, devn, MAX_NAME_LENGTH);
|
---|
68 | if (rc != EOK)
|
---|
69 | return NULL;
|
---|
70 |
|
---|
71 | size_t devn_size = str_size(devn);
|
---|
72 |
|
---|
73 | if ((devn_size > 5) && (str_lcmp(devn, "ahci_", 5) == 0)) {
|
---|
74 | async_sess_t *sess = devman_device_connect(EXCHANGE_PARALLEL,
|
---|
75 | funh, IPC_FLAG_BLOCKING);
|
---|
76 |
|
---|
77 | if (sess) {
|
---|
78 | *name = str_dup(devn);
|
---|
79 | return sess;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | return NULL;
|
---|
84 | }
|
---|
85 |
|
---|
86 | int ahci_get_sata_device_name(async_sess_t *sess, size_t sata_dev_name_length,
|
---|
87 | char *sata_dev_name)
|
---|
88 | {
|
---|
89 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
90 | if (!exch)
|
---|
91 | return EINVAL;
|
---|
92 |
|
---|
93 | aid_t req = async_send_2(exch, DEV_IFACE_ID(AHCI_DEV_IFACE),
|
---|
94 | IPC_M_AHCI_GET_SATA_DEVICE_NAME, sata_dev_name_length, NULL);
|
---|
95 |
|
---|
96 | async_data_read_start(exch, sata_dev_name, sata_dev_name_length);
|
---|
97 |
|
---|
98 | sysarg_t rc;
|
---|
99 | async_wait_for(req, &rc);
|
---|
100 |
|
---|
101 | return rc;
|
---|
102 | }
|
---|
103 |
|
---|
104 | int ahci_get_num_blocks(async_sess_t *sess, uint64_t *blocks)
|
---|
105 | {
|
---|
106 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
107 | if (!exch)
|
---|
108 | return EINVAL;
|
---|
109 |
|
---|
110 | sysarg_t blocks_hi;
|
---|
111 | sysarg_t blocks_lo;
|
---|
112 | int rc = async_req_1_2(exch, DEV_IFACE_ID(AHCI_DEV_IFACE),
|
---|
113 | IPC_M_AHCI_GET_NUM_BLOCKS, &blocks_hi, &blocks_lo);
|
---|
114 |
|
---|
115 | async_exchange_end(exch);
|
---|
116 |
|
---|
117 | if (rc == EOK) {
|
---|
118 | *blocks = (((uint64_t) blocks_hi) << 32)
|
---|
119 | | (((uint64_t) blocks_lo) & 0xffffffff);
|
---|
120 | }
|
---|
121 |
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 |
|
---|
125 | int ahci_get_block_size(async_sess_t *sess, size_t *blocks_size)
|
---|
126 | {
|
---|
127 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
128 | if (!exch)
|
---|
129 | return EINVAL;
|
---|
130 |
|
---|
131 | sysarg_t bs;
|
---|
132 | int rc = async_req_1_1(exch, DEV_IFACE_ID(AHCI_DEV_IFACE),
|
---|
133 | IPC_M_AHCI_GET_BLOCK_SIZE, &bs);
|
---|
134 |
|
---|
135 | async_exchange_end(exch);
|
---|
136 |
|
---|
137 | if (rc == EOK)
|
---|
138 | *blocks_size = (size_t) bs;
|
---|
139 |
|
---|
140 | return rc;
|
---|
141 | }
|
---|
142 |
|
---|
143 | int ahci_read_blocks(async_sess_t *sess, uint64_t blocknum, size_t count,
|
---|
144 | void *buf)
|
---|
145 | {
|
---|
146 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
147 | if (!exch)
|
---|
148 | return EINVAL;
|
---|
149 |
|
---|
150 | aid_t req;
|
---|
151 | req = async_send_4(exch, DEV_IFACE_ID(AHCI_DEV_IFACE),
|
---|
152 | IPC_M_AHCI_READ_BLOCKS, HI(blocknum), LO(blocknum), count, NULL);
|
---|
153 |
|
---|
154 | async_share_out_start(exch, buf, AS_AREA_READ | AS_AREA_WRITE);
|
---|
155 |
|
---|
156 | async_exchange_end(exch);
|
---|
157 |
|
---|
158 | sysarg_t rc;
|
---|
159 | async_wait_for(req, &rc);
|
---|
160 |
|
---|
161 | return rc;
|
---|
162 | }
|
---|
163 |
|
---|
164 | int ahci_write_blocks(async_sess_t *sess, uint64_t blocknum, size_t count,
|
---|
165 | void* buf)
|
---|
166 | {
|
---|
167 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
168 | if (!exch)
|
---|
169 | return EINVAL;
|
---|
170 |
|
---|
171 | aid_t req = async_send_4(exch, DEV_IFACE_ID(AHCI_DEV_IFACE),
|
---|
172 | IPC_M_AHCI_WRITE_BLOCKS, HI(blocknum), LO(blocknum), count, NULL);
|
---|
173 |
|
---|
174 | async_share_out_start(exch, buf, AS_AREA_READ | AS_AREA_WRITE);
|
---|
175 |
|
---|
176 | async_exchange_end(exch);
|
---|
177 |
|
---|
178 | sysarg_t rc;
|
---|
179 | async_wait_for(req, &rc);
|
---|
180 |
|
---|
181 | return rc;
|
---|
182 | }
|
---|
183 |
|
---|
184 | static void remote_ahci_get_sata_device_name(ddf_fun_t *, void *, ipc_callid_t,
|
---|
185 | ipc_call_t *);
|
---|
186 | static void remote_ahci_get_num_blocks(ddf_fun_t *, void *, ipc_callid_t,
|
---|
187 | ipc_call_t *);
|
---|
188 | static void remote_ahci_get_block_size(ddf_fun_t *, void *, ipc_callid_t,
|
---|
189 | ipc_call_t *);
|
---|
190 | static void remote_ahci_read_blocks(ddf_fun_t *, void *, ipc_callid_t,
|
---|
191 | ipc_call_t *);
|
---|
192 | static void remote_ahci_write_blocks(ddf_fun_t *, void *, ipc_callid_t,
|
---|
193 | ipc_call_t *);
|
---|
194 |
|
---|
195 | /** Remote AHCI interface operations. */
|
---|
196 | static const remote_iface_func_ptr_t remote_ahci_iface_ops [] = {
|
---|
197 | [IPC_M_AHCI_GET_SATA_DEVICE_NAME] = remote_ahci_get_sata_device_name,
|
---|
198 | [IPC_M_AHCI_GET_NUM_BLOCKS] = remote_ahci_get_num_blocks,
|
---|
199 | [IPC_M_AHCI_GET_BLOCK_SIZE] = remote_ahci_get_block_size,
|
---|
200 | [IPC_M_AHCI_READ_BLOCKS] = remote_ahci_read_blocks,
|
---|
201 | [IPC_M_AHCI_WRITE_BLOCKS] = remote_ahci_write_blocks
|
---|
202 | };
|
---|
203 |
|
---|
204 | /** Remote AHCI interface structure.
|
---|
205 | */
|
---|
206 | const remote_iface_t remote_ahci_iface = {
|
---|
207 | .method_count = ARRAY_SIZE(remote_ahci_iface_ops),
|
---|
208 | .methods = remote_ahci_iface_ops
|
---|
209 | };
|
---|
210 |
|
---|
211 | void remote_ahci_get_sata_device_name(ddf_fun_t *fun, void *iface,
|
---|
212 | ipc_callid_t callid, ipc_call_t *call)
|
---|
213 | {
|
---|
214 | const ahci_iface_t *ahci_iface = (ahci_iface_t *) iface;
|
---|
215 |
|
---|
216 | if (ahci_iface->get_sata_device_name == NULL) {
|
---|
217 | async_answer_0(callid, ENOTSUP);
|
---|
218 | return;
|
---|
219 | }
|
---|
220 |
|
---|
221 | const size_t sata_dev_name_length =
|
---|
222 | (size_t) DEV_IPC_GET_ARG1(*call);
|
---|
223 |
|
---|
224 | char* sata_dev_name = malloc(sata_dev_name_length);
|
---|
225 |
|
---|
226 | const int ret = ahci_iface->get_sata_device_name(fun,
|
---|
227 | sata_dev_name_length, sata_dev_name);
|
---|
228 |
|
---|
229 | size_t real_size;
|
---|
230 | ipc_callid_t cid;
|
---|
231 | if ((async_data_read_receive(&cid, &real_size)) &&
|
---|
232 | (real_size == sata_dev_name_length))
|
---|
233 | async_data_read_finalize(cid, sata_dev_name, sata_dev_name_length);
|
---|
234 |
|
---|
235 | async_answer_0(callid, ret);
|
---|
236 | }
|
---|
237 |
|
---|
238 | static void remote_ahci_get_num_blocks(ddf_fun_t *fun, void *iface,
|
---|
239 | ipc_callid_t callid, ipc_call_t *call)
|
---|
240 | {
|
---|
241 | const ahci_iface_t *ahci_iface = (ahci_iface_t *) iface;
|
---|
242 |
|
---|
243 | if (ahci_iface->get_num_blocks == NULL) {
|
---|
244 | async_answer_0(callid, ENOTSUP);
|
---|
245 | return;
|
---|
246 | }
|
---|
247 |
|
---|
248 | uint64_t blocks;
|
---|
249 | const int ret = ahci_iface->get_num_blocks(fun, &blocks);
|
---|
250 |
|
---|
251 | if (ret != EOK)
|
---|
252 | async_answer_0(callid, ret);
|
---|
253 | else
|
---|
254 | async_answer_2(callid, EOK, HI(blocks), LO(blocks));
|
---|
255 | }
|
---|
256 |
|
---|
257 | static void remote_ahci_get_block_size(ddf_fun_t *fun, void *iface,
|
---|
258 | ipc_callid_t callid, ipc_call_t *call)
|
---|
259 | {
|
---|
260 | const ahci_iface_t *ahci_iface = (ahci_iface_t *) iface;
|
---|
261 |
|
---|
262 | if (ahci_iface->get_block_size == NULL) {
|
---|
263 | async_answer_0(callid, ENOTSUP);
|
---|
264 | return;
|
---|
265 | }
|
---|
266 |
|
---|
267 | size_t blocks;
|
---|
268 | const int ret = ahci_iface->get_block_size(fun, &blocks);
|
---|
269 |
|
---|
270 | if (ret != EOK)
|
---|
271 | async_answer_0(callid, ret);
|
---|
272 | else
|
---|
273 | async_answer_1(callid, EOK, blocks);
|
---|
274 | }
|
---|
275 |
|
---|
276 | void remote_ahci_read_blocks(ddf_fun_t *fun, void *iface,
|
---|
277 | ipc_callid_t callid, ipc_call_t *call)
|
---|
278 | {
|
---|
279 | const ahci_iface_t *ahci_iface = (ahci_iface_t *) iface;
|
---|
280 |
|
---|
281 | if (ahci_iface->read_blocks == NULL) {
|
---|
282 | async_answer_0(callid, ENOTSUP);
|
---|
283 | return;
|
---|
284 | }
|
---|
285 |
|
---|
286 | size_t maxblock_size;
|
---|
287 | unsigned int flags;
|
---|
288 |
|
---|
289 | ipc_callid_t cid;
|
---|
290 | async_share_out_receive(&cid, &maxblock_size, &flags);
|
---|
291 |
|
---|
292 | void *buf;
|
---|
293 | async_share_out_finalize(cid, &buf);
|
---|
294 |
|
---|
295 | const uint64_t blocknum =
|
---|
296 | (((uint64_t) (DEV_IPC_GET_ARG1(*call))) << 32) |
|
---|
297 | (((uint64_t) (DEV_IPC_GET_ARG2(*call))) & 0xffffffff);
|
---|
298 | const size_t cnt = (size_t) DEV_IPC_GET_ARG3(*call);
|
---|
299 |
|
---|
300 | const int ret = ahci_iface->read_blocks(fun, blocknum, cnt, buf);
|
---|
301 |
|
---|
302 | async_answer_0(callid, ret);
|
---|
303 | }
|
---|
304 |
|
---|
305 | void remote_ahci_write_blocks(ddf_fun_t *fun, void *iface, ipc_callid_t callid,
|
---|
306 | ipc_call_t *call)
|
---|
307 | {
|
---|
308 | const ahci_iface_t *ahci_iface = (ahci_iface_t *) iface;
|
---|
309 |
|
---|
310 | if (ahci_iface->read_blocks == NULL) {
|
---|
311 | async_answer_0(callid, ENOTSUP);
|
---|
312 | return;
|
---|
313 | }
|
---|
314 |
|
---|
315 | size_t maxblock_size;
|
---|
316 | unsigned int flags;
|
---|
317 |
|
---|
318 | ipc_callid_t cid;
|
---|
319 | async_share_out_receive(&cid, &maxblock_size, &flags);
|
---|
320 |
|
---|
321 | void *buf;
|
---|
322 | async_share_out_finalize(cid, &buf);
|
---|
323 |
|
---|
324 | const uint64_t blocknum =
|
---|
325 | (((uint64_t)(DEV_IPC_GET_ARG1(*call))) << 32) |
|
---|
326 | (((uint64_t)(DEV_IPC_GET_ARG2(*call))) & 0xffffffff);
|
---|
327 | const size_t cnt = (size_t) DEV_IPC_GET_ARG3(*call);
|
---|
328 |
|
---|
329 | const int ret = ahci_iface->write_blocks(fun, blocknum, cnt, buf);
|
---|
330 |
|
---|
331 | async_answer_0(callid, ret);
|
---|
332 | }
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * @}
|
---|
336 | */
|
---|