AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
ata.h File Reference
#include <ds/queue.h>
#include <aquila/errno.h>

Go to the source code of this file.

Data Structures

struct  ata_channel
 An IDE channel, with the master and slave drives on it. More...
struct  ata_drive
 A drive on a channel. More...

Macros

#define ATADEV_UNKNOWN   0x000
#define ATADEV_NOTFOUND   0x001
#define ATADEV_PATA   0x002
#define ATADEV_SATA   0x003
#define ATADEV_PATAPI   0x004
#define ATADEV_SATAPI   0x005
#define ATA_PIO_PRI_PORT_BASE   0x1F0
#define ATA_PIO_PRI_PORT_CONTROL   0x3F6
#define ATA_PIO_SEC_PORT_BASE   0x170
#define ATA_PIO_SEC_PORT_CONTROL   0x376
#define ATA_REG_DATA   0x00
#define ATA_REG_ERROR   0x01
#define ATA_REG_FEATURES   0x01
#define ATA_REG_SECCOUNT0   0x02
#define ATA_REG_LBA0   0x03
#define ATA_REG_LBA1   0x04
#define ATA_REG_LBA2   0x05
#define ATA_REG_HDDEVSEL   0x06
#define ATA_REG_CMD   0x07
#define ATA_REG_STATUS   0x07
#define ATA_REG_SECCOUNT1   0x08
#define ATA_REG_LBA3   0x09
#define ATA_REG_LBA4   0x0A
#define ATA_REG_LBA5   0x0B
#define ATA_REG_CONTROL   0x0C
#define ATA_REG_ALTSTATUS   0x0C
#define ATA_REG_DEVADDRESS   0x0D
#define ATA_CMD_RESET   0x04
#define ATA_CMD_READ_SECTORS   0x20
#define ATA_CMD_READ_SECTORS_EXT   0x24
#define ATA_CMD_WRITE_SECTORS   0x30
#define ATA_CMD_WRITE_SECTORS_EXT   0x34
#define ATA_CMD_READ_DMA   0xC8
#define ATA_CMD_READ_DMA_EXT   0x25
#define ATA_CMD_WRITE_DMA   0xCA
#define ATA_CMD_WRITE_DMA_EXT   0x35
#define ATA_CMD_CACHE_FLUSH   0xE7
#define ATA_CMD_CACHE_FLUSH_EXT   0xEA
#define ATA_CMD_PACKET   0xA0
#define ATA_CMD_IDENTIFY_PACKET   0xA1
#define ATA_CMD_IDENTIFY   0xEC
#define ATA_DRIVE_LBA48   0x40
#define ATA_STATUS_BSY   0x80
#define ATA_STATUS_DRDY   0x40
#define ATA_STATUS_DF   0x20
#define ATA_STATUS_DSC   0x10
#define ATA_STATUS_DRQ   0x08
#define ATA_STATUS_CORR   0x04
#define ATA_STATUS_IDX   0x02
#define ATA_STATUS_ERR   0x01
#define ATA_ERROR_BBK   0x080
#define ATA_ERROR_UNC   0x040
#define ATA_ERROR_MC   0x020
#define ATA_ERROR_IDNF   0x010
#define ATA_ERROR_MCR   0x008
#define ATA_ERROR_ABRT   0x004
#define ATA_ERROR_TK0NF   0x002
#define ATA_ERROR_AMNF   0x001
#define ATA_IDENT_DEVICETYPE   0
#define ATA_IDENT_CYLINDERS   2
#define ATA_IDENT_HEADS   6
#define ATA_IDENT_SECTORS   12
#define ATA_IDENT_SERIAL   20
#define ATA_IDENT_MODEL   54
#define ATA_IDENT_CAPABILITIES   98
#define ATA_IDENT_FIELDVALID   106
#define ATA_IDENT_MAX_LBA   120
#define ATA_IDENT_COMMANDSETS   164
#define ATA_IDENT_MAX_LBA_EXT   200
#define ATA_CAP_DMA   0x100
#define ATA_CAP_LBA   0x200
#define ATA_MODE_CHS   0x10
#define ATA_MODE_LBA28   0x20
#define ATA_MODE_LBA48   0x40
#define ATA_TIMEOUT   1000000

Functions

static const char * ata_error_string (uint8_t err)
 Returns the name of an error, from the error register.
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 times.
static int ata_poll (struct ata_drive *drive, int advanced_check)
 Waits until a drive is done with what it was doing (BSY clears).
void ata_reset (struct ata_drive *drive)
 Resets both drives on a drive's channel, and waits until they are done.
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, and waits for the drive to take it.
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.
void pio_init (struct ata_drive *drive)
 Sets a drive up for PIO, and resets its channel.
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.
void dma_irq (struct ata_channel *channel)
 Handles a channel's interrupt.

Macro Definition Documentation

◆ ATA_CAP_DMA

#define ATA_CAP_DMA   0x100

Definition at line 95 of file ata.h.

Referenced by dma_init().

◆ ATA_CAP_LBA

#define ATA_CAP_LBA   0x200

Definition at line 96 of file ata.h.

Referenced by pio_read_sectors(), and pio_write_sectors().

◆ ATA_CMD_CACHE_FLUSH

#define ATA_CMD_CACHE_FLUSH   0xE7

Definition at line 53 of file ata.h.

Referenced by pio_flush().

◆ ATA_CMD_CACHE_FLUSH_EXT

#define ATA_CMD_CACHE_FLUSH_EXT   0xEA

Definition at line 54 of file ata.h.

Referenced by pio_flush().

◆ ATA_CMD_IDENTIFY

#define ATA_CMD_IDENTIFY   0xEC

Definition at line 57 of file ata.h.

Referenced by ata_detect_drive().

◆ ATA_CMD_IDENTIFY_PACKET

#define ATA_CMD_IDENTIFY_PACKET   0xA1

Definition at line 56 of file ata.h.

Referenced by ata_detect_drive().

◆ ATA_CMD_PACKET

#define ATA_CMD_PACKET   0xA0

Definition at line 55 of file ata.h.

◆ ATA_CMD_READ_DMA

#define ATA_CMD_READ_DMA   0xC8

Definition at line 49 of file ata.h.

Referenced by dma_transfer().

◆ ATA_CMD_READ_DMA_EXT

#define ATA_CMD_READ_DMA_EXT   0x25

Definition at line 50 of file ata.h.

Referenced by dma_transfer().

◆ ATA_CMD_READ_SECTORS

#define ATA_CMD_READ_SECTORS   0x20

Definition at line 45 of file ata.h.

Referenced by pio_read_sectors().

◆ ATA_CMD_READ_SECTORS_EXT

#define ATA_CMD_READ_SECTORS_EXT   0x24

Definition at line 46 of file ata.h.

Referenced by pio_read_sectors().

◆ ATA_CMD_RESET

#define ATA_CMD_RESET   0x04

Definition at line 44 of file ata.h.

Referenced by ata_reset().

◆ ATA_CMD_WRITE_DMA

#define ATA_CMD_WRITE_DMA   0xCA

Definition at line 51 of file ata.h.

Referenced by dma_transfer().

◆ ATA_CMD_WRITE_DMA_EXT

#define ATA_CMD_WRITE_DMA_EXT   0x35

Definition at line 52 of file ata.h.

Referenced by dma_transfer().

◆ ATA_CMD_WRITE_SECTORS

#define ATA_CMD_WRITE_SECTORS   0x30

Definition at line 47 of file ata.h.

Referenced by pio_write_sectors().

◆ ATA_CMD_WRITE_SECTORS_EXT

#define ATA_CMD_WRITE_SECTORS_EXT   0x34

Definition at line 48 of file ata.h.

Referenced by pio_write_sectors().

◆ ATA_DRIVE_LBA48

#define ATA_DRIVE_LBA48   0x40

Definition at line 59 of file ata.h.

◆ ATA_ERROR_ABRT

#define ATA_ERROR_ABRT   0x004

Definition at line 77 of file ata.h.

Referenced by ata_error_string(), and ata_poll().

◆ ATA_ERROR_AMNF

#define ATA_ERROR_AMNF   0x001

Definition at line 79 of file ata.h.

Referenced by ata_error_string().

◆ ATA_ERROR_BBK

#define ATA_ERROR_BBK   0x080

Definition at line 72 of file ata.h.

Referenced by ata_error_string().

◆ ATA_ERROR_IDNF

#define ATA_ERROR_IDNF   0x010

Definition at line 75 of file ata.h.

Referenced by ata_error_string().

◆ ATA_ERROR_MC

#define ATA_ERROR_MC   0x020

Definition at line 74 of file ata.h.

Referenced by ata_error_string().

◆ ATA_ERROR_MCR

#define ATA_ERROR_MCR   0x008

Definition at line 76 of file ata.h.

Referenced by ata_error_string().

◆ ATA_ERROR_TK0NF

#define ATA_ERROR_TK0NF   0x002

Definition at line 78 of file ata.h.

Referenced by ata_error_string().

◆ ATA_ERROR_UNC

#define ATA_ERROR_UNC   0x040

Definition at line 73 of file ata.h.

Referenced by ata_error_string().

◆ ATA_IDENT_CAPABILITIES

#define ATA_IDENT_CAPABILITIES   98

Definition at line 88 of file ata.h.

Referenced by ata_detect_drive().

◆ ATA_IDENT_COMMANDSETS

#define ATA_IDENT_COMMANDSETS   164

Definition at line 91 of file ata.h.

Referenced by ata_detect_drive().

◆ ATA_IDENT_CYLINDERS

#define ATA_IDENT_CYLINDERS   2

Definition at line 83 of file ata.h.

◆ ATA_IDENT_DEVICETYPE

#define ATA_IDENT_DEVICETYPE   0

Definition at line 82 of file ata.h.

Referenced by ata_detect_drive().

◆ ATA_IDENT_FIELDVALID

#define ATA_IDENT_FIELDVALID   106

Definition at line 89 of file ata.h.

◆ ATA_IDENT_HEADS

#define ATA_IDENT_HEADS   6

Definition at line 84 of file ata.h.

◆ ATA_IDENT_MAX_LBA

#define ATA_IDENT_MAX_LBA   120

Definition at line 90 of file ata.h.

Referenced by ata_detect_drive().

◆ ATA_IDENT_MAX_LBA_EXT

#define ATA_IDENT_MAX_LBA_EXT   200

Definition at line 92 of file ata.h.

Referenced by ata_detect_drive().

◆ ATA_IDENT_MODEL

#define ATA_IDENT_MODEL   54

Definition at line 87 of file ata.h.

Referenced by ata_detect_drive().

◆ ATA_IDENT_SECTORS

#define ATA_IDENT_SECTORS   12

Definition at line 85 of file ata.h.

◆ ATA_IDENT_SERIAL

#define ATA_IDENT_SERIAL   20

Definition at line 86 of file ata.h.

◆ ATA_MODE_CHS

#define ATA_MODE_CHS   0x10

Definition at line 99 of file ata.h.

◆ ATA_MODE_LBA28

#define ATA_MODE_LBA28   0x20

Definition at line 100 of file ata.h.

Referenced by ata_detect_drive(), and ata_select_drive().

◆ ATA_MODE_LBA48

#define ATA_MODE_LBA48   0x40

◆ ATA_PIO_PRI_PORT_BASE

#define ATA_PIO_PRI_PORT_BASE   0x1F0

Definition at line 19 of file ata.h.

Referenced by ata_probe().

◆ ATA_PIO_PRI_PORT_CONTROL

#define ATA_PIO_PRI_PORT_CONTROL   0x3F6

Definition at line 20 of file ata.h.

Referenced by ata_probe().

◆ ATA_PIO_SEC_PORT_BASE

#define ATA_PIO_SEC_PORT_BASE   0x170

Definition at line 21 of file ata.h.

Referenced by ata_probe().

◆ ATA_PIO_SEC_PORT_CONTROL

#define ATA_PIO_SEC_PORT_CONTROL   0x376

Definition at line 22 of file ata.h.

Referenced by ata_probe().

◆ ATA_REG_ALTSTATUS

#define ATA_REG_ALTSTATUS   0x0C

Definition at line 40 of file ata.h.

◆ ATA_REG_CMD

#define ATA_REG_CMD   0x07

Definition at line 33 of file ata.h.

Referenced by ata_detect_drive(), dma_transfer(), pio_flush(), pio_read_sectors(), and pio_write_sectors().

◆ ATA_REG_CONTROL

#define ATA_REG_CONTROL   0x0C

Definition at line 39 of file ata.h.

◆ ATA_REG_DATA

#define ATA_REG_DATA   0x00

Definition at line 25 of file ata.h.

Referenced by ata_detect_drive(), pio_read_sectors(), and pio_write_sectors().

◆ ATA_REG_DEVADDRESS

#define ATA_REG_DEVADDRESS   0x0D

Definition at line 41 of file ata.h.

◆ ATA_REG_ERROR

#define ATA_REG_ERROR   0x01

Definition at line 26 of file ata.h.

Referenced by ata_poll(), and ata_setup().

◆ ATA_REG_FEATURES

#define ATA_REG_FEATURES   0x01

Definition at line 27 of file ata.h.

◆ ATA_REG_HDDEVSEL

#define ATA_REG_HDDEVSEL   0x06

Definition at line 32 of file ata.h.

Referenced by ata_reset(), ata_select_drive(), and ata_setup().

◆ ATA_REG_LBA0

#define ATA_REG_LBA0   0x03

Definition at line 29 of file ata.h.

Referenced by ata_setup().

◆ ATA_REG_LBA1

#define ATA_REG_LBA1   0x04

Definition at line 30 of file ata.h.

Referenced by ata_detect_drive(), ata_drive_present(), and ata_setup().

◆ ATA_REG_LBA2

#define ATA_REG_LBA2   0x05

Definition at line 31 of file ata.h.

Referenced by ata_detect_drive(), ata_drive_present(), and ata_setup().

◆ ATA_REG_LBA3

#define ATA_REG_LBA3   0x09

Definition at line 36 of file ata.h.

◆ ATA_REG_LBA4

#define ATA_REG_LBA4   0x0A

Definition at line 37 of file ata.h.

◆ ATA_REG_LBA5

#define ATA_REG_LBA5   0x0B

Definition at line 38 of file ata.h.

◆ ATA_REG_SECCOUNT0

#define ATA_REG_SECCOUNT0   0x02

Definition at line 28 of file ata.h.

Referenced by ata_setup().

◆ ATA_REG_SECCOUNT1

#define ATA_REG_SECCOUNT1   0x08

Definition at line 35 of file ata.h.

◆ ATA_REG_STATUS

#define ATA_REG_STATUS   0x07

Definition at line 34 of file ata.h.

Referenced by ata_detect_drive(), ata_drive_present(), ata_poll(), dma_complete(), and dma_irq().

◆ ATA_STATUS_BSY

#define ATA_STATUS_BSY   0x80

Definition at line 62 of file ata.h.

Referenced by ata_drive_present(), ata_poll(), and ata_reset().

◆ ATA_STATUS_CORR

#define ATA_STATUS_CORR   0x04

Definition at line 67 of file ata.h.

◆ ATA_STATUS_DF

#define ATA_STATUS_DF   0x20

Definition at line 64 of file ata.h.

Referenced by ata_poll(), and dma_transfer().

◆ ATA_STATUS_DRDY

#define ATA_STATUS_DRDY   0x40

Definition at line 63 of file ata.h.

Referenced by ata_drive_present().

◆ ATA_STATUS_DRQ

#define ATA_STATUS_DRQ   0x08

Definition at line 66 of file ata.h.

Referenced by ata_poll().

◆ ATA_STATUS_DSC

#define ATA_STATUS_DSC   0x10

Definition at line 65 of file ata.h.

◆ ATA_STATUS_ERR

#define ATA_STATUS_ERR   0x01

Definition at line 69 of file ata.h.

Referenced by ata_poll(), and dma_transfer().

◆ ATA_STATUS_IDX

#define ATA_STATUS_IDX   0x02

Definition at line 68 of file ata.h.

◆ ATA_TIMEOUT

#define ATA_TIMEOUT   1000000

Definition at line 180 of file ata.h.

Referenced by ata_poll(), and ata_reset().

◆ ATADEV_NOTFOUND

#define ATADEV_NOTFOUND   0x001

Definition at line 12 of file ata.h.

Referenced by ata_detect_drive(), and ata_probe().

◆ ATADEV_PATA

#define ATADEV_PATA   0x002

Definition at line 13 of file ata.h.

Referenced by ata_detect_drive(), and ata_probe().

◆ ATADEV_PATAPI

#define ATADEV_PATAPI   0x004

Definition at line 15 of file ata.h.

Referenced by ata_detect_drive(), and ata_probe().

◆ ATADEV_SATA

#define ATADEV_SATA   0x003

Definition at line 14 of file ata.h.

Referenced by ata_detect_drive(), and ata_probe().

◆ ATADEV_SATAPI

#define ATADEV_SATAPI   0x005

Definition at line 16 of file ata.h.

Referenced by ata_detect_drive(), and ata_probe().

◆ ATADEV_UNKNOWN

#define ATADEV_UNKNOWN   0x000

Definition at line 11 of file ata.h.

Referenced by ata_detect_drive().

Function Documentation

◆ ata_error_string()

const char * ata_error_string ( uint8_t err)
static

Returns the name of an error, from the error register.

Definition at line 153 of file ata.h.

References ATA_ERROR_ABRT, ATA_ERROR_AMNF, ATA_ERROR_BBK, ATA_ERROR_IDNF, ATA_ERROR_MC, ATA_ERROR_MCR, ata_error_string(), ATA_ERROR_TK0NF, and ATA_ERROR_UNC.

Referenced by ata_error_string(), and ata_poll().

◆ ata_poll()

int ata_poll ( struct ata_drive * drive,
int advanced_check )
static

Waits until a drive is done with what it was doing (BSY clears).

With advanced_check, it also checks that the drive is ready for data (DRQ) and not in error. Returns 0, -ETIMEDOUT if the drive stays busy, -EAGAIN if it aborted the command (which may be tried again), or -EIO for any other error.

Definition at line 189 of file ata.h.

References ATA_ERROR_ABRT, ata_error_string(), ATA_REG_ERROR, ATA_REG_STATUS, ATA_STATUS_BSY, ATA_STATUS_DF, ATA_STATUS_DRQ, ATA_STATUS_ERR, ATA_TIMEOUT, ata_wait(), ata_drive::base, io_in8(), and printk().

◆ ata_reset()

void ata_reset ( struct ata_drive * drive)

Resets both drives on a drive's channel, and waits until they are done.

Parameters
drivea drive on the channel

Definition at line 97 of file ata.c.

References ATA_CMD_RESET, ATA_REG_HDDEVSEL, ata_reset(), ATA_STATUS_BSY, ATA_TIMEOUT, ata_drive::base, ata_drive::ctrl, io_in8(), io_out8(), last_selected_drive, last_selected_mode, and NULL.

Referenced by ata_detect_drive(), ata_reset(), and pio_init().

◆ ata_select_drive()

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, and waits for the drive to take it.

Parameters
drivethe drive
modethe addressing mode (ATA_MODE_*), or 0 to detect it

Definition at line 126 of file ata.c.

References ATA_MODE_LBA28, ATA_MODE_LBA48, ATA_REG_HDDEVSEL, ata_select_drive(), ata_drive::base, io_out8(), last_selected_drive, last_selected_mode, ata_drive::mode, and ata_drive::slave.

Referenced by ata_detect_drive(), ata_select_drive(), ata_setup(), and pio_flush().

◆ ata_setup()

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.

The command itself is sent after.

Parameters
drivethe drive
lbathe first sector
counthow many sectors

Definition at line 155 of file ata.c.

References ATA_MODE_LBA48, ATA_REG_ERROR, ATA_REG_HDDEVSEL, ATA_REG_LBA0, ATA_REG_LBA1, ATA_REG_LBA2, ATA_REG_SECCOUNT0, ata_select_drive(), ata_setup(), ata_drive::base, io_out8(), ata_drive::mode, and ata_drive::slave.

Referenced by ata_setup(), dma_transfer(), pio_read_sectors(), and pio_write_sectors().

◆ ata_wait()

void ata_wait ( struct ata_drive * drive)
static

Waits about 400 ns, as a drive needs after a command, by reading the alternate status register four times.

Definition at line 170 of file ata.h.

References ata_drive::ctrl, and io_in8().

Referenced by ata_poll().

◆ dma_init()

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.

Parameters
drivethe drive
Returns
0 if the drive uses DMA now, or -ENODEV if it can't

Definition at line 195 of file dma.c.

References ioaddr::addr, ATA_CAP_DMA, ata_channel::bm, ata_drive::capabilities, ata_drive::channel, dma_init(), dma_read(), dma_write(), ata_drive::read, and ata_drive::write.

Referenced by ata_probe(), and dma_init().

◆ dma_irq()

void dma_irq ( struct ata_channel * ch)

Handles a channel's interrupt.

If it ends a DMA transfer, the request waiting for it wakes up; otherwise (PIO polls) reading the drive's status acknowledges it.

Parameters
chthe channel

Definition at line 76 of file dma.c.

References ioaddr::addr, ATA_REG_STATUS, ata_channel::base, ata_channel::bm, BM_ST_IRQ, BM_STATUS, dma_complete(), dma_irq(), io_in8(), ata_channel::irq_wait, and waitq_wakeup_nopoll().

Referenced by ata_irq(), and dma_irq().

◆ pio_init()

void pio_init ( struct ata_drive * drive)

Sets a drive up for PIO, and resets its channel.

Parameters
drivethe drive

Definition at line 182 of file pio.c.

References ata_reset(), ata_drive::flush, pio_flush(), pio_init(), pio_read(), pio_write(), ata_drive::read, and ata_drive::write.

Referenced by ata_probe(), and pio_init().