Subject: Re: [PATCH v3 3/3] can: length: refactor frame lengths definition to add size in bits

From: Simon Horman <[email-masked]>
Date: Tue, 30 May 2023 17:51:05 +0200

On Tue, May 30, 2023 at 11:46:37PM +0900, Vincent Mailhol wrote:
> Introduce a method to calculate the exact size in bits of a CAN(-FD)
> frame with or without dynamic bitsuffing.
> 
> These are all the possible combinations taken into account:
> 
>   - Classical CAN or CAN-FD
>   - Standard or Extended frame format
>   - CAN-FD CRC17 or CRC21
>   - Include or not intermission
> 
> Instead of doing several individual macro definitions, declare the
> can_frame_bits() function-like macro. To this extent, do a full
> refactoring of the length definitions.
> 
> In addition add the can_frame_bytes(). This function-like macro
> replaces the existing macro:
> 
>   - CAN_FRAME_OVERHEAD_SFF: can_frame_bytes(false, false, 0)
>   - CAN_FRAME_OVERHEAD_EFF: can_frame_bytes(false, true, 0)
>   - CANFD_FRAME_OVERHEAD_SFF: can_frame_bytes(true, false, 0)
>   - CANFD_FRAME_OVERHEAD_EFF: can_frame_bytes(true, true, 0)
> 
> The different maximum frame lengths (maximum data length, including
> intermission) are as follow:
> 
>    Frame type				bits	bytes
>   -------------------------------------------------------
>    Classic CAN SFF no-bitstuffing	111	14
>    Classic CAN EFF no-bitstuffing	131	17
>    Classic CAN SFF bitstuffing		135	17
>    Classic CAN EFF bitstuffing		160	20
>    CAN-FD SFF no-bitstuffing		579	73
>    CAN-FD EFF no-bitstuffing		598	75
>    CAN-FD SFF bitstuffing		712	89
>    CAN-FD EFF bitstuffing		736	92
> 
> The macro CAN_FRAME_LEN_MAX and CANFD_FRAME_LEN_MAX are kept as an
> alias to, respectively, can_frame_bytes(false, true, CAN_MAX_DLEN) and
> can_frame_bytes(true, true, CANFD_MAX_DLEN).
> 
> In addition to the above:
> 
>  - Use ISO 11898-1:2015 definitions for the name of the CAN frame
>    fields.
>  - Include linux/bits.h for use of BITS_PER_BYTE.
>  - Include linux/math.h for use of mult_frac() and
>    DIV_ROUND_UP(). N.B: the use of DIV_ROUND_UP() is not new to this
>    patch, but the include was previously omitted.
>  - Add copyright 2023 for myself.
> 
> Signed-off-by: Vincent Mailhol <[email-masked]>

...

> +/**
> + * can_bitstuffing_len() - Calculate the maximum length with bitsuffing
> + * @bitstream_len: length of a destuffed bit stream

Hi Vincent,

it looks like an editing error has crept in here:

	s/bitstream_len/destuffed_len/

> + *
> + * 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 of 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)