Merge tag 'mt76-for-kvalo-2021-01-29' of https://github.com/nbd168/wireless
[linux-2.6-microblaze.git] / drivers / net / can / dev / length.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2012, 2020 Oliver Hartkopp <socketcan@hartkopp.net>
3  */
4
5 #include <linux/can/dev.h>
6
7 /* CAN DLC to real data length conversion helpers */
8
9 static const u8 dlc2len[] = {
10         0, 1, 2, 3, 4, 5, 6, 7,
11         8, 12, 16, 20, 24, 32, 48, 64
12 };
13
14 /* get data length from raw data length code (DLC) */
15 u8 can_fd_dlc2len(u8 dlc)
16 {
17         return dlc2len[dlc & 0x0F];
18 }
19 EXPORT_SYMBOL_GPL(can_fd_dlc2len);
20
21 static const u8 len2dlc[] = {
22         0, 1, 2, 3, 4, 5, 6, 7, 8,      /* 0 - 8 */
23         9, 9, 9, 9,                     /* 9 - 12 */
24         10, 10, 10, 10,                 /* 13 - 16 */
25         11, 11, 11, 11,                 /* 17 - 20 */
26         12, 12, 12, 12,                 /* 21 - 24 */
27         13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
28         14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
29         14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
30         15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
31         15, 15, 15, 15, 15, 15, 15, 15  /* 57 - 64 */
32 };
33
34 /* map the sanitized data length to an appropriate data length code */
35 u8 can_fd_len2dlc(u8 len)
36 {
37         /* check for length mapping table size at build time */
38         BUILD_BUG_ON(ARRAY_SIZE(len2dlc) != CANFD_MAX_DLEN + 1);
39
40         if (unlikely(len > CANFD_MAX_DLEN))
41                 return CANFD_MAX_DLC;
42
43         return len2dlc[len];
44 }
45 EXPORT_SYMBOL_GPL(can_fd_len2dlc);
46
47 /**
48  * can_skb_get_frame_len() - Calculate the CAN Frame length in bytes
49  *      of a given skb.
50  * @skb: socket buffer of a CAN message.
51  *
52  * Do a rough calculation: bit stuffing is ignored and length in bits
53  * is rounded up to a length in bytes.
54  *
55  * Rationale: this function is to be used for the BQL functions
56  * (netdev_sent_queue() and netdev_completed_queue()) which expect a
57  * value in bytes. Just using skb->len is insufficient because it will
58  * return the constant value of CAN(FD)_MTU. Doing the bit stuffing
59  * calculation would be too expensive in term of computing resources
60  * for no noticeable gain.
61  *
62  * Remarks: The payload of CAN FD frames with BRS flag are sent at a
63  * different bitrate. Currently, the can-utils canbusload tool does
64  * not support CAN-FD yet and so we could not run any benchmark to
65  * measure the impact. There might be possible improvement here.
66  *
67  * Return: length in bytes.
68  */
69 unsigned int can_skb_get_frame_len(const struct sk_buff *skb)
70 {
71         const struct canfd_frame *cf = (const struct canfd_frame *)skb->data;
72         u8 len;
73
74         if (can_is_canfd_skb(skb))
75                 len = canfd_sanitize_len(cf->len);
76         else if (cf->can_id & CAN_RTR_FLAG)
77                 len = 0;
78         else
79                 len = cf->len;
80
81         if (can_is_canfd_skb(skb)) {
82                 if (cf->can_id & CAN_EFF_FLAG)
83                         len += CANFD_FRAME_OVERHEAD_EFF;
84                 else
85                         len += CANFD_FRAME_OVERHEAD_SFF;
86         } else {
87                 if (cf->can_id & CAN_EFF_FLAG)
88                         len += CAN_FRAME_OVERHEAD_EFF;
89                 else
90                         len += CAN_FRAME_OVERHEAD_SFF;
91         }
92
93         return len;
94 }
95 EXPORT_SYMBOL_GPL(can_skb_get_frame_len);