Subversion Repositories HelenOS

Rev

Rev 4529 | Rev 4531 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4529 Rev 4530
Line 27... Line 27...
27
 */
27
 */
28
 
28
 
29
/** @addtogroup bd
29
/** @addtogroup bd
30
 * @{
30
 * @{
31
 */
31
 */
32
 
-
 
33
/**
-
 
34
 * @file
32
/** @file
35
 * @brief ATA disk driver
-
 
36
 *
-
 
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.
-
 
39
 * At this point only reading is possible, not writing.
-
 
40
 */
33
 */
41
 
34
 
42
#include <stdio.h>
-
 
43
#include <libarch/ddi.h>
-
 
44
#include <ddi.h>
-
 
45
#include <ipc/ipc.h>
35
#ifndef __ATA_BD_H__
46
#include <ipc/bd.h>
-
 
47
#include <async.h>
-
 
48
#include <as.h>
-
 
49
#include <futex.h>
-
 
50
#include <devmap.h>
-
 
51
#include <sys/types.h>
36
#define __ATA_BD_H__
52
#include <errno.h>
-
 
53
#include <bool.h>
-
 
54
 
37
 
55
#define NAME "ata_bd"
38
#include <sys/types.h>
56
 
39
 
57
enum {
40
enum {
58
    CTL_READ_START  = 0,
41
    CTL_READ_START  = 0,
59
    CTL_WRITE_START = 1,
42
    CTL_WRITE_START = 1,
60
};
43
};
Line 140... Line 123...
140
    unsigned cylinders;
123
    unsigned cylinders;
141
    unsigned sectors;
124
    unsigned sectors;
142
    uint64_t blocks;
125
    uint64_t blocks;
143
} disk_t;
126
} disk_t;
144
 
127
 
145
static const size_t block_size = 512;
-
 
146
static size_t comm_size;
-
 
147
 
-
 
148
static uintptr_t cmd_physical = 0x1f0;
-
 
149
static uintptr_t ctl_physical = 0x170;
-
 
150
static ata_cmd_t *cmd;
-
 
151
static ata_ctl_t *ctl;
-
 
152
 
-
 
153
static dev_handle_t dev_handle[MAX_DISKS];
-
 
154
 
-
 
155
static atomic_t dev_futex = FUTEX_INITIALIZER;
-
 
156
 
-
 
157
static disk_t disk[2];
-
 
158
 
-
 
159
static int ata_bd_init(void);
-
 
160
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
-
 
161
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, off_t size,
-
 
162
    void *buf);
-
 
163
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
-
 
164
    void *buf);
-
 
165
static int drive_identify(int drive_id, disk_t *d);
-
 
166
 
-
 
167
int main(int argc, char **argv)
-
 
168
{
-
 
169
    uint8_t status;
-
 
170
    char name[16];
-
 
171
    int i, rc;
-
 
172
    int n_disks;
-
 
173
 
-
 
174
    printf(NAME ": ATA disk driver\n");
-
 
175
 
-
 
176
    printf("cmd_physical = 0x%x\n", cmd_physical);
-
 
177
    printf("ctl_physical = 0x%x\n", ctl_physical);
-
 
178
 
-
 
179
    if (ata_bd_init() != EOK)
-
 
180
        return -1;
-
 
181
 
-
 
182
    /* Put drives to reset, disable interrupts. */
-
 
183
    printf("Reset drives...\n");
-
 
184
    pio_write_8(&ctl->device_control, DCR_SRST);
-
 
185
/*  printf("wait for busy\n");
-
 
186
    do {
-
 
187
        status = pio_read_8(&cmd->status);
-
 
188
    } while ((status & SR_BSY) == 0);
-
 
189
*/
-
 
190
    async_usleep(100);
-
 
191
    pio_write_8(&ctl->device_control, 0);
-
 
192
 
-
 
193
    do {
-
 
194
        status = pio_read_8(&cmd->status);
-
 
195
    } while ((status & SR_BSY) != 0);
-
 
196
    printf("Done\n");
-
 
197
 
-
 
198
    printf("Status = 0x%x\n", pio_read_8(&cmd->status));
-
 
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;
128
#endif
238
 
-
 
239
    printf("Identify drive %d\n", disk_id);
-
 
240
    pio_write_8(&cmd->drive_head, ((disk_id != 0) ? DHR_DRV : 0));
-
 
241
    async_usleep(100);
-
 
242
    pio_write_8(&cmd->command, 0xEC);
-
 
243
 
-
 
244
    status = 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
    }
-
 
257
 
-
 
258
    for (i = 0; i < 256; i++) {
-
 
259
        do {
-
 
260
            status = pio_read_8(&cmd->status);
-
 
261
        } while ((status & SR_DRDY) == 0);
-
 
262
 
-
 
263
        data = pio_read_16(&cmd->data_port);
-
 
264
 
-
 
265
        switch (i) {
-
 
266
        case 1: d->cylinders = data; break;
-
 
267
        case 3: d->heads = data; break;
-
 
268
        case 6: d->sectors = data; break;
-
 
269
        }
-
 
270
    }
-
 
271
 
-
 
272
    printf("\n\nStatus = 0x%x\n", pio_read_8(&cmd->status));
-
 
273
 
-
 
274
    d->blocks = d->cylinders * d->heads * d->sectors;
-
 
275
 
-
 
276
    printf("Geometry: %u cylinders, %u heads, %u sectors\n",
-
 
277
        d->cylinders, d->heads, d->sectors);
-
 
278
 
-
 
279
    d->present = true;
-
 
280
 
-
 
281
    return EOK;
-
 
282
}
-
 
283
 
-
 
284
static int ata_bd_init(void)
-
 
285
{
-
 
286
    void *vaddr;
-
 
287
    int rc;
-
 
288
 
-
 
289
    rc = devmap_driver_register(NAME, ata_bd_connection);
-
 
290
    if (rc < 0) {
-
 
291
        printf(NAME ": Unable to register driver.\n");
-
 
292
        return rc;
-
 
293
    }
-
 
294
 
-
 
295
    rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr);
-
 
296
    if (rc != EOK) {
-
 
297
        printf(NAME ": Could not initialize device I/O space.\n");
-
 
298
        return rc;
-
 
299
    }
-
 
300
 
-
 
301
    cmd = vaddr;
-
 
302
 
-
 
303
    rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr);
-
 
304
    if (rc != EOK) {
-
 
305
        printf(NAME ": Could not initialize device I/O space.\n");
-
 
306
        return rc;
-
 
307
    }
-
 
308
 
-
 
309
    ctl = vaddr;
-
 
310
 
-
 
311
 
-
 
312
    return EOK;
-
 
313
}
-
 
314
 
-
 
315
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
-
 
316
{
-
 
317
    void *fs_va = NULL;
-
 
318
    ipc_callid_t callid;
-
 
319
    ipc_call_t call;
-
 
320
    ipcarg_t method;
-
 
321
    dev_handle_t dh;
-
 
322
    int flags;
-
 
323
    int retval;
-
 
324
    off_t idx;
-
 
325
    off_t size;
-
 
326
    int disk_id, i;
-
 
327
 
-
 
328
    /* Get the device handle. */
-
 
329
    dh = IPC_GET_ARG1(*icall);
-
 
330
 
-
 
331
    /* Determine which disk device is the client connecting to. */
-
 
332
    disk_id = -1;
-
 
333
    for (i = 0; i < MAX_DISKS; i++)
-
 
334
        if (dev_handle[i] == dh)
-
 
335
            disk_id = i;
-
 
336
 
-
 
337
    if (disk_id < 0 || disk[disk_id].present == false) {
-
 
338
        ipc_answer_0(iid, EINVAL);
-
 
339
        return;
-
 
340
    }
-
 
341
 
-
 
342
    /* Answer the IPC_M_CONNECT_ME_TO call. */
-
 
343
    ipc_answer_0(iid, EOK);
-
 
344
 
-
 
345
    if (!ipc_share_out_receive(&callid, &comm_size, &flags)) {
-
 
346
        ipc_answer_0(callid, EHANGUP);
-
 
347
        return;
-
 
348
    }
-
 
349
 
-
 
350
    fs_va = as_get_mappable_page(comm_size);
-
 
351
    if (fs_va == NULL) {
-
 
352
        ipc_answer_0(callid, EHANGUP);
-
 
353
        return;
-
 
354
    }
-
 
355
 
-
 
356
    (void) ipc_share_out_finalize(callid, fs_va);
-
 
357
 
-
 
358
    while (1) {
-
 
359
        callid = async_get_call(&call);
-
 
360
        method = IPC_GET_METHOD(call);
-
 
361
        switch (method) {
-
 
362
        case IPC_M_PHONE_HUNGUP:
-
 
363
            /* The other side has hung up. */
-
 
364
            ipc_answer_0(callid, EOK);
-
 
365
            return;
-
 
366
        case BD_READ_BLOCK:
-
 
367
        case BD_WRITE_BLOCK:
-
 
368
            idx = IPC_GET_ARG1(call);
-
 
369
            size = IPC_GET_ARG2(call);
-
 
370
            if (size > comm_size) {
-
 
371
                retval = EINVAL;
-
 
372
                break;
-
 
373
            }
-
 
374
            retval = ata_bd_rdwr(disk_id, method, idx,
-
 
375
                size, fs_va);
-
 
376
            break;
-
 
377
        default:
-
 
378
            retval = EINVAL;
-
 
379
            break;
-
 
380
        }
-
 
381
        ipc_answer_0(callid, retval);
-
 
382
    }
-
 
383
}
-
 
384
 
-
 
385
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t blk_idx, off_t size,
-
 
386
    void *buf)
-
 
387
{
-
 
388
    int rc;
-
 
389
    off_t now;
-
 
390
 
-
 
391
    while (size > 0) {
-
 
392
        now = size < block_size ? size : (off_t) block_size;
-
 
393
        if (now != block_size)
-
 
394
            return EINVAL;
-
 
395
 
-
 
396
        if (method == BD_READ_BLOCK)
-
 
397
            rc = ata_bd_read_block(disk_id, blk_idx, 1, buf);
-
 
398
        else
-
 
399
            rc = ENOTSUP;
-
 
400
 
-
 
401
        if (rc != EOK)
-
 
402
            return rc;
-
 
403
 
-
 
404
        buf += block_size;
-
 
405
        blk_idx++;
-
 
406
 
-
 
407
        if (size > block_size)
-
 
408
            size -= block_size;
-
 
409
        else
-
 
410
            size = 0;
-
 
411
    }
-
 
412
 
-
 
413
    return EOK;
-
 
414
}
-
 
415
 
-
 
416
 
-
 
417
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
-
 
418
    void *buf)
-
 
419
{
-
 
420
    size_t i;
-
 
421
    uint16_t data;
-
 
422
    uint8_t status;
-
 
423
    uint64_t c, h, s;
-
 
424
    uint64_t idx;
-
 
425
    uint8_t drv_head;
-
 
426
    disk_t *d;
-
 
427
 
-
 
428
    d = &disk[disk_id];
-
 
429
 
-
 
430
    /* Check device bounds. */
-
 
431
    if (blk_idx >= d->blocks)
-
 
432
        return EINVAL;
-
 
433
 
-
 
434
    /* Compute CHS. */
-
 
435
    c = blk_idx / (d->heads * d->sectors);
-
 
436
    idx = blk_idx % (d->heads * d->sectors);
-
 
437
 
-
 
438
    h = idx / d->sectors;
-
 
439
    s = 1 + (idx % d->sectors);
-
 
440
 
-
 
441
    /* New value for Drive/Head register */
-
 
442
    drv_head =
-
 
443
        ((disk_id != 0) ? DHR_DRV : 0) |
-
 
444
        (h & 0x0f);
-
 
445
 
-
 
446
    futex_down(&dev_futex);
-
 
447
 
-
 
448
    /* Program a Read Sectors operation. */
-
 
449
 
-
 
450
    pio_write_8(&cmd->drive_head, drv_head);
-
 
451
    pio_write_8(&cmd->sector_count, 1);
-
 
452
    pio_write_8(&cmd->sector_number, s);
-
 
453
    pio_write_8(&cmd->cylinder_low, c & 0xff);
-
 
454
    pio_write_8(&cmd->cylinder_high, c >> 16);
-
 
455
    pio_write_8(&cmd->command, 0x20);
-
 
456
 
-
 
457
    /* Read data from the disk buffer. */
-
 
458
 
-
 
459
    for (i = 0; i < 256; i++) {
-
 
460
        do {
-
 
461
            status = pio_read_8(&cmd->status);
-
 
462
        } while ((status & SR_DRDY) == 0);
-
 
463
 
-
 
464
        data = pio_read_16(&cmd->data_port);
-
 
465
        ((uint16_t *) buf)[i] = data;
-
 
466
    }
-
 
467
 
-
 
468
    futex_up(&dev_futex);
-
 
469
    return EOK;
-
 
470
}
-
 
471
 
-
 
472
 
129
 
473
/**
-
 
474
 * @}
130
/** @}
475
 */
131
 */