Skip to contentSkip to conversation

ATAPI pass through

PATCH In review Bique Alexandre 5 participants 10 messages Jun 30, 2009 – Jul 1, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ATAPI pass through

Hi,

This patch implements ATAPI pass through on Linux by using bsg device.

What works:
- read CD
- burn CD with TAO
- blank CD
- play audio CD
- play DVD movie

What doesn't work:
- burn CD with SAO

DVD blanking and burning needs some testing.

Things which can be done better:
- I exported the data structure BDRVRawState from raw-posix.c in raw-posix.h,
because I use the file descriptor of the bsg device to poll, write and read. Maybe there is a better solution, but I didn't find what I was looking for in block.h.
- file splitting: everything is in ide.c, even my atapi pass through code in
the beginning. I didn't want to put everything in this file so I moved my code in atapi-pt.c but I did #include "atapi-pt.c" in ide.c so it's still in the spirit of what has been done before: all in ide.c. I'd like to split correctly this code but I will have to make some static functions public, and if I move atapi-pt out from ide.c why not atapi out from ide.c and maybe some other stuff ? That's why I did nothing yet. What would you prefer ?

Please, can you comment on this patch ? Thank you very much.

Regards,

Signature · 1 line
Alexandre Bique
Makefile +2 -1
index 2a4b3f3..df5b5c4 100644
@@ -300,8 +300,9 @@ endif
 test speed: all
 	$(MAKE) -C tests $@
 
+.PHONY: TAGS
 TAGS:
-	etags *.[ch] tests/*.[ch] block/*.[ch] hw/*.[ch]
+	find "$(SRC_PATH)" -name '*.[hc]' -print0 | xargs -0 etags
 
 cscope:
 	rm -f ./cscope.*
block.c +1 -0
index 3fe9317..962b640 100644
@@ -883,6 +883,7 @@ void bdrv_set_type_hint(BlockDriverState *bs, int type)
 {
     bs->type = type;
     bs->removable = ((type == BDRV_TYPE_CDROM ||
+                      type == BDRV_TYPE_CDROM_PT ||
                       type == BDRV_TYPE_FLOPPY));
 }
 
block.h +5 -3
index b595772..b362218 100644
@@ -102,9 +102,11 @@ void bdrv_flush_all(void);
 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
 	int *pnum);
 
-#define BDRV_TYPE_HD     0
-#define BDRV_TYPE_CDROM  1
-#define BDRV_TYPE_FLOPPY 2
+#define BDRV_TYPE_HD            0
+#define BDRV_TYPE_CDROM         1
+#define BDRV_TYPE_FLOPPY        2
+#define BDRV_TYPE_CDROM_PT      3
+
 #define BIOS_ATA_TRANSLATION_AUTO   0
 #define BIOS_ATA_TRANSLATION_NONE   1
 #define BIOS_ATA_TRANSLATION_LBA    2
block/raw-posix.c +1 -14
index 8b1e67c..39a83f3 100644
@@ -99,20 +99,7 @@
    reopen it to see if the disk has been changed */
 #define FD_OPEN_TIMEOUT 1000
 
-typedef struct BDRVRawState {
-    int fd;
-    int type;
-    unsigned int lseek_err_cnt;
-    int open_flags;
-#if defined(__linux__)
-    /* linux floppy specific */
-    int64_t fd_open_time;
-    int64_t fd_error_time;
-    int fd_got_error;
-    int fd_media_changed;
-#endif
-    uint8_t* aligned_buf;
-} BDRVRawState;
+#include "raw-posix.h"
 
 static int posix_aio_init(void);
 
block/raw-posix.h new file +19 -0
new file mode 100644
index 0000000..0d3bf85
@@ -0,0 +1,19 @@
+#ifndef BLOCK_RAW_POSIX_H
+# define BLOCK_RAW_POSIX_H
+
+typedef struct BDRVRawState {
+    int fd;
+    int type;
+    unsigned int lseek_err_cnt;
+    int open_flags;
+#if defined(__linux__)
+    /* linux floppy specific */
+    int64_t fd_open_time;
+    int64_t fd_error_time;
+    int fd_got_error;
+    int fd_media_changed;
+#endif
+    uint8_t* aligned_buf;
+} BDRVRawState;
+
+#endif /* !BLOCK_RAW_POSIX_H */
configure +37 -0
index eb9d73a..9a28082 100755
@@ -191,6 +191,7 @@ nptl="yes"
 mixemu="no"
 bluez="yes"
 kvm="no"
+atapi_pt="no"
 kerneldir=""
 aix="no"
 blobs="yes"
@@ -323,6 +324,19 @@ AIX)
 aix="yes"
 make="gmake"
 ;;
+Linux)
+atapi_pt="yes"
+audio_drv_list="oss"
+audio_possible_drivers="oss alsa sdl esd pa"
+linux="yes"
+linux_user="yes"
+usb="linux"
+kvm="yes"
+if [ "$cpu" = "i386" -o "$cpu" = "x86_64" ] ; then
+    kqemu="yes"
+    audio_possible_drivers="$audio_possible_drivers fmod"
+fi
+;;
 *)
 audio_drv_list="oss"
 audio_possible_drivers="oss alsa sdl esd pa"
@@ -501,6 +515,10 @@ for opt do
   ;;
   --disable-docs) build_docs="no"
   ;;
+  --enable-atapi-pt) atapi_pt="yes"
+  ;;
+  --disable-atapi-pt) atapi_pt="no"
+  ;;
   *) echo "ERROR: unknown option $opt"; show_help="yes"
   ;;
   esac
@@ -646,6 +664,8 @@ echo "  --disable-vde            disable support for vde network"
 echo "  --disable-pthread        disable pthread support"
 echo "  --disable-aio            disable AIO support"
 echo "  --enable-io-thread       enable IO thread"
+echo "  --enable-atapi-pt        enable atapi device pass through"
+echo "  --disable-atapi-pt       disable atapi device pass through"
 echo "  --disable-blobs          disable installing provided firmware blobs"
 echo "  --kerneldir=PATH         look for kernel includes in PATH"
 echo ""
@@ -1453,6 +1473,7 @@ echo "Install blobs     $blobs"
 echo -e "KVM support       $kvm"
 echo "fdt support       $fdt"
 echo "preadv support    $preadv"
+echo "atapi-pt support  $atapi_pt"
 
 if test $sdl_too_old = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -1823,6 +1844,22 @@ bsd)
 ;;
 esac
 
+# ATAPI pass through
+if [ "$atapi_pt" = "yes" ] ; then
+  if [ "$targetos" != "Linux" ] ; then
+    echo "error: you need a Linux target OS to use ATAPI pass through" >&2
+    exit 1
+  fi
+  cat <<EOF >>$config_h
+#define CONFIG_ATAPI_PT 1
+#ifndef __linux__
+#error "You can't enable ATAPI pass through if you're not using linux."
+#endif /* __linux__ */
+EOF
+else
+  echo "#define CONFIG_ATAPI_PT 0" >>$config_h
+fi
+
 # Determine what linker flags to use to force archive inclusion
 check_linker_flags()
 {
hw/atapi-pt.c new file +968 -0

968 lines not on this page · raw message

+}

hw/ide.c +108 -20

273 lines not on this page · raw message

qemu-options.hx +8 -0
index a94f9d3..d362954 100644
@@ -88,6 +88,14 @@ Use @var{file} as CD-ROM image (you cannot use @option{-hdc} and
 using @file{/dev/cdrom} as filename (@pxref{host_drives}).
 ETEXI
 
+DEF("cdrom-pt", HAS_ARG, QEMU_OPTION_cdrom_pt,
+    "-cdrom-pt file     use cdrom pass through\n")
+STEXI
+@item -cdrom-pt @var{device}
+Use @var{device} as CD-ROM device. It uses bsg on linux so the device
+should be something like @file{/dev/bsg/1:0:0:0}.
+ETEXI
+
 DEF("drive", HAS_ARG, QEMU_OPTION_drive,
     "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
     "       [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
vl.c +10 -1
index 7b7489c..178125a 100644
@@ -2071,6 +2071,7 @@ static int bt_parse(const char *opt)
 
 #define HD_ALIAS "index=%d,media=disk"
 #define CDROM_ALIAS "index=2,media=cdrom"
+#define CDROM_PT_ALIAS "index=2,media=cdrompt"
 #define FD_ALIAS "index=%d,if=floppy"
 #define PFLASH_ALIAS "if=pflash"
 #define MTD_ALIAS "if=mtd"
@@ -2207,7 +2208,7 @@ int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
     char serial[21];
     const char *mediastr = "";
     BlockInterfaceType type;
-    enum { MEDIA_DISK, MEDIA_CDROM } media;
+    enum { MEDIA_DISK, MEDIA_CDROM, MEDIA_CDROM_PT } media;
     int bus_id, unit_id;
     int cyls, heads, secs, translation;
     BlockDriverState *bdrv;
@@ -2366,6 +2367,8 @@ int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
 	        return -1;
             }
 	    media = MEDIA_CDROM;
+        } else if (!strcmp(buf, "cdrompt")) {
+	    media = MEDIA_CDROM_PT;
 	} else {
 	    fprintf(stderr, "qemu: '%s' invalid media\n", str);
 	    return -1;
@@ -2531,6 +2534,9 @@ int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
 	case MEDIA_CDROM:
             bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
 	    break;
+        case MEDIA_CDROM_PT:
+            bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM_PT);
+            break;
 	}
         break;
     case IF_SD:
@@ -5266,6 +5272,9 @@ int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_cdrom:
                 drive_add(optarg, CDROM_ALIAS);
                 break;
+            case QEMU_OPTION_cdrom_pt:
+                drive_add(optarg, CDROM_PT_ALIAS);
+                break;
             case QEMU_OPTION_boot:
                 boot_devices = optarg;
                 /* We just do some generic consistency checks */
#1 lore raw
Quoted 11 lines · Bique Alexandre

Hi,

This patch implements ATAPI pass through on Linux by using bsg device.

What works:
- read CD
- burn CD with TAO
- blank CD
- play audio CD
- play DVD movie

Quoted 4 lines

What doesn't work:
- burn CD with SAO

DVD blanking and burning needs some testing.

First of all, I would suggest splitting this patch up. You seem to have included some pieces of unrelated changes like adding comments, changing constants (Are these fixes? Definitely need a good patch description!), moving code (block-raw-posix.h) and probably some more.

Quoted 5 lines

Things which can be done better:
- I exported the data structure BDRVRawState from raw-posix.c in raw-posix.h,
because I use the file descriptor of the bsg device to poll, write and read. Maybe there is a better solution, but I didn't find what I was looking for in block.h.

Don't do that. If there is something missing in the block interface, we need to add it there and properly wire things up. Just poking in block driver internals is clearly the wrong approach.

Quoted 1 line

From what I see the only thing you are doing with it is read/write. Why

can't you use the normal block layer functions for it?

Quoted 7 lines

- file splitting: everything is in ide.c, even my atapi pass through code in
the beginning. I didn't want to put everything in this file so I moved my code in atapi-pt.c but I did #include "atapi-pt.c" in ide.c so it's still in the spirit of what has been done before: all in ide.c. I'd like to split correctly this code but I will have to make some static functions public, and if I move atapi-pt out from ide.c why not atapi out from ide.c and maybe some other stuff ? That's why I did nothing yet. What would you prefer ?

I guess proper splitting wouldn't hurt anyway. ide.c looks way too large.

Quoted 2 lines

Please, can you comment on this patch ? Thank you very much.

Another detail I saw is ATAPI_PT_SEND_PACKET. I mean, isn't this a bit too big for a macro? No chance to make this nicer?

I haven't really looked at the functionality. There are most probably people around who can do a better review of it.

Kevin

#2 lore raw
Quoted 15 lines · On Tuesday 30 June 2009 17:26:31 Kevin Wolf wrote:
Quoted 14 lines · Bique Alexandre

Hi,

This patch implements ATAPI pass through on Linux by using bsg device.

What works:
- read CD
- burn CD with TAO
- blank CD
- play audio CD
- play DVD movie

What doesn't work:
- burn CD with SAO

Quoted 6 lines
Quoted 1 line

DVD blanking and burning needs some testing.

First of all, I would suggest splitting this patch up. You seem to have included some pieces of unrelated changes like adding comments, changing constants (Are these fixes? Definitely need a good patch description!), moving code (block-raw-posix.h) and probably some more.

Ok, I will.

Quoted 6 lines
Quoted 5 lines

Things which can be done better:
- I exported the data structure BDRVRawState from raw-posix.c in
raw-posix.h, because I use the file descriptor of the bsg device to poll, write and read. Maybe there is a better solution, but I didn't find what I was looking for in block.h.

Quoted 6 lines

Don't do that. If there is something missing in the block interface, we need to add it there and properly wire things up. Just poking in block driver internals is clearly the wrong approach.

From what I see the only thing you are doing with it is read/write. Why can't you use the normal block layer functions for it?

I also use poll (qemu_set_fd_handler). bsg works like that:
- write your command
=> I am not writing a sector, but just a command which is <512B. So I won't
use bdrv_write. I may use bdrv_pwrite, but why should I seek before writing ? And where ?
- poll for command completion.
=> I didn't find any polling function from block.h. I can't use bdrv_aio_read
because I am not reading a sector.
- read the result
=> The same as for writing.

Maybe block is not the right semantic for the bsg device and I should open it manually instead of using QEMU's block driver?

Quoted 9 lines
Quoted 8 lines

- file splitting: everything is in ide.c, even my atapi pass through
code in the beginning. I didn't want to put everything in this file so I moved my code in atapi-pt.c but I did #include "atapi-pt.c" in ide.c so it's still in the spirit of what has been done before: all in ide.c. I'd like to split correctly this code but I will have to make some static functions public, and if I move atapi-pt out from ide.c why not atapi out from ide.c and maybe some other stuff ? That's why I did nothing yet. What would you prefer ?

Quoted 6 lines

I guess proper splitting wouldn't hurt anyway. ide.c looks way too large.

Quoted 1 line

Please, can you comment on this patch ? Thank you very much.

Another detail I saw is ATAPI_PT_SEND_PACKET. I mean, isn't this a bit too big for a macro? No chance to make this nicer?

It was a macro for historical reason, but it's a static function now.

Quoted 4 lines

I haven't really looked at the functionality. There are most probably people around who can do a better review of it.

Kevin

Thanks Kevin.

Signature · 1 line
Alexandre Bique
#3 lore raw
In reply to Kevin Wolf
Quoted 11 lines · Kevin Wolf
Quoted 8 lines · Bique Alexandre

- file splitting: everything is in ide.c, even my atapi pass through code in
the beginning. I didn't want to put everything in this file so I moved my code in atapi-pt.c but I did #include "atapi-pt.c" in ide.c so it's still in the spirit of what has been done before: all in ide.c. I'd like to split correctly this code but I will have to make some static functions public, and if I move atapi-pt out from ide.c why not atapi out from ide.c and maybe some other stuff ? That's why I did nothing yet. What would you prefer ?

Quoted 1 line

I guess proper splitting wouldn't hurt anyway. ide.c looks way too large.

I don't really know what became of the SCSI layer since I haven't looked at it for quite some time now. But last time I checked, we had a cdrom emulation in scsi-disk.c _and_ in ide.c.

I personally see no reason for that. Why can't we just have an atapi emulation layer in ide.c and use whatever scsi backends we have? That way, we'd only need to implement cd-rom and scsi passthrough once and have it available either via SCSI or via ATAPI.

Theoretically it should even be possible to do scsi-disk or scsi-tape over ATAPI ;-).

I'd really love to see all this cleaned up a bit, but having atapi passthrough is probably better than nothing already.

Alex

#4 lore raw
Quoted 7 lines · Alexander Graf

But last time I checked, we had a cdrom emulation in scsi-disk.c _and_ in ide.c.

I personally see no reason for that. Why can't we just have an atapi emulation layer in ide.c and use whatever scsi backends we have? That way, we'd only need to implement cd-rom and scsi passthrough once and have it available either via SCSI or via ATAPI.

That would be best. Note that we don't implement SCSI passthrough in scsi-disk.c but rather in scsi-generic.c. But being able to attach both of them to ata.c using an ATAPI shim would indeed be best.

#5 lore raw
In reply to Alexander Graf
Quoted 4 lines

I don't really know what became of the SCSI layer since I haven't looked at it for quite some time now.
But last time I checked, we had a cdrom emulation in scsi-disk.c _and_ in ide.c.

IIRC ATAPI and SCSI are not the same. They provide very similar functionality, however the actual commands are different.

While it is possible to connect SATA drives to a SAS HBA, I believe this is done by tunneling IDE/ATAPI commands, rather then using native SCSI commands.

The common bits of the cdrom emulation are already broken out into cdrom.c.

USB Mass Storage can (in theory) use both ATAPI and SCSI devices. Currently we only implement SCSI.

Paul

#6 lore raw
Quoted 2 lines · Paul Brook

IIRC ATAPI and SCSI are not the same. They provide very similar functionality, however the actual commands are different.

ATAPI has somewhat more limited command sets, mostly because it does not support the traditional 6 byte commands and doesn't support the scsi block commands (SBC) used for disks. For CDROMs both traditional parallel scsi and atap use the same command set (MMC in various revisions)

Quoted 2 lines

While it is possible to connect SATA drives to a SAS HBA, I believe this is done by tunneling IDE/ATAPI commands, rather then using native SCSI commands.

There are no ATAPI hard drives, just plain ATA which is not a SCSI command set at all. For directly connected disks on a SAS HBA the HBA speaks plain SATA to the disk (as the SAS and SATA link layers are the same). If you connect a SATA disk to a SAS expander the expander talks SATA to the disk and the expander encapsulates it in STP which is just a thin layer of SAS routing information around the ATA command block, similar to the SSP protocol used to talk to SCSI devices which is a thin layer around SCSI command blocks.

I would expect an ATAPI cdrom attached to a SAS expander to talk MMC command blocks over ATAPI over STP, but I haven't actually seen this setup in practice.

#7 lore raw
In reply to Bique Alexandre
Quoted 4 lines · Bique Alexandre

- I exported the data structure BDRVRawState from raw-posix.c in raw-posix.h,
because I use the file descriptor of the bsg device to poll, write and read. Maybe there is a better solution, but I didn't find what I was looking for in block.h.

The way we do scsi passthrough (and ATAPI really just is SCSI passthrough over ATA with a slightly special command set) is to call into the ioctl/aio_ioctl methods of the block driver and let that do the actual I/O. I think your patch would be simpler and cleaner by following that model

#8 lore raw
Quoted 11 lines · On Tuesday 30 June 2009 18:01:00 Christoph Hellwig wrote:
Quoted 4 lines · Bique Alexandre

- I exported the data structure BDRVRawState from raw-posix.c in
raw-posix.h, because I use the file descriptor of the bsg device to poll, write and read. Maybe there is a better solution, but I didn't find what I was looking for in block.h.

The way we do scsi passthrough (and ATAPI really just is SCSI passthrough over ATA with a slightly special command set) is to call into the ioctl/aio_ioctl methods of the block driver and let that do the actual I/O. I think your patch would be simpler and cleaner by following that model

I can switch to aio_ioctl, and use CDROM_SEND_PACKET ioctl.
Is it possible to send multiple requests at the same time with aio_ioctl ? When CONFIG_AIO is not available ?

Thanks.

Signature · 1 line
Alexandre Bique
#9 lore raw
Quoted 1 line · Bique Alexandre

I can switch to aio_ioctl, and use CDROM_SEND_PACKET ioctl.

CDROM_SEND_PACKET has a rather horrible API and should be avoided. Just use SG_IO like the the scsi passthrough code. In fact there might be some quite a few ways to unify that code if you're motivated.

Quoted 1 line

Is it possible to send multiple requests at the same time with aio_ioctl ?

SG_IO sends one requests per ioctl, but you can have multiple requests outstanding on a single file descriptor.

Quoted 1 line

When CONFIG_AIO is not available ?

aio_ioctl needs CONFIG_AIO - but on Linux hosts where you have bsd/sg/etc it always is available.