Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Copyright (C) 2012, 2020 Oliver Hartkopp <socketcan@hartkopp.net>
0003  */
0004 
0005 #include <linux/can/dev.h>
0006 
0007 /* CAN DLC to real data length conversion helpers */
0008 
0009 static const u8 dlc2len[] = {
0010     0, 1, 2, 3, 4, 5, 6, 7,
0011     8, 12, 16, 20, 24, 32, 48, 64
0012 };
0013 
0014 /* get data length from raw data length code (DLC) */
0015 u8 can_fd_dlc2len(u8 dlc)
0016 {
0017     return dlc2len[dlc & 0x0F];
0018 }
0019 EXPORT_SYMBOL_GPL(can_fd_dlc2len);
0020 
0021 static const u8 len2dlc[] = {
0022     0, 1, 2, 3, 4, 5, 6, 7, 8,  /* 0 - 8 */
0023     9, 9, 9, 9,         /* 9 - 12 */
0024     10, 10, 10, 10,         /* 13 - 16 */
0025     11, 11, 11, 11,         /* 17 - 20 */
0026     12, 12, 12, 12,         /* 21 - 24 */
0027     13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
0028     14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
0029     14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
0030     15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
0031     15, 15, 15, 15, 15, 15, 15, 15  /* 57 - 64 */
0032 };
0033 
0034 /* map the sanitized data length to an appropriate data length code */
0035 u8 can_fd_len2dlc(u8 len)
0036 {
0037     /* check for length mapping table size at build time */
0038     BUILD_BUG_ON(ARRAY_SIZE(len2dlc) != CANFD_MAX_DLEN + 1);
0039 
0040     if (unlikely(len > CANFD_MAX_DLEN))
0041         return CANFD_MAX_DLC;
0042 
0043     return len2dlc[len];
0044 }
0045 EXPORT_SYMBOL_GPL(can_fd_len2dlc);
0046 
0047 /**
0048  * can_skb_get_frame_len() - Calculate the CAN Frame length in bytes
0049  *  of a given skb.
0050  * @skb: socket buffer of a CAN message.
0051  *
0052  * Do a rough calculation: bit stuffing is ignored and length in bits
0053  * is rounded up to a length in bytes.
0054  *
0055  * Rationale: this function is to be used for the BQL functions
0056  * (netdev_sent_queue() and netdev_completed_queue()) which expect a
0057  * value in bytes. Just using skb->len is insufficient because it will
0058  * return the constant value of CAN(FD)_MTU. Doing the bit stuffing
0059  * calculation would be too expensive in term of computing resources
0060  * for no noticeable gain.
0061  *
0062  * Remarks: The payload of CAN FD frames with BRS flag are sent at a
0063  * different bitrate. Currently, the can-utils canbusload tool does
0064  * not support CAN-FD yet and so we could not run any benchmark to
0065  * measure the impact. There might be possible improvement here.
0066  *
0067  * Return: length in bytes.
0068  */
0069 unsigned int can_skb_get_frame_len(const struct sk_buff *skb)
0070 {
0071     const struct canfd_frame *cf = (const struct canfd_frame *)skb->data;
0072     u8 len;
0073 
0074     if (can_is_canfd_skb(skb))
0075         len = canfd_sanitize_len(cf->len);
0076     else if (cf->can_id & CAN_RTR_FLAG)
0077         len = 0;
0078     else
0079         len = cf->len;
0080 
0081     if (can_is_canfd_skb(skb)) {
0082         if (cf->can_id & CAN_EFF_FLAG)
0083             len += CANFD_FRAME_OVERHEAD_EFF;
0084         else
0085             len += CANFD_FRAME_OVERHEAD_SFF;
0086     } else {
0087         if (cf->can_id & CAN_EFF_FLAG)
0088             len += CAN_FRAME_OVERHEAD_EFF;
0089         else
0090             len += CAN_FRAME_OVERHEAD_SFF;
0091     }
0092 
0093     return len;
0094 }
0095 EXPORT_SYMBOL_GPL(can_skb_get_frame_len);