From patchwork Sun Feb 5 22:10:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geoff Levand X-Patchwork-Id: 13129147 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7FA2FC636D4 for ; Sun, 5 Feb 2023 22:10:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229595AbjBEWK0 (ORCPT ); Sun, 5 Feb 2023 17:10:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229522AbjBEWKX (ORCPT ); Sun, 5 Feb 2023 17:10:23 -0500 Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1234::107]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B95E1ABEC for ; Sun, 5 Feb 2023 14:10:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=Date:To:Subject:From:References: In-Reply-To:Message-Id:Sender:Reply-To:Cc:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description; bh=5rTwwskUPZzJV/mQr5VFwBvGgaEYVVx+4axYWt0kjr8=; b=R2hscksm4lvhIluqehAYmp8v85 KsbDcJkKHRbz/kDUrxdh7lOdvUXJ1gE6pVqf5unE5u09MTpEHVe6f5AsRu40Jnpg8duzBvYYn81Lg UthoCzFRWdiQSjN7K1AetDKRZNOrzHUG/32JjnSN42EXeZyuW36M1Y0qMqiZPwg6i5Zdkk5trEHAw FSsDHSfW2f9IhdfOVaYNQ+7Iki+T8IFcoHV1Xlqy2GsZH7/OrlimAIw5CYKswDpGT4Fb5RMDvnaMP SF9IxsP4ecIgW3HLIcG8rthgWXBJ0SdhEIqnEI7m7kC2uE7rWlhSq3So5Oh92qPq27BkHNhpghgZk m8MOvqZQ==; Received: from geoff by merlin.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pOnDE-002Oky-TL; Sun, 05 Feb 2023 22:10:17 +0000 Message-Id: <4150b1589ed367e18855c16762ff160e9d73a42f.1675632296.git.geoff@infradead.org> In-Reply-To: References: From: Geoff Levand Patch-Date: Sun, 5 Feb 2023 11:10:22 -0800 Subject: [PATCH net v4 1/2] net/ps3_gelic_net: Fix RX sk_buff length To: netdev@vger.kernel.org, Jakub Kicinski , "David S. Miller" Date: Sun, 05 Feb 2023 22:10:16 +0000 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org The Gelic Ethernet device needs to have the RX sk_buffs aligned to GELIC_NET_RXBUF_ALIGN and the length of the RX sk_buffs must be a multiple of GELIC_NET_RXBUF_ALIGN. The current Gelic Ethernet driver was not allocating sk_buffs large enough to allow for this alignment. Fixes various randomly occurring runtime network errors. Fixes: 02c1889166b4 (ps3: gigabit ethernet driver for PS3, take3) Signed-off-by: Geoff Levand --- drivers/net/ethernet/toshiba/ps3_gelic_net.c | 56 ++++++++++++-------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/drivers/net/ethernet/toshiba/ps3_gelic_net.c b/drivers/net/ethernet/toshiba/ps3_gelic_net.c index cf8de8a7a8a1..7a8b5e1e77a6 100644 --- a/drivers/net/ethernet/toshiba/ps3_gelic_net.c +++ b/drivers/net/ethernet/toshiba/ps3_gelic_net.c @@ -365,51 +365,63 @@ static int gelic_card_init_chain(struct gelic_card *card, * * allocates a new rx skb, iommu-maps it and attaches it to the descriptor. * Activate the descriptor state-wise + * + * Gelic RX sk_buffs must be aligned to GELIC_NET_RXBUF_ALIGN and the length + * must be a multiple of GELIC_NET_RXBUF_ALIGN. */ static int gelic_descr_prepare_rx(struct gelic_card *card, struct gelic_descr *descr) { - int offset; - unsigned int bufsize; + struct device *dev = ctodev(card); + struct { + unsigned int total_bytes; + unsigned int offset; + } aligned_buf; + dma_addr_t cpu_addr; if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE) dev_info(ctodev(card), "%s: ERROR status\n", __func__); - /* we need to round up the buffer size to a multiple of 128 */ - bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN); - /* and we need to have it 128 byte aligned, therefore we allocate a - * bit more */ - descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1); + aligned_buf.total_bytes = (GELIC_NET_RXBUF_ALIGN - 1) + + GELIC_NET_MAX_MTU + (GELIC_NET_RXBUF_ALIGN - 1); + + descr->skb = dev_alloc_skb(aligned_buf.total_bytes); + if (!descr->skb) { - descr->buf_addr = 0; /* tell DMAC don't touch memory */ + descr->buf_addr = 0; return -ENOMEM; } - descr->buf_size = cpu_to_be32(bufsize); + + aligned_buf.offset = + PTR_ALIGN(descr->skb->data, GELIC_NET_RXBUF_ALIGN) - + descr->skb->data; + + descr->buf_size = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN); descr->dmac_cmd_status = 0; descr->result_size = 0; descr->valid_size = 0; descr->data_error = 0; - offset = ((unsigned long)descr->skb->data) & - (GELIC_NET_RXBUF_ALIGN - 1); - if (offset) - skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset); - /* io-mmu-map the skb */ - descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card), - descr->skb->data, - GELIC_NET_MAX_MTU, - DMA_FROM_DEVICE)); + skb_reserve(descr->skb, aligned_buf.offset); + + cpu_addr = dma_map_single(dev, descr->skb->data, descr->buf_size, + DMA_FROM_DEVICE); + + descr->buf_addr = cpu_to_be32(cpu_addr); + if (!descr->buf_addr) { dev_kfree_skb_any(descr->skb); + descr->buf_addr = 0; + descr->buf_size = 0; descr->skb = NULL; - dev_info(ctodev(card), + dev_info(dev, "%s:Could not iommu-map rx buffer\n", __func__); gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); return -ENOMEM; - } else { - gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED); - return 0; } + + gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED); + return 0; } /** From patchwork Sun Feb 5 22:10:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geoff Levand X-Patchwork-Id: 13129146 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A30C4C636CC for ; Sun, 5 Feb 2023 22:10:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229481AbjBEWKY (ORCPT ); Sun, 5 Feb 2023 17:10:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229486AbjBEWKX (ORCPT ); Sun, 5 Feb 2023 17:10:23 -0500 Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1234::107]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F33D1ADC5 for ; Sun, 5 Feb 2023 14:10:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=Date:To:Subject:From:References: In-Reply-To:Message-Id:Sender:Reply-To:Cc:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description; bh=neI+YkCTPXrkVPL5x8/t9ynrGai34LT/MwmL/gO/Xq8=; b=yd0dN+QL7tdqSw31IzCvFakhs1 X5djXrEbkKw8cby7I4l7qEH3xyXCOOAQ/mBIrmqggfVdFucAcSYmoQrCYUSs4WSwhft1Xkccs1Czq jzvKHaTkfs8wIVgV9a09+8ojl/PyX9BO/ceXLD/u5Cc6YsNnl+wsibQIaD6pL9MjQD3+8JTRij+Y/ bmnHdZDzhBh4zMPh0uBPXF7vFxh8x0clwSgRXuMmPjFIKDlLdbTO/lY4d91kt9mPAEoZrUfU+IDfE 8ZeLoieb65Yxja1YQGs8jY6yM5LWK0Nv3xHSra6AHJC7kL1CQ+OvfHTU8Hj5odfPOk8WEAlw1Uid+ zscFrMFw==; Received: from geoff by merlin.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pOnDF-002Ol0-55; Sun, 05 Feb 2023 22:10:17 +0000 Message-Id: <8d40259f863ed1a077687f3c3d5b8b3707478170.1675632296.git.geoff@infradead.org> In-Reply-To: References: From: Geoff Levand Patch-Date: Sun, 5 Feb 2023 11:10:22 -0800 Subject: [PATCH net v4 2/2] net/ps3_gelic_net: Use dma_mapping_error To: netdev@vger.kernel.org, Jakub Kicinski , "David S. Miller" Date: Sun, 05 Feb 2023 22:10:17 +0000 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org The current Gelic Etherenet driver was checking the return value of its dma_map_single call, and not using the dma_mapping_error() routine. Fixes runtime problems like these: DMA-API: ps3_gelic_driver sb_05: device driver failed to check map error WARNING: CPU: 0 PID: 0 at kernel/dma/debug.c:1027 .check_unmap+0x888/0x8dc Fixes: 02c1889166b4 (ps3: gigabit ethernet driver for PS3, take3) Signed-off-by: Geoff Levand --- drivers/net/ethernet/toshiba/ps3_gelic_net.c | 52 ++++++++++---------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/drivers/net/ethernet/toshiba/ps3_gelic_net.c b/drivers/net/ethernet/toshiba/ps3_gelic_net.c index 7a8b5e1e77a6..5622b512e2e4 100644 --- a/drivers/net/ethernet/toshiba/ps3_gelic_net.c +++ b/drivers/net/ethernet/toshiba/ps3_gelic_net.c @@ -309,22 +309,34 @@ static int gelic_card_init_chain(struct gelic_card *card, struct gelic_descr_chain *chain, struct gelic_descr *start_descr, int no) { - int i; + struct device *dev = ctodev(card); struct gelic_descr *descr; + int i; - descr = start_descr; - memset(descr, 0, sizeof(*descr) * no); + memset(start_descr, 0, no * sizeof(*start_descr)); /* set up the hardware pointers in each descriptor */ - for (i = 0; i < no; i++, descr++) { + for (i = 0, descr = start_descr; i < no; i++, descr++) { gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); descr->bus_addr = dma_map_single(ctodev(card), descr, GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL); - if (!descr->bus_addr) - goto iommu_error; + if (unlikely(dma_mapping_error(dev, descr->bus_addr))) { + dev_err(dev, "%s:%d: dma_mapping_error\n", __func__, + __LINE__); + + for (i--, descr--; i > 0; i--, descr--) { + if (descr->bus_addr) { + dma_unmap_single(ctodev(card), + descr->bus_addr, + GELIC_DESCR_SIZE, + DMA_BIDIRECTIONAL); + } + } + return -ENOMEM; + } descr->next = descr + 1; descr->prev = descr - 1; @@ -334,8 +346,7 @@ static int gelic_card_init_chain(struct gelic_card *card, start_descr->prev = (descr - 1); /* chain bus addr of hw descriptor */ - descr = start_descr; - for (i = 0; i < no; i++, descr++) { + for (i = 0, descr = start_descr; i < no; i++, descr++) { descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr); } @@ -346,14 +357,6 @@ static int gelic_card_init_chain(struct gelic_card *card, (descr - 1)->next_descr_addr = 0; return 0; - -iommu_error: - for (i--, descr--; 0 <= i; i--, descr--) - if (descr->bus_addr) - dma_unmap_single(ctodev(card), descr->bus_addr, - GELIC_DESCR_SIZE, - DMA_BIDIRECTIONAL); - return -ENOMEM; } /** @@ -407,19 +410,18 @@ static int gelic_descr_prepare_rx(struct gelic_card *card, cpu_addr = dma_map_single(dev, descr->skb->data, descr->buf_size, DMA_FROM_DEVICE); - descr->buf_addr = cpu_to_be32(cpu_addr); - - if (!descr->buf_addr) { + if (unlikely(dma_mapping_error(dev, cpu_addr))) { + dev_err(dev, "%s:%d: dma_mapping_error\n", __func__, __LINE__); dev_kfree_skb_any(descr->skb); descr->buf_addr = 0; descr->buf_size = 0; descr->skb = NULL; - dev_info(dev, - "%s:Could not iommu-map rx buffer\n", __func__); gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); return -ENOMEM; } + descr->buf_addr = cpu_to_be32(cpu_addr); + gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED); return 0; } @@ -775,6 +777,7 @@ static int gelic_descr_prepare_tx(struct gelic_card *card, struct gelic_descr *descr, struct sk_buff *skb) { + struct device *dev = ctodev(card); dma_addr_t buf; if (card->vlan_required) { @@ -789,11 +792,10 @@ static int gelic_descr_prepare_tx(struct gelic_card *card, skb = skb_tmp; } - buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE); + buf = dma_map_single(dev, skb->data, skb->len, DMA_TO_DEVICE); - if (!buf) { - dev_err(ctodev(card), - "dma map 2 failed (%p, %i). Dropping packet\n", + if (unlikely(dma_mapping_error(dev, buf))) { + dev_err(dev, "dma map 2 failed (%p, %i). Dropping packet\n", skb->data, skb->len); return -ENOMEM; }