From nobody Thu Nov 14 17:47:30 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1708305922; cv=none; d=zohomail.com; s=zohoarc; b=Qn5Od7AlPyk+1s/u2GcB5pKFBs4JDBeio+L0HaEFEY51ZARsbEtHQWNdTjQv2BNxVS5nkCHqh0mZXz6hTQ+4gFlU82PLyqz1PLpGhSkY9QMpd62ns3SDycqYX4MOb7vkaPhBedcqi98RcqdlwjNZT9oAToBDYa4a1n2UuCIFRek= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708305922; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=+hjmG1A2tPvmGc3N2NipyVPILE0g9ff37LS2A1O0hS0=; b=ORt6NMFzoL5J1PxhJq+JWN4K7TLIcPDVqs8i0OJvmvrAoKYe/gWicI6MDA7bkF/zINn5aY/l9CpGQqS7Sjgmu3AhU4SVXNxFZ1PjIIaBydc2jInR/3UG+SLMaDo/Zhg0uqPmQ2GMwnRpLaEHYTkGBL+7K5xygWQriQ6nafdsw6c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1708305922116778.9588961356359; Sun, 18 Feb 2024 17:25:22 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rbsJZ-0005Qz-I6; Sun, 18 Feb 2024 20:19:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rbsIe-0003YC-Kg; Sun, 18 Feb 2024 20:18:29 -0500 Received: from mail-yw1-x1135.google.com ([2607:f8b0:4864:20::1135]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rbsIW-0004JW-PN; Sun, 18 Feb 2024 20:18:23 -0500 Received: by mail-yw1-x1135.google.com with SMTP id 00721157ae682-6080f44d128so11745967b3.2; Sun, 18 Feb 2024 17:18:17 -0800 (PST) Received: from localhost.localdomain ([201.203.117.224]) by smtp.gmail.com with ESMTPSA id m12-20020a819e0c000000b00607ff905ed3sm1204704ywj.58.2024.02.18.17.18.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 17:18:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708305496; x=1708910296; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+hjmG1A2tPvmGc3N2NipyVPILE0g9ff37LS2A1O0hS0=; b=AXwD1TD0rtl5V3Vl8smKCSC822FpuGgmSsfgD26tn/brgvQ6kjwIkCm8pA1eb/xfS3 ac/JChV7yy3j7HmPmnB0ZtCo1RMh5UzRNBymfGBTfjlTndAyZenr6R/hSVSdTmy/eYmQ tQ3R+wUyMlO96QEK4aZYMkeJtaRlS5U3dxzIGZiUH87RdD3bWH17WeAc+MLvqmTYbSJG UHlren1SnYc7eFYdL4Vw7BCEPd0mRFUIFKufJ5lGx+13GclX9fcCYyYakrmmldMdtHWv kcBrlfHH8QH85fILQBndKGvk5JmAi3Oc7qyV1KZ60JR10W6UKxOqSXvYrsQQZKW+S43V 9Lmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708305496; x=1708910296; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+hjmG1A2tPvmGc3N2NipyVPILE0g9ff37LS2A1O0hS0=; b=ibkyElysZq5EAVAZ91qqh9QkM+NKG5zmzm5VyCU0+Y3fiHtuwbRl9rzYvPuobTIsfM 47epFYfLTFMkTjx5Z4mZKxV1KAeHy7JhdzEKXSprwi56enAMSkUpy4WIZTjbmkGpqy3l VNaMZ/2P4XcefiWz8Hz/BiZOCG/qO3XHcrB84TUfZZjB+mf8OUja3IxEmn2FB/6fVBLW RqIfkzaylRbFSFSroSl9G9zHU/+Uvewg8Bvl3/cGnaz8ewMkVV2RmRuliufqoNoqo+2Z rUEc3E5D7oCIx1Ok8mCWcGrdt5BPTdv8AhtZV12ednPYjc6RU6KEBYuTyX7+RL4Ff+BJ 5dZQ== X-Gm-Message-State: AOJu0Yy8W5eOfeBuEbQQWSIelqbWYTOfrYkwFUfIdiWlBSnU6VpBR5Vu EcLvMchfLpiaWR0rlOlBj2hw/z0Wit7zB/DyAPY/H+DG6ESj4uK7Tt2QRpmaI3E= X-Google-Smtp-Source: AGHT+IGCmCoQ9lpjPwdwTKF06K9MVmTYJAuq9/DznwcmbNzs0Uk+8OsnyTG5Bbh20fvi5BVgJ55Vqw== X-Received: by 2002:a05:690c:93:b0:607:d5f3:f02 with SMTP id be19-20020a05690c009300b00607d5f30f02mr14564314ywb.18.1708305496653; Sun, 18 Feb 2024 17:18:16 -0800 (PST) From: Sergey Kambalin X-Google-Original-From: Sergey Kambalin To: qemu-arm@nongnu.org Cc: qemu-devel@nongnu.org, Sergey Kambalin Subject: [PATCH v5 27/41] Implement GENET TX path Date: Sun, 18 Feb 2024 19:17:25 -0600 Message-Id: <20240219011739.2316619-28-sergey.kambalin@auriga.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240219011739.2316619-1-sergey.kambalin@auriga.com> References: <20240219011739.2316619-1-sergey.kambalin@auriga.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1135; envelope-from=serg.oker@gmail.com; helo=mail-yw1-x1135.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1708305923697100005 Content-Type: text/plain; charset="utf-8" Signed-off-by: Sergey Kambalin --- hw/net/bcm2838_genet.c | 221 ++++++++++++++++++++++++++++++++- include/hw/net/bcm2838_genet.h | 17 +++ 2 files changed, 237 insertions(+), 1 deletion(-) diff --git a/hw/net/bcm2838_genet.c b/hw/net/bcm2838_genet.c index 1fae3ecbc2..4c9b39a3ca 100644 --- a/hw/net/bcm2838_genet.c +++ b/hw/net/bcm2838_genet.c @@ -234,6 +234,13 @@ REG16(GENET_PHY_EXP_SEL, 0) FIELD(GENET_PHY_EXP_SEL, REG_ID, 0, 8) FIELD(GENET_PHY_EXP_SEL, BLOCK_ID, 8, 8) =20 +REG32(GENET_TX_CSUM_INFO, 0) +FIELD(GENET_TX_CSUM_INFO, OFFSET, 0, 15) +FIELD(GENET_TX_CSUM_INFO, PROTO_UDP, 15, 1) +FIELD(GENET_TX_CSUM_INFO, START, 16, 15) +FIELD(GENET_TX_CSUM_INFO, LV, 30, 1) + + static void bcm2838_genet_set_qemu_mac(BCM2838GenetState *s) { const MACAddr *addr =3D &s->nic_conf.macaddr; @@ -387,6 +394,218 @@ static uint64_t bcm2838_genet_mdio_cmd(BCM2838GenetSt= ate *s, uint64_t cmd) return cmd; } =20 +static void bcm2838_genet_xmit_packet(NetClientState *s, void *packet, + size_t size) +{ + uint8_t *buf =3D packet + sizeof(BCM2838GenetXmitStatus); + size_t len =3D size; + uint16_t len_type =3D 0; + + len -=3D sizeof(BCM2838GenetXmitStatus); + net_checksum_calculate(buf, len, CSUM_ALL); + + memcpy(&len_type, &buf[12], sizeof(len_type)); + len_type =3D ntohs(len_type); + if (len_type < MAX_PAYLOAD_SIZE) { + len_type =3D len; + len_type =3D 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_i= ndex, + uint32_t prod_index, + uint32_t cons_index) +{ + const unsigned int DESC_SIZE_WORDS + =3D sizeof(BCM2838GenetTdmaDesc) / sizeof(uint32_t); + const uint64_t RING_START_ADDR + =3D ((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 + =3D ((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; + uint32_t desc_status =3D 0; + uint32_t buflength =3D 0; + uint64_t num_descs =3D 0; + uint64_t read_ptr + =3D ((uint64_t)s->regs.tdma.rings[ring_index].read_ptr_hi << 32) + + s->regs.tdma.rings[ring_index].read_ptr; + off_t packet_off =3D 0; + + uint32_t prod_index_fld =3D FIELD_EX32(prod_index, + GENET_DMA_PROD_INDEX, INDEX); + uint32_t cons_index_fld =3D FIELD_EX32(cons_index, + GENET_DMA_CONS_INDEX, INDEX); + + while (cons_index_fld !=3D prod_index_fld) { + desc_index =3D read_ptr / DESC_SIZE_WORDS; + if (desc_index >=3D 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 =3D s->regs.tdma.descs[desc_index].length_status; + data_addr =3D ((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, + data_addr); + + if (FIELD_EX32(desc_status, GENET_RDMA_LENGTH_STATUS, SOP) !=3D 0)= { + packet_off =3D 0; + } + + buflength =3D FIELD_EX32(desc_status, + GENET_RDMA_LENGTH_STATUS, BUFLENGTH); + + /* TODO: Add address_space_read() return value check */ + address_space_read(&s->dma_as, data_addr, + MEMTXATTRS_UNSPECIFIED, + s->tx_packet + packet_off, + buflength); + packet_off +=3D buflength; + + if (FIELD_EX32(desc_status, GENET_RDMA_LENGTH_STATUS, EOP) !=3D 0)= { + bcm2838_genet_xmit_packet(qemu_get_queue(s->nic), s->tx_packet, + packet_off); + packet_off =3D 0; + } + + num_descs++; + cons_index_fld++; + s->regs.tdma.descs[desc_index].length_status =3D + FIELD_DP32(s->regs.tdma.descs[desc_index].length_status, + GENET_RDMA_LENGTH_STATUS, OWN, 1); + read_ptr =3D read_ptr =3D=3D RING_END_ADDR + 1 - DESC_SIZE_WORDS + ? RING_START_ADDR : read_ptr + DESC_SIZE_WORDS; + } + + s->regs.tdma.rings[ring_index].read_ptr =3D read_ptr; + s->regs.tdma.rings[ring_index].read_ptr_hi =3D read_ptr >> 32; + + return num_descs; +} + +static bool bcm2838_genet_tdma_ring_active(BCM2838GenetState *s, + unsigned int ring_index) +{ + uint32_t ctrl_reg =3D s->regs.tdma.ctrl; + uint32_t ring_cfg_reg =3D s->regs.tdma.ring_cfg; + uint32_t ring_mask =3D 1 << ring_index; + bool dma_en =3D FIELD_EX32(ctrl_reg, GENET_DMA_CTRL, EN) !=3D 0; + bool ring_en =3D + (FIELD_EX32(ring_cfg_reg, GENET_DMA_CTRL, EN) & ring_mask) !=3D 0; + bool ring_buf_en =3D + (FIELD_EX32(ctrl_reg, GENET_DMA_CTRL, RING_BUF_EN) & ring_mask) != =3D 0; + bool active =3D 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; + uint32_t tx_intrs; + uint32_t cons_index; + uint32_t prod_index =3D value; + uint32_t ring_cfg =3D value; + uint32_t dma_ctrl =3D value; + + uint32_t cons_index_fld; + uint32_t prod_index_fld =3D + FIELD_EX32(prod_index, GENET_DMA_PROD_INDEX, INDEX); + + uint32_t exst_tdma_en =3D + FIELD_EX32(s->regs.tdma.ctrl, GENET_DMA_CTRL, EN); + uint32_t exst_ring_en =3D + FIELD_EX32(s->regs.tdma.ring_cfg, GENET_DMA_RING_CFG, EN); + uint32_t incm_tdma_en =3D + FIELD_EX32(dma_ctrl, GENET_DMA_CTRL, EN); + uint32_t incm_ring_en =3D + FIELD_EX32(ring_cfg, GENET_DMA_RING_CFG, EN); + uint32_t incm_ring_buf_en =3D + FIELD_EX32(dma_ctrl, GENET_DMA_CTRL, RING_BUF_EN); + + switch (offset) { + case BCM2838_GENET_TDMA_RINGS + ... BCM2838_GENET_TDMA_RINGS + sizeof(s->regs.tdma.rings) - 1: + ring_index =3D (offset - BCM2838_GENET_TDMA_RINGS) + / sizeof(BCM2838GenetTdmaRing); + if (bcm2838_genet_tdma_ring_active(s, ring_index)) { + ring_offset =3D offset - BCM2838_GENET_TDMA_RINGS + - ring_index * sizeof(BCM2838GenetTdmaRing); + switch (ring_offset) { + case BCM2838_GENET_TRING_PROD_INDEX: + cons_index =3D s->regs.tdma.rings[ring_index].cons_index; + cons_index_fld =3D FIELD_EX32(cons_index, + GENET_DMA_CONS_INDEX, INDEX); + if (cons_index_fld !=3D prod_index_fld) { + trace_bcm2838_genet_tx_request(ring_index, + prod_index_fld, + cons_index_fld); + num_descs_tx =3D bcm2838_genet_tx(s, ring_index, prod_= index, + cons_index); + if (num_descs_tx > 0) { + s->regs.tdma.rings[ring_index].cons_index =3D + FIELD_DP32(s->regs.tdma.rings[ring_index].cons= _index, + GENET_DMA_CONS_INDEX, INDEX, + cons_index + num_descs_tx); + + if (ring_index =3D=3D BCM2838_GENET_DMA_RING_DEFAU= LT) { + s->regs.intrl0.stat =3D + FIELD_DP32(s->regs.intrl0.stat, GENET_INTR= L_0, + TXDMA_MBDONE, 1); + } else { + tx_intrs =3D FIELD_EX32(s->regs.intrl1.stat, + GENET_INTRL_1, TX_INTRS= ); + s->regs.intrl1.stat =3D + FIELD_DP32(s->regs.intrl1.stat, + GENET_INTRL_1, TX_INTRS, + tx_intrs | 1 << ring_index); + } + } + } + break; + default: + break; + } + } + break; + case BCM2838_GENET_TDMA_RING_CFG: + if (exst_ring_en !=3D incm_ring_en) { + trace_bcm2838_genet_tx_dma_ring(incm_ring_en); + } + break; + case BCM2838_GENET_TDMA_CTRL: + if (exst_tdma_en !=3D incm_tdma_en) { + s->regs.tdma.status =3D FIELD_DP32(s->regs.tdma.status, + GENET_DMA_STATUS, + DISABLED, !exst_tdma_en); + trace_bcm2838_genet_tx_dma(incm_tdma_en =3D=3D 1 + ? "enabled" + : "disabled"); + } + if (exst_ring_en !=3D incm_ring_buf_en) { + trace_bcm2838_genet_tx_dma_ring_buf(incm_ring_buf_en); + } + break; + default: + break; + } +} + static uint64_t bcm2838_genet_read(void *opaque, hwaddr offset, unsigned s= ize) { uint64_t value =3D ~0; @@ -477,7 +696,7 @@ static void bcm2838_genet_write(void *opaque, hwaddr of= fset, 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 7a483bd265..f96ea3a145 100644 --- a/include/hw/net/bcm2838_genet.h +++ b/include/hw/net/bcm2838_genet.h @@ -108,6 +108,21 @@ OBJECT_DECLARE_SIMPLE_TYPE(BCM2838GenetState, BCM2838_= GENET) #define BCM2838_GENET_PHY_EXP_SHD_BLOCKS_CNT 256 #define BCM2838_GENET_PHY_EXP_SHD_REGS_CNT 256 =20 +#define MAX_FRAME_SIZE 0xFFF +#define MAX_PACKET_SIZE 1518 +#define MAX_PAYLOAD_SIZE 1500 +#define TX_MIN_PKT_SIZE 60 + + +typedef struct 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 */ + uint32_t tx_csum_info; /* Tx checksum info. */ + uint32_t unused2[3]; /* unused */ +} BCM2838GenetXmitStatus; + typedef struct { uint32_t rev_ctrl; uint32_t port_ctrl; @@ -403,6 +418,8 @@ struct BCM2838GenetState { =20 qemu_irq irq_default; qemu_irq irq_prio; + + uint8_t tx_packet[MAX_FRAME_SIZE]; }; =20 #endif /* BCM2838_GENET_H */ --=20 2.34.1