summaryrefslogtreecommitdiff
path: root/Kernel/IDEDiskDevice.cpp
blob: 944ffbe91c57ef4b9a71b6f35b9846ecca631a8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "IDEDiskDevice.h"
#include "types.h"
#include "Process.h"
#include "StdLib.h"
#include "IO.h"
#include "Scheduler.h"
#include "PIC.h"
#include <AK/Lock.h>

//#define DISK_DEBUG

#define IRQ_FIXED_DISK 14

#define IDE0_DATA        0x1F0
#define IDE0_STATUS      0x1F7
#define IDE0_COMMAND     0x1F7

enum IDECommand : byte {
    IDENTIFY_DRIVE = 0xEC,
    READ_SECTORS = 0x21,
    WRITE_SECTORS = 0x30,
};

enum IDEStatus : byte {
    BUSY = (1 << 7),
    DRDY = (1 << 6),
    DF   = (1 << 5),
    SRV  = (1 << 4),
    DRQ  = (1 << 3),
    CORR = (1 << 2),
    IDX  = (1 << 1),
    ERR  = (1 << 0),
};

RetainPtr<IDEDiskDevice> IDEDiskDevice::create()
{
    return adopt(*new IDEDiskDevice);
}

IDEDiskDevice::IDEDiskDevice()
    : IRQHandler(IRQ_FIXED_DISK)
{
    initialize();
}

IDEDiskDevice::~IDEDiskDevice()
{
}

const char* IDEDiskDevice::class_name() const
{
    return "IDEDiskDevice";
}

unsigned IDEDiskDevice::block_size() const
{
    return 512;
}

bool IDEDiskDevice::read_block(unsigned index, byte* out) const
{
    const_cast<IDEDiskDevice&>(*this).read_sectors(index, 1, out);
    return true;
}

bool IDEDiskDevice::write_block(unsigned index, const byte* data)
{
    write_sectors(index, 1, data);
    return true;
}

#ifdef DISK_DEBUG
static void print_ide_status(byte status)
{
    kprintf("DRQ=%u BUSY=%u DRDY=%u SRV=%u DF=%u CORR=%u IDX=%u ERR=%u\n",
            (status & DRQ) != 0,
            (status & BUSY) != 0,
            (status & DRDY) != 0,
            (status & SRV) != 0,
            (status & DF) != 0,
            (status & CORR) != 0,
            (status & IDX) != 0,
            (status & ERR) != 0);
}
#endif

bool IDEDiskDevice::wait_for_irq()
{
#ifdef DISK_DEBUG
    kprintf("disk: waiting for interrupt...\n");
#endif
    // FIXME: Add timeout.
    while (!m_interrupted) {
        // FIXME: Put this process into a Blocked state instead, it's stupid to wake up just to check a flag.
        Scheduler::yield();
    }
#ifdef DISK_DEBUG
    kprintf("disk: got interrupt!\n");
#endif
    return true;
}

void IDEDiskDevice::handle_irq()
{
#ifdef DISK_DEBUG
    byte status = IO::in8(0x1f7);
    kprintf("disk:interrupt: DRQ=%u BUSY=%u DRDY=%u\n", (status & DRQ) != 0, (status & BUSY) != 0, (status & DRDY) != 0);
#endif
    m_interrupted = true;
}

void IDEDiskDevice::initialize()
{
    byte status;

    status = IO::in8(IDE0_STATUS);
#ifdef DISK_DEBUG
    kprintf("initial status: ");
    print_ide_status(status);
#endif

    m_interrupted = false;

    while (IO::in8(IDE0_STATUS) & BUSY);

    enable_irq();

    IO::out8(0x1F6, 0xA0); // 0xB0 for 2nd device
    IO::out8(0x3F6, 0xA0); // 0xB0 for 2nd device
    IO::out8(IDE0_COMMAND, IDENTIFY_DRIVE);

    enable_irq();
    wait_for_irq();

    ByteBuffer wbuf = ByteBuffer::create_uninitialized(512);
    ByteBuffer bbuf = ByteBuffer::create_uninitialized(512);
    byte* b = bbuf.pointer();
    word* w = (word*)wbuf.pointer();
    const word* wbufbase = (word*)wbuf.pointer();

    for (dword i = 0; i < 256; ++i) {
        word data = IO::in16(IDE0_DATA);
        *(w++) = data;
        *(b++) = MSB(data);
        *(b++) = LSB(data);
    }

    // "Unpad" the device name string.
    for (dword i = 93; i > 54 && bbuf[i] == ' '; --i)
        bbuf[i] = 0;

    m_cylinders = wbufbase[1];
    m_heads = wbufbase[3];
    m_sectors_per_track = wbufbase[6];

    kprintf(
        "IDEDiskDevice: Master=\"%s\", C/H/Spt=%u/%u/%u\n",
        bbuf.pointer() + 54,
        m_cylinders,
        m_heads,
        m_sectors_per_track
    );
}

IDEDiskDevice::CHS IDEDiskDevice::lba_to_chs(dword lba) const
{
    CHS chs;
    chs.cylinder = lba / (m_sectors_per_track * m_heads);
    chs.head = (lba / m_sectors_per_track) % m_heads;
    chs.sector = (lba % m_sectors_per_track) + 1;
    return chs;
}

bool IDEDiskDevice::read_sectors(dword start_sector, word count, byte* outbuf)
{
    LOCKER(m_lock);
#ifdef DISK_DEBUG
    kprintf("%s: Disk::read_sectors request (%u sector(s) @ %u)\n",
            current->name().characters(),
            count,
            start_sector);
#endif
    disable_irq();

    auto chs = lba_to_chs(start_sector);

    while (IO::in8(IDE0_STATUS) & BUSY);

#ifdef DISK_DEBUG
    kprintf("IDEDiskDevice: Reading %u sector(s) @ LBA %u (%u/%u/%u)\n", count, start_sector, chs.cylinder, chs.head, chs.sector);
#endif

    IO::out8(0x1F2, count == 256 ? 0 : LSB(count));
    IO::out8(0x1F3, chs.sector);
    IO::out8(0x1F4, LSB(chs.cylinder));
    IO::out8(0x1F5, MSB(chs.cylinder));

    IO::out8(0x1F6, 0xA0 | chs.head); /* 0xB0 for 2nd device */

    IO::out8(0x3F6, 0x08);
    while (!(IO::in8(IDE0_STATUS) & DRDY));

    IO::out8(IDE0_COMMAND, READ_SECTORS);
    m_interrupted = false;
    enable_irq();
    wait_for_irq();

    byte status = IO::in8(0x1f7);
    if (status & DRQ) {
#ifdef DISK_DEBUG
        kprintf("Retrieving %u bytes (status=%b), outbuf=%p...\n", count * 512, status, outbuf);
#endif
        for (dword i = 0; i < (count * 512); i += 2) {
            word w = IO::in16(IDE0_DATA);
            outbuf[i] = LSB(w);
            outbuf[i+1] = MSB(w);
        }
    }

    return true;
}

bool IDEDiskDevice::write_sectors(dword start_sector, word count, const byte* data)
{
    LOCKER(m_lock);
#ifdef DISK_DEBUG
    dbgprintf("%s(%u): IDEDiskDevice::write_sectors request (%u sector(s) @ %u)\n",
            current->name().characters(),
            current->pid(),
            count,
            start_sector);
#endif
    disable_irq();

    auto chs = lba_to_chs(start_sector);

    while (IO::in8(IDE0_STATUS) & BUSY);

    //dbgprintf("IDEDiskDevice: Writing %u sector(s) @ LBA %u (%u/%u/%u)\n", count, start_sector, chs.cylinder, chs.head, chs.sector);

    IO::out8(0x1F2, count == 256 ? 0 : LSB(count));
    IO::out8(0x1F3, chs.sector);
    IO::out8(0x1F4, LSB(chs.cylinder));
    IO::out8(0x1F5, MSB(chs.cylinder));

    IO::out8(0x1F6, 0xA0 | chs.head); /* 0xB0 for 2nd device */

    IO::out8(0x3F6, 0x08);

    IO::out8(IDE0_COMMAND, WRITE_SECTORS);

    while (!(IO::in8(IDE0_STATUS) & DRQ));

    byte status = IO::in8(0x1f7);
    if (status & DRQ) {
        //dbgprintf("Sending %u bytes (status=%b), data=%p...\n", count * 512, status, data);
        auto* data_as_words = (const word*)data;
        for (dword i = 0; i < (count * 512) / 2; ++i) {
            IO::out16(IDE0_DATA, data_as_words[i]);
        }
    }

    m_interrupted = false;
    enable_irq();
    wait_for_irq();

    return true;
}