linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Marc Kleine-Budde <mkl@pengutronix.de>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org,
	kernel@pengutronix.de, Dario Binacchi <dariobin@libero.it>,
	Gianluca Falavigna <gianluca.falavigna@inwind.it>,
	Marc Kleine-Budde <mkl@pengutronix.de>
Subject: [PATCH net-next 22/22] can: c_can: cache frames to operate as a true FIFO
Date: Thu, 19 Aug 2021 15:39:13 +0200	[thread overview]
Message-ID: <20210819133913.657715-23-mkl@pengutronix.de> (raw)
In-Reply-To: <20210819133913.657715-1-mkl@pengutronix.de>

From: Dario Binacchi <dariobin@libero.it>

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.

Link: https://lore.kernel.org/r/20210807130800.5246-5-dariobin@libero.it
Suggested-by: Gianluca Falavigna <gianluca.falavigna@inwind.it>
Signed-off-by: Dario Binacchi <dariobin@libero.it>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/net/can/c_can/c_can.h      | 11 +----------
 drivers/net/can/c_can/c_can_main.c | 23 ++++++++++++++++++-----
 2 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/drivers/net/can/c_can/c_can.h b/drivers/net/can/c_can/c_can.h
index 9b4e54c950a6..08b6efa7a1a7 100644
--- a/drivers/net/can/c_can/c_can.h
+++ b/drivers/net/can/c_can/c_can.h
@@ -238,16 +238,7 @@ static inline u8 c_can_get_tx_tail(const struct c_can_tx_ring *ring)
 
 static inline 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;
+	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 eb324fffab09..52671d1ea17d 100644
--- a/drivers/net/can/c_can/c_can_main.c
+++ b/drivers/net/can/c_can/c_can_main.c
@@ -456,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;
@@ -469,7 +469,8 @@ 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;
+	if (idx < c_can_get_tx_tail(tx_ring))
+		cmd &= ~IF_COMM_TXRQST; /* Cache the message */
 
 	/* Store the message in the interface so we can call
 	 * can_put_echo_skb(). We must do this before we enable
@@ -478,9 +479,8 @@ 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);
-
-	/* Start transmission */
-	c_can_object_put(dev, IF_TX, obj, IF_COMM_TX);
+	obj = idx + priv->msg_obj_tx_first;
+	c_can_object_put(dev, IF_TX, obj, cmd);
 
 	return NETDEV_TX_OK;
 }
@@ -725,6 +725,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);
@@ -761,6 +762,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_NAPI, obj, IF_COMM_TXRQST);
+		}
+	}
 }
 
 /* If we have a gap in the pending bits, that means we either
-- 
2.32.0



  parent reply	other threads:[~2021-08-19 13:40 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-19 13:38 pull-request: can-next 2021-08-19 Marc Kleine-Budde
2021-08-19 13:38 ` [PATCH net-next 01/22] mailmap: update email address of Matthias Fuchs and Thomas Körper Marc Kleine-Budde
2021-08-19 13:38 ` [PATCH net-next 02/22] dt-bindings: can-controller: add support for termination-gpios Marc Kleine-Budde
2021-08-19 13:38 ` [PATCH net-next 03/22] dt-bindings: can: fsl,flexcan: enable termination-* bindings Marc Kleine-Budde
2021-08-19 13:38 ` [PATCH net-next 04/22] can: dev: provide optional GPIO based termination support Marc Kleine-Budde
2021-08-19 13:38 ` [PATCH net-next 05/22] can: netlink: allow user to turn off unsupported features Marc Kleine-Budde
2021-08-19 13:38 ` [PATCH net-next 06/22] MAINTAINERS: add Vincent MAILHOL as maintainer for the ETAS ES58X CAN/USB driver Marc Kleine-Budde
2021-08-19 13:38 ` [PATCH net-next 07/22] can: etas_es58x: clean-up documentation of struct es58x_fd_tx_conf_msg Marc Kleine-Budde
2021-08-19 13:38 ` [PATCH net-next 08/22] can: mcp251xfd: mark some instances of struct mcp251xfd_priv as const Marc Kleine-Budde
2021-08-19 13:39 ` [PATCH net-next 09/22] dt-bindings: net: can: renesas,rcar-canfd: Document RZ/G2L SoC Marc Kleine-Budde
2021-08-19 13:39 ` [PATCH net-next 10/22] can: rcar_canfd: Add support for RZ/G2L family Marc Kleine-Budde
2021-08-19 13:39 ` [PATCH net-next 11/22] can: tcan4x5x: cdev_to_priv(): remove stray empty line Marc Kleine-Budde
2021-08-19 13:39 ` [PATCH net-next 12/22] can: m_can: fix block comment style Marc Kleine-Budde
2021-08-19 13:39 ` [PATCH net-next 13/22] can: m_can: Disable IRQs on FIFO bus errors Marc Kleine-Budde
2021-08-19 13:39 ` [PATCH net-next 14/22] can: m_can: Batch FIFO reads during CAN receive Marc Kleine-Budde
2021-08-19 13:39 ` [PATCH net-next 15/22] can: m_can: Batch FIFO writes during CAN transmit Marc Kleine-Budde
2021-08-19 13:39 ` [PATCH net-next 16/22] dt-bindings: net: can: c_can: convert to json-schema Marc Kleine-Budde
2021-08-19 13:39 ` [PATCH net-next 17/22] can: c_can: c_can_do_tx(): fix typo in comment Marc Kleine-Budde
2021-08-19 13:39 ` [PATCH net-next 18/22] can: c_can: rename IF_RX -> IF_NAPI Marc Kleine-Budde
2021-08-19 13:39 ` [PATCH net-next 19/22] can: c_can: remove struct c_can_priv::priv field Marc Kleine-Budde
2021-08-19 13:39 ` [PATCH net-next 20/22] can: c_can: exit c_can_do_tx() early if no frames have been sent Marc Kleine-Budde
2021-08-19 13:39 ` [PATCH net-next 21/22] can: c_can: support tx ring algorithm Marc Kleine-Budde
2021-08-19 13:39 ` Marc Kleine-Budde [this message]
2021-08-19 19:00 ` pull-request: can-next 2021-08-19 patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210819133913.657715-23-mkl@pengutronix.de \
    --to=mkl@pengutronix.de \
    --cc=dariobin@libero.it \
    --cc=davem@davemloft.net \
    --cc=gianluca.falavigna@inwind.it \
    --cc=kernel@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).