block.h 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * Common code for block device models
 *
 * Copyright (C) 2012 Red Hat, Inc.
 * Copyright (c) 2003-2008 Fabrice Bellard
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or
 * later.  See the COPYING file in the top-level directory.
 */

11 12
#ifndef HW_BLOCK_H
#define HW_BLOCK_H
13 14

#include "qemu-common.h"
15
#include "qapi/qapi-types-block-core.h"
16

17 18 19
/* Configuration */

typedef struct BlockConf {
20
    BlockBackend *blk;
21 22 23 24 25 26 27 28
    uint16_t physical_block_size;
    uint16_t logical_block_size;
    uint16_t min_io_size;
    uint32_t opt_io_size;
    int32_t bootindex;
    uint32_t discard_granularity;
    /* geometry, not all devices use this */
    uint32_t cyls, heads, secs;
29
    OnOffAuto wce;
30
    bool share_rw;
31 32
    BlockdevOnError rerror;
    BlockdevOnError werror;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
} BlockConf;

static inline unsigned int get_physical_block_exp(BlockConf *conf)
{
    unsigned int exp = 0, size;

    for (size = conf->physical_block_size;
        size > conf->logical_block_size;
        size >>= 1) {
        exp++;
    }

    return exp;
}

#define DEFINE_BLOCK_PROPERTIES(_state, _conf)                          \
49
    DEFINE_PROP_DRIVE("drive", _state, _conf.blk),                      \
50
    DEFINE_PROP_BLOCKSIZE("logical_block_size", _state,                 \
51
                          _conf.logical_block_size),                    \
52
    DEFINE_PROP_BLOCKSIZE("physical_block_size", _state,                \
53
                          _conf.physical_block_size),                   \
54 55 56
    DEFINE_PROP_UINT16("min_io_size", _state, _conf.min_io_size, 0),  \
    DEFINE_PROP_UINT32("opt_io_size", _state, _conf.opt_io_size, 0),    \
    DEFINE_PROP_UINT32("discard_granularity", _state, \
57
                       _conf.discard_granularity, -1), \
58 59 60
    DEFINE_PROP_ON_OFF_AUTO("write-cache", _state, _conf.wce, \
                            ON_OFF_AUTO_AUTO), \
    DEFINE_PROP_BOOL("share-rw", _state, _conf.share_rw, false)
61 62 63 64 65 66

#define DEFINE_BLOCK_CHS_PROPERTIES(_state, _conf)      \
    DEFINE_PROP_UINT32("cyls", _state, _conf.cyls, 0),  \
    DEFINE_PROP_UINT32("heads", _state, _conf.heads, 0), \
    DEFINE_PROP_UINT32("secs", _state, _conf.secs, 0)

67 68 69 70 71 72
#define DEFINE_BLOCK_ERROR_PROPERTIES(_state, _conf)                    \
    DEFINE_PROP_BLOCKDEV_ON_ERROR("rerror", _state, _conf.rerror,       \
                                  BLOCKDEV_ON_ERROR_AUTO),              \
    DEFINE_PROP_BLOCKDEV_ON_ERROR("werror", _state, _conf.werror,       \
                                  BLOCKDEV_ON_ERROR_AUTO)

73 74
/* Configuration helpers */

M
Mao Zhongyi 已提交
75
bool blkconf_geometry(BlockConf *conf, int *trans,
F
Fam Zheng 已提交
76 77
                      unsigned cyls_max, unsigned heads_max, unsigned secs_max,
                      Error **errp);
78
void blkconf_blocksizes(BlockConf *conf);
M
Mao Zhongyi 已提交
79
bool blkconf_apply_backend_options(BlockConf *conf, bool readonly,
K
Kevin Wolf 已提交
80
                                   bool resizable, Error **errp);
81

82 83
/* Hard disk geometry */

84
void hd_geometry_guess(BlockBackend *blk,
85
                       uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs,
86
                       int *ptrans);
87
int hd_bios_chs_auto_trans(uint32_t cyls, uint32_t heads, uint32_t secs);
88 89

#endif