public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5 1/3] can: length: fix bitstuffing count
@ 2023-06-14 20:40 HMS Incident Management
  2023-06-15  7:51 ` Simon Horman
  0 siblings, 1 reply; 5+ messages in thread
From: HMS Incident Management @ 2023-06-14 20:40 UTC (permalink / raw)
  To: mkl@pengutronix.de, linux-can@vger.kernel.org,
	Thomas.Kopp@microchip.com
  Cc: socketcan@hartkopp.net, netdev@vger.kernel.org, marex@denx.de,
	simon.horman@corigine.com, linux-kernel@vger.kernel.org,
	mailhol.vincent@wanadoo.fr

**We apologize for the delay in delivering this email, which was caused by a mail incident that occurred over the weekend on June 10th. This email was originally sent from vincent.mailhol@gmail.com on 06/11/2023 02:58:08 

The Stuff Bit Count is always coded on 4 bits [1]. Update the Stuff
Bit Count size accordingly.

In addition, the CRC fields of CAN FD Frames contain stuff bits at
fixed positions called fixed stuff bits [2]. The CRC field starts with
a fixed stuff bit and then has another fixed stuff bit after each
fourth bit [2], which allows us to derive this formula:

  FSB count = 1 + round_down(len(CRC field)/4)

The length of the CRC field is [1]:

  len(CRC field) = len(Stuff Bit Count) + len(CRC)
                 = 4 + len(CRC)

with len(CRC) either 17 or 21 bits depending of the payload length.

In conclusion, for CRC17:

  FSB count = 1 + round_down((4 + 17)/4)
            = 6

and for CRC 21:

  FSB count = 1 + round_down((4 + 21)/4)
            = 7

Add a Fixed Stuff bits (FSB) field with above values and update
CANFD_FRAME_OVERHEAD_SFF and CANFD_FRAME_OVERHEAD_EFF accordingly.

[1] ISO 11898-1:2015 section 10.4.2.6 "CRC field":

  The CRC field shall contain the CRC sequence followed by a recessive
  CRC delimiter. For FD Frames, the CRC field shall also contain the
  stuff count.

  Stuff count

  If FD Frames, the stuff count shall be at the beginning of the CRC
  field. It shall consist of the stuff bit count modulo 8 in a 3-bit
  gray code followed by a parity bit [...]

[2] ISO 11898-1:2015 paragraph 10.5 "Frame coding":

  In the CRC field of FD Frames, the stuff bits shall be inserted at
  fixed positions; they are called fixed stuff bits. There shall be a
  fixed stuff bit before the first bit of the stuff count, even if the
  last bits of the preceding field are a sequence of five consecutive
  bits of identical value, there shall be only the fixed stuff bit,
  there shall not be two consecutive stuff bits. A further fixed stuff
  bit shall be inserted after each fourth bit of the CRC field [...]

Fixes: 85d99c3e2a13 ("can: length: can_skb_get_frame_len(): introduce function to get data length of frame in data link layer")
Suggested-by: Thomas Kopp 
Signed-off-by: Vincent Mailhol 
Reviewed-by: Thomas Kopp 
---
 include/linux/can/length.h | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/include/linux/can/length.h b/include/linux/can/length.h
index 69336549d24f..b8c12c83bc51 100644
--- a/include/linux/can/length.h
+++ b/include/linux/can/length.h
@@ -72,17 +72,18 @@
  * Error Status Indicator (ESI)		1
  * Data length code (DLC)		4
  * Data field				0...512
- * Stuff Bit Count (SBC)		0...16: 4 20...64:5
+ * Stuff Bit Count (SBC)		4
  * CRC					0...16: 17 20...64:21
  * CRC delimiter (CD)			1
+ * Fixed Stuff bits (FSB)		0...16: 6 20...64:7
  * ACK slot (AS)			1
  * ACK delimiter (AD)			1
  * End-of-frame (EOF)			7
  * Inter frame spacing			3
  *
- * assuming CRC21, rounded up and ignoring bitstuffing
+ * assuming CRC21, rounded up and ignoring dynamic bitstuffing
  */
-#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(61, 8)
+#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(67, 8)
 
 /*
  * Size of a CAN-FD Extended Frame
@@ -101,17 +102,18 @@
  * Error Status Indicator (ESI)		1
  * Data length code (DLC)		4
  * Data field				0...512
- * Stuff Bit Count (SBC)		0...16: 4 20...64:5
+ * Stuff Bit Count (SBC)		4
  * CRC					0...16: 17 20...64:21
  * CRC delimiter (CD)			1
+ * Fixed Stuff bits (FSB)		0...16: 6 20...64:7
  * ACK slot (AS)			1
  * ACK delimiter (AD)			1
  * End-of-frame (EOF)			7
  * Inter frame spacing			3
  *
- * assuming CRC21, rounded up and ignoring bitstuffing
+ * assuming CRC21, rounded up and ignoring dynamic bitstuffing
  */
-#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(80, 8)
+#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(86, 8)
 
 /*
  * Maximum size of a Classical CAN frame
-- 
2.39.3

^ permalink raw reply related	[flat|nested] 5+ messages in thread
* [PATCH] can: length: add definitions for frame lengths in bits
@ 2023-05-07 15:55 Vincent Mailhol
  2023-06-11  2:57 ` [PATCH v5 0/3] can: length: fix definitions and add bit length calculation Vincent Mailhol
  0 siblings, 1 reply; 5+ messages in thread
From: Vincent Mailhol @ 2023-05-07 15:55 UTC (permalink / raw)
  To: Marc Kleine-Budde, linux-can, Marek Vasut; +Cc: linux-kernel, Vincent Mailhol

When created in [1], frames length definitions were added to implement
byte queue limits (bql). Because bql expects lengths in bytes, bit
length definitions were not considered back then.

Recently, a need to refer to the exact frame length in bits, with CAN
bit stuffing, appeared in [2].

Add 9 frames length definitions:

 - CAN_FRAME_OVERHEAD_SFF_BITS:
 - CAN_FRAME_OVERHEAD_EFF_BITS
 - CANFD_FRAME_OVERHEAD_SFF_BITS
 - CANFD_FRAME_OVERHEAD_EFF_BITS
 - CAN_BIT_STUFFING_OVERHEAD
 - CAN_FRAME_LEN_MAX_BITS_NO_STUFFING
 - CAN_FRAME_LEN_MAX_BITS_STUFFING
 - CANFD_FRAME_LEN_MAX_BITS_NO_STUFFING
 - CANFD_FRAME_LEN_MAX_BITS_STUFFING

CAN_FRAME_LEN_MAX_BITS_STUFFING and CANFD_FRAME_LEN_MAX_BITS_STUFFING
define respectively the maximum number of bits in a classical CAN and
CAN-FD frame including bit stuffing. The other definitions are
intermediate values.

In addition to the above:

 - Include linux/bits.h and then replace the value 8 by BITS_PER_BYTE
   whenever relevant.
 - Include linux/math.h because of DIV_ROUND_UP(). N.B: the use of
   DIV_ROUND_UP() is not new to this patch, but the include was
   previously omitted.
 - Update the existing length definitions to use the newly defined values.
 - Add myself as copyright owner for 2020 (as coauthor of the initial
   version, c.f. [1]) and for 2023 (this patch).

[1] commit 85d99c3e2a13 ("can: length: can_skb_get_frame_len(): introduce
    function to get data length of frame in data link layer")
Link: https://git.kernel.org/torvalds/c/85d99c3e2a13

[2] RE: [PATCH] can: mcp251xfd: Increase poll timeout
Link: https://lore.kernel.org/linux-can/BL3PR11MB64846C83ACD04E9330B0FE66FB729@BL3PR11MB6484.namprd11.prod.outlook.com/

Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
---
As always, let me know if you have better inspiration than me for the
naming.
---
 include/linux/can/length.h | 84 ++++++++++++++++++++++++++++++++------
 1 file changed, 72 insertions(+), 12 deletions(-)

diff --git a/include/linux/can/length.h b/include/linux/can/length.h
index 6995092b774e..60492fcbe34d 100644
--- a/include/linux/can/length.h
+++ b/include/linux/can/length.h
@@ -1,13 +1,17 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 /* Copyright (C) 2020 Oliver Hartkopp <socketcan@hartkopp.net>
  * Copyright (C) 2020 Marc Kleine-Budde <kernel@pengutronix.de>
+ * Copyright (C) 2020, 2023 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
  */
 
 #ifndef _CAN_LENGTH_H
 #define _CAN_LENGTH_H
 
+#include <linux/bits.h>
+#include <linux/math.h>
+
 /*
- * Size of a Classical CAN Standard Frame
+ * Size of a Classical CAN Standard Frame in bits
  *
  * Name of Field			Bits
  * ---------------------------------------------------------
@@ -25,12 +29,19 @@
  * End-of-frame (EOF)			7
  * Inter frame spacing			3
  *
- * rounded up and ignoring bitstuffing
+ * ignoring bitstuffing
  */
-#define CAN_FRAME_OVERHEAD_SFF DIV_ROUND_UP(47, 8)
+#define CAN_FRAME_OVERHEAD_SFF_BITS 47
 
 /*
- * Size of a Classical CAN Extended Frame
+ * Size of a Classical CAN Standard Frame
+ * (rounded up and ignoring bitstuffing)
+ */
+#define CAN_FRAME_OVERHEAD_SFF \
+	DIV_ROUND_UP(CAN_FRAME_OVERHEAD_SFF_BITS, BITS_PER_BYTE)
+
+/*
+ * Size of a Classical CAN Extended Frame in bits
  *
  * Name of Field			Bits
  * ---------------------------------------------------------
@@ -50,12 +61,19 @@
  * End-of-frame (EOF)			7
  * Inter frame spacing			3
  *
- * rounded up and ignoring bitstuffing
+ * ignoring bitstuffing
  */
-#define CAN_FRAME_OVERHEAD_EFF DIV_ROUND_UP(67, 8)
+#define CAN_FRAME_OVERHEAD_EFF_BITS 67
 
 /*
- * Size of a CAN-FD Standard Frame
+ * Size of a Classical CAN Extended Frame
+ * (rounded up and ignoring bitstuffing)
+ */
+#define CAN_FRAME_OVERHEAD_EFF \
+	DIV_ROUND_UP(CAN_FRAME_OVERHEAD_EFF_BITS, BITS_PER_BYTE)
+
+/*
+ * Size of a CAN-FD Standard Frame in bits
  *
  * Name of Field			Bits
  * ---------------------------------------------------------
@@ -77,12 +95,19 @@
  * End-of-frame (EOF)			7
  * Inter frame spacing			3
  *
- * assuming CRC21, rounded up and ignoring bitstuffing
+ * assuming CRC21 and ignoring bitstuffing
  */
-#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(61, 8)
+#define CANFD_FRAME_OVERHEAD_SFF_BITS 61
 
 /*
- * Size of a CAN-FD Extended Frame
+ * Size of a CAN-FD Standard Frame
+ * (assuming CRC21, rounded up and ignoring bitstuffing)
+ */
+#define CANFD_FRAME_OVERHEAD_SFF \
+	DIV_ROUND_UP(CANFD_FRAME_OVERHEAD_SFF_BITS, BITS_PER_BYTE)
+
+/*
+ * Size of a CAN-FD Extended Frame in bits
  *
  * Name of Field			Bits
  * ---------------------------------------------------------
@@ -106,9 +131,32 @@
  * End-of-frame (EOF)			7
  * Inter frame spacing			3
  *
- * assuming CRC21, rounded up and ignoring bitstuffing
+ * assuming CRC21 and ignoring bitstuffing
+ */
+#define CANFD_FRAME_OVERHEAD_EFF_BITS 80
+
+/*
+ * Size of a CAN-FD Extended Frame
+ * (assuming CRC21, rounded up and ignoring bitstuffing)
+ */
+#define CANFD_FRAME_OVERHEAD_EFF \
+	DIV_ROUND_UP(CANFD_FRAME_OVERHEAD_EFF_BITS, BITS_PER_BYTE)
+
+/* CAN bit stuffing overhead multiplication factor */
+#define CAN_BIT_STUFFING_OVERHEAD 1.2
+
+/*
+ * Maximum size of a Classical CAN frame in bits, ignoring bitstuffing
  */
-#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(80, 8)
+#define CAN_FRAME_LEN_MAX_BITS_NO_STUFFING \
+	(CAN_FRAME_OVERHEAD_EFF_BITS + CAN_MAX_DLEN * BITS_PER_BYTE)
+
+/*
+ * Maximum size of a Classical CAN frame in bits, including bitstuffing
+ */
+#define CAN_FRAME_LEN_MAX_BITS_STUFFING				\
+	((unsigned int)(CAN_FRAME_LEN_MAX_BITS_NO_STUFFING *	\
+			CAN_BIT_STUFFING_OVERHEAD))
 
 /*
  * Maximum size of a Classical CAN frame
@@ -116,6 +164,18 @@
  */
 #define CAN_FRAME_LEN_MAX (CAN_FRAME_OVERHEAD_EFF + CAN_MAX_DLEN)
 
+/*
+ * Maximum size of a CAN-FD frame in bits, ignoring bitstuffing
+ */
+#define CANFD_FRAME_LEN_MAX_BITS_NO_STUFFING \
+	(CANFD_FRAME_OVERHEAD_EFF_BITS + CANFD_MAX_DLEN * BITS_PER_BYTE)
+
+/*
+ * Maximum size of a CAN-FD frame in bits, ignoring bitstuffing
+ */
+#define CANFD_FRAME_LEN_MAX_BITS_STUFFING			\
+	((unsigned int)(CANFD_FRAME_LEN_MAX_BITS_NO_STUFFING *	\
+			CAN_BIT_STUFFING_OVERHEAD))
 /*
  * Maximum size of a CAN-FD frame
  * (rounded up and ignoring bitstuffing)
-- 
2.39.3


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

end of thread, other threads:[~2023-06-20 13:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-14 20:40 [PATCH v5 1/3] can: length: fix bitstuffing count HMS Incident Management
2023-06-15  7:51 ` Simon Horman
2023-06-15  9:58   ` Vincent MAILHOL
2023-06-20 13:35     ` Marc Kleine-Budde
  -- strict thread matches above, loose matches on Subject: below --
2023-05-07 15:55 [PATCH] can: length: add definitions for frame lengths in bits Vincent Mailhol
2023-06-11  2:57 ` [PATCH v5 0/3] can: length: fix definitions and add bit length calculation Vincent Mailhol
2023-06-11  2:57   ` [PATCH v5 1/3] can: length: fix bitstuffing count Vincent Mailhol

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