Skip to contentSkip to conversation

dp8393x: Reviewing CRC code

RFC PATCH In review Philippe Mathieu-Daudé 3 participants 8 messages Jul 3, 2021 – Jul 4, 2021

lore
Share & tools
lore
#0 lore raw

cover dp8393x: Reviewing CRC code

Hi Mark, few more patches while reviewing.

Again, not tested (yet)... Simply compiled.

Please tell me what you think of them.

Regards,

Phil.

Philippe Mathieu-Daudé (3):
dp8393x: Store CRC using address_space_stl_le()
dp8393x: Do not amend CRC if it is inhibited (CRCI bit set)
dp8393x: Store CRC using device configured endianess

 hw/net/dp8393x.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

RFC PATCH 1/3 dp8393x: Store CRC using address_space_stl_le()

The address_space API can handle endianess conversion. Replace cpu_to_le32() + address_space_write() by a single address_space_stl_le() call.

---
 hw/net/dp8393x.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/net/dp8393x.c +3 -3
index db9cfd786f5..99e179a5e86 100644
@@ -802,7 +802,7 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
     s->regs[SONIC_TRBA0] = s->regs[SONIC_CRBA0];
 
     /* Calculate the ethernet checksum */
-    checksum = cpu_to_le32(crc32(0, buf, pkt_size));
+    checksum = crc32(0, buf, pkt_size);
 
     /* Put packet into RBA */
     trace_dp8393x_receive_packet(dp8393x_crba(s));
@@ -812,8 +812,8 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
     address += pkt_size;
 
     /* Put frame checksum into RBA */
-    address_space_write(&s->as, address, MEMTXATTRS_UNSPECIFIED,
-                        &checksum, sizeof(checksum));
+    address_space_stl_le(&s->as, address, checksum, MEMTXATTRS_UNSPECIFIED,
+                         NULL);
     address += sizeof(checksum);
 
     /* Pad short packets to keep pointers aligned */
Signature · 1 line
2.31.1
#2 lore raw

RFC PATCH 2/3 dp8393x: Do not amend CRC if it is inhibited (CRCI bit set)

In reply to Philippe Mathieu-Daudé

When the CRCI (CRC INHIBIT) bit is set, the 4-byte FCS field is not transmitted.

---
 hw/net/dp8393x.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
hw/net/dp8393x.c +14 -8
index 99e179a5e86..dee8236400c 100644
@@ -472,6 +472,7 @@ static void dp8393x_do_transmit_packets(dp8393xState *s)
              */
         } else {
             /* Remove existing FCS */
+            /* TODO check crc */
             tx_len -= 4;
             if (tx_len < 0) {
                 trace_dp8393x_transmit_txlen_error(tx_len);
@@ -758,7 +759,10 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
         return pkt_size;
     }
 
-    rx_len = pkt_size + sizeof(checksum);
+    rx_len = pkt_size;
+    if (s->regs[SONIC_TCR] & SONIC_TCR_CRCI) {
+        rx_len += sizeof(checksum);
+    }
     if (s->regs[SONIC_DCR] & SONIC_DCR_DW) {
         padded_len = ((rx_len - 1) | 3) + 1;
     } else {
@@ -801,9 +805,6 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
     s->regs[SONIC_TRBA1] = s->regs[SONIC_CRBA1];
     s->regs[SONIC_TRBA0] = s->regs[SONIC_CRBA0];
 
-    /* Calculate the ethernet checksum */
-    checksum = crc32(0, buf, pkt_size);
-
     /* Put packet into RBA */
     trace_dp8393x_receive_packet(dp8393x_crba(s));
     address = dp8393x_crba(s);
@@ -811,10 +812,15 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
                         buf, pkt_size);
     address += pkt_size;
 
-    /* Put frame checksum into RBA */
-    address_space_stl_le(&s->as, address, checksum, MEMTXATTRS_UNSPECIFIED,
-                         NULL);
-    address += sizeof(checksum);
+    if (s->regs[SONIC_TCR] & SONIC_TCR_CRCI) {
+        /* Calculate the ethernet checksum */
+        checksum = crc32(0, buf, pkt_size);
+
+        /* Put frame checksum into RBA */
+        address_space_stl_le(&s->as, address, checksum, MEMTXATTRS_UNSPECIFIED,
+                             NULL);
+        address += sizeof(checksum);
+    }
 
     /* Pad short packets to keep pointers aligned */
     if (rx_len < padded_len) {
Signature · 1 line
2.31.1
#3 lore raw

Re: [RFC PATCH 2/3] dp8393x: Do not amend CRC if it is inhibited (CRCI bit set)

Quoted 2 lines · Philippe Mathieu-Daudé

When the CRCI (CRC INHIBIT) bit is set, the 4-byte FCS field is not transmitted.

You say when CRCI is 1 then no checksum...

Quoted 19 lines

---
hw/net/dp8393x.c | 22 ++++++++++++++--------

1 file changed, 14 insertions(+), 8 deletions(-)
hw/net/dp8393x.c +1 -0
index 99e179a5e86..dee8236400c 100644
@@ -472,6 +472,7 @@ static void dp8393x_do_transmit_packets(dp8393xState *s)
             */
        } else {
            /* Remove existing FCS */
+            /* TODO check crc */
            tx_len -= 4;
            if (tx_len < 0) {
                trace_dp8393x_transmit_txlen_error(tx_len);

hw/net/dp8393x.c static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf, · line 763

Quoted 6 lines
hw/net/dp8393x.c +2 -1
@@ -758,7 +759,10 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
        return pkt_size;
    }
 
-    rx_len = pkt_size + sizeof(checksum);
+    rx_len = pkt_size;
+    if (s->regs[SONIC_TCR] & SONIC_TCR_CRCI) {

... but you seem to add checksum if bit is set.

Quoted 16 lines
hw/net/dp8393x.c +2 -3
+        rx_len += sizeof(checksum);
+    }
    if (s->regs[SONIC_DCR] & SONIC_DCR_DW) {
        padded_len = ((rx_len - 1) | 3) + 1;
    } else {
@@ -801,9 +805,6 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
    s->regs[SONIC_TRBA1] = s->regs[SONIC_CRBA1];
    s->regs[SONIC_TRBA0] = s->regs[SONIC_CRBA0];
 
-    /* Calculate the ethernet checksum */
-    checksum = crc32(0, buf, pkt_size);
-
    /* Put packet into RBA */
    trace_dp8393x_receive_packet(dp8393x_crba(s));
    address = dp8393x_crba(s);

hw/net/dp8393x.c static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf, · line 815

Quoted 8 lines
hw/net/dp8393x.c +1 -4
@@ -811,10 +812,15 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
                        buf, pkt_size);
    address += pkt_size;
 
-    /* Put frame checksum into RBA */
-    address_space_stl_le(&s->as, address, checksum, MEMTXATTRS_UNSPECIFIED,
-                         NULL);
-    address += sizeof(checksum);
+    if (s->regs[SONIC_TCR] & SONIC_TCR_CRCI) {

Here too. Either these should be inverted or the commit message is wrong if the bit is active 0 (or I'm not getting this alltogether which is also possible as I've just had a quick look without really understanding it).

Regards,
BALATON Zoltan

Quoted 12 lines
hw/net/dp8393x.c +8 -0
+        /* Calculate the ethernet checksum */
+        checksum = crc32(0, buf, pkt_size);
+
+        /* Put frame checksum into RBA */
+        address_space_stl_le(&s->as, address, checksum, MEMTXATTRS_UNSPECIFIED,
+                             NULL);
+        address += sizeof(checksum);
+    }
 
    /* Pad short packets to keep pointers aligned */
    if (rx_len < padded_len) {
#4 lore raw

Re: [RFC PATCH 2/3] dp8393x: Do not amend CRC if it is inhibited (CRCI bit set)

Quoted 53 lines · BALATON Zoltan
Quoted 2 lines · Philippe Mathieu-Daudé

When the CRCI (CRC INHIBIT) bit is set, the 4-byte FCS field is not transmitted.

You say when CRCI is 1 then no checksum...

Quoted 19 lines

---
hw/net/dp8393x.c | 22 ++++++++++++++--------

1 file changed, 14 insertions(+), 8 deletions(-)
hw/net/dp8393x.c +1 -0
index 99e179a5e86..dee8236400c 100644
@@ -472,6 +472,7 @@ static void dp8393x_do_transmit_packets(dp8393xState *s)
             */
        } else {
            /* Remove existing FCS */
+            /* TODO check crc */
            tx_len -= 4;
            if (tx_len < 0) {
                trace_dp8393x_transmit_txlen_error(tx_len);

hw/net/dp8393x.c static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf, · line 763

Quoted 6 lines
hw/net/dp8393x.c +2 -1
@@ -758,7 +759,10 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
        return pkt_size;
    }
 
-    rx_len = pkt_size + sizeof(checksum);
+    rx_len = pkt_size;
+    if (s->regs[SONIC_TCR] & SONIC_TCR_CRCI) {

... but you seem to add checksum if bit is set.

Quoted 19 lines
hw/net/dp8393x.c +2 -3
+        rx_len += sizeof(checksum);
+    }
    if (s->regs[SONIC_DCR] & SONIC_DCR_DW) {
        padded_len = ((rx_len - 1) | 3) + 1;
    } else {
@@ -801,9 +805,6 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
    s->regs[SONIC_TRBA1] = s->regs[SONIC_CRBA1];
    s->regs[SONIC_TRBA0] = s->regs[SONIC_CRBA0];
 
-    /* Calculate the ethernet checksum */
-    checksum = crc32(0, buf, pkt_size);
-
    /* Put packet into RBA */
    trace_dp8393x_receive_packet(dp8393x_crba(s));
    address = dp8393x_crba(s);
@@ -811,10 +812,15 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
                        buf, pkt_size);
    address += pkt_size;
 
Quoted 9 lines

hw/net/dp8393x.c static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf, · line 815

Quoted 5 lines
hw/net/dp8393x.c +1 -4
-    /* Put frame checksum into RBA */
-    address_space_stl_le(&s->as, address, checksum, MEMTXATTRS_UNSPECIFIED,
-                         NULL);
-    address += sizeof(checksum);
+    if (s->regs[SONIC_TCR] & SONIC_TCR_CRCI) {

Here too. Either these should be inverted or the commit message is wrong if the bit is active 0 (or I'm not getting this alltogether which is also possible as I've just had a quick look without really understanding it).

You are right indeed, this should be the opposite.

Thanks!

Phil.

#5 lore raw

Re: [RFC PATCH 2/3] dp8393x: Do not amend CRC if it is inhibited (CRCI bit set)

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

When the CRCI (CRC INHIBIT) bit is set, the 4-byte FCS field is not transmitted.

---
 hw/net/dp8393x.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
hw/net/dp8393x.c +0 -0
index 99e179a5e86..dee8236400c 100644

hw/net/dp8393x.c static void dp8393x_do_transmit_packets(dp8393xState *s) · line 475

Quoted 4 lines
hw/net/dp8393x.c +1 -0
@@ -472,6 +472,7 @@ static void dp8393x_do_transmit_packets(dp8393xState *s)
              */
         } else {
             /* Remove existing FCS */
+            /* TODO check crc */

I don't understand this comment. Why would you check the CRC when it's meant to be discarded here? (This is the CRCI enabled case.)

Quoted 5 lines
hw/net/dp8393x.c +0 -0
             tx_len -= 4;
             if (tx_len < 0) {
                 trace_dp8393x_transmit_txlen_error(tx_len);
@@ -758,7 +759,10 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
         return pkt_size;

hw/net/dp8393x.c static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf, · line 768

Quoted 10 lines
hw/net/dp8393x.c +4 -1
     }
 
-    rx_len = pkt_size + sizeof(checksum);
+    rx_len = pkt_size;
+    if (s->regs[SONIC_TCR] & SONIC_TCR_CRCI) {
+        rx_len += sizeof(checksum);
+    }
     if (s->regs[SONIC_DCR] & SONIC_DCR_DW) {
         padded_len = ((rx_len - 1) | 3) + 1;
     } else {

This is in dp8393x_receive(), but CRCI does not apply to the recieve side of the chip.

Quoted 11 lines
hw/net/dp8393x.c +0 -3
@@ -801,9 +805,6 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
     s->regs[SONIC_TRBA1] = s->regs[SONIC_CRBA1];
     s->regs[SONIC_TRBA0] = s->regs[SONIC_CRBA0];
 
-    /* Calculate the ethernet checksum */
-    checksum = crc32(0, buf, pkt_size);
-
     /* Put packet into RBA */
     trace_dp8393x_receive_packet(dp8393x_crba(s));
     address = dp8393x_crba(s);

hw/net/dp8393x.c static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf, · line 815

Quoted 8 lines
hw/net/dp8393x.c +1 -4
@@ -811,10 +812,15 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
                         buf, pkt_size);
     address += pkt_size;
 
-    /* Put frame checksum into RBA */
-    address_space_stl_le(&s->as, address, checksum, MEMTXATTRS_UNSPECIFIED,
-                         NULL);
-    address += sizeof(checksum);
+    if (s->regs[SONIC_TCR] & SONIC_TCR_CRCI) {

Same mistake here.

hw/net/dp8393x.c static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf, · line 826

Quoted 12 lines
hw/net/dp8393x.c +8 -0
+        /* Calculate the ethernet checksum */
+        checksum = crc32(0, buf, pkt_size);
+
+        /* Put frame checksum into RBA */
+        address_space_stl_le(&s->as, address, checksum, MEMTXATTRS_UNSPECIFIED,
+                             NULL);
+        address += sizeof(checksum);
+    }
 
     /* Pad short packets to keep pointers aligned */
     if (rx_len < padded_len) {

Anyway, I think you are right about the FCS endianness error (which you address in the next patch).

#6 lore raw

RFC PATCH 3/3 dp8393x: Store CRC using device configured endianess

In reply to Philippe Mathieu-Daudé

Little-Endian CRC is dubious, and the datasheet does not specify it being little-endian. Proceed similarly with the other memory accesses, use the device endianess.

---
 hw/net/dp8393x.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
hw/net/dp8393x.c +6 -2
index dee8236400c..3a07f5c8ac9 100644
@@ -817,8 +817,12 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
         checksum = crc32(0, buf, pkt_size);
 
         /* Put frame checksum into RBA */
-        address_space_stl_le(&s->as, address, checksum, MEMTXATTRS_UNSPECIFIED,
-                             NULL);
+        if (s->big_endian) {
+            address_space_stl_be(&s->as, address, checksum,
+                                 MEMTXATTRS_UNSPECIFIED, NULL);
+        } else {
+            address_space_stl_le(&s->as, address, checksum,
+                                 MEMTXATTRS_UNSPECIFIED, NULL);
         address += sizeof(checksum);
     }
 
Signature · 1 line
2.31.1
#7 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 11 lines · Philippe Mathieu-Daudé

Hi Mark, few more patches while reviewing.

Again, not tested (yet)... Simply compiled.

Please tell me what you think of them.

I think these 3 patches can be reduced to this theoretical bug fix:

hw/net/dp8393x.c +8 -3
index db9cfd786f..e278daebc5 100644
@@ -802,7 +802,7 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
     s->regs[SONIC_TRBA0] = s->regs[SONIC_CRBA0];
 
     /* Calculate the ethernet checksum */
-    checksum = cpu_to_le32(crc32(0, buf, pkt_size));
+    checksum = crc32(0, buf, pkt_size);
 
     /* Put packet into RBA */
     trace_dp8393x_receive_packet(dp8393x_crba(s));
@@ -812,8 +812,13 @@ static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
     address += pkt_size;
 
     /* Put frame checksum into RBA */
-    address_space_write(&s->as, address, MEMTXATTRS_UNSPECIFIED,
-                        &checksum, sizeof(checksum));
+    if (s->big_endian) {
+        address_space_stl_be(&s->as, address, checksum,
+                             MEMTXATTRS_UNSPECIFIED, NULL);
+    } else {
+        address_space_stl_le(&s->as, address, checksum,
+                             MEMTXATTRS_UNSPECIFIED, NULL);
+    }
     address += sizeof(checksum);
 
     /* Pad short packets to keep pointers aligned */
Quoted 32 lines

Regards,

Phil.

Philippe Mathieu-Daudé (3):

dp8393x: Store CRC using address_space_stl_le()

dp8393x: Do not amend CRC if it is inhibited (CRCI bit set)

dp8393x: Store CRC using device configured endianess

 hw/net/dp8393x.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
Signature · 1 line
2.31.1