Index: trunk/src/os2ahci/ahci.c
===================================================================
--- trunk/src/os2ahci/ahci.c	(revision 180)
+++ trunk/src/os2ahci/ahci.c	(revision 181)
@@ -207,4 +207,17 @@
 #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
@@ -605,4 +618,6 @@
     if (port_ignore[ad_no(ai)][p]) continue;
 
+    // DAZ allocate port structure here
+
     DPRINTF(3,"ahci_scan_ports: Wait till not busy on port %d\n", p);
     /* wait until all active commands have completed on this port */
@@ -629,32 +644,29 @@
       rc = ahci_start_port(ai, p, 0);
     }
-    if (rc)
-    {
-      /* no device attached to this port */
-      ai->port_map &= ~(1UL << p);
-      goto restore_port_config;
-    }
-
-    /* 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);
-
-    /* Get ATA(PI) identity. The so-called signature gives us a hint whether
-     * this is an ATA or an ATAPI device but we'll try both in either case;
-     * the signature will merely determine whether we're going to probe for
-     * an ATA or ATAPI device, first, in order to reduce the chance of sending
-     * the wrong command (which would result in a port reset given the way
-     * ahci_exec_polled_cmd() was implemented).
-     */
-    is_ata = readl(port_base(ai, p) + PORT_SIG) == 0x00000101UL;
-    for (i = 0; i < 2; i++)
-    {
-      rc = ahci_exec_polled_cmd(ai, p, 0, 500,
-                                (is_ata) ? ATA_CMD_ID_ATA : ATA_CMD_ID_ATAPI,
-                                AP_VADDR, (void *) id_buf, ATA_ID_WORDS * sizeof(u16),
-                                AP_END);
-      if (rc == 0) break;
-
-      /* try again with ATA/ATAPI swapped */
-      is_ata = !is_ata;
+
+    if (rc == 0)
+    {
+      /* 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);
+
+      /* Get ATA(PI) identity. The so-called signature gives us a hint whether
+       * this is an ATA or an ATAPI device but we'll try both in either case;
+       * the signature will merely determine whether we're going to probe for
+       * an ATA or ATAPI device, first, in order to reduce the chance of sending
+       * the wrong command (which would result in a port reset given the way
+       * ahci_exec_polled_cmd() was implemented).
+       */
+      is_ata = readl(port_base(ai, p) + PORT_SIG) == 0x00000101UL;
+      for (i = 0; i < 2; i++)
+      {
+        rc = ahci_exec_polled_cmd(ai, p, 0, 500,
+                                  (is_ata) ? ATA_CMD_ID_ATA : ATA_CMD_ID_ATAPI,
+                                  AP_VADDR, (void *) id_buf, ATA_ID_WORDS * sizeof(u16),
+                                  AP_END);
+        if (rc == 0) break;
+
+        /* try again with ATA/ATAPI swapped */
+        is_ata = !is_ata;
+      }
     }
 
@@ -669,10 +681,8 @@
       /* no device attached to this port */
       ai->port_map &= ~(1UL << p);
-    }
-
-  restore_port_config:
-    if (pc != NULL) {
-      ahci_restore_port_config(ai, p, pc);
-    }
+      // DAZ free port structure here
+    }
+
+    if (pc != NULL) ahci_restore_port_config(ai, p, pc);
   }
 
@@ -1096,5 +1106,5 @@
   }
 
-  DPRINTF(1,"---------- ahci_exec_iorb: iorb=%x\n", vIorb);
+  DPRINTF(7,"---------- ahci_exec_iorb: iorb=%x\n", vIorb);
 
   /* Enable AHCI mode; apparently, the AHCI mode may end up becoming
@@ -1122,5 +1132,5 @@
       cmd_max = ai->cmd_max;
     }
-    DPRINTF(3,"NCQ command; cmd_max = %d->%d\n", ai->cmd_max, cmd_max);
+    DPRINTF(8,"NCQ command; cmd_max = %d->%d\n", ai->cmd_max, cmd_max);
   }
 
@@ -1175,5 +1185,5 @@
         aws->cmd_slot = port->cmd_slot;
 
-        DPRINTF(1,"Issuing command Slot=%d cmds=%x\n", port->cmd_slot, *cmds);
+        DPRINTF(7,"Issuing command Slot=%d cmds=%x\n", port->cmd_slot, *cmds);
         if (aws->is_ncq)
         {
@@ -1432,6 +1442,4 @@
  * queues, ...)
  */
-u32 DazCount = 0;
-
 int ahci_intr(u16 irq)
 {
@@ -1440,6 +1448,4 @@
   int a;
   int p;
-
-  DPRINTF(1,"AI=%x",DazCount++);
 
   /* find adapter(s) with pending interrupts */
@@ -1530,5 +1536,4 @@
   readl(port_mmio + PORT_IRQ_STAT); /* flush */
 
-  DPRINTF(3,"port interrupt A=%d Port=%d stat=%x\n", ad_no(ai), p, irq_stat);
   memset(&done_queue, 0x00, sizeof(done_queue));
 
@@ -1556,5 +1561,5 @@
     active_cmds = readl(port_mmio + PORT_SCR_ACT);
     done_mask = ai->ports[p].ncq_cmds ^ active_cmds;
-    DPRINTF(1,"[ncq_cmds]: active_cmds=0x%08x done_mask=0x%08x\n", active_cmds, done_mask);
+    DPRINTF(7,"[ncq_cmds]: active_cmds=0x%08x done_mask=0x%08x\n", active_cmds, done_mask);
   }
   else
@@ -1562,5 +1567,5 @@
     active_cmds = readl(port_mmio + PORT_CMD_ISSUE);
     done_mask = ai->ports[p].reg_cmds ^ active_cmds;
-    DPRINTF(1,"[reg_cmds]: active_cmds=0x%08x  done_mask=0x%08x\n", active_cmds, done_mask);
+    DPRINTF(7,"[reg_cmds]: active_cmds=0x%08x  done_mask=0x%08x\n", active_cmds, done_mask);
   }
 
@@ -1681,5 +1686,5 @@
 {
   #ifdef DEBUG
-  DPRINTF(2,"ahci_get_geometry(%d.%d.%d)\n", iorb_unit_adapter(pIorb),
+  DPRINTF(7,"ahci_get_geometry(%d.%d.%d)\n", iorb_unit_adapter(pIorb),
           iorb_unit_port(pIorb), iorb_unit_device(pIorb));
   #endif
@@ -1694,5 +1699,5 @@
 {
   #ifdef DEBUG
-  DPRINTF(2,"ahci_unit_ready(%d.%d.%d)\n", iorb_unit_adapter(pIorb),
+  DPRINTF(7,"ahci_unit_ready(%d.%d.%d)\n", iorb_unit_adapter(pIorb),
           iorb_unit_port(pIorb), iorb_unit_device(pIorb));
   #endif
@@ -1707,5 +1712,5 @@
 {
   #ifdef DEBUG
-  DPRINTF(2,"ahci_read(%d.%d.%d, %d, %d)\n", iorb_unit_adapter(vIorb),
+  DPRINTF(7,"ahci_read(%d.%d.%d, %d, %d)\n", iorb_unit_adapter(vIorb),
           iorb_unit_port(pIorb), iorb_unit_device(pIorb),
           ((IORB_EXECUTEIO *) pIorb)->RBA,
@@ -1722,5 +1727,5 @@
 {
   #ifdef DEBUG
-  DPRINTF(2,"ahci_verify(%d.%d.%d, %d, %d)\n", iorb_unit_adapter(pIorb),
+  DPRINTF(7,"ahci_verify(%d.%d.%d, %d, %d)\n", iorb_unit_adapter(pIorb),
           iorb_unit_port(pIorb), iorb_unit_device(pIorb),
           ((IORB_EXECUTEIO *)pIorb)->RBA,
@@ -1737,5 +1742,5 @@
 {
   #ifdef DEBUG
-  DPRINTF(2,"ahci_write(%d.%d.%d, %d, %d)\n", iorb_unit_adapter(pIorb),
+  DPRINTF(7,"ahci_write(%d.%d.%d, %d, %d)\n", iorb_unit_adapter(pIorb),
           iorb_unit_port(pIorb), iorb_unit_device(pIorb),
           ((IORB_EXECUTEIO *)pIorb)->RBA,
Index: trunk/src/os2ahci/ata.c
===================================================================
--- trunk/src/os2ahci/ata.c	(revision 180)
+++ trunk/src/os2ahci/ata.c	(revision 181)
@@ -287,7 +287,7 @@
   cmd_hdr->options |= n << 16;
 
-  if (D32g_DbgLevel >= 2)
-  {
-    DPRINTF(2,"ATA command for %d.%d.%d, slot %d:\n", ad_no(ai), p, d, slot);
+  if (D32g_DbgLevel >= 7)
+  {
+    DPRINTF(0,"ATA command for %d.%d.%d, slot %d:\n", ad_no(ai), p, d, slot);
     dHexDump(0,cmd_hdr, offsetof(AHCI_CMD_HDR, reserved), "cmd_hdr: ");
     dHexDump(0,&ata_cmd, sizeof(ata_cmd), "ata_cmd: ");
@@ -834,5 +834,5 @@
   int rc;
 
-  DPRINTF(3,"ata_read_unaligned(%d.%d.%d, %d)\n", ad_no(ai), p, d, sector);
+  DPRINTF(7,"ata_read_unaligned(%d.%d.%d, %d)\n", ad_no(ai), p, d, sector);
 
   /* allocate transfer buffer */
@@ -883,5 +883,5 @@
 
   io->BlocksXferred += add_workspace(pIorb)->blocks;
-  DPRINTF(3,"ata_read_pp(): blocks transferred = %d\n", io->BlocksXferred);
+  DPRINTF(7,"ata_read_pp(): blocks transferred = %d\n", io->BlocksXferred);
 
   if (io->BlocksXferred >= io->BlockCount)
@@ -1020,5 +1020,5 @@
   int rc;
 
-  DPRINTF(3,"ata_write_unaligned(%d.%d.%d, %d)\n", ad_no(ai), p, d, sector);
+  DPRINTF(7,"ata_write_unaligned(%d.%d.%d, %d)\n", ad_no(ai), p, d, sector);
 
   /* allocate transfer buffer */
@@ -1068,5 +1068,5 @@
 
   io->BlocksXferred += add_workspace(pIorb)->blocks;
-  DPRINTF(3,"ata_write_pp(): blocks transferred = %d\n", io->BlocksXferred);
+  DPRINTF(7,"ata_write_pp(): blocks transferred = %d\n", io->BlocksXferred);
 
   if (io->BlocksXferred >= io->BlockCount)
Index: trunk/src/os2ahci/atapi.c
===================================================================
--- trunk/src/os2ahci/atapi.c	(revision 180)
+++ trunk/src/os2ahci/atapi.c	(revision 181)
@@ -31,5 +31,5 @@
 
 /* need this for the SCSI status block */
-#include <scsi.h>
+#include <Dev32scsi.h>
 
 /* -------------------------- macros and constants ------------------------- */
@@ -51,5 +51,5 @@
 int atapi_get_geometry(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
 {
-  DPRINTF(2,"atapi_get_geometry called\n");
+  DPRINTF(4,"atapi_get_geometry called\n");
   iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
   return(-1);
@@ -61,5 +61,5 @@
 int atapi_unit_ready(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
 {
-  DPRINTF(2,"atapi_unit_ready called\n");
+  DPRINTF(4,"atapi_unit_ready called\n");
   iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
   return(-1);
@@ -102,4 +102,6 @@
   SET_CDB_32(cdb.lba, io->RBA + io->BlocksXferred);
 
+  DPRINTF(4, "atapi_read\n");
+
   do {
     /* update sector count (might have been updated due to S/G limitations) */
@@ -173,4 +175,6 @@
   SET_CDB_32(cdb.trans_len, 1UL);
 
+  DPRINTF(4, "atapi_read_unaligned\n");
+
   /* allocate transfer buffer */
   if ((aws->buf = MemAlloc(io->BlockSize)) == NULL)
@@ -212,5 +216,5 @@
 int atapi_verify(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
 {
-  DPRINTF(3,"atapi_verify called\n");
+  DPRINTF(4,"atapi_verify called\n");
   iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
   return(-1);
@@ -222,5 +226,5 @@
 int atapi_write(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
 {
-  DPRINTF(3,"atapi_write called\n");
+  DPRINTF(4,"atapi_write called\n");
   iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
   return(-1);
@@ -244,6 +248,5 @@
   }
   /* AHCI requires 12 or 16 byte commands */
-  atapi_pad_cdb(Far16ToFlat(pt->pControllerCmd), pt->ControllerCmdLen,
-                (u8 *) cdb, (u16 *) &cdb_len);
+  atapi_pad_cdb(Far16ToFlat(pt->pControllerCmd), pt->ControllerCmdLen, cdb, &cdb_len);
 
   if (cdb[0] == 0x12 || cdb[0] == 0x5a)
@@ -293,5 +296,5 @@
   size_t sense_buf_len = ATAPI_SENSE_LEN;
 
-  DPRINTF(2,"atapi_req_sense\n");
+  DPRINTF(4,"atapi_req_sense\n");
 
   if ((pIorb->RequestControl & IORB_REQ_STATUSBLOCK) &&
@@ -386,5 +389,5 @@
     if (ssb->SenseData != NULL)
     {
-      memcpy(ssb->SenseData, psd, ssb->ReqSenseLen);
+      memcpy(Far16ToFlat(ssb->SenseData), psd, ssb->ReqSenseLen);
       ssb->Flags |= STATUS_SENSEDATA_VALID;
     }
Index: trunk/src/os2ahci/ctxhook.c
===================================================================
--- trunk/src/os2ahci/ctxhook.c	(revision 180)
+++ trunk/src/os2ahci/ctxhook.c	(revision 181)
@@ -102,5 +102,7 @@
   int p;
 
-  DPRINTF(2,"restart_ctxhook() started\n");
+  D32ThunkStackTo32();
+
+  DPRINTF(8,"restart_ctxhook() started\n");
   memset(&done_queue, 0x00, sizeof(done_queue));
 
@@ -129,9 +131,9 @@
         need_reset = 0;
 
-        DPRINTF(2,"port %d, TF_DATA: 0x%x\n", p, readl(port_mmio + PORT_TFDATA));
+        DPRINTF(8,"port %d, TF_DATA: 0x%x\n", p, readl(port_mmio + PORT_TFDATA));
 
         /* get "current command slot"; only valid if there are no NCQ cmds */
         ccs = (int) ((readl(port_mmio + PORT_CMD) >> 8) & 0x1f);
-        DPRINTF(3," PORT_CMD      = 0x%x\n", ccs);
+        DPRINTF(8," PORT_CMD      = 0x%x\n", ccs);
 
         for (vIorb = ai->ports[p].iorb_queue.vRoot; vIorb != NULL; vIorb = vNext)
@@ -283,5 +285,5 @@
   spin_unlock(drv_lock);
 
-  DPRINTF(2,"restart_ctxhook() completed\n");
+  DPRINTF(8,"restart_ctxhook() completed\n");
 
   /* Check whether we have to rearm ourselves because some adapters were busy
@@ -293,4 +295,5 @@
     KernArmHook(restart_ctxhook_h, 0, 0);
   }
+  KernThunkStackTo16();
 }
 
@@ -331,5 +334,7 @@
   int p;
 
-  DPRINTF(2,"reset_ctxhook() started\n");
+  D32ThunkStackTo32();
+
+  DPRINTF(8,"reset_ctxhook() started\n");
   memset(&done_queue, 0x00, sizeof(done_queue));
 
@@ -455,5 +460,5 @@
   spin_unlock(drv_lock);
 
-  DPRINTF(2,"reset_ctxhook() completed\n");
+  DPRINTF(8,"reset_ctxhook() completed\n");
 
   /* Check whether we have to rearm ourselves because some adapters were busy
@@ -465,4 +470,6 @@
     KernArmHook(reset_ctxhook_h, 0, 0);
   }
+
+  KernThunkStackTo16();
 }
 
@@ -478,5 +485,7 @@
   int i;
 
-  DPRINTF(2,"engine_ctxhook() started\n");
+  D32ThunkStackTo32();
+
+  DPRINTF(8,"engine_ctxhook() started\n");
   if (resume_sleep_flag)
   {
@@ -492,5 +501,5 @@
   spin_unlock(drv_lock);
 
-  DPRINTF(2,"engine_ctxhook() completed\n");
+  DPRINTF(8,"engine_ctxhook() completed\n");
 
   if (iorbs_sent != 0)
@@ -500,4 +509,6 @@
     KernArmHook(engine_ctxhook_h, 0, 0);
   }
+
+  KernThunkStackTo16();
 }
 
Index: trunk/src/os2ahci/ioctl.c
===================================================================
--- trunk/src/os2ahci/ioctl.c	(revision 180)
+++ trunk/src/os2ahci/ioctl.c	(revision 181)
@@ -31,5 +31,5 @@
 #include "ioctl.h"
 
-#include <scsi.h>
+#include <Dev32scsi.h>
 
 #pragma pack(1)
@@ -191,5 +191,5 @@
     ic->iorb.iorbh.pStatusBlock     = CastFar16ToULONG(MemFar16Adr(&ic->ssb));
     ic->iorb.iorbh.StatusBlockLen   = sizeof(ic->ssb);
-    ic->ssb.SenseData               = (SCSI_REQSENSE_DATA *) ic->sense;
+    ic->ssb.SenseData               = MemFar16Adr(ic->sense);
     ic->ssb.ReqSenseLen             = sizeof(ic->sense);
     ic->iorb.iorbh.RequestControl  |= IORB_REQ_STATUSBLOCK;
@@ -268,5 +268,5 @@
         (ic->ssb.Flags | STATUS_SENSEDATA_VALID))
     {
-      memcpy(sense_buf, ic->ssb.SenseData, min(ic->ssb.ReqSenseLen, req->sense_len));
+      memcpy(sense_buf, ic->sense, min(ic->ssb.ReqSenseLen, req->sense_len));
     }
 
Index: trunk/src/os2ahci/os2ahci.c
===================================================================
--- trunk/src/os2ahci/os2ahci.c	(revision 180)
+++ trunk/src/os2ahci/os2ahci.c	(revision 181)
@@ -606,5 +606,6 @@
      * link in 'vNext'.
      */
-    vNext = (pIorb->RequestControl | IORB_CHAIN) ? pIorb->pNxtIORB : NULL;
+    if (pIorb->RequestControl & IORB_CHAIN) vNext = pIorb->pNxtIORB;
+    else vNext = (IORBH FAR16DATA *)0;
 
     pIorb->Status = 0;
@@ -1317,5 +1318,5 @@
      */
     char *queue_type;
-    int min_debug = 1;
+    int min_debug = 7;
 
     if ((u32)queue >> 16 == (u32)&queue >> 16) /* DAZ this is bogus */
@@ -1323,5 +1324,5 @@
       /* this queue is on the stack */
       queue_type = "local";
-      min_debug = 2;
+      min_debug = 8;
 
     }
@@ -1334,5 +1335,5 @@
     {
       queue_type = "abort";
-      min_debug = 2;
+      min_debug = 8;
 
     }
@@ -1386,5 +1387,5 @@
   if (found)
   {
-    DPRINTF(3,"IORB %x removed (queue = %x)\n", vIorb, queue);
+    DPRINTF(8,"IORB %x removed (queue = %x)\n", vIorb, queue);
   }
   else
@@ -1458,5 +1459,5 @@
   pIorb->Status |= IORB_DONE;
 
-  DPRINTF(1,"IORB %x complete status=0x%04x error=0x%04x\n",
+  DPRINTF(7,"IORB %x complete status=0x%04x error=0x%04x\n",
           vIorb, pIorb->Status, pIorb->ErrorCode);
 
Index: trunk/src/os2ahci/os2ahci.h
===================================================================
--- trunk/src/os2ahci/os2ahci.h	(revision 180)
+++ trunk/src/os2ahci/os2ahci.h	(revision 181)
@@ -301,5 +301,5 @@
 
   u32           dma_buf_phys[AHCI_MAX_PORTS];          /* physical address of DMA scratch buffer */
-  u8         *dma_buf[AHCI_MAX_PORTS]; /* DMA scatch buffers */
+  u8           *dma_buf[AHCI_MAX_PORTS]; /* DMA scatch buffers */
 
   P_INFO        ports[AHCI_MAX_PORTS]; /* SATA ports on this adapter */
Index: trunk/src/os2ahci/trace.c
===================================================================
--- trunk/src/os2ahci/trace.c	(revision 180)
+++ trunk/src/os2ahci/trace.c	(revision 181)
@@ -78,5 +78,5 @@
               pi->devs[d].dev_info.Cylinders, pi->devs[d].dev_info.HeadsPerCylinder, pi->devs[d].dev_info.SectorsPerTrack,
               pi->devs[d].dev_info.TotalSectors/2048, pi->devs[d].dev_info.Method);
-          } else NTPRINTF(" Drive present but no information available.");
+          }
           NTPRINTF("\n");
         } /* if */
