]> xenbits.xen.org Git - xenclient/ioemu-pq.git/commitdiff
Tidy up atapi-pt write code, remove accidental aardvark.
authorAndrew McNeil <andrewmcn@bob.uk.xensource.com>
Fri, 4 Sep 2009 13:11:00 +0000 (14:11 +0100)
committerAndrew McNeil <andrewmcn@bob.uk.xensource.com>
Fri, 4 Sep 2009 13:11:00 +0000 (14:11 +0100)
master/atapi-pt-write

index 809736561c898124c5662ce0035f09a2d06ef66f..3f298fdee525c55199bb223a596e0d36d43e714e 100644 (file)
@@ -1,8 +1,8 @@
 diff --git a/hw/atapi-pt.c b/hw/atapi-pt.c
-index 413cc47..2441096 100644
+index 413cc47..d5c5f0b 100644
 --- a/hw/atapi-pt.c
 +++ b/hw/atapi-pt.c
-@@ -497,18 +497,157 @@ static void ide_atapi_pt_error(IDEState *s)
+@@ -497,124 +497,106 @@ static void ide_atapi_pt_error(IDEState *s)
      ide_set_irq(s);
  }
  
@@ -12,64 +12,68 @@ index 413cc47..2441096 100644
 -static int no_finish_count = 0;
 -
 -static void ide_atapi_pt_sg_io_finished(IDEState *s)
-+static void ide_atapi_pt_sg_io_finish(IDEState *s)
++static void ide_atapi_pt_do_sg_io(IDEState *s)
  {
 +    struct sg_io_v4 *cmd = &s->atapi_pt.cmd;
-+    BDRVRawState *raw_state = s->bs->opaque;
+     BDRVRawState *raw_state = s->bs->opaque;
+-    int read_bytes;
+-    int i;
 +    int r;
-+
+-    ++finish_count;
+-    DEBUG_PRINTF("****  finish   (%p, u=%p, raw=%p, fd=%d, %d-%d(%d))\n",
+-                 s, s->atapi_pt.cmd.usr_ptr,
+-                 raw_state, raw_state->fd, cmd_count,
+-                 finish_count + no_finish_count, no_finish_count);
 +    s->atapi_pt.sense.error_code = 0;
 +    s->atapi_pt.sense.sense_key = 0;
 +    s->atapi_pt.sense.asc = 0;
 +    s->atapi_pt.sense.ascq = 0;
-+
-+    if(cmd->dout_xfer_len > 0)
-+    {
-+        int max, i;
-+
-+        max = cmd->dout_xfer_len;
-+        if(max > 0x100)
-+            max = 0x100;
-+
-+        DEBUG_PRINTF("Dout: 0x");
-+        for(i = 0; i < max; ++i)
-+            DEBUG_PRINTF("%02x ", ((char *)cmd->dout_xferp)[i]);
-+        DEBUG_PRINTF("\n");
-+    }
-+
-+
+-    DEBUG_PRINTF("Preread:  0x");
+-    for(i = 0; i < sizeof(s->atapi_pt.cmd); ++i)
+-        DEBUG_PRINTF("%02x", ((unsigned char *)&s->atapi_pt.cmd)[i]);
+-    DEBUG_PRINTF("\n");
 +    /* Send command and wait for reply, SG_IO ioctl*/
 +    r = ioctl(raw_state->fd, 0x2285, cmd);
-+
-+    if(s->atapi_pt.request[0] == GPCMD_MODE_SENSE_10)
-+    {
-+        DEBUG_PRINTF("Mode sense 10, %d bytes\n", cmd->din_xfer_len);
-+        {
-+            int i;
-+            DEBUG_PRINTF("0x");
-+            for(i = 0; i < cmd->din_xfer_len; ++i)
-+                DEBUG_PRINTF("%02x ", s->io_buffer[i]);
-+            DEBUG_PRINTF("\n");
-+        }
-+    }
-+
-+
+-#if 1
+-    read_bytes = read(raw_state->fd, &s->atapi_pt.cmd, sizeof (s->atapi_pt.cmd));
 +    if(s->atapi_pt.request[0] == GPCMD_GET_EVENT_STATUS_NOTIFICATION)
 +    {
 +        struct stat file_stat;
-+
+-    DEBUG_PRINTF("Postread: 0x");
+-    for(i = 0; i < sizeof(s->atapi_pt.cmd); ++i)
+-        DEBUG_PRINTF("%02x", ((unsigned char *)&s->atapi_pt.cmd)[i]);
+-    DEBUG_PRINTF("\n");
+-#else
+-    DEBUG_PRINTF("Postread: 0x");
+-    for(i = 0; i < sizeof (s->atapi_pt.cmd); ++i) {
+-        read(raw_state->fd, &((unsigned char *)&s->atapi_pt.cmd)[i], 1);
+-        DEBUG_PRINTF("%02x", ((unsigned char *)&s->atapi_pt.cmd)[i]);
+-    }
+-    DEBUG_PRINTF("\n");
+-    read_bytes = i;
+-#endif
 +        if(s->io_buffer[2] == 4 && s->io_buffer[4] == 2)
 +        {
 +            /* This is a "new media" message, tell any other VMs */
-+            DEBUG_PRINTF("[ATAPI] new media\n");
++            DEBUG_PRINTF("[ATAPI] new media detected\n");
 +            system("touch " IDE_ATAPI_PT_NEW_CD_FILE);
-+
+-    //    DEBUG_PRINTF("finish read\n");
 +            if(stat(IDE_ATAPI_PT_NEW_CD_FILE, &file_stat) != 0)
 +                DEBUG_PRINTF("Error writing to new CD file\n");
 +            else
 +                s->atapi_pt.new_cd_time = file_stat.st_ctime;
 +        }
-+
+-    if (read_bytes != sizeof (s->atapi_pt.cmd))
+-    {
+-        ide_atapi_pt_error(s);
+-        s->atapi_pt.cmd_sent = NULL;
+-        return;
 +        if(s->io_buffer[2] == 4 && s->io_buffer[4] == 3)
 +        {
 +            /* This is a "media removed" message, tell any other VMs */
@@ -88,14 +92,11 @@ index 413cc47..2441096 100644
 +        {
 +            /* This is a no activity message we can hijack if we need to */
 +
-+            if((stat(IDE_ATAPI_PT_NEW_CD_FILE, &file_stat) == 0 &&
-+               s->atapi_pt.new_cd_time < file_stat.st_ctime) ||
-+               s->atapi_pt.aardvark == 1)
++            if(stat(IDE_ATAPI_PT_NEW_CD_FILE, &file_stat) == 0)
 +            {
 +                /* There's been a new media message that we haven't seen yet */
 +                DEBUG_PRINTF("[ATAPI] new media message spotted\n");
 +                s->atapi_pt.new_cd_time = file_stat.st_ctime;
-+                s->atapi_pt.aardvark = 0;
 +
 +                s->io_buffer[2] = 4;
 +                s->io_buffer[4] = 2;
@@ -117,76 +118,96 @@ index 413cc47..2441096 100644
 +                s->io_buffer[7] = 0;
 +            }
 +        }
-+    }
-+
+     }
+-    if (s->atapi_pt.cmd.driver_status ||
+-        s->atapi_pt.cmd.transport_status ||
+-        s->atapi_pt.cmd.device_status)
+-    {
+-        DEBUG_PRINTF("[\e[1;31mERROR\e[m]\n"
+-                     "\tsense_key: 0x%02x (\e[0;35m%s\e[m)\n"
+-                     "\terror: 0x%02x\n"
+-                     "\tasc: 0x%02x, 0x%x (\e[0;35m%s\e[m)\n"
+-                     "\terrno: %d (%s)\n"
+-                     "\tdriver: %d, transport: %d, device: %d\n",
 +    if(r || cmd->driver_status || cmd->transport_status ||
 +       cmd->device_status) {
-+        /*
-+        DEBUG_PRINTF("[\e[1;31mERROR\e[m]\n"
-+                     "\tcommand 0x%02x (%s)\n"
-+                     "\terrno: %d (%s)\n"
-+                     "\tsense: 0x%02x,%02x,%02x (%s)\n"
-+                     "\tdriver: %d, transport: %d, device: %d\n",
-+                     command, atapi_cmd_to_str(command),
-+                     errno,
-+                     strerror(errno) ? : "(null)",
-+                     s->atapi_pt.sense.sense_key,
-+                     s->atapi_pt.sense.asc,
-+                     s->atapi_pt.sense.ascq,
-+                     atapi_sense_to_str(s->atapi_pt.sense.sense_key,
-+                                        s->atapi_pt.sense.asc,
-+                                        s->atapi_pt.sense.ascq),
-+                     cmd->driver_status,
-+                     cmd->transport_status,
-+                     cmd->device_status);
-+        */
 +        DEBUG_PRINTF("[\e[1;31mERROR\e[m] (%s)  sense: 0x%02x,%02x,%02x (%s)\n",
 +                     atapi_cmd_to_str(s->atapi_pt.request[0]),
-+                     s->atapi_pt.sense.sense_key,
-+                     s->atapi_pt.sense.asc,
-+                     s->atapi_pt.sense.ascq,
-+                     atapi_sense_to_str(s->atapi_pt.sense.sense_key,
-+                                        s->atapi_pt.sense.asc,
+                      s->atapi_pt.sense.sense_key,
+-                     sense_key_texts[s->atapi_pt.sense.sense_key],
+-                     s->atapi_pt.sense.error_code,
+                      s->atapi_pt.sense.asc,
+                      s->atapi_pt.sense.ascq,
+                      atapi_sense_to_str(s->atapi_pt.sense.sense_key,
+                                         s->atapi_pt.sense.asc,
+-                                        s->atapi_pt.sense.ascq),
+-                     errno,
+-                     strerror(errno) ? : "(null)",
+-                     s->atapi_pt.cmd.driver_status,
+-                     s->atapi_pt.cmd.transport_status,
+-                     s->atapi_pt.cmd.device_status);
 +                                        s->atapi_pt.sense.ascq));
-+        ide_atapi_pt_error(s);
-+        return;
-+    }
-+
+         ide_atapi_pt_error(s);
+-        s->atapi_pt.cmd_sent = NULL;
+-        return;
+-    }
+-    if(s->atapi_pt.cmd_sent == NULL) {
+-        static int null_count = 0;
+-        ++null_count;
+-        DEBUG_PRINTF("\e[1;31m%s called with cmd_sent = NULL %d time%c\e[m\n",
+-                     __FUNCTION__, null_count, (null_count == 1) ? ' ' : 's');
+-        ide_atapi_cmd_ok(s);
+-        s->atapi_pt.cmd_sent = NULL;
+         return;
+     }
+-    //    DEBUG_PRINTF("finish sent\n");
+-    s->atapi_pt.cmd_sent(s);
+-    s->atapi_pt.cmd_sent = NULL;
+-    DEBUG_PRINTF("****  finished (%p)\n", s);
+-}
+-
+-#define ATAPI_PT_SEND_PACKET                                            \
+-  do {                                                                  \
+-        BDRVRawState *raw_state = s->bs->opaque;                        \
+-        DEBUG_PRINTF("[ATAPI:%d] sending command: 0x%02x (\e[0;32m%s\e[m)\n", \
+-                     raw_state->fd, s->atapi_pt.request[0],             \
+-                     atapi_cmd_to_str(s->atapi_pt.request[0]));         \
+-        memset(&s->atapi_pt.sense, 0, sizeof (s->atapi_pt.sense));      \
+-        int wrote = write(raw_state->fd, &s->atapi_pt.cmd,              \
+-                          sizeof (s->atapi_pt.cmd));                    \
+-        if (wrote != sizeof (s->atapi_pt.cmd))                          \
+-            ide_atapi_pt_error(s);                                      \
+-    } while (0)
+-static void ide_atapi_pt_read_finish(IDEState *s)
+-{
+-    assert(s->atapi_pt.cmd.dout_xfer_len > 0);
+-    s->atapi_pt.cmd.dout_xferp = (__u64)s->io_buffer;
+-    s->atapi_pt.cmd_sent = ide_atapi_cmd_ok;
+-    ATAPI_PT_SEND_PACKET;
 +    s->atapi_pt.cmd_sent(s);
-+}
-+
-+
-+
-+
-+#if 0
-     BDRVRawState *raw_state = s->bs->opaque;
-     int read_bytes;
-     int i;
--    ++finish_count;
-     DEBUG_PRINTF("****  finish   (%p, u=%p, raw=%p, fd=%d, %d-%d(%d))\n",
-                  s, s->atapi_pt.cmd.usr_ptr,
-                  raw_state, raw_state->fd, cmd_count,
-@@ -607,11 +746,12 @@ static void ide_atapi_pt_read_finish(IDEState *s)
-     s->atapi_pt.cmd_sent = ide_atapi_cmd_ok;
-     ATAPI_PT_SEND_PACKET;
  }
-+#endif
  
- static void ide_atapi_pt_read_pio_end(IDEState *s)
+-static void ide_atapi_pt_read_pio_end(IDEState *s)
++static void ide_atapi_pt_dout_fetch_pio_done(IDEState *s)
  {
      ide_transfer_stop(s);
 -    ide_atapi_pt_read_finish(s);
-+    ide_atapi_pt_sg_io_finish(s);
++    ide_atapi_pt_do_sg_io(s);
  }
  
- static void ide_atapi_pt_read_dma_cb(void *opaque, int ret)
-@@ -626,11 +766,9 @@ static void ide_atapi_pt_read_dma_cb(void *opaque, int ret)
+-static void ide_atapi_pt_read_dma_cb(void *opaque, int ret)
++static void ide_atapi_pt_dout_fetch_dma_done(void *opaque, int ret)
+ {
+     BMDMAState *bm = opaque;
+     IDEState *s = bm->ide_if;
+@@ -626,11 +608,9 @@ static void ide_atapi_pt_read_dma_cb(void *opaque, int ret)
      }
  
      i = dma_buf_rw(bm, 0);
 -    ide_atapi_pt_read_finish(s);
-+    ide_atapi_pt_sg_io_finish(s);
++    ide_atapi_pt_do_sg_io(s);
  }
 -#endif
  
@@ -194,7 +215,21 @@ index 413cc47..2441096 100644
  static void ide_atapi_pt_wcmd(IDEState *s)
  {
      if (s->atapi_dma)
-@@ -657,7 +795,6 @@ static void ide_atapi_pt_wcmd(IDEState *s)
+@@ -638,7 +618,7 @@ static void ide_atapi_pt_wcmd(IDEState *s)
+         /* DMA */
+         s->io_buffer_index = 0;
+         s->io_buffer_size = s->atapi_pt.cmd.dout_xfer_len;
+-        ide_dma_start(s, ide_atapi_pt_read_dma_cb);
++        ide_dma_start(s, ide_atapi_pt_dout_fetch_dma_done);
+         return;
+     }
+@@ -653,11 +633,10 @@ static void ide_atapi_pt_wcmd(IDEState *s)
+         ~ATAPI_INT_REASON_IO &
+         ~ATAPI_INT_REASON_CD;
+     ide_transfer_start(s, s->io_buffer, s->atapi_pt.cmd.dout_xfer_len,
+-                       ide_atapi_pt_read_pio_end);
++                       ide_atapi_pt_dout_fetch_pio_done);
      ide_set_irq(s);
      return;
  }
@@ -202,25 +237,17 @@ index 413cc47..2441096 100644
  
  static void ide_atapi_pt_read_format_capacities_sent(IDEState *s)
  {
-@@ -817,9 +954,14 @@ static int ide_atapi_pt_read_cd_block_size(const uint8_t *io_buffer)
+@@ -817,9 +796,6 @@ static int ide_atapi_pt_read_cd_block_size(const uint8_t *io_buffer)
  static void ide_atapi_pt_cmd(IDEState *s)
  {
      struct sg_io_v4 *cmd = &s->atapi_pt.cmd;
 -    int r;
 -    int command;
 -    BDRVRawState *raw_state = s->bs->opaque;
-+
-+    {
-+        static int foo = 0;
-+
-+        if(foo == 0)
-+            printf("Welcome to pt 2.1\n");
-+        foo = 1;
-+    }
  
      memset(cmd, 0, sizeof (*cmd));
      memcpy(s->atapi_pt.request, s->io_buffer, ATAPI_PACKET_SIZE);
-@@ -838,11 +980,13 @@ static void ide_atapi_pt_cmd(IDEState *s)
+@@ -838,11 +814,13 @@ static void ide_atapi_pt_cmd(IDEState *s)
      s->atapi_pt.reply_size_len    = 0;
  
      cmd->din_xferp = (__u64)s->io_buffer;
@@ -237,45 +264,46 @@ index 413cc47..2441096 100644
      {
          /*******************/
          /* SIMPLE COMMANDS */
-@@ -881,24 +1025,24 @@ static void ide_atapi_pt_cmd(IDEState *s)
+@@ -880,25 +858,19 @@ static void ide_atapi_pt_cmd(IDEState *s)
+     case GPCMD_WRITE_10:
      case GPCMD_WRITE_AND_VERIFY_10:
          cmd->dout_xfer_len = ube16_to_cpu(s->io_buffer + 7) * CD_FRAMESIZE;
+-
 -        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(command),
-+        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(s->atapi_pt.request[0]),
-                      cmd->dout_xfer_len);
+-                     cmd->dout_xfer_len);
+-
          if (cmd->dout_xfer_len == 0)
              goto simple_cmd;
 -        break;
++
 +        ide_atapi_pt_wcmd(s);
 +        return;
  
      case GPCMD_WRITE_12:
          cmd->dout_xfer_len = ube32_to_cpu(s->io_buffer + 6);
+-
 -        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(command),
-+        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(s->atapi_pt.request[0]),
-                      cmd->dout_xfer_len);
+-                     cmd->dout_xfer_len);
+-
          if (cmd->dout_xfer_len == 0)
              goto simple_cmd;
 -        //        ide_atapi_pt_wcmd(s);
 -        //        return;
 -        break;
++
 +        ide_atapi_pt_wcmd(s);
 +        return;
  
      case GPCMD_WRITE_BUFFER:
      {
-@@ -926,25 +1070,23 @@ static void ide_atapi_pt_cmd(IDEState *s)
+@@ -925,109 +897,68 @@ static void ide_atapi_pt_cmd(IDEState *s)
+             goto illegal_request;
          }
  
+-
 -        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(command),
-+        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(s->atapi_pt.request[0]),
-                      cmd->dout_xfer_len);
+-                     cmd->dout_xfer_len);
+-
 -        //        ide_atapi_pt_wcmd(s);
 -        //        return;
 -        break;
@@ -285,97 +313,97 @@ index 413cc47..2441096 100644
  
      case GPCMD_SEND_CUE_SHEET:
          cmd->dout_xfer_len = ube24_to_cpu(s->io_buffer + 6);
+-
 -        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(command),
-+        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(s->atapi_pt.request[0]),
-                      cmd->dout_xfer_len);
+-                     cmd->dout_xfer_len);
+-
          if (cmd->dout_xfer_len == 0)
              goto simple_cmd;
 -        //        ide_atapi_pt_wcmd(s);
 -        //        return;
 -        break;
++
 +        ide_atapi_pt_wcmd(s);
 +        return;
  
      case GPCMD_MODE_SELECT_10:
          cmd->dout_xfer_len = ube16_to_cpu(s->io_buffer + 7);
-@@ -956,65 +1098,63 @@ static void ide_atapi_pt_cmd(IDEState *s)
-             DEBUG_PRINTF("0x");
-             for(i = 0; i < 12; ++i)
-                 DEBUG_PRINTF("%02x ", s->io_buffer[i]);
+         CHECK_SAME_VALUE(s->lcyl | (s->hcyl << 8), cmd->dout_xfer_len);
+-
+-        DEBUG_PRINTF("Mode select 10  %d bytes\n", cmd->dout_xfer_len);
+-        {
+-            int i;
+-            DEBUG_PRINTF("0x");
+-            for(i = 0; i < 12; ++i)
+-                DEBUG_PRINTF("%02x ", s->io_buffer[i]);
 -            DEBUG_PRINTF("\n0x");
-+            DEBUG_PRINTF("\n+12   0x");
-             for(i = 0; i < cmd->dout_xfer_len; ++i)
-                 DEBUG_PRINTF("%02x ", s->io_buffer[i + 12]);
-+            DEBUG_PRINTF("\n+512  0x");
-+            for(i = 0; i < cmd->dout_xfer_len; ++i)
-+                DEBUG_PRINTF("%02x ", s->io_buffer[i + 512]);
-             DEBUG_PRINTF("\n");
-         }
+-            for(i = 0; i < cmd->dout_xfer_len; ++i)
+-                DEBUG_PRINTF("%02x ", s->io_buffer[i + 12]);
+-            DEBUG_PRINTF("\n");
+-        }
+-
          if (cmd->dout_xfer_len == 0)
              goto simple_cmd;
 -        //        ide_atapi_pt_wcmd(s);
 -        //        return;
 -        break;
++
 +        ide_atapi_pt_wcmd(s);
 +        return;
  
      case GPCMD_SEND_KEY:
      case GPCMD_SEND_EVENT:
          cmd->dout_xfer_len = ube16_to_cpu(s->io_buffer + 8);
+-
 -        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(command),
-+        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(s->atapi_pt.request[0]),
-                      cmd->dout_xfer_len);
+-                     cmd->dout_xfer_len);
+-
          if (cmd->dout_xfer_len == 0)
              goto simple_cmd;
 -        //        ide_atapi_pt_wcmd(s);
 -        //        return;
 -        break;
++
 +        ide_atapi_pt_wcmd(s);
 +        return;
  
      case GPCMD_SEND_OPC:
          cmd->dout_xfer_len = ube16_to_cpu(s->io_buffer + 7) << 3;
          CHECK_SAME_VALUE(s->lcyl | (s->hcyl << 8), cmd->dout_xfer_len);
+-
 -        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(command),
-+        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(s->atapi_pt.request[0]),
-                      cmd->dout_xfer_len);
+-                     cmd->dout_xfer_len);
+-
          if (cmd->dout_xfer_len == 0)
              goto simple_cmd;
 -        //        ide_atapi_pt_wcmd(s);
 -        //        return;
 -        break;
++
 +        ide_atapi_pt_wcmd(s);
 +        return;
  
      case GPCMD_SET_STREAMING:
          cmd->dout_xfer_len = ube16_to_cpu(s->io_buffer + 9);
+-
 -        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(command),
-+        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(s->atapi_pt.request[0]),
-                      cmd->dout_xfer_len);
+-                     cmd->dout_xfer_len);
+-
          if (cmd->dout_xfer_len == 0)
              goto simple_cmd;
 -        //        ide_atapi_pt_wcmd(s);
 -        //        return;
 -        break;
++
 +        ide_atapi_pt_wcmd(s);
 +        return;
  
      case GPCMD_FORMAT_UNIT:
          cmd->dout_xfer_len = 12;
+-
 -        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(command),
-+        DEBUG_PRINTF("Write (%s) %d bytes\n", atapi_cmd_to_str(s->atapi_pt.request[0]),
-                      cmd->dout_xfer_len);
+-                     cmd->dout_xfer_len);
+-
 -        //        ide_atapi_pt_wcmd(s);
 -        //        return;
 -        break;
@@ -384,96 +412,346 @@ index 413cc47..2441096 100644
  
          /*****************/
          /* READ COMMANDS */
-@@ -1047,6 +1187,13 @@ static void ide_atapi_pt_cmd(IDEState *s)
-         //               max_size, s->atapi_pt.sense.add_sense_len,
-         //             sizeof (s->atapi_pt.sense));
+         /*****************/
+     case GPCMD_INQUIRY:
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         cmd->din_xfer_len = s->io_buffer[4];
+         s->atapi_pt.reply_size_init = 5;
+         s->atapi_pt.reply_size_offset = 4;
+         s->atapi_pt.reply_size_len = 1;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+-        //        ATAPI_PT_SEND_PACKET;
+         break;
+     case GPCMD_REQUEST_SENSE:
+@@ -1042,10 +973,6 @@ static void ide_atapi_pt_cmd(IDEState *s)
+         int max_size = s->io_buffer[4];
+         int size = 8 + s->atapi_pt.sense.add_sense_len;
+-
+-        //        printf("max_size: %d, add_sense_len: %d, sizeof: %lu\n",
+-        //               max_size, s->atapi_pt.sense.add_sense_len,
+-        //             sizeof (s->atapi_pt.sense));
          memcpy(s->io_buffer, &s->atapi_pt.sense, sizeof (s->atapi_pt.sense));
-+        {
-+            int i;
-+            DEBUG_PRINTF("Sense data: 0x");
-+            for(i = 0; i < sizeof(s->atapi_pt.sense); ++i)
-+                DEBUG_PRINTF("%02x ", s->io_buffer[i]);
-+            DEBUG_PRINTF("\n");
-+        }
          ide_atapi_cmd_reply(s, size, max_size);
          return;
+@@ -1053,57 +980,40 @@ static void ide_atapi_pt_cmd(IDEState *s)
+     case GPCMD_READ_DVD_STRUCTURE:
+         cmd->din_xfer_len = ube16_to_cpu(s->io_buffer + 8);
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_len = 2;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     case GPCMD_READ_HEADER:
+         cmd->din_xfer_len = ube16_to_cpu(s->io_buffer + 7);
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_init = cmd->din_xfer_len;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     case GPCMD_MECHANISM_STATUS:
+         cmd->din_xfer_len = ube16_to_cpu(s->io_buffer + 8);
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_offset = 6;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     case GPCMD_REPORT_KEY:
+         cmd->din_xfer_len = ube16_to_cpu(s->io_buffer + 8);
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_len = 2;
+         s->atapi_pt.reply_size_init = 2;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     case GPCMD_READ_BUFFER_CAPACITY:
+         cmd->din_xfer_len = ube16_to_cpu(s->io_buffer + 7);
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_len = 2;
+         s->atapi_pt.reply_size_init = 2;
+-        //        return;
+         break;
+     case GPCMD_GET_PERFORMANCE:
+         cmd->din_xfer_len = 8 + 8 * ube16_to_cpu(s->io_buffer + 8);
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_len = 4;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     case GPCMD_READ_10:
+@@ -1123,17 +1033,12 @@ static void ide_atapi_pt_cmd(IDEState *s)
+         default: assert(0);
+         }
+         cmd->din_xfer_len = nbblocks * blocksize;
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_init = cmd->din_xfer_len;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
      }
-@@ -1181,6 +1328,13 @@ static void ide_atapi_pt_cmd(IDEState *s)
-         //s->atapi_pt.reply_size_init = cmd->din_xfer_len;
-         //        ATAPI_PT_SEND_PACKET;
-         //        return;
-+        {
-+            int i;
-+            DEBUG_PRINTF("Cmd: 0x");
-+            for(i = 0; i < ATAPI_PACKET_SIZE; ++i)
-+                DEBUG_PRINTF("%02x ", s->io_buffer[i]);
-+            DEBUG_PRINTF("\n");
-+        }
+     case GPCMD_READ_BUFFER:
+-        // TODO check this one is correct
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         cmd->din_xfer_len = ube24_to_cpu(s->io_buffer + 6);
+         switch (s->io_buffer[1] & 0x7)
+@@ -1157,30 +1062,21 @@ static void ide_atapi_pt_cmd(IDEState *s)
+             goto illegal_request;
+         }
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     case GPCMD_READ_CDVD_CAPACITY:
+         cmd->din_xfer_len = 8;
+         CHECK_SAME_VALUE(s->lcyl | (s->hcyl << 8), cmd->din_xfer_len);
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_init = cmd->din_xfer_len;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     case GPCMD_MODE_SENSE_10:
+         cmd->din_xfer_len = ube16_to_cpu(s->io_buffer + 7);
+         CHECK_SAME_VALUE(s->lcyl | (s->hcyl << 8), cmd->din_xfer_len);
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_len = 2;
+         s->atapi_pt.reply_size_init = 2;
+-        //s->atapi_pt.reply_size_init = cmd->din_xfer_len;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
          break;
  
      case GPCMD_GET_EVENT_STATUS_NOTIFICATION:
-@@ -1318,7 +1472,9 @@ static void ide_atapi_pt_cmd(IDEState *s)
-         return;
+@@ -1188,22 +1084,16 @@ static void ide_atapi_pt_cmd(IDEState *s)
+     case GPCMD_READ_TOC_PMA_ATIP:
+     case GPCMD_READ_TRACK_RZONE_INFO:
+         cmd->din_xfer_len = ube16_to_cpu(s->io_buffer + 7);
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_len = 2;
+         s->atapi_pt.reply_size_init = 2;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     case GPCMD_READ_SUBCHANNEL:
+         cmd->din_xfer_len = ube16_to_cpu(s->io_buffer + 7);
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_len = 2;
+         s->atapi_pt.reply_size_offset = 2;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     case GPCMD_READ_CD:
+@@ -1214,11 +1104,8 @@ static void ide_atapi_pt_cmd(IDEState *s)
+         int block_size = ide_atapi_pt_read_cd_block_size(s->io_buffer);
+         cmd->din_xfer_len = block_count * block_size;
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_init = cmd->din_xfer_len;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
      }
  
-+    ide_atapi_pt_sg_io_finish(s);
+@@ -1233,11 +1120,8 @@ static void ide_atapi_pt_cmd(IDEState *s)
+         int block_size = ide_atapi_pt_read_cd_block_size(s->io_buffer);
  
-+#if 0
-     s->atapi_pt.sense.error_code = 0;
-     s->atapi_pt.sense.sense_key = 0;
-     s->atapi_pt.sense.asc = 0;
-@@ -1327,6 +1483,19 @@ static void ide_atapi_pt_cmd(IDEState *s)
-     /* Send command and wait for reply, SG_IO ioctl*/
-     r = ioctl(raw_state->fd, 0x2285, cmd);
+         cmd->din_xfer_len = block_count * block_size;
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_init = cmd->din_xfer_len;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     }
  
-+    if(command == GPCMD_MODE_SENSE_10)
-+    {
-+        DEBUG_PRINTF("Mode sense 10, %d bytes\n", cmd->din_xfer_len);
-+        {
-+            int i;
-+            DEBUG_PRINTF("0x");
-+            for(i = 0; i < cmd->din_xfer_len; ++i)
-+                DEBUG_PRINTF("%02x ", s->io_buffer[i]);
-+            DEBUG_PRINTF("\n");
-+        }
-+    }
-+
-+
-     if(command == GPCMD_GET_EVENT_STATUS_NOTIFICATION)
+@@ -1245,11 +1129,8 @@ static void ide_atapi_pt_cmd(IDEState *s)
      {
-         struct stat file_stat;
-@@ -1361,12 +1530,14 @@ static void ide_atapi_pt_cmd(IDEState *s)
-         {
-             /* This is a no activity message we can hijack if we need to */
+         int block_count = ube16_to_cpu(s->io_buffer + 7);
+         cmd->din_xfer_len = block_count * CD_FRAMESIZE;
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_init = cmd->din_xfer_len;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     }
  
--            if(stat(IDE_ATAPI_PT_NEW_CD_FILE, &file_stat) == 0 &&
--               s->atapi_pt.new_cd_time < file_stat.st_ctime)
-+            if((stat(IDE_ATAPI_PT_NEW_CD_FILE, &file_stat) == 0 &&
-+               s->atapi_pt.new_cd_time < file_stat.st_ctime) ||
-+               s->atapi_pt.aardvark == 1)
-             {
-                 /* There's been a new media message that we haven't seen yet */
-                 DEBUG_PRINTF("[ATAPI] new media message spotted\n");
-                 s->atapi_pt.new_cd_time = file_stat.st_ctime;
-+                s->atapi_pt.aardvark = 0;
-                 s->io_buffer[2] = 4;
-                 s->io_buffer[4] = 2;
-@@ -1424,4 +1595,5 @@ static void ide_atapi_pt_cmd(IDEState *s)
+@@ -1261,40 +1142,28 @@ static void ide_atapi_pt_cmd(IDEState *s)
+             MSF_TO_FRAMES(s->io_buffer[6], s->io_buffer[7], s->io_buffer[8]);
+         int block_count = ending_frame - starting_frame;
+         cmd->din_xfer_len = block_count * CD_FRAMESIZE;
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_init = cmd->din_xfer_len;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
      }
  
-     s->atapi_pt.cmd_sent(s);
-+#endif
- }
-diff --git a/hw/ide.c b/hw/ide.c
-index 6c1d60a..0a5a764 100644
---- a/hw/ide.c
-+++ b/hw/ide.c
-@@ -435,6 +435,7 @@ typedef struct ATAPIPassThroughState
-     time_t               new_cd_time;
-     time_t               eject_time;
-+    uint8_t              aardvark;
- } ATAPIPassThroughState;
- #endif /* __linux__ */
+     case GPCMD_READ_FORMAT_CAPACITIES:
+         cmd->din_xfer_len = ube16_to_cpu(s->io_buffer + 7);
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_read_format_capacities_sent;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     case GPCMD_GET_CONFIGURATION:
+         cmd->din_xfer_len = ube16_to_cpu(s->io_buffer + 7);
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_init = 4;
+         s->atapi_pt.reply_size_len = 4;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     case GPCMD_SEND_DVD_STRUCTURE:
+         cmd->din_xfer_len = ube16_to_cpu(s->io_buffer + 8);
+-        //        cmd->din_xferp = (__u64)s->io_buffer;
+         s->atapi_pt.cmd_sent = ide_atapi_pt_standard_reply;
+         s->atapi_pt.reply_size_init = 2;
+         s->atapi_pt.reply_size_len = 2;
+-        //        ATAPI_PT_SEND_PACKET;
+-        //        return;
+         break;
+     case 0x01: // GPMODE_R_W_ERROR_PAGE ?
+@@ -1318,110 +1187,5 @@ static void ide_atapi_pt_cmd(IDEState *s)
+         return;
+     }
  
+-
+-    s->atapi_pt.sense.error_code = 0;
+-    s->atapi_pt.sense.sense_key = 0;
+-    s->atapi_pt.sense.asc = 0;
+-    s->atapi_pt.sense.ascq = 0;
+-
+-    /* Send command and wait for reply, SG_IO ioctl*/
+-    r = ioctl(raw_state->fd, 0x2285, cmd);
+-
+-    if(command == GPCMD_GET_EVENT_STATUS_NOTIFICATION)
+-    {
+-        struct stat file_stat;
+-
+-        if(s->io_buffer[2] == 4 && s->io_buffer[4] == 2)
+-        {
+-            /* This is a "new media" message, tell any other VMs */
+-            DEBUG_PRINTF("[ATAPI] new media\n");
+-            system("touch " IDE_ATAPI_PT_NEW_CD_FILE);
+-
+-            if(stat(IDE_ATAPI_PT_NEW_CD_FILE, &file_stat) != 0)
+-                DEBUG_PRINTF("Error writing to new CD file\n");
+-            else
+-                s->atapi_pt.new_cd_time = file_stat.st_ctime;
+-        }
+-
+-        if(s->io_buffer[2] == 4 && s->io_buffer[4] == 3)
+-        {
+-            /* This is a "media removed" message, tell any other VMs */
+-            DEBUG_PRINTF("[ATAPI] media removed\n");
+-            system("touch " IDE_ATAPI_PT_EJECT_CD_FILE);
+-
+-            if(stat(IDE_ATAPI_PT_EJECT_CD_FILE, &file_stat) != 0)
+-                DEBUG_PRINTF("Error writing to eject CD file\n");
+-            else
+-                s->atapi_pt.eject_time = file_stat.st_ctime;
+-        }
+-
+-        if((s->io_buffer[2] == 4 && s->io_buffer[4] == 0 && s->io_buffer[5] == 2) ||
+-           (s->io_buffer[4] == 0 && s->io_buffer[5] == 0 &&
+-            s->io_buffer[6] == 0 && s->io_buffer[7] == 0))
+-        {
+-            /* This is a no activity message we can hijack if we need to */
+-
+-            if(stat(IDE_ATAPI_PT_NEW_CD_FILE, &file_stat) == 0 &&
+-               s->atapi_pt.new_cd_time < file_stat.st_ctime)
+-            {
+-                /* There's been a new media message that we haven't seen yet */
+-                DEBUG_PRINTF("[ATAPI] new media message spotted\n");
+-                s->atapi_pt.new_cd_time = file_stat.st_ctime;
+-
+-                s->io_buffer[2] = 4;
+-                s->io_buffer[4] = 2;
+-                s->io_buffer[5] = 2;
+-                s->io_buffer[6] = 0;
+-                s->io_buffer[7] = 0;
+-            }
+-            else if(stat(IDE_ATAPI_PT_EJECT_CD_FILE, &file_stat) == 0 &&
+-               s->atapi_pt.eject_time < file_stat.st_ctime)
+-            {
+-                /* There's been an eject message that we haven't seen yet */
+-                DEBUG_PRINTF("[ATAPI] media removed message spotted\n");
+-                s->atapi_pt.eject_time = file_stat.st_ctime;
+-
+-                s->io_buffer[2] = 4;
+-                s->io_buffer[4] = 3;
+-                s->io_buffer[5] = 1;
+-                s->io_buffer[6] = 0;
+-                s->io_buffer[7] = 0;
+-            }
+-        }
+-    }
+-
+-    if(r || cmd->driver_status || cmd->transport_status ||
+-       cmd->device_status) {
+-        /*
+-        DEBUG_PRINTF("[\e[1;31mERROR\e[m]\n"
+-                     "\tcommand 0x%02x (%s)\n"
+-                     "\terrno: %d (%s)\n"
+-                     "\tsense: 0x%02x,%02x,%02x (%s)\n"
+-                     "\tdriver: %d, transport: %d, device: %d\n",
+-                     command, atapi_cmd_to_str(command),
+-                     errno,
+-                     strerror(errno) ? : "(null)",
+-                     s->atapi_pt.sense.sense_key,
+-                     s->atapi_pt.sense.asc,
+-                     s->atapi_pt.sense.ascq,
+-                     atapi_sense_to_str(s->atapi_pt.sense.sense_key,
+-                                        s->atapi_pt.sense.asc,
+-                                        s->atapi_pt.sense.ascq),
+-                     cmd->driver_status,
+-                     cmd->transport_status,
+-                     cmd->device_status);
+-        */
+-        DEBUG_PRINTF("[\e[1;31mERROR\e[m] (%s)  sense: 0x%02x,%02x,%02x (%s)\n",
+-                     atapi_cmd_to_str(command),
+-                     s->atapi_pt.sense.sense_key,
+-                     s->atapi_pt.sense.asc,
+-                     s->atapi_pt.sense.ascq,
+-                     atapi_sense_to_str(s->atapi_pt.sense.sense_key,
+-                                        s->atapi_pt.sense.asc,
+-                                        s->atapi_pt.sense.ascq));
+-        ide_atapi_pt_error(s);
+-        return;
+-    }
+-
+-    s->atapi_pt.cmd_sent(s);
++    ide_atapi_pt_do_sg_io(s);
+ }