xref: /openbmc/qemu/include/hw/block/block.h (revision 083fab02)
1 /*
2  * Common code for block device models
3  *
4  * Copyright (C) 2012 Red Hat, Inc.
5  * Copyright (c) 2003-2008 Fabrice Bellard
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2 or
8  * later.  See the COPYING file in the top-level directory.
9  */
10 
11 #ifndef HW_BLOCK_H
12 #define HW_BLOCK_H
13 
14 #include "qemu-common.h"
15 
16 /* Configuration */
17 
18 typedef struct BlockConf {
19     BlockBackend *blk;
20     uint16_t physical_block_size;
21     uint16_t logical_block_size;
22     uint16_t min_io_size;
23     uint32_t opt_io_size;
24     int32_t bootindex;
25     uint32_t discard_granularity;
26     /* geometry, not all devices use this */
27     uint32_t cyls, heads, secs;
28     OnOffAuto wce;
29     bool share_rw;
30     BlockdevOnError rerror;
31     BlockdevOnError werror;
32 } BlockConf;
33 
34 static inline unsigned int get_physical_block_exp(BlockConf *conf)
35 {
36     unsigned int exp = 0, size;
37 
38     for (size = conf->physical_block_size;
39         size > conf->logical_block_size;
40         size >>= 1) {
41         exp++;
42     }
43 
44     return exp;
45 }
46 
47 #define DEFINE_BLOCK_PROPERTIES(_state, _conf)                          \
48     DEFINE_PROP_DRIVE("drive", _state, _conf.blk),                      \
49     DEFINE_PROP_BLOCKSIZE("logical_block_size", _state,                 \
50                           _conf.logical_block_size),                    \
51     DEFINE_PROP_BLOCKSIZE("physical_block_size", _state,                \
52                           _conf.physical_block_size),                   \
53     DEFINE_PROP_UINT16("min_io_size", _state, _conf.min_io_size, 0),  \
54     DEFINE_PROP_UINT32("opt_io_size", _state, _conf.opt_io_size, 0),    \
55     DEFINE_PROP_UINT32("discard_granularity", _state, \
56                        _conf.discard_granularity, -1), \
57     DEFINE_PROP_ON_OFF_AUTO("write-cache", _state, _conf.wce, \
58                             ON_OFF_AUTO_AUTO), \
59     DEFINE_PROP_BOOL("share-rw", _state, _conf.share_rw, false)
60 
61 #define DEFINE_BLOCK_CHS_PROPERTIES(_state, _conf)      \
62     DEFINE_PROP_UINT32("cyls", _state, _conf.cyls, 0),  \
63     DEFINE_PROP_UINT32("heads", _state, _conf.heads, 0), \
64     DEFINE_PROP_UINT32("secs", _state, _conf.secs, 0)
65 
66 #define DEFINE_BLOCK_ERROR_PROPERTIES(_state, _conf)                    \
67     DEFINE_PROP_BLOCKDEV_ON_ERROR("rerror", _state, _conf.rerror,       \
68                                   BLOCKDEV_ON_ERROR_AUTO),              \
69     DEFINE_PROP_BLOCKDEV_ON_ERROR("werror", _state, _conf.werror,       \
70                                   BLOCKDEV_ON_ERROR_AUTO)
71 
72 /* Configuration helpers */
73 
74 void blkconf_serial(BlockConf *conf, char **serial);
75 void blkconf_geometry(BlockConf *conf, int *trans,
76                       unsigned cyls_max, unsigned heads_max, unsigned secs_max,
77                       Error **errp);
78 void blkconf_blocksizes(BlockConf *conf);
79 void blkconf_apply_backend_options(BlockConf *conf, bool readonly,
80                                    bool resizable, Error **errp);
81 
82 /* Hard disk geometry */
83 
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
90