From nobody Wed Apr 8 04:41:04 2026 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B37E93CC9F0 for ; Tue, 10 Mar 2026 21:22:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773177768; cv=none; b=NwRJZYnq8KSqcowFpuHa4bXLzpHDOEkpv2q9lu+9uvF77vDgcVW5TQ8DTKwXrENbpw7i3HgY8LkHuDgjbc7XlOpQkWlwHrb2njAsz3xg7c2x3lvGyZ51GuvXoKPba+Ydhq0ZuNRAekam3YBJji+pOIKNg0nePeho5wRMicX1H8o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773177768; c=relaxed/simple; bh=KPVuKItZd1MhwLbF0SvCW6nyq/QrT40WF9DJe9SLVVE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PeJn9EYOoYA/sr0aRaAtYSO7pVzFlimtn7TgpmLZ5R8wC7xoijio0OAgEyuNfYhBwccNY33Ot5zKMQbjWiOEVeY7EfrBah+vzf4KaGo73ilupOGXT90onjpLeFGgKKXvg50NrigOI50tiz12GW1SrI8rD70hhkqsTmDUYGve/XE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dama.to; spf=none smtp.mailfrom=dama.to; dkim=pass (2048-bit key) header.d=dama-to.20230601.gappssmtp.com header.i=@dama-to.20230601.gappssmtp.com header.b=a1aRl5RA; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dama.to Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=dama.to Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=dama-to.20230601.gappssmtp.com header.i=@dama-to.20230601.gappssmtp.com header.b="a1aRl5RA" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-3597fea200dso7386762a91.3 for ; Tue, 10 Mar 2026 14:22:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dama-to.20230601.gappssmtp.com; s=20230601; t=1773177766; x=1773782566; darn=vger.kernel.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=im3H2jdH9CTQLzHqJHTpkhsVYCfPOcqP+VrdKZHbyAg=; b=a1aRl5RAfE8J5vxCEc+8wuXY29QYEcNJvSAXsaY5+ZR1AlWD1aUFlkpUzMX53eNpSt zuO42pGwkzzBi1iHXq+MGEYfMUc1867JImm/ZFy6HU+W+2OdqrAnbpf9Tq5qJEZacrEC CvgMnEIg2CfEfbh7NtLR1ldYo5onZSBf3odvEsPK3fhXQbLuJ6/Tjtqk+d/EPb6RfEQi Nz3DhYCDPfk1GmIXmj18y6kJXBQ9CkDOck+cEyVpN26epXMe9KJOvx0KNRDEBXi4ZSMU nOsLAXtYv7U1UyoSCi61ZPeCBwETx1AoSzC+gtt9cd7Exn40OFny4HPVr2PFh/So59oB 7zSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773177766; x=1773782566; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=im3H2jdH9CTQLzHqJHTpkhsVYCfPOcqP+VrdKZHbyAg=; b=Xxwyru5QtleSmhvZJ9Df8yWwZ94oaod7cK9GEY7ndk/Zmu3stKZ/3Iw9rSVLHxpOwH Clx8SzE5WzHvRPD4MEUZYPYfEU/+OaAUEvNbtHMLGXApr+f0TGYyTyM1XPBArYaREKvw a2abkqwPK/4Mpzq4hupcT+rGui6tvKdX7eeSP7Igic06SPij7tF1ZU/RiNo6JYnSGO/g PqZ0Xzz+uQI7PBctY85ICd8EVXayQwSmrRTWxzlJSiRrU1HkIlzHbFacU73oDXQVYF80 LmO4rOLtI18H9Db+V7g9sCCdVcOPzoLO3NwnakPWAm/wvh6vI/SxBEp2cqDXblXMnqaC Vs8g== X-Forwarded-Encrypted: i=1; AJvYcCVBAEYHh2PZvl+Gf6yMSBvGetL+rzG8X4iYUOYlitnnlA3kPAALpaF704B6tBjIEaXVd0OwtP8dMJjMwpo=@vger.kernel.org X-Gm-Message-State: AOJu0YxTxmy37910F53fOaAlhphU0UDQ/y4Io+BW0saZbGwS+7bEs1RY Abp+r0Ev/nVVsAxfpqcbdzE2iC0L5NAnv6cB+R/ApWr/2JrTjmMiUum6W6ks5DA2ELw= X-Gm-Gg: ATEYQzzKFQM2wIpOMkNOm0Wh/fGd1pWf/PuWU+9ydiCOlnLgVAQlYzQK9MQZlk9FI3B 2YxJgzu+KLlgUY0S9xOh6yqOX/AF3bTWkMhiL5h+B6Y26aIFZeGXX+QkrqAt6bnpdoK4gbReapy ovYRqg6FjVzD8I9mdhn2Y9AxPv91zuqS2NaAQDHXX/oriPOm1wNxtaz6jF8k/BzNof552xZ2mKv 9ugOsNZm7VBCBDXZFgxN6b4ICo8b5xBiFL7nc3/iXIXgly7UgiiFfXpBdGJO+BXYo6kSG75uWi3 sVee33Zeui08UCllra0O6xynOF+1HEATvSMmWaNYNrf8PNYoQs+TFp458LpQcKtG0GbZiS3URd4 wZXnkWgqR6Nm+K/PJO7h25mG4DrMFGp77XI7VEHxzqdut45rFDbxTA2q01OKFatOtoZI53ldDcR TBtb8= X-Received: by 2002:a17:90b:380a:b0:359:fd9a:c513 with SMTP id 98e67ed59e1d1-35a012c383emr247308a91.24.1773177765930; Tue, 10 Mar 2026 14:22:45 -0700 (PDT) Received: from localhost ([2a03:2880:2ff:6::]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-35a02474a97sm37744a91.11.2026.03.10.14.22.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Mar 2026 14:22:45 -0700 (PDT) From: Joe Damato To: netdev@vger.kernel.org, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman Cc: michael.chan@broadcom.com, pavan.chebbi@broadcom.com, linux-kernel@vger.kernel.org, Joe Damato Subject: [RFC net-next 02/10] net: tso: Add tso_dma_map helpers Date: Tue, 10 Mar 2026 14:21:50 -0700 Message-ID: <20260310212209.2263939-3-joe@dama.to> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260310212209.2263939-1-joe@dama.to> References: <20260310212209.2263939-1-joe@dama.to> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add helpers to initialize, iterate, and clean up a tso_dma_map: tso_dma_map_init(): DMA-maps the linear payload region and all frags upfront into the tso_dma_map struct. Returns 0 on success, cleans up partial mappings on failure. tso_dma_map_cleanup(): unmaps all DMA regions. Used on error paths. tso_dma_map_count(): counts how many descriptors the next N bytes of payload will need, without advancing the iterator. tso_dma_map_next(): yields the next (dma_addr, chunk_len) pair. Indicates when a chunk starts a new DMA mapping so the driver can set dma_unmap_len on that BD for completion-time unmapping. Suggested-by: Jakub Kicinski Signed-off-by: Joe Damato --- include/net/tso.h | 8 +++ net/core/tso.c | 165 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+) diff --git a/include/net/tso.h b/include/net/tso.h index 9a508e60ee19..dcb93c7fb917 100644 --- a/include/net/tso.h +++ b/include/net/tso.h @@ -65,4 +65,12 @@ struct tso_dma_map { } frags[MAX_SKB_FRAGS]; }; =20 +int tso_dma_map_init(struct tso_dma_map *map, struct device *dev, + const struct sk_buff *skb, unsigned int hdr_len); +void tso_dma_map_cleanup(struct tso_dma_map *map); +unsigned int tso_dma_map_count(const struct tso_dma_map *map, unsigned int= len); +bool tso_dma_map_next(struct tso_dma_map *map, dma_addr_t *addr, + unsigned int *chunk_len, unsigned int *mapping_len, + unsigned int seg_remaining); + #endif /* _TSO_H */ diff --git a/net/core/tso.c b/net/core/tso.c index 6df997b9076e..48348ad94501 100644 --- a/net/core/tso.c +++ b/net/core/tso.c @@ -3,6 +3,7 @@ #include #include #include +#include #include =20 void tso_build_hdr(const struct sk_buff *skb, char *hdr, struct tso_t *tso, @@ -87,3 +88,167 @@ int tso_start(struct sk_buff *skb, struct tso_t *tso) return hdr_len; } EXPORT_SYMBOL(tso_start); + +/** + * tso_dma_map_init - DMA-map GSO payload regions + * @map: map struct to initialize + * @dev: device for DMA mapping + * @skb: the GSO skb + * @hdr_len: per-segment header length in bytes + * + * DMA-maps the linear payload (after headers) and all frags. + * Positions the iterator at byte 0 of the payload. + * + * Returns 0 on success, -ENOMEM on DMA mapping failure (partial mappings + * are cleaned up internally). + */ +int tso_dma_map_init(struct tso_dma_map *map, struct device *dev, + const struct sk_buff *skb, unsigned int hdr_len) +{ + unsigned int linear_len =3D skb_headlen(skb) - hdr_len; + unsigned int nr_frags =3D skb_shinfo(skb)->nr_frags; + int i; + + map->dev =3D dev; + map->skb =3D skb; + map->hdr_len =3D hdr_len; + map->frag_idx =3D -1; + map->offset =3D 0; + map->linear_len =3D 0; + map->nr_frags =3D 0; + + if (linear_len > 0) { + map->linear_dma =3D dma_map_single(dev, skb->data + hdr_len, + linear_len, DMA_TO_DEVICE); + if (dma_mapping_error(dev, map->linear_dma)) + return -ENOMEM; + map->linear_len =3D linear_len; + } + + for (i =3D 0; i < nr_frags; i++) { + skb_frag_t *frag =3D &skb_shinfo(skb)->frags[i]; + + map->frags[i].len =3D skb_frag_size(frag); + map->frags[i].dma =3D skb_frag_dma_map(dev, frag, 0, + map->frags[i].len, + DMA_TO_DEVICE); + if (dma_mapping_error(dev, map->frags[i].dma)) { + tso_dma_map_cleanup(map); + return -ENOMEM; + } + map->nr_frags =3D i + 1; + } + + if (linear_len =3D=3D 0 && nr_frags > 0) + map->frag_idx =3D 0; + + return 0; +} +EXPORT_SYMBOL(tso_dma_map_init); + +/** + * tso_dma_map_cleanup - unmap all DMA regions in a tso_dma_map + * @map: the map to clean up + * + * Unmaps linear payload and all mapped frags. Used on error paths. + * Success paths use the driver's completion path to handle unmapping. + */ +void tso_dma_map_cleanup(struct tso_dma_map *map) +{ + int i; + + if (map->linear_len) + dma_unmap_single(map->dev, map->linear_dma, map->linear_len, + DMA_TO_DEVICE); + + for (i =3D 0; i < map->nr_frags; i++) + dma_unmap_page(map->dev, map->frags[i].dma, map->frags[i].len, + DMA_TO_DEVICE); + + map->linear_len =3D 0; + map->nr_frags =3D 0; +} +EXPORT_SYMBOL(tso_dma_map_cleanup); + +/** + * tso_dma_map_count - count descriptors for a payload range + * @map: the payload map + * @len: number of payload bytes in this segment + * + * Counts how many contiguous DMA region chunks the next @len bytes + * will span, without advancing the iterator. Uses frag sizes from + * the current position. + * + * Returns the number of descriptors needed for @len bytes of payload. + */ +unsigned int tso_dma_map_count(const struct tso_dma_map *map, unsigned int= len) +{ + unsigned int offset =3D map->offset; + int idx =3D map->frag_idx; + unsigned int count =3D 0; + + while (len > 0) { + unsigned int region_len, chunk; + + if (idx =3D=3D -1) + region_len =3D map->linear_len; + else + region_len =3D map->frags[idx].len; + + chunk =3D min(len, region_len - offset); + len -=3D chunk; + count++; + offset =3D 0; + idx++; + } + + return count; +} +EXPORT_SYMBOL(tso_dma_map_count); + +/** + * tso_dma_map_next - yield the next DMA address range + * @map: the payload map + * @addr: output DMA address + * @chunk_len: output chunk length + * @mapping_len: output full mapping length when this is the first BD of + * a DMA mapping (driver should set dma_unmap_len to this), + * or 0 when continuing a previous mapping + * @seg_remaining: bytes left in current segment + * + * Yields the next (dma_addr, chunk_len) pair and advances the iterator. + * + * Returns true if a chunk was yielded, false when @seg_remaining is 0. + */ +bool tso_dma_map_next(struct tso_dma_map *map, dma_addr_t *addr, + unsigned int *chunk_len, unsigned int *mapping_len, + unsigned int seg_remaining) +{ + unsigned int region_len, chunk; + + if (!seg_remaining) + return false; + + if (map->frag_idx =3D=3D -1) { + region_len =3D map->linear_len; + chunk =3D min(seg_remaining, region_len - map->offset); + *addr =3D map->linear_dma + map->offset; + *mapping_len =3D (map->offset =3D=3D 0) ? region_len : 0; + } else { + region_len =3D map->frags[map->frag_idx].len; + chunk =3D min(seg_remaining, region_len - map->offset); + *addr =3D map->frags[map->frag_idx].dma + map->offset; + *mapping_len =3D (map->offset =3D=3D 0) ? region_len : 0; + } + + *chunk_len =3D chunk; + map->offset +=3D chunk; + + if (map->offset >=3D region_len) { + map->frag_idx++; + map->offset =3D 0; + } + + return true; +} +EXPORT_SYMBOL(tso_dma_map_next); --=20 2.52.0