Index: trunk/src/os2ahci/ahci.c
===================================================================
--- trunk/src/os2ahci/ahci.c	(revision 181)
+++ trunk/src/os2ahci/ahci.c	(revision 182)
@@ -207,68 +207,22 @@
 #endif
 
-int AllocPortData(AD_INFO *ai, int p)
-{
-  if (ai->ports[p]) return 0;
-  ai->ports[p] = MemAlloc(sizeof(P_INFO));
-  if (ai->ports[p] == NULL) return 1;
-  memset(ai->ports[p], 0, sizeof(P_INFO));
-  return 0;
-}
-
-FreePortData(AD_INFO *ai, int p)
-{
-}
-
-/******************************************************************************
- * Save BIOS configuration of AHCI adapter. As a side effect, this also saves
- * generic configuration information which we may have to restore after an
- * adapter reset.
- *
- * NOTE: This function also saves working copies of the CAP and CAP2 registers
- *       as well as the initial port map in the AD_INFO structure after
- *       removing features which are known to cause trouble on this specific
- *       piece of hardware.
- */
-int ahci_save_bios_config(AD_INFO *ai)
+/******************************************************************************
+ * setup the CAPS and other adapter information for this adapter.
+ *
+ * This function saves working copies of the CAP and CAP2 registers
+ * as well as the initial port map in the AD_INFO structure after
+ * removing features which are known to cause trouble on this specific
+ * piece of hardware.
+ */
+int ahci_config_caps(AD_INFO *ai)
 {
   int ports;
   int i;
 
-  /* save BIOS configuration */
-  for (i = 0; i < HOST_CAP2; i += sizeof(u32))
-  {
-    ai->bios_config[i / sizeof(u32)] = readl(ai->mmio + i);
-  }
-
-  DPRINTF(3,"ahci_save_bios_config: BIOS AHCI mode is %d\n", ai->bios_config[HOST_CTL / sizeof(u32)] & HOST_AHCI_EN);
+  ai->cap = readl(ai->mmio + HOST_CAP);
+  ai->port_map = readl(ai->mmio + HOST_PORTS_IMPL);
 
   /* HOST_CAP2 only exists for AHCI V1.2 and later */
-  if (ai->bios_config[HOST_VERSION / sizeof(u32)] >= 0x00010200L)
-  {
-    ai->bios_config[HOST_CAP2 / sizeof(u32)] = readl(ai->mmio + HOST_CAP2);
-  }
-  else
-  {
-    ai->bios_config[HOST_CAP2 / sizeof(u32)] = 0;
-  }
-
-  if ((ai->bios_config[HOST_CTL / sizeof(u32)] & HOST_AHCI_EN) == 0 &&
-      ai->pci_vendor == PCI_VENDOR_ID_INTEL)
-  {
-    /* Adapter is not in AHCI mode and the spec says a COMRESET is
-     * required when switching from SATA to AHCI mode and vice versa.
-     */
-    init_reset = 1;
-  }
-
-  DUMP_HOST_REGS(2,ai,1);
-
-  /* Save working copies of CAP, CAP2 and port_map and remove broken feature
-   * bits. This is largely copied from the Linux AHCI driver -- the wisdom
-   * around quirks and faulty hardware is hard to come by...
-   */
-  ai->cap      = ai->bios_config[HOST_CAP        / sizeof(u32)];
-  ai->cap2     = ai->bios_config[HOST_CAP2       / sizeof(u32)];
-  ai->port_map = ai->bios_config[HOST_PORTS_IMPL / sizeof(u32)];
+  if (readl(ai->mmio + HOST_VERSION) >= 0x00010200L) ai->cap2 = readl(ai->mmio + HOST_CAP2);
 
   if (ai->pci->board >= sizeof(initial_flags) / sizeof(*initial_flags))
@@ -289,4 +243,7 @@
   }
 
+  /* Remove broken feature bits. This is largely copied from the Linux AHCI driver -- the wisdom
+   * around quirks and faulty hardware is hard to come by...
+   */
   if ((ai->cap & HOST_CAP_NCQ) && (ai->flags & AHCI_HFLAG_NO_NCQ))
   {
@@ -332,8 +289,5 @@
   for (i = 0; i < AHCI_MAX_PORTS; i++)
   {
-    if (ai->port_map & (1UL << i))
-    {
-      ports--;
-    }
+    if (ai->port_map & (1UL << i)) ports--;
   }
   if (ports < 0)
@@ -347,4 +301,34 @@
   /* set maximum command slot number */
   ai->cmd_max = ((ai->cap >> 8) & 0x1f);
+
+  return(0);
+}
+
+/******************************************************************************
+ * Save BIOS configuration of AHCI adapter. As a side effect, this also saves
+ * generic configuration information which we may have to restore after an
+ * adapter reset.
+ */
+int ahci_save_bios_config(AD_INFO *ai)
+{
+  int i;
+
+  /* save BIOS configuration */
+  for (i = 0; i < HOST_CAP2; i += sizeof(u32))
+  {
+    ai->bios_config[i / sizeof(u32)] = readl(ai->mmio + i);
+  }
+
+  DPRINTF(3,"ahci_save_bios_config: BIOS AHCI mode is %d\n", ai->bios_config[HOST_CTL / sizeof(u32)] & HOST_AHCI_EN);
+
+  if ((ai->bios_config[HOST_CTL / sizeof(u32)] & HOST_AHCI_EN) == 0 && ai->pci_vendor == PCI_VENDOR_ID_INTEL)
+  {
+    /* Adapter is not in AHCI mode and the spec says a COMRESET is
+     * required when switching from SATA to AHCI mode and vice versa.
+     */
+    init_reset = 1;
+  }
+
+  DUMP_HOST_REGS(2,ai,1);
 
   return(0);
@@ -649,4 +633,9 @@
       /* this port seems to have a device attached and ready for commands */
       DPRINTF(1,"ahci_scan_ports: port %d seems to be attached to a device; probing...\n", p);
+
+      #ifdef DAZ_NEW_CODE
+      ai->dma_buf[p] = MemAllocAlign(AHCI_PORT_PRIV_DMA_SZ, 1024);
+      ai->dma_buf_phys[p] = MemPhysAdr(ai->dma_buf[p]);
+      #endif
 
       /* Get ATA(PI) identity. The so-called signature gives us a hint whether
@@ -681,5 +670,8 @@
       /* no device attached to this port */
       ai->port_map &= ~(1UL << p);
-      // DAZ free port structure here
+      #ifdef DAZ_NEW_CODE
+      if (ai->dma_buf[p]) MemFree(ai->dma_buf[p]);
+      ai->dma_buf[p] = NULL;
+      #endif
     }
 
Index: trunk/src/os2ahci/ata.c
===================================================================
--- trunk/src/os2ahci/ata.c	(revision 181)
+++ trunk/src/os2ahci/ata.c	(revision 182)
@@ -263,6 +263,5 @@
     do
     {
-      u32 chunk = (sg_size > AHCI_MAX_SG_ELEMENT_LEN) ? AHCI_MAX_SG_ELEMENT_LEN
-                                                      : sg_size;
+      u32 chunk = (sg_size > AHCI_MAX_SG_ELEMENT_LEN) ? AHCI_MAX_SG_ELEMENT_LEN : sg_size;
       if (n >= AHCI_MAX_SG)
       {
Index: trunk/src/os2ahci/os2ahci.h
===================================================================
--- trunk/src/os2ahci/os2ahci.h	(revision 181)
+++ trunk/src/os2ahci/os2ahci.h	(revision 182)
@@ -42,4 +42,5 @@
 #define DEBUG
 //#define LEGACY_APM
+//#define DAZ_NEW_CODE
 
 #include "Dev32lib.h"
@@ -401,4 +402,5 @@
 
 /* ahci.c */
+extern int ahci_config_caps(AD_INFO *ai);
 extern int ahci_save_bios_config(AD_INFO *ai);
 extern int ahci_restore_bios_config(AD_INFO *ai);
Index: trunk/src/os2ahci/pci.c
===================================================================
--- trunk/src/os2ahci/pci.c	(revision 181)
+++ trunk/src/os2ahci/pci.c	(revision 182)
@@ -602,11 +602,4 @@
   if (!ad_info->mmio) goto add_pci_fail;
 
-  /* fill in DMA scratch buffer addresses in adapter info */
-  for (i = 0; i < AHCI_MAX_PORTS; i++)
-  {
-    ad_info->dma_buf[i] = MemAlloc(AHCI_PORT_PRIV_DMA_SZ);
-    ad_info->dma_buf_phys[i] = MemPhysAdr(ad_info->dma_buf[i]);
-  }
-
   /* register adapter with resource manager */
   memset(&adj, 0x00, sizeof(adj));
@@ -634,8 +627,20 @@
   }
 
+  if (ahci_config_caps(ad_info)) goto add_pci_fail;
+
+  #ifndef DAZ_NEW_CODE
+  /* fill in DMA scratch buffer addresses in adapter info */
+  for (i = 0; i < ad_info->hw_ports; i++)
+  {
+    ad_info->dma_buf[i] = MemAlloc(AHCI_PORT_PRIV_DMA_SZ);
+    ad_info->dma_buf_phys[i] = MemPhysAdr(ad_info->dma_buf[i]);
+  }
+  #endif
+
   /* Successfully added the adapter and reserved its resources; the adapter
    * is still under BIOS control so we're not going to do anything else at
    * this point.
    */
+
   ad_info_cnt++;
   return;
