1 /* SPDX-License-Identifier: GPL-2.0 */ 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* Copyright (C) 2020 Oliver Hartkopp <socketc 2 /* Copyright (C) 2020 Oliver Hartkopp <socketcan@hartkopp.net> 3 * Copyright (C) 2020 Marc Kleine-Budde <kerne 3 * Copyright (C) 2020 Marc Kleine-Budde <kernel@pengutronix.de> 4 * Copyright (C) 2020, 2023 Vincent Mailhol <m 4 * Copyright (C) 2020, 2023 Vincent Mailhol <mailhol.vincent@wanadoo.fr> 5 */ 5 */ 6 6 7 #ifndef _CAN_LENGTH_H 7 #ifndef _CAN_LENGTH_H 8 #define _CAN_LENGTH_H 8 #define _CAN_LENGTH_H 9 9 10 #include <linux/bits.h> 10 #include <linux/bits.h> 11 #include <linux/can.h> 11 #include <linux/can.h> 12 #include <linux/can/netlink.h> 12 #include <linux/can/netlink.h> 13 #include <linux/math.h> 13 #include <linux/math.h> 14 14 15 /* 15 /* 16 * Size of a Classical CAN Standard Frame head 16 * Size of a Classical CAN Standard Frame header in bits 17 * 17 * 18 * Name of Field 18 * Name of Field Bits 19 * ------------------------------------------- 19 * --------------------------------------------------------- 20 * Start Of Frame (SOF) 20 * Start Of Frame (SOF) 1 21 * Arbitration field: 21 * Arbitration field: 22 * base ID 22 * base ID 11 23 * Remote Transmission Request (RTR) 23 * Remote Transmission Request (RTR) 1 24 * Control field: 24 * Control field: 25 * IDentifier Extension bit (IDE) 25 * IDentifier Extension bit (IDE) 1 26 * FD Format indicator (FDF) 26 * FD Format indicator (FDF) 1 27 * Data Length Code (DLC) 27 * Data Length Code (DLC) 4 28 * 28 * 29 * including all fields preceding the data fie 29 * including all fields preceding the data field, ignoring bitstuffing 30 */ 30 */ 31 #define CAN_FRAME_HEADER_SFF_BITS 19 31 #define CAN_FRAME_HEADER_SFF_BITS 19 32 32 33 /* 33 /* 34 * Size of a Classical CAN Extended Frame head 34 * Size of a Classical CAN Extended Frame header in bits 35 * 35 * 36 * Name of Field 36 * Name of Field Bits 37 * ------------------------------------------- 37 * --------------------------------------------------------- 38 * Start Of Frame (SOF) 38 * Start Of Frame (SOF) 1 39 * Arbitration field: 39 * Arbitration field: 40 * base ID 40 * base ID 11 41 * Substitute Remote Request (SRR) 41 * Substitute Remote Request (SRR) 1 42 * IDentifier Extension bit (IDE) 42 * IDentifier Extension bit (IDE) 1 43 * ID extension 43 * ID extension 18 44 * Remote Transmission Request (RTR) 44 * Remote Transmission Request (RTR) 1 45 * Control field: 45 * Control field: 46 * FD Format indicator (FDF) 46 * FD Format indicator (FDF) 1 47 * Reserved bit (r0) 47 * Reserved bit (r0) 1 48 * Data length code (DLC) 48 * Data length code (DLC) 4 49 * 49 * 50 * including all fields preceding the data fie 50 * including all fields preceding the data field, ignoring bitstuffing 51 */ 51 */ 52 #define CAN_FRAME_HEADER_EFF_BITS 39 52 #define CAN_FRAME_HEADER_EFF_BITS 39 53 53 54 /* 54 /* 55 * Size of a CAN-FD Standard Frame in bits 55 * Size of a CAN-FD Standard Frame in bits 56 * 56 * 57 * Name of Field 57 * Name of Field Bits 58 * ------------------------------------------- 58 * --------------------------------------------------------- 59 * Start Of Frame (SOF) 59 * Start Of Frame (SOF) 1 60 * Arbitration field: 60 * Arbitration field: 61 * base ID 61 * base ID 11 62 * Remote Request Substitution (RRS) 62 * Remote Request Substitution (RRS) 1 63 * Control field: 63 * Control field: 64 * IDentifier Extension bit (IDE) 64 * IDentifier Extension bit (IDE) 1 65 * FD Format indicator (FDF) 65 * FD Format indicator (FDF) 1 66 * Reserved bit (res) 66 * Reserved bit (res) 1 67 * Bit Rate Switch (BRS) 67 * Bit Rate Switch (BRS) 1 68 * Error Status Indicator (ESI) 68 * Error Status Indicator (ESI) 1 69 * Data length code (DLC) 69 * Data length code (DLC) 4 70 * 70 * 71 * including all fields preceding the data fie 71 * including all fields preceding the data field, ignoring bitstuffing 72 */ 72 */ 73 #define CANFD_FRAME_HEADER_SFF_BITS 22 73 #define CANFD_FRAME_HEADER_SFF_BITS 22 74 74 75 /* 75 /* 76 * Size of a CAN-FD Extended Frame in bits 76 * Size of a CAN-FD Extended Frame in bits 77 * 77 * 78 * Name of Field 78 * Name of Field Bits 79 * ------------------------------------------- 79 * --------------------------------------------------------- 80 * Start Of Frame (SOF) 80 * Start Of Frame (SOF) 1 81 * Arbitration field: 81 * Arbitration field: 82 * base ID 82 * base ID 11 83 * Substitute Remote Request (SRR) 83 * Substitute Remote Request (SRR) 1 84 * IDentifier Extension bit (IDE) 84 * IDentifier Extension bit (IDE) 1 85 * ID extension 85 * ID extension 18 86 * Remote Request Substitution (RRS) 86 * Remote Request Substitution (RRS) 1 87 * Control field: 87 * Control field: 88 * FD Format indicator (FDF) 88 * FD Format indicator (FDF) 1 89 * Reserved bit (res) 89 * Reserved bit (res) 1 90 * Bit Rate Switch (BRS) 90 * Bit Rate Switch (BRS) 1 91 * Error Status Indicator (ESI) 91 * Error Status Indicator (ESI) 1 92 * Data length code (DLC) 92 * Data length code (DLC) 4 93 * 93 * 94 * including all fields preceding the data fie 94 * including all fields preceding the data field, ignoring bitstuffing 95 */ 95 */ 96 #define CANFD_FRAME_HEADER_EFF_BITS 41 96 #define CANFD_FRAME_HEADER_EFF_BITS 41 97 97 98 /* 98 /* 99 * Size of a CAN CRC Field in bits 99 * Size of a CAN CRC Field in bits 100 * 100 * 101 * Name of Field Bits 101 * Name of Field Bits 102 * ------------------------------------------- 102 * --------------------------------------------------------- 103 * CRC sequence (CRC15) 15 103 * CRC sequence (CRC15) 15 104 * CRC Delimiter 1 104 * CRC Delimiter 1 105 * 105 * 106 * ignoring bitstuffing 106 * ignoring bitstuffing 107 */ 107 */ 108 #define CAN_FRAME_CRC_FIELD_BITS 16 108 #define CAN_FRAME_CRC_FIELD_BITS 16 109 109 110 /* 110 /* 111 * Size of a CAN-FD CRC17 Field in bits (lengt 111 * Size of a CAN-FD CRC17 Field in bits (length: 0..16) 112 * 112 * 113 * Name of Field Bits 113 * Name of Field Bits 114 * ------------------------------------------- 114 * --------------------------------------------------------- 115 * Stuff Count 4 115 * Stuff Count 4 116 * CRC Sequence (CRC17) 17 116 * CRC Sequence (CRC17) 17 117 * CRC Delimiter 1 117 * CRC Delimiter 1 118 * Fixed stuff bits 6 118 * Fixed stuff bits 6 119 */ 119 */ 120 #define CANFD_FRAME_CRC17_FIELD_BITS 28 120 #define CANFD_FRAME_CRC17_FIELD_BITS 28 121 121 122 /* 122 /* 123 * Size of a CAN-FD CRC21 Field in bits (lengt 123 * Size of a CAN-FD CRC21 Field in bits (length: 20..64) 124 * 124 * 125 * Name of Field Bits 125 * Name of Field Bits 126 * ------------------------------------------- 126 * --------------------------------------------------------- 127 * Stuff Count 4 127 * Stuff Count 4 128 * CRC sequence (CRC21) 21 128 * CRC sequence (CRC21) 21 129 * CRC Delimiter 1 129 * CRC Delimiter 1 130 * Fixed stuff bits 7 130 * Fixed stuff bits 7 131 */ 131 */ 132 #define CANFD_FRAME_CRC21_FIELD_BITS 33 132 #define CANFD_FRAME_CRC21_FIELD_BITS 33 133 133 134 /* 134 /* 135 * Size of a CAN(-FD) Frame footer in bits 135 * Size of a CAN(-FD) Frame footer in bits 136 * 136 * 137 * Name of Field Bits 137 * Name of Field Bits 138 * ------------------------------------------- 138 * --------------------------------------------------------- 139 * ACK slot 1 139 * ACK slot 1 140 * ACK delimiter 1 140 * ACK delimiter 1 141 * End Of Frame (EOF) 7 141 * End Of Frame (EOF) 7 142 * 142 * 143 * including all fields following the CRC fiel 143 * including all fields following the CRC field 144 */ 144 */ 145 #define CAN_FRAME_FOOTER_BITS 9 145 #define CAN_FRAME_FOOTER_BITS 9 146 146 147 /* 147 /* 148 * First part of the Inter Frame Space 148 * First part of the Inter Frame Space 149 * (a.k.a. IMF - intermission field) 149 * (a.k.a. IMF - intermission field) 150 */ 150 */ 151 #define CAN_INTERMISSION_BITS 3 151 #define CAN_INTERMISSION_BITS 3 152 152 153 /** 153 /** 154 * can_bitstuffing_len() - Calculate the maxim 154 * can_bitstuffing_len() - Calculate the maximum length with bitstuffing 155 * @destuffed_len: length of a destuffed bit s 155 * @destuffed_len: length of a destuffed bit stream 156 * 156 * 157 * The worst bit stuffing case is a sequence i 157 * The worst bit stuffing case is a sequence in which dominant and 158 * recessive bits alternate every four bits: 158 * recessive bits alternate every four bits: 159 * 159 * 160 * Destuffed: 1 1111 0000 1111 0000 1111 160 * Destuffed: 1 1111 0000 1111 0000 1111 161 * Stuffed: 1 1111o 0000i 1111o 0000i 1111 161 * Stuffed: 1 1111o 0000i 1111o 0000i 1111o 162 * 162 * 163 * Nomenclature 163 * Nomenclature 164 * 164 * 165 * - "": dominant bit 165 * - "": dominant bit 166 * - "o": dominant stuff bit 166 * - "o": dominant stuff bit 167 * - "1": recessive bit 167 * - "1": recessive bit 168 * - "i": recessive stuff bit 168 * - "i": recessive stuff bit 169 * 169 * 170 * Aside from the first bit, one stuff bit is 170 * Aside from the first bit, one stuff bit is added every four bits. 171 * 171 * 172 * Return: length of the stuffed bit stream in 172 * Return: length of the stuffed bit stream in the worst case scenario. 173 */ 173 */ 174 #define can_bitstuffing_len(destuffed_len) 174 #define can_bitstuffing_len(destuffed_len) \ 175 (destuffed_len + (destuffed_len - 1) / 175 (destuffed_len + (destuffed_len - 1) / 4) 176 176 177 #define __can_bitstuffing_len(bitstuffing, des 177 #define __can_bitstuffing_len(bitstuffing, destuffed_len) \ 178 (bitstuffing ? can_bitstuffing_len(des 178 (bitstuffing ? can_bitstuffing_len(destuffed_len) : \ 179 destuffed_len) 179 destuffed_len) 180 180 181 #define __can_cc_frame_bits(is_eff, bitstuffin 181 #define __can_cc_frame_bits(is_eff, bitstuffing, \ 182 intermission, data 182 intermission, data_len) \ 183 ( 183 ( \ 184 __can_bitstuffing_len(bitstuffing, 184 __can_bitstuffing_len(bitstuffing, \ 185 (is_eff ? CAN_FRAME_HEADER_EFF 185 (is_eff ? CAN_FRAME_HEADER_EFF_BITS : \ 186 CAN_FRAME_HEADER_SFF 186 CAN_FRAME_HEADER_SFF_BITS) + \ 187 (data_len) * BITS_PER_BYTE + 187 (data_len) * BITS_PER_BYTE + \ 188 CAN_FRAME_CRC_FIELD_BITS) + 188 CAN_FRAME_CRC_FIELD_BITS) + \ 189 CAN_FRAME_FOOTER_BITS + 189 CAN_FRAME_FOOTER_BITS + \ 190 (intermission ? CAN_INTERMISSION_BITS 190 (intermission ? CAN_INTERMISSION_BITS : 0) \ 191 ) 191 ) 192 192 193 #define __can_fd_frame_bits(is_eff, bitstuffin 193 #define __can_fd_frame_bits(is_eff, bitstuffing, \ 194 intermission, data 194 intermission, data_len) \ 195 ( 195 ( \ 196 __can_bitstuffing_len(bitstuffing, 196 __can_bitstuffing_len(bitstuffing, \ 197 (is_eff ? CANFD_FRAME_HEADER_E 197 (is_eff ? CANFD_FRAME_HEADER_EFF_BITS : \ 198 CANFD_FRAME_HEADER_S 198 CANFD_FRAME_HEADER_SFF_BITS) + \ 199 (data_len) * BITS_PER_BYTE) + 199 (data_len) * BITS_PER_BYTE) + \ 200 ((data_len) <= 16 ? 200 ((data_len) <= 16 ? \ 201 CANFD_FRAME_CRC17_FIELD_BITS : 201 CANFD_FRAME_CRC17_FIELD_BITS : \ 202 CANFD_FRAME_CRC21_FIELD_BITS) 202 CANFD_FRAME_CRC21_FIELD_BITS) + \ 203 CAN_FRAME_FOOTER_BITS + 203 CAN_FRAME_FOOTER_BITS + \ 204 (intermission ? CAN_INTERMISSION_BITS 204 (intermission ? CAN_INTERMISSION_BITS : 0) \ 205 ) 205 ) 206 206 207 /** 207 /** 208 * can_frame_bits() - Calculate the number of 208 * can_frame_bits() - Calculate the number of bits on the wire in a 209 * CAN frame 209 * CAN frame 210 * @is_fd: true: CAN-FD frame; false: Classica 210 * @is_fd: true: CAN-FD frame; false: Classical CAN frame. 211 * @is_eff: true: Extended frame; false: Stand 211 * @is_eff: true: Extended frame; false: Standard frame. 212 * @bitstuffing: true: calculate the bitstuffi 212 * @bitstuffing: true: calculate the bitstuffing worst case; false: 213 * calculate the bitstuffing best case (n 213 * calculate the bitstuffing best case (no dynamic 214 * bitstuffing). CAN-FD's fixed stuff bit 214 * bitstuffing). CAN-FD's fixed stuff bits are always included. 215 * @intermission: if and only if true, include 215 * @intermission: if and only if true, include the inter frame space 216 * assuming no bus idle (i.e. only the in 216 * assuming no bus idle (i.e. only the intermission). Strictly 217 * speaking, the inter frame space is not 217 * speaking, the inter frame space is not part of the 218 * frame. However, it is needed when calc 218 * frame. However, it is needed when calculating the delay 219 * between the Start Of Frame of two cons 219 * between the Start Of Frame of two consecutive frames. 220 * @data_len: length of the data field in byte 220 * @data_len: length of the data field in bytes. Correspond to 221 * can(fd)_frame->len. Should be zero for 221 * can(fd)_frame->len. Should be zero for remote frames. No 222 * sanitization is done on @data_len and 222 * sanitization is done on @data_len and it shall have no side 223 * effects. 223 * effects. 224 * 224 * 225 * Return: the numbers of bits on the wire of 225 * Return: the numbers of bits on the wire of a CAN frame. 226 */ 226 */ 227 #define can_frame_bits(is_fd, is_eff, bitstuff 227 #define can_frame_bits(is_fd, is_eff, bitstuffing, \ 228 intermission, data_len) 228 intermission, data_len) \ 229 ( 229 ( \ 230 is_fd ? __can_fd_frame_bits(is_eff, bi 230 is_fd ? __can_fd_frame_bits(is_eff, bitstuffing, \ 231 intermissi 231 intermission, data_len) : \ 232 __can_cc_frame_bits(is_eff, bi 232 __can_cc_frame_bits(is_eff, bitstuffing, \ 233 intermissi 233 intermission, data_len) \ 234 ) 234 ) 235 235 236 /* 236 /* 237 * Number of bytes in a CAN frame 237 * Number of bytes in a CAN frame 238 * (rounded up, including intermission) 238 * (rounded up, including intermission) 239 */ 239 */ 240 #define can_frame_bytes(is_fd, is_eff, bitstuf 240 #define can_frame_bytes(is_fd, is_eff, bitstuffing, data_len) \ 241 DIV_ROUND_UP(can_frame_bits(is_fd, is_ 241 DIV_ROUND_UP(can_frame_bits(is_fd, is_eff, bitstuffing, \ 242 true, data 242 true, data_len), \ 243 BITS_PER_BYTE) 243 BITS_PER_BYTE) 244 244 245 /* 245 /* 246 * Maximum size of a Classical CAN frame 246 * Maximum size of a Classical CAN frame 247 * (rounded up, ignoring bitstuffing but inclu 247 * (rounded up, ignoring bitstuffing but including intermission) 248 */ 248 */ 249 #define CAN_FRAME_LEN_MAX can_frame_bytes(fals 249 #define CAN_FRAME_LEN_MAX can_frame_bytes(false, true, false, CAN_MAX_DLEN) 250 250 251 /* 251 /* 252 * Maximum size of a CAN-FD frame 252 * Maximum size of a CAN-FD frame 253 * (rounded up, ignoring dynamic bitstuffing b 253 * (rounded up, ignoring dynamic bitstuffing but including intermission) 254 */ 254 */ 255 #define CANFD_FRAME_LEN_MAX can_frame_bytes(tr 255 #define CANFD_FRAME_LEN_MAX can_frame_bytes(true, true, false, CANFD_MAX_DLEN) 256 256 257 /* 257 /* 258 * can_cc_dlc2len(value) - convert a given dat 258 * can_cc_dlc2len(value) - convert a given data length code (dlc) of a 259 * Classical CAN frame into a valid data lengt 259 * Classical CAN frame into a valid data length of max. 8 bytes. 260 * 260 * 261 * To be used in the CAN netdriver receive pat 261 * To be used in the CAN netdriver receive path to ensure conformance with 262 * ISO 11898-1 Chapter 8.4.2.3 (DLC field) 262 * ISO 11898-1 Chapter 8.4.2.3 (DLC field) 263 */ 263 */ 264 #define can_cc_dlc2len(dlc) (min_t(u8, (dl 264 #define can_cc_dlc2len(dlc) (min_t(u8, (dlc), CAN_MAX_DLEN)) 265 265 266 /* helper to get the data length code (DLC) fo 266 /* helper to get the data length code (DLC) for Classical CAN raw DLC access */ 267 static inline u8 can_get_cc_dlc(const struct c 267 static inline u8 can_get_cc_dlc(const struct can_frame *cf, const u32 ctrlmode) 268 { 268 { 269 /* return len8_dlc as dlc value only i 269 /* return len8_dlc as dlc value only if all conditions apply */ 270 if ((ctrlmode & CAN_CTRLMODE_CC_LEN8_D 270 if ((ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC) && 271 (cf->len == CAN_MAX_DLEN) && 271 (cf->len == CAN_MAX_DLEN) && 272 (cf->len8_dlc > CAN_MAX_DLEN && cf 272 (cf->len8_dlc > CAN_MAX_DLEN && cf->len8_dlc <= CAN_MAX_RAW_DLC)) 273 return cf->len8_dlc; 273 return cf->len8_dlc; 274 274 275 /* return the payload length as dlc va 275 /* return the payload length as dlc value */ 276 return cf->len; 276 return cf->len; 277 } 277 } 278 278 279 /* helper to set len and len8_dlc value for Cl 279 /* helper to set len and len8_dlc value for Classical CAN raw DLC access */ 280 static inline void can_frame_set_cc_len(struct 280 static inline void can_frame_set_cc_len(struct can_frame *cf, const u8 dlc, 281 const 281 const u32 ctrlmode) 282 { 282 { 283 /* the caller already ensured that dlc 283 /* the caller already ensured that dlc is a value from 0 .. 15 */ 284 if (ctrlmode & CAN_CTRLMODE_CC_LEN8_DL 284 if (ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC && dlc > CAN_MAX_DLEN) 285 cf->len8_dlc = dlc; 285 cf->len8_dlc = dlc; 286 286 287 /* limit the payload length 'len' to C 287 /* limit the payload length 'len' to CAN_MAX_DLEN */ 288 cf->len = can_cc_dlc2len(dlc); 288 cf->len = can_cc_dlc2len(dlc); 289 } 289 } 290 290 291 /* get data length from raw data length code ( 291 /* get data length from raw data length code (DLC) */ 292 u8 can_fd_dlc2len(u8 dlc); 292 u8 can_fd_dlc2len(u8 dlc); 293 293 294 /* map the sanitized data length to an appropr 294 /* map the sanitized data length to an appropriate data length code */ 295 u8 can_fd_len2dlc(u8 len); 295 u8 can_fd_len2dlc(u8 len); 296 296 297 /* calculate the CAN Frame length in bytes of 297 /* calculate the CAN Frame length in bytes of a given skb */ 298 unsigned int can_skb_get_frame_len(const struc 298 unsigned int can_skb_get_frame_len(const struct sk_buff *skb); 299 299 300 /* map the data length to an appropriate data 300 /* map the data length to an appropriate data link layer length */ 301 static inline u8 canfd_sanitize_len(u8 len) 301 static inline u8 canfd_sanitize_len(u8 len) 302 { 302 { 303 return can_fd_dlc2len(can_fd_len2dlc(l 303 return can_fd_dlc2len(can_fd_len2dlc(len)); 304 } 304 } 305 305 306 #endif /* !_CAN_LENGTH_H */ 306 #endif /* !_CAN_LENGTH_H */ 307 307
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.