block/nvme: Use union of NvmeIdCtrl / NvmeIdNs structures
We allocate an unique chunk of memory then use it for two different structures. By using an union, we make it clear the data is overlapping (and we can remove the casts). Suggested-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Stefano Garzarella <sgarzare@redhat.com> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com> Message-Id: <20200821195359.1285345-9-philmd@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
4d98093937
commit
7d3b214ae4
31
block/nvme.c
31
block/nvme.c
|
@ -508,9 +508,10 @@ static int nvme_cmd_sync(BlockDriverState *bs, NVMeQueuePair *q,
|
||||||
static void nvme_identify(BlockDriverState *bs, int namespace, Error **errp)
|
static void nvme_identify(BlockDriverState *bs, int namespace, Error **errp)
|
||||||
{
|
{
|
||||||
BDRVNVMeState *s = bs->opaque;
|
BDRVNVMeState *s = bs->opaque;
|
||||||
NvmeIdCtrl *idctrl;
|
union {
|
||||||
NvmeIdNs *idns;
|
NvmeIdCtrl ctrl;
|
||||||
uint8_t *id;
|
NvmeIdNs ns;
|
||||||
|
} *id;
|
||||||
NvmeLBAF *lbaf;
|
NvmeLBAF *lbaf;
|
||||||
uint16_t oncs;
|
uint16_t oncs;
|
||||||
int r;
|
int r;
|
||||||
|
@ -520,14 +521,12 @@ static void nvme_identify(BlockDriverState *bs, int namespace, Error **errp)
|
||||||
.cdw10 = cpu_to_le32(0x1),
|
.cdw10 = cpu_to_le32(0x1),
|
||||||
};
|
};
|
||||||
|
|
||||||
id = qemu_try_blockalign0(bs, sizeof(NvmeIdCtrl));
|
id = qemu_try_blockalign0(bs, sizeof(*id));
|
||||||
if (!id) {
|
if (!id) {
|
||||||
error_setg(errp, "Cannot allocate buffer for identify response");
|
error_setg(errp, "Cannot allocate buffer for identify response");
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
idctrl = (NvmeIdCtrl *)id;
|
r = qemu_vfio_dma_map(s->vfio, id, sizeof(*id), true, &iova);
|
||||||
idns = (NvmeIdNs *)id;
|
|
||||||
r = qemu_vfio_dma_map(s->vfio, id, sizeof(NvmeIdCtrl), true, &iova);
|
|
||||||
if (r) {
|
if (r) {
|
||||||
error_setg(errp, "Cannot map buffer for DMA");
|
error_setg(errp, "Cannot map buffer for DMA");
|
||||||
goto out;
|
goto out;
|
||||||
|
@ -539,22 +538,22 @@ static void nvme_identify(BlockDriverState *bs, int namespace, Error **errp)
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (le32_to_cpu(idctrl->nn) < namespace) {
|
if (le32_to_cpu(id->ctrl.nn) < namespace) {
|
||||||
error_setg(errp, "Invalid namespace");
|
error_setg(errp, "Invalid namespace");
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
s->write_cache_supported = le32_to_cpu(idctrl->vwc) & 0x1;
|
s->write_cache_supported = le32_to_cpu(id->ctrl.vwc) & 0x1;
|
||||||
s->max_transfer = (idctrl->mdts ? 1 << idctrl->mdts : 0) * s->page_size;
|
s->max_transfer = (id->ctrl.mdts ? 1 << id->ctrl.mdts : 0) * s->page_size;
|
||||||
/* For now the page list buffer per command is one page, to hold at most
|
/* For now the page list buffer per command is one page, to hold at most
|
||||||
* s->page_size / sizeof(uint64_t) entries. */
|
* s->page_size / sizeof(uint64_t) entries. */
|
||||||
s->max_transfer = MIN_NON_ZERO(s->max_transfer,
|
s->max_transfer = MIN_NON_ZERO(s->max_transfer,
|
||||||
s->page_size / sizeof(uint64_t) * s->page_size);
|
s->page_size / sizeof(uint64_t) * s->page_size);
|
||||||
|
|
||||||
oncs = le16_to_cpu(idctrl->oncs);
|
oncs = le16_to_cpu(id->ctrl.oncs);
|
||||||
s->supports_write_zeroes = !!(oncs & NVME_ONCS_WRITE_ZEROES);
|
s->supports_write_zeroes = !!(oncs & NVME_ONCS_WRITE_ZEROES);
|
||||||
s->supports_discard = !!(oncs & NVME_ONCS_DSM);
|
s->supports_discard = !!(oncs & NVME_ONCS_DSM);
|
||||||
|
|
||||||
memset(id, 0, 4096);
|
memset(id, 0, sizeof(*id));
|
||||||
cmd.cdw10 = 0;
|
cmd.cdw10 = 0;
|
||||||
cmd.nsid = cpu_to_le32(namespace);
|
cmd.nsid = cpu_to_le32(namespace);
|
||||||
if (nvme_cmd_sync(bs, s->queues[INDEX_ADMIN], &cmd)) {
|
if (nvme_cmd_sync(bs, s->queues[INDEX_ADMIN], &cmd)) {
|
||||||
|
@ -562,11 +561,11 @@ static void nvme_identify(BlockDriverState *bs, int namespace, Error **errp)
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->nsze = le64_to_cpu(idns->nsze);
|
s->nsze = le64_to_cpu(id->ns.nsze);
|
||||||
lbaf = &idns->lbaf[NVME_ID_NS_FLBAS_INDEX(idns->flbas)];
|
lbaf = &id->ns.lbaf[NVME_ID_NS_FLBAS_INDEX(id->ns.flbas)];
|
||||||
|
|
||||||
if (NVME_ID_NS_DLFEAT_WRITE_ZEROES(idns->dlfeat) &&
|
if (NVME_ID_NS_DLFEAT_WRITE_ZEROES(id->ns.dlfeat) &&
|
||||||
NVME_ID_NS_DLFEAT_READ_BEHAVIOR(idns->dlfeat) ==
|
NVME_ID_NS_DLFEAT_READ_BEHAVIOR(id->ns.dlfeat) ==
|
||||||
NVME_ID_NS_DLFEAT_READ_BEHAVIOR_ZEROES) {
|
NVME_ID_NS_DLFEAT_READ_BEHAVIOR_ZEROES) {
|
||||||
bs->supported_write_flags |= BDRV_REQ_MAY_UNMAP;
|
bs->supported_write_flags |= BDRV_REQ_MAY_UNMAP;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue