// Low level ATA disk access
//
-// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2002 MandrakeSoft S.A.
//
// This file may be distributed under the terms of the GNU LGPLv3 license.
#include "cmos.h" // inb_cmos
#include "pic.h" // enable_hwirq
#include "biosvar.h" // GET_EBDA
-#include "pci.h" // pci_find_class
+#include "pci.h" // foreachpci
#include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
#include "pci_regs.h" // PCI_INTERRUPT_LINE
#include "boot.h" // add_bcv_hd
#include "disk.h" // struct ata_s
-#include "atabits.h" // ATA_CB_STAT
-
-#define IDE_SECTOR_SIZE 512
-#define CDROM_SECTOR_SIZE 2048
+#include "ata.h" // ATA_CB_STAT
#define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
-struct ata_s ATA VAR16_32;
+struct ata_channel_s ATA_channels[CONFIG_MAX_ATA_INTERFACES] VAR16VISIBLE;
/****************************************************************
u8 status = inb(base+ATA_CB_STAT);
if ((status & mask) == flags)
return status;
- if (rdtscll() > end) {
+ if (check_time(end)) {
dprintf(1, "IDE time out\n");
return -1;
}
+ yield();
}
}
// Reset a drive
static void
-ata_reset(int driveid)
+ata_reset(struct drive_s *drive_g)
{
- u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
+ u8 ataid = GET_GLOBAL(drive_g->cntl_id);
u8 channel = ataid / 2;
u8 slave = ataid % 2;
- u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
- u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
+ u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
- dprintf(6, "ata_reset driveid=%d\n", driveid);
+ dprintf(6, "ata_reset drive=%p\n", drive_g);
// Pulse SRST
outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
udelay(5);
outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
- mdelay(2);
+ msleep(2);
// wait for device to become not busy.
int status = await_not_bsy(iobase1);
if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
break;
// Change drive request failed to take effect - retry.
- if (rdtscll() > end) {
+ if (check_time(end)) {
dprintf(1, "ata_reset slave time out\n");
goto done;
}
}
+ } else {
+ // QEMU doesn't reset dh on reset, so set it explicitly.
+ outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
}
// On a user-reset request, wait for RDY if it is an ATA device.
- u8 type=GET_GLOBAL(ATA.devices[driveid].type);
+ u8 type=GET_GLOBAL(drive_g->type);
if (type == DTYPE_ATA)
status = await_rdy(iobase1);
}
static int
-isready(int driveid)
+isready(struct drive_s *drive_g)
{
// Read the status from controller
- u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
+ u8 ataid = GET_GLOBAL(drive_g->cntl_id);
u8 channel = ataid / 2;
- u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
u8 status = inb(iobase1 + ATA_CB_STAT);
- return (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY;
+ if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY)
+ return DISK_RET_SUCCESS;
+ return DISK_RET_ENOTREADY;
}
static int
process_ata_misc_op(struct disk_op_s *op)
{
- switch (op->command) {
- default:
+ if (!CONFIG_ATA)
return 0;
+
+ switch (op->command) {
case CMD_RESET:
- ata_reset(op->driveid);
- return 0;
+ ata_reset(op->drive_g);
+ return DISK_RET_SUCCESS;
case CMD_ISREADY:
- return isready(op->driveid);
+ return isready(op->drive_g);
+ case CMD_FORMAT:
+ case CMD_VERIFY:
+ case CMD_SEEK:
+ return DISK_RET_SUCCESS;
+ default:
+ op->count = 0;
+ return DISK_RET_EPARAM;
}
}
// Send an ata command to the drive.
static int
-send_cmd(int driveid, struct ata_pio_command *cmd)
+send_cmd(struct drive_s *drive_g, struct ata_pio_command *cmd)
{
- u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
+ u8 ataid = GET_GLOBAL(drive_g->cntl_id);
u8 channel = ataid / 2;
u8 slave = ataid % 2;
- u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
- u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
-
- // Disable interrupts
- outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
// Select device
int status = await_not_bsy(iobase1);
****************************************************************/
// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
-// 'op->driveid'.
+// 'op->drive_g'.
static int
ata_transfer(struct disk_op_s *op, int iswrite, int blocksize)
{
- dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d buf=%p\n"
- , op->driveid, iswrite, op->count, blocksize, op->buf_fl);
+ dprintf(16, "ata_transfer id=%p write=%d count=%d bs=%d buf=%p\n"
+ , op->drive_g, iswrite, op->count, blocksize, op->buf_fl);
- u8 ataid = GET_GLOBAL(ATA.devices[op->driveid].cntl_id);
+ u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
u8 channel = ataid / 2;
- u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
- u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
+ u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
int count = op->count;
void *buf_fl = op->buf_fl;
int status;
for (;;) {
if (iswrite) {
// Write data to controller
- dprintf(16, "Write sector id=%d dest=%p\n", op->driveid, buf_fl);
+ dprintf(16, "Write sector id=%p dest=%p\n", op->drive_g, buf_fl);
if (CONFIG_ATA_PIO32)
outsl_fl(iobase1, buf_fl, blocksize / 4);
else
outsw_fl(iobase1, buf_fl, blocksize / 2);
} else {
// Read data from controller
- dprintf(16, "Read sector id=%d dest=%p\n", op->driveid, buf_fl);
+ dprintf(16, "Read sector id=%p dest=%p\n", op->drive_g, buf_fl);
if (CONFIG_ATA_PIO32)
insl_fl(iobase1, buf_fl, blocksize / 4);
else
return -7;
}
- // Enable interrupts
- outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
return 0;
}
static int
ata_cmd_data(struct disk_op_s *op, int iswrite, int command)
{
+ u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
+ u8 channel = ataid / 2;
+ u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
u64 lba = op->lba;
struct ata_pio_command cmd;
cmd.lba_high = lba >> 16;
cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
- int ret = send_cmd(op->driveid, &cmd);
+ // Disable interrupts
+ outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
+
+ int ret = send_cmd(op->drive_g, &cmd);
if (ret)
- return ret;
- return ata_transfer(op, iswrite, IDE_SECTOR_SIZE);
+ goto fail;
+ ret = ata_transfer(op, iswrite, DISK_SECTOR_SIZE);
+
+fail:
+ // Enable interrupts
+ outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
+ return ret;
}
int
process_ata_op(struct disk_op_s *op)
{
+ if (!CONFIG_ATA)
+ return 0;
+
+ int ret;
switch (op->command) {
case CMD_READ:
- return ata_cmd_data(op, 0, ATA_CMD_READ_SECTORS);
+ ret = ata_cmd_data(op, 0, ATA_CMD_READ_SECTORS);
+ break;
case CMD_WRITE:
- return ata_cmd_data(op, 1, ATA_CMD_WRITE_SECTORS);
+ ret = ata_cmd_data(op, 1, ATA_CMD_WRITE_SECTORS);
+ break;
default:
return process_ata_misc_op(op);
}
+ if (ret)
+ return DISK_RET_EBADTRACK;
+ return DISK_RET_SUCCESS;
}
// Low-level atapi command transmit function.
static int
-send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
+atapi_cmd_data(struct disk_op_s *op, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
{
- u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
+ u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
u8 channel = ataid / 2;
- u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
- u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
+ u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
struct ata_pio_command cmd;
cmd.sector_count = 0;
cmd.device = 0;
cmd.command = ATA_CMD_PACKET;
- int ret = send_cmd(driveid, &cmd);
+ // Disable interrupts
+ outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
+
+ int ret = send_cmd(op->drive_g, &cmd);
if (ret)
- return ret;
+ goto fail;
// Send command to device
outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
int status = pause_await_not_bsy(iobase1, iobase2);
- if (status < 0)
- return status;
+ if (status < 0) {
+ ret = status;
+ goto fail;
+ }
if (status & ATA_CB_STAT_ERR) {
u8 err = inb(iobase1 + ATA_CB_ERR);
if (err != 0x20)
dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
, status, err);
- return -2;
+ ret = -2;
+ goto fail;
}
if (!(status & ATA_CB_STAT_DRQ)) {
dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
- return -3;
+ ret = -3;
+ goto fail;
}
- return 0;
+ ret = ata_transfer(op, 0, blocksize);
+
+fail:
+ // Enable interrupts
+ outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
+ return ret;
}
// Read sectors from the cdrom.
atacmd[4]=(op->lba & 0x0000ff00) >> 8;
atacmd[5]=(op->lba & 0x000000ff);
- int ret = send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
- , CDROM_SECTOR_SIZE);
- if (ret)
- return ret;
-
- return ata_transfer(op, 0, CDROM_SECTOR_SIZE);
+ return atapi_cmd_data(op, atacmd, sizeof(atacmd), CDROM_SECTOR_SIZE);
}
int
process_atapi_op(struct disk_op_s *op)
{
+ int ret;
switch (op->command) {
case CMD_READ:
- return cdrom_read(op);
+ ret = cdrom_read(op);
+ break;
+ case CMD_FORMAT:
+ case CMD_WRITE:
+ return DISK_RET_EWRITEPROTECT;
default:
return process_ata_misc_op(op);
}
+ if (ret)
+ return DISK_RET_EBADTRACK;
+ return DISK_RET_SUCCESS;
}
// Send a simple atapi command to a drive.
int
-ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
+ata_cmd_packet(struct drive_s *drive_g, u8 *cmdbuf, u8 cmdlen
, u32 length, void *buf_fl)
{
- int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
- if (ret)
- return ret;
-
struct disk_op_s dop;
memset(&dop, 0, sizeof(dop));
- dop.driveid = driveid;
+ dop.drive_g = drive_g;
dop.count = 1;
dop.buf_fl = buf_fl;
- return ata_transfer(&dop, 0, length);
-}
-
-
-/****************************************************************
- * Disk geometry translation
- ****************************************************************/
-
-static u8
-get_translation(int driveid)
-{
- if (! CONFIG_COREBOOT) {
- // Emulators pass in the translation info via nvram.
- u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
- u8 channel = ataid / 2;
- u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
- translation >>= 2 * (ataid % 4);
- translation &= 0x03;
- return translation;
- }
-
- // On COREBOOT, use a heuristic to determine translation type.
- u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
- u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
- u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
-
- if (cylinders <= 1024 && heads <= 16 && spt <= 63)
- return TRANSLATION_NONE;
- if (cylinders * heads <= 131072)
- return TRANSLATION_LARGE;
- return TRANSLATION_LBA;
-}
-
-static void
-setup_translation(int driveid)
-{
- u8 translation = get_translation(driveid);
- SET_GLOBAL(ATA.devices[driveid].translation, translation);
-
- u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
- u8 channel = ataid / 2;
- u8 slave = ataid % 2;
- u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
- u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
- u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
- u64 sectors = GET_GLOBAL(ATA.devices[driveid].sectors);
-
- dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
- , channel, slave, cylinders, heads, spt);
- switch (translation) {
- case TRANSLATION_NONE:
- dprintf(1, "none");
- break;
- case TRANSLATION_LBA:
- dprintf(1, "lba");
- spt = 63;
- if (sectors > 63*255*1024) {
- heads = 255;
- cylinders = 1024;
- break;
- }
- u32 sect = (u32)sectors / 63;
- heads = sect / 1024;
- if (heads>128)
- heads = 255;
- else if (heads>64)
- heads = 128;
- else if (heads>32)
- heads = 64;
- else if (heads>16)
- heads = 32;
- else
- heads = 16;
- cylinders = sect / heads;
- break;
- case TRANSLATION_RECHS:
- dprintf(1, "r-echs");
- // Take care not to overflow
- if (heads==16) {
- if (cylinders>61439)
- cylinders=61439;
- heads=15;
- cylinders = (u16)((u32)(cylinders)*16/15);
- }
- // then go through the large bitshift process
- case TRANSLATION_LARGE:
- if (translation == TRANSLATION_LARGE)
- dprintf(1, "large");
- while (cylinders > 1024) {
- cylinders >>= 1;
- heads <<= 1;
-
- // If we max out the head count
- if (heads > 127)
- break;
- }
- break;
- }
- // clip to 1024 cylinders in lchs
- if (cylinders > 1024)
- cylinders = 1024;
- dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
-
- SET_GLOBAL(ATA.devices[driveid].lchs.heads, heads);
- SET_GLOBAL(ATA.devices[driveid].lchs.cylinders, cylinders);
- SET_GLOBAL(ATA.devices[driveid].lchs.spt, spt);
+ return atapi_cmd_data(&dop, cmdbuf, cmdlen, length);
}
// Extract common information from IDENTIFY commands.
static void
-extract_identify(int driveid, u16 *buffer)
+extract_identify(struct drive_s *drive_g, u16 *buffer)
{
dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
// Read model name
- char *model = ATA.devices[driveid].model;
- int maxsize = ARRAY_SIZE(ATA.devices[driveid].model);
+ char *model = drive_g->model;
+ int maxsize = ARRAY_SIZE(drive_g->model);
int i;
for (i=0; i<maxsize/2; i++) {
u16 v = buffer[27+i];
model[i] = 0x00;
// Common flags.
- SET_GLOBAL(ATA.devices[driveid].removable, (buffer[0] & 0x80) ? 1 : 0);
+ SET_GLOBAL(drive_g->removable, (buffer[0] & 0x80) ? 1 : 0);
+ SET_GLOBAL(drive_g->cntl_info, extract_version(buffer));
}
-static int
-init_drive_atapi(int driveid, u16 *buffer)
+void
+describe_atapi(struct drive_s *drive_g)
+{
+ u8 ataid = drive_g->cntl_id;
+ u8 channel = ataid / 2;
+ u8 slave = ataid % 2;
+ u8 version = drive_g->cntl_info;
+ int iscd = drive_g->floppy_type;
+ printf("ata%d-%d: %s ATAPI-%d %s", channel, slave
+ , drive_g->model, version
+ , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
+}
+
+static struct drive_s *
+init_drive_atapi(struct drive_s *dummy, u16 *buffer)
{
// Send an IDENTIFY_DEVICE_PACKET command to device
- memset(buffer, 0, IDE_SECTOR_SIZE);
+ memset(buffer, 0, DISK_SECTOR_SIZE);
struct disk_op_s dop;
memset(&dop, 0, sizeof(dop));
- dop.driveid = driveid;
+ dop.drive_g = dummy;
dop.count = 1;
dop.lba = 1;
dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE_PACKET);
if (ret)
- return ret;
+ return NULL;
// Success - setup as ATAPI.
- extract_identify(driveid, buffer);
- SET_GLOBAL(ATA.devices[driveid].type, DTYPE_ATAPI);
- SET_GLOBAL(ATA.devices[driveid].blksize, CDROM_SECTOR_SIZE);
- SET_GLOBAL(ATA.devices[driveid].sectors, (u64)-1);
+ struct drive_s *drive_g = allocDrive();
+ if (! drive_g)
+ return NULL;
+ SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
+ extract_identify(drive_g, buffer);
+ SET_GLOBAL(drive_g->type, DTYPE_ATAPI);
+ SET_GLOBAL(drive_g->blksize, CDROM_SECTOR_SIZE);
+ SET_GLOBAL(drive_g->sectors, (u64)-1);
u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
-
- // Report drive info to user.
- u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
- u8 channel = ataid / 2;
- u8 slave = ataid % 2;
- printf("ata%d-%d: %s ATAPI-%d %s\n", channel, slave
- , ATA.devices[driveid].model, extract_version(buffer)
- , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
+ SET_GLOBAL(drive_g->floppy_type, iscd);
// fill cdidmap
- if (iscd) {
- u8 cdcount = GET_GLOBAL(ATA.cdcount);
- SET_GLOBAL(ATA.idmap[1][cdcount], driveid);
- SET_GLOBAL(ATA.cdcount, cdcount+1);
- }
+ if (iscd)
+ map_cd_drive(drive_g);
- return 0;
+ return drive_g;
}
-static int
-init_drive_ata(int driveid, u16 *buffer)
+void
+describe_ata(struct drive_s *drive_g)
+{
+ u8 ataid = drive_g->cntl_id;
+ u8 channel = ataid / 2;
+ u8 slave = ataid % 2;
+ u64 sectors = drive_g->sectors;
+ u8 version = drive_g->cntl_info;
+ char *model = drive_g->model;
+ printf("ata%d-%d: %s ATA-%d Hard-Disk", channel, slave, model, version);
+ u64 sizeinmb = sectors >> 11;
+ if (sizeinmb < (1 << 16))
+ printf(" (%u MiBytes)", (u32)sizeinmb);
+ else
+ printf(" (%u GiBytes)", (u32)(sizeinmb >> 10));
+}
+
+static struct drive_s *
+init_drive_ata(struct drive_s *dummy, u16 *buffer)
{
// Send an IDENTIFY_DEVICE command to device
- memset(buffer, 0, IDE_SECTOR_SIZE);
+ memset(buffer, 0, DISK_SECTOR_SIZE);
struct disk_op_s dop;
memset(&dop, 0, sizeof(dop));
- dop.driveid = driveid;
+ dop.drive_g = dummy;
dop.count = 1;
dop.lba = 1;
dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE);
if (ret)
- return ret;
+ return NULL;
// Success - setup as ATA.
- extract_identify(driveid, buffer);
- SET_GLOBAL(ATA.devices[driveid].type, DTYPE_ATA);
- SET_GLOBAL(ATA.devices[driveid].blksize, IDE_SECTOR_SIZE);
-
- SET_GLOBAL(ATA.devices[driveid].pchs.cylinders, buffer[1]);
- SET_GLOBAL(ATA.devices[driveid].pchs.heads, buffer[3]);
- SET_GLOBAL(ATA.devices[driveid].pchs.spt, buffer[6]);
+ struct drive_s *drive_g = allocDrive();
+ if (! drive_g)
+ return NULL;
+ SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
+ extract_identify(drive_g, buffer);
+ SET_GLOBAL(drive_g->type, DTYPE_ATA);
+ SET_GLOBAL(drive_g->blksize, DISK_SECTOR_SIZE);
+
+ SET_GLOBAL(drive_g->pchs.cylinders, buffer[1]);
+ SET_GLOBAL(drive_g->pchs.heads, buffer[3]);
+ SET_GLOBAL(drive_g->pchs.spt, buffer[6]);
u64 sectors;
if (buffer[83] & (1 << 10)) // word 83 - lba48 support
sectors = *(u64*)&buffer[100]; // word 100-103
else
sectors = *(u32*)&buffer[60]; // word 60 and word 61
- SET_GLOBAL(ATA.devices[driveid].sectors, sectors);
+ SET_GLOBAL(drive_g->sectors, sectors);
// Setup disk geometry translation.
- setup_translation(driveid);
-
- // Report drive info to user.
- u8 ataid = GET_GLOBAL(ATA.devices[driveid].cntl_id);
- u8 channel = ataid / 2;
- u8 slave = ataid % 2;
- char *model = ATA.devices[driveid].model;
- printf("ata%d-%d: %s ATA-%d Hard-Disk ", channel, slave, model
- , extract_version(buffer));
- u64 sizeinmb = sectors >> 11;
- if (sizeinmb < (1 << 16))
- printf("(%u MiBytes)\n", (u32)sizeinmb);
- else
- printf("(%u GiBytes)\n", (u32)(sizeinmb >> 10));
+ setup_translation(drive_g);
// Register with bcv system.
- add_bcv_hd(driveid, model);
+ add_bcv_internal(drive_g);
- return 0;
+ return drive_g;
}
+static u64 SpinupEnd;
+
static int
-powerup_await_non_bsy(u16 base, u64 end)
+powerup_await_non_bsy(u16 base)
{
u8 orstatus = 0;
u8 status;
dprintf(1, "powerup IDE floating\n");
return orstatus;
}
- if (rdtscll() > end) {
+ if (check_time(SpinupEnd)) {
dprintf(1, "powerup IDE time out\n");
return -1;
}
+ yield();
}
dprintf(6, "powerup iobase=%x st=%x\n", base, status);
return status;
}
static void
-ata_detect()
+ata_detect(void *data)
{
+ struct ata_channel_s *atachannel = data;
+ int startid = (atachannel - ATA_channels) * 2;
+ struct drive_s dummy;
+ memset(&dummy, 0, sizeof(dummy));
// Device detection
- u64 end = calc_future_tsc(IDE_TIMEOUT);
- int ataid, last_reset_ataid=-1, driveid=0;
- for (ataid=0; ataid<CONFIG_MAX_ATA_INTERFACES*2; ataid++) {
+ int ataid, last_reset_ataid=-1;
+ for (ataid=startid; ataid<startid+2; ataid++) {
u8 channel = ataid / 2;
u8 slave = ataid % 2;
- u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
if (!iobase1)
break;
// Wait for not-bsy.
- int status = powerup_await_non_bsy(iobase1, end);
+ int status = powerup_await_non_bsy(iobase1);
if (status < 0)
continue;
u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
outb(newdh, iobase1+ATA_CB_DH);
ndelay(400);
- status = powerup_await_non_bsy(iobase1, end);
+ status = powerup_await_non_bsy(iobase1);
if (status < 0)
continue;
if (sc != 0x55 || sn != 0xaa || dh != newdh)
continue;
- // Prepare new driveid.
- if (driveid >= ARRAY_SIZE(ATA.devices))
- break;
- memset(&ATA.devices[driveid], 0, sizeof(ATA.devices[0]));
- ATA.devices[driveid].cntl_id = ataid;
+ // Prepare new drive.
+ dummy.cntl_id = ataid;
// reset the channel
if (slave && ataid == last_reset_ataid + 1) {
// The drive was just reset - no need to reset it again.
} else {
- ata_reset(driveid);
+ ata_reset(&dummy);
last_reset_ataid = ataid;
}
// check for ATAPI
u16 buffer[256];
- int ret = init_drive_atapi(driveid, buffer);
- if (!ret) {
- // Found an ATAPI drive.
- } else {
+ struct drive_s *drive_g = init_drive_atapi(&dummy, buffer);
+ if (!drive_g) {
+ // Didn't find an ATAPI drive - look for ATA drive.
u8 st = inb(iobase1+ATA_CB_STAT);
if (!st)
// Status not set - can't be a valid drive.
continue;
// Wait for RDY.
- ret = await_rdy(iobase1);
+ int ret = await_rdy(iobase1);
if (ret < 0)
continue;
// check for ATA.
- ret = init_drive_ata(driveid, buffer);
- if (ret)
+ drive_g = init_drive_ata(&dummy, buffer);
+ if (!drive_g)
// No ATA drive found
continue;
}
- driveid++;
u16 resetresult = buffer[93];
dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
// detection.
ataid++;
}
-
- printf("\n");
}
static void
ata_init()
{
- memset(&ATA, 0, sizeof(ATA));
-
- // hdidmap and cdidmap init.
- u8 device;
- for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
- SET_GLOBAL(ATA.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
- SET_GLOBAL(ATA.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
- }
-
// Scan PCI bus for ATA adapters
int count=0;
int bdf, max;
foreachpci(bdf, max) {
if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
continue;
- if (count >= ARRAY_SIZE(ATA.channels))
+ if (count >= ARRAY_SIZE(ATA_channels))
break;
u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
- SET_GLOBAL(ATA.channels[count].irq, irq);
- SET_GLOBAL(ATA.channels[count].pci_bdf, bdf);
+ SET_GLOBAL(ATA_channels[count].irq, irq);
+ SET_GLOBAL(ATA_channels[count].pci_bdf, bdf);
u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
u32 port1, port2;
port1 = 0x1f0;
port2 = 0x3f0;
}
- SET_GLOBAL(ATA.channels[count].iobase1, port1);
- SET_GLOBAL(ATA.channels[count].iobase2, port2);
+ SET_GLOBAL(ATA_channels[count].iobase1, port1);
+ SET_GLOBAL(ATA_channels[count].iobase2, port2);
dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
, count, port1, port2, bdf, prog_if);
+ run_thread(ata_detect, &ATA_channels[count]);
count++;
if (prog_if & 4) {
}
dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
, count, port1, port2, bdf, prog_if);
- SET_GLOBAL(ATA.channels[count].iobase1, port1);
- SET_GLOBAL(ATA.channels[count].iobase2, port2);
+ SET_GLOBAL(ATA_channels[count].iobase1, port1);
+ SET_GLOBAL(ATA_channels[count].iobase2, port2);
+ run_thread(ata_detect, &ATA_channels[count]);
count++;
}
}
void
-hard_drive_setup()
+ata_setup()
{
if (!CONFIG_ATA)
return;
dprintf(3, "init hard drives\n");
+
+ SpinupEnd = calc_future_tsc(IDE_TIMEOUT);
ata_init();
- ata_detect();
SET_BDA(disk_control_byte, 0xc0);
enable_hwirq(14, entry_76);
}
-
-
-/****************************************************************
- * Drive mapping
- ****************************************************************/
-
-// Fill in Fixed Disk Parameter Table (located in ebda).
-static void
-fill_fdpt(int driveid)
-{
- if (driveid > 1)
- return;
-
- u16 nlc = GET_GLOBAL(ATA.devices[driveid].lchs.cylinders);
- u16 nlh = GET_GLOBAL(ATA.devices[driveid].lchs.heads);
- u16 nlspt = GET_GLOBAL(ATA.devices[driveid].lchs.spt);
-
- u16 npc = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
- u16 nph = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
- u16 npspt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
-
- struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[driveid];
- fdpt->precompensation = 0xffff;
- fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
- fdpt->landing_zone = npc;
- fdpt->cylinders = nlc;
- fdpt->heads = nlh;
- fdpt->sectors = nlspt;
-
- if (nlc == npc && nlh == nph && nlspt == npspt)
- // no logical CHS mapping used, just physical CHS
- // use Standard Fixed Disk Parameter Table (FDPT)
- return;
-
- // complies with Phoenix style Translated Fixed Disk Parameter
- // Table (FDPT)
- fdpt->phys_cylinders = npc;
- fdpt->phys_heads = nph;
- fdpt->phys_sectors = npspt;
- fdpt->a0h_signature = 0xa0;
-
- // Checksum structure.
- fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
-
- if (driveid == 0)
- SET_IVT(0x41, get_ebda_seg()
- , offsetof(struct extended_bios_data_area_s, fdpt[0]));
- else
- SET_IVT(0x46, get_ebda_seg()
- , offsetof(struct extended_bios_data_area_s, fdpt[1]));
-}
-
-// Map a drive (that was registered via add_bcv_hd)
-void
-map_drive(int driveid)
-{
- // fill hdidmap
- u8 hdcount = GET_BDA(hdcount);
- dprintf(3, "Mapping driveid %d to %d\n", driveid, hdcount);
- SET_GLOBAL(ATA.idmap[0][hdcount], driveid);
- SET_BDA(hdcount, hdcount + 1);
-
- // Fill "fdpt" structure.
- fill_fdpt(hdcount);
-}