Subversion Repositories HelenOS

Rev

Rev 4623 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4623 Rev 4687
1
/*
1
/*
2
 * Copyright (c) 2009 Jiri Svoboda
2
 * Copyright (c) 2009 Jiri Svoboda
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup bd
29
/** @addtogroup bd
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief ATA disk driver
35
 * @brief ATA disk driver
36
 *
36
 *
37
 * This driver currently works only with CHS addressing and uses PIO.
37
 * This driver currently works only with CHS addressing and uses PIO.
38
 * Currently based on the (now obsolete) ANSI X3.221-1994 (ATA-1) standard.
38
 * Currently based on the (now obsolete) ANSI X3.221-1994 (ATA-1) standard.
39
 * At this point only reading is possible, not writing.
39
 * At this point only reading is possible, not writing.
40
 *
40
 *
41
 * The driver services a single controller which can have up to two disks
41
 * The driver services a single controller which can have up to two disks
42
 * attached.
42
 * attached.
43
 */
43
 */
44
 
44
 
45
#include <stdio.h>
45
#include <stdio.h>
46
#include <libarch/ddi.h>
46
#include <libarch/ddi.h>
47
#include <ddi.h>
47
#include <ddi.h>
48
#include <ipc/ipc.h>
48
#include <ipc/ipc.h>
49
#include <ipc/bd.h>
49
#include <ipc/bd.h>
50
#include <async.h>
50
#include <async.h>
51
#include <as.h>
51
#include <as.h>
52
#include <fibril_sync.h>
52
#include <fibril_sync.h>
53
#include <devmap.h>
53
#include <devmap.h>
54
#include <sys/types.h>
54
#include <sys/types.h>
55
#include <errno.h>
55
#include <errno.h>
56
#include <bool.h>
56
#include <bool.h>
57
#include <task.h>
57
#include <task.h>
58
 
58
 
59
#include "ata_bd.h"
59
#include "ata_bd.h"
60
 
60
 
61
#define NAME "ata_bd"
61
#define NAME "ata_bd"
62
 
62
 
63
static const size_t block_size = 512;
63
static const size_t block_size = 512;
64
static size_t comm_size;
64
static size_t comm_size;
65
 
65
 
66
static uintptr_t cmd_physical = 0x1f0;
66
static uintptr_t cmd_physical = 0x1f0;
67
static uintptr_t ctl_physical = 0x170;
67
static uintptr_t ctl_physical = 0x170;
68
static ata_cmd_t *cmd;
68
static ata_cmd_t *cmd;
69
static ata_ctl_t *ctl;
69
static ata_ctl_t *ctl;
70
 
70
 
71
/** Per-disk state. */
71
/** Per-disk state. */
72
static disk_t disk[MAX_DISKS];
72
static disk_t disk[MAX_DISKS];
73
 
73
 
74
static int ata_bd_init(void);
74
static int ata_bd_init(void);
75
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
75
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
76
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, size_t size,
76
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, size_t size,
77
    void *buf);
77
    void *buf);
78
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
78
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
79
    void *buf);
79
    void *buf);
80
static int ata_bd_write_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
80
static int ata_bd_write_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
81
    const void *buf);
81
    const void *buf);
82
static int drive_identify(int drive_id, disk_t *d);
82
static int drive_identify(int drive_id, disk_t *d);
83
 
83
 
84
int main(int argc, char **argv)
84
int main(int argc, char **argv)
85
{
85
{
86
    uint8_t status;
86
    uint8_t status;
87
    char name[16];
87
    char name[16];
88
    int i, rc;
88
    int i, rc;
89
    int n_disks;
89
    int n_disks;
90
 
90
 
91
    printf(NAME ": ATA disk driver\n");
91
    printf(NAME ": ATA disk driver\n");
92
 
92
 
93
    printf("I/O address 0x%x\n", cmd_physical);
93
    printf("I/O address 0x%x\n", cmd_physical);
94
 
94
 
95
    if (ata_bd_init() != EOK)
95
    if (ata_bd_init() != EOK)
96
        return -1;
96
        return -1;
97
 
97
 
98
    /* Put drives to reset, disable interrupts. */
98
    /* Put drives to reset, disable interrupts. */
99
    printf("Reset drives... ");
99
    printf("Reset drives... ");
100
    fflush(stdout);
100
    fflush(stdout);
101
 
101
 
102
    pio_write_8(&ctl->device_control, DCR_SRST);
102
    pio_write_8(&ctl->device_control, DCR_SRST);
103
    /* FIXME: Find out how to do this properly. */
103
    /* FIXME: Find out how to do this properly. */
104
    async_usleep(100);
104
    async_usleep(100);
105
    pio_write_8(&ctl->device_control, 0);
105
    pio_write_8(&ctl->device_control, 0);
106
 
106
 
107
    do {
107
    do {
108
        status = pio_read_8(&cmd->status);
108
        status = pio_read_8(&cmd->status);
109
    } while ((status & SR_BSY) != 0);
109
    } while ((status & SR_BSY) != 0);
110
    printf("Done\n");
110
    printf("Done\n");
111
 
111
 
112
    (void) drive_identify(0, &disk[0]);
112
    (void) drive_identify(0, &disk[0]);
113
    (void) drive_identify(1, &disk[1]);
113
    (void) drive_identify(1, &disk[1]);
114
 
114
 
115
    n_disks = 0;
115
    n_disks = 0;
116
 
116
 
117
    for (i = 0; i < MAX_DISKS; i++) {
117
    for (i = 0; i < MAX_DISKS; i++) {
118
        /* Skip unattached drives. */
118
        /* Skip unattached drives. */
119
        if (disk[i].present == false)
119
        if (disk[i].present == false)
120
            continue;
120
            continue;
121
 
121
 
122
        snprintf(name, 16, "disk%d", i);
122
        snprintf(name, 16, "disk%d", i);
123
        rc = devmap_device_register(name, &disk[i].dev_handle);
123
        rc = devmap_device_register(name, &disk[i].dev_handle);
124
        if (rc != EOK) {
124
        if (rc != EOK) {
125
            devmap_hangup_phone(DEVMAP_DRIVER);
125
            devmap_hangup_phone(DEVMAP_DRIVER);
126
            printf(NAME ": Unable to register device %s.\n",
126
            printf(NAME ": Unable to register device %s.\n",
127
                name);
127
                name);
128
            return rc;
128
            return rc;
129
        }
129
        }
130
        ++n_disks;
130
        ++n_disks;
131
    }
131
    }
132
 
132
 
133
    if (n_disks == 0) {
133
    if (n_disks == 0) {
134
        printf("No disks detected.\n");
134
        printf("No disks detected.\n");
135
        return -1;
135
        return -1;
136
    }
136
    }
137
 
137
 
138
    printf(NAME ": Accepting connections\n");
138
    printf(NAME ": Accepting connections\n");
139
    task_retval(0);
139
    task_retval(0);
140
    async_manager();
140
    async_manager();
141
 
141
 
142
    /* Not reached */
142
    /* Not reached */
143
    return 0;
143
    return 0;
144
}
144
}
145
 
145
 
146
static int drive_identify(int disk_id, disk_t *d)
146
static int drive_identify(int disk_id, disk_t *d)
147
{
147
{
148
    uint16_t data;
148
    uint16_t data;
149
    uint8_t status;
149
    uint8_t status;
150
    size_t i;
150
    size_t i;
151
 
151
 
152
    printf("Identify drive %d... ", disk_id);
152
    printf("Identify drive %d... ", disk_id);
153
    fflush(stdout);
153
    fflush(stdout);
154
 
154
 
155
    pio_write_8(&cmd->drive_head, ((disk_id != 0) ? DHR_DRV : 0));
155
    pio_write_8(&cmd->drive_head, ((disk_id != 0) ? DHR_DRV : 0));
156
    async_usleep(100);
156
    async_usleep(100);
157
    pio_write_8(&cmd->command, CMD_IDENTIFY_DRIVE);
157
    pio_write_8(&cmd->command, CMD_IDENTIFY_DRIVE);
158
 
158
 
159
    status = pio_read_8(&cmd->status);
159
    status = pio_read_8(&cmd->status);
160
 
160
 
161
    d->present = false;
161
    d->present = false;
162
 
162
 
163
    /*
163
    /*
164
     * Detect if drive is present. This is Qemu only! Need to
164
     * Detect if drive is present. This is Qemu only! Need to
165
     * do the right thing to work with real drives.
165
     * do the right thing to work with real drives.
166
     */
166
     */
167
    if ((status & SR_DRDY) == 0) {
167
    if ((status & SR_DRDY) == 0) {
168
        printf("None attached.\n");
168
        printf("None attached.\n");
169
        return ENOENT;
169
        return ENOENT;
170
    }
170
    }
171
 
171
 
172
    for (i = 0; i < block_size / 2; i++) {
172
    for (i = 0; i < block_size / 2; i++) {
173
        do {
173
        do {
174
            status = pio_read_8(&cmd->status);
174
            status = pio_read_8(&cmd->status);
175
        } while ((status & SR_DRDY) == 0);
175
        } while ((status & SR_DRDY) == 0);
176
 
176
 
177
        data = pio_read_16(&cmd->data_port);
177
        data = pio_read_16(&cmd->data_port);
178
 
178
 
179
        switch (i) {
179
        switch (i) {
180
        case 1: d->cylinders = data; break;
180
        case 1: d->cylinders = data; break;
181
        case 3: d->heads = data; break;
181
        case 3: d->heads = data; break;
182
        case 6: d->sectors = data; break;
182
        case 6: d->sectors = data; break;
183
        }
183
        }
184
    }
184
    }
185
 
185
 
186
    d->blocks = d->cylinders * d->heads * d->sectors;
186
    d->blocks = d->cylinders * d->heads * d->sectors;
187
 
187
 
188
    printf("Geometry: %u cylinders, %u heads, %u sectors\n",
188
    printf("Geometry: %u cylinders, %u heads, %u sectors\n",
189
        d->cylinders, d->heads, d->sectors);
189
        d->cylinders, d->heads, d->sectors);
190
 
190
 
191
    d->present = true;
191
    d->present = true;
192
    fibril_mutex_initialize(&d->lock);
192
    fibril_mutex_initialize(&d->lock);
193
 
193
 
194
    return EOK;
194
    return EOK;
195
}
195
}
196
 
196
 
197
static int ata_bd_init(void)
197
static int ata_bd_init(void)
198
{
198
{
199
    void *vaddr;
199
    void *vaddr;
200
    int rc;
200
    int rc;
201
 
201
 
202
    rc = devmap_driver_register(NAME, ata_bd_connection);
202
    rc = devmap_driver_register(NAME, ata_bd_connection);
203
    if (rc < 0) {
203
    if (rc < 0) {
204
        printf(NAME ": Unable to register driver.\n");
204
        printf(NAME ": Unable to register driver.\n");
205
        return rc;
205
        return rc;
206
    }
206
    }
207
 
207
 
208
    rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr);
208
    rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr);
209
    if (rc != EOK) {
209
    if (rc != EOK) {
210
        printf(NAME ": Could not initialize device I/O space.\n");
210
        printf(NAME ": Could not initialize device I/O space.\n");
211
        return rc;
211
        return rc;
212
    }
212
    }
213
 
213
 
214
    cmd = vaddr;
214
    cmd = vaddr;
215
 
215
 
216
    rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr);
216
    rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr);
217
    if (rc != EOK) {
217
    if (rc != EOK) {
218
        printf(NAME ": Could not initialize device I/O space.\n");
218
        printf(NAME ": Could not initialize device I/O space.\n");
219
        return rc;
219
        return rc;
220
    }
220
    }
221
 
221
 
222
    ctl = vaddr;
222
    ctl = vaddr;
223
 
223
 
224
 
224
 
225
    return EOK;
225
    return EOK;
226
}
226
}
227
 
227
 
228
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
228
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
229
{
229
{
230
    void *fs_va = NULL;
230
    void *fs_va = NULL;
231
    ipc_callid_t callid;
231
    ipc_callid_t callid;
232
    ipc_call_t call;
232
    ipc_call_t call;
233
    ipcarg_t method;
233
    ipcarg_t method;
234
    dev_handle_t dh;
234
    dev_handle_t dh;
235
    int flags;
235
    int flags;
236
    int retval;
236
    int retval;
237
    off_t idx;
237
    off_t idx;
238
    size_t size;
238
    size_t size;
239
    int disk_id, i;
239
    int disk_id, i;
240
 
240
 
241
    /* Get the device handle. */
241
    /* Get the device handle. */
242
    dh = IPC_GET_ARG1(*icall);
242
    dh = IPC_GET_ARG1(*icall);
243
 
243
 
244
    /* Determine which disk device is the client connecting to. */
244
    /* Determine which disk device is the client connecting to. */
245
    disk_id = -1;
245
    disk_id = -1;
246
    for (i = 0; i < MAX_DISKS; i++)
246
    for (i = 0; i < MAX_DISKS; i++)
247
        if (disk[i].dev_handle == dh)
247
        if (disk[i].dev_handle == dh)
248
            disk_id = i;
248
            disk_id = i;
249
 
249
 
250
    if (disk_id < 0 || disk[disk_id].present == false) {
250
    if (disk_id < 0 || disk[disk_id].present == false) {
251
        ipc_answer_0(iid, EINVAL);
251
        ipc_answer_0(iid, EINVAL);
252
        return;
252
        return;
253
    }
253
    }
254
 
254
 
255
    /* Answer the IPC_M_CONNECT_ME_TO call. */
255
    /* Answer the IPC_M_CONNECT_ME_TO call. */
256
    ipc_answer_0(iid, EOK);
256
    ipc_answer_0(iid, EOK);
257
 
257
 
258
    if (!ipc_share_out_receive(&callid, &comm_size, &flags)) {
258
    if (!ipc_share_out_receive(&callid, &comm_size, &flags)) {
259
        ipc_answer_0(callid, EHANGUP);
259
        ipc_answer_0(callid, EHANGUP);
260
        return;
260
        return;
261
    }
261
    }
262
 
262
 
263
    fs_va = as_get_mappable_page(comm_size);
263
    fs_va = as_get_mappable_page(comm_size);
264
    if (fs_va == NULL) {
264
    if (fs_va == NULL) {
265
        ipc_answer_0(callid, EHANGUP);
265
        ipc_answer_0(callid, EHANGUP);
266
        return;
266
        return;
267
    }
267
    }
268
 
268
 
269
    (void) ipc_share_out_finalize(callid, fs_va);
269
    (void) ipc_share_out_finalize(callid, fs_va);
270
 
270
 
271
    while (1) {
271
    while (1) {
272
        callid = async_get_call(&call);
272
        callid = async_get_call(&call);
273
        method = IPC_GET_METHOD(call);
273
        method = IPC_GET_METHOD(call);
274
        switch (method) {
274
        switch (method) {
275
        case IPC_M_PHONE_HUNGUP:
275
        case IPC_M_PHONE_HUNGUP:
276
            /* The other side has hung up. */
276
            /* The other side has hung up. */
277
            ipc_answer_0(callid, EOK);
277
            ipc_answer_0(callid, EOK);
278
            return;
278
            return;
279
        case BD_READ_BLOCK:
279
        case BD_READ_BLOCK:
280
        case BD_WRITE_BLOCK:
280
        case BD_WRITE_BLOCK:
281
            idx = IPC_GET_ARG1(call);
281
            idx = IPC_GET_ARG1(call);
282
            size = IPC_GET_ARG2(call);
282
            size = IPC_GET_ARG2(call);
283
            if (size > comm_size) {
283
            if (size > comm_size) {
284
                retval = EINVAL;
284
                retval = EINVAL;
285
                break;
285
                break;
286
            }
286
            }
287
            retval = ata_bd_rdwr(disk_id, method, idx,
287
            retval = ata_bd_rdwr(disk_id, method, idx,
288
                size, fs_va);
288
                size, fs_va);
289
            break;
289
            break;
290
        default:
290
        default:
291
            retval = EINVAL;
291
            retval = EINVAL;
292
            break;
292
            break;
293
        }
293
        }
294
        ipc_answer_0(callid, retval);
294
        ipc_answer_0(callid, retval);
295
    }
295
    }
296
}
296
}
297
 
297
 
298
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t blk_idx, size_t size,
298
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t blk_idx, size_t size,
299
    void *buf)
299
    void *buf)
300
{
300
{
301
    int rc;
301
    int rc;
302
    size_t now;
302
    size_t now;
303
 
303
 
304
    while (size > 0) {
304
    while (size > 0) {
305
        now = size < block_size ? size : block_size;
305
        now = size < block_size ? size : block_size;
306
        if (now != block_size)
306
        if (now != block_size)
307
            return EINVAL;
307
            return EINVAL;
308
 
308
 
309
        if (method == BD_READ_BLOCK)
309
        if (method == BD_READ_BLOCK)
310
            rc = ata_bd_read_block(disk_id, blk_idx, 1, buf);
310
            rc = ata_bd_read_block(disk_id, blk_idx, 1, buf);
311
        else
311
        else
312
            rc = ata_bd_write_block(disk_id, blk_idx, 1, buf);
312
            rc = ata_bd_write_block(disk_id, blk_idx, 1, buf);
313
 
313
 
314
        if (rc != EOK)
314
        if (rc != EOK)
315
            return rc;
315
            return rc;
316
 
316
 
317
        buf += block_size;
317
        buf += block_size;
318
        blk_idx++;
318
        blk_idx++;
319
 
319
 
320
        if (size > block_size)
320
        if (size > block_size)
321
            size -= block_size;
321
            size -= block_size;
322
        else
322
        else
323
            size = 0;
323
            size = 0;
324
    }
324
    }
325
 
325
 
326
    return EOK;
326
    return EOK;
327
}
327
}
328
 
328
 
329
 
329
 
330
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
330
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
331
    void *buf)
331
    void *buf)
332
{
332
{
333
    size_t i;
333
    size_t i;
334
    uint16_t data;
334
    uint16_t data;
335
    uint8_t status;
335
    uint8_t status;
336
    uint64_t c, h, s;
336
    uint64_t c, h, s;
337
    uint64_t idx;
337
    uint64_t idx;
338
    uint8_t drv_head;
338
    uint8_t drv_head;
339
    disk_t *d;
339
    disk_t *d;
340
 
340
 
341
    d = &disk[disk_id];
341
    d = &disk[disk_id];
342
 
342
 
343
    /* Check device bounds. */
343
    /* Check device bounds. */
344
    if (blk_idx >= d->blocks)
344
    if (blk_idx >= d->blocks)
345
        return EINVAL;
345
        return EINVAL;
346
 
346
 
347
    /* Compute CHS. */
347
    /* Compute CHS. */
348
    c = blk_idx / (d->heads * d->sectors);
348
    c = blk_idx / (d->heads * d->sectors);
349
    idx = blk_idx % (d->heads * d->sectors);
349
    idx = blk_idx % (d->heads * d->sectors);
350
 
350
 
351
    h = idx / d->sectors;
351
    h = idx / d->sectors;
352
    s = 1 + (idx % d->sectors);
352
    s = 1 + (idx % d->sectors);
353
 
353
 
354
    /* New value for Drive/Head register */
354
    /* New value for Drive/Head register */
355
    drv_head =
355
    drv_head =
356
        ((disk_id != 0) ? DHR_DRV : 0) |
356
        ((disk_id != 0) ? DHR_DRV : 0) |
357
        (h & 0x0f);
357
        (h & 0x0f);
358
 
358
 
359
    fibril_mutex_lock(&d->lock);
359
    fibril_mutex_lock(&d->lock);
360
 
360
 
361
    /* Program a Read Sectors operation. */
361
    /* Program a Read Sectors operation. */
362
 
362
 
363
    pio_write_8(&cmd->drive_head, drv_head);
363
    pio_write_8(&cmd->drive_head, drv_head);
364
    pio_write_8(&cmd->sector_count, 1);
364
    pio_write_8(&cmd->sector_count, 1);
365
    pio_write_8(&cmd->sector_number, s);
365
    pio_write_8(&cmd->sector_number, s);
366
    pio_write_8(&cmd->cylinder_low, c & 0xff);
366
    pio_write_8(&cmd->cylinder_low, c & 0xff);
367
    pio_write_8(&cmd->cylinder_high, c >> 16);
367
    pio_write_8(&cmd->cylinder_high, c >> 16);
368
    pio_write_8(&cmd->command, CMD_READ_SECTORS);
368
    pio_write_8(&cmd->command, CMD_READ_SECTORS);
369
 
369
 
370
    /* Read data from the disk buffer. */
370
    /* Read data from the disk buffer. */
371
 
371
 
372
    for (i = 0; i < block_size / 2; i++) {
372
    for (i = 0; i < block_size / 2; i++) {
373
        do {
373
        do {
374
            status = pio_read_8(&cmd->status);
374
            status = pio_read_8(&cmd->status);
375
        } while ((status & SR_DRDY) == 0);
375
        } while ((status & SR_DRDY) == 0);
376
 
376
 
377
        data = pio_read_16(&cmd->data_port);
377
        data = pio_read_16(&cmd->data_port);
378
        ((uint16_t *) buf)[i] = data;
378
        ((uint16_t *) buf)[i] = data;
379
    }
379
    }
380
 
380
 
381
    fibril_mutex_unlock(&d->lock);
381
    fibril_mutex_unlock(&d->lock);
382
    return EOK;
382
    return EOK;
383
}
383
}
384
 
384
 
385
static int ata_bd_write_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
385
static int ata_bd_write_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
386
    const void *buf)
386
    const void *buf)
387
{
387
{
388
    size_t i;
388
    size_t i;
389
    uint8_t status;
389
    uint8_t status;
390
    uint64_t c, h, s;
390
    uint64_t c, h, s;
391
    uint64_t idx;
391
    uint64_t idx;
392
    uint8_t drv_head;
392
    uint8_t drv_head;
393
    disk_t *d;
393
    disk_t *d;
394
 
394
 
395
    d = &disk[disk_id];
395
    d = &disk[disk_id];
396
 
396
 
397
    /* Check device bounds. */
397
    /* Check device bounds. */
398
    if (blk_idx >= d->blocks)
398
    if (blk_idx >= d->blocks)
399
        return EINVAL;
399
        return EINVAL;
400
 
400
 
401
    /* Compute CHS. */
401
    /* Compute CHS. */
402
    c = blk_idx / (d->heads * d->sectors);
402
    c = blk_idx / (d->heads * d->sectors);
403
    idx = blk_idx % (d->heads * d->sectors);
403
    idx = blk_idx % (d->heads * d->sectors);
404
 
404
 
405
    h = idx / d->sectors;
405
    h = idx / d->sectors;
406
    s = 1 + (idx % d->sectors);
406
    s = 1 + (idx % d->sectors);
407
 
407
 
408
    /* New value for Drive/Head register */
408
    /* New value for Drive/Head register */
409
    drv_head =
409
    drv_head =
410
        ((disk_id != 0) ? DHR_DRV : 0) |
410
        ((disk_id != 0) ? DHR_DRV : 0) |
411
        (h & 0x0f);
411
        (h & 0x0f);
412
 
412
 
413
    fibril_mutex_lock(&d->lock);
413
    fibril_mutex_lock(&d->lock);
414
 
414
 
415
    /* Program a Read Sectors operation. */
415
    /* Program a Read Sectors operation. */
416
 
416
 
417
    pio_write_8(&cmd->drive_head, drv_head);
417
    pio_write_8(&cmd->drive_head, drv_head);
418
    pio_write_8(&cmd->sector_count, 1);
418
    pio_write_8(&cmd->sector_count, 1);
419
    pio_write_8(&cmd->sector_number, s);
419
    pio_write_8(&cmd->sector_number, s);
420
    pio_write_8(&cmd->cylinder_low, c & 0xff);
420
    pio_write_8(&cmd->cylinder_low, c & 0xff);
421
    pio_write_8(&cmd->cylinder_high, c >> 16);
421
    pio_write_8(&cmd->cylinder_high, c >> 16);
422
    pio_write_8(&cmd->command, CMD_WRITE_SECTORS);
422
    pio_write_8(&cmd->command, CMD_WRITE_SECTORS);
423
 
423
 
424
    /* Write data to the disk buffer. */
424
    /* Write data to the disk buffer. */
425
 
425
 
426
    for (i = 0; i < block_size / 2; i++) {
426
    for (i = 0; i < block_size / 2; i++) {
427
        do {
427
        do {
428
            status = pio_read_8(&cmd->status);
428
            status = pio_read_8(&cmd->status);
429
        } while ((status & SR_DRDY) == 0);
429
        } while ((status & SR_DRDY) == 0);
430
 
430
 
431
        pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]);
431
        pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]);
432
    }
432
    }
433
 
433
 
434
    fibril_mutex_unlock(&d->lock);
434
    fibril_mutex_unlock(&d->lock);
435
    return EOK;
435
    return EOK;
436
}
436
}
437
 
437
 
438
 
438
 
439
/**
439
/**
440
 * @}
440
 * @}
441
 */
441
 */
442
 
442