1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2020 Oliver Hartkopp <socketcan@hartkopp.net>
3 * Copyright (C) 2020 Marc Kleine-Budde <kernel@pengutronix.de>
4 */
5
6 #ifndef _CAN_LENGTH_H
7 #define _CAN_LENGTH_H
8
9 /*
10 * Size of a Classical CAN Standard Frame
11 *
12 * Name of Field Bits
13 * ---------------------------------------------------------
14 * Start-of-frame 1
15 * Identifier 11
16 * Remote transmission request (RTR) 1
17 * Identifier extension bit (IDE) 1
18 * Reserved bit (r0) 1
19 * Data length code (DLC) 4
20 * Data field 0...64
21 * CRC 15
22 * CRC delimiter 1
23 * ACK slot 1
24 * ACK delimiter 1
25 * End-of-frame (EOF) 7
26 * Inter frame spacing 3
27 *
28 * rounded up and ignoring bitstuffing
29 */
30 #define CAN_FRAME_OVERHEAD_SFF DIV_ROUND_UP(47, 8)
31
32 /*
33 * Size of a Classical CAN Extended Frame
34 *
35 * Name of Field Bits
36 * ---------------------------------------------------------
37 * Start-of-frame 1
38 * Identifier A 11
39 * Substitute remote request (SRR) 1
40 * Identifier extension bit (IDE) 1
41 * Identifier B 18
42 * Remote transmission request (RTR) 1
43 * Reserved bits (r1, r0) 2
44 * Data length code (DLC) 4
45 * Data field 0...64
46 * CRC 15
47 * CRC delimiter 1
48 * ACK slot 1
49 * ACK delimiter 1
50 * End-of-frame (EOF) 7
51 * Inter frame spacing 3
52 *
53 * rounded up and ignoring bitstuffing
54 */
55 #define CAN_FRAME_OVERHEAD_EFF DIV_ROUND_UP(67, 8)
56
57 /*
58 * Size of a CAN-FD Standard Frame
59 *
60 * Name of Field Bits
61 * ---------------------------------------------------------
62 * Start-of-frame 1
63 * Identifier 11
64 * Reserved bit (r1) 1
65 * Identifier extension bit (IDE) 1
66 * Flexible data rate format (FDF) 1
67 * Reserved bit (r0) 1
68 * Bit Rate Switch (BRS) 1
69 * Error Status Indicator (ESI) 1
70 * Data length code (DLC) 4
71 * Data field 0...512
72 * Stuff Bit Count (SBC) 4
73 * CRC 0...16: 17 20...64:21
74 * CRC delimiter (CD) 1
75 * Fixed Stuff bits (FSB) 0...16: 6 20...64:7
76 * ACK slot (AS) 1
77 * ACK delimiter (AD) 1
78 * End-of-frame (EOF) 7
79 * Inter frame spacing 3
80 *
81 * assuming CRC21, rounded up and ignoring dynamic bitstuffing
82 */
83 #define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(67, 8)
84
85 /*
86 * Size of a CAN-FD Extended Frame
87 *
88 * Name of Field Bits
89 * ---------------------------------------------------------
90 * Start-of-frame 1
91 * Identifier A 11
92 * Substitute remote request (SRR) 1
93 * Identifier extension bit (IDE) 1
94 * Identifier B 18
95 * Reserved bit (r1) 1
96 * Flexible data rate format (FDF) 1
97 * Reserved bit (r0) 1
98 * Bit Rate Switch (BRS) 1
99 * Error Status Indicator (ESI) 1
100 * Data length code (DLC) 4
101 * Data field 0...512
102 * Stuff Bit Count (SBC) 4
103 * CRC 0...16: 17 20...64:21
104 * CRC delimiter (CD) 1
105 * Fixed Stuff bits (FSB) 0...16: 6 20...64:7
106 * ACK slot (AS) 1
107 * ACK delimiter (AD) 1
108 * End-of-frame (EOF) 7
109 * Inter frame spacing 3
110 *
111 * assuming CRC21, rounded up and ignoring dynamic bitstuffing
112 */
113 #define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(86, 8)
114
115 /*
116 * Maximum size of a Classical CAN frame
117 * (rounded up and ignoring bitstuffing)
118 */
119 #define CAN_FRAME_LEN_MAX (CAN_FRAME_OVERHEAD_EFF + CAN_MAX_DLEN)
120
121 /*
122 * Maximum size of a CAN-FD frame
123 * (rounded up and ignoring bitstuffing)
124 */
125 #define CANFD_FRAME_LEN_MAX (CANFD_FRAME_OVERHEAD_EFF + CANFD_MAX_DLEN)
126
127 /*
128 * can_cc_dlc2len(value) - convert a given data length code (dlc) of a
129 * Classical CAN frame into a valid data length of max. 8 bytes.
130 *
131 * To be used in the CAN netdriver receive path to ensure conformance with
132 * ISO 11898-1 Chapter 8.4.2.3 (DLC field)
133 */
134 #define can_cc_dlc2len(dlc) (min_t(u8, (dlc), CAN_MAX_DLEN))
135
136 /* helper to get the data length code (DLC) for Classical CAN raw DLC access */
can_get_cc_dlc(const struct can_frame * cf,const u32 ctrlmode)137 static inline u8 can_get_cc_dlc(const struct can_frame *cf, const u32 ctrlmode)
138 {
139 /* return len8_dlc as dlc value only if all conditions apply */
140 if ((ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC) &&
141 (cf->len == CAN_MAX_DLEN) &&
142 (cf->len8_dlc > CAN_MAX_DLEN && cf->len8_dlc <= CAN_MAX_RAW_DLC))
143 return cf->len8_dlc;
144
145 /* return the payload length as dlc value */
146 return cf->len;
147 }
148
149 /* helper to set len and len8_dlc value for Classical CAN raw DLC access */
can_frame_set_cc_len(struct can_frame * cf,const u8 dlc,const u32 ctrlmode)150 static inline void can_frame_set_cc_len(struct can_frame *cf, const u8 dlc,
151 const u32 ctrlmode)
152 {
153 /* the caller already ensured that dlc is a value from 0 .. 15 */
154 if (ctrlmode & CAN_CTRLMODE_CC_LEN8_DLC && dlc > CAN_MAX_DLEN)
155 cf->len8_dlc = dlc;
156
157 /* limit the payload length 'len' to CAN_MAX_DLEN */
158 cf->len = can_cc_dlc2len(dlc);
159 }
160
161 /* get data length from raw data length code (DLC) */
162 u8 can_fd_dlc2len(u8 dlc);
163
164 /* map the sanitized data length to an appropriate data length code */
165 u8 can_fd_len2dlc(u8 len);
166
167 /* calculate the CAN Frame length in bytes of a given skb */
168 unsigned int can_skb_get_frame_len(const struct sk_buff *skb);
169
170 /* map the data length to an appropriate data link layer length */
canfd_sanitize_len(u8 len)171 static inline u8 canfd_sanitize_len(u8 len)
172 {
173 return can_fd_dlc2len(can_fd_len2dlc(len));
174 }
175
176 #endif /* !_CAN_LENGTH_H */
177