length.c 2.5 KB

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