xref: /openbmc/qemu/hw/block/block.c (revision d36f7de8)
1 /*
2  * Common code for block device models
3  *
4  * Copyright (C) 2012 Red Hat, Inc.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or
7  * later.  See the COPYING file in the top-level directory.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "sysemu/blockdev.h"
12 #include "sysemu/block-backend.h"
13 #include "hw/block/block.h"
14 #include "qapi/error.h"
15 #include "qapi/qapi-types-block.h"
16 #include "qemu/error-report.h"
17 
18 void blkconf_blocksizes(BlockConf *conf)
19 {
20     BlockBackend *blk = conf->blk;
21     BlockSizes blocksizes;
22     int backend_ret;
23 
24     backend_ret = blk_probe_blocksizes(blk, &blocksizes);
25     /* fill in detected values if they are not defined via qemu command line */
26     if (!conf->physical_block_size) {
27         if (!backend_ret) {
28            conf->physical_block_size = blocksizes.phys;
29         } else {
30             conf->physical_block_size = BDRV_SECTOR_SIZE;
31         }
32     }
33     if (!conf->logical_block_size) {
34         if (!backend_ret) {
35             conf->logical_block_size = blocksizes.log;
36         } else {
37             conf->logical_block_size = BDRV_SECTOR_SIZE;
38         }
39     }
40 }
41 
42 bool blkconf_apply_backend_options(BlockConf *conf, bool readonly,
43                                    bool resizable, Error **errp)
44 {
45     BlockBackend *blk = conf->blk;
46     BlockdevOnError rerror, werror;
47     uint64_t perm, shared_perm;
48     bool wce;
49     int ret;
50 
51     perm = BLK_PERM_CONSISTENT_READ;
52     if (!readonly) {
53         perm |= BLK_PERM_WRITE;
54     }
55 
56     shared_perm = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
57                   BLK_PERM_GRAPH_MOD;
58     if (resizable) {
59         shared_perm |= BLK_PERM_RESIZE;
60     }
61     if (conf->share_rw) {
62         shared_perm |= BLK_PERM_WRITE;
63     }
64 
65     ret = blk_set_perm(blk, perm, shared_perm, errp);
66     if (ret < 0) {
67         return false;
68     }
69 
70     switch (conf->wce) {
71     case ON_OFF_AUTO_ON:    wce = true; break;
72     case ON_OFF_AUTO_OFF:   wce = false; break;
73     case ON_OFF_AUTO_AUTO:  wce = blk_enable_write_cache(blk); break;
74     default:
75         abort();
76     }
77 
78     rerror = conf->rerror;
79     if (rerror == BLOCKDEV_ON_ERROR_AUTO) {
80         rerror = blk_get_on_error(blk, true);
81     }
82 
83     werror = conf->werror;
84     if (werror == BLOCKDEV_ON_ERROR_AUTO) {
85         werror = blk_get_on_error(blk, false);
86     }
87 
88     blk_set_enable_write_cache(blk, wce);
89     blk_set_on_error(blk, rerror, werror);
90 
91     return true;
92 }
93 
94 bool blkconf_geometry(BlockConf *conf, int *ptrans,
95                       unsigned cyls_max, unsigned heads_max, unsigned secs_max,
96                       Error **errp)
97 {
98     if (!conf->cyls && !conf->heads && !conf->secs) {
99         hd_geometry_guess(conf->blk,
100                           &conf->cyls, &conf->heads, &conf->secs,
101                           ptrans);
102     } else if (ptrans && *ptrans == BIOS_ATA_TRANSLATION_AUTO) {
103         *ptrans = hd_bios_chs_auto_trans(conf->cyls, conf->heads, conf->secs);
104     }
105     if (conf->cyls || conf->heads || conf->secs) {
106         if (conf->cyls < 1 || conf->cyls > cyls_max) {
107             error_setg(errp, "cyls must be between 1 and %u", cyls_max);
108             return false;
109         }
110         if (conf->heads < 1 || conf->heads > heads_max) {
111             error_setg(errp, "heads must be between 1 and %u", heads_max);
112             return false;
113         }
114         if (conf->secs < 1 || conf->secs > secs_max) {
115             error_setg(errp, "secs must be between 1 and %u", secs_max);
116             return false;
117         }
118     }
119     return true;
120 }
121