xref: /openbmc/qemu/include/hw/block/block.h (revision bfb27e60)
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_COMMON_H
12 #define HW_BLOCK_COMMON_H
13 
14 #include "qemu-common.h"
15 #include "qapi/error.h"
16 
17 /* Configuration */
18 
19 typedef struct BlockConf {
20     BlockDriverState *bs;
21     uint16_t physical_block_size;
22     uint16_t logical_block_size;
23     uint16_t min_io_size;
24     uint32_t opt_io_size;
25     int32_t bootindex;
26     uint32_t discard_granularity;
27     /* geometry, not all devices use this */
28     uint32_t cyls, heads, secs;
29 } BlockConf;
30 
31 static inline unsigned int get_physical_block_exp(BlockConf *conf)
32 {
33     unsigned int exp = 0, size;
34 
35     for (size = conf->physical_block_size;
36         size > conf->logical_block_size;
37         size >>= 1) {
38         exp++;
39     }
40 
41     return exp;
42 }
43 
44 #define DEFINE_BLOCK_PROPERTIES(_state, _conf)                          \
45     DEFINE_PROP_DRIVE("drive", _state, _conf.bs),                       \
46     DEFINE_PROP_BLOCKSIZE("logical_block_size", _state,                 \
47                           _conf.logical_block_size, 512),               \
48     DEFINE_PROP_BLOCKSIZE("physical_block_size", _state,                \
49                           _conf.physical_block_size, 512),              \
50     DEFINE_PROP_UINT16("min_io_size", _state, _conf.min_io_size, 0),  \
51     DEFINE_PROP_UINT32("opt_io_size", _state, _conf.opt_io_size, 0),    \
52     DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1),        \
53     DEFINE_PROP_UINT32("discard_granularity", _state, \
54                        _conf.discard_granularity, -1)
55 
56 #define DEFINE_BLOCK_CHS_PROPERTIES(_state, _conf)      \
57     DEFINE_PROP_UINT32("cyls", _state, _conf.cyls, 0),  \
58     DEFINE_PROP_UINT32("heads", _state, _conf.heads, 0), \
59     DEFINE_PROP_UINT32("secs", _state, _conf.secs, 0)
60 
61 /* Configuration helpers */
62 
63 void blkconf_serial(BlockConf *conf, char **serial);
64 void blkconf_geometry(BlockConf *conf, int *trans,
65                       unsigned cyls_max, unsigned heads_max, unsigned secs_max,
66                       Error **errp);
67 
68 /* Hard disk geometry */
69 
70 void hd_geometry_guess(BlockDriverState *bs,
71                        uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs,
72                        int *ptrans);
73 int hd_bios_chs_auto_trans(uint32_t cyls, uint32_t heads, uint32_t secs);
74 
75 #endif
76