virtio-blk: revert serial number support
PATCH 1/5 virtio-blk: revert serial number support
The addition of the whole ATA IDENTIY page caused the config space to go above the allowed size in the PCI spec, and thus the feature was already reverted in the Linux guest driver and disabled by default in qemu.
hw/virtio-blk.c +2 -58
@@ -25,9 +25,7 @@ typedef struct VirtIOBlockBlockDriverState *bs;VirtQueue *vq;void *rq; -char serial_str[BLOCK_SERIAL_STRLEN +1 ];QEMUBH *bh; -size_t config_size; } VirtIOBlock;static VirtIOBlock *to_virtio_blk (VirtIODevice *vdev)
@@ -35,47 +33,6 @@ static VirtIOBlock *to_virtio_blk(VirtIOreturn (VirtIOBlock *)vdev; } -/* store identify data in little endian format -*/ -static inline void put_le16 (uint16_t *p,unsigned int v) -{ - *p =cpu_to_le16 (v); -} - -/* copy to *dst from *src, nul pad dst tail as needed to len bytes -*/ -static inline void padstr (char *dst,const char *src,int len) -{ -while (len--) - *dst++ = *src ? *src++ :'\0' ; -} - -/* setup simulated identify data as appropriate for virtio block device -* -* ref: AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS) -*/ -static inline void virtio_identify_template (struct virtio_blk_config *bc) -{ -uint16_t *p = &bc->identify[0 ]; -uint64_t lba_sectors = bc->capacity; - -memset (p,0 ,sizeof (bc->identify)); -put_le16 (p +0 ,0x0 );/* ATA device */ -padstr ((char *)(p +23 ),QEMU_VERSION ,8 );/* firmware revision */ -padstr ((char *)(p +27 ),"QEMU VIRT_BLK" ,40 );/* model# */ -put_le16 (p +47 ,0x80ff );/* max xfer 255 sectors */ -put_le16 (p +49 ,0x0b00 );/* support IORDY/LBA/DMA */ -put_le16 (p +59 ,0x1ff );/* cur xfer 255 sectors */ -put_le16 (p +80 ,0x1f0 );/* support ATA8/7/6/5/4 */ -put_le16 (p +81 ,0x16 ); -put_le16 (p +82 ,0x400 ); -put_le16 (p +83 ,0x400 ); -put_le16 (p +100 , lba_sectors); -put_le16 (p +101 , lba_sectors >>16 ); -put_le16 (p +102 , lba_sectors >>32 ); -put_le16 (p +103 , lba_sectors >>48 ); -} - typedef struct VirtIOBlockReq { VirtIOBlock *dev;
@@ -448,10 +405,7 @@ static void virtio_blk_update_config(Vir blkcfg.heads = heads; blkcfg.sectors = secs; blkcfg.size_max =0 ; -virtio_identify_template (&blkcfg); -memcpy (&blkcfg.identify[VIRTIO_BLK_ID_SN ], s->serial_str, -VIRTIO_BLK_ID_SN_BYTES ); -memcpy (config, &blkcfg, s->config_size); +memcpy (config, &blkcfg,sizeof (struct virtio_blk_config )); }static uint32_t virtio_blk_get_features (VirtIODevice *vdev,uint32_t features)
@@ -463,8 +417,6 @@ static uint32_t virtio_blk_get_features(if (bdrv_enable_write_cache (s->bs)) features |= (1 <<VIRTIO_BLK_F_WCACHE ); -if (strcmp (s->serial_str,"0" )) - features |=1 <<VIRTIO_BLK_F_IDENTIFY ;if (bdrv_is_read_only (s->bs)) features |=1 <<VIRTIO_BLK_F_RO ;
@@ -510,24 +462,16 @@ VirtIODevice *virtio_blk_init(DeviceStatVirtIOBlock *s;int cylinders, heads, secs;static int virtio_blk_id; -char *ps = (char *)drive_get_serial (dinfo->bdrv); -size_t size =strlen (ps) ?sizeof (struct virtio_blk_config ) : - offsetof(struct virtio_blk_config , _blk_size); s = (VirtIOBlock *)virtio_common_init ("virtio-blk" ,VIRTIO_ID_BLOCK , - size, +sizeof (struct virtio_blk_config ),sizeof (VirtIOBlock)); - s->config_size = size; s->vdev.get_config = virtio_blk_update_config; s->vdev.get_features = virtio_blk_get_features; s->vdev.reset = virtio_blk_reset; s->bs = dinfo->bdrv; s->rq =NULL ; -if (strlen (ps)) -strncpy (s->serial_str, ps,sizeof (s->serial_str)); -else -snprintf (s->serial_str,sizeof (s->serial_str),"0" );bdrv_guess_geometry (s->bs, &cylinders, &heads, &secs);bdrv_set_geometry_hint (s->bs, cylinders, heads, secs);
hw/virtio-blk.h +1 -6
@@ -30,13 +30,9 @@#define VIRTIO_BLK_F_RO 5/* Disk is read-only */ #define VIRTIO_BLK_F_BLK_SIZE 6/* Block size of disk is available*/ #define VIRTIO_BLK_F_SCSI 7/* Supports scsi command passthru */ -#define VIRTIO_BLK_F_IDENTIFY 8/* ATA IDENTIFY supported */ +/* #define VIRTIO_BLK_F_IDENTIFY 8 ATA IDENTIFY supported, DEPRECATED */ #define VIRTIO_BLK_F_WCACHE 9/* write cache enabled */ -#define VIRTIO_BLK_ID_LEN 256/* length of identify u16 array */ -#define VIRTIO_BLK_ID_SN 10/* start of char * serial# */ -#define VIRTIO_BLK_ID_SN_BYTES 20/* length in bytes of serial# */ -struct virtio_blk_config {uint64_t capacity;
@@ -46,7 +42,6 @@ struct virtio_blk_configuint8_t heads;uint8_t sectors;uint32_t _blk_size;/* structure pad, currently unused */ -uint16_t identify[VIRTIO_BLK_ID_LEN ]; } __attribute__((packed));/* These two define direction. */
PATCH 2/5 block: add topology qdev properties
Add three new qdev properties to export block topology information to the guest. This is needed to get optimal I/O alignment for RAID arrays or SSDs.
The options are:
- physical_block_size to specify the physical block size of the device, this is going to increase from 512 bytes to 4096 kilobytes for many modern storage devices
- min_io_size to specify the minimal I/O size without performance impact, this is typically set to the RAID chunk size for arrays.
- opt_io_size to specify the optimal sustained I/O size, this is typically the RAID stripe width for arrays.
I decided to not auto-probe these values from blkid which might easily be possible as I don't know how to deal with these issues on migration.
Note that we specificly only set the physical_block_size, and not the logial one which is the unit all I/O is described in. The reason for that is that IDE does not support increasing the logical block size and at last for now I want to stick to one meachnisms in queue and allow for easy switching of transports for a given backing image which would not be possible if scsi and virtio use real 4k sectors, while ide only uses the physical block exponent.
To make this more common for the different block drivers introduce a new BlockConf structure holding all common block properties and a DEFINE_BLOCK_PROPERTIES macro to add them all together, mirroring what is done for network drivers. Also switch over all block drivers to use it, except for the floppy driver which has weird driveA/driveB properties and probably won't require any advanced block options ever.
Example usage for a virtio device with 4k physical block size and 8k optimal I/O size:
-drive file=scratch.img,media=disk,cache=none,id=scratch \ -device virtio-blk-pci,drive=scratch,physical_block_size=4096,opt_io_size=8192
block_int.h +27 -0
@@ -202,4 +202,31 @@ extern BlockDriverState *bdrv_first;int is_windows_drive (const char *filename);#endif +struct DriveInfo ; + +typedef struct BlockConf { +struct DriveInfo *dinfo; +uint16_t physical_block_size; +uint16_t min_io_size; +uint32_t opt_io_size; +}BlockConf ; + +static inline unsigned int get_physical_block_exp (BlockConf *conf) +{ +unsigned int exp =0 , size; + +for (size = conf->physical_block_size; size >512 ; size >>=1 ) { + exp++; + } + +return exp; +} + +#define DEFINE_BLOCK_PROPERTIES (_state, _conf) \ + DEFINE_PROP_DRIVE("drive", _state, _conf.dinfo), \ + DEFINE_PROP_UINT16("physical_block_size", _state, \ + _conf.physical_block_size, 512), \ + DEFINE_PROP_UINT16("min_io_size", _state, _conf.min_io_size, 512), \ + DEFINE_PROP_UINT32("opt_io_size", _state, _conf.opt_io_size, 512) +#endif /* BLOCK_INT_H */
hw/s390-virtio-bus.c +2 -2
@@ -123,7 +123,7 @@ static int s390_virtio_blk_init(VirtIOS3 {VirtIODevice *vdev; - vdev =virtio_blk_init ((DeviceState *)dev, dev->dinfo); + vdev =virtio_blk_init ((DeviceState *)dev, dev->block.dinfo);if (!vdev) {return -1 ; }
@@ -337,7 +337,7 @@ static VirtIOS390DeviceInfo s390_virtio_ .qdev.name = "virtio-blk-s390", .qdev.size = sizeof(VirtIOS390Device), .qdev.props = (Property []) { -DEFINE_PROP_DRIVE ("drive" , VirtIOS390Device, dinfo), +DEFINE_BLOCK_PROPERTIES (VirtIOS390Device, block),DEFINE_PROP_END_OF_LIST (), }, };
hw/s390-virtio-bus.h +1 -1
@@ -38,7 +38,7 @@ typedef struct VirtIOS390Device {ram_addr_t feat_offs;uint8_t feat_len;VirtIODevice *vdev; -DriveInfo *dinfo; +BlockConf block;NICConf nic;uint32_t host_features;/* Max. number of ports we can have for a the virtio-serial device */
hw/virtio-blk.c +2 -2
@@ -457,7 +457,7 @@ static int virtio_blk_load(QEMUFile *f,return 0 ; } -VirtIODevice *virtio_blk_init (DeviceState *dev,DriveInfo *dinfo) +VirtIODevice *virtio_blk_init (DeviceState *dev,BlockConf *conf) {VirtIOBlock *s;int cylinders, heads, secs;
@@ -470,7 +470,7 @@ VirtIODevice *virtio_blk_init(DeviceStat s->vdev.get_config = virtio_blk_update_config; s->vdev.get_features = virtio_blk_get_features; s->vdev.reset = virtio_blk_reset; - s->bs = dinfo->bdrv; + s->bs = conf->dinfo->bdrv; s->rq =NULL ;bdrv_guess_geometry (s->bs, &cylinders, &heads, &secs);bdrv_set_geometry_hint (s->bs, cylinders, heads, secs);
hw/virtio-pci.c +6 -5
@@ -22,6 +22,7 @@#include "sysemu.h" #include "msix.h" #include "net.h" +#include "block_int.h" #include "loader.h" /* from Linux's linux/virtio_pci.h */
@@ -92,7 +93,7 @@ typedef struct {uint32_t addr;uint32_t class_code;uint32_t nvectors; -DriveInfo *dinfo; +BlockConf block;NICConf nic;uint32_t host_features;/* Max. number of ports we can have for a the virtio-serial device */
@@ -457,11 +458,11 @@ static int virtio_blk_init_pci(PCIDevice proxy->class_code != PCI_CLASS_STORAGE_OTHER) proxy->class_code =PCI_CLASS_STORAGE_SCSI ; -if (!proxy->dinfo) { +if (!proxy->block.dinfo) {qemu_error ("virtio-blk-pci: drive property not set\n" );return -1 ; } - vdev =virtio_blk_init (&pci_dev->qdev, proxy->dinfo); + vdev =virtio_blk_init (&pci_dev->qdev, &proxy->block); vdev->nvectors = proxy->nvectors; virtio_init_pci(proxy, vdev, PCI_VENDOR_ID_REDHAT_QUMRANET,
@@ -481,7 +482,7 @@ static int virtio_blk_exit_pci(PCIDevice {VirtIOPCIProxy *proxy =DO_UPCAST (VirtIOPCIProxy, pci_dev, pci_dev); -drive_uninit (proxy->dinfo); +drive_uninit (proxy->block.dinfo);return virtio_exit_pci (pci_dev); }
@@ -558,7 +559,7 @@ static PCIDeviceInfo virtio_info[] = { .exit = virtio_blk_exit_pci, .qdev.props = (Property []) {DEFINE_PROP_HEX32 ("class" , VirtIOPCIProxy, class_code,0 ), -DEFINE_PROP_DRIVE ("drive" , VirtIOPCIProxy, dinfo), +DEFINE_BLOCK_PROPERTIES (VirtIOPCIProxy, block),DEFINE_PROP_UINT32 ("vectors" , VirtIOPCIProxy, nvectors,2 ),DEFINE_VIRTIO_BLK_FEATURES (VirtIOPCIProxy, host_features),DEFINE_PROP_END_OF_LIST (),
hw/virtio.h +2 -1
@@ -18,6 +18,7 @@#include "net.h" #include "qdev.h" #include "sysemu.h" +#include "block_int.h" /* from Linux's linux/virtio_config.h */
@@ -169,7 +170,7 @@ void virtio_bind_device(VirtIODevice *vd void *opaque);/* Base devices. */ -VirtIODevice *virtio_blk_init (DeviceState *dev,DriveInfo *dinfo); +VirtIODevice *virtio_blk_init (DeviceState *dev,BlockConf *conf);VirtIODevice *virtio_net_init (DeviceState *dev,NICConf *conf);VirtIODevice *virtio_serial_init (DeviceState *dev,uint32_t max_nr_ports);VirtIODevice *virtio_balloon_init (DeviceState *dev);
hw/ide/internal.h +5 -2
@@ -7,6 +7,7 @@* non-internal declarations are in hw/ide.h */ #include <hw/ide.h> +#include "block_int.h" /* debug IDE devices */ //#define DEBUG_IDE
@@ -397,6 +398,7 @@ struct IDEState {/* set for lba48 access */ uint8_t lba48;BlockDriverState *bs; +BlockConf *conf;char version[9 ];/* ATAPI specific */ uint8_t sense_key;
@@ -449,7 +451,7 @@ struct IDEBus {struct IDEDevice {DeviceState qdev;uint32_t unit; -DriveInfo *dinfo; +BlockConf conf;char *version; };
@@ -551,7 +553,8 @@ uint32_t ide_data_readw(void *opaque, uivoid ide_data_writel (void *opaque,uint32_t addr,uint32_t val);uint32_t ide_data_readl (void *opaque,uint32_t addr); -void ide_init_drive (IDEState *s,DriveInfo *dinfo,const char *version); +void ide_init_drive (IDEState *s,DriveInfo *dinfo,BlockConf *conf, +const char *version);void ide_init2 (IDEBus *bus,DriveInfo *hd0,DriveInfo *hd1,qemu_irq irq);void ide_init_ioport (IDEBus *bus,int iobase,int iobase2);
hw/ide/qdev.c +4 -3
@@ -40,7 +40,7 @@ static int ide_qdev_init(DeviceState *qdIDEDeviceInfo *info =DO_UPCAST (IDEDeviceInfo, qdev, base);IDEBus *bus =DO_UPCAST (IDEBus, qbus, qdev->parent_bus); -if (!dev->dinfo) { +if (!dev->conf.dinfo) {fprintf (stderr,"%s: no drive specified\n" , qdev->info->name); goto err; }
@@ -99,7 +99,8 @@ typedef struct IDEDrive {static int ide_drive_initfn (IDEDevice *dev) {IDEBus *bus =DO_UPCAST (IDEBus, qbus, dev->qdev.parent_bus); -ide_init_drive (bus->ifs + dev->unit, dev->dinfo, dev->version); +ide_init_drive (bus->ifs + dev->unit, dev->conf.dinfo, &dev->conf, + dev->version);return 0 ; }
@@ -109,7 +110,7 @@ static IDEDeviceInfo ide_drive_info = { .init = ide_drive_initfn, .qdev.props = (Property []) {DEFINE_PROP_UINT32 ("unit" , IDEDrive, dev.unit,-1 ), -DEFINE_PROP_DRIVE ("drive" , IDEDrive, dev.dinfo), +DEFINE_BLOCK_PROPERTIES (IDEDrive, dev.conf),DEFINE_PROP_STRING ("ver" , IDEDrive, dev.version),DEFINE_PROP_END_OF_LIST (), }
hw/scsi-disk.c +30 -33
234 lines not on this page · raw message
hw/scsi.h +2 -1
@@ -3,6 +3,7 @@#include "qdev.h" #include "block.h" +#include "block_int.h" #define SCSI_CMD_BUF_SIZE 16
@@ -49,7 +50,7 @@ struct SCSIDevice {DeviceState qdev;uint32_t id; -DriveInfo *dinfo; +BlockConf conf;SCSIDeviceInfo *info; QTAILQ_HEAD(, SCSIRequest) requests;int blocksize;
hw/scsi-generic.c +13 -11
@@ -58,6 +58,7 @@ typedef struct SCSIGenericReq {struct SCSIGenericState {SCSIDevice qdev; +BlockDriverState *bs;int lun;int driver_status;uint8_t sensebuf[SCSI_SENSE_BUF_SIZE ];
@@ -212,7 +213,7 @@ static void scsi_read_data(SCSIDevice *dreturn ; } - ret =execute_command (s->qdev.dinfo->bdrv, r,SG_DXFER_FROM_DEV , scsi_read_complete); + ret =execute_command (s->bs, r,SG_DXFER_FROM_DEV , scsi_read_complete);if (ret ==-1 ) {scsi_command_complete (r, -EINVAL );return ;
@@ -263,7 +264,7 @@ static int scsi_write_data(SCSIDevice *dreturn 0 ; } - ret =execute_command (s->qdev.dinfo->bdrv, r,SG_DXFER_TO_DEV , scsi_write_complete); + ret =execute_command (s->bs, r,SG_DXFER_TO_DEV , scsi_write_complete);if (ret ==-1 ) {scsi_command_complete (r, -EINVAL );return 1 ;
@@ -357,7 +358,7 @@ static int32_t scsi_send_command(SCSIDevqemu_free (r->buf); r->buflen =0 ; r->buf =NULL ; - ret =execute_command (s->qdev.dinfo->bdrv, r,SG_DXFER_NONE , scsi_command_complete); + ret =execute_command (s->bs, r,SG_DXFER_NONE , scsi_command_complete);if (ret ==-1 ) {scsi_command_complete (r, -EINVAL );return 0 ;
@@ -452,7 +453,7 @@ static void scsi_destroy(SCSIDevice *d) r =DO_UPCAST (SCSIGenericReq, req,QTAILQ_FIRST (&s->qdev.requests));scsi_remove_request (r); } -drive_uninit (s->qdev.dinfo); +drive_uninit (s->qdev.conf.dinfo); }static int scsi_generic_initfn (SCSIDevice *dev)
@@ -461,26 +462,27 @@ static int scsi_generic_initfn(SCSIDevicint sg_version;struct sg_scsi_id scsiid; -if (!s->qdev.dinfo || !s->qdev.dinfo->bdrv) { +if (!s->qdev.conf.dinfo || !s->qdev.conf.dinfo->bdrv) {qemu_error ("scsi-generic: drive property not set\n" );return -1 ; } + s->bs = s->qdev.conf.dinfo->bdrv;/* check we are really using a /dev/sg* file */ -if (!bdrv_is_sg (s->qdev.dinfo->bdrv)) { +if (!bdrv_is_sg (s->bs)) {qemu_error ("scsi-generic: not /dev/sg*\n" );return -1 ; }/* check we are using a driver managing SG_IO (version 3 and after */ -if (bdrv_ioctl (s->qdev.dinfo->bdrv,SG_GET_VERSION_NUM , &sg_version) <0 || +if (bdrv_ioctl (s->bs,SG_GET_VERSION_NUM , &sg_version) <0 || sg_version <30000 ) {qemu_error ("scsi-generic: scsi generic interface too old\n" );return -1 ; }/* get LUN of the /dev/sg? */ -if (bdrv_ioctl (s->qdev.dinfo->bdrv,SG_GET_SCSI_ID , &scsiid)) { +if (bdrv_ioctl (s->bs,SG_GET_SCSI_ID , &scsiid)) {qemu_error ("scsi-generic: SG_GET_SCSI_ID ioctl failed\n" );return -1 ; }
@@ -491,11 +493,11 @@ static int scsi_generic_initfn(SCSIDevic s->qdev.type = scsiid.scsi_type;DPRINTF ("device type %d\n" , s->qdev.type);if (s->qdev.type ==TYPE_TAPE ) { - s->qdev.blocksize =get_stream_blocksize (s->qdev.dinfo->bdrv); + s->qdev.blocksize =get_stream_blocksize (s->bs);if (s->qdev.blocksize ==-1 ) s->qdev.blocksize =0 ; }else { - s->qdev.blocksize =get_blocksize (s->qdev.dinfo->bdrv); + s->qdev.blocksize =get_blocksize (s->bs);/* removable media returns 0 if not present */ if (s->qdev.blocksize <=0 ) {if (s->qdev.type ==TYPE_ROM || s->qdev.type ==TYPE_WORM )
@@ -522,7 +524,7 @@ static SCSIDeviceInfo scsi_generic_info .cancel_io = scsi_cancel_io, .get_buf = scsi_get_buf, .qdev.props = (Property []) { -DEFINE_PROP_DRIVE ("drive" , SCSIGenericState, qdev.dinfo), +DEFINE_BLOCK_PROPERTIES (SCSIGenericState, qdev.conf),DEFINE_PROP_END_OF_LIST (), }, };
hw/usb-msd.c +6 -6
@@ -47,7 +47,7 @@ typedef struct {uint32_t residue;uint32_t tag;SCSIBus bus; -DriveInfo *dinfo; +BlockConf conf;SCSIDevice *scsi_dev;int result;/* For async completion. */
@@ -523,20 +523,20 @@ static int usb_msd_initfn(USBDevice *dev {MSDState *s =DO_UPCAST (MSDState, dev, dev); -if (!s->dinfo || !s->dinfo->bdrv) { +if (!s->conf.dinfo || !s->conf.dinfo->bdrv) {qemu_error ("usb-msd: drive property not set\n" );return -1 ; } s->dev.speed =USB_SPEED_FULL ;scsi_bus_new (&s->bus, &s->dev.qdev,0 ,1 , usb_msd_command_complete); - s->scsi_dev =scsi_bus_legacy_add_drive (&s->bus, s->dinfo,0 ); + s->scsi_dev =scsi_bus_legacy_add_drive (&s->bus, s->conf.dinfo,0 ); s->bus.qbus.allow_hotplug =0 ;usb_msd_handle_reset (dev); -if (bdrv_key_required (s->dinfo->bdrv)) { +if (bdrv_key_required (s->conf.dinfo->bdrv)) {if (s->dev.qdev.hotplugged) { -monitor_read_bdrv_key_start (cur_mon, s->dinfo->bdrv, +monitor_read_bdrv_key_start (cur_mon, s->conf.dinfo->bdrv, usb_msd_password_cb, s); s->dev.auto_attach =0 ; }else {
@@ -611,7 +611,7 @@ static struct USBDeviceInfo msd_info = { .usbdevice_name = "disk", .usbdevice_init = usb_msd_init, .qdev.props = (Property []) { -DEFINE_PROP_DRIVE ("drive" , MSDState, dinfo), +DEFINE_BLOCK_PROPERTIES (MSDState, conf),DEFINE_PROP_END_OF_LIST (), }, };
hw/ide/core.c +7 -3
@@ -2594,7 +2594,8 @@ void ide_bus_reset(IDEBus *bus)ide_clear_hob (bus); } -void ide_init_drive (IDEState *s,DriveInfo *dinfo,const char *version) +void ide_init_drive (IDEState *s,DriveInfo *dinfo,BlockConf *conf, +const char *version) {int cylinders, heads, secs;uint64_t nb_sectors;
@@ -2619,6 +2620,9 @@ void ide_init_drive(IDEState *s, DriveIn }strncpy (s->drive_serial_str,drive_get_serial (s->bs),sizeof (s->drive_serial_str)); +if (conf) { + s->conf = conf; + } }if (strlen (s->drive_serial_str) ==0 ) snprintf(s->drive_serial_str, sizeof(s->drive_serial_str),
@@ -2648,9 +2652,9 @@ void ide_init2(IDEBus *bus, DriveInfo *h s->sector_write_timer =qemu_new_timer (vm_clock, ide_sector_write_timer_cb, s);if (i ==0 ) -ide_init_drive (s, hd0,NULL ); +ide_init_drive (s, hd0,NULL ,NULL );if (i ==1 ) -ide_init_drive (s, hd1,NULL ); +ide_init_drive (s, hd1,NULL ,NULL ); } bus->irq = irq; }
PATCH 3/5 virtio-blk: add topology support
In reply to Christoph HellwigExport all topology information in the block config structure, guarded by a new VIRTIO_BLK_F_TOPOLOGY feature flag.
hw/virtio-blk.c +7 -0
@@ -26,6 +26,7 @@ typedef struct VirtIOBlockVirtQueue *vq;void *rq;QEMUBH *bh; +BlockConf *conf; } VirtIOBlock;static VirtIOBlock *to_virtio_blk (VirtIODevice *vdev)
@@ -405,6 +406,10 @@ static void virtio_blk_update_config(Vir blkcfg.heads = heads; blkcfg.sectors = secs; blkcfg.size_max =0 ; + blkcfg.physical_block_exp =get_physical_block_exp (s->conf); + blkcfg.alignment_offset =0 ; + blkcfg.min_io_size = s->conf->min_io_size /512 ; + blkcfg.opt_io_size = s->conf->opt_io_size /512 ;memcpy (config, &blkcfg,sizeof (struct virtio_blk_config )); }
@@ -414,6 +419,7 @@ static uint32_t virtio_blk_get_features( features |= (1 <<VIRTIO_BLK_F_SEG_MAX ); features |= (1 <<VIRTIO_BLK_F_GEOMETRY ); + features |= (1 <<VIRTIO_BLK_F_TOPOLOGY );if (bdrv_enable_write_cache (s->bs)) features |= (1 <<VIRTIO_BLK_F_WCACHE );
@@ -471,6 +477,7 @@ VirtIODevice *virtio_blk_init(DeviceStat s->vdev.get_features = virtio_blk_get_features; s->vdev.reset = virtio_blk_reset; s->bs = conf->dinfo->bdrv; + s->conf = conf; s->rq =NULL ;bdrv_guess_geometry (s->bs, &cylinders, &heads, &secs);bdrv_set_geometry_hint (s->bs, cylinders, heads, secs);
hw/virtio-blk.h +5 -0
@@ -32,6 +32,7 @@#define VIRTIO_BLK_F_SCSI 7/* Supports scsi command passthru */ /* #define VIRTIO_BLK_F_IDENTIFY 8 ATA IDENTIFY supported, DEPRECATED */ #define VIRTIO_BLK_F_WCACHE 9/* write cache enabled */ +#define VIRTIO_BLK_F_TOPOLOGY 10/* Topology information is available */ struct virtio_blk_config {
@@ -42,6 +43,10 @@ struct virtio_blk_configuint8_t heads;uint8_t sectors;uint32_t _blk_size;/* structure pad, currently unused */ +uint8_t physical_block_exp; +uint8_t alignment_offset; +uint16_t min_io_size; +uint32_t opt_io_size; } __attribute__((packed));/* These two define direction. */
PATCH 4/5 scsi: add topology support
In reply to Christoph HellwigExport the physical block size in the READ CAPACITY (16) command, and add the new block limits VPD page to export the minimum and optiomal I/O sizes.
Note that we also need to bump the scsi revision level to SPC-2 as that is the minimum requirement by at least the Linux kernel to try READ CAPACITY (16) first and look at the block limits VPD page.
hw/scsi-disk.c +26 -2
@@ -349,10 +349,11 @@ static int scsi_disk_emulate_inquiry(SCScase 0x00 :/* Supported page codes, mandatory */ DPRINTF ("Inquiry EVPD[Supported pages] " "buffer size %zd\n" , req->cmd.xfer); - outbuf[buflen++] =3 ;// number of pages + outbuf[buflen++] =4 ;// number of pages outbuf[buflen++] =0x00 ;// list of supported pages (this page) outbuf[buflen++] =0x80 ;// unit serial number outbuf[buflen++] =0x83 ;// device identification + outbuf[buflen++] =0xb0 ;// block device characteristics break ;case 0x80 :/* Device serial number, optional */
@@ -394,6 +395,27 @@ static int scsi_disk_emulate_inquiry(SCS buflen += id_len;break ; } +case 0xb0 :/* block device characteristics */ + { +unsigned int min_io_size = s->qdev.conf.min_io_size >>9 ; +unsigned int opt_io_size = s->qdev.conf.opt_io_size >>9 ; + +/* required VPD size with unmap support */ + outbuf[3 ] = buflen =0x3c ; + +memset (outbuf +4 ,0 , buflen -4 ); + +/* optimal transfer length granularity */ + outbuf[6 ] = (min_io_size >>8 ) &0xff ; + outbuf[7 ] = min_io_size &0xff ; + +/* optimal transfer length */ + outbuf[12 ] = (opt_io_size >>24 ) &0xff ; + outbuf[13 ] = (opt_io_size >>16 ) &0xff ; + outbuf[14 ] = (opt_io_size >>8 ) &0xff ; + outbuf[15 ] = opt_io_size &0xff ; +break ; + }default :BADF ("Error: unsupported Inquiry (EVPD[%02X]) " "buffer size %zd\n" , page_code, req->cmd.xfer);
@@ -440,7 +462,7 @@ static int scsi_disk_emulate_inquiry(SCSmemcpy (&outbuf[32 ], s->version ? s->version :QEMU_VERSION ,4 );/* Identify device as SCSI-3 rev 1. Some later commands are also implemented. */ - outbuf[2 ] =3 ; + outbuf[2 ] =5 ; outbuf[3 ] =2 ;/* Format 2 */ if (buflen >36 ) {
@@ -794,6 +816,8 @@ static int scsi_disk_emulate_command(SCS outbuf[9 ] =0 ; outbuf[10 ] = s->cluster_size *2 ; outbuf[11 ] =0 ; + outbuf[12 ] =0 ; + outbuf[13 ] =get_physical_block_exp (&s->qdev.conf);/* Protection, exponent and lowest lba field left blank. */ buflen = req->cmd.xfer;break ;
PATCH 5/5 ide: add topology support
In reply to Christoph HellwigExport the physical block size in the ATA IDENTIFY command. The other topology values are not supported in ATA so skip them.
hw/ide/core.c +2 -0
@@ -164,6 +164,8 @@ static void ide_identify(IDEState *s)put_le16 (p +101 , s->nb_sectors >>16 );put_le16 (p +102 , s->nb_sectors >>32 );put_le16 (p +103 , s->nb_sectors >>48 ); +if (s->conf && s->conf->physical_block_size) +put_le16 (p +106 ,0x6000 |get_physical_block_exp (s->conf));memcpy (s->identify_data, p,sizeof (s->identify_data)); s->identify_set =1 ;
Anthony Liguori
AppliedQuoted 7 lines · Christoph Hellwig
The addition of the whole ATA IDENTIY page caused the config space to go above the allowed size in the PCI spec, and thus the feature was already reverted in the Linux guest driver and disabled by default in qemu.
Signed-off-by Christoph Hellwig<[email protected]>
Applied all. Thanks.
Regards,
Anthony Liguori
Quoted 140 lines
Index:
qemu/hw/virtio-blk.c
===================================================================hw/virtio-blk.c +2
-58@@ -25,9 +25,7 @@ typedef struct VirtIOBlockBlockDriverState *bs;VirtQueue *vq;void *rq; -char serial_str[BLOCK_SERIAL_STRLEN +1 ];QEMUBH *bh; -size_t config_size; } VirtIOBlock;static VirtIOBlock *to_virtio_blk (VirtIODevice *vdev)@@ -35,47 +33,6 @@ static VirtIOBlock *to_virtio_blk(VirtIOreturn (VirtIOBlock *)vdev; } -/* store identify data in little endian format -*/ -static inline void put_le16 (uint16_t *p,unsigned int v) -{ - *p =cpu_to_le16 (v); -} - -/* copy to *dst from *src, nul pad dst tail as needed to len bytes -*/ -static inline void padstr (char *dst,const char *src,int len) -{ -while (len--) - *dst++ = *src ? *src++ :'\0' ; -} - -/* setup simulated identify data as appropriate for virtio block device -* -* ref: AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS) -*/ -static inline void virtio_identify_template (struct virtio_blk_config *bc) -{ -uint16_t *p =&bc->identify[0 ]; -uint64_t lba_sectors = bc->capacity; - -memset (p,0 ,sizeof (bc->identify)); -put_le16 (p +0 ,0x0 );/* ATA device */ -padstr ((char *)(p +23 ),QEMU_VERSION ,8 );/* firmware revision */ -padstr ((char *)(p +27 ),"QEMU VIRT_BLK" ,40 );/* model# */ -put_le16 (p +47 ,0x80ff );/* max xfer 255 sectors */ -put_le16 (p +49 ,0x0b00 );/* support IORDY/LBA/DMA */ -put_le16 (p +59 ,0x1ff );/* cur xfer 255 sectors */ -put_le16 (p +80 ,0x1f0 );/* support ATA8/7/6/5/4 */ -put_le16 (p +81 ,0x16 ); -put_le16 (p +82 ,0x400 ); -put_le16 (p +83 ,0x400 ); -put_le16 (p +100 , lba_sectors); -put_le16 (p +101 , lba_sectors>>16 ); -put_le16 (p +102 , lba_sectors>>32 ); -put_le16 (p +103 , lba_sectors>>48 ); -} - typedef struct VirtIOBlockReq { VirtIOBlock *dev;@@ -448,10 +405,7 @@ static void virtio_blk_update_config(Vir blkcfg.heads = heads; blkcfg.sectors = secs; blkcfg.size_max =0 ; -virtio_identify_template (&blkcfg); -memcpy (&blkcfg.identify[VIRTIO_BLK_ID_SN ], s->serial_str, -VIRTIO_BLK_ID_SN_BYTES ); -memcpy (config,&blkcfg, s->config_size); +memcpy (config,&blkcfg,sizeof (struct virtio_blk_config )); }static uint32_t virtio_blk_get_features (VirtIODevice *vdev,uint32_t features)@@ -463,8 +417,6 @@ static uint32_t virtio_blk_get_features(if (bdrv_enable_write_cache (s->bs)) features |= (1 <<VIRTIO_BLK_F_WCACHE ); -if (strcmp (s->serial_str,"0" )) - features |=1 <<VIRTIO_BLK_F_IDENTIFY ;if (bdrv_is_read_only (s->bs)) features |=1 <<VIRTIO_BLK_F_RO ;@@ -510,24 +462,16 @@ VirtIODevice *virtio_blk_init(DeviceStatVirtIOBlock *s;int cylinders, heads, secs;static int virtio_blk_id; -char *ps = (char *)drive_get_serial (dinfo->bdrv); -size_t size =strlen (ps) ?sizeof (struct virtio_blk_config ) : - offsetof(struct virtio_blk_config , _blk_size); s = (VirtIOBlock *)virtio_common_init ("virtio-blk" ,VIRTIO_ID_BLOCK , - size, +sizeof (struct virtio_blk_config ),sizeof (VirtIOBlock)); - s->config_size = size; s->vdev.get_config = virtio_blk_update_config; s->vdev.get_features = virtio_blk_get_features; s->vdev.reset = virtio_blk_reset; s->bs = dinfo->bdrv; s->rq =NULL ; -if (strlen (ps)) -strncpy (s->serial_str, ps,sizeof (s->serial_str)); -else -snprintf (s->serial_str,sizeof (s->serial_str),"0" );bdrv_guess_geometry (s->bs,&cylinders,&heads,&secs);bdrv_set_geometry_hint (s->bs, cylinders, heads, secs);Index:
qemu/hw/virtio-blk.h
===================================================================hw/virtio-blk.h +1
-6@@ -30,13 +30,9 @@#define VIRTIO_BLK_F_RO 5/* Disk is read-only */ #define VIRTIO_BLK_F_BLK_SIZE 6/* Block size of disk is available*/ #define VIRTIO_BLK_F_SCSI 7/* Supports scsi command passthru */ -#define VIRTIO_BLK_F_IDENTIFY 8/* ATA IDENTIFY supported */ +/* #define VIRTIO_BLK_F_IDENTIFY 8 ATA IDENTIFY supported, DEPRECATED */ #define VIRTIO_BLK_F_WCACHE 9/* write cache enabled */ -#define VIRTIO_BLK_ID_LEN 256/* length of identify u16 array */ -#define VIRTIO_BLK_ID_SN 10/* start of char * serial# */ -#define VIRTIO_BLK_ID_SN_BYTES 20/* length in bytes of serial# */ -struct virtio_blk_config {uint64_t capacity;@@ -46,7 +42,6 @@ struct virtio_blk_configuint8_t heads;uint8_t sectors;uint32_t _blk_size;/* structure pad, currently unused */ -uint16_t identify[VIRTIO_BLK_ID_LEN ]; } __attribute__((packed));/* These two define direction. */