Skip to contentSkip to conversation

Add Ethernet device for i.MX6 SOC

PATCH Superseded Jean-Christophe Dubois 2 participants 9 messages Apr 23, 2016 – May 6, 2016

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add Ethernet device for i.MX6 SOC

This patch series adds Gb ENET Ethernet device to the i.MX6 SOC.

The ENET device is an evolution of the FEC device present on the i.MX25 SOC and is backward compatible with it.

Therefore the ENET support has been added to the actual Qemu FEC device.

The Patch has been tested by:
* Booting linux on i.MX25 PDK board emulation and accessing internet
* Booting linux on i.MX6 Sabrelite board emulation and accessing internet

Jean-Christophe Dubois (3):
net: improve UDP/TCP checksum computation.
Add ENET/Gbps Ethernet support to FEC device
Add ENET device to i.MX6 SOC.

 hw/arm/fsl-imx25.c        |   3 +
 hw/arm/fsl-imx6.c         |  17 +
 hw/net/imx_fec.c          | 984 ++++++++++++++++++++++++++++++++++------------
 include/hw/arm/fsl-imx6.h |   2 +
 include/hw/net/imx_fec.h  | 243 +++++++++---
 net/checksum.c            |  83 ++--
 6 files changed, 1005 insertions(+), 327 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/3 net: improve UDP/TCP checksum computation.

This patch adds:
* based on Eth, UDP, TCP struct present in eth.h instead of hardcoded indexes.
* based on various macros present in eth.h.
* allow to account for optional VLAN header.

---
 net/checksum.c | 83 ++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 57 insertions(+), 26 deletions(-)
net/checksum.c +57 -26
index d0fa424..fd25209 100644
@@ -18,9 +18,7 @@
 #include "qemu/osdep.h"
 #include "qemu-common.h"
 #include "net/checksum.h"
-
-#define PROTO_TCP  6
-#define PROTO_UDP 17
+#include "net/eth.h"
 
 uint32_t net_checksum_add_cont(int len, uint8_t *buf, int seq)
 {
@@ -57,40 +55,73 @@ uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
 
 void net_checksum_calculate(uint8_t *data, int length)
 {
-    int hlen, plen, proto, csum_offset;
-    uint16_t csum;
+    int plen;
+    struct ip_header *ip;
+
+    /* Ensure we have at least a Eth header */
+    if (length < sizeof(struct eth_header)) {
+        return;
+    }
 
-    /* Ensure data has complete L2 & L3 headers. */
-    if (length < 14 + 20) {
+    /* Now check we have an IP header (with an optonnal VLAN header */
+    if (length < eth_get_l2_hdr_length(data) + sizeof(struct ip_header)) {
         return;
     }
 
-    if ((data[14] & 0xf0) != 0x40)
+    ip = PKT_GET_IP_HDR(data);
+
+    if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
 	return; /* not IPv4 */
-    hlen  = (data[14] & 0x0f) * 4;
-    plen  = (data[16] << 8 | data[17]) - hlen;
-    proto = data[23];
+    }
+
+    /* Last, check that we have enough data for the IP frame */
+    if (length < eth_get_l2_hdr_length(data) + be16_to_cpu(ip->ip_len)) {
+        return;
+    }
+
+    plen  = be16_to_cpu(ip->ip_len) - IP_HDR_GET_LEN(ip);
+
+    switch (ip->ip_p) {
+    case IP_PROTO_TCP:
+        {
+            uint16_t csum;
+            tcp_header *tcp = (tcp_header *)(ip + 1);
+
+            if (plen < sizeof(tcp_header)) {
+                return;
+            }
 
-    switch (proto) {
-    case PROTO_TCP:
-	csum_offset = 16;
+            tcp->th_sum = 0;
+
+            csum = net_checksum_tcpudp(plen, ip->ip_p,
+                                       (uint8_t *)&ip->ip_src,
+                                       (uint8_t *)tcp);
+
+            tcp->th_sum = cpu_to_be16(csum);
+        }
 	break;
-    case PROTO_UDP:
-	csum_offset = 6;
+    case IP_PROTO_UDP:
+        {
+            uint16_t csum;
+            udp_header *udp = (udp_header *)(ip + 1);
+
+            if (plen < sizeof(udp_header)) {
+                return;
+            }
+
+            udp->uh_sum = 0;
+
+            csum = net_checksum_tcpudp(plen, ip->ip_p,
+                                       (uint8_t *)&ip->ip_src,
+                                       (uint8_t *)udp);
+
+            udp->uh_sum = cpu_to_be16(csum);
+        }
 	break;
     default:
+        /* Can't handle any other protocol */
 	return;
     }
-
-    if (plen < csum_offset + 2 || 14 + hlen + plen > length) {
-        return;
-    }
-
-    data[14+hlen+csum_offset]   = 0;
-    data[14+hlen+csum_offset+1] = 0;
-    csum = net_checksum_tcpudp(plen, proto, data+14+12, data+14+hlen);
-    data[14+hlen+csum_offset]   = csum >> 8;
-    data[14+hlen+csum_offset+1] = csum & 0xff;
 }
 
 uint32_t
Signature · 1 line
2.7.4
#7 lore raw

PATCH v2 3/3 Add ENET device to i.MX6 SOC.

In reply to Jean-Christophe Dubois

This adds the ENET device to the i.MX6 SOC.

This was tested by booting Linux on an Qemu i.MX6 instance and accessing the internet from the linux guest.

---
 hw/arm/fsl-imx6.c         | 17 +++++++++++++++++
 include/hw/arm/fsl-imx6.h |  2 ++
 2 files changed, 19 insertions(+)
hw/arm/fsl-imx6.c +17 -0
index a5331bf..c08222c 100644
@@ -105,6 +105,10 @@ static void fsl_imx6_init(Object *obj)
         snprintf(name, NAME_SIZE, "spi%d", i + 1);
         object_property_add_child(obj, name, OBJECT(&s->spi[i]), NULL);
     }
+
+    object_initialize(&s->eth, sizeof(s->eth), TYPE_IMX_FEC);
+    qdev_set_parent_bus(DEVICE(&s->eth), sysbus_get_default());
+    object_property_add_child(obj, "eth", OBJECT(&s->eth), NULL);
 }
 
 static void fsl_imx6_realize(DeviceState *dev, Error **errp)
@@ -381,6 +385,19 @@ static void fsl_imx6_realize(DeviceState *dev, Error **errp)
                                             spi_table[i].irq));
     }
 
+    object_property_set_bool(OBJECT(&s->eth), true, "realized", &err);
+    if (err) {
+        error_propagate(errp, err);
+        return;
+    }
+    sysbus_mmio_map(SYS_BUS_DEVICE(&s->eth), 0, FSL_IMX6_ENET_ADDR);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->eth), 0,
+                       qdev_get_gpio_in(DEVICE(&s->a9mpcore),
+                                        FSL_IMX6_ENET_MAC_IRQ));
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->eth), 1,
+                       qdev_get_gpio_in(DEVICE(&s->a9mpcore),
+                                        FSL_IMX6_ENET_MAC_1588_IRQ));
+
     /* ROM memory */
     memory_region_init_rom_device(&s->rom, NULL, NULL, NULL, "imx6.rom",
                                   FSL_IMX6_ROM_SIZE, &err);
include/hw/arm/fsl-imx6.h +2 -0
index d24aaee..98b7599 100644
@@ -28,6 +28,7 @@
 #include "hw/gpio/imx_gpio.h"
 #include "hw/sd/sdhci.h"
 #include "hw/ssi/imx_spi.h"
+#include "hw/net/imx_fec.h"
 #include "exec/memory.h"
 
 #define TYPE_FSL_IMX6 "fsl,imx6"
@@ -57,6 +58,7 @@ typedef struct FslIMX6State {
     IMXGPIOState   gpio[FSL_IMX6_NUM_GPIOS];
     SDHCIState     esdhc[FSL_IMX6_NUM_ESDHCS];
     IMXSPIState    spi[FSL_IMX6_NUM_ECSPIS];
+    IMXFECState    eth;
     MemoryRegion   rom;
     MemoryRegion   caam;
     MemoryRegion   ocram;
Signature · 1 line
2.7.4
#5 lore raw

PATCH v2 2/3 Add ENET/Gbps Ethernet support to FEC device

In reply to Jean-Christophe Dubois

The ENET device (present in i.MX6) is "derived" from FEC and backward compatible with it.

This patch add the necessary support of the added feature in the ENET device to allow Linux to use it (on supported processors).

---
 hw/arm/fsl-imx25.c       |   3 +
 hw/net/imx_fec.c         | 984 +++++++++++++++++++++++++++++++++++------------
 include/hw/net/imx_fec.h | 243 +++++++++---
 3 files changed, 929 insertions(+), 301 deletions(-)
hw/arm/fsl-imx25.c +3 -0
index 2f878b9..ddb2b22 100644
@@ -191,6 +191,9 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
     }
 
     qdev_set_nic_properties(DEVICE(&s->fec), &nd_table[0]);
+
+    object_property_set_bool(OBJECT(&s->fec), true, "is-fec", &error_abort);
+
     object_property_set_bool(OBJECT(&s->fec), true, "realized", &err);
     if (err) {
         error_propagate(errp, err);
hw/net/imx_fec.c +737 -247

1,240 lines not on this page · raw message

include/hw/net/imx_fec.h +189 -54

276 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] net: improve UDP/TCP checksum computation.

In reply to Jean-Christophe Dubois
Quoted 4 lines · Jean-Christophe Dubois

This patch adds:
* based on Eth, UDP, TCP struct present in eth.h instead of hardcoded indexes.
* based on various macros present in eth.h.
* allow to account for optional VLAN header.

This is doing several things:
(1) changing style to use structs and macros rather than raw array accesses (which shouldn't affect functionality)
(2) adding new functionality

I think they would be better in separate patches.

This function is used by multiple network devices, including the important ones xen_nic and virtio-net -- is it definitely OK to have it change behaviour for those devices?

Quoted 27 lines
---
 net/checksum.c | 83 ++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 57 insertions(+), 26 deletions(-)
net/checksum.c +2 -5
index d0fa424..fd25209 100644
@@ -18,9 +18,7 @@
 #include "qemu/osdep.h"
 #include "qemu-common.h"
 #include "net/checksum.h"
-
-#define PROTO_TCP  6
-#define PROTO_UDP 17
+#include "net/eth.h"
 
 uint32_t net_checksum_add_cont(int len, uint8_t *buf, int seq)
 {
@@ -57,40 +55,73 @@ uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
 
 void net_checksum_calculate(uint8_t *data, int length)
 {
-    int hlen, plen, proto, csum_offset;
-    uint16_t csum;
+    int plen;

net/checksum.c uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto, · line 66

Quoted 10 lines
net/checksum.c +7 -2
+    struct ip_header *ip;
+
+    /* Ensure we have at least a Eth header */
+    if (length < sizeof(struct eth_header)) {
+        return;
+    }
 
-    /* Ensure data has complete L2 & L3 headers. */
-    if (length < 14 + 20) {
+    /* Now check we have an IP header (with an optonnal VLAN header */

"optional", also missing ")".

net/checksum.c uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto, · line 71

Quoted 6 lines
net/checksum.c +2 -1
+    if (length < eth_get_l2_hdr_length(data) + sizeof(struct ip_header)) {
         return;
     }
 
-    if ((data[14] & 0xf0) != 0x40)
+    ip = PKT_GET_IP_HDR(data);

Are we definitely guaranteed that the input data buffer is aligned? It seems unlikely, and if it isn't then a lot of this code (both in macros like PKT_GET_IP_HDR, and open coded stuff below) is going to go wrong if it tries to just access 16 bit struct fields and they're not aligned and we're on a host that requires strict alignment.

Quoted 14 lines
net/checksum.c +10 -3
+
+    if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
        return; /* not IPv4 */
-    hlen  = (data[14] & 0x0f) * 4;
-    plen  = (data[16] << 8 | data[17]) - hlen;
-    proto = data[23];
+    }
+
+    /* Last, check that we have enough data for the IP frame */
+    if (length < eth_get_l2_hdr_length(data) + be16_to_cpu(ip->ip_len)) {
+        return;
+    }
+
+    plen  = be16_to_cpu(ip->ip_len) - IP_HDR_GET_LEN(ip);

net/checksum.c uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto, · line 92

Quoted 10 lines
net/checksum.c +10 -0
+
+    switch (ip->ip_p) {
+    case IP_PROTO_TCP:
+        {
+            uint16_t csum;
+            tcp_header *tcp = (tcp_header *)(ip + 1);
+
+            if (plen < sizeof(tcp_header)) {
+                return;
+            }

I think this indent style is indenting to much. switch statements usually look like:

    switch (whatever) {
    case FOO:
    {
        code here;
    }
    case BAR:
    {
        more code;
    }
    default:
        whatever;
    }
Quoted 51 lines
net/checksum.c +26 -16
-    switch (proto) {
-    case PROTO_TCP:
-       csum_offset = 16;
+            tcp->th_sum = 0;
+
+            csum = net_checksum_tcpudp(plen, ip->ip_p,
+                                       (uint8_t *)&ip->ip_src,
+                                       (uint8_t *)tcp);
+
+            tcp->th_sum = cpu_to_be16(csum);
+        }
        break;
-    case PROTO_UDP:
-       csum_offset = 6;
+    case IP_PROTO_UDP:
+        {
+            uint16_t csum;
+            udp_header *udp = (udp_header *)(ip + 1);
+
+            if (plen < sizeof(udp_header)) {
+                return;
+            }
+
+            udp->uh_sum = 0;
+
+            csum = net_checksum_tcpudp(plen, ip->ip_p,
+                                       (uint8_t *)&ip->ip_src,
+                                       (uint8_t *)udp);
+
+            udp->uh_sum = cpu_to_be16(csum);
+        }
        break;
     default:
+        /* Can't handle any other protocol */
        return;
     }
-
-    if (plen < csum_offset + 2 || 14 + hlen + plen > length) {
-        return;
-    }
-
-    data[14+hlen+csum_offset]   = 0;
-    data[14+hlen+csum_offset+1] = 0;
-    csum = net_checksum_tcpudp(plen, proto, data+14+12, data+14+hlen);
-    data[14+hlen+csum_offset]   = csum >> 8;
-    data[14+hlen+csum_offset+1] = csum & 0xff;
 }
 
 uint32_t
--
Quoted 1 line

2.7.4

thanks
-- PMM

#6 lore raw

Re: [Qemu-devel] [PATCH v2 2/3] Add ENET/Gbps Ethernet support to FEC device

In reply to Jean-Christophe Dubois
Quoted 6 lines · Jean-Christophe Dubois

The ENET device (present in i.MX6) is "derived" from FEC and backward compatible with it.

This patch add the necessary support of the added feature in the ENET device to allow Linux to use it (on supported processors).

Quoted 6 lines
---
 hw/arm/fsl-imx25.c       |   3 +
 hw/net/imx_fec.c         | 984 +++++++++++++++++++++++++++++++++++------------
 include/hw/net/imx_fec.h | 243 +++++++++---
 3 files changed, 929 insertions(+), 301 deletions(-)

I'm afraid this patch is too large for me to review, and quickly scanning through the patch makes it look like it's half "new feature" and half "rewrite existing stuff". Please can you split this up somehow so it's easier to review?

thanks
-- PMM

#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] net: improve UDP/TCP checksum computation.

In reply to Peter Maydell
Quoted 11 lines · Peter Maydell
Quoted 4 lines · Jean-Christophe Dubois

This patch adds:
* based on Eth, UDP, TCP struct present in eth.h instead of hardcoded indexes.
* based on various macros present in eth.h.
* allow to account for optional VLAN header.

This is doing several things:
(1) changing style to use structs and macros rather than raw array accesses (which shouldn't affect functionality)
(2) adding new functionality

I think they would be better in separate patches.

Well, the added functionality comes as a bonus because of the use of the struct and macros. It is not so easy to split the 2.

Quoted 5 lines

This function is used by multiple network devices, including the important ones xen_nic and virtio-net -- is it definitely OK to have it change behaviour for those devices?

If xen_nic never tries to support VLAN then there is no real change in behavior. This patch adds the VLAN support which is a legal network frame format which was unsupported so far.

VLAN is supported by the i.MX6 Gb Ethernet device accelerator. Therefore, I needed a checksum function that would account for it.

Quoted 36 lines
Quoted 36 lines
---
  net/checksum.c | 83 ++++++++++++++++++++++++++++++++++++++++------------------
  1 file changed, 57 insertions(+), 26 deletions(-)
net/checksum.c +8 -5
index d0fa424..fd25209 100644
@@ -18,9 +18,7 @@
  #include "qemu/osdep.h"
  #include "qemu-common.h"
  #include "net/checksum.h"
-
-#define PROTO_TCP  6
-#define PROTO_UDP 17
+#include "net/eth.h"
 
  uint32_t net_checksum_add_cont(int len, uint8_t *buf, int seq)
  {
@@ -57,40 +55,73 @@ uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
 
  void net_checksum_calculate(uint8_t *data, int length)
  {
-    int hlen, plen, proto, csum_offset;
-    uint16_t csum;
+    int plen;
+    struct ip_header *ip;
+
+    /* Ensure we have at least a Eth header */
+    if (length < sizeof(struct eth_header)) {
+        return;
+    }
 
Quoted 4 lines

net/checksum.c uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto, · line 66

Quoted 3 lines
net/checksum.c +1 -2
-    /* Ensure data has complete L2 & L3 headers. */
-    if (length < 14 + 20) {
+    /* Now check we have an IP header (with an optonnal VLAN header */

"optional", also missing ")".

OK

Quoted 5 lines
Quoted 5 lines
net/checksum.c +1 -0
+    if (length < eth_get_l2_hdr_length(data) + sizeof(struct ip_header)) {
          return;
      }
 
Quoted 7 lines

net/checksum.c uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto, · line 71

Quoted 2 lines
net/checksum.c +1 -1
-    if ((data[14] & 0xf0) != 0x40)
+    ip = PKT_GET_IP_HDR(data);

Are we definitely guaranteed that the input data buffer is aligned? It seems unlikely, and if it isn't then a lot of this code (both in macros like PKT_GET_IP_HDR, and open coded stuff below) is going to go wrong if it tries to just access 16 bit struct fields and they're not aligned and we're on a host that requires strict alignment.

Beside a potential performance hit on unaligned data (but is it worst than accessing all struct members at byte level) is there any Qemu host that cannot handle unaligned struct members?

Now most network stacks run by the host or the guest should generally used aligned pointers for network buffers. The opposite should be quite uncommon. It seems most (emulated) Ethernet chip expect aligned buffers to work and therefore the provided pointer should always be correctly set to allow aligned access.

Do you have a different experience in Qemu?

Quoted 19 lines
Quoted 19 lines
net/checksum.c +14 -3
+
+    if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
         return; /* not IPv4 */
-    hlen  = (data[14] & 0x0f) * 4;
-    plen  = (data[16] << 8 | data[17]) - hlen;
-    proto = data[23];
+    }
+
+    /* Last, check that we have enough data for the IP frame */
+    if (length < eth_get_l2_hdr_length(data) + be16_to_cpu(ip->ip_len)) {
+        return;
+    }
+
+    plen  = be16_to_cpu(ip->ip_len) - IP_HDR_GET_LEN(ip);
+
+    switch (ip->ip_p) {
+    case IP_PROTO_TCP:
+        {
Quoted 8 lines

net/checksum.c uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto, · line 92

Quoted 6 lines
net/checksum.c +6 -0
+            uint16_t csum;
+            tcp_header *tcp = (tcp_header *)(ip + 1);
+
+            if (plen < sizeof(tcp_header)) {
+                return;
+            }

I think this indent style is indenting to much. switch statements usually look like:

OK

Quoted 68 lines
     switch (whatever) {
     case FOO:
     {
         code here;
     }
     case BAR:
     {
         more code;
     }
     default:
         whatever;
     }
Quoted 50 lines
net/checksum.c +26 -16
-    switch (proto) {
-    case PROTO_TCP:
-       csum_offset = 16;
+            tcp->th_sum = 0;
+
+            csum = net_checksum_tcpudp(plen, ip->ip_p,
+                                       (uint8_t *)&ip->ip_src,
+                                       (uint8_t *)tcp);
+
+            tcp->th_sum = cpu_to_be16(csum);
+        }
         break;
-    case PROTO_UDP:
-       csum_offset = 6;
+    case IP_PROTO_UDP:
+        {
+            uint16_t csum;
+            udp_header *udp = (udp_header *)(ip + 1);
+
+            if (plen < sizeof(udp_header)) {
+                return;
+            }
+
+            udp->uh_sum = 0;
+
+            csum = net_checksum_tcpudp(plen, ip->ip_p,
+                                       (uint8_t *)&ip->ip_src,
+                                       (uint8_t *)udp);
+
+            udp->uh_sum = cpu_to_be16(csum);
+        }
         break;
      default:
+        /* Can't handle any other protocol */
         return;
      }
-
-    if (plen < csum_offset + 2 || 14 + hlen + plen > length) {
-        return;
-    }
-
-    data[14+hlen+csum_offset]   = 0;
-    data[14+hlen+csum_offset+1] = 0;
-    csum = net_checksum_tcpudp(plen, proto, data+14+12, data+14+hlen);
-    data[14+hlen+csum_offset]   = csum >> 8;
-    data[14+hlen+csum_offset+1] = csum & 0xff;
  }
 
  uint32_t
--
Quoted 1 line

2.7.4

thanks
-- PMM

#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] net: improve UDP/TCP checksum computation.

Quoted 19 lines · Jean-Christophe DUBOIS
Quoted 8 lines · Peter Maydell
Quoted 5 lines · Jean-Christophe Dubois

+ if (length < eth_get_l2_hdr_length(data) + sizeof(struct ip_header))

{
          return;
      }
Quoted 8 lines

net/checksum.c uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto, · line 71

Quoted 2 lines
net/checksum.c +1 -1
-    if ((data[14] & 0xf0) != 0x40)
+    ip = PKT_GET_IP_HDR(data);

Are we definitely guaranteed that the input data buffer is aligned? It seems unlikely, and if it isn't then a lot of this code (both in macros like PKT_GET_IP_HDR, and open coded stuff below) is going to go wrong if it tries to just access 16 bit struct fields and they're not aligned and we're on a host that requires strict alignment.

Quoted 3 lines

Beside a potential performance hit on unaligned data (but is it worst than accessing all struct members at byte level) is there any Qemu host that cannot handle unaligned struct members?

MIPS, for instance, also older ARM CPUs. I wouldn't be surprised if PPC also required alignment.

Quoted 5 lines

Now most network stacks run by the host or the guest should generally used aligned pointers for network buffers. The opposite should be quite uncommon. It seems most (emulated) Ethernet chip expect aligned buffers to work and therefore the provided pointer should always be correctly set to allow aligned access.

Since the buffer alignment is provided by the guest, you can't allow QEMU to crash if it's unaligned.

We have good working support for handling loading data from potentially unaligned buffers (and in particular loading data of a fixed endianness), so we should probably just use it.

thanks
-- PMM