PCI IDE 控制器:接口、检测与 ATA/ATAPI 驱动器读写
文章目录
IDE 是一个关键字,指的是将 ATA 驱动器(如硬盘驱动器)连接到另一设备的电缆的电气规范。这些驱动器使用 ATA(Advanced Technology Attachment,高级技术附件)接口。IDE 电缆也可以终止于连接到 PCI 的 IDE 卡。
ATAPI 是 ATA(最近更名为 PATA)的扩展,它增加了对 SCSI 命令集的支持。
并行/串行 ATA/ATAPI
IDE 最多可以连接 4 个驱动器。每个驱动器可以是以下之一:
- ATA(串行):用于大多数现代硬盘驱动器。
- ATA(并行):常用于硬盘驱动器。
- ATAPI(串行):用于大多数现代光盘驱动器。
- ATAPI(并行):常用于光盘驱动器。
访问 ATA/PATA 驱动器的方式与访问 SATA 驱动器相同。这也隐含地说明访问 PATAPI ODD 与访问 SATAPI ODD 相同。IDE 驱动程序不需要知道驱动器是并行还是串行,它只需要知道使用的是 ATA 还是 ATAPI。
IDE 接口
如果你打开机箱并查看主板,你很可能会看到一个或两个(也可能更多)插槽。
白色和绿色的端口是 IDE 端口,也称为 通道。在这个例子中,有主 IDE 通道和次 IDE 通道,只能连接 PATA;这意味着它只支持 PATA/PATAPI 驱动器。
每个端口可以连接一根 PATA 电缆。一根 PATA 电缆可以连接一个主驱动器,或两个驱动器(主和从)。因此我们有以下可能性:
- 主通道主驱动器。
- 主通道从驱动器。
- 次通道主驱动器。
- 次通道从驱动器。
每个驱动器可以是 PATA 或 PATAPI。
串行 IDE
几乎每块现代(本文可能写于 2010 初,因此假设主板仍有 ide/ahci 模式)主板都有一个串行 IDE 通道,允许连接 SATA 和 SATAPI 驱动器。有 4 个串行 IDE 端口。每个端口通过一根 SATA 电缆连接到一个驱动器。基本上,串行 IDE 端口只能连接一个驱动器。每对端口(每 2 个端口)构成一个通道。
串行 IDE 也有几种可能性:
- 主通道主驱动器,也称为 SATA1。
- 主通道从驱动器,也称为 SATA2。
- 次通道主驱动器,也称为 SATA3。
- 次通道从驱动器,也称为 SATA4。
检测 PCI IDE 控制器
每个 IDE 控制器在 PCI 总线上表现为一个设备,可以通过读取配置空间来识别。如果类别代码是 0x01(大容量存储控制器)且子类别代码是 0x01(IDE),则该设备是 IDE 控制器。编程接口字节(Prog If)决定你将如何访问它。
- 位 0:置位时,主通道处于 PCI 原生模式。清零时,主通道处于兼容模式(端口 0x1F0-0x1F7、0x3F6、IRQ14)。
- 位 1:置位时,你可以修改位 0 以在 PCI 原生模式和兼容模式之间切换。清零时,你不能修改位 0。
- 位 2:置位时,次通道处于 PCI 原生模式。清零时,次通道处于兼容模式(端口 0x170-0x177、0x376、IRQ15)。
- 位 3:置位时,你可以修改位 2 以在 PCI 原生模式和兼容模式之间切换。清零时,你不能修改位 2。
- 位 7:置位时,这是一个总线主控 IDE 控制器。清零时,该控制器不支持 DMA。
如果你想以 PCI 原生模式访问 IDE 通道或使用总线主控功能,你必须额外读取 BAR 以确定要使用的 I/O 端口。
- BAR0:PCI 原生模式下主通道的基地址(8 个端口)
- BAR1:PCI 原生模式下主通道控制端口的基地址(4 个端口)
- BAR2:PCI 原生模式下次通道的基地址(8 个端口)
- BAR3:PCI 原生模式下次通道控制端口的基地址(4 个端口)
- BAR4:总线主控 IDE(16 个端口,每个通道 8 个)
注意 BAR1 和 BAR3 指定了 4 个端口,但只使用偏移 2 处的端口。偏移 0、1 和 3 不应被访问。
如果任一 IDE 通道处于 PCI 原生模式,你还必须读取中断线或中断引脚寄存器以确定使用哪个中断。如果两个通道都处于 PCI 原生模式,它们将共享同一个中断。中断线字段仅在使用 PIC 时有效。
检测 IDE 驱动器
要初始化 IDE 驱动程序,我们调用 ide_initialise:
void ide_initialize(unsigned int BAR0, unsigned int BAR1, unsigned int BAR2, unsigned int BAR3, unsigned int BAR4) {
如果你只想支持并行 IDE,可以使用这些参数:
ide_initialize(0x1F0, 0x3F6, 0x170, 0x376, 0x000);
我们可以假设 BAR4 是 0x0,因为我们暂时不会使用它。我们将回到 ide_initialize,它会搜索连接到 IDE 的驱动器。在进入这个函数之前,我们应该编写一些辅助函数和定义,它们会给我们很大帮助。
状态
命令/状态端口在读取时返回一个表示通道状态的位掩码。
#define ATA_SR_BSY 0x80 // Busy #define ATA_SR_DRDY 0x40 // Drive ready #define ATA_SR_DF 0x20 // Drive write fault #define ATA_SR_DSC 0x10 // Drive seek complete #define ATA_SR_DRQ 0x08 // Data request ready #define ATA_SR_CORR 0x04 // Corrected data #define ATA_SR_IDX 0x02 // Index #define ATA_SR_ERR 0x01 // Error
错误
功能/错误端口在读取时返回最近的错误,具有以下可能的位掩码:
#define ATA_ER_BBK 0x80 // Bad block #define ATA_ER_UNC 0x40 // Uncorrectable data #define ATA_ER_MC 0x20 // Media changed #define ATA_ER_IDNF 0x10 // ID mark not found #define ATA_ER_MCR 0x08 // Media change request #define ATA_ER_ABRT 0x04 // Command aborted #define ATA_ER_TK0NF 0x02 // Track 0 not found #define ATA_ER_AMNF 0x01 // No address mark
命令
当你写入命令/状态端口时,你正在执行下面的某个命令。
#define ATA_CMD_READ_PIO 0x20 #define ATA_CMD_READ_PIO_EXT 0x24 #define ATA_CMD_READ_DMA 0xC8 #define ATA_CMD_READ_DMA_EXT 0x25 #define ATA_CMD_WRITE_PIO 0x30 #define ATA_CMD_WRITE_PIO_EXT 0x34 #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
下面的命令用于 ATAPI 设备,稍后就会理解。
#define ATAPI_CMD_READ 0xA8 #define ATAPI_CMD_EJECT 0x1B
ATA_CMD_IDENTIFY_PACKET 和 ATA_CMD_IDENTIFY 返回一个 512 字节的缓冲区,称为标识空间;以下定义用于从标识空间读取信息。
#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 IDE_ATA 0x00 #define IDE_ATAPI 0x01 #define ATA_MASTER 0x00 #define ATA_SLAVE 0x01
任务文件是一组 8 个端口,它们是相对于 BAR0(主通道)和/或 BAR2(次通道)的偏移。举例说明:
- BAR0 + 0 是第一个端口。
- BAR0 + 1 是第二个端口。
- BAR0 + 2 是第三个
#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_COMMAND 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
ALTSTATUS/CONTROL 端口在读取时返回备用状态,在写入时控制通道。
- 对于主通道,ALTSTATUS/CONTROL 端口是 BAR1 + 2。
- 对于次通道,ALTSTATUS/CONTROL 端口是 BAR3 + 2。
我们现在可以说每个通道有 13 个寄存器。对于主通道,我们使用以下值:
- 数据寄存器:BAR0 + 0; // 读写
- 错误寄存器:BAR0 + 1; // 只读
- 功能寄存器:BAR0 + 1; // 只写
- SECCOUNT0:BAR0 + 2; // 读写
- LBA0:BAR0 + 3; // 读写
- LBA1:BAR0 + 4; // 读写
- LBA2:BAR0 + 5; // 读写
- HDDEVSEL:BAR0 + 6; // 读写,用于选择通道中的驱动器。
- 命令寄存器:BAR0 + 7; // 只写。
- 状态寄存器:BAR0 + 7; // 只读。
- 备用状态寄存器:BAR1 + 2; // 只读。
- 控制寄存器:BAR1 + 2; // 只写。
- DEVADDRESS:BAR1 + 3; // 我不知道这个寄存器有什么用处。
上面的映射与次通道相同,但它使用 BAR2 和 BAR3 而不是 BAR0 和 BAR1。
// Channels: #define ATA_PRIMARY 0x00 #define ATA_SECONDARY 0x01 // Directions: #define ATA_READ 0x00 #define ATA_WRITE 0x01
我们已经定义了驱动程序所需的一切,现在让我们进入一个重要部分。我们说过:
- BAR0 是主通道使用的 I/O 端口的起始地址。
- BAR1 是控制主通道的 I/O 端口的起始地址。
- BAR2 是次通道使用的 I/O 端口的起始地址。
- BAR3 是控制次通道的 I/O 端口的起始地址。
- BAR4 是 8 个 I/O 端口的起始地址,这些端口控制主通道的总线主控 IDE。
- BAR4 + 8 是 8 个 I/O 端口的基地址,这些端口控制次通道的总线主控 IDE。
因此我们可以创建这个全局结构:
struct IDEChannelRegisters { unsigned short base; // I/O Base. unsigned short ctrl; // Control Base unsigned short bmide; // Bus Master IDE unsigned char nIEN; // nIEN (No Interrupt); } channels[2];
我们还需要一个缓冲区来读取标识空间,需要一个变量来指示 irq 是否被触发,最后我们需要一个 6 个字 [12 字节] 的数组用于 ATAPI 驱动器:
unsigned char ide_buf[2048] = {0}; volatile unsigned static char ide_irq_invoked = 0; unsigned static char atapi_packet[12] = {0xA8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
我们说过 IDE 最多可以包含 4 个驱动器:
struct ide_device { unsigned char Reserved; // 0 (Empty) or 1 (This Drive really exists). unsigned char Channel; // 0 (Primary Channel) or 1 (Secondary Channel). unsigned char Drive; // 0 (Master Drive) or 1 (Slave Drive). unsigned short Type; // 0: ATA, 1:ATAPI. unsigned short Signature; // Drive Signature unsigned short Capabilities;// Features. unsigned int CommandSets; // Command Sets Supported. unsigned int Size; // Size in Sectors. unsigned char Model[41]; // Model in string. } ide_devices[4];
当我们读取通道中的寄存器(如状态寄存器)时,执行起来很简单:
ide_read(channel, ATA_REG_STATUS); unsigned char ide_read(unsigned char channel, unsigned char reg) { unsigned char result; if (reg > 0x07 && reg < 0x0C) ide_write(channel, ATA_REG_CONTROL, 0x80 | channels[channel].nIEN); if (reg < 0x08) result = inb(channels[channel].base + reg - 0x00); else if (reg < 0x0C) result = inb(channels[channel].base + reg - 0x06); else if (reg < 0x0E) result = inb(channels[channel].ctrl + reg - 0x0A); else if (reg < 0x16) result = inb(channels[channel].bmide + reg - 0x0E); if (reg > 0x07 && reg < 0x0C) ide_write(channel, ATA_REG_CONTROL, channels[channel].nIEN); return result; }
我们还需要一个用于写入寄存器的函数:
void ide_write(unsigned char channel, unsigned char reg, unsigned char data) { if (reg > 0x07 && reg < 0x0C) ide_write(channel, ATA_REG_CONTROL, 0x80 | channels[channel].nIEN); if (reg < 0x08) outb(channels[channel].base + reg - 0x00, data); else if (reg < 0x0C) outb(channels[channel].base + reg - 0x06, data); else if (reg < 0x0E) outb(channels[channel].ctrl + reg - 0x0A, data); else if (reg < 0x16) outb(channels[channel].bmide + reg - 0x0E, data); if (reg > 0x07 && reg < 0x0C) ide_write(channel, ATA_REG_CONTROL, channels[channel].nIEN); }
要读取标识空间,我们应该将数据寄存器作为双字读取 128 次。然后我们可以将它们复制到缓冲区。
void ide_read_buffer(unsigned char channel, unsigned char reg, unsigned int buffer, unsigned int quads) { /* WARNING: This code contains a serious bug. The inline assembly trashes ES and * ESP for all of the code the compiler generates between the inline * assembly blocks. */ if (reg > 0x07 && reg < 0x0C) ide_write(channel, ATA_REG_CONTROL, 0x80 | channels[channel].nIEN); asm("pushw %es; movw %ds, %ax; movw %ax, %es"); if (reg < 0x08) insl(channels[channel].base + reg - 0x00, buffer, quads); else if (reg < 0x0C) insl(channels[channel].base + reg - 0x06, buffer, quads); else if (reg < 0x0E) insl(channels[channel].ctrl + reg - 0x0A, buffer, quads); else if (reg < 0x16) insl(channels[channel].bmide + reg - 0x0E, buffer, quads); asm("popw %es;"); if (reg > 0x07 && reg < 0x0C) ide_write(channel, ATA_REG_CONTROL, channels[channel].nIEN); }
当我们发送命令时,应该等待 400 纳秒,然后读取状态端口。如果 Busy 位为 1,我们应该再次读取状态端口,直到 Busy 位为 0;然后我们可以读取命令的结果。此操作称为“轮询”。我们也可以使用 IRQ 代替轮询。
在许多命令执行后,如果 Device Fault 位被置位,则存在故障;如果 DRQ 未被置位,则存在错误。如果 ERR 位被置位,则存在错误,该错误在 Error 端口中描述。
unsigned char ide_polling(unsigned char channel, unsigned int advanced_check) { // (I) Delay 400 nanosecond for BSY to be set: // ------------------------------------------------- for(int i = 0; i < 4; i++) ide_read(channel, ATA_REG_ALTSTATUS); // Reading the Alternate Status port wastes 100ns; loop four times. // (II) Wait for BSY to be cleared: // ------------------------------------------------- while (ide_read(channel, ATA_REG_STATUS) & ATA_SR_BSY) ; // Wait for BSY to be zero. if (advanced_check) { unsigned char state = ide_read(channel, ATA_REG_STATUS); // Read Status Register. // (III) Check For Errors: // ------------------------------------------------- if (state & ATA_SR_ERR) return 2; // Error. // (IV) Check If Device fault: // ------------------------------------------------- if (state & ATA_SR_DF) return 1; // Device Fault. // (V) Check DRQ: // ------------------------------------------------- // BSY = 0; DF = 0; ERR = 0 so we should check for DRQ now. if ((state & ATA_SR_DRQ) == 0) return 3; // DRQ should be set } return 0; // No Error. }
如果发生错误,我们有一个在屏幕上打印错误的函数:
unsigned char ide_print_error(unsigned int drive, unsigned char err) { if (err == 0) return err; printk("IDE:"); if (err == 1) {printk("- Device Fault\n "); err = 19;} else if (err == 2) { unsigned char st = ide_read(ide_devices[drive].channel, ATA_REG_ERROR); if (st & ATA_ER_AMNF) {printk("- No Address Mark Found\n "); err = 7;} if (st & ATA_ER_TK0NF) {printk("- No Media or Media Error\n "); err = 3;} if (st & ATA_ER_ABRT) {printk("- Command Aborted\n "); err = 20;} if (st & ATA_ER_MCR) {printk("- No Media or Media Error\n "); err = 3;} if (st & ATA_ER_IDNF) {printk("- ID mark not Found\n "); err = 21;} if (st & ATA_ER_MC) {printk("- No Media or Media Error\n "); err = 3;} if (st & ATA_ER_UNC) {printk("- Uncorrectable Data Error\n "); err = 22;} if (st & ATA_ER_BBK) {printk("- Bad Sectors\n "); err = 13;} } else if (err == 3) {printk("- Reads Nothing\n "); err = 23;} else if (err == 4) {printk("- Write Protected\n "); err = 8;} printk("- [%s %s] %s\n", (const char *[]){"Primary", "Secondary"}[ide_devices[drive].channel], // Use the channel as an index into the array (const char *[]){"Master", "Slave"}[ide_devices[drive].drive], // Same as above, using the drive ide_devices[drive].model); return err; }
现在让我们回到初始化函数:
void ide_initialize(unsigned int BAR0, unsigned int BAR1, unsigned int BAR2, unsigned int BAR3, unsigned int BAR4) { int j, k, count = 0; // 1- Detect I/O Ports which interface IDE Controller: channels[ATA_PRIMARY ].base = (BAR0 & 0xFFFFFFFC) + 0x1F0 * (!BAR0); channels[ATA_PRIMARY ].ctrl = (BAR1 & 0xFFFFFFFC) + 0x3F6 * (!BAR1); channels[ATA_SECONDARY].base = (BAR2 & 0xFFFFFFFC) + 0x170 * (!BAR2); channels[ATA_SECONDARY].ctrl = (BAR3 & 0xFFFFFFFC) + 0x376 * (!BAR3); channels[ATA_PRIMARY ].bmide = (BAR4 & 0xFFFFFFFC) + 0; // Bus Master IDE channels[ATA_SECONDARY].bmide = (BAR4 & 0xFFFFFFFC) + 8; // Bus Master IDE
然后我们应该通过在 Control 端口中设置位 1 (nIEN) 来禁用两个通道的 IRQ:
// 2- Disable IRQs: ide_write(ATA_PRIMARY , ATA_REG_CONTROL, 2); ide_write(ATA_SECONDARY, ATA_REG_CONTROL, 2);
现在我们需要检查每个通道上可能连接的驱动器。我们将选择每个通道的主驱动器,并发送 ATA_IDENTIFY 命令(ATA 驱动器支持该命令)。如果没有错误,寄存器中会返回确定驱动器类型的值;如果没有驱动器存在,则会出现奇怪的值。
注意,如果 HDDEVSEL 中的位 4 被置为 1,我们选择的是从驱动器;如果置为 0,我们选择的是主驱动器。
// 3- Detect ATA-ATAPI Devices: for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) { unsigned char err = 0, type = IDE_ATA, status; ide_devices[count].Reserved = 0; // Assuming that no drive here. // (I) Select Drive: ide_write(i, ATA_REG_HDDEVSEL, 0xA0 | (j << 4)); // Select Drive. sleep(1); // Wait 1ms for drive select to work. // (II) Send ATA Identify Command: ide_write(i, ATA_REG_COMMAND, ATA_CMD_IDENTIFY); sleep(1); // This function should be implemented in your OS. which waits for 1 ms. // it is based on System Timer Device Driver. // (III) Polling: if (ide_read(i, ATA_REG_STATUS) == 0) continue; // If Status = 0, No Device. while(1) { status = ide_read(i, ATA_REG_STATUS); if ((status & ATA_SR_ERR)) {err = 1; break;} // If Err, Device is not ATA. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRQ)) break; // Everything is right. } // (IV) Probe for ATAPI Devices: if (err != 0) { unsigned char cl = ide_read(i, ATA_REG_LBA1); unsigned char ch = ide_read(i, ATA_REG_LBA2); if (cl == 0x14 && ch == 0xEB) type = IDE_ATAPI; else if (cl == 0x69 && ch == 0x96) type = IDE_ATAPI; else continue; // Unknown Type (may not be a device). ide_write(i, ATA_REG_COMMAND, ATA_CMD_IDENTIFY_PACKET); sleep(1); } // (V) Read Identification Space of the Device: ide_read_buffer(i, ATA_REG_DATA, (unsigned int) ide_buf, 128); // (VI) Read Device Parameters: ide_devices[count].Reserved = 1; ide_devices[count].Type = type; ide_devices[count].Channel = i; ide_devices[count].Drive = j; ide_devices[count].Signature = *((unsigned short *)(ide_buf + ATA_IDENT_DEVICETYPE)); ide_devices[count].Capabilities = *((unsigned short *)(ide_buf + ATA_IDENT_CAPABILITIES)); ide_devices[count].CommandSets = *((unsigned int *)(ide_buf + ATA_IDENT_COMMANDSETS)); // (VII) Get Size: if (ide_devices[count].CommandSets & (1 << 26)) // Device uses 48-Bit Addressing: ide_devices[count].Size = *((unsigned int *)(ide_buf + ATA_IDENT_MAX_LBA_EXT)); else // Device uses CHS or 28-bit Addressing: ide_devices[count].Size = *((unsigned int *)(ide_buf + ATA_IDENT_MAX_LBA)); // (VIII) String indicates model of device (like Western Digital HDD and SONY DVD-RW...): for(k = 0; k < 40; k += 2) { ide_devices[count].Model[k] = ide_buf[ATA_IDENT_MODEL + k + 1]; ide_devices[count].Model[k + 1] = ide_buf[ATA_IDENT_MODEL + k];} ide_devices[count].Model[40] = 0; // Terminate String. count++; } // 4- Print Summary: for (i = 0; i < 4; i++) if (ide_devices[i].Reserved == 1) { printk(" Found %s Drive %dGB - %s\n", (const char *[]){"ATA", "ATAPI"}[ide_devices[i].Type], /* Type */ ide_devices[i].Size / 1024 / 1024 / 2, /* Size */ ide_devices[i].Model); } }
从 ATA 驱动器读取/写入
现在我们进入一个稍微高级的部分,即从/向 ATA 驱动器读取和写入。 寻址一个扇区有 3 种方式:
- CHS(Cylinder-Head-Sector,柱面-磁头-扇区):ATA 驱动器中一种旧的扇区寻址方式,所有 ATA 驱动器都应支持这种寻址方式。
- LBA28:通过其 28 位 LBA 地址访问扇区。所有 ATA 驱动器都应支持这种寻址方式,LBA28 寻址的问题是它只允许访问 128GB,因此如果磁盘大于 128GB,它应支持 LBA48 特性集。
- LBA48:通过其 48 位 LBA 地址访问扇区。由于我们在 GCC 中使用整数,本教程中我们的最大地址长度为 32 位,这允许访问最大 2TB 的驱动器。
因此我们可以总结出一个确定使用哪种寻址类型的算法:
if (No LBA support) Use CHS. else if (the LBA Sector Address > 0x0FFFFFFF) Use LBA48. else Use LBA28.
读取缓冲区可以通过轮询或 DMA 完成。 PIO:在发送读取或写入扇区的命令后,我们读取或写入数据端口(以字为单位)。这与读取标识空间的方式相同。 DMA:在发送命令后,你应该等待 IRQ,在等待期间,缓冲区会自动直接写入内存。
我们将使用 PIO,因为它不那么复杂。
我们还可以总结出这个表:
/* ATA/ATAPI Read/Write Modes: * ++++++++++++++++++++++++++++++++ * Addressing Modes: * ================ * - LBA28 Mode. (+) * - LBA48 Mode. (+) * - CHS. (+) * Reading Modes: * ================ * - PIO Modes (0 : 6) (+) // Slower than DMA, but not a problem. * - Single Word DMA Modes (0, 1, 2). * - Double Word DMA Modes (0, 1, 2). * - Ultra DMA Modes (0 : 6). * Polling Modes: * ================ * - IRQs * - Polling Status (+) // Suitable for Singletasking */
这里有一点需要说明,我之前说过 Task-File 是这样的:
- 寄存器 0:[字] 数据寄存器。(读写)。
- 寄存器 1:[字节] 错误寄存器。(读)。
- 寄存器 1:[字节] 特性寄存器。(写)。
- 寄存器 2:[字节] SECCOUNT0 寄存器。(读写)。
- 寄存器 3:[字节] LBA0 寄存器。(读写)。
- 寄存器 4:[字节] LBA1 寄存器。(读写)。
- 寄存器 5:[字节] LBA2 寄存器。(读写)。
- 寄存器 6:[字节] HDDEVSEL 寄存器。(读写)。
- 寄存器 7:[字节] 命令寄存器。(写)。
- 寄存器 7:[字节] 状态寄存器。(读)。
因此 2 到 5 之间的每个寄存器应该是 8 位长。实际上它们每个都是 16 位长。
- 寄存器 2:[位 0-7] SECCOUNT0,[位 8-15] SECOUNT1
- 寄存器 3:[位 0-7] LBA0,[位 8-15] LBA3
- 寄存器 4:[位 0-7] LBA1,[位 8-15] LBA4
- 寄存器 5:[位 0-7] LBA2,[位 8-15] LBA5
字 [(SECCOUNT1 << 8) | SECCOUNT0] 表示使用 LBA48 访问时可以读取的扇区数。 当使用 CHS 或 LBA28 访问时,SECCOUNT0 仅表示扇区数。
- LBA0 在使用 LBA28 或 LBA48 读取时构成 LBA 地址的位 0 : 7;它也可以是 CHS 的扇区号。
- LBA1 在使用 LBA28 或 LBA48 读取时构成 LBA 地址的位 8 : 15;它也可以是 CHS 柱面号的低字节。
- LBA2 在使用 LBA28 或 LBA48 读取时构成 LBA 地址的位 16 : 23;它也可以是 CHS 柱面号的高字节。
- LBA3 构成 LBA48 地址的位 24 : 31。
- LBA4 构成 LBA48 地址的位 32 : 39。
- LBA5 构成 LBA48 地址的位 40 : 47。
注意,LBA0、1 和 2 寄存器总共只有 24 位长,这对于 LBA28 来说不够;较高的 4 位可以写入 HDDEVSEL 寄存器的较低 4 位。
还要注意,如果此寄存器的位 6 被置位,我们将使用 LBA;如果没有被置位,我们将使用 CHS。有一种模式称为扩展 CHS。
让我们进入代码:
unsigned char ide_ata_access(unsigned char direction, unsigned char drive, unsigned int lba, unsigned char numsects, unsigned short selector, unsigned int edi) {
此函数从 ATA 驱动器读取/写入扇区。如果 direction 为 0,我们执行读取,否则执行写入。
- drive 是驱动器号,可以是 0 到 3。
- lba 是 LBA 地址,它允许我们访问最大 2TB 的磁盘。
- numsects 是要读取的扇区数,它是一个 char,因为一次立即读取超过 256 个扇区可能会导致性能问题。如果 numsects 为 0,ATA 控制器将知道我们想要 256 个扇区。
- selector 是要读取或写入的段选择子。
- edi 是该段中的偏移量。(数据缓冲区的内存地址)
unsigned char lba_mode /* 0: CHS, 1:LBA28, 2: LBA48 */, dma /* 0: No DMA, 1: DMA */, cmd; unsigned char lba_io[6]; unsigned int channel = ide_devices[drive].Channel; // Read the Channel. unsigned int slavebit = ide_devices[drive].Drive; // Read the Drive [Master/Slave] unsigned int bus = channels[channel].Base; // Bus Base, like 0x1F0 which is also data port. unsigned int words = 256; // Almost every ATA drive has a sector-size of 512-byte. unsigned short cyl, i; unsigned char head, sect, err;
我们不需要 IRQ,因此我们应该禁用它以防止问题发生。我们之前说过,如果 Control 寄存器的位 1(称为 nIEN 位)被置位,则此通道上的任何驱动器(无论是主驱动器还是从驱动器)都不会触发 IRQ。
ide_write(channel, ATA_REG_CONTROL, channels[channel].nIEN = (ide_irq_invoked = 0x0) + 0x02);
现在让我们读取参数:
// (I) Select one from LBA28, LBA48 or CHS; if (lba >= 0x10000000) { // Sure Drive should support LBA in this case, or you are // giving a wrong LBA. // LBA48: lba_mode = 2; lba_io[0] = (lba & 0x000000FF) >> 0; lba_io[1] = (lba & 0x0000FF00) >> 8; lba_io[2] = (lba & 0x00FF0000) >> 16; lba_io[3] = (lba & 0xFF000000) >> 24; lba_io[4] = 0; // LBA28 is integer, so 32-bits are enough to access 2TB. lba_io[5] = 0; // LBA28 is integer, so 32-bits are enough to access 2TB. head = 0; // Lower 4-bits of HDDEVSEL are not used here. } else if (ide_devices[drive].Capabilities & 0x200) { // Drive supports LBA? // LBA28: lba_mode = 1; lba_io[0] = (lba & 0x00000FF) >> 0; lba_io[1] = (lba & 0x000FF00) >> 8; lba_io[2] = (lba & 0x0FF0000) >> 16; lba_io[3] = 0; // These Registers are not used here. lba_io[4] = 0; // These Registers are not used here. lba_io[5] = 0; // These Registers are not used here. head = (lba & 0xF000000) >> 24; } else { // CHS: lba_mode = 0; sect = (lba % 63) + 1; cyl = (lba + 1 - sect) / (16 * 63); lba_io[0] = sect; lba_io[1] = (cyl >> 0) & 0xFF; lba_io[2] = (cyl >> 8) & 0xFF; lba_io[3] = 0; lba_io[4] = 0; lba_io[5] = 0; head = (lba + 1 - sect) % (16 * 63) / (63); // Head number is written to HDDEVSEL lower 4-bits. }
现在我们将选择读取缓冲区的方式 [PIO 或 DMA]:
// (II) See if drive supports DMA or not; dma = 0; // We don't support DMA
让我们在通道忙时轮询状态端口:
// (III) Wait if the drive is busy; while (ide_read(channel, ATA_REG_STATUS) & ATA_SR_BSY){ } // Wait if busy.
HDDDEVSEL 寄存器现在看起来像这样:
- 位 0 : 3:CHS 的磁头号。
- 位 4:从驱动器位。(0:选择主驱动器,1:选择从驱动器)。
- 位 5:已废弃且不使用,但应置位。
- 位 6:LBA(0:CHS,1:LBA)。
- 位 7:已废弃且不使用,但应置位。
让我们将所有这些信息写入寄存器,同时将废弃位置位(0xA0):
// (IV) Select Drive from the controller; if (lba_mode == 0) ide_write(channel, ATA_REG_HDDEVSEL, 0xA0 | (slavebit << 4) | head); // Drive & CHS. else ide_write(channel, ATA_REG_HDDEVSEL, 0xE0 | (slavebit << 4) | head); // Drive & LBA
让我们将参数写入寄存器:
// (V) Write Parameters; if (lba_mode == 2) { ide_write(channel, ATA_REG_SECCOUNT1, 0); ide_write(channel, ATA_REG_LBA3, lba_io[3]); ide_write(channel, ATA_REG_LBA4, lba_io[4]); ide_write(channel, ATA_REG_LBA5, lba_io[5]); } ide_write(channel, ATA_REG_SECCOUNT0, numsects); ide_write(channel, ATA_REG_LBA0, lba_io[0]); ide_write(channel, ATA_REG_LBA1, lba_io[1]); ide_write(channel, ATA_REG_LBA2, lba_io[2]);
如果你使用 LBA48 并想写入 LBA0 和 LBA3 寄存器,你应该先将 LBA3 写入寄存器 3,然后将 LBA0 写入寄存器 3。ide_write 函数使这变得非常简单,参考该函数你将完全理解代码。
现在,我们在 ATA/ATAPI-8 规范中有一组很棒的命令,我们应该选择合适的命令来执行:
// (VI) Select the command and send it; // Routine that is followed: // If ( DMA & LBA48) DO_DMA_EXT; // If ( DMA & LBA28) DO_DMA_LBA; // If ( DMA & LBA28) DO_DMA_CHS; // If (!DMA & LBA48) DO_PIO_EXT; // If (!DMA & LBA28) DO_PIO_LBA; // If (!DMA & !LBA#) DO_PIO_CHS;
没有用于使用 DMA 执行 CHS 的命令。
if (lba_mode == 0 && dma == 0 && direction == 0) cmd = ATA_CMD_READ_PIO; if (lba_mode == 1 && dma == 0 && direction == 0) cmd = ATA_CMD_READ_PIO; if (lba_mode == 2 && dma == 0 && direction == 0) cmd = ATA_CMD_READ_PIO_EXT; if (lba_mode == 0 && dma == 1 && direction == 0) cmd = ATA_CMD_READ_DMA; if (lba_mode == 1 && dma == 1 && direction == 0) cmd = ATA_CMD_READ_DMA; if (lba_mode == 2 && dma == 1 && direction == 0) cmd = ATA_CMD_READ_DMA_EXT; if (lba_mode == 0 && dma == 0 && direction == 1) cmd = ATA_CMD_WRITE_PIO; if (lba_mode == 1 && dma == 0 && direction == 1) cmd = ATA_CMD_WRITE_PIO; if (lba_mode == 2 && dma == 0 && direction == 1) cmd = ATA_CMD_WRITE_PIO_EXT; if (lba_mode == 0 && dma == 1 && direction == 1) cmd = ATA_CMD_WRITE_DMA; if (lba_mode == 1 && dma == 1 && direction == 1) cmd = ATA_CMD_WRITE_DMA; if (lba_mode == 2 && dma == 1 && direction == 1) cmd = ATA_CMD_WRITE_DMA_EXT; ide_write(channel, ATA_REG_COMMAND, cmd); // Send the Command.
此 ATA_CMD_READ_PIO 命令用于在 LBA28 或 CHS 中读取,IDE 控制器参考 HDDEVSEL 寄存器的位 6 来确定读取模式(LBA 或 CHS)。
发送命令后,我们应该进行轮询,然后读取/写入一个扇区,接着再次轮询,然后读取/写入一个扇区,直到读取/写入所有需要的扇区;如果发生错误,该函数将返回特定的错误代码。
注意,写入后,我们应该执行 CACHE FLUSH 命令,并且之后应该轮询,但不检查错误。
if (dma) if (direction == 0); // DMA Read. else; // DMA Write. else if (direction == 0) // PIO Read. for (i = 0; i < numsects; i++) { if (err = ide_polling(channel, 1)) return err; // Polling, set error and exit if there is. asm("pushw %es"); asm("mov %%ax, %%es" : : "a"(selector)); asm("rep insw" : : "c"(words), "d"(bus), "D"(edi)); // Receive Data. asm("popw %es"); edi += (words*2); } else { // PIO Write. for (i = 0; i < numsects; i++) { ide_polling(channel, 0); // Polling. asm("pushw %ds"); asm("mov %%ax, %%ds"::"a"(selector)); asm("rep outsw"::"c"(words), "d"(bus), "S"(edi)); // Send Data asm("popw %ds"); edi += (words*2); } ide_write(channel, ATA_REG_COMMAND, (char []) { ATA_CMD_CACHE_FLUSH, ATA_CMD_CACHE_FLUSH, ATA_CMD_CACHE_FLUSH_EXT}[lba_mode]); ide_polling(channel, 0); // Polling. } return 0; // Easy, isn't it? }
从 ATAPI 驱动器读取
让我们进入一个更简单的部分——从 ATAPI 驱动器读取。我不会编写写入 ATAPI 驱动器的函数,因为向其写入非常复杂,且超出了本教程的范围。
ATAPI 驱动器与 ATA 驱动器不同,因为它使用 SCSI 命令集而不是 ATA 命令集。参数以数据包形式发送,因此称为 ATA 数据包接口 [ATAPI]。
还要注意,ATAPI 驱动器始终使用 IRQ,你无法禁用它们。我们应该创建一个等待 IRQ 的函数:
void ide_wait_irq() { while (!ide_irq_invoked) ; ide_irq_invoked = 0; }
当 IRQ 发生时,ISR 应执行以下函数:
void ide_irq() { ide_irq_invoked = 1; }
ide_wait_irq 将进入一个 while 循环,等待变量 ide_irq_invoked 被置位,然后清除它。
unsigned char ide_atapi_read(unsigned char drive, unsigned int lba, unsigned char numsects, unsigned short selector, unsigned int edi) {
- drive 是驱动器号,范围是 0 到 3。
- lba 是 LBA 地址。
- numsects 是扇区数。它应该始终为 1,如果你想读取多个扇区,请使用更新后的 LBA 地址重新执行此函数。
- selector 是段选择子。
- edi 是 selector 中的偏移量。
让我们读取驱动器的参数:
unsigned int channel = ide_devices[drive].Channel; unsigned int slavebit = ide_devices[drive].Drive; unsigned int bus = channels[channel].Base; unsigned int words = 1024; // Sector Size. ATAPI drives have a sector size of 2048 bytes. unsigned char err; int i;
我们需要 IRQ:
// Enable IRQs: ide_write(channel, ATA_REG_CONTROL, channels[channel].nIEN = ide_irq_invoked = 0x0);
让我们设置 SCSI 数据包,它长 6 个字(12 字节):
// (I): Setup SCSI Packet: // ------------------------------------------------------------------ atapi_packet[ 0] = ATAPI_CMD_READ; atapi_packet[ 1] = 0x0; atapi_packet[ 2] = (lba >> 24) & 0xFF; atapi_packet[ 3] = (lba >> 16) & 0xFF; atapi_packet[ 4] = (lba >> 8) & 0xFF; atapi_packet[ 5] = (lba >> 0) & 0xFF; atapi_packet[ 6] = 0x0; atapi_packet[ 7] = 0x0; atapi_packet[ 8] = 0x0; atapi_packet[ 9] = numsects; atapi_packet[10] = 0x0; atapi_packet[11] = 0x0;
现在我们应该选择驱动器:
// (II): Select the drive: // ------------------------------------------------------------------ ide_write(channel, ATA_REG_HDDEVSEL, slavebit << 4);
在此选择后延迟 400 纳秒是个好主意:
// (III): Delay 400 nanoseconds for select to complete: // ------------------------------------------------------------------ for(int i = 0; i < 4; i++) ide_read(channel, ATA_REG_ALTSTATUS); // Reading the Alternate Status port wastes 100ns.
// (IV): Inform the Controller that we use PIO mode: // ------------------------------------------------------------------ ide_write(channel, ATA_REG_FEATURES, 0); // PIO mode.
告诉控制器缓冲区的大小
// (V): Tell the Controller the size of buffer: // ------------------------------------------------------------------ ide_write(channel, ATA_REG_LBA1, (words * 2) & 0xFF); // Lower Byte of Sector Size. ide_write(channel, ATA_REG_LBA2, (words * 2) >> 8); // Upper Byte of Sector Size.
现在我们要发送数据包,我们应该先发送命令“Packet”:
// (VI): Send the Packet Command: // ------------------------------------------------------------------ ide_write(channel, ATA_REG_COMMAND, ATA_CMD_PACKET); // Send the Command. // (VII): Waiting for the driver to finish or return an error code: // ------------------------------------------------------------------ if (err = ide_polling(channel, 1)) return err; // Polling and return if error. // (VIII): Sending the packet data: // ------------------------------------------------------------------ asm("rep outsw" : : "c"(6), "d"(bus), "S"(atapi_packet)); // Send Packet Data
这里我们不能轮询。我们应该等待 IRQ,然后读取扇区。这两个操作应对每个扇区重复执行。
// (IX): Receiving Data: // ------------------------------------------------------------------ for (i = 0; i < numsects; i++) { ide_wait_irq(); // Wait for an IRQ. if (err = ide_polling(channel, 1)) return err; // Polling and return if error. asm("pushw %es"); asm("mov %%ax, %%es"::"a"(selector)); asm("rep insw"::"c"(words), "d"(bus), "D"(edi));// Receive Data. asm("popw %es"); edi += (words * 2); }
现在我们应该等待 IRQ 并轮询,直到 Busy 和 DRQ 位被清除:
// (X): Waiting for an IRQ: // ------------------------------------------------------------------ ide_wait_irq(); // (XI): Waiting for BSY & DRQ to clear: // ------------------------------------------------------------------ while (ide_read(channel, ATA_REG_STATUS) & (ATA_SR_BSY | ATA_SR_DRQ)) ; return 0; // Easy, ... Isn't it? }
从 ATA/ATAPI 驱动器读取
void ide_read_sectors(unsigned char drive, unsigned char numsects, unsigned int lba, unsigned short es, unsigned int edi) { // 1: Check if the drive presents: // ================================== if (drive > 3 || ide_devices[drive].Reserved == 0) package[0] = 0x1; // Drive Not Found! // 2: Check if inputs are valid: // ================================== else if (((lba + numsects) > ide_devices[drive].Size) && (ide_devices[drive].Type == IDE_ATA)) package[0] = 0x2; // Seeking to invalid position. // 3: Read in PIO Mode through Polling & IRQs: // ============================================ else { unsigned char err; if (ide_devices[drive].Type == IDE_ATA) err = ide_ata_access(ATA_READ, drive, lba, numsects, es, edi); else if (ide_devices[drive].Type == IDE_ATAPI) for (i = 0; i < numsects; i++) err = ide_atapi_read(drive, lba + i, 1, es, edi + (i*2048)); package[0] = ide_print_error(drive, err); } } // package[0] is an entry of an array. It contains the Error Code.
写入 ATA 驱动器
void ide_write_sectors(unsigned char drive, unsigned char numsects, unsigned int lba, unsigned short es, unsigned int edi) { // 1: Check if the drive presents: // ================================== if (drive > 3 || ide_devices[drive].Reserved == 0) package[0] = 0x1; // Drive Not Found! // 2: Check if inputs are valid: // ================================== else if (((lba + numsects) > ide_devices[drive].Size) && (ide_devices[drive].Type == IDE_ATA)) package[0] = 0x2; // Seeking to invalid position. // 3: Read in PIO Mode through Polling & IRQs: // ============================================ else { unsigned char err; if (ide_devices[drive].Type == IDE_ATA) err = ide_ata_access(ATA_WRITE, drive, lba, numsects, es, edi); else if (ide_devices[drive].Type == IDE_ATAPI) err = 4; // Write-Protected. package[0] = ide_print_error(drive, err); } }
弹出 ATAPI 驱动器
void ide_atapi_eject(unsigned char drive) { unsigned int channel = ide_devices[drive].Channel; unsigned int slavebit = ide_devices[drive].Drive; unsigned int bus = channels[channel].Base; unsigned int words = 2048 / 2; // Sector Size in Words. unsigned char err = 0; ide_irq_invoked = 0; // 1: Check if the drive presents: // ================================== if (drive > 3 || ide_devices[drive].Reserved == 0) package[0] = 0x1; // Drive Not Found! // 2: Check if drive isn't ATAPI: // ================================== else if (ide_devices[drive].Type == IDE_ATA) package[0] = 20; // Command Aborted. // 3: Eject ATAPI Driver: // ============================================ else { // Enable IRQs: ide_write(channel, ATA_REG_CONTROL, channels[channel].nIEN = ide_irq_invoked = 0x0); // (I): Setup SCSI Packet: // ------------------------------------------------------------------ atapi_packet[ 0] = ATAPI_CMD_EJECT; atapi_packet[ 1] = 0x00; atapi_packet[ 2] = 0x00; atapi_packet[ 3] = 0x00; atapi_packet[ 4] = 0x02; atapi_packet[ 5] = 0x00; atapi_packet[ 6] = 0x00; atapi_packet[ 7] = 0x00; atapi_packet[ 8] = 0x00; atapi_packet[ 9] = 0x00; atapi_packet[10] = 0x00; atapi_packet[11] = 0x00; // (II): Select the Drive: // ------------------------------------------------------------------ ide_write(channel, ATA_REG_HDDEVSEL, slavebit << 4); // (III): Delay 400 nanosecond for select to complete: // ------------------------------------------------------------------ for(int i = 0; i < 4; i++) ide_read(channel, ATA_REG_ALTSTATUS); // Reading Alternate Status Port wastes 100ns. // (IV): Send the Packet Command: // ------------------------------------------------------------------ ide_write(channel, ATA_REG_COMMAND, ATA_CMD_PACKET); // Send the Command. // (V): Waiting for the driver to finish or invoke an error: // ------------------------------------------------------------------ err = ide_polling(channel, 1); // Polling and stop if error. // (VI): Sending the packet data: // ------------------------------------------------------------------ else { asm("rep outsw"::"c"(6), "d"(bus), "S"(atapi_packet));// Send Packet Data ide_wait_irq(); // Wait for an IRQ. err = ide_polling(channel, 1); // Polling and get error code. if (err == 3) err = 0; // DRQ is not needed here. } package[0] = ide_print_error(drive, err); // Return; } }
调用此方法时,给定通道上的光驱将被弹出。
另请参阅
Wiki 页面
线程
外部链接
- T13 -- 制定 ATA 标准的组织
- ATA-ATAPI -- 公共领域的 C 驱动程序源代码(包括 SATA、总线主控 DMA、ATAPI),相当不错。
- HDD Guru -- 从 1994 年发布的第一份到 2003 年的第 7 份的实际 ATA 规范。
- 分区入门 -- 一个包含有关分区的一些信息的 .HTM 文件。
- PCI IDE 控制器规范 -- 包含有关“兼容模式”和“PCI 原生模式”(以及它们之间切换)信息的规范
- 总线主控 IDE 控制器编程接口 -- 总线主控 IDE 规范
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:PCI IDE Controller;离线修订号:28314。对应原文版本

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论