From patchwork Sun Jun 6 20:17:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 12302291 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 X-Spam-Level: X-Spam-Status: No, score=-15.8 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 AD0CEC4743F for ; Sun, 6 Jun 2021 20:17:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9301D613D4 for ; Sun, 6 Jun 2021 20:17:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230105AbhFFUTM (ORCPT ); Sun, 6 Jun 2021 16:19:12 -0400 Received: from smtp-35.italiaonline.it ([213.209.10.35]:49209 "EHLO libero.it" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230081AbhFFUTL (ORCPT ); Sun, 6 Jun 2021 16:19:11 -0400 Received: from passgat-Modern-14-A10M.homenet.telecomitalia.it ([79.17.119.101]) by smtp-35.iol.local with ESMTPA id pzCol3UwhsptipzCxlruIj; Sun, 06 Jun 2021 22:17:19 +0200 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=libero.it; s=s2021; t=1623010639; bh=iaj/7gfRZLK4M6U1KtJSY7aMtir2BnviUeoSzGrV5jA=; h=From; b=UYKlsRzAVHy2Xtgd5656aE1ELI/DGKo6fgndfGA6dFJgyKtVpkUt9A3PsNIacEYbZ 2/6X5a/PhNbN+3zwOlYst+FZm2DdRWcZW2NP82EtNCFe0FHj6L3w9ZUq7Tn/8keto3 IzbAGs1m5pEsnSfYzMEGi1fU2PokGaLSHz90pdh0fMyOKgUVwqj6x4q3Ls+ZcaQgKx ANzHbx75LAOm5M5io0xtEax3lNCTUSGKVCSsNq84wsIfKN+Lncsm6VjhEvPzuEJPUO eKIrK2py7EOSKX1Z1hp+Uto0XMIVblm2nppOwbman/dSAKV55ZRkIwkJNggucJ/thW uKbSrSheekrKw== X-CNFS-Analysis: v=2.4 cv=Bo1Yfab5 c=1 sm=1 tr=0 ts=60bd2d4f cx=a_exe a=do1bHx4A/kh2kuTIUQHSxQ==:117 a=do1bHx4A/kh2kuTIUQHSxQ==: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 , Tong Zhang , Vincent Mailhol , Wolfgang Grandegger , YueHaibing , Zhang Qilong , linux-can@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH 1/3] can: c_can: exit c_can_do_tx() early if no frames have been sent Date: Sun, 6 Jun 2021 22:17:03 +0200 Message-Id: <20210606201705.31307-2-dariobin@libero.it> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210606201705.31307-1-dariobin@libero.it> References: <20210606201705.31307-1-dariobin@libero.it> X-CMAE-Envelope: MS4xfEigeVgfGzfYsUw+nlXrrTI+4nn6l8rAIFrAow8hFZtgZEyocdbXmzmVZhAN00WjUvaxQhlq1NwZ05MpZyeLxncHQSMZJDbR4AQvx0wgtGyzdGOwUy8/ s72raGwdPeHj/t7onlgGMpdB2l5uSi7g340Bh0tNIckR/jcvITMm3rreWuu/dSGXqpjz5YB/722dUY98+wbr8v0yzwDwHO4aD86ZR87xxv9/ajUGWeFQUDcY rXmREEjnnJgww8WWd0PTpn5S0jHM1Mdr/0S9AZicegv4djlQJjuTvQpE0KK75xYTuYOh2MhiC9GPg4b2V8UHLr2E1jXusxAR1Ph76FKr2InNKF6UqBI2yM7a 0p2iTiml6uy4kRHZdyuCxu/T2330iiD7EO56h1lzK6ZwvO6Bjun1o32SWvXuO6hI7ZHeg0EF8+DfJsIshyJODvnM+o2Va2q8bc0qnwImq3sMEvzZ+ANaf9uA 5OKG/6kNq9KVmHuERYAwo5AA8Uj/sgNJ2UtiruNyJ2FJO9JiCMxQ7DuXnMnsH8EDM0BSK/wYpHXITT4KKZdPhVjdElsUrIrsExGP52yD8jUX+idUVHrQC6m9 EbOV3d31cA/W8147xLWp7EvmPJ8LCq07ZqEdj0gXis6b+SwOYxJ3aCrlp74KBQbwh6w8XzNyyyGtmvrCE77ETWis Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@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.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/drivers/net/can/c_can/c_can.c b/drivers/net/can/c_can/c_can.c index 313793f6922d..2b203bf004f9 100644 --- a/drivers/net/can/c_can/c_can.c +++ b/drivers/net/can/c_can/c_can.c @@ -721,17 +721,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 Sun Jun 6 20:17:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 12302295 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 X-Spam-Level: X-Spam-Status: No, score=-15.8 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=unavailable 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 2771FC4743E for ; Sun, 6 Jun 2021 20:18:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0F5036142B for ; Sun, 6 Jun 2021 20:18:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230242AbhFFUUW (ORCPT ); Sun, 6 Jun 2021 16:20:22 -0400 Received: from smtp-35.italiaonline.it ([213.209.10.35]:47242 "EHLO libero.it" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230255AbhFFUUL (ORCPT ); Sun, 6 Jun 2021 16:20:11 -0400 Received: from passgat-Modern-14-A10M.homenet.telecomitalia.it ([79.17.119.101]) by smtp-35.iol.local with ESMTPA id pzCol3UwhsptipzCylruJ2; Sun, 06 Jun 2021 22:17:20 +0200 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=libero.it; s=s2021; t=1623010640; bh=yM4Ecf6jM2pkrfTHiQuLryX7gOIQJGU67Jex3g2HI04=; h=From; b=NG8XhtZuTm4k6RfmioIijcOeRbhLRpeL7bsHOaUM/JzyDdUZRgDLaXwSbbm0pz2s8 JcGFfmjn1nygwhNkhk9Hrpoks29eqz2Gi2gVneWw3JeYJtXS7xJ1ZGR+cU8kR0Qp7W 368eqCBeEH4H+2dWjqrK5yY2YQcT02ZmAX2EMY7PItE80g/Ehr/OwTE/qnnMy+ub46 ndV8bq801L4PF/8VgLgzxfDa541PbYMo3CokLTpOsXhBmbA073hjXdt8jZJx5B9LIJ lzEqZo8Lq3JnzMCmf7XXNW2F/dXsU+f47MWIAnwq0Rnf0oA55+ElnT60tRcFImdJSC nDoHw85dmHdbQ== X-CNFS-Analysis: v=2.4 cv=Bo1Yfab5 c=1 sm=1 tr=0 ts=60bd2d50 cx=a_exe a=do1bHx4A/kh2kuTIUQHSxQ==:117 a=do1bHx4A/kh2kuTIUQHSxQ==:17 a=VwQbUJbxAAAA:8 a=83yiEfbaggHBMMd4CMwA:9 a=qO9HUs-gi3uPu0iE:21 a=Xo9eem3BNHsY2aya:21 a=AjGcO6oz07-iQ99wixmX:22 From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Gianluca Falavigna , Dario Binacchi , "David S. Miller" , Jakub Kicinski , Marc Kleine-Budde , Oliver Hartkopp , Tong Zhang , Vincent Mailhol , Wolfgang Grandegger , YueHaibing , Zhang Qilong , linux-can@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH 2/3] can: c_can: support tx ring algorithm Date: Sun, 6 Jun 2021 22:17:04 +0200 Message-Id: <20210606201705.31307-3-dariobin@libero.it> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210606201705.31307-1-dariobin@libero.it> References: <20210606201705.31307-1-dariobin@libero.it> X-CMAE-Envelope: MS4xfDchYpEkYbK8rqUL14JdCOhe/YrJik2yHYair6L2hhefcZ/sqASMaDsTwpu4dVlpaipewTuvRXjwkHD7R0r1i9F2E2wMT7q8nQF2pnRXBc63Pg0ENT/u J+YxX9UqzKstWQVBhmAcl5kBa2eGDTlZ9ffLvsCo9F2VpWCfbIv0hY8BlpHyhy7HQ1ytY9oVnDH3MFKMuBWWxYxgCumehys8VFGntfS+CF0r592W/VnkofYf tIIDtxTtbju6mXHdzrGNfgiurzMW6vH2k+DKGTUaMFr5bPu4BTxDCO4hgwtFVcY5/GAWZvq9q222mv4B+gzOq4R8Rsakd3VpFTkqn+34Az+TNdDnegcU2ZRH uFXc7mafznsOeJo0nzdq3WsreVN8hUNxcsc6Eg+142XtiYt7q6RS7ny0CesmJJLK2jCdoYv6be2debes9FXtKz2KovvuQWX47Fbp685q50kLSYhVRpHz6dyj wpfViziNLZvjkoP6wSmRxe68eIdxJt3jSyCBatxVY2iNi41QRu3rf2bLfSgM5sajywhPmPYJMeVuI75GGnsfMUbeCGMW7anzB2d7BDvQqw6THvYwBWc81U6g D4HTAxR3sAPfUNCfgD2mI5PzAT6++G3axCiAsbqK/Gq8PsCg+Mg6+w2GLNxNHmg03PnfetPB1EymlLUtL9Ba5CyL Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@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.c | 81 +++++++++++++++++++++++++++-------- drivers/net/can/c_can/c_can.h | 19 +++++++- 2 files changed, 82 insertions(+), 18 deletions(-) diff --git a/drivers/net/can/c_can/c_can.c b/drivers/net/can/c_can/c_can.c index 2b203bf004f9..0548485f522d 100644 --- a/drivers/net/can/c_can/c_can.c +++ b/drivers/net/can/c_can/c_can.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->rxmasked = 0; priv->tx_dir = 0; @@ -697,14 +737,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--; @@ -724,11 +764,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; @@ -1207,6 +1250,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; diff --git a/drivers/net/can/c_can/c_can.h b/drivers/net/can/c_can/c_can.h index 06045f610f0e..c72cb6a7fd37 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_up(struct net_device *dev); int c_can_power_down(struct net_device *dev); #endif +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 */ From patchwork Sun Jun 6 20:17:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 12302297 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 X-Spam-Level: X-Spam-Status: No, score=-15.8 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=unavailable 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 0FF05C4743F for ; Sun, 6 Jun 2021 20:18:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E9D1C613D4 for ; Sun, 6 Jun 2021 20:18:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230264AbhFFUUY (ORCPT ); Sun, 6 Jun 2021 16:20:24 -0400 Received: from smtp-35.italiaonline.it ([213.209.10.35]:51641 "EHLO libero.it" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230230AbhFFUUM (ORCPT ); Sun, 6 Jun 2021 16:20:12 -0400 Received: from passgat-Modern-14-A10M.homenet.telecomitalia.it ([79.17.119.101]) by smtp-35.iol.local with ESMTPA id pzCol3UwhsptipzCylruJJ; Sun, 06 Jun 2021 22:17:21 +0200 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=libero.it; s=s2021; t=1623010641; bh=jgd8yWy9ycdnS8FdZkmaUBTnv8/MoVE/T54CaBIExOE=; h=From; b=STQrR5ZofOrxKk16qXif3sknCfHvhRYnqPAL8GjRj4LTvV8U9ZnaUpTHcVOmWbeL2 8XWkDsmY7uneYCl/YKgtZj6YYvz8+qr7lDiY8zeL3H6tbKQLBFO2rVhAMcKeY2wTi7 FciCA7FeSdAs2wuVatM3xGVNR2cV97a/PQgZInq+4Tjd5CG/oB1jEpCLAXyIkbxnXv Y5cxWuSIpRuVBbSCawhg7cqiDlPdJxctsHkOj4tIB9zcNLvt9nYoWl0OEK5/X9LlFt QNJngXg4wJCQBlUu4uyBYQobBvSbTvw5p2CiiV3aU/o9HqX5Dt0/v7hno8Gw8z+bKV MAbI2o0dmGtcg== X-CNFS-Analysis: v=2.4 cv=Bo1Yfab5 c=1 sm=1 tr=0 ts=60bd2d51 cx=a_exe a=do1bHx4A/kh2kuTIUQHSxQ==:117 a=do1bHx4A/kh2kuTIUQHSxQ==:17 a=ci6mTl68RD0LDj9SGHYA:9 a=kqXjr2oOka_tHwkt:21 a=DlUeu6Gs4-n7kmor:21 From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Gianluca Falavigna , Dario Binacchi , "David S. Miller" , Jakub Kicinski , Marc Kleine-Budde , Oliver Hartkopp , Tong Zhang , Vincent Mailhol , Wolfgang Grandegger , YueHaibing , Zhang Qilong , linux-can@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH 3/3] can: c_can: cache frames to operate as a true FIFO Date: Sun, 6 Jun 2021 22:17:05 +0200 Message-Id: <20210606201705.31307-4-dariobin@libero.it> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210606201705.31307-1-dariobin@libero.it> References: <20210606201705.31307-1-dariobin@libero.it> X-CMAE-Envelope: MS4xfKYlXNfsIY8XQo0/VZtZ/jVxW1tFYPIL6EqmoOTPNSosmOeyx+bjOl4Al3lvymNWw0aANeu3su6pOrK/OXC4tIFiym8onyMgWzQzKcUiMp9xDBaIpOnr TJWpLTZJXJSVOYrySNBKi2NGWHAhGOVgx+gR9Vnu9OMQKRy9uP/TXWQXYB4OzVrE9wu6Lip0i/bGQ75DWkgM9G4AnIhQaC4X4hsSbZtBSzKsv5kzp4cqDtMi YKuoyAlAQukM4821j9bHcixVko/36aS6SNI9p1s/Mre/paLDQCkhPL0L++a6L1D77Tbrqb2oVB+dbLzCKeVxOfMMc+wjNaQvKO4fQdzVvJPnTkJACybGMzYd 15KGSg8eTSa0lTOHS9sbeduWd9KtyUbSbIw5p5D8SPX9Z/FIZcQmttaXhxmhkiOK5uvDSc+/qXhU+68QyMAccHOISsjPy91Qj1BCZQmjEmE50hn9B5nIIfk0 Jf+zssq06cqI4iTIz68EJRWXtTQ+4YI2ykgQBoAXLWTGebEXcvU4I9by675IAM+I8oHcplE8222zvFyODbODtt1QrH0l/WTrbYYRfdS6lspSqV+OaI5pWvbd hSaC2qqKNCnbkouEwQxIwyq38qDeFZyaH2l2nHilPCH4Y4iYBrhD+34bgXMCaYsuWIB4ftVQQiUkQFfuGfHYVHtt Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@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.c | 42 ++++++++++++++++++++--------------- drivers/net/can/c_can/c_can.h | 6 +++++ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/drivers/net/can/c_can/c_can.c b/drivers/net/can/c_can/c_can.c index 0548485f522d..9b809ea61094 100644 --- a/drivers/net/can/c_can/c_can.c +++ b/drivers/net/can/c_can/c_can.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; } @@ -740,6 +732,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); @@ -776,6 +769,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 @@ -1238,6 +1243,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; diff --git a/drivers/net/can/c_can/c_can.h b/drivers/net/can/c_can/c_can.h index c72cb6a7fd37..520daa77f876 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 */