From 1c2301b05baefe4e224adb93e1b642df4edca3f2 Mon Sep 17 00:00:00 2001 From: John Levon Date: Tue, 13 Apr 2021 14:25:45 +0100 Subject: pci: use ERROR_INT() (#430) Signed-off-by: John Levon Reviewed-by: Thanos Makatos --- lib/libvfio-user.c | 3 +++ lib/pci.c | 21 ++++++++++----------- lib/pci_caps.c | 39 +++++++++++++++++++++------------------ 3 files changed, 34 insertions(+), 29 deletions(-) (limited to 'lib') diff --git a/lib/libvfio-user.c b/lib/libvfio-user.c index b0bfbbe..c86713a 100644 --- a/lib/libvfio-user.c +++ b/lib/libvfio-user.c @@ -216,6 +216,9 @@ region_access(vfu_ctx_t *vfu_ctx, size_t region_index, char *buf, if (region_index == VFU_PCI_DEV_CFG_REGION_IDX) { ret = pci_config_space_access(vfu_ctx, buf, count, offset, is_write); + if (ret == -1) { + ret = -errno; + } } else if (is_migr_reg(vfu_ctx, region_index) && vfu_ctx->migration != NULL) { ret = migration_region_access(vfu_ctx, buf, count, offset, is_write); } else { diff --git a/lib/pci.c b/lib/pci.c index 54e90f9..467d31c 100644 --- a/lib/pci.c +++ b/lib/pci.c @@ -89,7 +89,7 @@ handle_command_write(vfu_ctx_t *ctx, vfu_pci_config_space_t *pci, if (count != 2) { vfu_log(ctx, LOG_ERR, "bad write command size %lu", count); - return -EINVAL; + return ERROR_INT(EINVAL); } assert(pci != NULL); @@ -182,7 +182,7 @@ handle_command_write(vfu_ctx_t *ctx, vfu_pci_config_space_t *pci, if (v != 0) { vfu_log(ctx, LOG_ERR, "unconsumed command flags %x", v); - return -EINVAL; + return ERROR_INT(EINVAL); } return 0; @@ -199,7 +199,7 @@ handle_erom_write(vfu_ctx_t *ctx, vfu_pci_config_space_t *pci, if (count != 0x4) { vfu_log(ctx, LOG_ERR, "bad EROM count %lu", count); - return -EINVAL; + return ERROR_INT(EINVAL); } v = *(uint32_t*)buf; @@ -212,7 +212,7 @@ handle_erom_write(vfu_ctx_t *ctx, vfu_pci_config_space_t *pci, vfu_log(ctx, LOG_INFO, "EROM disable ignored"); } else { vfu_log(ctx, LOG_ERR, "bad write to EROM 0x%x bytes", v); - return -EINVAL; + return ERROR_INT(EINVAL); } return 0; } @@ -237,7 +237,7 @@ pci_hdr_write(vfu_ctx_t *vfu_ctx, const char *buf, size_t count, loff_t offset) break; case PCI_INTERRUPT_PIN: vfu_log(vfu_ctx, LOG_ERR, "attempt to write read-only field IPIN"); - ret = -EINVAL; + ret = ERROR_INT(EINVAL); break; case PCI_INTERRUPT_LINE: cfg_space->hdr.intr.iline = buf[0]; @@ -262,7 +262,7 @@ pci_hdr_write(vfu_ctx_t *vfu_ctx, const char *buf, size_t count, loff_t offset) default: vfu_log(vfu_ctx, LOG_INFO, "PCI config write %#lx-%#lx not handled", offset, offset + count); - ret = -EINVAL; + ret = ERROR_INT(EINVAL); } #ifdef VFU_VERBOSE_LOGGING @@ -286,8 +286,7 @@ pci_hdr_access(vfu_ctx_t *vfu_ctx, char *buf, size_t count, if (is_write) { ret = pci_hdr_write(vfu_ctx, buf, count, offset); if (ret < 0) { - vfu_log(vfu_ctx, LOG_ERR, "failed to write to PCI header: %s", - strerror(-ret)); + vfu_log(vfu_ctx, LOG_ERR, "failed to write to PCI header: %m"); } else { dump_buffer("buffer write", buf, count); ret = count; @@ -318,7 +317,7 @@ pci_nonstd_access(vfu_ctx_t *vfu_ctx, char *buf, size_t count, if (is_write) { vfu_log(vfu_ctx, LOG_ERR, "no callback for write to config space " "offset %lu size %zu", offset, count); - return -EINVAL; + return ERROR_INT(EINVAL); } memcpy(buf, pci_config_space_ptr(vfu_ctx, offset), count); @@ -368,7 +367,7 @@ pci_config_space_next_segment(vfu_ctx_t *ctx, size_t count, loff_t offset, * that; otherwise, writes are not allowed, and reads are satisfied with * memcpy(). * - * Returns the number of bytes handled, or -errno on error. + * Returns the number of bytes handled, or -1 and errno on error. */ ssize_t pci_config_space_access(vfu_ctx_t *vfu_ctx, char *buf, size_t count, @@ -469,7 +468,7 @@ vfu_pci_set_class(vfu_ctx_t *vfu_ctx, uint8_t base, uint8_t sub, uint8_t pi) vfu_ctx->pci.config_space->hdr.cc.pi = pi; } -inline vfu_pci_config_space_t * +vfu_pci_config_space_t * vfu_pci_get_config_space(vfu_ctx_t *vfu_ctx) { assert(vfu_ctx != NULL); diff --git a/lib/pci_caps.c b/lib/pci_caps.c index 7ea8be2..f523b98 100644 --- a/lib/pci_caps.c +++ b/lib/pci_caps.c @@ -139,18 +139,18 @@ cap_write_pm(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, char * buf, switch (offset - cap->off) { case offsetof(struct pmcap, pc): if (count != sizeof(struct pc)) { - return -EINVAL; + return ERROR_INT(EINVAL); } assert(false); /* FIXME implement */ break; case offsetof(struct pmcap, pmcs): if (count != sizeof(struct pmcs)) { - return -EINVAL; + return ERROR_INT(EINVAL); } handle_pmcs_write(vfu_ctx, pm, (struct pmcs *)buf); return sizeof(struct pmcs); } - return -EINVAL; + return ERROR_INT(EINVAL); } static ssize_t @@ -192,11 +192,11 @@ cap_write_msix(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, char *buf, default: vfu_log(vfu_ctx, LOG_ERR, "invalid MSI-X write offset %ld", offset); - return -EINVAL; + return ERROR_INT(EINVAL); } } vfu_log(vfu_ctx, LOG_ERR, "invalid MSI-X write size %lu", count); - return -EINVAL; + return ERROR_INT(EINVAL); } static int @@ -279,7 +279,7 @@ handle_px_write_2_bytes(vfu_ctx_t *vfu_ctx, struct pxcap *px, char *buf, case offsetof(struct pxcap, pxdc): return handle_px_pxdc_write(vfu_ctx, px, (union pxdc *)buf); } - return -EINVAL; + return ERROR_INT(EINVAL); } static ssize_t @@ -287,12 +287,15 @@ cap_write_px(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, char *buf, size_t count, loff_t offset) { struct pxcap *px = cap_data(vfu_ctx, cap); + int err; - int err = -EINVAL; switch (count) { case 2: err = handle_px_write_2_bytes(vfu_ctx, px, buf, offset - cap->off); break; + default: + err = ERROR_INT(EINVAL); + break; } if (err != 0) { return err; @@ -313,7 +316,7 @@ ext_cap_write_dsn(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, char *buf UNUSED, size_t count UNUSED, loff_t offset UNUSED) { vfu_log(vfu_ctx, LOG_ERR, "%s capability is read-only", cap->name); - return -EPERM; + return ERROR_INT(EPERM); } static ssize_t @@ -364,7 +367,7 @@ pci_cap_access(vfu_ctx_t *vfu_ctx, char *buf, size_t count, loff_t offset, if (is_write && (cap->flags & VFU_CAP_FLAG_READONLY)) { vfu_log(vfu_ctx, LOG_ERR, "write of %zu bytes to read-only capability " "%u (%s)", count, cap->id, cap->name); - return -EPERM; + return ERROR_INT(EPERM); } if (cap->flags & VFU_CAP_FLAG_CALLBACK) { @@ -380,7 +383,7 @@ pci_cap_access(vfu_ctx_t *vfu_ctx, char *buf, size_t count, loff_t offset, vfu_log(vfu_ctx, LOG_ERR, "disallowed write to header for cap %d (%s)", cap->id, cap->name); - return -EPERM; + return ERROR_INT(EPERM); } return cap->cb(vfu_ctx, cap, buf, count, offset); @@ -410,13 +413,13 @@ cap_place(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, void *data) if (cap->off < PCI_STD_HEADER_SIZEOF) { vfu_log(vfu_ctx, LOG_ERR, "invalid offset %#lx for capability " "%u (%s)", cap->off, cap->id, cap->name); - return EINVAL; + return ERROR_INT(EINVAL); } if (cap_find_by_offset(vfu_ctx, cap->off, cap->size) != NULL) { vfu_log(vfu_ctx, LOG_ERR, "overlap found for capability " "%u (%s)", cap->id, cap->name); - return EINVAL; + return ERROR_INT(EINVAL); } while (*prevp != 0) { @@ -443,7 +446,7 @@ cap_place(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, void *data) vfu_log(vfu_ctx, LOG_ERR, "no config space left for capability " "%u (%s) of size %zu bytes at offset %#lx", cap->id, cap->name, cap->size, cap->off); - return ENOSPC; + return ERROR_INT(ENOSPC); } memcpy(cap_data(vfu_ctx, cap), data, cap->size); @@ -474,19 +477,19 @@ ext_cap_place(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, void *data) if (cap->off < PCI_CFG_SPACE_SIZE) { vfu_log(vfu_ctx, LOG_ERR, "invalid offset %#lx for capability " "%u (%s)", cap->off, cap->id, cap->name); - return EINVAL; + return ERROR_INT(EINVAL); } if (cap_find_by_offset(vfu_ctx, cap->off, cap->size) != NULL) { vfu_log(vfu_ctx, LOG_ERR, "overlap found for capability " "%u (%s)", cap->id, cap->name); - return EINVAL; + return ERROR_INT(EINVAL); } if (hdr->id == 0x0 && cap->off != PCI_CFG_SPACE_SIZE) { vfu_log(vfu_ctx, LOG_ERR, "first extended capability must be at " "%#x", PCI_CFG_SPACE_SIZE); - return EINVAL; + return ERROR_INT(EINVAL); } while (hdr->next != 0) { @@ -508,7 +511,7 @@ ext_cap_place(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, void *data) vfu_log(vfu_ctx, LOG_ERR, "no config space left for capability " "%u (%s) of size %zu bytes at offset %#lx", cap->id, cap->name, cap->size, cap->off); - return ENOSPC; + return ERROR_INT(ENOSPC); } memcpy(cap_data(vfu_ctx, cap), data, cap->size); @@ -627,7 +630,7 @@ vfu_pci_add_capability(vfu_ctx_t *vfu_ctx, size_t pos, int flags, void *data) } if (ret != 0) { - return ERROR_INT(ret); + return ret; } if (extended) { -- cgit v1.1