Skip to contentSkip to conversation

hw/ppc & net: Simplify LD/ST API uses

PATCH Superseded Philippe Mathieu-Daudé 1 participant 5 messages Sep 27, 2024 Networking

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover hw/ppc & net: Simplify LD/ST API uses

Use ldN / stN methods to access variable lengths.

Philippe Mathieu-Daudé (4):
hw/ppc/spapr_nvdimm: Simplify LD/ST API uses
hw/ppc/spapr_vof: Simplify LD/ST API uses
hw/ppc/vof: Simplify LD/ST API uses
net/l2tpv3: Simplify LD/ST API uses

 hw/ppc/spapr_nvdimm.c | 47 ++++---------------------------------------
 hw/ppc/spapr_vof.c    | 27 +++++++++----------------
 hw/ppc/vof.c          |  6 +-----
 net/l2tpv3.c          |  6 +-----
 4 files changed, 15 insertions(+), 71 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/4 hw/ppc/spapr_nvdimm: Simplify LD/ST API uses

ldn/stn methods handle the access size, no need for the switch case.

---
 hw/ppc/spapr_nvdimm.c | 47 ++++---------------------------------------
 1 file changed, 4 insertions(+), 43 deletions(-)
hw/ppc/spapr_nvdimm.c +4 -43
index 7d2dfe5e3d..5af0b13370 100644
@@ -250,7 +250,6 @@ static target_ulong h_scm_read_metadata(PowerPCCPU *cpu,
     SpaprDrc *drc = spapr_drc_by_index(drc_index);
     NVDIMMDevice *nvdimm;
     NVDIMMClass *ddc;
-    uint64_t data = 0;
     uint8_t buf[8] = { 0 };
 
     if (!drc || !drc->dev ||
@@ -272,24 +271,7 @@ static target_ulong h_scm_read_metadata(PowerPCCPU *cpu,
     ddc = NVDIMM_GET_CLASS(nvdimm);
     ddc->read_label_data(nvdimm, buf, len, offset);
 
-    switch (len) {
-    case 1:
-        data = ldub_p(buf);
-        break;
-    case 2:
-        data = lduw_be_p(buf);
-        break;
-    case 4:
-        data = ldl_be_p(buf);
-        break;
-    case 8:
-        data = ldq_be_p(buf);
-        break;
-    default:
-        g_assert_not_reached();
-    }
-
-    args[0] = data;
+    args[0] = ldn_be_p(buf, len);
 
     return H_SUCCESS;
 }
@@ -325,31 +307,10 @@ static target_ulong h_scm_write_metadata(PowerPCCPU *cpu,
         return H_P2;
     }
 
-    switch (len) {
-    case 1:
-        if (data & 0xffffffffffffff00) {
-            return H_P2;
-        }
-        stb_p(buf, data);
-        break;
-    case 2:
-        if (data & 0xffffffffffff0000) {
-            return H_P2;
-        }
-        stw_be_p(buf, data);
-        break;
-    case 4:
-        if (data & 0xffffffff00000000) {
-            return H_P2;
-        }
-        stl_be_p(buf, data);
-        break;
-    case 8:
-        stq_be_p(buf, data);
-        break;
-    default:
-            g_assert_not_reached();
+    if (len < 8 && extract64(data, 8 * len, 64 - 8 * len)) {
+        return H_P2;
     }
+    stn_be_p(buf, data, len);
 
     ddc = NVDIMM_GET_CLASS(nvdimm);
     ddc->write_label_data(nvdimm, buf, len, offset);
Signature · 1 line
2.45.2
#2 lore raw

PATCH 2/4 hw/ppc/spapr_vof: Simplify LD/ST API uses

In reply to Philippe Mathieu-Daudé

Directly call ldn_be_p once instead of ldl_be_p / ldq_be_p.

---
 hw/ppc/spapr_vof.c | 27 +++++++++------------------
 1 file changed, 9 insertions(+), 18 deletions(-)
hw/ppc/spapr_vof.c +9 -18
index c02eaacfed..d238a44d88 100644
@@ -136,26 +136,17 @@ bool spapr_vof_setprop(MachineState *ms, const char *path, const char *propname,
             vof->bootargs = g_strndup(val, vallen);
             return true;
         }
-        if (strcmp(propname, "linux,initrd-start") == 0) {
-            if (vallen == sizeof(uint32_t)) {
-                spapr->initrd_base = ldl_be_p(val);
-                return true;
+        switch (vallen) {
+        case 4:
+        case 8:
+            if (strcmp(propname, "linux,initrd-start") == 0) {
+                spapr->initrd_base = ldn_be_p(val, vallen);
             }
-            if (vallen == sizeof(uint64_t)) {
-                spapr->initrd_base = ldq_be_p(val);
-                return true;
-            }
-            return false;
-        }
-        if (strcmp(propname, "linux,initrd-end") == 0) {
-            if (vallen == sizeof(uint32_t)) {
-                spapr->initrd_size = ldl_be_p(val) - spapr->initrd_base;
-                return true;
-            }
-            if (vallen == sizeof(uint64_t)) {
-                spapr->initrd_size = ldq_be_p(val) - spapr->initrd_base;
-                return true;
+            if (strcmp(propname, "linux,initrd-end") == 0) {
+                spapr->initrd_size = ldn_be_p(val, vallen);
             }
+            return true;
+        default:
             return false;
         }
     }
Signature · 1 line
2.45.2
#3 lore raw

PATCH 3/4 hw/ppc/vof: Simplify LD/ST API uses

In reply to Philippe Mathieu-Daudé

Directly call ldn_be_p once instead of be32_to_cpu / ldq_be_p.

---
 hw/ppc/vof.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
hw/ppc/vof.c +1 -5
index b5b6514d79..fb152efbe0 100644
@@ -645,11 +645,7 @@ static void vof_dt_memory_available(void *fdt, GArray *claimed, uint64_t base)
 
     mem0_reg = fdt_getprop(fdt, offset, "reg", &proplen);
     g_assert(mem0_reg && proplen == sizeof(uint32_t) * (ac + sc));
-    if (sc == 2) {
-        mem0_end = ldq_be_p(mem0_reg + sizeof(uint32_t) * ac);
-    } else {
-        mem0_end = be32_to_cpu(*(uint32_t *)(mem0_reg + sizeof(uint32_t) * ac));
-    }
+    mem0_end = ldn_be_p(mem0_reg + sizeof(uint32_t) * ac, 4 * sc);
 
     g_array_sort(claimed, of_claimed_compare_func);
     vof_claimed_dump(claimed);
Signature · 1 line
2.45.2
#4 lore raw

PATCH 4/4 net/l2tpv3: Simplify LD/ST API uses

In reply to Philippe Mathieu-Daudé

Directly call ldn_be_p once instead of ldl_be_p / ldq_be_p.

---
 net/l2tpv3.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
net/l2tpv3.c +1 -5
index b5547cb917..7a0d5dcfe9 100644
@@ -318,11 +318,7 @@ static int l2tpv3_verify_header(NetL2TPV3State *s, uint8_t *buf)
     */
 
     if (s->cookie) {
-        if (s->cookie_is_64) {
-            cookie = ldq_be_p(buf + s->cookie_offset);
-        } else {
-            cookie = ldl_be_p(buf + s->cookie_offset) & 0xffffffffULL;
-        }
+        cookie = ldn_be_p(buf + s->cookie_offset, s->cookie_is_64 ? 8 : 4);
         if (cookie != s->rx_cookie) {
             if (!s->header_mismatch) {
                 error_report("unknown cookie id");
Signature · 1 line
2.45.2