diff mbox series

[30/44] Impl GENET TX path

Message ID 20230726132512.149618-31-sergey.kambalin@auriga.com (mailing list archive)
State New, archived
Headers show
Series Raspberry Pi 4B machine | expand

Commit Message

Sergey Kambalin July 26, 2023, 1:24 p.m. UTC
Signed-off-by: Sergey Kambalin <sergey.kambalin@auriga.com>
---
 hw/net/bcm2838_genet.c         | 170 ++++++++++++++++++++++++++++++++-
 include/hw/net/bcm2838_genet.h |  26 +++++
 2 files changed, 195 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/hw/net/bcm2838_genet.c b/hw/net/bcm2838_genet.c
index e633323d17..98030216f4 100644
--- a/hw/net/bcm2838_genet.c
+++ b/hw/net/bcm2838_genet.c
@@ -149,6 +149,174 @@  static uint64_t bcm2838_genet_mdio_cmd(BCM2838GenetState *s, uint64_t cmd)
     return umac_mdio_cmd.value;
 }
 
+static void bcm2838_genet_xmit_packet(NetClientState *s, void *packet,
+                                      size_t size)
+{
+    uint8_t *buf = packet + sizeof(BCM2838GenetXmitStatus);
+    size_t len = size;
+    uint16_t len_type = 0;
+
+    len -= sizeof(BCM2838GenetXmitStatus);
+    net_checksum_calculate(buf, len, CSUM_ALL);
+
+    memcpy(&len_type, &buf[12], sizeof(len_type));
+    len_type = ntohs(len_type);
+    if (len_type < MAX_PAYLOAD_SIZE) {
+        len_type = len;
+        len_type = htons(len_type);
+        memcpy(&buf[12], &len_type, sizeof(len_type));
+    }
+
+    qemu_send_packet(s, buf, len);
+}
+
+static uint64_t bcm2838_genet_tx(BCM2838GenetState *s, unsigned int ring_index,
+                                 BCM2838GenetDmaProdIndex prod_index,
+                                 BCM2838GenetDmaConsIndex cons_index)
+{
+    const unsigned int DESC_SIZE_WORDS
+        = sizeof(BCM2838GenetTdmaDesc) / sizeof(uint32_t);
+    const uint64_t RING_START_ADDR
+        = ((uint64_t)s->regs.tdma.rings[ring_index].start_addr_hi << 32)
+            + s->regs.tdma.rings[ring_index].start_addr;
+    const uint64_t RING_END_ADDR
+        = ((uint64_t)s->regs.tdma.rings[ring_index].end_addr_hi << 32)
+            + s->regs.tdma.rings[ring_index].end_addr;
+
+    hwaddr data_addr;
+    uint64_t desc_index;
+    BCM2838GenetTdmaLengthStatus desc_status;
+    uint64_t num_descs = 0;
+    uint64_t read_ptr
+        = ((uint64_t)s->regs.tdma.rings[ring_index].read_ptr_hi << 32)
+            + s->regs.tdma.rings[ring_index].read_ptr;
+    off_t packet_off = 0;
+
+    while (cons_index.fields.index != prod_index.fields.index) {
+        desc_index = read_ptr / DESC_SIZE_WORDS;
+        if (desc_index >= BCM2838_GENET_DMA_DESC_CNT) {
+            qemu_log_mask(
+                LOG_GUEST_ERROR,
+                "%s: invalid TX descriptor index %" PRIu64 " (exceeds %u)\n",
+                __func__, desc_index, BCM2838_GENET_DMA_DESC_CNT - 1);
+            break;
+        }
+        desc_status.value = s->regs.tdma.descs[desc_index].length_status.value;
+        data_addr = ((uint64_t)s->regs.tdma.descs[desc_index].address_hi << 32)
+            + s->regs.tdma.descs[desc_index].address_lo;
+        trace_bcm2838_genet_tx(ring_index, desc_index, desc_status.value,
+                               data_addr);
+
+        if (desc_status.fields.sop) {
+            packet_off = 0;
+        }
+
+        /* TODO: Add address_space_read() return value check */
+        address_space_read(&s->dma_as, data_addr,
+                                        MEMTXATTRS_UNSPECIFIED,
+                                        s->tx_packet + packet_off,
+                                        desc_status.fields.buflength);
+        packet_off += desc_status.fields.buflength;
+
+        if (desc_status.fields.eop) {
+            bcm2838_genet_xmit_packet(qemu_get_queue(s->nic), s->tx_packet,
+                                                     packet_off);
+            packet_off = 0;
+        }
+
+        num_descs++;
+        cons_index.fields.index++;
+        s->regs.tdma.descs[desc_index].length_status.fields.own = 1;
+        read_ptr = read_ptr == RING_END_ADDR + 1 - DESC_SIZE_WORDS
+            ? RING_START_ADDR : read_ptr + DESC_SIZE_WORDS;
+    }
+
+    s->regs.tdma.rings[ring_index].read_ptr = read_ptr;
+    s->regs.tdma.rings[ring_index].read_ptr_hi = read_ptr >> 32;
+
+    return num_descs;
+}
+
+static bool bcm2838_genet_tdma_ring_active(BCM2838GenetState *s,
+                                           unsigned int ring_index)
+{
+    uint32_t ring_mask = 1 << ring_index;
+    bool dma_en = s->regs.tdma.ctrl.fields.en == 1;
+    bool ring_en = (s->regs.tdma.ring_cfg.fields.en & ring_mask) != 0;
+    bool ring_buf_en = (s->regs.tdma.ctrl.fields.ring_buf_en & ring_mask) != 0;
+    bool active = dma_en && ring_en && ring_buf_en;
+
+    trace_bcm2838_genet_tx_dma_ring_active(ring_index,
+                                           active ? "active" : "halted");
+    return active;
+}
+
+static void bcm2838_genet_tdma(BCM2838GenetState *s, hwaddr offset,
+                               uint64_t value)
+{
+    hwaddr ring_offset;
+    uint64_t num_descs_tx;
+    unsigned int ring_index;
+    BCM2838GenetDmaConsIndex cons_index;
+    BCM2838GenetDmaRingCfg ring_cfg = {.value = value};
+    BCM2838GenetDmaCtrl ctrl = {.value = value};
+    BCM2838GenetDmaProdIndex prod_index = {.value = value};
+
+    switch (offset) {
+    case BCM2838_GENET_TDMA_RINGS
+        ... BCM2838_GENET_TDMA_RINGS + sizeof(s->regs.tdma.rings) - 1:
+        ring_index = (offset - BCM2838_GENET_TDMA_RINGS)
+            / sizeof(BCM2838GenetTdmaRing);
+        if (bcm2838_genet_tdma_ring_active(s, ring_index)) {
+            ring_offset = offset - BCM2838_GENET_TDMA_RINGS
+                - ring_index * sizeof(BCM2838GenetTdmaRing);
+            switch (ring_offset) {
+            case BCM2838_GENET_TRING_PROD_INDEX:
+                cons_index.value
+                    = s->regs.tdma.rings[ring_index].cons_index.value;
+                if (cons_index.fields.index != prod_index.fields.index) {
+                    trace_bcm2838_genet_tx_request(ring_index,
+                                                   prod_index.fields.index,
+                                                   cons_index.fields.index);
+                    num_descs_tx = bcm2838_genet_tx(s, ring_index, prod_index,
+                                                    cons_index);
+                    if (num_descs_tx > 0) {
+                        s->regs.tdma.rings[ring_index].cons_index.fields.index
+                            += num_descs_tx;
+                        if (ring_index == BCM2838_GENET_DMA_RING_DEFAULT) {
+                            s->regs.intrl0.stat.fields.txdma_mbdone = 1;
+                        } else {
+                            s->regs.intrl1.stat.fields.tx_intrs
+                                |= 1 << ring_index;
+                        }
+                    }
+                }
+                break;
+            default:
+                break;
+            }
+        }
+        break;
+    case BCM2838_GENET_TDMA_RING_CFG:
+        if (s->regs.tdma.ring_cfg.fields.en != ring_cfg.fields.en) {
+            trace_bcm2838_genet_tx_dma_ring(ring_cfg.fields.en);
+        }
+        break;
+    case BCM2838_GENET_TDMA_CTRL:
+        if (s->regs.tdma.ctrl.fields.en != ctrl.fields.en) {
+            s->regs.tdma.status.fields.disabled = s->regs.tdma.ctrl.fields.en;
+            trace_bcm2838_genet_tx_dma(
+                ctrl.fields.en == 1 ? "enabled" : "disabled");
+        }
+        if (s->regs.tdma.ctrl.fields.ring_buf_en != ctrl.fields.ring_buf_en) {
+            trace_bcm2838_genet_tx_dma_ring_buf(ctrl.fields.ring_buf_en);
+        }
+        break;
+    default:
+        break;
+    }
+}
+
 static uint64_t bcm2838_genet_read(void *opaque, hwaddr offset, unsigned size)
 {
     uint64_t value = ~0;
@@ -241,7 +409,7 @@  static void bcm2838_genet_write(void *opaque, hwaddr offset, uint64_t value,
             break;
         case BCM2838_GENET_TDMA_REGS
             ... BCM2838_GENET_TDMA_REGS + sizeof(BCM2838GenetRegsTdma) - 1:
-            qemu_log_mask(LOG_UNIMP, "TDMA isn't implemented yet");
+            bcm2838_genet_tdma(s, offset, value);
             break;
         default:
             break;
diff --git a/include/hw/net/bcm2838_genet.h b/include/hw/net/bcm2838_genet.h
index 025ac467d7..7fbe4d3ea5 100644
--- a/include/hw/net/bcm2838_genet.h
+++ b/include/hw/net/bcm2838_genet.h
@@ -110,6 +110,30 @@  OBJECT_DECLARE_SIMPLE_TYPE(BCM2838GenetState, BCM2838_GENET)
 #define BCM2838_GENET_PHY_EXP_SHD_REGS_CNT \
     (1u << (8 * SIZEOF_FIELD(BCM2838GenetPhyExpSel, reg_id)))
 
+#define MAX_FRAME_SIZE                  0xFFF
+#define MAX_PACKET_SIZE                 1518
+#define MAX_PAYLOAD_SIZE                1500
+#define TX_MIN_PKT_SIZE                 60
+
+typedef union BCM2838GenetTxCsumInfo {
+    uint32_t value;
+    struct {
+        uint32_t offset:15;
+        uint32_t proto_udp:1;
+        uint32_t start:15;
+        uint32_t lv:1;
+    };
+} BCM2838GenetTxCsumInfo;
+
+typedef struct QEMU_PACKED BCM2838GenetXmitStatus {
+    uint32_t                length_status;  /* length and peripheral status */
+    uint32_t                ext_status;     /* Extended status */
+    uint32_t                rx_csum;        /* partial rx checksum */
+    uint32_t                unused1[9];     /* unused */
+    BCM2838GenetTxCsumInfo  tx_csum_info;   /* Tx checksum info. */
+    uint32_t                unused2[3];     /* unused */
+} BCM2838GenetXmitStatus;
+
 typedef union {
     uint32_t value;
     struct {
@@ -700,6 +724,8 @@  struct BCM2838GenetState {
 
     qemu_irq irq_default;
     qemu_irq irq_prio;
+
+    uint8_t tx_packet[MAX_FRAME_SIZE];
 };
 
 #endif /* BCM2838_GENET_H */