Change license from GPLv3 to LGPLv3.
[seabios.git] / src / ata.c
index 04b0a439f27043df548ec0f35296f6bdce63789a..0b0bdc1fd80031de1bca88022870a29529d95c3b 100644 (file)
--- a/src/ata.c
+++ b/src/ata.c
@@ -3,15 +3,19 @@
 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
 // Copyright (C) 2002  MandrakeSoft S.A.
 //
-// This file may be distributed under the terms of the GNU GPLv3 license.
+// This file may be distributed under the terms of the GNU LGPLv3 license.
 
-#include "ata.h" // ATA_*
 #include "types.h" // u8
 #include "ioport.h" // inb
 #include "util.h" // dprintf
 #include "cmos.h" // inb_cmos
-#include "pic.h" // unmask_pic2
+#include "pic.h" // enable_hwirq
 #include "biosvar.h" // GET_EBDA
+#include "pci.h" // pci_find_class
+#include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
+#include "pci_regs.h" // PCI_INTERRUPT_LINE
+#include "disk.h" // struct ata_s
+#include "atabits.h" // ATA_CB_STAT
 
 #define TIMEOUT 0
 #define BSY 1
@@ -25,6 +29,8 @@
 
 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
 
+struct ata_s ATA VAR16_32;
+
 
 /****************************************************************
  * Helper functions
 static int
 await_ide(u8 when_done, u16 base, u16 timeout)
 {
-    u32 time=0, last=0;
+    u64 end = calc_future_tsc(timeout);
     for (;;) {
         u8 status = inb(base+ATA_CB_STAT);
-        time++;
         u8 result = 0;
         if (when_done == BSY)
             result = status & ATA_CB_STAT_BSY;
@@ -52,20 +57,13 @@ await_ide(u8 when_done, u16 base, u16 timeout)
 
         if (result)
             return status;
-        // mod 2048 each 16 ms
-        if (time>>16 != last) {
-            last = time >>16;
-            dprintf(6, "await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ"
-                    ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
-                    , when_done, time>>11, timeout);
-        }
         if (status & ATA_CB_STAT_ERR) {
             dprintf(1, "await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
-                    ",!BSY_!DRQ,!BSY_RDY) %d status=%x time= %d timeout= %d\n"
-                    , when_done, status, time>>11, timeout);
+                    ",!BSY_!DRQ,!BSY_RDY) %d status=%x timeout=%d\n"
+                    , when_done, status, timeout);
             return -1;
         }
-        if (timeout == 0 || (time>>11) > timeout)
+        if (rdtscll() >= end)
             break;
     }
     dprintf(1, "IDE time out\n");
@@ -82,38 +80,22 @@ pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout)
     return await_ide(when_done, iobase1, timeout);
 }
 
-// Delay for x nanoseconds
-static void
-nsleep(u32 delay)
-{
-    // XXX - how to implement ndelay?
-    while (delay--)
-        nop();
-}
-
 // Wait for ide state - pause for 400ns first.
 static __always_inline int
 ndelay_await_ide(u8 when_done, u16 iobase1, u16 timeout)
 {
-    nsleep(400);
+    ndelay(400);
     return await_ide(when_done, iobase1, timeout);
 }
 
-// Delay for x milliseconds
-static void
-msleep(u32 delay)
-{
-    usleep(delay * 1000);
-}
-
 // Reset a drive
 void
 ata_reset(int driveid)
 {
     u8 channel = driveid / 2;
     u8 slave = driveid % 2;
-    u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
-    u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
+    u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
+    u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
 
     // Reset
 
@@ -130,13 +112,13 @@ ata_reset(int driveid)
     // 8.2.1 (g) -- check for sc==sn==0x01
     // select device
     outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
-    msleep(50);
+    mdelay(50);
     u8 sc = inb(iobase1+ATA_CB_SC);
     u8 sn = inb(iobase1+ATA_CB_SN);
 
     // For predetermined ATA drives - wait for ready.
     if (sc==0x01 && sn==0x01) {
-        u8 type=GET_EBDA(ata.devices[driveid].type);
+        u8 type=GET_GLOBAL(ATA.devices[driveid].type);
         if (type == ATA_TYPE_ATA)
             await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
     }
@@ -154,13 +136,6 @@ ata_reset(int driveid)
  * ATA send command
  ****************************************************************/
 
-struct ata_op_s {
-    u64 lba;
-    void *far_buffer;
-    u16 driveid;
-    u16 count;
-};
-
 struct ata_pio_command {
     u8 feature;
     u8 sector_count;
@@ -181,8 +156,8 @@ static int
 send_cmd(int driveid, struct ata_pio_command *cmd)
 {
     u8 channel = driveid / 2;
-    u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
-    u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
+    u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
+    u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
 
     int status = inb(iobase1 + ATA_CB_STAT);
     if (status & ATA_CB_STAT_BSY)
@@ -196,7 +171,7 @@ send_cmd(int driveid, struct ata_pio_command *cmd)
     outb(cmd->device, iobase1 + ATA_CB_DH);
     if ((device ^ cmd->device) & (1 << 4))
         // Wait for device to become active.
-        msleep(50);
+        mdelay(50);
 
     if (cmd->command & 0x04) {
         outb(0x00, iobase1 + ATA_CB_FR);
@@ -265,12 +240,12 @@ ata_transfer(int driveid, int iswrite, int count, int blocksize
             , skipfirst, skiplast, far_buffer);
 
     // Reset count of transferred data
-    SET_EBDA(ata.trsfsectors, 0);
+    SET_EBDA(sector_count, 0);
 
     u8 channel  = driveid / 2;
-    u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
-    u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
-    u8 mode     = GET_EBDA(ata.devices[driveid].mode);
+    u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
+    u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
+    u8 mode     = GET_GLOBAL(ATA.devices[driveid].mode);
     int current = 0;
     int status;
     for (;;) {
@@ -308,7 +283,7 @@ ata_transfer(int driveid, int iswrite, int count, int blocksize
             return status;
 
         current++;
-        SET_EBDA(ata.trsfsectors, current);
+        SET_EBDA(sector_count, current);
         if (current == count)
             break;
         status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
@@ -336,30 +311,30 @@ ata_transfer(int driveid, int iswrite, int count, int blocksize
 }
 
 static noinline int
-ata_transfer_disk(const struct ata_op_s *op, int iswrite)
+ata_transfer_disk(const struct disk_op_s *op)
 {
-    return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
-                        , 0, 0, op->far_buffer);
+    return ata_transfer(op->driveid, op->command == ATA_CMD_WRITE_SECTORS
+                        , op->count, IDE_SECTOR_SIZE, 0, 0, op->far_buffer);
 }
 
 static noinline int
-ata_transfer_cdrom(const struct ata_op_s *op)
+ata_transfer_cdrom(const struct disk_op_s *op)
 {
     return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
                         , 0, 0, op->far_buffer);
 }
 
 static noinline int
-ata_transfer_emu(const struct ata_op_s *op, int before, int after)
+ata_transfer_cdemu(const struct disk_op_s *op, int before, int after)
 {
     int vcount = op->count * 4 - before - after;
     int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
                            , before*512, after*512, op->far_buffer);
     if (ret) {
-        SET_EBDA(ata.trsfsectors, 0);
+        SET_EBDA(sector_count, 0);
         return ret;
     }
-    SET_EBDA(ata.trsfsectors, vcount);
+    SET_EBDA(sector_count, vcount);
     return 0;
 }
 
@@ -369,7 +344,7 @@ ata_transfer_emu(const struct ata_op_s *op, int before, int after)
  ****************************************************************/
 
 static noinline int
-send_cmd_disk(const struct ata_op_s *op, u16 command)
+send_cmd_disk(const struct disk_op_s *op)
 {
     u8 slave = op->driveid % 2;
     u64 lba = op->lba;
@@ -377,7 +352,7 @@ send_cmd_disk(const struct ata_op_s *op, u16 command)
     struct ata_pio_command cmd;
     memset(&cmd, 0, sizeof(cmd));
 
-    cmd.command = command;
+    cmd.command = op->command;
     if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
         cmd.sector_count2 = op->count >> 8;
         cmd.lba_low2 = lba >> 24;
@@ -401,20 +376,12 @@ send_cmd_disk(const struct ata_op_s *op, u16 command)
 
 // Read/write count blocks from a harddrive.
 __always_inline int
-ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
+ata_cmd_data(struct disk_op_s *op)
 {
-    struct ata_op_s op;
-    op.driveid = driveid;
-    op.lba = lba;
-    op.count = count;
-    op.far_buffer = far_buffer;
-
-    int ret = send_cmd_disk(&op, command);
+    int ret = send_cmd_disk(op);
     if (ret)
         return ret;
-
-    int iswrite = command == ATA_CMD_WRITE_SECTORS;
-    return ata_transfer_disk(&op, iswrite);
+    return ata_transfer_disk(op);
 }
 
 
@@ -428,8 +395,8 @@ send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
 {
     u8 channel = driveid / 2;
     u8 slave = driveid % 2;
-    u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
-    u16 iobase2 = GET_EBDA(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;
@@ -456,7 +423,7 @@ send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
 
 // Low-level cdrom read atapi command transmit function.
 static int
-send_cmd_cdrom(const struct ata_op_s *op)
+send_cmd_cdrom(const struct disk_op_s *op)
 {
     u8 atacmd[12];
     memset(atacmd, 0, sizeof(atacmd));
@@ -475,49 +442,39 @@ send_cmd_cdrom(const struct ata_op_s *op)
 
 // Read sectors from the cdrom.
 __always_inline int
-cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
+cdrom_read(struct disk_op_s *op)
 {
-    struct ata_op_s op;
-    op.driveid = driveid;
-    op.lba = lba;
-    op.count = count;
-    op.far_buffer = far_buffer;
-
-    int ret = send_cmd_cdrom(&op);
+    int ret = send_cmd_cdrom(op);
     if (ret)
         return ret;
 
-    return ata_transfer_cdrom(&op);
+    return ata_transfer_cdrom(op);
 }
 
 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
 // sectors.
 __always_inline int
-cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
+cdrom_read_512(struct disk_op_s *op)
 {
+    u32 vlba = op->lba;
+    u32 vcount = op->count;
+    u32 lba = op->lba = vlba / 4;
     u32 velba = vlba + vcount - 1;
-    u32 lba = vlba / 4;
     u32 elba = velba / 4;
-    int count = elba - lba + 1;
+    op->count = elba - lba + 1;
     int before = vlba % 4;
     int after = 3 - (velba % 4);
 
-    struct ata_op_s op;
-    op.driveid = driveid;
-    op.lba = lba;
-    op.count = count;
-    op.far_buffer = far_buffer;
-
     dprintf(16, "cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
             " count=%d before=%d after=%d\n"
-            , driveid, vlba, vcount, far_buffer, lba, elba
-            , count, before, after);
+            , op->driveid, vlba, vcount, op->far_buffer, lba, elba
+            , op->count, before, after);
 
-    int ret = send_cmd_cdrom(&op);
+    int ret = send_cmd_cdrom(op);
     if (ret)
         return ret;
 
-    return ata_transfer_emu(&op, before, after);
+    return ata_transfer_cdemu(op, before, after);
 }
 
 // Send a simple atapi command to a drive.
@@ -577,18 +534,22 @@ get_ata_version(u8 *buffer)
 static void
 init_drive_atapi(int driveid)
 {
-    SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
+    SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATAPI);
 
     // Temporary values to do the transfer
-    SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
-    SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
+    SET_GLOBAL(ATA.devices[driveid].device,ATA_DEVICE_CDROM);
+    SET_GLOBAL(ATA.devices[driveid].mode, ATA_MODE_PIO16);
 
     // Now we send a IDENTIFY command to ATAPI device
     u8 buffer[0x0200];
     memset(buffer, 0, sizeof(buffer));
-    u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
-                           , 1, 1
-                           , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
+    struct disk_op_s dop;
+    dop.driveid = driveid;
+    dop.command = ATA_CMD_IDENTIFY_DEVICE_PACKET;
+    dop.count = 1;
+    dop.lba = 1;
+    dop.far_buffer = MAKE_FARPTR(GET_SEG(SS), (u32)buffer);
+    u16 ret = ata_cmd_data(&dop);
     if (ret != 0)
         BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
 
@@ -597,19 +558,19 @@ init_drive_atapi(int driveid)
     u8 mode      = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
     u16 blksize  = CDROM_SECTOR_SIZE;
 
-    SET_EBDA(ata.devices[driveid].device, type);
-    SET_EBDA(ata.devices[driveid].removable, removable);
-    SET_EBDA(ata.devices[driveid].mode, mode);
-    SET_EBDA(ata.devices[driveid].blksize, blksize);
+    SET_GLOBAL(ATA.devices[driveid].device, type);
+    SET_GLOBAL(ATA.devices[driveid].removable, removable);
+    SET_GLOBAL(ATA.devices[driveid].mode, mode);
+    SET_GLOBAL(ATA.devices[driveid].blksize, blksize);
 
     // fill cdidmap
-    u8 cdcount = GET_EBDA(ata.cdcount);
-    SET_EBDA(ata.idmap[1][cdcount], driveid);
-    SET_EBDA(ata.cdcount, ++cdcount);
+    u8 cdcount = GET_GLOBAL(ATA.cdcount);
+    SET_GLOBAL(ATA.idmap[1][cdcount], driveid);
+    SET_GLOBAL(ATA.cdcount, ++cdcount);
 
     report_model(driveid, buffer);
     u8 version = get_ata_version(buffer);
-    if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
+    if (GET_GLOBAL(ATA.devices[driveid].device)==ATA_DEVICE_CDROM)
         printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
     else
         printf(" ATAPI-%d Device\n", version);
@@ -621,20 +582,21 @@ fill_fdpt(int driveid)
     if (driveid > 1)
         return;
 
-    u16 nlc   = GET_EBDA(ata.devices[driveid].lchs.cylinders);
-    u16 nlh   = GET_EBDA(ata.devices[driveid].lchs.heads);
-    u16 nlspt = GET_EBDA(ata.devices[driveid].lchs.spt);
+    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_EBDA(ata.devices[driveid].pchs.cylinders);
-    u16 nph   = GET_EBDA(ata.devices[driveid].pchs.heads);
-    u16 npspt = GET_EBDA(ata.devices[driveid].pchs.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);
 
-    SET_EBDA(fdpt[driveid].precompensation, 0xffff);
-    SET_EBDA(fdpt[driveid].drive_control_byte, 0xc0 | ((nph > 8) << 3));
-    SET_EBDA(fdpt[driveid].landing_zone, npc);
-    SET_EBDA(fdpt[driveid].cylinders, nlc);
-    SET_EBDA(fdpt[driveid].heads, nlh);
-    SET_EBDA(fdpt[driveid].sectors, nlspt);
+    struct extended_bios_data_area_s *ebda = get_ebda_ptr();
+    ebda->fdpt[driveid].precompensation = 0xffff;
+    ebda->fdpt[driveid].drive_control_byte = 0xc0 | ((nph > 8) << 3);
+    ebda->fdpt[driveid].landing_zone = npc;
+    ebda->fdpt[driveid].cylinders = nlc;
+    ebda->fdpt[driveid].heads = nlh;
+    ebda->fdpt[driveid].sectors = nlspt;
 
     if (nlc == npc && nlh == nph && nlspt == npspt)
         // no logical CHS mapping used, just physical CHS
@@ -643,17 +605,14 @@ fill_fdpt(int driveid)
 
     // complies with Phoenix style Translated Fixed Disk Parameter
     // Table (FDPT)
-    SET_EBDA(fdpt[driveid].phys_cylinders, npc);
-    SET_EBDA(fdpt[driveid].phys_heads, nph);
-    SET_EBDA(fdpt[driveid].phys_sectors, npspt);
-    SET_EBDA(fdpt[driveid].a0h_signature, 0xa0);
+    ebda->fdpt[driveid].phys_cylinders = npc;
+    ebda->fdpt[driveid].phys_heads = nph;
+    ebda->fdpt[driveid].phys_sectors = npspt;
+    ebda->fdpt[driveid].a0h_signature = 0xa0;
 
     // Checksum structure.
-    u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
-                                           , fdpt[driveid]));
-    u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
-                                      , fdpt[driveid]) - 1);
-    SET_EBDA(fdpt[driveid].checksum, -sum);
+    u8 sum = checksum((u8*)&ebda->fdpt[driveid], sizeof(ebda->fdpt[driveid])-1);
+    ebda->fdpt[driveid].checksum = -sum;
 }
 
 static u8
@@ -669,9 +628,9 @@ get_translation(int driveid)
     }
 
     // On COREBOOT, use a heuristic to determine translation type.
-    u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
-    u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
-    u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
+    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 ATA_TRANSLATION_NONE;
@@ -684,14 +643,14 @@ static void
 setup_translation(int driveid)
 {
     u8 translation = get_translation(driveid);
-    SET_EBDA(ata.devices[driveid].translation, translation);
+    SET_GLOBAL(ATA.devices[driveid].translation, translation);
 
     u8 channel = driveid / 2;
     u8 slave = driveid % 2;
-    u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
-    u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
-    u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
-    u64 sectors = GET_EBDA(ata.devices[driveid].sectors);
+    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);
@@ -749,32 +708,36 @@ setup_translation(int driveid)
         cylinders = 1024;
     dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
 
-    SET_EBDA(ata.devices[driveid].lchs.heads, heads);
-    SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
-    SET_EBDA(ata.devices[driveid].lchs.spt, 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);
 }
 
 static void
 init_drive_ata(int driveid)
 {
-    SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA);
+    SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATA);
 
     // Temporary values to do the transfer
-    SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
-    SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
+    SET_GLOBAL(ATA.devices[driveid].device, ATA_DEVICE_HD);
+    SET_GLOBAL(ATA.devices[driveid].mode, ATA_MODE_PIO16);
 
     // Now we send a IDENTIFY command to ATA device
     u8 buffer[0x0200];
     memset(buffer, 0, sizeof(buffer));
-    u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
-                           , 1, 1
-                           , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
+    struct disk_op_s dop;
+    dop.driveid = driveid;
+    dop.command = ATA_CMD_IDENTIFY_DEVICE;
+    dop.count = 1;
+    dop.lba = 1;
+    dop.far_buffer = MAKE_FARPTR(GET_SEG(SS), (u32)buffer);
+    u16 ret = ata_cmd_data(&dop);
     if (ret)
         BX_PANIC("ata-detect: Failed to detect ATA device\n");
 
     u8 removable  = (buffer[0] & 0x80) ? 1 : 0;
-    u8 mode       = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
-    u16 blksize   = *(u16*)&buffer[10];
+    u8 mode       = buffer[48*2] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
+    u16 blksize   = IDE_SECTOR_SIZE;
 
     u16 cylinders = *(u16*)&buffer[1*2]; // word 1
     u16 heads     = *(u16*)&buffer[3*2]; // word 3
@@ -786,28 +749,28 @@ init_drive_ata(int driveid)
     else
         sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
 
-    SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
-    SET_EBDA(ata.devices[driveid].removable, removable);
-    SET_EBDA(ata.devices[driveid].mode, mode);
-    SET_EBDA(ata.devices[driveid].blksize, blksize);
-    SET_EBDA(ata.devices[driveid].pchs.heads, heads);
-    SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
-    SET_EBDA(ata.devices[driveid].pchs.spt, spt);
-    SET_EBDA(ata.devices[driveid].sectors, sectors);
+    SET_GLOBAL(ATA.devices[driveid].device, ATA_DEVICE_HD);
+    SET_GLOBAL(ATA.devices[driveid].removable, removable);
+    SET_GLOBAL(ATA.devices[driveid].mode, mode);
+    SET_GLOBAL(ATA.devices[driveid].blksize, blksize);
+    SET_GLOBAL(ATA.devices[driveid].pchs.heads, heads);
+    SET_GLOBAL(ATA.devices[driveid].pchs.cylinders, cylinders);
+    SET_GLOBAL(ATA.devices[driveid].pchs.spt, spt);
+    SET_GLOBAL(ATA.devices[driveid].sectors, sectors);
 
     // Setup disk geometry translation.
     setup_translation(driveid);
 
     // fill hdidmap
-    u8 hdcount = GET_EBDA(ata.hdcount);
-    SET_EBDA(ata.idmap[0][hdcount], driveid);
-    SET_EBDA(ata.hdcount, ++hdcount);
+    u8 hdcount = GET_BDA(hdcount);
+    SET_GLOBAL(ATA.idmap[0][hdcount], driveid);
+    SET_BDA(hdcount, ++hdcount);
 
     // Fill "fdpt" structure.
     fill_fdpt(driveid);
 
     // Report drive info to user.
-    u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11;
+    u64 sizeinmb = GET_GLOBAL(ATA.devices[driveid].sectors) >> 11;
     report_model(driveid, buffer);
     u8 version = get_ata_version(buffer);
     if (sizeinmb < (1 << 16))
@@ -820,7 +783,7 @@ init_drive_ata(int driveid)
 static void
 init_drive_unknown(int driveid)
 {
-    SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
+    SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_UNKNOWN);
 
     u8 channel = driveid / 2;
     u8 slave = driveid % 2;
@@ -836,15 +799,17 @@ ata_detect()
         u8 channel = driveid / 2;
         u8 slave = driveid % 2;
 
-        u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
-        u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
+        u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
+        u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
+        if (!iobase1)
+            break;
 
         // Disable interrupts
         outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
 
         // Look for device
         outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
-        msleep(50);
+        mdelay(50);
         outb(0x55, iobase1+ATA_CB_SC);
         outb(0xaa, iobase1+ATA_CB_SN);
         outb(0xaa, iobase1+ATA_CB_SC);
@@ -865,7 +830,7 @@ ata_detect()
 
         // check for ATA or ATAPI
         outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
-        msleep(50);
+        mdelay(50);
         sc = inb(iobase1+ATA_CB_SC);
         sn = inb(iobase1+ATA_CB_SN);
         dprintf(6, "ata_detect(2) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
@@ -899,37 +864,50 @@ ata_init()
     // hdidmap and cdidmap init.
     u8 device;
     for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
-        SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
-        SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
+        SET_GLOBAL(ATA.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
+        SET_GLOBAL(ATA.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
     }
 
-#if CONFIG_MAX_ATA_INTERFACES > 0
-    SET_EBDA(ata.channels[0].iface, ATA_IFACE_ISA);
-    SET_EBDA(ata.channels[0].iobase1, 0x1f0);
-    SET_EBDA(ata.channels[0].iobase2, 0x3f0);
-    SET_EBDA(ata.channels[0].irq, 14);
-#endif
-#if CONFIG_MAX_ATA_INTERFACES > 1
-    SET_EBDA(ata.channels[1].iface, ATA_IFACE_ISA);
-    SET_EBDA(ata.channels[1].iobase1, 0x170);
-    SET_EBDA(ata.channels[1].iobase2, 0x370);
-    SET_EBDA(ata.channels[1].irq, 15);
-#endif
-#if CONFIG_MAX_ATA_INTERFACES > 2
-    SET_EBDA(ata.channels[2].iface, ATA_IFACE_ISA);
-    SET_EBDA(ata.channels[2].iobase1, 0x1e8);
-    SET_EBDA(ata.channels[2].iobase2, 0x3e0);
-    SET_EBDA(ata.channels[2].irq, 12);
-#endif
-#if CONFIG_MAX_ATA_INTERFACES > 3
-    SET_EBDA(ata.channels[3].iface, ATA_IFACE_ISA);
-    SET_EBDA(ata.channels[3].iobase1, 0x168);
-    SET_EBDA(ata.channels[3].iobase2, 0x360);
-    SET_EBDA(ata.channels[3].irq, 11);
-#endif
-#if CONFIG_MAX_ATA_INTERFACES > 4
-#error Please fill the ATA interface informations
-#endif
+    // 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;
+
+        u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
+        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;
+
+        if (prog_if & 1) {
+            port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
+            port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
+        } else {
+            port1 = 0x1f0;
+            port2 = 0x3f0;
+        }
+        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);
+        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;
+        } else {
+            port1 = 0x170;
+            port2 = 0x370;
+        }
+        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);
+        count++;
+    }
 }
 
 void
@@ -942,11 +920,7 @@ hard_drive_setup()
     ata_init();
     ata_detect();
 
-    // Store the device count
-    SET_BDA(disk_count, GET_EBDA(ata.hdcount));
-
     SET_BDA(disk_control_byte, 0xc0);
 
-    // Enable IRQ14 (handle_76)
-    unmask_pic2(PIC2_IRQ14);
+    enable_hwirq(14, entry_76);
 }