Subversion Repositories HelenOS

Rev

Rev 4507 | Rev 4530 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4507 Rev 4529
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
 
41
 
42
#include <stdio.h>
42
#include <stdio.h>
43
#include <libarch/ddi.h>
43
#include <libarch/ddi.h>
44
#include <ddi.h>
44
#include <ddi.h>
45
#include <ipc/ipc.h>
45
#include <ipc/ipc.h>
46
#include <ipc/bd.h>
46
#include <ipc/bd.h>
47
#include <async.h>
47
#include <async.h>
48
#include <as.h>
48
#include <as.h>
49
#include <futex.h>
49
#include <futex.h>
50
#include <devmap.h>
50
#include <devmap.h>
51
#include <sys/types.h>
51
#include <sys/types.h>
52
#include <errno.h>
52
#include <errno.h>
-
 
53
#include <bool.h>
53
 
54
 
54
#define NAME "ata_bd"
55
#define NAME "ata_bd"
55
 
56
 
56
enum {
57
enum {
57
    CTL_READ_START  = 0,
58
    CTL_READ_START  = 0,
58
    CTL_WRITE_START = 1,
59
    CTL_WRITE_START = 1,
59
};
60
};
60
 
61
 
61
enum {
62
enum {
62
    STATUS_FAILURE  = 0
63
    STATUS_FAILURE  = 0
63
};
64
};
64
 
65
 
65
enum {
66
enum {
66
    MAX_DISKS   = 2
67
    MAX_DISKS   = 2
67
};
68
};
68
 
69
 
69
typedef union {
70
typedef union {
70
    /* Read */
71
    /* Read */
71
    struct {
72
    struct {
72
        uint8_t data_port;
73
        uint8_t data_port;
73
        uint8_t error;
74
        uint8_t error;
74
        uint8_t sector_count;
75
        uint8_t sector_count;
75
        uint8_t sector_number;
76
        uint8_t sector_number;
76
        uint8_t cylinder_low;
77
        uint8_t cylinder_low;
77
        uint8_t cylinder_high;
78
        uint8_t cylinder_high;
78
        uint8_t drive_head;
79
        uint8_t drive_head;
79
        uint8_t status;
80
        uint8_t status;
80
    };
81
    };
81
 
82
 
82
    /* Write */
83
    /* Write */
83
    struct {
84
    struct {
84
        uint8_t pad0[7];
85
        uint8_t pad0[7];
85
        uint8_t command;
86
        uint8_t command;
86
    };
87
    };
87
} ata_cmd_t;
88
} ata_cmd_t;
88
 
89
 
89
typedef union {
90
typedef union {
90
    /* Read */
91
    /* Read */
91
    struct {
92
    struct {
92
        uint8_t pad0[6];
93
        uint8_t pad0[6];
93
        uint8_t alt_status;
94
        uint8_t alt_status;
94
        uint8_t drive_address;
95
        uint8_t drive_address;
95
    };
96
    };
96
 
97
 
97
    /* Write */
98
    /* Write */
98
    struct {
99
    struct {
99
        uint8_t pad1[6];
100
        uint8_t pad1[6];
100
        uint8_t device_control;
101
        uint8_t device_control;
101
        uint8_t pad2;
102
        uint8_t pad2;
102
    };
103
    };
103
} ata_ctl_t;
104
} ata_ctl_t;
104
 
105
 
105
enum devctl_bits {
106
enum devctl_bits {
106
    DCR_SRST    = 0x04, /**< Software Reset */
107
    DCR_SRST    = 0x04, /**< Software Reset */
107
    DCR_nIEN    = 0x02  /**< Interrupt Enable (negated) */
108
    DCR_nIEN    = 0x02  /**< Interrupt Enable (negated) */
108
};
109
};
109
 
110
 
110
enum status_bits {
111
enum status_bits {
111
    SR_BSY      = 0x80, /**< Busy */
112
    SR_BSY      = 0x80, /**< Busy */
112
    SR_DRDY     = 0x40, /**< Drive Ready */
113
    SR_DRDY     = 0x40, /**< Drive Ready */
113
    SR_DWF      = 0x20, /**< Drive Write Fault */
114
    SR_DWF      = 0x20, /**< Drive Write Fault */
114
    SR_DSC      = 0x10, /**< Drive Seek Complete */
115
    SR_DSC      = 0x10, /**< Drive Seek Complete */
115
    SR_DRQ      = 0x08, /**< Data Request */
116
    SR_DRQ      = 0x08, /**< Data Request */
116
    SR_CORR     = 0x04, /**< Corrected Data */
117
    SR_CORR     = 0x04, /**< Corrected Data */
117
    SR_IDX      = 0x02, /**< Index */
118
    SR_IDX      = 0x02, /**< Index */
118
    SR_ERR      = 0x01  /**< Error */
119
    SR_ERR      = 0x01  /**< Error */
119
};
120
};
120
 
121
 
121
enum drive_head_bits {
122
enum drive_head_bits {
122
    DHR_DRV     = 0x10
123
    DHR_DRV     = 0x10
123
};
124
};
124
 
125
 
125
enum error_bits {
126
enum error_bits {
126
    ER_BBK      = 0x80, /**< Bad Block Detected */
127
    ER_BBK      = 0x80, /**< Bad Block Detected */
127
    ER_UNC      = 0x40, /**< Uncorrectable Data Error */
128
    ER_UNC      = 0x40, /**< Uncorrectable Data Error */
128
    ER_MC       = 0x20, /**< Media Changed */
129
    ER_MC       = 0x20, /**< Media Changed */
129
    ER_IDNF     = 0x10, /**< ID Not Found */
130
    ER_IDNF     = 0x10, /**< ID Not Found */
130
    ER_MCR      = 0x08, /**< Media Change Request */
131
    ER_MCR      = 0x08, /**< Media Change Request */
131
    ER_ABRT     = 0x04, /**< Aborted Command */
132
    ER_ABRT     = 0x04, /**< Aborted Command */
132
    ER_TK0NF    = 0x02, /**< Track 0 Not Found */
133
    ER_TK0NF    = 0x02, /**< Track 0 Not Found */
133
    ER_AMNF     = 0x01  /**< Address Mark Not Found */
134
    ER_AMNF     = 0x01  /**< Address Mark Not Found */
134
};
135
};
135
 
136
 
-
 
137
typedef struct {
-
 
138
    bool present;
-
 
139
    unsigned heads;
-
 
140
    unsigned cylinders;
-
 
141
    unsigned sectors;
-
 
142
    uint64_t blocks;
-
 
143
} disk_t;
-
 
144
 
136
static const size_t block_size = 512;
145
static const size_t block_size = 512;
137
static size_t comm_size;
146
static size_t comm_size;
138
 
147
 
139
static uintptr_t cmd_physical = 0x1f0;
148
static uintptr_t cmd_physical = 0x1f0;
140
static uintptr_t ctl_physical = 0x170;
149
static uintptr_t ctl_physical = 0x170;
141
static ata_cmd_t *cmd;
150
static ata_cmd_t *cmd;
142
static ata_ctl_t *ctl;
151
static ata_ctl_t *ctl;
143
 
152
 
144
static dev_handle_t dev_handle[MAX_DISKS];
153
static dev_handle_t dev_handle[MAX_DISKS];
145
 
154
 
146
static atomic_t dev_futex = FUTEX_INITIALIZER;
155
static atomic_t dev_futex = FUTEX_INITIALIZER;
147
 
156
 
148
static unsigned heads, cylinders, sectors;
-
 
149
static uint64_t disk_blocks;
157
static disk_t disk[2];
150
 
158
 
151
static int ata_bd_init(void);
159
static int ata_bd_init(void);
152
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
160
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
153
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, off_t size,
161
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, off_t size,
154
    void *buf);
162
    void *buf);
155
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
163
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
156
    void *buf);
164
    void *buf);
-
 
165
static int drive_identify(int drive_id, disk_t *d);
157
 
166
 
158
int main(int argc, char **argv)
167
int main(int argc, char **argv)
159
{
168
{
160
    uint8_t status;
169
    uint8_t status;
-
 
170
    char name[16];
161
    uint16_t data;
171
    int i, rc;
162
    int i;
172
    int n_disks;
163
 
173
 
164
    printf(NAME ": ATA disk driver\n");
174
    printf(NAME ": ATA disk driver\n");
165
 
175
 
166
    printf("cmd_physical = 0x%x\n", cmd_physical);
176
    printf("cmd_physical = 0x%x\n", cmd_physical);
167
    printf("ctl_physical = 0x%x\n", ctl_physical);
177
    printf("ctl_physical = 0x%x\n", ctl_physical);
168
 
178
 
169
    if (ata_bd_init() != EOK)
179
    if (ata_bd_init() != EOK)
170
        return -1;
180
        return -1;
171
 
181
 
172
    /* Put drives to reset, disable interrupts. */
182
    /* Put drives to reset, disable interrupts. */
173
    printf("Reset drives...\n");
183
    printf("Reset drives...\n");
174
    pio_write_8(&ctl->device_control, DCR_SRST);
184
    pio_write_8(&ctl->device_control, DCR_SRST);
175
/*  printf("wait for busy\n");
185
/*  printf("wait for busy\n");
176
    do {
186
    do {
177
        status = pio_read_8(&cmd->status);
187
        status = pio_read_8(&cmd->status);
178
    } while ((status & SR_BSY) == 0);
188
    } while ((status & SR_BSY) == 0);
179
*/
189
*/
180
    async_usleep(100);
190
    async_usleep(100);
181
    pio_write_8(&ctl->device_control, 0);
191
    pio_write_8(&ctl->device_control, 0);
182
 
192
 
183
    do {
193
    do {
184
        status = pio_read_8(&cmd->status);
194
        status = pio_read_8(&cmd->status);
185
    } while ((status & SR_BSY) != 0);
195
    } while ((status & SR_BSY) != 0);
186
    printf("Done\n");
196
    printf("Done\n");
187
 
197
 
188
    printf("Status = 0x%x\n", pio_read_8(&cmd->status));
198
    printf("Status = 0x%x\n", pio_read_8(&cmd->status));
189
 
199
 
-
 
200
    (void) drive_identify(0, &disk[0]);
-
 
201
    (void) drive_identify(1, &disk[1]);
-
 
202
 
-
 
203
    n_disks = 0;
-
 
204
 
-
 
205
    for (i = 0; i < MAX_DISKS; i++) {
-
 
206
        /* Skip unattached drives. */
-
 
207
        if (disk[i].present == false)
-
 
208
            continue;
-
 
209
 
-
 
210
        snprintf(name, 16, "disk%d", i);
-
 
211
        rc = devmap_device_register(name, &dev_handle[i]);
-
 
212
        if (rc != EOK) {
-
 
213
            devmap_hangup_phone(DEVMAP_DRIVER);
-
 
214
            printf(NAME ": Unable to register device %s.\n",
-
 
215
                name);
-
 
216
            return rc;
-
 
217
        }
-
 
218
        ++n_disks;
-
 
219
    }
-
 
220
 
-
 
221
    if (n_disks == 0) {
-
 
222
        printf("No disks detected.\n");
-
 
223
        return -1;
-
 
224
    }
-
 
225
 
-
 
226
    printf(NAME ": Accepting connections\n");
-
 
227
    async_manager();
-
 
228
 
-
 
229
    /* Not reached */
-
 
230
    return 0;
-
 
231
}
-
 
232
 
-
 
233
static int drive_identify(int disk_id, disk_t *d)
-
 
234
{
-
 
235
    uint16_t data;
-
 
236
    uint8_t status;
-
 
237
    int i;
-
 
238
 
190
    printf("Issue drive_identify command\n");
239
    printf("Identify drive %d\n", disk_id);
191
    pio_write_8(&cmd->drive_head, 0x00);
240
    pio_write_8(&cmd->drive_head, ((disk_id != 0) ? DHR_DRV : 0));
192
    async_usleep(100);
241
    async_usleep(100);
193
    pio_write_8(&cmd->command, 0xEC);
242
    pio_write_8(&cmd->command, 0xEC);
194
 
243
 
-
 
244
    status = pio_read_8(&cmd->status);
195
    printf("Status = 0x%x\n", pio_read_8(&cmd->status));
245
    printf("Status = 0x%x\n", status);
-
 
246
 
-
 
247
    d->present = false;
-
 
248
 
-
 
249
    /*
-
 
250
     * Detect if drive is present. This is Qemu only! Need to
-
 
251
     * do the right thing to work with real drives.
-
 
252
     */
-
 
253
    if ((status & SR_DRDY) == 0) {
-
 
254
        printf("None attached.\n");
-
 
255
        return ENOENT;
-
 
256
    }
196
 
257
 
197
    for (i = 0; i < 256; i++) {
258
    for (i = 0; i < 256; i++) {
198
        do {
259
        do {
199
            status = pio_read_8(&cmd->status);
260
            status = pio_read_8(&cmd->status);
200
        } while ((status & SR_DRDY) == 0);
261
        } while ((status & SR_DRDY) == 0);
201
 
262
 
202
        data = pio_read_16(&cmd->data_port);
263
        data = pio_read_16(&cmd->data_port);
203
 
264
 
204
        switch (i) {
265
        switch (i) {
205
        case 1: cylinders = data; break;
266
        case 1: d->cylinders = data; break;
206
        case 3: heads = data; break;
267
        case 3: d->heads = data; break;
207
        case 6: sectors = data; break;
268
        case 6: d->sectors = data; break;
208
        }
269
        }
209
    }
270
    }
210
 
271
 
211
    printf("\n\nStatus = 0x%x\n", pio_read_8(&cmd->status));
272
    printf("\n\nStatus = 0x%x\n", pio_read_8(&cmd->status));
212
 
273
 
-
 
274
    d->blocks = d->cylinders * d->heads * d->sectors;
-
 
275
 
213
    printf("Geometry: %u cylinders, %u heads, %u sectors\n",
276
    printf("Geometry: %u cylinders, %u heads, %u sectors\n",
214
        cylinders, heads, sectors);
277
        d->cylinders, d->heads, d->sectors);
215
    disk_blocks = cylinders * heads * sectors;
-
 
216
 
278
 
217
    printf(NAME ": Accepting connections\n");
-
 
218
    async_manager();
279
    d->present = true;
219
 
280
 
220
    /* Not reached */
-
 
221
    return 0;
281
    return EOK;
222
}
282
}
223
 
283
 
224
static int ata_bd_init(void)
284
static int ata_bd_init(void)
225
{
285
{
226
    void *vaddr;
286
    void *vaddr;
227
    int rc, i;
287
    int rc;
228
    char name[16];
-
 
229
 
288
 
230
    rc = devmap_driver_register(NAME, ata_bd_connection);
289
    rc = devmap_driver_register(NAME, ata_bd_connection);
231
    if (rc < 0) {
290
    if (rc < 0) {
232
        printf(NAME ": Unable to register driver.\n");
291
        printf(NAME ": Unable to register driver.\n");
233
        return rc;
292
        return rc;
234
    }
293
    }
235
 
294
 
236
    rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr);
295
    rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr);
237
    if (rc != EOK) {
296
    if (rc != EOK) {
238
        printf(NAME ": Could not initialize device I/O space.\n");
297
        printf(NAME ": Could not initialize device I/O space.\n");
239
        return rc;
298
        return rc;
240
    }
299
    }
241
 
300
 
242
    cmd = vaddr;
301
    cmd = vaddr;
243
 
302
 
244
    rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr);
303
    rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr);
245
    if (rc != EOK) {
304
    if (rc != EOK) {
246
        printf(NAME ": Could not initialize device I/O space.\n");
305
        printf(NAME ": Could not initialize device I/O space.\n");
247
        return rc;
306
        return rc;
248
    }
307
    }
249
 
308
 
250
    ctl = vaddr;
309
    ctl = vaddr;
251
 
310
 
252
    for (i = 0; i < MAX_DISKS; i++) {
-
 
253
        snprintf(name, 16, "disk%d", i);
-
 
254
        rc = devmap_device_register(name, &dev_handle[i]);
-
 
255
        if (rc != EOK) {
-
 
256
            devmap_hangup_phone(DEVMAP_DRIVER);
-
 
257
            printf(NAME ": Unable to register device %s.\n",
-
 
258
                name);
-
 
259
            return rc;
-
 
260
        }
-
 
261
    }
-
 
262
 
311
 
263
    return EOK;
312
    return EOK;
264
}
313
}
265
 
314
 
266
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
315
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
267
{
316
{
268
    void *fs_va = NULL;
317
    void *fs_va = NULL;
269
    ipc_callid_t callid;
318
    ipc_callid_t callid;
270
    ipc_call_t call;
319
    ipc_call_t call;
271
    ipcarg_t method;
320
    ipcarg_t method;
272
    dev_handle_t dh;
321
    dev_handle_t dh;
273
    int flags;
322
    int flags;
274
    int retval;
323
    int retval;
275
    off_t idx;
324
    off_t idx;
276
    off_t size;
325
    off_t size;
277
    int disk_id, i;
326
    int disk_id, i;
278
 
327
 
279
    /* Get the device handle. */
328
    /* Get the device handle. */
280
    dh = IPC_GET_ARG1(*icall);
329
    dh = IPC_GET_ARG1(*icall);
281
 
330
 
282
    /* Determine which disk device is the client connecting to. */
331
    /* Determine which disk device is the client connecting to. */
283
    disk_id = -1;
332
    disk_id = -1;
284
    for (i = 0; i < MAX_DISKS; i++)
333
    for (i = 0; i < MAX_DISKS; i++)
285
        if (dev_handle[i] == dh)
334
        if (dev_handle[i] == dh)
286
            disk_id = i;
335
            disk_id = i;
287
 
336
 
288
    if (disk_id < 0) {
337
    if (disk_id < 0 || disk[disk_id].present == false) {
289
        ipc_answer_0(iid, EINVAL);
338
        ipc_answer_0(iid, EINVAL);
290
        return;
339
        return;
291
    }
340
    }
292
 
341
 
293
    /* Answer the IPC_M_CONNECT_ME_TO call. */
342
    /* Answer the IPC_M_CONNECT_ME_TO call. */
294
    ipc_answer_0(iid, EOK);
343
    ipc_answer_0(iid, EOK);
295
 
344
 
296
    if (!ipc_share_out_receive(&callid, &comm_size, &flags)) {
345
    if (!ipc_share_out_receive(&callid, &comm_size, &flags)) {
297
        ipc_answer_0(callid, EHANGUP);
346
        ipc_answer_0(callid, EHANGUP);
298
        return;
347
        return;
299
    }
348
    }
300
 
349
 
301
    fs_va = as_get_mappable_page(comm_size);
350
    fs_va = as_get_mappable_page(comm_size);
302
    if (fs_va == NULL) {
351
    if (fs_va == NULL) {
303
        ipc_answer_0(callid, EHANGUP);
352
        ipc_answer_0(callid, EHANGUP);
304
        return;
353
        return;
305
    }
354
    }
306
 
355
 
307
    (void) ipc_share_out_finalize(callid, fs_va);
356
    (void) ipc_share_out_finalize(callid, fs_va);
308
 
357
 
309
    while (1) {
358
    while (1) {
310
        callid = async_get_call(&call);
359
        callid = async_get_call(&call);
311
        method = IPC_GET_METHOD(call);
360
        method = IPC_GET_METHOD(call);
312
        switch (method) {
361
        switch (method) {
313
        case IPC_M_PHONE_HUNGUP:
362
        case IPC_M_PHONE_HUNGUP:
314
            /* The other side has hung up. */
363
            /* The other side has hung up. */
315
            ipc_answer_0(callid, EOK);
364
            ipc_answer_0(callid, EOK);
316
            return;
365
            return;
317
        case BD_READ_BLOCK:
366
        case BD_READ_BLOCK:
318
        case BD_WRITE_BLOCK:
367
        case BD_WRITE_BLOCK:
319
            idx = IPC_GET_ARG1(call);
368
            idx = IPC_GET_ARG1(call);
320
            size = IPC_GET_ARG2(call);
369
            size = IPC_GET_ARG2(call);
321
            if (size > comm_size) {
370
            if (size > comm_size) {
322
                retval = EINVAL;
371
                retval = EINVAL;
323
                break;
372
                break;
324
            }
373
            }
325
            retval = ata_bd_rdwr(disk_id, method, idx,
374
            retval = ata_bd_rdwr(disk_id, method, idx,
326
                size, fs_va);
375
                size, fs_va);
327
            break;
376
            break;
328
        default:
377
        default:
329
            retval = EINVAL;
378
            retval = EINVAL;
330
            break;
379
            break;
331
        }
380
        }
332
        ipc_answer_0(callid, retval);
381
        ipc_answer_0(callid, retval);
333
    }
382
    }
334
}
383
}
335
 
384
 
336
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t blk_idx, off_t size,
385
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t blk_idx, off_t size,
337
    void *buf)
386
    void *buf)
338
{
387
{
339
    int rc;
388
    int rc;
340
    off_t now;
389
    off_t now;
341
 
390
 
342
    while (size > 0) {
391
    while (size > 0) {
343
        now = size < block_size ? size : (off_t) block_size;
392
        now = size < block_size ? size : (off_t) block_size;
344
        if (now != block_size)
393
        if (now != block_size)
345
            return EINVAL;
394
            return EINVAL;
346
 
395
 
347
        if (method == BD_READ_BLOCK)
396
        if (method == BD_READ_BLOCK)
348
            rc = ata_bd_read_block(disk_id, blk_idx, 1, buf);
397
            rc = ata_bd_read_block(disk_id, blk_idx, 1, buf);
349
        else
398
        else
350
            rc = ENOTSUP;
399
            rc = ENOTSUP;
351
 
400
 
352
        if (rc != EOK)
401
        if (rc != EOK)
353
            return rc;
402
            return rc;
354
 
403
 
355
        buf += block_size;
404
        buf += block_size;
356
        blk_idx++;
405
        blk_idx++;
357
 
406
 
358
        if (size > block_size)
407
        if (size > block_size)
359
            size -= block_size;
408
            size -= block_size;
360
        else
409
        else
361
            size = 0;
410
            size = 0;
362
    }
411
    }
363
 
412
 
364
    return EOK;
413
    return EOK;
365
}
414
}
366
 
415
 
367
 
416
 
368
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
417
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
369
    void *buf)
418
    void *buf)
370
{
419
{
371
    size_t i;
420
    size_t i;
372
    uint16_t data;
421
    uint16_t data;
373
    uint8_t status;
422
    uint8_t status;
374
    uint64_t c, h, s;
423
    uint64_t c, h, s;
375
    uint64_t idx;
424
    uint64_t idx;
376
    uint8_t drv_head;
425
    uint8_t drv_head;
-
 
426
    disk_t *d;
-
 
427
 
-
 
428
    d = &disk[disk_id];
377
 
429
 
378
    /* Check device bounds. */
430
    /* Check device bounds. */
379
    if (blk_idx >= disk_blocks)
431
    if (blk_idx >= d->blocks)
380
        return EINVAL;
432
        return EINVAL;
381
 
433
 
382
    /* Compute CHS. */
434
    /* Compute CHS. */
383
    c = blk_idx / (heads * sectors);
435
    c = blk_idx / (d->heads * d->sectors);
384
    idx = blk_idx % (heads * sectors);
436
    idx = blk_idx % (d->heads * d->sectors);
385
 
437
 
386
    h = idx / sectors;
438
    h = idx / d->sectors;
387
    s = 1 + (idx % sectors);
439
    s = 1 + (idx % d->sectors);
388
 
440
 
389
    /* New value for Drive/Head register */
441
    /* New value for Drive/Head register */
390
    drv_head =
442
    drv_head =
391
        ((disk_id != 0) ? DHR_DRV : 0) |
443
        ((disk_id != 0) ? DHR_DRV : 0) |
392
        (h & 0x0f);
444
        (h & 0x0f);
393
 
445
 
394
    futex_down(&dev_futex);
446
    futex_down(&dev_futex);
395
 
447
 
396
    /* Program a Read Sectors operation. */
448
    /* Program a Read Sectors operation. */
397
 
449
 
398
    pio_write_8(&cmd->drive_head, drv_head);
450
    pio_write_8(&cmd->drive_head, drv_head);
399
    pio_write_8(&cmd->sector_count, 1);
451
    pio_write_8(&cmd->sector_count, 1);
400
    pio_write_8(&cmd->sector_number, s);
452
    pio_write_8(&cmd->sector_number, s);
401
    pio_write_8(&cmd->cylinder_low, c & 0xff);
453
    pio_write_8(&cmd->cylinder_low, c & 0xff);
402
    pio_write_8(&cmd->cylinder_high, c >> 16);
454
    pio_write_8(&cmd->cylinder_high, c >> 16);
403
    pio_write_8(&cmd->command, 0x20);
455
    pio_write_8(&cmd->command, 0x20);
404
 
456
 
405
    /* Read data from the disk buffer. */
457
    /* Read data from the disk buffer. */
406
 
458
 
407
    for (i = 0; i < 256; i++) {
459
    for (i = 0; i < 256; i++) {
408
        do {
460
        do {
409
            status = pio_read_8(&cmd->status);
461
            status = pio_read_8(&cmd->status);
410
        } while ((status & SR_DRDY) == 0);
462
        } while ((status & SR_DRDY) == 0);
411
 
463
 
412
        data = pio_read_16(&cmd->data_port);
464
        data = pio_read_16(&cmd->data_port);
413
        ((uint16_t *) buf)[i] = data;
465
        ((uint16_t *) buf)[i] = data;
414
    }
466
    }
415
 
467
 
416
    futex_up(&dev_futex);
468
    futex_up(&dev_futex);
417
    return EOK;
469
    return EOK;
418
}
470
}
419
 
471
 
420
 
472
 
421
/**
473
/**
422
 * @}
474
 * @}
423
 */
475
 */
424
 
476