From patchwork Sat Sep 29 11:28:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 147836 Delivered-To: patch@linaro.org Received: by 2002:a2e:8595:0:0:0:0:0 with SMTP id b21-v6csp1815187lji; Sat, 29 Sep 2018 04:28:18 -0700 (PDT) X-Google-Smtp-Source: ACcGV6136ffC//JcnnyrMG0y2n00aIDbugJKM82muA89ePztM0iu0xC4sR14NzWFzR9Gv0x0JK8u X-Received: by 2002:a63:5445:: with SMTP id e5-v6mr2615361pgm.316.1538220497939; Sat, 29 Sep 2018 04:28:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538220497; cv=none; d=google.com; s=arc-20160816; b=oP90NbaO/0z/bRcEmEgMtyq5tGOkb048Iz7agRslOyZiUDA+cCovvdxPfxwyW064pE CH2lLPS4wvMPiyLlH1W6Sr7SXHKfXoMQeXN5qw+vPDAJ94zDu7448eC6f/D+zoNTCl0T /FfcAXBtbkPWKmH9Sj2nxTRly6hRKJ4rqSPXPcGtxf3475cBYKIJX3MMizG6g9su1XBQ RLp2tSn+1AZlv4Snn8ym9pFzvF5LP4qHB+vmeECG0LyHbYIALiqnk+1CLDhO+JZHRGw4 nnGkzPhVMp64LYd6z58uLbV6fIivqBdPXZwmafzCdf60EFpNn0/h6fLAGhQa4gn+x+Rh yDIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=/O1Qc58Y8ZoDoCSyTMy0cxjicF0L4uRy8MQT4/cGw0w=; b=zRm6Cx8Gqsw579HkMS2YenRC42FYlymzXUDZhDBwIAwEDO7pEQclE0fZ45p9mZxGWD rLxNxUGvLuKJCALj05Sk+k7ipPg2gx2oED9SqgxPuXTSqAupNGGXfvno5Ci4ThAukXQS FmRgu2FsLtPEB0r1oZzlbUDKe8tPONI4KopS2swXDpVIj3cZcOmVrhdxuAEcRA3dPhAv Qqt1HDE1SFnzIP/IHRW+lVX+ZscbDrkxcPkq0FgQE4Ddxgyn84zm/A3Kk1lOadllPZ/5 Lsr+0fm8lk1de59JCNDaKF1fQ+NkSQ2IUWNAuviSceWBqA9+/5svQ8qHowFHPs5+T19J a64w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fQGALSAs; spf=pass (google.com: best guess record for domain of netdev-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m76-v6si7494789pfg.323.2018.09.29.04.28.17; Sat, 29 Sep 2018 04:28:17 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of netdev-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fQGALSAs; spf=pass (google.com: best guess record for domain of netdev-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728164AbeI2R4W (ORCPT + 10 others); Sat, 29 Sep 2018 13:56:22 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:53535 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727961AbeI2R4V (ORCPT ); Sat, 29 Sep 2018 13:56:21 -0400 Received: by mail-wm1-f65.google.com with SMTP id b19-v6so4492919wme.3 for ; Sat, 29 Sep 2018 04:28:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=/O1Qc58Y8ZoDoCSyTMy0cxjicF0L4uRy8MQT4/cGw0w=; b=fQGALSAs0JeEnZ73JnjjohmgL8WC8a2Jy3QUDg4s3+oS6F6hFYt5amj0myER2I24h6 a1cQN4/npZ1ShkJ6j8St+bY4d4EGF57TYmPXFhqP2dRzi5mpIP0bvP/vUJBD2YR3VSTk MqWuYpLgIswgBMpnxqgT4lkSAEyV2VvJlwWa0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=/O1Qc58Y8ZoDoCSyTMy0cxjicF0L4uRy8MQT4/cGw0w=; b=V1OqGkI3XAHrr5bscBz4/SUA2UoGu1MZloIolUeWx9v1wSfMK5OFjPvF/jq7dlItec 1Z5BQ7KnzUd6dzzKlNLQQnlYUOAEuESjyjZ6ms6Kp4z2nIGpNj90I+9pDbdS9rpL9jj7 zYO1/Vr8HmL1Mfu6iQ+KLCJuhfKls+pxXJqgfA5g/zkJTrcxxrdIWPSp8NuU3tcC9qiG Ktb6Fyj9SBoXbMg8R+TuMrdhkIVorrgnRdemGcu0uk/BG7JovmJtArOgmP5bJ5D6AAEz 3lT/BQzObNJgrj8tbvyRPi4Xv/WUnskYWHaaY7X2J+2+8eWRK2/9JiUH9eY+YrgRtIZH SKYg== X-Gm-Message-State: ABuFfohEIpTvrJj2GXlRrKq2dujzFQczCWgdtV7knVT8QD0Y9z5VzUMP gO+mPxZ1lFg+jyEUXrxQsHB59lUhzYw= X-Received: by 2002:a1c:1d58:: with SMTP id d85-v6mr4411454wmd.142.1538220492035; Sat, 29 Sep 2018 04:28:12 -0700 (PDT) Received: from localhost.localdomain ([2a02:587:4603:8d00:2e56:dcff:fe9a:8f06]) by smtp.gmail.com with ESMTPSA id q5-v6sm6841080wmd.29.2018.09.29.04.28.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 29 Sep 2018 04:28:11 -0700 (PDT) From: Ilias Apalodimas To: netdev@vger.kernel.org, jaswinder.singh@linaro.org Cc: ard.biesheuvel@linaro.org, masami.hiramatsu@linaro.org, arnd@arndb.de, bjorn.topel@intel.com, magnus.karlsson@intel.com, brouer@redhat.com, daniel@iogearbox.net, ast@kernel.org, jesus.sanchez-palencia@intel.com, vinicius.gomes@intel.com, makita.toshiaki@lab.ntt.co.jp, Ilias Apalodimas Subject: [net-next, PATCH 1/2, v3] net: socionext: different approach on DMA Date: Sat, 29 Sep 2018 14:28:01 +0300 Message-Id: <1538220482-16129-2-git-send-email-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1538220482-16129-1-git-send-email-ilias.apalodimas@linaro.org> References: <1538220482-16129-1-git-send-email-ilias.apalodimas@linaro.org> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Current driver dynamically allocates an skb and maps it as DMA rx buffer. A following patch introduces XDP functionality, so we need a different allocation scheme. Buffers are allocated dynamically and mapped into hardware. During the Rx operation the driver uses build_skb() to produce the necessary buffers for the network stack Signed-off-by: Ilias Apalodimas --- drivers/net/ethernet/socionext/netsec.c | 238 +++++++++++++++++--------------- 1 file changed, 129 insertions(+), 109 deletions(-) -- 2.7.4 diff --git a/drivers/net/ethernet/socionext/netsec.c b/drivers/net/ethernet/socionext/netsec.c index 7aa5ebb..8f788a1 100644 --- a/drivers/net/ethernet/socionext/netsec.c +++ b/drivers/net/ethernet/socionext/netsec.c @@ -296,6 +296,11 @@ struct netsec_rx_pkt_info { bool err_flag; }; +static void netsec_rx_fill(struct netsec_priv *priv, u16 from, u16 num); + +static void *netsec_alloc_rx_data(struct netsec_priv *priv, + dma_addr_t *dma_addr, u16 *len); + static void netsec_write(struct netsec_priv *priv, u32 reg_addr, u32 val) { writel(val, priv->ioaddr + reg_addr); @@ -556,34 +561,10 @@ static const struct ethtool_ops netsec_ethtool_ops = { /************* NETDEV_OPS FOLLOW *************/ -static struct sk_buff *netsec_alloc_skb(struct netsec_priv *priv, - struct netsec_desc *desc) -{ - struct sk_buff *skb; - - if (device_get_dma_attr(priv->dev) == DEV_DMA_COHERENT) { - skb = netdev_alloc_skb_ip_align(priv->ndev, desc->len); - } else { - desc->len = L1_CACHE_ALIGN(desc->len); - skb = netdev_alloc_skb(priv->ndev, desc->len); - } - if (!skb) - return NULL; - - desc->addr = skb->data; - desc->dma_addr = dma_map_single(priv->dev, desc->addr, desc->len, - DMA_FROM_DEVICE); - if (dma_mapping_error(priv->dev, desc->dma_addr)) { - dev_kfree_skb_any(skb); - return NULL; - } - return skb; -} static void netsec_set_rx_de(struct netsec_priv *priv, struct netsec_desc_ring *dring, u16 idx, - const struct netsec_desc *desc, - struct sk_buff *skb) + const struct netsec_desc *desc) { struct netsec_de *de = dring->vaddr + DESC_SZ * idx; u32 attr = (1 << NETSEC_RX_PKT_OWN_FIELD) | @@ -602,59 +583,6 @@ static void netsec_set_rx_de(struct netsec_priv *priv, dring->desc[idx].dma_addr = desc->dma_addr; dring->desc[idx].addr = desc->addr; dring->desc[idx].len = desc->len; - dring->desc[idx].skb = skb; -} - -static struct sk_buff *netsec_get_rx_de(struct netsec_priv *priv, - struct netsec_desc_ring *dring, - u16 idx, - struct netsec_rx_pkt_info *rxpi, - struct netsec_desc *desc, u16 *len) -{ - struct netsec_de de = {}; - - memcpy(&de, dring->vaddr + DESC_SZ * idx, DESC_SZ); - - *len = de.buf_len_info >> 16; - - rxpi->err_flag = (de.attr >> NETSEC_RX_PKT_ER_FIELD) & 1; - rxpi->rx_cksum_result = (de.attr >> NETSEC_RX_PKT_CO_FIELD) & 3; - rxpi->err_code = (de.attr >> NETSEC_RX_PKT_ERR_FIELD) & - NETSEC_RX_PKT_ERR_MASK; - *desc = dring->desc[idx]; - return desc->skb; -} - -static struct sk_buff *netsec_get_rx_pkt_data(struct netsec_priv *priv, - struct netsec_rx_pkt_info *rxpi, - struct netsec_desc *desc, - u16 *len) -{ - struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; - struct sk_buff *tmp_skb, *skb = NULL; - struct netsec_desc td; - int tail; - - *rxpi = (struct netsec_rx_pkt_info){}; - - td.len = priv->ndev->mtu + 22; - - tmp_skb = netsec_alloc_skb(priv, &td); - - tail = dring->tail; - - if (!tmp_skb) { - netsec_set_rx_de(priv, dring, tail, &dring->desc[tail], - dring->desc[tail].skb); - } else { - skb = netsec_get_rx_de(priv, dring, tail, rxpi, desc, len); - netsec_set_rx_de(priv, dring, tail, &td, tmp_skb); - } - - /* move tail ahead */ - dring->tail = (dring->tail + 1) % DESC_NUM; - - return skb; } static int netsec_clean_tx_dring(struct netsec_priv *priv, int budget) @@ -721,19 +649,28 @@ static int netsec_process_tx(struct netsec_priv *priv, int budget) return done; } +static void netsec_adv_desc(u16 *idx) +{ + *idx = *idx + 1; + if (unlikely(*idx >= DESC_NUM)) + *idx = 0; +} + static int netsec_process_rx(struct netsec_priv *priv, int budget) { struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; struct net_device *ndev = priv->ndev; - struct netsec_rx_pkt_info rx_info; - int done = 0; - struct netsec_desc desc; struct sk_buff *skb; - u16 len; + int done = 0; while (done < budget) { u16 idx = dring->tail; struct netsec_de *de = dring->vaddr + (DESC_SZ * idx); + struct netsec_desc *desc = &dring->desc[idx]; + struct netsec_rx_pkt_info rpi; + u16 pkt_len, desc_len; + dma_addr_t dma_handle; + void *buf_addr; if (de->attr & (1U << NETSEC_RX_PKT_OWN_FIELD)) break; @@ -744,28 +681,62 @@ static int netsec_process_rx(struct netsec_priv *priv, int budget) */ dma_rmb(); done++; - skb = netsec_get_rx_pkt_data(priv, &rx_info, &desc, &len); - if (unlikely(!skb) || rx_info.err_flag) { + + pkt_len = de->buf_len_info >> 16; + rpi.err_code = (de->attr >> NETSEC_RX_PKT_ERR_FIELD) & + NETSEC_RX_PKT_ERR_MASK; + rpi.err_flag = (de->attr >> NETSEC_RX_PKT_ER_FIELD) & 1; + if (rpi.err_flag) { netif_err(priv, drv, priv->ndev, - "%s: rx fail err(%d)\n", - __func__, rx_info.err_code); + "%s: rx fail err(%d)\n", __func__, + rpi.err_code); ndev->stats.rx_dropped++; + netsec_adv_desc(&dring->tail); + /* reuse buffer page frag */ + netsec_rx_fill(priv, idx, 1); continue; } + rpi.rx_cksum_result = (de->attr >> NETSEC_RX_PKT_CO_FIELD) & 3; - dma_unmap_single(priv->dev, desc.dma_addr, desc.len, - DMA_FROM_DEVICE); - skb_put(skb, len); + buf_addr = netsec_alloc_rx_data(priv, &dma_handle, &desc_len); + if (unlikely(!buf_addr)) + break; + + dma_sync_single_for_cpu(priv->dev, desc->dma_addr, pkt_len, + DMA_FROM_DEVICE); + prefetch(desc->addr); + + skb = build_skb(desc->addr, desc->len); + if (unlikely(!skb)) { + dma_unmap_single(priv->dev, dma_handle, desc_len, + DMA_TO_DEVICE); + skb_free_frag(buf_addr); + netif_err(priv, drv, priv->ndev, + "rx failed to alloc skb\n"); + break; + } + dma_unmap_single_attrs(priv->dev, desc->dma_addr, desc->len, + DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC); + + /* Update the descriptor with fresh buffers */ + desc->len = desc_len; + desc->dma_addr = dma_handle; + desc->addr = buf_addr; + + skb_put(skb, pkt_len); skb->protocol = eth_type_trans(skb, priv->ndev); if (priv->rx_cksum_offload_flag && - rx_info.rx_cksum_result == NETSEC_RX_CKSUM_OK) + rpi.rx_cksum_result == NETSEC_RX_CKSUM_OK) skb->ip_summed = CHECKSUM_UNNECESSARY; if (napi_gro_receive(&priv->napi, skb) != GRO_DROP) { ndev->stats.rx_packets++; - ndev->stats.rx_bytes += len; + ndev->stats.rx_bytes += pkt_len; } + + netsec_rx_fill(priv, idx, 1); + netsec_adv_desc(&dring->tail); } return done; @@ -928,7 +899,10 @@ static void netsec_uninit_pkt_dring(struct netsec_priv *priv, int id) dma_unmap_single(priv->dev, desc->dma_addr, desc->len, id == NETSEC_RING_RX ? DMA_FROM_DEVICE : DMA_TO_DEVICE); - dev_kfree_skb(desc->skb); + if (id == NETSEC_RING_RX) + skb_free_frag(desc->addr); + else if (id == NETSEC_RING_TX) + dev_kfree_skb(desc->skb); } memset(dring->desc, 0, sizeof(struct netsec_desc) * DESC_NUM); @@ -953,50 +927,96 @@ static void netsec_free_dring(struct netsec_priv *priv, int id) dring->desc = NULL; } +static void *netsec_alloc_rx_data(struct netsec_priv *priv, + dma_addr_t *dma_handle, u16 *desc_len) +{ + size_t len = priv->ndev->mtu + ETH_HLEN + 2 * VLAN_HLEN + NET_SKB_PAD + + NET_IP_ALIGN; + dma_addr_t mapping; + void *buf; + + len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); + len = SKB_DATA_ALIGN(len); + + buf = napi_alloc_frag(len); + if (!buf) + return NULL; + + mapping = dma_map_single(priv->dev, buf, len, DMA_FROM_DEVICE); + if (unlikely(dma_mapping_error(priv->dev, mapping))) + goto err_out; + + *dma_handle = mapping; + *desc_len = len; + + return buf; + +err_out: + skb_free_frag(buf); + return NULL; +} + +static void netsec_rx_fill(struct netsec_priv *priv, u16 from, u16 num) +{ + struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; + u16 idx = from; + + while (num) { + netsec_set_rx_de(priv, dring, idx, &dring->desc[idx]); + idx++; + if (idx >= DESC_NUM) + idx = 0; + num--; + } +} + static int netsec_alloc_dring(struct netsec_priv *priv, enum ring_id id) { struct netsec_desc_ring *dring = &priv->desc_ring[id]; - int ret = 0; dring->vaddr = dma_zalloc_coherent(priv->dev, DESC_SZ * DESC_NUM, &dring->desc_dma, GFP_KERNEL); - if (!dring->vaddr) { - ret = -ENOMEM; + if (!dring->vaddr) goto err; - } dring->desc = kcalloc(DESC_NUM, sizeof(*dring->desc), GFP_KERNEL); - if (!dring->desc) { - ret = -ENOMEM; + if (!dring->desc) goto err; - } return 0; err: netsec_free_dring(priv, id); - return ret; + return -ENOMEM; } static int netsec_setup_rx_dring(struct netsec_priv *priv) { struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; - struct netsec_desc desc; - struct sk_buff *skb; - int n; + int i; - desc.len = priv->ndev->mtu + 22; + for (i = 0; i < DESC_NUM; i++) { + struct netsec_desc *desc = &dring->desc[i]; + dma_addr_t dma_handle; + void *buf; + u16 len; - for (n = 0; n < DESC_NUM; n++) { - skb = netsec_alloc_skb(priv, &desc); - if (!skb) { + buf = netsec_alloc_rx_data(priv, &dma_handle, &len); + if (!buf) { netsec_uninit_pkt_dring(priv, NETSEC_RING_RX); - return -ENOMEM; + goto err_out; } - netsec_set_rx_de(priv, dring, n, &desc, skb); + desc->dma_addr = dma_handle; + desc->addr = buf; + desc->len = len; } + netsec_rx_fill(priv, 0, DESC_NUM); + return 0; + +err_out: + return -ENOMEM; } static int netsec_netdev_load_ucode_region(struct netsec_priv *priv, u32 reg,