From patchwork Fri Jul 16 16:56:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 479567 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 11634C12002 for ; Fri, 16 Jul 2021 16:57:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E4D38613EB for ; Fri, 16 Jul 2021 16:57:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230143AbhGPQ75 (ORCPT ); Fri, 16 Jul 2021 12:59:57 -0400 Received: from smtp-33.italiaonline.it ([213.209.10.33]:34251 "EHLO libero.it" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S229794AbhGPQ7y (ORCPT ); Fri, 16 Jul 2021 12:59:54 -0400 Received: from passgat-Modern-14-A10M.homenet.telecomitalia.it ([79.54.92.92]) by smtp-33.iol.local with ESMTPA id 4R8tmKNNmS6GM4R8zmO7bE; Fri, 16 Jul 2021 18:56:57 +0200 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=libero.it; s=s2021; t=1626454617; bh=ukKbkV8ZQ3gcpPYd1NqECiAHtTGPMg1hlJa5khe340Q=; h=From; b=PvKsqz2ilX8HLF1W+dIjHYBr5w5CTtdCOS7Skf32W6bO/t3G51+Yumry2woQVanWt EGAIJjzWS7ZAFWR4+/RkKDZnEgm4WwG2ZWPG0Jg2/aELrO7Htb4RscVgZxlHE3a6go 9G/K6oGEJojUAgoRKNsR/2VcABQlXBDWMWJHFn7fLcL7c63E1yMHTjQZXDHm6kbzvh 4ITMIzK5Vk5zsl1li2Kt9aBrT8RI0QQ7tlvRAiT4BOeLY9RfqwiIEU1OdTj9cMYcaW XKwYb8i/3y7PCenVKSsdV4EreDBEGq9FGL5+uoEFLaeWKRJY6086tLXKqFdwzxAoLM 394N6KjQv3LSg== X-CNFS-Analysis: v=2.4 cv=AcF0o1bG c=1 sm=1 tr=0 ts=60f1ba59 cx=a_exe a=eKwsI+FXzXP/Nc4oRbpalQ==:117 a=eKwsI+FXzXP/Nc4oRbpalQ==:17 a=TSeAaTSCjiJB7GOU1roA:9 From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Gianluca Falavigna , Dario Binacchi , Andrew Lunn , "David S. Miller" , Jakub Kicinski , Marc Kleine-Budde , Tong Zhang , Wolfgang Grandegger , linux-can@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH 1/4] can: c_can: remove struct c_can_priv::priv field Date: Fri, 16 Jul 2021 18:56:20 +0200 Message-Id: <20210716165623.19677-2-dariobin@libero.it> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210716165623.19677-1-dariobin@libero.it> References: <20210716165623.19677-1-dariobin@libero.it> X-CMAE-Envelope: MS4xfKoMV/+nZGBCPIXBiH3gvl6PDXsLIr3ajj3MP7Mrxti8SWfQlE/FXiPXzaPo3/kHl2XKpq4Gy1ZfkK7F1OP7Wfm9TETNnU1dtw+U56kgbTbcL3g2u1YN ItnFnJG5lXNPyTBo5oQjLpt8tnjq6mhufAss2yoNaPmKrVAtfctBfjD9X5jec6FT0TKq+QEf6bxYkGdrxWEDn8Y50gZEeS0i3DeEi6bM1iaPQNRwqRXisEVx /3p6Uonpt9uW1AapLvVqBNwL5yneD2CeenAe5qCVjBdY3McW7MsSoko84sGBgJ8ZsolWYSRM6UnZuPJ9axnV5Hq6tbKF3FNv/YvoCCRfiqtDKIqZV2vc9QWg pJaUlH4TjjOuKiD44lZU/G1uxqTzG9P5QQ61fwJyK5B5bVLCgAw+3kAtEajNvJ33VIxjPUk2sNo68SCGcMYgwAwvT2lvelE7KR6DlUwZC9At4ThTfH+AsMvF YxJmrJqMdgr1bfpJUU8fVjdHMXCKNAnx1psceFuGnYRRyebx/KP/y63R3RNZp7RunUTginxg8tucDuTW Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org It references the clock but it is never used. So let's remove it. Signed-off-by: Dario Binacchi --- drivers/net/can/c_can/c_can.h | 1 - drivers/net/can/c_can/c_can_platform.c | 1 - 2 files changed, 2 deletions(-) diff --git a/drivers/net/can/c_can/c_can.h b/drivers/net/can/c_can/c_can.h index 4247ff80a29c..8f23e9c83c84 100644 --- a/drivers/net/can/c_can/c_can.h +++ b/drivers/net/can/c_can/c_can.h @@ -200,7 +200,6 @@ struct c_can_priv { void (*write_reg32)(const struct c_can_priv *priv, enum reg index, u32 val); void __iomem *base; const u16 *regs; - void *priv; /* for board-specific data */ enum c_can_dev_id type; struct c_can_raminit raminit_sys; /* RAMINIT via syscon regmap */ void (*raminit)(const struct c_can_priv *priv, bool enable); diff --git a/drivers/net/can/c_can/c_can_platform.c b/drivers/net/can/c_can/c_can_platform.c index 36950363682f..86e95e9d6533 100644 --- a/drivers/net/can/c_can/c_can_platform.c +++ b/drivers/net/can/c_can/c_can_platform.c @@ -385,7 +385,6 @@ static int c_can_plat_probe(struct platform_device *pdev) priv->base = addr; priv->device = &pdev->dev; priv->can.clock.freq = clk_get_rate(clk); - priv->priv = clk; priv->type = drvdata->id; platform_set_drvdata(pdev, dev); From patchwork Fri Jul 16 16:56:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 479566 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4DE61C636CA for ; Fri, 16 Jul 2021 16:57:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 31DC361396 for ; Fri, 16 Jul 2021 16:57:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230431AbhGPRAL (ORCPT ); Fri, 16 Jul 2021 13:00:11 -0400 Received: from smtp-33-i2.italiaonline.it ([213.209.12.33]:46235 "EHLO libero.it" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230088AbhGPQ7z (ORCPT ); Fri, 16 Jul 2021 12:59:55 -0400 Received: from passgat-Modern-14-A10M.homenet.telecomitalia.it ([79.54.92.92]) by smtp-33.iol.local with ESMTPA id 4R8tmKNNmS6GM4R90mO7bT; Fri, 16 Jul 2021 18:56:58 +0200 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=libero.it; s=s2021; t=1626454618; bh=aipa+Z7keBjj+2EjLI43y9cbq8bUs5mFT1fgia6YCVA=; h=From; b=TOINo3DuxcZVbmN6AyVOVV7HrJ46uzp6zlcShoAtcF305J3+4PE1CX6X9rJHPR82d pBFsy05IUlbKgXkFKQhJfkZosCKYeyyA1vAHGhzN9bLFhIy56inHqEH7jRgwvK6BdG 6f8eLwsLSAwN5tdS89+9MyVgprDuQZzKZPFK04ap5P7OfDaBZpaglutyGV1bws7uAE niR9THYk2QDVofzIMYrRLnw+KUz2+N/PPuoP+tUdc+J01Ss8omHXphW9BsABIYEJvK t8PmOeG5fxrnFMmUcJtat9yzq4YSeIvUyrygmP5s6iH/5dYNi7H5zNjYUDUXlgMewI 3d9f8d1+l/54w== X-CNFS-Analysis: v=2.4 cv=AcF0o1bG c=1 sm=1 tr=0 ts=60f1ba5a cx=a_exe a=eKwsI+FXzXP/Nc4oRbpalQ==:117 a=eKwsI+FXzXP/Nc4oRbpalQ==:17 a=0RFnQWo-as75lT-uasUA:9 From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Gianluca Falavigna , Dario Binacchi , "David S. Miller" , Jakub Kicinski , Marc Kleine-Budde , Oliver Hartkopp , Vincent Mailhol , Wolfgang Grandegger , linux-can@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH 2/4] can: c_can: exit c_can_do_tx() early if no frames have been sent Date: Fri, 16 Jul 2021 18:56:21 +0200 Message-Id: <20210716165623.19677-3-dariobin@libero.it> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210716165623.19677-1-dariobin@libero.it> References: <20210716165623.19677-1-dariobin@libero.it> X-CMAE-Envelope: MS4xfLH9hcar4n2Z0lDfTRw6KsDOa/8RUbDd3uxDk+x6+jx+2/mqp6ypsPo8YZHLpIWU9dbXqEKenWUeixgw0FphysQXlC+rw8+nSjVW1UJdJgPNSwc6veNg 5sUZzzTTYd26HNcbs9fulOr5URgYfAQVDVmq1igHEyR94YHpY25DsrxO6tm4Bs1Rsgq86eGg5ML+J3GRWDinCqRCkTevqu7VDp4qiCMf05hsuRqURjmEhvmD NAuQgvp7L3LaSjK2en2BSeewHz8qkYQX1fAg2mLQJqKzBbM/Re5wUCO6NUXy4QCWcSycxMieUVoT0CUvTHjP6C+kD85uimoXA9c1Yy54DK1Q1/bXQApL8SQ3 fjTgrIF/INX//+rUtfSa82/0yx56TZ5LZhJ8gxVYqRD1E6ZsVdxwjcoJV/xFTcb8/UZN3qwr/vRTI4uhkX5lDmiZHhALPib7PB4Dnsn9AZOtTG+FjTfyN0TA Ksppf9d6Qj4nCfWRgEIqU/aWHDKIvHD84byTdd9vaGYcAebc+FBqj461CkfA6cftBmUidka8yjY9WXDQuChdoPMuQBny38Eaa8V8uQ== Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org The c_can_poll() handles RX/TX events unconditionally. It may therefore happen that c_can_do_tx() is called unnecessarily because the interrupt was triggered by the reception of a frame. In these cases, we avoid to execute unnecessary statements and exit immediately. Signed-off-by: Dario Binacchi --- drivers/net/can/c_can/c_can_main.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/drivers/net/can/c_can/c_can_main.c b/drivers/net/can/c_can/c_can_main.c index 7588f70ca0fe..fec0e3416970 100644 --- a/drivers/net/can/c_can/c_can_main.c +++ b/drivers/net/can/c_can/c_can_main.c @@ -720,17 +720,18 @@ static void c_can_do_tx(struct net_device *dev) pkts++; } + if (!pkts) + return; + /* Clear the bits in the tx_active mask */ atomic_sub(clr, &priv->tx_active); if (clr & BIT(priv->msg_obj_tx_num - 1)) netif_wake_queue(dev); - if (pkts) { - stats->tx_bytes += bytes; - stats->tx_packets += pkts; - can_led_event(dev, CAN_LED_EVENT_TX); - } + stats->tx_bytes += bytes; + stats->tx_packets += pkts; + can_led_event(dev, CAN_LED_EVENT_TX); } /* If we have a gap in the pending bits, that means we either From patchwork Fri Jul 16 16:56:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 478736 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1497BC12002 for ; Fri, 16 Jul 2021 16:57:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ECD6E61396 for ; Fri, 16 Jul 2021 16:57:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230466AbhGPRAQ (ORCPT ); Fri, 16 Jul 2021 13:00:16 -0400 Received: from smtp-33-i2.italiaonline.it ([213.209.12.33]:37885 "EHLO libero.it" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230208AbhGPQ76 (ORCPT ); Fri, 16 Jul 2021 12:59:58 -0400 Received: from passgat-Modern-14-A10M.homenet.telecomitalia.it ([79.54.92.92]) by smtp-33.iol.local with ESMTPA id 4R8tmKNNmS6GM4R90mO7bh; Fri, 16 Jul 2021 18:56:58 +0200 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=libero.it; s=s2021; t=1626454618; bh=VGNCFLBzeyw84IVyEb6PZZrx3WMwI604pwR2tdhmc4g=; h=From; b=UBk+z1nm9wR4TYy2TKzrMrAwrjUSmOVulhehuJc3Jkfzq06N5YCLfBzFB+3vZv2Ia co5t4rQViC16HA1IRTs2Bt4Yki1S7XO1GXC6vG7d6BLSMZeq2zl1fmbptSUjG6faYi IIQPHMem+ZXDn2EB74RTCB2hSQkVavCsZFuCoWOD0A+wAHET2taVxZmUC2utDTeAwA HV8C9KDiMBuFwVrxWvP+Fw9Xi6xFx2MVMmnyduqHtCdhZmL3g/jJCJIxWEMUV0+vHU lpeYWfYnuJAy8dN8je6XiIcOkGgOBIhgaKnJb6S/ArJP5Q30GVXYUQzBOQuOtS6O5i d3dmPaNgpkLrg== X-CNFS-Analysis: v=2.4 cv=AcF0o1bG c=1 sm=1 tr=0 ts=60f1ba5a cx=a_exe a=eKwsI+FXzXP/Nc4oRbpalQ==:117 a=eKwsI+FXzXP/Nc4oRbpalQ==:17 a=VwQbUJbxAAAA:8 a=83yiEfbaggHBMMd4CMwA:9 a=TlxWwjO5pACKU3_K:21 a=9s_oUIA50SGwrPgz:21 a=AjGcO6oz07-iQ99wixmX:22 From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Gianluca Falavigna , Dario Binacchi , Andrew Lunn , "David S. Miller" , Jakub Kicinski , Marc Kleine-Budde , Oliver Hartkopp , Vincent Mailhol , Wolfgang Grandegger , linux-can@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH 3/4] can: c_can: support tx ring algorithm Date: Fri, 16 Jul 2021 18:56:22 +0200 Message-Id: <20210716165623.19677-4-dariobin@libero.it> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210716165623.19677-1-dariobin@libero.it> References: <20210716165623.19677-1-dariobin@libero.it> X-CMAE-Envelope: MS4xfLH9hcar4n2Z0lDfTRw6KsDOa/8RUbDd3uxDk+x6+jx+2/mqp6ypsPo8YZHLpIWU9dbXqEKenWUeixgw0FphysQXlC+rw8+nSjVW1UJdJgPNSwc6veNg 5sUZzzTTYd26HNcbs9fulOr5URgYfAQVDVmq1igHEyR94YHpY25DsrxOf5vR5PXH5BQvJu/QzohOnGEkrpcGdpiQ6jJZKF3N1yxyRTqRPvaGeuvcEPFDfLO5 cH4bIfuvHiuKmwbCc1Rm+XoPULxlyqDJ4IjHgHDlybQjPw/OF24GEB/sGNN069IEfEFhUj8OgN1vmfHnhhe6XrKFoLWBuapp+c3tW9Wv5aZGrkycmc1WZXDP di/E+GPp5trVFsnRIe4YKtJd9/8LXPCkcHnFVj5ufgEiqjIcAK/icBFezkPFCGsUj0XU/b7909HOrNFO+WHpU1LDY2xlVXpo1tpbGeKmAeFtexSWLDf7D5LH 9pG7CfqmNEPaIPvjl+iWhr8EU+8PvzZB5zssx8eDo3UK0r6dym+TyR7CEVnq9DrRZRaOFlyBfZ5VOPbnxyVW9qSyPUXNbEceKeBLzVrzCzb+nOAlMk8ueiNl 9V0= Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org The algorithm is already used successfully by other CAN drivers (e.g. mcp251xfd). Its implementation was kindly suggested to me by Marc Kleine-Budde following a patch I had previously submitted. You can find every detail at https://lore.kernel.org/patchwork/patch/1422929/. The idea is that after this patch, it will be easier to patch the driver to use the message object memory as a true FIFO. Suggested-by: Marc Kleine-Budde Signed-off-by: Dario Binacchi --- drivers/net/can/c_can/c_can.h | 19 ++++++- drivers/net/can/c_can/c_can_main.c | 81 +++++++++++++++++++++++------- 2 files changed, 82 insertions(+), 18 deletions(-) diff --git a/drivers/net/can/c_can/c_can.h b/drivers/net/can/c_can/c_can.h index 8f23e9c83c84..8fe7e2138620 100644 --- a/drivers/net/can/c_can/c_can.h +++ b/drivers/net/can/c_can/c_can.h @@ -176,6 +176,13 @@ struct c_can_raminit { bool needs_pulse; }; +/* c_can tx ring structure */ +struct c_can_tx_ring { + unsigned int head; + unsigned int tail; + unsigned int obj_num; +}; + /* c_can private data structure */ struct c_can_priv { struct can_priv can; /* must be the first member */ @@ -190,10 +197,10 @@ struct c_can_priv { unsigned int msg_obj_tx_first; unsigned int msg_obj_tx_last; u32 msg_obj_rx_mask; - atomic_t tx_active; atomic_t sie_pending; unsigned long tx_dir; int last_status; + struct c_can_tx_ring tx; u16 (*read_reg)(const struct c_can_priv *priv, enum reg index); void (*write_reg)(const struct c_can_priv *priv, enum reg index, u16 val); u32 (*read_reg32)(const struct c_can_priv *priv, enum reg index); @@ -219,4 +226,14 @@ int c_can_power_down(struct net_device *dev); void c_can_set_ethtool_ops(struct net_device *dev); +static inline u8 c_can_get_tx_head(const struct c_can_tx_ring *ring) +{ + return ring->head & (ring->obj_num - 1); +} + +static inline u8 c_can_get_tx_tail(const struct c_can_tx_ring *ring) +{ + return ring->tail & (ring->obj_num - 1); +} + #endif /* C_CAN_H */ diff --git a/drivers/net/can/c_can/c_can_main.c b/drivers/net/can/c_can/c_can_main.c index fec0e3416970..451ac9a9586a 100644 --- a/drivers/net/can/c_can/c_can_main.c +++ b/drivers/net/can/c_can/c_can_main.c @@ -427,24 +427,64 @@ static void c_can_setup_receive_object(struct net_device *dev, int iface, c_can_object_put(dev, iface, obj, IF_COMM_RCV_SETUP); } +static u8 c_can_get_tx_free(const struct c_can_tx_ring *ring) +{ + u8 head = c_can_get_tx_head(ring); + u8 tail = c_can_get_tx_tail(ring); + + /* This is not a FIFO. C/D_CAN sends out the buffers + * prioritized. The lowest buffer number wins. + */ + if (head < tail) + return 0; + + return ring->obj_num - head; +} + +static bool c_can_tx_busy(const struct c_can_priv *priv, + const struct c_can_tx_ring *tx_ring) +{ + if (c_can_get_tx_free(tx_ring) > 0) + return false; + + netif_stop_queue(priv->dev); + + /* Memory barrier before checking tx_free (head and tail) */ + smp_mb(); + + if (c_can_get_tx_free(tx_ring) == 0) { + netdev_dbg(priv->dev, + "Stopping tx-queue (tx_head=0x%08x, tx_tail=0x%08x, len=%d).\n", + tx_ring->head, tx_ring->tail, + tx_ring->head - tx_ring->tail); + return true; + } + + netif_start_queue(priv->dev); + return false; +} + static netdev_tx_t c_can_start_xmit(struct sk_buff *skb, struct net_device *dev) { struct can_frame *frame = (struct can_frame *)skb->data; struct c_can_priv *priv = netdev_priv(dev); + struct c_can_tx_ring *tx_ring = &priv->tx; u32 idx, obj; if (can_dropped_invalid_skb(dev, skb)) return NETDEV_TX_OK; - /* This is not a FIFO. C/D_CAN sends out the buffers - * prioritized. The lowest buffer number wins. - */ - idx = fls(atomic_read(&priv->tx_active)); - obj = idx + priv->msg_obj_tx_first; - /* If this is the last buffer, stop the xmit queue */ - if (idx == priv->msg_obj_tx_num - 1) + if (c_can_tx_busy(priv, tx_ring)) + return NETDEV_TX_BUSY; + + idx = c_can_get_tx_head(tx_ring); + tx_ring->head++; + if (c_can_get_tx_free(tx_ring) == 0) netif_stop_queue(dev); + + obj = idx + priv->msg_obj_tx_first; + /* Store the message in the interface so we can call * can_put_echo_skb(). We must do this before we enable * transmit as we might race against do_tx(). @@ -453,8 +493,6 @@ static netdev_tx_t c_can_start_xmit(struct sk_buff *skb, priv->dlc[idx] = frame->len; can_put_echo_skb(skb, dev, idx, 0); - /* Update the active bits */ - atomic_add(BIT(idx), &priv->tx_active); /* Start transmission */ c_can_object_put(dev, IF_TX, obj, IF_COMM_TX); @@ -567,6 +605,7 @@ static int c_can_software_reset(struct net_device *dev) static int c_can_chip_config(struct net_device *dev) { struct c_can_priv *priv = netdev_priv(dev); + struct c_can_tx_ring *tx_ring = &priv->tx; int err; err = c_can_software_reset(dev); @@ -598,7 +637,8 @@ static int c_can_chip_config(struct net_device *dev) priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED); /* Clear all internal status */ - atomic_set(&priv->tx_active, 0); + tx_ring->head = 0; + tx_ring->tail = 0; priv->tx_dir = 0; /* set bittiming params */ @@ -696,14 +736,14 @@ static int c_can_get_berr_counter(const struct net_device *dev, static void c_can_do_tx(struct net_device *dev) { struct c_can_priv *priv = netdev_priv(dev); + struct c_can_tx_ring *tx_ring = &priv->tx; struct net_device_stats *stats = &dev->stats; - u32 idx, obj, pkts = 0, bytes = 0, pend, clr; + u32 idx, obj, pkts = 0, bytes = 0, pend; if (priv->msg_obj_tx_last > 32) pend = priv->read_reg32(priv, C_CAN_INTPND3_REG); else pend = priv->read_reg(priv, C_CAN_INTPND2_REG); - clr = pend; while ((idx = ffs(pend))) { idx--; @@ -723,11 +763,14 @@ static void c_can_do_tx(struct net_device *dev) if (!pkts) return; - /* Clear the bits in the tx_active mask */ - atomic_sub(clr, &priv->tx_active); - - if (clr & BIT(priv->msg_obj_tx_num - 1)) - netif_wake_queue(dev); + tx_ring->tail += pkts; + if (c_can_get_tx_free(tx_ring)) { + /* Make sure that anybody stopping the queue after + * this sees the new tx_ring->tail. + */ + smp_mb(); + netif_wake_queue(priv->dev); + } stats->tx_bytes += bytes; stats->tx_packets += pkts; @@ -1206,6 +1249,10 @@ struct net_device *alloc_c_can_dev(int msg_obj_num) priv->msg_obj_tx_last = priv->msg_obj_tx_first + priv->msg_obj_tx_num - 1; + priv->tx.head = 0; + priv->tx.tail = 0; + priv->tx.obj_num = msg_obj_tx_num; + netif_napi_add(dev, &priv->napi, c_can_poll, priv->msg_obj_rx_num); priv->dev = dev; From patchwork Fri Jul 16 16:56:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 479565 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 67E19C12002 for ; Fri, 16 Jul 2021 16:58:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 52743613EB for ; Fri, 16 Jul 2021 16:58:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231205AbhGPRA4 (ORCPT ); Fri, 16 Jul 2021 13:00:56 -0400 Received: from smtp-33-i2.italiaonline.it ([213.209.12.33]:43699 "EHLO libero.it" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230191AbhGPRAy (ORCPT ); Fri, 16 Jul 2021 13:00:54 -0400 Received: from passgat-Modern-14-A10M.homenet.telecomitalia.it ([79.54.92.92]) by smtp-33.iol.local with ESMTPA id 4R8tmKNNmS6GM4R91mO7bu; Fri, 16 Jul 2021 18:56:59 +0200 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=libero.it; s=s2021; t=1626454619; bh=KWD/wzdaPvT0fsYgJVYpslUqQQ4hDFaggJS+tSzFXM8=; h=From; b=M5p9z9KnI4apX914fitNQ6VTd5iC/2725NNtm0KExszlYW/EtJ/Jg/3bFgeBBGGTb ROajGgDwxxtrR3NDGzTtroByIS/GkxkbRc1RB5+un0hyD/1Xgfy1rFxu9cCf9hUIro URIcibzf96D9iFb5OlP7y015nwBKVAjlhhBVFPXvTHBCOLgsFqA29KWD8Z9KATBSfA gj/Z14yuJT8/CVeAPKMFLl3M0c01We7eyQ0EM7w0UhoT9uOALFzU+S7H61KWd5SbXA hKEiXjluPAu6CclG+1OVqfxXiHb7qW5oRgGCYWzeRHti/WE4uOh8WD4wrE6xJFSIAW l9L0eZrSrg/IQ== X-CNFS-Analysis: v=2.4 cv=AcF0o1bG c=1 sm=1 tr=0 ts=60f1ba5b cx=a_exe a=eKwsI+FXzXP/Nc4oRbpalQ==:117 a=eKwsI+FXzXP/Nc4oRbpalQ==:17 a=ci6mTl68RD0LDj9SGHYA:9 a=zPaA-w0dnRgsZjSy:21 a=aeJ6SaH8bT6bZ-aJ:21 From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Gianluca Falavigna , Dario Binacchi , Andrew Lunn , "David S. Miller" , Jakub Kicinski , Marc Kleine-Budde , Oliver Hartkopp , Vincent Mailhol , Wolfgang Grandegger , linux-can@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH 4/4] can: c_can: cache frames to operate as a true FIFO Date: Fri, 16 Jul 2021 18:56:23 +0200 Message-Id: <20210716165623.19677-5-dariobin@libero.it> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210716165623.19677-1-dariobin@libero.it> References: <20210716165623.19677-1-dariobin@libero.it> X-CMAE-Envelope: MS4xfJwJslVA2cb60Nx41WhPUMCgwwFuMvrWlgOPnh7OBeEkiIb5GpdEvI4dVbdUaD3iUexMwIQ+3DDGlWch5+pFyKLUTcctcoU0N9eU6WYtl26gBxorS/6j ZZhsuH8DU+qEQR37f9emL2YbBfAEplXbJsyWpFoe2VmTFM/6uOEIBxWv19SL67bBJ+dEiQTu3hCzbPv5Ycg8r7Qvy2W4F5/ITgsTrSLOSqQG/XvwzAMztK7I 7NI+8ZjAP7QVdVOAt8MYUT6Tv1MDKp1s109ef9SCNAvTpX1RG0TabzqNdTHq9K9JmHu83XiQxfGbyQv40N24mQ4U4owIXntQOdmXtLoiKAYT9uqhO6QbwMQG uMLdQCKk0EvI6HznNJYIyrMAhVCMKKuGQKYMOG1sDFU5jnQIjYP/4xgbPB4tM0FFKv9A2JTBBgPnn1eQH+G1bLmw9Bn9BQb5tRjB/V2SKjG0htVAuVF9A2YU nm9Oepz55WPIHc19XR6mvXb+GqLvikWf03Hf6w7Chu4eYxPBK6ii0m8cVXBGAcUZMwPk3Snl1W5cJ+g1d4q34V2vj2zxCR31lF0myycRze1YsTsvG/S3Wpmj ccw= Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org As reported by a comment in the c_can_start_xmit() this was not a FIFO. C/D_CAN controller sends out the buffers prioritized so that the lowest buffer number wins. What did c_can_start_xmit() do if head was less tail in the tx ring ? It waited until all the frames queued in the FIFO was actually transmitted by the controller before accepting a new CAN frame to transmit, even if the FIFO was not full, to ensure that the messages were transmitted in the order in which they were loaded. By storing the frames in the FIFO without requiring its transmission, we will be able to use the full size of the FIFO even in cases such as the one described above. The transmission interrupt will trigger their transmission only when all the messages previously loaded but stored in less priority positions of the buffers have been transmitted. Suggested-by: Gianluca Falavigna Signed-off-by: Dario Binacchi --- drivers/net/can/c_can/c_can.h | 6 +++++ drivers/net/can/c_can/c_can_main.c | 42 +++++++++++++++++------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/drivers/net/can/c_can/c_can.h b/drivers/net/can/c_can/c_can.h index 8fe7e2138620..fc499a70b797 100644 --- a/drivers/net/can/c_can/c_can.h +++ b/drivers/net/can/c_can/c_can.h @@ -200,6 +200,7 @@ struct c_can_priv { atomic_t sie_pending; unsigned long tx_dir; int last_status; + spinlock_t tx_lock; struct c_can_tx_ring tx; u16 (*read_reg)(const struct c_can_priv *priv, enum reg index); void (*write_reg)(const struct c_can_priv *priv, enum reg index, u16 val); @@ -236,4 +237,9 @@ static inline u8 c_can_get_tx_tail(const struct c_can_tx_ring *ring) return ring->tail & (ring->obj_num - 1); } +static inline u8 c_can_get_tx_free(const struct c_can_tx_ring *ring) +{ + return ring->obj_num - (ring->head - ring->tail); +} + #endif /* C_CAN_H */ diff --git a/drivers/net/can/c_can/c_can_main.c b/drivers/net/can/c_can/c_can_main.c index 451ac9a9586a..4c061fef002c 100644 --- a/drivers/net/can/c_can/c_can_main.c +++ b/drivers/net/can/c_can/c_can_main.c @@ -427,20 +427,6 @@ static void c_can_setup_receive_object(struct net_device *dev, int iface, c_can_object_put(dev, iface, obj, IF_COMM_RCV_SETUP); } -static u8 c_can_get_tx_free(const struct c_can_tx_ring *ring) -{ - u8 head = c_can_get_tx_head(ring); - u8 tail = c_can_get_tx_tail(ring); - - /* This is not a FIFO. C/D_CAN sends out the buffers - * prioritized. The lowest buffer number wins. - */ - if (head < tail) - return 0; - - return ring->obj_num - head; -} - static bool c_can_tx_busy(const struct c_can_priv *priv, const struct c_can_tx_ring *tx_ring) { @@ -470,7 +456,7 @@ static netdev_tx_t c_can_start_xmit(struct sk_buff *skb, struct can_frame *frame = (struct can_frame *)skb->data; struct c_can_priv *priv = netdev_priv(dev); struct c_can_tx_ring *tx_ring = &priv->tx; - u32 idx, obj; + u32 idx, obj, cmd = IF_COMM_TX; if (can_dropped_invalid_skb(dev, skb)) return NETDEV_TX_OK; @@ -483,7 +469,11 @@ static netdev_tx_t c_can_start_xmit(struct sk_buff *skb, if (c_can_get_tx_free(tx_ring) == 0) netif_stop_queue(dev); - obj = idx + priv->msg_obj_tx_first; + spin_lock_bh(&priv->tx_lock); + if (idx < c_can_get_tx_tail(tx_ring)) + cmd &= ~IF_COMM_TXRQST; /* Cache the message */ + else + spin_unlock_bh(&priv->tx_lock); /* Store the message in the interface so we can call * can_put_echo_skb(). We must do this before we enable @@ -492,9 +482,11 @@ static netdev_tx_t c_can_start_xmit(struct sk_buff *skb, c_can_setup_tx_object(dev, IF_TX, frame, idx); priv->dlc[idx] = frame->len; can_put_echo_skb(skb, dev, idx, 0); + obj = idx + priv->msg_obj_tx_first; + c_can_object_put(dev, IF_TX, obj, cmd); - /* Start transmission */ - c_can_object_put(dev, IF_TX, obj, IF_COMM_TX); + if (spin_is_locked(&priv->tx_lock)) + spin_unlock_bh(&priv->tx_lock); return NETDEV_TX_OK; } @@ -739,6 +731,7 @@ static void c_can_do_tx(struct net_device *dev) struct c_can_tx_ring *tx_ring = &priv->tx; struct net_device_stats *stats = &dev->stats; u32 idx, obj, pkts = 0, bytes = 0, pend; + u8 tail; if (priv->msg_obj_tx_last > 32) pend = priv->read_reg32(priv, C_CAN_INTPND3_REG); @@ -775,6 +768,18 @@ static void c_can_do_tx(struct net_device *dev) stats->tx_bytes += bytes; stats->tx_packets += pkts; can_led_event(dev, CAN_LED_EVENT_TX); + + tail = c_can_get_tx_tail(tx_ring); + + if (tail == 0) { + u8 head = c_can_get_tx_head(tx_ring); + + /* Start transmission for all cached messages */ + for (idx = tail; idx < head; idx++) { + obj = idx + priv->msg_obj_tx_first; + c_can_object_put(dev, IF_TX, obj, IF_COMM_TXRQST); + } + } } /* If we have a gap in the pending bits, that means we either @@ -1237,6 +1242,7 @@ struct net_device *alloc_c_can_dev(int msg_obj_num) return NULL; priv = netdev_priv(dev); + spin_lock_init(&priv->tx_lock); priv->msg_obj_num = msg_obj_num; priv->msg_obj_rx_num = msg_obj_num - msg_obj_tx_num; priv->msg_obj_rx_first = 1;