From nobody Wed Nov 27 02:19:51 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=1702003206; cv=none; d=zohomail.com; s=zohoarc; b=ARcicG3+MvphRIFUF/ZuPGTBbRzwmvHYLeqTc1S20JgFlqliaygidjXiz3tuTAEId8xa3pzfIPHBXLRkBWY5yvL66eULPjJNo3gkL54SG9Jd85ZCQnQahzAfhWxbI4SBaxgnFwlJtFZBhvxh2YSE67aj2VbUwBckl9fvGC7bTC8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1702003206; 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=B9Nhv49pbU7bZ8mzot2h9NoKamHuW2o0gQPpPceL6vk=; b=c4SRbY78VJesznEERVgDHC0ofQTGqTI8BqlFMUlxTCOvz/PrFwfNsPwyHeIDYllS7X+YwZv1CJqMaut/gH9ce+QEolg7cufX5oXbfXA0YtfMhvrxruOripui58KYXc/2d990wVt5HU8hJwKArRHhjYaIm/b+U56SMcl74jWQQhs= 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 1702003206747968.0068751670175; Thu, 7 Dec 2023 18:40:06 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rBQga-0006oU-Es; Thu, 07 Dec 2023 21:33:52 -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 1rBQgX-0006YF-QX; Thu, 07 Dec 2023 21:33:49 -0500 Received: from mail-lj1-x230.google.com ([2a00:1450:4864:20::230]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rBQgU-0002ea-9K; Thu, 07 Dec 2023 21:33:49 -0500 Received: by mail-lj1-x230.google.com with SMTP id 38308e7fff4ca-2ca02def690so18916721fa.3; Thu, 07 Dec 2023 18:33:45 -0800 (PST) Received: from localhost.localdomain ([185.200.240.39]) by smtp.gmail.com with ESMTPSA id r1-20020a2e94c1000000b002c9e6cbf78esm99062ljh.19.2023.12.07.18.33.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 18:33:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702002823; x=1702607623; 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=B9Nhv49pbU7bZ8mzot2h9NoKamHuW2o0gQPpPceL6vk=; b=Zhx1/D6JS9LMmH51RNbXVVJY937U3oBLH0ODWCImYUMrtr2TmQk7N3asCNOg+lD/Hl iEzQz/8HmOYiPCnsMg828mDOqhS7baSCiwCsRNeqJrCGZJF6C4gjABwA4qfnmG7RvAaI VocCpnxBN1ZKiy9STI2jF71SEokFatBRvyem/YGCnIHage6UlFJzKAb8b7gM3s3/LC9a DF9L0002z+gasIiNgmx2Bt4+bteHVoUVrNAAuzvhYigJYdpR5lJZAeStYf7f1+0o/W/c Yex2kGSrVtjDbUeRblgnKiYoZAthH1/R8Eot4OH1rzKMZ8RqKWEHs0SXgROtIGxkH5/k qAcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702002823; x=1702607623; 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=B9Nhv49pbU7bZ8mzot2h9NoKamHuW2o0gQPpPceL6vk=; b=D0DYpzCIgfVl5asvVTDizv2r06YGGxlvsUdSO4T6mRiT7T1GXr8uo+CWm8IcJzd+qB Fq8WEGvviYFSHIco7re7wqNFuJnt8E1TraCbFro5ZXgreoCw0eAlGFQPMumyb98N7Uj5 xuYQiYuba5qbo5tPO/z+b7sukIybJ2P1V3VPBU0lFcAwwXbi1hQq0DCutfpbBAcB73LJ reFXWPineUcNyq7GIUCndcDN28LOYb2kKgUW+5gD1UfOCbAk52Ddf4ZUimUHSQ/MYWUi +oSAI+JRpFBGGdqhjnb0pDLnZ2UItGnrfUAe9ifSkTs2Iq8kTE/nYAosEQYubjXYpMnG e/Nw== X-Gm-Message-State: AOJu0YwhOm8ncrdIf/HIQZ1Q8YqvYQCERr+FAO4owqig4/zw8in5G9HR 3TN41hpKx5hFOiZiAQm1SvPa5Md6fddjwg== X-Google-Smtp-Source: AGHT+IGQR0FYjgmY+GV3jmrmjjo8AkEFNkUpaGELIXMR/WRikIJ6VDTtuI5ma+DlfHihPeKix8JLnA== X-Received: by 2002:a2e:9cd5:0:b0:2c9:f727:7e65 with SMTP id g21-20020a2e9cd5000000b002c9f7277e65mr2077161ljj.38.1702002823401; Thu, 07 Dec 2023 18:33:43 -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 v4 31/45] Implement GENET RX path Date: Thu, 7 Dec 2023 20:31:31 -0600 Message-Id: <20231208023145.1385775-32-sergey.kambalin@auriga.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231208023145.1385775-1-sergey.kambalin@auriga.com> References: <20231208023145.1385775-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=2a00:1450:4864:20::230; envelope-from=serg.oker@gmail.com; helo=mail-lj1-x230.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: 1702003207190100001 Content-Type: text/plain; charset="utf-8" Signed-off-by: Sergey Kambalin --- hw/net/bcm2838_genet.c | 265 ++++++++++++++++++++++++++++++++- include/hw/net/bcm2838_genet.h | 1 + 2 files changed, 265 insertions(+), 1 deletion(-) diff --git a/hw/net/bcm2838_genet.c b/hw/net/bcm2838_genet.c index 4c9b39a3ca..61c1981e10 100644 --- a/hw/net/bcm2838_genet.c +++ b/hw/net/bcm2838_genet.c @@ -511,6 +511,25 @@ static bool bcm2838_genet_tdma_ring_active(BCM2838Gene= tState *s, return active; } =20 +static bool bcm2838_genet_rdma_ring_active(BCM2838GenetState *s, + unsigned int ring_index) +{ + uint32_t ring_mask =3D 1 << ring_index; + + bool dma_en =3D FIELD_EX32(s->regs.rdma.ctrl, GENET_DMA_CTRL, EN) !=3D= 0; + bool ring_en =3D (FIELD_EX32(s->regs.rdma.ring_cfg, GENET_DMA_RING_CFG= , EN) + & ring_mask) !=3D 0; + bool ring_buf_en =3D (FIELD_EX32(s->regs.rdma.ctrl, + GENET_DMA_CTRL, RING_BUF_EN) + & ring_mask) !=3D 0; + bool active =3D dma_en && ring_en && ring_buf_en; + + trace_bcm2838_genet_rx_dma_ring_active(ring_index, + active ? "active" : "halted"); + + return active; +} + static void bcm2838_genet_tdma(BCM2838GenetState *s, hwaddr offset, uint64_t value) { @@ -721,9 +740,251 @@ static const MemoryRegionOps bcm2838_genet_ops =3D { .valid =3D {.min_access_size =3D 4}, }; =20 +static int32_t bcm2838_genet_filter(BCM2838GenetState *s, const void *buf, + size_t size) +{ + qemu_log_mask(LOG_UNIMP, + "Packet filtration with HFB isn't implemented yet"); + return -1; +} + +static int32_t bcm2838_genet_filter2ring(BCM2838GenetState *s, + uint32_t filter_idx) +{ + qemu_log_mask(LOG_UNIMP, + "Packet filtration with HFB isn't implemented yet"); + return -1; +} + +static bool is_packet_broadcast(const uint8_t *buf, size_t size) +{ + static const uint8_t bcst_addr[] =3D {0xff, 0xff, 0xff, 0xff, 0xff, 0x= ff}; + + if (size < sizeof(bcst_addr)) { + return false; + } + + return !memcmp(buf, bcst_addr, sizeof(bcst_addr)); +} + +static bool is_packet_multicast(const uint8_t *buf, size_t size) +{ + return !!(buf[0] & 0x01); +} + +static ssize_t bcm2838_genet_rdma(BCM2838GenetState *s, uint32_t ring_idx, + const void *buf, size_t size) +{ + const size_t DESC_WORD_SIZE =3D + sizeof(BCM2838GenetRdmaDesc) / sizeof(uint32_t); + + ssize_t len =3D 0; + BCM2838GenetRegsRdma *rdma =3D &s->regs.rdma; + BCM2838GenetRdmaRing *ring =3D &rdma->rings[ring_idx]; + hwaddr write_index =3D + (ring->write_ptr + ((hwaddr)ring->write_ptr_hi << 32)) / DESC_WORD= _SIZE; + BCM2838GenetRdmaDesc *desc =3D &rdma->descs[write_index]; + + const hwaddr START_INDEX =3D + (ring->start_addr + ((hwaddr)ring->start_addr_hi << 32)) + / DESC_WORD_SIZE; + const hwaddr END_INDEX =3D + (ring->end_addr + ((hwaddr)ring->end_addr_hi << 32)) / DESC_WORD_S= IZE; + + if (!bcm2838_genet_rdma_ring_active(s, ring_idx)) { + return -1; + } + + desc->length_status =3D FIELD_DP32(desc->length_status, + GENET_RDMA_LENGTH_STATUS, SOP, 1); + + while (len < size) { + size_t l =3D size - len; + size_t buf_size =3D ring->ring_buf_size & 0xffff; + uint8_t *dma_buf =3D s->rx_packet; + hwaddr dma_buf_addr =3D + desc->address_lo + ((hwaddr)desc->address_hi << 32); + MemTxResult mem_tx_result =3D MEMTX_OK; + uint8_t *frame_buf =3D dma_buf + sizeof(BCM2838GenetXmitStatus) + = 2; + BCM2838GenetXmitStatus *xmit_status =3D (BCM2838GenetXmitStatus *)= dma_buf; + struct iovec iov; + bool isip4, isip6; + size_t l3hdr_off, l4hdr_off, l5hdr_off; + eth_ip6_hdr_info ip6hdr_info; + eth_ip4_hdr_info ip4hdr_info; + eth_l4_hdr_info l4hdr_info; + + bool crc_fwd =3D FIELD_EX32(s->regs.umac.cmd, GENET_UMAC_CMD, CRC_= FWD); + size_t buflength; + uint32_t prod_index; + + if (l > ring->ring_buf_size) { + l =3D ring->ring_buf_size; + } + + memcpy(frame_buf, buf + len, l); + iov.iov_base =3D frame_buf; + iov.iov_len =3D l; + eth_get_protocols(&iov, 1, 0, + &isip4, &isip6, + &l3hdr_off, &l4hdr_off, &l5hdr_off, + &ip6hdr_info, &ip4hdr_info, &l4hdr_info); + + len +=3D l; + + desc->length_status =3D FIELD_DP32(desc->length_status, + GENET_RDMA_LENGTH_STATUS, + EOP, !!(len >=3D size)); + + buflength =3D l + sizeof(BCM2838GenetXmitStatus) + 2; + if (crc_fwd) { + buflength +=3D 4; + } + + desc->length_status =3D FIELD_DP32(desc->length_status, + GENET_RDMA_LENGTH_STATUS, + BUFLENGTH, buflength); + + desc->length_status =3D FIELD_DP32(desc->length_status, + GENET_RDMA_LENGTH_STATUS, + BROADCAST, + !!is_packet_broadcast(frame_buf, = l)); + desc->length_status =3D FIELD_DP32(desc->length_status, + GENET_RDMA_LENGTH_STATUS, + MULTICAST, + !!is_packet_multicast(frame_buf, = l)); + + xmit_status->rx_csum =3D 0; + if (isip4) { + xmit_status->rx_csum =3D ip4hdr_info.ip4_hdr.ip_sum; + } + xmit_status->length_status =3D desc->length_status; + + mem_tx_result =3D address_space_write(&s->dma_as, dma_buf_addr, + MEMTXATTRS_UNSPECIFIED, + dma_buf, buf_size); + if (mem_tx_result !=3D MEMTX_OK) { + desc->length_status =3D FIELD_DP32(desc->length_status, + GENET_RDMA_LENGTH_STATUS, + RXERR, 1); + } + + if (FIELD_EX32(desc->length_status, + GENET_RDMA_LENGTH_STATUS, RXERR) !=3D 0) { + break; + } + + prod_index =3D FIELD_EX32(ring->prod_index, GENET_DMA_PROD_INDEX, = INDEX); + ring->prod_index =3D FIELD_DP32(ring->prod_index, + GENET_DMA_PROD_INDEX, + INDEX, ++prod_index); + if (++write_index > END_INDEX) { + write_index =3D START_INDEX; + } + desc =3D &rdma->descs[write_index]; + ring->write_ptr =3D write_index * DESC_WORD_SIZE; + ring->write_ptr_hi =3D ((hwaddr)write_index * DESC_WORD_SIZE) >> 3= 2; + } + + if (ring_idx =3D=3D BCM2838_GENET_DMA_RING_DEFAULT) { + s->regs.intrl0.stat =3D FIELD_DP32(s->regs.intrl0.stat, + GENET_INTRL_0, RXDMA_MBDONE, 1); + } else { + uint32_t rx_intrs =3D + FIELD_EX32(s->regs.intrl1.stat, GENET_INTRL_1, RX_INTRS); + rx_intrs |=3D 1 << ring_idx; + + s->regs.intrl1.stat =3D FIELD_DP32(s->regs.intrl1.stat, + GENET_INTRL_1, RX_INTRS, rx_intrs= ); + } + + return len; +} + +static ssize_t bcm2838_genet_receive(NetClientState *nc, const uint8_t *bu= f, + size_t size) +{ + BCM2838GenetState *s =3D (BCM2838GenetState *)qemu_get_nic_opaque(nc); + ssize_t bytes_received =3D -1; + int32_t filter_index =3D -1; + int32_t ring_index =3D -1; + + if (FIELD_EX32(s->regs.rdma.ctrl, GENET_DMA_CTRL, EN) !=3D 0) { + filter_index =3D bcm2838_genet_filter(s, buf, size); + + if (filter_index >=3D 0) { + ring_index =3D bcm2838_genet_filter2ring(s, filter_index); + } else { + ring_index =3D BCM2838_GENET_DMA_RING_CNT - 1; + } + + if (size <=3D MAX_PACKET_SIZE) { + bytes_received =3D bcm2838_genet_rdma(s, ring_index, buf, size= ); + } + } + + bcm2838_genet_set_irq_default(s); + bcm2838_genet_set_irq_prio(s); + + return bytes_received; +} + +static void bcm2838_genet_phy_update_link(BCM2838GenetState *s) +{ + bool qemu_link_down =3D qemu_get_queue(s->nic)->link_down !=3D 0; + + bool lstatus =3D FIELD_EX32(s->phy_regs.bmsr, GENET_PHY_BMSR, LSTATUS)= !=3D 0; + + if (qemu_link_down && lstatus) { + trace_bcm2838_genet_phy_update_link("down"); + + s->phy_regs.bmsr =3D FIELD_DP32(s->phy_regs.bmsr, + GENET_PHY_BMSR, ANEGCOMPLETE, 0); + s->phy_regs.bmsr =3D FIELD_DP32(s->phy_regs.bmsr, + GENET_PHY_BMSR, LSTATUS, 0); + s->regs.intrl0.stat =3D FIELD_DP32(s->regs.intrl0.stat, + GENET_INTRL_0, LINK_DOWN, 1); + } else if (!qemu_link_down && !lstatus) { + trace_bcm2838_genet_phy_update_link("up"); + + /* + * Complete auto-negotiation (fixed link partner's abilities for n= ow: + * 1Gbps with flow control) + */ + s->phy_regs.stat1000 =3D FIELD_DP32(s->phy_regs.stat1000, + GENET_PHY_STAT_1000, HALF, 1); + s->phy_regs.stat1000 =3D FIELD_DP32(s->phy_regs.stat1000, + GENET_PHY_STAT_1000, FULL, 1); + + s->phy_regs.lpa =3D FIELD_DP32(s->phy_regs.lpa, + GENET_PHY_LPA, PAUSE_CAP, 1); + s->phy_regs.lpa =3D FIELD_DP32(s->phy_regs.lpa, + GENET_PHY_LPA, PAUSE_ASYM, 1); + s->phy_regs.lpa =3D FIELD_DP32(s->phy_regs.lpa, GENET_PHY_LPA, LPA= CK, 1); + + s->phy_regs.bmsr =3D FIELD_DP32(s->phy_regs.bmsr, + GENET_PHY_BMSR, ANEGCOMPLETE, 1); + s->phy_regs.bmsr =3D FIELD_DP32(s->phy_regs.bmsr, + GENET_PHY_BMSR, LSTATUS, 1); + + s->regs.intrl0.stat =3D FIELD_DP32(s->regs.intrl0.stat, + GENET_INTRL_0, LINK_UP, 1); + } + + bcm2838_genet_set_irq_default(s); +} +static void bcm2838_genet_set_link(NetClientState *nc) +{ + BCM2838GenetState *s =3D qemu_get_nic_opaque(nc); + + bcm2838_genet_phy_update_link(s); +} + static NetClientInfo bcm2838_genet_client_info =3D { .type =3D NET_CLIENT_DRIVER_NIC, - .size =3D sizeof(NICState) + .size =3D sizeof(NICState), + .receive =3D bcm2838_genet_receive, + .link_status_changed =3D bcm2838_genet_set_link, }; =20 static void bcm2838_genet_realize(DeviceState *dev, Error **errp) @@ -777,6 +1038,8 @@ static void bcm2838_genet_phy_reset(BCM2838GenetState = *s) s->phy_aux_ctl_shd_regs.misc =3D 0x1E; =20 trace_bcm2838_genet_phy_reset("done"); + + bcm2838_genet_phy_update_link(s); } =20 static void bcm2838_genet_reset(DeviceState *d) diff --git a/include/hw/net/bcm2838_genet.h b/include/hw/net/bcm2838_genet.h index f96ea3a145..f044d0d17e 100644 --- a/include/hw/net/bcm2838_genet.h +++ b/include/hw/net/bcm2838_genet.h @@ -420,6 +420,7 @@ struct BCM2838GenetState { qemu_irq irq_prio; =20 uint8_t tx_packet[MAX_FRAME_SIZE]; + uint8_t rx_packet[MAX_FRAME_SIZE]; }; =20 #endif /* BCM2838_GENET_H */ --=20 2.34.1