Subversion Repositories HelenOS

Rev

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

Rev 4537 Rev 4668
Line 35... Line 35...
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
 *
-
 
41
 * The driver services a single controller which can have up to two disks
-
 
42
 * attached.
40
 */
43
 */
41
 
44
 
42
#include <stdio.h>
45
#include <stdio.h>
43
#include <libarch/ddi.h>
46
#include <libarch/ddi.h>
44
#include <ddi.h>
47
#include <ddi.h>
45
#include <ipc/ipc.h>
48
#include <ipc/ipc.h>
46
#include <ipc/bd.h>
49
#include <ipc/bd.h>
47
#include <async.h>
50
#include <async.h>
48
#include <as.h>
51
#include <as.h>
49
#include <futex.h>
52
#include <fibril_sync.h>
50
#include <devmap.h>
53
#include <devmap.h>
51
#include <sys/types.h>
54
#include <sys/types.h>
52
#include <errno.h>
55
#include <errno.h>
53
#include <bool.h>
56
#include <bool.h>
-
 
57
#include <task.h>
54
 
58
 
55
#include "ata_bd.h"
59
#include "ata_bd.h"
56
 
60
 
57
#define NAME "ata_bd"
61
#define NAME "ata_bd"
58
 
62
 
Line 62... Line 66...
62
static uintptr_t cmd_physical = 0x1f0;
66
static uintptr_t cmd_physical = 0x1f0;
63
static uintptr_t ctl_physical = 0x170;
67
static uintptr_t ctl_physical = 0x170;
64
static ata_cmd_t *cmd;
68
static ata_cmd_t *cmd;
65
static ata_ctl_t *ctl;
69
static ata_ctl_t *ctl;
66
 
70
 
67
static dev_handle_t dev_handle[MAX_DISKS];
71
/** Per-disk state. */
68
 
-
 
69
static atomic_t dev_futex = FUTEX_INITIALIZER;
-
 
70
 
-
 
71
static disk_t disk[MAX_DISKS];
72
static disk_t disk[MAX_DISKS];
72
 
73
 
73
static int ata_bd_init(void);
74
static int ata_bd_init(void);
74
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);
75
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, off_t size,
76
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, size_t size,
76
    void *buf);
77
    void *buf);
77
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,
78
    void *buf);
79
    void *buf);
79
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,
80
    const void *buf);
81
    const void *buf);
Line 87... Line 88...
87
    int i, rc;
88
    int i, rc;
88
    int n_disks;
89
    int n_disks;
89
 
90
 
90
    printf(NAME ": ATA disk driver\n");
91
    printf(NAME ": ATA disk driver\n");
91
 
92
 
92
    printf("cmd_physical = 0x%x\n", cmd_physical);
93
    printf("I/O address 0x%x\n", cmd_physical);
93
    printf("ctl_physical = 0x%x\n", ctl_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...\n");
99
    printf("Reset drives... ");
-
 
100
    fflush(stdout);
-
 
101
 
100
    pio_write_8(&ctl->device_control, DCR_SRST);
102
    pio_write_8(&ctl->device_control, DCR_SRST);
101
    /* FIXME: Find out how to do this properly. */
103
    /* FIXME: Find out how to do this properly. */
102
    async_usleep(100);
104
    async_usleep(100);
103
    pio_write_8(&ctl->device_control, 0);
105
    pio_write_8(&ctl->device_control, 0);
104
 
106
 
105
    do {
107
    do {
106
        status = pio_read_8(&cmd->status);
108
        status = pio_read_8(&cmd->status);
107
    } while ((status & SR_BSY) != 0);
109
    } while ((status & SR_BSY) != 0);
108
    printf("Done\n");
110
    printf("Done\n");
109
 
111
 
110
    printf("Status = 0x%x\n", pio_read_8(&cmd->status));
-
 
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
 
Line 118... Line 118...
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, &dev_handle[i]);
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;
Line 134... Line 134...
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
    async_manager();
140
    async_manager();
140
 
141
 
141
    /* Not reached */
142
    /* Not reached */
142
    return 0;
143
    return 0;
143
}
144
}
144
 
145
 
145
static int drive_identify(int disk_id, disk_t *d)
146
static int drive_identify(int disk_id, disk_t *d)
146
{
147
{
147
    uint16_t data;
148
    uint16_t data;
148
    uint8_t status;
149
    uint8_t status;
149
    int i;
150
    size_t i;
-
 
151
 
-
 
152
    printf("Identify drive %d... ", disk_id);
-
 
153
    fflush(stdout);
150
 
154
 
151
    printf("Identify drive %d\n", disk_id);
-
 
152
    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));
153
    async_usleep(100);
156
    async_usleep(100);
154
    pio_write_8(&cmd->command, CMD_IDENTIFY_DRIVE);
157
    pio_write_8(&cmd->command, CMD_IDENTIFY_DRIVE);
155
 
158
 
156
    status = pio_read_8(&cmd->status);
159
    status = pio_read_8(&cmd->status);
157
    printf("Status = 0x%x\n", status);
-
 
158
 
160
 
159
    d->present = false;
161
    d->present = false;
160
 
162
 
161
    /*
163
    /*
162
     * Detect if drive is present. This is Qemu only! Need to
164
     * Detect if drive is present. This is Qemu only! Need to
Line 179... Line 181...
179
        case 3: d->heads = data; break;
181
        case 3: d->heads = data; break;
180
        case 6: d->sectors = data; break;
182
        case 6: d->sectors = data; break;
181
        }
183
        }
182
    }
184
    }
183
 
185
 
184
    printf("\n\nStatus = 0x%x\n", pio_read_8(&cmd->status));
-
 
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
 
193
 
193
    return EOK;
194
    return EOK;
194
}
195
}
195
 
196
 
196
static int ata_bd_init(void)
197
static int ata_bd_init(void)
Line 232... Line 233...
232
    ipcarg_t method;
233
    ipcarg_t method;
233
    dev_handle_t dh;
234
    dev_handle_t dh;
234
    int flags;
235
    int flags;
235
    int retval;
236
    int retval;
236
    off_t idx;
237
    off_t idx;
237
    off_t size;
238
    size_t size;
238
    int disk_id, i;
239
    int disk_id, i;
239
 
240
 
240
    /* Get the device handle. */
241
    /* Get the device handle. */
241
    dh = IPC_GET_ARG1(*icall);
242
    dh = IPC_GET_ARG1(*icall);
242
 
243
 
243
    /* Determine which disk device is the client connecting to. */
244
    /* Determine which disk device is the client connecting to. */
244
    disk_id = -1;
245
    disk_id = -1;
245
    for (i = 0; i < MAX_DISKS; i++)
246
    for (i = 0; i < MAX_DISKS; i++)
246
        if (dev_handle[i] == dh)
247
        if (disk[i].dev_handle == dh)
247
            disk_id = i;
248
            disk_id = i;
248
 
249
 
249
    if (disk_id < 0 || disk[disk_id].present == false) {
250
    if (disk_id < 0 || disk[disk_id].present == false) {
250
        ipc_answer_0(iid, EINVAL);
251
        ipc_answer_0(iid, EINVAL);
251
        return;
252
        return;
Line 292... Line 293...
292
        }
293
        }
293
        ipc_answer_0(callid, retval);
294
        ipc_answer_0(callid, retval);
294
    }
295
    }
295
}
296
}
296
 
297
 
297
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t blk_idx, off_t size,
298
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t blk_idx, size_t size,
298
    void *buf)
299
    void *buf)
299
{
300
{
300
    int rc;
301
    int rc;
301
    off_t now;
302
    size_t now;
302
 
303
 
303
    while (size > 0) {
304
    while (size > 0) {
304
        now = size < block_size ? size : (off_t) block_size;
305
        now = size < block_size ? size : block_size;
305
        if (now != block_size)
306
        if (now != block_size)
306
            return EINVAL;
307
            return EINVAL;
307
 
308
 
308
        if (method == BD_READ_BLOCK)
309
        if (method == BD_READ_BLOCK)
309
            rc = ata_bd_read_block(disk_id, blk_idx, 1, buf);
310
            rc = ata_bd_read_block(disk_id, blk_idx, 1, buf);
Line 353... Line 354...
353
    /* New value for Drive/Head register */
354
    /* New value for Drive/Head register */
354
    drv_head =
355
    drv_head =
355
        ((disk_id != 0) ? DHR_DRV : 0) |
356
        ((disk_id != 0) ? DHR_DRV : 0) |
356
        (h & 0x0f);
357
        (h & 0x0f);
357
 
358
 
358
    futex_down(&dev_futex);
359
    fibril_mutex_lock(&d->lock);
359
 
360
 
360
    /* Program a Read Sectors operation. */
361
    /* Program a Read Sectors operation. */
361
 
362
 
362
    pio_write_8(&cmd->drive_head, drv_head);
363
    pio_write_8(&cmd->drive_head, drv_head);
363
    pio_write_8(&cmd->sector_count, 1);
364
    pio_write_8(&cmd->sector_count, 1);
Line 375... Line 376...
375
 
376
 
376
        data = pio_read_16(&cmd->data_port);
377
        data = pio_read_16(&cmd->data_port);
377
        ((uint16_t *) buf)[i] = data;
378
        ((uint16_t *) buf)[i] = data;
378
    }
379
    }
379
 
380
 
380
    futex_up(&dev_futex);
381
    fibril_mutex_unlock(&d->lock);
381
    return EOK;
382
    return EOK;
382
}
383
}
383
 
384
 
384
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,
385
    const void *buf)
386
    const void *buf)
Line 407... Line 408...
407
    /* New value for Drive/Head register */
408
    /* New value for Drive/Head register */
408
    drv_head =
409
    drv_head =
409
        ((disk_id != 0) ? DHR_DRV : 0) |
410
        ((disk_id != 0) ? DHR_DRV : 0) |
410
        (h & 0x0f);
411
        (h & 0x0f);
411
 
412
 
412
    futex_down(&dev_futex);
413
    fibril_mutex_lock(&d->lock);
413
 
414
 
414
    /* Program a Read Sectors operation. */
415
    /* Program a Read Sectors operation. */
415
 
416
 
416
    pio_write_8(&cmd->drive_head, drv_head);
417
    pio_write_8(&cmd->drive_head, drv_head);
417
    pio_write_8(&cmd->sector_count, 1);
418
    pio_write_8(&cmd->sector_count, 1);
Line 428... Line 429...
428
        } while ((status & SR_DRDY) == 0);
429
        } while ((status & SR_DRDY) == 0);
429
 
430
 
430
        pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]);
431
        pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]);
431
    }
432
    }
432
 
433
 
433
    futex_up(&dev_futex);
434
    fibril_mutex_unlock(&d->lock);
434
    return EOK;
435
    return EOK;
435
}
436
}
436
 
437
 
437
 
438
 
438
/**
439
/**