block-common.h 2.5 KB

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