Skip to contentSkip to conversation

hw/dma: Use dma_addr_t type definition when relevant

PATCH Superseded Philippe Mathieu-Daudé 5 participants 30 messages Jan 4, 2022 – Jan 11, 2022 Arm/Amlogic Meson SoC support

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover hw/dma: Use dma_addr_t type definition when relevant

Since v1:
- Addressed David review comment (stick to dma_addr_t type)
- Addressed Peter review comment (incorrect doc string)

Hi,

This series aims to clarify a bit the DMA API, in particular the 'size' argument which is not clear enough (as we use multiple types for it). This helps avoiding build failures on 32-bit host [*] (and likely overflows calculation too IMO).

Some units using the DMA API are first removed from user-mode emulation to avoid build failure (they shouldn't be there in the first place).

Then some variables are renamed for clarity (no functional change).

Finally we replace misuses with dma_addr_t typedef. The previous patch which failed on 32-bit host applied on top (not failing anymore).

Regards,

Phil.

[*] https://www.mail-archive.com/[email protected]/msg858825.html

Supersedes: <[email protected]>

Philippe Mathieu-Daudé (9):
hw/nvram: Restrict stub to sysemu and tools
hw/pci: Restrict pci-bus stub to sysemu
hw/pci: Document pci_dma_map()
hw/dma: Remove CONFIG_USER_ONLY check hw/rdma/rdma_utils: Rename rdma_pci_dma_map 'len' argument hw/scsi: Rename SCSIRequest::resid as 'residual' hw/dma: Fix format string issues using dma_addr_t hw/dma: Use dma_addr_t type definition when relevant hw/dma: Let dma_buf_read() / dma_buf_write() propagate MemTxResult

 hw/rdma/rdma_utils.h   |  2 +-
 include/hw/pci/pci.h   | 12 ++++++
 include/hw/scsi/scsi.h |  4 +-
 include/sysemu/dma.h   | 31 ++++++++-------
 hw/ide/ahci.c          | 10 ++---
 hw/nvme/ctrl.c         |  6 +--
 hw/rdma/rdma_utils.c   | 14 +++----
 hw/scsi/megasas.c      | 85 +++++++++++++++++++++++++++++-------------
 hw/scsi/scsi-bus.c     | 12 +++---
 hw/scsi/scsi-disk.c    |  4 +-
 softmmu/dma-helpers.c  | 34 +++++++----------
 hw/nvram/meson.build   |  6 ++-
 hw/rdma/trace-events   |  2 +-
 stubs/meson.build      |  4 +-
 14 files changed, 134 insertions(+), 92 deletions(-)
Signature · 1 line
2.33.1
#1 lore raw

PATCH v2 1/9 hw/nvram: Restrict stub to sysemu and tools

From: Philippe Mathieu-Daudé <[email protected]>

User-mode emulation doesn't need the fw_cfg stub.

---
 hw/nvram/meson.build | 6 ++++--
 stubs/meson.build    | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)
hw/nvram/meson.build +4 -2
index 202a5466e63..f5ee9f6b88c 100644
@@ -1,5 +1,7 @@
-# QOM interfaces must be available anytime QOM is used.
-qom_ss.add(files('fw_cfg-interface.c'))
+if have_system or have_tools
+  # QOM interfaces must be available anytime QOM is used.
+  qom_ss.add(files('fw_cfg-interface.c'))
+endif
 
 softmmu_ss.add(files('fw_cfg.c'))
 softmmu_ss.add(when: 'CONFIG_CHRP_NVRAM', if_true: files('chrp_nvram.c'))
stubs/meson.build +1 -1
index 71469c1d50a..363f6fa785d 100644
@@ -11,7 +11,6 @@
 stub_ss.add(files('dump.c'))
 stub_ss.add(files('error-printf.c'))
 stub_ss.add(files('fdset.c'))
-stub_ss.add(files('fw_cfg.c'))
 stub_ss.add(files('gdbstub.c'))
 stub_ss.add(files('get-vm-name.c'))
 if linux_io_uring.found()
@@ -51,6 +50,7 @@
   stub_ss.add(files('replay-tools.c'))
 endif
 if have_system
+  stub_ss.add(files('fw_cfg.c'))
   stub_ss.add(files('semihost.c'))
   stub_ss.add(files('usb-dev-stub.c'))
   stub_ss.add(files('xen-hw-stub.c'))
Signature · 1 line
2.33.1
#2 lore raw

Re: [PATCH v2 1/9] hw/nvram: Restrict stub to sysemu and tools

Quoted 1 line · Philippe Mathieu-Daudé

+if have_system or have_tools

...

Quoted 2 lines

if have_system
+ stub_ss.add(files('fw_cfg.c'))

Disconnect in tests?

r~

#3 lore raw

Re: [PATCH v2 1/9] hw/nvram: Restrict stub to sysemu and tools

Quoted 7 lines · Richard Henderson
Quoted 1 line · Philippe Mathieu-Daudé

+if have_system or have_tools

...

Quoted 2 lines

  if have_system
+  stub_ss.add(files('fw_cfg.c'))

Disconnect in tests?

(I assume s/tests/stubs/).

fw_cfg-interface.c exposes the fw_cfg QOM interface, which is not used by tools, but since interfaces are checked when QOM object is created, we must include it to be able to use qemu-storage-daemon.

fw_cfg.c add a fw_cfg_arch_key_name() stub which is only required for sysemu.

So no, there is no disconnect. I will however split this patch in 2 and better describe each change.

Thanks,

Phil.

#4 lore raw

PATCH v2 2/9 hw/pci: Restrict pci-bus stub to sysemu

In reply to Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

Neither tools nor user-mode emulation require the PCI bus stub.

---
 stubs/meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
stubs/meson.build +1 -1
index 363f6fa785d..d359cbe1ad7 100644
@@ -26,7 +26,6 @@
 stub_ss.add(files('module-opts.c'))
 stub_ss.add(files('monitor.c'))
 stub_ss.add(files('monitor-core.c'))
-stub_ss.add(files('pci-bus.c'))
 stub_ss.add(files('qemu-timer-notify-cb.c'))
 stub_ss.add(files('qmp_memory_device.c'))
 stub_ss.add(files('qmp-command-available.c'))
@@ -51,6 +50,7 @@
 endif
 if have_system
   stub_ss.add(files('fw_cfg.c'))
+  stub_ss.add(files('pci-bus.c'))
   stub_ss.add(files('semihost.c'))
   stub_ss.add(files('usb-dev-stub.c'))
   stub_ss.add(files('xen-hw-stub.c'))
Signature · 1 line
2.33.1
Reviewed-by on v2 2/9
#5 lore raw

Re: [PATCH v2 2/9] hw/pci: Restrict pci-bus stub to sysemu

Quoted 9 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé<[email protected]>

Neither tools nor user-mode emulation require the PCI bus stub.

---
  stubs/meson.build | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~

Reviewed-by on v2 2/9
#6 lore raw

Re: [PATCH v2 2/9] hw/pci: Restrict pci-bus stub to sysemu

In reply to Philippe Mathieu-Daudé
Quoted 23 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

Neither tools nor user-mode emulation require the PCI bus stub.

---
 stubs/meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
stubs/meson.build +0 -1
index 363f6fa785d..d359cbe1ad7 100644
@@ -26,7 +26,6 @@
 stub_ss.add(files('module-opts.c'))
 stub_ss.add(files('monitor.c'))
 stub_ss.add(files('monitor-core.c'))
-stub_ss.add(files('pci-bus.c'))
 stub_ss.add(files('qemu-timer-notify-cb.c'))
 stub_ss.add(files('qmp_memory_device.c'))
 stub_ss.add(files('qmp-command-available.c'))

stubs/meson.build line 56

Quoted 7 lines
stubs/meson.build +1 -0
@@ -51,6 +50,7 @@
 endif
 if have_system
   stub_ss.add(files('fw_cfg.c'))
+  stub_ss.add(files('pci-bus.c'))
   stub_ss.add(files('semihost.c'))
   stub_ss.add(files('usb-dev-stub.c'))
   stub_ss.add(files('xen-hw-stub.c'))
Signature · 3 lines
Thanks,

David / dhildenb
#7 lore raw

PATCH v2 3/9 hw/pci: Document pci_dma_map()

In reply to Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

---
 include/hw/pci/pci.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/hw/pci/pci.h +12 -0
index 5b36334a28a..07f08aa0626 100644
@@ -876,6 +876,18 @@ PCI_DMA_DEFINE_LDST(q_be, q_be, 64);
 
 #undef PCI_DMA_DEFINE_LDST
 
+/**
+ * pci_dma_map: Map device PCI address space range into host virtual address
+ *
+ * May map a subset of the requested range, given by and returned in @plen.
+ * May return %NULL and set *@plen to zero(0), if resources needed to perform
+ * the mapping are exhausted.
+ *
+ * @dev: #PCIDevice to be accessed
+ * @addr: address within that device's address space
+ * @plen: pointer to length of buffer; updated on return
+ * @dir: indicates the transfer direction
+ */
 static inline void *pci_dma_map(PCIDevice *dev, dma_addr_t addr,
                                 dma_addr_t *plen, DMADirection dir)
 {
Signature · 1 line
2.33.1
Reviewed-by on v2 3/9
#8 lore raw

Re: [PATCH v2 3/9] hw/pci: Document pci_dma_map()

Quoted 18 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

---
  include/hw/pci/pci.h | 12 ++++++++++++
  1 file changed, 12 insertions(+)
include/hw/pci/pci.h +2 -0
index 5b36334a28a..07f08aa0626 100644
@@ -876,6 +876,18 @@ PCI_DMA_DEFINE_LDST(q_be, q_be, 64);
 
  #undef PCI_DMA_DEFINE_LDST
 
+/**
+ * pci_dma_map: Map device PCI address space range into host virtual address

include/hw/pci/pci.h PCI_DMA_DEFINE_LDST(q_be, q_be, 64); · line 890

Quoted 10 lines
include/hw/pci/pci.h +10 -0
+ *
+ * May map a subset of the requested range, given by and returned in @plen.
+ * May return %NULL and set *@plen to zero(0), if resources needed to perform
+ * the mapping are exhausted.
+ *
+ * @dev: #PCIDevice to be accessed
+ * @addr: address within that device's address space
+ * @plen: pointer to length of buffer; updated on return
+ * @dir: indicates the transfer direction
+ */

As Peter recently mentioned, the ** format is off. The description goes below the arguments.

Otherwise,

r~

#9 lore raw

PATCH v2 4/9 hw/dma: Remove CONFIG_USER_ONLY check

In reply to Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

DMA API should not be included in user-mode emulation. If so, build should fail. Remove the CONFIG_USER_ONLY check.

---
 include/sysemu/dma.h | 3 ---
 1 file changed, 3 deletions(-)
include/sysemu/dma.h +0 -3
index b3faef41b2f..0db2478a506 100644
@@ -31,8 +31,6 @@ struct QEMUSGList {
     AddressSpace *as;
 };
 
-#ifndef CONFIG_USER_ONLY
-
 /*
  * When an IOMMU is present, bus addresses become distinct from
  * CPU/memory physical addresses and may be a different size.  Because
@@ -288,7 +286,6 @@ void qemu_sglist_init(QEMUSGList *qsg, DeviceState *dev, int alloc_hint,
                       AddressSpace *as);
 void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len);
 void qemu_sglist_destroy(QEMUSGList *qsg);
-#endif
 
 typedef BlockAIOCB *DMAIOFunc(int64_t offset, QEMUIOVector *iov,
                               BlockCompletionFunc *cb, void *cb_opaque,
Signature · 1 line
2.33.1
Reviewed-by on v2 4/9
#10 lore raw

Re: [PATCH v2 4/9] hw/dma: Remove CONFIG_USER_ONLY check

Quoted 10 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé<[email protected]>

DMA API should not be included in user-mode emulation. If so, build should fail. Remove the CONFIG_USER_ONLY check.

---
  include/sysemu/dma.h | 3 ---
  1 file changed, 3 deletions(-)

r~

Reviewed-by on v2 4/9
#11 lore raw

Re: [PATCH v2 4/9] hw/dma: Remove CONFIG_USER_ONLY check

In reply to Philippe Mathieu-Daudé
Quoted 25 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

DMA API should not be included in user-mode emulation. If so, build should fail. Remove the CONFIG_USER_ONLY check.

---
 include/sysemu/dma.h | 3 ---
 1 file changed, 3 deletions(-)
include/sysemu/dma.h +0 -2
index b3faef41b2f..0db2478a506 100644
@@ -31,8 +31,6 @@ struct QEMUSGList {
     AddressSpace *as;
 };
 
-#ifndef CONFIG_USER_ONLY
-
 /*
  * When an IOMMU is present, bus addresses become distinct from
  * CPU/memory physical addresses and may be a different size.  Because

include/sysemu/dma.h void qemu_sglist_init(QEMUSGList *qsg, DeviceState *dev, int alloc_hint, · line 291

Quoted 7 lines
include/sysemu/dma.h +0 -1
@@ -288,7 +286,6 @@ void qemu_sglist_init(QEMUSGList *qsg, DeviceState *dev, int alloc_hint,
                       AddressSpace *as);
 void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len);
 void qemu_sglist_destroy(QEMUSGList *qsg);
-#endif
 
 typedef BlockAIOCB *DMAIOFunc(int64_t offset, QEMUIOVector *iov,
                               BlockCompletionFunc *cb, void *cb_opaque,
Signature · 3 lines
Thanks,

David / dhildenb
#12 lore raw

PATCH v2 5/9 hw/rdma/rdma_utils: Rename rdma_pci_dma_map 'len' argument

In reply to Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

Various APIs use 'pval' naming for 'pointer to val'. rdma_pci_dma_map() uses 'plen' for 'PCI length', but since 'PCI' is already explicit in the function name, simplify and rename the argument 'len'. No logical change.

---
 hw/rdma/rdma_utils.h |  2 +-
 hw/rdma/rdma_utils.c | 14 +++++++-------
 2 files changed, 8 insertions(+), 8 deletions(-)
hw/rdma/rdma_utils.h +1 -1
index 9fd0efd940b..0c6414e7e0a 100644
@@ -38,7 +38,7 @@ typedef struct RdmaProtectedGSList {
     GSList *list;
 } RdmaProtectedGSList;
 
-void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t plen);
+void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t len);
 void rdma_pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len);
 void rdma_protected_gqueue_init(RdmaProtectedGQueue *list);
 void rdma_protected_gqueue_destroy(RdmaProtectedGQueue *list);
hw/rdma/rdma_utils.c +7 -7
index 98df58f6897..61cb8ede0fd 100644
@@ -17,29 +17,29 @@
 #include "trace.h"
 #include "rdma_utils.h"
 
-void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t plen)
+void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t len)
 {
     void *p;
-    hwaddr len = plen;
+    hwaddr pci_len = len;
 
     if (!addr) {
         rdma_error_report("addr is NULL");
         return NULL;
     }
 
-    p = pci_dma_map(dev, addr, &len, DMA_DIRECTION_TO_DEVICE);
+    p = pci_dma_map(dev, addr, &pci_len, DMA_DIRECTION_TO_DEVICE);
     if (!p) {
         rdma_error_report("pci_dma_map fail, addr=0x%"PRIx64", len=%"PRId64,
-                          addr, len);
+                          addr, pci_len);
         return NULL;
     }
 
-    if (len != plen) {
-        rdma_pci_dma_unmap(dev, p, len);
+    if (pci_len != len) {
+        rdma_pci_dma_unmap(dev, p, pci_len);
         return NULL;
     }
 
-    trace_rdma_pci_dma_map(addr, p, len);
+    trace_rdma_pci_dma_map(addr, p, pci_len);
 
     return p;
 }
Signature · 1 line
2.33.1
Reviewed-by on v2 5/9
#13 lore raw

Re: [PATCH v2 5/9] hw/rdma/rdma_utils: Rename rdma_pci_dma_map 'len' argument

Quoted 7 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé<[email protected]>

Various APIs use 'pval' naming for 'pointer to val'. rdma_pci_dma_map() uses 'plen' for 'PCI length', but since 'PCI' is already explicit in the function name, simplify and rename the argument 'len'. No logical change.

Quoted 6 lines
---
  hw/rdma/rdma_utils.h |  2 +-
  hw/rdma/rdma_utils.c | 14 +++++++-------
  2 files changed, 8 insertions(+), 8 deletions(-)

r~

Reviewed-by on v2 5/9
#14 lore raw

Re: [PATCH v2 5/9] hw/rdma/rdma_utils: Rename rdma_pci_dma_map 'len' argument

In reply to Philippe Mathieu-Daudé
Quoted 58 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

Various APIs use 'pval' naming for 'pointer to val'. rdma_pci_dma_map() uses 'plen' for 'PCI length', but since 'PCI' is already explicit in the function name, simplify and rename the argument 'len'. No logical change.

---
 hw/rdma/rdma_utils.h |  2 +-
 hw/rdma/rdma_utils.c | 14 +++++++-------
 2 files changed, 8 insertions(+), 8 deletions(-)
hw/rdma/rdma_utils.h +1 -1
index 9fd0efd940b..0c6414e7e0a 100644
@@ -38,7 +38,7 @@ typedef struct RdmaProtectedGSList {
     GSList *list;
 } RdmaProtectedGSList;
 
-void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t plen);
+void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t len);
 void rdma_pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len);
 void rdma_protected_gqueue_init(RdmaProtectedGQueue *list);
 void rdma_protected_gqueue_destroy(RdmaProtectedGQueue *list);
hw/rdma/rdma_utils.c +4 -6
index 98df58f6897..61cb8ede0fd 100644
@@ -17,29 +17,29 @@
 #include "trace.h"
 #include "rdma_utils.h"
 
-void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t plen)
+void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t len)
 {
     void *p;
-    hwaddr len = plen;
+    hwaddr pci_len = len;
 
     if (!addr) {
         rdma_error_report("addr is NULL");
         return NULL;
     }
 
-    p = pci_dma_map(dev, addr, &len, DMA_DIRECTION_TO_DEVICE);
+    p = pci_dma_map(dev, addr, &pci_len, DMA_DIRECTION_TO_DEVICE);
     if (!p) {
         rdma_error_report("pci_dma_map fail, addr=0x%"PRIx64", len=%"PRId64,
-                          addr, len);
+                          addr, pci_len);
         return NULL;
     }
 
-    if (len != plen) {
-        rdma_pci_dma_unmap(dev, p, len);

hw/rdma/rdma_utils.c line 45

Quoted 10 lines
hw/rdma/rdma_utils.c +3 -1
+    if (pci_len != len) {
+        rdma_pci_dma_unmap(dev, p, pci_len);
         return NULL;
     }
 
-    trace_rdma_pci_dma_map(addr, p, len);
+    trace_rdma_pci_dma_map(addr, p, pci_len);
 
     return p;
 }
Signature · 3 lines
Thanks,

David / dhildenb
#15 lore raw

PATCH v2 6/9 hw/scsi: Rename SCSIRequest::resid as 'residual'

In reply to Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

The 'resid' field is slightly confusing and could be interpreted as some ID. Rename it as 'residual' which is clearer to review. No logical change.

---
 include/hw/scsi/scsi.h |  4 ++--
 hw/scsi/megasas.c      | 42 +++++++++++++++++++++++++-----------------
 hw/scsi/scsi-bus.c     | 10 +++++-----
 hw/scsi/scsi-disk.c    |  4 ++--
 softmmu/dma-helpers.c  | 26 +++++++++++++-------------
 5 files changed, 47 insertions(+), 39 deletions(-)
include/hw/scsi/scsi.h +2 -2
index 2ef80af6dca..b27d133b113 100644
@@ -30,7 +30,7 @@ struct SCSIRequest {
     int16_t           status;
     int16_t           host_status;
     void              *hba_private;
-    size_t            resid;
+    size_t            residual;
     SCSICommand       cmd;
     NotifierList      cancel_notifiers;
 
@@ -125,7 +125,7 @@ struct SCSIBusInfo {
                      void *hba_private);
     void (*transfer_data)(SCSIRequest *req, uint32_t arg);
     void (*fail)(SCSIRequest *req);
-    void (*complete)(SCSIRequest *req, size_t resid);
+    void (*complete)(SCSIRequest *req, size_t residual);
     void (*cancel)(SCSIRequest *req);
     void (*change)(SCSIBus *bus, SCSIDevice *dev, SCSISense sense);
     QEMUSGList *(*get_sg_list)(SCSIRequest *req);
hw/scsi/megasas.c +25 -17

119 lines not on this page · raw message

hw/scsi/scsi-bus.c +5 -5
index 2b5e9dca311..3466e680ac7 100644
@@ -760,7 +760,7 @@ SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
     }
 
     req->cmd = cmd;
-    req->resid = req->cmd.xfer;
+    req->residual = req->cmd.xfer;
 
     switch (buf[0]) {
     case INQUIRY:
@@ -1408,7 +1408,7 @@ void scsi_req_data(SCSIRequest *req, int len)
     trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
     assert(req->cmd.mode != SCSI_XFER_NONE);
     if (!req->sg) {
-        req->resid -= len;
+        req->residual -= len;
         req->bus->info->transfer_data(req, len);
         return;
     }
@@ -1421,9 +1421,9 @@ void scsi_req_data(SCSIRequest *req, int len)
 
     buf = scsi_req_get_buf(req);
     if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
-        req->resid = dma_buf_read(buf, len, req->sg, MEMTXATTRS_UNSPECIFIED);
+        req->residual = dma_buf_read(buf, len, req->sg, MEMTXATTRS_UNSPECIFIED);
     } else {
-        req->resid = dma_buf_write(buf, len, req->sg, MEMTXATTRS_UNSPECIFIED);
+        req->residual = dma_buf_write(buf, len, req->sg, MEMTXATTRS_UNSPECIFIED);
     }
     scsi_req_continue(req);
 }
@@ -1512,7 +1512,7 @@ void scsi_req_complete(SCSIRequest *req, int status)
 
     scsi_req_ref(req);
     scsi_req_dequeue(req);
-    req->bus->info->complete(req, req->resid);
+    req->bus->info->complete(req, req->residual);
 
     /* Cancelled requests might end up being completed instead of cancelled */
     notifier_list_notify(&req->cancel_notifiers, req);
hw/scsi/scsi-disk.c +2 -2
index d4914178ea0..9c0dc7b9468 100644
@@ -420,7 +420,7 @@ static void scsi_do_read(SCSIDiskReq *r, int ret)
 
     if (r->req.sg) {
         dma_acct_start(s->qdev.conf.blk, &r->acct, r->req.sg, BLOCK_ACCT_READ);
-        r->req.resid -= r->req.sg->size;
+        r->req.residual -= r->req.sg->size;
         r->req.aiocb = dma_blk_io(blk_get_aio_context(s->qdev.conf.blk),
                                   r->req.sg, r->sector << BDRV_SECTOR_BITS,
                                   BDRV_SECTOR_SIZE,
@@ -580,7 +580,7 @@ static void scsi_write_data(SCSIRequest *req)
 
     if (r->req.sg) {
         dma_acct_start(s->qdev.conf.blk, &r->acct, r->req.sg, BLOCK_ACCT_WRITE);
-        r->req.resid -= r->req.sg->size;
+        r->req.residual -= r->req.sg->size;
         r->req.aiocb = dma_blk_io(blk_get_aio_context(s->qdev.conf.blk),
                                   r->req.sg, r->sector << BDRV_SECTOR_BITS,
                                   BDRV_SECTOR_SIZE,
softmmu/dma-helpers.c +13 -13
index b0be1564797..4563a775aa7 100644
@@ -294,49 +294,49 @@ BlockAIOCB *dma_blk_write(BlockBackend *blk,
 }
 
 
-static MemTxResult dma_buf_rw(void *buf, int32_t len, uint64_t *residp,
+static MemTxResult dma_buf_rw(void *buf, int32_t len, uint64_t *residual,
                               QEMUSGList *sg, DMADirection dir,
                               MemTxAttrs attrs)
 {
     uint8_t *ptr = buf;
-    uint64_t resid;
+    uint64_t xresidual;
     int sg_cur_index;
     MemTxResult res = MEMTX_OK;
 
-    resid = sg->size;
+    xresidual = sg->size;
     sg_cur_index = 0;
-    len = MIN(len, resid);
+    len = MIN(len, xresidual);
     while (len > 0) {
         ScatterGatherEntry entry = sg->sg[sg_cur_index++];
         int32_t xfer = MIN(len, entry.len);
         res |= dma_memory_rw(sg->as, entry.base, ptr, xfer, dir, attrs);
         ptr += xfer;
         len -= xfer;
-        resid -= xfer;
+        xresidual -= xfer;
     }
 
-    if (residp) {
-        *residp = resid;
+    if (residual) {
+        *residual = xresidual;
     }
     return res;
 }
 
 uint64_t dma_buf_read(void *ptr, int32_t len, QEMUSGList *sg, MemTxAttrs attrs)
 {
-    uint64_t resid;
+    uint64_t residual;
 
-    dma_buf_rw(ptr, len, &resid, sg, DMA_DIRECTION_FROM_DEVICE, attrs);
+    dma_buf_rw(ptr, len, &residual, sg, DMA_DIRECTION_FROM_DEVICE, attrs);
 
-    return resid;
+    return residual;
 }
 
 uint64_t dma_buf_write(void *ptr, int32_t len, QEMUSGList *sg, MemTxAttrs attrs)
 {
-    uint64_t resid;
+    uint64_t residual;
 
-    dma_buf_rw(ptr, len, &resid, sg, DMA_DIRECTION_TO_DEVICE, attrs);
+    dma_buf_rw(ptr, len, &residual, sg, DMA_DIRECTION_TO_DEVICE, attrs);
 
-    return resid;
+    return residual;
 }
 
 void dma_acct_start(BlockBackend *blk, BlockAcctCookie *cookie,
Signature · 1 line
2.33.1
Reviewed-by on v2 6/9
#16 lore raw

Re: [PATCH v2 6/9] hw/scsi: Rename SCSIRequest::resid as 'residual'

Quoted 8 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé<[email protected]>

The 'resid' field is slightly confusing and could be interpreted as some ID. Rename it as 'residual' which is clearer to review. No logical change.

Quoted 7 lines
---
  include/hw/scsi/scsi.h |  4 ++--
  hw/scsi/megasas.c      | 42 +++++++++++++++++++++++++-----------------
  hw/scsi/scsi-bus.c     | 10 +++++-----
  hw/scsi/scsi-disk.c    |  4 ++--
  softmmu/dma-helpers.c  | 26 +++++++++++++-------------
  5 files changed, 47 insertions(+), 39 deletions(-)

r~

Reviewed-by on v2 6/9
#17 lore raw

Re: [PATCH v2 6/9] hw/scsi: Rename SCSIRequest::resid as 'residual'

In reply to Philippe Mathieu-Daudé
Quoted 9 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

The 'resid' field is slightly confusing and could be interpreted as some ID. Rename it as 'residual' which is clearer to review. No logical change.

---

Signature · 3 lines
Thanks,

David / dhildenb
#18 lore raw

PATCH v2 7/9 hw/dma: Fix format string issues using dma_addr_t

In reply to Philippe Mathieu-Daudé
---
 hw/ide/ahci.c        | 2 +-
 hw/rdma/trace-events | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/ide/ahci.c +1 -1
index 205dfdc6622..6c727dd0c08 100644
@@ -1159,7 +1159,7 @@ static void process_ncq_command(AHCIState *s, int port, const uint8_t *cmd_fis,
     ahci_populate_sglist(ad, &ncq_tfs->sglist, ncq_tfs->cmdh, size, 0);
 
     if (ncq_tfs->sglist.size < size) {
-        error_report("ahci: PRDT length for NCQ command (0x%zx) "
+        error_report("ahci: PRDT length for NCQ command (0x" DMA_ADDR_FMT ") "
                      "is smaller than the requested size (0x%zx)",
                      ncq_tfs->sglist.size, size);
         ncq_err(ncq_tfs);
hw/rdma/trace-events +1 -1
index 9accb149734..c23175120e1 100644
@@ -27,5 +27,5 @@ rdma_rm_alloc_qp(uint32_t rm_qpn, uint32_t backend_qpn, uint8_t qp_type) "rm_qpn
 rdma_rm_modify_qp(uint32_t qpn, uint32_t attr_mask, int qp_state, uint8_t sgid_idx) "qpn=0x%x, attr_mask=0x%x, qp_state=%d, sgid_idx=%d"
 
 # rdma_utils.c
-rdma_pci_dma_map(uint64_t addr, void *vaddr, uint64_t len) "0x%"PRIx64" -> %p (len=%" PRId64")"
+rdma_pci_dma_map(uint64_t addr, void *vaddr, uint64_t len) "0x%"PRIx64" -> %p (len=%" PRIu64")"
 rdma_pci_dma_unmap(void *vaddr) "%p"
Signature · 1 line
2.33.1
Reviewed-by on v2 7/9
#20 lore raw

Re: [PATCH v2 7/9] hw/dma: Fix format string issues using dma_addr_t

In reply to Philippe Mathieu-Daudé
Quoted 24 lines · Philippe Mathieu-Daudé
---
 hw/ide/ahci.c        | 2 +-
 hw/rdma/trace-events | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/ide/ahci.c +1 -1
index 205dfdc6622..6c727dd0c08 100644
@@ -1159,7 +1159,7 @@ static void process_ncq_command(AHCIState *s, int port, const uint8_t *cmd_fis,
     ahci_populate_sglist(ad, &ncq_tfs->sglist, ncq_tfs->cmdh, size, 0);
 
     if (ncq_tfs->sglist.size < size) {
-        error_report("ahci: PRDT length for NCQ command (0x%zx) "
+        error_report("ahci: PRDT length for NCQ command (0x" DMA_ADDR_FMT ") "
                      "is smaller than the requested size (0x%zx)",
                      ncq_tfs->sglist.size, size);
         ncq_err(ncq_tfs);
hw/rdma/trace-events +0 -0
index 9accb149734..c23175120e1 100644

hw/rdma/trace-events rdma_rm_alloc_qp(uint32_t rm_qpn, uint32_t backend_qpn, uint8_t qp_type) "rm_qpn · line 31

Quoted 6 lines
hw/rdma/trace-events +1 -1
@@ -27,5 +27,5 @@ rdma_rm_alloc_qp(uint32_t rm_qpn, uint32_t backend_qpn, uint8_t qp_type) "rm_qpn
 rdma_rm_modify_qp(uint32_t qpn, uint32_t attr_mask, int qp_state, uint8_t sgid_idx) "qpn=0x%x, attr_mask=0x%x, qp_state=%d, sgid_idx=%d"
 
 # rdma_utils.c
-rdma_pci_dma_map(uint64_t addr, void *vaddr, uint64_t len) "0x%"PRIx64" -> %p (len=%" PRId64")"
+rdma_pci_dma_map(uint64_t addr, void *vaddr, uint64_t len) "0x%"PRIx64" -> %p (len=%" PRIu64")"
 rdma_pci_dma_unmap(void *vaddr) "%p"
Signature · 3 lines
Thanks,

David / dhildenb
#21 lore raw

PATCH v2 8/9 hw/dma: Use dma_addr_t type definition when relevant

In reply to Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

Update the obvious places where dma_addr_t should be used (instead of uint64_t, hwaddr, size_t, int32_t types).

This allows to have &dma_addr_t type portable on 32/64-bit hosts.

Move QEMUSGList declaration after dma_addr_t declaration so this structure can use the new type.

---
 include/sysemu/dma.h  | 22 +++++++++++-----------
 hw/nvme/ctrl.c        |  2 +-
 hw/rdma/rdma_utils.c  |  2 +-
 hw/scsi/megasas.c     | 10 +++++-----
 softmmu/dma-helpers.c |  6 +++---
 5 files changed, 21 insertions(+), 21 deletions(-)
include/sysemu/dma.h +11 -11
index 0db2478a506..7a8ae4fcd0b 100644
@@ -15,22 +15,11 @@
 #include "block/block.h"
 #include "block/accounting.h"
 
-typedef struct ScatterGatherEntry ScatterGatherEntry;
-
 typedef enum {
     DMA_DIRECTION_TO_DEVICE = 0,
     DMA_DIRECTION_FROM_DEVICE = 1,
 } DMADirection;
 
-struct QEMUSGList {
-    ScatterGatherEntry *sg;
-    int nsg;
-    int nalloc;
-    size_t size;
-    DeviceState *dev;
-    AddressSpace *as;
-};
-
 /*
  * When an IOMMU is present, bus addresses become distinct from
  * CPU/memory physical addresses and may be a different size.  Because
@@ -43,6 +32,17 @@ typedef uint64_t dma_addr_t;
 #define DMA_ADDR_BITS 64
 #define DMA_ADDR_FMT "%" PRIx64
 
+typedef struct ScatterGatherEntry ScatterGatherEntry;
+
+struct QEMUSGList {
+    ScatterGatherEntry *sg;
+    int nsg;
+    int nalloc;
+    dma_addr_t size;
+    DeviceState *dev;
+    AddressSpace *as;
+};
+
 static inline void dma_barrier(AddressSpace *as, DMADirection dir)
 {
     /*
hw/nvme/ctrl.c +1 -1
index 462f79a1f60..c3c49176110 100644
@@ -1147,7 +1147,7 @@ static uint16_t nvme_tx(NvmeCtrl *n, NvmeSg *sg, uint8_t *ptr, uint32_t len,
 
     if (sg->flags & NVME_SG_DMA) {
         const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
-        uint64_t residual;
+        dma_addr_t residual;
 
         if (dir == NVME_TX_DIRECTION_TO_DEVICE) {
             residual = dma_buf_write(ptr, len, &sg->qsg, attrs);
hw/rdma/rdma_utils.c +1 -1
index 61cb8ede0fd..5a7ef63ad28 100644
@@ -20,7 +20,7 @@
 void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t len)
 {
     void *p;
-    hwaddr pci_len = len;
+    dma_addr_t pci_len = len;
 
     if (!addr) {
         rdma_error_report("addr is NULL");
hw/scsi/megasas.c +5 -5
index cb019549371..6c1ae6b980f 100644
@@ -1046,7 +1046,7 @@ static int megasas_pd_get_info_submit(SCSIDevice *sdev, int lun,
     uint16_t pd_id = ((sdev->id & 0xFF) << 8) | (lun & 0xFF);
     uint8_t cmdbuf[6];
     size_t len;
-    size_t residual;
+    dma_addr_t residual;
 
     if (!cmd->iov_buf) {
         cmd->iov_buf = g_malloc0(dcmd_size);
@@ -1152,7 +1152,7 @@ static int megasas_dcmd_ld_get_list(MegasasState *s, MegasasCmd *cmd)
 {
     struct mfi_ld_list info;
     size_t dcmd_size = sizeof(info);
-    size_t residual;
+    dma_addr_t residual;
     uint32_t num_ld_disks = 0, max_ld_disks;
     uint64_t ld_size;
     BusChild *kid;
@@ -1198,7 +1198,7 @@ static int megasas_dcmd_ld_list_query(MegasasState *s, MegasasCmd *cmd)
     uint16_t flags;
     struct mfi_ld_targetid_list info;
     size_t dcmd_size = sizeof(info);
-    size_t residual;
+    dma_addr_t residual;
     uint32_t num_ld_disks = 0, max_ld_disks = s->fw_luns;
     BusChild *kid;
 
@@ -1251,7 +1251,7 @@ static int megasas_ld_get_info_submit(SCSIDevice *sdev, int lun,
     size_t dcmd_size = sizeof(struct mfi_ld_info);
     uint8_t cdb[6];
     ssize_t len;
-    size_t residual;
+    dma_addr_t residual;
     uint16_t sdev_id = ((sdev->id & 0xFF) << 8) | (lun & 0xFF);
     uint64_t ld_size;
 
@@ -1625,7 +1625,7 @@ static int megasas_handle_dcmd(MegasasState *s, MegasasCmd *cmd)
 }
 
 static int megasas_finish_internal_dcmd(MegasasCmd *cmd,
-                                        SCSIRequest *req, size_t residual)
+                                        SCSIRequest *req, dma_addr_t residual)
 {
     int retval = MFI_STAT_OK;
     int lun = req->lun;
softmmu/dma-helpers.c +3 -3
index 4563a775aa7..54340929334 100644
@@ -294,12 +294,12 @@ BlockAIOCB *dma_blk_write(BlockBackend *blk,
 }
 
 
-static MemTxResult dma_buf_rw(void *buf, int32_t len, uint64_t *residual,
+static MemTxResult dma_buf_rw(void *buf, dma_addr_t len, dma_addr_t *residual,
                               QEMUSGList *sg, DMADirection dir,
                               MemTxAttrs attrs)
 {
     uint8_t *ptr = buf;
-    uint64_t xresidual;
+    dma_addr_t xresidual;
     int sg_cur_index;
     MemTxResult res = MEMTX_OK;
 
@@ -308,7 +308,7 @@ static MemTxResult dma_buf_rw(void *buf, int32_t len, uint64_t *residual,
     len = MIN(len, xresidual);
     while (len > 0) {
         ScatterGatherEntry entry = sg->sg[sg_cur_index++];
-        int32_t xfer = MIN(len, entry.len);
+        dma_addr_t xfer = MIN(len, entry.len);
         res |= dma_memory_rw(sg->as, entry.base, ptr, xfer, dir, attrs);
         ptr += xfer;
         len -= xfer;
Signature · 1 line
2.33.1
#22 lore raw

hw/nvme/ctrl.c static uint16_t nvme_tx(NvmeCtrl *n, NvmeSg *sg, uint8_t *ptr, uint32_t len, · line 1153

Quoted 13 lines · Philippe Mathieu-Daudé
hw/nvme/ctrl.c +1 -1
index 462f79a1f60..c3c49176110 100644
@@ -1147,7 +1147,7 @@ static uint16_t nvme_tx(NvmeCtrl *n, NvmeSg *sg, uint8_t *ptr, uint32_t len,
 
     if (sg->flags & NVME_SG_DMA) {
         const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
-        uint64_t residual;
+        dma_addr_t residual;
 
         if (dir == NVME_TX_DIRECTION_TO_DEVICE) {
             residual = dma_buf_write(ptr, len, &sg->qsg, attrs);

If there's a new version: Maybe also change the return value types of dma_buf_write|read() to dma_addr_t?

It'll be changed anyway in the next patch, so not a big deal.

The rest patches looks good to me. Thanks.

Signature · 1 line
Peter Xu
Reviewed-by on v2 8/9
#23 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 52 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

Update the obvious places where dma_addr_t should be used (instead of uint64_t, hwaddr, size_t, int32_t types).

This allows to have &dma_addr_t type portable on 32/64-bit hosts.

Move QEMUSGList declaration after dma_addr_t declaration so this structure can use the new type.

---
 include/sysemu/dma.h  | 22 +++++++++++-----------
 hw/nvme/ctrl.c        |  2 +-
 hw/rdma/rdma_utils.c  |  2 +-
 hw/scsi/megasas.c     | 10 +++++-----
 softmmu/dma-helpers.c |  6 +++---
 5 files changed, 21 insertions(+), 21 deletions(-)
include/sysemu/dma.h +0 -11
index 0db2478a506..7a8ae4fcd0b 100644
@@ -15,22 +15,11 @@
 #include "block/block.h"
 #include "block/accounting.h"
 
-typedef struct ScatterGatherEntry ScatterGatherEntry;
-
 typedef enum {
     DMA_DIRECTION_TO_DEVICE = 0,
     DMA_DIRECTION_FROM_DEVICE = 1,
 } DMADirection;
 
-struct QEMUSGList {
-    ScatterGatherEntry *sg;
-    int nsg;
-    int nalloc;
-    size_t size;
-    DeviceState *dev;
-    AddressSpace *as;
-};
-
 /*
  * When an IOMMU is present, bus addresses become distinct from
  * CPU/memory physical addresses and may be a different size.  Because
@@ -43,6 +32,17 @@ typedef uint64_t dma_addr_t;
 #define DMA_ADDR_BITS 64
 #define DMA_ADDR_FMT "%" PRIx64
 

include/sysemu/dma.h typedef uint64_t dma_addr_t; · line 44

Quoted 10 lines
include/sysemu/dma.h +10 -0
+typedef struct ScatterGatherEntry ScatterGatherEntry;
+
+struct QEMUSGList {
+    ScatterGatherEntry *sg;
+    int nsg;
+    int nalloc;
+    dma_addr_t size;
+    DeviceState *dev;
+    AddressSpace *as;
+};

Changing one member while moving is sneaky. Why the move in this patch?

Apart from that and Peters comment

Signature · 3 lines
Thanks,

David / dhildenb
#24 lore raw
Quoted 53 lines · David Hildenbrand
Quoted 52 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

Update the obvious places where dma_addr_t should be used (instead of uint64_t, hwaddr, size_t, int32_t types).

This allows to have &dma_addr_t type portable on 32/64-bit hosts.

Move QEMUSGList declaration after dma_addr_t declaration so this structure can use the new type.

---
 include/sysemu/dma.h  | 22 +++++++++++-----------
 hw/nvme/ctrl.c        |  2 +-
 hw/rdma/rdma_utils.c  |  2 +-
 hw/scsi/megasas.c     | 10 +++++-----
 softmmu/dma-helpers.c |  6 +++---
 5 files changed, 21 insertions(+), 21 deletions(-)
include/sysemu/dma.h +0 -11
index 0db2478a506..7a8ae4fcd0b 100644
@@ -15,22 +15,11 @@
 #include "block/block.h"
 #include "block/accounting.h"
 
-typedef struct ScatterGatherEntry ScatterGatherEntry;
-
 typedef enum {
     DMA_DIRECTION_TO_DEVICE = 0,
     DMA_DIRECTION_FROM_DEVICE = 1,
 } DMADirection;
 
-struct QEMUSGList {
-    ScatterGatherEntry *sg;
-    int nsg;
-    int nalloc;
-    size_t size;
-    DeviceState *dev;
-    AddressSpace *as;
-};
-
 /*
  * When an IOMMU is present, bus addresses become distinct from
  * CPU/memory physical addresses and may be a different size.  Because
@@ -43,6 +32,17 @@ typedef uint64_t dma_addr_t;
 #define DMA_ADDR_BITS 64
 #define DMA_ADDR_FMT "%" PRIx64
 
Quoted 12 lines

include/sysemu/dma.h typedef uint64_t dma_addr_t; · line 44

Quoted 10 lines
include/sysemu/dma.h +10 -0
+typedef struct ScatterGatherEntry ScatterGatherEntry;
+
+struct QEMUSGList {
+    ScatterGatherEntry *sg;
+    int nsg;
+    int nalloc;
+    dma_addr_t size;
+    DeviceState *dev;
+    AddressSpace *as;
+};

Changing one member while moving is sneaky. Why the move in this patch?

Because dma_addr_t is declared before QEMUSGList. I will add an intermediate patch.

Quoted 3 lines

Apart from that and Peters comment

Thanks (Peter's comment addressed).

#25 lore raw

PATCH v2 9/9 hw/dma: Let dma_buf_read() / dma_buf_write() propagate MemTxResult

In reply to Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

Since the previous commit, dma_buf_rw() returns a MemTxResult type. Do not discard it, return it to the caller.

Since both dma_buf_read/dma_buf_write functions were previously returning the QEMUSGList size not consumed, add an extra argument where the unconsummed size can be stored.

Update the few callers.

---
 include/hw/scsi/scsi.h |  2 +-
 include/sysemu/dma.h   |  6 +++--
 hw/ide/ahci.c          |  8 +++---
 hw/nvme/ctrl.c         |  4 +--
 hw/scsi/megasas.c      | 59 ++++++++++++++++++++++++++++++------------
 hw/scsi/scsi-bus.c     |  6 +++--
 softmmu/dma-helpers.c  | 18 +++++--------
 7 files changed, 63 insertions(+), 40 deletions(-)
include/hw/scsi/scsi.h +1 -1
index b27d133b113..1ffb367f94f 100644
@@ -30,7 +30,7 @@ struct SCSIRequest {
     int16_t           status;
     int16_t           host_status;
     void              *hba_private;
-    size_t            residual;
+    uint64_t          residual;
     SCSICommand       cmd;
     NotifierList      cancel_notifiers;
 
include/sysemu/dma.h +4 -2
index 7a8ae4fcd0b..a1ac5bc1b54 100644
@@ -301,8 +301,10 @@ BlockAIOCB *dma_blk_read(BlockBackend *blk,
 BlockAIOCB *dma_blk_write(BlockBackend *blk,
                           QEMUSGList *sg, uint64_t offset, uint32_t align,
                           BlockCompletionFunc *cb, void *opaque);
-uint64_t dma_buf_read(void *ptr, int32_t len, QEMUSGList *sg, MemTxAttrs attrs);
-uint64_t dma_buf_write(void *ptr, int32_t len, QEMUSGList *sg, MemTxAttrs attrs);
+MemTxResult dma_buf_read(void *ptr, dma_addr_t len, dma_addr_t *residual,
+                         QEMUSGList *sg, MemTxAttrs attrs);
+MemTxResult dma_buf_write(void *ptr, dma_addr_t len, dma_addr_t *residual,
+                          QEMUSGList *sg, MemTxAttrs attrs);
 
 void dma_acct_start(BlockBackend *blk, BlockAcctCookie *cookie,
                     QEMUSGList *sg, enum BlockAcctType type);
hw/ide/ahci.c +4 -4
index 6c727dd0c08..7ce001cacdb 100644
@@ -1384,9 +1384,9 @@ static void ahci_pio_transfer(const IDEDMA *dma)
         const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
 
         if (is_write) {
-            dma_buf_write(s->data_ptr, size, &s->sg, attrs);
+            dma_buf_write(s->data_ptr, size, NULL, &s->sg, attrs);
         } else {
-            dma_buf_read(s->data_ptr, size, &s->sg, attrs);
+            dma_buf_read(s->data_ptr, size, NULL, &s->sg, attrs);
         }
     }
 
@@ -1479,9 +1479,9 @@ static int ahci_dma_rw_buf(const IDEDMA *dma, bool is_write)
     }
 
     if (is_write) {
-        dma_buf_read(p, l, &s->sg, MEMTXATTRS_UNSPECIFIED);
+        dma_buf_read(p, l, NULL, &s->sg, MEMTXATTRS_UNSPECIFIED);
     } else {
-        dma_buf_write(p, l, &s->sg, MEMTXATTRS_UNSPECIFIED);
+        dma_buf_write(p, l, NULL, &s->sg, MEMTXATTRS_UNSPECIFIED);
     }
 
     /* free sglist, update byte count */
hw/nvme/ctrl.c +2 -2
index c3c49176110..1f62116af98 100644
@@ -1150,9 +1150,9 @@ static uint16_t nvme_tx(NvmeCtrl *n, NvmeSg *sg, uint8_t *ptr, uint32_t len,
         dma_addr_t residual;
 
         if (dir == NVME_TX_DIRECTION_TO_DEVICE) {
-            residual = dma_buf_write(ptr, len, &sg->qsg, attrs);
+            dma_buf_write(ptr, len, &residual, &sg->qsg, attrs);
         } else {
-            residual = dma_buf_read(ptr, len, &sg->qsg, attrs);
+            dma_buf_read(ptr, len, &residual, &sg->qsg, attrs);
         }
 
         if (unlikely(residual)) {
hw/scsi/megasas.c +42 -17

182 lines not on this page · raw message

hw/scsi/scsi-bus.c +4 -2
index 3466e680ac7..4057e04ce89 100644
@@ -1421,9 +1421,11 @@ void scsi_req_data(SCSIRequest *req, int len)
 
     buf = scsi_req_get_buf(req);
     if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
-        req->residual = dma_buf_read(buf, len, req->sg, MEMTXATTRS_UNSPECIFIED);
+        dma_buf_read(buf, len, &req->residual, req->sg,
+                     MEMTXATTRS_UNSPECIFIED);
     } else {
-        req->residual = dma_buf_write(buf, len, req->sg, MEMTXATTRS_UNSPECIFIED);
+        dma_buf_write(buf, len, &req->residual, req->sg,
+                      MEMTXATTRS_UNSPECIFIED);
     }
     scsi_req_continue(req);
 }
softmmu/dma-helpers.c +6 -12
index 54340929334..1c6fba6a112 100644
@@ -321,22 +321,16 @@ static MemTxResult dma_buf_rw(void *buf, dma_addr_t len, dma_addr_t *residual,
     return res;
 }
 
-uint64_t dma_buf_read(void *ptr, int32_t len, QEMUSGList *sg, MemTxAttrs attrs)
+MemTxResult dma_buf_read(void *ptr, dma_addr_t len, dma_addr_t *residual,
+                         QEMUSGList *sg, MemTxAttrs attrs)
 {
-    uint64_t residual;
-
-    dma_buf_rw(ptr, len, &residual, sg, DMA_DIRECTION_FROM_DEVICE, attrs);
-
-    return residual;
+    return dma_buf_rw(ptr, len, residual, sg, DMA_DIRECTION_FROM_DEVICE, attrs);
 }
 
-uint64_t dma_buf_write(void *ptr, int32_t len, QEMUSGList *sg, MemTxAttrs attrs)
+MemTxResult dma_buf_write(void *ptr, dma_addr_t len, dma_addr_t *residual,
+                          QEMUSGList *sg, MemTxAttrs attrs)
 {
-    uint64_t residual;
-
-    dma_buf_rw(ptr, len, &residual, sg, DMA_DIRECTION_TO_DEVICE, attrs);
-
-    return residual;
+    return dma_buf_rw(ptr, len, residual, sg, DMA_DIRECTION_TO_DEVICE, attrs);
 }
 
 void dma_acct_start(BlockBackend *blk, BlockAcctCookie *cookie,
Signature · 1 line
2.33.1
#26 lore raw

Re: [PATCH v2 9/9] hw/dma: Let dma_buf_read() / dma_buf_write() propagate MemTxResult

Quoted 10 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

Since the previous commit, dma_buf_rw() returns a MemTxResult type. Do not discard it, return it to the caller.

Since both dma_buf_read/dma_buf_write functions were previously returning the QEMUSGList size not consumed, add an extra argument where the unconsummed size can be stored.

Update the few callers.

I feel like this patch doesn't fit into the context of this series. Especially as the cover letter mentiones "Finally we replace misuses with dma_addr_t typedef."

Am I missing something?

Signature · 3 lines
Thanks,

David / dhildenb
#27 lore raw

Re: [PATCH v2 9/9] hw/dma: Let dma_buf_read() / dma_buf_write() propagate MemTxResult

Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

Since the previous commit, dma_buf_rw() returns a MemTxResult type. Do not discard it, return it to the caller.

Since both dma_buf_read/dma_buf_write functions were previously returning the QEMUSGList size not consumed, add an extra argument where the unconsummed size can be stored.

Quoted 7 lines
Quoted 1 line

Update the few callers.

I feel like this patch doesn't fit into the context of this series. Especially as the cover letter mentiones "Finally we replace misuses with dma_addr_t typedef."

Am I missing something?

OK, I will simply repost it once this series gets merged.

Reviewed-by on the v2 series
#28 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 3 lines · Philippe Mathieu-Daudé

Since v1:
- Addressed David review comment (stick to dma_addr_t type)
- Addressed Peter review comment (incorrect doc string)

PCI things:

who's merging all this? Yourself?

Quoted 54 lines

Hi,

This series aims to clarify a bit the DMA API, in particular the 'size' argument which is not clear enough (as we use multiple types for it). This helps avoiding build failures on 32-bit host [*] (and likely overflows calculation too IMO).

Some units using the DMA API are first removed from user-mode emulation to avoid build failure (they shouldn't be there in the first place).

Then some variables are renamed for clarity (no functional change).

Finally we replace misuses with dma_addr_t typedef. The previous patch which failed on 32-bit host applied on top (not failing anymore).

Regards,

Phil.

[*] https://www.mail-archive.com/[email protected]/msg858825.html

Supersedes: <[email protected]>

Philippe Mathieu-Daudé (9):
hw/nvram: Restrict stub to sysemu and tools
hw/pci: Restrict pci-bus stub to sysemu
hw/pci: Document pci_dma_map()
hw/dma: Remove CONFIG_USER_ONLY check hw/rdma/rdma_utils: Rename rdma_pci_dma_map 'len' argument hw/scsi: Rename SCSIRequest::resid as 'residual' hw/dma: Fix format string issues using dma_addr_t hw/dma: Use dma_addr_t type definition when relevant hw/dma: Let dma_buf_read() / dma_buf_write() propagate MemTxResult

 hw/rdma/rdma_utils.h   |  2 +-
 include/hw/pci/pci.h   | 12 ++++++
 include/hw/scsi/scsi.h |  4 +-
 include/sysemu/dma.h   | 31 ++++++++-------
 hw/ide/ahci.c          | 10 ++---
 hw/nvme/ctrl.c         |  6 +--
 hw/rdma/rdma_utils.c   | 14 +++----
 hw/scsi/megasas.c      | 85 +++++++++++++++++++++++++++++-------------
 hw/scsi/scsi-bus.c     | 12 +++---
 hw/scsi/scsi-disk.c    |  4 +-
 softmmu/dma-helpers.c  | 34 +++++++----------
 hw/nvram/meson.build   |  6 ++-
 hw/rdma/trace-events   |  2 +-
 stubs/meson.build      |  4 +-
 14 files changed, 134 insertions(+), 92 deletions(-)
Signature · 1 line
2.33.1
#29 lore raw
Quoted 9 lines · Michael S. Tsirkin
Quoted 3 lines · Philippe Mathieu-Daudé

Since v1:
- Addressed David review comment (stick to dma_addr_t type)
- Addressed Peter review comment (incorrect doc string)

PCI things:

Thanks.

Quoted 1 line

who's merging all this? Yourself?

I just posted a v3 addressing review comments on this series. Usually Paolo takes such patches, but I am happy to help sending PR when this is trivial content (such this series).

Regards,

Phil.