X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=src%2Fata.c;h=76e4f20d2f9cd6b35a078d9e2200a8ef21969c7d;hb=e1a17bbb0ee4fb11526e46f5aecf8b7ebaafdca4;hp=f935e1f6fc140fbaf540c508f13bd7e73a1cf727;hpb=d7e998fdd0ea1cacbc83e9add182c49917bf35e6;p=seabios.git diff --git a/src/ata.c b/src/ata.c index f935e1f..76e4f20 100644 --- a/src/ata.c +++ b/src/ata.c @@ -14,15 +14,13 @@ #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 "boot.h" // boot_add_hd #include "disk.h" // struct ata_s #include "ata.h" // ATA_CB_STAT #include "blockcmd.h" // CDB_CMD_READ_10 #define IDE_TIMEOUT 32000 //32 seconds max for IDE ops -struct ata_channel_s ATA_channels[CONFIG_MAX_ATA_INTERFACES] VAR16VISIBLE; - /**************************************************************** * Helper functions @@ -37,7 +35,7 @@ await_ide(u8 mask, u8 flags, u16 base, u16 timeout) u8 status = inb(base+ATA_CB_STAT); if ((status & mask) == flags) return status; - if (check_time(end)) { + if (check_tsc(end)) { warn_timeout(); return -1; } @@ -79,15 +77,14 @@ ndelay_await_not_bsy(u16 iobase1) // Reset a drive static void -ata_reset(struct drive_s *drive_g) +ata_reset(struct atadrive_s *adrive_g) { - 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); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u8 slave = GET_GLOBAL(adrive_g->slave); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); + u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2); - dprintf(6, "ata_reset drive=%p\n", drive_g); + dprintf(6, "ata_reset drive=%p\n", &adrive_g->drive); // Pulse SRST outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC); udelay(5); @@ -109,7 +106,7 @@ ata_reset(struct drive_s *drive_g) if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1) break; // Change drive request failed to take effect - retry. - if (check_time(end)) { + if (check_tsc(end)) { warn_timeout(); goto done; } @@ -120,7 +117,7 @@ ata_reset(struct drive_s *drive_g) } // On a user-reset request, wait for RDY if it is an ATA device. - u8 type=GET_GLOBAL(drive_g->type); + u8 type=GET_GLOBAL(adrive_g->drive.type); if (type == DTYPE_ATA) status = await_rdy(iobase1); @@ -133,12 +130,11 @@ done: // Check for drive RDY for 16bit interface command. static int -isready(struct drive_s *drive_g) +isready(struct atadrive_s *adrive_g) { // Read the status from controller - u8 ataid = GET_GLOBAL(drive_g->cntl_id); - u8 channel = ataid / 2; - u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); u8 status = inb(iobase1 + ATA_CB_STAT); if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY) return DISK_RET_SUCCESS; @@ -152,12 +148,14 @@ process_ata_misc_op(struct disk_op_s *op) if (!CONFIG_ATA) return 0; + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); switch (op->command) { case CMD_RESET: - ata_reset(op->drive_g); + ata_reset(adrive_g); return DISK_RET_SUCCESS; case CMD_ISREADY: - return isready(op->drive_g); + return isready(adrive_g); case CMD_FORMAT: case CMD_VERIFY: case CMD_SEEK: @@ -191,12 +189,11 @@ struct ata_pio_command { // Send an ata command to the drive. static int -send_cmd(struct drive_s *drive_g, struct ata_pio_command *cmd) +send_cmd(struct atadrive_s *adrive_g, struct ata_pio_command *cmd) { - u8 ataid = GET_GLOBAL(drive_g->cntl_id); - u8 channel = ataid / 2; - u8 slave = ataid % 2; - u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u8 slave = GET_GLOBAL(adrive_g->slave); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); // Select device int status = await_not_bsy(iobase1); @@ -254,17 +251,16 @@ ata_wait_data(u16 iobase1) // Send an ata command that does not transfer any further data. int -ata_cmd_nondata(struct drive_s *drive_g, struct ata_pio_command *cmd) +ata_cmd_nondata(struct atadrive_s *adrive_g, struct ata_pio_command *cmd) { - u8 ataid = GET_GLOBAL(drive_g->cntl_id); - u8 channel = ataid / 2; - u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1); - u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); + u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2); // Disable interrupts outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC); - int ret = send_cmd(drive_g, cmd); + int ret = send_cmd(adrive_g, cmd); if (ret) goto fail; ret = ndelay_await_not_bsy(iobase1); @@ -303,10 +299,11 @@ ata_pio_transfer(struct disk_op_s *op, int iswrite, int blocksize) dprintf(16, "ata_pio_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(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); + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); + u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2); int count = op->count; void *buf_fl = op->buf_fl; int status; @@ -388,9 +385,10 @@ ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize) if (dest & 1) // Need minimum alignment of 1. return -1; - u8 ataid = GET_GLOBAL(op->drive_g->cntl_id); - u8 channel = ataid / 2; - u16 iomaster = GET_GLOBAL(ATA_channels[channel].iomaster); + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster); if (! iomaster) return -1; u32 bytes = op->count * blocksize; @@ -407,8 +405,6 @@ ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize) // Too many descriptors.. return -1; u32 count = bytes; - if (count > 0x10000) - count = 0x10000; u32 max = 0x10000 - (dest & 0xffff); if (count > max) count = max; @@ -439,12 +435,12 @@ ata_dma_transfer(struct disk_op_s *op) { if (! CONFIG_ATA_DMA) return -1; - dprintf(16, "ata_dma_transfer id=%p buf=%p\n" - , op->drive_g, op->buf_fl); + dprintf(16, "ata_dma_transfer id=%p buf=%p\n", op->drive_g, op->buf_fl); - u8 ataid = GET_GLOBAL(op->drive_g->cntl_id); - u8 channel = ataid / 2; - u16 iomaster = GET_GLOBAL(ATA_channels[channel].iomaster); + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster); // Start bus-master controller. u8 oldcmd = inb(iomaster + BM_CMD); @@ -457,7 +453,7 @@ ata_dma_transfer(struct disk_op_s *op) if (status & BM_STATUS_IRQ) break; // Transfer in progress - if (check_time(end)) { + if (check_tsc(end)) { // Timeout. warn_timeout(); break; @@ -466,8 +462,8 @@ ata_dma_transfer(struct disk_op_s *op) } outb(oldcmd & ~BM_CMD_START, iomaster + BM_CMD); - u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1); - u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); + u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2); int idestatus = pause_await_not_bsy(iobase1, iobase2); if ((status & (BM_STATUS_IRQ|BM_STATUS_ACTIVE)) == BM_STATUS_IRQ @@ -492,15 +488,16 @@ ata_dma_transfer(struct disk_op_s *op) static int ata_pio_cmd_data(struct disk_op_s *op, int iswrite, struct ata_pio_command *cmd) { - 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); + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); + u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2); // Disable interrupts outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC); - int ret = send_cmd(op->drive_g, cmd); + int ret = send_cmd(adrive_g, cmd); if (ret) goto fail; ret = ata_wait_data(iobase1); @@ -520,7 +517,9 @@ ata_dma_cmd_data(struct disk_op_s *op, struct ata_pio_command *cmd) { if (! CONFIG_ATA_DMA) return -1; - int ret = send_cmd(op->drive_g, cmd); + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); + int ret = send_cmd(adrive_g, cmd); if (ret) return ret; return ata_dma_transfer(op); @@ -603,10 +602,14 @@ process_ata_op(struct disk_op_s *op) int atapi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize) { - 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); + if (! CONFIG_ATA) + return 0; + + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); + u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2); struct ata_pio_command cmd; memset(&cmd, 0, sizeof(cmd)); @@ -617,7 +620,7 @@ atapi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize) // Disable interrupts outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC); - int ret = send_cmd(op->drive_g, &cmd); + int ret = send_cmd(adrive_g, &cmd); if (ret) goto fail; ret = ata_wait_data(iobase1); @@ -653,32 +656,20 @@ atapi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize) fail: // Enable interrupts outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC); - return ret; -} - -// Read sectors from the cdrom. -int -cdrom_read(struct disk_op_s *op) -{ - struct cdb_rwdata_10 cmd; - memset(&cmd, 0, sizeof(cmd)); - cmd.command = CDB_CMD_READ_10; - cmd.lba = htonl(op->lba); - cmd.count = htons(op->count); - return atapi_cmd_data(op, &cmd, CDROM_SECTOR_SIZE); + if (ret) + return DISK_RET_EBADTRACK; + return DISK_RET_SUCCESS; } // 16bit command demuxer for ATAPI cdroms. int process_atapi_op(struct disk_op_s *op) { - int ret; + if (!CONFIG_ATA) + return 0; switch (op->command) { case CMD_READ: - ret = cdrom_read(op); - if (ret) - return DISK_RET_EBADTRACK; - return DISK_RET_SUCCESS; + return cdb_read(op); case CMD_FORMAT: case CMD_WRITE: return DISK_RET_EWRITEPROTECT; @@ -694,13 +685,13 @@ process_atapi_op(struct disk_op_s *op) // Send an identify device or identify device packet command. static int -send_ata_identity(struct drive_s *drive_g, u16 *buffer, int command) +send_ata_identity(struct atadrive_s *adrive_g, u16 *buffer, int command) { memset(buffer, 0, DISK_SECTOR_SIZE); struct disk_op_s dop; memset(&dop, 0, sizeof(dop)); - dop.drive_g = drive_g; + dop.drive_g = &adrive_g->drive; dop.count = 1; dop.lba = 1; dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer); @@ -713,8 +704,8 @@ send_ata_identity(struct drive_s *drive_g, u16 *buffer, int command) } // Extract the ATA/ATAPI version info. -static int -extract_version(u16 *buffer) +int +ata_extract_version(u16 *buffer) { // Extract ATA/ATAPI version. u16 ataversion = buffer[80]; @@ -725,49 +716,41 @@ extract_version(u16 *buffer) return version; } -// Extract common information from IDENTIFY commands. -static void -extract_identify(struct drive_s *drive_g, u16 *buffer) -{ - dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]); +#define MAXMODEL 40 +// Extract the ATA/ATAPI model info. +char * +ata_extract_model(char *model, u32 size, u16 *buffer) +{ // Read model name - char *model = drive_g->model; - int maxsize = ARRAY_SIZE(drive_g->model); int i; - for (i=0; i> 8; - model[i*2+1] = v & 0xff; - } - model[maxsize-1] = 0x00; - - // Trim trailing spaces from model name. - for (i=maxsize-2; i>0 && model[i] == 0x20; i--) - model[i] = 0x00; - - // Common flags. - SET_GLOBAL(drive_g->removable, (buffer[0] & 0x80) ? 1 : 0); - SET_GLOBAL(drive_g->cntl_info, extract_version(buffer)); + for (i=0; icntl_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")); + struct atadrive_s *adrive_g = malloc_fseg(sizeof(*adrive_g)); + if (!adrive_g) { + warn_noalloc(); + return NULL; + } + memset(adrive_g, 0, sizeof(*adrive_g)); + adrive_g->chan_gf = dummy->chan_gf; + adrive_g->slave = dummy->slave; + adrive_g->drive.cntl_id = adrive_g->chan_gf->chanid * 2 + dummy->slave; + adrive_g->drive.removable = (buffer[0] & 0x80) ? 1 : 0; + return adrive_g; } // Detect if the given drive is an atapi - initialize it if so. -static struct drive_s * -init_drive_atapi(struct drive_s *dummy, u16 *buffer) +static struct atadrive_s * +init_drive_atapi(struct atadrive_s *dummy, u16 *buffer) { // Send an IDENTIFY_DEVICE_PACKET command to device int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_PACKET_DEVICE); @@ -775,48 +758,36 @@ init_drive_atapi(struct drive_s *dummy, u16 *buffer) return NULL; // Success - setup as ATAPI. - struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g)); - if (! drive_g) { - warn_noalloc(); + struct atadrive_s *adrive_g = init_atadrive(dummy, buffer); + if (!adrive_g) return NULL; - } - memset(drive_g, 0, sizeof(*drive_g)); - 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); + adrive_g->drive.type = DTYPE_ATAPI; + adrive_g->drive.blksize = CDROM_SECTOR_SIZE; + adrive_g->drive.sectors = (u64)-1; u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05; - SET_GLOBAL(drive_g->floppy_type, iscd); + char model[MAXMODEL+1]; + char *desc = znprintf(MAXDESCSIZE + , "DVD/CD [ata%d-%d: %s ATAPI-%d %s]" + , adrive_g->chan_gf->chanid, adrive_g->slave + , ata_extract_model(model, MAXMODEL, buffer) + , ata_extract_version(buffer) + , (iscd ? "DVD/CD" : "Device")); + dprintf(1, "%s\n", desc); // fill cdidmap - if (iscd) - map_cd_drive(drive_g); + if (iscd) { + int prio = bootprio_find_ata_device(adrive_g->chan_gf->pci_tmp, + adrive_g->chan_gf->chanid, + adrive_g->slave); + boot_add_cd(&adrive_g->drive, desc, prio); + } - return drive_g; -} - -// Print out a description of the given ata drive. -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)); + return adrive_g; } // Detect if the given drive is a regular ata drive - initialize it if so. -static struct drive_s * -init_drive_ata(struct drive_s *dummy, u16 *buffer) +static struct atadrive_s * +init_drive_ata(struct atadrive_s *dummy, u16 *buffer) { // Send an IDENTIFY_DEVICE command to device int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_DEVICE); @@ -824,35 +795,44 @@ init_drive_ata(struct drive_s *dummy, u16 *buffer) return NULL; // Success - setup as ATA. - struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g)); - if (! drive_g) { - warn_noalloc(); + struct atadrive_s *adrive_g = init_atadrive(dummy, buffer); + if (!adrive_g) return NULL; - } - memset(drive_g, 0, sizeof(*drive_g)); - 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); + adrive_g->drive.type = DTYPE_ATA; + adrive_g->drive.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]); + adrive_g->drive.pchs.cylinders = buffer[1]; + adrive_g->drive.pchs.heads = buffer[3]; + adrive_g->drive.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(drive_g->sectors, sectors); - - // Setup disk geometry translation. - setup_translation(drive_g); - + adrive_g->drive.sectors = sectors; + u64 adjsize = sectors >> 11; + char adjprefix = 'M'; + if (adjsize >= (1 << 16)) { + adjsize >>= 10; + adjprefix = 'G'; + } + char model[MAXMODEL+1]; + char *desc = znprintf(MAXDESCSIZE + , "ata%d-%d: %s ATA-%d Hard-Disk (%u %ciBytes)" + , adrive_g->chan_gf->chanid, adrive_g->slave + , ata_extract_model(model, MAXMODEL, buffer) + , ata_extract_version(buffer) + , (u32)adjsize, adjprefix); + dprintf(1, "%s\n", desc); + + int prio = bootprio_find_ata_device(adrive_g->chan_gf->pci_tmp, + adrive_g->chan_gf->chanid, + adrive_g->slave); // Register with bcv system. - add_bcv_internal(drive_g); + boot_add_hd(&adrive_g->drive, desc, prio); - return drive_g; + return adrive_g; } static u64 SpinupEnd; @@ -869,10 +849,10 @@ powerup_await_non_bsy(u16 base) break; orstatus |= status; if (orstatus == 0xff) { - dprintf(1, "powerup IDE floating\n"); + dprintf(4, "powerup IDE floating\n"); return orstatus; } - if (check_time(SpinupEnd)) { + if (check_tsc(SpinupEnd)) { warn_timeout(); return -1; } @@ -886,21 +866,16 @@ powerup_await_non_bsy(u16 base) static void ata_detect(void *data) { - struct ata_channel_s *atachannel = data; - int startid = (atachannel - ATA_channels) * 2; - struct drive_s dummy; + struct ata_channel_s *chan_gf = data; + struct atadrive_s dummy; memset(&dummy, 0, sizeof(dummy)); + dummy.chan_gf = chan_gf; // Device detection - int ataid, last_reset_ataid=-1; - for (ataid=startid; ataidiobase1; int status = powerup_await_non_bsy(iobase1); if (status < 0) continue; @@ -918,26 +893,24 @@ ata_detect(void *data) outb(0xaa, iobase1+ATA_CB_SN); u8 sc = inb(iobase1+ATA_CB_SC); u8 sn = inb(iobase1+ATA_CB_SN); - dprintf(6, "ata_detect ataid=%d sc=%x sn=%x dh=%x\n" - , ataid, sc, sn, dh); + dprintf(6, "ata_detect ata%d-%d: sc=%x sn=%x dh=%x\n" + , chan_gf->chanid, slave, sc, sn, dh); if (sc != 0x55 || sn != 0xaa || dh != newdh) continue; // Prepare new drive. - dummy.cntl_id = ataid; + dummy.slave = slave; // reset the channel - if (slave && ataid == last_reset_ataid + 1) { - // The drive was just reset - no need to reset it again. - } else { + if (!didreset) { ata_reset(&dummy); - last_reset_ataid = ataid; + didreset = 1; } // check for ATAPI u16 buffer[256]; - struct drive_s *drive_g = init_drive_atapi(&dummy, buffer); - if (!drive_g) { + struct atadrive_s *adrive_g = init_drive_atapi(&dummy, buffer); + if (!adrive_g) { // Didn't find an ATAPI drive - look for ATA drive. u8 st = inb(iobase1+ATA_CB_STAT); if (!st) @@ -950,8 +923,8 @@ ata_detect(void *data) continue; // check for ATA. - drive_g = init_drive_ata(&dummy, buffer); - if (!drive_g) + adrive_g = init_drive_ata(&dummy, buffer); + if (!adrive_g) // No ATA drive found continue; } @@ -962,88 +935,121 @@ ata_detect(void *data) // resetresult looks valid and device 0 is responding to // device 1 requests - device 1 must not be present - skip // detection. - ataid++; + break; } } // Initialize an ata controller and detect its drives. static void -init_controller(struct ata_channel_s *atachannel - , int bdf, int irq, u32 port1, u32 port2, u32 master) +init_controller(struct pci_device *pci, int irq + , u32 port1, u32 port2, u32 master) { - SET_GLOBAL(atachannel->irq, irq); - SET_GLOBAL(atachannel->pci_bdf, bdf); - SET_GLOBAL(atachannel->iobase1, port1); - SET_GLOBAL(atachannel->iobase2, port2); - SET_GLOBAL(atachannel->iomaster, master); + static int chanid = 0; + struct ata_channel_s *chan_gf = malloc_fseg(sizeof(*chan_gf)); + if (!chan_gf) { + warn_noalloc(); + return; + } + chan_gf->chanid = chanid++; + chan_gf->irq = irq; + chan_gf->pci_bdf = pci ? pci->bdf : -1; + chan_gf->pci_tmp = pci; + chan_gf->iobase1 = port1; + chan_gf->iobase2 = port2; + chan_gf->iomaster = master; dprintf(1, "ATA controller %d at %x/%x/%x (irq %d dev %x)\n" - , atachannel - ATA_channels, port1, port2, master, irq, bdf); - run_thread(ata_detect, atachannel); + , chanid, port1, port2, master, irq, chan_gf->pci_bdf); + run_thread(ata_detect, chan_gf); } #define IRQ_ATA1 14 #define IRQ_ATA2 15 -// Locate and init ata controllers. +// Handle controllers on an ATA PCI device. static void -ata_init(void) +init_pciata(struct pci_device *pci, u8 prog_if) { - // Scan PCI bus for ATA adapters - int count=0, pcicount=0; - int bdf, max; - foreachpci(bdf, max) { - pcicount++; - if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE) - continue; - if (count >= ARRAY_SIZE(ATA_channels)) - break; - - u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE); - u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG); - int master = 0; - if (CONFIG_ATA_DMA && prog_if & 0x80) { - // Check for bus-mastering. - u32 bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_4); - if (bar & PCI_BASE_ADDRESS_SPACE_IO) { - master = bar & PCI_BASE_ADDRESS_IO_MASK; - pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER); - } - } - - u32 port1, port2, irq; - if (prog_if & 1) { - port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3; - port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3; - irq = pciirq; - } else { - port1 = PORT_ATA1_CMD_BASE; - port2 = PORT_ATA1_CTRL_BASE; - irq = IRQ_ATA1; + pci->have_driver = 1; + u16 bdf = pci->bdf; + u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE); + int master = 0; + if (CONFIG_ATA_DMA && prog_if & 0x80) { + // Check for bus-mastering. + u32 bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_4); + if (bar & PCI_BASE_ADDRESS_SPACE_IO) { + master = bar & PCI_BASE_ADDRESS_IO_MASK; + pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER); } - init_controller(&ATA_channels[count], bdf, irq, port1, port2, master); - count++; + } - if (prog_if & 4) { - port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3; - port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3; - irq = pciirq; - } else { - port1 = PORT_ATA2_CMD_BASE; - port2 = PORT_ATA2_CTRL_BASE; - irq = IRQ_ATA2; - } - init_controller(&ATA_channels[count], bdf, irq, port1, port2 - , master ? master + 8 : 0); - count++; + u32 port1, port2, irq; + if (prog_if & 1) { + port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_0) + & PCI_BASE_ADDRESS_IO_MASK); + port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_1) + & PCI_BASE_ADDRESS_IO_MASK); + irq = pciirq; + } else { + port1 = PORT_ATA1_CMD_BASE; + port2 = PORT_ATA1_CTRL_BASE; + irq = IRQ_ATA1; + } + init_controller(pci, irq, port1, port2, master); + + if (prog_if & 4) { + port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_2) + & PCI_BASE_ADDRESS_IO_MASK); + port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_3) + & PCI_BASE_ADDRESS_IO_MASK); + irq = pciirq; + } else { + port1 = PORT_ATA2_CMD_BASE; + port2 = PORT_ATA2_CTRL_BASE; + irq = IRQ_ATA2; } + init_controller(pci, irq, port1, port2, master ? master + 8 : 0); +} + +static void +found_genericata(struct pci_device *pci, void *arg) +{ + init_pciata(pci, pci->prog_if); +} + +static void +found_compatibleahci(struct pci_device *pci, void *arg) +{ + if (CONFIG_AHCI) + // Already handled directly via native ahci interface. + return; + init_pciata(pci, 0x8f); +} - if (!CONFIG_COREBOOT && !pcicount && ARRAY_SIZE(ATA_channels) >= 2) { +static const struct pci_device_id pci_ata_tbl[] = { + PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE + , found_genericata), + PCI_DEVICE(PCI_VENDOR_ID_ATI, 0x4391, found_compatibleahci), + PCI_DEVICE_END, +}; + +// Locate and init ata controllers. +static void +ata_init(void) +{ + if (!CONFIG_COREBOOT && !PCIDevices) { // No PCI devices found - probably a QEMU "-M isapc" machine. // Try using ISA ports for ATA controllers. - init_controller(&ATA_channels[0], -1, IRQ_ATA1 + init_controller(NULL, IRQ_ATA1 , PORT_ATA1_CMD_BASE, PORT_ATA1_CTRL_BASE, 0); - init_controller(&ATA_channels[1], -1, IRQ_ATA2 + init_controller(NULL, IRQ_ATA2 , PORT_ATA2_CMD_BASE, PORT_ATA2_CTRL_BASE, 0); + return; + } + + // Scan PCI bus for ATA adapters + struct pci_device *pci; + foreachpci(pci) { + pci_init_device(pci_ata_tbl, pci, NULL); } } @@ -1061,5 +1067,5 @@ ata_setup(void) SET_BDA(disk_control_byte, 0xc0); - enable_hwirq(14, entry_76); + enable_hwirq(14, FUNC16(entry_76)); }