Skip to contentSkip to conversation

QEMU ATAPI-SCSI bridge GSoC project

RFC PATCH Superseded Alexander Bezzubikov 1 participant 6 messages Aug 18, 2015

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover QEMU ATAPI-SCSI bridge GSoC project

This is my QEMU Google Summer of Code project.
Here I introduce new device - ATAPI-SCSI bridge.
Its purpose is to unify IDE ATAPI CD-ROM emulation
with SCSI CD-ROM emulation to reduce code duplication and squash bugs. It's purpose is simple - it just forwards ATAPI commands
to SCSI side for parsing and execution.

Example of using:

qemu-system-x86_64 \
        -drive if=none,file=<filename>,id=cdrom \
        -drive if=none,id=fake \
        -device ide-bridge,id=bridge,drive=fake \
        -device scsi-cd,drive=cdrom,bus=bridge.0 \
        -hda diskimage \
        -m 1024

Alexander Bezzubikov (5):
ide: ATAPI-SCSI bridge TypeInfo and init function created
ide: necessary checks corrected to treat ATAPI-SCSI bridge as CDROM ATAPI-SCSI bridge functions created an can be used by bridge
scsi: SCSIDiskReq declaration moved to header
ide: ATAPI-SCSI bridge transfer is treated as PIO

 hw/ide/Makefile.objs   |   2 +-
 hw/ide/atapi.c         |  20 ++++++++-
 hw/ide/bridge.c        | 114 +++++++++++++++++++++++++++++++++++++++++++++++++
 hw/ide/bridge.h        |  10 +++++
 hw/ide/core.c          |  28 +++++++-----
 hw/ide/internal.h      |   6 ++-
 hw/ide/qdev.c          |  43 ++++++++++++++++++-
 hw/scsi/scsi-disk.c    |  55 ++++++++++++++++++------
 include/hw/scsi/scsi.h |  16 +++++++
 9 files changed, 267 insertions(+), 27 deletions(-)
 create mode 100644 hw/ide/bridge.c
 create mode 100644 hw/ide/bridge.h
Signature · 1 line
2.1.4
#1 lore raw

RFC PATCH v2 1/5 ide: ATAPI-SCSI bridge TypeInfo and init function created

---
 hw/ide/bridge.h   |  9 +++++++++
 hw/ide/internal.h |  4 +++-
 hw/ide/qdev.c     | 41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100644 hw/ide/bridge.h
hw/ide/bridge.h new file +9 -0
new file mode 100644
index 0000000..dca5d73
@@ -0,0 +1,9 @@
+#ifndef HW_IDE_BRIDGE_H
+#define HW_IDE_BRIDGE_H
+
+#include "hw/ide/internal.h"
+
+void ide_bridge_start_transfer(SCSIRequest *req, uint32_t len);
+void ide_bridge_complete(SCSIRequest *req, uint32_t status, size_t resid);
+
+#endif
hw/ide/internal.h +3 -1
index 30fdcbc..79c85be 100644
@@ -12,6 +12,7 @@
 #include "sysemu/sysemu.h"
 #include "hw/block/block.h"
 #include "block/scsi.h"
+#include "hw/scsi/scsi.h"
 
 /* debug IDE devices */
 //#define DEBUG_IDE
@@ -317,7 +318,7 @@ typedef struct IDEDMAOps IDEDMAOps;
 #define SMART_DISABLE         0xd9
 #define SMART_STATUS          0xda
 
-typedef enum { IDE_HD, IDE_CD, IDE_CFATA } IDEDriveKind;
+typedef enum { IDE_HD, IDE_CD, IDE_CFATA, IDE_BRIDGE } IDEDriveKind;
 
 typedef void EndTransferFunc(IDEState *);
 
@@ -492,6 +493,7 @@ struct IDEDevice {
     char *serial;
     char *model;
     uint64_t wwn;
+    SCSIBus scsi_bus;
 };
 
 /* These are used for the error_status field of IDEBus */
hw/ide/qdev.c +41 -0

77 lines not on this page · raw message

Signature · 1 line
2.1.4
#2 lore raw

RFC PATCH v2 2/5 ide: necessary checks corrected to treat ATAPI-SCSI bridge as CDROM

In reply to Alexander Bezzubikov

hw/ide/qdev.c: corrected to treat bridge as CDROM
hw/ide/core.c: same corrections as in qdev.c
hw/ide/atapi.c: skip some CDROM checks because bridge has only fake drive

---
 hw/ide/atapi.c |  4 +++-
 hw/ide/core.c  | 24 ++++++++++++++----------
 hw/ide/qdev.c  |  2 +-
 3 files changed, 18 insertions(+), 12 deletions(-)
hw/ide/atapi.c +3 -1
index 79dd167..f6135e1 100644
@@ -1230,6 +1230,7 @@ void ide_atapi_cmd(IDEState *s)
      * states rely on this behavior.
      */
     if (!(atapi_cmd_table[s->io_buffer[0]].flags & ALLOW_UA) &&
+        s->drive_kind != IDE_BRIDGE &&
         !s->tray_open && blk_is_inserted(s->blk) && s->cdrom_changed) {
 
         if (s->cdrom_changed == 1) {
@@ -1245,7 +1246,8 @@ void ide_atapi_cmd(IDEState *s)
 
     /* Report a Not Ready condition if appropriate for the command */
     if ((atapi_cmd_table[s->io_buffer[0]].flags & CHECK_READY) &&
-        (!media_present(s) || !blk_is_inserted(s->blk)))
+        (s->drive_kind != IDE_BRIDGE &&
+        (!media_present(s) || !blk_is_inserted(s->blk))))
     {
         ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
         return;
hw/ide/core.c +14 -10
index 50449ca..96824ab 100644
@@ -348,7 +348,7 @@ static void ide_set_signature(IDEState *s)
     /* put signature */
     s->nsector = 1;
     s->sector = 1;
-    if (s->drive_kind == IDE_CD) {
+    if (s->drive_kind == IDE_CD || s->drive_kind == IDE_BRIDGE) {
         s->lcyl = 0x14;
         s->hcyl = 0xeb;
     } else if (s->blk) {
@@ -1144,7 +1144,7 @@ static bool cmd_data_set_management(IDEState *s, uint8_t cmd)
 
 static bool cmd_identify(IDEState *s, uint8_t cmd)
 {
-    if (s->blk && s->drive_kind != IDE_CD) {
+    if (s->blk && s->drive_kind != IDE_CD && s->drive_kind != IDE_BRIDGE) {
         if (s->drive_kind != IDE_CFATA) {
             ide_identify(s);
         } else {
@@ -1155,7 +1155,7 @@ static bool cmd_identify(IDEState *s, uint8_t cmd)
         ide_set_irq(s->bus);
         return false;
     } else {
-        if (s->drive_kind == IDE_CD) {
+        if (s->drive_kind == IDE_CD || s->drive_kind == IDE_BRIDGE) {
             ide_set_signature(s);
         }
         ide_abort_command(s);
@@ -1232,7 +1232,7 @@ static bool cmd_read_pio(IDEState *s, uint8_t cmd)
 {
     bool lba48 = (cmd == WIN_READ_EXT);
 
-    if (s->drive_kind == IDE_CD) {
+    if (s->drive_kind == IDE_CD || s->drive_kind == IDE_BRIDGE) {
         ide_set_signature(s); /* odd, but ATA4 8.27.5.2 requires it */
         ide_abort_command(s);
         return true;
@@ -1426,7 +1426,7 @@ static bool cmd_exec_dev_diagnostic(IDEState *s, uint8_t cmd)
 {
     ide_set_signature(s);
 
-    if (s->drive_kind == IDE_CD) {
+    if (s->drive_kind == IDE_CD || s->drive_kind == IDE_BRIDGE) {
         s->status = 0; /* ATAPI spec (v6) section 9.10 defines packet
                         * devices to return a clear status register
                         * with READY_STAT *not* set. */
@@ -1731,7 +1731,7 @@ abort_cmd:
 }
 
 #define HD_OK (1u << IDE_HD)
-#define CD_OK (1u << IDE_CD)
+#define CD_OK ((1u << IDE_CD) | (1u << IDE_BRIDGE))
 #define CFA_OK (1u << IDE_CFATA)
 #define HD_CFA_OK (HD_OK | CFA_OK)
 #define ALL_OK (HD_OK | CD_OK | CFA_OK)
@@ -1978,10 +1978,11 @@ void ide_cmd_write(void *opaque, uint32_t addr, uint32_t val)
         /* high to low */
         for(i = 0;i < 2; i++) {
             s = &bus->ifs[i];
-            if (s->drive_kind == IDE_CD)
+            if (s->drive_kind == IDE_CD || s->drive_kind == IDE_BRIDGE) {
                 s->status = 0x00; /* NOTE: READY is _not_ set */
-            else
+            } else {
                 s->status = READY_STAT | SEEK_STAT;
+            }
             ide_set_signature(s);
         }
     }
@@ -2234,7 +2235,7 @@ static void ide_resize_cb(void *opaque)
         ide_cfata_identify_size(s);
     } else {
         /* IDE_CD uses a different set of callbacks entirely. */
-        assert(s->drive_kind != IDE_CD);
+        assert(s->drive_kind != IDE_CD && s->drive_kind != IDE_BRIDGE);
         ide_identify_size(s);
     }
 }
@@ -2274,7 +2275,7 @@ int ide_init_drive(IDEState *s, BlockBackend *blk, IDEDriveKind kind,
     s->smart_autosave = 1;
     s->smart_errors = 0;
     s->smart_selftest_count = 0;
-    if (kind == IDE_CD) {
+    if (kind == IDE_CD || kind == IDE_BRIDGE) {
         blk_set_dev_ops(blk, &ide_cd_block_ops, s);
         blk_set_guest_block_size(blk, 2048);
     } else {
@@ -2301,6 +2302,9 @@ int ide_init_drive(IDEState *s, BlockBackend *blk, IDEDriveKind kind,
         case IDE_CD:
             strcpy(s->drive_model_str, "QEMU DVD-ROM");
             break;
+        case IDE_BRIDGE:
+            strcpy(s->drive_model_str, "QEMU VIRTUAL ATAPI-SCSI BRIDGE");
+            break;
         case IDE_CFATA:
             strcpy(s->drive_model_str, "QEMU MICRODRIVE");
             break;
hw/ide/qdev.c +1 -1
index 3bf3401..2ed0c39 100644
@@ -182,7 +182,7 @@ static int ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind)
     }
 
     blkconf_serial(&dev->conf, &dev->serial);
-    if (kind != IDE_CD) {
+    if (kind != IDE_CD && kind != IDE_BRIDGE) {
         blkconf_geometry(&dev->conf, &dev->chs_trans, 65536, 16, 255, &err);
         if (err) {
             error_report_err(err);
Signature · 1 line
2.1.4
#3 lore raw

RFC PATCH v2 3/5 ATAPI-SCSI bridge functions created an can be used by bridge

In reply to Alexander Bezzubikov

ide: bridge functions created
ide: Makefile corrected due to bridge creation
scsi: added function to enable bridge send SCSI requests
ide: bridge can now forward requests to SCSI
ide: bridge functions assigned to SCSIBusInfo

---
 hw/ide/Makefile.objs   |   2 +-
 hw/ide/atapi.c         |  16 +++++++
 hw/ide/bridge.c        | 114 +++++++++++++++++++++++++++++++++++++++++++++++++
 hw/ide/bridge.h        |   1 +
 hw/ide/internal.h      |   2 +
 hw/ide/qdev.c          |   4 +-
 hw/scsi/scsi-disk.c    |  43 +++++++++++++++++++
 include/hw/scsi/scsi.h |   3 ++
 8 files changed, 182 insertions(+), 3 deletions(-)
 create mode 100644 hw/ide/bridge.c
hw/ide/Makefile.objs +1 -1
index 729e9bd..f54f275 100644
@@ -1,4 +1,4 @@
-common-obj-$(CONFIG_IDE_CORE) += core.o atapi.o
+common-obj-$(CONFIG_IDE_CORE) += core.o atapi.o bridge.o
 common-obj-$(CONFIG_IDE_QDEV) += qdev.o
 common-obj-$(CONFIG_IDE_PCI) += pci.o
 common-obj-$(CONFIG_IDE_ISA) += isa.o
hw/ide/atapi.c +16 -0
index f6135e1..3eb56e2 100644
@@ -1253,6 +1253,22 @@ void ide_atapi_cmd(IDEState *s)
         return;
     }
 
+    if (s->drive_kind == IDE_BRIDGE) {
+        IDEDevice *dev = s->bus->master;
+        SCSIDevice *scsi_dev = scsi_device_find(&dev->scsi_bus, 0, 0, 0);
+        s->cur_req = scsi_new_request_from_bridge(scsi_dev, 0, 0, buf, NULL);
+
+        /* Necessary to prevent ide from reading while data isn't ready */
+        if (buf[0] == READ_10) {
+            s->status |= BUSY_STAT;
+        }
+
+        if (scsi_req_enqueue(s->cur_req)) {
+            scsi_req_continue(s->cur_req);
+        }
+        return;
+    }
+
     /* Execute the command */
     if (atapi_cmd_table[s->io_buffer[0]].handler) {
         atapi_cmd_table[s->io_buffer[0]].handler(s, buf);
hw/ide/bridge.c new file +114 -0

114 lines not on this page · raw message

hw/ide/bridge.h +1 -0
index dca5d73..59f2f25 100644
@@ -5,5 +5,6 @@
 
 void ide_bridge_start_transfer(SCSIRequest *req, uint32_t len);
 void ide_bridge_complete(SCSIRequest *req, uint32_t status, size_t resid);
+void ide_bridge_do_transfer(IDEState *s);
 
 #endif
hw/ide/internal.h +2 -0
index 79c85be..fd385bc 100644
@@ -429,6 +429,8 @@ struct IDEState {
     uint8_t *smart_selftest_data;
     /* AHCI */
     int ncq_queues;
+    /* ATAPI-SCSI bridge */
+    SCSIRequest *cur_req;
 };
 
 struct IDEDMAOps {
hw/ide/qdev.c +2 -2
index 2ed0c39..e18d222 100644
@@ -150,8 +150,8 @@ static const struct SCSIBusInfo atapi_scsi_info = {
     .max_target = 0,
     .max_lun = 0,
 
-    .transfer_data = NULL,
-    .complete = NULL,
+    .transfer_data = ide_bridge_start_transfer,
+    .complete = ide_bridge_complete,
     .cancel = NULL
 };
 
hw/scsi/scsi-disk.c +43 -0

49 lines not on this page · raw message

include/hw/scsi/scsi.h +3 -0
index cdaf0f8..881ed23 100644
@@ -249,6 +249,9 @@ SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
                             uint32_t tag, uint32_t lun, void *hba_private);
 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
                           uint8_t *buf, void *hba_private);
+SCSIRequest *scsi_new_request_from_bridge(SCSIDevice *d, uint32_t tag,
+                                          uint32_t lun, uint8_t *buf,
+                                          void *hba_private);
 int32_t scsi_req_enqueue(SCSIRequest *req);
 void scsi_req_free(SCSIRequest *req);
 SCSIRequest *scsi_req_ref(SCSIRequest *req);
Signature · 1 line
2.1.4
#4 lore raw

RFC PATCH v2 4/5 scsi: SCSIDiskReq declaration moved to header

In reply to Alexander Bezzubikov
---
 hw/scsi/scsi-disk.c    | 12 ------------
 include/hw/scsi/scsi.h | 13 +++++++++++++
 2 files changed, 13 insertions(+), 12 deletions(-)
hw/scsi/scsi-disk.c +0 -12
index f67d816..9d5f0a4 100644
@@ -53,18 +53,6 @@ do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
 
 typedef struct SCSIDiskState SCSIDiskState;
 
-typedef struct SCSIDiskReq {
-    SCSIRequest req;
-    /* Both sector and sector_count are in terms of qemu 512 byte blocks.  */
-    uint64_t sector;
-    uint32_t sector_count;
-    uint32_t buflen;
-    bool started;
-    struct iovec iov;
-    QEMUIOVector qiov;
-    BlockAcctCookie acct;
-} SCSIDiskReq;
-
 #define SCSI_DISK_F_REMOVABLE             0
 #define SCSI_DISK_F_DPOFUA                1
 #define SCSI_DISK_F_NO_REMOVABLE_DEVOPS   2
include/hw/scsi/scsi.h +13 -0
index 881ed23..de0546e 100644
@@ -6,6 +6,7 @@
 #include "hw/block/block.h"
 #include "sysemu/sysemu.h"
 #include "qemu/notify.h"
+#include "block/accounting.h"
 
 #define MAX_SCSI_DEVS	255
 
@@ -120,6 +121,18 @@ extern const VMStateDescription vmstate_scsi_device;
     .offset     = vmstate_offset_value(_state, _field, SCSIDevice),  \
 }
 
+typedef struct SCSIDiskReq {
+    SCSIRequest req;
+    /* Both sector and sector_count are in terms of qemu 512 byte blocks.  */
+    uint64_t sector;
+    uint32_t sector_count;
+    uint32_t buflen;
+    bool started;
+    struct iovec iov;
+    QEMUIOVector qiov;
+    BlockAcctCookie acct;
+} SCSIDiskReq;
+
 /* cdrom.c */
 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
Signature · 1 line
2.1.4
#5 lore raw

RFC PATCH v2 5/5 ide: ATAPI-SCSI bridge transfer is treated as PIO

In reply to Alexander Bezzubikov

This patch is necessary because ATAPI-SCSI bridge transfer uses ide_transfer_start/stop and ide_data_read function check if PIO transfer is running, so bridge function should be added to this check

---
 hw/ide/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/ide/core.c +3 -1
index 96824ab..4988db6 100644
@@ -34,6 +34,7 @@
 #include "sysemu/block-backend.h"
 
 #include <hw/ide/internal.h>
+#include "hw/ide/bridge.h"
 
 /* These values were based on a Seagate ST3500418AS but have been modified
    to make more sense in QEMU */
@@ -2002,7 +2003,8 @@ static bool ide_is_pio_out(IDEState *s)
     } else if (s->end_transfer_func == ide_sector_read ||
                s->end_transfer_func == ide_transfer_stop ||
                s->end_transfer_func == ide_atapi_cmd_reply_end ||
-               s->end_transfer_func == ide_dummy_transfer_stop) {
+               s->end_transfer_func == ide_dummy_transfer_stop ||
+               s->end_transfer_func == ide_bridge_do_transfer) {
         return true;
     }
 
Signature · 1 line
2.1.4