public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH v2 0/6] can: dev: add generic function can_update_bus_error_stats()
@ 2024-10-29  8:44 Dario Binacchi
  2024-10-29  8:44 ` [RFC PATCH v2 1/6] " Dario Binacchi
                   ` (5 more replies)
  0 siblings, 6 replies; 9+ messages in thread
From: Dario Binacchi @ 2024-10-29  8:44 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-amarula, Dario Binacchi, Alexandra Winter, Andrew Lunn,
	David S. Miller, Eric Dumazet, Frank Li, Gal Pressman, Haibo Chen,
	Han Xu, Jakub Kicinski, Kory Maincent, Marc Kleine-Budde,
	Paolo Abeni, Rob Herring, Sabrina Dubroca, Shannon Nelson,
	Simon Horman, Uwe Kleine-König, Vincent Mailhol, linux-can,
	netdev

This series originates from some tests I ran on a CAN communication for
one of my clients that reports sporadic errors. After enabling BERR
reporting, I was surprised that the command:

ip -details -statistics link show can0

did not display the occurrence of different types of errors, but only the
generic ones for reception and transmission. In trying to export this
information, I felt that the code related to managing statistics and handling
CAN errors (CRC, STUF, BIT, ACK, and FORM) was quite duplicated in the
implementation of various drivers, and there wasn't a generic function like
in the case of state changes (i. e. can_change_state). This led to the idea
of adding can_update_bus_error_stats() and the helpers for setting up the
CAN error frame.

Regarding patch 5/6 ("can: netlink: extend stats to the error types (ack,
CRC, form, ..."), I ran

./scripts/check-uapi.sh

which found

"error - 1/934 UAPI headers compatible with x86 appear _not_ to be backwards
compatible."

I included it in the series because I am currently interested in understanding
whether the idea behind each of the submitted patches makes sense, and I can
adjust them later if the response is positive, following your suggestions.

Changes in v2:
- Replace macros with static inline functions
- Update the commit message
- Replace the macros with static inline funcions calls.
- Update the commit message

Dario Binacchi (6):
  can: dev: add generic function can_update_bus_error_stats()
  can: flexcan: use can_update_bus_error_stats()
  can: dev: add helpers to setup an error frame
  can: flexcan: use helpers to setup the error frame
  can: netlink: extend stats to the error types (ack, CRC, form, ...)
  can: dev: update the error types stats (ack, CRC, form, ...)

 drivers/net/can/dev/dev.c              | 45 ++++++++++++++++++++++++
 drivers/net/can/flexcan/flexcan-core.c | 29 +++++-----------
 include/linux/can/dev.h                | 47 ++++++++++++++++++++++++++
 include/uapi/linux/can/netlink.h       |  6 ++++
 4 files changed, 106 insertions(+), 21 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [RFC PATCH v2 1/6] can: dev: add generic function can_update_bus_error_stats()
  2024-10-29  8:44 [RFC PATCH v2 0/6] can: dev: add generic function can_update_bus_error_stats() Dario Binacchi
@ 2024-10-29  8:44 ` Dario Binacchi
  2024-10-29  8:49   ` Marc Kleine-Budde
  2024-10-29  8:44 ` [RFC PATCH v2 2/6] can: flexcan: use can_update_bus_error_stats() Dario Binacchi
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 9+ messages in thread
From: Dario Binacchi @ 2024-10-29  8:44 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-amarula, Dario Binacchi, Andrew Lunn, David S. Miller,
	Eric Dumazet, Gal Pressman, Jakub Kicinski, Kory Maincent,
	Marc Kleine-Budde, Paolo Abeni, Sabrina Dubroca, Shannon Nelson,
	Simon Horman, Vincent Mailhol, linux-can, netdev

The function aims to generalize the statistics update by centralizing
the related code, thus avoiding code duplication.

Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
---

(no changes since v1)

 drivers/net/can/dev/dev.c | 30 ++++++++++++++++++++++++++++++
 include/linux/can/dev.h   |  1 +
 2 files changed, 31 insertions(+)

diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 6792c14fd7eb..0a3b1aad405b 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -16,6 +16,36 @@
 #include <linux/gpio/consumer.h>
 #include <linux/of.h>
 
+void can_update_bus_error_stats(struct net_device *dev, struct can_frame *cf)
+{
+	struct can_priv *priv = netdev_priv(dev);
+	bool rx_errors = false, tx_errors = false;
+
+	if (!cf || !(cf->can_id & (CAN_ERR_PROT | CAN_ERR_BUSERROR)))
+		return;
+
+	priv = netdev_priv(dev);
+	priv->can_stats.bus_error++;
+
+	if ((cf->can_id & CAN_ERR_ACK) && cf->data[3] == CAN_ERR_PROT_LOC_ACK)
+		tx_errors = true;
+	else if (cf->data[2] & (CAN_ERR_PROT_BIT1 | CAN_ERR_PROT_BIT0))
+		tx_errors = true;
+
+	if (cf->data[2] & (CAN_ERR_PROT_FORM | CAN_ERR_PROT_STUFF))
+		rx_errors = true;
+	else if ((cf->data[2] & CAN_ERR_PROT_BIT) &&
+		 (cf->data[3] == CAN_ERR_PROT_LOC_CRC_SEQ))
+		rx_errors = true;
+
+	if (rx_errors)
+		dev->stats.rx_errors++;
+
+	if (tx_errors)
+		dev->stats.tx_errors++;
+}
+EXPORT_SYMBOL_GPL(can_update_bus_error_stats);
+
 static void can_update_state_error_stats(struct net_device *dev,
 					 enum can_state new_state)
 {
diff --git a/include/linux/can/dev.h b/include/linux/can/dev.h
index 23492213ea35..0977656b366d 100644
--- a/include/linux/can/dev.h
+++ b/include/linux/can/dev.h
@@ -201,6 +201,7 @@ void can_state_get_by_berr_counter(const struct net_device *dev,
 				   enum can_state *rx_state);
 void can_change_state(struct net_device *dev, struct can_frame *cf,
 		      enum can_state tx_state, enum can_state rx_state);
+void can_update_bus_error_stats(struct net_device *dev, struct can_frame *cf);
 
 #ifdef CONFIG_OF
 void of_can_transceiver(struct net_device *dev);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [RFC PATCH v2 2/6] can: flexcan: use can_update_bus_error_stats()
  2024-10-29  8:44 [RFC PATCH v2 0/6] can: dev: add generic function can_update_bus_error_stats() Dario Binacchi
  2024-10-29  8:44 ` [RFC PATCH v2 1/6] " Dario Binacchi
@ 2024-10-29  8:44 ` Dario Binacchi
  2024-10-29  8:44 ` [RFC PATCH v2 3/6] can: dev: add helpers to setup an error frame Dario Binacchi
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Dario Binacchi @ 2024-10-29  8:44 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-amarula, Dario Binacchi, Andrew Lunn, David S. Miller,
	Eric Dumazet, Haibo Chen, Jakub Kicinski, Marc Kleine-Budde,
	Paolo Abeni, Rob Herring, Uwe Kleine-König, Vincent Mailhol,
	linux-can, netdev

The patch delegates the statistics update in case of bus error to the
can_update_bus_error_stats().

Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
---

(no changes since v1)

 drivers/net/can/flexcan/flexcan-core.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)

diff --git a/drivers/net/can/flexcan/flexcan-core.c b/drivers/net/can/flexcan/flexcan-core.c
index ac1a860986df..790b8e162d73 100644
--- a/drivers/net/can/flexcan/flexcan-core.c
+++ b/drivers/net/can/flexcan/flexcan-core.c
@@ -819,7 +819,6 @@ static void flexcan_irq_bus_err(struct net_device *dev, u32 reg_esr)
 	struct flexcan_regs __iomem *regs = priv->regs;
 	struct sk_buff *skb;
 	struct can_frame *cf;
-	bool rx_errors = false, tx_errors = false;
 	u32 timestamp;
 	int err;
 
@@ -834,41 +833,31 @@ static void flexcan_irq_bus_err(struct net_device *dev, u32 reg_esr)
 	if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
 		netdev_dbg(dev, "BIT1_ERR irq\n");
 		cf->data[2] |= CAN_ERR_PROT_BIT1;
-		tx_errors = true;
 	}
 	if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
 		netdev_dbg(dev, "BIT0_ERR irq\n");
 		cf->data[2] |= CAN_ERR_PROT_BIT0;
-		tx_errors = true;
 	}
 	if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
 		netdev_dbg(dev, "ACK_ERR irq\n");
 		cf->can_id |= CAN_ERR_ACK;
 		cf->data[3] = CAN_ERR_PROT_LOC_ACK;
-		tx_errors = true;
 	}
 	if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
 		netdev_dbg(dev, "CRC_ERR irq\n");
 		cf->data[2] |= CAN_ERR_PROT_BIT;
 		cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
-		rx_errors = true;
 	}
 	if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
 		netdev_dbg(dev, "FRM_ERR irq\n");
 		cf->data[2] |= CAN_ERR_PROT_FORM;
-		rx_errors = true;
 	}
 	if (reg_esr & FLEXCAN_ESR_STF_ERR) {
 		netdev_dbg(dev, "STF_ERR irq\n");
 		cf->data[2] |= CAN_ERR_PROT_STUFF;
-		rx_errors = true;
 	}
 
-	priv->can.can_stats.bus_error++;
-	if (rx_errors)
-		dev->stats.rx_errors++;
-	if (tx_errors)
-		dev->stats.tx_errors++;
+	can_update_bus_error_stats(dev, cf);
 
 	err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp);
 	if (err)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [RFC PATCH v2 3/6] can: dev: add helpers to setup an error frame
  2024-10-29  8:44 [RFC PATCH v2 0/6] can: dev: add generic function can_update_bus_error_stats() Dario Binacchi
  2024-10-29  8:44 ` [RFC PATCH v2 1/6] " Dario Binacchi
  2024-10-29  8:44 ` [RFC PATCH v2 2/6] can: flexcan: use can_update_bus_error_stats() Dario Binacchi
@ 2024-10-29  8:44 ` Dario Binacchi
  2024-10-29  8:57   ` Marc Kleine-Budde
  2024-10-29  8:44 ` [RFC PATCH v2 4/6] can: flexcan: use helpers to setup the " Dario Binacchi
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 9+ messages in thread
From: Dario Binacchi @ 2024-10-29  8:44 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-amarula, Dario Binacchi, Marc Kleine-Budde, Vincent Mailhol,
	linux-can

These helpers can prevent errors and code duplication when setting up a
CAN error frame.

Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>

---

Changes in v2:
- Replace macros with static inline functions
- Update the commit message

 include/linux/can/dev.h | 46 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/include/linux/can/dev.h b/include/linux/can/dev.h
index 0977656b366d..1b09d30dae32 100644
--- a/include/linux/can/dev.h
+++ b/include/linux/can/dev.h
@@ -168,6 +168,52 @@ static inline bool can_dev_dropped_skb(struct net_device *dev, struct sk_buff *s
 	return can_dropped_invalid_skb(dev, skb);
 }
 
+static inline void can_frame_error_init(struct can_frame *cf)
+{
+	if (cf)
+		cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
+}
+
+static inline void can_frame_set_err_bit0(struct can_frame *cf)
+{
+	if (cf)
+		cf->data[2] |= CAN_ERR_PROT_BIT0;
+}
+
+static inline void can_frame_set_err_bit1(struct can_frame *cf)
+{
+	if (cf)
+		cf->data[2] |= CAN_ERR_PROT_BIT1;
+}
+
+static inline void can_frame_set_err_ack(struct can_frame *cf)
+{
+	if (cf) {
+		cf->can_id |= CAN_ERR_ACK;
+		cf->data[3] = CAN_ERR_PROT_LOC_ACK;
+	}
+}
+
+static inline void can_frame_set_err_crc(struct can_frame *cf)
+{
+	if (cf) {
+		cf->data[2] |= CAN_ERR_PROT_BIT;
+		cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
+	}
+}
+
+static inline void can_frame_set_err_form(struct can_frame *cf)
+{
+	if (cf)
+		cf->data[2] |= CAN_ERR_PROT_FORM;
+}
+
+static inline void can_frame_set_err_stuff(struct can_frame *cf)
+{
+	if (cf)
+		cf->data[2] |= CAN_ERR_PROT_STUFF;
+}
+
 void can_setup(struct net_device *dev);
 
 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [RFC PATCH v2 4/6] can: flexcan: use helpers to setup the error frame
  2024-10-29  8:44 [RFC PATCH v2 0/6] can: dev: add generic function can_update_bus_error_stats() Dario Binacchi
                   ` (2 preceding siblings ...)
  2024-10-29  8:44 ` [RFC PATCH v2 3/6] can: dev: add helpers to setup an error frame Dario Binacchi
@ 2024-10-29  8:44 ` Dario Binacchi
  2024-10-29  8:44 ` [RFC PATCH v2 5/6] can: netlink: extend stats to the error types (ack, CRC, form, ...) Dario Binacchi
  2024-10-29  8:44 ` [RFC PATCH v2 6/6] can: dev: update the error types stats " Dario Binacchi
  5 siblings, 0 replies; 9+ messages in thread
From: Dario Binacchi @ 2024-10-29  8:44 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-amarula, Dario Binacchi, Andrew Lunn, David S. Miller,
	Eric Dumazet, Frank Li, Haibo Chen, Han Xu, Jakub Kicinski,
	Marc Kleine-Budde, Paolo Abeni, Rob Herring,
	Uwe Kleine-König, Vincent Mailhol, linux-can, netdev

The patch replaces the code that directly accesses cf->data[] for setting
up the CAN error frame with the appropriate helpers.

Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>

---

Changes in v2:
- Replace the macros with static inline funcions calls.
- Update the commit message

 drivers/net/can/flexcan/flexcan-core.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/drivers/net/can/flexcan/flexcan-core.c b/drivers/net/can/flexcan/flexcan-core.c
index 790b8e162d73..85a124a31752 100644
--- a/drivers/net/can/flexcan/flexcan-core.c
+++ b/drivers/net/can/flexcan/flexcan-core.c
@@ -828,33 +828,31 @@ static void flexcan_irq_bus_err(struct net_device *dev, u32 reg_esr)
 	if (unlikely(!skb))
 		return;
 
-	cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
+	can_frame_error_init(cf);
 
 	if (reg_esr & FLEXCAN_ESR_BIT1_ERR) {
 		netdev_dbg(dev, "BIT1_ERR irq\n");
-		cf->data[2] |= CAN_ERR_PROT_BIT1;
+		can_frame_set_err_bit1(cf);
 	}
 	if (reg_esr & FLEXCAN_ESR_BIT0_ERR) {
 		netdev_dbg(dev, "BIT0_ERR irq\n");
-		cf->data[2] |= CAN_ERR_PROT_BIT0;
+		can_frame_set_err_bit0(cf);
 	}
 	if (reg_esr & FLEXCAN_ESR_ACK_ERR) {
 		netdev_dbg(dev, "ACK_ERR irq\n");
-		cf->can_id |= CAN_ERR_ACK;
-		cf->data[3] = CAN_ERR_PROT_LOC_ACK;
+		can_frame_set_err_ack(cf);
 	}
 	if (reg_esr & FLEXCAN_ESR_CRC_ERR) {
 		netdev_dbg(dev, "CRC_ERR irq\n");
-		cf->data[2] |= CAN_ERR_PROT_BIT;
-		cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
+		can_frame_set_err_crc(cf);
 	}
 	if (reg_esr & FLEXCAN_ESR_FRM_ERR) {
 		netdev_dbg(dev, "FRM_ERR irq\n");
-		cf->data[2] |= CAN_ERR_PROT_FORM;
+		can_frame_set_err_form(cf);
 	}
 	if (reg_esr & FLEXCAN_ESR_STF_ERR) {
 		netdev_dbg(dev, "STF_ERR irq\n");
-		cf->data[2] |= CAN_ERR_PROT_STUFF;
+		can_frame_set_err_stuff(cf);
 	}
 
 	can_update_bus_error_stats(dev, cf);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [RFC PATCH v2 5/6] can: netlink: extend stats to the error types (ack, CRC, form, ...)
  2024-10-29  8:44 [RFC PATCH v2 0/6] can: dev: add generic function can_update_bus_error_stats() Dario Binacchi
                   ` (3 preceding siblings ...)
  2024-10-29  8:44 ` [RFC PATCH v2 4/6] can: flexcan: use helpers to setup the " Dario Binacchi
@ 2024-10-29  8:44 ` Dario Binacchi
  2024-10-29  8:44 ` [RFC PATCH v2 6/6] can: dev: update the error types stats " Dario Binacchi
  5 siblings, 0 replies; 9+ messages in thread
From: Dario Binacchi @ 2024-10-29  8:44 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-amarula, Dario Binacchi, Marc Kleine-Budde, Vincent Mailhol,
	linux-can

The CAN bus protocol specifies 5 CAN error types:

- Bit Error [Transmitter]
- Bit Stuffing Error [Receiver]
- Form Error [Receiver]
- ACK Error (Acknowledgment) [Transmitter]
- CRC Error (Cyclic Redundancy Check) [Receiver]

The patch also adds the corresponding counters to the statistics. Since
each device/driver can determine which of these errors has occurred, why
not export this information along with the more general counters?

Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
---

(no changes since v1)

 include/uapi/linux/can/netlink.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/include/uapi/linux/can/netlink.h b/include/uapi/linux/can/netlink.h
index 02ec32d69474..8b33549e7e19 100644
--- a/include/uapi/linux/can/netlink.h
+++ b/include/uapi/linux/can/netlink.h
@@ -114,6 +114,12 @@ struct can_device_stats {
 	__u32 bus_off;		/* Changes to bus off state */
 	__u32 arbitration_lost; /* Arbitration lost errors */
 	__u32 restarts;		/* CAN controller re-starts */
+	__u32 bit_error;        /* Bit error */
+	__u32 stuff_error;      /* Bit stuffing error */
+	__u32 form_error;       /* form error */
+	__u32 ack_error;        /* ack error */
+	__u32 crc_error;        /* CRC error */
+	__u32 pad;
 };
 
 /*
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [RFC PATCH v2 6/6] can: dev: update the error types stats (ack, CRC, form, ...)
  2024-10-29  8:44 [RFC PATCH v2 0/6] can: dev: add generic function can_update_bus_error_stats() Dario Binacchi
                   ` (4 preceding siblings ...)
  2024-10-29  8:44 ` [RFC PATCH v2 5/6] can: netlink: extend stats to the error types (ack, CRC, form, ...) Dario Binacchi
@ 2024-10-29  8:44 ` Dario Binacchi
  5 siblings, 0 replies; 9+ messages in thread
From: Dario Binacchi @ 2024-10-29  8:44 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-amarula, Dario Binacchi, Alexandra Winter, Andrew Lunn,
	David S. Miller, Eric Dumazet, Gal Pressman, Jakub Kicinski,
	Kory Maincent, Marc Kleine-Budde, Paolo Abeni, Shannon Nelson,
	Vincent Mailhol, linux-can, netdev

The patch modifies can_update_bus_error_stats() by also updating the
counters related to the types of CAN errors.

Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>

---

(no changes since v1)

 drivers/net/can/dev/dev.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 0a3b1aad405b..f035e68044b3 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -27,16 +27,31 @@ void can_update_bus_error_stats(struct net_device *dev, struct can_frame *cf)
 	priv = netdev_priv(dev);
 	priv->can_stats.bus_error++;
 
-	if ((cf->can_id & CAN_ERR_ACK) && cf->data[3] == CAN_ERR_PROT_LOC_ACK)
+	if ((cf->can_id & CAN_ERR_ACK) && cf->data[3] == CAN_ERR_PROT_LOC_ACK) {
+		priv->can_stats.ack_error++;
 		tx_errors = true;
-	else if (cf->data[2] & (CAN_ERR_PROT_BIT1 | CAN_ERR_PROT_BIT0))
+	}
+
+	if (cf->data[2] & (CAN_ERR_PROT_BIT1 | CAN_ERR_PROT_BIT0)) {
+		priv->can_stats.bit_error++;
 		tx_errors = true;
+	}
 
-	if (cf->data[2] & (CAN_ERR_PROT_FORM | CAN_ERR_PROT_STUFF))
+	if (cf->data[2] & CAN_ERR_PROT_FORM) {
+		priv->can_stats.form_error++;
 		rx_errors = true;
-	else if ((cf->data[2] & CAN_ERR_PROT_BIT) &&
-		 (cf->data[3] == CAN_ERR_PROT_LOC_CRC_SEQ))
+	}
+
+	if (cf->data[2] & CAN_ERR_PROT_STUFF) {
+		priv->can_stats.stuff_error++;
 		rx_errors = true;
+	}
+
+	if ((cf->data[2] & CAN_ERR_PROT_BIT) &&
+	    cf->data[3] == CAN_ERR_PROT_LOC_CRC_SEQ) {
+		priv->can_stats.crc_error++;
+		rx_errors = true;
+	}
 
 	if (rx_errors)
 		dev->stats.rx_errors++;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [RFC PATCH v2 1/6] can: dev: add generic function can_update_bus_error_stats()
  2024-10-29  8:44 ` [RFC PATCH v2 1/6] " Dario Binacchi
@ 2024-10-29  8:49   ` Marc Kleine-Budde
  0 siblings, 0 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2024-10-29  8:49 UTC (permalink / raw)
  To: Dario Binacchi
  Cc: linux-kernel, linux-amarula, Andrew Lunn, David S. Miller,
	Eric Dumazet, Gal Pressman, Jakub Kicinski, Kory Maincent,
	Paolo Abeni, Sabrina Dubroca, Shannon Nelson, Simon Horman,
	Vincent Mailhol, linux-can, netdev

[-- Attachment #1: Type: text/plain, Size: 2799 bytes --]

Hello Dario,

On 29.10.2024 09:44:45, Dario Binacchi wrote:
> The function aims to generalize the statistics update by centralizing
> the related code, thus avoiding code duplication.
> 
> Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
> ---

no proper review, just found that double assignment.

Marc

> 
> (no changes since v1)
> 
>  drivers/net/can/dev/dev.c | 30 ++++++++++++++++++++++++++++++
>  include/linux/can/dev.h   |  1 +
>  2 files changed, 31 insertions(+)
> 
> diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
> index 6792c14fd7eb..0a3b1aad405b 100644
> --- a/drivers/net/can/dev/dev.c
> +++ b/drivers/net/can/dev/dev.c
> @@ -16,6 +16,36 @@
>  #include <linux/gpio/consumer.h>
>  #include <linux/of.h>
>  
> +void can_update_bus_error_stats(struct net_device *dev, struct can_frame *cf)
> +{
> +	struct can_priv *priv = netdev_priv(dev);
                                ^^^^^^^^^^^^^^^^
> +	bool rx_errors = false, tx_errors = false;
> +
> +	if (!cf || !(cf->can_id & (CAN_ERR_PROT | CAN_ERR_BUSERROR)))
> +		return;
> +
> +	priv = netdev_priv(dev);
               ^^^^^^^^^^^^^^^^
> +	priv->can_stats.bus_error++;
> +
> +	if ((cf->can_id & CAN_ERR_ACK) && cf->data[3] == CAN_ERR_PROT_LOC_ACK)
> +		tx_errors = true;
> +	else if (cf->data[2] & (CAN_ERR_PROT_BIT1 | CAN_ERR_PROT_BIT0))
> +		tx_errors = true;
> +
> +	if (cf->data[2] & (CAN_ERR_PROT_FORM | CAN_ERR_PROT_STUFF))
> +		rx_errors = true;
> +	else if ((cf->data[2] & CAN_ERR_PROT_BIT) &&
> +		 (cf->data[3] == CAN_ERR_PROT_LOC_CRC_SEQ))
> +		rx_errors = true;
> +
> +	if (rx_errors)
> +		dev->stats.rx_errors++;
> +
> +	if (tx_errors)
> +		dev->stats.tx_errors++;
> +}
> +EXPORT_SYMBOL_GPL(can_update_bus_error_stats);
> +
>  static void can_update_state_error_stats(struct net_device *dev,
>  					 enum can_state new_state)
>  {
> diff --git a/include/linux/can/dev.h b/include/linux/can/dev.h
> index 23492213ea35..0977656b366d 100644
> --- a/include/linux/can/dev.h
> +++ b/include/linux/can/dev.h
> @@ -201,6 +201,7 @@ void can_state_get_by_berr_counter(const struct net_device *dev,
>  				   enum can_state *rx_state);
>  void can_change_state(struct net_device *dev, struct can_frame *cf,
>  		      enum can_state tx_state, enum can_state rx_state);
> +void can_update_bus_error_stats(struct net_device *dev, struct can_frame *cf);
>  
>  #ifdef CONFIG_OF
>  void of_can_transceiver(struct net_device *dev);
> -- 
> 2.43.0
> 
> 

-- 
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [RFC PATCH v2 3/6] can: dev: add helpers to setup an error frame
  2024-10-29  8:44 ` [RFC PATCH v2 3/6] can: dev: add helpers to setup an error frame Dario Binacchi
@ 2024-10-29  8:57   ` Marc Kleine-Budde
  0 siblings, 0 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2024-10-29  8:57 UTC (permalink / raw)
  To: Dario Binacchi; +Cc: linux-kernel, linux-amarula, Vincent Mailhol, linux-can

[-- Attachment #1: Type: text/plain, Size: 2429 bytes --]

On 29.10.2024 09:44:47, Dario Binacchi wrote:
> These helpers can prevent errors and code duplication when setting up a
> CAN error frame.
> 
> Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>

AFAICS in the flexcan driver we don't need the "if (cf)" checks, do we?
Having repeated NULL pointer checks don't feel right.

Marc

> 
> ---
> 
> Changes in v2:
> - Replace macros with static inline functions
> - Update the commit message
> 
>  include/linux/can/dev.h | 46 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 46 insertions(+)
> 
> diff --git a/include/linux/can/dev.h b/include/linux/can/dev.h
> index 0977656b366d..1b09d30dae32 100644
> --- a/include/linux/can/dev.h
> +++ b/include/linux/can/dev.h
> @@ -168,6 +168,52 @@ static inline bool can_dev_dropped_skb(struct net_device *dev, struct sk_buff *s
>  	return can_dropped_invalid_skb(dev, skb);
>  }
>  
> +static inline void can_frame_error_init(struct can_frame *cf)
> +{
> +	if (cf)
> +		cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
> +}
> +
> +static inline void can_frame_set_err_bit0(struct can_frame *cf)
> +{
> +	if (cf)
> +		cf->data[2] |= CAN_ERR_PROT_BIT0;
> +}
> +
> +static inline void can_frame_set_err_bit1(struct can_frame *cf)
> +{
> +	if (cf)
> +		cf->data[2] |= CAN_ERR_PROT_BIT1;
> +}
> +
> +static inline void can_frame_set_err_ack(struct can_frame *cf)
> +{
> +	if (cf) {
> +		cf->can_id |= CAN_ERR_ACK;
> +		cf->data[3] = CAN_ERR_PROT_LOC_ACK;
> +	}
> +}
> +
> +static inline void can_frame_set_err_crc(struct can_frame *cf)
> +{
> +	if (cf) {
> +		cf->data[2] |= CAN_ERR_PROT_BIT;
> +		cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
> +	}
> +}
> +
> +static inline void can_frame_set_err_form(struct can_frame *cf)
> +{
> +	if (cf)
> +		cf->data[2] |= CAN_ERR_PROT_FORM;
> +}
> +
> +static inline void can_frame_set_err_stuff(struct can_frame *cf)
> +{
> +	if (cf)
> +		cf->data[2] |= CAN_ERR_PROT_STUFF;
> +}
> +
>  void can_setup(struct net_device *dev);
>  
>  struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
> -- 
> 2.43.0
> 
> 

-- 
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-10-29  8:57 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-29  8:44 [RFC PATCH v2 0/6] can: dev: add generic function can_update_bus_error_stats() Dario Binacchi
2024-10-29  8:44 ` [RFC PATCH v2 1/6] " Dario Binacchi
2024-10-29  8:49   ` Marc Kleine-Budde
2024-10-29  8:44 ` [RFC PATCH v2 2/6] can: flexcan: use can_update_bus_error_stats() Dario Binacchi
2024-10-29  8:44 ` [RFC PATCH v2 3/6] can: dev: add helpers to setup an error frame Dario Binacchi
2024-10-29  8:57   ` Marc Kleine-Budde
2024-10-29  8:44 ` [RFC PATCH v2 4/6] can: flexcan: use helpers to setup the " Dario Binacchi
2024-10-29  8:44 ` [RFC PATCH v2 5/6] can: netlink: extend stats to the error types (ack, CRC, form, ...) Dario Binacchi
2024-10-29  8:44 ` [RFC PATCH v2 6/6] can: dev: update the error types stats " Dario Binacchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox