Merge patch series "can: length: fix definitions and add bit length calculation"
Vincent Mailhol <mailhol.vincent@wanadoo.fr> says:
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].
This series introduces can_frame_bits(): a function-like macro that
can calculate the exact size of a CAN(-FD) frame in bits with or
without bitsuffing.
[1] commit 85d99c3e2a
("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
* Changelog *
v4 -> v5:
* In __can_cc_frame_bits() and __can_fd_frame_bits(), enclose
data_len in brackets to prevent operator precedence issues.
* Add a note in can_frame_bits() documentation to explain that
data_len shall have no side effects.
* While at it, make CAN(FD)_FRAME_LEN_MAX definition fit on a single
line.
* A few typo/grammar small fixes in the commit descriptions.
Link: https://lore.kernel.org/linux-can/20230601165625.100040-1-mailhol.vincent@wanadoo.fr
v3 -> v4:
* No functional changes.
* as reported by Simon Horman, fix typo in the documentation of
can_bitstuffing_len(): "bitstream_len" -> "destuffed_len".
* as reported by Thomas Kopp, fix several other typos:
- "indicatior" -> "indicator"
- "in on the wire" -> "on the wire"
- "bitsuffing" -> "bitstuffing".
* in CAN_FRAME_LEN_MAX comment: specify that only the dynamic
bitstuffing gets ignored but that the intermission is included.
* move the Suggested-by: Thomas Kopp tag from patch 2 to patch 3.
* add Reviewed-by: Thomas Kopp tag on the full series.
* add an additional line of comment for the @intermission argument
of can_frame_bits().
Link: https://lore.kernel.org/linux-can/20230530144637.4746-1-mailhol.vincent@wanadoo.fr
v2 -> v3:
* turn can_frame_bits() and can_frame_bytes() into function-like
macros. The fact that inline functions can not be used to
initialize constant struct fields was bothering me. I did my best
to make the macro look as less ugly as possible.
* as reported by Simon Horman, add missing document for the is_fd
argument of can_frame_bits().
Link: https://lore.kernel.org/linux-can/20230523065218.51227-1-mailhol.vincent@wanadoo.fr
v1 -> v2:
* as suggested by Thomas Kopp, add a new patch to the series to fix
the stuff bit count and the fixed stuff bits definitions
* and another patch to fix documentation of the Remote Request
Substitution (RRS).
* refactor the length definition. Instead of using individual macro,
rely on an inline function. One reason is to minimize the number
of definitions. Another reason is that because the dynamic bit
stuff is calculated differently for CAN and CAN-FD, it is just not
possible to multiply the existing CANFD_FRAME_OVERHEAD_SFF/EFF by
the overhead ratio to get the bitsuffing: for CAN-FD, the CRC
field is already stuffed by the fixed stuff bits and is out of
scope of the dynamic bitstuffing.
Link: https://lore.kernel.org/linux-can/20230507155506.3179711-1-mailhol.vincent@wanadoo.fr
Link: https://lore.kernel.org/all/20230611025728.450837-1-mailhol.vincent@wanadoo.fr
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
This commit is contained in:
commit
dc7dabab8c
@ -78,18 +78,7 @@ unsigned int can_skb_get_frame_len(const struct sk_buff *skb)
|
||||
else
|
||||
len = cf->len;
|
||||
|
||||
if (can_is_canfd_skb(skb)) {
|
||||
if (cf->can_id & CAN_EFF_FLAG)
|
||||
len += CANFD_FRAME_OVERHEAD_EFF;
|
||||
else
|
||||
len += CANFD_FRAME_OVERHEAD_SFF;
|
||||
} else {
|
||||
if (cf->can_id & CAN_EFF_FLAG)
|
||||
len += CAN_FRAME_OVERHEAD_EFF;
|
||||
else
|
||||
len += CAN_FRAME_OVERHEAD_SFF;
|
||||
}
|
||||
|
||||
return len;
|
||||
return can_frame_bytes(can_is_canfd_skb(skb), cf->can_id & CAN_EFF_FLAG,
|
||||
false, len);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(can_skb_get_frame_len);
|
||||
|
@ -1,131 +1,258 @@
|
||||
/* 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/can.h>
|
||||
#include <linux/can/netlink.h>
|
||||
#include <linux/math.h>
|
||||
|
||||
/*
|
||||
* Size of a Classical CAN Standard Frame
|
||||
* Size of a Classical CAN Standard Frame header in bits
|
||||
*
|
||||
* Name of Field Bits
|
||||
* ---------------------------------------------------------
|
||||
* Start Of Frame (SOF) 1
|
||||
* Arbitration field:
|
||||
* base ID 11
|
||||
* Remote Transmission Request (RTR) 1
|
||||
* Control field:
|
||||
* IDentifier Extension bit (IDE) 1
|
||||
* FD Format indicator (FDF) 1
|
||||
* Data Length Code (DLC) 4
|
||||
*
|
||||
* including all fields preceding the data field, ignoring bitstuffing
|
||||
*/
|
||||
#define CAN_FRAME_HEADER_SFF_BITS 19
|
||||
|
||||
/*
|
||||
* Size of a Classical CAN Extended Frame header in bits
|
||||
*
|
||||
* Name of Field Bits
|
||||
* ---------------------------------------------------------
|
||||
* Start Of Frame (SOF) 1
|
||||
* Arbitration field:
|
||||
* base ID 11
|
||||
* Substitute Remote Request (SRR) 1
|
||||
* IDentifier Extension bit (IDE) 1
|
||||
* ID extension 18
|
||||
* Remote Transmission Request (RTR) 1
|
||||
* Control field:
|
||||
* FD Format indicator (FDF) 1
|
||||
* Reserved bit (r0) 1
|
||||
* Data length code (DLC) 4
|
||||
*
|
||||
* including all fields preceding the data field, ignoring bitstuffing
|
||||
*/
|
||||
#define CAN_FRAME_HEADER_EFF_BITS 39
|
||||
|
||||
/*
|
||||
* Size of a CAN-FD Standard Frame in bits
|
||||
*
|
||||
* Name of Field Bits
|
||||
* ---------------------------------------------------------
|
||||
* Start Of Frame (SOF) 1
|
||||
* Arbitration field:
|
||||
* base ID 11
|
||||
* Remote Request Substitution (RRS) 1
|
||||
* Control field:
|
||||
* IDentifier Extension bit (IDE) 1
|
||||
* FD Format indicator (FDF) 1
|
||||
* Reserved bit (res) 1
|
||||
* Bit Rate Switch (BRS) 1
|
||||
* Error Status Indicator (ESI) 1
|
||||
* Data length code (DLC) 4
|
||||
*
|
||||
* including all fields preceding the data field, ignoring bitstuffing
|
||||
*/
|
||||
#define CANFD_FRAME_HEADER_SFF_BITS 22
|
||||
|
||||
/*
|
||||
* Size of a CAN-FD Extended Frame in bits
|
||||
*
|
||||
* Name of Field Bits
|
||||
* ---------------------------------------------------------
|
||||
* Start Of Frame (SOF) 1
|
||||
* Arbitration field:
|
||||
* base ID 11
|
||||
* Substitute Remote Request (SRR) 1
|
||||
* IDentifier Extension bit (IDE) 1
|
||||
* ID extension 18
|
||||
* Remote Request Substitution (RRS) 1
|
||||
* Control field:
|
||||
* FD Format indicator (FDF) 1
|
||||
* Reserved bit (res) 1
|
||||
* Bit Rate Switch (BRS) 1
|
||||
* Error Status Indicator (ESI) 1
|
||||
* Data length code (DLC) 4
|
||||
*
|
||||
* including all fields preceding the data field, ignoring bitstuffing
|
||||
*/
|
||||
#define CANFD_FRAME_HEADER_EFF_BITS 41
|
||||
|
||||
/*
|
||||
* Size of a CAN CRC Field in bits
|
||||
*
|
||||
* Name of Field Bits
|
||||
* ---------------------------------------------------------
|
||||
* CRC sequence (CRC15) 15
|
||||
* CRC Delimiter 1
|
||||
*
|
||||
* ignoring bitstuffing
|
||||
*/
|
||||
#define CAN_FRAME_CRC_FIELD_BITS 16
|
||||
|
||||
/*
|
||||
* Size of a CAN-FD CRC17 Field in bits (length: 0..16)
|
||||
*
|
||||
* Name of Field Bits
|
||||
* ---------------------------------------------------------
|
||||
* Stuff Count 4
|
||||
* CRC Sequence (CRC17) 17
|
||||
* CRC Delimiter 1
|
||||
* Fixed stuff bits 6
|
||||
*/
|
||||
#define CANFD_FRAME_CRC17_FIELD_BITS 28
|
||||
|
||||
/*
|
||||
* Size of a CAN-FD CRC21 Field in bits (length: 20..64)
|
||||
*
|
||||
* Name of Field Bits
|
||||
* ---------------------------------------------------------
|
||||
* Stuff Count 4
|
||||
* CRC sequence (CRC21) 21
|
||||
* CRC Delimiter 1
|
||||
* Fixed stuff bits 7
|
||||
*/
|
||||
#define CANFD_FRAME_CRC21_FIELD_BITS 33
|
||||
|
||||
/*
|
||||
* Size of a CAN(-FD) Frame footer in bits
|
||||
*
|
||||
* Name of Field Bits
|
||||
* ---------------------------------------------------------
|
||||
* Start-of-frame 1
|
||||
* Identifier 11
|
||||
* Remote transmission request (RTR) 1
|
||||
* Identifier extension bit (IDE) 1
|
||||
* Reserved bit (r0) 1
|
||||
* Data length code (DLC) 4
|
||||
* Data field 0...64
|
||||
* CRC 15
|
||||
* CRC delimiter 1
|
||||
* ACK slot 1
|
||||
* ACK delimiter 1
|
||||
* End-of-frame (EOF) 7
|
||||
* Inter frame spacing 3
|
||||
* End Of Frame (EOF) 7
|
||||
*
|
||||
* rounded up and ignoring bitstuffing
|
||||
* including all fields following the CRC field
|
||||
*/
|
||||
#define CAN_FRAME_OVERHEAD_SFF DIV_ROUND_UP(47, 8)
|
||||
#define CAN_FRAME_FOOTER_BITS 9
|
||||
|
||||
/*
|
||||
* Size of a Classical CAN Extended Frame
|
||||
*
|
||||
* Name of Field Bits
|
||||
* ---------------------------------------------------------
|
||||
* Start-of-frame 1
|
||||
* Identifier A 11
|
||||
* Substitute remote request (SRR) 1
|
||||
* Identifier extension bit (IDE) 1
|
||||
* Identifier B 18
|
||||
* Remote transmission request (RTR) 1
|
||||
* Reserved bits (r1, r0) 2
|
||||
* Data length code (DLC) 4
|
||||
* Data field 0...64
|
||||
* CRC 15
|
||||
* CRC delimiter 1
|
||||
* ACK slot 1
|
||||
* ACK delimiter 1
|
||||
* End-of-frame (EOF) 7
|
||||
* Inter frame spacing 3
|
||||
*
|
||||
* rounded up and ignoring bitstuffing
|
||||
* First part of the Inter Frame Space
|
||||
* (a.k.a. IMF - intermission field)
|
||||
*/
|
||||
#define CAN_FRAME_OVERHEAD_EFF DIV_ROUND_UP(67, 8)
|
||||
#define CAN_INTERMISSION_BITS 3
|
||||
|
||||
/**
|
||||
* can_bitstuffing_len() - Calculate the maximum length with bitstuffing
|
||||
* @destuffed_len: length of a destuffed bit stream
|
||||
*
|
||||
* The worst bit stuffing case is a sequence in which dominant and
|
||||
* recessive bits alternate every four bits:
|
||||
*
|
||||
* Destuffed: 1 1111 0000 1111 0000 1111
|
||||
* Stuffed: 1 1111o 0000i 1111o 0000i 1111o
|
||||
*
|
||||
* Nomenclature
|
||||
*
|
||||
* - "0": dominant bit
|
||||
* - "o": dominant stuff bit
|
||||
* - "1": recessive bit
|
||||
* - "i": recessive stuff bit
|
||||
*
|
||||
* Aside from the first bit, one stuff bit is added every four bits.
|
||||
*
|
||||
* Return: length of the stuffed bit stream in the worst case scenario.
|
||||
*/
|
||||
#define can_bitstuffing_len(destuffed_len) \
|
||||
(destuffed_len + (destuffed_len - 1) / 4)
|
||||
|
||||
#define __can_bitstuffing_len(bitstuffing, destuffed_len) \
|
||||
(bitstuffing ? can_bitstuffing_len(destuffed_len) : \
|
||||
destuffed_len)
|
||||
|
||||
#define __can_cc_frame_bits(is_eff, bitstuffing, \
|
||||
intermission, data_len) \
|
||||
( \
|
||||
__can_bitstuffing_len(bitstuffing, \
|
||||
(is_eff ? CAN_FRAME_HEADER_EFF_BITS : \
|
||||
CAN_FRAME_HEADER_SFF_BITS) + \
|
||||
(data_len) * BITS_PER_BYTE + \
|
||||
CAN_FRAME_CRC_FIELD_BITS) + \
|
||||
CAN_FRAME_FOOTER_BITS + \
|
||||
(intermission ? CAN_INTERMISSION_BITS : 0) \
|
||||
)
|
||||
|
||||
#define __can_fd_frame_bits(is_eff, bitstuffing, \
|
||||
intermission, data_len) \
|
||||
( \
|
||||
__can_bitstuffing_len(bitstuffing, \
|
||||
(is_eff ? CANFD_FRAME_HEADER_EFF_BITS : \
|
||||
CANFD_FRAME_HEADER_SFF_BITS) + \
|
||||
(data_len) * BITS_PER_BYTE) + \
|
||||
((data_len) <= 16 ? \
|
||||
CANFD_FRAME_CRC17_FIELD_BITS : \
|
||||
CANFD_FRAME_CRC21_FIELD_BITS) + \
|
||||
CAN_FRAME_FOOTER_BITS + \
|
||||
(intermission ? CAN_INTERMISSION_BITS : 0) \
|
||||
)
|
||||
|
||||
/**
|
||||
* can_frame_bits() - Calculate the number of bits on the wire in a
|
||||
* CAN frame
|
||||
* @is_fd: true: CAN-FD frame; false: Classical CAN frame.
|
||||
* @is_eff: true: Extended frame; false: Standard frame.
|
||||
* @bitstuffing: true: calculate the bitstuffing worst case; false:
|
||||
* calculate the bitstuffing best case (no dynamic
|
||||
* bitstuffing). CAN-FD's fixed stuff bits are always included.
|
||||
* @intermission: if and only if true, include the inter frame space
|
||||
* assuming no bus idle (i.e. only the intermission). Strictly
|
||||
* speaking, the inter frame space is not part of the
|
||||
* frame. However, it is needed when calculating the delay
|
||||
* between the Start Of Frame of two consecutive frames.
|
||||
* @data_len: length of the data field in bytes. Correspond to
|
||||
* can(fd)_frame->len. Should be zero for remote frames. No
|
||||
* sanitization is done on @data_len and it shall have no side
|
||||
* effects.
|
||||
*
|
||||
* Return: the numbers of bits on the wire of a CAN frame.
|
||||
*/
|
||||
#define can_frame_bits(is_fd, is_eff, bitstuffing, \
|
||||
intermission, data_len) \
|
||||
( \
|
||||
is_fd ? __can_fd_frame_bits(is_eff, bitstuffing, \
|
||||
intermission, data_len) : \
|
||||
__can_cc_frame_bits(is_eff, bitstuffing, \
|
||||
intermission, data_len) \
|
||||
)
|
||||
|
||||
/*
|
||||
* Size of a CAN-FD Standard Frame
|
||||
*
|
||||
* Name of Field Bits
|
||||
* ---------------------------------------------------------
|
||||
* Start-of-frame 1
|
||||
* Identifier 11
|
||||
* Reserved bit (r1) 1
|
||||
* Identifier extension bit (IDE) 1
|
||||
* Flexible data rate format (FDF) 1
|
||||
* Reserved bit (r0) 1
|
||||
* Bit Rate Switch (BRS) 1
|
||||
* Error Status Indicator (ESI) 1
|
||||
* Data length code (DLC) 4
|
||||
* Data field 0...512
|
||||
* 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 dynamic bitstuffing
|
||||
* Number of bytes in a CAN frame
|
||||
* (rounded up, including intermission)
|
||||
*/
|
||||
#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(67, 8)
|
||||
|
||||
/*
|
||||
* Size of a CAN-FD Extended Frame
|
||||
*
|
||||
* Name of Field Bits
|
||||
* ---------------------------------------------------------
|
||||
* Start-of-frame 1
|
||||
* Identifier A 11
|
||||
* Substitute remote request (SRR) 1
|
||||
* Identifier extension bit (IDE) 1
|
||||
* Identifier B 18
|
||||
* Reserved bit (r1) 1
|
||||
* Flexible data rate format (FDF) 1
|
||||
* Reserved bit (r0) 1
|
||||
* Bit Rate Switch (BRS) 1
|
||||
* Error Status Indicator (ESI) 1
|
||||
* Data length code (DLC) 4
|
||||
* Data field 0...512
|
||||
* 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 dynamic bitstuffing
|
||||
*/
|
||||
#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(86, 8)
|
||||
#define can_frame_bytes(is_fd, is_eff, bitstuffing, data_len) \
|
||||
DIV_ROUND_UP(can_frame_bits(is_fd, is_eff, bitstuffing, \
|
||||
true, data_len), \
|
||||
BITS_PER_BYTE)
|
||||
|
||||
/*
|
||||
* Maximum size of a Classical CAN frame
|
||||
* (rounded up and ignoring bitstuffing)
|
||||
* (rounded up, ignoring bitstuffing but including intermission)
|
||||
*/
|
||||
#define CAN_FRAME_LEN_MAX (CAN_FRAME_OVERHEAD_EFF + CAN_MAX_DLEN)
|
||||
#define CAN_FRAME_LEN_MAX can_frame_bytes(false, true, false, CAN_MAX_DLEN)
|
||||
|
||||
/*
|
||||
* Maximum size of a CAN-FD frame
|
||||
* (rounded up and ignoring bitstuffing)
|
||||
* (rounded up, ignoring dynamic bitstuffing but including intermission)
|
||||
*/
|
||||
#define CANFD_FRAME_LEN_MAX (CANFD_FRAME_OVERHEAD_EFF + CANFD_MAX_DLEN)
|
||||
#define CANFD_FRAME_LEN_MAX can_frame_bytes(true, true, false, CANFD_MAX_DLEN)
|
||||
|
||||
/*
|
||||
* can_cc_dlc2len(value) - convert a given data length code (dlc) of a
|
||||
|
Loading…
Reference in New Issue
Block a user