AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
ata.h
Go to the documentation of this file.
1#ifndef _ATA_H
2#define _ATA_H
3
4/*
5 * The ATA driver's own definitions: the drives' registers, commands and
6 * status bits, its structures for channels and drives, and the helpers
7 * that ata.c, pio.c and dma.c share.
8 */
9
10/* the kinds of drive that detection finds */
11#define ATADEV_UNKNOWN 0x000
12#define ATADEV_NOTFOUND 0x001
13#define ATADEV_PATA 0x002
14#define ATADEV_SATA 0x003
15#define ATADEV_PATAPI 0x004
16#define ATADEV_SATAPI 0x005
17
18/* the channels' ports in compatibility mode, the ISA's */
19#define ATA_PIO_PRI_PORT_BASE 0x1F0
20#define ATA_PIO_PRI_PORT_CONTROL 0x3F6
21#define ATA_PIO_SEC_PORT_BASE 0x170
22#define ATA_PIO_SEC_PORT_CONTROL 0x376
23
24/* the ATA registers, from the channel's base */
25#define ATA_REG_DATA 0x00
26#define ATA_REG_ERROR 0x01
27#define ATA_REG_FEATURES 0x01
28#define ATA_REG_SECCOUNT0 0x02
29#define ATA_REG_LBA0 0x03
30#define ATA_REG_LBA1 0x04
31#define ATA_REG_LBA2 0x05
32#define ATA_REG_HDDEVSEL 0x06
33#define ATA_REG_CMD 0x07
34#define ATA_REG_STATUS 0x07
35#define ATA_REG_SECCOUNT1 0x08
36#define ATA_REG_LBA3 0x09
37#define ATA_REG_LBA4 0x0A
38#define ATA_REG_LBA5 0x0B
39#define ATA_REG_CONTROL 0x0C
40#define ATA_REG_ALTSTATUS 0x0C
41#define ATA_REG_DEVADDRESS 0x0D
42
43/* the ATA commands */
44#define ATA_CMD_RESET 0x04
45#define ATA_CMD_READ_SECTORS 0x20
46#define ATA_CMD_READ_SECTORS_EXT 0x24
47#define ATA_CMD_WRITE_SECTORS 0x30
48#define ATA_CMD_WRITE_SECTORS_EXT 0x34
49#define ATA_CMD_READ_DMA 0xC8
50#define ATA_CMD_READ_DMA_EXT 0x25
51#define ATA_CMD_WRITE_DMA 0xCA
52#define ATA_CMD_WRITE_DMA_EXT 0x35
53#define ATA_CMD_CACHE_FLUSH 0xE7
54#define ATA_CMD_CACHE_FLUSH_EXT 0xEA
55#define ATA_CMD_PACKET 0xA0
56#define ATA_CMD_IDENTIFY_PACKET 0xA1
57#define ATA_CMD_IDENTIFY 0xEC
58
59#define ATA_DRIVE_LBA48 0x40
60
61/* the status register's bits */
62#define ATA_STATUS_BSY 0x80 /* busy */
63#define ATA_STATUS_DRDY 0x40 /* drive ready */
64#define ATA_STATUS_DF 0x20 /* drive write fault */
65#define ATA_STATUS_DSC 0x10 /* drive seek complete */
66#define ATA_STATUS_DRQ 0x08 /* ready to transfer data */
67#define ATA_STATUS_CORR 0x04 /* corrected data */
68#define ATA_STATUS_IDX 0x02 /* index */
69#define ATA_STATUS_ERR 0x01 /* error */
70
71/* the error register's bits */
72#define ATA_ERROR_BBK 0x080 /* bad block */
73#define ATA_ERROR_UNC 0x040 /* uncorrectable data */
74#define ATA_ERROR_MC 0x020 /* media changed */
75#define ATA_ERROR_IDNF 0x010 /* ID mark not found */
76#define ATA_ERROR_MCR 0x008 /* media change request */
77#define ATA_ERROR_ABRT 0x004 /* command aborted */
78#define ATA_ERROR_TK0NF 0x002 /* track 0 not found */
79#define ATA_ERROR_AMNF 0x001 /* no address mark */
80
81/* where things are in the IDENTIFY command's reply, in bytes */
82#define ATA_IDENT_DEVICETYPE 0
83#define ATA_IDENT_CYLINDERS 2
84#define ATA_IDENT_HEADS 6
85#define ATA_IDENT_SECTORS 12
86#define ATA_IDENT_SERIAL 20
87#define ATA_IDENT_MODEL 54
88#define ATA_IDENT_CAPABILITIES 98
89#define ATA_IDENT_FIELDVALID 106
90#define ATA_IDENT_MAX_LBA 120
91#define ATA_IDENT_COMMANDSETS 164
92#define ATA_IDENT_MAX_LBA_EXT 200
93
94/* the capabilities word's bits */
95#define ATA_CAP_DMA 0x100
96#define ATA_CAP_LBA 0x200
97
98/* how a drive's sectors are addressed */
99#define ATA_MODE_CHS 0x10
100#define ATA_MODE_LBA28 0x20
101#define ATA_MODE_LBA48 0x40
102
103#include <ds/queue.h>
104#include <aquila/errno.h>
105
110 struct ioaddr base;
111 struct ioaddr bm;
112 int irq;
113
116 int busy;
117 struct waitq wait;
118
121 volatile int done;
123};
124
126struct ata_drive {
127 struct ioaddr base;
128 struct ioaddr ctrl;
130
135 size_t poff[4]; /* each partition's first sector */
136 size_t psize[4]; /* each partition's size in sectors, 0 until
137 * the MBR was read */
138
139
144 char model[41];
145
146 /* how sectors are moved: by PIO (pio.c) or DMA (dma.c) */
147 ssize_t (*read) (struct ata_drive *drive, uint64_t lba, size_t count, void *buf);
148 ssize_t (*write)(struct ata_drive *drive, uint64_t lba, size_t count, void *buf);
149 int (*flush)(struct ata_drive *drive);
150};
151
153static const char *ata_error_string(uint8_t err)
154{
155 switch (err) {
156 case ATA_ERROR_BBK: return "Bad block";
157 case ATA_ERROR_UNC: return "Uncorrectable data";
158 case ATA_ERROR_MC: return "Media changed";
159 case ATA_ERROR_IDNF: return "ID mark not found";
160 case ATA_ERROR_MCR: return "Media change request";
161 case ATA_ERROR_ABRT: return "Command aborted";
162 case ATA_ERROR_TK0NF: return "Track 0 not found";
163 case ATA_ERROR_AMNF: return "No address mark";
164 default: return "Unknown";
165 }
166}
167
170static void ata_wait(struct ata_drive *drive)
171{
172 /* the alternate status register is at offset 0 of the control block */
173 for (int i = 0; i < 4; ++i)
174 io_in8(&drive->ctrl, 0);
175}
176
177/* the most status reads while waiting for a drive: a drive is busy for
178 * milliseconds, and one that never answers mustn't be waited for forever,
179 * with interrupts off */
180#define ATA_TIMEOUT 1000000
181
189static int ata_poll(struct ata_drive *drive, int advanced_check)
190{
191 ata_wait(drive);
192
194
195 for (int i = 0; i < ATA_TIMEOUT && (s & ATA_STATUS_BSY); ++i)
196 s = io_in8(&drive->base, ATA_REG_STATUS);
197
198 if (s & ATA_STATUS_BSY) {
199 printk("ata: drive busy, not answering\n");
200 return -ETIMEDOUT;
201 }
202
203 if (advanced_check) {
204 if ((s & ATA_STATUS_ERR) || (s & ATA_STATUS_DF)) {
205 ata_wait(drive);
206 uint8_t err = io_in8(&drive->base, ATA_REG_ERROR);
207 printk("ata: drive error: %s\n", ata_error_string(err));
208 return err == ATA_ERROR_ABRT ? -EAGAIN : -EIO;
209 }
210
211 if (!(s & ATA_STATUS_DRQ)) {
212 printk("ata: drive error: DRQ not set\n");
213 return -EIO;
214 }
215 }
216
217 return 0;
218}
219
220/* ata.c: resets a drive's channel */
221void ata_reset(struct ata_drive *drive);
222
223/* ata.c: selects a drive on its channel, for an addressing mode */
224void ata_select_drive(struct ata_drive *drive, uint32_t mode);
225
226/* ata.c: writes the sectors' address and count for the next command */
227void ata_setup(struct ata_drive *drive, uint64_t lba, size_t count);
228
229/* pio.c: sets a drive up for PIO */
230void pio_init(struct ata_drive *drive);
231
232/* dma.c: sets a drive up for DMA, if it and its controller can */
233int dma_init(struct ata_drive *drive);
234
235/* dma.c: handles a channel's interrupt */
236void dma_irq(struct ata_channel *channel);
237
238#endif
static uint8_t io_in8(struct ioaddr *io, uintptr_t off)
Reads an 8-, 16- or 32-bit register.
Definition io.h:28
void ata_select_drive(struct ata_drive *drive, uint32_t mode)
Selects a drive on its channel, for an addressing mode, if it isn't selected so already,...
Definition ata.c:126
void ata_setup(struct ata_drive *drive, uint64_t lba, size_t count)
Writes the registers of a command on count sectors from lba: the drive, the address and the count.
Definition ata.c:155
void ata_reset(struct ata_drive *drive)
Resets both drives on a drive's channel, and waits until they are done.
Definition ata.c:97
void dma_irq(struct ata_channel *channel)
Handles a channel's interrupt.
Definition dma.c:76
void pio_init(struct ata_drive *drive)
Sets a drive up for PIO, and resets its channel.
Definition pio.c:182
#define ATA_ERROR_AMNF
Definition ata.h:79
#define ATA_ERROR_BBK
Definition ata.h:72
#define ATA_REG_STATUS
Definition ata.h:34
#define ATA_ERROR_MCR
Definition ata.h:76
static int ata_poll(struct ata_drive *drive, int advanced_check)
Waits until a drive is done with what it was doing (BSY clears).
Definition ata.h:189
#define ATA_ERROR_UNC
Definition ata.h:73
#define ATA_TIMEOUT
Definition ata.h:180
#define ATA_ERROR_IDNF
Definition ata.h:75
#define ATA_ERROR_TK0NF
Definition ata.h:78
static void ata_wait(struct ata_drive *drive)
Waits about 400 ns, as a drive needs after a command, by reading the alternate status register four t...
Definition ata.h:170
int dma_init(struct ata_drive *drive)
Sets a drive up for DMA, if both the drive and its channel's controller can do it.
Definition dma.c:195
#define ATA_STATUS_BSY
Definition ata.h:62
#define ATA_ERROR_MC
Definition ata.h:74
#define ATA_STATUS_DRQ
Definition ata.h:66
#define ATA_ERROR_ABRT
Definition ata.h:77
#define ATA_REG_ERROR
Definition ata.h:26
static const char * ata_error_string(uint8_t err)
Returns the name of an error, from the error register.
Definition ata.h:153
#define ATA_STATUS_DF
Definition ata.h:64
#define ATA_STATUS_ERR
Definition ata.h:69
int printk(const char *fmt,...)
Writes a kernel message, to the message buffer and the consoles.
Definition printk.c:249
unsigned int uint32_t
Definition stddef.h:22
unsigned char uint8_t
Definition stddef.h:20
unsigned long long int uint64_t
Definition stddef.h:23
unsigned short int uint16_t
Definition stddef.h:21
An IDE channel, with the master and slave drives on it.
Definition ata.h:107
int irq
Definition ata.h:112
struct waitq wait
Definition ata.h:117
int busy
one command at a time: whether one is running, and the requests waiting for the channel
Definition ata.h:116
struct ioaddr base
the command block's registers, and the bus master's for DMA (whose address is 0 if the controller has...
Definition ata.h:110
struct ioaddr bm
Definition ata.h:111
volatile int done
Definition ata.h:121
struct waitq irq_wait
the DMA transfer waiting for the interrupt, and what it found
Definition ata.h:120
uint8_t status
Definition ata.h:122
uint8_t bm_status
Definition ata.h:122
A drive on a channel.
Definition ata.h:126
uint8_t id
Definition ata.h:131
uint8_t slave
Definition ata.h:134
struct ioaddr ctrl
Definition ata.h:128
struct ioaddr base
Definition ata.h:127
ssize_t(*) read(struct ata_drive *drive, uint64_t lba, size_t count, void *buf)
Definition ata.h:147
struct ata_channel * channel
Definition ata.h:129
int(*) flush(struct ata_drive *drive)
Definition ata.h:149
uint16_t capabilities
Definition ata.h:141
size_t psize[4]
Definition ata.h:136
uint64_t max_lba
Definition ata.h:143
uint16_t type
Definition ata.h:133
ssize_t(*) write(struct ata_drive *drive, uint64_t lba, size_t count, void *buf)
Definition ata.h:148
uint32_t command_sets
Definition ata.h:142
uint8_t mode
Definition ata.h:132
uint16_t signature
Definition ata.h:140
size_t poff[4]
Definition ata.h:135
char model[41]
Definition ata.h:144
Where a device's registers are.
Definition io.h:95
Definition waitq.h:19
long int ssize_t
Definition types.h:12