| 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 "exec/hwaddr.h" |
| 15 | #include "qapi/qapi-types-block-core.h" |
| 16 | #include "hw/core/qdev-properties-system.h" |
| 17 | |
| 18 | /* Configuration */ |
| 19 | |
| 20 | typedef struct BlockConf { |
| 21 | BlockBackend *blk; |
| 22 | OnOffAuto backend_defaults; |
| 23 | uint32_t physical_block_size; |
| 24 | uint32_t logical_block_size; |
| 25 | uint32_t min_io_size; |
| 26 | uint32_t opt_io_size; |
| 27 | int32_t bootindex; |
| 28 | uint32_t discard_granularity; |
| 29 | /* geometry, not all devices use this */ |
| 30 | uint32_t cyls, heads, secs; |
| 31 | uint32_t lcyls, lheads, lsecs; |
| 32 | OnOffAuto wce; |
| 33 | bool share_rw; |
| 34 | OnOffAuto account_invalid, account_failed; |
| 35 | BlockdevOnError rerror; |
| 36 | BlockdevOnError werror; |
| 37 | uint32_t num_stats_intervals; |
| 38 | uint32_t *stats_intervals; |
| 39 | } BlockConf; |
| 40 | |
| 41 | static inline unsigned int get_physical_block_exp(BlockConf *conf) |
| 42 | { |
| 43 | unsigned int exp = 0, size; |
| 44 | |
| 45 | for (size = conf->physical_block_size; |
| 46 | size > conf->logical_block_size; |
| 47 | size >>= 1) { |
| 48 | exp++; |
| 49 | } |
| 50 | |
| 51 | return exp; |
| 52 | } |
| 53 | |
| 54 | #define DEFAULT_BLOCK_CONF (BlockConf) { \ |
| 55 | .bootindex = -1, \ |
| 56 | .backend_defaults = ON_OFF_AUTO_AUTO, \ |
| 57 | .discard_granularity = -1, \ |
| 58 | .wce = ON_OFF_AUTO_AUTO, \ |
| 59 | .share_rw = false, \ |
| 60 | .account_invalid = ON_OFF_AUTO_AUTO, \ |
| 61 | .account_failed = ON_OFF_AUTO_AUTO, \ |
| 62 | .rerror = BLOCKDEV_ON_ERROR_AUTO, \ |
| 63 | .werror = BLOCKDEV_ON_ERROR_AUTO, \ |
| 64 | } |
| 65 | |
| 66 | #define DEFINE_BLOCK_PROPERTIES_BASE(_state, _conf) \ |
| 67 | DEFINE_PROP_ON_OFF_AUTO("backend_defaults", _state, \ |
| 68 | _conf.backend_defaults, ON_OFF_AUTO_AUTO), \ |
| 69 | DEFINE_PROP_BLOCKSIZE("logical_block_size", _state, \ |
| 70 | _conf.logical_block_size), \ |
| 71 | DEFINE_PROP_BLOCKSIZE("physical_block_size", _state, \ |
| 72 | _conf.physical_block_size), \ |
| 73 | DEFINE_PROP_SIZE32("min_io_size", _state, _conf.min_io_size, 0), \ |
| 74 | DEFINE_PROP_SIZE32("opt_io_size", _state, _conf.opt_io_size, 0), \ |
| 75 | DEFINE_PROP_SIZE32("discard_granularity", _state, \ |
| 76 | _conf.discard_granularity, -1), \ |
| 77 | DEFINE_PROP_ON_OFF_AUTO("write-cache", _state, _conf.wce, \ |
| 78 | ON_OFF_AUTO_AUTO), \ |
| 79 | DEFINE_PROP_BOOL("share-rw", _state, _conf.share_rw, false), \ |
| 80 | DEFINE_PROP_ON_OFF_AUTO("account-invalid", _state, \ |
| 81 | _conf.account_invalid, ON_OFF_AUTO_AUTO), \ |
| 82 | DEFINE_PROP_ON_OFF_AUTO("account-failed", _state, \ |
| 83 | _conf.account_failed, ON_OFF_AUTO_AUTO), \ |
| 84 | DEFINE_PROP_ARRAY("stats-intervals", _state, \ |
| 85 | _conf.num_stats_intervals, _conf.stats_intervals, \ |
| 86 | qdev_prop_uint32, uint32_t) |
| 87 | |
| 88 | #define DEFINE_BLOCK_PROPERTIES(_state, _conf) \ |
| 89 | DEFINE_PROP_DRIVE("drive", _state, _conf.blk), \ |
| 90 | DEFINE_BLOCK_PROPERTIES_BASE(_state, _conf) |
| 91 | |
| 92 | #define DEFINE_BLOCK_CHS_PROPERTIES(_state, _conf) \ |
| 93 | DEFINE_PROP_UINT32("cyls", _state, _conf.cyls, 0), \ |
| 94 | DEFINE_PROP_UINT32("heads", _state, _conf.heads, 0), \ |
| 95 | DEFINE_PROP_UINT32("secs", _state, _conf.secs, 0), \ |
| 96 | DEFINE_PROP_UINT32("lcyls", _state, _conf.lcyls, 0), \ |
| 97 | DEFINE_PROP_UINT32("lheads", _state, _conf.lheads, 0), \ |
| 98 | DEFINE_PROP_UINT32("lsecs", _state, _conf.lsecs, 0) |
| 99 | |
| 100 | #define DEFINE_BLOCK_ERROR_PROPERTIES(_state, _conf) \ |
| 101 | DEFINE_PROP_BLOCKDEV_ON_ERROR("rerror", _state, _conf.rerror, \ |
| 102 | BLOCKDEV_ON_ERROR_AUTO), \ |
| 103 | DEFINE_PROP_BLOCKDEV_ON_ERROR("werror", _state, _conf.werror, \ |
| 104 | BLOCKDEV_ON_ERROR_AUTO) |
| 105 | |
| 106 | /* Backend access helpers */ |
| 107 | |
| 108 | bool blk_check_size_and_read_all(BlockBackend *blk, DeviceState *dev, |
| 109 | void *buf, hwaddr size, Error **errp); |
| 110 | |
| 111 | /* Configuration helpers */ |
| 112 | |
| 113 | bool blkconf_geometry(BlockConf *conf, int *trans, |
| 114 | unsigned cyls_max, unsigned heads_max, unsigned secs_max, |
| 115 | Error **errp); |
| 116 | bool blkconf_blocksizes(BlockConf *conf, Error **errp); |
| 117 | bool blkconf_apply_backend_options(BlockConf *conf, bool readonly, |
| 118 | bool resizable, Error **errp); |
| 119 | |
| 120 | /* Hard disk geometry */ |
| 121 | |
| 122 | void hd_geometry_guess(BlockBackend *blk, |
| 123 | uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs, |
| 124 | int *ptrans); |
| 125 | int hd_bios_chs_auto_trans(uint32_t cyls, uint32_t heads, uint32_t secs); |
| 126 | |
| 127 | #endif |