Skip to contentSkip to conversation

hw/net: Don't use cpu_to_*w() and *_to_cpup()

PATCH Applied Peter Maydell 4 participants 10 messages Jun 16, 2016 – Jun 27, 2016

lore
Share & tools
lore
#0 lore raw

cover hw/net: Don't use cpu_to_*w() and *_to_cpup()

This patchset converts a handful of network devices to use ld*_p() and st*_p() instead of cpu_to_*w() and *_to_cpup().

This is the last lot of conversion patches; I have the "delete the implementations from bswap.h" patch, and will send that out once all of the in-flight patches have hit master.

thanks
-- PMM

Peter Maydell (5):
hw/net/eepro100.c: Don't use cpu_to_*w() and *_to_cpup() hw/net/rtl8139.c: Don't use *_to_cpup() hw/net/rocker: Don't use *_to_cpup() hw/net/virtio-net.c: Don't use *_to_cpup() hw/net/e1000: Don't use *_to_cpup()

 hw/net/e1000.c             | 18 +++++++++---------
 hw/net/e1000e_core.c       |  6 +++---
 hw/net/e1000x_common.c     |  2 +-
 hw/net/eepro100.c          |  8 ++++----
 hw/net/rocker/rocker_tlv.h |  6 +++---
 hw/net/rtl8139.c           |  9 ++++-----
 hw/net/virtio-net.c        |  2 +-
 7 files changed, 25 insertions(+), 26 deletions(-)
Signature · 1 line
1.9.1
#2 lore raw

PATCH 2/5 hw/net/rtl8139.c: Don't use *_to_cpup()

Don't use *_to_cpup() to do byte-swapped loads; instead use ld*_p() which correctly handle misaligned accesses.

---
 hw/net/rtl8139.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
hw/net/rtl8139.c +4 -5
index 562c1fd..e2b140d 100644
@@ -1013,8 +1013,8 @@ static ssize_t rtl8139_do_receive(NetClientState *nc, const uint8_t *buf, size_t
         uint32_t rx_space = rxdw0 & CP_RX_BUFFER_SIZE_MASK;
 
         /* write VLAN info to descriptor variables. */
-        if (s->CpCmd & CPlusRxVLAN && be16_to_cpup((uint16_t *)
-                &buf[ETH_ALEN * 2]) == ETH_P_VLAN) {
+        if (s->CpCmd & CPlusRxVLAN &&
+            lduw_be_p(&buf[ETH_ALEN * 2]) == ETH_P_VLAN) {
             dot1q_buf = &buf[ETH_ALEN * 2];
             size -= VLAN_HLEN;
             /* if too small buffer, use the tailroom added duing expansion */
@@ -1024,11 +1024,10 @@ static ssize_t rtl8139_do_receive(NetClientState *nc, const uint8_t *buf, size_t
 
             rxdw1 &= ~CP_RX_VLAN_TAG_MASK;
             /* BE + ~le_to_cpu()~ + cpu_to_le() = BE */
-            rxdw1 |= CP_RX_TAVA | le16_to_cpup((uint16_t *)
-                &dot1q_buf[ETHER_TYPE_LEN]);
+            rxdw1 |= CP_RX_TAVA | lduw_le_p(&dot1q_buf[ETHER_TYPE_LEN]);
 
             DPRINTF("C+ Rx mode : extracted vlan tag with tci: ""%u\n",
-                be16_to_cpup((uint16_t *)&dot1q_buf[ETHER_TYPE_LEN]));
+                lduw_be_p(&dot1q_buf[ETHER_TYPE_LEN]));
         } else {
             /* reset VLAN tag flag */
             rxdw1 &= ~CP_RX_TAVA;
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/5 hw/net/eepro100.c: Don't use cpu_to_*w() and *_to_cpup()

In reply to Peter Maydell

Don't use cpu_to_*w() and *_to_cpup() to do byte-swapped loads and stores; instead use ld*_p() and st*_p() which correctly handle misaligned accesses.

---
 hw/net/eepro100.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/net/eepro100.c +4 -4
index 9b4b9b5..b10c419 100644
@@ -352,14 +352,14 @@ static unsigned e100_compute_mcast_idx(const uint8_t *ep)
 static uint16_t e100_read_reg2(EEPRO100State *s, E100RegisterOffset addr)
 {
     assert(!((uintptr_t)&s->mem[addr] & 1));
-    return le16_to_cpup((uint16_t *)&s->mem[addr]);
+    return lduw_le_p(&s->mem[addr]);
 }
 
 /* Read a 32 bit control/status (CSR) register. */
 static uint32_t e100_read_reg4(EEPRO100State *s, E100RegisterOffset addr)
 {
     assert(!((uintptr_t)&s->mem[addr] & 3));
-    return le32_to_cpup((uint32_t *)&s->mem[addr]);
+    return ldl_le_p(&s->mem[addr]);
 }
 
 /* Write a 16 bit control/status (CSR) register. */
@@ -367,7 +367,7 @@ static void e100_write_reg2(EEPRO100State *s, E100RegisterOffset addr,
                             uint16_t val)
 {
     assert(!((uintptr_t)&s->mem[addr] & 1));
-    cpu_to_le16w((uint16_t *)&s->mem[addr], val);
+    stw_le_p(&s->mem[addr], val);
 }
 
 /* Read a 32 bit control/status (CSR) register. */
@@ -375,7 +375,7 @@ static void e100_write_reg4(EEPRO100State *s, E100RegisterOffset addr,
                             uint32_t val)
 {
     assert(!((uintptr_t)&s->mem[addr] & 3));
-    cpu_to_le32w((uint32_t *)&s->mem[addr], val);
+    stl_le_p(&s->mem[addr], val);
 }
 
 #if defined(DEBUG_EEPRO100)
Signature · 1 line
1.9.1
#3 lore raw

PATCH 3/5 hw/net/rocker: Don't use *_to_cpup()

In reply to Peter Maydell

Don't use *_to_cpup() to do byte-swapped loads; instead use ld*_p() which correctly handle misaligned accesses.

---
 hw/net/rocker/rocker_tlv.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/net/rocker/rocker_tlv.h +3 -3
index e3c4ab6..8856164 100644
@@ -106,17 +106,17 @@ static inline uint64_t rocker_tlv_get_u64(const RockerTlv *tlv)
 
 static inline uint16_t rocker_tlv_get_le16(const RockerTlv *tlv)
 {
-    return le16_to_cpup((uint16_t *) rocker_tlv_data(tlv));
+    return lduw_le_p(rocker_tlv_data(tlv));
 }
 
 static inline uint32_t rocker_tlv_get_le32(const RockerTlv *tlv)
 {
-    return le32_to_cpup((uint32_t *) rocker_tlv_data(tlv));
+    return ldl_le_p(rocker_tlv_data(tlv));
 }
 
 static inline uint64_t rocker_tlv_get_le64(const RockerTlv *tlv)
 {
-    return le64_to_cpup((uint64_t *) rocker_tlv_data(tlv));
+    return ldq_le_p(rocker_tlv_data(tlv));
 }
 
 static inline void rocker_tlv_parse(RockerTlv **tb, int maxtype,
Signature · 1 line
1.9.1
#4 lore raw

PATCH 4/5 hw/net/virtio-net.c: Don't use *_to_cpup()

In reply to Peter Maydell

Don't use *_to_cpup() to do byte-swapped loads; instead use ld*_p() which correctly handle misaligned accesses.

---
 hw/net/virtio-net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/virtio-net.c +1 -1
index 5798f87..7e6a60a 100644
@@ -1051,7 +1051,7 @@ static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
     ptr += n->host_hdr_len;
 
     if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
-        int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
+        int vid = lduw_be_p(ptr + 14) & 0xfff;
         if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
             return 0;
     }
Signature · 1 line
1.9.1
#5 lore raw

PATCH 5/5 hw/net/e1000: Don't use *_to_cpup()

In reply to Peter Maydell

Don't use *_to_cpup() to do byte-swapped loads; instead use ld*_p() which correctly handle misaligned accesses.

---
 hw/net/e1000.c         | 18 +++++++++---------
 hw/net/e1000e_core.c   |  6 +++---
 hw/net/e1000x_common.c |  2 +-
 3 files changed, 13 insertions(+), 13 deletions(-)
hw/net/e1000.c +9 -9
index 1202371..06ca7b2 100644
@@ -536,7 +536,7 @@ e1000_send_packet(E1000State *s, const uint8_t *buf, int size)
 static void
 xmit_seg(E1000State *s)
 {
-    uint16_t len, *sp;
+    uint16_t len;
     unsigned int frames = s->tx.tso_frames, css, sofar;
     struct e1000_tx *tp = &s->tx;
 
@@ -547,7 +547,7 @@ xmit_seg(E1000State *s)
         if (tp->props.ip) {    /* IPv4 */
             stw_be_p(tp->data+css+2, tp->size - css);
             stw_be_p(tp->data+css+4,
-                     be16_to_cpup((uint16_t *)(tp->data+css+4))+frames);
+                     lduw_be_p(tp->data + css + 4) + frames);
         } else {         /* IPv6 */
             stw_be_p(tp->data+css+4, tp->size - css);
         }
@@ -567,8 +567,9 @@ xmit_seg(E1000State *s)
         if (tp->props.sum_needed & E1000_TXD_POPTS_TXSM) {
             unsigned int phsum;
             // add pseudo-header length before checksum calculation
-            sp = (uint16_t *)(tp->data + tp->props.tucso);
-            phsum = be16_to_cpup(sp) + len;
+            void *sp = tp->data + tp->props.tucso;
+
+            phsum = lduw_be_p(sp) + len;
             phsum = (phsum >> 16) + (phsum & 0xffff);
             stw_be_p(sp, phsum);
         }
@@ -759,9 +760,9 @@ receive_filter(E1000State *s, const uint8_t *buf, int size)
 
     if (e1000x_is_vlan_packet(buf, le16_to_cpu(s->mac_reg[VET])) &&
         e1000x_vlan_rx_filter_enabled(s->mac_reg)) {
-        uint16_t vid = be16_to_cpup((uint16_t *)(buf + 14));
-        uint32_t vfta = le32_to_cpup((uint32_t *)(s->mac_reg + VFTA) +
-                                     ((vid >> 5) & 0x7f));
+        uint16_t vid = lduw_be_p(buf + 14);
+        uint32_t vfta = ldl_le_p((uint32_t*)(s->mac_reg + VFTA) +
+                                 ((vid >> 5) & 0x7f));
         if ((vfta & (1 << (vid & 0x1f))) == 0)
             return 0;
     }
@@ -889,8 +890,7 @@ e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt)
 
     if (e1000x_vlan_enabled(s->mac_reg) &&
         e1000x_is_vlan_packet(filter_buf, le16_to_cpu(s->mac_reg[VET]))) {
-        vlan_special = cpu_to_le16(be16_to_cpup((uint16_t *)(filter_buf
-                                                                + 14)));
+        vlan_special = cpu_to_le16(lduw_be_p(filter_buf + 14));
         iov_ofs = 4;
         if (filter_buf == iov->iov_base) {
             memmove(filter_buf + 4, filter_buf, 12);
hw/net/e1000e_core.c +3 -3
index 4549acb..6050d8b 100644
@@ -1019,9 +1019,9 @@ e1000e_receive_filter(E1000ECore *core, const uint8_t *buf, int size)
 
     if (e1000x_is_vlan_packet(buf, core->vet) &&
         e1000x_vlan_rx_filter_enabled(core->mac)) {
-        uint16_t vid = be16_to_cpup((uint16_t *)(buf + 14));
-        uint32_t vfta = le32_to_cpup((uint32_t *)(core->mac + VFTA) +
-                                     ((vid >> 5) & 0x7f));
+        uint16_t vid = lduw_be_p(buf + 14);
+        uint32_t vfta = ldl_le_p((uint32_t *)(core->mac + VFTA) +
+                                 ((vid >> 5) & 0x7f));
         if ((vfta & (1 << (vid & 0x1f))) == 0) {
             trace_e1000e_rx_flt_vlan_mismatch(vid);
             return false;
hw/net/e1000x_common.c +1 -1
index 94f85c9..eb0e097 100644
@@ -47,7 +47,7 @@ bool e1000x_rx_ready(PCIDevice *d, uint32_t *mac)
 
 bool e1000x_is_vlan_packet(const uint8_t *buf, uint16_t vet)
 {
-    uint16_t eth_proto = be16_to_cpup((uint16_t *)(buf + 12));
+    uint16_t eth_proto = lduw_be_p(buf + 12);
     bool res = (eth_proto == vet);
 
     trace_e1000x_vlan_is_vlan_pkt(res, eth_proto, vet);
Signature · 1 line
1.9.1
Reviewed-by on the v1 series
#6 lore raw
In reply to Peter Maydell
Quoted 10 lines · Peter Maydell

This patchset converts a handful of network devices to use ld*_p() and st*_p() instead of cpu_to_*w() and *_to_cpup().

This is the last lot of conversion patches; I have the "delete the implementations from bswap.h" patch, and will send that out once all of the in-flight patches have hit master.

thanks
-- PMM

Quoted 7 lines

Peter Maydell (5):
hw/net/eepro100.c: Don't use cpu_to_*w() and *_to_cpup() hw/net/rtl8139.c: Don't use *_to_cpup() hw/net/rocker: Don't use *_to_cpup() hw/net/virtio-net.c: Don't use *_to_cpup() hw/net/e1000: Don't use *_to_cpup()

r~

Acked-by on the v1 series
#7 lore raw
In reply to Peter Maydell
Quoted 17 lines · Peter Maydell

This patchset converts a handful of network devices to use ld*_p() and st*_p() instead of cpu_to_*w() and *_to_cpup().

This is the last lot of conversion patches; I have the "delete the implementations from bswap.h" patch, and will send that out once all of the in-flight patches have hit master.

thanks
-- PMM

Peter Maydell (5):

   hw/net/eepro100.c: Don't use cpu_to_*w() and *_to_cpup()
   hw/net/rtl8139.c: Don't use *_to_cpup()
   hw/net/rocker: Don't use *_to_cpup()
   hw/net/virtio-net.c: Don't use *_to_cpup()
   hw/net/e1000: Don't use *_to_cpup()
Quoted 9 lines
  hw/net/e1000.c             | 18 +++++++++---------
  hw/net/e1000e_core.c       |  6 +++---
  hw/net/e1000x_common.c     |  2 +-
  hw/net/eepro100.c          |  8 ++++----
  hw/net/rocker/rocker_tlv.h |  6 +++---
  hw/net/rtl8139.c           |  9 ++++-----
  hw/net/virtio-net.c        |  2 +-
  7 files changed, 25 insertions(+), 26 deletions(-)
#8 lore raw
Quoted 34 lines · Jason Wang
Quoted 17 lines · Peter Maydell

This patchset converts a handful of network devices to use ld*_p() and st*_p() instead of cpu_to_*w() and *_to_cpup().

This is the last lot of conversion patches; I have the "delete the implementations from bswap.h" patch, and will send that out once all of the in-flight patches have hit master.

thanks
-- PMM

Peter Maydell (5):
hw/net/eepro100.c: Don't use cpu_to_*w() and *_to_cpup() hw/net/rtl8139.c: Don't use *_to_cpup() hw/net/rocker: Don't use *_to_cpup() hw/net/virtio-net.c: Don't use *_to_cpup() hw/net/e1000: Don't use *_to_cpup()

Quoted 9 lines
 hw/net/e1000.c             | 18 +++++++++---------
 hw/net/e1000e_core.c       |  6 +++---
 hw/net/e1000x_common.c     |  2 +-
 hw/net/eepro100.c          |  8 ++++----
 hw/net/rocker/rocker_tlv.h |  6 +++---
 hw/net/rtl8139.c           |  9 ++++-----
 hw/net/virtio-net.c        |  2 +-
 7 files changed, 25 insertions(+), 26 deletions(-)
Quoted 1 line
Acked-by Dmitry Fleytman <[email protected] <mailto:[email protected]>>
author Applied
#9 lore raw
In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

This patchset converts a handful of network devices to use ld*_p() and st*_p() instead of cpu_to_*w() and *_to_cpup().

This is the last lot of conversion patches; I have the "delete the implementations from bswap.h" patch, and will send that out once all of the in-flight patches have hit master.

Applied to master, thanks.

-- PMM